1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 Checks; use Checks;
28 with Einfo; use Einfo;
29 with Exp_Ch3; use Exp_Ch3;
30 with Exp_Ch6; use Exp_Ch6;
31 with Exp_Imgv; use Exp_Imgv;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
38 with Restrict; use Restrict;
39 with Rident; use Rident;
40 with Rtsfind; use Rtsfind;
42 with Sem_Ch7; use Sem_Ch7;
43 with Sem_Ch8; use Sem_Ch8;
44 with Sem_Eval; use Sem_Eval;
45 with Sem_Util; use Sem_Util;
46 with Sinfo; use Sinfo;
47 with Snames; use Snames;
48 with Targparm; use Targparm;
49 with Tbuild; use Tbuild;
50 with Uintp; use Uintp;
51 with Validsw; use Validsw;
53 package body Exp_Ch13 is
55 ------------------------------------------
56 -- Expand_N_Attribute_Definition_Clause --
57 ------------------------------------------
59 -- Expansion action depends on attribute involved
61 procedure Expand_N_Attribute_Definition_Clause (N : Node_Id) is
62 Loc : constant Source_Ptr := Sloc (N);
63 Exp : constant Node_Id := Expression (N);
68 Ent := Entity (Name (N));
71 Ent := Underlying_Type (Ent);
74 case Get_Attribute_Id (Chars (N)) is
80 when Attribute_Address =>
82 -- If there is an initialization which did not come from the
83 -- source program, then it is an artifact of our expansion, and we
84 -- suppress it. The case we are most concerned about here is the
85 -- initialization of a packed array to all false, which seems
86 -- inappropriate for variable to which an address clause is
87 -- applied. The expression may itself have been rewritten if the
88 -- type is packed array, so we need to examine whether the
89 -- original node is in the source. An exception though is the case
90 -- of an access variable which is default initialized to null, and
91 -- such initialization is retained.
93 -- Furthermore, if the initialization is the equivalent aggregate
94 -- of the type initialization procedure, it replaces an implicit
95 -- call to the init proc, and must be respected. Note that for
96 -- packed types we do not build equivalent aggregates.
98 -- Also, if Init_Or_Norm_Scalars applies, then we need to retain
99 -- any default initialization for objects of scalar types and
100 -- types with scalar components. Normally a composite type will
101 -- have an init_proc in the presence of Init_Or_Norm_Scalars,
102 -- so when that flag is set we have just have to do a test for
103 -- scalar and string types (the predefined string types such as
104 -- String and Wide_String don't have an init_proc).
107 Decl : constant Node_Id := Declaration_Node (Ent);
108 Typ : constant Entity_Id := Etype (Ent);
111 if Nkind (Decl) = N_Object_Declaration
112 and then Present (Expression (Decl))
113 and then Nkind (Expression (Decl)) /= N_Null
115 not Comes_From_Source (Original_Node (Expression (Decl)))
117 if Present (Base_Init_Proc (Typ))
119 Present (Static_Initialization (Base_Init_Proc (Typ)))
123 elsif Init_Or_Norm_Scalars
125 (Is_Scalar_Type (Typ) or else Is_String_Type (Typ))
130 Set_Expression (Decl, Empty);
133 -- An object declaration to which an address clause applies
134 -- has a delayed freeze, but the address expression itself
135 -- must be elaborated at the point it appears. If the object
136 -- is controlled, additional checks apply elsewhere.
138 elsif Nkind (Decl) = N_Object_Declaration
139 and then not Needs_Constant_Address (Decl, Typ)
141 Remove_Side_Effects (Exp);
149 when Attribute_Alignment =>
151 -- As required by Gigi, we guarantee that the operand is an
152 -- integer literal (this simplifies things in Gigi).
154 if Nkind (Exp) /= N_Integer_Literal then
156 (Exp, Make_Integer_Literal (Loc, Expr_Value (Exp)));
163 when Attribute_Storage_Size =>
165 -- If the type is a task type, then assign the value of the
166 -- storage size to the Size variable associated with the task.
167 -- task_typeZ := expression
169 if Ekind (Ent) = E_Task_Type then
171 Make_Assignment_Statement (Loc,
172 Name => New_Reference_To (Storage_Size_Variable (Ent), Loc),
174 Convert_To (RTE (RE_Size_Type), Expression (N))));
176 -- For Storage_Size for an access type, create a variable to hold
177 -- the value of the specified size with name typeV and expand an
178 -- assignment statement to initialize this value.
180 elsif Is_Access_Type (Ent) then
182 -- We don't need the variable for a storage size of zero
184 if not No_Pool_Assigned (Ent) then
186 Make_Defining_Identifier (Loc,
187 Chars => New_External_Name (Chars (Ent), 'V'));
189 -- Insert the declaration of the object
192 Make_Object_Declaration (Loc,
193 Defining_Identifier => V,
195 New_Reference_To (RTE (RE_Storage_Offset), Loc),
197 Convert_To (RTE (RE_Storage_Offset), Expression (N))));
199 Set_Storage_Size_Variable (Ent, Entity_Id (V));
203 -- Other attributes require no expansion
209 end Expand_N_Attribute_Definition_Clause;
211 -----------------------------
212 -- Expand_N_Free_Statement --
213 -----------------------------
215 procedure Expand_N_Free_Statement (N : Node_Id) is
216 Expr : constant Node_Id := Expression (N);
220 -- Certain run-time configurations and targets do not provide support
221 -- for controlled types.
223 if Restriction_Active (No_Finalization) then
226 -- Do not create a specialized Deallocate since .NET/JVM compilers do
227 -- not support pools and address arithmetic.
229 elsif VM_Target /= No_VM then
233 -- Use the base type to perform the check for finalization master
237 if Ekind (Typ) = E_Access_Subtype then
241 -- Handle private access types
243 if Is_Private_Type (Typ)
244 and then Present (Full_View (Typ))
246 Typ := Full_View (Typ);
249 -- Do not create a custom Deallocate when freeing an object with
250 -- suppressed finalization. In such cases the object is never attached
251 -- to a master, so it does not need to be detached. Use a regular free
252 -- statement instead.
254 if No (Finalization_Master (Typ)) then
258 -- Use a temporary to store the result of a complex expression. Perform
259 -- the following transformation:
261 -- Free (Complex_Expression);
263 -- Temp : constant Type_Of_Expression := Complex_Expression;
266 if Nkind (Expr) /= N_Identifier then
268 Expr_Typ : constant Entity_Id := Etype (Expr);
269 Loc : constant Source_Ptr := Sloc (N);
274 Temp_Id := Make_Temporary (Loc, 'T');
276 Make_Object_Declaration (Loc,
277 Defining_Identifier => Temp_Id,
279 New_Reference_To (Expr_Typ, Loc),
281 Relocate_Node (Expr)));
283 New_Expr := New_Reference_To (Temp_Id, Loc);
284 Set_Etype (New_Expr, Expr_Typ);
286 Set_Expression (N, New_Expr);
290 -- Create a custom Deallocate for a controlled object. This routine
291 -- ensures that the hidden list header will be deallocated along with
292 -- the actual object.
294 Build_Allocate_Deallocate_Proc (N, Is_Allocate => False);
295 end Expand_N_Free_Statement;
297 ----------------------------
298 -- Expand_N_Freeze_Entity --
299 ----------------------------
301 procedure Expand_N_Freeze_Entity (N : Node_Id) is
302 E : constant Entity_Id := Entity (N);
304 In_Other_Scope : Boolean;
305 In_Outer_Scope : Boolean;
307 Delete : Boolean := False;
310 -- If there are delayed aspect specifications, we insert them just
311 -- before the freeze node. They are already analyzed so we don't need
312 -- to reanalyze them (they were analyzed before the type was frozen),
313 -- but we want them in the tree for the back end, and so that the
314 -- listing from sprint is clearer on where these occur logically.
316 if Has_Delayed_Aspects (E) then
322 -- Look for aspect specs for this entity
324 Ritem := First_Rep_Item (E);
325 while Present (Ritem) loop
326 if Nkind (Ritem) = N_Aspect_Specification
327 and then Entity (Ritem) = E
329 Aitem := Aspect_Rep_Item (Ritem);
331 -- Skip this for aspects (e.g. Current_Value) for which
332 -- there is no corresponding pragma or attribute.
334 if Present (Aitem) then
335 pragma Assert (Is_Delayed_Aspect (Aitem));
336 Insert_Before (N, Aitem);
340 Next_Rep_Item (Ritem);
345 -- Processing for objects with address clauses
347 if Is_Object (E) and then Present (Address_Clause (E)) then
348 Apply_Address_Clause_Check (E, N);
351 -- Only other items requiring any front end action are types and
354 elsif not Is_Type (E) and then not Is_Subprogram (E) then
358 -- Here E is a type or a subprogram
360 E_Scope := Scope (E);
362 -- This is an error protection against previous errors
368 -- Remember that we are processing a freezing entity and its freezing
369 -- nodes. This flag (non-zero = set) is used to avoid the need of
370 -- climbing through the tree while processing the freezing actions (ie.
371 -- to avoid generating spurious warnings or to avoid killing constant
372 -- indications while processing the code associated with freezing
373 -- actions). We use a counter to deal with nesting.
375 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
377 -- If we are freezing entities defined in protected types, they belong
378 -- in the enclosing scope, given that the original type has been
379 -- expanded away. The same is true for entities in task types, in
380 -- particular the parameter records of entries (Entities in bodies are
381 -- all frozen within the body). If we are in the task body, this is a
382 -- proper scope. If we are within a subprogram body, the proper scope
383 -- is the corresponding spec. This may happen for itypes generated in
384 -- the bodies of protected operations.
386 if Ekind (E_Scope) = E_Protected_Type
387 or else (Ekind (E_Scope) = E_Task_Type
388 and then not Has_Completion (E_Scope))
390 E_Scope := Scope (E_Scope);
392 elsif Ekind (E_Scope) = E_Subprogram_Body then
393 E_Scope := Corresponding_Spec (Unit_Declaration_Node (E_Scope));
396 -- If the scope of the entity is in open scopes, it is the current one
397 -- or an enclosing one, including a loop, a block, or a subprogram.
399 if In_Open_Scopes (E_Scope) then
400 In_Other_Scope := False;
401 In_Outer_Scope := E_Scope /= Current_Scope;
403 -- Otherwise it is a local package or a different compilation unit
406 In_Other_Scope := True;
407 In_Outer_Scope := False;
410 -- If the entity being frozen is defined in a scope that is not
411 -- currently on the scope stack, we must establish the proper
412 -- visibility before freezing the entity and related subprograms.
414 if In_Other_Scope then
415 Push_Scope (E_Scope);
417 -- Finalizers are little odd in terms of freezing. The spec of the
418 -- procedure appears in the declarations while the body appears in
419 -- the statement part of a single construct. Since the finalizer must
420 -- be called by the At_End handler of the construct, the spec is
421 -- manually frozen right after its declaration. The only side effect
422 -- of this action appears in contexts where the construct is not in
423 -- its final resting place. These contexts are:
425 -- * Entry bodies - The declarations and statements are moved to
426 -- the procedure equivalen of the entry.
427 -- * Protected subprograms - The declarations and statements are
428 -- moved to the non-protected version of the subprogram.
429 -- * Task bodies - The declarations and statements are moved to the
430 -- task body procedure.
432 -- Visible declarations do not need to be installed in these three
433 -- cases since it does not make semantic sense to do so. All entities
434 -- referenced by a finalizer are visible and already resolved, plus
435 -- the enclosing scope may not have visible declarations at all.
437 if Ekind (E) = E_Procedure
438 and then Is_Finalizer (E)
441 or else (Is_Subprogram (E_Scope)
442 and then Is_Protected_Type (Scope (E_Scope)))
443 or else Is_Task_Type (E_Scope))
447 Install_Visible_Declarations (E_Scope);
450 if Is_Package_Or_Generic_Package (E_Scope) or else
451 Is_Protected_Type (E_Scope) or else
452 Is_Task_Type (E_Scope)
454 Install_Private_Declarations (E_Scope);
457 -- If the entity is in an outer scope, then that scope needs to
458 -- temporarily become the current scope so that operations created
459 -- during type freezing will be declared in the right scope and
460 -- can properly override any corresponding inherited operations.
462 elsif In_Outer_Scope then
463 Push_Scope (E_Scope);
466 -- If type, freeze the type
469 Delete := Freeze_Type (N);
471 -- And for enumeration type, build the enumeration tables
473 if Is_Enumeration_Type (E) then
474 Build_Enumeration_Image_Tables (E, N);
477 -- If subprogram, freeze the subprogram
479 elsif Is_Subprogram (E) then
480 Freeze_Subprogram (N);
482 -- Ada 2005 (AI-251): Remove the freezing node associated with the
483 -- entities internally used by the frontend to register primitives
484 -- covering abstract interfaces. The call to Freeze_Subprogram has
485 -- already expanded the code that fills the corresponding entry in
486 -- its secondary dispatch table and therefore the code generator
487 -- has nothing else to do with this freezing node.
489 Delete := Present (Interface_Alias (E));
492 -- Analyze actions generated by freezing. The init_proc contains source
493 -- expressions that may raise Constraint_Error, and the assignment
494 -- procedure for complex types needs checks on individual component
495 -- assignments, but all other freezing actions should be compiled with
498 if Present (Actions (N)) then
499 Decl := First (Actions (N));
500 while Present (Decl) loop
501 if Nkind (Decl) = N_Subprogram_Body
502 and then (Is_Init_Proc (Defining_Entity (Decl))
504 Chars (Defining_Entity (Decl)) = Name_uAssign)
508 -- A subprogram body created for a renaming_as_body completes
509 -- a previous declaration, which may be in a different scope.
510 -- Establish the proper scope before analysis.
512 elsif Nkind (Decl) = N_Subprogram_Body
513 and then Present (Corresponding_Spec (Decl))
514 and then Scope (Corresponding_Spec (Decl)) /= Current_Scope
516 Push_Scope (Scope (Corresponding_Spec (Decl)));
517 Analyze (Decl, Suppress => All_Checks);
520 -- We treat generated equality specially, if validity checks are
521 -- enabled, in order to detect components default-initialized
522 -- with invalid values.
524 elsif Nkind (Decl) = N_Subprogram_Body
525 and then Chars (Defining_Entity (Decl)) = Name_Op_Eq
526 and then Validity_Checks_On
527 and then Initialize_Scalars
530 Save_Force : constant Boolean := Force_Validity_Checks;
532 Force_Validity_Checks := True;
534 Force_Validity_Checks := Save_Force;
538 Analyze (Decl, Suppress => All_Checks);
545 -- If we are to delete this N_Freeze_Entity, do so by rewriting so that
546 -- a loop on all nodes being inserted will work propertly.
549 Rewrite (N, Make_Null_Statement (Sloc (N)));
552 -- Pop scope if we installed one for the analysis
554 if In_Other_Scope then
555 if Ekind (Current_Scope) = E_Package then
556 End_Package_Scope (E_Scope);
561 elsif In_Outer_Scope then
565 -- Restore previous value of the nesting-level counter that records
566 -- whether we are inside a (possibly nested) call to this procedure.
568 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
569 end Expand_N_Freeze_Entity;
571 -------------------------------------------
572 -- Expand_N_Record_Representation_Clause --
573 -------------------------------------------
575 -- The only expansion required is for the case of a mod clause present,
576 -- which is removed, and translated into an alignment representation
577 -- clause inserted immediately after the record rep clause with any
578 -- initial pragmas inserted at the start of the component clause list.
580 procedure Expand_N_Record_Representation_Clause (N : Node_Id) is
581 Loc : constant Source_Ptr := Sloc (N);
582 Rectype : constant Entity_Id := Entity (Identifier (N));
589 if Present (Mod_Clause (N)) and then not Ignore_Rep_Clauses then
590 Mod_Val := Expr_Value (Expression (Mod_Clause (N)));
591 Citems := Pragmas_Before (Mod_Clause (N));
593 if Present (Citems) then
594 Append_List_To (Citems, Component_Clauses (N));
595 Set_Component_Clauses (N, Citems);
599 Make_Attribute_Definition_Clause (Loc,
600 Name => New_Reference_To (Base_Type (Rectype), Loc),
601 Chars => Name_Alignment,
602 Expression => Make_Integer_Literal (Loc, Mod_Val));
604 Set_From_At_Mod (AtM_Nod);
605 Insert_After (N, AtM_Nod);
606 Set_Mod_Clause (N, Empty);
609 -- If the record representation clause has no components, then
610 -- completely remove it. Note that we also have to remove
611 -- ourself from the Rep Item list.
613 if Is_Empty_List (Component_Clauses (N)) then
614 if First_Rep_Item (Rectype) = N then
615 Set_First_Rep_Item (Rectype, Next_Rep_Item (N));
617 Repitem := First_Rep_Item (Rectype);
618 while Present (Next_Rep_Item (Repitem)) loop
619 if Next_Rep_Item (Repitem) = N then
620 Set_Next_Rep_Item (Repitem, Next_Rep_Item (N));
624 Next_Rep_Item (Repitem);
629 Make_Null_Statement (Loc));
631 end Expand_N_Record_Representation_Clause;