2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / ada / exp_util.adb
blobb9e5d389fcec394071e8241751dcc92481f01ebc
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ U T I L --
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 Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Inline; use Inline;
36 with Itypes; use Itypes;
37 with Lib; use Lib;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
40 with Opt; use Opt;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Sem; use Sem;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Ch8; use Sem_Ch8;
46 with Sem_Eval; use Sem_Eval;
47 with Sem_Res; use Sem_Res;
48 with Sem_Type; use Sem_Type;
49 with Sem_Util; use Sem_Util;
50 with Snames; use Snames;
51 with Stand; use Stand;
52 with Stringt; use Stringt;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Ttypes; use Ttypes;
56 with Uintp; use Uintp;
57 with Urealp; use Urealp;
58 with Validsw; use Validsw;
60 package body Exp_Util is
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 function Build_Task_Array_Image
67 (Loc : Source_Ptr;
68 Id_Ref : Node_Id;
69 A_Type : Entity_Id;
70 Dyn : Boolean := False) return Node_Id;
71 -- Build function to generate the image string for a task that is an
72 -- array component, concatenating the images of each index. To avoid
73 -- storage leaks, the string is built with successive slice assignments.
74 -- The flag Dyn indicates whether this is called for the initialization
75 -- procedure of an array of tasks, or for the name of a dynamically
76 -- created task that is assigned to an indexed component.
78 function Build_Task_Image_Function
79 (Loc : Source_Ptr;
80 Decls : List_Id;
81 Stats : List_Id;
82 Res : Entity_Id) return Node_Id;
83 -- Common processing for Task_Array_Image and Task_Record_Image.
84 -- Build function body that computes image.
86 procedure Build_Task_Image_Prefix
87 (Loc : Source_Ptr;
88 Len : out Entity_Id;
89 Res : out Entity_Id;
90 Pos : out Entity_Id;
91 Prefix : Entity_Id;
92 Sum : Node_Id;
93 Decls : List_Id;
94 Stats : List_Id);
95 -- Common processing for Task_Array_Image and Task_Record_Image.
96 -- Create local variables and assign prefix of name to result string.
98 function Build_Task_Record_Image
99 (Loc : Source_Ptr;
100 Id_Ref : Node_Id;
101 Dyn : Boolean := False) return Node_Id;
102 -- Build function to generate the image string for a task that is a
103 -- record component. Concatenate name of variable with that of selector.
104 -- The flag Dyn indicates whether this is called for the initialization
105 -- procedure of record with task components, or for a dynamically
106 -- created task that is assigned to a selected component.
108 function Make_CW_Equivalent_Type
109 (T : Entity_Id;
110 E : Node_Id) return Entity_Id;
111 -- T is a class-wide type entity, E is the initial expression node that
112 -- constrains T in case such as: " X: T := E" or "new T'(E)"
113 -- This function returns the entity of the Equivalent type and inserts
114 -- on the fly the necessary declaration such as:
116 -- type anon is record
117 -- _parent : Root_Type (T); constrained with E discriminants (if any)
118 -- Extension : String (1 .. expr to match size of E);
119 -- end record;
121 -- This record is compatible with any object of the class of T thanks
122 -- to the first field and has the same size as E thanks to the second.
124 function Make_Literal_Range
125 (Loc : Source_Ptr;
126 Literal_Typ : Entity_Id) return Node_Id;
127 -- Produce a Range node whose bounds are:
128 -- Low_Bound (Literal_Type) ..
129 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
130 -- this is used for expanding declarations like X : String := "sdfgdfg";
132 -- If the index type of the target array is not integer, we generate:
133 -- Low_Bound (Literal_Type) ..
134 -- Literal_Type'Val
135 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
136 -- + (Length (Literal_Typ) -1))
138 function Make_Non_Empty_Check
139 (Loc : Source_Ptr;
140 N : Node_Id) return Node_Id;
141 -- Produce a boolean expression checking that the unidimensional array
142 -- node N is not empty.
144 function New_Class_Wide_Subtype
145 (CW_Typ : Entity_Id;
146 N : Node_Id) return Entity_Id;
147 -- Create an implicit subtype of CW_Typ attached to node N
149 ----------------------
150 -- Adjust_Condition --
151 ----------------------
153 procedure Adjust_Condition (N : Node_Id) is
154 begin
155 if No (N) then
156 return;
157 end if;
159 declare
160 Loc : constant Source_Ptr := Sloc (N);
161 T : constant Entity_Id := Etype (N);
162 Ti : Entity_Id;
164 begin
165 -- For now, we simply ignore a call where the argument has no
166 -- type (probably case of unanalyzed condition), or has a type
167 -- that is not Boolean. This is because this is a pretty marginal
168 -- piece of functionality, and violations of these rules are
169 -- likely to be truly marginal (how much code uses Fortran Logical
170 -- as the barrier to a protected entry?) and we do not want to
171 -- blow up existing programs. We can change this to an assertion
172 -- after 3.12a is released ???
174 if No (T) or else not Is_Boolean_Type (T) then
175 return;
176 end if;
178 -- Apply validity checking if needed
180 if Validity_Checks_On and Validity_Check_Tests then
181 Ensure_Valid (N);
182 end if;
184 -- Immediate return if standard boolean, the most common case,
185 -- where nothing needs to be done.
187 if Base_Type (T) = Standard_Boolean then
188 return;
189 end if;
191 -- Case of zero/non-zero semantics or non-standard enumeration
192 -- representation. In each case, we rewrite the node as:
194 -- ityp!(N) /= False'Enum_Rep
196 -- where ityp is an integer type with large enough size to hold
197 -- any value of type T.
199 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
200 if Esize (T) <= Esize (Standard_Integer) then
201 Ti := Standard_Integer;
202 else
203 Ti := Standard_Long_Long_Integer;
204 end if;
206 Rewrite (N,
207 Make_Op_Ne (Loc,
208 Left_Opnd => Unchecked_Convert_To (Ti, N),
209 Right_Opnd =>
210 Make_Attribute_Reference (Loc,
211 Attribute_Name => Name_Enum_Rep,
212 Prefix =>
213 New_Occurrence_Of (First_Literal (T), Loc))));
214 Analyze_And_Resolve (N, Standard_Boolean);
216 else
217 Rewrite (N, Convert_To (Standard_Boolean, N));
218 Analyze_And_Resolve (N, Standard_Boolean);
219 end if;
220 end;
221 end Adjust_Condition;
223 ------------------------
224 -- Adjust_Result_Type --
225 ------------------------
227 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
228 begin
229 -- Ignore call if current type is not Standard.Boolean
231 if Etype (N) /= Standard_Boolean then
232 return;
233 end if;
235 -- If result is already of correct type, nothing to do. Note that
236 -- this will get the most common case where everything has a type
237 -- of Standard.Boolean.
239 if Base_Type (T) = Standard_Boolean then
240 return;
242 else
243 declare
244 KP : constant Node_Kind := Nkind (Parent (N));
246 begin
247 -- If result is to be used as a Condition in the syntax, no need
248 -- to convert it back, since if it was changed to Standard.Boolean
249 -- using Adjust_Condition, that is just fine for this usage.
251 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
252 return;
254 -- If result is an operand of another logical operation, no need
255 -- to reset its type, since Standard.Boolean is just fine, and
256 -- such operations always do Adjust_Condition on their operands.
258 elsif KP in N_Op_Boolean
259 or else KP in N_Short_Circuit
260 or else KP = N_Op_Not
261 then
262 return;
264 -- Otherwise we perform a conversion from the current type,
265 -- which must be Standard.Boolean, to the desired type.
267 else
268 Set_Analyzed (N);
269 Rewrite (N, Convert_To (T, N));
270 Analyze_And_Resolve (N, T);
271 end if;
272 end;
273 end if;
274 end Adjust_Result_Type;
276 --------------------------
277 -- Append_Freeze_Action --
278 --------------------------
280 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
281 Fnode : Node_Id;
283 begin
284 Ensure_Freeze_Node (T);
285 Fnode := Freeze_Node (T);
287 if No (Actions (Fnode)) then
288 Set_Actions (Fnode, New_List);
289 end if;
291 Append (N, Actions (Fnode));
292 end Append_Freeze_Action;
294 ---------------------------
295 -- Append_Freeze_Actions --
296 ---------------------------
298 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
299 Fnode : constant Node_Id := Freeze_Node (T);
301 begin
302 if No (L) then
303 return;
305 else
306 if No (Actions (Fnode)) then
307 Set_Actions (Fnode, L);
308 else
309 Append_List (L, Actions (Fnode));
310 end if;
311 end if;
312 end Append_Freeze_Actions;
314 ------------------------
315 -- Build_Runtime_Call --
316 ------------------------
318 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
319 begin
320 -- If entity is not available, we can skip making the call (this avoids
321 -- junk duplicated error messages in a number of cases).
323 if not RTE_Available (RE) then
324 return Make_Null_Statement (Loc);
325 else
326 return
327 Make_Procedure_Call_Statement (Loc,
328 Name => New_Reference_To (RTE (RE), Loc));
329 end if;
330 end Build_Runtime_Call;
332 ----------------------------
333 -- Build_Task_Array_Image --
334 ----------------------------
336 -- This function generates the body for a function that constructs the
337 -- image string for a task that is an array component. The function is
338 -- local to the init proc for the array type, and is called for each one
339 -- of the components. The constructed image has the form of an indexed
340 -- component, whose prefix is the outer variable of the array type.
341 -- The n-dimensional array type has known indices Index, Index2...
342 -- Id_Ref is an indexed component form created by the enclosing init proc.
343 -- Its successive indices are Val1, Val2, ... which are the loop variables
344 -- in the loops that call the individual task init proc on each component.
346 -- The generated function has the following structure:
348 -- function F return String is
349 -- Pref : string renames Task_Name;
350 -- T1 : String := Index1'Image (Val1);
351 -- ...
352 -- Tn : String := indexn'image (Valn);
353 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
354 -- -- Len includes commas and the end parentheses.
355 -- Res : String (1..Len);
356 -- Pos : Integer := Pref'Length;
358 -- begin
359 -- Res (1 .. Pos) := Pref;
360 -- Pos := Pos + 1;
361 -- Res (Pos) := '(';
362 -- Pos := Pos + 1;
363 -- Res (Pos .. Pos + T1'Length - 1) := T1;
364 -- Pos := Pos + T1'Length;
365 -- Res (Pos) := '.';
366 -- Pos := Pos + 1;
367 -- ...
368 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
369 -- Res (Len) := ')';
371 -- return Res;
372 -- end F;
374 -- Needless to say, multidimensional arrays of tasks are rare enough
375 -- that the bulkiness of this code is not really a concern.
377 function Build_Task_Array_Image
378 (Loc : Source_Ptr;
379 Id_Ref : Node_Id;
380 A_Type : Entity_Id;
381 Dyn : Boolean := False) return Node_Id
383 Dims : constant Nat := Number_Dimensions (A_Type);
384 -- Number of dimensions for array of tasks
386 Temps : array (1 .. Dims) of Entity_Id;
387 -- Array of temporaries to hold string for each index
389 Indx : Node_Id;
390 -- Index expression
392 Len : Entity_Id;
393 -- Total length of generated name
395 Pos : Entity_Id;
396 -- Running index for substring assignments
398 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
399 -- Name of enclosing variable, prefix of resulting name
401 Res : Entity_Id;
402 -- String to hold result
404 Val : Node_Id;
405 -- Value of successive indices
407 Sum : Node_Id;
408 -- Expression to compute total size of string
410 T : Entity_Id;
411 -- Entity for name at one index position
413 Decls : constant List_Id := New_List;
414 Stats : constant List_Id := New_List;
416 begin
417 -- For a dynamic task, the name comes from the target variable.
418 -- For a static one it is a formal of the enclosing init proc.
420 if Dyn then
421 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
422 Append_To (Decls,
423 Make_Object_Declaration (Loc,
424 Defining_Identifier => Pref,
425 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
426 Expression =>
427 Make_String_Literal (Loc,
428 Strval => String_From_Name_Buffer)));
430 else
431 Append_To (Decls,
432 Make_Object_Renaming_Declaration (Loc,
433 Defining_Identifier => Pref,
434 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
435 Name => Make_Identifier (Loc, Name_uTask_Name)));
436 end if;
438 Indx := First_Index (A_Type);
439 Val := First (Expressions (Id_Ref));
441 for J in 1 .. Dims loop
442 T := Make_Temporary (Loc, 'T');
443 Temps (J) := T;
445 Append_To (Decls,
446 Make_Object_Declaration (Loc,
447 Defining_Identifier => T,
448 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
449 Expression =>
450 Make_Attribute_Reference (Loc,
451 Attribute_Name => Name_Image,
452 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
453 Expressions => New_List (New_Copy_Tree (Val)))));
455 Next_Index (Indx);
456 Next (Val);
457 end loop;
459 Sum := Make_Integer_Literal (Loc, Dims + 1);
461 Sum :=
462 Make_Op_Add (Loc,
463 Left_Opnd => Sum,
464 Right_Opnd =>
465 Make_Attribute_Reference (Loc,
466 Attribute_Name => Name_Length,
467 Prefix =>
468 New_Occurrence_Of (Pref, Loc),
469 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
471 for J in 1 .. Dims loop
472 Sum :=
473 Make_Op_Add (Loc,
474 Left_Opnd => Sum,
475 Right_Opnd =>
476 Make_Attribute_Reference (Loc,
477 Attribute_Name => Name_Length,
478 Prefix =>
479 New_Occurrence_Of (Temps (J), Loc),
480 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
481 end loop;
483 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
485 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
487 Append_To (Stats,
488 Make_Assignment_Statement (Loc,
489 Name => Make_Indexed_Component (Loc,
490 Prefix => New_Occurrence_Of (Res, Loc),
491 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
492 Expression =>
493 Make_Character_Literal (Loc,
494 Chars => Name_Find,
495 Char_Literal_Value =>
496 UI_From_Int (Character'Pos ('(')))));
498 Append_To (Stats,
499 Make_Assignment_Statement (Loc,
500 Name => New_Occurrence_Of (Pos, Loc),
501 Expression =>
502 Make_Op_Add (Loc,
503 Left_Opnd => New_Occurrence_Of (Pos, Loc),
504 Right_Opnd => Make_Integer_Literal (Loc, 1))));
506 for J in 1 .. Dims loop
508 Append_To (Stats,
509 Make_Assignment_Statement (Loc,
510 Name => Make_Slice (Loc,
511 Prefix => New_Occurrence_Of (Res, Loc),
512 Discrete_Range =>
513 Make_Range (Loc,
514 Low_Bound => New_Occurrence_Of (Pos, Loc),
515 High_Bound => Make_Op_Subtract (Loc,
516 Left_Opnd =>
517 Make_Op_Add (Loc,
518 Left_Opnd => New_Occurrence_Of (Pos, Loc),
519 Right_Opnd =>
520 Make_Attribute_Reference (Loc,
521 Attribute_Name => Name_Length,
522 Prefix =>
523 New_Occurrence_Of (Temps (J), Loc),
524 Expressions =>
525 New_List (Make_Integer_Literal (Loc, 1)))),
526 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
528 Expression => New_Occurrence_Of (Temps (J), Loc)));
530 if J < Dims then
531 Append_To (Stats,
532 Make_Assignment_Statement (Loc,
533 Name => New_Occurrence_Of (Pos, Loc),
534 Expression =>
535 Make_Op_Add (Loc,
536 Left_Opnd => New_Occurrence_Of (Pos, Loc),
537 Right_Opnd =>
538 Make_Attribute_Reference (Loc,
539 Attribute_Name => Name_Length,
540 Prefix => New_Occurrence_Of (Temps (J), Loc),
541 Expressions =>
542 New_List (Make_Integer_Literal (Loc, 1))))));
544 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
546 Append_To (Stats,
547 Make_Assignment_Statement (Loc,
548 Name => Make_Indexed_Component (Loc,
549 Prefix => New_Occurrence_Of (Res, Loc),
550 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
551 Expression =>
552 Make_Character_Literal (Loc,
553 Chars => Name_Find,
554 Char_Literal_Value =>
555 UI_From_Int (Character'Pos (',')))));
557 Append_To (Stats,
558 Make_Assignment_Statement (Loc,
559 Name => New_Occurrence_Of (Pos, Loc),
560 Expression =>
561 Make_Op_Add (Loc,
562 Left_Opnd => New_Occurrence_Of (Pos, Loc),
563 Right_Opnd => Make_Integer_Literal (Loc, 1))));
564 end if;
565 end loop;
567 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
569 Append_To (Stats,
570 Make_Assignment_Statement (Loc,
571 Name => Make_Indexed_Component (Loc,
572 Prefix => New_Occurrence_Of (Res, Loc),
573 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
574 Expression =>
575 Make_Character_Literal (Loc,
576 Chars => Name_Find,
577 Char_Literal_Value =>
578 UI_From_Int (Character'Pos (')')))));
579 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
580 end Build_Task_Array_Image;
582 ----------------------------
583 -- Build_Task_Image_Decls --
584 ----------------------------
586 function Build_Task_Image_Decls
587 (Loc : Source_Ptr;
588 Id_Ref : Node_Id;
589 A_Type : Entity_Id;
590 In_Init_Proc : Boolean := False) return List_Id
592 Decls : constant List_Id := New_List;
593 T_Id : Entity_Id := Empty;
594 Decl : Node_Id;
595 Expr : Node_Id := Empty;
596 Fun : Node_Id := Empty;
597 Is_Dyn : constant Boolean :=
598 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
599 and then
600 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
602 begin
603 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
604 -- generate a dummy declaration only.
606 if Restriction_Active (No_Implicit_Heap_Allocations)
607 or else Global_Discard_Names
608 then
609 T_Id := Make_Temporary (Loc, 'J');
610 Name_Len := 0;
612 return
613 New_List (
614 Make_Object_Declaration (Loc,
615 Defining_Identifier => T_Id,
616 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
617 Expression =>
618 Make_String_Literal (Loc,
619 Strval => String_From_Name_Buffer)));
621 else
622 if Nkind (Id_Ref) = N_Identifier
623 or else Nkind (Id_Ref) = N_Defining_Identifier
624 then
625 -- For a simple variable, the image of the task is built from
626 -- the name of the variable. To avoid possible conflict with
627 -- the anonymous type created for a single protected object,
628 -- add a numeric suffix.
630 T_Id :=
631 Make_Defining_Identifier (Loc,
632 New_External_Name (Chars (Id_Ref), 'T', 1));
634 Get_Name_String (Chars (Id_Ref));
636 Expr :=
637 Make_String_Literal (Loc,
638 Strval => String_From_Name_Buffer);
640 elsif Nkind (Id_Ref) = N_Selected_Component then
641 T_Id :=
642 Make_Defining_Identifier (Loc,
643 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
644 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
646 elsif Nkind (Id_Ref) = N_Indexed_Component then
647 T_Id :=
648 Make_Defining_Identifier (Loc,
649 New_External_Name (Chars (A_Type), 'N'));
651 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
652 end if;
653 end if;
655 if Present (Fun) then
656 Append (Fun, Decls);
657 Expr := Make_Function_Call (Loc,
658 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
660 if not In_Init_Proc and then VM_Target = No_VM then
661 Set_Uses_Sec_Stack (Defining_Entity (Fun));
662 end if;
663 end if;
665 Decl := Make_Object_Declaration (Loc,
666 Defining_Identifier => T_Id,
667 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
668 Constant_Present => True,
669 Expression => Expr);
671 Append (Decl, Decls);
672 return Decls;
673 end Build_Task_Image_Decls;
675 -------------------------------
676 -- Build_Task_Image_Function --
677 -------------------------------
679 function Build_Task_Image_Function
680 (Loc : Source_Ptr;
681 Decls : List_Id;
682 Stats : List_Id;
683 Res : Entity_Id) return Node_Id
685 Spec : Node_Id;
687 begin
688 Append_To (Stats,
689 Make_Simple_Return_Statement (Loc,
690 Expression => New_Occurrence_Of (Res, Loc)));
692 Spec := Make_Function_Specification (Loc,
693 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
694 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
696 -- Calls to 'Image use the secondary stack, which must be cleaned
697 -- up after the task name is built.
699 return Make_Subprogram_Body (Loc,
700 Specification => Spec,
701 Declarations => Decls,
702 Handled_Statement_Sequence =>
703 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
704 end Build_Task_Image_Function;
706 -----------------------------
707 -- Build_Task_Image_Prefix --
708 -----------------------------
710 procedure Build_Task_Image_Prefix
711 (Loc : Source_Ptr;
712 Len : out Entity_Id;
713 Res : out Entity_Id;
714 Pos : out Entity_Id;
715 Prefix : Entity_Id;
716 Sum : Node_Id;
717 Decls : List_Id;
718 Stats : List_Id)
720 begin
721 Len := Make_Temporary (Loc, 'L', Sum);
723 Append_To (Decls,
724 Make_Object_Declaration (Loc,
725 Defining_Identifier => Len,
726 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
727 Expression => Sum));
729 Res := Make_Temporary (Loc, 'R');
731 Append_To (Decls,
732 Make_Object_Declaration (Loc,
733 Defining_Identifier => Res,
734 Object_Definition =>
735 Make_Subtype_Indication (Loc,
736 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
737 Constraint =>
738 Make_Index_Or_Discriminant_Constraint (Loc,
739 Constraints =>
740 New_List (
741 Make_Range (Loc,
742 Low_Bound => Make_Integer_Literal (Loc, 1),
743 High_Bound => New_Occurrence_Of (Len, Loc)))))));
745 Pos := Make_Temporary (Loc, 'P');
747 Append_To (Decls,
748 Make_Object_Declaration (Loc,
749 Defining_Identifier => Pos,
750 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
752 -- Pos := Prefix'Length;
754 Append_To (Stats,
755 Make_Assignment_Statement (Loc,
756 Name => New_Occurrence_Of (Pos, Loc),
757 Expression =>
758 Make_Attribute_Reference (Loc,
759 Attribute_Name => Name_Length,
760 Prefix => New_Occurrence_Of (Prefix, Loc),
761 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
763 -- Res (1 .. Pos) := Prefix;
765 Append_To (Stats,
766 Make_Assignment_Statement (Loc,
767 Name =>
768 Make_Slice (Loc,
769 Prefix => New_Occurrence_Of (Res, Loc),
770 Discrete_Range =>
771 Make_Range (Loc,
772 Low_Bound => Make_Integer_Literal (Loc, 1),
773 High_Bound => New_Occurrence_Of (Pos, Loc))),
775 Expression => New_Occurrence_Of (Prefix, Loc)));
777 Append_To (Stats,
778 Make_Assignment_Statement (Loc,
779 Name => New_Occurrence_Of (Pos, Loc),
780 Expression =>
781 Make_Op_Add (Loc,
782 Left_Opnd => New_Occurrence_Of (Pos, Loc),
783 Right_Opnd => Make_Integer_Literal (Loc, 1))));
784 end Build_Task_Image_Prefix;
786 -----------------------------
787 -- Build_Task_Record_Image --
788 -----------------------------
790 function Build_Task_Record_Image
791 (Loc : Source_Ptr;
792 Id_Ref : Node_Id;
793 Dyn : Boolean := False) return Node_Id
795 Len : Entity_Id;
796 -- Total length of generated name
798 Pos : Entity_Id;
799 -- Index into result
801 Res : Entity_Id;
802 -- String to hold result
804 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
805 -- Name of enclosing variable, prefix of resulting name
807 Sum : Node_Id;
808 -- Expression to compute total size of string
810 Sel : Entity_Id;
811 -- Entity for selector name
813 Decls : constant List_Id := New_List;
814 Stats : constant List_Id := New_List;
816 begin
817 -- For a dynamic task, the name comes from the target variable.
818 -- For a static one it is a formal of the enclosing init proc.
820 if Dyn then
821 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
822 Append_To (Decls,
823 Make_Object_Declaration (Loc,
824 Defining_Identifier => Pref,
825 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
826 Expression =>
827 Make_String_Literal (Loc,
828 Strval => String_From_Name_Buffer)));
830 else
831 Append_To (Decls,
832 Make_Object_Renaming_Declaration (Loc,
833 Defining_Identifier => Pref,
834 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
835 Name => Make_Identifier (Loc, Name_uTask_Name)));
836 end if;
838 Sel := Make_Temporary (Loc, 'S');
840 Get_Name_String (Chars (Selector_Name (Id_Ref)));
842 Append_To (Decls,
843 Make_Object_Declaration (Loc,
844 Defining_Identifier => Sel,
845 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
846 Expression =>
847 Make_String_Literal (Loc,
848 Strval => String_From_Name_Buffer)));
850 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
852 Sum :=
853 Make_Op_Add (Loc,
854 Left_Opnd => Sum,
855 Right_Opnd =>
856 Make_Attribute_Reference (Loc,
857 Attribute_Name => Name_Length,
858 Prefix =>
859 New_Occurrence_Of (Pref, Loc),
860 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
862 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
864 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
866 -- Res (Pos) := '.';
868 Append_To (Stats,
869 Make_Assignment_Statement (Loc,
870 Name => Make_Indexed_Component (Loc,
871 Prefix => New_Occurrence_Of (Res, Loc),
872 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
873 Expression =>
874 Make_Character_Literal (Loc,
875 Chars => Name_Find,
876 Char_Literal_Value =>
877 UI_From_Int (Character'Pos ('.')))));
879 Append_To (Stats,
880 Make_Assignment_Statement (Loc,
881 Name => New_Occurrence_Of (Pos, Loc),
882 Expression =>
883 Make_Op_Add (Loc,
884 Left_Opnd => New_Occurrence_Of (Pos, Loc),
885 Right_Opnd => Make_Integer_Literal (Loc, 1))));
887 -- Res (Pos .. Len) := Selector;
889 Append_To (Stats,
890 Make_Assignment_Statement (Loc,
891 Name => Make_Slice (Loc,
892 Prefix => New_Occurrence_Of (Res, Loc),
893 Discrete_Range =>
894 Make_Range (Loc,
895 Low_Bound => New_Occurrence_Of (Pos, Loc),
896 High_Bound => New_Occurrence_Of (Len, Loc))),
897 Expression => New_Occurrence_Of (Sel, Loc)));
899 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
900 end Build_Task_Record_Image;
902 ----------------------------------
903 -- Component_May_Be_Bit_Aligned --
904 ----------------------------------
906 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
907 UT : constant Entity_Id := Underlying_Type (Etype (Comp));
909 begin
910 -- If no component clause, then everything is fine, since the back end
911 -- never bit-misaligns by default, even if there is a pragma Packed for
912 -- the record.
914 if No (Component_Clause (Comp)) then
915 return False;
916 end if;
918 -- It is only array and record types that cause trouble
920 if not Is_Record_Type (UT)
921 and then not Is_Array_Type (UT)
922 then
923 return False;
925 -- If we know that we have a small (64 bits or less) record or small
926 -- bit-packed array, then everything is fine, since the back end can
927 -- handle these cases correctly.
929 elsif Esize (Comp) <= 64
930 and then (Is_Record_Type (UT)
931 or else Is_Bit_Packed_Array (UT))
932 then
933 return False;
935 -- Otherwise if the component is not byte aligned, we know we have the
936 -- nasty unaligned case.
938 elsif Normalized_First_Bit (Comp) /= Uint_0
939 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
940 then
941 return True;
943 -- If we are large and byte aligned, then OK at this level
945 else
946 return False;
947 end if;
948 end Component_May_Be_Bit_Aligned;
950 -----------------------------------
951 -- Corresponding_Runtime_Package --
952 -----------------------------------
954 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
955 Pkg_Id : RTU_Id := RTU_Null;
957 begin
958 pragma Assert (Is_Concurrent_Type (Typ));
960 if Ekind (Typ) in Protected_Kind then
961 if Has_Entries (Typ)
962 or else Has_Interrupt_Handler (Typ)
963 or else (Has_Attach_Handler (Typ)
964 and then not Restricted_Profile)
966 -- A protected type without entries that covers an interface and
967 -- overrides the abstract routines with protected procedures is
968 -- considered equivalent to a protected type with entries in the
969 -- context of dispatching select statements. It is sufficient to
970 -- check for the presence of an interface list in the declaration
971 -- node to recognize this case.
973 or else Present (Interface_List (Parent (Typ)))
974 then
975 if Abort_Allowed
976 or else Restriction_Active (No_Entry_Queue) = False
977 or else Number_Entries (Typ) > 1
978 or else (Has_Attach_Handler (Typ)
979 and then not Restricted_Profile)
980 then
981 Pkg_Id := System_Tasking_Protected_Objects_Entries;
982 else
983 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
984 end if;
986 else
987 Pkg_Id := System_Tasking_Protected_Objects;
988 end if;
989 end if;
991 return Pkg_Id;
992 end Corresponding_Runtime_Package;
994 -------------------------------
995 -- Convert_To_Actual_Subtype --
996 -------------------------------
998 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
999 Act_ST : Entity_Id;
1001 begin
1002 Act_ST := Get_Actual_Subtype (Exp);
1004 if Act_ST = Etype (Exp) then
1005 return;
1007 else
1008 Rewrite (Exp,
1009 Convert_To (Act_ST, Relocate_Node (Exp)));
1010 Analyze_And_Resolve (Exp, Act_ST);
1011 end if;
1012 end Convert_To_Actual_Subtype;
1014 -----------------------------------
1015 -- Current_Sem_Unit_Declarations --
1016 -----------------------------------
1018 function Current_Sem_Unit_Declarations return List_Id is
1019 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1020 Decls : List_Id;
1022 begin
1023 -- If the current unit is a package body, locate the visible
1024 -- declarations of the package spec.
1026 if Nkind (U) = N_Package_Body then
1027 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1028 end if;
1030 if Nkind (U) = N_Package_Declaration then
1031 U := Specification (U);
1032 Decls := Visible_Declarations (U);
1034 if No (Decls) then
1035 Decls := New_List;
1036 Set_Visible_Declarations (U, Decls);
1037 end if;
1039 else
1040 Decls := Declarations (U);
1042 if No (Decls) then
1043 Decls := New_List;
1044 Set_Declarations (U, Decls);
1045 end if;
1046 end if;
1048 return Decls;
1049 end Current_Sem_Unit_Declarations;
1051 -----------------------
1052 -- Duplicate_Subexpr --
1053 -----------------------
1055 function Duplicate_Subexpr
1056 (Exp : Node_Id;
1057 Name_Req : Boolean := False) return Node_Id
1059 begin
1060 Remove_Side_Effects (Exp, Name_Req);
1061 return New_Copy_Tree (Exp);
1062 end Duplicate_Subexpr;
1064 ---------------------------------
1065 -- Duplicate_Subexpr_No_Checks --
1066 ---------------------------------
1068 function Duplicate_Subexpr_No_Checks
1069 (Exp : Node_Id;
1070 Name_Req : Boolean := False) return Node_Id
1072 New_Exp : Node_Id;
1074 begin
1075 Remove_Side_Effects (Exp, Name_Req);
1076 New_Exp := New_Copy_Tree (Exp);
1077 Remove_Checks (New_Exp);
1078 return New_Exp;
1079 end Duplicate_Subexpr_No_Checks;
1081 -----------------------------------
1082 -- Duplicate_Subexpr_Move_Checks --
1083 -----------------------------------
1085 function Duplicate_Subexpr_Move_Checks
1086 (Exp : Node_Id;
1087 Name_Req : Boolean := False) return Node_Id
1089 New_Exp : Node_Id;
1091 begin
1092 Remove_Side_Effects (Exp, Name_Req);
1093 New_Exp := New_Copy_Tree (Exp);
1094 Remove_Checks (Exp);
1095 return New_Exp;
1096 end Duplicate_Subexpr_Move_Checks;
1098 --------------------
1099 -- Ensure_Defined --
1100 --------------------
1102 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1103 IR : Node_Id;
1105 begin
1106 -- An itype reference must only be created if this is a local
1107 -- itype, so that gigi can elaborate it on the proper objstack.
1109 if Is_Itype (Typ)
1110 and then Scope (Typ) = Current_Scope
1111 then
1112 IR := Make_Itype_Reference (Sloc (N));
1113 Set_Itype (IR, Typ);
1114 Insert_Action (N, IR);
1115 end if;
1116 end Ensure_Defined;
1118 --------------------
1119 -- Entry_Names_OK --
1120 --------------------
1122 function Entry_Names_OK return Boolean is
1123 begin
1124 return
1125 not Restricted_Profile
1126 and then not Global_Discard_Names
1127 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1128 and then not Restriction_Active (No_Local_Allocators);
1129 end Entry_Names_OK;
1131 ---------------------
1132 -- Evolve_And_Then --
1133 ---------------------
1135 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1136 begin
1137 if No (Cond) then
1138 Cond := Cond1;
1139 else
1140 Cond :=
1141 Make_And_Then (Sloc (Cond1),
1142 Left_Opnd => Cond,
1143 Right_Opnd => Cond1);
1144 end if;
1145 end Evolve_And_Then;
1147 --------------------
1148 -- Evolve_Or_Else --
1149 --------------------
1151 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1152 begin
1153 if No (Cond) then
1154 Cond := Cond1;
1155 else
1156 Cond :=
1157 Make_Or_Else (Sloc (Cond1),
1158 Left_Opnd => Cond,
1159 Right_Opnd => Cond1);
1160 end if;
1161 end Evolve_Or_Else;
1163 ------------------------------
1164 -- Expand_Subtype_From_Expr --
1165 ------------------------------
1167 -- This function is applicable for both static and dynamic allocation of
1168 -- objects which are constrained by an initial expression. Basically it
1169 -- transforms an unconstrained subtype indication into a constrained one.
1170 -- The expression may also be transformed in certain cases in order to
1171 -- avoid multiple evaluation. In the static allocation case, the general
1172 -- scheme is:
1174 -- Val : T := Expr;
1176 -- is transformed into
1178 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1180 -- Here are the main cases :
1182 -- <if Expr is a Slice>
1183 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1185 -- <elsif Expr is a String Literal>
1186 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1188 -- <elsif Expr is Constrained>
1189 -- subtype T is Type_Of_Expr
1190 -- Val : T := Expr;
1192 -- <elsif Expr is an entity_name>
1193 -- Val : T (constraints taken from Expr) := Expr;
1195 -- <else>
1196 -- type Axxx is access all T;
1197 -- Rval : Axxx := Expr'ref;
1198 -- Val : T (constraints taken from Rval) := Rval.all;
1200 -- ??? note: when the Expression is allocated in the secondary stack
1201 -- we could use it directly instead of copying it by declaring
1202 -- Val : T (...) renames Rval.all
1204 procedure Expand_Subtype_From_Expr
1205 (N : Node_Id;
1206 Unc_Type : Entity_Id;
1207 Subtype_Indic : Node_Id;
1208 Exp : Node_Id)
1210 Loc : constant Source_Ptr := Sloc (N);
1211 Exp_Typ : constant Entity_Id := Etype (Exp);
1212 T : Entity_Id;
1214 begin
1215 -- In general we cannot build the subtype if expansion is disabled,
1216 -- because internal entities may not have been defined. However, to
1217 -- avoid some cascaded errors, we try to continue when the expression
1218 -- is an array (or string), because it is safe to compute the bounds.
1219 -- It is in fact required to do so even in a generic context, because
1220 -- there may be constants that depend on bounds of string literal.
1222 if not Expander_Active
1223 and then (No (Etype (Exp))
1224 or else Base_Type (Etype (Exp)) /= Standard_String)
1225 then
1226 return;
1227 end if;
1229 if Nkind (Exp) = N_Slice then
1230 declare
1231 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1233 begin
1234 Rewrite (Subtype_Indic,
1235 Make_Subtype_Indication (Loc,
1236 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1237 Constraint =>
1238 Make_Index_Or_Discriminant_Constraint (Loc,
1239 Constraints => New_List
1240 (New_Reference_To (Slice_Type, Loc)))));
1242 -- This subtype indication may be used later for constraint checks
1243 -- we better make sure that if a variable was used as a bound of
1244 -- of the original slice, its value is frozen.
1246 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
1247 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
1248 end;
1250 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
1251 Rewrite (Subtype_Indic,
1252 Make_Subtype_Indication (Loc,
1253 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1254 Constraint =>
1255 Make_Index_Or_Discriminant_Constraint (Loc,
1256 Constraints => New_List (
1257 Make_Literal_Range (Loc,
1258 Literal_Typ => Exp_Typ)))));
1260 elsif Is_Constrained (Exp_Typ)
1261 and then not Is_Class_Wide_Type (Unc_Type)
1262 then
1263 if Is_Itype (Exp_Typ) then
1265 -- Within an initialization procedure, a selected component
1266 -- denotes a component of the enclosing record, and it appears
1267 -- as an actual in a call to its own initialization procedure.
1268 -- If this component depends on the outer discriminant, we must
1269 -- generate the proper actual subtype for it.
1271 if Nkind (Exp) = N_Selected_Component
1272 and then Within_Init_Proc
1273 then
1274 declare
1275 Decl : constant Node_Id :=
1276 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
1277 begin
1278 if Present (Decl) then
1279 Insert_Action (N, Decl);
1280 T := Defining_Identifier (Decl);
1281 else
1282 T := Exp_Typ;
1283 end if;
1284 end;
1286 -- No need to generate a new one (new what???)
1288 else
1289 T := Exp_Typ;
1290 end if;
1292 else
1293 T := Make_Temporary (Loc, 'T');
1295 Insert_Action (N,
1296 Make_Subtype_Declaration (Loc,
1297 Defining_Identifier => T,
1298 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
1300 -- This type is marked as an itype even though it has an
1301 -- explicit declaration because otherwise it can be marked
1302 -- with Is_Generic_Actual_Type and generate spurious errors.
1303 -- (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1305 Set_Is_Itype (T);
1306 Set_Associated_Node_For_Itype (T, Exp);
1307 end if;
1309 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
1311 -- Nothing needs to be done for private types with unknown discriminants
1312 -- if the underlying type is not an unconstrained composite type or it
1313 -- is an unchecked union.
1315 elsif Is_Private_Type (Unc_Type)
1316 and then Has_Unknown_Discriminants (Unc_Type)
1317 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
1318 or else Is_Constrained (Underlying_Type (Unc_Type))
1319 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
1320 then
1321 null;
1323 -- Case of derived type with unknown discriminants where the parent type
1324 -- also has unknown discriminants.
1326 elsif Is_Record_Type (Unc_Type)
1327 and then not Is_Class_Wide_Type (Unc_Type)
1328 and then Has_Unknown_Discriminants (Unc_Type)
1329 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
1330 then
1331 -- Nothing to be done if no underlying record view available
1333 if No (Underlying_Record_View (Unc_Type)) then
1334 null;
1336 -- Otherwise use the Underlying_Record_View to create the proper
1337 -- constrained subtype for an object of a derived type with unknown
1338 -- discriminants.
1340 else
1341 Remove_Side_Effects (Exp);
1342 Rewrite (Subtype_Indic,
1343 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
1344 end if;
1346 -- Renamings of class-wide interface types require no equivalent
1347 -- constrained type declarations because we only need to reference
1348 -- the tag component associated with the interface.
1350 elsif Present (N)
1351 and then Nkind (N) = N_Object_Renaming_Declaration
1352 and then Is_Interface (Unc_Type)
1353 then
1354 pragma Assert (Is_Class_Wide_Type (Unc_Type));
1355 null;
1357 -- In Ada95, nothing to be done if the type of the expression is
1358 -- limited, because in this case the expression cannot be copied,
1359 -- and its use can only be by reference.
1361 -- In Ada2005, the context can be an object declaration whose expression
1362 -- is a function that returns in place. If the nominal subtype has
1363 -- unknown discriminants, the call still provides constraints on the
1364 -- object, and we have to create an actual subtype from it.
1366 -- If the type is class-wide, the expression is dynamically tagged and
1367 -- we do not create an actual subtype either. Ditto for an interface.
1369 elsif Is_Limited_Type (Exp_Typ)
1370 and then
1371 (Is_Class_Wide_Type (Exp_Typ)
1372 or else Is_Interface (Exp_Typ)
1373 or else not Has_Unknown_Discriminants (Exp_Typ)
1374 or else not Is_Composite_Type (Unc_Type))
1375 then
1376 null;
1378 -- For limited objects initialized with build in place function calls,
1379 -- nothing to be done; otherwise we prematurely introduce an N_Reference
1380 -- node in the expression initializing the object, which breaks the
1381 -- circuitry that detects and adds the additional arguments to the
1382 -- called function.
1384 elsif Is_Build_In_Place_Function_Call (Exp) then
1385 null;
1387 else
1388 Remove_Side_Effects (Exp);
1389 Rewrite (Subtype_Indic,
1390 Make_Subtype_From_Expr (Exp, Unc_Type));
1391 end if;
1392 end Expand_Subtype_From_Expr;
1394 --------------------
1395 -- Find_Init_Call --
1396 --------------------
1398 function Find_Init_Call
1399 (Var : Entity_Id;
1400 Rep_Clause : Node_Id) return Node_Id
1402 Typ : constant Entity_Id := Etype (Var);
1404 Init_Proc : Entity_Id;
1405 -- Initialization procedure for Typ
1407 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
1408 -- Look for init call for Var starting at From and scanning the
1409 -- enclosing list until Rep_Clause or the end of the list is reached.
1411 ----------------------------
1412 -- Find_Init_Call_In_List --
1413 ----------------------------
1415 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
1416 Init_Call : Node_Id;
1417 begin
1418 Init_Call := From;
1420 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
1421 if Nkind (Init_Call) = N_Procedure_Call_Statement
1422 and then Is_Entity_Name (Name (Init_Call))
1423 and then Entity (Name (Init_Call)) = Init_Proc
1424 then
1425 return Init_Call;
1426 end if;
1427 Next (Init_Call);
1428 end loop;
1430 return Empty;
1431 end Find_Init_Call_In_List;
1433 Init_Call : Node_Id;
1435 -- Start of processing for Find_Init_Call
1437 begin
1438 if not Has_Non_Null_Base_Init_Proc (Typ) then
1439 -- No init proc for the type, so obviously no call to be found
1441 return Empty;
1442 end if;
1444 Init_Proc := Base_Init_Proc (Typ);
1446 -- First scan the list containing the declaration of Var
1448 Init_Call := Find_Init_Call_In_List (From => Next (Parent (Var)));
1450 -- If not found, also look on Var's freeze actions list, if any, since
1451 -- the init call may have been moved there (case of an address clause
1452 -- applying to Var).
1454 if No (Init_Call) and then Present (Freeze_Node (Var)) then
1455 Init_Call := Find_Init_Call_In_List
1456 (First (Actions (Freeze_Node (Var))));
1457 end if;
1459 return Init_Call;
1460 end Find_Init_Call;
1462 ------------------------
1463 -- Find_Interface_ADT --
1464 ------------------------
1466 function Find_Interface_ADT
1467 (T : Entity_Id;
1468 Iface : Entity_Id) return Elmt_Id
1470 ADT : Elmt_Id;
1471 Typ : Entity_Id := T;
1473 begin
1474 pragma Assert (Is_Interface (Iface));
1476 -- Handle private types
1478 if Has_Private_Declaration (Typ)
1479 and then Present (Full_View (Typ))
1480 then
1481 Typ := Full_View (Typ);
1482 end if;
1484 -- Handle access types
1486 if Is_Access_Type (Typ) then
1487 Typ := Designated_Type (Typ);
1488 end if;
1490 -- Handle task and protected types implementing interfaces
1492 if Is_Concurrent_Type (Typ) then
1493 Typ := Corresponding_Record_Type (Typ);
1494 end if;
1496 pragma Assert
1497 (not Is_Class_Wide_Type (Typ)
1498 and then Ekind (Typ) /= E_Incomplete_Type);
1500 if Is_Ancestor (Iface, Typ) then
1501 return First_Elmt (Access_Disp_Table (Typ));
1503 else
1504 ADT :=
1505 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
1506 while Present (ADT)
1507 and then Present (Related_Type (Node (ADT)))
1508 and then Related_Type (Node (ADT)) /= Iface
1509 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)))
1510 loop
1511 Next_Elmt (ADT);
1512 end loop;
1514 pragma Assert (Present (Related_Type (Node (ADT))));
1515 return ADT;
1516 end if;
1517 end Find_Interface_ADT;
1519 ------------------------
1520 -- Find_Interface_Tag --
1521 ------------------------
1523 function Find_Interface_Tag
1524 (T : Entity_Id;
1525 Iface : Entity_Id) return Entity_Id
1527 AI_Tag : Entity_Id;
1528 Found : Boolean := False;
1529 Typ : Entity_Id := T;
1531 procedure Find_Tag (Typ : Entity_Id);
1532 -- Internal subprogram used to recursively climb to the ancestors
1534 --------------
1535 -- Find_Tag --
1536 --------------
1538 procedure Find_Tag (Typ : Entity_Id) is
1539 AI_Elmt : Elmt_Id;
1540 AI : Node_Id;
1542 begin
1543 -- This routine does not handle the case in which the interface is an
1544 -- ancestor of Typ. That case is handled by the enclosing subprogram.
1546 pragma Assert (Typ /= Iface);
1548 -- Climb to the root type handling private types
1550 if Present (Full_View (Etype (Typ))) then
1551 if Full_View (Etype (Typ)) /= Typ then
1552 Find_Tag (Full_View (Etype (Typ)));
1553 end if;
1555 elsif Etype (Typ) /= Typ then
1556 Find_Tag (Etype (Typ));
1557 end if;
1559 -- Traverse the list of interfaces implemented by the type
1561 if not Found
1562 and then Present (Interfaces (Typ))
1563 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
1564 then
1565 -- Skip the tag associated with the primary table
1567 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1568 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1569 pragma Assert (Present (AI_Tag));
1571 AI_Elmt := First_Elmt (Interfaces (Typ));
1572 while Present (AI_Elmt) loop
1573 AI := Node (AI_Elmt);
1575 if AI = Iface or else Is_Ancestor (Iface, AI) then
1576 Found := True;
1577 return;
1578 end if;
1580 AI_Tag := Next_Tag_Component (AI_Tag);
1581 Next_Elmt (AI_Elmt);
1582 end loop;
1583 end if;
1584 end Find_Tag;
1586 -- Start of processing for Find_Interface_Tag
1588 begin
1589 pragma Assert (Is_Interface (Iface));
1591 -- Handle access types
1593 if Is_Access_Type (Typ) then
1594 Typ := Designated_Type (Typ);
1595 end if;
1597 -- Handle class-wide types
1599 if Is_Class_Wide_Type (Typ) then
1600 Typ := Root_Type (Typ);
1601 end if;
1603 -- Handle private types
1605 if Has_Private_Declaration (Typ)
1606 and then Present (Full_View (Typ))
1607 then
1608 Typ := Full_View (Typ);
1609 end if;
1611 -- Handle entities from the limited view
1613 if Ekind (Typ) = E_Incomplete_Type then
1614 pragma Assert (Present (Non_Limited_View (Typ)));
1615 Typ := Non_Limited_View (Typ);
1616 end if;
1618 -- Handle task and protected types implementing interfaces
1620 if Is_Concurrent_Type (Typ) then
1621 Typ := Corresponding_Record_Type (Typ);
1622 end if;
1624 -- If the interface is an ancestor of the type, then it shared the
1625 -- primary dispatch table.
1627 if Is_Ancestor (Iface, Typ) then
1628 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1629 return First_Tag_Component (Typ);
1631 -- Otherwise we need to search for its associated tag component
1633 else
1634 Find_Tag (Typ);
1635 pragma Assert (Found);
1636 return AI_Tag;
1637 end if;
1638 end Find_Interface_Tag;
1640 ------------------
1641 -- Find_Prim_Op --
1642 ------------------
1644 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
1645 Prim : Elmt_Id;
1646 Typ : Entity_Id := T;
1647 Op : Entity_Id;
1649 begin
1650 if Is_Class_Wide_Type (Typ) then
1651 Typ := Root_Type (Typ);
1652 end if;
1654 Typ := Underlying_Type (Typ);
1656 -- Loop through primitive operations
1658 Prim := First_Elmt (Primitive_Operations (Typ));
1659 while Present (Prim) loop
1660 Op := Node (Prim);
1662 -- We can retrieve primitive operations by name if it is an internal
1663 -- name. For equality we must check that both of its operands have
1664 -- the same type, to avoid confusion with user-defined equalities
1665 -- than may have a non-symmetric signature.
1667 exit when Chars (Op) = Name
1668 and then
1669 (Name /= Name_Op_Eq
1670 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
1672 Next_Elmt (Prim);
1674 -- Raise Program_Error if no primitive found
1676 if No (Prim) then
1677 raise Program_Error;
1678 end if;
1679 end loop;
1681 return Node (Prim);
1682 end Find_Prim_Op;
1684 ------------------
1685 -- Find_Prim_Op --
1686 ------------------
1688 function Find_Prim_Op
1689 (T : Entity_Id;
1690 Name : TSS_Name_Type) return Entity_Id
1692 Prim : Elmt_Id;
1693 Typ : Entity_Id := T;
1695 begin
1696 if Is_Class_Wide_Type (Typ) then
1697 Typ := Root_Type (Typ);
1698 end if;
1700 Typ := Underlying_Type (Typ);
1702 Prim := First_Elmt (Primitive_Operations (Typ));
1703 while not Is_TSS (Node (Prim), Name) loop
1704 Next_Elmt (Prim);
1706 -- Raise program error if no primitive found
1708 if No (Prim) then
1709 raise Program_Error;
1710 end if;
1711 end loop;
1713 return Node (Prim);
1714 end Find_Prim_Op;
1716 ----------------------------
1717 -- Find_Protection_Object --
1718 ----------------------------
1720 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
1721 S : Entity_Id;
1723 begin
1724 S := Scop;
1725 while Present (S) loop
1726 if (Ekind (S) = E_Entry
1727 or else Ekind (S) = E_Entry_Family
1728 or else Ekind (S) = E_Function
1729 or else Ekind (S) = E_Procedure)
1730 and then Present (Protection_Object (S))
1731 then
1732 return Protection_Object (S);
1733 end if;
1735 S := Scope (S);
1736 end loop;
1738 -- If we do not find a Protection object in the scope chain, then
1739 -- something has gone wrong, most likely the object was never created.
1741 raise Program_Error;
1742 end Find_Protection_Object;
1744 ----------------------
1745 -- Force_Evaluation --
1746 ----------------------
1748 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
1749 begin
1750 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
1751 end Force_Evaluation;
1753 ------------------------
1754 -- Generate_Poll_Call --
1755 ------------------------
1757 procedure Generate_Poll_Call (N : Node_Id) is
1758 begin
1759 -- No poll call if polling not active
1761 if not Polling_Required then
1762 return;
1764 -- Otherwise generate require poll call
1766 else
1767 Insert_Before_And_Analyze (N,
1768 Make_Procedure_Call_Statement (Sloc (N),
1769 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
1770 end if;
1771 end Generate_Poll_Call;
1773 ---------------------------------
1774 -- Get_Current_Value_Condition --
1775 ---------------------------------
1777 -- Note: the implementation of this procedure is very closely tied to the
1778 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
1779 -- interpret Current_Value fields set by the Set procedure, so the two
1780 -- procedures need to be closely coordinated.
1782 procedure Get_Current_Value_Condition
1783 (Var : Node_Id;
1784 Op : out Node_Kind;
1785 Val : out Node_Id)
1787 Loc : constant Source_Ptr := Sloc (Var);
1788 Ent : constant Entity_Id := Entity (Var);
1790 procedure Process_Current_Value_Condition
1791 (N : Node_Id;
1792 S : Boolean);
1793 -- N is an expression which holds either True (S = True) or False (S =
1794 -- False) in the condition. This procedure digs out the expression and
1795 -- if it refers to Ent, sets Op and Val appropriately.
1797 -------------------------------------
1798 -- Process_Current_Value_Condition --
1799 -------------------------------------
1801 procedure Process_Current_Value_Condition
1802 (N : Node_Id;
1803 S : Boolean)
1805 Cond : Node_Id;
1806 Sens : Boolean;
1808 begin
1809 Cond := N;
1810 Sens := S;
1812 -- Deal with NOT operators, inverting sense
1814 while Nkind (Cond) = N_Op_Not loop
1815 Cond := Right_Opnd (Cond);
1816 Sens := not Sens;
1817 end loop;
1819 -- Deal with AND THEN and AND cases
1821 if Nkind (Cond) = N_And_Then
1822 or else Nkind (Cond) = N_Op_And
1823 then
1824 -- Don't ever try to invert a condition that is of the form
1825 -- of an AND or AND THEN (since we are not doing sufficiently
1826 -- general processing to allow this).
1828 if Sens = False then
1829 Op := N_Empty;
1830 Val := Empty;
1831 return;
1832 end if;
1834 -- Recursively process AND and AND THEN branches
1836 Process_Current_Value_Condition (Left_Opnd (Cond), True);
1838 if Op /= N_Empty then
1839 return;
1840 end if;
1842 Process_Current_Value_Condition (Right_Opnd (Cond), True);
1843 return;
1845 -- Case of relational operator
1847 elsif Nkind (Cond) in N_Op_Compare then
1848 Op := Nkind (Cond);
1850 -- Invert sense of test if inverted test
1852 if Sens = False then
1853 case Op is
1854 when N_Op_Eq => Op := N_Op_Ne;
1855 when N_Op_Ne => Op := N_Op_Eq;
1856 when N_Op_Lt => Op := N_Op_Ge;
1857 when N_Op_Gt => Op := N_Op_Le;
1858 when N_Op_Le => Op := N_Op_Gt;
1859 when N_Op_Ge => Op := N_Op_Lt;
1860 when others => raise Program_Error;
1861 end case;
1862 end if;
1864 -- Case of entity op value
1866 if Is_Entity_Name (Left_Opnd (Cond))
1867 and then Ent = Entity (Left_Opnd (Cond))
1868 and then Compile_Time_Known_Value (Right_Opnd (Cond))
1869 then
1870 Val := Right_Opnd (Cond);
1872 -- Case of value op entity
1874 elsif Is_Entity_Name (Right_Opnd (Cond))
1875 and then Ent = Entity (Right_Opnd (Cond))
1876 and then Compile_Time_Known_Value (Left_Opnd (Cond))
1877 then
1878 Val := Left_Opnd (Cond);
1880 -- We are effectively swapping operands
1882 case Op is
1883 when N_Op_Eq => null;
1884 when N_Op_Ne => null;
1885 when N_Op_Lt => Op := N_Op_Gt;
1886 when N_Op_Gt => Op := N_Op_Lt;
1887 when N_Op_Le => Op := N_Op_Ge;
1888 when N_Op_Ge => Op := N_Op_Le;
1889 when others => raise Program_Error;
1890 end case;
1892 else
1893 Op := N_Empty;
1894 end if;
1896 return;
1898 -- Case of Boolean variable reference, return as though the
1899 -- reference had said var = True.
1901 else
1902 if Is_Entity_Name (Cond)
1903 and then Ent = Entity (Cond)
1904 then
1905 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
1907 if Sens = False then
1908 Op := N_Op_Ne;
1909 else
1910 Op := N_Op_Eq;
1911 end if;
1912 end if;
1913 end if;
1914 end Process_Current_Value_Condition;
1916 -- Start of processing for Get_Current_Value_Condition
1918 begin
1919 Op := N_Empty;
1920 Val := Empty;
1922 -- Immediate return, nothing doing, if this is not an object
1924 if Ekind (Ent) not in Object_Kind then
1925 return;
1926 end if;
1928 -- Otherwise examine current value
1930 declare
1931 CV : constant Node_Id := Current_Value (Ent);
1932 Sens : Boolean;
1933 Stm : Node_Id;
1935 begin
1936 -- If statement. Condition is known true in THEN section, known False
1937 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
1939 if Nkind (CV) = N_If_Statement then
1941 -- Before start of IF statement
1943 if Loc < Sloc (CV) then
1944 return;
1946 -- After end of IF statement
1948 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
1949 return;
1950 end if;
1952 -- At this stage we know that we are within the IF statement, but
1953 -- unfortunately, the tree does not record the SLOC of the ELSE so
1954 -- we cannot use a simple SLOC comparison to distinguish between
1955 -- the then/else statements, so we have to climb the tree.
1957 declare
1958 N : Node_Id;
1960 begin
1961 N := Parent (Var);
1962 while Parent (N) /= CV loop
1963 N := Parent (N);
1965 -- If we fall off the top of the tree, then that's odd, but
1966 -- perhaps it could occur in some error situation, and the
1967 -- safest response is simply to assume that the outcome of
1968 -- the condition is unknown. No point in bombing during an
1969 -- attempt to optimize things.
1971 if No (N) then
1972 return;
1973 end if;
1974 end loop;
1976 -- Now we have N pointing to a node whose parent is the IF
1977 -- statement in question, so now we can tell if we are within
1978 -- the THEN statements.
1980 if Is_List_Member (N)
1981 and then List_Containing (N) = Then_Statements (CV)
1982 then
1983 Sens := True;
1985 -- If the variable reference does not come from source, we
1986 -- cannot reliably tell whether it appears in the else part.
1987 -- In particular, if it appears in generated code for a node
1988 -- that requires finalization, it may be attached to a list
1989 -- that has not been yet inserted into the code. For now,
1990 -- treat it as unknown.
1992 elsif not Comes_From_Source (N) then
1993 return;
1995 -- Otherwise we must be in ELSIF or ELSE part
1997 else
1998 Sens := False;
1999 end if;
2000 end;
2002 -- ELSIF part. Condition is known true within the referenced
2003 -- ELSIF, known False in any subsequent ELSIF or ELSE part, and
2004 -- unknown before the ELSE part or after the IF statement.
2006 elsif Nkind (CV) = N_Elsif_Part then
2008 -- if the Elsif_Part had condition_actions, the elsif has been
2009 -- rewritten as a nested if, and the original elsif_part is
2010 -- detached from the tree, so there is no way to obtain useful
2011 -- information on the current value of the variable.
2012 -- Can this be improved ???
2014 if No (Parent (CV)) then
2015 return;
2016 end if;
2018 Stm := Parent (CV);
2020 -- Before start of ELSIF part
2022 if Loc < Sloc (CV) then
2023 return;
2025 -- After end of IF statement
2027 elsif Loc >= Sloc (Stm) +
2028 Text_Ptr (UI_To_Int (End_Span (Stm)))
2029 then
2030 return;
2031 end if;
2033 -- Again we lack the SLOC of the ELSE, so we need to climb the
2034 -- tree to see if we are within the ELSIF part in question.
2036 declare
2037 N : Node_Id;
2039 begin
2040 N := Parent (Var);
2041 while Parent (N) /= Stm loop
2042 N := Parent (N);
2044 -- If we fall off the top of the tree, then that's odd, but
2045 -- perhaps it could occur in some error situation, and the
2046 -- safest response is simply to assume that the outcome of
2047 -- the condition is unknown. No point in bombing during an
2048 -- attempt to optimize things.
2050 if No (N) then
2051 return;
2052 end if;
2053 end loop;
2055 -- Now we have N pointing to a node whose parent is the IF
2056 -- statement in question, so see if is the ELSIF part we want.
2057 -- the THEN statements.
2059 if N = CV then
2060 Sens := True;
2062 -- Otherwise we must be in subsequent ELSIF or ELSE part
2064 else
2065 Sens := False;
2066 end if;
2067 end;
2069 -- Iteration scheme of while loop. The condition is known to be
2070 -- true within the body of the loop.
2072 elsif Nkind (CV) = N_Iteration_Scheme then
2073 declare
2074 Loop_Stmt : constant Node_Id := Parent (CV);
2076 begin
2077 -- Before start of body of loop
2079 if Loc < Sloc (Loop_Stmt) then
2080 return;
2082 -- After end of LOOP statement
2084 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2085 return;
2087 -- We are within the body of the loop
2089 else
2090 Sens := True;
2091 end if;
2092 end;
2094 -- All other cases of Current_Value settings
2096 else
2097 return;
2098 end if;
2100 -- If we fall through here, then we have a reportable condition, Sens
2101 -- is True if the condition is true and False if it needs inverting.
2103 Process_Current_Value_Condition (Condition (CV), Sens);
2104 end;
2105 end Get_Current_Value_Condition;
2107 ---------------------------------
2108 -- Has_Controlled_Coextensions --
2109 ---------------------------------
2111 function Has_Controlled_Coextensions (Typ : Entity_Id) return Boolean is
2112 D_Typ : Entity_Id;
2113 Discr : Entity_Id;
2115 begin
2116 -- Only consider record types
2118 if not Ekind_In (Typ, E_Record_Type, E_Record_Subtype) then
2119 return False;
2120 end if;
2122 if Has_Discriminants (Typ) then
2123 Discr := First_Discriminant (Typ);
2124 while Present (Discr) loop
2125 D_Typ := Etype (Discr);
2127 if Ekind (D_Typ) = E_Anonymous_Access_Type
2128 and then
2129 (Is_Controlled (Designated_Type (D_Typ))
2130 or else
2131 Is_Concurrent_Type (Designated_Type (D_Typ)))
2132 then
2133 return True;
2134 end if;
2136 Next_Discriminant (Discr);
2137 end loop;
2138 end if;
2140 return False;
2141 end Has_Controlled_Coextensions;
2143 ------------------------
2144 -- Has_Address_Clause --
2145 ------------------------
2147 -- Should this function check the private part in a package ???
2149 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
2150 Id : constant Entity_Id := Defining_Identifier (D);
2151 Decl : Node_Id;
2153 begin
2154 Decl := Next (D);
2155 while Present (Decl) loop
2156 if Nkind (Decl) = N_At_Clause
2157 and then Chars (Identifier (Decl)) = Chars (Id)
2158 then
2159 return True;
2161 elsif Nkind (Decl) = N_Attribute_Definition_Clause
2162 and then Chars (Decl) = Name_Address
2163 and then Chars (Name (Decl)) = Chars (Id)
2164 then
2165 return True;
2166 end if;
2168 Next (Decl);
2169 end loop;
2171 return False;
2172 end Has_Following_Address_Clause;
2174 --------------------
2175 -- Homonym_Number --
2176 --------------------
2178 function Homonym_Number (Subp : Entity_Id) return Nat is
2179 Count : Nat;
2180 Hom : Entity_Id;
2182 begin
2183 Count := 1;
2184 Hom := Homonym (Subp);
2185 while Present (Hom) loop
2186 if Scope (Hom) = Scope (Subp) then
2187 Count := Count + 1;
2188 end if;
2190 Hom := Homonym (Hom);
2191 end loop;
2193 return Count;
2194 end Homonym_Number;
2196 ------------------------------
2197 -- In_Unconditional_Context --
2198 ------------------------------
2200 function In_Unconditional_Context (Node : Node_Id) return Boolean is
2201 P : Node_Id;
2203 begin
2204 P := Node;
2205 while Present (P) loop
2206 case Nkind (P) is
2207 when N_Subprogram_Body =>
2208 return True;
2210 when N_If_Statement =>
2211 return False;
2213 when N_Loop_Statement =>
2214 return False;
2216 when N_Case_Statement =>
2217 return False;
2219 when others =>
2220 P := Parent (P);
2221 end case;
2222 end loop;
2224 return False;
2225 end In_Unconditional_Context;
2227 -------------------
2228 -- Insert_Action --
2229 -------------------
2231 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
2232 begin
2233 if Present (Ins_Action) then
2234 Insert_Actions (Assoc_Node, New_List (Ins_Action));
2235 end if;
2236 end Insert_Action;
2238 -- Version with check(s) suppressed
2240 procedure Insert_Action
2241 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
2243 begin
2244 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
2245 end Insert_Action;
2247 --------------------
2248 -- Insert_Actions --
2249 --------------------
2251 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
2252 N : Node_Id;
2253 P : Node_Id;
2255 Wrapped_Node : Node_Id := Empty;
2257 begin
2258 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
2259 return;
2260 end if;
2262 -- Ignore insert of actions from inside default expression (or other
2263 -- similar "spec expression") in the special spec-expression analyze
2264 -- mode. Any insertions at this point have no relevance, since we are
2265 -- only doing the analyze to freeze the types of any static expressions.
2266 -- See section "Handling of Default Expressions" in the spec of package
2267 -- Sem for further details.
2269 if In_Spec_Expression then
2270 return;
2271 end if;
2273 -- If the action derives from stuff inside a record, then the actions
2274 -- are attached to the current scope, to be inserted and analyzed on
2275 -- exit from the scope. The reason for this is that we may also
2276 -- be generating freeze actions at the same time, and they must
2277 -- eventually be elaborated in the correct order.
2279 if Is_Record_Type (Current_Scope)
2280 and then not Is_Frozen (Current_Scope)
2281 then
2282 if No (Scope_Stack.Table
2283 (Scope_Stack.Last).Pending_Freeze_Actions)
2284 then
2285 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
2286 Ins_Actions;
2287 else
2288 Append_List
2289 (Ins_Actions,
2290 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
2291 end if;
2293 return;
2294 end if;
2296 -- We now intend to climb up the tree to find the right point to
2297 -- insert the actions. We start at Assoc_Node, unless this node is
2298 -- a subexpression in which case we start with its parent. We do this
2299 -- for two reasons. First it speeds things up. Second, if Assoc_Node
2300 -- is itself one of the special nodes like N_And_Then, then we assume
2301 -- that an initial request to insert actions for such a node does not
2302 -- expect the actions to get deposited in the node for later handling
2303 -- when the node is expanded, since clearly the node is being dealt
2304 -- with by the caller. Note that in the subexpression case, N is
2305 -- always the child we came from.
2307 -- N_Raise_xxx_Error is an annoying special case, it is a statement
2308 -- if it has type Standard_Void_Type, and a subexpression otherwise.
2309 -- otherwise. Procedure attribute references are also statements.
2311 if Nkind (Assoc_Node) in N_Subexpr
2312 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
2313 or else Etype (Assoc_Node) /= Standard_Void_Type)
2314 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
2315 or else
2316 not Is_Procedure_Attribute_Name
2317 (Attribute_Name (Assoc_Node)))
2318 then
2319 P := Assoc_Node; -- ??? does not agree with above!
2320 N := Parent (Assoc_Node);
2322 -- Non-subexpression case. Note that N is initially Empty in this
2323 -- case (N is only guaranteed Non-Empty in the subexpr case).
2325 else
2326 P := Assoc_Node;
2327 N := Empty;
2328 end if;
2330 -- Capture root of the transient scope
2332 if Scope_Is_Transient then
2333 Wrapped_Node := Node_To_Be_Wrapped;
2334 end if;
2336 loop
2337 pragma Assert (Present (P));
2339 case Nkind (P) is
2341 -- Case of right operand of AND THEN or OR ELSE. Put the actions
2342 -- in the Actions field of the right operand. They will be moved
2343 -- out further when the AND THEN or OR ELSE operator is expanded.
2344 -- Nothing special needs to be done for the left operand since
2345 -- in that case the actions are executed unconditionally.
2347 when N_Short_Circuit =>
2348 if N = Right_Opnd (P) then
2350 -- We are now going to either append the actions to the
2351 -- actions field of the short-circuit operation. We will
2352 -- also analyze the actions now.
2354 -- This analysis is really too early, the proper thing would
2355 -- be to just park them there now, and only analyze them if
2356 -- we find we really need them, and to it at the proper
2357 -- final insertion point. However attempting to this proved
2358 -- tricky, so for now we just kill current values before and
2359 -- after the analyze call to make sure we avoid peculiar
2360 -- optimizations from this out of order insertion.
2362 Kill_Current_Values;
2364 if Present (Actions (P)) then
2365 Insert_List_After_And_Analyze
2366 (Last (Actions (P)), Ins_Actions);
2367 else
2368 Set_Actions (P, Ins_Actions);
2369 Analyze_List (Actions (P));
2370 end if;
2372 Kill_Current_Values;
2374 return;
2375 end if;
2377 -- Then or Else operand of conditional expression. Add actions to
2378 -- Then_Actions or Else_Actions field as appropriate. The actions
2379 -- will be moved further out when the conditional is expanded.
2381 when N_Conditional_Expression =>
2382 declare
2383 ThenX : constant Node_Id := Next (First (Expressions (P)));
2384 ElseX : constant Node_Id := Next (ThenX);
2386 begin
2387 -- Actions belong to the then expression, temporarily
2388 -- place them as Then_Actions of the conditional expr.
2389 -- They will be moved to the proper place later when
2390 -- the conditional expression is expanded.
2392 if N = ThenX then
2393 if Present (Then_Actions (P)) then
2394 Insert_List_After_And_Analyze
2395 (Last (Then_Actions (P)), Ins_Actions);
2396 else
2397 Set_Then_Actions (P, Ins_Actions);
2398 Analyze_List (Then_Actions (P));
2399 end if;
2401 return;
2403 -- Actions belong to the else expression, temporarily
2404 -- place them as Else_Actions of the conditional expr.
2405 -- They will be moved to the proper place later when
2406 -- the conditional expression is expanded.
2408 elsif N = ElseX then
2409 if Present (Else_Actions (P)) then
2410 Insert_List_After_And_Analyze
2411 (Last (Else_Actions (P)), Ins_Actions);
2412 else
2413 Set_Else_Actions (P, Ins_Actions);
2414 Analyze_List (Else_Actions (P));
2415 end if;
2417 return;
2419 -- Actions belong to the condition. In this case they are
2420 -- unconditionally executed, and so we can continue the
2421 -- search for the proper insert point.
2423 else
2424 null;
2425 end if;
2426 end;
2428 -- Alternative of case expression, we place the action in
2429 -- the Actions field of the case expression alternative, this
2430 -- will be handled when the case expression is expanded.
2432 when N_Case_Expression_Alternative =>
2433 if Present (Actions (P)) then
2434 Insert_List_After_And_Analyze
2435 (Last (Actions (P)), Ins_Actions);
2436 else
2437 Set_Actions (P, Ins_Actions);
2438 Analyze_List (Then_Actions (P));
2439 end if;
2441 return;
2443 -- Case of appearing within an Expressions_With_Actions node. We
2444 -- prepend the actions to the list of actions already there.
2446 when N_Expression_With_Actions =>
2447 Prepend_List (Ins_Actions, Actions (P));
2448 return;
2450 -- Case of appearing in the condition of a while expression or
2451 -- elsif. We insert the actions into the Condition_Actions field.
2452 -- They will be moved further out when the while loop or elsif
2453 -- is analyzed.
2455 when N_Iteration_Scheme |
2456 N_Elsif_Part
2458 if N = Condition (P) then
2459 if Present (Condition_Actions (P)) then
2460 Insert_List_After_And_Analyze
2461 (Last (Condition_Actions (P)), Ins_Actions);
2462 else
2463 Set_Condition_Actions (P, Ins_Actions);
2465 -- Set the parent of the insert actions explicitly.
2466 -- This is not a syntactic field, but we need the
2467 -- parent field set, in particular so that freeze
2468 -- can understand that it is dealing with condition
2469 -- actions, and properly insert the freezing actions.
2471 Set_Parent (Ins_Actions, P);
2472 Analyze_List (Condition_Actions (P));
2473 end if;
2475 return;
2476 end if;
2478 -- Statements, declarations, pragmas, representation clauses
2480 when
2481 -- Statements
2483 N_Procedure_Call_Statement |
2484 N_Statement_Other_Than_Procedure_Call |
2486 -- Pragmas
2488 N_Pragma |
2490 -- Representation_Clause
2492 N_At_Clause |
2493 N_Attribute_Definition_Clause |
2494 N_Enumeration_Representation_Clause |
2495 N_Record_Representation_Clause |
2497 -- Declarations
2499 N_Abstract_Subprogram_Declaration |
2500 N_Entry_Body |
2501 N_Exception_Declaration |
2502 N_Exception_Renaming_Declaration |
2503 N_Formal_Abstract_Subprogram_Declaration |
2504 N_Formal_Concrete_Subprogram_Declaration |
2505 N_Formal_Object_Declaration |
2506 N_Formal_Type_Declaration |
2507 N_Full_Type_Declaration |
2508 N_Function_Instantiation |
2509 N_Generic_Function_Renaming_Declaration |
2510 N_Generic_Package_Declaration |
2511 N_Generic_Package_Renaming_Declaration |
2512 N_Generic_Procedure_Renaming_Declaration |
2513 N_Generic_Subprogram_Declaration |
2514 N_Implicit_Label_Declaration |
2515 N_Incomplete_Type_Declaration |
2516 N_Number_Declaration |
2517 N_Object_Declaration |
2518 N_Object_Renaming_Declaration |
2519 N_Package_Body |
2520 N_Package_Body_Stub |
2521 N_Package_Declaration |
2522 N_Package_Instantiation |
2523 N_Package_Renaming_Declaration |
2524 N_Private_Extension_Declaration |
2525 N_Private_Type_Declaration |
2526 N_Procedure_Instantiation |
2527 N_Protected_Body |
2528 N_Protected_Body_Stub |
2529 N_Protected_Type_Declaration |
2530 N_Single_Task_Declaration |
2531 N_Subprogram_Body |
2532 N_Subprogram_Body_Stub |
2533 N_Subprogram_Declaration |
2534 N_Subprogram_Renaming_Declaration |
2535 N_Subtype_Declaration |
2536 N_Task_Body |
2537 N_Task_Body_Stub |
2538 N_Task_Type_Declaration |
2540 -- Freeze entity behaves like a declaration or statement
2542 N_Freeze_Entity
2544 -- Do not insert here if the item is not a list member (this
2545 -- happens for example with a triggering statement, and the
2546 -- proper approach is to insert before the entire select).
2548 if not Is_List_Member (P) then
2549 null;
2551 -- Do not insert if parent of P is an N_Component_Association
2552 -- node (i.e. we are in the context of an N_Aggregate or
2553 -- N_Extension_Aggregate node. In this case we want to insert
2554 -- before the entire aggregate.
2556 elsif Nkind (Parent (P)) = N_Component_Association then
2557 null;
2559 -- Do not insert if the parent of P is either an N_Variant
2560 -- node or an N_Record_Definition node, meaning in either
2561 -- case that P is a member of a component list, and that
2562 -- therefore the actions should be inserted outside the
2563 -- complete record declaration.
2565 elsif Nkind (Parent (P)) = N_Variant
2566 or else Nkind (Parent (P)) = N_Record_Definition
2567 then
2568 null;
2570 -- Do not insert freeze nodes within the loop generated for
2571 -- an aggregate, because they may be elaborated too late for
2572 -- subsequent use in the back end: within a package spec the
2573 -- loop is part of the elaboration procedure and is only
2574 -- elaborated during the second pass.
2575 -- If the loop comes from source, or the entity is local to
2576 -- the loop itself it must remain within.
2578 elsif Nkind (Parent (P)) = N_Loop_Statement
2579 and then not Comes_From_Source (Parent (P))
2580 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
2581 and then
2582 Scope (Entity (First (Ins_Actions))) /= Current_Scope
2583 then
2584 null;
2586 -- Otherwise we can go ahead and do the insertion
2588 elsif P = Wrapped_Node then
2589 Store_Before_Actions_In_Scope (Ins_Actions);
2590 return;
2592 else
2593 Insert_List_Before_And_Analyze (P, Ins_Actions);
2594 return;
2595 end if;
2597 -- A special case, N_Raise_xxx_Error can act either as a
2598 -- statement or a subexpression. We tell the difference
2599 -- by looking at the Etype. It is set to Standard_Void_Type
2600 -- in the statement case.
2602 when
2603 N_Raise_xxx_Error =>
2604 if Etype (P) = Standard_Void_Type then
2605 if P = Wrapped_Node then
2606 Store_Before_Actions_In_Scope (Ins_Actions);
2607 else
2608 Insert_List_Before_And_Analyze (P, Ins_Actions);
2609 end if;
2611 return;
2613 -- In the subexpression case, keep climbing
2615 else
2616 null;
2617 end if;
2619 -- If a component association appears within a loop created for
2620 -- an array aggregate, attach the actions to the association so
2621 -- they can be subsequently inserted within the loop. For other
2622 -- component associations insert outside of the aggregate. For
2623 -- an association that will generate a loop, its Loop_Actions
2624 -- attribute is already initialized (see exp_aggr.adb).
2626 -- The list of loop_actions can in turn generate additional ones,
2627 -- that are inserted before the associated node. If the associated
2628 -- node is outside the aggregate, the new actions are collected
2629 -- at the end of the loop actions, to respect the order in which
2630 -- they are to be elaborated.
2632 when
2633 N_Component_Association =>
2634 if Nkind (Parent (P)) = N_Aggregate
2635 and then Present (Loop_Actions (P))
2636 then
2637 if Is_Empty_List (Loop_Actions (P)) then
2638 Set_Loop_Actions (P, Ins_Actions);
2639 Analyze_List (Ins_Actions);
2641 else
2642 declare
2643 Decl : Node_Id;
2645 begin
2646 -- Check whether these actions were generated
2647 -- by a declaration that is part of the loop_
2648 -- actions for the component_association.
2650 Decl := Assoc_Node;
2651 while Present (Decl) loop
2652 exit when Parent (Decl) = P
2653 and then Is_List_Member (Decl)
2654 and then
2655 List_Containing (Decl) = Loop_Actions (P);
2656 Decl := Parent (Decl);
2657 end loop;
2659 if Present (Decl) then
2660 Insert_List_Before_And_Analyze
2661 (Decl, Ins_Actions);
2662 else
2663 Insert_List_After_And_Analyze
2664 (Last (Loop_Actions (P)), Ins_Actions);
2665 end if;
2666 end;
2667 end if;
2669 return;
2671 else
2672 null;
2673 end if;
2675 -- Another special case, an attribute denoting a procedure call
2677 when
2678 N_Attribute_Reference =>
2679 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
2680 if P = Wrapped_Node then
2681 Store_Before_Actions_In_Scope (Ins_Actions);
2682 else
2683 Insert_List_Before_And_Analyze (P, Ins_Actions);
2684 end if;
2686 return;
2688 -- In the subexpression case, keep climbing
2690 else
2691 null;
2692 end if;
2694 -- For all other node types, keep climbing tree
2696 when
2697 N_Abortable_Part |
2698 N_Accept_Alternative |
2699 N_Access_Definition |
2700 N_Access_Function_Definition |
2701 N_Access_Procedure_Definition |
2702 N_Access_To_Object_Definition |
2703 N_Aggregate |
2704 N_Allocator |
2705 N_Case_Expression |
2706 N_Case_Statement_Alternative |
2707 N_Character_Literal |
2708 N_Compilation_Unit |
2709 N_Compilation_Unit_Aux |
2710 N_Component_Clause |
2711 N_Component_Declaration |
2712 N_Component_Definition |
2713 N_Component_List |
2714 N_Constrained_Array_Definition |
2715 N_Decimal_Fixed_Point_Definition |
2716 N_Defining_Character_Literal |
2717 N_Defining_Identifier |
2718 N_Defining_Operator_Symbol |
2719 N_Defining_Program_Unit_Name |
2720 N_Delay_Alternative |
2721 N_Delta_Constraint |
2722 N_Derived_Type_Definition |
2723 N_Designator |
2724 N_Digits_Constraint |
2725 N_Discriminant_Association |
2726 N_Discriminant_Specification |
2727 N_Empty |
2728 N_Entry_Body_Formal_Part |
2729 N_Entry_Call_Alternative |
2730 N_Entry_Declaration |
2731 N_Entry_Index_Specification |
2732 N_Enumeration_Type_Definition |
2733 N_Error |
2734 N_Exception_Handler |
2735 N_Expanded_Name |
2736 N_Explicit_Dereference |
2737 N_Extension_Aggregate |
2738 N_Floating_Point_Definition |
2739 N_Formal_Decimal_Fixed_Point_Definition |
2740 N_Formal_Derived_Type_Definition |
2741 N_Formal_Discrete_Type_Definition |
2742 N_Formal_Floating_Point_Definition |
2743 N_Formal_Modular_Type_Definition |
2744 N_Formal_Ordinary_Fixed_Point_Definition |
2745 N_Formal_Package_Declaration |
2746 N_Formal_Private_Type_Definition |
2747 N_Formal_Signed_Integer_Type_Definition |
2748 N_Function_Call |
2749 N_Function_Specification |
2750 N_Generic_Association |
2751 N_Handled_Sequence_Of_Statements |
2752 N_Identifier |
2753 N_In |
2754 N_Index_Or_Discriminant_Constraint |
2755 N_Indexed_Component |
2756 N_Integer_Literal |
2757 N_Itype_Reference |
2758 N_Label |
2759 N_Loop_Parameter_Specification |
2760 N_Mod_Clause |
2761 N_Modular_Type_Definition |
2762 N_Not_In |
2763 N_Null |
2764 N_Op_Abs |
2765 N_Op_Add |
2766 N_Op_And |
2767 N_Op_Concat |
2768 N_Op_Divide |
2769 N_Op_Eq |
2770 N_Op_Expon |
2771 N_Op_Ge |
2772 N_Op_Gt |
2773 N_Op_Le |
2774 N_Op_Lt |
2775 N_Op_Minus |
2776 N_Op_Mod |
2777 N_Op_Multiply |
2778 N_Op_Ne |
2779 N_Op_Not |
2780 N_Op_Or |
2781 N_Op_Plus |
2782 N_Op_Rem |
2783 N_Op_Rotate_Left |
2784 N_Op_Rotate_Right |
2785 N_Op_Shift_Left |
2786 N_Op_Shift_Right |
2787 N_Op_Shift_Right_Arithmetic |
2788 N_Op_Subtract |
2789 N_Op_Xor |
2790 N_Operator_Symbol |
2791 N_Ordinary_Fixed_Point_Definition |
2792 N_Others_Choice |
2793 N_Package_Specification |
2794 N_Parameter_Association |
2795 N_Parameter_Specification |
2796 N_Pop_Constraint_Error_Label |
2797 N_Pop_Program_Error_Label |
2798 N_Pop_Storage_Error_Label |
2799 N_Pragma_Argument_Association |
2800 N_Procedure_Specification |
2801 N_Protected_Definition |
2802 N_Push_Constraint_Error_Label |
2803 N_Push_Program_Error_Label |
2804 N_Push_Storage_Error_Label |
2805 N_Qualified_Expression |
2806 N_Range |
2807 N_Range_Constraint |
2808 N_Real_Literal |
2809 N_Real_Range_Specification |
2810 N_Record_Definition |
2811 N_Reference |
2812 N_SCIL_Dispatch_Table_Tag_Init |
2813 N_SCIL_Dispatching_Call |
2814 N_SCIL_Membership_Test |
2815 N_Selected_Component |
2816 N_Signed_Integer_Type_Definition |
2817 N_Single_Protected_Declaration |
2818 N_Slice |
2819 N_String_Literal |
2820 N_Subprogram_Info |
2821 N_Subtype_Indication |
2822 N_Subunit |
2823 N_Task_Definition |
2824 N_Terminate_Alternative |
2825 N_Triggering_Alternative |
2826 N_Type_Conversion |
2827 N_Unchecked_Expression |
2828 N_Unchecked_Type_Conversion |
2829 N_Unconstrained_Array_Definition |
2830 N_Unused_At_End |
2831 N_Unused_At_Start |
2832 N_Use_Package_Clause |
2833 N_Use_Type_Clause |
2834 N_Variant |
2835 N_Variant_Part |
2836 N_Validate_Unchecked_Conversion |
2837 N_With_Clause
2839 null;
2841 end case;
2843 -- Make sure that inserted actions stay in the transient scope
2845 if P = Wrapped_Node then
2846 Store_Before_Actions_In_Scope (Ins_Actions);
2847 return;
2848 end if;
2850 -- If we fall through above tests, keep climbing tree
2852 N := P;
2854 if Nkind (Parent (N)) = N_Subunit then
2856 -- This is the proper body corresponding to a stub. Insertion
2857 -- must be done at the point of the stub, which is in the decla-
2858 -- rative part of the parent unit.
2860 P := Corresponding_Stub (Parent (N));
2862 else
2863 P := Parent (N);
2864 end if;
2865 end loop;
2866 end Insert_Actions;
2868 -- Version with check(s) suppressed
2870 procedure Insert_Actions
2871 (Assoc_Node : Node_Id;
2872 Ins_Actions : List_Id;
2873 Suppress : Check_Id)
2875 begin
2876 if Suppress = All_Checks then
2877 declare
2878 Svg : constant Suppress_Array := Scope_Suppress;
2879 begin
2880 Scope_Suppress := (others => True);
2881 Insert_Actions (Assoc_Node, Ins_Actions);
2882 Scope_Suppress := Svg;
2883 end;
2885 else
2886 declare
2887 Svg : constant Boolean := Scope_Suppress (Suppress);
2888 begin
2889 Scope_Suppress (Suppress) := True;
2890 Insert_Actions (Assoc_Node, Ins_Actions);
2891 Scope_Suppress (Suppress) := Svg;
2892 end;
2893 end if;
2894 end Insert_Actions;
2896 --------------------------
2897 -- Insert_Actions_After --
2898 --------------------------
2900 procedure Insert_Actions_After
2901 (Assoc_Node : Node_Id;
2902 Ins_Actions : List_Id)
2904 begin
2905 if Scope_Is_Transient
2906 and then Assoc_Node = Node_To_Be_Wrapped
2907 then
2908 Store_After_Actions_In_Scope (Ins_Actions);
2909 else
2910 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
2911 end if;
2912 end Insert_Actions_After;
2914 ---------------------------------
2915 -- Insert_Library_Level_Action --
2916 ---------------------------------
2918 procedure Insert_Library_Level_Action (N : Node_Id) is
2919 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2921 begin
2922 Push_Scope (Cunit_Entity (Main_Unit));
2923 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
2925 if No (Actions (Aux)) then
2926 Set_Actions (Aux, New_List (N));
2927 else
2928 Append (N, Actions (Aux));
2929 end if;
2931 Analyze (N);
2932 Pop_Scope;
2933 end Insert_Library_Level_Action;
2935 ----------------------------------
2936 -- Insert_Library_Level_Actions --
2937 ----------------------------------
2939 procedure Insert_Library_Level_Actions (L : List_Id) is
2940 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2942 begin
2943 if Is_Non_Empty_List (L) then
2944 Push_Scope (Cunit_Entity (Main_Unit));
2945 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
2947 if No (Actions (Aux)) then
2948 Set_Actions (Aux, L);
2949 Analyze_List (L);
2950 else
2951 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
2952 end if;
2954 Pop_Scope;
2955 end if;
2956 end Insert_Library_Level_Actions;
2958 ----------------------
2959 -- Inside_Init_Proc --
2960 ----------------------
2962 function Inside_Init_Proc return Boolean is
2963 S : Entity_Id;
2965 begin
2966 S := Current_Scope;
2967 while Present (S)
2968 and then S /= Standard_Standard
2969 loop
2970 if Is_Init_Proc (S) then
2971 return True;
2972 else
2973 S := Scope (S);
2974 end if;
2975 end loop;
2977 return False;
2978 end Inside_Init_Proc;
2980 ----------------------------
2981 -- Is_All_Null_Statements --
2982 ----------------------------
2984 function Is_All_Null_Statements (L : List_Id) return Boolean is
2985 Stm : Node_Id;
2987 begin
2988 Stm := First (L);
2989 while Present (Stm) loop
2990 if Nkind (Stm) /= N_Null_Statement then
2991 return False;
2992 end if;
2994 Next (Stm);
2995 end loop;
2997 return True;
2998 end Is_All_Null_Statements;
3000 ---------------------------------
3001 -- Is_Fully_Repped_Tagged_Type --
3002 ---------------------------------
3004 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
3005 U : constant Entity_Id := Underlying_Type (T);
3006 Comp : Entity_Id;
3008 begin
3009 if No (U) or else not Is_Tagged_Type (U) then
3010 return False;
3011 elsif Has_Discriminants (U) then
3012 return False;
3013 elsif not Has_Specified_Layout (U) then
3014 return False;
3015 end if;
3017 -- Here we have a tagged type, see if it has any unlayed out fields
3018 -- other than a possible tag and parent fields. If so, we return False.
3020 Comp := First_Component (U);
3021 while Present (Comp) loop
3022 if not Is_Tag (Comp)
3023 and then Chars (Comp) /= Name_uParent
3024 and then No (Component_Clause (Comp))
3025 then
3026 return False;
3027 else
3028 Next_Component (Comp);
3029 end if;
3030 end loop;
3032 -- All components are layed out
3034 return True;
3035 end Is_Fully_Repped_Tagged_Type;
3037 ----------------------------------
3038 -- Is_Library_Level_Tagged_Type --
3039 ----------------------------------
3041 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
3042 begin
3043 return Is_Tagged_Type (Typ)
3044 and then Is_Library_Level_Entity (Typ);
3045 end Is_Library_Level_Tagged_Type;
3047 ----------------------------------
3048 -- Is_Possibly_Unaligned_Object --
3049 ----------------------------------
3051 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
3052 T : constant Entity_Id := Etype (N);
3054 begin
3055 -- If renamed object, apply test to underlying object
3057 if Is_Entity_Name (N)
3058 and then Is_Object (Entity (N))
3059 and then Present (Renamed_Object (Entity (N)))
3060 then
3061 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
3062 end if;
3064 -- Tagged and controlled types and aliased types are always aligned,
3065 -- as are concurrent types.
3067 if Is_Aliased (T)
3068 or else Has_Controlled_Component (T)
3069 or else Is_Concurrent_Type (T)
3070 or else Is_Tagged_Type (T)
3071 or else Is_Controlled (T)
3072 then
3073 return False;
3074 end if;
3076 -- If this is an element of a packed array, may be unaligned
3078 if Is_Ref_To_Bit_Packed_Array (N) then
3079 return True;
3080 end if;
3082 -- Case of component reference
3084 if Nkind (N) = N_Selected_Component then
3085 declare
3086 P : constant Node_Id := Prefix (N);
3087 C : constant Entity_Id := Entity (Selector_Name (N));
3088 M : Nat;
3089 S : Nat;
3091 begin
3092 -- If component reference is for an array with non-static bounds,
3093 -- then it is always aligned: we can only process unaligned
3094 -- arrays with static bounds (more accurately bounds known at
3095 -- compile time).
3097 if Is_Array_Type (T)
3098 and then not Compile_Time_Known_Bounds (T)
3099 then
3100 return False;
3101 end if;
3103 -- If component is aliased, it is definitely properly aligned
3105 if Is_Aliased (C) then
3106 return False;
3107 end if;
3109 -- If component is for a type implemented as a scalar, and the
3110 -- record is packed, and the component is other than the first
3111 -- component of the record, then the component may be unaligned.
3113 if Is_Packed (Etype (P))
3114 and then Represented_As_Scalar (Etype (C))
3115 and then First_Entity (Scope (C)) /= C
3116 then
3117 return True;
3118 end if;
3120 -- Compute maximum possible alignment for T
3122 -- If alignment is known, then that settles things
3124 if Known_Alignment (T) then
3125 M := UI_To_Int (Alignment (T));
3127 -- If alignment is not known, tentatively set max alignment
3129 else
3130 M := Ttypes.Maximum_Alignment;
3132 -- We can reduce this if the Esize is known since the default
3133 -- alignment will never be more than the smallest power of 2
3134 -- that does not exceed this Esize value.
3136 if Known_Esize (T) then
3137 S := UI_To_Int (Esize (T));
3139 while (M / 2) >= S loop
3140 M := M / 2;
3141 end loop;
3142 end if;
3143 end if;
3145 -- The following code is historical, it used to be present but it
3146 -- is too cautious, because the front-end does not know the proper
3147 -- default alignments for the target. Also, if the alignment is
3148 -- not known, the front end can't know in any case! If a copy is
3149 -- needed, the back-end will take care of it. This whole section
3150 -- including this comment can be removed later ???
3152 -- If the component reference is for a record that has a specified
3153 -- alignment, and we either know it is too small, or cannot tell,
3154 -- then the component may be unaligned.
3156 -- if Known_Alignment (Etype (P))
3157 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
3158 -- and then M > Alignment (Etype (P))
3159 -- then
3160 -- return True;
3161 -- end if;
3163 -- Case of component clause present which may specify an
3164 -- unaligned position.
3166 if Present (Component_Clause (C)) then
3168 -- Otherwise we can do a test to make sure that the actual
3169 -- start position in the record, and the length, are both
3170 -- consistent with the required alignment. If not, we know
3171 -- that we are unaligned.
3173 declare
3174 Align_In_Bits : constant Nat := M * System_Storage_Unit;
3175 begin
3176 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
3177 or else Esize (C) mod Align_In_Bits /= 0
3178 then
3179 return True;
3180 end if;
3181 end;
3182 end if;
3184 -- Otherwise, for a component reference, test prefix
3186 return Is_Possibly_Unaligned_Object (P);
3187 end;
3189 -- If not a component reference, must be aligned
3191 else
3192 return False;
3193 end if;
3194 end Is_Possibly_Unaligned_Object;
3196 ---------------------------------
3197 -- Is_Possibly_Unaligned_Slice --
3198 ---------------------------------
3200 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
3201 begin
3202 -- Go to renamed object
3204 if Is_Entity_Name (N)
3205 and then Is_Object (Entity (N))
3206 and then Present (Renamed_Object (Entity (N)))
3207 then
3208 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
3209 end if;
3211 -- The reference must be a slice
3213 if Nkind (N) /= N_Slice then
3214 return False;
3215 end if;
3217 -- Always assume the worst for a nested record component with a
3218 -- component clause, which gigi/gcc does not appear to handle well.
3219 -- It is not clear why this special test is needed at all ???
3221 if Nkind (Prefix (N)) = N_Selected_Component
3222 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
3223 and then
3224 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
3225 then
3226 return True;
3227 end if;
3229 -- We only need to worry if the target has strict alignment
3231 if not Target_Strict_Alignment then
3232 return False;
3233 end if;
3235 -- If it is a slice, then look at the array type being sliced
3237 declare
3238 Sarr : constant Node_Id := Prefix (N);
3239 -- Prefix of the slice, i.e. the array being sliced
3241 Styp : constant Entity_Id := Etype (Prefix (N));
3242 -- Type of the array being sliced
3244 Pref : Node_Id;
3245 Ptyp : Entity_Id;
3247 begin
3248 -- The problems arise if the array object that is being sliced
3249 -- is a component of a record or array, and we cannot guarantee
3250 -- the alignment of the array within its containing object.
3252 -- To investigate this, we look at successive prefixes to see
3253 -- if we have a worrisome indexed or selected component.
3255 Pref := Sarr;
3256 loop
3257 -- Case of array is part of an indexed component reference
3259 if Nkind (Pref) = N_Indexed_Component then
3260 Ptyp := Etype (Prefix (Pref));
3262 -- The only problematic case is when the array is packed,
3263 -- in which case we really know nothing about the alignment
3264 -- of individual components.
3266 if Is_Bit_Packed_Array (Ptyp) then
3267 return True;
3268 end if;
3270 -- Case of array is part of a selected component reference
3272 elsif Nkind (Pref) = N_Selected_Component then
3273 Ptyp := Etype (Prefix (Pref));
3275 -- We are definitely in trouble if the record in question
3276 -- has an alignment, and either we know this alignment is
3277 -- inconsistent with the alignment of the slice, or we
3278 -- don't know what the alignment of the slice should be.
3280 if Known_Alignment (Ptyp)
3281 and then (Unknown_Alignment (Styp)
3282 or else Alignment (Styp) > Alignment (Ptyp))
3283 then
3284 return True;
3285 end if;
3287 -- We are in potential trouble if the record type is packed.
3288 -- We could special case when we know that the array is the
3289 -- first component, but that's not such a simple case ???
3291 if Is_Packed (Ptyp) then
3292 return True;
3293 end if;
3295 -- We are in trouble if there is a component clause, and
3296 -- either we do not know the alignment of the slice, or
3297 -- the alignment of the slice is inconsistent with the
3298 -- bit position specified by the component clause.
3300 declare
3301 Field : constant Entity_Id := Entity (Selector_Name (Pref));
3302 begin
3303 if Present (Component_Clause (Field))
3304 and then
3305 (Unknown_Alignment (Styp)
3306 or else
3307 (Component_Bit_Offset (Field) mod
3308 (System_Storage_Unit * Alignment (Styp))) /= 0)
3309 then
3310 return True;
3311 end if;
3312 end;
3314 -- For cases other than selected or indexed components we
3315 -- know we are OK, since no issues arise over alignment.
3317 else
3318 return False;
3319 end if;
3321 -- We processed an indexed component or selected component
3322 -- reference that looked safe, so keep checking prefixes.
3324 Pref := Prefix (Pref);
3325 end loop;
3326 end;
3327 end Is_Possibly_Unaligned_Slice;
3329 --------------------------------
3330 -- Is_Ref_To_Bit_Packed_Array --
3331 --------------------------------
3333 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
3334 Result : Boolean;
3335 Expr : Node_Id;
3337 begin
3338 if Is_Entity_Name (N)
3339 and then Is_Object (Entity (N))
3340 and then Present (Renamed_Object (Entity (N)))
3341 then
3342 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
3343 end if;
3345 if Nkind (N) = N_Indexed_Component
3346 or else
3347 Nkind (N) = N_Selected_Component
3348 then
3349 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
3350 Result := True;
3351 else
3352 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
3353 end if;
3355 if Result and then Nkind (N) = N_Indexed_Component then
3356 Expr := First (Expressions (N));
3357 while Present (Expr) loop
3358 Force_Evaluation (Expr);
3359 Next (Expr);
3360 end loop;
3361 end if;
3363 return Result;
3365 else
3366 return False;
3367 end if;
3368 end Is_Ref_To_Bit_Packed_Array;
3370 --------------------------------
3371 -- Is_Ref_To_Bit_Packed_Slice --
3372 --------------------------------
3374 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
3375 begin
3376 if Nkind (N) = N_Type_Conversion then
3377 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
3379 elsif Is_Entity_Name (N)
3380 and then Is_Object (Entity (N))
3381 and then Present (Renamed_Object (Entity (N)))
3382 then
3383 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
3385 elsif Nkind (N) = N_Slice
3386 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
3387 then
3388 return True;
3390 elsif Nkind (N) = N_Indexed_Component
3391 or else
3392 Nkind (N) = N_Selected_Component
3393 then
3394 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
3396 else
3397 return False;
3398 end if;
3399 end Is_Ref_To_Bit_Packed_Slice;
3401 -----------------------
3402 -- Is_Renamed_Object --
3403 -----------------------
3405 function Is_Renamed_Object (N : Node_Id) return Boolean is
3406 Pnod : constant Node_Id := Parent (N);
3407 Kind : constant Node_Kind := Nkind (Pnod);
3408 begin
3409 if Kind = N_Object_Renaming_Declaration then
3410 return True;
3411 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
3412 return Is_Renamed_Object (Pnod);
3413 else
3414 return False;
3415 end if;
3416 end Is_Renamed_Object;
3418 ----------------------------
3419 -- Is_Untagged_Derivation --
3420 ----------------------------
3422 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
3423 begin
3424 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
3425 or else
3426 (Is_Private_Type (T) and then Present (Full_View (T))
3427 and then not Is_Tagged_Type (Full_View (T))
3428 and then Is_Derived_Type (Full_View (T))
3429 and then Etype (Full_View (T)) /= T);
3430 end Is_Untagged_Derivation;
3432 ---------------------------
3433 -- Is_Volatile_Reference --
3434 ---------------------------
3436 function Is_Volatile_Reference (N : Node_Id) return Boolean is
3437 begin
3438 if Nkind (N) in N_Has_Etype
3439 and then Present (Etype (N))
3440 and then Treat_As_Volatile (Etype (N))
3441 then
3442 return True;
3444 elsif Is_Entity_Name (N) then
3445 return Treat_As_Volatile (Entity (N));
3447 elsif Nkind (N) = N_Slice then
3448 return Is_Volatile_Reference (Prefix (N));
3450 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
3451 if (Is_Entity_Name (Prefix (N))
3452 and then Has_Volatile_Components (Entity (Prefix (N))))
3453 or else (Present (Etype (Prefix (N)))
3454 and then Has_Volatile_Components (Etype (Prefix (N))))
3455 then
3456 return True;
3457 else
3458 return Is_Volatile_Reference (Prefix (N));
3459 end if;
3461 else
3462 return False;
3463 end if;
3464 end Is_Volatile_Reference;
3466 --------------------
3467 -- Kill_Dead_Code --
3468 --------------------
3470 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
3471 W : Boolean := Warn;
3472 -- Set False if warnings suppressed
3474 begin
3475 if Present (N) then
3476 Remove_Warning_Messages (N);
3478 -- Generate warning if appropriate
3480 if W then
3482 -- We suppress the warning if this code is under control of an
3483 -- if statement, whose condition is a simple identifier, and
3484 -- either we are in an instance, or warnings off is set for this
3485 -- identifier. The reason for killing it in the instance case is
3486 -- that it is common and reasonable for code to be deleted in
3487 -- instances for various reasons.
3489 if Nkind (Parent (N)) = N_If_Statement then
3490 declare
3491 C : constant Node_Id := Condition (Parent (N));
3492 begin
3493 if Nkind (C) = N_Identifier
3494 and then
3495 (In_Instance
3496 or else (Present (Entity (C))
3497 and then Has_Warnings_Off (Entity (C))))
3498 then
3499 W := False;
3500 end if;
3501 end;
3502 end if;
3504 -- Generate warning if not suppressed
3506 if W then
3507 Error_Msg_F
3508 ("?this code can never be executed and has been deleted!", N);
3509 end if;
3510 end if;
3512 -- Recurse into block statements and bodies to process declarations
3513 -- and statements.
3515 if Nkind (N) = N_Block_Statement
3516 or else Nkind (N) = N_Subprogram_Body
3517 or else Nkind (N) = N_Package_Body
3518 then
3519 Kill_Dead_Code (Declarations (N), False);
3520 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
3522 if Nkind (N) = N_Subprogram_Body then
3523 Set_Is_Eliminated (Defining_Entity (N));
3524 end if;
3526 elsif Nkind (N) = N_Package_Declaration then
3527 Kill_Dead_Code (Visible_Declarations (Specification (N)));
3528 Kill_Dead_Code (Private_Declarations (Specification (N)));
3530 -- ??? After this point, Delete_Tree has been called on all
3531 -- declarations in Specification (N), so references to
3532 -- entities therein look suspicious.
3534 declare
3535 E : Entity_Id := First_Entity (Defining_Entity (N));
3536 begin
3537 while Present (E) loop
3538 if Ekind (E) = E_Operator then
3539 Set_Is_Eliminated (E);
3540 end if;
3542 Next_Entity (E);
3543 end loop;
3544 end;
3546 -- Recurse into composite statement to kill individual statements,
3547 -- in particular instantiations.
3549 elsif Nkind (N) = N_If_Statement then
3550 Kill_Dead_Code (Then_Statements (N));
3551 Kill_Dead_Code (Elsif_Parts (N));
3552 Kill_Dead_Code (Else_Statements (N));
3554 elsif Nkind (N) = N_Loop_Statement then
3555 Kill_Dead_Code (Statements (N));
3557 elsif Nkind (N) = N_Case_Statement then
3558 declare
3559 Alt : Node_Id;
3560 begin
3561 Alt := First (Alternatives (N));
3562 while Present (Alt) loop
3563 Kill_Dead_Code (Statements (Alt));
3564 Next (Alt);
3565 end loop;
3566 end;
3568 elsif Nkind (N) = N_Case_Statement_Alternative then
3569 Kill_Dead_Code (Statements (N));
3571 -- Deal with dead instances caused by deleting instantiations
3573 elsif Nkind (N) in N_Generic_Instantiation then
3574 Remove_Dead_Instance (N);
3575 end if;
3576 end if;
3577 end Kill_Dead_Code;
3579 -- Case where argument is a list of nodes to be killed
3581 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
3582 N : Node_Id;
3583 W : Boolean;
3584 begin
3585 W := Warn;
3586 if Is_Non_Empty_List (L) then
3587 N := First (L);
3588 while Present (N) loop
3589 Kill_Dead_Code (N, W);
3590 W := False;
3591 Next (N);
3592 end loop;
3593 end if;
3594 end Kill_Dead_Code;
3596 ------------------------
3597 -- Known_Non_Negative --
3598 ------------------------
3600 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
3601 begin
3602 if Is_OK_Static_Expression (Opnd)
3603 and then Expr_Value (Opnd) >= 0
3604 then
3605 return True;
3607 else
3608 declare
3609 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
3611 begin
3612 return
3613 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
3614 end;
3615 end if;
3616 end Known_Non_Negative;
3618 --------------------
3619 -- Known_Non_Null --
3620 --------------------
3622 function Known_Non_Null (N : Node_Id) return Boolean is
3623 begin
3624 -- Checks for case where N is an entity reference
3626 if Is_Entity_Name (N) and then Present (Entity (N)) then
3627 declare
3628 E : constant Entity_Id := Entity (N);
3629 Op : Node_Kind;
3630 Val : Node_Id;
3632 begin
3633 -- First check if we are in decisive conditional
3635 Get_Current_Value_Condition (N, Op, Val);
3637 if Known_Null (Val) then
3638 if Op = N_Op_Eq then
3639 return False;
3640 elsif Op = N_Op_Ne then
3641 return True;
3642 end if;
3643 end if;
3645 -- If OK to do replacement, test Is_Known_Non_Null flag
3647 if OK_To_Do_Constant_Replacement (E) then
3648 return Is_Known_Non_Null (E);
3650 -- Otherwise if not safe to do replacement, then say so
3652 else
3653 return False;
3654 end if;
3655 end;
3657 -- True if access attribute
3659 elsif Nkind (N) = N_Attribute_Reference
3660 and then (Attribute_Name (N) = Name_Access
3661 or else
3662 Attribute_Name (N) = Name_Unchecked_Access
3663 or else
3664 Attribute_Name (N) = Name_Unrestricted_Access)
3665 then
3666 return True;
3668 -- True if allocator
3670 elsif Nkind (N) = N_Allocator then
3671 return True;
3673 -- For a conversion, true if expression is known non-null
3675 elsif Nkind (N) = N_Type_Conversion then
3676 return Known_Non_Null (Expression (N));
3678 -- Above are all cases where the value could be determined to be
3679 -- non-null. In all other cases, we don't know, so return False.
3681 else
3682 return False;
3683 end if;
3684 end Known_Non_Null;
3686 ----------------
3687 -- Known_Null --
3688 ----------------
3690 function Known_Null (N : Node_Id) return Boolean is
3691 begin
3692 -- Checks for case where N is an entity reference
3694 if Is_Entity_Name (N) and then Present (Entity (N)) then
3695 declare
3696 E : constant Entity_Id := Entity (N);
3697 Op : Node_Kind;
3698 Val : Node_Id;
3700 begin
3701 -- Constant null value is for sure null
3703 if Ekind (E) = E_Constant
3704 and then Known_Null (Constant_Value (E))
3705 then
3706 return True;
3707 end if;
3709 -- First check if we are in decisive conditional
3711 Get_Current_Value_Condition (N, Op, Val);
3713 if Known_Null (Val) then
3714 if Op = N_Op_Eq then
3715 return True;
3716 elsif Op = N_Op_Ne then
3717 return False;
3718 end if;
3719 end if;
3721 -- If OK to do replacement, test Is_Known_Null flag
3723 if OK_To_Do_Constant_Replacement (E) then
3724 return Is_Known_Null (E);
3726 -- Otherwise if not safe to do replacement, then say so
3728 else
3729 return False;
3730 end if;
3731 end;
3733 -- True if explicit reference to null
3735 elsif Nkind (N) = N_Null then
3736 return True;
3738 -- For a conversion, true if expression is known null
3740 elsif Nkind (N) = N_Type_Conversion then
3741 return Known_Null (Expression (N));
3743 -- Above are all cases where the value could be determined to be null.
3744 -- In all other cases, we don't know, so return False.
3746 else
3747 return False;
3748 end if;
3749 end Known_Null;
3751 -----------------------------
3752 -- Make_CW_Equivalent_Type --
3753 -----------------------------
3755 -- Create a record type used as an equivalent of any member of the class
3756 -- which takes its size from exp.
3758 -- Generate the following code:
3760 -- type Equiv_T is record
3761 -- _parent : T (List of discriminant constraints taken from Exp);
3762 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
3763 -- end Equiv_T;
3765 -- ??? Note that this type does not guarantee same alignment as all
3766 -- derived types
3768 function Make_CW_Equivalent_Type
3769 (T : Entity_Id;
3770 E : Node_Id) return Entity_Id
3772 Loc : constant Source_Ptr := Sloc (E);
3773 Root_Typ : constant Entity_Id := Root_Type (T);
3774 List_Def : constant List_Id := Empty_List;
3775 Comp_List : constant List_Id := New_List;
3776 Equiv_Type : Entity_Id;
3777 Range_Type : Entity_Id;
3778 Str_Type : Entity_Id;
3779 Constr_Root : Entity_Id;
3780 Sizexpr : Node_Id;
3782 begin
3783 -- If the root type is already constrained, there are no discriminants
3784 -- in the expression.
3786 if not Has_Discriminants (Root_Typ)
3787 or else Is_Constrained (Root_Typ)
3788 then
3789 Constr_Root := Root_Typ;
3790 else
3791 Constr_Root := Make_Temporary (Loc, 'R');
3793 -- subtype cstr__n is T (List of discr constraints taken from Exp)
3795 Append_To (List_Def,
3796 Make_Subtype_Declaration (Loc,
3797 Defining_Identifier => Constr_Root,
3798 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
3799 end if;
3801 -- Generate the range subtype declaration
3803 Range_Type := Make_Temporary (Loc, 'G');
3805 if not Is_Interface (Root_Typ) then
3807 -- subtype rg__xx is
3808 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
3810 Sizexpr :=
3811 Make_Op_Subtract (Loc,
3812 Left_Opnd =>
3813 Make_Attribute_Reference (Loc,
3814 Prefix =>
3815 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3816 Attribute_Name => Name_Size),
3817 Right_Opnd =>
3818 Make_Attribute_Reference (Loc,
3819 Prefix => New_Reference_To (Constr_Root, Loc),
3820 Attribute_Name => Name_Object_Size));
3821 else
3822 -- subtype rg__xx is
3823 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
3825 Sizexpr :=
3826 Make_Attribute_Reference (Loc,
3827 Prefix =>
3828 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3829 Attribute_Name => Name_Size);
3830 end if;
3832 Set_Paren_Count (Sizexpr, 1);
3834 Append_To (List_Def,
3835 Make_Subtype_Declaration (Loc,
3836 Defining_Identifier => Range_Type,
3837 Subtype_Indication =>
3838 Make_Subtype_Indication (Loc,
3839 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
3840 Constraint => Make_Range_Constraint (Loc,
3841 Range_Expression =>
3842 Make_Range (Loc,
3843 Low_Bound => Make_Integer_Literal (Loc, 1),
3844 High_Bound =>
3845 Make_Op_Divide (Loc,
3846 Left_Opnd => Sizexpr,
3847 Right_Opnd => Make_Integer_Literal (Loc,
3848 Intval => System_Storage_Unit)))))));
3850 -- subtype str__nn is Storage_Array (rg__x);
3852 Str_Type := Make_Temporary (Loc, 'S');
3853 Append_To (List_Def,
3854 Make_Subtype_Declaration (Loc,
3855 Defining_Identifier => Str_Type,
3856 Subtype_Indication =>
3857 Make_Subtype_Indication (Loc,
3858 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
3859 Constraint =>
3860 Make_Index_Or_Discriminant_Constraint (Loc,
3861 Constraints =>
3862 New_List (New_Reference_To (Range_Type, Loc))))));
3864 -- type Equiv_T is record
3865 -- [ _parent : Tnn; ]
3866 -- E : Str_Type;
3867 -- end Equiv_T;
3869 Equiv_Type := Make_Temporary (Loc, 'T');
3870 Set_Ekind (Equiv_Type, E_Record_Type);
3871 Set_Parent_Subtype (Equiv_Type, Constr_Root);
3873 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
3874 -- treatment for this type. In particular, even though _parent's type
3875 -- is a controlled type or contains controlled components, we do not
3876 -- want to set Has_Controlled_Component on it to avoid making it gain
3877 -- an unwanted _controller component.
3879 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
3881 if not Is_Interface (Root_Typ) then
3882 Append_To (Comp_List,
3883 Make_Component_Declaration (Loc,
3884 Defining_Identifier =>
3885 Make_Defining_Identifier (Loc, Name_uParent),
3886 Component_Definition =>
3887 Make_Component_Definition (Loc,
3888 Aliased_Present => False,
3889 Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
3890 end if;
3892 Append_To (Comp_List,
3893 Make_Component_Declaration (Loc,
3894 Defining_Identifier => Make_Temporary (Loc, 'C'),
3895 Component_Definition =>
3896 Make_Component_Definition (Loc,
3897 Aliased_Present => False,
3898 Subtype_Indication => New_Reference_To (Str_Type, Loc))));
3900 Append_To (List_Def,
3901 Make_Full_Type_Declaration (Loc,
3902 Defining_Identifier => Equiv_Type,
3903 Type_Definition =>
3904 Make_Record_Definition (Loc,
3905 Component_List =>
3906 Make_Component_List (Loc,
3907 Component_Items => Comp_List,
3908 Variant_Part => Empty))));
3910 -- Suppress all checks during the analysis of the expanded code
3911 -- to avoid the generation of spurious warnings under ZFP run-time.
3913 Insert_Actions (E, List_Def, Suppress => All_Checks);
3914 return Equiv_Type;
3915 end Make_CW_Equivalent_Type;
3917 ------------------------
3918 -- Make_Literal_Range --
3919 ------------------------
3921 function Make_Literal_Range
3922 (Loc : Source_Ptr;
3923 Literal_Typ : Entity_Id) return Node_Id
3925 Lo : constant Node_Id :=
3926 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
3927 Index : constant Entity_Id := Etype (Lo);
3929 Hi : Node_Id;
3930 Length_Expr : constant Node_Id :=
3931 Make_Op_Subtract (Loc,
3932 Left_Opnd =>
3933 Make_Integer_Literal (Loc,
3934 Intval => String_Literal_Length (Literal_Typ)),
3935 Right_Opnd =>
3936 Make_Integer_Literal (Loc, 1));
3938 begin
3939 Set_Analyzed (Lo, False);
3941 if Is_Integer_Type (Index) then
3942 Hi :=
3943 Make_Op_Add (Loc,
3944 Left_Opnd => New_Copy_Tree (Lo),
3945 Right_Opnd => Length_Expr);
3946 else
3947 Hi :=
3948 Make_Attribute_Reference (Loc,
3949 Attribute_Name => Name_Val,
3950 Prefix => New_Occurrence_Of (Index, Loc),
3951 Expressions => New_List (
3952 Make_Op_Add (Loc,
3953 Left_Opnd =>
3954 Make_Attribute_Reference (Loc,
3955 Attribute_Name => Name_Pos,
3956 Prefix => New_Occurrence_Of (Index, Loc),
3957 Expressions => New_List (New_Copy_Tree (Lo))),
3958 Right_Opnd => Length_Expr)));
3959 end if;
3961 return
3962 Make_Range (Loc,
3963 Low_Bound => Lo,
3964 High_Bound => Hi);
3965 end Make_Literal_Range;
3967 --------------------------
3968 -- Make_Non_Empty_Check --
3969 --------------------------
3971 function Make_Non_Empty_Check
3972 (Loc : Source_Ptr;
3973 N : Node_Id) return Node_Id
3975 begin
3976 return
3977 Make_Op_Ne (Loc,
3978 Left_Opnd =>
3979 Make_Attribute_Reference (Loc,
3980 Attribute_Name => Name_Length,
3981 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
3982 Right_Opnd =>
3983 Make_Integer_Literal (Loc, 0));
3984 end Make_Non_Empty_Check;
3986 ----------------------------
3987 -- Make_Subtype_From_Expr --
3988 ----------------------------
3990 -- 1. If Expr is an unconstrained array expression, creates
3991 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
3993 -- 2. If Expr is a unconstrained discriminated type expression, creates
3994 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
3996 -- 3. If Expr is class-wide, creates an implicit class wide subtype
3998 function Make_Subtype_From_Expr
3999 (E : Node_Id;
4000 Unc_Typ : Entity_Id) return Node_Id
4002 Loc : constant Source_Ptr := Sloc (E);
4003 List_Constr : constant List_Id := New_List;
4004 D : Entity_Id;
4006 Full_Subtyp : Entity_Id;
4007 Priv_Subtyp : Entity_Id;
4008 Utyp : Entity_Id;
4009 Full_Exp : Node_Id;
4011 begin
4012 if Is_Private_Type (Unc_Typ)
4013 and then Has_Unknown_Discriminants (Unc_Typ)
4014 then
4015 -- Prepare the subtype completion, Go to base type to
4016 -- find underlying type, because the type may be a generic
4017 -- actual or an explicit subtype.
4019 Utyp := Underlying_Type (Base_Type (Unc_Typ));
4020 Full_Subtyp := Make_Temporary (Loc, 'C');
4021 Full_Exp :=
4022 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
4023 Set_Parent (Full_Exp, Parent (E));
4025 Priv_Subtyp := Make_Temporary (Loc, 'P');
4027 Insert_Action (E,
4028 Make_Subtype_Declaration (Loc,
4029 Defining_Identifier => Full_Subtyp,
4030 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
4032 -- Define the dummy private subtype
4034 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
4035 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
4036 Set_Scope (Priv_Subtyp, Full_Subtyp);
4037 Set_Is_Constrained (Priv_Subtyp);
4038 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
4039 Set_Is_Itype (Priv_Subtyp);
4040 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
4042 if Is_Tagged_Type (Priv_Subtyp) then
4043 Set_Class_Wide_Type
4044 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
4045 Set_Primitive_Operations (Priv_Subtyp,
4046 Primitive_Operations (Unc_Typ));
4047 end if;
4049 Set_Full_View (Priv_Subtyp, Full_Subtyp);
4051 return New_Reference_To (Priv_Subtyp, Loc);
4053 elsif Is_Array_Type (Unc_Typ) then
4054 for J in 1 .. Number_Dimensions (Unc_Typ) loop
4055 Append_To (List_Constr,
4056 Make_Range (Loc,
4057 Low_Bound =>
4058 Make_Attribute_Reference (Loc,
4059 Prefix => Duplicate_Subexpr_No_Checks (E),
4060 Attribute_Name => Name_First,
4061 Expressions => New_List (
4062 Make_Integer_Literal (Loc, J))),
4064 High_Bound =>
4065 Make_Attribute_Reference (Loc,
4066 Prefix => Duplicate_Subexpr_No_Checks (E),
4067 Attribute_Name => Name_Last,
4068 Expressions => New_List (
4069 Make_Integer_Literal (Loc, J)))));
4070 end loop;
4072 elsif Is_Class_Wide_Type (Unc_Typ) then
4073 declare
4074 CW_Subtype : Entity_Id;
4075 EQ_Typ : Entity_Id := Empty;
4077 begin
4078 -- A class-wide equivalent type is not needed when VM_Target
4079 -- because the VM back-ends handle the class-wide object
4080 -- initialization itself (and doesn't need or want the
4081 -- additional intermediate type to handle the assignment).
4083 if Expander_Active and then Tagged_Type_Expansion then
4085 -- If this is the class_wide type of a completion that is
4086 -- a record subtype, set the type of the class_wide type
4087 -- to be the full base type, for use in the expanded code
4088 -- for the equivalent type. Should this be done earlier when
4089 -- the completion is analyzed ???
4091 if Is_Private_Type (Etype (Unc_Typ))
4092 and then
4093 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
4094 then
4095 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
4096 end if;
4098 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
4099 end if;
4101 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
4102 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
4103 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
4105 return New_Occurrence_Of (CW_Subtype, Loc);
4106 end;
4108 -- Indefinite record type with discriminants
4110 else
4111 D := First_Discriminant (Unc_Typ);
4112 while Present (D) loop
4113 Append_To (List_Constr,
4114 Make_Selected_Component (Loc,
4115 Prefix => Duplicate_Subexpr_No_Checks (E),
4116 Selector_Name => New_Reference_To (D, Loc)));
4118 Next_Discriminant (D);
4119 end loop;
4120 end if;
4122 return
4123 Make_Subtype_Indication (Loc,
4124 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
4125 Constraint =>
4126 Make_Index_Or_Discriminant_Constraint (Loc,
4127 Constraints => List_Constr));
4128 end Make_Subtype_From_Expr;
4130 -----------------------------
4131 -- May_Generate_Large_Temp --
4132 -----------------------------
4134 -- At the current time, the only types that we return False for (i.e.
4135 -- where we decide we know they cannot generate large temps) are ones
4136 -- where we know the size is 256 bits or less at compile time, and we
4137 -- are still not doing a thorough job on arrays and records ???
4139 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
4140 begin
4141 if not Size_Known_At_Compile_Time (Typ) then
4142 return False;
4144 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
4145 return False;
4147 elsif Is_Array_Type (Typ)
4148 and then Present (Packed_Array_Type (Typ))
4149 then
4150 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
4152 -- We could do more here to find other small types ???
4154 else
4155 return True;
4156 end if;
4157 end May_Generate_Large_Temp;
4159 ----------------------------
4160 -- New_Class_Wide_Subtype --
4161 ----------------------------
4163 function New_Class_Wide_Subtype
4164 (CW_Typ : Entity_Id;
4165 N : Node_Id) return Entity_Id
4167 Res : constant Entity_Id := Create_Itype (E_Void, N);
4168 Res_Name : constant Name_Id := Chars (Res);
4169 Res_Scope : constant Entity_Id := Scope (Res);
4171 begin
4172 Copy_Node (CW_Typ, Res);
4173 Set_Comes_From_Source (Res, False);
4174 Set_Sloc (Res, Sloc (N));
4175 Set_Is_Itype (Res);
4176 Set_Associated_Node_For_Itype (Res, N);
4177 Set_Is_Public (Res, False); -- By default, may be changed below.
4178 Set_Public_Status (Res);
4179 Set_Chars (Res, Res_Name);
4180 Set_Scope (Res, Res_Scope);
4181 Set_Ekind (Res, E_Class_Wide_Subtype);
4182 Set_Next_Entity (Res, Empty);
4183 Set_Etype (Res, Base_Type (CW_Typ));
4184 Set_Is_Frozen (Res, False);
4185 Set_Freeze_Node (Res, Empty);
4186 return (Res);
4187 end New_Class_Wide_Subtype;
4189 --------------------------------
4190 -- Non_Limited_Designated_Type --
4191 ---------------------------------
4193 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
4194 Desig : constant Entity_Id := Designated_Type (T);
4195 begin
4196 if Ekind (Desig) = E_Incomplete_Type
4197 and then Present (Non_Limited_View (Desig))
4198 then
4199 return Non_Limited_View (Desig);
4200 else
4201 return Desig;
4202 end if;
4203 end Non_Limited_Designated_Type;
4205 -----------------------------------
4206 -- OK_To_Do_Constant_Replacement --
4207 -----------------------------------
4209 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
4210 ES : constant Entity_Id := Scope (E);
4211 CS : Entity_Id;
4213 begin
4214 -- Do not replace statically allocated objects, because they may be
4215 -- modified outside the current scope.
4217 if Is_Statically_Allocated (E) then
4218 return False;
4220 -- Do not replace aliased or volatile objects, since we don't know what
4221 -- else might change the value.
4223 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
4224 return False;
4226 -- Debug flag -gnatdM disconnects this optimization
4228 elsif Debug_Flag_MM then
4229 return False;
4231 -- Otherwise check scopes
4233 else
4234 CS := Current_Scope;
4236 loop
4237 -- If we are in right scope, replacement is safe
4239 if CS = ES then
4240 return True;
4242 -- Packages do not affect the determination of safety
4244 elsif Ekind (CS) = E_Package then
4245 exit when CS = Standard_Standard;
4246 CS := Scope (CS);
4248 -- Blocks do not affect the determination of safety
4250 elsif Ekind (CS) = E_Block then
4251 CS := Scope (CS);
4253 -- Loops do not affect the determination of safety. Note that we
4254 -- kill all current values on entry to a loop, so we are just
4255 -- talking about processing within a loop here.
4257 elsif Ekind (CS) = E_Loop then
4258 CS := Scope (CS);
4260 -- Otherwise, the reference is dubious, and we cannot be sure that
4261 -- it is safe to do the replacement.
4263 else
4264 exit;
4265 end if;
4266 end loop;
4268 return False;
4269 end if;
4270 end OK_To_Do_Constant_Replacement;
4272 ------------------------------------
4273 -- Possible_Bit_Aligned_Component --
4274 ------------------------------------
4276 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
4277 begin
4278 case Nkind (N) is
4280 -- Case of indexed component
4282 when N_Indexed_Component =>
4283 declare
4284 P : constant Node_Id := Prefix (N);
4285 Ptyp : constant Entity_Id := Etype (P);
4287 begin
4288 -- If we know the component size and it is less than 64, then
4289 -- we are definitely OK. The back end always does assignment of
4290 -- misaligned small objects correctly.
4292 if Known_Static_Component_Size (Ptyp)
4293 and then Component_Size (Ptyp) <= 64
4294 then
4295 return False;
4297 -- Otherwise, we need to test the prefix, to see if we are
4298 -- indexing from a possibly unaligned component.
4300 else
4301 return Possible_Bit_Aligned_Component (P);
4302 end if;
4303 end;
4305 -- Case of selected component
4307 when N_Selected_Component =>
4308 declare
4309 P : constant Node_Id := Prefix (N);
4310 Comp : constant Entity_Id := Entity (Selector_Name (N));
4312 begin
4313 -- If there is no component clause, then we are in the clear
4314 -- since the back end will never misalign a large component
4315 -- unless it is forced to do so. In the clear means we need
4316 -- only the recursive test on the prefix.
4318 if Component_May_Be_Bit_Aligned (Comp) then
4319 return True;
4320 else
4321 return Possible_Bit_Aligned_Component (P);
4322 end if;
4323 end;
4325 -- For a slice, test the prefix, if that is possibly misaligned,
4326 -- then for sure the slice is!
4328 when N_Slice =>
4329 return Possible_Bit_Aligned_Component (Prefix (N));
4331 -- If we have none of the above, it means that we have fallen off the
4332 -- top testing prefixes recursively, and we now have a stand alone
4333 -- object, where we don't have a problem.
4335 when others =>
4336 return False;
4338 end case;
4339 end Possible_Bit_Aligned_Component;
4341 -------------------------
4342 -- Remove_Side_Effects --
4343 -------------------------
4345 procedure Remove_Side_Effects
4346 (Exp : Node_Id;
4347 Name_Req : Boolean := False;
4348 Variable_Ref : Boolean := False)
4350 Loc : constant Source_Ptr := Sloc (Exp);
4351 Exp_Type : constant Entity_Id := Etype (Exp);
4352 Svg_Suppress : constant Suppress_Array := Scope_Suppress;
4353 Def_Id : Entity_Id;
4354 Ref_Type : Entity_Id;
4355 Res : Node_Id;
4356 Ptr_Typ_Decl : Node_Id;
4357 New_Exp : Node_Id;
4358 E : Node_Id;
4360 function Side_Effect_Free (N : Node_Id) return Boolean;
4361 -- Determines if the tree N represents an expression that is known not
4362 -- to have side effects, and for which no processing is required.
4364 function Side_Effect_Free (L : List_Id) return Boolean;
4365 -- Determines if all elements of the list L are side effect free
4367 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
4368 -- The argument N is a construct where the Prefix is dereferenced if it
4369 -- is an access type and the result is a variable. The call returns True
4370 -- if the construct is side effect free (not considering side effects in
4371 -- other than the prefix which are to be tested by the caller).
4373 function Within_In_Parameter (N : Node_Id) return Boolean;
4374 -- Determines if N is a subcomponent of a composite in-parameter. If so,
4375 -- N is not side-effect free when the actual is global and modifiable
4376 -- indirectly from within a subprogram, because it may be passed by
4377 -- reference. The front-end must be conservative here and assume that
4378 -- this may happen with any array or record type. On the other hand, we
4379 -- cannot create temporaries for all expressions for which this
4380 -- condition is true, for various reasons that might require clearing up
4381 -- ??? For example, discriminant references that appear out of place, or
4382 -- spurious type errors with class-wide expressions. As a result, we
4383 -- limit the transformation to loop bounds, which is so far the only
4384 -- case that requires it.
4386 -----------------------------
4387 -- Safe_Prefixed_Reference --
4388 -----------------------------
4390 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
4391 begin
4392 -- If prefix is not side effect free, definitely not safe
4394 if not Side_Effect_Free (Prefix (N)) then
4395 return False;
4397 -- If the prefix is of an access type that is not access-to-constant,
4398 -- then this construct is a variable reference, which means it is to
4399 -- be considered to have side effects if Variable_Ref is set True
4400 -- Exception is an access to an entity that is a constant or an
4401 -- in-parameter which does not come from source, and is the result
4402 -- of a previous removal of side-effects.
4404 elsif Is_Access_Type (Etype (Prefix (N)))
4405 and then not Is_Access_Constant (Etype (Prefix (N)))
4406 and then Variable_Ref
4407 then
4408 if not Is_Entity_Name (Prefix (N)) then
4409 return False;
4410 else
4411 return Ekind (Entity (Prefix (N))) = E_Constant
4412 or else Ekind (Entity (Prefix (N))) = E_In_Parameter;
4413 end if;
4415 -- The following test is the simplest way of solving a complex
4416 -- problem uncovered by BB08-010: Side effect on loop bound that
4417 -- is a subcomponent of a global variable:
4418 -- If a loop bound is a subcomponent of a global variable, a
4419 -- modification of that variable within the loop may incorrectly
4420 -- affect the execution of the loop.
4422 elsif not
4423 (Nkind (Parent (Parent (N))) /= N_Loop_Parameter_Specification
4424 or else not Within_In_Parameter (Prefix (N)))
4425 then
4426 return False;
4428 -- All other cases are side effect free
4430 else
4431 return True;
4432 end if;
4433 end Safe_Prefixed_Reference;
4435 ----------------------
4436 -- Side_Effect_Free --
4437 ----------------------
4439 function Side_Effect_Free (N : Node_Id) return Boolean is
4440 begin
4441 -- Note on checks that could raise Constraint_Error. Strictly, if
4442 -- we take advantage of 11.6, these checks do not count as side
4443 -- effects. However, we would just as soon consider that they are
4444 -- side effects, since the backend CSE does not work very well on
4445 -- expressions which can raise Constraint_Error. On the other
4446 -- hand, if we do not consider them to be side effect free, then
4447 -- we get some awkward expansions in -gnato mode, resulting in
4448 -- code insertions at a point where we do not have a clear model
4449 -- for performing the insertions.
4451 -- Special handling for entity names
4453 if Is_Entity_Name (N) then
4455 -- If the entity is a constant, it is definitely side effect
4456 -- free. Note that the test of Is_Variable (N) below might
4457 -- be expected to catch this case, but it does not, because
4458 -- this test goes to the original tree, and we may have
4459 -- already rewritten a variable node with a constant as
4460 -- a result of an earlier Force_Evaluation call.
4462 if Ekind_In (Entity (N), E_Constant, E_In_Parameter) then
4463 return True;
4465 -- Functions are not side effect free
4467 elsif Ekind (Entity (N)) = E_Function then
4468 return False;
4470 -- Variables are considered to be a side effect if Variable_Ref
4471 -- is set or if we have a volatile reference and Name_Req is off.
4472 -- If Name_Req is True then we can't help returning a name which
4473 -- effectively allows multiple references in any case.
4475 elsif Is_Variable (N) then
4476 return not Variable_Ref
4477 and then (not Is_Volatile_Reference (N) or else Name_Req);
4479 -- Any other entity (e.g. a subtype name) is definitely side
4480 -- effect free.
4482 else
4483 return True;
4484 end if;
4486 -- A value known at compile time is always side effect free
4488 elsif Compile_Time_Known_Value (N) then
4489 return True;
4491 -- A variable renaming is not side-effect free, because the
4492 -- renaming will function like a macro in the front-end in
4493 -- some cases, and an assignment can modify the component
4494 -- designated by N, so we need to create a temporary for it.
4496 elsif Is_Entity_Name (Original_Node (N))
4497 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
4498 and then Ekind (Entity (Original_Node (N))) /= E_Constant
4499 then
4500 return False;
4501 end if;
4503 -- For other than entity names and compile time known values,
4504 -- check the node kind for special processing.
4506 case Nkind (N) is
4508 -- An attribute reference is side effect free if its expressions
4509 -- are side effect free and its prefix is side effect free or
4510 -- is an entity reference.
4512 -- Is this right? what about x'first where x is a variable???
4514 when N_Attribute_Reference =>
4515 return Side_Effect_Free (Expressions (N))
4516 and then Attribute_Name (N) /= Name_Input
4517 and then (Is_Entity_Name (Prefix (N))
4518 or else Side_Effect_Free (Prefix (N)));
4520 -- A binary operator is side effect free if and both operands
4521 -- are side effect free. For this purpose binary operators
4522 -- include membership tests and short circuit forms
4524 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
4525 return Side_Effect_Free (Left_Opnd (N))
4526 and then
4527 Side_Effect_Free (Right_Opnd (N));
4529 -- An explicit dereference is side effect free only if it is
4530 -- a side effect free prefixed reference.
4532 when N_Explicit_Dereference =>
4533 return Safe_Prefixed_Reference (N);
4535 -- A call to _rep_to_pos is side effect free, since we generate
4536 -- this pure function call ourselves. Moreover it is critically
4537 -- important to make this exception, since otherwise we can
4538 -- have discriminants in array components which don't look
4539 -- side effect free in the case of an array whose index type
4540 -- is an enumeration type with an enumeration rep clause.
4542 -- All other function calls are not side effect free
4544 when N_Function_Call =>
4545 return Nkind (Name (N)) = N_Identifier
4546 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
4547 and then
4548 Side_Effect_Free (First (Parameter_Associations (N)));
4550 -- An indexed component is side effect free if it is a side
4551 -- effect free prefixed reference and all the indexing
4552 -- expressions are side effect free.
4554 when N_Indexed_Component =>
4555 return Side_Effect_Free (Expressions (N))
4556 and then Safe_Prefixed_Reference (N);
4558 -- A type qualification is side effect free if the expression
4559 -- is side effect free.
4561 when N_Qualified_Expression =>
4562 return Side_Effect_Free (Expression (N));
4564 -- A selected component is side effect free only if it is a
4565 -- side effect free prefixed reference. If it designates a
4566 -- component with a rep. clause it must be treated has having
4567 -- a potential side effect, because it may be modified through
4568 -- a renaming, and a subsequent use of the renaming as a macro
4569 -- will yield the wrong value. This complex interaction between
4570 -- renaming and removing side effects is a reminder that the
4571 -- latter has become a headache to maintain, and that it should
4572 -- be removed in favor of the gcc mechanism to capture values ???
4574 when N_Selected_Component =>
4575 if Nkind (Parent (N)) = N_Explicit_Dereference
4576 and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
4577 then
4578 return False;
4579 else
4580 return Safe_Prefixed_Reference (N);
4581 end if;
4583 -- A range is side effect free if the bounds are side effect free
4585 when N_Range =>
4586 return Side_Effect_Free (Low_Bound (N))
4587 and then Side_Effect_Free (High_Bound (N));
4589 -- A slice is side effect free if it is a side effect free
4590 -- prefixed reference and the bounds are side effect free.
4592 when N_Slice =>
4593 return Side_Effect_Free (Discrete_Range (N))
4594 and then Safe_Prefixed_Reference (N);
4596 -- A type conversion is side effect free if the expression to be
4597 -- converted is side effect free.
4599 when N_Type_Conversion =>
4600 return Side_Effect_Free (Expression (N));
4602 -- A unary operator is side effect free if the operand
4603 -- is side effect free.
4605 when N_Unary_Op =>
4606 return Side_Effect_Free (Right_Opnd (N));
4608 -- An unchecked type conversion is side effect free only if it
4609 -- is safe and its argument is side effect free.
4611 when N_Unchecked_Type_Conversion =>
4612 return Safe_Unchecked_Type_Conversion (N)
4613 and then Side_Effect_Free (Expression (N));
4615 -- An unchecked expression is side effect free if its expression
4616 -- is side effect free.
4618 when N_Unchecked_Expression =>
4619 return Side_Effect_Free (Expression (N));
4621 -- A literal is side effect free
4623 when N_Character_Literal |
4624 N_Integer_Literal |
4625 N_Real_Literal |
4626 N_String_Literal =>
4627 return True;
4629 -- We consider that anything else has side effects. This is a bit
4630 -- crude, but we are pretty close for most common cases, and we
4631 -- are certainly correct (i.e. we never return True when the
4632 -- answer should be False).
4634 when others =>
4635 return False;
4636 end case;
4637 end Side_Effect_Free;
4639 -- A list is side effect free if all elements of the list are
4640 -- side effect free.
4642 function Side_Effect_Free (L : List_Id) return Boolean is
4643 N : Node_Id;
4645 begin
4646 if L = No_List or else L = Error_List then
4647 return True;
4649 else
4650 N := First (L);
4651 while Present (N) loop
4652 if not Side_Effect_Free (N) then
4653 return False;
4654 else
4655 Next (N);
4656 end if;
4657 end loop;
4659 return True;
4660 end if;
4661 end Side_Effect_Free;
4663 -------------------------
4664 -- Within_In_Parameter --
4665 -------------------------
4667 function Within_In_Parameter (N : Node_Id) return Boolean is
4668 begin
4669 if not Comes_From_Source (N) then
4670 return False;
4672 elsif Is_Entity_Name (N) then
4673 return Ekind (Entity (N)) = E_In_Parameter;
4675 elsif Nkind (N) = N_Indexed_Component
4676 or else Nkind (N) = N_Selected_Component
4677 then
4678 return Within_In_Parameter (Prefix (N));
4679 else
4681 return False;
4682 end if;
4683 end Within_In_Parameter;
4685 -- Start of processing for Remove_Side_Effects
4687 begin
4688 -- If we are side effect free already or expansion is disabled,
4689 -- there is nothing to do.
4691 if Side_Effect_Free (Exp) or else not Expander_Active then
4692 return;
4693 end if;
4695 -- All this must not have any checks
4697 Scope_Suppress := (others => True);
4699 -- If it is a scalar type and we need to capture the value, just make
4700 -- a copy. Likewise for a function call, an attribute reference, an
4701 -- allocator, or an operator. And if we have a volatile reference and
4702 -- Name_Req is not set (see comments above for Side_Effect_Free).
4704 if Is_Elementary_Type (Exp_Type)
4705 and then (Variable_Ref
4706 or else Nkind (Exp) = N_Function_Call
4707 or else Nkind (Exp) = N_Attribute_Reference
4708 or else Nkind (Exp) = N_Allocator
4709 or else Nkind (Exp) in N_Op
4710 or else (not Name_Req and then Is_Volatile_Reference (Exp)))
4711 then
4712 Def_Id := Make_Temporary (Loc, 'R', Exp);
4713 Set_Etype (Def_Id, Exp_Type);
4714 Res := New_Reference_To (Def_Id, Loc);
4716 E :=
4717 Make_Object_Declaration (Loc,
4718 Defining_Identifier => Def_Id,
4719 Object_Definition => New_Reference_To (Exp_Type, Loc),
4720 Constant_Present => True,
4721 Expression => Relocate_Node (Exp));
4723 Set_Assignment_OK (E);
4724 Insert_Action (Exp, E);
4726 -- If the expression has the form v.all then we can just capture
4727 -- the pointer, and then do an explicit dereference on the result.
4729 elsif Nkind (Exp) = N_Explicit_Dereference then
4730 Def_Id := Make_Temporary (Loc, 'R', Exp);
4731 Res :=
4732 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
4734 Insert_Action (Exp,
4735 Make_Object_Declaration (Loc,
4736 Defining_Identifier => Def_Id,
4737 Object_Definition =>
4738 New_Reference_To (Etype (Prefix (Exp)), Loc),
4739 Constant_Present => True,
4740 Expression => Relocate_Node (Prefix (Exp))));
4742 -- Similar processing for an unchecked conversion of an expression
4743 -- of the form v.all, where we want the same kind of treatment.
4745 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4746 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
4747 then
4748 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4749 Scope_Suppress := Svg_Suppress;
4750 return;
4752 -- If this is a type conversion, leave the type conversion and remove
4753 -- the side effects in the expression. This is important in several
4754 -- circumstances: for change of representations, and also when this is
4755 -- a view conversion to a smaller object, where gigi can end up creating
4756 -- its own temporary of the wrong size.
4758 elsif Nkind (Exp) = N_Type_Conversion then
4759 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4760 Scope_Suppress := Svg_Suppress;
4761 return;
4763 -- If this is an unchecked conversion that Gigi can't handle, make
4764 -- a copy or a use a renaming to capture the value.
4766 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4767 and then not Safe_Unchecked_Type_Conversion (Exp)
4768 then
4769 if CW_Or_Has_Controlled_Part (Exp_Type) then
4771 -- Use a renaming to capture the expression, rather than create
4772 -- a controlled temporary.
4774 Def_Id := Make_Temporary (Loc, 'R', Exp);
4775 Res := New_Reference_To (Def_Id, Loc);
4777 Insert_Action (Exp,
4778 Make_Object_Renaming_Declaration (Loc,
4779 Defining_Identifier => Def_Id,
4780 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4781 Name => Relocate_Node (Exp)));
4783 else
4784 Def_Id := Make_Temporary (Loc, 'R', Exp);
4785 Set_Etype (Def_Id, Exp_Type);
4786 Res := New_Reference_To (Def_Id, Loc);
4788 E :=
4789 Make_Object_Declaration (Loc,
4790 Defining_Identifier => Def_Id,
4791 Object_Definition => New_Reference_To (Exp_Type, Loc),
4792 Constant_Present => not Is_Variable (Exp),
4793 Expression => Relocate_Node (Exp));
4795 Set_Assignment_OK (E);
4796 Insert_Action (Exp, E);
4797 end if;
4799 -- For expressions that denote objects, we can use a renaming scheme.
4800 -- We skip using this if we have a volatile reference and we do not
4801 -- have Name_Req set true (see comments above for Side_Effect_Free).
4803 elsif Is_Object_Reference (Exp)
4804 and then Nkind (Exp) /= N_Function_Call
4805 and then (Name_Req or else not Is_Volatile_Reference (Exp))
4806 then
4807 Def_Id := Make_Temporary (Loc, 'R', Exp);
4809 if Nkind (Exp) = N_Selected_Component
4810 and then Nkind (Prefix (Exp)) = N_Function_Call
4811 and then Is_Array_Type (Exp_Type)
4812 then
4813 -- Avoid generating a variable-sized temporary, by generating
4814 -- the renaming declaration just for the function call. The
4815 -- transformation could be refined to apply only when the array
4816 -- component is constrained by a discriminant???
4818 Res :=
4819 Make_Selected_Component (Loc,
4820 Prefix => New_Occurrence_Of (Def_Id, Loc),
4821 Selector_Name => Selector_Name (Exp));
4823 Insert_Action (Exp,
4824 Make_Object_Renaming_Declaration (Loc,
4825 Defining_Identifier => Def_Id,
4826 Subtype_Mark =>
4827 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
4828 Name => Relocate_Node (Prefix (Exp))));
4830 else
4831 Res := New_Reference_To (Def_Id, Loc);
4833 Insert_Action (Exp,
4834 Make_Object_Renaming_Declaration (Loc,
4835 Defining_Identifier => Def_Id,
4836 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4837 Name => Relocate_Node (Exp)));
4838 end if;
4840 -- If this is a packed reference, or a selected component with a
4841 -- non-standard representation, a reference to the temporary will
4842 -- be replaced by a copy of the original expression (see
4843 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
4844 -- elaborated by gigi, and is of course not to be replaced in-line
4845 -- by the expression it renames, which would defeat the purpose of
4846 -- removing the side-effect.
4848 if (Nkind (Exp) = N_Selected_Component
4849 or else Nkind (Exp) = N_Indexed_Component)
4850 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
4851 then
4852 null;
4853 else
4854 Set_Is_Renaming_Of_Object (Def_Id, False);
4855 end if;
4857 -- Otherwise we generate a reference to the value
4859 else
4860 -- Special processing for function calls that return a limited type.
4861 -- We need to build a declaration that will enable build-in-place
4862 -- expansion of the call. This is not done if the context is already
4863 -- an object declaration, to prevent infinite recursion.
4865 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
4866 -- to accommodate functions returning limited objects by reference.
4868 if Nkind (Exp) = N_Function_Call
4869 and then Is_Inherently_Limited_Type (Etype (Exp))
4870 and then Nkind (Parent (Exp)) /= N_Object_Declaration
4871 and then Ada_Version >= Ada_05
4872 then
4873 declare
4874 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
4875 Decl : Node_Id;
4877 begin
4878 Decl :=
4879 Make_Object_Declaration (Loc,
4880 Defining_Identifier => Obj,
4881 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
4882 Expression => Relocate_Node (Exp));
4884 Insert_Action (Exp, Decl);
4885 Set_Etype (Obj, Exp_Type);
4886 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
4887 return;
4888 end;
4889 end if;
4891 Ref_Type := Make_Temporary (Loc, 'A');
4893 Ptr_Typ_Decl :=
4894 Make_Full_Type_Declaration (Loc,
4895 Defining_Identifier => Ref_Type,
4896 Type_Definition =>
4897 Make_Access_To_Object_Definition (Loc,
4898 All_Present => True,
4899 Subtype_Indication =>
4900 New_Reference_To (Exp_Type, Loc)));
4902 E := Exp;
4903 Insert_Action (Exp, Ptr_Typ_Decl);
4905 Def_Id := Make_Temporary (Loc, 'R', Exp);
4906 Set_Etype (Def_Id, Exp_Type);
4908 Res :=
4909 Make_Explicit_Dereference (Loc,
4910 Prefix => New_Reference_To (Def_Id, Loc));
4912 if Nkind (E) = N_Explicit_Dereference then
4913 New_Exp := Relocate_Node (Prefix (E));
4914 else
4915 E := Relocate_Node (E);
4916 New_Exp := Make_Reference (Loc, E);
4917 end if;
4919 if Is_Delayed_Aggregate (E) then
4921 -- The expansion of nested aggregates is delayed until the
4922 -- enclosing aggregate is expanded. As aggregates are often
4923 -- qualified, the predicate applies to qualified expressions
4924 -- as well, indicating that the enclosing aggregate has not
4925 -- been expanded yet. At this point the aggregate is part of
4926 -- a stand-alone declaration, and must be fully expanded.
4928 if Nkind (E) = N_Qualified_Expression then
4929 Set_Expansion_Delayed (Expression (E), False);
4930 Set_Analyzed (Expression (E), False);
4931 else
4932 Set_Expansion_Delayed (E, False);
4933 end if;
4935 Set_Analyzed (E, False);
4936 end if;
4938 Insert_Action (Exp,
4939 Make_Object_Declaration (Loc,
4940 Defining_Identifier => Def_Id,
4941 Object_Definition => New_Reference_To (Ref_Type, Loc),
4942 Expression => New_Exp));
4943 end if;
4945 -- Preserve the Assignment_OK flag in all copies, since at least
4946 -- one copy may be used in a context where this flag must be set
4947 -- (otherwise why would the flag be set in the first place).
4949 Set_Assignment_OK (Res, Assignment_OK (Exp));
4951 -- Finally rewrite the original expression and we are done
4953 Rewrite (Exp, Res);
4954 Analyze_And_Resolve (Exp, Exp_Type);
4955 Scope_Suppress := Svg_Suppress;
4956 end Remove_Side_Effects;
4958 ---------------------------
4959 -- Represented_As_Scalar --
4960 ---------------------------
4962 function Represented_As_Scalar (T : Entity_Id) return Boolean is
4963 UT : constant Entity_Id := Underlying_Type (T);
4964 begin
4965 return Is_Scalar_Type (UT)
4966 or else (Is_Bit_Packed_Array (UT)
4967 and then Is_Scalar_Type (Packed_Array_Type (UT)));
4968 end Represented_As_Scalar;
4970 ------------------------------------
4971 -- Safe_Unchecked_Type_Conversion --
4972 ------------------------------------
4974 -- Note: this function knows quite a bit about the exact requirements
4975 -- of Gigi with respect to unchecked type conversions, and its code
4976 -- must be coordinated with any changes in Gigi in this area.
4978 -- The above requirements should be documented in Sinfo ???
4980 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
4981 Otyp : Entity_Id;
4982 Ityp : Entity_Id;
4983 Oalign : Uint;
4984 Ialign : Uint;
4985 Pexp : constant Node_Id := Parent (Exp);
4987 begin
4988 -- If the expression is the RHS of an assignment or object declaration
4989 -- we are always OK because there will always be a target.
4991 -- Object renaming declarations, (generated for view conversions of
4992 -- actuals in inlined calls), like object declarations, provide an
4993 -- explicit type, and are safe as well.
4995 if (Nkind (Pexp) = N_Assignment_Statement
4996 and then Expression (Pexp) = Exp)
4997 or else Nkind (Pexp) = N_Object_Declaration
4998 or else Nkind (Pexp) = N_Object_Renaming_Declaration
4999 then
5000 return True;
5002 -- If the expression is the prefix of an N_Selected_Component
5003 -- we should also be OK because GCC knows to look inside the
5004 -- conversion except if the type is discriminated. We assume
5005 -- that we are OK anyway if the type is not set yet or if it is
5006 -- controlled since we can't afford to introduce a temporary in
5007 -- this case.
5009 elsif Nkind (Pexp) = N_Selected_Component
5010 and then Prefix (Pexp) = Exp
5011 then
5012 if No (Etype (Pexp)) then
5013 return True;
5014 else
5015 return
5016 not Has_Discriminants (Etype (Pexp))
5017 or else Is_Constrained (Etype (Pexp));
5018 end if;
5019 end if;
5021 -- Set the output type, this comes from Etype if it is set, otherwise
5022 -- we take it from the subtype mark, which we assume was already
5023 -- fully analyzed.
5025 if Present (Etype (Exp)) then
5026 Otyp := Etype (Exp);
5027 else
5028 Otyp := Entity (Subtype_Mark (Exp));
5029 end if;
5031 -- The input type always comes from the expression, and we assume
5032 -- this is indeed always analyzed, so we can simply get the Etype.
5034 Ityp := Etype (Expression (Exp));
5036 -- Initialize alignments to unknown so far
5038 Oalign := No_Uint;
5039 Ialign := No_Uint;
5041 -- Replace a concurrent type by its corresponding record type
5042 -- and each type by its underlying type and do the tests on those.
5043 -- The original type may be a private type whose completion is a
5044 -- concurrent type, so find the underlying type first.
5046 if Present (Underlying_Type (Otyp)) then
5047 Otyp := Underlying_Type (Otyp);
5048 end if;
5050 if Present (Underlying_Type (Ityp)) then
5051 Ityp := Underlying_Type (Ityp);
5052 end if;
5054 if Is_Concurrent_Type (Otyp) then
5055 Otyp := Corresponding_Record_Type (Otyp);
5056 end if;
5058 if Is_Concurrent_Type (Ityp) then
5059 Ityp := Corresponding_Record_Type (Ityp);
5060 end if;
5062 -- If the base types are the same, we know there is no problem since
5063 -- this conversion will be a noop.
5065 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
5066 return True;
5068 -- Same if this is an upwards conversion of an untagged type, and there
5069 -- are no constraints involved (could be more general???)
5071 elsif Etype (Ityp) = Otyp
5072 and then not Is_Tagged_Type (Ityp)
5073 and then not Has_Discriminants (Ityp)
5074 and then No (First_Rep_Item (Base_Type (Ityp)))
5075 then
5076 return True;
5078 -- If the expression has an access type (object or subprogram) we
5079 -- assume that the conversion is safe, because the size of the target
5080 -- is safe, even if it is a record (which might be treated as having
5081 -- unknown size at this point).
5083 elsif Is_Access_Type (Ityp) then
5084 return True;
5086 -- If the size of output type is known at compile time, there is
5087 -- never a problem. Note that unconstrained records are considered
5088 -- to be of known size, but we can't consider them that way here,
5089 -- because we are talking about the actual size of the object.
5091 -- We also make sure that in addition to the size being known, we do
5092 -- not have a case which might generate an embarrassingly large temp
5093 -- in stack checking mode.
5095 elsif Size_Known_At_Compile_Time (Otyp)
5096 and then
5097 (not Stack_Checking_Enabled
5098 or else not May_Generate_Large_Temp (Otyp))
5099 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
5100 then
5101 return True;
5103 -- If either type is tagged, then we know the alignment is OK so
5104 -- Gigi will be able to use pointer punning.
5106 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
5107 return True;
5109 -- If either type is a limited record type, we cannot do a copy, so
5110 -- say safe since there's nothing else we can do.
5112 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
5113 return True;
5115 -- Conversions to and from packed array types are always ignored and
5116 -- hence are safe.
5118 elsif Is_Packed_Array_Type (Otyp)
5119 or else Is_Packed_Array_Type (Ityp)
5120 then
5121 return True;
5122 end if;
5124 -- The only other cases known to be safe is if the input type's
5125 -- alignment is known to be at least the maximum alignment for the
5126 -- target or if both alignments are known and the output type's
5127 -- alignment is no stricter than the input's. We can use the alignment
5128 -- of the component type of an array if a type is an unpacked
5129 -- array type.
5131 if Present (Alignment_Clause (Otyp)) then
5132 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
5134 elsif Is_Array_Type (Otyp)
5135 and then Present (Alignment_Clause (Component_Type (Otyp)))
5136 then
5137 Oalign := Expr_Value (Expression (Alignment_Clause
5138 (Component_Type (Otyp))));
5139 end if;
5141 if Present (Alignment_Clause (Ityp)) then
5142 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
5144 elsif Is_Array_Type (Ityp)
5145 and then Present (Alignment_Clause (Component_Type (Ityp)))
5146 then
5147 Ialign := Expr_Value (Expression (Alignment_Clause
5148 (Component_Type (Ityp))));
5149 end if;
5151 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
5152 return True;
5154 elsif Ialign /= No_Uint and then Oalign /= No_Uint
5155 and then Ialign <= Oalign
5156 then
5157 return True;
5159 -- Otherwise, Gigi cannot handle this and we must make a temporary
5161 else
5162 return False;
5163 end if;
5164 end Safe_Unchecked_Type_Conversion;
5166 ---------------------------------
5167 -- Set_Current_Value_Condition --
5168 ---------------------------------
5170 -- Note: the implementation of this procedure is very closely tied to the
5171 -- implementation of Get_Current_Value_Condition. Here we set required
5172 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
5173 -- them, so they must have a consistent view.
5175 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
5177 procedure Set_Entity_Current_Value (N : Node_Id);
5178 -- If N is an entity reference, where the entity is of an appropriate
5179 -- kind, then set the current value of this entity to Cnode, unless
5180 -- there is already a definite value set there.
5182 procedure Set_Expression_Current_Value (N : Node_Id);
5183 -- If N is of an appropriate form, sets an appropriate entry in current
5184 -- value fields of relevant entities. Multiple entities can be affected
5185 -- in the case of an AND or AND THEN.
5187 ------------------------------
5188 -- Set_Entity_Current_Value --
5189 ------------------------------
5191 procedure Set_Entity_Current_Value (N : Node_Id) is
5192 begin
5193 if Is_Entity_Name (N) then
5194 declare
5195 Ent : constant Entity_Id := Entity (N);
5197 begin
5198 -- Don't capture if not safe to do so
5200 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
5201 return;
5202 end if;
5204 -- Here we have a case where the Current_Value field may
5205 -- need to be set. We set it if it is not already set to a
5206 -- compile time expression value.
5208 -- Note that this represents a decision that one condition
5209 -- blots out another previous one. That's certainly right
5210 -- if they occur at the same level. If the second one is
5211 -- nested, then the decision is neither right nor wrong (it
5212 -- would be equally OK to leave the outer one in place, or
5213 -- take the new inner one. Really we should record both, but
5214 -- our data structures are not that elaborate.
5216 if Nkind (Current_Value (Ent)) not in N_Subexpr then
5217 Set_Current_Value (Ent, Cnode);
5218 end if;
5219 end;
5220 end if;
5221 end Set_Entity_Current_Value;
5223 ----------------------------------
5224 -- Set_Expression_Current_Value --
5225 ----------------------------------
5227 procedure Set_Expression_Current_Value (N : Node_Id) is
5228 Cond : Node_Id;
5230 begin
5231 Cond := N;
5233 -- Loop to deal with (ignore for now) any NOT operators present. The
5234 -- presence of NOT operators will be handled properly when we call
5235 -- Get_Current_Value_Condition.
5237 while Nkind (Cond) = N_Op_Not loop
5238 Cond := Right_Opnd (Cond);
5239 end loop;
5241 -- For an AND or AND THEN, recursively process operands
5243 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
5244 Set_Expression_Current_Value (Left_Opnd (Cond));
5245 Set_Expression_Current_Value (Right_Opnd (Cond));
5246 return;
5247 end if;
5249 -- Check possible relational operator
5251 if Nkind (Cond) in N_Op_Compare then
5252 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
5253 Set_Entity_Current_Value (Left_Opnd (Cond));
5254 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
5255 Set_Entity_Current_Value (Right_Opnd (Cond));
5256 end if;
5258 -- Check possible boolean variable reference
5260 else
5261 Set_Entity_Current_Value (Cond);
5262 end if;
5263 end Set_Expression_Current_Value;
5265 -- Start of processing for Set_Current_Value_Condition
5267 begin
5268 Set_Expression_Current_Value (Condition (Cnode));
5269 end Set_Current_Value_Condition;
5271 --------------------------
5272 -- Set_Elaboration_Flag --
5273 --------------------------
5275 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
5276 Loc : constant Source_Ptr := Sloc (N);
5277 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
5278 Asn : Node_Id;
5280 begin
5281 if Present (Ent) then
5283 -- Nothing to do if at the compilation unit level, because in this
5284 -- case the flag is set by the binder generated elaboration routine.
5286 if Nkind (Parent (N)) = N_Compilation_Unit then
5287 null;
5289 -- Here we do need to generate an assignment statement
5291 else
5292 Check_Restriction (No_Elaboration_Code, N);
5293 Asn :=
5294 Make_Assignment_Statement (Loc,
5295 Name => New_Occurrence_Of (Ent, Loc),
5296 Expression => New_Occurrence_Of (Standard_True, Loc));
5298 if Nkind (Parent (N)) = N_Subunit then
5299 Insert_After (Corresponding_Stub (Parent (N)), Asn);
5300 else
5301 Insert_After (N, Asn);
5302 end if;
5304 Analyze (Asn);
5306 -- Kill current value indication. This is necessary because the
5307 -- tests of this flag are inserted out of sequence and must not
5308 -- pick up bogus indications of the wrong constant value.
5310 Set_Current_Value (Ent, Empty);
5311 end if;
5312 end if;
5313 end Set_Elaboration_Flag;
5315 ----------------------------
5316 -- Set_Renamed_Subprogram --
5317 ----------------------------
5319 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
5320 begin
5321 -- If input node is an identifier, we can just reset it
5323 if Nkind (N) = N_Identifier then
5324 Set_Chars (N, Chars (E));
5325 Set_Entity (N, E);
5327 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
5329 else
5330 declare
5331 CS : constant Boolean := Comes_From_Source (N);
5332 begin
5333 Rewrite (N, Make_Identifier (Sloc (N), Chars => Chars (E)));
5334 Set_Entity (N, E);
5335 Set_Comes_From_Source (N, CS);
5336 Set_Analyzed (N, True);
5337 end;
5338 end if;
5339 end Set_Renamed_Subprogram;
5341 ----------------------------------
5342 -- Silly_Boolean_Array_Not_Test --
5343 ----------------------------------
5345 -- This procedure implements an odd and silly test. We explicitly check
5346 -- for the case where the 'First of the component type is equal to the
5347 -- 'Last of this component type, and if this is the case, we make sure
5348 -- that constraint error is raised. The reason is that the NOT is bound
5349 -- to cause CE in this case, and we will not otherwise catch it.
5351 -- No such check is required for AND and OR, since for both these cases
5352 -- False op False = False, and True op True = True. For the XOR case,
5353 -- see Silly_Boolean_Array_Xor_Test.
5355 -- Believe it or not, this was reported as a bug. Note that nearly
5356 -- always, the test will evaluate statically to False, so the code will
5357 -- be statically removed, and no extra overhead caused.
5359 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
5360 Loc : constant Source_Ptr := Sloc (N);
5361 CT : constant Entity_Id := Component_Type (T);
5363 begin
5364 -- The check we install is
5366 -- constraint_error when
5367 -- component_type'first = component_type'last
5368 -- and then array_type'Length /= 0)
5370 -- We need the last guard because we don't want to raise CE for empty
5371 -- arrays since no out of range values result. (Empty arrays with a
5372 -- component type of True .. True -- very useful -- even the ACATS
5373 -- does not test that marginal case!)
5375 Insert_Action (N,
5376 Make_Raise_Constraint_Error (Loc,
5377 Condition =>
5378 Make_And_Then (Loc,
5379 Left_Opnd =>
5380 Make_Op_Eq (Loc,
5381 Left_Opnd =>
5382 Make_Attribute_Reference (Loc,
5383 Prefix => New_Occurrence_Of (CT, Loc),
5384 Attribute_Name => Name_First),
5386 Right_Opnd =>
5387 Make_Attribute_Reference (Loc,
5388 Prefix => New_Occurrence_Of (CT, Loc),
5389 Attribute_Name => Name_Last)),
5391 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5392 Reason => CE_Range_Check_Failed));
5393 end Silly_Boolean_Array_Not_Test;
5395 ----------------------------------
5396 -- Silly_Boolean_Array_Xor_Test --
5397 ----------------------------------
5399 -- This procedure implements an odd and silly test. We explicitly check
5400 -- for the XOR case where the component type is True .. True, since this
5401 -- will raise constraint error. A special check is required since CE
5402 -- will not be generated otherwise (cf Expand_Packed_Not).
5404 -- No such check is required for AND and OR, since for both these cases
5405 -- False op False = False, and True op True = True, and no check is
5406 -- required for the case of False .. False, since False xor False = False.
5407 -- See also Silly_Boolean_Array_Not_Test
5409 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
5410 Loc : constant Source_Ptr := Sloc (N);
5411 CT : constant Entity_Id := Component_Type (T);
5413 begin
5414 -- The check we install is
5416 -- constraint_error when
5417 -- Boolean (component_type'First)
5418 -- and then Boolean (component_type'Last)
5419 -- and then array_type'Length /= 0)
5421 -- We need the last guard because we don't want to raise CE for empty
5422 -- arrays since no out of range values result (Empty arrays with a
5423 -- component type of True .. True -- very useful -- even the ACATS
5424 -- does not test that marginal case!).
5426 Insert_Action (N,
5427 Make_Raise_Constraint_Error (Loc,
5428 Condition =>
5429 Make_And_Then (Loc,
5430 Left_Opnd =>
5431 Make_And_Then (Loc,
5432 Left_Opnd =>
5433 Convert_To (Standard_Boolean,
5434 Make_Attribute_Reference (Loc,
5435 Prefix => New_Occurrence_Of (CT, Loc),
5436 Attribute_Name => Name_First)),
5438 Right_Opnd =>
5439 Convert_To (Standard_Boolean,
5440 Make_Attribute_Reference (Loc,
5441 Prefix => New_Occurrence_Of (CT, Loc),
5442 Attribute_Name => Name_Last))),
5444 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5445 Reason => CE_Range_Check_Failed));
5446 end Silly_Boolean_Array_Xor_Test;
5448 --------------------------
5449 -- Target_Has_Fixed_Ops --
5450 --------------------------
5452 Integer_Sized_Small : Ureal;
5453 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this
5454 -- function is called (we don't want to compute it more than once!)
5456 Long_Integer_Sized_Small : Ureal;
5457 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
5458 -- function is called (we don't want to compute it more than once)
5460 First_Time_For_THFO : Boolean := True;
5461 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
5463 function Target_Has_Fixed_Ops
5464 (Left_Typ : Entity_Id;
5465 Right_Typ : Entity_Id;
5466 Result_Typ : Entity_Id) return Boolean
5468 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
5469 -- Return True if the given type is a fixed-point type with a small
5470 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
5471 -- an absolute value less than 1.0. This is currently limited
5472 -- to fixed-point types that map to Integer or Long_Integer.
5474 ------------------------
5475 -- Is_Fractional_Type --
5476 ------------------------
5478 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
5479 begin
5480 if Esize (Typ) = Standard_Integer_Size then
5481 return Small_Value (Typ) = Integer_Sized_Small;
5483 elsif Esize (Typ) = Standard_Long_Integer_Size then
5484 return Small_Value (Typ) = Long_Integer_Sized_Small;
5486 else
5487 return False;
5488 end if;
5489 end Is_Fractional_Type;
5491 -- Start of processing for Target_Has_Fixed_Ops
5493 begin
5494 -- Return False if Fractional_Fixed_Ops_On_Target is false
5496 if not Fractional_Fixed_Ops_On_Target then
5497 return False;
5498 end if;
5500 -- Here the target has Fractional_Fixed_Ops, if first time, compute
5501 -- standard constants used by Is_Fractional_Type.
5503 if First_Time_For_THFO then
5504 First_Time_For_THFO := False;
5506 Integer_Sized_Small :=
5507 UR_From_Components
5508 (Num => Uint_1,
5509 Den => UI_From_Int (Standard_Integer_Size - 1),
5510 Rbase => 2);
5512 Long_Integer_Sized_Small :=
5513 UR_From_Components
5514 (Num => Uint_1,
5515 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
5516 Rbase => 2);
5517 end if;
5519 -- Return True if target supports fixed-by-fixed multiply/divide
5520 -- for fractional fixed-point types (see Is_Fractional_Type) and
5521 -- the operand and result types are equivalent fractional types.
5523 return Is_Fractional_Type (Base_Type (Left_Typ))
5524 and then Is_Fractional_Type (Base_Type (Right_Typ))
5525 and then Is_Fractional_Type (Base_Type (Result_Typ))
5526 and then Esize (Left_Typ) = Esize (Right_Typ)
5527 and then Esize (Left_Typ) = Esize (Result_Typ);
5528 end Target_Has_Fixed_Ops;
5530 ------------------------------------------
5531 -- Type_May_Have_Bit_Aligned_Components --
5532 ------------------------------------------
5534 function Type_May_Have_Bit_Aligned_Components
5535 (Typ : Entity_Id) return Boolean
5537 begin
5538 -- Array type, check component type
5540 if Is_Array_Type (Typ) then
5541 return
5542 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
5544 -- Record type, check components
5546 elsif Is_Record_Type (Typ) then
5547 declare
5548 E : Entity_Id;
5550 begin
5551 E := First_Component_Or_Discriminant (Typ);
5552 while Present (E) loop
5553 if Component_May_Be_Bit_Aligned (E)
5554 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
5555 then
5556 return True;
5557 end if;
5559 Next_Component_Or_Discriminant (E);
5560 end loop;
5562 return False;
5563 end;
5565 -- Type other than array or record is always OK
5567 else
5568 return False;
5569 end if;
5570 end Type_May_Have_Bit_Aligned_Components;
5572 ----------------------------
5573 -- Wrap_Cleanup_Procedure --
5574 ----------------------------
5576 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
5577 Loc : constant Source_Ptr := Sloc (N);
5578 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
5579 Stmts : constant List_Id := Statements (Stseq);
5581 begin
5582 if Abort_Allowed then
5583 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5584 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
5585 end if;
5586 end Wrap_Cleanup_Procedure;
5588 end Exp_Util;