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