1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Einfo
; use Einfo
;
29 with Elists
; use Elists
;
30 with Errout
; use Errout
;
31 with Exp_Smem
; use Exp_Smem
;
32 with Exp_Util
; use Exp_Util
;
33 with Exp_VFpt
; use Exp_VFpt
;
34 with Nmake
; use Nmake
;
36 with Sem_Res
; use Sem_Res
;
37 with Sem_Util
; use Sem_Util
;
38 with Sinfo
; use Sinfo
;
39 with Tbuild
; use Tbuild
;
40 with Snames
; use Snames
;
42 package body Exp_Ch2
is
44 -----------------------
45 -- Local Subprograms --
46 -----------------------
48 procedure Expand_Discriminant
(N
: Node_Id
);
49 -- An occurrence of a discriminant within a discriminated type is replaced
50 -- with the corresponding discriminal, that is to say the formal parameter
51 -- of the initialization procedure for the type that is associated with
52 -- that particular discriminant. This replacement is not performed for
53 -- discriminants of records that appear in constraints of component of the
54 -- record, because Gigi uses the discriminant name to retrieve its value.
55 -- In the other hand, it has to be performed for default expressions of
56 -- components because they are used in the record init procedure. See
57 -- Einfo for more details, and Exp_Ch3, Exp_Ch9 for examples of use.
58 -- For discriminants of tasks and protected types, the transformation is
59 -- more complex when it occurs within a default expression for an entry
60 -- or protected operation. The corresponding default_expression_function
61 -- has an additional parameter which is the target of an entry call, and
62 -- the discriminant of the task must be replaced with a reference to the
63 -- discriminant of that formal parameter.
65 procedure Expand_Entity_Reference
(N
: Node_Id
);
66 -- Common processing for expansion of identifiers and expanded names
68 procedure Expand_Entry_Index_Parameter
(N
: Node_Id
);
69 -- A reference to the identifier in the entry index specification
70 -- of a protected entry body is modified to a reference to a constant
71 -- definintion equal to the index of the entry family member being
72 -- called. This constant is calculated as part of the elaboration
73 -- of the expanded code for the body, and is calculated from the
74 -- object-wide entry index returned by Next_Entry_Call.
76 procedure Expand_Entry_Parameter
(N
: Node_Id
);
77 -- A reference to an entry parameter is modified to be a reference to
78 -- the corresponding component of the entry parameter record that is
79 -- passed by the runtime to the accept body procedure
81 procedure Expand_Formal
(N
: Node_Id
);
82 -- A reference to a formal parameter of a protected subprogram is
83 -- expanded to the corresponding formal of the unprotected procedure
84 -- used to represent the protected subprogram within the protected object.
86 procedure Expand_Protected_Private
(N
: Node_Id
);
87 -- A reference to a private object of a protected type is expanded
88 -- to a component selected from the record used to implement
89 -- the protected object. Such a record is passed to all operations
90 -- on a protected object in a parameter named _object. Such an object
91 -- is a constant within a function, and a variable otherwise.
93 procedure Expand_Renaming
(N
: Node_Id
);
94 -- For renamings, just replace the identifier by the corresponding
95 -- name expression. Note that this has been evaluated (see routine
96 -- Exp_Ch8.Expand_N_Object_Renaming.Evaluate_Name) so this gives
97 -- the correct renaming semantics.
99 -------------------------
100 -- Expand_Discriminant --
101 -------------------------
103 procedure Expand_Discriminant
(N
: Node_Id
) is
104 Scop
: constant Entity_Id
:= Scope
(Entity
(N
));
106 Parent_P
: Node_Id
:= Parent
(P
);
107 In_Entry
: Boolean := False;
110 -- The Incomplete_Or_Private_Kind happens while resolving the
111 -- discriminant constraint involved in a derived full type,
114 -- type D is private;
115 -- type D(C : ...) is new T(C);
117 if Ekind
(Scop
) = E_Record_Type
118 or Ekind
(Scop
) in Incomplete_Or_Private_Kind
121 -- Find the origin by walking up the tree till the component
124 while Present
(Parent_P
)
125 and then Nkind
(Parent_P
) /= N_Component_Declaration
128 Parent_P
:= Parent
(P
);
131 -- If the discriminant reference was part of the default expression
132 -- it has to be "discriminalized"
134 if Present
(Parent_P
) and then P
= Expression
(Parent_P
) then
135 Set_Entity
(N
, Discriminal
(Entity
(N
)));
138 elsif Is_Concurrent_Type
(Scop
) then
139 while Present
(Parent_P
)
140 and then Nkind
(Parent_P
) /= N_Subprogram_Body
144 if Nkind
(P
) = N_Entry_Declaration
then
148 Parent_P
:= Parent
(Parent_P
);
151 -- If the discriminant occurs within the default expression for
152 -- a formal of an entry or protected operation, create a default
153 -- function for it, and replace the discriminant with a reference
154 -- to the discriminant of the formal of the default function.
155 -- The discriminant entity is the one defined in the corresponding
158 if Present
(Parent_P
)
159 and then Present
(Corresponding_Spec
(Parent_P
))
163 Loc
: constant Source_Ptr
:= Sloc
(N
);
164 D_Fun
: Entity_Id
:= Corresponding_Spec
(Parent_P
);
165 Formal
: Entity_Id
:= First_Formal
(D_Fun
);
170 -- Verify that we are within a default function: the type of
171 -- its formal parameter is the same task or protected type.
174 and then Etype
(Formal
) = Scope
(Entity
(N
))
176 Disc
:= CR_Discriminant
(Entity
(N
));
179 Make_Selected_Component
(Loc
,
180 Prefix
=> New_Occurrence_Of
(Formal
, Loc
),
181 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
));
183 Set_Etype
(New_N
, Etype
(N
));
187 Set_Entity
(N
, Discriminal
(Entity
(N
)));
191 elsif Nkind
(Parent
(N
)) = N_Range
194 Set_Entity
(N
, CR_Discriminant
(Entity
(N
)));
196 Set_Entity
(N
, Discriminal
(Entity
(N
)));
200 Set_Entity
(N
, Discriminal
(Entity
(N
)));
202 end Expand_Discriminant
;
204 -----------------------------
205 -- Expand_Entity_Reference --
206 -----------------------------
208 procedure Expand_Entity_Reference
(N
: Node_Id
) is
209 E
: constant Entity_Id
:= Entity
(N
);
212 -- Defend against errors
214 if No
(E
) and then Total_Errors_Detected
/= 0 then
218 if Ekind
(E
) = E_Discriminant
then
219 Expand_Discriminant
(N
);
221 elsif Is_Entry_Formal
(E
) then
222 Expand_Entry_Parameter
(N
);
224 elsif Ekind
(E
) = E_Component
225 and then Is_Protected_Private
(E
)
227 Expand_Protected_Private
(N
);
229 elsif Ekind
(E
) = E_Entry_Index_Parameter
then
230 Expand_Entry_Index_Parameter
(N
);
232 elsif Is_Formal
(E
) then
235 elsif Is_Renaming_Of_Object
(E
) then
238 elsif Ekind
(E
) = E_Variable
239 and then Is_Shared_Passive
(E
)
241 Expand_Shared_Passive_Variable
(N
);
243 end Expand_Entity_Reference
;
245 ----------------------------------
246 -- Expand_Entry_Index_Parameter --
247 ----------------------------------
249 procedure Expand_Entry_Index_Parameter
(N
: Node_Id
) is
251 Set_Entity
(N
, Entry_Index_Constant
(Entity
(N
)));
252 end Expand_Entry_Index_Parameter
;
254 ----------------------------
255 -- Expand_Entry_Parameter --
256 ----------------------------
258 procedure Expand_Entry_Parameter
(N
: Node_Id
) is
259 Loc
: constant Source_Ptr
:= Sloc
(N
);
260 Ent_Formal
: constant Entity_Id
:= Entity
(N
);
261 Ent_Spec
: constant Entity_Id
:= Scope
(Ent_Formal
);
262 Parm_Type
: constant Entity_Id
:= Entry_Parameters_Type
(Ent_Spec
);
263 Acc_Stack
: constant Elist_Id
:= Accept_Address
(Ent_Spec
);
264 Addr_Ent
: constant Entity_Id
:= Node
(Last_Elmt
(Acc_Stack
));
265 P_Comp_Ref
: Entity_Id
;
268 -- What we need is a reference to the corresponding component of the
269 -- parameter record object. The Accept_Address field of the entry
270 -- entity references the address variable that contains the address
271 -- of the accept parameters record. We first have to do an unchecked
272 -- conversion to turn this into a pointer to the parameter record and
273 -- then we select the required parameter field.
276 Make_Selected_Component
(Loc
,
278 Unchecked_Convert_To
(Parm_Type
,
279 New_Reference_To
(Addr_Ent
, Loc
)),
281 New_Reference_To
(Entry_Component
(Ent_Formal
), Loc
));
283 -- For all types of parameters, the constructed parameter record
284 -- object contains a pointer to the parameter. Thus we must
285 -- dereference them to access them (this will often be redundant,
286 -- since the needed deference is implicit, but no harm is done by
287 -- making it explicit).
290 Make_Explicit_Dereference
(Loc
, P_Comp_Ref
));
293 end Expand_Entry_Parameter
;
299 procedure Expand_Formal
(N
: Node_Id
) is
300 E
: constant Entity_Id
:= Entity
(N
);
301 Subp
: constant Entity_Id
:= Scope
(E
);
304 if Is_Protected_Type
(Scope
(Subp
))
305 and then Chars
(Subp
) /= Name_uInit_Proc
306 and then Present
(Protected_Formal
(E
))
308 Set_Entity
(N
, Protected_Formal
(E
));
312 ----------------------------
313 -- Expand_N_Expanded_Name --
314 ----------------------------
316 procedure Expand_N_Expanded_Name
(N
: Node_Id
) is
318 Expand_Entity_Reference
(N
);
319 end Expand_N_Expanded_Name
;
321 -------------------------
322 -- Expand_N_Identifier --
323 -------------------------
325 procedure Expand_N_Identifier
(N
: Node_Id
) is
327 Expand_Entity_Reference
(N
);
328 end Expand_N_Identifier
;
330 ---------------------------
331 -- Expand_N_Real_Literal --
332 ---------------------------
334 procedure Expand_N_Real_Literal
(N
: Node_Id
) is
336 if Vax_Float
(Etype
(N
)) then
337 Expand_Vax_Real_Literal
(N
);
339 end Expand_N_Real_Literal
;
341 ------------------------------
342 -- Expand_Protected_Private --
343 ------------------------------
345 procedure Expand_Protected_Private
(N
: Node_Id
) is
346 Loc
: constant Source_Ptr
:= Sloc
(N
);
347 E
: constant Entity_Id
:= Entity
(N
);
348 Op
: constant Node_Id
:= Protected_Operation
(E
);
355 if Nkind
(Op
) /= N_Subprogram_Body
356 or else Nkind
(Specification
(Op
)) /= N_Function_Specification
358 Set_Ekind
(Prival
(E
), E_Variable
);
360 Set_Ekind
(Prival
(E
), E_Constant
);
363 -- If the private component appears in an assignment (either lhs or
364 -- rhs) and is a one-dimensional array constrained by a discriminant,
365 -- rewrite as P (Lo .. Hi) with an explicit range, so that discriminal
366 -- is directly visible. This solves delicate visibility problems.
368 if Comes_From_Source
(N
)
369 and then Is_Array_Type
(Etype
(E
))
370 and then Number_Dimensions
(Etype
(E
)) = 1
371 and then not Within_Init_Proc
373 Lo
:= Type_Low_Bound
(Etype
(First_Index
(Etype
(E
))));
374 Hi
:= Type_High_Bound
(Etype
(First_Index
(Etype
(E
))));
376 if Nkind
(Parent
(N
)) = N_Assignment_Statement
377 and then ((Is_Entity_Name
(Lo
)
378 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
)
379 or else (Is_Entity_Name
(Hi
)
381 Ekind
(Entity
(Hi
)) = E_In_Parameter
))
383 D_Range
:= New_Node
(N_Range
, Loc
);
385 if Is_Entity_Name
(Lo
)
386 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
388 Set_Low_Bound
(D_Range
,
389 Make_Identifier
(Loc
, Chars
(Entity
(Lo
))));
391 Set_Low_Bound
(D_Range
, Duplicate_Subexpr
(Lo
));
394 if Is_Entity_Name
(Hi
)
395 and then Ekind
(Entity
(Hi
)) = E_In_Parameter
397 Set_High_Bound
(D_Range
,
398 Make_Identifier
(Loc
, Chars
(Entity
(Hi
))));
400 Set_High_Bound
(D_Range
, Duplicate_Subexpr
(Hi
));
405 Prefix
=> New_Occurrence_Of
(E
, Loc
),
406 Discrete_Range
=> D_Range
));
408 Analyze_And_Resolve
(N
, Etype
(E
));
413 -- The type of the reference is the type of the prival, which may
414 -- differ from that of the original component if it is an itype.
416 Set_Entity
(N
, Prival
(E
));
417 Set_Etype
(N
, Etype
(Prival
(E
)));
418 Scop
:= Current_Scope
;
420 -- Find entity for protected operation, which must be on scope stack.
422 while not Is_Protected_Type
(Scope
(Scop
)) loop
423 Scop
:= Scope
(Scop
);
426 Append_Elmt
(N
, Privals_Chain
(Scop
));
427 end Expand_Protected_Private
;
429 ---------------------
430 -- Expand_Renaming --
431 ---------------------
433 procedure Expand_Renaming
(N
: Node_Id
) is
434 E
: constant Entity_Id
:= Entity
(N
);
435 T
: constant Entity_Id
:= Etype
(N
);
438 Rewrite
(N
, New_Copy_Tree
(Renamed_Object
(E
)));
440 -- We mark the copy as unanalyzed, so that it is sure to be
441 -- reanalyzed at the top level. This is needed in the packed
442 -- case since we specifically avoided expanding packed array
443 -- references when the renaming declaration was analyzed.
445 Reset_Analyzed_Flags
(N
);
446 Analyze_And_Resolve
(N
, T
);
453 -- This would be trivial, simply a test for an identifier that was a
454 -- reference to a formal, if it were not for the fact that a previous
455 -- call to Expand_Entry_Parameter will have modified the reference
456 -- to the identifier to be of the form
458 -- typ!(recobj).rec.all'Constrained
460 -- where rec is a selector whose Entry_Formal link points to the formal
462 function Param_Entity
(N
: Node_Id
) return Entity_Id
is
464 -- Simple reference case
466 if Nkind
(N
) = N_Identifier
then
467 if Is_Formal
(Entity
(N
)) then
472 if Nkind
(N
) = N_Explicit_Dereference
then
474 P
: constant Node_Id
:= Prefix
(N
);
478 if Nkind
(P
) = N_Selected_Component
then
479 S
:= Selector_Name
(P
);
481 if Present
(Entry_Formal
(Entity
(S
))) then
482 return Entry_Formal
(Entity
(S
));