1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 Namet; use Namet;
36 with Nmake; use Nmake;
39 with Sem_Eval; use Sem_Eval;
40 with Sem_Res; use Sem_Res;
41 with Sem_Util; use Sem_Util;
42 with Sem_Warn; use Sem_Warn;
43 with Sinfo; use Sinfo;
44 with Snames; use Snames;
45 with Tbuild; use Tbuild;
46 with Uintp; use Uintp;
48 package body Exp_Ch2 is
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
54 procedure Expand_Current_Value (N : Node_Id);
55 -- N is a node for a variable whose Current_Value field is set. If N is
56 -- node is for a discrete type, replaces node with a copy of the referenced
57 -- value. This provides a limited form of value propagation for variables
58 -- which are initialized or assigned not been further modified at the time
59 -- of reference. The call has no effect if the Current_Value refers to a
60 -- conditional with condition other than equality.
62 procedure Expand_Discriminant (N : Node_Id);
63 -- An occurrence of a discriminant within a discriminated type is replaced
64 -- with the corresponding discriminal, that is to say the formal parameter
65 -- of the initialization procedure for the type that is associated with
66 -- that particular discriminant. This replacement is not performed for
67 -- discriminants of records that appear in constraints of component of the
68 -- record, because Gigi uses the discriminant name to retrieve its value.
69 -- In the other hand, it has to be performed for default expressions of
70 -- components because they are used in the record init procedure. See Einfo
71 -- for more details, and Exp_Ch3, Exp_Ch9 for examples of use. For
72 -- discriminants of tasks and protected types, the transformation is more
73 -- complex when it occurs within a default expression for an entry or
74 -- protected operation. The corresponding default_expression_function has
75 -- an additional parameter which is the target of an entry call, and the
76 -- discriminant of the task must be replaced with a reference to the
77 -- discriminant of that formal parameter.
79 procedure Expand_Entity_Reference (N : Node_Id);
80 -- Common processing for expansion of identifiers and expanded names
81 -- Dispatches to specific expansion procedures.
83 procedure Expand_Entry_Index_Parameter (N : Node_Id);
84 -- A reference to the identifier in the entry index specification of
85 -- protected entry body is modified to a reference to a constant definition
86 -- equal to the index of the entry family member being called. This
87 -- constant is calculated as part of the elaboration of the expanded code
88 -- for the body, and is calculated from the object-wide entry index
89 -- returned by Next_Entry_Call.
91 procedure Expand_Entry_Parameter (N : Node_Id);
92 -- A reference to an entry parameter is modified to be a reference to the
93 -- corresponding component of the entry parameter record that is passed by
94 -- the runtime to the accept body procedure.
96 procedure Expand_Formal (N : Node_Id);
97 -- A reference to a formal parameter of a protected subprogram is expanded
98 -- into the corresponding formal of the unprotected procedure used to
99 -- represent the operation within the protected object. In other cases
100 -- Expand_Formal is a no-op.
102 procedure Expand_Protected_Private (N : Node_Id);
103 -- A reference to a private component of a protected type is expanded to a
104 -- component selected from the record used to implement the protected
105 -- object. Such a record is passed to all operations on a protected object
106 -- in a parameter named _object. This object is a constant in the body of a
107 -- function, and a variable within a procedure or entry body.
109 procedure Expand_Renaming (N : Node_Id);
110 -- For renamings, just replace the identifier by the corresponding
111 -- named expression. Note that this has been evaluated (see routine
112 -- Exp_Ch8.Expand_N_Object_Renaming.Evaluate_Name) so this gives
113 -- the correct renaming semantics.
115 --------------------------
116 -- Expand_Current_Value --
117 --------------------------
119 procedure Expand_Current_Value (N : Node_Id) is
120 Loc : constant Source_Ptr := Sloc (N);
121 E : constant Entity_Id := Entity (N);
122 CV : constant Node_Id := Current_Value (E);
123 T : constant Entity_Id := Etype (N);
127 -- Start of processing for Expand_Current_Value
132 -- No replacement if value raises constraint error
134 and then Nkind (CV) /= N_Raise_Constraint_Error
136 -- Do this only for discrete types
138 and then Is_Discrete_Type (T)
140 -- Do not replace biased types, since it is problematic to
141 -- consistently generate a sensible constant value in this case.
143 and then not Has_Biased_Representation (T)
145 -- Do not replace lvalues
147 and then not May_Be_Lvalue (N)
149 -- Check that entity is suitable for replacement
151 and then OK_To_Do_Constant_Replacement (E)
153 -- Do not replace occurrences in pragmas (where names typically
154 -- appear not as values, but as simply names. If there are cases
155 -- where values are required, it is only a very minor efficiency
156 -- issue that they do not get replaced when they could be).
158 and then Nkind (Parent (N)) /= N_Pragma_Argument_Association
160 -- Do not replace the prefixes of attribute references, since this
161 -- causes trouble with cases like 4'Size. Also for Name_Asm_Input and
162 -- Name_Asm_Output, don't do replacement anywhere, since we can have
163 -- lvalue references in the arguments.
165 and then not (Nkind (Parent (N)) = N_Attribute_Reference
167 (Attribute_Name (Parent (N)) = Name_Asm_Input
169 Attribute_Name (Parent (N)) = Name_Asm_Output
171 Prefix (Parent (N)) = N))
174 -- Case of Current_Value is a compile time known value
176 if Nkind (CV) in N_Subexpr then
179 -- Case of Current_Value is a conditional expression reference
182 Get_Current_Value_Condition (N, Op, Val);
184 if Op /= N_Op_Eq then
189 -- If constant value is an occurrence of an enumeration literal,
190 -- then we just make another occurence of the same literal.
192 if Is_Entity_Name (Val)
193 and then Ekind (Entity (Val)) = E_Enumeration_Literal
196 Unchecked_Convert_To (T,
197 New_Occurrence_Of (Entity (Val), Loc)));
199 -- Otherwise get the value, and convert to appropriate type
203 Unchecked_Convert_To (T,
204 Make_Integer_Literal (Loc,
205 Intval => Expr_Rep_Value (Val))));
208 Analyze_And_Resolve (N, T);
209 Set_Is_Static_Expression (N, False);
211 end Expand_Current_Value;
213 -------------------------
214 -- Expand_Discriminant --
215 -------------------------
217 procedure Expand_Discriminant (N : Node_Id) is
218 Scop : constant Entity_Id := Scope (Entity (N));
220 Parent_P : Node_Id := Parent (P);
221 In_Entry : Boolean := False;
224 -- The Incomplete_Or_Private_Kind happens while resolving the
225 -- discriminant constraint involved in a derived full type,
228 -- type D is private;
229 -- type D(C : ...) is new T(C);
231 if Ekind (Scop) = E_Record_Type
232 or Ekind (Scop) in Incomplete_Or_Private_Kind
234 -- Find the origin by walking up the tree till the component
237 while Present (Parent_P)
238 and then Nkind (Parent_P) /= N_Component_Declaration
241 Parent_P := Parent (P);
244 -- If the discriminant reference was part of the default expression
245 -- it has to be "discriminalized"
247 if Present (Parent_P) and then P = Expression (Parent_P) then
248 Set_Entity (N, Discriminal (Entity (N)));
251 elsif Is_Concurrent_Type (Scop) then
252 while Present (Parent_P)
253 and then Nkind (Parent_P) /= N_Subprogram_Body
257 if Nkind (P) = N_Entry_Declaration then
261 Parent_P := Parent (Parent_P);
264 -- If the discriminant occurs within the default expression for a
265 -- formal of an entry or protected operation, create a default
266 -- function for it, and replace the discriminant with a reference to
267 -- the discriminant of the formal of the default function. The
268 -- discriminant entity is the one defined in the corresponding
271 if Present (Parent_P)
272 and then Present (Corresponding_Spec (Parent_P))
275 Loc : constant Source_Ptr := Sloc (N);
276 D_Fun : constant Entity_Id := Corresponding_Spec (Parent_P);
277 Formal : constant Entity_Id := First_Formal (D_Fun);
282 -- Verify that we are within a default function: the type of
283 -- its formal parameter is the same task or protected type.
286 and then Etype (Formal) = Scope (Entity (N))
288 Disc := CR_Discriminant (Entity (N));
291 Make_Selected_Component (Loc,
292 Prefix => New_Occurrence_Of (Formal, Loc),
293 Selector_Name => New_Occurrence_Of (Disc, Loc));
295 Set_Etype (New_N, Etype (N));
299 Set_Entity (N, Discriminal (Entity (N)));
303 elsif Nkind (Parent (N)) = N_Range
306 Set_Entity (N, CR_Discriminant (Entity (N)));
308 Set_Entity (N, Discriminal (Entity (N)));
312 Set_Entity (N, Discriminal (Entity (N)));
314 end Expand_Discriminant;
316 -----------------------------
317 -- Expand_Entity_Reference --
318 -----------------------------
320 procedure Expand_Entity_Reference (N : Node_Id) is
321 E : constant Entity_Id := Entity (N);
324 -- Defend against errors
326 if No (E) and then Total_Errors_Detected /= 0 then
330 if Ekind (E) = E_Discriminant then
331 Expand_Discriminant (N);
333 elsif Is_Entry_Formal (E) then
334 Expand_Entry_Parameter (N);
336 elsif Ekind (E) = E_Component
337 and then Is_Protected_Private (E)
339 -- Protect against junk use of tasking in no run time mode
341 if No_Run_Time_Mode then
345 Expand_Protected_Private (N);
347 elsif Ekind (E) = E_Entry_Index_Parameter then
348 Expand_Entry_Index_Parameter (N);
350 elsif Is_Formal (E) then
353 elsif Is_Renaming_Of_Object (E) then
356 elsif Ekind (E) = E_Variable
357 and then Is_Shared_Passive (E)
359 Expand_Shared_Passive_Variable (N);
362 -- Interpret possible Current_Value for variable case
364 if (Ekind (E) = E_Variable
366 Ekind (E) = E_In_Out_Parameter
368 Ekind (E) = E_Out_Parameter)
369 and then Present (Current_Value (E))
371 Expand_Current_Value (N);
373 -- We do want to warn for the case of a boolean variable (not a
374 -- boolean constant) whose value is known at compile time.
376 if Is_Boolean_Type (Etype (N)) then
377 Warn_On_Known_Condition (N);
380 -- Don't mess with Current_Value for compile time known values. Not
381 -- only is it unnecessary, but we could disturb an indication of a
382 -- static value, which could cause semantic trouble.
384 elsif Compile_Time_Known_Value (N) then
387 -- Interpret possible Current_Value for constant case
389 elsif (Ekind (E) = E_Constant
391 Ekind (E) = E_In_Parameter
393 Ekind (E) = E_Loop_Parameter)
394 and then Present (Current_Value (E))
396 Expand_Current_Value (N);
398 end Expand_Entity_Reference;
400 ----------------------------------
401 -- Expand_Entry_Index_Parameter --
402 ----------------------------------
404 procedure Expand_Entry_Index_Parameter (N : Node_Id) is
406 Set_Entity (N, Entry_Index_Constant (Entity (N)));
407 end Expand_Entry_Index_Parameter;
409 ----------------------------
410 -- Expand_Entry_Parameter --
411 ----------------------------
413 procedure Expand_Entry_Parameter (N : Node_Id) is
414 Loc : constant Source_Ptr := Sloc (N);
415 Ent_Formal : constant Entity_Id := Entity (N);
416 Ent_Spec : constant Entity_Id := Scope (Ent_Formal);
417 Parm_Type : constant Entity_Id := Entry_Parameters_Type (Ent_Spec);
418 Acc_Stack : constant Elist_Id := Accept_Address (Ent_Spec);
419 Addr_Ent : constant Entity_Id := Node (Last_Elmt (Acc_Stack));
420 P_Comp_Ref : Entity_Id;
422 function In_Assignment_Context (N : Node_Id) return Boolean;
423 -- Check whether this is a context in which the entry formal may be
426 ---------------------------
427 -- In_Assignment_Context --
428 ---------------------------
430 function In_Assignment_Context (N : Node_Id) return Boolean is
432 -- Case of use in a call
434 -- ??? passing a formal as actual for a mode IN formal is
435 -- considered as an assignment?
437 if Nkind (Parent (N)) = N_Procedure_Call_Statement
438 or else Nkind (Parent (N)) = N_Entry_Call_Statement
440 (Nkind (Parent (N)) = N_Assignment_Statement
441 and then N = Name (Parent (N)))
445 -- Case of a parameter association: climb up to enclosing call
447 elsif Nkind (Parent (N)) = N_Parameter_Association then
448 return In_Assignment_Context (Parent (N));
450 -- Case of a selected component, indexed component or slice prefix:
451 -- climb up the tree, unless the prefix is of an access type (in
452 -- which case there is an implicit dereference, and the formal itself
453 -- is not being assigned to).
455 elsif (Nkind (Parent (N)) = N_Selected_Component
456 or else Nkind (Parent (N)) = N_Indexed_Component
457 or else Nkind (Parent (N)) = N_Slice)
458 and then N = Prefix (Parent (N))
459 and then not Is_Access_Type (Etype (N))
460 and then In_Assignment_Context (Parent (N))
467 end In_Assignment_Context;
469 -- Start of processing for Expand_Entry_Parameter
472 if Is_Task_Type (Scope (Ent_Spec))
473 and then Comes_From_Source (Ent_Formal)
475 -- Before replacing the formal with the local renaming that is used
476 -- in the accept block, note if this is an assignment context, and
477 -- note the modification to avoid spurious warnings, because the
478 -- original entity is not used further. If formal is unconstrained,
479 -- we also generate an extra parameter to hold the Constrained
480 -- attribute of the actual. No renaming is generated for this flag.
482 if Ekind (Entity (N)) /= E_In_Parameter
483 and then In_Assignment_Context (N)
485 Note_Possible_Modification (N);
488 Rewrite (N, New_Occurrence_Of (Renamed_Object (Entity (N)), Loc));
492 -- What we need is a reference to the corresponding component of the
493 -- parameter record object. The Accept_Address field of the entry entity
494 -- references the address variable that contains the address of the
495 -- accept parameters record. We first have to do an unchecked conversion
496 -- to turn this into a pointer to the parameter record and then we
497 -- select the required parameter field.
500 Make_Selected_Component (Loc,
502 Make_Explicit_Dereference (Loc,
503 Unchecked_Convert_To (Parm_Type,
504 New_Reference_To (Addr_Ent, Loc))),
506 New_Reference_To (Entry_Component (Ent_Formal), Loc));
508 -- For all types of parameters, the constructed parameter record object
509 -- contains a pointer to the parameter. Thus we must dereference them to
510 -- access them (this will often be redundant, since the needed deference
511 -- is implicit, but no harm is done by making it explicit).
514 Make_Explicit_Dereference (Loc, P_Comp_Ref));
517 end Expand_Entry_Parameter;
523 procedure Expand_Formal (N : Node_Id) is
524 E : constant Entity_Id := Entity (N);
525 Scop : constant Entity_Id := Scope (E);
528 -- Check whether the subprogram of which this is a formal is
529 -- a protected operation. The initialization procedure for
530 -- the corresponding record type is not itself a protected operation.
532 if Is_Protected_Type (Scope (Scop))
533 and then not Is_Init_Proc (Scop)
534 and then Present (Protected_Formal (E))
536 Set_Entity (N, Protected_Formal (E));
540 ----------------------------
541 -- Expand_N_Expanded_Name --
542 ----------------------------
544 procedure Expand_N_Expanded_Name (N : Node_Id) is
546 Expand_Entity_Reference (N);
547 end Expand_N_Expanded_Name;
549 -------------------------
550 -- Expand_N_Identifier --
551 -------------------------
553 procedure Expand_N_Identifier (N : Node_Id) is
555 Expand_Entity_Reference (N);
556 end Expand_N_Identifier;
558 ---------------------------
559 -- Expand_N_Real_Literal --
560 ---------------------------
562 procedure Expand_N_Real_Literal (N : Node_Id) is
564 if Vax_Float (Etype (N)) then
565 Expand_Vax_Real_Literal (N);
567 end Expand_N_Real_Literal;
569 ------------------------------
570 -- Expand_Protected_Private --
571 ------------------------------
573 procedure Expand_Protected_Private (N : Node_Id) is
574 Loc : constant Source_Ptr := Sloc (N);
575 E : constant Entity_Id := Entity (N);
576 Op : constant Node_Id := Protected_Operation (E);
583 if Nkind (Op) /= N_Subprogram_Body
584 or else Nkind (Specification (Op)) /= N_Function_Specification
586 Set_Ekind (Prival (E), E_Variable);
588 Set_Ekind (Prival (E), E_Constant);
591 -- If the private component appears in an assignment (either lhs or
592 -- rhs) and is a one-dimensional array constrained by a discriminant,
593 -- rewrite as P (Lo .. Hi) with an explicit range, so that discriminal
594 -- is directly visible. This solves delicate visibility problems.
596 if Comes_From_Source (N)
597 and then Is_Array_Type (Etype (E))
598 and then Number_Dimensions (Etype (E)) = 1
599 and then not Within_Init_Proc
601 Lo := Type_Low_Bound (Etype (First_Index (Etype (E))));
602 Hi := Type_High_Bound (Etype (First_Index (Etype (E))));
604 if Nkind (Parent (N)) = N_Assignment_Statement
605 and then ((Is_Entity_Name (Lo)
606 and then Ekind (Entity (Lo)) = E_In_Parameter)
607 or else (Is_Entity_Name (Hi)
609 Ekind (Entity (Hi)) = E_In_Parameter))
611 D_Range := New_Node (N_Range, Loc);
613 if Is_Entity_Name (Lo)
614 and then Ekind (Entity (Lo)) = E_In_Parameter
616 Set_Low_Bound (D_Range,
617 Make_Identifier (Loc, Chars (Entity (Lo))));
619 Set_Low_Bound (D_Range, Duplicate_Subexpr (Lo));
622 if Is_Entity_Name (Hi)
623 and then Ekind (Entity (Hi)) = E_In_Parameter
625 Set_High_Bound (D_Range,
626 Make_Identifier (Loc, Chars (Entity (Hi))));
628 Set_High_Bound (D_Range, Duplicate_Subexpr (Hi));
633 Prefix => New_Occurrence_Of (E, Loc),
634 Discrete_Range => D_Range));
636 Analyze_And_Resolve (N, Etype (E));
641 -- The type of the reference is the type of the prival, which may differ
642 -- from that of the original component if it is an itype.
644 Set_Entity (N, Prival (E));
645 Set_Etype (N, Etype (Prival (E)));
646 Scop := Current_Scope;
648 -- Find entity for protected operation, which must be on scope stack
650 while not Is_Protected_Type (Scope (Scop)) loop
651 Scop := Scope (Scop);
654 Append_Elmt (N, Privals_Chain (Scop));
655 end Expand_Protected_Private;
657 ---------------------
658 -- Expand_Renaming --
659 ---------------------
661 procedure Expand_Renaming (N : Node_Id) is
662 E : constant Entity_Id := Entity (N);
663 T : constant Entity_Id := Etype (N);
666 Rewrite (N, New_Copy_Tree (Renamed_Object (E)));
668 -- We mark the copy as unanalyzed, so that it is sure to be reanalyzed
669 -- at the top level. This is needed in the packed case since we
670 -- specifically avoided expanding packed array references when the
671 -- renaming declaration was analyzed.
673 Reset_Analyzed_Flags (N);
674 Analyze_And_Resolve (N, T);
681 -- This would be trivial, simply a test for an identifier that was a
682 -- reference to a formal, if it were not for the fact that a previous call
683 -- to Expand_Entry_Parameter will have modified the reference to the
684 -- identifier. A formal of a protected entity is rewritten as
686 -- typ!(recobj).rec.all'Constrained
688 -- where rec is a selector whose Entry_Formal link points to the formal
689 -- For a formal of a task entity, the formal is rewritten as a local
692 -- In addition, a formal that is marked volatile because it is aliased
693 -- through an address clause is rewritten as dereference as well.
695 function Param_Entity (N : Node_Id) return Entity_Id is
696 Renamed_Obj : Node_Id;
699 -- Simple reference case
701 if Nkind (N) = N_Identifier or else Nkind (N) = N_Expanded_Name then
702 if Is_Formal (Entity (N)) then
705 -- Handle renamings of formal parameters and formals of tasks that
706 -- are rewritten as renamings.
708 elsif Nkind (Parent (Entity (N))) = N_Object_Renaming_Declaration then
709 Renamed_Obj := Get_Referenced_Object (Renamed_Object (Entity (N)));
711 if Is_Entity_Name (Renamed_Obj)
712 and then Is_Formal (Entity (Renamed_Obj))
714 return Entity (Renamed_Obj);
717 Nkind (Parent (Parent (Entity (N)))) = N_Accept_Statement
724 if Nkind (N) = N_Explicit_Dereference then
726 P : constant Node_Id := Prefix (N);
730 if Nkind (P) = N_Selected_Component then
731 S := Selector_Name (P);
733 if Present (Entry_Formal (Entity (S))) then
734 return Entry_Formal (Entity (S));
737 elsif Nkind (Original_Node (N)) = N_Identifier then
738 return Param_Entity (Original_Node (N));