2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / ada / exp_ch13.adb
blobd0004f473a0a87bf32e02452548fa19ea7e8ce8b
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 1 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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;
37 with Opt; use Opt;
38 with Rtsfind; use Rtsfind;
39 with Sem; use Sem;
40 with Sem_Ch7; use Sem_Ch7;
41 with Sem_Ch8; use Sem_Ch8;
42 with Sem_Eval; use Sem_Eval;
43 with Sem_Util; use Sem_Util;
44 with Sinfo; use Sinfo;
45 with Snames; use Snames;
46 with Stand; use Stand;
47 with Tbuild; use Tbuild;
48 with Uintp; use Uintp;
49 with Validsw; use Validsw;
51 package body Exp_Ch13 is
53 ------------------------------------------
54 -- Expand_N_Attribute_Definition_Clause --
55 ------------------------------------------
57 -- Expansion action depends on attribute involved
59 procedure Expand_N_Attribute_Definition_Clause (N : Node_Id) is
60 Loc : constant Source_Ptr := Sloc (N);
61 Exp : constant Node_Id := Expression (N);
62 Ent : Entity_Id;
63 V : Node_Id;
65 begin
66 Ent := Entity (Name (N));
68 if Is_Type (Ent) then
69 Ent := Underlying_Type (Ent);
70 end if;
72 case Get_Attribute_Id (Chars (N)) is
74 -------------
75 -- Address --
76 -------------
78 when Attribute_Address =>
80 -- If there is an initialization which did not come from the
81 -- source program, then it is an artifact of our expansion, and we
82 -- suppress it. The case we are most concerned about here is the
83 -- initialization of a packed array to all false, which seems
84 -- inappropriate for variable to which an address clause is
85 -- applied. The expression may itself have been rewritten if the
86 -- type is packed array, so we need to examine whether the
87 -- original node is in the source. An exception though is the case
88 -- of an access variable which is default initialized to null, and
89 -- such initialization is retained.
91 -- Furthermore, if the initialization is the equivalent aggregate
92 -- of the type initialization procedure, it replaces an implicit
93 -- call to the init proc, and must be respected. Note that for
94 -- packed types we do not build equivalent aggregates.
96 -- Also, if Init_Or_Norm_Scalars applies, then we need to retain
97 -- any default initialization for objects of scalar types and
98 -- types with scalar components. Normally a composite type will
99 -- have an init_proc in the presence of Init_Or_Norm_Scalars,
100 -- so when that flag is set we have just have to do a test for
101 -- scalar and string types (the predefined string types such as
102 -- String and Wide_String don't have an init_proc).
104 declare
105 Decl : constant Node_Id := Declaration_Node (Ent);
106 Typ : constant Entity_Id := Etype (Ent);
108 begin
109 if Nkind (Decl) = N_Object_Declaration
110 and then Present (Expression (Decl))
111 and then Nkind (Expression (Decl)) /= N_Null
112 and then
113 not Comes_From_Source (Original_Node (Expression (Decl)))
114 then
115 if Present (Base_Init_Proc (Typ))
116 and then
117 Present (Static_Initialization (Base_Init_Proc (Typ)))
118 then
119 null;
121 elsif Init_Or_Norm_Scalars
122 and then
123 (Is_Scalar_Type (Typ) or else Is_String_Type (Typ))
124 then
125 null;
127 else
128 Set_Expression (Decl, Empty);
129 end if;
130 end if;
131 end;
133 ---------------
134 -- Alignment --
135 ---------------
137 when Attribute_Alignment =>
139 -- As required by Gigi, we guarantee that the operand is an
140 -- integer literal (this simplifies things in Gigi).
142 if Nkind (Exp) /= N_Integer_Literal then
143 Rewrite
144 (Exp, Make_Integer_Literal (Loc, Expr_Value (Exp)));
145 end if;
147 ------------------
148 -- Storage_Size --
149 ------------------
151 when Attribute_Storage_Size =>
153 -- If the type is a task type, then assign the value of the
154 -- storage size to the Size variable associated with the task.
155 -- task_typeZ := expression
157 if Ekind (Ent) = E_Task_Type then
158 Insert_Action (N,
159 Make_Assignment_Statement (Loc,
160 Name => New_Reference_To (Storage_Size_Variable (Ent), Loc),
161 Expression =>
162 Convert_To (RTE (RE_Size_Type), Expression (N))));
164 -- For Storage_Size for an access type, create a variable to hold
165 -- the value of the specified size with name typeV and expand an
166 -- assignment statement to initialize this value.
168 elsif Is_Access_Type (Ent) then
170 -- We don't need the variable for a storage size of zero
172 if not No_Pool_Assigned (Ent) then
173 V :=
174 Make_Defining_Identifier (Loc,
175 Chars => New_External_Name (Chars (Ent), 'V'));
177 -- Insert the declaration of the object
179 Insert_Action (N,
180 Make_Object_Declaration (Loc,
181 Defining_Identifier => V,
182 Object_Definition =>
183 New_Reference_To (RTE (RE_Storage_Offset), Loc),
184 Expression =>
185 Convert_To (RTE (RE_Storage_Offset), Expression (N))));
187 Set_Storage_Size_Variable (Ent, Entity_Id (V));
188 end if;
189 end if;
191 -- Other attributes require no expansion
193 when others =>
194 null;
196 end case;
197 end Expand_N_Attribute_Definition_Clause;
199 ----------------------------
200 -- Expand_N_Freeze_Entity --
201 ----------------------------
203 procedure Expand_N_Freeze_Entity (N : Node_Id) is
204 E : constant Entity_Id := Entity (N);
205 E_Scope : Entity_Id;
206 S : Entity_Id;
207 In_Other_Scope : Boolean;
208 In_Outer_Scope : Boolean;
209 Decl : Node_Id;
210 Delete : Boolean := False;
212 begin
213 -- Processing for objects with address clauses
215 if Is_Object (E) and then Present (Address_Clause (E)) then
216 Apply_Address_Clause_Check (E, N);
217 return;
219 -- Only other items requiring any front end action are types and
220 -- subprograms.
222 elsif not Is_Type (E) and then not Is_Subprogram (E) then
223 return;
224 end if;
226 -- Here E is a type or a subprogram
228 E_Scope := Scope (E);
230 -- This is an error protection against previous errors
232 if No (E_Scope) then
233 return;
234 end if;
236 -- Remember that we are processing a freezing entity and its freezing
237 -- nodes. This flag (non-zero = set) is used to avoid the need of
238 -- climbing through the tree while processing the freezing actions (ie.
239 -- to avoid generating spurious warnings or to avoid killing constant
240 -- indications while processing the code associated with freezing
241 -- actions). We use a counter to deal with nesting.
243 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
245 -- If we are freezing entities defined in protected types, they belong
246 -- in the enclosing scope, given that the original type has been
247 -- expanded away. The same is true for entities in task types, in
248 -- particular the parameter records of entries (Entities in bodies are
249 -- all frozen within the body). If we are in the task body, this is a
250 -- proper scope. If we are within a subprogram body, the proper scope
251 -- is the corresponding spec. This may happen for itypes generated in
252 -- the bodies of protected operations.
254 if Ekind (E_Scope) = E_Protected_Type
255 or else (Ekind (E_Scope) = E_Task_Type
256 and then not Has_Completion (E_Scope))
257 then
258 E_Scope := Scope (E_Scope);
260 elsif Ekind (E_Scope) = E_Subprogram_Body then
261 E_Scope := Corresponding_Spec (Unit_Declaration_Node (E_Scope));
262 end if;
264 S := Current_Scope;
265 while S /= Standard_Standard and then S /= E_Scope loop
266 S := Scope (S);
267 end loop;
269 In_Other_Scope := not (S = E_Scope);
270 In_Outer_Scope := (not In_Other_Scope) and then (S /= Current_Scope);
272 -- If the entity being frozen is defined in a scope that is not
273 -- currently on the scope stack, we must establish the proper
274 -- visibility before freezing the entity and related subprograms.
276 if In_Other_Scope then
277 Push_Scope (E_Scope);
278 Install_Visible_Declarations (E_Scope);
280 if Is_Package_Or_Generic_Package (E_Scope) or else
281 Is_Protected_Type (E_Scope) or else
282 Is_Task_Type (E_Scope)
283 then
284 Install_Private_Declarations (E_Scope);
285 end if;
287 -- If the entity is in an outer scope, then that scope needs to
288 -- temporarily become the current scope so that operations created
289 -- during type freezing will be declared in the right scope and
290 -- can properly override any corresponding inherited operations.
292 elsif In_Outer_Scope then
293 Push_Scope (E_Scope);
294 end if;
296 -- If type, freeze the type
298 if Is_Type (E) then
299 Delete := Freeze_Type (N);
301 -- And for enumeration type, build the enumeration tables
303 if Is_Enumeration_Type (E) then
304 Build_Enumeration_Image_Tables (E, N);
305 end if;
307 -- If subprogram, freeze the subprogram
309 elsif Is_Subprogram (E) then
310 Freeze_Subprogram (N);
312 -- Ada 2005 (AI-251): Remove the freezing node associated with the
313 -- entities internally used by the frontend to register primitives
314 -- covering abstract interfaces. The call to Freeze_Subprogram has
315 -- already expanded the code that fills the corresponding entry in
316 -- its secondary dispatch table and therefore the code generator
317 -- has nothing else to do with this freezing node.
319 Delete := Present (Interface_Alias (E));
320 end if;
322 -- Analyze actions generated by freezing. The init_proc contains source
323 -- expressions that may raise Constraint_Error, and the assignment
324 -- procedure for complex types needs checks on individual component
325 -- assignments, but all other freezing actions should be compiled with
326 -- all checks off.
328 if Present (Actions (N)) then
329 Decl := First (Actions (N));
330 while Present (Decl) loop
331 if Nkind (Decl) = N_Subprogram_Body
332 and then (Is_Init_Proc (Defining_Entity (Decl))
333 or else
334 Chars (Defining_Entity (Decl)) = Name_uAssign)
335 then
336 Analyze (Decl);
338 -- A subprogram body created for a renaming_as_body completes
339 -- a previous declaration, which may be in a different scope.
340 -- Establish the proper scope before analysis.
342 elsif Nkind (Decl) = N_Subprogram_Body
343 and then Present (Corresponding_Spec (Decl))
344 and then Scope (Corresponding_Spec (Decl)) /= Current_Scope
345 then
346 Push_Scope (Scope (Corresponding_Spec (Decl)));
347 Analyze (Decl, Suppress => All_Checks);
348 Pop_Scope;
350 -- We treat generated equality specially, if validity checks are
351 -- enabled, in order to detect components default-initialized
352 -- with invalid values.
354 elsif Nkind (Decl) = N_Subprogram_Body
355 and then Chars (Defining_Entity (Decl)) = Name_Op_Eq
356 and then Validity_Checks_On
357 and then Initialize_Scalars
358 then
359 declare
360 Save_Force : constant Boolean := Force_Validity_Checks;
361 begin
362 Force_Validity_Checks := True;
363 Analyze (Decl);
364 Force_Validity_Checks := Save_Force;
365 end;
367 else
368 Analyze (Decl, Suppress => All_Checks);
369 end if;
371 Next (Decl);
372 end loop;
373 end if;
375 -- If we are to delete this N_Freeze_Entity, do so by rewriting so that
376 -- a loop on all nodes being inserted will work propertly.
378 if Delete then
379 Rewrite (N, Make_Null_Statement (Sloc (N)));
380 end if;
382 if In_Other_Scope then
383 if Ekind (Current_Scope) = E_Package then
384 End_Package_Scope (E_Scope);
385 else
386 End_Scope;
387 end if;
389 elsif In_Outer_Scope then
390 Pop_Scope;
391 end if;
393 -- Restore previous value of the nesting-level counter that records
394 -- whether we are inside a (possibly nested) call to this procedure.
396 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
397 end Expand_N_Freeze_Entity;
399 -------------------------------------------
400 -- Expand_N_Record_Representation_Clause --
401 -------------------------------------------
403 -- The only expansion required is for the case of a mod clause present,
404 -- which is removed, and translated into an alignment representation
405 -- clause inserted immediately after the record rep clause with any
406 -- initial pragmas inserted at the start of the component clause list.
408 procedure Expand_N_Record_Representation_Clause (N : Node_Id) is
409 Loc : constant Source_Ptr := Sloc (N);
410 Rectype : constant Entity_Id := Entity (Identifier (N));
411 Mod_Val : Uint;
412 Citems : List_Id;
413 Repitem : Node_Id;
414 AtM_Nod : Node_Id;
416 begin
417 if Present (Mod_Clause (N)) and then not Ignore_Rep_Clauses then
418 Mod_Val := Expr_Value (Expression (Mod_Clause (N)));
419 Citems := Pragmas_Before (Mod_Clause (N));
421 if Present (Citems) then
422 Append_List_To (Citems, Component_Clauses (N));
423 Set_Component_Clauses (N, Citems);
424 end if;
426 AtM_Nod :=
427 Make_Attribute_Definition_Clause (Loc,
428 Name => New_Reference_To (Base_Type (Rectype), Loc),
429 Chars => Name_Alignment,
430 Expression => Make_Integer_Literal (Loc, Mod_Val));
432 Set_From_At_Mod (AtM_Nod);
433 Insert_After (N, AtM_Nod);
434 Set_Mod_Clause (N, Empty);
435 end if;
437 -- If the record representation clause has no components, then
438 -- completely remove it. Note that we also have to remove
439 -- ourself from the Rep Item list.
441 if Is_Empty_List (Component_Clauses (N)) then
442 if First_Rep_Item (Rectype) = N then
443 Set_First_Rep_Item (Rectype, Next_Rep_Item (N));
444 else
445 Repitem := First_Rep_Item (Rectype);
446 while Present (Next_Rep_Item (Repitem)) loop
447 if Next_Rep_Item (Repitem) = N then
448 Set_Next_Rep_Item (Repitem, Next_Rep_Item (N));
449 exit;
450 end if;
452 Next_Rep_Item (Repitem);
453 end loop;
454 end if;
456 Rewrite (N,
457 Make_Null_Statement (Loc));
458 end if;
459 end Expand_N_Record_Representation_Clause;
461 end Exp_Ch13;