]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_util.adb
[Ada] Debug info not available on subp having a class-wide precondition
[thirdparty/gcc.git] / gcc / ada / sem_util.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ U T I L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Treepr; -- ???For debugging code below
27
28 with Aspects; use Aspects;
29 with Casing; use Casing;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Erroutc; use Erroutc;
35 with Exp_Ch11; use Exp_Ch11;
36 with Exp_Util; use Exp_Util;
37 with Fname; use Fname;
38 with Freeze; use Freeze;
39 with Lib; use Lib;
40 with Lib.Xref; use Lib.Xref;
41 with Namet.Sp; use Namet.Sp;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Attr; use Sem_Attr;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Elab; use Sem_Elab;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Prag; use Sem_Prag;
57 with Sem_Res; use Sem_Res;
58 with Sem_Warn; use Sem_Warn;
59 with Sem_Type; use Sem_Type;
60 with Sinfo; use Sinfo;
61 with Sinput; use Sinput;
62 with Stand; use Stand;
63 with Style;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uname; use Uname;
69
70 with GNAT.Heap_Sort_G;
71 with GNAT.HTable; use GNAT.HTable;
72
73 package body Sem_Util is
74
75 ---------------------------
76 -- Local Data Structures --
77 ---------------------------
78
79 Invalid_Binder_Values : array (Scalar_Id) of Entity_Id := (others => Empty);
80 -- A collection to hold the entities of the variables declared in package
81 -- System.Scalar_Values which describe the invalid values of scalar types.
82
83 Invalid_Binder_Values_Set : Boolean := False;
84 -- This flag prevents multiple attempts to initialize Invalid_Binder_Values
85
86 Invalid_Floats : array (Float_Scalar_Id) of Ureal := (others => No_Ureal);
87 -- A collection to hold the invalid values of float types as specified by
88 -- pragma Initialize_Scalars.
89
90 Invalid_Integers : array (Integer_Scalar_Id) of Uint := (others => No_Uint);
91 -- A collection to hold the invalid values of integer types as specified
92 -- by pragma Initialize_Scalars.
93
94 -----------------------
95 -- Local Subprograms --
96 -----------------------
97
98 function Build_Component_Subtype
99 (C : List_Id;
100 Loc : Source_Ptr;
101 T : Entity_Id) return Node_Id;
102 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
103 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
104 -- Loc is the source location, T is the original subtype.
105
106 procedure Examine_Array_Bounds
107 (Typ : Entity_Id;
108 All_Static : out Boolean;
109 Has_Empty : out Boolean);
110 -- Inspect the index constraints of array type Typ. Flag All_Static is set
111 -- when all ranges are static. Flag Has_Empty is set only when All_Static
112 -- is set and indicates that at least one range is empty.
113
114 function Has_Enabled_Property
115 (Item_Id : Entity_Id;
116 Property : Name_Id) return Boolean;
117 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
118 -- Determine whether an abstract state or a variable denoted by entity
119 -- Item_Id has enabled property Property.
120
121 function Has_Null_Extension (T : Entity_Id) return Boolean;
122 -- T is a derived tagged type. Check whether the type extension is null.
123 -- If the parent type is fully initialized, T can be treated as such.
124
125 function Is_Atomic_Object_Entity (Id : Entity_Id) return Boolean;
126 -- Determine whether arbitrary entity Id denotes an atomic object as per
127 -- RM C.6(7).
128
129 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
130 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
131 -- with discriminants whose default values are static, examine only the
132 -- components in the selected variant to determine whether all of them
133 -- have a default.
134
135 type Null_Status_Kind is
136 (Is_Null,
137 -- This value indicates that a subexpression is known to have a null
138 -- value at compile time.
139
140 Is_Non_Null,
141 -- This value indicates that a subexpression is known to have a non-null
142 -- value at compile time.
143
144 Unknown);
145 -- This value indicates that it cannot be determined at compile time
146 -- whether a subexpression yields a null or non-null value.
147
148 function Null_Status (N : Node_Id) return Null_Status_Kind;
149 -- Determine whether subexpression N of an access type yields a null value,
150 -- a non-null value, or the value cannot be determined at compile time. The
151 -- routine does not take simple flow diagnostics into account, it relies on
152 -- static facts such as the presence of null exclusions.
153
154 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
155 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
156 -- ???We retain the old and new algorithms for Requires_Transient_Scope for
157 -- the time being. New_Requires_Transient_Scope is used by default; the
158 -- debug switch -gnatdQ can be used to do Old_Requires_Transient_Scope
159 -- instead. The intent is to use this temporarily to measure before/after
160 -- efficiency. Note: when this temporary code is removed, the documentation
161 -- of dQ in debug.adb should be removed.
162
163 procedure Results_Differ
164 (Id : Entity_Id;
165 Old_Val : Boolean;
166 New_Val : Boolean);
167 -- ???Debugging code. Called when the Old_Val and New_Val differ. This
168 -- routine will be removed eventially when New_Requires_Transient_Scope
169 -- becomes Requires_Transient_Scope and Old_Requires_Transient_Scope is
170 -- eliminated.
171
172 function Subprogram_Name (N : Node_Id) return String;
173 -- Return the fully qualified name of the enclosing subprogram for the
174 -- given node N, with file:line:col information appended, e.g.
175 -- "subp:file:line:col", corresponding to the source location of the
176 -- body of the subprogram.
177
178 ------------------------------
179 -- Abstract_Interface_List --
180 ------------------------------
181
182 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
183 Nod : Node_Id;
184
185 begin
186 if Is_Concurrent_Type (Typ) then
187
188 -- If we are dealing with a synchronized subtype, go to the base
189 -- type, whose declaration has the interface list.
190
191 Nod := Declaration_Node (Base_Type (Typ));
192
193 if Nkind_In (Nod, N_Full_Type_Declaration,
194 N_Private_Type_Declaration)
195 then
196 return Empty_List;
197 end if;
198
199 elsif Ekind (Typ) = E_Record_Type_With_Private then
200 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
201 Nod := Type_Definition (Parent (Typ));
202
203 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
204 if Present (Full_View (Typ))
205 and then
206 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
207 then
208 Nod := Type_Definition (Parent (Full_View (Typ)));
209
210 -- If the full-view is not available we cannot do anything else
211 -- here (the source has errors).
212
213 else
214 return Empty_List;
215 end if;
216
217 -- Support for generic formals with interfaces is still missing ???
218
219 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
220 return Empty_List;
221
222 else
223 pragma Assert
224 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
225 Nod := Parent (Typ);
226 end if;
227
228 elsif Ekind (Typ) = E_Record_Subtype then
229 Nod := Type_Definition (Parent (Etype (Typ)));
230
231 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
232
233 -- Recurse, because parent may still be a private extension. Also
234 -- note that the full view of the subtype or the full view of its
235 -- base type may (both) be unavailable.
236
237 return Abstract_Interface_List (Etype (Typ));
238
239 elsif Ekind (Typ) = E_Record_Type then
240 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
241 Nod := Formal_Type_Definition (Parent (Typ));
242 else
243 Nod := Type_Definition (Parent (Typ));
244 end if;
245
246 -- Otherwise the type is of a kind which does not implement interfaces
247
248 else
249 return Empty_List;
250 end if;
251
252 return Interface_List (Nod);
253 end Abstract_Interface_List;
254
255 ----------------------------------
256 -- Acquire_Warning_Match_String --
257 ----------------------------------
258
259 function Acquire_Warning_Match_String (Str_Lit : Node_Id) return String is
260 S : constant String := To_String (Strval (Str_Lit));
261 begin
262 if S = "" then
263 return "";
264 else
265 -- Put "*" before or after or both, if it's not already there
266
267 declare
268 F : constant Boolean := S (S'First) = '*';
269 L : constant Boolean := S (S'Last) = '*';
270 begin
271 if F then
272 if L then
273 return S;
274 else
275 return S & "*";
276 end if;
277 else
278 if L then
279 return "*" & S;
280 else
281 return "*" & S & "*";
282 end if;
283 end if;
284 end;
285 end if;
286 end Acquire_Warning_Match_String;
287
288 --------------------------------
289 -- Add_Access_Type_To_Process --
290 --------------------------------
291
292 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
293 L : Elist_Id;
294
295 begin
296 Ensure_Freeze_Node (E);
297 L := Access_Types_To_Process (Freeze_Node (E));
298
299 if No (L) then
300 L := New_Elmt_List;
301 Set_Access_Types_To_Process (Freeze_Node (E), L);
302 end if;
303
304 Append_Elmt (A, L);
305 end Add_Access_Type_To_Process;
306
307 --------------------------
308 -- Add_Block_Identifier --
309 --------------------------
310
311 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
312 Loc : constant Source_Ptr := Sloc (N);
313
314 begin
315 pragma Assert (Nkind (N) = N_Block_Statement);
316
317 -- The block already has a label, return its entity
318
319 if Present (Identifier (N)) then
320 Id := Entity (Identifier (N));
321
322 -- Create a new block label and set its attributes
323
324 else
325 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
326 Set_Etype (Id, Standard_Void_Type);
327 Set_Parent (Id, N);
328
329 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
330 Set_Block_Node (Id, Identifier (N));
331 end if;
332 end Add_Block_Identifier;
333
334 ----------------------------
335 -- Add_Global_Declaration --
336 ----------------------------
337
338 procedure Add_Global_Declaration (N : Node_Id) is
339 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
340
341 begin
342 if No (Declarations (Aux_Node)) then
343 Set_Declarations (Aux_Node, New_List);
344 end if;
345
346 Append_To (Declarations (Aux_Node), N);
347 Analyze (N);
348 end Add_Global_Declaration;
349
350 --------------------------------
351 -- Address_Integer_Convert_OK --
352 --------------------------------
353
354 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
355 begin
356 if Allow_Integer_Address
357 and then ((Is_Descendant_Of_Address (T1)
358 and then Is_Private_Type (T1)
359 and then Is_Integer_Type (T2))
360 or else
361 (Is_Descendant_Of_Address (T2)
362 and then Is_Private_Type (T2)
363 and then Is_Integer_Type (T1)))
364 then
365 return True;
366 else
367 return False;
368 end if;
369 end Address_Integer_Convert_OK;
370
371 -------------------
372 -- Address_Value --
373 -------------------
374
375 function Address_Value (N : Node_Id) return Node_Id is
376 Expr : Node_Id := N;
377
378 begin
379 loop
380 -- For constant, get constant expression
381
382 if Is_Entity_Name (Expr)
383 and then Ekind (Entity (Expr)) = E_Constant
384 then
385 Expr := Constant_Value (Entity (Expr));
386
387 -- For unchecked conversion, get result to convert
388
389 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
390 Expr := Expression (Expr);
391
392 -- For (common case) of To_Address call, get argument
393
394 elsif Nkind (Expr) = N_Function_Call
395 and then Is_Entity_Name (Name (Expr))
396 and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
397 then
398 Expr := First (Parameter_Associations (Expr));
399
400 if Nkind (Expr) = N_Parameter_Association then
401 Expr := Explicit_Actual_Parameter (Expr);
402 end if;
403
404 -- We finally have the real expression
405
406 else
407 exit;
408 end if;
409 end loop;
410
411 return Expr;
412 end Address_Value;
413
414 -----------------
415 -- Addressable --
416 -----------------
417
418 -- For now, just 8/16/32/64
419
420 function Addressable (V : Uint) return Boolean is
421 begin
422 return V = Uint_8 or else
423 V = Uint_16 or else
424 V = Uint_32 or else
425 V = Uint_64;
426 end Addressable;
427
428 function Addressable (V : Int) return Boolean is
429 begin
430 return V = 8 or else
431 V = 16 or else
432 V = 32 or else
433 V = 64;
434 end Addressable;
435
436 ---------------------------------
437 -- Aggregate_Constraint_Checks --
438 ---------------------------------
439
440 procedure Aggregate_Constraint_Checks
441 (Exp : Node_Id;
442 Check_Typ : Entity_Id)
443 is
444 Exp_Typ : constant Entity_Id := Etype (Exp);
445
446 begin
447 if Raises_Constraint_Error (Exp) then
448 return;
449 end if;
450
451 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
452 -- component's type to force the appropriate accessibility checks.
453
454 -- Ada 2005 (AI-231): Generate conversion to the null-excluding type to
455 -- force the corresponding run-time check
456
457 if Is_Access_Type (Check_Typ)
458 and then Is_Local_Anonymous_Access (Check_Typ)
459 then
460 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
461 Analyze_And_Resolve (Exp, Check_Typ);
462 Check_Unset_Reference (Exp);
463 end if;
464
465 -- What follows is really expansion activity, so check that expansion
466 -- is on and is allowed. In GNATprove mode, we also want check flags to
467 -- be added in the tree, so that the formal verification can rely on
468 -- those to be present. In GNATprove mode for formal verification, some
469 -- treatment typically only done during expansion needs to be performed
470 -- on the tree, but it should not be applied inside generics. Otherwise,
471 -- this breaks the name resolution mechanism for generic instances.
472
473 if not Expander_Active
474 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
475 then
476 return;
477 end if;
478
479 if Is_Access_Type (Check_Typ)
480 and then Can_Never_Be_Null (Check_Typ)
481 and then not Can_Never_Be_Null (Exp_Typ)
482 then
483 Install_Null_Excluding_Check (Exp);
484 end if;
485
486 -- First check if we have to insert discriminant checks
487
488 if Has_Discriminants (Exp_Typ) then
489 Apply_Discriminant_Check (Exp, Check_Typ);
490
491 -- Next emit length checks for array aggregates
492
493 elsif Is_Array_Type (Exp_Typ) then
494 Apply_Length_Check (Exp, Check_Typ);
495
496 -- Finally emit scalar and string checks. If we are dealing with a
497 -- scalar literal we need to check by hand because the Etype of
498 -- literals is not necessarily correct.
499
500 elsif Is_Scalar_Type (Exp_Typ)
501 and then Compile_Time_Known_Value (Exp)
502 then
503 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
504 Apply_Compile_Time_Constraint_Error
505 (Exp, "value not in range of}??", CE_Range_Check_Failed,
506 Ent => Base_Type (Check_Typ),
507 Typ => Base_Type (Check_Typ));
508
509 elsif Is_Out_Of_Range (Exp, Check_Typ) then
510 Apply_Compile_Time_Constraint_Error
511 (Exp, "value not in range of}??", CE_Range_Check_Failed,
512 Ent => Check_Typ,
513 Typ => Check_Typ);
514
515 elsif not Range_Checks_Suppressed (Check_Typ) then
516 Apply_Scalar_Range_Check (Exp, Check_Typ);
517 end if;
518
519 -- Verify that target type is also scalar, to prevent view anomalies
520 -- in instantiations.
521
522 elsif (Is_Scalar_Type (Exp_Typ)
523 or else Nkind (Exp) = N_String_Literal)
524 and then Is_Scalar_Type (Check_Typ)
525 and then Exp_Typ /= Check_Typ
526 then
527 if Is_Entity_Name (Exp)
528 and then Ekind (Entity (Exp)) = E_Constant
529 then
530 -- If expression is a constant, it is worthwhile checking whether
531 -- it is a bound of the type.
532
533 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
534 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
535 or else
536 (Is_Entity_Name (Type_High_Bound (Check_Typ))
537 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
538 then
539 return;
540
541 else
542 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
543 Analyze_And_Resolve (Exp, Check_Typ);
544 Check_Unset_Reference (Exp);
545 end if;
546
547 -- Could use a comment on this case ???
548
549 else
550 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
551 Analyze_And_Resolve (Exp, Check_Typ);
552 Check_Unset_Reference (Exp);
553 end if;
554
555 end if;
556 end Aggregate_Constraint_Checks;
557
558 -----------------------
559 -- Alignment_In_Bits --
560 -----------------------
561
562 function Alignment_In_Bits (E : Entity_Id) return Uint is
563 begin
564 return Alignment (E) * System_Storage_Unit;
565 end Alignment_In_Bits;
566
567 --------------------------------------
568 -- All_Composite_Constraints_Static --
569 --------------------------------------
570
571 function All_Composite_Constraints_Static
572 (Constr : Node_Id) return Boolean
573 is
574 begin
575 if No (Constr) or else Error_Posted (Constr) then
576 return True;
577 end if;
578
579 case Nkind (Constr) is
580 when N_Subexpr =>
581 if Nkind (Constr) in N_Has_Entity
582 and then Present (Entity (Constr))
583 then
584 if Is_Type (Entity (Constr)) then
585 return
586 not Is_Discrete_Type (Entity (Constr))
587 or else Is_OK_Static_Subtype (Entity (Constr));
588 end if;
589
590 elsif Nkind (Constr) = N_Range then
591 return
592 Is_OK_Static_Expression (Low_Bound (Constr))
593 and then
594 Is_OK_Static_Expression (High_Bound (Constr));
595
596 elsif Nkind (Constr) = N_Attribute_Reference
597 and then Attribute_Name (Constr) = Name_Range
598 then
599 return
600 Is_OK_Static_Expression
601 (Type_Low_Bound (Etype (Prefix (Constr))))
602 and then
603 Is_OK_Static_Expression
604 (Type_High_Bound (Etype (Prefix (Constr))));
605 end if;
606
607 return
608 not Present (Etype (Constr)) -- previous error
609 or else not Is_Discrete_Type (Etype (Constr))
610 or else Is_OK_Static_Expression (Constr);
611
612 when N_Discriminant_Association =>
613 return All_Composite_Constraints_Static (Expression (Constr));
614
615 when N_Range_Constraint =>
616 return
617 All_Composite_Constraints_Static (Range_Expression (Constr));
618
619 when N_Index_Or_Discriminant_Constraint =>
620 declare
621 One_Cstr : Entity_Id;
622 begin
623 One_Cstr := First (Constraints (Constr));
624 while Present (One_Cstr) loop
625 if not All_Composite_Constraints_Static (One_Cstr) then
626 return False;
627 end if;
628
629 Next (One_Cstr);
630 end loop;
631 end;
632
633 return True;
634
635 when N_Subtype_Indication =>
636 return
637 All_Composite_Constraints_Static (Subtype_Mark (Constr))
638 and then
639 All_Composite_Constraints_Static (Constraint (Constr));
640
641 when others =>
642 raise Program_Error;
643 end case;
644 end All_Composite_Constraints_Static;
645
646 ------------------------
647 -- Append_Entity_Name --
648 ------------------------
649
650 procedure Append_Entity_Name (Buf : in out Bounded_String; E : Entity_Id) is
651 Temp : Bounded_String;
652
653 procedure Inner (E : Entity_Id);
654 -- Inner recursive routine, keep outer routine nonrecursive to ease
655 -- debugging when we get strange results from this routine.
656
657 -----------
658 -- Inner --
659 -----------
660
661 procedure Inner (E : Entity_Id) is
662 Scop : Node_Id;
663
664 begin
665 -- If entity has an internal name, skip by it, and print its scope.
666 -- Note that we strip a final R from the name before the test; this
667 -- is needed for some cases of instantiations.
668
669 declare
670 E_Name : Bounded_String;
671
672 begin
673 Append (E_Name, Chars (E));
674
675 if E_Name.Chars (E_Name.Length) = 'R' then
676 E_Name.Length := E_Name.Length - 1;
677 end if;
678
679 if Is_Internal_Name (E_Name) then
680 Inner (Scope (E));
681 return;
682 end if;
683 end;
684
685 Scop := Scope (E);
686
687 -- Just print entity name if its scope is at the outer level
688
689 if Scop = Standard_Standard then
690 null;
691
692 -- If scope comes from source, write scope and entity
693
694 elsif Comes_From_Source (Scop) then
695 Append_Entity_Name (Temp, Scop);
696 Append (Temp, '.');
697
698 -- If in wrapper package skip past it
699
700 elsif Present (Scop) and then Is_Wrapper_Package (Scop) then
701 Append_Entity_Name (Temp, Scope (Scop));
702 Append (Temp, '.');
703
704 -- Otherwise nothing to output (happens in unnamed block statements)
705
706 else
707 null;
708 end if;
709
710 -- Output the name
711
712 declare
713 E_Name : Bounded_String;
714
715 begin
716 Append_Unqualified_Decoded (E_Name, Chars (E));
717
718 -- Remove trailing upper-case letters from the name (useful for
719 -- dealing with some cases of internal names generated in the case
720 -- of references from within a generic).
721
722 while E_Name.Length > 1
723 and then E_Name.Chars (E_Name.Length) in 'A' .. 'Z'
724 loop
725 E_Name.Length := E_Name.Length - 1;
726 end loop;
727
728 -- Adjust casing appropriately (gets name from source if possible)
729
730 Adjust_Name_Case (E_Name, Sloc (E));
731 Append (Temp, E_Name);
732 end;
733 end Inner;
734
735 -- Start of processing for Append_Entity_Name
736
737 begin
738 Inner (E);
739 Append (Buf, Temp);
740 end Append_Entity_Name;
741
742 ---------------------------------
743 -- Append_Inherited_Subprogram --
744 ---------------------------------
745
746 procedure Append_Inherited_Subprogram (S : Entity_Id) is
747 Par : constant Entity_Id := Alias (S);
748 -- The parent subprogram
749
750 Scop : constant Entity_Id := Scope (Par);
751 -- The scope of definition of the parent subprogram
752
753 Typ : constant Entity_Id := Defining_Entity (Parent (S));
754 -- The derived type of which S is a primitive operation
755
756 Decl : Node_Id;
757 Next_E : Entity_Id;
758
759 begin
760 if Ekind (Current_Scope) = E_Package
761 and then In_Private_Part (Current_Scope)
762 and then Has_Private_Declaration (Typ)
763 and then Is_Tagged_Type (Typ)
764 and then Scop = Current_Scope
765 then
766 -- The inherited operation is available at the earliest place after
767 -- the derived type declaration (RM 7.3.1 (6/1)). This is only
768 -- relevant for type extensions. If the parent operation appears
769 -- after the type extension, the operation is not visible.
770
771 Decl := First
772 (Visible_Declarations
773 (Package_Specification (Current_Scope)));
774 while Present (Decl) loop
775 if Nkind (Decl) = N_Private_Extension_Declaration
776 and then Defining_Entity (Decl) = Typ
777 then
778 if Sloc (Decl) > Sloc (Par) then
779 Next_E := Next_Entity (Par);
780 Link_Entities (Par, S);
781 Link_Entities (S, Next_E);
782 return;
783
784 else
785 exit;
786 end if;
787 end if;
788
789 Next (Decl);
790 end loop;
791 end if;
792
793 -- If partial view is not a type extension, or it appears before the
794 -- subprogram declaration, insert normally at end of entity list.
795
796 Append_Entity (S, Current_Scope);
797 end Append_Inherited_Subprogram;
798
799 -----------------------------------------
800 -- Apply_Compile_Time_Constraint_Error --
801 -----------------------------------------
802
803 procedure Apply_Compile_Time_Constraint_Error
804 (N : Node_Id;
805 Msg : String;
806 Reason : RT_Exception_Code;
807 Ent : Entity_Id := Empty;
808 Typ : Entity_Id := Empty;
809 Loc : Source_Ptr := No_Location;
810 Rep : Boolean := True;
811 Warn : Boolean := False)
812 is
813 Stat : constant Boolean := Is_Static_Expression (N);
814 R_Stat : constant Node_Id :=
815 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
816 Rtyp : Entity_Id;
817
818 begin
819 if No (Typ) then
820 Rtyp := Etype (N);
821 else
822 Rtyp := Typ;
823 end if;
824
825 Discard_Node
826 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
827
828 -- In GNATprove mode, do not replace the node with an exception raised.
829 -- In such a case, either the call to Compile_Time_Constraint_Error
830 -- issues an error which stops analysis, or it issues a warning in
831 -- a few cases where a suitable check flag is set for GNATprove to
832 -- generate a check message.
833
834 if not Rep or GNATprove_Mode then
835 return;
836 end if;
837
838 -- Now we replace the node by an N_Raise_Constraint_Error node
839 -- This does not need reanalyzing, so set it as analyzed now.
840
841 Rewrite (N, R_Stat);
842 Set_Analyzed (N, True);
843
844 Set_Etype (N, Rtyp);
845 Set_Raises_Constraint_Error (N);
846
847 -- Now deal with possible local raise handling
848
849 Possible_Local_Raise (N, Standard_Constraint_Error);
850
851 -- If the original expression was marked as static, the result is
852 -- still marked as static, but the Raises_Constraint_Error flag is
853 -- always set so that further static evaluation is not attempted.
854
855 if Stat then
856 Set_Is_Static_Expression (N);
857 end if;
858 end Apply_Compile_Time_Constraint_Error;
859
860 ---------------------------
861 -- Async_Readers_Enabled --
862 ---------------------------
863
864 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
865 begin
866 return Has_Enabled_Property (Id, Name_Async_Readers);
867 end Async_Readers_Enabled;
868
869 ---------------------------
870 -- Async_Writers_Enabled --
871 ---------------------------
872
873 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
874 begin
875 return Has_Enabled_Property (Id, Name_Async_Writers);
876 end Async_Writers_Enabled;
877
878 --------------------------------------
879 -- Available_Full_View_Of_Component --
880 --------------------------------------
881
882 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
883 ST : constant Entity_Id := Scope (T);
884 SCT : constant Entity_Id := Scope (Component_Type (T));
885 begin
886 return In_Open_Scopes (ST)
887 and then In_Open_Scopes (SCT)
888 and then Scope_Depth (ST) >= Scope_Depth (SCT);
889 end Available_Full_View_Of_Component;
890
891 -------------------
892 -- Bad_Attribute --
893 -------------------
894
895 procedure Bad_Attribute
896 (N : Node_Id;
897 Nam : Name_Id;
898 Warn : Boolean := False)
899 is
900 begin
901 Error_Msg_Warn := Warn;
902 Error_Msg_N ("unrecognized attribute&<<", N);
903
904 -- Check for possible misspelling
905
906 Error_Msg_Name_1 := First_Attribute_Name;
907 while Error_Msg_Name_1 <= Last_Attribute_Name loop
908 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
909 Error_Msg_N -- CODEFIX
910 ("\possible misspelling of %<<", N);
911 exit;
912 end if;
913
914 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
915 end loop;
916 end Bad_Attribute;
917
918 --------------------------------
919 -- Bad_Predicated_Subtype_Use --
920 --------------------------------
921
922 procedure Bad_Predicated_Subtype_Use
923 (Msg : String;
924 N : Node_Id;
925 Typ : Entity_Id;
926 Suggest_Static : Boolean := False)
927 is
928 Gen : Entity_Id;
929
930 begin
931 -- Avoid cascaded errors
932
933 if Error_Posted (N) then
934 return;
935 end if;
936
937 if Inside_A_Generic then
938 Gen := Current_Scope;
939 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
940 Gen := Scope (Gen);
941 end loop;
942
943 if No (Gen) then
944 return;
945 end if;
946
947 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
948 Set_No_Predicate_On_Actual (Typ);
949 end if;
950
951 elsif Has_Predicates (Typ) then
952 if Is_Generic_Actual_Type (Typ) then
953
954 -- The restriction on loop parameters is only that the type
955 -- should have no dynamic predicates.
956
957 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
958 and then not Has_Dynamic_Predicate_Aspect (Typ)
959 and then Is_OK_Static_Subtype (Typ)
960 then
961 return;
962 end if;
963
964 Gen := Current_Scope;
965 while not Is_Generic_Instance (Gen) loop
966 Gen := Scope (Gen);
967 end loop;
968
969 pragma Assert (Present (Gen));
970
971 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
972 Error_Msg_Warn := SPARK_Mode /= On;
973 Error_Msg_FE (Msg & "<<", N, Typ);
974 Error_Msg_F ("\Program_Error [<<", N);
975
976 Insert_Action (N,
977 Make_Raise_Program_Error (Sloc (N),
978 Reason => PE_Bad_Predicated_Generic_Type));
979
980 else
981 Error_Msg_FE (Msg & "<<", N, Typ);
982 end if;
983
984 else
985 Error_Msg_FE (Msg, N, Typ);
986 end if;
987
988 -- Emit an optional suggestion on how to remedy the error if the
989 -- context warrants it.
990
991 if Suggest_Static and then Has_Static_Predicate (Typ) then
992 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
993 end if;
994 end if;
995 end Bad_Predicated_Subtype_Use;
996
997 -----------------------------------------
998 -- Bad_Unordered_Enumeration_Reference --
999 -----------------------------------------
1000
1001 function Bad_Unordered_Enumeration_Reference
1002 (N : Node_Id;
1003 T : Entity_Id) return Boolean
1004 is
1005 begin
1006 return Is_Enumeration_Type (T)
1007 and then Warn_On_Unordered_Enumeration_Type
1008 and then not Is_Generic_Type (T)
1009 and then Comes_From_Source (N)
1010 and then not Has_Pragma_Ordered (T)
1011 and then not In_Same_Extended_Unit (N, T);
1012 end Bad_Unordered_Enumeration_Reference;
1013
1014 ----------------------------
1015 -- Begin_Keyword_Location --
1016 ----------------------------
1017
1018 function Begin_Keyword_Location (N : Node_Id) return Source_Ptr is
1019 HSS : Node_Id;
1020
1021 begin
1022 pragma Assert (Nkind_In (N, N_Block_Statement,
1023 N_Entry_Body,
1024 N_Package_Body,
1025 N_Subprogram_Body,
1026 N_Task_Body));
1027
1028 HSS := Handled_Statement_Sequence (N);
1029
1030 -- When the handled sequence of statements comes from source, the
1031 -- location of the "begin" keyword is that of the sequence itself.
1032 -- Note that an internal construct may inherit a source sequence.
1033
1034 if Comes_From_Source (HSS) then
1035 return Sloc (HSS);
1036
1037 -- The parser generates an internal handled sequence of statements to
1038 -- capture the location of the "begin" keyword if present in the source.
1039 -- Since there are no source statements, the location of the "begin"
1040 -- keyword is effectively that of the "end" keyword.
1041
1042 elsif Comes_From_Source (N) then
1043 return Sloc (HSS);
1044
1045 -- Otherwise the construct is internal and should carry the location of
1046 -- the original construct which prompted its creation.
1047
1048 else
1049 return Sloc (N);
1050 end if;
1051 end Begin_Keyword_Location;
1052
1053 --------------------------
1054 -- Build_Actual_Subtype --
1055 --------------------------
1056
1057 function Build_Actual_Subtype
1058 (T : Entity_Id;
1059 N : Node_Or_Entity_Id) return Node_Id
1060 is
1061 Loc : Source_Ptr;
1062 -- Normally Sloc (N), but may point to corresponding body in some cases
1063
1064 Constraints : List_Id;
1065 Decl : Node_Id;
1066 Discr : Entity_Id;
1067 Hi : Node_Id;
1068 Lo : Node_Id;
1069 Subt : Entity_Id;
1070 Disc_Type : Entity_Id;
1071 Obj : Node_Id;
1072
1073 begin
1074 Loc := Sloc (N);
1075
1076 if Nkind (N) = N_Defining_Identifier then
1077 Obj := New_Occurrence_Of (N, Loc);
1078
1079 -- If this is a formal parameter of a subprogram declaration, and
1080 -- we are compiling the body, we want the declaration for the
1081 -- actual subtype to carry the source position of the body, to
1082 -- prevent anomalies in gdb when stepping through the code.
1083
1084 if Is_Formal (N) then
1085 declare
1086 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
1087 begin
1088 if Nkind (Decl) = N_Subprogram_Declaration
1089 and then Present (Corresponding_Body (Decl))
1090 then
1091 Loc := Sloc (Corresponding_Body (Decl));
1092 end if;
1093 end;
1094 end if;
1095
1096 else
1097 Obj := N;
1098 end if;
1099
1100 if Is_Array_Type (T) then
1101 Constraints := New_List;
1102 for J in 1 .. Number_Dimensions (T) loop
1103
1104 -- Build an array subtype declaration with the nominal subtype and
1105 -- the bounds of the actual. Add the declaration in front of the
1106 -- local declarations for the subprogram, for analysis before any
1107 -- reference to the formal in the body.
1108
1109 Lo :=
1110 Make_Attribute_Reference (Loc,
1111 Prefix =>
1112 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
1113 Attribute_Name => Name_First,
1114 Expressions => New_List (
1115 Make_Integer_Literal (Loc, J)));
1116
1117 Hi :=
1118 Make_Attribute_Reference (Loc,
1119 Prefix =>
1120 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
1121 Attribute_Name => Name_Last,
1122 Expressions => New_List (
1123 Make_Integer_Literal (Loc, J)));
1124
1125 Append (Make_Range (Loc, Lo, Hi), Constraints);
1126 end loop;
1127
1128 -- If the type has unknown discriminants there is no constrained
1129 -- subtype to build. This is never called for a formal or for a
1130 -- lhs, so returning the type is ok ???
1131
1132 elsif Has_Unknown_Discriminants (T) then
1133 return T;
1134
1135 else
1136 Constraints := New_List;
1137
1138 -- Type T is a generic derived type, inherit the discriminants from
1139 -- the parent type.
1140
1141 if Is_Private_Type (T)
1142 and then No (Full_View (T))
1143
1144 -- T was flagged as an error if it was declared as a formal
1145 -- derived type with known discriminants. In this case there
1146 -- is no need to look at the parent type since T already carries
1147 -- its own discriminants.
1148
1149 and then not Error_Posted (T)
1150 then
1151 Disc_Type := Etype (Base_Type (T));
1152 else
1153 Disc_Type := T;
1154 end if;
1155
1156 Discr := First_Discriminant (Disc_Type);
1157 while Present (Discr) loop
1158 Append_To (Constraints,
1159 Make_Selected_Component (Loc,
1160 Prefix =>
1161 Duplicate_Subexpr_No_Checks (Obj),
1162 Selector_Name => New_Occurrence_Of (Discr, Loc)));
1163 Next_Discriminant (Discr);
1164 end loop;
1165 end if;
1166
1167 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
1168 Set_Is_Internal (Subt);
1169
1170 Decl :=
1171 Make_Subtype_Declaration (Loc,
1172 Defining_Identifier => Subt,
1173 Subtype_Indication =>
1174 Make_Subtype_Indication (Loc,
1175 Subtype_Mark => New_Occurrence_Of (T, Loc),
1176 Constraint =>
1177 Make_Index_Or_Discriminant_Constraint (Loc,
1178 Constraints => Constraints)));
1179
1180 Mark_Rewrite_Insertion (Decl);
1181 return Decl;
1182 end Build_Actual_Subtype;
1183
1184 ---------------------------------------
1185 -- Build_Actual_Subtype_Of_Component --
1186 ---------------------------------------
1187
1188 function Build_Actual_Subtype_Of_Component
1189 (T : Entity_Id;
1190 N : Node_Id) return Node_Id
1191 is
1192 Loc : constant Source_Ptr := Sloc (N);
1193 P : constant Node_Id := Prefix (N);
1194
1195 D : Elmt_Id;
1196 Id : Node_Id;
1197 Index_Typ : Entity_Id;
1198 Sel : Entity_Id := Empty;
1199
1200 Desig_Typ : Entity_Id;
1201 -- This is either a copy of T, or if T is an access type, then it is
1202 -- the directly designated type of this access type.
1203
1204 function Build_Access_Record_Constraint (C : List_Id) return List_Id;
1205 -- If the record component is a constrained access to the current
1206 -- record, the subtype has not been constructed during analysis of
1207 -- the enclosing record type (see Analyze_Access). In that case, build
1208 -- a constrained access subtype after replacing references to the
1209 -- enclosing discriminants with the corresponding discriminant values
1210 -- of the prefix.
1211
1212 function Build_Actual_Array_Constraint return List_Id;
1213 -- If one or more of the bounds of the component depends on
1214 -- discriminants, build actual constraint using the discriminants
1215 -- of the prefix, as above.
1216
1217 function Build_Actual_Record_Constraint return List_Id;
1218 -- Similar to previous one, for discriminated components constrained
1219 -- by the discriminant of the enclosing object.
1220
1221 -----------------------------------
1222 -- Build_Actual_Array_Constraint --
1223 -----------------------------------
1224
1225 function Build_Actual_Array_Constraint return List_Id is
1226 Constraints : constant List_Id := New_List;
1227 Indx : Node_Id;
1228 Hi : Node_Id;
1229 Lo : Node_Id;
1230 Old_Hi : Node_Id;
1231 Old_Lo : Node_Id;
1232
1233 begin
1234 Indx := First_Index (Desig_Typ);
1235 while Present (Indx) loop
1236 Old_Lo := Type_Low_Bound (Etype (Indx));
1237 Old_Hi := Type_High_Bound (Etype (Indx));
1238
1239 if Denotes_Discriminant (Old_Lo) then
1240 Lo :=
1241 Make_Selected_Component (Loc,
1242 Prefix => New_Copy_Tree (P),
1243 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1244
1245 else
1246 Lo := New_Copy_Tree (Old_Lo);
1247
1248 -- The new bound will be reanalyzed in the enclosing
1249 -- declaration. For literal bounds that come from a type
1250 -- declaration, the type of the context must be imposed, so
1251 -- insure that analysis will take place. For non-universal
1252 -- types this is not strictly necessary.
1253
1254 Set_Analyzed (Lo, False);
1255 end if;
1256
1257 if Denotes_Discriminant (Old_Hi) then
1258 Hi :=
1259 Make_Selected_Component (Loc,
1260 Prefix => New_Copy_Tree (P),
1261 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1262
1263 else
1264 Hi := New_Copy_Tree (Old_Hi);
1265 Set_Analyzed (Hi, False);
1266 end if;
1267
1268 Append (Make_Range (Loc, Lo, Hi), Constraints);
1269 Next_Index (Indx);
1270 end loop;
1271
1272 return Constraints;
1273 end Build_Actual_Array_Constraint;
1274
1275 ------------------------------------
1276 -- Build_Actual_Record_Constraint --
1277 ------------------------------------
1278
1279 function Build_Actual_Record_Constraint return List_Id is
1280 Constraints : constant List_Id := New_List;
1281 D : Elmt_Id;
1282 D_Val : Node_Id;
1283
1284 begin
1285 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1286 while Present (D) loop
1287 if Denotes_Discriminant (Node (D)) then
1288 D_Val := Make_Selected_Component (Loc,
1289 Prefix => New_Copy_Tree (P),
1290 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1291
1292 else
1293 D_Val := New_Copy_Tree (Node (D));
1294 end if;
1295
1296 Append (D_Val, Constraints);
1297 Next_Elmt (D);
1298 end loop;
1299
1300 return Constraints;
1301 end Build_Actual_Record_Constraint;
1302
1303 ------------------------------------
1304 -- Build_Access_Record_Constraint --
1305 ------------------------------------
1306
1307 function Build_Access_Record_Constraint (C : List_Id) return List_Id is
1308 Constraints : constant List_Id := New_List;
1309 D : Node_Id;
1310 D_Val : Node_Id;
1311
1312 begin
1313 -- Retrieve the constraint from the component declaration, because
1314 -- the component subtype has not been constructed and the component
1315 -- type is an unconstrained access.
1316
1317 D := First (C);
1318 while Present (D) loop
1319 if Nkind (D) = N_Discriminant_Association
1320 and then Denotes_Discriminant (Expression (D))
1321 then
1322 D_Val := New_Copy_Tree (D);
1323 Set_Expression (D_Val,
1324 Make_Selected_Component (Loc,
1325 Prefix => New_Copy_Tree (P),
1326 Selector_Name =>
1327 New_Occurrence_Of (Entity (Expression (D)), Loc)));
1328
1329 elsif Denotes_Discriminant (D) then
1330 D_Val := Make_Selected_Component (Loc,
1331 Prefix => New_Copy_Tree (P),
1332 Selector_Name => New_Occurrence_Of (Entity (D), Loc));
1333
1334 else
1335 D_Val := New_Copy_Tree (D);
1336 end if;
1337
1338 Append (D_Val, Constraints);
1339 Next (D);
1340 end loop;
1341
1342 return Constraints;
1343 end Build_Access_Record_Constraint;
1344
1345 -- Start of processing for Build_Actual_Subtype_Of_Component
1346
1347 begin
1348 -- The subtype does not need to be created for a selected component
1349 -- in a Spec_Expression.
1350
1351 if In_Spec_Expression then
1352 return Empty;
1353
1354 -- More comments for the rest of this body would be good ???
1355
1356 elsif Nkind (N) = N_Explicit_Dereference then
1357 if Is_Composite_Type (T)
1358 and then not Is_Constrained (T)
1359 and then not (Is_Class_Wide_Type (T)
1360 and then Is_Constrained (Root_Type (T)))
1361 and then not Has_Unknown_Discriminants (T)
1362 then
1363 -- If the type of the dereference is already constrained, it is an
1364 -- actual subtype.
1365
1366 if Is_Array_Type (Etype (N))
1367 and then Is_Constrained (Etype (N))
1368 then
1369 return Empty;
1370 else
1371 Remove_Side_Effects (P);
1372 return Build_Actual_Subtype (T, N);
1373 end if;
1374
1375 else
1376 return Empty;
1377 end if;
1378
1379 elsif Nkind (N) = N_Selected_Component then
1380 -- The entity of the selected component allows us to retrieve
1381 -- the original constraint from its component declaration.
1382
1383 Sel := Entity (Selector_Name (N));
1384 if Nkind (Parent (Sel)) /= N_Component_Declaration then
1385 return Empty;
1386 end if;
1387 end if;
1388
1389 if Is_Access_Type (T) then
1390 Desig_Typ := Designated_Type (T);
1391
1392 else
1393 Desig_Typ := T;
1394 end if;
1395
1396 if Ekind (Desig_Typ) = E_Array_Subtype then
1397 Id := First_Index (Desig_Typ);
1398
1399 -- Check whether an index bound is constrained by a discriminant.
1400
1401 while Present (Id) loop
1402 Index_Typ := Underlying_Type (Etype (Id));
1403
1404 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1405 or else
1406 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1407 then
1408 Remove_Side_Effects (P);
1409 return
1410 Build_Component_Subtype
1411 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1412 end if;
1413
1414 Next_Index (Id);
1415 end loop;
1416
1417 elsif Is_Composite_Type (Desig_Typ)
1418 and then Has_Discriminants (Desig_Typ)
1419 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Desig_Typ))
1420 and then not Has_Unknown_Discriminants (Desig_Typ)
1421 then
1422 if Is_Private_Type (Desig_Typ)
1423 and then No (Discriminant_Constraint (Desig_Typ))
1424 then
1425 Desig_Typ := Full_View (Desig_Typ);
1426 end if;
1427
1428 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1429 while Present (D) loop
1430 if Denotes_Discriminant (Node (D)) then
1431 Remove_Side_Effects (P);
1432 return
1433 Build_Component_Subtype (
1434 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1435 end if;
1436
1437 Next_Elmt (D);
1438 end loop;
1439
1440 -- Special processing for an access record component that is
1441 -- the target of an assignment. If the designated type is an
1442 -- unconstrained discriminated record we create its actual
1443 -- subtype now.
1444
1445 elsif Ekind (T) = E_Access_Type
1446 and then Present (Sel)
1447 and then Has_Per_Object_Constraint (Sel)
1448 and then Nkind (Parent (N)) = N_Assignment_Statement
1449 and then N = Name (Parent (N))
1450 -- and then not Inside_Init_Proc
1451 -- and then Has_Discriminants (Desig_Typ)
1452 -- and then not Is_Constrained (Desig_Typ)
1453 then
1454 declare
1455 S_Indic : constant Node_Id :=
1456 (Subtype_Indication
1457 (Component_Definition (Parent (Sel))));
1458 Discs : List_Id;
1459 begin
1460 if Nkind (S_Indic) = N_Subtype_Indication then
1461 Discs := Constraints (Constraint (S_Indic));
1462
1463 Remove_Side_Effects (P);
1464 return Build_Component_Subtype
1465 (Build_Access_Record_Constraint (Discs), Loc, T);
1466 else
1467 return Empty;
1468 end if;
1469 end;
1470 end if;
1471
1472 -- If none of the above, the actual and nominal subtypes are the same
1473
1474 return Empty;
1475 end Build_Actual_Subtype_Of_Component;
1476
1477 ---------------------------------
1478 -- Build_Class_Wide_Clone_Body --
1479 ---------------------------------
1480
1481 procedure Build_Class_Wide_Clone_Body
1482 (Spec_Id : Entity_Id;
1483 Bod : Node_Id)
1484 is
1485 Loc : constant Source_Ptr := Sloc (Bod);
1486 Clone_Id : constant Entity_Id := Class_Wide_Clone (Spec_Id);
1487 Clone_Body : Node_Id;
1488
1489 begin
1490 -- The declaration of the class-wide clone was created when the
1491 -- corresponding class-wide condition was analyzed.
1492
1493 Clone_Body :=
1494 Make_Subprogram_Body (Loc,
1495 Specification =>
1496 Copy_Subprogram_Spec (Parent (Clone_Id)),
1497 Declarations => Declarations (Bod),
1498 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
1499
1500 -- The new operation is internal and overriding indicators do not apply
1501 -- (the original primitive may have carried one).
1502
1503 Set_Must_Override (Specification (Clone_Body), False);
1504
1505 -- If the subprogram body is the proper body of a stub, insert the
1506 -- subprogram after the stub, i.e. the same declarative region as
1507 -- the original sugprogram.
1508
1509 if Nkind (Parent (Bod)) = N_Subunit then
1510 Insert_After (Corresponding_Stub (Parent (Bod)), Clone_Body);
1511
1512 else
1513 Insert_Before (Bod, Clone_Body);
1514 end if;
1515
1516 Analyze (Clone_Body);
1517 end Build_Class_Wide_Clone_Body;
1518
1519 ---------------------------------
1520 -- Build_Class_Wide_Clone_Call --
1521 ---------------------------------
1522
1523 function Build_Class_Wide_Clone_Call
1524 (Loc : Source_Ptr;
1525 Decls : List_Id;
1526 Spec_Id : Entity_Id;
1527 Spec : Node_Id) return Node_Id
1528 is
1529 Clone_Id : constant Entity_Id := Class_Wide_Clone (Spec_Id);
1530 Par_Type : constant Entity_Id := Find_Dispatching_Type (Spec_Id);
1531
1532 Actuals : List_Id;
1533 Call : Node_Id;
1534 Formal : Entity_Id;
1535 New_Body : Node_Id;
1536 New_F_Spec : Entity_Id;
1537 New_Formal : Entity_Id;
1538
1539 begin
1540 Actuals := Empty_List;
1541 Formal := First_Formal (Spec_Id);
1542 New_F_Spec := First (Parameter_Specifications (Spec));
1543
1544 -- Build parameter association for call to class-wide clone.
1545
1546 while Present (Formal) loop
1547 New_Formal := Defining_Identifier (New_F_Spec);
1548
1549 -- If controlling argument and operation is inherited, add conversion
1550 -- to parent type for the call.
1551
1552 if Etype (Formal) = Par_Type
1553 and then not Is_Empty_List (Decls)
1554 then
1555 Append_To (Actuals,
1556 Make_Type_Conversion (Loc,
1557 New_Occurrence_Of (Par_Type, Loc),
1558 New_Occurrence_Of (New_Formal, Loc)));
1559
1560 else
1561 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1562 end if;
1563
1564 Next_Formal (Formal);
1565 Next (New_F_Spec);
1566 end loop;
1567
1568 if Ekind (Spec_Id) = E_Procedure then
1569 Call :=
1570 Make_Procedure_Call_Statement (Loc,
1571 Name => New_Occurrence_Of (Clone_Id, Loc),
1572 Parameter_Associations => Actuals);
1573 else
1574 Call :=
1575 Make_Simple_Return_Statement (Loc,
1576 Expression =>
1577 Make_Function_Call (Loc,
1578 Name => New_Occurrence_Of (Clone_Id, Loc),
1579 Parameter_Associations => Actuals));
1580 end if;
1581
1582 New_Body :=
1583 Make_Subprogram_Body (Loc,
1584 Specification =>
1585 Copy_Subprogram_Spec (Spec),
1586 Declarations => Decls,
1587 Handled_Statement_Sequence =>
1588 Make_Handled_Sequence_Of_Statements (Loc,
1589 Statements => New_List (Call),
1590 End_Label => Make_Identifier (Loc, Chars (Spec_Id))));
1591
1592 return New_Body;
1593 end Build_Class_Wide_Clone_Call;
1594
1595 ---------------------------------
1596 -- Build_Class_Wide_Clone_Decl --
1597 ---------------------------------
1598
1599 procedure Build_Class_Wide_Clone_Decl (Spec_Id : Entity_Id) is
1600 Loc : constant Source_Ptr := Sloc (Spec_Id);
1601 Clone_Id : constant Entity_Id :=
1602 Make_Defining_Identifier (Loc,
1603 New_External_Name (Chars (Spec_Id), Suffix => "CL"));
1604
1605 Decl : Node_Id;
1606 Spec : Node_Id;
1607
1608 begin
1609 Spec := Copy_Subprogram_Spec (Parent (Spec_Id));
1610 Set_Must_Override (Spec, False);
1611 Set_Must_Not_Override (Spec, False);
1612 Set_Defining_Unit_Name (Spec, Clone_Id);
1613
1614 Decl := Make_Subprogram_Declaration (Loc, Spec);
1615 Append (Decl, List_Containing (Unit_Declaration_Node (Spec_Id)));
1616
1617 -- Link clone to original subprogram, for use when building body and
1618 -- wrapper call to inherited operation.
1619
1620 Set_Class_Wide_Clone (Spec_Id, Clone_Id);
1621
1622 -- Inherit debug info flag from Spec_Id to Clone_Id to allow debugging
1623 -- of the class-wide clone subprogram.
1624
1625 if Needs_Debug_Info (Spec_Id) then
1626 Set_Debug_Info_Needed (Clone_Id);
1627 end if;
1628 end Build_Class_Wide_Clone_Decl;
1629
1630 -----------------------------
1631 -- Build_Component_Subtype --
1632 -----------------------------
1633
1634 function Build_Component_Subtype
1635 (C : List_Id;
1636 Loc : Source_Ptr;
1637 T : Entity_Id) return Node_Id
1638 is
1639 Subt : Entity_Id;
1640 Decl : Node_Id;
1641
1642 begin
1643 -- Unchecked_Union components do not require component subtypes
1644
1645 if Is_Unchecked_Union (T) then
1646 return Empty;
1647 end if;
1648
1649 Subt := Make_Temporary (Loc, 'S');
1650 Set_Is_Internal (Subt);
1651
1652 Decl :=
1653 Make_Subtype_Declaration (Loc,
1654 Defining_Identifier => Subt,
1655 Subtype_Indication =>
1656 Make_Subtype_Indication (Loc,
1657 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1658 Constraint =>
1659 Make_Index_Or_Discriminant_Constraint (Loc,
1660 Constraints => C)));
1661
1662 Mark_Rewrite_Insertion (Decl);
1663 return Decl;
1664 end Build_Component_Subtype;
1665
1666 ---------------------------
1667 -- Build_Default_Subtype --
1668 ---------------------------
1669
1670 function Build_Default_Subtype
1671 (T : Entity_Id;
1672 N : Node_Id) return Entity_Id
1673 is
1674 Loc : constant Source_Ptr := Sloc (N);
1675 Disc : Entity_Id;
1676
1677 Bas : Entity_Id;
1678 -- The base type that is to be constrained by the defaults
1679
1680 begin
1681 if not Has_Discriminants (T) or else Is_Constrained (T) then
1682 return T;
1683 end if;
1684
1685 Bas := Base_Type (T);
1686
1687 -- If T is non-private but its base type is private, this is the
1688 -- completion of a subtype declaration whose parent type is private
1689 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1690 -- are to be found in the full view of the base. Check that the private
1691 -- status of T and its base differ.
1692
1693 if Is_Private_Type (Bas)
1694 and then not Is_Private_Type (T)
1695 and then Present (Full_View (Bas))
1696 then
1697 Bas := Full_View (Bas);
1698 end if;
1699
1700 Disc := First_Discriminant (T);
1701
1702 if No (Discriminant_Default_Value (Disc)) then
1703 return T;
1704 end if;
1705
1706 declare
1707 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1708 Constraints : constant List_Id := New_List;
1709 Decl : Node_Id;
1710
1711 begin
1712 while Present (Disc) loop
1713 Append_To (Constraints,
1714 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1715 Next_Discriminant (Disc);
1716 end loop;
1717
1718 Decl :=
1719 Make_Subtype_Declaration (Loc,
1720 Defining_Identifier => Act,
1721 Subtype_Indication =>
1722 Make_Subtype_Indication (Loc,
1723 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1724 Constraint =>
1725 Make_Index_Or_Discriminant_Constraint (Loc,
1726 Constraints => Constraints)));
1727
1728 Insert_Action (N, Decl);
1729
1730 -- If the context is a component declaration the subtype declaration
1731 -- will be analyzed when the enclosing type is frozen, otherwise do
1732 -- it now.
1733
1734 if Ekind (Current_Scope) /= E_Record_Type then
1735 Analyze (Decl);
1736 end if;
1737
1738 return Act;
1739 end;
1740 end Build_Default_Subtype;
1741
1742 --------------------------------------------
1743 -- Build_Discriminal_Subtype_Of_Component --
1744 --------------------------------------------
1745
1746 function Build_Discriminal_Subtype_Of_Component
1747 (T : Entity_Id) return Node_Id
1748 is
1749 Loc : constant Source_Ptr := Sloc (T);
1750 D : Elmt_Id;
1751 Id : Node_Id;
1752
1753 function Build_Discriminal_Array_Constraint return List_Id;
1754 -- If one or more of the bounds of the component depends on
1755 -- discriminants, build actual constraint using the discriminants
1756 -- of the prefix.
1757
1758 function Build_Discriminal_Record_Constraint return List_Id;
1759 -- Similar to previous one, for discriminated components constrained by
1760 -- the discriminant of the enclosing object.
1761
1762 ----------------------------------------
1763 -- Build_Discriminal_Array_Constraint --
1764 ----------------------------------------
1765
1766 function Build_Discriminal_Array_Constraint return List_Id is
1767 Constraints : constant List_Id := New_List;
1768 Indx : Node_Id;
1769 Hi : Node_Id;
1770 Lo : Node_Id;
1771 Old_Hi : Node_Id;
1772 Old_Lo : Node_Id;
1773
1774 begin
1775 Indx := First_Index (T);
1776 while Present (Indx) loop
1777 Old_Lo := Type_Low_Bound (Etype (Indx));
1778 Old_Hi := Type_High_Bound (Etype (Indx));
1779
1780 if Denotes_Discriminant (Old_Lo) then
1781 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1782
1783 else
1784 Lo := New_Copy_Tree (Old_Lo);
1785 end if;
1786
1787 if Denotes_Discriminant (Old_Hi) then
1788 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1789
1790 else
1791 Hi := New_Copy_Tree (Old_Hi);
1792 end if;
1793
1794 Append (Make_Range (Loc, Lo, Hi), Constraints);
1795 Next_Index (Indx);
1796 end loop;
1797
1798 return Constraints;
1799 end Build_Discriminal_Array_Constraint;
1800
1801 -----------------------------------------
1802 -- Build_Discriminal_Record_Constraint --
1803 -----------------------------------------
1804
1805 function Build_Discriminal_Record_Constraint return List_Id is
1806 Constraints : constant List_Id := New_List;
1807 D : Elmt_Id;
1808 D_Val : Node_Id;
1809
1810 begin
1811 D := First_Elmt (Discriminant_Constraint (T));
1812 while Present (D) loop
1813 if Denotes_Discriminant (Node (D)) then
1814 D_Val :=
1815 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1816 else
1817 D_Val := New_Copy_Tree (Node (D));
1818 end if;
1819
1820 Append (D_Val, Constraints);
1821 Next_Elmt (D);
1822 end loop;
1823
1824 return Constraints;
1825 end Build_Discriminal_Record_Constraint;
1826
1827 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1828
1829 begin
1830 if Ekind (T) = E_Array_Subtype then
1831 Id := First_Index (T);
1832 while Present (Id) loop
1833 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1834 or else
1835 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1836 then
1837 return Build_Component_Subtype
1838 (Build_Discriminal_Array_Constraint, Loc, T);
1839 end if;
1840
1841 Next_Index (Id);
1842 end loop;
1843
1844 elsif Ekind (T) = E_Record_Subtype
1845 and then Has_Discriminants (T)
1846 and then not Has_Unknown_Discriminants (T)
1847 then
1848 D := First_Elmt (Discriminant_Constraint (T));
1849 while Present (D) loop
1850 if Denotes_Discriminant (Node (D)) then
1851 return Build_Component_Subtype
1852 (Build_Discriminal_Record_Constraint, Loc, T);
1853 end if;
1854
1855 Next_Elmt (D);
1856 end loop;
1857 end if;
1858
1859 -- If none of the above, the actual and nominal subtypes are the same
1860
1861 return Empty;
1862 end Build_Discriminal_Subtype_Of_Component;
1863
1864 ------------------------------
1865 -- Build_Elaboration_Entity --
1866 ------------------------------
1867
1868 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1869 Loc : constant Source_Ptr := Sloc (N);
1870 Decl : Node_Id;
1871 Elab_Ent : Entity_Id;
1872
1873 procedure Set_Package_Name (Ent : Entity_Id);
1874 -- Given an entity, sets the fully qualified name of the entity in
1875 -- Name_Buffer, with components separated by double underscores. This
1876 -- is a recursive routine that climbs the scope chain to Standard.
1877
1878 ----------------------
1879 -- Set_Package_Name --
1880 ----------------------
1881
1882 procedure Set_Package_Name (Ent : Entity_Id) is
1883 begin
1884 if Scope (Ent) /= Standard_Standard then
1885 Set_Package_Name (Scope (Ent));
1886
1887 declare
1888 Nam : constant String := Get_Name_String (Chars (Ent));
1889 begin
1890 Name_Buffer (Name_Len + 1) := '_';
1891 Name_Buffer (Name_Len + 2) := '_';
1892 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1893 Name_Len := Name_Len + Nam'Length + 2;
1894 end;
1895
1896 else
1897 Get_Name_String (Chars (Ent));
1898 end if;
1899 end Set_Package_Name;
1900
1901 -- Start of processing for Build_Elaboration_Entity
1902
1903 begin
1904 -- Ignore call if already constructed
1905
1906 if Present (Elaboration_Entity (Spec_Id)) then
1907 return;
1908
1909 -- Do not generate an elaboration entity in GNATprove move because the
1910 -- elaboration counter is a form of expansion.
1911
1912 elsif GNATprove_Mode then
1913 return;
1914
1915 -- See if we need elaboration entity
1916
1917 -- We always need an elaboration entity when preserving control flow, as
1918 -- we want to remain explicit about the unit's elaboration order.
1919
1920 elsif Opt.Suppress_Control_Flow_Optimizations then
1921 null;
1922
1923 -- We always need an elaboration entity for the dynamic elaboration
1924 -- model, since it is needed to properly generate the PE exception for
1925 -- access before elaboration.
1926
1927 elsif Dynamic_Elaboration_Checks then
1928 null;
1929
1930 -- For the static model, we don't need the elaboration counter if this
1931 -- unit is sure to have no elaboration code, since that means there
1932 -- is no elaboration unit to be called. Note that we can't just decide
1933 -- after the fact by looking to see whether there was elaboration code,
1934 -- because that's too late to make this decision.
1935
1936 elsif Restriction_Active (No_Elaboration_Code) then
1937 return;
1938
1939 -- Similarly, for the static model, we can skip the elaboration counter
1940 -- if we have the No_Multiple_Elaboration restriction, since for the
1941 -- static model, that's the only purpose of the counter (to avoid
1942 -- multiple elaboration).
1943
1944 elsif Restriction_Active (No_Multiple_Elaboration) then
1945 return;
1946 end if;
1947
1948 -- Here we need the elaboration entity
1949
1950 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1951 -- name with dots replaced by double underscore. We have to manually
1952 -- construct this name, since it will be elaborated in the outer scope,
1953 -- and thus will not have the unit name automatically prepended.
1954
1955 Set_Package_Name (Spec_Id);
1956 Add_Str_To_Name_Buffer ("_E");
1957
1958 -- Create elaboration counter
1959
1960 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1961 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1962
1963 Decl :=
1964 Make_Object_Declaration (Loc,
1965 Defining_Identifier => Elab_Ent,
1966 Object_Definition =>
1967 New_Occurrence_Of (Standard_Short_Integer, Loc),
1968 Expression => Make_Integer_Literal (Loc, Uint_0));
1969
1970 Push_Scope (Standard_Standard);
1971 Add_Global_Declaration (Decl);
1972 Pop_Scope;
1973
1974 -- Reset True_Constant indication, since we will indeed assign a value
1975 -- to the variable in the binder main. We also kill the Current_Value
1976 -- and Last_Assignment fields for the same reason.
1977
1978 Set_Is_True_Constant (Elab_Ent, False);
1979 Set_Current_Value (Elab_Ent, Empty);
1980 Set_Last_Assignment (Elab_Ent, Empty);
1981
1982 -- We do not want any further qualification of the name (if we did not
1983 -- do this, we would pick up the name of the generic package in the case
1984 -- of a library level generic instantiation).
1985
1986 Set_Has_Qualified_Name (Elab_Ent);
1987 Set_Has_Fully_Qualified_Name (Elab_Ent);
1988 end Build_Elaboration_Entity;
1989
1990 --------------------------------
1991 -- Build_Explicit_Dereference --
1992 --------------------------------
1993
1994 procedure Build_Explicit_Dereference
1995 (Expr : Node_Id;
1996 Disc : Entity_Id)
1997 is
1998 Loc : constant Source_Ptr := Sloc (Expr);
1999 I : Interp_Index;
2000 It : Interp;
2001
2002 begin
2003 -- An entity of a type with a reference aspect is overloaded with
2004 -- both interpretations: with and without the dereference. Now that
2005 -- the dereference is made explicit, set the type of the node properly,
2006 -- to prevent anomalies in the backend. Same if the expression is an
2007 -- overloaded function call whose return type has a reference aspect.
2008
2009 if Is_Entity_Name (Expr) then
2010 Set_Etype (Expr, Etype (Entity (Expr)));
2011
2012 -- The designated entity will not be examined again when resolving
2013 -- the dereference, so generate a reference to it now.
2014
2015 Generate_Reference (Entity (Expr), Expr);
2016
2017 elsif Nkind (Expr) = N_Function_Call then
2018
2019 -- If the name of the indexing function is overloaded, locate the one
2020 -- whose return type has an implicit dereference on the desired
2021 -- discriminant, and set entity and type of function call.
2022
2023 if Is_Overloaded (Name (Expr)) then
2024 Get_First_Interp (Name (Expr), I, It);
2025
2026 while Present (It.Nam) loop
2027 if Ekind ((It.Typ)) = E_Record_Type
2028 and then First_Entity ((It.Typ)) = Disc
2029 then
2030 Set_Entity (Name (Expr), It.Nam);
2031 Set_Etype (Name (Expr), Etype (It.Nam));
2032 exit;
2033 end if;
2034
2035 Get_Next_Interp (I, It);
2036 end loop;
2037 end if;
2038
2039 -- Set type of call from resolved function name.
2040
2041 Set_Etype (Expr, Etype (Name (Expr)));
2042 end if;
2043
2044 Set_Is_Overloaded (Expr, False);
2045
2046 -- The expression will often be a generalized indexing that yields a
2047 -- container element that is then dereferenced, in which case the
2048 -- generalized indexing call is also non-overloaded.
2049
2050 if Nkind (Expr) = N_Indexed_Component
2051 and then Present (Generalized_Indexing (Expr))
2052 then
2053 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
2054 end if;
2055
2056 Rewrite (Expr,
2057 Make_Explicit_Dereference (Loc,
2058 Prefix =>
2059 Make_Selected_Component (Loc,
2060 Prefix => Relocate_Node (Expr),
2061 Selector_Name => New_Occurrence_Of (Disc, Loc))));
2062 Set_Etype (Prefix (Expr), Etype (Disc));
2063 Set_Etype (Expr, Designated_Type (Etype (Disc)));
2064 end Build_Explicit_Dereference;
2065
2066 ---------------------------
2067 -- Build_Overriding_Spec --
2068 ---------------------------
2069
2070 function Build_Overriding_Spec
2071 (Op : Entity_Id;
2072 Typ : Entity_Id) return Node_Id
2073 is
2074 Loc : constant Source_Ptr := Sloc (Typ);
2075 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Op);
2076 Spec : constant Node_Id := Specification (Unit_Declaration_Node (Op));
2077
2078 Formal_Spec : Node_Id;
2079 Formal_Type : Node_Id;
2080 New_Spec : Node_Id;
2081
2082 begin
2083 New_Spec := Copy_Subprogram_Spec (Spec);
2084
2085 Formal_Spec := First (Parameter_Specifications (New_Spec));
2086 while Present (Formal_Spec) loop
2087 Formal_Type := Parameter_Type (Formal_Spec);
2088
2089 if Is_Entity_Name (Formal_Type)
2090 and then Entity (Formal_Type) = Par_Typ
2091 then
2092 Rewrite (Formal_Type, New_Occurrence_Of (Typ, Loc));
2093 end if;
2094
2095 -- Nothing needs to be done for access parameters
2096
2097 Next (Formal_Spec);
2098 end loop;
2099
2100 return New_Spec;
2101 end Build_Overriding_Spec;
2102
2103 -----------------------------------
2104 -- Cannot_Raise_Constraint_Error --
2105 -----------------------------------
2106
2107 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
2108 begin
2109 if Compile_Time_Known_Value (Expr) then
2110 return True;
2111
2112 elsif Do_Range_Check (Expr) then
2113 return False;
2114
2115 elsif Raises_Constraint_Error (Expr) then
2116 return False;
2117
2118 else
2119 case Nkind (Expr) is
2120 when N_Identifier =>
2121 return True;
2122
2123 when N_Expanded_Name =>
2124 return True;
2125
2126 when N_Selected_Component =>
2127 return not Do_Discriminant_Check (Expr);
2128
2129 when N_Attribute_Reference =>
2130 if Do_Overflow_Check (Expr) then
2131 return False;
2132
2133 elsif No (Expressions (Expr)) then
2134 return True;
2135
2136 else
2137 declare
2138 N : Node_Id;
2139
2140 begin
2141 N := First (Expressions (Expr));
2142 while Present (N) loop
2143 if Cannot_Raise_Constraint_Error (N) then
2144 Next (N);
2145 else
2146 return False;
2147 end if;
2148 end loop;
2149
2150 return True;
2151 end;
2152 end if;
2153
2154 when N_Type_Conversion =>
2155 if Do_Overflow_Check (Expr)
2156 or else Do_Length_Check (Expr)
2157 or else Do_Tag_Check (Expr)
2158 then
2159 return False;
2160 else
2161 return Cannot_Raise_Constraint_Error (Expression (Expr));
2162 end if;
2163
2164 when N_Unchecked_Type_Conversion =>
2165 return Cannot_Raise_Constraint_Error (Expression (Expr));
2166
2167 when N_Unary_Op =>
2168 if Do_Overflow_Check (Expr) then
2169 return False;
2170 else
2171 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2172 end if;
2173
2174 when N_Op_Divide
2175 | N_Op_Mod
2176 | N_Op_Rem
2177 =>
2178 if Do_Division_Check (Expr)
2179 or else
2180 Do_Overflow_Check (Expr)
2181 then
2182 return False;
2183 else
2184 return
2185 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
2186 and then
2187 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2188 end if;
2189
2190 when N_Op_Add
2191 | N_Op_And
2192 | N_Op_Concat
2193 | N_Op_Eq
2194 | N_Op_Expon
2195 | N_Op_Ge
2196 | N_Op_Gt
2197 | N_Op_Le
2198 | N_Op_Lt
2199 | N_Op_Multiply
2200 | N_Op_Ne
2201 | N_Op_Or
2202 | N_Op_Rotate_Left
2203 | N_Op_Rotate_Right
2204 | N_Op_Shift_Left
2205 | N_Op_Shift_Right
2206 | N_Op_Shift_Right_Arithmetic
2207 | N_Op_Subtract
2208 | N_Op_Xor
2209 =>
2210 if Do_Overflow_Check (Expr) then
2211 return False;
2212 else
2213 return
2214 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
2215 and then
2216 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2217 end if;
2218
2219 when others =>
2220 return False;
2221 end case;
2222 end if;
2223 end Cannot_Raise_Constraint_Error;
2224
2225 -----------------------------------------
2226 -- Check_Dynamically_Tagged_Expression --
2227 -----------------------------------------
2228
2229 procedure Check_Dynamically_Tagged_Expression
2230 (Expr : Node_Id;
2231 Typ : Entity_Id;
2232 Related_Nod : Node_Id)
2233 is
2234 begin
2235 pragma Assert (Is_Tagged_Type (Typ));
2236
2237 -- In order to avoid spurious errors when analyzing the expanded code,
2238 -- this check is done only for nodes that come from source and for
2239 -- actuals of generic instantiations.
2240
2241 if (Comes_From_Source (Related_Nod)
2242 or else In_Generic_Actual (Expr))
2243 and then (Is_Class_Wide_Type (Etype (Expr))
2244 or else Is_Dynamically_Tagged (Expr))
2245 and then not Is_Class_Wide_Type (Typ)
2246 then
2247 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
2248 end if;
2249 end Check_Dynamically_Tagged_Expression;
2250
2251 --------------------------
2252 -- Check_Fully_Declared --
2253 --------------------------
2254
2255 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2256 begin
2257 if Ekind (T) = E_Incomplete_Type then
2258
2259 -- Ada 2005 (AI-50217): If the type is available through a limited
2260 -- with_clause, verify that its full view has been analyzed.
2261
2262 if From_Limited_With (T)
2263 and then Present (Non_Limited_View (T))
2264 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2265 then
2266 -- The non-limited view is fully declared
2267
2268 null;
2269
2270 else
2271 Error_Msg_NE
2272 ("premature usage of incomplete}", N, First_Subtype (T));
2273 end if;
2274
2275 -- Need comments for these tests ???
2276
2277 elsif Has_Private_Component (T)
2278 and then not Is_Generic_Type (Root_Type (T))
2279 and then not In_Spec_Expression
2280 then
2281 -- Special case: if T is the anonymous type created for a single
2282 -- task or protected object, use the name of the source object.
2283
2284 if Is_Concurrent_Type (T)
2285 and then not Comes_From_Source (T)
2286 and then Nkind (N) = N_Object_Declaration
2287 then
2288 Error_Msg_NE
2289 ("type of& has incomplete component",
2290 N, Defining_Identifier (N));
2291 else
2292 Error_Msg_NE
2293 ("premature usage of incomplete}",
2294 N, First_Subtype (T));
2295 end if;
2296 end if;
2297 end Check_Fully_Declared;
2298
2299 -------------------------------------------
2300 -- Check_Function_With_Address_Parameter --
2301 -------------------------------------------
2302
2303 procedure Check_Function_With_Address_Parameter (Subp_Id : Entity_Id) is
2304 F : Entity_Id;
2305 T : Entity_Id;
2306
2307 begin
2308 F := First_Formal (Subp_Id);
2309 while Present (F) loop
2310 T := Etype (F);
2311
2312 if Is_Private_Type (T) and then Present (Full_View (T)) then
2313 T := Full_View (T);
2314 end if;
2315
2316 if Is_Descendant_Of_Address (T) or else Is_Limited_Type (T) then
2317 Set_Is_Pure (Subp_Id, False);
2318 exit;
2319 end if;
2320
2321 Next_Formal (F);
2322 end loop;
2323 end Check_Function_With_Address_Parameter;
2324
2325 -------------------------------------
2326 -- Check_Function_Writable_Actuals --
2327 -------------------------------------
2328
2329 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2330 Writable_Actuals_List : Elist_Id := No_Elist;
2331 Identifiers_List : Elist_Id := No_Elist;
2332 Aggr_Error_Node : Node_Id := Empty;
2333 Error_Node : Node_Id := Empty;
2334
2335 procedure Collect_Identifiers (N : Node_Id);
2336 -- In a single traversal of subtree N collect in Writable_Actuals_List
2337 -- all the actuals of functions with writable actuals, and in the list
2338 -- Identifiers_List collect all the identifiers that are not actuals of
2339 -- functions with writable actuals. If a writable actual is referenced
2340 -- twice as writable actual then Error_Node is set to reference its
2341 -- second occurrence, the error is reported, and the tree traversal
2342 -- is abandoned.
2343
2344 procedure Preanalyze_Without_Errors (N : Node_Id);
2345 -- Preanalyze N without reporting errors. Very dubious, you can't just
2346 -- go analyzing things more than once???
2347
2348 -------------------------
2349 -- Collect_Identifiers --
2350 -------------------------
2351
2352 procedure Collect_Identifiers (N : Node_Id) is
2353
2354 function Check_Node (N : Node_Id) return Traverse_Result;
2355 -- Process a single node during the tree traversal to collect the
2356 -- writable actuals of functions and all the identifiers which are
2357 -- not writable actuals of functions.
2358
2359 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2360 -- Returns True if List has a node whose Entity is Entity (N)
2361
2362 ----------------
2363 -- Check_Node --
2364 ----------------
2365
2366 function Check_Node (N : Node_Id) return Traverse_Result is
2367 Is_Writable_Actual : Boolean := False;
2368 Id : Entity_Id;
2369
2370 begin
2371 if Nkind (N) = N_Identifier then
2372
2373 -- No analysis possible if the entity is not decorated
2374
2375 if No (Entity (N)) then
2376 return Skip;
2377
2378 -- Don't collect identifiers of packages, called functions, etc
2379
2380 elsif Ekind_In (Entity (N), E_Package,
2381 E_Function,
2382 E_Procedure,
2383 E_Entry)
2384 then
2385 return Skip;
2386
2387 -- For rewritten nodes, continue the traversal in the original
2388 -- subtree. Needed to handle aggregates in original expressions
2389 -- extracted from the tree by Remove_Side_Effects.
2390
2391 elsif Is_Rewrite_Substitution (N) then
2392 Collect_Identifiers (Original_Node (N));
2393 return Skip;
2394
2395 -- For now we skip aggregate discriminants, since they require
2396 -- performing the analysis in two phases to identify conflicts:
2397 -- first one analyzing discriminants and second one analyzing
2398 -- the rest of components (since at run time, discriminants are
2399 -- evaluated prior to components): too much computation cost
2400 -- to identify a corner case???
2401
2402 elsif Nkind (Parent (N)) = N_Component_Association
2403 and then Nkind_In (Parent (Parent (N)),
2404 N_Aggregate,
2405 N_Extension_Aggregate)
2406 then
2407 declare
2408 Choice : constant Node_Id := First (Choices (Parent (N)));
2409
2410 begin
2411 if Ekind (Entity (N)) = E_Discriminant then
2412 return Skip;
2413
2414 elsif Expression (Parent (N)) = N
2415 and then Nkind (Choice) = N_Identifier
2416 and then Ekind (Entity (Choice)) = E_Discriminant
2417 then
2418 return Skip;
2419 end if;
2420 end;
2421
2422 -- Analyze if N is a writable actual of a function
2423
2424 elsif Nkind (Parent (N)) = N_Function_Call then
2425 declare
2426 Call : constant Node_Id := Parent (N);
2427 Actual : Node_Id;
2428 Formal : Node_Id;
2429
2430 begin
2431 Id := Get_Called_Entity (Call);
2432
2433 -- In case of previous error, no check is possible
2434
2435 if No (Id) then
2436 return Abandon;
2437 end if;
2438
2439 if Ekind_In (Id, E_Function, E_Generic_Function)
2440 and then Has_Out_Or_In_Out_Parameter (Id)
2441 then
2442 Formal := First_Formal (Id);
2443 Actual := First_Actual (Call);
2444 while Present (Actual) and then Present (Formal) loop
2445 if Actual = N then
2446 if Ekind_In (Formal, E_Out_Parameter,
2447 E_In_Out_Parameter)
2448 then
2449 Is_Writable_Actual := True;
2450 end if;
2451
2452 exit;
2453 end if;
2454
2455 Next_Formal (Formal);
2456 Next_Actual (Actual);
2457 end loop;
2458 end if;
2459 end;
2460 end if;
2461
2462 if Is_Writable_Actual then
2463
2464 -- Skip checking the error in non-elementary types since
2465 -- RM 6.4.1(6.15/3) is restricted to elementary types, but
2466 -- store this actual in Writable_Actuals_List since it is
2467 -- needed to perform checks on other constructs that have
2468 -- arbitrary order of evaluation (for example, aggregates).
2469
2470 if not Is_Elementary_Type (Etype (N)) then
2471 if not Contains (Writable_Actuals_List, N) then
2472 Append_New_Elmt (N, To => Writable_Actuals_List);
2473 end if;
2474
2475 -- Second occurrence of an elementary type writable actual
2476
2477 elsif Contains (Writable_Actuals_List, N) then
2478
2479 -- Report the error on the second occurrence of the
2480 -- identifier. We cannot assume that N is the second
2481 -- occurrence (according to their location in the
2482 -- sources), since Traverse_Func walks through Field2
2483 -- last (see comment in the body of Traverse_Func).
2484
2485 declare
2486 Elmt : Elmt_Id;
2487
2488 begin
2489 Elmt := First_Elmt (Writable_Actuals_List);
2490 while Present (Elmt)
2491 and then Entity (Node (Elmt)) /= Entity (N)
2492 loop
2493 Next_Elmt (Elmt);
2494 end loop;
2495
2496 if Sloc (N) > Sloc (Node (Elmt)) then
2497 Error_Node := N;
2498 else
2499 Error_Node := Node (Elmt);
2500 end if;
2501
2502 Error_Msg_NE
2503 ("value may be affected by call to & "
2504 & "because order of evaluation is arbitrary",
2505 Error_Node, Id);
2506 return Abandon;
2507 end;
2508
2509 -- First occurrence of a elementary type writable actual
2510
2511 else
2512 Append_New_Elmt (N, To => Writable_Actuals_List);
2513 end if;
2514
2515 else
2516 if Identifiers_List = No_Elist then
2517 Identifiers_List := New_Elmt_List;
2518 end if;
2519
2520 Append_Unique_Elmt (N, Identifiers_List);
2521 end if;
2522 end if;
2523
2524 return OK;
2525 end Check_Node;
2526
2527 --------------
2528 -- Contains --
2529 --------------
2530
2531 function Contains
2532 (List : Elist_Id;
2533 N : Node_Id) return Boolean
2534 is
2535 pragma Assert (Nkind (N) in N_Has_Entity);
2536
2537 Elmt : Elmt_Id;
2538
2539 begin
2540 if List = No_Elist then
2541 return False;
2542 end if;
2543
2544 Elmt := First_Elmt (List);
2545 while Present (Elmt) loop
2546 if Entity (Node (Elmt)) = Entity (N) then
2547 return True;
2548 else
2549 Next_Elmt (Elmt);
2550 end if;
2551 end loop;
2552
2553 return False;
2554 end Contains;
2555
2556 ------------------
2557 -- Do_Traversal --
2558 ------------------
2559
2560 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2561 -- The traversal procedure
2562
2563 -- Start of processing for Collect_Identifiers
2564
2565 begin
2566 if Present (Error_Node) then
2567 return;
2568 end if;
2569
2570 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2571 return;
2572 end if;
2573
2574 Do_Traversal (N);
2575 end Collect_Identifiers;
2576
2577 -------------------------------
2578 -- Preanalyze_Without_Errors --
2579 -------------------------------
2580
2581 procedure Preanalyze_Without_Errors (N : Node_Id) is
2582 Status : constant Boolean := Get_Ignore_Errors;
2583 begin
2584 Set_Ignore_Errors (True);
2585 Preanalyze (N);
2586 Set_Ignore_Errors (Status);
2587 end Preanalyze_Without_Errors;
2588
2589 -- Start of processing for Check_Function_Writable_Actuals
2590
2591 begin
2592 -- The check only applies to Ada 2012 code on which Check_Actuals has
2593 -- been set, and only to constructs that have multiple constituents
2594 -- whose order of evaluation is not specified by the language.
2595
2596 if Ada_Version < Ada_2012
2597 or else not Check_Actuals (N)
2598 or else (not (Nkind (N) in N_Op)
2599 and then not (Nkind (N) in N_Membership_Test)
2600 and then not Nkind_In (N, N_Range,
2601 N_Aggregate,
2602 N_Extension_Aggregate,
2603 N_Full_Type_Declaration,
2604 N_Function_Call,
2605 N_Procedure_Call_Statement,
2606 N_Entry_Call_Statement))
2607 or else (Nkind (N) = N_Full_Type_Declaration
2608 and then not Is_Record_Type (Defining_Identifier (N)))
2609
2610 -- In addition, this check only applies to source code, not to code
2611 -- generated by constraint checks.
2612
2613 or else not Comes_From_Source (N)
2614 then
2615 return;
2616 end if;
2617
2618 -- If a construct C has two or more direct constituents that are names
2619 -- or expressions whose evaluation may occur in an arbitrary order, at
2620 -- least one of which contains a function call with an in out or out
2621 -- parameter, then the construct is legal only if: for each name N that
2622 -- is passed as a parameter of mode in out or out to some inner function
2623 -- call C2 (not including the construct C itself), there is no other
2624 -- name anywhere within a direct constituent of the construct C other
2625 -- than the one containing C2, that is known to refer to the same
2626 -- object (RM 6.4.1(6.17/3)).
2627
2628 case Nkind (N) is
2629 when N_Range =>
2630 Collect_Identifiers (Low_Bound (N));
2631 Collect_Identifiers (High_Bound (N));
2632
2633 when N_Membership_Test
2634 | N_Op
2635 =>
2636 declare
2637 Expr : Node_Id;
2638
2639 begin
2640 Collect_Identifiers (Left_Opnd (N));
2641
2642 if Present (Right_Opnd (N)) then
2643 Collect_Identifiers (Right_Opnd (N));
2644 end if;
2645
2646 if Nkind_In (N, N_In, N_Not_In)
2647 and then Present (Alternatives (N))
2648 then
2649 Expr := First (Alternatives (N));
2650 while Present (Expr) loop
2651 Collect_Identifiers (Expr);
2652
2653 Next (Expr);
2654 end loop;
2655 end if;
2656 end;
2657
2658 when N_Full_Type_Declaration =>
2659 declare
2660 function Get_Record_Part (N : Node_Id) return Node_Id;
2661 -- Return the record part of this record type definition
2662
2663 function Get_Record_Part (N : Node_Id) return Node_Id is
2664 Type_Def : constant Node_Id := Type_Definition (N);
2665 begin
2666 if Nkind (Type_Def) = N_Derived_Type_Definition then
2667 return Record_Extension_Part (Type_Def);
2668 else
2669 return Type_Def;
2670 end if;
2671 end Get_Record_Part;
2672
2673 Comp : Node_Id;
2674 Def_Id : Entity_Id := Defining_Identifier (N);
2675 Rec : Node_Id := Get_Record_Part (N);
2676
2677 begin
2678 -- No need to perform any analysis if the record has no
2679 -- components
2680
2681 if No (Rec) or else No (Component_List (Rec)) then
2682 return;
2683 end if;
2684
2685 -- Collect the identifiers starting from the deepest
2686 -- derivation. Done to report the error in the deepest
2687 -- derivation.
2688
2689 loop
2690 if Present (Component_List (Rec)) then
2691 Comp := First (Component_Items (Component_List (Rec)));
2692 while Present (Comp) loop
2693 if Nkind (Comp) = N_Component_Declaration
2694 and then Present (Expression (Comp))
2695 then
2696 Collect_Identifiers (Expression (Comp));
2697 end if;
2698
2699 Next (Comp);
2700 end loop;
2701 end if;
2702
2703 exit when No (Underlying_Type (Etype (Def_Id)))
2704 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2705 = Def_Id;
2706
2707 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2708 Rec := Get_Record_Part (Parent (Def_Id));
2709 end loop;
2710 end;
2711
2712 when N_Entry_Call_Statement
2713 | N_Subprogram_Call
2714 =>
2715 declare
2716 Id : constant Entity_Id := Get_Called_Entity (N);
2717 Formal : Node_Id;
2718 Actual : Node_Id;
2719
2720 begin
2721 Formal := First_Formal (Id);
2722 Actual := First_Actual (N);
2723 while Present (Actual) and then Present (Formal) loop
2724 if Ekind_In (Formal, E_Out_Parameter,
2725 E_In_Out_Parameter)
2726 then
2727 Collect_Identifiers (Actual);
2728 end if;
2729
2730 Next_Formal (Formal);
2731 Next_Actual (Actual);
2732 end loop;
2733 end;
2734
2735 when N_Aggregate
2736 | N_Extension_Aggregate
2737 =>
2738 declare
2739 Assoc : Node_Id;
2740 Choice : Node_Id;
2741 Comp_Expr : Node_Id;
2742
2743 begin
2744 -- Handle the N_Others_Choice of array aggregates with static
2745 -- bounds. There is no need to perform this analysis in
2746 -- aggregates without static bounds since we cannot evaluate
2747 -- if the N_Others_Choice covers several elements. There is
2748 -- no need to handle the N_Others choice of record aggregates
2749 -- since at this stage it has been already expanded by
2750 -- Resolve_Record_Aggregate.
2751
2752 if Is_Array_Type (Etype (N))
2753 and then Nkind (N) = N_Aggregate
2754 and then Present (Aggregate_Bounds (N))
2755 and then Compile_Time_Known_Bounds (Etype (N))
2756 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2757 >
2758 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2759 then
2760 declare
2761 Count_Components : Uint := Uint_0;
2762 Num_Components : Uint;
2763 Others_Assoc : Node_Id := Empty;
2764 Others_Choice : Node_Id := Empty;
2765 Others_Box_Present : Boolean := False;
2766
2767 begin
2768 -- Count positional associations
2769
2770 if Present (Expressions (N)) then
2771 Comp_Expr := First (Expressions (N));
2772 while Present (Comp_Expr) loop
2773 Count_Components := Count_Components + 1;
2774 Next (Comp_Expr);
2775 end loop;
2776 end if;
2777
2778 -- Count the rest of elements and locate the N_Others
2779 -- choice (if any)
2780
2781 Assoc := First (Component_Associations (N));
2782 while Present (Assoc) loop
2783 Choice := First (Choices (Assoc));
2784 while Present (Choice) loop
2785 if Nkind (Choice) = N_Others_Choice then
2786 Others_Assoc := Assoc;
2787 Others_Choice := Choice;
2788 Others_Box_Present := Box_Present (Assoc);
2789
2790 -- Count several components
2791
2792 elsif Nkind_In (Choice, N_Range,
2793 N_Subtype_Indication)
2794 or else (Is_Entity_Name (Choice)
2795 and then Is_Type (Entity (Choice)))
2796 then
2797 declare
2798 L, H : Node_Id;
2799 begin
2800 Get_Index_Bounds (Choice, L, H);
2801 pragma Assert
2802 (Compile_Time_Known_Value (L)
2803 and then Compile_Time_Known_Value (H));
2804 Count_Components :=
2805 Count_Components
2806 + Expr_Value (H) - Expr_Value (L) + 1;
2807 end;
2808
2809 -- Count single component. No other case available
2810 -- since we are handling an aggregate with static
2811 -- bounds.
2812
2813 else
2814 pragma Assert (Is_OK_Static_Expression (Choice)
2815 or else Nkind (Choice) = N_Identifier
2816 or else Nkind (Choice) = N_Integer_Literal);
2817
2818 Count_Components := Count_Components + 1;
2819 end if;
2820
2821 Next (Choice);
2822 end loop;
2823
2824 Next (Assoc);
2825 end loop;
2826
2827 Num_Components :=
2828 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2829 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2830
2831 pragma Assert (Count_Components <= Num_Components);
2832
2833 -- Handle the N_Others choice if it covers several
2834 -- components
2835
2836 if Present (Others_Choice)
2837 and then (Num_Components - Count_Components) > 1
2838 then
2839 if not Others_Box_Present then
2840
2841 -- At this stage, if expansion is active, the
2842 -- expression of the others choice has not been
2843 -- analyzed. Hence we generate a duplicate and
2844 -- we analyze it silently to have available the
2845 -- minimum decoration required to collect the
2846 -- identifiers.
2847
2848 pragma Assert (Present (Others_Assoc));
2849
2850 if not Expander_Active then
2851 Comp_Expr := Expression (Others_Assoc);
2852 else
2853 Comp_Expr :=
2854 New_Copy_Tree (Expression (Others_Assoc));
2855 Preanalyze_Without_Errors (Comp_Expr);
2856 end if;
2857
2858 Collect_Identifiers (Comp_Expr);
2859
2860 if Writable_Actuals_List /= No_Elist then
2861
2862 -- As suggested by Robert, at current stage we
2863 -- report occurrences of this case as warnings.
2864
2865 Error_Msg_N
2866 ("writable function parameter may affect "
2867 & "value in other component because order "
2868 & "of evaluation is unspecified??",
2869 Node (First_Elmt (Writable_Actuals_List)));
2870 end if;
2871 end if;
2872 end if;
2873 end;
2874
2875 -- For an array aggregate, a discrete_choice_list that has
2876 -- a nonstatic range is considered as two or more separate
2877 -- occurrences of the expression (RM 6.4.1(20/3)).
2878
2879 elsif Is_Array_Type (Etype (N))
2880 and then Nkind (N) = N_Aggregate
2881 and then Present (Aggregate_Bounds (N))
2882 and then not Compile_Time_Known_Bounds (Etype (N))
2883 then
2884 -- Collect identifiers found in the dynamic bounds
2885
2886 declare
2887 Count_Components : Natural := 0;
2888 Low, High : Node_Id;
2889
2890 begin
2891 Assoc := First (Component_Associations (N));
2892 while Present (Assoc) loop
2893 Choice := First (Choices (Assoc));
2894 while Present (Choice) loop
2895 if Nkind_In (Choice, N_Range,
2896 N_Subtype_Indication)
2897 or else (Is_Entity_Name (Choice)
2898 and then Is_Type (Entity (Choice)))
2899 then
2900 Get_Index_Bounds (Choice, Low, High);
2901
2902 if not Compile_Time_Known_Value (Low) then
2903 Collect_Identifiers (Low);
2904
2905 if No (Aggr_Error_Node) then
2906 Aggr_Error_Node := Low;
2907 end if;
2908 end if;
2909
2910 if not Compile_Time_Known_Value (High) then
2911 Collect_Identifiers (High);
2912
2913 if No (Aggr_Error_Node) then
2914 Aggr_Error_Node := High;
2915 end if;
2916 end if;
2917
2918 -- The RM rule is violated if there is more than
2919 -- a single choice in a component association.
2920
2921 else
2922 Count_Components := Count_Components + 1;
2923
2924 if No (Aggr_Error_Node)
2925 and then Count_Components > 1
2926 then
2927 Aggr_Error_Node := Choice;
2928 end if;
2929
2930 if not Compile_Time_Known_Value (Choice) then
2931 Collect_Identifiers (Choice);
2932 end if;
2933 end if;
2934
2935 Next (Choice);
2936 end loop;
2937
2938 Next (Assoc);
2939 end loop;
2940 end;
2941 end if;
2942
2943 -- Handle ancestor part of extension aggregates
2944
2945 if Nkind (N) = N_Extension_Aggregate then
2946 Collect_Identifiers (Ancestor_Part (N));
2947 end if;
2948
2949 -- Handle positional associations
2950
2951 if Present (Expressions (N)) then
2952 Comp_Expr := First (Expressions (N));
2953 while Present (Comp_Expr) loop
2954 if not Is_OK_Static_Expression (Comp_Expr) then
2955 Collect_Identifiers (Comp_Expr);
2956 end if;
2957
2958 Next (Comp_Expr);
2959 end loop;
2960 end if;
2961
2962 -- Handle discrete associations
2963
2964 if Present (Component_Associations (N)) then
2965 Assoc := First (Component_Associations (N));
2966 while Present (Assoc) loop
2967
2968 if not Box_Present (Assoc) then
2969 Choice := First (Choices (Assoc));
2970 while Present (Choice) loop
2971
2972 -- For now we skip discriminants since it requires
2973 -- performing the analysis in two phases: first one
2974 -- analyzing discriminants and second one analyzing
2975 -- the rest of components since discriminants are
2976 -- evaluated prior to components: too much extra
2977 -- work to detect a corner case???
2978
2979 if Nkind (Choice) in N_Has_Entity
2980 and then Present (Entity (Choice))
2981 and then Ekind (Entity (Choice)) = E_Discriminant
2982 then
2983 null;
2984
2985 elsif Box_Present (Assoc) then
2986 null;
2987
2988 else
2989 if not Analyzed (Expression (Assoc)) then
2990 Comp_Expr :=
2991 New_Copy_Tree (Expression (Assoc));
2992 Set_Parent (Comp_Expr, Parent (N));
2993 Preanalyze_Without_Errors (Comp_Expr);
2994 else
2995 Comp_Expr := Expression (Assoc);
2996 end if;
2997
2998 Collect_Identifiers (Comp_Expr);
2999 end if;
3000
3001 Next (Choice);
3002 end loop;
3003 end if;
3004
3005 Next (Assoc);
3006 end loop;
3007 end if;
3008 end;
3009
3010 when others =>
3011 return;
3012 end case;
3013
3014 -- No further action needed if we already reported an error
3015
3016 if Present (Error_Node) then
3017 return;
3018 end if;
3019
3020 -- Check violation of RM 6.20/3 in aggregates
3021
3022 if Present (Aggr_Error_Node)
3023 and then Writable_Actuals_List /= No_Elist
3024 then
3025 Error_Msg_N
3026 ("value may be affected by call in other component because they "
3027 & "are evaluated in unspecified order",
3028 Node (First_Elmt (Writable_Actuals_List)));
3029 return;
3030 end if;
3031
3032 -- Check if some writable argument of a function is referenced
3033
3034 if Writable_Actuals_List /= No_Elist
3035 and then Identifiers_List /= No_Elist
3036 then
3037 declare
3038 Elmt_1 : Elmt_Id;
3039 Elmt_2 : Elmt_Id;
3040
3041 begin
3042 Elmt_1 := First_Elmt (Writable_Actuals_List);
3043 while Present (Elmt_1) loop
3044 Elmt_2 := First_Elmt (Identifiers_List);
3045 while Present (Elmt_2) loop
3046 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
3047 case Nkind (Parent (Node (Elmt_2))) is
3048 when N_Aggregate
3049 | N_Component_Association
3050 | N_Component_Declaration
3051 =>
3052 Error_Msg_N
3053 ("value may be affected by call in other "
3054 & "component because they are evaluated "
3055 & "in unspecified order",
3056 Node (Elmt_2));
3057
3058 when N_In
3059 | N_Not_In
3060 =>
3061 Error_Msg_N
3062 ("value may be affected by call in other "
3063 & "alternative because they are evaluated "
3064 & "in unspecified order",
3065 Node (Elmt_2));
3066
3067 when others =>
3068 Error_Msg_N
3069 ("value of actual may be affected by call in "
3070 & "other actual because they are evaluated "
3071 & "in unspecified order",
3072 Node (Elmt_2));
3073 end case;
3074 end if;
3075
3076 Next_Elmt (Elmt_2);
3077 end loop;
3078
3079 Next_Elmt (Elmt_1);
3080 end loop;
3081 end;
3082 end if;
3083 end Check_Function_Writable_Actuals;
3084
3085 --------------------------------
3086 -- Check_Implicit_Dereference --
3087 --------------------------------
3088
3089 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
3090 Disc : Entity_Id;
3091 Desig : Entity_Id;
3092 Nam : Node_Id;
3093
3094 begin
3095 if Nkind (N) = N_Indexed_Component
3096 and then Present (Generalized_Indexing (N))
3097 then
3098 Nam := Generalized_Indexing (N);
3099 else
3100 Nam := N;
3101 end if;
3102
3103 if Ada_Version < Ada_2012
3104 or else not Has_Implicit_Dereference (Base_Type (Typ))
3105 then
3106 return;
3107
3108 elsif not Comes_From_Source (N)
3109 and then Nkind (N) /= N_Indexed_Component
3110 then
3111 return;
3112
3113 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
3114 null;
3115
3116 else
3117 Disc := First_Discriminant (Typ);
3118 while Present (Disc) loop
3119 if Has_Implicit_Dereference (Disc) then
3120 Desig := Designated_Type (Etype (Disc));
3121 Add_One_Interp (Nam, Disc, Desig);
3122
3123 -- If the node is a generalized indexing, add interpretation
3124 -- to that node as well, for subsequent resolution.
3125
3126 if Nkind (N) = N_Indexed_Component then
3127 Add_One_Interp (N, Disc, Desig);
3128 end if;
3129
3130 -- If the operation comes from a generic unit and the context
3131 -- is a selected component, the selector name may be global
3132 -- and set in the instance already. Remove the entity to
3133 -- force resolution of the selected component, and the
3134 -- generation of an explicit dereference if needed.
3135
3136 if In_Instance
3137 and then Nkind (Parent (Nam)) = N_Selected_Component
3138 then
3139 Set_Entity (Selector_Name (Parent (Nam)), Empty);
3140 end if;
3141
3142 exit;
3143 end if;
3144
3145 Next_Discriminant (Disc);
3146 end loop;
3147 end if;
3148 end Check_Implicit_Dereference;
3149
3150 ----------------------------------
3151 -- Check_Internal_Protected_Use --
3152 ----------------------------------
3153
3154 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
3155 S : Entity_Id;
3156 Prot : Entity_Id;
3157
3158 begin
3159 Prot := Empty;
3160
3161 S := Current_Scope;
3162 while Present (S) loop
3163 if S = Standard_Standard then
3164 exit;
3165
3166 elsif Ekind (S) = E_Function
3167 and then Ekind (Scope (S)) = E_Protected_Type
3168 then
3169 Prot := Scope (S);
3170 exit;
3171 end if;
3172
3173 S := Scope (S);
3174 end loop;
3175
3176 if Present (Prot)
3177 and then Scope (Nam) = Prot
3178 and then Ekind (Nam) /= E_Function
3179 then
3180 -- An indirect function call (e.g. a callback within a protected
3181 -- function body) is not statically illegal. If the access type is
3182 -- anonymous and is the type of an access parameter, the scope of Nam
3183 -- will be the protected type, but it is not a protected operation.
3184
3185 if Ekind (Nam) = E_Subprogram_Type
3186 and then Nkind (Associated_Node_For_Itype (Nam)) =
3187 N_Function_Specification
3188 then
3189 null;
3190
3191 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
3192 Error_Msg_N
3193 ("within protected function cannot use protected procedure in "
3194 & "renaming or as generic actual", N);
3195
3196 elsif Nkind (N) = N_Attribute_Reference then
3197 Error_Msg_N
3198 ("within protected function cannot take access of protected "
3199 & "procedure", N);
3200
3201 else
3202 Error_Msg_N
3203 ("within protected function, protected object is constant", N);
3204 Error_Msg_N
3205 ("\cannot call operation that may modify it", N);
3206 end if;
3207 end if;
3208
3209 -- Verify that an internal call does not appear within a precondition
3210 -- of a protected operation. This implements AI12-0166.
3211 -- The precondition aspect has been rewritten as a pragma Precondition
3212 -- and we check whether the scope of the called subprogram is the same
3213 -- as that of the entity to which the aspect applies.
3214
3215 if Convention (Nam) = Convention_Protected then
3216 declare
3217 P : Node_Id;
3218
3219 begin
3220 P := Parent (N);
3221 while Present (P) loop
3222 if Nkind (P) = N_Pragma
3223 and then Chars (Pragma_Identifier (P)) = Name_Precondition
3224 and then From_Aspect_Specification (P)
3225 and then
3226 Scope (Entity (Corresponding_Aspect (P))) = Scope (Nam)
3227 then
3228 Error_Msg_N
3229 ("internal call cannot appear in precondition of "
3230 & "protected operation", N);
3231 return;
3232
3233 elsif Nkind (P) = N_Pragma
3234 and then Chars (Pragma_Identifier (P)) = Name_Contract_Cases
3235 then
3236 -- Check whether call is in a case guard. It is legal in a
3237 -- consequence.
3238
3239 P := N;
3240 while Present (P) loop
3241 if Nkind (Parent (P)) = N_Component_Association
3242 and then P /= Expression (Parent (P))
3243 then
3244 Error_Msg_N
3245 ("internal call cannot appear in case guard in a "
3246 & "contract case", N);
3247 end if;
3248
3249 P := Parent (P);
3250 end loop;
3251
3252 return;
3253
3254 elsif Nkind (P) = N_Parameter_Specification
3255 and then Scope (Current_Scope) = Scope (Nam)
3256 and then Nkind_In (Parent (P), N_Entry_Declaration,
3257 N_Subprogram_Declaration)
3258 then
3259 Error_Msg_N
3260 ("internal call cannot appear in default for formal of "
3261 & "protected operation", N);
3262 return;
3263 end if;
3264
3265 P := Parent (P);
3266 end loop;
3267 end;
3268 end if;
3269 end Check_Internal_Protected_Use;
3270
3271 ---------------------------------------
3272 -- Check_Later_Vs_Basic_Declarations --
3273 ---------------------------------------
3274
3275 procedure Check_Later_Vs_Basic_Declarations
3276 (Decls : List_Id;
3277 During_Parsing : Boolean)
3278 is
3279 Body_Sloc : Source_Ptr;
3280 Decl : Node_Id;
3281
3282 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
3283 -- Return whether Decl is considered as a declarative item.
3284 -- When During_Parsing is True, the semantics of Ada 83 is followed.
3285 -- When During_Parsing is False, the semantics of SPARK is followed.
3286
3287 -------------------------------
3288 -- Is_Later_Declarative_Item --
3289 -------------------------------
3290
3291 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
3292 begin
3293 if Nkind (Decl) in N_Later_Decl_Item then
3294 return True;
3295
3296 elsif Nkind (Decl) = N_Pragma then
3297 return True;
3298
3299 elsif During_Parsing then
3300 return False;
3301
3302 -- In SPARK, a package declaration is not considered as a later
3303 -- declarative item.
3304
3305 elsif Nkind (Decl) = N_Package_Declaration then
3306 return False;
3307
3308 -- In SPARK, a renaming is considered as a later declarative item
3309
3310 elsif Nkind (Decl) in N_Renaming_Declaration then
3311 return True;
3312
3313 else
3314 return False;
3315 end if;
3316 end Is_Later_Declarative_Item;
3317
3318 -- Start of processing for Check_Later_Vs_Basic_Declarations
3319
3320 begin
3321 Decl := First (Decls);
3322
3323 -- Loop through sequence of basic declarative items
3324
3325 Outer : while Present (Decl) loop
3326 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
3327 and then Nkind (Decl) not in N_Body_Stub
3328 then
3329 Next (Decl);
3330
3331 -- Once a body is encountered, we only allow later declarative
3332 -- items. The inner loop checks the rest of the list.
3333
3334 else
3335 Body_Sloc := Sloc (Decl);
3336
3337 Inner : while Present (Decl) loop
3338 if not Is_Later_Declarative_Item (Decl) then
3339 if During_Parsing then
3340 if Ada_Version = Ada_83 then
3341 Error_Msg_Sloc := Body_Sloc;
3342 Error_Msg_N
3343 ("(Ada 83) decl cannot appear after body#", Decl);
3344 end if;
3345 else
3346 Error_Msg_Sloc := Body_Sloc;
3347 Check_SPARK_05_Restriction
3348 ("decl cannot appear after body#", Decl);
3349 end if;
3350 end if;
3351
3352 Next (Decl);
3353 end loop Inner;
3354 end if;
3355 end loop Outer;
3356 end Check_Later_Vs_Basic_Declarations;
3357
3358 ---------------------------
3359 -- Check_No_Hidden_State --
3360 ---------------------------
3361
3362 procedure Check_No_Hidden_State (Id : Entity_Id) is
3363 Context : Entity_Id := Empty;
3364 Not_Visible : Boolean := False;
3365 Scop : Entity_Id;
3366
3367 begin
3368 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3369
3370 -- Nothing to do for internally-generated abstract states and variables
3371 -- because they do not represent the hidden state of the source unit.
3372
3373 if not Comes_From_Source (Id) then
3374 return;
3375 end if;
3376
3377 -- Find the proper context where the object or state appears
3378
3379 Scop := Scope (Id);
3380 while Present (Scop) loop
3381 Context := Scop;
3382
3383 -- Keep track of the context's visibility
3384
3385 Not_Visible := Not_Visible or else In_Private_Part (Context);
3386
3387 -- Prevent the search from going too far
3388
3389 if Context = Standard_Standard then
3390 return;
3391
3392 -- Objects and states that appear immediately within a subprogram or
3393 -- entry inside a construct nested within a subprogram do not
3394 -- introduce a hidden state. They behave as local variable
3395 -- declarations. The same is true for elaboration code inside a block
3396 -- or a task.
3397
3398 elsif Is_Subprogram_Or_Entry (Context)
3399 or else Ekind_In (Context, E_Block, E_Task_Type)
3400 then
3401 return;
3402 end if;
3403
3404 -- Stop the traversal when a package subject to a null abstract state
3405 -- has been found.
3406
3407 if Is_Package_Or_Generic_Package (Context)
3408 and then Has_Null_Abstract_State (Context)
3409 then
3410 exit;
3411 end if;
3412
3413 Scop := Scope (Scop);
3414 end loop;
3415
3416 -- At this point we know that there is at least one package with a null
3417 -- abstract state in visibility. Emit an error message unconditionally
3418 -- if the entity being processed is a state because the placement of the
3419 -- related package is irrelevant. This is not the case for objects as
3420 -- the intermediate context matters.
3421
3422 if Present (Context)
3423 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3424 then
3425 Error_Msg_N ("cannot introduce hidden state &", Id);
3426 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3427 end if;
3428 end Check_No_Hidden_State;
3429
3430 ----------------------------------------
3431 -- Check_Nonvolatile_Function_Profile --
3432 ----------------------------------------
3433
3434 procedure Check_Nonvolatile_Function_Profile (Func_Id : Entity_Id) is
3435 Formal : Entity_Id;
3436
3437 begin
3438 -- Inspect all formal parameters
3439
3440 Formal := First_Formal (Func_Id);
3441 while Present (Formal) loop
3442 if Is_Effectively_Volatile (Etype (Formal)) then
3443 Error_Msg_NE
3444 ("nonvolatile function & cannot have a volatile parameter",
3445 Formal, Func_Id);
3446 end if;
3447
3448 Next_Formal (Formal);
3449 end loop;
3450
3451 -- Inspect the return type
3452
3453 if Is_Effectively_Volatile (Etype (Func_Id)) then
3454 Error_Msg_NE
3455 ("nonvolatile function & cannot have a volatile return type",
3456 Result_Definition (Parent (Func_Id)), Func_Id);
3457 end if;
3458 end Check_Nonvolatile_Function_Profile;
3459
3460 -----------------------------
3461 -- Check_Part_Of_Reference --
3462 -----------------------------
3463
3464 procedure Check_Part_Of_Reference (Var_Id : Entity_Id; Ref : Node_Id) is
3465 function Is_Enclosing_Package_Body
3466 (Body_Decl : Node_Id;
3467 Obj_Id : Entity_Id) return Boolean;
3468 pragma Inline (Is_Enclosing_Package_Body);
3469 -- Determine whether package body Body_Decl or its corresponding spec
3470 -- immediately encloses the declaration of object Obj_Id.
3471
3472 function Is_Internal_Declaration_Or_Body
3473 (Decl : Node_Id) return Boolean;
3474 pragma Inline (Is_Internal_Declaration_Or_Body);
3475 -- Determine whether declaration or body denoted by Decl is internal
3476
3477 function Is_Single_Declaration_Or_Body
3478 (Decl : Node_Id;
3479 Conc_Typ : Entity_Id) return Boolean;
3480 pragma Inline (Is_Single_Declaration_Or_Body);
3481 -- Determine whether protected/task declaration or body denoted by Decl
3482 -- belongs to single concurrent type Conc_Typ.
3483
3484 function Is_Single_Task_Pragma
3485 (Prag : Node_Id;
3486 Task_Typ : Entity_Id) return Boolean;
3487 pragma Inline (Is_Single_Task_Pragma);
3488 -- Determine whether pragma Prag belongs to single task type Task_Typ
3489
3490 -------------------------------
3491 -- Is_Enclosing_Package_Body --
3492 -------------------------------
3493
3494 function Is_Enclosing_Package_Body
3495 (Body_Decl : Node_Id;
3496 Obj_Id : Entity_Id) return Boolean
3497 is
3498 Obj_Context : Node_Id;
3499
3500 begin
3501 -- Find the context of the object declaration
3502
3503 Obj_Context := Parent (Declaration_Node (Obj_Id));
3504
3505 if Nkind (Obj_Context) = N_Package_Specification then
3506 Obj_Context := Parent (Obj_Context);
3507 end if;
3508
3509 -- The object appears immediately within the package body
3510
3511 if Obj_Context = Body_Decl then
3512 return True;
3513
3514 -- The object appears immediately within the corresponding spec
3515
3516 elsif Nkind (Obj_Context) = N_Package_Declaration
3517 and then Unit_Declaration_Node (Corresponding_Spec (Body_Decl)) =
3518 Obj_Context
3519 then
3520 return True;
3521 end if;
3522
3523 return False;
3524 end Is_Enclosing_Package_Body;
3525
3526 -------------------------------------
3527 -- Is_Internal_Declaration_Or_Body --
3528 -------------------------------------
3529
3530 function Is_Internal_Declaration_Or_Body
3531 (Decl : Node_Id) return Boolean
3532 is
3533 begin
3534 if Comes_From_Source (Decl) then
3535 return False;
3536
3537 -- A body generated for an expression function which has not been
3538 -- inserted into the tree yet (In_Spec_Expression is True) is not
3539 -- considered internal.
3540
3541 elsif Nkind (Decl) = N_Subprogram_Body
3542 and then Was_Expression_Function (Decl)
3543 and then not In_Spec_Expression
3544 then
3545 return False;
3546 end if;
3547
3548 return True;
3549 end Is_Internal_Declaration_Or_Body;
3550
3551 -----------------------------------
3552 -- Is_Single_Declaration_Or_Body --
3553 -----------------------------------
3554
3555 function Is_Single_Declaration_Or_Body
3556 (Decl : Node_Id;
3557 Conc_Typ : Entity_Id) return Boolean
3558 is
3559 Spec_Id : constant Entity_Id := Unique_Defining_Entity (Decl);
3560
3561 begin
3562 return
3563 Present (Anonymous_Object (Spec_Id))
3564 and then Anonymous_Object (Spec_Id) = Conc_Typ;
3565 end Is_Single_Declaration_Or_Body;
3566
3567 ---------------------------
3568 -- Is_Single_Task_Pragma --
3569 ---------------------------
3570
3571 function Is_Single_Task_Pragma
3572 (Prag : Node_Id;
3573 Task_Typ : Entity_Id) return Boolean
3574 is
3575 Decl : constant Node_Id := Find_Related_Declaration_Or_Body (Prag);
3576
3577 begin
3578 -- To qualify, the pragma must be associated with single task type
3579 -- Task_Typ.
3580
3581 return
3582 Is_Single_Task_Object (Task_Typ)
3583 and then Nkind (Decl) = N_Object_Declaration
3584 and then Defining_Entity (Decl) = Task_Typ;
3585 end Is_Single_Task_Pragma;
3586
3587 -- Local variables
3588
3589 Conc_Obj : constant Entity_Id := Encapsulating_State (Var_Id);
3590 Par : Node_Id;
3591 Prag_Nam : Name_Id;
3592 Prev : Node_Id;
3593
3594 -- Start of processing for Check_Part_Of_Reference
3595
3596 begin
3597 -- Nothing to do when the variable was recorded, but did not become a
3598 -- constituent of a single concurrent type.
3599
3600 if No (Conc_Obj) then
3601 return;
3602 end if;
3603
3604 -- Traverse the parent chain looking for a suitable context for the
3605 -- reference to the concurrent constituent.
3606
3607 Prev := Ref;
3608 Par := Parent (Prev);
3609 while Present (Par) loop
3610 if Nkind (Par) = N_Pragma then
3611 Prag_Nam := Pragma_Name (Par);
3612
3613 -- A concurrent constituent is allowed to appear in pragmas
3614 -- Initial_Condition and Initializes as this is part of the
3615 -- elaboration checks for the constituent (SPARK RM 9(3)).
3616
3617 if Nam_In (Prag_Nam, Name_Initial_Condition, Name_Initializes) then
3618 return;
3619
3620 -- When the reference appears within pragma Depends or Global,
3621 -- check whether the pragma applies to a single task type. Note
3622 -- that the pragma may not encapsulated by the type definition,
3623 -- but this is still a valid context.
3624
3625 elsif Nam_In (Prag_Nam, Name_Depends, Name_Global)
3626 and then Is_Single_Task_Pragma (Par, Conc_Obj)
3627 then
3628 return;
3629 end if;
3630
3631 -- The reference appears somewhere in the definition of a single
3632 -- concurrent type (SPARK RM 9(3)).
3633
3634 elsif Nkind_In (Par, N_Single_Protected_Declaration,
3635 N_Single_Task_Declaration)
3636 and then Defining_Entity (Par) = Conc_Obj
3637 then
3638 return;
3639
3640 -- The reference appears within the declaration or body of a single
3641 -- concurrent type (SPARK RM 9(3)).
3642
3643 elsif Nkind_In (Par, N_Protected_Body,
3644 N_Protected_Type_Declaration,
3645 N_Task_Body,
3646 N_Task_Type_Declaration)
3647 and then Is_Single_Declaration_Or_Body (Par, Conc_Obj)
3648 then
3649 return;
3650
3651 -- The reference appears within the statement list of the object's
3652 -- immediately enclosing package (SPARK RM 9(3)).
3653
3654 elsif Nkind (Par) = N_Package_Body
3655 and then Nkind (Prev) = N_Handled_Sequence_Of_Statements
3656 and then Is_Enclosing_Package_Body (Par, Var_Id)
3657 then
3658 return;
3659
3660 -- The reference has been relocated within an internally generated
3661 -- package or subprogram. Assume that the reference is legal as the
3662 -- real check was already performed in the original context of the
3663 -- reference.
3664
3665 elsif Nkind_In (Par, N_Package_Body,
3666 N_Package_Declaration,
3667 N_Subprogram_Body,
3668 N_Subprogram_Declaration)
3669 and then Is_Internal_Declaration_Or_Body (Par)
3670 then
3671 return;
3672
3673 -- The reference has been relocated to an inlined body for GNATprove.
3674 -- Assume that the reference is legal as the real check was already
3675 -- performed in the original context of the reference.
3676
3677 elsif GNATprove_Mode
3678 and then Nkind (Par) = N_Subprogram_Body
3679 and then Chars (Defining_Entity (Par)) = Name_uParent
3680 then
3681 return;
3682 end if;
3683
3684 Prev := Par;
3685 Par := Parent (Prev);
3686 end loop;
3687
3688 -- At this point it is known that the reference does not appear within a
3689 -- legal context.
3690
3691 Error_Msg_NE
3692 ("reference to variable & cannot appear in this context", Ref, Var_Id);
3693 Error_Msg_Name_1 := Chars (Var_Id);
3694
3695 if Is_Single_Protected_Object (Conc_Obj) then
3696 Error_Msg_NE
3697 ("\% is constituent of single protected type &", Ref, Conc_Obj);
3698
3699 else
3700 Error_Msg_NE
3701 ("\% is constituent of single task type &", Ref, Conc_Obj);
3702 end if;
3703 end Check_Part_Of_Reference;
3704
3705 ------------------------------------------
3706 -- Check_Potentially_Blocking_Operation --
3707 ------------------------------------------
3708
3709 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3710 S : Entity_Id;
3711
3712 begin
3713 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3714 -- When pragma Detect_Blocking is active, the run time will raise
3715 -- Program_Error. Here we only issue a warning, since we generally
3716 -- support the use of potentially blocking operations in the absence
3717 -- of the pragma.
3718
3719 -- Indirect blocking through a subprogram call cannot be diagnosed
3720 -- statically without interprocedural analysis, so we do not attempt
3721 -- to do it here.
3722
3723 S := Scope (Current_Scope);
3724 while Present (S) and then S /= Standard_Standard loop
3725 if Is_Protected_Type (S) then
3726 Error_Msg_N
3727 ("potentially blocking operation in protected operation??", N);
3728 return;
3729 end if;
3730
3731 S := Scope (S);
3732 end loop;
3733 end Check_Potentially_Blocking_Operation;
3734
3735 ------------------------------------
3736 -- Check_Previous_Null_Procedure --
3737 ------------------------------------
3738
3739 procedure Check_Previous_Null_Procedure
3740 (Decl : Node_Id;
3741 Prev : Entity_Id)
3742 is
3743 begin
3744 if Ekind (Prev) = E_Procedure
3745 and then Nkind (Parent (Prev)) = N_Procedure_Specification
3746 and then Null_Present (Parent (Prev))
3747 then
3748 Error_Msg_Sloc := Sloc (Prev);
3749 Error_Msg_N
3750 ("declaration cannot complete previous null procedure#", Decl);
3751 end if;
3752 end Check_Previous_Null_Procedure;
3753
3754 ---------------------------------
3755 -- Check_Result_And_Post_State --
3756 ---------------------------------
3757
3758 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3759 procedure Check_Result_And_Post_State_In_Pragma
3760 (Prag : Node_Id;
3761 Result_Seen : in out Boolean);
3762 -- Determine whether pragma Prag mentions attribute 'Result and whether
3763 -- the pragma contains an expression that evaluates differently in pre-
3764 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3765 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3766
3767 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3768 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3769 -- formal parameter.
3770
3771 -------------------------------------------
3772 -- Check_Result_And_Post_State_In_Pragma --
3773 -------------------------------------------
3774
3775 procedure Check_Result_And_Post_State_In_Pragma
3776 (Prag : Node_Id;
3777 Result_Seen : in out Boolean)
3778 is
3779 procedure Check_Conjunct (Expr : Node_Id);
3780 -- Check an individual conjunct in a conjunction of Boolean
3781 -- expressions, connected by "and" or "and then" operators.
3782
3783 procedure Check_Conjuncts (Expr : Node_Id);
3784 -- Apply the post-state check to every conjunct in an expression, in
3785 -- case this is a conjunction of Boolean expressions. Otherwise apply
3786 -- it to the expression as a whole.
3787
3788 procedure Check_Expression (Expr : Node_Id);
3789 -- Perform the 'Result and post-state checks on a given expression
3790
3791 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3792 -- Attempt to find attribute 'Result in a subtree denoted by N
3793
3794 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3795 -- Determine whether source node N denotes "True" or "False"
3796
3797 function Mentions_Post_State (N : Node_Id) return Boolean;
3798 -- Determine whether a subtree denoted by N mentions any construct
3799 -- that denotes a post-state.
3800
3801 procedure Check_Function_Result is
3802 new Traverse_Proc (Is_Function_Result);
3803
3804 --------------------
3805 -- Check_Conjunct --
3806 --------------------
3807
3808 procedure Check_Conjunct (Expr : Node_Id) is
3809 function Adjust_Message (Msg : String) return String;
3810 -- Prepend a prefix to the input message Msg denoting that the
3811 -- message applies to a conjunct in the expression, when this
3812 -- is the case.
3813
3814 function Applied_On_Conjunct return Boolean;
3815 -- Returns True if the message applies to a conjunct in the
3816 -- expression, instead of the whole expression.
3817
3818 function Has_Global_Output (Subp : Entity_Id) return Boolean;
3819 -- Returns True if Subp has an output in its Global contract
3820
3821 function Has_No_Output (Subp : Entity_Id) return Boolean;
3822 -- Returns True if Subp has no declared output: no function
3823 -- result, no output parameter, and no output in its Global
3824 -- contract.
3825
3826 --------------------
3827 -- Adjust_Message --
3828 --------------------
3829
3830 function Adjust_Message (Msg : String) return String is
3831 begin
3832 if Applied_On_Conjunct then
3833 return "conjunct in " & Msg;
3834 else
3835 return Msg;
3836 end if;
3837 end Adjust_Message;
3838
3839 -------------------------
3840 -- Applied_On_Conjunct --
3841 -------------------------
3842
3843 function Applied_On_Conjunct return Boolean is
3844 begin
3845 -- Expr is the conjunct of an enclosing "and" expression
3846
3847 return Nkind (Parent (Expr)) in N_Subexpr
3848
3849 -- or Expr is a conjunct of an enclosing "and then"
3850 -- expression in a postcondition aspect that was split into
3851 -- multiple pragmas. The first conjunct has the "and then"
3852 -- expression as Original_Node, and other conjuncts have
3853 -- Split_PCC set to True.
3854
3855 or else Nkind (Original_Node (Expr)) = N_And_Then
3856 or else Split_PPC (Prag);
3857 end Applied_On_Conjunct;
3858
3859 -----------------------
3860 -- Has_Global_Output --
3861 -----------------------
3862
3863 function Has_Global_Output (Subp : Entity_Id) return Boolean is
3864 Global : constant Node_Id := Get_Pragma (Subp, Pragma_Global);
3865 List : Node_Id;
3866 Assoc : Node_Id;
3867
3868 begin
3869 if No (Global) then
3870 return False;
3871 end if;
3872
3873 List := Expression (Get_Argument (Global, Subp));
3874
3875 -- Empty list (no global items) or single global item
3876 -- declaration (only input items).
3877
3878 if Nkind_In (List, N_Null,
3879 N_Expanded_Name,
3880 N_Identifier,
3881 N_Selected_Component)
3882 then
3883 return False;
3884
3885 -- Simple global list (only input items) or moded global list
3886 -- declaration.
3887
3888 elsif Nkind (List) = N_Aggregate then
3889 if Present (Expressions (List)) then
3890 return False;
3891
3892 else
3893 Assoc := First (Component_Associations (List));
3894 while Present (Assoc) loop
3895 if Chars (First (Choices (Assoc))) /= Name_Input then
3896 return True;
3897 end if;
3898
3899 Next (Assoc);
3900 end loop;
3901
3902 return False;
3903 end if;
3904
3905 -- To accommodate partial decoration of disabled SPARK
3906 -- features, this routine may be called with illegal input.
3907 -- If this is the case, do not raise Program_Error.
3908
3909 else
3910 return False;
3911 end if;
3912 end Has_Global_Output;
3913
3914 -------------------
3915 -- Has_No_Output --
3916 -------------------
3917
3918 function Has_No_Output (Subp : Entity_Id) return Boolean is
3919 Param : Node_Id;
3920
3921 begin
3922 -- A function has its result as output
3923
3924 if Ekind (Subp) = E_Function then
3925 return False;
3926 end if;
3927
3928 -- An OUT or IN OUT parameter is an output
3929
3930 Param := First_Formal (Subp);
3931 while Present (Param) loop
3932 if Ekind_In (Param, E_Out_Parameter, E_In_Out_Parameter) then
3933 return False;
3934 end if;
3935
3936 Next_Formal (Param);
3937 end loop;
3938
3939 -- An item of mode Output or In_Out in the Global contract is
3940 -- an output.
3941
3942 if Has_Global_Output (Subp) then
3943 return False;
3944 end if;
3945
3946 return True;
3947 end Has_No_Output;
3948
3949 -- Local variables
3950
3951 Err_Node : Node_Id;
3952 -- Error node when reporting a warning on a (refined)
3953 -- postcondition.
3954
3955 -- Start of processing for Check_Conjunct
3956
3957 begin
3958 if Applied_On_Conjunct then
3959 Err_Node := Expr;
3960 else
3961 Err_Node := Prag;
3962 end if;
3963
3964 -- Do not report missing reference to outcome in postcondition if
3965 -- either the postcondition is trivially True or False, or if the
3966 -- subprogram is ghost and has no declared output.
3967
3968 if not Is_Trivial_Boolean (Expr)
3969 and then not Mentions_Post_State (Expr)
3970 and then not (Is_Ghost_Entity (Subp_Id)
3971 and then Has_No_Output (Subp_Id))
3972 then
3973 if Pragma_Name (Prag) = Name_Contract_Cases then
3974 Error_Msg_NE (Adjust_Message
3975 ("contract case does not check the outcome of calling "
3976 & "&?T?"), Expr, Subp_Id);
3977
3978 elsif Pragma_Name (Prag) = Name_Refined_Post then
3979 Error_Msg_NE (Adjust_Message
3980 ("refined postcondition does not check the outcome of "
3981 & "calling &?T?"), Err_Node, Subp_Id);
3982
3983 else
3984 Error_Msg_NE (Adjust_Message
3985 ("postcondition does not check the outcome of calling "
3986 & "&?T?"), Err_Node, Subp_Id);
3987 end if;
3988 end if;
3989 end Check_Conjunct;
3990
3991 ---------------------
3992 -- Check_Conjuncts --
3993 ---------------------
3994
3995 procedure Check_Conjuncts (Expr : Node_Id) is
3996 begin
3997 if Nkind_In (Expr, N_Op_And, N_And_Then) then
3998 Check_Conjuncts (Left_Opnd (Expr));
3999 Check_Conjuncts (Right_Opnd (Expr));
4000 else
4001 Check_Conjunct (Expr);
4002 end if;
4003 end Check_Conjuncts;
4004
4005 ----------------------
4006 -- Check_Expression --
4007 ----------------------
4008
4009 procedure Check_Expression (Expr : Node_Id) is
4010 begin
4011 if not Is_Trivial_Boolean (Expr) then
4012 Check_Function_Result (Expr);
4013 Check_Conjuncts (Expr);
4014 end if;
4015 end Check_Expression;
4016
4017 ------------------------
4018 -- Is_Function_Result --
4019 ------------------------
4020
4021 function Is_Function_Result (N : Node_Id) return Traverse_Result is
4022 begin
4023 if Is_Attribute_Result (N) then
4024 Result_Seen := True;
4025 return Abandon;
4026
4027 -- Warn on infinite recursion if call is to current function
4028
4029 elsif Nkind (N) = N_Function_Call
4030 and then Is_Entity_Name (Name (N))
4031 and then Entity (Name (N)) = Subp_Id
4032 and then not Is_Potentially_Unevaluated (N)
4033 then
4034 Error_Msg_NE
4035 ("call to & within its postcondition will lead to infinite "
4036 & "recursion?", N, Subp_Id);
4037 return OK;
4038
4039 -- Continue the traversal
4040
4041 else
4042 return OK;
4043 end if;
4044 end Is_Function_Result;
4045
4046 ------------------------
4047 -- Is_Trivial_Boolean --
4048 ------------------------
4049
4050 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
4051 begin
4052 return
4053 Comes_From_Source (N)
4054 and then Is_Entity_Name (N)
4055 and then (Entity (N) = Standard_True
4056 or else
4057 Entity (N) = Standard_False);
4058 end Is_Trivial_Boolean;
4059
4060 -------------------------
4061 -- Mentions_Post_State --
4062 -------------------------
4063
4064 function Mentions_Post_State (N : Node_Id) return Boolean is
4065 Post_State_Seen : Boolean := False;
4066
4067 function Is_Post_State (N : Node_Id) return Traverse_Result;
4068 -- Attempt to find a construct that denotes a post-state. If this
4069 -- is the case, set flag Post_State_Seen.
4070
4071 -------------------
4072 -- Is_Post_State --
4073 -------------------
4074
4075 function Is_Post_State (N : Node_Id) return Traverse_Result is
4076 Ent : Entity_Id;
4077
4078 begin
4079 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
4080 Post_State_Seen := True;
4081 return Abandon;
4082
4083 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
4084 Ent := Entity (N);
4085
4086 -- Treat an undecorated reference as OK
4087
4088 if No (Ent)
4089
4090 -- A reference to an assignable entity is considered a
4091 -- change in the post-state of a subprogram.
4092
4093 or else Ekind_In (Ent, E_Generic_In_Out_Parameter,
4094 E_In_Out_Parameter,
4095 E_Out_Parameter,
4096 E_Variable)
4097
4098 -- The reference may be modified through a dereference
4099
4100 or else (Is_Access_Type (Etype (Ent))
4101 and then Nkind (Parent (N)) =
4102 N_Selected_Component)
4103 then
4104 Post_State_Seen := True;
4105 return Abandon;
4106 end if;
4107
4108 elsif Nkind (N) = N_Attribute_Reference then
4109 if Attribute_Name (N) = Name_Old then
4110 return Skip;
4111
4112 elsif Attribute_Name (N) = Name_Result then
4113 Post_State_Seen := True;
4114 return Abandon;
4115 end if;
4116 end if;
4117
4118 return OK;
4119 end Is_Post_State;
4120
4121 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
4122
4123 -- Start of processing for Mentions_Post_State
4124
4125 begin
4126 Find_Post_State (N);
4127
4128 return Post_State_Seen;
4129 end Mentions_Post_State;
4130
4131 -- Local variables
4132
4133 Expr : constant Node_Id :=
4134 Get_Pragma_Arg
4135 (First (Pragma_Argument_Associations (Prag)));
4136 Nam : constant Name_Id := Pragma_Name (Prag);
4137 CCase : Node_Id;
4138
4139 -- Start of processing for Check_Result_And_Post_State_In_Pragma
4140
4141 begin
4142 -- Examine all consequences
4143
4144 if Nam = Name_Contract_Cases then
4145 CCase := First (Component_Associations (Expr));
4146 while Present (CCase) loop
4147 Check_Expression (Expression (CCase));
4148
4149 Next (CCase);
4150 end loop;
4151
4152 -- Examine the expression of a postcondition
4153
4154 else pragma Assert (Nam_In (Nam, Name_Postcondition,
4155 Name_Refined_Post));
4156 Check_Expression (Expr);
4157 end if;
4158 end Check_Result_And_Post_State_In_Pragma;
4159
4160 --------------------------
4161 -- Has_In_Out_Parameter --
4162 --------------------------
4163
4164 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
4165 Formal : Entity_Id;
4166
4167 begin
4168 -- Traverse the formals looking for an IN OUT parameter
4169
4170 Formal := First_Formal (Subp_Id);
4171 while Present (Formal) loop
4172 if Ekind (Formal) = E_In_Out_Parameter then
4173 return True;
4174 end if;
4175
4176 Next_Formal (Formal);
4177 end loop;
4178
4179 return False;
4180 end Has_In_Out_Parameter;
4181
4182 -- Local variables
4183
4184 Items : constant Node_Id := Contract (Subp_Id);
4185 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
4186 Case_Prag : Node_Id := Empty;
4187 Post_Prag : Node_Id := Empty;
4188 Prag : Node_Id;
4189 Seen_In_Case : Boolean := False;
4190 Seen_In_Post : Boolean := False;
4191 Spec_Id : Entity_Id;
4192
4193 -- Start of processing for Check_Result_And_Post_State
4194
4195 begin
4196 -- The lack of attribute 'Result or a post-state is classified as a
4197 -- suspicious contract. Do not perform the check if the corresponding
4198 -- swich is not set.
4199
4200 if not Warn_On_Suspicious_Contract then
4201 return;
4202
4203 -- Nothing to do if there is no contract
4204
4205 elsif No (Items) then
4206 return;
4207 end if;
4208
4209 -- Retrieve the entity of the subprogram spec (if any)
4210
4211 if Nkind (Subp_Decl) = N_Subprogram_Body
4212 and then Present (Corresponding_Spec (Subp_Decl))
4213 then
4214 Spec_Id := Corresponding_Spec (Subp_Decl);
4215
4216 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
4217 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
4218 then
4219 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
4220
4221 else
4222 Spec_Id := Subp_Id;
4223 end if;
4224
4225 -- Examine all postconditions for attribute 'Result and a post-state
4226
4227 Prag := Pre_Post_Conditions (Items);
4228 while Present (Prag) loop
4229 if Nam_In (Pragma_Name_Unmapped (Prag),
4230 Name_Postcondition, Name_Refined_Post)
4231 and then not Error_Posted (Prag)
4232 then
4233 Post_Prag := Prag;
4234 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
4235 end if;
4236
4237 Prag := Next_Pragma (Prag);
4238 end loop;
4239
4240 -- Examine the contract cases of the subprogram for attribute 'Result
4241 -- and a post-state.
4242
4243 Prag := Contract_Test_Cases (Items);
4244 while Present (Prag) loop
4245 if Pragma_Name (Prag) = Name_Contract_Cases
4246 and then not Error_Posted (Prag)
4247 then
4248 Case_Prag := Prag;
4249 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
4250 end if;
4251
4252 Prag := Next_Pragma (Prag);
4253 end loop;
4254
4255 -- Do not emit any errors if the subprogram is not a function
4256
4257 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
4258 null;
4259
4260 -- Regardless of whether the function has postconditions or contract
4261 -- cases, or whether they mention attribute 'Result, an IN OUT formal
4262 -- parameter is always treated as a result.
4263
4264 elsif Has_In_Out_Parameter (Spec_Id) then
4265 null;
4266
4267 -- The function has both a postcondition and contract cases and they do
4268 -- not mention attribute 'Result.
4269
4270 elsif Present (Case_Prag)
4271 and then not Seen_In_Case
4272 and then Present (Post_Prag)
4273 and then not Seen_In_Post
4274 then
4275 Error_Msg_N
4276 ("neither postcondition nor contract cases mention function "
4277 & "result?T?", Post_Prag);
4278
4279 -- The function has contract cases only and they do not mention
4280 -- attribute 'Result.
4281
4282 elsif Present (Case_Prag) and then not Seen_In_Case then
4283 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
4284
4285 -- The function has postconditions only and they do not mention
4286 -- attribute 'Result.
4287
4288 elsif Present (Post_Prag) and then not Seen_In_Post then
4289 Error_Msg_N
4290 ("postcondition does not mention function result?T?", Post_Prag);
4291 end if;
4292 end Check_Result_And_Post_State;
4293
4294 -----------------------------
4295 -- Check_State_Refinements --
4296 -----------------------------
4297
4298 procedure Check_State_Refinements
4299 (Context : Node_Id;
4300 Is_Main_Unit : Boolean := False)
4301 is
4302 procedure Check_Package (Pack : Node_Id);
4303 -- Verify that all abstract states of a [generic] package denoted by its
4304 -- declarative node Pack have proper refinement. Recursively verify the
4305 -- visible and private declarations of the [generic] package for other
4306 -- nested packages.
4307
4308 procedure Check_Packages_In (Decls : List_Id);
4309 -- Seek out [generic] package declarations within declarative list Decls
4310 -- and verify the status of their abstract state refinement.
4311
4312 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean;
4313 -- Determine whether construct N is subject to pragma SPARK_Mode Off
4314
4315 -------------------
4316 -- Check_Package --
4317 -------------------
4318
4319 procedure Check_Package (Pack : Node_Id) is
4320 Body_Id : constant Entity_Id := Corresponding_Body (Pack);
4321 Spec : constant Node_Id := Specification (Pack);
4322 States : constant Elist_Id :=
4323 Abstract_States (Defining_Entity (Pack));
4324
4325 State_Elmt : Elmt_Id;
4326 State_Id : Entity_Id;
4327
4328 begin
4329 -- Do not verify proper state refinement when the package is subject
4330 -- to pragma SPARK_Mode Off because this disables the requirement for
4331 -- state refinement.
4332
4333 if SPARK_Mode_Is_Off (Pack) then
4334 null;
4335
4336 -- State refinement can only occur in a completing package body. Do
4337 -- not verify proper state refinement when the body is subject to
4338 -- pragma SPARK_Mode Off because this disables the requirement for
4339 -- state refinement.
4340
4341 elsif Present (Body_Id)
4342 and then SPARK_Mode_Is_Off (Unit_Declaration_Node (Body_Id))
4343 then
4344 null;
4345
4346 -- Do not verify proper state refinement when the package is an
4347 -- instance as this check was already performed in the generic.
4348
4349 elsif Present (Generic_Parent (Spec)) then
4350 null;
4351
4352 -- Otherwise examine the contents of the package
4353
4354 else
4355 if Present (States) then
4356 State_Elmt := First_Elmt (States);
4357 while Present (State_Elmt) loop
4358 State_Id := Node (State_Elmt);
4359
4360 -- Emit an error when a non-null state lacks any form of
4361 -- refinement.
4362
4363 if not Is_Null_State (State_Id)
4364 and then not Has_Null_Refinement (State_Id)
4365 and then not Has_Non_Null_Refinement (State_Id)
4366 then
4367 Error_Msg_N ("state & requires refinement", State_Id);
4368 end if;
4369
4370 Next_Elmt (State_Elmt);
4371 end loop;
4372 end if;
4373
4374 Check_Packages_In (Visible_Declarations (Spec));
4375 Check_Packages_In (Private_Declarations (Spec));
4376 end if;
4377 end Check_Package;
4378
4379 -----------------------
4380 -- Check_Packages_In --
4381 -----------------------
4382
4383 procedure Check_Packages_In (Decls : List_Id) is
4384 Decl : Node_Id;
4385
4386 begin
4387 if Present (Decls) then
4388 Decl := First (Decls);
4389 while Present (Decl) loop
4390 if Nkind_In (Decl, N_Generic_Package_Declaration,
4391 N_Package_Declaration)
4392 then
4393 Check_Package (Decl);
4394 end if;
4395
4396 Next (Decl);
4397 end loop;
4398 end if;
4399 end Check_Packages_In;
4400
4401 -----------------------
4402 -- SPARK_Mode_Is_Off --
4403 -----------------------
4404
4405 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean is
4406 Id : constant Entity_Id := Defining_Entity (N);
4407 Prag : constant Node_Id := SPARK_Pragma (Id);
4408
4409 begin
4410 -- Default the mode to "off" when the context is an instance and all
4411 -- SPARK_Mode pragmas found within are to be ignored.
4412
4413 if Ignore_SPARK_Mode_Pragmas (Id) then
4414 return True;
4415
4416 else
4417 return
4418 Present (Prag)
4419 and then Get_SPARK_Mode_From_Annotation (Prag) = Off;
4420 end if;
4421 end SPARK_Mode_Is_Off;
4422
4423 -- Start of processing for Check_State_Refinements
4424
4425 begin
4426 -- A block may declare a nested package
4427
4428 if Nkind (Context) = N_Block_Statement then
4429 Check_Packages_In (Declarations (Context));
4430
4431 -- An entry, protected, subprogram, or task body may declare a nested
4432 -- package.
4433
4434 elsif Nkind_In (Context, N_Entry_Body,
4435 N_Protected_Body,
4436 N_Subprogram_Body,
4437 N_Task_Body)
4438 then
4439 -- Do not verify proper state refinement when the body is subject to
4440 -- pragma SPARK_Mode Off because this disables the requirement for
4441 -- state refinement.
4442
4443 if not SPARK_Mode_Is_Off (Context) then
4444 Check_Packages_In (Declarations (Context));
4445 end if;
4446
4447 -- A package body may declare a nested package
4448
4449 elsif Nkind (Context) = N_Package_Body then
4450 Check_Package (Unit_Declaration_Node (Corresponding_Spec (Context)));
4451
4452 -- Do not verify proper state refinement when the body is subject to
4453 -- pragma SPARK_Mode Off because this disables the requirement for
4454 -- state refinement.
4455
4456 if not SPARK_Mode_Is_Off (Context) then
4457 Check_Packages_In (Declarations (Context));
4458 end if;
4459
4460 -- A library level [generic] package may declare a nested package
4461
4462 elsif Nkind_In (Context, N_Generic_Package_Declaration,
4463 N_Package_Declaration)
4464 and then Is_Main_Unit
4465 then
4466 Check_Package (Context);
4467 end if;
4468 end Check_State_Refinements;
4469
4470 ------------------------------
4471 -- Check_Unprotected_Access --
4472 ------------------------------
4473
4474 procedure Check_Unprotected_Access
4475 (Context : Node_Id;
4476 Expr : Node_Id)
4477 is
4478 Cont_Encl_Typ : Entity_Id;
4479 Pref_Encl_Typ : Entity_Id;
4480
4481 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
4482 -- Check whether Obj is a private component of a protected object.
4483 -- Return the protected type where the component resides, Empty
4484 -- otherwise.
4485
4486 function Is_Public_Operation return Boolean;
4487 -- Verify that the enclosing operation is callable from outside the
4488 -- protected object, to minimize false positives.
4489
4490 ------------------------------
4491 -- Enclosing_Protected_Type --
4492 ------------------------------
4493
4494 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
4495 begin
4496 if Is_Entity_Name (Obj) then
4497 declare
4498 Ent : Entity_Id := Entity (Obj);
4499
4500 begin
4501 -- The object can be a renaming of a private component, use
4502 -- the original record component.
4503
4504 if Is_Prival (Ent) then
4505 Ent := Prival_Link (Ent);
4506 end if;
4507
4508 if Is_Protected_Type (Scope (Ent)) then
4509 return Scope (Ent);
4510 end if;
4511 end;
4512 end if;
4513
4514 -- For indexed and selected components, recursively check the prefix
4515
4516 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
4517 return Enclosing_Protected_Type (Prefix (Obj));
4518
4519 -- The object does not denote a protected component
4520
4521 else
4522 return Empty;
4523 end if;
4524 end Enclosing_Protected_Type;
4525
4526 -------------------------
4527 -- Is_Public_Operation --
4528 -------------------------
4529
4530 function Is_Public_Operation return Boolean is
4531 S : Entity_Id;
4532 E : Entity_Id;
4533
4534 begin
4535 S := Current_Scope;
4536 while Present (S) and then S /= Pref_Encl_Typ loop
4537 if Scope (S) = Pref_Encl_Typ then
4538 E := First_Entity (Pref_Encl_Typ);
4539 while Present (E)
4540 and then E /= First_Private_Entity (Pref_Encl_Typ)
4541 loop
4542 if E = S then
4543 return True;
4544 end if;
4545
4546 Next_Entity (E);
4547 end loop;
4548 end if;
4549
4550 S := Scope (S);
4551 end loop;
4552
4553 return False;
4554 end Is_Public_Operation;
4555
4556 -- Start of processing for Check_Unprotected_Access
4557
4558 begin
4559 if Nkind (Expr) = N_Attribute_Reference
4560 and then Attribute_Name (Expr) = Name_Unchecked_Access
4561 then
4562 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
4563 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
4564
4565 -- Check whether we are trying to export a protected component to a
4566 -- context with an equal or lower access level.
4567
4568 if Present (Pref_Encl_Typ)
4569 and then No (Cont_Encl_Typ)
4570 and then Is_Public_Operation
4571 and then Scope_Depth (Pref_Encl_Typ) >=
4572 Object_Access_Level (Context)
4573 then
4574 Error_Msg_N
4575 ("??possible unprotected access to protected data", Expr);
4576 end if;
4577 end if;
4578 end Check_Unprotected_Access;
4579
4580 ------------------------------
4581 -- Check_Unused_Body_States --
4582 ------------------------------
4583
4584 procedure Check_Unused_Body_States (Body_Id : Entity_Id) is
4585 procedure Process_Refinement_Clause
4586 (Clause : Node_Id;
4587 States : Elist_Id);
4588 -- Inspect all constituents of refinement clause Clause and remove any
4589 -- matches from body state list States.
4590
4591 procedure Report_Unused_Body_States (States : Elist_Id);
4592 -- Emit errors for each abstract state or object found in list States
4593
4594 -------------------------------
4595 -- Process_Refinement_Clause --
4596 -------------------------------
4597
4598 procedure Process_Refinement_Clause
4599 (Clause : Node_Id;
4600 States : Elist_Id)
4601 is
4602 procedure Process_Constituent (Constit : Node_Id);
4603 -- Remove constituent Constit from body state list States
4604
4605 -------------------------
4606 -- Process_Constituent --
4607 -------------------------
4608
4609 procedure Process_Constituent (Constit : Node_Id) is
4610 Constit_Id : Entity_Id;
4611
4612 begin
4613 -- Guard against illegal constituents. Only abstract states and
4614 -- objects can appear on the right hand side of a refinement.
4615
4616 if Is_Entity_Name (Constit) then
4617 Constit_Id := Entity_Of (Constit);
4618
4619 if Present (Constit_Id)
4620 and then Ekind_In (Constit_Id, E_Abstract_State,
4621 E_Constant,
4622 E_Variable)
4623 then
4624 Remove (States, Constit_Id);
4625 end if;
4626 end if;
4627 end Process_Constituent;
4628
4629 -- Local variables
4630
4631 Constit : Node_Id;
4632
4633 -- Start of processing for Process_Refinement_Clause
4634
4635 begin
4636 if Nkind (Clause) = N_Component_Association then
4637 Constit := Expression (Clause);
4638
4639 -- Multiple constituents appear as an aggregate
4640
4641 if Nkind (Constit) = N_Aggregate then
4642 Constit := First (Expressions (Constit));
4643 while Present (Constit) loop
4644 Process_Constituent (Constit);
4645 Next (Constit);
4646 end loop;
4647
4648 -- Various forms of a single constituent
4649
4650 else
4651 Process_Constituent (Constit);
4652 end if;
4653 end if;
4654 end Process_Refinement_Clause;
4655
4656 -------------------------------
4657 -- Report_Unused_Body_States --
4658 -------------------------------
4659
4660 procedure Report_Unused_Body_States (States : Elist_Id) is
4661 Posted : Boolean := False;
4662 State_Elmt : Elmt_Id;
4663 State_Id : Entity_Id;
4664
4665 begin
4666 if Present (States) then
4667 State_Elmt := First_Elmt (States);
4668 while Present (State_Elmt) loop
4669 State_Id := Node (State_Elmt);
4670
4671 -- Constants are part of the hidden state of a package, but the
4672 -- compiler cannot determine whether they have variable input
4673 -- (SPARK RM 7.1.1(2)) and cannot classify them properly as a
4674 -- hidden state. Do not emit an error when a constant does not
4675 -- participate in a state refinement, even though it acts as a
4676 -- hidden state.
4677
4678 if Ekind (State_Id) = E_Constant then
4679 null;
4680
4681 -- Generate an error message of the form:
4682
4683 -- body of package ... has unused hidden states
4684 -- abstract state ... defined at ...
4685 -- variable ... defined at ...
4686
4687 else
4688 if not Posted then
4689 Posted := True;
4690 SPARK_Msg_N
4691 ("body of package & has unused hidden states", Body_Id);
4692 end if;
4693
4694 Error_Msg_Sloc := Sloc (State_Id);
4695
4696 if Ekind (State_Id) = E_Abstract_State then
4697 SPARK_Msg_NE
4698 ("\abstract state & defined #", Body_Id, State_Id);
4699
4700 else
4701 SPARK_Msg_NE ("\variable & defined #", Body_Id, State_Id);
4702 end if;
4703 end if;
4704
4705 Next_Elmt (State_Elmt);
4706 end loop;
4707 end if;
4708 end Report_Unused_Body_States;
4709
4710 -- Local variables
4711
4712 Prag : constant Node_Id := Get_Pragma (Body_Id, Pragma_Refined_State);
4713 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
4714 Clause : Node_Id;
4715 States : Elist_Id;
4716
4717 -- Start of processing for Check_Unused_Body_States
4718
4719 begin
4720 -- Inspect the clauses of pragma Refined_State and determine whether all
4721 -- visible states declared within the package body participate in the
4722 -- refinement.
4723
4724 if Present (Prag) then
4725 Clause := Expression (Get_Argument (Prag, Spec_Id));
4726 States := Collect_Body_States (Body_Id);
4727
4728 -- Multiple non-null state refinements appear as an aggregate
4729
4730 if Nkind (Clause) = N_Aggregate then
4731 Clause := First (Component_Associations (Clause));
4732 while Present (Clause) loop
4733 Process_Refinement_Clause (Clause, States);
4734 Next (Clause);
4735 end loop;
4736
4737 -- Various forms of a single state refinement
4738
4739 else
4740 Process_Refinement_Clause (Clause, States);
4741 end if;
4742
4743 -- Ensure that all abstract states and objects declared in the
4744 -- package body state space are utilized as constituents.
4745
4746 Report_Unused_Body_States (States);
4747 end if;
4748 end Check_Unused_Body_States;
4749
4750 -----------------
4751 -- Choice_List --
4752 -----------------
4753
4754 function Choice_List (N : Node_Id) return List_Id is
4755 begin
4756 if Nkind (N) = N_Iterated_Component_Association then
4757 return Discrete_Choices (N);
4758 else
4759 return Choices (N);
4760 end if;
4761 end Choice_List;
4762
4763 -------------------------
4764 -- Collect_Body_States --
4765 -------------------------
4766
4767 function Collect_Body_States (Body_Id : Entity_Id) return Elist_Id is
4768 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean;
4769 -- Determine whether object Obj_Id is a suitable visible state of a
4770 -- package body.
4771
4772 procedure Collect_Visible_States
4773 (Pack_Id : Entity_Id;
4774 States : in out Elist_Id);
4775 -- Gather the entities of all abstract states and objects declared in
4776 -- the visible state space of package Pack_Id.
4777
4778 ----------------------------
4779 -- Collect_Visible_States --
4780 ----------------------------
4781
4782 procedure Collect_Visible_States
4783 (Pack_Id : Entity_Id;
4784 States : in out Elist_Id)
4785 is
4786 Item_Id : Entity_Id;
4787
4788 begin
4789 -- Traverse the entity chain of the package and inspect all visible
4790 -- items.
4791
4792 Item_Id := First_Entity (Pack_Id);
4793 while Present (Item_Id) and then not In_Private_Part (Item_Id) loop
4794
4795 -- Do not consider internally generated items as those cannot be
4796 -- named and participate in refinement.
4797
4798 if not Comes_From_Source (Item_Id) then
4799 null;
4800
4801 elsif Ekind (Item_Id) = E_Abstract_State then
4802 Append_New_Elmt (Item_Id, States);
4803
4804 elsif Ekind_In (Item_Id, E_Constant, E_Variable)
4805 and then Is_Visible_Object (Item_Id)
4806 then
4807 Append_New_Elmt (Item_Id, States);
4808
4809 -- Recursively gather the visible states of a nested package
4810
4811 elsif Ekind (Item_Id) = E_Package then
4812 Collect_Visible_States (Item_Id, States);
4813 end if;
4814
4815 Next_Entity (Item_Id);
4816 end loop;
4817 end Collect_Visible_States;
4818
4819 -----------------------
4820 -- Is_Visible_Object --
4821 -----------------------
4822
4823 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean is
4824 begin
4825 -- Objects that map generic formals to their actuals are not visible
4826 -- from outside the generic instantiation.
4827
4828 if Present (Corresponding_Generic_Association
4829 (Declaration_Node (Obj_Id)))
4830 then
4831 return False;
4832
4833 -- Constituents of a single protected/task type act as components of
4834 -- the type and are not visible from outside the type.
4835
4836 elsif Ekind (Obj_Id) = E_Variable
4837 and then Present (Encapsulating_State (Obj_Id))
4838 and then Is_Single_Concurrent_Object (Encapsulating_State (Obj_Id))
4839 then
4840 return False;
4841
4842 else
4843 return True;
4844 end if;
4845 end Is_Visible_Object;
4846
4847 -- Local variables
4848
4849 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
4850 Decl : Node_Id;
4851 Item_Id : Entity_Id;
4852 States : Elist_Id := No_Elist;
4853
4854 -- Start of processing for Collect_Body_States
4855
4856 begin
4857 -- Inspect the declarations of the body looking for source objects,
4858 -- packages and package instantiations. Note that even though this
4859 -- processing is very similar to Collect_Visible_States, a package
4860 -- body does not have a First/Next_Entity list.
4861
4862 Decl := First (Declarations (Body_Decl));
4863 while Present (Decl) loop
4864
4865 -- Capture source objects as internally generated temporaries cannot
4866 -- be named and participate in refinement.
4867
4868 if Nkind (Decl) = N_Object_Declaration then
4869 Item_Id := Defining_Entity (Decl);
4870
4871 if Comes_From_Source (Item_Id)
4872 and then Is_Visible_Object (Item_Id)
4873 then
4874 Append_New_Elmt (Item_Id, States);
4875 end if;
4876
4877 -- Capture the visible abstract states and objects of a source
4878 -- package [instantiation].
4879
4880 elsif Nkind (Decl) = N_Package_Declaration then
4881 Item_Id := Defining_Entity (Decl);
4882
4883 if Comes_From_Source (Item_Id) then
4884 Collect_Visible_States (Item_Id, States);
4885 end if;
4886 end if;
4887
4888 Next (Decl);
4889 end loop;
4890
4891 return States;
4892 end Collect_Body_States;
4893
4894 ------------------------
4895 -- Collect_Interfaces --
4896 ------------------------
4897
4898 procedure Collect_Interfaces
4899 (T : Entity_Id;
4900 Ifaces_List : out Elist_Id;
4901 Exclude_Parents : Boolean := False;
4902 Use_Full_View : Boolean := True)
4903 is
4904 procedure Collect (Typ : Entity_Id);
4905 -- Subsidiary subprogram used to traverse the whole list
4906 -- of directly and indirectly implemented interfaces
4907
4908 -------------
4909 -- Collect --
4910 -------------
4911
4912 procedure Collect (Typ : Entity_Id) is
4913 Ancestor : Entity_Id;
4914 Full_T : Entity_Id;
4915 Id : Node_Id;
4916 Iface : Entity_Id;
4917
4918 begin
4919 Full_T := Typ;
4920
4921 -- Handle private types and subtypes
4922
4923 if Use_Full_View
4924 and then Is_Private_Type (Typ)
4925 and then Present (Full_View (Typ))
4926 then
4927 Full_T := Full_View (Typ);
4928
4929 if Ekind (Full_T) = E_Record_Subtype then
4930 Full_T := Etype (Typ);
4931
4932 if Present (Full_View (Full_T)) then
4933 Full_T := Full_View (Full_T);
4934 end if;
4935 end if;
4936 end if;
4937
4938 -- Include the ancestor if we are generating the whole list of
4939 -- abstract interfaces.
4940
4941 if Etype (Full_T) /= Typ
4942
4943 -- Protect the frontend against wrong sources. For example:
4944
4945 -- package P is
4946 -- type A is tagged null record;
4947 -- type B is new A with private;
4948 -- type C is new A with private;
4949 -- private
4950 -- type B is new C with null record;
4951 -- type C is new B with null record;
4952 -- end P;
4953
4954 and then Etype (Full_T) /= T
4955 then
4956 Ancestor := Etype (Full_T);
4957 Collect (Ancestor);
4958
4959 if Is_Interface (Ancestor) and then not Exclude_Parents then
4960 Append_Unique_Elmt (Ancestor, Ifaces_List);
4961 end if;
4962 end if;
4963
4964 -- Traverse the graph of ancestor interfaces
4965
4966 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
4967 Id := First (Abstract_Interface_List (Full_T));
4968 while Present (Id) loop
4969 Iface := Etype (Id);
4970
4971 -- Protect against wrong uses. For example:
4972 -- type I is interface;
4973 -- type O is tagged null record;
4974 -- type Wrong is new I and O with null record; -- ERROR
4975
4976 if Is_Interface (Iface) then
4977 if Exclude_Parents
4978 and then Etype (T) /= T
4979 and then Interface_Present_In_Ancestor (Etype (T), Iface)
4980 then
4981 null;
4982 else
4983 Collect (Iface);
4984 Append_Unique_Elmt (Iface, Ifaces_List);
4985 end if;
4986 end if;
4987
4988 Next (Id);
4989 end loop;
4990 end if;
4991 end Collect;
4992
4993 -- Start of processing for Collect_Interfaces
4994
4995 begin
4996 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
4997 Ifaces_List := New_Elmt_List;
4998 Collect (T);
4999 end Collect_Interfaces;
5000
5001 ----------------------------------
5002 -- Collect_Interface_Components --
5003 ----------------------------------
5004
5005 procedure Collect_Interface_Components
5006 (Tagged_Type : Entity_Id;
5007 Components_List : out Elist_Id)
5008 is
5009 procedure Collect (Typ : Entity_Id);
5010 -- Subsidiary subprogram used to climb to the parents
5011
5012 -------------
5013 -- Collect --
5014 -------------
5015
5016 procedure Collect (Typ : Entity_Id) is
5017 Tag_Comp : Entity_Id;
5018 Parent_Typ : Entity_Id;
5019
5020 begin
5021 -- Handle private types
5022
5023 if Present (Full_View (Etype (Typ))) then
5024 Parent_Typ := Full_View (Etype (Typ));
5025 else
5026 Parent_Typ := Etype (Typ);
5027 end if;
5028
5029 if Parent_Typ /= Typ
5030
5031 -- Protect the frontend against wrong sources. For example:
5032
5033 -- package P is
5034 -- type A is tagged null record;
5035 -- type B is new A with private;
5036 -- type C is new A with private;
5037 -- private
5038 -- type B is new C with null record;
5039 -- type C is new B with null record;
5040 -- end P;
5041
5042 and then Parent_Typ /= Tagged_Type
5043 then
5044 Collect (Parent_Typ);
5045 end if;
5046
5047 -- Collect the components containing tags of secondary dispatch
5048 -- tables.
5049
5050 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
5051 while Present (Tag_Comp) loop
5052 pragma Assert (Present (Related_Type (Tag_Comp)));
5053 Append_Elmt (Tag_Comp, Components_List);
5054
5055 Tag_Comp := Next_Tag_Component (Tag_Comp);
5056 end loop;
5057 end Collect;
5058
5059 -- Start of processing for Collect_Interface_Components
5060
5061 begin
5062 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
5063 and then Is_Tagged_Type (Tagged_Type));
5064
5065 Components_List := New_Elmt_List;
5066 Collect (Tagged_Type);
5067 end Collect_Interface_Components;
5068
5069 -----------------------------
5070 -- Collect_Interfaces_Info --
5071 -----------------------------
5072
5073 procedure Collect_Interfaces_Info
5074 (T : Entity_Id;
5075 Ifaces_List : out Elist_Id;
5076 Components_List : out Elist_Id;
5077 Tags_List : out Elist_Id)
5078 is
5079 Comps_List : Elist_Id;
5080 Comp_Elmt : Elmt_Id;
5081 Comp_Iface : Entity_Id;
5082 Iface_Elmt : Elmt_Id;
5083 Iface : Entity_Id;
5084
5085 function Search_Tag (Iface : Entity_Id) return Entity_Id;
5086 -- Search for the secondary tag associated with the interface type
5087 -- Iface that is implemented by T.
5088
5089 ----------------
5090 -- Search_Tag --
5091 ----------------
5092
5093 function Search_Tag (Iface : Entity_Id) return Entity_Id is
5094 ADT : Elmt_Id;
5095 begin
5096 if not Is_CPP_Class (T) then
5097 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
5098 else
5099 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
5100 end if;
5101
5102 while Present (ADT)
5103 and then Is_Tag (Node (ADT))
5104 and then Related_Type (Node (ADT)) /= Iface
5105 loop
5106 -- Skip secondary dispatch table referencing thunks to user
5107 -- defined primitives covered by this interface.
5108
5109 pragma Assert (Has_Suffix (Node (ADT), 'P'));
5110 Next_Elmt (ADT);
5111
5112 -- Skip secondary dispatch tables of Ada types
5113
5114 if not Is_CPP_Class (T) then
5115
5116 -- Skip secondary dispatch table referencing thunks to
5117 -- predefined primitives.
5118
5119 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
5120 Next_Elmt (ADT);
5121
5122 -- Skip secondary dispatch table referencing user-defined
5123 -- primitives covered by this interface.
5124
5125 pragma Assert (Has_Suffix (Node (ADT), 'D'));
5126 Next_Elmt (ADT);
5127
5128 -- Skip secondary dispatch table referencing predefined
5129 -- primitives.
5130
5131 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
5132 Next_Elmt (ADT);
5133 end if;
5134 end loop;
5135
5136 pragma Assert (Is_Tag (Node (ADT)));
5137 return Node (ADT);
5138 end Search_Tag;
5139
5140 -- Start of processing for Collect_Interfaces_Info
5141
5142 begin
5143 Collect_Interfaces (T, Ifaces_List);
5144 Collect_Interface_Components (T, Comps_List);
5145
5146 -- Search for the record component and tag associated with each
5147 -- interface type of T.
5148
5149 Components_List := New_Elmt_List;
5150 Tags_List := New_Elmt_List;
5151
5152 Iface_Elmt := First_Elmt (Ifaces_List);
5153 while Present (Iface_Elmt) loop
5154 Iface := Node (Iface_Elmt);
5155
5156 -- Associate the primary tag component and the primary dispatch table
5157 -- with all the interfaces that are parents of T
5158
5159 if Is_Ancestor (Iface, T, Use_Full_View => True) then
5160 Append_Elmt (First_Tag_Component (T), Components_List);
5161 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
5162
5163 -- Otherwise search for the tag component and secondary dispatch
5164 -- table of Iface
5165
5166 else
5167 Comp_Elmt := First_Elmt (Comps_List);
5168 while Present (Comp_Elmt) loop
5169 Comp_Iface := Related_Type (Node (Comp_Elmt));
5170
5171 if Comp_Iface = Iface
5172 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
5173 then
5174 Append_Elmt (Node (Comp_Elmt), Components_List);
5175 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
5176 exit;
5177 end if;
5178
5179 Next_Elmt (Comp_Elmt);
5180 end loop;
5181 pragma Assert (Present (Comp_Elmt));
5182 end if;
5183
5184 Next_Elmt (Iface_Elmt);
5185 end loop;
5186 end Collect_Interfaces_Info;
5187
5188 ---------------------
5189 -- Collect_Parents --
5190 ---------------------
5191
5192 procedure Collect_Parents
5193 (T : Entity_Id;
5194 List : out Elist_Id;
5195 Use_Full_View : Boolean := True)
5196 is
5197 Current_Typ : Entity_Id := T;
5198 Parent_Typ : Entity_Id;
5199
5200 begin
5201 List := New_Elmt_List;
5202
5203 -- No action if the if the type has no parents
5204
5205 if T = Etype (T) then
5206 return;
5207 end if;
5208
5209 loop
5210 Parent_Typ := Etype (Current_Typ);
5211
5212 if Is_Private_Type (Parent_Typ)
5213 and then Present (Full_View (Parent_Typ))
5214 and then Use_Full_View
5215 then
5216 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5217 end if;
5218
5219 Append_Elmt (Parent_Typ, List);
5220
5221 exit when Parent_Typ = Current_Typ;
5222 Current_Typ := Parent_Typ;
5223 end loop;
5224 end Collect_Parents;
5225
5226 ----------------------------------
5227 -- Collect_Primitive_Operations --
5228 ----------------------------------
5229
5230 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
5231 B_Type : constant Entity_Id := Base_Type (T);
5232
5233 function Match (E : Entity_Id) return Boolean;
5234 -- True if E's base type is B_Type, or E is of an anonymous access type
5235 -- and the base type of its designated type is B_Type.
5236
5237 -----------
5238 -- Match --
5239 -----------
5240
5241 function Match (E : Entity_Id) return Boolean is
5242 Etyp : Entity_Id := Etype (E);
5243
5244 begin
5245 if Ekind (Etyp) = E_Anonymous_Access_Type then
5246 Etyp := Designated_Type (Etyp);
5247 end if;
5248
5249 -- In Ada 2012 a primitive operation may have a formal of an
5250 -- incomplete view of the parent type.
5251
5252 return Base_Type (Etyp) = B_Type
5253 or else
5254 (Ada_Version >= Ada_2012
5255 and then Ekind (Etyp) = E_Incomplete_Type
5256 and then Full_View (Etyp) = B_Type);
5257 end Match;
5258
5259 -- Local variables
5260
5261 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
5262 B_Scope : Entity_Id := Scope (B_Type);
5263 Op_List : Elist_Id;
5264 Eq_Prims_List : Elist_Id := No_Elist;
5265 Formal : Entity_Id;
5266 Is_Prim : Boolean;
5267 Is_Type_In_Pkg : Boolean;
5268 Formal_Derived : Boolean := False;
5269 Id : Entity_Id;
5270
5271 -- Start of processing for Collect_Primitive_Operations
5272
5273 begin
5274 -- For tagged types, the primitive operations are collected as they
5275 -- are declared, and held in an explicit list which is simply returned.
5276
5277 if Is_Tagged_Type (B_Type) then
5278 return Primitive_Operations (B_Type);
5279
5280 -- An untagged generic type that is a derived type inherits the
5281 -- primitive operations of its parent type. Other formal types only
5282 -- have predefined operators, which are not explicitly represented.
5283
5284 elsif Is_Generic_Type (B_Type) then
5285 if Nkind (B_Decl) = N_Formal_Type_Declaration
5286 and then Nkind (Formal_Type_Definition (B_Decl)) =
5287 N_Formal_Derived_Type_Definition
5288 then
5289 Formal_Derived := True;
5290 else
5291 return New_Elmt_List;
5292 end if;
5293 end if;
5294
5295 Op_List := New_Elmt_List;
5296
5297 if B_Scope = Standard_Standard then
5298 if B_Type = Standard_String then
5299 Append_Elmt (Standard_Op_Concat, Op_List);
5300
5301 elsif B_Type = Standard_Wide_String then
5302 Append_Elmt (Standard_Op_Concatw, Op_List);
5303
5304 else
5305 null;
5306 end if;
5307
5308 -- Locate the primitive subprograms of the type
5309
5310 else
5311 -- The primitive operations appear after the base type, except if the
5312 -- derivation happens within the private part of B_Scope and the type
5313 -- is a private type, in which case both the type and some primitive
5314 -- operations may appear before the base type, and the list of
5315 -- candidates starts after the type.
5316
5317 if In_Open_Scopes (B_Scope)
5318 and then Scope (T) = B_Scope
5319 and then In_Private_Part (B_Scope)
5320 then
5321 Id := Next_Entity (T);
5322
5323 -- In Ada 2012, If the type has an incomplete partial view, there may
5324 -- be primitive operations declared before the full view, so we need
5325 -- to start scanning from the incomplete view, which is earlier on
5326 -- the entity chain.
5327
5328 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
5329 and then Present (Incomplete_View (Parent (B_Type)))
5330 then
5331 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
5332
5333 -- If T is a derived from a type with an incomplete view declared
5334 -- elsewhere, that incomplete view is irrelevant, we want the
5335 -- operations in the scope of T.
5336
5337 if Scope (Id) /= Scope (B_Type) then
5338 Id := Next_Entity (B_Type);
5339 end if;
5340
5341 else
5342 Id := Next_Entity (B_Type);
5343 end if;
5344
5345 -- Set flag if this is a type in a package spec
5346
5347 Is_Type_In_Pkg :=
5348 Is_Package_Or_Generic_Package (B_Scope)
5349 and then
5350 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
5351 N_Package_Body;
5352
5353 while Present (Id) loop
5354
5355 -- Test whether the result type or any of the parameter types of
5356 -- each subprogram following the type match that type when the
5357 -- type is declared in a package spec, is a derived type, or the
5358 -- subprogram is marked as primitive. (The Is_Primitive test is
5359 -- needed to find primitives of nonderived types in declarative
5360 -- parts that happen to override the predefined "=" operator.)
5361
5362 -- Note that generic formal subprograms are not considered to be
5363 -- primitive operations and thus are never inherited.
5364
5365 if Is_Overloadable (Id)
5366 and then (Is_Type_In_Pkg
5367 or else Is_Derived_Type (B_Type)
5368 or else Is_Primitive (Id))
5369 and then Nkind (Parent (Parent (Id)))
5370 not in N_Formal_Subprogram_Declaration
5371 then
5372 Is_Prim := False;
5373
5374 if Match (Id) then
5375 Is_Prim := True;
5376
5377 else
5378 Formal := First_Formal (Id);
5379 while Present (Formal) loop
5380 if Match (Formal) then
5381 Is_Prim := True;
5382 exit;
5383 end if;
5384
5385 Next_Formal (Formal);
5386 end loop;
5387 end if;
5388
5389 -- For a formal derived type, the only primitives are the ones
5390 -- inherited from the parent type. Operations appearing in the
5391 -- package declaration are not primitive for it.
5392
5393 if Is_Prim
5394 and then (not Formal_Derived or else Present (Alias (Id)))
5395 then
5396 -- In the special case of an equality operator aliased to
5397 -- an overriding dispatching equality belonging to the same
5398 -- type, we don't include it in the list of primitives.
5399 -- This avoids inheriting multiple equality operators when
5400 -- deriving from untagged private types whose full type is
5401 -- tagged, which can otherwise cause ambiguities. Note that
5402 -- this should only happen for this kind of untagged parent
5403 -- type, since normally dispatching operations are inherited
5404 -- using the type's Primitive_Operations list.
5405
5406 if Chars (Id) = Name_Op_Eq
5407 and then Is_Dispatching_Operation (Id)
5408 and then Present (Alias (Id))
5409 and then Present (Overridden_Operation (Alias (Id)))
5410 and then Base_Type (Etype (First_Entity (Id))) =
5411 Base_Type (Etype (First_Entity (Alias (Id))))
5412 then
5413 null;
5414
5415 -- Include the subprogram in the list of primitives
5416
5417 else
5418 Append_Elmt (Id, Op_List);
5419
5420 -- Save collected equality primitives for later filtering
5421 -- (if we are processing a private type for which we can
5422 -- collect several candidates).
5423
5424 if Inherits_From_Tagged_Full_View (T)
5425 and then Chars (Id) = Name_Op_Eq
5426 and then Etype (First_Formal (Id)) =
5427 Etype (Next_Formal (First_Formal (Id)))
5428 then
5429 if No (Eq_Prims_List) then
5430 Eq_Prims_List := New_Elmt_List;
5431 end if;
5432
5433 Append_Elmt (Id, Eq_Prims_List);
5434 end if;
5435 end if;
5436 end if;
5437 end if;
5438
5439 Next_Entity (Id);
5440
5441 -- For a type declared in System, some of its operations may
5442 -- appear in the target-specific extension to System.
5443
5444 if No (Id)
5445 and then B_Scope = RTU_Entity (System)
5446 and then Present_System_Aux
5447 then
5448 B_Scope := System_Aux_Id;
5449 Id := First_Entity (System_Aux_Id);
5450 end if;
5451 end loop;
5452
5453 -- Filter collected equality primitives
5454
5455 if Inherits_From_Tagged_Full_View (T)
5456 and then Present (Eq_Prims_List)
5457 then
5458 declare
5459 First : constant Elmt_Id := First_Elmt (Eq_Prims_List);
5460 Second : Elmt_Id;
5461
5462 begin
5463 pragma Assert (No (Next_Elmt (First))
5464 or else No (Next_Elmt (Next_Elmt (First))));
5465
5466 -- No action needed if we have collected a single equality
5467 -- primitive
5468
5469 if Present (Next_Elmt (First)) then
5470 Second := Next_Elmt (First);
5471
5472 if Is_Dispatching_Operation
5473 (Ultimate_Alias (Node (First)))
5474 then
5475 Remove (Op_List, Node (First));
5476
5477 elsif Is_Dispatching_Operation
5478 (Ultimate_Alias (Node (Second)))
5479 then
5480 Remove (Op_List, Node (Second));
5481
5482 else
5483 pragma Assert (False);
5484 raise Program_Error;
5485 end if;
5486 end if;
5487 end;
5488 end if;
5489 end if;
5490
5491 return Op_List;
5492 end Collect_Primitive_Operations;
5493
5494 -----------------------------------
5495 -- Compile_Time_Constraint_Error --
5496 -----------------------------------
5497
5498 function Compile_Time_Constraint_Error
5499 (N : Node_Id;
5500 Msg : String;
5501 Ent : Entity_Id := Empty;
5502 Loc : Source_Ptr := No_Location;
5503 Warn : Boolean := False;
5504 Extra_Msg : String := "") return Node_Id
5505 is
5506 Msgc : String (1 .. Msg'Length + 3);
5507 -- Copy of message, with room for possible ?? or << and ! at end
5508
5509 Msgl : Natural;
5510 Wmsg : Boolean;
5511 Eloc : Source_Ptr;
5512
5513 -- Start of processing for Compile_Time_Constraint_Error
5514
5515 begin
5516 -- If this is a warning, convert it into an error if we are in code
5517 -- subject to SPARK_Mode being set On, unless Warn is True to force a
5518 -- warning. The rationale is that a compile-time constraint error should
5519 -- lead to an error instead of a warning when SPARK_Mode is On, but in
5520 -- a few cases we prefer to issue a warning and generate both a suitable
5521 -- run-time error in GNAT and a suitable check message in GNATprove.
5522 -- Those cases are those that likely correspond to deactivated SPARK
5523 -- code, so that this kind of code can be compiled and analyzed instead
5524 -- of being rejected.
5525
5526 Error_Msg_Warn := Warn or SPARK_Mode /= On;
5527
5528 -- A static constraint error in an instance body is not a fatal error.
5529 -- we choose to inhibit the message altogether, because there is no
5530 -- obvious node (for now) on which to post it. On the other hand the
5531 -- offending node must be replaced with a constraint_error in any case.
5532
5533 -- No messages are generated if we already posted an error on this node
5534
5535 if not Error_Posted (N) then
5536 if Loc /= No_Location then
5537 Eloc := Loc;
5538 else
5539 Eloc := Sloc (N);
5540 end if;
5541
5542 -- Copy message to Msgc, converting any ? in the message into <
5543 -- instead, so that we have an error in GNATprove mode.
5544
5545 Msgl := Msg'Length;
5546
5547 for J in 1 .. Msgl loop
5548 if Msg (J) = '?' and then (J = 1 or else Msg (J - 1) /= ''') then
5549 Msgc (J) := '<';
5550 else
5551 Msgc (J) := Msg (J);
5552 end if;
5553 end loop;
5554
5555 -- Message is a warning, even in Ada 95 case
5556
5557 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
5558 Wmsg := True;
5559
5560 -- In Ada 83, all messages are warnings. In the private part and the
5561 -- body of an instance, constraint_checks are only warnings. We also
5562 -- make this a warning if the Warn parameter is set.
5563
5564 elsif Warn
5565 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
5566 or else In_Instance_Not_Visible
5567 then
5568 Msgl := Msgl + 1;
5569 Msgc (Msgl) := '<';
5570 Msgl := Msgl + 1;
5571 Msgc (Msgl) := '<';
5572 Wmsg := True;
5573
5574 -- Otherwise we have a real error message (Ada 95 static case) and we
5575 -- make this an unconditional message. Note that in the warning case
5576 -- we do not make the message unconditional, it seems reasonable to
5577 -- delete messages like this (about exceptions that will be raised)
5578 -- in dead code.
5579
5580 else
5581 Wmsg := False;
5582 Msgl := Msgl + 1;
5583 Msgc (Msgl) := '!';
5584 end if;
5585
5586 -- One more test, skip the warning if the related expression is
5587 -- statically unevaluated, since we don't want to warn about what
5588 -- will happen when something is evaluated if it never will be
5589 -- evaluated.
5590
5591 if not Is_Statically_Unevaluated (N) then
5592 if Present (Ent) then
5593 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
5594 else
5595 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
5596 end if;
5597
5598 -- Emit any extra message as a continuation
5599
5600 if Extra_Msg /= "" then
5601 Error_Msg_N ('\' & Extra_Msg, N);
5602 end if;
5603
5604 if Wmsg then
5605
5606 -- Check whether the context is an Init_Proc
5607
5608 if Inside_Init_Proc then
5609 declare
5610 Conc_Typ : constant Entity_Id :=
5611 Corresponding_Concurrent_Type
5612 (Entity (Parameter_Type (First
5613 (Parameter_Specifications
5614 (Parent (Current_Scope))))));
5615
5616 begin
5617 -- Don't complain if the corresponding concurrent type
5618 -- doesn't come from source (i.e. a single task/protected
5619 -- object).
5620
5621 if Present (Conc_Typ)
5622 and then not Comes_From_Source (Conc_Typ)
5623 then
5624 Error_Msg_NEL
5625 ("\& [<<", N, Standard_Constraint_Error, Eloc);
5626
5627 else
5628 if GNATprove_Mode then
5629 Error_Msg_NEL
5630 ("\& would have been raised for objects of this "
5631 & "type", N, Standard_Constraint_Error, Eloc);
5632 else
5633 Error_Msg_NEL
5634 ("\& will be raised for objects of this type??",
5635 N, Standard_Constraint_Error, Eloc);
5636 end if;
5637 end if;
5638 end;
5639
5640 else
5641 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
5642 end if;
5643
5644 else
5645 Error_Msg ("\static expression fails Constraint_Check", Eloc);
5646 Set_Error_Posted (N);
5647 end if;
5648 end if;
5649 end if;
5650
5651 return N;
5652 end Compile_Time_Constraint_Error;
5653
5654 -----------------------
5655 -- Conditional_Delay --
5656 -----------------------
5657
5658 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
5659 begin
5660 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
5661 Set_Has_Delayed_Freeze (New_Ent);
5662 end if;
5663 end Conditional_Delay;
5664
5665 -------------------------
5666 -- Copy_Component_List --
5667 -------------------------
5668
5669 function Copy_Component_List
5670 (R_Typ : Entity_Id;
5671 Loc : Source_Ptr) return List_Id
5672 is
5673 Comp : Node_Id;
5674 Comps : constant List_Id := New_List;
5675
5676 begin
5677 Comp := First_Component (Underlying_Type (R_Typ));
5678 while Present (Comp) loop
5679 if Comes_From_Source (Comp) then
5680 declare
5681 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
5682 begin
5683 Append_To (Comps,
5684 Make_Component_Declaration (Loc,
5685 Defining_Identifier =>
5686 Make_Defining_Identifier (Loc, Chars (Comp)),
5687 Component_Definition =>
5688 New_Copy_Tree
5689 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
5690 end;
5691 end if;
5692
5693 Next_Component (Comp);
5694 end loop;
5695
5696 return Comps;
5697 end Copy_Component_List;
5698
5699 -------------------------
5700 -- Copy_Parameter_List --
5701 -------------------------
5702
5703 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
5704 Loc : constant Source_Ptr := Sloc (Subp_Id);
5705 Plist : List_Id;
5706 Formal : Entity_Id;
5707
5708 begin
5709 if No (First_Formal (Subp_Id)) then
5710 return No_List;
5711 else
5712 Plist := New_List;
5713 Formal := First_Formal (Subp_Id);
5714 while Present (Formal) loop
5715 Append_To (Plist,
5716 Make_Parameter_Specification (Loc,
5717 Defining_Identifier =>
5718 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
5719 In_Present => In_Present (Parent (Formal)),
5720 Out_Present => Out_Present (Parent (Formal)),
5721 Parameter_Type =>
5722 New_Occurrence_Of (Etype (Formal), Loc),
5723 Expression =>
5724 New_Copy_Tree (Expression (Parent (Formal)))));
5725
5726 Next_Formal (Formal);
5727 end loop;
5728 end if;
5729
5730 return Plist;
5731 end Copy_Parameter_List;
5732
5733 ----------------------------
5734 -- Copy_SPARK_Mode_Aspect --
5735 ----------------------------
5736
5737 procedure Copy_SPARK_Mode_Aspect (From : Node_Id; To : Node_Id) is
5738 pragma Assert (not Has_Aspects (To));
5739 Asp : Node_Id;
5740
5741 begin
5742 if Has_Aspects (From) then
5743 Asp := Find_Aspect (Defining_Entity (From), Aspect_SPARK_Mode);
5744
5745 if Present (Asp) then
5746 Set_Aspect_Specifications (To, New_List (New_Copy_Tree (Asp)));
5747 Set_Has_Aspects (To, True);
5748 end if;
5749 end if;
5750 end Copy_SPARK_Mode_Aspect;
5751
5752 --------------------------
5753 -- Copy_Subprogram_Spec --
5754 --------------------------
5755
5756 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
5757 Def_Id : Node_Id;
5758 Formal_Spec : Node_Id;
5759 Result : Node_Id;
5760
5761 begin
5762 -- The structure of the original tree must be replicated without any
5763 -- alterations. Use New_Copy_Tree for this purpose.
5764
5765 Result := New_Copy_Tree (Spec);
5766
5767 -- However, the spec of a null procedure carries the corresponding null
5768 -- statement of the body (created by the parser), and this cannot be
5769 -- shared with the new subprogram spec.
5770
5771 if Nkind (Result) = N_Procedure_Specification then
5772 Set_Null_Statement (Result, Empty);
5773 end if;
5774
5775 -- Create a new entity for the defining unit name
5776
5777 Def_Id := Defining_Unit_Name (Result);
5778 Set_Defining_Unit_Name (Result,
5779 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5780
5781 -- Create new entities for the formal parameters
5782
5783 if Present (Parameter_Specifications (Result)) then
5784 Formal_Spec := First (Parameter_Specifications (Result));
5785 while Present (Formal_Spec) loop
5786 Def_Id := Defining_Identifier (Formal_Spec);
5787 Set_Defining_Identifier (Formal_Spec,
5788 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5789
5790 Next (Formal_Spec);
5791 end loop;
5792 end if;
5793
5794 return Result;
5795 end Copy_Subprogram_Spec;
5796
5797 --------------------------------
5798 -- Corresponding_Generic_Type --
5799 --------------------------------
5800
5801 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
5802 Inst : Entity_Id;
5803 Gen : Entity_Id;
5804 Typ : Entity_Id;
5805
5806 begin
5807 if not Is_Generic_Actual_Type (T) then
5808 return Any_Type;
5809
5810 -- If the actual is the actual of an enclosing instance, resolution
5811 -- was correct in the generic.
5812
5813 elsif Nkind (Parent (T)) = N_Subtype_Declaration
5814 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
5815 and then
5816 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
5817 then
5818 return Any_Type;
5819
5820 else
5821 Inst := Scope (T);
5822
5823 if Is_Wrapper_Package (Inst) then
5824 Inst := Related_Instance (Inst);
5825 end if;
5826
5827 Gen :=
5828 Generic_Parent
5829 (Specification (Unit_Declaration_Node (Inst)));
5830
5831 -- Generic actual has the same name as the corresponding formal
5832
5833 Typ := First_Entity (Gen);
5834 while Present (Typ) loop
5835 if Chars (Typ) = Chars (T) then
5836 return Typ;
5837 end if;
5838
5839 Next_Entity (Typ);
5840 end loop;
5841
5842 return Any_Type;
5843 end if;
5844 end Corresponding_Generic_Type;
5845
5846 --------------------
5847 -- Current_Entity --
5848 --------------------
5849
5850 -- The currently visible definition for a given identifier is the
5851 -- one most chained at the start of the visibility chain, i.e. the
5852 -- one that is referenced by the Node_Id value of the name of the
5853 -- given identifier.
5854
5855 function Current_Entity (N : Node_Id) return Entity_Id is
5856 begin
5857 return Get_Name_Entity_Id (Chars (N));
5858 end Current_Entity;
5859
5860 -----------------------------
5861 -- Current_Entity_In_Scope --
5862 -----------------------------
5863
5864 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
5865 E : Entity_Id;
5866 CS : constant Entity_Id := Current_Scope;
5867
5868 Transient_Case : constant Boolean := Scope_Is_Transient;
5869
5870 begin
5871 E := Get_Name_Entity_Id (Chars (N));
5872 while Present (E)
5873 and then Scope (E) /= CS
5874 and then (not Transient_Case or else Scope (E) /= Scope (CS))
5875 loop
5876 E := Homonym (E);
5877 end loop;
5878
5879 return E;
5880 end Current_Entity_In_Scope;
5881
5882 -------------------
5883 -- Current_Scope --
5884 -------------------
5885
5886 function Current_Scope return Entity_Id is
5887 begin
5888 if Scope_Stack.Last = -1 then
5889 return Standard_Standard;
5890 else
5891 declare
5892 C : constant Entity_Id :=
5893 Scope_Stack.Table (Scope_Stack.Last).Entity;
5894 begin
5895 if Present (C) then
5896 return C;
5897 else
5898 return Standard_Standard;
5899 end if;
5900 end;
5901 end if;
5902 end Current_Scope;
5903
5904 ----------------------------
5905 -- Current_Scope_No_Loops --
5906 ----------------------------
5907
5908 function Current_Scope_No_Loops return Entity_Id is
5909 S : Entity_Id;
5910
5911 begin
5912 -- Examine the scope stack starting from the current scope and skip any
5913 -- internally generated loops.
5914
5915 S := Current_Scope;
5916 while Present (S) and then S /= Standard_Standard loop
5917 if Ekind (S) = E_Loop and then not Comes_From_Source (S) then
5918 S := Scope (S);
5919 else
5920 exit;
5921 end if;
5922 end loop;
5923
5924 return S;
5925 end Current_Scope_No_Loops;
5926
5927 ------------------------
5928 -- Current_Subprogram --
5929 ------------------------
5930
5931 function Current_Subprogram return Entity_Id is
5932 Scop : constant Entity_Id := Current_Scope;
5933 begin
5934 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
5935 return Scop;
5936 else
5937 return Enclosing_Subprogram (Scop);
5938 end if;
5939 end Current_Subprogram;
5940
5941 ----------------------------------
5942 -- Deepest_Type_Access_Level --
5943 ----------------------------------
5944
5945 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
5946 begin
5947 if Ekind (Typ) = E_Anonymous_Access_Type
5948 and then not Is_Local_Anonymous_Access (Typ)
5949 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
5950 then
5951 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
5952 -- access type.
5953
5954 return
5955 Scope_Depth (Enclosing_Dynamic_Scope
5956 (Defining_Identifier
5957 (Associated_Node_For_Itype (Typ))));
5958
5959 -- For generic formal type, return Int'Last (infinite).
5960 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
5961
5962 elsif Is_Generic_Type (Root_Type (Typ)) then
5963 return UI_From_Int (Int'Last);
5964
5965 else
5966 return Type_Access_Level (Typ);
5967 end if;
5968 end Deepest_Type_Access_Level;
5969
5970 ---------------------
5971 -- Defining_Entity --
5972 ---------------------
5973
5974 function Defining_Entity (N : Node_Id) return Entity_Id is
5975 begin
5976 case Nkind (N) is
5977 when N_Abstract_Subprogram_Declaration
5978 | N_Expression_Function
5979 | N_Formal_Subprogram_Declaration
5980 | N_Generic_Package_Declaration
5981 | N_Generic_Subprogram_Declaration
5982 | N_Package_Declaration
5983 | N_Subprogram_Body
5984 | N_Subprogram_Body_Stub
5985 | N_Subprogram_Declaration
5986 | N_Subprogram_Renaming_Declaration
5987 =>
5988 return Defining_Entity (Specification (N));
5989
5990 when N_Component_Declaration
5991 | N_Defining_Program_Unit_Name
5992 | N_Discriminant_Specification
5993 | N_Entry_Body
5994 | N_Entry_Declaration
5995 | N_Entry_Index_Specification
5996 | N_Exception_Declaration
5997 | N_Exception_Renaming_Declaration
5998 | N_Formal_Object_Declaration
5999 | N_Formal_Package_Declaration
6000 | N_Formal_Type_Declaration
6001 | N_Full_Type_Declaration
6002 | N_Implicit_Label_Declaration
6003 | N_Incomplete_Type_Declaration
6004 | N_Iterator_Specification
6005 | N_Loop_Parameter_Specification
6006 | N_Number_Declaration
6007 | N_Object_Declaration
6008 | N_Object_Renaming_Declaration
6009 | N_Package_Body_Stub
6010 | N_Parameter_Specification
6011 | N_Private_Extension_Declaration
6012 | N_Private_Type_Declaration
6013 | N_Protected_Body
6014 | N_Protected_Body_Stub
6015 | N_Protected_Type_Declaration
6016 | N_Single_Protected_Declaration
6017 | N_Single_Task_Declaration
6018 | N_Subtype_Declaration
6019 | N_Task_Body
6020 | N_Task_Body_Stub
6021 | N_Task_Type_Declaration
6022 =>
6023 return Defining_Identifier (N);
6024
6025 when N_Compilation_Unit =>
6026 return Defining_Entity (Unit (N));
6027
6028 when N_Subunit =>
6029 return Defining_Entity (Proper_Body (N));
6030
6031 when N_Function_Instantiation
6032 | N_Function_Specification
6033 | N_Generic_Function_Renaming_Declaration
6034 | N_Generic_Package_Renaming_Declaration
6035 | N_Generic_Procedure_Renaming_Declaration
6036 | N_Package_Body
6037 | N_Package_Instantiation
6038 | N_Package_Renaming_Declaration
6039 | N_Package_Specification
6040 | N_Procedure_Instantiation
6041 | N_Procedure_Specification
6042 =>
6043 declare
6044 Nam : constant Node_Id := Defining_Unit_Name (N);
6045 Err : Entity_Id := Empty;
6046
6047 begin
6048 if Nkind (Nam) in N_Entity then
6049 return Nam;
6050
6051 -- For Error, make up a name and attach to declaration so we
6052 -- can continue semantic analysis.
6053
6054 elsif Nam = Error then
6055 Err := Make_Temporary (Sloc (N), 'T');
6056 Set_Defining_Unit_Name (N, Err);
6057
6058 return Err;
6059
6060 -- If not an entity, get defining identifier
6061
6062 else
6063 return Defining_Identifier (Nam);
6064 end if;
6065 end;
6066
6067 when N_Block_Statement
6068 | N_Loop_Statement
6069 =>
6070 return Entity (Identifier (N));
6071
6072 when others =>
6073 raise Program_Error;
6074 end case;
6075 end Defining_Entity;
6076
6077 --------------------------
6078 -- Denotes_Discriminant --
6079 --------------------------
6080
6081 function Denotes_Discriminant
6082 (N : Node_Id;
6083 Check_Concurrent : Boolean := False) return Boolean
6084 is
6085 E : Entity_Id;
6086
6087 begin
6088 if not Is_Entity_Name (N) or else No (Entity (N)) then
6089 return False;
6090 else
6091 E := Entity (N);
6092 end if;
6093
6094 -- If we are checking for a protected type, the discriminant may have
6095 -- been rewritten as the corresponding discriminal of the original type
6096 -- or of the corresponding concurrent record, depending on whether we
6097 -- are in the spec or body of the protected type.
6098
6099 return Ekind (E) = E_Discriminant
6100 or else
6101 (Check_Concurrent
6102 and then Ekind (E) = E_In_Parameter
6103 and then Present (Discriminal_Link (E))
6104 and then
6105 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
6106 or else
6107 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
6108 end Denotes_Discriminant;
6109
6110 -------------------------
6111 -- Denotes_Same_Object --
6112 -------------------------
6113
6114 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
6115 function Is_Renaming (N : Node_Id) return Boolean;
6116 -- Return true if N names a renaming entity
6117
6118 function Is_Valid_Renaming (N : Node_Id) return Boolean;
6119 -- For renamings, return False if the prefix of any dereference within
6120 -- the renamed object_name is a variable, or any expression within the
6121 -- renamed object_name contains references to variables or calls on
6122 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
6123
6124 -----------------
6125 -- Is_Renaming --
6126 -----------------
6127
6128 function Is_Renaming (N : Node_Id) return Boolean is
6129 begin
6130 return
6131 Is_Entity_Name (N) and then Present (Renamed_Entity (Entity (N)));
6132 end Is_Renaming;
6133
6134 -----------------------
6135 -- Is_Valid_Renaming --
6136 -----------------------
6137
6138 function Is_Valid_Renaming (N : Node_Id) return Boolean is
6139 function Check_Renaming (N : Node_Id) return Boolean;
6140 -- Recursive function used to traverse all the prefixes of N
6141
6142 --------------------
6143 -- Check_Renaming --
6144 --------------------
6145
6146 function Check_Renaming (N : Node_Id) return Boolean is
6147 begin
6148 if Is_Renaming (N)
6149 and then not Check_Renaming (Renamed_Entity (Entity (N)))
6150 then
6151 return False;
6152 end if;
6153
6154 if Nkind (N) = N_Indexed_Component then
6155 declare
6156 Indx : Node_Id;
6157
6158 begin
6159 Indx := First (Expressions (N));
6160 while Present (Indx) loop
6161 if not Is_OK_Static_Expression (Indx) then
6162 return False;
6163 end if;
6164
6165 Next_Index (Indx);
6166 end loop;
6167 end;
6168 end if;
6169
6170 if Has_Prefix (N) then
6171 declare
6172 P : constant Node_Id := Prefix (N);
6173
6174 begin
6175 if Nkind (N) = N_Explicit_Dereference
6176 and then Is_Variable (P)
6177 then
6178 return False;
6179
6180 elsif Is_Entity_Name (P)
6181 and then Ekind (Entity (P)) = E_Function
6182 then
6183 return False;
6184
6185 elsif Nkind (P) = N_Function_Call then
6186 return False;
6187 end if;
6188
6189 -- Recursion to continue traversing the prefix of the
6190 -- renaming expression
6191
6192 return Check_Renaming (P);
6193 end;
6194 end if;
6195
6196 return True;
6197 end Check_Renaming;
6198
6199 -- Start of processing for Is_Valid_Renaming
6200
6201 begin
6202 return Check_Renaming (N);
6203 end Is_Valid_Renaming;
6204
6205 -- Local variables
6206
6207 Obj1 : Node_Id := A1;
6208 Obj2 : Node_Id := A2;
6209
6210 -- Start of processing for Denotes_Same_Object
6211
6212 begin
6213 -- Both names statically denote the same stand-alone object or parameter
6214 -- (RM 6.4.1(6.5/3))
6215
6216 if Is_Entity_Name (Obj1)
6217 and then Is_Entity_Name (Obj2)
6218 and then Entity (Obj1) = Entity (Obj2)
6219 then
6220 return True;
6221 end if;
6222
6223 -- For renamings, the prefix of any dereference within the renamed
6224 -- object_name is not a variable, and any expression within the
6225 -- renamed object_name contains no references to variables nor
6226 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
6227
6228 if Is_Renaming (Obj1) then
6229 if Is_Valid_Renaming (Obj1) then
6230 Obj1 := Renamed_Entity (Entity (Obj1));
6231 else
6232 return False;
6233 end if;
6234 end if;
6235
6236 if Is_Renaming (Obj2) then
6237 if Is_Valid_Renaming (Obj2) then
6238 Obj2 := Renamed_Entity (Entity (Obj2));
6239 else
6240 return False;
6241 end if;
6242 end if;
6243
6244 -- No match if not same node kind (such cases are handled by
6245 -- Denotes_Same_Prefix)
6246
6247 if Nkind (Obj1) /= Nkind (Obj2) then
6248 return False;
6249
6250 -- After handling valid renamings, one of the two names statically
6251 -- denoted a renaming declaration whose renamed object_name is known
6252 -- to denote the same object as the other (RM 6.4.1(6.10/3))
6253
6254 elsif Is_Entity_Name (Obj1) then
6255 if Is_Entity_Name (Obj2) then
6256 return Entity (Obj1) = Entity (Obj2);
6257 else
6258 return False;
6259 end if;
6260
6261 -- Both names are selected_components, their prefixes are known to
6262 -- denote the same object, and their selector_names denote the same
6263 -- component (RM 6.4.1(6.6/3)).
6264
6265 elsif Nkind (Obj1) = N_Selected_Component then
6266 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
6267 and then
6268 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
6269
6270 -- Both names are dereferences and the dereferenced names are known to
6271 -- denote the same object (RM 6.4.1(6.7/3))
6272
6273 elsif Nkind (Obj1) = N_Explicit_Dereference then
6274 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
6275
6276 -- Both names are indexed_components, their prefixes are known to denote
6277 -- the same object, and each of the pairs of corresponding index values
6278 -- are either both static expressions with the same static value or both
6279 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
6280
6281 elsif Nkind (Obj1) = N_Indexed_Component then
6282 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
6283 return False;
6284 else
6285 declare
6286 Indx1 : Node_Id;
6287 Indx2 : Node_Id;
6288
6289 begin
6290 Indx1 := First (Expressions (Obj1));
6291 Indx2 := First (Expressions (Obj2));
6292 while Present (Indx1) loop
6293
6294 -- Indexes must denote the same static value or same object
6295
6296 if Is_OK_Static_Expression (Indx1) then
6297 if not Is_OK_Static_Expression (Indx2) then
6298 return False;
6299
6300 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
6301 return False;
6302 end if;
6303
6304 elsif not Denotes_Same_Object (Indx1, Indx2) then
6305 return False;
6306 end if;
6307
6308 Next (Indx1);
6309 Next (Indx2);
6310 end loop;
6311
6312 return True;
6313 end;
6314 end if;
6315
6316 -- Both names are slices, their prefixes are known to denote the same
6317 -- object, and the two slices have statically matching index constraints
6318 -- (RM 6.4.1(6.9/3))
6319
6320 elsif Nkind (Obj1) = N_Slice
6321 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
6322 then
6323 declare
6324 Lo1, Lo2, Hi1, Hi2 : Node_Id;
6325
6326 begin
6327 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
6328 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
6329
6330 -- Check whether bounds are statically identical. There is no
6331 -- attempt to detect partial overlap of slices.
6332
6333 return Denotes_Same_Object (Lo1, Lo2)
6334 and then
6335 Denotes_Same_Object (Hi1, Hi2);
6336 end;
6337
6338 -- In the recursion, literals appear as indexes
6339
6340 elsif Nkind (Obj1) = N_Integer_Literal
6341 and then
6342 Nkind (Obj2) = N_Integer_Literal
6343 then
6344 return Intval (Obj1) = Intval (Obj2);
6345
6346 else
6347 return False;
6348 end if;
6349 end Denotes_Same_Object;
6350
6351 -------------------------
6352 -- Denotes_Same_Prefix --
6353 -------------------------
6354
6355 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
6356 begin
6357 if Is_Entity_Name (A1) then
6358 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
6359 and then not Is_Access_Type (Etype (A1))
6360 then
6361 return Denotes_Same_Object (A1, Prefix (A2))
6362 or else Denotes_Same_Prefix (A1, Prefix (A2));
6363 else
6364 return False;
6365 end if;
6366
6367 elsif Is_Entity_Name (A2) then
6368 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
6369
6370 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
6371 and then
6372 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
6373 then
6374 declare
6375 Root1, Root2 : Node_Id;
6376 Depth1, Depth2 : Nat := 0;
6377
6378 begin
6379 Root1 := Prefix (A1);
6380 while not Is_Entity_Name (Root1) loop
6381 if not Nkind_In
6382 (Root1, N_Selected_Component, N_Indexed_Component)
6383 then
6384 return False;
6385 else
6386 Root1 := Prefix (Root1);
6387 end if;
6388
6389 Depth1 := Depth1 + 1;
6390 end loop;
6391
6392 Root2 := Prefix (A2);
6393 while not Is_Entity_Name (Root2) loop
6394 if not Nkind_In (Root2, N_Selected_Component,
6395 N_Indexed_Component)
6396 then
6397 return False;
6398 else
6399 Root2 := Prefix (Root2);
6400 end if;
6401
6402 Depth2 := Depth2 + 1;
6403 end loop;
6404
6405 -- If both have the same depth and they do not denote the same
6406 -- object, they are disjoint and no warning is needed.
6407
6408 if Depth1 = Depth2 then
6409 return False;
6410
6411 elsif Depth1 > Depth2 then
6412 Root1 := Prefix (A1);
6413 for J in 1 .. Depth1 - Depth2 - 1 loop
6414 Root1 := Prefix (Root1);
6415 end loop;
6416
6417 return Denotes_Same_Object (Root1, A2);
6418
6419 else
6420 Root2 := Prefix (A2);
6421 for J in 1 .. Depth2 - Depth1 - 1 loop
6422 Root2 := Prefix (Root2);
6423 end loop;
6424
6425 return Denotes_Same_Object (A1, Root2);
6426 end if;
6427 end;
6428
6429 else
6430 return False;
6431 end if;
6432 end Denotes_Same_Prefix;
6433
6434 ----------------------
6435 -- Denotes_Variable --
6436 ----------------------
6437
6438 function Denotes_Variable (N : Node_Id) return Boolean is
6439 begin
6440 return Is_Variable (N) and then Paren_Count (N) = 0;
6441 end Denotes_Variable;
6442
6443 -----------------------------
6444 -- Depends_On_Discriminant --
6445 -----------------------------
6446
6447 function Depends_On_Discriminant (N : Node_Id) return Boolean is
6448 L : Node_Id;
6449 H : Node_Id;
6450
6451 begin
6452 Get_Index_Bounds (N, L, H);
6453 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
6454 end Depends_On_Discriminant;
6455
6456 -------------------------
6457 -- Designate_Same_Unit --
6458 -------------------------
6459
6460 function Designate_Same_Unit
6461 (Name1 : Node_Id;
6462 Name2 : Node_Id) return Boolean
6463 is
6464 K1 : constant Node_Kind := Nkind (Name1);
6465 K2 : constant Node_Kind := Nkind (Name2);
6466
6467 function Prefix_Node (N : Node_Id) return Node_Id;
6468 -- Returns the parent unit name node of a defining program unit name
6469 -- or the prefix if N is a selected component or an expanded name.
6470
6471 function Select_Node (N : Node_Id) return Node_Id;
6472 -- Returns the defining identifier node of a defining program unit
6473 -- name or the selector node if N is a selected component or an
6474 -- expanded name.
6475
6476 -----------------
6477 -- Prefix_Node --
6478 -----------------
6479
6480 function Prefix_Node (N : Node_Id) return Node_Id is
6481 begin
6482 if Nkind (N) = N_Defining_Program_Unit_Name then
6483 return Name (N);
6484 else
6485 return Prefix (N);
6486 end if;
6487 end Prefix_Node;
6488
6489 -----------------
6490 -- Select_Node --
6491 -----------------
6492
6493 function Select_Node (N : Node_Id) return Node_Id is
6494 begin
6495 if Nkind (N) = N_Defining_Program_Unit_Name then
6496 return Defining_Identifier (N);
6497 else
6498 return Selector_Name (N);
6499 end if;
6500 end Select_Node;
6501
6502 -- Start of processing for Designate_Same_Unit
6503
6504 begin
6505 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
6506 and then
6507 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
6508 then
6509 return Chars (Name1) = Chars (Name2);
6510
6511 elsif Nkind_In (K1, N_Expanded_Name,
6512 N_Selected_Component,
6513 N_Defining_Program_Unit_Name)
6514 and then
6515 Nkind_In (K2, N_Expanded_Name,
6516 N_Selected_Component,
6517 N_Defining_Program_Unit_Name)
6518 then
6519 return
6520 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
6521 and then
6522 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
6523
6524 else
6525 return False;
6526 end if;
6527 end Designate_Same_Unit;
6528
6529 ---------------------------------------------
6530 -- Diagnose_Iterated_Component_Association --
6531 ---------------------------------------------
6532
6533 procedure Diagnose_Iterated_Component_Association (N : Node_Id) is
6534 Def_Id : constant Entity_Id := Defining_Identifier (N);
6535 Aggr : Node_Id;
6536
6537 begin
6538 -- Determine whether the iterated component association appears within
6539 -- an aggregate. If this is the case, raise Program_Error because the
6540 -- iterated component association cannot be left in the tree as is and
6541 -- must always be processed by the related aggregate.
6542
6543 Aggr := N;
6544 while Present (Aggr) loop
6545 if Nkind (Aggr) = N_Aggregate then
6546 raise Program_Error;
6547
6548 -- Prevent the search from going too far
6549
6550 elsif Is_Body_Or_Package_Declaration (Aggr) then
6551 exit;
6552 end if;
6553
6554 Aggr := Parent (Aggr);
6555 end loop;
6556
6557 -- At this point it is known that the iterated component association is
6558 -- not within an aggregate. This is really a quantified expression with
6559 -- a missing "all" or "some" quantifier.
6560
6561 Error_Msg_N ("missing quantifier", Def_Id);
6562
6563 -- Rewrite the iterated component association as True to prevent any
6564 -- cascaded errors.
6565
6566 Rewrite (N, New_Occurrence_Of (Standard_True, Sloc (N)));
6567 Analyze (N);
6568 end Diagnose_Iterated_Component_Association;
6569
6570 ---------------------------------
6571 -- Dynamic_Accessibility_Level --
6572 ---------------------------------
6573
6574 function Dynamic_Accessibility_Level (N : Node_Id) return Node_Id is
6575 Loc : constant Source_Ptr := Sloc (N);
6576
6577 function Make_Level_Literal (Level : Uint) return Node_Id;
6578 -- Construct an integer literal representing an accessibility level
6579 -- with its type set to Natural.
6580
6581 ------------------------
6582 -- Make_Level_Literal --
6583 ------------------------
6584
6585 function Make_Level_Literal (Level : Uint) return Node_Id is
6586 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
6587
6588 begin
6589 Set_Etype (Result, Standard_Natural);
6590 return Result;
6591 end Make_Level_Literal;
6592
6593 -- Local variables
6594
6595 Expr : Node_Id := Original_Node (N);
6596 -- Expr references the original node because at this stage N may be the
6597 -- reference to a variable internally created by the frontend to remove
6598 -- side effects of an expression.
6599
6600 E : Entity_Id;
6601
6602 -- Start of processing for Dynamic_Accessibility_Level
6603
6604 begin
6605 if Is_Entity_Name (Expr) then
6606 E := Entity (Expr);
6607
6608 if Present (Renamed_Object (E)) then
6609 return Dynamic_Accessibility_Level (Renamed_Object (E));
6610 end if;
6611
6612 if (Is_Formal (E)
6613 or else Ekind_In (E, E_Variable, E_Constant))
6614 and then Present (Get_Accessibility (E))
6615 then
6616 return New_Occurrence_Of (Get_Accessibility (E), Loc);
6617 end if;
6618 end if;
6619
6620 -- Handle a constant-folded conditional expression by avoiding use of
6621 -- the original node.
6622
6623 if Nkind_In (Expr, N_Case_Expression, N_If_Expression) then
6624 Expr := N;
6625 end if;
6626
6627 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
6628
6629 case Nkind (Expr) is
6630 -- It may be possible that we have an access object denoted by an
6631 -- attribute reference for 'Loop_Entry which may, in turn, have an
6632 -- indexed component representing a loop identifier.
6633
6634 -- In this case we must climb up the indexed component and set expr
6635 -- to the attribute reference so the rest of the machinery can
6636 -- operate as expected.
6637
6638 when N_Indexed_Component =>
6639 if Nkind (Prefix (Expr)) = N_Attribute_Reference
6640 and then Get_Attribute_Id (Attribute_Name (Prefix (Expr)))
6641 = Attribute_Loop_Entry
6642 then
6643 Expr := Prefix (Expr);
6644 end if;
6645
6646 -- For access discriminant, the level of the enclosing object
6647
6648 when N_Selected_Component =>
6649 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
6650 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
6651 E_Anonymous_Access_Type
6652 then
6653 return Make_Level_Literal (Object_Access_Level (Expr));
6654 end if;
6655
6656 when N_Attribute_Reference =>
6657 case Get_Attribute_Id (Attribute_Name (Expr)) is
6658
6659 -- Ignore 'Loop_Entry, 'Result, and 'Old as they can be used to
6660 -- identify access objects and do not have an effect on
6661 -- accessibility level.
6662
6663 when Attribute_Loop_Entry | Attribute_Old | Attribute_Result =>
6664 null;
6665
6666 -- For X'Access, the level of the prefix X
6667
6668 when Attribute_Access =>
6669 return Make_Level_Literal
6670 (Object_Access_Level (Prefix (Expr)));
6671
6672 -- Treat the unchecked attributes as library-level
6673
6674 when Attribute_Unchecked_Access
6675 | Attribute_Unrestricted_Access
6676 =>
6677 return Make_Level_Literal (Scope_Depth (Standard_Standard));
6678
6679 -- No other access-valued attributes
6680
6681 when others =>
6682 raise Program_Error;
6683 end case;
6684
6685 when N_Allocator =>
6686
6687 -- This is not fully implemented since it depends on context (see
6688 -- 3.10.2(14/3-14.2/3). More work is needed in the following cases
6689 --
6690 -- 1) For an anonymous allocator defining the value of an access
6691 -- parameter, the accessibility level is that of the innermost
6692 -- master of the call; however currently we pass the level of
6693 -- execution of the called subprogram, which is one greater
6694 -- than the current scope level (see Expand_Call_Helper).
6695 --
6696 -- For example, a statement is a master and a declaration is
6697 -- not a master; so we should not pass in the same level for
6698 -- the following cases:
6699 --
6700 -- function F (X : access Integer) return T is ... ;
6701 -- Decl : T := F (new Integer); -- level is off by one
6702 -- begin
6703 -- Decl := F (new Integer); -- we get this case right
6704 --
6705 -- 2) For an anonymous allocator that defines the result of a
6706 -- function with an access result, the accessibility level is
6707 -- determined as though the allocator were in place of the call
6708 -- of the function. In the special case of a call that is the
6709 -- operand of a type conversion the level is that of the target
6710 -- access type of the conversion.
6711 --
6712 -- 3) For an anonymous allocator defining an access discriminant
6713 -- the accessibility level is determined as follows:
6714 -- * for an allocator used to define the discriminant of an
6715 -- object, the level of the object
6716 -- * for an allocator used to define the constraint in a
6717 -- subtype_indication in any other context, the level of
6718 -- the master that elaborates the subtype_indication.
6719
6720 case Nkind (Parent (N)) is
6721 when N_Object_Declaration =>
6722
6723 -- For an anonymous allocator whose type is that of a
6724 -- stand-alone object of an anonymous access-to-object type,
6725 -- the accessibility level is that of the declaration of the
6726 -- stand-alone object.
6727
6728 return
6729 Make_Level_Literal
6730 (Object_Access_Level
6731 (Defining_Identifier (Parent (N))));
6732
6733 when N_Assignment_Statement =>
6734 return
6735 Make_Level_Literal
6736 (Object_Access_Level (Name (Parent (N))));
6737
6738 when others =>
6739 declare
6740 S : constant String :=
6741 Node_Kind'Image (Nkind (Parent (N)));
6742 begin
6743 Error_Msg_Strlen := S'Length;
6744 Error_Msg_String (1 .. Error_Msg_Strlen) := S;
6745 Error_Msg_N
6746 ("unsupported context for anonymous allocator (~)",
6747 Parent (N));
6748 end;
6749 end case;
6750
6751 when N_Type_Conversion =>
6752 if not Is_Local_Anonymous_Access (Etype (Expr)) then
6753
6754 -- Handle type conversions introduced for a rename of an
6755 -- Ada 2012 stand-alone object of an anonymous access type.
6756
6757 return Dynamic_Accessibility_Level (Expression (Expr));
6758 end if;
6759
6760 when others =>
6761 null;
6762 end case;
6763
6764 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
6765 end Dynamic_Accessibility_Level;
6766
6767 ------------------------
6768 -- Discriminated_Size --
6769 ------------------------
6770
6771 function Discriminated_Size (Comp : Entity_Id) return Boolean is
6772 function Non_Static_Bound (Bound : Node_Id) return Boolean;
6773 -- Check whether the bound of an index is non-static and does denote
6774 -- a discriminant, in which case any object of the type (protected or
6775 -- otherwise) will have a non-static size.
6776
6777 ----------------------
6778 -- Non_Static_Bound --
6779 ----------------------
6780
6781 function Non_Static_Bound (Bound : Node_Id) return Boolean is
6782 begin
6783 if Is_OK_Static_Expression (Bound) then
6784 return False;
6785
6786 -- If the bound is given by a discriminant it is non-static
6787 -- (A static constraint replaces the reference with the value).
6788 -- In an protected object the discriminant has been replaced by
6789 -- the corresponding discriminal within the protected operation.
6790
6791 elsif Is_Entity_Name (Bound)
6792 and then
6793 (Ekind (Entity (Bound)) = E_Discriminant
6794 or else Present (Discriminal_Link (Entity (Bound))))
6795 then
6796 return False;
6797
6798 else
6799 return True;
6800 end if;
6801 end Non_Static_Bound;
6802
6803 -- Local variables
6804
6805 Typ : constant Entity_Id := Etype (Comp);
6806 Index : Node_Id;
6807
6808 -- Start of processing for Discriminated_Size
6809
6810 begin
6811 if not Is_Array_Type (Typ) then
6812 return False;
6813 end if;
6814
6815 if Ekind (Typ) = E_Array_Subtype then
6816 Index := First_Index (Typ);
6817 while Present (Index) loop
6818 if Non_Static_Bound (Low_Bound (Index))
6819 or else Non_Static_Bound (High_Bound (Index))
6820 then
6821 return False;
6822 end if;
6823
6824 Next_Index (Index);
6825 end loop;
6826
6827 return True;
6828 end if;
6829
6830 return False;
6831 end Discriminated_Size;
6832
6833 -----------------------------------
6834 -- Effective_Extra_Accessibility --
6835 -----------------------------------
6836
6837 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
6838 begin
6839 if Present (Renamed_Object (Id))
6840 and then Is_Entity_Name (Renamed_Object (Id))
6841 then
6842 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
6843 else
6844 return Extra_Accessibility (Id);
6845 end if;
6846 end Effective_Extra_Accessibility;
6847
6848 -----------------------------
6849 -- Effective_Reads_Enabled --
6850 -----------------------------
6851
6852 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
6853 begin
6854 return Has_Enabled_Property (Id, Name_Effective_Reads);
6855 end Effective_Reads_Enabled;
6856
6857 ------------------------------
6858 -- Effective_Writes_Enabled --
6859 ------------------------------
6860
6861 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
6862 begin
6863 return Has_Enabled_Property (Id, Name_Effective_Writes);
6864 end Effective_Writes_Enabled;
6865
6866 ------------------------------
6867 -- Enclosing_Comp_Unit_Node --
6868 ------------------------------
6869
6870 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
6871 Current_Node : Node_Id;
6872
6873 begin
6874 Current_Node := N;
6875 while Present (Current_Node)
6876 and then Nkind (Current_Node) /= N_Compilation_Unit
6877 loop
6878 Current_Node := Parent (Current_Node);
6879 end loop;
6880
6881 if Nkind (Current_Node) /= N_Compilation_Unit then
6882 return Empty;
6883 else
6884 return Current_Node;
6885 end if;
6886 end Enclosing_Comp_Unit_Node;
6887
6888 --------------------------
6889 -- Enclosing_CPP_Parent --
6890 --------------------------
6891
6892 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
6893 Parent_Typ : Entity_Id := Typ;
6894
6895 begin
6896 while not Is_CPP_Class (Parent_Typ)
6897 and then Etype (Parent_Typ) /= Parent_Typ
6898 loop
6899 Parent_Typ := Etype (Parent_Typ);
6900
6901 if Is_Private_Type (Parent_Typ) then
6902 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6903 end if;
6904 end loop;
6905
6906 pragma Assert (Is_CPP_Class (Parent_Typ));
6907 return Parent_Typ;
6908 end Enclosing_CPP_Parent;
6909
6910 ---------------------------
6911 -- Enclosing_Declaration --
6912 ---------------------------
6913
6914 function Enclosing_Declaration (N : Node_Id) return Node_Id is
6915 Decl : Node_Id := N;
6916
6917 begin
6918 while Present (Decl)
6919 and then not (Nkind (Decl) in N_Declaration
6920 or else
6921 Nkind (Decl) in N_Later_Decl_Item
6922 or else
6923 Nkind (Decl) = N_Number_Declaration)
6924 loop
6925 Decl := Parent (Decl);
6926 end loop;
6927
6928 return Decl;
6929 end Enclosing_Declaration;
6930
6931 ----------------------------
6932 -- Enclosing_Generic_Body --
6933 ----------------------------
6934
6935 function Enclosing_Generic_Body (N : Node_Id) return Node_Id is
6936 Par : Node_Id;
6937 Spec_Id : Entity_Id;
6938
6939 begin
6940 Par := Parent (N);
6941 while Present (Par) loop
6942 if Nkind_In (Par, N_Package_Body, N_Subprogram_Body) then
6943 Spec_Id := Corresponding_Spec (Par);
6944
6945 if Present (Spec_Id)
6946 and then Nkind_In (Unit_Declaration_Node (Spec_Id),
6947 N_Generic_Package_Declaration,
6948 N_Generic_Subprogram_Declaration)
6949 then
6950 return Par;
6951 end if;
6952 end if;
6953
6954 Par := Parent (Par);
6955 end loop;
6956
6957 return Empty;
6958 end Enclosing_Generic_Body;
6959
6960 ----------------------------
6961 -- Enclosing_Generic_Unit --
6962 ----------------------------
6963
6964 function Enclosing_Generic_Unit (N : Node_Id) return Node_Id is
6965 Par : Node_Id;
6966 Spec_Decl : Node_Id;
6967 Spec_Id : Entity_Id;
6968
6969 begin
6970 Par := Parent (N);
6971 while Present (Par) loop
6972 if Nkind_In (Par, N_Generic_Package_Declaration,
6973 N_Generic_Subprogram_Declaration)
6974 then
6975 return Par;
6976
6977 elsif Nkind_In (Par, N_Package_Body, N_Subprogram_Body) then
6978 Spec_Id := Corresponding_Spec (Par);
6979
6980 if Present (Spec_Id) then
6981 Spec_Decl := Unit_Declaration_Node (Spec_Id);
6982
6983 if Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
6984 N_Generic_Subprogram_Declaration)
6985 then
6986 return Spec_Decl;
6987 end if;
6988 end if;
6989 end if;
6990
6991 Par := Parent (Par);
6992 end loop;
6993
6994 return Empty;
6995 end Enclosing_Generic_Unit;
6996
6997 -------------------------------
6998 -- Enclosing_Lib_Unit_Entity --
6999 -------------------------------
7000
7001 function Enclosing_Lib_Unit_Entity
7002 (E : Entity_Id := Current_Scope) return Entity_Id
7003 is
7004 Unit_Entity : Entity_Id;
7005
7006 begin
7007 -- Look for enclosing library unit entity by following scope links.
7008 -- Equivalent to, but faster than indexing through the scope stack.
7009
7010 Unit_Entity := E;
7011 while (Present (Scope (Unit_Entity))
7012 and then Scope (Unit_Entity) /= Standard_Standard)
7013 and not Is_Child_Unit (Unit_Entity)
7014 loop
7015 Unit_Entity := Scope (Unit_Entity);
7016 end loop;
7017
7018 return Unit_Entity;
7019 end Enclosing_Lib_Unit_Entity;
7020
7021 -----------------------------
7022 -- Enclosing_Lib_Unit_Node --
7023 -----------------------------
7024
7025 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
7026 Encl_Unit : Node_Id;
7027
7028 begin
7029 Encl_Unit := Enclosing_Comp_Unit_Node (N);
7030 while Present (Encl_Unit)
7031 and then Nkind (Unit (Encl_Unit)) = N_Subunit
7032 loop
7033 Encl_Unit := Library_Unit (Encl_Unit);
7034 end loop;
7035
7036 pragma Assert (Nkind (Encl_Unit) = N_Compilation_Unit);
7037 return Encl_Unit;
7038 end Enclosing_Lib_Unit_Node;
7039
7040 -----------------------
7041 -- Enclosing_Package --
7042 -----------------------
7043
7044 function Enclosing_Package (E : Entity_Id) return Entity_Id is
7045 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
7046
7047 begin
7048 if Dynamic_Scope = Standard_Standard then
7049 return Standard_Standard;
7050
7051 elsif Dynamic_Scope = Empty then
7052 return Empty;
7053
7054 elsif Ekind_In (Dynamic_Scope, E_Generic_Package,
7055 E_Package,
7056 E_Package_Body)
7057 then
7058 return Dynamic_Scope;
7059
7060 else
7061 return Enclosing_Package (Dynamic_Scope);
7062 end if;
7063 end Enclosing_Package;
7064
7065 -------------------------------------
7066 -- Enclosing_Package_Or_Subprogram --
7067 -------------------------------------
7068
7069 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
7070 S : Entity_Id;
7071
7072 begin
7073 S := Scope (E);
7074 while Present (S) loop
7075 if Is_Package_Or_Generic_Package (S)
7076 or else Is_Subprogram_Or_Generic_Subprogram (S)
7077 then
7078 return S;
7079
7080 else
7081 S := Scope (S);
7082 end if;
7083 end loop;
7084
7085 return Empty;
7086 end Enclosing_Package_Or_Subprogram;
7087
7088 --------------------------
7089 -- Enclosing_Subprogram --
7090 --------------------------
7091
7092 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
7093 Dyn_Scop : constant Entity_Id := Enclosing_Dynamic_Scope (E);
7094
7095 begin
7096 if Dyn_Scop = Standard_Standard then
7097 return Empty;
7098
7099 elsif Dyn_Scop = Empty then
7100 return Empty;
7101
7102 elsif Ekind (Dyn_Scop) = E_Subprogram_Body then
7103 return Corresponding_Spec (Parent (Parent (Dyn_Scop)));
7104
7105 elsif Ekind_In (Dyn_Scop, E_Block, E_Loop, E_Return_Statement) then
7106 return Enclosing_Subprogram (Dyn_Scop);
7107
7108 elsif Ekind_In (Dyn_Scop, E_Entry, E_Entry_Family) then
7109
7110 -- For a task entry or entry family, return the enclosing subprogram
7111 -- of the task itself.
7112
7113 if Ekind (Scope (Dyn_Scop)) = E_Task_Type then
7114 return Enclosing_Subprogram (Dyn_Scop);
7115
7116 -- A protected entry or entry family is rewritten as a protected
7117 -- procedure which is the desired enclosing subprogram. This is
7118 -- relevant when unnesting a procedure local to an entry body.
7119
7120 else
7121 return Protected_Body_Subprogram (Dyn_Scop);
7122 end if;
7123
7124 elsif Ekind (Dyn_Scop) = E_Task_Type then
7125 return Get_Task_Body_Procedure (Dyn_Scop);
7126
7127 -- The scope may appear as a private type or as a private extension
7128 -- whose completion is a task or protected type.
7129
7130 elsif Ekind_In (Dyn_Scop, E_Limited_Private_Type,
7131 E_Record_Type_With_Private)
7132 and then Present (Full_View (Dyn_Scop))
7133 and then Ekind_In (Full_View (Dyn_Scop), E_Task_Type, E_Protected_Type)
7134 then
7135 return Get_Task_Body_Procedure (Full_View (Dyn_Scop));
7136
7137 -- No body is generated if the protected operation is eliminated
7138
7139 elsif Convention (Dyn_Scop) = Convention_Protected
7140 and then not Is_Eliminated (Dyn_Scop)
7141 and then Present (Protected_Body_Subprogram (Dyn_Scop))
7142 then
7143 return Protected_Body_Subprogram (Dyn_Scop);
7144
7145 else
7146 return Dyn_Scop;
7147 end if;
7148 end Enclosing_Subprogram;
7149
7150 --------------------------
7151 -- End_Keyword_Location --
7152 --------------------------
7153
7154 function End_Keyword_Location (N : Node_Id) return Source_Ptr is
7155 function End_Label_Loc (Nod : Node_Id) return Source_Ptr;
7156 -- Return the source location of Nod's end label according to the
7157 -- following precedence rules:
7158 --
7159 -- 1) If the end label exists, return its location
7160 -- 2) If Nod exists, return its location
7161 -- 3) Return the location of N
7162
7163 -------------------
7164 -- End_Label_Loc --
7165 -------------------
7166
7167 function End_Label_Loc (Nod : Node_Id) return Source_Ptr is
7168 Label : Node_Id;
7169
7170 begin
7171 if Present (Nod) then
7172 Label := End_Label (Nod);
7173
7174 if Present (Label) then
7175 return Sloc (Label);
7176 else
7177 return Sloc (Nod);
7178 end if;
7179
7180 else
7181 return Sloc (N);
7182 end if;
7183 end End_Label_Loc;
7184
7185 -- Local variables
7186
7187 Owner : Node_Id;
7188
7189 -- Start of processing for End_Keyword_Location
7190
7191 begin
7192 if Nkind_In (N, N_Block_Statement,
7193 N_Entry_Body,
7194 N_Package_Body,
7195 N_Subprogram_Body,
7196 N_Task_Body)
7197 then
7198 Owner := Handled_Statement_Sequence (N);
7199
7200 elsif Nkind (N) = N_Package_Declaration then
7201 Owner := Specification (N);
7202
7203 elsif Nkind (N) = N_Protected_Body then
7204 Owner := N;
7205
7206 elsif Nkind_In (N, N_Protected_Type_Declaration,
7207 N_Single_Protected_Declaration)
7208 then
7209 Owner := Protected_Definition (N);
7210
7211 elsif Nkind_In (N, N_Single_Task_Declaration,
7212 N_Task_Type_Declaration)
7213 then
7214 Owner := Task_Definition (N);
7215
7216 -- This routine should not be called with other contexts
7217
7218 else
7219 pragma Assert (False);
7220 null;
7221 end if;
7222
7223 return End_Label_Loc (Owner);
7224 end End_Keyword_Location;
7225
7226 ------------------------
7227 -- Ensure_Freeze_Node --
7228 ------------------------
7229
7230 procedure Ensure_Freeze_Node (E : Entity_Id) is
7231 FN : Node_Id;
7232 begin
7233 if No (Freeze_Node (E)) then
7234 FN := Make_Freeze_Entity (Sloc (E));
7235 Set_Has_Delayed_Freeze (E);
7236 Set_Freeze_Node (E, FN);
7237 Set_Access_Types_To_Process (FN, No_Elist);
7238 Set_TSS_Elist (FN, No_Elist);
7239 Set_Entity (FN, E);
7240 end if;
7241 end Ensure_Freeze_Node;
7242
7243 ----------------
7244 -- Enter_Name --
7245 ----------------
7246
7247 procedure Enter_Name (Def_Id : Entity_Id) is
7248 C : constant Entity_Id := Current_Entity (Def_Id);
7249 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
7250 S : constant Entity_Id := Current_Scope;
7251
7252 begin
7253 Generate_Definition (Def_Id);
7254
7255 -- Add new name to current scope declarations. Check for duplicate
7256 -- declaration, which may or may not be a genuine error.
7257
7258 if Present (E) then
7259
7260 -- Case of previous entity entered because of a missing declaration
7261 -- or else a bad subtype indication. Best is to use the new entity,
7262 -- and make the previous one invisible.
7263
7264 if Etype (E) = Any_Type then
7265 Set_Is_Immediately_Visible (E, False);
7266
7267 -- Case of renaming declaration constructed for package instances.
7268 -- if there is an explicit declaration with the same identifier,
7269 -- the renaming is not immediately visible any longer, but remains
7270 -- visible through selected component notation.
7271
7272 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
7273 and then not Comes_From_Source (E)
7274 then
7275 Set_Is_Immediately_Visible (E, False);
7276
7277 -- The new entity may be the package renaming, which has the same
7278 -- same name as a generic formal which has been seen already.
7279
7280 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
7281 and then not Comes_From_Source (Def_Id)
7282 then
7283 Set_Is_Immediately_Visible (E, False);
7284
7285 -- For a fat pointer corresponding to a remote access to subprogram,
7286 -- we use the same identifier as the RAS type, so that the proper
7287 -- name appears in the stub. This type is only retrieved through
7288 -- the RAS type and never by visibility, and is not added to the
7289 -- visibility list (see below).
7290
7291 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
7292 and then Ekind (Def_Id) = E_Record_Type
7293 and then Present (Corresponding_Remote_Type (Def_Id))
7294 then
7295 null;
7296
7297 -- Case of an implicit operation or derived literal. The new entity
7298 -- hides the implicit one, which is removed from all visibility,
7299 -- i.e. the entity list of its scope, and homonym chain of its name.
7300
7301 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
7302 or else Is_Internal (E)
7303 then
7304 declare
7305 Decl : constant Node_Id := Parent (E);
7306 Prev : Entity_Id;
7307 Prev_Vis : Entity_Id;
7308
7309 begin
7310 -- If E is an implicit declaration, it cannot be the first
7311 -- entity in the scope.
7312
7313 Prev := First_Entity (Current_Scope);
7314 while Present (Prev) and then Next_Entity (Prev) /= E loop
7315 Next_Entity (Prev);
7316 end loop;
7317
7318 if No (Prev) then
7319
7320 -- If E is not on the entity chain of the current scope,
7321 -- it is an implicit declaration in the generic formal
7322 -- part of a generic subprogram. When analyzing the body,
7323 -- the generic formals are visible but not on the entity
7324 -- chain of the subprogram. The new entity will become
7325 -- the visible one in the body.
7326
7327 pragma Assert
7328 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
7329 null;
7330
7331 else
7332 Link_Entities (Prev, Next_Entity (E));
7333
7334 if No (Next_Entity (Prev)) then
7335 Set_Last_Entity (Current_Scope, Prev);
7336 end if;
7337
7338 if E = Current_Entity (E) then
7339 Prev_Vis := Empty;
7340
7341 else
7342 Prev_Vis := Current_Entity (E);
7343 while Homonym (Prev_Vis) /= E loop
7344 Prev_Vis := Homonym (Prev_Vis);
7345 end loop;
7346 end if;
7347
7348 if Present (Prev_Vis) then
7349
7350 -- Skip E in the visibility chain
7351
7352 Set_Homonym (Prev_Vis, Homonym (E));
7353
7354 else
7355 Set_Name_Entity_Id (Chars (E), Homonym (E));
7356 end if;
7357 end if;
7358 end;
7359
7360 -- This section of code could use a comment ???
7361
7362 elsif Present (Etype (E))
7363 and then Is_Concurrent_Type (Etype (E))
7364 and then E = Def_Id
7365 then
7366 return;
7367
7368 -- If the homograph is a protected component renaming, it should not
7369 -- be hiding the current entity. Such renamings are treated as weak
7370 -- declarations.
7371
7372 elsif Is_Prival (E) then
7373 Set_Is_Immediately_Visible (E, False);
7374
7375 -- In this case the current entity is a protected component renaming.
7376 -- Perform minimal decoration by setting the scope and return since
7377 -- the prival should not be hiding other visible entities.
7378
7379 elsif Is_Prival (Def_Id) then
7380 Set_Scope (Def_Id, Current_Scope);
7381 return;
7382
7383 -- Analogous to privals, the discriminal generated for an entry index
7384 -- parameter acts as a weak declaration. Perform minimal decoration
7385 -- to avoid bogus errors.
7386
7387 elsif Is_Discriminal (Def_Id)
7388 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
7389 then
7390 Set_Scope (Def_Id, Current_Scope);
7391 return;
7392
7393 -- In the body or private part of an instance, a type extension may
7394 -- introduce a component with the same name as that of an actual. The
7395 -- legality rule is not enforced, but the semantics of the full type
7396 -- with two components of same name are not clear at this point???
7397
7398 elsif In_Instance_Not_Visible then
7399 null;
7400
7401 -- When compiling a package body, some child units may have become
7402 -- visible. They cannot conflict with local entities that hide them.
7403
7404 elsif Is_Child_Unit (E)
7405 and then In_Open_Scopes (Scope (E))
7406 and then not Is_Immediately_Visible (E)
7407 then
7408 null;
7409
7410 -- Conversely, with front-end inlining we may compile the parent body
7411 -- first, and a child unit subsequently. The context is now the
7412 -- parent spec, and body entities are not visible.
7413
7414 elsif Is_Child_Unit (Def_Id)
7415 and then Is_Package_Body_Entity (E)
7416 and then not In_Package_Body (Current_Scope)
7417 then
7418 null;
7419
7420 -- Case of genuine duplicate declaration
7421
7422 else
7423 Error_Msg_Sloc := Sloc (E);
7424
7425 -- If the previous declaration is an incomplete type declaration
7426 -- this may be an attempt to complete it with a private type. The
7427 -- following avoids confusing cascaded errors.
7428
7429 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
7430 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
7431 then
7432 Error_Msg_N
7433 ("incomplete type cannot be completed with a private " &
7434 "declaration", Parent (Def_Id));
7435 Set_Is_Immediately_Visible (E, False);
7436 Set_Full_View (E, Def_Id);
7437
7438 -- An inherited component of a record conflicts with a new
7439 -- discriminant. The discriminant is inserted first in the scope,
7440 -- but the error should be posted on it, not on the component.
7441
7442 elsif Ekind (E) = E_Discriminant
7443 and then Present (Scope (Def_Id))
7444 and then Scope (Def_Id) /= Current_Scope
7445 then
7446 Error_Msg_Sloc := Sloc (Def_Id);
7447 Error_Msg_N ("& conflicts with declaration#", E);
7448 return;
7449
7450 -- If the name of the unit appears in its own context clause, a
7451 -- dummy package with the name has already been created, and the
7452 -- error emitted. Try to continue quietly.
7453
7454 elsif Error_Posted (E)
7455 and then Sloc (E) = No_Location
7456 and then Nkind (Parent (E)) = N_Package_Specification
7457 and then Current_Scope = Standard_Standard
7458 then
7459 Set_Scope (Def_Id, Current_Scope);
7460 return;
7461
7462 else
7463 Error_Msg_N ("& conflicts with declaration#", Def_Id);
7464
7465 -- Avoid cascaded messages with duplicate components in
7466 -- derived types.
7467
7468 if Ekind_In (E, E_Component, E_Discriminant) then
7469 return;
7470 end if;
7471 end if;
7472
7473 if Nkind (Parent (Parent (Def_Id))) =
7474 N_Generic_Subprogram_Declaration
7475 and then Def_Id =
7476 Defining_Entity (Specification (Parent (Parent (Def_Id))))
7477 then
7478 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
7479 end if;
7480
7481 -- If entity is in standard, then we are in trouble, because it
7482 -- means that we have a library package with a duplicated name.
7483 -- That's hard to recover from, so abort.
7484
7485 if S = Standard_Standard then
7486 raise Unrecoverable_Error;
7487
7488 -- Otherwise we continue with the declaration. Having two
7489 -- identical declarations should not cause us too much trouble.
7490
7491 else
7492 null;
7493 end if;
7494 end if;
7495 end if;
7496
7497 -- If we fall through, declaration is OK, at least OK enough to continue
7498
7499 -- If Def_Id is a discriminant or a record component we are in the midst
7500 -- of inheriting components in a derived record definition. Preserve
7501 -- their Ekind and Etype.
7502
7503 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
7504 null;
7505
7506 -- If a type is already set, leave it alone (happens when a type
7507 -- declaration is reanalyzed following a call to the optimizer).
7508
7509 elsif Present (Etype (Def_Id)) then
7510 null;
7511
7512 -- Otherwise, the kind E_Void insures that premature uses of the entity
7513 -- will be detected. Any_Type insures that no cascaded errors will occur
7514
7515 else
7516 Set_Ekind (Def_Id, E_Void);
7517 Set_Etype (Def_Id, Any_Type);
7518 end if;
7519
7520 -- Inherited discriminants and components in derived record types are
7521 -- immediately visible. Itypes are not.
7522
7523 -- Unless the Itype is for a record type with a corresponding remote
7524 -- type (what is that about, it was not commented ???)
7525
7526 if Ekind_In (Def_Id, E_Discriminant, E_Component)
7527 or else
7528 ((not Is_Record_Type (Def_Id)
7529 or else No (Corresponding_Remote_Type (Def_Id)))
7530 and then not Is_Itype (Def_Id))
7531 then
7532 Set_Is_Immediately_Visible (Def_Id);
7533 Set_Current_Entity (Def_Id);
7534 end if;
7535
7536 Set_Homonym (Def_Id, C);
7537 Append_Entity (Def_Id, S);
7538 Set_Public_Status (Def_Id);
7539
7540 -- Declaring a homonym is not allowed in SPARK ...
7541
7542 if Present (C) and then Restriction_Check_Required (SPARK_05) then
7543 declare
7544 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
7545 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
7546 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
7547
7548 begin
7549 -- ... unless the new declaration is in a subprogram, and the
7550 -- visible declaration is a variable declaration or a parameter
7551 -- specification outside that subprogram.
7552
7553 if Present (Enclosing_Subp)
7554 and then Nkind_In (Parent (C), N_Object_Declaration,
7555 N_Parameter_Specification)
7556 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
7557 then
7558 null;
7559
7560 -- ... or the new declaration is in a package, and the visible
7561 -- declaration occurs outside that package.
7562
7563 elsif Present (Enclosing_Pack)
7564 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
7565 then
7566 null;
7567
7568 -- ... or the new declaration is a component declaration in a
7569 -- record type definition.
7570
7571 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
7572 null;
7573
7574 -- Don't issue error for non-source entities
7575
7576 elsif Comes_From_Source (Def_Id)
7577 and then Comes_From_Source (C)
7578 then
7579 Error_Msg_Sloc := Sloc (C);
7580 Check_SPARK_05_Restriction
7581 ("redeclaration of identifier &#", Def_Id);
7582 end if;
7583 end;
7584 end if;
7585
7586 -- Warn if new entity hides an old one
7587
7588 if Warn_On_Hiding and then Present (C)
7589
7590 -- Don't warn for record components since they always have a well
7591 -- defined scope which does not confuse other uses. Note that in
7592 -- some cases, Ekind has not been set yet.
7593
7594 and then Ekind (C) /= E_Component
7595 and then Ekind (C) /= E_Discriminant
7596 and then Nkind (Parent (C)) /= N_Component_Declaration
7597 and then Ekind (Def_Id) /= E_Component
7598 and then Ekind (Def_Id) /= E_Discriminant
7599 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
7600
7601 -- Don't warn for one character variables. It is too common to use
7602 -- such variables as locals and will just cause too many false hits.
7603
7604 and then Length_Of_Name (Chars (C)) /= 1
7605
7606 -- Don't warn for non-source entities
7607
7608 and then Comes_From_Source (C)
7609 and then Comes_From_Source (Def_Id)
7610
7611 -- Don't warn unless entity in question is in extended main source
7612
7613 and then In_Extended_Main_Source_Unit (Def_Id)
7614
7615 -- Finally, the hidden entity must be either immediately visible or
7616 -- use visible (i.e. from a used package).
7617
7618 and then
7619 (Is_Immediately_Visible (C)
7620 or else
7621 Is_Potentially_Use_Visible (C))
7622 then
7623 Error_Msg_Sloc := Sloc (C);
7624 Error_Msg_N ("declaration hides &#?h?", Def_Id);
7625 end if;
7626 end Enter_Name;
7627
7628 ---------------
7629 -- Entity_Of --
7630 ---------------
7631
7632 function Entity_Of (N : Node_Id) return Entity_Id is
7633 Id : Entity_Id;
7634 Ren : Node_Id;
7635
7636 begin
7637 -- Assume that the arbitrary node does not have an entity
7638
7639 Id := Empty;
7640
7641 if Is_Entity_Name (N) then
7642 Id := Entity (N);
7643
7644 -- Follow a possible chain of renamings to reach the earliest renamed
7645 -- source object.
7646
7647 while Present (Id)
7648 and then Is_Object (Id)
7649 and then Present (Renamed_Object (Id))
7650 loop
7651 Ren := Renamed_Object (Id);
7652
7653 -- The reference renames an abstract state or a whole object
7654
7655 -- Obj : ...;
7656 -- Ren : ... renames Obj;
7657
7658 if Is_Entity_Name (Ren) then
7659
7660 -- Do not follow a renaming that goes through a generic formal,
7661 -- because these entities are hidden and must not be referenced
7662 -- from outside the generic.
7663
7664 if Is_Hidden (Entity (Ren)) then
7665 exit;
7666
7667 else
7668 Id := Entity (Ren);
7669 end if;
7670
7671 -- The reference renames a function result. Check the original
7672 -- node in case expansion relocates the function call.
7673
7674 -- Ren : ... renames Func_Call;
7675
7676 elsif Nkind (Original_Node (Ren)) = N_Function_Call then
7677 exit;
7678
7679 -- Otherwise the reference renames something which does not yield
7680 -- an abstract state or a whole object. Treat the reference as not
7681 -- having a proper entity for SPARK legality purposes.
7682
7683 else
7684 Id := Empty;
7685 exit;
7686 end if;
7687 end loop;
7688 end if;
7689
7690 return Id;
7691 end Entity_Of;
7692
7693 --------------------------
7694 -- Examine_Array_Bounds --
7695 --------------------------
7696
7697 procedure Examine_Array_Bounds
7698 (Typ : Entity_Id;
7699 All_Static : out Boolean;
7700 Has_Empty : out Boolean)
7701 is
7702 function Is_OK_Static_Bound (Bound : Node_Id) return Boolean;
7703 -- Determine whether bound Bound is a suitable static bound
7704
7705 ------------------------
7706 -- Is_OK_Static_Bound --
7707 ------------------------
7708
7709 function Is_OK_Static_Bound (Bound : Node_Id) return Boolean is
7710 begin
7711 return
7712 not Error_Posted (Bound)
7713 and then Is_OK_Static_Expression (Bound);
7714 end Is_OK_Static_Bound;
7715
7716 -- Local variables
7717
7718 Hi_Bound : Node_Id;
7719 Index : Node_Id;
7720 Lo_Bound : Node_Id;
7721
7722 -- Start of processing for Examine_Array_Bounds
7723
7724 begin
7725 -- An unconstrained array type does not have static bounds, and it is
7726 -- not known whether they are empty or not.
7727
7728 if not Is_Constrained (Typ) then
7729 All_Static := False;
7730 Has_Empty := False;
7731
7732 -- A string literal has static bounds, and is not empty as long as it
7733 -- contains at least one character.
7734
7735 elsif Ekind (Typ) = E_String_Literal_Subtype then
7736 All_Static := True;
7737 Has_Empty := String_Literal_Length (Typ) > 0;
7738 end if;
7739
7740 -- Assume that all bounds are static and not empty
7741
7742 All_Static := True;
7743 Has_Empty := False;
7744
7745 -- Examine each index
7746
7747 Index := First_Index (Typ);
7748 while Present (Index) loop
7749 if Is_Discrete_Type (Etype (Index)) then
7750 Get_Index_Bounds (Index, Lo_Bound, Hi_Bound);
7751
7752 if Is_OK_Static_Bound (Lo_Bound)
7753 and then
7754 Is_OK_Static_Bound (Hi_Bound)
7755 then
7756 -- The static bounds produce an empty range
7757
7758 if Is_Null_Range (Lo_Bound, Hi_Bound) then
7759 Has_Empty := True;
7760 end if;
7761
7762 -- Otherwise at least one of the bounds is not static
7763
7764 else
7765 All_Static := False;
7766 end if;
7767
7768 -- Otherwise the index is non-discrete, therefore not static
7769
7770 else
7771 All_Static := False;
7772 end if;
7773
7774 Next_Index (Index);
7775 end loop;
7776 end Examine_Array_Bounds;
7777
7778 -------------------
7779 -- Exceptions_OK --
7780 -------------------
7781
7782 function Exceptions_OK return Boolean is
7783 begin
7784 return
7785 not (Restriction_Active (No_Exception_Handlers) or else
7786 Restriction_Active (No_Exception_Propagation) or else
7787 Restriction_Active (No_Exceptions));
7788 end Exceptions_OK;
7789
7790 --------------------------
7791 -- Explain_Limited_Type --
7792 --------------------------
7793
7794 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
7795 C : Entity_Id;
7796
7797 begin
7798 -- For array, component type must be limited
7799
7800 if Is_Array_Type (T) then
7801 Error_Msg_Node_2 := T;
7802 Error_Msg_NE
7803 ("\component type& of type& is limited", N, Component_Type (T));
7804 Explain_Limited_Type (Component_Type (T), N);
7805
7806 elsif Is_Record_Type (T) then
7807
7808 -- No need for extra messages if explicit limited record
7809
7810 if Is_Limited_Record (Base_Type (T)) then
7811 return;
7812 end if;
7813
7814 -- Otherwise find a limited component. Check only components that
7815 -- come from source, or inherited components that appear in the
7816 -- source of the ancestor.
7817
7818 C := First_Component (T);
7819 while Present (C) loop
7820 if Is_Limited_Type (Etype (C))
7821 and then
7822 (Comes_From_Source (C)
7823 or else
7824 (Present (Original_Record_Component (C))
7825 and then
7826 Comes_From_Source (Original_Record_Component (C))))
7827 then
7828 Error_Msg_Node_2 := T;
7829 Error_Msg_NE ("\component& of type& has limited type", N, C);
7830 Explain_Limited_Type (Etype (C), N);
7831 return;
7832 end if;
7833
7834 Next_Component (C);
7835 end loop;
7836
7837 -- The type may be declared explicitly limited, even if no component
7838 -- of it is limited, in which case we fall out of the loop.
7839 return;
7840 end if;
7841 end Explain_Limited_Type;
7842
7843 ---------------------------------------
7844 -- Expression_Of_Expression_Function --
7845 ---------------------------------------
7846
7847 function Expression_Of_Expression_Function
7848 (Subp : Entity_Id) return Node_Id
7849 is
7850 Expr_Func : Node_Id;
7851
7852 begin
7853 pragma Assert (Is_Expression_Function_Or_Completion (Subp));
7854
7855 if Nkind (Original_Node (Subprogram_Spec (Subp))) =
7856 N_Expression_Function
7857 then
7858 Expr_Func := Original_Node (Subprogram_Spec (Subp));
7859
7860 elsif Nkind (Original_Node (Subprogram_Body (Subp))) =
7861 N_Expression_Function
7862 then
7863 Expr_Func := Original_Node (Subprogram_Body (Subp));
7864
7865 else
7866 pragma Assert (False);
7867 null;
7868 end if;
7869
7870 return Original_Node (Expression (Expr_Func));
7871 end Expression_Of_Expression_Function;
7872
7873 -------------------------------
7874 -- Extensions_Visible_Status --
7875 -------------------------------
7876
7877 function Extensions_Visible_Status
7878 (Id : Entity_Id) return Extensions_Visible_Mode
7879 is
7880 Arg : Node_Id;
7881 Decl : Node_Id;
7882 Expr : Node_Id;
7883 Prag : Node_Id;
7884 Subp : Entity_Id;
7885
7886 begin
7887 -- When a formal parameter is subject to Extensions_Visible, the pragma
7888 -- is stored in the contract of related subprogram.
7889
7890 if Is_Formal (Id) then
7891 Subp := Scope (Id);
7892
7893 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
7894 Subp := Id;
7895
7896 -- No other construct carries this pragma
7897
7898 else
7899 return Extensions_Visible_None;
7900 end if;
7901
7902 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
7903
7904 -- In certain cases analysis may request the Extensions_Visible status
7905 -- of an expression function before the pragma has been analyzed yet.
7906 -- Inspect the declarative items after the expression function looking
7907 -- for the pragma (if any).
7908
7909 if No (Prag) and then Is_Expression_Function (Subp) then
7910 Decl := Next (Unit_Declaration_Node (Subp));
7911 while Present (Decl) loop
7912 if Nkind (Decl) = N_Pragma
7913 and then Pragma_Name (Decl) = Name_Extensions_Visible
7914 then
7915 Prag := Decl;
7916 exit;
7917
7918 -- A source construct ends the region where Extensions_Visible may
7919 -- appear, stop the traversal. An expanded expression function is
7920 -- no longer a source construct, but it must still be recognized.
7921
7922 elsif Comes_From_Source (Decl)
7923 or else
7924 (Nkind_In (Decl, N_Subprogram_Body,
7925 N_Subprogram_Declaration)
7926 and then Is_Expression_Function (Defining_Entity (Decl)))
7927 then
7928 exit;
7929 end if;
7930
7931 Next (Decl);
7932 end loop;
7933 end if;
7934
7935 -- Extract the value from the Boolean expression (if any)
7936
7937 if Present (Prag) then
7938 Arg := First (Pragma_Argument_Associations (Prag));
7939
7940 if Present (Arg) then
7941 Expr := Get_Pragma_Arg (Arg);
7942
7943 -- When the associated subprogram is an expression function, the
7944 -- argument of the pragma may not have been analyzed.
7945
7946 if not Analyzed (Expr) then
7947 Preanalyze_And_Resolve (Expr, Standard_Boolean);
7948 end if;
7949
7950 -- Guard against cascading errors when the argument of pragma
7951 -- Extensions_Visible is not a valid static Boolean expression.
7952
7953 if Error_Posted (Expr) then
7954 return Extensions_Visible_None;
7955
7956 elsif Is_True (Expr_Value (Expr)) then
7957 return Extensions_Visible_True;
7958
7959 else
7960 return Extensions_Visible_False;
7961 end if;
7962
7963 -- Otherwise the aspect or pragma defaults to True
7964
7965 else
7966 return Extensions_Visible_True;
7967 end if;
7968
7969 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
7970 -- directly specified. In SPARK code, its value defaults to "False".
7971
7972 elsif SPARK_Mode = On then
7973 return Extensions_Visible_False;
7974
7975 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
7976 -- "True".
7977
7978 else
7979 return Extensions_Visible_True;
7980 end if;
7981 end Extensions_Visible_Status;
7982
7983 -----------------
7984 -- Find_Actual --
7985 -----------------
7986
7987 procedure Find_Actual
7988 (N : Node_Id;
7989 Formal : out Entity_Id;
7990 Call : out Node_Id)
7991 is
7992 Context : constant Node_Id := Parent (N);
7993 Actual : Node_Id;
7994 Call_Nam : Node_Id;
7995
7996 begin
7997 if Nkind_In (Context, N_Indexed_Component, N_Selected_Component)
7998 and then N = Prefix (Context)
7999 then
8000 Find_Actual (Context, Formal, Call);
8001 return;
8002
8003 elsif Nkind (Context) = N_Parameter_Association
8004 and then N = Explicit_Actual_Parameter (Context)
8005 then
8006 Call := Parent (Context);
8007
8008 elsif Nkind_In (Context, N_Entry_Call_Statement,
8009 N_Function_Call,
8010 N_Procedure_Call_Statement)
8011 then
8012 Call := Context;
8013
8014 else
8015 Formal := Empty;
8016 Call := Empty;
8017 return;
8018 end if;
8019
8020 -- If we have a call to a subprogram look for the parameter. Note that
8021 -- we exclude overloaded calls, since we don't know enough to be sure
8022 -- of giving the right answer in this case.
8023
8024 if Nkind_In (Call, N_Entry_Call_Statement,
8025 N_Function_Call,
8026 N_Procedure_Call_Statement)
8027 then
8028 Call_Nam := Name (Call);
8029
8030 -- A call to a protected or task entry appears as a selected
8031 -- component rather than an expanded name.
8032
8033 if Nkind (Call_Nam) = N_Selected_Component then
8034 Call_Nam := Selector_Name (Call_Nam);
8035 end if;
8036
8037 if Is_Entity_Name (Call_Nam)
8038 and then Present (Entity (Call_Nam))
8039 and then Is_Overloadable (Entity (Call_Nam))
8040 and then not Is_Overloaded (Call_Nam)
8041 then
8042 -- If node is name in call it is not an actual
8043
8044 if N = Call_Nam then
8045 Formal := Empty;
8046 Call := Empty;
8047 return;
8048 end if;
8049
8050 -- Fall here if we are definitely a parameter
8051
8052 Actual := First_Actual (Call);
8053 Formal := First_Formal (Entity (Call_Nam));
8054 while Present (Formal) and then Present (Actual) loop
8055 if Actual = N then
8056 return;
8057
8058 -- An actual that is the prefix in a prefixed call may have
8059 -- been rewritten in the call, after the deferred reference
8060 -- was collected. Check if sloc and kinds and names match.
8061
8062 elsif Sloc (Actual) = Sloc (N)
8063 and then Nkind (Actual) = N_Identifier
8064 and then Nkind (Actual) = Nkind (N)
8065 and then Chars (Actual) = Chars (N)
8066 then
8067 return;
8068
8069 else
8070 Next_Actual (Actual);
8071 Next_Formal (Formal);
8072 end if;
8073 end loop;
8074 end if;
8075 end if;
8076
8077 -- Fall through here if we did not find matching actual
8078
8079 Formal := Empty;
8080 Call := Empty;
8081 end Find_Actual;
8082
8083 ---------------------------
8084 -- Find_Body_Discriminal --
8085 ---------------------------
8086
8087 function Find_Body_Discriminal
8088 (Spec_Discriminant : Entity_Id) return Entity_Id
8089 is
8090 Tsk : Entity_Id;
8091 Disc : Entity_Id;
8092
8093 begin
8094 -- If expansion is suppressed, then the scope can be the concurrent type
8095 -- itself rather than a corresponding concurrent record type.
8096
8097 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
8098 Tsk := Scope (Spec_Discriminant);
8099
8100 else
8101 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
8102
8103 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
8104 end if;
8105
8106 -- Find discriminant of original concurrent type, and use its current
8107 -- discriminal, which is the renaming within the task/protected body.
8108
8109 Disc := First_Discriminant (Tsk);
8110 while Present (Disc) loop
8111 if Chars (Disc) = Chars (Spec_Discriminant) then
8112 return Discriminal (Disc);
8113 end if;
8114
8115 Next_Discriminant (Disc);
8116 end loop;
8117
8118 -- That loop should always succeed in finding a matching entry and
8119 -- returning. Fatal error if not.
8120
8121 raise Program_Error;
8122 end Find_Body_Discriminal;
8123
8124 -------------------------------------
8125 -- Find_Corresponding_Discriminant --
8126 -------------------------------------
8127
8128 function Find_Corresponding_Discriminant
8129 (Id : Node_Id;
8130 Typ : Entity_Id) return Entity_Id
8131 is
8132 Par_Disc : Entity_Id;
8133 Old_Disc : Entity_Id;
8134 New_Disc : Entity_Id;
8135
8136 begin
8137 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
8138
8139 -- The original type may currently be private, and the discriminant
8140 -- only appear on its full view.
8141
8142 if Is_Private_Type (Scope (Par_Disc))
8143 and then not Has_Discriminants (Scope (Par_Disc))
8144 and then Present (Full_View (Scope (Par_Disc)))
8145 then
8146 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
8147 else
8148 Old_Disc := First_Discriminant (Scope (Par_Disc));
8149 end if;
8150
8151 if Is_Class_Wide_Type (Typ) then
8152 New_Disc := First_Discriminant (Root_Type (Typ));
8153 else
8154 New_Disc := First_Discriminant (Typ);
8155 end if;
8156
8157 while Present (Old_Disc) and then Present (New_Disc) loop
8158 if Old_Disc = Par_Disc then
8159 return New_Disc;
8160 end if;
8161
8162 Next_Discriminant (Old_Disc);
8163 Next_Discriminant (New_Disc);
8164 end loop;
8165
8166 -- Should always find it
8167
8168 raise Program_Error;
8169 end Find_Corresponding_Discriminant;
8170
8171 -------------------
8172 -- Find_DIC_Type --
8173 -------------------
8174
8175 function Find_DIC_Type (Typ : Entity_Id) return Entity_Id is
8176 Curr_Typ : Entity_Id;
8177 -- The current type being examined in the parent hierarchy traversal
8178
8179 DIC_Typ : Entity_Id;
8180 -- The type which carries the DIC pragma. This variable denotes the
8181 -- partial view when private types are involved.
8182
8183 Par_Typ : Entity_Id;
8184 -- The parent type of the current type. This variable denotes the full
8185 -- view when private types are involved.
8186
8187 begin
8188 -- The input type defines its own DIC pragma, therefore it is the owner
8189
8190 if Has_Own_DIC (Typ) then
8191 DIC_Typ := Typ;
8192
8193 -- Otherwise the DIC pragma is inherited from a parent type
8194
8195 else
8196 pragma Assert (Has_Inherited_DIC (Typ));
8197
8198 -- Climb the parent chain
8199
8200 Curr_Typ := Typ;
8201 loop
8202 -- Inspect the parent type. Do not consider subtypes as they
8203 -- inherit the DIC attributes from their base types.
8204
8205 DIC_Typ := Base_Type (Etype (Curr_Typ));
8206
8207 -- Look at the full view of a private type because the type may
8208 -- have a hidden parent introduced in the full view.
8209
8210 Par_Typ := DIC_Typ;
8211
8212 if Is_Private_Type (Par_Typ)
8213 and then Present (Full_View (Par_Typ))
8214 then
8215 Par_Typ := Full_View (Par_Typ);
8216 end if;
8217
8218 -- Stop the climb once the nearest parent type which defines a DIC
8219 -- pragma of its own is encountered or when the root of the parent
8220 -- chain is reached.
8221
8222 exit when Has_Own_DIC (DIC_Typ) or else Curr_Typ = Par_Typ;
8223
8224 Curr_Typ := Par_Typ;
8225 end loop;
8226 end if;
8227
8228 return DIC_Typ;
8229 end Find_DIC_Type;
8230
8231 ----------------------------------
8232 -- Find_Enclosing_Iterator_Loop --
8233 ----------------------------------
8234
8235 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
8236 Constr : Node_Id;
8237 S : Entity_Id;
8238
8239 begin
8240 -- Traverse the scope chain looking for an iterator loop. Such loops are
8241 -- usually transformed into blocks, hence the use of Original_Node.
8242
8243 S := Id;
8244 while Present (S) and then S /= Standard_Standard loop
8245 if Ekind (S) = E_Loop
8246 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
8247 then
8248 Constr := Original_Node (Label_Construct (Parent (S)));
8249
8250 if Nkind (Constr) = N_Loop_Statement
8251 and then Present (Iteration_Scheme (Constr))
8252 and then Nkind (Iterator_Specification
8253 (Iteration_Scheme (Constr))) =
8254 N_Iterator_Specification
8255 then
8256 return S;
8257 end if;
8258 end if;
8259
8260 S := Scope (S);
8261 end loop;
8262
8263 return Empty;
8264 end Find_Enclosing_Iterator_Loop;
8265
8266 --------------------------
8267 -- Find_Enclosing_Scope --
8268 --------------------------
8269
8270 function Find_Enclosing_Scope (N : Node_Id) return Entity_Id is
8271 Par : Node_Id;
8272
8273 begin
8274 -- Examine the parent chain looking for a construct which defines a
8275 -- scope.
8276
8277 Par := Parent (N);
8278 while Present (Par) loop
8279 case Nkind (Par) is
8280
8281 -- The construct denotes a declaration, the proper scope is its
8282 -- entity.
8283
8284 when N_Entry_Declaration
8285 | N_Expression_Function
8286 | N_Full_Type_Declaration
8287 | N_Generic_Package_Declaration
8288 | N_Generic_Subprogram_Declaration
8289 | N_Package_Declaration
8290 | N_Private_Extension_Declaration
8291 | N_Protected_Type_Declaration
8292 | N_Single_Protected_Declaration
8293 | N_Single_Task_Declaration
8294 | N_Subprogram_Declaration
8295 | N_Task_Type_Declaration
8296 =>
8297 return Defining_Entity (Par);
8298
8299 -- The construct denotes a body, the proper scope is the entity of
8300 -- the corresponding spec or that of the body if the body does not
8301 -- complete a previous declaration.
8302
8303 when N_Entry_Body
8304 | N_Package_Body
8305 | N_Protected_Body
8306 | N_Subprogram_Body
8307 | N_Task_Body
8308 =>
8309 return Unique_Defining_Entity (Par);
8310
8311 -- Special cases
8312
8313 -- Blocks carry either a source or an internally-generated scope,
8314 -- unless the block is a byproduct of exception handling.
8315
8316 when N_Block_Statement =>
8317 if not Exception_Junk (Par) then
8318 return Entity (Identifier (Par));
8319 end if;
8320
8321 -- Loops carry an internally-generated scope
8322
8323 when N_Loop_Statement =>
8324 return Entity (Identifier (Par));
8325
8326 -- Extended return statements carry an internally-generated scope
8327
8328 when N_Extended_Return_Statement =>
8329 return Return_Statement_Entity (Par);
8330
8331 -- A traversal from a subunit continues via the corresponding stub
8332
8333 when N_Subunit =>
8334 Par := Corresponding_Stub (Par);
8335
8336 when others =>
8337 null;
8338 end case;
8339
8340 Par := Parent (Par);
8341 end loop;
8342
8343 return Standard_Standard;
8344 end Find_Enclosing_Scope;
8345
8346 ------------------------------------
8347 -- Find_Loop_In_Conditional_Block --
8348 ------------------------------------
8349
8350 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
8351 Stmt : Node_Id;
8352
8353 begin
8354 Stmt := N;
8355
8356 if Nkind (Stmt) = N_If_Statement then
8357 Stmt := First (Then_Statements (Stmt));
8358 end if;
8359
8360 pragma Assert (Nkind (Stmt) = N_Block_Statement);
8361
8362 -- Inspect the statements of the conditional block. In general the loop
8363 -- should be the first statement in the statement sequence of the block,
8364 -- but the finalization machinery may have introduced extra object
8365 -- declarations.
8366
8367 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
8368 while Present (Stmt) loop
8369 if Nkind (Stmt) = N_Loop_Statement then
8370 return Stmt;
8371 end if;
8372
8373 Next (Stmt);
8374 end loop;
8375
8376 -- The expansion of attribute 'Loop_Entry produced a malformed block
8377
8378 raise Program_Error;
8379 end Find_Loop_In_Conditional_Block;
8380
8381 --------------------------
8382 -- Find_Overlaid_Entity --
8383 --------------------------
8384
8385 procedure Find_Overlaid_Entity
8386 (N : Node_Id;
8387 Ent : out Entity_Id;
8388 Off : out Boolean)
8389 is
8390 Expr : Node_Id;
8391
8392 begin
8393 -- We are looking for one of the two following forms:
8394
8395 -- for X'Address use Y'Address
8396
8397 -- or
8398
8399 -- Const : constant Address := expr;
8400 -- ...
8401 -- for X'Address use Const;
8402
8403 -- In the second case, the expr is either Y'Address, or recursively a
8404 -- constant that eventually references Y'Address.
8405
8406 Ent := Empty;
8407 Off := False;
8408
8409 if Nkind (N) = N_Attribute_Definition_Clause
8410 and then Chars (N) = Name_Address
8411 then
8412 Expr := Expression (N);
8413
8414 -- This loop checks the form of the expression for Y'Address,
8415 -- using recursion to deal with intermediate constants.
8416
8417 loop
8418 -- Check for Y'Address
8419
8420 if Nkind (Expr) = N_Attribute_Reference
8421 and then Attribute_Name (Expr) = Name_Address
8422 then
8423 Expr := Prefix (Expr);
8424 exit;
8425
8426 -- Check for Const where Const is a constant entity
8427
8428 elsif Is_Entity_Name (Expr)
8429 and then Ekind (Entity (Expr)) = E_Constant
8430 then
8431 Expr := Constant_Value (Entity (Expr));
8432
8433 -- Anything else does not need checking
8434
8435 else
8436 return;
8437 end if;
8438 end loop;
8439
8440 -- This loop checks the form of the prefix for an entity, using
8441 -- recursion to deal with intermediate components.
8442
8443 loop
8444 -- Check for Y where Y is an entity
8445
8446 if Is_Entity_Name (Expr) then
8447 Ent := Entity (Expr);
8448 return;
8449
8450 -- Check for components
8451
8452 elsif
8453 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
8454 then
8455 Expr := Prefix (Expr);
8456 Off := True;
8457
8458 -- Anything else does not need checking
8459
8460 else
8461 return;
8462 end if;
8463 end loop;
8464 end if;
8465 end Find_Overlaid_Entity;
8466
8467 -------------------------
8468 -- Find_Parameter_Type --
8469 -------------------------
8470
8471 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
8472 begin
8473 if Nkind (Param) /= N_Parameter_Specification then
8474 return Empty;
8475
8476 -- For an access parameter, obtain the type from the formal entity
8477 -- itself, because access to subprogram nodes do not carry a type.
8478 -- Shouldn't we always use the formal entity ???
8479
8480 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
8481 return Etype (Defining_Identifier (Param));
8482
8483 else
8484 return Etype (Parameter_Type (Param));
8485 end if;
8486 end Find_Parameter_Type;
8487
8488 -----------------------------------
8489 -- Find_Placement_In_State_Space --
8490 -----------------------------------
8491
8492 procedure Find_Placement_In_State_Space
8493 (Item_Id : Entity_Id;
8494 Placement : out State_Space_Kind;
8495 Pack_Id : out Entity_Id)
8496 is
8497 Context : Entity_Id;
8498
8499 begin
8500 -- Assume that the item does not appear in the state space of a package
8501
8502 Placement := Not_In_Package;
8503 Pack_Id := Empty;
8504
8505 -- Climb the scope stack and examine the enclosing context
8506
8507 Context := Scope (Item_Id);
8508 while Present (Context) and then Context /= Standard_Standard loop
8509 if Is_Package_Or_Generic_Package (Context) then
8510 Pack_Id := Context;
8511
8512 -- A package body is a cut off point for the traversal as the item
8513 -- cannot be visible to the outside from this point on. Note that
8514 -- this test must be done first as a body is also classified as a
8515 -- private part.
8516
8517 if In_Package_Body (Context) then
8518 Placement := Body_State_Space;
8519 return;
8520
8521 -- The private part of a package is a cut off point for the
8522 -- traversal as the item cannot be visible to the outside from
8523 -- this point on.
8524
8525 elsif In_Private_Part (Context) then
8526 Placement := Private_State_Space;
8527 return;
8528
8529 -- When the item appears in the visible state space of a package,
8530 -- continue to climb the scope stack as this may not be the final
8531 -- state space.
8532
8533 else
8534 Placement := Visible_State_Space;
8535
8536 -- The visible state space of a child unit acts as the proper
8537 -- placement of an item.
8538
8539 if Is_Child_Unit (Context) then
8540 return;
8541 end if;
8542 end if;
8543
8544 -- The item or its enclosing package appear in a construct that has
8545 -- no state space.
8546
8547 else
8548 Placement := Not_In_Package;
8549 return;
8550 end if;
8551
8552 Context := Scope (Context);
8553 end loop;
8554 end Find_Placement_In_State_Space;
8555
8556 -----------------------
8557 -- Find_Primitive_Eq --
8558 -----------------------
8559
8560 function Find_Primitive_Eq (Typ : Entity_Id) return Entity_Id is
8561 function Find_Eq_Prim (Prims_List : Elist_Id) return Entity_Id;
8562 -- Search for the equality primitive; return Empty if the primitive is
8563 -- not found.
8564
8565 ------------------
8566 -- Find_Eq_Prim --
8567 ------------------
8568
8569 function Find_Eq_Prim (Prims_List : Elist_Id) return Entity_Id is
8570 Prim : Entity_Id;
8571 Prim_Elmt : Elmt_Id;
8572
8573 begin
8574 Prim_Elmt := First_Elmt (Prims_List);
8575 while Present (Prim_Elmt) loop
8576 Prim := Node (Prim_Elmt);
8577
8578 -- Locate primitive equality with the right signature
8579
8580 if Chars (Prim) = Name_Op_Eq
8581 and then Etype (First_Formal (Prim)) =
8582 Etype (Next_Formal (First_Formal (Prim)))
8583 and then Base_Type (Etype (Prim)) = Standard_Boolean
8584 then
8585 return Prim;
8586 end if;
8587
8588 Next_Elmt (Prim_Elmt);
8589 end loop;
8590
8591 return Empty;
8592 end Find_Eq_Prim;
8593
8594 -- Local Variables
8595
8596 Eq_Prim : Entity_Id;
8597 Full_Type : Entity_Id;
8598
8599 -- Start of processing for Find_Primitive_Eq
8600
8601 begin
8602 if Is_Private_Type (Typ) then
8603 Full_Type := Underlying_Type (Typ);
8604 else
8605 Full_Type := Typ;
8606 end if;
8607
8608 if No (Full_Type) then
8609 return Empty;
8610 end if;
8611
8612 Full_Type := Base_Type (Full_Type);
8613
8614 -- When the base type itself is private, use the full view
8615
8616 if Is_Private_Type (Full_Type) then
8617 Full_Type := Underlying_Type (Full_Type);
8618 end if;
8619
8620 if Is_Class_Wide_Type (Full_Type) then
8621 Full_Type := Root_Type (Full_Type);
8622 end if;
8623
8624 if not Is_Tagged_Type (Full_Type) then
8625 Eq_Prim := Find_Eq_Prim (Collect_Primitive_Operations (Typ));
8626
8627 -- If this is an untagged private type completed with a derivation of
8628 -- an untagged private type whose full view is a tagged type, we use
8629 -- the primitive operations of the private parent type (since it does
8630 -- not have a full view, and also because its equality primitive may
8631 -- have been overridden in its untagged full view). If no equality was
8632 -- defined for it then take its dispatching equality primitive.
8633
8634 elsif Inherits_From_Tagged_Full_View (Typ) then
8635 Eq_Prim := Find_Eq_Prim (Collect_Primitive_Operations (Typ));
8636
8637 if No (Eq_Prim) then
8638 Eq_Prim := Find_Eq_Prim (Primitive_Operations (Full_Type));
8639 end if;
8640
8641 else
8642 Eq_Prim := Find_Eq_Prim (Primitive_Operations (Full_Type));
8643 end if;
8644
8645 return Eq_Prim;
8646 end Find_Primitive_Eq;
8647
8648 ------------------------
8649 -- Find_Specific_Type --
8650 ------------------------
8651
8652 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
8653 Typ : Entity_Id := Root_Type (CW);
8654
8655 begin
8656 if Ekind (Typ) = E_Incomplete_Type then
8657 if From_Limited_With (Typ) then
8658 Typ := Non_Limited_View (Typ);
8659 else
8660 Typ := Full_View (Typ);
8661 end if;
8662 end if;
8663
8664 if Is_Private_Type (Typ)
8665 and then not Is_Tagged_Type (Typ)
8666 and then Present (Full_View (Typ))
8667 then
8668 return Full_View (Typ);
8669 else
8670 return Typ;
8671 end if;
8672 end Find_Specific_Type;
8673
8674 -----------------------------
8675 -- Find_Static_Alternative --
8676 -----------------------------
8677
8678 function Find_Static_Alternative (N : Node_Id) return Node_Id is
8679 Expr : constant Node_Id := Expression (N);
8680 Val : constant Uint := Expr_Value (Expr);
8681 Alt : Node_Id;
8682 Choice : Node_Id;
8683
8684 begin
8685 Alt := First (Alternatives (N));
8686
8687 Search : loop
8688 if Nkind (Alt) /= N_Pragma then
8689 Choice := First (Discrete_Choices (Alt));
8690 while Present (Choice) loop
8691
8692 -- Others choice, always matches
8693
8694 if Nkind (Choice) = N_Others_Choice then
8695 exit Search;
8696
8697 -- Range, check if value is in the range
8698
8699 elsif Nkind (Choice) = N_Range then
8700 exit Search when
8701 Val >= Expr_Value (Low_Bound (Choice))
8702 and then
8703 Val <= Expr_Value (High_Bound (Choice));
8704
8705 -- Choice is a subtype name. Note that we know it must
8706 -- be a static subtype, since otherwise it would have
8707 -- been diagnosed as illegal.
8708
8709 elsif Is_Entity_Name (Choice)
8710 and then Is_Type (Entity (Choice))
8711 then
8712 exit Search when Is_In_Range (Expr, Etype (Choice),
8713 Assume_Valid => False);
8714
8715 -- Choice is a subtype indication
8716
8717 elsif Nkind (Choice) = N_Subtype_Indication then
8718 declare
8719 C : constant Node_Id := Constraint (Choice);
8720 R : constant Node_Id := Range_Expression (C);
8721
8722 begin
8723 exit Search when
8724 Val >= Expr_Value (Low_Bound (R))
8725 and then
8726 Val <= Expr_Value (High_Bound (R));
8727 end;
8728
8729 -- Choice is a simple expression
8730
8731 else
8732 exit Search when Val = Expr_Value (Choice);
8733 end if;
8734
8735 Next (Choice);
8736 end loop;
8737 end if;
8738
8739 Next (Alt);
8740 pragma Assert (Present (Alt));
8741 end loop Search;
8742
8743 -- The above loop *must* terminate by finding a match, since we know the
8744 -- case statement is valid, and the value of the expression is known at
8745 -- compile time. When we fall out of the loop, Alt points to the
8746 -- alternative that we know will be selected at run time.
8747
8748 return Alt;
8749 end Find_Static_Alternative;
8750
8751 ------------------
8752 -- First_Actual --
8753 ------------------
8754
8755 function First_Actual (Node : Node_Id) return Node_Id is
8756 N : Node_Id;
8757
8758 begin
8759 if No (Parameter_Associations (Node)) then
8760 return Empty;
8761 end if;
8762
8763 N := First (Parameter_Associations (Node));
8764
8765 if Nkind (N) = N_Parameter_Association then
8766 return First_Named_Actual (Node);
8767 else
8768 return N;
8769 end if;
8770 end First_Actual;
8771
8772 ------------------
8773 -- First_Global --
8774 ------------------
8775
8776 function First_Global
8777 (Subp : Entity_Id;
8778 Global_Mode : Name_Id;
8779 Refined : Boolean := False) return Node_Id
8780 is
8781 function First_From_Global_List
8782 (List : Node_Id;
8783 Global_Mode : Name_Id := Name_Input) return Entity_Id;
8784 -- Get the first item with suitable mode from List
8785
8786 ----------------------------
8787 -- First_From_Global_List --
8788 ----------------------------
8789
8790 function First_From_Global_List
8791 (List : Node_Id;
8792 Global_Mode : Name_Id := Name_Input) return Entity_Id
8793 is
8794 Assoc : Node_Id;
8795
8796 begin
8797 -- Empty list (no global items)
8798
8799 if Nkind (List) = N_Null then
8800 return Empty;
8801
8802 -- Single global item declaration (only input items)
8803
8804 elsif Nkind_In (List, N_Expanded_Name, N_Identifier) then
8805 if Global_Mode = Name_Input then
8806 return List;
8807 else
8808 return Empty;
8809 end if;
8810
8811 -- Simple global list (only input items) or moded global list
8812 -- declaration.
8813
8814 elsif Nkind (List) = N_Aggregate then
8815 if Present (Expressions (List)) then
8816 if Global_Mode = Name_Input then
8817 return First (Expressions (List));
8818 else
8819 return Empty;
8820 end if;
8821
8822 else
8823 Assoc := First (Component_Associations (List));
8824 while Present (Assoc) loop
8825
8826 -- When we find the desired mode in an association, call
8827 -- recursively First_From_Global_List as if the mode was
8828 -- Name_Input, in order to reuse the existing machinery
8829 -- for the other cases.
8830
8831 if Chars (First (Choices (Assoc))) = Global_Mode then
8832 return First_From_Global_List (Expression (Assoc));
8833 end if;
8834
8835 Next (Assoc);
8836 end loop;
8837
8838 return Empty;
8839 end if;
8840
8841 -- To accommodate partial decoration of disabled SPARK features,
8842 -- this routine may be called with illegal input. If this is the
8843 -- case, do not raise Program_Error.
8844
8845 else
8846 return Empty;
8847 end if;
8848 end First_From_Global_List;
8849
8850 -- Local variables
8851
8852 Global : Node_Id := Empty;
8853 Body_Id : Entity_Id;
8854
8855 -- Start of processing for First_Global
8856
8857 begin
8858 pragma Assert (Nam_In (Global_Mode, Name_In_Out,
8859 Name_Input,
8860 Name_Output,
8861 Name_Proof_In));
8862
8863 -- Retrieve the suitable pragma Global or Refined_Global. In the second
8864 -- case, it can only be located on the body entity.
8865
8866 if Refined then
8867 if Is_Subprogram_Or_Generic_Subprogram (Subp) then
8868 Body_Id := Subprogram_Body_Entity (Subp);
8869
8870 elsif Is_Entry (Subp) or else Is_Task_Type (Subp) then
8871 Body_Id := Corresponding_Body (Parent (Subp));
8872
8873 -- ??? It should be possible to retrieve the Refined_Global on the
8874 -- task body associated to the task object. This is not yet possible.
8875
8876 elsif Is_Single_Task_Object (Subp) then
8877 Body_Id := Empty;
8878
8879 else
8880 Body_Id := Empty;
8881 end if;
8882
8883 if Present (Body_Id) then
8884 Global := Get_Pragma (Body_Id, Pragma_Refined_Global);
8885 end if;
8886 else
8887 Global := Get_Pragma (Subp, Pragma_Global);
8888 end if;
8889
8890 -- No corresponding global if pragma is not present
8891
8892 if No (Global) then
8893 return Empty;
8894
8895 -- Otherwise retrieve the corresponding list of items depending on the
8896 -- Global_Mode.
8897
8898 else
8899 return First_From_Global_List
8900 (Expression (Get_Argument (Global, Subp)), Global_Mode);
8901 end if;
8902 end First_Global;
8903
8904 -------------
8905 -- Fix_Msg --
8906 -------------
8907
8908 function Fix_Msg (Id : Entity_Id; Msg : String) return String is
8909 Is_Task : constant Boolean :=
8910 Ekind_In (Id, E_Task_Body, E_Task_Type)
8911 or else Is_Single_Task_Object (Id);
8912 Msg_Last : constant Natural := Msg'Last;
8913 Msg_Index : Natural;
8914 Res : String (Msg'Range) := (others => ' ');
8915 Res_Index : Natural;
8916
8917 begin
8918 -- Copy all characters from the input message Msg to result Res with
8919 -- suitable replacements.
8920
8921 Msg_Index := Msg'First;
8922 Res_Index := Res'First;
8923 while Msg_Index <= Msg_Last loop
8924
8925 -- Replace "subprogram" with a different word
8926
8927 if Msg_Index <= Msg_Last - 10
8928 and then Msg (Msg_Index .. Msg_Index + 9) = "subprogram"
8929 then
8930 if Ekind_In (Id, E_Entry, E_Entry_Family) then
8931 Res (Res_Index .. Res_Index + 4) := "entry";
8932 Res_Index := Res_Index + 5;
8933
8934 elsif Is_Task then
8935 Res (Res_Index .. Res_Index + 8) := "task type";
8936 Res_Index := Res_Index + 9;
8937
8938 else
8939 Res (Res_Index .. Res_Index + 9) := "subprogram";
8940 Res_Index := Res_Index + 10;
8941 end if;
8942
8943 Msg_Index := Msg_Index + 10;
8944
8945 -- Replace "protected" with a different word
8946
8947 elsif Msg_Index <= Msg_Last - 9
8948 and then Msg (Msg_Index .. Msg_Index + 8) = "protected"
8949 and then Is_Task
8950 then
8951 Res (Res_Index .. Res_Index + 3) := "task";
8952 Res_Index := Res_Index + 4;
8953 Msg_Index := Msg_Index + 9;
8954
8955 -- Otherwise copy the character
8956
8957 else
8958 Res (Res_Index) := Msg (Msg_Index);
8959 Msg_Index := Msg_Index + 1;
8960 Res_Index := Res_Index + 1;
8961 end if;
8962 end loop;
8963
8964 return Res (Res'First .. Res_Index - 1);
8965 end Fix_Msg;
8966
8967 -------------------------
8968 -- From_Nested_Package --
8969 -------------------------
8970
8971 function From_Nested_Package (T : Entity_Id) return Boolean is
8972 Pack : constant Entity_Id := Scope (T);
8973
8974 begin
8975 return
8976 Ekind (Pack) = E_Package
8977 and then not Is_Frozen (Pack)
8978 and then not Scope_Within_Or_Same (Current_Scope, Pack)
8979 and then In_Open_Scopes (Scope (Pack));
8980 end From_Nested_Package;
8981
8982 -----------------------
8983 -- Gather_Components --
8984 -----------------------
8985
8986 procedure Gather_Components
8987 (Typ : Entity_Id;
8988 Comp_List : Node_Id;
8989 Governed_By : List_Id;
8990 Into : Elist_Id;
8991 Report_Errors : out Boolean)
8992 is
8993 Assoc : Node_Id;
8994 Variant : Node_Id;
8995 Discrete_Choice : Node_Id;
8996 Comp_Item : Node_Id;
8997 Discrim : Entity_Id;
8998 Discrim_Name : Node_Id;
8999
9000 type Discriminant_Value_Status is
9001 (Static_Expr, Static_Subtype, Bad);
9002 subtype Good_Discrim_Value_Status is Discriminant_Value_Status
9003 range Static_Expr .. Static_Subtype; -- range excludes Bad
9004
9005 Discrim_Value : Node_Id;
9006 Discrim_Value_Subtype : Node_Id;
9007 Discrim_Value_Status : Discriminant_Value_Status := Bad;
9008 begin
9009 Report_Errors := False;
9010
9011 if No (Comp_List) or else Null_Present (Comp_List) then
9012 return;
9013
9014 elsif Present (Component_Items (Comp_List)) then
9015 Comp_Item := First (Component_Items (Comp_List));
9016
9017 else
9018 Comp_Item := Empty;
9019 end if;
9020
9021 while Present (Comp_Item) loop
9022
9023 -- Skip the tag of a tagged record, the interface tags, as well
9024 -- as all items that are not user components (anonymous types,
9025 -- rep clauses, Parent field, controller field).
9026
9027 if Nkind (Comp_Item) = N_Component_Declaration then
9028 declare
9029 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
9030 begin
9031 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
9032 Append_Elmt (Comp, Into);
9033 end if;
9034 end;
9035 end if;
9036
9037 Next (Comp_Item);
9038 end loop;
9039
9040 if No (Variant_Part (Comp_List)) then
9041 return;
9042 else
9043 Discrim_Name := Name (Variant_Part (Comp_List));
9044 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
9045 end if;
9046
9047 -- Look for the discriminant that governs this variant part.
9048 -- The discriminant *must* be in the Governed_By List
9049
9050 Assoc := First (Governed_By);
9051 Find_Constraint : loop
9052 Discrim := First (Choices (Assoc));
9053 exit Find_Constraint when
9054 Chars (Discrim_Name) = Chars (Discrim)
9055 or else
9056 (Present (Corresponding_Discriminant (Entity (Discrim)))
9057 and then Chars (Corresponding_Discriminant
9058 (Entity (Discrim))) = Chars (Discrim_Name))
9059 or else
9060 Chars (Original_Record_Component (Entity (Discrim))) =
9061 Chars (Discrim_Name);
9062
9063 if No (Next (Assoc)) then
9064 if not Is_Constrained (Typ) and then Is_Derived_Type (Typ) then
9065
9066 -- If the type is a tagged type with inherited discriminants,
9067 -- use the stored constraint on the parent in order to find
9068 -- the values of discriminants that are otherwise hidden by an
9069 -- explicit constraint. Renamed discriminants are handled in
9070 -- the code above.
9071
9072 -- If several parent discriminants are renamed by a single
9073 -- discriminant of the derived type, the call to obtain the
9074 -- Corresponding_Discriminant field only retrieves the last
9075 -- of them. We recover the constraint on the others from the
9076 -- Stored_Constraint as well.
9077
9078 -- An inherited discriminant may have been constrained in a
9079 -- later ancestor (not the immediate parent) so we must examine
9080 -- the stored constraint of all of them to locate the inherited
9081 -- value.
9082
9083 declare
9084 C : Elmt_Id;
9085 D : Entity_Id;
9086 T : Entity_Id := Typ;
9087
9088 begin
9089 while Is_Derived_Type (T) loop
9090 if Present (Stored_Constraint (T)) then
9091 D := First_Discriminant (Etype (T));
9092 C := First_Elmt (Stored_Constraint (T));
9093 while Present (D) and then Present (C) loop
9094 if Chars (Discrim_Name) = Chars (D) then
9095 if Is_Entity_Name (Node (C))
9096 and then Entity (Node (C)) = Entity (Discrim)
9097 then
9098 -- D is renamed by Discrim, whose value is
9099 -- given in Assoc.
9100
9101 null;
9102
9103 else
9104 Assoc :=
9105 Make_Component_Association (Sloc (Typ),
9106 New_List
9107 (New_Occurrence_Of (D, Sloc (Typ))),
9108 Duplicate_Subexpr_No_Checks (Node (C)));
9109 end if;
9110
9111 exit Find_Constraint;
9112 end if;
9113
9114 Next_Discriminant (D);
9115 Next_Elmt (C);
9116 end loop;
9117 end if;
9118
9119 -- Discriminant may be inherited from ancestor
9120
9121 T := Etype (T);
9122 end loop;
9123 end;
9124 end if;
9125 end if;
9126
9127 if No (Next (Assoc)) then
9128 Error_Msg_NE
9129 (" missing value for discriminant&",
9130 First (Governed_By), Discrim_Name);
9131
9132 Report_Errors := True;
9133 return;
9134 end if;
9135
9136 Next (Assoc);
9137 end loop Find_Constraint;
9138
9139 Discrim_Value := Expression (Assoc);
9140 if Is_OK_Static_Expression (Discrim_Value) then
9141 Discrim_Value_Status := Static_Expr;
9142 else
9143 if Ada_Version >= Ada_2020 then
9144 if Original_Node (Discrim_Value) /= Discrim_Value
9145 and then Nkind (Discrim_Value) = N_Type_Conversion
9146 and then Etype (Original_Node (Discrim_Value))
9147 = Etype (Expression (Discrim_Value))
9148 then
9149 Discrim_Value_Subtype := Etype (Original_Node (Discrim_Value));
9150 -- An unhelpful (for this code) type conversion may be
9151 -- introduced in some cases; deal with it.
9152 else
9153 Discrim_Value_Subtype := Etype (Discrim_Value);
9154 end if;
9155
9156 if Is_OK_Static_Subtype (Discrim_Value_Subtype) and then
9157 not Is_Null_Range (Type_Low_Bound (Discrim_Value_Subtype),
9158 Type_High_Bound (Discrim_Value_Subtype))
9159 then
9160 -- Is_Null_Range test doesn't account for predicates, as in
9161 -- subtype Null_By_Predicate is Natural
9162 -- with Static_Predicate => Null_By_Predicate < 0;
9163 -- so test for that null case separately.
9164
9165 if (not Has_Static_Predicate (Discrim_Value_Subtype))
9166 or else Present (First (Static_Discrete_Predicate
9167 (Discrim_Value_Subtype)))
9168 then
9169 Discrim_Value_Status := Static_Subtype;
9170 end if;
9171 end if;
9172 end if;
9173
9174 if Discrim_Value_Status = Bad then
9175
9176 -- If the variant part is governed by a discriminant of the type
9177 -- this is an error. If the variant part and the discriminant are
9178 -- inherited from an ancestor this is legal (AI05-220) unless the
9179 -- components are being gathered for an aggregate, in which case
9180 -- the caller must check Report_Errors.
9181 --
9182 -- In Ada 2020 the above rules are relaxed. A nonstatic governing
9183 -- discriminant is OK as long as it has a static subtype and
9184 -- every value of that subtype (and there must be at least one)
9185 -- selects the same variant.
9186
9187 if Scope (Original_Record_Component
9188 ((Entity (First (Choices (Assoc)))))) = Typ
9189 then
9190 if Ada_Version >= Ada_2020 then
9191 Error_Msg_FE
9192 ("value for discriminant & must be static or " &
9193 "discriminant's nominal subtype must be static " &
9194 "and non-null!",
9195 Discrim_Value, Discrim);
9196 else
9197 Error_Msg_FE
9198 ("value for discriminant & must be static!",
9199 Discrim_Value, Discrim);
9200 end if;
9201 Why_Not_Static (Discrim_Value);
9202 end if;
9203
9204 Report_Errors := True;
9205 return;
9206 end if;
9207 end if;
9208
9209 Search_For_Discriminant_Value : declare
9210 Low : Node_Id;
9211 High : Node_Id;
9212
9213 UI_High : Uint;
9214 UI_Low : Uint;
9215 UI_Discrim_Value : Uint;
9216
9217 begin
9218 case Good_Discrim_Value_Status'(Discrim_Value_Status) is
9219 when Static_Expr =>
9220 UI_Discrim_Value := Expr_Value (Discrim_Value);
9221 when Static_Subtype =>
9222 -- Arbitrarily pick one value of the subtype and look
9223 -- for the variant associated with that value; we will
9224 -- check later that the same variant is associated with
9225 -- all of the other values of the subtype.
9226 if Has_Static_Predicate (Discrim_Value_Subtype) then
9227 declare
9228 Range_Or_Expr : constant Node_Id :=
9229 First (Static_Discrete_Predicate
9230 (Discrim_Value_Subtype));
9231 begin
9232 if Nkind (Range_Or_Expr) = N_Range then
9233 UI_Discrim_Value :=
9234 Expr_Value (Low_Bound (Range_Or_Expr));
9235 else
9236 UI_Discrim_Value := Expr_Value (Range_Or_Expr);
9237 end if;
9238 end;
9239 else
9240 UI_Discrim_Value
9241 := Expr_Value (Type_Low_Bound (Discrim_Value_Subtype));
9242 end if;
9243 end case;
9244
9245 Find_Discrete_Value : while Present (Variant) loop
9246
9247 -- If a choice is a subtype with a static predicate, it must
9248 -- be rewritten as an explicit list of non-predicated choices.
9249
9250 Expand_Static_Predicates_In_Choices (Variant);
9251
9252 Discrete_Choice := First (Discrete_Choices (Variant));
9253 while Present (Discrete_Choice) loop
9254 exit Find_Discrete_Value when
9255 Nkind (Discrete_Choice) = N_Others_Choice;
9256
9257 Get_Index_Bounds (Discrete_Choice, Low, High);
9258
9259 UI_Low := Expr_Value (Low);
9260 UI_High := Expr_Value (High);
9261
9262 exit Find_Discrete_Value when
9263 UI_Low <= UI_Discrim_Value
9264 and then
9265 UI_High >= UI_Discrim_Value;
9266
9267 Next (Discrete_Choice);
9268 end loop;
9269
9270 Next_Non_Pragma (Variant);
9271 end loop Find_Discrete_Value;
9272 end Search_For_Discriminant_Value;
9273
9274 -- The case statement must include a variant that corresponds to the
9275 -- value of the discriminant, unless the discriminant type has a
9276 -- static predicate. In that case the absence of an others_choice that
9277 -- would cover this value becomes a run-time error (3.8.1 (21.1/2)).
9278
9279 if No (Variant)
9280 and then not Has_Static_Predicate (Etype (Discrim_Name))
9281 then
9282 Error_Msg_NE
9283 ("value of discriminant & is out of range", Discrim_Value, Discrim);
9284 Report_Errors := True;
9285 return;
9286 end if;
9287
9288 -- If we have found the corresponding choice, recursively add its
9289 -- components to the Into list. The nested components are part of
9290 -- the same record type.
9291
9292 if Present (Variant) then
9293 if Discrim_Value_Status = Static_Subtype then
9294 declare
9295 Discrim_Value_Subtype_Intervals
9296 : constant Interval_Lists.Discrete_Interval_List
9297 := Interval_Lists.Type_Intervals (Discrim_Value_Subtype);
9298
9299 Variant_Intervals
9300 : constant Interval_Lists.Discrete_Interval_List
9301 := Interval_Lists.Choice_List_Intervals
9302 (Discrete_Choices => Discrete_Choices (Variant));
9303 begin
9304 if not Interval_Lists.Is_Subset
9305 (Subset => Discrim_Value_Subtype_Intervals,
9306 Of_Set => Variant_Intervals)
9307 then
9308 Error_Msg_NE
9309 ("no single variant is associated with all values of " &
9310 "the subtype of discriminant value &",
9311 Discrim_Value, Discrim);
9312 Report_Errors := True;
9313 return;
9314 end if;
9315 end;
9316 end if;
9317
9318 Gather_Components
9319 (Typ, Component_List (Variant), Governed_By, Into, Report_Errors);
9320 end if;
9321 end Gather_Components;
9322
9323 -----------------------
9324 -- Get_Accessibility --
9325 -----------------------
9326
9327 function Get_Accessibility (E : Entity_Id) return Node_Id is
9328 begin
9329 -- When minimum accessibility is set for E then we utilize it - except
9330 -- in a few edge cases like the expansion of select statements where
9331 -- generated subprogram may attempt to unnecessarily use a minimum
9332 -- accessibility object declared outside of scope.
9333
9334 -- To avoid these situations where expansion may get complex we verify
9335 -- that the minimum accessibility object is within scope.
9336
9337 if Ekind (E) in Formal_Kind
9338 and then Present (Minimum_Accessibility (E))
9339 and then In_Open_Scopes (Scope (Minimum_Accessibility (E)))
9340 then
9341 return Minimum_Accessibility (E);
9342 end if;
9343
9344 return Extra_Accessibility (E);
9345 end Get_Accessibility;
9346
9347 ------------------------
9348 -- Get_Actual_Subtype --
9349 ------------------------
9350
9351 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
9352 Typ : constant Entity_Id := Etype (N);
9353 Utyp : Entity_Id := Underlying_Type (Typ);
9354 Decl : Node_Id;
9355 Atyp : Entity_Id;
9356
9357 begin
9358 if No (Utyp) then
9359 Utyp := Typ;
9360 end if;
9361
9362 -- If what we have is an identifier that references a subprogram
9363 -- formal, or a variable or constant object, then we get the actual
9364 -- subtype from the referenced entity if one has been built.
9365
9366 if Nkind (N) = N_Identifier
9367 and then
9368 (Is_Formal (Entity (N))
9369 or else Ekind (Entity (N)) = E_Constant
9370 or else Ekind (Entity (N)) = E_Variable)
9371 and then Present (Actual_Subtype (Entity (N)))
9372 then
9373 return Actual_Subtype (Entity (N));
9374
9375 -- Actual subtype of unchecked union is always itself. We never need
9376 -- the "real" actual subtype. If we did, we couldn't get it anyway
9377 -- because the discriminant is not available. The restrictions on
9378 -- Unchecked_Union are designed to make sure that this is OK.
9379
9380 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
9381 return Typ;
9382
9383 -- Here for the unconstrained case, we must find actual subtype
9384 -- No actual subtype is available, so we must build it on the fly.
9385
9386 -- Checking the type, not the underlying type, for constrainedness
9387 -- seems to be necessary. Maybe all the tests should be on the type???
9388
9389 elsif (not Is_Constrained (Typ))
9390 and then (Is_Array_Type (Utyp)
9391 or else (Is_Record_Type (Utyp)
9392 and then Has_Discriminants (Utyp)))
9393 and then not Has_Unknown_Discriminants (Utyp)
9394 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
9395 then
9396 -- Nothing to do if in spec expression (why not???)
9397
9398 if In_Spec_Expression then
9399 return Typ;
9400
9401 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
9402
9403 -- If the type has no discriminants, there is no subtype to
9404 -- build, even if the underlying type is discriminated.
9405
9406 return Typ;
9407
9408 -- Else build the actual subtype
9409
9410 else
9411 Decl := Build_Actual_Subtype (Typ, N);
9412
9413 -- The call may yield a declaration, or just return the entity
9414
9415 if Decl = Typ then
9416 return Typ;
9417 end if;
9418
9419 Atyp := Defining_Identifier (Decl);
9420
9421 -- If Build_Actual_Subtype generated a new declaration then use it
9422
9423 if Atyp /= Typ then
9424
9425 -- The actual subtype is an Itype, so analyze the declaration,
9426 -- but do not attach it to the tree, to get the type defined.
9427
9428 Set_Parent (Decl, N);
9429 Set_Is_Itype (Atyp);
9430 Analyze (Decl, Suppress => All_Checks);
9431 Set_Associated_Node_For_Itype (Atyp, N);
9432 Set_Has_Delayed_Freeze (Atyp, False);
9433
9434 -- We need to freeze the actual subtype immediately. This is
9435 -- needed, because otherwise this Itype will not get frozen
9436 -- at all, and it is always safe to freeze on creation because
9437 -- any associated types must be frozen at this point.
9438
9439 Freeze_Itype (Atyp, N);
9440 return Atyp;
9441
9442 -- Otherwise we did not build a declaration, so return original
9443
9444 else
9445 return Typ;
9446 end if;
9447 end if;
9448
9449 -- For all remaining cases, the actual subtype is the same as
9450 -- the nominal type.
9451
9452 else
9453 return Typ;
9454 end if;
9455 end Get_Actual_Subtype;
9456
9457 -------------------------------------
9458 -- Get_Actual_Subtype_If_Available --
9459 -------------------------------------
9460
9461 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
9462 Typ : constant Entity_Id := Etype (N);
9463
9464 begin
9465 -- If what we have is an identifier that references a subprogram
9466 -- formal, or a variable or constant object, then we get the actual
9467 -- subtype from the referenced entity if one has been built.
9468
9469 if Nkind (N) = N_Identifier
9470 and then
9471 (Is_Formal (Entity (N))
9472 or else Ekind (Entity (N)) = E_Constant
9473 or else Ekind (Entity (N)) = E_Variable)
9474 and then Present (Actual_Subtype (Entity (N)))
9475 then
9476 return Actual_Subtype (Entity (N));
9477
9478 -- Otherwise the Etype of N is returned unchanged
9479
9480 else
9481 return Typ;
9482 end if;
9483 end Get_Actual_Subtype_If_Available;
9484
9485 ------------------------
9486 -- Get_Body_From_Stub --
9487 ------------------------
9488
9489 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
9490 begin
9491 return Proper_Body (Unit (Library_Unit (N)));
9492 end Get_Body_From_Stub;
9493
9494 ---------------------
9495 -- Get_Cursor_Type --
9496 ---------------------
9497
9498 function Get_Cursor_Type
9499 (Aspect : Node_Id;
9500 Typ : Entity_Id) return Entity_Id
9501 is
9502 Assoc : Node_Id;
9503 Func : Entity_Id;
9504 First_Op : Entity_Id;
9505 Cursor : Entity_Id;
9506
9507 begin
9508 -- If error already detected, return
9509
9510 if Error_Posted (Aspect) then
9511 return Any_Type;
9512 end if;
9513
9514 -- The cursor type for an Iterable aspect is the return type of a
9515 -- non-overloaded First primitive operation. Locate association for
9516 -- First.
9517
9518 Assoc := First (Component_Associations (Expression (Aspect)));
9519 First_Op := Any_Id;
9520 while Present (Assoc) loop
9521 if Chars (First (Choices (Assoc))) = Name_First then
9522 First_Op := Expression (Assoc);
9523 exit;
9524 end if;
9525
9526 Next (Assoc);
9527 end loop;
9528
9529 if First_Op = Any_Id then
9530 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
9531 return Any_Type;
9532
9533 elsif not Analyzed (First_Op) then
9534 Analyze (First_Op);
9535 end if;
9536
9537 Cursor := Any_Type;
9538
9539 -- Locate function with desired name and profile in scope of type
9540 -- In the rare case where the type is an integer type, a base type
9541 -- is created for it, check that the base type of the first formal
9542 -- of First matches the base type of the domain.
9543
9544 Func := First_Entity (Scope (Typ));
9545 while Present (Func) loop
9546 if Chars (Func) = Chars (First_Op)
9547 and then Ekind (Func) = E_Function
9548 and then Present (First_Formal (Func))
9549 and then Base_Type (Etype (First_Formal (Func))) = Base_Type (Typ)
9550 and then No (Next_Formal (First_Formal (Func)))
9551 then
9552 if Cursor /= Any_Type then
9553 Error_Msg_N
9554 ("Operation First for iterable type must be unique", Aspect);
9555 return Any_Type;
9556 else
9557 Cursor := Etype (Func);
9558 end if;
9559 end if;
9560
9561 Next_Entity (Func);
9562 end loop;
9563
9564 -- If not found, no way to resolve remaining primitives
9565
9566 if Cursor = Any_Type then
9567 Error_Msg_N
9568 ("primitive operation for Iterable type must appear in the same "
9569 & "list of declarations as the type", Aspect);
9570 end if;
9571
9572 return Cursor;
9573 end Get_Cursor_Type;
9574
9575 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
9576 begin
9577 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
9578 end Get_Cursor_Type;
9579
9580 -------------------------------
9581 -- Get_Default_External_Name --
9582 -------------------------------
9583
9584 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
9585 begin
9586 Get_Decoded_Name_String (Chars (E));
9587
9588 if Opt.External_Name_Imp_Casing = Uppercase then
9589 Set_Casing (All_Upper_Case);
9590 else
9591 Set_Casing (All_Lower_Case);
9592 end if;
9593
9594 return
9595 Make_String_Literal (Sloc (E),
9596 Strval => String_From_Name_Buffer);
9597 end Get_Default_External_Name;
9598
9599 --------------------------
9600 -- Get_Enclosing_Object --
9601 --------------------------
9602
9603 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
9604 begin
9605 if Is_Entity_Name (N) then
9606 return Entity (N);
9607 else
9608 case Nkind (N) is
9609 when N_Indexed_Component
9610 | N_Selected_Component
9611 | N_Slice
9612 =>
9613 -- If not generating code, a dereference may be left implicit.
9614 -- In thoses cases, return Empty.
9615
9616 if Is_Access_Type (Etype (Prefix (N))) then
9617 return Empty;
9618 else
9619 return Get_Enclosing_Object (Prefix (N));
9620 end if;
9621
9622 when N_Type_Conversion =>
9623 return Get_Enclosing_Object (Expression (N));
9624
9625 when others =>
9626 return Empty;
9627 end case;
9628 end if;
9629 end Get_Enclosing_Object;
9630
9631 ---------------------------
9632 -- Get_Enum_Lit_From_Pos --
9633 ---------------------------
9634
9635 function Get_Enum_Lit_From_Pos
9636 (T : Entity_Id;
9637 Pos : Uint;
9638 Loc : Source_Ptr) return Node_Id
9639 is
9640 Btyp : Entity_Id := Base_Type (T);
9641 Lit : Node_Id;
9642 LLoc : Source_Ptr;
9643
9644 begin
9645 -- In the case where the literal is of type Character, Wide_Character
9646 -- or Wide_Wide_Character or of a type derived from them, there needs
9647 -- to be some special handling since there is no explicit chain of
9648 -- literals to search. Instead, an N_Character_Literal node is created
9649 -- with the appropriate Char_Code and Chars fields.
9650
9651 if Is_Standard_Character_Type (T) then
9652 Set_Character_Literal_Name (UI_To_CC (Pos));
9653
9654 return
9655 Make_Character_Literal (Loc,
9656 Chars => Name_Find,
9657 Char_Literal_Value => Pos);
9658
9659 -- For all other cases, we have a complete table of literals, and
9660 -- we simply iterate through the chain of literal until the one
9661 -- with the desired position value is found.
9662
9663 else
9664 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
9665 Btyp := Full_View (Btyp);
9666 end if;
9667
9668 Lit := First_Literal (Btyp);
9669
9670 -- Position in the enumeration type starts at 0
9671
9672 if UI_To_Int (Pos) < 0 then
9673 raise Constraint_Error;
9674 end if;
9675
9676 for J in 1 .. UI_To_Int (Pos) loop
9677 Next_Literal (Lit);
9678
9679 -- If Lit is Empty, Pos is not in range, so raise Constraint_Error
9680 -- inside the loop to avoid calling Next_Literal on Empty.
9681
9682 if No (Lit) then
9683 raise Constraint_Error;
9684 end if;
9685 end loop;
9686
9687 -- Create a new node from Lit, with source location provided by Loc
9688 -- if not equal to No_Location, or by copying the source location of
9689 -- Lit otherwise.
9690
9691 LLoc := Loc;
9692
9693 if LLoc = No_Location then
9694 LLoc := Sloc (Lit);
9695 end if;
9696
9697 return New_Occurrence_Of (Lit, LLoc);
9698 end if;
9699 end Get_Enum_Lit_From_Pos;
9700
9701 ------------------------
9702 -- Get_Generic_Entity --
9703 ------------------------
9704
9705 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
9706 Ent : constant Entity_Id := Entity (Name (N));
9707 begin
9708 if Present (Renamed_Object (Ent)) then
9709 return Renamed_Object (Ent);
9710 else
9711 return Ent;
9712 end if;
9713 end Get_Generic_Entity;
9714
9715 -------------------------------------
9716 -- Get_Incomplete_View_Of_Ancestor --
9717 -------------------------------------
9718
9719 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
9720 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9721 Par_Scope : Entity_Id;
9722 Par_Type : Entity_Id;
9723
9724 begin
9725 -- The incomplete view of an ancestor is only relevant for private
9726 -- derived types in child units.
9727
9728 if not Is_Derived_Type (E)
9729 or else not Is_Child_Unit (Cur_Unit)
9730 then
9731 return Empty;
9732
9733 else
9734 Par_Scope := Scope (Cur_Unit);
9735 if No (Par_Scope) then
9736 return Empty;
9737 end if;
9738
9739 Par_Type := Etype (Base_Type (E));
9740
9741 -- Traverse list of ancestor types until we find one declared in
9742 -- a parent or grandparent unit (two levels seem sufficient).
9743
9744 while Present (Par_Type) loop
9745 if Scope (Par_Type) = Par_Scope
9746 or else Scope (Par_Type) = Scope (Par_Scope)
9747 then
9748 return Par_Type;
9749
9750 elsif not Is_Derived_Type (Par_Type) then
9751 return Empty;
9752
9753 else
9754 Par_Type := Etype (Base_Type (Par_Type));
9755 end if;
9756 end loop;
9757
9758 -- If none found, there is no relevant ancestor type.
9759
9760 return Empty;
9761 end if;
9762 end Get_Incomplete_View_Of_Ancestor;
9763
9764 ----------------------
9765 -- Get_Index_Bounds --
9766 ----------------------
9767
9768 procedure Get_Index_Bounds
9769 (N : Node_Id;
9770 L : out Node_Id;
9771 H : out Node_Id;
9772 Use_Full_View : Boolean := False)
9773 is
9774 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id;
9775 -- Obtain the scalar range of type Typ. If flag Use_Full_View is set and
9776 -- Typ qualifies, the scalar range is obtained from the full view of the
9777 -- type.
9778
9779 --------------------------
9780 -- Scalar_Range_Of_Type --
9781 --------------------------
9782
9783 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id is
9784 T : Entity_Id := Typ;
9785
9786 begin
9787 if Use_Full_View and then Present (Full_View (T)) then
9788 T := Full_View (T);
9789 end if;
9790
9791 return Scalar_Range (T);
9792 end Scalar_Range_Of_Type;
9793
9794 -- Local variables
9795
9796 Kind : constant Node_Kind := Nkind (N);
9797 Rng : Node_Id;
9798
9799 -- Start of processing for Get_Index_Bounds
9800
9801 begin
9802 if Kind = N_Range then
9803 L := Low_Bound (N);
9804 H := High_Bound (N);
9805
9806 elsif Kind = N_Subtype_Indication then
9807 Rng := Range_Expression (Constraint (N));
9808
9809 if Rng = Error then
9810 L := Error;
9811 H := Error;
9812 return;
9813
9814 else
9815 L := Low_Bound (Range_Expression (Constraint (N)));
9816 H := High_Bound (Range_Expression (Constraint (N)));
9817 end if;
9818
9819 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9820 Rng := Scalar_Range_Of_Type (Entity (N));
9821
9822 if Error_Posted (Rng) then
9823 L := Error;
9824 H := Error;
9825
9826 elsif Nkind (Rng) = N_Subtype_Indication then
9827 Get_Index_Bounds (Rng, L, H);
9828
9829 else
9830 L := Low_Bound (Rng);
9831 H := High_Bound (Rng);
9832 end if;
9833
9834 else
9835 -- N is an expression, indicating a range with one value
9836
9837 L := N;
9838 H := N;
9839 end if;
9840 end Get_Index_Bounds;
9841
9842 -----------------------------
9843 -- Get_Interfacing_Aspects --
9844 -----------------------------
9845
9846 procedure Get_Interfacing_Aspects
9847 (Iface_Asp : Node_Id;
9848 Conv_Asp : out Node_Id;
9849 EN_Asp : out Node_Id;
9850 Expo_Asp : out Node_Id;
9851 Imp_Asp : out Node_Id;
9852 LN_Asp : out Node_Id;
9853 Do_Checks : Boolean := False)
9854 is
9855 procedure Save_Or_Duplication_Error
9856 (Asp : Node_Id;
9857 To : in out Node_Id);
9858 -- Save the value of aspect Asp in node To. If To already has a value,
9859 -- then this is considered a duplicate use of aspect. Emit an error if
9860 -- flag Do_Checks is set.
9861
9862 -------------------------------
9863 -- Save_Or_Duplication_Error --
9864 -------------------------------
9865
9866 procedure Save_Or_Duplication_Error
9867 (Asp : Node_Id;
9868 To : in out Node_Id)
9869 is
9870 begin
9871 -- Detect an extra aspect and issue an error
9872
9873 if Present (To) then
9874 if Do_Checks then
9875 Error_Msg_Name_1 := Chars (Identifier (Asp));
9876 Error_Msg_Sloc := Sloc (To);
9877 Error_Msg_N ("aspect % previously given #", Asp);
9878 end if;
9879
9880 -- Otherwise capture the aspect
9881
9882 else
9883 To := Asp;
9884 end if;
9885 end Save_Or_Duplication_Error;
9886
9887 -- Local variables
9888
9889 Asp : Node_Id;
9890 Asp_Id : Aspect_Id;
9891
9892 -- The following variables capture each individual aspect
9893
9894 Conv : Node_Id := Empty;
9895 EN : Node_Id := Empty;
9896 Expo : Node_Id := Empty;
9897 Imp : Node_Id := Empty;
9898 LN : Node_Id := Empty;
9899
9900 -- Start of processing for Get_Interfacing_Aspects
9901
9902 begin
9903 -- The input interfacing aspect should reside in an aspect specification
9904 -- list.
9905
9906 pragma Assert (Is_List_Member (Iface_Asp));
9907
9908 -- Examine the aspect specifications of the related entity. Find and
9909 -- capture all interfacing aspects. Detect duplicates and emit errors
9910 -- if applicable.
9911
9912 Asp := First (List_Containing (Iface_Asp));
9913 while Present (Asp) loop
9914 Asp_Id := Get_Aspect_Id (Asp);
9915
9916 if Asp_Id = Aspect_Convention then
9917 Save_Or_Duplication_Error (Asp, Conv);
9918
9919 elsif Asp_Id = Aspect_External_Name then
9920 Save_Or_Duplication_Error (Asp, EN);
9921
9922 elsif Asp_Id = Aspect_Export then
9923 Save_Or_Duplication_Error (Asp, Expo);
9924
9925 elsif Asp_Id = Aspect_Import then
9926 Save_Or_Duplication_Error (Asp, Imp);
9927
9928 elsif Asp_Id = Aspect_Link_Name then
9929 Save_Or_Duplication_Error (Asp, LN);
9930 end if;
9931
9932 Next (Asp);
9933 end loop;
9934
9935 Conv_Asp := Conv;
9936 EN_Asp := EN;
9937 Expo_Asp := Expo;
9938 Imp_Asp := Imp;
9939 LN_Asp := LN;
9940 end Get_Interfacing_Aspects;
9941
9942 ---------------------------------
9943 -- Get_Iterable_Type_Primitive --
9944 ---------------------------------
9945
9946 function Get_Iterable_Type_Primitive
9947 (Typ : Entity_Id;
9948 Nam : Name_Id) return Entity_Id
9949 is
9950 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
9951 Assoc : Node_Id;
9952
9953 begin
9954 if No (Funcs) then
9955 return Empty;
9956
9957 else
9958 Assoc := First (Component_Associations (Funcs));
9959 while Present (Assoc) loop
9960 if Chars (First (Choices (Assoc))) = Nam then
9961 return Entity (Expression (Assoc));
9962 end if;
9963
9964 Assoc := Next (Assoc);
9965 end loop;
9966
9967 return Empty;
9968 end if;
9969 end Get_Iterable_Type_Primitive;
9970
9971 ----------------------------------
9972 -- Get_Library_Unit_Name_String --
9973 ----------------------------------
9974
9975 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
9976 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
9977
9978 begin
9979 Get_Unit_Name_String (Unit_Name_Id);
9980
9981 -- Remove seven last character (" (spec)" or " (body)")
9982
9983 Name_Len := Name_Len - 7;
9984 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
9985 end Get_Library_Unit_Name_String;
9986
9987 --------------------------
9988 -- Get_Max_Queue_Length --
9989 --------------------------
9990
9991 function Get_Max_Queue_Length (Id : Entity_Id) return Uint is
9992 pragma Assert (Is_Entry (Id));
9993 Prag : constant Entity_Id := Get_Pragma (Id, Pragma_Max_Queue_Length);
9994 Max : Uint;
9995
9996 begin
9997 -- A value of 0 or -1 represents no maximum specified, and entries and
9998 -- entry families with no Max_Queue_Length aspect or pragma default to
9999 -- it.
10000
10001 if not Present (Prag) then
10002 return Uint_0;
10003 end if;
10004
10005 Max := Expr_Value
10006 (Expression (First (Pragma_Argument_Associations (Prag))));
10007
10008 -- Since -1 and 0 are equivalent, return 0 for instances of -1 for
10009 -- uniformity.
10010
10011 if Max = -1 then
10012 return Uint_0;
10013 end if;
10014
10015 return Max;
10016 end Get_Max_Queue_Length;
10017
10018 ------------------------
10019 -- Get_Name_Entity_Id --
10020 ------------------------
10021
10022 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
10023 begin
10024 return Entity_Id (Get_Name_Table_Int (Id));
10025 end Get_Name_Entity_Id;
10026
10027 ------------------------------
10028 -- Get_Name_From_CTC_Pragma --
10029 ------------------------------
10030
10031 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
10032 Arg : constant Node_Id :=
10033 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
10034 begin
10035 return Strval (Expr_Value_S (Arg));
10036 end Get_Name_From_CTC_Pragma;
10037
10038 -----------------------
10039 -- Get_Parent_Entity --
10040 -----------------------
10041
10042 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
10043 begin
10044 if Nkind (Unit) = N_Package_Body
10045 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
10046 then
10047 return Defining_Entity
10048 (Specification (Instance_Spec (Original_Node (Unit))));
10049 elsif Nkind (Unit) = N_Package_Instantiation then
10050 return Defining_Entity (Specification (Instance_Spec (Unit)));
10051 else
10052 return Defining_Entity (Unit);
10053 end if;
10054 end Get_Parent_Entity;
10055
10056 -------------------
10057 -- Get_Pragma_Id --
10058 -------------------
10059
10060 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
10061 begin
10062 return Get_Pragma_Id (Pragma_Name_Unmapped (N));
10063 end Get_Pragma_Id;
10064
10065 ------------------------
10066 -- Get_Qualified_Name --
10067 ------------------------
10068
10069 function Get_Qualified_Name
10070 (Id : Entity_Id;
10071 Suffix : Entity_Id := Empty) return Name_Id
10072 is
10073 Suffix_Nam : Name_Id := No_Name;
10074
10075 begin
10076 if Present (Suffix) then
10077 Suffix_Nam := Chars (Suffix);
10078 end if;
10079
10080 return Get_Qualified_Name (Chars (Id), Suffix_Nam, Scope (Id));
10081 end Get_Qualified_Name;
10082
10083 function Get_Qualified_Name
10084 (Nam : Name_Id;
10085 Suffix : Name_Id := No_Name;
10086 Scop : Entity_Id := Current_Scope) return Name_Id
10087 is
10088 procedure Add_Scope (S : Entity_Id);
10089 -- Add the fully qualified form of scope S to the name buffer. The
10090 -- format is:
10091 -- s-1__s__
10092
10093 ---------------
10094 -- Add_Scope --
10095 ---------------
10096
10097 procedure Add_Scope (S : Entity_Id) is
10098 begin
10099 if S = Empty then
10100 null;
10101
10102 elsif S = Standard_Standard then
10103 null;
10104
10105 else
10106 Add_Scope (Scope (S));
10107 Get_Name_String_And_Append (Chars (S));
10108 Add_Str_To_Name_Buffer ("__");
10109 end if;
10110 end Add_Scope;
10111
10112 -- Start of processing for Get_Qualified_Name
10113
10114 begin
10115 Name_Len := 0;
10116 Add_Scope (Scop);
10117
10118 -- Append the base name after all scopes have been chained
10119
10120 Get_Name_String_And_Append (Nam);
10121
10122 -- Append the suffix (if present)
10123
10124 if Suffix /= No_Name then
10125 Add_Str_To_Name_Buffer ("__");
10126 Get_Name_String_And_Append (Suffix);
10127 end if;
10128
10129 return Name_Find;
10130 end Get_Qualified_Name;
10131
10132 -----------------------
10133 -- Get_Reason_String --
10134 -----------------------
10135
10136 procedure Get_Reason_String (N : Node_Id) is
10137 begin
10138 if Nkind (N) = N_String_Literal then
10139 Store_String_Chars (Strval (N));
10140
10141 elsif Nkind (N) = N_Op_Concat then
10142 Get_Reason_String (Left_Opnd (N));
10143 Get_Reason_String (Right_Opnd (N));
10144
10145 -- If not of required form, error
10146
10147 else
10148 Error_Msg_N
10149 ("Reason for pragma Warnings has wrong form", N);
10150 Error_Msg_N
10151 ("\must be string literal or concatenation of string literals", N);
10152 return;
10153 end if;
10154 end Get_Reason_String;
10155
10156 --------------------------------
10157 -- Get_Reference_Discriminant --
10158 --------------------------------
10159
10160 function Get_Reference_Discriminant (Typ : Entity_Id) return Entity_Id is
10161 D : Entity_Id;
10162
10163 begin
10164 D := First_Discriminant (Typ);
10165 while Present (D) loop
10166 if Has_Implicit_Dereference (D) then
10167 return D;
10168 end if;
10169 Next_Discriminant (D);
10170 end loop;
10171
10172 return Empty;
10173 end Get_Reference_Discriminant;
10174
10175 ---------------------------
10176 -- Get_Referenced_Object --
10177 ---------------------------
10178
10179 function Get_Referenced_Object (N : Node_Id) return Node_Id is
10180 R : Node_Id;
10181
10182 begin
10183 R := N;
10184 while Is_Entity_Name (R)
10185 and then Is_Object (Entity (R))
10186 and then Present (Renamed_Object (Entity (R)))
10187 loop
10188 R := Renamed_Object (Entity (R));
10189 end loop;
10190
10191 return R;
10192 end Get_Referenced_Object;
10193
10194 ------------------------
10195 -- Get_Renamed_Entity --
10196 ------------------------
10197
10198 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
10199 R : Entity_Id;
10200
10201 begin
10202 R := E;
10203 while Present (Renamed_Entity (R)) loop
10204 R := Renamed_Entity (R);
10205 end loop;
10206
10207 return R;
10208 end Get_Renamed_Entity;
10209
10210 -----------------------
10211 -- Get_Return_Object --
10212 -----------------------
10213
10214 function Get_Return_Object (N : Node_Id) return Entity_Id is
10215 Decl : Node_Id;
10216
10217 begin
10218 Decl := First (Return_Object_Declarations (N));
10219 while Present (Decl) loop
10220 exit when Nkind (Decl) = N_Object_Declaration
10221 and then Is_Return_Object (Defining_Identifier (Decl));
10222 Next (Decl);
10223 end loop;
10224
10225 pragma Assert (Present (Decl));
10226 return Defining_Identifier (Decl);
10227 end Get_Return_Object;
10228
10229 ---------------------------
10230 -- Get_Subprogram_Entity --
10231 ---------------------------
10232
10233 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
10234 Subp : Node_Id;
10235 Subp_Id : Entity_Id;
10236
10237 begin
10238 if Nkind (Nod) = N_Accept_Statement then
10239 Subp := Entry_Direct_Name (Nod);
10240
10241 elsif Nkind (Nod) = N_Slice then
10242 Subp := Prefix (Nod);
10243
10244 else
10245 Subp := Name (Nod);
10246 end if;
10247
10248 -- Strip the subprogram call
10249
10250 loop
10251 if Nkind_In (Subp, N_Explicit_Dereference,
10252 N_Indexed_Component,
10253 N_Selected_Component)
10254 then
10255 Subp := Prefix (Subp);
10256
10257 elsif Nkind_In (Subp, N_Type_Conversion,
10258 N_Unchecked_Type_Conversion)
10259 then
10260 Subp := Expression (Subp);
10261
10262 else
10263 exit;
10264 end if;
10265 end loop;
10266
10267 -- Extract the entity of the subprogram call
10268
10269 if Is_Entity_Name (Subp) then
10270 Subp_Id := Entity (Subp);
10271
10272 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
10273 Subp_Id := Directly_Designated_Type (Subp_Id);
10274 end if;
10275
10276 if Is_Subprogram (Subp_Id) then
10277 return Subp_Id;
10278 else
10279 return Empty;
10280 end if;
10281
10282 -- The search did not find a construct that denotes a subprogram
10283
10284 else
10285 return Empty;
10286 end if;
10287 end Get_Subprogram_Entity;
10288
10289 -----------------------------
10290 -- Get_Task_Body_Procedure --
10291 -----------------------------
10292
10293 function Get_Task_Body_Procedure (E : Entity_Id) return Entity_Id is
10294 begin
10295 -- Note: A task type may be the completion of a private type with
10296 -- discriminants. When performing elaboration checks on a task
10297 -- declaration, the current view of the type may be the private one,
10298 -- and the procedure that holds the body of the task is held in its
10299 -- underlying type.
10300
10301 -- This is an odd function, why not have Task_Body_Procedure do
10302 -- the following digging???
10303
10304 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
10305 end Get_Task_Body_Procedure;
10306
10307 -------------------------
10308 -- Get_User_Defined_Eq --
10309 -------------------------
10310
10311 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
10312 Prim : Elmt_Id;
10313 Op : Entity_Id;
10314
10315 begin
10316 Prim := First_Elmt (Collect_Primitive_Operations (E));
10317 while Present (Prim) loop
10318 Op := Node (Prim);
10319
10320 if Chars (Op) = Name_Op_Eq
10321 and then Etype (Op) = Standard_Boolean
10322 and then Etype (First_Formal (Op)) = E
10323 and then Etype (Next_Formal (First_Formal (Op))) = E
10324 then
10325 return Op;
10326 end if;
10327
10328 Next_Elmt (Prim);
10329 end loop;
10330
10331 return Empty;
10332 end Get_User_Defined_Eq;
10333
10334 ---------------
10335 -- Get_Views --
10336 ---------------
10337
10338 procedure Get_Views
10339 (Typ : Entity_Id;
10340 Priv_Typ : out Entity_Id;
10341 Full_Typ : out Entity_Id;
10342 Full_Base : out Entity_Id;
10343 CRec_Typ : out Entity_Id)
10344 is
10345 IP_View : Entity_Id;
10346
10347 begin
10348 -- Assume that none of the views can be recovered
10349
10350 Priv_Typ := Empty;
10351 Full_Typ := Empty;
10352 Full_Base := Empty;
10353 CRec_Typ := Empty;
10354
10355 -- The input type is the corresponding record type of a protected or a
10356 -- task type.
10357
10358 if Ekind (Typ) = E_Record_Type
10359 and then Is_Concurrent_Record_Type (Typ)
10360 then
10361 CRec_Typ := Typ;
10362 Full_Typ := Corresponding_Concurrent_Type (CRec_Typ);
10363 Full_Base := Base_Type (Full_Typ);
10364 Priv_Typ := Incomplete_Or_Partial_View (Full_Typ);
10365
10366 -- Otherwise the input type denotes an arbitrary type
10367
10368 else
10369 IP_View := Incomplete_Or_Partial_View (Typ);
10370
10371 -- The input type denotes the full view of a private type
10372
10373 if Present (IP_View) then
10374 Priv_Typ := IP_View;
10375 Full_Typ := Typ;
10376
10377 -- The input type is a private type
10378
10379 elsif Is_Private_Type (Typ) then
10380 Priv_Typ := Typ;
10381 Full_Typ := Full_View (Priv_Typ);
10382
10383 -- Otherwise the input type does not have any views
10384
10385 else
10386 Full_Typ := Typ;
10387 end if;
10388
10389 if Present (Full_Typ) then
10390 Full_Base := Base_Type (Full_Typ);
10391
10392 if Ekind_In (Full_Typ, E_Protected_Type, E_Task_Type) then
10393 CRec_Typ := Corresponding_Record_Type (Full_Typ);
10394 end if;
10395 end if;
10396 end if;
10397 end Get_Views;
10398
10399 -----------------------
10400 -- Has_Access_Values --
10401 -----------------------
10402
10403 function Has_Access_Values (T : Entity_Id) return Boolean is
10404 Typ : constant Entity_Id := Underlying_Type (T);
10405
10406 begin
10407 -- Case of a private type which is not completed yet. This can only
10408 -- happen in the case of a generic format type appearing directly, or
10409 -- as a component of the type to which this function is being applied
10410 -- at the top level. Return False in this case, since we certainly do
10411 -- not know that the type contains access types.
10412
10413 if No (Typ) then
10414 return False;
10415
10416 elsif Is_Access_Type (Typ) then
10417 return True;
10418
10419 elsif Is_Array_Type (Typ) then
10420 return Has_Access_Values (Component_Type (Typ));
10421
10422 elsif Is_Record_Type (Typ) then
10423 declare
10424 Comp : Entity_Id;
10425
10426 begin
10427 -- Loop to Check components
10428
10429 Comp := First_Component_Or_Discriminant (Typ);
10430 while Present (Comp) loop
10431
10432 -- Check for access component, tag field does not count, even
10433 -- though it is implemented internally using an access type.
10434
10435 if Has_Access_Values (Etype (Comp))
10436 and then Chars (Comp) /= Name_uTag
10437 then
10438 return True;
10439 end if;
10440
10441 Next_Component_Or_Discriminant (Comp);
10442 end loop;
10443 end;
10444
10445 return False;
10446
10447 else
10448 return False;
10449 end if;
10450 end Has_Access_Values;
10451
10452 ------------------------------
10453 -- Has_Compatible_Alignment --
10454 ------------------------------
10455
10456 function Has_Compatible_Alignment
10457 (Obj : Entity_Id;
10458 Expr : Node_Id;
10459 Layout_Done : Boolean) return Alignment_Result
10460 is
10461 function Has_Compatible_Alignment_Internal
10462 (Obj : Entity_Id;
10463 Expr : Node_Id;
10464 Layout_Done : Boolean;
10465 Default : Alignment_Result) return Alignment_Result;
10466 -- This is the internal recursive function that actually does the work.
10467 -- There is one additional parameter, which says what the result should
10468 -- be if no alignment information is found, and there is no definite
10469 -- indication of compatible alignments. At the outer level, this is set
10470 -- to Unknown, but for internal recursive calls in the case where types
10471 -- are known to be correct, it is set to Known_Compatible.
10472
10473 ---------------------------------------
10474 -- Has_Compatible_Alignment_Internal --
10475 ---------------------------------------
10476
10477 function Has_Compatible_Alignment_Internal
10478 (Obj : Entity_Id;
10479 Expr : Node_Id;
10480 Layout_Done : Boolean;
10481 Default : Alignment_Result) return Alignment_Result
10482 is
10483 Result : Alignment_Result := Known_Compatible;
10484 -- Holds the current status of the result. Note that once a value of
10485 -- Known_Incompatible is set, it is sticky and does not get changed
10486 -- to Unknown (the value in Result only gets worse as we go along,
10487 -- never better).
10488
10489 Offs : Uint := No_Uint;
10490 -- Set to a factor of the offset from the base object when Expr is a
10491 -- selected or indexed component, based on Component_Bit_Offset and
10492 -- Component_Size respectively. A negative value is used to represent
10493 -- a value which is not known at compile time.
10494
10495 procedure Check_Prefix;
10496 -- Checks the prefix recursively in the case where the expression
10497 -- is an indexed or selected component.
10498
10499 procedure Set_Result (R : Alignment_Result);
10500 -- If R represents a worse outcome (unknown instead of known
10501 -- compatible, or known incompatible), then set Result to R.
10502
10503 ------------------
10504 -- Check_Prefix --
10505 ------------------
10506
10507 procedure Check_Prefix is
10508 begin
10509 -- The subtlety here is that in doing a recursive call to check
10510 -- the prefix, we have to decide what to do in the case where we
10511 -- don't find any specific indication of an alignment problem.
10512
10513 -- At the outer level, we normally set Unknown as the result in
10514 -- this case, since we can only set Known_Compatible if we really
10515 -- know that the alignment value is OK, but for the recursive
10516 -- call, in the case where the types match, and we have not
10517 -- specified a peculiar alignment for the object, we are only
10518 -- concerned about suspicious rep clauses, the default case does
10519 -- not affect us, since the compiler will, in the absence of such
10520 -- rep clauses, ensure that the alignment is correct.
10521
10522 if Default = Known_Compatible
10523 or else
10524 (Etype (Obj) = Etype (Expr)
10525 and then (Unknown_Alignment (Obj)
10526 or else
10527 Alignment (Obj) = Alignment (Etype (Obj))))
10528 then
10529 Set_Result
10530 (Has_Compatible_Alignment_Internal
10531 (Obj, Prefix (Expr), Layout_Done, Known_Compatible));
10532
10533 -- In all other cases, we need a full check on the prefix
10534
10535 else
10536 Set_Result
10537 (Has_Compatible_Alignment_Internal
10538 (Obj, Prefix (Expr), Layout_Done, Unknown));
10539 end if;
10540 end Check_Prefix;
10541
10542 ----------------
10543 -- Set_Result --
10544 ----------------
10545
10546 procedure Set_Result (R : Alignment_Result) is
10547 begin
10548 if R > Result then
10549 Result := R;
10550 end if;
10551 end Set_Result;
10552
10553 -- Start of processing for Has_Compatible_Alignment_Internal
10554
10555 begin
10556 -- If Expr is a selected component, we must make sure there is no
10557 -- potentially troublesome component clause and that the record is
10558 -- not packed if the layout is not done.
10559
10560 if Nkind (Expr) = N_Selected_Component then
10561
10562 -- Packing generates unknown alignment if layout is not done
10563
10564 if Is_Packed (Etype (Prefix (Expr))) and then not Layout_Done then
10565 Set_Result (Unknown);
10566 end if;
10567
10568 -- Check prefix and component offset
10569
10570 Check_Prefix;
10571 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
10572
10573 -- If Expr is an indexed component, we must make sure there is no
10574 -- potentially troublesome Component_Size clause and that the array
10575 -- is not bit-packed if the layout is not done.
10576
10577 elsif Nkind (Expr) = N_Indexed_Component then
10578 declare
10579 Typ : constant Entity_Id := Etype (Prefix (Expr));
10580
10581 begin
10582 -- Packing generates unknown alignment if layout is not done
10583
10584 if Is_Bit_Packed_Array (Typ) and then not Layout_Done then
10585 Set_Result (Unknown);
10586 end if;
10587
10588 -- Check prefix and component offset (or at least size)
10589
10590 Check_Prefix;
10591 Offs := Indexed_Component_Bit_Offset (Expr);
10592 if Offs = No_Uint then
10593 Offs := Component_Size (Typ);
10594 end if;
10595 end;
10596 end if;
10597
10598 -- If we have a null offset, the result is entirely determined by
10599 -- the base object and has already been computed recursively.
10600
10601 if Offs = Uint_0 then
10602 null;
10603
10604 -- Case where we know the alignment of the object
10605
10606 elsif Known_Alignment (Obj) then
10607 declare
10608 ObjA : constant Uint := Alignment (Obj);
10609 ExpA : Uint := No_Uint;
10610 SizA : Uint := No_Uint;
10611
10612 begin
10613 -- If alignment of Obj is 1, then we are always OK
10614
10615 if ObjA = 1 then
10616 Set_Result (Known_Compatible);
10617
10618 -- Alignment of Obj is greater than 1, so we need to check
10619
10620 else
10621 -- If we have an offset, see if it is compatible
10622
10623 if Offs /= No_Uint and Offs > Uint_0 then
10624 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
10625 Set_Result (Known_Incompatible);
10626 end if;
10627
10628 -- See if Expr is an object with known alignment
10629
10630 elsif Is_Entity_Name (Expr)
10631 and then Known_Alignment (Entity (Expr))
10632 then
10633 ExpA := Alignment (Entity (Expr));
10634
10635 -- Otherwise, we can use the alignment of the type of
10636 -- Expr given that we already checked for
10637 -- discombobulating rep clauses for the cases of indexed
10638 -- and selected components above.
10639
10640 elsif Known_Alignment (Etype (Expr)) then
10641 ExpA := Alignment (Etype (Expr));
10642
10643 -- Otherwise the alignment is unknown
10644
10645 else
10646 Set_Result (Default);
10647 end if;
10648
10649 -- If we got an alignment, see if it is acceptable
10650
10651 if ExpA /= No_Uint and then ExpA < ObjA then
10652 Set_Result (Known_Incompatible);
10653 end if;
10654
10655 -- If Expr is not a piece of a larger object, see if size
10656 -- is given. If so, check that it is not too small for the
10657 -- required alignment.
10658
10659 if Offs /= No_Uint then
10660 null;
10661
10662 -- See if Expr is an object with known size
10663
10664 elsif Is_Entity_Name (Expr)
10665 and then Known_Static_Esize (Entity (Expr))
10666 then
10667 SizA := Esize (Entity (Expr));
10668
10669 -- Otherwise, we check the object size of the Expr type
10670
10671 elsif Known_Static_Esize (Etype (Expr)) then
10672 SizA := Esize (Etype (Expr));
10673 end if;
10674
10675 -- If we got a size, see if it is a multiple of the Obj
10676 -- alignment, if not, then the alignment cannot be
10677 -- acceptable, since the size is always a multiple of the
10678 -- alignment.
10679
10680 if SizA /= No_Uint then
10681 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
10682 Set_Result (Known_Incompatible);
10683 end if;
10684 end if;
10685 end if;
10686 end;
10687
10688 -- If we do not know required alignment, any non-zero offset is a
10689 -- potential problem (but certainly may be OK, so result is unknown).
10690
10691 elsif Offs /= No_Uint then
10692 Set_Result (Unknown);
10693
10694 -- If we can't find the result by direct comparison of alignment
10695 -- values, then there is still one case that we can determine known
10696 -- result, and that is when we can determine that the types are the
10697 -- same, and no alignments are specified. Then we known that the
10698 -- alignments are compatible, even if we don't know the alignment
10699 -- value in the front end.
10700
10701 elsif Etype (Obj) = Etype (Expr) then
10702
10703 -- Types are the same, but we have to check for possible size
10704 -- and alignments on the Expr object that may make the alignment
10705 -- different, even though the types are the same.
10706
10707 if Is_Entity_Name (Expr) then
10708
10709 -- First check alignment of the Expr object. Any alignment less
10710 -- than Maximum_Alignment is worrisome since this is the case
10711 -- where we do not know the alignment of Obj.
10712
10713 if Known_Alignment (Entity (Expr))
10714 and then UI_To_Int (Alignment (Entity (Expr))) <
10715 Ttypes.Maximum_Alignment
10716 then
10717 Set_Result (Unknown);
10718
10719 -- Now check size of Expr object. Any size that is not an
10720 -- even multiple of Maximum_Alignment is also worrisome
10721 -- since it may cause the alignment of the object to be less
10722 -- than the alignment of the type.
10723
10724 elsif Known_Static_Esize (Entity (Expr))
10725 and then
10726 (UI_To_Int (Esize (Entity (Expr))) mod
10727 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
10728 /= 0
10729 then
10730 Set_Result (Unknown);
10731
10732 -- Otherwise same type is decisive
10733
10734 else
10735 Set_Result (Known_Compatible);
10736 end if;
10737 end if;
10738
10739 -- Another case to deal with is when there is an explicit size or
10740 -- alignment clause when the types are not the same. If so, then the
10741 -- result is Unknown. We don't need to do this test if the Default is
10742 -- Unknown, since that result will be set in any case.
10743
10744 elsif Default /= Unknown
10745 and then (Has_Size_Clause (Etype (Expr))
10746 or else
10747 Has_Alignment_Clause (Etype (Expr)))
10748 then
10749 Set_Result (Unknown);
10750
10751 -- If no indication found, set default
10752
10753 else
10754 Set_Result (Default);
10755 end if;
10756
10757 -- Return worst result found
10758
10759 return Result;
10760 end Has_Compatible_Alignment_Internal;
10761
10762 -- Start of processing for Has_Compatible_Alignment
10763
10764 begin
10765 -- If Obj has no specified alignment, then set alignment from the type
10766 -- alignment. Perhaps we should always do this, but for sure we should
10767 -- do it when there is an address clause since we can do more if the
10768 -- alignment is known.
10769
10770 if Unknown_Alignment (Obj) then
10771 Set_Alignment (Obj, Alignment (Etype (Obj)));
10772 end if;
10773
10774 -- Now do the internal call that does all the work
10775
10776 return
10777 Has_Compatible_Alignment_Internal (Obj, Expr, Layout_Done, Unknown);
10778 end Has_Compatible_Alignment;
10779
10780 ----------------------
10781 -- Has_Declarations --
10782 ----------------------
10783
10784 function Has_Declarations (N : Node_Id) return Boolean is
10785 begin
10786 return Nkind_In (Nkind (N), N_Accept_Statement,
10787 N_Block_Statement,
10788 N_Compilation_Unit_Aux,
10789 N_Entry_Body,
10790 N_Package_Body,
10791 N_Protected_Body,
10792 N_Subprogram_Body,
10793 N_Task_Body,
10794 N_Package_Specification);
10795 end Has_Declarations;
10796
10797 ---------------------------------
10798 -- Has_Defaulted_Discriminants --
10799 ---------------------------------
10800
10801 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10802 begin
10803 return Has_Discriminants (Typ)
10804 and then Present (First_Discriminant (Typ))
10805 and then Present (Discriminant_Default_Value
10806 (First_Discriminant (Typ)));
10807 end Has_Defaulted_Discriminants;
10808
10809 -------------------
10810 -- Has_Denormals --
10811 -------------------
10812
10813 function Has_Denormals (E : Entity_Id) return Boolean is
10814 begin
10815 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
10816 end Has_Denormals;
10817
10818 -------------------------------------------
10819 -- Has_Discriminant_Dependent_Constraint --
10820 -------------------------------------------
10821
10822 function Has_Discriminant_Dependent_Constraint
10823 (Comp : Entity_Id) return Boolean
10824 is
10825 Comp_Decl : constant Node_Id := Parent (Comp);
10826 Subt_Indic : Node_Id;
10827 Constr : Node_Id;
10828 Assn : Node_Id;
10829
10830 begin
10831 -- Discriminants can't depend on discriminants
10832
10833 if Ekind (Comp) = E_Discriminant then
10834 return False;
10835
10836 else
10837 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
10838
10839 if Nkind (Subt_Indic) = N_Subtype_Indication then
10840 Constr := Constraint (Subt_Indic);
10841
10842 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
10843 Assn := First (Constraints (Constr));
10844 while Present (Assn) loop
10845 case Nkind (Assn) is
10846 when N_Identifier
10847 | N_Range
10848 | N_Subtype_Indication
10849 =>
10850 if Depends_On_Discriminant (Assn) then
10851 return True;
10852 end if;
10853
10854 when N_Discriminant_Association =>
10855 if Depends_On_Discriminant (Expression (Assn)) then
10856 return True;
10857 end if;
10858
10859 when others =>
10860 null;
10861 end case;
10862
10863 Next (Assn);
10864 end loop;
10865 end if;
10866 end if;
10867 end if;
10868
10869 return False;
10870 end Has_Discriminant_Dependent_Constraint;
10871
10872 --------------------------------------
10873 -- Has_Effectively_Volatile_Profile --
10874 --------------------------------------
10875
10876 function Has_Effectively_Volatile_Profile
10877 (Subp_Id : Entity_Id) return Boolean
10878 is
10879 Formal : Entity_Id;
10880
10881 begin
10882 -- Inspect the formal parameters looking for an effectively volatile
10883 -- type.
10884
10885 Formal := First_Formal (Subp_Id);
10886 while Present (Formal) loop
10887 if Is_Effectively_Volatile (Etype (Formal)) then
10888 return True;
10889 end if;
10890
10891 Next_Formal (Formal);
10892 end loop;
10893
10894 -- Inspect the return type of functions
10895
10896 if Ekind_In (Subp_Id, E_Function, E_Generic_Function)
10897 and then Is_Effectively_Volatile (Etype (Subp_Id))
10898 then
10899 return True;
10900 end if;
10901
10902 return False;
10903 end Has_Effectively_Volatile_Profile;
10904
10905 --------------------------
10906 -- Has_Enabled_Property --
10907 --------------------------
10908
10909 function Has_Enabled_Property
10910 (Item_Id : Entity_Id;
10911 Property : Name_Id) return Boolean
10912 is
10913 function Protected_Object_Has_Enabled_Property return Boolean;
10914 -- Determine whether a protected object denoted by Item_Id has the
10915 -- property enabled.
10916
10917 function State_Has_Enabled_Property return Boolean;
10918 -- Determine whether a state denoted by Item_Id has the property enabled
10919
10920 function Variable_Has_Enabled_Property return Boolean;
10921 -- Determine whether a variable denoted by Item_Id has the property
10922 -- enabled.
10923
10924 -------------------------------------------
10925 -- Protected_Object_Has_Enabled_Property --
10926 -------------------------------------------
10927
10928 function Protected_Object_Has_Enabled_Property return Boolean is
10929 Constits : constant Elist_Id := Part_Of_Constituents (Item_Id);
10930 Constit_Elmt : Elmt_Id;
10931 Constit_Id : Entity_Id;
10932
10933 begin
10934 -- Protected objects always have the properties Async_Readers and
10935 -- Async_Writers (SPARK RM 7.1.2(16)).
10936
10937 if Property = Name_Async_Readers
10938 or else Property = Name_Async_Writers
10939 then
10940 return True;
10941
10942 -- Protected objects that have Part_Of components also inherit their
10943 -- properties Effective_Reads and Effective_Writes
10944 -- (SPARK RM 7.1.2(16)).
10945
10946 elsif Present (Constits) then
10947 Constit_Elmt := First_Elmt (Constits);
10948 while Present (Constit_Elmt) loop
10949 Constit_Id := Node (Constit_Elmt);
10950
10951 if Has_Enabled_Property (Constit_Id, Property) then
10952 return True;
10953 end if;
10954
10955 Next_Elmt (Constit_Elmt);
10956 end loop;
10957 end if;
10958
10959 return False;
10960 end Protected_Object_Has_Enabled_Property;
10961
10962 --------------------------------
10963 -- State_Has_Enabled_Property --
10964 --------------------------------
10965
10966 function State_Has_Enabled_Property return Boolean is
10967 Decl : constant Node_Id := Parent (Item_Id);
10968
10969 procedure Find_Simple_Properties
10970 (Has_External : out Boolean;
10971 Has_Synchronous : out Boolean);
10972 -- Extract the simple properties associated with declaration Decl
10973
10974 function Is_Enabled_External_Property return Boolean;
10975 -- Determine whether property Property appears within the external
10976 -- property list of declaration Decl, and return its status.
10977
10978 ----------------------------
10979 -- Find_Simple_Properties --
10980 ----------------------------
10981
10982 procedure Find_Simple_Properties
10983 (Has_External : out Boolean;
10984 Has_Synchronous : out Boolean)
10985 is
10986 Opt : Node_Id;
10987
10988 begin
10989 -- Assume that none of the properties are available
10990
10991 Has_External := False;
10992 Has_Synchronous := False;
10993
10994 Opt := First (Expressions (Decl));
10995 while Present (Opt) loop
10996 if Nkind (Opt) = N_Identifier then
10997 if Chars (Opt) = Name_External then
10998 Has_External := True;
10999
11000 elsif Chars (Opt) = Name_Synchronous then
11001 Has_Synchronous := True;
11002 end if;
11003 end if;
11004
11005 Next (Opt);
11006 end loop;
11007 end Find_Simple_Properties;
11008
11009 ----------------------------------
11010 -- Is_Enabled_External_Property --
11011 ----------------------------------
11012
11013 function Is_Enabled_External_Property return Boolean is
11014 Opt : Node_Id;
11015 Opt_Nam : Node_Id;
11016 Prop : Node_Id;
11017 Prop_Nam : Node_Id;
11018 Props : Node_Id;
11019
11020 begin
11021 Opt := First (Component_Associations (Decl));
11022 while Present (Opt) loop
11023 Opt_Nam := First (Choices (Opt));
11024
11025 if Nkind (Opt_Nam) = N_Identifier
11026 and then Chars (Opt_Nam) = Name_External
11027 then
11028 Props := Expression (Opt);
11029
11030 -- Multiple properties appear as an aggregate
11031
11032 if Nkind (Props) = N_Aggregate then
11033
11034 -- Simple property form
11035
11036 Prop := First (Expressions (Props));
11037 while Present (Prop) loop
11038 if Chars (Prop) = Property then
11039 return True;
11040 end if;
11041
11042 Next (Prop);
11043 end loop;
11044
11045 -- Property with expression form
11046
11047 Prop := First (Component_Associations (Props));
11048 while Present (Prop) loop
11049 Prop_Nam := First (Choices (Prop));
11050
11051 -- The property can be represented in two ways:
11052 -- others => <value>
11053 -- <property> => <value>
11054
11055 if Nkind (Prop_Nam) = N_Others_Choice
11056 or else (Nkind (Prop_Nam) = N_Identifier
11057 and then Chars (Prop_Nam) = Property)
11058 then
11059 return Is_True (Expr_Value (Expression (Prop)));
11060 end if;
11061
11062 Next (Prop);
11063 end loop;
11064
11065 -- Single property
11066
11067 else
11068 return Chars (Props) = Property;
11069 end if;
11070 end if;
11071
11072 Next (Opt);
11073 end loop;
11074
11075 return False;
11076 end Is_Enabled_External_Property;
11077
11078 -- Local variables
11079
11080 Has_External : Boolean;
11081 Has_Synchronous : Boolean;
11082
11083 -- Start of processing for State_Has_Enabled_Property
11084
11085 begin
11086 -- The declaration of an external abstract state appears as an
11087 -- extension aggregate. If this is not the case, properties can
11088 -- never be set.
11089
11090 if Nkind (Decl) /= N_Extension_Aggregate then
11091 return False;
11092 end if;
11093
11094 Find_Simple_Properties (Has_External, Has_Synchronous);
11095
11096 -- Simple option External enables all properties (SPARK RM 7.1.2(2))
11097
11098 if Has_External then
11099 return True;
11100
11101 -- Option External may enable or disable specific properties
11102
11103 elsif Is_Enabled_External_Property then
11104 return True;
11105
11106 -- Simple option Synchronous
11107 --
11108 -- enables disables
11109 -- Async_Readers Effective_Reads
11110 -- Async_Writers Effective_Writes
11111 --
11112 -- Note that both forms of External have higher precedence than
11113 -- Synchronous (SPARK RM 7.1.4(9)).
11114
11115 elsif Has_Synchronous then
11116 return Nam_In (Property, Name_Async_Readers, Name_Async_Writers);
11117 end if;
11118
11119 return False;
11120 end State_Has_Enabled_Property;
11121
11122 -----------------------------------
11123 -- Variable_Has_Enabled_Property --
11124 -----------------------------------
11125
11126 function Variable_Has_Enabled_Property return Boolean is
11127 function Is_Enabled (Prag : Node_Id) return Boolean;
11128 -- Determine whether property pragma Prag (if present) denotes an
11129 -- enabled property.
11130
11131 ----------------
11132 -- Is_Enabled --
11133 ----------------
11134
11135 function Is_Enabled (Prag : Node_Id) return Boolean is
11136 Arg1 : Node_Id;
11137
11138 begin
11139 if Present (Prag) then
11140 Arg1 := First (Pragma_Argument_Associations (Prag));
11141
11142 -- The pragma has an optional Boolean expression, the related
11143 -- property is enabled only when the expression evaluates to
11144 -- True.
11145
11146 if Present (Arg1) then
11147 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
11148
11149 -- Otherwise the lack of expression enables the property by
11150 -- default.
11151
11152 else
11153 return True;
11154 end if;
11155
11156 -- The property was never set in the first place
11157
11158 else
11159 return False;
11160 end if;
11161 end Is_Enabled;
11162
11163 -- Local variables
11164
11165 AR : constant Node_Id :=
11166 Get_Pragma (Item_Id, Pragma_Async_Readers);
11167 AW : constant Node_Id :=
11168 Get_Pragma (Item_Id, Pragma_Async_Writers);
11169 ER : constant Node_Id :=
11170 Get_Pragma (Item_Id, Pragma_Effective_Reads);
11171 EW : constant Node_Id :=
11172 Get_Pragma (Item_Id, Pragma_Effective_Writes);
11173
11174 -- Start of processing for Variable_Has_Enabled_Property
11175
11176 begin
11177 -- A non-effectively volatile object can never possess external
11178 -- properties.
11179
11180 if not Is_Effectively_Volatile (Item_Id) then
11181 return False;
11182
11183 -- External properties related to variables come in two flavors -
11184 -- explicit and implicit. The explicit case is characterized by the
11185 -- presence of a property pragma with an optional Boolean flag. The
11186 -- property is enabled when the flag evaluates to True or the flag is
11187 -- missing altogether.
11188
11189 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
11190 return True;
11191
11192 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
11193 return True;
11194
11195 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
11196 return True;
11197
11198 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
11199 return True;
11200
11201 -- The implicit case lacks all property pragmas
11202
11203 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
11204 if Is_Protected_Type (Etype (Item_Id)) then
11205 return Protected_Object_Has_Enabled_Property;
11206 else
11207 return True;
11208 end if;
11209
11210 else
11211 return False;
11212 end if;
11213 end Variable_Has_Enabled_Property;
11214
11215 -- Start of processing for Has_Enabled_Property
11216
11217 begin
11218 -- Abstract states and variables have a flexible scheme of specifying
11219 -- external properties.
11220
11221 if Ekind (Item_Id) = E_Abstract_State then
11222 return State_Has_Enabled_Property;
11223
11224 elsif Ekind (Item_Id) = E_Variable then
11225 return Variable_Has_Enabled_Property;
11226
11227 -- By default, protected objects only have the properties Async_Readers
11228 -- and Async_Writers. If they have Part_Of components, they also inherit
11229 -- their properties Effective_Reads and Effective_Writes
11230 -- (SPARK RM 7.1.2(16)).
11231
11232 elsif Ekind (Item_Id) = E_Protected_Object then
11233 return Protected_Object_Has_Enabled_Property;
11234
11235 -- Otherwise a property is enabled when the related item is effectively
11236 -- volatile.
11237
11238 else
11239 return Is_Effectively_Volatile (Item_Id);
11240 end if;
11241 end Has_Enabled_Property;
11242
11243 -------------------------------------
11244 -- Has_Full_Default_Initialization --
11245 -------------------------------------
11246
11247 function Has_Full_Default_Initialization (Typ : Entity_Id) return Boolean is
11248 Comp : Entity_Id;
11249
11250 begin
11251 -- A type subject to pragma Default_Initial_Condition may be fully
11252 -- default initialized depending on inheritance and the argument of
11253 -- the pragma. Since any type may act as the full view of a private
11254 -- type, this check must be performed prior to the specialized tests
11255 -- below.
11256
11257 if Has_Fully_Default_Initializing_DIC_Pragma (Typ) then
11258 return True;
11259 end if;
11260
11261 -- A scalar type is fully default initialized if it is subject to aspect
11262 -- Default_Value.
11263
11264 if Is_Scalar_Type (Typ) then
11265 return Has_Default_Aspect (Typ);
11266
11267 -- An access type is fully default initialized by default
11268
11269 elsif Is_Access_Type (Typ) then
11270 return True;
11271
11272 -- An array type is fully default initialized if its element type is
11273 -- scalar and the array type carries aspect Default_Component_Value or
11274 -- the element type is fully default initialized.
11275
11276 elsif Is_Array_Type (Typ) then
11277 return
11278 Has_Default_Aspect (Typ)
11279 or else Has_Full_Default_Initialization (Component_Type (Typ));
11280
11281 -- A protected type, record type, or type extension is fully default
11282 -- initialized if all its components either carry an initialization
11283 -- expression or have a type that is fully default initialized. The
11284 -- parent type of a type extension must be fully default initialized.
11285
11286 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
11287
11288 -- Inspect all entities defined in the scope of the type, looking for
11289 -- uninitialized components.
11290
11291 Comp := First_Entity (Typ);
11292 while Present (Comp) loop
11293 if Ekind (Comp) = E_Component
11294 and then Comes_From_Source (Comp)
11295 and then No (Expression (Parent (Comp)))
11296 and then not Has_Full_Default_Initialization (Etype (Comp))
11297 then
11298 return False;
11299 end if;
11300
11301 Next_Entity (Comp);
11302 end loop;
11303
11304 -- Ensure that the parent type of a type extension is fully default
11305 -- initialized.
11306
11307 if Etype (Typ) /= Typ
11308 and then not Has_Full_Default_Initialization (Etype (Typ))
11309 then
11310 return False;
11311 end if;
11312
11313 -- If we get here, then all components and parent portion are fully
11314 -- default initialized.
11315
11316 return True;
11317
11318 -- A task type is fully default initialized by default
11319
11320 elsif Is_Task_Type (Typ) then
11321 return True;
11322
11323 -- Otherwise the type is not fully default initialized
11324
11325 else
11326 return False;
11327 end if;
11328 end Has_Full_Default_Initialization;
11329
11330 -----------------------------------------------
11331 -- Has_Fully_Default_Initializing_DIC_Pragma --
11332 -----------------------------------------------
11333
11334 function Has_Fully_Default_Initializing_DIC_Pragma
11335 (Typ : Entity_Id) return Boolean
11336 is
11337 Args : List_Id;
11338 Prag : Node_Id;
11339
11340 begin
11341 -- A type that inherits pragma Default_Initial_Condition from a parent
11342 -- type is automatically fully default initialized.
11343
11344 if Has_Inherited_DIC (Typ) then
11345 return True;
11346
11347 -- Otherwise the type is fully default initialized only when the pragma
11348 -- appears without an argument, or the argument is non-null.
11349
11350 elsif Has_Own_DIC (Typ) then
11351 Prag := Get_Pragma (Typ, Pragma_Default_Initial_Condition);
11352 pragma Assert (Present (Prag));
11353 Args := Pragma_Argument_Associations (Prag);
11354
11355 -- The pragma appears without an argument in which case it defaults
11356 -- to True.
11357
11358 if No (Args) then
11359 return True;
11360
11361 -- The pragma appears with a non-null expression
11362
11363 elsif Nkind (Get_Pragma_Arg (First (Args))) /= N_Null then
11364 return True;
11365 end if;
11366 end if;
11367
11368 return False;
11369 end Has_Fully_Default_Initializing_DIC_Pragma;
11370
11371 --------------------
11372 -- Has_Infinities --
11373 --------------------
11374
11375 function Has_Infinities (E : Entity_Id) return Boolean is
11376 begin
11377 return
11378 Is_Floating_Point_Type (E)
11379 and then Nkind (Scalar_Range (E)) = N_Range
11380 and then Includes_Infinities (Scalar_Range (E));
11381 end Has_Infinities;
11382
11383 --------------------
11384 -- Has_Interfaces --
11385 --------------------
11386
11387 function Has_Interfaces
11388 (T : Entity_Id;
11389 Use_Full_View : Boolean := True) return Boolean
11390 is
11391 Typ : Entity_Id := Base_Type (T);
11392
11393 begin
11394 -- Handle concurrent types
11395
11396 if Is_Concurrent_Type (Typ) then
11397 Typ := Corresponding_Record_Type (Typ);
11398 end if;
11399
11400 if not Present (Typ)
11401 or else not Is_Record_Type (Typ)
11402 or else not Is_Tagged_Type (Typ)
11403 then
11404 return False;
11405 end if;
11406
11407 -- Handle private types
11408
11409 if Use_Full_View and then Present (Full_View (Typ)) then
11410 Typ := Full_View (Typ);
11411 end if;
11412
11413 -- Handle concurrent record types
11414
11415 if Is_Concurrent_Record_Type (Typ)
11416 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
11417 then
11418 return True;
11419 end if;
11420
11421 loop
11422 if Is_Interface (Typ)
11423 or else
11424 (Is_Record_Type (Typ)
11425 and then Present (Interfaces (Typ))
11426 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
11427 then
11428 return True;
11429 end if;
11430
11431 exit when Etype (Typ) = Typ
11432
11433 -- Handle private types
11434
11435 or else (Present (Full_View (Etype (Typ)))
11436 and then Full_View (Etype (Typ)) = Typ)
11437
11438 -- Protect frontend against wrong sources with cyclic derivations
11439
11440 or else Etype (Typ) = T;
11441
11442 -- Climb to the ancestor type handling private types
11443
11444 if Present (Full_View (Etype (Typ))) then
11445 Typ := Full_View (Etype (Typ));
11446 else
11447 Typ := Etype (Typ);
11448 end if;
11449 end loop;
11450
11451 return False;
11452 end Has_Interfaces;
11453
11454 --------------------------
11455 -- Has_Max_Queue_Length --
11456 --------------------------
11457
11458 function Has_Max_Queue_Length (Id : Entity_Id) return Boolean is
11459 begin
11460 return
11461 Ekind (Id) = E_Entry
11462 and then Present (Get_Pragma (Id, Pragma_Max_Queue_Length));
11463 end Has_Max_Queue_Length;
11464
11465 ---------------------------------
11466 -- Has_No_Obvious_Side_Effects --
11467 ---------------------------------
11468
11469 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
11470 begin
11471 -- For now handle literals, constants, and non-volatile variables and
11472 -- expressions combining these with operators or short circuit forms.
11473
11474 if Nkind (N) in N_Numeric_Or_String_Literal then
11475 return True;
11476
11477 elsif Nkind (N) = N_Character_Literal then
11478 return True;
11479
11480 elsif Nkind (N) in N_Unary_Op then
11481 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
11482
11483 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
11484 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
11485 and then
11486 Has_No_Obvious_Side_Effects (Right_Opnd (N));
11487
11488 elsif Nkind (N) = N_Expression_With_Actions
11489 and then Is_Empty_List (Actions (N))
11490 then
11491 return Has_No_Obvious_Side_Effects (Expression (N));
11492
11493 elsif Nkind (N) in N_Has_Entity then
11494 return Present (Entity (N))
11495 and then Ekind_In (Entity (N), E_Variable,
11496 E_Constant,
11497 E_Enumeration_Literal,
11498 E_In_Parameter,
11499 E_Out_Parameter,
11500 E_In_Out_Parameter)
11501 and then not Is_Volatile (Entity (N));
11502
11503 else
11504 return False;
11505 end if;
11506 end Has_No_Obvious_Side_Effects;
11507
11508 -----------------------------
11509 -- Has_Non_Null_Refinement --
11510 -----------------------------
11511
11512 function Has_Non_Null_Refinement (Id : Entity_Id) return Boolean is
11513 Constits : Elist_Id;
11514
11515 begin
11516 pragma Assert (Ekind (Id) = E_Abstract_State);
11517 Constits := Refinement_Constituents (Id);
11518
11519 -- For a refinement to be non-null, the first constituent must be
11520 -- anything other than null.
11521
11522 return
11523 Present (Constits)
11524 and then Nkind (Node (First_Elmt (Constits))) /= N_Null;
11525 end Has_Non_Null_Refinement;
11526
11527 -----------------------------
11528 -- Has_Non_Null_Statements --
11529 -----------------------------
11530
11531 function Has_Non_Null_Statements (L : List_Id) return Boolean is
11532 Node : Node_Id;
11533
11534 begin
11535 if Is_Non_Empty_List (L) then
11536 Node := First (L);
11537
11538 loop
11539 if Nkind (Node) /= N_Null_Statement then
11540 return True;
11541 end if;
11542
11543 Next (Node);
11544 exit when Node = Empty;
11545 end loop;
11546 end if;
11547
11548 return False;
11549 end Has_Non_Null_Statements;
11550
11551 ----------------------------------
11552 -- Has_Non_Trivial_Precondition --
11553 ----------------------------------
11554
11555 function Has_Non_Trivial_Precondition (Subp : Entity_Id) return Boolean is
11556 Pre : constant Node_Id := Find_Aspect (Subp, Aspect_Pre);
11557
11558 begin
11559 return
11560 Present (Pre)
11561 and then Class_Present (Pre)
11562 and then not Is_Entity_Name (Expression (Pre));
11563 end Has_Non_Trivial_Precondition;
11564
11565 -------------------
11566 -- Has_Null_Body --
11567 -------------------
11568
11569 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
11570 Body_Id : Entity_Id;
11571 Decl : Node_Id;
11572 Spec : Node_Id;
11573 Stmt1 : Node_Id;
11574 Stmt2 : Node_Id;
11575
11576 begin
11577 Spec := Parent (Proc_Id);
11578 Decl := Parent (Spec);
11579
11580 -- Retrieve the entity of the procedure body (e.g. invariant proc).
11581
11582 if Nkind (Spec) = N_Procedure_Specification
11583 and then Nkind (Decl) = N_Subprogram_Declaration
11584 then
11585 Body_Id := Corresponding_Body (Decl);
11586
11587 -- The body acts as a spec
11588
11589 else
11590 Body_Id := Proc_Id;
11591 end if;
11592
11593 -- The body will be generated later
11594
11595 if No (Body_Id) then
11596 return False;
11597 end if;
11598
11599 Spec := Parent (Body_Id);
11600 Decl := Parent (Spec);
11601
11602 pragma Assert
11603 (Nkind (Spec) = N_Procedure_Specification
11604 and then Nkind (Decl) = N_Subprogram_Body);
11605
11606 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
11607
11608 -- Look for a null statement followed by an optional return
11609 -- statement.
11610
11611 if Nkind (Stmt1) = N_Null_Statement then
11612 Stmt2 := Next (Stmt1);
11613
11614 if Present (Stmt2) then
11615 return Nkind (Stmt2) = N_Simple_Return_Statement;
11616 else
11617 return True;
11618 end if;
11619 end if;
11620
11621 return False;
11622 end Has_Null_Body;
11623
11624 ------------------------
11625 -- Has_Null_Exclusion --
11626 ------------------------
11627
11628 function Has_Null_Exclusion (N : Node_Id) return Boolean is
11629 begin
11630 case Nkind (N) is
11631 when N_Access_Definition
11632 | N_Access_Function_Definition
11633 | N_Access_Procedure_Definition
11634 | N_Access_To_Object_Definition
11635 | N_Allocator
11636 | N_Derived_Type_Definition
11637 | N_Function_Specification
11638 | N_Subtype_Declaration
11639 =>
11640 return Null_Exclusion_Present (N);
11641
11642 when N_Component_Definition
11643 | N_Formal_Object_Declaration
11644 | N_Object_Renaming_Declaration
11645 =>
11646 if Present (Subtype_Mark (N)) then
11647 return Null_Exclusion_Present (N);
11648 else pragma Assert (Present (Access_Definition (N)));
11649 return Null_Exclusion_Present (Access_Definition (N));
11650 end if;
11651
11652 when N_Discriminant_Specification =>
11653 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
11654 return Null_Exclusion_Present (Discriminant_Type (N));
11655 else
11656 return Null_Exclusion_Present (N);
11657 end if;
11658
11659 when N_Object_Declaration =>
11660 if Nkind (Object_Definition (N)) = N_Access_Definition then
11661 return Null_Exclusion_Present (Object_Definition (N));
11662 else
11663 return Null_Exclusion_Present (N);
11664 end if;
11665
11666 when N_Parameter_Specification =>
11667 if Nkind (Parameter_Type (N)) = N_Access_Definition then
11668 return Null_Exclusion_Present (Parameter_Type (N));
11669 else
11670 return Null_Exclusion_Present (N);
11671 end if;
11672
11673 when others =>
11674 return False;
11675 end case;
11676 end Has_Null_Exclusion;
11677
11678 ------------------------
11679 -- Has_Null_Extension --
11680 ------------------------
11681
11682 function Has_Null_Extension (T : Entity_Id) return Boolean is
11683 B : constant Entity_Id := Base_Type (T);
11684 Comps : Node_Id;
11685 Ext : Node_Id;
11686
11687 begin
11688 if Nkind (Parent (B)) = N_Full_Type_Declaration
11689 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
11690 then
11691 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
11692
11693 if Present (Ext) then
11694 if Null_Present (Ext) then
11695 return True;
11696 else
11697 Comps := Component_List (Ext);
11698
11699 -- The null component list is rewritten during analysis to
11700 -- include the parent component. Any other component indicates
11701 -- that the extension was not originally null.
11702
11703 return Null_Present (Comps)
11704 or else No (Next (First (Component_Items (Comps))));
11705 end if;
11706 else
11707 return False;
11708 end if;
11709
11710 else
11711 return False;
11712 end if;
11713 end Has_Null_Extension;
11714
11715 -------------------------
11716 -- Has_Null_Refinement --
11717 -------------------------
11718
11719 function Has_Null_Refinement (Id : Entity_Id) return Boolean is
11720 Constits : Elist_Id;
11721
11722 begin
11723 pragma Assert (Ekind (Id) = E_Abstract_State);
11724 Constits := Refinement_Constituents (Id);
11725
11726 -- For a refinement to be null, the state's sole constituent must be a
11727 -- null.
11728
11729 return
11730 Present (Constits)
11731 and then Nkind (Node (First_Elmt (Constits))) = N_Null;
11732 end Has_Null_Refinement;
11733
11734 -------------------------------
11735 -- Has_Overriding_Initialize --
11736 -------------------------------
11737
11738 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
11739 BT : constant Entity_Id := Base_Type (T);
11740 P : Elmt_Id;
11741
11742 begin
11743 if Is_Controlled (BT) then
11744 if Is_RTU (Scope (BT), Ada_Finalization) then
11745 return False;
11746
11747 elsif Present (Primitive_Operations (BT)) then
11748 P := First_Elmt (Primitive_Operations (BT));
11749 while Present (P) loop
11750 declare
11751 Init : constant Entity_Id := Node (P);
11752 Formal : constant Entity_Id := First_Formal (Init);
11753 begin
11754 if Ekind (Init) = E_Procedure
11755 and then Chars (Init) = Name_Initialize
11756 and then Comes_From_Source (Init)
11757 and then Present (Formal)
11758 and then Etype (Formal) = BT
11759 and then No (Next_Formal (Formal))
11760 and then (Ada_Version < Ada_2012
11761 or else not Null_Present (Parent (Init)))
11762 then
11763 return True;
11764 end if;
11765 end;
11766
11767 Next_Elmt (P);
11768 end loop;
11769 end if;
11770
11771 -- Here if type itself does not have a non-null Initialize operation:
11772 -- check immediate ancestor.
11773
11774 if Is_Derived_Type (BT)
11775 and then Has_Overriding_Initialize (Etype (BT))
11776 then
11777 return True;
11778 end if;
11779 end if;
11780
11781 return False;
11782 end Has_Overriding_Initialize;
11783
11784 --------------------------------------
11785 -- Has_Preelaborable_Initialization --
11786 --------------------------------------
11787
11788 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
11789 Has_PE : Boolean;
11790
11791 procedure Check_Components (E : Entity_Id);
11792 -- Check component/discriminant chain, sets Has_PE False if a component
11793 -- or discriminant does not meet the preelaborable initialization rules.
11794
11795 ----------------------
11796 -- Check_Components --
11797 ----------------------
11798
11799 procedure Check_Components (E : Entity_Id) is
11800 Ent : Entity_Id;
11801 Exp : Node_Id;
11802
11803 begin
11804 -- Loop through entities of record or protected type
11805
11806 Ent := E;
11807 while Present (Ent) loop
11808
11809 -- We are interested only in components and discriminants
11810
11811 Exp := Empty;
11812
11813 case Ekind (Ent) is
11814 when E_Component =>
11815
11816 -- Get default expression if any. If there is no declaration
11817 -- node, it means we have an internal entity. The parent and
11818 -- tag fields are examples of such entities. For such cases,
11819 -- we just test the type of the entity.
11820
11821 if Present (Declaration_Node (Ent)) then
11822 Exp := Expression (Declaration_Node (Ent));
11823 end if;
11824
11825 when E_Discriminant =>
11826
11827 -- Note: for a renamed discriminant, the Declaration_Node
11828 -- may point to the one from the ancestor, and have a
11829 -- different expression, so use the proper attribute to
11830 -- retrieve the expression from the derived constraint.
11831
11832 Exp := Discriminant_Default_Value (Ent);
11833
11834 when others =>
11835 goto Check_Next_Entity;
11836 end case;
11837
11838 -- A component has PI if it has no default expression and the
11839 -- component type has PI.
11840
11841 if No (Exp) then
11842 if not Has_Preelaborable_Initialization (Etype (Ent)) then
11843 Has_PE := False;
11844 exit;
11845 end if;
11846
11847 -- Require the default expression to be preelaborable
11848
11849 elsif not Is_Preelaborable_Construct (Exp) then
11850 Has_PE := False;
11851 exit;
11852 end if;
11853
11854 <<Check_Next_Entity>>
11855 Next_Entity (Ent);
11856 end loop;
11857 end Check_Components;
11858
11859 -- Start of processing for Has_Preelaborable_Initialization
11860
11861 begin
11862 -- Immediate return if already marked as known preelaborable init. This
11863 -- covers types for which this function has already been called once
11864 -- and returned True (in which case the result is cached), and also
11865 -- types to which a pragma Preelaborable_Initialization applies.
11866
11867 if Known_To_Have_Preelab_Init (E) then
11868 return True;
11869 end if;
11870
11871 -- If the type is a subtype representing a generic actual type, then
11872 -- test whether its base type has preelaborable initialization since
11873 -- the subtype representing the actual does not inherit this attribute
11874 -- from the actual or formal. (but maybe it should???)
11875
11876 if Is_Generic_Actual_Type (E) then
11877 return Has_Preelaborable_Initialization (Base_Type (E));
11878 end if;
11879
11880 -- All elementary types have preelaborable initialization
11881
11882 if Is_Elementary_Type (E) then
11883 Has_PE := True;
11884
11885 -- Array types have PI if the component type has PI
11886
11887 elsif Is_Array_Type (E) then
11888 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
11889
11890 -- A derived type has preelaborable initialization if its parent type
11891 -- has preelaborable initialization and (in the case of a derived record
11892 -- extension) if the non-inherited components all have preelaborable
11893 -- initialization. However, a user-defined controlled type with an
11894 -- overriding Initialize procedure does not have preelaborable
11895 -- initialization.
11896
11897 elsif Is_Derived_Type (E) then
11898
11899 -- If the derived type is a private extension then it doesn't have
11900 -- preelaborable initialization.
11901
11902 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
11903 return False;
11904 end if;
11905
11906 -- First check whether ancestor type has preelaborable initialization
11907
11908 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
11909
11910 -- If OK, check extension components (if any)
11911
11912 if Has_PE and then Is_Record_Type (E) then
11913 Check_Components (First_Entity (E));
11914 end if;
11915
11916 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
11917 -- with a user defined Initialize procedure does not have PI. If
11918 -- the type is untagged, the control primitives come from a component
11919 -- that has already been checked.
11920
11921 if Has_PE
11922 and then Is_Controlled (E)
11923 and then Is_Tagged_Type (E)
11924 and then Has_Overriding_Initialize (E)
11925 then
11926 Has_PE := False;
11927 end if;
11928
11929 -- Private types not derived from a type having preelaborable init and
11930 -- that are not marked with pragma Preelaborable_Initialization do not
11931 -- have preelaborable initialization.
11932
11933 elsif Is_Private_Type (E) then
11934 return False;
11935
11936 -- Record type has PI if it is non private and all components have PI
11937
11938 elsif Is_Record_Type (E) then
11939 Has_PE := True;
11940 Check_Components (First_Entity (E));
11941
11942 -- Protected types must not have entries, and components must meet
11943 -- same set of rules as for record components.
11944
11945 elsif Is_Protected_Type (E) then
11946 if Has_Entries (E) then
11947 Has_PE := False;
11948 else
11949 Has_PE := True;
11950 Check_Components (First_Entity (E));
11951 Check_Components (First_Private_Entity (E));
11952 end if;
11953
11954 -- Type System.Address always has preelaborable initialization
11955
11956 elsif Is_RTE (E, RE_Address) then
11957 Has_PE := True;
11958
11959 -- In all other cases, type does not have preelaborable initialization
11960
11961 else
11962 return False;
11963 end if;
11964
11965 -- If type has preelaborable initialization, cache result
11966
11967 if Has_PE then
11968 Set_Known_To_Have_Preelab_Init (E);
11969 end if;
11970
11971 return Has_PE;
11972 end Has_Preelaborable_Initialization;
11973
11974 ----------------
11975 -- Has_Prefix --
11976 ----------------
11977
11978 function Has_Prefix (N : Node_Id) return Boolean is
11979 begin
11980 return
11981 Nkind_In (N, N_Attribute_Reference,
11982 N_Expanded_Name,
11983 N_Explicit_Dereference,
11984 N_Indexed_Component,
11985 N_Reference,
11986 N_Selected_Component,
11987 N_Slice);
11988 end Has_Prefix;
11989
11990 ---------------------------
11991 -- Has_Private_Component --
11992 ---------------------------
11993
11994 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
11995 Btype : Entity_Id := Base_Type (Type_Id);
11996 Component : Entity_Id;
11997
11998 begin
11999 if Error_Posted (Type_Id)
12000 or else Error_Posted (Btype)
12001 then
12002 return False;
12003 end if;
12004
12005 if Is_Class_Wide_Type (Btype) then
12006 Btype := Root_Type (Btype);
12007 end if;
12008
12009 if Is_Private_Type (Btype) then
12010 declare
12011 UT : constant Entity_Id := Underlying_Type (Btype);
12012 begin
12013 if No (UT) then
12014 if No (Full_View (Btype)) then
12015 return not Is_Generic_Type (Btype)
12016 and then
12017 not Is_Generic_Type (Root_Type (Btype));
12018 else
12019 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
12020 end if;
12021 else
12022 return not Is_Frozen (UT) and then Has_Private_Component (UT);
12023 end if;
12024 end;
12025
12026 elsif Is_Array_Type (Btype) then
12027 return Has_Private_Component (Component_Type (Btype));
12028
12029 elsif Is_Record_Type (Btype) then
12030 Component := First_Component (Btype);
12031 while Present (Component) loop
12032 if Has_Private_Component (Etype (Component)) then
12033 return True;
12034 end if;
12035
12036 Next_Component (Component);
12037 end loop;
12038
12039 return False;
12040
12041 elsif Is_Protected_Type (Btype)
12042 and then Present (Corresponding_Record_Type (Btype))
12043 then
12044 return Has_Private_Component (Corresponding_Record_Type (Btype));
12045
12046 else
12047 return False;
12048 end if;
12049 end Has_Private_Component;
12050
12051 ----------------------
12052 -- Has_Signed_Zeros --
12053 ----------------------
12054
12055 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
12056 begin
12057 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
12058 end Has_Signed_Zeros;
12059
12060 ------------------------------
12061 -- Has_Significant_Contract --
12062 ------------------------------
12063
12064 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
12065 Subp_Nam : constant Name_Id := Chars (Subp_Id);
12066
12067 begin
12068 -- _Finalizer procedure
12069
12070 if Subp_Nam = Name_uFinalizer then
12071 return False;
12072
12073 -- _Postconditions procedure
12074
12075 elsif Subp_Nam = Name_uPostconditions then
12076 return False;
12077
12078 -- Predicate function
12079
12080 elsif Ekind (Subp_Id) = E_Function
12081 and then Is_Predicate_Function (Subp_Id)
12082 then
12083 return False;
12084
12085 -- TSS subprogram
12086
12087 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
12088 return False;
12089
12090 else
12091 return True;
12092 end if;
12093 end Has_Significant_Contract;
12094
12095 -----------------------------
12096 -- Has_Static_Array_Bounds --
12097 -----------------------------
12098
12099 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
12100 All_Static : Boolean;
12101 Dummy : Boolean;
12102
12103 begin
12104 Examine_Array_Bounds (Typ, All_Static, Dummy);
12105
12106 return All_Static;
12107 end Has_Static_Array_Bounds;
12108
12109 ---------------------------------------
12110 -- Has_Static_Non_Empty_Array_Bounds --
12111 ---------------------------------------
12112
12113 function Has_Static_Non_Empty_Array_Bounds (Typ : Node_Id) return Boolean is
12114 All_Static : Boolean;
12115 Has_Empty : Boolean;
12116
12117 begin
12118 Examine_Array_Bounds (Typ, All_Static, Has_Empty);
12119
12120 return All_Static and not Has_Empty;
12121 end Has_Static_Non_Empty_Array_Bounds;
12122
12123 ----------------
12124 -- Has_Stream --
12125 ----------------
12126
12127 function Has_Stream (T : Entity_Id) return Boolean is
12128 E : Entity_Id;
12129
12130 begin
12131 if No (T) then
12132 return False;
12133
12134 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
12135 return True;
12136
12137 elsif Is_Array_Type (T) then
12138 return Has_Stream (Component_Type (T));
12139
12140 elsif Is_Record_Type (T) then
12141 E := First_Component (T);
12142 while Present (E) loop
12143 if Has_Stream (Etype (E)) then
12144 return True;
12145 else
12146 Next_Component (E);
12147 end if;
12148 end loop;
12149
12150 return False;
12151
12152 elsif Is_Private_Type (T) then
12153 return Has_Stream (Underlying_Type (T));
12154
12155 else
12156 return False;
12157 end if;
12158 end Has_Stream;
12159
12160 ----------------
12161 -- Has_Suffix --
12162 ----------------
12163
12164 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
12165 begin
12166 Get_Name_String (Chars (E));
12167 return Name_Buffer (Name_Len) = Suffix;
12168 end Has_Suffix;
12169
12170 ----------------
12171 -- Add_Suffix --
12172 ----------------
12173
12174 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
12175 begin
12176 Get_Name_String (Chars (E));
12177 Add_Char_To_Name_Buffer (Suffix);
12178 return Name_Find;
12179 end Add_Suffix;
12180
12181 -------------------
12182 -- Remove_Suffix --
12183 -------------------
12184
12185 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
12186 begin
12187 pragma Assert (Has_Suffix (E, Suffix));
12188 Get_Name_String (Chars (E));
12189 Name_Len := Name_Len - 1;
12190 return Name_Find;
12191 end Remove_Suffix;
12192
12193 ----------------------------------
12194 -- Replace_Null_By_Null_Address --
12195 ----------------------------------
12196
12197 procedure Replace_Null_By_Null_Address (N : Node_Id) is
12198 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id);
12199 -- Replace operand Op with a reference to Null_Address when the operand
12200 -- denotes a null Address. Other_Op denotes the other operand.
12201
12202 --------------------------
12203 -- Replace_Null_Operand --
12204 --------------------------
12205
12206 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id) is
12207 begin
12208 -- Check the type of the complementary operand since the N_Null node
12209 -- has not been decorated yet.
12210
12211 if Nkind (Op) = N_Null
12212 and then Is_Descendant_Of_Address (Etype (Other_Op))
12213 then
12214 Rewrite (Op, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (Op)));
12215 end if;
12216 end Replace_Null_Operand;
12217
12218 -- Start of processing for Replace_Null_By_Null_Address
12219
12220 begin
12221 pragma Assert (Relaxed_RM_Semantics);
12222 pragma Assert (Nkind_In (N, N_Null,
12223 N_Op_Eq,
12224 N_Op_Ge,
12225 N_Op_Gt,
12226 N_Op_Le,
12227 N_Op_Lt,
12228 N_Op_Ne));
12229
12230 if Nkind (N) = N_Null then
12231 Rewrite (N, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
12232
12233 else
12234 declare
12235 L : constant Node_Id := Left_Opnd (N);
12236 R : constant Node_Id := Right_Opnd (N);
12237
12238 begin
12239 Replace_Null_Operand (L, Other_Op => R);
12240 Replace_Null_Operand (R, Other_Op => L);
12241 end;
12242 end if;
12243 end Replace_Null_By_Null_Address;
12244
12245 --------------------------
12246 -- Has_Tagged_Component --
12247 --------------------------
12248
12249 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
12250 Comp : Entity_Id;
12251
12252 begin
12253 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
12254 return Has_Tagged_Component (Underlying_Type (Typ));
12255
12256 elsif Is_Array_Type (Typ) then
12257 return Has_Tagged_Component (Component_Type (Typ));
12258
12259 elsif Is_Tagged_Type (Typ) then
12260 return True;
12261
12262 elsif Is_Record_Type (Typ) then
12263 Comp := First_Component (Typ);
12264 while Present (Comp) loop
12265 if Has_Tagged_Component (Etype (Comp)) then
12266 return True;
12267 end if;
12268
12269 Next_Component (Comp);
12270 end loop;
12271
12272 return False;
12273
12274 else
12275 return False;
12276 end if;
12277 end Has_Tagged_Component;
12278
12279 -----------------------------
12280 -- Has_Undefined_Reference --
12281 -----------------------------
12282
12283 function Has_Undefined_Reference (Expr : Node_Id) return Boolean is
12284 Has_Undef_Ref : Boolean := False;
12285 -- Flag set when expression Expr contains at least one undefined
12286 -- reference.
12287
12288 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result;
12289 -- Determine whether N denotes a reference and if it does, whether it is
12290 -- undefined.
12291
12292 ----------------------------
12293 -- Is_Undefined_Reference --
12294 ----------------------------
12295
12296 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result is
12297 begin
12298 if Is_Entity_Name (N)
12299 and then Present (Entity (N))
12300 and then Entity (N) = Any_Id
12301 then
12302 Has_Undef_Ref := True;
12303 return Abandon;
12304 end if;
12305
12306 return OK;
12307 end Is_Undefined_Reference;
12308
12309 procedure Find_Undefined_References is
12310 new Traverse_Proc (Is_Undefined_Reference);
12311
12312 -- Start of processing for Has_Undefined_Reference
12313
12314 begin
12315 Find_Undefined_References (Expr);
12316
12317 return Has_Undef_Ref;
12318 end Has_Undefined_Reference;
12319
12320 ----------------------------
12321 -- Has_Volatile_Component --
12322 ----------------------------
12323
12324 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
12325 Comp : Entity_Id;
12326
12327 begin
12328 if Has_Volatile_Components (Typ) then
12329 return True;
12330
12331 elsif Is_Array_Type (Typ) then
12332 return Is_Volatile (Component_Type (Typ));
12333
12334 elsif Is_Record_Type (Typ) then
12335 Comp := First_Component (Typ);
12336 while Present (Comp) loop
12337 if Is_Volatile_Object (Comp) then
12338 return True;
12339 end if;
12340
12341 Next_Component (Comp);
12342 end loop;
12343 end if;
12344
12345 return False;
12346 end Has_Volatile_Component;
12347
12348 -------------------------
12349 -- Implementation_Kind --
12350 -------------------------
12351
12352 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
12353 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
12354 Arg : Node_Id;
12355 begin
12356 pragma Assert (Present (Impl_Prag));
12357 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
12358 return Chars (Get_Pragma_Arg (Arg));
12359 end Implementation_Kind;
12360
12361 --------------------------
12362 -- Implements_Interface --
12363 --------------------------
12364
12365 function Implements_Interface
12366 (Typ_Ent : Entity_Id;
12367 Iface_Ent : Entity_Id;
12368 Exclude_Parents : Boolean := False) return Boolean
12369 is
12370 Ifaces_List : Elist_Id;
12371 Elmt : Elmt_Id;
12372 Iface : Entity_Id := Base_Type (Iface_Ent);
12373 Typ : Entity_Id := Base_Type (Typ_Ent);
12374
12375 begin
12376 if Is_Class_Wide_Type (Typ) then
12377 Typ := Root_Type (Typ);
12378 end if;
12379
12380 if not Has_Interfaces (Typ) then
12381 return False;
12382 end if;
12383
12384 if Is_Class_Wide_Type (Iface) then
12385 Iface := Root_Type (Iface);
12386 end if;
12387
12388 Collect_Interfaces (Typ, Ifaces_List);
12389
12390 Elmt := First_Elmt (Ifaces_List);
12391 while Present (Elmt) loop
12392 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
12393 and then Exclude_Parents
12394 then
12395 null;
12396
12397 elsif Node (Elmt) = Iface then
12398 return True;
12399 end if;
12400
12401 Next_Elmt (Elmt);
12402 end loop;
12403
12404 return False;
12405 end Implements_Interface;
12406
12407 ------------------------------------
12408 -- In_Assertion_Expression_Pragma --
12409 ------------------------------------
12410
12411 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
12412 Par : Node_Id;
12413 Prag : Node_Id := Empty;
12414
12415 begin
12416 -- Climb the parent chain looking for an enclosing pragma
12417
12418 Par := N;
12419 while Present (Par) loop
12420 if Nkind (Par) = N_Pragma then
12421 Prag := Par;
12422 exit;
12423
12424 -- Precondition-like pragmas are expanded into if statements, check
12425 -- the original node instead.
12426
12427 elsif Nkind (Original_Node (Par)) = N_Pragma then
12428 Prag := Original_Node (Par);
12429 exit;
12430
12431 -- The expansion of attribute 'Old generates a constant to capture
12432 -- the result of the prefix. If the parent traversal reaches
12433 -- one of these constants, then the node technically came from a
12434 -- postcondition-like pragma. Note that the Ekind is not tested here
12435 -- because N may be the expression of an object declaration which is
12436 -- currently being analyzed. Such objects carry Ekind of E_Void.
12437
12438 elsif Nkind (Par) = N_Object_Declaration
12439 and then Constant_Present (Par)
12440 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
12441 then
12442 return True;
12443
12444 -- Prevent the search from going too far
12445
12446 elsif Is_Body_Or_Package_Declaration (Par) then
12447 return False;
12448 end if;
12449
12450 Par := Parent (Par);
12451 end loop;
12452
12453 return
12454 Present (Prag)
12455 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
12456 end In_Assertion_Expression_Pragma;
12457
12458 ----------------------
12459 -- In_Generic_Scope --
12460 ----------------------
12461
12462 function In_Generic_Scope (E : Entity_Id) return Boolean is
12463 S : Entity_Id;
12464
12465 begin
12466 S := Scope (E);
12467 while Present (S) and then S /= Standard_Standard loop
12468 if Is_Generic_Unit (S) then
12469 return True;
12470 end if;
12471
12472 S := Scope (S);
12473 end loop;
12474
12475 return False;
12476 end In_Generic_Scope;
12477
12478 -----------------
12479 -- In_Instance --
12480 -----------------
12481
12482 function In_Instance return Boolean is
12483 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
12484 S : Entity_Id;
12485
12486 begin
12487 S := Current_Scope;
12488 while Present (S) and then S /= Standard_Standard loop
12489 if Is_Generic_Instance (S) then
12490
12491 -- A child instance is always compiled in the context of a parent
12492 -- instance. Nevertheless, its actuals must not be analyzed in an
12493 -- instance context. We detect this case by examining the current
12494 -- compilation unit, which must be a child instance, and checking
12495 -- that it has not been analyzed yet.
12496
12497 if Is_Child_Unit (Curr_Unit)
12498 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
12499 N_Package_Instantiation
12500 and then Ekind (Curr_Unit) = E_Void
12501 then
12502 return False;
12503 else
12504 return True;
12505 end if;
12506 end if;
12507
12508 S := Scope (S);
12509 end loop;
12510
12511 return False;
12512 end In_Instance;
12513
12514 ----------------------
12515 -- In_Instance_Body --
12516 ----------------------
12517
12518 function In_Instance_Body return Boolean is
12519 S : Entity_Id;
12520
12521 begin
12522 S := Current_Scope;
12523 while Present (S) and then S /= Standard_Standard loop
12524 if Ekind_In (S, E_Function, E_Procedure)
12525 and then Is_Generic_Instance (S)
12526 then
12527 return True;
12528
12529 elsif Ekind (S) = E_Package
12530 and then In_Package_Body (S)
12531 and then Is_Generic_Instance (S)
12532 then
12533 return True;
12534 end if;
12535
12536 S := Scope (S);
12537 end loop;
12538
12539 return False;
12540 end In_Instance_Body;
12541
12542 -----------------------------
12543 -- In_Instance_Not_Visible --
12544 -----------------------------
12545
12546 function In_Instance_Not_Visible return Boolean is
12547 S : Entity_Id;
12548
12549 begin
12550 S := Current_Scope;
12551 while Present (S) and then S /= Standard_Standard loop
12552 if Ekind_In (S, E_Function, E_Procedure)
12553 and then Is_Generic_Instance (S)
12554 then
12555 return True;
12556
12557 elsif Ekind (S) = E_Package
12558 and then (In_Package_Body (S) or else In_Private_Part (S))
12559 and then Is_Generic_Instance (S)
12560 then
12561 return True;
12562 end if;
12563
12564 S := Scope (S);
12565 end loop;
12566
12567 return False;
12568 end In_Instance_Not_Visible;
12569
12570 ------------------------------
12571 -- In_Instance_Visible_Part --
12572 ------------------------------
12573
12574 function In_Instance_Visible_Part
12575 (Id : Entity_Id := Current_Scope) return Boolean
12576 is
12577 Inst : Entity_Id;
12578
12579 begin
12580 Inst := Id;
12581 while Present (Inst) and then Inst /= Standard_Standard loop
12582 if Ekind (Inst) = E_Package
12583 and then Is_Generic_Instance (Inst)
12584 and then not In_Package_Body (Inst)
12585 and then not In_Private_Part (Inst)
12586 then
12587 return True;
12588 end if;
12589
12590 Inst := Scope (Inst);
12591 end loop;
12592
12593 return False;
12594 end In_Instance_Visible_Part;
12595
12596 ---------------------
12597 -- In_Package_Body --
12598 ---------------------
12599
12600 function In_Package_Body return Boolean is
12601 S : Entity_Id;
12602
12603 begin
12604 S := Current_Scope;
12605 while Present (S) and then S /= Standard_Standard loop
12606 if Ekind (S) = E_Package and then In_Package_Body (S) then
12607 return True;
12608 else
12609 S := Scope (S);
12610 end if;
12611 end loop;
12612
12613 return False;
12614 end In_Package_Body;
12615
12616 --------------------------
12617 -- In_Pragma_Expression --
12618 --------------------------
12619
12620 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
12621 P : Node_Id;
12622 begin
12623 P := Parent (N);
12624 loop
12625 if No (P) then
12626 return False;
12627 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
12628 return True;
12629 else
12630 P := Parent (P);
12631 end if;
12632 end loop;
12633 end In_Pragma_Expression;
12634
12635 ---------------------------
12636 -- In_Pre_Post_Condition --
12637 ---------------------------
12638
12639 function In_Pre_Post_Condition (N : Node_Id) return Boolean is
12640 Par : Node_Id;
12641 Prag : Node_Id := Empty;
12642 Prag_Id : Pragma_Id;
12643
12644 begin
12645 -- Climb the parent chain looking for an enclosing pragma
12646
12647 Par := N;
12648 while Present (Par) loop
12649 if Nkind (Par) = N_Pragma then
12650 Prag := Par;
12651 exit;
12652
12653 -- Prevent the search from going too far
12654
12655 elsif Is_Body_Or_Package_Declaration (Par) then
12656 exit;
12657 end if;
12658
12659 Par := Parent (Par);
12660 end loop;
12661
12662 if Present (Prag) then
12663 Prag_Id := Get_Pragma_Id (Prag);
12664
12665 return
12666 Prag_Id = Pragma_Post
12667 or else Prag_Id = Pragma_Post_Class
12668 or else Prag_Id = Pragma_Postcondition
12669 or else Prag_Id = Pragma_Pre
12670 or else Prag_Id = Pragma_Pre_Class
12671 or else Prag_Id = Pragma_Precondition;
12672
12673 -- Otherwise the node is not enclosed by a pre/postcondition pragma
12674
12675 else
12676 return False;
12677 end if;
12678 end In_Pre_Post_Condition;
12679
12680 ------------------------------
12681 -- In_Quantified_Expression --
12682 ------------------------------
12683
12684 function In_Quantified_Expression (N : Node_Id) return Boolean is
12685 P : Node_Id;
12686 begin
12687 P := Parent (N);
12688 loop
12689 if No (P) then
12690 return False;
12691 elsif Nkind (P) = N_Quantified_Expression then
12692 return True;
12693 else
12694 P := Parent (P);
12695 end if;
12696 end loop;
12697 end In_Quantified_Expression;
12698
12699 -------------------------------------
12700 -- In_Reverse_Storage_Order_Object --
12701 -------------------------------------
12702
12703 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
12704 Pref : Node_Id;
12705 Btyp : Entity_Id := Empty;
12706
12707 begin
12708 -- Climb up indexed components
12709
12710 Pref := N;
12711 loop
12712 case Nkind (Pref) is
12713 when N_Selected_Component =>
12714 Pref := Prefix (Pref);
12715 exit;
12716
12717 when N_Indexed_Component =>
12718 Pref := Prefix (Pref);
12719
12720 when others =>
12721 Pref := Empty;
12722 exit;
12723 end case;
12724 end loop;
12725
12726 if Present (Pref) then
12727 Btyp := Base_Type (Etype (Pref));
12728 end if;
12729
12730 return Present (Btyp)
12731 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
12732 and then Reverse_Storage_Order (Btyp);
12733 end In_Reverse_Storage_Order_Object;
12734
12735 ------------------------------
12736 -- In_Same_Declarative_Part --
12737 ------------------------------
12738
12739 function In_Same_Declarative_Part
12740 (Context : Node_Id;
12741 N : Node_Id) return Boolean
12742 is
12743 Cont : Node_Id := Context;
12744 Nod : Node_Id;
12745
12746 begin
12747 if Nkind (Cont) = N_Compilation_Unit_Aux then
12748 Cont := Parent (Cont);
12749 end if;
12750
12751 Nod := Parent (N);
12752 while Present (Nod) loop
12753 if Nod = Cont then
12754 return True;
12755
12756 elsif Nkind_In (Nod, N_Accept_Statement,
12757 N_Block_Statement,
12758 N_Compilation_Unit,
12759 N_Entry_Body,
12760 N_Package_Body,
12761 N_Package_Declaration,
12762 N_Protected_Body,
12763 N_Subprogram_Body,
12764 N_Task_Body)
12765 then
12766 return False;
12767
12768 elsif Nkind (Nod) = N_Subunit then
12769 Nod := Corresponding_Stub (Nod);
12770
12771 else
12772 Nod := Parent (Nod);
12773 end if;
12774 end loop;
12775
12776 return False;
12777 end In_Same_Declarative_Part;
12778
12779 --------------------------------------
12780 -- In_Subprogram_Or_Concurrent_Unit --
12781 --------------------------------------
12782
12783 function In_Subprogram_Or_Concurrent_Unit return Boolean is
12784 E : Entity_Id;
12785 K : Entity_Kind;
12786
12787 begin
12788 -- Use scope chain to check successively outer scopes
12789
12790 E := Current_Scope;
12791 loop
12792 K := Ekind (E);
12793
12794 if K in Subprogram_Kind
12795 or else K in Concurrent_Kind
12796 or else K in Generic_Subprogram_Kind
12797 then
12798 return True;
12799
12800 elsif E = Standard_Standard then
12801 return False;
12802 end if;
12803
12804 E := Scope (E);
12805 end loop;
12806 end In_Subprogram_Or_Concurrent_Unit;
12807
12808 ----------------
12809 -- In_Subtree --
12810 ----------------
12811
12812 function In_Subtree (N : Node_Id; Root : Node_Id) return Boolean is
12813 Curr : Node_Id;
12814
12815 begin
12816 Curr := N;
12817 while Present (Curr) loop
12818 if Curr = Root then
12819 return True;
12820 end if;
12821
12822 Curr := Parent (Curr);
12823 end loop;
12824
12825 return False;
12826 end In_Subtree;
12827
12828 ----------------
12829 -- In_Subtree --
12830 ----------------
12831
12832 function In_Subtree
12833 (N : Node_Id;
12834 Root1 : Node_Id;
12835 Root2 : Node_Id) return Boolean
12836 is
12837 Curr : Node_Id;
12838
12839 begin
12840 Curr := N;
12841 while Present (Curr) loop
12842 if Curr = Root1 or else Curr = Root2 then
12843 return True;
12844 end if;
12845
12846 Curr := Parent (Curr);
12847 end loop;
12848
12849 return False;
12850 end In_Subtree;
12851
12852 ---------------------
12853 -- In_Visible_Part --
12854 ---------------------
12855
12856 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
12857 begin
12858 return Is_Package_Or_Generic_Package (Scope_Id)
12859 and then In_Open_Scopes (Scope_Id)
12860 and then not In_Package_Body (Scope_Id)
12861 and then not In_Private_Part (Scope_Id);
12862 end In_Visible_Part;
12863
12864 -----------------------------
12865 -- In_While_Loop_Condition --
12866 -----------------------------
12867
12868 function In_While_Loop_Condition (N : Node_Id) return Boolean is
12869 Prev : Node_Id := N;
12870 P : Node_Id := Parent (N);
12871 -- P and Prev will be used for traversing the AST, while maintaining an
12872 -- invariant that P = Parent (Prev).
12873 begin
12874 loop
12875 if No (P) then
12876 return False;
12877 elsif Nkind (P) = N_Iteration_Scheme
12878 and then Prev = Condition (P)
12879 then
12880 return True;
12881 else
12882 Prev := P;
12883 P := Parent (P);
12884 end if;
12885 end loop;
12886 end In_While_Loop_Condition;
12887
12888 --------------------------------
12889 -- Incomplete_Or_Partial_View --
12890 --------------------------------
12891
12892 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
12893 function Inspect_Decls
12894 (Decls : List_Id;
12895 Taft : Boolean := False) return Entity_Id;
12896 -- Check whether a declarative region contains the incomplete or partial
12897 -- view of Id.
12898
12899 -------------------
12900 -- Inspect_Decls --
12901 -------------------
12902
12903 function Inspect_Decls
12904 (Decls : List_Id;
12905 Taft : Boolean := False) return Entity_Id
12906 is
12907 Decl : Node_Id;
12908 Match : Node_Id;
12909
12910 begin
12911 Decl := First (Decls);
12912 while Present (Decl) loop
12913 Match := Empty;
12914
12915 -- The partial view of a Taft-amendment type is an incomplete
12916 -- type.
12917
12918 if Taft then
12919 if Nkind (Decl) = N_Incomplete_Type_Declaration then
12920 Match := Defining_Identifier (Decl);
12921 end if;
12922
12923 -- Otherwise look for a private type whose full view matches the
12924 -- input type. Note that this checks full_type_declaration nodes
12925 -- to account for derivations from a private type where the type
12926 -- declaration hold the partial view and the full view is an
12927 -- itype.
12928
12929 elsif Nkind_In (Decl, N_Full_Type_Declaration,
12930 N_Private_Extension_Declaration,
12931 N_Private_Type_Declaration)
12932 then
12933 Match := Defining_Identifier (Decl);
12934 end if;
12935
12936 -- Guard against unanalyzed entities
12937
12938 if Present (Match)
12939 and then Is_Type (Match)
12940 and then Present (Full_View (Match))
12941 and then Full_View (Match) = Id
12942 then
12943 return Match;
12944 end if;
12945
12946 Next (Decl);
12947 end loop;
12948
12949 return Empty;
12950 end Inspect_Decls;
12951
12952 -- Local variables
12953
12954 Prev : Entity_Id;
12955
12956 -- Start of processing for Incomplete_Or_Partial_View
12957
12958 begin
12959 -- Deferred constant or incomplete type case
12960
12961 Prev := Current_Entity_In_Scope (Id);
12962
12963 if Present (Prev)
12964 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
12965 and then Present (Full_View (Prev))
12966 and then Full_View (Prev) = Id
12967 then
12968 return Prev;
12969 end if;
12970
12971 -- Private or Taft amendment type case
12972
12973 declare
12974 Pkg : constant Entity_Id := Scope (Id);
12975 Pkg_Decl : Node_Id := Pkg;
12976
12977 begin
12978 if Present (Pkg)
12979 and then Is_Package_Or_Generic_Package (Pkg)
12980 then
12981 while Nkind (Pkg_Decl) /= N_Package_Specification loop
12982 Pkg_Decl := Parent (Pkg_Decl);
12983 end loop;
12984
12985 -- It is knows that Typ has a private view, look for it in the
12986 -- visible declarations of the enclosing scope. A special case
12987 -- of this is when the two views have been exchanged - the full
12988 -- appears earlier than the private.
12989
12990 if Has_Private_Declaration (Id) then
12991 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
12992
12993 -- Exchanged view case, look in the private declarations
12994
12995 if No (Prev) then
12996 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
12997 end if;
12998
12999 return Prev;
13000
13001 -- Otherwise if this is the package body, then Typ is a potential
13002 -- Taft amendment type. The incomplete view should be located in
13003 -- the private declarations of the enclosing scope.
13004
13005 elsif In_Package_Body (Pkg) then
13006 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
13007 end if;
13008 end if;
13009 end;
13010
13011 -- The type has no incomplete or private view
13012
13013 return Empty;
13014 end Incomplete_Or_Partial_View;
13015
13016 ---------------------------------------
13017 -- Incomplete_View_From_Limited_With --
13018 ---------------------------------------
13019
13020 function Incomplete_View_From_Limited_With
13021 (Typ : Entity_Id) return Entity_Id
13022 is
13023 begin
13024 -- It might make sense to make this an attribute in Einfo, and set it
13025 -- in Sem_Ch10 in Build_Shadow_Entity. However, we're running short on
13026 -- slots for new attributes, and it seems a bit simpler to just search
13027 -- the Limited_View (if it exists) for an incomplete type whose
13028 -- Non_Limited_View is Typ.
13029
13030 if Ekind (Scope (Typ)) = E_Package
13031 and then Present (Limited_View (Scope (Typ)))
13032 then
13033 declare
13034 Ent : Entity_Id := First_Entity (Limited_View (Scope (Typ)));
13035 begin
13036 while Present (Ent) loop
13037 if Ekind (Ent) in Incomplete_Kind
13038 and then Non_Limited_View (Ent) = Typ
13039 then
13040 return Ent;
13041 end if;
13042
13043 Next_Entity (Ent);
13044 end loop;
13045 end;
13046 end if;
13047
13048 return Typ;
13049 end Incomplete_View_From_Limited_With;
13050
13051 ----------------------------------
13052 -- Indexed_Component_Bit_Offset --
13053 ----------------------------------
13054
13055 function Indexed_Component_Bit_Offset (N : Node_Id) return Uint is
13056 Exp : constant Node_Id := First (Expressions (N));
13057 Typ : constant Entity_Id := Etype (Prefix (N));
13058 Off : constant Uint := Component_Size (Typ);
13059 Ind : Node_Id;
13060
13061 begin
13062 -- Return early if the component size is not known or variable
13063
13064 if Off = No_Uint or else Off < Uint_0 then
13065 return No_Uint;
13066 end if;
13067
13068 -- Deal with the degenerate case of an empty component
13069
13070 if Off = Uint_0 then
13071 return Off;
13072 end if;
13073
13074 -- Check that both the index value and the low bound are known
13075
13076 if not Compile_Time_Known_Value (Exp) then
13077 return No_Uint;
13078 end if;
13079
13080 Ind := First_Index (Typ);
13081 if No (Ind) then
13082 return No_Uint;
13083 end if;
13084
13085 if Nkind (Ind) = N_Subtype_Indication then
13086 Ind := Constraint (Ind);
13087
13088 if Nkind (Ind) = N_Range_Constraint then
13089 Ind := Range_Expression (Ind);
13090 end if;
13091 end if;
13092
13093 if Nkind (Ind) /= N_Range
13094 or else not Compile_Time_Known_Value (Low_Bound (Ind))
13095 then
13096 return No_Uint;
13097 end if;
13098
13099 -- Return the scaled offset
13100
13101 return Off * (Expr_Value (Exp) - Expr_Value (Low_Bound ((Ind))));
13102 end Indexed_Component_Bit_Offset;
13103
13104 ----------------------------
13105 -- Inherit_Rep_Item_Chain --
13106 ----------------------------
13107
13108 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
13109 Item : Node_Id;
13110 Next_Item : Node_Id;
13111
13112 begin
13113 -- There are several inheritance scenarios to consider depending on
13114 -- whether both types have rep item chains and whether the destination
13115 -- type already inherits part of the source type's rep item chain.
13116
13117 -- 1) The source type lacks a rep item chain
13118 -- From_Typ ---> Empty
13119 --
13120 -- Typ --------> Item (or Empty)
13121
13122 -- In this case inheritance cannot take place because there are no items
13123 -- to inherit.
13124
13125 -- 2) The destination type lacks a rep item chain
13126 -- From_Typ ---> Item ---> ...
13127 --
13128 -- Typ --------> Empty
13129
13130 -- Inheritance takes place by setting the First_Rep_Item of the
13131 -- destination type to the First_Rep_Item of the source type.
13132 -- From_Typ ---> Item ---> ...
13133 -- ^
13134 -- Typ -----------+
13135
13136 -- 3.1) Both source and destination types have at least one rep item.
13137 -- The destination type does NOT inherit a rep item from the source
13138 -- type.
13139 -- From_Typ ---> Item ---> Item
13140 --
13141 -- Typ --------> Item ---> Item
13142
13143 -- Inheritance takes place by setting the Next_Rep_Item of the last item
13144 -- of the destination type to the First_Rep_Item of the source type.
13145 -- From_Typ -------------------> Item ---> Item
13146 -- ^
13147 -- Typ --------> Item ---> Item --+
13148
13149 -- 3.2) Both source and destination types have at least one rep item.
13150 -- The destination type DOES inherit part of the rep item chain of the
13151 -- source type.
13152 -- From_Typ ---> Item ---> Item ---> Item
13153 -- ^
13154 -- Typ --------> Item ------+
13155
13156 -- This rare case arises when the full view of a private extension must
13157 -- inherit the rep item chain from the full view of its parent type and
13158 -- the full view of the parent type contains extra rep items. Currently
13159 -- only invariants may lead to such form of inheritance.
13160
13161 -- type From_Typ is tagged private
13162 -- with Type_Invariant'Class => Item_2;
13163
13164 -- type Typ is new From_Typ with private
13165 -- with Type_Invariant => Item_4;
13166
13167 -- At this point the rep item chains contain the following items
13168
13169 -- From_Typ -----------> Item_2 ---> Item_3
13170 -- ^
13171 -- Typ --------> Item_4 --+
13172
13173 -- The full views of both types may introduce extra invariants
13174
13175 -- type From_Typ is tagged null record
13176 -- with Type_Invariant => Item_1;
13177
13178 -- type Typ is new From_Typ with null record;
13179
13180 -- The full view of Typ would have to inherit any new rep items added to
13181 -- the full view of From_Typ.
13182
13183 -- From_Typ -----------> Item_1 ---> Item_2 ---> Item_3
13184 -- ^
13185 -- Typ --------> Item_4 --+
13186
13187 -- To achieve this form of inheritance, the destination type must first
13188 -- sever the link between its own rep chain and that of the source type,
13189 -- then inheritance 3.1 takes place.
13190
13191 -- Case 1: The source type lacks a rep item chain
13192
13193 if No (First_Rep_Item (From_Typ)) then
13194 return;
13195
13196 -- Case 2: The destination type lacks a rep item chain
13197
13198 elsif No (First_Rep_Item (Typ)) then
13199 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
13200
13201 -- Case 3: Both the source and destination types have at least one rep
13202 -- item. Traverse the rep item chain of the destination type to find the
13203 -- last rep item.
13204
13205 else
13206 Item := Empty;
13207 Next_Item := First_Rep_Item (Typ);
13208 while Present (Next_Item) loop
13209
13210 -- Detect a link between the destination type's rep chain and that
13211 -- of the source type. There are two possibilities:
13212
13213 -- Variant 1
13214 -- Next_Item
13215 -- V
13216 -- From_Typ ---> Item_1 --->
13217 -- ^
13218 -- Typ -----------+
13219 --
13220 -- Item is Empty
13221
13222 -- Variant 2
13223 -- Next_Item
13224 -- V
13225 -- From_Typ ---> Item_1 ---> Item_2 --->
13226 -- ^
13227 -- Typ --------> Item_3 ------+
13228 -- ^
13229 -- Item
13230
13231 if Has_Rep_Item (From_Typ, Next_Item) then
13232 exit;
13233 end if;
13234
13235 Item := Next_Item;
13236 Next_Item := Next_Rep_Item (Next_Item);
13237 end loop;
13238
13239 -- Inherit the source type's rep item chain
13240
13241 if Present (Item) then
13242 Set_Next_Rep_Item (Item, First_Rep_Item (From_Typ));
13243 else
13244 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
13245 end if;
13246 end if;
13247 end Inherit_Rep_Item_Chain;
13248
13249 ------------------------------------
13250 -- Inherits_From_Tagged_Full_View --
13251 ------------------------------------
13252
13253 function Inherits_From_Tagged_Full_View (Typ : Entity_Id) return Boolean is
13254 begin
13255 return Is_Private_Type (Typ)
13256 and then Present (Full_View (Typ))
13257 and then Is_Private_Type (Full_View (Typ))
13258 and then not Is_Tagged_Type (Full_View (Typ))
13259 and then Present (Underlying_Type (Full_View (Typ)))
13260 and then Is_Tagged_Type (Underlying_Type (Full_View (Typ)));
13261 end Inherits_From_Tagged_Full_View;
13262
13263 ---------------------------------
13264 -- Insert_Explicit_Dereference --
13265 ---------------------------------
13266
13267 procedure Insert_Explicit_Dereference (N : Node_Id) is
13268 New_Prefix : constant Node_Id := Relocate_Node (N);
13269 Ent : Entity_Id := Empty;
13270 Pref : Node_Id := Empty;
13271 I : Interp_Index;
13272 It : Interp;
13273 T : Entity_Id;
13274
13275 begin
13276 Save_Interps (N, New_Prefix);
13277
13278 Rewrite (N,
13279 Make_Explicit_Dereference (Sloc (Parent (N)),
13280 Prefix => New_Prefix));
13281
13282 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
13283
13284 if Is_Overloaded (New_Prefix) then
13285
13286 -- The dereference is also overloaded, and its interpretations are
13287 -- the designated types of the interpretations of the original node.
13288
13289 Set_Etype (N, Any_Type);
13290
13291 Get_First_Interp (New_Prefix, I, It);
13292 while Present (It.Nam) loop
13293 T := It.Typ;
13294
13295 if Is_Access_Type (T) then
13296 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
13297 end if;
13298
13299 Get_Next_Interp (I, It);
13300 end loop;
13301
13302 End_Interp_List;
13303
13304 else
13305 -- Prefix is unambiguous: mark the original prefix (which might
13306 -- Come_From_Source) as a reference, since the new (relocated) one
13307 -- won't be taken into account.
13308
13309 if Is_Entity_Name (New_Prefix) then
13310 Ent := Entity (New_Prefix);
13311 Pref := New_Prefix;
13312
13313 -- For a retrieval of a subcomponent of some composite object,
13314 -- retrieve the ultimate entity if there is one.
13315
13316 elsif Nkind_In (New_Prefix, N_Selected_Component,
13317 N_Indexed_Component)
13318 then
13319 Pref := Prefix (New_Prefix);
13320 while Present (Pref)
13321 and then Nkind_In (Pref, N_Selected_Component,
13322 N_Indexed_Component)
13323 loop
13324 Pref := Prefix (Pref);
13325 end loop;
13326
13327 if Present (Pref) and then Is_Entity_Name (Pref) then
13328 Ent := Entity (Pref);
13329 end if;
13330 end if;
13331
13332 -- Place the reference on the entity node
13333
13334 if Present (Ent) then
13335 Generate_Reference (Ent, Pref);
13336 end if;
13337 end if;
13338 end Insert_Explicit_Dereference;
13339
13340 ------------------------------------------
13341 -- Inspect_Deferred_Constant_Completion --
13342 ------------------------------------------
13343
13344 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
13345 Decl : Node_Id;
13346
13347 begin
13348 Decl := First (Decls);
13349 while Present (Decl) loop
13350
13351 -- Deferred constant signature
13352
13353 if Nkind (Decl) = N_Object_Declaration
13354 and then Constant_Present (Decl)
13355 and then No (Expression (Decl))
13356
13357 -- No need to check internally generated constants
13358
13359 and then Comes_From_Source (Decl)
13360
13361 -- The constant is not completed. A full object declaration or a
13362 -- pragma Import complete a deferred constant.
13363
13364 and then not Has_Completion (Defining_Identifier (Decl))
13365 then
13366 Error_Msg_N
13367 ("constant declaration requires initialization expression",
13368 Defining_Identifier (Decl));
13369 end if;
13370
13371 Decl := Next (Decl);
13372 end loop;
13373 end Inspect_Deferred_Constant_Completion;
13374
13375 -------------------------------
13376 -- Install_Elaboration_Model --
13377 -------------------------------
13378
13379 procedure Install_Elaboration_Model (Unit_Id : Entity_Id) is
13380 function Find_Elaboration_Checks_Pragma (L : List_Id) return Node_Id;
13381 -- Try to find pragma Elaboration_Checks in arbitrary list L. Return
13382 -- Empty if there is no such pragma.
13383
13384 ------------------------------------
13385 -- Find_Elaboration_Checks_Pragma --
13386 ------------------------------------
13387
13388 function Find_Elaboration_Checks_Pragma (L : List_Id) return Node_Id is
13389 Item : Node_Id;
13390
13391 begin
13392 Item := First (L);
13393 while Present (Item) loop
13394 if Nkind (Item) = N_Pragma
13395 and then Pragma_Name (Item) = Name_Elaboration_Checks
13396 then
13397 return Item;
13398 end if;
13399
13400 Next (Item);
13401 end loop;
13402
13403 return Empty;
13404 end Find_Elaboration_Checks_Pragma;
13405
13406 -- Local variables
13407
13408 Args : List_Id;
13409 Model : Node_Id;
13410 Prag : Node_Id;
13411 Unit : Node_Id;
13412
13413 -- Start of processing for Install_Elaboration_Model
13414
13415 begin
13416 -- Nothing to do when the unit does not exist
13417
13418 if No (Unit_Id) then
13419 return;
13420 end if;
13421
13422 Unit := Parent (Unit_Declaration_Node (Unit_Id));
13423
13424 -- Nothing to do when the unit is not a library unit
13425
13426 if Nkind (Unit) /= N_Compilation_Unit then
13427 return;
13428 end if;
13429
13430 Prag := Find_Elaboration_Checks_Pragma (Context_Items (Unit));
13431
13432 -- The compilation unit is subject to pragma Elaboration_Checks. Set the
13433 -- elaboration model as specified by the pragma.
13434
13435 if Present (Prag) then
13436 Args := Pragma_Argument_Associations (Prag);
13437
13438 -- Guard against an illegal pragma. The sole argument must be an
13439 -- identifier which specifies either Dynamic or Static model.
13440
13441 if Present (Args) then
13442 Model := Get_Pragma_Arg (First (Args));
13443
13444 if Nkind (Model) = N_Identifier then
13445 Dynamic_Elaboration_Checks := Chars (Model) = Name_Dynamic;
13446 end if;
13447 end if;
13448 end if;
13449 end Install_Elaboration_Model;
13450
13451 -----------------------------
13452 -- Install_Generic_Formals --
13453 -----------------------------
13454
13455 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
13456 E : Entity_Id;
13457
13458 begin
13459 pragma Assert (Is_Generic_Subprogram (Subp_Id));
13460
13461 E := First_Entity (Subp_Id);
13462 while Present (E) loop
13463 Install_Entity (E);
13464 Next_Entity (E);
13465 end loop;
13466 end Install_Generic_Formals;
13467
13468 ------------------------
13469 -- Install_SPARK_Mode --
13470 ------------------------
13471
13472 procedure Install_SPARK_Mode (Mode : SPARK_Mode_Type; Prag : Node_Id) is
13473 begin
13474 SPARK_Mode := Mode;
13475 SPARK_Mode_Pragma := Prag;
13476 end Install_SPARK_Mode;
13477
13478 --------------------------
13479 -- Invalid_Scalar_Value --
13480 --------------------------
13481
13482 function Invalid_Scalar_Value
13483 (Loc : Source_Ptr;
13484 Scal_Typ : Scalar_Id) return Node_Id
13485 is
13486 function Invalid_Binder_Value return Node_Id;
13487 -- Return a reference to the corresponding invalid value for type
13488 -- Scal_Typ as defined in unit System.Scalar_Values.
13489
13490 function Invalid_Float_Value return Node_Id;
13491 -- Return the invalid value of float type Scal_Typ
13492
13493 function Invalid_Integer_Value return Node_Id;
13494 -- Return the invalid value of integer type Scal_Typ
13495
13496 procedure Set_Invalid_Binder_Values;
13497 -- Set the contents of collection Invalid_Binder_Values
13498
13499 --------------------------
13500 -- Invalid_Binder_Value --
13501 --------------------------
13502
13503 function Invalid_Binder_Value return Node_Id is
13504 Val_Id : Entity_Id;
13505
13506 begin
13507 -- Initialize the collection of invalid binder values the first time
13508 -- around.
13509
13510 Set_Invalid_Binder_Values;
13511
13512 -- Obtain the corresponding variable from System.Scalar_Values which
13513 -- holds the invalid value for this type.
13514
13515 Val_Id := Invalid_Binder_Values (Scal_Typ);
13516 pragma Assert (Present (Val_Id));
13517
13518 return New_Occurrence_Of (Val_Id, Loc);
13519 end Invalid_Binder_Value;
13520
13521 -------------------------
13522 -- Invalid_Float_Value --
13523 -------------------------
13524
13525 function Invalid_Float_Value return Node_Id is
13526 Value : constant Ureal := Invalid_Floats (Scal_Typ);
13527
13528 begin
13529 -- Pragma Invalid_Scalars did not specify an invalid value for this
13530 -- type. Fall back to the value provided by the binder.
13531
13532 if Value = No_Ureal then
13533 return Invalid_Binder_Value;
13534 else
13535 return Make_Real_Literal (Loc, Realval => Value);
13536 end if;
13537 end Invalid_Float_Value;
13538
13539 ---------------------------
13540 -- Invalid_Integer_Value --
13541 ---------------------------
13542
13543 function Invalid_Integer_Value return Node_Id is
13544 Value : constant Uint := Invalid_Integers (Scal_Typ);
13545
13546 begin
13547 -- Pragma Invalid_Scalars did not specify an invalid value for this
13548 -- type. Fall back to the value provided by the binder.
13549
13550 if Value = No_Uint then
13551 return Invalid_Binder_Value;
13552 else
13553 return Make_Integer_Literal (Loc, Intval => Value);
13554 end if;
13555 end Invalid_Integer_Value;
13556
13557 -------------------------------
13558 -- Set_Invalid_Binder_Values --
13559 -------------------------------
13560
13561 procedure Set_Invalid_Binder_Values is
13562 begin
13563 if not Invalid_Binder_Values_Set then
13564 Invalid_Binder_Values_Set := True;
13565
13566 -- Initialize the contents of the collection once since RTE calls
13567 -- are not cheap.
13568
13569 Invalid_Binder_Values :=
13570 (Name_Short_Float => RTE (RE_IS_Isf),
13571 Name_Float => RTE (RE_IS_Ifl),
13572 Name_Long_Float => RTE (RE_IS_Ilf),
13573 Name_Long_Long_Float => RTE (RE_IS_Ill),
13574 Name_Signed_8 => RTE (RE_IS_Is1),
13575 Name_Signed_16 => RTE (RE_IS_Is2),
13576 Name_Signed_32 => RTE (RE_IS_Is4),
13577 Name_Signed_64 => RTE (RE_IS_Is8),
13578 Name_Unsigned_8 => RTE (RE_IS_Iu1),
13579 Name_Unsigned_16 => RTE (RE_IS_Iu2),
13580 Name_Unsigned_32 => RTE (RE_IS_Iu4),
13581 Name_Unsigned_64 => RTE (RE_IS_Iu8));
13582 end if;
13583 end Set_Invalid_Binder_Values;
13584
13585 -- Start of processing for Invalid_Scalar_Value
13586
13587 begin
13588 if Scal_Typ in Float_Scalar_Id then
13589 return Invalid_Float_Value;
13590
13591 else pragma Assert (Scal_Typ in Integer_Scalar_Id);
13592 return Invalid_Integer_Value;
13593 end if;
13594 end Invalid_Scalar_Value;
13595
13596 -----------------------------
13597 -- Is_Actual_Out_Parameter --
13598 -----------------------------
13599
13600 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
13601 Formal : Entity_Id;
13602 Call : Node_Id;
13603 begin
13604 Find_Actual (N, Formal, Call);
13605 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
13606 end Is_Actual_Out_Parameter;
13607
13608 -------------------------
13609 -- Is_Actual_Parameter --
13610 -------------------------
13611
13612 function Is_Actual_Parameter (N : Node_Id) return Boolean is
13613 PK : constant Node_Kind := Nkind (Parent (N));
13614
13615 begin
13616 case PK is
13617 when N_Parameter_Association =>
13618 return N = Explicit_Actual_Parameter (Parent (N));
13619
13620 when N_Subprogram_Call =>
13621 return Is_List_Member (N)
13622 and then
13623 List_Containing (N) = Parameter_Associations (Parent (N));
13624
13625 when others =>
13626 return False;
13627 end case;
13628 end Is_Actual_Parameter;
13629
13630 --------------------------------
13631 -- Is_Actual_Tagged_Parameter --
13632 --------------------------------
13633
13634 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
13635 Formal : Entity_Id;
13636 Call : Node_Id;
13637 begin
13638 Find_Actual (N, Formal, Call);
13639 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
13640 end Is_Actual_Tagged_Parameter;
13641
13642 ---------------------
13643 -- Is_Aliased_View --
13644 ---------------------
13645
13646 function Is_Aliased_View (Obj : Node_Id) return Boolean is
13647 E : Entity_Id;
13648
13649 begin
13650 if Is_Entity_Name (Obj) then
13651 E := Entity (Obj);
13652
13653 return
13654 (Is_Object (E)
13655 and then
13656 (Is_Aliased (E)
13657 or else (Present (Renamed_Object (E))
13658 and then Is_Aliased_View (Renamed_Object (E)))))
13659
13660 or else ((Is_Formal (E) or else Is_Formal_Object (E))
13661 and then Is_Tagged_Type (Etype (E)))
13662
13663 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
13664
13665 -- Current instance of type, either directly or as rewritten
13666 -- reference to the current object.
13667
13668 or else (Is_Entity_Name (Original_Node (Obj))
13669 and then Present (Entity (Original_Node (Obj)))
13670 and then Is_Type (Entity (Original_Node (Obj))))
13671
13672 or else (Is_Type (E) and then E = Current_Scope)
13673
13674 or else (Is_Incomplete_Or_Private_Type (E)
13675 and then Full_View (E) = Current_Scope)
13676
13677 -- Ada 2012 AI05-0053: the return object of an extended return
13678 -- statement is aliased if its type is immutably limited.
13679
13680 or else (Is_Return_Object (E)
13681 and then Is_Limited_View (Etype (E)));
13682
13683 elsif Nkind (Obj) = N_Selected_Component then
13684 return Is_Aliased (Entity (Selector_Name (Obj)));
13685
13686 elsif Nkind (Obj) = N_Indexed_Component then
13687 return Has_Aliased_Components (Etype (Prefix (Obj)))
13688 or else
13689 (Is_Access_Type (Etype (Prefix (Obj)))
13690 and then Has_Aliased_Components
13691 (Designated_Type (Etype (Prefix (Obj)))));
13692
13693 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
13694 return Is_Tagged_Type (Etype (Obj))
13695 and then Is_Aliased_View (Expression (Obj));
13696
13697 elsif Nkind (Obj) = N_Explicit_Dereference then
13698 return Nkind (Original_Node (Obj)) /= N_Function_Call;
13699
13700 else
13701 return False;
13702 end if;
13703 end Is_Aliased_View;
13704
13705 -------------------------
13706 -- Is_Ancestor_Package --
13707 -------------------------
13708
13709 function Is_Ancestor_Package
13710 (E1 : Entity_Id;
13711 E2 : Entity_Id) return Boolean
13712 is
13713 Par : Entity_Id;
13714
13715 begin
13716 Par := E2;
13717 while Present (Par) and then Par /= Standard_Standard loop
13718 if Par = E1 then
13719 return True;
13720 end if;
13721
13722 Par := Scope (Par);
13723 end loop;
13724
13725 return False;
13726 end Is_Ancestor_Package;
13727
13728 ----------------------
13729 -- Is_Atomic_Object --
13730 ----------------------
13731
13732 function Is_Atomic_Object (N : Node_Id) return Boolean is
13733 function Prefix_Has_Atomic_Components (P : Node_Id) return Boolean;
13734 -- Determine whether prefix P has atomic components. This requires the
13735 -- presence of an Atomic_Components aspect/pragma.
13736
13737 ---------------------------------
13738 -- Prefix_Has_Atomic_Components --
13739 ---------------------------------
13740
13741 function Prefix_Has_Atomic_Components (P : Node_Id) return Boolean is
13742 Typ : constant Entity_Id := Etype (P);
13743
13744 begin
13745 if Is_Access_Type (Typ) then
13746 return Has_Atomic_Components (Designated_Type (Typ));
13747
13748 elsif Has_Atomic_Components (Typ) then
13749 return True;
13750
13751 elsif Is_Entity_Name (P)
13752 and then Has_Atomic_Components (Entity (P))
13753 then
13754 return True;
13755
13756 else
13757 return False;
13758 end if;
13759 end Prefix_Has_Atomic_Components;
13760
13761 -- Start of processing for Is_Atomic_Object
13762
13763 begin
13764 if Is_Entity_Name (N) then
13765 return Is_Atomic_Object_Entity (Entity (N));
13766
13767 elsif Is_Atomic (Etype (N)) then
13768 return True;
13769
13770 elsif Nkind (N) = N_Indexed_Component then
13771 return Prefix_Has_Atomic_Components (Prefix (N));
13772
13773 elsif Nkind (N) = N_Selected_Component then
13774 return Is_Atomic (Entity (Selector_Name (N)));
13775
13776 else
13777 return False;
13778 end if;
13779 end Is_Atomic_Object;
13780
13781 -----------------------------
13782 -- Is_Atomic_Object_Entity --
13783 -----------------------------
13784
13785 function Is_Atomic_Object_Entity (Id : Entity_Id) return Boolean is
13786 begin
13787 return
13788 Is_Object (Id)
13789 and then (Is_Atomic (Id) or else Is_Atomic (Etype (Id)));
13790 end Is_Atomic_Object_Entity;
13791
13792 -----------------------------
13793 -- Is_Atomic_Or_VFA_Object --
13794 -----------------------------
13795
13796 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
13797 begin
13798 return Is_Atomic_Object (N) or else Is_Volatile_Full_Access_Object (N);
13799 end Is_Atomic_Or_VFA_Object;
13800
13801 ----------------------
13802 -- Is_Attribute_Old --
13803 ----------------------
13804
13805 function Is_Attribute_Old (N : Node_Id) return Boolean is
13806 begin
13807 return Nkind (N) = N_Attribute_Reference
13808 and then Attribute_Name (N) = Name_Old;
13809 end Is_Attribute_Old;
13810
13811 -------------------------
13812 -- Is_Attribute_Result --
13813 -------------------------
13814
13815 function Is_Attribute_Result (N : Node_Id) return Boolean is
13816 begin
13817 return Nkind (N) = N_Attribute_Reference
13818 and then Attribute_Name (N) = Name_Result;
13819 end Is_Attribute_Result;
13820
13821 -------------------------
13822 -- Is_Attribute_Update --
13823 -------------------------
13824
13825 function Is_Attribute_Update (N : Node_Id) return Boolean is
13826 begin
13827 return Nkind (N) = N_Attribute_Reference
13828 and then Attribute_Name (N) = Name_Update;
13829 end Is_Attribute_Update;
13830
13831 ------------------------------------
13832 -- Is_Body_Or_Package_Declaration --
13833 ------------------------------------
13834
13835 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
13836 begin
13837 return Is_Body (N) or else Nkind (N) = N_Package_Declaration;
13838 end Is_Body_Or_Package_Declaration;
13839
13840 -----------------------
13841 -- Is_Bounded_String --
13842 -----------------------
13843
13844 function Is_Bounded_String (T : Entity_Id) return Boolean is
13845 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
13846
13847 begin
13848 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
13849 -- Super_String, or one of the [Wide_]Wide_ versions. This will
13850 -- be True for all the Bounded_String types in instances of the
13851 -- Generic_Bounded_Length generics, and for types derived from those.
13852
13853 return Present (Under)
13854 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
13855 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
13856 Is_RTE (Root_Type (Under), RO_WW_Super_String));
13857 end Is_Bounded_String;
13858
13859 ---------------------
13860 -- Is_CCT_Instance --
13861 ---------------------
13862
13863 function Is_CCT_Instance
13864 (Ref_Id : Entity_Id;
13865 Context_Id : Entity_Id) return Boolean
13866 is
13867 begin
13868 pragma Assert (Ekind_In (Ref_Id, E_Protected_Type, E_Task_Type));
13869
13870 if Is_Single_Task_Object (Context_Id) then
13871 return Scope_Within_Or_Same (Etype (Context_Id), Ref_Id);
13872
13873 else
13874 pragma Assert (Ekind_In (Context_Id, E_Entry,
13875 E_Entry_Family,
13876 E_Function,
13877 E_Package,
13878 E_Procedure,
13879 E_Protected_Type,
13880 E_Task_Type)
13881 or else
13882 Is_Record_Type (Context_Id));
13883 return Scope_Within_Or_Same (Context_Id, Ref_Id);
13884 end if;
13885 end Is_CCT_Instance;
13886
13887 -------------------------
13888 -- Is_Child_Or_Sibling --
13889 -------------------------
13890
13891 function Is_Child_Or_Sibling
13892 (Pack_1 : Entity_Id;
13893 Pack_2 : Entity_Id) return Boolean
13894 is
13895 function Distance_From_Standard (Pack : Entity_Id) return Nat;
13896 -- Given an arbitrary package, return the number of "climbs" necessary
13897 -- to reach scope Standard_Standard.
13898
13899 procedure Equalize_Depths
13900 (Pack : in out Entity_Id;
13901 Depth : in out Nat;
13902 Depth_To_Reach : Nat);
13903 -- Given an arbitrary package, its depth and a target depth to reach,
13904 -- climb the scope chain until the said depth is reached. The pointer
13905 -- to the package and its depth a modified during the climb.
13906
13907 ----------------------------
13908 -- Distance_From_Standard --
13909 ----------------------------
13910
13911 function Distance_From_Standard (Pack : Entity_Id) return Nat is
13912 Dist : Nat;
13913 Scop : Entity_Id;
13914
13915 begin
13916 Dist := 0;
13917 Scop := Pack;
13918 while Present (Scop) and then Scop /= Standard_Standard loop
13919 Dist := Dist + 1;
13920 Scop := Scope (Scop);
13921 end loop;
13922
13923 return Dist;
13924 end Distance_From_Standard;
13925
13926 ---------------------
13927 -- Equalize_Depths --
13928 ---------------------
13929
13930 procedure Equalize_Depths
13931 (Pack : in out Entity_Id;
13932 Depth : in out Nat;
13933 Depth_To_Reach : Nat)
13934 is
13935 begin
13936 -- The package must be at a greater or equal depth
13937
13938 if Depth < Depth_To_Reach then
13939 raise Program_Error;
13940 end if;
13941
13942 -- Climb the scope chain until the desired depth is reached
13943
13944 while Present (Pack) and then Depth /= Depth_To_Reach loop
13945 Pack := Scope (Pack);
13946 Depth := Depth - 1;
13947 end loop;
13948 end Equalize_Depths;
13949
13950 -- Local variables
13951
13952 P_1 : Entity_Id := Pack_1;
13953 P_1_Child : Boolean := False;
13954 P_1_Depth : Nat := Distance_From_Standard (P_1);
13955 P_2 : Entity_Id := Pack_2;
13956 P_2_Child : Boolean := False;
13957 P_2_Depth : Nat := Distance_From_Standard (P_2);
13958
13959 -- Start of processing for Is_Child_Or_Sibling
13960
13961 begin
13962 pragma Assert
13963 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
13964
13965 -- Both packages denote the same entity, therefore they cannot be
13966 -- children or siblings.
13967
13968 if P_1 = P_2 then
13969 return False;
13970
13971 -- One of the packages is at a deeper level than the other. Note that
13972 -- both may still come from different hierarchies.
13973
13974 -- (root) P_2
13975 -- / \ :
13976 -- X P_2 or X
13977 -- : :
13978 -- P_1 P_1
13979
13980 elsif P_1_Depth > P_2_Depth then
13981 Equalize_Depths
13982 (Pack => P_1,
13983 Depth => P_1_Depth,
13984 Depth_To_Reach => P_2_Depth);
13985 P_1_Child := True;
13986
13987 -- (root) P_1
13988 -- / \ :
13989 -- P_1 X or X
13990 -- : :
13991 -- P_2 P_2
13992
13993 elsif P_2_Depth > P_1_Depth then
13994 Equalize_Depths
13995 (Pack => P_2,
13996 Depth => P_2_Depth,
13997 Depth_To_Reach => P_1_Depth);
13998 P_2_Child := True;
13999 end if;
14000
14001 -- At this stage the package pointers have been elevated to the same
14002 -- depth. If the related entities are the same, then one package is a
14003 -- potential child of the other:
14004
14005 -- P_1
14006 -- :
14007 -- X became P_1 P_2 or vice versa
14008 -- :
14009 -- P_2
14010
14011 if P_1 = P_2 then
14012 if P_1_Child then
14013 return Is_Child_Unit (Pack_1);
14014
14015 else pragma Assert (P_2_Child);
14016 return Is_Child_Unit (Pack_2);
14017 end if;
14018
14019 -- The packages may come from the same package chain or from entirely
14020 -- different hierarcies. To determine this, climb the scope stack until
14021 -- a common root is found.
14022
14023 -- (root) (root 1) (root 2)
14024 -- / \ | |
14025 -- P_1 P_2 P_1 P_2
14026
14027 else
14028 while Present (P_1) and then Present (P_2) loop
14029
14030 -- The two packages may be siblings
14031
14032 if P_1 = P_2 then
14033 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
14034 end if;
14035
14036 P_1 := Scope (P_1);
14037 P_2 := Scope (P_2);
14038 end loop;
14039 end if;
14040
14041 return False;
14042 end Is_Child_Or_Sibling;
14043
14044 -----------------------------
14045 -- Is_Concurrent_Interface --
14046 -----------------------------
14047
14048 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
14049 begin
14050 return Is_Interface (T)
14051 and then
14052 (Is_Protected_Interface (T)
14053 or else Is_Synchronized_Interface (T)
14054 or else Is_Task_Interface (T));
14055 end Is_Concurrent_Interface;
14056
14057 -----------------------
14058 -- Is_Constant_Bound --
14059 -----------------------
14060
14061 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
14062 begin
14063 if Compile_Time_Known_Value (Exp) then
14064 return True;
14065
14066 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
14067 return Is_Constant_Object (Entity (Exp))
14068 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
14069
14070 elsif Nkind (Exp) in N_Binary_Op then
14071 return Is_Constant_Bound (Left_Opnd (Exp))
14072 and then Is_Constant_Bound (Right_Opnd (Exp))
14073 and then Scope (Entity (Exp)) = Standard_Standard;
14074
14075 else
14076 return False;
14077 end if;
14078 end Is_Constant_Bound;
14079
14080 ---------------------------
14081 -- Is_Container_Element --
14082 ---------------------------
14083
14084 function Is_Container_Element (Exp : Node_Id) return Boolean is
14085 Loc : constant Source_Ptr := Sloc (Exp);
14086 Pref : constant Node_Id := Prefix (Exp);
14087
14088 Call : Node_Id;
14089 -- Call to an indexing aspect
14090
14091 Cont_Typ : Entity_Id;
14092 -- The type of the container being accessed
14093
14094 Elem_Typ : Entity_Id;
14095 -- Its element type
14096
14097 Indexing : Entity_Id;
14098 Is_Const : Boolean;
14099 -- Indicates that constant indexing is used, and the element is thus
14100 -- a constant.
14101
14102 Ref_Typ : Entity_Id;
14103 -- The reference type returned by the indexing operation
14104
14105 begin
14106 -- If C is a container, in a context that imposes the element type of
14107 -- that container, the indexing notation C (X) is rewritten as:
14108
14109 -- Indexing (C, X).Discr.all
14110
14111 -- where Indexing is one of the indexing aspects of the container.
14112 -- If the context does not require a reference, the construct can be
14113 -- rewritten as
14114
14115 -- Element (C, X)
14116
14117 -- First, verify that the construct has the proper form
14118
14119 if not Expander_Active then
14120 return False;
14121
14122 elsif Nkind (Pref) /= N_Selected_Component then
14123 return False;
14124
14125 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
14126 return False;
14127
14128 else
14129 Call := Prefix (Pref);
14130 Ref_Typ := Etype (Call);
14131 end if;
14132
14133 if not Has_Implicit_Dereference (Ref_Typ)
14134 or else No (First (Parameter_Associations (Call)))
14135 or else not Is_Entity_Name (Name (Call))
14136 then
14137 return False;
14138 end if;
14139
14140 -- Retrieve type of container object, and its iterator aspects
14141
14142 Cont_Typ := Etype (First (Parameter_Associations (Call)));
14143 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
14144 Is_Const := False;
14145
14146 if No (Indexing) then
14147
14148 -- Container should have at least one indexing operation
14149
14150 return False;
14151
14152 elsif Entity (Name (Call)) /= Entity (Indexing) then
14153
14154 -- This may be a variable indexing operation
14155
14156 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
14157
14158 if No (Indexing)
14159 or else Entity (Name (Call)) /= Entity (Indexing)
14160 then
14161 return False;
14162 end if;
14163
14164 else
14165 Is_Const := True;
14166 end if;
14167
14168 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
14169
14170 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
14171 return False;
14172 end if;
14173
14174 -- Check that the expression is not the target of an assignment, in
14175 -- which case the rewriting is not possible.
14176
14177 if not Is_Const then
14178 declare
14179 Par : Node_Id;
14180
14181 begin
14182 Par := Exp;
14183 while Present (Par)
14184 loop
14185 if Nkind (Parent (Par)) = N_Assignment_Statement
14186 and then Par = Name (Parent (Par))
14187 then
14188 return False;
14189
14190 -- A renaming produces a reference, and the transformation
14191 -- does not apply.
14192
14193 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
14194 return False;
14195
14196 elsif Nkind_In
14197 (Nkind (Parent (Par)), N_Function_Call,
14198 N_Procedure_Call_Statement,
14199 N_Entry_Call_Statement)
14200 then
14201 -- Check that the element is not part of an actual for an
14202 -- in-out parameter.
14203
14204 declare
14205 F : Entity_Id;
14206 A : Node_Id;
14207
14208 begin
14209 F := First_Formal (Entity (Name (Parent (Par))));
14210 A := First (Parameter_Associations (Parent (Par)));
14211 while Present (F) loop
14212 if A = Par and then Ekind (F) /= E_In_Parameter then
14213 return False;
14214 end if;
14215
14216 Next_Formal (F);
14217 Next (A);
14218 end loop;
14219 end;
14220
14221 -- E_In_Parameter in a call: element is not modified.
14222
14223 exit;
14224 end if;
14225
14226 Par := Parent (Par);
14227 end loop;
14228 end;
14229 end if;
14230
14231 -- The expression has the proper form and the context requires the
14232 -- element type. Retrieve the Element function of the container and
14233 -- rewrite the construct as a call to it.
14234
14235 declare
14236 Op : Elmt_Id;
14237
14238 begin
14239 Op := First_Elmt (Primitive_Operations (Cont_Typ));
14240 while Present (Op) loop
14241 exit when Chars (Node (Op)) = Name_Element;
14242 Next_Elmt (Op);
14243 end loop;
14244
14245 if No (Op) then
14246 return False;
14247
14248 else
14249 Rewrite (Exp,
14250 Make_Function_Call (Loc,
14251 Name => New_Occurrence_Of (Node (Op), Loc),
14252 Parameter_Associations => Parameter_Associations (Call)));
14253 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
14254 return True;
14255 end if;
14256 end;
14257 end Is_Container_Element;
14258
14259 ----------------------------
14260 -- Is_Contract_Annotation --
14261 ----------------------------
14262
14263 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
14264 begin
14265 return Is_Package_Contract_Annotation (Item)
14266 or else
14267 Is_Subprogram_Contract_Annotation (Item);
14268 end Is_Contract_Annotation;
14269
14270 --------------------------------------
14271 -- Is_Controlling_Limited_Procedure --
14272 --------------------------------------
14273
14274 function Is_Controlling_Limited_Procedure
14275 (Proc_Nam : Entity_Id) return Boolean
14276 is
14277 Param : Node_Id;
14278 Param_Typ : Entity_Id := Empty;
14279
14280 begin
14281 if Ekind (Proc_Nam) = E_Procedure
14282 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
14283 then
14284 Param :=
14285 Parameter_Type
14286 (First (Parameter_Specifications (Parent (Proc_Nam))));
14287
14288 -- The formal may be an anonymous access type
14289
14290 if Nkind (Param) = N_Access_Definition then
14291 Param_Typ := Entity (Subtype_Mark (Param));
14292 else
14293 Param_Typ := Etype (Param);
14294 end if;
14295
14296 -- In the case where an Itype was created for a dispatchin call, the
14297 -- procedure call has been rewritten. The actual may be an access to
14298 -- interface type in which case it is the designated type that is the
14299 -- controlling type.
14300
14301 elsif Present (Associated_Node_For_Itype (Proc_Nam))
14302 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
14303 and then
14304 Present (Parameter_Associations
14305 (Associated_Node_For_Itype (Proc_Nam)))
14306 then
14307 Param_Typ :=
14308 Etype (First (Parameter_Associations
14309 (Associated_Node_For_Itype (Proc_Nam))));
14310
14311 if Ekind (Param_Typ) = E_Anonymous_Access_Type then
14312 Param_Typ := Directly_Designated_Type (Param_Typ);
14313 end if;
14314 end if;
14315
14316 if Present (Param_Typ) then
14317 return
14318 Is_Interface (Param_Typ)
14319 and then Is_Limited_Record (Param_Typ);
14320 end if;
14321
14322 return False;
14323 end Is_Controlling_Limited_Procedure;
14324
14325 -----------------------------
14326 -- Is_CPP_Constructor_Call --
14327 -----------------------------
14328
14329 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
14330 begin
14331 return Nkind (N) = N_Function_Call
14332 and then Is_CPP_Class (Etype (Etype (N)))
14333 and then Is_Constructor (Entity (Name (N)))
14334 and then Is_Imported (Entity (Name (N)));
14335 end Is_CPP_Constructor_Call;
14336
14337 -------------------------
14338 -- Is_Current_Instance --
14339 -------------------------
14340
14341 function Is_Current_Instance (N : Node_Id) return Boolean is
14342 Typ : constant Entity_Id := Entity (N);
14343 P : Node_Id;
14344
14345 begin
14346 -- Simplest case: entity is a concurrent type and we are currently
14347 -- inside the body. This will eventually be expanded into a call to
14348 -- Self (for tasks) or _object (for protected objects).
14349
14350 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
14351 return True;
14352
14353 else
14354 -- Check whether the context is a (sub)type declaration for the
14355 -- type entity.
14356
14357 P := Parent (N);
14358 while Present (P) loop
14359 if Nkind_In (P, N_Full_Type_Declaration,
14360 N_Private_Type_Declaration,
14361 N_Subtype_Declaration)
14362 and then Comes_From_Source (P)
14363 and then Defining_Entity (P) = Typ
14364 then
14365 return True;
14366
14367 -- A subtype name may appear in an aspect specification for a
14368 -- Predicate_Failure aspect, for which we do not construct a
14369 -- wrapper procedure. The subtype will be replaced by the
14370 -- expression being tested when the corresponding predicate
14371 -- check is expanded.
14372
14373 elsif Nkind (P) = N_Aspect_Specification
14374 and then Nkind (Parent (P)) = N_Subtype_Declaration
14375 then
14376 return True;
14377
14378 elsif Nkind (P) = N_Pragma
14379 and then Get_Pragma_Id (P) = Pragma_Predicate_Failure
14380 then
14381 return True;
14382 end if;
14383
14384 P := Parent (P);
14385 end loop;
14386 end if;
14387
14388 -- In any other context this is not a current occurrence
14389
14390 return False;
14391 end Is_Current_Instance;
14392
14393 --------------------
14394 -- Is_Declaration --
14395 --------------------
14396
14397 function Is_Declaration
14398 (N : Node_Id;
14399 Body_OK : Boolean := True;
14400 Concurrent_OK : Boolean := True;
14401 Formal_OK : Boolean := True;
14402 Generic_OK : Boolean := True;
14403 Instantiation_OK : Boolean := True;
14404 Renaming_OK : Boolean := True;
14405 Stub_OK : Boolean := True;
14406 Subprogram_OK : Boolean := True;
14407 Type_OK : Boolean := True) return Boolean
14408 is
14409 begin
14410 case Nkind (N) is
14411
14412 -- Body declarations
14413
14414 when N_Proper_Body =>
14415 return Body_OK;
14416
14417 -- Concurrent type declarations
14418
14419 when N_Protected_Type_Declaration
14420 | N_Single_Protected_Declaration
14421 | N_Single_Task_Declaration
14422 | N_Task_Type_Declaration
14423 =>
14424 return Concurrent_OK or Type_OK;
14425
14426 -- Formal declarations
14427
14428 when N_Formal_Abstract_Subprogram_Declaration
14429 | N_Formal_Concrete_Subprogram_Declaration
14430 | N_Formal_Object_Declaration
14431 | N_Formal_Package_Declaration
14432 | N_Formal_Type_Declaration
14433 =>
14434 return Formal_OK;
14435
14436 -- Generic declarations
14437
14438 when N_Generic_Package_Declaration
14439 | N_Generic_Subprogram_Declaration
14440 =>
14441 return Generic_OK;
14442
14443 -- Generic instantiations
14444
14445 when N_Function_Instantiation
14446 | N_Package_Instantiation
14447 | N_Procedure_Instantiation
14448 =>
14449 return Instantiation_OK;
14450
14451 -- Generic renaming declarations
14452
14453 when N_Generic_Renaming_Declaration =>
14454 return Generic_OK or Renaming_OK;
14455
14456 -- Renaming declarations
14457
14458 when N_Exception_Renaming_Declaration
14459 | N_Object_Renaming_Declaration
14460 | N_Package_Renaming_Declaration
14461 | N_Subprogram_Renaming_Declaration
14462 =>
14463 return Renaming_OK;
14464
14465 -- Stub declarations
14466
14467 when N_Body_Stub =>
14468 return Stub_OK;
14469
14470 -- Subprogram declarations
14471
14472 when N_Abstract_Subprogram_Declaration
14473 | N_Entry_Declaration
14474 | N_Expression_Function
14475 | N_Subprogram_Declaration
14476 =>
14477 return Subprogram_OK;
14478
14479 -- Type declarations
14480
14481 when N_Full_Type_Declaration
14482 | N_Incomplete_Type_Declaration
14483 | N_Private_Extension_Declaration
14484 | N_Private_Type_Declaration
14485 | N_Subtype_Declaration
14486 =>
14487 return Type_OK;
14488
14489 -- Miscellaneous
14490
14491 when N_Component_Declaration
14492 | N_Exception_Declaration
14493 | N_Implicit_Label_Declaration
14494 | N_Number_Declaration
14495 | N_Object_Declaration
14496 | N_Package_Declaration
14497 =>
14498 return True;
14499
14500 when others =>
14501 return False;
14502 end case;
14503 end Is_Declaration;
14504
14505 --------------------------------
14506 -- Is_Declared_Within_Variant --
14507 --------------------------------
14508
14509 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
14510 Comp_Decl : constant Node_Id := Parent (Comp);
14511 Comp_List : constant Node_Id := Parent (Comp_Decl);
14512 begin
14513 return Nkind (Parent (Comp_List)) = N_Variant;
14514 end Is_Declared_Within_Variant;
14515
14516 ----------------------------------------------
14517 -- Is_Dependent_Component_Of_Mutable_Object --
14518 ----------------------------------------------
14519
14520 function Is_Dependent_Component_Of_Mutable_Object
14521 (Object : Node_Id) return Boolean
14522 is
14523 P : Node_Id;
14524 Prefix_Type : Entity_Id;
14525 P_Aliased : Boolean := False;
14526 Comp : Entity_Id;
14527
14528 Deref : Node_Id := Object;
14529 -- Dereference node, in something like X.all.Y(2)
14530
14531 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
14532
14533 begin
14534 -- Find the dereference node if any
14535
14536 while Nkind_In (Deref, N_Indexed_Component,
14537 N_Selected_Component,
14538 N_Slice)
14539 loop
14540 Deref := Prefix (Deref);
14541 end loop;
14542
14543 -- If the prefix is a qualified expression of a variable, then function
14544 -- Is_Variable will return False for that because a qualified expression
14545 -- denotes a constant view, so we need to get the name being qualified
14546 -- so we can test below whether that's a variable (or a dereference).
14547
14548 if Nkind (Deref) = N_Qualified_Expression then
14549 Deref := Expression (Deref);
14550 end if;
14551
14552 -- Ada 2005: If we have a component or slice of a dereference, something
14553 -- like X.all.Y (2) and the type of X is access-to-constant, Is_Variable
14554 -- will return False, because it is indeed a constant view. But it might
14555 -- be a view of a variable object, so we want the following condition to
14556 -- be True in that case.
14557
14558 if Is_Variable (Object)
14559 or else Is_Variable (Deref)
14560 or else (Ada_Version >= Ada_2005
14561 and then (Nkind (Deref) = N_Explicit_Dereference
14562 or else Is_Access_Type (Etype (Deref))))
14563 then
14564 if Nkind (Object) = N_Selected_Component then
14565
14566 -- If the selector is not a component, then we definitely return
14567 -- False (it could be a function selector in a prefix form call
14568 -- occurring in an iterator specification).
14569
14570 if not Ekind_In (Entity (Selector_Name (Object)), E_Component,
14571 E_Discriminant)
14572 then
14573 return False;
14574 end if;
14575
14576 -- Get the original node of the prefix in case it has been
14577 -- rewritten, which can occur, for example, in qualified
14578 -- expression cases. Also, a discriminant check on a selected
14579 -- component may be expanded into a dereference when removing
14580 -- side effects, and the subtype of the original node may be
14581 -- unconstrained.
14582
14583 P := Original_Node (Prefix (Object));
14584 Prefix_Type := Etype (P);
14585
14586 -- If the prefix is a qualified expression, we want to look at its
14587 -- operand.
14588
14589 if Nkind (P) = N_Qualified_Expression then
14590 P := Expression (P);
14591 Prefix_Type := Etype (P);
14592 end if;
14593
14594 if Is_Entity_Name (P) then
14595 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
14596 Prefix_Type := Base_Type (Prefix_Type);
14597 end if;
14598
14599 if Is_Aliased (Entity (P)) then
14600 P_Aliased := True;
14601 end if;
14602
14603 -- For explicit dereferences we get the access prefix so we can
14604 -- treat this similarly to implicit dereferences and examine the
14605 -- kind of the access type and its designated subtype further
14606 -- below.
14607
14608 elsif Nkind (P) = N_Explicit_Dereference then
14609 P := Prefix (P);
14610 Prefix_Type := Etype (P);
14611
14612 else
14613 -- Check for prefix being an aliased component???
14614
14615 null;
14616 end if;
14617
14618 -- A heap object is constrained by its initial value
14619
14620 -- Ada 2005 (AI-363): Always assume the object could be mutable in
14621 -- the dereferenced case, since the access value might denote an
14622 -- unconstrained aliased object, whereas in Ada 95 the designated
14623 -- object is guaranteed to be constrained. A worst-case assumption
14624 -- has to apply in Ada 2005 because we can't tell at compile
14625 -- time whether the object is "constrained by its initial value",
14626 -- despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
14627 -- rules (these rules are acknowledged to need fixing). We don't
14628 -- impose this more stringent checking for earlier Ada versions or
14629 -- when Relaxed_RM_Semantics applies (the latter for CodePeer's
14630 -- benefit, though it's unclear on why using -gnat95 would not be
14631 -- sufficient???).
14632
14633 if Ada_Version < Ada_2005 or else Relaxed_RM_Semantics then
14634 if Is_Access_Type (Prefix_Type)
14635 or else Nkind (P) = N_Explicit_Dereference
14636 then
14637 return False;
14638 end if;
14639
14640 else pragma Assert (Ada_Version >= Ada_2005);
14641 if Is_Access_Type (Prefix_Type) then
14642 -- We need to make sure we have the base subtype, in case
14643 -- this is actually an access subtype (whose Ekind will be
14644 -- E_Access_Subtype).
14645
14646 Prefix_Type := Etype (Prefix_Type);
14647
14648 -- If the access type is pool-specific, and there is no
14649 -- constrained partial view of the designated type, then the
14650 -- designated object is known to be constrained. If it's a
14651 -- formal access type and the renaming is in the generic
14652 -- spec, we also treat it as pool-specific (known to be
14653 -- constrained), but assume the worst if in the generic body
14654 -- (see RM 3.3(23.3/3)).
14655
14656 if Ekind (Prefix_Type) = E_Access_Type
14657 and then (not Is_Generic_Type (Prefix_Type)
14658 or else not In_Generic_Body (Current_Scope))
14659 and then not Object_Type_Has_Constrained_Partial_View
14660 (Typ => Designated_Type (Prefix_Type),
14661 Scop => Current_Scope)
14662 then
14663 return False;
14664
14665 -- Otherwise (general access type, or there is a constrained
14666 -- partial view of the designated type), we need to check
14667 -- based on the designated type.
14668
14669 else
14670 Prefix_Type := Designated_Type (Prefix_Type);
14671 end if;
14672 end if;
14673 end if;
14674
14675 Comp :=
14676 Original_Record_Component (Entity (Selector_Name (Object)));
14677
14678 -- As per AI-0017, the renaming is illegal in a generic body, even
14679 -- if the subtype is indefinite (only applies to prefixes of an
14680 -- untagged formal type, see RM 3.3 (23.11/3)).
14681
14682 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
14683
14684 if not Is_Constrained (Prefix_Type)
14685 and then (Is_Definite_Subtype (Prefix_Type)
14686 or else
14687 (not Is_Tagged_Type (Prefix_Type)
14688 and then Is_Generic_Type (Prefix_Type)
14689 and then In_Generic_Body (Current_Scope)))
14690
14691 and then (Is_Declared_Within_Variant (Comp)
14692 or else Has_Discriminant_Dependent_Constraint (Comp))
14693 and then (not P_Aliased or else Ada_Version >= Ada_2005)
14694 then
14695 return True;
14696
14697 -- If the prefix is of an access type at this point, then we want
14698 -- to return False, rather than calling this function recursively
14699 -- on the access object (which itself might be a discriminant-
14700 -- dependent component of some other object, but that isn't
14701 -- relevant to checking the object passed to us). This avoids
14702 -- issuing wrong errors when compiling with -gnatc, where there
14703 -- can be implicit dereferences that have not been expanded.
14704
14705 elsif Is_Access_Type (Etype (Prefix (Object))) then
14706 return False;
14707
14708 else
14709 return
14710 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
14711 end if;
14712
14713 elsif Nkind (Object) = N_Indexed_Component
14714 or else Nkind (Object) = N_Slice
14715 then
14716 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
14717
14718 -- A type conversion that Is_Variable is a view conversion:
14719 -- go back to the denoted object.
14720
14721 elsif Nkind (Object) = N_Type_Conversion then
14722 return
14723 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
14724 end if;
14725 end if;
14726
14727 return False;
14728 end Is_Dependent_Component_Of_Mutable_Object;
14729
14730 ---------------------
14731 -- Is_Dereferenced --
14732 ---------------------
14733
14734 function Is_Dereferenced (N : Node_Id) return Boolean is
14735 P : constant Node_Id := Parent (N);
14736 begin
14737 return Nkind_In (P, N_Selected_Component,
14738 N_Explicit_Dereference,
14739 N_Indexed_Component,
14740 N_Slice)
14741 and then Prefix (P) = N;
14742 end Is_Dereferenced;
14743
14744 ----------------------
14745 -- Is_Descendant_Of --
14746 ----------------------
14747
14748 function Is_Descendant_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
14749 T : Entity_Id;
14750 Etyp : Entity_Id;
14751
14752 begin
14753 pragma Assert (Nkind (T1) in N_Entity);
14754 pragma Assert (Nkind (T2) in N_Entity);
14755
14756 T := Base_Type (T1);
14757
14758 -- Immediate return if the types match
14759
14760 if T = T2 then
14761 return True;
14762
14763 -- Comment needed here ???
14764
14765 elsif Ekind (T) = E_Class_Wide_Type then
14766 return Etype (T) = T2;
14767
14768 -- All other cases
14769
14770 else
14771 loop
14772 Etyp := Etype (T);
14773
14774 -- Done if we found the type we are looking for
14775
14776 if Etyp = T2 then
14777 return True;
14778
14779 -- Done if no more derivations to check
14780
14781 elsif T = T1
14782 or else T = Etyp
14783 then
14784 return False;
14785
14786 -- Following test catches error cases resulting from prev errors
14787
14788 elsif No (Etyp) then
14789 return False;
14790
14791 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
14792 return False;
14793
14794 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
14795 return False;
14796 end if;
14797
14798 T := Base_Type (Etyp);
14799 end loop;
14800 end if;
14801 end Is_Descendant_Of;
14802
14803 ----------------------------------------
14804 -- Is_Descendant_Of_Suspension_Object --
14805 ----------------------------------------
14806
14807 function Is_Descendant_Of_Suspension_Object
14808 (Typ : Entity_Id) return Boolean
14809 is
14810 Cur_Typ : Entity_Id;
14811 Par_Typ : Entity_Id;
14812
14813 begin
14814 -- Climb the type derivation chain checking each parent type against
14815 -- Suspension_Object.
14816
14817 Cur_Typ := Base_Type (Typ);
14818 while Present (Cur_Typ) loop
14819 Par_Typ := Etype (Cur_Typ);
14820
14821 -- The current type is a match
14822
14823 if Is_Suspension_Object (Cur_Typ) then
14824 return True;
14825
14826 -- Stop the traversal once the root of the derivation chain has been
14827 -- reached. In that case the current type is its own base type.
14828
14829 elsif Cur_Typ = Par_Typ then
14830 exit;
14831 end if;
14832
14833 Cur_Typ := Base_Type (Par_Typ);
14834 end loop;
14835
14836 return False;
14837 end Is_Descendant_Of_Suspension_Object;
14838
14839 ---------------------------------------------
14840 -- Is_Double_Precision_Floating_Point_Type --
14841 ---------------------------------------------
14842
14843 function Is_Double_Precision_Floating_Point_Type
14844 (E : Entity_Id) return Boolean is
14845 begin
14846 return Is_Floating_Point_Type (E)
14847 and then Machine_Radix_Value (E) = Uint_2
14848 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
14849 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
14850 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
14851 end Is_Double_Precision_Floating_Point_Type;
14852
14853 -----------------------------
14854 -- Is_Effectively_Volatile --
14855 -----------------------------
14856
14857 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
14858 begin
14859 if Is_Type (Id) then
14860
14861 -- An arbitrary type is effectively volatile when it is subject to
14862 -- pragma Atomic or Volatile.
14863
14864 if Is_Volatile (Id) then
14865 return True;
14866
14867 -- An array type is effectively volatile when it is subject to pragma
14868 -- Atomic_Components or Volatile_Components or its component type is
14869 -- effectively volatile.
14870
14871 elsif Is_Array_Type (Id) then
14872 declare
14873 Anc : Entity_Id := Base_Type (Id);
14874 begin
14875 if Is_Private_Type (Anc) then
14876 Anc := Full_View (Anc);
14877 end if;
14878
14879 -- Test for presence of ancestor, as the full view of a private
14880 -- type may be missing in case of error.
14881
14882 return
14883 Has_Volatile_Components (Id)
14884 or else
14885 (Present (Anc)
14886 and then Is_Effectively_Volatile (Component_Type (Anc)));
14887 end;
14888
14889 -- A protected type is always volatile
14890
14891 elsif Is_Protected_Type (Id) then
14892 return True;
14893
14894 -- A descendant of Ada.Synchronous_Task_Control.Suspension_Object is
14895 -- automatically volatile.
14896
14897 elsif Is_Descendant_Of_Suspension_Object (Id) then
14898 return True;
14899
14900 -- Otherwise the type is not effectively volatile
14901
14902 else
14903 return False;
14904 end if;
14905
14906 -- Otherwise Id denotes an object
14907
14908 else
14909 -- A volatile object for which No_Caching is enabled is not
14910 -- effectively volatile.
14911
14912 return
14913 (Is_Volatile (Id) and then not No_Caching_Enabled (Id))
14914 or else Has_Volatile_Components (Id)
14915 or else Is_Effectively_Volatile (Etype (Id));
14916 end if;
14917 end Is_Effectively_Volatile;
14918
14919 ------------------------------------
14920 -- Is_Effectively_Volatile_Object --
14921 ------------------------------------
14922
14923 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
14924 begin
14925 if Is_Entity_Name (N) then
14926 return Is_Effectively_Volatile (Entity (N));
14927
14928 elsif Nkind (N) = N_Indexed_Component then
14929 return Is_Effectively_Volatile_Object (Prefix (N));
14930
14931 elsif Nkind (N) = N_Selected_Component then
14932 return
14933 Is_Effectively_Volatile_Object (Prefix (N))
14934 or else
14935 Is_Effectively_Volatile_Object (Selector_Name (N));
14936
14937 else
14938 return False;
14939 end if;
14940 end Is_Effectively_Volatile_Object;
14941
14942 -------------------
14943 -- Is_Entry_Body --
14944 -------------------
14945
14946 function Is_Entry_Body (Id : Entity_Id) return Boolean is
14947 begin
14948 return
14949 Ekind_In (Id, E_Entry, E_Entry_Family)
14950 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Body;
14951 end Is_Entry_Body;
14952
14953 --------------------------
14954 -- Is_Entry_Declaration --
14955 --------------------------
14956
14957 function Is_Entry_Declaration (Id : Entity_Id) return Boolean is
14958 begin
14959 return
14960 Ekind_In (Id, E_Entry, E_Entry_Family)
14961 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Declaration;
14962 end Is_Entry_Declaration;
14963
14964 ------------------------------------
14965 -- Is_Expanded_Priority_Attribute --
14966 ------------------------------------
14967
14968 function Is_Expanded_Priority_Attribute (E : Entity_Id) return Boolean is
14969 begin
14970 return
14971 Nkind (E) = N_Function_Call
14972 and then not Configurable_Run_Time_Mode
14973 and then Nkind (Original_Node (E)) = N_Attribute_Reference
14974 and then (Entity (Name (E)) = RTE (RE_Get_Ceiling)
14975 or else Entity (Name (E)) = RTE (RO_PE_Get_Ceiling));
14976 end Is_Expanded_Priority_Attribute;
14977
14978 ----------------------------
14979 -- Is_Expression_Function --
14980 ----------------------------
14981
14982 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
14983 begin
14984 if Ekind_In (Subp, E_Function, E_Subprogram_Body) then
14985 return
14986 Nkind (Original_Node (Unit_Declaration_Node (Subp))) =
14987 N_Expression_Function;
14988 else
14989 return False;
14990 end if;
14991 end Is_Expression_Function;
14992
14993 ------------------------------------------
14994 -- Is_Expression_Function_Or_Completion --
14995 ------------------------------------------
14996
14997 function Is_Expression_Function_Or_Completion
14998 (Subp : Entity_Id) return Boolean
14999 is
15000 Subp_Decl : Node_Id;
15001
15002 begin
15003 if Ekind (Subp) = E_Function then
15004 Subp_Decl := Unit_Declaration_Node (Subp);
15005
15006 -- The function declaration is either an expression function or is
15007 -- completed by an expression function body.
15008
15009 return
15010 Is_Expression_Function (Subp)
15011 or else (Nkind (Subp_Decl) = N_Subprogram_Declaration
15012 and then Present (Corresponding_Body (Subp_Decl))
15013 and then Is_Expression_Function
15014 (Corresponding_Body (Subp_Decl)));
15015
15016 elsif Ekind (Subp) = E_Subprogram_Body then
15017 return Is_Expression_Function (Subp);
15018
15019 else
15020 return False;
15021 end if;
15022 end Is_Expression_Function_Or_Completion;
15023
15024 -----------------------
15025 -- Is_EVF_Expression --
15026 -----------------------
15027
15028 function Is_EVF_Expression (N : Node_Id) return Boolean is
15029 Orig_N : constant Node_Id := Original_Node (N);
15030 Alt : Node_Id;
15031 Expr : Node_Id;
15032 Id : Entity_Id;
15033
15034 begin
15035 -- Detect a reference to a formal parameter of a specific tagged type
15036 -- whose related subprogram is subject to pragma Expresions_Visible with
15037 -- value "False".
15038
15039 if Is_Entity_Name (N) and then Present (Entity (N)) then
15040 Id := Entity (N);
15041
15042 return
15043 Is_Formal (Id)
15044 and then Is_Specific_Tagged_Type (Etype (Id))
15045 and then Extensions_Visible_Status (Id) =
15046 Extensions_Visible_False;
15047
15048 -- A case expression is an EVF expression when it contains at least one
15049 -- EVF dependent_expression. Note that a case expression may have been
15050 -- expanded, hence the use of Original_Node.
15051
15052 elsif Nkind (Orig_N) = N_Case_Expression then
15053 Alt := First (Alternatives (Orig_N));
15054 while Present (Alt) loop
15055 if Is_EVF_Expression (Expression (Alt)) then
15056 return True;
15057 end if;
15058
15059 Next (Alt);
15060 end loop;
15061
15062 -- An if expression is an EVF expression when it contains at least one
15063 -- EVF dependent_expression. Note that an if expression may have been
15064 -- expanded, hence the use of Original_Node.
15065
15066 elsif Nkind (Orig_N) = N_If_Expression then
15067 Expr := Next (First (Expressions (Orig_N)));
15068 while Present (Expr) loop
15069 if Is_EVF_Expression (Expr) then
15070 return True;
15071 end if;
15072
15073 Next (Expr);
15074 end loop;
15075
15076 -- A qualified expression or a type conversion is an EVF expression when
15077 -- its operand is an EVF expression.
15078
15079 elsif Nkind_In (N, N_Qualified_Expression,
15080 N_Unchecked_Type_Conversion,
15081 N_Type_Conversion)
15082 then
15083 return Is_EVF_Expression (Expression (N));
15084
15085 -- Attributes 'Loop_Entry, 'Old, and 'Update are EVF expressions when
15086 -- their prefix denotes an EVF expression.
15087
15088 elsif Nkind (N) = N_Attribute_Reference
15089 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
15090 Name_Old,
15091 Name_Update)
15092 then
15093 return Is_EVF_Expression (Prefix (N));
15094 end if;
15095
15096 return False;
15097 end Is_EVF_Expression;
15098
15099 --------------
15100 -- Is_False --
15101 --------------
15102
15103 function Is_False (U : Uint) return Boolean is
15104 begin
15105 return (U = 0);
15106 end Is_False;
15107
15108 ---------------------------
15109 -- Is_Fixed_Model_Number --
15110 ---------------------------
15111
15112 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
15113 S : constant Ureal := Small_Value (T);
15114 M : Urealp.Save_Mark;
15115 R : Boolean;
15116
15117 begin
15118 M := Urealp.Mark;
15119 R := (U = UR_Trunc (U / S) * S);
15120 Urealp.Release (M);
15121 return R;
15122 end Is_Fixed_Model_Number;
15123
15124 -------------------------------
15125 -- Is_Fully_Initialized_Type --
15126 -------------------------------
15127
15128 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
15129 begin
15130 -- Scalar types
15131
15132 if Is_Scalar_Type (Typ) then
15133
15134 -- A scalar type with an aspect Default_Value is fully initialized
15135
15136 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
15137 -- of a scalar type, but we don't take that into account here, since
15138 -- we don't want these to affect warnings.
15139
15140 return Has_Default_Aspect (Typ);
15141
15142 elsif Is_Access_Type (Typ) then
15143 return True;
15144
15145 elsif Is_Array_Type (Typ) then
15146 if Is_Fully_Initialized_Type (Component_Type (Typ))
15147 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
15148 then
15149 return True;
15150 end if;
15151
15152 -- An interesting case, if we have a constrained type one of whose
15153 -- bounds is known to be null, then there are no elements to be
15154 -- initialized, so all the elements are initialized.
15155
15156 if Is_Constrained (Typ) then
15157 declare
15158 Indx : Node_Id;
15159 Indx_Typ : Entity_Id;
15160 Lbd, Hbd : Node_Id;
15161
15162 begin
15163 Indx := First_Index (Typ);
15164 while Present (Indx) loop
15165 if Etype (Indx) = Any_Type then
15166 return False;
15167
15168 -- If index is a range, use directly
15169
15170 elsif Nkind (Indx) = N_Range then
15171 Lbd := Low_Bound (Indx);
15172 Hbd := High_Bound (Indx);
15173
15174 else
15175 Indx_Typ := Etype (Indx);
15176
15177 if Is_Private_Type (Indx_Typ) then
15178 Indx_Typ := Full_View (Indx_Typ);
15179 end if;
15180
15181 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
15182 return False;
15183 else
15184 Lbd := Type_Low_Bound (Indx_Typ);
15185 Hbd := Type_High_Bound (Indx_Typ);
15186 end if;
15187 end if;
15188
15189 if Compile_Time_Known_Value (Lbd)
15190 and then
15191 Compile_Time_Known_Value (Hbd)
15192 then
15193 if Expr_Value (Hbd) < Expr_Value (Lbd) then
15194 return True;
15195 end if;
15196 end if;
15197
15198 Next_Index (Indx);
15199 end loop;
15200 end;
15201 end if;
15202
15203 -- If no null indexes, then type is not fully initialized
15204
15205 return False;
15206
15207 -- Record types
15208
15209 elsif Is_Record_Type (Typ) then
15210 if Has_Discriminants (Typ)
15211 and then
15212 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
15213 and then Is_Fully_Initialized_Variant (Typ)
15214 then
15215 return True;
15216 end if;
15217
15218 -- We consider bounded string types to be fully initialized, because
15219 -- otherwise we get false alarms when the Data component is not
15220 -- default-initialized.
15221
15222 if Is_Bounded_String (Typ) then
15223 return True;
15224 end if;
15225
15226 -- Controlled records are considered to be fully initialized if
15227 -- there is a user defined Initialize routine. This may not be
15228 -- entirely correct, but as the spec notes, we are guessing here
15229 -- what is best from the point of view of issuing warnings.
15230
15231 if Is_Controlled (Typ) then
15232 declare
15233 Utyp : constant Entity_Id := Underlying_Type (Typ);
15234
15235 begin
15236 if Present (Utyp) then
15237 declare
15238 Init : constant Entity_Id :=
15239 (Find_Optional_Prim_Op
15240 (Underlying_Type (Typ), Name_Initialize));
15241
15242 begin
15243 if Present (Init)
15244 and then Comes_From_Source (Init)
15245 and then not In_Predefined_Unit (Init)
15246 then
15247 return True;
15248
15249 elsif Has_Null_Extension (Typ)
15250 and then
15251 Is_Fully_Initialized_Type
15252 (Etype (Base_Type (Typ)))
15253 then
15254 return True;
15255 end if;
15256 end;
15257 end if;
15258 end;
15259 end if;
15260
15261 -- Otherwise see if all record components are initialized
15262
15263 declare
15264 Ent : Entity_Id;
15265
15266 begin
15267 Ent := First_Entity (Typ);
15268 while Present (Ent) loop
15269 if Ekind (Ent) = E_Component
15270 and then (No (Parent (Ent))
15271 or else No (Expression (Parent (Ent))))
15272 and then not Is_Fully_Initialized_Type (Etype (Ent))
15273
15274 -- Special VM case for tag components, which need to be
15275 -- defined in this case, but are never initialized as VMs
15276 -- are using other dispatching mechanisms. Ignore this
15277 -- uninitialized case. Note that this applies both to the
15278 -- uTag entry and the main vtable pointer (CPP_Class case).
15279
15280 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
15281 then
15282 return False;
15283 end if;
15284
15285 Next_Entity (Ent);
15286 end loop;
15287 end;
15288
15289 -- No uninitialized components, so type is fully initialized.
15290 -- Note that this catches the case of no components as well.
15291
15292 return True;
15293
15294 elsif Is_Concurrent_Type (Typ) then
15295 return True;
15296
15297 elsif Is_Private_Type (Typ) then
15298 declare
15299 U : constant Entity_Id := Underlying_Type (Typ);
15300
15301 begin
15302 if No (U) then
15303 return False;
15304 else
15305 return Is_Fully_Initialized_Type (U);
15306 end if;
15307 end;
15308
15309 else
15310 return False;
15311 end if;
15312 end Is_Fully_Initialized_Type;
15313
15314 ----------------------------------
15315 -- Is_Fully_Initialized_Variant --
15316 ----------------------------------
15317
15318 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
15319 Loc : constant Source_Ptr := Sloc (Typ);
15320 Constraints : constant List_Id := New_List;
15321 Components : constant Elist_Id := New_Elmt_List;
15322 Comp_Elmt : Elmt_Id;
15323 Comp_Id : Node_Id;
15324 Comp_List : Node_Id;
15325 Discr : Entity_Id;
15326 Discr_Val : Node_Id;
15327
15328 Report_Errors : Boolean;
15329 pragma Warnings (Off, Report_Errors);
15330
15331 begin
15332 if Serious_Errors_Detected > 0 then
15333 return False;
15334 end if;
15335
15336 if Is_Record_Type (Typ)
15337 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15338 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
15339 then
15340 Comp_List := Component_List (Type_Definition (Parent (Typ)));
15341
15342 Discr := First_Discriminant (Typ);
15343 while Present (Discr) loop
15344 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
15345 Discr_Val := Expression (Parent (Discr));
15346
15347 if Present (Discr_Val)
15348 and then Is_OK_Static_Expression (Discr_Val)
15349 then
15350 Append_To (Constraints,
15351 Make_Component_Association (Loc,
15352 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
15353 Expression => New_Copy (Discr_Val)));
15354 else
15355 return False;
15356 end if;
15357 else
15358 return False;
15359 end if;
15360
15361 Next_Discriminant (Discr);
15362 end loop;
15363
15364 Gather_Components
15365 (Typ => Typ,
15366 Comp_List => Comp_List,
15367 Governed_By => Constraints,
15368 Into => Components,
15369 Report_Errors => Report_Errors);
15370
15371 -- Check that each component present is fully initialized
15372
15373 Comp_Elmt := First_Elmt (Components);
15374 while Present (Comp_Elmt) loop
15375 Comp_Id := Node (Comp_Elmt);
15376
15377 if Ekind (Comp_Id) = E_Component
15378 and then (No (Parent (Comp_Id))
15379 or else No (Expression (Parent (Comp_Id))))
15380 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
15381 then
15382 return False;
15383 end if;
15384
15385 Next_Elmt (Comp_Elmt);
15386 end loop;
15387
15388 return True;
15389
15390 elsif Is_Private_Type (Typ) then
15391 declare
15392 U : constant Entity_Id := Underlying_Type (Typ);
15393
15394 begin
15395 if No (U) then
15396 return False;
15397 else
15398 return Is_Fully_Initialized_Variant (U);
15399 end if;
15400 end;
15401
15402 else
15403 return False;
15404 end if;
15405 end Is_Fully_Initialized_Variant;
15406
15407 ------------------------------------
15408 -- Is_Generic_Declaration_Or_Body --
15409 ------------------------------------
15410
15411 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
15412 Spec_Decl : Node_Id;
15413
15414 begin
15415 -- Package/subprogram body
15416
15417 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
15418 and then Present (Corresponding_Spec (Decl))
15419 then
15420 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
15421
15422 -- Package/subprogram body stub
15423
15424 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
15425 and then Present (Corresponding_Spec_Of_Stub (Decl))
15426 then
15427 Spec_Decl :=
15428 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
15429
15430 -- All other cases
15431
15432 else
15433 Spec_Decl := Decl;
15434 end if;
15435
15436 -- Rather than inspecting the defining entity of the spec declaration,
15437 -- look at its Nkind. This takes care of the case where the analysis of
15438 -- a generic body modifies the Ekind of its spec to allow for recursive
15439 -- calls.
15440
15441 return
15442 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
15443 N_Generic_Subprogram_Declaration);
15444 end Is_Generic_Declaration_Or_Body;
15445
15446 ---------------------------
15447 -- Is_Independent_Object --
15448 ---------------------------
15449
15450 function Is_Independent_Object (N : Node_Id) return Boolean is
15451 function Is_Independent_Object_Entity (Id : Entity_Id) return Boolean;
15452 -- Determine whether arbitrary entity Id denotes an object that is
15453 -- Independent.
15454
15455 function Prefix_Has_Independent_Components (P : Node_Id) return Boolean;
15456 -- Determine whether prefix P has independent components. This requires
15457 -- the presence of an Independent_Components aspect/pragma.
15458
15459 ------------------------------------
15460 -- Is_Independent_Object_Entity --
15461 ------------------------------------
15462
15463 function Is_Independent_Object_Entity (Id : Entity_Id) return Boolean is
15464 begin
15465 return
15466 Is_Object (Id)
15467 and then (Is_Independent (Id)
15468 or else
15469 Is_Independent (Etype (Id)));
15470 end Is_Independent_Object_Entity;
15471
15472 -------------------------------------
15473 -- Prefix_Has_Independent_Components --
15474 -------------------------------------
15475
15476 function Prefix_Has_Independent_Components (P : Node_Id) return Boolean
15477 is
15478 Typ : constant Entity_Id := Etype (P);
15479
15480 begin
15481 if Is_Access_Type (Typ) then
15482 return Has_Independent_Components (Designated_Type (Typ));
15483
15484 elsif Has_Independent_Components (Typ) then
15485 return True;
15486
15487 elsif Is_Entity_Name (P)
15488 and then Has_Independent_Components (Entity (P))
15489 then
15490 return True;
15491
15492 else
15493 return False;
15494 end if;
15495 end Prefix_Has_Independent_Components;
15496
15497 -- Start of processing for Is_Independent_Object
15498
15499 begin
15500 if Is_Entity_Name (N) then
15501 return Is_Independent_Object_Entity (Entity (N));
15502
15503 elsif Is_Independent (Etype (N)) then
15504 return True;
15505
15506 elsif Nkind (N) = N_Indexed_Component then
15507 return Prefix_Has_Independent_Components (Prefix (N));
15508
15509 elsif Nkind (N) = N_Selected_Component then
15510 return Prefix_Has_Independent_Components (Prefix (N))
15511 or else Is_Independent (Entity (Selector_Name (N)));
15512
15513 else
15514 return False;
15515 end if;
15516 end Is_Independent_Object;
15517
15518 ----------------------------
15519 -- Is_Inherited_Operation --
15520 ----------------------------
15521
15522 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
15523 pragma Assert (Is_Overloadable (E));
15524 Kind : constant Node_Kind := Nkind (Parent (E));
15525 begin
15526 return Kind = N_Full_Type_Declaration
15527 or else Kind = N_Private_Extension_Declaration
15528 or else Kind = N_Subtype_Declaration
15529 or else (Ekind (E) = E_Enumeration_Literal
15530 and then Is_Derived_Type (Etype (E)));
15531 end Is_Inherited_Operation;
15532
15533 -------------------------------------
15534 -- Is_Inherited_Operation_For_Type --
15535 -------------------------------------
15536
15537 function Is_Inherited_Operation_For_Type
15538 (E : Entity_Id;
15539 Typ : Entity_Id) return Boolean
15540 is
15541 begin
15542 -- Check that the operation has been created by the type declaration
15543
15544 return Is_Inherited_Operation (E)
15545 and then Defining_Identifier (Parent (E)) = Typ;
15546 end Is_Inherited_Operation_For_Type;
15547
15548 --------------------------------------
15549 -- Is_Inlinable_Expression_Function --
15550 --------------------------------------
15551
15552 function Is_Inlinable_Expression_Function
15553 (Subp : Entity_Id) return Boolean
15554 is
15555 Return_Expr : Node_Id;
15556
15557 begin
15558 if Is_Expression_Function_Or_Completion (Subp)
15559 and then Has_Pragma_Inline_Always (Subp)
15560 and then Needs_No_Actuals (Subp)
15561 and then No (Contract (Subp))
15562 and then not Is_Dispatching_Operation (Subp)
15563 and then Needs_Finalization (Etype (Subp))
15564 and then not Is_Class_Wide_Type (Etype (Subp))
15565 and then not (Has_Invariants (Etype (Subp)))
15566 and then Present (Subprogram_Body (Subp))
15567 and then Was_Expression_Function (Subprogram_Body (Subp))
15568 then
15569 Return_Expr := Expression_Of_Expression_Function (Subp);
15570
15571 -- The returned object must not have a qualified expression and its
15572 -- nominal subtype must be statically compatible with the result
15573 -- subtype of the expression function.
15574
15575 return
15576 Nkind (Return_Expr) = N_Identifier
15577 and then Etype (Return_Expr) = Etype (Subp);
15578 end if;
15579
15580 return False;
15581 end Is_Inlinable_Expression_Function;
15582
15583 -----------------
15584 -- Is_Iterator --
15585 -----------------
15586
15587 function Is_Iterator (Typ : Entity_Id) return Boolean is
15588 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean;
15589 -- Determine whether type Iter_Typ is a predefined forward or reversible
15590 -- iterator.
15591
15592 ----------------------
15593 -- Denotes_Iterator --
15594 ----------------------
15595
15596 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean is
15597 begin
15598 -- Check that the name matches, and that the ultimate ancestor is in
15599 -- a predefined unit, i.e the one that declares iterator interfaces.
15600
15601 return
15602 Nam_In (Chars (Iter_Typ), Name_Forward_Iterator,
15603 Name_Reversible_Iterator)
15604 and then In_Predefined_Unit (Root_Type (Iter_Typ));
15605 end Denotes_Iterator;
15606
15607 -- Local variables
15608
15609 Iface_Elmt : Elmt_Id;
15610 Ifaces : Elist_Id;
15611
15612 -- Start of processing for Is_Iterator
15613
15614 begin
15615 -- The type may be a subtype of a descendant of the proper instance of
15616 -- the predefined interface type, so we must use the root type of the
15617 -- given type. The same is done for Is_Reversible_Iterator.
15618
15619 if Is_Class_Wide_Type (Typ)
15620 and then Denotes_Iterator (Root_Type (Typ))
15621 then
15622 return True;
15623
15624 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
15625 return False;
15626
15627 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
15628 return True;
15629
15630 else
15631 Collect_Interfaces (Typ, Ifaces);
15632
15633 Iface_Elmt := First_Elmt (Ifaces);
15634 while Present (Iface_Elmt) loop
15635 if Denotes_Iterator (Node (Iface_Elmt)) then
15636 return True;
15637 end if;
15638
15639 Next_Elmt (Iface_Elmt);
15640 end loop;
15641
15642 return False;
15643 end if;
15644 end Is_Iterator;
15645
15646 ----------------------------
15647 -- Is_Iterator_Over_Array --
15648 ----------------------------
15649
15650 function Is_Iterator_Over_Array (N : Node_Id) return Boolean is
15651 Container : constant Node_Id := Name (N);
15652 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
15653 begin
15654 return Is_Array_Type (Container_Typ);
15655 end Is_Iterator_Over_Array;
15656
15657 ------------
15658 -- Is_LHS --
15659 ------------
15660
15661 -- We seem to have a lot of overlapping functions that do similar things
15662 -- (testing for left hand sides or lvalues???).
15663
15664 function Is_LHS (N : Node_Id) return Is_LHS_Result is
15665 P : constant Node_Id := Parent (N);
15666
15667 begin
15668 -- Return True if we are the left hand side of an assignment statement
15669
15670 if Nkind (P) = N_Assignment_Statement then
15671 if Name (P) = N then
15672 return Yes;
15673 else
15674 return No;
15675 end if;
15676
15677 -- Case of prefix of indexed or selected component or slice
15678
15679 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
15680 and then N = Prefix (P)
15681 then
15682 -- Here we have the case where the parent P is N.Q or N(Q .. R).
15683 -- If P is an LHS, then N is also effectively an LHS, but there
15684 -- is an important exception. If N is of an access type, then
15685 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
15686 -- case this makes N.all a left hand side but not N itself.
15687
15688 -- If we don't know the type yet, this is the case where we return
15689 -- Unknown, since the answer depends on the type which is unknown.
15690
15691 if No (Etype (N)) then
15692 return Unknown;
15693
15694 -- We have an Etype set, so we can check it
15695
15696 elsif Is_Access_Type (Etype (N)) then
15697 return No;
15698
15699 -- OK, not access type case, so just test whole expression
15700
15701 else
15702 return Is_LHS (P);
15703 end if;
15704
15705 -- All other cases are not left hand sides
15706
15707 else
15708 return No;
15709 end if;
15710 end Is_LHS;
15711
15712 -----------------------------
15713 -- Is_Library_Level_Entity --
15714 -----------------------------
15715
15716 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
15717 begin
15718 -- The following is a small optimization, and it also properly handles
15719 -- discriminals, which in task bodies might appear in expressions before
15720 -- the corresponding procedure has been created, and which therefore do
15721 -- not have an assigned scope.
15722
15723 if Is_Formal (E) then
15724 return False;
15725 end if;
15726
15727 -- Normal test is simply that the enclosing dynamic scope is Standard
15728
15729 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
15730 end Is_Library_Level_Entity;
15731
15732 --------------------------------
15733 -- Is_Limited_Class_Wide_Type --
15734 --------------------------------
15735
15736 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
15737 begin
15738 return
15739 Is_Class_Wide_Type (Typ)
15740 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
15741 end Is_Limited_Class_Wide_Type;
15742
15743 ---------------------------------
15744 -- Is_Local_Variable_Reference --
15745 ---------------------------------
15746
15747 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
15748 begin
15749 if not Is_Entity_Name (Expr) then
15750 return False;
15751
15752 else
15753 declare
15754 Ent : constant Entity_Id := Entity (Expr);
15755 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
15756 begin
15757 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
15758 return False;
15759 else
15760 return Present (Sub) and then Sub = Current_Subprogram;
15761 end if;
15762 end;
15763 end if;
15764 end Is_Local_Variable_Reference;
15765
15766 -----------------------
15767 -- Is_Name_Reference --
15768 -----------------------
15769
15770 function Is_Name_Reference (N : Node_Id) return Boolean is
15771 begin
15772 if Is_Entity_Name (N) then
15773 return Present (Entity (N)) and then Is_Object (Entity (N));
15774 end if;
15775
15776 case Nkind (N) is
15777 when N_Indexed_Component
15778 | N_Slice
15779 =>
15780 return
15781 Is_Name_Reference (Prefix (N))
15782 or else Is_Access_Type (Etype (Prefix (N)));
15783
15784 -- Attributes 'Input, 'Old and 'Result produce objects
15785
15786 when N_Attribute_Reference =>
15787 return
15788 Nam_In (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
15789
15790 when N_Selected_Component =>
15791 return
15792 Is_Name_Reference (Selector_Name (N))
15793 and then
15794 (Is_Name_Reference (Prefix (N))
15795 or else Is_Access_Type (Etype (Prefix (N))));
15796
15797 when N_Explicit_Dereference =>
15798 return True;
15799
15800 -- A view conversion of a tagged name is a name reference
15801
15802 when N_Type_Conversion =>
15803 return
15804 Is_Tagged_Type (Etype (Subtype_Mark (N)))
15805 and then Is_Tagged_Type (Etype (Expression (N)))
15806 and then Is_Name_Reference (Expression (N));
15807
15808 -- An unchecked type conversion is considered to be a name if the
15809 -- operand is a name (this construction arises only as a result of
15810 -- expansion activities).
15811
15812 when N_Unchecked_Type_Conversion =>
15813 return Is_Name_Reference (Expression (N));
15814
15815 when others =>
15816 return False;
15817 end case;
15818 end Is_Name_Reference;
15819
15820 ------------------------------------
15821 -- Is_Non_Preelaborable_Construct --
15822 ------------------------------------
15823
15824 function Is_Non_Preelaborable_Construct (N : Node_Id) return Boolean is
15825
15826 -- NOTE: the routines within Is_Non_Preelaborable_Construct are
15827 -- intentionally unnested to avoid deep indentation of code.
15828
15829 Non_Preelaborable : exception;
15830 -- This exception is raised when the construct violates preelaborability
15831 -- to terminate the recursion.
15832
15833 procedure Visit (Nod : Node_Id);
15834 -- Semantically inspect construct Nod to determine whether it violates
15835 -- preelaborability. This routine raises Non_Preelaborable.
15836
15837 procedure Visit_List (List : List_Id);
15838 pragma Inline (Visit_List);
15839 -- Invoke Visit on each element of list List. This routine raises
15840 -- Non_Preelaborable.
15841
15842 procedure Visit_Pragma (Prag : Node_Id);
15843 pragma Inline (Visit_Pragma);
15844 -- Semantically inspect pragma Prag to determine whether it violates
15845 -- preelaborability. This routine raises Non_Preelaborable.
15846
15847 procedure Visit_Subexpression (Expr : Node_Id);
15848 pragma Inline (Visit_Subexpression);
15849 -- Semantically inspect expression Expr to determine whether it violates
15850 -- preelaborability. This routine raises Non_Preelaborable.
15851
15852 -----------
15853 -- Visit --
15854 -----------
15855
15856 procedure Visit (Nod : Node_Id) is
15857 begin
15858 case Nkind (Nod) is
15859
15860 -- Declarations
15861
15862 when N_Component_Declaration =>
15863
15864 -- Defining_Identifier is left out because it is not relevant
15865 -- for preelaborability.
15866
15867 Visit (Component_Definition (Nod));
15868 Visit (Expression (Nod));
15869
15870 when N_Derived_Type_Definition =>
15871
15872 -- Interface_List is left out because it is not relevant for
15873 -- preelaborability.
15874
15875 Visit (Record_Extension_Part (Nod));
15876 Visit (Subtype_Indication (Nod));
15877
15878 when N_Entry_Declaration =>
15879
15880 -- A protected type with at leat one entry is not preelaborable
15881 -- while task types are never preelaborable. This renders entry
15882 -- declarations non-preelaborable.
15883
15884 raise Non_Preelaborable;
15885
15886 when N_Full_Type_Declaration =>
15887
15888 -- Defining_Identifier and Discriminant_Specifications are left
15889 -- out because they are not relevant for preelaborability.
15890
15891 Visit (Type_Definition (Nod));
15892
15893 when N_Function_Instantiation
15894 | N_Package_Instantiation
15895 | N_Procedure_Instantiation
15896 =>
15897 -- Defining_Unit_Name and Name are left out because they are
15898 -- not relevant for preelaborability.
15899
15900 Visit_List (Generic_Associations (Nod));
15901
15902 when N_Object_Declaration =>
15903
15904 -- Defining_Identifier is left out because it is not relevant
15905 -- for preelaborability.
15906
15907 Visit (Object_Definition (Nod));
15908
15909 if Has_Init_Expression (Nod) then
15910 Visit (Expression (Nod));
15911
15912 elsif not Has_Preelaborable_Initialization
15913 (Etype (Defining_Entity (Nod)))
15914 then
15915 raise Non_Preelaborable;
15916 end if;
15917
15918 when N_Private_Extension_Declaration
15919 | N_Subtype_Declaration
15920 =>
15921 -- Defining_Identifier, Discriminant_Specifications, and
15922 -- Interface_List are left out because they are not relevant
15923 -- for preelaborability.
15924
15925 Visit (Subtype_Indication (Nod));
15926
15927 when N_Protected_Type_Declaration
15928 | N_Single_Protected_Declaration
15929 =>
15930 -- Defining_Identifier, Discriminant_Specifications, and
15931 -- Interface_List are left out because they are not relevant
15932 -- for preelaborability.
15933
15934 Visit (Protected_Definition (Nod));
15935
15936 -- A [single] task type is never preelaborable
15937
15938 when N_Single_Task_Declaration
15939 | N_Task_Type_Declaration
15940 =>
15941 raise Non_Preelaborable;
15942
15943 -- Pragmas
15944
15945 when N_Pragma =>
15946 Visit_Pragma (Nod);
15947
15948 -- Statements
15949
15950 when N_Statement_Other_Than_Procedure_Call =>
15951 if Nkind (Nod) /= N_Null_Statement then
15952 raise Non_Preelaborable;
15953 end if;
15954
15955 -- Subexpressions
15956
15957 when N_Subexpr =>
15958 Visit_Subexpression (Nod);
15959
15960 -- Special
15961
15962 when N_Access_To_Object_Definition =>
15963 Visit (Subtype_Indication (Nod));
15964
15965 when N_Case_Expression_Alternative =>
15966 Visit (Expression (Nod));
15967 Visit_List (Discrete_Choices (Nod));
15968
15969 when N_Component_Definition =>
15970 Visit (Access_Definition (Nod));
15971 Visit (Subtype_Indication (Nod));
15972
15973 when N_Component_List =>
15974 Visit_List (Component_Items (Nod));
15975 Visit (Variant_Part (Nod));
15976
15977 when N_Constrained_Array_Definition =>
15978 Visit_List (Discrete_Subtype_Definitions (Nod));
15979 Visit (Component_Definition (Nod));
15980
15981 when N_Delta_Constraint
15982 | N_Digits_Constraint
15983 =>
15984 -- Delta_Expression and Digits_Expression are left out because
15985 -- they are not relevant for preelaborability.
15986
15987 Visit (Range_Constraint (Nod));
15988
15989 when N_Discriminant_Specification =>
15990
15991 -- Defining_Identifier and Expression are left out because they
15992 -- are not relevant for preelaborability.
15993
15994 Visit (Discriminant_Type (Nod));
15995
15996 when N_Generic_Association =>
15997
15998 -- Selector_Name is left out because it is not relevant for
15999 -- preelaborability.
16000
16001 Visit (Explicit_Generic_Actual_Parameter (Nod));
16002
16003 when N_Index_Or_Discriminant_Constraint =>
16004 Visit_List (Constraints (Nod));
16005
16006 when N_Iterator_Specification =>
16007
16008 -- Defining_Identifier is left out because it is not relevant
16009 -- for preelaborability.
16010
16011 Visit (Name (Nod));
16012 Visit (Subtype_Indication (Nod));
16013
16014 when N_Loop_Parameter_Specification =>
16015
16016 -- Defining_Identifier is left out because it is not relevant
16017 -- for preelaborability.
16018
16019 Visit (Discrete_Subtype_Definition (Nod));
16020
16021 when N_Protected_Definition =>
16022
16023 -- End_Label is left out because it is not relevant for
16024 -- preelaborability.
16025
16026 Visit_List (Private_Declarations (Nod));
16027 Visit_List (Visible_Declarations (Nod));
16028
16029 when N_Range_Constraint =>
16030 Visit (Range_Expression (Nod));
16031
16032 when N_Record_Definition
16033 | N_Variant
16034 =>
16035 -- End_Label, Discrete_Choices, and Interface_List are left out
16036 -- because they are not relevant for preelaborability.
16037
16038 Visit (Component_List (Nod));
16039
16040 when N_Subtype_Indication =>
16041
16042 -- Subtype_Mark is left out because it is not relevant for
16043 -- preelaborability.
16044
16045 Visit (Constraint (Nod));
16046
16047 when N_Unconstrained_Array_Definition =>
16048
16049 -- Subtype_Marks is left out because it is not relevant for
16050 -- preelaborability.
16051
16052 Visit (Component_Definition (Nod));
16053
16054 when N_Variant_Part =>
16055
16056 -- Name is left out because it is not relevant for
16057 -- preelaborability.
16058
16059 Visit_List (Variants (Nod));
16060
16061 -- Default
16062
16063 when others =>
16064 null;
16065 end case;
16066 end Visit;
16067
16068 ----------------
16069 -- Visit_List --
16070 ----------------
16071
16072 procedure Visit_List (List : List_Id) is
16073 Nod : Node_Id;
16074
16075 begin
16076 if Present (List) then
16077 Nod := First (List);
16078 while Present (Nod) loop
16079 Visit (Nod);
16080 Next (Nod);
16081 end loop;
16082 end if;
16083 end Visit_List;
16084
16085 ------------------
16086 -- Visit_Pragma --
16087 ------------------
16088
16089 procedure Visit_Pragma (Prag : Node_Id) is
16090 begin
16091 case Get_Pragma_Id (Prag) is
16092 when Pragma_Assert
16093 | Pragma_Assert_And_Cut
16094 | Pragma_Assume
16095 | Pragma_Async_Readers
16096 | Pragma_Async_Writers
16097 | Pragma_Attribute_Definition
16098 | Pragma_Check
16099 | Pragma_Constant_After_Elaboration
16100 | Pragma_CPU
16101 | Pragma_Deadline_Floor
16102 | Pragma_Dispatching_Domain
16103 | Pragma_Effective_Reads
16104 | Pragma_Effective_Writes
16105 | Pragma_Extensions_Visible
16106 | Pragma_Ghost
16107 | Pragma_Secondary_Stack_Size
16108 | Pragma_Task_Name
16109 | Pragma_Volatile_Function
16110 =>
16111 Visit_List (Pragma_Argument_Associations (Prag));
16112
16113 -- Default
16114
16115 when others =>
16116 null;
16117 end case;
16118 end Visit_Pragma;
16119
16120 -------------------------
16121 -- Visit_Subexpression --
16122 -------------------------
16123
16124 procedure Visit_Subexpression (Expr : Node_Id) is
16125 procedure Visit_Aggregate (Aggr : Node_Id);
16126 pragma Inline (Visit_Aggregate);
16127 -- Semantically inspect aggregate Aggr to determine whether it
16128 -- violates preelaborability.
16129
16130 ---------------------
16131 -- Visit_Aggregate --
16132 ---------------------
16133
16134 procedure Visit_Aggregate (Aggr : Node_Id) is
16135 begin
16136 if not Is_Preelaborable_Aggregate (Aggr) then
16137 raise Non_Preelaborable;
16138 end if;
16139 end Visit_Aggregate;
16140
16141 -- Start of processing for Visit_Subexpression
16142
16143 begin
16144 case Nkind (Expr) is
16145 when N_Allocator
16146 | N_Qualified_Expression
16147 | N_Type_Conversion
16148 | N_Unchecked_Expression
16149 | N_Unchecked_Type_Conversion
16150 =>
16151 -- Subpool_Handle_Name and Subtype_Mark are left out because
16152 -- they are not relevant for preelaborability.
16153
16154 Visit (Expression (Expr));
16155
16156 when N_Aggregate
16157 | N_Extension_Aggregate
16158 =>
16159 Visit_Aggregate (Expr);
16160
16161 when N_Attribute_Reference
16162 | N_Explicit_Dereference
16163 | N_Reference
16164 =>
16165 -- Attribute_Name and Expressions are left out because they are
16166 -- not relevant for preelaborability.
16167
16168 Visit (Prefix (Expr));
16169
16170 when N_Case_Expression =>
16171
16172 -- End_Span is left out because it is not relevant for
16173 -- preelaborability.
16174
16175 Visit_List (Alternatives (Expr));
16176 Visit (Expression (Expr));
16177
16178 when N_Delta_Aggregate =>
16179 Visit_Aggregate (Expr);
16180 Visit (Expression (Expr));
16181
16182 when N_Expression_With_Actions =>
16183 Visit_List (Actions (Expr));
16184 Visit (Expression (Expr));
16185
16186 when N_If_Expression =>
16187 Visit_List (Expressions (Expr));
16188
16189 when N_Quantified_Expression =>
16190 Visit (Condition (Expr));
16191 Visit (Iterator_Specification (Expr));
16192 Visit (Loop_Parameter_Specification (Expr));
16193
16194 when N_Range =>
16195 Visit (High_Bound (Expr));
16196 Visit (Low_Bound (Expr));
16197
16198 when N_Slice =>
16199 Visit (Discrete_Range (Expr));
16200 Visit (Prefix (Expr));
16201
16202 -- Default
16203
16204 when others =>
16205
16206 -- The evaluation of an object name is not preelaborable,
16207 -- unless the name is a static expression (checked further
16208 -- below), or statically denotes a discriminant.
16209
16210 if Is_Entity_Name (Expr) then
16211 Object_Name : declare
16212 Id : constant Entity_Id := Entity (Expr);
16213
16214 begin
16215 if Is_Object (Id) then
16216 if Ekind (Id) = E_Discriminant then
16217 null;
16218
16219 elsif Ekind_In (Id, E_Constant, E_In_Parameter)
16220 and then Present (Discriminal_Link (Id))
16221 then
16222 null;
16223
16224 else
16225 raise Non_Preelaborable;
16226 end if;
16227 end if;
16228 end Object_Name;
16229
16230 -- A non-static expression is not preelaborable
16231
16232 elsif not Is_OK_Static_Expression (Expr) then
16233 raise Non_Preelaborable;
16234 end if;
16235 end case;
16236 end Visit_Subexpression;
16237
16238 -- Start of processing for Is_Non_Preelaborable_Construct
16239
16240 begin
16241 Visit (N);
16242
16243 -- At this point it is known that the construct is preelaborable
16244
16245 return False;
16246
16247 exception
16248
16249 -- The elaboration of the construct performs an action which violates
16250 -- preelaborability.
16251
16252 when Non_Preelaborable =>
16253 return True;
16254 end Is_Non_Preelaborable_Construct;
16255
16256 ---------------------------------
16257 -- Is_Nontrivial_DIC_Procedure --
16258 ---------------------------------
16259
16260 function Is_Nontrivial_DIC_Procedure (Id : Entity_Id) return Boolean is
16261 Body_Decl : Node_Id;
16262 Stmt : Node_Id;
16263
16264 begin
16265 if Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id) then
16266 Body_Decl :=
16267 Unit_Declaration_Node
16268 (Corresponding_Body (Unit_Declaration_Node (Id)));
16269
16270 -- The body of the Default_Initial_Condition procedure must contain
16271 -- at least one statement, otherwise the generation of the subprogram
16272 -- body failed.
16273
16274 pragma Assert (Present (Handled_Statement_Sequence (Body_Decl)));
16275
16276 -- To qualify as nontrivial, the first statement of the procedure
16277 -- must be a check in the form of an if statement. If the original
16278 -- Default_Initial_Condition expression was folded, then the first
16279 -- statement is not a check.
16280
16281 Stmt := First (Statements (Handled_Statement_Sequence (Body_Decl)));
16282
16283 return
16284 Nkind (Stmt) = N_If_Statement
16285 and then Nkind (Original_Node (Stmt)) = N_Pragma;
16286 end if;
16287
16288 return False;
16289 end Is_Nontrivial_DIC_Procedure;
16290
16291 -------------------------
16292 -- Is_Null_Record_Type --
16293 -------------------------
16294
16295 function Is_Null_Record_Type (T : Entity_Id) return Boolean is
16296 Decl : constant Node_Id := Parent (T);
16297 begin
16298 return Nkind (Decl) = N_Full_Type_Declaration
16299 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
16300 and then
16301 (No (Component_List (Type_Definition (Decl)))
16302 or else Null_Present (Component_List (Type_Definition (Decl))));
16303 end Is_Null_Record_Type;
16304
16305 ---------------------
16306 -- Is_Object_Image --
16307 ---------------------
16308
16309 function Is_Object_Image (Prefix : Node_Id) return Boolean is
16310 begin
16311 -- When the type of the prefix is not scalar, then the prefix is not
16312 -- valid in any scenario.
16313
16314 if not Is_Scalar_Type (Etype (Prefix)) then
16315 return False;
16316 end if;
16317
16318 -- Here we test for the case that the prefix is not a type and assume
16319 -- if it is not then it must be a named value or an object reference.
16320 -- This is because the parser always checks that prefixes of attributes
16321 -- are named.
16322
16323 return not (Is_Entity_Name (Prefix) and then Is_Type (Entity (Prefix)));
16324 end Is_Object_Image;
16325
16326 -------------------------
16327 -- Is_Object_Reference --
16328 -------------------------
16329
16330 function Is_Object_Reference (N : Node_Id) return Boolean is
16331 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
16332 -- Determine whether N is the name of an internally-generated renaming
16333
16334 --------------------------------------
16335 -- Is_Internally_Generated_Renaming --
16336 --------------------------------------
16337
16338 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
16339 P : Node_Id;
16340
16341 begin
16342 P := N;
16343 while Present (P) loop
16344 if Nkind (P) = N_Object_Renaming_Declaration then
16345 return not Comes_From_Source (P);
16346 elsif Is_List_Member (P) then
16347 return False;
16348 end if;
16349
16350 P := Parent (P);
16351 end loop;
16352
16353 return False;
16354 end Is_Internally_Generated_Renaming;
16355
16356 -- Start of processing for Is_Object_Reference
16357
16358 begin
16359 if Is_Entity_Name (N) then
16360 return Present (Entity (N)) and then Is_Object (Entity (N));
16361
16362 else
16363 case Nkind (N) is
16364 when N_Indexed_Component
16365 | N_Slice
16366 =>
16367 return
16368 Is_Object_Reference (Prefix (N))
16369 or else Is_Access_Type (Etype (Prefix (N)));
16370
16371 -- In Ada 95, a function call is a constant object; a procedure
16372 -- call is not.
16373
16374 -- Note that predefined operators are functions as well, and so
16375 -- are attributes that are (can be renamed as) functions.
16376
16377 when N_Binary_Op
16378 | N_Function_Call
16379 | N_Unary_Op
16380 =>
16381 return Etype (N) /= Standard_Void_Type;
16382
16383 -- Attributes references 'Loop_Entry, 'Old, and 'Result yield
16384 -- objects, even though they are not functions.
16385
16386 when N_Attribute_Reference =>
16387 return
16388 Nam_In (Attribute_Name (N), Name_Loop_Entry,
16389 Name_Old,
16390 Name_Result)
16391 or else Is_Function_Attribute_Name (Attribute_Name (N));
16392
16393 when N_Selected_Component =>
16394 return
16395 Is_Object_Reference (Selector_Name (N))
16396 and then
16397 (Is_Object_Reference (Prefix (N))
16398 or else Is_Access_Type (Etype (Prefix (N))));
16399
16400 -- An explicit dereference denotes an object, except that a
16401 -- conditional expression gets turned into an explicit dereference
16402 -- in some cases, and conditional expressions are not object
16403 -- names.
16404
16405 when N_Explicit_Dereference =>
16406 return not Nkind_In (Original_Node (N), N_Case_Expression,
16407 N_If_Expression);
16408
16409 -- A view conversion of a tagged object is an object reference
16410
16411 when N_Type_Conversion =>
16412 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
16413 and then Is_Tagged_Type (Etype (Expression (N)))
16414 and then Is_Object_Reference (Expression (N));
16415
16416 -- An unchecked type conversion is considered to be an object if
16417 -- the operand is an object (this construction arises only as a
16418 -- result of expansion activities).
16419
16420 when N_Unchecked_Type_Conversion =>
16421 return True;
16422
16423 -- Allow string literals to act as objects as long as they appear
16424 -- in internally-generated renamings. The expansion of iterators
16425 -- may generate such renamings when the range involves a string
16426 -- literal.
16427
16428 when N_String_Literal =>
16429 return Is_Internally_Generated_Renaming (Parent (N));
16430
16431 -- AI05-0003: In Ada 2012 a qualified expression is a name.
16432 -- This allows disambiguation of function calls and the use
16433 -- of aggregates in more contexts.
16434
16435 when N_Qualified_Expression =>
16436 if Ada_Version < Ada_2012 then
16437 return False;
16438 else
16439 return Is_Object_Reference (Expression (N))
16440 or else Nkind (Expression (N)) = N_Aggregate;
16441 end if;
16442
16443 when others =>
16444 return False;
16445 end case;
16446 end if;
16447 end Is_Object_Reference;
16448
16449 -----------------------------------
16450 -- Is_OK_Variable_For_Out_Formal --
16451 -----------------------------------
16452
16453 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
16454 begin
16455 Note_Possible_Modification (AV, Sure => True);
16456
16457 -- We must reject parenthesized variable names. Comes_From_Source is
16458 -- checked because there are currently cases where the compiler violates
16459 -- this rule (e.g. passing a task object to its controlled Initialize
16460 -- routine). This should be properly documented in sinfo???
16461
16462 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
16463 return False;
16464
16465 -- A variable is always allowed
16466
16467 elsif Is_Variable (AV) then
16468 return True;
16469
16470 -- Generalized indexing operations are rewritten as explicit
16471 -- dereferences, and it is only during resolution that we can
16472 -- check whether the context requires an access_to_variable type.
16473
16474 elsif Nkind (AV) = N_Explicit_Dereference
16475 and then Ada_Version >= Ada_2012
16476 and then Nkind (Original_Node (AV)) = N_Indexed_Component
16477 and then Present (Etype (Original_Node (AV)))
16478 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
16479 then
16480 return not Is_Access_Constant (Etype (Prefix (AV)));
16481
16482 -- Unchecked conversions are allowed only if they come from the
16483 -- generated code, which sometimes uses unchecked conversions for out
16484 -- parameters in cases where code generation is unaffected. We tell
16485 -- source unchecked conversions by seeing if they are rewrites of
16486 -- an original Unchecked_Conversion function call, or of an explicit
16487 -- conversion of a function call or an aggregate (as may happen in the
16488 -- expansion of a packed array aggregate).
16489
16490 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
16491 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
16492 return False;
16493
16494 elsif Comes_From_Source (AV)
16495 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
16496 then
16497 return False;
16498
16499 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
16500 return Is_OK_Variable_For_Out_Formal (Expression (AV));
16501
16502 else
16503 return True;
16504 end if;
16505
16506 -- Normal type conversions are allowed if argument is a variable
16507
16508 elsif Nkind (AV) = N_Type_Conversion then
16509 if Is_Variable (Expression (AV))
16510 and then Paren_Count (Expression (AV)) = 0
16511 then
16512 Note_Possible_Modification (Expression (AV), Sure => True);
16513 return True;
16514
16515 -- We also allow a non-parenthesized expression that raises
16516 -- constraint error if it rewrites what used to be a variable
16517
16518 elsif Raises_Constraint_Error (Expression (AV))
16519 and then Paren_Count (Expression (AV)) = 0
16520 and then Is_Variable (Original_Node (Expression (AV)))
16521 then
16522 return True;
16523
16524 -- Type conversion of something other than a variable
16525
16526 else
16527 return False;
16528 end if;
16529
16530 -- If this node is rewritten, then test the original form, if that is
16531 -- OK, then we consider the rewritten node OK (for example, if the
16532 -- original node is a conversion, then Is_Variable will not be true
16533 -- but we still want to allow the conversion if it converts a variable).
16534
16535 elsif Is_Rewrite_Substitution (AV) then
16536
16537 -- In Ada 2012, the explicit dereference may be a rewritten call to a
16538 -- Reference function.
16539
16540 if Ada_Version >= Ada_2012
16541 and then Nkind (Original_Node (AV)) = N_Function_Call
16542 and then
16543 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
16544 then
16545
16546 -- Check that this is not a constant reference.
16547
16548 return not Is_Access_Constant (Etype (Prefix (AV)));
16549
16550 elsif Has_Implicit_Dereference (Etype (Original_Node (AV))) then
16551 return
16552 not Is_Access_Constant (Etype
16553 (Get_Reference_Discriminant (Etype (Original_Node (AV)))));
16554
16555 else
16556 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
16557 end if;
16558
16559 -- All other non-variables are rejected
16560
16561 else
16562 return False;
16563 end if;
16564 end Is_OK_Variable_For_Out_Formal;
16565
16566 ----------------------------
16567 -- Is_OK_Volatile_Context --
16568 ----------------------------
16569
16570 function Is_OK_Volatile_Context
16571 (Context : Node_Id;
16572 Obj_Ref : Node_Id) return Boolean
16573 is
16574 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
16575 -- Determine whether an arbitrary node denotes a call to a protected
16576 -- entry, function, or procedure in prefixed form where the prefix is
16577 -- Obj_Ref.
16578
16579 function Within_Check (Nod : Node_Id) return Boolean;
16580 -- Determine whether an arbitrary node appears in a check node
16581
16582 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
16583 -- Determine whether an arbitrary entity appears in a volatile function
16584
16585 ---------------------------------
16586 -- Is_Protected_Operation_Call --
16587 ---------------------------------
16588
16589 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
16590 Pref : Node_Id;
16591 Subp : Node_Id;
16592
16593 begin
16594 -- A call to a protected operations retains its selected component
16595 -- form as opposed to other prefixed calls that are transformed in
16596 -- expanded names.
16597
16598 if Nkind (Nod) = N_Selected_Component then
16599 Pref := Prefix (Nod);
16600 Subp := Selector_Name (Nod);
16601
16602 return
16603 Pref = Obj_Ref
16604 and then Present (Etype (Pref))
16605 and then Is_Protected_Type (Etype (Pref))
16606 and then Is_Entity_Name (Subp)
16607 and then Present (Entity (Subp))
16608 and then Ekind_In (Entity (Subp), E_Entry,
16609 E_Entry_Family,
16610 E_Function,
16611 E_Procedure);
16612 else
16613 return False;
16614 end if;
16615 end Is_Protected_Operation_Call;
16616
16617 ------------------
16618 -- Within_Check --
16619 ------------------
16620
16621 function Within_Check (Nod : Node_Id) return Boolean is
16622 Par : Node_Id;
16623
16624 begin
16625 -- Climb the parent chain looking for a check node
16626
16627 Par := Nod;
16628 while Present (Par) loop
16629 if Nkind (Par) in N_Raise_xxx_Error then
16630 return True;
16631
16632 -- Prevent the search from going too far
16633
16634 elsif Is_Body_Or_Package_Declaration (Par) then
16635 exit;
16636 end if;
16637
16638 Par := Parent (Par);
16639 end loop;
16640
16641 return False;
16642 end Within_Check;
16643
16644 ------------------------------
16645 -- Within_Volatile_Function --
16646 ------------------------------
16647
16648 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
16649 Func_Id : Entity_Id;
16650
16651 begin
16652 -- Traverse the scope stack looking for a [generic] function
16653
16654 Func_Id := Id;
16655 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
16656 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
16657 return Is_Volatile_Function (Func_Id);
16658 end if;
16659
16660 Func_Id := Scope (Func_Id);
16661 end loop;
16662
16663 return False;
16664 end Within_Volatile_Function;
16665
16666 -- Local variables
16667
16668 Obj_Id : Entity_Id;
16669
16670 -- Start of processing for Is_OK_Volatile_Context
16671
16672 begin
16673 -- The volatile object appears on either side of an assignment
16674
16675 if Nkind (Context) = N_Assignment_Statement then
16676 return True;
16677
16678 -- The volatile object is part of the initialization expression of
16679 -- another object.
16680
16681 elsif Nkind (Context) = N_Object_Declaration
16682 and then Present (Expression (Context))
16683 and then Expression (Context) = Obj_Ref
16684 then
16685 Obj_Id := Defining_Entity (Context);
16686
16687 -- The volatile object acts as the initialization expression of an
16688 -- extended return statement. This is valid context as long as the
16689 -- function is volatile.
16690
16691 if Is_Return_Object (Obj_Id) then
16692 return Within_Volatile_Function (Obj_Id);
16693
16694 -- Otherwise this is a normal object initialization
16695
16696 else
16697 return True;
16698 end if;
16699
16700 -- The volatile object acts as the name of a renaming declaration
16701
16702 elsif Nkind (Context) = N_Object_Renaming_Declaration
16703 and then Name (Context) = Obj_Ref
16704 then
16705 return True;
16706
16707 -- The volatile object appears as an actual parameter in a call to an
16708 -- instance of Unchecked_Conversion whose result is renamed.
16709
16710 elsif Nkind (Context) = N_Function_Call
16711 and then Is_Entity_Name (Name (Context))
16712 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
16713 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
16714 then
16715 return True;
16716
16717 -- The volatile object is actually the prefix in a protected entry,
16718 -- function, or procedure call.
16719
16720 elsif Is_Protected_Operation_Call (Context) then
16721 return True;
16722
16723 -- The volatile object appears as the expression of a simple return
16724 -- statement that applies to a volatile function.
16725
16726 elsif Nkind (Context) = N_Simple_Return_Statement
16727 and then Expression (Context) = Obj_Ref
16728 then
16729 return
16730 Within_Volatile_Function (Return_Statement_Entity (Context));
16731
16732 -- The volatile object appears as the prefix of a name occurring in a
16733 -- non-interfering context.
16734
16735 elsif Nkind_In (Context, N_Attribute_Reference,
16736 N_Explicit_Dereference,
16737 N_Indexed_Component,
16738 N_Selected_Component,
16739 N_Slice)
16740 and then Prefix (Context) = Obj_Ref
16741 and then Is_OK_Volatile_Context
16742 (Context => Parent (Context),
16743 Obj_Ref => Context)
16744 then
16745 return True;
16746
16747 -- The volatile object appears as the prefix of attributes Address,
16748 -- Alignment, Component_Size, First, First_Bit, Last, Last_Bit, Length,
16749 -- Position, Size, Storage_Size.
16750
16751 elsif Nkind (Context) = N_Attribute_Reference
16752 and then Prefix (Context) = Obj_Ref
16753 and then Nam_In (Attribute_Name (Context), Name_Address,
16754 Name_Alignment,
16755 Name_Component_Size,
16756 Name_First,
16757 Name_First_Bit,
16758 Name_Last,
16759 Name_Last_Bit,
16760 Name_Length,
16761 Name_Position,
16762 Name_Size,
16763 Name_Storage_Size)
16764 then
16765 return True;
16766
16767 -- The volatile object appears as the expression of a type conversion
16768 -- occurring in a non-interfering context.
16769
16770 elsif Nkind_In (Context, N_Type_Conversion,
16771 N_Unchecked_Type_Conversion)
16772 and then Expression (Context) = Obj_Ref
16773 and then Is_OK_Volatile_Context
16774 (Context => Parent (Context),
16775 Obj_Ref => Context)
16776 then
16777 return True;
16778
16779 -- The volatile object appears as the expression in a delay statement
16780
16781 elsif Nkind (Context) in N_Delay_Statement then
16782 return True;
16783
16784 -- Allow references to volatile objects in various checks. This is not a
16785 -- direct SPARK 2014 requirement.
16786
16787 elsif Within_Check (Context) then
16788 return True;
16789
16790 -- Assume that references to effectively volatile objects that appear
16791 -- as actual parameters in a subprogram call are always legal. A full
16792 -- legality check is done when the actuals are resolved (see routine
16793 -- Resolve_Actuals).
16794
16795 elsif Within_Subprogram_Call (Context) then
16796 return True;
16797
16798 -- Otherwise the context is not suitable for an effectively volatile
16799 -- object.
16800
16801 else
16802 return False;
16803 end if;
16804 end Is_OK_Volatile_Context;
16805
16806 ------------------------------------
16807 -- Is_Package_Contract_Annotation --
16808 ------------------------------------
16809
16810 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
16811 Nam : Name_Id;
16812
16813 begin
16814 if Nkind (Item) = N_Aspect_Specification then
16815 Nam := Chars (Identifier (Item));
16816
16817 else pragma Assert (Nkind (Item) = N_Pragma);
16818 Nam := Pragma_Name (Item);
16819 end if;
16820
16821 return Nam = Name_Abstract_State
16822 or else Nam = Name_Initial_Condition
16823 or else Nam = Name_Initializes
16824 or else Nam = Name_Refined_State;
16825 end Is_Package_Contract_Annotation;
16826
16827 -----------------------------------
16828 -- Is_Partially_Initialized_Type --
16829 -----------------------------------
16830
16831 function Is_Partially_Initialized_Type
16832 (Typ : Entity_Id;
16833 Include_Implicit : Boolean := True) return Boolean
16834 is
16835 begin
16836 if Is_Scalar_Type (Typ) then
16837 return False;
16838
16839 elsif Is_Access_Type (Typ) then
16840 return Include_Implicit;
16841
16842 elsif Is_Array_Type (Typ) then
16843
16844 -- If component type is partially initialized, so is array type
16845
16846 if Is_Partially_Initialized_Type
16847 (Component_Type (Typ), Include_Implicit)
16848 then
16849 return True;
16850
16851 -- Otherwise we are only partially initialized if we are fully
16852 -- initialized (this is the empty array case, no point in us
16853 -- duplicating that code here).
16854
16855 else
16856 return Is_Fully_Initialized_Type (Typ);
16857 end if;
16858
16859 elsif Is_Record_Type (Typ) then
16860
16861 -- A discriminated type is always partially initialized if in
16862 -- all mode
16863
16864 if Has_Discriminants (Typ) and then Include_Implicit then
16865 return True;
16866
16867 -- A tagged type is always partially initialized
16868
16869 elsif Is_Tagged_Type (Typ) then
16870 return True;
16871
16872 -- Case of non-discriminated record
16873
16874 else
16875 declare
16876 Ent : Entity_Id;
16877
16878 Component_Present : Boolean := False;
16879 -- Set True if at least one component is present. If no
16880 -- components are present, then record type is fully
16881 -- initialized (another odd case, like the null array).
16882
16883 begin
16884 -- Loop through components
16885
16886 Ent := First_Entity (Typ);
16887 while Present (Ent) loop
16888 if Ekind (Ent) = E_Component then
16889 Component_Present := True;
16890
16891 -- If a component has an initialization expression then
16892 -- the enclosing record type is partially initialized
16893
16894 if Present (Parent (Ent))
16895 and then Present (Expression (Parent (Ent)))
16896 then
16897 return True;
16898
16899 -- If a component is of a type which is itself partially
16900 -- initialized, then the enclosing record type is also.
16901
16902 elsif Is_Partially_Initialized_Type
16903 (Etype (Ent), Include_Implicit)
16904 then
16905 return True;
16906 end if;
16907 end if;
16908
16909 Next_Entity (Ent);
16910 end loop;
16911
16912 -- No initialized components found. If we found any components
16913 -- they were all uninitialized so the result is false.
16914
16915 if Component_Present then
16916 return False;
16917
16918 -- But if we found no components, then all the components are
16919 -- initialized so we consider the type to be initialized.
16920
16921 else
16922 return True;
16923 end if;
16924 end;
16925 end if;
16926
16927 -- Concurrent types are always fully initialized
16928
16929 elsif Is_Concurrent_Type (Typ) then
16930 return True;
16931
16932 -- For a private type, go to underlying type. If there is no underlying
16933 -- type then just assume this partially initialized. Not clear if this
16934 -- can happen in a non-error case, but no harm in testing for this.
16935
16936 elsif Is_Private_Type (Typ) then
16937 declare
16938 U : constant Entity_Id := Underlying_Type (Typ);
16939 begin
16940 if No (U) then
16941 return True;
16942 else
16943 return Is_Partially_Initialized_Type (U, Include_Implicit);
16944 end if;
16945 end;
16946
16947 -- For any other type (are there any?) assume partially initialized
16948
16949 else
16950 return True;
16951 end if;
16952 end Is_Partially_Initialized_Type;
16953
16954 ------------------------------------
16955 -- Is_Potentially_Persistent_Type --
16956 ------------------------------------
16957
16958 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
16959 Comp : Entity_Id;
16960 Indx : Node_Id;
16961
16962 begin
16963 -- For private type, test corresponding full type
16964
16965 if Is_Private_Type (T) then
16966 return Is_Potentially_Persistent_Type (Full_View (T));
16967
16968 -- Scalar types are potentially persistent
16969
16970 elsif Is_Scalar_Type (T) then
16971 return True;
16972
16973 -- Record type is potentially persistent if not tagged and the types of
16974 -- all it components are potentially persistent, and no component has
16975 -- an initialization expression.
16976
16977 elsif Is_Record_Type (T)
16978 and then not Is_Tagged_Type (T)
16979 and then not Is_Partially_Initialized_Type (T)
16980 then
16981 Comp := First_Component (T);
16982 while Present (Comp) loop
16983 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
16984 return False;
16985 else
16986 Next_Entity (Comp);
16987 end if;
16988 end loop;
16989
16990 return True;
16991
16992 -- Array type is potentially persistent if its component type is
16993 -- potentially persistent and if all its constraints are static.
16994
16995 elsif Is_Array_Type (T) then
16996 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
16997 return False;
16998 end if;
16999
17000 Indx := First_Index (T);
17001 while Present (Indx) loop
17002 if not Is_OK_Static_Subtype (Etype (Indx)) then
17003 return False;
17004 else
17005 Next_Index (Indx);
17006 end if;
17007 end loop;
17008
17009 return True;
17010
17011 -- All other types are not potentially persistent
17012
17013 else
17014 return False;
17015 end if;
17016 end Is_Potentially_Persistent_Type;
17017
17018 --------------------------------
17019 -- Is_Potentially_Unevaluated --
17020 --------------------------------
17021
17022 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
17023 Par : Node_Id;
17024 Expr : Node_Id;
17025
17026 begin
17027 Expr := N;
17028 Par := N;
17029
17030 -- A postcondition whose expression is a short-circuit is broken down
17031 -- into individual aspects for better exception reporting. The original
17032 -- short-circuit expression is rewritten as the second operand, and an
17033 -- occurrence of 'Old in that operand is potentially unevaluated.
17034 -- See sem_ch13.adb for details of this transformation. The reference
17035 -- to 'Old may appear within an expression, so we must look for the
17036 -- enclosing pragma argument in the tree that contains the reference.
17037
17038 while Present (Par)
17039 and then Nkind (Par) /= N_Pragma_Argument_Association
17040 loop
17041 if Is_Rewrite_Substitution (Par)
17042 and then Nkind (Original_Node (Par)) = N_And_Then
17043 then
17044 return True;
17045 end if;
17046
17047 Par := Parent (Par);
17048 end loop;
17049
17050 -- Other cases; 'Old appears within other expression (not the top-level
17051 -- conjunct in a postcondition) with a potentially unevaluated operand.
17052
17053 Par := Parent (Expr);
17054 while not Nkind_In (Par, N_And_Then,
17055 N_Case_Expression,
17056 N_If_Expression,
17057 N_In,
17058 N_Not_In,
17059 N_Or_Else,
17060 N_Quantified_Expression)
17061 loop
17062 Expr := Par;
17063 Par := Parent (Par);
17064
17065 -- If the context is not an expression, or if is the result of
17066 -- expansion of an enclosing construct (such as another attribute)
17067 -- the predicate does not apply.
17068
17069 if Nkind (Par) = N_Case_Expression_Alternative then
17070 null;
17071
17072 elsif Nkind (Par) not in N_Subexpr
17073 or else not Comes_From_Source (Par)
17074 then
17075 return False;
17076 end if;
17077 end loop;
17078
17079 if Nkind (Par) = N_If_Expression then
17080 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
17081
17082 elsif Nkind (Par) = N_Case_Expression then
17083 return Expr /= Expression (Par);
17084
17085 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
17086 return Expr = Right_Opnd (Par);
17087
17088 elsif Nkind_In (Par, N_In, N_Not_In) then
17089
17090 -- If the membership includes several alternatives, only the first is
17091 -- definitely evaluated.
17092
17093 if Present (Alternatives (Par)) then
17094 return Expr /= First (Alternatives (Par));
17095
17096 -- If this is a range membership both bounds are evaluated
17097
17098 else
17099 return False;
17100 end if;
17101
17102 elsif Nkind (Par) = N_Quantified_Expression then
17103 return Expr = Condition (Par);
17104
17105 else
17106 return False;
17107 end if;
17108 end Is_Potentially_Unevaluated;
17109
17110 -----------------------------------------
17111 -- Is_Predefined_Dispatching_Operation --
17112 -----------------------------------------
17113
17114 function Is_Predefined_Dispatching_Operation
17115 (E : Entity_Id) return Boolean
17116 is
17117 TSS_Name : TSS_Name_Type;
17118
17119 begin
17120 if not Is_Dispatching_Operation (E) then
17121 return False;
17122 end if;
17123
17124 Get_Name_String (Chars (E));
17125
17126 -- Most predefined primitives have internally generated names. Equality
17127 -- must be treated differently; the predefined operation is recognized
17128 -- as a homogeneous binary operator that returns Boolean.
17129
17130 if Name_Len > TSS_Name_Type'Last then
17131 TSS_Name :=
17132 TSS_Name_Type
17133 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
17134
17135 if Nam_In (Chars (E), Name_uAssign, Name_uSize)
17136 or else
17137 (Chars (E) = Name_Op_Eq
17138 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
17139 or else TSS_Name = TSS_Deep_Adjust
17140 or else TSS_Name = TSS_Deep_Finalize
17141 or else TSS_Name = TSS_Stream_Input
17142 or else TSS_Name = TSS_Stream_Output
17143 or else TSS_Name = TSS_Stream_Read
17144 or else TSS_Name = TSS_Stream_Write
17145 or else Is_Predefined_Interface_Primitive (E)
17146 then
17147 return True;
17148 end if;
17149 end if;
17150
17151 return False;
17152 end Is_Predefined_Dispatching_Operation;
17153
17154 ---------------------------------------
17155 -- Is_Predefined_Interface_Primitive --
17156 ---------------------------------------
17157
17158 function Is_Predefined_Interface_Primitive (E : Entity_Id) return Boolean is
17159 begin
17160 -- In VM targets we don't restrict the functionality of this test to
17161 -- compiling in Ada 2005 mode since in VM targets any tagged type has
17162 -- these primitives.
17163
17164 return (Ada_Version >= Ada_2005 or else not Tagged_Type_Expansion)
17165 and then Nam_In (Chars (E), Name_uDisp_Asynchronous_Select,
17166 Name_uDisp_Conditional_Select,
17167 Name_uDisp_Get_Prim_Op_Kind,
17168 Name_uDisp_Get_Task_Id,
17169 Name_uDisp_Requeue,
17170 Name_uDisp_Timed_Select);
17171 end Is_Predefined_Interface_Primitive;
17172
17173 ---------------------------------------
17174 -- Is_Predefined_Internal_Operation --
17175 ---------------------------------------
17176
17177 function Is_Predefined_Internal_Operation
17178 (E : Entity_Id) return Boolean
17179 is
17180 TSS_Name : TSS_Name_Type;
17181
17182 begin
17183 if not Is_Dispatching_Operation (E) then
17184 return False;
17185 end if;
17186
17187 Get_Name_String (Chars (E));
17188
17189 -- Most predefined primitives have internally generated names. Equality
17190 -- must be treated differently; the predefined operation is recognized
17191 -- as a homogeneous binary operator that returns Boolean.
17192
17193 if Name_Len > TSS_Name_Type'Last then
17194 TSS_Name :=
17195 TSS_Name_Type
17196 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
17197
17198 if Nam_In (Chars (E), Name_uSize, Name_uAssign)
17199 or else
17200 (Chars (E) = Name_Op_Eq
17201 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
17202 or else TSS_Name = TSS_Deep_Adjust
17203 or else TSS_Name = TSS_Deep_Finalize
17204 or else Is_Predefined_Interface_Primitive (E)
17205 then
17206 return True;
17207 end if;
17208 end if;
17209
17210 return False;
17211 end Is_Predefined_Internal_Operation;
17212
17213 --------------------------------
17214 -- Is_Preelaborable_Aggregate --
17215 --------------------------------
17216
17217 function Is_Preelaborable_Aggregate (Aggr : Node_Id) return Boolean is
17218 Aggr_Typ : constant Entity_Id := Etype (Aggr);
17219 Array_Aggr : constant Boolean := Is_Array_Type (Aggr_Typ);
17220
17221 Anc_Part : Node_Id;
17222 Assoc : Node_Id;
17223 Choice : Node_Id;
17224 Comp_Typ : Entity_Id := Empty; -- init to avoid warning
17225 Expr : Node_Id;
17226
17227 begin
17228 if Array_Aggr then
17229 Comp_Typ := Component_Type (Aggr_Typ);
17230 end if;
17231
17232 -- Inspect the ancestor part
17233
17234 if Nkind (Aggr) = N_Extension_Aggregate then
17235 Anc_Part := Ancestor_Part (Aggr);
17236
17237 -- The ancestor denotes a subtype mark
17238
17239 if Is_Entity_Name (Anc_Part)
17240 and then Is_Type (Entity (Anc_Part))
17241 then
17242 if not Has_Preelaborable_Initialization (Entity (Anc_Part)) then
17243 return False;
17244 end if;
17245
17246 -- Otherwise the ancestor denotes an expression
17247
17248 elsif not Is_Preelaborable_Construct (Anc_Part) then
17249 return False;
17250 end if;
17251 end if;
17252
17253 -- Inspect the positional associations
17254
17255 Expr := First (Expressions (Aggr));
17256 while Present (Expr) loop
17257 if not Is_Preelaborable_Construct (Expr) then
17258 return False;
17259 end if;
17260
17261 Next (Expr);
17262 end loop;
17263
17264 -- Inspect the named associations
17265
17266 Assoc := First (Component_Associations (Aggr));
17267 while Present (Assoc) loop
17268
17269 -- Inspect the choices of the current named association
17270
17271 Choice := First (Choices (Assoc));
17272 while Present (Choice) loop
17273 if Array_Aggr then
17274
17275 -- For a choice to be preelaborable, it must denote either a
17276 -- static range or a static expression.
17277
17278 if Nkind (Choice) = N_Others_Choice then
17279 null;
17280
17281 elsif Nkind (Choice) = N_Range then
17282 if not Is_OK_Static_Range (Choice) then
17283 return False;
17284 end if;
17285
17286 elsif not Is_OK_Static_Expression (Choice) then
17287 return False;
17288 end if;
17289
17290 else
17291 Comp_Typ := Etype (Choice);
17292 end if;
17293
17294 Next (Choice);
17295 end loop;
17296
17297 -- The type of the choice must have preelaborable initialization if
17298 -- the association carries a <>.
17299
17300 pragma Assert (Present (Comp_Typ));
17301 if Box_Present (Assoc) then
17302 if not Has_Preelaborable_Initialization (Comp_Typ) then
17303 return False;
17304 end if;
17305
17306 -- The type of the expression must have preelaborable initialization
17307
17308 elsif not Is_Preelaborable_Construct (Expression (Assoc)) then
17309 return False;
17310 end if;
17311
17312 Next (Assoc);
17313 end loop;
17314
17315 -- At this point the aggregate is preelaborable
17316
17317 return True;
17318 end Is_Preelaborable_Aggregate;
17319
17320 --------------------------------
17321 -- Is_Preelaborable_Construct --
17322 --------------------------------
17323
17324 function Is_Preelaborable_Construct (N : Node_Id) return Boolean is
17325 begin
17326 -- Aggregates
17327
17328 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
17329 return Is_Preelaborable_Aggregate (N);
17330
17331 -- Attributes are allowed in general, even if their prefix is a formal
17332 -- type. It seems that certain attributes known not to be static might
17333 -- not be allowed, but there are no rules to prevent them.
17334
17335 elsif Nkind (N) = N_Attribute_Reference then
17336 return True;
17337
17338 -- Expressions
17339
17340 elsif Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
17341 return True;
17342
17343 elsif Nkind (N) = N_Qualified_Expression then
17344 return Is_Preelaborable_Construct (Expression (N));
17345
17346 -- Names are preelaborable when they denote a discriminant of an
17347 -- enclosing type. Discriminals are also considered for this check.
17348
17349 elsif Is_Entity_Name (N)
17350 and then Present (Entity (N))
17351 and then
17352 (Ekind (Entity (N)) = E_Discriminant
17353 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
17354 and then Present (Discriminal_Link (Entity (N)))))
17355 then
17356 return True;
17357
17358 -- Statements
17359
17360 elsif Nkind (N) = N_Null then
17361 return True;
17362
17363 -- Otherwise the construct is not preelaborable
17364
17365 else
17366 return False;
17367 end if;
17368 end Is_Preelaborable_Construct;
17369
17370 ---------------------------------
17371 -- Is_Protected_Self_Reference --
17372 ---------------------------------
17373
17374 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
17375
17376 function In_Access_Definition (N : Node_Id) return Boolean;
17377 -- Returns true if N belongs to an access definition
17378
17379 --------------------------
17380 -- In_Access_Definition --
17381 --------------------------
17382
17383 function In_Access_Definition (N : Node_Id) return Boolean is
17384 P : Node_Id;
17385
17386 begin
17387 P := Parent (N);
17388 while Present (P) loop
17389 if Nkind (P) = N_Access_Definition then
17390 return True;
17391 end if;
17392
17393 P := Parent (P);
17394 end loop;
17395
17396 return False;
17397 end In_Access_Definition;
17398
17399 -- Start of processing for Is_Protected_Self_Reference
17400
17401 begin
17402 -- Verify that prefix is analyzed and has the proper form. Note that
17403 -- the attributes Elab_Spec, Elab_Body, and Elab_Subp_Body, which also
17404 -- produce the address of an entity, do not analyze their prefix
17405 -- because they denote entities that are not necessarily visible.
17406 -- Neither of them can apply to a protected type.
17407
17408 return Ada_Version >= Ada_2005
17409 and then Is_Entity_Name (N)
17410 and then Present (Entity (N))
17411 and then Is_Protected_Type (Entity (N))
17412 and then In_Open_Scopes (Entity (N))
17413 and then not In_Access_Definition (N);
17414 end Is_Protected_Self_Reference;
17415
17416 -----------------------------
17417 -- Is_RCI_Pkg_Spec_Or_Body --
17418 -----------------------------
17419
17420 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
17421
17422 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
17423 -- Return True if the unit of Cunit is an RCI package declaration
17424
17425 ---------------------------
17426 -- Is_RCI_Pkg_Decl_Cunit --
17427 ---------------------------
17428
17429 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
17430 The_Unit : constant Node_Id := Unit (Cunit);
17431
17432 begin
17433 if Nkind (The_Unit) /= N_Package_Declaration then
17434 return False;
17435 end if;
17436
17437 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
17438 end Is_RCI_Pkg_Decl_Cunit;
17439
17440 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
17441
17442 begin
17443 return Is_RCI_Pkg_Decl_Cunit (Cunit)
17444 or else
17445 (Nkind (Unit (Cunit)) = N_Package_Body
17446 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
17447 end Is_RCI_Pkg_Spec_Or_Body;
17448
17449 -----------------------------------------
17450 -- Is_Remote_Access_To_Class_Wide_Type --
17451 -----------------------------------------
17452
17453 function Is_Remote_Access_To_Class_Wide_Type
17454 (E : Entity_Id) return Boolean
17455 is
17456 begin
17457 -- A remote access to class-wide type is a general access to object type
17458 -- declared in the visible part of a Remote_Types or Remote_Call_
17459 -- Interface unit.
17460
17461 return Ekind (E) = E_General_Access_Type
17462 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
17463 end Is_Remote_Access_To_Class_Wide_Type;
17464
17465 -----------------------------------------
17466 -- Is_Remote_Access_To_Subprogram_Type --
17467 -----------------------------------------
17468
17469 function Is_Remote_Access_To_Subprogram_Type
17470 (E : Entity_Id) return Boolean
17471 is
17472 begin
17473 return (Ekind (E) = E_Access_Subprogram_Type
17474 or else (Ekind (E) = E_Record_Type
17475 and then Present (Corresponding_Remote_Type (E))))
17476 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
17477 end Is_Remote_Access_To_Subprogram_Type;
17478
17479 --------------------
17480 -- Is_Remote_Call --
17481 --------------------
17482
17483 function Is_Remote_Call (N : Node_Id) return Boolean is
17484 begin
17485 if Nkind (N) not in N_Subprogram_Call then
17486
17487 -- An entry call cannot be remote
17488
17489 return False;
17490
17491 elsif Nkind (Name (N)) in N_Has_Entity
17492 and then Is_Remote_Call_Interface (Entity (Name (N)))
17493 then
17494 -- A subprogram declared in the spec of a RCI package is remote
17495
17496 return True;
17497
17498 elsif Nkind (Name (N)) = N_Explicit_Dereference
17499 and then Is_Remote_Access_To_Subprogram_Type
17500 (Etype (Prefix (Name (N))))
17501 then
17502 -- The dereference of a RAS is a remote call
17503
17504 return True;
17505
17506 elsif Present (Controlling_Argument (N))
17507 and then Is_Remote_Access_To_Class_Wide_Type
17508 (Etype (Controlling_Argument (N)))
17509 then
17510 -- Any primitive operation call with a controlling argument of
17511 -- a RACW type is a remote call.
17512
17513 return True;
17514 end if;
17515
17516 -- All other calls are local calls
17517
17518 return False;
17519 end Is_Remote_Call;
17520
17521 ----------------------
17522 -- Is_Renamed_Entry --
17523 ----------------------
17524
17525 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
17526 Orig_Node : Node_Id := Empty;
17527 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
17528
17529 function Is_Entry (Nam : Node_Id) return Boolean;
17530 -- Determine whether Nam is an entry. Traverse selectors if there are
17531 -- nested selected components.
17532
17533 --------------
17534 -- Is_Entry --
17535 --------------
17536
17537 function Is_Entry (Nam : Node_Id) return Boolean is
17538 begin
17539 if Nkind (Nam) = N_Selected_Component then
17540 return Is_Entry (Selector_Name (Nam));
17541 end if;
17542
17543 return Ekind (Entity (Nam)) = E_Entry;
17544 end Is_Entry;
17545
17546 -- Start of processing for Is_Renamed_Entry
17547
17548 begin
17549 if Present (Alias (Proc_Nam)) then
17550 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
17551 end if;
17552
17553 -- Look for a rewritten subprogram renaming declaration
17554
17555 if Nkind (Subp_Decl) = N_Subprogram_Declaration
17556 and then Present (Original_Node (Subp_Decl))
17557 then
17558 Orig_Node := Original_Node (Subp_Decl);
17559 end if;
17560
17561 -- The rewritten subprogram is actually an entry
17562
17563 if Present (Orig_Node)
17564 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
17565 and then Is_Entry (Name (Orig_Node))
17566 then
17567 return True;
17568 end if;
17569
17570 return False;
17571 end Is_Renamed_Entry;
17572
17573 -----------------------------
17574 -- Is_Renaming_Declaration --
17575 -----------------------------
17576
17577 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
17578 begin
17579 case Nkind (N) is
17580 when N_Exception_Renaming_Declaration
17581 | N_Generic_Function_Renaming_Declaration
17582 | N_Generic_Package_Renaming_Declaration
17583 | N_Generic_Procedure_Renaming_Declaration
17584 | N_Object_Renaming_Declaration
17585 | N_Package_Renaming_Declaration
17586 | N_Subprogram_Renaming_Declaration
17587 =>
17588 return True;
17589
17590 when others =>
17591 return False;
17592 end case;
17593 end Is_Renaming_Declaration;
17594
17595 ----------------------------
17596 -- Is_Reversible_Iterator --
17597 ----------------------------
17598
17599 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
17600 Ifaces_List : Elist_Id;
17601 Iface_Elmt : Elmt_Id;
17602 Iface : Entity_Id;
17603
17604 begin
17605 if Is_Class_Wide_Type (Typ)
17606 and then Chars (Root_Type (Typ)) = Name_Reversible_Iterator
17607 and then In_Predefined_Unit (Root_Type (Typ))
17608 then
17609 return True;
17610
17611 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
17612 return False;
17613
17614 else
17615 Collect_Interfaces (Typ, Ifaces_List);
17616
17617 Iface_Elmt := First_Elmt (Ifaces_List);
17618 while Present (Iface_Elmt) loop
17619 Iface := Node (Iface_Elmt);
17620 if Chars (Iface) = Name_Reversible_Iterator
17621 and then In_Predefined_Unit (Iface)
17622 then
17623 return True;
17624 end if;
17625
17626 Next_Elmt (Iface_Elmt);
17627 end loop;
17628 end if;
17629
17630 return False;
17631 end Is_Reversible_Iterator;
17632
17633 ----------------------
17634 -- Is_Selector_Name --
17635 ----------------------
17636
17637 function Is_Selector_Name (N : Node_Id) return Boolean is
17638 begin
17639 if not Is_List_Member (N) then
17640 declare
17641 P : constant Node_Id := Parent (N);
17642 begin
17643 return Nkind_In (P, N_Expanded_Name,
17644 N_Generic_Association,
17645 N_Parameter_Association,
17646 N_Selected_Component)
17647 and then Selector_Name (P) = N;
17648 end;
17649
17650 else
17651 declare
17652 L : constant List_Id := List_Containing (N);
17653 P : constant Node_Id := Parent (L);
17654 begin
17655 return (Nkind (P) = N_Discriminant_Association
17656 and then Selector_Names (P) = L)
17657 or else
17658 (Nkind (P) = N_Component_Association
17659 and then Choices (P) = L);
17660 end;
17661 end if;
17662 end Is_Selector_Name;
17663
17664 ---------------------------------
17665 -- Is_Single_Concurrent_Object --
17666 ---------------------------------
17667
17668 function Is_Single_Concurrent_Object (Id : Entity_Id) return Boolean is
17669 begin
17670 return
17671 Is_Single_Protected_Object (Id) or else Is_Single_Task_Object (Id);
17672 end Is_Single_Concurrent_Object;
17673
17674 -------------------------------
17675 -- Is_Single_Concurrent_Type --
17676 -------------------------------
17677
17678 function Is_Single_Concurrent_Type (Id : Entity_Id) return Boolean is
17679 begin
17680 return
17681 Ekind_In (Id, E_Protected_Type, E_Task_Type)
17682 and then Is_Single_Concurrent_Type_Declaration
17683 (Declaration_Node (Id));
17684 end Is_Single_Concurrent_Type;
17685
17686 -------------------------------------------
17687 -- Is_Single_Concurrent_Type_Declaration --
17688 -------------------------------------------
17689
17690 function Is_Single_Concurrent_Type_Declaration
17691 (N : Node_Id) return Boolean
17692 is
17693 begin
17694 return Nkind_In (Original_Node (N), N_Single_Protected_Declaration,
17695 N_Single_Task_Declaration);
17696 end Is_Single_Concurrent_Type_Declaration;
17697
17698 ---------------------------------------------
17699 -- Is_Single_Precision_Floating_Point_Type --
17700 ---------------------------------------------
17701
17702 function Is_Single_Precision_Floating_Point_Type
17703 (E : Entity_Id) return Boolean is
17704 begin
17705 return Is_Floating_Point_Type (E)
17706 and then Machine_Radix_Value (E) = Uint_2
17707 and then Machine_Mantissa_Value (E) = Uint_24
17708 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
17709 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
17710 end Is_Single_Precision_Floating_Point_Type;
17711
17712 --------------------------------
17713 -- Is_Single_Protected_Object --
17714 --------------------------------
17715
17716 function Is_Single_Protected_Object (Id : Entity_Id) return Boolean is
17717 begin
17718 return
17719 Ekind (Id) = E_Variable
17720 and then Ekind (Etype (Id)) = E_Protected_Type
17721 and then Is_Single_Concurrent_Type (Etype (Id));
17722 end Is_Single_Protected_Object;
17723
17724 ---------------------------
17725 -- Is_Single_Task_Object --
17726 ---------------------------
17727
17728 function Is_Single_Task_Object (Id : Entity_Id) return Boolean is
17729 begin
17730 return
17731 Ekind (Id) = E_Variable
17732 and then Ekind (Etype (Id)) = E_Task_Type
17733 and then Is_Single_Concurrent_Type (Etype (Id));
17734 end Is_Single_Task_Object;
17735
17736 -------------------------------------
17737 -- Is_SPARK_05_Initialization_Expr --
17738 -------------------------------------
17739
17740 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
17741 Is_Ok : Boolean;
17742 Expr : Node_Id;
17743 Comp_Assn : Node_Id;
17744 Orig_N : constant Node_Id := Original_Node (N);
17745
17746 begin
17747 Is_Ok := True;
17748
17749 if not Comes_From_Source (Orig_N) then
17750 goto Done;
17751 end if;
17752
17753 pragma Assert (Nkind (Orig_N) in N_Subexpr);
17754
17755 case Nkind (Orig_N) is
17756 when N_Character_Literal
17757 | N_Integer_Literal
17758 | N_Real_Literal
17759 | N_String_Literal
17760 =>
17761 null;
17762
17763 when N_Expanded_Name
17764 | N_Identifier
17765 =>
17766 if Is_Entity_Name (Orig_N)
17767 and then Present (Entity (Orig_N)) -- needed in some cases
17768 then
17769 case Ekind (Entity (Orig_N)) is
17770 when E_Constant
17771 | E_Enumeration_Literal
17772 | E_Named_Integer
17773 | E_Named_Real
17774 =>
17775 null;
17776
17777 when others =>
17778 if Is_Type (Entity (Orig_N)) then
17779 null;
17780 else
17781 Is_Ok := False;
17782 end if;
17783 end case;
17784 end if;
17785
17786 when N_Qualified_Expression
17787 | N_Type_Conversion
17788 =>
17789 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
17790
17791 when N_Unary_Op =>
17792 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
17793
17794 when N_Binary_Op
17795 | N_Membership_Test
17796 | N_Short_Circuit
17797 =>
17798 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
17799 and then
17800 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
17801
17802 when N_Aggregate
17803 | N_Extension_Aggregate
17804 =>
17805 if Nkind (Orig_N) = N_Extension_Aggregate then
17806 Is_Ok :=
17807 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
17808 end if;
17809
17810 Expr := First (Expressions (Orig_N));
17811 while Present (Expr) loop
17812 if not Is_SPARK_05_Initialization_Expr (Expr) then
17813 Is_Ok := False;
17814 goto Done;
17815 end if;
17816
17817 Next (Expr);
17818 end loop;
17819
17820 Comp_Assn := First (Component_Associations (Orig_N));
17821 while Present (Comp_Assn) loop
17822 Expr := Expression (Comp_Assn);
17823
17824 -- Note: test for Present here needed for box assocation
17825
17826 if Present (Expr)
17827 and then not Is_SPARK_05_Initialization_Expr (Expr)
17828 then
17829 Is_Ok := False;
17830 goto Done;
17831 end if;
17832
17833 Next (Comp_Assn);
17834 end loop;
17835
17836 when N_Attribute_Reference =>
17837 if Nkind (Prefix (Orig_N)) in N_Subexpr then
17838 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
17839 end if;
17840
17841 Expr := First (Expressions (Orig_N));
17842 while Present (Expr) loop
17843 if not Is_SPARK_05_Initialization_Expr (Expr) then
17844 Is_Ok := False;
17845 goto Done;
17846 end if;
17847
17848 Next (Expr);
17849 end loop;
17850
17851 -- Selected components might be expanded named not yet resolved, so
17852 -- default on the safe side. (Eg on sparklex.ads)
17853
17854 when N_Selected_Component =>
17855 null;
17856
17857 when others =>
17858 Is_Ok := False;
17859 end case;
17860
17861 <<Done>>
17862 return Is_Ok;
17863 end Is_SPARK_05_Initialization_Expr;
17864
17865 ----------------------------------
17866 -- Is_SPARK_05_Object_Reference --
17867 ----------------------------------
17868
17869 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
17870 begin
17871 if Is_Entity_Name (N) then
17872 return Present (Entity (N))
17873 and then
17874 (Ekind_In (Entity (N), E_Constant, E_Variable)
17875 or else Ekind (Entity (N)) in Formal_Kind);
17876
17877 else
17878 case Nkind (N) is
17879 when N_Selected_Component =>
17880 return Is_SPARK_05_Object_Reference (Prefix (N));
17881
17882 when others =>
17883 return False;
17884 end case;
17885 end if;
17886 end Is_SPARK_05_Object_Reference;
17887
17888 -----------------------------
17889 -- Is_Specific_Tagged_Type --
17890 -----------------------------
17891
17892 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
17893 Full_Typ : Entity_Id;
17894
17895 begin
17896 -- Handle private types
17897
17898 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
17899 Full_Typ := Full_View (Typ);
17900 else
17901 Full_Typ := Typ;
17902 end if;
17903
17904 -- A specific tagged type is a non-class-wide tagged type
17905
17906 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
17907 end Is_Specific_Tagged_Type;
17908
17909 ------------------
17910 -- Is_Statement --
17911 ------------------
17912
17913 function Is_Statement (N : Node_Id) return Boolean is
17914 begin
17915 return
17916 Nkind (N) in N_Statement_Other_Than_Procedure_Call
17917 or else Nkind (N) = N_Procedure_Call_Statement;
17918 end Is_Statement;
17919
17920 ----------------------------------------
17921 -- Is_Subcomponent_Of_Atomic_Object --
17922 ----------------------------------------
17923
17924 function Is_Subcomponent_Of_Atomic_Object (N : Node_Id) return Boolean is
17925 R : Node_Id;
17926
17927 begin
17928 R := Get_Referenced_Object (N);
17929
17930 while Nkind_In (R, N_Indexed_Component, N_Selected_Component, N_Slice)
17931 loop
17932 R := Get_Referenced_Object (Prefix (R));
17933
17934 -- If the prefix is an access value, only the designated type matters
17935
17936 if Is_Access_Type (Etype (R)) then
17937 if Is_Atomic (Designated_Type (Etype (R))) then
17938 return True;
17939 end if;
17940
17941 else
17942 if Is_Atomic_Object (R) then
17943 return True;
17944 end if;
17945 end if;
17946 end loop;
17947
17948 return False;
17949 end Is_Subcomponent_Of_Atomic_Object;
17950
17951 ---------------------------------------
17952 -- Is_Subprogram_Contract_Annotation --
17953 ---------------------------------------
17954
17955 function Is_Subprogram_Contract_Annotation
17956 (Item : Node_Id) return Boolean
17957 is
17958 Nam : Name_Id;
17959
17960 begin
17961 if Nkind (Item) = N_Aspect_Specification then
17962 Nam := Chars (Identifier (Item));
17963
17964 else pragma Assert (Nkind (Item) = N_Pragma);
17965 Nam := Pragma_Name (Item);
17966 end if;
17967
17968 return Nam = Name_Contract_Cases
17969 or else Nam = Name_Depends
17970 or else Nam = Name_Extensions_Visible
17971 or else Nam = Name_Global
17972 or else Nam = Name_Post
17973 or else Nam = Name_Post_Class
17974 or else Nam = Name_Postcondition
17975 or else Nam = Name_Pre
17976 or else Nam = Name_Pre_Class
17977 or else Nam = Name_Precondition
17978 or else Nam = Name_Refined_Depends
17979 or else Nam = Name_Refined_Global
17980 or else Nam = Name_Refined_Post
17981 or else Nam = Name_Test_Case;
17982 end Is_Subprogram_Contract_Annotation;
17983
17984 --------------------------------------------------
17985 -- Is_Subprogram_Stub_Without_Prior_Declaration --
17986 --------------------------------------------------
17987
17988 function Is_Subprogram_Stub_Without_Prior_Declaration
17989 (N : Node_Id) return Boolean
17990 is
17991 begin
17992 pragma Assert (Nkind (N) = N_Subprogram_Body_Stub);
17993
17994 case Ekind (Defining_Entity (N)) is
17995
17996 -- A subprogram stub without prior declaration serves as declaration
17997 -- for the actual subprogram body. As such, it has an attached
17998 -- defining entity of E_Function or E_Procedure.
17999
18000 when E_Function
18001 | E_Procedure
18002 =>
18003 return True;
18004
18005 -- Otherwise, it is completes a [generic] subprogram declaration
18006
18007 when E_Generic_Function
18008 | E_Generic_Procedure
18009 | E_Subprogram_Body
18010 =>
18011 return False;
18012
18013 when others =>
18014 raise Program_Error;
18015 end case;
18016 end Is_Subprogram_Stub_Without_Prior_Declaration;
18017
18018 ---------------------------
18019 -- Is_Suitable_Primitive --
18020 ---------------------------
18021
18022 function Is_Suitable_Primitive (Subp_Id : Entity_Id) return Boolean is
18023 begin
18024 -- The Default_Initial_Condition and invariant procedures must not be
18025 -- treated as primitive operations even when they apply to a tagged
18026 -- type. These routines must not act as targets of dispatching calls
18027 -- because they already utilize class-wide-precondition semantics to
18028 -- handle inheritance and overriding.
18029
18030 if Ekind (Subp_Id) = E_Procedure
18031 and then (Is_DIC_Procedure (Subp_Id)
18032 or else
18033 Is_Invariant_Procedure (Subp_Id))
18034 then
18035 return False;
18036 end if;
18037
18038 return True;
18039 end Is_Suitable_Primitive;
18040
18041 --------------------------
18042 -- Is_Suspension_Object --
18043 --------------------------
18044
18045 function Is_Suspension_Object (Id : Entity_Id) return Boolean is
18046 begin
18047 -- This approach does an exact name match rather than to rely on
18048 -- RTSfind. Routine Is_Effectively_Volatile is used by clients of the
18049 -- front end at point where all auxiliary tables are locked and any
18050 -- modifications to them are treated as violations. Do not tamper with
18051 -- the tables, instead examine the Chars fields of all the scopes of Id.
18052
18053 return
18054 Chars (Id) = Name_Suspension_Object
18055 and then Present (Scope (Id))
18056 and then Chars (Scope (Id)) = Name_Synchronous_Task_Control
18057 and then Present (Scope (Scope (Id)))
18058 and then Chars (Scope (Scope (Id))) = Name_Ada
18059 and then Present (Scope (Scope (Scope (Id))))
18060 and then Scope (Scope (Scope (Id))) = Standard_Standard;
18061 end Is_Suspension_Object;
18062
18063 ----------------------------
18064 -- Is_Synchronized_Object --
18065 ----------------------------
18066
18067 function Is_Synchronized_Object (Id : Entity_Id) return Boolean is
18068 Prag : Node_Id;
18069
18070 begin
18071 if Is_Object (Id) then
18072
18073 -- The object is synchronized if it is of a type that yields a
18074 -- synchronized object.
18075
18076 if Yields_Synchronized_Object (Etype (Id)) then
18077 return True;
18078
18079 -- The object is synchronized if it is atomic and Async_Writers is
18080 -- enabled.
18081
18082 elsif Is_Atomic_Object_Entity (Id)
18083 and then Async_Writers_Enabled (Id)
18084 then
18085 return True;
18086
18087 -- A constant is a synchronized object by default
18088
18089 elsif Ekind (Id) = E_Constant then
18090 return True;
18091
18092 -- A variable is a synchronized object if it is subject to pragma
18093 -- Constant_After_Elaboration.
18094
18095 elsif Ekind (Id) = E_Variable then
18096 Prag := Get_Pragma (Id, Pragma_Constant_After_Elaboration);
18097
18098 return Present (Prag) and then Is_Enabled_Pragma (Prag);
18099 end if;
18100 end if;
18101
18102 -- Otherwise the input is not an object or it does not qualify as a
18103 -- synchronized object.
18104
18105 return False;
18106 end Is_Synchronized_Object;
18107
18108 ---------------------------------
18109 -- Is_Synchronized_Tagged_Type --
18110 ---------------------------------
18111
18112 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
18113 Kind : constant Entity_Kind := Ekind (Base_Type (E));
18114
18115 begin
18116 -- A task or protected type derived from an interface is a tagged type.
18117 -- Such a tagged type is called a synchronized tagged type, as are
18118 -- synchronized interfaces and private extensions whose declaration
18119 -- includes the reserved word synchronized.
18120
18121 return (Is_Tagged_Type (E)
18122 and then (Kind = E_Task_Type
18123 or else
18124 Kind = E_Protected_Type))
18125 or else
18126 (Is_Interface (E)
18127 and then Is_Synchronized_Interface (E))
18128 or else
18129 (Ekind (E) = E_Record_Type_With_Private
18130 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
18131 and then (Synchronized_Present (Parent (E))
18132 or else Is_Synchronized_Interface (Etype (E))));
18133 end Is_Synchronized_Tagged_Type;
18134
18135 -----------------
18136 -- Is_Transfer --
18137 -----------------
18138
18139 function Is_Transfer (N : Node_Id) return Boolean is
18140 Kind : constant Node_Kind := Nkind (N);
18141
18142 begin
18143 if Kind = N_Simple_Return_Statement
18144 or else
18145 Kind = N_Extended_Return_Statement
18146 or else
18147 Kind = N_Goto_Statement
18148 or else
18149 Kind = N_Raise_Statement
18150 or else
18151 Kind = N_Requeue_Statement
18152 then
18153 return True;
18154
18155 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
18156 and then No (Condition (N))
18157 then
18158 return True;
18159
18160 elsif Kind = N_Procedure_Call_Statement
18161 and then Is_Entity_Name (Name (N))
18162 and then Present (Entity (Name (N)))
18163 and then No_Return (Entity (Name (N)))
18164 then
18165 return True;
18166
18167 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
18168 return True;
18169
18170 else
18171 return False;
18172 end if;
18173 end Is_Transfer;
18174
18175 -------------
18176 -- Is_True --
18177 -------------
18178
18179 function Is_True (U : Uint) return Boolean is
18180 begin
18181 return (U /= 0);
18182 end Is_True;
18183
18184 --------------------------------------
18185 -- Is_Unchecked_Conversion_Instance --
18186 --------------------------------------
18187
18188 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
18189 Par : Node_Id;
18190
18191 begin
18192 -- Look for a function whose generic parent is the predefined intrinsic
18193 -- function Unchecked_Conversion, or for one that renames such an
18194 -- instance.
18195
18196 if Ekind (Id) = E_Function then
18197 Par := Parent (Id);
18198
18199 if Nkind (Par) = N_Function_Specification then
18200 Par := Generic_Parent (Par);
18201
18202 if Present (Par) then
18203 return
18204 Chars (Par) = Name_Unchecked_Conversion
18205 and then Is_Intrinsic_Subprogram (Par)
18206 and then In_Predefined_Unit (Par);
18207 else
18208 return
18209 Present (Alias (Id))
18210 and then Is_Unchecked_Conversion_Instance (Alias (Id));
18211 end if;
18212 end if;
18213 end if;
18214
18215 return False;
18216 end Is_Unchecked_Conversion_Instance;
18217
18218 -------------------------------
18219 -- Is_Universal_Numeric_Type --
18220 -------------------------------
18221
18222 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
18223 begin
18224 return T = Universal_Integer or else T = Universal_Real;
18225 end Is_Universal_Numeric_Type;
18226
18227 ------------------------------
18228 -- Is_User_Defined_Equality --
18229 ------------------------------
18230
18231 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean is
18232 begin
18233 return Ekind (Id) = E_Function
18234 and then Chars (Id) = Name_Op_Eq
18235 and then Comes_From_Source (Id)
18236
18237 -- Internally generated equalities have a full type declaration
18238 -- as their parent.
18239
18240 and then Nkind (Parent (Id)) = N_Function_Specification;
18241 end Is_User_Defined_Equality;
18242
18243 --------------------------------------
18244 -- Is_Validation_Variable_Reference --
18245 --------------------------------------
18246
18247 function Is_Validation_Variable_Reference (N : Node_Id) return Boolean is
18248 Var : constant Node_Id := Unqual_Conv (N);
18249 Var_Id : Entity_Id;
18250
18251 begin
18252 Var_Id := Empty;
18253
18254 if Is_Entity_Name (Var) then
18255 Var_Id := Entity (Var);
18256 end if;
18257
18258 return
18259 Present (Var_Id)
18260 and then Ekind (Var_Id) = E_Variable
18261 and then Present (Validated_Object (Var_Id));
18262 end Is_Validation_Variable_Reference;
18263
18264 ----------------------------
18265 -- Is_Variable_Size_Array --
18266 ----------------------------
18267
18268 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
18269 Idx : Node_Id;
18270
18271 begin
18272 pragma Assert (Is_Array_Type (E));
18273
18274 -- Check if some index is initialized with a non-constant value
18275
18276 Idx := First_Index (E);
18277 while Present (Idx) loop
18278 if Nkind (Idx) = N_Range then
18279 if not Is_Constant_Bound (Low_Bound (Idx))
18280 or else not Is_Constant_Bound (High_Bound (Idx))
18281 then
18282 return True;
18283 end if;
18284 end if;
18285
18286 Next_Index (Idx);
18287 end loop;
18288
18289 return False;
18290 end Is_Variable_Size_Array;
18291
18292 -----------------------------
18293 -- Is_Variable_Size_Record --
18294 -----------------------------
18295
18296 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
18297 Comp : Entity_Id;
18298 Comp_Typ : Entity_Id;
18299
18300 begin
18301 pragma Assert (Is_Record_Type (E));
18302
18303 Comp := First_Component (E);
18304 while Present (Comp) loop
18305 Comp_Typ := Underlying_Type (Etype (Comp));
18306
18307 -- Recursive call if the record type has discriminants
18308
18309 if Is_Record_Type (Comp_Typ)
18310 and then Has_Discriminants (Comp_Typ)
18311 and then Is_Variable_Size_Record (Comp_Typ)
18312 then
18313 return True;
18314
18315 elsif Is_Array_Type (Comp_Typ)
18316 and then Is_Variable_Size_Array (Comp_Typ)
18317 then
18318 return True;
18319 end if;
18320
18321 Next_Component (Comp);
18322 end loop;
18323
18324 return False;
18325 end Is_Variable_Size_Record;
18326
18327 -----------------
18328 -- Is_Variable --
18329 -----------------
18330
18331 function Is_Variable
18332 (N : Node_Id;
18333 Use_Original_Node : Boolean := True) return Boolean
18334 is
18335 Orig_Node : Node_Id;
18336
18337 function In_Protected_Function (E : Entity_Id) return Boolean;
18338 -- Within a protected function, the private components of the enclosing
18339 -- protected type are constants. A function nested within a (protected)
18340 -- procedure is not itself protected. Within the body of a protected
18341 -- function the current instance of the protected type is a constant.
18342
18343 function Is_Variable_Prefix (P : Node_Id) return Boolean;
18344 -- Prefixes can involve implicit dereferences, in which case we must
18345 -- test for the case of a reference of a constant access type, which can
18346 -- can never be a variable.
18347
18348 ---------------------------
18349 -- In_Protected_Function --
18350 ---------------------------
18351
18352 function In_Protected_Function (E : Entity_Id) return Boolean is
18353 Prot : Entity_Id;
18354 S : Entity_Id;
18355
18356 begin
18357 -- E is the current instance of a type
18358
18359 if Is_Type (E) then
18360 Prot := E;
18361
18362 -- E is an object
18363
18364 else
18365 Prot := Scope (E);
18366 end if;
18367
18368 if not Is_Protected_Type (Prot) then
18369 return False;
18370
18371 else
18372 S := Current_Scope;
18373 while Present (S) and then S /= Prot loop
18374 if Ekind (S) = E_Function and then Scope (S) = Prot then
18375 return True;
18376 end if;
18377
18378 S := Scope (S);
18379 end loop;
18380
18381 return False;
18382 end if;
18383 end In_Protected_Function;
18384
18385 ------------------------
18386 -- Is_Variable_Prefix --
18387 ------------------------
18388
18389 function Is_Variable_Prefix (P : Node_Id) return Boolean is
18390 begin
18391 if Is_Access_Type (Etype (P)) then
18392 return not Is_Access_Constant (Root_Type (Etype (P)));
18393
18394 -- For the case of an indexed component whose prefix has a packed
18395 -- array type, the prefix has been rewritten into a type conversion.
18396 -- Determine variable-ness from the converted expression.
18397
18398 elsif Nkind (P) = N_Type_Conversion
18399 and then not Comes_From_Source (P)
18400 and then Is_Array_Type (Etype (P))
18401 and then Is_Packed (Etype (P))
18402 then
18403 return Is_Variable (Expression (P));
18404
18405 else
18406 return Is_Variable (P);
18407 end if;
18408 end Is_Variable_Prefix;
18409
18410 -- Start of processing for Is_Variable
18411
18412 begin
18413 -- Special check, allow x'Deref(expr) as a variable
18414
18415 if Nkind (N) = N_Attribute_Reference
18416 and then Attribute_Name (N) = Name_Deref
18417 then
18418 return True;
18419 end if;
18420
18421 -- Check if we perform the test on the original node since this may be a
18422 -- test of syntactic categories which must not be disturbed by whatever
18423 -- rewriting might have occurred. For example, an aggregate, which is
18424 -- certainly NOT a variable, could be turned into a variable by
18425 -- expansion.
18426
18427 if Use_Original_Node then
18428 Orig_Node := Original_Node (N);
18429 else
18430 Orig_Node := N;
18431 end if;
18432
18433 -- Definitely OK if Assignment_OK is set. Since this is something that
18434 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
18435
18436 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
18437 return True;
18438
18439 -- Normally we go to the original node, but there is one exception where
18440 -- we use the rewritten node, namely when it is an explicit dereference.
18441 -- The generated code may rewrite a prefix which is an access type with
18442 -- an explicit dereference. The dereference is a variable, even though
18443 -- the original node may not be (since it could be a constant of the
18444 -- access type).
18445
18446 -- In Ada 2005 we have a further case to consider: the prefix may be a
18447 -- function call given in prefix notation. The original node appears to
18448 -- be a selected component, but we need to examine the call.
18449
18450 elsif Nkind (N) = N_Explicit_Dereference
18451 and then Nkind (Orig_Node) /= N_Explicit_Dereference
18452 and then Present (Etype (Orig_Node))
18453 and then Is_Access_Type (Etype (Orig_Node))
18454 then
18455 -- Note that if the prefix is an explicit dereference that does not
18456 -- come from source, we must check for a rewritten function call in
18457 -- prefixed notation before other forms of rewriting, to prevent a
18458 -- compiler crash.
18459
18460 return
18461 (Nkind (Orig_Node) = N_Function_Call
18462 and then not Is_Access_Constant (Etype (Prefix (N))))
18463 or else
18464 Is_Variable_Prefix (Original_Node (Prefix (N)));
18465
18466 -- in Ada 2012, the dereference may have been added for a type with
18467 -- a declared implicit dereference aspect. Check that it is not an
18468 -- access to constant.
18469
18470 elsif Nkind (N) = N_Explicit_Dereference
18471 and then Present (Etype (Orig_Node))
18472 and then Ada_Version >= Ada_2012
18473 and then Has_Implicit_Dereference (Etype (Orig_Node))
18474 then
18475 return not Is_Access_Constant (Etype (Prefix (N)));
18476
18477 -- A function call is never a variable
18478
18479 elsif Nkind (N) = N_Function_Call then
18480 return False;
18481
18482 -- All remaining checks use the original node
18483
18484 elsif Is_Entity_Name (Orig_Node)
18485 and then Present (Entity (Orig_Node))
18486 then
18487 declare
18488 E : constant Entity_Id := Entity (Orig_Node);
18489 K : constant Entity_Kind := Ekind (E);
18490
18491 begin
18492 if Is_Loop_Parameter (E) then
18493 return False;
18494 end if;
18495
18496 return (K = E_Variable
18497 and then Nkind (Parent (E)) /= N_Exception_Handler)
18498 or else (K = E_Component
18499 and then not In_Protected_Function (E))
18500 or else K = E_Out_Parameter
18501 or else K = E_In_Out_Parameter
18502 or else K = E_Generic_In_Out_Parameter
18503
18504 -- Current instance of type. If this is a protected type, check
18505 -- we are not within the body of one of its protected functions.
18506
18507 or else (Is_Type (E)
18508 and then In_Open_Scopes (E)
18509 and then not In_Protected_Function (E))
18510
18511 or else (Is_Incomplete_Or_Private_Type (E)
18512 and then In_Open_Scopes (Full_View (E)));
18513 end;
18514
18515 else
18516 case Nkind (Orig_Node) is
18517 when N_Indexed_Component
18518 | N_Slice
18519 =>
18520 return Is_Variable_Prefix (Prefix (Orig_Node));
18521
18522 when N_Selected_Component =>
18523 return (Is_Variable (Selector_Name (Orig_Node))
18524 and then Is_Variable_Prefix (Prefix (Orig_Node)))
18525 or else
18526 (Nkind (N) = N_Expanded_Name
18527 and then Scope (Entity (N)) = Entity (Prefix (N)));
18528
18529 -- For an explicit dereference, the type of the prefix cannot
18530 -- be an access to constant or an access to subprogram.
18531
18532 when N_Explicit_Dereference =>
18533 declare
18534 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
18535 begin
18536 return Is_Access_Type (Typ)
18537 and then not Is_Access_Constant (Root_Type (Typ))
18538 and then Ekind (Typ) /= E_Access_Subprogram_Type;
18539 end;
18540
18541 -- The type conversion is the case where we do not deal with the
18542 -- context dependent special case of an actual parameter. Thus
18543 -- the type conversion is only considered a variable for the
18544 -- purposes of this routine if the target type is tagged. However,
18545 -- a type conversion is considered to be a variable if it does not
18546 -- come from source (this deals for example with the conversions
18547 -- of expressions to their actual subtypes).
18548
18549 when N_Type_Conversion =>
18550 return Is_Variable (Expression (Orig_Node))
18551 and then
18552 (not Comes_From_Source (Orig_Node)
18553 or else
18554 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
18555 and then
18556 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
18557
18558 -- GNAT allows an unchecked type conversion as a variable. This
18559 -- only affects the generation of internal expanded code, since
18560 -- calls to instantiations of Unchecked_Conversion are never
18561 -- considered variables (since they are function calls).
18562
18563 when N_Unchecked_Type_Conversion =>
18564 return Is_Variable (Expression (Orig_Node));
18565
18566 when others =>
18567 return False;
18568 end case;
18569 end if;
18570 end Is_Variable;
18571
18572 ---------------------------
18573 -- Is_Visibly_Controlled --
18574 ---------------------------
18575
18576 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
18577 Root : constant Entity_Id := Root_Type (T);
18578 begin
18579 return Chars (Scope (Root)) = Name_Finalization
18580 and then Chars (Scope (Scope (Root))) = Name_Ada
18581 and then Scope (Scope (Scope (Root))) = Standard_Standard;
18582 end Is_Visibly_Controlled;
18583
18584 --------------------------------------
18585 -- Is_Volatile_Full_Access_Object --
18586 --------------------------------------
18587
18588 function Is_Volatile_Full_Access_Object (N : Node_Id) return Boolean is
18589 function Is_VFA_Object_Entity (Id : Entity_Id) return Boolean;
18590 -- Determine whether arbitrary entity Id denotes an object that is
18591 -- Volatile_Full_Access.
18592
18593 ----------------------------
18594 -- Is_VFA_Object_Entity --
18595 ----------------------------
18596
18597 function Is_VFA_Object_Entity (Id : Entity_Id) return Boolean is
18598 begin
18599 return
18600 Is_Object (Id)
18601 and then (Is_Volatile_Full_Access (Id)
18602 or else
18603 Is_Volatile_Full_Access (Etype (Id)));
18604 end Is_VFA_Object_Entity;
18605
18606 -- Start of processing for Is_Volatile_Full_Access_Object
18607
18608 begin
18609 if Is_Entity_Name (N) then
18610 return Is_VFA_Object_Entity (Entity (N));
18611
18612 elsif Is_Volatile_Full_Access (Etype (N)) then
18613 return True;
18614
18615 elsif Nkind (N) = N_Selected_Component then
18616 return Is_Volatile_Full_Access (Entity (Selector_Name (N)));
18617
18618 else
18619 return False;
18620 end if;
18621 end Is_Volatile_Full_Access_Object;
18622
18623 --------------------------
18624 -- Is_Volatile_Function --
18625 --------------------------
18626
18627 function Is_Volatile_Function (Func_Id : Entity_Id) return Boolean is
18628 begin
18629 pragma Assert (Ekind_In (Func_Id, E_Function, E_Generic_Function));
18630
18631 -- A function declared within a protected type is volatile
18632
18633 if Is_Protected_Type (Scope (Func_Id)) then
18634 return True;
18635
18636 -- An instance of Ada.Unchecked_Conversion is a volatile function if
18637 -- either the source or the target are effectively volatile.
18638
18639 elsif Is_Unchecked_Conversion_Instance (Func_Id)
18640 and then Has_Effectively_Volatile_Profile (Func_Id)
18641 then
18642 return True;
18643
18644 -- Otherwise the function is treated as volatile if it is subject to
18645 -- enabled pragma Volatile_Function.
18646
18647 else
18648 return
18649 Is_Enabled_Pragma (Get_Pragma (Func_Id, Pragma_Volatile_Function));
18650 end if;
18651 end Is_Volatile_Function;
18652
18653 ------------------------
18654 -- Is_Volatile_Object --
18655 ------------------------
18656
18657 function Is_Volatile_Object (N : Node_Id) return Boolean is
18658 function Is_Volatile_Object_Entity (Id : Entity_Id) return Boolean;
18659 -- Determine whether arbitrary entity Id denotes an object that is
18660 -- Volatile.
18661
18662 function Prefix_Has_Volatile_Components (P : Node_Id) return Boolean;
18663 -- Determine whether prefix P has volatile components. This requires
18664 -- the presence of a Volatile_Components aspect/pragma or that P be
18665 -- itself a volatile object as per RM C.6(8).
18666
18667 ---------------------------------
18668 -- Is_Volatile_Object_Entity --
18669 ---------------------------------
18670
18671 function Is_Volatile_Object_Entity (Id : Entity_Id) return Boolean is
18672 begin
18673 return
18674 Is_Object (Id)
18675 and then (Is_Volatile (Id) or else Is_Volatile (Etype (Id)));
18676 end Is_Volatile_Object_Entity;
18677
18678 ------------------------------------
18679 -- Prefix_Has_Volatile_Components --
18680 ------------------------------------
18681
18682 function Prefix_Has_Volatile_Components (P : Node_Id) return Boolean is
18683 Typ : constant Entity_Id := Etype (P);
18684
18685 begin
18686 if Is_Access_Type (Typ) then
18687 declare
18688 Dtyp : constant Entity_Id := Designated_Type (Typ);
18689
18690 begin
18691 return Has_Volatile_Components (Dtyp)
18692 or else Is_Volatile (Dtyp);
18693 end;
18694
18695 elsif Has_Volatile_Components (Typ) then
18696 return True;
18697
18698 elsif Is_Entity_Name (P)
18699 and then Has_Volatile_Component (Entity (P))
18700 then
18701 return True;
18702
18703 elsif Is_Volatile_Object (P) then
18704 return True;
18705
18706 else
18707 return False;
18708 end if;
18709 end Prefix_Has_Volatile_Components;
18710
18711 -- Start of processing for Is_Volatile_Object
18712
18713 begin
18714 if Is_Entity_Name (N) then
18715 return Is_Volatile_Object_Entity (Entity (N));
18716
18717 elsif Is_Volatile (Etype (N)) then
18718 return True;
18719
18720 elsif Nkind (N) = N_Indexed_Component then
18721 return Prefix_Has_Volatile_Components (Prefix (N));
18722
18723 elsif Nkind (N) = N_Selected_Component then
18724 return Prefix_Has_Volatile_Components (Prefix (N))
18725 or else Is_Volatile (Entity (Selector_Name (N)));
18726
18727 else
18728 return False;
18729 end if;
18730 end Is_Volatile_Object;
18731
18732 -----------------------------
18733 -- Iterate_Call_Parameters --
18734 -----------------------------
18735
18736 procedure Iterate_Call_Parameters (Call : Node_Id) is
18737 Actual : Node_Id := First_Actual (Call);
18738 Formal : Entity_Id := First_Formal (Get_Called_Entity (Call));
18739
18740 begin
18741 while Present (Formal) and then Present (Actual) loop
18742 Handle_Parameter (Formal, Actual);
18743
18744 Next_Formal (Formal);
18745 Next_Actual (Actual);
18746 end loop;
18747
18748 pragma Assert (No (Formal));
18749 pragma Assert (No (Actual));
18750 end Iterate_Call_Parameters;
18751
18752 ---------------------------
18753 -- Itype_Has_Declaration --
18754 ---------------------------
18755
18756 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
18757 begin
18758 pragma Assert (Is_Itype (Id));
18759 return Present (Parent (Id))
18760 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
18761 N_Subtype_Declaration)
18762 and then Defining_Entity (Parent (Id)) = Id;
18763 end Itype_Has_Declaration;
18764
18765 -------------------------
18766 -- Kill_Current_Values --
18767 -------------------------
18768
18769 procedure Kill_Current_Values
18770 (Ent : Entity_Id;
18771 Last_Assignment_Only : Boolean := False)
18772 is
18773 begin
18774 if Is_Assignable (Ent) then
18775 Set_Last_Assignment (Ent, Empty);
18776 end if;
18777
18778 if Is_Object (Ent) then
18779 if not Last_Assignment_Only then
18780 Kill_Checks (Ent);
18781 Set_Current_Value (Ent, Empty);
18782
18783 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
18784 -- for a constant. Once the constant is elaborated, its value is
18785 -- not changed, therefore the associated flags that describe the
18786 -- value should not be modified either.
18787
18788 if Ekind (Ent) = E_Constant then
18789 null;
18790
18791 -- Non-constant entities
18792
18793 else
18794 if not Can_Never_Be_Null (Ent) then
18795 Set_Is_Known_Non_Null (Ent, False);
18796 end if;
18797
18798 Set_Is_Known_Null (Ent, False);
18799
18800 -- Reset the Is_Known_Valid flag unless the type is always
18801 -- valid. This does not apply to a loop parameter because its
18802 -- bounds are defined by the loop header and therefore always
18803 -- valid.
18804
18805 if not Is_Known_Valid (Etype (Ent))
18806 and then Ekind (Ent) /= E_Loop_Parameter
18807 then
18808 Set_Is_Known_Valid (Ent, False);
18809 end if;
18810 end if;
18811 end if;
18812 end if;
18813 end Kill_Current_Values;
18814
18815 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
18816 S : Entity_Id;
18817
18818 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
18819 -- Clear current value for entity E and all entities chained to E
18820
18821 ------------------------------------------
18822 -- Kill_Current_Values_For_Entity_Chain --
18823 ------------------------------------------
18824
18825 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
18826 Ent : Entity_Id;
18827 begin
18828 Ent := E;
18829 while Present (Ent) loop
18830 Kill_Current_Values (Ent, Last_Assignment_Only);
18831 Next_Entity (Ent);
18832 end loop;
18833 end Kill_Current_Values_For_Entity_Chain;
18834
18835 -- Start of processing for Kill_Current_Values
18836
18837 begin
18838 -- Kill all saved checks, a special case of killing saved values
18839
18840 if not Last_Assignment_Only then
18841 Kill_All_Checks;
18842 end if;
18843
18844 -- Loop through relevant scopes, which includes the current scope and
18845 -- any parent scopes if the current scope is a block or a package.
18846
18847 S := Current_Scope;
18848 Scope_Loop : loop
18849
18850 -- Clear current values of all entities in current scope
18851
18852 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
18853
18854 -- If scope is a package, also clear current values of all private
18855 -- entities in the scope.
18856
18857 if Is_Package_Or_Generic_Package (S)
18858 or else Is_Concurrent_Type (S)
18859 then
18860 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
18861 end if;
18862
18863 -- If this is a not a subprogram, deal with parents
18864
18865 if not Is_Subprogram (S) then
18866 S := Scope (S);
18867 exit Scope_Loop when S = Standard_Standard;
18868 else
18869 exit Scope_Loop;
18870 end if;
18871 end loop Scope_Loop;
18872 end Kill_Current_Values;
18873
18874 --------------------------
18875 -- Kill_Size_Check_Code --
18876 --------------------------
18877
18878 procedure Kill_Size_Check_Code (E : Entity_Id) is
18879 begin
18880 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
18881 and then Present (Size_Check_Code (E))
18882 then
18883 Remove (Size_Check_Code (E));
18884 Set_Size_Check_Code (E, Empty);
18885 end if;
18886 end Kill_Size_Check_Code;
18887
18888 --------------------
18889 -- Known_Non_Null --
18890 --------------------
18891
18892 function Known_Non_Null (N : Node_Id) return Boolean is
18893 Status : constant Null_Status_Kind := Null_Status (N);
18894
18895 Id : Entity_Id;
18896 Op : Node_Kind;
18897 Val : Node_Id;
18898
18899 begin
18900 -- The expression yields a non-null value ignoring simple flow analysis
18901
18902 if Status = Is_Non_Null then
18903 return True;
18904
18905 -- Otherwise check whether N is a reference to an entity that appears
18906 -- within a conditional construct.
18907
18908 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18909
18910 -- First check if we are in decisive conditional
18911
18912 Get_Current_Value_Condition (N, Op, Val);
18913
18914 if Known_Null (Val) then
18915 if Op = N_Op_Eq then
18916 return False;
18917 elsif Op = N_Op_Ne then
18918 return True;
18919 end if;
18920 end if;
18921
18922 -- If OK to do replacement, test Is_Known_Non_Null flag
18923
18924 Id := Entity (N);
18925
18926 if OK_To_Do_Constant_Replacement (Id) then
18927 return Is_Known_Non_Null (Id);
18928 end if;
18929 end if;
18930
18931 -- Otherwise it is not possible to determine whether N yields a non-null
18932 -- value.
18933
18934 return False;
18935 end Known_Non_Null;
18936
18937 ----------------
18938 -- Known_Null --
18939 ----------------
18940
18941 function Known_Null (N : Node_Id) return Boolean is
18942 Status : constant Null_Status_Kind := Null_Status (N);
18943
18944 Id : Entity_Id;
18945 Op : Node_Kind;
18946 Val : Node_Id;
18947
18948 begin
18949 -- The expression yields a null value ignoring simple flow analysis
18950
18951 if Status = Is_Null then
18952 return True;
18953
18954 -- Otherwise check whether N is a reference to an entity that appears
18955 -- within a conditional construct.
18956
18957 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18958
18959 -- First check if we are in decisive conditional
18960
18961 Get_Current_Value_Condition (N, Op, Val);
18962
18963 if Known_Null (Val) then
18964 if Op = N_Op_Eq then
18965 return True;
18966 elsif Op = N_Op_Ne then
18967 return False;
18968 end if;
18969 end if;
18970
18971 -- If OK to do replacement, test Is_Known_Null flag
18972
18973 Id := Entity (N);
18974
18975 if OK_To_Do_Constant_Replacement (Id) then
18976 return Is_Known_Null (Id);
18977 end if;
18978 end if;
18979
18980 -- Otherwise it is not possible to determine whether N yields a null
18981 -- value.
18982
18983 return False;
18984 end Known_Null;
18985
18986 --------------------------
18987 -- Known_To_Be_Assigned --
18988 --------------------------
18989
18990 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
18991 P : constant Node_Id := Parent (N);
18992
18993 begin
18994 case Nkind (P) is
18995
18996 -- Test left side of assignment
18997
18998 when N_Assignment_Statement =>
18999 return N = Name (P);
19000
19001 -- Function call arguments are never lvalues
19002
19003 when N_Function_Call =>
19004 return False;
19005
19006 -- Positional parameter for procedure or accept call
19007
19008 when N_Accept_Statement
19009 | N_Procedure_Call_Statement
19010 =>
19011 declare
19012 Proc : Entity_Id;
19013 Form : Entity_Id;
19014 Act : Node_Id;
19015
19016 begin
19017 Proc := Get_Subprogram_Entity (P);
19018
19019 if No (Proc) then
19020 return False;
19021 end if;
19022
19023 -- If we are not a list member, something is strange, so
19024 -- be conservative and return False.
19025
19026 if not Is_List_Member (N) then
19027 return False;
19028 end if;
19029
19030 -- We are going to find the right formal by stepping forward
19031 -- through the formals, as we step backwards in the actuals.
19032
19033 Form := First_Formal (Proc);
19034 Act := N;
19035 loop
19036 -- If no formal, something is weird, so be conservative
19037 -- and return False.
19038
19039 if No (Form) then
19040 return False;
19041 end if;
19042
19043 Prev (Act);
19044 exit when No (Act);
19045 Next_Formal (Form);
19046 end loop;
19047
19048 return Ekind (Form) /= E_In_Parameter;
19049 end;
19050
19051 -- Named parameter for procedure or accept call
19052
19053 when N_Parameter_Association =>
19054 declare
19055 Proc : Entity_Id;
19056 Form : Entity_Id;
19057
19058 begin
19059 Proc := Get_Subprogram_Entity (Parent (P));
19060
19061 if No (Proc) then
19062 return False;
19063 end if;
19064
19065 -- Loop through formals to find the one that matches
19066
19067 Form := First_Formal (Proc);
19068 loop
19069 -- If no matching formal, that's peculiar, some kind of
19070 -- previous error, so return False to be conservative.
19071 -- Actually this also happens in legal code in the case
19072 -- where P is a parameter association for an Extra_Formal???
19073
19074 if No (Form) then
19075 return False;
19076 end if;
19077
19078 -- Else test for match
19079
19080 if Chars (Form) = Chars (Selector_Name (P)) then
19081 return Ekind (Form) /= E_In_Parameter;
19082 end if;
19083
19084 Next_Formal (Form);
19085 end loop;
19086 end;
19087
19088 -- Test for appearing in a conversion that itself appears
19089 -- in an lvalue context, since this should be an lvalue.
19090
19091 when N_Type_Conversion =>
19092 return Known_To_Be_Assigned (P);
19093
19094 -- All other references are definitely not known to be modifications
19095
19096 when others =>
19097 return False;
19098 end case;
19099 end Known_To_Be_Assigned;
19100
19101 ---------------------------
19102 -- Last_Source_Statement --
19103 ---------------------------
19104
19105 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
19106 N : Node_Id;
19107
19108 begin
19109 N := Last (Statements (HSS));
19110 while Present (N) loop
19111 exit when Comes_From_Source (N);
19112 Prev (N);
19113 end loop;
19114
19115 return N;
19116 end Last_Source_Statement;
19117
19118 -----------------------
19119 -- Mark_Coextensions --
19120 -----------------------
19121
19122 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
19123 Is_Dynamic : Boolean;
19124 -- Indicates whether the context causes nested coextensions to be
19125 -- dynamic or static
19126
19127 function Mark_Allocator (N : Node_Id) return Traverse_Result;
19128 -- Recognize an allocator node and label it as a dynamic coextension
19129
19130 --------------------
19131 -- Mark_Allocator --
19132 --------------------
19133
19134 function Mark_Allocator (N : Node_Id) return Traverse_Result is
19135 begin
19136 if Nkind (N) = N_Allocator then
19137 if Is_Dynamic then
19138 Set_Is_Static_Coextension (N, False);
19139 Set_Is_Dynamic_Coextension (N);
19140
19141 -- If the allocator expression is potentially dynamic, it may
19142 -- be expanded out of order and require dynamic allocation
19143 -- anyway, so we treat the coextension itself as dynamic.
19144 -- Potential optimization ???
19145
19146 elsif Nkind (Expression (N)) = N_Qualified_Expression
19147 and then Nkind (Expression (Expression (N))) = N_Op_Concat
19148 then
19149 Set_Is_Static_Coextension (N, False);
19150 Set_Is_Dynamic_Coextension (N);
19151 else
19152 Set_Is_Dynamic_Coextension (N, False);
19153 Set_Is_Static_Coextension (N);
19154 end if;
19155 end if;
19156
19157 return OK;
19158 end Mark_Allocator;
19159
19160 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
19161
19162 -- Start of processing for Mark_Coextensions
19163
19164 begin
19165 -- An allocator that appears on the right-hand side of an assignment is
19166 -- treated as a potentially dynamic coextension when the right-hand side
19167 -- is an allocator or a qualified expression.
19168
19169 -- Obj := new ...'(new Coextension ...);
19170
19171 if Nkind (Context_Nod) = N_Assignment_Statement then
19172 Is_Dynamic :=
19173 Nkind_In (Expression (Context_Nod), N_Allocator,
19174 N_Qualified_Expression);
19175
19176 -- An allocator that appears within the expression of a simple return
19177 -- statement is treated as a potentially dynamic coextension when the
19178 -- expression is either aggregate, allocator, or qualified expression.
19179
19180 -- return (new Coextension ...);
19181 -- return new ...'(new Coextension ...);
19182
19183 elsif Nkind (Context_Nod) = N_Simple_Return_Statement then
19184 Is_Dynamic :=
19185 Nkind_In (Expression (Context_Nod), N_Aggregate,
19186 N_Allocator,
19187 N_Qualified_Expression);
19188
19189 -- An alloctor that appears within the initialization expression of an
19190 -- object declaration is considered a potentially dynamic coextension
19191 -- when the initialization expression is an allocator or a qualified
19192 -- expression.
19193
19194 -- Obj : ... := new ...'(new Coextension ...);
19195
19196 -- A similar case arises when the object declaration is part of an
19197 -- extended return statement.
19198
19199 -- return Obj : ... := new ...'(new Coextension ...);
19200 -- return Obj : ... := (new Coextension ...);
19201
19202 elsif Nkind (Context_Nod) = N_Object_Declaration then
19203 Is_Dynamic :=
19204 Nkind_In (Root_Nod, N_Allocator, N_Qualified_Expression)
19205 or else
19206 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
19207
19208 -- This routine should not be called with constructs that cannot contain
19209 -- coextensions.
19210
19211 else
19212 raise Program_Error;
19213 end if;
19214
19215 Mark_Allocators (Root_Nod);
19216 end Mark_Coextensions;
19217
19218 ---------------------------------
19219 -- Mark_Elaboration_Attributes --
19220 ---------------------------------
19221
19222 procedure Mark_Elaboration_Attributes
19223 (N_Id : Node_Or_Entity_Id;
19224 Checks : Boolean := False;
19225 Level : Boolean := False;
19226 Modes : Boolean := False;
19227 Warnings : Boolean := False)
19228 is
19229 function Elaboration_Checks_OK
19230 (Target_Id : Entity_Id;
19231 Context_Id : Entity_Id) return Boolean;
19232 -- Determine whether elaboration checks are enabled for target Target_Id
19233 -- which resides within context Context_Id.
19234
19235 procedure Mark_Elaboration_Attributes_Id (Id : Entity_Id);
19236 -- Preserve relevant attributes of the context in arbitrary entity Id
19237
19238 procedure Mark_Elaboration_Attributes_Node (N : Node_Id);
19239 -- Preserve relevant attributes of the context in arbitrary node N
19240
19241 ---------------------------
19242 -- Elaboration_Checks_OK --
19243 ---------------------------
19244
19245 function Elaboration_Checks_OK
19246 (Target_Id : Entity_Id;
19247 Context_Id : Entity_Id) return Boolean
19248 is
19249 Encl_Scop : Entity_Id;
19250
19251 begin
19252 -- Elaboration checks are suppressed for the target
19253
19254 if Elaboration_Checks_Suppressed (Target_Id) then
19255 return False;
19256 end if;
19257
19258 -- Otherwise elaboration checks are OK for the target, but may be
19259 -- suppressed for the context where the target is declared.
19260
19261 Encl_Scop := Context_Id;
19262 while Present (Encl_Scop) and then Encl_Scop /= Standard_Standard loop
19263 if Elaboration_Checks_Suppressed (Encl_Scop) then
19264 return False;
19265 end if;
19266
19267 Encl_Scop := Scope (Encl_Scop);
19268 end loop;
19269
19270 -- Neither the target nor its declarative context have elaboration
19271 -- checks suppressed.
19272
19273 return True;
19274 end Elaboration_Checks_OK;
19275
19276 ------------------------------------
19277 -- Mark_Elaboration_Attributes_Id --
19278 ------------------------------------
19279
19280 procedure Mark_Elaboration_Attributes_Id (Id : Entity_Id) is
19281 begin
19282 -- Mark the status of elaboration checks in effect. Do not reset the
19283 -- status in case the entity is reanalyzed with checks suppressed.
19284
19285 if Checks and then not Is_Elaboration_Checks_OK_Id (Id) then
19286 Set_Is_Elaboration_Checks_OK_Id (Id,
19287 Elaboration_Checks_OK
19288 (Target_Id => Id,
19289 Context_Id => Scope (Id)));
19290 end if;
19291
19292 -- Mark the status of elaboration warnings in effect. Do not reset
19293 -- the status in case the entity is reanalyzed with warnings off.
19294
19295 if Warnings and then not Is_Elaboration_Warnings_OK_Id (Id) then
19296 Set_Is_Elaboration_Warnings_OK_Id (Id, Elab_Warnings);
19297 end if;
19298 end Mark_Elaboration_Attributes_Id;
19299
19300 --------------------------------------
19301 -- Mark_Elaboration_Attributes_Node --
19302 --------------------------------------
19303
19304 procedure Mark_Elaboration_Attributes_Node (N : Node_Id) is
19305 function Extract_Name (N : Node_Id) return Node_Id;
19306 -- Obtain the Name attribute of call or instantiation N
19307
19308 ------------------
19309 -- Extract_Name --
19310 ------------------
19311
19312 function Extract_Name (N : Node_Id) return Node_Id is
19313 Nam : Node_Id;
19314
19315 begin
19316 Nam := Name (N);
19317
19318 -- A call to an entry family appears in indexed form
19319
19320 if Nkind (Nam) = N_Indexed_Component then
19321 Nam := Prefix (Nam);
19322 end if;
19323
19324 -- The name may also appear in qualified form
19325
19326 if Nkind (Nam) = N_Selected_Component then
19327 Nam := Selector_Name (Nam);
19328 end if;
19329
19330 return Nam;
19331 end Extract_Name;
19332
19333 -- Local variables
19334
19335 Context_Id : Entity_Id;
19336 Nam : Node_Id;
19337
19338 -- Start of processing for Mark_Elaboration_Attributes_Node
19339
19340 begin
19341 -- Mark the status of elaboration checks in effect. Do not reset the
19342 -- status in case the node is reanalyzed with checks suppressed.
19343
19344 if Checks and then not Is_Elaboration_Checks_OK_Node (N) then
19345
19346 -- Assignments, attribute references, and variable references do
19347 -- not have a "declarative" context.
19348
19349 Context_Id := Empty;
19350
19351 -- The status of elaboration checks for calls and instantiations
19352 -- depends on the most recent pragma Suppress/Unsuppress, as well
19353 -- as the suppression status of the context where the target is
19354 -- defined.
19355
19356 -- package Pack is
19357 -- function Func ...;
19358 -- end Pack;
19359
19360 -- with Pack;
19361 -- procedure Main is
19362 -- pragma Suppress (Elaboration_Checks, Pack);
19363 -- X : ... := Pack.Func;
19364 -- ...
19365
19366 -- In the example above, the call to Func has elaboration checks
19367 -- enabled because there is no active general purpose suppression
19368 -- pragma, however the elaboration checks of Pack are explicitly
19369 -- suppressed. As a result the elaboration checks of the call must
19370 -- be disabled in order to preserve this dependency.
19371
19372 if Nkind_In (N, N_Entry_Call_Statement,
19373 N_Function_Call,
19374 N_Function_Instantiation,
19375 N_Package_Instantiation,
19376 N_Procedure_Call_Statement,
19377 N_Procedure_Instantiation)
19378 then
19379 Nam := Extract_Name (N);
19380
19381 if Is_Entity_Name (Nam) and then Present (Entity (Nam)) then
19382 Context_Id := Scope (Entity (Nam));
19383 end if;
19384 end if;
19385
19386 Set_Is_Elaboration_Checks_OK_Node (N,
19387 Elaboration_Checks_OK
19388 (Target_Id => Empty,
19389 Context_Id => Context_Id));
19390 end if;
19391
19392 -- Mark the enclosing level of the node. Do not reset the status in
19393 -- case the node is relocated and reanalyzed.
19394
19395 if Level and then not Is_Declaration_Level_Node (N) then
19396 Set_Is_Declaration_Level_Node (N,
19397 Find_Enclosing_Level (N) = Declaration_Level);
19398 end if;
19399
19400 -- Mark the Ghost and SPARK mode in effect
19401
19402 if Modes then
19403 if Ghost_Mode = Ignore then
19404 Set_Is_Ignored_Ghost_Node (N);
19405 end if;
19406
19407 if SPARK_Mode = On then
19408 Set_Is_SPARK_Mode_On_Node (N);
19409 end if;
19410 end if;
19411
19412 -- Mark the status of elaboration warnings in effect. Do not reset
19413 -- the status in case the node is reanalyzed with warnings off.
19414
19415 if Warnings and then not Is_Elaboration_Warnings_OK_Node (N) then
19416 Set_Is_Elaboration_Warnings_OK_Node (N, Elab_Warnings);
19417 end if;
19418 end Mark_Elaboration_Attributes_Node;
19419
19420 -- Start of processing for Mark_Elaboration_Attributes
19421
19422 begin
19423 -- Do not capture any elaboration-related attributes when switch -gnatH
19424 -- (legacy elaboration checking mode enabled) is in effect because the
19425 -- attributes are useless to the legacy model.
19426
19427 if Legacy_Elaboration_Checks then
19428 return;
19429 end if;
19430
19431 if Nkind (N_Id) in N_Entity then
19432 Mark_Elaboration_Attributes_Id (N_Id);
19433 else
19434 Mark_Elaboration_Attributes_Node (N_Id);
19435 end if;
19436 end Mark_Elaboration_Attributes;
19437
19438 ----------------------------------------
19439 -- Mark_Save_Invocation_Graph_Of_Body --
19440 ----------------------------------------
19441
19442 procedure Mark_Save_Invocation_Graph_Of_Body is
19443 Main : constant Node_Id := Cunit (Main_Unit);
19444 Main_Unit : constant Node_Id := Unit (Main);
19445 Aux_Id : Entity_Id;
19446
19447 begin
19448 Set_Save_Invocation_Graph_Of_Body (Main);
19449
19450 -- Assume that the main unit does not have a complimentary unit
19451
19452 Aux_Id := Empty;
19453
19454 -- Obtain the complimentary unit of the main unit
19455
19456 if Nkind_In (Main_Unit, N_Generic_Package_Declaration,
19457 N_Generic_Subprogram_Declaration,
19458 N_Package_Declaration,
19459 N_Subprogram_Declaration)
19460 then
19461 Aux_Id := Corresponding_Body (Main_Unit);
19462
19463 elsif Nkind_In (Main_Unit, N_Package_Body,
19464 N_Subprogram_Body,
19465 N_Subprogram_Renaming_Declaration)
19466 then
19467 Aux_Id := Corresponding_Spec (Main_Unit);
19468 end if;
19469
19470 if Present (Aux_Id) then
19471 Set_Save_Invocation_Graph_Of_Body
19472 (Parent (Unit_Declaration_Node (Aux_Id)));
19473 end if;
19474 end Mark_Save_Invocation_Graph_Of_Body;
19475
19476 ----------------------------------
19477 -- Matching_Static_Array_Bounds --
19478 ----------------------------------
19479
19480 function Matching_Static_Array_Bounds
19481 (L_Typ : Node_Id;
19482 R_Typ : Node_Id) return Boolean
19483 is
19484 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
19485 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
19486
19487 L_Index : Node_Id := Empty; -- init to ...
19488 R_Index : Node_Id := Empty; -- ...avoid warnings
19489 L_Low : Node_Id;
19490 L_High : Node_Id;
19491 L_Len : Uint;
19492 R_Low : Node_Id;
19493 R_High : Node_Id;
19494 R_Len : Uint;
19495
19496 begin
19497 if L_Ndims /= R_Ndims then
19498 return False;
19499 end if;
19500
19501 -- Unconstrained types do not have static bounds
19502
19503 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
19504 return False;
19505 end if;
19506
19507 -- First treat specially the first dimension, as the lower bound and
19508 -- length of string literals are not stored like those of arrays.
19509
19510 if Ekind (L_Typ) = E_String_Literal_Subtype then
19511 L_Low := String_Literal_Low_Bound (L_Typ);
19512 L_Len := String_Literal_Length (L_Typ);
19513 else
19514 L_Index := First_Index (L_Typ);
19515 Get_Index_Bounds (L_Index, L_Low, L_High);
19516
19517 if Is_OK_Static_Expression (L_Low)
19518 and then
19519 Is_OK_Static_Expression (L_High)
19520 then
19521 if Expr_Value (L_High) < Expr_Value (L_Low) then
19522 L_Len := Uint_0;
19523 else
19524 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
19525 end if;
19526 else
19527 return False;
19528 end if;
19529 end if;
19530
19531 if Ekind (R_Typ) = E_String_Literal_Subtype then
19532 R_Low := String_Literal_Low_Bound (R_Typ);
19533 R_Len := String_Literal_Length (R_Typ);
19534 else
19535 R_Index := First_Index (R_Typ);
19536 Get_Index_Bounds (R_Index, R_Low, R_High);
19537
19538 if Is_OK_Static_Expression (R_Low)
19539 and then
19540 Is_OK_Static_Expression (R_High)
19541 then
19542 if Expr_Value (R_High) < Expr_Value (R_Low) then
19543 R_Len := Uint_0;
19544 else
19545 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
19546 end if;
19547 else
19548 return False;
19549 end if;
19550 end if;
19551
19552 if (Is_OK_Static_Expression (L_Low)
19553 and then
19554 Is_OK_Static_Expression (R_Low))
19555 and then Expr_Value (L_Low) = Expr_Value (R_Low)
19556 and then L_Len = R_Len
19557 then
19558 null;
19559 else
19560 return False;
19561 end if;
19562
19563 -- Then treat all other dimensions
19564
19565 for Indx in 2 .. L_Ndims loop
19566 Next (L_Index);
19567 Next (R_Index);
19568
19569 Get_Index_Bounds (L_Index, L_Low, L_High);
19570 Get_Index_Bounds (R_Index, R_Low, R_High);
19571
19572 if (Is_OK_Static_Expression (L_Low) and then
19573 Is_OK_Static_Expression (L_High) and then
19574 Is_OK_Static_Expression (R_Low) and then
19575 Is_OK_Static_Expression (R_High))
19576 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
19577 and then
19578 Expr_Value (L_High) = Expr_Value (R_High))
19579 then
19580 null;
19581 else
19582 return False;
19583 end if;
19584 end loop;
19585
19586 -- If we fall through the loop, all indexes matched
19587
19588 return True;
19589 end Matching_Static_Array_Bounds;
19590
19591 -------------------
19592 -- May_Be_Lvalue --
19593 -------------------
19594
19595 function May_Be_Lvalue (N : Node_Id) return Boolean is
19596 P : constant Node_Id := Parent (N);
19597
19598 begin
19599 case Nkind (P) is
19600
19601 -- Test left side of assignment
19602
19603 when N_Assignment_Statement =>
19604 return N = Name (P);
19605
19606 -- Test prefix of component or attribute. Note that the prefix of an
19607 -- explicit or implicit dereference cannot be an l-value. In the case
19608 -- of a 'Read attribute, the reference can be an actual in the
19609 -- argument list of the attribute.
19610
19611 when N_Attribute_Reference =>
19612 return (N = Prefix (P)
19613 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P)))
19614 or else
19615 Attribute_Name (P) = Name_Read;
19616
19617 -- For an expanded name, the name is an lvalue if the expanded name
19618 -- is an lvalue, but the prefix is never an lvalue, since it is just
19619 -- the scope where the name is found.
19620
19621 when N_Expanded_Name =>
19622 if N = Prefix (P) then
19623 return May_Be_Lvalue (P);
19624 else
19625 return False;
19626 end if;
19627
19628 -- For a selected component A.B, A is certainly an lvalue if A.B is.
19629 -- B is a little interesting, if we have A.B := 3, there is some
19630 -- discussion as to whether B is an lvalue or not, we choose to say
19631 -- it is. Note however that A is not an lvalue if it is of an access
19632 -- type since this is an implicit dereference.
19633
19634 when N_Selected_Component =>
19635 if N = Prefix (P)
19636 and then Present (Etype (N))
19637 and then Is_Access_Type (Etype (N))
19638 then
19639 return False;
19640 else
19641 return May_Be_Lvalue (P);
19642 end if;
19643
19644 -- For an indexed component or slice, the index or slice bounds is
19645 -- never an lvalue. The prefix is an lvalue if the indexed component
19646 -- or slice is an lvalue, except if it is an access type, where we
19647 -- have an implicit dereference.
19648
19649 when N_Indexed_Component
19650 | N_Slice
19651 =>
19652 if N /= Prefix (P)
19653 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
19654 then
19655 return False;
19656 else
19657 return May_Be_Lvalue (P);
19658 end if;
19659
19660 -- Prefix of a reference is an lvalue if the reference is an lvalue
19661
19662 when N_Reference =>
19663 return May_Be_Lvalue (P);
19664
19665 -- Prefix of explicit dereference is never an lvalue
19666
19667 when N_Explicit_Dereference =>
19668 return False;
19669
19670 -- Positional parameter for subprogram, entry, or accept call.
19671 -- In older versions of Ada function call arguments are never
19672 -- lvalues. In Ada 2012 functions can have in-out parameters.
19673
19674 when N_Accept_Statement
19675 | N_Entry_Call_Statement
19676 | N_Subprogram_Call
19677 =>
19678 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
19679 return False;
19680 end if;
19681
19682 -- The following mechanism is clumsy and fragile. A single flag
19683 -- set in Resolve_Actuals would be preferable ???
19684
19685 declare
19686 Proc : Entity_Id;
19687 Form : Entity_Id;
19688 Act : Node_Id;
19689
19690 begin
19691 Proc := Get_Subprogram_Entity (P);
19692
19693 if No (Proc) then
19694 return True;
19695 end if;
19696
19697 -- If we are not a list member, something is strange, so be
19698 -- conservative and return True.
19699
19700 if not Is_List_Member (N) then
19701 return True;
19702 end if;
19703
19704 -- We are going to find the right formal by stepping forward
19705 -- through the formals, as we step backwards in the actuals.
19706
19707 Form := First_Formal (Proc);
19708 Act := N;
19709 loop
19710 -- If no formal, something is weird, so be conservative and
19711 -- return True.
19712
19713 if No (Form) then
19714 return True;
19715 end if;
19716
19717 Prev (Act);
19718 exit when No (Act);
19719 Next_Formal (Form);
19720 end loop;
19721
19722 return Ekind (Form) /= E_In_Parameter;
19723 end;
19724
19725 -- Named parameter for procedure or accept call
19726
19727 when N_Parameter_Association =>
19728 declare
19729 Proc : Entity_Id;
19730 Form : Entity_Id;
19731
19732 begin
19733 Proc := Get_Subprogram_Entity (Parent (P));
19734
19735 if No (Proc) then
19736 return True;
19737 end if;
19738
19739 -- Loop through formals to find the one that matches
19740
19741 Form := First_Formal (Proc);
19742 loop
19743 -- If no matching formal, that's peculiar, some kind of
19744 -- previous error, so return True to be conservative.
19745 -- Actually happens with legal code for an unresolved call
19746 -- where we may get the wrong homonym???
19747
19748 if No (Form) then
19749 return True;
19750 end if;
19751
19752 -- Else test for match
19753
19754 if Chars (Form) = Chars (Selector_Name (P)) then
19755 return Ekind (Form) /= E_In_Parameter;
19756 end if;
19757
19758 Next_Formal (Form);
19759 end loop;
19760 end;
19761
19762 -- Test for appearing in a conversion that itself appears in an
19763 -- lvalue context, since this should be an lvalue.
19764
19765 when N_Type_Conversion =>
19766 return May_Be_Lvalue (P);
19767
19768 -- Test for appearance in object renaming declaration
19769
19770 when N_Object_Renaming_Declaration =>
19771 return True;
19772
19773 -- All other references are definitely not lvalues
19774
19775 when others =>
19776 return False;
19777 end case;
19778 end May_Be_Lvalue;
19779
19780 -----------------
19781 -- Might_Raise --
19782 -----------------
19783
19784 function Might_Raise (N : Node_Id) return Boolean is
19785 Result : Boolean := False;
19786
19787 function Process (N : Node_Id) return Traverse_Result;
19788 -- Set Result to True if we find something that could raise an exception
19789
19790 -------------
19791 -- Process --
19792 -------------
19793
19794 function Process (N : Node_Id) return Traverse_Result is
19795 begin
19796 if Nkind_In (N, N_Procedure_Call_Statement,
19797 N_Function_Call,
19798 N_Raise_Statement,
19799 N_Raise_Constraint_Error,
19800 N_Raise_Program_Error,
19801 N_Raise_Storage_Error)
19802 then
19803 Result := True;
19804 return Abandon;
19805 else
19806 return OK;
19807 end if;
19808 end Process;
19809
19810 procedure Set_Result is new Traverse_Proc (Process);
19811
19812 -- Start of processing for Might_Raise
19813
19814 begin
19815 -- False if exceptions can't be propagated
19816
19817 if No_Exception_Handlers_Set then
19818 return False;
19819 end if;
19820
19821 -- If the checks handled by the back end are not disabled, we cannot
19822 -- ensure that no exception will be raised.
19823
19824 if not Access_Checks_Suppressed (Empty)
19825 or else not Discriminant_Checks_Suppressed (Empty)
19826 or else not Range_Checks_Suppressed (Empty)
19827 or else not Index_Checks_Suppressed (Empty)
19828 or else Opt.Stack_Checking_Enabled
19829 then
19830 return True;
19831 end if;
19832
19833 Set_Result (N);
19834 return Result;
19835 end Might_Raise;
19836
19837 --------------------------------
19838 -- Nearest_Enclosing_Instance --
19839 --------------------------------
19840
19841 function Nearest_Enclosing_Instance (E : Entity_Id) return Entity_Id is
19842 Inst : Entity_Id;
19843
19844 begin
19845 Inst := Scope (E);
19846 while Present (Inst) and then Inst /= Standard_Standard loop
19847 if Is_Generic_Instance (Inst) then
19848 return Inst;
19849 end if;
19850
19851 Inst := Scope (Inst);
19852 end loop;
19853
19854 return Empty;
19855 end Nearest_Enclosing_Instance;
19856
19857 ------------------------
19858 -- Needs_Finalization --
19859 ------------------------
19860
19861 function Needs_Finalization (Typ : Entity_Id) return Boolean is
19862 function Has_Some_Controlled_Component
19863 (Input_Typ : Entity_Id) return Boolean;
19864 -- Determine whether type Input_Typ has at least one controlled
19865 -- component.
19866
19867 -----------------------------------
19868 -- Has_Some_Controlled_Component --
19869 -----------------------------------
19870
19871 function Has_Some_Controlled_Component
19872 (Input_Typ : Entity_Id) return Boolean
19873 is
19874 Comp : Entity_Id;
19875
19876 begin
19877 -- When a type is already frozen and has at least one controlled
19878 -- component, or is manually decorated, it is sufficient to inspect
19879 -- flag Has_Controlled_Component.
19880
19881 if Has_Controlled_Component (Input_Typ) then
19882 return True;
19883
19884 -- Otherwise inspect the internals of the type
19885
19886 elsif not Is_Frozen (Input_Typ) then
19887 if Is_Array_Type (Input_Typ) then
19888 return Needs_Finalization (Component_Type (Input_Typ));
19889
19890 elsif Is_Record_Type (Input_Typ) then
19891 Comp := First_Component (Input_Typ);
19892 while Present (Comp) loop
19893 if Needs_Finalization (Etype (Comp)) then
19894 return True;
19895 end if;
19896
19897 Next_Component (Comp);
19898 end loop;
19899 end if;
19900 end if;
19901
19902 return False;
19903 end Has_Some_Controlled_Component;
19904
19905 -- Start of processing for Needs_Finalization
19906
19907 begin
19908 -- Certain run-time configurations and targets do not provide support
19909 -- for controlled types.
19910
19911 if Restriction_Active (No_Finalization) then
19912 return False;
19913
19914 -- C++ types are not considered controlled. It is assumed that the non-
19915 -- Ada side will handle their clean up.
19916
19917 elsif Convention (Typ) = Convention_CPP then
19918 return False;
19919
19920 -- Class-wide types are treated as controlled because derivations from
19921 -- the root type may introduce controlled components.
19922
19923 elsif Is_Class_Wide_Type (Typ) then
19924 return True;
19925
19926 -- Concurrent types are controlled as long as their corresponding record
19927 -- is controlled.
19928
19929 elsif Is_Concurrent_Type (Typ)
19930 and then Present (Corresponding_Record_Type (Typ))
19931 and then Needs_Finalization (Corresponding_Record_Type (Typ))
19932 then
19933 return True;
19934
19935 -- Otherwise the type is controlled when it is either derived from type
19936 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
19937 -- contains at least one controlled component.
19938
19939 else
19940 return
19941 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
19942 end if;
19943 end Needs_Finalization;
19944
19945 ----------------------
19946 -- Needs_One_Actual --
19947 ----------------------
19948
19949 function Needs_One_Actual (E : Entity_Id) return Boolean is
19950 Formal : Entity_Id;
19951
19952 begin
19953 -- Ada 2005 or later, and formals present. The first formal must be
19954 -- of a type that supports prefix notation: a controlling argument,
19955 -- a class-wide type, or an access to such.
19956
19957 if Ada_Version >= Ada_2005
19958 and then Present (First_Formal (E))
19959 and then No (Default_Value (First_Formal (E)))
19960 and then
19961 (Is_Controlling_Formal (First_Formal (E))
19962 or else Is_Class_Wide_Type (Etype (First_Formal (E)))
19963 or else Is_Anonymous_Access_Type (Etype (First_Formal (E))))
19964 then
19965 Formal := Next_Formal (First_Formal (E));
19966 while Present (Formal) loop
19967 if No (Default_Value (Formal)) then
19968 return False;
19969 end if;
19970
19971 Next_Formal (Formal);
19972 end loop;
19973
19974 return True;
19975
19976 -- Ada 83/95 or no formals
19977
19978 else
19979 return False;
19980 end if;
19981 end Needs_One_Actual;
19982
19983 ---------------------------------
19984 -- Needs_Simple_Initialization --
19985 ---------------------------------
19986
19987 function Needs_Simple_Initialization
19988 (Typ : Entity_Id;
19989 Consider_IS : Boolean := True) return Boolean
19990 is
19991 Consider_IS_NS : constant Boolean :=
19992 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
19993
19994 begin
19995 -- Never need initialization if it is suppressed
19996
19997 if Initialization_Suppressed (Typ) then
19998 return False;
19999 end if;
20000
20001 -- Check for private type, in which case test applies to the underlying
20002 -- type of the private type.
20003
20004 if Is_Private_Type (Typ) then
20005 declare
20006 RT : constant Entity_Id := Underlying_Type (Typ);
20007 begin
20008 if Present (RT) then
20009 return Needs_Simple_Initialization (RT);
20010 else
20011 return False;
20012 end if;
20013 end;
20014
20015 -- Scalar type with Default_Value aspect requires initialization
20016
20017 elsif Is_Scalar_Type (Typ) and then Has_Default_Aspect (Typ) then
20018 return True;
20019
20020 -- Cases needing simple initialization are access types, and, if pragma
20021 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
20022 -- types.
20023
20024 elsif Is_Access_Type (Typ)
20025 or else (Consider_IS_NS and then (Is_Scalar_Type (Typ)))
20026 then
20027 return True;
20028
20029 -- If Initialize/Normalize_Scalars is in effect, string objects also
20030 -- need initialization, unless they are created in the course of
20031 -- expanding an aggregate (since in the latter case they will be
20032 -- filled with appropriate initializing values before they are used).
20033
20034 elsif Consider_IS_NS
20035 and then Is_Standard_String_Type (Typ)
20036 and then
20037 (not Is_Itype (Typ)
20038 or else Nkind (Associated_Node_For_Itype (Typ)) /= N_Aggregate)
20039 then
20040 return True;
20041
20042 else
20043 return False;
20044 end if;
20045 end Needs_Simple_Initialization;
20046
20047 -------------------------------------
20048 -- Needs_Variable_Reference_Marker --
20049 -------------------------------------
20050
20051 function Needs_Variable_Reference_Marker
20052 (N : Node_Id;
20053 Calls_OK : Boolean) return Boolean
20054 is
20055 function Within_Suitable_Context (Ref : Node_Id) return Boolean;
20056 -- Deteremine whether variable reference Ref appears within a suitable
20057 -- context that allows the creation of a marker.
20058
20059 -----------------------------
20060 -- Within_Suitable_Context --
20061 -----------------------------
20062
20063 function Within_Suitable_Context (Ref : Node_Id) return Boolean is
20064 Par : Node_Id;
20065
20066 begin
20067 Par := Ref;
20068 while Present (Par) loop
20069
20070 -- The context is not suitable when the reference appears within
20071 -- the formal part of an instantiation which acts as compilation
20072 -- unit because there is no proper list for the insertion of the
20073 -- marker.
20074
20075 if Nkind (Par) = N_Generic_Association
20076 and then Nkind (Parent (Par)) in N_Generic_Instantiation
20077 and then Nkind (Parent (Parent (Par))) = N_Compilation_Unit
20078 then
20079 return False;
20080
20081 -- The context is not suitable when the reference appears within
20082 -- a pragma. If the pragma has run-time semantics, the reference
20083 -- will be reconsidered once the pragma is expanded.
20084
20085 elsif Nkind (Par) = N_Pragma then
20086 return False;
20087
20088 -- The context is not suitable when the reference appears within a
20089 -- subprogram call, and the caller requests this behavior.
20090
20091 elsif not Calls_OK
20092 and then Nkind_In (Par, N_Entry_Call_Statement,
20093 N_Function_Call,
20094 N_Procedure_Call_Statement)
20095 then
20096 return False;
20097
20098 -- Prevent the search from going too far
20099
20100 elsif Is_Body_Or_Package_Declaration (Par) then
20101 exit;
20102 end if;
20103
20104 Par := Parent (Par);
20105 end loop;
20106
20107 return True;
20108 end Within_Suitable_Context;
20109
20110 -- Local variables
20111
20112 Prag : Node_Id;
20113 Var_Id : Entity_Id;
20114
20115 -- Start of processing for Needs_Variable_Reference_Marker
20116
20117 begin
20118 -- No marker needs to be created when switch -gnatH (legacy elaboration
20119 -- checking mode enabled) is in effect because the legacy ABE mechanism
20120 -- does not use markers.
20121
20122 if Legacy_Elaboration_Checks then
20123 return False;
20124
20125 -- No marker needs to be created when the reference is preanalyzed
20126 -- because the marker will be inserted in the wrong place.
20127
20128 elsif Preanalysis_Active then
20129 return False;
20130
20131 -- Only references warrant a marker
20132
20133 elsif not Nkind_In (N, N_Expanded_Name, N_Identifier) then
20134 return False;
20135
20136 -- Only source references warrant a marker
20137
20138 elsif not Comes_From_Source (N) then
20139 return False;
20140
20141 -- No marker needs to be created when the reference is erroneous, left
20142 -- in a bad state, or does not denote a variable.
20143
20144 elsif not (Present (Entity (N))
20145 and then Ekind (Entity (N)) = E_Variable
20146 and then Entity (N) /= Any_Id)
20147 then
20148 return False;
20149 end if;
20150
20151 Var_Id := Entity (N);
20152 Prag := SPARK_Pragma (Var_Id);
20153
20154 -- Both the variable and reference must appear in SPARK_Mode On regions
20155 -- because this elaboration scenario falls under the SPARK rules.
20156
20157 if not (Comes_From_Source (Var_Id)
20158 and then Present (Prag)
20159 and then Get_SPARK_Mode_From_Annotation (Prag) = On
20160 and then Is_SPARK_Mode_On_Node (N))
20161 then
20162 return False;
20163
20164 -- No marker needs to be created when the reference does not appear
20165 -- within a suitable context (see body for details).
20166
20167 -- Performance note: parent traversal
20168
20169 elsif not Within_Suitable_Context (N) then
20170 return False;
20171 end if;
20172
20173 -- At this point it is known that the variable reference will play a
20174 -- role in ABE diagnostics and requires a marker.
20175
20176 return True;
20177 end Needs_Variable_Reference_Marker;
20178
20179 ------------------------
20180 -- New_Copy_List_Tree --
20181 ------------------------
20182
20183 function New_Copy_List_Tree (List : List_Id) return List_Id is
20184 NL : List_Id;
20185 E : Node_Id;
20186
20187 begin
20188 if List = No_List then
20189 return No_List;
20190
20191 else
20192 NL := New_List;
20193 E := First (List);
20194
20195 while Present (E) loop
20196 Append (New_Copy_Tree (E), NL);
20197 E := Next (E);
20198 end loop;
20199
20200 return NL;
20201 end if;
20202 end New_Copy_List_Tree;
20203
20204 -------------------
20205 -- New_Copy_Tree --
20206 -------------------
20207
20208 -- The following tables play a key role in replicating entities and Itypes.
20209 -- They are intentionally declared at the library level rather than within
20210 -- New_Copy_Tree to avoid elaborating them on each call. This performance
20211 -- optimization saves up to 2% of the entire compilation time spent in the
20212 -- front end. Care should be taken to reset the tables on each new call to
20213 -- New_Copy_Tree.
20214
20215 NCT_Table_Max : constant := 511;
20216
20217 subtype NCT_Table_Index is Nat range 0 .. NCT_Table_Max - 1;
20218
20219 function NCT_Table_Hash (Key : Node_Or_Entity_Id) return NCT_Table_Index;
20220 -- Obtain the hash value of node or entity Key
20221
20222 --------------------
20223 -- NCT_Table_Hash --
20224 --------------------
20225
20226 function NCT_Table_Hash (Key : Node_Or_Entity_Id) return NCT_Table_Index is
20227 begin
20228 return NCT_Table_Index (Key mod NCT_Table_Max);
20229 end NCT_Table_Hash;
20230
20231 ----------------------
20232 -- NCT_New_Entities --
20233 ----------------------
20234
20235 -- The following table maps old entities and Itypes to their corresponding
20236 -- new entities and Itypes.
20237
20238 -- Aaa -> Xxx
20239
20240 package NCT_New_Entities is new Simple_HTable (
20241 Header_Num => NCT_Table_Index,
20242 Element => Entity_Id,
20243 No_Element => Empty,
20244 Key => Entity_Id,
20245 Hash => NCT_Table_Hash,
20246 Equal => "=");
20247
20248 ------------------------
20249 -- NCT_Pending_Itypes --
20250 ------------------------
20251
20252 -- The following table maps old Associated_Node_For_Itype nodes to a set of
20253 -- new itypes. Given a set of old Itypes Aaa, Bbb, and Ccc, where all three
20254 -- have the same Associated_Node_For_Itype Ppp, and their corresponding new
20255 -- Itypes Xxx, Yyy, Zzz, the table contains the following mapping:
20256
20257 -- Ppp -> (Xxx, Yyy, Zzz)
20258
20259 -- The set is expressed as an Elist
20260
20261 package NCT_Pending_Itypes is new Simple_HTable (
20262 Header_Num => NCT_Table_Index,
20263 Element => Elist_Id,
20264 No_Element => No_Elist,
20265 Key => Node_Id,
20266 Hash => NCT_Table_Hash,
20267 Equal => "=");
20268
20269 NCT_Tables_In_Use : Boolean := False;
20270 -- This flag keeps track of whether the two tables NCT_New_Entities and
20271 -- NCT_Pending_Itypes are in use. The flag is part of an optimization
20272 -- where certain operations are not performed if the tables are not in
20273 -- use. This saves up to 8% of the entire compilation time spent in the
20274 -- front end.
20275
20276 -------------------
20277 -- New_Copy_Tree --
20278 -------------------
20279
20280 function New_Copy_Tree
20281 (Source : Node_Id;
20282 Map : Elist_Id := No_Elist;
20283 New_Sloc : Source_Ptr := No_Location;
20284 New_Scope : Entity_Id := Empty;
20285 Scopes_In_EWA_OK : Boolean := False) return Node_Id
20286 is
20287 -- This routine performs low-level tree manipulations and needs access
20288 -- to the internals of the tree.
20289
20290 use Atree.Unchecked_Access;
20291 use Atree_Private_Part;
20292
20293 EWA_Level : Nat := 0;
20294 -- This counter keeps track of how many N_Expression_With_Actions nodes
20295 -- are encountered during a depth-first traversal of the subtree. These
20296 -- nodes may define new entities in their Actions lists and thus require
20297 -- special processing.
20298
20299 EWA_Inner_Scope_Level : Nat := 0;
20300 -- This counter keeps track of how many scoping constructs appear within
20301 -- an N_Expression_With_Actions node.
20302
20303 procedure Add_New_Entity (Old_Id : Entity_Id; New_Id : Entity_Id);
20304 pragma Inline (Add_New_Entity);
20305 -- Add an entry in the NCT_New_Entities table which maps key Old_Id to
20306 -- value New_Id. Old_Id is an entity which appears within the Actions
20307 -- list of an N_Expression_With_Actions node, or within an entity map.
20308 -- New_Id is the corresponding new entity generated during Phase 1.
20309
20310 procedure Add_Pending_Itype (Assoc_Nod : Node_Id; Itype : Entity_Id);
20311 pragma Inline (Add_New_Entity);
20312 -- Add an entry in the NCT_Pending_Itypes which maps key Assoc_Nod to
20313 -- value Itype. Assoc_Nod is the associated node of an itype. Itype is
20314 -- an itype.
20315
20316 procedure Build_NCT_Tables (Entity_Map : Elist_Id);
20317 pragma Inline (Build_NCT_Tables);
20318 -- Populate tables NCT_New_Entities and NCT_Pending_Itypes with the
20319 -- information supplied in entity map Entity_Map. The format of the
20320 -- entity map must be as follows:
20321 --
20322 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
20323
20324 function Copy_Any_Node_With_Replacement
20325 (N : Node_Or_Entity_Id) return Node_Or_Entity_Id;
20326 pragma Inline (Copy_Any_Node_With_Replacement);
20327 -- Replicate entity or node N by invoking one of the following routines:
20328 --
20329 -- Copy_Node_With_Replacement
20330 -- Corresponding_Entity
20331
20332 function Copy_Elist_With_Replacement (List : Elist_Id) return Elist_Id;
20333 -- Replicate the elements of entity list List
20334
20335 function Copy_Field_With_Replacement
20336 (Field : Union_Id;
20337 Old_Par : Node_Id := Empty;
20338 New_Par : Node_Id := Empty;
20339 Semantic : Boolean := False) return Union_Id;
20340 -- Replicate field Field by invoking one of the following routines:
20341 --
20342 -- Copy_Elist_With_Replacement
20343 -- Copy_List_With_Replacement
20344 -- Copy_Node_With_Replacement
20345 -- Corresponding_Entity
20346 --
20347 -- If the field is not an entity list, entity, itype, syntactic list,
20348 -- or node, then the field is returned unchanged. The routine always
20349 -- replicates entities, itypes, and valid syntactic fields. Old_Par is
20350 -- the expected parent of a syntactic field. New_Par is the new parent
20351 -- associated with a replicated syntactic field. Flag Semantic should
20352 -- be set when the input is a semantic field.
20353
20354 function Copy_List_With_Replacement (List : List_Id) return List_Id;
20355 -- Replicate the elements of syntactic list List
20356
20357 function Copy_Node_With_Replacement (N : Node_Id) return Node_Id;
20358 -- Replicate node N
20359
20360 function Corresponding_Entity (Id : Entity_Id) return Entity_Id;
20361 pragma Inline (Corresponding_Entity);
20362 -- Return the corresponding new entity of Id generated during Phase 1.
20363 -- If there is no such entity, return Id.
20364
20365 function In_Entity_Map
20366 (Id : Entity_Id;
20367 Entity_Map : Elist_Id) return Boolean;
20368 pragma Inline (In_Entity_Map);
20369 -- Determine whether entity Id is one of the old ids specified in entity
20370 -- map Entity_Map. The format of the entity map must be as follows:
20371 --
20372 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
20373
20374 procedure Update_CFS_Sloc (N : Node_Or_Entity_Id);
20375 pragma Inline (Update_CFS_Sloc);
20376 -- Update the Comes_From_Source and Sloc attributes of node or entity N
20377
20378 procedure Update_First_Real_Statement
20379 (Old_HSS : Node_Id;
20380 New_HSS : Node_Id);
20381 pragma Inline (Update_First_Real_Statement);
20382 -- Update semantic attribute First_Real_Statement of handled sequence of
20383 -- statements New_HSS based on handled sequence of statements Old_HSS.
20384
20385 procedure Update_Named_Associations
20386 (Old_Call : Node_Id;
20387 New_Call : Node_Id);
20388 pragma Inline (Update_Named_Associations);
20389 -- Update semantic chain First/Next_Named_Association of call New_call
20390 -- based on call Old_Call.
20391
20392 procedure Update_New_Entities (Entity_Map : Elist_Id);
20393 pragma Inline (Update_New_Entities);
20394 -- Update the semantic attributes of all new entities generated during
20395 -- Phase 1 that do not appear in entity map Entity_Map. The format of
20396 -- the entity map must be as follows:
20397 --
20398 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
20399
20400 procedure Update_Pending_Itypes
20401 (Old_Assoc : Node_Id;
20402 New_Assoc : Node_Id);
20403 pragma Inline (Update_Pending_Itypes);
20404 -- Update semantic attribute Associated_Node_For_Itype to refer to node
20405 -- New_Assoc for all itypes whose associated node is Old_Assoc.
20406
20407 procedure Update_Semantic_Fields (Id : Entity_Id);
20408 pragma Inline (Update_Semantic_Fields);
20409 -- Subsidiary to Update_New_Entities. Update semantic fields of entity
20410 -- or itype Id.
20411
20412 procedure Visit_Any_Node (N : Node_Or_Entity_Id);
20413 pragma Inline (Visit_Any_Node);
20414 -- Visit entity of node N by invoking one of the following routines:
20415 --
20416 -- Visit_Entity
20417 -- Visit_Itype
20418 -- Visit_Node
20419
20420 procedure Visit_Elist (List : Elist_Id);
20421 -- Visit the elements of entity list List
20422
20423 procedure Visit_Entity (Id : Entity_Id);
20424 -- Visit entity Id. This action may create a new entity of Id and save
20425 -- it in table NCT_New_Entities.
20426
20427 procedure Visit_Field
20428 (Field : Union_Id;
20429 Par_Nod : Node_Id := Empty;
20430 Semantic : Boolean := False);
20431 -- Visit field Field by invoking one of the following routines:
20432 --
20433 -- Visit_Elist
20434 -- Visit_Entity
20435 -- Visit_Itype
20436 -- Visit_List
20437 -- Visit_Node
20438 --
20439 -- If the field is not an entity list, entity, itype, syntactic list,
20440 -- or node, then the field is not visited. The routine always visits
20441 -- valid syntactic fields. Par_Nod is the expected parent of the
20442 -- syntactic field. Flag Semantic should be set when the input is a
20443 -- semantic field.
20444
20445 procedure Visit_Itype (Itype : Entity_Id);
20446 -- Visit itype Itype. This action may create a new entity for Itype and
20447 -- save it in table NCT_New_Entities. In addition, the routine may map
20448 -- the associated node of Itype to the new itype in NCT_Pending_Itypes.
20449
20450 procedure Visit_List (List : List_Id);
20451 -- Visit the elements of syntactic list List
20452
20453 procedure Visit_Node (N : Node_Id);
20454 -- Visit node N
20455
20456 procedure Visit_Semantic_Fields (Id : Entity_Id);
20457 pragma Inline (Visit_Semantic_Fields);
20458 -- Subsidiary to Visit_Entity and Visit_Itype. Visit common semantic
20459 -- fields of entity or itype Id.
20460
20461 --------------------
20462 -- Add_New_Entity --
20463 --------------------
20464
20465 procedure Add_New_Entity (Old_Id : Entity_Id; New_Id : Entity_Id) is
20466 begin
20467 pragma Assert (Present (Old_Id));
20468 pragma Assert (Present (New_Id));
20469 pragma Assert (Nkind (Old_Id) in N_Entity);
20470 pragma Assert (Nkind (New_Id) in N_Entity);
20471
20472 NCT_Tables_In_Use := True;
20473
20474 -- Sanity check the NCT_New_Entities table. No previous mapping with
20475 -- key Old_Id should exist.
20476
20477 pragma Assert (No (NCT_New_Entities.Get (Old_Id)));
20478
20479 -- Establish the mapping
20480
20481 -- Old_Id -> New_Id
20482
20483 NCT_New_Entities.Set (Old_Id, New_Id);
20484 end Add_New_Entity;
20485
20486 -----------------------
20487 -- Add_Pending_Itype --
20488 -----------------------
20489
20490 procedure Add_Pending_Itype (Assoc_Nod : Node_Id; Itype : Entity_Id) is
20491 Itypes : Elist_Id;
20492
20493 begin
20494 pragma Assert (Present (Assoc_Nod));
20495 pragma Assert (Present (Itype));
20496 pragma Assert (Nkind (Itype) in N_Entity);
20497 pragma Assert (Is_Itype (Itype));
20498
20499 NCT_Tables_In_Use := True;
20500
20501 -- It is not possible to sanity check the NCT_Pendint_Itypes table
20502 -- directly because a single node may act as the associated node for
20503 -- multiple itypes.
20504
20505 Itypes := NCT_Pending_Itypes.Get (Assoc_Nod);
20506
20507 if No (Itypes) then
20508 Itypes := New_Elmt_List;
20509 NCT_Pending_Itypes.Set (Assoc_Nod, Itypes);
20510 end if;
20511
20512 -- Establish the mapping
20513
20514 -- Assoc_Nod -> (Itype, ...)
20515
20516 -- Avoid inserting the same itype multiple times. This involves a
20517 -- linear search, however the set of itypes with the same associated
20518 -- node is very small.
20519
20520 Append_Unique_Elmt (Itype, Itypes);
20521 end Add_Pending_Itype;
20522
20523 ----------------------
20524 -- Build_NCT_Tables --
20525 ----------------------
20526
20527 procedure Build_NCT_Tables (Entity_Map : Elist_Id) is
20528 Elmt : Elmt_Id;
20529 Old_Id : Entity_Id;
20530 New_Id : Entity_Id;
20531
20532 begin
20533 -- Nothing to do when there is no entity map
20534
20535 if No (Entity_Map) then
20536 return;
20537 end if;
20538
20539 Elmt := First_Elmt (Entity_Map);
20540 while Present (Elmt) loop
20541
20542 -- Extract the (Old_Id, New_Id) pair from the entity map
20543
20544 Old_Id := Node (Elmt);
20545 Next_Elmt (Elmt);
20546
20547 New_Id := Node (Elmt);
20548 Next_Elmt (Elmt);
20549
20550 -- Establish the following mapping within table NCT_New_Entities
20551
20552 -- Old_Id -> New_Id
20553
20554 Add_New_Entity (Old_Id, New_Id);
20555
20556 -- Establish the following mapping within table NCT_Pending_Itypes
20557 -- when the new entity is an itype.
20558
20559 -- Assoc_Nod -> (New_Id, ...)
20560
20561 -- IMPORTANT: the associated node is that of the old itype because
20562 -- the node will be replicated in Phase 2.
20563
20564 if Is_Itype (Old_Id) then
20565 Add_Pending_Itype
20566 (Assoc_Nod => Associated_Node_For_Itype (Old_Id),
20567 Itype => New_Id);
20568 end if;
20569 end loop;
20570 end Build_NCT_Tables;
20571
20572 ------------------------------------
20573 -- Copy_Any_Node_With_Replacement --
20574 ------------------------------------
20575
20576 function Copy_Any_Node_With_Replacement
20577 (N : Node_Or_Entity_Id) return Node_Or_Entity_Id
20578 is
20579 begin
20580 if Nkind (N) in N_Entity then
20581 return Corresponding_Entity (N);
20582 else
20583 return Copy_Node_With_Replacement (N);
20584 end if;
20585 end Copy_Any_Node_With_Replacement;
20586
20587 ---------------------------------
20588 -- Copy_Elist_With_Replacement --
20589 ---------------------------------
20590
20591 function Copy_Elist_With_Replacement (List : Elist_Id) return Elist_Id is
20592 Elmt : Elmt_Id;
20593 Result : Elist_Id;
20594
20595 begin
20596 -- Copy the contents of the old list. Note that the list itself may
20597 -- be empty, in which case the routine returns a new empty list. This
20598 -- avoids sharing lists between subtrees. The element of an entity
20599 -- list could be an entity or a node, hence the invocation of routine
20600 -- Copy_Any_Node_With_Replacement.
20601
20602 if Present (List) then
20603 Result := New_Elmt_List;
20604
20605 Elmt := First_Elmt (List);
20606 while Present (Elmt) loop
20607 Append_Elmt
20608 (Copy_Any_Node_With_Replacement (Node (Elmt)), Result);
20609
20610 Next_Elmt (Elmt);
20611 end loop;
20612
20613 -- Otherwise the list does not exist
20614
20615 else
20616 Result := No_Elist;
20617 end if;
20618
20619 return Result;
20620 end Copy_Elist_With_Replacement;
20621
20622 ---------------------------------
20623 -- Copy_Field_With_Replacement --
20624 ---------------------------------
20625
20626 function Copy_Field_With_Replacement
20627 (Field : Union_Id;
20628 Old_Par : Node_Id := Empty;
20629 New_Par : Node_Id := Empty;
20630 Semantic : Boolean := False) return Union_Id
20631 is
20632 begin
20633 -- The field is empty
20634
20635 if Field = Union_Id (Empty) then
20636 return Field;
20637
20638 -- The field is an entity/itype/node
20639
20640 elsif Field in Node_Range then
20641 declare
20642 Old_N : constant Node_Id := Node_Id (Field);
20643 Syntactic : constant Boolean := Parent (Old_N) = Old_Par;
20644
20645 New_N : Node_Id;
20646
20647 begin
20648 -- The field is an entity/itype
20649
20650 if Nkind (Old_N) in N_Entity then
20651
20652 -- An entity/itype is always replicated
20653
20654 New_N := Corresponding_Entity (Old_N);
20655
20656 -- Update the parent pointer when the entity is a syntactic
20657 -- field. Note that itypes do not have parent pointers.
20658
20659 if Syntactic and then New_N /= Old_N then
20660 Set_Parent (New_N, New_Par);
20661 end if;
20662
20663 -- The field is a node
20664
20665 else
20666 -- A node is replicated when it is either a syntactic field
20667 -- or when the caller treats it as a semantic attribute.
20668
20669 if Syntactic or else Semantic then
20670 New_N := Copy_Node_With_Replacement (Old_N);
20671
20672 -- Update the parent pointer when the node is a syntactic
20673 -- field.
20674
20675 if Syntactic and then New_N /= Old_N then
20676 Set_Parent (New_N, New_Par);
20677 end if;
20678
20679 -- Otherwise the node is returned unchanged
20680
20681 else
20682 New_N := Old_N;
20683 end if;
20684 end if;
20685
20686 return Union_Id (New_N);
20687 end;
20688
20689 -- The field is an entity list
20690
20691 elsif Field in Elist_Range then
20692 return Union_Id (Copy_Elist_With_Replacement (Elist_Id (Field)));
20693
20694 -- The field is a syntactic list
20695
20696 elsif Field in List_Range then
20697 declare
20698 Old_List : constant List_Id := List_Id (Field);
20699 Syntactic : constant Boolean := Parent (Old_List) = Old_Par;
20700
20701 New_List : List_Id;
20702
20703 begin
20704 -- A list is replicated when it is either a syntactic field or
20705 -- when the caller treats it as a semantic attribute.
20706
20707 if Syntactic or else Semantic then
20708 New_List := Copy_List_With_Replacement (Old_List);
20709
20710 -- Update the parent pointer when the list is a syntactic
20711 -- field.
20712
20713 if Syntactic and then New_List /= Old_List then
20714 Set_Parent (New_List, New_Par);
20715 end if;
20716
20717 -- Otherwise the list is returned unchanged
20718
20719 else
20720 New_List := Old_List;
20721 end if;
20722
20723 return Union_Id (New_List);
20724 end;
20725
20726 -- Otherwise the field denotes an attribute that does not need to be
20727 -- replicated (Chars, literals, etc).
20728
20729 else
20730 return Field;
20731 end if;
20732 end Copy_Field_With_Replacement;
20733
20734 --------------------------------
20735 -- Copy_List_With_Replacement --
20736 --------------------------------
20737
20738 function Copy_List_With_Replacement (List : List_Id) return List_Id is
20739 Elmt : Node_Id;
20740 Result : List_Id;
20741
20742 begin
20743 -- Copy the contents of the old list. Note that the list itself may
20744 -- be empty, in which case the routine returns a new empty list. This
20745 -- avoids sharing lists between subtrees. The element of a syntactic
20746 -- list is always a node, never an entity or itype, hence the call to
20747 -- routine Copy_Node_With_Replacement.
20748
20749 if Present (List) then
20750 Result := New_List;
20751
20752 Elmt := First (List);
20753 while Present (Elmt) loop
20754 Append (Copy_Node_With_Replacement (Elmt), Result);
20755
20756 Next (Elmt);
20757 end loop;
20758
20759 -- Otherwise the list does not exist
20760
20761 else
20762 Result := No_List;
20763 end if;
20764
20765 return Result;
20766 end Copy_List_With_Replacement;
20767
20768 --------------------------------
20769 -- Copy_Node_With_Replacement --
20770 --------------------------------
20771
20772 function Copy_Node_With_Replacement (N : Node_Id) return Node_Id is
20773 Result : Node_Id;
20774
20775 begin
20776 -- Assume that the node must be returned unchanged
20777
20778 Result := N;
20779
20780 if N > Empty_Or_Error then
20781 pragma Assert (Nkind (N) not in N_Entity);
20782
20783 Result := New_Copy (N);
20784
20785 Set_Field1 (Result,
20786 Copy_Field_With_Replacement
20787 (Field => Field1 (Result),
20788 Old_Par => N,
20789 New_Par => Result));
20790
20791 Set_Field2 (Result,
20792 Copy_Field_With_Replacement
20793 (Field => Field2 (Result),
20794 Old_Par => N,
20795 New_Par => Result));
20796
20797 Set_Field3 (Result,
20798 Copy_Field_With_Replacement
20799 (Field => Field3 (Result),
20800 Old_Par => N,
20801 New_Par => Result));
20802
20803 Set_Field4 (Result,
20804 Copy_Field_With_Replacement
20805 (Field => Field4 (Result),
20806 Old_Par => N,
20807 New_Par => Result));
20808
20809 Set_Field5 (Result,
20810 Copy_Field_With_Replacement
20811 (Field => Field5 (Result),
20812 Old_Par => N,
20813 New_Par => Result));
20814
20815 -- Update the Comes_From_Source and Sloc attributes of the node
20816 -- in case the caller has supplied new values.
20817
20818 Update_CFS_Sloc (Result);
20819
20820 -- Update the Associated_Node_For_Itype attribute of all itypes
20821 -- created during Phase 1 whose associated node is N. As a result
20822 -- the Associated_Node_For_Itype refers to the replicated node.
20823 -- No action needs to be taken when the Associated_Node_For_Itype
20824 -- refers to an entity because this was already handled during
20825 -- Phase 1, in Visit_Itype.
20826
20827 Update_Pending_Itypes
20828 (Old_Assoc => N,
20829 New_Assoc => Result);
20830
20831 -- Update the First/Next_Named_Association chain for a replicated
20832 -- call.
20833
20834 if Nkind_In (N, N_Entry_Call_Statement,
20835 N_Function_Call,
20836 N_Procedure_Call_Statement)
20837 then
20838 Update_Named_Associations
20839 (Old_Call => N,
20840 New_Call => Result);
20841
20842 -- Update the Renamed_Object attribute of a replicated object
20843 -- declaration.
20844
20845 elsif Nkind (N) = N_Object_Renaming_Declaration then
20846 Set_Renamed_Object (Defining_Entity (Result), Name (Result));
20847
20848 -- Update the First_Real_Statement attribute of a replicated
20849 -- handled sequence of statements.
20850
20851 elsif Nkind (N) = N_Handled_Sequence_Of_Statements then
20852 Update_First_Real_Statement
20853 (Old_HSS => N,
20854 New_HSS => Result);
20855
20856 -- Update the Chars attribute of identifiers
20857
20858 elsif Nkind (N) = N_Identifier then
20859
20860 -- The Entity field of identifiers that denote aspects is used
20861 -- to store arbitrary expressions (and hence we must check that
20862 -- they reference an actual entity before copying their Chars
20863 -- value).
20864
20865 if Present (Entity (Result))
20866 and then Nkind (Entity (Result)) in N_Entity
20867 then
20868 Set_Chars (Result, Chars (Entity (Result)));
20869 end if;
20870 end if;
20871 end if;
20872
20873 return Result;
20874 end Copy_Node_With_Replacement;
20875
20876 --------------------------
20877 -- Corresponding_Entity --
20878 --------------------------
20879
20880 function Corresponding_Entity (Id : Entity_Id) return Entity_Id is
20881 New_Id : Entity_Id;
20882 Result : Entity_Id;
20883
20884 begin
20885 -- Assume that the entity must be returned unchanged
20886
20887 Result := Id;
20888
20889 if Id > Empty_Or_Error then
20890 pragma Assert (Nkind (Id) in N_Entity);
20891
20892 -- Determine whether the entity has a corresponding new entity
20893 -- generated during Phase 1 and if it does, use it.
20894
20895 if NCT_Tables_In_Use then
20896 New_Id := NCT_New_Entities.Get (Id);
20897
20898 if Present (New_Id) then
20899 Result := New_Id;
20900 end if;
20901 end if;
20902 end if;
20903
20904 return Result;
20905 end Corresponding_Entity;
20906
20907 -------------------
20908 -- In_Entity_Map --
20909 -------------------
20910
20911 function In_Entity_Map
20912 (Id : Entity_Id;
20913 Entity_Map : Elist_Id) return Boolean
20914 is
20915 Elmt : Elmt_Id;
20916 Old_Id : Entity_Id;
20917
20918 begin
20919 -- The entity map contains pairs (Old_Id, New_Id). The advancement
20920 -- step always skips the New_Id portion of the pair.
20921
20922 if Present (Entity_Map) then
20923 Elmt := First_Elmt (Entity_Map);
20924 while Present (Elmt) loop
20925 Old_Id := Node (Elmt);
20926
20927 if Old_Id = Id then
20928 return True;
20929 end if;
20930
20931 Next_Elmt (Elmt);
20932 Next_Elmt (Elmt);
20933 end loop;
20934 end if;
20935
20936 return False;
20937 end In_Entity_Map;
20938
20939 ---------------------
20940 -- Update_CFS_Sloc --
20941 ---------------------
20942
20943 procedure Update_CFS_Sloc (N : Node_Or_Entity_Id) is
20944 begin
20945 -- A new source location defaults the Comes_From_Source attribute
20946
20947 if New_Sloc /= No_Location then
20948 Set_Comes_From_Source (N, Default_Node.Comes_From_Source);
20949 Set_Sloc (N, New_Sloc);
20950 end if;
20951 end Update_CFS_Sloc;
20952
20953 ---------------------------------
20954 -- Update_First_Real_Statement --
20955 ---------------------------------
20956
20957 procedure Update_First_Real_Statement
20958 (Old_HSS : Node_Id;
20959 New_HSS : Node_Id)
20960 is
20961 Old_First_Stmt : constant Node_Id := First_Real_Statement (Old_HSS);
20962
20963 New_Stmt : Node_Id;
20964 Old_Stmt : Node_Id;
20965
20966 begin
20967 -- Recreate the First_Real_Statement attribute of a handled sequence
20968 -- of statements by traversing the statement lists of both sequences
20969 -- in parallel.
20970
20971 if Present (Old_First_Stmt) then
20972 New_Stmt := First (Statements (New_HSS));
20973 Old_Stmt := First (Statements (Old_HSS));
20974 while Present (Old_Stmt) and then Old_Stmt /= Old_First_Stmt loop
20975 Next (New_Stmt);
20976 Next (Old_Stmt);
20977 end loop;
20978
20979 pragma Assert (Present (New_Stmt));
20980 pragma Assert (Present (Old_Stmt));
20981
20982 Set_First_Real_Statement (New_HSS, New_Stmt);
20983 end if;
20984 end Update_First_Real_Statement;
20985
20986 -------------------------------
20987 -- Update_Named_Associations --
20988 -------------------------------
20989
20990 procedure Update_Named_Associations
20991 (Old_Call : Node_Id;
20992 New_Call : Node_Id)
20993 is
20994 New_Act : Node_Id;
20995 New_Next : Node_Id;
20996 Old_Act : Node_Id;
20997 Old_Next : Node_Id;
20998
20999 begin
21000 if No (First_Named_Actual (Old_Call)) then
21001 return;
21002 end if;
21003
21004 -- Recreate the First/Next_Named_Actual chain of a call by traversing
21005 -- the chains of both the old and new calls in parallel.
21006
21007 New_Act := First (Parameter_Associations (New_Call));
21008 Old_Act := First (Parameter_Associations (Old_Call));
21009 while Present (Old_Act) loop
21010 if Nkind (Old_Act) = N_Parameter_Association
21011 and then Explicit_Actual_Parameter (Old_Act)
21012 = First_Named_Actual (Old_Call)
21013 then
21014 Set_First_Named_Actual (New_Call,
21015 Explicit_Actual_Parameter (New_Act));
21016 end if;
21017
21018 if Nkind (Old_Act) = N_Parameter_Association
21019 and then Present (Next_Named_Actual (Old_Act))
21020 then
21021 -- Scan the actual parameter list to find the next suitable
21022 -- named actual. Note that the list may be out of order.
21023
21024 New_Next := First (Parameter_Associations (New_Call));
21025 Old_Next := First (Parameter_Associations (Old_Call));
21026 while Nkind (Old_Next) /= N_Parameter_Association
21027 or else Explicit_Actual_Parameter (Old_Next) /=
21028 Next_Named_Actual (Old_Act)
21029 loop
21030 Next (New_Next);
21031 Next (Old_Next);
21032 end loop;
21033
21034 Set_Next_Named_Actual (New_Act,
21035 Explicit_Actual_Parameter (New_Next));
21036 end if;
21037
21038 Next (New_Act);
21039 Next (Old_Act);
21040 end loop;
21041 end Update_Named_Associations;
21042
21043 -------------------------
21044 -- Update_New_Entities --
21045 -------------------------
21046
21047 procedure Update_New_Entities (Entity_Map : Elist_Id) is
21048 New_Id : Entity_Id := Empty;
21049 Old_Id : Entity_Id := Empty;
21050
21051 begin
21052 if NCT_Tables_In_Use then
21053 NCT_New_Entities.Get_First (Old_Id, New_Id);
21054
21055 -- Update the semantic fields of all new entities created during
21056 -- Phase 1 which were not supplied via an entity map.
21057 -- ??? Is there a better way of distinguishing those?
21058
21059 while Present (Old_Id) and then Present (New_Id) loop
21060 if not (Present (Entity_Map)
21061 and then In_Entity_Map (Old_Id, Entity_Map))
21062 then
21063 Update_Semantic_Fields (New_Id);
21064 end if;
21065
21066 NCT_New_Entities.Get_Next (Old_Id, New_Id);
21067 end loop;
21068 end if;
21069 end Update_New_Entities;
21070
21071 ---------------------------
21072 -- Update_Pending_Itypes --
21073 ---------------------------
21074
21075 procedure Update_Pending_Itypes
21076 (Old_Assoc : Node_Id;
21077 New_Assoc : Node_Id)
21078 is
21079 Item : Elmt_Id;
21080 Itypes : Elist_Id;
21081
21082 begin
21083 if NCT_Tables_In_Use then
21084 Itypes := NCT_Pending_Itypes.Get (Old_Assoc);
21085
21086 -- Update the Associated_Node_For_Itype attribute for all itypes
21087 -- which originally refer to Old_Assoc to designate New_Assoc.
21088
21089 if Present (Itypes) then
21090 Item := First_Elmt (Itypes);
21091 while Present (Item) loop
21092 Set_Associated_Node_For_Itype (Node (Item), New_Assoc);
21093
21094 Next_Elmt (Item);
21095 end loop;
21096 end if;
21097 end if;
21098 end Update_Pending_Itypes;
21099
21100 ----------------------------
21101 -- Update_Semantic_Fields --
21102 ----------------------------
21103
21104 procedure Update_Semantic_Fields (Id : Entity_Id) is
21105 begin
21106 -- Discriminant_Constraint
21107
21108 if Is_Type (Id) and then Has_Discriminants (Base_Type (Id)) then
21109 Set_Discriminant_Constraint (Id, Elist_Id (
21110 Copy_Field_With_Replacement
21111 (Field => Union_Id (Discriminant_Constraint (Id)),
21112 Semantic => True)));
21113 end if;
21114
21115 -- Etype
21116
21117 Set_Etype (Id, Node_Id (
21118 Copy_Field_With_Replacement
21119 (Field => Union_Id (Etype (Id)),
21120 Semantic => True)));
21121
21122 -- First_Index
21123 -- Packed_Array_Impl_Type
21124
21125 if Is_Array_Type (Id) then
21126 if Present (First_Index (Id)) then
21127 Set_First_Index (Id, First (List_Id (
21128 Copy_Field_With_Replacement
21129 (Field => Union_Id (List_Containing (First_Index (Id))),
21130 Semantic => True))));
21131 end if;
21132
21133 if Is_Packed (Id) then
21134 Set_Packed_Array_Impl_Type (Id, Node_Id (
21135 Copy_Field_With_Replacement
21136 (Field => Union_Id (Packed_Array_Impl_Type (Id)),
21137 Semantic => True)));
21138 end if;
21139 end if;
21140
21141 -- Prev_Entity
21142
21143 Set_Prev_Entity (Id, Node_Id (
21144 Copy_Field_With_Replacement
21145 (Field => Union_Id (Prev_Entity (Id)),
21146 Semantic => True)));
21147
21148 -- Next_Entity
21149
21150 Set_Next_Entity (Id, Node_Id (
21151 Copy_Field_With_Replacement
21152 (Field => Union_Id (Next_Entity (Id)),
21153 Semantic => True)));
21154
21155 -- Scalar_Range
21156
21157 if Is_Discrete_Type (Id) then
21158 Set_Scalar_Range (Id, Node_Id (
21159 Copy_Field_With_Replacement
21160 (Field => Union_Id (Scalar_Range (Id)),
21161 Semantic => True)));
21162 end if;
21163
21164 -- Scope
21165
21166 -- Update the scope when the caller specified an explicit one
21167
21168 if Present (New_Scope) then
21169 Set_Scope (Id, New_Scope);
21170 else
21171 Set_Scope (Id, Node_Id (
21172 Copy_Field_With_Replacement
21173 (Field => Union_Id (Scope (Id)),
21174 Semantic => True)));
21175 end if;
21176 end Update_Semantic_Fields;
21177
21178 --------------------
21179 -- Visit_Any_Node --
21180 --------------------
21181
21182 procedure Visit_Any_Node (N : Node_Or_Entity_Id) is
21183 begin
21184 if Nkind (N) in N_Entity then
21185 if Is_Itype (N) then
21186 Visit_Itype (N);
21187 else
21188 Visit_Entity (N);
21189 end if;
21190 else
21191 Visit_Node (N);
21192 end if;
21193 end Visit_Any_Node;
21194
21195 -----------------
21196 -- Visit_Elist --
21197 -----------------
21198
21199 procedure Visit_Elist (List : Elist_Id) is
21200 Elmt : Elmt_Id;
21201
21202 begin
21203 -- The element of an entity list could be an entity, itype, or a
21204 -- node, hence the call to Visit_Any_Node.
21205
21206 if Present (List) then
21207 Elmt := First_Elmt (List);
21208 while Present (Elmt) loop
21209 Visit_Any_Node (Node (Elmt));
21210
21211 Next_Elmt (Elmt);
21212 end loop;
21213 end if;
21214 end Visit_Elist;
21215
21216 ------------------
21217 -- Visit_Entity --
21218 ------------------
21219
21220 procedure Visit_Entity (Id : Entity_Id) is
21221 New_Id : Entity_Id;
21222
21223 begin
21224 pragma Assert (Nkind (Id) in N_Entity);
21225 pragma Assert (not Is_Itype (Id));
21226
21227 -- Nothing to do when the entity is not defined in the Actions list
21228 -- of an N_Expression_With_Actions node.
21229
21230 if EWA_Level = 0 then
21231 return;
21232
21233 -- Nothing to do when the entity is defined in a scoping construct
21234 -- within an N_Expression_With_Actions node, unless the caller has
21235 -- requested their replication.
21236
21237 -- ??? should this restriction be eliminated?
21238
21239 elsif EWA_Inner_Scope_Level > 0 and then not Scopes_In_EWA_OK then
21240 return;
21241
21242 -- Nothing to do when the entity does not denote a construct that
21243 -- may appear within an N_Expression_With_Actions node. Relaxing
21244 -- this restriction leads to a performance penalty.
21245
21246 -- ??? this list is flaky, and may hide dormant bugs
21247 -- Should functions be included???
21248
21249 -- Loop parameters appear within quantified expressions and contain
21250 -- an entity declaration that must be replaced when the expander is
21251 -- active if the expression has been preanalyzed or analyzed.
21252
21253 elsif not Ekind_In (Id, E_Block,
21254 E_Constant,
21255 E_Label,
21256 E_Loop_Parameter,
21257 E_Procedure,
21258 E_Variable)
21259 and then not Is_Type (Id)
21260 then
21261 return;
21262
21263 elsif Ekind (Id) = E_Loop_Parameter
21264 and then No (Etype (Condition (Parent (Parent (Id)))))
21265 then
21266 return;
21267
21268 -- Nothing to do when the entity was already visited
21269
21270 elsif NCT_Tables_In_Use
21271 and then Present (NCT_New_Entities.Get (Id))
21272 then
21273 return;
21274
21275 -- Nothing to do when the declaration node of the entity is not in
21276 -- the subtree being replicated.
21277
21278 elsif not In_Subtree
21279 (N => Declaration_Node (Id),
21280 Root => Source)
21281 then
21282 return;
21283 end if;
21284
21285 -- Create a new entity by directly copying the old entity. This
21286 -- action causes all attributes of the old entity to be inherited.
21287
21288 New_Id := New_Copy (Id);
21289
21290 -- Create a new name for the new entity because the back end needs
21291 -- distinct names for debugging purposes.
21292
21293 Set_Chars (New_Id, New_Internal_Name ('T'));
21294
21295 -- Update the Comes_From_Source and Sloc attributes of the entity in
21296 -- case the caller has supplied new values.
21297
21298 Update_CFS_Sloc (New_Id);
21299
21300 -- Establish the following mapping within table NCT_New_Entities:
21301
21302 -- Id -> New_Id
21303
21304 Add_New_Entity (Id, New_Id);
21305
21306 -- Deal with the semantic fields of entities. The fields are visited
21307 -- because they may mention entities which reside within the subtree
21308 -- being copied.
21309
21310 Visit_Semantic_Fields (Id);
21311 end Visit_Entity;
21312
21313 -----------------
21314 -- Visit_Field --
21315 -----------------
21316
21317 procedure Visit_Field
21318 (Field : Union_Id;
21319 Par_Nod : Node_Id := Empty;
21320 Semantic : Boolean := False)
21321 is
21322 begin
21323 -- The field is empty
21324
21325 if Field = Union_Id (Empty) then
21326 return;
21327
21328 -- The field is an entity/itype/node
21329
21330 elsif Field in Node_Range then
21331 declare
21332 N : constant Node_Id := Node_Id (Field);
21333
21334 begin
21335 -- The field is an entity/itype
21336
21337 if Nkind (N) in N_Entity then
21338
21339 -- Itypes are always visited
21340
21341 if Is_Itype (N) then
21342 Visit_Itype (N);
21343
21344 -- An entity is visited when it is either a syntactic field
21345 -- or when the caller treats it as a semantic attribute.
21346
21347 elsif Parent (N) = Par_Nod or else Semantic then
21348 Visit_Entity (N);
21349 end if;
21350
21351 -- The field is a node
21352
21353 else
21354 -- A node is visited when it is either a syntactic field or
21355 -- when the caller treats it as a semantic attribute.
21356
21357 if Parent (N) = Par_Nod or else Semantic then
21358 Visit_Node (N);
21359 end if;
21360 end if;
21361 end;
21362
21363 -- The field is an entity list
21364
21365 elsif Field in Elist_Range then
21366 Visit_Elist (Elist_Id (Field));
21367
21368 -- The field is a syntax list
21369
21370 elsif Field in List_Range then
21371 declare
21372 List : constant List_Id := List_Id (Field);
21373
21374 begin
21375 -- A syntax list is visited when it is either a syntactic field
21376 -- or when the caller treats it as a semantic attribute.
21377
21378 if Parent (List) = Par_Nod or else Semantic then
21379 Visit_List (List);
21380 end if;
21381 end;
21382
21383 -- Otherwise the field denotes information which does not need to be
21384 -- visited (chars, literals, etc.).
21385
21386 else
21387 null;
21388 end if;
21389 end Visit_Field;
21390
21391 -----------------
21392 -- Visit_Itype --
21393 -----------------
21394
21395 procedure Visit_Itype (Itype : Entity_Id) is
21396 New_Assoc : Node_Id;
21397 New_Itype : Entity_Id;
21398 Old_Assoc : Node_Id;
21399
21400 begin
21401 pragma Assert (Nkind (Itype) in N_Entity);
21402 pragma Assert (Is_Itype (Itype));
21403
21404 -- Itypes that describe the designated type of access to subprograms
21405 -- have the structure of subprogram declarations, with signatures,
21406 -- etc. Either we duplicate the signatures completely, or choose to
21407 -- share such itypes, which is fine because their elaboration will
21408 -- have no side effects.
21409
21410 if Ekind (Itype) = E_Subprogram_Type then
21411 return;
21412
21413 -- Nothing to do if the itype was already visited
21414
21415 elsif NCT_Tables_In_Use
21416 and then Present (NCT_New_Entities.Get (Itype))
21417 then
21418 return;
21419
21420 -- Nothing to do if the associated node of the itype is not within
21421 -- the subtree being replicated.
21422
21423 elsif not In_Subtree
21424 (N => Associated_Node_For_Itype (Itype),
21425 Root => Source)
21426 then
21427 return;
21428 end if;
21429
21430 -- Create a new itype by directly copying the old itype. This action
21431 -- causes all attributes of the old itype to be inherited.
21432
21433 New_Itype := New_Copy (Itype);
21434
21435 -- Create a new name for the new itype because the back end requires
21436 -- distinct names for debugging purposes.
21437
21438 Set_Chars (New_Itype, New_Internal_Name ('T'));
21439
21440 -- Update the Comes_From_Source and Sloc attributes of the itype in
21441 -- case the caller has supplied new values.
21442
21443 Update_CFS_Sloc (New_Itype);
21444
21445 -- Establish the following mapping within table NCT_New_Entities:
21446
21447 -- Itype -> New_Itype
21448
21449 Add_New_Entity (Itype, New_Itype);
21450
21451 -- The new itype must be unfrozen because the resulting subtree may
21452 -- be inserted anywhere and cause an earlier or later freezing.
21453
21454 if Present (Freeze_Node (New_Itype)) then
21455 Set_Freeze_Node (New_Itype, Empty);
21456 Set_Is_Frozen (New_Itype, False);
21457 end if;
21458
21459 -- If a record subtype is simply copied, the entity list will be
21460 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
21461 -- ??? What does this do?
21462
21463 if Ekind_In (Itype, E_Class_Wide_Subtype, E_Record_Subtype) then
21464 Set_Cloned_Subtype (New_Itype, Itype);
21465 end if;
21466
21467 -- The associated node may denote an entity, in which case it may
21468 -- already have a new corresponding entity created during a prior
21469 -- call to Visit_Entity or Visit_Itype for the same subtree.
21470
21471 -- Given
21472 -- Old_Assoc ---------> New_Assoc
21473
21474 -- Created by Visit_Itype
21475 -- Itype -------------> New_Itype
21476 -- ANFI = Old_Assoc ANFI = Old_Assoc < must be updated
21477
21478 -- In the example above, Old_Assoc is an arbitrary entity that was
21479 -- already visited for the same subtree and has a corresponding new
21480 -- entity New_Assoc. Old_Assoc was inherited by New_Itype by virtue
21481 -- of copying entities, however it must be updated to New_Assoc.
21482
21483 Old_Assoc := Associated_Node_For_Itype (Itype);
21484
21485 if Nkind (Old_Assoc) in N_Entity then
21486 if NCT_Tables_In_Use then
21487 New_Assoc := NCT_New_Entities.Get (Old_Assoc);
21488
21489 if Present (New_Assoc) then
21490 Set_Associated_Node_For_Itype (New_Itype, New_Assoc);
21491 end if;
21492 end if;
21493
21494 -- Otherwise the associated node denotes a node. Postpone the update
21495 -- until Phase 2 when the node is replicated. Establish the following
21496 -- mapping within table NCT_Pending_Itypes:
21497
21498 -- Old_Assoc -> (New_Type, ...)
21499
21500 else
21501 Add_Pending_Itype (Old_Assoc, New_Itype);
21502 end if;
21503
21504 -- Deal with the semantic fields of itypes. The fields are visited
21505 -- because they may mention entities that reside within the subtree
21506 -- being copied.
21507
21508 Visit_Semantic_Fields (Itype);
21509 end Visit_Itype;
21510
21511 ----------------
21512 -- Visit_List --
21513 ----------------
21514
21515 procedure Visit_List (List : List_Id) is
21516 Elmt : Node_Id;
21517
21518 begin
21519 -- Note that the element of a syntactic list is always a node, never
21520 -- an entity or itype, hence the call to Visit_Node.
21521
21522 if Present (List) then
21523 Elmt := First (List);
21524 while Present (Elmt) loop
21525 Visit_Node (Elmt);
21526
21527 Next (Elmt);
21528 end loop;
21529 end if;
21530 end Visit_List;
21531
21532 ----------------
21533 -- Visit_Node --
21534 ----------------
21535
21536 procedure Visit_Node (N : Node_Or_Entity_Id) is
21537 begin
21538 pragma Assert (Nkind (N) not in N_Entity);
21539
21540 -- If the node is a quantified expression and expander is active,
21541 -- it contains an implicit declaration that may require a new entity
21542 -- when the condition has already been (pre)analyzed.
21543
21544 if Nkind (N) = N_Expression_With_Actions
21545 or else
21546 (Nkind (N) = N_Quantified_Expression and then Expander_Active)
21547 then
21548 EWA_Level := EWA_Level + 1;
21549
21550 elsif EWA_Level > 0
21551 and then Nkind_In (N, N_Block_Statement,
21552 N_Subprogram_Body,
21553 N_Subprogram_Declaration)
21554 then
21555 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level + 1;
21556 end if;
21557
21558 Visit_Field
21559 (Field => Field1 (N),
21560 Par_Nod => N);
21561
21562 Visit_Field
21563 (Field => Field2 (N),
21564 Par_Nod => N);
21565
21566 Visit_Field
21567 (Field => Field3 (N),
21568 Par_Nod => N);
21569
21570 Visit_Field
21571 (Field => Field4 (N),
21572 Par_Nod => N);
21573
21574 Visit_Field
21575 (Field => Field5 (N),
21576 Par_Nod => N);
21577
21578 if EWA_Level > 0
21579 and then Nkind_In (N, N_Block_Statement,
21580 N_Subprogram_Body,
21581 N_Subprogram_Declaration)
21582 then
21583 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level - 1;
21584
21585 elsif Nkind (N) = N_Expression_With_Actions then
21586 EWA_Level := EWA_Level - 1;
21587 end if;
21588 end Visit_Node;
21589
21590 ---------------------------
21591 -- Visit_Semantic_Fields --
21592 ---------------------------
21593
21594 procedure Visit_Semantic_Fields (Id : Entity_Id) is
21595 begin
21596 pragma Assert (Nkind (Id) in N_Entity);
21597
21598 -- Discriminant_Constraint
21599
21600 if Is_Type (Id) and then Has_Discriminants (Base_Type (Id)) then
21601 Visit_Field
21602 (Field => Union_Id (Discriminant_Constraint (Id)),
21603 Semantic => True);
21604 end if;
21605
21606 -- Etype
21607
21608 Visit_Field
21609 (Field => Union_Id (Etype (Id)),
21610 Semantic => True);
21611
21612 -- First_Index
21613 -- Packed_Array_Impl_Type
21614
21615 if Is_Array_Type (Id) then
21616 if Present (First_Index (Id)) then
21617 Visit_Field
21618 (Field => Union_Id (List_Containing (First_Index (Id))),
21619 Semantic => True);
21620 end if;
21621
21622 if Is_Packed (Id) then
21623 Visit_Field
21624 (Field => Union_Id (Packed_Array_Impl_Type (Id)),
21625 Semantic => True);
21626 end if;
21627 end if;
21628
21629 -- Scalar_Range
21630
21631 if Is_Discrete_Type (Id) then
21632 Visit_Field
21633 (Field => Union_Id (Scalar_Range (Id)),
21634 Semantic => True);
21635 end if;
21636 end Visit_Semantic_Fields;
21637
21638 -- Start of processing for New_Copy_Tree
21639
21640 begin
21641 -- Routine New_Copy_Tree performs a deep copy of a subtree by creating
21642 -- shallow copies for each node within, and then updating the child and
21643 -- parent pointers accordingly. This process is straightforward, however
21644 -- the routine must deal with the following complications:
21645
21646 -- * Entities defined within N_Expression_With_Actions nodes must be
21647 -- replicated rather than shared to avoid introducing two identical
21648 -- symbols within the same scope. Note that no other expression can
21649 -- currently define entities.
21650
21651 -- do
21652 -- Source_Low : ...;
21653 -- Source_High : ...;
21654
21655 -- <reference to Source_Low>
21656 -- <reference to Source_High>
21657 -- in ... end;
21658
21659 -- New_Copy_Tree handles this case by first creating new entities
21660 -- and then updating all existing references to point to these new
21661 -- entities.
21662
21663 -- do
21664 -- New_Low : ...;
21665 -- New_High : ...;
21666
21667 -- <reference to New_Low>
21668 -- <reference to New_High>
21669 -- in ... end;
21670
21671 -- * Itypes defined within the subtree must be replicated to avoid any
21672 -- dependencies on invalid or inaccessible data.
21673
21674 -- subtype Source_Itype is ... range Source_Low .. Source_High;
21675
21676 -- New_Copy_Tree handles this case by first creating a new itype in
21677 -- the same fashion as entities, and then updating various relevant
21678 -- constraints.
21679
21680 -- subtype New_Itype is ... range New_Low .. New_High;
21681
21682 -- * The Associated_Node_For_Itype field of itypes must be updated to
21683 -- reference the proper replicated entity or node.
21684
21685 -- * Semantic fields of entities such as Etype and Scope must be
21686 -- updated to reference the proper replicated entities.
21687
21688 -- * Semantic fields of nodes such as First_Real_Statement must be
21689 -- updated to reference the proper replicated nodes.
21690
21691 -- Finally, quantified expressions contain an implicit delaration for
21692 -- the bound variable. Given that quantified expressions appearing
21693 -- in contracts are copied to create pragmas and eventually checking
21694 -- procedures, a new bound variable must be created for each copy, to
21695 -- prevent multiple declarations of the same symbol.
21696
21697 -- To meet all these demands, routine New_Copy_Tree is split into two
21698 -- phases.
21699
21700 -- Phase 1 traverses the tree in order to locate entities and itypes
21701 -- defined within the subtree. New entities are generated and saved in
21702 -- table NCT_New_Entities. The semantic fields of all new entities and
21703 -- itypes are then updated accordingly.
21704
21705 -- Phase 2 traverses the tree in order to replicate each node. Various
21706 -- semantic fields of nodes and entities are updated accordingly.
21707
21708 -- Preparatory phase. Clear the contents of tables NCT_New_Entities and
21709 -- NCT_Pending_Itypes in case a previous call to New_Copy_Tree left some
21710 -- data inside.
21711
21712 if NCT_Tables_In_Use then
21713 NCT_Tables_In_Use := False;
21714
21715 NCT_New_Entities.Reset;
21716 NCT_Pending_Itypes.Reset;
21717 end if;
21718
21719 -- Populate tables NCT_New_Entities and NCT_Pending_Itypes with data
21720 -- supplied by a linear entity map. The tables offer faster access to
21721 -- the same data.
21722
21723 Build_NCT_Tables (Map);
21724
21725 -- Execute Phase 1. Traverse the subtree and generate new entities for
21726 -- the following cases:
21727
21728 -- * An entity defined within an N_Expression_With_Actions node
21729
21730 -- * An itype referenced within the subtree where the associated node
21731 -- is also in the subtree.
21732
21733 -- All new entities are accessible via table NCT_New_Entities, which
21734 -- contains mappings of the form:
21735
21736 -- Old_Entity -> New_Entity
21737 -- Old_Itype -> New_Itype
21738
21739 -- In addition, the associated nodes of all new itypes are mapped in
21740 -- table NCT_Pending_Itypes:
21741
21742 -- Assoc_Nod -> (New_Itype1, New_Itype2, .., New_ItypeN)
21743
21744 Visit_Any_Node (Source);
21745
21746 -- Update the semantic attributes of all new entities generated during
21747 -- Phase 1 before starting Phase 2. The updates could be performed in
21748 -- routine Corresponding_Entity, however this may cause the same entity
21749 -- to be updated multiple times, effectively generating useless nodes.
21750 -- Keeping the updates separates from Phase 2 ensures that only one set
21751 -- of attributes is generated for an entity at any one time.
21752
21753 Update_New_Entities (Map);
21754
21755 -- Execute Phase 2. Replicate the source subtree one node at a time.
21756 -- The following transformations take place:
21757
21758 -- * References to entities and itypes are updated to refer to the
21759 -- new entities and itypes generated during Phase 1.
21760
21761 -- * All Associated_Node_For_Itype attributes of itypes are updated
21762 -- to refer to the new replicated Associated_Node_For_Itype.
21763
21764 return Copy_Node_With_Replacement (Source);
21765 end New_Copy_Tree;
21766
21767 -------------------------
21768 -- New_External_Entity --
21769 -------------------------
21770
21771 function New_External_Entity
21772 (Kind : Entity_Kind;
21773 Scope_Id : Entity_Id;
21774 Sloc_Value : Source_Ptr;
21775 Related_Id : Entity_Id;
21776 Suffix : Character;
21777 Suffix_Index : Int := 0;
21778 Prefix : Character := ' ') return Entity_Id
21779 is
21780 N : constant Entity_Id :=
21781 Make_Defining_Identifier (Sloc_Value,
21782 New_External_Name
21783 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
21784
21785 begin
21786 Set_Ekind (N, Kind);
21787 Set_Is_Internal (N, True);
21788 Append_Entity (N, Scope_Id);
21789 Set_Public_Status (N);
21790
21791 if Kind in Type_Kind then
21792 Init_Size_Align (N);
21793 end if;
21794
21795 return N;
21796 end New_External_Entity;
21797
21798 -------------------------
21799 -- New_Internal_Entity --
21800 -------------------------
21801
21802 function New_Internal_Entity
21803 (Kind : Entity_Kind;
21804 Scope_Id : Entity_Id;
21805 Sloc_Value : Source_Ptr;
21806 Id_Char : Character) return Entity_Id
21807 is
21808 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
21809
21810 begin
21811 Set_Ekind (N, Kind);
21812 Set_Is_Internal (N, True);
21813 Append_Entity (N, Scope_Id);
21814
21815 if Kind in Type_Kind then
21816 Init_Size_Align (N);
21817 end if;
21818
21819 return N;
21820 end New_Internal_Entity;
21821
21822 -----------------
21823 -- Next_Actual --
21824 -----------------
21825
21826 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
21827 Par : constant Node_Id := Parent (Actual_Id);
21828 N : Node_Id;
21829
21830 begin
21831 -- If we are pointing at a positional parameter, it is a member of a
21832 -- node list (the list of parameters), and the next parameter is the
21833 -- next node on the list, unless we hit a parameter association, then
21834 -- we shift to using the chain whose head is the First_Named_Actual in
21835 -- the parent, and then is threaded using the Next_Named_Actual of the
21836 -- Parameter_Association. All this fiddling is because the original node
21837 -- list is in the textual call order, and what we need is the
21838 -- declaration order.
21839
21840 if Is_List_Member (Actual_Id) then
21841 N := Next (Actual_Id);
21842
21843 if Nkind (N) = N_Parameter_Association then
21844
21845 -- In case of a build-in-place call, the call will no longer be a
21846 -- call; it will have been rewritten.
21847
21848 if Nkind_In (Par, N_Entry_Call_Statement,
21849 N_Function_Call,
21850 N_Procedure_Call_Statement)
21851 then
21852 return First_Named_Actual (Par);
21853
21854 -- In case of a call rewritten in GNATprove mode while "inlining
21855 -- for proof" go to the original call.
21856
21857 elsif Nkind (Par) = N_Null_Statement then
21858 pragma Assert
21859 (GNATprove_Mode
21860 and then
21861 Nkind (Original_Node (Par)) in N_Subprogram_Call);
21862
21863 return First_Named_Actual (Original_Node (Par));
21864 else
21865 return Empty;
21866 end if;
21867 else
21868 return N;
21869 end if;
21870
21871 else
21872 return Next_Named_Actual (Parent (Actual_Id));
21873 end if;
21874 end Next_Actual;
21875
21876 procedure Next_Actual (Actual_Id : in out Node_Id) is
21877 begin
21878 Actual_Id := Next_Actual (Actual_Id);
21879 end Next_Actual;
21880
21881 -----------------
21882 -- Next_Global --
21883 -----------------
21884
21885 function Next_Global (Node : Node_Id) return Node_Id is
21886 begin
21887 -- The global item may either be in a list, or by itself, in which case
21888 -- there is no next global item with the same mode.
21889
21890 if Is_List_Member (Node) then
21891 return Next (Node);
21892 else
21893 return Empty;
21894 end if;
21895 end Next_Global;
21896
21897 procedure Next_Global (Node : in out Node_Id) is
21898 begin
21899 Node := Next_Global (Node);
21900 end Next_Global;
21901
21902 ----------------------------------
21903 -- New_Requires_Transient_Scope --
21904 ----------------------------------
21905
21906 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
21907 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
21908 -- This is called for untagged records and protected types, with
21909 -- nondefaulted discriminants. Returns True if the size of function
21910 -- results is known at the call site, False otherwise. Returns False
21911 -- if there is a variant part that depends on the discriminants of
21912 -- this type, or if there is an array constrained by the discriminants
21913 -- of this type. ???Currently, this is overly conservative (the array
21914 -- could be nested inside some other record that is constrained by
21915 -- nondiscriminants). That is, the recursive calls are too conservative.
21916
21917 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean;
21918 -- Returns True if Typ is a nonlimited record with defaulted
21919 -- discriminants whose max size makes it unsuitable for allocating on
21920 -- the primary stack.
21921
21922 ------------------------------
21923 -- Caller_Known_Size_Record --
21924 ------------------------------
21925
21926 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
21927 pragma Assert (Typ = Underlying_Type (Typ));
21928
21929 begin
21930 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
21931 return False;
21932 end if;
21933
21934 declare
21935 Comp : Entity_Id;
21936
21937 begin
21938 Comp := First_Entity (Typ);
21939 while Present (Comp) loop
21940
21941 -- Only look at E_Component entities. No need to look at
21942 -- E_Discriminant entities, and we must ignore internal
21943 -- subtypes generated for constrained components.
21944
21945 if Ekind (Comp) = E_Component then
21946 declare
21947 Comp_Type : constant Entity_Id :=
21948 Underlying_Type (Etype (Comp));
21949
21950 begin
21951 if Is_Record_Type (Comp_Type)
21952 or else
21953 Is_Protected_Type (Comp_Type)
21954 then
21955 if not Caller_Known_Size_Record (Comp_Type) then
21956 return False;
21957 end if;
21958
21959 elsif Is_Array_Type (Comp_Type) then
21960 if Size_Depends_On_Discriminant (Comp_Type) then
21961 return False;
21962 end if;
21963 end if;
21964 end;
21965 end if;
21966
21967 Next_Entity (Comp);
21968 end loop;
21969 end;
21970
21971 return True;
21972 end Caller_Known_Size_Record;
21973
21974 ------------------------------
21975 -- Large_Max_Size_Mutable --
21976 ------------------------------
21977
21978 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean is
21979 pragma Assert (Typ = Underlying_Type (Typ));
21980
21981 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean;
21982 -- Returns true if the discrete type T has a large range
21983
21984 ----------------------------
21985 -- Is_Large_Discrete_Type --
21986 ----------------------------
21987
21988 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean is
21989 Threshold : constant Int := 16;
21990 -- Arbitrary threshold above which we consider it "large". We want
21991 -- a fairly large threshold, because these large types really
21992 -- shouldn't have default discriminants in the first place, in
21993 -- most cases.
21994
21995 begin
21996 return UI_To_Int (RM_Size (T)) > Threshold;
21997 end Is_Large_Discrete_Type;
21998
21999 -- Start of processing for Large_Max_Size_Mutable
22000
22001 begin
22002 if Is_Record_Type (Typ)
22003 and then not Is_Limited_View (Typ)
22004 and then Has_Defaulted_Discriminants (Typ)
22005 then
22006 -- Loop through the components, looking for an array whose upper
22007 -- bound(s) depends on discriminants, where both the subtype of
22008 -- the discriminant and the index subtype are too large.
22009
22010 declare
22011 Comp : Entity_Id;
22012
22013 begin
22014 Comp := First_Entity (Typ);
22015 while Present (Comp) loop
22016 if Ekind (Comp) = E_Component then
22017 declare
22018 Comp_Type : constant Entity_Id :=
22019 Underlying_Type (Etype (Comp));
22020
22021 Hi : Node_Id;
22022 Indx : Node_Id;
22023 Ityp : Entity_Id;
22024
22025 begin
22026 if Is_Array_Type (Comp_Type) then
22027 Indx := First_Index (Comp_Type);
22028
22029 while Present (Indx) loop
22030 Ityp := Etype (Indx);
22031 Hi := Type_High_Bound (Ityp);
22032
22033 if Nkind (Hi) = N_Identifier
22034 and then Ekind (Entity (Hi)) = E_Discriminant
22035 and then Is_Large_Discrete_Type (Ityp)
22036 and then Is_Large_Discrete_Type
22037 (Etype (Entity (Hi)))
22038 then
22039 return True;
22040 end if;
22041
22042 Next_Index (Indx);
22043 end loop;
22044 end if;
22045 end;
22046 end if;
22047
22048 Next_Entity (Comp);
22049 end loop;
22050 end;
22051 end if;
22052
22053 return False;
22054 end Large_Max_Size_Mutable;
22055
22056 -- Local declarations
22057
22058 Typ : constant Entity_Id := Underlying_Type (Id);
22059
22060 -- Start of processing for New_Requires_Transient_Scope
22061
22062 begin
22063 -- This is a private type which is not completed yet. This can only
22064 -- happen in a default expression (of a formal parameter or of a
22065 -- record component). Do not expand transient scope in this case.
22066
22067 if No (Typ) then
22068 return False;
22069
22070 -- Do not expand transient scope for non-existent procedure return or
22071 -- string literal types.
22072
22073 elsif Typ = Standard_Void_Type
22074 or else Ekind (Typ) = E_String_Literal_Subtype
22075 then
22076 return False;
22077
22078 -- If Typ is a generic formal incomplete type, then we want to look at
22079 -- the actual type.
22080
22081 elsif Ekind (Typ) = E_Record_Subtype
22082 and then Present (Cloned_Subtype (Typ))
22083 then
22084 return New_Requires_Transient_Scope (Cloned_Subtype (Typ));
22085
22086 -- Functions returning specific tagged types may dispatch on result, so
22087 -- their returned value is allocated on the secondary stack, even in the
22088 -- definite case. We must treat nondispatching functions the same way,
22089 -- because access-to-function types can point at both, so the calling
22090 -- conventions must be compatible. Is_Tagged_Type includes controlled
22091 -- types and class-wide types. Controlled type temporaries need
22092 -- finalization.
22093
22094 -- ???It's not clear why we need to return noncontrolled types with
22095 -- controlled components on the secondary stack.
22096
22097 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
22098 return True;
22099
22100 -- Untagged definite subtypes are known size. This includes all
22101 -- elementary [sub]types. Tasks are known size even if they have
22102 -- discriminants. So we return False here, with one exception:
22103 -- For a type like:
22104 -- type T (Last : Natural := 0) is
22105 -- X : String (1 .. Last);
22106 -- end record;
22107 -- we return True. That's because for "P(F(...));", where F returns T,
22108 -- we don't know the size of the result at the call site, so if we
22109 -- allocated it on the primary stack, we would have to allocate the
22110 -- maximum size, which is way too big.
22111
22112 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
22113 return Large_Max_Size_Mutable (Typ);
22114
22115 -- Indefinite (discriminated) untagged record or protected type
22116
22117 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
22118 return not Caller_Known_Size_Record (Typ);
22119
22120 -- Unconstrained array
22121
22122 else
22123 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
22124 return True;
22125 end if;
22126 end New_Requires_Transient_Scope;
22127
22128 ------------------------
22129 -- No_Caching_Enabled --
22130 ------------------------
22131
22132 function No_Caching_Enabled (Id : Entity_Id) return Boolean is
22133 Prag : constant Node_Id := Get_Pragma (Id, Pragma_No_Caching);
22134 Arg1 : Node_Id;
22135
22136 begin
22137 if Present (Prag) then
22138 Arg1 := First (Pragma_Argument_Associations (Prag));
22139
22140 -- The pragma has an optional Boolean expression, the related
22141 -- property is enabled only when the expression evaluates to True.
22142
22143 if Present (Arg1) then
22144 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
22145
22146 -- Otherwise the lack of expression enables the property by
22147 -- default.
22148
22149 else
22150 return True;
22151 end if;
22152
22153 -- The property was never set in the first place
22154
22155 else
22156 return False;
22157 end if;
22158 end No_Caching_Enabled;
22159
22160 --------------------------
22161 -- No_Heap_Finalization --
22162 --------------------------
22163
22164 function No_Heap_Finalization (Typ : Entity_Id) return Boolean is
22165 begin
22166 if Ekind_In (Typ, E_Access_Type, E_General_Access_Type)
22167 and then Is_Library_Level_Entity (Typ)
22168 then
22169 -- A global No_Heap_Finalization pragma applies to all library-level
22170 -- named access-to-object types.
22171
22172 if Present (No_Heap_Finalization_Pragma) then
22173 return True;
22174
22175 -- The library-level named access-to-object type itself is subject to
22176 -- pragma No_Heap_Finalization.
22177
22178 elsif Present (Get_Pragma (Typ, Pragma_No_Heap_Finalization)) then
22179 return True;
22180 end if;
22181 end if;
22182
22183 return False;
22184 end No_Heap_Finalization;
22185
22186 -----------------------
22187 -- Normalize_Actuals --
22188 -----------------------
22189
22190 -- Chain actuals according to formals of subprogram. If there are no named
22191 -- associations, the chain is simply the list of Parameter Associations,
22192 -- since the order is the same as the declaration order. If there are named
22193 -- associations, then the First_Named_Actual field in the N_Function_Call
22194 -- or N_Procedure_Call_Statement node points to the Parameter_Association
22195 -- node for the parameter that comes first in declaration order. The
22196 -- remaining named parameters are then chained in declaration order using
22197 -- Next_Named_Actual.
22198
22199 -- This routine also verifies that the number of actuals is compatible with
22200 -- the number and default values of formals, but performs no type checking
22201 -- (type checking is done by the caller).
22202
22203 -- If the matching succeeds, Success is set to True and the caller proceeds
22204 -- with type-checking. If the match is unsuccessful, then Success is set to
22205 -- False, and the caller attempts a different interpretation, if there is
22206 -- one.
22207
22208 -- If the flag Report is on, the call is not overloaded, and a failure to
22209 -- match can be reported here, rather than in the caller.
22210
22211 procedure Normalize_Actuals
22212 (N : Node_Id;
22213 S : Entity_Id;
22214 Report : Boolean;
22215 Success : out Boolean)
22216 is
22217 Actuals : constant List_Id := Parameter_Associations (N);
22218 Actual : Node_Id := Empty;
22219 Formal : Entity_Id;
22220 Last : Node_Id := Empty;
22221 First_Named : Node_Id := Empty;
22222 Found : Boolean;
22223
22224 Formals_To_Match : Integer := 0;
22225 Actuals_To_Match : Integer := 0;
22226
22227 procedure Chain (A : Node_Id);
22228 -- Add named actual at the proper place in the list, using the
22229 -- Next_Named_Actual link.
22230
22231 function Reporting return Boolean;
22232 -- Determines if an error is to be reported. To report an error, we
22233 -- need Report to be True, and also we do not report errors caused
22234 -- by calls to init procs that occur within other init procs. Such
22235 -- errors must always be cascaded errors, since if all the types are
22236 -- declared correctly, the compiler will certainly build decent calls.
22237
22238 -----------
22239 -- Chain --
22240 -----------
22241
22242 procedure Chain (A : Node_Id) is
22243 begin
22244 if No (Last) then
22245
22246 -- Call node points to first actual in list
22247
22248 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
22249
22250 else
22251 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
22252 end if;
22253
22254 Last := A;
22255 Set_Next_Named_Actual (Last, Empty);
22256 end Chain;
22257
22258 ---------------
22259 -- Reporting --
22260 ---------------
22261
22262 function Reporting return Boolean is
22263 begin
22264 if not Report then
22265 return False;
22266
22267 elsif not Within_Init_Proc then
22268 return True;
22269
22270 elsif Is_Init_Proc (Entity (Name (N))) then
22271 return False;
22272
22273 else
22274 return True;
22275 end if;
22276 end Reporting;
22277
22278 -- Start of processing for Normalize_Actuals
22279
22280 begin
22281 if Is_Access_Type (S) then
22282
22283 -- The name in the call is a function call that returns an access
22284 -- to subprogram. The designated type has the list of formals.
22285
22286 Formal := First_Formal (Designated_Type (S));
22287 else
22288 Formal := First_Formal (S);
22289 end if;
22290
22291 while Present (Formal) loop
22292 Formals_To_Match := Formals_To_Match + 1;
22293 Next_Formal (Formal);
22294 end loop;
22295
22296 -- Find if there is a named association, and verify that no positional
22297 -- associations appear after named ones.
22298
22299 if Present (Actuals) then
22300 Actual := First (Actuals);
22301 end if;
22302
22303 while Present (Actual)
22304 and then Nkind (Actual) /= N_Parameter_Association
22305 loop
22306 Actuals_To_Match := Actuals_To_Match + 1;
22307 Next (Actual);
22308 end loop;
22309
22310 if No (Actual) and Actuals_To_Match = Formals_To_Match then
22311
22312 -- Most common case: positional notation, no defaults
22313
22314 Success := True;
22315 return;
22316
22317 elsif Actuals_To_Match > Formals_To_Match then
22318
22319 -- Too many actuals: will not work
22320
22321 if Reporting then
22322 if Is_Entity_Name (Name (N)) then
22323 Error_Msg_N ("too many arguments in call to&", Name (N));
22324 else
22325 Error_Msg_N ("too many arguments in call", N);
22326 end if;
22327 end if;
22328
22329 Success := False;
22330 return;
22331 end if;
22332
22333 First_Named := Actual;
22334
22335 while Present (Actual) loop
22336 if Nkind (Actual) /= N_Parameter_Association then
22337 Error_Msg_N
22338 ("positional parameters not allowed after named ones", Actual);
22339 Success := False;
22340 return;
22341
22342 else
22343 Actuals_To_Match := Actuals_To_Match + 1;
22344 end if;
22345
22346 Next (Actual);
22347 end loop;
22348
22349 if Present (Actuals) then
22350 Actual := First (Actuals);
22351 end if;
22352
22353 Formal := First_Formal (S);
22354 while Present (Formal) loop
22355
22356 -- Match the formals in order. If the corresponding actual is
22357 -- positional, nothing to do. Else scan the list of named actuals
22358 -- to find the one with the right name.
22359
22360 if Present (Actual)
22361 and then Nkind (Actual) /= N_Parameter_Association
22362 then
22363 Next (Actual);
22364 Actuals_To_Match := Actuals_To_Match - 1;
22365 Formals_To_Match := Formals_To_Match - 1;
22366
22367 else
22368 -- For named parameters, search the list of actuals to find
22369 -- one that matches the next formal name.
22370
22371 Actual := First_Named;
22372 Found := False;
22373 while Present (Actual) loop
22374 if Chars (Selector_Name (Actual)) = Chars (Formal) then
22375 Found := True;
22376 Chain (Actual);
22377 Actuals_To_Match := Actuals_To_Match - 1;
22378 Formals_To_Match := Formals_To_Match - 1;
22379 exit;
22380 end if;
22381
22382 Next (Actual);
22383 end loop;
22384
22385 if not Found then
22386 if Ekind (Formal) /= E_In_Parameter
22387 or else No (Default_Value (Formal))
22388 then
22389 if Reporting then
22390 if (Comes_From_Source (S)
22391 or else Sloc (S) = Standard_Location)
22392 and then Is_Overloadable (S)
22393 then
22394 if No (Actuals)
22395 and then
22396 Nkind_In (Parent (N), N_Procedure_Call_Statement,
22397 N_Function_Call,
22398 N_Parameter_Association)
22399 and then Ekind (S) /= E_Function
22400 then
22401 Set_Etype (N, Etype (S));
22402
22403 else
22404 Error_Msg_Name_1 := Chars (S);
22405 Error_Msg_Sloc := Sloc (S);
22406 Error_Msg_NE
22407 ("missing argument for parameter & "
22408 & "in call to % declared #", N, Formal);
22409 end if;
22410
22411 elsif Is_Overloadable (S) then
22412 Error_Msg_Name_1 := Chars (S);
22413
22414 -- Point to type derivation that generated the
22415 -- operation.
22416
22417 Error_Msg_Sloc := Sloc (Parent (S));
22418
22419 Error_Msg_NE
22420 ("missing argument for parameter & "
22421 & "in call to % (inherited) #", N, Formal);
22422
22423 else
22424 Error_Msg_NE
22425 ("missing argument for parameter &", N, Formal);
22426 end if;
22427 end if;
22428
22429 Success := False;
22430 return;
22431
22432 else
22433 Formals_To_Match := Formals_To_Match - 1;
22434 end if;
22435 end if;
22436 end if;
22437
22438 Next_Formal (Formal);
22439 end loop;
22440
22441 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
22442 Success := True;
22443 return;
22444
22445 else
22446 if Reporting then
22447
22448 -- Find some superfluous named actual that did not get
22449 -- attached to the list of associations.
22450
22451 Actual := First (Actuals);
22452 while Present (Actual) loop
22453 if Nkind (Actual) = N_Parameter_Association
22454 and then Actual /= Last
22455 and then No (Next_Named_Actual (Actual))
22456 then
22457 -- A validity check may introduce a copy of a call that
22458 -- includes an extra actual (for example for an unrelated
22459 -- accessibility check). Check that the extra actual matches
22460 -- some extra formal, which must exist already because
22461 -- subprogram must be frozen at this point.
22462
22463 if Present (Extra_Formals (S))
22464 and then not Comes_From_Source (Actual)
22465 and then Nkind (Actual) = N_Parameter_Association
22466 and then Chars (Extra_Formals (S)) =
22467 Chars (Selector_Name (Actual))
22468 then
22469 null;
22470 else
22471 Error_Msg_N
22472 ("unmatched actual & in call", Selector_Name (Actual));
22473 exit;
22474 end if;
22475 end if;
22476
22477 Next (Actual);
22478 end loop;
22479 end if;
22480
22481 Success := False;
22482 return;
22483 end if;
22484 end Normalize_Actuals;
22485
22486 --------------------------------
22487 -- Note_Possible_Modification --
22488 --------------------------------
22489
22490 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
22491 Modification_Comes_From_Source : constant Boolean :=
22492 Comes_From_Source (Parent (N));
22493
22494 Ent : Entity_Id;
22495 Exp : Node_Id;
22496
22497 begin
22498 -- Loop to find referenced entity, if there is one
22499
22500 Exp := N;
22501 loop
22502 Ent := Empty;
22503
22504 if Is_Entity_Name (Exp) then
22505 Ent := Entity (Exp);
22506
22507 -- If the entity is missing, it is an undeclared identifier,
22508 -- and there is nothing to annotate.
22509
22510 if No (Ent) then
22511 return;
22512 end if;
22513
22514 elsif Nkind (Exp) = N_Explicit_Dereference then
22515 declare
22516 P : constant Node_Id := Prefix (Exp);
22517
22518 begin
22519 -- In formal verification mode, keep track of all reads and
22520 -- writes through explicit dereferences.
22521
22522 if GNATprove_Mode then
22523 SPARK_Specific.Generate_Dereference (N, 'm');
22524 end if;
22525
22526 if Nkind (P) = N_Selected_Component
22527 and then Present (Entry_Formal (Entity (Selector_Name (P))))
22528 then
22529 -- Case of a reference to an entry formal
22530
22531 Ent := Entry_Formal (Entity (Selector_Name (P)));
22532
22533 elsif Nkind (P) = N_Identifier
22534 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
22535 and then Present (Expression (Parent (Entity (P))))
22536 and then Nkind (Expression (Parent (Entity (P)))) =
22537 N_Reference
22538 then
22539 -- Case of a reference to a value on which side effects have
22540 -- been removed.
22541
22542 Exp := Prefix (Expression (Parent (Entity (P))));
22543 goto Continue;
22544
22545 else
22546 return;
22547 end if;
22548 end;
22549
22550 elsif Nkind_In (Exp, N_Type_Conversion,
22551 N_Unchecked_Type_Conversion)
22552 then
22553 Exp := Expression (Exp);
22554 goto Continue;
22555
22556 elsif Nkind_In (Exp, N_Slice,
22557 N_Indexed_Component,
22558 N_Selected_Component)
22559 then
22560 -- Special check, if the prefix is an access type, then return
22561 -- since we are modifying the thing pointed to, not the prefix.
22562 -- When we are expanding, most usually the prefix is replaced
22563 -- by an explicit dereference, and this test is not needed, but
22564 -- in some cases (notably -gnatc mode and generics) when we do
22565 -- not do full expansion, we need this special test.
22566
22567 if Is_Access_Type (Etype (Prefix (Exp))) then
22568 return;
22569
22570 -- Otherwise go to prefix and keep going
22571
22572 else
22573 Exp := Prefix (Exp);
22574 goto Continue;
22575 end if;
22576
22577 -- All other cases, not a modification
22578
22579 else
22580 return;
22581 end if;
22582
22583 -- Now look for entity being referenced
22584
22585 if Present (Ent) then
22586 if Is_Object (Ent) then
22587 if Comes_From_Source (Exp)
22588 or else Modification_Comes_From_Source
22589 then
22590 -- Give warning if pragma unmodified is given and we are
22591 -- sure this is a modification.
22592
22593 if Has_Pragma_Unmodified (Ent) and then Sure then
22594
22595 -- Note that the entity may be present only as a result
22596 -- of pragma Unused.
22597
22598 if Has_Pragma_Unused (Ent) then
22599 Error_Msg_NE ("??pragma Unused given for &!", N, Ent);
22600 else
22601 Error_Msg_NE
22602 ("??pragma Unmodified given for &!", N, Ent);
22603 end if;
22604 end if;
22605
22606 Set_Never_Set_In_Source (Ent, False);
22607 end if;
22608
22609 Set_Is_True_Constant (Ent, False);
22610 Set_Current_Value (Ent, Empty);
22611 Set_Is_Known_Null (Ent, False);
22612
22613 if not Can_Never_Be_Null (Ent) then
22614 Set_Is_Known_Non_Null (Ent, False);
22615 end if;
22616
22617 -- Follow renaming chain
22618
22619 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
22620 and then Present (Renamed_Object (Ent))
22621 then
22622 Exp := Renamed_Object (Ent);
22623
22624 -- If the entity is the loop variable in an iteration over
22625 -- a container, retrieve container expression to indicate
22626 -- possible modification.
22627
22628 if Present (Related_Expression (Ent))
22629 and then Nkind (Parent (Related_Expression (Ent))) =
22630 N_Iterator_Specification
22631 then
22632 Exp := Original_Node (Related_Expression (Ent));
22633 end if;
22634
22635 goto Continue;
22636
22637 -- The expression may be the renaming of a subcomponent of an
22638 -- array or container. The assignment to the subcomponent is
22639 -- a modification of the container.
22640
22641 elsif Comes_From_Source (Original_Node (Exp))
22642 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
22643 N_Indexed_Component)
22644 then
22645 Exp := Prefix (Original_Node (Exp));
22646 goto Continue;
22647 end if;
22648
22649 -- Generate a reference only if the assignment comes from
22650 -- source. This excludes, for example, calls to a dispatching
22651 -- assignment operation when the left-hand side is tagged. In
22652 -- GNATprove mode, we need those references also on generated
22653 -- code, as these are used to compute the local effects of
22654 -- subprograms.
22655
22656 if Modification_Comes_From_Source or GNATprove_Mode then
22657 Generate_Reference (Ent, Exp, 'm');
22658
22659 -- If the target of the assignment is the bound variable
22660 -- in an iterator, indicate that the corresponding array
22661 -- or container is also modified.
22662
22663 if Ada_Version >= Ada_2012
22664 and then Nkind (Parent (Ent)) = N_Iterator_Specification
22665 then
22666 declare
22667 Domain : constant Node_Id := Name (Parent (Ent));
22668
22669 begin
22670 -- TBD : in the full version of the construct, the
22671 -- domain of iteration can be given by an expression.
22672
22673 if Is_Entity_Name (Domain) then
22674 Generate_Reference (Entity (Domain), Exp, 'm');
22675 Set_Is_True_Constant (Entity (Domain), False);
22676 Set_Never_Set_In_Source (Entity (Domain), False);
22677 end if;
22678 end;
22679 end if;
22680 end if;
22681 end if;
22682
22683 Kill_Checks (Ent);
22684
22685 -- If we are sure this is a modification from source, and we know
22686 -- this modifies a constant, then give an appropriate warning.
22687
22688 if Sure
22689 and then Modification_Comes_From_Source
22690 and then Overlays_Constant (Ent)
22691 and then Address_Clause_Overlay_Warnings
22692 then
22693 declare
22694 Addr : constant Node_Id := Address_Clause (Ent);
22695 O_Ent : Entity_Id;
22696 Off : Boolean;
22697
22698 begin
22699 Find_Overlaid_Entity (Addr, O_Ent, Off);
22700
22701 Error_Msg_Sloc := Sloc (Addr);
22702 Error_Msg_NE
22703 ("??constant& may be modified via address clause#",
22704 N, O_Ent);
22705 end;
22706 end if;
22707
22708 return;
22709 end if;
22710
22711 <<Continue>>
22712 null;
22713 end loop;
22714 end Note_Possible_Modification;
22715
22716 -----------------
22717 -- Null_Status --
22718 -----------------
22719
22720 function Null_Status (N : Node_Id) return Null_Status_Kind is
22721 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean;
22722 -- Determine whether definition Def carries a null exclusion
22723
22724 function Null_Status_Of_Entity (Id : Entity_Id) return Null_Status_Kind;
22725 -- Determine the null status of arbitrary entity Id
22726
22727 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind;
22728 -- Determine the null status of type Typ
22729
22730 ---------------------------
22731 -- Is_Null_Excluding_Def --
22732 ---------------------------
22733
22734 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean is
22735 begin
22736 return
22737 Nkind_In (Def, N_Access_Definition,
22738 N_Access_Function_Definition,
22739 N_Access_Procedure_Definition,
22740 N_Access_To_Object_Definition,
22741 N_Component_Definition,
22742 N_Derived_Type_Definition)
22743 and then Null_Exclusion_Present (Def);
22744 end Is_Null_Excluding_Def;
22745
22746 ---------------------------
22747 -- Null_Status_Of_Entity --
22748 ---------------------------
22749
22750 function Null_Status_Of_Entity
22751 (Id : Entity_Id) return Null_Status_Kind
22752 is
22753 Decl : constant Node_Id := Declaration_Node (Id);
22754 Def : Node_Id;
22755
22756 begin
22757 -- The value of an imported or exported entity may be set externally
22758 -- regardless of a null exclusion. As a result, the value cannot be
22759 -- determined statically.
22760
22761 if Is_Imported (Id) or else Is_Exported (Id) then
22762 return Unknown;
22763
22764 elsif Nkind_In (Decl, N_Component_Declaration,
22765 N_Discriminant_Specification,
22766 N_Formal_Object_Declaration,
22767 N_Object_Declaration,
22768 N_Object_Renaming_Declaration,
22769 N_Parameter_Specification)
22770 then
22771 -- A component declaration yields a non-null value when either
22772 -- its component definition or access definition carries a null
22773 -- exclusion.
22774
22775 if Nkind (Decl) = N_Component_Declaration then
22776 Def := Component_Definition (Decl);
22777
22778 if Is_Null_Excluding_Def (Def) then
22779 return Is_Non_Null;
22780 end if;
22781
22782 Def := Access_Definition (Def);
22783
22784 if Present (Def) and then Is_Null_Excluding_Def (Def) then
22785 return Is_Non_Null;
22786 end if;
22787
22788 -- A formal object declaration yields a non-null value if its
22789 -- access definition carries a null exclusion. If the object is
22790 -- default initialized, then the value depends on the expression.
22791
22792 elsif Nkind (Decl) = N_Formal_Object_Declaration then
22793 Def := Access_Definition (Decl);
22794
22795 if Present (Def) and then Is_Null_Excluding_Def (Def) then
22796 return Is_Non_Null;
22797 end if;
22798
22799 -- A constant may yield a null or non-null value depending on its
22800 -- initialization expression.
22801
22802 elsif Ekind (Id) = E_Constant then
22803 return Null_Status (Constant_Value (Id));
22804
22805 -- The construct yields a non-null value when it has a null
22806 -- exclusion.
22807
22808 elsif Null_Exclusion_Present (Decl) then
22809 return Is_Non_Null;
22810
22811 -- An object renaming declaration yields a non-null value if its
22812 -- access definition carries a null exclusion. Otherwise the value
22813 -- depends on the renamed name.
22814
22815 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
22816 Def := Access_Definition (Decl);
22817
22818 if Present (Def) and then Is_Null_Excluding_Def (Def) then
22819 return Is_Non_Null;
22820
22821 else
22822 return Null_Status (Name (Decl));
22823 end if;
22824 end if;
22825 end if;
22826
22827 -- At this point the declaration of the entity does not carry a null
22828 -- exclusion and lacks an initialization expression. Check the status
22829 -- of its type.
22830
22831 return Null_Status_Of_Type (Etype (Id));
22832 end Null_Status_Of_Entity;
22833
22834 -------------------------
22835 -- Null_Status_Of_Type --
22836 -------------------------
22837
22838 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind is
22839 Curr : Entity_Id;
22840 Decl : Node_Id;
22841
22842 begin
22843 -- Traverse the type chain looking for types with null exclusion
22844
22845 Curr := Typ;
22846 while Present (Curr) and then Etype (Curr) /= Curr loop
22847 Decl := Parent (Curr);
22848
22849 -- Guard against itypes which do not always have declarations. A
22850 -- type yields a non-null value if it carries a null exclusion.
22851
22852 if Present (Decl) then
22853 if Nkind (Decl) = N_Full_Type_Declaration
22854 and then Is_Null_Excluding_Def (Type_Definition (Decl))
22855 then
22856 return Is_Non_Null;
22857
22858 elsif Nkind (Decl) = N_Subtype_Declaration
22859 and then Null_Exclusion_Present (Decl)
22860 then
22861 return Is_Non_Null;
22862 end if;
22863 end if;
22864
22865 Curr := Etype (Curr);
22866 end loop;
22867
22868 -- The type chain does not contain any null excluding types
22869
22870 return Unknown;
22871 end Null_Status_Of_Type;
22872
22873 -- Start of processing for Null_Status
22874
22875 begin
22876 -- Prevent cascaded errors or infinite loops when trying to determine
22877 -- the null status of an erroneous construct.
22878
22879 if Error_Posted (N) then
22880 return Unknown;
22881
22882 -- An allocator always creates a non-null value
22883
22884 elsif Nkind (N) = N_Allocator then
22885 return Is_Non_Null;
22886
22887 -- Taking the 'Access of something yields a non-null value
22888
22889 elsif Nkind (N) = N_Attribute_Reference
22890 and then Nam_In (Attribute_Name (N), Name_Access,
22891 Name_Unchecked_Access,
22892 Name_Unrestricted_Access)
22893 then
22894 return Is_Non_Null;
22895
22896 -- "null" yields null
22897
22898 elsif Nkind (N) = N_Null then
22899 return Is_Null;
22900
22901 -- Check the status of the operand of a type conversion
22902
22903 elsif Nkind (N) = N_Type_Conversion then
22904 return Null_Status (Expression (N));
22905
22906 -- The input denotes a reference to an entity. Determine whether the
22907 -- entity or its type yields a null or non-null value.
22908
22909 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
22910 return Null_Status_Of_Entity (Entity (N));
22911 end if;
22912
22913 -- Otherwise it is not possible to determine the null status of the
22914 -- subexpression at compile time without resorting to simple flow
22915 -- analysis.
22916
22917 return Unknown;
22918 end Null_Status;
22919
22920 --------------------------------------
22921 -- Null_To_Null_Address_Convert_OK --
22922 --------------------------------------
22923
22924 function Null_To_Null_Address_Convert_OK
22925 (N : Node_Id;
22926 Typ : Entity_Id := Empty) return Boolean
22927 is
22928 begin
22929 if not Relaxed_RM_Semantics then
22930 return False;
22931 end if;
22932
22933 if Nkind (N) = N_Null then
22934 return Present (Typ) and then Is_Descendant_Of_Address (Typ);
22935
22936 elsif Nkind_In (N, N_Op_Eq, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt, N_Op_Ne)
22937 then
22938 declare
22939 L : constant Node_Id := Left_Opnd (N);
22940 R : constant Node_Id := Right_Opnd (N);
22941
22942 begin
22943 -- We check the Etype of the complementary operand since the
22944 -- N_Null node is not decorated at this stage.
22945
22946 return
22947 ((Nkind (L) = N_Null
22948 and then Is_Descendant_Of_Address (Etype (R)))
22949 or else
22950 (Nkind (R) = N_Null
22951 and then Is_Descendant_Of_Address (Etype (L))));
22952 end;
22953 end if;
22954
22955 return False;
22956 end Null_To_Null_Address_Convert_OK;
22957
22958 ---------------------------------
22959 -- Number_Of_Elements_In_Array --
22960 ---------------------------------
22961
22962 function Number_Of_Elements_In_Array (T : Entity_Id) return Int is
22963 Indx : Node_Id;
22964 Typ : Entity_Id;
22965 Low : Node_Id;
22966 High : Node_Id;
22967 Num : Int := 1;
22968
22969 begin
22970 pragma Assert (Is_Array_Type (T));
22971
22972 Indx := First_Index (T);
22973 while Present (Indx) loop
22974 Typ := Underlying_Type (Etype (Indx));
22975
22976 -- Never look at junk bounds of a generic type
22977
22978 if Is_Generic_Type (Typ) then
22979 return 0;
22980 end if;
22981
22982 -- Check the array bounds are known at compile time and return zero
22983 -- if they are not.
22984
22985 Low := Type_Low_Bound (Typ);
22986 High := Type_High_Bound (Typ);
22987
22988 if not Compile_Time_Known_Value (Low) then
22989 return 0;
22990 elsif not Compile_Time_Known_Value (High) then
22991 return 0;
22992 else
22993 Num :=
22994 Num * UI_To_Int ((Expr_Value (High) - Expr_Value (Low) + 1));
22995 end if;
22996
22997 Next_Index (Indx);
22998 end loop;
22999
23000 return Num;
23001 end Number_Of_Elements_In_Array;
23002
23003 -------------------------
23004 -- Object_Access_Level --
23005 -------------------------
23006
23007 -- Returns the static accessibility level of the view denoted by Obj. Note
23008 -- that the value returned is the result of a call to Scope_Depth. Only
23009 -- scope depths associated with dynamic scopes can actually be returned.
23010 -- Since only relative levels matter for accessibility checking, the fact
23011 -- that the distance between successive levels of accessibility is not
23012 -- always one is immaterial (invariant: if level(E2) is deeper than
23013 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
23014
23015 function Object_Access_Level (Obj : Node_Id) return Uint is
23016 function Is_Interface_Conversion (N : Node_Id) return Boolean;
23017 -- Determine whether N is a construct of the form
23018 -- Some_Type (Operand._tag'Address)
23019 -- This construct appears in the context of dispatching calls.
23020
23021 function Reference_To (Obj : Node_Id) return Node_Id;
23022 -- An explicit dereference is created when removing side effects from
23023 -- expressions for constraint checking purposes. In this case a local
23024 -- access type is created for it. The correct access level is that of
23025 -- the original source node. We detect this case by noting that the
23026 -- prefix of the dereference is created by an object declaration whose
23027 -- initial expression is a reference.
23028
23029 -----------------------------
23030 -- Is_Interface_Conversion --
23031 -----------------------------
23032
23033 function Is_Interface_Conversion (N : Node_Id) return Boolean is
23034 begin
23035 return Nkind (N) = N_Unchecked_Type_Conversion
23036 and then Nkind (Expression (N)) = N_Attribute_Reference
23037 and then Attribute_Name (Expression (N)) = Name_Address;
23038 end Is_Interface_Conversion;
23039
23040 ------------------
23041 -- Reference_To --
23042 ------------------
23043
23044 function Reference_To (Obj : Node_Id) return Node_Id is
23045 Pref : constant Node_Id := Prefix (Obj);
23046 begin
23047 if Is_Entity_Name (Pref)
23048 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
23049 and then Present (Expression (Parent (Entity (Pref))))
23050 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
23051 then
23052 return (Prefix (Expression (Parent (Entity (Pref)))));
23053 else
23054 return Empty;
23055 end if;
23056 end Reference_To;
23057
23058 -- Local variables
23059
23060 E : Entity_Id;
23061
23062 -- Start of processing for Object_Access_Level
23063
23064 begin
23065 if Nkind (Obj) = N_Defining_Identifier
23066 or else Is_Entity_Name (Obj)
23067 then
23068 if Nkind (Obj) = N_Defining_Identifier then
23069 E := Obj;
23070 else
23071 E := Entity (Obj);
23072 end if;
23073
23074 if Is_Prival (E) then
23075 E := Prival_Link (E);
23076 end if;
23077
23078 -- If E is a type then it denotes a current instance. For this case
23079 -- we add one to the normal accessibility level of the type to ensure
23080 -- that current instances are treated as always being deeper than
23081 -- than the level of any visible named access type (see 3.10.2(21)).
23082
23083 if Is_Type (E) then
23084 return Type_Access_Level (E) + 1;
23085
23086 elsif Present (Renamed_Object (E)) then
23087 return Object_Access_Level (Renamed_Object (E));
23088
23089 -- Similarly, if E is a component of the current instance of a
23090 -- protected type, any instance of it is assumed to be at a deeper
23091 -- level than the type. For a protected object (whose type is an
23092 -- anonymous protected type) its components are at the same level
23093 -- as the type itself.
23094
23095 elsif not Is_Overloadable (E)
23096 and then Ekind (Scope (E)) = E_Protected_Type
23097 and then Comes_From_Source (Scope (E))
23098 then
23099 return Type_Access_Level (Scope (E)) + 1;
23100
23101 else
23102 -- Aliased formals of functions take their access level from the
23103 -- point of call, i.e. require a dynamic check. For static check
23104 -- purposes, this is smaller than the level of the subprogram
23105 -- itself. For procedures the aliased makes no difference.
23106
23107 if Is_Formal (E)
23108 and then Is_Aliased (E)
23109 and then Ekind (Scope (E)) = E_Function
23110 then
23111 return Type_Access_Level (Etype (E));
23112
23113 else
23114 return Scope_Depth (Enclosing_Dynamic_Scope (E));
23115 end if;
23116 end if;
23117
23118 elsif Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
23119 if Is_Access_Type (Etype (Prefix (Obj))) then
23120 return Type_Access_Level (Etype (Prefix (Obj)));
23121 else
23122 return Object_Access_Level (Prefix (Obj));
23123 end if;
23124
23125 elsif Nkind (Obj) = N_Explicit_Dereference then
23126
23127 -- If the prefix is a selected access discriminant then we make a
23128 -- recursive call on the prefix, which will in turn check the level
23129 -- of the prefix object of the selected discriminant.
23130
23131 -- In Ada 2012, if the discriminant has implicit dereference and
23132 -- the context is a selected component, treat this as an object of
23133 -- unknown scope (see below). This is necessary in compile-only mode;
23134 -- otherwise expansion will already have transformed the prefix into
23135 -- a temporary.
23136
23137 if Nkind (Prefix (Obj)) = N_Selected_Component
23138 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
23139 and then
23140 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
23141 and then
23142 (not Has_Implicit_Dereference
23143 (Entity (Selector_Name (Prefix (Obj))))
23144 or else Nkind (Parent (Obj)) /= N_Selected_Component)
23145 then
23146 return Object_Access_Level (Prefix (Obj));
23147
23148 -- Detect an interface conversion in the context of a dispatching
23149 -- call. Use the original form of the conversion to find the access
23150 -- level of the operand.
23151
23152 elsif Is_Interface (Etype (Obj))
23153 and then Is_Interface_Conversion (Prefix (Obj))
23154 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
23155 then
23156 return Object_Access_Level (Original_Node (Obj));
23157
23158 elsif not Comes_From_Source (Obj) then
23159 declare
23160 Ref : constant Node_Id := Reference_To (Obj);
23161 begin
23162 if Present (Ref) then
23163 return Object_Access_Level (Ref);
23164 else
23165 return Type_Access_Level (Etype (Prefix (Obj)));
23166 end if;
23167 end;
23168
23169 else
23170 return Type_Access_Level (Etype (Prefix (Obj)));
23171 end if;
23172
23173 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
23174 return Object_Access_Level (Expression (Obj));
23175
23176 elsif Nkind (Obj) = N_Function_Call then
23177
23178 -- Function results are objects, so we get either the access level of
23179 -- the function or, in the case of an indirect call, the level of the
23180 -- access-to-subprogram type. (This code is used for Ada 95, but it
23181 -- looks wrong, because it seems that we should be checking the level
23182 -- of the call itself, even for Ada 95. However, using the Ada 2005
23183 -- version of the code causes regressions in several tests that are
23184 -- compiled with -gnat95. ???)
23185
23186 if Ada_Version < Ada_2005 then
23187 if Is_Entity_Name (Name (Obj)) then
23188 return Subprogram_Access_Level (Entity (Name (Obj)));
23189 else
23190 return Type_Access_Level (Etype (Prefix (Name (Obj))));
23191 end if;
23192
23193 -- For Ada 2005, the level of the result object of a function call is
23194 -- defined to be the level of the call's innermost enclosing master.
23195 -- We determine that by querying the depth of the innermost enclosing
23196 -- dynamic scope.
23197
23198 else
23199 Return_Master_Scope_Depth_Of_Call : declare
23200 function Innermost_Master_Scope_Depth
23201 (N : Node_Id) return Uint;
23202 -- Returns the scope depth of the given node's innermost
23203 -- enclosing dynamic scope (effectively the accessibility
23204 -- level of the innermost enclosing master).
23205
23206 ----------------------------------
23207 -- Innermost_Master_Scope_Depth --
23208 ----------------------------------
23209
23210 function Innermost_Master_Scope_Depth
23211 (N : Node_Id) return Uint
23212 is
23213 Node_Par : Node_Id := Parent (N);
23214
23215 begin
23216 -- Locate the nearest enclosing node (by traversing Parents)
23217 -- that Defining_Entity can be applied to, and return the
23218 -- depth of that entity's nearest enclosing dynamic scope.
23219
23220 while Present (Node_Par) loop
23221 case Nkind (Node_Par) is
23222 when N_Abstract_Subprogram_Declaration
23223 | N_Block_Statement
23224 | N_Body_Stub
23225 | N_Component_Declaration
23226 | N_Entry_Body
23227 | N_Entry_Declaration
23228 | N_Exception_Declaration
23229 | N_Formal_Object_Declaration
23230 | N_Formal_Package_Declaration
23231 | N_Formal_Subprogram_Declaration
23232 | N_Formal_Type_Declaration
23233 | N_Full_Type_Declaration
23234 | N_Function_Specification
23235 | N_Generic_Declaration
23236 | N_Generic_Instantiation
23237 | N_Implicit_Label_Declaration
23238 | N_Incomplete_Type_Declaration
23239 | N_Loop_Parameter_Specification
23240 | N_Number_Declaration
23241 | N_Object_Declaration
23242 | N_Package_Declaration
23243 | N_Package_Specification
23244 | N_Parameter_Specification
23245 | N_Private_Extension_Declaration
23246 | N_Private_Type_Declaration
23247 | N_Procedure_Specification
23248 | N_Proper_Body
23249 | N_Protected_Type_Declaration
23250 | N_Renaming_Declaration
23251 | N_Single_Protected_Declaration
23252 | N_Single_Task_Declaration
23253 | N_Subprogram_Declaration
23254 | N_Subtype_Declaration
23255 | N_Subunit
23256 | N_Task_Type_Declaration
23257 =>
23258 return Scope_Depth
23259 (Nearest_Dynamic_Scope
23260 (Defining_Entity (Node_Par)));
23261
23262 -- For a return statement within a function, return
23263 -- the depth of the function itself. This is not just
23264 -- a small optimization, but matters when analyzing
23265 -- the expression in an expression function before
23266 -- the body is created.
23267
23268 when N_Simple_Return_Statement =>
23269 if Ekind (Current_Scope) = E_Function then
23270 return Scope_Depth (Current_Scope);
23271 end if;
23272
23273 when others =>
23274 null;
23275 end case;
23276
23277 Node_Par := Parent (Node_Par);
23278 end loop;
23279
23280 pragma Assert (False);
23281
23282 -- Should never reach the following return
23283
23284 return Scope_Depth (Current_Scope) + 1;
23285 end Innermost_Master_Scope_Depth;
23286
23287 -- Start of processing for Return_Master_Scope_Depth_Of_Call
23288
23289 begin
23290 return Innermost_Master_Scope_Depth (Obj);
23291 end Return_Master_Scope_Depth_Of_Call;
23292 end if;
23293
23294 -- For convenience we handle qualified expressions, even though they
23295 -- aren't technically object names.
23296
23297 elsif Nkind (Obj) = N_Qualified_Expression then
23298 return Object_Access_Level (Expression (Obj));
23299
23300 -- Ditto for aggregates. They have the level of the temporary that
23301 -- will hold their value.
23302
23303 elsif Nkind (Obj) = N_Aggregate then
23304 return Object_Access_Level (Current_Scope);
23305
23306 -- Otherwise return the scope level of Standard. (If there are cases
23307 -- that fall through to this point they will be treated as having
23308 -- global accessibility for now. ???)
23309
23310 else
23311 return Scope_Depth (Standard_Standard);
23312 end if;
23313 end Object_Access_Level;
23314
23315 ----------------------------------
23316 -- Old_Requires_Transient_Scope --
23317 ----------------------------------
23318
23319 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
23320 Typ : constant Entity_Id := Underlying_Type (Id);
23321
23322 begin
23323 -- This is a private type which is not completed yet. This can only
23324 -- happen in a default expression (of a formal parameter or of a
23325 -- record component). Do not expand transient scope in this case.
23326
23327 if No (Typ) then
23328 return False;
23329
23330 -- Do not expand transient scope for non-existent procedure return
23331
23332 elsif Typ = Standard_Void_Type then
23333 return False;
23334
23335 -- Elementary types do not require a transient scope
23336
23337 elsif Is_Elementary_Type (Typ) then
23338 return False;
23339
23340 -- Generally, indefinite subtypes require a transient scope, since the
23341 -- back end cannot generate temporaries, since this is not a valid type
23342 -- for declaring an object. It might be possible to relax this in the
23343 -- future, e.g. by declaring the maximum possible space for the type.
23344
23345 elsif not Is_Definite_Subtype (Typ) then
23346 return True;
23347
23348 -- Functions returning tagged types may dispatch on result so their
23349 -- returned value is allocated on the secondary stack. Controlled
23350 -- type temporaries need finalization.
23351
23352 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
23353 return True;
23354
23355 -- Record type
23356
23357 elsif Is_Record_Type (Typ) then
23358 declare
23359 Comp : Entity_Id;
23360
23361 begin
23362 Comp := First_Entity (Typ);
23363 while Present (Comp) loop
23364 if Ekind (Comp) = E_Component then
23365
23366 -- ???It's not clear we need a full recursive call to
23367 -- Old_Requires_Transient_Scope here. Note that the
23368 -- following can't happen.
23369
23370 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
23371 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
23372
23373 if Old_Requires_Transient_Scope (Etype (Comp)) then
23374 return True;
23375 end if;
23376 end if;
23377
23378 Next_Entity (Comp);
23379 end loop;
23380 end;
23381
23382 return False;
23383
23384 -- String literal types never require transient scope
23385
23386 elsif Ekind (Typ) = E_String_Literal_Subtype then
23387 return False;
23388
23389 -- Array type. Note that we already know that this is a constrained
23390 -- array, since unconstrained arrays will fail the indefinite test.
23391
23392 elsif Is_Array_Type (Typ) then
23393
23394 -- If component type requires a transient scope, the array does too
23395
23396 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
23397 return True;
23398
23399 -- Otherwise, we only need a transient scope if the size depends on
23400 -- the value of one or more discriminants.
23401
23402 else
23403 return Size_Depends_On_Discriminant (Typ);
23404 end if;
23405
23406 -- All other cases do not require a transient scope
23407
23408 else
23409 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
23410 return False;
23411 end if;
23412 end Old_Requires_Transient_Scope;
23413
23414 ---------------------------------
23415 -- Original_Aspect_Pragma_Name --
23416 ---------------------------------
23417
23418 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
23419 Item : Node_Id;
23420 Item_Nam : Name_Id;
23421
23422 begin
23423 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
23424
23425 Item := N;
23426
23427 -- The pragma was generated to emulate an aspect, use the original
23428 -- aspect specification.
23429
23430 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
23431 Item := Corresponding_Aspect (Item);
23432 end if;
23433
23434 -- Retrieve the name of the aspect/pragma. As assertion pragmas from
23435 -- a generic instantiation might have been rewritten into pragma Check,
23436 -- we look at the original node for Item. Note also that Pre, Pre_Class,
23437 -- Post and Post_Class rewrite their pragma identifier to preserve the
23438 -- original name, so we look at the original node for the identifier.
23439 -- ??? this is kludgey
23440
23441 if Nkind (Item) = N_Pragma then
23442 Item_Nam :=
23443 Chars (Original_Node (Pragma_Identifier (Original_Node (Item))));
23444
23445 else
23446 pragma Assert (Nkind (Item) = N_Aspect_Specification);
23447 Item_Nam := Chars (Identifier (Item));
23448 end if;
23449
23450 -- Deal with 'Class by converting the name to its _XXX form
23451
23452 if Class_Present (Item) then
23453 if Item_Nam = Name_Invariant then
23454 Item_Nam := Name_uInvariant;
23455
23456 elsif Item_Nam = Name_Post then
23457 Item_Nam := Name_uPost;
23458
23459 elsif Item_Nam = Name_Pre then
23460 Item_Nam := Name_uPre;
23461
23462 elsif Nam_In (Item_Nam, Name_Type_Invariant,
23463 Name_Type_Invariant_Class)
23464 then
23465 Item_Nam := Name_uType_Invariant;
23466
23467 -- Nothing to do for other cases (e.g. a Check that derived from
23468 -- Pre_Class and has the flag set). Also we do nothing if the name
23469 -- is already in special _xxx form.
23470
23471 end if;
23472 end if;
23473
23474 return Item_Nam;
23475 end Original_Aspect_Pragma_Name;
23476
23477 --------------------------------------
23478 -- Original_Corresponding_Operation --
23479 --------------------------------------
23480
23481 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
23482 is
23483 Typ : constant Entity_Id := Find_Dispatching_Type (S);
23484
23485 begin
23486 -- If S is an inherited primitive S2 the original corresponding
23487 -- operation of S is the original corresponding operation of S2
23488
23489 if Present (Alias (S))
23490 and then Find_Dispatching_Type (Alias (S)) /= Typ
23491 then
23492 return Original_Corresponding_Operation (Alias (S));
23493
23494 -- If S overrides an inherited subprogram S2 the original corresponding
23495 -- operation of S is the original corresponding operation of S2
23496
23497 elsif Present (Overridden_Operation (S)) then
23498 return Original_Corresponding_Operation (Overridden_Operation (S));
23499
23500 -- otherwise it is S itself
23501
23502 else
23503 return S;
23504 end if;
23505 end Original_Corresponding_Operation;
23506
23507 -------------------
23508 -- Output_Entity --
23509 -------------------
23510
23511 procedure Output_Entity (Id : Entity_Id) is
23512 Scop : Entity_Id;
23513
23514 begin
23515 Scop := Scope (Id);
23516
23517 -- The entity may lack a scope when it is in the process of being
23518 -- analyzed. Use the current scope as an approximation.
23519
23520 if No (Scop) then
23521 Scop := Current_Scope;
23522 end if;
23523
23524 Output_Name (Chars (Id), Scop);
23525 end Output_Entity;
23526
23527 -----------------
23528 -- Output_Name --
23529 -----------------
23530
23531 procedure Output_Name (Nam : Name_Id; Scop : Entity_Id := Current_Scope) is
23532 begin
23533 Write_Str
23534 (Get_Name_String
23535 (Get_Qualified_Name
23536 (Nam => Nam,
23537 Suffix => No_Name,
23538 Scop => Scop)));
23539 Write_Eol;
23540 end Output_Name;
23541
23542 ----------------------
23543 -- Policy_In_Effect --
23544 ----------------------
23545
23546 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
23547 function Policy_In_List (List : Node_Id) return Name_Id;
23548 -- Determine the mode of a policy in a N_Pragma list
23549
23550 --------------------
23551 -- Policy_In_List --
23552 --------------------
23553
23554 function Policy_In_List (List : Node_Id) return Name_Id is
23555 Arg1 : Node_Id;
23556 Arg2 : Node_Id;
23557 Prag : Node_Id;
23558
23559 begin
23560 Prag := List;
23561 while Present (Prag) loop
23562 Arg1 := First (Pragma_Argument_Associations (Prag));
23563 Arg2 := Next (Arg1);
23564
23565 Arg1 := Get_Pragma_Arg (Arg1);
23566 Arg2 := Get_Pragma_Arg (Arg2);
23567
23568 -- The current Check_Policy pragma matches the requested policy or
23569 -- appears in the single argument form (Assertion, policy_id).
23570
23571 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
23572 return Chars (Arg2);
23573 end if;
23574
23575 Prag := Next_Pragma (Prag);
23576 end loop;
23577
23578 return No_Name;
23579 end Policy_In_List;
23580
23581 -- Local variables
23582
23583 Kind : Name_Id;
23584
23585 -- Start of processing for Policy_In_Effect
23586
23587 begin
23588 if not Is_Valid_Assertion_Kind (Policy) then
23589 raise Program_Error;
23590 end if;
23591
23592 -- Inspect all policy pragmas that appear within scopes (if any)
23593
23594 Kind := Policy_In_List (Check_Policy_List);
23595
23596 -- Inspect all configuration policy pragmas (if any)
23597
23598 if Kind = No_Name then
23599 Kind := Policy_In_List (Check_Policy_List_Config);
23600 end if;
23601
23602 -- The context lacks policy pragmas, determine the mode based on whether
23603 -- assertions are enabled at the configuration level. This ensures that
23604 -- the policy is preserved when analyzing generics.
23605
23606 if Kind = No_Name then
23607 if Assertions_Enabled_Config then
23608 Kind := Name_Check;
23609 else
23610 Kind := Name_Ignore;
23611 end if;
23612 end if;
23613
23614 -- In CodePeer mode and GNATprove mode, we need to consider all
23615 -- assertions, unless they are disabled. Force Name_Check on
23616 -- ignored assertions.
23617
23618 if Nam_In (Kind, Name_Ignore, Name_Off)
23619 and then (CodePeer_Mode or GNATprove_Mode)
23620 then
23621 Kind := Name_Check;
23622 end if;
23623
23624 return Kind;
23625 end Policy_In_Effect;
23626
23627 ----------------------------------
23628 -- Predicate_Tests_On_Arguments --
23629 ----------------------------------
23630
23631 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
23632 begin
23633 -- Always test predicates on indirect call
23634
23635 if Ekind (Subp) = E_Subprogram_Type then
23636 return True;
23637
23638 -- Do not test predicates on call to generated default Finalize, since
23639 -- we are not interested in whether something we are finalizing (and
23640 -- typically destroying) satisfies its predicates.
23641
23642 elsif Chars (Subp) = Name_Finalize
23643 and then not Comes_From_Source (Subp)
23644 then
23645 return False;
23646
23647 -- Do not test predicates on any internally generated routines
23648
23649 elsif Is_Internal_Name (Chars (Subp)) then
23650 return False;
23651
23652 -- Do not test predicates on call to Init_Proc, since if needed the
23653 -- predicate test will occur at some other point.
23654
23655 elsif Is_Init_Proc (Subp) then
23656 return False;
23657
23658 -- Do not test predicates on call to predicate function, since this
23659 -- would cause infinite recursion.
23660
23661 elsif Ekind (Subp) = E_Function
23662 and then (Is_Predicate_Function (Subp)
23663 or else
23664 Is_Predicate_Function_M (Subp))
23665 then
23666 return False;
23667
23668 -- For now, no other exceptions
23669
23670 else
23671 return True;
23672 end if;
23673 end Predicate_Tests_On_Arguments;
23674
23675 -----------------------
23676 -- Private_Component --
23677 -----------------------
23678
23679 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
23680 Ancestor : constant Entity_Id := Base_Type (Type_Id);
23681
23682 function Trace_Components
23683 (T : Entity_Id;
23684 Check : Boolean) return Entity_Id;
23685 -- Recursive function that does the work, and checks against circular
23686 -- definition for each subcomponent type.
23687
23688 ----------------------
23689 -- Trace_Components --
23690 ----------------------
23691
23692 function Trace_Components
23693 (T : Entity_Id;
23694 Check : Boolean) return Entity_Id
23695 is
23696 Btype : constant Entity_Id := Base_Type (T);
23697 Component : Entity_Id;
23698 P : Entity_Id;
23699 Candidate : Entity_Id := Empty;
23700
23701 begin
23702 if Check and then Btype = Ancestor then
23703 Error_Msg_N ("circular type definition", Type_Id);
23704 return Any_Type;
23705 end if;
23706
23707 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
23708 if Present (Full_View (Btype))
23709 and then Is_Record_Type (Full_View (Btype))
23710 and then not Is_Frozen (Btype)
23711 then
23712 -- To indicate that the ancestor depends on a private type, the
23713 -- current Btype is sufficient. However, to check for circular
23714 -- definition we must recurse on the full view.
23715
23716 Candidate := Trace_Components (Full_View (Btype), True);
23717
23718 if Candidate = Any_Type then
23719 return Any_Type;
23720 else
23721 return Btype;
23722 end if;
23723
23724 else
23725 return Btype;
23726 end if;
23727
23728 elsif Is_Array_Type (Btype) then
23729 return Trace_Components (Component_Type (Btype), True);
23730
23731 elsif Is_Record_Type (Btype) then
23732 Component := First_Entity (Btype);
23733 while Present (Component)
23734 and then Comes_From_Source (Component)
23735 loop
23736 -- Skip anonymous types generated by constrained components
23737
23738 if not Is_Type (Component) then
23739 P := Trace_Components (Etype (Component), True);
23740
23741 if Present (P) then
23742 if P = Any_Type then
23743 return P;
23744 else
23745 Candidate := P;
23746 end if;
23747 end if;
23748 end if;
23749
23750 Next_Entity (Component);
23751 end loop;
23752
23753 return Candidate;
23754
23755 else
23756 return Empty;
23757 end if;
23758 end Trace_Components;
23759
23760 -- Start of processing for Private_Component
23761
23762 begin
23763 return Trace_Components (Type_Id, False);
23764 end Private_Component;
23765
23766 ---------------------------
23767 -- Primitive_Names_Match --
23768 ---------------------------
23769
23770 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
23771 function Non_Internal_Name (E : Entity_Id) return Name_Id;
23772 -- Given an internal name, returns the corresponding non-internal name
23773
23774 ------------------------
23775 -- Non_Internal_Name --
23776 ------------------------
23777
23778 function Non_Internal_Name (E : Entity_Id) return Name_Id is
23779 begin
23780 Get_Name_String (Chars (E));
23781 Name_Len := Name_Len - 1;
23782 return Name_Find;
23783 end Non_Internal_Name;
23784
23785 -- Start of processing for Primitive_Names_Match
23786
23787 begin
23788 pragma Assert (Present (E1) and then Present (E2));
23789
23790 return Chars (E1) = Chars (E2)
23791 or else
23792 (not Is_Internal_Name (Chars (E1))
23793 and then Is_Internal_Name (Chars (E2))
23794 and then Non_Internal_Name (E2) = Chars (E1))
23795 or else
23796 (not Is_Internal_Name (Chars (E2))
23797 and then Is_Internal_Name (Chars (E1))
23798 and then Non_Internal_Name (E1) = Chars (E2))
23799 or else
23800 (Is_Predefined_Dispatching_Operation (E1)
23801 and then Is_Predefined_Dispatching_Operation (E2)
23802 and then Same_TSS (E1, E2))
23803 or else
23804 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
23805 end Primitive_Names_Match;
23806
23807 -----------------------
23808 -- Process_End_Label --
23809 -----------------------
23810
23811 procedure Process_End_Label
23812 (N : Node_Id;
23813 Typ : Character;
23814 Ent : Entity_Id)
23815 is
23816 Loc : Source_Ptr;
23817 Nam : Node_Id;
23818 Scop : Entity_Id;
23819
23820 Label_Ref : Boolean;
23821 -- Set True if reference to end label itself is required
23822
23823 Endl : Node_Id;
23824 -- Gets set to the operator symbol or identifier that references the
23825 -- entity Ent. For the child unit case, this is the identifier from the
23826 -- designator. For other cases, this is simply Endl.
23827
23828 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
23829 -- N is an identifier node that appears as a parent unit reference in
23830 -- the case where Ent is a child unit. This procedure generates an
23831 -- appropriate cross-reference entry. E is the corresponding entity.
23832
23833 -------------------------
23834 -- Generate_Parent_Ref --
23835 -------------------------
23836
23837 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
23838 begin
23839 -- If names do not match, something weird, skip reference
23840
23841 if Chars (E) = Chars (N) then
23842
23843 -- Generate the reference. We do NOT consider this as a reference
23844 -- for unreferenced symbol purposes.
23845
23846 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
23847
23848 if Style_Check then
23849 Style.Check_Identifier (N, E);
23850 end if;
23851 end if;
23852 end Generate_Parent_Ref;
23853
23854 -- Start of processing for Process_End_Label
23855
23856 begin
23857 -- If no node, ignore. This happens in some error situations, and
23858 -- also for some internally generated structures where no end label
23859 -- references are required in any case.
23860
23861 if No (N) then
23862 return;
23863 end if;
23864
23865 -- Nothing to do if no End_Label, happens for internally generated
23866 -- constructs where we don't want an end label reference anyway. Also
23867 -- nothing to do if Endl is a string literal, which means there was
23868 -- some prior error (bad operator symbol)
23869
23870 Endl := End_Label (N);
23871
23872 if No (Endl) or else Nkind (Endl) = N_String_Literal then
23873 return;
23874 end if;
23875
23876 -- Reference node is not in extended main source unit
23877
23878 if not In_Extended_Main_Source_Unit (N) then
23879
23880 -- Generally we do not collect references except for the extended
23881 -- main source unit. The one exception is the 'e' entry for a
23882 -- package spec, where it is useful for a client to have the
23883 -- ending information to define scopes.
23884
23885 if Typ /= 'e' then
23886 return;
23887
23888 else
23889 Label_Ref := False;
23890
23891 -- For this case, we can ignore any parent references, but we
23892 -- need the package name itself for the 'e' entry.
23893
23894 if Nkind (Endl) = N_Designator then
23895 Endl := Identifier (Endl);
23896 end if;
23897 end if;
23898
23899 -- Reference is in extended main source unit
23900
23901 else
23902 Label_Ref := True;
23903
23904 -- For designator, generate references for the parent entries
23905
23906 if Nkind (Endl) = N_Designator then
23907
23908 -- Generate references for the prefix if the END line comes from
23909 -- source (otherwise we do not need these references) We climb the
23910 -- scope stack to find the expected entities.
23911
23912 if Comes_From_Source (Endl) then
23913 Nam := Name (Endl);
23914 Scop := Current_Scope;
23915 while Nkind (Nam) = N_Selected_Component loop
23916 Scop := Scope (Scop);
23917 exit when No (Scop);
23918 Generate_Parent_Ref (Selector_Name (Nam), Scop);
23919 Nam := Prefix (Nam);
23920 end loop;
23921
23922 if Present (Scop) then
23923 Generate_Parent_Ref (Nam, Scope (Scop));
23924 end if;
23925 end if;
23926
23927 Endl := Identifier (Endl);
23928 end if;
23929 end if;
23930
23931 -- If the end label is not for the given entity, then either we have
23932 -- some previous error, or this is a generic instantiation for which
23933 -- we do not need to make a cross-reference in this case anyway. In
23934 -- either case we simply ignore the call.
23935
23936 if Chars (Ent) /= Chars (Endl) then
23937 return;
23938 end if;
23939
23940 -- If label was really there, then generate a normal reference and then
23941 -- adjust the location in the end label to point past the name (which
23942 -- should almost always be the semicolon).
23943
23944 Loc := Sloc (Endl);
23945
23946 if Comes_From_Source (Endl) then
23947
23948 -- If a label reference is required, then do the style check and
23949 -- generate an l-type cross-reference entry for the label
23950
23951 if Label_Ref then
23952 if Style_Check then
23953 Style.Check_Identifier (Endl, Ent);
23954 end if;
23955
23956 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
23957 end if;
23958
23959 -- Set the location to point past the label (normally this will
23960 -- mean the semicolon immediately following the label). This is
23961 -- done for the sake of the 'e' or 't' entry generated below.
23962
23963 Get_Decoded_Name_String (Chars (Endl));
23964 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
23965
23966 else
23967 -- In SPARK mode, no missing label is allowed for packages and
23968 -- subprogram bodies. Detect those cases by testing whether
23969 -- Process_End_Label was called for a body (Typ = 't') or a package.
23970
23971 if Restriction_Check_Required (SPARK_05)
23972 and then (Typ = 't' or else Ekind (Ent) = E_Package)
23973 then
23974 Error_Msg_Node_1 := Endl;
23975 Check_SPARK_05_Restriction
23976 ("`END &` required", Endl, Force => True);
23977 end if;
23978 end if;
23979
23980 -- Now generate the e/t reference
23981
23982 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
23983
23984 -- Restore Sloc, in case modified above, since we have an identifier
23985 -- and the normal Sloc should be left set in the tree.
23986
23987 Set_Sloc (Endl, Loc);
23988 end Process_End_Label;
23989
23990 --------------------------------
23991 -- Propagate_Concurrent_Flags --
23992 --------------------------------
23993
23994 procedure Propagate_Concurrent_Flags
23995 (Typ : Entity_Id;
23996 Comp_Typ : Entity_Id)
23997 is
23998 begin
23999 if Has_Task (Comp_Typ) then
24000 Set_Has_Task (Typ);
24001 end if;
24002
24003 if Has_Protected (Comp_Typ) then
24004 Set_Has_Protected (Typ);
24005 end if;
24006
24007 if Has_Timing_Event (Comp_Typ) then
24008 Set_Has_Timing_Event (Typ);
24009 end if;
24010 end Propagate_Concurrent_Flags;
24011
24012 ------------------------------
24013 -- Propagate_DIC_Attributes --
24014 ------------------------------
24015
24016 procedure Propagate_DIC_Attributes
24017 (Typ : Entity_Id;
24018 From_Typ : Entity_Id)
24019 is
24020 DIC_Proc : Entity_Id;
24021
24022 begin
24023 if Present (Typ) and then Present (From_Typ) then
24024 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
24025
24026 -- Nothing to do if both the source and the destination denote the
24027 -- same type.
24028
24029 if From_Typ = Typ then
24030 return;
24031
24032 -- Nothing to do when the destination denotes an incomplete type
24033 -- because the DIC is associated with the current instance of a
24034 -- private type, thus it can never apply to an incomplete type.
24035
24036 elsif Is_Incomplete_Type (Typ) then
24037 return;
24038 end if;
24039
24040 DIC_Proc := DIC_Procedure (From_Typ);
24041
24042 -- The setting of the attributes is intentionally conservative. This
24043 -- prevents accidental clobbering of enabled attributes.
24044
24045 if Has_Inherited_DIC (From_Typ)
24046 and then not Has_Inherited_DIC (Typ)
24047 then
24048 Set_Has_Inherited_DIC (Typ);
24049 end if;
24050
24051 if Has_Own_DIC (From_Typ) and then not Has_Own_DIC (Typ) then
24052 Set_Has_Own_DIC (Typ);
24053 end if;
24054
24055 if Present (DIC_Proc) and then No (DIC_Procedure (Typ)) then
24056 Set_DIC_Procedure (Typ, DIC_Proc);
24057 end if;
24058 end if;
24059 end Propagate_DIC_Attributes;
24060
24061 ------------------------------------
24062 -- Propagate_Invariant_Attributes --
24063 ------------------------------------
24064
24065 procedure Propagate_Invariant_Attributes
24066 (Typ : Entity_Id;
24067 From_Typ : Entity_Id)
24068 is
24069 Full_IP : Entity_Id;
24070 Part_IP : Entity_Id;
24071
24072 begin
24073 if Present (Typ) and then Present (From_Typ) then
24074 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
24075
24076 -- Nothing to do if both the source and the destination denote the
24077 -- same type.
24078
24079 if From_Typ = Typ then
24080 return;
24081 end if;
24082
24083 Full_IP := Invariant_Procedure (From_Typ);
24084 Part_IP := Partial_Invariant_Procedure (From_Typ);
24085
24086 -- The setting of the attributes is intentionally conservative. This
24087 -- prevents accidental clobbering of enabled attributes.
24088
24089 if Has_Inheritable_Invariants (From_Typ)
24090 and then not Has_Inheritable_Invariants (Typ)
24091 then
24092 Set_Has_Inheritable_Invariants (Typ);
24093 end if;
24094
24095 if Has_Inherited_Invariants (From_Typ)
24096 and then not Has_Inherited_Invariants (Typ)
24097 then
24098 Set_Has_Inherited_Invariants (Typ);
24099 end if;
24100
24101 if Has_Own_Invariants (From_Typ)
24102 and then not Has_Own_Invariants (Typ)
24103 then
24104 Set_Has_Own_Invariants (Typ);
24105 end if;
24106
24107 if Present (Full_IP) and then No (Invariant_Procedure (Typ)) then
24108 Set_Invariant_Procedure (Typ, Full_IP);
24109 end if;
24110
24111 if Present (Part_IP) and then No (Partial_Invariant_Procedure (Typ))
24112 then
24113 Set_Partial_Invariant_Procedure (Typ, Part_IP);
24114 end if;
24115 end if;
24116 end Propagate_Invariant_Attributes;
24117
24118 ---------------------------------------
24119 -- Record_Possible_Part_Of_Reference --
24120 ---------------------------------------
24121
24122 procedure Record_Possible_Part_Of_Reference
24123 (Var_Id : Entity_Id;
24124 Ref : Node_Id)
24125 is
24126 Encap : constant Entity_Id := Encapsulating_State (Var_Id);
24127 Refs : Elist_Id;
24128
24129 begin
24130 -- The variable is a constituent of a single protected/task type. Such
24131 -- a variable acts as a component of the type and must appear within a
24132 -- specific region (SPARK RM 9(3)). Instead of recording the reference,
24133 -- verify its legality now.
24134
24135 if Present (Encap) and then Is_Single_Concurrent_Object (Encap) then
24136 Check_Part_Of_Reference (Var_Id, Ref);
24137
24138 -- The variable is subject to pragma Part_Of and may eventually become a
24139 -- constituent of a single protected/task type. Record the reference to
24140 -- verify its placement when the contract of the variable is analyzed.
24141
24142 elsif Present (Get_Pragma (Var_Id, Pragma_Part_Of)) then
24143 Refs := Part_Of_References (Var_Id);
24144
24145 if No (Refs) then
24146 Refs := New_Elmt_List;
24147 Set_Part_Of_References (Var_Id, Refs);
24148 end if;
24149
24150 Append_Elmt (Ref, Refs);
24151 end if;
24152 end Record_Possible_Part_Of_Reference;
24153
24154 ----------------
24155 -- Referenced --
24156 ----------------
24157
24158 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
24159 Seen : Boolean := False;
24160
24161 function Is_Reference (N : Node_Id) return Traverse_Result;
24162 -- Determine whether node N denotes a reference to Id. If this is the
24163 -- case, set global flag Seen to True and stop the traversal.
24164
24165 ------------------
24166 -- Is_Reference --
24167 ------------------
24168
24169 function Is_Reference (N : Node_Id) return Traverse_Result is
24170 begin
24171 if Is_Entity_Name (N)
24172 and then Present (Entity (N))
24173 and then Entity (N) = Id
24174 then
24175 Seen := True;
24176 return Abandon;
24177 else
24178 return OK;
24179 end if;
24180 end Is_Reference;
24181
24182 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
24183
24184 -- Start of processing for Referenced
24185
24186 begin
24187 Inspect_Expression (Expr);
24188 return Seen;
24189 end Referenced;
24190
24191 ------------------------------------
24192 -- References_Generic_Formal_Type --
24193 ------------------------------------
24194
24195 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
24196
24197 function Process (N : Node_Id) return Traverse_Result;
24198 -- Process one node in search for generic formal type
24199
24200 -------------
24201 -- Process --
24202 -------------
24203
24204 function Process (N : Node_Id) return Traverse_Result is
24205 begin
24206 if Nkind (N) in N_Has_Entity then
24207 declare
24208 E : constant Entity_Id := Entity (N);
24209 begin
24210 if Present (E) then
24211 if Is_Generic_Type (E) then
24212 return Abandon;
24213 elsif Present (Etype (E))
24214 and then Is_Generic_Type (Etype (E))
24215 then
24216 return Abandon;
24217 end if;
24218 end if;
24219 end;
24220 end if;
24221
24222 return Atree.OK;
24223 end Process;
24224
24225 function Traverse is new Traverse_Func (Process);
24226 -- Traverse tree to look for generic type
24227
24228 begin
24229 if Inside_A_Generic then
24230 return Traverse (N) = Abandon;
24231 else
24232 return False;
24233 end if;
24234 end References_Generic_Formal_Type;
24235
24236 -------------------------------
24237 -- Remove_Entity_And_Homonym --
24238 -------------------------------
24239
24240 procedure Remove_Entity_And_Homonym (Id : Entity_Id) is
24241 begin
24242 Remove_Entity (Id);
24243 Remove_Homonym (Id);
24244 end Remove_Entity_And_Homonym;
24245
24246 --------------------
24247 -- Remove_Homonym --
24248 --------------------
24249
24250 procedure Remove_Homonym (Id : Entity_Id) is
24251 Hom : Entity_Id;
24252 Prev : Entity_Id := Empty;
24253
24254 begin
24255 if Id = Current_Entity (Id) then
24256 if Present (Homonym (Id)) then
24257 Set_Current_Entity (Homonym (Id));
24258 else
24259 Set_Name_Entity_Id (Chars (Id), Empty);
24260 end if;
24261
24262 else
24263 Hom := Current_Entity (Id);
24264 while Present (Hom) and then Hom /= Id loop
24265 Prev := Hom;
24266 Hom := Homonym (Hom);
24267 end loop;
24268
24269 -- If Id is not on the homonym chain, nothing to do
24270
24271 if Present (Hom) then
24272 Set_Homonym (Prev, Homonym (Id));
24273 end if;
24274 end if;
24275 end Remove_Homonym;
24276
24277 ------------------------------
24278 -- Remove_Overloaded_Entity --
24279 ------------------------------
24280
24281 procedure Remove_Overloaded_Entity (Id : Entity_Id) is
24282 procedure Remove_Primitive_Of (Typ : Entity_Id);
24283 -- Remove primitive subprogram Id from the list of primitives that
24284 -- belong to type Typ.
24285
24286 -------------------------
24287 -- Remove_Primitive_Of --
24288 -------------------------
24289
24290 procedure Remove_Primitive_Of (Typ : Entity_Id) is
24291 Prims : Elist_Id;
24292
24293 begin
24294 if Is_Tagged_Type (Typ) then
24295 Prims := Direct_Primitive_Operations (Typ);
24296
24297 if Present (Prims) then
24298 Remove (Prims, Id);
24299 end if;
24300 end if;
24301 end Remove_Primitive_Of;
24302
24303 -- Local variables
24304
24305 Formal : Entity_Id;
24306
24307 -- Start of processing for Remove_Overloaded_Entity
24308
24309 begin
24310 Remove_Entity_And_Homonym (Id);
24311
24312 -- The entity denotes a primitive subprogram. Remove it from the list of
24313 -- primitives of the associated controlling type.
24314
24315 if Ekind_In (Id, E_Function, E_Procedure) and then Is_Primitive (Id) then
24316 Formal := First_Formal (Id);
24317 while Present (Formal) loop
24318 if Is_Controlling_Formal (Formal) then
24319 Remove_Primitive_Of (Etype (Formal));
24320 exit;
24321 end if;
24322
24323 Next_Formal (Formal);
24324 end loop;
24325
24326 if Ekind (Id) = E_Function and then Has_Controlling_Result (Id) then
24327 Remove_Primitive_Of (Etype (Id));
24328 end if;
24329 end if;
24330 end Remove_Overloaded_Entity;
24331
24332 ---------------------
24333 -- Rep_To_Pos_Flag --
24334 ---------------------
24335
24336 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
24337 begin
24338 return New_Occurrence_Of
24339 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
24340 end Rep_To_Pos_Flag;
24341
24342 --------------------
24343 -- Require_Entity --
24344 --------------------
24345
24346 procedure Require_Entity (N : Node_Id) is
24347 begin
24348 if Is_Entity_Name (N) and then No (Entity (N)) then
24349 if Total_Errors_Detected /= 0 then
24350 Set_Entity (N, Any_Id);
24351 else
24352 raise Program_Error;
24353 end if;
24354 end if;
24355 end Require_Entity;
24356
24357 ------------------------------
24358 -- Requires_Transient_Scope --
24359 ------------------------------
24360
24361 -- A transient scope is required when variable-sized temporaries are
24362 -- allocated on the secondary stack, or when finalization actions must be
24363 -- generated before the next instruction.
24364
24365 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
24366 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
24367
24368 procedure Ensure_Minimum_Decoration (Typ : Entity_Id);
24369 -- If Typ is not frozen then add to Typ the minimum decoration required
24370 -- by Requires_Transient_Scope to reliably provide its functionality;
24371 -- otherwise no action is performed.
24372
24373 -------------------------------
24374 -- Ensure_Minimum_Decoration --
24375 -------------------------------
24376
24377 procedure Ensure_Minimum_Decoration (Typ : Entity_Id) is
24378 begin
24379 -- Do not set Has_Controlled_Component on a class-wide equivalent
24380 -- type. See Make_CW_Equivalent_Type.
24381
24382 if Present (Typ)
24383 and then not Is_Frozen (Typ)
24384 and then (Is_Record_Type (Typ)
24385 or else Is_Concurrent_Type (Typ)
24386 or else Is_Incomplete_Or_Private_Type (Typ))
24387 and then not Is_Class_Wide_Equivalent_Type (Typ)
24388 then
24389 declare
24390 Comp : Entity_Id;
24391
24392 begin
24393 Comp := First_Component (Typ);
24394 while Present (Comp) loop
24395 if Has_Controlled_Component (Etype (Comp))
24396 or else
24397 (Chars (Comp) /= Name_uParent
24398 and then Is_Controlled (Etype (Comp)))
24399 or else
24400 (Is_Protected_Type (Etype (Comp))
24401 and then
24402 Present (Corresponding_Record_Type (Etype (Comp)))
24403 and then
24404 Has_Controlled_Component
24405 (Corresponding_Record_Type (Etype (Comp))))
24406 then
24407 Set_Has_Controlled_Component (Typ);
24408 exit;
24409 end if;
24410
24411 Next_Component (Comp);
24412 end loop;
24413 end;
24414 end if;
24415 end Ensure_Minimum_Decoration;
24416
24417 -- Start of processing for Requires_Transient_Scope
24418
24419 begin
24420 if Debug_Flag_QQ then
24421 return Old_Result;
24422 end if;
24423
24424 Ensure_Minimum_Decoration (Id);
24425
24426 declare
24427 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
24428
24429 begin
24430 -- Assert that we're not putting things on the secondary stack if we
24431 -- didn't before; we are trying to AVOID secondary stack when
24432 -- possible.
24433
24434 if not Old_Result then
24435 pragma Assert (not New_Result);
24436 null;
24437 end if;
24438
24439 if New_Result /= Old_Result then
24440 Results_Differ (Id, Old_Result, New_Result);
24441 end if;
24442
24443 return New_Result;
24444 end;
24445 end Requires_Transient_Scope;
24446
24447 --------------------
24448 -- Results_Differ --
24449 --------------------
24450
24451 procedure Results_Differ
24452 (Id : Entity_Id;
24453 Old_Val : Boolean;
24454 New_Val : Boolean)
24455 is
24456 begin
24457 if False then -- False to disable; True for debugging
24458 Treepr.Print_Tree_Node (Id);
24459
24460 if Old_Val = New_Val then
24461 raise Program_Error;
24462 end if;
24463 end if;
24464 end Results_Differ;
24465
24466 --------------------------
24467 -- Reset_Analyzed_Flags --
24468 --------------------------
24469
24470 procedure Reset_Analyzed_Flags (N : Node_Id) is
24471 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
24472 -- Function used to reset Analyzed flags in tree. Note that we do
24473 -- not reset Analyzed flags in entities, since there is no need to
24474 -- reanalyze entities, and indeed, it is wrong to do so, since it
24475 -- can result in generating auxiliary stuff more than once.
24476
24477 --------------------
24478 -- Clear_Analyzed --
24479 --------------------
24480
24481 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
24482 begin
24483 if Nkind (N) not in N_Entity then
24484 Set_Analyzed (N, False);
24485 end if;
24486
24487 return OK;
24488 end Clear_Analyzed;
24489
24490 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
24491
24492 -- Start of processing for Reset_Analyzed_Flags
24493
24494 begin
24495 Reset_Analyzed (N);
24496 end Reset_Analyzed_Flags;
24497
24498 ------------------------
24499 -- Restore_SPARK_Mode --
24500 ------------------------
24501
24502 procedure Restore_SPARK_Mode
24503 (Mode : SPARK_Mode_Type;
24504 Prag : Node_Id)
24505 is
24506 begin
24507 SPARK_Mode := Mode;
24508 SPARK_Mode_Pragma := Prag;
24509 end Restore_SPARK_Mode;
24510
24511 --------------------------------
24512 -- Returns_Unconstrained_Type --
24513 --------------------------------
24514
24515 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
24516 begin
24517 return Ekind (Subp) = E_Function
24518 and then not Is_Scalar_Type (Etype (Subp))
24519 and then not Is_Access_Type (Etype (Subp))
24520 and then not Is_Constrained (Etype (Subp));
24521 end Returns_Unconstrained_Type;
24522
24523 ----------------------------
24524 -- Root_Type_Of_Full_View --
24525 ----------------------------
24526
24527 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
24528 Rtyp : constant Entity_Id := Root_Type (T);
24529
24530 begin
24531 -- The root type of the full view may itself be a private type. Keep
24532 -- looking for the ultimate derivation parent.
24533
24534 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
24535 return Root_Type_Of_Full_View (Full_View (Rtyp));
24536 else
24537 return Rtyp;
24538 end if;
24539 end Root_Type_Of_Full_View;
24540
24541 ---------------------------
24542 -- Safe_To_Capture_Value --
24543 ---------------------------
24544
24545 function Safe_To_Capture_Value
24546 (N : Node_Id;
24547 Ent : Entity_Id;
24548 Cond : Boolean := False) return Boolean
24549 is
24550 begin
24551 -- The only entities for which we track constant values are variables
24552 -- which are not renamings, constants, out parameters, and in out
24553 -- parameters, so check if we have this case.
24554
24555 -- Note: it may seem odd to track constant values for constants, but in
24556 -- fact this routine is used for other purposes than simply capturing
24557 -- the value. In particular, the setting of Known[_Non]_Null.
24558
24559 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
24560 or else
24561 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
24562 then
24563 null;
24564
24565 -- For conditionals, we also allow loop parameters and all formals,
24566 -- including in parameters.
24567
24568 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
24569 null;
24570
24571 -- For all other cases, not just unsafe, but impossible to capture
24572 -- Current_Value, since the above are the only entities which have
24573 -- Current_Value fields.
24574
24575 else
24576 return False;
24577 end if;
24578
24579 -- Skip if volatile or aliased, since funny things might be going on in
24580 -- these cases which we cannot necessarily track. Also skip any variable
24581 -- for which an address clause is given, or whose address is taken. Also
24582 -- never capture value of library level variables (an attempt to do so
24583 -- can occur in the case of package elaboration code).
24584
24585 if Treat_As_Volatile (Ent)
24586 or else Is_Aliased (Ent)
24587 or else Present (Address_Clause (Ent))
24588 or else Address_Taken (Ent)
24589 or else (Is_Library_Level_Entity (Ent)
24590 and then Ekind (Ent) = E_Variable)
24591 then
24592 return False;
24593 end if;
24594
24595 -- OK, all above conditions are met. We also require that the scope of
24596 -- the reference be the same as the scope of the entity, not counting
24597 -- packages and blocks and loops.
24598
24599 declare
24600 E_Scope : constant Entity_Id := Scope (Ent);
24601 R_Scope : Entity_Id;
24602
24603 begin
24604 R_Scope := Current_Scope;
24605 while R_Scope /= Standard_Standard loop
24606 exit when R_Scope = E_Scope;
24607
24608 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
24609 return False;
24610 else
24611 R_Scope := Scope (R_Scope);
24612 end if;
24613 end loop;
24614 end;
24615
24616 -- We also require that the reference does not appear in a context
24617 -- where it is not sure to be executed (i.e. a conditional context
24618 -- or an exception handler). We skip this if Cond is True, since the
24619 -- capturing of values from conditional tests handles this ok.
24620
24621 if Cond then
24622 return True;
24623 end if;
24624
24625 declare
24626 Desc : Node_Id;
24627 P : Node_Id;
24628
24629 begin
24630 Desc := N;
24631
24632 -- Seems dubious that case expressions are not handled here ???
24633
24634 P := Parent (N);
24635 while Present (P) loop
24636 if Nkind (P) = N_If_Statement
24637 or else Nkind (P) = N_Case_Statement
24638 or else (Nkind (P) in N_Short_Circuit
24639 and then Desc = Right_Opnd (P))
24640 or else (Nkind (P) = N_If_Expression
24641 and then Desc /= First (Expressions (P)))
24642 or else Nkind (P) = N_Exception_Handler
24643 or else Nkind (P) = N_Selective_Accept
24644 or else Nkind (P) = N_Conditional_Entry_Call
24645 or else Nkind (P) = N_Timed_Entry_Call
24646 or else Nkind (P) = N_Asynchronous_Select
24647 then
24648 return False;
24649
24650 else
24651 Desc := P;
24652 P := Parent (P);
24653
24654 -- A special Ada 2012 case: the original node may be part
24655 -- of the else_actions of a conditional expression, in which
24656 -- case it might not have been expanded yet, and appears in
24657 -- a non-syntactic list of actions. In that case it is clearly
24658 -- not safe to save a value.
24659
24660 if No (P)
24661 and then Is_List_Member (Desc)
24662 and then No (Parent (List_Containing (Desc)))
24663 then
24664 return False;
24665 end if;
24666 end if;
24667 end loop;
24668 end;
24669
24670 -- OK, looks safe to set value
24671
24672 return True;
24673 end Safe_To_Capture_Value;
24674
24675 ---------------
24676 -- Same_Name --
24677 ---------------
24678
24679 function Same_Name (N1, N2 : Node_Id) return Boolean is
24680 K1 : constant Node_Kind := Nkind (N1);
24681 K2 : constant Node_Kind := Nkind (N2);
24682
24683 begin
24684 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
24685 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
24686 then
24687 return Chars (N1) = Chars (N2);
24688
24689 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
24690 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
24691 then
24692 return Same_Name (Selector_Name (N1), Selector_Name (N2))
24693 and then Same_Name (Prefix (N1), Prefix (N2));
24694
24695 else
24696 return False;
24697 end if;
24698 end Same_Name;
24699
24700 -----------------
24701 -- Same_Object --
24702 -----------------
24703
24704 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
24705 N1 : constant Node_Id := Original_Node (Node1);
24706 N2 : constant Node_Id := Original_Node (Node2);
24707 -- We do the tests on original nodes, since we are most interested
24708 -- in the original source, not any expansion that got in the way.
24709
24710 K1 : constant Node_Kind := Nkind (N1);
24711 K2 : constant Node_Kind := Nkind (N2);
24712
24713 begin
24714 -- First case, both are entities with same entity
24715
24716 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
24717 declare
24718 EN1 : constant Entity_Id := Entity (N1);
24719 EN2 : constant Entity_Id := Entity (N2);
24720 begin
24721 if Present (EN1) and then Present (EN2)
24722 and then (Ekind_In (EN1, E_Variable, E_Constant)
24723 or else Is_Formal (EN1))
24724 and then EN1 = EN2
24725 then
24726 return True;
24727 end if;
24728 end;
24729 end if;
24730
24731 -- Second case, selected component with same selector, same record
24732
24733 if K1 = N_Selected_Component
24734 and then K2 = N_Selected_Component
24735 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
24736 then
24737 return Same_Object (Prefix (N1), Prefix (N2));
24738
24739 -- Third case, indexed component with same subscripts, same array
24740
24741 elsif K1 = N_Indexed_Component
24742 and then K2 = N_Indexed_Component
24743 and then Same_Object (Prefix (N1), Prefix (N2))
24744 then
24745 declare
24746 E1, E2 : Node_Id;
24747 begin
24748 E1 := First (Expressions (N1));
24749 E2 := First (Expressions (N2));
24750 while Present (E1) loop
24751 if not Same_Value (E1, E2) then
24752 return False;
24753 else
24754 Next (E1);
24755 Next (E2);
24756 end if;
24757 end loop;
24758
24759 return True;
24760 end;
24761
24762 -- Fourth case, slice of same array with same bounds
24763
24764 elsif K1 = N_Slice
24765 and then K2 = N_Slice
24766 and then Nkind (Discrete_Range (N1)) = N_Range
24767 and then Nkind (Discrete_Range (N2)) = N_Range
24768 and then Same_Value (Low_Bound (Discrete_Range (N1)),
24769 Low_Bound (Discrete_Range (N2)))
24770 and then Same_Value (High_Bound (Discrete_Range (N1)),
24771 High_Bound (Discrete_Range (N2)))
24772 then
24773 return Same_Name (Prefix (N1), Prefix (N2));
24774
24775 -- All other cases, not clearly the same object
24776
24777 else
24778 return False;
24779 end if;
24780 end Same_Object;
24781
24782 ---------------
24783 -- Same_Type --
24784 ---------------
24785
24786 function Same_Type (T1, T2 : Entity_Id) return Boolean is
24787 begin
24788 if T1 = T2 then
24789 return True;
24790
24791 elsif not Is_Constrained (T1)
24792 and then not Is_Constrained (T2)
24793 and then Base_Type (T1) = Base_Type (T2)
24794 then
24795 return True;
24796
24797 -- For now don't bother with case of identical constraints, to be
24798 -- fiddled with later on perhaps (this is only used for optimization
24799 -- purposes, so it is not critical to do a best possible job)
24800
24801 else
24802 return False;
24803 end if;
24804 end Same_Type;
24805
24806 ----------------
24807 -- Same_Value --
24808 ----------------
24809
24810 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
24811 begin
24812 if Compile_Time_Known_Value (Node1)
24813 and then Compile_Time_Known_Value (Node2)
24814 then
24815 -- Handle properly compile-time expressions that are not
24816 -- scalar.
24817
24818 if Is_String_Type (Etype (Node1)) then
24819 return Expr_Value_S (Node1) = Expr_Value_S (Node2);
24820
24821 else
24822 return Expr_Value (Node1) = Expr_Value (Node2);
24823 end if;
24824
24825 elsif Same_Object (Node1, Node2) then
24826 return True;
24827 else
24828 return False;
24829 end if;
24830 end Same_Value;
24831
24832 --------------------
24833 -- Set_SPARK_Mode --
24834 --------------------
24835
24836 procedure Set_SPARK_Mode (Context : Entity_Id) is
24837 begin
24838 -- Do not consider illegal or partially decorated constructs
24839
24840 if Ekind (Context) = E_Void or else Error_Posted (Context) then
24841 null;
24842
24843 elsif Present (SPARK_Pragma (Context)) then
24844 Install_SPARK_Mode
24845 (Mode => Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Context)),
24846 Prag => SPARK_Pragma (Context));
24847 end if;
24848 end Set_SPARK_Mode;
24849
24850 -------------------------
24851 -- Scalar_Part_Present --
24852 -------------------------
24853
24854 function Scalar_Part_Present (Typ : Entity_Id) return Boolean is
24855 Val_Typ : constant Entity_Id := Validated_View (Typ);
24856 Field : Entity_Id;
24857
24858 begin
24859 if Is_Scalar_Type (Val_Typ) then
24860 return True;
24861
24862 elsif Is_Array_Type (Val_Typ) then
24863 return Scalar_Part_Present (Component_Type (Val_Typ));
24864
24865 elsif Is_Record_Type (Val_Typ) then
24866 Field := First_Component_Or_Discriminant (Val_Typ);
24867 while Present (Field) loop
24868 if Scalar_Part_Present (Etype (Field)) then
24869 return True;
24870 end if;
24871
24872 Next_Component_Or_Discriminant (Field);
24873 end loop;
24874 end if;
24875
24876 return False;
24877 end Scalar_Part_Present;
24878
24879 ------------------------
24880 -- Scope_Is_Transient --
24881 ------------------------
24882
24883 function Scope_Is_Transient return Boolean is
24884 begin
24885 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
24886 end Scope_Is_Transient;
24887
24888 ------------------
24889 -- Scope_Within --
24890 ------------------
24891
24892 function Scope_Within
24893 (Inner : Entity_Id;
24894 Outer : Entity_Id) return Boolean
24895 is
24896 Curr : Entity_Id;
24897
24898 begin
24899 Curr := Inner;
24900 while Present (Curr) and then Curr /= Standard_Standard loop
24901 Curr := Scope (Curr);
24902
24903 if Curr = Outer then
24904 return True;
24905
24906 -- A selective accept body appears within a task type, but the
24907 -- enclosing subprogram is the procedure of the task body.
24908
24909 elsif Ekind (Implementation_Base_Type (Curr)) = E_Task_Type
24910 and then
24911 Outer = Task_Body_Procedure (Implementation_Base_Type (Curr))
24912 then
24913 return True;
24914
24915 -- Ditto for the body of a protected operation
24916
24917 elsif Is_Subprogram (Curr)
24918 and then Outer = Protected_Body_Subprogram (Curr)
24919 then
24920 return True;
24921
24922 -- Outside of its scope, a synchronized type may just be private
24923
24924 elsif Is_Private_Type (Curr)
24925 and then Present (Full_View (Curr))
24926 and then Is_Concurrent_Type (Full_View (Curr))
24927 then
24928 return Scope_Within (Full_View (Curr), Outer);
24929 end if;
24930 end loop;
24931
24932 return False;
24933 end Scope_Within;
24934
24935 --------------------------
24936 -- Scope_Within_Or_Same --
24937 --------------------------
24938
24939 function Scope_Within_Or_Same
24940 (Inner : Entity_Id;
24941 Outer : Entity_Id) return Boolean
24942 is
24943 Curr : Entity_Id := Inner;
24944
24945 begin
24946 -- Similar to the above, but check for scope identity first
24947
24948 while Present (Curr) and then Curr /= Standard_Standard loop
24949 if Curr = Outer then
24950 return True;
24951
24952 elsif Ekind (Implementation_Base_Type (Curr)) = E_Task_Type
24953 and then
24954 Outer = Task_Body_Procedure (Implementation_Base_Type (Curr))
24955 then
24956 return True;
24957
24958 elsif Is_Subprogram (Curr)
24959 and then Outer = Protected_Body_Subprogram (Curr)
24960 then
24961 return True;
24962
24963 elsif Is_Private_Type (Curr)
24964 and then Present (Full_View (Curr))
24965 then
24966 if Full_View (Curr) = Outer then
24967 return True;
24968 else
24969 return Scope_Within (Full_View (Curr), Outer);
24970 end if;
24971 end if;
24972
24973 Curr := Scope (Curr);
24974 end loop;
24975
24976 return False;
24977 end Scope_Within_Or_Same;
24978
24979 --------------------
24980 -- Set_Convention --
24981 --------------------
24982
24983 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
24984 begin
24985 Basic_Set_Convention (E, Val);
24986
24987 if Is_Type (E)
24988 and then Is_Access_Subprogram_Type (Base_Type (E))
24989 and then Has_Foreign_Convention (E)
24990 then
24991 Set_Can_Use_Internal_Rep (E, False);
24992 end if;
24993
24994 -- If E is an object, including a component, and the type of E is an
24995 -- anonymous access type with no convention set, then also set the
24996 -- convention of the anonymous access type. We do not do this for
24997 -- anonymous protected types, since protected types always have the
24998 -- default convention.
24999
25000 if Present (Etype (E))
25001 and then (Is_Object (E)
25002
25003 -- Allow E_Void (happens for pragma Convention appearing
25004 -- in the middle of a record applying to a component)
25005
25006 or else Ekind (E) = E_Void)
25007 then
25008 declare
25009 Typ : constant Entity_Id := Etype (E);
25010
25011 begin
25012 if Ekind_In (Typ, E_Anonymous_Access_Type,
25013 E_Anonymous_Access_Subprogram_Type)
25014 and then not Has_Convention_Pragma (Typ)
25015 then
25016 Basic_Set_Convention (Typ, Val);
25017 Set_Has_Convention_Pragma (Typ);
25018
25019 -- And for the access subprogram type, deal similarly with the
25020 -- designated E_Subprogram_Type, which is always internal.
25021
25022 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
25023 declare
25024 Dtype : constant Entity_Id := Designated_Type (Typ);
25025 begin
25026 if Ekind (Dtype) = E_Subprogram_Type
25027 and then not Has_Convention_Pragma (Dtype)
25028 then
25029 Basic_Set_Convention (Dtype, Val);
25030 Set_Has_Convention_Pragma (Dtype);
25031 end if;
25032 end;
25033 end if;
25034 end if;
25035 end;
25036 end if;
25037 end Set_Convention;
25038
25039 ------------------------
25040 -- Set_Current_Entity --
25041 ------------------------
25042
25043 -- The given entity is to be set as the currently visible definition of its
25044 -- associated name (i.e. the Node_Id associated with its name). All we have
25045 -- to do is to get the name from the identifier, and then set the
25046 -- associated Node_Id to point to the given entity.
25047
25048 procedure Set_Current_Entity (E : Entity_Id) is
25049 begin
25050 Set_Name_Entity_Id (Chars (E), E);
25051 end Set_Current_Entity;
25052
25053 ---------------------------
25054 -- Set_Debug_Info_Needed --
25055 ---------------------------
25056
25057 procedure Set_Debug_Info_Needed (T : Entity_Id) is
25058
25059 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
25060 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
25061 -- Used to set debug info in a related node if not set already
25062
25063 --------------------------------------
25064 -- Set_Debug_Info_Needed_If_Not_Set --
25065 --------------------------------------
25066
25067 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
25068 begin
25069 if Present (E) and then not Needs_Debug_Info (E) then
25070 Set_Debug_Info_Needed (E);
25071
25072 -- For a private type, indicate that the full view also needs
25073 -- debug information.
25074
25075 if Is_Type (E)
25076 and then Is_Private_Type (E)
25077 and then Present (Full_View (E))
25078 then
25079 Set_Debug_Info_Needed (Full_View (E));
25080 end if;
25081 end if;
25082 end Set_Debug_Info_Needed_If_Not_Set;
25083
25084 -- Start of processing for Set_Debug_Info_Needed
25085
25086 begin
25087 -- Nothing to do if there is no available entity
25088
25089 if No (T) then
25090 return;
25091
25092 -- Nothing to do for an entity with suppressed debug information
25093
25094 elsif Debug_Info_Off (T) then
25095 return;
25096
25097 -- Nothing to do for an ignored Ghost entity because the entity will be
25098 -- eliminated from the tree.
25099
25100 elsif Is_Ignored_Ghost_Entity (T) then
25101 return;
25102
25103 -- Nothing to do if entity comes from a predefined file. Library files
25104 -- are compiled without debug information, but inlined bodies of these
25105 -- routines may appear in user code, and debug information on them ends
25106 -- up complicating debugging the user code.
25107
25108 elsif In_Inlined_Body and then In_Predefined_Unit (T) then
25109 Set_Needs_Debug_Info (T, False);
25110 end if;
25111
25112 -- Set flag in entity itself. Note that we will go through the following
25113 -- circuitry even if the flag is already set on T. That's intentional,
25114 -- it makes sure that the flag will be set in subsidiary entities.
25115
25116 Set_Needs_Debug_Info (T);
25117
25118 -- Set flag on subsidiary entities if not set already
25119
25120 if Is_Object (T) then
25121 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
25122
25123 elsif Is_Type (T) then
25124 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
25125
25126 if Is_Record_Type (T) then
25127 declare
25128 Ent : Entity_Id := First_Entity (T);
25129 begin
25130 while Present (Ent) loop
25131 Set_Debug_Info_Needed_If_Not_Set (Ent);
25132 Next_Entity (Ent);
25133 end loop;
25134 end;
25135
25136 -- For a class wide subtype, we also need debug information
25137 -- for the equivalent type.
25138
25139 if Ekind (T) = E_Class_Wide_Subtype then
25140 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
25141 end if;
25142
25143 elsif Is_Array_Type (T) then
25144 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
25145
25146 declare
25147 Indx : Node_Id := First_Index (T);
25148 begin
25149 while Present (Indx) loop
25150 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
25151 Next_Index (Indx);
25152 end loop;
25153 end;
25154
25155 -- For a packed array type, we also need debug information for
25156 -- the type used to represent the packed array. Conversely, we
25157 -- also need it for the former if we need it for the latter.
25158
25159 if Is_Packed (T) then
25160 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
25161 end if;
25162
25163 if Is_Packed_Array_Impl_Type (T) then
25164 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
25165 end if;
25166
25167 elsif Is_Access_Type (T) then
25168 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
25169
25170 elsif Is_Private_Type (T) then
25171 declare
25172 FV : constant Entity_Id := Full_View (T);
25173
25174 begin
25175 Set_Debug_Info_Needed_If_Not_Set (FV);
25176
25177 -- If the full view is itself a derived private type, we need
25178 -- debug information on its underlying type.
25179
25180 if Present (FV)
25181 and then Is_Private_Type (FV)
25182 and then Present (Underlying_Full_View (FV))
25183 then
25184 Set_Needs_Debug_Info (Underlying_Full_View (FV));
25185 end if;
25186 end;
25187
25188 elsif Is_Protected_Type (T) then
25189 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
25190
25191 elsif Is_Scalar_Type (T) then
25192
25193 -- If the subrange bounds are materialized by dedicated constant
25194 -- objects, also include them in the debug info to make sure the
25195 -- debugger can properly use them.
25196
25197 if Present (Scalar_Range (T))
25198 and then Nkind (Scalar_Range (T)) = N_Range
25199 then
25200 declare
25201 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
25202 High_Bnd : constant Node_Id := Type_High_Bound (T);
25203
25204 begin
25205 if Is_Entity_Name (Low_Bnd) then
25206 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
25207 end if;
25208
25209 if Is_Entity_Name (High_Bnd) then
25210 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
25211 end if;
25212 end;
25213 end if;
25214 end if;
25215 end if;
25216 end Set_Debug_Info_Needed;
25217
25218 ----------------------------
25219 -- Set_Entity_With_Checks --
25220 ----------------------------
25221
25222 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
25223 Val_Actual : Entity_Id;
25224 Nod : Node_Id;
25225 Post_Node : Node_Id;
25226
25227 begin
25228 -- Unconditionally set the entity
25229
25230 Set_Entity (N, Val);
25231
25232 -- The node to post on is the selector in the case of an expanded name,
25233 -- and otherwise the node itself.
25234
25235 if Nkind (N) = N_Expanded_Name then
25236 Post_Node := Selector_Name (N);
25237 else
25238 Post_Node := N;
25239 end if;
25240
25241 -- Check for violation of No_Fixed_IO
25242
25243 if Restriction_Check_Required (No_Fixed_IO)
25244 and then
25245 ((RTU_Loaded (Ada_Text_IO)
25246 and then (Is_RTE (Val, RE_Decimal_IO)
25247 or else
25248 Is_RTE (Val, RE_Fixed_IO)))
25249
25250 or else
25251 (RTU_Loaded (Ada_Wide_Text_IO)
25252 and then (Is_RTE (Val, RO_WT_Decimal_IO)
25253 or else
25254 Is_RTE (Val, RO_WT_Fixed_IO)))
25255
25256 or else
25257 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
25258 and then (Is_RTE (Val, RO_WW_Decimal_IO)
25259 or else
25260 Is_RTE (Val, RO_WW_Fixed_IO))))
25261
25262 -- A special extra check, don't complain about a reference from within
25263 -- the Ada.Interrupts package itself!
25264
25265 and then not In_Same_Extended_Unit (N, Val)
25266 then
25267 Check_Restriction (No_Fixed_IO, Post_Node);
25268 end if;
25269
25270 -- Remaining checks are only done on source nodes. Note that we test
25271 -- for violation of No_Fixed_IO even on non-source nodes, because the
25272 -- cases for checking violations of this restriction are instantiations
25273 -- where the reference in the instance has Comes_From_Source False.
25274
25275 if not Comes_From_Source (N) then
25276 return;
25277 end if;
25278
25279 -- Check for violation of No_Abort_Statements, which is triggered by
25280 -- call to Ada.Task_Identification.Abort_Task.
25281
25282 if Restriction_Check_Required (No_Abort_Statements)
25283 and then (Is_RTE (Val, RE_Abort_Task))
25284
25285 -- A special extra check, don't complain about a reference from within
25286 -- the Ada.Task_Identification package itself!
25287
25288 and then not In_Same_Extended_Unit (N, Val)
25289 then
25290 Check_Restriction (No_Abort_Statements, Post_Node);
25291 end if;
25292
25293 if Val = Standard_Long_Long_Integer then
25294 Check_Restriction (No_Long_Long_Integers, Post_Node);
25295 end if;
25296
25297 -- Check for violation of No_Dynamic_Attachment
25298
25299 if Restriction_Check_Required (No_Dynamic_Attachment)
25300 and then RTU_Loaded (Ada_Interrupts)
25301 and then (Is_RTE (Val, RE_Is_Reserved) or else
25302 Is_RTE (Val, RE_Is_Attached) or else
25303 Is_RTE (Val, RE_Current_Handler) or else
25304 Is_RTE (Val, RE_Attach_Handler) or else
25305 Is_RTE (Val, RE_Exchange_Handler) or else
25306 Is_RTE (Val, RE_Detach_Handler) or else
25307 Is_RTE (Val, RE_Reference))
25308
25309 -- A special extra check, don't complain about a reference from within
25310 -- the Ada.Interrupts package itself!
25311
25312 and then not In_Same_Extended_Unit (N, Val)
25313 then
25314 Check_Restriction (No_Dynamic_Attachment, Post_Node);
25315 end if;
25316
25317 -- Check for No_Implementation_Identifiers
25318
25319 if Restriction_Check_Required (No_Implementation_Identifiers) then
25320
25321 -- We have an implementation defined entity if it is marked as
25322 -- implementation defined, or is defined in a package marked as
25323 -- implementation defined. However, library packages themselves
25324 -- are excluded (we don't want to flag Interfaces itself, just
25325 -- the entities within it).
25326
25327 if (Is_Implementation_Defined (Val)
25328 or else
25329 (Present (Scope (Val))
25330 and then Is_Implementation_Defined (Scope (Val))))
25331 and then not (Is_Package_Or_Generic_Package (Val)
25332 and then Is_Library_Level_Entity (Val))
25333 then
25334 Check_Restriction (No_Implementation_Identifiers, Post_Node);
25335 end if;
25336 end if;
25337
25338 -- Do the style check
25339
25340 if Style_Check
25341 and then not Suppress_Style_Checks (Val)
25342 and then not In_Instance
25343 then
25344 if Nkind (N) = N_Identifier then
25345 Nod := N;
25346 elsif Nkind (N) = N_Expanded_Name then
25347 Nod := Selector_Name (N);
25348 else
25349 return;
25350 end if;
25351
25352 -- A special situation arises for derived operations, where we want
25353 -- to do the check against the parent (since the Sloc of the derived
25354 -- operation points to the derived type declaration itself).
25355
25356 Val_Actual := Val;
25357 while not Comes_From_Source (Val_Actual)
25358 and then Nkind (Val_Actual) in N_Entity
25359 and then (Ekind (Val_Actual) = E_Enumeration_Literal
25360 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
25361 and then Present (Alias (Val_Actual))
25362 loop
25363 Val_Actual := Alias (Val_Actual);
25364 end loop;
25365
25366 -- Renaming declarations for generic actuals do not come from source,
25367 -- and have a different name from that of the entity they rename, so
25368 -- there is no style check to perform here.
25369
25370 if Chars (Nod) = Chars (Val_Actual) then
25371 Style.Check_Identifier (Nod, Val_Actual);
25372 end if;
25373 end if;
25374
25375 Set_Entity (N, Val);
25376 end Set_Entity_With_Checks;
25377
25378 ------------------------------
25379 -- Set_Invalid_Scalar_Value --
25380 ------------------------------
25381
25382 procedure Set_Invalid_Scalar_Value
25383 (Scal_Typ : Float_Scalar_Id;
25384 Value : Ureal)
25385 is
25386 Slot : Ureal renames Invalid_Floats (Scal_Typ);
25387
25388 begin
25389 -- Detect an attempt to set a different value for the same scalar type
25390
25391 pragma Assert (Slot = No_Ureal);
25392 Slot := Value;
25393 end Set_Invalid_Scalar_Value;
25394
25395 ------------------------------
25396 -- Set_Invalid_Scalar_Value --
25397 ------------------------------
25398
25399 procedure Set_Invalid_Scalar_Value
25400 (Scal_Typ : Integer_Scalar_Id;
25401 Value : Uint)
25402 is
25403 Slot : Uint renames Invalid_Integers (Scal_Typ);
25404
25405 begin
25406 -- Detect an attempt to set a different value for the same scalar type
25407
25408 pragma Assert (Slot = No_Uint);
25409 Slot := Value;
25410 end Set_Invalid_Scalar_Value;
25411
25412 ------------------------
25413 -- Set_Name_Entity_Id --
25414 ------------------------
25415
25416 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
25417 begin
25418 Set_Name_Table_Int (Id, Int (Val));
25419 end Set_Name_Entity_Id;
25420
25421 ---------------------
25422 -- Set_Next_Actual --
25423 ---------------------
25424
25425 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
25426 begin
25427 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
25428 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
25429 end if;
25430 end Set_Next_Actual;
25431
25432 ----------------------------------
25433 -- Set_Optimize_Alignment_Flags --
25434 ----------------------------------
25435
25436 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
25437 begin
25438 if Optimize_Alignment = 'S' then
25439 Set_Optimize_Alignment_Space (E);
25440 elsif Optimize_Alignment = 'T' then
25441 Set_Optimize_Alignment_Time (E);
25442 end if;
25443 end Set_Optimize_Alignment_Flags;
25444
25445 -----------------------
25446 -- Set_Public_Status --
25447 -----------------------
25448
25449 procedure Set_Public_Status (Id : Entity_Id) is
25450 S : constant Entity_Id := Current_Scope;
25451
25452 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
25453 -- Determines if E is defined within handled statement sequence or
25454 -- an if statement, returns True if so, False otherwise.
25455
25456 ----------------------
25457 -- Within_HSS_Or_If --
25458 ----------------------
25459
25460 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
25461 N : Node_Id;
25462 begin
25463 N := Declaration_Node (E);
25464 loop
25465 N := Parent (N);
25466
25467 if No (N) then
25468 return False;
25469
25470 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
25471 N_If_Statement)
25472 then
25473 return True;
25474 end if;
25475 end loop;
25476 end Within_HSS_Or_If;
25477
25478 -- Start of processing for Set_Public_Status
25479
25480 begin
25481 -- Everything in the scope of Standard is public
25482
25483 if S = Standard_Standard then
25484 Set_Is_Public (Id);
25485
25486 -- Entity is definitely not public if enclosing scope is not public
25487
25488 elsif not Is_Public (S) then
25489 return;
25490
25491 -- An object or function declaration that occurs in a handled sequence
25492 -- of statements or within an if statement is the declaration for a
25493 -- temporary object or local subprogram generated by the expander. It
25494 -- never needs to be made public and furthermore, making it public can
25495 -- cause back end problems.
25496
25497 elsif Nkind_In (Parent (Id), N_Object_Declaration,
25498 N_Function_Specification)
25499 and then Within_HSS_Or_If (Id)
25500 then
25501 return;
25502
25503 -- Entities in public packages or records are public
25504
25505 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
25506 Set_Is_Public (Id);
25507
25508 -- The bounds of an entry family declaration can generate object
25509 -- declarations that are visible to the back-end, e.g. in the
25510 -- the declaration of a composite type that contains tasks.
25511
25512 elsif Is_Concurrent_Type (S)
25513 and then not Has_Completion (S)
25514 and then Nkind (Parent (Id)) = N_Object_Declaration
25515 then
25516 Set_Is_Public (Id);
25517 end if;
25518 end Set_Public_Status;
25519
25520 -----------------------------
25521 -- Set_Referenced_Modified --
25522 -----------------------------
25523
25524 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
25525 Pref : Node_Id;
25526
25527 begin
25528 -- Deal with indexed or selected component where prefix is modified
25529
25530 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
25531 Pref := Prefix (N);
25532
25533 -- If prefix is access type, then it is the designated object that is
25534 -- being modified, which means we have no entity to set the flag on.
25535
25536 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
25537 return;
25538
25539 -- Otherwise chase the prefix
25540
25541 else
25542 Set_Referenced_Modified (Pref, Out_Param);
25543 end if;
25544
25545 -- Otherwise see if we have an entity name (only other case to process)
25546
25547 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
25548 Set_Referenced_As_LHS (Entity (N), not Out_Param);
25549 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
25550 end if;
25551 end Set_Referenced_Modified;
25552
25553 ------------------
25554 -- Set_Rep_Info --
25555 ------------------
25556
25557 procedure Set_Rep_Info (T1 : Entity_Id; T2 : Entity_Id) is
25558 begin
25559 Set_Is_Atomic (T1, Is_Atomic (T2));
25560 Set_Is_Independent (T1, Is_Independent (T2));
25561 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
25562
25563 if Is_Base_Type (T1) then
25564 Set_Is_Volatile (T1, Is_Volatile (T2));
25565 end if;
25566 end Set_Rep_Info;
25567
25568 ----------------------------
25569 -- Set_Scope_Is_Transient --
25570 ----------------------------
25571
25572 procedure Set_Scope_Is_Transient (V : Boolean := True) is
25573 begin
25574 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
25575 end Set_Scope_Is_Transient;
25576
25577 -------------------
25578 -- Set_Size_Info --
25579 -------------------
25580
25581 procedure Set_Size_Info (T1, T2 : Entity_Id) is
25582 begin
25583 -- We copy Esize, but not RM_Size, since in general RM_Size is
25584 -- subtype specific and does not get inherited by all subtypes.
25585
25586 Set_Esize (T1, Esize (T2));
25587 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
25588
25589 if Is_Discrete_Or_Fixed_Point_Type (T1)
25590 and then
25591 Is_Discrete_Or_Fixed_Point_Type (T2)
25592 then
25593 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
25594 end if;
25595
25596 Set_Alignment (T1, Alignment (T2));
25597 end Set_Size_Info;
25598
25599 ------------------------------
25600 -- Should_Ignore_Pragma_Par --
25601 ------------------------------
25602
25603 function Should_Ignore_Pragma_Par (Prag_Name : Name_Id) return Boolean is
25604 pragma Assert (Compiler_State = Parsing);
25605 -- This one can't work during semantic analysis, because we don't have a
25606 -- correct Current_Source_File.
25607
25608 Result : constant Boolean :=
25609 Get_Name_Table_Boolean3 (Prag_Name)
25610 and then not Is_Internal_File_Name
25611 (File_Name (Current_Source_File));
25612 begin
25613 return Result;
25614 end Should_Ignore_Pragma_Par;
25615
25616 ------------------------------
25617 -- Should_Ignore_Pragma_Sem --
25618 ------------------------------
25619
25620 function Should_Ignore_Pragma_Sem (N : Node_Id) return Boolean is
25621 pragma Assert (Compiler_State = Analyzing);
25622 Prag_Name : constant Name_Id := Pragma_Name (N);
25623 Result : constant Boolean :=
25624 Get_Name_Table_Boolean3 (Prag_Name)
25625 and then not In_Internal_Unit (N);
25626
25627 begin
25628 return Result;
25629 end Should_Ignore_Pragma_Sem;
25630
25631 --------------------
25632 -- Static_Boolean --
25633 --------------------
25634
25635 function Static_Boolean (N : Node_Id) return Uint is
25636 begin
25637 Analyze_And_Resolve (N, Standard_Boolean);
25638
25639 if N = Error
25640 or else Error_Posted (N)
25641 or else Etype (N) = Any_Type
25642 then
25643 return No_Uint;
25644 end if;
25645
25646 if Is_OK_Static_Expression (N) then
25647 if not Raises_Constraint_Error (N) then
25648 return Expr_Value (N);
25649 else
25650 return No_Uint;
25651 end if;
25652
25653 elsif Etype (N) = Any_Type then
25654 return No_Uint;
25655
25656 else
25657 Flag_Non_Static_Expr
25658 ("static boolean expression required here", N);
25659 return No_Uint;
25660 end if;
25661 end Static_Boolean;
25662
25663 --------------------
25664 -- Static_Integer --
25665 --------------------
25666
25667 function Static_Integer (N : Node_Id) return Uint is
25668 begin
25669 Analyze_And_Resolve (N, Any_Integer);
25670
25671 if N = Error
25672 or else Error_Posted (N)
25673 or else Etype (N) = Any_Type
25674 then
25675 return No_Uint;
25676 end if;
25677
25678 if Is_OK_Static_Expression (N) then
25679 if not Raises_Constraint_Error (N) then
25680 return Expr_Value (N);
25681 else
25682 return No_Uint;
25683 end if;
25684
25685 elsif Etype (N) = Any_Type then
25686 return No_Uint;
25687
25688 else
25689 Flag_Non_Static_Expr
25690 ("static integer expression required here", N);
25691 return No_Uint;
25692 end if;
25693 end Static_Integer;
25694
25695 --------------------------
25696 -- Statically_Different --
25697 --------------------------
25698
25699 function Statically_Different (E1, E2 : Node_Id) return Boolean is
25700 R1 : constant Node_Id := Get_Referenced_Object (E1);
25701 R2 : constant Node_Id := Get_Referenced_Object (E2);
25702 begin
25703 return Is_Entity_Name (R1)
25704 and then Is_Entity_Name (R2)
25705 and then Entity (R1) /= Entity (R2)
25706 and then not Is_Formal (Entity (R1))
25707 and then not Is_Formal (Entity (R2));
25708 end Statically_Different;
25709
25710 --------------------------------------
25711 -- Subject_To_Loop_Entry_Attributes --
25712 --------------------------------------
25713
25714 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
25715 Stmt : Node_Id;
25716
25717 begin
25718 Stmt := N;
25719
25720 -- The expansion mechanism transform a loop subject to at least one
25721 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
25722 -- the conditional part.
25723
25724 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
25725 and then Nkind (Original_Node (N)) = N_Loop_Statement
25726 then
25727 Stmt := Original_Node (N);
25728 end if;
25729
25730 return
25731 Nkind (Stmt) = N_Loop_Statement
25732 and then Present (Identifier (Stmt))
25733 and then Present (Entity (Identifier (Stmt)))
25734 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
25735 end Subject_To_Loop_Entry_Attributes;
25736
25737 -----------------------------
25738 -- Subprogram_Access_Level --
25739 -----------------------------
25740
25741 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
25742 begin
25743 if Present (Alias (Subp)) then
25744 return Subprogram_Access_Level (Alias (Subp));
25745 else
25746 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
25747 end if;
25748 end Subprogram_Access_Level;
25749
25750 ---------------------
25751 -- Subprogram_Name --
25752 ---------------------
25753
25754 function Subprogram_Name (N : Node_Id) return String is
25755 Buf : Bounded_String;
25756 Ent : Node_Id := N;
25757 Nod : Node_Id;
25758
25759 begin
25760 while Present (Ent) loop
25761 case Nkind (Ent) is
25762 when N_Subprogram_Body =>
25763 Ent := Defining_Unit_Name (Specification (Ent));
25764 exit;
25765
25766 when N_Subprogram_Declaration =>
25767 Nod := Corresponding_Body (Ent);
25768
25769 if Present (Nod) then
25770 Ent := Nod;
25771 else
25772 Ent := Defining_Unit_Name (Specification (Ent));
25773 end if;
25774
25775 exit;
25776
25777 when N_Subprogram_Instantiation
25778 | N_Package_Body
25779 | N_Package_Specification
25780 =>
25781 Ent := Defining_Unit_Name (Ent);
25782 exit;
25783
25784 when N_Protected_Type_Declaration =>
25785 Ent := Corresponding_Body (Ent);
25786 exit;
25787
25788 when N_Protected_Body
25789 | N_Task_Body
25790 =>
25791 Ent := Defining_Identifier (Ent);
25792 exit;
25793
25794 when others =>
25795 null;
25796 end case;
25797
25798 Ent := Parent (Ent);
25799 end loop;
25800
25801 if No (Ent) then
25802 return "unknown subprogram:unknown file:0:0";
25803 end if;
25804
25805 -- If the subprogram is a child unit, use its simple name to start the
25806 -- construction of the fully qualified name.
25807
25808 if Nkind (Ent) = N_Defining_Program_Unit_Name then
25809 Ent := Defining_Identifier (Ent);
25810 end if;
25811
25812 Append_Entity_Name (Buf, Ent);
25813
25814 -- Append homonym number if needed
25815
25816 if Nkind (N) in N_Entity and then Has_Homonym (N) then
25817 declare
25818 H : Entity_Id := Homonym (N);
25819 Nr : Nat := 1;
25820
25821 begin
25822 while Present (H) loop
25823 if Scope (H) = Scope (N) then
25824 Nr := Nr + 1;
25825 end if;
25826
25827 H := Homonym (H);
25828 end loop;
25829
25830 if Nr > 1 then
25831 Append (Buf, '#');
25832 Append (Buf, Nr);
25833 end if;
25834 end;
25835 end if;
25836
25837 -- Append source location of Ent to Buf so that the string will
25838 -- look like "subp:file:line:col".
25839
25840 declare
25841 Loc : constant Source_Ptr := Sloc (Ent);
25842 begin
25843 Append (Buf, ':');
25844 Append (Buf, Reference_Name (Get_Source_File_Index (Loc)));
25845 Append (Buf, ':');
25846 Append (Buf, Nat (Get_Logical_Line_Number (Loc)));
25847 Append (Buf, ':');
25848 Append (Buf, Nat (Get_Column_Number (Loc)));
25849 end;
25850
25851 return +Buf;
25852 end Subprogram_Name;
25853
25854 -------------------------------
25855 -- Support_Atomic_Primitives --
25856 -------------------------------
25857
25858 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
25859 Size : Int;
25860
25861 begin
25862 -- Verify the alignment of Typ is known
25863
25864 if not Known_Alignment (Typ) then
25865 return False;
25866 end if;
25867
25868 if Known_Static_Esize (Typ) then
25869 Size := UI_To_Int (Esize (Typ));
25870
25871 -- If the Esize (Object_Size) is unknown at compile time, look at the
25872 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
25873
25874 elsif Known_Static_RM_Size (Typ) then
25875 Size := UI_To_Int (RM_Size (Typ));
25876
25877 -- Otherwise, the size is considered to be unknown.
25878
25879 else
25880 return False;
25881 end if;
25882
25883 -- Check that the size of the component is 8, 16, 32, or 64 bits and
25884 -- that Typ is properly aligned.
25885
25886 case Size is
25887 when 8 | 16 | 32 | 64 =>
25888 return Size = UI_To_Int (Alignment (Typ)) * 8;
25889
25890 when others =>
25891 return False;
25892 end case;
25893 end Support_Atomic_Primitives;
25894
25895 -----------------
25896 -- Trace_Scope --
25897 -----------------
25898
25899 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
25900 begin
25901 if Debug_Flag_W then
25902 for J in 0 .. Scope_Stack.Last loop
25903 Write_Str (" ");
25904 end loop;
25905
25906 Write_Str (Msg);
25907 Write_Name (Chars (E));
25908 Write_Str (" from ");
25909 Write_Location (Sloc (N));
25910 Write_Eol;
25911 end if;
25912 end Trace_Scope;
25913
25914 -----------------------
25915 -- Transfer_Entities --
25916 -----------------------
25917
25918 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
25919 procedure Set_Public_Status_Of (Id : Entity_Id);
25920 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
25921 -- Set_Public_Status. If successful and Id denotes a record type, set
25922 -- the Is_Public attribute of its fields.
25923
25924 --------------------------
25925 -- Set_Public_Status_Of --
25926 --------------------------
25927
25928 procedure Set_Public_Status_Of (Id : Entity_Id) is
25929 Field : Entity_Id;
25930
25931 begin
25932 if not Is_Public (Id) then
25933 Set_Public_Status (Id);
25934
25935 -- When the input entity is a public record type, ensure that all
25936 -- its internal fields are also exposed to the linker. The fields
25937 -- of a class-wide type are never made public.
25938
25939 if Is_Public (Id)
25940 and then Is_Record_Type (Id)
25941 and then not Is_Class_Wide_Type (Id)
25942 then
25943 Field := First_Entity (Id);
25944 while Present (Field) loop
25945 Set_Is_Public (Field);
25946 Next_Entity (Field);
25947 end loop;
25948 end if;
25949 end if;
25950 end Set_Public_Status_Of;
25951
25952 -- Local variables
25953
25954 Full_Id : Entity_Id;
25955 Id : Entity_Id;
25956
25957 -- Start of processing for Transfer_Entities
25958
25959 begin
25960 Id := First_Entity (From);
25961
25962 if Present (Id) then
25963
25964 -- Merge the entity chain of the source scope with that of the
25965 -- destination scope.
25966
25967 if Present (Last_Entity (To)) then
25968 Link_Entities (Last_Entity (To), Id);
25969 else
25970 Set_First_Entity (To, Id);
25971 end if;
25972
25973 Set_Last_Entity (To, Last_Entity (From));
25974
25975 -- Inspect the entities of the source scope and update their Scope
25976 -- attribute.
25977
25978 while Present (Id) loop
25979 Set_Scope (Id, To);
25980 Set_Public_Status_Of (Id);
25981
25982 -- Handle an internally generated full view for a private type
25983
25984 if Is_Private_Type (Id)
25985 and then Present (Full_View (Id))
25986 and then Is_Itype (Full_View (Id))
25987 then
25988 Full_Id := Full_View (Id);
25989
25990 Set_Scope (Full_Id, To);
25991 Set_Public_Status_Of (Full_Id);
25992 end if;
25993
25994 Next_Entity (Id);
25995 end loop;
25996
25997 Set_First_Entity (From, Empty);
25998 Set_Last_Entity (From, Empty);
25999 end if;
26000 end Transfer_Entities;
26001
26002 ------------------------
26003 -- Traverse_More_Func --
26004 ------------------------
26005
26006 function Traverse_More_Func (Node : Node_Id) return Traverse_Final_Result is
26007
26008 Processing_Itype : Boolean := False;
26009 -- Set to True while traversing the nodes under an Itype, to prevent
26010 -- looping on Itype handling during that traversal.
26011
26012 function Process_More (N : Node_Id) return Traverse_Result;
26013 -- Wrapper over the Process callback to handle parts of the AST that
26014 -- are not normally traversed as syntactic children.
26015
26016 function Traverse_Rec (N : Node_Id) return Traverse_Final_Result;
26017 -- Main recursive traversal implemented as an instantiation of
26018 -- Traverse_Func over a modified Process callback.
26019
26020 ------------------
26021 -- Process_More --
26022 ------------------
26023
26024 function Process_More (N : Node_Id) return Traverse_Result is
26025
26026 procedure Traverse_More (N : Node_Id;
26027 Res : in out Traverse_Result);
26028 procedure Traverse_More (L : List_Id;
26029 Res : in out Traverse_Result);
26030 -- Traverse a node or list and update the traversal result to value
26031 -- Abandon when needed.
26032
26033 -------------------
26034 -- Traverse_More --
26035 -------------------
26036
26037 procedure Traverse_More (N : Node_Id;
26038 Res : in out Traverse_Result)
26039 is
26040 begin
26041 -- Do not process any more nodes if Abandon was reached
26042
26043 if Res = Abandon then
26044 return;
26045 end if;
26046
26047 if Traverse_Rec (N) = Abandon then
26048 Res := Abandon;
26049 end if;
26050 end Traverse_More;
26051
26052 procedure Traverse_More (L : List_Id;
26053 Res : in out Traverse_Result)
26054 is
26055 N : Node_Id := First (L);
26056
26057 begin
26058 -- Do not process any more nodes if Abandon was reached
26059
26060 if Res = Abandon then
26061 return;
26062 end if;
26063
26064 while Present (N) loop
26065 Traverse_More (N, Res);
26066 Next (N);
26067 end loop;
26068 end Traverse_More;
26069
26070 -- Local variables
26071
26072 Node : Node_Id;
26073 Result : Traverse_Result;
26074
26075 -- Start of processing for Process_More
26076
26077 begin
26078 -- Initial callback to Process. Return immediately on Skip/Abandon.
26079 -- Otherwise update the value of Node for further processing of
26080 -- non-syntactic children.
26081
26082 Result := Process (N);
26083
26084 case Result is
26085 when OK => Node := N;
26086 when OK_Orig => Node := Original_Node (N);
26087 when Skip => return Skip;
26088 when Abandon => return Abandon;
26089 end case;
26090
26091 -- Process the relevant semantic children which are a logical part of
26092 -- the AST under this node before returning for the processing of
26093 -- syntactic children.
26094
26095 -- Start with all non-syntactic lists of action nodes
26096
26097 case Nkind (Node) is
26098 when N_Component_Association =>
26099 Traverse_More (Loop_Actions (Node), Result);
26100
26101 when N_Elsif_Part =>
26102 Traverse_More (Condition_Actions (Node), Result);
26103
26104 when N_Short_Circuit =>
26105 Traverse_More (Actions (Node), Result);
26106
26107 when N_Case_Expression_Alternative =>
26108 Traverse_More (Actions (Node), Result);
26109
26110 when N_Iterated_Component_Association =>
26111 Traverse_More (Loop_Actions (Node), Result);
26112
26113 when N_Iteration_Scheme =>
26114 Traverse_More (Condition_Actions (Node), Result);
26115
26116 when N_If_Expression =>
26117 Traverse_More (Then_Actions (Node), Result);
26118 Traverse_More (Else_Actions (Node), Result);
26119
26120 -- Various nodes have a field Actions as a syntactic node,
26121 -- so it will be traversed in the regular syntactic traversal.
26122
26123 when N_Compilation_Unit_Aux
26124 | N_Compound_Statement
26125 | N_Expression_With_Actions
26126 | N_Freeze_Entity
26127 =>
26128 null;
26129
26130 when others =>
26131 null;
26132 end case;
26133
26134 -- If Process_Itypes is True, process unattached nodes which come
26135 -- from Itypes. This only concerns currently ranges of scalar
26136 -- (possibly as index) types. This traversal is protected against
26137 -- looping with Processing_Itype.
26138
26139 if Process_Itypes
26140 and then not Processing_Itype
26141 and then Nkind (Node) in N_Has_Etype
26142 and then Present (Etype (Node))
26143 and then Is_Itype (Etype (Node))
26144 then
26145 declare
26146 Typ : constant Entity_Id := Etype (Node);
26147 begin
26148 Processing_Itype := True;
26149
26150 case Ekind (Typ) is
26151 when Scalar_Kind =>
26152 Traverse_More (Scalar_Range (Typ), Result);
26153
26154 when Array_Kind =>
26155 declare
26156 Index : Node_Id := First_Index (Typ);
26157 Rng : Node_Id;
26158 begin
26159 while Present (Index) loop
26160 if Nkind (Index) in N_Has_Entity then
26161 Rng := Scalar_Range (Entity (Index));
26162 else
26163 Rng := Index;
26164 end if;
26165
26166 Traverse_More (Rng, Result);
26167 Next_Index (Index);
26168 end loop;
26169 end;
26170 when others =>
26171 null;
26172 end case;
26173
26174 Processing_Itype := False;
26175 end;
26176 end if;
26177
26178 return Result;
26179 end Process_More;
26180
26181 -- Define Traverse_Rec as a renaming of the instantiation, as an
26182 -- instantiation cannot complete a previous spec.
26183
26184 function Traverse_Recursive is new Traverse_Func (Process_More);
26185 function Traverse_Rec (N : Node_Id) return Traverse_Final_Result
26186 renames Traverse_Recursive;
26187
26188 -- Start of processing for Traverse_More_Func
26189
26190 begin
26191 return Traverse_Rec (Node);
26192 end Traverse_More_Func;
26193
26194 ------------------------
26195 -- Traverse_More_Proc --
26196 ------------------------
26197
26198 procedure Traverse_More_Proc (Node : Node_Id) is
26199 function Traverse is new Traverse_More_Func (Process, Process_Itypes);
26200 Discard : Traverse_Final_Result;
26201 pragma Warnings (Off, Discard);
26202 begin
26203 Discard := Traverse (Node);
26204 end Traverse_More_Proc;
26205
26206 -----------------------
26207 -- Type_Access_Level --
26208 -----------------------
26209
26210 function Type_Access_Level (Typ : Entity_Id) return Uint is
26211 Btyp : Entity_Id;
26212
26213 begin
26214 Btyp := Base_Type (Typ);
26215
26216 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
26217 -- simply use the level where the type is declared. This is true for
26218 -- stand-alone object declarations, and for anonymous access types
26219 -- associated with components the level is the same as that of the
26220 -- enclosing composite type. However, special treatment is needed for
26221 -- the cases of access parameters, return objects of an anonymous access
26222 -- type, and, in Ada 95, access discriminants of limited types.
26223
26224 if Is_Access_Type (Btyp) then
26225 if Ekind (Btyp) = E_Anonymous_Access_Type then
26226
26227 -- If the type is a nonlocal anonymous access type (such as for
26228 -- an access parameter) we treat it as being declared at the
26229 -- library level to ensure that names such as X.all'access don't
26230 -- fail static accessibility checks.
26231
26232 if not Is_Local_Anonymous_Access (Typ) then
26233 return Scope_Depth (Standard_Standard);
26234
26235 -- If this is a return object, the accessibility level is that of
26236 -- the result subtype of the enclosing function. The test here is
26237 -- little complicated, because we have to account for extended
26238 -- return statements that have been rewritten as blocks, in which
26239 -- case we have to find and the Is_Return_Object attribute of the
26240 -- itype's associated object. It would be nice to find a way to
26241 -- simplify this test, but it doesn't seem worthwhile to add a new
26242 -- flag just for purposes of this test. ???
26243
26244 elsif Ekind (Scope (Btyp)) = E_Return_Statement
26245 or else
26246 (Is_Itype (Btyp)
26247 and then Nkind (Associated_Node_For_Itype (Btyp)) =
26248 N_Object_Declaration
26249 and then Is_Return_Object
26250 (Defining_Identifier
26251 (Associated_Node_For_Itype (Btyp))))
26252 then
26253 declare
26254 Scop : Entity_Id;
26255
26256 begin
26257 Scop := Scope (Scope (Btyp));
26258 while Present (Scop) loop
26259 exit when Ekind (Scop) = E_Function;
26260 Scop := Scope (Scop);
26261 end loop;
26262
26263 -- Treat the return object's type as having the level of the
26264 -- function's result subtype (as per RM05-6.5(5.3/2)).
26265
26266 return Type_Access_Level (Etype (Scop));
26267 end;
26268 end if;
26269 end if;
26270
26271 Btyp := Root_Type (Btyp);
26272
26273 -- The accessibility level of anonymous access types associated with
26274 -- discriminants is that of the current instance of the type, and
26275 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
26276
26277 -- AI-402: access discriminants have accessibility based on the
26278 -- object rather than the type in Ada 2005, so the above paragraph
26279 -- doesn't apply.
26280
26281 -- ??? Needs completion with rules from AI-416
26282
26283 if Ada_Version <= Ada_95
26284 and then Ekind (Typ) = E_Anonymous_Access_Type
26285 and then Present (Associated_Node_For_Itype (Typ))
26286 and then Nkind (Associated_Node_For_Itype (Typ)) =
26287 N_Discriminant_Specification
26288 then
26289 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
26290 end if;
26291 end if;
26292
26293 -- Return library level for a generic formal type. This is done because
26294 -- RM(10.3.2) says that "The statically deeper relationship does not
26295 -- apply to ... a descendant of a generic formal type". Rather than
26296 -- checking at each point where a static accessibility check is
26297 -- performed to see if we are dealing with a formal type, this rule is
26298 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
26299 -- return extreme values for a formal type; Deepest_Type_Access_Level
26300 -- returns Int'Last. By calling the appropriate function from among the
26301 -- two, we ensure that the static accessibility check will pass if we
26302 -- happen to run into a formal type. More specifically, we should call
26303 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
26304 -- call occurs as part of a static accessibility check and the error
26305 -- case is the case where the type's level is too shallow (as opposed
26306 -- to too deep).
26307
26308 if Is_Generic_Type (Root_Type (Btyp)) then
26309 return Scope_Depth (Standard_Standard);
26310 end if;
26311
26312 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
26313 end Type_Access_Level;
26314
26315 ------------------------------------
26316 -- Type_Without_Stream_Operation --
26317 ------------------------------------
26318
26319 function Type_Without_Stream_Operation
26320 (T : Entity_Id;
26321 Op : TSS_Name_Type := TSS_Null) return Entity_Id
26322 is
26323 BT : constant Entity_Id := Base_Type (T);
26324 Op_Missing : Boolean;
26325
26326 begin
26327 if not Restriction_Active (No_Default_Stream_Attributes) then
26328 return Empty;
26329 end if;
26330
26331 if Is_Elementary_Type (T) then
26332 if Op = TSS_Null then
26333 Op_Missing :=
26334 No (TSS (BT, TSS_Stream_Read))
26335 or else No (TSS (BT, TSS_Stream_Write));
26336
26337 else
26338 Op_Missing := No (TSS (BT, Op));
26339 end if;
26340
26341 if Op_Missing then
26342 return T;
26343 else
26344 return Empty;
26345 end if;
26346
26347 elsif Is_Array_Type (T) then
26348 return Type_Without_Stream_Operation (Component_Type (T), Op);
26349
26350 elsif Is_Record_Type (T) then
26351 declare
26352 Comp : Entity_Id;
26353 C_Typ : Entity_Id;
26354
26355 begin
26356 Comp := First_Component (T);
26357 while Present (Comp) loop
26358 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
26359
26360 if Present (C_Typ) then
26361 return C_Typ;
26362 end if;
26363
26364 Next_Component (Comp);
26365 end loop;
26366
26367 return Empty;
26368 end;
26369
26370 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
26371 return Type_Without_Stream_Operation (Full_View (T), Op);
26372 else
26373 return Empty;
26374 end if;
26375 end Type_Without_Stream_Operation;
26376
26377 ---------------------
26378 -- Ultimate_Prefix --
26379 ---------------------
26380
26381 function Ultimate_Prefix (N : Node_Id) return Node_Id is
26382 Pref : Node_Id;
26383
26384 begin
26385 Pref := N;
26386 while Nkind_In (Pref, N_Explicit_Dereference,
26387 N_Indexed_Component,
26388 N_Selected_Component,
26389 N_Slice)
26390 loop
26391 Pref := Prefix (Pref);
26392 end loop;
26393
26394 return Pref;
26395 end Ultimate_Prefix;
26396
26397 ----------------------------
26398 -- Unique_Defining_Entity --
26399 ----------------------------
26400
26401 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
26402 begin
26403 return Unique_Entity (Defining_Entity (N));
26404 end Unique_Defining_Entity;
26405
26406 -------------------
26407 -- Unique_Entity --
26408 -------------------
26409
26410 function Unique_Entity (E : Entity_Id) return Entity_Id is
26411 U : Entity_Id := E;
26412 P : Node_Id;
26413
26414 begin
26415 case Ekind (E) is
26416 when E_Constant =>
26417 if Present (Full_View (E)) then
26418 U := Full_View (E);
26419 end if;
26420
26421 when Entry_Kind =>
26422 if Nkind (Parent (E)) = N_Entry_Body then
26423 declare
26424 Prot_Item : Entity_Id;
26425 Prot_Type : Entity_Id;
26426
26427 begin
26428 if Ekind (E) = E_Entry then
26429 Prot_Type := Scope (E);
26430
26431 -- Bodies of entry families are nested within an extra scope
26432 -- that contains an entry index declaration.
26433
26434 else
26435 Prot_Type := Scope (Scope (E));
26436 end if;
26437
26438 -- A protected type may be declared as a private type, in
26439 -- which case we need to get its full view.
26440
26441 if Is_Private_Type (Prot_Type) then
26442 Prot_Type := Full_View (Prot_Type);
26443 end if;
26444
26445 -- Full view may not be present on error, in which case
26446 -- return E by default.
26447
26448 if Present (Prot_Type) then
26449 pragma Assert (Ekind (Prot_Type) = E_Protected_Type);
26450
26451 -- Traverse the entity list of the protected type and
26452 -- locate an entry declaration which matches the entry
26453 -- body.
26454
26455 Prot_Item := First_Entity (Prot_Type);
26456 while Present (Prot_Item) loop
26457 if Ekind (Prot_Item) in Entry_Kind
26458 and then Corresponding_Body (Parent (Prot_Item)) = E
26459 then
26460 U := Prot_Item;
26461 exit;
26462 end if;
26463
26464 Next_Entity (Prot_Item);
26465 end loop;
26466 end if;
26467 end;
26468 end if;
26469
26470 when Formal_Kind =>
26471 if Present (Spec_Entity (E)) then
26472 U := Spec_Entity (E);
26473 end if;
26474
26475 when E_Package_Body =>
26476 P := Parent (E);
26477
26478 if Nkind (P) = N_Defining_Program_Unit_Name then
26479 P := Parent (P);
26480 end if;
26481
26482 if Nkind (P) = N_Package_Body
26483 and then Present (Corresponding_Spec (P))
26484 then
26485 U := Corresponding_Spec (P);
26486
26487 elsif Nkind (P) = N_Package_Body_Stub
26488 and then Present (Corresponding_Spec_Of_Stub (P))
26489 then
26490 U := Corresponding_Spec_Of_Stub (P);
26491 end if;
26492
26493 when E_Protected_Body =>
26494 P := Parent (E);
26495
26496 if Nkind (P) = N_Protected_Body
26497 and then Present (Corresponding_Spec (P))
26498 then
26499 U := Corresponding_Spec (P);
26500
26501 elsif Nkind (P) = N_Protected_Body_Stub
26502 and then Present (Corresponding_Spec_Of_Stub (P))
26503 then
26504 U := Corresponding_Spec_Of_Stub (P);
26505
26506 if Is_Single_Protected_Object (U) then
26507 U := Etype (U);
26508 end if;
26509 end if;
26510
26511 if Is_Private_Type (U) then
26512 U := Full_View (U);
26513 end if;
26514
26515 when E_Subprogram_Body =>
26516 P := Parent (E);
26517
26518 if Nkind (P) = N_Defining_Program_Unit_Name then
26519 P := Parent (P);
26520 end if;
26521
26522 P := Parent (P);
26523
26524 if Nkind (P) = N_Subprogram_Body
26525 and then Present (Corresponding_Spec (P))
26526 then
26527 U := Corresponding_Spec (P);
26528
26529 elsif Nkind (P) = N_Subprogram_Body_Stub
26530 and then Present (Corresponding_Spec_Of_Stub (P))
26531 then
26532 U := Corresponding_Spec_Of_Stub (P);
26533
26534 elsif Nkind (P) = N_Subprogram_Renaming_Declaration then
26535 U := Corresponding_Spec (P);
26536 end if;
26537
26538 when E_Task_Body =>
26539 P := Parent (E);
26540
26541 if Nkind (P) = N_Task_Body
26542 and then Present (Corresponding_Spec (P))
26543 then
26544 U := Corresponding_Spec (P);
26545
26546 elsif Nkind (P) = N_Task_Body_Stub
26547 and then Present (Corresponding_Spec_Of_Stub (P))
26548 then
26549 U := Corresponding_Spec_Of_Stub (P);
26550
26551 if Is_Single_Task_Object (U) then
26552 U := Etype (U);
26553 end if;
26554 end if;
26555
26556 if Is_Private_Type (U) then
26557 U := Full_View (U);
26558 end if;
26559
26560 when Type_Kind =>
26561 if Present (Full_View (E)) then
26562 U := Full_View (E);
26563 end if;
26564
26565 when others =>
26566 null;
26567 end case;
26568
26569 return U;
26570 end Unique_Entity;
26571
26572 -----------------
26573 -- Unique_Name --
26574 -----------------
26575
26576 function Unique_Name (E : Entity_Id) return String is
26577
26578 -- Local subprograms
26579
26580 function Add_Homonym_Suffix (E : Entity_Id) return String;
26581
26582 function This_Name return String;
26583
26584 ------------------------
26585 -- Add_Homonym_Suffix --
26586 ------------------------
26587
26588 function Add_Homonym_Suffix (E : Entity_Id) return String is
26589
26590 -- Names in E_Subprogram_Body or E_Package_Body entities are not
26591 -- reliable, as they may not include the overloading suffix.
26592 -- Instead, when looking for the name of E or one of its enclosing
26593 -- scope, we get the name of the corresponding Unique_Entity.
26594
26595 U : constant Entity_Id := Unique_Entity (E);
26596 Nam : constant String := Get_Name_String (Chars (U));
26597
26598 begin
26599 -- If E has homonyms but is not fully qualified, as done in
26600 -- GNATprove mode, append the homonym number on the fly. Strip the
26601 -- leading space character in the image of natural numbers. Also do
26602 -- not print the homonym value of 1.
26603
26604 if Has_Homonym (U) then
26605 declare
26606 N : constant Pos := Homonym_Number (U);
26607 S : constant String := N'Img;
26608 begin
26609 if N > 1 then
26610 return Nam & "__" & S (2 .. S'Last);
26611 end if;
26612 end;
26613 end if;
26614
26615 return Nam;
26616 end Add_Homonym_Suffix;
26617
26618 ---------------
26619 -- This_Name --
26620 ---------------
26621
26622 function This_Name return String is
26623 begin
26624 return Add_Homonym_Suffix (E);
26625 end This_Name;
26626
26627 -- Local variables
26628
26629 U : constant Entity_Id := Unique_Entity (E);
26630
26631 -- Start of processing for Unique_Name
26632
26633 begin
26634 if E = Standard_Standard
26635 or else Has_Fully_Qualified_Name (E)
26636 then
26637 return This_Name;
26638
26639 elsif Ekind (E) = E_Enumeration_Literal then
26640 return Unique_Name (Etype (E)) & "__" & This_Name;
26641
26642 else
26643 declare
26644 S : constant Entity_Id := Scope (U);
26645 pragma Assert (Present (S));
26646
26647 begin
26648 -- Prefix names of predefined types with standard__, but leave
26649 -- names of user-defined packages and subprograms without prefix
26650 -- (even if technically they are nested in the Standard package).
26651
26652 if S = Standard_Standard then
26653 if Ekind (U) = E_Package or else Is_Subprogram (U) then
26654 return This_Name;
26655 else
26656 return Unique_Name (S) & "__" & This_Name;
26657 end if;
26658
26659 -- For intances of generic subprograms use the name of the related
26660 -- instance and skip the scope of its wrapper package.
26661
26662 elsif Is_Wrapper_Package (S) then
26663 pragma Assert (Scope (S) = Scope (Related_Instance (S)));
26664 -- Wrapper package and the instantiation are in the same scope
26665
26666 declare
26667 Related_Name : constant String :=
26668 Add_Homonym_Suffix (Related_Instance (S));
26669 Enclosing_Name : constant String :=
26670 Unique_Name (Scope (S)) & "__" & Related_Name;
26671
26672 begin
26673 if Is_Subprogram (U)
26674 and then not Is_Generic_Actual_Subprogram (U)
26675 then
26676 return Enclosing_Name;
26677 else
26678 return Enclosing_Name & "__" & This_Name;
26679 end if;
26680 end;
26681
26682 elsif Is_Child_Unit (U) then
26683 return Child_Prefix & Unique_Name (S) & "__" & This_Name;
26684 else
26685 return Unique_Name (S) & "__" & This_Name;
26686 end if;
26687 end;
26688 end if;
26689 end Unique_Name;
26690
26691 ---------------------
26692 -- Unit_Is_Visible --
26693 ---------------------
26694
26695 function Unit_Is_Visible (U : Entity_Id) return Boolean is
26696 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
26697 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
26698
26699 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
26700 -- For a child unit, check whether unit appears in a with_clause
26701 -- of a parent.
26702
26703 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
26704 -- Scan the context clause of one compilation unit looking for a
26705 -- with_clause for the unit in question.
26706
26707 ----------------------------
26708 -- Unit_In_Parent_Context --
26709 ----------------------------
26710
26711 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
26712 begin
26713 if Unit_In_Context (Par_Unit) then
26714 return True;
26715
26716 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
26717 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
26718
26719 else
26720 return False;
26721 end if;
26722 end Unit_In_Parent_Context;
26723
26724 ---------------------
26725 -- Unit_In_Context --
26726 ---------------------
26727
26728 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
26729 Clause : Node_Id;
26730
26731 begin
26732 Clause := First (Context_Items (Comp_Unit));
26733 while Present (Clause) loop
26734 if Nkind (Clause) = N_With_Clause then
26735 if Library_Unit (Clause) = U then
26736 return True;
26737
26738 -- The with_clause may denote a renaming of the unit we are
26739 -- looking for, eg. Text_IO which renames Ada.Text_IO.
26740
26741 elsif
26742 Renamed_Entity (Entity (Name (Clause))) =
26743 Defining_Entity (Unit (U))
26744 then
26745 return True;
26746 end if;
26747 end if;
26748
26749 Next (Clause);
26750 end loop;
26751
26752 return False;
26753 end Unit_In_Context;
26754
26755 -- Start of processing for Unit_Is_Visible
26756
26757 begin
26758 -- The currrent unit is directly visible
26759
26760 if Curr = U then
26761 return True;
26762
26763 elsif Unit_In_Context (Curr) then
26764 return True;
26765
26766 -- If the current unit is a body, check the context of the spec
26767
26768 elsif Nkind (Unit (Curr)) = N_Package_Body
26769 or else
26770 (Nkind (Unit (Curr)) = N_Subprogram_Body
26771 and then not Acts_As_Spec (Unit (Curr)))
26772 then
26773 if Unit_In_Context (Library_Unit (Curr)) then
26774 return True;
26775 end if;
26776 end if;
26777
26778 -- If the spec is a child unit, examine the parents
26779
26780 if Is_Child_Unit (Curr_Entity) then
26781 if Nkind (Unit (Curr)) in N_Unit_Body then
26782 return
26783 Unit_In_Parent_Context
26784 (Parent_Spec (Unit (Library_Unit (Curr))));
26785 else
26786 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
26787 end if;
26788
26789 else
26790 return False;
26791 end if;
26792 end Unit_Is_Visible;
26793
26794 ------------------------------
26795 -- Universal_Interpretation --
26796 ------------------------------
26797
26798 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
26799 Index : Interp_Index;
26800 It : Interp;
26801
26802 begin
26803 -- The argument may be a formal parameter of an operator or subprogram
26804 -- with multiple interpretations, or else an expression for an actual.
26805
26806 if Nkind (Opnd) = N_Defining_Identifier
26807 or else not Is_Overloaded (Opnd)
26808 then
26809 if Etype (Opnd) = Universal_Integer
26810 or else Etype (Opnd) = Universal_Real
26811 then
26812 return Etype (Opnd);
26813 else
26814 return Empty;
26815 end if;
26816
26817 else
26818 Get_First_Interp (Opnd, Index, It);
26819 while Present (It.Typ) loop
26820 if It.Typ = Universal_Integer
26821 or else It.Typ = Universal_Real
26822 then
26823 return It.Typ;
26824 end if;
26825
26826 Get_Next_Interp (Index, It);
26827 end loop;
26828
26829 return Empty;
26830 end if;
26831 end Universal_Interpretation;
26832
26833 ---------------
26834 -- Unqualify --
26835 ---------------
26836
26837 function Unqualify (Expr : Node_Id) return Node_Id is
26838 begin
26839 -- Recurse to handle unlikely case of multiple levels of qualification
26840
26841 if Nkind (Expr) = N_Qualified_Expression then
26842 return Unqualify (Expression (Expr));
26843
26844 -- Normal case, not a qualified expression
26845
26846 else
26847 return Expr;
26848 end if;
26849 end Unqualify;
26850
26851 -----------------
26852 -- Unqual_Conv --
26853 -----------------
26854
26855 function Unqual_Conv (Expr : Node_Id) return Node_Id is
26856 begin
26857 -- Recurse to handle unlikely case of multiple levels of qualification
26858 -- and/or conversion.
26859
26860 if Nkind_In (Expr, N_Qualified_Expression,
26861 N_Type_Conversion,
26862 N_Unchecked_Type_Conversion)
26863 then
26864 return Unqual_Conv (Expression (Expr));
26865
26866 -- Normal case, not a qualified expression
26867
26868 else
26869 return Expr;
26870 end if;
26871 end Unqual_Conv;
26872
26873 --------------------
26874 -- Validated_View --
26875 --------------------
26876
26877 function Validated_View (Typ : Entity_Id) return Entity_Id is
26878 Continue : Boolean;
26879 Val_Typ : Entity_Id;
26880
26881 begin
26882 Continue := True;
26883 Val_Typ := Base_Type (Typ);
26884
26885 -- Obtain the full view of the input type by stripping away concurrency,
26886 -- derivations, and privacy.
26887
26888 while Continue loop
26889 Continue := False;
26890
26891 if Is_Concurrent_Type (Val_Typ) then
26892 if Present (Corresponding_Record_Type (Val_Typ)) then
26893 Continue := True;
26894 Val_Typ := Corresponding_Record_Type (Val_Typ);
26895 end if;
26896
26897 elsif Is_Derived_Type (Val_Typ) then
26898 Continue := True;
26899 Val_Typ := Etype (Val_Typ);
26900
26901 elsif Is_Private_Type (Val_Typ) then
26902 if Present (Underlying_Full_View (Val_Typ)) then
26903 Continue := True;
26904 Val_Typ := Underlying_Full_View (Val_Typ);
26905
26906 elsif Present (Full_View (Val_Typ)) then
26907 Continue := True;
26908 Val_Typ := Full_View (Val_Typ);
26909 end if;
26910 end if;
26911 end loop;
26912
26913 return Val_Typ;
26914 end Validated_View;
26915
26916 -----------------------
26917 -- Visible_Ancestors --
26918 -----------------------
26919
26920 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
26921 List_1 : Elist_Id;
26922 List_2 : Elist_Id;
26923 Elmt : Elmt_Id;
26924
26925 begin
26926 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
26927
26928 -- Collect all the parents and progenitors of Typ. If the full-view of
26929 -- private parents and progenitors is available then it is used to
26930 -- generate the list of visible ancestors; otherwise their partial
26931 -- view is added to the resulting list.
26932
26933 Collect_Parents
26934 (T => Typ,
26935 List => List_1,
26936 Use_Full_View => True);
26937
26938 Collect_Interfaces
26939 (T => Typ,
26940 Ifaces_List => List_2,
26941 Exclude_Parents => True,
26942 Use_Full_View => True);
26943
26944 -- Join the two lists. Avoid duplications because an interface may
26945 -- simultaneously be parent and progenitor of a type.
26946
26947 Elmt := First_Elmt (List_2);
26948 while Present (Elmt) loop
26949 Append_Unique_Elmt (Node (Elmt), List_1);
26950 Next_Elmt (Elmt);
26951 end loop;
26952
26953 return List_1;
26954 end Visible_Ancestors;
26955
26956 ----------------------
26957 -- Within_Init_Proc --
26958 ----------------------
26959
26960 function Within_Init_Proc return Boolean is
26961 S : Entity_Id;
26962
26963 begin
26964 S := Current_Scope;
26965 while not Is_Overloadable (S) loop
26966 if S = Standard_Standard then
26967 return False;
26968 else
26969 S := Scope (S);
26970 end if;
26971 end loop;
26972
26973 return Is_Init_Proc (S);
26974 end Within_Init_Proc;
26975
26976 ---------------------------
26977 -- Within_Protected_Type --
26978 ---------------------------
26979
26980 function Within_Protected_Type (E : Entity_Id) return Boolean is
26981 Scop : Entity_Id := Scope (E);
26982
26983 begin
26984 while Present (Scop) loop
26985 if Ekind (Scop) = E_Protected_Type then
26986 return True;
26987 end if;
26988
26989 Scop := Scope (Scop);
26990 end loop;
26991
26992 return False;
26993 end Within_Protected_Type;
26994
26995 ------------------
26996 -- Within_Scope --
26997 ------------------
26998
26999 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
27000 begin
27001 return Scope_Within_Or_Same (Scope (E), S);
27002 end Within_Scope;
27003
27004 ----------------------------
27005 -- Within_Subprogram_Call --
27006 ----------------------------
27007
27008 function Within_Subprogram_Call (N : Node_Id) return Boolean is
27009 Par : Node_Id;
27010
27011 begin
27012 -- Climb the parent chain looking for a function or procedure call
27013
27014 Par := N;
27015 while Present (Par) loop
27016 if Nkind_In (Par, N_Entry_Call_Statement,
27017 N_Function_Call,
27018 N_Procedure_Call_Statement)
27019 then
27020 return True;
27021
27022 -- Prevent the search from going too far
27023
27024 elsif Is_Body_Or_Package_Declaration (Par) then
27025 exit;
27026 end if;
27027
27028 Par := Parent (Par);
27029 end loop;
27030
27031 return False;
27032 end Within_Subprogram_Call;
27033
27034 ----------------
27035 -- Wrong_Type --
27036 ----------------
27037
27038 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
27039 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
27040 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
27041
27042 Matching_Field : Entity_Id;
27043 -- Entity to give a more precise suggestion on how to write a one-
27044 -- element positional aggregate.
27045
27046 function Has_One_Matching_Field return Boolean;
27047 -- Determines if Expec_Type is a record type with a single component or
27048 -- discriminant whose type matches the found type or is one dimensional
27049 -- array whose component type matches the found type. In the case of
27050 -- one discriminant, we ignore the variant parts. That's not accurate,
27051 -- but good enough for the warning.
27052
27053 ----------------------------
27054 -- Has_One_Matching_Field --
27055 ----------------------------
27056
27057 function Has_One_Matching_Field return Boolean is
27058 E : Entity_Id;
27059
27060 begin
27061 Matching_Field := Empty;
27062
27063 if Is_Array_Type (Expec_Type)
27064 and then Number_Dimensions (Expec_Type) = 1
27065 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
27066 then
27067 -- Use type name if available. This excludes multidimensional
27068 -- arrays and anonymous arrays.
27069
27070 if Comes_From_Source (Expec_Type) then
27071 Matching_Field := Expec_Type;
27072
27073 -- For an assignment, use name of target
27074
27075 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
27076 and then Is_Entity_Name (Name (Parent (Expr)))
27077 then
27078 Matching_Field := Entity (Name (Parent (Expr)));
27079 end if;
27080
27081 return True;
27082
27083 elsif not Is_Record_Type (Expec_Type) then
27084 return False;
27085
27086 else
27087 E := First_Entity (Expec_Type);
27088 loop
27089 if No (E) then
27090 return False;
27091
27092 elsif not Ekind_In (E, E_Discriminant, E_Component)
27093 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
27094 then
27095 Next_Entity (E);
27096
27097 else
27098 exit;
27099 end if;
27100 end loop;
27101
27102 if not Covers (Etype (E), Found_Type) then
27103 return False;
27104
27105 elsif Present (Next_Entity (E))
27106 and then (Ekind (E) = E_Component
27107 or else Ekind (Next_Entity (E)) = E_Discriminant)
27108 then
27109 return False;
27110
27111 else
27112 Matching_Field := E;
27113 return True;
27114 end if;
27115 end if;
27116 end Has_One_Matching_Field;
27117
27118 -- Start of processing for Wrong_Type
27119
27120 begin
27121 -- Don't output message if either type is Any_Type, or if a message
27122 -- has already been posted for this node. We need to do the latter
27123 -- check explicitly (it is ordinarily done in Errout), because we
27124 -- are using ! to force the output of the error messages.
27125
27126 if Expec_Type = Any_Type
27127 or else Found_Type = Any_Type
27128 or else Error_Posted (Expr)
27129 then
27130 return;
27131
27132 -- If one of the types is a Taft-Amendment type and the other it its
27133 -- completion, it must be an illegal use of a TAT in the spec, for
27134 -- which an error was already emitted. Avoid cascaded errors.
27135
27136 elsif Is_Incomplete_Type (Expec_Type)
27137 and then Has_Completion_In_Body (Expec_Type)
27138 and then Full_View (Expec_Type) = Etype (Expr)
27139 then
27140 return;
27141
27142 elsif Is_Incomplete_Type (Etype (Expr))
27143 and then Has_Completion_In_Body (Etype (Expr))
27144 and then Full_View (Etype (Expr)) = Expec_Type
27145 then
27146 return;
27147
27148 -- In an instance, there is an ongoing problem with completion of
27149 -- types derived from private types. Their structure is what Gigi
27150 -- expects, but the Etype is the parent type rather than the
27151 -- derived private type itself. Do not flag error in this case. The
27152 -- private completion is an entity without a parent, like an Itype.
27153 -- Similarly, full and partial views may be incorrect in the instance.
27154 -- There is no simple way to insure that it is consistent ???
27155
27156 -- A similar view discrepancy can happen in an inlined body, for the
27157 -- same reason: inserted body may be outside of the original package
27158 -- and only partial views are visible at the point of insertion.
27159
27160 -- If In_Generic_Actual (Expr) is True then we cannot assume that
27161 -- the successful semantic analysis of the generic guarantees anything
27162 -- useful about type checking of this instance, so we ignore
27163 -- In_Instance in that case. There may be cases where this is not
27164 -- right (the symptom would probably be rejecting something
27165 -- that ought to be accepted) but we don't currently have any
27166 -- concrete examples of this.
27167
27168 elsif (In_Instance and then not In_Generic_Actual (Expr))
27169 or else In_Inlined_Body
27170 then
27171 if Etype (Etype (Expr)) = Etype (Expected_Type)
27172 and then
27173 (Has_Private_Declaration (Expected_Type)
27174 or else Has_Private_Declaration (Etype (Expr)))
27175 and then No (Parent (Expected_Type))
27176 then
27177 return;
27178
27179 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
27180 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
27181 then
27182 return;
27183
27184 elsif Is_Private_Type (Expected_Type)
27185 and then Present (Full_View (Expected_Type))
27186 and then Covers (Full_View (Expected_Type), Etype (Expr))
27187 then
27188 return;
27189
27190 -- Conversely, type of expression may be the private one
27191
27192 elsif Is_Private_Type (Base_Type (Etype (Expr)))
27193 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
27194 then
27195 return;
27196 end if;
27197 end if;
27198
27199 -- An interesting special check. If the expression is parenthesized
27200 -- and its type corresponds to the type of the sole component of the
27201 -- expected record type, or to the component type of the expected one
27202 -- dimensional array type, then assume we have a bad aggregate attempt.
27203
27204 if Nkind (Expr) in N_Subexpr
27205 and then Paren_Count (Expr) /= 0
27206 and then Has_One_Matching_Field
27207 then
27208 Error_Msg_N ("positional aggregate cannot have one component", Expr);
27209
27210 if Present (Matching_Field) then
27211 if Is_Array_Type (Expec_Type) then
27212 Error_Msg_NE
27213 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
27214 else
27215 Error_Msg_NE
27216 ("\write instead `& ='> ...`", Expr, Matching_Field);
27217 end if;
27218 end if;
27219
27220 -- Another special check, if we are looking for a pool-specific access
27221 -- type and we found an E_Access_Attribute_Type, then we have the case
27222 -- of an Access attribute being used in a context which needs a pool-
27223 -- specific type, which is never allowed. The one extra check we make
27224 -- is that the expected designated type covers the Found_Type.
27225
27226 elsif Is_Access_Type (Expec_Type)
27227 and then Ekind (Found_Type) = E_Access_Attribute_Type
27228 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
27229 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
27230 and then Covers
27231 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
27232 then
27233 Error_Msg_N -- CODEFIX
27234 ("result must be general access type!", Expr);
27235 Error_Msg_NE -- CODEFIX
27236 ("add ALL to }!", Expr, Expec_Type);
27237
27238 -- Another special check, if the expected type is an integer type,
27239 -- but the expression is of type System.Address, and the parent is
27240 -- an addition or subtraction operation whose left operand is the
27241 -- expression in question and whose right operand is of an integral
27242 -- type, then this is an attempt at address arithmetic, so give
27243 -- appropriate message.
27244
27245 elsif Is_Integer_Type (Expec_Type)
27246 and then Is_RTE (Found_Type, RE_Address)
27247 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
27248 and then Expr = Left_Opnd (Parent (Expr))
27249 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
27250 then
27251 Error_Msg_N
27252 ("address arithmetic not predefined in package System",
27253 Parent (Expr));
27254 Error_Msg_N
27255 ("\possible missing with/use of System.Storage_Elements",
27256 Parent (Expr));
27257 return;
27258
27259 -- If the expected type is an anonymous access type, as for access
27260 -- parameters and discriminants, the error is on the designated types.
27261
27262 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
27263 if Comes_From_Source (Expec_Type) then
27264 Error_Msg_NE ("expected}!", Expr, Expec_Type);
27265 else
27266 Error_Msg_NE
27267 ("expected an access type with designated}",
27268 Expr, Designated_Type (Expec_Type));
27269 end if;
27270
27271 if Is_Access_Type (Found_Type)
27272 and then not Comes_From_Source (Found_Type)
27273 then
27274 Error_Msg_NE
27275 ("\\found an access type with designated}!",
27276 Expr, Designated_Type (Found_Type));
27277 else
27278 if From_Limited_With (Found_Type) then
27279 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
27280 Error_Msg_Qual_Level := 99;
27281 Error_Msg_NE -- CODEFIX
27282 ("\\missing `WITH &;", Expr, Scope (Found_Type));
27283 Error_Msg_Qual_Level := 0;
27284 else
27285 Error_Msg_NE ("found}!", Expr, Found_Type);
27286 end if;
27287 end if;
27288
27289 -- Normal case of one type found, some other type expected
27290
27291 else
27292 -- If the names of the two types are the same, see if some number
27293 -- of levels of qualification will help. Don't try more than three
27294 -- levels, and if we get to standard, it's no use (and probably
27295 -- represents an error in the compiler) Also do not bother with
27296 -- internal scope names.
27297
27298 declare
27299 Expec_Scope : Entity_Id;
27300 Found_Scope : Entity_Id;
27301
27302 begin
27303 Expec_Scope := Expec_Type;
27304 Found_Scope := Found_Type;
27305
27306 for Levels in Nat range 0 .. 3 loop
27307 if Chars (Expec_Scope) /= Chars (Found_Scope) then
27308 Error_Msg_Qual_Level := Levels;
27309 exit;
27310 end if;
27311
27312 Expec_Scope := Scope (Expec_Scope);
27313 Found_Scope := Scope (Found_Scope);
27314
27315 exit when Expec_Scope = Standard_Standard
27316 or else Found_Scope = Standard_Standard
27317 or else not Comes_From_Source (Expec_Scope)
27318 or else not Comes_From_Source (Found_Scope);
27319 end loop;
27320 end;
27321
27322 if Is_Record_Type (Expec_Type)
27323 and then Present (Corresponding_Remote_Type (Expec_Type))
27324 then
27325 Error_Msg_NE ("expected}!", Expr,
27326 Corresponding_Remote_Type (Expec_Type));
27327 else
27328 Error_Msg_NE ("expected}!", Expr, Expec_Type);
27329 end if;
27330
27331 if Is_Entity_Name (Expr)
27332 and then Is_Package_Or_Generic_Package (Entity (Expr))
27333 then
27334 Error_Msg_N ("\\found package name!", Expr);
27335
27336 elsif Is_Entity_Name (Expr)
27337 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
27338 then
27339 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
27340 Error_Msg_N
27341 ("found procedure name, possibly missing Access attribute!",
27342 Expr);
27343 else
27344 Error_Msg_N
27345 ("\\found procedure name instead of function!", Expr);
27346 end if;
27347
27348 elsif Nkind (Expr) = N_Function_Call
27349 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
27350 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
27351 and then No (Parameter_Associations (Expr))
27352 then
27353 Error_Msg_N
27354 ("found function name, possibly missing Access attribute!",
27355 Expr);
27356
27357 -- Catch common error: a prefix or infix operator which is not
27358 -- directly visible because the type isn't.
27359
27360 elsif Nkind (Expr) in N_Op
27361 and then Is_Overloaded (Expr)
27362 and then not Is_Immediately_Visible (Expec_Type)
27363 and then not Is_Potentially_Use_Visible (Expec_Type)
27364 and then not In_Use (Expec_Type)
27365 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
27366 then
27367 Error_Msg_N
27368 ("operator of the type is not directly visible!", Expr);
27369
27370 elsif Ekind (Found_Type) = E_Void
27371 and then Present (Parent (Found_Type))
27372 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
27373 then
27374 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
27375
27376 else
27377 Error_Msg_NE ("\\found}!", Expr, Found_Type);
27378 end if;
27379
27380 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
27381 -- of the same modular type, and (M1 and M2) = 0 was intended.
27382
27383 if Expec_Type = Standard_Boolean
27384 and then Is_Modular_Integer_Type (Found_Type)
27385 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
27386 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
27387 then
27388 declare
27389 Op : constant Node_Id := Right_Opnd (Parent (Expr));
27390 L : constant Node_Id := Left_Opnd (Op);
27391 R : constant Node_Id := Right_Opnd (Op);
27392
27393 begin
27394 -- The case for the message is when the left operand of the
27395 -- comparison is the same modular type, or when it is an
27396 -- integer literal (or other universal integer expression),
27397 -- which would have been typed as the modular type if the
27398 -- parens had been there.
27399
27400 if (Etype (L) = Found_Type
27401 or else
27402 Etype (L) = Universal_Integer)
27403 and then Is_Integer_Type (Etype (R))
27404 then
27405 Error_Msg_N
27406 ("\\possible missing parens for modular operation", Expr);
27407 end if;
27408 end;
27409 end if;
27410
27411 -- Reset error message qualification indication
27412
27413 Error_Msg_Qual_Level := 0;
27414 end if;
27415 end Wrong_Type;
27416
27417 --------------------------------
27418 -- Yields_Synchronized_Object --
27419 --------------------------------
27420
27421 function Yields_Synchronized_Object (Typ : Entity_Id) return Boolean is
27422 Has_Sync_Comp : Boolean := False;
27423 Id : Entity_Id;
27424
27425 begin
27426 -- An array type yields a synchronized object if its component type
27427 -- yields a synchronized object.
27428
27429 if Is_Array_Type (Typ) then
27430 return Yields_Synchronized_Object (Component_Type (Typ));
27431
27432 -- A descendant of type Ada.Synchronous_Task_Control.Suspension_Object
27433 -- yields a synchronized object by default.
27434
27435 elsif Is_Descendant_Of_Suspension_Object (Typ) then
27436 return True;
27437
27438 -- A protected type yields a synchronized object by default
27439
27440 elsif Is_Protected_Type (Typ) then
27441 return True;
27442
27443 -- A record type or type extension yields a synchronized object when its
27444 -- discriminants (if any) lack default values and all components are of
27445 -- a type that yields a synchronized object.
27446
27447 elsif Is_Record_Type (Typ) then
27448
27449 -- Inspect all entities defined in the scope of the type, looking for
27450 -- components of a type that does not yield a synchronized object or
27451 -- for discriminants with default values.
27452
27453 Id := First_Entity (Typ);
27454 while Present (Id) loop
27455 if Comes_From_Source (Id) then
27456 if Ekind (Id) = E_Component then
27457 if Yields_Synchronized_Object (Etype (Id)) then
27458 Has_Sync_Comp := True;
27459
27460 -- The component does not yield a synchronized object
27461
27462 else
27463 return False;
27464 end if;
27465
27466 elsif Ekind (Id) = E_Discriminant
27467 and then Present (Expression (Parent (Id)))
27468 then
27469 return False;
27470 end if;
27471 end if;
27472
27473 Next_Entity (Id);
27474 end loop;
27475
27476 -- Ensure that the parent type of a type extension yields a
27477 -- synchronized object.
27478
27479 if Etype (Typ) /= Typ
27480 and then not Is_Private_Type (Etype (Typ))
27481 and then not Yields_Synchronized_Object (Etype (Typ))
27482 then
27483 return False;
27484 end if;
27485
27486 -- If we get here, then all discriminants lack default values and all
27487 -- components are of a type that yields a synchronized object.
27488
27489 return Has_Sync_Comp;
27490
27491 -- A synchronized interface type yields a synchronized object by default
27492
27493 elsif Is_Synchronized_Interface (Typ) then
27494 return True;
27495
27496 -- A task type yields a synchronized object by default
27497
27498 elsif Is_Task_Type (Typ) then
27499 return True;
27500
27501 -- A private type yields a synchronized object if its underlying type
27502 -- does.
27503
27504 elsif Is_Private_Type (Typ)
27505 and then Present (Underlying_Type (Typ))
27506 then
27507 return Yields_Synchronized_Object (Underlying_Type (Typ));
27508
27509 -- Otherwise the type does not yield a synchronized object
27510
27511 else
27512 return False;
27513 end if;
27514 end Yields_Synchronized_Object;
27515
27516 ---------------------------
27517 -- Yields_Universal_Type --
27518 ---------------------------
27519
27520 function Yields_Universal_Type (N : Node_Id) return Boolean is
27521 begin
27522 -- Integer and real literals are of a universal type
27523
27524 if Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
27525 return True;
27526
27527 -- The values of certain attributes are of a universal type
27528
27529 elsif Nkind (N) = N_Attribute_Reference then
27530 return
27531 Universal_Type_Attribute (Get_Attribute_Id (Attribute_Name (N)));
27532
27533 -- ??? There are possibly other cases to consider
27534
27535 else
27536 return False;
27537 end if;
27538 end Yields_Universal_Type;
27539
27540 package body Interval_Lists is
27541
27542 function In_Interval
27543 (Value : Uint; Interval : Discrete_Interval) return Boolean;
27544 -- Does the given value lie within the given interval?
27545
27546 -----------------
27547 -- In_Interval --
27548 -----------------
27549 function In_Interval
27550 (Value : Uint; Interval : Discrete_Interval) return Boolean is
27551 begin
27552 return Value >= Interval.Low and then Value <= Interval.High;
27553 end In_Interval;
27554
27555 procedure Check_Consistency (Intervals : Discrete_Interval_List);
27556 -- Check that list is sorted, lacks null intervals, and has gaps
27557 -- between intervals.
27558
27559 ------------------------
27560 -- Check_Consistency --
27561 ------------------------
27562 procedure Check_Consistency (Intervals : Discrete_Interval_List) is
27563 begin
27564 if Serious_Errors_Detected > 0 then
27565 return;
27566 end if;
27567
27568 -- low bound is 1 and high bound equals length
27569 pragma Assert (Intervals'First = 1 and Intervals'Last >= 0);
27570 for Idx in Intervals'Range loop
27571 -- each interval is non-null
27572 pragma Assert (Intervals (Idx).Low <= Intervals (Idx).High);
27573 if Idx /= Intervals'First then
27574 -- intervals are sorted with non-empty gaps between them
27575 pragma Assert
27576 (Intervals (Idx - 1).High < (Intervals (Idx).Low - 1));
27577 null;
27578 end if;
27579 end loop;
27580 end Check_Consistency;
27581
27582 function Chosen_Interval (Choice : Node_Id) return Discrete_Interval;
27583 -- Given an element of a Discrete_Choices list, a
27584 -- Static_Discrete_Predicate list, or an Others_Discrete_Choices
27585 -- list (but not an N_Others_Choice node) return the corresponding
27586 -- interval. If an element that does not represent a single
27587 -- contiguous interval due to a static predicate (or which
27588 -- represents a single contiguous interval whose bounds depend on
27589 -- a static predicate) is encountered, then that is an error on the
27590 -- part of whoever built the list in question.
27591
27592 ---------------------
27593 -- Chosen_Interval --
27594 ---------------------
27595 function Chosen_Interval (Choice : Node_Id) return Discrete_Interval is
27596 begin
27597 case Nkind (Choice) is
27598 when N_Range =>
27599 return (Low => Expr_Value (Low_Bound (Choice)),
27600 High => Expr_Value (High_Bound (Choice)));
27601
27602 when N_Subtype_Indication =>
27603 declare
27604 Range_Exp : constant Node_Id
27605 := Range_Expression (Constraint (Choice));
27606 begin
27607 return (Low => Expr_Value (Low_Bound (Range_Exp)),
27608 High => Expr_Value (High_Bound (Range_Exp)));
27609 end;
27610
27611 when N_Others_Choice =>
27612 raise Program_Error;
27613
27614 when others =>
27615 if Is_Entity_Name (Choice) and then Is_Type (Entity (Choice))
27616 then
27617 return
27618 (Low => Expr_Value (Type_Low_Bound (Entity (Choice))),
27619 High => Expr_Value (Type_High_Bound (Entity (Choice))));
27620 else
27621 -- an expression
27622 return (Low | High => Expr_Value (Choice));
27623 end if;
27624 end case;
27625 end Chosen_Interval;
27626
27627 --------------------
27628 -- Type_Intervals --
27629 --------------------
27630 function Type_Intervals
27631 (Typ : Entity_Id) return Discrete_Interval_List
27632 is
27633 begin
27634 if Has_Static_Predicate (Typ) then
27635 declare
27636 -- No sorting or merging needed
27637 SDP_List : constant List_Id := Static_Discrete_Predicate (Typ);
27638 Range_Or_Expr : Node_Id := First (SDP_List);
27639 Result :
27640 Discrete_Interval_List (1 .. List_Length (SDP_List));
27641 begin
27642 for Idx in Result'Range loop
27643 Result (Idx) := Chosen_Interval (Range_Or_Expr);
27644 Range_Or_Expr := Next (Range_Or_Expr);
27645 end loop;
27646 pragma Assert (not Present (Range_Or_Expr));
27647 Check_Consistency (Result);
27648 return Result;
27649 end;
27650 else
27651 declare
27652 Low : constant Uint := Expr_Value (Type_Low_Bound (Typ));
27653 High : constant Uint := Expr_Value (Type_High_Bound (Typ));
27654 begin
27655 if Low > High then
27656 declare
27657 Null_Array : Discrete_Interval_List (1 .. 0);
27658 begin
27659 return Null_Array;
27660 end;
27661 else
27662 return (1 => (Low => Low, High => High));
27663 end if;
27664 end;
27665 end if;
27666 end Type_Intervals;
27667
27668 procedure Normalize_Interval_List
27669 (List : in out Discrete_Interval_List; Last : out Nat);
27670 -- Perform sorting and merging as required by Check_Consistency.
27671
27672 -----------------------------
27673 -- Normalize_Interval_List --
27674 -----------------------------
27675 procedure Normalize_Interval_List
27676 (List : in out Discrete_Interval_List; Last : out Nat) is
27677
27678 procedure Move_Interval (From, To : Natural);
27679 -- Copy interval from one location to another
27680
27681 function Lt_Interval (Idx1, Idx2 : Natural) return Boolean;
27682 -- Compare two list elements
27683
27684 Temp_0 : Discrete_Interval := (others => Uint_0);
27685 -- cope with Heap_Sort_G idiosyncrasies.
27686
27687 function Read_Interval (From : Natural) return Discrete_Interval;
27688 -- Normal array indexing unless From = 0
27689
27690 -------------------
27691 -- Read_Interval --
27692 -------------------
27693 function Read_Interval (From : Natural) return Discrete_Interval is
27694 begin
27695 if From = 0 then
27696 return Temp_0;
27697 else
27698 return List (Pos (From));
27699 end if;
27700 end Read_Interval;
27701
27702 -------------------
27703 -- Move_Interval --
27704 -------------------
27705 procedure Move_Interval (From, To : Natural) is
27706 Rhs : constant Discrete_Interval := Read_Interval (From);
27707 begin
27708 if To = 0 then
27709 Temp_0 := Rhs;
27710 else
27711 List (Pos (To)) := Rhs;
27712 end if;
27713 end Move_Interval;
27714
27715 -----------------
27716 -- Lt_Interval --
27717 -----------------
27718 function Lt_Interval (Idx1, Idx2 : Natural) return Boolean is
27719 Elem1 : constant Discrete_Interval := Read_Interval (Idx1);
27720 Elem2 : constant Discrete_Interval := Read_Interval (Idx2);
27721 Null_1 : constant Boolean := Elem1.Low > Elem1.High;
27722 Null_2 : constant Boolean := Elem2.Low > Elem2.High;
27723 begin
27724 if Null_1 /= Null_2 then
27725 -- So that sorting moves null intervals to high end
27726 return Null_2;
27727 elsif Elem1.Low /= Elem2.Low then
27728 return Elem1.Low < Elem2.Low;
27729 else
27730 return Elem1.High < Elem2.High;
27731 end if;
27732 end Lt_Interval;
27733
27734 package Interval_Sorting is
27735 new Gnat.Heap_Sort_G (Move_Interval, Lt_Interval);
27736
27737 function Is_Null (Idx : Pos) return Boolean;
27738 -- True iff List (Idx) defines a null range
27739
27740 function Is_Null (Idx : Pos) return Boolean is
27741 begin
27742 return List (Idx).Low > List (Idx).High;
27743 end Is_Null;
27744
27745 procedure Merge_Intervals (Null_Interval_Count : out Nat);
27746 -- Merge contiguous ranges by replacing one with merged range
27747 -- and the other with a null value. Return a count of the
27748 -- null intervals, both preexisting and those introduced by
27749 -- merging.
27750
27751 ---------------------
27752 -- Merge_Intervals --
27753 ---------------------
27754 procedure Merge_Intervals (Null_Interval_Count : out Nat) is
27755 Not_Null : Pos range List'Range;
27756 -- Index of the most recently examined non-null interval
27757
27758 Null_Interval : constant Discrete_Interval
27759 := (Low => Uint_1, High => Uint_0); -- any null range ok here
27760 begin
27761 if List'Length = 0 or else Is_Null (List'First) then
27762 Null_Interval_Count := List'Length;
27763 -- no non-null elements, so no merge candidates
27764 return;
27765 end if;
27766
27767 Null_Interval_Count := 0;
27768 Not_Null := List'First;
27769 for Idx in List'First + 1 .. List'Last loop
27770 if Is_Null (Idx) then
27771 -- all remaining elements are null
27772 Null_Interval_Count :=
27773 Null_Interval_Count + List (Idx .. List'Last)'Length;
27774 return;
27775 elsif List (Idx).Low = List (Not_Null).High + 1 then
27776 -- Merge the two intervals into one; discard the other
27777 List (Not_Null).High := List (Idx).High;
27778 List (Idx) := Null_Interval;
27779 Null_Interval_Count := Null_Interval_Count + 1;
27780 else
27781 pragma Assert (List (Idx).Low > List (Not_Null).High);
27782 Not_Null := Idx;
27783 end if;
27784 end loop;
27785 end Merge_Intervals;
27786 begin
27787 Interval_Sorting.Sort (Natural (List'Last));
27788 declare
27789 Null_Interval_Count : Nat;
27790 begin
27791 Merge_Intervals (Null_Interval_Count);
27792 Last := List'Last - Null_Interval_Count;
27793 if Null_Interval_Count /= 0 then
27794 -- Move null intervals introduced during merging to high end
27795 Interval_Sorting.Sort (Natural (List'Last));
27796 end if;
27797 end;
27798 end Normalize_Interval_List;
27799
27800 ---------------------------
27801 -- Choice_List_Intervals --
27802 ---------------------------
27803 function Choice_List_Intervals
27804 (Discrete_Choices : List_Id) return Discrete_Interval_List
27805 is
27806 function Unmerged_Choice_Count return Nat;
27807 -- The number of intervals before adjacent intervals are merged.
27808
27809 ---------------------------
27810 -- Unmerged_Choice_Count --
27811 ---------------------------
27812 function Unmerged_Choice_Count return Nat is
27813 Choice : Node_Id := First (Discrete_Choices);
27814 Count : Nat := 0;
27815 begin
27816 while Present (Choice) loop
27817 -- Non-contiguous choices involving static predicates
27818 -- have already been normalized away.
27819
27820 if Nkind (Choice) = N_Others_Choice then
27821 Count :=
27822 Count + List_Length (Others_Discrete_Choices (Choice));
27823 else
27824 Count := Count + 1; -- an ordinary expression or range
27825 end if;
27826
27827 Choice := Next (Choice);
27828 end loop;
27829 return Count;
27830 end Unmerged_Choice_Count;
27831
27832 Choice : Node_Id := First (Discrete_Choices);
27833 Result : Discrete_Interval_List (1 .. Unmerged_Choice_Count);
27834 Count : Nat := 0;
27835 begin
27836 while Present (Choice) loop
27837 if Nkind (Choice) = N_Others_Choice then
27838 declare
27839 Others_Choice : Node_Id
27840 := First (Others_Discrete_Choices (Choice));
27841 begin
27842 while Present (Others_Choice) loop
27843 Count := Count + 1;
27844 Result (Count) := Chosen_Interval (Others_Choice);
27845 Others_Choice := Next (Others_Choice);
27846 end loop;
27847 end;
27848 else
27849 Count := Count + 1;
27850 Result (Count) := Chosen_Interval (Choice);
27851 end if;
27852 Choice := Next (Choice);
27853 end loop;
27854 pragma Assert (Count = Result'Last);
27855 Normalize_Interval_List (Result, Count);
27856 Check_Consistency (Result (1 .. Count));
27857 return Result (1 .. Count);
27858 end Choice_List_Intervals;
27859
27860 ---------------
27861 -- Is_Subset --
27862 ---------------
27863 function Is_Subset
27864 (Subset, Of_Set : Discrete_Interval_List) return Boolean
27865 is
27866 -- Returns True iff for each interval of Subset we can find
27867 -- a single interval of Of_Set which contains the Subset interval.
27868 begin
27869 if Of_Set'Length = 0 then
27870 return Subset'Length = 0;
27871 end if;
27872
27873 declare
27874 Set_Index : Pos range Of_Set'Range := Of_Set'First;
27875 begin
27876 for Ss_Idx in Subset'Range loop
27877 while not In_Interval
27878 (Value => Subset (Ss_Idx).Low,
27879 Interval => Of_Set (Set_Index))
27880 loop
27881 if Set_Index = Of_Set'Last then
27882 return False;
27883 end if;
27884 Set_Index := Set_Index + 1;
27885 end loop;
27886
27887 if not In_Interval
27888 (Value => Subset (Ss_Idx).High,
27889 Interval => Of_Set (Set_Index))
27890 then
27891 return False;
27892 end if;
27893 end loop;
27894 end;
27895
27896 return True;
27897 end Is_Subset;
27898
27899 end Interval_Lists;
27900
27901 begin
27902 Erroutc.Subprogram_Name_Ptr := Subprogram_Name'Access;
27903 end Sem_Util;