1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 ------------------------------------------------------------------------------
28 with Atree
; use Atree
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Exp_Smem
; use Exp_Smem
;
33 with Exp_Util
; use Exp_Util
;
34 with Exp_VFpt
; use Exp_VFpt
;
35 with Nmake
; use Nmake
;
37 with Sem_Res
; use Sem_Res
;
38 with Sem_Util
; use Sem_Util
;
39 with Sinfo
; use Sinfo
;
40 with Tbuild
; use Tbuild
;
41 with Snames
; use Snames
;
43 package body Exp_Ch2
is
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
49 procedure Expand_Discriminant
(N
: Node_Id
);
50 -- An occurrence of a discriminant within a discriminated type is replaced
51 -- with the corresponding discriminal, that is to say the formal parameter
52 -- of the initialization procedure for the type that is associated with
53 -- that particular discriminant. This replacement is not performed for
54 -- discriminants of records that appear in constraints of component of the
55 -- record, because Gigi uses the discriminant name to retrieve its value.
56 -- In the other hand, it has to be performed for default expressions of
57 -- components because they are used in the record init procedure. See
58 -- Einfo for more details, and Exp_Ch3, Exp_Ch9 for examples of use.
59 -- For discriminants of tasks and protected types, the transformation is
60 -- more complex when it occurs within a default expression for an entry
61 -- or protected operation. The corresponding default_expression_function
62 -- has an additional parameter which is the target of an entry call, and
63 -- the discriminant of the task must be replaced with a reference to the
64 -- discriminant of that formal parameter.
66 procedure Expand_Entity_Reference
(N
: Node_Id
);
67 -- Common processing for expansion of identifiers and expanded names
69 procedure Expand_Entry_Index_Parameter
(N
: Node_Id
);
70 -- A reference to the identifier in the entry index specification
71 -- of a protected entry body is modified to a reference to a constant
72 -- definintion equal to the index of the entry family member being
73 -- called. This constant is calculated as part of the elaboration
74 -- of the expanded code for the body, and is calculated from the
75 -- object-wide entry index returned by Next_Entry_Call.
77 procedure Expand_Entry_Parameter
(N
: Node_Id
);
78 -- A reference to an entry parameter is modified to be a reference to
79 -- the corresponding component of the entry parameter record that is
80 -- passed by the runtime to the accept body procedure
82 procedure Expand_Formal
(N
: Node_Id
);
83 -- A reference to a formal parameter of a protected subprogram is
84 -- expanded to the corresponding formal of the unprotected procedure
85 -- used to represent the protected subprogram within the protected object.
87 procedure Expand_Protected_Private
(N
: Node_Id
);
88 -- A reference to a private object of a protected type is expanded
89 -- to a component selected from the record used to implement
90 -- the protected object. Such a record is passed to all operations
91 -- on a protected object in a parameter named _object. Such an object
92 -- is a constant within a function, and a variable otherwise.
94 procedure Expand_Renaming
(N
: Node_Id
);
95 -- For renamings, just replace the identifier by the corresponding
96 -- name expression. Note that this has been evaluated (see routine
97 -- Exp_Ch8.Expand_N_Object_Renaming.Evaluate_Name) so this gives
98 -- the correct renaming semantics.
100 -------------------------
101 -- Expand_Discriminant --
102 -------------------------
104 procedure Expand_Discriminant
(N
: Node_Id
) is
105 Scop
: constant Entity_Id
:= Scope
(Entity
(N
));
107 Parent_P
: Node_Id
:= Parent
(P
);
108 In_Entry
: Boolean := False;
111 -- The Incomplete_Or_Private_Kind happens while resolving the
112 -- discriminant constraint involved in a derived full type,
115 -- type D is private;
116 -- type D(C : ...) is new T(C);
118 if Ekind
(Scop
) = E_Record_Type
119 or Ekind
(Scop
) in Incomplete_Or_Private_Kind
122 -- Find the origin by walking up the tree till the component
125 while Present
(Parent_P
)
126 and then Nkind
(Parent_P
) /= N_Component_Declaration
129 Parent_P
:= Parent
(P
);
132 -- If the discriminant reference was part of the default expression
133 -- it has to be "discriminalized"
135 if Present
(Parent_P
) and then P
= Expression
(Parent_P
) then
136 Set_Entity
(N
, Discriminal
(Entity
(N
)));
139 elsif Is_Concurrent_Type
(Scop
) then
140 while Present
(Parent_P
)
141 and then Nkind
(Parent_P
) /= N_Subprogram_Body
145 if Nkind
(P
) = N_Entry_Declaration
then
149 Parent_P
:= Parent
(Parent_P
);
152 -- If the discriminant occurs within the default expression for
153 -- a formal of an entry or protected operation, create a default
154 -- function for it, and replace the discriminant with a reference
155 -- to the discriminant of the formal of the default function.
156 -- The discriminant entity is the one defined in the corresponding
159 if Present
(Parent_P
)
160 and then Present
(Corresponding_Spec
(Parent_P
))
164 Loc
: constant Source_Ptr
:= Sloc
(N
);
165 D_Fun
: Entity_Id
:= Corresponding_Spec
(Parent_P
);
166 Formal
: Entity_Id
:= First_Formal
(D_Fun
);
171 -- Verify that we are within a default function: the type of
172 -- its formal parameter is the same task or protected type.
175 and then Etype
(Formal
) = Scope
(Entity
(N
))
177 Disc
:= CR_Discriminant
(Entity
(N
));
180 Make_Selected_Component
(Loc
,
181 Prefix
=> New_Occurrence_Of
(Formal
, Loc
),
182 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
));
184 Set_Etype
(New_N
, Etype
(N
));
188 Set_Entity
(N
, Discriminal
(Entity
(N
)));
192 elsif Nkind
(Parent
(N
)) = N_Range
195 Set_Entity
(N
, CR_Discriminant
(Entity
(N
)));
197 Set_Entity
(N
, Discriminal
(Entity
(N
)));
201 Set_Entity
(N
, Discriminal
(Entity
(N
)));
203 end Expand_Discriminant
;
205 -----------------------------
206 -- Expand_Entity_Reference --
207 -----------------------------
209 procedure Expand_Entity_Reference
(N
: Node_Id
) is
210 E
: constant Entity_Id
:= Entity
(N
);
213 -- Defend against errors
215 if No
(E
) and then Total_Errors_Detected
/= 0 then
219 if Ekind
(E
) = E_Discriminant
then
220 Expand_Discriminant
(N
);
222 elsif Is_Entry_Formal
(E
) then
223 Expand_Entry_Parameter
(N
);
225 elsif Ekind
(E
) = E_Component
226 and then Is_Protected_Private
(E
)
228 Expand_Protected_Private
(N
);
230 elsif Ekind
(E
) = E_Entry_Index_Parameter
then
231 Expand_Entry_Index_Parameter
(N
);
233 elsif Is_Formal
(E
) then
236 elsif Is_Renaming_Of_Object
(E
) then
239 elsif Ekind
(E
) = E_Variable
240 and then Is_Shared_Passive
(E
)
242 Expand_Shared_Passive_Variable
(N
);
244 end Expand_Entity_Reference
;
246 ----------------------------------
247 -- Expand_Entry_Index_Parameter --
248 ----------------------------------
250 procedure Expand_Entry_Index_Parameter
(N
: Node_Id
) is
252 Set_Entity
(N
, Entry_Index_Constant
(Entity
(N
)));
253 end Expand_Entry_Index_Parameter
;
255 ----------------------------
256 -- Expand_Entry_Parameter --
257 ----------------------------
259 procedure Expand_Entry_Parameter
(N
: Node_Id
) is
260 Loc
: constant Source_Ptr
:= Sloc
(N
);
261 Ent_Formal
: constant Entity_Id
:= Entity
(N
);
262 Ent_Spec
: constant Entity_Id
:= Scope
(Ent_Formal
);
263 Parm_Type
: constant Entity_Id
:= Entry_Parameters_Type
(Ent_Spec
);
264 Acc_Stack
: constant Elist_Id
:= Accept_Address
(Ent_Spec
);
265 Addr_Ent
: constant Entity_Id
:= Node
(Last_Elmt
(Acc_Stack
));
266 P_Comp_Ref
: Entity_Id
;
269 -- What we need is a reference to the corresponding component of the
270 -- parameter record object. The Accept_Address field of the entry
271 -- entity references the address variable that contains the address
272 -- of the accept parameters record. We first have to do an unchecked
273 -- conversion to turn this into a pointer to the parameter record and
274 -- then we select the required parameter field.
277 Make_Selected_Component
(Loc
,
279 Unchecked_Convert_To
(Parm_Type
,
280 New_Reference_To
(Addr_Ent
, Loc
)),
282 New_Reference_To
(Entry_Component
(Ent_Formal
), Loc
));
284 -- For all types of parameters, the constructed parameter record
285 -- object contains a pointer to the parameter. Thus we must
286 -- dereference them to access them (this will often be redundant,
287 -- since the needed deference is implicit, but no harm is done by
288 -- making it explicit).
291 Make_Explicit_Dereference
(Loc
, P_Comp_Ref
));
294 end Expand_Entry_Parameter
;
300 procedure Expand_Formal
(N
: Node_Id
) is
301 E
: constant Entity_Id
:= Entity
(N
);
302 Subp
: constant Entity_Id
:= Scope
(E
);
305 if Is_Protected_Type
(Scope
(Subp
))
306 and then Chars
(Subp
) /= Name_uInit_Proc
307 and then Present
(Protected_Formal
(E
))
309 Set_Entity
(N
, Protected_Formal
(E
));
313 ----------------------------
314 -- Expand_N_Expanded_Name --
315 ----------------------------
317 procedure Expand_N_Expanded_Name
(N
: Node_Id
) is
319 Expand_Entity_Reference
(N
);
320 end Expand_N_Expanded_Name
;
322 -------------------------
323 -- Expand_N_Identifier --
324 -------------------------
326 procedure Expand_N_Identifier
(N
: Node_Id
) is
328 Expand_Entity_Reference
(N
);
329 end Expand_N_Identifier
;
331 ---------------------------
332 -- Expand_N_Real_Literal --
333 ---------------------------
335 procedure Expand_N_Real_Literal
(N
: Node_Id
) is
337 if Vax_Float
(Etype
(N
)) then
338 Expand_Vax_Real_Literal
(N
);
340 end Expand_N_Real_Literal
;
342 ------------------------------
343 -- Expand_Protected_Private --
344 ------------------------------
346 procedure Expand_Protected_Private
(N
: Node_Id
) is
347 Loc
: constant Source_Ptr
:= Sloc
(N
);
348 E
: constant Entity_Id
:= Entity
(N
);
349 Op
: constant Node_Id
:= Protected_Operation
(E
);
356 if Nkind
(Op
) /= N_Subprogram_Body
357 or else Nkind
(Specification
(Op
)) /= N_Function_Specification
359 Set_Ekind
(Prival
(E
), E_Variable
);
361 Set_Ekind
(Prival
(E
), E_Constant
);
364 -- If the private component appears in an assignment (either lhs or
365 -- rhs) and is a one-dimensional array constrained by a discriminant,
366 -- rewrite as P (Lo .. Hi) with an explicit range, so that discriminal
367 -- is directly visible. This solves delicate visibility problems.
369 if Comes_From_Source
(N
)
370 and then Is_Array_Type
(Etype
(E
))
371 and then Number_Dimensions
(Etype
(E
)) = 1
372 and then not Within_Init_Proc
374 Lo
:= Type_Low_Bound
(Etype
(First_Index
(Etype
(E
))));
375 Hi
:= Type_High_Bound
(Etype
(First_Index
(Etype
(E
))));
377 if Nkind
(Parent
(N
)) = N_Assignment_Statement
378 and then ((Is_Entity_Name
(Lo
)
379 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
)
380 or else (Is_Entity_Name
(Hi
)
382 Ekind
(Entity
(Hi
)) = E_In_Parameter
))
384 D_Range
:= New_Node
(N_Range
, Loc
);
386 if Is_Entity_Name
(Lo
)
387 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
389 Set_Low_Bound
(D_Range
,
390 Make_Identifier
(Loc
, Chars
(Entity
(Lo
))));
392 Set_Low_Bound
(D_Range
, Duplicate_Subexpr
(Lo
));
395 if Is_Entity_Name
(Hi
)
396 and then Ekind
(Entity
(Hi
)) = E_In_Parameter
398 Set_High_Bound
(D_Range
,
399 Make_Identifier
(Loc
, Chars
(Entity
(Hi
))));
401 Set_High_Bound
(D_Range
, Duplicate_Subexpr
(Hi
));
406 Prefix
=> New_Occurrence_Of
(E
, Loc
),
407 Discrete_Range
=> D_Range
));
409 Analyze_And_Resolve
(N
, Etype
(E
));
414 -- The type of the reference is the type of the prival, which may
415 -- differ from that of the original component if it is an itype.
417 Set_Entity
(N
, Prival
(E
));
418 Set_Etype
(N
, Etype
(Prival
(E
)));
419 Scop
:= Current_Scope
;
421 -- Find entity for protected operation, which must be on scope stack.
423 while not Is_Protected_Type
(Scope
(Scop
)) loop
424 Scop
:= Scope
(Scop
);
427 Append_Elmt
(N
, Privals_Chain
(Scop
));
428 end Expand_Protected_Private
;
430 ---------------------
431 -- Expand_Renaming --
432 ---------------------
434 procedure Expand_Renaming
(N
: Node_Id
) is
435 E
: constant Entity_Id
:= Entity
(N
);
436 T
: constant Entity_Id
:= Etype
(N
);
439 Rewrite
(N
, New_Copy_Tree
(Renamed_Object
(E
)));
441 -- We mark the copy as unanalyzed, so that it is sure to be
442 -- reanalyzed at the top level. This is needed in the packed
443 -- case since we specifically avoided expanding packed array
444 -- references when the renaming declaration was analyzed.
446 Reset_Analyzed_Flags
(N
);
447 Analyze_And_Resolve
(N
, T
);
454 -- This would be trivial, simply a test for an identifier that was a
455 -- reference to a formal, if it were not for the fact that a previous
456 -- call to Expand_Entry_Parameter will have modified the reference
457 -- to the identifier to be of the form
459 -- typ!(recobj).rec.all'Constrained
461 -- where rec is a selector whose Entry_Formal link points to the formal
463 function Param_Entity
(N
: Node_Id
) return Entity_Id
is
465 -- Simple reference case
467 if Nkind
(N
) = N_Identifier
then
468 if Is_Formal
(Entity
(N
)) then
473 if Nkind
(N
) = N_Explicit_Dereference
then
475 P
: constant Node_Id
:= Prefix
(N
);
479 if Nkind
(P
) = N_Selected_Component
then
480 S
:= Selector_Name
(P
);
482 if Present
(Entry_Formal
(Entity
(S
))) then
483 return Entry_Formal
(Entity
(S
));