2009-10-01 Tobias Burnus <burnus@net-b.de>
[official-gcc/alias-decl.git] / gcc / ada / exp_util.adb
blobfcf3878f61df27d4321c9a1720ec4db198c03c0f
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-2009, 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_SCIL; use Sem_SCIL;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Res; use Sem_Res;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Snames; use Snames;
52 with Stand; use Stand;
53 with Stringt; use Stringt;
54 with Targparm; use Targparm;
55 with Tbuild; use Tbuild;
56 with Ttypes; use Ttypes;
57 with Uintp; use Uintp;
58 with Urealp; use Urealp;
59 with Validsw; use Validsw;
61 package body Exp_Util is
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Build_Task_Array_Image
68 (Loc : Source_Ptr;
69 Id_Ref : Node_Id;
70 A_Type : Entity_Id;
71 Dyn : Boolean := False) return Node_Id;
72 -- Build function to generate the image string for a task that is an
73 -- array component, concatenating the images of each index. To avoid
74 -- storage leaks, the string is built with successive slice assignments.
75 -- The flag Dyn indicates whether this is called for the initialization
76 -- procedure of an array of tasks, or for the name of a dynamically
77 -- created task that is assigned to an indexed component.
79 function Build_Task_Image_Function
80 (Loc : Source_Ptr;
81 Decls : List_Id;
82 Stats : List_Id;
83 Res : Entity_Id) return Node_Id;
84 -- Common processing for Task_Array_Image and Task_Record_Image.
85 -- Build function body that computes image.
87 procedure Build_Task_Image_Prefix
88 (Loc : Source_Ptr;
89 Len : out Entity_Id;
90 Res : out Entity_Id;
91 Pos : out Entity_Id;
92 Prefix : Entity_Id;
93 Sum : Node_Id;
94 Decls : List_Id;
95 Stats : List_Id);
96 -- Common processing for Task_Array_Image and Task_Record_Image.
97 -- Create local variables and assign prefix of name to result string.
99 function Build_Task_Record_Image
100 (Loc : Source_Ptr;
101 Id_Ref : Node_Id;
102 Dyn : Boolean := False) return Node_Id;
103 -- Build function to generate the image string for a task that is a
104 -- record component. Concatenate name of variable with that of selector.
105 -- The flag Dyn indicates whether this is called for the initialization
106 -- procedure of record with task components, or for a dynamically
107 -- created task that is assigned to a selected component.
109 function Make_CW_Equivalent_Type
110 (T : Entity_Id;
111 E : Node_Id) return Entity_Id;
112 -- T is a class-wide type entity, E is the initial expression node that
113 -- constrains T in case such as: " X: T := E" or "new T'(E)"
114 -- This function returns the entity of the Equivalent type and inserts
115 -- on the fly the necessary declaration such as:
117 -- type anon is record
118 -- _parent : Root_Type (T); constrained with E discriminants (if any)
119 -- Extension : String (1 .. expr to match size of E);
120 -- end record;
122 -- This record is compatible with any object of the class of T thanks
123 -- to the first field and has the same size as E thanks to the second.
125 function Make_Literal_Range
126 (Loc : Source_Ptr;
127 Literal_Typ : Entity_Id) return Node_Id;
128 -- Produce a Range node whose bounds are:
129 -- Low_Bound (Literal_Type) ..
130 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
131 -- this is used for expanding declarations like X : String := "sdfgdfg";
133 -- If the index type of the target array is not integer, we generate:
134 -- Low_Bound (Literal_Type) ..
135 -- Literal_Type'Val
136 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
137 -- + (Length (Literal_Typ) -1))
139 function Make_Non_Empty_Check
140 (Loc : Source_Ptr;
141 N : Node_Id) return Node_Id;
142 -- Produce a boolean expression checking that the unidimensional array
143 -- node N is not empty.
145 function New_Class_Wide_Subtype
146 (CW_Typ : Entity_Id;
147 N : Node_Id) return Entity_Id;
148 -- Create an implicit subtype of CW_Typ attached to node N
150 ----------------------
151 -- Adjust_Condition --
152 ----------------------
154 procedure Adjust_Condition (N : Node_Id) is
155 begin
156 if No (N) then
157 return;
158 end if;
160 declare
161 Loc : constant Source_Ptr := Sloc (N);
162 T : constant Entity_Id := Etype (N);
163 Ti : Entity_Id;
165 begin
166 -- For now, we simply ignore a call where the argument has no
167 -- type (probably case of unanalyzed condition), or has a type
168 -- that is not Boolean. This is because this is a pretty marginal
169 -- piece of functionality, and violations of these rules are
170 -- likely to be truly marginal (how much code uses Fortran Logical
171 -- as the barrier to a protected entry?) and we do not want to
172 -- blow up existing programs. We can change this to an assertion
173 -- after 3.12a is released ???
175 if No (T) or else not Is_Boolean_Type (T) then
176 return;
177 end if;
179 -- Apply validity checking if needed
181 if Validity_Checks_On and Validity_Check_Tests then
182 Ensure_Valid (N);
183 end if;
185 -- Immediate return if standard boolean, the most common case,
186 -- where nothing needs to be done.
188 if Base_Type (T) = Standard_Boolean then
189 return;
190 end if;
192 -- Case of zero/non-zero semantics or non-standard enumeration
193 -- representation. In each case, we rewrite the node as:
195 -- ityp!(N) /= False'Enum_Rep
197 -- where ityp is an integer type with large enough size to hold
198 -- any value of type T.
200 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
201 if Esize (T) <= Esize (Standard_Integer) then
202 Ti := Standard_Integer;
203 else
204 Ti := Standard_Long_Long_Integer;
205 end if;
207 Rewrite (N,
208 Make_Op_Ne (Loc,
209 Left_Opnd => Unchecked_Convert_To (Ti, N),
210 Right_Opnd =>
211 Make_Attribute_Reference (Loc,
212 Attribute_Name => Name_Enum_Rep,
213 Prefix =>
214 New_Occurrence_Of (First_Literal (T), Loc))));
215 Analyze_And_Resolve (N, Standard_Boolean);
217 else
218 Rewrite (N, Convert_To (Standard_Boolean, N));
219 Analyze_And_Resolve (N, Standard_Boolean);
220 end if;
221 end;
222 end Adjust_Condition;
224 ------------------------
225 -- Adjust_Result_Type --
226 ------------------------
228 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
229 begin
230 -- Ignore call if current type is not Standard.Boolean
232 if Etype (N) /= Standard_Boolean then
233 return;
234 end if;
236 -- If result is already of correct type, nothing to do. Note that
237 -- this will get the most common case where everything has a type
238 -- of Standard.Boolean.
240 if Base_Type (T) = Standard_Boolean then
241 return;
243 else
244 declare
245 KP : constant Node_Kind := Nkind (Parent (N));
247 begin
248 -- If result is to be used as a Condition in the syntax, no need
249 -- to convert it back, since if it was changed to Standard.Boolean
250 -- using Adjust_Condition, that is just fine for this usage.
252 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
253 return;
255 -- If result is an operand of another logical operation, no need
256 -- to reset its type, since Standard.Boolean is just fine, and
257 -- such operations always do Adjust_Condition on their operands.
259 elsif KP in N_Op_Boolean
260 or else KP in N_Short_Circuit
261 or else KP = N_Op_Not
262 then
263 return;
265 -- Otherwise we perform a conversion from the current type,
266 -- which must be Standard.Boolean, to the desired type.
268 else
269 Set_Analyzed (N);
270 Rewrite (N, Convert_To (T, N));
271 Analyze_And_Resolve (N, T);
272 end if;
273 end;
274 end if;
275 end Adjust_Result_Type;
277 --------------------------
278 -- Append_Freeze_Action --
279 --------------------------
281 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
282 Fnode : Node_Id;
284 begin
285 Ensure_Freeze_Node (T);
286 Fnode := Freeze_Node (T);
288 if No (Actions (Fnode)) then
289 Set_Actions (Fnode, New_List);
290 end if;
292 Append (N, Actions (Fnode));
293 end Append_Freeze_Action;
295 ---------------------------
296 -- Append_Freeze_Actions --
297 ---------------------------
299 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
300 Fnode : constant Node_Id := Freeze_Node (T);
302 begin
303 if No (L) then
304 return;
306 else
307 if No (Actions (Fnode)) then
308 Set_Actions (Fnode, L);
310 else
311 Append_List (L, Actions (Fnode));
312 end if;
314 end if;
315 end Append_Freeze_Actions;
317 ------------------------
318 -- Build_Runtime_Call --
319 ------------------------
321 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
322 begin
323 -- If entity is not available, we can skip making the call (this avoids
324 -- junk duplicated error messages in a number of cases).
326 if not RTE_Available (RE) then
327 return Make_Null_Statement (Loc);
328 else
329 return
330 Make_Procedure_Call_Statement (Loc,
331 Name => New_Reference_To (RTE (RE), Loc));
332 end if;
333 end Build_Runtime_Call;
335 ----------------------------
336 -- Build_Task_Array_Image --
337 ----------------------------
339 -- This function generates the body for a function that constructs the
340 -- image string for a task that is an array component. The function is
341 -- local to the init proc for the array type, and is called for each one
342 -- of the components. The constructed image has the form of an indexed
343 -- component, whose prefix is the outer variable of the array type.
344 -- The n-dimensional array type has known indices Index, Index2...
345 -- Id_Ref is an indexed component form created by the enclosing init proc.
346 -- Its successive indices are Val1, Val2, ... which are the loop variables
347 -- in the loops that call the individual task init proc on each component.
349 -- The generated function has the following structure:
351 -- function F return String is
352 -- Pref : string renames Task_Name;
353 -- T1 : String := Index1'Image (Val1);
354 -- ...
355 -- Tn : String := indexn'image (Valn);
356 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
357 -- -- Len includes commas and the end parentheses.
358 -- Res : String (1..Len);
359 -- Pos : Integer := Pref'Length;
361 -- begin
362 -- Res (1 .. Pos) := Pref;
363 -- Pos := Pos + 1;
364 -- Res (Pos) := '(';
365 -- Pos := Pos + 1;
366 -- Res (Pos .. Pos + T1'Length - 1) := T1;
367 -- Pos := Pos + T1'Length;
368 -- Res (Pos) := '.';
369 -- Pos := Pos + 1;
370 -- ...
371 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
372 -- Res (Len) := ')';
374 -- return Res;
375 -- end F;
377 -- Needless to say, multidimensional arrays of tasks are rare enough
378 -- that the bulkiness of this code is not really a concern.
380 function Build_Task_Array_Image
381 (Loc : Source_Ptr;
382 Id_Ref : Node_Id;
383 A_Type : Entity_Id;
384 Dyn : Boolean := False) return Node_Id
386 Dims : constant Nat := Number_Dimensions (A_Type);
387 -- Number of dimensions for array of tasks
389 Temps : array (1 .. Dims) of Entity_Id;
390 -- Array of temporaries to hold string for each index
392 Indx : Node_Id;
393 -- Index expression
395 Len : Entity_Id;
396 -- Total length of generated name
398 Pos : Entity_Id;
399 -- Running index for substring assignments
401 Pref : Entity_Id;
402 -- Name of enclosing variable, prefix of resulting name
404 Res : Entity_Id;
405 -- String to hold result
407 Val : Node_Id;
408 -- Value of successive indices
410 Sum : Node_Id;
411 -- Expression to compute total size of string
413 T : Entity_Id;
414 -- Entity for name at one index position
416 Decls : constant List_Id := New_List;
417 Stats : constant List_Id := New_List;
419 begin
420 Pref := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
422 -- For a dynamic task, the name comes from the target variable.
423 -- For a static one it is a formal of the enclosing init proc.
425 if Dyn then
426 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
427 Append_To (Decls,
428 Make_Object_Declaration (Loc,
429 Defining_Identifier => Pref,
430 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
431 Expression =>
432 Make_String_Literal (Loc,
433 Strval => String_From_Name_Buffer)));
435 else
436 Append_To (Decls,
437 Make_Object_Renaming_Declaration (Loc,
438 Defining_Identifier => Pref,
439 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
440 Name => Make_Identifier (Loc, Name_uTask_Name)));
441 end if;
443 Indx := First_Index (A_Type);
444 Val := First (Expressions (Id_Ref));
446 for J in 1 .. Dims loop
447 T := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
448 Temps (J) := T;
450 Append_To (Decls,
451 Make_Object_Declaration (Loc,
452 Defining_Identifier => T,
453 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
454 Expression =>
455 Make_Attribute_Reference (Loc,
456 Attribute_Name => Name_Image,
457 Prefix =>
458 New_Occurrence_Of (Etype (Indx), Loc),
459 Expressions => New_List (
460 New_Copy_Tree (Val)))));
462 Next_Index (Indx);
463 Next (Val);
464 end loop;
466 Sum := Make_Integer_Literal (Loc, Dims + 1);
468 Sum :=
469 Make_Op_Add (Loc,
470 Left_Opnd => Sum,
471 Right_Opnd =>
472 Make_Attribute_Reference (Loc,
473 Attribute_Name => Name_Length,
474 Prefix =>
475 New_Occurrence_Of (Pref, Loc),
476 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
478 for J in 1 .. Dims loop
479 Sum :=
480 Make_Op_Add (Loc,
481 Left_Opnd => Sum,
482 Right_Opnd =>
483 Make_Attribute_Reference (Loc,
484 Attribute_Name => Name_Length,
485 Prefix =>
486 New_Occurrence_Of (Temps (J), Loc),
487 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
488 end loop;
490 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
492 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
494 Append_To (Stats,
495 Make_Assignment_Statement (Loc,
496 Name => Make_Indexed_Component (Loc,
497 Prefix => New_Occurrence_Of (Res, Loc),
498 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
499 Expression =>
500 Make_Character_Literal (Loc,
501 Chars => Name_Find,
502 Char_Literal_Value =>
503 UI_From_Int (Character'Pos ('(')))));
505 Append_To (Stats,
506 Make_Assignment_Statement (Loc,
507 Name => New_Occurrence_Of (Pos, Loc),
508 Expression =>
509 Make_Op_Add (Loc,
510 Left_Opnd => New_Occurrence_Of (Pos, Loc),
511 Right_Opnd => Make_Integer_Literal (Loc, 1))));
513 for J in 1 .. Dims loop
515 Append_To (Stats,
516 Make_Assignment_Statement (Loc,
517 Name => Make_Slice (Loc,
518 Prefix => New_Occurrence_Of (Res, Loc),
519 Discrete_Range =>
520 Make_Range (Loc,
521 Low_Bound => New_Occurrence_Of (Pos, Loc),
522 High_Bound => Make_Op_Subtract (Loc,
523 Left_Opnd =>
524 Make_Op_Add (Loc,
525 Left_Opnd => New_Occurrence_Of (Pos, Loc),
526 Right_Opnd =>
527 Make_Attribute_Reference (Loc,
528 Attribute_Name => Name_Length,
529 Prefix =>
530 New_Occurrence_Of (Temps (J), Loc),
531 Expressions =>
532 New_List (Make_Integer_Literal (Loc, 1)))),
533 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
535 Expression => New_Occurrence_Of (Temps (J), Loc)));
537 if J < Dims then
538 Append_To (Stats,
539 Make_Assignment_Statement (Loc,
540 Name => New_Occurrence_Of (Pos, Loc),
541 Expression =>
542 Make_Op_Add (Loc,
543 Left_Opnd => New_Occurrence_Of (Pos, Loc),
544 Right_Opnd =>
545 Make_Attribute_Reference (Loc,
546 Attribute_Name => Name_Length,
547 Prefix => New_Occurrence_Of (Temps (J), Loc),
548 Expressions =>
549 New_List (Make_Integer_Literal (Loc, 1))))));
551 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
553 Append_To (Stats,
554 Make_Assignment_Statement (Loc,
555 Name => Make_Indexed_Component (Loc,
556 Prefix => New_Occurrence_Of (Res, Loc),
557 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
558 Expression =>
559 Make_Character_Literal (Loc,
560 Chars => Name_Find,
561 Char_Literal_Value =>
562 UI_From_Int (Character'Pos (',')))));
564 Append_To (Stats,
565 Make_Assignment_Statement (Loc,
566 Name => New_Occurrence_Of (Pos, Loc),
567 Expression =>
568 Make_Op_Add (Loc,
569 Left_Opnd => New_Occurrence_Of (Pos, Loc),
570 Right_Opnd => Make_Integer_Literal (Loc, 1))));
571 end if;
572 end loop;
574 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
576 Append_To (Stats,
577 Make_Assignment_Statement (Loc,
578 Name => Make_Indexed_Component (Loc,
579 Prefix => New_Occurrence_Of (Res, Loc),
580 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
581 Expression =>
582 Make_Character_Literal (Loc,
583 Chars => Name_Find,
584 Char_Literal_Value =>
585 UI_From_Int (Character'Pos (')')))));
586 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
587 end Build_Task_Array_Image;
589 ----------------------------
590 -- Build_Task_Image_Decls --
591 ----------------------------
593 function Build_Task_Image_Decls
594 (Loc : Source_Ptr;
595 Id_Ref : Node_Id;
596 A_Type : Entity_Id;
597 In_Init_Proc : Boolean := False) return List_Id
599 Decls : constant List_Id := New_List;
600 T_Id : Entity_Id := Empty;
601 Decl : Node_Id;
602 Expr : Node_Id := Empty;
603 Fun : Node_Id := Empty;
604 Is_Dyn : constant Boolean :=
605 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
606 and then
607 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
609 begin
610 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
611 -- generate a dummy declaration only.
613 if Restriction_Active (No_Implicit_Heap_Allocations)
614 or else Global_Discard_Names
615 then
616 T_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
617 Name_Len := 0;
619 return
620 New_List (
621 Make_Object_Declaration (Loc,
622 Defining_Identifier => T_Id,
623 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
624 Expression =>
625 Make_String_Literal (Loc,
626 Strval => String_From_Name_Buffer)));
628 else
629 if Nkind (Id_Ref) = N_Identifier
630 or else Nkind (Id_Ref) = N_Defining_Identifier
631 then
632 -- For a simple variable, the image of the task is built from
633 -- the name of the variable. To avoid possible conflict with
634 -- the anonymous type created for a single protected object,
635 -- add a numeric suffix.
637 T_Id :=
638 Make_Defining_Identifier (Loc,
639 New_External_Name (Chars (Id_Ref), 'T', 1));
641 Get_Name_String (Chars (Id_Ref));
643 Expr :=
644 Make_String_Literal (Loc,
645 Strval => String_From_Name_Buffer);
647 elsif Nkind (Id_Ref) = N_Selected_Component then
648 T_Id :=
649 Make_Defining_Identifier (Loc,
650 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
651 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
653 elsif Nkind (Id_Ref) = N_Indexed_Component then
654 T_Id :=
655 Make_Defining_Identifier (Loc,
656 New_External_Name (Chars (A_Type), 'N'));
658 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
659 end if;
660 end if;
662 if Present (Fun) then
663 Append (Fun, Decls);
664 Expr := Make_Function_Call (Loc,
665 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
667 if not In_Init_Proc and then VM_Target = No_VM then
668 Set_Uses_Sec_Stack (Defining_Entity (Fun));
669 end if;
670 end if;
672 Decl := Make_Object_Declaration (Loc,
673 Defining_Identifier => T_Id,
674 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
675 Constant_Present => True,
676 Expression => Expr);
678 Append (Decl, Decls);
679 return Decls;
680 end Build_Task_Image_Decls;
682 -------------------------------
683 -- Build_Task_Image_Function --
684 -------------------------------
686 function Build_Task_Image_Function
687 (Loc : Source_Ptr;
688 Decls : List_Id;
689 Stats : List_Id;
690 Res : Entity_Id) return Node_Id
692 Spec : Node_Id;
694 begin
695 Append_To (Stats,
696 Make_Simple_Return_Statement (Loc,
697 Expression => New_Occurrence_Of (Res, Loc)));
699 Spec := Make_Function_Specification (Loc,
700 Defining_Unit_Name =>
701 Make_Defining_Identifier (Loc, New_Internal_Name ('F')),
702 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
704 -- Calls to 'Image use the secondary stack, which must be cleaned
705 -- up after the task name is built.
707 return Make_Subprogram_Body (Loc,
708 Specification => Spec,
709 Declarations => Decls,
710 Handled_Statement_Sequence =>
711 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
712 end Build_Task_Image_Function;
714 -----------------------------
715 -- Build_Task_Image_Prefix --
716 -----------------------------
718 procedure Build_Task_Image_Prefix
719 (Loc : Source_Ptr;
720 Len : out Entity_Id;
721 Res : out Entity_Id;
722 Pos : out Entity_Id;
723 Prefix : Entity_Id;
724 Sum : Node_Id;
725 Decls : List_Id;
726 Stats : List_Id)
728 begin
729 Len := Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
731 Append_To (Decls,
732 Make_Object_Declaration (Loc,
733 Defining_Identifier => Len,
734 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
735 Expression => Sum));
737 Res := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
739 Append_To (Decls,
740 Make_Object_Declaration (Loc,
741 Defining_Identifier => Res,
742 Object_Definition =>
743 Make_Subtype_Indication (Loc,
744 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
745 Constraint =>
746 Make_Index_Or_Discriminant_Constraint (Loc,
747 Constraints =>
748 New_List (
749 Make_Range (Loc,
750 Low_Bound => Make_Integer_Literal (Loc, 1),
751 High_Bound => New_Occurrence_Of (Len, Loc)))))));
753 Pos := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
755 Append_To (Decls,
756 Make_Object_Declaration (Loc,
757 Defining_Identifier => Pos,
758 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
760 -- Pos := Prefix'Length;
762 Append_To (Stats,
763 Make_Assignment_Statement (Loc,
764 Name => New_Occurrence_Of (Pos, Loc),
765 Expression =>
766 Make_Attribute_Reference (Loc,
767 Attribute_Name => Name_Length,
768 Prefix => New_Occurrence_Of (Prefix, Loc),
769 Expressions =>
770 New_List (Make_Integer_Literal (Loc, 1)))));
772 -- Res (1 .. Pos) := Prefix;
774 Append_To (Stats,
775 Make_Assignment_Statement (Loc,
776 Name => Make_Slice (Loc,
777 Prefix => New_Occurrence_Of (Res, Loc),
778 Discrete_Range =>
779 Make_Range (Loc,
780 Low_Bound => Make_Integer_Literal (Loc, 1),
781 High_Bound => New_Occurrence_Of (Pos, Loc))),
783 Expression => New_Occurrence_Of (Prefix, Loc)));
785 Append_To (Stats,
786 Make_Assignment_Statement (Loc,
787 Name => New_Occurrence_Of (Pos, Loc),
788 Expression =>
789 Make_Op_Add (Loc,
790 Left_Opnd => New_Occurrence_Of (Pos, Loc),
791 Right_Opnd => Make_Integer_Literal (Loc, 1))));
792 end Build_Task_Image_Prefix;
794 -----------------------------
795 -- Build_Task_Record_Image --
796 -----------------------------
798 function Build_Task_Record_Image
799 (Loc : Source_Ptr;
800 Id_Ref : Node_Id;
801 Dyn : Boolean := False) return Node_Id
803 Len : Entity_Id;
804 -- Total length of generated name
806 Pos : Entity_Id;
807 -- Index into result
809 Res : Entity_Id;
810 -- String to hold result
812 Pref : Entity_Id;
813 -- Name of enclosing variable, prefix of resulting name
815 Sum : Node_Id;
816 -- Expression to compute total size of string
818 Sel : Entity_Id;
819 -- Entity for selector name
821 Decls : constant List_Id := New_List;
822 Stats : constant List_Id := New_List;
824 begin
825 Pref := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
827 -- For a dynamic task, the name comes from the target variable.
828 -- For a static one it is a formal of the enclosing init proc.
830 if Dyn then
831 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
832 Append_To (Decls,
833 Make_Object_Declaration (Loc,
834 Defining_Identifier => Pref,
835 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
836 Expression =>
837 Make_String_Literal (Loc,
838 Strval => String_From_Name_Buffer)));
840 else
841 Append_To (Decls,
842 Make_Object_Renaming_Declaration (Loc,
843 Defining_Identifier => Pref,
844 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
845 Name => Make_Identifier (Loc, Name_uTask_Name)));
846 end if;
848 Sel := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
850 Get_Name_String (Chars (Selector_Name (Id_Ref)));
852 Append_To (Decls,
853 Make_Object_Declaration (Loc,
854 Defining_Identifier => Sel,
855 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
856 Expression =>
857 Make_String_Literal (Loc,
858 Strval => String_From_Name_Buffer)));
860 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
862 Sum :=
863 Make_Op_Add (Loc,
864 Left_Opnd => Sum,
865 Right_Opnd =>
866 Make_Attribute_Reference (Loc,
867 Attribute_Name => Name_Length,
868 Prefix =>
869 New_Occurrence_Of (Pref, Loc),
870 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
872 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
874 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
876 -- Res (Pos) := '.';
878 Append_To (Stats,
879 Make_Assignment_Statement (Loc,
880 Name => Make_Indexed_Component (Loc,
881 Prefix => New_Occurrence_Of (Res, Loc),
882 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
883 Expression =>
884 Make_Character_Literal (Loc,
885 Chars => Name_Find,
886 Char_Literal_Value =>
887 UI_From_Int (Character'Pos ('.')))));
889 Append_To (Stats,
890 Make_Assignment_Statement (Loc,
891 Name => New_Occurrence_Of (Pos, Loc),
892 Expression =>
893 Make_Op_Add (Loc,
894 Left_Opnd => New_Occurrence_Of (Pos, Loc),
895 Right_Opnd => Make_Integer_Literal (Loc, 1))));
897 -- Res (Pos .. Len) := Selector;
899 Append_To (Stats,
900 Make_Assignment_Statement (Loc,
901 Name => Make_Slice (Loc,
902 Prefix => New_Occurrence_Of (Res, Loc),
903 Discrete_Range =>
904 Make_Range (Loc,
905 Low_Bound => New_Occurrence_Of (Pos, Loc),
906 High_Bound => New_Occurrence_Of (Len, Loc))),
907 Expression => New_Occurrence_Of (Sel, Loc)));
909 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
910 end Build_Task_Record_Image;
912 ----------------------------------
913 -- Component_May_Be_Bit_Aligned --
914 ----------------------------------
916 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
917 UT : constant Entity_Id := Underlying_Type (Etype (Comp));
919 begin
920 -- If no component clause, then everything is fine, since the back end
921 -- never bit-misaligns by default, even if there is a pragma Packed for
922 -- the record.
924 if No (Component_Clause (Comp)) then
925 return False;
926 end if;
928 -- It is only array and record types that cause trouble
930 if not Is_Record_Type (UT)
931 and then not Is_Array_Type (UT)
932 then
933 return False;
935 -- If we know that we have a small (64 bits or less) record or small
936 -- bit-packed array, then everything is fine, since the back end can
937 -- handle these cases correctly.
939 elsif Esize (Comp) <= 64
940 and then (Is_Record_Type (UT)
941 or else Is_Bit_Packed_Array (UT))
942 then
943 return False;
945 -- Otherwise if the component is not byte aligned, we know we have the
946 -- nasty unaligned case.
948 elsif Normalized_First_Bit (Comp) /= Uint_0
949 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
950 then
951 return True;
953 -- If we are large and byte aligned, then OK at this level
955 else
956 return False;
957 end if;
958 end Component_May_Be_Bit_Aligned;
960 -----------------------------------
961 -- Corresponding_Runtime_Package --
962 -----------------------------------
964 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
965 Pkg_Id : RTU_Id := RTU_Null;
967 begin
968 pragma Assert (Is_Concurrent_Type (Typ));
970 if Ekind (Typ) in Protected_Kind then
971 if Has_Entries (Typ)
972 or else Has_Interrupt_Handler (Typ)
973 or else (Has_Attach_Handler (Typ)
974 and then not Restricted_Profile)
976 -- A protected type without entries that covers an interface and
977 -- overrides the abstract routines with protected procedures is
978 -- considered equivalent to a protected type with entries in the
979 -- context of dispatching select statements. It is sufficient to
980 -- check for the presence of an interface list in the declaration
981 -- node to recognize this case.
983 or else Present (Interface_List (Parent (Typ)))
984 then
985 if Abort_Allowed
986 or else Restriction_Active (No_Entry_Queue) = False
987 or else Number_Entries (Typ) > 1
988 or else (Has_Attach_Handler (Typ)
989 and then not Restricted_Profile)
990 then
991 Pkg_Id := System_Tasking_Protected_Objects_Entries;
992 else
993 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
994 end if;
996 else
997 Pkg_Id := System_Tasking_Protected_Objects;
998 end if;
999 end if;
1001 return Pkg_Id;
1002 end Corresponding_Runtime_Package;
1004 -------------------------------
1005 -- Convert_To_Actual_Subtype --
1006 -------------------------------
1008 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1009 Act_ST : Entity_Id;
1011 begin
1012 Act_ST := Get_Actual_Subtype (Exp);
1014 if Act_ST = Etype (Exp) then
1015 return;
1017 else
1018 Rewrite (Exp,
1019 Convert_To (Act_ST, Relocate_Node (Exp)));
1020 Analyze_And_Resolve (Exp, Act_ST);
1021 end if;
1022 end Convert_To_Actual_Subtype;
1024 -----------------------------------
1025 -- Current_Sem_Unit_Declarations --
1026 -----------------------------------
1028 function Current_Sem_Unit_Declarations return List_Id is
1029 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1030 Decls : List_Id;
1032 begin
1033 -- If the current unit is a package body, locate the visible
1034 -- declarations of the package spec.
1036 if Nkind (U) = N_Package_Body then
1037 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1038 end if;
1040 if Nkind (U) = N_Package_Declaration then
1041 U := Specification (U);
1042 Decls := Visible_Declarations (U);
1044 if No (Decls) then
1045 Decls := New_List;
1046 Set_Visible_Declarations (U, Decls);
1047 end if;
1049 else
1050 Decls := Declarations (U);
1052 if No (Decls) then
1053 Decls := New_List;
1054 Set_Declarations (U, Decls);
1055 end if;
1056 end if;
1058 return Decls;
1059 end Current_Sem_Unit_Declarations;
1061 -----------------------
1062 -- Duplicate_Subexpr --
1063 -----------------------
1065 function Duplicate_Subexpr
1066 (Exp : Node_Id;
1067 Name_Req : Boolean := False) return Node_Id
1069 begin
1070 Remove_Side_Effects (Exp, Name_Req);
1071 return New_Copy_Tree (Exp);
1072 end Duplicate_Subexpr;
1074 ---------------------------------
1075 -- Duplicate_Subexpr_No_Checks --
1076 ---------------------------------
1078 function Duplicate_Subexpr_No_Checks
1079 (Exp : Node_Id;
1080 Name_Req : Boolean := False) return Node_Id
1082 New_Exp : Node_Id;
1084 begin
1085 Remove_Side_Effects (Exp, Name_Req);
1086 New_Exp := New_Copy_Tree (Exp);
1087 Remove_Checks (New_Exp);
1088 return New_Exp;
1089 end Duplicate_Subexpr_No_Checks;
1091 -----------------------------------
1092 -- Duplicate_Subexpr_Move_Checks --
1093 -----------------------------------
1095 function Duplicate_Subexpr_Move_Checks
1096 (Exp : Node_Id;
1097 Name_Req : Boolean := False) return Node_Id
1099 New_Exp : Node_Id;
1101 begin
1102 Remove_Side_Effects (Exp, Name_Req);
1103 New_Exp := New_Copy_Tree (Exp);
1104 Remove_Checks (Exp);
1105 return New_Exp;
1106 end Duplicate_Subexpr_Move_Checks;
1108 --------------------
1109 -- Ensure_Defined --
1110 --------------------
1112 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1113 IR : Node_Id;
1115 begin
1116 -- An itype reference must only be created if this is a local
1117 -- itype, so that gigi can elaborate it on the proper objstack.
1119 if Is_Itype (Typ)
1120 and then Scope (Typ) = Current_Scope
1121 then
1122 IR := Make_Itype_Reference (Sloc (N));
1123 Set_Itype (IR, Typ);
1124 Insert_Action (N, IR);
1125 end if;
1126 end Ensure_Defined;
1128 --------------------
1129 -- Entry_Names_OK --
1130 --------------------
1132 function Entry_Names_OK return Boolean is
1133 begin
1134 return
1135 not Restricted_Profile
1136 and then not Global_Discard_Names
1137 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1138 and then not Restriction_Active (No_Local_Allocators);
1139 end Entry_Names_OK;
1141 ---------------------
1142 -- Evolve_And_Then --
1143 ---------------------
1145 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1146 begin
1147 if No (Cond) then
1148 Cond := Cond1;
1149 else
1150 Cond :=
1151 Make_And_Then (Sloc (Cond1),
1152 Left_Opnd => Cond,
1153 Right_Opnd => Cond1);
1154 end if;
1155 end Evolve_And_Then;
1157 --------------------
1158 -- Evolve_Or_Else --
1159 --------------------
1161 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1162 begin
1163 if No (Cond) then
1164 Cond := Cond1;
1165 else
1166 Cond :=
1167 Make_Or_Else (Sloc (Cond1),
1168 Left_Opnd => Cond,
1169 Right_Opnd => Cond1);
1170 end if;
1171 end Evolve_Or_Else;
1173 ------------------------------
1174 -- Expand_Subtype_From_Expr --
1175 ------------------------------
1177 -- This function is applicable for both static and dynamic allocation of
1178 -- objects which are constrained by an initial expression. Basically it
1179 -- transforms an unconstrained subtype indication into a constrained one.
1180 -- The expression may also be transformed in certain cases in order to
1181 -- avoid multiple evaluation. In the static allocation case, the general
1182 -- scheme is:
1184 -- Val : T := Expr;
1186 -- is transformed into
1188 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1190 -- Here are the main cases :
1192 -- <if Expr is a Slice>
1193 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1195 -- <elsif Expr is a String Literal>
1196 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1198 -- <elsif Expr is Constrained>
1199 -- subtype T is Type_Of_Expr
1200 -- Val : T := Expr;
1202 -- <elsif Expr is an entity_name>
1203 -- Val : T (constraints taken from Expr) := Expr;
1205 -- <else>
1206 -- type Axxx is access all T;
1207 -- Rval : Axxx := Expr'ref;
1208 -- Val : T (constraints taken from Rval) := Rval.all;
1210 -- ??? note: when the Expression is allocated in the secondary stack
1211 -- we could use it directly instead of copying it by declaring
1212 -- Val : T (...) renames Rval.all
1214 procedure Expand_Subtype_From_Expr
1215 (N : Node_Id;
1216 Unc_Type : Entity_Id;
1217 Subtype_Indic : Node_Id;
1218 Exp : Node_Id)
1220 Loc : constant Source_Ptr := Sloc (N);
1221 Exp_Typ : constant Entity_Id := Etype (Exp);
1222 T : Entity_Id;
1224 begin
1225 -- In general we cannot build the subtype if expansion is disabled,
1226 -- because internal entities may not have been defined. However, to
1227 -- avoid some cascaded errors, we try to continue when the expression
1228 -- is an array (or string), because it is safe to compute the bounds.
1229 -- It is in fact required to do so even in a generic context, because
1230 -- there may be constants that depend on bounds of string literal.
1232 if not Expander_Active
1233 and then (No (Etype (Exp))
1234 or else Base_Type (Etype (Exp)) /= Standard_String)
1235 then
1236 return;
1237 end if;
1239 if Nkind (Exp) = N_Slice then
1240 declare
1241 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1243 begin
1244 Rewrite (Subtype_Indic,
1245 Make_Subtype_Indication (Loc,
1246 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1247 Constraint =>
1248 Make_Index_Or_Discriminant_Constraint (Loc,
1249 Constraints => New_List
1250 (New_Reference_To (Slice_Type, Loc)))));
1252 -- This subtype indication may be used later for constraint checks
1253 -- we better make sure that if a variable was used as a bound of
1254 -- of the original slice, its value is frozen.
1256 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
1257 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
1258 end;
1260 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
1261 Rewrite (Subtype_Indic,
1262 Make_Subtype_Indication (Loc,
1263 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1264 Constraint =>
1265 Make_Index_Or_Discriminant_Constraint (Loc,
1266 Constraints => New_List (
1267 Make_Literal_Range (Loc,
1268 Literal_Typ => Exp_Typ)))));
1270 elsif Is_Constrained (Exp_Typ)
1271 and then not Is_Class_Wide_Type (Unc_Type)
1272 then
1273 if Is_Itype (Exp_Typ) then
1275 -- Within an initialization procedure, a selected component
1276 -- denotes a component of the enclosing record, and it appears
1277 -- as an actual in a call to its own initialization procedure.
1278 -- If this component depends on the outer discriminant, we must
1279 -- generate the proper actual subtype for it.
1281 if Nkind (Exp) = N_Selected_Component
1282 and then Within_Init_Proc
1283 then
1284 declare
1285 Decl : constant Node_Id :=
1286 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
1287 begin
1288 if Present (Decl) then
1289 Insert_Action (N, Decl);
1290 T := Defining_Identifier (Decl);
1291 else
1292 T := Exp_Typ;
1293 end if;
1294 end;
1296 -- No need to generate a new one (new what???)
1298 else
1299 T := Exp_Typ;
1300 end if;
1302 else
1303 T :=
1304 Make_Defining_Identifier (Loc,
1305 Chars => New_Internal_Name ('T'));
1307 Insert_Action (N,
1308 Make_Subtype_Declaration (Loc,
1309 Defining_Identifier => T,
1310 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
1312 -- This type is marked as an itype even though it has an
1313 -- explicit declaration because otherwise it can be marked
1314 -- with Is_Generic_Actual_Type and generate spurious errors.
1315 -- (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1317 Set_Is_Itype (T);
1318 Set_Associated_Node_For_Itype (T, Exp);
1319 end if;
1321 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
1323 -- Nothing needs to be done for private types with unknown discriminants
1324 -- if the underlying type is not an unconstrained composite type or it
1325 -- is an unchecked union.
1327 elsif Is_Private_Type (Unc_Type)
1328 and then Has_Unknown_Discriminants (Unc_Type)
1329 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
1330 or else Is_Constrained (Underlying_Type (Unc_Type))
1331 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
1332 then
1333 null;
1335 -- Case of derived type with unknown discriminants where the parent type
1336 -- also has unknown discriminants.
1338 elsif Is_Record_Type (Unc_Type)
1339 and then not Is_Class_Wide_Type (Unc_Type)
1340 and then Has_Unknown_Discriminants (Unc_Type)
1341 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
1342 then
1343 -- Nothing to be done if no underlying record view available
1345 if No (Underlying_Record_View (Unc_Type)) then
1346 null;
1348 -- Otherwise use the Underlying_Record_View to create the proper
1349 -- constrained subtype for an object of a derived type with unknown
1350 -- discriminants.
1352 else
1353 Remove_Side_Effects (Exp);
1354 Rewrite (Subtype_Indic,
1355 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
1356 end if;
1358 -- Renamings of class-wide interface types require no equivalent
1359 -- constrained type declarations because we only need to reference
1360 -- the tag component associated with the interface.
1362 elsif Present (N)
1363 and then Nkind (N) = N_Object_Renaming_Declaration
1364 and then Is_Interface (Unc_Type)
1365 then
1366 pragma Assert (Is_Class_Wide_Type (Unc_Type));
1367 null;
1369 -- In Ada95, nothing to be done if the type of the expression is
1370 -- limited, because in this case the expression cannot be copied,
1371 -- and its use can only be by reference.
1373 -- In Ada2005, the context can be an object declaration whose expression
1374 -- is a function that returns in place. If the nominal subtype has
1375 -- unknown discriminants, the call still provides constraints on the
1376 -- object, and we have to create an actual subtype from it.
1378 -- If the type is class-wide, the expression is dynamically tagged and
1379 -- we do not create an actual subtype either. Ditto for an interface.
1381 elsif Is_Limited_Type (Exp_Typ)
1382 and then
1383 (Is_Class_Wide_Type (Exp_Typ)
1384 or else Is_Interface (Exp_Typ)
1385 or else not Has_Unknown_Discriminants (Exp_Typ)
1386 or else not Is_Composite_Type (Unc_Type))
1387 then
1388 null;
1390 -- For limited objects initialized with build in place function calls,
1391 -- nothing to be done; otherwise we prematurely introduce an N_Reference
1392 -- node in the expression initializing the object, which breaks the
1393 -- circuitry that detects and adds the additional arguments to the
1394 -- called function.
1396 elsif Is_Build_In_Place_Function_Call (Exp) then
1397 null;
1399 else
1400 Remove_Side_Effects (Exp);
1401 Rewrite (Subtype_Indic,
1402 Make_Subtype_From_Expr (Exp, Unc_Type));
1403 end if;
1404 end Expand_Subtype_From_Expr;
1406 --------------------
1407 -- Find_Init_Call --
1408 --------------------
1410 function Find_Init_Call
1411 (Var : Entity_Id;
1412 Rep_Clause : Node_Id) return Node_Id
1414 Typ : constant Entity_Id := Etype (Var);
1416 Init_Proc : Entity_Id;
1417 -- Initialization procedure for Typ
1419 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
1420 -- Look for init call for Var starting at From and scanning the
1421 -- enclosing list until Rep_Clause or the end of the list is reached.
1423 ----------------------------
1424 -- Find_Init_Call_In_List --
1425 ----------------------------
1427 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
1428 Init_Call : Node_Id;
1429 begin
1430 Init_Call := From;
1432 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
1433 if Nkind (Init_Call) = N_Procedure_Call_Statement
1434 and then Is_Entity_Name (Name (Init_Call))
1435 and then Entity (Name (Init_Call)) = Init_Proc
1436 then
1437 return Init_Call;
1438 end if;
1439 Next (Init_Call);
1440 end loop;
1442 return Empty;
1443 end Find_Init_Call_In_List;
1445 Init_Call : Node_Id;
1447 -- Start of processing for Find_Init_Call
1449 begin
1450 if not Has_Non_Null_Base_Init_Proc (Typ) then
1451 -- No init proc for the type, so obviously no call to be found
1453 return Empty;
1454 end if;
1456 Init_Proc := Base_Init_Proc (Typ);
1458 -- First scan the list containing the declaration of Var
1460 Init_Call := Find_Init_Call_In_List (From => Next (Parent (Var)));
1462 -- If not found, also look on Var's freeze actions list, if any, since
1463 -- the init call may have been moved there (case of an address clause
1464 -- applying to Var).
1466 if No (Init_Call) and then Present (Freeze_Node (Var)) then
1467 Init_Call := Find_Init_Call_In_List
1468 (First (Actions (Freeze_Node (Var))));
1469 end if;
1471 return Init_Call;
1472 end Find_Init_Call;
1474 ------------------------
1475 -- Find_Interface_ADT --
1476 ------------------------
1478 function Find_Interface_ADT
1479 (T : Entity_Id;
1480 Iface : Entity_Id) return Elmt_Id
1482 ADT : Elmt_Id;
1483 Typ : Entity_Id := T;
1485 begin
1486 pragma Assert (Is_Interface (Iface));
1488 -- Handle private types
1490 if Has_Private_Declaration (Typ)
1491 and then Present (Full_View (Typ))
1492 then
1493 Typ := Full_View (Typ);
1494 end if;
1496 -- Handle access types
1498 if Is_Access_Type (Typ) then
1499 Typ := Directly_Designated_Type (Typ);
1500 end if;
1502 -- Handle task and protected types implementing interfaces
1504 if Is_Concurrent_Type (Typ) then
1505 Typ := Corresponding_Record_Type (Typ);
1506 end if;
1508 pragma Assert
1509 (not Is_Class_Wide_Type (Typ)
1510 and then Ekind (Typ) /= E_Incomplete_Type);
1512 if Is_Ancestor (Iface, Typ) then
1513 return First_Elmt (Access_Disp_Table (Typ));
1515 else
1516 ADT :=
1517 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
1518 while Present (ADT)
1519 and then Present (Related_Type (Node (ADT)))
1520 and then Related_Type (Node (ADT)) /= Iface
1521 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)))
1522 loop
1523 Next_Elmt (ADT);
1524 end loop;
1526 pragma Assert (Present (Related_Type (Node (ADT))));
1527 return ADT;
1528 end if;
1529 end Find_Interface_ADT;
1531 ------------------------
1532 -- Find_Interface_Tag --
1533 ------------------------
1535 function Find_Interface_Tag
1536 (T : Entity_Id;
1537 Iface : Entity_Id) return Entity_Id
1539 AI_Tag : Entity_Id;
1540 Found : Boolean := False;
1541 Typ : Entity_Id := T;
1543 procedure Find_Tag (Typ : Entity_Id);
1544 -- Internal subprogram used to recursively climb to the ancestors
1546 --------------
1547 -- Find_Tag --
1548 --------------
1550 procedure Find_Tag (Typ : Entity_Id) is
1551 AI_Elmt : Elmt_Id;
1552 AI : Node_Id;
1554 begin
1555 -- This routine does not handle the case in which the interface is an
1556 -- ancestor of Typ. That case is handled by the enclosing subprogram.
1558 pragma Assert (Typ /= Iface);
1560 -- Climb to the root type handling private types
1562 if Present (Full_View (Etype (Typ))) then
1563 if Full_View (Etype (Typ)) /= Typ then
1564 Find_Tag (Full_View (Etype (Typ)));
1565 end if;
1567 elsif Etype (Typ) /= Typ then
1568 Find_Tag (Etype (Typ));
1569 end if;
1571 -- Traverse the list of interfaces implemented by the type
1573 if not Found
1574 and then Present (Interfaces (Typ))
1575 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
1576 then
1577 -- Skip the tag associated with the primary table
1579 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1580 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
1581 pragma Assert (Present (AI_Tag));
1583 AI_Elmt := First_Elmt (Interfaces (Typ));
1584 while Present (AI_Elmt) loop
1585 AI := Node (AI_Elmt);
1587 if AI = Iface or else Is_Ancestor (Iface, AI) then
1588 Found := True;
1589 return;
1590 end if;
1592 AI_Tag := Next_Tag_Component (AI_Tag);
1593 Next_Elmt (AI_Elmt);
1594 end loop;
1595 end if;
1596 end Find_Tag;
1598 -- Start of processing for Find_Interface_Tag
1600 begin
1601 pragma Assert (Is_Interface (Iface));
1603 -- Handle access types
1605 if Is_Access_Type (Typ) then
1606 Typ := Directly_Designated_Type (Typ);
1607 end if;
1609 -- Handle class-wide types
1611 if Is_Class_Wide_Type (Typ) then
1612 Typ := Root_Type (Typ);
1613 end if;
1615 -- Handle private types
1617 if Has_Private_Declaration (Typ)
1618 and then Present (Full_View (Typ))
1619 then
1620 Typ := Full_View (Typ);
1621 end if;
1623 -- Handle entities from the limited view
1625 if Ekind (Typ) = E_Incomplete_Type then
1626 pragma Assert (Present (Non_Limited_View (Typ)));
1627 Typ := Non_Limited_View (Typ);
1628 end if;
1630 -- Handle task and protected types implementing interfaces
1632 if Is_Concurrent_Type (Typ) then
1633 Typ := Corresponding_Record_Type (Typ);
1634 end if;
1636 -- If the interface is an ancestor of the type, then it shared the
1637 -- primary dispatch table.
1639 if Is_Ancestor (Iface, Typ) then
1640 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
1641 return First_Tag_Component (Typ);
1643 -- Otherwise we need to search for its associated tag component
1645 else
1646 Find_Tag (Typ);
1647 pragma Assert (Found);
1648 return AI_Tag;
1649 end if;
1650 end Find_Interface_Tag;
1652 ------------------
1653 -- Find_Prim_Op --
1654 ------------------
1656 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
1657 Prim : Elmt_Id;
1658 Typ : Entity_Id := T;
1659 Op : Entity_Id;
1661 begin
1662 if Is_Class_Wide_Type (Typ) then
1663 Typ := Root_Type (Typ);
1664 end if;
1666 Typ := Underlying_Type (Typ);
1668 -- Loop through primitive operations
1670 Prim := First_Elmt (Primitive_Operations (Typ));
1671 while Present (Prim) loop
1672 Op := Node (Prim);
1674 -- We can retrieve primitive operations by name if it is an internal
1675 -- name. For equality we must check that both of its operands have
1676 -- the same type, to avoid confusion with user-defined equalities
1677 -- than may have a non-symmetric signature.
1679 exit when Chars (Op) = Name
1680 and then
1681 (Name /= Name_Op_Eq
1682 or else Etype (First_Entity (Op)) = Etype (Last_Entity (Op)));
1684 Next_Elmt (Prim);
1686 -- Raise Program_Error if no primitive found
1688 if No (Prim) then
1689 raise Program_Error;
1690 end if;
1691 end loop;
1693 return Node (Prim);
1694 end Find_Prim_Op;
1696 ------------------
1697 -- Find_Prim_Op --
1698 ------------------
1700 function Find_Prim_Op
1701 (T : Entity_Id;
1702 Name : TSS_Name_Type) return Entity_Id
1704 Prim : Elmt_Id;
1705 Typ : Entity_Id := T;
1707 begin
1708 if Is_Class_Wide_Type (Typ) then
1709 Typ := Root_Type (Typ);
1710 end if;
1712 Typ := Underlying_Type (Typ);
1714 Prim := First_Elmt (Primitive_Operations (Typ));
1715 while not Is_TSS (Node (Prim), Name) loop
1716 Next_Elmt (Prim);
1718 -- Raise program error if no primitive found
1720 if No (Prim) then
1721 raise Program_Error;
1722 end if;
1723 end loop;
1725 return Node (Prim);
1726 end Find_Prim_Op;
1728 ----------------------------
1729 -- Find_Protection_Object --
1730 ----------------------------
1732 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
1733 S : Entity_Id;
1735 begin
1736 S := Scop;
1737 while Present (S) loop
1738 if (Ekind (S) = E_Entry
1739 or else Ekind (S) = E_Entry_Family
1740 or else Ekind (S) = E_Function
1741 or else Ekind (S) = E_Procedure)
1742 and then Present (Protection_Object (S))
1743 then
1744 return Protection_Object (S);
1745 end if;
1747 S := Scope (S);
1748 end loop;
1750 -- If we do not find a Protection object in the scope chain, then
1751 -- something has gone wrong, most likely the object was never created.
1753 raise Program_Error;
1754 end Find_Protection_Object;
1756 ----------------------
1757 -- Force_Evaluation --
1758 ----------------------
1760 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
1761 begin
1762 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
1763 end Force_Evaluation;
1765 ------------------------
1766 -- Generate_Poll_Call --
1767 ------------------------
1769 procedure Generate_Poll_Call (N : Node_Id) is
1770 begin
1771 -- No poll call if polling not active
1773 if not Polling_Required then
1774 return;
1776 -- Otherwise generate require poll call
1778 else
1779 Insert_Before_And_Analyze (N,
1780 Make_Procedure_Call_Statement (Sloc (N),
1781 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
1782 end if;
1783 end Generate_Poll_Call;
1785 ---------------------------------
1786 -- Get_Current_Value_Condition --
1787 ---------------------------------
1789 -- Note: the implementation of this procedure is very closely tied to the
1790 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
1791 -- interpret Current_Value fields set by the Set procedure, so the two
1792 -- procedures need to be closely coordinated.
1794 procedure Get_Current_Value_Condition
1795 (Var : Node_Id;
1796 Op : out Node_Kind;
1797 Val : out Node_Id)
1799 Loc : constant Source_Ptr := Sloc (Var);
1800 Ent : constant Entity_Id := Entity (Var);
1802 procedure Process_Current_Value_Condition
1803 (N : Node_Id;
1804 S : Boolean);
1805 -- N is an expression which holds either True (S = True) or False (S =
1806 -- False) in the condition. This procedure digs out the expression and
1807 -- if it refers to Ent, sets Op and Val appropriately.
1809 -------------------------------------
1810 -- Process_Current_Value_Condition --
1811 -------------------------------------
1813 procedure Process_Current_Value_Condition
1814 (N : Node_Id;
1815 S : Boolean)
1817 Cond : Node_Id;
1818 Sens : Boolean;
1820 begin
1821 Cond := N;
1822 Sens := S;
1824 -- Deal with NOT operators, inverting sense
1826 while Nkind (Cond) = N_Op_Not loop
1827 Cond := Right_Opnd (Cond);
1828 Sens := not Sens;
1829 end loop;
1831 -- Deal with AND THEN and AND cases
1833 if Nkind (Cond) = N_And_Then
1834 or else Nkind (Cond) = N_Op_And
1835 then
1836 -- Don't ever try to invert a condition that is of the form
1837 -- of an AND or AND THEN (since we are not doing sufficiently
1838 -- general processing to allow this).
1840 if Sens = False then
1841 Op := N_Empty;
1842 Val := Empty;
1843 return;
1844 end if;
1846 -- Recursively process AND and AND THEN branches
1848 Process_Current_Value_Condition (Left_Opnd (Cond), True);
1850 if Op /= N_Empty then
1851 return;
1852 end if;
1854 Process_Current_Value_Condition (Right_Opnd (Cond), True);
1855 return;
1857 -- Case of relational operator
1859 elsif Nkind (Cond) in N_Op_Compare then
1860 Op := Nkind (Cond);
1862 -- Invert sense of test if inverted test
1864 if Sens = False then
1865 case Op is
1866 when N_Op_Eq => Op := N_Op_Ne;
1867 when N_Op_Ne => Op := N_Op_Eq;
1868 when N_Op_Lt => Op := N_Op_Ge;
1869 when N_Op_Gt => Op := N_Op_Le;
1870 when N_Op_Le => Op := N_Op_Gt;
1871 when N_Op_Ge => Op := N_Op_Lt;
1872 when others => raise Program_Error;
1873 end case;
1874 end if;
1876 -- Case of entity op value
1878 if Is_Entity_Name (Left_Opnd (Cond))
1879 and then Ent = Entity (Left_Opnd (Cond))
1880 and then Compile_Time_Known_Value (Right_Opnd (Cond))
1881 then
1882 Val := Right_Opnd (Cond);
1884 -- Case of value op entity
1886 elsif Is_Entity_Name (Right_Opnd (Cond))
1887 and then Ent = Entity (Right_Opnd (Cond))
1888 and then Compile_Time_Known_Value (Left_Opnd (Cond))
1889 then
1890 Val := Left_Opnd (Cond);
1892 -- We are effectively swapping operands
1894 case Op is
1895 when N_Op_Eq => null;
1896 when N_Op_Ne => null;
1897 when N_Op_Lt => Op := N_Op_Gt;
1898 when N_Op_Gt => Op := N_Op_Lt;
1899 when N_Op_Le => Op := N_Op_Ge;
1900 when N_Op_Ge => Op := N_Op_Le;
1901 when others => raise Program_Error;
1902 end case;
1904 else
1905 Op := N_Empty;
1906 end if;
1908 return;
1910 -- Case of Boolean variable reference, return as though the
1911 -- reference had said var = True.
1913 else
1914 if Is_Entity_Name (Cond)
1915 and then Ent = Entity (Cond)
1916 then
1917 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
1919 if Sens = False then
1920 Op := N_Op_Ne;
1921 else
1922 Op := N_Op_Eq;
1923 end if;
1924 end if;
1925 end if;
1926 end Process_Current_Value_Condition;
1928 -- Start of processing for Get_Current_Value_Condition
1930 begin
1931 Op := N_Empty;
1932 Val := Empty;
1934 -- Immediate return, nothing doing, if this is not an object
1936 if Ekind (Ent) not in Object_Kind then
1937 return;
1938 end if;
1940 -- Otherwise examine current value
1942 declare
1943 CV : constant Node_Id := Current_Value (Ent);
1944 Sens : Boolean;
1945 Stm : Node_Id;
1947 begin
1948 -- If statement. Condition is known true in THEN section, known False
1949 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
1951 if Nkind (CV) = N_If_Statement then
1953 -- Before start of IF statement
1955 if Loc < Sloc (CV) then
1956 return;
1958 -- After end of IF statement
1960 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
1961 return;
1962 end if;
1964 -- At this stage we know that we are within the IF statement, but
1965 -- unfortunately, the tree does not record the SLOC of the ELSE so
1966 -- we cannot use a simple SLOC comparison to distinguish between
1967 -- the then/else statements, so we have to climb the tree.
1969 declare
1970 N : Node_Id;
1972 begin
1973 N := Parent (Var);
1974 while Parent (N) /= CV loop
1975 N := Parent (N);
1977 -- If we fall off the top of the tree, then that's odd, but
1978 -- perhaps it could occur in some error situation, and the
1979 -- safest response is simply to assume that the outcome of
1980 -- the condition is unknown. No point in bombing during an
1981 -- attempt to optimize things.
1983 if No (N) then
1984 return;
1985 end if;
1986 end loop;
1988 -- Now we have N pointing to a node whose parent is the IF
1989 -- statement in question, so now we can tell if we are within
1990 -- the THEN statements.
1992 if Is_List_Member (N)
1993 and then List_Containing (N) = Then_Statements (CV)
1994 then
1995 Sens := True;
1997 -- If the variable reference does not come from source, we
1998 -- cannot reliably tell whether it appears in the else part.
1999 -- In particular, if it appears in generated code for a node
2000 -- that requires finalization, it may be attached to a list
2001 -- that has not been yet inserted into the code. For now,
2002 -- treat it as unknown.
2004 elsif not Comes_From_Source (N) then
2005 return;
2007 -- Otherwise we must be in ELSIF or ELSE part
2009 else
2010 Sens := False;
2011 end if;
2012 end;
2014 -- ELSIF part. Condition is known true within the referenced
2015 -- ELSIF, known False in any subsequent ELSIF or ELSE part, and
2016 -- unknown before the ELSE part or after the IF statement.
2018 elsif Nkind (CV) = N_Elsif_Part then
2019 Stm := Parent (CV);
2021 -- Before start of ELSIF part
2023 if Loc < Sloc (CV) then
2024 return;
2026 -- After end of IF statement
2028 elsif Loc >= Sloc (Stm) +
2029 Text_Ptr (UI_To_Int (End_Span (Stm)))
2030 then
2031 return;
2032 end if;
2034 -- Again we lack the SLOC of the ELSE, so we need to climb the
2035 -- tree to see if we are within the ELSIF part in question.
2037 declare
2038 N : Node_Id;
2040 begin
2041 N := Parent (Var);
2042 while Parent (N) /= Stm loop
2043 N := Parent (N);
2045 -- If we fall off the top of the tree, then that's odd, but
2046 -- perhaps it could occur in some error situation, and the
2047 -- safest response is simply to assume that the outcome of
2048 -- the condition is unknown. No point in bombing during an
2049 -- attempt to optimize things.
2051 if No (N) then
2052 return;
2053 end if;
2054 end loop;
2056 -- Now we have N pointing to a node whose parent is the IF
2057 -- statement in question, so see if is the ELSIF part we want.
2058 -- the THEN statements.
2060 if N = CV then
2061 Sens := True;
2063 -- Otherwise we must be in subsequent ELSIF or ELSE part
2065 else
2066 Sens := False;
2067 end if;
2068 end;
2070 -- Iteration scheme of while loop. The condition is known to be
2071 -- true within the body of the loop.
2073 elsif Nkind (CV) = N_Iteration_Scheme then
2074 declare
2075 Loop_Stmt : constant Node_Id := Parent (CV);
2077 begin
2078 -- Before start of body of loop
2080 if Loc < Sloc (Loop_Stmt) then
2081 return;
2083 -- After end of LOOP statement
2085 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2086 return;
2088 -- We are within the body of the loop
2090 else
2091 Sens := True;
2092 end if;
2093 end;
2095 -- All other cases of Current_Value settings
2097 else
2098 return;
2099 end if;
2101 -- If we fall through here, then we have a reportable condition, Sens
2102 -- is True if the condition is true and False if it needs inverting.
2104 Process_Current_Value_Condition (Condition (CV), Sens);
2105 end;
2106 end Get_Current_Value_Condition;
2108 ---------------------------------
2109 -- Has_Controlled_Coextensions --
2110 ---------------------------------
2112 function Has_Controlled_Coextensions (Typ : Entity_Id) return Boolean is
2113 D_Typ : Entity_Id;
2114 Discr : Entity_Id;
2116 begin
2117 -- Only consider record types
2119 if Ekind (Typ) /= E_Record_Type
2120 and then Ekind (Typ) /= E_Record_Subtype
2121 then
2122 return False;
2123 end if;
2125 if Has_Discriminants (Typ) then
2126 Discr := First_Discriminant (Typ);
2127 while Present (Discr) loop
2128 D_Typ := Etype (Discr);
2130 if Ekind (D_Typ) = E_Anonymous_Access_Type
2131 and then
2132 (Is_Controlled (Directly_Designated_Type (D_Typ))
2133 or else
2134 Is_Concurrent_Type (Directly_Designated_Type (D_Typ)))
2135 then
2136 return True;
2137 end if;
2139 Next_Discriminant (Discr);
2140 end loop;
2141 end if;
2143 return False;
2144 end Has_Controlled_Coextensions;
2146 --------------------
2147 -- Homonym_Number --
2148 --------------------
2150 function Homonym_Number (Subp : Entity_Id) return Nat is
2151 Count : Nat;
2152 Hom : Entity_Id;
2154 begin
2155 Count := 1;
2156 Hom := Homonym (Subp);
2157 while Present (Hom) loop
2158 if Scope (Hom) = Scope (Subp) then
2159 Count := Count + 1;
2160 end if;
2162 Hom := Homonym (Hom);
2163 end loop;
2165 return Count;
2166 end Homonym_Number;
2168 ------------------------------
2169 -- In_Unconditional_Context --
2170 ------------------------------
2172 function In_Unconditional_Context (Node : Node_Id) return Boolean is
2173 P : Node_Id;
2175 begin
2176 P := Node;
2177 while Present (P) loop
2178 case Nkind (P) is
2179 when N_Subprogram_Body =>
2180 return True;
2182 when N_If_Statement =>
2183 return False;
2185 when N_Loop_Statement =>
2186 return False;
2188 when N_Case_Statement =>
2189 return False;
2191 when others =>
2192 P := Parent (P);
2193 end case;
2194 end loop;
2196 return False;
2197 end In_Unconditional_Context;
2199 -------------------
2200 -- Insert_Action --
2201 -------------------
2203 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
2204 begin
2205 if Present (Ins_Action) then
2206 Insert_Actions (Assoc_Node, New_List (Ins_Action));
2207 end if;
2208 end Insert_Action;
2210 -- Version with check(s) suppressed
2212 procedure Insert_Action
2213 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
2215 begin
2216 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
2217 end Insert_Action;
2219 --------------------
2220 -- Insert_Actions --
2221 --------------------
2223 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
2224 N : Node_Id;
2225 P : Node_Id;
2227 Wrapped_Node : Node_Id := Empty;
2229 begin
2230 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
2231 return;
2232 end if;
2234 -- Ignore insert of actions from inside default expression (or other
2235 -- similar "spec expression") in the special spec-expression analyze
2236 -- mode. Any insertions at this point have no relevance, since we are
2237 -- only doing the analyze to freeze the types of any static expressions.
2238 -- See section "Handling of Default Expressions" in the spec of package
2239 -- Sem for further details.
2241 if In_Spec_Expression then
2242 return;
2243 end if;
2245 -- If the action derives from stuff inside a record, then the actions
2246 -- are attached to the current scope, to be inserted and analyzed on
2247 -- exit from the scope. The reason for this is that we may also
2248 -- be generating freeze actions at the same time, and they must
2249 -- eventually be elaborated in the correct order.
2251 if Is_Record_Type (Current_Scope)
2252 and then not Is_Frozen (Current_Scope)
2253 then
2254 if No (Scope_Stack.Table
2255 (Scope_Stack.Last).Pending_Freeze_Actions)
2256 then
2257 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
2258 Ins_Actions;
2259 else
2260 Append_List
2261 (Ins_Actions,
2262 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
2263 end if;
2265 return;
2266 end if;
2268 -- We now intend to climb up the tree to find the right point to
2269 -- insert the actions. We start at Assoc_Node, unless this node is
2270 -- a subexpression in which case we start with its parent. We do this
2271 -- for two reasons. First it speeds things up. Second, if Assoc_Node
2272 -- is itself one of the special nodes like N_And_Then, then we assume
2273 -- that an initial request to insert actions for such a node does not
2274 -- expect the actions to get deposited in the node for later handling
2275 -- when the node is expanded, since clearly the node is being dealt
2276 -- with by the caller. Note that in the subexpression case, N is
2277 -- always the child we came from.
2279 -- N_Raise_xxx_Error is an annoying special case, it is a statement
2280 -- if it has type Standard_Void_Type, and a subexpression otherwise.
2281 -- otherwise. Procedure attribute references are also statements.
2283 if Nkind (Assoc_Node) in N_Subexpr
2284 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
2285 or else Etype (Assoc_Node) /= Standard_Void_Type)
2286 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
2287 or else
2288 not Is_Procedure_Attribute_Name
2289 (Attribute_Name (Assoc_Node)))
2290 then
2291 P := Assoc_Node; -- ??? does not agree with above!
2292 N := Parent (Assoc_Node);
2294 -- Non-subexpression case. Note that N is initially Empty in this
2295 -- case (N is only guaranteed Non-Empty in the subexpr case).
2297 else
2298 P := Assoc_Node;
2299 N := Empty;
2300 end if;
2302 -- Capture root of the transient scope
2304 if Scope_Is_Transient then
2305 Wrapped_Node := Node_To_Be_Wrapped;
2306 end if;
2308 loop
2309 pragma Assert (Present (P));
2311 case Nkind (P) is
2313 -- Case of right operand of AND THEN or OR ELSE. Put the actions
2314 -- in the Actions field of the right operand. They will be moved
2315 -- out further when the AND THEN or OR ELSE operator is expanded.
2316 -- Nothing special needs to be done for the left operand since
2317 -- in that case the actions are executed unconditionally.
2319 when N_Short_Circuit =>
2320 if N = Right_Opnd (P) then
2322 -- We are now going to either append the actions to the
2323 -- actions field of the short-circuit operation. We will
2324 -- also analyze the actions now.
2326 -- This analysis is really too early, the proper thing would
2327 -- be to just park them there now, and only analyze them if
2328 -- we find we really need them, and to it at the proper
2329 -- final insertion point. However attempting to this proved
2330 -- tricky, so for now we just kill current values before and
2331 -- after the analyze call to make sure we avoid peculiar
2332 -- optimizations from this out of order insertion.
2334 Kill_Current_Values;
2336 if Present (Actions (P)) then
2337 Insert_List_After_And_Analyze
2338 (Last (Actions (P)), Ins_Actions);
2339 else
2340 Set_Actions (P, Ins_Actions);
2341 Analyze_List (Actions (P));
2342 end if;
2344 Kill_Current_Values;
2346 return;
2347 end if;
2349 -- Then or Else operand of conditional expression. Add actions to
2350 -- Then_Actions or Else_Actions field as appropriate. The actions
2351 -- will be moved further out when the conditional is expanded.
2353 when N_Conditional_Expression =>
2354 declare
2355 ThenX : constant Node_Id := Next (First (Expressions (P)));
2356 ElseX : constant Node_Id := Next (ThenX);
2358 begin
2359 -- Actions belong to the then expression, temporarily
2360 -- place them as Then_Actions of the conditional expr.
2361 -- They will be moved to the proper place later when
2362 -- the conditional expression is expanded.
2364 if N = ThenX then
2365 if Present (Then_Actions (P)) then
2366 Insert_List_After_And_Analyze
2367 (Last (Then_Actions (P)), Ins_Actions);
2368 else
2369 Set_Then_Actions (P, Ins_Actions);
2370 Analyze_List (Then_Actions (P));
2371 end if;
2373 return;
2375 -- Actions belong to the else expression, temporarily
2376 -- place them as Else_Actions of the conditional expr.
2377 -- They will be moved to the proper place later when
2378 -- the conditional expression is expanded.
2380 elsif N = ElseX then
2381 if Present (Else_Actions (P)) then
2382 Insert_List_After_And_Analyze
2383 (Last (Else_Actions (P)), Ins_Actions);
2384 else
2385 Set_Else_Actions (P, Ins_Actions);
2386 Analyze_List (Else_Actions (P));
2387 end if;
2389 return;
2391 -- Actions belong to the condition. In this case they are
2392 -- unconditionally executed, and so we can continue the
2393 -- search for the proper insert point.
2395 else
2396 null;
2397 end if;
2398 end;
2400 -- Case of appearing in the condition of a while expression or
2401 -- elsif. We insert the actions into the Condition_Actions field.
2402 -- They will be moved further out when the while loop or elsif
2403 -- is analyzed.
2405 when N_Iteration_Scheme |
2406 N_Elsif_Part
2408 if N = Condition (P) then
2409 if Present (Condition_Actions (P)) then
2410 Insert_List_After_And_Analyze
2411 (Last (Condition_Actions (P)), Ins_Actions);
2412 else
2413 Set_Condition_Actions (P, Ins_Actions);
2415 -- Set the parent of the insert actions explicitly.
2416 -- This is not a syntactic field, but we need the
2417 -- parent field set, in particular so that freeze
2418 -- can understand that it is dealing with condition
2419 -- actions, and properly insert the freezing actions.
2421 Set_Parent (Ins_Actions, P);
2422 Analyze_List (Condition_Actions (P));
2423 end if;
2425 return;
2426 end if;
2428 -- Statements, declarations, pragmas, representation clauses
2430 when
2431 -- Statements
2433 N_Procedure_Call_Statement |
2434 N_Statement_Other_Than_Procedure_Call |
2436 -- Pragmas
2438 N_Pragma |
2440 -- Representation_Clause
2442 N_At_Clause |
2443 N_Attribute_Definition_Clause |
2444 N_Enumeration_Representation_Clause |
2445 N_Record_Representation_Clause |
2447 -- Declarations
2449 N_Abstract_Subprogram_Declaration |
2450 N_Entry_Body |
2451 N_Exception_Declaration |
2452 N_Exception_Renaming_Declaration |
2453 N_Formal_Abstract_Subprogram_Declaration |
2454 N_Formal_Concrete_Subprogram_Declaration |
2455 N_Formal_Object_Declaration |
2456 N_Formal_Type_Declaration |
2457 N_Full_Type_Declaration |
2458 N_Function_Instantiation |
2459 N_Generic_Function_Renaming_Declaration |
2460 N_Generic_Package_Declaration |
2461 N_Generic_Package_Renaming_Declaration |
2462 N_Generic_Procedure_Renaming_Declaration |
2463 N_Generic_Subprogram_Declaration |
2464 N_Implicit_Label_Declaration |
2465 N_Incomplete_Type_Declaration |
2466 N_Number_Declaration |
2467 N_Object_Declaration |
2468 N_Object_Renaming_Declaration |
2469 N_Package_Body |
2470 N_Package_Body_Stub |
2471 N_Package_Declaration |
2472 N_Package_Instantiation |
2473 N_Package_Renaming_Declaration |
2474 N_Private_Extension_Declaration |
2475 N_Private_Type_Declaration |
2476 N_Procedure_Instantiation |
2477 N_Protected_Body |
2478 N_Protected_Body_Stub |
2479 N_Protected_Type_Declaration |
2480 N_Single_Task_Declaration |
2481 N_Subprogram_Body |
2482 N_Subprogram_Body_Stub |
2483 N_Subprogram_Declaration |
2484 N_Subprogram_Renaming_Declaration |
2485 N_Subtype_Declaration |
2486 N_Task_Body |
2487 N_Task_Body_Stub |
2488 N_Task_Type_Declaration |
2490 -- Freeze entity behaves like a declaration or statement
2492 N_Freeze_Entity
2494 -- Do not insert here if the item is not a list member (this
2495 -- happens for example with a triggering statement, and the
2496 -- proper approach is to insert before the entire select).
2498 if not Is_List_Member (P) then
2499 null;
2501 -- Do not insert if parent of P is an N_Component_Association
2502 -- node (i.e. we are in the context of an N_Aggregate or
2503 -- N_Extension_Aggregate node. In this case we want to insert
2504 -- before the entire aggregate.
2506 elsif Nkind (Parent (P)) = N_Component_Association then
2507 null;
2509 -- Do not insert if the parent of P is either an N_Variant
2510 -- node or an N_Record_Definition node, meaning in either
2511 -- case that P is a member of a component list, and that
2512 -- therefore the actions should be inserted outside the
2513 -- complete record declaration.
2515 elsif Nkind (Parent (P)) = N_Variant
2516 or else Nkind (Parent (P)) = N_Record_Definition
2517 then
2518 null;
2520 -- Do not insert freeze nodes within the loop generated for
2521 -- an aggregate, because they may be elaborated too late for
2522 -- subsequent use in the back end: within a package spec the
2523 -- loop is part of the elaboration procedure and is only
2524 -- elaborated during the second pass.
2525 -- If the loop comes from source, or the entity is local to
2526 -- the loop itself it must remain within.
2528 elsif Nkind (Parent (P)) = N_Loop_Statement
2529 and then not Comes_From_Source (Parent (P))
2530 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
2531 and then
2532 Scope (Entity (First (Ins_Actions))) /= Current_Scope
2533 then
2534 null;
2536 -- Otherwise we can go ahead and do the insertion
2538 elsif P = Wrapped_Node then
2539 Store_Before_Actions_In_Scope (Ins_Actions);
2540 return;
2542 else
2543 Insert_List_Before_And_Analyze (P, Ins_Actions);
2544 return;
2545 end if;
2547 -- A special case, N_Raise_xxx_Error can act either as a
2548 -- statement or a subexpression. We tell the difference
2549 -- by looking at the Etype. It is set to Standard_Void_Type
2550 -- in the statement case.
2552 when
2553 N_Raise_xxx_Error =>
2554 if Etype (P) = Standard_Void_Type then
2555 if P = Wrapped_Node then
2556 Store_Before_Actions_In_Scope (Ins_Actions);
2557 else
2558 Insert_List_Before_And_Analyze (P, Ins_Actions);
2559 end if;
2561 return;
2563 -- In the subexpression case, keep climbing
2565 else
2566 null;
2567 end if;
2569 -- If a component association appears within a loop created for
2570 -- an array aggregate, attach the actions to the association so
2571 -- they can be subsequently inserted within the loop. For other
2572 -- component associations insert outside of the aggregate. For
2573 -- an association that will generate a loop, its Loop_Actions
2574 -- attribute is already initialized (see exp_aggr.adb).
2576 -- The list of loop_actions can in turn generate additional ones,
2577 -- that are inserted before the associated node. If the associated
2578 -- node is outside the aggregate, the new actions are collected
2579 -- at the end of the loop actions, to respect the order in which
2580 -- they are to be elaborated.
2582 when
2583 N_Component_Association =>
2584 if Nkind (Parent (P)) = N_Aggregate
2585 and then Present (Loop_Actions (P))
2586 then
2587 if Is_Empty_List (Loop_Actions (P)) then
2588 Set_Loop_Actions (P, Ins_Actions);
2589 Analyze_List (Ins_Actions);
2591 else
2592 declare
2593 Decl : Node_Id;
2595 begin
2596 -- Check whether these actions were generated
2597 -- by a declaration that is part of the loop_
2598 -- actions for the component_association.
2600 Decl := Assoc_Node;
2601 while Present (Decl) loop
2602 exit when Parent (Decl) = P
2603 and then Is_List_Member (Decl)
2604 and then
2605 List_Containing (Decl) = Loop_Actions (P);
2606 Decl := Parent (Decl);
2607 end loop;
2609 if Present (Decl) then
2610 Insert_List_Before_And_Analyze
2611 (Decl, Ins_Actions);
2612 else
2613 Insert_List_After_And_Analyze
2614 (Last (Loop_Actions (P)), Ins_Actions);
2615 end if;
2616 end;
2617 end if;
2619 return;
2621 else
2622 null;
2623 end if;
2625 -- Another special case, an attribute denoting a procedure call
2627 when
2628 N_Attribute_Reference =>
2629 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
2630 if P = Wrapped_Node then
2631 Store_Before_Actions_In_Scope (Ins_Actions);
2632 else
2633 Insert_List_Before_And_Analyze (P, Ins_Actions);
2634 end if;
2636 return;
2638 -- In the subexpression case, keep climbing
2640 else
2641 null;
2642 end if;
2644 -- For all other node types, keep climbing tree
2646 when
2647 N_Abortable_Part |
2648 N_Accept_Alternative |
2649 N_Access_Definition |
2650 N_Access_Function_Definition |
2651 N_Access_Procedure_Definition |
2652 N_Access_To_Object_Definition |
2653 N_Aggregate |
2654 N_Allocator |
2655 N_Case_Statement_Alternative |
2656 N_Character_Literal |
2657 N_Compilation_Unit |
2658 N_Compilation_Unit_Aux |
2659 N_Component_Clause |
2660 N_Component_Declaration |
2661 N_Component_Definition |
2662 N_Component_List |
2663 N_Constrained_Array_Definition |
2664 N_Decimal_Fixed_Point_Definition |
2665 N_Defining_Character_Literal |
2666 N_Defining_Identifier |
2667 N_Defining_Operator_Symbol |
2668 N_Defining_Program_Unit_Name |
2669 N_Delay_Alternative |
2670 N_Delta_Constraint |
2671 N_Derived_Type_Definition |
2672 N_Designator |
2673 N_Digits_Constraint |
2674 N_Discriminant_Association |
2675 N_Discriminant_Specification |
2676 N_Empty |
2677 N_Entry_Body_Formal_Part |
2678 N_Entry_Call_Alternative |
2679 N_Entry_Declaration |
2680 N_Entry_Index_Specification |
2681 N_Enumeration_Type_Definition |
2682 N_Error |
2683 N_Exception_Handler |
2684 N_Expanded_Name |
2685 N_Explicit_Dereference |
2686 N_Extension_Aggregate |
2687 N_Floating_Point_Definition |
2688 N_Formal_Decimal_Fixed_Point_Definition |
2689 N_Formal_Derived_Type_Definition |
2690 N_Formal_Discrete_Type_Definition |
2691 N_Formal_Floating_Point_Definition |
2692 N_Formal_Modular_Type_Definition |
2693 N_Formal_Ordinary_Fixed_Point_Definition |
2694 N_Formal_Package_Declaration |
2695 N_Formal_Private_Type_Definition |
2696 N_Formal_Signed_Integer_Type_Definition |
2697 N_Function_Call |
2698 N_Function_Specification |
2699 N_Generic_Association |
2700 N_Handled_Sequence_Of_Statements |
2701 N_Identifier |
2702 N_In |
2703 N_Index_Or_Discriminant_Constraint |
2704 N_Indexed_Component |
2705 N_Integer_Literal |
2706 N_Itype_Reference |
2707 N_Label |
2708 N_Loop_Parameter_Specification |
2709 N_Mod_Clause |
2710 N_Modular_Type_Definition |
2711 N_Not_In |
2712 N_Null |
2713 N_Op_Abs |
2714 N_Op_Add |
2715 N_Op_And |
2716 N_Op_Concat |
2717 N_Op_Divide |
2718 N_Op_Eq |
2719 N_Op_Expon |
2720 N_Op_Ge |
2721 N_Op_Gt |
2722 N_Op_Le |
2723 N_Op_Lt |
2724 N_Op_Minus |
2725 N_Op_Mod |
2726 N_Op_Multiply |
2727 N_Op_Ne |
2728 N_Op_Not |
2729 N_Op_Or |
2730 N_Op_Plus |
2731 N_Op_Rem |
2732 N_Op_Rotate_Left |
2733 N_Op_Rotate_Right |
2734 N_Op_Shift_Left |
2735 N_Op_Shift_Right |
2736 N_Op_Shift_Right_Arithmetic |
2737 N_Op_Subtract |
2738 N_Op_Xor |
2739 N_Operator_Symbol |
2740 N_Ordinary_Fixed_Point_Definition |
2741 N_Others_Choice |
2742 N_Package_Specification |
2743 N_Parameter_Association |
2744 N_Parameter_Specification |
2745 N_Pop_Constraint_Error_Label |
2746 N_Pop_Program_Error_Label |
2747 N_Pop_Storage_Error_Label |
2748 N_Pragma_Argument_Association |
2749 N_Procedure_Specification |
2750 N_Protected_Definition |
2751 N_Push_Constraint_Error_Label |
2752 N_Push_Program_Error_Label |
2753 N_Push_Storage_Error_Label |
2754 N_Qualified_Expression |
2755 N_Range |
2756 N_Range_Constraint |
2757 N_Real_Literal |
2758 N_Real_Range_Specification |
2759 N_Record_Definition |
2760 N_Reference |
2761 N_SCIL_Dispatch_Table_Object_Init |
2762 N_SCIL_Dispatch_Table_Tag_Init |
2763 N_SCIL_Dispatching_Call |
2764 N_SCIL_Tag_Init |
2765 N_Selected_Component |
2766 N_Signed_Integer_Type_Definition |
2767 N_Single_Protected_Declaration |
2768 N_Slice |
2769 N_String_Literal |
2770 N_Subprogram_Info |
2771 N_Subtype_Indication |
2772 N_Subunit |
2773 N_Task_Definition |
2774 N_Terminate_Alternative |
2775 N_Triggering_Alternative |
2776 N_Type_Conversion |
2777 N_Unchecked_Expression |
2778 N_Unchecked_Type_Conversion |
2779 N_Unconstrained_Array_Definition |
2780 N_Unused_At_End |
2781 N_Unused_At_Start |
2782 N_Use_Package_Clause |
2783 N_Use_Type_Clause |
2784 N_Variant |
2785 N_Variant_Part |
2786 N_Validate_Unchecked_Conversion |
2787 N_With_Clause
2789 null;
2791 end case;
2793 -- Make sure that inserted actions stay in the transient scope
2795 if P = Wrapped_Node then
2796 Store_Before_Actions_In_Scope (Ins_Actions);
2797 return;
2798 end if;
2800 -- If we fall through above tests, keep climbing tree
2802 N := P;
2804 if Nkind (Parent (N)) = N_Subunit then
2806 -- This is the proper body corresponding to a stub. Insertion
2807 -- must be done at the point of the stub, which is in the decla-
2808 -- rative part of the parent unit.
2810 P := Corresponding_Stub (Parent (N));
2812 else
2813 P := Parent (N);
2814 end if;
2815 end loop;
2816 end Insert_Actions;
2818 -- Version with check(s) suppressed
2820 procedure Insert_Actions
2821 (Assoc_Node : Node_Id;
2822 Ins_Actions : List_Id;
2823 Suppress : Check_Id)
2825 begin
2826 if Suppress = All_Checks then
2827 declare
2828 Svg : constant Suppress_Array := Scope_Suppress;
2829 begin
2830 Scope_Suppress := (others => True);
2831 Insert_Actions (Assoc_Node, Ins_Actions);
2832 Scope_Suppress := Svg;
2833 end;
2835 else
2836 declare
2837 Svg : constant Boolean := Scope_Suppress (Suppress);
2838 begin
2839 Scope_Suppress (Suppress) := True;
2840 Insert_Actions (Assoc_Node, Ins_Actions);
2841 Scope_Suppress (Suppress) := Svg;
2842 end;
2843 end if;
2844 end Insert_Actions;
2846 --------------------------
2847 -- Insert_Actions_After --
2848 --------------------------
2850 procedure Insert_Actions_After
2851 (Assoc_Node : Node_Id;
2852 Ins_Actions : List_Id)
2854 begin
2855 if Scope_Is_Transient
2856 and then Assoc_Node = Node_To_Be_Wrapped
2857 then
2858 Store_After_Actions_In_Scope (Ins_Actions);
2859 else
2860 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
2861 end if;
2862 end Insert_Actions_After;
2864 ---------------------------------
2865 -- Insert_Library_Level_Action --
2866 ---------------------------------
2868 procedure Insert_Library_Level_Action (N : Node_Id) is
2869 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2871 begin
2872 Push_Scope (Cunit_Entity (Main_Unit));
2873 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
2875 if No (Actions (Aux)) then
2876 Set_Actions (Aux, New_List (N));
2877 else
2878 Append (N, Actions (Aux));
2879 end if;
2881 Analyze (N);
2882 Pop_Scope;
2883 end Insert_Library_Level_Action;
2885 ----------------------------------
2886 -- Insert_Library_Level_Actions --
2887 ----------------------------------
2889 procedure Insert_Library_Level_Actions (L : List_Id) is
2890 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2892 begin
2893 if Is_Non_Empty_List (L) then
2894 Push_Scope (Cunit_Entity (Main_Unit));
2895 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
2897 if No (Actions (Aux)) then
2898 Set_Actions (Aux, L);
2899 Analyze_List (L);
2900 else
2901 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
2902 end if;
2904 Pop_Scope;
2905 end if;
2906 end Insert_Library_Level_Actions;
2908 ----------------------
2909 -- Inside_Init_Proc --
2910 ----------------------
2912 function Inside_Init_Proc return Boolean is
2913 S : Entity_Id;
2915 begin
2916 S := Current_Scope;
2917 while Present (S)
2918 and then S /= Standard_Standard
2919 loop
2920 if Is_Init_Proc (S) then
2921 return True;
2922 else
2923 S := Scope (S);
2924 end if;
2925 end loop;
2927 return False;
2928 end Inside_Init_Proc;
2930 ----------------------------
2931 -- Is_All_Null_Statements --
2932 ----------------------------
2934 function Is_All_Null_Statements (L : List_Id) return Boolean is
2935 Stm : Node_Id;
2937 begin
2938 Stm := First (L);
2939 while Present (Stm) loop
2940 if Nkind (Stm) /= N_Null_Statement then
2941 return False;
2942 end if;
2944 Next (Stm);
2945 end loop;
2947 return True;
2948 end Is_All_Null_Statements;
2950 ---------------------------------
2951 -- Is_Fully_Repped_Tagged_Type --
2952 ---------------------------------
2954 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
2955 U : constant Entity_Id := Underlying_Type (T);
2956 Comp : Entity_Id;
2958 begin
2959 if No (U) or else not Is_Tagged_Type (U) then
2960 return False;
2961 elsif Has_Discriminants (U) then
2962 return False;
2963 elsif not Has_Specified_Layout (U) then
2964 return False;
2965 end if;
2967 -- Here we have a tagged type, see if it has any unlayed out fields
2968 -- other than a possible tag and parent fields. If so, we return False.
2970 Comp := First_Component (U);
2971 while Present (Comp) loop
2972 if not Is_Tag (Comp)
2973 and then Chars (Comp) /= Name_uParent
2974 and then No (Component_Clause (Comp))
2975 then
2976 return False;
2977 else
2978 Next_Component (Comp);
2979 end if;
2980 end loop;
2982 -- All components are layed out
2984 return True;
2985 end Is_Fully_Repped_Tagged_Type;
2987 ----------------------------------
2988 -- Is_Library_Level_Tagged_Type --
2989 ----------------------------------
2991 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
2992 begin
2993 return Is_Tagged_Type (Typ)
2994 and then Is_Library_Level_Entity (Typ);
2995 end Is_Library_Level_Tagged_Type;
2997 ----------------------------------
2998 -- Is_Possibly_Unaligned_Object --
2999 ----------------------------------
3001 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
3002 T : constant Entity_Id := Etype (N);
3004 begin
3005 -- If renamed object, apply test to underlying object
3007 if Is_Entity_Name (N)
3008 and then Is_Object (Entity (N))
3009 and then Present (Renamed_Object (Entity (N)))
3010 then
3011 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
3012 end if;
3014 -- Tagged and controlled types and aliased types are always aligned,
3015 -- as are concurrent types.
3017 if Is_Aliased (T)
3018 or else Has_Controlled_Component (T)
3019 or else Is_Concurrent_Type (T)
3020 or else Is_Tagged_Type (T)
3021 or else Is_Controlled (T)
3022 then
3023 return False;
3024 end if;
3026 -- If this is an element of a packed array, may be unaligned
3028 if Is_Ref_To_Bit_Packed_Array (N) then
3029 return True;
3030 end if;
3032 -- Case of component reference
3034 if Nkind (N) = N_Selected_Component then
3035 declare
3036 P : constant Node_Id := Prefix (N);
3037 C : constant Entity_Id := Entity (Selector_Name (N));
3038 M : Nat;
3039 S : Nat;
3041 begin
3042 -- If component reference is for an array with non-static bounds,
3043 -- then it is always aligned: we can only process unaligned
3044 -- arrays with static bounds (more accurately bounds known at
3045 -- compile time).
3047 if Is_Array_Type (T)
3048 and then not Compile_Time_Known_Bounds (T)
3049 then
3050 return False;
3051 end if;
3053 -- If component is aliased, it is definitely properly aligned
3055 if Is_Aliased (C) then
3056 return False;
3057 end if;
3059 -- If component is for a type implemented as a scalar, and the
3060 -- record is packed, and the component is other than the first
3061 -- component of the record, then the component may be unaligned.
3063 if Is_Packed (Etype (P))
3064 and then Represented_As_Scalar (Etype (C))
3065 and then First_Entity (Scope (C)) /= C
3066 then
3067 return True;
3068 end if;
3070 -- Compute maximum possible alignment for T
3072 -- If alignment is known, then that settles things
3074 if Known_Alignment (T) then
3075 M := UI_To_Int (Alignment (T));
3077 -- If alignment is not known, tentatively set max alignment
3079 else
3080 M := Ttypes.Maximum_Alignment;
3082 -- We can reduce this if the Esize is known since the default
3083 -- alignment will never be more than the smallest power of 2
3084 -- that does not exceed this Esize value.
3086 if Known_Esize (T) then
3087 S := UI_To_Int (Esize (T));
3089 while (M / 2) >= S loop
3090 M := M / 2;
3091 end loop;
3092 end if;
3093 end if;
3095 -- If the component reference is for a record that has a specified
3096 -- alignment, and we either know it is too small, or cannot tell,
3097 -- then the component may be unaligned
3099 if Known_Alignment (Etype (P))
3100 and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
3101 and then M > Alignment (Etype (P))
3102 then
3103 return True;
3104 end if;
3106 -- Case of component clause present which may specify an
3107 -- unaligned position.
3109 if Present (Component_Clause (C)) then
3111 -- Otherwise we can do a test to make sure that the actual
3112 -- start position in the record, and the length, are both
3113 -- consistent with the required alignment. If not, we know
3114 -- that we are unaligned.
3116 declare
3117 Align_In_Bits : constant Nat := M * System_Storage_Unit;
3118 begin
3119 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
3120 or else Esize (C) mod Align_In_Bits /= 0
3121 then
3122 return True;
3123 end if;
3124 end;
3125 end if;
3127 -- Otherwise, for a component reference, test prefix
3129 return Is_Possibly_Unaligned_Object (P);
3130 end;
3132 -- If not a component reference, must be aligned
3134 else
3135 return False;
3136 end if;
3137 end Is_Possibly_Unaligned_Object;
3139 ---------------------------------
3140 -- Is_Possibly_Unaligned_Slice --
3141 ---------------------------------
3143 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
3144 begin
3145 -- Go to renamed object
3147 if Is_Entity_Name (N)
3148 and then Is_Object (Entity (N))
3149 and then Present (Renamed_Object (Entity (N)))
3150 then
3151 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
3152 end if;
3154 -- The reference must be a slice
3156 if Nkind (N) /= N_Slice then
3157 return False;
3158 end if;
3160 -- Always assume the worst for a nested record component with a
3161 -- component clause, which gigi/gcc does not appear to handle well.
3162 -- It is not clear why this special test is needed at all ???
3164 if Nkind (Prefix (N)) = N_Selected_Component
3165 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
3166 and then
3167 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
3168 then
3169 return True;
3170 end if;
3172 -- We only need to worry if the target has strict alignment
3174 if not Target_Strict_Alignment then
3175 return False;
3176 end if;
3178 -- If it is a slice, then look at the array type being sliced
3180 declare
3181 Sarr : constant Node_Id := Prefix (N);
3182 -- Prefix of the slice, i.e. the array being sliced
3184 Styp : constant Entity_Id := Etype (Prefix (N));
3185 -- Type of the array being sliced
3187 Pref : Node_Id;
3188 Ptyp : Entity_Id;
3190 begin
3191 -- The problems arise if the array object that is being sliced
3192 -- is a component of a record or array, and we cannot guarantee
3193 -- the alignment of the array within its containing object.
3195 -- To investigate this, we look at successive prefixes to see
3196 -- if we have a worrisome indexed or selected component.
3198 Pref := Sarr;
3199 loop
3200 -- Case of array is part of an indexed component reference
3202 if Nkind (Pref) = N_Indexed_Component then
3203 Ptyp := Etype (Prefix (Pref));
3205 -- The only problematic case is when the array is packed,
3206 -- in which case we really know nothing about the alignment
3207 -- of individual components.
3209 if Is_Bit_Packed_Array (Ptyp) then
3210 return True;
3211 end if;
3213 -- Case of array is part of a selected component reference
3215 elsif Nkind (Pref) = N_Selected_Component then
3216 Ptyp := Etype (Prefix (Pref));
3218 -- We are definitely in trouble if the record in question
3219 -- has an alignment, and either we know this alignment is
3220 -- inconsistent with the alignment of the slice, or we
3221 -- don't know what the alignment of the slice should be.
3223 if Known_Alignment (Ptyp)
3224 and then (Unknown_Alignment (Styp)
3225 or else Alignment (Styp) > Alignment (Ptyp))
3226 then
3227 return True;
3228 end if;
3230 -- We are in potential trouble if the record type is packed.
3231 -- We could special case when we know that the array is the
3232 -- first component, but that's not such a simple case ???
3234 if Is_Packed (Ptyp) then
3235 return True;
3236 end if;
3238 -- We are in trouble if there is a component clause, and
3239 -- either we do not know the alignment of the slice, or
3240 -- the alignment of the slice is inconsistent with the
3241 -- bit position specified by the component clause.
3243 declare
3244 Field : constant Entity_Id := Entity (Selector_Name (Pref));
3245 begin
3246 if Present (Component_Clause (Field))
3247 and then
3248 (Unknown_Alignment (Styp)
3249 or else
3250 (Component_Bit_Offset (Field) mod
3251 (System_Storage_Unit * Alignment (Styp))) /= 0)
3252 then
3253 return True;
3254 end if;
3255 end;
3257 -- For cases other than selected or indexed components we
3258 -- know we are OK, since no issues arise over alignment.
3260 else
3261 return False;
3262 end if;
3264 -- We processed an indexed component or selected component
3265 -- reference that looked safe, so keep checking prefixes.
3267 Pref := Prefix (Pref);
3268 end loop;
3269 end;
3270 end Is_Possibly_Unaligned_Slice;
3272 --------------------------------
3273 -- Is_Ref_To_Bit_Packed_Array --
3274 --------------------------------
3276 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
3277 Result : Boolean;
3278 Expr : Node_Id;
3280 begin
3281 if Is_Entity_Name (N)
3282 and then Is_Object (Entity (N))
3283 and then Present (Renamed_Object (Entity (N)))
3284 then
3285 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
3286 end if;
3288 if Nkind (N) = N_Indexed_Component
3289 or else
3290 Nkind (N) = N_Selected_Component
3291 then
3292 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
3293 Result := True;
3294 else
3295 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
3296 end if;
3298 if Result and then Nkind (N) = N_Indexed_Component then
3299 Expr := First (Expressions (N));
3300 while Present (Expr) loop
3301 Force_Evaluation (Expr);
3302 Next (Expr);
3303 end loop;
3304 end if;
3306 return Result;
3308 else
3309 return False;
3310 end if;
3311 end Is_Ref_To_Bit_Packed_Array;
3313 --------------------------------
3314 -- Is_Ref_To_Bit_Packed_Slice --
3315 --------------------------------
3317 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
3318 begin
3319 if Nkind (N) = N_Type_Conversion then
3320 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
3322 elsif Is_Entity_Name (N)
3323 and then Is_Object (Entity (N))
3324 and then Present (Renamed_Object (Entity (N)))
3325 then
3326 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
3328 elsif Nkind (N) = N_Slice
3329 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
3330 then
3331 return True;
3333 elsif Nkind (N) = N_Indexed_Component
3334 or else
3335 Nkind (N) = N_Selected_Component
3336 then
3337 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
3339 else
3340 return False;
3341 end if;
3342 end Is_Ref_To_Bit_Packed_Slice;
3344 -----------------------
3345 -- Is_Renamed_Object --
3346 -----------------------
3348 function Is_Renamed_Object (N : Node_Id) return Boolean is
3349 Pnod : constant Node_Id := Parent (N);
3350 Kind : constant Node_Kind := Nkind (Pnod);
3351 begin
3352 if Kind = N_Object_Renaming_Declaration then
3353 return True;
3354 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
3355 return Is_Renamed_Object (Pnod);
3356 else
3357 return False;
3358 end if;
3359 end Is_Renamed_Object;
3361 ----------------------------
3362 -- Is_Untagged_Derivation --
3363 ----------------------------
3365 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
3366 begin
3367 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
3368 or else
3369 (Is_Private_Type (T) and then Present (Full_View (T))
3370 and then not Is_Tagged_Type (Full_View (T))
3371 and then Is_Derived_Type (Full_View (T))
3372 and then Etype (Full_View (T)) /= T);
3373 end Is_Untagged_Derivation;
3375 ---------------------------
3376 -- Is_Volatile_Reference --
3377 ---------------------------
3379 function Is_Volatile_Reference (N : Node_Id) return Boolean is
3380 begin
3381 if Nkind (N) in N_Has_Etype
3382 and then Present (Etype (N))
3383 and then Treat_As_Volatile (Etype (N))
3384 then
3385 return True;
3387 elsif Is_Entity_Name (N) then
3388 return Treat_As_Volatile (Entity (N));
3390 elsif Nkind (N) = N_Slice then
3391 return Is_Volatile_Reference (Prefix (N));
3393 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
3394 if (Is_Entity_Name (Prefix (N))
3395 and then Has_Volatile_Components (Entity (Prefix (N))))
3396 or else (Present (Etype (Prefix (N)))
3397 and then Has_Volatile_Components (Etype (Prefix (N))))
3398 then
3399 return True;
3400 else
3401 return Is_Volatile_Reference (Prefix (N));
3402 end if;
3404 else
3405 return False;
3406 end if;
3407 end Is_Volatile_Reference;
3409 --------------------
3410 -- Kill_Dead_Code --
3411 --------------------
3413 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
3414 begin
3415 if Present (N) then
3416 Remove_Warning_Messages (N);
3418 if Warn then
3419 Error_Msg_F
3420 ("?this code can never be executed and has been deleted!", N);
3421 end if;
3423 -- Recurse into block statements and bodies to process declarations
3424 -- and statements
3426 if Nkind (N) = N_Block_Statement
3427 or else Nkind (N) = N_Subprogram_Body
3428 or else Nkind (N) = N_Package_Body
3429 then
3430 Kill_Dead_Code (Declarations (N), False);
3431 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
3433 if Nkind (N) = N_Subprogram_Body then
3434 Set_Is_Eliminated (Defining_Entity (N));
3435 end if;
3437 elsif Nkind (N) = N_Package_Declaration then
3438 Kill_Dead_Code (Visible_Declarations (Specification (N)));
3439 Kill_Dead_Code (Private_Declarations (Specification (N)));
3441 -- ??? After this point, Delete_Tree has been called on all
3442 -- declarations in Specification (N), so references to
3443 -- entities therein look suspicious.
3445 declare
3446 E : Entity_Id := First_Entity (Defining_Entity (N));
3447 begin
3448 while Present (E) loop
3449 if Ekind (E) = E_Operator then
3450 Set_Is_Eliminated (E);
3451 end if;
3453 Next_Entity (E);
3454 end loop;
3455 end;
3457 -- Recurse into composite statement to kill individual statements,
3458 -- in particular instantiations.
3460 elsif Nkind (N) = N_If_Statement then
3461 Kill_Dead_Code (Then_Statements (N));
3462 Kill_Dead_Code (Elsif_Parts (N));
3463 Kill_Dead_Code (Else_Statements (N));
3465 elsif Nkind (N) = N_Loop_Statement then
3466 Kill_Dead_Code (Statements (N));
3468 elsif Nkind (N) = N_Case_Statement then
3469 declare
3470 Alt : Node_Id;
3471 begin
3472 Alt := First (Alternatives (N));
3473 while Present (Alt) loop
3474 Kill_Dead_Code (Statements (Alt));
3475 Next (Alt);
3476 end loop;
3477 end;
3479 elsif Nkind (N) = N_Case_Statement_Alternative then
3480 Kill_Dead_Code (Statements (N));
3482 -- Deal with dead instances caused by deleting instantiations
3484 elsif Nkind (N) in N_Generic_Instantiation then
3485 Remove_Dead_Instance (N);
3486 end if;
3487 end if;
3488 end Kill_Dead_Code;
3490 -- Case where argument is a list of nodes to be killed
3492 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
3493 N : Node_Id;
3494 W : Boolean;
3495 begin
3496 W := Warn;
3497 if Is_Non_Empty_List (L) then
3498 N := First (L);
3499 while Present (N) loop
3500 Kill_Dead_Code (N, W);
3501 W := False;
3502 Next (N);
3503 end loop;
3504 end if;
3505 end Kill_Dead_Code;
3507 ------------------------
3508 -- Known_Non_Negative --
3509 ------------------------
3511 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
3512 begin
3513 if Is_OK_Static_Expression (Opnd)
3514 and then Expr_Value (Opnd) >= 0
3515 then
3516 return True;
3518 else
3519 declare
3520 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
3522 begin
3523 return
3524 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
3525 end;
3526 end if;
3527 end Known_Non_Negative;
3529 --------------------
3530 -- Known_Non_Null --
3531 --------------------
3533 function Known_Non_Null (N : Node_Id) return Boolean is
3534 begin
3535 -- Checks for case where N is an entity reference
3537 if Is_Entity_Name (N) and then Present (Entity (N)) then
3538 declare
3539 E : constant Entity_Id := Entity (N);
3540 Op : Node_Kind;
3541 Val : Node_Id;
3543 begin
3544 -- First check if we are in decisive conditional
3546 Get_Current_Value_Condition (N, Op, Val);
3548 if Known_Null (Val) then
3549 if Op = N_Op_Eq then
3550 return False;
3551 elsif Op = N_Op_Ne then
3552 return True;
3553 end if;
3554 end if;
3556 -- If OK to do replacement, test Is_Known_Non_Null flag
3558 if OK_To_Do_Constant_Replacement (E) then
3559 return Is_Known_Non_Null (E);
3561 -- Otherwise if not safe to do replacement, then say so
3563 else
3564 return False;
3565 end if;
3566 end;
3568 -- True if access attribute
3570 elsif Nkind (N) = N_Attribute_Reference
3571 and then (Attribute_Name (N) = Name_Access
3572 or else
3573 Attribute_Name (N) = Name_Unchecked_Access
3574 or else
3575 Attribute_Name (N) = Name_Unrestricted_Access)
3576 then
3577 return True;
3579 -- True if allocator
3581 elsif Nkind (N) = N_Allocator then
3582 return True;
3584 -- For a conversion, true if expression is known non-null
3586 elsif Nkind (N) = N_Type_Conversion then
3587 return Known_Non_Null (Expression (N));
3589 -- Above are all cases where the value could be determined to be
3590 -- non-null. In all other cases, we don't know, so return False.
3592 else
3593 return False;
3594 end if;
3595 end Known_Non_Null;
3597 ----------------
3598 -- Known_Null --
3599 ----------------
3601 function Known_Null (N : Node_Id) return Boolean is
3602 begin
3603 -- Checks for case where N is an entity reference
3605 if Is_Entity_Name (N) and then Present (Entity (N)) then
3606 declare
3607 E : constant Entity_Id := Entity (N);
3608 Op : Node_Kind;
3609 Val : Node_Id;
3611 begin
3612 -- Constant null value is for sure null
3614 if Ekind (E) = E_Constant
3615 and then Known_Null (Constant_Value (E))
3616 then
3617 return True;
3618 end if;
3620 -- First check if we are in decisive conditional
3622 Get_Current_Value_Condition (N, Op, Val);
3624 if Known_Null (Val) then
3625 if Op = N_Op_Eq then
3626 return True;
3627 elsif Op = N_Op_Ne then
3628 return False;
3629 end if;
3630 end if;
3632 -- If OK to do replacement, test Is_Known_Null flag
3634 if OK_To_Do_Constant_Replacement (E) then
3635 return Is_Known_Null (E);
3637 -- Otherwise if not safe to do replacement, then say so
3639 else
3640 return False;
3641 end if;
3642 end;
3644 -- True if explicit reference to null
3646 elsif Nkind (N) = N_Null then
3647 return True;
3649 -- For a conversion, true if expression is known null
3651 elsif Nkind (N) = N_Type_Conversion then
3652 return Known_Null (Expression (N));
3654 -- Above are all cases where the value could be determined to be null.
3655 -- In all other cases, we don't know, so return False.
3657 else
3658 return False;
3659 end if;
3660 end Known_Null;
3662 -----------------------------
3663 -- Make_CW_Equivalent_Type --
3664 -----------------------------
3666 -- Create a record type used as an equivalent of any member of the class
3667 -- which takes its size from exp.
3669 -- Generate the following code:
3671 -- type Equiv_T is record
3672 -- _parent : T (List of discriminant constraints taken from Exp);
3673 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
3674 -- end Equiv_T;
3676 -- ??? Note that this type does not guarantee same alignment as all
3677 -- derived types
3679 function Make_CW_Equivalent_Type
3680 (T : Entity_Id;
3681 E : Node_Id) return Entity_Id
3683 Loc : constant Source_Ptr := Sloc (E);
3684 Root_Typ : constant Entity_Id := Root_Type (T);
3685 List_Def : constant List_Id := Empty_List;
3686 Comp_List : constant List_Id := New_List;
3687 Equiv_Type : Entity_Id;
3688 Range_Type : Entity_Id;
3689 Str_Type : Entity_Id;
3690 Constr_Root : Entity_Id;
3691 Sizexpr : Node_Id;
3693 begin
3694 if not Has_Discriminants (Root_Typ) then
3695 Constr_Root := Root_Typ;
3696 else
3697 Constr_Root :=
3698 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3700 -- subtype cstr__n is T (List of discr constraints taken from Exp)
3702 Append_To (List_Def,
3703 Make_Subtype_Declaration (Loc,
3704 Defining_Identifier => Constr_Root,
3705 Subtype_Indication =>
3706 Make_Subtype_From_Expr (E, Root_Typ)));
3707 end if;
3709 -- Generate the range subtype declaration
3711 Range_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
3713 if not Is_Interface (Root_Typ) then
3715 -- subtype rg__xx is
3716 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
3718 Sizexpr :=
3719 Make_Op_Subtract (Loc,
3720 Left_Opnd =>
3721 Make_Attribute_Reference (Loc,
3722 Prefix =>
3723 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3724 Attribute_Name => Name_Size),
3725 Right_Opnd =>
3726 Make_Attribute_Reference (Loc,
3727 Prefix => New_Reference_To (Constr_Root, Loc),
3728 Attribute_Name => Name_Object_Size));
3729 else
3730 -- subtype rg__xx is
3731 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
3733 Sizexpr :=
3734 Make_Attribute_Reference (Loc,
3735 Prefix =>
3736 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3737 Attribute_Name => Name_Size);
3738 end if;
3740 Set_Paren_Count (Sizexpr, 1);
3742 Append_To (List_Def,
3743 Make_Subtype_Declaration (Loc,
3744 Defining_Identifier => Range_Type,
3745 Subtype_Indication =>
3746 Make_Subtype_Indication (Loc,
3747 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
3748 Constraint => Make_Range_Constraint (Loc,
3749 Range_Expression =>
3750 Make_Range (Loc,
3751 Low_Bound => Make_Integer_Literal (Loc, 1),
3752 High_Bound =>
3753 Make_Op_Divide (Loc,
3754 Left_Opnd => Sizexpr,
3755 Right_Opnd => Make_Integer_Literal (Loc,
3756 Intval => System_Storage_Unit)))))));
3758 -- subtype str__nn is Storage_Array (rg__x);
3760 Str_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
3761 Append_To (List_Def,
3762 Make_Subtype_Declaration (Loc,
3763 Defining_Identifier => Str_Type,
3764 Subtype_Indication =>
3765 Make_Subtype_Indication (Loc,
3766 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
3767 Constraint =>
3768 Make_Index_Or_Discriminant_Constraint (Loc,
3769 Constraints =>
3770 New_List (New_Reference_To (Range_Type, Loc))))));
3772 -- type Equiv_T is record
3773 -- [ _parent : Tnn; ]
3774 -- E : Str_Type;
3775 -- end Equiv_T;
3777 Equiv_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3779 -- When the target requires front-end layout, it's necessary to allow
3780 -- the equivalent type to be frozen so that layout can occur (when the
3781 -- associated class-wide subtype is frozen, the equivalent type will
3782 -- be frozen, see freeze.adb). For other targets, Gigi wants to have
3783 -- the equivalent type marked as frozen and deals with this type itself.
3784 -- In the Gigi case this will also avoid the generation of an init
3785 -- procedure for the type.
3787 if not Frontend_Layout_On_Target then
3788 Set_Is_Frozen (Equiv_Type);
3789 end if;
3791 Set_Ekind (Equiv_Type, E_Record_Type);
3792 Set_Parent_Subtype (Equiv_Type, Constr_Root);
3794 if not Is_Interface (Root_Typ) then
3795 Append_To (Comp_List,
3796 Make_Component_Declaration (Loc,
3797 Defining_Identifier =>
3798 Make_Defining_Identifier (Loc, Name_uParent),
3799 Component_Definition =>
3800 Make_Component_Definition (Loc,
3801 Aliased_Present => False,
3802 Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
3803 end if;
3805 Append_To (Comp_List,
3806 Make_Component_Declaration (Loc,
3807 Defining_Identifier =>
3808 Make_Defining_Identifier (Loc,
3809 Chars => New_Internal_Name ('C')),
3810 Component_Definition =>
3811 Make_Component_Definition (Loc,
3812 Aliased_Present => False,
3813 Subtype_Indication => New_Reference_To (Str_Type, Loc))));
3815 Append_To (List_Def,
3816 Make_Full_Type_Declaration (Loc,
3817 Defining_Identifier => Equiv_Type,
3818 Type_Definition =>
3819 Make_Record_Definition (Loc,
3820 Component_List =>
3821 Make_Component_List (Loc,
3822 Component_Items => Comp_List,
3823 Variant_Part => Empty))));
3825 -- Suppress all checks during the analysis of the expanded code
3826 -- to avoid the generation of spurious warnings under ZFP run-time.
3828 Insert_Actions (E, List_Def, Suppress => All_Checks);
3829 return Equiv_Type;
3830 end Make_CW_Equivalent_Type;
3832 ------------------------
3833 -- Make_Literal_Range --
3834 ------------------------
3836 function Make_Literal_Range
3837 (Loc : Source_Ptr;
3838 Literal_Typ : Entity_Id) return Node_Id
3840 Lo : constant Node_Id :=
3841 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
3842 Index : constant Entity_Id := Etype (Lo);
3844 Hi : Node_Id;
3845 Length_Expr : constant Node_Id :=
3846 Make_Op_Subtract (Loc,
3847 Left_Opnd =>
3848 Make_Integer_Literal (Loc,
3849 Intval => String_Literal_Length (Literal_Typ)),
3850 Right_Opnd =>
3851 Make_Integer_Literal (Loc, 1));
3853 begin
3854 Set_Analyzed (Lo, False);
3856 if Is_Integer_Type (Index) then
3857 Hi :=
3858 Make_Op_Add (Loc,
3859 Left_Opnd => New_Copy_Tree (Lo),
3860 Right_Opnd => Length_Expr);
3861 else
3862 Hi :=
3863 Make_Attribute_Reference (Loc,
3864 Attribute_Name => Name_Val,
3865 Prefix => New_Occurrence_Of (Index, Loc),
3866 Expressions => New_List (
3867 Make_Op_Add (Loc,
3868 Left_Opnd =>
3869 Make_Attribute_Reference (Loc,
3870 Attribute_Name => Name_Pos,
3871 Prefix => New_Occurrence_Of (Index, Loc),
3872 Expressions => New_List (New_Copy_Tree (Lo))),
3873 Right_Opnd => Length_Expr)));
3874 end if;
3876 return
3877 Make_Range (Loc,
3878 Low_Bound => Lo,
3879 High_Bound => Hi);
3880 end Make_Literal_Range;
3882 --------------------------
3883 -- Make_Non_Empty_Check --
3884 --------------------------
3886 function Make_Non_Empty_Check
3887 (Loc : Source_Ptr;
3888 N : Node_Id) return Node_Id
3890 begin
3891 return
3892 Make_Op_Ne (Loc,
3893 Left_Opnd =>
3894 Make_Attribute_Reference (Loc,
3895 Attribute_Name => Name_Length,
3896 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
3897 Right_Opnd =>
3898 Make_Integer_Literal (Loc, 0));
3899 end Make_Non_Empty_Check;
3901 ----------------------------
3902 -- Make_Subtype_From_Expr --
3903 ----------------------------
3905 -- 1. If Expr is an unconstrained array expression, creates
3906 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
3908 -- 2. If Expr is a unconstrained discriminated type expression, creates
3909 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
3911 -- 3. If Expr is class-wide, creates an implicit class wide subtype
3913 function Make_Subtype_From_Expr
3914 (E : Node_Id;
3915 Unc_Typ : Entity_Id) return Node_Id
3917 Loc : constant Source_Ptr := Sloc (E);
3918 List_Constr : constant List_Id := New_List;
3919 D : Entity_Id;
3921 Full_Subtyp : Entity_Id;
3922 Priv_Subtyp : Entity_Id;
3923 Utyp : Entity_Id;
3924 Full_Exp : Node_Id;
3926 begin
3927 if Is_Private_Type (Unc_Typ)
3928 and then Has_Unknown_Discriminants (Unc_Typ)
3929 then
3930 -- Prepare the subtype completion, Go to base type to
3931 -- find underlying type, because the type may be a generic
3932 -- actual or an explicit subtype.
3934 Utyp := Underlying_Type (Base_Type (Unc_Typ));
3935 Full_Subtyp := Make_Defining_Identifier (Loc,
3936 New_Internal_Name ('C'));
3937 Full_Exp :=
3938 Unchecked_Convert_To
3939 (Utyp, Duplicate_Subexpr_No_Checks (E));
3940 Set_Parent (Full_Exp, Parent (E));
3942 Priv_Subtyp :=
3943 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3945 Insert_Action (E,
3946 Make_Subtype_Declaration (Loc,
3947 Defining_Identifier => Full_Subtyp,
3948 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
3950 -- Define the dummy private subtype
3952 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
3953 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
3954 Set_Scope (Priv_Subtyp, Full_Subtyp);
3955 Set_Is_Constrained (Priv_Subtyp);
3956 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
3957 Set_Is_Itype (Priv_Subtyp);
3958 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
3960 if Is_Tagged_Type (Priv_Subtyp) then
3961 Set_Class_Wide_Type
3962 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
3963 Set_Primitive_Operations (Priv_Subtyp,
3964 Primitive_Operations (Unc_Typ));
3965 end if;
3967 Set_Full_View (Priv_Subtyp, Full_Subtyp);
3969 return New_Reference_To (Priv_Subtyp, Loc);
3971 elsif Is_Array_Type (Unc_Typ) then
3972 for J in 1 .. Number_Dimensions (Unc_Typ) loop
3973 Append_To (List_Constr,
3974 Make_Range (Loc,
3975 Low_Bound =>
3976 Make_Attribute_Reference (Loc,
3977 Prefix => Duplicate_Subexpr_No_Checks (E),
3978 Attribute_Name => Name_First,
3979 Expressions => New_List (
3980 Make_Integer_Literal (Loc, J))),
3982 High_Bound =>
3983 Make_Attribute_Reference (Loc,
3984 Prefix => Duplicate_Subexpr_No_Checks (E),
3985 Attribute_Name => Name_Last,
3986 Expressions => New_List (
3987 Make_Integer_Literal (Loc, J)))));
3988 end loop;
3990 elsif Is_Class_Wide_Type (Unc_Typ) then
3991 declare
3992 CW_Subtype : Entity_Id;
3993 EQ_Typ : Entity_Id := Empty;
3995 begin
3996 -- A class-wide equivalent type is not needed when VM_Target
3997 -- because the VM back-ends handle the class-wide object
3998 -- initialization itself (and doesn't need or want the
3999 -- additional intermediate type to handle the assignment).
4001 if Expander_Active and then Tagged_Type_Expansion then
4002 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
4003 end if;
4005 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
4006 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
4008 if Present (EQ_Typ) then
4009 Set_Is_Class_Wide_Equivalent_Type (EQ_Typ);
4010 end if;
4012 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
4014 return New_Occurrence_Of (CW_Subtype, Loc);
4015 end;
4017 -- Indefinite record type with discriminants
4019 else
4020 D := First_Discriminant (Unc_Typ);
4021 while Present (D) loop
4022 Append_To (List_Constr,
4023 Make_Selected_Component (Loc,
4024 Prefix => Duplicate_Subexpr_No_Checks (E),
4025 Selector_Name => New_Reference_To (D, Loc)));
4027 Next_Discriminant (D);
4028 end loop;
4029 end if;
4031 return
4032 Make_Subtype_Indication (Loc,
4033 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
4034 Constraint =>
4035 Make_Index_Or_Discriminant_Constraint (Loc,
4036 Constraints => List_Constr));
4037 end Make_Subtype_From_Expr;
4039 -----------------------------
4040 -- May_Generate_Large_Temp --
4041 -----------------------------
4043 -- At the current time, the only types that we return False for (i.e.
4044 -- where we decide we know they cannot generate large temps) are ones
4045 -- where we know the size is 256 bits or less at compile time, and we
4046 -- are still not doing a thorough job on arrays and records ???
4048 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
4049 begin
4050 if not Size_Known_At_Compile_Time (Typ) then
4051 return False;
4053 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
4054 return False;
4056 elsif Is_Array_Type (Typ)
4057 and then Present (Packed_Array_Type (Typ))
4058 then
4059 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
4061 -- We could do more here to find other small types ???
4063 else
4064 return True;
4065 end if;
4066 end May_Generate_Large_Temp;
4068 ----------------------------
4069 -- New_Class_Wide_Subtype --
4070 ----------------------------
4072 function New_Class_Wide_Subtype
4073 (CW_Typ : Entity_Id;
4074 N : Node_Id) return Entity_Id
4076 Res : constant Entity_Id := Create_Itype (E_Void, N);
4077 Res_Name : constant Name_Id := Chars (Res);
4078 Res_Scope : constant Entity_Id := Scope (Res);
4080 begin
4081 Copy_Node (CW_Typ, Res);
4082 Set_Comes_From_Source (Res, False);
4083 Set_Sloc (Res, Sloc (N));
4084 Set_Is_Itype (Res);
4085 Set_Associated_Node_For_Itype (Res, N);
4086 Set_Is_Public (Res, False); -- By default, may be changed below.
4087 Set_Public_Status (Res);
4088 Set_Chars (Res, Res_Name);
4089 Set_Scope (Res, Res_Scope);
4090 Set_Ekind (Res, E_Class_Wide_Subtype);
4091 Set_Next_Entity (Res, Empty);
4092 Set_Etype (Res, Base_Type (CW_Typ));
4094 -- For targets where front-end layout is required, reset the Is_Frozen
4095 -- status of the subtype to False (it can be implicitly set to true
4096 -- from the copy of the class-wide type). For other targets, Gigi
4097 -- doesn't want the class-wide subtype to go through the freezing
4098 -- process (though it's unclear why that causes problems and it would
4099 -- be nice to allow freezing to occur normally for all targets ???).
4101 if Frontend_Layout_On_Target then
4102 Set_Is_Frozen (Res, False);
4103 end if;
4105 Set_Freeze_Node (Res, Empty);
4106 return (Res);
4107 end New_Class_Wide_Subtype;
4109 --------------------------------
4110 -- Non_Limited_Designated_Type --
4111 ---------------------------------
4113 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
4114 Desig : constant Entity_Id := Designated_Type (T);
4115 begin
4116 if Ekind (Desig) = E_Incomplete_Type
4117 and then Present (Non_Limited_View (Desig))
4118 then
4119 return Non_Limited_View (Desig);
4120 else
4121 return Desig;
4122 end if;
4123 end Non_Limited_Designated_Type;
4125 -----------------------------------
4126 -- OK_To_Do_Constant_Replacement --
4127 -----------------------------------
4129 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
4130 ES : constant Entity_Id := Scope (E);
4131 CS : Entity_Id;
4133 begin
4134 -- Do not replace statically allocated objects, because they may be
4135 -- modified outside the current scope.
4137 if Is_Statically_Allocated (E) then
4138 return False;
4140 -- Do not replace aliased or volatile objects, since we don't know what
4141 -- else might change the value.
4143 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
4144 return False;
4146 -- Debug flag -gnatdM disconnects this optimization
4148 elsif Debug_Flag_MM then
4149 return False;
4151 -- Otherwise check scopes
4153 else
4154 CS := Current_Scope;
4156 loop
4157 -- If we are in right scope, replacement is safe
4159 if CS = ES then
4160 return True;
4162 -- Packages do not affect the determination of safety
4164 elsif Ekind (CS) = E_Package then
4165 exit when CS = Standard_Standard;
4166 CS := Scope (CS);
4168 -- Blocks do not affect the determination of safety
4170 elsif Ekind (CS) = E_Block then
4171 CS := Scope (CS);
4173 -- Loops do not affect the determination of safety. Note that we
4174 -- kill all current values on entry to a loop, so we are just
4175 -- talking about processing within a loop here.
4177 elsif Ekind (CS) = E_Loop then
4178 CS := Scope (CS);
4180 -- Otherwise, the reference is dubious, and we cannot be sure that
4181 -- it is safe to do the replacement.
4183 else
4184 exit;
4185 end if;
4186 end loop;
4188 return False;
4189 end if;
4190 end OK_To_Do_Constant_Replacement;
4192 ------------------------------------
4193 -- Possible_Bit_Aligned_Component --
4194 ------------------------------------
4196 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
4197 begin
4198 case Nkind (N) is
4200 -- Case of indexed component
4202 when N_Indexed_Component =>
4203 declare
4204 P : constant Node_Id := Prefix (N);
4205 Ptyp : constant Entity_Id := Etype (P);
4207 begin
4208 -- If we know the component size and it is less than 64, then
4209 -- we are definitely OK. The back end always does assignment of
4210 -- misaligned small objects correctly.
4212 if Known_Static_Component_Size (Ptyp)
4213 and then Component_Size (Ptyp) <= 64
4214 then
4215 return False;
4217 -- Otherwise, we need to test the prefix, to see if we are
4218 -- indexing from a possibly unaligned component.
4220 else
4221 return Possible_Bit_Aligned_Component (P);
4222 end if;
4223 end;
4225 -- Case of selected component
4227 when N_Selected_Component =>
4228 declare
4229 P : constant Node_Id := Prefix (N);
4230 Comp : constant Entity_Id := Entity (Selector_Name (N));
4232 begin
4233 -- If there is no component clause, then we are in the clear
4234 -- since the back end will never misalign a large component
4235 -- unless it is forced to do so. In the clear means we need
4236 -- only the recursive test on the prefix.
4238 if Component_May_Be_Bit_Aligned (Comp) then
4239 return True;
4240 else
4241 return Possible_Bit_Aligned_Component (P);
4242 end if;
4243 end;
4245 -- For a slice, test the prefix, if that is possibly misaligned,
4246 -- then for sure the slice is!
4248 when N_Slice =>
4249 return Possible_Bit_Aligned_Component (Prefix (N));
4251 -- If we have none of the above, it means that we have fallen off the
4252 -- top testing prefixes recursively, and we now have a stand alone
4253 -- object, where we don't have a problem.
4255 when others =>
4256 return False;
4258 end case;
4259 end Possible_Bit_Aligned_Component;
4261 -------------------------
4262 -- Remove_Side_Effects --
4263 -------------------------
4265 procedure Remove_Side_Effects
4266 (Exp : Node_Id;
4267 Name_Req : Boolean := False;
4268 Variable_Ref : Boolean := False)
4270 Loc : constant Source_Ptr := Sloc (Exp);
4271 Exp_Type : constant Entity_Id := Etype (Exp);
4272 Svg_Suppress : constant Suppress_Array := Scope_Suppress;
4273 Def_Id : Entity_Id;
4274 Ref_Type : Entity_Id;
4275 Res : Node_Id;
4276 Ptr_Typ_Decl : Node_Id;
4277 New_Exp : Node_Id;
4278 E : Node_Id;
4280 function Side_Effect_Free (N : Node_Id) return Boolean;
4281 -- Determines if the tree N represents an expression that is known not
4282 -- to have side effects, and for which no processing is required.
4284 function Side_Effect_Free (L : List_Id) return Boolean;
4285 -- Determines if all elements of the list L are side effect free
4287 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
4288 -- The argument N is a construct where the Prefix is dereferenced if it
4289 -- is an access type and the result is a variable. The call returns True
4290 -- if the construct is side effect free (not considering side effects in
4291 -- other than the prefix which are to be tested by the caller).
4293 function Within_In_Parameter (N : Node_Id) return Boolean;
4294 -- Determines if N is a subcomponent of a composite in-parameter. If so,
4295 -- N is not side-effect free when the actual is global and modifiable
4296 -- indirectly from within a subprogram, because it may be passed by
4297 -- reference. The front-end must be conservative here and assume that
4298 -- this may happen with any array or record type. On the other hand, we
4299 -- cannot create temporaries for all expressions for which this
4300 -- condition is true, for various reasons that might require clearing up
4301 -- ??? For example, discriminant references that appear out of place, or
4302 -- spurious type errors with class-wide expressions. As a result, we
4303 -- limit the transformation to loop bounds, which is so far the only
4304 -- case that requires it.
4306 -----------------------------
4307 -- Safe_Prefixed_Reference --
4308 -----------------------------
4310 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
4311 begin
4312 -- If prefix is not side effect free, definitely not safe
4314 if not Side_Effect_Free (Prefix (N)) then
4315 return False;
4317 -- If the prefix is of an access type that is not access-to-constant,
4318 -- then this construct is a variable reference, which means it is to
4319 -- be considered to have side effects if Variable_Ref is set True
4320 -- Exception is an access to an entity that is a constant or an
4321 -- in-parameter which does not come from source, and is the result
4322 -- of a previous removal of side-effects.
4324 elsif Is_Access_Type (Etype (Prefix (N)))
4325 and then not Is_Access_Constant (Etype (Prefix (N)))
4326 and then Variable_Ref
4327 then
4328 if not Is_Entity_Name (Prefix (N)) then
4329 return False;
4330 else
4331 return Ekind (Entity (Prefix (N))) = E_Constant
4332 or else Ekind (Entity (Prefix (N))) = E_In_Parameter;
4333 end if;
4335 -- The following test is the simplest way of solving a complex
4336 -- problem uncovered by BB08-010: Side effect on loop bound that
4337 -- is a subcomponent of a global variable:
4338 -- If a loop bound is a subcomponent of a global variable, a
4339 -- modification of that variable within the loop may incorrectly
4340 -- affect the execution of the loop.
4342 elsif not
4343 (Nkind (Parent (Parent (N))) /= N_Loop_Parameter_Specification
4344 or else not Within_In_Parameter (Prefix (N)))
4345 then
4346 return False;
4348 -- All other cases are side effect free
4350 else
4351 return True;
4352 end if;
4353 end Safe_Prefixed_Reference;
4355 ----------------------
4356 -- Side_Effect_Free --
4357 ----------------------
4359 function Side_Effect_Free (N : Node_Id) return Boolean is
4360 begin
4361 -- Note on checks that could raise Constraint_Error. Strictly, if
4362 -- we take advantage of 11.6, these checks do not count as side
4363 -- effects. However, we would just as soon consider that they are
4364 -- side effects, since the backend CSE does not work very well on
4365 -- expressions which can raise Constraint_Error. On the other
4366 -- hand, if we do not consider them to be side effect free, then
4367 -- we get some awkward expansions in -gnato mode, resulting in
4368 -- code insertions at a point where we do not have a clear model
4369 -- for performing the insertions.
4371 -- Special handling for entity names
4373 if Is_Entity_Name (N) then
4375 -- If the entity is a constant, it is definitely side effect
4376 -- free. Note that the test of Is_Variable (N) below might
4377 -- be expected to catch this case, but it does not, because
4378 -- this test goes to the original tree, and we may have
4379 -- already rewritten a variable node with a constant as
4380 -- a result of an earlier Force_Evaluation call.
4382 if Ekind (Entity (N)) = E_Constant
4383 or else Ekind (Entity (N)) = E_In_Parameter
4384 then
4385 return True;
4387 -- Functions are not side effect free
4389 elsif Ekind (Entity (N)) = E_Function then
4390 return False;
4392 -- Variables are considered to be a side effect if Variable_Ref
4393 -- is set or if we have a volatile reference and Name_Req is off.
4394 -- If Name_Req is True then we can't help returning a name which
4395 -- effectively allows multiple references in any case.
4397 elsif Is_Variable (N) then
4398 return not Variable_Ref
4399 and then (not Is_Volatile_Reference (N) or else Name_Req);
4401 -- Any other entity (e.g. a subtype name) is definitely side
4402 -- effect free.
4404 else
4405 return True;
4406 end if;
4408 -- A value known at compile time is always side effect free
4410 elsif Compile_Time_Known_Value (N) then
4411 return True;
4413 -- A variable renaming is not side-effect free, because the
4414 -- renaming will function like a macro in the front-end in
4415 -- some cases, and an assignment can modify the component
4416 -- designated by N, so we need to create a temporary for it.
4418 elsif Is_Entity_Name (Original_Node (N))
4419 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
4420 and then Ekind (Entity (Original_Node (N))) /= E_Constant
4421 then
4422 return False;
4423 end if;
4425 -- For other than entity names and compile time known values,
4426 -- check the node kind for special processing.
4428 case Nkind (N) is
4430 -- An attribute reference is side effect free if its expressions
4431 -- are side effect free and its prefix is side effect free or
4432 -- is an entity reference.
4434 -- Is this right? what about x'first where x is a variable???
4436 when N_Attribute_Reference =>
4437 return Side_Effect_Free (Expressions (N))
4438 and then Attribute_Name (N) /= Name_Input
4439 and then (Is_Entity_Name (Prefix (N))
4440 or else Side_Effect_Free (Prefix (N)));
4442 -- A binary operator is side effect free if and both operands
4443 -- are side effect free. For this purpose binary operators
4444 -- include membership tests and short circuit forms
4446 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
4447 return Side_Effect_Free (Left_Opnd (N))
4448 and then
4449 Side_Effect_Free (Right_Opnd (N));
4451 -- An explicit dereference is side effect free only if it is
4452 -- a side effect free prefixed reference.
4454 when N_Explicit_Dereference =>
4455 return Safe_Prefixed_Reference (N);
4457 -- A call to _rep_to_pos is side effect free, since we generate
4458 -- this pure function call ourselves. Moreover it is critically
4459 -- important to make this exception, since otherwise we can
4460 -- have discriminants in array components which don't look
4461 -- side effect free in the case of an array whose index type
4462 -- is an enumeration type with an enumeration rep clause.
4464 -- All other function calls are not side effect free
4466 when N_Function_Call =>
4467 return Nkind (Name (N)) = N_Identifier
4468 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
4469 and then
4470 Side_Effect_Free (First (Parameter_Associations (N)));
4472 -- An indexed component is side effect free if it is a side
4473 -- effect free prefixed reference and all the indexing
4474 -- expressions are side effect free.
4476 when N_Indexed_Component =>
4477 return Side_Effect_Free (Expressions (N))
4478 and then Safe_Prefixed_Reference (N);
4480 -- A type qualification is side effect free if the expression
4481 -- is side effect free.
4483 when N_Qualified_Expression =>
4484 return Side_Effect_Free (Expression (N));
4486 -- A selected component is side effect free only if it is a
4487 -- side effect free prefixed reference. If it designates a
4488 -- component with a rep. clause it must be treated has having
4489 -- a potential side effect, because it may be modified through
4490 -- a renaming, and a subsequent use of the renaming as a macro
4491 -- will yield the wrong value. This complex interaction between
4492 -- renaming and removing side effects is a reminder that the
4493 -- latter has become a headache to maintain, and that it should
4494 -- be removed in favor of the gcc mechanism to capture values ???
4496 when N_Selected_Component =>
4497 if Nkind (Parent (N)) = N_Explicit_Dereference
4498 and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
4499 then
4500 return False;
4501 else
4502 return Safe_Prefixed_Reference (N);
4503 end if;
4505 -- A range is side effect free if the bounds are side effect free
4507 when N_Range =>
4508 return Side_Effect_Free (Low_Bound (N))
4509 and then Side_Effect_Free (High_Bound (N));
4511 -- A slice is side effect free if it is a side effect free
4512 -- prefixed reference and the bounds are side effect free.
4514 when N_Slice =>
4515 return Side_Effect_Free (Discrete_Range (N))
4516 and then Safe_Prefixed_Reference (N);
4518 -- A type conversion is side effect free if the expression to be
4519 -- converted is side effect free.
4521 when N_Type_Conversion =>
4522 return Side_Effect_Free (Expression (N));
4524 -- A unary operator is side effect free if the operand
4525 -- is side effect free.
4527 when N_Unary_Op =>
4528 return Side_Effect_Free (Right_Opnd (N));
4530 -- An unchecked type conversion is side effect free only if it
4531 -- is safe and its argument is side effect free.
4533 when N_Unchecked_Type_Conversion =>
4534 return Safe_Unchecked_Type_Conversion (N)
4535 and then Side_Effect_Free (Expression (N));
4537 -- An unchecked expression is side effect free if its expression
4538 -- is side effect free.
4540 when N_Unchecked_Expression =>
4541 return Side_Effect_Free (Expression (N));
4543 -- A literal is side effect free
4545 when N_Character_Literal |
4546 N_Integer_Literal |
4547 N_Real_Literal |
4548 N_String_Literal =>
4549 return True;
4551 -- We consider that anything else has side effects. This is a bit
4552 -- crude, but we are pretty close for most common cases, and we
4553 -- are certainly correct (i.e. we never return True when the
4554 -- answer should be False).
4556 when others =>
4557 return False;
4558 end case;
4559 end Side_Effect_Free;
4561 -- A list is side effect free if all elements of the list are
4562 -- side effect free.
4564 function Side_Effect_Free (L : List_Id) return Boolean is
4565 N : Node_Id;
4567 begin
4568 if L = No_List or else L = Error_List then
4569 return True;
4571 else
4572 N := First (L);
4573 while Present (N) loop
4574 if not Side_Effect_Free (N) then
4575 return False;
4576 else
4577 Next (N);
4578 end if;
4579 end loop;
4581 return True;
4582 end if;
4583 end Side_Effect_Free;
4585 -------------------------
4586 -- Within_In_Parameter --
4587 -------------------------
4589 function Within_In_Parameter (N : Node_Id) return Boolean is
4590 begin
4591 if not Comes_From_Source (N) then
4592 return False;
4594 elsif Is_Entity_Name (N) then
4595 return Ekind (Entity (N)) = E_In_Parameter;
4597 elsif Nkind (N) = N_Indexed_Component
4598 or else Nkind (N) = N_Selected_Component
4599 then
4600 return Within_In_Parameter (Prefix (N));
4601 else
4603 return False;
4604 end if;
4605 end Within_In_Parameter;
4607 -- Start of processing for Remove_Side_Effects
4609 begin
4610 -- If we are side effect free already or expansion is disabled,
4611 -- there is nothing to do.
4613 if Side_Effect_Free (Exp) or else not Expander_Active then
4614 return;
4615 end if;
4617 -- All this must not have any checks
4619 Scope_Suppress := (others => True);
4621 -- If it is a scalar type and we need to capture the value, just make
4622 -- a copy. Likewise for a function call, an attribute reference or an
4623 -- operator. And if we have a volatile reference and Name_Req is not
4624 -- set (see comments above for Side_Effect_Free).
4626 if Is_Elementary_Type (Exp_Type)
4627 and then (Variable_Ref
4628 or else Nkind (Exp) = N_Function_Call
4629 or else Nkind (Exp) = N_Attribute_Reference
4630 or else Nkind (Exp) in N_Op
4631 or else (not Name_Req and then Is_Volatile_Reference (Exp)))
4632 then
4633 Def_Id := Make_Temporary (Loc, 'R', Exp);
4634 Set_Etype (Def_Id, Exp_Type);
4635 Res := New_Reference_To (Def_Id, Loc);
4637 E :=
4638 Make_Object_Declaration (Loc,
4639 Defining_Identifier => Def_Id,
4640 Object_Definition => New_Reference_To (Exp_Type, Loc),
4641 Constant_Present => True,
4642 Expression => Relocate_Node (Exp));
4644 -- Check if the previous node relocation requires readjustment of
4645 -- some SCIL Dispatching node.
4647 if Generate_SCIL
4648 and then Nkind (Exp) = N_Function_Call
4649 then
4650 Adjust_SCIL_Node (Exp, Expression (E));
4651 end if;
4653 Set_Assignment_OK (E);
4654 Insert_Action (Exp, E);
4656 -- If the expression has the form v.all then we can just capture
4657 -- the pointer, and then do an explicit dereference on the result.
4659 elsif Nkind (Exp) = N_Explicit_Dereference then
4660 Def_Id := Make_Temporary (Loc, 'R', Exp);
4661 Res :=
4662 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
4664 Insert_Action (Exp,
4665 Make_Object_Declaration (Loc,
4666 Defining_Identifier => Def_Id,
4667 Object_Definition =>
4668 New_Reference_To (Etype (Prefix (Exp)), Loc),
4669 Constant_Present => True,
4670 Expression => Relocate_Node (Prefix (Exp))));
4672 -- Similar processing for an unchecked conversion of an expression
4673 -- of the form v.all, where we want the same kind of treatment.
4675 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4676 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
4677 then
4678 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4679 Scope_Suppress := Svg_Suppress;
4680 return;
4682 -- If this is a type conversion, leave the type conversion and remove
4683 -- the side effects in the expression. This is important in several
4684 -- circumstances: for change of representations, and also when this is
4685 -- a view conversion to a smaller object, where gigi can end up creating
4686 -- its own temporary of the wrong size.
4688 elsif Nkind (Exp) = N_Type_Conversion then
4689 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4690 Scope_Suppress := Svg_Suppress;
4691 return;
4693 -- If this is an unchecked conversion that Gigi can't handle, make
4694 -- a copy or a use a renaming to capture the value.
4696 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4697 and then not Safe_Unchecked_Type_Conversion (Exp)
4698 then
4699 if CW_Or_Has_Controlled_Part (Exp_Type) then
4701 -- Use a renaming to capture the expression, rather than create
4702 -- a controlled temporary.
4704 Def_Id := Make_Temporary (Loc, 'R', Exp);
4705 Res := New_Reference_To (Def_Id, Loc);
4707 Insert_Action (Exp,
4708 Make_Object_Renaming_Declaration (Loc,
4709 Defining_Identifier => Def_Id,
4710 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4711 Name => Relocate_Node (Exp)));
4713 else
4714 Def_Id := Make_Temporary (Loc, 'R', Exp);
4715 Set_Etype (Def_Id, Exp_Type);
4716 Res := New_Reference_To (Def_Id, Loc);
4718 E :=
4719 Make_Object_Declaration (Loc,
4720 Defining_Identifier => Def_Id,
4721 Object_Definition => New_Reference_To (Exp_Type, Loc),
4722 Constant_Present => not Is_Variable (Exp),
4723 Expression => Relocate_Node (Exp));
4725 Set_Assignment_OK (E);
4726 Insert_Action (Exp, E);
4727 end if;
4729 -- For expressions that denote objects, we can use a renaming scheme.
4730 -- We skip using this if we have a volatile reference and we do not
4731 -- have Name_Req set true (see comments above for Side_Effect_Free).
4733 elsif Is_Object_Reference (Exp)
4734 and then Nkind (Exp) /= N_Function_Call
4735 and then (Name_Req or else not Is_Volatile_Reference (Exp))
4736 then
4737 Def_Id := Make_Temporary (Loc, 'R', Exp);
4739 if Nkind (Exp) = N_Selected_Component
4740 and then Nkind (Prefix (Exp)) = N_Function_Call
4741 and then Is_Array_Type (Exp_Type)
4742 then
4743 -- Avoid generating a variable-sized temporary, by generating
4744 -- the renaming declaration just for the function call. The
4745 -- transformation could be refined to apply only when the array
4746 -- component is constrained by a discriminant???
4748 Res :=
4749 Make_Selected_Component (Loc,
4750 Prefix => New_Occurrence_Of (Def_Id, Loc),
4751 Selector_Name => Selector_Name (Exp));
4753 Insert_Action (Exp,
4754 Make_Object_Renaming_Declaration (Loc,
4755 Defining_Identifier => Def_Id,
4756 Subtype_Mark =>
4757 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
4758 Name => Relocate_Node (Prefix (Exp))));
4760 else
4761 Res := New_Reference_To (Def_Id, Loc);
4763 Insert_Action (Exp,
4764 Make_Object_Renaming_Declaration (Loc,
4765 Defining_Identifier => Def_Id,
4766 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
4767 Name => Relocate_Node (Exp)));
4768 end if;
4770 -- If this is a packed reference, or a selected component with a
4771 -- non-standard representation, a reference to the temporary will
4772 -- be replaced by a copy of the original expression (see
4773 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
4774 -- elaborated by gigi, and is of course not to be replaced in-line
4775 -- by the expression it renames, which would defeat the purpose of
4776 -- removing the side-effect.
4778 if (Nkind (Exp) = N_Selected_Component
4779 or else Nkind (Exp) = N_Indexed_Component)
4780 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
4781 then
4782 null;
4783 else
4784 Set_Is_Renaming_Of_Object (Def_Id, False);
4785 end if;
4787 -- Otherwise we generate a reference to the value
4789 else
4790 -- Special processing for function calls that return a limited type.
4791 -- We need to build a declaration that will enable build-in-place
4792 -- expansion of the call. This is not done if the context is already
4793 -- an object declaration, to prevent infinite recursion.
4795 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
4796 -- to accommodate functions returning limited objects by reference.
4798 if Nkind (Exp) = N_Function_Call
4799 and then Is_Inherently_Limited_Type (Etype (Exp))
4800 and then Nkind (Parent (Exp)) /= N_Object_Declaration
4801 and then Ada_Version >= Ada_05
4802 then
4803 declare
4804 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
4805 Decl : Node_Id;
4807 begin
4808 Decl :=
4809 Make_Object_Declaration (Loc,
4810 Defining_Identifier => Obj,
4811 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
4812 Expression => Relocate_Node (Exp));
4814 -- Check if the previous node relocation requires readjustment
4815 -- of some SCIL Dispatching node.
4817 if Generate_SCIL
4818 and then Nkind (Exp) = N_Function_Call
4819 then
4820 Adjust_SCIL_Node (Exp, Expression (Decl));
4821 end if;
4823 Insert_Action (Exp, Decl);
4824 Set_Etype (Obj, Exp_Type);
4825 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
4826 return;
4827 end;
4828 end if;
4830 Ref_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4832 Ptr_Typ_Decl :=
4833 Make_Full_Type_Declaration (Loc,
4834 Defining_Identifier => Ref_Type,
4835 Type_Definition =>
4836 Make_Access_To_Object_Definition (Loc,
4837 All_Present => True,
4838 Subtype_Indication =>
4839 New_Reference_To (Exp_Type, Loc)));
4841 E := Exp;
4842 Insert_Action (Exp, Ptr_Typ_Decl);
4844 Def_Id := Make_Temporary (Loc, 'R', Exp);
4845 Set_Etype (Def_Id, Exp_Type);
4847 Res :=
4848 Make_Explicit_Dereference (Loc,
4849 Prefix => New_Reference_To (Def_Id, Loc));
4851 if Nkind (E) = N_Explicit_Dereference then
4852 New_Exp := Relocate_Node (Prefix (E));
4853 else
4854 E := Relocate_Node (E);
4855 New_Exp := Make_Reference (Loc, E);
4856 end if;
4858 if Is_Delayed_Aggregate (E) then
4860 -- The expansion of nested aggregates is delayed until the
4861 -- enclosing aggregate is expanded. As aggregates are often
4862 -- qualified, the predicate applies to qualified expressions
4863 -- as well, indicating that the enclosing aggregate has not
4864 -- been expanded yet. At this point the aggregate is part of
4865 -- a stand-alone declaration, and must be fully expanded.
4867 if Nkind (E) = N_Qualified_Expression then
4868 Set_Expansion_Delayed (Expression (E), False);
4869 Set_Analyzed (Expression (E), False);
4870 else
4871 Set_Expansion_Delayed (E, False);
4872 end if;
4874 Set_Analyzed (E, False);
4875 end if;
4877 Insert_Action (Exp,
4878 Make_Object_Declaration (Loc,
4879 Defining_Identifier => Def_Id,
4880 Object_Definition => New_Reference_To (Ref_Type, Loc),
4881 Expression => New_Exp));
4883 -- Check if the previous node relocation requires readjustment
4884 -- of some SCIL Dispatching node.
4886 if Generate_SCIL
4887 and then Nkind (Exp) = N_Function_Call
4888 then
4889 Adjust_SCIL_Node (Exp, Prefix (New_Exp));
4890 end if;
4891 end if;
4893 -- Preserve the Assignment_OK flag in all copies, since at least
4894 -- one copy may be used in a context where this flag must be set
4895 -- (otherwise why would the flag be set in the first place).
4897 Set_Assignment_OK (Res, Assignment_OK (Exp));
4899 -- Finally rewrite the original expression and we are done
4901 Rewrite (Exp, Res);
4902 Analyze_And_Resolve (Exp, Exp_Type);
4903 Scope_Suppress := Svg_Suppress;
4904 end Remove_Side_Effects;
4906 ---------------------------
4907 -- Represented_As_Scalar --
4908 ---------------------------
4910 function Represented_As_Scalar (T : Entity_Id) return Boolean is
4911 UT : constant Entity_Id := Underlying_Type (T);
4912 begin
4913 return Is_Scalar_Type (UT)
4914 or else (Is_Bit_Packed_Array (UT)
4915 and then Is_Scalar_Type (Packed_Array_Type (UT)));
4916 end Represented_As_Scalar;
4918 ------------------------------------
4919 -- Safe_Unchecked_Type_Conversion --
4920 ------------------------------------
4922 -- Note: this function knows quite a bit about the exact requirements
4923 -- of Gigi with respect to unchecked type conversions, and its code
4924 -- must be coordinated with any changes in Gigi in this area.
4926 -- The above requirements should be documented in Sinfo ???
4928 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
4929 Otyp : Entity_Id;
4930 Ityp : Entity_Id;
4931 Oalign : Uint;
4932 Ialign : Uint;
4933 Pexp : constant Node_Id := Parent (Exp);
4935 begin
4936 -- If the expression is the RHS of an assignment or object declaration
4937 -- we are always OK because there will always be a target.
4939 -- Object renaming declarations, (generated for view conversions of
4940 -- actuals in inlined calls), like object declarations, provide an
4941 -- explicit type, and are safe as well.
4943 if (Nkind (Pexp) = N_Assignment_Statement
4944 and then Expression (Pexp) = Exp)
4945 or else Nkind (Pexp) = N_Object_Declaration
4946 or else Nkind (Pexp) = N_Object_Renaming_Declaration
4947 then
4948 return True;
4950 -- If the expression is the prefix of an N_Selected_Component
4951 -- we should also be OK because GCC knows to look inside the
4952 -- conversion except if the type is discriminated. We assume
4953 -- that we are OK anyway if the type is not set yet or if it is
4954 -- controlled since we can't afford to introduce a temporary in
4955 -- this case.
4957 elsif Nkind (Pexp) = N_Selected_Component
4958 and then Prefix (Pexp) = Exp
4959 then
4960 if No (Etype (Pexp)) then
4961 return True;
4962 else
4963 return
4964 not Has_Discriminants (Etype (Pexp))
4965 or else Is_Constrained (Etype (Pexp));
4966 end if;
4967 end if;
4969 -- Set the output type, this comes from Etype if it is set, otherwise
4970 -- we take it from the subtype mark, which we assume was already
4971 -- fully analyzed.
4973 if Present (Etype (Exp)) then
4974 Otyp := Etype (Exp);
4975 else
4976 Otyp := Entity (Subtype_Mark (Exp));
4977 end if;
4979 -- The input type always comes from the expression, and we assume
4980 -- this is indeed always analyzed, so we can simply get the Etype.
4982 Ityp := Etype (Expression (Exp));
4984 -- Initialize alignments to unknown so far
4986 Oalign := No_Uint;
4987 Ialign := No_Uint;
4989 -- Replace a concurrent type by its corresponding record type
4990 -- and each type by its underlying type and do the tests on those.
4991 -- The original type may be a private type whose completion is a
4992 -- concurrent type, so find the underlying type first.
4994 if Present (Underlying_Type (Otyp)) then
4995 Otyp := Underlying_Type (Otyp);
4996 end if;
4998 if Present (Underlying_Type (Ityp)) then
4999 Ityp := Underlying_Type (Ityp);
5000 end if;
5002 if Is_Concurrent_Type (Otyp) then
5003 Otyp := Corresponding_Record_Type (Otyp);
5004 end if;
5006 if Is_Concurrent_Type (Ityp) then
5007 Ityp := Corresponding_Record_Type (Ityp);
5008 end if;
5010 -- If the base types are the same, we know there is no problem since
5011 -- this conversion will be a noop.
5013 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
5014 return True;
5016 -- Same if this is an upwards conversion of an untagged type, and there
5017 -- are no constraints involved (could be more general???)
5019 elsif Etype (Ityp) = Otyp
5020 and then not Is_Tagged_Type (Ityp)
5021 and then not Has_Discriminants (Ityp)
5022 and then No (First_Rep_Item (Base_Type (Ityp)))
5023 then
5024 return True;
5026 -- If the expression has an access type (object or subprogram) we
5027 -- assume that the conversion is safe, because the size of the target
5028 -- is safe, even if it is a record (which might be treated as having
5029 -- unknown size at this point).
5031 elsif Is_Access_Type (Ityp) then
5032 return True;
5034 -- If the size of output type is known at compile time, there is
5035 -- never a problem. Note that unconstrained records are considered
5036 -- to be of known size, but we can't consider them that way here,
5037 -- because we are talking about the actual size of the object.
5039 -- We also make sure that in addition to the size being known, we do
5040 -- not have a case which might generate an embarrassingly large temp
5041 -- in stack checking mode.
5043 elsif Size_Known_At_Compile_Time (Otyp)
5044 and then
5045 (not Stack_Checking_Enabled
5046 or else not May_Generate_Large_Temp (Otyp))
5047 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
5048 then
5049 return True;
5051 -- If either type is tagged, then we know the alignment is OK so
5052 -- Gigi will be able to use pointer punning.
5054 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
5055 return True;
5057 -- If either type is a limited record type, we cannot do a copy, so
5058 -- say safe since there's nothing else we can do.
5060 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
5061 return True;
5063 -- Conversions to and from packed array types are always ignored and
5064 -- hence are safe.
5066 elsif Is_Packed_Array_Type (Otyp)
5067 or else Is_Packed_Array_Type (Ityp)
5068 then
5069 return True;
5070 end if;
5072 -- The only other cases known to be safe is if the input type's
5073 -- alignment is known to be at least the maximum alignment for the
5074 -- target or if both alignments are known and the output type's
5075 -- alignment is no stricter than the input's. We can use the alignment
5076 -- of the component type of an array if a type is an unpacked
5077 -- array type.
5079 if Present (Alignment_Clause (Otyp)) then
5080 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
5082 elsif Is_Array_Type (Otyp)
5083 and then Present (Alignment_Clause (Component_Type (Otyp)))
5084 then
5085 Oalign := Expr_Value (Expression (Alignment_Clause
5086 (Component_Type (Otyp))));
5087 end if;
5089 if Present (Alignment_Clause (Ityp)) then
5090 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
5092 elsif Is_Array_Type (Ityp)
5093 and then Present (Alignment_Clause (Component_Type (Ityp)))
5094 then
5095 Ialign := Expr_Value (Expression (Alignment_Clause
5096 (Component_Type (Ityp))));
5097 end if;
5099 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
5100 return True;
5102 elsif Ialign /= No_Uint and then Oalign /= No_Uint
5103 and then Ialign <= Oalign
5104 then
5105 return True;
5107 -- Otherwise, Gigi cannot handle this and we must make a temporary
5109 else
5110 return False;
5111 end if;
5112 end Safe_Unchecked_Type_Conversion;
5114 ---------------------------------
5115 -- Set_Current_Value_Condition --
5116 ---------------------------------
5118 -- Note: the implementation of this procedure is very closely tied to the
5119 -- implementation of Get_Current_Value_Condition. Here we set required
5120 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
5121 -- them, so they must have a consistent view.
5123 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
5125 procedure Set_Entity_Current_Value (N : Node_Id);
5126 -- If N is an entity reference, where the entity is of an appropriate
5127 -- kind, then set the current value of this entity to Cnode, unless
5128 -- there is already a definite value set there.
5130 procedure Set_Expression_Current_Value (N : Node_Id);
5131 -- If N is of an appropriate form, sets an appropriate entry in current
5132 -- value fields of relevant entities. Multiple entities can be affected
5133 -- in the case of an AND or AND THEN.
5135 ------------------------------
5136 -- Set_Entity_Current_Value --
5137 ------------------------------
5139 procedure Set_Entity_Current_Value (N : Node_Id) is
5140 begin
5141 if Is_Entity_Name (N) then
5142 declare
5143 Ent : constant Entity_Id := Entity (N);
5145 begin
5146 -- Don't capture if not safe to do so
5148 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
5149 return;
5150 end if;
5152 -- Here we have a case where the Current_Value field may
5153 -- need to be set. We set it if it is not already set to a
5154 -- compile time expression value.
5156 -- Note that this represents a decision that one condition
5157 -- blots out another previous one. That's certainly right
5158 -- if they occur at the same level. If the second one is
5159 -- nested, then the decision is neither right nor wrong (it
5160 -- would be equally OK to leave the outer one in place, or
5161 -- take the new inner one. Really we should record both, but
5162 -- our data structures are not that elaborate.
5164 if Nkind (Current_Value (Ent)) not in N_Subexpr then
5165 Set_Current_Value (Ent, Cnode);
5166 end if;
5167 end;
5168 end if;
5169 end Set_Entity_Current_Value;
5171 ----------------------------------
5172 -- Set_Expression_Current_Value --
5173 ----------------------------------
5175 procedure Set_Expression_Current_Value (N : Node_Id) is
5176 Cond : Node_Id;
5178 begin
5179 Cond := N;
5181 -- Loop to deal with (ignore for now) any NOT operators present. The
5182 -- presence of NOT operators will be handled properly when we call
5183 -- Get_Current_Value_Condition.
5185 while Nkind (Cond) = N_Op_Not loop
5186 Cond := Right_Opnd (Cond);
5187 end loop;
5189 -- For an AND or AND THEN, recursively process operands
5191 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
5192 Set_Expression_Current_Value (Left_Opnd (Cond));
5193 Set_Expression_Current_Value (Right_Opnd (Cond));
5194 return;
5195 end if;
5197 -- Check possible relational operator
5199 if Nkind (Cond) in N_Op_Compare then
5200 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
5201 Set_Entity_Current_Value (Left_Opnd (Cond));
5202 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
5203 Set_Entity_Current_Value (Right_Opnd (Cond));
5204 end if;
5206 -- Check possible boolean variable reference
5208 else
5209 Set_Entity_Current_Value (Cond);
5210 end if;
5211 end Set_Expression_Current_Value;
5213 -- Start of processing for Set_Current_Value_Condition
5215 begin
5216 Set_Expression_Current_Value (Condition (Cnode));
5217 end Set_Current_Value_Condition;
5219 --------------------------
5220 -- Set_Elaboration_Flag --
5221 --------------------------
5223 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
5224 Loc : constant Source_Ptr := Sloc (N);
5225 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
5226 Asn : Node_Id;
5228 begin
5229 if Present (Ent) then
5231 -- Nothing to do if at the compilation unit level, because in this
5232 -- case the flag is set by the binder generated elaboration routine.
5234 if Nkind (Parent (N)) = N_Compilation_Unit then
5235 null;
5237 -- Here we do need to generate an assignment statement
5239 else
5240 Check_Restriction (No_Elaboration_Code, N);
5241 Asn :=
5242 Make_Assignment_Statement (Loc,
5243 Name => New_Occurrence_Of (Ent, Loc),
5244 Expression => New_Occurrence_Of (Standard_True, Loc));
5246 if Nkind (Parent (N)) = N_Subunit then
5247 Insert_After (Corresponding_Stub (Parent (N)), Asn);
5248 else
5249 Insert_After (N, Asn);
5250 end if;
5252 Analyze (Asn);
5254 -- Kill current value indication. This is necessary because the
5255 -- tests of this flag are inserted out of sequence and must not
5256 -- pick up bogus indications of the wrong constant value.
5258 Set_Current_Value (Ent, Empty);
5259 end if;
5260 end if;
5261 end Set_Elaboration_Flag;
5263 ----------------------------
5264 -- Set_Renamed_Subprogram --
5265 ----------------------------
5267 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
5268 begin
5269 -- If input node is an identifier, we can just reset it
5271 if Nkind (N) = N_Identifier then
5272 Set_Chars (N, Chars (E));
5273 Set_Entity (N, E);
5275 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
5277 else
5278 declare
5279 CS : constant Boolean := Comes_From_Source (N);
5280 begin
5281 Rewrite (N, Make_Identifier (Sloc (N), Chars => Chars (E)));
5282 Set_Entity (N, E);
5283 Set_Comes_From_Source (N, CS);
5284 Set_Analyzed (N, True);
5285 end;
5286 end if;
5287 end Set_Renamed_Subprogram;
5289 ----------------------------------
5290 -- Silly_Boolean_Array_Not_Test --
5291 ----------------------------------
5293 -- This procedure implements an odd and silly test. We explicitly check
5294 -- for the case where the 'First of the component type is equal to the
5295 -- 'Last of this component type, and if this is the case, we make sure
5296 -- that constraint error is raised. The reason is that the NOT is bound
5297 -- to cause CE in this case, and we will not otherwise catch it.
5299 -- No such check is required for AND and OR, since for both these cases
5300 -- False op False = False, and True op True = True. For the XOR case,
5301 -- see Silly_Boolean_Array_Xor_Test.
5303 -- Believe it or not, this was reported as a bug. Note that nearly
5304 -- always, the test will evaluate statically to False, so the code will
5305 -- be statically removed, and no extra overhead caused.
5307 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
5308 Loc : constant Source_Ptr := Sloc (N);
5309 CT : constant Entity_Id := Component_Type (T);
5311 begin
5312 -- The check we install is
5314 -- constraint_error when
5315 -- component_type'first = component_type'last
5316 -- and then array_type'Length /= 0)
5318 -- We need the last guard because we don't want to raise CE for empty
5319 -- arrays since no out of range values result. (Empty arrays with a
5320 -- component type of True .. True -- very useful -- even the ACATS
5321 -- does not test that marginal case!)
5323 Insert_Action (N,
5324 Make_Raise_Constraint_Error (Loc,
5325 Condition =>
5326 Make_And_Then (Loc,
5327 Left_Opnd =>
5328 Make_Op_Eq (Loc,
5329 Left_Opnd =>
5330 Make_Attribute_Reference (Loc,
5331 Prefix => New_Occurrence_Of (CT, Loc),
5332 Attribute_Name => Name_First),
5334 Right_Opnd =>
5335 Make_Attribute_Reference (Loc,
5336 Prefix => New_Occurrence_Of (CT, Loc),
5337 Attribute_Name => Name_Last)),
5339 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5340 Reason => CE_Range_Check_Failed));
5341 end Silly_Boolean_Array_Not_Test;
5343 ----------------------------------
5344 -- Silly_Boolean_Array_Xor_Test --
5345 ----------------------------------
5347 -- This procedure implements an odd and silly test. We explicitly check
5348 -- for the XOR case where the component type is True .. True, since this
5349 -- will raise constraint error. A special check is required since CE
5350 -- will not be generated otherwise (cf Expand_Packed_Not).
5352 -- No such check is required for AND and OR, since for both these cases
5353 -- False op False = False, and True op True = True, and no check is
5354 -- required for the case of False .. False, since False xor False = False.
5355 -- See also Silly_Boolean_Array_Not_Test
5357 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
5358 Loc : constant Source_Ptr := Sloc (N);
5359 CT : constant Entity_Id := Component_Type (T);
5361 begin
5362 -- The check we install is
5364 -- constraint_error when
5365 -- Boolean (component_type'First)
5366 -- and then Boolean (component_type'Last)
5367 -- and then array_type'Length /= 0)
5369 -- We need the last guard because we don't want to raise CE for empty
5370 -- arrays since no out of range values result (Empty arrays with a
5371 -- component type of True .. True -- very useful -- even the ACATS
5372 -- does not test that marginal case!).
5374 Insert_Action (N,
5375 Make_Raise_Constraint_Error (Loc,
5376 Condition =>
5377 Make_And_Then (Loc,
5378 Left_Opnd =>
5379 Make_And_Then (Loc,
5380 Left_Opnd =>
5381 Convert_To (Standard_Boolean,
5382 Make_Attribute_Reference (Loc,
5383 Prefix => New_Occurrence_Of (CT, Loc),
5384 Attribute_Name => Name_First)),
5386 Right_Opnd =>
5387 Convert_To (Standard_Boolean,
5388 Make_Attribute_Reference (Loc,
5389 Prefix => New_Occurrence_Of (CT, Loc),
5390 Attribute_Name => Name_Last))),
5392 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5393 Reason => CE_Range_Check_Failed));
5394 end Silly_Boolean_Array_Xor_Test;
5396 --------------------------
5397 -- Target_Has_Fixed_Ops --
5398 --------------------------
5400 Integer_Sized_Small : Ureal;
5401 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this
5402 -- function is called (we don't want to compute it more than once!)
5404 Long_Integer_Sized_Small : Ureal;
5405 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
5406 -- function is called (we don't want to compute it more than once)
5408 First_Time_For_THFO : Boolean := True;
5409 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
5411 function Target_Has_Fixed_Ops
5412 (Left_Typ : Entity_Id;
5413 Right_Typ : Entity_Id;
5414 Result_Typ : Entity_Id) return Boolean
5416 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
5417 -- Return True if the given type is a fixed-point type with a small
5418 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
5419 -- an absolute value less than 1.0. This is currently limited
5420 -- to fixed-point types that map to Integer or Long_Integer.
5422 ------------------------
5423 -- Is_Fractional_Type --
5424 ------------------------
5426 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
5427 begin
5428 if Esize (Typ) = Standard_Integer_Size then
5429 return Small_Value (Typ) = Integer_Sized_Small;
5431 elsif Esize (Typ) = Standard_Long_Integer_Size then
5432 return Small_Value (Typ) = Long_Integer_Sized_Small;
5434 else
5435 return False;
5436 end if;
5437 end Is_Fractional_Type;
5439 -- Start of processing for Target_Has_Fixed_Ops
5441 begin
5442 -- Return False if Fractional_Fixed_Ops_On_Target is false
5444 if not Fractional_Fixed_Ops_On_Target then
5445 return False;
5446 end if;
5448 -- Here the target has Fractional_Fixed_Ops, if first time, compute
5449 -- standard constants used by Is_Fractional_Type.
5451 if First_Time_For_THFO then
5452 First_Time_For_THFO := False;
5454 Integer_Sized_Small :=
5455 UR_From_Components
5456 (Num => Uint_1,
5457 Den => UI_From_Int (Standard_Integer_Size - 1),
5458 Rbase => 2);
5460 Long_Integer_Sized_Small :=
5461 UR_From_Components
5462 (Num => Uint_1,
5463 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
5464 Rbase => 2);
5465 end if;
5467 -- Return True if target supports fixed-by-fixed multiply/divide
5468 -- for fractional fixed-point types (see Is_Fractional_Type) and
5469 -- the operand and result types are equivalent fractional types.
5471 return Is_Fractional_Type (Base_Type (Left_Typ))
5472 and then Is_Fractional_Type (Base_Type (Right_Typ))
5473 and then Is_Fractional_Type (Base_Type (Result_Typ))
5474 and then Esize (Left_Typ) = Esize (Right_Typ)
5475 and then Esize (Left_Typ) = Esize (Result_Typ);
5476 end Target_Has_Fixed_Ops;
5478 ------------------------------------------
5479 -- Type_May_Have_Bit_Aligned_Components --
5480 ------------------------------------------
5482 function Type_May_Have_Bit_Aligned_Components
5483 (Typ : Entity_Id) return Boolean
5485 begin
5486 -- Array type, check component type
5488 if Is_Array_Type (Typ) then
5489 return
5490 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
5492 -- Record type, check components
5494 elsif Is_Record_Type (Typ) then
5495 declare
5496 E : Entity_Id;
5498 begin
5499 E := First_Component_Or_Discriminant (Typ);
5500 while Present (E) loop
5501 if Component_May_Be_Bit_Aligned (E)
5502 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
5503 then
5504 return True;
5505 end if;
5507 Next_Component_Or_Discriminant (E);
5508 end loop;
5510 return False;
5511 end;
5513 -- Type other than array or record is always OK
5515 else
5516 return False;
5517 end if;
5518 end Type_May_Have_Bit_Aligned_Components;
5520 ----------------------------
5521 -- Wrap_Cleanup_Procedure --
5522 ----------------------------
5524 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
5525 Loc : constant Source_Ptr := Sloc (N);
5526 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
5527 Stmts : constant List_Id := Statements (Stseq);
5529 begin
5530 if Abort_Allowed then
5531 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5532 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
5533 end if;
5534 end Wrap_Cleanup_Procedure;
5536 end Exp_Util;