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