1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Einfo; use Einfo;
28 with Elists; use Elists;
29 with Errout; use Errout;
30 with Exp_Smem; use Exp_Smem;
31 with Exp_Tss; use Exp_Tss;
32 with Exp_Util; use Exp_Util;
33 with Exp_VFpt; use Exp_VFpt;
34 with Namet; use Namet;
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
80 -- Dispatches to specific expansion procedures.
82 procedure Expand_Entry_Index_Parameter (N : Node_Id);
83 -- A reference to the identifier in the entry index specification of an
84 -- entry body is modified to a reference to a constant definition equal to
85 -- the index of the entry family member being called. This constant is
86 -- calculated as part of the elaboration of the expanded code for the body,
87 -- and is calculated from the object-wide entry index returned by Next_
90 procedure Expand_Entry_Parameter (N : Node_Id);
91 -- A reference to an entry parameter is modified to be a reference to the
92 -- corresponding component of the entry parameter record that is passed by
93 -- the runtime to the accept body procedure.
95 procedure Expand_Formal (N : Node_Id);
96 -- A reference to a formal parameter of a protected subprogram is expanded
97 -- into the corresponding formal of the unprotected procedure used to
98 -- represent the operation within the protected object. In other cases
99 -- Expand_Formal is a no-op.
101 procedure Expand_Protected_Component (N : Node_Id);
102 -- A reference to a private component of a protected type is expanded into
103 -- a reference to the corresponding prival in the current protected entry
106 procedure Expand_Renaming (N : Node_Id);
107 -- For renamings, just replace the identifier by the corresponding
108 -- named 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 May_Be_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 -- Do not replace the prefixes of attribute references, since this
158 -- causes trouble with cases like 4'Size. Also for Name_Asm_Input and
159 -- Name_Asm_Output, don't do replacement anywhere, since we can have
160 -- lvalue references in the arguments.
162 and then not (Nkind (Parent (N)) = N_Attribute_Reference
164 (Attribute_Name (Parent (N)) = Name_Asm_Input
166 Attribute_Name (Parent (N)) = Name_Asm_Output
168 Prefix (Parent (N)) = N))
171 -- Case of Current_Value is a compile time known value
173 if Nkind (CV) in N_Subexpr then
176 -- Case of Current_Value is a conditional expression reference
179 Get_Current_Value_Condition (N, Op, Val);
181 if Op /= N_Op_Eq then
186 -- If constant value is an occurrence of an enumeration literal,
187 -- then we just make another occurence of the same literal.
189 if Is_Entity_Name (Val)
190 and then Ekind (Entity (Val)) = E_Enumeration_Literal
193 Unchecked_Convert_To (T,
194 New_Occurrence_Of (Entity (Val), Loc)));
196 -- Otherwise get the value, and convert to appropriate type
200 Unchecked_Convert_To (T,
201 Make_Integer_Literal (Loc,
202 Intval => Expr_Rep_Value (Val))));
205 Analyze_And_Resolve (N, T);
206 Set_Is_Static_Expression (N, False);
208 end Expand_Current_Value;
210 -------------------------
211 -- Expand_Discriminant --
212 -------------------------
214 procedure Expand_Discriminant (N : Node_Id) is
215 Scop : constant Entity_Id := Scope (Entity (N));
217 Parent_P : Node_Id := Parent (P);
218 In_Entry : Boolean := False;
221 -- The Incomplete_Or_Private_Kind happens while resolving the
222 -- discriminant constraint involved in a derived full type,
225 -- type D is private;
226 -- type D(C : ...) is new T(C);
228 if Ekind (Scop) = E_Record_Type
229 or Ekind (Scop) in Incomplete_Or_Private_Kind
231 -- Find the origin by walking up the tree till the component
234 while Present (Parent_P)
235 and then Nkind (Parent_P) /= N_Component_Declaration
238 Parent_P := Parent (P);
241 -- If the discriminant reference was part of the default expression
242 -- it has to be "discriminalized"
244 if Present (Parent_P) and then P = Expression (Parent_P) then
245 Set_Entity (N, Discriminal (Entity (N)));
248 elsif Is_Concurrent_Type (Scop) then
249 while Present (Parent_P)
250 and then Nkind (Parent_P) /= N_Subprogram_Body
254 if Nkind (P) = N_Entry_Declaration then
258 Parent_P := Parent (Parent_P);
261 -- If the discriminant occurs within the default expression for a
262 -- formal of an entry or protected operation, create a default
263 -- function for it, and replace the discriminant with a reference to
264 -- the discriminant of the formal of the default function. The
265 -- discriminant entity is the one defined in the corresponding
268 if Present (Parent_P)
269 and then Present (Corresponding_Spec (Parent_P))
272 Loc : constant Source_Ptr := Sloc (N);
273 D_Fun : constant Entity_Id := Corresponding_Spec (Parent_P);
274 Formal : constant Entity_Id := First_Formal (D_Fun);
279 -- Verify that we are within a default function: the type of
280 -- its formal parameter is the same task or protected type.
283 and then Etype (Formal) = Scope (Entity (N))
285 Disc := CR_Discriminant (Entity (N));
288 Make_Selected_Component (Loc,
289 Prefix => New_Occurrence_Of (Formal, Loc),
290 Selector_Name => New_Occurrence_Of (Disc, Loc));
292 Set_Etype (New_N, Etype (N));
296 Set_Entity (N, Discriminal (Entity (N)));
300 elsif Nkind (Parent (N)) = N_Range
303 Set_Entity (N, CR_Discriminant (Entity (N)));
305 Set_Entity (N, Discriminal (Entity (N)));
309 Set_Entity (N, Discriminal (Entity (N)));
311 end Expand_Discriminant;
313 -----------------------------
314 -- Expand_Entity_Reference --
315 -----------------------------
317 procedure Expand_Entity_Reference (N : Node_Id) is
318 E : constant Entity_Id := Entity (N);
321 -- Defend against errors
323 if No (E) and then Total_Errors_Detected /= 0 then
327 if Ekind (E) = E_Discriminant then
328 Expand_Discriminant (N);
330 elsif Is_Entry_Formal (E) then
331 Expand_Entry_Parameter (N);
333 elsif Is_Protected_Component (E) then
334 if No_Run_Time_Mode then
338 Expand_Protected_Component (N);
340 elsif Ekind (E) = E_Entry_Index_Parameter then
341 Expand_Entry_Index_Parameter (N);
343 elsif Is_Formal (E) then
346 elsif Is_Renaming_Of_Object (E) then
349 elsif Ekind (E) = E_Variable
350 and then Is_Shared_Passive (E)
352 Expand_Shared_Passive_Variable (N);
355 -- Interpret possible Current_Value for variable case
357 if (Ekind (E) = E_Variable
359 Ekind (E) = E_In_Out_Parameter
361 Ekind (E) = E_Out_Parameter)
362 and then Present (Current_Value (E))
364 Expand_Current_Value (N);
366 -- We do want to warn for the case of a boolean variable (not a
367 -- boolean constant) whose value is known at compile time.
369 if Is_Boolean_Type (Etype (N)) then
370 Warn_On_Known_Condition (N);
373 -- Don't mess with Current_Value for compile time known values. Not
374 -- only is it unnecessary, but we could disturb an indication of a
375 -- static value, which could cause semantic trouble.
377 elsif Compile_Time_Known_Value (N) then
380 -- Interpret possible Current_Value for constant case
382 elsif Is_Constant_Object (E)
383 and then Present (Current_Value (E))
385 Expand_Current_Value (N);
387 end Expand_Entity_Reference;
389 ----------------------------------
390 -- Expand_Entry_Index_Parameter --
391 ----------------------------------
393 procedure Expand_Entry_Index_Parameter (N : Node_Id) is
394 Index_Con : constant Entity_Id := Entry_Index_Constant (Entity (N));
396 Set_Entity (N, Index_Con);
397 Set_Etype (N, Etype (Index_Con));
398 end Expand_Entry_Index_Parameter;
400 ----------------------------
401 -- Expand_Entry_Parameter --
402 ----------------------------
404 procedure Expand_Entry_Parameter (N : Node_Id) is
405 Loc : constant Source_Ptr := Sloc (N);
406 Ent_Formal : constant Entity_Id := Entity (N);
407 Ent_Spec : constant Entity_Id := Scope (Ent_Formal);
408 Parm_Type : constant Entity_Id := Entry_Parameters_Type (Ent_Spec);
409 Acc_Stack : constant Elist_Id := Accept_Address (Ent_Spec);
410 Addr_Ent : constant Entity_Id := Node (Last_Elmt (Acc_Stack));
411 P_Comp_Ref : Entity_Id;
413 function In_Assignment_Context (N : Node_Id) return Boolean;
414 -- Check whether this is a context in which the entry formal may be
417 ---------------------------
418 -- In_Assignment_Context --
419 ---------------------------
421 function In_Assignment_Context (N : Node_Id) return Boolean is
423 -- Case of use in a call
425 -- ??? passing a formal as actual for a mode IN formal is
426 -- considered as an assignment?
428 if Nkind_In (Parent (N), N_Procedure_Call_Statement,
429 N_Entry_Call_Statement)
430 or else (Nkind (Parent (N)) = N_Assignment_Statement
431 and then N = Name (Parent (N)))
435 -- Case of a parameter association: climb up to enclosing call
437 elsif Nkind (Parent (N)) = N_Parameter_Association then
438 return In_Assignment_Context (Parent (N));
440 -- Case of a selected component, indexed component or slice prefix:
441 -- climb up the tree, unless the prefix is of an access type (in
442 -- which case there is an implicit dereference, and the formal itself
443 -- is not being assigned to).
445 elsif Nkind_In (Parent (N), N_Selected_Component,
448 and then N = Prefix (Parent (N))
449 and then not Is_Access_Type (Etype (N))
450 and then In_Assignment_Context (Parent (N))
457 end In_Assignment_Context;
459 -- Start of processing for Expand_Entry_Parameter
462 if Is_Task_Type (Scope (Ent_Spec))
463 and then Comes_From_Source (Ent_Formal)
465 -- Before replacing the formal with the local renaming that is used
466 -- in the accept block, note if this is an assignment context, and
467 -- note the modification to avoid spurious warnings, because the
468 -- original entity is not used further. If formal is unconstrained,
469 -- we also generate an extra parameter to hold the Constrained
470 -- attribute of the actual. No renaming is generated for this flag.
472 -- Calling Node_Posssible_Modifications in the expander is dubious,
473 -- because this generates a cross-reference entry, and should be
474 -- done during semantic processing so it is called in -gnatc mode???
476 if Ekind (Entity (N)) /= E_In_Parameter
477 and then In_Assignment_Context (N)
479 Note_Possible_Modification (N, Sure => True);
482 Rewrite (N, New_Occurrence_Of (Renamed_Object (Entity (N)), Loc));
486 -- What we need is a reference to the corresponding component of the
487 -- parameter record object. The Accept_Address field of the entry entity
488 -- references the address variable that contains the address of the
489 -- accept parameters record. We first have to do an unchecked conversion
490 -- to turn this into a pointer to the parameter record and then we
491 -- select the required parameter field.
494 Make_Selected_Component (Loc,
496 Make_Explicit_Dereference (Loc,
497 Unchecked_Convert_To (Parm_Type,
498 New_Reference_To (Addr_Ent, Loc))),
500 New_Reference_To (Entry_Component (Ent_Formal), Loc));
502 -- For all types of parameters, the constructed parameter record object
503 -- contains a pointer to the parameter. Thus we must dereference them to
504 -- access them (this will often be redundant, since the needed deference
505 -- is implicit, but no harm is done by making it explicit).
508 Make_Explicit_Dereference (Loc, P_Comp_Ref));
511 end Expand_Entry_Parameter;
517 procedure Expand_Formal (N : Node_Id) is
518 E : constant Entity_Id := Entity (N);
519 Scop : constant Entity_Id := Scope (E);
522 -- Check whether the subprogram of which this is a formal is
523 -- a protected operation. The initialization procedure for
524 -- the corresponding record type is not itself a protected operation.
526 if Is_Protected_Type (Scope (Scop))
527 and then not Is_Init_Proc (Scop)
528 and then Present (Protected_Formal (E))
530 Set_Entity (N, Protected_Formal (E));
534 ----------------------------
535 -- Expand_N_Expanded_Name --
536 ----------------------------
538 procedure Expand_N_Expanded_Name (N : Node_Id) is
540 Expand_Entity_Reference (N);
541 end Expand_N_Expanded_Name;
543 -------------------------
544 -- Expand_N_Identifier --
545 -------------------------
547 procedure Expand_N_Identifier (N : Node_Id) is
549 Expand_Entity_Reference (N);
550 end Expand_N_Identifier;
552 ---------------------------
553 -- Expand_N_Real_Literal --
554 ---------------------------
556 procedure Expand_N_Real_Literal (N : Node_Id) is
558 if Vax_Float (Etype (N)) then
559 Expand_Vax_Real_Literal (N);
561 end Expand_N_Real_Literal;
563 --------------------------------
564 -- Expand_Protected_Component --
565 --------------------------------
567 procedure Expand_Protected_Component (N : Node_Id) is
569 function Inside_Eliminated_Body return Boolean;
570 -- Determine whether the current entity is inside a subprogram or an
571 -- entry which has been marked as eliminated.
573 ----------------------------
574 -- Inside_Eliminated_Body --
575 ----------------------------
577 function Inside_Eliminated_Body return Boolean is
578 S : Entity_Id := Current_Scope;
581 while Present (S) loop
582 if (Ekind (S) = E_Entry
583 or else Ekind (S) = E_Entry_Family
584 or else Ekind (S) = E_Function
585 or else Ekind (S) = E_Procedure)
586 and then Is_Eliminated (S)
595 end Inside_Eliminated_Body;
597 -- Start of processing for Expand_Protected_Component
600 -- Eliminated bodies are not expanded and thus do not need privals
602 if not Inside_Eliminated_Body then
604 Priv : constant Entity_Id := Prival (Entity (N));
606 Set_Entity (N, Priv);
607 Set_Etype (N, Etype (Priv));
610 end Expand_Protected_Component;
612 ---------------------
613 -- Expand_Renaming --
614 ---------------------
616 procedure Expand_Renaming (N : Node_Id) is
617 E : constant Entity_Id := Entity (N);
618 T : constant Entity_Id := Etype (N);
621 Rewrite (N, New_Copy_Tree (Renamed_Object (E)));
623 -- We mark the copy as unanalyzed, so that it is sure to be reanalyzed
624 -- at the top level. This is needed in the packed case since we
625 -- specifically avoided expanding packed array references when the
626 -- renaming declaration was analyzed.
628 Reset_Analyzed_Flags (N);
629 Analyze_And_Resolve (N, T);
636 -- This would be trivial, simply a test for an identifier that was a
637 -- reference to a formal, if it were not for the fact that a previous call
638 -- to Expand_Entry_Parameter will have modified the reference to the
639 -- identifier. A formal of a protected entity is rewritten as
641 -- typ!(recobj).rec.all'Constrained
643 -- where rec is a selector whose Entry_Formal link points to the formal
644 -- For a formal of a task entity, the formal is rewritten as a local
647 -- In addition, a formal that is marked volatile because it is aliased
648 -- through an address clause is rewritten as dereference as well.
650 function Param_Entity (N : Node_Id) return Entity_Id is
651 Renamed_Obj : Node_Id;
654 -- Simple reference case
656 if Nkind_In (N, N_Identifier, N_Expanded_Name) then
657 if Is_Formal (Entity (N)) then
660 -- Handle renamings of formal parameters and formals of tasks that
661 -- are rewritten as renamings.
663 elsif Nkind (Parent (Entity (N))) = N_Object_Renaming_Declaration then
664 Renamed_Obj := Get_Referenced_Object (Renamed_Object (Entity (N)));
666 if Is_Entity_Name (Renamed_Obj)
667 and then Is_Formal (Entity (Renamed_Obj))
669 return Entity (Renamed_Obj);
672 Nkind (Parent (Parent (Entity (N)))) = N_Accept_Statement
679 if Nkind (N) = N_Explicit_Dereference then
681 P : constant Node_Id := Prefix (N);
685 if Nkind (P) = N_Selected_Component then
686 S := Selector_Name (P);
688 if Present (Entry_Formal (Entity (S))) then
689 return Entry_Formal (Entity (S));
692 elsif Nkind (Original_Node (N)) = N_Identifier then
693 return Param_Entity (Original_Node (N));