Merge from the pain train
[official-gcc.git] / gcc / ada / exp_util.adb
blob162b939f125fbc0bb1c7dcff9ec09c72982d06a5
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-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Tss; use Exp_Tss;
35 with Hostparm; use Hostparm;
36 with Inline; use Inline;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Sem; use Sem;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Res; use Sem_Res;
49 with Sem_Util; use Sem_Util;
50 with Sinfo; use Sinfo;
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 : in out List_Id;
95 Stats : in out 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 function New_Class_Wide_Subtype
134 (CW_Typ : Entity_Id;
135 N : Node_Id) return Entity_Id;
136 -- Create an implicit subtype of CW_Typ attached to node N
138 ----------------------
139 -- Adjust_Condition --
140 ----------------------
142 procedure Adjust_Condition (N : Node_Id) is
143 begin
144 if No (N) then
145 return;
146 end if;
148 declare
149 Loc : constant Source_Ptr := Sloc (N);
150 T : constant Entity_Id := Etype (N);
151 Ti : Entity_Id;
153 begin
154 -- For now, we simply ignore a call where the argument has no
155 -- type (probably case of unanalyzed condition), or has a type
156 -- that is not Boolean. This is because this is a pretty marginal
157 -- piece of functionality, and violations of these rules are
158 -- likely to be truly marginal (how much code uses Fortran Logical
159 -- as the barrier to a protected entry?) and we do not want to
160 -- blow up existing programs. We can change this to an assertion
161 -- after 3.12a is released ???
163 if No (T) or else not Is_Boolean_Type (T) then
164 return;
165 end if;
167 -- Apply validity checking if needed
169 if Validity_Checks_On and Validity_Check_Tests then
170 Ensure_Valid (N);
171 end if;
173 -- Immediate return if standard boolean, the most common case,
174 -- where nothing needs to be done.
176 if Base_Type (T) = Standard_Boolean then
177 return;
178 end if;
180 -- Case of zero/non-zero semantics or non-standard enumeration
181 -- representation. In each case, we rewrite the node as:
183 -- ityp!(N) /= False'Enum_Rep
185 -- where ityp is an integer type with large enough size to hold
186 -- any value of type T.
188 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
189 if Esize (T) <= Esize (Standard_Integer) then
190 Ti := Standard_Integer;
191 else
192 Ti := Standard_Long_Long_Integer;
193 end if;
195 Rewrite (N,
196 Make_Op_Ne (Loc,
197 Left_Opnd => Unchecked_Convert_To (Ti, N),
198 Right_Opnd =>
199 Make_Attribute_Reference (Loc,
200 Attribute_Name => Name_Enum_Rep,
201 Prefix =>
202 New_Occurrence_Of (First_Literal (T), Loc))));
203 Analyze_And_Resolve (N, Standard_Boolean);
205 else
206 Rewrite (N, Convert_To (Standard_Boolean, N));
207 Analyze_And_Resolve (N, Standard_Boolean);
208 end if;
209 end;
210 end Adjust_Condition;
212 ------------------------
213 -- Adjust_Result_Type --
214 ------------------------
216 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
217 begin
218 -- Ignore call if current type is not Standard.Boolean
220 if Etype (N) /= Standard_Boolean then
221 return;
222 end if;
224 -- If result is already of correct type, nothing to do. Note that
225 -- this will get the most common case where everything has a type
226 -- of Standard.Boolean.
228 if Base_Type (T) = Standard_Boolean then
229 return;
231 else
232 declare
233 KP : constant Node_Kind := Nkind (Parent (N));
235 begin
236 -- If result is to be used as a Condition in the syntax, no need
237 -- to convert it back, since if it was changed to Standard.Boolean
238 -- using Adjust_Condition, that is just fine for this usage.
240 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
241 return;
243 -- If result is an operand of another logical operation, no need
244 -- to reset its type, since Standard.Boolean is just fine, and
245 -- such operations always do Adjust_Condition on their operands.
247 elsif KP in N_Op_Boolean
248 or else KP = N_And_Then
249 or else KP = N_Or_Else
250 or else KP = N_Op_Not
251 then
252 return;
254 -- Otherwise we perform a conversion from the current type,
255 -- which must be Standard.Boolean, to the desired type.
257 else
258 Set_Analyzed (N);
259 Rewrite (N, Convert_To (T, N));
260 Analyze_And_Resolve (N, T);
261 end if;
262 end;
263 end if;
264 end Adjust_Result_Type;
266 --------------------------
267 -- Append_Freeze_Action --
268 --------------------------
270 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
271 Fnode : Node_Id := Freeze_Node (T);
273 begin
274 Ensure_Freeze_Node (T);
275 Fnode := Freeze_Node (T);
277 if not Present (Actions (Fnode)) then
278 Set_Actions (Fnode, New_List);
279 end if;
281 Append (N, Actions (Fnode));
282 end Append_Freeze_Action;
284 ---------------------------
285 -- Append_Freeze_Actions --
286 ---------------------------
288 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
289 Fnode : constant Node_Id := Freeze_Node (T);
291 begin
292 if No (L) then
293 return;
295 else
296 if No (Actions (Fnode)) then
297 Set_Actions (Fnode, L);
299 else
300 Append_List (L, Actions (Fnode));
301 end if;
303 end if;
304 end Append_Freeze_Actions;
306 ------------------------
307 -- Build_Runtime_Call --
308 ------------------------
310 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
311 begin
312 -- If entity is not available, we can skip making the call (this avoids
313 -- junk duplicated error messages in a number of cases).
315 if not RTE_Available (RE) then
316 return Make_Null_Statement (Loc);
317 else
318 return
319 Make_Procedure_Call_Statement (Loc,
320 Name => New_Reference_To (RTE (RE), Loc));
321 end if;
322 end Build_Runtime_Call;
324 ----------------------------
325 -- Build_Task_Array_Image --
326 ----------------------------
328 -- This function generates the body for a function that constructs the
329 -- image string for a task that is an array component. The function is
330 -- local to the init proc for the array type, and is called for each one
331 -- of the components. The constructed image has the form of an indexed
332 -- component, whose prefix is the outer variable of the array type.
333 -- The n-dimensional array type has known indices Index, Index2...
334 -- Id_Ref is an indexed component form created by the enclosing init proc.
335 -- Its successive indices are Val1, Val2,.. which are the loop variables
336 -- in the loops that call the individual task init proc on each component.
338 -- The generated function has the following structure:
340 -- function F return String is
341 -- Pref : string renames Task_Name;
342 -- T1 : String := Index1'Image (Val1);
343 -- ...
344 -- Tn : String := indexn'image (Valn);
345 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
346 -- -- Len includes commas and the end parentheses.
347 -- Res : String (1..Len);
348 -- Pos : Integer := Pref'Length;
350 -- begin
351 -- Res (1 .. Pos) := Pref;
352 -- Pos := Pos + 1;
353 -- Res (Pos) := '(';
354 -- Pos := Pos + 1;
355 -- Res (Pos .. Pos + T1'Length - 1) := T1;
356 -- Pos := Pos + T1'Length;
357 -- Res (Pos) := '.';
358 -- Pos := Pos + 1;
359 -- ...
360 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
361 -- Res (Len) := ')';
363 -- return Res;
364 -- end F;
366 -- Needless to say, multidimensional arrays of tasks are rare enough
367 -- that the bulkiness of this code is not really a concern.
369 function Build_Task_Array_Image
370 (Loc : Source_Ptr;
371 Id_Ref : Node_Id;
372 A_Type : Entity_Id;
373 Dyn : Boolean := False) return Node_Id
375 Dims : constant Nat := Number_Dimensions (A_Type);
376 -- Number of dimensions for array of tasks
378 Temps : array (1 .. Dims) of Entity_Id;
379 -- Array of temporaries to hold string for each index
381 Indx : Node_Id;
382 -- Index expression
384 Len : Entity_Id;
385 -- Total length of generated name
387 Pos : Entity_Id;
388 -- Running index for substring assignments
390 Pref : Entity_Id;
391 -- Name of enclosing variable, prefix of resulting name
393 Res : Entity_Id;
394 -- String to hold result
396 Val : Node_Id;
397 -- Value of successive indices
399 Sum : Node_Id;
400 -- Expression to compute total size of string
402 T : Entity_Id;
403 -- Entity for name at one index position
405 Decls : List_Id := New_List;
406 Stats : List_Id := New_List;
408 begin
409 Pref := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
411 -- For a dynamic task, the name comes from the target variable.
412 -- For a static one it is a formal of the enclosing init proc.
414 if Dyn then
415 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
416 Append_To (Decls,
417 Make_Object_Declaration (Loc,
418 Defining_Identifier => Pref,
419 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
420 Expression =>
421 Make_String_Literal (Loc,
422 Strval => String_From_Name_Buffer)));
424 else
425 Append_To (Decls,
426 Make_Object_Renaming_Declaration (Loc,
427 Defining_Identifier => Pref,
428 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
429 Name => Make_Identifier (Loc, Name_uTask_Name)));
430 end if;
432 Indx := First_Index (A_Type);
433 Val := First (Expressions (Id_Ref));
435 for J in 1 .. Dims loop
436 T := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
437 Temps (J) := T;
439 Append_To (Decls,
440 Make_Object_Declaration (Loc,
441 Defining_Identifier => T,
442 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
443 Expression =>
444 Make_Attribute_Reference (Loc,
445 Attribute_Name => Name_Image,
446 Prefix =>
447 New_Occurrence_Of (Etype (Indx), Loc),
448 Expressions => New_List (
449 New_Copy_Tree (Val)))));
451 Next_Index (Indx);
452 Next (Val);
453 end loop;
455 Sum := Make_Integer_Literal (Loc, Dims + 1);
457 Sum :=
458 Make_Op_Add (Loc,
459 Left_Opnd => Sum,
460 Right_Opnd =>
461 Make_Attribute_Reference (Loc,
462 Attribute_Name => Name_Length,
463 Prefix =>
464 New_Occurrence_Of (Pref, Loc),
465 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
467 for J in 1 .. Dims loop
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 (Temps (J), Loc),
476 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
477 end loop;
479 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
481 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
483 Append_To (Stats,
484 Make_Assignment_Statement (Loc,
485 Name => Make_Indexed_Component (Loc,
486 Prefix => New_Occurrence_Of (Res, Loc),
487 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
488 Expression =>
489 Make_Character_Literal (Loc,
490 Chars => Name_Find,
491 Char_Literal_Value =>
492 UI_From_Int (Character'Pos ('(')))));
494 Append_To (Stats,
495 Make_Assignment_Statement (Loc,
496 Name => New_Occurrence_Of (Pos, Loc),
497 Expression =>
498 Make_Op_Add (Loc,
499 Left_Opnd => New_Occurrence_Of (Pos, Loc),
500 Right_Opnd => Make_Integer_Literal (Loc, 1))));
502 for J in 1 .. Dims loop
504 Append_To (Stats,
505 Make_Assignment_Statement (Loc,
506 Name => Make_Slice (Loc,
507 Prefix => New_Occurrence_Of (Res, Loc),
508 Discrete_Range =>
509 Make_Range (Loc,
510 Low_Bound => New_Occurrence_Of (Pos, Loc),
511 High_Bound => Make_Op_Subtract (Loc,
512 Left_Opnd =>
513 Make_Op_Add (Loc,
514 Left_Opnd => New_Occurrence_Of (Pos, Loc),
515 Right_Opnd =>
516 Make_Attribute_Reference (Loc,
517 Attribute_Name => Name_Length,
518 Prefix =>
519 New_Occurrence_Of (Temps (J), Loc),
520 Expressions =>
521 New_List (Make_Integer_Literal (Loc, 1)))),
522 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
524 Expression => New_Occurrence_Of (Temps (J), Loc)));
526 if J < Dims then
527 Append_To (Stats,
528 Make_Assignment_Statement (Loc,
529 Name => New_Occurrence_Of (Pos, Loc),
530 Expression =>
531 Make_Op_Add (Loc,
532 Left_Opnd => New_Occurrence_Of (Pos, Loc),
533 Right_Opnd =>
534 Make_Attribute_Reference (Loc,
535 Attribute_Name => Name_Length,
536 Prefix => New_Occurrence_Of (Temps (J), Loc),
537 Expressions =>
538 New_List (Make_Integer_Literal (Loc, 1))))));
540 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
542 Append_To (Stats,
543 Make_Assignment_Statement (Loc,
544 Name => Make_Indexed_Component (Loc,
545 Prefix => New_Occurrence_Of (Res, Loc),
546 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
547 Expression =>
548 Make_Character_Literal (Loc,
549 Chars => Name_Find,
550 Char_Literal_Value =>
551 UI_From_Int (Character'Pos (',')))));
553 Append_To (Stats,
554 Make_Assignment_Statement (Loc,
555 Name => New_Occurrence_Of (Pos, Loc),
556 Expression =>
557 Make_Op_Add (Loc,
558 Left_Opnd => New_Occurrence_Of (Pos, Loc),
559 Right_Opnd => Make_Integer_Literal (Loc, 1))));
560 end if;
561 end loop;
563 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
565 Append_To (Stats,
566 Make_Assignment_Statement (Loc,
567 Name => Make_Indexed_Component (Loc,
568 Prefix => New_Occurrence_Of (Res, Loc),
569 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
570 Expression =>
571 Make_Character_Literal (Loc,
572 Chars => Name_Find,
573 Char_Literal_Value =>
574 UI_From_Int (Character'Pos (')')))));
575 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
576 end Build_Task_Array_Image;
578 ----------------------------
579 -- Build_Task_Image_Decls --
580 ----------------------------
582 function Build_Task_Image_Decls
583 (Loc : Source_Ptr;
584 Id_Ref : Node_Id;
585 A_Type : Entity_Id) return List_Id
587 Decls : constant List_Id := New_List;
588 T_Id : Entity_Id := Empty;
589 Decl : Node_Id;
590 Expr : Node_Id := Empty;
591 Fun : Node_Id := Empty;
592 Is_Dyn : constant Boolean :=
593 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
594 and then
595 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
597 begin
598 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
599 -- generate a dummy declaration only.
601 if Restriction_Active (No_Implicit_Heap_Allocations)
602 or else Global_Discard_Names
603 then
604 T_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
605 Name_Len := 0;
607 return
608 New_List (
609 Make_Object_Declaration (Loc,
610 Defining_Identifier => T_Id,
611 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
612 Expression =>
613 Make_String_Literal (Loc,
614 Strval => String_From_Name_Buffer)));
616 else
617 if Nkind (Id_Ref) = N_Identifier
618 or else Nkind (Id_Ref) = N_Defining_Identifier
619 then
620 -- For a simple variable, the image of the task is built from
621 -- the name of the variable. To avoid possible conflict with
622 -- the anonymous type created for a single protected object,
623 -- add a numeric suffix.
625 T_Id :=
626 Make_Defining_Identifier (Loc,
627 New_External_Name (Chars (Id_Ref), 'T', 1));
629 Get_Name_String (Chars (Id_Ref));
631 Expr :=
632 Make_String_Literal (Loc,
633 Strval => String_From_Name_Buffer);
635 elsif Nkind (Id_Ref) = N_Selected_Component then
636 T_Id :=
637 Make_Defining_Identifier (Loc,
638 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
639 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
641 elsif Nkind (Id_Ref) = N_Indexed_Component then
642 T_Id :=
643 Make_Defining_Identifier (Loc,
644 New_External_Name (Chars (A_Type), 'N'));
646 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
647 end if;
648 end if;
650 if Present (Fun) then
651 Append (Fun, Decls);
652 Expr := Make_Function_Call (Loc,
653 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
654 end if;
656 Decl := Make_Object_Declaration (Loc,
657 Defining_Identifier => T_Id,
658 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
659 Constant_Present => True,
660 Expression => Expr);
662 Append (Decl, Decls);
663 return Decls;
664 end Build_Task_Image_Decls;
666 -------------------------------
667 -- Build_Task_Image_Function --
668 -------------------------------
670 function Build_Task_Image_Function
671 (Loc : Source_Ptr;
672 Decls : List_Id;
673 Stats : List_Id;
674 Res : Entity_Id) return Node_Id
676 Spec : Node_Id;
678 begin
679 Append_To (Stats,
680 Make_Return_Statement (Loc,
681 Expression => New_Occurrence_Of (Res, Loc)));
683 Spec := Make_Function_Specification (Loc,
684 Defining_Unit_Name =>
685 Make_Defining_Identifier (Loc, New_Internal_Name ('F')),
686 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc));
688 -- Calls to 'Image use the secondary stack, which must be cleaned
689 -- up after the task name is built.
691 Set_Uses_Sec_Stack (Defining_Unit_Name (Spec));
693 return Make_Subprogram_Body (Loc,
694 Specification => Spec,
695 Declarations => Decls,
696 Handled_Statement_Sequence =>
697 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
698 end Build_Task_Image_Function;
700 -----------------------------
701 -- Build_Task_Image_Prefix --
702 -----------------------------
704 procedure Build_Task_Image_Prefix
705 (Loc : Source_Ptr;
706 Len : out Entity_Id;
707 Res : out Entity_Id;
708 Pos : out Entity_Id;
709 Prefix : Entity_Id;
710 Sum : Node_Id;
711 Decls : in out List_Id;
712 Stats : in out List_Id)
714 begin
715 Len := Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
717 Append_To (Decls,
718 Make_Object_Declaration (Loc,
719 Defining_Identifier => Len,
720 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
721 Expression => Sum));
723 Res := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
725 Append_To (Decls,
726 Make_Object_Declaration (Loc,
727 Defining_Identifier => Res,
728 Object_Definition =>
729 Make_Subtype_Indication (Loc,
730 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
731 Constraint =>
732 Make_Index_Or_Discriminant_Constraint (Loc,
733 Constraints =>
734 New_List (
735 Make_Range (Loc,
736 Low_Bound => Make_Integer_Literal (Loc, 1),
737 High_Bound => New_Occurrence_Of (Len, Loc)))))));
739 Pos := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
741 Append_To (Decls,
742 Make_Object_Declaration (Loc,
743 Defining_Identifier => Pos,
744 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
746 -- Pos := Prefix'Length;
748 Append_To (Stats,
749 Make_Assignment_Statement (Loc,
750 Name => New_Occurrence_Of (Pos, Loc),
751 Expression =>
752 Make_Attribute_Reference (Loc,
753 Attribute_Name => Name_Length,
754 Prefix => New_Occurrence_Of (Prefix, Loc),
755 Expressions =>
756 New_List (Make_Integer_Literal (Loc, 1)))));
758 -- Res (1 .. Pos) := Prefix;
760 Append_To (Stats,
761 Make_Assignment_Statement (Loc,
762 Name => Make_Slice (Loc,
763 Prefix => New_Occurrence_Of (Res, Loc),
764 Discrete_Range =>
765 Make_Range (Loc,
766 Low_Bound => Make_Integer_Literal (Loc, 1),
767 High_Bound => New_Occurrence_Of (Pos, Loc))),
769 Expression => New_Occurrence_Of (Prefix, Loc)));
771 Append_To (Stats,
772 Make_Assignment_Statement (Loc,
773 Name => New_Occurrence_Of (Pos, Loc),
774 Expression =>
775 Make_Op_Add (Loc,
776 Left_Opnd => New_Occurrence_Of (Pos, Loc),
777 Right_Opnd => Make_Integer_Literal (Loc, 1))));
778 end Build_Task_Image_Prefix;
780 -----------------------------
781 -- Build_Task_Record_Image --
782 -----------------------------
784 function Build_Task_Record_Image
785 (Loc : Source_Ptr;
786 Id_Ref : Node_Id;
787 Dyn : Boolean := False) return Node_Id
789 Len : Entity_Id;
790 -- Total length of generated name
792 Pos : Entity_Id;
793 -- Index into result
795 Res : Entity_Id;
796 -- String to hold result
798 Pref : Entity_Id;
799 -- Name of enclosing variable, prefix of resulting name
801 Sum : Node_Id;
802 -- Expression to compute total size of string
804 Sel : Entity_Id;
805 -- Entity for selector name
807 Decls : List_Id := New_List;
808 Stats : List_Id := New_List;
810 begin
811 Pref := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
813 -- For a dynamic task, the name comes from the target variable.
814 -- For a static one it is a formal of the enclosing init proc.
816 if Dyn then
817 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
818 Append_To (Decls,
819 Make_Object_Declaration (Loc,
820 Defining_Identifier => Pref,
821 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
822 Expression =>
823 Make_String_Literal (Loc,
824 Strval => String_From_Name_Buffer)));
826 else
827 Append_To (Decls,
828 Make_Object_Renaming_Declaration (Loc,
829 Defining_Identifier => Pref,
830 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
831 Name => Make_Identifier (Loc, Name_uTask_Name)));
832 end if;
834 Sel := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
836 Get_Name_String (Chars (Selector_Name (Id_Ref)));
838 Append_To (Decls,
839 Make_Object_Declaration (Loc,
840 Defining_Identifier => Sel,
841 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
842 Expression =>
843 Make_String_Literal (Loc,
844 Strval => String_From_Name_Buffer)));
846 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
848 Sum :=
849 Make_Op_Add (Loc,
850 Left_Opnd => Sum,
851 Right_Opnd =>
852 Make_Attribute_Reference (Loc,
853 Attribute_Name => Name_Length,
854 Prefix =>
855 New_Occurrence_Of (Pref, Loc),
856 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
858 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
860 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
862 -- Res (Pos) := '.';
864 Append_To (Stats,
865 Make_Assignment_Statement (Loc,
866 Name => Make_Indexed_Component (Loc,
867 Prefix => New_Occurrence_Of (Res, Loc),
868 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
869 Expression =>
870 Make_Character_Literal (Loc,
871 Chars => Name_Find,
872 Char_Literal_Value =>
873 UI_From_Int (Character'Pos ('.')))));
875 Append_To (Stats,
876 Make_Assignment_Statement (Loc,
877 Name => New_Occurrence_Of (Pos, Loc),
878 Expression =>
879 Make_Op_Add (Loc,
880 Left_Opnd => New_Occurrence_Of (Pos, Loc),
881 Right_Opnd => Make_Integer_Literal (Loc, 1))));
883 -- Res (Pos .. Len) := Selector;
885 Append_To (Stats,
886 Make_Assignment_Statement (Loc,
887 Name => Make_Slice (Loc,
888 Prefix => New_Occurrence_Of (Res, Loc),
889 Discrete_Range =>
890 Make_Range (Loc,
891 Low_Bound => New_Occurrence_Of (Pos, Loc),
892 High_Bound => New_Occurrence_Of (Len, Loc))),
893 Expression => New_Occurrence_Of (Sel, Loc)));
895 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
896 end Build_Task_Record_Image;
898 ----------------------------------
899 -- Component_May_Be_Bit_Aligned --
900 ----------------------------------
902 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
903 begin
904 -- If no component clause, then everything is fine, since the
905 -- back end never bit-misaligns by default, even if there is
906 -- a pragma Packed for the record.
908 if No (Component_Clause (Comp)) then
909 return False;
910 end if;
912 -- It is only array and record types that cause trouble
914 if not Is_Record_Type (Etype (Comp))
915 and then not Is_Array_Type (Etype (Comp))
916 then
917 return False;
919 -- If we know that we have a small (64 bits or less) record
920 -- or bit-packed array, then everything is fine, since the
921 -- back end can handle these cases correctly.
923 elsif Esize (Comp) <= 64
924 and then (Is_Record_Type (Etype (Comp))
925 or else Is_Bit_Packed_Array (Etype (Comp)))
926 then
927 return False;
929 -- Otherwise if the component is not byte aligned, we
930 -- know we have the nasty unaligned case.
932 elsif Normalized_First_Bit (Comp) /= Uint_0
933 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
934 then
935 return True;
937 -- If we are large and byte aligned, then OK at this level
939 else
940 return False;
941 end if;
942 end Component_May_Be_Bit_Aligned;
944 -------------------------------
945 -- Convert_To_Actual_Subtype --
946 -------------------------------
948 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
949 Act_ST : Entity_Id;
951 begin
952 Act_ST := Get_Actual_Subtype (Exp);
954 if Act_ST = Etype (Exp) then
955 return;
957 else
958 Rewrite (Exp,
959 Convert_To (Act_ST, Relocate_Node (Exp)));
960 Analyze_And_Resolve (Exp, Act_ST);
961 end if;
962 end Convert_To_Actual_Subtype;
964 -----------------------------------
965 -- Current_Sem_Unit_Declarations --
966 -----------------------------------
968 function Current_Sem_Unit_Declarations return List_Id is
969 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
970 Decls : List_Id;
972 begin
973 -- If the current unit is a package body, locate the visible
974 -- declarations of the package spec.
976 if Nkind (U) = N_Package_Body then
977 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
978 end if;
980 if Nkind (U) = N_Package_Declaration then
981 U := Specification (U);
982 Decls := Visible_Declarations (U);
984 if No (Decls) then
985 Decls := New_List;
986 Set_Visible_Declarations (U, Decls);
987 end if;
989 else
990 Decls := Declarations (U);
992 if No (Decls) then
993 Decls := New_List;
994 Set_Declarations (U, Decls);
995 end if;
996 end if;
998 return Decls;
999 end Current_Sem_Unit_Declarations;
1001 -----------------------
1002 -- Duplicate_Subexpr --
1003 -----------------------
1005 function Duplicate_Subexpr
1006 (Exp : Node_Id;
1007 Name_Req : Boolean := False) return Node_Id
1009 begin
1010 Remove_Side_Effects (Exp, Name_Req);
1011 return New_Copy_Tree (Exp);
1012 end Duplicate_Subexpr;
1014 ---------------------------------
1015 -- Duplicate_Subexpr_No_Checks --
1016 ---------------------------------
1018 function Duplicate_Subexpr_No_Checks
1019 (Exp : Node_Id;
1020 Name_Req : Boolean := False) return Node_Id
1022 New_Exp : Node_Id;
1024 begin
1025 Remove_Side_Effects (Exp, Name_Req);
1026 New_Exp := New_Copy_Tree (Exp);
1027 Remove_Checks (New_Exp);
1028 return New_Exp;
1029 end Duplicate_Subexpr_No_Checks;
1031 -----------------------------------
1032 -- Duplicate_Subexpr_Move_Checks --
1033 -----------------------------------
1035 function Duplicate_Subexpr_Move_Checks
1036 (Exp : Node_Id;
1037 Name_Req : Boolean := False) return Node_Id
1039 New_Exp : Node_Id;
1041 begin
1042 Remove_Side_Effects (Exp, Name_Req);
1043 New_Exp := New_Copy_Tree (Exp);
1044 Remove_Checks (Exp);
1045 return New_Exp;
1046 end Duplicate_Subexpr_Move_Checks;
1048 --------------------
1049 -- Ensure_Defined --
1050 --------------------
1052 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1053 IR : Node_Id;
1054 P : Node_Id;
1056 begin
1057 if Is_Itype (Typ) then
1058 IR := Make_Itype_Reference (Sloc (N));
1059 Set_Itype (IR, Typ);
1061 if not In_Open_Scopes (Scope (Typ))
1062 and then Is_Subprogram (Current_Scope)
1063 and then Scope (Current_Scope) /= Standard_Standard
1064 then
1065 -- Insert node in front of subprogram, to avoid scope anomalies
1066 -- in gigi.
1068 P := Parent (N);
1069 while Present (P)
1070 and then Nkind (P) /= N_Subprogram_Body
1071 loop
1072 P := Parent (P);
1073 end loop;
1075 if Present (P) then
1076 Insert_Action (P, IR);
1077 else
1078 Insert_Action (N, IR);
1079 end if;
1081 else
1082 Insert_Action (N, IR);
1083 end if;
1084 end if;
1085 end Ensure_Defined;
1087 ---------------------
1088 -- Evolve_And_Then --
1089 ---------------------
1091 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1092 begin
1093 if No (Cond) then
1094 Cond := Cond1;
1095 else
1096 Cond :=
1097 Make_And_Then (Sloc (Cond1),
1098 Left_Opnd => Cond,
1099 Right_Opnd => Cond1);
1100 end if;
1101 end Evolve_And_Then;
1103 --------------------
1104 -- Evolve_Or_Else --
1105 --------------------
1107 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1108 begin
1109 if No (Cond) then
1110 Cond := Cond1;
1111 else
1112 Cond :=
1113 Make_Or_Else (Sloc (Cond1),
1114 Left_Opnd => Cond,
1115 Right_Opnd => Cond1);
1116 end if;
1117 end Evolve_Or_Else;
1119 ------------------------------
1120 -- Expand_Subtype_From_Expr --
1121 ------------------------------
1123 -- This function is applicable for both static and dynamic allocation of
1124 -- objects which are constrained by an initial expression. Basically it
1125 -- transforms an unconstrained subtype indication into a constrained one.
1126 -- The expression may also be transformed in certain cases in order to
1127 -- avoid multiple evaulation. In the static allocation case, the general
1128 -- scheme is :
1130 -- Val : T := Expr;
1132 -- is transformed into
1134 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1136 -- Here are the main cases :
1138 -- <if Expr is a Slice>
1139 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1141 -- <elsif Expr is a String Literal>
1142 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1144 -- <elsif Expr is Constrained>
1145 -- subtype T is Type_Of_Expr
1146 -- Val : T := Expr;
1148 -- <elsif Expr is an entity_name>
1149 -- Val : T (constraints taken from Expr) := Expr;
1151 -- <else>
1152 -- type Axxx is access all T;
1153 -- Rval : Axxx := Expr'ref;
1154 -- Val : T (constraints taken from Rval) := Rval.all;
1156 -- ??? note: when the Expression is allocated in the secondary stack
1157 -- we could use it directly instead of copying it by declaring
1158 -- Val : T (...) renames Rval.all
1160 procedure Expand_Subtype_From_Expr
1161 (N : Node_Id;
1162 Unc_Type : Entity_Id;
1163 Subtype_Indic : Node_Id;
1164 Exp : Node_Id)
1166 Loc : constant Source_Ptr := Sloc (N);
1167 Exp_Typ : constant Entity_Id := Etype (Exp);
1168 T : Entity_Id;
1170 begin
1171 -- In general we cannot build the subtype if expansion is disabled,
1172 -- because internal entities may not have been defined. However, to
1173 -- avoid some cascaded errors, we try to continue when the expression
1174 -- is an array (or string), because it is safe to compute the bounds.
1175 -- It is in fact required to do so even in a generic context, because
1176 -- there may be constants that depend on bounds of string literal.
1178 if not Expander_Active
1179 and then (No (Etype (Exp))
1180 or else Base_Type (Etype (Exp)) /= Standard_String)
1181 then
1182 return;
1183 end if;
1185 if Nkind (Exp) = N_Slice then
1186 declare
1187 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1189 begin
1190 Rewrite (Subtype_Indic,
1191 Make_Subtype_Indication (Loc,
1192 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1193 Constraint =>
1194 Make_Index_Or_Discriminant_Constraint (Loc,
1195 Constraints => New_List
1196 (New_Reference_To (Slice_Type, Loc)))));
1198 -- This subtype indication may be used later for contraint checks
1199 -- we better make sure that if a variable was used as a bound of
1200 -- of the original slice, its value is frozen.
1202 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
1203 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
1204 end;
1206 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
1207 Rewrite (Subtype_Indic,
1208 Make_Subtype_Indication (Loc,
1209 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1210 Constraint =>
1211 Make_Index_Or_Discriminant_Constraint (Loc,
1212 Constraints => New_List (
1213 Make_Literal_Range (Loc,
1214 Literal_Typ => Exp_Typ)))));
1216 elsif Is_Constrained (Exp_Typ)
1217 and then not Is_Class_Wide_Type (Unc_Type)
1218 then
1219 if Is_Itype (Exp_Typ) then
1221 -- No need to generate a new one
1223 T := Exp_Typ;
1225 else
1226 T :=
1227 Make_Defining_Identifier (Loc,
1228 Chars => New_Internal_Name ('T'));
1230 Insert_Action (N,
1231 Make_Subtype_Declaration (Loc,
1232 Defining_Identifier => T,
1233 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
1235 -- This type is marked as an itype even though it has an
1236 -- explicit declaration because otherwise it can be marked
1237 -- with Is_Generic_Actual_Type and generate spurious errors.
1238 -- (see sem_ch8.Analyze_Package_Renaming and sem_type.covers)
1240 Set_Is_Itype (T);
1241 Set_Associated_Node_For_Itype (T, Exp);
1242 end if;
1244 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
1246 -- nothing needs to be done for private types with unknown discriminants
1247 -- if the underlying type is not an unconstrained composite type.
1249 elsif Is_Private_Type (Unc_Type)
1250 and then Has_Unknown_Discriminants (Unc_Type)
1251 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
1252 or else Is_Constrained (Underlying_Type (Unc_Type)))
1253 then
1254 null;
1256 else
1257 Remove_Side_Effects (Exp);
1258 Rewrite (Subtype_Indic,
1259 Make_Subtype_From_Expr (Exp, Unc_Type));
1260 end if;
1261 end Expand_Subtype_From_Expr;
1263 ------------------
1264 -- Find_Prim_Op --
1265 ------------------
1267 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
1268 Prim : Elmt_Id;
1269 Typ : Entity_Id := T;
1271 begin
1272 if Is_Class_Wide_Type (Typ) then
1273 Typ := Root_Type (Typ);
1274 end if;
1276 Typ := Underlying_Type (Typ);
1278 Prim := First_Elmt (Primitive_Operations (Typ));
1279 while Chars (Node (Prim)) /= Name loop
1280 Next_Elmt (Prim);
1281 pragma Assert (Present (Prim));
1282 end loop;
1284 return Node (Prim);
1285 end Find_Prim_Op;
1287 function Find_Prim_Op
1288 (T : Entity_Id;
1289 Name : TSS_Name_Type) return Entity_Id
1291 Prim : Elmt_Id;
1292 Typ : Entity_Id := T;
1294 begin
1295 if Is_Class_Wide_Type (Typ) then
1296 Typ := Root_Type (Typ);
1297 end if;
1299 Typ := Underlying_Type (Typ);
1301 Prim := First_Elmt (Primitive_Operations (Typ));
1302 while not Is_TSS (Node (Prim), Name) loop
1303 Next_Elmt (Prim);
1304 pragma Assert (Present (Prim));
1305 end loop;
1307 return Node (Prim);
1308 end Find_Prim_Op;
1310 ----------------------
1311 -- Force_Evaluation --
1312 ----------------------
1314 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
1315 Component_In_Lhs : Boolean := False;
1316 Par : Node_Id;
1318 begin
1319 -- Loop to determine whether there is a component reference in
1320 -- the left hand side if Exp appears on the left side of an
1321 -- assignment statement. Needed to determine if form of result
1322 -- must be a variable.
1324 Par := Exp;
1325 while Present (Par)
1326 and then
1327 (Nkind (Par) = N_Selected_Component
1328 or else
1329 Nkind (Par) = N_Indexed_Component)
1330 loop
1331 if Nkind (Parent (Par)) = N_Assignment_Statement
1332 and then Par = Name (Parent (Par))
1333 then
1334 Component_In_Lhs := True;
1335 exit;
1336 else
1337 Par := Parent (Par);
1338 end if;
1339 end loop;
1341 -- If the expression is a selected component, it is being evaluated
1342 -- as part of a discriminant check. If it is part of a left-hand
1343 -- side, this is the last use of its value and it is safe to create
1344 -- a renaming for it, rather than a temporary. In addition, if it
1345 -- is not an addressable field, creating a temporary may be a problem
1346 -- for gigi, or might drop the value of the assignment. Therefore,
1347 -- if the expression is on the lhs of an assignment, remove side
1348 -- effects without requiring a temporary, and create a renaming.
1349 -- (See remove_side_effects for details).
1351 Remove_Side_Effects
1352 (Exp, Name_Req, Variable_Ref => not Component_In_Lhs);
1353 end Force_Evaluation;
1355 ------------------------
1356 -- Generate_Poll_Call --
1357 ------------------------
1359 procedure Generate_Poll_Call (N : Node_Id) is
1360 begin
1361 -- No poll call if polling not active
1363 if not Polling_Required then
1364 return;
1366 -- Otherwise generate require poll call
1368 else
1369 Insert_Before_And_Analyze (N,
1370 Make_Procedure_Call_Statement (Sloc (N),
1371 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
1372 end if;
1373 end Generate_Poll_Call;
1375 ---------------------------------
1376 -- Get_Current_Value_Condition --
1377 ---------------------------------
1379 procedure Get_Current_Value_Condition
1380 (Var : Node_Id;
1381 Op : out Node_Kind;
1382 Val : out Node_Id)
1384 Loc : constant Source_Ptr := Sloc (Var);
1385 CV : constant Node_Id := Current_Value (Entity (Var));
1386 Sens : Boolean;
1387 Stm : Node_Id;
1388 Cond : Node_Id;
1390 begin
1391 Op := N_Empty;
1392 Val := Empty;
1394 -- If statement. Condition is known true in THEN section, known False
1395 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
1397 if Nkind (CV) = N_If_Statement then
1399 -- Before start of IF statement
1401 if Loc < Sloc (CV) then
1402 return;
1404 -- After end of IF statement
1406 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
1407 return;
1408 end if;
1410 -- At this stage we know that we are within the IF statement, but
1411 -- unfortunately, the tree does not record the SLOC of the ELSE so
1412 -- we cannot use a simple SLOC comparison to distinguish between
1413 -- the then/else statements, so we have to climb the tree.
1415 declare
1416 N : Node_Id;
1418 begin
1419 N := Parent (Var);
1420 while Parent (N) /= CV loop
1421 N := Parent (N);
1423 -- If we fall off the top of the tree, then that's odd, but
1424 -- perhaps it could occur in some error situation, and the
1425 -- safest response is simply to assume that the outcome of
1426 -- the condition is unknown. No point in bombing during an
1427 -- attempt to optimize things.
1429 if No (N) then
1430 return;
1431 end if;
1432 end loop;
1434 -- Now we have N pointing to a node whose parent is the IF
1435 -- statement in question, so now we can tell if we are within
1436 -- the THEN statements.
1438 if Is_List_Member (N)
1439 and then List_Containing (N) = Then_Statements (CV)
1440 then
1441 Sens := True;
1443 -- Otherwise we must be in ELSIF or ELSE part
1445 else
1446 Sens := False;
1447 end if;
1448 end;
1450 -- ELSIF part. Condition is known true within the referenced
1451 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
1452 -- and unknown before the ELSE part or after the IF statement.
1454 elsif Nkind (CV) = N_Elsif_Part then
1455 Stm := Parent (CV);
1457 -- Before start of ELSIF part
1459 if Loc < Sloc (CV) then
1460 return;
1462 -- After end of IF statement
1464 elsif Loc >= Sloc (Stm) +
1465 Text_Ptr (UI_To_Int (End_Span (Stm)))
1466 then
1467 return;
1468 end if;
1470 -- Again we lack the SLOC of the ELSE, so we need to climb the
1471 -- tree to see if we are within the ELSIF part in question.
1473 declare
1474 N : Node_Id;
1476 begin
1477 N := Parent (Var);
1478 while Parent (N) /= Stm loop
1479 N := Parent (N);
1481 -- If we fall off the top of the tree, then that's odd, but
1482 -- perhaps it could occur in some error situation, and the
1483 -- safest response is simply to assume that the outcome of
1484 -- the condition is unknown. No point in bombing during an
1485 -- attempt to optimize things.
1487 if No (N) then
1488 return;
1489 end if;
1490 end loop;
1492 -- Now we have N pointing to a node whose parent is the IF
1493 -- statement in question, so see if is the ELSIF part we want.
1494 -- the THEN statements.
1496 if N = CV then
1497 Sens := True;
1499 -- Otherwise we must be in susbequent ELSIF or ELSE part
1501 else
1502 Sens := False;
1503 end if;
1504 end;
1506 -- All other cases of Current_Value settings
1508 else
1509 return;
1510 end if;
1512 -- If we fall through here, then we have a reportable
1513 -- condition, Sens is True if the condition is true and
1514 -- False if it needs inverting.
1516 -- Deal with NOT operators, inverting sense
1518 Cond := Condition (CV);
1519 while Nkind (Cond) = N_Op_Not loop
1520 Cond := Right_Opnd (Cond);
1521 Sens := not Sens;
1522 end loop;
1524 -- Now we must have a relational operator
1526 pragma Assert (Entity (Var) = Entity (Left_Opnd (Cond)));
1527 Val := Right_Opnd (Cond);
1528 Op := Nkind (Cond);
1530 if Sens = False then
1531 case Op is
1532 when N_Op_Eq => Op := N_Op_Ne;
1533 when N_Op_Ne => Op := N_Op_Eq;
1534 when N_Op_Lt => Op := N_Op_Ge;
1535 when N_Op_Gt => Op := N_Op_Le;
1536 when N_Op_Le => Op := N_Op_Gt;
1537 when N_Op_Ge => Op := N_Op_Lt;
1539 -- No other entry should be possible
1541 when others =>
1542 raise Program_Error;
1543 end case;
1544 end if;
1545 end Get_Current_Value_Condition;
1547 --------------------
1548 -- Homonym_Number --
1549 --------------------
1551 function Homonym_Number (Subp : Entity_Id) return Nat is
1552 Count : Nat;
1553 Hom : Entity_Id;
1555 begin
1556 Count := 1;
1557 Hom := Homonym (Subp);
1558 while Present (Hom) loop
1559 if Scope (Hom) = Scope (Subp) then
1560 Count := Count + 1;
1561 end if;
1563 Hom := Homonym (Hom);
1564 end loop;
1566 return Count;
1567 end Homonym_Number;
1569 ------------------------------
1570 -- In_Unconditional_Context --
1571 ------------------------------
1573 function In_Unconditional_Context (Node : Node_Id) return Boolean is
1574 P : Node_Id;
1576 begin
1577 P := Node;
1578 while Present (P) loop
1579 case Nkind (P) is
1580 when N_Subprogram_Body =>
1581 return True;
1583 when N_If_Statement =>
1584 return False;
1586 when N_Loop_Statement =>
1587 return False;
1589 when N_Case_Statement =>
1590 return False;
1592 when others =>
1593 P := Parent (P);
1594 end case;
1595 end loop;
1597 return False;
1598 end In_Unconditional_Context;
1600 -------------------
1601 -- Insert_Action --
1602 -------------------
1604 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
1605 begin
1606 if Present (Ins_Action) then
1607 Insert_Actions (Assoc_Node, New_List (Ins_Action));
1608 end if;
1609 end Insert_Action;
1611 -- Version with check(s) suppressed
1613 procedure Insert_Action
1614 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
1616 begin
1617 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
1618 end Insert_Action;
1620 --------------------
1621 -- Insert_Actions --
1622 --------------------
1624 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
1625 N : Node_Id;
1626 P : Node_Id;
1628 Wrapped_Node : Node_Id := Empty;
1630 begin
1631 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
1632 return;
1633 end if;
1635 -- Ignore insert of actions from inside default expression in the
1636 -- special preliminary analyze mode. Any insertions at this point
1637 -- have no relevance, since we are only doing the analyze to freeze
1638 -- the types of any static expressions. See section "Handling of
1639 -- Default Expressions" in the spec of package Sem for further details.
1641 if In_Default_Expression then
1642 return;
1643 end if;
1645 -- If the action derives from stuff inside a record, then the actions
1646 -- are attached to the current scope, to be inserted and analyzed on
1647 -- exit from the scope. The reason for this is that we may also
1648 -- be generating freeze actions at the same time, and they must
1649 -- eventually be elaborated in the correct order.
1651 if Is_Record_Type (Current_Scope)
1652 and then not Is_Frozen (Current_Scope)
1653 then
1654 if No (Scope_Stack.Table
1655 (Scope_Stack.Last).Pending_Freeze_Actions)
1656 then
1657 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
1658 Ins_Actions;
1659 else
1660 Append_List
1661 (Ins_Actions,
1662 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
1663 end if;
1665 return;
1666 end if;
1668 -- We now intend to climb up the tree to find the right point to
1669 -- insert the actions. We start at Assoc_Node, unless this node is
1670 -- a subexpression in which case we start with its parent. We do this
1671 -- for two reasons. First it speeds things up. Second, if Assoc_Node
1672 -- is itself one of the special nodes like N_And_Then, then we assume
1673 -- that an initial request to insert actions for such a node does not
1674 -- expect the actions to get deposited in the node for later handling
1675 -- when the node is expanded, since clearly the node is being dealt
1676 -- with by the caller. Note that in the subexpression case, N is
1677 -- always the child we came from.
1679 -- N_Raise_xxx_Error is an annoying special case, it is a statement
1680 -- if it has type Standard_Void_Type, and a subexpression otherwise.
1681 -- otherwise. Procedure attribute references are also statements.
1683 if Nkind (Assoc_Node) in N_Subexpr
1684 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
1685 or else Etype (Assoc_Node) /= Standard_Void_Type)
1686 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
1687 or else
1688 not Is_Procedure_Attribute_Name
1689 (Attribute_Name (Assoc_Node)))
1690 then
1691 P := Assoc_Node; -- ??? does not agree with above!
1692 N := Parent (Assoc_Node);
1694 -- Non-subexpression case. Note that N is initially Empty in this
1695 -- case (N is only guaranteed Non-Empty in the subexpr case).
1697 else
1698 P := Assoc_Node;
1699 N := Empty;
1700 end if;
1702 -- Capture root of the transient scope
1704 if Scope_Is_Transient then
1705 Wrapped_Node := Node_To_Be_Wrapped;
1706 end if;
1708 loop
1709 pragma Assert (Present (P));
1711 case Nkind (P) is
1713 -- Case of right operand of AND THEN or OR ELSE. Put the actions
1714 -- in the Actions field of the right operand. They will be moved
1715 -- out further when the AND THEN or OR ELSE operator is expanded.
1716 -- Nothing special needs to be done for the left operand since
1717 -- in that case the actions are executed unconditionally.
1719 when N_And_Then | N_Or_Else =>
1720 if N = Right_Opnd (P) then
1721 if Present (Actions (P)) then
1722 Insert_List_After_And_Analyze
1723 (Last (Actions (P)), Ins_Actions);
1724 else
1725 Set_Actions (P, Ins_Actions);
1726 Analyze_List (Actions (P));
1727 end if;
1729 return;
1730 end if;
1732 -- Then or Else operand of conditional expression. Add actions to
1733 -- Then_Actions or Else_Actions field as appropriate. The actions
1734 -- will be moved further out when the conditional is expanded.
1736 when N_Conditional_Expression =>
1737 declare
1738 ThenX : constant Node_Id := Next (First (Expressions (P)));
1739 ElseX : constant Node_Id := Next (ThenX);
1741 begin
1742 -- Actions belong to the then expression, temporarily
1743 -- place them as Then_Actions of the conditional expr.
1744 -- They will be moved to the proper place later when
1745 -- the conditional expression is expanded.
1747 if N = ThenX then
1748 if Present (Then_Actions (P)) then
1749 Insert_List_After_And_Analyze
1750 (Last (Then_Actions (P)), Ins_Actions);
1751 else
1752 Set_Then_Actions (P, Ins_Actions);
1753 Analyze_List (Then_Actions (P));
1754 end if;
1756 return;
1758 -- Actions belong to the else expression, temporarily
1759 -- place them as Else_Actions of the conditional expr.
1760 -- They will be moved to the proper place later when
1761 -- the conditional expression is expanded.
1763 elsif N = ElseX then
1764 if Present (Else_Actions (P)) then
1765 Insert_List_After_And_Analyze
1766 (Last (Else_Actions (P)), Ins_Actions);
1767 else
1768 Set_Else_Actions (P, Ins_Actions);
1769 Analyze_List (Else_Actions (P));
1770 end if;
1772 return;
1774 -- Actions belong to the condition. In this case they are
1775 -- unconditionally executed, and so we can continue the
1776 -- search for the proper insert point.
1778 else
1779 null;
1780 end if;
1781 end;
1783 -- Case of appearing in the condition of a while expression or
1784 -- elsif. We insert the actions into the Condition_Actions field.
1785 -- They will be moved further out when the while loop or elsif
1786 -- is analyzed.
1788 when N_Iteration_Scheme |
1789 N_Elsif_Part
1791 if N = Condition (P) then
1792 if Present (Condition_Actions (P)) then
1793 Insert_List_After_And_Analyze
1794 (Last (Condition_Actions (P)), Ins_Actions);
1795 else
1796 Set_Condition_Actions (P, Ins_Actions);
1798 -- Set the parent of the insert actions explicitly.
1799 -- This is not a syntactic field, but we need the
1800 -- parent field set, in particular so that freeze
1801 -- can understand that it is dealing with condition
1802 -- actions, and properly insert the freezing actions.
1804 Set_Parent (Ins_Actions, P);
1805 Analyze_List (Condition_Actions (P));
1806 end if;
1808 return;
1809 end if;
1811 -- Statements, declarations, pragmas, representation clauses
1813 when
1814 -- Statements
1816 N_Procedure_Call_Statement |
1817 N_Statement_Other_Than_Procedure_Call |
1819 -- Pragmas
1821 N_Pragma |
1823 -- Representation_Clause
1825 N_At_Clause |
1826 N_Attribute_Definition_Clause |
1827 N_Enumeration_Representation_Clause |
1828 N_Record_Representation_Clause |
1830 -- Declarations
1832 N_Abstract_Subprogram_Declaration |
1833 N_Entry_Body |
1834 N_Exception_Declaration |
1835 N_Exception_Renaming_Declaration |
1836 N_Formal_Abstract_Subprogram_Declaration |
1837 N_Formal_Concrete_Subprogram_Declaration |
1838 N_Formal_Object_Declaration |
1839 N_Formal_Type_Declaration |
1840 N_Full_Type_Declaration |
1841 N_Function_Instantiation |
1842 N_Generic_Function_Renaming_Declaration |
1843 N_Generic_Package_Declaration |
1844 N_Generic_Package_Renaming_Declaration |
1845 N_Generic_Procedure_Renaming_Declaration |
1846 N_Generic_Subprogram_Declaration |
1847 N_Implicit_Label_Declaration |
1848 N_Incomplete_Type_Declaration |
1849 N_Number_Declaration |
1850 N_Object_Declaration |
1851 N_Object_Renaming_Declaration |
1852 N_Package_Body |
1853 N_Package_Body_Stub |
1854 N_Package_Declaration |
1855 N_Package_Instantiation |
1856 N_Package_Renaming_Declaration |
1857 N_Private_Extension_Declaration |
1858 N_Private_Type_Declaration |
1859 N_Procedure_Instantiation |
1860 N_Protected_Body_Stub |
1861 N_Protected_Type_Declaration |
1862 N_Single_Task_Declaration |
1863 N_Subprogram_Body |
1864 N_Subprogram_Body_Stub |
1865 N_Subprogram_Declaration |
1866 N_Subprogram_Renaming_Declaration |
1867 N_Subtype_Declaration |
1868 N_Task_Body |
1869 N_Task_Body_Stub |
1870 N_Task_Type_Declaration |
1872 -- Freeze entity behaves like a declaration or statement
1874 N_Freeze_Entity
1876 -- Do not insert here if the item is not a list member (this
1877 -- happens for example with a triggering statement, and the
1878 -- proper approach is to insert before the entire select).
1880 if not Is_List_Member (P) then
1881 null;
1883 -- Do not insert if parent of P is an N_Component_Association
1884 -- node (i.e. we are in the context of an N_Aggregate node.
1885 -- In this case we want to insert before the entire aggregate.
1887 elsif Nkind (Parent (P)) = N_Component_Association then
1888 null;
1890 -- Do not insert if the parent of P is either an N_Variant
1891 -- node or an N_Record_Definition node, meaning in either
1892 -- case that P is a member of a component list, and that
1893 -- therefore the actions should be inserted outside the
1894 -- complete record declaration.
1896 elsif Nkind (Parent (P)) = N_Variant
1897 or else Nkind (Parent (P)) = N_Record_Definition
1898 then
1899 null;
1901 -- Do not insert freeze nodes within the loop generated for
1902 -- an aggregate, because they may be elaborated too late for
1903 -- subsequent use in the back end: within a package spec the
1904 -- loop is part of the elaboration procedure and is only
1905 -- elaborated during the second pass.
1906 -- If the loop comes from source, or the entity is local to
1907 -- the loop itself it must remain within.
1909 elsif Nkind (Parent (P)) = N_Loop_Statement
1910 and then not Comes_From_Source (Parent (P))
1911 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
1912 and then
1913 Scope (Entity (First (Ins_Actions))) /= Current_Scope
1914 then
1915 null;
1917 -- Otherwise we can go ahead and do the insertion
1919 elsif P = Wrapped_Node then
1920 Store_Before_Actions_In_Scope (Ins_Actions);
1921 return;
1923 else
1924 Insert_List_Before_And_Analyze (P, Ins_Actions);
1925 return;
1926 end if;
1928 -- A special case, N_Raise_xxx_Error can act either as a
1929 -- statement or a subexpression. We tell the difference
1930 -- by looking at the Etype. It is set to Standard_Void_Type
1931 -- in the statement case.
1933 when
1934 N_Raise_xxx_Error =>
1935 if Etype (P) = Standard_Void_Type then
1936 if P = Wrapped_Node then
1937 Store_Before_Actions_In_Scope (Ins_Actions);
1938 else
1939 Insert_List_Before_And_Analyze (P, Ins_Actions);
1940 end if;
1942 return;
1944 -- In the subexpression case, keep climbing
1946 else
1947 null;
1948 end if;
1950 -- If a component association appears within a loop created for
1951 -- an array aggregate, attach the actions to the association so
1952 -- they can be subsequently inserted within the loop. For other
1953 -- component associations insert outside of the aggregate. For
1954 -- an association that will generate a loop, its Loop_Actions
1955 -- attribute is already initialized (see exp_aggr.adb).
1957 -- The list of loop_actions can in turn generate additional ones,
1958 -- that are inserted before the associated node. If the associated
1959 -- node is outside the aggregate, the new actions are collected
1960 -- at the end of the loop actions, to respect the order in which
1961 -- they are to be elaborated.
1963 when
1964 N_Component_Association =>
1965 if Nkind (Parent (P)) = N_Aggregate
1966 and then Present (Loop_Actions (P))
1967 then
1968 if Is_Empty_List (Loop_Actions (P)) then
1969 Set_Loop_Actions (P, Ins_Actions);
1970 Analyze_List (Ins_Actions);
1972 else
1973 declare
1974 Decl : Node_Id;
1976 begin
1977 -- Check whether these actions were generated
1978 -- by a declaration that is part of the loop_
1979 -- actions for the component_association.
1981 Decl := Assoc_Node;
1982 while Present (Decl) loop
1983 exit when Parent (Decl) = P
1984 and then Is_List_Member (Decl)
1985 and then
1986 List_Containing (Decl) = Loop_Actions (P);
1987 Decl := Parent (Decl);
1988 end loop;
1990 if Present (Decl) then
1991 Insert_List_Before_And_Analyze
1992 (Decl, Ins_Actions);
1993 else
1994 Insert_List_After_And_Analyze
1995 (Last (Loop_Actions (P)), Ins_Actions);
1996 end if;
1997 end;
1998 end if;
2000 return;
2002 else
2003 null;
2004 end if;
2006 -- Another special case, an attribute denoting a procedure call
2008 when
2009 N_Attribute_Reference =>
2010 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
2011 if P = Wrapped_Node then
2012 Store_Before_Actions_In_Scope (Ins_Actions);
2013 else
2014 Insert_List_Before_And_Analyze (P, Ins_Actions);
2015 end if;
2017 return;
2019 -- In the subexpression case, keep climbing
2021 else
2022 null;
2023 end if;
2025 -- For all other node types, keep climbing tree
2027 when
2028 N_Abortable_Part |
2029 N_Accept_Alternative |
2030 N_Access_Definition |
2031 N_Access_Function_Definition |
2032 N_Access_Procedure_Definition |
2033 N_Access_To_Object_Definition |
2034 N_Aggregate |
2035 N_Allocator |
2036 N_Case_Statement_Alternative |
2037 N_Character_Literal |
2038 N_Compilation_Unit |
2039 N_Compilation_Unit_Aux |
2040 N_Component_Clause |
2041 N_Component_Declaration |
2042 N_Component_Definition |
2043 N_Component_List |
2044 N_Constrained_Array_Definition |
2045 N_Decimal_Fixed_Point_Definition |
2046 N_Defining_Character_Literal |
2047 N_Defining_Identifier |
2048 N_Defining_Operator_Symbol |
2049 N_Defining_Program_Unit_Name |
2050 N_Delay_Alternative |
2051 N_Delta_Constraint |
2052 N_Derived_Type_Definition |
2053 N_Designator |
2054 N_Digits_Constraint |
2055 N_Discriminant_Association |
2056 N_Discriminant_Specification |
2057 N_Empty |
2058 N_Entry_Body_Formal_Part |
2059 N_Entry_Call_Alternative |
2060 N_Entry_Declaration |
2061 N_Entry_Index_Specification |
2062 N_Enumeration_Type_Definition |
2063 N_Error |
2064 N_Exception_Handler |
2065 N_Expanded_Name |
2066 N_Explicit_Dereference |
2067 N_Extension_Aggregate |
2068 N_Floating_Point_Definition |
2069 N_Formal_Decimal_Fixed_Point_Definition |
2070 N_Formal_Derived_Type_Definition |
2071 N_Formal_Discrete_Type_Definition |
2072 N_Formal_Floating_Point_Definition |
2073 N_Formal_Modular_Type_Definition |
2074 N_Formal_Ordinary_Fixed_Point_Definition |
2075 N_Formal_Package_Declaration |
2076 N_Formal_Private_Type_Definition |
2077 N_Formal_Signed_Integer_Type_Definition |
2078 N_Function_Call |
2079 N_Function_Specification |
2080 N_Generic_Association |
2081 N_Handled_Sequence_Of_Statements |
2082 N_Identifier |
2083 N_In |
2084 N_Index_Or_Discriminant_Constraint |
2085 N_Indexed_Component |
2086 N_Integer_Literal |
2087 N_Itype_Reference |
2088 N_Label |
2089 N_Loop_Parameter_Specification |
2090 N_Mod_Clause |
2091 N_Modular_Type_Definition |
2092 N_Not_In |
2093 N_Null |
2094 N_Op_Abs |
2095 N_Op_Add |
2096 N_Op_And |
2097 N_Op_Concat |
2098 N_Op_Divide |
2099 N_Op_Eq |
2100 N_Op_Expon |
2101 N_Op_Ge |
2102 N_Op_Gt |
2103 N_Op_Le |
2104 N_Op_Lt |
2105 N_Op_Minus |
2106 N_Op_Mod |
2107 N_Op_Multiply |
2108 N_Op_Ne |
2109 N_Op_Not |
2110 N_Op_Or |
2111 N_Op_Plus |
2112 N_Op_Rem |
2113 N_Op_Rotate_Left |
2114 N_Op_Rotate_Right |
2115 N_Op_Shift_Left |
2116 N_Op_Shift_Right |
2117 N_Op_Shift_Right_Arithmetic |
2118 N_Op_Subtract |
2119 N_Op_Xor |
2120 N_Operator_Symbol |
2121 N_Ordinary_Fixed_Point_Definition |
2122 N_Others_Choice |
2123 N_Package_Specification |
2124 N_Parameter_Association |
2125 N_Parameter_Specification |
2126 N_Pragma_Argument_Association |
2127 N_Procedure_Specification |
2128 N_Protected_Body |
2129 N_Protected_Definition |
2130 N_Qualified_Expression |
2131 N_Range |
2132 N_Range_Constraint |
2133 N_Real_Literal |
2134 N_Real_Range_Specification |
2135 N_Record_Definition |
2136 N_Reference |
2137 N_Selected_Component |
2138 N_Signed_Integer_Type_Definition |
2139 N_Single_Protected_Declaration |
2140 N_Slice |
2141 N_String_Literal |
2142 N_Subprogram_Info |
2143 N_Subtype_Indication |
2144 N_Subunit |
2145 N_Task_Definition |
2146 N_Terminate_Alternative |
2147 N_Triggering_Alternative |
2148 N_Type_Conversion |
2149 N_Unchecked_Expression |
2150 N_Unchecked_Type_Conversion |
2151 N_Unconstrained_Array_Definition |
2152 N_Unused_At_End |
2153 N_Unused_At_Start |
2154 N_Use_Package_Clause |
2155 N_Use_Type_Clause |
2156 N_Variant |
2157 N_Variant_Part |
2158 N_Validate_Unchecked_Conversion |
2159 N_With_Clause |
2160 N_With_Type_Clause
2162 null;
2164 end case;
2166 -- Make sure that inserted actions stay in the transient scope
2168 if P = Wrapped_Node then
2169 Store_Before_Actions_In_Scope (Ins_Actions);
2170 return;
2171 end if;
2173 -- If we fall through above tests, keep climbing tree
2175 N := P;
2177 if Nkind (Parent (N)) = N_Subunit then
2179 -- This is the proper body corresponding to a stub. Insertion
2180 -- must be done at the point of the stub, which is in the decla-
2181 -- tive part of the parent unit.
2183 P := Corresponding_Stub (Parent (N));
2185 else
2186 P := Parent (N);
2187 end if;
2188 end loop;
2190 end Insert_Actions;
2192 -- Version with check(s) suppressed
2194 procedure Insert_Actions
2195 (Assoc_Node : Node_Id; Ins_Actions : List_Id; Suppress : Check_Id)
2197 begin
2198 if Suppress = All_Checks then
2199 declare
2200 Svg : constant Suppress_Array := Scope_Suppress;
2202 begin
2203 Scope_Suppress := (others => True);
2204 Insert_Actions (Assoc_Node, Ins_Actions);
2205 Scope_Suppress := Svg;
2206 end;
2208 else
2209 declare
2210 Svg : constant Boolean := Scope_Suppress (Suppress);
2212 begin
2213 Scope_Suppress (Suppress) := True;
2214 Insert_Actions (Assoc_Node, Ins_Actions);
2215 Scope_Suppress (Suppress) := Svg;
2216 end;
2217 end if;
2218 end Insert_Actions;
2220 --------------------------
2221 -- Insert_Actions_After --
2222 --------------------------
2224 procedure Insert_Actions_After
2225 (Assoc_Node : Node_Id;
2226 Ins_Actions : List_Id)
2228 begin
2229 if Scope_Is_Transient
2230 and then Assoc_Node = Node_To_Be_Wrapped
2231 then
2232 Store_After_Actions_In_Scope (Ins_Actions);
2233 else
2234 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
2235 end if;
2236 end Insert_Actions_After;
2238 ---------------------------------
2239 -- Insert_Library_Level_Action --
2240 ---------------------------------
2242 procedure Insert_Library_Level_Action (N : Node_Id) is
2243 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2245 begin
2246 New_Scope (Cunit_Entity (Main_Unit));
2248 if No (Actions (Aux)) then
2249 Set_Actions (Aux, New_List (N));
2250 else
2251 Append (N, Actions (Aux));
2252 end if;
2254 Analyze (N);
2255 Pop_Scope;
2256 end Insert_Library_Level_Action;
2258 ----------------------------------
2259 -- Insert_Library_Level_Actions --
2260 ----------------------------------
2262 procedure Insert_Library_Level_Actions (L : List_Id) is
2263 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2265 begin
2266 if Is_Non_Empty_List (L) then
2267 New_Scope (Cunit_Entity (Main_Unit));
2269 if No (Actions (Aux)) then
2270 Set_Actions (Aux, L);
2271 Analyze_List (L);
2272 else
2273 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
2274 end if;
2276 Pop_Scope;
2277 end if;
2278 end Insert_Library_Level_Actions;
2280 ----------------------
2281 -- Inside_Init_Proc --
2282 ----------------------
2284 function Inside_Init_Proc return Boolean is
2285 S : Entity_Id;
2287 begin
2288 S := Current_Scope;
2289 while Present (S)
2290 and then S /= Standard_Standard
2291 loop
2292 if Is_Init_Proc (S) then
2293 return True;
2294 else
2295 S := Scope (S);
2296 end if;
2297 end loop;
2299 return False;
2300 end Inside_Init_Proc;
2302 ----------------------------
2303 -- Is_All_Null_Statements --
2304 ----------------------------
2306 function Is_All_Null_Statements (L : List_Id) return Boolean is
2307 Stm : Node_Id;
2309 begin
2310 Stm := First (L);
2311 while Present (Stm) loop
2312 if Nkind (Stm) /= N_Null_Statement then
2313 return False;
2314 end if;
2316 Next (Stm);
2317 end loop;
2319 return True;
2320 end Is_All_Null_Statements;
2322 ----------------------------------
2323 -- Is_Possibly_Unaligned_Object --
2324 ----------------------------------
2326 function Is_Possibly_Unaligned_Object (P : Node_Id) return Boolean is
2327 begin
2328 -- If target does not have strict alignment, result is always
2329 -- False, since correctness of code does no depend on alignment.
2331 if not Target_Strict_Alignment then
2332 return False;
2333 end if;
2335 -- If renamed object, apply test to underlying object
2337 if Is_Entity_Name (P)
2338 and then Is_Object (Entity (P))
2339 and then Present (Renamed_Object (Entity (P)))
2340 then
2341 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (P)));
2342 end if;
2344 -- If this is an element of a packed array, may be unaligned
2346 if Is_Ref_To_Bit_Packed_Array (P) then
2347 return True;
2348 end if;
2350 -- Case of component reference
2352 if Nkind (P) = N_Selected_Component then
2354 -- If component reference is for a record that is bit packed
2355 -- or has a specified alignment (that might be too small) or
2356 -- the component reference has a component clause, then the
2357 -- object may be unaligned.
2359 if Is_Packed (Etype (Prefix (P)))
2360 or else Known_Alignment (Etype (Prefix (P)))
2361 or else Present (Component_Clause (Entity (Selector_Name (P))))
2362 then
2363 return True;
2365 -- Otherwise, for a component reference, test prefix
2367 else
2368 return Is_Possibly_Unaligned_Object (Prefix (P));
2369 end if;
2371 -- If not a component reference, must be aligned
2373 else
2374 return False;
2375 end if;
2376 end Is_Possibly_Unaligned_Object;
2378 ---------------------------------
2379 -- Is_Possibly_Unaligned_Slice --
2380 ---------------------------------
2382 function Is_Possibly_Unaligned_Slice (P : Node_Id) return Boolean is
2383 begin
2384 -- ??? GCC3 will eventually handle strings with arbitrary alignments,
2385 -- but for now the following check must be disabled.
2387 -- if get_gcc_version >= 3 then
2388 -- return False;
2389 -- end if;
2391 -- For renaming case, go to renamed object
2393 if Is_Entity_Name (P)
2394 and then Is_Object (Entity (P))
2395 and then Present (Renamed_Object (Entity (P)))
2396 then
2397 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (P)));
2398 end if;
2400 -- The reference must be a slice
2402 if Nkind (P) /= N_Slice then
2403 return False;
2404 end if;
2406 -- Always assume the worst for a nested record component with a
2407 -- component clause, which gigi/gcc does not appear to handle well.
2408 -- It is not clear why this special test is needed at all ???
2410 if Nkind (Prefix (P)) = N_Selected_Component
2411 and then Nkind (Prefix (Prefix (P))) = N_Selected_Component
2412 and then
2413 Present (Component_Clause (Entity (Selector_Name (Prefix (P)))))
2414 then
2415 return True;
2416 end if;
2418 -- We only need to worry if the target has strict alignment
2420 if not Target_Strict_Alignment then
2421 return False;
2422 end if;
2424 -- If it is a slice, then look at the array type being sliced
2426 declare
2427 Sarr : constant Node_Id := Prefix (P);
2428 -- Prefix of the slice, i.e. the array being sliced
2430 Styp : constant Entity_Id := Etype (Prefix (P));
2431 -- Type of the array being sliced
2433 Pref : Node_Id;
2434 Ptyp : Entity_Id;
2436 begin
2437 -- The problems arise if the array object that is being sliced
2438 -- is a component of a record or array, and we cannot guarantee
2439 -- the alignment of the array within its containing object.
2441 -- To investigate this, we look at successive prefixes to see
2442 -- if we have a worrisome indexed or selected component.
2444 Pref := Sarr;
2445 loop
2446 -- Case of array is part of an indexed component reference
2448 if Nkind (Pref) = N_Indexed_Component then
2449 Ptyp := Etype (Prefix (Pref));
2451 -- The only problematic case is when the array is packed,
2452 -- in which case we really know nothing about the alignment
2453 -- of individual components.
2455 if Is_Bit_Packed_Array (Ptyp) then
2456 return True;
2457 end if;
2459 -- Case of array is part of a selected component reference
2461 elsif Nkind (Pref) = N_Selected_Component then
2462 Ptyp := Etype (Prefix (Pref));
2464 -- We are definitely in trouble if the record in question
2465 -- has an alignment, and either we know this alignment is
2466 -- inconsistent with the alignment of the slice, or we
2467 -- don't know what the alignment of the slice should be.
2469 if Known_Alignment (Ptyp)
2470 and then (Unknown_Alignment (Styp)
2471 or else Alignment (Styp) > Alignment (Ptyp))
2472 then
2473 return True;
2474 end if;
2476 -- We are in potential trouble if the record type is packed.
2477 -- We could special case when we know that the array is the
2478 -- first component, but that's not such a simple case ???
2480 if Is_Packed (Ptyp) then
2481 return True;
2482 end if;
2484 -- We are in trouble if there is a component clause, and
2485 -- either we do not know the alignment of the slice, or
2486 -- the alignment of the slice is inconsistent with the
2487 -- bit position specified by the component clause.
2489 declare
2490 Field : constant Entity_Id := Entity (Selector_Name (Pref));
2491 begin
2492 if Present (Component_Clause (Field))
2493 and then
2494 (Unknown_Alignment (Styp)
2495 or else
2496 (Component_Bit_Offset (Field) mod
2497 (System_Storage_Unit * Alignment (Styp))) /= 0)
2498 then
2499 return True;
2500 end if;
2501 end;
2503 -- For cases other than selected or indexed components we
2504 -- know we are OK, since no issues arise over alignment.
2506 else
2507 return False;
2508 end if;
2510 -- We processed an indexed component or selected component
2511 -- reference that looked safe, so keep checking prefixes.
2513 Pref := Prefix (Pref);
2514 end loop;
2515 end;
2516 end Is_Possibly_Unaligned_Slice;
2518 --------------------------------
2519 -- Is_Ref_To_Bit_Packed_Array --
2520 --------------------------------
2522 function Is_Ref_To_Bit_Packed_Array (P : Node_Id) return Boolean is
2523 Result : Boolean;
2524 Expr : Node_Id;
2526 begin
2527 if Is_Entity_Name (P)
2528 and then Is_Object (Entity (P))
2529 and then Present (Renamed_Object (Entity (P)))
2530 then
2531 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (P)));
2532 end if;
2534 if Nkind (P) = N_Indexed_Component
2535 or else
2536 Nkind (P) = N_Selected_Component
2537 then
2538 if Is_Bit_Packed_Array (Etype (Prefix (P))) then
2539 Result := True;
2540 else
2541 Result := Is_Ref_To_Bit_Packed_Array (Prefix (P));
2542 end if;
2544 if Result and then Nkind (P) = N_Indexed_Component then
2545 Expr := First (Expressions (P));
2546 while Present (Expr) loop
2547 Force_Evaluation (Expr);
2548 Next (Expr);
2549 end loop;
2550 end if;
2552 return Result;
2554 else
2555 return False;
2556 end if;
2557 end Is_Ref_To_Bit_Packed_Array;
2559 --------------------------------
2560 -- Is_Ref_To_Bit_Packed_Slice --
2561 --------------------------------
2563 function Is_Ref_To_Bit_Packed_Slice (P : Node_Id) return Boolean is
2564 begin
2565 if Is_Entity_Name (P)
2566 and then Is_Object (Entity (P))
2567 and then Present (Renamed_Object (Entity (P)))
2568 then
2569 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (P)));
2570 end if;
2572 if Nkind (P) = N_Slice
2573 and then Is_Bit_Packed_Array (Etype (Prefix (P)))
2574 then
2575 return True;
2577 elsif Nkind (P) = N_Indexed_Component
2578 or else
2579 Nkind (P) = N_Selected_Component
2580 then
2581 return Is_Ref_To_Bit_Packed_Slice (Prefix (P));
2583 else
2584 return False;
2585 end if;
2586 end Is_Ref_To_Bit_Packed_Slice;
2588 -----------------------
2589 -- Is_Renamed_Object --
2590 -----------------------
2592 function Is_Renamed_Object (N : Node_Id) return Boolean is
2593 Pnod : constant Node_Id := Parent (N);
2594 Kind : constant Node_Kind := Nkind (Pnod);
2596 begin
2597 if Kind = N_Object_Renaming_Declaration then
2598 return True;
2600 elsif Kind = N_Indexed_Component
2601 or else Kind = N_Selected_Component
2602 then
2603 return Is_Renamed_Object (Pnod);
2605 else
2606 return False;
2607 end if;
2608 end Is_Renamed_Object;
2610 ----------------------------
2611 -- Is_Untagged_Derivation --
2612 ----------------------------
2614 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
2615 begin
2616 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
2617 or else
2618 (Is_Private_Type (T) and then Present (Full_View (T))
2619 and then not Is_Tagged_Type (Full_View (T))
2620 and then Is_Derived_Type (Full_View (T))
2621 and then Etype (Full_View (T)) /= T);
2623 end Is_Untagged_Derivation;
2625 --------------------
2626 -- Kill_Dead_Code --
2627 --------------------
2629 procedure Kill_Dead_Code (N : Node_Id) is
2630 begin
2631 if Present (N) then
2632 Remove_Handler_Entries (N);
2633 Remove_Warning_Messages (N);
2635 -- Recurse into block statements and bodies to process declarations
2636 -- and statements
2638 if Nkind (N) = N_Block_Statement
2639 or else Nkind (N) = N_Subprogram_Body
2640 or else Nkind (N) = N_Package_Body
2641 then
2642 Kill_Dead_Code (Declarations (N));
2643 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
2645 if Nkind (N) = N_Subprogram_Body then
2646 Set_Is_Eliminated (Defining_Entity (N));
2647 end if;
2649 -- Recurse into composite statement to kill individual statements,
2650 -- in particular instantiations.
2652 elsif Nkind (N) = N_If_Statement then
2653 Kill_Dead_Code (Then_Statements (N));
2654 Kill_Dead_Code (Elsif_Parts (N));
2655 Kill_Dead_Code (Else_Statements (N));
2657 elsif Nkind (N) = N_Loop_Statement then
2658 Kill_Dead_Code (Statements (N));
2660 elsif Nkind (N) = N_Case_Statement then
2661 declare
2662 Alt : Node_Id;
2663 begin
2664 Alt := First (Alternatives (N));
2665 while Present (Alt) loop
2666 Kill_Dead_Code (Statements (Alt));
2667 Next (Alt);
2668 end loop;
2669 end;
2671 elsif Nkind (N) = N_Case_Statement_Alternative then
2672 Kill_Dead_Code (Statements (N));
2674 -- Deal with dead instances caused by deleting instantiations
2676 elsif Nkind (N) in N_Generic_Instantiation then
2677 Remove_Dead_Instance (N);
2678 end if;
2680 Delete_Tree (N);
2681 end if;
2682 end Kill_Dead_Code;
2684 -- Case where argument is a list of nodes to be killed
2686 procedure Kill_Dead_Code (L : List_Id) is
2687 N : Node_Id;
2689 begin
2690 if Is_Non_Empty_List (L) then
2691 loop
2692 N := Remove_Head (L);
2693 exit when No (N);
2694 Kill_Dead_Code (N);
2695 end loop;
2696 end if;
2697 end Kill_Dead_Code;
2699 ------------------------
2700 -- Known_Non_Negative --
2701 ------------------------
2703 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
2704 begin
2705 if Is_OK_Static_Expression (Opnd)
2706 and then Expr_Value (Opnd) >= 0
2707 then
2708 return True;
2710 else
2711 declare
2712 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
2714 begin
2715 return
2716 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
2717 end;
2718 end if;
2719 end Known_Non_Negative;
2721 --------------------
2722 -- Known_Non_Null --
2723 --------------------
2725 function Known_Non_Null (N : Node_Id) return Boolean is
2726 begin
2727 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N))));
2729 -- Case of entity for which Is_Known_Non_Null is True
2731 if Is_Entity_Name (N) and then Is_Known_Non_Null (Entity (N)) then
2733 -- If the entity is aliased or volatile, then we decide that
2734 -- we don't know it is really non-null even if the sequential
2735 -- flow indicates that it is, since such variables can be
2736 -- changed without us noticing.
2738 if Is_Aliased (Entity (N))
2739 or else Treat_As_Volatile (Entity (N))
2740 then
2741 return False;
2743 -- For all other cases, the flag is decisive
2745 else
2746 return True;
2747 end if;
2749 -- True if access attribute
2751 elsif Nkind (N) = N_Attribute_Reference
2752 and then (Attribute_Name (N) = Name_Access
2753 or else
2754 Attribute_Name (N) = Name_Unchecked_Access
2755 or else
2756 Attribute_Name (N) = Name_Unrestricted_Access)
2757 then
2758 return True;
2760 -- True if allocator
2762 elsif Nkind (N) = N_Allocator then
2763 return True;
2765 -- For a conversion, true if expression is known non-null
2767 elsif Nkind (N) = N_Type_Conversion then
2768 return Known_Non_Null (Expression (N));
2770 -- One more case is when Current_Value references a condition
2771 -- that ensures a non-null value.
2773 elsif Is_Entity_Name (N) then
2774 declare
2775 Op : Node_Kind;
2776 Val : Node_Id;
2778 begin
2779 Get_Current_Value_Condition (N, Op, Val);
2780 return Op = N_Op_Ne and then Nkind (Val) = N_Null;
2781 end;
2783 -- Above are all cases where the value could be determined to be
2784 -- non-null. In all other cases, we don't know, so return False.
2786 else
2787 return False;
2788 end if;
2789 end Known_Non_Null;
2791 -----------------------------
2792 -- Make_CW_Equivalent_Type --
2793 -----------------------------
2795 -- Create a record type used as an equivalent of any member
2796 -- of the class which takes its size from exp.
2798 -- Generate the following code:
2800 -- type Equiv_T is record
2801 -- _parent : T (List of discriminant constaints taken from Exp);
2802 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
2803 -- end Equiv_T;
2805 -- ??? Note that this type does not guarantee same alignment as all
2806 -- derived types
2808 function Make_CW_Equivalent_Type
2809 (T : Entity_Id;
2810 E : Node_Id) return Entity_Id
2812 Loc : constant Source_Ptr := Sloc (E);
2813 Root_Typ : constant Entity_Id := Root_Type (T);
2814 List_Def : constant List_Id := Empty_List;
2815 Equiv_Type : Entity_Id;
2816 Range_Type : Entity_Id;
2817 Str_Type : Entity_Id;
2818 Constr_Root : Entity_Id;
2819 Sizexpr : Node_Id;
2821 begin
2822 if not Has_Discriminants (Root_Typ) then
2823 Constr_Root := Root_Typ;
2824 else
2825 Constr_Root :=
2826 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
2828 -- subtype cstr__n is T (List of discr constraints taken from Exp)
2830 Append_To (List_Def,
2831 Make_Subtype_Declaration (Loc,
2832 Defining_Identifier => Constr_Root,
2833 Subtype_Indication =>
2834 Make_Subtype_From_Expr (E, Root_Typ)));
2835 end if;
2837 -- subtype rg__xx is Storage_Offset range
2838 -- (Expr'size - typ'size) / Storage_Unit
2840 Range_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
2842 Sizexpr :=
2843 Make_Op_Subtract (Loc,
2844 Left_Opnd =>
2845 Make_Attribute_Reference (Loc,
2846 Prefix =>
2847 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
2848 Attribute_Name => Name_Size),
2849 Right_Opnd =>
2850 Make_Attribute_Reference (Loc,
2851 Prefix => New_Reference_To (Constr_Root, Loc),
2852 Attribute_Name => Name_Object_Size));
2854 Set_Paren_Count (Sizexpr, 1);
2856 Append_To (List_Def,
2857 Make_Subtype_Declaration (Loc,
2858 Defining_Identifier => Range_Type,
2859 Subtype_Indication =>
2860 Make_Subtype_Indication (Loc,
2861 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
2862 Constraint => Make_Range_Constraint (Loc,
2863 Range_Expression =>
2864 Make_Range (Loc,
2865 Low_Bound => Make_Integer_Literal (Loc, 1),
2866 High_Bound =>
2867 Make_Op_Divide (Loc,
2868 Left_Opnd => Sizexpr,
2869 Right_Opnd => Make_Integer_Literal (Loc,
2870 Intval => System_Storage_Unit)))))));
2872 -- subtype str__nn is Storage_Array (rg__x);
2874 Str_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
2875 Append_To (List_Def,
2876 Make_Subtype_Declaration (Loc,
2877 Defining_Identifier => Str_Type,
2878 Subtype_Indication =>
2879 Make_Subtype_Indication (Loc,
2880 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
2881 Constraint =>
2882 Make_Index_Or_Discriminant_Constraint (Loc,
2883 Constraints =>
2884 New_List (New_Reference_To (Range_Type, Loc))))));
2886 -- type Equiv_T is record
2887 -- _parent : Tnn;
2888 -- E : Str_Type;
2889 -- end Equiv_T;
2891 Equiv_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
2893 -- When the target requires front-end layout, it's necessary to allow
2894 -- the equivalent type to be frozen so that layout can occur (when the
2895 -- associated class-wide subtype is frozen, the equivalent type will
2896 -- be frozen, see freeze.adb). For other targets, Gigi wants to have
2897 -- the equivalent type marked as frozen and deals with this type itself.
2898 -- In the Gigi case this will also avoid the generation of an init
2899 -- procedure for the type.
2901 if not Frontend_Layout_On_Target then
2902 Set_Is_Frozen (Equiv_Type);
2903 end if;
2905 Set_Ekind (Equiv_Type, E_Record_Type);
2906 Set_Parent_Subtype (Equiv_Type, Constr_Root);
2908 Append_To (List_Def,
2909 Make_Full_Type_Declaration (Loc,
2910 Defining_Identifier => Equiv_Type,
2912 Type_Definition =>
2913 Make_Record_Definition (Loc,
2914 Component_List => Make_Component_List (Loc,
2915 Component_Items => New_List (
2916 Make_Component_Declaration (Loc,
2917 Defining_Identifier =>
2918 Make_Defining_Identifier (Loc, Name_uParent),
2919 Component_Definition =>
2920 Make_Component_Definition (Loc,
2921 Aliased_Present => False,
2922 Subtype_Indication =>
2923 New_Reference_To (Constr_Root, Loc))),
2925 Make_Component_Declaration (Loc,
2926 Defining_Identifier =>
2927 Make_Defining_Identifier (Loc,
2928 Chars => New_Internal_Name ('C')),
2929 Component_Definition =>
2930 Make_Component_Definition (Loc,
2931 Aliased_Present => False,
2932 Subtype_Indication =>
2933 New_Reference_To (Str_Type, Loc)))),
2935 Variant_Part => Empty))));
2937 Insert_Actions (E, List_Def);
2938 return Equiv_Type;
2939 end Make_CW_Equivalent_Type;
2941 ------------------------
2942 -- Make_Literal_Range --
2943 ------------------------
2945 function Make_Literal_Range
2946 (Loc : Source_Ptr;
2947 Literal_Typ : Entity_Id) return Node_Id
2949 Lo : constant Node_Id :=
2950 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
2952 begin
2953 Set_Analyzed (Lo, False);
2955 return
2956 Make_Range (Loc,
2957 Low_Bound => Lo,
2959 High_Bound =>
2960 Make_Op_Subtract (Loc,
2961 Left_Opnd =>
2962 Make_Op_Add (Loc,
2963 Left_Opnd => New_Copy_Tree (Lo),
2964 Right_Opnd =>
2965 Make_Integer_Literal (Loc,
2966 String_Literal_Length (Literal_Typ))),
2967 Right_Opnd => Make_Integer_Literal (Loc, 1)));
2968 end Make_Literal_Range;
2970 ----------------------------
2971 -- Make_Subtype_From_Expr --
2972 ----------------------------
2974 -- 1. If Expr is an uncontrained array expression, creates
2975 -- Unc_Type(Expr'first(1)..Expr'Last(1),..., Expr'first(n)..Expr'last(n))
2977 -- 2. If Expr is a unconstrained discriminated type expression, creates
2978 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
2980 -- 3. If Expr is class-wide, creates an implicit class wide subtype
2982 function Make_Subtype_From_Expr
2983 (E : Node_Id;
2984 Unc_Typ : Entity_Id) return Node_Id
2986 Loc : constant Source_Ptr := Sloc (E);
2987 List_Constr : constant List_Id := New_List;
2988 D : Entity_Id;
2990 Full_Subtyp : Entity_Id;
2991 Priv_Subtyp : Entity_Id;
2992 Utyp : Entity_Id;
2993 Full_Exp : Node_Id;
2995 begin
2996 if Is_Private_Type (Unc_Typ)
2997 and then Has_Unknown_Discriminants (Unc_Typ)
2998 then
2999 -- Prepare the subtype completion, Go to base type to
3000 -- find underlying type.
3002 Utyp := Underlying_Type (Base_Type (Unc_Typ));
3003 Full_Subtyp := Make_Defining_Identifier (Loc,
3004 New_Internal_Name ('C'));
3005 Full_Exp :=
3006 Unchecked_Convert_To
3007 (Utyp, Duplicate_Subexpr_No_Checks (E));
3008 Set_Parent (Full_Exp, Parent (E));
3010 Priv_Subtyp :=
3011 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3013 Insert_Action (E,
3014 Make_Subtype_Declaration (Loc,
3015 Defining_Identifier => Full_Subtyp,
3016 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
3018 -- Define the dummy private subtype
3020 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
3021 Set_Etype (Priv_Subtyp, Unc_Typ);
3022 Set_Scope (Priv_Subtyp, Full_Subtyp);
3023 Set_Is_Constrained (Priv_Subtyp);
3024 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
3025 Set_Is_Itype (Priv_Subtyp);
3026 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
3028 if Is_Tagged_Type (Priv_Subtyp) then
3029 Set_Class_Wide_Type
3030 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
3031 Set_Primitive_Operations (Priv_Subtyp,
3032 Primitive_Operations (Unc_Typ));
3033 end if;
3035 Set_Full_View (Priv_Subtyp, Full_Subtyp);
3037 return New_Reference_To (Priv_Subtyp, Loc);
3039 elsif Is_Array_Type (Unc_Typ) then
3040 for J in 1 .. Number_Dimensions (Unc_Typ) loop
3041 Append_To (List_Constr,
3042 Make_Range (Loc,
3043 Low_Bound =>
3044 Make_Attribute_Reference (Loc,
3045 Prefix => Duplicate_Subexpr_No_Checks (E),
3046 Attribute_Name => Name_First,
3047 Expressions => New_List (
3048 Make_Integer_Literal (Loc, J))),
3050 High_Bound =>
3051 Make_Attribute_Reference (Loc,
3052 Prefix => Duplicate_Subexpr_No_Checks (E),
3053 Attribute_Name => Name_Last,
3054 Expressions => New_List (
3055 Make_Integer_Literal (Loc, J)))));
3056 end loop;
3058 elsif Is_Class_Wide_Type (Unc_Typ) then
3059 declare
3060 CW_Subtype : Entity_Id;
3061 EQ_Typ : Entity_Id := Empty;
3063 begin
3064 -- A class-wide equivalent type is not needed when Java_VM
3065 -- because the JVM back end handles the class-wide object
3066 -- initialization itself (and doesn't need or want the
3067 -- additional intermediate type to handle the assignment).
3069 if Expander_Active and then not Java_VM then
3070 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
3071 end if;
3073 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
3074 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
3076 if Present (EQ_Typ) then
3077 Set_Is_Class_Wide_Equivalent_Type (EQ_Typ);
3078 end if;
3080 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
3082 return New_Occurrence_Of (CW_Subtype, Loc);
3083 end;
3085 -- Comment needed (what case is this ???)
3087 else
3088 D := First_Discriminant (Unc_Typ);
3089 while Present (D) loop
3090 Append_To (List_Constr,
3091 Make_Selected_Component (Loc,
3092 Prefix => Duplicate_Subexpr_No_Checks (E),
3093 Selector_Name => New_Reference_To (D, Loc)));
3095 Next_Discriminant (D);
3096 end loop;
3097 end if;
3099 return
3100 Make_Subtype_Indication (Loc,
3101 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
3102 Constraint =>
3103 Make_Index_Or_Discriminant_Constraint (Loc,
3104 Constraints => List_Constr));
3105 end Make_Subtype_From_Expr;
3107 -----------------------------
3108 -- May_Generate_Large_Temp --
3109 -----------------------------
3111 -- At the current time, the only types that we return False for (i.e.
3112 -- where we decide we know they cannot generate large temps) are ones
3113 -- where we know the size is 128 bits or less at compile time, and we
3114 -- are still not doing a thorough job on arrays and records ???
3116 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
3117 begin
3118 if not Size_Known_At_Compile_Time (Typ) then
3119 return False;
3121 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
3122 return False;
3124 elsif Is_Array_Type (Typ)
3125 and then Present (Packed_Array_Type (Typ))
3126 then
3127 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
3129 -- We could do more here to find other small types ???
3131 else
3132 return True;
3133 end if;
3134 end May_Generate_Large_Temp;
3136 ----------------------------
3137 -- New_Class_Wide_Subtype --
3138 ----------------------------
3140 function New_Class_Wide_Subtype
3141 (CW_Typ : Entity_Id;
3142 N : Node_Id) return Entity_Id
3144 Res : constant Entity_Id := Create_Itype (E_Void, N);
3145 Res_Name : constant Name_Id := Chars (Res);
3146 Res_Scope : constant Entity_Id := Scope (Res);
3148 begin
3149 Copy_Node (CW_Typ, Res);
3150 Set_Sloc (Res, Sloc (N));
3151 Set_Is_Itype (Res);
3152 Set_Associated_Node_For_Itype (Res, N);
3153 Set_Is_Public (Res, False); -- By default, may be changed below.
3154 Set_Public_Status (Res);
3155 Set_Chars (Res, Res_Name);
3156 Set_Scope (Res, Res_Scope);
3157 Set_Ekind (Res, E_Class_Wide_Subtype);
3158 Set_Next_Entity (Res, Empty);
3159 Set_Etype (Res, Base_Type (CW_Typ));
3161 -- For targets where front-end layout is required, reset the Is_Frozen
3162 -- status of the subtype to False (it can be implicitly set to true
3163 -- from the copy of the class-wide type). For other targets, Gigi
3164 -- doesn't want the class-wide subtype to go through the freezing
3165 -- process (though it's unclear why that causes problems and it would
3166 -- be nice to allow freezing to occur normally for all targets ???).
3168 if Frontend_Layout_On_Target then
3169 Set_Is_Frozen (Res, False);
3170 end if;
3172 Set_Freeze_Node (Res, Empty);
3173 return (Res);
3174 end New_Class_Wide_Subtype;
3176 -------------------------
3177 -- Remove_Side_Effects --
3178 -------------------------
3180 procedure Remove_Side_Effects
3181 (Exp : Node_Id;
3182 Name_Req : Boolean := False;
3183 Variable_Ref : Boolean := False)
3185 Loc : constant Source_Ptr := Sloc (Exp);
3186 Exp_Type : constant Entity_Id := Etype (Exp);
3187 Svg_Suppress : constant Suppress_Array := Scope_Suppress;
3188 Def_Id : Entity_Id;
3189 Ref_Type : Entity_Id;
3190 Res : Node_Id;
3191 Ptr_Typ_Decl : Node_Id;
3192 New_Exp : Node_Id;
3193 E : Node_Id;
3195 function Side_Effect_Free (N : Node_Id) return Boolean;
3196 -- Determines if the tree N represents an expession that is known
3197 -- not to have side effects, and for which no processing is required.
3199 function Side_Effect_Free (L : List_Id) return Boolean;
3200 -- Determines if all elements of the list L are side effect free
3202 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
3203 -- The argument N is a construct where the Prefix is dereferenced
3204 -- if it is a an access type and the result is a variable. The call
3205 -- returns True if the construct is side effect free (not considering
3206 -- side effects in other than the prefix which are to be tested by the
3207 -- caller).
3209 function Within_In_Parameter (N : Node_Id) return Boolean;
3210 -- Determines if N is a subcomponent of a composite in-parameter.
3211 -- If so, N is not side-effect free when the actual is global and
3212 -- modifiable indirectly from within a subprogram, because it may
3213 -- be passed by reference. The front-end must be conservative here
3214 -- and assume that this may happen with any array or record type.
3215 -- On the other hand, we cannot create temporaries for all expressions
3216 -- for which this condition is true, for various reasons that might
3217 -- require clearing up ??? For example, descriminant references that
3218 -- appear out of place, or spurious type errors with class-wide
3219 -- expressions. As a result, we limit the transformation to loop
3220 -- bounds, which is so far the only case that requires it.
3222 -----------------------------
3223 -- Safe_Prefixed_Reference --
3224 -----------------------------
3226 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
3227 begin
3228 -- If prefix is not side effect free, definitely not safe
3230 if not Side_Effect_Free (Prefix (N)) then
3231 return False;
3233 -- If the prefix is of an access type that is not access-to-constant,
3234 -- then this construct is a variable reference, which means it is to
3235 -- be considered to have side effects if Variable_Ref is set True
3236 -- Exception is an access to an entity that is a constant or an
3237 -- in-parameter which does not come from source, and is the result
3238 -- of a previous removal of side-effects.
3240 elsif Is_Access_Type (Etype (Prefix (N)))
3241 and then not Is_Access_Constant (Etype (Prefix (N)))
3242 and then Variable_Ref
3243 then
3244 if not Is_Entity_Name (Prefix (N)) then
3245 return False;
3246 else
3247 return Ekind (Entity (Prefix (N))) = E_Constant
3248 or else Ekind (Entity (Prefix (N))) = E_In_Parameter;
3249 end if;
3251 -- The following test is the simplest way of solving a complex
3252 -- problem uncovered by BB08-010: Side effect on loop bound that
3253 -- is a subcomponent of a global variable:
3254 -- If a loop bound is a subcomponent of a global variable, a
3255 -- modification of that variable within the loop may incorrectly
3256 -- affect the execution of the loop.
3258 elsif not
3259 (Nkind (Parent (Parent (N))) /= N_Loop_Parameter_Specification
3260 or else not Within_In_Parameter (Prefix (N)))
3261 then
3262 return False;
3264 -- All other cases are side effect free
3266 else
3267 return True;
3268 end if;
3269 end Safe_Prefixed_Reference;
3271 ----------------------
3272 -- Side_Effect_Free --
3273 ----------------------
3275 function Side_Effect_Free (N : Node_Id) return Boolean is
3276 begin
3277 -- Note on checks that could raise Constraint_Error. Strictly, if
3278 -- we take advantage of 11.6, these checks do not count as side
3279 -- effects. However, we would just as soon consider that they are
3280 -- side effects, since the backend CSE does not work very well on
3281 -- expressions which can raise Constraint_Error. On the other
3282 -- hand, if we do not consider them to be side effect free, then
3283 -- we get some awkward expansions in -gnato mode, resulting in
3284 -- code insertions at a point where we do not have a clear model
3285 -- for performing the insertions. See 4908-002/comment for details.
3287 -- Special handling for entity names
3289 if Is_Entity_Name (N) then
3291 -- If the entity is a constant, it is definitely side effect
3292 -- free. Note that the test of Is_Variable (N) below might
3293 -- be expected to catch this case, but it does not, because
3294 -- this test goes to the original tree, and we may have
3295 -- already rewritten a variable node with a constant as
3296 -- a result of an earlier Force_Evaluation call.
3298 if Ekind (Entity (N)) = E_Constant
3299 or else Ekind (Entity (N)) = E_In_Parameter
3300 then
3301 return True;
3303 -- Functions are not side effect free
3305 elsif Ekind (Entity (N)) = E_Function then
3306 return False;
3308 -- Variables are considered to be a side effect if Variable_Ref
3309 -- is set or if we have a volatile variable and Name_Req is off.
3310 -- If Name_Req is True then we can't help returning a name which
3311 -- effectively allows multiple references in any case.
3313 elsif Is_Variable (N) then
3314 return not Variable_Ref
3315 and then (not Treat_As_Volatile (Entity (N))
3316 or else Name_Req);
3318 -- Any other entity (e.g. a subtype name) is definitely side
3319 -- effect free.
3321 else
3322 return True;
3323 end if;
3325 -- A value known at compile time is always side effect free
3327 elsif Compile_Time_Known_Value (N) then
3328 return True;
3329 end if;
3331 -- For other than entity names and compile time known values,
3332 -- check the node kind for special processing.
3334 case Nkind (N) is
3336 -- An attribute reference is side effect free if its expressions
3337 -- are side effect free and its prefix is side effect free or
3338 -- is an entity reference.
3340 -- Is this right? what about x'first where x is a variable???
3342 when N_Attribute_Reference =>
3343 return Side_Effect_Free (Expressions (N))
3344 and then (Is_Entity_Name (Prefix (N))
3345 or else Side_Effect_Free (Prefix (N)));
3347 -- A binary operator is side effect free if and both operands
3348 -- are side effect free. For this purpose binary operators
3349 -- include membership tests and short circuit forms
3351 when N_Binary_Op |
3352 N_In |
3353 N_Not_In |
3354 N_And_Then |
3355 N_Or_Else =>
3356 return Side_Effect_Free (Left_Opnd (N))
3357 and then Side_Effect_Free (Right_Opnd (N));
3359 -- An explicit dereference is side effect free only if it is
3360 -- a side effect free prefixed reference.
3362 when N_Explicit_Dereference =>
3363 return Safe_Prefixed_Reference (N);
3365 -- A call to _rep_to_pos is side effect free, since we generate
3366 -- this pure function call ourselves. Moreover it is critically
3367 -- important to make this exception, since otherwise we can
3368 -- have discriminants in array components which don't look
3369 -- side effect free in the case of an array whose index type
3370 -- is an enumeration type with an enumeration rep clause.
3372 -- All other function calls are not side effect free
3374 when N_Function_Call =>
3375 return Nkind (Name (N)) = N_Identifier
3376 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
3377 and then
3378 Side_Effect_Free (First (Parameter_Associations (N)));
3380 -- An indexed component is side effect free if it is a side
3381 -- effect free prefixed reference and all the indexing
3382 -- expressions are side effect free.
3384 when N_Indexed_Component =>
3385 return Side_Effect_Free (Expressions (N))
3386 and then Safe_Prefixed_Reference (N);
3388 -- A type qualification is side effect free if the expression
3389 -- is side effect free.
3391 when N_Qualified_Expression =>
3392 return Side_Effect_Free (Expression (N));
3394 -- A selected component is side effect free only if it is a
3395 -- side effect free prefixed reference.
3397 when N_Selected_Component =>
3398 return Safe_Prefixed_Reference (N);
3400 -- A range is side effect free if the bounds are side effect free
3402 when N_Range =>
3403 return Side_Effect_Free (Low_Bound (N))
3404 and then Side_Effect_Free (High_Bound (N));
3406 -- A slice is side effect free if it is a side effect free
3407 -- prefixed reference and the bounds are side effect free.
3409 when N_Slice =>
3410 return Side_Effect_Free (Discrete_Range (N))
3411 and then Safe_Prefixed_Reference (N);
3413 -- A type conversion is side effect free if the expression
3414 -- to be converted is side effect free.
3416 when N_Type_Conversion =>
3417 return Side_Effect_Free (Expression (N));
3419 -- A unary operator is side effect free if the operand
3420 -- is side effect free.
3422 when N_Unary_Op =>
3423 return Side_Effect_Free (Right_Opnd (N));
3425 -- An unchecked type conversion is side effect free only if it
3426 -- is safe and its argument is side effect free.
3428 when N_Unchecked_Type_Conversion =>
3429 return Safe_Unchecked_Type_Conversion (N)
3430 and then Side_Effect_Free (Expression (N));
3432 -- An unchecked expression is side effect free if its expression
3433 -- is side effect free.
3435 when N_Unchecked_Expression =>
3436 return Side_Effect_Free (Expression (N));
3438 -- A literal is side effect free
3440 when N_Character_Literal |
3441 N_Integer_Literal |
3442 N_Real_Literal |
3443 N_String_Literal =>
3444 return True;
3446 -- We consider that anything else has side effects. This is a bit
3447 -- crude, but we are pretty close for most common cases, and we
3448 -- are certainly correct (i.e. we never return True when the
3449 -- answer should be False).
3451 when others =>
3452 return False;
3453 end case;
3454 end Side_Effect_Free;
3456 -- A list is side effect free if all elements of the list are
3457 -- side effect free.
3459 function Side_Effect_Free (L : List_Id) return Boolean is
3460 N : Node_Id;
3462 begin
3463 if L = No_List or else L = Error_List then
3464 return True;
3466 else
3467 N := First (L);
3468 while Present (N) loop
3469 if not Side_Effect_Free (N) then
3470 return False;
3471 else
3472 Next (N);
3473 end if;
3474 end loop;
3476 return True;
3477 end if;
3478 end Side_Effect_Free;
3480 -------------------------
3481 -- Within_In_Parameter --
3482 -------------------------
3484 function Within_In_Parameter (N : Node_Id) return Boolean is
3485 begin
3486 if not Comes_From_Source (N) then
3487 return False;
3489 elsif Is_Entity_Name (N) then
3490 return
3491 Ekind (Entity (N)) = E_In_Parameter;
3493 elsif Nkind (N) = N_Indexed_Component
3494 or else Nkind (N) = N_Selected_Component
3495 then
3496 return Within_In_Parameter (Prefix (N));
3497 else
3499 return False;
3500 end if;
3501 end Within_In_Parameter;
3503 -- Start of processing for Remove_Side_Effects
3505 begin
3506 -- If we are side effect free already or expansion is disabled,
3507 -- there is nothing to do.
3509 if Side_Effect_Free (Exp) or else not Expander_Active then
3510 return;
3511 end if;
3513 -- All this must not have any checks
3515 Scope_Suppress := (others => True);
3517 -- If the expression has the form v.all then we can just capture
3518 -- the pointer, and then do an explicit dereference on the result.
3520 if Nkind (Exp) = N_Explicit_Dereference then
3521 Def_Id :=
3522 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3523 Res :=
3524 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
3526 Insert_Action (Exp,
3527 Make_Object_Declaration (Loc,
3528 Defining_Identifier => Def_Id,
3529 Object_Definition =>
3530 New_Reference_To (Etype (Prefix (Exp)), Loc),
3531 Constant_Present => True,
3532 Expression => Relocate_Node (Prefix (Exp))));
3534 -- Similar processing for an unchecked conversion of an expression
3535 -- of the form v.all, where we want the same kind of treatment.
3537 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
3538 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
3539 then
3540 Remove_Side_Effects (Expression (Exp), Variable_Ref);
3541 Scope_Suppress := Svg_Suppress;
3542 return;
3544 -- If this is a type conversion, leave the type conversion and remove
3545 -- the side effects in the expression. This is important in several
3546 -- circumstances: for change of representations, and also when this
3547 -- is a view conversion to a smaller object, where gigi can end up
3548 -- its own temporary of the wrong size.
3550 -- ??? this transformation is inhibited for elementary types that are
3551 -- not involved in a change of representation because it causes
3552 -- regressions that are not fully understood yet.
3554 elsif Nkind (Exp) = N_Type_Conversion
3555 and then (not Is_Elementary_Type (Underlying_Type (Exp_Type))
3556 or else Nkind (Parent (Exp)) = N_Assignment_Statement)
3557 then
3558 Remove_Side_Effects (Expression (Exp), Variable_Ref);
3559 Scope_Suppress := Svg_Suppress;
3560 return;
3562 -- For expressions that denote objects, we can use a renaming scheme.
3563 -- We skip using this if we have a volatile variable and we do not
3564 -- have Nam_Req set true (see comments above for Side_Effect_Free).
3565 -- We also skip this scheme for class-wide expressions in order to
3566 -- avoid recursive expansion (see Expand_N_Object_Renaming_Declaration)
3567 -- If the object is a function call, we need to create a temporary and
3568 -- not a renaming.
3570 -- Note that we could use ordinary object declarations in the case of
3571 -- expressions not appearing as lvalues. That is left as a possible
3572 -- optimization in the future but we prefer to generate renamings
3573 -- right now, since we may indeed be transforming an lvalue.
3575 elsif Is_Object_Reference (Exp)
3576 and then Nkind (Exp) /= N_Function_Call
3577 and then not Variable_Ref
3578 and then (Name_Req
3579 or else not Is_Entity_Name (Exp)
3580 or else not Treat_As_Volatile (Entity (Exp)))
3581 and then not Is_Class_Wide_Type (Exp_Type)
3582 then
3583 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3585 if Nkind (Exp) = N_Selected_Component
3586 and then Nkind (Prefix (Exp)) = N_Function_Call
3587 and then Is_Array_Type (Etype (Exp))
3588 then
3589 -- Avoid generating a variable-sized temporary, by generating
3590 -- the renaming declaration just for the function call. The
3591 -- transformation could be refined to apply only when the array
3592 -- component is constrained by a discriminant???
3594 Res :=
3595 Make_Selected_Component (Loc,
3596 Prefix => New_Occurrence_Of (Def_Id, Loc),
3597 Selector_Name => Selector_Name (Exp));
3599 Insert_Action (Exp,
3600 Make_Object_Renaming_Declaration (Loc,
3601 Defining_Identifier => Def_Id,
3602 Subtype_Mark =>
3603 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
3604 Name => Relocate_Node (Prefix (Exp))));
3606 -- The temporary must be elaborated by gigi, and is of course
3607 -- not to be replaced in-line by the expression it renames,
3608 -- which would defeat the purpose of removing the side-effect.
3610 Set_Is_Renaming_Of_Object (Def_Id, False);
3612 else
3613 Res := New_Reference_To (Def_Id, Loc);
3615 Insert_Action (Exp,
3616 Make_Object_Renaming_Declaration (Loc,
3617 Defining_Identifier => Def_Id,
3618 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
3619 Name => Relocate_Node (Exp)));
3621 Set_Is_Renaming_Of_Object (Def_Id, False);
3622 end if;
3624 -- If it is a scalar type, just make a copy
3626 elsif Is_Elementary_Type (Exp_Type) then
3627 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3628 Set_Etype (Def_Id, Exp_Type);
3629 Res := New_Reference_To (Def_Id, Loc);
3631 E :=
3632 Make_Object_Declaration (Loc,
3633 Defining_Identifier => Def_Id,
3634 Object_Definition => New_Reference_To (Exp_Type, Loc),
3635 Constant_Present => True,
3636 Expression => Relocate_Node (Exp));
3638 Set_Assignment_OK (E);
3639 Insert_Action (Exp, E);
3641 -- Always use a renaming for an unchecked conversion
3642 -- If this is an unchecked conversion that Gigi can't handle, make
3643 -- a copy or a use a renaming to capture the value.
3645 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
3646 and then not Safe_Unchecked_Type_Conversion (Exp)
3647 then
3648 if Controlled_Type (Etype (Exp)) then
3650 -- Use a renaming to capture the expression, rather than create
3651 -- a controlled temporary.
3653 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3654 Res := New_Reference_To (Def_Id, Loc);
3656 Insert_Action (Exp,
3657 Make_Object_Renaming_Declaration (Loc,
3658 Defining_Identifier => Def_Id,
3659 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
3660 Name => Relocate_Node (Exp)));
3662 else
3663 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3664 Set_Etype (Def_Id, Exp_Type);
3665 Res := New_Reference_To (Def_Id, Loc);
3667 E :=
3668 Make_Object_Declaration (Loc,
3669 Defining_Identifier => Def_Id,
3670 Object_Definition => New_Reference_To (Exp_Type, Loc),
3671 Constant_Present => not Is_Variable (Exp),
3672 Expression => Relocate_Node (Exp));
3674 Set_Assignment_OK (E);
3675 Insert_Action (Exp, E);
3676 end if;
3678 -- Otherwise we generate a reference to the value
3680 else
3681 Ref_Type := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3683 Ptr_Typ_Decl :=
3684 Make_Full_Type_Declaration (Loc,
3685 Defining_Identifier => Ref_Type,
3686 Type_Definition =>
3687 Make_Access_To_Object_Definition (Loc,
3688 All_Present => True,
3689 Subtype_Indication =>
3690 New_Reference_To (Exp_Type, Loc)));
3692 E := Exp;
3693 Insert_Action (Exp, Ptr_Typ_Decl);
3695 Def_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3696 Set_Etype (Def_Id, Exp_Type);
3698 Res :=
3699 Make_Explicit_Dereference (Loc,
3700 Prefix => New_Reference_To (Def_Id, Loc));
3702 if Nkind (E) = N_Explicit_Dereference then
3703 New_Exp := Relocate_Node (Prefix (E));
3704 else
3705 E := Relocate_Node (E);
3706 New_Exp := Make_Reference (Loc, E);
3707 end if;
3709 if Nkind (E) = N_Aggregate and then Expansion_Delayed (E) then
3710 Set_Expansion_Delayed (E, False);
3711 Set_Analyzed (E, False);
3712 end if;
3714 Insert_Action (Exp,
3715 Make_Object_Declaration (Loc,
3716 Defining_Identifier => Def_Id,
3717 Object_Definition => New_Reference_To (Ref_Type, Loc),
3718 Expression => New_Exp));
3719 end if;
3721 -- Preserve the Assignment_OK flag in all copies, since at least
3722 -- one copy may be used in a context where this flag must be set
3723 -- (otherwise why would the flag be set in the first place).
3725 Set_Assignment_OK (Res, Assignment_OK (Exp));
3727 -- Finally rewrite the original expression and we are done
3729 Rewrite (Exp, Res);
3730 Analyze_And_Resolve (Exp, Exp_Type);
3731 Scope_Suppress := Svg_Suppress;
3732 end Remove_Side_Effects;
3734 ------------------------------------
3735 -- Safe_Unchecked_Type_Conversion --
3736 ------------------------------------
3738 -- Note: this function knows quite a bit about the exact requirements
3739 -- of Gigi with respect to unchecked type conversions, and its code
3740 -- must be coordinated with any changes in Gigi in this area.
3742 -- The above requirements should be documented in Sinfo ???
3744 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
3745 Otyp : Entity_Id;
3746 Ityp : Entity_Id;
3747 Oalign : Uint;
3748 Ialign : Uint;
3749 Pexp : constant Node_Id := Parent (Exp);
3751 begin
3752 -- If the expression is the RHS of an assignment or object declaration
3753 -- we are always OK because there will always be a target.
3755 -- Object renaming declarations, (generated for view conversions of
3756 -- actuals in inlined calls), like object declarations, provide an
3757 -- explicit type, and are safe as well.
3759 if (Nkind (Pexp) = N_Assignment_Statement
3760 and then Expression (Pexp) = Exp)
3761 or else Nkind (Pexp) = N_Object_Declaration
3762 or else Nkind (Pexp) = N_Object_Renaming_Declaration
3763 then
3764 return True;
3766 -- If the expression is the prefix of an N_Selected_Component
3767 -- we should also be OK because GCC knows to look inside the
3768 -- conversion except if the type is discriminated. We assume
3769 -- that we are OK anyway if the type is not set yet or if it is
3770 -- controlled since we can't afford to introduce a temporary in
3771 -- this case.
3773 elsif Nkind (Pexp) = N_Selected_Component
3774 and then Prefix (Pexp) = Exp
3775 then
3776 if No (Etype (Pexp)) then
3777 return True;
3778 else
3779 return
3780 not Has_Discriminants (Etype (Pexp))
3781 or else Is_Constrained (Etype (Pexp));
3782 end if;
3783 end if;
3785 -- Set the output type, this comes from Etype if it is set, otherwise
3786 -- we take it from the subtype mark, which we assume was already
3787 -- fully analyzed.
3789 if Present (Etype (Exp)) then
3790 Otyp := Etype (Exp);
3791 else
3792 Otyp := Entity (Subtype_Mark (Exp));
3793 end if;
3795 -- The input type always comes from the expression, and we assume
3796 -- this is indeed always analyzed, so we can simply get the Etype.
3798 Ityp := Etype (Expression (Exp));
3800 -- Initialize alignments to unknown so far
3802 Oalign := No_Uint;
3803 Ialign := No_Uint;
3805 -- Replace a concurrent type by its corresponding record type
3806 -- and each type by its underlying type and do the tests on those.
3807 -- The original type may be a private type whose completion is a
3808 -- concurrent type, so find the underlying type first.
3810 if Present (Underlying_Type (Otyp)) then
3811 Otyp := Underlying_Type (Otyp);
3812 end if;
3814 if Present (Underlying_Type (Ityp)) then
3815 Ityp := Underlying_Type (Ityp);
3816 end if;
3818 if Is_Concurrent_Type (Otyp) then
3819 Otyp := Corresponding_Record_Type (Otyp);
3820 end if;
3822 if Is_Concurrent_Type (Ityp) then
3823 Ityp := Corresponding_Record_Type (Ityp);
3824 end if;
3826 -- If the base types are the same, we know there is no problem since
3827 -- this conversion will be a noop.
3829 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
3830 return True;
3832 -- Same if this is an upwards conversion of an untagged type, and there
3833 -- are no constraints involved (could be more general???)
3835 elsif Etype (Ityp) = Otyp
3836 and then not Is_Tagged_Type (Ityp)
3837 and then not Has_Discriminants (Ityp)
3838 and then No (First_Rep_Item (Base_Type (Ityp)))
3839 then
3840 return True;
3842 -- If the size of output type is known at compile time, there is
3843 -- never a problem. Note that unconstrained records are considered
3844 -- to be of known size, but we can't consider them that way here,
3845 -- because we are talking about the actual size of the object.
3847 -- We also make sure that in addition to the size being known, we do
3848 -- not have a case which might generate an embarrassingly large temp
3849 -- in stack checking mode.
3851 elsif Size_Known_At_Compile_Time (Otyp)
3852 and then
3853 (not Stack_Checking_Enabled
3854 or else not May_Generate_Large_Temp (Otyp))
3855 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
3856 then
3857 return True;
3859 -- If either type is tagged, then we know the alignment is OK so
3860 -- Gigi will be able to use pointer punning.
3862 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
3863 return True;
3865 -- If either type is a limited record type, we cannot do a copy, so
3866 -- say safe since there's nothing else we can do.
3868 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
3869 return True;
3871 -- Conversions to and from packed array types are always ignored and
3872 -- hence are safe.
3874 elsif Is_Packed_Array_Type (Otyp)
3875 or else Is_Packed_Array_Type (Ityp)
3876 then
3877 return True;
3878 end if;
3880 -- The only other cases known to be safe is if the input type's
3881 -- alignment is known to be at least the maximum alignment for the
3882 -- target or if both alignments are known and the output type's
3883 -- alignment is no stricter than the input's. We can use the alignment
3884 -- of the component type of an array if a type is an unpacked
3885 -- array type.
3887 if Present (Alignment_Clause (Otyp)) then
3888 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
3890 elsif Is_Array_Type (Otyp)
3891 and then Present (Alignment_Clause (Component_Type (Otyp)))
3892 then
3893 Oalign := Expr_Value (Expression (Alignment_Clause
3894 (Component_Type (Otyp))));
3895 end if;
3897 if Present (Alignment_Clause (Ityp)) then
3898 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
3900 elsif Is_Array_Type (Ityp)
3901 and then Present (Alignment_Clause (Component_Type (Ityp)))
3902 then
3903 Ialign := Expr_Value (Expression (Alignment_Clause
3904 (Component_Type (Ityp))));
3905 end if;
3907 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
3908 return True;
3910 elsif Ialign /= No_Uint and then Oalign /= No_Uint
3911 and then Ialign <= Oalign
3912 then
3913 return True;
3915 -- Otherwise, Gigi cannot handle this and we must make a temporary
3917 else
3918 return False;
3919 end if;
3921 end Safe_Unchecked_Type_Conversion;
3923 --------------------------
3924 -- Set_Elaboration_Flag --
3925 --------------------------
3927 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
3928 Loc : constant Source_Ptr := Sloc (N);
3929 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
3930 Asn : Node_Id;
3932 begin
3933 if Present (Ent) then
3935 -- Nothing to do if at the compilation unit level, because in this
3936 -- case the flag is set by the binder generated elaboration routine.
3938 if Nkind (Parent (N)) = N_Compilation_Unit then
3939 null;
3941 -- Here we do need to generate an assignment statement
3943 else
3944 Check_Restriction (No_Elaboration_Code, N);
3945 Asn :=
3946 Make_Assignment_Statement (Loc,
3947 Name => New_Occurrence_Of (Ent, Loc),
3948 Expression => New_Occurrence_Of (Standard_True, Loc));
3950 if Nkind (Parent (N)) = N_Subunit then
3951 Insert_After (Corresponding_Stub (Parent (N)), Asn);
3952 else
3953 Insert_After (N, Asn);
3954 end if;
3956 Analyze (Asn);
3958 -- Kill current value indication. This is necessary because
3959 -- the tests of this flag are inserted out of sequence and must
3960 -- not pick up bogus indications of the wrong constant value.
3962 Set_Current_Value (Ent, Empty);
3963 end if;
3964 end if;
3965 end Set_Elaboration_Flag;
3967 --------------------------
3968 -- Target_Has_Fixed_Ops --
3969 --------------------------
3971 Integer_Sized_Small : Ureal;
3972 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this
3973 -- function is called (we don't want to compute it more than once!)
3975 Long_Integer_Sized_Small : Ureal;
3976 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
3977 -- functoin is called (we don't want to compute it more than once)
3979 First_Time_For_THFO : Boolean := True;
3980 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
3982 function Target_Has_Fixed_Ops
3983 (Left_Typ : Entity_Id;
3984 Right_Typ : Entity_Id;
3985 Result_Typ : Entity_Id) return Boolean
3987 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
3988 -- Return True if the given type is a fixed-point type with a small
3989 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
3990 -- an absolute value less than 1.0. This is currently limited
3991 -- to fixed-point types that map to Integer or Long_Integer.
3993 ------------------------
3994 -- Is_Fractional_Type --
3995 ------------------------
3997 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
3998 begin
3999 if Esize (Typ) = Standard_Integer_Size then
4000 return Small_Value (Typ) = Integer_Sized_Small;
4002 elsif Esize (Typ) = Standard_Long_Integer_Size then
4003 return Small_Value (Typ) = Long_Integer_Sized_Small;
4005 else
4006 return False;
4007 end if;
4008 end Is_Fractional_Type;
4010 -- Start of processing for Target_Has_Fixed_Ops
4012 begin
4013 -- Return False if Fractional_Fixed_Ops_On_Target is false
4015 if not Fractional_Fixed_Ops_On_Target then
4016 return False;
4017 end if;
4019 -- Here the target has Fractional_Fixed_Ops, if first time, compute
4020 -- standard constants used by Is_Fractional_Type.
4022 if First_Time_For_THFO then
4023 First_Time_For_THFO := False;
4025 Integer_Sized_Small :=
4026 UR_From_Components
4027 (Num => Uint_1,
4028 Den => UI_From_Int (Standard_Integer_Size - 1),
4029 Rbase => 2);
4031 Long_Integer_Sized_Small :=
4032 UR_From_Components
4033 (Num => Uint_1,
4034 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
4035 Rbase => 2);
4036 end if;
4038 -- Return True if target supports fixed-by-fixed multiply/divide
4039 -- for fractional fixed-point types (see Is_Fractional_Type) and
4040 -- the operand and result types are equivalent fractional types.
4042 return Is_Fractional_Type (Base_Type (Left_Typ))
4043 and then Is_Fractional_Type (Base_Type (Right_Typ))
4044 and then Is_Fractional_Type (Base_Type (Result_Typ))
4045 and then Esize (Left_Typ) = Esize (Right_Typ)
4046 and then Esize (Left_Typ) = Esize (Result_Typ);
4047 end Target_Has_Fixed_Ops;
4049 ------------------------------------------
4050 -- Type_May_Have_Bit_Aligned_Components --
4051 ------------------------------------------
4053 function Type_May_Have_Bit_Aligned_Components
4054 (Typ : Entity_Id) return Boolean
4056 begin
4057 -- Array type, check component type
4059 if Is_Array_Type (Typ) then
4060 return
4061 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
4063 -- Record type, check components
4065 elsif Is_Record_Type (Typ) then
4066 declare
4067 E : Entity_Id;
4069 begin
4070 E := First_Entity (Typ);
4071 while Present (E) loop
4072 if Ekind (E) = E_Component
4073 or else Ekind (E) = E_Discriminant
4074 then
4075 if Component_May_Be_Bit_Aligned (E)
4076 or else
4077 Type_May_Have_Bit_Aligned_Components (Etype (E))
4078 then
4079 return True;
4080 end if;
4081 end if;
4083 Next_Entity (E);
4084 end loop;
4086 return False;
4087 end;
4089 -- Type other than array or record is always OK
4091 else
4092 return False;
4093 end if;
4094 end Type_May_Have_Bit_Aligned_Components;
4096 ----------------------------
4097 -- Wrap_Cleanup_Procedure --
4098 ----------------------------
4100 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
4101 Loc : constant Source_Ptr := Sloc (N);
4102 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
4103 Stmts : constant List_Id := Statements (Stseq);
4105 begin
4106 if Abort_Allowed then
4107 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
4108 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
4109 end if;
4110 end Wrap_Cleanup_Procedure;
4112 end Exp_Util;