1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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_Tss
; use Exp_Tss
;
33 with Exp_Util
; use Exp_Util
;
34 with Exp_VFpt
; use Exp_VFpt
;
35 with Nmake
; use Nmake
;
38 with Sem_Eval
; use Sem_Eval
;
39 with Sem_Res
; use Sem_Res
;
40 with Sem_Util
; use Sem_Util
;
41 with Sem_Warn
; use Sem_Warn
;
42 with Sinfo
; use Sinfo
;
43 with Snames
; use Snames
;
44 with Tbuild
; use Tbuild
;
45 with Uintp
; use Uintp
;
47 package body Exp_Ch2
is
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 procedure Expand_Current_Value
(N
: Node_Id
);
54 -- N is a node for a variable whose Current_Value field is set. If N is
55 -- node is for a discrete type, replaces node with a copy of the referenced
56 -- value. This provides a limited form of value propagation for variables
57 -- which are initialized or assigned not been further modified at the time
58 -- of reference. The call has no effect if the Current_Value refers to a
59 -- conditional with condition other than equality.
61 procedure Expand_Discriminant
(N
: Node_Id
);
62 -- An occurrence of a discriminant within a discriminated type is replaced
63 -- with the corresponding discriminal, that is to say the formal parameter
64 -- of the initialization procedure for the type that is associated with
65 -- that particular discriminant. This replacement is not performed for
66 -- discriminants of records that appear in constraints of component of the
67 -- record, because Gigi uses the discriminant name to retrieve its value.
68 -- In the other hand, it has to be performed for default expressions of
69 -- components because they are used in the record init procedure. See Einfo
70 -- for more details, and Exp_Ch3, Exp_Ch9 for examples of use. For
71 -- discriminants of tasks and protected types, the transformation is more
72 -- complex when it occurs within a default expression for an entry or
73 -- protected operation. The corresponding default_expression_function has
74 -- an additional parameter which is the target of an entry call, and the
75 -- discriminant of the task must be replaced with a reference to the
76 -- discriminant of that formal parameter.
78 procedure Expand_Entity_Reference
(N
: Node_Id
);
79 -- Common processing for expansion of identifiers and expanded names
81 procedure Expand_Entry_Index_Parameter
(N
: Node_Id
);
82 -- A reference to the identifier in the entry index specification of
83 -- protected entry body is modified to a reference to a constant definition
84 -- equal to the index of the entry family member being called. This
85 -- constant is calculated as part of the elaboration of the expanded code
86 -- for the body, and is calculated from the object-wide entry index
87 -- returned by Next_Entry_Call.
89 procedure Expand_Entry_Parameter
(N
: Node_Id
);
90 -- A reference to an entry parameter is modified to be a reference to the
91 -- corresponding component of the entry parameter record that is passed by
92 -- the runtime to the accept body procedure
94 procedure Expand_Formal
(N
: Node_Id
);
95 -- A reference to a formal parameter of a protected subprogram is expanded
96 -- to the corresponding formal of the unprotected procedure used to
97 -- represent the protected subprogram within the protected object.
99 procedure Expand_Protected_Private
(N
: Node_Id
);
100 -- A reference to a private object of a protected type is expanded to a
101 -- component selected from the record used to implement the protected
102 -- object. Such a record is passed to all operations on a protected object
103 -- in a parameter named _object. Such an object is a constant within a
104 -- function, and a variable otherwise.
106 procedure Expand_Renaming
(N
: Node_Id
);
107 -- For renamings, just replace the identifier by the corresponding
108 -- name expression. Note that this has been evaluated (see routine
109 -- Exp_Ch8.Expand_N_Object_Renaming.Evaluate_Name) so this gives
110 -- the correct renaming semantics.
112 --------------------------
113 -- Expand_Current_Value --
114 --------------------------
116 procedure Expand_Current_Value
(N
: Node_Id
) is
117 Loc
: constant Source_Ptr
:= Sloc
(N
);
118 E
: constant Entity_Id
:= Entity
(N
);
119 CV
: constant Node_Id
:= Current_Value
(E
);
120 T
: constant Entity_Id
:= Etype
(N
);
124 -- Start of processing for Expand_Current_Value
129 -- No replacement if value raises constraint error
131 and then Nkind
(CV
) /= N_Raise_Constraint_Error
133 -- Do this only for discrete types
135 and then Is_Discrete_Type
(T
)
137 -- Do not replace biased types, since it is problematic to
138 -- consistently generate a sensible constant value in this case.
140 and then not Has_Biased_Representation
(T
)
142 -- Do not replace lvalues
144 and then not Is_Lvalue
(N
)
146 -- Check that entity is suitable for replacement
148 and then OK_To_Do_Constant_Replacement
(E
)
150 -- Do not replace occurrences in pragmas (where names typically
151 -- appear not as values, but as simply names. If there are cases
152 -- where values are required, it is only a very minor efficiency
153 -- issue that they do not get replaced when they could be).
155 and then Nkind
(Parent
(N
)) /= N_Pragma_Argument_Association
157 -- Same for Asm_Input and Asm_Output attribute references
159 and then not (Nkind
(Parent
(N
)) = N_Attribute_Reference
161 (Attribute_Name
(Parent
(N
)) = Name_Asm_Input
163 Attribute_Name
(Parent
(N
)) = Name_Asm_Output
))
165 -- Case of Current_Value is a compile time known value
167 if Nkind
(CV
) in N_Subexpr
then
170 -- Case of Current_Value is a conditional expression reference
173 Get_Current_Value_Condition
(N
, Op
, Val
);
175 if Op
/= N_Op_Eq
then
180 -- If constant value is an occurrence of an enumeration literal,
181 -- then we just make another occurence of the same literal.
183 if Is_Entity_Name
(Val
)
184 and then Ekind
(Entity
(Val
)) = E_Enumeration_Literal
187 Unchecked_Convert_To
(T
,
188 New_Occurrence_Of
(Entity
(Val
), Loc
)));
190 -- Otherwise get the value, and convert to appropriate type
194 Unchecked_Convert_To
(T
,
195 Make_Integer_Literal
(Loc
,
196 Intval
=> Expr_Rep_Value
(Val
))));
199 Analyze_And_Resolve
(N
, T
);
200 Set_Is_Static_Expression
(N
, False);
202 end Expand_Current_Value
;
204 -------------------------
205 -- Expand_Discriminant --
206 -------------------------
208 procedure Expand_Discriminant
(N
: Node_Id
) is
209 Scop
: constant Entity_Id
:= Scope
(Entity
(N
));
211 Parent_P
: Node_Id
:= Parent
(P
);
212 In_Entry
: Boolean := False;
215 -- The Incomplete_Or_Private_Kind happens while resolving the
216 -- discriminant constraint involved in a derived full type,
219 -- type D is private;
220 -- type D(C : ...) is new T(C);
222 if Ekind
(Scop
) = E_Record_Type
223 or Ekind
(Scop
) in Incomplete_Or_Private_Kind
225 -- Find the origin by walking up the tree till the component
228 while Present
(Parent_P
)
229 and then Nkind
(Parent_P
) /= N_Component_Declaration
232 Parent_P
:= Parent
(P
);
235 -- If the discriminant reference was part of the default expression
236 -- it has to be "discriminalized"
238 if Present
(Parent_P
) and then P
= Expression
(Parent_P
) then
239 Set_Entity
(N
, Discriminal
(Entity
(N
)));
242 elsif Is_Concurrent_Type
(Scop
) then
243 while Present
(Parent_P
)
244 and then Nkind
(Parent_P
) /= N_Subprogram_Body
248 if Nkind
(P
) = N_Entry_Declaration
then
252 Parent_P
:= Parent
(Parent_P
);
255 -- If the discriminant occurs within the default expression for a
256 -- formal of an entry or protected operation, create a default
257 -- function for it, and replace the discriminant with a reference to
258 -- the discriminant of the formal of the default function. The
259 -- discriminant entity is the one defined in the corresponding
262 if Present
(Parent_P
)
263 and then Present
(Corresponding_Spec
(Parent_P
))
266 Loc
: constant Source_Ptr
:= Sloc
(N
);
267 D_Fun
: constant Entity_Id
:= Corresponding_Spec
(Parent_P
);
268 Formal
: constant Entity_Id
:= First_Formal
(D_Fun
);
273 -- Verify that we are within a default function: the type of
274 -- its formal parameter is the same task or protected type.
277 and then Etype
(Formal
) = Scope
(Entity
(N
))
279 Disc
:= CR_Discriminant
(Entity
(N
));
282 Make_Selected_Component
(Loc
,
283 Prefix
=> New_Occurrence_Of
(Formal
, Loc
),
284 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
));
286 Set_Etype
(New_N
, Etype
(N
));
290 Set_Entity
(N
, Discriminal
(Entity
(N
)));
294 elsif Nkind
(Parent
(N
)) = N_Range
297 Set_Entity
(N
, CR_Discriminant
(Entity
(N
)));
299 Set_Entity
(N
, Discriminal
(Entity
(N
)));
303 Set_Entity
(N
, Discriminal
(Entity
(N
)));
305 end Expand_Discriminant
;
307 -----------------------------
308 -- Expand_Entity_Reference --
309 -----------------------------
311 procedure Expand_Entity_Reference
(N
: Node_Id
) is
312 E
: constant Entity_Id
:= Entity
(N
);
315 -- Defend against errors
317 if No
(E
) and then Total_Errors_Detected
/= 0 then
321 if Ekind
(E
) = E_Discriminant
then
322 Expand_Discriminant
(N
);
324 elsif Is_Entry_Formal
(E
) then
325 Expand_Entry_Parameter
(N
);
327 elsif Ekind
(E
) = E_Component
328 and then Is_Protected_Private
(E
)
330 -- Protect against junk use of tasking in no run time mode
332 if No_Run_Time_Mode
then
336 Expand_Protected_Private
(N
);
338 elsif Ekind
(E
) = E_Entry_Index_Parameter
then
339 Expand_Entry_Index_Parameter
(N
);
341 elsif Is_Formal
(E
) then
344 elsif Is_Renaming_Of_Object
(E
) then
347 elsif Ekind
(E
) = E_Variable
348 and then Is_Shared_Passive
(E
)
350 Expand_Shared_Passive_Variable
(N
);
352 elsif (Ekind
(E
) = E_Variable
354 Ekind
(E
) = E_In_Out_Parameter
356 Ekind
(E
) = E_Out_Parameter
)
357 and then Present
(Current_Value
(E
))
359 Expand_Current_Value
(N
);
361 -- We do want to warn for the case of a boolean variable (not a
362 -- boolean constant) whose value is known at compile time.
364 if Is_Boolean_Type
(Etype
(N
)) then
365 Warn_On_Known_Condition
(N
);
368 end Expand_Entity_Reference
;
370 ----------------------------------
371 -- Expand_Entry_Index_Parameter --
372 ----------------------------------
374 procedure Expand_Entry_Index_Parameter
(N
: Node_Id
) is
376 Set_Entity
(N
, Entry_Index_Constant
(Entity
(N
)));
377 end Expand_Entry_Index_Parameter
;
379 ----------------------------
380 -- Expand_Entry_Parameter --
381 ----------------------------
383 procedure Expand_Entry_Parameter
(N
: Node_Id
) is
384 Loc
: constant Source_Ptr
:= Sloc
(N
);
385 Ent_Formal
: constant Entity_Id
:= Entity
(N
);
386 Ent_Spec
: constant Entity_Id
:= Scope
(Ent_Formal
);
387 Parm_Type
: constant Entity_Id
:= Entry_Parameters_Type
(Ent_Spec
);
388 Acc_Stack
: constant Elist_Id
:= Accept_Address
(Ent_Spec
);
389 Addr_Ent
: constant Entity_Id
:= Node
(Last_Elmt
(Acc_Stack
));
390 P_Comp_Ref
: Entity_Id
;
392 function In_Assignment_Context
(N
: Node_Id
) return Boolean;
393 -- Check whether this is a context in which the entry formal may be
396 ---------------------------
397 -- In_Assignment_Context --
398 ---------------------------
400 function In_Assignment_Context
(N
: Node_Id
) return Boolean is
402 if Nkind
(Parent
(N
)) = N_Procedure_Call_Statement
403 or else Nkind
(Parent
(N
)) = N_Entry_Call_Statement
405 (Nkind
(Parent
(N
)) = N_Assignment_Statement
406 and then N
= Name
(Parent
(N
)))
410 elsif Nkind
(Parent
(N
)) = N_Parameter_Association
then
411 return In_Assignment_Context
(Parent
(N
));
413 elsif (Nkind
(Parent
(N
)) = N_Selected_Component
414 or else Nkind
(Parent
(N
)) = N_Indexed_Component
415 or else Nkind
(Parent
(N
)) = N_Slice
)
416 and then In_Assignment_Context
(Parent
(N
))
422 end In_Assignment_Context
;
424 -- Start of processing for Expand_Entry_Parameter
427 if Is_Task_Type
(Scope
(Ent_Spec
))
428 and then Comes_From_Source
(Ent_Formal
)
430 -- Before replacing the formal with the local renaming that is used
431 -- in the accept block, note if this is an assignment context, and
432 -- note the modification to avoid spurious warnings, because the
433 -- original entity is not used further. If formal is unconstrained,
434 -- we also generate an extra parameter to hold the Constrained
435 -- attribute of the actual. No renaming is generated for this flag.
437 if Ekind
(Entity
(N
)) /= E_In_Parameter
438 and then In_Assignment_Context
(N
)
440 Note_Possible_Modification
(N
);
443 Rewrite
(N
, New_Occurrence_Of
(Renamed_Object
(Entity
(N
)), Loc
));
447 -- What we need is a reference to the corresponding component of the
448 -- parameter record object. The Accept_Address field of the entry entity
449 -- references the address variable that contains the address of the
450 -- accept parameters record. We first have to do an unchecked conversion
451 -- to turn this into a pointer to the parameter record and then we
452 -- select the required parameter field.
455 Make_Selected_Component
(Loc
,
457 Make_Explicit_Dereference
(Loc
,
458 Unchecked_Convert_To
(Parm_Type
,
459 New_Reference_To
(Addr_Ent
, Loc
))),
461 New_Reference_To
(Entry_Component
(Ent_Formal
), Loc
));
463 -- For all types of parameters, the constructed parameter record object
464 -- contains a pointer to the parameter. Thus we must dereference them to
465 -- access them (this will often be redundant, since the needed deference
466 -- is implicit, but no harm is done by making it explicit).
469 Make_Explicit_Dereference
(Loc
, P_Comp_Ref
));
472 end Expand_Entry_Parameter
;
478 procedure Expand_Formal
(N
: Node_Id
) is
479 E
: constant Entity_Id
:= Entity
(N
);
480 Subp
: constant Entity_Id
:= Scope
(E
);
483 if Is_Protected_Type
(Scope
(Subp
))
484 and then not Is_Init_Proc
(Subp
)
485 and then Present
(Protected_Formal
(E
))
487 Set_Entity
(N
, Protected_Formal
(E
));
491 ----------------------------
492 -- Expand_N_Expanded_Name --
493 ----------------------------
495 procedure Expand_N_Expanded_Name
(N
: Node_Id
) is
497 Expand_Entity_Reference
(N
);
498 end Expand_N_Expanded_Name
;
500 -------------------------
501 -- Expand_N_Identifier --
502 -------------------------
504 procedure Expand_N_Identifier
(N
: Node_Id
) is
506 Expand_Entity_Reference
(N
);
507 end Expand_N_Identifier
;
509 ---------------------------
510 -- Expand_N_Real_Literal --
511 ---------------------------
513 procedure Expand_N_Real_Literal
(N
: Node_Id
) is
515 if Vax_Float
(Etype
(N
)) then
516 Expand_Vax_Real_Literal
(N
);
518 end Expand_N_Real_Literal
;
520 ------------------------------
521 -- Expand_Protected_Private --
522 ------------------------------
524 procedure Expand_Protected_Private
(N
: Node_Id
) is
525 Loc
: constant Source_Ptr
:= Sloc
(N
);
526 E
: constant Entity_Id
:= Entity
(N
);
527 Op
: constant Node_Id
:= Protected_Operation
(E
);
534 if Nkind
(Op
) /= N_Subprogram_Body
535 or else Nkind
(Specification
(Op
)) /= N_Function_Specification
537 Set_Ekind
(Prival
(E
), E_Variable
);
539 Set_Ekind
(Prival
(E
), E_Constant
);
542 -- If the private component appears in an assignment (either lhs or
543 -- rhs) and is a one-dimensional array constrained by a discriminant,
544 -- rewrite as P (Lo .. Hi) with an explicit range, so that discriminal
545 -- is directly visible. This solves delicate visibility problems.
547 if Comes_From_Source
(N
)
548 and then Is_Array_Type
(Etype
(E
))
549 and then Number_Dimensions
(Etype
(E
)) = 1
550 and then not Within_Init_Proc
552 Lo
:= Type_Low_Bound
(Etype
(First_Index
(Etype
(E
))));
553 Hi
:= Type_High_Bound
(Etype
(First_Index
(Etype
(E
))));
555 if Nkind
(Parent
(N
)) = N_Assignment_Statement
556 and then ((Is_Entity_Name
(Lo
)
557 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
)
558 or else (Is_Entity_Name
(Hi
)
560 Ekind
(Entity
(Hi
)) = E_In_Parameter
))
562 D_Range
:= New_Node
(N_Range
, Loc
);
564 if Is_Entity_Name
(Lo
)
565 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
567 Set_Low_Bound
(D_Range
,
568 Make_Identifier
(Loc
, Chars
(Entity
(Lo
))));
570 Set_Low_Bound
(D_Range
, Duplicate_Subexpr
(Lo
));
573 if Is_Entity_Name
(Hi
)
574 and then Ekind
(Entity
(Hi
)) = E_In_Parameter
576 Set_High_Bound
(D_Range
,
577 Make_Identifier
(Loc
, Chars
(Entity
(Hi
))));
579 Set_High_Bound
(D_Range
, Duplicate_Subexpr
(Hi
));
584 Prefix
=> New_Occurrence_Of
(E
, Loc
),
585 Discrete_Range
=> D_Range
));
587 Analyze_And_Resolve
(N
, Etype
(E
));
592 -- The type of the reference is the type of the prival, which may differ
593 -- from that of the original component if it is an itype.
595 Set_Entity
(N
, Prival
(E
));
596 Set_Etype
(N
, Etype
(Prival
(E
)));
597 Scop
:= Current_Scope
;
599 -- Find entity for protected operation, which must be on scope stack
601 while not Is_Protected_Type
(Scope
(Scop
)) loop
602 Scop
:= Scope
(Scop
);
605 Append_Elmt
(N
, Privals_Chain
(Scop
));
606 end Expand_Protected_Private
;
608 ---------------------
609 -- Expand_Renaming --
610 ---------------------
612 procedure Expand_Renaming
(N
: Node_Id
) is
613 E
: constant Entity_Id
:= Entity
(N
);
614 T
: constant Entity_Id
:= Etype
(N
);
617 Rewrite
(N
, New_Copy_Tree
(Renamed_Object
(E
)));
619 -- We mark the copy as unanalyzed, so that it is sure to be reanalyzed
620 -- at the top level. This is needed in the packed case since we
621 -- specifically avoided expanding packed array references when the
622 -- renaming declaration was analyzed.
624 Reset_Analyzed_Flags
(N
);
625 Analyze_And_Resolve
(N
, T
);
632 -- This would be trivial, simply a test for an identifier that was a
633 -- reference to a formal, if it were not for the fact that a previous call
634 -- to Expand_Entry_Parameter will have modified the reference to the
635 -- identifier. A formal of a protected entity is rewritten as
637 -- typ!(recobj).rec.all'Constrained
639 -- where rec is a selector whose Entry_Formal link points to the formal
640 -- For a formal of a task entity, the formal is rewritten as a local
643 -- In addition, a formal that is marked volatile because it is aliased
644 -- through an address clause is rewritten as dereference as well.
646 function Param_Entity
(N
: Node_Id
) return Entity_Id
is
648 -- Simple reference case
650 if Nkind
(N
) = N_Identifier
or else Nkind
(N
) = N_Expanded_Name
then
651 if Is_Formal
(Entity
(N
)) then
654 elsif Nkind
(Parent
(Entity
(N
))) = N_Object_Renaming_Declaration
655 and then Nkind
(Parent
(Parent
(Entity
(N
)))) = N_Accept_Statement
661 if Nkind
(N
) = N_Explicit_Dereference
then
663 P
: constant Node_Id
:= Prefix
(N
);
667 if Nkind
(P
) = N_Selected_Component
then
668 S
:= Selector_Name
(P
);
670 if Present
(Entry_Formal
(Entity
(S
))) then
671 return Entry_Formal
(Entity
(S
));
674 elsif Nkind
(Original_Node
(N
)) = N_Identifier
then
675 return Param_Entity
(Original_Node
(N
));