1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree
; use Atree
;
30 with Einfo
; use Einfo
;
31 with Elists
; use Elists
;
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 if Ekind
(E
) = E_Discriminant
then
214 Expand_Discriminant
(N
);
216 elsif Is_Entry_Formal
(E
) then
217 Expand_Entry_Parameter
(N
);
219 elsif Ekind
(E
) = E_Component
220 and then Is_Protected_Private
(E
)
222 Expand_Protected_Private
(N
);
224 elsif Ekind
(E
) = E_Entry_Index_Parameter
then
225 Expand_Entry_Index_Parameter
(N
);
227 elsif Is_Formal
(E
) then
230 elsif Is_Renaming_Of_Object
(E
) then
233 elsif Ekind
(E
) = E_Variable
234 and then Is_Shared_Passive
(E
)
236 Expand_Shared_Passive_Variable
(N
);
238 end Expand_Entity_Reference
;
240 ----------------------------------
241 -- Expand_Entry_Index_Parameter --
242 ----------------------------------
244 procedure Expand_Entry_Index_Parameter
(N
: Node_Id
) is
246 Set_Entity
(N
, Entry_Index_Constant
(Entity
(N
)));
247 end Expand_Entry_Index_Parameter
;
249 ----------------------------
250 -- Expand_Entry_Parameter --
251 ----------------------------
253 procedure Expand_Entry_Parameter
(N
: Node_Id
) is
254 Loc
: constant Source_Ptr
:= Sloc
(N
);
255 Ent_Formal
: constant Entity_Id
:= Entity
(N
);
256 Ent_Spec
: constant Entity_Id
:= Scope
(Ent_Formal
);
257 Parm_Type
: constant Entity_Id
:= Entry_Parameters_Type
(Ent_Spec
);
258 Acc_Stack
: constant Elist_Id
:= Accept_Address
(Ent_Spec
);
259 Addr_Ent
: constant Entity_Id
:= Node
(Last_Elmt
(Acc_Stack
));
260 P_Comp_Ref
: Entity_Id
;
263 -- What we need is a reference to the corresponding component of the
264 -- parameter record object. The Accept_Address field of the entry
265 -- entity references the address variable that contains the address
266 -- of the accept parameters record. We first have to do an unchecked
267 -- conversion to turn this into a pointer to the parameter record and
268 -- then we select the required parameter field.
271 Make_Selected_Component
(Loc
,
273 Unchecked_Convert_To
(Parm_Type
,
274 New_Reference_To
(Addr_Ent
, Loc
)),
276 New_Reference_To
(Entry_Component
(Ent_Formal
), Loc
));
278 -- For all types of parameters, the constructed parameter record
279 -- object contains a pointer to the parameter. Thus we must
280 -- dereference them to access them (this will often be redundant,
281 -- since the needed deference is implicit, but no harm is done by
282 -- making it explicit).
285 Make_Explicit_Dereference
(Loc
, P_Comp_Ref
));
288 end Expand_Entry_Parameter
;
294 procedure Expand_Formal
(N
: Node_Id
) is
295 E
: constant Entity_Id
:= Entity
(N
);
296 Subp
: constant Entity_Id
:= Scope
(E
);
299 if Is_Protected_Type
(Scope
(Subp
))
300 and then Chars
(Subp
) /= Name_uInit_Proc
301 and then Present
(Protected_Formal
(E
))
303 Set_Entity
(N
, Protected_Formal
(E
));
307 ----------------------------
308 -- Expand_N_Expanded_Name --
309 ----------------------------
311 procedure Expand_N_Expanded_Name
(N
: Node_Id
) is
313 Expand_Entity_Reference
(N
);
314 end Expand_N_Expanded_Name
;
316 -------------------------
317 -- Expand_N_Identifier --
318 -------------------------
320 procedure Expand_N_Identifier
(N
: Node_Id
) is
322 Expand_Entity_Reference
(N
);
323 end Expand_N_Identifier
;
325 ---------------------------
326 -- Expand_N_Real_Literal --
327 ---------------------------
329 procedure Expand_N_Real_Literal
(N
: Node_Id
) is
331 if Vax_Float
(Etype
(N
)) then
332 Expand_Vax_Real_Literal
(N
);
334 end Expand_N_Real_Literal
;
336 ------------------------------
337 -- Expand_Protected_Private --
338 ------------------------------
340 procedure Expand_Protected_Private
(N
: Node_Id
) is
341 Loc
: constant Source_Ptr
:= Sloc
(N
);
342 E
: constant Entity_Id
:= Entity
(N
);
343 Op
: constant Node_Id
:= Protected_Operation
(E
);
350 if Nkind
(Op
) /= N_Subprogram_Body
351 or else Nkind
(Specification
(Op
)) /= N_Function_Specification
353 Set_Ekind
(Prival
(E
), E_Variable
);
355 Set_Ekind
(Prival
(E
), E_Constant
);
358 -- If the private component appears in an assignment (either lhs or
359 -- rhs) and is a one-dimensional array constrained by a discriminant,
360 -- rewrite as P (Lo .. Hi) with an explicit range, so that discriminal
361 -- is directly visible. This solves delicate visibility problems.
363 if Comes_From_Source
(N
)
364 and then Is_Array_Type
(Etype
(E
))
365 and then Number_Dimensions
(Etype
(E
)) = 1
366 and then not Within_Init_Proc
368 Lo
:= Type_Low_Bound
(Etype
(First_Index
(Etype
(E
))));
369 Hi
:= Type_High_Bound
(Etype
(First_Index
(Etype
(E
))));
371 if Nkind
(Parent
(N
)) = N_Assignment_Statement
372 and then ((Is_Entity_Name
(Lo
)
373 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
)
374 or else (Is_Entity_Name
(Hi
)
376 Ekind
(Entity
(Hi
)) = E_In_Parameter
))
378 D_Range
:= New_Node
(N_Range
, Loc
);
380 if Is_Entity_Name
(Lo
)
381 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
383 Set_Low_Bound
(D_Range
,
384 Make_Identifier
(Loc
, Chars
(Entity
(Lo
))));
386 Set_Low_Bound
(D_Range
, Duplicate_Subexpr
(Lo
));
389 if Is_Entity_Name
(Hi
)
390 and then Ekind
(Entity
(Hi
)) = E_In_Parameter
392 Set_High_Bound
(D_Range
,
393 Make_Identifier
(Loc
, Chars
(Entity
(Hi
))));
395 Set_High_Bound
(D_Range
, Duplicate_Subexpr
(Hi
));
400 Prefix
=> New_Occurrence_Of
(E
, Loc
),
401 Discrete_Range
=> D_Range
));
403 Analyze_And_Resolve
(N
, Etype
(E
));
408 -- The type of the reference is the type of the prival, which may
409 -- differ from that of the original component if it is an itype.
411 Set_Entity
(N
, Prival
(E
));
412 Set_Etype
(N
, Etype
(Prival
(E
)));
413 Scop
:= Current_Scope
;
415 -- Find entity for protected operation, which must be on scope stack.
417 while not Is_Protected_Type
(Scope
(Scop
)) loop
418 Scop
:= Scope
(Scop
);
421 Append_Elmt
(N
, Privals_Chain
(Scop
));
422 end Expand_Protected_Private
;
424 ---------------------
425 -- Expand_Renaming --
426 ---------------------
428 procedure Expand_Renaming
(N
: Node_Id
) is
429 E
: constant Entity_Id
:= Entity
(N
);
430 T
: constant Entity_Id
:= Etype
(N
);
433 Rewrite
(N
, New_Copy_Tree
(Renamed_Object
(E
)));
435 -- We mark the copy as unanalyzed, so that it is sure to be
436 -- reanalyzed at the top level. This is needed in the packed
437 -- case since we specifically avoided expanding packed array
438 -- references when the renaming declaration was analyzed.
440 Reset_Analyzed_Flags
(N
);
441 Analyze_And_Resolve
(N
, T
);
448 -- This would be trivial, simply a test for an identifier that was a
449 -- reference to a formal, if it were not for the fact that a previous
450 -- call to Expand_Entry_Parameter will have modified the reference
451 -- to the identifier to be of the form
453 -- typ!(recobj).rec.all'Constrained
455 -- where rec is a selector whose Entry_Formal link points to the formal
457 function Param_Entity
(N
: Node_Id
) return Entity_Id
is
459 -- Simple reference case
461 if Nkind
(N
) = N_Identifier
then
462 if Is_Formal
(Entity
(N
)) then
467 if Nkind
(N
) = N_Explicit_Dereference
then
469 P
: constant Node_Id
:= Prefix
(N
);
473 if Nkind
(P
) = N_Selected_Component
then
474 S
:= Selector_Name
(P
);
476 if Present
(Entry_Formal
(Entity
(S
))) then
477 return Entry_Formal
(Entity
(S
));