]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_util.adb
[Ada] Reuse Is_Package_Or_Generic_Package where possible
[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-2019, 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 end Build_Class_Wide_Clone_Decl;
1622
1623 -----------------------------
1624 -- Build_Component_Subtype --
1625 -----------------------------
1626
1627 function Build_Component_Subtype
1628 (C : List_Id;
1629 Loc : Source_Ptr;
1630 T : Entity_Id) return Node_Id
1631 is
1632 Subt : Entity_Id;
1633 Decl : Node_Id;
1634
1635 begin
1636 -- Unchecked_Union components do not require component subtypes
1637
1638 if Is_Unchecked_Union (T) then
1639 return Empty;
1640 end if;
1641
1642 Subt := Make_Temporary (Loc, 'S');
1643 Set_Is_Internal (Subt);
1644
1645 Decl :=
1646 Make_Subtype_Declaration (Loc,
1647 Defining_Identifier => Subt,
1648 Subtype_Indication =>
1649 Make_Subtype_Indication (Loc,
1650 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1651 Constraint =>
1652 Make_Index_Or_Discriminant_Constraint (Loc,
1653 Constraints => C)));
1654
1655 Mark_Rewrite_Insertion (Decl);
1656 return Decl;
1657 end Build_Component_Subtype;
1658
1659 ---------------------------
1660 -- Build_Default_Subtype --
1661 ---------------------------
1662
1663 function Build_Default_Subtype
1664 (T : Entity_Id;
1665 N : Node_Id) return Entity_Id
1666 is
1667 Loc : constant Source_Ptr := Sloc (N);
1668 Disc : Entity_Id;
1669
1670 Bas : Entity_Id;
1671 -- The base type that is to be constrained by the defaults
1672
1673 begin
1674 if not Has_Discriminants (T) or else Is_Constrained (T) then
1675 return T;
1676 end if;
1677
1678 Bas := Base_Type (T);
1679
1680 -- If T is non-private but its base type is private, this is the
1681 -- completion of a subtype declaration whose parent type is private
1682 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1683 -- are to be found in the full view of the base. Check that the private
1684 -- status of T and its base differ.
1685
1686 if Is_Private_Type (Bas)
1687 and then not Is_Private_Type (T)
1688 and then Present (Full_View (Bas))
1689 then
1690 Bas := Full_View (Bas);
1691 end if;
1692
1693 Disc := First_Discriminant (T);
1694
1695 if No (Discriminant_Default_Value (Disc)) then
1696 return T;
1697 end if;
1698
1699 declare
1700 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1701 Constraints : constant List_Id := New_List;
1702 Decl : Node_Id;
1703
1704 begin
1705 while Present (Disc) loop
1706 Append_To (Constraints,
1707 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1708 Next_Discriminant (Disc);
1709 end loop;
1710
1711 Decl :=
1712 Make_Subtype_Declaration (Loc,
1713 Defining_Identifier => Act,
1714 Subtype_Indication =>
1715 Make_Subtype_Indication (Loc,
1716 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1717 Constraint =>
1718 Make_Index_Or_Discriminant_Constraint (Loc,
1719 Constraints => Constraints)));
1720
1721 Insert_Action (N, Decl);
1722
1723 -- If the context is a component declaration the subtype declaration
1724 -- will be analyzed when the enclosing type is frozen, otherwise do
1725 -- it now.
1726
1727 if Ekind (Current_Scope) /= E_Record_Type then
1728 Analyze (Decl);
1729 end if;
1730
1731 return Act;
1732 end;
1733 end Build_Default_Subtype;
1734
1735 --------------------------------------------
1736 -- Build_Discriminal_Subtype_Of_Component --
1737 --------------------------------------------
1738
1739 function Build_Discriminal_Subtype_Of_Component
1740 (T : Entity_Id) return Node_Id
1741 is
1742 Loc : constant Source_Ptr := Sloc (T);
1743 D : Elmt_Id;
1744 Id : Node_Id;
1745
1746 function Build_Discriminal_Array_Constraint return List_Id;
1747 -- If one or more of the bounds of the component depends on
1748 -- discriminants, build actual constraint using the discriminants
1749 -- of the prefix.
1750
1751 function Build_Discriminal_Record_Constraint return List_Id;
1752 -- Similar to previous one, for discriminated components constrained by
1753 -- the discriminant of the enclosing object.
1754
1755 ----------------------------------------
1756 -- Build_Discriminal_Array_Constraint --
1757 ----------------------------------------
1758
1759 function Build_Discriminal_Array_Constraint return List_Id is
1760 Constraints : constant List_Id := New_List;
1761 Indx : Node_Id;
1762 Hi : Node_Id;
1763 Lo : Node_Id;
1764 Old_Hi : Node_Id;
1765 Old_Lo : Node_Id;
1766
1767 begin
1768 Indx := First_Index (T);
1769 while Present (Indx) loop
1770 Old_Lo := Type_Low_Bound (Etype (Indx));
1771 Old_Hi := Type_High_Bound (Etype (Indx));
1772
1773 if Denotes_Discriminant (Old_Lo) then
1774 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1775
1776 else
1777 Lo := New_Copy_Tree (Old_Lo);
1778 end if;
1779
1780 if Denotes_Discriminant (Old_Hi) then
1781 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1782
1783 else
1784 Hi := New_Copy_Tree (Old_Hi);
1785 end if;
1786
1787 Append (Make_Range (Loc, Lo, Hi), Constraints);
1788 Next_Index (Indx);
1789 end loop;
1790
1791 return Constraints;
1792 end Build_Discriminal_Array_Constraint;
1793
1794 -----------------------------------------
1795 -- Build_Discriminal_Record_Constraint --
1796 -----------------------------------------
1797
1798 function Build_Discriminal_Record_Constraint return List_Id is
1799 Constraints : constant List_Id := New_List;
1800 D : Elmt_Id;
1801 D_Val : Node_Id;
1802
1803 begin
1804 D := First_Elmt (Discriminant_Constraint (T));
1805 while Present (D) loop
1806 if Denotes_Discriminant (Node (D)) then
1807 D_Val :=
1808 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1809 else
1810 D_Val := New_Copy_Tree (Node (D));
1811 end if;
1812
1813 Append (D_Val, Constraints);
1814 Next_Elmt (D);
1815 end loop;
1816
1817 return Constraints;
1818 end Build_Discriminal_Record_Constraint;
1819
1820 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1821
1822 begin
1823 if Ekind (T) = E_Array_Subtype then
1824 Id := First_Index (T);
1825 while Present (Id) loop
1826 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1827 or else
1828 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1829 then
1830 return Build_Component_Subtype
1831 (Build_Discriminal_Array_Constraint, Loc, T);
1832 end if;
1833
1834 Next_Index (Id);
1835 end loop;
1836
1837 elsif Ekind (T) = E_Record_Subtype
1838 and then Has_Discriminants (T)
1839 and then not Has_Unknown_Discriminants (T)
1840 then
1841 D := First_Elmt (Discriminant_Constraint (T));
1842 while Present (D) loop
1843 if Denotes_Discriminant (Node (D)) then
1844 return Build_Component_Subtype
1845 (Build_Discriminal_Record_Constraint, Loc, T);
1846 end if;
1847
1848 Next_Elmt (D);
1849 end loop;
1850 end if;
1851
1852 -- If none of the above, the actual and nominal subtypes are the same
1853
1854 return Empty;
1855 end Build_Discriminal_Subtype_Of_Component;
1856
1857 ------------------------------
1858 -- Build_Elaboration_Entity --
1859 ------------------------------
1860
1861 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1862 Loc : constant Source_Ptr := Sloc (N);
1863 Decl : Node_Id;
1864 Elab_Ent : Entity_Id;
1865
1866 procedure Set_Package_Name (Ent : Entity_Id);
1867 -- Given an entity, sets the fully qualified name of the entity in
1868 -- Name_Buffer, with components separated by double underscores. This
1869 -- is a recursive routine that climbs the scope chain to Standard.
1870
1871 ----------------------
1872 -- Set_Package_Name --
1873 ----------------------
1874
1875 procedure Set_Package_Name (Ent : Entity_Id) is
1876 begin
1877 if Scope (Ent) /= Standard_Standard then
1878 Set_Package_Name (Scope (Ent));
1879
1880 declare
1881 Nam : constant String := Get_Name_String (Chars (Ent));
1882 begin
1883 Name_Buffer (Name_Len + 1) := '_';
1884 Name_Buffer (Name_Len + 2) := '_';
1885 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1886 Name_Len := Name_Len + Nam'Length + 2;
1887 end;
1888
1889 else
1890 Get_Name_String (Chars (Ent));
1891 end if;
1892 end Set_Package_Name;
1893
1894 -- Start of processing for Build_Elaboration_Entity
1895
1896 begin
1897 -- Ignore call if already constructed
1898
1899 if Present (Elaboration_Entity (Spec_Id)) then
1900 return;
1901
1902 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1903 -- no role in analysis.
1904
1905 elsif ASIS_Mode then
1906 return;
1907
1908 -- Do not generate an elaboration entity in GNATprove move because the
1909 -- elaboration counter is a form of expansion.
1910
1911 elsif GNATprove_Mode then
1912 return;
1913
1914 -- See if we need elaboration entity
1915
1916 -- We always need an elaboration entity when preserving control flow, as
1917 -- we want to remain explicit about the unit's elaboration order.
1918
1919 elsif Opt.Suppress_Control_Flow_Optimizations then
1920 null;
1921
1922 -- We always need an elaboration entity for the dynamic elaboration
1923 -- model, since it is needed to properly generate the PE exception for
1924 -- access before elaboration.
1925
1926 elsif Dynamic_Elaboration_Checks then
1927 null;
1928
1929 -- For the static model, we don't need the elaboration counter if this
1930 -- unit is sure to have no elaboration code, since that means there
1931 -- is no elaboration unit to be called. Note that we can't just decide
1932 -- after the fact by looking to see whether there was elaboration code,
1933 -- because that's too late to make this decision.
1934
1935 elsif Restriction_Active (No_Elaboration_Code) then
1936 return;
1937
1938 -- Similarly, for the static model, we can skip the elaboration counter
1939 -- if we have the No_Multiple_Elaboration restriction, since for the
1940 -- static model, that's the only purpose of the counter (to avoid
1941 -- multiple elaboration).
1942
1943 elsif Restriction_Active (No_Multiple_Elaboration) then
1944 return;
1945 end if;
1946
1947 -- Here we need the elaboration entity
1948
1949 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1950 -- name with dots replaced by double underscore. We have to manually
1951 -- construct this name, since it will be elaborated in the outer scope,
1952 -- and thus will not have the unit name automatically prepended.
1953
1954 Set_Package_Name (Spec_Id);
1955 Add_Str_To_Name_Buffer ("_E");
1956
1957 -- Create elaboration counter
1958
1959 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1960 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1961
1962 Decl :=
1963 Make_Object_Declaration (Loc,
1964 Defining_Identifier => Elab_Ent,
1965 Object_Definition =>
1966 New_Occurrence_Of (Standard_Short_Integer, Loc),
1967 Expression => Make_Integer_Literal (Loc, Uint_0));
1968
1969 Push_Scope (Standard_Standard);
1970 Add_Global_Declaration (Decl);
1971 Pop_Scope;
1972
1973 -- Reset True_Constant indication, since we will indeed assign a value
1974 -- to the variable in the binder main. We also kill the Current_Value
1975 -- and Last_Assignment fields for the same reason.
1976
1977 Set_Is_True_Constant (Elab_Ent, False);
1978 Set_Current_Value (Elab_Ent, Empty);
1979 Set_Last_Assignment (Elab_Ent, Empty);
1980
1981 -- We do not want any further qualification of the name (if we did not
1982 -- do this, we would pick up the name of the generic package in the case
1983 -- of a library level generic instantiation).
1984
1985 Set_Has_Qualified_Name (Elab_Ent);
1986 Set_Has_Fully_Qualified_Name (Elab_Ent);
1987 end Build_Elaboration_Entity;
1988
1989 --------------------------------
1990 -- Build_Explicit_Dereference --
1991 --------------------------------
1992
1993 procedure Build_Explicit_Dereference
1994 (Expr : Node_Id;
1995 Disc : Entity_Id)
1996 is
1997 Loc : constant Source_Ptr := Sloc (Expr);
1998 I : Interp_Index;
1999 It : Interp;
2000
2001 begin
2002 -- An entity of a type with a reference aspect is overloaded with
2003 -- both interpretations: with and without the dereference. Now that
2004 -- the dereference is made explicit, set the type of the node properly,
2005 -- to prevent anomalies in the backend. Same if the expression is an
2006 -- overloaded function call whose return type has a reference aspect.
2007
2008 if Is_Entity_Name (Expr) then
2009 Set_Etype (Expr, Etype (Entity (Expr)));
2010
2011 -- The designated entity will not be examined again when resolving
2012 -- the dereference, so generate a reference to it now.
2013
2014 Generate_Reference (Entity (Expr), Expr);
2015
2016 elsif Nkind (Expr) = N_Function_Call then
2017
2018 -- If the name of the indexing function is overloaded, locate the one
2019 -- whose return type has an implicit dereference on the desired
2020 -- discriminant, and set entity and type of function call.
2021
2022 if Is_Overloaded (Name (Expr)) then
2023 Get_First_Interp (Name (Expr), I, It);
2024
2025 while Present (It.Nam) loop
2026 if Ekind ((It.Typ)) = E_Record_Type
2027 and then First_Entity ((It.Typ)) = Disc
2028 then
2029 Set_Entity (Name (Expr), It.Nam);
2030 Set_Etype (Name (Expr), Etype (It.Nam));
2031 exit;
2032 end if;
2033
2034 Get_Next_Interp (I, It);
2035 end loop;
2036 end if;
2037
2038 -- Set type of call from resolved function name.
2039
2040 Set_Etype (Expr, Etype (Name (Expr)));
2041 end if;
2042
2043 Set_Is_Overloaded (Expr, False);
2044
2045 -- The expression will often be a generalized indexing that yields a
2046 -- container element that is then dereferenced, in which case the
2047 -- generalized indexing call is also non-overloaded.
2048
2049 if Nkind (Expr) = N_Indexed_Component
2050 and then Present (Generalized_Indexing (Expr))
2051 then
2052 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
2053 end if;
2054
2055 Rewrite (Expr,
2056 Make_Explicit_Dereference (Loc,
2057 Prefix =>
2058 Make_Selected_Component (Loc,
2059 Prefix => Relocate_Node (Expr),
2060 Selector_Name => New_Occurrence_Of (Disc, Loc))));
2061 Set_Etype (Prefix (Expr), Etype (Disc));
2062 Set_Etype (Expr, Designated_Type (Etype (Disc)));
2063 end Build_Explicit_Dereference;
2064
2065 ---------------------------
2066 -- Build_Overriding_Spec --
2067 ---------------------------
2068
2069 function Build_Overriding_Spec
2070 (Op : Entity_Id;
2071 Typ : Entity_Id) return Node_Id
2072 is
2073 Loc : constant Source_Ptr := Sloc (Typ);
2074 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Op);
2075 Spec : constant Node_Id := Specification (Unit_Declaration_Node (Op));
2076
2077 Formal_Spec : Node_Id;
2078 Formal_Type : Node_Id;
2079 New_Spec : Node_Id;
2080
2081 begin
2082 New_Spec := Copy_Subprogram_Spec (Spec);
2083
2084 Formal_Spec := First (Parameter_Specifications (New_Spec));
2085 while Present (Formal_Spec) loop
2086 Formal_Type := Parameter_Type (Formal_Spec);
2087
2088 if Is_Entity_Name (Formal_Type)
2089 and then Entity (Formal_Type) = Par_Typ
2090 then
2091 Rewrite (Formal_Type, New_Occurrence_Of (Typ, Loc));
2092 end if;
2093
2094 -- Nothing needs to be done for access parameters
2095
2096 Next (Formal_Spec);
2097 end loop;
2098
2099 return New_Spec;
2100 end Build_Overriding_Spec;
2101
2102 -----------------------------------
2103 -- Cannot_Raise_Constraint_Error --
2104 -----------------------------------
2105
2106 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
2107 begin
2108 if Compile_Time_Known_Value (Expr) then
2109 return True;
2110
2111 elsif Do_Range_Check (Expr) then
2112 return False;
2113
2114 elsif Raises_Constraint_Error (Expr) then
2115 return False;
2116
2117 else
2118 case Nkind (Expr) is
2119 when N_Identifier =>
2120 return True;
2121
2122 when N_Expanded_Name =>
2123 return True;
2124
2125 when N_Selected_Component =>
2126 return not Do_Discriminant_Check (Expr);
2127
2128 when N_Attribute_Reference =>
2129 if Do_Overflow_Check (Expr) then
2130 return False;
2131
2132 elsif No (Expressions (Expr)) then
2133 return True;
2134
2135 else
2136 declare
2137 N : Node_Id;
2138
2139 begin
2140 N := First (Expressions (Expr));
2141 while Present (N) loop
2142 if Cannot_Raise_Constraint_Error (N) then
2143 Next (N);
2144 else
2145 return False;
2146 end if;
2147 end loop;
2148
2149 return True;
2150 end;
2151 end if;
2152
2153 when N_Type_Conversion =>
2154 if Do_Overflow_Check (Expr)
2155 or else Do_Length_Check (Expr)
2156 or else Do_Tag_Check (Expr)
2157 then
2158 return False;
2159 else
2160 return Cannot_Raise_Constraint_Error (Expression (Expr));
2161 end if;
2162
2163 when N_Unchecked_Type_Conversion =>
2164 return Cannot_Raise_Constraint_Error (Expression (Expr));
2165
2166 when N_Unary_Op =>
2167 if Do_Overflow_Check (Expr) then
2168 return False;
2169 else
2170 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2171 end if;
2172
2173 when N_Op_Divide
2174 | N_Op_Mod
2175 | N_Op_Rem
2176 =>
2177 if Do_Division_Check (Expr)
2178 or else
2179 Do_Overflow_Check (Expr)
2180 then
2181 return False;
2182 else
2183 return
2184 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
2185 and then
2186 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2187 end if;
2188
2189 when N_Op_Add
2190 | N_Op_And
2191 | N_Op_Concat
2192 | N_Op_Eq
2193 | N_Op_Expon
2194 | N_Op_Ge
2195 | N_Op_Gt
2196 | N_Op_Le
2197 | N_Op_Lt
2198 | N_Op_Multiply
2199 | N_Op_Ne
2200 | N_Op_Or
2201 | N_Op_Rotate_Left
2202 | N_Op_Rotate_Right
2203 | N_Op_Shift_Left
2204 | N_Op_Shift_Right
2205 | N_Op_Shift_Right_Arithmetic
2206 | N_Op_Subtract
2207 | N_Op_Xor
2208 =>
2209 if Do_Overflow_Check (Expr) then
2210 return False;
2211 else
2212 return
2213 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
2214 and then
2215 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2216 end if;
2217
2218 when others =>
2219 return False;
2220 end case;
2221 end if;
2222 end Cannot_Raise_Constraint_Error;
2223
2224 -----------------------------------------
2225 -- Check_Dynamically_Tagged_Expression --
2226 -----------------------------------------
2227
2228 procedure Check_Dynamically_Tagged_Expression
2229 (Expr : Node_Id;
2230 Typ : Entity_Id;
2231 Related_Nod : Node_Id)
2232 is
2233 begin
2234 pragma Assert (Is_Tagged_Type (Typ));
2235
2236 -- In order to avoid spurious errors when analyzing the expanded code,
2237 -- this check is done only for nodes that come from source and for
2238 -- actuals of generic instantiations.
2239
2240 if (Comes_From_Source (Related_Nod)
2241 or else In_Generic_Actual (Expr))
2242 and then (Is_Class_Wide_Type (Etype (Expr))
2243 or else Is_Dynamically_Tagged (Expr))
2244 and then not Is_Class_Wide_Type (Typ)
2245 then
2246 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
2247 end if;
2248 end Check_Dynamically_Tagged_Expression;
2249
2250 --------------------------
2251 -- Check_Fully_Declared --
2252 --------------------------
2253
2254 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2255 begin
2256 if Ekind (T) = E_Incomplete_Type then
2257
2258 -- Ada 2005 (AI-50217): If the type is available through a limited
2259 -- with_clause, verify that its full view has been analyzed.
2260
2261 if From_Limited_With (T)
2262 and then Present (Non_Limited_View (T))
2263 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2264 then
2265 -- The non-limited view is fully declared
2266
2267 null;
2268
2269 else
2270 Error_Msg_NE
2271 ("premature usage of incomplete}", N, First_Subtype (T));
2272 end if;
2273
2274 -- Need comments for these tests ???
2275
2276 elsif Has_Private_Component (T)
2277 and then not Is_Generic_Type (Root_Type (T))
2278 and then not In_Spec_Expression
2279 then
2280 -- Special case: if T is the anonymous type created for a single
2281 -- task or protected object, use the name of the source object.
2282
2283 if Is_Concurrent_Type (T)
2284 and then not Comes_From_Source (T)
2285 and then Nkind (N) = N_Object_Declaration
2286 then
2287 Error_Msg_NE
2288 ("type of& has incomplete component",
2289 N, Defining_Identifier (N));
2290 else
2291 Error_Msg_NE
2292 ("premature usage of incomplete}",
2293 N, First_Subtype (T));
2294 end if;
2295 end if;
2296 end Check_Fully_Declared;
2297
2298 -------------------------------------------
2299 -- Check_Function_With_Address_Parameter --
2300 -------------------------------------------
2301
2302 procedure Check_Function_With_Address_Parameter (Subp_Id : Entity_Id) is
2303 F : Entity_Id;
2304 T : Entity_Id;
2305
2306 begin
2307 F := First_Formal (Subp_Id);
2308 while Present (F) loop
2309 T := Etype (F);
2310
2311 if Is_Private_Type (T) and then Present (Full_View (T)) then
2312 T := Full_View (T);
2313 end if;
2314
2315 if Is_Descendant_Of_Address (T) or else Is_Limited_Type (T) then
2316 Set_Is_Pure (Subp_Id, False);
2317 exit;
2318 end if;
2319
2320 Next_Formal (F);
2321 end loop;
2322 end Check_Function_With_Address_Parameter;
2323
2324 -------------------------------------
2325 -- Check_Function_Writable_Actuals --
2326 -------------------------------------
2327
2328 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2329 Writable_Actuals_List : Elist_Id := No_Elist;
2330 Identifiers_List : Elist_Id := No_Elist;
2331 Aggr_Error_Node : Node_Id := Empty;
2332 Error_Node : Node_Id := Empty;
2333
2334 procedure Collect_Identifiers (N : Node_Id);
2335 -- In a single traversal of subtree N collect in Writable_Actuals_List
2336 -- all the actuals of functions with writable actuals, and in the list
2337 -- Identifiers_List collect all the identifiers that are not actuals of
2338 -- functions with writable actuals. If a writable actual is referenced
2339 -- twice as writable actual then Error_Node is set to reference its
2340 -- second occurrence, the error is reported, and the tree traversal
2341 -- is abandoned.
2342
2343 procedure Preanalyze_Without_Errors (N : Node_Id);
2344 -- Preanalyze N without reporting errors. Very dubious, you can't just
2345 -- go analyzing things more than once???
2346
2347 -------------------------
2348 -- Collect_Identifiers --
2349 -------------------------
2350
2351 procedure Collect_Identifiers (N : Node_Id) is
2352
2353 function Check_Node (N : Node_Id) return Traverse_Result;
2354 -- Process a single node during the tree traversal to collect the
2355 -- writable actuals of functions and all the identifiers which are
2356 -- not writable actuals of functions.
2357
2358 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2359 -- Returns True if List has a node whose Entity is Entity (N)
2360
2361 ----------------
2362 -- Check_Node --
2363 ----------------
2364
2365 function Check_Node (N : Node_Id) return Traverse_Result is
2366 Is_Writable_Actual : Boolean := False;
2367 Id : Entity_Id;
2368
2369 begin
2370 if Nkind (N) = N_Identifier then
2371
2372 -- No analysis possible if the entity is not decorated
2373
2374 if No (Entity (N)) then
2375 return Skip;
2376
2377 -- Don't collect identifiers of packages, called functions, etc
2378
2379 elsif Ekind_In (Entity (N), E_Package,
2380 E_Function,
2381 E_Procedure,
2382 E_Entry)
2383 then
2384 return Skip;
2385
2386 -- For rewritten nodes, continue the traversal in the original
2387 -- subtree. Needed to handle aggregates in original expressions
2388 -- extracted from the tree by Remove_Side_Effects.
2389
2390 elsif Is_Rewrite_Substitution (N) then
2391 Collect_Identifiers (Original_Node (N));
2392 return Skip;
2393
2394 -- For now we skip aggregate discriminants, since they require
2395 -- performing the analysis in two phases to identify conflicts:
2396 -- first one analyzing discriminants and second one analyzing
2397 -- the rest of components (since at run time, discriminants are
2398 -- evaluated prior to components): too much computation cost
2399 -- to identify a corner case???
2400
2401 elsif Nkind (Parent (N)) = N_Component_Association
2402 and then Nkind_In (Parent (Parent (N)),
2403 N_Aggregate,
2404 N_Extension_Aggregate)
2405 then
2406 declare
2407 Choice : constant Node_Id := First (Choices (Parent (N)));
2408
2409 begin
2410 if Ekind (Entity (N)) = E_Discriminant then
2411 return Skip;
2412
2413 elsif Expression (Parent (N)) = N
2414 and then Nkind (Choice) = N_Identifier
2415 and then Ekind (Entity (Choice)) = E_Discriminant
2416 then
2417 return Skip;
2418 end if;
2419 end;
2420
2421 -- Analyze if N is a writable actual of a function
2422
2423 elsif Nkind (Parent (N)) = N_Function_Call then
2424 declare
2425 Call : constant Node_Id := Parent (N);
2426 Actual : Node_Id;
2427 Formal : Node_Id;
2428
2429 begin
2430 Id := Get_Called_Entity (Call);
2431
2432 -- In case of previous error, no check is possible
2433
2434 if No (Id) then
2435 return Abandon;
2436 end if;
2437
2438 if Ekind_In (Id, E_Function, E_Generic_Function)
2439 and then Has_Out_Or_In_Out_Parameter (Id)
2440 then
2441 Formal := First_Formal (Id);
2442 Actual := First_Actual (Call);
2443 while Present (Actual) and then Present (Formal) loop
2444 if Actual = N then
2445 if Ekind_In (Formal, E_Out_Parameter,
2446 E_In_Out_Parameter)
2447 then
2448 Is_Writable_Actual := True;
2449 end if;
2450
2451 exit;
2452 end if;
2453
2454 Next_Formal (Formal);
2455 Next_Actual (Actual);
2456 end loop;
2457 end if;
2458 end;
2459 end if;
2460
2461 if Is_Writable_Actual then
2462
2463 -- Skip checking the error in non-elementary types since
2464 -- RM 6.4.1(6.15/3) is restricted to elementary types, but
2465 -- store this actual in Writable_Actuals_List since it is
2466 -- needed to perform checks on other constructs that have
2467 -- arbitrary order of evaluation (for example, aggregates).
2468
2469 if not Is_Elementary_Type (Etype (N)) then
2470 if not Contains (Writable_Actuals_List, N) then
2471 Append_New_Elmt (N, To => Writable_Actuals_List);
2472 end if;
2473
2474 -- Second occurrence of an elementary type writable actual
2475
2476 elsif Contains (Writable_Actuals_List, N) then
2477
2478 -- Report the error on the second occurrence of the
2479 -- identifier. We cannot assume that N is the second
2480 -- occurrence (according to their location in the
2481 -- sources), since Traverse_Func walks through Field2
2482 -- last (see comment in the body of Traverse_Func).
2483
2484 declare
2485 Elmt : Elmt_Id;
2486
2487 begin
2488 Elmt := First_Elmt (Writable_Actuals_List);
2489 while Present (Elmt)
2490 and then Entity (Node (Elmt)) /= Entity (N)
2491 loop
2492 Next_Elmt (Elmt);
2493 end loop;
2494
2495 if Sloc (N) > Sloc (Node (Elmt)) then
2496 Error_Node := N;
2497 else
2498 Error_Node := Node (Elmt);
2499 end if;
2500
2501 Error_Msg_NE
2502 ("value may be affected by call to & "
2503 & "because order of evaluation is arbitrary",
2504 Error_Node, Id);
2505 return Abandon;
2506 end;
2507
2508 -- First occurrence of a elementary type writable actual
2509
2510 else
2511 Append_New_Elmt (N, To => Writable_Actuals_List);
2512 end if;
2513
2514 else
2515 if Identifiers_List = No_Elist then
2516 Identifiers_List := New_Elmt_List;
2517 end if;
2518
2519 Append_Unique_Elmt (N, Identifiers_List);
2520 end if;
2521 end if;
2522
2523 return OK;
2524 end Check_Node;
2525
2526 --------------
2527 -- Contains --
2528 --------------
2529
2530 function Contains
2531 (List : Elist_Id;
2532 N : Node_Id) return Boolean
2533 is
2534 pragma Assert (Nkind (N) in N_Has_Entity);
2535
2536 Elmt : Elmt_Id;
2537
2538 begin
2539 if List = No_Elist then
2540 return False;
2541 end if;
2542
2543 Elmt := First_Elmt (List);
2544 while Present (Elmt) loop
2545 if Entity (Node (Elmt)) = Entity (N) then
2546 return True;
2547 else
2548 Next_Elmt (Elmt);
2549 end if;
2550 end loop;
2551
2552 return False;
2553 end Contains;
2554
2555 ------------------
2556 -- Do_Traversal --
2557 ------------------
2558
2559 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2560 -- The traversal procedure
2561
2562 -- Start of processing for Collect_Identifiers
2563
2564 begin
2565 if Present (Error_Node) then
2566 return;
2567 end if;
2568
2569 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2570 return;
2571 end if;
2572
2573 Do_Traversal (N);
2574 end Collect_Identifiers;
2575
2576 -------------------------------
2577 -- Preanalyze_Without_Errors --
2578 -------------------------------
2579
2580 procedure Preanalyze_Without_Errors (N : Node_Id) is
2581 Status : constant Boolean := Get_Ignore_Errors;
2582 begin
2583 Set_Ignore_Errors (True);
2584 Preanalyze (N);
2585 Set_Ignore_Errors (Status);
2586 end Preanalyze_Without_Errors;
2587
2588 -- Start of processing for Check_Function_Writable_Actuals
2589
2590 begin
2591 -- The check only applies to Ada 2012 code on which Check_Actuals has
2592 -- been set, and only to constructs that have multiple constituents
2593 -- whose order of evaluation is not specified by the language.
2594
2595 if Ada_Version < Ada_2012
2596 or else not Check_Actuals (N)
2597 or else (not (Nkind (N) in N_Op)
2598 and then not (Nkind (N) in N_Membership_Test)
2599 and then not Nkind_In (N, N_Range,
2600 N_Aggregate,
2601 N_Extension_Aggregate,
2602 N_Full_Type_Declaration,
2603 N_Function_Call,
2604 N_Procedure_Call_Statement,
2605 N_Entry_Call_Statement))
2606 or else (Nkind (N) = N_Full_Type_Declaration
2607 and then not Is_Record_Type (Defining_Identifier (N)))
2608
2609 -- In addition, this check only applies to source code, not to code
2610 -- generated by constraint checks.
2611
2612 or else not Comes_From_Source (N)
2613 then
2614 return;
2615 end if;
2616
2617 -- If a construct C has two or more direct constituents that are names
2618 -- or expressions whose evaluation may occur in an arbitrary order, at
2619 -- least one of which contains a function call with an in out or out
2620 -- parameter, then the construct is legal only if: for each name N that
2621 -- is passed as a parameter of mode in out or out to some inner function
2622 -- call C2 (not including the construct C itself), there is no other
2623 -- name anywhere within a direct constituent of the construct C other
2624 -- than the one containing C2, that is known to refer to the same
2625 -- object (RM 6.4.1(6.17/3)).
2626
2627 case Nkind (N) is
2628 when N_Range =>
2629 Collect_Identifiers (Low_Bound (N));
2630 Collect_Identifiers (High_Bound (N));
2631
2632 when N_Membership_Test
2633 | N_Op
2634 =>
2635 declare
2636 Expr : Node_Id;
2637
2638 begin
2639 Collect_Identifiers (Left_Opnd (N));
2640
2641 if Present (Right_Opnd (N)) then
2642 Collect_Identifiers (Right_Opnd (N));
2643 end if;
2644
2645 if Nkind_In (N, N_In, N_Not_In)
2646 and then Present (Alternatives (N))
2647 then
2648 Expr := First (Alternatives (N));
2649 while Present (Expr) loop
2650 Collect_Identifiers (Expr);
2651
2652 Next (Expr);
2653 end loop;
2654 end if;
2655 end;
2656
2657 when N_Full_Type_Declaration =>
2658 declare
2659 function Get_Record_Part (N : Node_Id) return Node_Id;
2660 -- Return the record part of this record type definition
2661
2662 function Get_Record_Part (N : Node_Id) return Node_Id is
2663 Type_Def : constant Node_Id := Type_Definition (N);
2664 begin
2665 if Nkind (Type_Def) = N_Derived_Type_Definition then
2666 return Record_Extension_Part (Type_Def);
2667 else
2668 return Type_Def;
2669 end if;
2670 end Get_Record_Part;
2671
2672 Comp : Node_Id;
2673 Def_Id : Entity_Id := Defining_Identifier (N);
2674 Rec : Node_Id := Get_Record_Part (N);
2675
2676 begin
2677 -- No need to perform any analysis if the record has no
2678 -- components
2679
2680 if No (Rec) or else No (Component_List (Rec)) then
2681 return;
2682 end if;
2683
2684 -- Collect the identifiers starting from the deepest
2685 -- derivation. Done to report the error in the deepest
2686 -- derivation.
2687
2688 loop
2689 if Present (Component_List (Rec)) then
2690 Comp := First (Component_Items (Component_List (Rec)));
2691 while Present (Comp) loop
2692 if Nkind (Comp) = N_Component_Declaration
2693 and then Present (Expression (Comp))
2694 then
2695 Collect_Identifiers (Expression (Comp));
2696 end if;
2697
2698 Next (Comp);
2699 end loop;
2700 end if;
2701
2702 exit when No (Underlying_Type (Etype (Def_Id)))
2703 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2704 = Def_Id;
2705
2706 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2707 Rec := Get_Record_Part (Parent (Def_Id));
2708 end loop;
2709 end;
2710
2711 when N_Entry_Call_Statement
2712 | N_Subprogram_Call
2713 =>
2714 declare
2715 Id : constant Entity_Id := Get_Called_Entity (N);
2716 Formal : Node_Id;
2717 Actual : Node_Id;
2718
2719 begin
2720 Formal := First_Formal (Id);
2721 Actual := First_Actual (N);
2722 while Present (Actual) and then Present (Formal) loop
2723 if Ekind_In (Formal, E_Out_Parameter,
2724 E_In_Out_Parameter)
2725 then
2726 Collect_Identifiers (Actual);
2727 end if;
2728
2729 Next_Formal (Formal);
2730 Next_Actual (Actual);
2731 end loop;
2732 end;
2733
2734 when N_Aggregate
2735 | N_Extension_Aggregate
2736 =>
2737 declare
2738 Assoc : Node_Id;
2739 Choice : Node_Id;
2740 Comp_Expr : Node_Id;
2741
2742 begin
2743 -- Handle the N_Others_Choice of array aggregates with static
2744 -- bounds. There is no need to perform this analysis in
2745 -- aggregates without static bounds since we cannot evaluate
2746 -- if the N_Others_Choice covers several elements. There is
2747 -- no need to handle the N_Others choice of record aggregates
2748 -- since at this stage it has been already expanded by
2749 -- Resolve_Record_Aggregate.
2750
2751 if Is_Array_Type (Etype (N))
2752 and then Nkind (N) = N_Aggregate
2753 and then Present (Aggregate_Bounds (N))
2754 and then Compile_Time_Known_Bounds (Etype (N))
2755 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2756 >
2757 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2758 then
2759 declare
2760 Count_Components : Uint := Uint_0;
2761 Num_Components : Uint;
2762 Others_Assoc : Node_Id;
2763 Others_Choice : Node_Id := Empty;
2764 Others_Box_Present : Boolean := False;
2765
2766 begin
2767 -- Count positional associations
2768
2769 if Present (Expressions (N)) then
2770 Comp_Expr := First (Expressions (N));
2771 while Present (Comp_Expr) loop
2772 Count_Components := Count_Components + 1;
2773 Next (Comp_Expr);
2774 end loop;
2775 end if;
2776
2777 -- Count the rest of elements and locate the N_Others
2778 -- choice (if any)
2779
2780 Assoc := First (Component_Associations (N));
2781 while Present (Assoc) loop
2782 Choice := First (Choices (Assoc));
2783 while Present (Choice) loop
2784 if Nkind (Choice) = N_Others_Choice then
2785 Others_Assoc := Assoc;
2786 Others_Choice := Choice;
2787 Others_Box_Present := Box_Present (Assoc);
2788
2789 -- Count several components
2790
2791 elsif Nkind_In (Choice, N_Range,
2792 N_Subtype_Indication)
2793 or else (Is_Entity_Name (Choice)
2794 and then Is_Type (Entity (Choice)))
2795 then
2796 declare
2797 L, H : Node_Id;
2798 begin
2799 Get_Index_Bounds (Choice, L, H);
2800 pragma Assert
2801 (Compile_Time_Known_Value (L)
2802 and then Compile_Time_Known_Value (H));
2803 Count_Components :=
2804 Count_Components
2805 + Expr_Value (H) - Expr_Value (L) + 1;
2806 end;
2807
2808 -- Count single component. No other case available
2809 -- since we are handling an aggregate with static
2810 -- bounds.
2811
2812 else
2813 pragma Assert (Is_OK_Static_Expression (Choice)
2814 or else Nkind (Choice) = N_Identifier
2815 or else Nkind (Choice) = N_Integer_Literal);
2816
2817 Count_Components := Count_Components + 1;
2818 end if;
2819
2820 Next (Choice);
2821 end loop;
2822
2823 Next (Assoc);
2824 end loop;
2825
2826 Num_Components :=
2827 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2828 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2829
2830 pragma Assert (Count_Components <= Num_Components);
2831
2832 -- Handle the N_Others choice if it covers several
2833 -- components
2834
2835 if Present (Others_Choice)
2836 and then (Num_Components - Count_Components) > 1
2837 then
2838 if not Others_Box_Present then
2839
2840 -- At this stage, if expansion is active, the
2841 -- expression of the others choice has not been
2842 -- analyzed. Hence we generate a duplicate and
2843 -- we analyze it silently to have available the
2844 -- minimum decoration required to collect the
2845 -- identifiers.
2846
2847 if not Expander_Active then
2848 Comp_Expr := Expression (Others_Assoc);
2849 else
2850 Comp_Expr :=
2851 New_Copy_Tree (Expression (Others_Assoc));
2852 Preanalyze_Without_Errors (Comp_Expr);
2853 end if;
2854
2855 Collect_Identifiers (Comp_Expr);
2856
2857 if Writable_Actuals_List /= No_Elist then
2858
2859 -- As suggested by Robert, at current stage we
2860 -- report occurrences of this case as warnings.
2861
2862 Error_Msg_N
2863 ("writable function parameter may affect "
2864 & "value in other component because order "
2865 & "of evaluation is unspecified??",
2866 Node (First_Elmt (Writable_Actuals_List)));
2867 end if;
2868 end if;
2869 end if;
2870 end;
2871
2872 -- For an array aggregate, a discrete_choice_list that has
2873 -- a nonstatic range is considered as two or more separate
2874 -- occurrences of the expression (RM 6.4.1(20/3)).
2875
2876 elsif Is_Array_Type (Etype (N))
2877 and then Nkind (N) = N_Aggregate
2878 and then Present (Aggregate_Bounds (N))
2879 and then not Compile_Time_Known_Bounds (Etype (N))
2880 then
2881 -- Collect identifiers found in the dynamic bounds
2882
2883 declare
2884 Count_Components : Natural := 0;
2885 Low, High : Node_Id;
2886
2887 begin
2888 Assoc := First (Component_Associations (N));
2889 while Present (Assoc) loop
2890 Choice := First (Choices (Assoc));
2891 while Present (Choice) loop
2892 if Nkind_In (Choice, N_Range,
2893 N_Subtype_Indication)
2894 or else (Is_Entity_Name (Choice)
2895 and then Is_Type (Entity (Choice)))
2896 then
2897 Get_Index_Bounds (Choice, Low, High);
2898
2899 if not Compile_Time_Known_Value (Low) then
2900 Collect_Identifiers (Low);
2901
2902 if No (Aggr_Error_Node) then
2903 Aggr_Error_Node := Low;
2904 end if;
2905 end if;
2906
2907 if not Compile_Time_Known_Value (High) then
2908 Collect_Identifiers (High);
2909
2910 if No (Aggr_Error_Node) then
2911 Aggr_Error_Node := High;
2912 end if;
2913 end if;
2914
2915 -- The RM rule is violated if there is more than
2916 -- a single choice in a component association.
2917
2918 else
2919 Count_Components := Count_Components + 1;
2920
2921 if No (Aggr_Error_Node)
2922 and then Count_Components > 1
2923 then
2924 Aggr_Error_Node := Choice;
2925 end if;
2926
2927 if not Compile_Time_Known_Value (Choice) then
2928 Collect_Identifiers (Choice);
2929 end if;
2930 end if;
2931
2932 Next (Choice);
2933 end loop;
2934
2935 Next (Assoc);
2936 end loop;
2937 end;
2938 end if;
2939
2940 -- Handle ancestor part of extension aggregates
2941
2942 if Nkind (N) = N_Extension_Aggregate then
2943 Collect_Identifiers (Ancestor_Part (N));
2944 end if;
2945
2946 -- Handle positional associations
2947
2948 if Present (Expressions (N)) then
2949 Comp_Expr := First (Expressions (N));
2950 while Present (Comp_Expr) loop
2951 if not Is_OK_Static_Expression (Comp_Expr) then
2952 Collect_Identifiers (Comp_Expr);
2953 end if;
2954
2955 Next (Comp_Expr);
2956 end loop;
2957 end if;
2958
2959 -- Handle discrete associations
2960
2961 if Present (Component_Associations (N)) then
2962 Assoc := First (Component_Associations (N));
2963 while Present (Assoc) loop
2964
2965 if not Box_Present (Assoc) then
2966 Choice := First (Choices (Assoc));
2967 while Present (Choice) loop
2968
2969 -- For now we skip discriminants since it requires
2970 -- performing the analysis in two phases: first one
2971 -- analyzing discriminants and second one analyzing
2972 -- the rest of components since discriminants are
2973 -- evaluated prior to components: too much extra
2974 -- work to detect a corner case???
2975
2976 if Nkind (Choice) in N_Has_Entity
2977 and then Present (Entity (Choice))
2978 and then Ekind (Entity (Choice)) = E_Discriminant
2979 then
2980 null;
2981
2982 elsif Box_Present (Assoc) then
2983 null;
2984
2985 else
2986 if not Analyzed (Expression (Assoc)) then
2987 Comp_Expr :=
2988 New_Copy_Tree (Expression (Assoc));
2989 Set_Parent (Comp_Expr, Parent (N));
2990 Preanalyze_Without_Errors (Comp_Expr);
2991 else
2992 Comp_Expr := Expression (Assoc);
2993 end if;
2994
2995 Collect_Identifiers (Comp_Expr);
2996 end if;
2997
2998 Next (Choice);
2999 end loop;
3000 end if;
3001
3002 Next (Assoc);
3003 end loop;
3004 end if;
3005 end;
3006
3007 when others =>
3008 return;
3009 end case;
3010
3011 -- No further action needed if we already reported an error
3012
3013 if Present (Error_Node) then
3014 return;
3015 end if;
3016
3017 -- Check violation of RM 6.20/3 in aggregates
3018
3019 if Present (Aggr_Error_Node)
3020 and then Writable_Actuals_List /= No_Elist
3021 then
3022 Error_Msg_N
3023 ("value may be affected by call in other component because they "
3024 & "are evaluated in unspecified order",
3025 Node (First_Elmt (Writable_Actuals_List)));
3026 return;
3027 end if;
3028
3029 -- Check if some writable argument of a function is referenced
3030
3031 if Writable_Actuals_List /= No_Elist
3032 and then Identifiers_List /= No_Elist
3033 then
3034 declare
3035 Elmt_1 : Elmt_Id;
3036 Elmt_2 : Elmt_Id;
3037
3038 begin
3039 Elmt_1 := First_Elmt (Writable_Actuals_List);
3040 while Present (Elmt_1) loop
3041 Elmt_2 := First_Elmt (Identifiers_List);
3042 while Present (Elmt_2) loop
3043 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
3044 case Nkind (Parent (Node (Elmt_2))) is
3045 when N_Aggregate
3046 | N_Component_Association
3047 | N_Component_Declaration
3048 =>
3049 Error_Msg_N
3050 ("value may be affected by call in other "
3051 & "component because they are evaluated "
3052 & "in unspecified order",
3053 Node (Elmt_2));
3054
3055 when N_In
3056 | N_Not_In
3057 =>
3058 Error_Msg_N
3059 ("value may be affected by call in other "
3060 & "alternative because they are evaluated "
3061 & "in unspecified order",
3062 Node (Elmt_2));
3063
3064 when others =>
3065 Error_Msg_N
3066 ("value of actual may be affected by call in "
3067 & "other actual because they are evaluated "
3068 & "in unspecified order",
3069 Node (Elmt_2));
3070 end case;
3071 end if;
3072
3073 Next_Elmt (Elmt_2);
3074 end loop;
3075
3076 Next_Elmt (Elmt_1);
3077 end loop;
3078 end;
3079 end if;
3080 end Check_Function_Writable_Actuals;
3081
3082 --------------------------------
3083 -- Check_Implicit_Dereference --
3084 --------------------------------
3085
3086 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
3087 Disc : Entity_Id;
3088 Desig : Entity_Id;
3089 Nam : Node_Id;
3090
3091 begin
3092 if Nkind (N) = N_Indexed_Component
3093 and then Present (Generalized_Indexing (N))
3094 then
3095 Nam := Generalized_Indexing (N);
3096 else
3097 Nam := N;
3098 end if;
3099
3100 if Ada_Version < Ada_2012
3101 or else not Has_Implicit_Dereference (Base_Type (Typ))
3102 then
3103 return;
3104
3105 elsif not Comes_From_Source (N)
3106 and then Nkind (N) /= N_Indexed_Component
3107 then
3108 return;
3109
3110 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
3111 null;
3112
3113 else
3114 Disc := First_Discriminant (Typ);
3115 while Present (Disc) loop
3116 if Has_Implicit_Dereference (Disc) then
3117 Desig := Designated_Type (Etype (Disc));
3118 Add_One_Interp (Nam, Disc, Desig);
3119
3120 -- If the node is a generalized indexing, add interpretation
3121 -- to that node as well, for subsequent resolution.
3122
3123 if Nkind (N) = N_Indexed_Component then
3124 Add_One_Interp (N, Disc, Desig);
3125 end if;
3126
3127 -- If the operation comes from a generic unit and the context
3128 -- is a selected component, the selector name may be global
3129 -- and set in the instance already. Remove the entity to
3130 -- force resolution of the selected component, and the
3131 -- generation of an explicit dereference if needed.
3132
3133 if In_Instance
3134 and then Nkind (Parent (Nam)) = N_Selected_Component
3135 then
3136 Set_Entity (Selector_Name (Parent (Nam)), Empty);
3137 end if;
3138
3139 exit;
3140 end if;
3141
3142 Next_Discriminant (Disc);
3143 end loop;
3144 end if;
3145 end Check_Implicit_Dereference;
3146
3147 ----------------------------------
3148 -- Check_Internal_Protected_Use --
3149 ----------------------------------
3150
3151 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
3152 S : Entity_Id;
3153 Prot : Entity_Id;
3154
3155 begin
3156 Prot := Empty;
3157
3158 S := Current_Scope;
3159 while Present (S) loop
3160 if S = Standard_Standard then
3161 exit;
3162
3163 elsif Ekind (S) = E_Function
3164 and then Ekind (Scope (S)) = E_Protected_Type
3165 then
3166 Prot := Scope (S);
3167 exit;
3168 end if;
3169
3170 S := Scope (S);
3171 end loop;
3172
3173 if Present (Prot)
3174 and then Scope (Nam) = Prot
3175 and then Ekind (Nam) /= E_Function
3176 then
3177 -- An indirect function call (e.g. a callback within a protected
3178 -- function body) is not statically illegal. If the access type is
3179 -- anonymous and is the type of an access parameter, the scope of Nam
3180 -- will be the protected type, but it is not a protected operation.
3181
3182 if Ekind (Nam) = E_Subprogram_Type
3183 and then Nkind (Associated_Node_For_Itype (Nam)) =
3184 N_Function_Specification
3185 then
3186 null;
3187
3188 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
3189 Error_Msg_N
3190 ("within protected function cannot use protected procedure in "
3191 & "renaming or as generic actual", N);
3192
3193 elsif Nkind (N) = N_Attribute_Reference then
3194 Error_Msg_N
3195 ("within protected function cannot take access of protected "
3196 & "procedure", N);
3197
3198 else
3199 Error_Msg_N
3200 ("within protected function, protected object is constant", N);
3201 Error_Msg_N
3202 ("\cannot call operation that may modify it", N);
3203 end if;
3204 end if;
3205
3206 -- Verify that an internal call does not appear within a precondition
3207 -- of a protected operation. This implements AI12-0166.
3208 -- The precondition aspect has been rewritten as a pragma Precondition
3209 -- and we check whether the scope of the called subprogram is the same
3210 -- as that of the entity to which the aspect applies.
3211
3212 if Convention (Nam) = Convention_Protected then
3213 declare
3214 P : Node_Id;
3215
3216 begin
3217 P := Parent (N);
3218 while Present (P) loop
3219 if Nkind (P) = N_Pragma
3220 and then Chars (Pragma_Identifier (P)) = Name_Precondition
3221 and then From_Aspect_Specification (P)
3222 and then
3223 Scope (Entity (Corresponding_Aspect (P))) = Scope (Nam)
3224 then
3225 Error_Msg_N
3226 ("internal call cannot appear in precondition of "
3227 & "protected operation", N);
3228 return;
3229
3230 elsif Nkind (P) = N_Pragma
3231 and then Chars (Pragma_Identifier (P)) = Name_Contract_Cases
3232 then
3233 -- Check whether call is in a case guard. It is legal in a
3234 -- consequence.
3235
3236 P := N;
3237 while Present (P) loop
3238 if Nkind (Parent (P)) = N_Component_Association
3239 and then P /= Expression (Parent (P))
3240 then
3241 Error_Msg_N
3242 ("internal call cannot appear in case guard in a "
3243 & "contract case", N);
3244 end if;
3245
3246 P := Parent (P);
3247 end loop;
3248
3249 return;
3250
3251 elsif Nkind (P) = N_Parameter_Specification
3252 and then Scope (Current_Scope) = Scope (Nam)
3253 and then Nkind_In (Parent (P), N_Entry_Declaration,
3254 N_Subprogram_Declaration)
3255 then
3256 Error_Msg_N
3257 ("internal call cannot appear in default for formal of "
3258 & "protected operation", N);
3259 return;
3260 end if;
3261
3262 P := Parent (P);
3263 end loop;
3264 end;
3265 end if;
3266 end Check_Internal_Protected_Use;
3267
3268 ---------------------------------------
3269 -- Check_Later_Vs_Basic_Declarations --
3270 ---------------------------------------
3271
3272 procedure Check_Later_Vs_Basic_Declarations
3273 (Decls : List_Id;
3274 During_Parsing : Boolean)
3275 is
3276 Body_Sloc : Source_Ptr;
3277 Decl : Node_Id;
3278
3279 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
3280 -- Return whether Decl is considered as a declarative item.
3281 -- When During_Parsing is True, the semantics of Ada 83 is followed.
3282 -- When During_Parsing is False, the semantics of SPARK is followed.
3283
3284 -------------------------------
3285 -- Is_Later_Declarative_Item --
3286 -------------------------------
3287
3288 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
3289 begin
3290 if Nkind (Decl) in N_Later_Decl_Item then
3291 return True;
3292
3293 elsif Nkind (Decl) = N_Pragma then
3294 return True;
3295
3296 elsif During_Parsing then
3297 return False;
3298
3299 -- In SPARK, a package declaration is not considered as a later
3300 -- declarative item.
3301
3302 elsif Nkind (Decl) = N_Package_Declaration then
3303 return False;
3304
3305 -- In SPARK, a renaming is considered as a later declarative item
3306
3307 elsif Nkind (Decl) in N_Renaming_Declaration then
3308 return True;
3309
3310 else
3311 return False;
3312 end if;
3313 end Is_Later_Declarative_Item;
3314
3315 -- Start of processing for Check_Later_Vs_Basic_Declarations
3316
3317 begin
3318 Decl := First (Decls);
3319
3320 -- Loop through sequence of basic declarative items
3321
3322 Outer : while Present (Decl) loop
3323 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
3324 and then Nkind (Decl) not in N_Body_Stub
3325 then
3326 Next (Decl);
3327
3328 -- Once a body is encountered, we only allow later declarative
3329 -- items. The inner loop checks the rest of the list.
3330
3331 else
3332 Body_Sloc := Sloc (Decl);
3333
3334 Inner : while Present (Decl) loop
3335 if not Is_Later_Declarative_Item (Decl) then
3336 if During_Parsing then
3337 if Ada_Version = Ada_83 then
3338 Error_Msg_Sloc := Body_Sloc;
3339 Error_Msg_N
3340 ("(Ada 83) decl cannot appear after body#", Decl);
3341 end if;
3342 else
3343 Error_Msg_Sloc := Body_Sloc;
3344 Check_SPARK_05_Restriction
3345 ("decl cannot appear after body#", Decl);
3346 end if;
3347 end if;
3348
3349 Next (Decl);
3350 end loop Inner;
3351 end if;
3352 end loop Outer;
3353 end Check_Later_Vs_Basic_Declarations;
3354
3355 ---------------------------
3356 -- Check_No_Hidden_State --
3357 ---------------------------
3358
3359 procedure Check_No_Hidden_State (Id : Entity_Id) is
3360 Context : Entity_Id := Empty;
3361 Not_Visible : Boolean := False;
3362 Scop : Entity_Id;
3363
3364 begin
3365 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3366
3367 -- Nothing to do for internally-generated abstract states and variables
3368 -- because they do not represent the hidden state of the source unit.
3369
3370 if not Comes_From_Source (Id) then
3371 return;
3372 end if;
3373
3374 -- Find the proper context where the object or state appears
3375
3376 Scop := Scope (Id);
3377 while Present (Scop) loop
3378 Context := Scop;
3379
3380 -- Keep track of the context's visibility
3381
3382 Not_Visible := Not_Visible or else In_Private_Part (Context);
3383
3384 -- Prevent the search from going too far
3385
3386 if Context = Standard_Standard then
3387 return;
3388
3389 -- Objects and states that appear immediately within a subprogram or
3390 -- entry inside a construct nested within a subprogram do not
3391 -- introduce a hidden state. They behave as local variable
3392 -- declarations. The same is true for elaboration code inside a block
3393 -- or a task.
3394
3395 elsif Is_Subprogram_Or_Entry (Context)
3396 or else Ekind_In (Context, E_Block, E_Task_Type)
3397 then
3398 return;
3399
3400 -- When examining a package body, use the entity of the spec as it
3401 -- carries the abstract state declarations.
3402
3403 elsif Ekind (Context) = E_Package_Body then
3404 Context := Spec_Entity (Context);
3405 end if;
3406
3407 -- Stop the traversal when a package subject to a null abstract state
3408 -- has been found.
3409
3410 if Is_Package_Or_Generic_Package (Context)
3411 and then Has_Null_Abstract_State (Context)
3412 then
3413 exit;
3414 end if;
3415
3416 Scop := Scope (Scop);
3417 end loop;
3418
3419 -- At this point we know that there is at least one package with a null
3420 -- abstract state in visibility. Emit an error message unconditionally
3421 -- if the entity being processed is a state because the placement of the
3422 -- related package is irrelevant. This is not the case for objects as
3423 -- the intermediate context matters.
3424
3425 if Present (Context)
3426 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3427 then
3428 Error_Msg_N ("cannot introduce hidden state &", Id);
3429 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3430 end if;
3431 end Check_No_Hidden_State;
3432
3433 ----------------------------------------
3434 -- Check_Nonvolatile_Function_Profile --
3435 ----------------------------------------
3436
3437 procedure Check_Nonvolatile_Function_Profile (Func_Id : Entity_Id) is
3438 Formal : Entity_Id;
3439
3440 begin
3441 -- Inspect all formal parameters
3442
3443 Formal := First_Formal (Func_Id);
3444 while Present (Formal) loop
3445 if Is_Effectively_Volatile (Etype (Formal)) then
3446 Error_Msg_NE
3447 ("nonvolatile function & cannot have a volatile parameter",
3448 Formal, Func_Id);
3449 end if;
3450
3451 Next_Formal (Formal);
3452 end loop;
3453
3454 -- Inspect the return type
3455
3456 if Is_Effectively_Volatile (Etype (Func_Id)) then
3457 Error_Msg_NE
3458 ("nonvolatile function & cannot have a volatile return type",
3459 Result_Definition (Parent (Func_Id)), Func_Id);
3460 end if;
3461 end Check_Nonvolatile_Function_Profile;
3462
3463 -----------------------------
3464 -- Check_Part_Of_Reference --
3465 -----------------------------
3466
3467 procedure Check_Part_Of_Reference (Var_Id : Entity_Id; Ref : Node_Id) is
3468 function Is_Enclosing_Package_Body
3469 (Body_Decl : Node_Id;
3470 Obj_Id : Entity_Id) return Boolean;
3471 pragma Inline (Is_Enclosing_Package_Body);
3472 -- Determine whether package body Body_Decl or its corresponding spec
3473 -- immediately encloses the declaration of object Obj_Id.
3474
3475 function Is_Internal_Declaration_Or_Body
3476 (Decl : Node_Id) return Boolean;
3477 pragma Inline (Is_Internal_Declaration_Or_Body);
3478 -- Determine whether declaration or body denoted by Decl is internal
3479
3480 function Is_Single_Declaration_Or_Body
3481 (Decl : Node_Id;
3482 Conc_Typ : Entity_Id) return Boolean;
3483 pragma Inline (Is_Single_Declaration_Or_Body);
3484 -- Determine whether protected/task declaration or body denoted by Decl
3485 -- belongs to single concurrent type Conc_Typ.
3486
3487 function Is_Single_Task_Pragma
3488 (Prag : Node_Id;
3489 Task_Typ : Entity_Id) return Boolean;
3490 pragma Inline (Is_Single_Task_Pragma);
3491 -- Determine whether pragma Prag belongs to single task type Task_Typ
3492
3493 -------------------------------
3494 -- Is_Enclosing_Package_Body --
3495 -------------------------------
3496
3497 function Is_Enclosing_Package_Body
3498 (Body_Decl : Node_Id;
3499 Obj_Id : Entity_Id) return Boolean
3500 is
3501 Obj_Context : Node_Id;
3502
3503 begin
3504 -- Find the context of the object declaration
3505
3506 Obj_Context := Parent (Declaration_Node (Obj_Id));
3507
3508 if Nkind (Obj_Context) = N_Package_Specification then
3509 Obj_Context := Parent (Obj_Context);
3510 end if;
3511
3512 -- The object appears immediately within the package body
3513
3514 if Obj_Context = Body_Decl then
3515 return True;
3516
3517 -- The object appears immediately within the corresponding spec
3518
3519 elsif Nkind (Obj_Context) = N_Package_Declaration
3520 and then Unit_Declaration_Node (Corresponding_Spec (Body_Decl)) =
3521 Obj_Context
3522 then
3523 return True;
3524 end if;
3525
3526 return False;
3527 end Is_Enclosing_Package_Body;
3528
3529 -------------------------------------
3530 -- Is_Internal_Declaration_Or_Body --
3531 -------------------------------------
3532
3533 function Is_Internal_Declaration_Or_Body
3534 (Decl : Node_Id) return Boolean
3535 is
3536 begin
3537 if Comes_From_Source (Decl) then
3538 return False;
3539
3540 -- A body generated for an expression function which has not been
3541 -- inserted into the tree yet (In_Spec_Expression is True) is not
3542 -- considered internal.
3543
3544 elsif Nkind (Decl) = N_Subprogram_Body
3545 and then Was_Expression_Function (Decl)
3546 and then not In_Spec_Expression
3547 then
3548 return False;
3549 end if;
3550
3551 return True;
3552 end Is_Internal_Declaration_Or_Body;
3553
3554 -----------------------------------
3555 -- Is_Single_Declaration_Or_Body --
3556 -----------------------------------
3557
3558 function Is_Single_Declaration_Or_Body
3559 (Decl : Node_Id;
3560 Conc_Typ : Entity_Id) return Boolean
3561 is
3562 Spec_Id : constant Entity_Id := Unique_Defining_Entity (Decl);
3563
3564 begin
3565 return
3566 Present (Anonymous_Object (Spec_Id))
3567 and then Anonymous_Object (Spec_Id) = Conc_Typ;
3568 end Is_Single_Declaration_Or_Body;
3569
3570 ---------------------------
3571 -- Is_Single_Task_Pragma --
3572 ---------------------------
3573
3574 function Is_Single_Task_Pragma
3575 (Prag : Node_Id;
3576 Task_Typ : Entity_Id) return Boolean
3577 is
3578 Decl : constant Node_Id := Find_Related_Declaration_Or_Body (Prag);
3579
3580 begin
3581 -- To qualify, the pragma must be associated with single task type
3582 -- Task_Typ.
3583
3584 return
3585 Is_Single_Task_Object (Task_Typ)
3586 and then Nkind (Decl) = N_Object_Declaration
3587 and then Defining_Entity (Decl) = Task_Typ;
3588 end Is_Single_Task_Pragma;
3589
3590 -- Local variables
3591
3592 Conc_Obj : constant Entity_Id := Encapsulating_State (Var_Id);
3593 Par : Node_Id;
3594 Prag_Nam : Name_Id;
3595 Prev : Node_Id;
3596
3597 -- Start of processing for Check_Part_Of_Reference
3598
3599 begin
3600 -- Nothing to do when the variable was recorded, but did not become a
3601 -- constituent of a single concurrent type.
3602
3603 if No (Conc_Obj) then
3604 return;
3605 end if;
3606
3607 -- Traverse the parent chain looking for a suitable context for the
3608 -- reference to the concurrent constituent.
3609
3610 Prev := Ref;
3611 Par := Parent (Prev);
3612 while Present (Par) loop
3613 if Nkind (Par) = N_Pragma then
3614 Prag_Nam := Pragma_Name (Par);
3615
3616 -- A concurrent constituent is allowed to appear in pragmas
3617 -- Initial_Condition and Initializes as this is part of the
3618 -- elaboration checks for the constituent (SPARK RM 9(3)).
3619
3620 if Nam_In (Prag_Nam, Name_Initial_Condition, Name_Initializes) then
3621 return;
3622
3623 -- When the reference appears within pragma Depends or Global,
3624 -- check whether the pragma applies to a single task type. Note
3625 -- that the pragma may not encapsulated by the type definition,
3626 -- but this is still a valid context.
3627
3628 elsif Nam_In (Prag_Nam, Name_Depends, Name_Global)
3629 and then Is_Single_Task_Pragma (Par, Conc_Obj)
3630 then
3631 return;
3632 end if;
3633
3634 -- The reference appears somewhere in the definition of a single
3635 -- concurrent type (SPARK RM 9(3)).
3636
3637 elsif Nkind_In (Par, N_Single_Protected_Declaration,
3638 N_Single_Task_Declaration)
3639 and then Defining_Entity (Par) = Conc_Obj
3640 then
3641 return;
3642
3643 -- The reference appears within the declaration or body of a single
3644 -- concurrent type (SPARK RM 9(3)).
3645
3646 elsif Nkind_In (Par, N_Protected_Body,
3647 N_Protected_Type_Declaration,
3648 N_Task_Body,
3649 N_Task_Type_Declaration)
3650 and then Is_Single_Declaration_Or_Body (Par, Conc_Obj)
3651 then
3652 return;
3653
3654 -- The reference appears within the statement list of the object's
3655 -- immediately enclosing package (SPARK RM 9(3)).
3656
3657 elsif Nkind (Par) = N_Package_Body
3658 and then Nkind (Prev) = N_Handled_Sequence_Of_Statements
3659 and then Is_Enclosing_Package_Body (Par, Var_Id)
3660 then
3661 return;
3662
3663 -- The reference has been relocated within an internally generated
3664 -- package or subprogram. Assume that the reference is legal as the
3665 -- real check was already performed in the original context of the
3666 -- reference.
3667
3668 elsif Nkind_In (Par, N_Package_Body,
3669 N_Package_Declaration,
3670 N_Subprogram_Body,
3671 N_Subprogram_Declaration)
3672 and then Is_Internal_Declaration_Or_Body (Par)
3673 then
3674 return;
3675
3676 -- The reference has been relocated to an inlined body for GNATprove.
3677 -- Assume that the reference is legal as the real check was already
3678 -- performed in the original context of the reference.
3679
3680 elsif GNATprove_Mode
3681 and then Nkind (Par) = N_Subprogram_Body
3682 and then Chars (Defining_Entity (Par)) = Name_uParent
3683 then
3684 return;
3685 end if;
3686
3687 Prev := Par;
3688 Par := Parent (Prev);
3689 end loop;
3690
3691 -- At this point it is known that the reference does not appear within a
3692 -- legal context.
3693
3694 Error_Msg_NE
3695 ("reference to variable & cannot appear in this context", Ref, Var_Id);
3696 Error_Msg_Name_1 := Chars (Var_Id);
3697
3698 if Is_Single_Protected_Object (Conc_Obj) then
3699 Error_Msg_NE
3700 ("\% is constituent of single protected type &", Ref, Conc_Obj);
3701
3702 else
3703 Error_Msg_NE
3704 ("\% is constituent of single task type &", Ref, Conc_Obj);
3705 end if;
3706 end Check_Part_Of_Reference;
3707
3708 ------------------------------------------
3709 -- Check_Potentially_Blocking_Operation --
3710 ------------------------------------------
3711
3712 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3713 S : Entity_Id;
3714
3715 begin
3716 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3717 -- When pragma Detect_Blocking is active, the run time will raise
3718 -- Program_Error. Here we only issue a warning, since we generally
3719 -- support the use of potentially blocking operations in the absence
3720 -- of the pragma.
3721
3722 -- Indirect blocking through a subprogram call cannot be diagnosed
3723 -- statically without interprocedural analysis, so we do not attempt
3724 -- to do it here.
3725
3726 S := Scope (Current_Scope);
3727 while Present (S) and then S /= Standard_Standard loop
3728 if Is_Protected_Type (S) then
3729 Error_Msg_N
3730 ("potentially blocking operation in protected operation??", N);
3731 return;
3732 end if;
3733
3734 S := Scope (S);
3735 end loop;
3736 end Check_Potentially_Blocking_Operation;
3737
3738 ------------------------------------
3739 -- Check_Previous_Null_Procedure --
3740 ------------------------------------
3741
3742 procedure Check_Previous_Null_Procedure
3743 (Decl : Node_Id;
3744 Prev : Entity_Id)
3745 is
3746 begin
3747 if Ekind (Prev) = E_Procedure
3748 and then Nkind (Parent (Prev)) = N_Procedure_Specification
3749 and then Null_Present (Parent (Prev))
3750 then
3751 Error_Msg_Sloc := Sloc (Prev);
3752 Error_Msg_N
3753 ("declaration cannot complete previous null procedure#", Decl);
3754 end if;
3755 end Check_Previous_Null_Procedure;
3756
3757 ---------------------------------
3758 -- Check_Result_And_Post_State --
3759 ---------------------------------
3760
3761 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3762 procedure Check_Result_And_Post_State_In_Pragma
3763 (Prag : Node_Id;
3764 Result_Seen : in out Boolean);
3765 -- Determine whether pragma Prag mentions attribute 'Result and whether
3766 -- the pragma contains an expression that evaluates differently in pre-
3767 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3768 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3769
3770 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3771 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3772 -- formal parameter.
3773
3774 -------------------------------------------
3775 -- Check_Result_And_Post_State_In_Pragma --
3776 -------------------------------------------
3777
3778 procedure Check_Result_And_Post_State_In_Pragma
3779 (Prag : Node_Id;
3780 Result_Seen : in out Boolean)
3781 is
3782 procedure Check_Conjunct (Expr : Node_Id);
3783 -- Check an individual conjunct in a conjunction of Boolean
3784 -- expressions, connected by "and" or "and then" operators.
3785
3786 procedure Check_Conjuncts (Expr : Node_Id);
3787 -- Apply the post-state check to every conjunct in an expression, in
3788 -- case this is a conjunction of Boolean expressions. Otherwise apply
3789 -- it to the expression as a whole.
3790
3791 procedure Check_Expression (Expr : Node_Id);
3792 -- Perform the 'Result and post-state checks on a given expression
3793
3794 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3795 -- Attempt to find attribute 'Result in a subtree denoted by N
3796
3797 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3798 -- Determine whether source node N denotes "True" or "False"
3799
3800 function Mentions_Post_State (N : Node_Id) return Boolean;
3801 -- Determine whether a subtree denoted by N mentions any construct
3802 -- that denotes a post-state.
3803
3804 procedure Check_Function_Result is
3805 new Traverse_Proc (Is_Function_Result);
3806
3807 --------------------
3808 -- Check_Conjunct --
3809 --------------------
3810
3811 procedure Check_Conjunct (Expr : Node_Id) is
3812 function Adjust_Message (Msg : String) return String;
3813 -- Prepend a prefix to the input message Msg denoting that the
3814 -- message applies to a conjunct in the expression, when this
3815 -- is the case.
3816
3817 function Applied_On_Conjunct return Boolean;
3818 -- Returns True if the message applies to a conjunct in the
3819 -- expression, instead of the whole expression.
3820
3821 function Has_Global_Output (Subp : Entity_Id) return Boolean;
3822 -- Returns True if Subp has an output in its Global contract
3823
3824 function Has_No_Output (Subp : Entity_Id) return Boolean;
3825 -- Returns True if Subp has no declared output: no function
3826 -- result, no output parameter, and no output in its Global
3827 -- contract.
3828
3829 --------------------
3830 -- Adjust_Message --
3831 --------------------
3832
3833 function Adjust_Message (Msg : String) return String is
3834 begin
3835 if Applied_On_Conjunct then
3836 return "conjunct in " & Msg;
3837 else
3838 return Msg;
3839 end if;
3840 end Adjust_Message;
3841
3842 -------------------------
3843 -- Applied_On_Conjunct --
3844 -------------------------
3845
3846 function Applied_On_Conjunct return Boolean is
3847 begin
3848 -- Expr is the conjunct of an enclosing "and" expression
3849
3850 return Nkind (Parent (Expr)) in N_Subexpr
3851
3852 -- or Expr is a conjunct of an enclosing "and then"
3853 -- expression in a postcondition aspect that was split into
3854 -- multiple pragmas. The first conjunct has the "and then"
3855 -- expression as Original_Node, and other conjuncts have
3856 -- Split_PCC set to True.
3857
3858 or else Nkind (Original_Node (Expr)) = N_And_Then
3859 or else Split_PPC (Prag);
3860 end Applied_On_Conjunct;
3861
3862 -----------------------
3863 -- Has_Global_Output --
3864 -----------------------
3865
3866 function Has_Global_Output (Subp : Entity_Id) return Boolean is
3867 Global : constant Node_Id := Get_Pragma (Subp, Pragma_Global);
3868 List : Node_Id;
3869 Assoc : Node_Id;
3870
3871 begin
3872 if No (Global) then
3873 return False;
3874 end if;
3875
3876 List := Expression (Get_Argument (Global, Subp));
3877
3878 -- Empty list (no global items) or single global item
3879 -- declaration (only input items).
3880
3881 if Nkind_In (List, N_Null,
3882 N_Expanded_Name,
3883 N_Identifier,
3884 N_Selected_Component)
3885 then
3886 return False;
3887
3888 -- Simple global list (only input items) or moded global list
3889 -- declaration.
3890
3891 elsif Nkind (List) = N_Aggregate then
3892 if Present (Expressions (List)) then
3893 return False;
3894
3895 else
3896 Assoc := First (Component_Associations (List));
3897 while Present (Assoc) loop
3898 if Chars (First (Choices (Assoc))) /= Name_Input then
3899 return True;
3900 end if;
3901
3902 Next (Assoc);
3903 end loop;
3904
3905 return False;
3906 end if;
3907
3908 -- To accommodate partial decoration of disabled SPARK
3909 -- features, this routine may be called with illegal input.
3910 -- If this is the case, do not raise Program_Error.
3911
3912 else
3913 return False;
3914 end if;
3915 end Has_Global_Output;
3916
3917 -------------------
3918 -- Has_No_Output --
3919 -------------------
3920
3921 function Has_No_Output (Subp : Entity_Id) return Boolean is
3922 Param : Node_Id;
3923
3924 begin
3925 -- A function has its result as output
3926
3927 if Ekind (Subp) = E_Function then
3928 return False;
3929 end if;
3930
3931 -- An OUT or IN OUT parameter is an output
3932
3933 Param := First_Formal (Subp);
3934 while Present (Param) loop
3935 if Ekind_In (Param, E_Out_Parameter, E_In_Out_Parameter) then
3936 return False;
3937 end if;
3938
3939 Next_Formal (Param);
3940 end loop;
3941
3942 -- An item of mode Output or In_Out in the Global contract is
3943 -- an output.
3944
3945 if Has_Global_Output (Subp) then
3946 return False;
3947 end if;
3948
3949 return True;
3950 end Has_No_Output;
3951
3952 -- Local variables
3953
3954 Err_Node : Node_Id;
3955 -- Error node when reporting a warning on a (refined)
3956 -- postcondition.
3957
3958 -- Start of processing for Check_Conjunct
3959
3960 begin
3961 if Applied_On_Conjunct then
3962 Err_Node := Expr;
3963 else
3964 Err_Node := Prag;
3965 end if;
3966
3967 -- Do not report missing reference to outcome in postcondition if
3968 -- either the postcondition is trivially True or False, or if the
3969 -- subprogram is ghost and has no declared output.
3970
3971 if not Is_Trivial_Boolean (Expr)
3972 and then not Mentions_Post_State (Expr)
3973 and then not (Is_Ghost_Entity (Subp_Id)
3974 and then Has_No_Output (Subp_Id))
3975 then
3976 if Pragma_Name (Prag) = Name_Contract_Cases then
3977 Error_Msg_NE (Adjust_Message
3978 ("contract case does not check the outcome of calling "
3979 & "&?T?"), Expr, Subp_Id);
3980
3981 elsif Pragma_Name (Prag) = Name_Refined_Post then
3982 Error_Msg_NE (Adjust_Message
3983 ("refined postcondition does not check the outcome of "
3984 & "calling &?T?"), Err_Node, Subp_Id);
3985
3986 else
3987 Error_Msg_NE (Adjust_Message
3988 ("postcondition does not check the outcome of calling "
3989 & "&?T?"), Err_Node, Subp_Id);
3990 end if;
3991 end if;
3992 end Check_Conjunct;
3993
3994 ---------------------
3995 -- Check_Conjuncts --
3996 ---------------------
3997
3998 procedure Check_Conjuncts (Expr : Node_Id) is
3999 begin
4000 if Nkind_In (Expr, N_Op_And, N_And_Then) then
4001 Check_Conjuncts (Left_Opnd (Expr));
4002 Check_Conjuncts (Right_Opnd (Expr));
4003 else
4004 Check_Conjunct (Expr);
4005 end if;
4006 end Check_Conjuncts;
4007
4008 ----------------------
4009 -- Check_Expression --
4010 ----------------------
4011
4012 procedure Check_Expression (Expr : Node_Id) is
4013 begin
4014 if not Is_Trivial_Boolean (Expr) then
4015 Check_Function_Result (Expr);
4016 Check_Conjuncts (Expr);
4017 end if;
4018 end Check_Expression;
4019
4020 ------------------------
4021 -- Is_Function_Result --
4022 ------------------------
4023
4024 function Is_Function_Result (N : Node_Id) return Traverse_Result is
4025 begin
4026 if Is_Attribute_Result (N) then
4027 Result_Seen := True;
4028 return Abandon;
4029
4030 -- Warn on infinite recursion if call is to current function
4031
4032 elsif Nkind (N) = N_Function_Call
4033 and then Is_Entity_Name (Name (N))
4034 and then Entity (Name (N)) = Subp_Id
4035 and then not Is_Potentially_Unevaluated (N)
4036 then
4037 Error_Msg_NE
4038 ("call to & within its postcondition will lead to infinite "
4039 & "recursion?", N, Subp_Id);
4040 return OK;
4041
4042 -- Continue the traversal
4043
4044 else
4045 return OK;
4046 end if;
4047 end Is_Function_Result;
4048
4049 ------------------------
4050 -- Is_Trivial_Boolean --
4051 ------------------------
4052
4053 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
4054 begin
4055 return
4056 Comes_From_Source (N)
4057 and then Is_Entity_Name (N)
4058 and then (Entity (N) = Standard_True
4059 or else
4060 Entity (N) = Standard_False);
4061 end Is_Trivial_Boolean;
4062
4063 -------------------------
4064 -- Mentions_Post_State --
4065 -------------------------
4066
4067 function Mentions_Post_State (N : Node_Id) return Boolean is
4068 Post_State_Seen : Boolean := False;
4069
4070 function Is_Post_State (N : Node_Id) return Traverse_Result;
4071 -- Attempt to find a construct that denotes a post-state. If this
4072 -- is the case, set flag Post_State_Seen.
4073
4074 -------------------
4075 -- Is_Post_State --
4076 -------------------
4077
4078 function Is_Post_State (N : Node_Id) return Traverse_Result is
4079 Ent : Entity_Id;
4080
4081 begin
4082 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
4083 Post_State_Seen := True;
4084 return Abandon;
4085
4086 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
4087 Ent := Entity (N);
4088
4089 -- Treat an undecorated reference as OK
4090
4091 if No (Ent)
4092
4093 -- A reference to an assignable entity is considered a
4094 -- change in the post-state of a subprogram.
4095
4096 or else Ekind_In (Ent, E_Generic_In_Out_Parameter,
4097 E_In_Out_Parameter,
4098 E_Out_Parameter,
4099 E_Variable)
4100
4101 -- The reference may be modified through a dereference
4102
4103 or else (Is_Access_Type (Etype (Ent))
4104 and then Nkind (Parent (N)) =
4105 N_Selected_Component)
4106 then
4107 Post_State_Seen := True;
4108 return Abandon;
4109 end if;
4110
4111 elsif Nkind (N) = N_Attribute_Reference then
4112 if Attribute_Name (N) = Name_Old then
4113 return Skip;
4114
4115 elsif Attribute_Name (N) = Name_Result then
4116 Post_State_Seen := True;
4117 return Abandon;
4118 end if;
4119 end if;
4120
4121 return OK;
4122 end Is_Post_State;
4123
4124 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
4125
4126 -- Start of processing for Mentions_Post_State
4127
4128 begin
4129 Find_Post_State (N);
4130
4131 return Post_State_Seen;
4132 end Mentions_Post_State;
4133
4134 -- Local variables
4135
4136 Expr : constant Node_Id :=
4137 Get_Pragma_Arg
4138 (First (Pragma_Argument_Associations (Prag)));
4139 Nam : constant Name_Id := Pragma_Name (Prag);
4140 CCase : Node_Id;
4141
4142 -- Start of processing for Check_Result_And_Post_State_In_Pragma
4143
4144 begin
4145 -- Examine all consequences
4146
4147 if Nam = Name_Contract_Cases then
4148 CCase := First (Component_Associations (Expr));
4149 while Present (CCase) loop
4150 Check_Expression (Expression (CCase));
4151
4152 Next (CCase);
4153 end loop;
4154
4155 -- Examine the expression of a postcondition
4156
4157 else pragma Assert (Nam_In (Nam, Name_Postcondition,
4158 Name_Refined_Post));
4159 Check_Expression (Expr);
4160 end if;
4161 end Check_Result_And_Post_State_In_Pragma;
4162
4163 --------------------------
4164 -- Has_In_Out_Parameter --
4165 --------------------------
4166
4167 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
4168 Formal : Entity_Id;
4169
4170 begin
4171 -- Traverse the formals looking for an IN OUT parameter
4172
4173 Formal := First_Formal (Subp_Id);
4174 while Present (Formal) loop
4175 if Ekind (Formal) = E_In_Out_Parameter then
4176 return True;
4177 end if;
4178
4179 Next_Formal (Formal);
4180 end loop;
4181
4182 return False;
4183 end Has_In_Out_Parameter;
4184
4185 -- Local variables
4186
4187 Items : constant Node_Id := Contract (Subp_Id);
4188 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
4189 Case_Prag : Node_Id := Empty;
4190 Post_Prag : Node_Id := Empty;
4191 Prag : Node_Id;
4192 Seen_In_Case : Boolean := False;
4193 Seen_In_Post : Boolean := False;
4194 Spec_Id : Entity_Id;
4195
4196 -- Start of processing for Check_Result_And_Post_State
4197
4198 begin
4199 -- The lack of attribute 'Result or a post-state is classified as a
4200 -- suspicious contract. Do not perform the check if the corresponding
4201 -- swich is not set.
4202
4203 if not Warn_On_Suspicious_Contract then
4204 return;
4205
4206 -- Nothing to do if there is no contract
4207
4208 elsif No (Items) then
4209 return;
4210 end if;
4211
4212 -- Retrieve the entity of the subprogram spec (if any)
4213
4214 if Nkind (Subp_Decl) = N_Subprogram_Body
4215 and then Present (Corresponding_Spec (Subp_Decl))
4216 then
4217 Spec_Id := Corresponding_Spec (Subp_Decl);
4218
4219 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
4220 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
4221 then
4222 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
4223
4224 else
4225 Spec_Id := Subp_Id;
4226 end if;
4227
4228 -- Examine all postconditions for attribute 'Result and a post-state
4229
4230 Prag := Pre_Post_Conditions (Items);
4231 while Present (Prag) loop
4232 if Nam_In (Pragma_Name_Unmapped (Prag),
4233 Name_Postcondition, Name_Refined_Post)
4234 and then not Error_Posted (Prag)
4235 then
4236 Post_Prag := Prag;
4237 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
4238 end if;
4239
4240 Prag := Next_Pragma (Prag);
4241 end loop;
4242
4243 -- Examine the contract cases of the subprogram for attribute 'Result
4244 -- and a post-state.
4245
4246 Prag := Contract_Test_Cases (Items);
4247 while Present (Prag) loop
4248 if Pragma_Name (Prag) = Name_Contract_Cases
4249 and then not Error_Posted (Prag)
4250 then
4251 Case_Prag := Prag;
4252 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
4253 end if;
4254
4255 Prag := Next_Pragma (Prag);
4256 end loop;
4257
4258 -- Do not emit any errors if the subprogram is not a function
4259
4260 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
4261 null;
4262
4263 -- Regardless of whether the function has postconditions or contract
4264 -- cases, or whether they mention attribute 'Result, an IN OUT formal
4265 -- parameter is always treated as a result.
4266
4267 elsif Has_In_Out_Parameter (Spec_Id) then
4268 null;
4269
4270 -- The function has both a postcondition and contract cases and they do
4271 -- not mention attribute 'Result.
4272
4273 elsif Present (Case_Prag)
4274 and then not Seen_In_Case
4275 and then Present (Post_Prag)
4276 and then not Seen_In_Post
4277 then
4278 Error_Msg_N
4279 ("neither postcondition nor contract cases mention function "
4280 & "result?T?", Post_Prag);
4281
4282 -- The function has contract cases only and they do not mention
4283 -- attribute 'Result.
4284
4285 elsif Present (Case_Prag) and then not Seen_In_Case then
4286 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
4287
4288 -- The function has postconditions only and they do not mention
4289 -- attribute 'Result.
4290
4291 elsif Present (Post_Prag) and then not Seen_In_Post then
4292 Error_Msg_N
4293 ("postcondition does not mention function result?T?", Post_Prag);
4294 end if;
4295 end Check_Result_And_Post_State;
4296
4297 -----------------------------
4298 -- Check_State_Refinements --
4299 -----------------------------
4300
4301 procedure Check_State_Refinements
4302 (Context : Node_Id;
4303 Is_Main_Unit : Boolean := False)
4304 is
4305 procedure Check_Package (Pack : Node_Id);
4306 -- Verify that all abstract states of a [generic] package denoted by its
4307 -- declarative node Pack have proper refinement. Recursively verify the
4308 -- visible and private declarations of the [generic] package for other
4309 -- nested packages.
4310
4311 procedure Check_Packages_In (Decls : List_Id);
4312 -- Seek out [generic] package declarations within declarative list Decls
4313 -- and verify the status of their abstract state refinement.
4314
4315 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean;
4316 -- Determine whether construct N is subject to pragma SPARK_Mode Off
4317
4318 -------------------
4319 -- Check_Package --
4320 -------------------
4321
4322 procedure Check_Package (Pack : Node_Id) is
4323 Body_Id : constant Entity_Id := Corresponding_Body (Pack);
4324 Spec : constant Node_Id := Specification (Pack);
4325 States : constant Elist_Id :=
4326 Abstract_States (Defining_Entity (Pack));
4327
4328 State_Elmt : Elmt_Id;
4329 State_Id : Entity_Id;
4330
4331 begin
4332 -- Do not verify proper state refinement when the package is subject
4333 -- to pragma SPARK_Mode Off because this disables the requirement for
4334 -- state refinement.
4335
4336 if SPARK_Mode_Is_Off (Pack) then
4337 null;
4338
4339 -- State refinement can only occur in a completing package body. Do
4340 -- not verify proper state refinement when the body is subject to
4341 -- pragma SPARK_Mode Off because this disables the requirement for
4342 -- state refinement.
4343
4344 elsif Present (Body_Id)
4345 and then SPARK_Mode_Is_Off (Unit_Declaration_Node (Body_Id))
4346 then
4347 null;
4348
4349 -- Do not verify proper state refinement when the package is an
4350 -- instance as this check was already performed in the generic.
4351
4352 elsif Present (Generic_Parent (Spec)) then
4353 null;
4354
4355 -- Otherwise examine the contents of the package
4356
4357 else
4358 if Present (States) then
4359 State_Elmt := First_Elmt (States);
4360 while Present (State_Elmt) loop
4361 State_Id := Node (State_Elmt);
4362
4363 -- Emit an error when a non-null state lacks any form of
4364 -- refinement.
4365
4366 if not Is_Null_State (State_Id)
4367 and then not Has_Null_Refinement (State_Id)
4368 and then not Has_Non_Null_Refinement (State_Id)
4369 then
4370 Error_Msg_N ("state & requires refinement", State_Id);
4371 end if;
4372
4373 Next_Elmt (State_Elmt);
4374 end loop;
4375 end if;
4376
4377 Check_Packages_In (Visible_Declarations (Spec));
4378 Check_Packages_In (Private_Declarations (Spec));
4379 end if;
4380 end Check_Package;
4381
4382 -----------------------
4383 -- Check_Packages_In --
4384 -----------------------
4385
4386 procedure Check_Packages_In (Decls : List_Id) is
4387 Decl : Node_Id;
4388
4389 begin
4390 if Present (Decls) then
4391 Decl := First (Decls);
4392 while Present (Decl) loop
4393 if Nkind_In (Decl, N_Generic_Package_Declaration,
4394 N_Package_Declaration)
4395 then
4396 Check_Package (Decl);
4397 end if;
4398
4399 Next (Decl);
4400 end loop;
4401 end if;
4402 end Check_Packages_In;
4403
4404 -----------------------
4405 -- SPARK_Mode_Is_Off --
4406 -----------------------
4407
4408 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean is
4409 Id : constant Entity_Id := Defining_Entity (N);
4410 Prag : constant Node_Id := SPARK_Pragma (Id);
4411
4412 begin
4413 -- Default the mode to "off" when the context is an instance and all
4414 -- SPARK_Mode pragmas found within are to be ignored.
4415
4416 if Ignore_SPARK_Mode_Pragmas (Id) then
4417 return True;
4418
4419 else
4420 return
4421 Present (Prag)
4422 and then Get_SPARK_Mode_From_Annotation (Prag) = Off;
4423 end if;
4424 end SPARK_Mode_Is_Off;
4425
4426 -- Start of processing for Check_State_Refinements
4427
4428 begin
4429 -- A block may declare a nested package
4430
4431 if Nkind (Context) = N_Block_Statement then
4432 Check_Packages_In (Declarations (Context));
4433
4434 -- An entry, protected, subprogram, or task body may declare a nested
4435 -- package.
4436
4437 elsif Nkind_In (Context, N_Entry_Body,
4438 N_Protected_Body,
4439 N_Subprogram_Body,
4440 N_Task_Body)
4441 then
4442 -- Do not verify proper state refinement when the body is subject to
4443 -- pragma SPARK_Mode Off because this disables the requirement for
4444 -- state refinement.
4445
4446 if not SPARK_Mode_Is_Off (Context) then
4447 Check_Packages_In (Declarations (Context));
4448 end if;
4449
4450 -- A package body may declare a nested package
4451
4452 elsif Nkind (Context) = N_Package_Body then
4453 Check_Package (Unit_Declaration_Node (Corresponding_Spec (Context)));
4454
4455 -- Do not verify proper state refinement when the body is subject to
4456 -- pragma SPARK_Mode Off because this disables the requirement for
4457 -- state refinement.
4458
4459 if not SPARK_Mode_Is_Off (Context) then
4460 Check_Packages_In (Declarations (Context));
4461 end if;
4462
4463 -- A library level [generic] package may declare a nested package
4464
4465 elsif Nkind_In (Context, N_Generic_Package_Declaration,
4466 N_Package_Declaration)
4467 and then Is_Main_Unit
4468 then
4469 Check_Package (Context);
4470 end if;
4471 end Check_State_Refinements;
4472
4473 ------------------------------
4474 -- Check_Unprotected_Access --
4475 ------------------------------
4476
4477 procedure Check_Unprotected_Access
4478 (Context : Node_Id;
4479 Expr : Node_Id)
4480 is
4481 Cont_Encl_Typ : Entity_Id;
4482 Pref_Encl_Typ : Entity_Id;
4483
4484 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
4485 -- Check whether Obj is a private component of a protected object.
4486 -- Return the protected type where the component resides, Empty
4487 -- otherwise.
4488
4489 function Is_Public_Operation return Boolean;
4490 -- Verify that the enclosing operation is callable from outside the
4491 -- protected object, to minimize false positives.
4492
4493 ------------------------------
4494 -- Enclosing_Protected_Type --
4495 ------------------------------
4496
4497 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
4498 begin
4499 if Is_Entity_Name (Obj) then
4500 declare
4501 Ent : Entity_Id := Entity (Obj);
4502
4503 begin
4504 -- The object can be a renaming of a private component, use
4505 -- the original record component.
4506
4507 if Is_Prival (Ent) then
4508 Ent := Prival_Link (Ent);
4509 end if;
4510
4511 if Is_Protected_Type (Scope (Ent)) then
4512 return Scope (Ent);
4513 end if;
4514 end;
4515 end if;
4516
4517 -- For indexed and selected components, recursively check the prefix
4518
4519 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
4520 return Enclosing_Protected_Type (Prefix (Obj));
4521
4522 -- The object does not denote a protected component
4523
4524 else
4525 return Empty;
4526 end if;
4527 end Enclosing_Protected_Type;
4528
4529 -------------------------
4530 -- Is_Public_Operation --
4531 -------------------------
4532
4533 function Is_Public_Operation return Boolean is
4534 S : Entity_Id;
4535 E : Entity_Id;
4536
4537 begin
4538 S := Current_Scope;
4539 while Present (S) and then S /= Pref_Encl_Typ loop
4540 if Scope (S) = Pref_Encl_Typ then
4541 E := First_Entity (Pref_Encl_Typ);
4542 while Present (E)
4543 and then E /= First_Private_Entity (Pref_Encl_Typ)
4544 loop
4545 if E = S then
4546 return True;
4547 end if;
4548
4549 Next_Entity (E);
4550 end loop;
4551 end if;
4552
4553 S := Scope (S);
4554 end loop;
4555
4556 return False;
4557 end Is_Public_Operation;
4558
4559 -- Start of processing for Check_Unprotected_Access
4560
4561 begin
4562 if Nkind (Expr) = N_Attribute_Reference
4563 and then Attribute_Name (Expr) = Name_Unchecked_Access
4564 then
4565 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
4566 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
4567
4568 -- Check whether we are trying to export a protected component to a
4569 -- context with an equal or lower access level.
4570
4571 if Present (Pref_Encl_Typ)
4572 and then No (Cont_Encl_Typ)
4573 and then Is_Public_Operation
4574 and then Scope_Depth (Pref_Encl_Typ) >=
4575 Object_Access_Level (Context)
4576 then
4577 Error_Msg_N
4578 ("??possible unprotected access to protected data", Expr);
4579 end if;
4580 end if;
4581 end Check_Unprotected_Access;
4582
4583 ------------------------------
4584 -- Check_Unused_Body_States --
4585 ------------------------------
4586
4587 procedure Check_Unused_Body_States (Body_Id : Entity_Id) is
4588 procedure Process_Refinement_Clause
4589 (Clause : Node_Id;
4590 States : Elist_Id);
4591 -- Inspect all constituents of refinement clause Clause and remove any
4592 -- matches from body state list States.
4593
4594 procedure Report_Unused_Body_States (States : Elist_Id);
4595 -- Emit errors for each abstract state or object found in list States
4596
4597 -------------------------------
4598 -- Process_Refinement_Clause --
4599 -------------------------------
4600
4601 procedure Process_Refinement_Clause
4602 (Clause : Node_Id;
4603 States : Elist_Id)
4604 is
4605 procedure Process_Constituent (Constit : Node_Id);
4606 -- Remove constituent Constit from body state list States
4607
4608 -------------------------
4609 -- Process_Constituent --
4610 -------------------------
4611
4612 procedure Process_Constituent (Constit : Node_Id) is
4613 Constit_Id : Entity_Id;
4614
4615 begin
4616 -- Guard against illegal constituents. Only abstract states and
4617 -- objects can appear on the right hand side of a refinement.
4618
4619 if Is_Entity_Name (Constit) then
4620 Constit_Id := Entity_Of (Constit);
4621
4622 if Present (Constit_Id)
4623 and then Ekind_In (Constit_Id, E_Abstract_State,
4624 E_Constant,
4625 E_Variable)
4626 then
4627 Remove (States, Constit_Id);
4628 end if;
4629 end if;
4630 end Process_Constituent;
4631
4632 -- Local variables
4633
4634 Constit : Node_Id;
4635
4636 -- Start of processing for Process_Refinement_Clause
4637
4638 begin
4639 if Nkind (Clause) = N_Component_Association then
4640 Constit := Expression (Clause);
4641
4642 -- Multiple constituents appear as an aggregate
4643
4644 if Nkind (Constit) = N_Aggregate then
4645 Constit := First (Expressions (Constit));
4646 while Present (Constit) loop
4647 Process_Constituent (Constit);
4648 Next (Constit);
4649 end loop;
4650
4651 -- Various forms of a single constituent
4652
4653 else
4654 Process_Constituent (Constit);
4655 end if;
4656 end if;
4657 end Process_Refinement_Clause;
4658
4659 -------------------------------
4660 -- Report_Unused_Body_States --
4661 -------------------------------
4662
4663 procedure Report_Unused_Body_States (States : Elist_Id) is
4664 Posted : Boolean := False;
4665 State_Elmt : Elmt_Id;
4666 State_Id : Entity_Id;
4667
4668 begin
4669 if Present (States) then
4670 State_Elmt := First_Elmt (States);
4671 while Present (State_Elmt) loop
4672 State_Id := Node (State_Elmt);
4673
4674 -- Constants are part of the hidden state of a package, but the
4675 -- compiler cannot determine whether they have variable input
4676 -- (SPARK RM 7.1.1(2)) and cannot classify them properly as a
4677 -- hidden state. Do not emit an error when a constant does not
4678 -- participate in a state refinement, even though it acts as a
4679 -- hidden state.
4680
4681 if Ekind (State_Id) = E_Constant then
4682 null;
4683
4684 -- Generate an error message of the form:
4685
4686 -- body of package ... has unused hidden states
4687 -- abstract state ... defined at ...
4688 -- variable ... defined at ...
4689
4690 else
4691 if not Posted then
4692 Posted := True;
4693 SPARK_Msg_N
4694 ("body of package & has unused hidden states", Body_Id);
4695 end if;
4696
4697 Error_Msg_Sloc := Sloc (State_Id);
4698
4699 if Ekind (State_Id) = E_Abstract_State then
4700 SPARK_Msg_NE
4701 ("\abstract state & defined #", Body_Id, State_Id);
4702
4703 else
4704 SPARK_Msg_NE ("\variable & defined #", Body_Id, State_Id);
4705 end if;
4706 end if;
4707
4708 Next_Elmt (State_Elmt);
4709 end loop;
4710 end if;
4711 end Report_Unused_Body_States;
4712
4713 -- Local variables
4714
4715 Prag : constant Node_Id := Get_Pragma (Body_Id, Pragma_Refined_State);
4716 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
4717 Clause : Node_Id;
4718 States : Elist_Id;
4719
4720 -- Start of processing for Check_Unused_Body_States
4721
4722 begin
4723 -- Inspect the clauses of pragma Refined_State and determine whether all
4724 -- visible states declared within the package body participate in the
4725 -- refinement.
4726
4727 if Present (Prag) then
4728 Clause := Expression (Get_Argument (Prag, Spec_Id));
4729 States := Collect_Body_States (Body_Id);
4730
4731 -- Multiple non-null state refinements appear as an aggregate
4732
4733 if Nkind (Clause) = N_Aggregate then
4734 Clause := First (Component_Associations (Clause));
4735 while Present (Clause) loop
4736 Process_Refinement_Clause (Clause, States);
4737 Next (Clause);
4738 end loop;
4739
4740 -- Various forms of a single state refinement
4741
4742 else
4743 Process_Refinement_Clause (Clause, States);
4744 end if;
4745
4746 -- Ensure that all abstract states and objects declared in the
4747 -- package body state space are utilized as constituents.
4748
4749 Report_Unused_Body_States (States);
4750 end if;
4751 end Check_Unused_Body_States;
4752
4753 -----------------
4754 -- Choice_List --
4755 -----------------
4756
4757 function Choice_List (N : Node_Id) return List_Id is
4758 begin
4759 if Nkind (N) = N_Iterated_Component_Association then
4760 return Discrete_Choices (N);
4761 else
4762 return Choices (N);
4763 end if;
4764 end Choice_List;
4765
4766 -------------------------
4767 -- Collect_Body_States --
4768 -------------------------
4769
4770 function Collect_Body_States (Body_Id : Entity_Id) return Elist_Id is
4771 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean;
4772 -- Determine whether object Obj_Id is a suitable visible state of a
4773 -- package body.
4774
4775 procedure Collect_Visible_States
4776 (Pack_Id : Entity_Id;
4777 States : in out Elist_Id);
4778 -- Gather the entities of all abstract states and objects declared in
4779 -- the visible state space of package Pack_Id.
4780
4781 ----------------------------
4782 -- Collect_Visible_States --
4783 ----------------------------
4784
4785 procedure Collect_Visible_States
4786 (Pack_Id : Entity_Id;
4787 States : in out Elist_Id)
4788 is
4789 Item_Id : Entity_Id;
4790
4791 begin
4792 -- Traverse the entity chain of the package and inspect all visible
4793 -- items.
4794
4795 Item_Id := First_Entity (Pack_Id);
4796 while Present (Item_Id) and then not In_Private_Part (Item_Id) loop
4797
4798 -- Do not consider internally generated items as those cannot be
4799 -- named and participate in refinement.
4800
4801 if not Comes_From_Source (Item_Id) then
4802 null;
4803
4804 elsif Ekind (Item_Id) = E_Abstract_State then
4805 Append_New_Elmt (Item_Id, States);
4806
4807 elsif Ekind_In (Item_Id, E_Constant, E_Variable)
4808 and then Is_Visible_Object (Item_Id)
4809 then
4810 Append_New_Elmt (Item_Id, States);
4811
4812 -- Recursively gather the visible states of a nested package
4813
4814 elsif Ekind (Item_Id) = E_Package then
4815 Collect_Visible_States (Item_Id, States);
4816 end if;
4817
4818 Next_Entity (Item_Id);
4819 end loop;
4820 end Collect_Visible_States;
4821
4822 -----------------------
4823 -- Is_Visible_Object --
4824 -----------------------
4825
4826 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean is
4827 begin
4828 -- Objects that map generic formals to their actuals are not visible
4829 -- from outside the generic instantiation.
4830
4831 if Present (Corresponding_Generic_Association
4832 (Declaration_Node (Obj_Id)))
4833 then
4834 return False;
4835
4836 -- Constituents of a single protected/task type act as components of
4837 -- the type and are not visible from outside the type.
4838
4839 elsif Ekind (Obj_Id) = E_Variable
4840 and then Present (Encapsulating_State (Obj_Id))
4841 and then Is_Single_Concurrent_Object (Encapsulating_State (Obj_Id))
4842 then
4843 return False;
4844
4845 else
4846 return True;
4847 end if;
4848 end Is_Visible_Object;
4849
4850 -- Local variables
4851
4852 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
4853 Decl : Node_Id;
4854 Item_Id : Entity_Id;
4855 States : Elist_Id := No_Elist;
4856
4857 -- Start of processing for Collect_Body_States
4858
4859 begin
4860 -- Inspect the declarations of the body looking for source objects,
4861 -- packages and package instantiations. Note that even though this
4862 -- processing is very similar to Collect_Visible_States, a package
4863 -- body does not have a First/Next_Entity list.
4864
4865 Decl := First (Declarations (Body_Decl));
4866 while Present (Decl) loop
4867
4868 -- Capture source objects as internally generated temporaries cannot
4869 -- be named and participate in refinement.
4870
4871 if Nkind (Decl) = N_Object_Declaration then
4872 Item_Id := Defining_Entity (Decl);
4873
4874 if Comes_From_Source (Item_Id)
4875 and then Is_Visible_Object (Item_Id)
4876 then
4877 Append_New_Elmt (Item_Id, States);
4878 end if;
4879
4880 -- Capture the visible abstract states and objects of a source
4881 -- package [instantiation].
4882
4883 elsif Nkind (Decl) = N_Package_Declaration then
4884 Item_Id := Defining_Entity (Decl);
4885
4886 if Comes_From_Source (Item_Id) then
4887 Collect_Visible_States (Item_Id, States);
4888 end if;
4889 end if;
4890
4891 Next (Decl);
4892 end loop;
4893
4894 return States;
4895 end Collect_Body_States;
4896
4897 ------------------------
4898 -- Collect_Interfaces --
4899 ------------------------
4900
4901 procedure Collect_Interfaces
4902 (T : Entity_Id;
4903 Ifaces_List : out Elist_Id;
4904 Exclude_Parents : Boolean := False;
4905 Use_Full_View : Boolean := True)
4906 is
4907 procedure Collect (Typ : Entity_Id);
4908 -- Subsidiary subprogram used to traverse the whole list
4909 -- of directly and indirectly implemented interfaces
4910
4911 -------------
4912 -- Collect --
4913 -------------
4914
4915 procedure Collect (Typ : Entity_Id) is
4916 Ancestor : Entity_Id;
4917 Full_T : Entity_Id;
4918 Id : Node_Id;
4919 Iface : Entity_Id;
4920
4921 begin
4922 Full_T := Typ;
4923
4924 -- Handle private types and subtypes
4925
4926 if Use_Full_View
4927 and then Is_Private_Type (Typ)
4928 and then Present (Full_View (Typ))
4929 then
4930 Full_T := Full_View (Typ);
4931
4932 if Ekind (Full_T) = E_Record_Subtype then
4933 Full_T := Etype (Typ);
4934
4935 if Present (Full_View (Full_T)) then
4936 Full_T := Full_View (Full_T);
4937 end if;
4938 end if;
4939 end if;
4940
4941 -- Include the ancestor if we are generating the whole list of
4942 -- abstract interfaces.
4943
4944 if Etype (Full_T) /= Typ
4945
4946 -- Protect the frontend against wrong sources. For example:
4947
4948 -- package P is
4949 -- type A is tagged null record;
4950 -- type B is new A with private;
4951 -- type C is new A with private;
4952 -- private
4953 -- type B is new C with null record;
4954 -- type C is new B with null record;
4955 -- end P;
4956
4957 and then Etype (Full_T) /= T
4958 then
4959 Ancestor := Etype (Full_T);
4960 Collect (Ancestor);
4961
4962 if Is_Interface (Ancestor) and then not Exclude_Parents then
4963 Append_Unique_Elmt (Ancestor, Ifaces_List);
4964 end if;
4965 end if;
4966
4967 -- Traverse the graph of ancestor interfaces
4968
4969 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
4970 Id := First (Abstract_Interface_List (Full_T));
4971 while Present (Id) loop
4972 Iface := Etype (Id);
4973
4974 -- Protect against wrong uses. For example:
4975 -- type I is interface;
4976 -- type O is tagged null record;
4977 -- type Wrong is new I and O with null record; -- ERROR
4978
4979 if Is_Interface (Iface) then
4980 if Exclude_Parents
4981 and then Etype (T) /= T
4982 and then Interface_Present_In_Ancestor (Etype (T), Iface)
4983 then
4984 null;
4985 else
4986 Collect (Iface);
4987 Append_Unique_Elmt (Iface, Ifaces_List);
4988 end if;
4989 end if;
4990
4991 Next (Id);
4992 end loop;
4993 end if;
4994 end Collect;
4995
4996 -- Start of processing for Collect_Interfaces
4997
4998 begin
4999 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
5000 Ifaces_List := New_Elmt_List;
5001 Collect (T);
5002 end Collect_Interfaces;
5003
5004 ----------------------------------
5005 -- Collect_Interface_Components --
5006 ----------------------------------
5007
5008 procedure Collect_Interface_Components
5009 (Tagged_Type : Entity_Id;
5010 Components_List : out Elist_Id)
5011 is
5012 procedure Collect (Typ : Entity_Id);
5013 -- Subsidiary subprogram used to climb to the parents
5014
5015 -------------
5016 -- Collect --
5017 -------------
5018
5019 procedure Collect (Typ : Entity_Id) is
5020 Tag_Comp : Entity_Id;
5021 Parent_Typ : Entity_Id;
5022
5023 begin
5024 -- Handle private types
5025
5026 if Present (Full_View (Etype (Typ))) then
5027 Parent_Typ := Full_View (Etype (Typ));
5028 else
5029 Parent_Typ := Etype (Typ);
5030 end if;
5031
5032 if Parent_Typ /= Typ
5033
5034 -- Protect the frontend against wrong sources. For example:
5035
5036 -- package P is
5037 -- type A is tagged null record;
5038 -- type B is new A with private;
5039 -- type C is new A with private;
5040 -- private
5041 -- type B is new C with null record;
5042 -- type C is new B with null record;
5043 -- end P;
5044
5045 and then Parent_Typ /= Tagged_Type
5046 then
5047 Collect (Parent_Typ);
5048 end if;
5049
5050 -- Collect the components containing tags of secondary dispatch
5051 -- tables.
5052
5053 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
5054 while Present (Tag_Comp) loop
5055 pragma Assert (Present (Related_Type (Tag_Comp)));
5056 Append_Elmt (Tag_Comp, Components_List);
5057
5058 Tag_Comp := Next_Tag_Component (Tag_Comp);
5059 end loop;
5060 end Collect;
5061
5062 -- Start of processing for Collect_Interface_Components
5063
5064 begin
5065 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
5066 and then Is_Tagged_Type (Tagged_Type));
5067
5068 Components_List := New_Elmt_List;
5069 Collect (Tagged_Type);
5070 end Collect_Interface_Components;
5071
5072 -----------------------------
5073 -- Collect_Interfaces_Info --
5074 -----------------------------
5075
5076 procedure Collect_Interfaces_Info
5077 (T : Entity_Id;
5078 Ifaces_List : out Elist_Id;
5079 Components_List : out Elist_Id;
5080 Tags_List : out Elist_Id)
5081 is
5082 Comps_List : Elist_Id;
5083 Comp_Elmt : Elmt_Id;
5084 Comp_Iface : Entity_Id;
5085 Iface_Elmt : Elmt_Id;
5086 Iface : Entity_Id;
5087
5088 function Search_Tag (Iface : Entity_Id) return Entity_Id;
5089 -- Search for the secondary tag associated with the interface type
5090 -- Iface that is implemented by T.
5091
5092 ----------------
5093 -- Search_Tag --
5094 ----------------
5095
5096 function Search_Tag (Iface : Entity_Id) return Entity_Id is
5097 ADT : Elmt_Id;
5098 begin
5099 if not Is_CPP_Class (T) then
5100 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
5101 else
5102 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
5103 end if;
5104
5105 while Present (ADT)
5106 and then Is_Tag (Node (ADT))
5107 and then Related_Type (Node (ADT)) /= Iface
5108 loop
5109 -- Skip secondary dispatch table referencing thunks to user
5110 -- defined primitives covered by this interface.
5111
5112 pragma Assert (Has_Suffix (Node (ADT), 'P'));
5113 Next_Elmt (ADT);
5114
5115 -- Skip secondary dispatch tables of Ada types
5116
5117 if not Is_CPP_Class (T) then
5118
5119 -- Skip secondary dispatch table referencing thunks to
5120 -- predefined primitives.
5121
5122 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
5123 Next_Elmt (ADT);
5124
5125 -- Skip secondary dispatch table referencing user-defined
5126 -- primitives covered by this interface.
5127
5128 pragma Assert (Has_Suffix (Node (ADT), 'D'));
5129 Next_Elmt (ADT);
5130
5131 -- Skip secondary dispatch table referencing predefined
5132 -- primitives.
5133
5134 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
5135 Next_Elmt (ADT);
5136 end if;
5137 end loop;
5138
5139 pragma Assert (Is_Tag (Node (ADT)));
5140 return Node (ADT);
5141 end Search_Tag;
5142
5143 -- Start of processing for Collect_Interfaces_Info
5144
5145 begin
5146 Collect_Interfaces (T, Ifaces_List);
5147 Collect_Interface_Components (T, Comps_List);
5148
5149 -- Search for the record component and tag associated with each
5150 -- interface type of T.
5151
5152 Components_List := New_Elmt_List;
5153 Tags_List := New_Elmt_List;
5154
5155 Iface_Elmt := First_Elmt (Ifaces_List);
5156 while Present (Iface_Elmt) loop
5157 Iface := Node (Iface_Elmt);
5158
5159 -- Associate the primary tag component and the primary dispatch table
5160 -- with all the interfaces that are parents of T
5161
5162 if Is_Ancestor (Iface, T, Use_Full_View => True) then
5163 Append_Elmt (First_Tag_Component (T), Components_List);
5164 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
5165
5166 -- Otherwise search for the tag component and secondary dispatch
5167 -- table of Iface
5168
5169 else
5170 Comp_Elmt := First_Elmt (Comps_List);
5171 while Present (Comp_Elmt) loop
5172 Comp_Iface := Related_Type (Node (Comp_Elmt));
5173
5174 if Comp_Iface = Iface
5175 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
5176 then
5177 Append_Elmt (Node (Comp_Elmt), Components_List);
5178 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
5179 exit;
5180 end if;
5181
5182 Next_Elmt (Comp_Elmt);
5183 end loop;
5184 pragma Assert (Present (Comp_Elmt));
5185 end if;
5186
5187 Next_Elmt (Iface_Elmt);
5188 end loop;
5189 end Collect_Interfaces_Info;
5190
5191 ---------------------
5192 -- Collect_Parents --
5193 ---------------------
5194
5195 procedure Collect_Parents
5196 (T : Entity_Id;
5197 List : out Elist_Id;
5198 Use_Full_View : Boolean := True)
5199 is
5200 Current_Typ : Entity_Id := T;
5201 Parent_Typ : Entity_Id;
5202
5203 begin
5204 List := New_Elmt_List;
5205
5206 -- No action if the if the type has no parents
5207
5208 if T = Etype (T) then
5209 return;
5210 end if;
5211
5212 loop
5213 Parent_Typ := Etype (Current_Typ);
5214
5215 if Is_Private_Type (Parent_Typ)
5216 and then Present (Full_View (Parent_Typ))
5217 and then Use_Full_View
5218 then
5219 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5220 end if;
5221
5222 Append_Elmt (Parent_Typ, List);
5223
5224 exit when Parent_Typ = Current_Typ;
5225 Current_Typ := Parent_Typ;
5226 end loop;
5227 end Collect_Parents;
5228
5229 ----------------------------------
5230 -- Collect_Primitive_Operations --
5231 ----------------------------------
5232
5233 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
5234 B_Type : constant Entity_Id := Base_Type (T);
5235
5236 function Match (E : Entity_Id) return Boolean;
5237 -- True if E's base type is B_Type, or E is of an anonymous access type
5238 -- and the base type of its designated type is B_Type.
5239
5240 -----------
5241 -- Match --
5242 -----------
5243
5244 function Match (E : Entity_Id) return Boolean is
5245 Etyp : Entity_Id := Etype (E);
5246
5247 begin
5248 if Ekind (Etyp) = E_Anonymous_Access_Type then
5249 Etyp := Designated_Type (Etyp);
5250 end if;
5251
5252 -- In Ada 2012 a primitive operation may have a formal of an
5253 -- incomplete view of the parent type.
5254
5255 return Base_Type (Etyp) = B_Type
5256 or else
5257 (Ada_Version >= Ada_2012
5258 and then Ekind (Etyp) = E_Incomplete_Type
5259 and then Full_View (Etyp) = B_Type);
5260 end Match;
5261
5262 -- Local variables
5263
5264 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
5265 B_Scope : Entity_Id := Scope (B_Type);
5266 Op_List : Elist_Id;
5267 Eq_Prims_List : Elist_Id := No_Elist;
5268 Formal : Entity_Id;
5269 Is_Prim : Boolean;
5270 Is_Type_In_Pkg : Boolean;
5271 Formal_Derived : Boolean := False;
5272 Id : Entity_Id;
5273
5274 -- Start of processing for Collect_Primitive_Operations
5275
5276 begin
5277 -- For tagged types, the primitive operations are collected as they
5278 -- are declared, and held in an explicit list which is simply returned.
5279
5280 if Is_Tagged_Type (B_Type) then
5281 return Primitive_Operations (B_Type);
5282
5283 -- An untagged generic type that is a derived type inherits the
5284 -- primitive operations of its parent type. Other formal types only
5285 -- have predefined operators, which are not explicitly represented.
5286
5287 elsif Is_Generic_Type (B_Type) then
5288 if Nkind (B_Decl) = N_Formal_Type_Declaration
5289 and then Nkind (Formal_Type_Definition (B_Decl)) =
5290 N_Formal_Derived_Type_Definition
5291 then
5292 Formal_Derived := True;
5293 else
5294 return New_Elmt_List;
5295 end if;
5296 end if;
5297
5298 Op_List := New_Elmt_List;
5299
5300 if B_Scope = Standard_Standard then
5301 if B_Type = Standard_String then
5302 Append_Elmt (Standard_Op_Concat, Op_List);
5303
5304 elsif B_Type = Standard_Wide_String then
5305 Append_Elmt (Standard_Op_Concatw, Op_List);
5306
5307 else
5308 null;
5309 end if;
5310
5311 -- Locate the primitive subprograms of the type
5312
5313 else
5314 -- The primitive operations appear after the base type, except if the
5315 -- derivation happens within the private part of B_Scope and the type
5316 -- is a private type, in which case both the type and some primitive
5317 -- operations may appear before the base type, and the list of
5318 -- candidates starts after the type.
5319
5320 if In_Open_Scopes (B_Scope)
5321 and then Scope (T) = B_Scope
5322 and then In_Private_Part (B_Scope)
5323 then
5324 Id := Next_Entity (T);
5325
5326 -- In Ada 2012, If the type has an incomplete partial view, there may
5327 -- be primitive operations declared before the full view, so we need
5328 -- to start scanning from the incomplete view, which is earlier on
5329 -- the entity chain.
5330
5331 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
5332 and then Present (Incomplete_View (Parent (B_Type)))
5333 then
5334 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
5335
5336 -- If T is a derived from a type with an incomplete view declared
5337 -- elsewhere, that incomplete view is irrelevant, we want the
5338 -- operations in the scope of T.
5339
5340 if Scope (Id) /= Scope (B_Type) then
5341 Id := Next_Entity (B_Type);
5342 end if;
5343
5344 else
5345 Id := Next_Entity (B_Type);
5346 end if;
5347
5348 -- Set flag if this is a type in a package spec
5349
5350 Is_Type_In_Pkg :=
5351 Is_Package_Or_Generic_Package (B_Scope)
5352 and then
5353 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
5354 N_Package_Body;
5355
5356 while Present (Id) loop
5357
5358 -- Test whether the result type or any of the parameter types of
5359 -- each subprogram following the type match that type when the
5360 -- type is declared in a package spec, is a derived type, or the
5361 -- subprogram is marked as primitive. (The Is_Primitive test is
5362 -- needed to find primitives of nonderived types in declarative
5363 -- parts that happen to override the predefined "=" operator.)
5364
5365 -- Note that generic formal subprograms are not considered to be
5366 -- primitive operations and thus are never inherited.
5367
5368 if Is_Overloadable (Id)
5369 and then (Is_Type_In_Pkg
5370 or else Is_Derived_Type (B_Type)
5371 or else Is_Primitive (Id))
5372 and then Nkind (Parent (Parent (Id)))
5373 not in N_Formal_Subprogram_Declaration
5374 then
5375 Is_Prim := False;
5376
5377 if Match (Id) then
5378 Is_Prim := True;
5379
5380 else
5381 Formal := First_Formal (Id);
5382 while Present (Formal) loop
5383 if Match (Formal) then
5384 Is_Prim := True;
5385 exit;
5386 end if;
5387
5388 Next_Formal (Formal);
5389 end loop;
5390 end if;
5391
5392 -- For a formal derived type, the only primitives are the ones
5393 -- inherited from the parent type. Operations appearing in the
5394 -- package declaration are not primitive for it.
5395
5396 if Is_Prim
5397 and then (not Formal_Derived or else Present (Alias (Id)))
5398 then
5399 -- In the special case of an equality operator aliased to
5400 -- an overriding dispatching equality belonging to the same
5401 -- type, we don't include it in the list of primitives.
5402 -- This avoids inheriting multiple equality operators when
5403 -- deriving from untagged private types whose full type is
5404 -- tagged, which can otherwise cause ambiguities. Note that
5405 -- this should only happen for this kind of untagged parent
5406 -- type, since normally dispatching operations are inherited
5407 -- using the type's Primitive_Operations list.
5408
5409 if Chars (Id) = Name_Op_Eq
5410 and then Is_Dispatching_Operation (Id)
5411 and then Present (Alias (Id))
5412 and then Present (Overridden_Operation (Alias (Id)))
5413 and then Base_Type (Etype (First_Entity (Id))) =
5414 Base_Type (Etype (First_Entity (Alias (Id))))
5415 then
5416 null;
5417
5418 -- Include the subprogram in the list of primitives
5419
5420 else
5421 Append_Elmt (Id, Op_List);
5422
5423 -- Save collected equality primitives for later filtering
5424 -- (if we are processing a private type for which we can
5425 -- collect several candidates).
5426
5427 if Inherits_From_Tagged_Full_View (T)
5428 and then Chars (Id) = Name_Op_Eq
5429 and then Etype (First_Formal (Id)) =
5430 Etype (Next_Formal (First_Formal (Id)))
5431 then
5432 if No (Eq_Prims_List) then
5433 Eq_Prims_List := New_Elmt_List;
5434 end if;
5435
5436 Append_Elmt (Id, Eq_Prims_List);
5437 end if;
5438 end if;
5439 end if;
5440 end if;
5441
5442 Next_Entity (Id);
5443
5444 -- For a type declared in System, some of its operations may
5445 -- appear in the target-specific extension to System.
5446
5447 if No (Id)
5448 and then B_Scope = RTU_Entity (System)
5449 and then Present_System_Aux
5450 then
5451 B_Scope := System_Aux_Id;
5452 Id := First_Entity (System_Aux_Id);
5453 end if;
5454 end loop;
5455
5456 -- Filter collected equality primitives
5457
5458 if Inherits_From_Tagged_Full_View (T)
5459 and then Present (Eq_Prims_List)
5460 then
5461 declare
5462 First : constant Elmt_Id := First_Elmt (Eq_Prims_List);
5463 Second : Elmt_Id;
5464
5465 begin
5466 pragma Assert (No (Next_Elmt (First))
5467 or else No (Next_Elmt (Next_Elmt (First))));
5468
5469 -- No action needed if we have collected a single equality
5470 -- primitive
5471
5472 if Present (Next_Elmt (First)) then
5473 Second := Next_Elmt (First);
5474
5475 if Is_Dispatching_Operation
5476 (Ultimate_Alias (Node (First)))
5477 then
5478 Remove (Op_List, Node (First));
5479
5480 elsif Is_Dispatching_Operation
5481 (Ultimate_Alias (Node (Second)))
5482 then
5483 Remove (Op_List, Node (Second));
5484
5485 else
5486 pragma Assert (False);
5487 raise Program_Error;
5488 end if;
5489 end if;
5490 end;
5491 end if;
5492 end if;
5493
5494 return Op_List;
5495 end Collect_Primitive_Operations;
5496
5497 -----------------------------------
5498 -- Compile_Time_Constraint_Error --
5499 -----------------------------------
5500
5501 function Compile_Time_Constraint_Error
5502 (N : Node_Id;
5503 Msg : String;
5504 Ent : Entity_Id := Empty;
5505 Loc : Source_Ptr := No_Location;
5506 Warn : Boolean := False;
5507 Extra_Msg : String := "") return Node_Id
5508 is
5509 Msgc : String (1 .. Msg'Length + 3);
5510 -- Copy of message, with room for possible ?? or << and ! at end
5511
5512 Msgl : Natural;
5513 Wmsg : Boolean;
5514 Eloc : Source_Ptr;
5515
5516 -- Start of processing for Compile_Time_Constraint_Error
5517
5518 begin
5519 -- If this is a warning, convert it into an error if we are in code
5520 -- subject to SPARK_Mode being set On, unless Warn is True to force a
5521 -- warning. The rationale is that a compile-time constraint error should
5522 -- lead to an error instead of a warning when SPARK_Mode is On, but in
5523 -- a few cases we prefer to issue a warning and generate both a suitable
5524 -- run-time error in GNAT and a suitable check message in GNATprove.
5525 -- Those cases are those that likely correspond to deactivated SPARK
5526 -- code, so that this kind of code can be compiled and analyzed instead
5527 -- of being rejected.
5528
5529 Error_Msg_Warn := Warn or SPARK_Mode /= On;
5530
5531 -- A static constraint error in an instance body is not a fatal error.
5532 -- we choose to inhibit the message altogether, because there is no
5533 -- obvious node (for now) on which to post it. On the other hand the
5534 -- offending node must be replaced with a constraint_error in any case.
5535
5536 -- No messages are generated if we already posted an error on this node
5537
5538 if not Error_Posted (N) then
5539 if Loc /= No_Location then
5540 Eloc := Loc;
5541 else
5542 Eloc := Sloc (N);
5543 end if;
5544
5545 -- Copy message to Msgc, converting any ? in the message into <
5546 -- instead, so that we have an error in GNATprove mode.
5547
5548 Msgl := Msg'Length;
5549
5550 for J in 1 .. Msgl loop
5551 if Msg (J) = '?' and then (J = 1 or else Msg (J - 1) /= ''') then
5552 Msgc (J) := '<';
5553 else
5554 Msgc (J) := Msg (J);
5555 end if;
5556 end loop;
5557
5558 -- Message is a warning, even in Ada 95 case
5559
5560 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
5561 Wmsg := True;
5562
5563 -- In Ada 83, all messages are warnings. In the private part and the
5564 -- body of an instance, constraint_checks are only warnings. We also
5565 -- make this a warning if the Warn parameter is set.
5566
5567 elsif Warn
5568 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
5569 or else In_Instance_Not_Visible
5570 then
5571 Msgl := Msgl + 1;
5572 Msgc (Msgl) := '<';
5573 Msgl := Msgl + 1;
5574 Msgc (Msgl) := '<';
5575 Wmsg := True;
5576
5577 -- Otherwise we have a real error message (Ada 95 static case) and we
5578 -- make this an unconditional message. Note that in the warning case
5579 -- we do not make the message unconditional, it seems reasonable to
5580 -- delete messages like this (about exceptions that will be raised)
5581 -- in dead code.
5582
5583 else
5584 Wmsg := False;
5585 Msgl := Msgl + 1;
5586 Msgc (Msgl) := '!';
5587 end if;
5588
5589 -- One more test, skip the warning if the related expression is
5590 -- statically unevaluated, since we don't want to warn about what
5591 -- will happen when something is evaluated if it never will be
5592 -- evaluated.
5593
5594 if not Is_Statically_Unevaluated (N) then
5595 if Present (Ent) then
5596 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
5597 else
5598 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
5599 end if;
5600
5601 -- Emit any extra message as a continuation
5602
5603 if Extra_Msg /= "" then
5604 Error_Msg_N ('\' & Extra_Msg, N);
5605 end if;
5606
5607 if Wmsg then
5608
5609 -- Check whether the context is an Init_Proc
5610
5611 if Inside_Init_Proc then
5612 declare
5613 Conc_Typ : constant Entity_Id :=
5614 Corresponding_Concurrent_Type
5615 (Entity (Parameter_Type (First
5616 (Parameter_Specifications
5617 (Parent (Current_Scope))))));
5618
5619 begin
5620 -- Don't complain if the corresponding concurrent type
5621 -- doesn't come from source (i.e. a single task/protected
5622 -- object).
5623
5624 if Present (Conc_Typ)
5625 and then not Comes_From_Source (Conc_Typ)
5626 then
5627 Error_Msg_NEL
5628 ("\& [<<", N, Standard_Constraint_Error, Eloc);
5629
5630 else
5631 if GNATprove_Mode then
5632 Error_Msg_NEL
5633 ("\& would have been raised for objects of this "
5634 & "type", N, Standard_Constraint_Error, Eloc);
5635 else
5636 Error_Msg_NEL
5637 ("\& will be raised for objects of this type??",
5638 N, Standard_Constraint_Error, Eloc);
5639 end if;
5640 end if;
5641 end;
5642
5643 else
5644 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
5645 end if;
5646
5647 else
5648 Error_Msg ("\static expression fails Constraint_Check", Eloc);
5649 Set_Error_Posted (N);
5650 end if;
5651 end if;
5652 end if;
5653
5654 return N;
5655 end Compile_Time_Constraint_Error;
5656
5657 -----------------------
5658 -- Conditional_Delay --
5659 -----------------------
5660
5661 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
5662 begin
5663 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
5664 Set_Has_Delayed_Freeze (New_Ent);
5665 end if;
5666 end Conditional_Delay;
5667
5668 -------------------------
5669 -- Copy_Component_List --
5670 -------------------------
5671
5672 function Copy_Component_List
5673 (R_Typ : Entity_Id;
5674 Loc : Source_Ptr) return List_Id
5675 is
5676 Comp : Node_Id;
5677 Comps : constant List_Id := New_List;
5678
5679 begin
5680 Comp := First_Component (Underlying_Type (R_Typ));
5681 while Present (Comp) loop
5682 if Comes_From_Source (Comp) then
5683 declare
5684 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
5685 begin
5686 Append_To (Comps,
5687 Make_Component_Declaration (Loc,
5688 Defining_Identifier =>
5689 Make_Defining_Identifier (Loc, Chars (Comp)),
5690 Component_Definition =>
5691 New_Copy_Tree
5692 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
5693 end;
5694 end if;
5695
5696 Next_Component (Comp);
5697 end loop;
5698
5699 return Comps;
5700 end Copy_Component_List;
5701
5702 -------------------------
5703 -- Copy_Parameter_List --
5704 -------------------------
5705
5706 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
5707 Loc : constant Source_Ptr := Sloc (Subp_Id);
5708 Plist : List_Id;
5709 Formal : Entity_Id;
5710
5711 begin
5712 if No (First_Formal (Subp_Id)) then
5713 return No_List;
5714 else
5715 Plist := New_List;
5716 Formal := First_Formal (Subp_Id);
5717 while Present (Formal) loop
5718 Append_To (Plist,
5719 Make_Parameter_Specification (Loc,
5720 Defining_Identifier =>
5721 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
5722 In_Present => In_Present (Parent (Formal)),
5723 Out_Present => Out_Present (Parent (Formal)),
5724 Parameter_Type =>
5725 New_Occurrence_Of (Etype (Formal), Loc),
5726 Expression =>
5727 New_Copy_Tree (Expression (Parent (Formal)))));
5728
5729 Next_Formal (Formal);
5730 end loop;
5731 end if;
5732
5733 return Plist;
5734 end Copy_Parameter_List;
5735
5736 ----------------------------
5737 -- Copy_SPARK_Mode_Aspect --
5738 ----------------------------
5739
5740 procedure Copy_SPARK_Mode_Aspect (From : Node_Id; To : Node_Id) is
5741 pragma Assert (not Has_Aspects (To));
5742 Asp : Node_Id;
5743
5744 begin
5745 if Has_Aspects (From) then
5746 Asp := Find_Aspect (Defining_Entity (From), Aspect_SPARK_Mode);
5747
5748 if Present (Asp) then
5749 Set_Aspect_Specifications (To, New_List (New_Copy_Tree (Asp)));
5750 Set_Has_Aspects (To, True);
5751 end if;
5752 end if;
5753 end Copy_SPARK_Mode_Aspect;
5754
5755 --------------------------
5756 -- Copy_Subprogram_Spec --
5757 --------------------------
5758
5759 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
5760 Def_Id : Node_Id;
5761 Formal_Spec : Node_Id;
5762 Result : Node_Id;
5763
5764 begin
5765 -- The structure of the original tree must be replicated without any
5766 -- alterations. Use New_Copy_Tree for this purpose.
5767
5768 Result := New_Copy_Tree (Spec);
5769
5770 -- However, the spec of a null procedure carries the corresponding null
5771 -- statement of the body (created by the parser), and this cannot be
5772 -- shared with the new subprogram spec.
5773
5774 if Nkind (Result) = N_Procedure_Specification then
5775 Set_Null_Statement (Result, Empty);
5776 end if;
5777
5778 -- Create a new entity for the defining unit name
5779
5780 Def_Id := Defining_Unit_Name (Result);
5781 Set_Defining_Unit_Name (Result,
5782 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5783
5784 -- Create new entities for the formal parameters
5785
5786 if Present (Parameter_Specifications (Result)) then
5787 Formal_Spec := First (Parameter_Specifications (Result));
5788 while Present (Formal_Spec) loop
5789 Def_Id := Defining_Identifier (Formal_Spec);
5790 Set_Defining_Identifier (Formal_Spec,
5791 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5792
5793 Next (Formal_Spec);
5794 end loop;
5795 end if;
5796
5797 return Result;
5798 end Copy_Subprogram_Spec;
5799
5800 --------------------------------
5801 -- Corresponding_Generic_Type --
5802 --------------------------------
5803
5804 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
5805 Inst : Entity_Id;
5806 Gen : Entity_Id;
5807 Typ : Entity_Id;
5808
5809 begin
5810 if not Is_Generic_Actual_Type (T) then
5811 return Any_Type;
5812
5813 -- If the actual is the actual of an enclosing instance, resolution
5814 -- was correct in the generic.
5815
5816 elsif Nkind (Parent (T)) = N_Subtype_Declaration
5817 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
5818 and then
5819 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
5820 then
5821 return Any_Type;
5822
5823 else
5824 Inst := Scope (T);
5825
5826 if Is_Wrapper_Package (Inst) then
5827 Inst := Related_Instance (Inst);
5828 end if;
5829
5830 Gen :=
5831 Generic_Parent
5832 (Specification (Unit_Declaration_Node (Inst)));
5833
5834 -- Generic actual has the same name as the corresponding formal
5835
5836 Typ := First_Entity (Gen);
5837 while Present (Typ) loop
5838 if Chars (Typ) = Chars (T) then
5839 return Typ;
5840 end if;
5841
5842 Next_Entity (Typ);
5843 end loop;
5844
5845 return Any_Type;
5846 end if;
5847 end Corresponding_Generic_Type;
5848
5849 --------------------
5850 -- Current_Entity --
5851 --------------------
5852
5853 -- The currently visible definition for a given identifier is the
5854 -- one most chained at the start of the visibility chain, i.e. the
5855 -- one that is referenced by the Node_Id value of the name of the
5856 -- given identifier.
5857
5858 function Current_Entity (N : Node_Id) return Entity_Id is
5859 begin
5860 return Get_Name_Entity_Id (Chars (N));
5861 end Current_Entity;
5862
5863 -----------------------------
5864 -- Current_Entity_In_Scope --
5865 -----------------------------
5866
5867 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
5868 E : Entity_Id;
5869 CS : constant Entity_Id := Current_Scope;
5870
5871 Transient_Case : constant Boolean := Scope_Is_Transient;
5872
5873 begin
5874 E := Get_Name_Entity_Id (Chars (N));
5875 while Present (E)
5876 and then Scope (E) /= CS
5877 and then (not Transient_Case or else Scope (E) /= Scope (CS))
5878 loop
5879 E := Homonym (E);
5880 end loop;
5881
5882 return E;
5883 end Current_Entity_In_Scope;
5884
5885 -------------------
5886 -- Current_Scope --
5887 -------------------
5888
5889 function Current_Scope return Entity_Id is
5890 begin
5891 if Scope_Stack.Last = -1 then
5892 return Standard_Standard;
5893 else
5894 declare
5895 C : constant Entity_Id :=
5896 Scope_Stack.Table (Scope_Stack.Last).Entity;
5897 begin
5898 if Present (C) then
5899 return C;
5900 else
5901 return Standard_Standard;
5902 end if;
5903 end;
5904 end if;
5905 end Current_Scope;
5906
5907 ----------------------------
5908 -- Current_Scope_No_Loops --
5909 ----------------------------
5910
5911 function Current_Scope_No_Loops return Entity_Id is
5912 S : Entity_Id;
5913
5914 begin
5915 -- Examine the scope stack starting from the current scope and skip any
5916 -- internally generated loops.
5917
5918 S := Current_Scope;
5919 while Present (S) and then S /= Standard_Standard loop
5920 if Ekind (S) = E_Loop and then not Comes_From_Source (S) then
5921 S := Scope (S);
5922 else
5923 exit;
5924 end if;
5925 end loop;
5926
5927 return S;
5928 end Current_Scope_No_Loops;
5929
5930 ------------------------
5931 -- Current_Subprogram --
5932 ------------------------
5933
5934 function Current_Subprogram return Entity_Id is
5935 Scop : constant Entity_Id := Current_Scope;
5936 begin
5937 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
5938 return Scop;
5939 else
5940 return Enclosing_Subprogram (Scop);
5941 end if;
5942 end Current_Subprogram;
5943
5944 ----------------------------------
5945 -- Deepest_Type_Access_Level --
5946 ----------------------------------
5947
5948 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
5949 begin
5950 if Ekind (Typ) = E_Anonymous_Access_Type
5951 and then not Is_Local_Anonymous_Access (Typ)
5952 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
5953 then
5954 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
5955 -- access type.
5956
5957 return
5958 Scope_Depth (Enclosing_Dynamic_Scope
5959 (Defining_Identifier
5960 (Associated_Node_For_Itype (Typ))));
5961
5962 -- For generic formal type, return Int'Last (infinite).
5963 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
5964
5965 elsif Is_Generic_Type (Root_Type (Typ)) then
5966 return UI_From_Int (Int'Last);
5967
5968 else
5969 return Type_Access_Level (Typ);
5970 end if;
5971 end Deepest_Type_Access_Level;
5972
5973 ---------------------
5974 -- Defining_Entity --
5975 ---------------------
5976
5977 function Defining_Entity (N : Node_Id) return Entity_Id is
5978 begin
5979 case Nkind (N) is
5980 when N_Abstract_Subprogram_Declaration
5981 | N_Expression_Function
5982 | N_Formal_Subprogram_Declaration
5983 | N_Generic_Package_Declaration
5984 | N_Generic_Subprogram_Declaration
5985 | N_Package_Declaration
5986 | N_Subprogram_Body
5987 | N_Subprogram_Body_Stub
5988 | N_Subprogram_Declaration
5989 | N_Subprogram_Renaming_Declaration
5990 =>
5991 return Defining_Entity (Specification (N));
5992
5993 when N_Component_Declaration
5994 | N_Defining_Program_Unit_Name
5995 | N_Discriminant_Specification
5996 | N_Entry_Body
5997 | N_Entry_Declaration
5998 | N_Entry_Index_Specification
5999 | N_Exception_Declaration
6000 | N_Exception_Renaming_Declaration
6001 | N_Formal_Object_Declaration
6002 | N_Formal_Package_Declaration
6003 | N_Formal_Type_Declaration
6004 | N_Full_Type_Declaration
6005 | N_Implicit_Label_Declaration
6006 | N_Incomplete_Type_Declaration
6007 | N_Iterator_Specification
6008 | N_Loop_Parameter_Specification
6009 | N_Number_Declaration
6010 | N_Object_Declaration
6011 | N_Object_Renaming_Declaration
6012 | N_Package_Body_Stub
6013 | N_Parameter_Specification
6014 | N_Private_Extension_Declaration
6015 | N_Private_Type_Declaration
6016 | N_Protected_Body
6017 | N_Protected_Body_Stub
6018 | N_Protected_Type_Declaration
6019 | N_Single_Protected_Declaration
6020 | N_Single_Task_Declaration
6021 | N_Subtype_Declaration
6022 | N_Task_Body
6023 | N_Task_Body_Stub
6024 | N_Task_Type_Declaration
6025 =>
6026 return Defining_Identifier (N);
6027
6028 when N_Compilation_Unit =>
6029 return Defining_Entity (Unit (N));
6030
6031 when N_Subunit =>
6032 return Defining_Entity (Proper_Body (N));
6033
6034 when N_Function_Instantiation
6035 | N_Function_Specification
6036 | N_Generic_Function_Renaming_Declaration
6037 | N_Generic_Package_Renaming_Declaration
6038 | N_Generic_Procedure_Renaming_Declaration
6039 | N_Package_Body
6040 | N_Package_Instantiation
6041 | N_Package_Renaming_Declaration
6042 | N_Package_Specification
6043 | N_Procedure_Instantiation
6044 | N_Procedure_Specification
6045 =>
6046 declare
6047 Nam : constant Node_Id := Defining_Unit_Name (N);
6048 Err : Entity_Id := Empty;
6049
6050 begin
6051 if Nkind (Nam) in N_Entity then
6052 return Nam;
6053
6054 -- For Error, make up a name and attach to declaration so we
6055 -- can continue semantic analysis.
6056
6057 elsif Nam = Error then
6058 Err := Make_Temporary (Sloc (N), 'T');
6059 Set_Defining_Unit_Name (N, Err);
6060
6061 return Err;
6062
6063 -- If not an entity, get defining identifier
6064
6065 else
6066 return Defining_Identifier (Nam);
6067 end if;
6068 end;
6069
6070 when N_Block_Statement
6071 | N_Loop_Statement
6072 =>
6073 return Entity (Identifier (N));
6074
6075 when others =>
6076 raise Program_Error;
6077 end case;
6078 end Defining_Entity;
6079
6080 --------------------------
6081 -- Denotes_Discriminant --
6082 --------------------------
6083
6084 function Denotes_Discriminant
6085 (N : Node_Id;
6086 Check_Concurrent : Boolean := False) return Boolean
6087 is
6088 E : Entity_Id;
6089
6090 begin
6091 if not Is_Entity_Name (N) or else No (Entity (N)) then
6092 return False;
6093 else
6094 E := Entity (N);
6095 end if;
6096
6097 -- If we are checking for a protected type, the discriminant may have
6098 -- been rewritten as the corresponding discriminal of the original type
6099 -- or of the corresponding concurrent record, depending on whether we
6100 -- are in the spec or body of the protected type.
6101
6102 return Ekind (E) = E_Discriminant
6103 or else
6104 (Check_Concurrent
6105 and then Ekind (E) = E_In_Parameter
6106 and then Present (Discriminal_Link (E))
6107 and then
6108 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
6109 or else
6110 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
6111 end Denotes_Discriminant;
6112
6113 -------------------------
6114 -- Denotes_Same_Object --
6115 -------------------------
6116
6117 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
6118 function Is_Renaming (N : Node_Id) return Boolean;
6119 -- Return true if N names a renaming entity
6120
6121 function Is_Valid_Renaming (N : Node_Id) return Boolean;
6122 -- For renamings, return False if the prefix of any dereference within
6123 -- the renamed object_name is a variable, or any expression within the
6124 -- renamed object_name contains references to variables or calls on
6125 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
6126
6127 -----------------
6128 -- Is_Renaming --
6129 -----------------
6130
6131 function Is_Renaming (N : Node_Id) return Boolean is
6132 begin
6133 return
6134 Is_Entity_Name (N) and then Present (Renamed_Entity (Entity (N)));
6135 end Is_Renaming;
6136
6137 -----------------------
6138 -- Is_Valid_Renaming --
6139 -----------------------
6140
6141 function Is_Valid_Renaming (N : Node_Id) return Boolean is
6142 function Check_Renaming (N : Node_Id) return Boolean;
6143 -- Recursive function used to traverse all the prefixes of N
6144
6145 --------------------
6146 -- Check_Renaming --
6147 --------------------
6148
6149 function Check_Renaming (N : Node_Id) return Boolean is
6150 begin
6151 if Is_Renaming (N)
6152 and then not Check_Renaming (Renamed_Entity (Entity (N)))
6153 then
6154 return False;
6155 end if;
6156
6157 if Nkind (N) = N_Indexed_Component then
6158 declare
6159 Indx : Node_Id;
6160
6161 begin
6162 Indx := First (Expressions (N));
6163 while Present (Indx) loop
6164 if not Is_OK_Static_Expression (Indx) then
6165 return False;
6166 end if;
6167
6168 Next_Index (Indx);
6169 end loop;
6170 end;
6171 end if;
6172
6173 if Has_Prefix (N) then
6174 declare
6175 P : constant Node_Id := Prefix (N);
6176
6177 begin
6178 if Nkind (N) = N_Explicit_Dereference
6179 and then Is_Variable (P)
6180 then
6181 return False;
6182
6183 elsif Is_Entity_Name (P)
6184 and then Ekind (Entity (P)) = E_Function
6185 then
6186 return False;
6187
6188 elsif Nkind (P) = N_Function_Call then
6189 return False;
6190 end if;
6191
6192 -- Recursion to continue traversing the prefix of the
6193 -- renaming expression
6194
6195 return Check_Renaming (P);
6196 end;
6197 end if;
6198
6199 return True;
6200 end Check_Renaming;
6201
6202 -- Start of processing for Is_Valid_Renaming
6203
6204 begin
6205 return Check_Renaming (N);
6206 end Is_Valid_Renaming;
6207
6208 -- Local variables
6209
6210 Obj1 : Node_Id := A1;
6211 Obj2 : Node_Id := A2;
6212
6213 -- Start of processing for Denotes_Same_Object
6214
6215 begin
6216 -- Both names statically denote the same stand-alone object or parameter
6217 -- (RM 6.4.1(6.5/3))
6218
6219 if Is_Entity_Name (Obj1)
6220 and then Is_Entity_Name (Obj2)
6221 and then Entity (Obj1) = Entity (Obj2)
6222 then
6223 return True;
6224 end if;
6225
6226 -- For renamings, the prefix of any dereference within the renamed
6227 -- object_name is not a variable, and any expression within the
6228 -- renamed object_name contains no references to variables nor
6229 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
6230
6231 if Is_Renaming (Obj1) then
6232 if Is_Valid_Renaming (Obj1) then
6233 Obj1 := Renamed_Entity (Entity (Obj1));
6234 else
6235 return False;
6236 end if;
6237 end if;
6238
6239 if Is_Renaming (Obj2) then
6240 if Is_Valid_Renaming (Obj2) then
6241 Obj2 := Renamed_Entity (Entity (Obj2));
6242 else
6243 return False;
6244 end if;
6245 end if;
6246
6247 -- No match if not same node kind (such cases are handled by
6248 -- Denotes_Same_Prefix)
6249
6250 if Nkind (Obj1) /= Nkind (Obj2) then
6251 return False;
6252
6253 -- After handling valid renamings, one of the two names statically
6254 -- denoted a renaming declaration whose renamed object_name is known
6255 -- to denote the same object as the other (RM 6.4.1(6.10/3))
6256
6257 elsif Is_Entity_Name (Obj1) then
6258 if Is_Entity_Name (Obj2) then
6259 return Entity (Obj1) = Entity (Obj2);
6260 else
6261 return False;
6262 end if;
6263
6264 -- Both names are selected_components, their prefixes are known to
6265 -- denote the same object, and their selector_names denote the same
6266 -- component (RM 6.4.1(6.6/3)).
6267
6268 elsif Nkind (Obj1) = N_Selected_Component then
6269 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
6270 and then
6271 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
6272
6273 -- Both names are dereferences and the dereferenced names are known to
6274 -- denote the same object (RM 6.4.1(6.7/3))
6275
6276 elsif Nkind (Obj1) = N_Explicit_Dereference then
6277 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
6278
6279 -- Both names are indexed_components, their prefixes are known to denote
6280 -- the same object, and each of the pairs of corresponding index values
6281 -- are either both static expressions with the same static value or both
6282 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
6283
6284 elsif Nkind (Obj1) = N_Indexed_Component then
6285 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
6286 return False;
6287 else
6288 declare
6289 Indx1 : Node_Id;
6290 Indx2 : Node_Id;
6291
6292 begin
6293 Indx1 := First (Expressions (Obj1));
6294 Indx2 := First (Expressions (Obj2));
6295 while Present (Indx1) loop
6296
6297 -- Indexes must denote the same static value or same object
6298
6299 if Is_OK_Static_Expression (Indx1) then
6300 if not Is_OK_Static_Expression (Indx2) then
6301 return False;
6302
6303 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
6304 return False;
6305 end if;
6306
6307 elsif not Denotes_Same_Object (Indx1, Indx2) then
6308 return False;
6309 end if;
6310
6311 Next (Indx1);
6312 Next (Indx2);
6313 end loop;
6314
6315 return True;
6316 end;
6317 end if;
6318
6319 -- Both names are slices, their prefixes are known to denote the same
6320 -- object, and the two slices have statically matching index constraints
6321 -- (RM 6.4.1(6.9/3))
6322
6323 elsif Nkind (Obj1) = N_Slice
6324 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
6325 then
6326 declare
6327 Lo1, Lo2, Hi1, Hi2 : Node_Id;
6328
6329 begin
6330 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
6331 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
6332
6333 -- Check whether bounds are statically identical. There is no
6334 -- attempt to detect partial overlap of slices.
6335
6336 return Denotes_Same_Object (Lo1, Lo2)
6337 and then
6338 Denotes_Same_Object (Hi1, Hi2);
6339 end;
6340
6341 -- In the recursion, literals appear as indexes
6342
6343 elsif Nkind (Obj1) = N_Integer_Literal
6344 and then
6345 Nkind (Obj2) = N_Integer_Literal
6346 then
6347 return Intval (Obj1) = Intval (Obj2);
6348
6349 else
6350 return False;
6351 end if;
6352 end Denotes_Same_Object;
6353
6354 -------------------------
6355 -- Denotes_Same_Prefix --
6356 -------------------------
6357
6358 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
6359 begin
6360 if Is_Entity_Name (A1) then
6361 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
6362 and then not Is_Access_Type (Etype (A1))
6363 then
6364 return Denotes_Same_Object (A1, Prefix (A2))
6365 or else Denotes_Same_Prefix (A1, Prefix (A2));
6366 else
6367 return False;
6368 end if;
6369
6370 elsif Is_Entity_Name (A2) then
6371 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
6372
6373 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
6374 and then
6375 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
6376 then
6377 declare
6378 Root1, Root2 : Node_Id;
6379 Depth1, Depth2 : Nat := 0;
6380
6381 begin
6382 Root1 := Prefix (A1);
6383 while not Is_Entity_Name (Root1) loop
6384 if not Nkind_In
6385 (Root1, N_Selected_Component, N_Indexed_Component)
6386 then
6387 return False;
6388 else
6389 Root1 := Prefix (Root1);
6390 end if;
6391
6392 Depth1 := Depth1 + 1;
6393 end loop;
6394
6395 Root2 := Prefix (A2);
6396 while not Is_Entity_Name (Root2) loop
6397 if not Nkind_In (Root2, N_Selected_Component,
6398 N_Indexed_Component)
6399 then
6400 return False;
6401 else
6402 Root2 := Prefix (Root2);
6403 end if;
6404
6405 Depth2 := Depth2 + 1;
6406 end loop;
6407
6408 -- If both have the same depth and they do not denote the same
6409 -- object, they are disjoint and no warning is needed.
6410
6411 if Depth1 = Depth2 then
6412 return False;
6413
6414 elsif Depth1 > Depth2 then
6415 Root1 := Prefix (A1);
6416 for J in 1 .. Depth1 - Depth2 - 1 loop
6417 Root1 := Prefix (Root1);
6418 end loop;
6419
6420 return Denotes_Same_Object (Root1, A2);
6421
6422 else
6423 Root2 := Prefix (A2);
6424 for J in 1 .. Depth2 - Depth1 - 1 loop
6425 Root2 := Prefix (Root2);
6426 end loop;
6427
6428 return Denotes_Same_Object (A1, Root2);
6429 end if;
6430 end;
6431
6432 else
6433 return False;
6434 end if;
6435 end Denotes_Same_Prefix;
6436
6437 ----------------------
6438 -- Denotes_Variable --
6439 ----------------------
6440
6441 function Denotes_Variable (N : Node_Id) return Boolean is
6442 begin
6443 return Is_Variable (N) and then Paren_Count (N) = 0;
6444 end Denotes_Variable;
6445
6446 -----------------------------
6447 -- Depends_On_Discriminant --
6448 -----------------------------
6449
6450 function Depends_On_Discriminant (N : Node_Id) return Boolean is
6451 L : Node_Id;
6452 H : Node_Id;
6453
6454 begin
6455 Get_Index_Bounds (N, L, H);
6456 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
6457 end Depends_On_Discriminant;
6458
6459 -------------------------
6460 -- Designate_Same_Unit --
6461 -------------------------
6462
6463 function Designate_Same_Unit
6464 (Name1 : Node_Id;
6465 Name2 : Node_Id) return Boolean
6466 is
6467 K1 : constant Node_Kind := Nkind (Name1);
6468 K2 : constant Node_Kind := Nkind (Name2);
6469
6470 function Prefix_Node (N : Node_Id) return Node_Id;
6471 -- Returns the parent unit name node of a defining program unit name
6472 -- or the prefix if N is a selected component or an expanded name.
6473
6474 function Select_Node (N : Node_Id) return Node_Id;
6475 -- Returns the defining identifier node of a defining program unit
6476 -- name or the selector node if N is a selected component or an
6477 -- expanded name.
6478
6479 -----------------
6480 -- Prefix_Node --
6481 -----------------
6482
6483 function Prefix_Node (N : Node_Id) return Node_Id is
6484 begin
6485 if Nkind (N) = N_Defining_Program_Unit_Name then
6486 return Name (N);
6487 else
6488 return Prefix (N);
6489 end if;
6490 end Prefix_Node;
6491
6492 -----------------
6493 -- Select_Node --
6494 -----------------
6495
6496 function Select_Node (N : Node_Id) return Node_Id is
6497 begin
6498 if Nkind (N) = N_Defining_Program_Unit_Name then
6499 return Defining_Identifier (N);
6500 else
6501 return Selector_Name (N);
6502 end if;
6503 end Select_Node;
6504
6505 -- Start of processing for Designate_Same_Unit
6506
6507 begin
6508 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
6509 and then
6510 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
6511 then
6512 return Chars (Name1) = Chars (Name2);
6513
6514 elsif Nkind_In (K1, N_Expanded_Name,
6515 N_Selected_Component,
6516 N_Defining_Program_Unit_Name)
6517 and then
6518 Nkind_In (K2, N_Expanded_Name,
6519 N_Selected_Component,
6520 N_Defining_Program_Unit_Name)
6521 then
6522 return
6523 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
6524 and then
6525 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
6526
6527 else
6528 return False;
6529 end if;
6530 end Designate_Same_Unit;
6531
6532 ---------------------------------------------
6533 -- Diagnose_Iterated_Component_Association --
6534 ---------------------------------------------
6535
6536 procedure Diagnose_Iterated_Component_Association (N : Node_Id) is
6537 Def_Id : constant Entity_Id := Defining_Identifier (N);
6538 Aggr : Node_Id;
6539
6540 begin
6541 -- Determine whether the iterated component association appears within
6542 -- an aggregate. If this is the case, raise Program_Error because the
6543 -- iterated component association cannot be left in the tree as is and
6544 -- must always be processed by the related aggregate.
6545
6546 Aggr := N;
6547 while Present (Aggr) loop
6548 if Nkind (Aggr) = N_Aggregate then
6549 raise Program_Error;
6550
6551 -- Prevent the search from going too far
6552
6553 elsif Is_Body_Or_Package_Declaration (Aggr) then
6554 exit;
6555 end if;
6556
6557 Aggr := Parent (Aggr);
6558 end loop;
6559
6560 -- At this point it is known that the iterated component association is
6561 -- not within an aggregate. This is really a quantified expression with
6562 -- a missing "all" or "some" quantifier.
6563
6564 Error_Msg_N ("missing quantifier", Def_Id);
6565
6566 -- Rewrite the iterated component association as True to prevent any
6567 -- cascaded errors.
6568
6569 Rewrite (N, New_Occurrence_Of (Standard_True, Sloc (N)));
6570 Analyze (N);
6571 end Diagnose_Iterated_Component_Association;
6572
6573 ---------------------------------
6574 -- Dynamic_Accessibility_Level --
6575 ---------------------------------
6576
6577 function Dynamic_Accessibility_Level (N : Node_Id) return Node_Id is
6578 Loc : constant Source_Ptr := Sloc (N);
6579
6580 function Make_Level_Literal (Level : Uint) return Node_Id;
6581 -- Construct an integer literal representing an accessibility level
6582 -- with its type set to Natural.
6583
6584 ------------------------
6585 -- Make_Level_Literal --
6586 ------------------------
6587
6588 function Make_Level_Literal (Level : Uint) return Node_Id is
6589 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
6590
6591 begin
6592 Set_Etype (Result, Standard_Natural);
6593 return Result;
6594 end Make_Level_Literal;
6595
6596 -- Local variables
6597
6598 Expr : Node_Id := Original_Node (N);
6599 -- Expr references the original node because at this stage N may be the
6600 -- reference to a variable internally created by the frontend to remove
6601 -- side effects of an expression.
6602
6603 E : Entity_Id;
6604
6605 -- Start of processing for Dynamic_Accessibility_Level
6606
6607 begin
6608 if Is_Entity_Name (Expr) then
6609 E := Entity (Expr);
6610
6611 if Present (Renamed_Object (E)) then
6612 return Dynamic_Accessibility_Level (Renamed_Object (E));
6613 end if;
6614
6615 if (Is_Formal (E)
6616 or else Ekind_In (E, E_Variable, E_Constant))
6617 and then Present (Get_Accessibility (E))
6618 then
6619 return New_Occurrence_Of (Get_Accessibility (E), Loc);
6620 end if;
6621 end if;
6622
6623 -- Handle a constant-folded conditional expression by avoiding use of
6624 -- the original node.
6625
6626 if Nkind_In (Expr, N_Case_Expression, N_If_Expression) then
6627 Expr := N;
6628 end if;
6629
6630 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
6631
6632 case Nkind (Expr) is
6633 -- It may be possible that we have an access object denoted by an
6634 -- attribute reference for 'Loop_Entry which may, in turn, have an
6635 -- indexed component representing a loop identifier.
6636
6637 -- In this case we must climb up the indexed component and set expr
6638 -- to the attribute reference so the rest of the machinery can
6639 -- operate as expected.
6640
6641 when N_Indexed_Component =>
6642 if Nkind (Prefix (Expr)) = N_Attribute_Reference
6643 and then Get_Attribute_Id (Attribute_Name (Prefix (Expr)))
6644 = Attribute_Loop_Entry
6645 then
6646 Expr := Prefix (Expr);
6647 end if;
6648
6649 -- For access discriminant, the level of the enclosing object
6650
6651 when N_Selected_Component =>
6652 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
6653 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
6654 E_Anonymous_Access_Type
6655 then
6656 return Make_Level_Literal (Object_Access_Level (Expr));
6657 end if;
6658
6659 when N_Attribute_Reference =>
6660 case Get_Attribute_Id (Attribute_Name (Expr)) is
6661
6662 -- Ignore 'Loop_Entry, 'Result, and 'Old as they can be used to
6663 -- identify access objects and do not have an effect on
6664 -- accessibility level.
6665
6666 when Attribute_Loop_Entry | Attribute_Old | Attribute_Result =>
6667 null;
6668
6669 -- For X'Access, the level of the prefix X
6670
6671 when Attribute_Access =>
6672 return Make_Level_Literal
6673 (Object_Access_Level (Prefix (Expr)));
6674
6675 -- Treat the unchecked attributes as library-level
6676
6677 when Attribute_Unchecked_Access
6678 | Attribute_Unrestricted_Access
6679 =>
6680 return Make_Level_Literal (Scope_Depth (Standard_Standard));
6681
6682 -- No other access-valued attributes
6683
6684 when others =>
6685 raise Program_Error;
6686 end case;
6687
6688 when N_Allocator =>
6689
6690 -- This is not fully implemented since it depends on context (see
6691 -- 3.10.2(14/3-14.2/3). More work is needed in the following cases
6692 --
6693 -- 1) For an anonymous allocator defining the value of an access
6694 -- parameter, the accessibility level is that of the innermost
6695 -- master of the call; however currently we pass the level of
6696 -- execution of the called subprogram, which is one greater
6697 -- than the current scope level (see Expand_Call_Helper).
6698 --
6699 -- For example, a statement is a master and a declaration is
6700 -- not a master; so we should not pass in the same level for
6701 -- the following cases:
6702 --
6703 -- function F (X : access Integer) return T is ... ;
6704 -- Decl : T := F (new Integer); -- level is off by one
6705 -- begin
6706 -- Decl := F (new Integer); -- we get this case right
6707 --
6708 -- 2) For an anonymous allocator that defines the result of a
6709 -- function with an access result, the accessibility level is
6710 -- determined as though the allocator were in place of the call
6711 -- of the function. In the special case of a call that is the
6712 -- operand of a type conversion the level is that of the target
6713 -- access type of the conversion.
6714 --
6715 -- 3) For an anonymous allocator defining an access discriminant
6716 -- the accessibility level is determined as follows:
6717 -- * for an allocator used to define the discriminant of an
6718 -- object, the level of the object
6719 -- * for an allocator used to define the constraint in a
6720 -- subtype_indication in any other context, the level of
6721 -- the master that elaborates the subtype_indication.
6722
6723 case Nkind (Parent (N)) is
6724 when N_Object_Declaration =>
6725
6726 -- For an anonymous allocator whose type is that of a
6727 -- stand-alone object of an anonymous access-to-object type,
6728 -- the accessibility level is that of the declaration of the
6729 -- stand-alone object.
6730
6731 return
6732 Make_Level_Literal
6733 (Object_Access_Level
6734 (Defining_Identifier (Parent (N))));
6735
6736 when N_Assignment_Statement =>
6737 return
6738 Make_Level_Literal
6739 (Object_Access_Level (Name (Parent (N))));
6740
6741 when others =>
6742 declare
6743 S : constant String :=
6744 Node_Kind'Image (Nkind (Parent (N)));
6745 begin
6746 Error_Msg_Strlen := S'Length;
6747 Error_Msg_String (1 .. Error_Msg_Strlen) := S;
6748 Error_Msg_N
6749 ("unsupported context for anonymous allocator (~)",
6750 Parent (N));
6751 end;
6752 end case;
6753
6754 when N_Type_Conversion =>
6755 if not Is_Local_Anonymous_Access (Etype (Expr)) then
6756
6757 -- Handle type conversions introduced for a rename of an
6758 -- Ada 2012 stand-alone object of an anonymous access type.
6759
6760 return Dynamic_Accessibility_Level (Expression (Expr));
6761 end if;
6762
6763 when others =>
6764 null;
6765 end case;
6766
6767 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
6768 end Dynamic_Accessibility_Level;
6769
6770 ------------------------
6771 -- Discriminated_Size --
6772 ------------------------
6773
6774 function Discriminated_Size (Comp : Entity_Id) return Boolean is
6775 function Non_Static_Bound (Bound : Node_Id) return Boolean;
6776 -- Check whether the bound of an index is non-static and does denote
6777 -- a discriminant, in which case any object of the type (protected or
6778 -- otherwise) will have a non-static size.
6779
6780 ----------------------
6781 -- Non_Static_Bound --
6782 ----------------------
6783
6784 function Non_Static_Bound (Bound : Node_Id) return Boolean is
6785 begin
6786 if Is_OK_Static_Expression (Bound) then
6787 return False;
6788
6789 -- If the bound is given by a discriminant it is non-static
6790 -- (A static constraint replaces the reference with the value).
6791 -- In an protected object the discriminant has been replaced by
6792 -- the corresponding discriminal within the protected operation.
6793
6794 elsif Is_Entity_Name (Bound)
6795 and then
6796 (Ekind (Entity (Bound)) = E_Discriminant
6797 or else Present (Discriminal_Link (Entity (Bound))))
6798 then
6799 return False;
6800
6801 else
6802 return True;
6803 end if;
6804 end Non_Static_Bound;
6805
6806 -- Local variables
6807
6808 Typ : constant Entity_Id := Etype (Comp);
6809 Index : Node_Id;
6810
6811 -- Start of processing for Discriminated_Size
6812
6813 begin
6814 if not Is_Array_Type (Typ) then
6815 return False;
6816 end if;
6817
6818 if Ekind (Typ) = E_Array_Subtype then
6819 Index := First_Index (Typ);
6820 while Present (Index) loop
6821 if Non_Static_Bound (Low_Bound (Index))
6822 or else Non_Static_Bound (High_Bound (Index))
6823 then
6824 return False;
6825 end if;
6826
6827 Next_Index (Index);
6828 end loop;
6829
6830 return True;
6831 end if;
6832
6833 return False;
6834 end Discriminated_Size;
6835
6836 -----------------------------------
6837 -- Effective_Extra_Accessibility --
6838 -----------------------------------
6839
6840 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
6841 begin
6842 if Present (Renamed_Object (Id))
6843 and then Is_Entity_Name (Renamed_Object (Id))
6844 then
6845 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
6846 else
6847 return Extra_Accessibility (Id);
6848 end if;
6849 end Effective_Extra_Accessibility;
6850
6851 -----------------------------
6852 -- Effective_Reads_Enabled --
6853 -----------------------------
6854
6855 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
6856 begin
6857 return Has_Enabled_Property (Id, Name_Effective_Reads);
6858 end Effective_Reads_Enabled;
6859
6860 ------------------------------
6861 -- Effective_Writes_Enabled --
6862 ------------------------------
6863
6864 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
6865 begin
6866 return Has_Enabled_Property (Id, Name_Effective_Writes);
6867 end Effective_Writes_Enabled;
6868
6869 ------------------------------
6870 -- Enclosing_Comp_Unit_Node --
6871 ------------------------------
6872
6873 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
6874 Current_Node : Node_Id;
6875
6876 begin
6877 Current_Node := N;
6878 while Present (Current_Node)
6879 and then Nkind (Current_Node) /= N_Compilation_Unit
6880 loop
6881 Current_Node := Parent (Current_Node);
6882 end loop;
6883
6884 if Nkind (Current_Node) /= N_Compilation_Unit then
6885 return Empty;
6886 else
6887 return Current_Node;
6888 end if;
6889 end Enclosing_Comp_Unit_Node;
6890
6891 --------------------------
6892 -- Enclosing_CPP_Parent --
6893 --------------------------
6894
6895 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
6896 Parent_Typ : Entity_Id := Typ;
6897
6898 begin
6899 while not Is_CPP_Class (Parent_Typ)
6900 and then Etype (Parent_Typ) /= Parent_Typ
6901 loop
6902 Parent_Typ := Etype (Parent_Typ);
6903
6904 if Is_Private_Type (Parent_Typ) then
6905 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6906 end if;
6907 end loop;
6908
6909 pragma Assert (Is_CPP_Class (Parent_Typ));
6910 return Parent_Typ;
6911 end Enclosing_CPP_Parent;
6912
6913 ---------------------------
6914 -- Enclosing_Declaration --
6915 ---------------------------
6916
6917 function Enclosing_Declaration (N : Node_Id) return Node_Id is
6918 Decl : Node_Id := N;
6919
6920 begin
6921 while Present (Decl)
6922 and then not (Nkind (Decl) in N_Declaration
6923 or else
6924 Nkind (Decl) in N_Later_Decl_Item
6925 or else
6926 Nkind (Decl) = N_Number_Declaration)
6927 loop
6928 Decl := Parent (Decl);
6929 end loop;
6930
6931 return Decl;
6932 end Enclosing_Declaration;
6933
6934 ----------------------------
6935 -- Enclosing_Generic_Body --
6936 ----------------------------
6937
6938 function Enclosing_Generic_Body (N : Node_Id) return Node_Id is
6939 Par : Node_Id;
6940 Spec_Id : Entity_Id;
6941
6942 begin
6943 Par := Parent (N);
6944 while Present (Par) loop
6945 if Nkind_In (Par, N_Package_Body, N_Subprogram_Body) then
6946 Spec_Id := Corresponding_Spec (Par);
6947
6948 if Present (Spec_Id)
6949 and then Nkind_In (Unit_Declaration_Node (Spec_Id),
6950 N_Generic_Package_Declaration,
6951 N_Generic_Subprogram_Declaration)
6952 then
6953 return Par;
6954 end if;
6955 end if;
6956
6957 Par := Parent (Par);
6958 end loop;
6959
6960 return Empty;
6961 end Enclosing_Generic_Body;
6962
6963 ----------------------------
6964 -- Enclosing_Generic_Unit --
6965 ----------------------------
6966
6967 function Enclosing_Generic_Unit (N : Node_Id) return Node_Id is
6968 Par : Node_Id;
6969 Spec_Decl : Node_Id;
6970 Spec_Id : Entity_Id;
6971
6972 begin
6973 Par := Parent (N);
6974 while Present (Par) loop
6975 if Nkind_In (Par, N_Generic_Package_Declaration,
6976 N_Generic_Subprogram_Declaration)
6977 then
6978 return Par;
6979
6980 elsif Nkind_In (Par, N_Package_Body, N_Subprogram_Body) then
6981 Spec_Id := Corresponding_Spec (Par);
6982
6983 if Present (Spec_Id) then
6984 Spec_Decl := Unit_Declaration_Node (Spec_Id);
6985
6986 if Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
6987 N_Generic_Subprogram_Declaration)
6988 then
6989 return Spec_Decl;
6990 end if;
6991 end if;
6992 end if;
6993
6994 Par := Parent (Par);
6995 end loop;
6996
6997 return Empty;
6998 end Enclosing_Generic_Unit;
6999
7000 -------------------------------
7001 -- Enclosing_Lib_Unit_Entity --
7002 -------------------------------
7003
7004 function Enclosing_Lib_Unit_Entity
7005 (E : Entity_Id := Current_Scope) return Entity_Id
7006 is
7007 Unit_Entity : Entity_Id;
7008
7009 begin
7010 -- Look for enclosing library unit entity by following scope links.
7011 -- Equivalent to, but faster than indexing through the scope stack.
7012
7013 Unit_Entity := E;
7014 while (Present (Scope (Unit_Entity))
7015 and then Scope (Unit_Entity) /= Standard_Standard)
7016 and not Is_Child_Unit (Unit_Entity)
7017 loop
7018 Unit_Entity := Scope (Unit_Entity);
7019 end loop;
7020
7021 return Unit_Entity;
7022 end Enclosing_Lib_Unit_Entity;
7023
7024 -----------------------------
7025 -- Enclosing_Lib_Unit_Node --
7026 -----------------------------
7027
7028 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
7029 Encl_Unit : Node_Id;
7030
7031 begin
7032 Encl_Unit := Enclosing_Comp_Unit_Node (N);
7033 while Present (Encl_Unit)
7034 and then Nkind (Unit (Encl_Unit)) = N_Subunit
7035 loop
7036 Encl_Unit := Library_Unit (Encl_Unit);
7037 end loop;
7038
7039 pragma Assert (Nkind (Encl_Unit) = N_Compilation_Unit);
7040 return Encl_Unit;
7041 end Enclosing_Lib_Unit_Node;
7042
7043 -----------------------
7044 -- Enclosing_Package --
7045 -----------------------
7046
7047 function Enclosing_Package (E : Entity_Id) return Entity_Id is
7048 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
7049
7050 begin
7051 if Dynamic_Scope = Standard_Standard then
7052 return Standard_Standard;
7053
7054 elsif Dynamic_Scope = Empty then
7055 return Empty;
7056
7057 elsif Ekind_In (Dynamic_Scope, E_Generic_Package,
7058 E_Package,
7059 E_Package_Body)
7060 then
7061 return Dynamic_Scope;
7062
7063 else
7064 return Enclosing_Package (Dynamic_Scope);
7065 end if;
7066 end Enclosing_Package;
7067
7068 -------------------------------------
7069 -- Enclosing_Package_Or_Subprogram --
7070 -------------------------------------
7071
7072 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
7073 S : Entity_Id;
7074
7075 begin
7076 S := Scope (E);
7077 while Present (S) loop
7078 if Is_Package_Or_Generic_Package (S)
7079 or else Is_Subprogram_Or_Generic_Subprogram (S)
7080 then
7081 return S;
7082
7083 else
7084 S := Scope (S);
7085 end if;
7086 end loop;
7087
7088 return Empty;
7089 end Enclosing_Package_Or_Subprogram;
7090
7091 --------------------------
7092 -- Enclosing_Subprogram --
7093 --------------------------
7094
7095 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
7096 Dyn_Scop : constant Entity_Id := Enclosing_Dynamic_Scope (E);
7097
7098 begin
7099 if Dyn_Scop = Standard_Standard then
7100 return Empty;
7101
7102 elsif Dyn_Scop = Empty then
7103 return Empty;
7104
7105 elsif Ekind (Dyn_Scop) = E_Subprogram_Body then
7106 return Corresponding_Spec (Parent (Parent (Dyn_Scop)));
7107
7108 elsif Ekind_In (Dyn_Scop, E_Block, E_Loop, E_Return_Statement) then
7109 return Enclosing_Subprogram (Dyn_Scop);
7110
7111 elsif Ekind_In (Dyn_Scop, E_Entry, E_Entry_Family) then
7112
7113 -- For a task entry or entry family, return the enclosing subprogram
7114 -- of the task itself.
7115
7116 if Ekind (Scope (Dyn_Scop)) = E_Task_Type then
7117 return Enclosing_Subprogram (Dyn_Scop);
7118
7119 -- A protected entry or entry family is rewritten as a protected
7120 -- procedure which is the desired enclosing subprogram. This is
7121 -- relevant when unnesting a procedure local to an entry body.
7122
7123 else
7124 return Protected_Body_Subprogram (Dyn_Scop);
7125 end if;
7126
7127 elsif Ekind (Dyn_Scop) = E_Task_Type then
7128 return Get_Task_Body_Procedure (Dyn_Scop);
7129
7130 -- The scope may appear as a private type or as a private extension
7131 -- whose completion is a task or protected type.
7132
7133 elsif Ekind_In (Dyn_Scop, E_Limited_Private_Type,
7134 E_Record_Type_With_Private)
7135 and then Present (Full_View (Dyn_Scop))
7136 and then Ekind_In (Full_View (Dyn_Scop), E_Task_Type, E_Protected_Type)
7137 then
7138 return Get_Task_Body_Procedure (Full_View (Dyn_Scop));
7139
7140 -- No body is generated if the protected operation is eliminated
7141
7142 elsif Convention (Dyn_Scop) = Convention_Protected
7143 and then not Is_Eliminated (Dyn_Scop)
7144 and then Present (Protected_Body_Subprogram (Dyn_Scop))
7145 then
7146 return Protected_Body_Subprogram (Dyn_Scop);
7147
7148 else
7149 return Dyn_Scop;
7150 end if;
7151 end Enclosing_Subprogram;
7152
7153 --------------------------
7154 -- End_Keyword_Location --
7155 --------------------------
7156
7157 function End_Keyword_Location (N : Node_Id) return Source_Ptr is
7158 function End_Label_Loc (Nod : Node_Id) return Source_Ptr;
7159 -- Return the source location of Nod's end label according to the
7160 -- following precedence rules:
7161 --
7162 -- 1) If the end label exists, return its location
7163 -- 2) If Nod exists, return its location
7164 -- 3) Return the location of N
7165
7166 -------------------
7167 -- End_Label_Loc --
7168 -------------------
7169
7170 function End_Label_Loc (Nod : Node_Id) return Source_Ptr is
7171 Label : Node_Id;
7172
7173 begin
7174 if Present (Nod) then
7175 Label := End_Label (Nod);
7176
7177 if Present (Label) then
7178 return Sloc (Label);
7179 else
7180 return Sloc (Nod);
7181 end if;
7182
7183 else
7184 return Sloc (N);
7185 end if;
7186 end End_Label_Loc;
7187
7188 -- Local variables
7189
7190 Owner : Node_Id;
7191
7192 -- Start of processing for End_Keyword_Location
7193
7194 begin
7195 if Nkind_In (N, N_Block_Statement,
7196 N_Entry_Body,
7197 N_Package_Body,
7198 N_Subprogram_Body,
7199 N_Task_Body)
7200 then
7201 Owner := Handled_Statement_Sequence (N);
7202
7203 elsif Nkind (N) = N_Package_Declaration then
7204 Owner := Specification (N);
7205
7206 elsif Nkind (N) = N_Protected_Body then
7207 Owner := N;
7208
7209 elsif Nkind_In (N, N_Protected_Type_Declaration,
7210 N_Single_Protected_Declaration)
7211 then
7212 Owner := Protected_Definition (N);
7213
7214 elsif Nkind_In (N, N_Single_Task_Declaration,
7215 N_Task_Type_Declaration)
7216 then
7217 Owner := Task_Definition (N);
7218
7219 -- This routine should not be called with other contexts
7220
7221 else
7222 pragma Assert (False);
7223 null;
7224 end if;
7225
7226 return End_Label_Loc (Owner);
7227 end End_Keyword_Location;
7228
7229 ------------------------
7230 -- Ensure_Freeze_Node --
7231 ------------------------
7232
7233 procedure Ensure_Freeze_Node (E : Entity_Id) is
7234 FN : Node_Id;
7235 begin
7236 if No (Freeze_Node (E)) then
7237 FN := Make_Freeze_Entity (Sloc (E));
7238 Set_Has_Delayed_Freeze (E);
7239 Set_Freeze_Node (E, FN);
7240 Set_Access_Types_To_Process (FN, No_Elist);
7241 Set_TSS_Elist (FN, No_Elist);
7242 Set_Entity (FN, E);
7243 end if;
7244 end Ensure_Freeze_Node;
7245
7246 ----------------
7247 -- Enter_Name --
7248 ----------------
7249
7250 procedure Enter_Name (Def_Id : Entity_Id) is
7251 C : constant Entity_Id := Current_Entity (Def_Id);
7252 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
7253 S : constant Entity_Id := Current_Scope;
7254
7255 begin
7256 Generate_Definition (Def_Id);
7257
7258 -- Add new name to current scope declarations. Check for duplicate
7259 -- declaration, which may or may not be a genuine error.
7260
7261 if Present (E) then
7262
7263 -- Case of previous entity entered because of a missing declaration
7264 -- or else a bad subtype indication. Best is to use the new entity,
7265 -- and make the previous one invisible.
7266
7267 if Etype (E) = Any_Type then
7268 Set_Is_Immediately_Visible (E, False);
7269
7270 -- Case of renaming declaration constructed for package instances.
7271 -- if there is an explicit declaration with the same identifier,
7272 -- the renaming is not immediately visible any longer, but remains
7273 -- visible through selected component notation.
7274
7275 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
7276 and then not Comes_From_Source (E)
7277 then
7278 Set_Is_Immediately_Visible (E, False);
7279
7280 -- The new entity may be the package renaming, which has the same
7281 -- same name as a generic formal which has been seen already.
7282
7283 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
7284 and then not Comes_From_Source (Def_Id)
7285 then
7286 Set_Is_Immediately_Visible (E, False);
7287
7288 -- For a fat pointer corresponding to a remote access to subprogram,
7289 -- we use the same identifier as the RAS type, so that the proper
7290 -- name appears in the stub. This type is only retrieved through
7291 -- the RAS type and never by visibility, and is not added to the
7292 -- visibility list (see below).
7293
7294 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
7295 and then Ekind (Def_Id) = E_Record_Type
7296 and then Present (Corresponding_Remote_Type (Def_Id))
7297 then
7298 null;
7299
7300 -- Case of an implicit operation or derived literal. The new entity
7301 -- hides the implicit one, which is removed from all visibility,
7302 -- i.e. the entity list of its scope, and homonym chain of its name.
7303
7304 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
7305 or else Is_Internal (E)
7306 then
7307 declare
7308 Decl : constant Node_Id := Parent (E);
7309 Prev : Entity_Id;
7310 Prev_Vis : Entity_Id;
7311
7312 begin
7313 -- If E is an implicit declaration, it cannot be the first
7314 -- entity in the scope.
7315
7316 Prev := First_Entity (Current_Scope);
7317 while Present (Prev) and then Next_Entity (Prev) /= E loop
7318 Next_Entity (Prev);
7319 end loop;
7320
7321 if No (Prev) then
7322
7323 -- If E is not on the entity chain of the current scope,
7324 -- it is an implicit declaration in the generic formal
7325 -- part of a generic subprogram. When analyzing the body,
7326 -- the generic formals are visible but not on the entity
7327 -- chain of the subprogram. The new entity will become
7328 -- the visible one in the body.
7329
7330 pragma Assert
7331 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
7332 null;
7333
7334 else
7335 Link_Entities (Prev, Next_Entity (E));
7336
7337 if No (Next_Entity (Prev)) then
7338 Set_Last_Entity (Current_Scope, Prev);
7339 end if;
7340
7341 if E = Current_Entity (E) then
7342 Prev_Vis := Empty;
7343
7344 else
7345 Prev_Vis := Current_Entity (E);
7346 while Homonym (Prev_Vis) /= E loop
7347 Prev_Vis := Homonym (Prev_Vis);
7348 end loop;
7349 end if;
7350
7351 if Present (Prev_Vis) then
7352
7353 -- Skip E in the visibility chain
7354
7355 Set_Homonym (Prev_Vis, Homonym (E));
7356
7357 else
7358 Set_Name_Entity_Id (Chars (E), Homonym (E));
7359 end if;
7360 end if;
7361 end;
7362
7363 -- This section of code could use a comment ???
7364
7365 elsif Present (Etype (E))
7366 and then Is_Concurrent_Type (Etype (E))
7367 and then E = Def_Id
7368 then
7369 return;
7370
7371 -- If the homograph is a protected component renaming, it should not
7372 -- be hiding the current entity. Such renamings are treated as weak
7373 -- declarations.
7374
7375 elsif Is_Prival (E) then
7376 Set_Is_Immediately_Visible (E, False);
7377
7378 -- In this case the current entity is a protected component renaming.
7379 -- Perform minimal decoration by setting the scope and return since
7380 -- the prival should not be hiding other visible entities.
7381
7382 elsif Is_Prival (Def_Id) then
7383 Set_Scope (Def_Id, Current_Scope);
7384 return;
7385
7386 -- Analogous to privals, the discriminal generated for an entry index
7387 -- parameter acts as a weak declaration. Perform minimal decoration
7388 -- to avoid bogus errors.
7389
7390 elsif Is_Discriminal (Def_Id)
7391 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
7392 then
7393 Set_Scope (Def_Id, Current_Scope);
7394 return;
7395
7396 -- In the body or private part of an instance, a type extension may
7397 -- introduce a component with the same name as that of an actual. The
7398 -- legality rule is not enforced, but the semantics of the full type
7399 -- with two components of same name are not clear at this point???
7400
7401 elsif In_Instance_Not_Visible then
7402 null;
7403
7404 -- When compiling a package body, some child units may have become
7405 -- visible. They cannot conflict with local entities that hide them.
7406
7407 elsif Is_Child_Unit (E)
7408 and then In_Open_Scopes (Scope (E))
7409 and then not Is_Immediately_Visible (E)
7410 then
7411 null;
7412
7413 -- Conversely, with front-end inlining we may compile the parent body
7414 -- first, and a child unit subsequently. The context is now the
7415 -- parent spec, and body entities are not visible.
7416
7417 elsif Is_Child_Unit (Def_Id)
7418 and then Is_Package_Body_Entity (E)
7419 and then not In_Package_Body (Current_Scope)
7420 then
7421 null;
7422
7423 -- Case of genuine duplicate declaration
7424
7425 else
7426 Error_Msg_Sloc := Sloc (E);
7427
7428 -- If the previous declaration is an incomplete type declaration
7429 -- this may be an attempt to complete it with a private type. The
7430 -- following avoids confusing cascaded errors.
7431
7432 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
7433 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
7434 then
7435 Error_Msg_N
7436 ("incomplete type cannot be completed with a private " &
7437 "declaration", Parent (Def_Id));
7438 Set_Is_Immediately_Visible (E, False);
7439 Set_Full_View (E, Def_Id);
7440
7441 -- An inherited component of a record conflicts with a new
7442 -- discriminant. The discriminant is inserted first in the scope,
7443 -- but the error should be posted on it, not on the component.
7444
7445 elsif Ekind (E) = E_Discriminant
7446 and then Present (Scope (Def_Id))
7447 and then Scope (Def_Id) /= Current_Scope
7448 then
7449 Error_Msg_Sloc := Sloc (Def_Id);
7450 Error_Msg_N ("& conflicts with declaration#", E);
7451 return;
7452
7453 -- If the name of the unit appears in its own context clause, a
7454 -- dummy package with the name has already been created, and the
7455 -- error emitted. Try to continue quietly.
7456
7457 elsif Error_Posted (E)
7458 and then Sloc (E) = No_Location
7459 and then Nkind (Parent (E)) = N_Package_Specification
7460 and then Current_Scope = Standard_Standard
7461 then
7462 Set_Scope (Def_Id, Current_Scope);
7463 return;
7464
7465 else
7466 Error_Msg_N ("& conflicts with declaration#", Def_Id);
7467
7468 -- Avoid cascaded messages with duplicate components in
7469 -- derived types.
7470
7471 if Ekind_In (E, E_Component, E_Discriminant) then
7472 return;
7473 end if;
7474 end if;
7475
7476 if Nkind (Parent (Parent (Def_Id))) =
7477 N_Generic_Subprogram_Declaration
7478 and then Def_Id =
7479 Defining_Entity (Specification (Parent (Parent (Def_Id))))
7480 then
7481 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
7482 end if;
7483
7484 -- If entity is in standard, then we are in trouble, because it
7485 -- means that we have a library package with a duplicated name.
7486 -- That's hard to recover from, so abort.
7487
7488 if S = Standard_Standard then
7489 raise Unrecoverable_Error;
7490
7491 -- Otherwise we continue with the declaration. Having two
7492 -- identical declarations should not cause us too much trouble.
7493
7494 else
7495 null;
7496 end if;
7497 end if;
7498 end if;
7499
7500 -- If we fall through, declaration is OK, at least OK enough to continue
7501
7502 -- If Def_Id is a discriminant or a record component we are in the midst
7503 -- of inheriting components in a derived record definition. Preserve
7504 -- their Ekind and Etype.
7505
7506 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
7507 null;
7508
7509 -- If a type is already set, leave it alone (happens when a type
7510 -- declaration is reanalyzed following a call to the optimizer).
7511
7512 elsif Present (Etype (Def_Id)) then
7513 null;
7514
7515 -- Otherwise, the kind E_Void insures that premature uses of the entity
7516 -- will be detected. Any_Type insures that no cascaded errors will occur
7517
7518 else
7519 Set_Ekind (Def_Id, E_Void);
7520 Set_Etype (Def_Id, Any_Type);
7521 end if;
7522
7523 -- Inherited discriminants and components in derived record types are
7524 -- immediately visible. Itypes are not.
7525
7526 -- Unless the Itype is for a record type with a corresponding remote
7527 -- type (what is that about, it was not commented ???)
7528
7529 if Ekind_In (Def_Id, E_Discriminant, E_Component)
7530 or else
7531 ((not Is_Record_Type (Def_Id)
7532 or else No (Corresponding_Remote_Type (Def_Id)))
7533 and then not Is_Itype (Def_Id))
7534 then
7535 Set_Is_Immediately_Visible (Def_Id);
7536 Set_Current_Entity (Def_Id);
7537 end if;
7538
7539 Set_Homonym (Def_Id, C);
7540 Append_Entity (Def_Id, S);
7541 Set_Public_Status (Def_Id);
7542
7543 -- Declaring a homonym is not allowed in SPARK ...
7544
7545 if Present (C) and then Restriction_Check_Required (SPARK_05) then
7546 declare
7547 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
7548 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
7549 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
7550
7551 begin
7552 -- ... unless the new declaration is in a subprogram, and the
7553 -- visible declaration is a variable declaration or a parameter
7554 -- specification outside that subprogram.
7555
7556 if Present (Enclosing_Subp)
7557 and then Nkind_In (Parent (C), N_Object_Declaration,
7558 N_Parameter_Specification)
7559 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
7560 then
7561 null;
7562
7563 -- ... or the new declaration is in a package, and the visible
7564 -- declaration occurs outside that package.
7565
7566 elsif Present (Enclosing_Pack)
7567 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
7568 then
7569 null;
7570
7571 -- ... or the new declaration is a component declaration in a
7572 -- record type definition.
7573
7574 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
7575 null;
7576
7577 -- Don't issue error for non-source entities
7578
7579 elsif Comes_From_Source (Def_Id)
7580 and then Comes_From_Source (C)
7581 then
7582 Error_Msg_Sloc := Sloc (C);
7583 Check_SPARK_05_Restriction
7584 ("redeclaration of identifier &#", Def_Id);
7585 end if;
7586 end;
7587 end if;
7588
7589 -- Warn if new entity hides an old one
7590
7591 if Warn_On_Hiding and then Present (C)
7592
7593 -- Don't warn for record components since they always have a well
7594 -- defined scope which does not confuse other uses. Note that in
7595 -- some cases, Ekind has not been set yet.
7596
7597 and then Ekind (C) /= E_Component
7598 and then Ekind (C) /= E_Discriminant
7599 and then Nkind (Parent (C)) /= N_Component_Declaration
7600 and then Ekind (Def_Id) /= E_Component
7601 and then Ekind (Def_Id) /= E_Discriminant
7602 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
7603
7604 -- Don't warn for one character variables. It is too common to use
7605 -- such variables as locals and will just cause too many false hits.
7606
7607 and then Length_Of_Name (Chars (C)) /= 1
7608
7609 -- Don't warn for non-source entities
7610
7611 and then Comes_From_Source (C)
7612 and then Comes_From_Source (Def_Id)
7613
7614 -- Don't warn unless entity in question is in extended main source
7615
7616 and then In_Extended_Main_Source_Unit (Def_Id)
7617
7618 -- Finally, the hidden entity must be either immediately visible or
7619 -- use visible (i.e. from a used package).
7620
7621 and then
7622 (Is_Immediately_Visible (C)
7623 or else
7624 Is_Potentially_Use_Visible (C))
7625 then
7626 Error_Msg_Sloc := Sloc (C);
7627 Error_Msg_N ("declaration hides &#?h?", Def_Id);
7628 end if;
7629 end Enter_Name;
7630
7631 ---------------
7632 -- Entity_Of --
7633 ---------------
7634
7635 function Entity_Of (N : Node_Id) return Entity_Id is
7636 Id : Entity_Id;
7637 Ren : Node_Id;
7638
7639 begin
7640 -- Assume that the arbitrary node does not have an entity
7641
7642 Id := Empty;
7643
7644 if Is_Entity_Name (N) then
7645 Id := Entity (N);
7646
7647 -- Follow a possible chain of renamings to reach the earliest renamed
7648 -- source object.
7649
7650 while Present (Id)
7651 and then Is_Object (Id)
7652 and then Present (Renamed_Object (Id))
7653 loop
7654 Ren := Renamed_Object (Id);
7655
7656 -- The reference renames an abstract state or a whole object
7657
7658 -- Obj : ...;
7659 -- Ren : ... renames Obj;
7660
7661 if Is_Entity_Name (Ren) then
7662
7663 -- Do not follow a renaming that goes through a generic formal,
7664 -- because these entities are hidden and must not be referenced
7665 -- from outside the generic.
7666
7667 if Is_Hidden (Entity (Ren)) then
7668 exit;
7669
7670 else
7671 Id := Entity (Ren);
7672 end if;
7673
7674 -- The reference renames a function result. Check the original
7675 -- node in case expansion relocates the function call.
7676
7677 -- Ren : ... renames Func_Call;
7678
7679 elsif Nkind (Original_Node (Ren)) = N_Function_Call then
7680 exit;
7681
7682 -- Otherwise the reference renames something which does not yield
7683 -- an abstract state or a whole object. Treat the reference as not
7684 -- having a proper entity for SPARK legality purposes.
7685
7686 else
7687 Id := Empty;
7688 exit;
7689 end if;
7690 end loop;
7691 end if;
7692
7693 return Id;
7694 end Entity_Of;
7695
7696 --------------------------
7697 -- Examine_Array_Bounds --
7698 --------------------------
7699
7700 procedure Examine_Array_Bounds
7701 (Typ : Entity_Id;
7702 All_Static : out Boolean;
7703 Has_Empty : out Boolean)
7704 is
7705 function Is_OK_Static_Bound (Bound : Node_Id) return Boolean;
7706 -- Determine whether bound Bound is a suitable static bound
7707
7708 ------------------------
7709 -- Is_OK_Static_Bound --
7710 ------------------------
7711
7712 function Is_OK_Static_Bound (Bound : Node_Id) return Boolean is
7713 begin
7714 return
7715 not Error_Posted (Bound)
7716 and then Is_OK_Static_Expression (Bound);
7717 end Is_OK_Static_Bound;
7718
7719 -- Local variables
7720
7721 Hi_Bound : Node_Id;
7722 Index : Node_Id;
7723 Lo_Bound : Node_Id;
7724
7725 -- Start of processing for Examine_Array_Bounds
7726
7727 begin
7728 -- An unconstrained array type does not have static bounds, and it is
7729 -- not known whether they are empty or not.
7730
7731 if not Is_Constrained (Typ) then
7732 All_Static := False;
7733 Has_Empty := False;
7734
7735 -- A string literal has static bounds, and is not empty as long as it
7736 -- contains at least one character.
7737
7738 elsif Ekind (Typ) = E_String_Literal_Subtype then
7739 All_Static := True;
7740 Has_Empty := String_Literal_Length (Typ) > 0;
7741 end if;
7742
7743 -- Assume that all bounds are static and not empty
7744
7745 All_Static := True;
7746 Has_Empty := False;
7747
7748 -- Examine each index
7749
7750 Index := First_Index (Typ);
7751 while Present (Index) loop
7752 if Is_Discrete_Type (Etype (Index)) then
7753 Get_Index_Bounds (Index, Lo_Bound, Hi_Bound);
7754
7755 if Is_OK_Static_Bound (Lo_Bound)
7756 and then
7757 Is_OK_Static_Bound (Hi_Bound)
7758 then
7759 -- The static bounds produce an empty range
7760
7761 if Is_Null_Range (Lo_Bound, Hi_Bound) then
7762 Has_Empty := True;
7763 end if;
7764
7765 -- Otherwise at least one of the bounds is not static
7766
7767 else
7768 All_Static := False;
7769 end if;
7770
7771 -- Otherwise the index is non-discrete, therefore not static
7772
7773 else
7774 All_Static := False;
7775 end if;
7776
7777 Next_Index (Index);
7778 end loop;
7779 end Examine_Array_Bounds;
7780
7781 -------------------
7782 -- Exceptions_OK --
7783 -------------------
7784
7785 function Exceptions_OK return Boolean is
7786 begin
7787 return
7788 not (Restriction_Active (No_Exception_Handlers) or else
7789 Restriction_Active (No_Exception_Propagation) or else
7790 Restriction_Active (No_Exceptions));
7791 end Exceptions_OK;
7792
7793 --------------------------
7794 -- Explain_Limited_Type --
7795 --------------------------
7796
7797 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
7798 C : Entity_Id;
7799
7800 begin
7801 -- For array, component type must be limited
7802
7803 if Is_Array_Type (T) then
7804 Error_Msg_Node_2 := T;
7805 Error_Msg_NE
7806 ("\component type& of type& is limited", N, Component_Type (T));
7807 Explain_Limited_Type (Component_Type (T), N);
7808
7809 elsif Is_Record_Type (T) then
7810
7811 -- No need for extra messages if explicit limited record
7812
7813 if Is_Limited_Record (Base_Type (T)) then
7814 return;
7815 end if;
7816
7817 -- Otherwise find a limited component. Check only components that
7818 -- come from source, or inherited components that appear in the
7819 -- source of the ancestor.
7820
7821 C := First_Component (T);
7822 while Present (C) loop
7823 if Is_Limited_Type (Etype (C))
7824 and then
7825 (Comes_From_Source (C)
7826 or else
7827 (Present (Original_Record_Component (C))
7828 and then
7829 Comes_From_Source (Original_Record_Component (C))))
7830 then
7831 Error_Msg_Node_2 := T;
7832 Error_Msg_NE ("\component& of type& has limited type", N, C);
7833 Explain_Limited_Type (Etype (C), N);
7834 return;
7835 end if;
7836
7837 Next_Component (C);
7838 end loop;
7839
7840 -- The type may be declared explicitly limited, even if no component
7841 -- of it is limited, in which case we fall out of the loop.
7842 return;
7843 end if;
7844 end Explain_Limited_Type;
7845
7846 ---------------------------------------
7847 -- Expression_Of_Expression_Function --
7848 ---------------------------------------
7849
7850 function Expression_Of_Expression_Function
7851 (Subp : Entity_Id) return Node_Id
7852 is
7853 Expr_Func : Node_Id;
7854
7855 begin
7856 pragma Assert (Is_Expression_Function_Or_Completion (Subp));
7857
7858 if Nkind (Original_Node (Subprogram_Spec (Subp))) =
7859 N_Expression_Function
7860 then
7861 Expr_Func := Original_Node (Subprogram_Spec (Subp));
7862
7863 elsif Nkind (Original_Node (Subprogram_Body (Subp))) =
7864 N_Expression_Function
7865 then
7866 Expr_Func := Original_Node (Subprogram_Body (Subp));
7867
7868 else
7869 pragma Assert (False);
7870 null;
7871 end if;
7872
7873 return Original_Node (Expression (Expr_Func));
7874 end Expression_Of_Expression_Function;
7875
7876 -------------------------------
7877 -- Extensions_Visible_Status --
7878 -------------------------------
7879
7880 function Extensions_Visible_Status
7881 (Id : Entity_Id) return Extensions_Visible_Mode
7882 is
7883 Arg : Node_Id;
7884 Decl : Node_Id;
7885 Expr : Node_Id;
7886 Prag : Node_Id;
7887 Subp : Entity_Id;
7888
7889 begin
7890 -- When a formal parameter is subject to Extensions_Visible, the pragma
7891 -- is stored in the contract of related subprogram.
7892
7893 if Is_Formal (Id) then
7894 Subp := Scope (Id);
7895
7896 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
7897 Subp := Id;
7898
7899 -- No other construct carries this pragma
7900
7901 else
7902 return Extensions_Visible_None;
7903 end if;
7904
7905 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
7906
7907 -- In certain cases analysis may request the Extensions_Visible status
7908 -- of an expression function before the pragma has been analyzed yet.
7909 -- Inspect the declarative items after the expression function looking
7910 -- for the pragma (if any).
7911
7912 if No (Prag) and then Is_Expression_Function (Subp) then
7913 Decl := Next (Unit_Declaration_Node (Subp));
7914 while Present (Decl) loop
7915 if Nkind (Decl) = N_Pragma
7916 and then Pragma_Name (Decl) = Name_Extensions_Visible
7917 then
7918 Prag := Decl;
7919 exit;
7920
7921 -- A source construct ends the region where Extensions_Visible may
7922 -- appear, stop the traversal. An expanded expression function is
7923 -- no longer a source construct, but it must still be recognized.
7924
7925 elsif Comes_From_Source (Decl)
7926 or else
7927 (Nkind_In (Decl, N_Subprogram_Body,
7928 N_Subprogram_Declaration)
7929 and then Is_Expression_Function (Defining_Entity (Decl)))
7930 then
7931 exit;
7932 end if;
7933
7934 Next (Decl);
7935 end loop;
7936 end if;
7937
7938 -- Extract the value from the Boolean expression (if any)
7939
7940 if Present (Prag) then
7941 Arg := First (Pragma_Argument_Associations (Prag));
7942
7943 if Present (Arg) then
7944 Expr := Get_Pragma_Arg (Arg);
7945
7946 -- When the associated subprogram is an expression function, the
7947 -- argument of the pragma may not have been analyzed.
7948
7949 if not Analyzed (Expr) then
7950 Preanalyze_And_Resolve (Expr, Standard_Boolean);
7951 end if;
7952
7953 -- Guard against cascading errors when the argument of pragma
7954 -- Extensions_Visible is not a valid static Boolean expression.
7955
7956 if Error_Posted (Expr) then
7957 return Extensions_Visible_None;
7958
7959 elsif Is_True (Expr_Value (Expr)) then
7960 return Extensions_Visible_True;
7961
7962 else
7963 return Extensions_Visible_False;
7964 end if;
7965
7966 -- Otherwise the aspect or pragma defaults to True
7967
7968 else
7969 return Extensions_Visible_True;
7970 end if;
7971
7972 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
7973 -- directly specified. In SPARK code, its value defaults to "False".
7974
7975 elsif SPARK_Mode = On then
7976 return Extensions_Visible_False;
7977
7978 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
7979 -- "True".
7980
7981 else
7982 return Extensions_Visible_True;
7983 end if;
7984 end Extensions_Visible_Status;
7985
7986 -----------------
7987 -- Find_Actual --
7988 -----------------
7989
7990 procedure Find_Actual
7991 (N : Node_Id;
7992 Formal : out Entity_Id;
7993 Call : out Node_Id)
7994 is
7995 Context : constant Node_Id := Parent (N);
7996 Actual : Node_Id;
7997 Call_Nam : Node_Id;
7998
7999 begin
8000 if Nkind_In (Context, N_Indexed_Component, N_Selected_Component)
8001 and then N = Prefix (Context)
8002 then
8003 Find_Actual (Context, Formal, Call);
8004 return;
8005
8006 elsif Nkind (Context) = N_Parameter_Association
8007 and then N = Explicit_Actual_Parameter (Context)
8008 then
8009 Call := Parent (Context);
8010
8011 elsif Nkind_In (Context, N_Entry_Call_Statement,
8012 N_Function_Call,
8013 N_Procedure_Call_Statement)
8014 then
8015 Call := Context;
8016
8017 else
8018 Formal := Empty;
8019 Call := Empty;
8020 return;
8021 end if;
8022
8023 -- If we have a call to a subprogram look for the parameter. Note that
8024 -- we exclude overloaded calls, since we don't know enough to be sure
8025 -- of giving the right answer in this case.
8026
8027 if Nkind_In (Call, N_Entry_Call_Statement,
8028 N_Function_Call,
8029 N_Procedure_Call_Statement)
8030 then
8031 Call_Nam := Name (Call);
8032
8033 -- A call to a protected or task entry appears as a selected
8034 -- component rather than an expanded name.
8035
8036 if Nkind (Call_Nam) = N_Selected_Component then
8037 Call_Nam := Selector_Name (Call_Nam);
8038 end if;
8039
8040 if Is_Entity_Name (Call_Nam)
8041 and then Present (Entity (Call_Nam))
8042 and then Is_Overloadable (Entity (Call_Nam))
8043 and then not Is_Overloaded (Call_Nam)
8044 then
8045 -- If node is name in call it is not an actual
8046
8047 if N = Call_Nam then
8048 Formal := Empty;
8049 Call := Empty;
8050 return;
8051 end if;
8052
8053 -- Fall here if we are definitely a parameter
8054
8055 Actual := First_Actual (Call);
8056 Formal := First_Formal (Entity (Call_Nam));
8057 while Present (Formal) and then Present (Actual) loop
8058 if Actual = N then
8059 return;
8060
8061 -- An actual that is the prefix in a prefixed call may have
8062 -- been rewritten in the call, after the deferred reference
8063 -- was collected. Check if sloc and kinds and names match.
8064
8065 elsif Sloc (Actual) = Sloc (N)
8066 and then Nkind (Actual) = N_Identifier
8067 and then Nkind (Actual) = Nkind (N)
8068 and then Chars (Actual) = Chars (N)
8069 then
8070 return;
8071
8072 else
8073 Actual := Next_Actual (Actual);
8074 Formal := Next_Formal (Formal);
8075 end if;
8076 end loop;
8077 end if;
8078 end if;
8079
8080 -- Fall through here if we did not find matching actual
8081
8082 Formal := Empty;
8083 Call := Empty;
8084 end Find_Actual;
8085
8086 ---------------------------
8087 -- Find_Body_Discriminal --
8088 ---------------------------
8089
8090 function Find_Body_Discriminal
8091 (Spec_Discriminant : Entity_Id) return Entity_Id
8092 is
8093 Tsk : Entity_Id;
8094 Disc : Entity_Id;
8095
8096 begin
8097 -- If expansion is suppressed, then the scope can be the concurrent type
8098 -- itself rather than a corresponding concurrent record type.
8099
8100 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
8101 Tsk := Scope (Spec_Discriminant);
8102
8103 else
8104 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
8105
8106 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
8107 end if;
8108
8109 -- Find discriminant of original concurrent type, and use its current
8110 -- discriminal, which is the renaming within the task/protected body.
8111
8112 Disc := First_Discriminant (Tsk);
8113 while Present (Disc) loop
8114 if Chars (Disc) = Chars (Spec_Discriminant) then
8115 return Discriminal (Disc);
8116 end if;
8117
8118 Next_Discriminant (Disc);
8119 end loop;
8120
8121 -- That loop should always succeed in finding a matching entry and
8122 -- returning. Fatal error if not.
8123
8124 raise Program_Error;
8125 end Find_Body_Discriminal;
8126
8127 -------------------------------------
8128 -- Find_Corresponding_Discriminant --
8129 -------------------------------------
8130
8131 function Find_Corresponding_Discriminant
8132 (Id : Node_Id;
8133 Typ : Entity_Id) return Entity_Id
8134 is
8135 Par_Disc : Entity_Id;
8136 Old_Disc : Entity_Id;
8137 New_Disc : Entity_Id;
8138
8139 begin
8140 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
8141
8142 -- The original type may currently be private, and the discriminant
8143 -- only appear on its full view.
8144
8145 if Is_Private_Type (Scope (Par_Disc))
8146 and then not Has_Discriminants (Scope (Par_Disc))
8147 and then Present (Full_View (Scope (Par_Disc)))
8148 then
8149 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
8150 else
8151 Old_Disc := First_Discriminant (Scope (Par_Disc));
8152 end if;
8153
8154 if Is_Class_Wide_Type (Typ) then
8155 New_Disc := First_Discriminant (Root_Type (Typ));
8156 else
8157 New_Disc := First_Discriminant (Typ);
8158 end if;
8159
8160 while Present (Old_Disc) and then Present (New_Disc) loop
8161 if Old_Disc = Par_Disc then
8162 return New_Disc;
8163 end if;
8164
8165 Next_Discriminant (Old_Disc);
8166 Next_Discriminant (New_Disc);
8167 end loop;
8168
8169 -- Should always find it
8170
8171 raise Program_Error;
8172 end Find_Corresponding_Discriminant;
8173
8174 -------------------
8175 -- Find_DIC_Type --
8176 -------------------
8177
8178 function Find_DIC_Type (Typ : Entity_Id) return Entity_Id is
8179 Curr_Typ : Entity_Id;
8180 -- The current type being examined in the parent hierarchy traversal
8181
8182 DIC_Typ : Entity_Id;
8183 -- The type which carries the DIC pragma. This variable denotes the
8184 -- partial view when private types are involved.
8185
8186 Par_Typ : Entity_Id;
8187 -- The parent type of the current type. This variable denotes the full
8188 -- view when private types are involved.
8189
8190 begin
8191 -- The input type defines its own DIC pragma, therefore it is the owner
8192
8193 if Has_Own_DIC (Typ) then
8194 DIC_Typ := Typ;
8195
8196 -- Otherwise the DIC pragma is inherited from a parent type
8197
8198 else
8199 pragma Assert (Has_Inherited_DIC (Typ));
8200
8201 -- Climb the parent chain
8202
8203 Curr_Typ := Typ;
8204 loop
8205 -- Inspect the parent type. Do not consider subtypes as they
8206 -- inherit the DIC attributes from their base types.
8207
8208 DIC_Typ := Base_Type (Etype (Curr_Typ));
8209
8210 -- Look at the full view of a private type because the type may
8211 -- have a hidden parent introduced in the full view.
8212
8213 Par_Typ := DIC_Typ;
8214
8215 if Is_Private_Type (Par_Typ)
8216 and then Present (Full_View (Par_Typ))
8217 then
8218 Par_Typ := Full_View (Par_Typ);
8219 end if;
8220
8221 -- Stop the climb once the nearest parent type which defines a DIC
8222 -- pragma of its own is encountered or when the root of the parent
8223 -- chain is reached.
8224
8225 exit when Has_Own_DIC (DIC_Typ) or else Curr_Typ = Par_Typ;
8226
8227 Curr_Typ := Par_Typ;
8228 end loop;
8229 end if;
8230
8231 return DIC_Typ;
8232 end Find_DIC_Type;
8233
8234 ----------------------------------
8235 -- Find_Enclosing_Iterator_Loop --
8236 ----------------------------------
8237
8238 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
8239 Constr : Node_Id;
8240 S : Entity_Id;
8241
8242 begin
8243 -- Traverse the scope chain looking for an iterator loop. Such loops are
8244 -- usually transformed into blocks, hence the use of Original_Node.
8245
8246 S := Id;
8247 while Present (S) and then S /= Standard_Standard loop
8248 if Ekind (S) = E_Loop
8249 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
8250 then
8251 Constr := Original_Node (Label_Construct (Parent (S)));
8252
8253 if Nkind (Constr) = N_Loop_Statement
8254 and then Present (Iteration_Scheme (Constr))
8255 and then Nkind (Iterator_Specification
8256 (Iteration_Scheme (Constr))) =
8257 N_Iterator_Specification
8258 then
8259 return S;
8260 end if;
8261 end if;
8262
8263 S := Scope (S);
8264 end loop;
8265
8266 return Empty;
8267 end Find_Enclosing_Iterator_Loop;
8268
8269 --------------------------
8270 -- Find_Enclosing_Scope --
8271 --------------------------
8272
8273 function Find_Enclosing_Scope (N : Node_Id) return Entity_Id is
8274 Par : Node_Id;
8275
8276 begin
8277 -- Examine the parent chain looking for a construct which defines a
8278 -- scope.
8279
8280 Par := Parent (N);
8281 while Present (Par) loop
8282 case Nkind (Par) is
8283
8284 -- The construct denotes a declaration, the proper scope is its
8285 -- entity.
8286
8287 when N_Entry_Declaration
8288 | N_Expression_Function
8289 | N_Full_Type_Declaration
8290 | N_Generic_Package_Declaration
8291 | N_Generic_Subprogram_Declaration
8292 | N_Package_Declaration
8293 | N_Private_Extension_Declaration
8294 | N_Protected_Type_Declaration
8295 | N_Single_Protected_Declaration
8296 | N_Single_Task_Declaration
8297 | N_Subprogram_Declaration
8298 | N_Task_Type_Declaration
8299 =>
8300 return Defining_Entity (Par);
8301
8302 -- The construct denotes a body, the proper scope is the entity of
8303 -- the corresponding spec or that of the body if the body does not
8304 -- complete a previous declaration.
8305
8306 when N_Entry_Body
8307 | N_Package_Body
8308 | N_Protected_Body
8309 | N_Subprogram_Body
8310 | N_Task_Body
8311 =>
8312 return Unique_Defining_Entity (Par);
8313
8314 -- Special cases
8315
8316 -- Blocks carry either a source or an internally-generated scope,
8317 -- unless the block is a byproduct of exception handling.
8318
8319 when N_Block_Statement =>
8320 if not Exception_Junk (Par) then
8321 return Entity (Identifier (Par));
8322 end if;
8323
8324 -- Loops carry an internally-generated scope
8325
8326 when N_Loop_Statement =>
8327 return Entity (Identifier (Par));
8328
8329 -- Extended return statements carry an internally-generated scope
8330
8331 when N_Extended_Return_Statement =>
8332 return Return_Statement_Entity (Par);
8333
8334 -- A traversal from a subunit continues via the corresponding stub
8335
8336 when N_Subunit =>
8337 Par := Corresponding_Stub (Par);
8338
8339 when others =>
8340 null;
8341 end case;
8342
8343 Par := Parent (Par);
8344 end loop;
8345
8346 return Standard_Standard;
8347 end Find_Enclosing_Scope;
8348
8349 ------------------------------------
8350 -- Find_Loop_In_Conditional_Block --
8351 ------------------------------------
8352
8353 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
8354 Stmt : Node_Id;
8355
8356 begin
8357 Stmt := N;
8358
8359 if Nkind (Stmt) = N_If_Statement then
8360 Stmt := First (Then_Statements (Stmt));
8361 end if;
8362
8363 pragma Assert (Nkind (Stmt) = N_Block_Statement);
8364
8365 -- Inspect the statements of the conditional block. In general the loop
8366 -- should be the first statement in the statement sequence of the block,
8367 -- but the finalization machinery may have introduced extra object
8368 -- declarations.
8369
8370 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
8371 while Present (Stmt) loop
8372 if Nkind (Stmt) = N_Loop_Statement then
8373 return Stmt;
8374 end if;
8375
8376 Next (Stmt);
8377 end loop;
8378
8379 -- The expansion of attribute 'Loop_Entry produced a malformed block
8380
8381 raise Program_Error;
8382 end Find_Loop_In_Conditional_Block;
8383
8384 --------------------------
8385 -- Find_Overlaid_Entity --
8386 --------------------------
8387
8388 procedure Find_Overlaid_Entity
8389 (N : Node_Id;
8390 Ent : out Entity_Id;
8391 Off : out Boolean)
8392 is
8393 Expr : Node_Id;
8394
8395 begin
8396 -- We are looking for one of the two following forms:
8397
8398 -- for X'Address use Y'Address
8399
8400 -- or
8401
8402 -- Const : constant Address := expr;
8403 -- ...
8404 -- for X'Address use Const;
8405
8406 -- In the second case, the expr is either Y'Address, or recursively a
8407 -- constant that eventually references Y'Address.
8408
8409 Ent := Empty;
8410 Off := False;
8411
8412 if Nkind (N) = N_Attribute_Definition_Clause
8413 and then Chars (N) = Name_Address
8414 then
8415 Expr := Expression (N);
8416
8417 -- This loop checks the form of the expression for Y'Address,
8418 -- using recursion to deal with intermediate constants.
8419
8420 loop
8421 -- Check for Y'Address
8422
8423 if Nkind (Expr) = N_Attribute_Reference
8424 and then Attribute_Name (Expr) = Name_Address
8425 then
8426 Expr := Prefix (Expr);
8427 exit;
8428
8429 -- Check for Const where Const is a constant entity
8430
8431 elsif Is_Entity_Name (Expr)
8432 and then Ekind (Entity (Expr)) = E_Constant
8433 then
8434 Expr := Constant_Value (Entity (Expr));
8435
8436 -- Anything else does not need checking
8437
8438 else
8439 return;
8440 end if;
8441 end loop;
8442
8443 -- This loop checks the form of the prefix for an entity, using
8444 -- recursion to deal with intermediate components.
8445
8446 loop
8447 -- Check for Y where Y is an entity
8448
8449 if Is_Entity_Name (Expr) then
8450 Ent := Entity (Expr);
8451 return;
8452
8453 -- Check for components
8454
8455 elsif
8456 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
8457 then
8458 Expr := Prefix (Expr);
8459 Off := True;
8460
8461 -- Anything else does not need checking
8462
8463 else
8464 return;
8465 end if;
8466 end loop;
8467 end if;
8468 end Find_Overlaid_Entity;
8469
8470 -------------------------
8471 -- Find_Parameter_Type --
8472 -------------------------
8473
8474 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
8475 begin
8476 if Nkind (Param) /= N_Parameter_Specification then
8477 return Empty;
8478
8479 -- For an access parameter, obtain the type from the formal entity
8480 -- itself, because access to subprogram nodes do not carry a type.
8481 -- Shouldn't we always use the formal entity ???
8482
8483 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
8484 return Etype (Defining_Identifier (Param));
8485
8486 else
8487 return Etype (Parameter_Type (Param));
8488 end if;
8489 end Find_Parameter_Type;
8490
8491 -----------------------------------
8492 -- Find_Placement_In_State_Space --
8493 -----------------------------------
8494
8495 procedure Find_Placement_In_State_Space
8496 (Item_Id : Entity_Id;
8497 Placement : out State_Space_Kind;
8498 Pack_Id : out Entity_Id)
8499 is
8500 Context : Entity_Id;
8501
8502 begin
8503 -- Assume that the item does not appear in the state space of a package
8504
8505 Placement := Not_In_Package;
8506 Pack_Id := Empty;
8507
8508 -- Climb the scope stack and examine the enclosing context
8509
8510 Context := Scope (Item_Id);
8511 while Present (Context) and then Context /= Standard_Standard loop
8512 if Is_Package_Or_Generic_Package (Context) then
8513 Pack_Id := Context;
8514
8515 -- A package body is a cut off point for the traversal as the item
8516 -- cannot be visible to the outside from this point on. Note that
8517 -- this test must be done first as a body is also classified as a
8518 -- private part.
8519
8520 if In_Package_Body (Context) then
8521 Placement := Body_State_Space;
8522 return;
8523
8524 -- The private part of a package is a cut off point for the
8525 -- traversal as the item cannot be visible to the outside from
8526 -- this point on.
8527
8528 elsif In_Private_Part (Context) then
8529 Placement := Private_State_Space;
8530 return;
8531
8532 -- When the item appears in the visible state space of a package,
8533 -- continue to climb the scope stack as this may not be the final
8534 -- state space.
8535
8536 else
8537 Placement := Visible_State_Space;
8538
8539 -- The visible state space of a child unit acts as the proper
8540 -- placement of an item.
8541
8542 if Is_Child_Unit (Context) then
8543 return;
8544 end if;
8545 end if;
8546
8547 -- The item or its enclosing package appear in a construct that has
8548 -- no state space.
8549
8550 else
8551 Placement := Not_In_Package;
8552 return;
8553 end if;
8554
8555 Context := Scope (Context);
8556 end loop;
8557 end Find_Placement_In_State_Space;
8558
8559 -----------------------
8560 -- Find_Primitive_Eq --
8561 -----------------------
8562
8563 function Find_Primitive_Eq (Typ : Entity_Id) return Entity_Id is
8564 function Find_Eq_Prim (Prims_List : Elist_Id) return Entity_Id;
8565 -- Search for the equality primitive; return Empty if the primitive is
8566 -- not found.
8567
8568 ------------------
8569 -- Find_Eq_Prim --
8570 ------------------
8571
8572 function Find_Eq_Prim (Prims_List : Elist_Id) return Entity_Id is
8573 Prim : Entity_Id;
8574 Prim_Elmt : Elmt_Id;
8575
8576 begin
8577 Prim_Elmt := First_Elmt (Prims_List);
8578 while Present (Prim_Elmt) loop
8579 Prim := Node (Prim_Elmt);
8580
8581 -- Locate primitive equality with the right signature
8582
8583 if Chars (Prim) = Name_Op_Eq
8584 and then Etype (First_Formal (Prim)) =
8585 Etype (Next_Formal (First_Formal (Prim)))
8586 and then Base_Type (Etype (Prim)) = Standard_Boolean
8587 then
8588 return Prim;
8589 end if;
8590
8591 Next_Elmt (Prim_Elmt);
8592 end loop;
8593
8594 return Empty;
8595 end Find_Eq_Prim;
8596
8597 -- Local Variables
8598
8599 Eq_Prim : Entity_Id;
8600 Full_Type : Entity_Id;
8601
8602 -- Start of processing for Find_Primitive_Eq
8603
8604 begin
8605 if Is_Private_Type (Typ) then
8606 Full_Type := Underlying_Type (Typ);
8607 else
8608 Full_Type := Typ;
8609 end if;
8610
8611 if No (Full_Type) then
8612 return Empty;
8613 end if;
8614
8615 Full_Type := Base_Type (Full_Type);
8616
8617 -- When the base type itself is private, use the full view
8618
8619 if Is_Private_Type (Full_Type) then
8620 Full_Type := Underlying_Type (Full_Type);
8621 end if;
8622
8623 if Is_Class_Wide_Type (Full_Type) then
8624 Full_Type := Root_Type (Full_Type);
8625 end if;
8626
8627 if not Is_Tagged_Type (Full_Type) then
8628 Eq_Prim := Find_Eq_Prim (Collect_Primitive_Operations (Typ));
8629
8630 -- If this is an untagged private type completed with a derivation of
8631 -- an untagged private type whose full view is a tagged type, we use
8632 -- the primitive operations of the private parent type (since it does
8633 -- not have a full view, and also because its equality primitive may
8634 -- have been overridden in its untagged full view). If no equality was
8635 -- defined for it then take its dispatching equality primitive.
8636
8637 elsif Inherits_From_Tagged_Full_View (Typ) then
8638 Eq_Prim := Find_Eq_Prim (Collect_Primitive_Operations (Typ));
8639
8640 if No (Eq_Prim) then
8641 Eq_Prim := Find_Eq_Prim (Primitive_Operations (Full_Type));
8642 end if;
8643
8644 else
8645 Eq_Prim := Find_Eq_Prim (Primitive_Operations (Full_Type));
8646 end if;
8647
8648 return Eq_Prim;
8649 end Find_Primitive_Eq;
8650
8651 ------------------------
8652 -- Find_Specific_Type --
8653 ------------------------
8654
8655 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
8656 Typ : Entity_Id := Root_Type (CW);
8657
8658 begin
8659 if Ekind (Typ) = E_Incomplete_Type then
8660 if From_Limited_With (Typ) then
8661 Typ := Non_Limited_View (Typ);
8662 else
8663 Typ := Full_View (Typ);
8664 end if;
8665 end if;
8666
8667 if Is_Private_Type (Typ)
8668 and then not Is_Tagged_Type (Typ)
8669 and then Present (Full_View (Typ))
8670 then
8671 return Full_View (Typ);
8672 else
8673 return Typ;
8674 end if;
8675 end Find_Specific_Type;
8676
8677 -----------------------------
8678 -- Find_Static_Alternative --
8679 -----------------------------
8680
8681 function Find_Static_Alternative (N : Node_Id) return Node_Id is
8682 Expr : constant Node_Id := Expression (N);
8683 Val : constant Uint := Expr_Value (Expr);
8684 Alt : Node_Id;
8685 Choice : Node_Id;
8686
8687 begin
8688 Alt := First (Alternatives (N));
8689
8690 Search : loop
8691 if Nkind (Alt) /= N_Pragma then
8692 Choice := First (Discrete_Choices (Alt));
8693 while Present (Choice) loop
8694
8695 -- Others choice, always matches
8696
8697 if Nkind (Choice) = N_Others_Choice then
8698 exit Search;
8699
8700 -- Range, check if value is in the range
8701
8702 elsif Nkind (Choice) = N_Range then
8703 exit Search when
8704 Val >= Expr_Value (Low_Bound (Choice))
8705 and then
8706 Val <= Expr_Value (High_Bound (Choice));
8707
8708 -- Choice is a subtype name. Note that we know it must
8709 -- be a static subtype, since otherwise it would have
8710 -- been diagnosed as illegal.
8711
8712 elsif Is_Entity_Name (Choice)
8713 and then Is_Type (Entity (Choice))
8714 then
8715 exit Search when Is_In_Range (Expr, Etype (Choice),
8716 Assume_Valid => False);
8717
8718 -- Choice is a subtype indication
8719
8720 elsif Nkind (Choice) = N_Subtype_Indication then
8721 declare
8722 C : constant Node_Id := Constraint (Choice);
8723 R : constant Node_Id := Range_Expression (C);
8724
8725 begin
8726 exit Search when
8727 Val >= Expr_Value (Low_Bound (R))
8728 and then
8729 Val <= Expr_Value (High_Bound (R));
8730 end;
8731
8732 -- Choice is a simple expression
8733
8734 else
8735 exit Search when Val = Expr_Value (Choice);
8736 end if;
8737
8738 Next (Choice);
8739 end loop;
8740 end if;
8741
8742 Next (Alt);
8743 pragma Assert (Present (Alt));
8744 end loop Search;
8745
8746 -- The above loop *must* terminate by finding a match, since we know the
8747 -- case statement is valid, and the value of the expression is known at
8748 -- compile time. When we fall out of the loop, Alt points to the
8749 -- alternative that we know will be selected at run time.
8750
8751 return Alt;
8752 end Find_Static_Alternative;
8753
8754 ------------------
8755 -- First_Actual --
8756 ------------------
8757
8758 function First_Actual (Node : Node_Id) return Node_Id is
8759 N : Node_Id;
8760
8761 begin
8762 if No (Parameter_Associations (Node)) then
8763 return Empty;
8764 end if;
8765
8766 N := First (Parameter_Associations (Node));
8767
8768 if Nkind (N) = N_Parameter_Association then
8769 return First_Named_Actual (Node);
8770 else
8771 return N;
8772 end if;
8773 end First_Actual;
8774
8775 ------------------
8776 -- First_Global --
8777 ------------------
8778
8779 function First_Global
8780 (Subp : Entity_Id;
8781 Global_Mode : Name_Id;
8782 Refined : Boolean := False) return Node_Id
8783 is
8784 function First_From_Global_List
8785 (List : Node_Id;
8786 Global_Mode : Name_Id := Name_Input) return Entity_Id;
8787 -- Get the first item with suitable mode from List
8788
8789 ----------------------------
8790 -- First_From_Global_List --
8791 ----------------------------
8792
8793 function First_From_Global_List
8794 (List : Node_Id;
8795 Global_Mode : Name_Id := Name_Input) return Entity_Id
8796 is
8797 Assoc : Node_Id;
8798
8799 begin
8800 -- Empty list (no global items)
8801
8802 if Nkind (List) = N_Null then
8803 return Empty;
8804
8805 -- Single global item declaration (only input items)
8806
8807 elsif Nkind_In (List, N_Expanded_Name, N_Identifier) then
8808 if Global_Mode = Name_Input then
8809 return List;
8810 else
8811 return Empty;
8812 end if;
8813
8814 -- Simple global list (only input items) or moded global list
8815 -- declaration.
8816
8817 elsif Nkind (List) = N_Aggregate then
8818 if Present (Expressions (List)) then
8819 if Global_Mode = Name_Input then
8820 return First (Expressions (List));
8821 else
8822 return Empty;
8823 end if;
8824
8825 else
8826 Assoc := First (Component_Associations (List));
8827 while Present (Assoc) loop
8828
8829 -- When we find the desired mode in an association, call
8830 -- recursively First_From_Global_List as if the mode was
8831 -- Name_Input, in order to reuse the existing machinery
8832 -- for the other cases.
8833
8834 if Chars (First (Choices (Assoc))) = Global_Mode then
8835 return First_From_Global_List (Expression (Assoc));
8836 end if;
8837
8838 Next (Assoc);
8839 end loop;
8840
8841 return Empty;
8842 end if;
8843
8844 -- To accommodate partial decoration of disabled SPARK features,
8845 -- this routine may be called with illegal input. If this is the
8846 -- case, do not raise Program_Error.
8847
8848 else
8849 return Empty;
8850 end if;
8851 end First_From_Global_List;
8852
8853 -- Local variables
8854
8855 Global : Node_Id := Empty;
8856 Body_Id : Entity_Id;
8857
8858 -- Start of processing for First_Global
8859
8860 begin
8861 pragma Assert (Nam_In (Global_Mode, Name_In_Out,
8862 Name_Input,
8863 Name_Output,
8864 Name_Proof_In));
8865
8866 -- Retrieve the suitable pragma Global or Refined_Global. In the second
8867 -- case, it can only be located on the body entity.
8868
8869 if Refined then
8870 if Is_Subprogram_Or_Generic_Subprogram (Subp) then
8871 Body_Id := Subprogram_Body_Entity (Subp);
8872
8873 elsif Is_Entry (Subp) or else Is_Task_Type (Subp) then
8874 Body_Id := Corresponding_Body (Parent (Subp));
8875
8876 -- ??? It should be possible to retrieve the Refined_Global on the
8877 -- task body associated to the task object. This is not yet possible.
8878
8879 elsif Is_Single_Task_Object (Subp) then
8880 Body_Id := Empty;
8881
8882 else
8883 Body_Id := Empty;
8884 end if;
8885
8886 if Present (Body_Id) then
8887 Global := Get_Pragma (Body_Id, Pragma_Refined_Global);
8888 end if;
8889 else
8890 Global := Get_Pragma (Subp, Pragma_Global);
8891 end if;
8892
8893 -- No corresponding global if pragma is not present
8894
8895 if No (Global) then
8896 return Empty;
8897
8898 -- Otherwise retrieve the corresponding list of items depending on the
8899 -- Global_Mode.
8900
8901 else
8902 return First_From_Global_List
8903 (Expression (Get_Argument (Global, Subp)), Global_Mode);
8904 end if;
8905 end First_Global;
8906
8907 -------------
8908 -- Fix_Msg --
8909 -------------
8910
8911 function Fix_Msg (Id : Entity_Id; Msg : String) return String is
8912 Is_Task : constant Boolean :=
8913 Ekind_In (Id, E_Task_Body, E_Task_Type)
8914 or else Is_Single_Task_Object (Id);
8915 Msg_Last : constant Natural := Msg'Last;
8916 Msg_Index : Natural;
8917 Res : String (Msg'Range) := (others => ' ');
8918 Res_Index : Natural;
8919
8920 begin
8921 -- Copy all characters from the input message Msg to result Res with
8922 -- suitable replacements.
8923
8924 Msg_Index := Msg'First;
8925 Res_Index := Res'First;
8926 while Msg_Index <= Msg_Last loop
8927
8928 -- Replace "subprogram" with a different word
8929
8930 if Msg_Index <= Msg_Last - 10
8931 and then Msg (Msg_Index .. Msg_Index + 9) = "subprogram"
8932 then
8933 if Ekind_In (Id, E_Entry, E_Entry_Family) then
8934 Res (Res_Index .. Res_Index + 4) := "entry";
8935 Res_Index := Res_Index + 5;
8936
8937 elsif Is_Task then
8938 Res (Res_Index .. Res_Index + 8) := "task type";
8939 Res_Index := Res_Index + 9;
8940
8941 else
8942 Res (Res_Index .. Res_Index + 9) := "subprogram";
8943 Res_Index := Res_Index + 10;
8944 end if;
8945
8946 Msg_Index := Msg_Index + 10;
8947
8948 -- Replace "protected" with a different word
8949
8950 elsif Msg_Index <= Msg_Last - 9
8951 and then Msg (Msg_Index .. Msg_Index + 8) = "protected"
8952 and then Is_Task
8953 then
8954 Res (Res_Index .. Res_Index + 3) := "task";
8955 Res_Index := Res_Index + 4;
8956 Msg_Index := Msg_Index + 9;
8957
8958 -- Otherwise copy the character
8959
8960 else
8961 Res (Res_Index) := Msg (Msg_Index);
8962 Msg_Index := Msg_Index + 1;
8963 Res_Index := Res_Index + 1;
8964 end if;
8965 end loop;
8966
8967 return Res (Res'First .. Res_Index - 1);
8968 end Fix_Msg;
8969
8970 -------------------------
8971 -- From_Nested_Package --
8972 -------------------------
8973
8974 function From_Nested_Package (T : Entity_Id) return Boolean is
8975 Pack : constant Entity_Id := Scope (T);
8976
8977 begin
8978 return
8979 Ekind (Pack) = E_Package
8980 and then not Is_Frozen (Pack)
8981 and then not Scope_Within_Or_Same (Current_Scope, Pack)
8982 and then In_Open_Scopes (Scope (Pack));
8983 end From_Nested_Package;
8984
8985 -----------------------
8986 -- Gather_Components --
8987 -----------------------
8988
8989 procedure Gather_Components
8990 (Typ : Entity_Id;
8991 Comp_List : Node_Id;
8992 Governed_By : List_Id;
8993 Into : Elist_Id;
8994 Report_Errors : out Boolean)
8995 is
8996 Assoc : Node_Id;
8997 Variant : Node_Id;
8998 Discrete_Choice : Node_Id;
8999 Comp_Item : Node_Id;
9000 Discrim : Entity_Id;
9001 Discrim_Name : Node_Id;
9002
9003 type Discriminant_Value_Status is
9004 (Static_Expr, Static_Subtype, Bad);
9005 subtype Good_Discrim_Value_Status is Discriminant_Value_Status
9006 range Static_Expr .. Static_Subtype; -- range excludes Bad
9007
9008 Discrim_Value : Node_Id;
9009 Discrim_Value_Subtype : Node_Id;
9010 Discrim_Value_Status : Discriminant_Value_Status := Bad;
9011 begin
9012 Report_Errors := False;
9013
9014 if No (Comp_List) or else Null_Present (Comp_List) then
9015 return;
9016
9017 elsif Present (Component_Items (Comp_List)) then
9018 Comp_Item := First (Component_Items (Comp_List));
9019
9020 else
9021 Comp_Item := Empty;
9022 end if;
9023
9024 while Present (Comp_Item) loop
9025
9026 -- Skip the tag of a tagged record, the interface tags, as well
9027 -- as all items that are not user components (anonymous types,
9028 -- rep clauses, Parent field, controller field).
9029
9030 if Nkind (Comp_Item) = N_Component_Declaration then
9031 declare
9032 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
9033 begin
9034 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
9035 Append_Elmt (Comp, Into);
9036 end if;
9037 end;
9038 end if;
9039
9040 Next (Comp_Item);
9041 end loop;
9042
9043 if No (Variant_Part (Comp_List)) then
9044 return;
9045 else
9046 Discrim_Name := Name (Variant_Part (Comp_List));
9047 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
9048 end if;
9049
9050 -- Look for the discriminant that governs this variant part.
9051 -- The discriminant *must* be in the Governed_By List
9052
9053 Assoc := First (Governed_By);
9054 Find_Constraint : loop
9055 Discrim := First (Choices (Assoc));
9056 exit Find_Constraint when
9057 Chars (Discrim_Name) = Chars (Discrim)
9058 or else
9059 (Present (Corresponding_Discriminant (Entity (Discrim)))
9060 and then Chars (Corresponding_Discriminant
9061 (Entity (Discrim))) = Chars (Discrim_Name))
9062 or else
9063 Chars (Original_Record_Component (Entity (Discrim))) =
9064 Chars (Discrim_Name);
9065
9066 if No (Next (Assoc)) then
9067 if not Is_Constrained (Typ) and then Is_Derived_Type (Typ) then
9068
9069 -- If the type is a tagged type with inherited discriminants,
9070 -- use the stored constraint on the parent in order to find
9071 -- the values of discriminants that are otherwise hidden by an
9072 -- explicit constraint. Renamed discriminants are handled in
9073 -- the code above.
9074
9075 -- If several parent discriminants are renamed by a single
9076 -- discriminant of the derived type, the call to obtain the
9077 -- Corresponding_Discriminant field only retrieves the last
9078 -- of them. We recover the constraint on the others from the
9079 -- Stored_Constraint as well.
9080
9081 -- An inherited discriminant may have been constrained in a
9082 -- later ancestor (not the immediate parent) so we must examine
9083 -- the stored constraint of all of them to locate the inherited
9084 -- value.
9085
9086 declare
9087 C : Elmt_Id;
9088 D : Entity_Id;
9089 T : Entity_Id := Typ;
9090
9091 begin
9092 while Is_Derived_Type (T) loop
9093 if Present (Stored_Constraint (T)) then
9094 D := First_Discriminant (Etype (T));
9095 C := First_Elmt (Stored_Constraint (T));
9096 while Present (D) and then Present (C) loop
9097 if Chars (Discrim_Name) = Chars (D) then
9098 if Is_Entity_Name (Node (C))
9099 and then Entity (Node (C)) = Entity (Discrim)
9100 then
9101 -- D is renamed by Discrim, whose value is
9102 -- given in Assoc.
9103
9104 null;
9105
9106 else
9107 Assoc :=
9108 Make_Component_Association (Sloc (Typ),
9109 New_List
9110 (New_Occurrence_Of (D, Sloc (Typ))),
9111 Duplicate_Subexpr_No_Checks (Node (C)));
9112 end if;
9113
9114 exit Find_Constraint;
9115 end if;
9116
9117 Next_Discriminant (D);
9118 Next_Elmt (C);
9119 end loop;
9120 end if;
9121
9122 -- Discriminant may be inherited from ancestor
9123
9124 T := Etype (T);
9125 end loop;
9126 end;
9127 end if;
9128 end if;
9129
9130 if No (Next (Assoc)) then
9131 Error_Msg_NE
9132 (" missing value for discriminant&",
9133 First (Governed_By), Discrim_Name);
9134
9135 Report_Errors := True;
9136 return;
9137 end if;
9138
9139 Next (Assoc);
9140 end loop Find_Constraint;
9141
9142 Discrim_Value := Expression (Assoc);
9143 if Is_OK_Static_Expression (Discrim_Value) then
9144 Discrim_Value_Status := Static_Expr;
9145 else
9146 if Ada_Version >= Ada_2020 then
9147 if Original_Node (Discrim_Value) /= Discrim_Value
9148 and then Nkind (Discrim_Value) = N_Type_Conversion
9149 and then Etype (Original_Node (Discrim_Value))
9150 = Etype (Expression (Discrim_Value))
9151 then
9152 Discrim_Value_Subtype := Etype (Original_Node (Discrim_Value));
9153 -- An unhelpful (for this code) type conversion may be
9154 -- introduced in some cases; deal with it.
9155 else
9156 Discrim_Value_Subtype := Etype (Discrim_Value);
9157 end if;
9158
9159 if Is_OK_Static_Subtype (Discrim_Value_Subtype) and then
9160 not Is_Null_Range (Type_Low_Bound (Discrim_Value_Subtype),
9161 Type_High_Bound (Discrim_Value_Subtype))
9162 then
9163 -- Is_Null_Range test doesn't account for predicates, as in
9164 -- subtype Null_By_Predicate is Natural
9165 -- with Static_Predicate => Null_By_Predicate < 0;
9166 -- so test for that null case separately.
9167
9168 if (not Has_Static_Predicate (Discrim_Value_Subtype))
9169 or else Present (First (Static_Discrete_Predicate
9170 (Discrim_Value_Subtype)))
9171 then
9172 Discrim_Value_Status := Static_Subtype;
9173 end if;
9174 end if;
9175 end if;
9176
9177 if Discrim_Value_Status = Bad then
9178
9179 -- If the variant part is governed by a discriminant of the type
9180 -- this is an error. If the variant part and the discriminant are
9181 -- inherited from an ancestor this is legal (AI05-220) unless the
9182 -- components are being gathered for an aggregate, in which case
9183 -- the caller must check Report_Errors.
9184 --
9185 -- In Ada 2020 the above rules are relaxed. A nonstatic governing
9186 -- discriminant is OK as long as it has a static subtype and
9187 -- every value of that subtype (and there must be at least one)
9188 -- selects the same variant.
9189
9190 if Scope (Original_Record_Component
9191 ((Entity (First (Choices (Assoc)))))) = Typ
9192 then
9193 if Ada_Version >= Ada_2020 then
9194 Error_Msg_FE
9195 ("value for discriminant & must be static or " &
9196 "discriminant's nominal subtype must be static " &
9197 "and non-null!",
9198 Discrim_Value, Discrim);
9199 else
9200 Error_Msg_FE
9201 ("value for discriminant & must be static!",
9202 Discrim_Value, Discrim);
9203 end if;
9204 Why_Not_Static (Discrim_Value);
9205 end if;
9206
9207 Report_Errors := True;
9208 return;
9209 end if;
9210 end if;
9211
9212 Search_For_Discriminant_Value : declare
9213 Low : Node_Id;
9214 High : Node_Id;
9215
9216 UI_High : Uint;
9217 UI_Low : Uint;
9218 UI_Discrim_Value : Uint;
9219
9220 begin
9221 case Good_Discrim_Value_Status'(Discrim_Value_Status) is
9222 when Static_Expr =>
9223 UI_Discrim_Value := Expr_Value (Discrim_Value);
9224 when Static_Subtype =>
9225 -- Arbitrarily pick one value of the subtype and look
9226 -- for the variant associated with that value; we will
9227 -- check later that the same variant is associated with
9228 -- all of the other values of the subtype.
9229 if Has_Static_Predicate (Discrim_Value_Subtype) then
9230 declare
9231 Range_Or_Expr : constant Node_Id :=
9232 First (Static_Discrete_Predicate
9233 (Discrim_Value_Subtype));
9234 begin
9235 if Nkind (Range_Or_Expr) = N_Range then
9236 UI_Discrim_Value :=
9237 Expr_Value (Low_Bound (Range_Or_Expr));
9238 else
9239 UI_Discrim_Value := Expr_Value (Range_Or_Expr);
9240 end if;
9241 end;
9242 else
9243 UI_Discrim_Value
9244 := Expr_Value (Type_Low_Bound (Discrim_Value_Subtype));
9245 end if;
9246 end case;
9247
9248 Find_Discrete_Value : while Present (Variant) loop
9249
9250 -- If a choice is a subtype with a static predicate, it must
9251 -- be rewritten as an explicit list of non-predicated choices.
9252
9253 Expand_Static_Predicates_In_Choices (Variant);
9254
9255 Discrete_Choice := First (Discrete_Choices (Variant));
9256 while Present (Discrete_Choice) loop
9257 exit Find_Discrete_Value when
9258 Nkind (Discrete_Choice) = N_Others_Choice;
9259
9260 Get_Index_Bounds (Discrete_Choice, Low, High);
9261
9262 UI_Low := Expr_Value (Low);
9263 UI_High := Expr_Value (High);
9264
9265 exit Find_Discrete_Value when
9266 UI_Low <= UI_Discrim_Value
9267 and then
9268 UI_High >= UI_Discrim_Value;
9269
9270 Next (Discrete_Choice);
9271 end loop;
9272
9273 Next_Non_Pragma (Variant);
9274 end loop Find_Discrete_Value;
9275 end Search_For_Discriminant_Value;
9276
9277 -- The case statement must include a variant that corresponds to the
9278 -- value of the discriminant, unless the discriminant type has a
9279 -- static predicate. In that case the absence of an others_choice that
9280 -- would cover this value becomes a run-time error (3.8.1 (21.1/2)).
9281
9282 if No (Variant)
9283 and then not Has_Static_Predicate (Etype (Discrim_Name))
9284 then
9285 Error_Msg_NE
9286 ("value of discriminant & is out of range", Discrim_Value, Discrim);
9287 Report_Errors := True;
9288 return;
9289 end if;
9290
9291 -- If we have found the corresponding choice, recursively add its
9292 -- components to the Into list. The nested components are part of
9293 -- the same record type.
9294
9295 if Present (Variant) then
9296 if Discrim_Value_Status = Static_Subtype then
9297 declare
9298 Discrim_Value_Subtype_Intervals
9299 : constant Interval_Lists.Discrete_Interval_List
9300 := Interval_Lists.Type_Intervals (Discrim_Value_Subtype);
9301
9302 Variant_Intervals
9303 : constant Interval_Lists.Discrete_Interval_List
9304 := Interval_Lists.Choice_List_Intervals
9305 (Discrete_Choices => Discrete_Choices (Variant));
9306 begin
9307 if not Interval_Lists.Is_Subset
9308 (Subset => Discrim_Value_Subtype_Intervals,
9309 Of_Set => Variant_Intervals)
9310 then
9311 Error_Msg_NE
9312 ("no single variant is associated with all values of " &
9313 "the subtype of discriminant value &",
9314 Discrim_Value, Discrim);
9315 Report_Errors := True;
9316 return;
9317 end if;
9318 end;
9319 end if;
9320
9321 Gather_Components
9322 (Typ, Component_List (Variant), Governed_By, Into, Report_Errors);
9323 end if;
9324 end Gather_Components;
9325
9326 -----------------------
9327 -- Get_Accessibility --
9328 -----------------------
9329
9330 function Get_Accessibility (E : Entity_Id) return Node_Id is
9331 begin
9332 -- When minimum accessibility is set for E then we utilize it - except
9333 -- in a few edge cases like the expansion of select statements where
9334 -- generated subprogram may attempt to unnecessarily use a minimum
9335 -- accessibility object declared outside of scope.
9336
9337 -- To avoid these situations where expansion may get complex we verify
9338 -- that the minimum accessibility object is within scope.
9339
9340 if Ekind (E) in Formal_Kind
9341 and then Present (Minimum_Accessibility (E))
9342 and then In_Open_Scopes (Scope (Minimum_Accessibility (E)))
9343 then
9344 return Minimum_Accessibility (E);
9345 end if;
9346
9347 return Extra_Accessibility (E);
9348 end Get_Accessibility;
9349
9350 ------------------------
9351 -- Get_Actual_Subtype --
9352 ------------------------
9353
9354 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
9355 Typ : constant Entity_Id := Etype (N);
9356 Utyp : Entity_Id := Underlying_Type (Typ);
9357 Decl : Node_Id;
9358 Atyp : Entity_Id;
9359
9360 begin
9361 if No (Utyp) then
9362 Utyp := Typ;
9363 end if;
9364
9365 -- If what we have is an identifier that references a subprogram
9366 -- formal, or a variable or constant object, then we get the actual
9367 -- subtype from the referenced entity if one has been built.
9368
9369 if Nkind (N) = N_Identifier
9370 and then
9371 (Is_Formal (Entity (N))
9372 or else Ekind (Entity (N)) = E_Constant
9373 or else Ekind (Entity (N)) = E_Variable)
9374 and then Present (Actual_Subtype (Entity (N)))
9375 then
9376 return Actual_Subtype (Entity (N));
9377
9378 -- Actual subtype of unchecked union is always itself. We never need
9379 -- the "real" actual subtype. If we did, we couldn't get it anyway
9380 -- because the discriminant is not available. The restrictions on
9381 -- Unchecked_Union are designed to make sure that this is OK.
9382
9383 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
9384 return Typ;
9385
9386 -- Here for the unconstrained case, we must find actual subtype
9387 -- No actual subtype is available, so we must build it on the fly.
9388
9389 -- Checking the type, not the underlying type, for constrainedness
9390 -- seems to be necessary. Maybe all the tests should be on the type???
9391
9392 elsif (not Is_Constrained (Typ))
9393 and then (Is_Array_Type (Utyp)
9394 or else (Is_Record_Type (Utyp)
9395 and then Has_Discriminants (Utyp)))
9396 and then not Has_Unknown_Discriminants (Utyp)
9397 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
9398 then
9399 -- Nothing to do if in spec expression (why not???)
9400
9401 if In_Spec_Expression then
9402 return Typ;
9403
9404 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
9405
9406 -- If the type has no discriminants, there is no subtype to
9407 -- build, even if the underlying type is discriminated.
9408
9409 return Typ;
9410
9411 -- Else build the actual subtype
9412
9413 else
9414 Decl := Build_Actual_Subtype (Typ, N);
9415
9416 -- The call may yield a declaration, or just return the entity
9417
9418 if Decl = Typ then
9419 return Typ;
9420 end if;
9421
9422 Atyp := Defining_Identifier (Decl);
9423
9424 -- If Build_Actual_Subtype generated a new declaration then use it
9425
9426 if Atyp /= Typ then
9427
9428 -- The actual subtype is an Itype, so analyze the declaration,
9429 -- but do not attach it to the tree, to get the type defined.
9430
9431 Set_Parent (Decl, N);
9432 Set_Is_Itype (Atyp);
9433 Analyze (Decl, Suppress => All_Checks);
9434 Set_Associated_Node_For_Itype (Atyp, N);
9435 Set_Has_Delayed_Freeze (Atyp, False);
9436
9437 -- We need to freeze the actual subtype immediately. This is
9438 -- needed, because otherwise this Itype will not get frozen
9439 -- at all, and it is always safe to freeze on creation because
9440 -- any associated types must be frozen at this point.
9441
9442 Freeze_Itype (Atyp, N);
9443 return Atyp;
9444
9445 -- Otherwise we did not build a declaration, so return original
9446
9447 else
9448 return Typ;
9449 end if;
9450 end if;
9451
9452 -- For all remaining cases, the actual subtype is the same as
9453 -- the nominal type.
9454
9455 else
9456 return Typ;
9457 end if;
9458 end Get_Actual_Subtype;
9459
9460 -------------------------------------
9461 -- Get_Actual_Subtype_If_Available --
9462 -------------------------------------
9463
9464 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
9465 Typ : constant Entity_Id := Etype (N);
9466
9467 begin
9468 -- If what we have is an identifier that references a subprogram
9469 -- formal, or a variable or constant object, then we get the actual
9470 -- subtype from the referenced entity if one has been built.
9471
9472 if Nkind (N) = N_Identifier
9473 and then
9474 (Is_Formal (Entity (N))
9475 or else Ekind (Entity (N)) = E_Constant
9476 or else Ekind (Entity (N)) = E_Variable)
9477 and then Present (Actual_Subtype (Entity (N)))
9478 then
9479 return Actual_Subtype (Entity (N));
9480
9481 -- Otherwise the Etype of N is returned unchanged
9482
9483 else
9484 return Typ;
9485 end if;
9486 end Get_Actual_Subtype_If_Available;
9487
9488 ------------------------
9489 -- Get_Body_From_Stub --
9490 ------------------------
9491
9492 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
9493 begin
9494 return Proper_Body (Unit (Library_Unit (N)));
9495 end Get_Body_From_Stub;
9496
9497 ---------------------
9498 -- Get_Cursor_Type --
9499 ---------------------
9500
9501 function Get_Cursor_Type
9502 (Aspect : Node_Id;
9503 Typ : Entity_Id) return Entity_Id
9504 is
9505 Assoc : Node_Id;
9506 Func : Entity_Id;
9507 First_Op : Entity_Id;
9508 Cursor : Entity_Id;
9509
9510 begin
9511 -- If error already detected, return
9512
9513 if Error_Posted (Aspect) then
9514 return Any_Type;
9515 end if;
9516
9517 -- The cursor type for an Iterable aspect is the return type of a
9518 -- non-overloaded First primitive operation. Locate association for
9519 -- First.
9520
9521 Assoc := First (Component_Associations (Expression (Aspect)));
9522 First_Op := Any_Id;
9523 while Present (Assoc) loop
9524 if Chars (First (Choices (Assoc))) = Name_First then
9525 First_Op := Expression (Assoc);
9526 exit;
9527 end if;
9528
9529 Next (Assoc);
9530 end loop;
9531
9532 if First_Op = Any_Id then
9533 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
9534 return Any_Type;
9535
9536 elsif not Analyzed (First_Op) then
9537 Analyze (First_Op);
9538 end if;
9539
9540 Cursor := Any_Type;
9541
9542 -- Locate function with desired name and profile in scope of type
9543 -- In the rare case where the type is an integer type, a base type
9544 -- is created for it, check that the base type of the first formal
9545 -- of First matches the base type of the domain.
9546
9547 Func := First_Entity (Scope (Typ));
9548 while Present (Func) loop
9549 if Chars (Func) = Chars (First_Op)
9550 and then Ekind (Func) = E_Function
9551 and then Present (First_Formal (Func))
9552 and then Base_Type (Etype (First_Formal (Func))) = Base_Type (Typ)
9553 and then No (Next_Formal (First_Formal (Func)))
9554 then
9555 if Cursor /= Any_Type then
9556 Error_Msg_N
9557 ("Operation First for iterable type must be unique", Aspect);
9558 return Any_Type;
9559 else
9560 Cursor := Etype (Func);
9561 end if;
9562 end if;
9563
9564 Next_Entity (Func);
9565 end loop;
9566
9567 -- If not found, no way to resolve remaining primitives
9568
9569 if Cursor = Any_Type then
9570 Error_Msg_N
9571 ("primitive operation for Iterable type must appear in the same "
9572 & "list of declarations as the type", Aspect);
9573 end if;
9574
9575 return Cursor;
9576 end Get_Cursor_Type;
9577
9578 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
9579 begin
9580 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
9581 end Get_Cursor_Type;
9582
9583 -------------------------------
9584 -- Get_Default_External_Name --
9585 -------------------------------
9586
9587 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
9588 begin
9589 Get_Decoded_Name_String (Chars (E));
9590
9591 if Opt.External_Name_Imp_Casing = Uppercase then
9592 Set_Casing (All_Upper_Case);
9593 else
9594 Set_Casing (All_Lower_Case);
9595 end if;
9596
9597 return
9598 Make_String_Literal (Sloc (E),
9599 Strval => String_From_Name_Buffer);
9600 end Get_Default_External_Name;
9601
9602 --------------------------
9603 -- Get_Enclosing_Object --
9604 --------------------------
9605
9606 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
9607 begin
9608 if Is_Entity_Name (N) then
9609 return Entity (N);
9610 else
9611 case Nkind (N) is
9612 when N_Indexed_Component
9613 | N_Selected_Component
9614 | N_Slice
9615 =>
9616 -- If not generating code, a dereference may be left implicit.
9617 -- In thoses cases, return Empty.
9618
9619 if Is_Access_Type (Etype (Prefix (N))) then
9620 return Empty;
9621 else
9622 return Get_Enclosing_Object (Prefix (N));
9623 end if;
9624
9625 when N_Type_Conversion =>
9626 return Get_Enclosing_Object (Expression (N));
9627
9628 when others =>
9629 return Empty;
9630 end case;
9631 end if;
9632 end Get_Enclosing_Object;
9633
9634 ---------------------------
9635 -- Get_Enum_Lit_From_Pos --
9636 ---------------------------
9637
9638 function Get_Enum_Lit_From_Pos
9639 (T : Entity_Id;
9640 Pos : Uint;
9641 Loc : Source_Ptr) return Node_Id
9642 is
9643 Btyp : Entity_Id := Base_Type (T);
9644 Lit : Node_Id;
9645 LLoc : Source_Ptr;
9646
9647 begin
9648 -- In the case where the literal is of type Character, Wide_Character
9649 -- or Wide_Wide_Character or of a type derived from them, there needs
9650 -- to be some special handling since there is no explicit chain of
9651 -- literals to search. Instead, an N_Character_Literal node is created
9652 -- with the appropriate Char_Code and Chars fields.
9653
9654 if Is_Standard_Character_Type (T) then
9655 Set_Character_Literal_Name (UI_To_CC (Pos));
9656
9657 return
9658 Make_Character_Literal (Loc,
9659 Chars => Name_Find,
9660 Char_Literal_Value => Pos);
9661
9662 -- For all other cases, we have a complete table of literals, and
9663 -- we simply iterate through the chain of literal until the one
9664 -- with the desired position value is found.
9665
9666 else
9667 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
9668 Btyp := Full_View (Btyp);
9669 end if;
9670
9671 Lit := First_Literal (Btyp);
9672
9673 -- Position in the enumeration type starts at 0
9674
9675 if UI_To_Int (Pos) < 0 then
9676 raise Constraint_Error;
9677 end if;
9678
9679 for J in 1 .. UI_To_Int (Pos) loop
9680 Next_Literal (Lit);
9681
9682 -- If Lit is Empty, Pos is not in range, so raise Constraint_Error
9683 -- inside the loop to avoid calling Next_Literal on Empty.
9684
9685 if No (Lit) then
9686 raise Constraint_Error;
9687 end if;
9688 end loop;
9689
9690 -- Create a new node from Lit, with source location provided by Loc
9691 -- if not equal to No_Location, or by copying the source location of
9692 -- Lit otherwise.
9693
9694 LLoc := Loc;
9695
9696 if LLoc = No_Location then
9697 LLoc := Sloc (Lit);
9698 end if;
9699
9700 return New_Occurrence_Of (Lit, LLoc);
9701 end if;
9702 end Get_Enum_Lit_From_Pos;
9703
9704 ------------------------
9705 -- Get_Generic_Entity --
9706 ------------------------
9707
9708 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
9709 Ent : constant Entity_Id := Entity (Name (N));
9710 begin
9711 if Present (Renamed_Object (Ent)) then
9712 return Renamed_Object (Ent);
9713 else
9714 return Ent;
9715 end if;
9716 end Get_Generic_Entity;
9717
9718 -------------------------------------
9719 -- Get_Incomplete_View_Of_Ancestor --
9720 -------------------------------------
9721
9722 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
9723 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9724 Par_Scope : Entity_Id;
9725 Par_Type : Entity_Id;
9726
9727 begin
9728 -- The incomplete view of an ancestor is only relevant for private
9729 -- derived types in child units.
9730
9731 if not Is_Derived_Type (E)
9732 or else not Is_Child_Unit (Cur_Unit)
9733 then
9734 return Empty;
9735
9736 else
9737 Par_Scope := Scope (Cur_Unit);
9738 if No (Par_Scope) then
9739 return Empty;
9740 end if;
9741
9742 Par_Type := Etype (Base_Type (E));
9743
9744 -- Traverse list of ancestor types until we find one declared in
9745 -- a parent or grandparent unit (two levels seem sufficient).
9746
9747 while Present (Par_Type) loop
9748 if Scope (Par_Type) = Par_Scope
9749 or else Scope (Par_Type) = Scope (Par_Scope)
9750 then
9751 return Par_Type;
9752
9753 elsif not Is_Derived_Type (Par_Type) then
9754 return Empty;
9755
9756 else
9757 Par_Type := Etype (Base_Type (Par_Type));
9758 end if;
9759 end loop;
9760
9761 -- If none found, there is no relevant ancestor type.
9762
9763 return Empty;
9764 end if;
9765 end Get_Incomplete_View_Of_Ancestor;
9766
9767 ----------------------
9768 -- Get_Index_Bounds --
9769 ----------------------
9770
9771 procedure Get_Index_Bounds
9772 (N : Node_Id;
9773 L : out Node_Id;
9774 H : out Node_Id;
9775 Use_Full_View : Boolean := False)
9776 is
9777 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id;
9778 -- Obtain the scalar range of type Typ. If flag Use_Full_View is set and
9779 -- Typ qualifies, the scalar range is obtained from the full view of the
9780 -- type.
9781
9782 --------------------------
9783 -- Scalar_Range_Of_Type --
9784 --------------------------
9785
9786 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id is
9787 T : Entity_Id := Typ;
9788
9789 begin
9790 if Use_Full_View and then Present (Full_View (T)) then
9791 T := Full_View (T);
9792 end if;
9793
9794 return Scalar_Range (T);
9795 end Scalar_Range_Of_Type;
9796
9797 -- Local variables
9798
9799 Kind : constant Node_Kind := Nkind (N);
9800 Rng : Node_Id;
9801
9802 -- Start of processing for Get_Index_Bounds
9803
9804 begin
9805 if Kind = N_Range then
9806 L := Low_Bound (N);
9807 H := High_Bound (N);
9808
9809 elsif Kind = N_Subtype_Indication then
9810 Rng := Range_Expression (Constraint (N));
9811
9812 if Rng = Error then
9813 L := Error;
9814 H := Error;
9815 return;
9816
9817 else
9818 L := Low_Bound (Range_Expression (Constraint (N)));
9819 H := High_Bound (Range_Expression (Constraint (N)));
9820 end if;
9821
9822 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9823 Rng := Scalar_Range_Of_Type (Entity (N));
9824
9825 if Error_Posted (Rng) then
9826 L := Error;
9827 H := Error;
9828
9829 elsif Nkind (Rng) = N_Subtype_Indication then
9830 Get_Index_Bounds (Rng, L, H);
9831
9832 else
9833 L := Low_Bound (Rng);
9834 H := High_Bound (Rng);
9835 end if;
9836
9837 else
9838 -- N is an expression, indicating a range with one value
9839
9840 L := N;
9841 H := N;
9842 end if;
9843 end Get_Index_Bounds;
9844
9845 -----------------------------
9846 -- Get_Interfacing_Aspects --
9847 -----------------------------
9848
9849 procedure Get_Interfacing_Aspects
9850 (Iface_Asp : Node_Id;
9851 Conv_Asp : out Node_Id;
9852 EN_Asp : out Node_Id;
9853 Expo_Asp : out Node_Id;
9854 Imp_Asp : out Node_Id;
9855 LN_Asp : out Node_Id;
9856 Do_Checks : Boolean := False)
9857 is
9858 procedure Save_Or_Duplication_Error
9859 (Asp : Node_Id;
9860 To : in out Node_Id);
9861 -- Save the value of aspect Asp in node To. If To already has a value,
9862 -- then this is considered a duplicate use of aspect. Emit an error if
9863 -- flag Do_Checks is set.
9864
9865 -------------------------------
9866 -- Save_Or_Duplication_Error --
9867 -------------------------------
9868
9869 procedure Save_Or_Duplication_Error
9870 (Asp : Node_Id;
9871 To : in out Node_Id)
9872 is
9873 begin
9874 -- Detect an extra aspect and issue an error
9875
9876 if Present (To) then
9877 if Do_Checks then
9878 Error_Msg_Name_1 := Chars (Identifier (Asp));
9879 Error_Msg_Sloc := Sloc (To);
9880 Error_Msg_N ("aspect % previously given #", Asp);
9881 end if;
9882
9883 -- Otherwise capture the aspect
9884
9885 else
9886 To := Asp;
9887 end if;
9888 end Save_Or_Duplication_Error;
9889
9890 -- Local variables
9891
9892 Asp : Node_Id;
9893 Asp_Id : Aspect_Id;
9894
9895 -- The following variables capture each individual aspect
9896
9897 Conv : Node_Id := Empty;
9898 EN : Node_Id := Empty;
9899 Expo : Node_Id := Empty;
9900 Imp : Node_Id := Empty;
9901 LN : Node_Id := Empty;
9902
9903 -- Start of processing for Get_Interfacing_Aspects
9904
9905 begin
9906 -- The input interfacing aspect should reside in an aspect specification
9907 -- list.
9908
9909 pragma Assert (Is_List_Member (Iface_Asp));
9910
9911 -- Examine the aspect specifications of the related entity. Find and
9912 -- capture all interfacing aspects. Detect duplicates and emit errors
9913 -- if applicable.
9914
9915 Asp := First (List_Containing (Iface_Asp));
9916 while Present (Asp) loop
9917 Asp_Id := Get_Aspect_Id (Asp);
9918
9919 if Asp_Id = Aspect_Convention then
9920 Save_Or_Duplication_Error (Asp, Conv);
9921
9922 elsif Asp_Id = Aspect_External_Name then
9923 Save_Or_Duplication_Error (Asp, EN);
9924
9925 elsif Asp_Id = Aspect_Export then
9926 Save_Or_Duplication_Error (Asp, Expo);
9927
9928 elsif Asp_Id = Aspect_Import then
9929 Save_Or_Duplication_Error (Asp, Imp);
9930
9931 elsif Asp_Id = Aspect_Link_Name then
9932 Save_Or_Duplication_Error (Asp, LN);
9933 end if;
9934
9935 Next (Asp);
9936 end loop;
9937
9938 Conv_Asp := Conv;
9939 EN_Asp := EN;
9940 Expo_Asp := Expo;
9941 Imp_Asp := Imp;
9942 LN_Asp := LN;
9943 end Get_Interfacing_Aspects;
9944
9945 ---------------------------------
9946 -- Get_Iterable_Type_Primitive --
9947 ---------------------------------
9948
9949 function Get_Iterable_Type_Primitive
9950 (Typ : Entity_Id;
9951 Nam : Name_Id) return Entity_Id
9952 is
9953 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
9954 Assoc : Node_Id;
9955
9956 begin
9957 if No (Funcs) then
9958 return Empty;
9959
9960 else
9961 Assoc := First (Component_Associations (Funcs));
9962 while Present (Assoc) loop
9963 if Chars (First (Choices (Assoc))) = Nam then
9964 return Entity (Expression (Assoc));
9965 end if;
9966
9967 Assoc := Next (Assoc);
9968 end loop;
9969
9970 return Empty;
9971 end if;
9972 end Get_Iterable_Type_Primitive;
9973
9974 ----------------------------------
9975 -- Get_Library_Unit_Name_String --
9976 ----------------------------------
9977
9978 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
9979 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
9980
9981 begin
9982 Get_Unit_Name_String (Unit_Name_Id);
9983
9984 -- Remove seven last character (" (spec)" or " (body)")
9985
9986 Name_Len := Name_Len - 7;
9987 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
9988 end Get_Library_Unit_Name_String;
9989
9990 --------------------------
9991 -- Get_Max_Queue_Length --
9992 --------------------------
9993
9994 function Get_Max_Queue_Length (Id : Entity_Id) return Uint is
9995 pragma Assert (Is_Entry (Id));
9996 Prag : constant Entity_Id := Get_Pragma (Id, Pragma_Max_Queue_Length);
9997 Max : Uint;
9998
9999 begin
10000 -- A value of 0 or -1 represents no maximum specified, and entries and
10001 -- entry families with no Max_Queue_Length aspect or pragma default to
10002 -- it.
10003
10004 if not Present (Prag) then
10005 return Uint_0;
10006 end if;
10007
10008 Max := Expr_Value
10009 (Expression (First (Pragma_Argument_Associations (Prag))));
10010
10011 -- Since -1 and 0 are equivalent, return 0 for instances of -1 for
10012 -- uniformity.
10013
10014 if Max = -1 then
10015 return Uint_0;
10016 end if;
10017
10018 return Max;
10019 end Get_Max_Queue_Length;
10020
10021 ------------------------
10022 -- Get_Name_Entity_Id --
10023 ------------------------
10024
10025 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
10026 begin
10027 return Entity_Id (Get_Name_Table_Int (Id));
10028 end Get_Name_Entity_Id;
10029
10030 ------------------------------
10031 -- Get_Name_From_CTC_Pragma --
10032 ------------------------------
10033
10034 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
10035 Arg : constant Node_Id :=
10036 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
10037 begin
10038 return Strval (Expr_Value_S (Arg));
10039 end Get_Name_From_CTC_Pragma;
10040
10041 -----------------------
10042 -- Get_Parent_Entity --
10043 -----------------------
10044
10045 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
10046 begin
10047 if Nkind (Unit) = N_Package_Body
10048 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
10049 then
10050 return Defining_Entity
10051 (Specification (Instance_Spec (Original_Node (Unit))));
10052 elsif Nkind (Unit) = N_Package_Instantiation then
10053 return Defining_Entity (Specification (Instance_Spec (Unit)));
10054 else
10055 return Defining_Entity (Unit);
10056 end if;
10057 end Get_Parent_Entity;
10058
10059 -------------------
10060 -- Get_Pragma_Id --
10061 -------------------
10062
10063 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
10064 begin
10065 return Get_Pragma_Id (Pragma_Name_Unmapped (N));
10066 end Get_Pragma_Id;
10067
10068 ------------------------
10069 -- Get_Qualified_Name --
10070 ------------------------
10071
10072 function Get_Qualified_Name
10073 (Id : Entity_Id;
10074 Suffix : Entity_Id := Empty) return Name_Id
10075 is
10076 Suffix_Nam : Name_Id := No_Name;
10077
10078 begin
10079 if Present (Suffix) then
10080 Suffix_Nam := Chars (Suffix);
10081 end if;
10082
10083 return Get_Qualified_Name (Chars (Id), Suffix_Nam, Scope (Id));
10084 end Get_Qualified_Name;
10085
10086 function Get_Qualified_Name
10087 (Nam : Name_Id;
10088 Suffix : Name_Id := No_Name;
10089 Scop : Entity_Id := Current_Scope) return Name_Id
10090 is
10091 procedure Add_Scope (S : Entity_Id);
10092 -- Add the fully qualified form of scope S to the name buffer. The
10093 -- format is:
10094 -- s-1__s__
10095
10096 ---------------
10097 -- Add_Scope --
10098 ---------------
10099
10100 procedure Add_Scope (S : Entity_Id) is
10101 begin
10102 if S = Empty then
10103 null;
10104
10105 elsif S = Standard_Standard then
10106 null;
10107
10108 else
10109 Add_Scope (Scope (S));
10110 Get_Name_String_And_Append (Chars (S));
10111 Add_Str_To_Name_Buffer ("__");
10112 end if;
10113 end Add_Scope;
10114
10115 -- Start of processing for Get_Qualified_Name
10116
10117 begin
10118 Name_Len := 0;
10119 Add_Scope (Scop);
10120
10121 -- Append the base name after all scopes have been chained
10122
10123 Get_Name_String_And_Append (Nam);
10124
10125 -- Append the suffix (if present)
10126
10127 if Suffix /= No_Name then
10128 Add_Str_To_Name_Buffer ("__");
10129 Get_Name_String_And_Append (Suffix);
10130 end if;
10131
10132 return Name_Find;
10133 end Get_Qualified_Name;
10134
10135 -----------------------
10136 -- Get_Reason_String --
10137 -----------------------
10138
10139 procedure Get_Reason_String (N : Node_Id) is
10140 begin
10141 if Nkind (N) = N_String_Literal then
10142 Store_String_Chars (Strval (N));
10143
10144 elsif Nkind (N) = N_Op_Concat then
10145 Get_Reason_String (Left_Opnd (N));
10146 Get_Reason_String (Right_Opnd (N));
10147
10148 -- If not of required form, error
10149
10150 else
10151 Error_Msg_N
10152 ("Reason for pragma Warnings has wrong form", N);
10153 Error_Msg_N
10154 ("\must be string literal or concatenation of string literals", N);
10155 return;
10156 end if;
10157 end Get_Reason_String;
10158
10159 --------------------------------
10160 -- Get_Reference_Discriminant --
10161 --------------------------------
10162
10163 function Get_Reference_Discriminant (Typ : Entity_Id) return Entity_Id is
10164 D : Entity_Id;
10165
10166 begin
10167 D := First_Discriminant (Typ);
10168 while Present (D) loop
10169 if Has_Implicit_Dereference (D) then
10170 return D;
10171 end if;
10172 Next_Discriminant (D);
10173 end loop;
10174
10175 return Empty;
10176 end Get_Reference_Discriminant;
10177
10178 ---------------------------
10179 -- Get_Referenced_Object --
10180 ---------------------------
10181
10182 function Get_Referenced_Object (N : Node_Id) return Node_Id is
10183 R : Node_Id;
10184
10185 begin
10186 R := N;
10187 while Is_Entity_Name (R)
10188 and then Present (Renamed_Object (Entity (R)))
10189 loop
10190 R := Renamed_Object (Entity (R));
10191 end loop;
10192
10193 return R;
10194 end Get_Referenced_Object;
10195
10196 ------------------------
10197 -- Get_Renamed_Entity --
10198 ------------------------
10199
10200 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
10201 R : Entity_Id;
10202
10203 begin
10204 R := E;
10205 while Present (Renamed_Entity (R)) loop
10206 R := Renamed_Entity (R);
10207 end loop;
10208
10209 return R;
10210 end Get_Renamed_Entity;
10211
10212 -----------------------
10213 -- Get_Return_Object --
10214 -----------------------
10215
10216 function Get_Return_Object (N : Node_Id) return Entity_Id is
10217 Decl : Node_Id;
10218
10219 begin
10220 Decl := First (Return_Object_Declarations (N));
10221 while Present (Decl) loop
10222 exit when Nkind (Decl) = N_Object_Declaration
10223 and then Is_Return_Object (Defining_Identifier (Decl));
10224 Next (Decl);
10225 end loop;
10226
10227 pragma Assert (Present (Decl));
10228 return Defining_Identifier (Decl);
10229 end Get_Return_Object;
10230
10231 ---------------------------
10232 -- Get_Subprogram_Entity --
10233 ---------------------------
10234
10235 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
10236 Subp : Node_Id;
10237 Subp_Id : Entity_Id;
10238
10239 begin
10240 if Nkind (Nod) = N_Accept_Statement then
10241 Subp := Entry_Direct_Name (Nod);
10242
10243 elsif Nkind (Nod) = N_Slice then
10244 Subp := Prefix (Nod);
10245
10246 else
10247 Subp := Name (Nod);
10248 end if;
10249
10250 -- Strip the subprogram call
10251
10252 loop
10253 if Nkind_In (Subp, N_Explicit_Dereference,
10254 N_Indexed_Component,
10255 N_Selected_Component)
10256 then
10257 Subp := Prefix (Subp);
10258
10259 elsif Nkind_In (Subp, N_Type_Conversion,
10260 N_Unchecked_Type_Conversion)
10261 then
10262 Subp := Expression (Subp);
10263
10264 else
10265 exit;
10266 end if;
10267 end loop;
10268
10269 -- Extract the entity of the subprogram call
10270
10271 if Is_Entity_Name (Subp) then
10272 Subp_Id := Entity (Subp);
10273
10274 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
10275 Subp_Id := Directly_Designated_Type (Subp_Id);
10276 end if;
10277
10278 if Is_Subprogram (Subp_Id) then
10279 return Subp_Id;
10280 else
10281 return Empty;
10282 end if;
10283
10284 -- The search did not find a construct that denotes a subprogram
10285
10286 else
10287 return Empty;
10288 end if;
10289 end Get_Subprogram_Entity;
10290
10291 -----------------------------
10292 -- Get_Task_Body_Procedure --
10293 -----------------------------
10294
10295 function Get_Task_Body_Procedure (E : Entity_Id) return Entity_Id is
10296 begin
10297 -- Note: A task type may be the completion of a private type with
10298 -- discriminants. When performing elaboration checks on a task
10299 -- declaration, the current view of the type may be the private one,
10300 -- and the procedure that holds the body of the task is held in its
10301 -- underlying type.
10302
10303 -- This is an odd function, why not have Task_Body_Procedure do
10304 -- the following digging???
10305
10306 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
10307 end Get_Task_Body_Procedure;
10308
10309 -------------------------
10310 -- Get_User_Defined_Eq --
10311 -------------------------
10312
10313 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
10314 Prim : Elmt_Id;
10315 Op : Entity_Id;
10316
10317 begin
10318 Prim := First_Elmt (Collect_Primitive_Operations (E));
10319 while Present (Prim) loop
10320 Op := Node (Prim);
10321
10322 if Chars (Op) = Name_Op_Eq
10323 and then Etype (Op) = Standard_Boolean
10324 and then Etype (First_Formal (Op)) = E
10325 and then Etype (Next_Formal (First_Formal (Op))) = E
10326 then
10327 return Op;
10328 end if;
10329
10330 Next_Elmt (Prim);
10331 end loop;
10332
10333 return Empty;
10334 end Get_User_Defined_Eq;
10335
10336 ---------------
10337 -- Get_Views --
10338 ---------------
10339
10340 procedure Get_Views
10341 (Typ : Entity_Id;
10342 Priv_Typ : out Entity_Id;
10343 Full_Typ : out Entity_Id;
10344 Full_Base : out Entity_Id;
10345 CRec_Typ : out Entity_Id)
10346 is
10347 IP_View : Entity_Id;
10348
10349 begin
10350 -- Assume that none of the views can be recovered
10351
10352 Priv_Typ := Empty;
10353 Full_Typ := Empty;
10354 Full_Base := Empty;
10355 CRec_Typ := Empty;
10356
10357 -- The input type is the corresponding record type of a protected or a
10358 -- task type.
10359
10360 if Ekind (Typ) = E_Record_Type
10361 and then Is_Concurrent_Record_Type (Typ)
10362 then
10363 CRec_Typ := Typ;
10364 Full_Typ := Corresponding_Concurrent_Type (CRec_Typ);
10365 Full_Base := Base_Type (Full_Typ);
10366 Priv_Typ := Incomplete_Or_Partial_View (Full_Typ);
10367
10368 -- Otherwise the input type denotes an arbitrary type
10369
10370 else
10371 IP_View := Incomplete_Or_Partial_View (Typ);
10372
10373 -- The input type denotes the full view of a private type
10374
10375 if Present (IP_View) then
10376 Priv_Typ := IP_View;
10377 Full_Typ := Typ;
10378
10379 -- The input type is a private type
10380
10381 elsif Is_Private_Type (Typ) then
10382 Priv_Typ := Typ;
10383 Full_Typ := Full_View (Priv_Typ);
10384
10385 -- Otherwise the input type does not have any views
10386
10387 else
10388 Full_Typ := Typ;
10389 end if;
10390
10391 if Present (Full_Typ) then
10392 Full_Base := Base_Type (Full_Typ);
10393
10394 if Ekind_In (Full_Typ, E_Protected_Type, E_Task_Type) then
10395 CRec_Typ := Corresponding_Record_Type (Full_Typ);
10396 end if;
10397 end if;
10398 end if;
10399 end Get_Views;
10400
10401 -----------------------
10402 -- Has_Access_Values --
10403 -----------------------
10404
10405 function Has_Access_Values (T : Entity_Id) return Boolean is
10406 Typ : constant Entity_Id := Underlying_Type (T);
10407
10408 begin
10409 -- Case of a private type which is not completed yet. This can only
10410 -- happen in the case of a generic format type appearing directly, or
10411 -- as a component of the type to which this function is being applied
10412 -- at the top level. Return False in this case, since we certainly do
10413 -- not know that the type contains access types.
10414
10415 if No (Typ) then
10416 return False;
10417
10418 elsif Is_Access_Type (Typ) then
10419 return True;
10420
10421 elsif Is_Array_Type (Typ) then
10422 return Has_Access_Values (Component_Type (Typ));
10423
10424 elsif Is_Record_Type (Typ) then
10425 declare
10426 Comp : Entity_Id;
10427
10428 begin
10429 -- Loop to Check components
10430
10431 Comp := First_Component_Or_Discriminant (Typ);
10432 while Present (Comp) loop
10433
10434 -- Check for access component, tag field does not count, even
10435 -- though it is implemented internally using an access type.
10436
10437 if Has_Access_Values (Etype (Comp))
10438 and then Chars (Comp) /= Name_uTag
10439 then
10440 return True;
10441 end if;
10442
10443 Next_Component_Or_Discriminant (Comp);
10444 end loop;
10445 end;
10446
10447 return False;
10448
10449 else
10450 return False;
10451 end if;
10452 end Has_Access_Values;
10453
10454 ------------------------------
10455 -- Has_Compatible_Alignment --
10456 ------------------------------
10457
10458 function Has_Compatible_Alignment
10459 (Obj : Entity_Id;
10460 Expr : Node_Id;
10461 Layout_Done : Boolean) return Alignment_Result
10462 is
10463 function Has_Compatible_Alignment_Internal
10464 (Obj : Entity_Id;
10465 Expr : Node_Id;
10466 Layout_Done : Boolean;
10467 Default : Alignment_Result) return Alignment_Result;
10468 -- This is the internal recursive function that actually does the work.
10469 -- There is one additional parameter, which says what the result should
10470 -- be if no alignment information is found, and there is no definite
10471 -- indication of compatible alignments. At the outer level, this is set
10472 -- to Unknown, but for internal recursive calls in the case where types
10473 -- are known to be correct, it is set to Known_Compatible.
10474
10475 ---------------------------------------
10476 -- Has_Compatible_Alignment_Internal --
10477 ---------------------------------------
10478
10479 function Has_Compatible_Alignment_Internal
10480 (Obj : Entity_Id;
10481 Expr : Node_Id;
10482 Layout_Done : Boolean;
10483 Default : Alignment_Result) return Alignment_Result
10484 is
10485 Result : Alignment_Result := Known_Compatible;
10486 -- Holds the current status of the result. Note that once a value of
10487 -- Known_Incompatible is set, it is sticky and does not get changed
10488 -- to Unknown (the value in Result only gets worse as we go along,
10489 -- never better).
10490
10491 Offs : Uint := No_Uint;
10492 -- Set to a factor of the offset from the base object when Expr is a
10493 -- selected or indexed component, based on Component_Bit_Offset and
10494 -- Component_Size respectively. A negative value is used to represent
10495 -- a value which is not known at compile time.
10496
10497 procedure Check_Prefix;
10498 -- Checks the prefix recursively in the case where the expression
10499 -- is an indexed or selected component.
10500
10501 procedure Set_Result (R : Alignment_Result);
10502 -- If R represents a worse outcome (unknown instead of known
10503 -- compatible, or known incompatible), then set Result to R.
10504
10505 ------------------
10506 -- Check_Prefix --
10507 ------------------
10508
10509 procedure Check_Prefix is
10510 begin
10511 -- The subtlety here is that in doing a recursive call to check
10512 -- the prefix, we have to decide what to do in the case where we
10513 -- don't find any specific indication of an alignment problem.
10514
10515 -- At the outer level, we normally set Unknown as the result in
10516 -- this case, since we can only set Known_Compatible if we really
10517 -- know that the alignment value is OK, but for the recursive
10518 -- call, in the case where the types match, and we have not
10519 -- specified a peculiar alignment for the object, we are only
10520 -- concerned about suspicious rep clauses, the default case does
10521 -- not affect us, since the compiler will, in the absence of such
10522 -- rep clauses, ensure that the alignment is correct.
10523
10524 if Default = Known_Compatible
10525 or else
10526 (Etype (Obj) = Etype (Expr)
10527 and then (Unknown_Alignment (Obj)
10528 or else
10529 Alignment (Obj) = Alignment (Etype (Obj))))
10530 then
10531 Set_Result
10532 (Has_Compatible_Alignment_Internal
10533 (Obj, Prefix (Expr), Layout_Done, Known_Compatible));
10534
10535 -- In all other cases, we need a full check on the prefix
10536
10537 else
10538 Set_Result
10539 (Has_Compatible_Alignment_Internal
10540 (Obj, Prefix (Expr), Layout_Done, Unknown));
10541 end if;
10542 end Check_Prefix;
10543
10544 ----------------
10545 -- Set_Result --
10546 ----------------
10547
10548 procedure Set_Result (R : Alignment_Result) is
10549 begin
10550 if R > Result then
10551 Result := R;
10552 end if;
10553 end Set_Result;
10554
10555 -- Start of processing for Has_Compatible_Alignment_Internal
10556
10557 begin
10558 -- If Expr is a selected component, we must make sure there is no
10559 -- potentially troublesome component clause and that the record is
10560 -- not packed if the layout is not done.
10561
10562 if Nkind (Expr) = N_Selected_Component then
10563
10564 -- Packing generates unknown alignment if layout is not done
10565
10566 if Is_Packed (Etype (Prefix (Expr))) and then not Layout_Done then
10567 Set_Result (Unknown);
10568 end if;
10569
10570 -- Check prefix and component offset
10571
10572 Check_Prefix;
10573 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
10574
10575 -- If Expr is an indexed component, we must make sure there is no
10576 -- potentially troublesome Component_Size clause and that the array
10577 -- is not bit-packed if the layout is not done.
10578
10579 elsif Nkind (Expr) = N_Indexed_Component then
10580 declare
10581 Typ : constant Entity_Id := Etype (Prefix (Expr));
10582
10583 begin
10584 -- Packing generates unknown alignment if layout is not done
10585
10586 if Is_Bit_Packed_Array (Typ) and then not Layout_Done then
10587 Set_Result (Unknown);
10588 end if;
10589
10590 -- Check prefix and component offset (or at least size)
10591
10592 Check_Prefix;
10593 Offs := Indexed_Component_Bit_Offset (Expr);
10594 if Offs = No_Uint then
10595 Offs := Component_Size (Typ);
10596 end if;
10597 end;
10598 end if;
10599
10600 -- If we have a null offset, the result is entirely determined by
10601 -- the base object and has already been computed recursively.
10602
10603 if Offs = Uint_0 then
10604 null;
10605
10606 -- Case where we know the alignment of the object
10607
10608 elsif Known_Alignment (Obj) then
10609 declare
10610 ObjA : constant Uint := Alignment (Obj);
10611 ExpA : Uint := No_Uint;
10612 SizA : Uint := No_Uint;
10613
10614 begin
10615 -- If alignment of Obj is 1, then we are always OK
10616
10617 if ObjA = 1 then
10618 Set_Result (Known_Compatible);
10619
10620 -- Alignment of Obj is greater than 1, so we need to check
10621
10622 else
10623 -- If we have an offset, see if it is compatible
10624
10625 if Offs /= No_Uint and Offs > Uint_0 then
10626 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
10627 Set_Result (Known_Incompatible);
10628 end if;
10629
10630 -- See if Expr is an object with known alignment
10631
10632 elsif Is_Entity_Name (Expr)
10633 and then Known_Alignment (Entity (Expr))
10634 then
10635 ExpA := Alignment (Entity (Expr));
10636
10637 -- Otherwise, we can use the alignment of the type of
10638 -- Expr given that we already checked for
10639 -- discombobulating rep clauses for the cases of indexed
10640 -- and selected components above.
10641
10642 elsif Known_Alignment (Etype (Expr)) then
10643 ExpA := Alignment (Etype (Expr));
10644
10645 -- Otherwise the alignment is unknown
10646
10647 else
10648 Set_Result (Default);
10649 end if;
10650
10651 -- If we got an alignment, see if it is acceptable
10652
10653 if ExpA /= No_Uint and then ExpA < ObjA then
10654 Set_Result (Known_Incompatible);
10655 end if;
10656
10657 -- If Expr is not a piece of a larger object, see if size
10658 -- is given. If so, check that it is not too small for the
10659 -- required alignment.
10660
10661 if Offs /= No_Uint then
10662 null;
10663
10664 -- See if Expr is an object with known size
10665
10666 elsif Is_Entity_Name (Expr)
10667 and then Known_Static_Esize (Entity (Expr))
10668 then
10669 SizA := Esize (Entity (Expr));
10670
10671 -- Otherwise, we check the object size of the Expr type
10672
10673 elsif Known_Static_Esize (Etype (Expr)) then
10674 SizA := Esize (Etype (Expr));
10675 end if;
10676
10677 -- If we got a size, see if it is a multiple of the Obj
10678 -- alignment, if not, then the alignment cannot be
10679 -- acceptable, since the size is always a multiple of the
10680 -- alignment.
10681
10682 if SizA /= No_Uint then
10683 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
10684 Set_Result (Known_Incompatible);
10685 end if;
10686 end if;
10687 end if;
10688 end;
10689
10690 -- If we do not know required alignment, any non-zero offset is a
10691 -- potential problem (but certainly may be OK, so result is unknown).
10692
10693 elsif Offs /= No_Uint then
10694 Set_Result (Unknown);
10695
10696 -- If we can't find the result by direct comparison of alignment
10697 -- values, then there is still one case that we can determine known
10698 -- result, and that is when we can determine that the types are the
10699 -- same, and no alignments are specified. Then we known that the
10700 -- alignments are compatible, even if we don't know the alignment
10701 -- value in the front end.
10702
10703 elsif Etype (Obj) = Etype (Expr) then
10704
10705 -- Types are the same, but we have to check for possible size
10706 -- and alignments on the Expr object that may make the alignment
10707 -- different, even though the types are the same.
10708
10709 if Is_Entity_Name (Expr) then
10710
10711 -- First check alignment of the Expr object. Any alignment less
10712 -- than Maximum_Alignment is worrisome since this is the case
10713 -- where we do not know the alignment of Obj.
10714
10715 if Known_Alignment (Entity (Expr))
10716 and then UI_To_Int (Alignment (Entity (Expr))) <
10717 Ttypes.Maximum_Alignment
10718 then
10719 Set_Result (Unknown);
10720
10721 -- Now check size of Expr object. Any size that is not an
10722 -- even multiple of Maximum_Alignment is also worrisome
10723 -- since it may cause the alignment of the object to be less
10724 -- than the alignment of the type.
10725
10726 elsif Known_Static_Esize (Entity (Expr))
10727 and then
10728 (UI_To_Int (Esize (Entity (Expr))) mod
10729 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
10730 /= 0
10731 then
10732 Set_Result (Unknown);
10733
10734 -- Otherwise same type is decisive
10735
10736 else
10737 Set_Result (Known_Compatible);
10738 end if;
10739 end if;
10740
10741 -- Another case to deal with is when there is an explicit size or
10742 -- alignment clause when the types are not the same. If so, then the
10743 -- result is Unknown. We don't need to do this test if the Default is
10744 -- Unknown, since that result will be set in any case.
10745
10746 elsif Default /= Unknown
10747 and then (Has_Size_Clause (Etype (Expr))
10748 or else
10749 Has_Alignment_Clause (Etype (Expr)))
10750 then
10751 Set_Result (Unknown);
10752
10753 -- If no indication found, set default
10754
10755 else
10756 Set_Result (Default);
10757 end if;
10758
10759 -- Return worst result found
10760
10761 return Result;
10762 end Has_Compatible_Alignment_Internal;
10763
10764 -- Start of processing for Has_Compatible_Alignment
10765
10766 begin
10767 -- If Obj has no specified alignment, then set alignment from the type
10768 -- alignment. Perhaps we should always do this, but for sure we should
10769 -- do it when there is an address clause since we can do more if the
10770 -- alignment is known.
10771
10772 if Unknown_Alignment (Obj) then
10773 Set_Alignment (Obj, Alignment (Etype (Obj)));
10774 end if;
10775
10776 -- Now do the internal call that does all the work
10777
10778 return
10779 Has_Compatible_Alignment_Internal (Obj, Expr, Layout_Done, Unknown);
10780 end Has_Compatible_Alignment;
10781
10782 ----------------------
10783 -- Has_Declarations --
10784 ----------------------
10785
10786 function Has_Declarations (N : Node_Id) return Boolean is
10787 begin
10788 return Nkind_In (Nkind (N), N_Accept_Statement,
10789 N_Block_Statement,
10790 N_Compilation_Unit_Aux,
10791 N_Entry_Body,
10792 N_Package_Body,
10793 N_Protected_Body,
10794 N_Subprogram_Body,
10795 N_Task_Body,
10796 N_Package_Specification);
10797 end Has_Declarations;
10798
10799 ---------------------------------
10800 -- Has_Defaulted_Discriminants --
10801 ---------------------------------
10802
10803 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10804 begin
10805 return Has_Discriminants (Typ)
10806 and then Present (First_Discriminant (Typ))
10807 and then Present (Discriminant_Default_Value
10808 (First_Discriminant (Typ)));
10809 end Has_Defaulted_Discriminants;
10810
10811 -------------------
10812 -- Has_Denormals --
10813 -------------------
10814
10815 function Has_Denormals (E : Entity_Id) return Boolean is
10816 begin
10817 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
10818 end Has_Denormals;
10819
10820 -------------------------------------------
10821 -- Has_Discriminant_Dependent_Constraint --
10822 -------------------------------------------
10823
10824 function Has_Discriminant_Dependent_Constraint
10825 (Comp : Entity_Id) return Boolean
10826 is
10827 Comp_Decl : constant Node_Id := Parent (Comp);
10828 Subt_Indic : Node_Id;
10829 Constr : Node_Id;
10830 Assn : Node_Id;
10831
10832 begin
10833 -- Discriminants can't depend on discriminants
10834
10835 if Ekind (Comp) = E_Discriminant then
10836 return False;
10837
10838 else
10839 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
10840
10841 if Nkind (Subt_Indic) = N_Subtype_Indication then
10842 Constr := Constraint (Subt_Indic);
10843
10844 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
10845 Assn := First (Constraints (Constr));
10846 while Present (Assn) loop
10847 case Nkind (Assn) is
10848 when N_Identifier
10849 | N_Range
10850 | N_Subtype_Indication
10851 =>
10852 if Depends_On_Discriminant (Assn) then
10853 return True;
10854 end if;
10855
10856 when N_Discriminant_Association =>
10857 if Depends_On_Discriminant (Expression (Assn)) then
10858 return True;
10859 end if;
10860
10861 when others =>
10862 null;
10863 end case;
10864
10865 Next (Assn);
10866 end loop;
10867 end if;
10868 end if;
10869 end if;
10870
10871 return False;
10872 end Has_Discriminant_Dependent_Constraint;
10873
10874 --------------------------------------
10875 -- Has_Effectively_Volatile_Profile --
10876 --------------------------------------
10877
10878 function Has_Effectively_Volatile_Profile
10879 (Subp_Id : Entity_Id) return Boolean
10880 is
10881 Formal : Entity_Id;
10882
10883 begin
10884 -- Inspect the formal parameters looking for an effectively volatile
10885 -- type.
10886
10887 Formal := First_Formal (Subp_Id);
10888 while Present (Formal) loop
10889 if Is_Effectively_Volatile (Etype (Formal)) then
10890 return True;
10891 end if;
10892
10893 Next_Formal (Formal);
10894 end loop;
10895
10896 -- Inspect the return type of functions
10897
10898 if Ekind_In (Subp_Id, E_Function, E_Generic_Function)
10899 and then Is_Effectively_Volatile (Etype (Subp_Id))
10900 then
10901 return True;
10902 end if;
10903
10904 return False;
10905 end Has_Effectively_Volatile_Profile;
10906
10907 --------------------------
10908 -- Has_Enabled_Property --
10909 --------------------------
10910
10911 function Has_Enabled_Property
10912 (Item_Id : Entity_Id;
10913 Property : Name_Id) return Boolean
10914 is
10915 function Protected_Object_Has_Enabled_Property return Boolean;
10916 -- Determine whether a protected object denoted by Item_Id has the
10917 -- property enabled.
10918
10919 function State_Has_Enabled_Property return Boolean;
10920 -- Determine whether a state denoted by Item_Id has the property enabled
10921
10922 function Variable_Has_Enabled_Property return Boolean;
10923 -- Determine whether a variable denoted by Item_Id has the property
10924 -- enabled.
10925
10926 -------------------------------------------
10927 -- Protected_Object_Has_Enabled_Property --
10928 -------------------------------------------
10929
10930 function Protected_Object_Has_Enabled_Property return Boolean is
10931 Constits : constant Elist_Id := Part_Of_Constituents (Item_Id);
10932 Constit_Elmt : Elmt_Id;
10933 Constit_Id : Entity_Id;
10934
10935 begin
10936 -- Protected objects always have the properties Async_Readers and
10937 -- Async_Writers (SPARK RM 7.1.2(16)).
10938
10939 if Property = Name_Async_Readers
10940 or else Property = Name_Async_Writers
10941 then
10942 return True;
10943
10944 -- Protected objects that have Part_Of components also inherit their
10945 -- properties Effective_Reads and Effective_Writes
10946 -- (SPARK RM 7.1.2(16)).
10947
10948 elsif Present (Constits) then
10949 Constit_Elmt := First_Elmt (Constits);
10950 while Present (Constit_Elmt) loop
10951 Constit_Id := Node (Constit_Elmt);
10952
10953 if Has_Enabled_Property (Constit_Id, Property) then
10954 return True;
10955 end if;
10956
10957 Next_Elmt (Constit_Elmt);
10958 end loop;
10959 end if;
10960
10961 return False;
10962 end Protected_Object_Has_Enabled_Property;
10963
10964 --------------------------------
10965 -- State_Has_Enabled_Property --
10966 --------------------------------
10967
10968 function State_Has_Enabled_Property return Boolean is
10969 Decl : constant Node_Id := Parent (Item_Id);
10970
10971 procedure Find_Simple_Properties
10972 (Has_External : out Boolean;
10973 Has_Synchronous : out Boolean);
10974 -- Extract the simple properties associated with declaration Decl
10975
10976 function Is_Enabled_External_Property return Boolean;
10977 -- Determine whether property Property appears within the external
10978 -- property list of declaration Decl, and return its status.
10979
10980 ----------------------------
10981 -- Find_Simple_Properties --
10982 ----------------------------
10983
10984 procedure Find_Simple_Properties
10985 (Has_External : out Boolean;
10986 Has_Synchronous : out Boolean)
10987 is
10988 Opt : Node_Id;
10989
10990 begin
10991 -- Assume that none of the properties are available
10992
10993 Has_External := False;
10994 Has_Synchronous := False;
10995
10996 Opt := First (Expressions (Decl));
10997 while Present (Opt) loop
10998 if Nkind (Opt) = N_Identifier then
10999 if Chars (Opt) = Name_External then
11000 Has_External := True;
11001
11002 elsif Chars (Opt) = Name_Synchronous then
11003 Has_Synchronous := True;
11004 end if;
11005 end if;
11006
11007 Next (Opt);
11008 end loop;
11009 end Find_Simple_Properties;
11010
11011 ----------------------------------
11012 -- Is_Enabled_External_Property --
11013 ----------------------------------
11014
11015 function Is_Enabled_External_Property return Boolean is
11016 Opt : Node_Id;
11017 Opt_Nam : Node_Id;
11018 Prop : Node_Id;
11019 Prop_Nam : Node_Id;
11020 Props : Node_Id;
11021
11022 begin
11023 Opt := First (Component_Associations (Decl));
11024 while Present (Opt) loop
11025 Opt_Nam := First (Choices (Opt));
11026
11027 if Nkind (Opt_Nam) = N_Identifier
11028 and then Chars (Opt_Nam) = Name_External
11029 then
11030 Props := Expression (Opt);
11031
11032 -- Multiple properties appear as an aggregate
11033
11034 if Nkind (Props) = N_Aggregate then
11035
11036 -- Simple property form
11037
11038 Prop := First (Expressions (Props));
11039 while Present (Prop) loop
11040 if Chars (Prop) = Property then
11041 return True;
11042 end if;
11043
11044 Next (Prop);
11045 end loop;
11046
11047 -- Property with expression form
11048
11049 Prop := First (Component_Associations (Props));
11050 while Present (Prop) loop
11051 Prop_Nam := First (Choices (Prop));
11052
11053 -- The property can be represented in two ways:
11054 -- others => <value>
11055 -- <property> => <value>
11056
11057 if Nkind (Prop_Nam) = N_Others_Choice
11058 or else (Nkind (Prop_Nam) = N_Identifier
11059 and then Chars (Prop_Nam) = Property)
11060 then
11061 return Is_True (Expr_Value (Expression (Prop)));
11062 end if;
11063
11064 Next (Prop);
11065 end loop;
11066
11067 -- Single property
11068
11069 else
11070 return Chars (Props) = Property;
11071 end if;
11072 end if;
11073
11074 Next (Opt);
11075 end loop;
11076
11077 return False;
11078 end Is_Enabled_External_Property;
11079
11080 -- Local variables
11081
11082 Has_External : Boolean;
11083 Has_Synchronous : Boolean;
11084
11085 -- Start of processing for State_Has_Enabled_Property
11086
11087 begin
11088 -- The declaration of an external abstract state appears as an
11089 -- extension aggregate. If this is not the case, properties can
11090 -- never be set.
11091
11092 if Nkind (Decl) /= N_Extension_Aggregate then
11093 return False;
11094 end if;
11095
11096 Find_Simple_Properties (Has_External, Has_Synchronous);
11097
11098 -- Simple option External enables all properties (SPARK RM 7.1.2(2))
11099
11100 if Has_External then
11101 return True;
11102
11103 -- Option External may enable or disable specific properties
11104
11105 elsif Is_Enabled_External_Property then
11106 return True;
11107
11108 -- Simple option Synchronous
11109 --
11110 -- enables disables
11111 -- Async_Readers Effective_Reads
11112 -- Async_Writers Effective_Writes
11113 --
11114 -- Note that both forms of External have higher precedence than
11115 -- Synchronous (SPARK RM 7.1.4(9)).
11116
11117 elsif Has_Synchronous then
11118 return Nam_In (Property, Name_Async_Readers, Name_Async_Writers);
11119 end if;
11120
11121 return False;
11122 end State_Has_Enabled_Property;
11123
11124 -----------------------------------
11125 -- Variable_Has_Enabled_Property --
11126 -----------------------------------
11127
11128 function Variable_Has_Enabled_Property return Boolean is
11129 function Is_Enabled (Prag : Node_Id) return Boolean;
11130 -- Determine whether property pragma Prag (if present) denotes an
11131 -- enabled property.
11132
11133 ----------------
11134 -- Is_Enabled --
11135 ----------------
11136
11137 function Is_Enabled (Prag : Node_Id) return Boolean is
11138 Arg1 : Node_Id;
11139
11140 begin
11141 if Present (Prag) then
11142 Arg1 := First (Pragma_Argument_Associations (Prag));
11143
11144 -- The pragma has an optional Boolean expression, the related
11145 -- property is enabled only when the expression evaluates to
11146 -- True.
11147
11148 if Present (Arg1) then
11149 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
11150
11151 -- Otherwise the lack of expression enables the property by
11152 -- default.
11153
11154 else
11155 return True;
11156 end if;
11157
11158 -- The property was never set in the first place
11159
11160 else
11161 return False;
11162 end if;
11163 end Is_Enabled;
11164
11165 -- Local variables
11166
11167 AR : constant Node_Id :=
11168 Get_Pragma (Item_Id, Pragma_Async_Readers);
11169 AW : constant Node_Id :=
11170 Get_Pragma (Item_Id, Pragma_Async_Writers);
11171 ER : constant Node_Id :=
11172 Get_Pragma (Item_Id, Pragma_Effective_Reads);
11173 EW : constant Node_Id :=
11174 Get_Pragma (Item_Id, Pragma_Effective_Writes);
11175
11176 -- Start of processing for Variable_Has_Enabled_Property
11177
11178 begin
11179 -- A non-effectively volatile object can never possess external
11180 -- properties.
11181
11182 if not Is_Effectively_Volatile (Item_Id) then
11183 return False;
11184
11185 -- External properties related to variables come in two flavors -
11186 -- explicit and implicit. The explicit case is characterized by the
11187 -- presence of a property pragma with an optional Boolean flag. The
11188 -- property is enabled when the flag evaluates to True or the flag is
11189 -- missing altogether.
11190
11191 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
11192 return True;
11193
11194 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
11195 return True;
11196
11197 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
11198 return True;
11199
11200 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
11201 return True;
11202
11203 -- The implicit case lacks all property pragmas
11204
11205 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
11206 if Is_Protected_Type (Etype (Item_Id)) then
11207 return Protected_Object_Has_Enabled_Property;
11208 else
11209 return True;
11210 end if;
11211
11212 else
11213 return False;
11214 end if;
11215 end Variable_Has_Enabled_Property;
11216
11217 -- Start of processing for Has_Enabled_Property
11218
11219 begin
11220 -- Abstract states and variables have a flexible scheme of specifying
11221 -- external properties.
11222
11223 if Ekind (Item_Id) = E_Abstract_State then
11224 return State_Has_Enabled_Property;
11225
11226 elsif Ekind (Item_Id) = E_Variable then
11227 return Variable_Has_Enabled_Property;
11228
11229 -- By default, protected objects only have the properties Async_Readers
11230 -- and Async_Writers. If they have Part_Of components, they also inherit
11231 -- their properties Effective_Reads and Effective_Writes
11232 -- (SPARK RM 7.1.2(16)).
11233
11234 elsif Ekind (Item_Id) = E_Protected_Object then
11235 return Protected_Object_Has_Enabled_Property;
11236
11237 -- Otherwise a property is enabled when the related item is effectively
11238 -- volatile.
11239
11240 else
11241 return Is_Effectively_Volatile (Item_Id);
11242 end if;
11243 end Has_Enabled_Property;
11244
11245 -------------------------------------
11246 -- Has_Full_Default_Initialization --
11247 -------------------------------------
11248
11249 function Has_Full_Default_Initialization (Typ : Entity_Id) return Boolean is
11250 Comp : Entity_Id;
11251
11252 begin
11253 -- A type subject to pragma Default_Initial_Condition may be fully
11254 -- default initialized depending on inheritance and the argument of
11255 -- the pragma. Since any type may act as the full view of a private
11256 -- type, this check must be performed prior to the specialized tests
11257 -- below.
11258
11259 if Has_Fully_Default_Initializing_DIC_Pragma (Typ) then
11260 return True;
11261 end if;
11262
11263 -- A scalar type is fully default initialized if it is subject to aspect
11264 -- Default_Value.
11265
11266 if Is_Scalar_Type (Typ) then
11267 return Has_Default_Aspect (Typ);
11268
11269 -- An access type is fully default initialized by default
11270
11271 elsif Is_Access_Type (Typ) then
11272 return True;
11273
11274 -- An array type is fully default initialized if its element type is
11275 -- scalar and the array type carries aspect Default_Component_Value or
11276 -- the element type is fully default initialized.
11277
11278 elsif Is_Array_Type (Typ) then
11279 return
11280 Has_Default_Aspect (Typ)
11281 or else Has_Full_Default_Initialization (Component_Type (Typ));
11282
11283 -- A protected type, record type, or type extension is fully default
11284 -- initialized if all its components either carry an initialization
11285 -- expression or have a type that is fully default initialized. The
11286 -- parent type of a type extension must be fully default initialized.
11287
11288 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
11289
11290 -- Inspect all entities defined in the scope of the type, looking for
11291 -- uninitialized components.
11292
11293 Comp := First_Entity (Typ);
11294 while Present (Comp) loop
11295 if Ekind (Comp) = E_Component
11296 and then Comes_From_Source (Comp)
11297 and then No (Expression (Parent (Comp)))
11298 and then not Has_Full_Default_Initialization (Etype (Comp))
11299 then
11300 return False;
11301 end if;
11302
11303 Next_Entity (Comp);
11304 end loop;
11305
11306 -- Ensure that the parent type of a type extension is fully default
11307 -- initialized.
11308
11309 if Etype (Typ) /= Typ
11310 and then not Has_Full_Default_Initialization (Etype (Typ))
11311 then
11312 return False;
11313 end if;
11314
11315 -- If we get here, then all components and parent portion are fully
11316 -- default initialized.
11317
11318 return True;
11319
11320 -- A task type is fully default initialized by default
11321
11322 elsif Is_Task_Type (Typ) then
11323 return True;
11324
11325 -- Otherwise the type is not fully default initialized
11326
11327 else
11328 return False;
11329 end if;
11330 end Has_Full_Default_Initialization;
11331
11332 -----------------------------------------------
11333 -- Has_Fully_Default_Initializing_DIC_Pragma --
11334 -----------------------------------------------
11335
11336 function Has_Fully_Default_Initializing_DIC_Pragma
11337 (Typ : Entity_Id) return Boolean
11338 is
11339 Args : List_Id;
11340 Prag : Node_Id;
11341
11342 begin
11343 -- A type that inherits pragma Default_Initial_Condition from a parent
11344 -- type is automatically fully default initialized.
11345
11346 if Has_Inherited_DIC (Typ) then
11347 return True;
11348
11349 -- Otherwise the type is fully default initialized only when the pragma
11350 -- appears without an argument, or the argument is non-null.
11351
11352 elsif Has_Own_DIC (Typ) then
11353 Prag := Get_Pragma (Typ, Pragma_Default_Initial_Condition);
11354 pragma Assert (Present (Prag));
11355 Args := Pragma_Argument_Associations (Prag);
11356
11357 -- The pragma appears without an argument in which case it defaults
11358 -- to True.
11359
11360 if No (Args) then
11361 return True;
11362
11363 -- The pragma appears with a non-null expression
11364
11365 elsif Nkind (Get_Pragma_Arg (First (Args))) /= N_Null then
11366 return True;
11367 end if;
11368 end if;
11369
11370 return False;
11371 end Has_Fully_Default_Initializing_DIC_Pragma;
11372
11373 --------------------
11374 -- Has_Infinities --
11375 --------------------
11376
11377 function Has_Infinities (E : Entity_Id) return Boolean is
11378 begin
11379 return
11380 Is_Floating_Point_Type (E)
11381 and then Nkind (Scalar_Range (E)) = N_Range
11382 and then Includes_Infinities (Scalar_Range (E));
11383 end Has_Infinities;
11384
11385 --------------------
11386 -- Has_Interfaces --
11387 --------------------
11388
11389 function Has_Interfaces
11390 (T : Entity_Id;
11391 Use_Full_View : Boolean := True) return Boolean
11392 is
11393 Typ : Entity_Id := Base_Type (T);
11394
11395 begin
11396 -- Handle concurrent types
11397
11398 if Is_Concurrent_Type (Typ) then
11399 Typ := Corresponding_Record_Type (Typ);
11400 end if;
11401
11402 if not Present (Typ)
11403 or else not Is_Record_Type (Typ)
11404 or else not Is_Tagged_Type (Typ)
11405 then
11406 return False;
11407 end if;
11408
11409 -- Handle private types
11410
11411 if Use_Full_View and then Present (Full_View (Typ)) then
11412 Typ := Full_View (Typ);
11413 end if;
11414
11415 -- Handle concurrent record types
11416
11417 if Is_Concurrent_Record_Type (Typ)
11418 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
11419 then
11420 return True;
11421 end if;
11422
11423 loop
11424 if Is_Interface (Typ)
11425 or else
11426 (Is_Record_Type (Typ)
11427 and then Present (Interfaces (Typ))
11428 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
11429 then
11430 return True;
11431 end if;
11432
11433 exit when Etype (Typ) = Typ
11434
11435 -- Handle private types
11436
11437 or else (Present (Full_View (Etype (Typ)))
11438 and then Full_View (Etype (Typ)) = Typ)
11439
11440 -- Protect frontend against wrong sources with cyclic derivations
11441
11442 or else Etype (Typ) = T;
11443
11444 -- Climb to the ancestor type handling private types
11445
11446 if Present (Full_View (Etype (Typ))) then
11447 Typ := Full_View (Etype (Typ));
11448 else
11449 Typ := Etype (Typ);
11450 end if;
11451 end loop;
11452
11453 return False;
11454 end Has_Interfaces;
11455
11456 --------------------------
11457 -- Has_Max_Queue_Length --
11458 --------------------------
11459
11460 function Has_Max_Queue_Length (Id : Entity_Id) return Boolean is
11461 begin
11462 return
11463 Ekind (Id) = E_Entry
11464 and then Present (Get_Pragma (Id, Pragma_Max_Queue_Length));
11465 end Has_Max_Queue_Length;
11466
11467 ---------------------------------
11468 -- Has_No_Obvious_Side_Effects --
11469 ---------------------------------
11470
11471 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
11472 begin
11473 -- For now handle literals, constants, and non-volatile variables and
11474 -- expressions combining these with operators or short circuit forms.
11475
11476 if Nkind (N) in N_Numeric_Or_String_Literal then
11477 return True;
11478
11479 elsif Nkind (N) = N_Character_Literal then
11480 return True;
11481
11482 elsif Nkind (N) in N_Unary_Op then
11483 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
11484
11485 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
11486 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
11487 and then
11488 Has_No_Obvious_Side_Effects (Right_Opnd (N));
11489
11490 elsif Nkind (N) = N_Expression_With_Actions
11491 and then Is_Empty_List (Actions (N))
11492 then
11493 return Has_No_Obvious_Side_Effects (Expression (N));
11494
11495 elsif Nkind (N) in N_Has_Entity then
11496 return Present (Entity (N))
11497 and then Ekind_In (Entity (N), E_Variable,
11498 E_Constant,
11499 E_Enumeration_Literal,
11500 E_In_Parameter,
11501 E_Out_Parameter,
11502 E_In_Out_Parameter)
11503 and then not Is_Volatile (Entity (N));
11504
11505 else
11506 return False;
11507 end if;
11508 end Has_No_Obvious_Side_Effects;
11509
11510 -----------------------------
11511 -- Has_Non_Null_Refinement --
11512 -----------------------------
11513
11514 function Has_Non_Null_Refinement (Id : Entity_Id) return Boolean is
11515 Constits : Elist_Id;
11516
11517 begin
11518 pragma Assert (Ekind (Id) = E_Abstract_State);
11519 Constits := Refinement_Constituents (Id);
11520
11521 -- For a refinement to be non-null, the first constituent must be
11522 -- anything other than null.
11523
11524 return
11525 Present (Constits)
11526 and then Nkind (Node (First_Elmt (Constits))) /= N_Null;
11527 end Has_Non_Null_Refinement;
11528
11529 -----------------------------
11530 -- Has_Non_Null_Statements --
11531 -----------------------------
11532
11533 function Has_Non_Null_Statements (L : List_Id) return Boolean is
11534 Node : Node_Id;
11535
11536 begin
11537 if Is_Non_Empty_List (L) then
11538 Node := First (L);
11539
11540 loop
11541 if Nkind (Node) /= N_Null_Statement then
11542 return True;
11543 end if;
11544
11545 Next (Node);
11546 exit when Node = Empty;
11547 end loop;
11548 end if;
11549
11550 return False;
11551 end Has_Non_Null_Statements;
11552
11553 ----------------------------------
11554 -- Has_Non_Trivial_Precondition --
11555 ----------------------------------
11556
11557 function Has_Non_Trivial_Precondition (Subp : Entity_Id) return Boolean is
11558 Pre : constant Node_Id := Find_Aspect (Subp, Aspect_Pre);
11559
11560 begin
11561 return
11562 Present (Pre)
11563 and then Class_Present (Pre)
11564 and then not Is_Entity_Name (Expression (Pre));
11565 end Has_Non_Trivial_Precondition;
11566
11567 -------------------
11568 -- Has_Null_Body --
11569 -------------------
11570
11571 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
11572 Body_Id : Entity_Id;
11573 Decl : Node_Id;
11574 Spec : Node_Id;
11575 Stmt1 : Node_Id;
11576 Stmt2 : Node_Id;
11577
11578 begin
11579 Spec := Parent (Proc_Id);
11580 Decl := Parent (Spec);
11581
11582 -- Retrieve the entity of the procedure body (e.g. invariant proc).
11583
11584 if Nkind (Spec) = N_Procedure_Specification
11585 and then Nkind (Decl) = N_Subprogram_Declaration
11586 then
11587 Body_Id := Corresponding_Body (Decl);
11588
11589 -- The body acts as a spec
11590
11591 else
11592 Body_Id := Proc_Id;
11593 end if;
11594
11595 -- The body will be generated later
11596
11597 if No (Body_Id) then
11598 return False;
11599 end if;
11600
11601 Spec := Parent (Body_Id);
11602 Decl := Parent (Spec);
11603
11604 pragma Assert
11605 (Nkind (Spec) = N_Procedure_Specification
11606 and then Nkind (Decl) = N_Subprogram_Body);
11607
11608 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
11609
11610 -- Look for a null statement followed by an optional return
11611 -- statement.
11612
11613 if Nkind (Stmt1) = N_Null_Statement then
11614 Stmt2 := Next (Stmt1);
11615
11616 if Present (Stmt2) then
11617 return Nkind (Stmt2) = N_Simple_Return_Statement;
11618 else
11619 return True;
11620 end if;
11621 end if;
11622
11623 return False;
11624 end Has_Null_Body;
11625
11626 ------------------------
11627 -- Has_Null_Exclusion --
11628 ------------------------
11629
11630 function Has_Null_Exclusion (N : Node_Id) return Boolean is
11631 begin
11632 case Nkind (N) is
11633 when N_Access_Definition
11634 | N_Access_Function_Definition
11635 | N_Access_Procedure_Definition
11636 | N_Access_To_Object_Definition
11637 | N_Allocator
11638 | N_Derived_Type_Definition
11639 | N_Function_Specification
11640 | N_Subtype_Declaration
11641 =>
11642 return Null_Exclusion_Present (N);
11643
11644 when N_Component_Definition
11645 | N_Formal_Object_Declaration
11646 | N_Object_Renaming_Declaration
11647 =>
11648 if Present (Subtype_Mark (N)) then
11649 return Null_Exclusion_Present (N);
11650 else pragma Assert (Present (Access_Definition (N)));
11651 return Null_Exclusion_Present (Access_Definition (N));
11652 end if;
11653
11654 when N_Discriminant_Specification =>
11655 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
11656 return Null_Exclusion_Present (Discriminant_Type (N));
11657 else
11658 return Null_Exclusion_Present (N);
11659 end if;
11660
11661 when N_Object_Declaration =>
11662 if Nkind (Object_Definition (N)) = N_Access_Definition then
11663 return Null_Exclusion_Present (Object_Definition (N));
11664 else
11665 return Null_Exclusion_Present (N);
11666 end if;
11667
11668 when N_Parameter_Specification =>
11669 if Nkind (Parameter_Type (N)) = N_Access_Definition then
11670 return Null_Exclusion_Present (Parameter_Type (N));
11671 else
11672 return Null_Exclusion_Present (N);
11673 end if;
11674
11675 when others =>
11676 return False;
11677 end case;
11678 end Has_Null_Exclusion;
11679
11680 ------------------------
11681 -- Has_Null_Extension --
11682 ------------------------
11683
11684 function Has_Null_Extension (T : Entity_Id) return Boolean is
11685 B : constant Entity_Id := Base_Type (T);
11686 Comps : Node_Id;
11687 Ext : Node_Id;
11688
11689 begin
11690 if Nkind (Parent (B)) = N_Full_Type_Declaration
11691 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
11692 then
11693 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
11694
11695 if Present (Ext) then
11696 if Null_Present (Ext) then
11697 return True;
11698 else
11699 Comps := Component_List (Ext);
11700
11701 -- The null component list is rewritten during analysis to
11702 -- include the parent component. Any other component indicates
11703 -- that the extension was not originally null.
11704
11705 return Null_Present (Comps)
11706 or else No (Next (First (Component_Items (Comps))));
11707 end if;
11708 else
11709 return False;
11710 end if;
11711
11712 else
11713 return False;
11714 end if;
11715 end Has_Null_Extension;
11716
11717 -------------------------
11718 -- Has_Null_Refinement --
11719 -------------------------
11720
11721 function Has_Null_Refinement (Id : Entity_Id) return Boolean is
11722 Constits : Elist_Id;
11723
11724 begin
11725 pragma Assert (Ekind (Id) = E_Abstract_State);
11726 Constits := Refinement_Constituents (Id);
11727
11728 -- For a refinement to be null, the state's sole constituent must be a
11729 -- null.
11730
11731 return
11732 Present (Constits)
11733 and then Nkind (Node (First_Elmt (Constits))) = N_Null;
11734 end Has_Null_Refinement;
11735
11736 -------------------------------
11737 -- Has_Overriding_Initialize --
11738 -------------------------------
11739
11740 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
11741 BT : constant Entity_Id := Base_Type (T);
11742 P : Elmt_Id;
11743
11744 begin
11745 if Is_Controlled (BT) then
11746 if Is_RTU (Scope (BT), Ada_Finalization) then
11747 return False;
11748
11749 elsif Present (Primitive_Operations (BT)) then
11750 P := First_Elmt (Primitive_Operations (BT));
11751 while Present (P) loop
11752 declare
11753 Init : constant Entity_Id := Node (P);
11754 Formal : constant Entity_Id := First_Formal (Init);
11755 begin
11756 if Ekind (Init) = E_Procedure
11757 and then Chars (Init) = Name_Initialize
11758 and then Comes_From_Source (Init)
11759 and then Present (Formal)
11760 and then Etype (Formal) = BT
11761 and then No (Next_Formal (Formal))
11762 and then (Ada_Version < Ada_2012
11763 or else not Null_Present (Parent (Init)))
11764 then
11765 return True;
11766 end if;
11767 end;
11768
11769 Next_Elmt (P);
11770 end loop;
11771 end if;
11772
11773 -- Here if type itself does not have a non-null Initialize operation:
11774 -- check immediate ancestor.
11775
11776 if Is_Derived_Type (BT)
11777 and then Has_Overriding_Initialize (Etype (BT))
11778 then
11779 return True;
11780 end if;
11781 end if;
11782
11783 return False;
11784 end Has_Overriding_Initialize;
11785
11786 --------------------------------------
11787 -- Has_Preelaborable_Initialization --
11788 --------------------------------------
11789
11790 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
11791 Has_PE : Boolean;
11792
11793 procedure Check_Components (E : Entity_Id);
11794 -- Check component/discriminant chain, sets Has_PE False if a component
11795 -- or discriminant does not meet the preelaborable initialization rules.
11796
11797 ----------------------
11798 -- Check_Components --
11799 ----------------------
11800
11801 procedure Check_Components (E : Entity_Id) is
11802 Ent : Entity_Id;
11803 Exp : Node_Id;
11804
11805 begin
11806 -- Loop through entities of record or protected type
11807
11808 Ent := E;
11809 while Present (Ent) loop
11810
11811 -- We are interested only in components and discriminants
11812
11813 Exp := Empty;
11814
11815 case Ekind (Ent) is
11816 when E_Component =>
11817
11818 -- Get default expression if any. If there is no declaration
11819 -- node, it means we have an internal entity. The parent and
11820 -- tag fields are examples of such entities. For such cases,
11821 -- we just test the type of the entity.
11822
11823 if Present (Declaration_Node (Ent)) then
11824 Exp := Expression (Declaration_Node (Ent));
11825 end if;
11826
11827 when E_Discriminant =>
11828
11829 -- Note: for a renamed discriminant, the Declaration_Node
11830 -- may point to the one from the ancestor, and have a
11831 -- different expression, so use the proper attribute to
11832 -- retrieve the expression from the derived constraint.
11833
11834 Exp := Discriminant_Default_Value (Ent);
11835
11836 when others =>
11837 goto Check_Next_Entity;
11838 end case;
11839
11840 -- A component has PI if it has no default expression and the
11841 -- component type has PI.
11842
11843 if No (Exp) then
11844 if not Has_Preelaborable_Initialization (Etype (Ent)) then
11845 Has_PE := False;
11846 exit;
11847 end if;
11848
11849 -- Require the default expression to be preelaborable
11850
11851 elsif not Is_Preelaborable_Construct (Exp) then
11852 Has_PE := False;
11853 exit;
11854 end if;
11855
11856 <<Check_Next_Entity>>
11857 Next_Entity (Ent);
11858 end loop;
11859 end Check_Components;
11860
11861 -- Start of processing for Has_Preelaborable_Initialization
11862
11863 begin
11864 -- Immediate return if already marked as known preelaborable init. This
11865 -- covers types for which this function has already been called once
11866 -- and returned True (in which case the result is cached), and also
11867 -- types to which a pragma Preelaborable_Initialization applies.
11868
11869 if Known_To_Have_Preelab_Init (E) then
11870 return True;
11871 end if;
11872
11873 -- If the type is a subtype representing a generic actual type, then
11874 -- test whether its base type has preelaborable initialization since
11875 -- the subtype representing the actual does not inherit this attribute
11876 -- from the actual or formal. (but maybe it should???)
11877
11878 if Is_Generic_Actual_Type (E) then
11879 return Has_Preelaborable_Initialization (Base_Type (E));
11880 end if;
11881
11882 -- All elementary types have preelaborable initialization
11883
11884 if Is_Elementary_Type (E) then
11885 Has_PE := True;
11886
11887 -- Array types have PI if the component type has PI
11888
11889 elsif Is_Array_Type (E) then
11890 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
11891
11892 -- A derived type has preelaborable initialization if its parent type
11893 -- has preelaborable initialization and (in the case of a derived record
11894 -- extension) if the non-inherited components all have preelaborable
11895 -- initialization. However, a user-defined controlled type with an
11896 -- overriding Initialize procedure does not have preelaborable
11897 -- initialization.
11898
11899 elsif Is_Derived_Type (E) then
11900
11901 -- If the derived type is a private extension then it doesn't have
11902 -- preelaborable initialization.
11903
11904 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
11905 return False;
11906 end if;
11907
11908 -- First check whether ancestor type has preelaborable initialization
11909
11910 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
11911
11912 -- If OK, check extension components (if any)
11913
11914 if Has_PE and then Is_Record_Type (E) then
11915 Check_Components (First_Entity (E));
11916 end if;
11917
11918 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
11919 -- with a user defined Initialize procedure does not have PI. If
11920 -- the type is untagged, the control primitives come from a component
11921 -- that has already been checked.
11922
11923 if Has_PE
11924 and then Is_Controlled (E)
11925 and then Is_Tagged_Type (E)
11926 and then Has_Overriding_Initialize (E)
11927 then
11928 Has_PE := False;
11929 end if;
11930
11931 -- Private types not derived from a type having preelaborable init and
11932 -- that are not marked with pragma Preelaborable_Initialization do not
11933 -- have preelaborable initialization.
11934
11935 elsif Is_Private_Type (E) then
11936 return False;
11937
11938 -- Record type has PI if it is non private and all components have PI
11939
11940 elsif Is_Record_Type (E) then
11941 Has_PE := True;
11942 Check_Components (First_Entity (E));
11943
11944 -- Protected types must not have entries, and components must meet
11945 -- same set of rules as for record components.
11946
11947 elsif Is_Protected_Type (E) then
11948 if Has_Entries (E) then
11949 Has_PE := False;
11950 else
11951 Has_PE := True;
11952 Check_Components (First_Entity (E));
11953 Check_Components (First_Private_Entity (E));
11954 end if;
11955
11956 -- Type System.Address always has preelaborable initialization
11957
11958 elsif Is_RTE (E, RE_Address) then
11959 Has_PE := True;
11960
11961 -- In all other cases, type does not have preelaborable initialization
11962
11963 else
11964 return False;
11965 end if;
11966
11967 -- If type has preelaborable initialization, cache result
11968
11969 if Has_PE then
11970 Set_Known_To_Have_Preelab_Init (E);
11971 end if;
11972
11973 return Has_PE;
11974 end Has_Preelaborable_Initialization;
11975
11976 ----------------
11977 -- Has_Prefix --
11978 ----------------
11979
11980 function Has_Prefix (N : Node_Id) return Boolean is
11981 begin
11982 return
11983 Nkind_In (N, N_Attribute_Reference,
11984 N_Expanded_Name,
11985 N_Explicit_Dereference,
11986 N_Indexed_Component,
11987 N_Reference,
11988 N_Selected_Component,
11989 N_Slice);
11990 end Has_Prefix;
11991
11992 ---------------------------
11993 -- Has_Private_Component --
11994 ---------------------------
11995
11996 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
11997 Btype : Entity_Id := Base_Type (Type_Id);
11998 Component : Entity_Id;
11999
12000 begin
12001 if Error_Posted (Type_Id)
12002 or else Error_Posted (Btype)
12003 then
12004 return False;
12005 end if;
12006
12007 if Is_Class_Wide_Type (Btype) then
12008 Btype := Root_Type (Btype);
12009 end if;
12010
12011 if Is_Private_Type (Btype) then
12012 declare
12013 UT : constant Entity_Id := Underlying_Type (Btype);
12014 begin
12015 if No (UT) then
12016 if No (Full_View (Btype)) then
12017 return not Is_Generic_Type (Btype)
12018 and then
12019 not Is_Generic_Type (Root_Type (Btype));
12020 else
12021 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
12022 end if;
12023 else
12024 return not Is_Frozen (UT) and then Has_Private_Component (UT);
12025 end if;
12026 end;
12027
12028 elsif Is_Array_Type (Btype) then
12029 return Has_Private_Component (Component_Type (Btype));
12030
12031 elsif Is_Record_Type (Btype) then
12032 Component := First_Component (Btype);
12033 while Present (Component) loop
12034 if Has_Private_Component (Etype (Component)) then
12035 return True;
12036 end if;
12037
12038 Next_Component (Component);
12039 end loop;
12040
12041 return False;
12042
12043 elsif Is_Protected_Type (Btype)
12044 and then Present (Corresponding_Record_Type (Btype))
12045 then
12046 return Has_Private_Component (Corresponding_Record_Type (Btype));
12047
12048 else
12049 return False;
12050 end if;
12051 end Has_Private_Component;
12052
12053 ----------------------
12054 -- Has_Signed_Zeros --
12055 ----------------------
12056
12057 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
12058 begin
12059 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
12060 end Has_Signed_Zeros;
12061
12062 ------------------------------
12063 -- Has_Significant_Contract --
12064 ------------------------------
12065
12066 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
12067 Subp_Nam : constant Name_Id := Chars (Subp_Id);
12068
12069 begin
12070 -- _Finalizer procedure
12071
12072 if Subp_Nam = Name_uFinalizer then
12073 return False;
12074
12075 -- _Postconditions procedure
12076
12077 elsif Subp_Nam = Name_uPostconditions then
12078 return False;
12079
12080 -- Predicate function
12081
12082 elsif Ekind (Subp_Id) = E_Function
12083 and then Is_Predicate_Function (Subp_Id)
12084 then
12085 return False;
12086
12087 -- TSS subprogram
12088
12089 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
12090 return False;
12091
12092 else
12093 return True;
12094 end if;
12095 end Has_Significant_Contract;
12096
12097 -----------------------------
12098 -- Has_Static_Array_Bounds --
12099 -----------------------------
12100
12101 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
12102 All_Static : Boolean;
12103 Dummy : Boolean;
12104
12105 begin
12106 Examine_Array_Bounds (Typ, All_Static, Dummy);
12107
12108 return All_Static;
12109 end Has_Static_Array_Bounds;
12110
12111 ---------------------------------------
12112 -- Has_Static_Non_Empty_Array_Bounds --
12113 ---------------------------------------
12114
12115 function Has_Static_Non_Empty_Array_Bounds (Typ : Node_Id) return Boolean is
12116 All_Static : Boolean;
12117 Has_Empty : Boolean;
12118
12119 begin
12120 Examine_Array_Bounds (Typ, All_Static, Has_Empty);
12121
12122 return All_Static and not Has_Empty;
12123 end Has_Static_Non_Empty_Array_Bounds;
12124
12125 ----------------
12126 -- Has_Stream --
12127 ----------------
12128
12129 function Has_Stream (T : Entity_Id) return Boolean is
12130 E : Entity_Id;
12131
12132 begin
12133 if No (T) then
12134 return False;
12135
12136 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
12137 return True;
12138
12139 elsif Is_Array_Type (T) then
12140 return Has_Stream (Component_Type (T));
12141
12142 elsif Is_Record_Type (T) then
12143 E := First_Component (T);
12144 while Present (E) loop
12145 if Has_Stream (Etype (E)) then
12146 return True;
12147 else
12148 Next_Component (E);
12149 end if;
12150 end loop;
12151
12152 return False;
12153
12154 elsif Is_Private_Type (T) then
12155 return Has_Stream (Underlying_Type (T));
12156
12157 else
12158 return False;
12159 end if;
12160 end Has_Stream;
12161
12162 ----------------
12163 -- Has_Suffix --
12164 ----------------
12165
12166 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
12167 begin
12168 Get_Name_String (Chars (E));
12169 return Name_Buffer (Name_Len) = Suffix;
12170 end Has_Suffix;
12171
12172 ----------------
12173 -- Add_Suffix --
12174 ----------------
12175
12176 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
12177 begin
12178 Get_Name_String (Chars (E));
12179 Add_Char_To_Name_Buffer (Suffix);
12180 return Name_Find;
12181 end Add_Suffix;
12182
12183 -------------------
12184 -- Remove_Suffix --
12185 -------------------
12186
12187 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
12188 begin
12189 pragma Assert (Has_Suffix (E, Suffix));
12190 Get_Name_String (Chars (E));
12191 Name_Len := Name_Len - 1;
12192 return Name_Find;
12193 end Remove_Suffix;
12194
12195 ----------------------------------
12196 -- Replace_Null_By_Null_Address --
12197 ----------------------------------
12198
12199 procedure Replace_Null_By_Null_Address (N : Node_Id) is
12200 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id);
12201 -- Replace operand Op with a reference to Null_Address when the operand
12202 -- denotes a null Address. Other_Op denotes the other operand.
12203
12204 --------------------------
12205 -- Replace_Null_Operand --
12206 --------------------------
12207
12208 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id) is
12209 begin
12210 -- Check the type of the complementary operand since the N_Null node
12211 -- has not been decorated yet.
12212
12213 if Nkind (Op) = N_Null
12214 and then Is_Descendant_Of_Address (Etype (Other_Op))
12215 then
12216 Rewrite (Op, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (Op)));
12217 end if;
12218 end Replace_Null_Operand;
12219
12220 -- Start of processing for Replace_Null_By_Null_Address
12221
12222 begin
12223 pragma Assert (Relaxed_RM_Semantics);
12224 pragma Assert (Nkind_In (N, N_Null,
12225 N_Op_Eq,
12226 N_Op_Ge,
12227 N_Op_Gt,
12228 N_Op_Le,
12229 N_Op_Lt,
12230 N_Op_Ne));
12231
12232 if Nkind (N) = N_Null then
12233 Rewrite (N, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
12234
12235 else
12236 declare
12237 L : constant Node_Id := Left_Opnd (N);
12238 R : constant Node_Id := Right_Opnd (N);
12239
12240 begin
12241 Replace_Null_Operand (L, Other_Op => R);
12242 Replace_Null_Operand (R, Other_Op => L);
12243 end;
12244 end if;
12245 end Replace_Null_By_Null_Address;
12246
12247 --------------------------
12248 -- Has_Tagged_Component --
12249 --------------------------
12250
12251 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
12252 Comp : Entity_Id;
12253
12254 begin
12255 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
12256 return Has_Tagged_Component (Underlying_Type (Typ));
12257
12258 elsif Is_Array_Type (Typ) then
12259 return Has_Tagged_Component (Component_Type (Typ));
12260
12261 elsif Is_Tagged_Type (Typ) then
12262 return True;
12263
12264 elsif Is_Record_Type (Typ) then
12265 Comp := First_Component (Typ);
12266 while Present (Comp) loop
12267 if Has_Tagged_Component (Etype (Comp)) then
12268 return True;
12269 end if;
12270
12271 Next_Component (Comp);
12272 end loop;
12273
12274 return False;
12275
12276 else
12277 return False;
12278 end if;
12279 end Has_Tagged_Component;
12280
12281 -----------------------------
12282 -- Has_Undefined_Reference --
12283 -----------------------------
12284
12285 function Has_Undefined_Reference (Expr : Node_Id) return Boolean is
12286 Has_Undef_Ref : Boolean := False;
12287 -- Flag set when expression Expr contains at least one undefined
12288 -- reference.
12289
12290 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result;
12291 -- Determine whether N denotes a reference and if it does, whether it is
12292 -- undefined.
12293
12294 ----------------------------
12295 -- Is_Undefined_Reference --
12296 ----------------------------
12297
12298 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result is
12299 begin
12300 if Is_Entity_Name (N)
12301 and then Present (Entity (N))
12302 and then Entity (N) = Any_Id
12303 then
12304 Has_Undef_Ref := True;
12305 return Abandon;
12306 end if;
12307
12308 return OK;
12309 end Is_Undefined_Reference;
12310
12311 procedure Find_Undefined_References is
12312 new Traverse_Proc (Is_Undefined_Reference);
12313
12314 -- Start of processing for Has_Undefined_Reference
12315
12316 begin
12317 Find_Undefined_References (Expr);
12318
12319 return Has_Undef_Ref;
12320 end Has_Undefined_Reference;
12321
12322 ----------------------------
12323 -- Has_Volatile_Component --
12324 ----------------------------
12325
12326 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
12327 Comp : Entity_Id;
12328
12329 begin
12330 if Has_Volatile_Components (Typ) then
12331 return True;
12332
12333 elsif Is_Array_Type (Typ) then
12334 return Is_Volatile (Component_Type (Typ));
12335
12336 elsif Is_Record_Type (Typ) then
12337 Comp := First_Component (Typ);
12338 while Present (Comp) loop
12339 if Is_Volatile_Object (Comp) then
12340 return True;
12341 end if;
12342
12343 Comp := Next_Component (Comp);
12344 end loop;
12345 end if;
12346
12347 return False;
12348 end Has_Volatile_Component;
12349
12350 -------------------------
12351 -- Implementation_Kind --
12352 -------------------------
12353
12354 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
12355 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
12356 Arg : Node_Id;
12357 begin
12358 pragma Assert (Present (Impl_Prag));
12359 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
12360 return Chars (Get_Pragma_Arg (Arg));
12361 end Implementation_Kind;
12362
12363 --------------------------
12364 -- Implements_Interface --
12365 --------------------------
12366
12367 function Implements_Interface
12368 (Typ_Ent : Entity_Id;
12369 Iface_Ent : Entity_Id;
12370 Exclude_Parents : Boolean := False) return Boolean
12371 is
12372 Ifaces_List : Elist_Id;
12373 Elmt : Elmt_Id;
12374 Iface : Entity_Id := Base_Type (Iface_Ent);
12375 Typ : Entity_Id := Base_Type (Typ_Ent);
12376
12377 begin
12378 if Is_Class_Wide_Type (Typ) then
12379 Typ := Root_Type (Typ);
12380 end if;
12381
12382 if not Has_Interfaces (Typ) then
12383 return False;
12384 end if;
12385
12386 if Is_Class_Wide_Type (Iface) then
12387 Iface := Root_Type (Iface);
12388 end if;
12389
12390 Collect_Interfaces (Typ, Ifaces_List);
12391
12392 Elmt := First_Elmt (Ifaces_List);
12393 while Present (Elmt) loop
12394 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
12395 and then Exclude_Parents
12396 then
12397 null;
12398
12399 elsif Node (Elmt) = Iface then
12400 return True;
12401 end if;
12402
12403 Next_Elmt (Elmt);
12404 end loop;
12405
12406 return False;
12407 end Implements_Interface;
12408
12409 ------------------------------------
12410 -- In_Assertion_Expression_Pragma --
12411 ------------------------------------
12412
12413 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
12414 Par : Node_Id;
12415 Prag : Node_Id := Empty;
12416
12417 begin
12418 -- Climb the parent chain looking for an enclosing pragma
12419
12420 Par := N;
12421 while Present (Par) loop
12422 if Nkind (Par) = N_Pragma then
12423 Prag := Par;
12424 exit;
12425
12426 -- Precondition-like pragmas are expanded into if statements, check
12427 -- the original node instead.
12428
12429 elsif Nkind (Original_Node (Par)) = N_Pragma then
12430 Prag := Original_Node (Par);
12431 exit;
12432
12433 -- The expansion of attribute 'Old generates a constant to capture
12434 -- the result of the prefix. If the parent traversal reaches
12435 -- one of these constants, then the node technically came from a
12436 -- postcondition-like pragma. Note that the Ekind is not tested here
12437 -- because N may be the expression of an object declaration which is
12438 -- currently being analyzed. Such objects carry Ekind of E_Void.
12439
12440 elsif Nkind (Par) = N_Object_Declaration
12441 and then Constant_Present (Par)
12442 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
12443 then
12444 return True;
12445
12446 -- Prevent the search from going too far
12447
12448 elsif Is_Body_Or_Package_Declaration (Par) then
12449 return False;
12450 end if;
12451
12452 Par := Parent (Par);
12453 end loop;
12454
12455 return
12456 Present (Prag)
12457 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
12458 end In_Assertion_Expression_Pragma;
12459
12460 ----------------------
12461 -- In_Generic_Scope --
12462 ----------------------
12463
12464 function In_Generic_Scope (E : Entity_Id) return Boolean is
12465 S : Entity_Id;
12466
12467 begin
12468 S := Scope (E);
12469 while Present (S) and then S /= Standard_Standard loop
12470 if Is_Generic_Unit (S) then
12471 return True;
12472 end if;
12473
12474 S := Scope (S);
12475 end loop;
12476
12477 return False;
12478 end In_Generic_Scope;
12479
12480 -----------------
12481 -- In_Instance --
12482 -----------------
12483
12484 function In_Instance return Boolean is
12485 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
12486 S : Entity_Id;
12487
12488 begin
12489 S := Current_Scope;
12490 while Present (S) and then S /= Standard_Standard loop
12491 if Is_Generic_Instance (S) then
12492
12493 -- A child instance is always compiled in the context of a parent
12494 -- instance. Nevertheless, its actuals must not be analyzed in an
12495 -- instance context. We detect this case by examining the current
12496 -- compilation unit, which must be a child instance, and checking
12497 -- that it has not been analyzed yet.
12498
12499 if Is_Child_Unit (Curr_Unit)
12500 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
12501 N_Package_Instantiation
12502 and then Ekind (Curr_Unit) = E_Void
12503 then
12504 return False;
12505 else
12506 return True;
12507 end if;
12508 end if;
12509
12510 S := Scope (S);
12511 end loop;
12512
12513 return False;
12514 end In_Instance;
12515
12516 ----------------------
12517 -- In_Instance_Body --
12518 ----------------------
12519
12520 function In_Instance_Body return Boolean is
12521 S : Entity_Id;
12522
12523 begin
12524 S := Current_Scope;
12525 while Present (S) and then S /= Standard_Standard loop
12526 if Ekind_In (S, E_Function, E_Procedure)
12527 and then Is_Generic_Instance (S)
12528 then
12529 return True;
12530
12531 elsif Ekind (S) = E_Package
12532 and then In_Package_Body (S)
12533 and then Is_Generic_Instance (S)
12534 then
12535 return True;
12536 end if;
12537
12538 S := Scope (S);
12539 end loop;
12540
12541 return False;
12542 end In_Instance_Body;
12543
12544 -----------------------------
12545 -- In_Instance_Not_Visible --
12546 -----------------------------
12547
12548 function In_Instance_Not_Visible return Boolean is
12549 S : Entity_Id;
12550
12551 begin
12552 S := Current_Scope;
12553 while Present (S) and then S /= Standard_Standard loop
12554 if Ekind_In (S, E_Function, E_Procedure)
12555 and then Is_Generic_Instance (S)
12556 then
12557 return True;
12558
12559 elsif Ekind (S) = E_Package
12560 and then (In_Package_Body (S) or else In_Private_Part (S))
12561 and then Is_Generic_Instance (S)
12562 then
12563 return True;
12564 end if;
12565
12566 S := Scope (S);
12567 end loop;
12568
12569 return False;
12570 end In_Instance_Not_Visible;
12571
12572 ------------------------------
12573 -- In_Instance_Visible_Part --
12574 ------------------------------
12575
12576 function In_Instance_Visible_Part
12577 (Id : Entity_Id := Current_Scope) return Boolean
12578 is
12579 Inst : Entity_Id;
12580
12581 begin
12582 Inst := Id;
12583 while Present (Inst) and then Inst /= Standard_Standard loop
12584 if Ekind (Inst) = E_Package
12585 and then Is_Generic_Instance (Inst)
12586 and then not In_Package_Body (Inst)
12587 and then not In_Private_Part (Inst)
12588 then
12589 return True;
12590 end if;
12591
12592 Inst := Scope (Inst);
12593 end loop;
12594
12595 return False;
12596 end In_Instance_Visible_Part;
12597
12598 ---------------------
12599 -- In_Package_Body --
12600 ---------------------
12601
12602 function In_Package_Body return Boolean is
12603 S : Entity_Id;
12604
12605 begin
12606 S := Current_Scope;
12607 while Present (S) and then S /= Standard_Standard loop
12608 if Ekind (S) = E_Package and then In_Package_Body (S) then
12609 return True;
12610 else
12611 S := Scope (S);
12612 end if;
12613 end loop;
12614
12615 return False;
12616 end In_Package_Body;
12617
12618 --------------------------
12619 -- In_Pragma_Expression --
12620 --------------------------
12621
12622 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
12623 P : Node_Id;
12624 begin
12625 P := Parent (N);
12626 loop
12627 if No (P) then
12628 return False;
12629 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
12630 return True;
12631 else
12632 P := Parent (P);
12633 end if;
12634 end loop;
12635 end In_Pragma_Expression;
12636
12637 ---------------------------
12638 -- In_Pre_Post_Condition --
12639 ---------------------------
12640
12641 function In_Pre_Post_Condition (N : Node_Id) return Boolean is
12642 Par : Node_Id;
12643 Prag : Node_Id := Empty;
12644 Prag_Id : Pragma_Id;
12645
12646 begin
12647 -- Climb the parent chain looking for an enclosing pragma
12648
12649 Par := N;
12650 while Present (Par) loop
12651 if Nkind (Par) = N_Pragma then
12652 Prag := Par;
12653 exit;
12654
12655 -- Prevent the search from going too far
12656
12657 elsif Is_Body_Or_Package_Declaration (Par) then
12658 exit;
12659 end if;
12660
12661 Par := Parent (Par);
12662 end loop;
12663
12664 if Present (Prag) then
12665 Prag_Id := Get_Pragma_Id (Prag);
12666
12667 return
12668 Prag_Id = Pragma_Post
12669 or else Prag_Id = Pragma_Post_Class
12670 or else Prag_Id = Pragma_Postcondition
12671 or else Prag_Id = Pragma_Pre
12672 or else Prag_Id = Pragma_Pre_Class
12673 or else Prag_Id = Pragma_Precondition;
12674
12675 -- Otherwise the node is not enclosed by a pre/postcondition pragma
12676
12677 else
12678 return False;
12679 end if;
12680 end In_Pre_Post_Condition;
12681
12682 ------------------------------
12683 -- In_Quantified_Expression --
12684 ------------------------------
12685
12686 function In_Quantified_Expression (N : Node_Id) return Boolean is
12687 P : Node_Id;
12688 begin
12689 P := Parent (N);
12690 loop
12691 if No (P) then
12692 return False;
12693 elsif Nkind (P) = N_Quantified_Expression then
12694 return True;
12695 else
12696 P := Parent (P);
12697 end if;
12698 end loop;
12699 end In_Quantified_Expression;
12700
12701 -------------------------------------
12702 -- In_Reverse_Storage_Order_Object --
12703 -------------------------------------
12704
12705 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
12706 Pref : Node_Id;
12707 Btyp : Entity_Id := Empty;
12708
12709 begin
12710 -- Climb up indexed components
12711
12712 Pref := N;
12713 loop
12714 case Nkind (Pref) is
12715 when N_Selected_Component =>
12716 Pref := Prefix (Pref);
12717 exit;
12718
12719 when N_Indexed_Component =>
12720 Pref := Prefix (Pref);
12721
12722 when others =>
12723 Pref := Empty;
12724 exit;
12725 end case;
12726 end loop;
12727
12728 if Present (Pref) then
12729 Btyp := Base_Type (Etype (Pref));
12730 end if;
12731
12732 return Present (Btyp)
12733 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
12734 and then Reverse_Storage_Order (Btyp);
12735 end In_Reverse_Storage_Order_Object;
12736
12737 ------------------------------
12738 -- In_Same_Declarative_Part --
12739 ------------------------------
12740
12741 function In_Same_Declarative_Part
12742 (Context : Node_Id;
12743 N : Node_Id) return Boolean
12744 is
12745 Cont : Node_Id := Context;
12746 Nod : Node_Id;
12747
12748 begin
12749 if Nkind (Cont) = N_Compilation_Unit_Aux then
12750 Cont := Parent (Cont);
12751 end if;
12752
12753 Nod := Parent (N);
12754 while Present (Nod) loop
12755 if Nod = Cont then
12756 return True;
12757
12758 elsif Nkind_In (Nod, N_Accept_Statement,
12759 N_Block_Statement,
12760 N_Compilation_Unit,
12761 N_Entry_Body,
12762 N_Package_Body,
12763 N_Package_Declaration,
12764 N_Protected_Body,
12765 N_Subprogram_Body,
12766 N_Task_Body)
12767 then
12768 return False;
12769
12770 elsif Nkind (Nod) = N_Subunit then
12771 Nod := Corresponding_Stub (Nod);
12772
12773 else
12774 Nod := Parent (Nod);
12775 end if;
12776 end loop;
12777
12778 return False;
12779 end In_Same_Declarative_Part;
12780
12781 --------------------------------------
12782 -- In_Subprogram_Or_Concurrent_Unit --
12783 --------------------------------------
12784
12785 function In_Subprogram_Or_Concurrent_Unit return Boolean is
12786 E : Entity_Id;
12787 K : Entity_Kind;
12788
12789 begin
12790 -- Use scope chain to check successively outer scopes
12791
12792 E := Current_Scope;
12793 loop
12794 K := Ekind (E);
12795
12796 if K in Subprogram_Kind
12797 or else K in Concurrent_Kind
12798 or else K in Generic_Subprogram_Kind
12799 then
12800 return True;
12801
12802 elsif E = Standard_Standard then
12803 return False;
12804 end if;
12805
12806 E := Scope (E);
12807 end loop;
12808 end In_Subprogram_Or_Concurrent_Unit;
12809
12810 ----------------
12811 -- In_Subtree --
12812 ----------------
12813
12814 function In_Subtree (N : Node_Id; Root : Node_Id) return Boolean is
12815 Curr : Node_Id;
12816
12817 begin
12818 Curr := N;
12819 while Present (Curr) loop
12820 if Curr = Root then
12821 return True;
12822 end if;
12823
12824 Curr := Parent (Curr);
12825 end loop;
12826
12827 return False;
12828 end In_Subtree;
12829
12830 ----------------
12831 -- In_Subtree --
12832 ----------------
12833
12834 function In_Subtree
12835 (N : Node_Id;
12836 Root1 : Node_Id;
12837 Root2 : Node_Id) return Boolean
12838 is
12839 Curr : Node_Id;
12840
12841 begin
12842 Curr := N;
12843 while Present (Curr) loop
12844 if Curr = Root1 or else Curr = Root2 then
12845 return True;
12846 end if;
12847
12848 Curr := Parent (Curr);
12849 end loop;
12850
12851 return False;
12852 end In_Subtree;
12853
12854 ---------------------
12855 -- In_Visible_Part --
12856 ---------------------
12857
12858 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
12859 begin
12860 return Is_Package_Or_Generic_Package (Scope_Id)
12861 and then In_Open_Scopes (Scope_Id)
12862 and then not In_Package_Body (Scope_Id)
12863 and then not In_Private_Part (Scope_Id);
12864 end In_Visible_Part;
12865
12866 -----------------------------
12867 -- In_While_Loop_Condition --
12868 -----------------------------
12869
12870 function In_While_Loop_Condition (N : Node_Id) return Boolean is
12871 Prev : Node_Id := N;
12872 P : Node_Id := Parent (N);
12873 -- P and Prev will be used for traversing the AST, while maintaining an
12874 -- invariant that P = Parent (Prev).
12875 begin
12876 loop
12877 if No (P) then
12878 return False;
12879 elsif Nkind (P) = N_Iteration_Scheme
12880 and then Prev = Condition (P)
12881 then
12882 return True;
12883 else
12884 Prev := P;
12885 P := Parent (P);
12886 end if;
12887 end loop;
12888 end In_While_Loop_Condition;
12889
12890 --------------------------------
12891 -- Incomplete_Or_Partial_View --
12892 --------------------------------
12893
12894 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
12895 function Inspect_Decls
12896 (Decls : List_Id;
12897 Taft : Boolean := False) return Entity_Id;
12898 -- Check whether a declarative region contains the incomplete or partial
12899 -- view of Id.
12900
12901 -------------------
12902 -- Inspect_Decls --
12903 -------------------
12904
12905 function Inspect_Decls
12906 (Decls : List_Id;
12907 Taft : Boolean := False) return Entity_Id
12908 is
12909 Decl : Node_Id;
12910 Match : Node_Id;
12911
12912 begin
12913 Decl := First (Decls);
12914 while Present (Decl) loop
12915 Match := Empty;
12916
12917 -- The partial view of a Taft-amendment type is an incomplete
12918 -- type.
12919
12920 if Taft then
12921 if Nkind (Decl) = N_Incomplete_Type_Declaration then
12922 Match := Defining_Identifier (Decl);
12923 end if;
12924
12925 -- Otherwise look for a private type whose full view matches the
12926 -- input type. Note that this checks full_type_declaration nodes
12927 -- to account for derivations from a private type where the type
12928 -- declaration hold the partial view and the full view is an
12929 -- itype.
12930
12931 elsif Nkind_In (Decl, N_Full_Type_Declaration,
12932 N_Private_Extension_Declaration,
12933 N_Private_Type_Declaration)
12934 then
12935 Match := Defining_Identifier (Decl);
12936 end if;
12937
12938 -- Guard against unanalyzed entities
12939
12940 if Present (Match)
12941 and then Is_Type (Match)
12942 and then Present (Full_View (Match))
12943 and then Full_View (Match) = Id
12944 then
12945 return Match;
12946 end if;
12947
12948 Next (Decl);
12949 end loop;
12950
12951 return Empty;
12952 end Inspect_Decls;
12953
12954 -- Local variables
12955
12956 Prev : Entity_Id;
12957
12958 -- Start of processing for Incomplete_Or_Partial_View
12959
12960 begin
12961 -- Deferred constant or incomplete type case
12962
12963 Prev := Current_Entity_In_Scope (Id);
12964
12965 if Present (Prev)
12966 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
12967 and then Present (Full_View (Prev))
12968 and then Full_View (Prev) = Id
12969 then
12970 return Prev;
12971 end if;
12972
12973 -- Private or Taft amendment type case
12974
12975 declare
12976 Pkg : constant Entity_Id := Scope (Id);
12977 Pkg_Decl : Node_Id := Pkg;
12978
12979 begin
12980 if Present (Pkg)
12981 and then Is_Package_Or_Generic_Package (Pkg)
12982 then
12983 while Nkind (Pkg_Decl) /= N_Package_Specification loop
12984 Pkg_Decl := Parent (Pkg_Decl);
12985 end loop;
12986
12987 -- It is knows that Typ has a private view, look for it in the
12988 -- visible declarations of the enclosing scope. A special case
12989 -- of this is when the two views have been exchanged - the full
12990 -- appears earlier than the private.
12991
12992 if Has_Private_Declaration (Id) then
12993 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
12994
12995 -- Exchanged view case, look in the private declarations
12996
12997 if No (Prev) then
12998 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
12999 end if;
13000
13001 return Prev;
13002
13003 -- Otherwise if this is the package body, then Typ is a potential
13004 -- Taft amendment type. The incomplete view should be located in
13005 -- the private declarations of the enclosing scope.
13006
13007 elsif In_Package_Body (Pkg) then
13008 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
13009 end if;
13010 end if;
13011 end;
13012
13013 -- The type has no incomplete or private view
13014
13015 return Empty;
13016 end Incomplete_Or_Partial_View;
13017
13018 ---------------------------------------
13019 -- Incomplete_View_From_Limited_With --
13020 ---------------------------------------
13021
13022 function Incomplete_View_From_Limited_With
13023 (Typ : Entity_Id) return Entity_Id
13024 is
13025 begin
13026 -- It might make sense to make this an attribute in Einfo, and set it
13027 -- in Sem_Ch10 in Build_Shadow_Entity. However, we're running short on
13028 -- slots for new attributes, and it seems a bit simpler to just search
13029 -- the Limited_View (if it exists) for an incomplete type whose
13030 -- Non_Limited_View is Typ.
13031
13032 if Ekind (Scope (Typ)) = E_Package
13033 and then Present (Limited_View (Scope (Typ)))
13034 then
13035 declare
13036 Ent : Entity_Id := First_Entity (Limited_View (Scope (Typ)));
13037 begin
13038 while Present (Ent) loop
13039 if Ekind (Ent) in Incomplete_Kind
13040 and then Non_Limited_View (Ent) = Typ
13041 then
13042 return Ent;
13043 end if;
13044
13045 Ent := Next_Entity (Ent);
13046 end loop;
13047 end;
13048 end if;
13049
13050 return Typ;
13051 end Incomplete_View_From_Limited_With;
13052
13053 ----------------------------------
13054 -- Indexed_Component_Bit_Offset --
13055 ----------------------------------
13056
13057 function Indexed_Component_Bit_Offset (N : Node_Id) return Uint is
13058 Exp : constant Node_Id := First (Expressions (N));
13059 Typ : constant Entity_Id := Etype (Prefix (N));
13060 Off : constant Uint := Component_Size (Typ);
13061 Ind : Node_Id;
13062
13063 begin
13064 -- Return early if the component size is not known or variable
13065
13066 if Off = No_Uint or else Off < Uint_0 then
13067 return No_Uint;
13068 end if;
13069
13070 -- Deal with the degenerate case of an empty component
13071
13072 if Off = Uint_0 then
13073 return Off;
13074 end if;
13075
13076 -- Check that both the index value and the low bound are known
13077
13078 if not Compile_Time_Known_Value (Exp) then
13079 return No_Uint;
13080 end if;
13081
13082 Ind := First_Index (Typ);
13083 if No (Ind) then
13084 return No_Uint;
13085 end if;
13086
13087 if Nkind (Ind) = N_Subtype_Indication then
13088 Ind := Constraint (Ind);
13089
13090 if Nkind (Ind) = N_Range_Constraint then
13091 Ind := Range_Expression (Ind);
13092 end if;
13093 end if;
13094
13095 if Nkind (Ind) /= N_Range
13096 or else not Compile_Time_Known_Value (Low_Bound (Ind))
13097 then
13098 return No_Uint;
13099 end if;
13100
13101 -- Return the scaled offset
13102
13103 return Off * (Expr_Value (Exp) - Expr_Value (Low_Bound ((Ind))));
13104 end Indexed_Component_Bit_Offset;
13105
13106 ----------------------------
13107 -- Inherit_Rep_Item_Chain --
13108 ----------------------------
13109
13110 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
13111 Item : Node_Id;
13112 Next_Item : Node_Id;
13113
13114 begin
13115 -- There are several inheritance scenarios to consider depending on
13116 -- whether both types have rep item chains and whether the destination
13117 -- type already inherits part of the source type's rep item chain.
13118
13119 -- 1) The source type lacks a rep item chain
13120 -- From_Typ ---> Empty
13121 --
13122 -- Typ --------> Item (or Empty)
13123
13124 -- In this case inheritance cannot take place because there are no items
13125 -- to inherit.
13126
13127 -- 2) The destination type lacks a rep item chain
13128 -- From_Typ ---> Item ---> ...
13129 --
13130 -- Typ --------> Empty
13131
13132 -- Inheritance takes place by setting the First_Rep_Item of the
13133 -- destination type to the First_Rep_Item of the source type.
13134 -- From_Typ ---> Item ---> ...
13135 -- ^
13136 -- Typ -----------+
13137
13138 -- 3.1) Both source and destination types have at least one rep item.
13139 -- The destination type does NOT inherit a rep item from the source
13140 -- type.
13141 -- From_Typ ---> Item ---> Item
13142 --
13143 -- Typ --------> Item ---> Item
13144
13145 -- Inheritance takes place by setting the Next_Rep_Item of the last item
13146 -- of the destination type to the First_Rep_Item of the source type.
13147 -- From_Typ -------------------> Item ---> Item
13148 -- ^
13149 -- Typ --------> Item ---> Item --+
13150
13151 -- 3.2) Both source and destination types have at least one rep item.
13152 -- The destination type DOES inherit part of the rep item chain of the
13153 -- source type.
13154 -- From_Typ ---> Item ---> Item ---> Item
13155 -- ^
13156 -- Typ --------> Item ------+
13157
13158 -- This rare case arises when the full view of a private extension must
13159 -- inherit the rep item chain from the full view of its parent type and
13160 -- the full view of the parent type contains extra rep items. Currently
13161 -- only invariants may lead to such form of inheritance.
13162
13163 -- type From_Typ is tagged private
13164 -- with Type_Invariant'Class => Item_2;
13165
13166 -- type Typ is new From_Typ with private
13167 -- with Type_Invariant => Item_4;
13168
13169 -- At this point the rep item chains contain the following items
13170
13171 -- From_Typ -----------> Item_2 ---> Item_3
13172 -- ^
13173 -- Typ --------> Item_4 --+
13174
13175 -- The full views of both types may introduce extra invariants
13176
13177 -- type From_Typ is tagged null record
13178 -- with Type_Invariant => Item_1;
13179
13180 -- type Typ is new From_Typ with null record;
13181
13182 -- The full view of Typ would have to inherit any new rep items added to
13183 -- the full view of From_Typ.
13184
13185 -- From_Typ -----------> Item_1 ---> Item_2 ---> Item_3
13186 -- ^
13187 -- Typ --------> Item_4 --+
13188
13189 -- To achieve this form of inheritance, the destination type must first
13190 -- sever the link between its own rep chain and that of the source type,
13191 -- then inheritance 3.1 takes place.
13192
13193 -- Case 1: The source type lacks a rep item chain
13194
13195 if No (First_Rep_Item (From_Typ)) then
13196 return;
13197
13198 -- Case 2: The destination type lacks a rep item chain
13199
13200 elsif No (First_Rep_Item (Typ)) then
13201 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
13202
13203 -- Case 3: Both the source and destination types have at least one rep
13204 -- item. Traverse the rep item chain of the destination type to find the
13205 -- last rep item.
13206
13207 else
13208 Item := Empty;
13209 Next_Item := First_Rep_Item (Typ);
13210 while Present (Next_Item) loop
13211
13212 -- Detect a link between the destination type's rep chain and that
13213 -- of the source type. There are two possibilities:
13214
13215 -- Variant 1
13216 -- Next_Item
13217 -- V
13218 -- From_Typ ---> Item_1 --->
13219 -- ^
13220 -- Typ -----------+
13221 --
13222 -- Item is Empty
13223
13224 -- Variant 2
13225 -- Next_Item
13226 -- V
13227 -- From_Typ ---> Item_1 ---> Item_2 --->
13228 -- ^
13229 -- Typ --------> Item_3 ------+
13230 -- ^
13231 -- Item
13232
13233 if Has_Rep_Item (From_Typ, Next_Item) then
13234 exit;
13235 end if;
13236
13237 Item := Next_Item;
13238 Next_Item := Next_Rep_Item (Next_Item);
13239 end loop;
13240
13241 -- Inherit the source type's rep item chain
13242
13243 if Present (Item) then
13244 Set_Next_Rep_Item (Item, First_Rep_Item (From_Typ));
13245 else
13246 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
13247 end if;
13248 end if;
13249 end Inherit_Rep_Item_Chain;
13250
13251 ------------------------------------
13252 -- Inherits_From_Tagged_Full_View --
13253 ------------------------------------
13254
13255 function Inherits_From_Tagged_Full_View (Typ : Entity_Id) return Boolean is
13256 begin
13257 return Is_Private_Type (Typ)
13258 and then Present (Full_View (Typ))
13259 and then Is_Private_Type (Full_View (Typ))
13260 and then not Is_Tagged_Type (Full_View (Typ))
13261 and then Present (Underlying_Type (Full_View (Typ)))
13262 and then Is_Tagged_Type (Underlying_Type (Full_View (Typ)));
13263 end Inherits_From_Tagged_Full_View;
13264
13265 ---------------------------------
13266 -- Insert_Explicit_Dereference --
13267 ---------------------------------
13268
13269 procedure Insert_Explicit_Dereference (N : Node_Id) is
13270 New_Prefix : constant Node_Id := Relocate_Node (N);
13271 Ent : Entity_Id := Empty;
13272 Pref : Node_Id;
13273 I : Interp_Index;
13274 It : Interp;
13275 T : Entity_Id;
13276
13277 begin
13278 Save_Interps (N, New_Prefix);
13279
13280 Rewrite (N,
13281 Make_Explicit_Dereference (Sloc (Parent (N)),
13282 Prefix => New_Prefix));
13283
13284 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
13285
13286 if Is_Overloaded (New_Prefix) then
13287
13288 -- The dereference is also overloaded, and its interpretations are
13289 -- the designated types of the interpretations of the original node.
13290
13291 Set_Etype (N, Any_Type);
13292
13293 Get_First_Interp (New_Prefix, I, It);
13294 while Present (It.Nam) loop
13295 T := It.Typ;
13296
13297 if Is_Access_Type (T) then
13298 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
13299 end if;
13300
13301 Get_Next_Interp (I, It);
13302 end loop;
13303
13304 End_Interp_List;
13305
13306 else
13307 -- Prefix is unambiguous: mark the original prefix (which might
13308 -- Come_From_Source) as a reference, since the new (relocated) one
13309 -- won't be taken into account.
13310
13311 if Is_Entity_Name (New_Prefix) then
13312 Ent := Entity (New_Prefix);
13313 Pref := New_Prefix;
13314
13315 -- For a retrieval of a subcomponent of some composite object,
13316 -- retrieve the ultimate entity if there is one.
13317
13318 elsif Nkind_In (New_Prefix, N_Selected_Component,
13319 N_Indexed_Component)
13320 then
13321 Pref := Prefix (New_Prefix);
13322 while Present (Pref)
13323 and then Nkind_In (Pref, N_Selected_Component,
13324 N_Indexed_Component)
13325 loop
13326 Pref := Prefix (Pref);
13327 end loop;
13328
13329 if Present (Pref) and then Is_Entity_Name (Pref) then
13330 Ent := Entity (Pref);
13331 end if;
13332 end if;
13333
13334 -- Place the reference on the entity node
13335
13336 if Present (Ent) then
13337 Generate_Reference (Ent, Pref);
13338 end if;
13339 end if;
13340 end Insert_Explicit_Dereference;
13341
13342 ------------------------------------------
13343 -- Inspect_Deferred_Constant_Completion --
13344 ------------------------------------------
13345
13346 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
13347 Decl : Node_Id;
13348
13349 begin
13350 Decl := First (Decls);
13351 while Present (Decl) loop
13352
13353 -- Deferred constant signature
13354
13355 if Nkind (Decl) = N_Object_Declaration
13356 and then Constant_Present (Decl)
13357 and then No (Expression (Decl))
13358
13359 -- No need to check internally generated constants
13360
13361 and then Comes_From_Source (Decl)
13362
13363 -- The constant is not completed. A full object declaration or a
13364 -- pragma Import complete a deferred constant.
13365
13366 and then not Has_Completion (Defining_Identifier (Decl))
13367 then
13368 Error_Msg_N
13369 ("constant declaration requires initialization expression",
13370 Defining_Identifier (Decl));
13371 end if;
13372
13373 Decl := Next (Decl);
13374 end loop;
13375 end Inspect_Deferred_Constant_Completion;
13376
13377 -------------------------------
13378 -- Install_Elaboration_Model --
13379 -------------------------------
13380
13381 procedure Install_Elaboration_Model (Unit_Id : Entity_Id) is
13382 function Find_Elaboration_Checks_Pragma (L : List_Id) return Node_Id;
13383 -- Try to find pragma Elaboration_Checks in arbitrary list L. Return
13384 -- Empty if there is no such pragma.
13385
13386 ------------------------------------
13387 -- Find_Elaboration_Checks_Pragma --
13388 ------------------------------------
13389
13390 function Find_Elaboration_Checks_Pragma (L : List_Id) return Node_Id is
13391 Item : Node_Id;
13392
13393 begin
13394 Item := First (L);
13395 while Present (Item) loop
13396 if Nkind (Item) = N_Pragma
13397 and then Pragma_Name (Item) = Name_Elaboration_Checks
13398 then
13399 return Item;
13400 end if;
13401
13402 Next (Item);
13403 end loop;
13404
13405 return Empty;
13406 end Find_Elaboration_Checks_Pragma;
13407
13408 -- Local variables
13409
13410 Args : List_Id;
13411 Model : Node_Id;
13412 Prag : Node_Id;
13413 Unit : Node_Id;
13414
13415 -- Start of processing for Install_Elaboration_Model
13416
13417 begin
13418 -- Nothing to do when the unit does not exist
13419
13420 if No (Unit_Id) then
13421 return;
13422 end if;
13423
13424 Unit := Parent (Unit_Declaration_Node (Unit_Id));
13425
13426 -- Nothing to do when the unit is not a library unit
13427
13428 if Nkind (Unit) /= N_Compilation_Unit then
13429 return;
13430 end if;
13431
13432 Prag := Find_Elaboration_Checks_Pragma (Context_Items (Unit));
13433
13434 -- The compilation unit is subject to pragma Elaboration_Checks. Set the
13435 -- elaboration model as specified by the pragma.
13436
13437 if Present (Prag) then
13438 Args := Pragma_Argument_Associations (Prag);
13439
13440 -- Guard against an illegal pragma. The sole argument must be an
13441 -- identifier which specifies either Dynamic or Static model.
13442
13443 if Present (Args) then
13444 Model := Get_Pragma_Arg (First (Args));
13445
13446 if Nkind (Model) = N_Identifier then
13447 Dynamic_Elaboration_Checks := Chars (Model) = Name_Dynamic;
13448 end if;
13449 end if;
13450 end if;
13451 end Install_Elaboration_Model;
13452
13453 -----------------------------
13454 -- Install_Generic_Formals --
13455 -----------------------------
13456
13457 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
13458 E : Entity_Id;
13459
13460 begin
13461 pragma Assert (Is_Generic_Subprogram (Subp_Id));
13462
13463 E := First_Entity (Subp_Id);
13464 while Present (E) loop
13465 Install_Entity (E);
13466 Next_Entity (E);
13467 end loop;
13468 end Install_Generic_Formals;
13469
13470 ------------------------
13471 -- Install_SPARK_Mode --
13472 ------------------------
13473
13474 procedure Install_SPARK_Mode (Mode : SPARK_Mode_Type; Prag : Node_Id) is
13475 begin
13476 SPARK_Mode := Mode;
13477 SPARK_Mode_Pragma := Prag;
13478 end Install_SPARK_Mode;
13479
13480 --------------------------
13481 -- Invalid_Scalar_Value --
13482 --------------------------
13483
13484 function Invalid_Scalar_Value
13485 (Loc : Source_Ptr;
13486 Scal_Typ : Scalar_Id) return Node_Id
13487 is
13488 function Invalid_Binder_Value return Node_Id;
13489 -- Return a reference to the corresponding invalid value for type
13490 -- Scal_Typ as defined in unit System.Scalar_Values.
13491
13492 function Invalid_Float_Value return Node_Id;
13493 -- Return the invalid value of float type Scal_Typ
13494
13495 function Invalid_Integer_Value return Node_Id;
13496 -- Return the invalid value of integer type Scal_Typ
13497
13498 procedure Set_Invalid_Binder_Values;
13499 -- Set the contents of collection Invalid_Binder_Values
13500
13501 --------------------------
13502 -- Invalid_Binder_Value --
13503 --------------------------
13504
13505 function Invalid_Binder_Value return Node_Id is
13506 Val_Id : Entity_Id;
13507
13508 begin
13509 -- Initialize the collection of invalid binder values the first time
13510 -- around.
13511
13512 Set_Invalid_Binder_Values;
13513
13514 -- Obtain the corresponding variable from System.Scalar_Values which
13515 -- holds the invalid value for this type.
13516
13517 Val_Id := Invalid_Binder_Values (Scal_Typ);
13518 pragma Assert (Present (Val_Id));
13519
13520 return New_Occurrence_Of (Val_Id, Loc);
13521 end Invalid_Binder_Value;
13522
13523 -------------------------
13524 -- Invalid_Float_Value --
13525 -------------------------
13526
13527 function Invalid_Float_Value return Node_Id is
13528 Value : constant Ureal := Invalid_Floats (Scal_Typ);
13529
13530 begin
13531 -- Pragma Invalid_Scalars did not specify an invalid value for this
13532 -- type. Fall back to the value provided by the binder.
13533
13534 if Value = No_Ureal then
13535 return Invalid_Binder_Value;
13536 else
13537 return Make_Real_Literal (Loc, Realval => Value);
13538 end if;
13539 end Invalid_Float_Value;
13540
13541 ---------------------------
13542 -- Invalid_Integer_Value --
13543 ---------------------------
13544
13545 function Invalid_Integer_Value return Node_Id is
13546 Value : constant Uint := Invalid_Integers (Scal_Typ);
13547
13548 begin
13549 -- Pragma Invalid_Scalars did not specify an invalid value for this
13550 -- type. Fall back to the value provided by the binder.
13551
13552 if Value = No_Uint then
13553 return Invalid_Binder_Value;
13554 else
13555 return Make_Integer_Literal (Loc, Intval => Value);
13556 end if;
13557 end Invalid_Integer_Value;
13558
13559 -------------------------------
13560 -- Set_Invalid_Binder_Values --
13561 -------------------------------
13562
13563 procedure Set_Invalid_Binder_Values is
13564 begin
13565 if not Invalid_Binder_Values_Set then
13566 Invalid_Binder_Values_Set := True;
13567
13568 -- Initialize the contents of the collection once since RTE calls
13569 -- are not cheap.
13570
13571 Invalid_Binder_Values :=
13572 (Name_Short_Float => RTE (RE_IS_Isf),
13573 Name_Float => RTE (RE_IS_Ifl),
13574 Name_Long_Float => RTE (RE_IS_Ilf),
13575 Name_Long_Long_Float => RTE (RE_IS_Ill),
13576 Name_Signed_8 => RTE (RE_IS_Is1),
13577 Name_Signed_16 => RTE (RE_IS_Is2),
13578 Name_Signed_32 => RTE (RE_IS_Is4),
13579 Name_Signed_64 => RTE (RE_IS_Is8),
13580 Name_Unsigned_8 => RTE (RE_IS_Iu1),
13581 Name_Unsigned_16 => RTE (RE_IS_Iu2),
13582 Name_Unsigned_32 => RTE (RE_IS_Iu4),
13583 Name_Unsigned_64 => RTE (RE_IS_Iu8));
13584 end if;
13585 end Set_Invalid_Binder_Values;
13586
13587 -- Start of processing for Invalid_Scalar_Value
13588
13589 begin
13590 if Scal_Typ in Float_Scalar_Id then
13591 return Invalid_Float_Value;
13592
13593 else pragma Assert (Scal_Typ in Integer_Scalar_Id);
13594 return Invalid_Integer_Value;
13595 end if;
13596 end Invalid_Scalar_Value;
13597
13598 -----------------------------
13599 -- Is_Actual_Out_Parameter --
13600 -----------------------------
13601
13602 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
13603 Formal : Entity_Id;
13604 Call : Node_Id;
13605 begin
13606 Find_Actual (N, Formal, Call);
13607 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
13608 end Is_Actual_Out_Parameter;
13609
13610 -------------------------
13611 -- Is_Actual_Parameter --
13612 -------------------------
13613
13614 function Is_Actual_Parameter (N : Node_Id) return Boolean is
13615 PK : constant Node_Kind := Nkind (Parent (N));
13616
13617 begin
13618 case PK is
13619 when N_Parameter_Association =>
13620 return N = Explicit_Actual_Parameter (Parent (N));
13621
13622 when N_Subprogram_Call =>
13623 return Is_List_Member (N)
13624 and then
13625 List_Containing (N) = Parameter_Associations (Parent (N));
13626
13627 when others =>
13628 return False;
13629 end case;
13630 end Is_Actual_Parameter;
13631
13632 --------------------------------
13633 -- Is_Actual_Tagged_Parameter --
13634 --------------------------------
13635
13636 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
13637 Formal : Entity_Id;
13638 Call : Node_Id;
13639 begin
13640 Find_Actual (N, Formal, Call);
13641 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
13642 end Is_Actual_Tagged_Parameter;
13643
13644 ---------------------
13645 -- Is_Aliased_View --
13646 ---------------------
13647
13648 function Is_Aliased_View (Obj : Node_Id) return Boolean is
13649 E : Entity_Id;
13650
13651 begin
13652 if Is_Entity_Name (Obj) then
13653 E := Entity (Obj);
13654
13655 return
13656 (Is_Object (E)
13657 and then
13658 (Is_Aliased (E)
13659 or else (Present (Renamed_Object (E))
13660 and then Is_Aliased_View (Renamed_Object (E)))))
13661
13662 or else ((Is_Formal (E) or else Is_Formal_Object (E))
13663 and then Is_Tagged_Type (Etype (E)))
13664
13665 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
13666
13667 -- Current instance of type, either directly or as rewritten
13668 -- reference to the current object.
13669
13670 or else (Is_Entity_Name (Original_Node (Obj))
13671 and then Present (Entity (Original_Node (Obj)))
13672 and then Is_Type (Entity (Original_Node (Obj))))
13673
13674 or else (Is_Type (E) and then E = Current_Scope)
13675
13676 or else (Is_Incomplete_Or_Private_Type (E)
13677 and then Full_View (E) = Current_Scope)
13678
13679 -- Ada 2012 AI05-0053: the return object of an extended return
13680 -- statement is aliased if its type is immutably limited.
13681
13682 or else (Is_Return_Object (E)
13683 and then Is_Limited_View (Etype (E)));
13684
13685 elsif Nkind (Obj) = N_Selected_Component then
13686 return Is_Aliased (Entity (Selector_Name (Obj)));
13687
13688 elsif Nkind (Obj) = N_Indexed_Component then
13689 return Has_Aliased_Components (Etype (Prefix (Obj)))
13690 or else
13691 (Is_Access_Type (Etype (Prefix (Obj)))
13692 and then Has_Aliased_Components
13693 (Designated_Type (Etype (Prefix (Obj)))));
13694
13695 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
13696 return Is_Tagged_Type (Etype (Obj))
13697 and then Is_Aliased_View (Expression (Obj));
13698
13699 elsif Nkind (Obj) = N_Explicit_Dereference then
13700 return Nkind (Original_Node (Obj)) /= N_Function_Call;
13701
13702 else
13703 return False;
13704 end if;
13705 end Is_Aliased_View;
13706
13707 -------------------------
13708 -- Is_Ancestor_Package --
13709 -------------------------
13710
13711 function Is_Ancestor_Package
13712 (E1 : Entity_Id;
13713 E2 : Entity_Id) return Boolean
13714 is
13715 Par : Entity_Id;
13716
13717 begin
13718 Par := E2;
13719 while Present (Par) and then Par /= Standard_Standard loop
13720 if Par = E1 then
13721 return True;
13722 end if;
13723
13724 Par := Scope (Par);
13725 end loop;
13726
13727 return False;
13728 end Is_Ancestor_Package;
13729
13730 ----------------------
13731 -- Is_Atomic_Object --
13732 ----------------------
13733
13734 function Is_Atomic_Object (N : Node_Id) return Boolean is
13735 function Prefix_Has_Atomic_Components (P : Node_Id) return Boolean;
13736 -- Determine whether prefix P has atomic components. This requires the
13737 -- presence of an Atomic_Components aspect/pragma.
13738
13739 ---------------------------------
13740 -- Prefix_Has_Atomic_Components --
13741 ---------------------------------
13742
13743 function Prefix_Has_Atomic_Components (P : Node_Id) return Boolean is
13744 Typ : constant Entity_Id := Etype (P);
13745
13746 begin
13747 if Is_Access_Type (Typ) then
13748 return Has_Atomic_Components (Designated_Type (Typ));
13749
13750 elsif Has_Atomic_Components (Typ) then
13751 return True;
13752
13753 elsif Is_Entity_Name (P)
13754 and then Has_Atomic_Components (Entity (P))
13755 then
13756 return True;
13757
13758 else
13759 return False;
13760 end if;
13761 end Prefix_Has_Atomic_Components;
13762
13763 -- Start of processing for Is_Atomic_Object
13764
13765 begin
13766 if Is_Entity_Name (N) then
13767 return Is_Atomic_Object_Entity (Entity (N));
13768
13769 elsif Is_Atomic (Etype (N)) then
13770 return True;
13771
13772 elsif Nkind (N) = N_Indexed_Component then
13773 return Prefix_Has_Atomic_Components (Prefix (N));
13774
13775 elsif Nkind (N) = N_Selected_Component then
13776 return Is_Atomic (Entity (Selector_Name (N)));
13777
13778 else
13779 return False;
13780 end if;
13781 end Is_Atomic_Object;
13782
13783 -----------------------------
13784 -- Is_Atomic_Object_Entity --
13785 -----------------------------
13786
13787 function Is_Atomic_Object_Entity (Id : Entity_Id) return Boolean is
13788 begin
13789 return
13790 Is_Object (Id)
13791 and then (Is_Atomic (Id) or else Is_Atomic (Etype (Id)));
13792 end Is_Atomic_Object_Entity;
13793
13794 -----------------------------
13795 -- Is_Atomic_Or_VFA_Object --
13796 -----------------------------
13797
13798 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
13799 begin
13800 return Is_Atomic_Object (N) or else Is_Volatile_Full_Access_Object (N);
13801 end Is_Atomic_Or_VFA_Object;
13802
13803 ----------------------
13804 -- Is_Attribute_Old --
13805 ----------------------
13806
13807 function Is_Attribute_Old (N : Node_Id) return Boolean is
13808 begin
13809 return Nkind (N) = N_Attribute_Reference
13810 and then Attribute_Name (N) = Name_Old;
13811 end Is_Attribute_Old;
13812
13813 -------------------------
13814 -- Is_Attribute_Result --
13815 -------------------------
13816
13817 function Is_Attribute_Result (N : Node_Id) return Boolean is
13818 begin
13819 return Nkind (N) = N_Attribute_Reference
13820 and then Attribute_Name (N) = Name_Result;
13821 end Is_Attribute_Result;
13822
13823 -------------------------
13824 -- Is_Attribute_Update --
13825 -------------------------
13826
13827 function Is_Attribute_Update (N : Node_Id) return Boolean is
13828 begin
13829 return Nkind (N) = N_Attribute_Reference
13830 and then Attribute_Name (N) = Name_Update;
13831 end Is_Attribute_Update;
13832
13833 ------------------------------------
13834 -- Is_Body_Or_Package_Declaration --
13835 ------------------------------------
13836
13837 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
13838 begin
13839 return Is_Body (N) or else Nkind (N) = N_Package_Declaration;
13840 end Is_Body_Or_Package_Declaration;
13841
13842 -----------------------
13843 -- Is_Bounded_String --
13844 -----------------------
13845
13846 function Is_Bounded_String (T : Entity_Id) return Boolean is
13847 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
13848
13849 begin
13850 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
13851 -- Super_String, or one of the [Wide_]Wide_ versions. This will
13852 -- be True for all the Bounded_String types in instances of the
13853 -- Generic_Bounded_Length generics, and for types derived from those.
13854
13855 return Present (Under)
13856 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
13857 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
13858 Is_RTE (Root_Type (Under), RO_WW_Super_String));
13859 end Is_Bounded_String;
13860
13861 ---------------------
13862 -- Is_CCT_Instance --
13863 ---------------------
13864
13865 function Is_CCT_Instance
13866 (Ref_Id : Entity_Id;
13867 Context_Id : Entity_Id) return Boolean
13868 is
13869 begin
13870 pragma Assert (Ekind_In (Ref_Id, E_Protected_Type, E_Task_Type));
13871
13872 if Is_Single_Task_Object (Context_Id) then
13873 return Scope_Within_Or_Same (Etype (Context_Id), Ref_Id);
13874
13875 else
13876 pragma Assert (Ekind_In (Context_Id, E_Entry,
13877 E_Entry_Family,
13878 E_Function,
13879 E_Package,
13880 E_Procedure,
13881 E_Protected_Type,
13882 E_Task_Type)
13883 or else
13884 Is_Record_Type (Context_Id));
13885 return Scope_Within_Or_Same (Context_Id, Ref_Id);
13886 end if;
13887 end Is_CCT_Instance;
13888
13889 -------------------------
13890 -- Is_Child_Or_Sibling --
13891 -------------------------
13892
13893 function Is_Child_Or_Sibling
13894 (Pack_1 : Entity_Id;
13895 Pack_2 : Entity_Id) return Boolean
13896 is
13897 function Distance_From_Standard (Pack : Entity_Id) return Nat;
13898 -- Given an arbitrary package, return the number of "climbs" necessary
13899 -- to reach scope Standard_Standard.
13900
13901 procedure Equalize_Depths
13902 (Pack : in out Entity_Id;
13903 Depth : in out Nat;
13904 Depth_To_Reach : Nat);
13905 -- Given an arbitrary package, its depth and a target depth to reach,
13906 -- climb the scope chain until the said depth is reached. The pointer
13907 -- to the package and its depth a modified during the climb.
13908
13909 ----------------------------
13910 -- Distance_From_Standard --
13911 ----------------------------
13912
13913 function Distance_From_Standard (Pack : Entity_Id) return Nat is
13914 Dist : Nat;
13915 Scop : Entity_Id;
13916
13917 begin
13918 Dist := 0;
13919 Scop := Pack;
13920 while Present (Scop) and then Scop /= Standard_Standard loop
13921 Dist := Dist + 1;
13922 Scop := Scope (Scop);
13923 end loop;
13924
13925 return Dist;
13926 end Distance_From_Standard;
13927
13928 ---------------------
13929 -- Equalize_Depths --
13930 ---------------------
13931
13932 procedure Equalize_Depths
13933 (Pack : in out Entity_Id;
13934 Depth : in out Nat;
13935 Depth_To_Reach : Nat)
13936 is
13937 begin
13938 -- The package must be at a greater or equal depth
13939
13940 if Depth < Depth_To_Reach then
13941 raise Program_Error;
13942 end if;
13943
13944 -- Climb the scope chain until the desired depth is reached
13945
13946 while Present (Pack) and then Depth /= Depth_To_Reach loop
13947 Pack := Scope (Pack);
13948 Depth := Depth - 1;
13949 end loop;
13950 end Equalize_Depths;
13951
13952 -- Local variables
13953
13954 P_1 : Entity_Id := Pack_1;
13955 P_1_Child : Boolean := False;
13956 P_1_Depth : Nat := Distance_From_Standard (P_1);
13957 P_2 : Entity_Id := Pack_2;
13958 P_2_Child : Boolean := False;
13959 P_2_Depth : Nat := Distance_From_Standard (P_2);
13960
13961 -- Start of processing for Is_Child_Or_Sibling
13962
13963 begin
13964 pragma Assert
13965 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
13966
13967 -- Both packages denote the same entity, therefore they cannot be
13968 -- children or siblings.
13969
13970 if P_1 = P_2 then
13971 return False;
13972
13973 -- One of the packages is at a deeper level than the other. Note that
13974 -- both may still come from different hierarchies.
13975
13976 -- (root) P_2
13977 -- / \ :
13978 -- X P_2 or X
13979 -- : :
13980 -- P_1 P_1
13981
13982 elsif P_1_Depth > P_2_Depth then
13983 Equalize_Depths
13984 (Pack => P_1,
13985 Depth => P_1_Depth,
13986 Depth_To_Reach => P_2_Depth);
13987 P_1_Child := True;
13988
13989 -- (root) P_1
13990 -- / \ :
13991 -- P_1 X or X
13992 -- : :
13993 -- P_2 P_2
13994
13995 elsif P_2_Depth > P_1_Depth then
13996 Equalize_Depths
13997 (Pack => P_2,
13998 Depth => P_2_Depth,
13999 Depth_To_Reach => P_1_Depth);
14000 P_2_Child := True;
14001 end if;
14002
14003 -- At this stage the package pointers have been elevated to the same
14004 -- depth. If the related entities are the same, then one package is a
14005 -- potential child of the other:
14006
14007 -- P_1
14008 -- :
14009 -- X became P_1 P_2 or vice versa
14010 -- :
14011 -- P_2
14012
14013 if P_1 = P_2 then
14014 if P_1_Child then
14015 return Is_Child_Unit (Pack_1);
14016
14017 else pragma Assert (P_2_Child);
14018 return Is_Child_Unit (Pack_2);
14019 end if;
14020
14021 -- The packages may come from the same package chain or from entirely
14022 -- different hierarcies. To determine this, climb the scope stack until
14023 -- a common root is found.
14024
14025 -- (root) (root 1) (root 2)
14026 -- / \ | |
14027 -- P_1 P_2 P_1 P_2
14028
14029 else
14030 while Present (P_1) and then Present (P_2) loop
14031
14032 -- The two packages may be siblings
14033
14034 if P_1 = P_2 then
14035 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
14036 end if;
14037
14038 P_1 := Scope (P_1);
14039 P_2 := Scope (P_2);
14040 end loop;
14041 end if;
14042
14043 return False;
14044 end Is_Child_Or_Sibling;
14045
14046 -----------------------------
14047 -- Is_Concurrent_Interface --
14048 -----------------------------
14049
14050 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
14051 begin
14052 return Is_Interface (T)
14053 and then
14054 (Is_Protected_Interface (T)
14055 or else Is_Synchronized_Interface (T)
14056 or else Is_Task_Interface (T));
14057 end Is_Concurrent_Interface;
14058
14059 -----------------------
14060 -- Is_Constant_Bound --
14061 -----------------------
14062
14063 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
14064 begin
14065 if Compile_Time_Known_Value (Exp) then
14066 return True;
14067
14068 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
14069 return Is_Constant_Object (Entity (Exp))
14070 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
14071
14072 elsif Nkind (Exp) in N_Binary_Op then
14073 return Is_Constant_Bound (Left_Opnd (Exp))
14074 and then Is_Constant_Bound (Right_Opnd (Exp))
14075 and then Scope (Entity (Exp)) = Standard_Standard;
14076
14077 else
14078 return False;
14079 end if;
14080 end Is_Constant_Bound;
14081
14082 ---------------------------
14083 -- Is_Container_Element --
14084 ---------------------------
14085
14086 function Is_Container_Element (Exp : Node_Id) return Boolean is
14087 Loc : constant Source_Ptr := Sloc (Exp);
14088 Pref : constant Node_Id := Prefix (Exp);
14089
14090 Call : Node_Id;
14091 -- Call to an indexing aspect
14092
14093 Cont_Typ : Entity_Id;
14094 -- The type of the container being accessed
14095
14096 Elem_Typ : Entity_Id;
14097 -- Its element type
14098
14099 Indexing : Entity_Id;
14100 Is_Const : Boolean;
14101 -- Indicates that constant indexing is used, and the element is thus
14102 -- a constant.
14103
14104 Ref_Typ : Entity_Id;
14105 -- The reference type returned by the indexing operation
14106
14107 begin
14108 -- If C is a container, in a context that imposes the element type of
14109 -- that container, the indexing notation C (X) is rewritten as:
14110
14111 -- Indexing (C, X).Discr.all
14112
14113 -- where Indexing is one of the indexing aspects of the container.
14114 -- If the context does not require a reference, the construct can be
14115 -- rewritten as
14116
14117 -- Element (C, X)
14118
14119 -- First, verify that the construct has the proper form
14120
14121 if not Expander_Active then
14122 return False;
14123
14124 elsif Nkind (Pref) /= N_Selected_Component then
14125 return False;
14126
14127 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
14128 return False;
14129
14130 else
14131 Call := Prefix (Pref);
14132 Ref_Typ := Etype (Call);
14133 end if;
14134
14135 if not Has_Implicit_Dereference (Ref_Typ)
14136 or else No (First (Parameter_Associations (Call)))
14137 or else not Is_Entity_Name (Name (Call))
14138 then
14139 return False;
14140 end if;
14141
14142 -- Retrieve type of container object, and its iterator aspects
14143
14144 Cont_Typ := Etype (First (Parameter_Associations (Call)));
14145 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
14146 Is_Const := False;
14147
14148 if No (Indexing) then
14149
14150 -- Container should have at least one indexing operation
14151
14152 return False;
14153
14154 elsif Entity (Name (Call)) /= Entity (Indexing) then
14155
14156 -- This may be a variable indexing operation
14157
14158 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
14159
14160 if No (Indexing)
14161 or else Entity (Name (Call)) /= Entity (Indexing)
14162 then
14163 return False;
14164 end if;
14165
14166 else
14167 Is_Const := True;
14168 end if;
14169
14170 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
14171
14172 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
14173 return False;
14174 end if;
14175
14176 -- Check that the expression is not the target of an assignment, in
14177 -- which case the rewriting is not possible.
14178
14179 if not Is_Const then
14180 declare
14181 Par : Node_Id;
14182
14183 begin
14184 Par := Exp;
14185 while Present (Par)
14186 loop
14187 if Nkind (Parent (Par)) = N_Assignment_Statement
14188 and then Par = Name (Parent (Par))
14189 then
14190 return False;
14191
14192 -- A renaming produces a reference, and the transformation
14193 -- does not apply.
14194
14195 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
14196 return False;
14197
14198 elsif Nkind_In
14199 (Nkind (Parent (Par)), N_Function_Call,
14200 N_Procedure_Call_Statement,
14201 N_Entry_Call_Statement)
14202 then
14203 -- Check that the element is not part of an actual for an
14204 -- in-out parameter.
14205
14206 declare
14207 F : Entity_Id;
14208 A : Node_Id;
14209
14210 begin
14211 F := First_Formal (Entity (Name (Parent (Par))));
14212 A := First (Parameter_Associations (Parent (Par)));
14213 while Present (F) loop
14214 if A = Par and then Ekind (F) /= E_In_Parameter then
14215 return False;
14216 end if;
14217
14218 Next_Formal (F);
14219 Next (A);
14220 end loop;
14221 end;
14222
14223 -- E_In_Parameter in a call: element is not modified.
14224
14225 exit;
14226 end if;
14227
14228 Par := Parent (Par);
14229 end loop;
14230 end;
14231 end if;
14232
14233 -- The expression has the proper form and the context requires the
14234 -- element type. Retrieve the Element function of the container and
14235 -- rewrite the construct as a call to it.
14236
14237 declare
14238 Op : Elmt_Id;
14239
14240 begin
14241 Op := First_Elmt (Primitive_Operations (Cont_Typ));
14242 while Present (Op) loop
14243 exit when Chars (Node (Op)) = Name_Element;
14244 Next_Elmt (Op);
14245 end loop;
14246
14247 if No (Op) then
14248 return False;
14249
14250 else
14251 Rewrite (Exp,
14252 Make_Function_Call (Loc,
14253 Name => New_Occurrence_Of (Node (Op), Loc),
14254 Parameter_Associations => Parameter_Associations (Call)));
14255 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
14256 return True;
14257 end if;
14258 end;
14259 end Is_Container_Element;
14260
14261 ----------------------------
14262 -- Is_Contract_Annotation --
14263 ----------------------------
14264
14265 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
14266 begin
14267 return Is_Package_Contract_Annotation (Item)
14268 or else
14269 Is_Subprogram_Contract_Annotation (Item);
14270 end Is_Contract_Annotation;
14271
14272 --------------------------------------
14273 -- Is_Controlling_Limited_Procedure --
14274 --------------------------------------
14275
14276 function Is_Controlling_Limited_Procedure
14277 (Proc_Nam : Entity_Id) return Boolean
14278 is
14279 Param : Node_Id;
14280 Param_Typ : Entity_Id := Empty;
14281
14282 begin
14283 if Ekind (Proc_Nam) = E_Procedure
14284 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
14285 then
14286 Param :=
14287 Parameter_Type
14288 (First (Parameter_Specifications (Parent (Proc_Nam))));
14289
14290 -- The formal may be an anonymous access type
14291
14292 if Nkind (Param) = N_Access_Definition then
14293 Param_Typ := Entity (Subtype_Mark (Param));
14294 else
14295 Param_Typ := Etype (Param);
14296 end if;
14297
14298 -- In the case where an Itype was created for a dispatchin call, the
14299 -- procedure call has been rewritten. The actual may be an access to
14300 -- interface type in which case it is the designated type that is the
14301 -- controlling type.
14302
14303 elsif Present (Associated_Node_For_Itype (Proc_Nam))
14304 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
14305 and then
14306 Present (Parameter_Associations
14307 (Associated_Node_For_Itype (Proc_Nam)))
14308 then
14309 Param_Typ :=
14310 Etype (First (Parameter_Associations
14311 (Associated_Node_For_Itype (Proc_Nam))));
14312
14313 if Ekind (Param_Typ) = E_Anonymous_Access_Type then
14314 Param_Typ := Directly_Designated_Type (Param_Typ);
14315 end if;
14316 end if;
14317
14318 if Present (Param_Typ) then
14319 return
14320 Is_Interface (Param_Typ)
14321 and then Is_Limited_Record (Param_Typ);
14322 end if;
14323
14324 return False;
14325 end Is_Controlling_Limited_Procedure;
14326
14327 -----------------------------
14328 -- Is_CPP_Constructor_Call --
14329 -----------------------------
14330
14331 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
14332 begin
14333 return Nkind (N) = N_Function_Call
14334 and then Is_CPP_Class (Etype (Etype (N)))
14335 and then Is_Constructor (Entity (Name (N)))
14336 and then Is_Imported (Entity (Name (N)));
14337 end Is_CPP_Constructor_Call;
14338
14339 -------------------------
14340 -- Is_Current_Instance --
14341 -------------------------
14342
14343 function Is_Current_Instance (N : Node_Id) return Boolean is
14344 Typ : constant Entity_Id := Entity (N);
14345 P : Node_Id;
14346
14347 begin
14348 -- Simplest case: entity is a concurrent type and we are currently
14349 -- inside the body. This will eventually be expanded into a call to
14350 -- Self (for tasks) or _object (for protected objects).
14351
14352 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
14353 return True;
14354
14355 else
14356 -- Check whether the context is a (sub)type declaration for the
14357 -- type entity.
14358
14359 P := Parent (N);
14360 while Present (P) loop
14361 if Nkind_In (P, N_Full_Type_Declaration,
14362 N_Private_Type_Declaration,
14363 N_Subtype_Declaration)
14364 and then Comes_From_Source (P)
14365 and then Defining_Entity (P) = Typ
14366 then
14367 return True;
14368
14369 -- A subtype name may appear in an aspect specification for a
14370 -- Predicate_Failure aspect, for which we do not construct a
14371 -- wrapper procedure. The subtype will be replaced by the
14372 -- expression being tested when the corresponding predicate
14373 -- check is expanded.
14374
14375 elsif Nkind (P) = N_Aspect_Specification
14376 and then Nkind (Parent (P)) = N_Subtype_Declaration
14377 then
14378 return True;
14379
14380 elsif Nkind (P) = N_Pragma
14381 and then Get_Pragma_Id (P) = Pragma_Predicate_Failure
14382 then
14383 return True;
14384 end if;
14385
14386 P := Parent (P);
14387 end loop;
14388 end if;
14389
14390 -- In any other context this is not a current occurrence
14391
14392 return False;
14393 end Is_Current_Instance;
14394
14395 --------------------
14396 -- Is_Declaration --
14397 --------------------
14398
14399 function Is_Declaration
14400 (N : Node_Id;
14401 Body_OK : Boolean := True;
14402 Concurrent_OK : Boolean := True;
14403 Formal_OK : Boolean := True;
14404 Generic_OK : Boolean := True;
14405 Instantiation_OK : Boolean := True;
14406 Renaming_OK : Boolean := True;
14407 Stub_OK : Boolean := True;
14408 Subprogram_OK : Boolean := True;
14409 Type_OK : Boolean := True) return Boolean
14410 is
14411 begin
14412 case Nkind (N) is
14413
14414 -- Body declarations
14415
14416 when N_Proper_Body =>
14417 return Body_OK;
14418
14419 -- Concurrent type declarations
14420
14421 when N_Protected_Type_Declaration
14422 | N_Single_Protected_Declaration
14423 | N_Single_Task_Declaration
14424 | N_Task_Type_Declaration
14425 =>
14426 return Concurrent_OK or Type_OK;
14427
14428 -- Formal declarations
14429
14430 when N_Formal_Abstract_Subprogram_Declaration
14431 | N_Formal_Concrete_Subprogram_Declaration
14432 | N_Formal_Object_Declaration
14433 | N_Formal_Package_Declaration
14434 | N_Formal_Type_Declaration
14435 =>
14436 return Formal_OK;
14437
14438 -- Generic declarations
14439
14440 when N_Generic_Package_Declaration
14441 | N_Generic_Subprogram_Declaration
14442 =>
14443 return Generic_OK;
14444
14445 -- Generic instantiations
14446
14447 when N_Function_Instantiation
14448 | N_Package_Instantiation
14449 | N_Procedure_Instantiation
14450 =>
14451 return Instantiation_OK;
14452
14453 -- Generic renaming declarations
14454
14455 when N_Generic_Renaming_Declaration =>
14456 return Generic_OK or Renaming_OK;
14457
14458 -- Renaming declarations
14459
14460 when N_Exception_Renaming_Declaration
14461 | N_Object_Renaming_Declaration
14462 | N_Package_Renaming_Declaration
14463 | N_Subprogram_Renaming_Declaration
14464 =>
14465 return Renaming_OK;
14466
14467 -- Stub declarations
14468
14469 when N_Body_Stub =>
14470 return Stub_OK;
14471
14472 -- Subprogram declarations
14473
14474 when N_Abstract_Subprogram_Declaration
14475 | N_Entry_Declaration
14476 | N_Expression_Function
14477 | N_Subprogram_Declaration
14478 =>
14479 return Subprogram_OK;
14480
14481 -- Type declarations
14482
14483 when N_Full_Type_Declaration
14484 | N_Incomplete_Type_Declaration
14485 | N_Private_Extension_Declaration
14486 | N_Private_Type_Declaration
14487 | N_Subtype_Declaration
14488 =>
14489 return Type_OK;
14490
14491 -- Miscellaneous
14492
14493 when N_Component_Declaration
14494 | N_Exception_Declaration
14495 | N_Implicit_Label_Declaration
14496 | N_Number_Declaration
14497 | N_Object_Declaration
14498 | N_Package_Declaration
14499 =>
14500 return True;
14501
14502 when others =>
14503 return False;
14504 end case;
14505 end Is_Declaration;
14506
14507 --------------------------------
14508 -- Is_Declared_Within_Variant --
14509 --------------------------------
14510
14511 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
14512 Comp_Decl : constant Node_Id := Parent (Comp);
14513 Comp_List : constant Node_Id := Parent (Comp_Decl);
14514 begin
14515 return Nkind (Parent (Comp_List)) = N_Variant;
14516 end Is_Declared_Within_Variant;
14517
14518 ----------------------------------------------
14519 -- Is_Dependent_Component_Of_Mutable_Object --
14520 ----------------------------------------------
14521
14522 function Is_Dependent_Component_Of_Mutable_Object
14523 (Object : Node_Id) return Boolean
14524 is
14525 P : Node_Id;
14526 Prefix_Type : Entity_Id;
14527 P_Aliased : Boolean := False;
14528 Comp : Entity_Id;
14529
14530 Deref : Node_Id := Object;
14531 -- Dereference node, in something like X.all.Y(2)
14532
14533 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
14534
14535 begin
14536 -- Find the dereference node if any
14537
14538 while Nkind_In (Deref, N_Indexed_Component,
14539 N_Selected_Component,
14540 N_Slice)
14541 loop
14542 Deref := Prefix (Deref);
14543 end loop;
14544
14545 -- If the prefix is a qualified expression of a variable, then function
14546 -- Is_Variable will return False for that because a qualified expression
14547 -- denotes a constant view, so we need to get the name being qualified
14548 -- so we can test below whether that's a variable (or a dereference).
14549
14550 if Nkind (Deref) = N_Qualified_Expression then
14551 Deref := Expression (Deref);
14552 end if;
14553
14554 -- Ada 2005: If we have a component or slice of a dereference, something
14555 -- like X.all.Y (2) and the type of X is access-to-constant, Is_Variable
14556 -- will return False, because it is indeed a constant view. But it might
14557 -- be a view of a variable object, so we want the following condition to
14558 -- be True in that case.
14559
14560 if Is_Variable (Object)
14561 or else Is_Variable (Deref)
14562 or else (Ada_Version >= Ada_2005
14563 and then (Nkind (Deref) = N_Explicit_Dereference
14564 or else Is_Access_Type (Etype (Deref))))
14565 then
14566 if Nkind (Object) = N_Selected_Component then
14567
14568 -- If the selector is not a component, then we definitely return
14569 -- False (it could be a function selector in a prefix form call
14570 -- occurring in an iterator specification).
14571
14572 if not Ekind_In (Entity (Selector_Name (Object)), E_Component,
14573 E_Discriminant)
14574 then
14575 return False;
14576 end if;
14577
14578 -- Get the original node of the prefix in case it has been
14579 -- rewritten, which can occur, for example, in qualified
14580 -- expression cases. Also, a discriminant check on a selected
14581 -- component may be expanded into a dereference when removing
14582 -- side effects, and the subtype of the original node may be
14583 -- unconstrained.
14584
14585 P := Original_Node (Prefix (Object));
14586 Prefix_Type := Etype (P);
14587
14588 -- If the prefix is a qualified expression, we want to look at its
14589 -- operand.
14590
14591 if Nkind (P) = N_Qualified_Expression then
14592 P := Expression (P);
14593 Prefix_Type := Etype (P);
14594 end if;
14595
14596 if Is_Entity_Name (P) then
14597 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
14598 Prefix_Type := Base_Type (Prefix_Type);
14599 end if;
14600
14601 if Is_Aliased (Entity (P)) then
14602 P_Aliased := True;
14603 end if;
14604
14605 -- For explicit dereferences we get the access prefix so we can
14606 -- treat this similarly to implicit dereferences and examine the
14607 -- kind of the access type and its designated subtype further
14608 -- below.
14609
14610 elsif Nkind (P) = N_Explicit_Dereference then
14611 P := Prefix (P);
14612 Prefix_Type := Etype (P);
14613
14614 else
14615 -- Check for prefix being an aliased component???
14616
14617 null;
14618 end if;
14619
14620 -- A heap object is constrained by its initial value
14621
14622 -- Ada 2005 (AI-363): Always assume the object could be mutable in
14623 -- the dereferenced case, since the access value might denote an
14624 -- unconstrained aliased object, whereas in Ada 95 the designated
14625 -- object is guaranteed to be constrained. A worst-case assumption
14626 -- has to apply in Ada 2005 because we can't tell at compile
14627 -- time whether the object is "constrained by its initial value",
14628 -- despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
14629 -- rules (these rules are acknowledged to need fixing). We don't
14630 -- impose this more stringent checking for earlier Ada versions or
14631 -- when Relaxed_RM_Semantics applies (the latter for CodePeer's
14632 -- benefit, though it's unclear on why using -gnat95 would not be
14633 -- sufficient???).
14634
14635 if Ada_Version < Ada_2005 or else Relaxed_RM_Semantics then
14636 if Is_Access_Type (Prefix_Type)
14637 or else Nkind (P) = N_Explicit_Dereference
14638 then
14639 return False;
14640 end if;
14641
14642 else pragma Assert (Ada_Version >= Ada_2005);
14643 if Is_Access_Type (Prefix_Type) then
14644 -- We need to make sure we have the base subtype, in case
14645 -- this is actually an access subtype (whose Ekind will be
14646 -- E_Access_Subtype).
14647
14648 Prefix_Type := Etype (Prefix_Type);
14649
14650 -- If the access type is pool-specific, and there is no
14651 -- constrained partial view of the designated type, then the
14652 -- designated object is known to be constrained. If it's a
14653 -- formal access type and the renaming is in the generic
14654 -- spec, we also treat it as pool-specific (known to be
14655 -- constrained), but assume the worst if in the generic body
14656 -- (see RM 3.3(23.3/3)).
14657
14658 if Ekind (Prefix_Type) = E_Access_Type
14659 and then (not Is_Generic_Type (Prefix_Type)
14660 or else not In_Generic_Body (Current_Scope))
14661 and then not Object_Type_Has_Constrained_Partial_View
14662 (Typ => Designated_Type (Prefix_Type),
14663 Scop => Current_Scope)
14664 then
14665 return False;
14666
14667 -- Otherwise (general access type, or there is a constrained
14668 -- partial view of the designated type), we need to check
14669 -- based on the designated type.
14670
14671 else
14672 Prefix_Type := Designated_Type (Prefix_Type);
14673 end if;
14674 end if;
14675 end if;
14676
14677 Comp :=
14678 Original_Record_Component (Entity (Selector_Name (Object)));
14679
14680 -- As per AI-0017, the renaming is illegal in a generic body, even
14681 -- if the subtype is indefinite (only applies to prefixes of an
14682 -- untagged formal type, see RM 3.3 (23.11/3)).
14683
14684 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
14685
14686 if not Is_Constrained (Prefix_Type)
14687 and then (Is_Definite_Subtype (Prefix_Type)
14688 or else
14689 (not Is_Tagged_Type (Prefix_Type)
14690 and then Is_Generic_Type (Prefix_Type)
14691 and then In_Generic_Body (Current_Scope)))
14692
14693 and then (Is_Declared_Within_Variant (Comp)
14694 or else Has_Discriminant_Dependent_Constraint (Comp))
14695 and then (not P_Aliased or else Ada_Version >= Ada_2005)
14696 then
14697 return True;
14698
14699 -- If the prefix is of an access type at this point, then we want
14700 -- to return False, rather than calling this function recursively
14701 -- on the access object (which itself might be a discriminant-
14702 -- dependent component of some other object, but that isn't
14703 -- relevant to checking the object passed to us). This avoids
14704 -- issuing wrong errors when compiling with -gnatc, where there
14705 -- can be implicit dereferences that have not been expanded.
14706
14707 elsif Is_Access_Type (Etype (Prefix (Object))) then
14708 return False;
14709
14710 else
14711 return
14712 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
14713 end if;
14714
14715 elsif Nkind (Object) = N_Indexed_Component
14716 or else Nkind (Object) = N_Slice
14717 then
14718 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
14719
14720 -- A type conversion that Is_Variable is a view conversion:
14721 -- go back to the denoted object.
14722
14723 elsif Nkind (Object) = N_Type_Conversion then
14724 return
14725 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
14726 end if;
14727 end if;
14728
14729 return False;
14730 end Is_Dependent_Component_Of_Mutable_Object;
14731
14732 ---------------------
14733 -- Is_Dereferenced --
14734 ---------------------
14735
14736 function Is_Dereferenced (N : Node_Id) return Boolean is
14737 P : constant Node_Id := Parent (N);
14738 begin
14739 return Nkind_In (P, N_Selected_Component,
14740 N_Explicit_Dereference,
14741 N_Indexed_Component,
14742 N_Slice)
14743 and then Prefix (P) = N;
14744 end Is_Dereferenced;
14745
14746 ----------------------
14747 -- Is_Descendant_Of --
14748 ----------------------
14749
14750 function Is_Descendant_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
14751 T : Entity_Id;
14752 Etyp : Entity_Id;
14753
14754 begin
14755 pragma Assert (Nkind (T1) in N_Entity);
14756 pragma Assert (Nkind (T2) in N_Entity);
14757
14758 T := Base_Type (T1);
14759
14760 -- Immediate return if the types match
14761
14762 if T = T2 then
14763 return True;
14764
14765 -- Comment needed here ???
14766
14767 elsif Ekind (T) = E_Class_Wide_Type then
14768 return Etype (T) = T2;
14769
14770 -- All other cases
14771
14772 else
14773 loop
14774 Etyp := Etype (T);
14775
14776 -- Done if we found the type we are looking for
14777
14778 if Etyp = T2 then
14779 return True;
14780
14781 -- Done if no more derivations to check
14782
14783 elsif T = T1
14784 or else T = Etyp
14785 then
14786 return False;
14787
14788 -- Following test catches error cases resulting from prev errors
14789
14790 elsif No (Etyp) then
14791 return False;
14792
14793 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
14794 return False;
14795
14796 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
14797 return False;
14798 end if;
14799
14800 T := Base_Type (Etyp);
14801 end loop;
14802 end if;
14803 end Is_Descendant_Of;
14804
14805 ----------------------------------------
14806 -- Is_Descendant_Of_Suspension_Object --
14807 ----------------------------------------
14808
14809 function Is_Descendant_Of_Suspension_Object
14810 (Typ : Entity_Id) return Boolean
14811 is
14812 Cur_Typ : Entity_Id;
14813 Par_Typ : Entity_Id;
14814
14815 begin
14816 -- Climb the type derivation chain checking each parent type against
14817 -- Suspension_Object.
14818
14819 Cur_Typ := Base_Type (Typ);
14820 while Present (Cur_Typ) loop
14821 Par_Typ := Etype (Cur_Typ);
14822
14823 -- The current type is a match
14824
14825 if Is_Suspension_Object (Cur_Typ) then
14826 return True;
14827
14828 -- Stop the traversal once the root of the derivation chain has been
14829 -- reached. In that case the current type is its own base type.
14830
14831 elsif Cur_Typ = Par_Typ then
14832 exit;
14833 end if;
14834
14835 Cur_Typ := Base_Type (Par_Typ);
14836 end loop;
14837
14838 return False;
14839 end Is_Descendant_Of_Suspension_Object;
14840
14841 ---------------------------------------------
14842 -- Is_Double_Precision_Floating_Point_Type --
14843 ---------------------------------------------
14844
14845 function Is_Double_Precision_Floating_Point_Type
14846 (E : Entity_Id) return Boolean is
14847 begin
14848 return Is_Floating_Point_Type (E)
14849 and then Machine_Radix_Value (E) = Uint_2
14850 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
14851 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
14852 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
14853 end Is_Double_Precision_Floating_Point_Type;
14854
14855 -----------------------------
14856 -- Is_Effectively_Volatile --
14857 -----------------------------
14858
14859 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
14860 begin
14861 if Is_Type (Id) then
14862
14863 -- An arbitrary type is effectively volatile when it is subject to
14864 -- pragma Atomic or Volatile.
14865
14866 if Is_Volatile (Id) then
14867 return True;
14868
14869 -- An array type is effectively volatile when it is subject to pragma
14870 -- Atomic_Components or Volatile_Components or its component type is
14871 -- effectively volatile.
14872
14873 elsif Is_Array_Type (Id) then
14874 declare
14875 Anc : Entity_Id := Base_Type (Id);
14876 begin
14877 if Is_Private_Type (Anc) then
14878 Anc := Full_View (Anc);
14879 end if;
14880
14881 -- Test for presence of ancestor, as the full view of a private
14882 -- type may be missing in case of error.
14883
14884 return
14885 Has_Volatile_Components (Id)
14886 or else
14887 (Present (Anc)
14888 and then Is_Effectively_Volatile (Component_Type (Anc)));
14889 end;
14890
14891 -- A protected type is always volatile
14892
14893 elsif Is_Protected_Type (Id) then
14894 return True;
14895
14896 -- A descendant of Ada.Synchronous_Task_Control.Suspension_Object is
14897 -- automatically volatile.
14898
14899 elsif Is_Descendant_Of_Suspension_Object (Id) then
14900 return True;
14901
14902 -- Otherwise the type is not effectively volatile
14903
14904 else
14905 return False;
14906 end if;
14907
14908 -- Otherwise Id denotes an object
14909
14910 else
14911 -- A volatile object for which No_Caching is enabled is not
14912 -- effectively volatile.
14913
14914 return
14915 (Is_Volatile (Id) and then not No_Caching_Enabled (Id))
14916 or else Has_Volatile_Components (Id)
14917 or else Is_Effectively_Volatile (Etype (Id));
14918 end if;
14919 end Is_Effectively_Volatile;
14920
14921 ------------------------------------
14922 -- Is_Effectively_Volatile_Object --
14923 ------------------------------------
14924
14925 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
14926 begin
14927 if Is_Entity_Name (N) then
14928 return Is_Effectively_Volatile (Entity (N));
14929
14930 elsif Nkind (N) = N_Indexed_Component then
14931 return Is_Effectively_Volatile_Object (Prefix (N));
14932
14933 elsif Nkind (N) = N_Selected_Component then
14934 return
14935 Is_Effectively_Volatile_Object (Prefix (N))
14936 or else
14937 Is_Effectively_Volatile_Object (Selector_Name (N));
14938
14939 else
14940 return False;
14941 end if;
14942 end Is_Effectively_Volatile_Object;
14943
14944 -------------------
14945 -- Is_Entry_Body --
14946 -------------------
14947
14948 function Is_Entry_Body (Id : Entity_Id) return Boolean is
14949 begin
14950 return
14951 Ekind_In (Id, E_Entry, E_Entry_Family)
14952 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Body;
14953 end Is_Entry_Body;
14954
14955 --------------------------
14956 -- Is_Entry_Declaration --
14957 --------------------------
14958
14959 function Is_Entry_Declaration (Id : Entity_Id) return Boolean is
14960 begin
14961 return
14962 Ekind_In (Id, E_Entry, E_Entry_Family)
14963 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Declaration;
14964 end Is_Entry_Declaration;
14965
14966 ------------------------------------
14967 -- Is_Expanded_Priority_Attribute --
14968 ------------------------------------
14969
14970 function Is_Expanded_Priority_Attribute (E : Entity_Id) return Boolean is
14971 begin
14972 return
14973 Nkind (E) = N_Function_Call
14974 and then not Configurable_Run_Time_Mode
14975 and then Nkind (Original_Node (E)) = N_Attribute_Reference
14976 and then (Entity (Name (E)) = RTE (RE_Get_Ceiling)
14977 or else Entity (Name (E)) = RTE (RO_PE_Get_Ceiling));
14978 end Is_Expanded_Priority_Attribute;
14979
14980 ----------------------------
14981 -- Is_Expression_Function --
14982 ----------------------------
14983
14984 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
14985 begin
14986 if Ekind_In (Subp, E_Function, E_Subprogram_Body) then
14987 return
14988 Nkind (Original_Node (Unit_Declaration_Node (Subp))) =
14989 N_Expression_Function;
14990 else
14991 return False;
14992 end if;
14993 end Is_Expression_Function;
14994
14995 ------------------------------------------
14996 -- Is_Expression_Function_Or_Completion --
14997 ------------------------------------------
14998
14999 function Is_Expression_Function_Or_Completion
15000 (Subp : Entity_Id) return Boolean
15001 is
15002 Subp_Decl : Node_Id;
15003
15004 begin
15005 if Ekind (Subp) = E_Function then
15006 Subp_Decl := Unit_Declaration_Node (Subp);
15007
15008 -- The function declaration is either an expression function or is
15009 -- completed by an expression function body.
15010
15011 return
15012 Is_Expression_Function (Subp)
15013 or else (Nkind (Subp_Decl) = N_Subprogram_Declaration
15014 and then Present (Corresponding_Body (Subp_Decl))
15015 and then Is_Expression_Function
15016 (Corresponding_Body (Subp_Decl)));
15017
15018 elsif Ekind (Subp) = E_Subprogram_Body then
15019 return Is_Expression_Function (Subp);
15020
15021 else
15022 return False;
15023 end if;
15024 end Is_Expression_Function_Or_Completion;
15025
15026 -----------------------
15027 -- Is_EVF_Expression --
15028 -----------------------
15029
15030 function Is_EVF_Expression (N : Node_Id) return Boolean is
15031 Orig_N : constant Node_Id := Original_Node (N);
15032 Alt : Node_Id;
15033 Expr : Node_Id;
15034 Id : Entity_Id;
15035
15036 begin
15037 -- Detect a reference to a formal parameter of a specific tagged type
15038 -- whose related subprogram is subject to pragma Expresions_Visible with
15039 -- value "False".
15040
15041 if Is_Entity_Name (N) and then Present (Entity (N)) then
15042 Id := Entity (N);
15043
15044 return
15045 Is_Formal (Id)
15046 and then Is_Specific_Tagged_Type (Etype (Id))
15047 and then Extensions_Visible_Status (Id) =
15048 Extensions_Visible_False;
15049
15050 -- A case expression is an EVF expression when it contains at least one
15051 -- EVF dependent_expression. Note that a case expression may have been
15052 -- expanded, hence the use of Original_Node.
15053
15054 elsif Nkind (Orig_N) = N_Case_Expression then
15055 Alt := First (Alternatives (Orig_N));
15056 while Present (Alt) loop
15057 if Is_EVF_Expression (Expression (Alt)) then
15058 return True;
15059 end if;
15060
15061 Next (Alt);
15062 end loop;
15063
15064 -- An if expression is an EVF expression when it contains at least one
15065 -- EVF dependent_expression. Note that an if expression may have been
15066 -- expanded, hence the use of Original_Node.
15067
15068 elsif Nkind (Orig_N) = N_If_Expression then
15069 Expr := Next (First (Expressions (Orig_N)));
15070 while Present (Expr) loop
15071 if Is_EVF_Expression (Expr) then
15072 return True;
15073 end if;
15074
15075 Next (Expr);
15076 end loop;
15077
15078 -- A qualified expression or a type conversion is an EVF expression when
15079 -- its operand is an EVF expression.
15080
15081 elsif Nkind_In (N, N_Qualified_Expression,
15082 N_Unchecked_Type_Conversion,
15083 N_Type_Conversion)
15084 then
15085 return Is_EVF_Expression (Expression (N));
15086
15087 -- Attributes 'Loop_Entry, 'Old, and 'Update are EVF expressions when
15088 -- their prefix denotes an EVF expression.
15089
15090 elsif Nkind (N) = N_Attribute_Reference
15091 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
15092 Name_Old,
15093 Name_Update)
15094 then
15095 return Is_EVF_Expression (Prefix (N));
15096 end if;
15097
15098 return False;
15099 end Is_EVF_Expression;
15100
15101 --------------
15102 -- Is_False --
15103 --------------
15104
15105 function Is_False (U : Uint) return Boolean is
15106 begin
15107 return (U = 0);
15108 end Is_False;
15109
15110 ---------------------------
15111 -- Is_Fixed_Model_Number --
15112 ---------------------------
15113
15114 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
15115 S : constant Ureal := Small_Value (T);
15116 M : Urealp.Save_Mark;
15117 R : Boolean;
15118
15119 begin
15120 M := Urealp.Mark;
15121 R := (U = UR_Trunc (U / S) * S);
15122 Urealp.Release (M);
15123 return R;
15124 end Is_Fixed_Model_Number;
15125
15126 -------------------------------
15127 -- Is_Fully_Initialized_Type --
15128 -------------------------------
15129
15130 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
15131 begin
15132 -- Scalar types
15133
15134 if Is_Scalar_Type (Typ) then
15135
15136 -- A scalar type with an aspect Default_Value is fully initialized
15137
15138 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
15139 -- of a scalar type, but we don't take that into account here, since
15140 -- we don't want these to affect warnings.
15141
15142 return Has_Default_Aspect (Typ);
15143
15144 elsif Is_Access_Type (Typ) then
15145 return True;
15146
15147 elsif Is_Array_Type (Typ) then
15148 if Is_Fully_Initialized_Type (Component_Type (Typ))
15149 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
15150 then
15151 return True;
15152 end if;
15153
15154 -- An interesting case, if we have a constrained type one of whose
15155 -- bounds is known to be null, then there are no elements to be
15156 -- initialized, so all the elements are initialized.
15157
15158 if Is_Constrained (Typ) then
15159 declare
15160 Indx : Node_Id;
15161 Indx_Typ : Entity_Id;
15162 Lbd, Hbd : Node_Id;
15163
15164 begin
15165 Indx := First_Index (Typ);
15166 while Present (Indx) loop
15167 if Etype (Indx) = Any_Type then
15168 return False;
15169
15170 -- If index is a range, use directly
15171
15172 elsif Nkind (Indx) = N_Range then
15173 Lbd := Low_Bound (Indx);
15174 Hbd := High_Bound (Indx);
15175
15176 else
15177 Indx_Typ := Etype (Indx);
15178
15179 if Is_Private_Type (Indx_Typ) then
15180 Indx_Typ := Full_View (Indx_Typ);
15181 end if;
15182
15183 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
15184 return False;
15185 else
15186 Lbd := Type_Low_Bound (Indx_Typ);
15187 Hbd := Type_High_Bound (Indx_Typ);
15188 end if;
15189 end if;
15190
15191 if Compile_Time_Known_Value (Lbd)
15192 and then
15193 Compile_Time_Known_Value (Hbd)
15194 then
15195 if Expr_Value (Hbd) < Expr_Value (Lbd) then
15196 return True;
15197 end if;
15198 end if;
15199
15200 Next_Index (Indx);
15201 end loop;
15202 end;
15203 end if;
15204
15205 -- If no null indexes, then type is not fully initialized
15206
15207 return False;
15208
15209 -- Record types
15210
15211 elsif Is_Record_Type (Typ) then
15212 if Has_Discriminants (Typ)
15213 and then
15214 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
15215 and then Is_Fully_Initialized_Variant (Typ)
15216 then
15217 return True;
15218 end if;
15219
15220 -- We consider bounded string types to be fully initialized, because
15221 -- otherwise we get false alarms when the Data component is not
15222 -- default-initialized.
15223
15224 if Is_Bounded_String (Typ) then
15225 return True;
15226 end if;
15227
15228 -- Controlled records are considered to be fully initialized if
15229 -- there is a user defined Initialize routine. This may not be
15230 -- entirely correct, but as the spec notes, we are guessing here
15231 -- what is best from the point of view of issuing warnings.
15232
15233 if Is_Controlled (Typ) then
15234 declare
15235 Utyp : constant Entity_Id := Underlying_Type (Typ);
15236
15237 begin
15238 if Present (Utyp) then
15239 declare
15240 Init : constant Entity_Id :=
15241 (Find_Optional_Prim_Op
15242 (Underlying_Type (Typ), Name_Initialize));
15243
15244 begin
15245 if Present (Init)
15246 and then Comes_From_Source (Init)
15247 and then not In_Predefined_Unit (Init)
15248 then
15249 return True;
15250
15251 elsif Has_Null_Extension (Typ)
15252 and then
15253 Is_Fully_Initialized_Type
15254 (Etype (Base_Type (Typ)))
15255 then
15256 return True;
15257 end if;
15258 end;
15259 end if;
15260 end;
15261 end if;
15262
15263 -- Otherwise see if all record components are initialized
15264
15265 declare
15266 Ent : Entity_Id;
15267
15268 begin
15269 Ent := First_Entity (Typ);
15270 while Present (Ent) loop
15271 if Ekind (Ent) = E_Component
15272 and then (No (Parent (Ent))
15273 or else No (Expression (Parent (Ent))))
15274 and then not Is_Fully_Initialized_Type (Etype (Ent))
15275
15276 -- Special VM case for tag components, which need to be
15277 -- defined in this case, but are never initialized as VMs
15278 -- are using other dispatching mechanisms. Ignore this
15279 -- uninitialized case. Note that this applies both to the
15280 -- uTag entry and the main vtable pointer (CPP_Class case).
15281
15282 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
15283 then
15284 return False;
15285 end if;
15286
15287 Next_Entity (Ent);
15288 end loop;
15289 end;
15290
15291 -- No uninitialized components, so type is fully initialized.
15292 -- Note that this catches the case of no components as well.
15293
15294 return True;
15295
15296 elsif Is_Concurrent_Type (Typ) then
15297 return True;
15298
15299 elsif Is_Private_Type (Typ) then
15300 declare
15301 U : constant Entity_Id := Underlying_Type (Typ);
15302
15303 begin
15304 if No (U) then
15305 return False;
15306 else
15307 return Is_Fully_Initialized_Type (U);
15308 end if;
15309 end;
15310
15311 else
15312 return False;
15313 end if;
15314 end Is_Fully_Initialized_Type;
15315
15316 ----------------------------------
15317 -- Is_Fully_Initialized_Variant --
15318 ----------------------------------
15319
15320 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
15321 Loc : constant Source_Ptr := Sloc (Typ);
15322 Constraints : constant List_Id := New_List;
15323 Components : constant Elist_Id := New_Elmt_List;
15324 Comp_Elmt : Elmt_Id;
15325 Comp_Id : Node_Id;
15326 Comp_List : Node_Id;
15327 Discr : Entity_Id;
15328 Discr_Val : Node_Id;
15329
15330 Report_Errors : Boolean;
15331 pragma Warnings (Off, Report_Errors);
15332
15333 begin
15334 if Serious_Errors_Detected > 0 then
15335 return False;
15336 end if;
15337
15338 if Is_Record_Type (Typ)
15339 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15340 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
15341 then
15342 Comp_List := Component_List (Type_Definition (Parent (Typ)));
15343
15344 Discr := First_Discriminant (Typ);
15345 while Present (Discr) loop
15346 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
15347 Discr_Val := Expression (Parent (Discr));
15348
15349 if Present (Discr_Val)
15350 and then Is_OK_Static_Expression (Discr_Val)
15351 then
15352 Append_To (Constraints,
15353 Make_Component_Association (Loc,
15354 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
15355 Expression => New_Copy (Discr_Val)));
15356 else
15357 return False;
15358 end if;
15359 else
15360 return False;
15361 end if;
15362
15363 Next_Discriminant (Discr);
15364 end loop;
15365
15366 Gather_Components
15367 (Typ => Typ,
15368 Comp_List => Comp_List,
15369 Governed_By => Constraints,
15370 Into => Components,
15371 Report_Errors => Report_Errors);
15372
15373 -- Check that each component present is fully initialized
15374
15375 Comp_Elmt := First_Elmt (Components);
15376 while Present (Comp_Elmt) loop
15377 Comp_Id := Node (Comp_Elmt);
15378
15379 if Ekind (Comp_Id) = E_Component
15380 and then (No (Parent (Comp_Id))
15381 or else No (Expression (Parent (Comp_Id))))
15382 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
15383 then
15384 return False;
15385 end if;
15386
15387 Next_Elmt (Comp_Elmt);
15388 end loop;
15389
15390 return True;
15391
15392 elsif Is_Private_Type (Typ) then
15393 declare
15394 U : constant Entity_Id := Underlying_Type (Typ);
15395
15396 begin
15397 if No (U) then
15398 return False;
15399 else
15400 return Is_Fully_Initialized_Variant (U);
15401 end if;
15402 end;
15403
15404 else
15405 return False;
15406 end if;
15407 end Is_Fully_Initialized_Variant;
15408
15409 ------------------------------------
15410 -- Is_Generic_Declaration_Or_Body --
15411 ------------------------------------
15412
15413 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
15414 Spec_Decl : Node_Id;
15415
15416 begin
15417 -- Package/subprogram body
15418
15419 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
15420 and then Present (Corresponding_Spec (Decl))
15421 then
15422 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
15423
15424 -- Package/subprogram body stub
15425
15426 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
15427 and then Present (Corresponding_Spec_Of_Stub (Decl))
15428 then
15429 Spec_Decl :=
15430 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
15431
15432 -- All other cases
15433
15434 else
15435 Spec_Decl := Decl;
15436 end if;
15437
15438 -- Rather than inspecting the defining entity of the spec declaration,
15439 -- look at its Nkind. This takes care of the case where the analysis of
15440 -- a generic body modifies the Ekind of its spec to allow for recursive
15441 -- calls.
15442
15443 return
15444 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
15445 N_Generic_Subprogram_Declaration);
15446 end Is_Generic_Declaration_Or_Body;
15447
15448 ---------------------------
15449 -- Is_Independent_Object --
15450 ---------------------------
15451
15452 function Is_Independent_Object (N : Node_Id) return Boolean is
15453 function Is_Independent_Object_Entity (Id : Entity_Id) return Boolean;
15454 -- Determine whether arbitrary entity Id denotes an object that is
15455 -- Independent.
15456
15457 function Prefix_Has_Independent_Components (P : Node_Id) return Boolean;
15458 -- Determine whether prefix P has independent components. This requires
15459 -- the presence of an Independent_Components aspect/pragma.
15460
15461 ------------------------------------
15462 -- Is_Independent_Object_Entity --
15463 ------------------------------------
15464
15465 function Is_Independent_Object_Entity (Id : Entity_Id) return Boolean is
15466 begin
15467 return
15468 Is_Object (Id)
15469 and then (Is_Independent (Id)
15470 or else
15471 Is_Independent (Etype (Id)));
15472 end Is_Independent_Object_Entity;
15473
15474 -------------------------------------
15475 -- Prefix_Has_Independent_Components --
15476 -------------------------------------
15477
15478 function Prefix_Has_Independent_Components (P : Node_Id) return Boolean
15479 is
15480 Typ : constant Entity_Id := Etype (P);
15481
15482 begin
15483 if Is_Access_Type (Typ) then
15484 return Has_Independent_Components (Designated_Type (Typ));
15485
15486 elsif Has_Independent_Components (Typ) then
15487 return True;
15488
15489 elsif Is_Entity_Name (P)
15490 and then Has_Independent_Components (Entity (P))
15491 then
15492 return True;
15493
15494 else
15495 return False;
15496 end if;
15497 end Prefix_Has_Independent_Components;
15498
15499 -- Start of processing for Is_Independent_Object
15500
15501 begin
15502 if Is_Entity_Name (N) then
15503 return Is_Independent_Object_Entity (Entity (N));
15504
15505 elsif Is_Independent (Etype (N)) then
15506 return True;
15507
15508 elsif Nkind (N) = N_Indexed_Component then
15509 return Prefix_Has_Independent_Components (Prefix (N));
15510
15511 elsif Nkind (N) = N_Selected_Component then
15512 return Prefix_Has_Independent_Components (Prefix (N))
15513 or else Is_Independent (Entity (Selector_Name (N)));
15514
15515 else
15516 return False;
15517 end if;
15518 end Is_Independent_Object;
15519
15520 ----------------------------
15521 -- Is_Inherited_Operation --
15522 ----------------------------
15523
15524 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
15525 pragma Assert (Is_Overloadable (E));
15526 Kind : constant Node_Kind := Nkind (Parent (E));
15527 begin
15528 return Kind = N_Full_Type_Declaration
15529 or else Kind = N_Private_Extension_Declaration
15530 or else Kind = N_Subtype_Declaration
15531 or else (Ekind (E) = E_Enumeration_Literal
15532 and then Is_Derived_Type (Etype (E)));
15533 end Is_Inherited_Operation;
15534
15535 -------------------------------------
15536 -- Is_Inherited_Operation_For_Type --
15537 -------------------------------------
15538
15539 function Is_Inherited_Operation_For_Type
15540 (E : Entity_Id;
15541 Typ : Entity_Id) return Boolean
15542 is
15543 begin
15544 -- Check that the operation has been created by the type declaration
15545
15546 return Is_Inherited_Operation (E)
15547 and then Defining_Identifier (Parent (E)) = Typ;
15548 end Is_Inherited_Operation_For_Type;
15549
15550 --------------------------------------
15551 -- Is_Inlinable_Expression_Function --
15552 --------------------------------------
15553
15554 function Is_Inlinable_Expression_Function
15555 (Subp : Entity_Id) return Boolean
15556 is
15557 Return_Expr : Node_Id;
15558
15559 begin
15560 if Is_Expression_Function_Or_Completion (Subp)
15561 and then Has_Pragma_Inline_Always (Subp)
15562 and then Needs_No_Actuals (Subp)
15563 and then No (Contract (Subp))
15564 and then not Is_Dispatching_Operation (Subp)
15565 and then Needs_Finalization (Etype (Subp))
15566 and then not Is_Class_Wide_Type (Etype (Subp))
15567 and then not (Has_Invariants (Etype (Subp)))
15568 and then Present (Subprogram_Body (Subp))
15569 and then Was_Expression_Function (Subprogram_Body (Subp))
15570 then
15571 Return_Expr := Expression_Of_Expression_Function (Subp);
15572
15573 -- The returned object must not have a qualified expression and its
15574 -- nominal subtype must be statically compatible with the result
15575 -- subtype of the expression function.
15576
15577 return
15578 Nkind (Return_Expr) = N_Identifier
15579 and then Etype (Return_Expr) = Etype (Subp);
15580 end if;
15581
15582 return False;
15583 end Is_Inlinable_Expression_Function;
15584
15585 -----------------
15586 -- Is_Iterator --
15587 -----------------
15588
15589 function Is_Iterator (Typ : Entity_Id) return Boolean is
15590 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean;
15591 -- Determine whether type Iter_Typ is a predefined forward or reversible
15592 -- iterator.
15593
15594 ----------------------
15595 -- Denotes_Iterator --
15596 ----------------------
15597
15598 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean is
15599 begin
15600 -- Check that the name matches, and that the ultimate ancestor is in
15601 -- a predefined unit, i.e the one that declares iterator interfaces.
15602
15603 return
15604 Nam_In (Chars (Iter_Typ), Name_Forward_Iterator,
15605 Name_Reversible_Iterator)
15606 and then In_Predefined_Unit (Root_Type (Iter_Typ));
15607 end Denotes_Iterator;
15608
15609 -- Local variables
15610
15611 Iface_Elmt : Elmt_Id;
15612 Ifaces : Elist_Id;
15613
15614 -- Start of processing for Is_Iterator
15615
15616 begin
15617 -- The type may be a subtype of a descendant of the proper instance of
15618 -- the predefined interface type, so we must use the root type of the
15619 -- given type. The same is done for Is_Reversible_Iterator.
15620
15621 if Is_Class_Wide_Type (Typ)
15622 and then Denotes_Iterator (Root_Type (Typ))
15623 then
15624 return True;
15625
15626 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
15627 return False;
15628
15629 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
15630 return True;
15631
15632 else
15633 Collect_Interfaces (Typ, Ifaces);
15634
15635 Iface_Elmt := First_Elmt (Ifaces);
15636 while Present (Iface_Elmt) loop
15637 if Denotes_Iterator (Node (Iface_Elmt)) then
15638 return True;
15639 end if;
15640
15641 Next_Elmt (Iface_Elmt);
15642 end loop;
15643
15644 return False;
15645 end if;
15646 end Is_Iterator;
15647
15648 ----------------------------
15649 -- Is_Iterator_Over_Array --
15650 ----------------------------
15651
15652 function Is_Iterator_Over_Array (N : Node_Id) return Boolean is
15653 Container : constant Node_Id := Name (N);
15654 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
15655 begin
15656 return Is_Array_Type (Container_Typ);
15657 end Is_Iterator_Over_Array;
15658
15659 ------------
15660 -- Is_LHS --
15661 ------------
15662
15663 -- We seem to have a lot of overlapping functions that do similar things
15664 -- (testing for left hand sides or lvalues???).
15665
15666 function Is_LHS (N : Node_Id) return Is_LHS_Result is
15667 P : constant Node_Id := Parent (N);
15668
15669 begin
15670 -- Return True if we are the left hand side of an assignment statement
15671
15672 if Nkind (P) = N_Assignment_Statement then
15673 if Name (P) = N then
15674 return Yes;
15675 else
15676 return No;
15677 end if;
15678
15679 -- Case of prefix of indexed or selected component or slice
15680
15681 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
15682 and then N = Prefix (P)
15683 then
15684 -- Here we have the case where the parent P is N.Q or N(Q .. R).
15685 -- If P is an LHS, then N is also effectively an LHS, but there
15686 -- is an important exception. If N is of an access type, then
15687 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
15688 -- case this makes N.all a left hand side but not N itself.
15689
15690 -- If we don't know the type yet, this is the case where we return
15691 -- Unknown, since the answer depends on the type which is unknown.
15692
15693 if No (Etype (N)) then
15694 return Unknown;
15695
15696 -- We have an Etype set, so we can check it
15697
15698 elsif Is_Access_Type (Etype (N)) then
15699 return No;
15700
15701 -- OK, not access type case, so just test whole expression
15702
15703 else
15704 return Is_LHS (P);
15705 end if;
15706
15707 -- All other cases are not left hand sides
15708
15709 else
15710 return No;
15711 end if;
15712 end Is_LHS;
15713
15714 -----------------------------
15715 -- Is_Library_Level_Entity --
15716 -----------------------------
15717
15718 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
15719 begin
15720 -- The following is a small optimization, and it also properly handles
15721 -- discriminals, which in task bodies might appear in expressions before
15722 -- the corresponding procedure has been created, and which therefore do
15723 -- not have an assigned scope.
15724
15725 if Is_Formal (E) then
15726 return False;
15727 end if;
15728
15729 -- Normal test is simply that the enclosing dynamic scope is Standard
15730
15731 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
15732 end Is_Library_Level_Entity;
15733
15734 --------------------------------
15735 -- Is_Limited_Class_Wide_Type --
15736 --------------------------------
15737
15738 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
15739 begin
15740 return
15741 Is_Class_Wide_Type (Typ)
15742 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
15743 end Is_Limited_Class_Wide_Type;
15744
15745 ---------------------------------
15746 -- Is_Local_Variable_Reference --
15747 ---------------------------------
15748
15749 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
15750 begin
15751 if not Is_Entity_Name (Expr) then
15752 return False;
15753
15754 else
15755 declare
15756 Ent : constant Entity_Id := Entity (Expr);
15757 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
15758 begin
15759 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
15760 return False;
15761 else
15762 return Present (Sub) and then Sub = Current_Subprogram;
15763 end if;
15764 end;
15765 end if;
15766 end Is_Local_Variable_Reference;
15767
15768 -----------------------
15769 -- Is_Name_Reference --
15770 -----------------------
15771
15772 function Is_Name_Reference (N : Node_Id) return Boolean is
15773 begin
15774 if Is_Entity_Name (N) then
15775 return Present (Entity (N)) and then Is_Object (Entity (N));
15776 end if;
15777
15778 case Nkind (N) is
15779 when N_Indexed_Component
15780 | N_Slice
15781 =>
15782 return
15783 Is_Name_Reference (Prefix (N))
15784 or else Is_Access_Type (Etype (Prefix (N)));
15785
15786 -- Attributes 'Input, 'Old and 'Result produce objects
15787
15788 when N_Attribute_Reference =>
15789 return
15790 Nam_In (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
15791
15792 when N_Selected_Component =>
15793 return
15794 Is_Name_Reference (Selector_Name (N))
15795 and then
15796 (Is_Name_Reference (Prefix (N))
15797 or else Is_Access_Type (Etype (Prefix (N))));
15798
15799 when N_Explicit_Dereference =>
15800 return True;
15801
15802 -- A view conversion of a tagged name is a name reference
15803
15804 when N_Type_Conversion =>
15805 return
15806 Is_Tagged_Type (Etype (Subtype_Mark (N)))
15807 and then Is_Tagged_Type (Etype (Expression (N)))
15808 and then Is_Name_Reference (Expression (N));
15809
15810 -- An unchecked type conversion is considered to be a name if the
15811 -- operand is a name (this construction arises only as a result of
15812 -- expansion activities).
15813
15814 when N_Unchecked_Type_Conversion =>
15815 return Is_Name_Reference (Expression (N));
15816
15817 when others =>
15818 return False;
15819 end case;
15820 end Is_Name_Reference;
15821
15822 ------------------------------------
15823 -- Is_Non_Preelaborable_Construct --
15824 ------------------------------------
15825
15826 function Is_Non_Preelaborable_Construct (N : Node_Id) return Boolean is
15827
15828 -- NOTE: the routines within Is_Non_Preelaborable_Construct are
15829 -- intentionally unnested to avoid deep indentation of code.
15830
15831 Non_Preelaborable : exception;
15832 -- This exception is raised when the construct violates preelaborability
15833 -- to terminate the recursion.
15834
15835 procedure Visit (Nod : Node_Id);
15836 -- Semantically inspect construct Nod to determine whether it violates
15837 -- preelaborability. This routine raises Non_Preelaborable.
15838
15839 procedure Visit_List (List : List_Id);
15840 pragma Inline (Visit_List);
15841 -- Invoke Visit on each element of list List. This routine raises
15842 -- Non_Preelaborable.
15843
15844 procedure Visit_Pragma (Prag : Node_Id);
15845 pragma Inline (Visit_Pragma);
15846 -- Semantically inspect pragma Prag to determine whether it violates
15847 -- preelaborability. This routine raises Non_Preelaborable.
15848
15849 procedure Visit_Subexpression (Expr : Node_Id);
15850 pragma Inline (Visit_Subexpression);
15851 -- Semantically inspect expression Expr to determine whether it violates
15852 -- preelaborability. This routine raises Non_Preelaborable.
15853
15854 -----------
15855 -- Visit --
15856 -----------
15857
15858 procedure Visit (Nod : Node_Id) is
15859 begin
15860 case Nkind (Nod) is
15861
15862 -- Declarations
15863
15864 when N_Component_Declaration =>
15865
15866 -- Defining_Identifier is left out because it is not relevant
15867 -- for preelaborability.
15868
15869 Visit (Component_Definition (Nod));
15870 Visit (Expression (Nod));
15871
15872 when N_Derived_Type_Definition =>
15873
15874 -- Interface_List is left out because it is not relevant for
15875 -- preelaborability.
15876
15877 Visit (Record_Extension_Part (Nod));
15878 Visit (Subtype_Indication (Nod));
15879
15880 when N_Entry_Declaration =>
15881
15882 -- A protected type with at leat one entry is not preelaborable
15883 -- while task types are never preelaborable. This renders entry
15884 -- declarations non-preelaborable.
15885
15886 raise Non_Preelaborable;
15887
15888 when N_Full_Type_Declaration =>
15889
15890 -- Defining_Identifier and Discriminant_Specifications are left
15891 -- out because they are not relevant for preelaborability.
15892
15893 Visit (Type_Definition (Nod));
15894
15895 when N_Function_Instantiation
15896 | N_Package_Instantiation
15897 | N_Procedure_Instantiation
15898 =>
15899 -- Defining_Unit_Name and Name are left out because they are
15900 -- not relevant for preelaborability.
15901
15902 Visit_List (Generic_Associations (Nod));
15903
15904 when N_Object_Declaration =>
15905
15906 -- Defining_Identifier is left out because it is not relevant
15907 -- for preelaborability.
15908
15909 Visit (Object_Definition (Nod));
15910
15911 if Has_Init_Expression (Nod) then
15912 Visit (Expression (Nod));
15913
15914 elsif not Has_Preelaborable_Initialization
15915 (Etype (Defining_Entity (Nod)))
15916 then
15917 raise Non_Preelaborable;
15918 end if;
15919
15920 when N_Private_Extension_Declaration
15921 | N_Subtype_Declaration
15922 =>
15923 -- Defining_Identifier, Discriminant_Specifications, and
15924 -- Interface_List are left out because they are not relevant
15925 -- for preelaborability.
15926
15927 Visit (Subtype_Indication (Nod));
15928
15929 when N_Protected_Type_Declaration
15930 | N_Single_Protected_Declaration
15931 =>
15932 -- Defining_Identifier, Discriminant_Specifications, and
15933 -- Interface_List are left out because they are not relevant
15934 -- for preelaborability.
15935
15936 Visit (Protected_Definition (Nod));
15937
15938 -- A [single] task type is never preelaborable
15939
15940 when N_Single_Task_Declaration
15941 | N_Task_Type_Declaration
15942 =>
15943 raise Non_Preelaborable;
15944
15945 -- Pragmas
15946
15947 when N_Pragma =>
15948 Visit_Pragma (Nod);
15949
15950 -- Statements
15951
15952 when N_Statement_Other_Than_Procedure_Call =>
15953 if Nkind (Nod) /= N_Null_Statement then
15954 raise Non_Preelaborable;
15955 end if;
15956
15957 -- Subexpressions
15958
15959 when N_Subexpr =>
15960 Visit_Subexpression (Nod);
15961
15962 -- Special
15963
15964 when N_Access_To_Object_Definition =>
15965 Visit (Subtype_Indication (Nod));
15966
15967 when N_Case_Expression_Alternative =>
15968 Visit (Expression (Nod));
15969 Visit_List (Discrete_Choices (Nod));
15970
15971 when N_Component_Definition =>
15972 Visit (Access_Definition (Nod));
15973 Visit (Subtype_Indication (Nod));
15974
15975 when N_Component_List =>
15976 Visit_List (Component_Items (Nod));
15977 Visit (Variant_Part (Nod));
15978
15979 when N_Constrained_Array_Definition =>
15980 Visit_List (Discrete_Subtype_Definitions (Nod));
15981 Visit (Component_Definition (Nod));
15982
15983 when N_Delta_Constraint
15984 | N_Digits_Constraint
15985 =>
15986 -- Delta_Expression and Digits_Expression are left out because
15987 -- they are not relevant for preelaborability.
15988
15989 Visit (Range_Constraint (Nod));
15990
15991 when N_Discriminant_Specification =>
15992
15993 -- Defining_Identifier and Expression are left out because they
15994 -- are not relevant for preelaborability.
15995
15996 Visit (Discriminant_Type (Nod));
15997
15998 when N_Generic_Association =>
15999
16000 -- Selector_Name is left out because it is not relevant for
16001 -- preelaborability.
16002
16003 Visit (Explicit_Generic_Actual_Parameter (Nod));
16004
16005 when N_Index_Or_Discriminant_Constraint =>
16006 Visit_List (Constraints (Nod));
16007
16008 when N_Iterator_Specification =>
16009
16010 -- Defining_Identifier is left out because it is not relevant
16011 -- for preelaborability.
16012
16013 Visit (Name (Nod));
16014 Visit (Subtype_Indication (Nod));
16015
16016 when N_Loop_Parameter_Specification =>
16017
16018 -- Defining_Identifier is left out because it is not relevant
16019 -- for preelaborability.
16020
16021 Visit (Discrete_Subtype_Definition (Nod));
16022
16023 when N_Protected_Definition =>
16024
16025 -- End_Label is left out because it is not relevant for
16026 -- preelaborability.
16027
16028 Visit_List (Private_Declarations (Nod));
16029 Visit_List (Visible_Declarations (Nod));
16030
16031 when N_Range_Constraint =>
16032 Visit (Range_Expression (Nod));
16033
16034 when N_Record_Definition
16035 | N_Variant
16036 =>
16037 -- End_Label, Discrete_Choices, and Interface_List are left out
16038 -- because they are not relevant for preelaborability.
16039
16040 Visit (Component_List (Nod));
16041
16042 when N_Subtype_Indication =>
16043
16044 -- Subtype_Mark is left out because it is not relevant for
16045 -- preelaborability.
16046
16047 Visit (Constraint (Nod));
16048
16049 when N_Unconstrained_Array_Definition =>
16050
16051 -- Subtype_Marks is left out because it is not relevant for
16052 -- preelaborability.
16053
16054 Visit (Component_Definition (Nod));
16055
16056 when N_Variant_Part =>
16057
16058 -- Name is left out because it is not relevant for
16059 -- preelaborability.
16060
16061 Visit_List (Variants (Nod));
16062
16063 -- Default
16064
16065 when others =>
16066 null;
16067 end case;
16068 end Visit;
16069
16070 ----------------
16071 -- Visit_List --
16072 ----------------
16073
16074 procedure Visit_List (List : List_Id) is
16075 Nod : Node_Id;
16076
16077 begin
16078 if Present (List) then
16079 Nod := First (List);
16080 while Present (Nod) loop
16081 Visit (Nod);
16082 Next (Nod);
16083 end loop;
16084 end if;
16085 end Visit_List;
16086
16087 ------------------
16088 -- Visit_Pragma --
16089 ------------------
16090
16091 procedure Visit_Pragma (Prag : Node_Id) is
16092 begin
16093 case Get_Pragma_Id (Prag) is
16094 when Pragma_Assert
16095 | Pragma_Assert_And_Cut
16096 | Pragma_Assume
16097 | Pragma_Async_Readers
16098 | Pragma_Async_Writers
16099 | Pragma_Attribute_Definition
16100 | Pragma_Check
16101 | Pragma_Constant_After_Elaboration
16102 | Pragma_CPU
16103 | Pragma_Deadline_Floor
16104 | Pragma_Dispatching_Domain
16105 | Pragma_Effective_Reads
16106 | Pragma_Effective_Writes
16107 | Pragma_Extensions_Visible
16108 | Pragma_Ghost
16109 | Pragma_Secondary_Stack_Size
16110 | Pragma_Task_Name
16111 | Pragma_Volatile_Function
16112 =>
16113 Visit_List (Pragma_Argument_Associations (Prag));
16114
16115 -- Default
16116
16117 when others =>
16118 null;
16119 end case;
16120 end Visit_Pragma;
16121
16122 -------------------------
16123 -- Visit_Subexpression --
16124 -------------------------
16125
16126 procedure Visit_Subexpression (Expr : Node_Id) is
16127 procedure Visit_Aggregate (Aggr : Node_Id);
16128 pragma Inline (Visit_Aggregate);
16129 -- Semantically inspect aggregate Aggr to determine whether it
16130 -- violates preelaborability.
16131
16132 ---------------------
16133 -- Visit_Aggregate --
16134 ---------------------
16135
16136 procedure Visit_Aggregate (Aggr : Node_Id) is
16137 begin
16138 if not Is_Preelaborable_Aggregate (Aggr) then
16139 raise Non_Preelaborable;
16140 end if;
16141 end Visit_Aggregate;
16142
16143 -- Start of processing for Visit_Subexpression
16144
16145 begin
16146 case Nkind (Expr) is
16147 when N_Allocator
16148 | N_Qualified_Expression
16149 | N_Type_Conversion
16150 | N_Unchecked_Expression
16151 | N_Unchecked_Type_Conversion
16152 =>
16153 -- Subpool_Handle_Name and Subtype_Mark are left out because
16154 -- they are not relevant for preelaborability.
16155
16156 Visit (Expression (Expr));
16157
16158 when N_Aggregate
16159 | N_Extension_Aggregate
16160 =>
16161 Visit_Aggregate (Expr);
16162
16163 when N_Attribute_Reference
16164 | N_Explicit_Dereference
16165 | N_Reference
16166 =>
16167 -- Attribute_Name and Expressions are left out because they are
16168 -- not relevant for preelaborability.
16169
16170 Visit (Prefix (Expr));
16171
16172 when N_Case_Expression =>
16173
16174 -- End_Span is left out because it is not relevant for
16175 -- preelaborability.
16176
16177 Visit_List (Alternatives (Expr));
16178 Visit (Expression (Expr));
16179
16180 when N_Delta_Aggregate =>
16181 Visit_Aggregate (Expr);
16182 Visit (Expression (Expr));
16183
16184 when N_Expression_With_Actions =>
16185 Visit_List (Actions (Expr));
16186 Visit (Expression (Expr));
16187
16188 when N_If_Expression =>
16189 Visit_List (Expressions (Expr));
16190
16191 when N_Quantified_Expression =>
16192 Visit (Condition (Expr));
16193 Visit (Iterator_Specification (Expr));
16194 Visit (Loop_Parameter_Specification (Expr));
16195
16196 when N_Range =>
16197 Visit (High_Bound (Expr));
16198 Visit (Low_Bound (Expr));
16199
16200 when N_Slice =>
16201 Visit (Discrete_Range (Expr));
16202 Visit (Prefix (Expr));
16203
16204 -- Default
16205
16206 when others =>
16207
16208 -- The evaluation of an object name is not preelaborable,
16209 -- unless the name is a static expression (checked further
16210 -- below), or statically denotes a discriminant.
16211
16212 if Is_Entity_Name (Expr) then
16213 Object_Name : declare
16214 Id : constant Entity_Id := Entity (Expr);
16215
16216 begin
16217 if Is_Object (Id) then
16218 if Ekind (Id) = E_Discriminant then
16219 null;
16220
16221 elsif Ekind_In (Id, E_Constant, E_In_Parameter)
16222 and then Present (Discriminal_Link (Id))
16223 then
16224 null;
16225
16226 else
16227 raise Non_Preelaborable;
16228 end if;
16229 end if;
16230 end Object_Name;
16231
16232 -- A non-static expression is not preelaborable
16233
16234 elsif not Is_OK_Static_Expression (Expr) then
16235 raise Non_Preelaborable;
16236 end if;
16237 end case;
16238 end Visit_Subexpression;
16239
16240 -- Start of processing for Is_Non_Preelaborable_Construct
16241
16242 begin
16243 Visit (N);
16244
16245 -- At this point it is known that the construct is preelaborable
16246
16247 return False;
16248
16249 exception
16250
16251 -- The elaboration of the construct performs an action which violates
16252 -- preelaborability.
16253
16254 when Non_Preelaborable =>
16255 return True;
16256 end Is_Non_Preelaborable_Construct;
16257
16258 ---------------------------------
16259 -- Is_Nontrivial_DIC_Procedure --
16260 ---------------------------------
16261
16262 function Is_Nontrivial_DIC_Procedure (Id : Entity_Id) return Boolean is
16263 Body_Decl : Node_Id;
16264 Stmt : Node_Id;
16265
16266 begin
16267 if Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id) then
16268 Body_Decl :=
16269 Unit_Declaration_Node
16270 (Corresponding_Body (Unit_Declaration_Node (Id)));
16271
16272 -- The body of the Default_Initial_Condition procedure must contain
16273 -- at least one statement, otherwise the generation of the subprogram
16274 -- body failed.
16275
16276 pragma Assert (Present (Handled_Statement_Sequence (Body_Decl)));
16277
16278 -- To qualify as nontrivial, the first statement of the procedure
16279 -- must be a check in the form of an if statement. If the original
16280 -- Default_Initial_Condition expression was folded, then the first
16281 -- statement is not a check.
16282
16283 Stmt := First (Statements (Handled_Statement_Sequence (Body_Decl)));
16284
16285 return
16286 Nkind (Stmt) = N_If_Statement
16287 and then Nkind (Original_Node (Stmt)) = N_Pragma;
16288 end if;
16289
16290 return False;
16291 end Is_Nontrivial_DIC_Procedure;
16292
16293 -------------------------
16294 -- Is_Null_Record_Type --
16295 -------------------------
16296
16297 function Is_Null_Record_Type (T : Entity_Id) return Boolean is
16298 Decl : constant Node_Id := Parent (T);
16299 begin
16300 return Nkind (Decl) = N_Full_Type_Declaration
16301 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
16302 and then
16303 (No (Component_List (Type_Definition (Decl)))
16304 or else Null_Present (Component_List (Type_Definition (Decl))));
16305 end Is_Null_Record_Type;
16306
16307 ---------------------
16308 -- Is_Object_Image --
16309 ---------------------
16310
16311 function Is_Object_Image (Prefix : Node_Id) return Boolean is
16312 begin
16313 -- When the type of the prefix is not scalar, then the prefix is not
16314 -- valid in any scenario.
16315
16316 if not Is_Scalar_Type (Etype (Prefix)) then
16317 return False;
16318 end if;
16319
16320 -- Here we test for the case that the prefix is not a type and assume
16321 -- if it is not then it must be a named value or an object reference.
16322 -- This is because the parser always checks that prefixes of attributes
16323 -- are named.
16324
16325 return not (Is_Entity_Name (Prefix) and then Is_Type (Entity (Prefix)));
16326 end Is_Object_Image;
16327
16328 -------------------------
16329 -- Is_Object_Reference --
16330 -------------------------
16331
16332 function Is_Object_Reference (N : Node_Id) return Boolean is
16333 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
16334 -- Determine whether N is the name of an internally-generated renaming
16335
16336 --------------------------------------
16337 -- Is_Internally_Generated_Renaming --
16338 --------------------------------------
16339
16340 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
16341 P : Node_Id;
16342
16343 begin
16344 P := N;
16345 while Present (P) loop
16346 if Nkind (P) = N_Object_Renaming_Declaration then
16347 return not Comes_From_Source (P);
16348 elsif Is_List_Member (P) then
16349 return False;
16350 end if;
16351
16352 P := Parent (P);
16353 end loop;
16354
16355 return False;
16356 end Is_Internally_Generated_Renaming;
16357
16358 -- Start of processing for Is_Object_Reference
16359
16360 begin
16361 if Is_Entity_Name (N) then
16362 return Present (Entity (N)) and then Is_Object (Entity (N));
16363
16364 else
16365 case Nkind (N) is
16366 when N_Indexed_Component
16367 | N_Slice
16368 =>
16369 return
16370 Is_Object_Reference (Prefix (N))
16371 or else Is_Access_Type (Etype (Prefix (N)));
16372
16373 -- In Ada 95, a function call is a constant object; a procedure
16374 -- call is not.
16375
16376 -- Note that predefined operators are functions as well, and so
16377 -- are attributes that are (can be renamed as) functions.
16378
16379 when N_Binary_Op
16380 | N_Function_Call
16381 | N_Unary_Op
16382 =>
16383 return Etype (N) /= Standard_Void_Type;
16384
16385 -- Attributes references 'Loop_Entry, 'Old, and 'Result yield
16386 -- objects, even though they are not functions.
16387
16388 when N_Attribute_Reference =>
16389 return
16390 Nam_In (Attribute_Name (N), Name_Loop_Entry,
16391 Name_Old,
16392 Name_Result)
16393 or else Is_Function_Attribute_Name (Attribute_Name (N));
16394
16395 when N_Selected_Component =>
16396 return
16397 Is_Object_Reference (Selector_Name (N))
16398 and then
16399 (Is_Object_Reference (Prefix (N))
16400 or else Is_Access_Type (Etype (Prefix (N))));
16401
16402 -- An explicit dereference denotes an object, except that a
16403 -- conditional expression gets turned into an explicit dereference
16404 -- in some cases, and conditional expressions are not object
16405 -- names.
16406
16407 when N_Explicit_Dereference =>
16408 return not Nkind_In (Original_Node (N), N_Case_Expression,
16409 N_If_Expression);
16410
16411 -- A view conversion of a tagged object is an object reference
16412
16413 when N_Type_Conversion =>
16414 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
16415 and then Is_Tagged_Type (Etype (Expression (N)))
16416 and then Is_Object_Reference (Expression (N));
16417
16418 -- An unchecked type conversion is considered to be an object if
16419 -- the operand is an object (this construction arises only as a
16420 -- result of expansion activities).
16421
16422 when N_Unchecked_Type_Conversion =>
16423 return True;
16424
16425 -- Allow string literals to act as objects as long as they appear
16426 -- in internally-generated renamings. The expansion of iterators
16427 -- may generate such renamings when the range involves a string
16428 -- literal.
16429
16430 when N_String_Literal =>
16431 return Is_Internally_Generated_Renaming (Parent (N));
16432
16433 -- AI05-0003: In Ada 2012 a qualified expression is a name.
16434 -- This allows disambiguation of function calls and the use
16435 -- of aggregates in more contexts.
16436
16437 when N_Qualified_Expression =>
16438 if Ada_Version < Ada_2012 then
16439 return False;
16440 else
16441 return Is_Object_Reference (Expression (N))
16442 or else Nkind (Expression (N)) = N_Aggregate;
16443 end if;
16444
16445 when others =>
16446 return False;
16447 end case;
16448 end if;
16449 end Is_Object_Reference;
16450
16451 -----------------------------------
16452 -- Is_OK_Variable_For_Out_Formal --
16453 -----------------------------------
16454
16455 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
16456 begin
16457 Note_Possible_Modification (AV, Sure => True);
16458
16459 -- We must reject parenthesized variable names. Comes_From_Source is
16460 -- checked because there are currently cases where the compiler violates
16461 -- this rule (e.g. passing a task object to its controlled Initialize
16462 -- routine). This should be properly documented in sinfo???
16463
16464 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
16465 return False;
16466
16467 -- A variable is always allowed
16468
16469 elsif Is_Variable (AV) then
16470 return True;
16471
16472 -- Generalized indexing operations are rewritten as explicit
16473 -- dereferences, and it is only during resolution that we can
16474 -- check whether the context requires an access_to_variable type.
16475
16476 elsif Nkind (AV) = N_Explicit_Dereference
16477 and then Ada_Version >= Ada_2012
16478 and then Nkind (Original_Node (AV)) = N_Indexed_Component
16479 and then Present (Etype (Original_Node (AV)))
16480 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
16481 then
16482 return not Is_Access_Constant (Etype (Prefix (AV)));
16483
16484 -- Unchecked conversions are allowed only if they come from the
16485 -- generated code, which sometimes uses unchecked conversions for out
16486 -- parameters in cases where code generation is unaffected. We tell
16487 -- source unchecked conversions by seeing if they are rewrites of
16488 -- an original Unchecked_Conversion function call, or of an explicit
16489 -- conversion of a function call or an aggregate (as may happen in the
16490 -- expansion of a packed array aggregate).
16491
16492 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
16493 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
16494 return False;
16495
16496 elsif Comes_From_Source (AV)
16497 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
16498 then
16499 return False;
16500
16501 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
16502 return Is_OK_Variable_For_Out_Formal (Expression (AV));
16503
16504 else
16505 return True;
16506 end if;
16507
16508 -- Normal type conversions are allowed if argument is a variable
16509
16510 elsif Nkind (AV) = N_Type_Conversion then
16511 if Is_Variable (Expression (AV))
16512 and then Paren_Count (Expression (AV)) = 0
16513 then
16514 Note_Possible_Modification (Expression (AV), Sure => True);
16515 return True;
16516
16517 -- We also allow a non-parenthesized expression that raises
16518 -- constraint error if it rewrites what used to be a variable
16519
16520 elsif Raises_Constraint_Error (Expression (AV))
16521 and then Paren_Count (Expression (AV)) = 0
16522 and then Is_Variable (Original_Node (Expression (AV)))
16523 then
16524 return True;
16525
16526 -- Type conversion of something other than a variable
16527
16528 else
16529 return False;
16530 end if;
16531
16532 -- If this node is rewritten, then test the original form, if that is
16533 -- OK, then we consider the rewritten node OK (for example, if the
16534 -- original node is a conversion, then Is_Variable will not be true
16535 -- but we still want to allow the conversion if it converts a variable).
16536
16537 elsif Is_Rewrite_Substitution (AV) then
16538
16539 -- In Ada 2012, the explicit dereference may be a rewritten call to a
16540 -- Reference function.
16541
16542 if Ada_Version >= Ada_2012
16543 and then Nkind (Original_Node (AV)) = N_Function_Call
16544 and then
16545 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
16546 then
16547
16548 -- Check that this is not a constant reference.
16549
16550 return not Is_Access_Constant (Etype (Prefix (AV)));
16551
16552 elsif Has_Implicit_Dereference (Etype (Original_Node (AV))) then
16553 return
16554 not Is_Access_Constant (Etype
16555 (Get_Reference_Discriminant (Etype (Original_Node (AV)))));
16556
16557 else
16558 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
16559 end if;
16560
16561 -- All other non-variables are rejected
16562
16563 else
16564 return False;
16565 end if;
16566 end Is_OK_Variable_For_Out_Formal;
16567
16568 ----------------------------
16569 -- Is_OK_Volatile_Context --
16570 ----------------------------
16571
16572 function Is_OK_Volatile_Context
16573 (Context : Node_Id;
16574 Obj_Ref : Node_Id) return Boolean
16575 is
16576 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
16577 -- Determine whether an arbitrary node denotes a call to a protected
16578 -- entry, function, or procedure in prefixed form where the prefix is
16579 -- Obj_Ref.
16580
16581 function Within_Check (Nod : Node_Id) return Boolean;
16582 -- Determine whether an arbitrary node appears in a check node
16583
16584 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
16585 -- Determine whether an arbitrary entity appears in a volatile function
16586
16587 ---------------------------------
16588 -- Is_Protected_Operation_Call --
16589 ---------------------------------
16590
16591 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
16592 Pref : Node_Id;
16593 Subp : Node_Id;
16594
16595 begin
16596 -- A call to a protected operations retains its selected component
16597 -- form as opposed to other prefixed calls that are transformed in
16598 -- expanded names.
16599
16600 if Nkind (Nod) = N_Selected_Component then
16601 Pref := Prefix (Nod);
16602 Subp := Selector_Name (Nod);
16603
16604 return
16605 Pref = Obj_Ref
16606 and then Present (Etype (Pref))
16607 and then Is_Protected_Type (Etype (Pref))
16608 and then Is_Entity_Name (Subp)
16609 and then Present (Entity (Subp))
16610 and then Ekind_In (Entity (Subp), E_Entry,
16611 E_Entry_Family,
16612 E_Function,
16613 E_Procedure);
16614 else
16615 return False;
16616 end if;
16617 end Is_Protected_Operation_Call;
16618
16619 ------------------
16620 -- Within_Check --
16621 ------------------
16622
16623 function Within_Check (Nod : Node_Id) return Boolean is
16624 Par : Node_Id;
16625
16626 begin
16627 -- Climb the parent chain looking for a check node
16628
16629 Par := Nod;
16630 while Present (Par) loop
16631 if Nkind (Par) in N_Raise_xxx_Error then
16632 return True;
16633
16634 -- Prevent the search from going too far
16635
16636 elsif Is_Body_Or_Package_Declaration (Par) then
16637 exit;
16638 end if;
16639
16640 Par := Parent (Par);
16641 end loop;
16642
16643 return False;
16644 end Within_Check;
16645
16646 ------------------------------
16647 -- Within_Volatile_Function --
16648 ------------------------------
16649
16650 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
16651 Func_Id : Entity_Id;
16652
16653 begin
16654 -- Traverse the scope stack looking for a [generic] function
16655
16656 Func_Id := Id;
16657 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
16658 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
16659 return Is_Volatile_Function (Func_Id);
16660 end if;
16661
16662 Func_Id := Scope (Func_Id);
16663 end loop;
16664
16665 return False;
16666 end Within_Volatile_Function;
16667
16668 -- Local variables
16669
16670 Obj_Id : Entity_Id;
16671
16672 -- Start of processing for Is_OK_Volatile_Context
16673
16674 begin
16675 -- The volatile object appears on either side of an assignment
16676
16677 if Nkind (Context) = N_Assignment_Statement then
16678 return True;
16679
16680 -- The volatile object is part of the initialization expression of
16681 -- another object.
16682
16683 elsif Nkind (Context) = N_Object_Declaration
16684 and then Present (Expression (Context))
16685 and then Expression (Context) = Obj_Ref
16686 then
16687 Obj_Id := Defining_Entity (Context);
16688
16689 -- The volatile object acts as the initialization expression of an
16690 -- extended return statement. This is valid context as long as the
16691 -- function is volatile.
16692
16693 if Is_Return_Object (Obj_Id) then
16694 return Within_Volatile_Function (Obj_Id);
16695
16696 -- Otherwise this is a normal object initialization
16697
16698 else
16699 return True;
16700 end if;
16701
16702 -- The volatile object acts as the name of a renaming declaration
16703
16704 elsif Nkind (Context) = N_Object_Renaming_Declaration
16705 and then Name (Context) = Obj_Ref
16706 then
16707 return True;
16708
16709 -- The volatile object appears as an actual parameter in a call to an
16710 -- instance of Unchecked_Conversion whose result is renamed.
16711
16712 elsif Nkind (Context) = N_Function_Call
16713 and then Is_Entity_Name (Name (Context))
16714 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
16715 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
16716 then
16717 return True;
16718
16719 -- The volatile object is actually the prefix in a protected entry,
16720 -- function, or procedure call.
16721
16722 elsif Is_Protected_Operation_Call (Context) then
16723 return True;
16724
16725 -- The volatile object appears as the expression of a simple return
16726 -- statement that applies to a volatile function.
16727
16728 elsif Nkind (Context) = N_Simple_Return_Statement
16729 and then Expression (Context) = Obj_Ref
16730 then
16731 return
16732 Within_Volatile_Function (Return_Statement_Entity (Context));
16733
16734 -- The volatile object appears as the prefix of a name occurring in a
16735 -- non-interfering context.
16736
16737 elsif Nkind_In (Context, N_Attribute_Reference,
16738 N_Explicit_Dereference,
16739 N_Indexed_Component,
16740 N_Selected_Component,
16741 N_Slice)
16742 and then Prefix (Context) = Obj_Ref
16743 and then Is_OK_Volatile_Context
16744 (Context => Parent (Context),
16745 Obj_Ref => Context)
16746 then
16747 return True;
16748
16749 -- The volatile object appears as the prefix of attributes Address,
16750 -- Alignment, Component_Size, First, First_Bit, Last, Last_Bit, Length,
16751 -- Position, Size, Storage_Size.
16752
16753 elsif Nkind (Context) = N_Attribute_Reference
16754 and then Prefix (Context) = Obj_Ref
16755 and then Nam_In (Attribute_Name (Context), Name_Address,
16756 Name_Alignment,
16757 Name_Component_Size,
16758 Name_First,
16759 Name_First_Bit,
16760 Name_Last,
16761 Name_Last_Bit,
16762 Name_Length,
16763 Name_Position,
16764 Name_Size,
16765 Name_Storage_Size)
16766 then
16767 return True;
16768
16769 -- The volatile object appears as the expression of a type conversion
16770 -- occurring in a non-interfering context.
16771
16772 elsif Nkind_In (Context, N_Type_Conversion,
16773 N_Unchecked_Type_Conversion)
16774 and then Expression (Context) = Obj_Ref
16775 and then Is_OK_Volatile_Context
16776 (Context => Parent (Context),
16777 Obj_Ref => Context)
16778 then
16779 return True;
16780
16781 -- The volatile object appears as the expression in a delay statement
16782
16783 elsif Nkind (Context) in N_Delay_Statement then
16784 return True;
16785
16786 -- Allow references to volatile objects in various checks. This is not a
16787 -- direct SPARK 2014 requirement.
16788
16789 elsif Within_Check (Context) then
16790 return True;
16791
16792 -- Assume that references to effectively volatile objects that appear
16793 -- as actual parameters in a subprogram call are always legal. A full
16794 -- legality check is done when the actuals are resolved (see routine
16795 -- Resolve_Actuals).
16796
16797 elsif Within_Subprogram_Call (Context) then
16798 return True;
16799
16800 -- Otherwise the context is not suitable for an effectively volatile
16801 -- object.
16802
16803 else
16804 return False;
16805 end if;
16806 end Is_OK_Volatile_Context;
16807
16808 ------------------------------------
16809 -- Is_Package_Contract_Annotation --
16810 ------------------------------------
16811
16812 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
16813 Nam : Name_Id;
16814
16815 begin
16816 if Nkind (Item) = N_Aspect_Specification then
16817 Nam := Chars (Identifier (Item));
16818
16819 else pragma Assert (Nkind (Item) = N_Pragma);
16820 Nam := Pragma_Name (Item);
16821 end if;
16822
16823 return Nam = Name_Abstract_State
16824 or else Nam = Name_Initial_Condition
16825 or else Nam = Name_Initializes
16826 or else Nam = Name_Refined_State;
16827 end Is_Package_Contract_Annotation;
16828
16829 -----------------------------------
16830 -- Is_Partially_Initialized_Type --
16831 -----------------------------------
16832
16833 function Is_Partially_Initialized_Type
16834 (Typ : Entity_Id;
16835 Include_Implicit : Boolean := True) return Boolean
16836 is
16837 begin
16838 if Is_Scalar_Type (Typ) then
16839 return False;
16840
16841 elsif Is_Access_Type (Typ) then
16842 return Include_Implicit;
16843
16844 elsif Is_Array_Type (Typ) then
16845
16846 -- If component type is partially initialized, so is array type
16847
16848 if Is_Partially_Initialized_Type
16849 (Component_Type (Typ), Include_Implicit)
16850 then
16851 return True;
16852
16853 -- Otherwise we are only partially initialized if we are fully
16854 -- initialized (this is the empty array case, no point in us
16855 -- duplicating that code here).
16856
16857 else
16858 return Is_Fully_Initialized_Type (Typ);
16859 end if;
16860
16861 elsif Is_Record_Type (Typ) then
16862
16863 -- A discriminated type is always partially initialized if in
16864 -- all mode
16865
16866 if Has_Discriminants (Typ) and then Include_Implicit then
16867 return True;
16868
16869 -- A tagged type is always partially initialized
16870
16871 elsif Is_Tagged_Type (Typ) then
16872 return True;
16873
16874 -- Case of non-discriminated record
16875
16876 else
16877 declare
16878 Ent : Entity_Id;
16879
16880 Component_Present : Boolean := False;
16881 -- Set True if at least one component is present. If no
16882 -- components are present, then record type is fully
16883 -- initialized (another odd case, like the null array).
16884
16885 begin
16886 -- Loop through components
16887
16888 Ent := First_Entity (Typ);
16889 while Present (Ent) loop
16890 if Ekind (Ent) = E_Component then
16891 Component_Present := True;
16892
16893 -- If a component has an initialization expression then
16894 -- the enclosing record type is partially initialized
16895
16896 if Present (Parent (Ent))
16897 and then Present (Expression (Parent (Ent)))
16898 then
16899 return True;
16900
16901 -- If a component is of a type which is itself partially
16902 -- initialized, then the enclosing record type is also.
16903
16904 elsif Is_Partially_Initialized_Type
16905 (Etype (Ent), Include_Implicit)
16906 then
16907 return True;
16908 end if;
16909 end if;
16910
16911 Next_Entity (Ent);
16912 end loop;
16913
16914 -- No initialized components found. If we found any components
16915 -- they were all uninitialized so the result is false.
16916
16917 if Component_Present then
16918 return False;
16919
16920 -- But if we found no components, then all the components are
16921 -- initialized so we consider the type to be initialized.
16922
16923 else
16924 return True;
16925 end if;
16926 end;
16927 end if;
16928
16929 -- Concurrent types are always fully initialized
16930
16931 elsif Is_Concurrent_Type (Typ) then
16932 return True;
16933
16934 -- For a private type, go to underlying type. If there is no underlying
16935 -- type then just assume this partially initialized. Not clear if this
16936 -- can happen in a non-error case, but no harm in testing for this.
16937
16938 elsif Is_Private_Type (Typ) then
16939 declare
16940 U : constant Entity_Id := Underlying_Type (Typ);
16941 begin
16942 if No (U) then
16943 return True;
16944 else
16945 return Is_Partially_Initialized_Type (U, Include_Implicit);
16946 end if;
16947 end;
16948
16949 -- For any other type (are there any?) assume partially initialized
16950
16951 else
16952 return True;
16953 end if;
16954 end Is_Partially_Initialized_Type;
16955
16956 ------------------------------------
16957 -- Is_Potentially_Persistent_Type --
16958 ------------------------------------
16959
16960 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
16961 Comp : Entity_Id;
16962 Indx : Node_Id;
16963
16964 begin
16965 -- For private type, test corresponding full type
16966
16967 if Is_Private_Type (T) then
16968 return Is_Potentially_Persistent_Type (Full_View (T));
16969
16970 -- Scalar types are potentially persistent
16971
16972 elsif Is_Scalar_Type (T) then
16973 return True;
16974
16975 -- Record type is potentially persistent if not tagged and the types of
16976 -- all it components are potentially persistent, and no component has
16977 -- an initialization expression.
16978
16979 elsif Is_Record_Type (T)
16980 and then not Is_Tagged_Type (T)
16981 and then not Is_Partially_Initialized_Type (T)
16982 then
16983 Comp := First_Component (T);
16984 while Present (Comp) loop
16985 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
16986 return False;
16987 else
16988 Next_Entity (Comp);
16989 end if;
16990 end loop;
16991
16992 return True;
16993
16994 -- Array type is potentially persistent if its component type is
16995 -- potentially persistent and if all its constraints are static.
16996
16997 elsif Is_Array_Type (T) then
16998 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
16999 return False;
17000 end if;
17001
17002 Indx := First_Index (T);
17003 while Present (Indx) loop
17004 if not Is_OK_Static_Subtype (Etype (Indx)) then
17005 return False;
17006 else
17007 Next_Index (Indx);
17008 end if;
17009 end loop;
17010
17011 return True;
17012
17013 -- All other types are not potentially persistent
17014
17015 else
17016 return False;
17017 end if;
17018 end Is_Potentially_Persistent_Type;
17019
17020 --------------------------------
17021 -- Is_Potentially_Unevaluated --
17022 --------------------------------
17023
17024 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
17025 Par : Node_Id;
17026 Expr : Node_Id;
17027
17028 begin
17029 Expr := N;
17030 Par := N;
17031
17032 -- A postcondition whose expression is a short-circuit is broken down
17033 -- into individual aspects for better exception reporting. The original
17034 -- short-circuit expression is rewritten as the second operand, and an
17035 -- occurrence of 'Old in that operand is potentially unevaluated.
17036 -- See sem_ch13.adb for details of this transformation. The reference
17037 -- to 'Old may appear within an expression, so we must look for the
17038 -- enclosing pragma argument in the tree that contains the reference.
17039
17040 while Present (Par)
17041 and then Nkind (Par) /= N_Pragma_Argument_Association
17042 loop
17043 if Is_Rewrite_Substitution (Par)
17044 and then Nkind (Original_Node (Par)) = N_And_Then
17045 then
17046 return True;
17047 end if;
17048
17049 Par := Parent (Par);
17050 end loop;
17051
17052 -- Other cases; 'Old appears within other expression (not the top-level
17053 -- conjunct in a postcondition) with a potentially unevaluated operand.
17054
17055 Par := Parent (Expr);
17056 while not Nkind_In (Par, N_And_Then,
17057 N_Case_Expression,
17058 N_If_Expression,
17059 N_In,
17060 N_Not_In,
17061 N_Or_Else,
17062 N_Quantified_Expression)
17063 loop
17064 Expr := Par;
17065 Par := Parent (Par);
17066
17067 -- If the context is not an expression, or if is the result of
17068 -- expansion of an enclosing construct (such as another attribute)
17069 -- the predicate does not apply.
17070
17071 if Nkind (Par) = N_Case_Expression_Alternative then
17072 null;
17073
17074 elsif Nkind (Par) not in N_Subexpr
17075 or else not Comes_From_Source (Par)
17076 then
17077 return False;
17078 end if;
17079 end loop;
17080
17081 if Nkind (Par) = N_If_Expression then
17082 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
17083
17084 elsif Nkind (Par) = N_Case_Expression then
17085 return Expr /= Expression (Par);
17086
17087 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
17088 return Expr = Right_Opnd (Par);
17089
17090 elsif Nkind_In (Par, N_In, N_Not_In) then
17091
17092 -- If the membership includes several alternatives, only the first is
17093 -- definitely evaluated.
17094
17095 if Present (Alternatives (Par)) then
17096 return Expr /= First (Alternatives (Par));
17097
17098 -- If this is a range membership both bounds are evaluated
17099
17100 else
17101 return False;
17102 end if;
17103
17104 elsif Nkind (Par) = N_Quantified_Expression then
17105 return Expr = Condition (Par);
17106
17107 else
17108 return False;
17109 end if;
17110 end Is_Potentially_Unevaluated;
17111
17112 -----------------------------------------
17113 -- Is_Predefined_Dispatching_Operation --
17114 -----------------------------------------
17115
17116 function Is_Predefined_Dispatching_Operation
17117 (E : Entity_Id) return Boolean
17118 is
17119 TSS_Name : TSS_Name_Type;
17120
17121 begin
17122 if not Is_Dispatching_Operation (E) then
17123 return False;
17124 end if;
17125
17126 Get_Name_String (Chars (E));
17127
17128 -- Most predefined primitives have internally generated names. Equality
17129 -- must be treated differently; the predefined operation is recognized
17130 -- as a homogeneous binary operator that returns Boolean.
17131
17132 if Name_Len > TSS_Name_Type'Last then
17133 TSS_Name :=
17134 TSS_Name_Type
17135 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
17136
17137 if Nam_In (Chars (E), Name_uAssign, Name_uSize)
17138 or else
17139 (Chars (E) = Name_Op_Eq
17140 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
17141 or else TSS_Name = TSS_Deep_Adjust
17142 or else TSS_Name = TSS_Deep_Finalize
17143 or else TSS_Name = TSS_Stream_Input
17144 or else TSS_Name = TSS_Stream_Output
17145 or else TSS_Name = TSS_Stream_Read
17146 or else TSS_Name = TSS_Stream_Write
17147 or else Is_Predefined_Interface_Primitive (E)
17148 then
17149 return True;
17150 end if;
17151 end if;
17152
17153 return False;
17154 end Is_Predefined_Dispatching_Operation;
17155
17156 ---------------------------------------
17157 -- Is_Predefined_Interface_Primitive --
17158 ---------------------------------------
17159
17160 function Is_Predefined_Interface_Primitive (E : Entity_Id) return Boolean is
17161 begin
17162 -- In VM targets we don't restrict the functionality of this test to
17163 -- compiling in Ada 2005 mode since in VM targets any tagged type has
17164 -- these primitives.
17165
17166 return (Ada_Version >= Ada_2005 or else not Tagged_Type_Expansion)
17167 and then Nam_In (Chars (E), Name_uDisp_Asynchronous_Select,
17168 Name_uDisp_Conditional_Select,
17169 Name_uDisp_Get_Prim_Op_Kind,
17170 Name_uDisp_Get_Task_Id,
17171 Name_uDisp_Requeue,
17172 Name_uDisp_Timed_Select);
17173 end Is_Predefined_Interface_Primitive;
17174
17175 ---------------------------------------
17176 -- Is_Predefined_Internal_Operation --
17177 ---------------------------------------
17178
17179 function Is_Predefined_Internal_Operation
17180 (E : Entity_Id) return Boolean
17181 is
17182 TSS_Name : TSS_Name_Type;
17183
17184 begin
17185 if not Is_Dispatching_Operation (E) then
17186 return False;
17187 end if;
17188
17189 Get_Name_String (Chars (E));
17190
17191 -- Most predefined primitives have internally generated names. Equality
17192 -- must be treated differently; the predefined operation is recognized
17193 -- as a homogeneous binary operator that returns Boolean.
17194
17195 if Name_Len > TSS_Name_Type'Last then
17196 TSS_Name :=
17197 TSS_Name_Type
17198 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
17199
17200 if Nam_In (Chars (E), Name_uSize, Name_uAssign)
17201 or else
17202 (Chars (E) = Name_Op_Eq
17203 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
17204 or else TSS_Name = TSS_Deep_Adjust
17205 or else TSS_Name = TSS_Deep_Finalize
17206 or else Is_Predefined_Interface_Primitive (E)
17207 then
17208 return True;
17209 end if;
17210 end if;
17211
17212 return False;
17213 end Is_Predefined_Internal_Operation;
17214
17215 --------------------------------
17216 -- Is_Preelaborable_Aggregate --
17217 --------------------------------
17218
17219 function Is_Preelaborable_Aggregate (Aggr : Node_Id) return Boolean is
17220 Aggr_Typ : constant Entity_Id := Etype (Aggr);
17221 Array_Aggr : constant Boolean := Is_Array_Type (Aggr_Typ);
17222
17223 Anc_Part : Node_Id;
17224 Assoc : Node_Id;
17225 Choice : Node_Id;
17226 Comp_Typ : Entity_Id := Empty; -- init to avoid warning
17227 Expr : Node_Id;
17228
17229 begin
17230 if Array_Aggr then
17231 Comp_Typ := Component_Type (Aggr_Typ);
17232 end if;
17233
17234 -- Inspect the ancestor part
17235
17236 if Nkind (Aggr) = N_Extension_Aggregate then
17237 Anc_Part := Ancestor_Part (Aggr);
17238
17239 -- The ancestor denotes a subtype mark
17240
17241 if Is_Entity_Name (Anc_Part)
17242 and then Is_Type (Entity (Anc_Part))
17243 then
17244 if not Has_Preelaborable_Initialization (Entity (Anc_Part)) then
17245 return False;
17246 end if;
17247
17248 -- Otherwise the ancestor denotes an expression
17249
17250 elsif not Is_Preelaborable_Construct (Anc_Part) then
17251 return False;
17252 end if;
17253 end if;
17254
17255 -- Inspect the positional associations
17256
17257 Expr := First (Expressions (Aggr));
17258 while Present (Expr) loop
17259 if not Is_Preelaborable_Construct (Expr) then
17260 return False;
17261 end if;
17262
17263 Next (Expr);
17264 end loop;
17265
17266 -- Inspect the named associations
17267
17268 Assoc := First (Component_Associations (Aggr));
17269 while Present (Assoc) loop
17270
17271 -- Inspect the choices of the current named association
17272
17273 Choice := First (Choices (Assoc));
17274 while Present (Choice) loop
17275 if Array_Aggr then
17276
17277 -- For a choice to be preelaborable, it must denote either a
17278 -- static range or a static expression.
17279
17280 if Nkind (Choice) = N_Others_Choice then
17281 null;
17282
17283 elsif Nkind (Choice) = N_Range then
17284 if not Is_OK_Static_Range (Choice) then
17285 return False;
17286 end if;
17287
17288 elsif not Is_OK_Static_Expression (Choice) then
17289 return False;
17290 end if;
17291
17292 else
17293 Comp_Typ := Etype (Choice);
17294 end if;
17295
17296 Next (Choice);
17297 end loop;
17298
17299 -- The type of the choice must have preelaborable initialization if
17300 -- the association carries a <>.
17301
17302 pragma Assert (Present (Comp_Typ));
17303 if Box_Present (Assoc) then
17304 if not Has_Preelaborable_Initialization (Comp_Typ) then
17305 return False;
17306 end if;
17307
17308 -- The type of the expression must have preelaborable initialization
17309
17310 elsif not Is_Preelaborable_Construct (Expression (Assoc)) then
17311 return False;
17312 end if;
17313
17314 Next (Assoc);
17315 end loop;
17316
17317 -- At this point the aggregate is preelaborable
17318
17319 return True;
17320 end Is_Preelaborable_Aggregate;
17321
17322 --------------------------------
17323 -- Is_Preelaborable_Construct --
17324 --------------------------------
17325
17326 function Is_Preelaborable_Construct (N : Node_Id) return Boolean is
17327 begin
17328 -- Aggregates
17329
17330 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
17331 return Is_Preelaborable_Aggregate (N);
17332
17333 -- Attributes are allowed in general, even if their prefix is a formal
17334 -- type. It seems that certain attributes known not to be static might
17335 -- not be allowed, but there are no rules to prevent them.
17336
17337 elsif Nkind (N) = N_Attribute_Reference then
17338 return True;
17339
17340 -- Expressions
17341
17342 elsif Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
17343 return True;
17344
17345 elsif Nkind (N) = N_Qualified_Expression then
17346 return Is_Preelaborable_Construct (Expression (N));
17347
17348 -- Names are preelaborable when they denote a discriminant of an
17349 -- enclosing type. Discriminals are also considered for this check.
17350
17351 elsif Is_Entity_Name (N)
17352 and then Present (Entity (N))
17353 and then
17354 (Ekind (Entity (N)) = E_Discriminant
17355 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
17356 and then Present (Discriminal_Link (Entity (N)))))
17357 then
17358 return True;
17359
17360 -- Statements
17361
17362 elsif Nkind (N) = N_Null then
17363 return True;
17364
17365 -- Otherwise the construct is not preelaborable
17366
17367 else
17368 return False;
17369 end if;
17370 end Is_Preelaborable_Construct;
17371
17372 ---------------------------------
17373 -- Is_Protected_Self_Reference --
17374 ---------------------------------
17375
17376 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
17377
17378 function In_Access_Definition (N : Node_Id) return Boolean;
17379 -- Returns true if N belongs to an access definition
17380
17381 --------------------------
17382 -- In_Access_Definition --
17383 --------------------------
17384
17385 function In_Access_Definition (N : Node_Id) return Boolean is
17386 P : Node_Id;
17387
17388 begin
17389 P := Parent (N);
17390 while Present (P) loop
17391 if Nkind (P) = N_Access_Definition then
17392 return True;
17393 end if;
17394
17395 P := Parent (P);
17396 end loop;
17397
17398 return False;
17399 end In_Access_Definition;
17400
17401 -- Start of processing for Is_Protected_Self_Reference
17402
17403 begin
17404 -- Verify that prefix is analyzed and has the proper form. Note that
17405 -- the attributes Elab_Spec, Elab_Body, and Elab_Subp_Body, which also
17406 -- produce the address of an entity, do not analyze their prefix
17407 -- because they denote entities that are not necessarily visible.
17408 -- Neither of them can apply to a protected type.
17409
17410 return Ada_Version >= Ada_2005
17411 and then Is_Entity_Name (N)
17412 and then Present (Entity (N))
17413 and then Is_Protected_Type (Entity (N))
17414 and then In_Open_Scopes (Entity (N))
17415 and then not In_Access_Definition (N);
17416 end Is_Protected_Self_Reference;
17417
17418 -----------------------------
17419 -- Is_RCI_Pkg_Spec_Or_Body --
17420 -----------------------------
17421
17422 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
17423
17424 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
17425 -- Return True if the unit of Cunit is an RCI package declaration
17426
17427 ---------------------------
17428 -- Is_RCI_Pkg_Decl_Cunit --
17429 ---------------------------
17430
17431 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
17432 The_Unit : constant Node_Id := Unit (Cunit);
17433
17434 begin
17435 if Nkind (The_Unit) /= N_Package_Declaration then
17436 return False;
17437 end if;
17438
17439 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
17440 end Is_RCI_Pkg_Decl_Cunit;
17441
17442 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
17443
17444 begin
17445 return Is_RCI_Pkg_Decl_Cunit (Cunit)
17446 or else
17447 (Nkind (Unit (Cunit)) = N_Package_Body
17448 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
17449 end Is_RCI_Pkg_Spec_Or_Body;
17450
17451 -----------------------------------------
17452 -- Is_Remote_Access_To_Class_Wide_Type --
17453 -----------------------------------------
17454
17455 function Is_Remote_Access_To_Class_Wide_Type
17456 (E : Entity_Id) return Boolean
17457 is
17458 begin
17459 -- A remote access to class-wide type is a general access to object type
17460 -- declared in the visible part of a Remote_Types or Remote_Call_
17461 -- Interface unit.
17462
17463 return Ekind (E) = E_General_Access_Type
17464 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
17465 end Is_Remote_Access_To_Class_Wide_Type;
17466
17467 -----------------------------------------
17468 -- Is_Remote_Access_To_Subprogram_Type --
17469 -----------------------------------------
17470
17471 function Is_Remote_Access_To_Subprogram_Type
17472 (E : Entity_Id) return Boolean
17473 is
17474 begin
17475 return (Ekind (E) = E_Access_Subprogram_Type
17476 or else (Ekind (E) = E_Record_Type
17477 and then Present (Corresponding_Remote_Type (E))))
17478 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
17479 end Is_Remote_Access_To_Subprogram_Type;
17480
17481 --------------------
17482 -- Is_Remote_Call --
17483 --------------------
17484
17485 function Is_Remote_Call (N : Node_Id) return Boolean is
17486 begin
17487 if Nkind (N) not in N_Subprogram_Call then
17488
17489 -- An entry call cannot be remote
17490
17491 return False;
17492
17493 elsif Nkind (Name (N)) in N_Has_Entity
17494 and then Is_Remote_Call_Interface (Entity (Name (N)))
17495 then
17496 -- A subprogram declared in the spec of a RCI package is remote
17497
17498 return True;
17499
17500 elsif Nkind (Name (N)) = N_Explicit_Dereference
17501 and then Is_Remote_Access_To_Subprogram_Type
17502 (Etype (Prefix (Name (N))))
17503 then
17504 -- The dereference of a RAS is a remote call
17505
17506 return True;
17507
17508 elsif Present (Controlling_Argument (N))
17509 and then Is_Remote_Access_To_Class_Wide_Type
17510 (Etype (Controlling_Argument (N)))
17511 then
17512 -- Any primitive operation call with a controlling argument of
17513 -- a RACW type is a remote call.
17514
17515 return True;
17516 end if;
17517
17518 -- All other calls are local calls
17519
17520 return False;
17521 end Is_Remote_Call;
17522
17523 ----------------------
17524 -- Is_Renamed_Entry --
17525 ----------------------
17526
17527 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
17528 Orig_Node : Node_Id := Empty;
17529 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
17530
17531 function Is_Entry (Nam : Node_Id) return Boolean;
17532 -- Determine whether Nam is an entry. Traverse selectors if there are
17533 -- nested selected components.
17534
17535 --------------
17536 -- Is_Entry --
17537 --------------
17538
17539 function Is_Entry (Nam : Node_Id) return Boolean is
17540 begin
17541 if Nkind (Nam) = N_Selected_Component then
17542 return Is_Entry (Selector_Name (Nam));
17543 end if;
17544
17545 return Ekind (Entity (Nam)) = E_Entry;
17546 end Is_Entry;
17547
17548 -- Start of processing for Is_Renamed_Entry
17549
17550 begin
17551 if Present (Alias (Proc_Nam)) then
17552 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
17553 end if;
17554
17555 -- Look for a rewritten subprogram renaming declaration
17556
17557 if Nkind (Subp_Decl) = N_Subprogram_Declaration
17558 and then Present (Original_Node (Subp_Decl))
17559 then
17560 Orig_Node := Original_Node (Subp_Decl);
17561 end if;
17562
17563 -- The rewritten subprogram is actually an entry
17564
17565 if Present (Orig_Node)
17566 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
17567 and then Is_Entry (Name (Orig_Node))
17568 then
17569 return True;
17570 end if;
17571
17572 return False;
17573 end Is_Renamed_Entry;
17574
17575 -----------------------------
17576 -- Is_Renaming_Declaration --
17577 -----------------------------
17578
17579 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
17580 begin
17581 case Nkind (N) is
17582 when N_Exception_Renaming_Declaration
17583 | N_Generic_Function_Renaming_Declaration
17584 | N_Generic_Package_Renaming_Declaration
17585 | N_Generic_Procedure_Renaming_Declaration
17586 | N_Object_Renaming_Declaration
17587 | N_Package_Renaming_Declaration
17588 | N_Subprogram_Renaming_Declaration
17589 =>
17590 return True;
17591
17592 when others =>
17593 return False;
17594 end case;
17595 end Is_Renaming_Declaration;
17596
17597 ----------------------------
17598 -- Is_Reversible_Iterator --
17599 ----------------------------
17600
17601 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
17602 Ifaces_List : Elist_Id;
17603 Iface_Elmt : Elmt_Id;
17604 Iface : Entity_Id;
17605
17606 begin
17607 if Is_Class_Wide_Type (Typ)
17608 and then Chars (Root_Type (Typ)) = Name_Reversible_Iterator
17609 and then In_Predefined_Unit (Root_Type (Typ))
17610 then
17611 return True;
17612
17613 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
17614 return False;
17615
17616 else
17617 Collect_Interfaces (Typ, Ifaces_List);
17618
17619 Iface_Elmt := First_Elmt (Ifaces_List);
17620 while Present (Iface_Elmt) loop
17621 Iface := Node (Iface_Elmt);
17622 if Chars (Iface) = Name_Reversible_Iterator
17623 and then In_Predefined_Unit (Iface)
17624 then
17625 return True;
17626 end if;
17627
17628 Next_Elmt (Iface_Elmt);
17629 end loop;
17630 end if;
17631
17632 return False;
17633 end Is_Reversible_Iterator;
17634
17635 ----------------------
17636 -- Is_Selector_Name --
17637 ----------------------
17638
17639 function Is_Selector_Name (N : Node_Id) return Boolean is
17640 begin
17641 if not Is_List_Member (N) then
17642 declare
17643 P : constant Node_Id := Parent (N);
17644 begin
17645 return Nkind_In (P, N_Expanded_Name,
17646 N_Generic_Association,
17647 N_Parameter_Association,
17648 N_Selected_Component)
17649 and then Selector_Name (P) = N;
17650 end;
17651
17652 else
17653 declare
17654 L : constant List_Id := List_Containing (N);
17655 P : constant Node_Id := Parent (L);
17656 begin
17657 return (Nkind (P) = N_Discriminant_Association
17658 and then Selector_Names (P) = L)
17659 or else
17660 (Nkind (P) = N_Component_Association
17661 and then Choices (P) = L);
17662 end;
17663 end if;
17664 end Is_Selector_Name;
17665
17666 ---------------------------------
17667 -- Is_Single_Concurrent_Object --
17668 ---------------------------------
17669
17670 function Is_Single_Concurrent_Object (Id : Entity_Id) return Boolean is
17671 begin
17672 return
17673 Is_Single_Protected_Object (Id) or else Is_Single_Task_Object (Id);
17674 end Is_Single_Concurrent_Object;
17675
17676 -------------------------------
17677 -- Is_Single_Concurrent_Type --
17678 -------------------------------
17679
17680 function Is_Single_Concurrent_Type (Id : Entity_Id) return Boolean is
17681 begin
17682 return
17683 Ekind_In (Id, E_Protected_Type, E_Task_Type)
17684 and then Is_Single_Concurrent_Type_Declaration
17685 (Declaration_Node (Id));
17686 end Is_Single_Concurrent_Type;
17687
17688 -------------------------------------------
17689 -- Is_Single_Concurrent_Type_Declaration --
17690 -------------------------------------------
17691
17692 function Is_Single_Concurrent_Type_Declaration
17693 (N : Node_Id) return Boolean
17694 is
17695 begin
17696 return Nkind_In (Original_Node (N), N_Single_Protected_Declaration,
17697 N_Single_Task_Declaration);
17698 end Is_Single_Concurrent_Type_Declaration;
17699
17700 ---------------------------------------------
17701 -- Is_Single_Precision_Floating_Point_Type --
17702 ---------------------------------------------
17703
17704 function Is_Single_Precision_Floating_Point_Type
17705 (E : Entity_Id) return Boolean is
17706 begin
17707 return Is_Floating_Point_Type (E)
17708 and then Machine_Radix_Value (E) = Uint_2
17709 and then Machine_Mantissa_Value (E) = Uint_24
17710 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
17711 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
17712 end Is_Single_Precision_Floating_Point_Type;
17713
17714 --------------------------------
17715 -- Is_Single_Protected_Object --
17716 --------------------------------
17717
17718 function Is_Single_Protected_Object (Id : Entity_Id) return Boolean is
17719 begin
17720 return
17721 Ekind (Id) = E_Variable
17722 and then Ekind (Etype (Id)) = E_Protected_Type
17723 and then Is_Single_Concurrent_Type (Etype (Id));
17724 end Is_Single_Protected_Object;
17725
17726 ---------------------------
17727 -- Is_Single_Task_Object --
17728 ---------------------------
17729
17730 function Is_Single_Task_Object (Id : Entity_Id) return Boolean is
17731 begin
17732 return
17733 Ekind (Id) = E_Variable
17734 and then Ekind (Etype (Id)) = E_Task_Type
17735 and then Is_Single_Concurrent_Type (Etype (Id));
17736 end Is_Single_Task_Object;
17737
17738 -------------------------------------
17739 -- Is_SPARK_05_Initialization_Expr --
17740 -------------------------------------
17741
17742 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
17743 Is_Ok : Boolean;
17744 Expr : Node_Id;
17745 Comp_Assn : Node_Id;
17746 Orig_N : constant Node_Id := Original_Node (N);
17747
17748 begin
17749 Is_Ok := True;
17750
17751 if not Comes_From_Source (Orig_N) then
17752 goto Done;
17753 end if;
17754
17755 pragma Assert (Nkind (Orig_N) in N_Subexpr);
17756
17757 case Nkind (Orig_N) is
17758 when N_Character_Literal
17759 | N_Integer_Literal
17760 | N_Real_Literal
17761 | N_String_Literal
17762 =>
17763 null;
17764
17765 when N_Expanded_Name
17766 | N_Identifier
17767 =>
17768 if Is_Entity_Name (Orig_N)
17769 and then Present (Entity (Orig_N)) -- needed in some cases
17770 then
17771 case Ekind (Entity (Orig_N)) is
17772 when E_Constant
17773 | E_Enumeration_Literal
17774 | E_Named_Integer
17775 | E_Named_Real
17776 =>
17777 null;
17778
17779 when others =>
17780 if Is_Type (Entity (Orig_N)) then
17781 null;
17782 else
17783 Is_Ok := False;
17784 end if;
17785 end case;
17786 end if;
17787
17788 when N_Qualified_Expression
17789 | N_Type_Conversion
17790 =>
17791 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
17792
17793 when N_Unary_Op =>
17794 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
17795
17796 when N_Binary_Op
17797 | N_Membership_Test
17798 | N_Short_Circuit
17799 =>
17800 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
17801 and then
17802 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
17803
17804 when N_Aggregate
17805 | N_Extension_Aggregate
17806 =>
17807 if Nkind (Orig_N) = N_Extension_Aggregate then
17808 Is_Ok :=
17809 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
17810 end if;
17811
17812 Expr := First (Expressions (Orig_N));
17813 while Present (Expr) loop
17814 if not Is_SPARK_05_Initialization_Expr (Expr) then
17815 Is_Ok := False;
17816 goto Done;
17817 end if;
17818
17819 Next (Expr);
17820 end loop;
17821
17822 Comp_Assn := First (Component_Associations (Orig_N));
17823 while Present (Comp_Assn) loop
17824 Expr := Expression (Comp_Assn);
17825
17826 -- Note: test for Present here needed for box assocation
17827
17828 if Present (Expr)
17829 and then not Is_SPARK_05_Initialization_Expr (Expr)
17830 then
17831 Is_Ok := False;
17832 goto Done;
17833 end if;
17834
17835 Next (Comp_Assn);
17836 end loop;
17837
17838 when N_Attribute_Reference =>
17839 if Nkind (Prefix (Orig_N)) in N_Subexpr then
17840 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
17841 end if;
17842
17843 Expr := First (Expressions (Orig_N));
17844 while Present (Expr) loop
17845 if not Is_SPARK_05_Initialization_Expr (Expr) then
17846 Is_Ok := False;
17847 goto Done;
17848 end if;
17849
17850 Next (Expr);
17851 end loop;
17852
17853 -- Selected components might be expanded named not yet resolved, so
17854 -- default on the safe side. (Eg on sparklex.ads)
17855
17856 when N_Selected_Component =>
17857 null;
17858
17859 when others =>
17860 Is_Ok := False;
17861 end case;
17862
17863 <<Done>>
17864 return Is_Ok;
17865 end Is_SPARK_05_Initialization_Expr;
17866
17867 ----------------------------------
17868 -- Is_SPARK_05_Object_Reference --
17869 ----------------------------------
17870
17871 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
17872 begin
17873 if Is_Entity_Name (N) then
17874 return Present (Entity (N))
17875 and then
17876 (Ekind_In (Entity (N), E_Constant, E_Variable)
17877 or else Ekind (Entity (N)) in Formal_Kind);
17878
17879 else
17880 case Nkind (N) is
17881 when N_Selected_Component =>
17882 return Is_SPARK_05_Object_Reference (Prefix (N));
17883
17884 when others =>
17885 return False;
17886 end case;
17887 end if;
17888 end Is_SPARK_05_Object_Reference;
17889
17890 -----------------------------
17891 -- Is_Specific_Tagged_Type --
17892 -----------------------------
17893
17894 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
17895 Full_Typ : Entity_Id;
17896
17897 begin
17898 -- Handle private types
17899
17900 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
17901 Full_Typ := Full_View (Typ);
17902 else
17903 Full_Typ := Typ;
17904 end if;
17905
17906 -- A specific tagged type is a non-class-wide tagged type
17907
17908 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
17909 end Is_Specific_Tagged_Type;
17910
17911 ------------------
17912 -- Is_Statement --
17913 ------------------
17914
17915 function Is_Statement (N : Node_Id) return Boolean is
17916 begin
17917 return
17918 Nkind (N) in N_Statement_Other_Than_Procedure_Call
17919 or else Nkind (N) = N_Procedure_Call_Statement;
17920 end Is_Statement;
17921
17922 ----------------------------------------
17923 -- Is_Subcomponent_Of_Atomic_Object --
17924 ----------------------------------------
17925
17926 function Is_Subcomponent_Of_Atomic_Object (N : Node_Id) return Boolean is
17927 R : Node_Id;
17928
17929 begin
17930 R := Get_Referenced_Object (N);
17931
17932 while Nkind_In (R, N_Indexed_Component, N_Selected_Component, N_Slice)
17933 loop
17934 R := Get_Referenced_Object (Prefix (R));
17935
17936 -- If the prefix is an access value, only the designated type matters
17937
17938 if Is_Access_Type (Etype (R)) then
17939 if Is_Atomic (Designated_Type (Etype (R))) then
17940 return True;
17941 end if;
17942
17943 else
17944 if Is_Atomic_Object (R) then
17945 return True;
17946 end if;
17947 end if;
17948 end loop;
17949
17950 return False;
17951 end Is_Subcomponent_Of_Atomic_Object;
17952
17953 ---------------------------------------
17954 -- Is_Subprogram_Contract_Annotation --
17955 ---------------------------------------
17956
17957 function Is_Subprogram_Contract_Annotation
17958 (Item : Node_Id) return Boolean
17959 is
17960 Nam : Name_Id;
17961
17962 begin
17963 if Nkind (Item) = N_Aspect_Specification then
17964 Nam := Chars (Identifier (Item));
17965
17966 else pragma Assert (Nkind (Item) = N_Pragma);
17967 Nam := Pragma_Name (Item);
17968 end if;
17969
17970 return Nam = Name_Contract_Cases
17971 or else Nam = Name_Depends
17972 or else Nam = Name_Extensions_Visible
17973 or else Nam = Name_Global
17974 or else Nam = Name_Post
17975 or else Nam = Name_Post_Class
17976 or else Nam = Name_Postcondition
17977 or else Nam = Name_Pre
17978 or else Nam = Name_Pre_Class
17979 or else Nam = Name_Precondition
17980 or else Nam = Name_Refined_Depends
17981 or else Nam = Name_Refined_Global
17982 or else Nam = Name_Refined_Post
17983 or else Nam = Name_Test_Case;
17984 end Is_Subprogram_Contract_Annotation;
17985
17986 --------------------------------------------------
17987 -- Is_Subprogram_Stub_Without_Prior_Declaration --
17988 --------------------------------------------------
17989
17990 function Is_Subprogram_Stub_Without_Prior_Declaration
17991 (N : Node_Id) return Boolean
17992 is
17993 begin
17994 pragma Assert (Nkind (N) = N_Subprogram_Body_Stub);
17995
17996 case Ekind (Defining_Entity (N)) is
17997
17998 -- A subprogram stub without prior declaration serves as declaration
17999 -- for the actual subprogram body. As such, it has an attached
18000 -- defining entity of E_Function or E_Procedure.
18001
18002 when E_Function
18003 | E_Procedure
18004 =>
18005 return True;
18006
18007 -- Otherwise, it is completes a [generic] subprogram declaration
18008
18009 when E_Generic_Function
18010 | E_Generic_Procedure
18011 | E_Subprogram_Body
18012 =>
18013 return False;
18014
18015 when others =>
18016 raise Program_Error;
18017 end case;
18018 end Is_Subprogram_Stub_Without_Prior_Declaration;
18019
18020 ---------------------------
18021 -- Is_Suitable_Primitive --
18022 ---------------------------
18023
18024 function Is_Suitable_Primitive (Subp_Id : Entity_Id) return Boolean is
18025 begin
18026 -- The Default_Initial_Condition and invariant procedures must not be
18027 -- treated as primitive operations even when they apply to a tagged
18028 -- type. These routines must not act as targets of dispatching calls
18029 -- because they already utilize class-wide-precondition semantics to
18030 -- handle inheritance and overriding.
18031
18032 if Ekind (Subp_Id) = E_Procedure
18033 and then (Is_DIC_Procedure (Subp_Id)
18034 or else
18035 Is_Invariant_Procedure (Subp_Id))
18036 then
18037 return False;
18038 end if;
18039
18040 return True;
18041 end Is_Suitable_Primitive;
18042
18043 --------------------------
18044 -- Is_Suspension_Object --
18045 --------------------------
18046
18047 function Is_Suspension_Object (Id : Entity_Id) return Boolean is
18048 begin
18049 -- This approach does an exact name match rather than to rely on
18050 -- RTSfind. Routine Is_Effectively_Volatile is used by clients of the
18051 -- front end at point where all auxiliary tables are locked and any
18052 -- modifications to them are treated as violations. Do not tamper with
18053 -- the tables, instead examine the Chars fields of all the scopes of Id.
18054
18055 return
18056 Chars (Id) = Name_Suspension_Object
18057 and then Present (Scope (Id))
18058 and then Chars (Scope (Id)) = Name_Synchronous_Task_Control
18059 and then Present (Scope (Scope (Id)))
18060 and then Chars (Scope (Scope (Id))) = Name_Ada
18061 and then Present (Scope (Scope (Scope (Id))))
18062 and then Scope (Scope (Scope (Id))) = Standard_Standard;
18063 end Is_Suspension_Object;
18064
18065 ----------------------------
18066 -- Is_Synchronized_Object --
18067 ----------------------------
18068
18069 function Is_Synchronized_Object (Id : Entity_Id) return Boolean is
18070 Prag : Node_Id;
18071
18072 begin
18073 if Is_Object (Id) then
18074
18075 -- The object is synchronized if it is of a type that yields a
18076 -- synchronized object.
18077
18078 if Yields_Synchronized_Object (Etype (Id)) then
18079 return True;
18080
18081 -- The object is synchronized if it is atomic and Async_Writers is
18082 -- enabled.
18083
18084 elsif Is_Atomic_Object_Entity (Id)
18085 and then Async_Writers_Enabled (Id)
18086 then
18087 return True;
18088
18089 -- A constant is a synchronized object by default
18090
18091 elsif Ekind (Id) = E_Constant then
18092 return True;
18093
18094 -- A variable is a synchronized object if it is subject to pragma
18095 -- Constant_After_Elaboration.
18096
18097 elsif Ekind (Id) = E_Variable then
18098 Prag := Get_Pragma (Id, Pragma_Constant_After_Elaboration);
18099
18100 return Present (Prag) and then Is_Enabled_Pragma (Prag);
18101 end if;
18102 end if;
18103
18104 -- Otherwise the input is not an object or it does not qualify as a
18105 -- synchronized object.
18106
18107 return False;
18108 end Is_Synchronized_Object;
18109
18110 ---------------------------------
18111 -- Is_Synchronized_Tagged_Type --
18112 ---------------------------------
18113
18114 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
18115 Kind : constant Entity_Kind := Ekind (Base_Type (E));
18116
18117 begin
18118 -- A task or protected type derived from an interface is a tagged type.
18119 -- Such a tagged type is called a synchronized tagged type, as are
18120 -- synchronized interfaces and private extensions whose declaration
18121 -- includes the reserved word synchronized.
18122
18123 return (Is_Tagged_Type (E)
18124 and then (Kind = E_Task_Type
18125 or else
18126 Kind = E_Protected_Type))
18127 or else
18128 (Is_Interface (E)
18129 and then Is_Synchronized_Interface (E))
18130 or else
18131 (Ekind (E) = E_Record_Type_With_Private
18132 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
18133 and then (Synchronized_Present (Parent (E))
18134 or else Is_Synchronized_Interface (Etype (E))));
18135 end Is_Synchronized_Tagged_Type;
18136
18137 -----------------
18138 -- Is_Transfer --
18139 -----------------
18140
18141 function Is_Transfer (N : Node_Id) return Boolean is
18142 Kind : constant Node_Kind := Nkind (N);
18143
18144 begin
18145 if Kind = N_Simple_Return_Statement
18146 or else
18147 Kind = N_Extended_Return_Statement
18148 or else
18149 Kind = N_Goto_Statement
18150 or else
18151 Kind = N_Raise_Statement
18152 or else
18153 Kind = N_Requeue_Statement
18154 then
18155 return True;
18156
18157 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
18158 and then No (Condition (N))
18159 then
18160 return True;
18161
18162 elsif Kind = N_Procedure_Call_Statement
18163 and then Is_Entity_Name (Name (N))
18164 and then Present (Entity (Name (N)))
18165 and then No_Return (Entity (Name (N)))
18166 then
18167 return True;
18168
18169 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
18170 return True;
18171
18172 else
18173 return False;
18174 end if;
18175 end Is_Transfer;
18176
18177 -------------
18178 -- Is_True --
18179 -------------
18180
18181 function Is_True (U : Uint) return Boolean is
18182 begin
18183 return (U /= 0);
18184 end Is_True;
18185
18186 --------------------------------------
18187 -- Is_Unchecked_Conversion_Instance --
18188 --------------------------------------
18189
18190 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
18191 Par : Node_Id;
18192
18193 begin
18194 -- Look for a function whose generic parent is the predefined intrinsic
18195 -- function Unchecked_Conversion, or for one that renames such an
18196 -- instance.
18197
18198 if Ekind (Id) = E_Function then
18199 Par := Parent (Id);
18200
18201 if Nkind (Par) = N_Function_Specification then
18202 Par := Generic_Parent (Par);
18203
18204 if Present (Par) then
18205 return
18206 Chars (Par) = Name_Unchecked_Conversion
18207 and then Is_Intrinsic_Subprogram (Par)
18208 and then In_Predefined_Unit (Par);
18209 else
18210 return
18211 Present (Alias (Id))
18212 and then Is_Unchecked_Conversion_Instance (Alias (Id));
18213 end if;
18214 end if;
18215 end if;
18216
18217 return False;
18218 end Is_Unchecked_Conversion_Instance;
18219
18220 -------------------------------
18221 -- Is_Universal_Numeric_Type --
18222 -------------------------------
18223
18224 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
18225 begin
18226 return T = Universal_Integer or else T = Universal_Real;
18227 end Is_Universal_Numeric_Type;
18228
18229 ------------------------------
18230 -- Is_User_Defined_Equality --
18231 ------------------------------
18232
18233 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean is
18234 begin
18235 return Ekind (Id) = E_Function
18236 and then Chars (Id) = Name_Op_Eq
18237 and then Comes_From_Source (Id)
18238
18239 -- Internally generated equalities have a full type declaration
18240 -- as their parent.
18241
18242 and then Nkind (Parent (Id)) = N_Function_Specification;
18243 end Is_User_Defined_Equality;
18244
18245 --------------------------------------
18246 -- Is_Validation_Variable_Reference --
18247 --------------------------------------
18248
18249 function Is_Validation_Variable_Reference (N : Node_Id) return Boolean is
18250 Var : constant Node_Id := Unqual_Conv (N);
18251 Var_Id : Entity_Id;
18252
18253 begin
18254 Var_Id := Empty;
18255
18256 if Is_Entity_Name (Var) then
18257 Var_Id := Entity (Var);
18258 end if;
18259
18260 return
18261 Present (Var_Id)
18262 and then Ekind (Var_Id) = E_Variable
18263 and then Present (Validated_Object (Var_Id));
18264 end Is_Validation_Variable_Reference;
18265
18266 ----------------------------
18267 -- Is_Variable_Size_Array --
18268 ----------------------------
18269
18270 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
18271 Idx : Node_Id;
18272
18273 begin
18274 pragma Assert (Is_Array_Type (E));
18275
18276 -- Check if some index is initialized with a non-constant value
18277
18278 Idx := First_Index (E);
18279 while Present (Idx) loop
18280 if Nkind (Idx) = N_Range then
18281 if not Is_Constant_Bound (Low_Bound (Idx))
18282 or else not Is_Constant_Bound (High_Bound (Idx))
18283 then
18284 return True;
18285 end if;
18286 end if;
18287
18288 Idx := Next_Index (Idx);
18289 end loop;
18290
18291 return False;
18292 end Is_Variable_Size_Array;
18293
18294 -----------------------------
18295 -- Is_Variable_Size_Record --
18296 -----------------------------
18297
18298 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
18299 Comp : Entity_Id;
18300 Comp_Typ : Entity_Id;
18301
18302 begin
18303 pragma Assert (Is_Record_Type (E));
18304
18305 Comp := First_Component (E);
18306 while Present (Comp) loop
18307 Comp_Typ := Underlying_Type (Etype (Comp));
18308
18309 -- Recursive call if the record type has discriminants
18310
18311 if Is_Record_Type (Comp_Typ)
18312 and then Has_Discriminants (Comp_Typ)
18313 and then Is_Variable_Size_Record (Comp_Typ)
18314 then
18315 return True;
18316
18317 elsif Is_Array_Type (Comp_Typ)
18318 and then Is_Variable_Size_Array (Comp_Typ)
18319 then
18320 return True;
18321 end if;
18322
18323 Next_Component (Comp);
18324 end loop;
18325
18326 return False;
18327 end Is_Variable_Size_Record;
18328
18329 -----------------
18330 -- Is_Variable --
18331 -----------------
18332
18333 function Is_Variable
18334 (N : Node_Id;
18335 Use_Original_Node : Boolean := True) return Boolean
18336 is
18337 Orig_Node : Node_Id;
18338
18339 function In_Protected_Function (E : Entity_Id) return Boolean;
18340 -- Within a protected function, the private components of the enclosing
18341 -- protected type are constants. A function nested within a (protected)
18342 -- procedure is not itself protected. Within the body of a protected
18343 -- function the current instance of the protected type is a constant.
18344
18345 function Is_Variable_Prefix (P : Node_Id) return Boolean;
18346 -- Prefixes can involve implicit dereferences, in which case we must
18347 -- test for the case of a reference of a constant access type, which can
18348 -- can never be a variable.
18349
18350 ---------------------------
18351 -- In_Protected_Function --
18352 ---------------------------
18353
18354 function In_Protected_Function (E : Entity_Id) return Boolean is
18355 Prot : Entity_Id;
18356 S : Entity_Id;
18357
18358 begin
18359 -- E is the current instance of a type
18360
18361 if Is_Type (E) then
18362 Prot := E;
18363
18364 -- E is an object
18365
18366 else
18367 Prot := Scope (E);
18368 end if;
18369
18370 if not Is_Protected_Type (Prot) then
18371 return False;
18372
18373 else
18374 S := Current_Scope;
18375 while Present (S) and then S /= Prot loop
18376 if Ekind (S) = E_Function and then Scope (S) = Prot then
18377 return True;
18378 end if;
18379
18380 S := Scope (S);
18381 end loop;
18382
18383 return False;
18384 end if;
18385 end In_Protected_Function;
18386
18387 ------------------------
18388 -- Is_Variable_Prefix --
18389 ------------------------
18390
18391 function Is_Variable_Prefix (P : Node_Id) return Boolean is
18392 begin
18393 if Is_Access_Type (Etype (P)) then
18394 return not Is_Access_Constant (Root_Type (Etype (P)));
18395
18396 -- For the case of an indexed component whose prefix has a packed
18397 -- array type, the prefix has been rewritten into a type conversion.
18398 -- Determine variable-ness from the converted expression.
18399
18400 elsif Nkind (P) = N_Type_Conversion
18401 and then not Comes_From_Source (P)
18402 and then Is_Array_Type (Etype (P))
18403 and then Is_Packed (Etype (P))
18404 then
18405 return Is_Variable (Expression (P));
18406
18407 else
18408 return Is_Variable (P);
18409 end if;
18410 end Is_Variable_Prefix;
18411
18412 -- Start of processing for Is_Variable
18413
18414 begin
18415 -- Special check, allow x'Deref(expr) as a variable
18416
18417 if Nkind (N) = N_Attribute_Reference
18418 and then Attribute_Name (N) = Name_Deref
18419 then
18420 return True;
18421 end if;
18422
18423 -- Check if we perform the test on the original node since this may be a
18424 -- test of syntactic categories which must not be disturbed by whatever
18425 -- rewriting might have occurred. For example, an aggregate, which is
18426 -- certainly NOT a variable, could be turned into a variable by
18427 -- expansion.
18428
18429 if Use_Original_Node then
18430 Orig_Node := Original_Node (N);
18431 else
18432 Orig_Node := N;
18433 end if;
18434
18435 -- Definitely OK if Assignment_OK is set. Since this is something that
18436 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
18437
18438 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
18439 return True;
18440
18441 -- Normally we go to the original node, but there is one exception where
18442 -- we use the rewritten node, namely when it is an explicit dereference.
18443 -- The generated code may rewrite a prefix which is an access type with
18444 -- an explicit dereference. The dereference is a variable, even though
18445 -- the original node may not be (since it could be a constant of the
18446 -- access type).
18447
18448 -- In Ada 2005 we have a further case to consider: the prefix may be a
18449 -- function call given in prefix notation. The original node appears to
18450 -- be a selected component, but we need to examine the call.
18451
18452 elsif Nkind (N) = N_Explicit_Dereference
18453 and then Nkind (Orig_Node) /= N_Explicit_Dereference
18454 and then Present (Etype (Orig_Node))
18455 and then Is_Access_Type (Etype (Orig_Node))
18456 then
18457 -- Note that if the prefix is an explicit dereference that does not
18458 -- come from source, we must check for a rewritten function call in
18459 -- prefixed notation before other forms of rewriting, to prevent a
18460 -- compiler crash.
18461
18462 return
18463 (Nkind (Orig_Node) = N_Function_Call
18464 and then not Is_Access_Constant (Etype (Prefix (N))))
18465 or else
18466 Is_Variable_Prefix (Original_Node (Prefix (N)));
18467
18468 -- in Ada 2012, the dereference may have been added for a type with
18469 -- a declared implicit dereference aspect. Check that it is not an
18470 -- access to constant.
18471
18472 elsif Nkind (N) = N_Explicit_Dereference
18473 and then Present (Etype (Orig_Node))
18474 and then Ada_Version >= Ada_2012
18475 and then Has_Implicit_Dereference (Etype (Orig_Node))
18476 then
18477 return not Is_Access_Constant (Etype (Prefix (N)));
18478
18479 -- A function call is never a variable
18480
18481 elsif Nkind (N) = N_Function_Call then
18482 return False;
18483
18484 -- All remaining checks use the original node
18485
18486 elsif Is_Entity_Name (Orig_Node)
18487 and then Present (Entity (Orig_Node))
18488 then
18489 declare
18490 E : constant Entity_Id := Entity (Orig_Node);
18491 K : constant Entity_Kind := Ekind (E);
18492
18493 begin
18494 if Is_Loop_Parameter (E) then
18495 return False;
18496 end if;
18497
18498 return (K = E_Variable
18499 and then Nkind (Parent (E)) /= N_Exception_Handler)
18500 or else (K = E_Component
18501 and then not In_Protected_Function (E))
18502 or else K = E_Out_Parameter
18503 or else K = E_In_Out_Parameter
18504 or else K = E_Generic_In_Out_Parameter
18505
18506 -- Current instance of type. If this is a protected type, check
18507 -- we are not within the body of one of its protected functions.
18508
18509 or else (Is_Type (E)
18510 and then In_Open_Scopes (E)
18511 and then not In_Protected_Function (E))
18512
18513 or else (Is_Incomplete_Or_Private_Type (E)
18514 and then In_Open_Scopes (Full_View (E)));
18515 end;
18516
18517 else
18518 case Nkind (Orig_Node) is
18519 when N_Indexed_Component
18520 | N_Slice
18521 =>
18522 return Is_Variable_Prefix (Prefix (Orig_Node));
18523
18524 when N_Selected_Component =>
18525 return (Is_Variable (Selector_Name (Orig_Node))
18526 and then Is_Variable_Prefix (Prefix (Orig_Node)))
18527 or else
18528 (Nkind (N) = N_Expanded_Name
18529 and then Scope (Entity (N)) = Entity (Prefix (N)));
18530
18531 -- For an explicit dereference, the type of the prefix cannot
18532 -- be an access to constant or an access to subprogram.
18533
18534 when N_Explicit_Dereference =>
18535 declare
18536 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
18537 begin
18538 return Is_Access_Type (Typ)
18539 and then not Is_Access_Constant (Root_Type (Typ))
18540 and then Ekind (Typ) /= E_Access_Subprogram_Type;
18541 end;
18542
18543 -- The type conversion is the case where we do not deal with the
18544 -- context dependent special case of an actual parameter. Thus
18545 -- the type conversion is only considered a variable for the
18546 -- purposes of this routine if the target type is tagged. However,
18547 -- a type conversion is considered to be a variable if it does not
18548 -- come from source (this deals for example with the conversions
18549 -- of expressions to their actual subtypes).
18550
18551 when N_Type_Conversion =>
18552 return Is_Variable (Expression (Orig_Node))
18553 and then
18554 (not Comes_From_Source (Orig_Node)
18555 or else
18556 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
18557 and then
18558 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
18559
18560 -- GNAT allows an unchecked type conversion as a variable. This
18561 -- only affects the generation of internal expanded code, since
18562 -- calls to instantiations of Unchecked_Conversion are never
18563 -- considered variables (since they are function calls).
18564
18565 when N_Unchecked_Type_Conversion =>
18566 return Is_Variable (Expression (Orig_Node));
18567
18568 when others =>
18569 return False;
18570 end case;
18571 end if;
18572 end Is_Variable;
18573
18574 ---------------------------
18575 -- Is_Visibly_Controlled --
18576 ---------------------------
18577
18578 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
18579 Root : constant Entity_Id := Root_Type (T);
18580 begin
18581 return Chars (Scope (Root)) = Name_Finalization
18582 and then Chars (Scope (Scope (Root))) = Name_Ada
18583 and then Scope (Scope (Scope (Root))) = Standard_Standard;
18584 end Is_Visibly_Controlled;
18585
18586 --------------------------------------
18587 -- Is_Volatile_Full_Access_Object --
18588 --------------------------------------
18589
18590 function Is_Volatile_Full_Access_Object (N : Node_Id) return Boolean is
18591 function Is_VFA_Object_Entity (Id : Entity_Id) return Boolean;
18592 -- Determine whether arbitrary entity Id denotes an object that is
18593 -- Volatile_Full_Access.
18594
18595 ----------------------------
18596 -- Is_VFA_Object_Entity --
18597 ----------------------------
18598
18599 function Is_VFA_Object_Entity (Id : Entity_Id) return Boolean is
18600 begin
18601 return
18602 Is_Object (Id)
18603 and then (Is_Volatile_Full_Access (Id)
18604 or else
18605 Is_Volatile_Full_Access (Etype (Id)));
18606 end Is_VFA_Object_Entity;
18607
18608 -- Start of processing for Is_Volatile_Full_Access_Object
18609
18610 begin
18611 if Is_Entity_Name (N) then
18612 return Is_VFA_Object_Entity (Entity (N));
18613
18614 elsif Is_Volatile_Full_Access (Etype (N)) then
18615 return True;
18616
18617 elsif Nkind (N) = N_Selected_Component then
18618 return Is_Volatile_Full_Access (Entity (Selector_Name (N)));
18619
18620 else
18621 return False;
18622 end if;
18623 end Is_Volatile_Full_Access_Object;
18624
18625 --------------------------
18626 -- Is_Volatile_Function --
18627 --------------------------
18628
18629 function Is_Volatile_Function (Func_Id : Entity_Id) return Boolean is
18630 begin
18631 pragma Assert (Ekind_In (Func_Id, E_Function, E_Generic_Function));
18632
18633 -- A function declared within a protected type is volatile
18634
18635 if Is_Protected_Type (Scope (Func_Id)) then
18636 return True;
18637
18638 -- An instance of Ada.Unchecked_Conversion is a volatile function if
18639 -- either the source or the target are effectively volatile.
18640
18641 elsif Is_Unchecked_Conversion_Instance (Func_Id)
18642 and then Has_Effectively_Volatile_Profile (Func_Id)
18643 then
18644 return True;
18645
18646 -- Otherwise the function is treated as volatile if it is subject to
18647 -- enabled pragma Volatile_Function.
18648
18649 else
18650 return
18651 Is_Enabled_Pragma (Get_Pragma (Func_Id, Pragma_Volatile_Function));
18652 end if;
18653 end Is_Volatile_Function;
18654
18655 ------------------------
18656 -- Is_Volatile_Object --
18657 ------------------------
18658
18659 function Is_Volatile_Object (N : Node_Id) return Boolean is
18660 function Is_Volatile_Object_Entity (Id : Entity_Id) return Boolean;
18661 -- Determine whether arbitrary entity Id denotes an object that is
18662 -- Volatile.
18663
18664 function Prefix_Has_Volatile_Components (P : Node_Id) return Boolean;
18665 -- Determine whether prefix P has volatile components. This requires
18666 -- the presence of a Volatile_Components aspect/pragma or that P be
18667 -- itself a volatile object as per RM C.6(8).
18668
18669 ---------------------------------
18670 -- Is_Volatile_Object_Entity --
18671 ---------------------------------
18672
18673 function Is_Volatile_Object_Entity (Id : Entity_Id) return Boolean is
18674 begin
18675 return
18676 Is_Object (Id)
18677 and then (Is_Volatile (Id) or else Is_Volatile (Etype (Id)));
18678 end Is_Volatile_Object_Entity;
18679
18680 ------------------------------------
18681 -- Prefix_Has_Volatile_Components --
18682 ------------------------------------
18683
18684 function Prefix_Has_Volatile_Components (P : Node_Id) return Boolean is
18685 Typ : constant Entity_Id := Etype (P);
18686
18687 begin
18688 if Is_Access_Type (Typ) then
18689 declare
18690 Dtyp : constant Entity_Id := Designated_Type (Typ);
18691
18692 begin
18693 return Has_Volatile_Components (Dtyp)
18694 or else Is_Volatile (Dtyp);
18695 end;
18696
18697 elsif Has_Volatile_Components (Typ) then
18698 return True;
18699
18700 elsif Is_Entity_Name (P)
18701 and then Has_Volatile_Component (Entity (P))
18702 then
18703 return True;
18704
18705 elsif Is_Volatile_Object (P) then
18706 return True;
18707
18708 else
18709 return False;
18710 end if;
18711 end Prefix_Has_Volatile_Components;
18712
18713 -- Start of processing for Is_Volatile_Object
18714
18715 begin
18716 if Is_Entity_Name (N) then
18717 return Is_Volatile_Object_Entity (Entity (N));
18718
18719 elsif Is_Volatile (Etype (N)) then
18720 return True;
18721
18722 elsif Nkind (N) = N_Indexed_Component then
18723 return Prefix_Has_Volatile_Components (Prefix (N));
18724
18725 elsif Nkind (N) = N_Selected_Component then
18726 return Prefix_Has_Volatile_Components (Prefix (N))
18727 or else Is_Volatile (Entity (Selector_Name (N)));
18728
18729 else
18730 return False;
18731 end if;
18732 end Is_Volatile_Object;
18733
18734 -----------------------------
18735 -- Iterate_Call_Parameters --
18736 -----------------------------
18737
18738 procedure Iterate_Call_Parameters (Call : Node_Id) is
18739 Actual : Node_Id := First_Actual (Call);
18740 Formal : Entity_Id := First_Formal (Get_Called_Entity (Call));
18741
18742 begin
18743 while Present (Formal) and then Present (Actual) loop
18744 Handle_Parameter (Formal, Actual);
18745
18746 Next_Formal (Formal);
18747 Next_Actual (Actual);
18748 end loop;
18749
18750 pragma Assert (No (Formal));
18751 pragma Assert (No (Actual));
18752 end Iterate_Call_Parameters;
18753
18754 ---------------------------
18755 -- Itype_Has_Declaration --
18756 ---------------------------
18757
18758 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
18759 begin
18760 pragma Assert (Is_Itype (Id));
18761 return Present (Parent (Id))
18762 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
18763 N_Subtype_Declaration)
18764 and then Defining_Entity (Parent (Id)) = Id;
18765 end Itype_Has_Declaration;
18766
18767 -------------------------
18768 -- Kill_Current_Values --
18769 -------------------------
18770
18771 procedure Kill_Current_Values
18772 (Ent : Entity_Id;
18773 Last_Assignment_Only : Boolean := False)
18774 is
18775 begin
18776 if Is_Assignable (Ent) then
18777 Set_Last_Assignment (Ent, Empty);
18778 end if;
18779
18780 if Is_Object (Ent) then
18781 if not Last_Assignment_Only then
18782 Kill_Checks (Ent);
18783 Set_Current_Value (Ent, Empty);
18784
18785 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
18786 -- for a constant. Once the constant is elaborated, its value is
18787 -- not changed, therefore the associated flags that describe the
18788 -- value should not be modified either.
18789
18790 if Ekind (Ent) = E_Constant then
18791 null;
18792
18793 -- Non-constant entities
18794
18795 else
18796 if not Can_Never_Be_Null (Ent) then
18797 Set_Is_Known_Non_Null (Ent, False);
18798 end if;
18799
18800 Set_Is_Known_Null (Ent, False);
18801
18802 -- Reset the Is_Known_Valid flag unless the type is always
18803 -- valid. This does not apply to a loop parameter because its
18804 -- bounds are defined by the loop header and therefore always
18805 -- valid.
18806
18807 if not Is_Known_Valid (Etype (Ent))
18808 and then Ekind (Ent) /= E_Loop_Parameter
18809 then
18810 Set_Is_Known_Valid (Ent, False);
18811 end if;
18812 end if;
18813 end if;
18814 end if;
18815 end Kill_Current_Values;
18816
18817 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
18818 S : Entity_Id;
18819
18820 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
18821 -- Clear current value for entity E and all entities chained to E
18822
18823 ------------------------------------------
18824 -- Kill_Current_Values_For_Entity_Chain --
18825 ------------------------------------------
18826
18827 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
18828 Ent : Entity_Id;
18829 begin
18830 Ent := E;
18831 while Present (Ent) loop
18832 Kill_Current_Values (Ent, Last_Assignment_Only);
18833 Next_Entity (Ent);
18834 end loop;
18835 end Kill_Current_Values_For_Entity_Chain;
18836
18837 -- Start of processing for Kill_Current_Values
18838
18839 begin
18840 -- Kill all saved checks, a special case of killing saved values
18841
18842 if not Last_Assignment_Only then
18843 Kill_All_Checks;
18844 end if;
18845
18846 -- Loop through relevant scopes, which includes the current scope and
18847 -- any parent scopes if the current scope is a block or a package.
18848
18849 S := Current_Scope;
18850 Scope_Loop : loop
18851
18852 -- Clear current values of all entities in current scope
18853
18854 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
18855
18856 -- If scope is a package, also clear current values of all private
18857 -- entities in the scope.
18858
18859 if Is_Package_Or_Generic_Package (S)
18860 or else Is_Concurrent_Type (S)
18861 then
18862 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
18863 end if;
18864
18865 -- If this is a not a subprogram, deal with parents
18866
18867 if not Is_Subprogram (S) then
18868 S := Scope (S);
18869 exit Scope_Loop when S = Standard_Standard;
18870 else
18871 exit Scope_Loop;
18872 end if;
18873 end loop Scope_Loop;
18874 end Kill_Current_Values;
18875
18876 --------------------------
18877 -- Kill_Size_Check_Code --
18878 --------------------------
18879
18880 procedure Kill_Size_Check_Code (E : Entity_Id) is
18881 begin
18882 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
18883 and then Present (Size_Check_Code (E))
18884 then
18885 Remove (Size_Check_Code (E));
18886 Set_Size_Check_Code (E, Empty);
18887 end if;
18888 end Kill_Size_Check_Code;
18889
18890 --------------------
18891 -- Known_Non_Null --
18892 --------------------
18893
18894 function Known_Non_Null (N : Node_Id) return Boolean is
18895 Status : constant Null_Status_Kind := Null_Status (N);
18896
18897 Id : Entity_Id;
18898 Op : Node_Kind;
18899 Val : Node_Id;
18900
18901 begin
18902 -- The expression yields a non-null value ignoring simple flow analysis
18903
18904 if Status = Is_Non_Null then
18905 return True;
18906
18907 -- Otherwise check whether N is a reference to an entity that appears
18908 -- within a conditional construct.
18909
18910 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18911
18912 -- First check if we are in decisive conditional
18913
18914 Get_Current_Value_Condition (N, Op, Val);
18915
18916 if Known_Null (Val) then
18917 if Op = N_Op_Eq then
18918 return False;
18919 elsif Op = N_Op_Ne then
18920 return True;
18921 end if;
18922 end if;
18923
18924 -- If OK to do replacement, test Is_Known_Non_Null flag
18925
18926 Id := Entity (N);
18927
18928 if OK_To_Do_Constant_Replacement (Id) then
18929 return Is_Known_Non_Null (Id);
18930 end if;
18931 end if;
18932
18933 -- Otherwise it is not possible to determine whether N yields a non-null
18934 -- value.
18935
18936 return False;
18937 end Known_Non_Null;
18938
18939 ----------------
18940 -- Known_Null --
18941 ----------------
18942
18943 function Known_Null (N : Node_Id) return Boolean is
18944 Status : constant Null_Status_Kind := Null_Status (N);
18945
18946 Id : Entity_Id;
18947 Op : Node_Kind;
18948 Val : Node_Id;
18949
18950 begin
18951 -- The expression yields a null value ignoring simple flow analysis
18952
18953 if Status = Is_Null then
18954 return True;
18955
18956 -- Otherwise check whether N is a reference to an entity that appears
18957 -- within a conditional construct.
18958
18959 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18960
18961 -- First check if we are in decisive conditional
18962
18963 Get_Current_Value_Condition (N, Op, Val);
18964
18965 if Known_Null (Val) then
18966 if Op = N_Op_Eq then
18967 return True;
18968 elsif Op = N_Op_Ne then
18969 return False;
18970 end if;
18971 end if;
18972
18973 -- If OK to do replacement, test Is_Known_Null flag
18974
18975 Id := Entity (N);
18976
18977 if OK_To_Do_Constant_Replacement (Id) then
18978 return Is_Known_Null (Id);
18979 end if;
18980 end if;
18981
18982 -- Otherwise it is not possible to determine whether N yields a null
18983 -- value.
18984
18985 return False;
18986 end Known_Null;
18987
18988 --------------------------
18989 -- Known_To_Be_Assigned --
18990 --------------------------
18991
18992 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
18993 P : constant Node_Id := Parent (N);
18994
18995 begin
18996 case Nkind (P) is
18997
18998 -- Test left side of assignment
18999
19000 when N_Assignment_Statement =>
19001 return N = Name (P);
19002
19003 -- Function call arguments are never lvalues
19004
19005 when N_Function_Call =>
19006 return False;
19007
19008 -- Positional parameter for procedure or accept call
19009
19010 when N_Accept_Statement
19011 | N_Procedure_Call_Statement
19012 =>
19013 declare
19014 Proc : Entity_Id;
19015 Form : Entity_Id;
19016 Act : Node_Id;
19017
19018 begin
19019 Proc := Get_Subprogram_Entity (P);
19020
19021 if No (Proc) then
19022 return False;
19023 end if;
19024
19025 -- If we are not a list member, something is strange, so
19026 -- be conservative and return False.
19027
19028 if not Is_List_Member (N) then
19029 return False;
19030 end if;
19031
19032 -- We are going to find the right formal by stepping forward
19033 -- through the formals, as we step backwards in the actuals.
19034
19035 Form := First_Formal (Proc);
19036 Act := N;
19037 loop
19038 -- If no formal, something is weird, so be conservative
19039 -- and return False.
19040
19041 if No (Form) then
19042 return False;
19043 end if;
19044
19045 Prev (Act);
19046 exit when No (Act);
19047 Next_Formal (Form);
19048 end loop;
19049
19050 return Ekind (Form) /= E_In_Parameter;
19051 end;
19052
19053 -- Named parameter for procedure or accept call
19054
19055 when N_Parameter_Association =>
19056 declare
19057 Proc : Entity_Id;
19058 Form : Entity_Id;
19059
19060 begin
19061 Proc := Get_Subprogram_Entity (Parent (P));
19062
19063 if No (Proc) then
19064 return False;
19065 end if;
19066
19067 -- Loop through formals to find the one that matches
19068
19069 Form := First_Formal (Proc);
19070 loop
19071 -- If no matching formal, that's peculiar, some kind of
19072 -- previous error, so return False to be conservative.
19073 -- Actually this also happens in legal code in the case
19074 -- where P is a parameter association for an Extra_Formal???
19075
19076 if No (Form) then
19077 return False;
19078 end if;
19079
19080 -- Else test for match
19081
19082 if Chars (Form) = Chars (Selector_Name (P)) then
19083 return Ekind (Form) /= E_In_Parameter;
19084 end if;
19085
19086 Next_Formal (Form);
19087 end loop;
19088 end;
19089
19090 -- Test for appearing in a conversion that itself appears
19091 -- in an lvalue context, since this should be an lvalue.
19092
19093 when N_Type_Conversion =>
19094 return Known_To_Be_Assigned (P);
19095
19096 -- All other references are definitely not known to be modifications
19097
19098 when others =>
19099 return False;
19100 end case;
19101 end Known_To_Be_Assigned;
19102
19103 ---------------------------
19104 -- Last_Source_Statement --
19105 ---------------------------
19106
19107 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
19108 N : Node_Id;
19109
19110 begin
19111 N := Last (Statements (HSS));
19112 while Present (N) loop
19113 exit when Comes_From_Source (N);
19114 Prev (N);
19115 end loop;
19116
19117 return N;
19118 end Last_Source_Statement;
19119
19120 -----------------------
19121 -- Mark_Coextensions --
19122 -----------------------
19123
19124 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
19125 Is_Dynamic : Boolean;
19126 -- Indicates whether the context causes nested coextensions to be
19127 -- dynamic or static
19128
19129 function Mark_Allocator (N : Node_Id) return Traverse_Result;
19130 -- Recognize an allocator node and label it as a dynamic coextension
19131
19132 --------------------
19133 -- Mark_Allocator --
19134 --------------------
19135
19136 function Mark_Allocator (N : Node_Id) return Traverse_Result is
19137 begin
19138 if Nkind (N) = N_Allocator then
19139 if Is_Dynamic then
19140 Set_Is_Static_Coextension (N, False);
19141 Set_Is_Dynamic_Coextension (N);
19142
19143 -- If the allocator expression is potentially dynamic, it may
19144 -- be expanded out of order and require dynamic allocation
19145 -- anyway, so we treat the coextension itself as dynamic.
19146 -- Potential optimization ???
19147
19148 elsif Nkind (Expression (N)) = N_Qualified_Expression
19149 and then Nkind (Expression (Expression (N))) = N_Op_Concat
19150 then
19151 Set_Is_Static_Coextension (N, False);
19152 Set_Is_Dynamic_Coextension (N);
19153 else
19154 Set_Is_Dynamic_Coextension (N, False);
19155 Set_Is_Static_Coextension (N);
19156 end if;
19157 end if;
19158
19159 return OK;
19160 end Mark_Allocator;
19161
19162 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
19163
19164 -- Start of processing for Mark_Coextensions
19165
19166 begin
19167 -- An allocator that appears on the right-hand side of an assignment is
19168 -- treated as a potentially dynamic coextension when the right-hand side
19169 -- is an allocator or a qualified expression.
19170
19171 -- Obj := new ...'(new Coextension ...);
19172
19173 if Nkind (Context_Nod) = N_Assignment_Statement then
19174 Is_Dynamic :=
19175 Nkind_In (Expression (Context_Nod), N_Allocator,
19176 N_Qualified_Expression);
19177
19178 -- An allocator that appears within the expression of a simple return
19179 -- statement is treated as a potentially dynamic coextension when the
19180 -- expression is either aggregate, allocator, or qualified expression.
19181
19182 -- return (new Coextension ...);
19183 -- return new ...'(new Coextension ...);
19184
19185 elsif Nkind (Context_Nod) = N_Simple_Return_Statement then
19186 Is_Dynamic :=
19187 Nkind_In (Expression (Context_Nod), N_Aggregate,
19188 N_Allocator,
19189 N_Qualified_Expression);
19190
19191 -- An alloctor that appears within the initialization expression of an
19192 -- object declaration is considered a potentially dynamic coextension
19193 -- when the initialization expression is an allocator or a qualified
19194 -- expression.
19195
19196 -- Obj : ... := new ...'(new Coextension ...);
19197
19198 -- A similar case arises when the object declaration is part of an
19199 -- extended return statement.
19200
19201 -- return Obj : ... := new ...'(new Coextension ...);
19202 -- return Obj : ... := (new Coextension ...);
19203
19204 elsif Nkind (Context_Nod) = N_Object_Declaration then
19205 Is_Dynamic :=
19206 Nkind_In (Root_Nod, N_Allocator, N_Qualified_Expression)
19207 or else
19208 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
19209
19210 -- This routine should not be called with constructs that cannot contain
19211 -- coextensions.
19212
19213 else
19214 raise Program_Error;
19215 end if;
19216
19217 Mark_Allocators (Root_Nod);
19218 end Mark_Coextensions;
19219
19220 ---------------------------------
19221 -- Mark_Elaboration_Attributes --
19222 ---------------------------------
19223
19224 procedure Mark_Elaboration_Attributes
19225 (N_Id : Node_Or_Entity_Id;
19226 Checks : Boolean := False;
19227 Level : Boolean := False;
19228 Modes : Boolean := False;
19229 Warnings : Boolean := False)
19230 is
19231 function Elaboration_Checks_OK
19232 (Target_Id : Entity_Id;
19233 Context_Id : Entity_Id) return Boolean;
19234 -- Determine whether elaboration checks are enabled for target Target_Id
19235 -- which resides within context Context_Id.
19236
19237 procedure Mark_Elaboration_Attributes_Id (Id : Entity_Id);
19238 -- Preserve relevant attributes of the context in arbitrary entity Id
19239
19240 procedure Mark_Elaboration_Attributes_Node (N : Node_Id);
19241 -- Preserve relevant attributes of the context in arbitrary node N
19242
19243 ---------------------------
19244 -- Elaboration_Checks_OK --
19245 ---------------------------
19246
19247 function Elaboration_Checks_OK
19248 (Target_Id : Entity_Id;
19249 Context_Id : Entity_Id) return Boolean
19250 is
19251 Encl_Scop : Entity_Id;
19252
19253 begin
19254 -- Elaboration checks are suppressed for the target
19255
19256 if Elaboration_Checks_Suppressed (Target_Id) then
19257 return False;
19258 end if;
19259
19260 -- Otherwise elaboration checks are OK for the target, but may be
19261 -- suppressed for the context where the target is declared.
19262
19263 Encl_Scop := Context_Id;
19264 while Present (Encl_Scop) and then Encl_Scop /= Standard_Standard loop
19265 if Elaboration_Checks_Suppressed (Encl_Scop) then
19266 return False;
19267 end if;
19268
19269 Encl_Scop := Scope (Encl_Scop);
19270 end loop;
19271
19272 -- Neither the target nor its declarative context have elaboration
19273 -- checks suppressed.
19274
19275 return True;
19276 end Elaboration_Checks_OK;
19277
19278 ------------------------------------
19279 -- Mark_Elaboration_Attributes_Id --
19280 ------------------------------------
19281
19282 procedure Mark_Elaboration_Attributes_Id (Id : Entity_Id) is
19283 begin
19284 -- Mark the status of elaboration checks in effect. Do not reset the
19285 -- status in case the entity is reanalyzed with checks suppressed.
19286
19287 if Checks and then not Is_Elaboration_Checks_OK_Id (Id) then
19288 Set_Is_Elaboration_Checks_OK_Id (Id,
19289 Elaboration_Checks_OK
19290 (Target_Id => Id,
19291 Context_Id => Scope (Id)));
19292 end if;
19293
19294 -- Mark the status of elaboration warnings in effect. Do not reset
19295 -- the status in case the entity is reanalyzed with warnings off.
19296
19297 if Warnings and then not Is_Elaboration_Warnings_OK_Id (Id) then
19298 Set_Is_Elaboration_Warnings_OK_Id (Id, Elab_Warnings);
19299 end if;
19300 end Mark_Elaboration_Attributes_Id;
19301
19302 --------------------------------------
19303 -- Mark_Elaboration_Attributes_Node --
19304 --------------------------------------
19305
19306 procedure Mark_Elaboration_Attributes_Node (N : Node_Id) is
19307 function Extract_Name (N : Node_Id) return Node_Id;
19308 -- Obtain the Name attribute of call or instantiation N
19309
19310 ------------------
19311 -- Extract_Name --
19312 ------------------
19313
19314 function Extract_Name (N : Node_Id) return Node_Id is
19315 Nam : Node_Id;
19316
19317 begin
19318 Nam := Name (N);
19319
19320 -- A call to an entry family appears in indexed form
19321
19322 if Nkind (Nam) = N_Indexed_Component then
19323 Nam := Prefix (Nam);
19324 end if;
19325
19326 -- The name may also appear in qualified form
19327
19328 if Nkind (Nam) = N_Selected_Component then
19329 Nam := Selector_Name (Nam);
19330 end if;
19331
19332 return Nam;
19333 end Extract_Name;
19334
19335 -- Local variables
19336
19337 Context_Id : Entity_Id;
19338 Nam : Node_Id;
19339
19340 -- Start of processing for Mark_Elaboration_Attributes_Node
19341
19342 begin
19343 -- Mark the status of elaboration checks in effect. Do not reset the
19344 -- status in case the node is reanalyzed with checks suppressed.
19345
19346 if Checks and then not Is_Elaboration_Checks_OK_Node (N) then
19347
19348 -- Assignments, attribute references, and variable references do
19349 -- not have a "declarative" context.
19350
19351 Context_Id := Empty;
19352
19353 -- The status of elaboration checks for calls and instantiations
19354 -- depends on the most recent pragma Suppress/Unsuppress, as well
19355 -- as the suppression status of the context where the target is
19356 -- defined.
19357
19358 -- package Pack is
19359 -- function Func ...;
19360 -- end Pack;
19361
19362 -- with Pack;
19363 -- procedure Main is
19364 -- pragma Suppress (Elaboration_Checks, Pack);
19365 -- X : ... := Pack.Func;
19366 -- ...
19367
19368 -- In the example above, the call to Func has elaboration checks
19369 -- enabled because there is no active general purpose suppression
19370 -- pragma, however the elaboration checks of Pack are explicitly
19371 -- suppressed. As a result the elaboration checks of the call must
19372 -- be disabled in order to preserve this dependency.
19373
19374 if Nkind_In (N, N_Entry_Call_Statement,
19375 N_Function_Call,
19376 N_Function_Instantiation,
19377 N_Package_Instantiation,
19378 N_Procedure_Call_Statement,
19379 N_Procedure_Instantiation)
19380 then
19381 Nam := Extract_Name (N);
19382
19383 if Is_Entity_Name (Nam) and then Present (Entity (Nam)) then
19384 Context_Id := Scope (Entity (Nam));
19385 end if;
19386 end if;
19387
19388 Set_Is_Elaboration_Checks_OK_Node (N,
19389 Elaboration_Checks_OK
19390 (Target_Id => Empty,
19391 Context_Id => Context_Id));
19392 end if;
19393
19394 -- Mark the enclosing level of the node. Do not reset the status in
19395 -- case the node is relocated and reanalyzed.
19396
19397 if Level and then not Is_Declaration_Level_Node (N) then
19398 Set_Is_Declaration_Level_Node (N,
19399 Find_Enclosing_Level (N) = Declaration_Level);
19400 end if;
19401
19402 -- Mark the Ghost and SPARK mode in effect
19403
19404 if Modes then
19405 if Ghost_Mode = Ignore then
19406 Set_Is_Ignored_Ghost_Node (N);
19407 end if;
19408
19409 if SPARK_Mode = On then
19410 Set_Is_SPARK_Mode_On_Node (N);
19411 end if;
19412 end if;
19413
19414 -- Mark the status of elaboration warnings in effect. Do not reset
19415 -- the status in case the node is reanalyzed with warnings off.
19416
19417 if Warnings and then not Is_Elaboration_Warnings_OK_Node (N) then
19418 Set_Is_Elaboration_Warnings_OK_Node (N, Elab_Warnings);
19419 end if;
19420 end Mark_Elaboration_Attributes_Node;
19421
19422 -- Start of processing for Mark_Elaboration_Attributes
19423
19424 begin
19425 -- Do not capture any elaboration-related attributes when switch -gnatH
19426 -- (legacy elaboration checking mode enabled) is in effect because the
19427 -- attributes are useless to the legacy model.
19428
19429 if Legacy_Elaboration_Checks then
19430 return;
19431 end if;
19432
19433 if Nkind (N_Id) in N_Entity then
19434 Mark_Elaboration_Attributes_Id (N_Id);
19435 else
19436 Mark_Elaboration_Attributes_Node (N_Id);
19437 end if;
19438 end Mark_Elaboration_Attributes;
19439
19440 ----------------------------------------
19441 -- Mark_Save_Invocation_Graph_Of_Body --
19442 ----------------------------------------
19443
19444 procedure Mark_Save_Invocation_Graph_Of_Body is
19445 Main : constant Node_Id := Cunit (Main_Unit);
19446 Main_Unit : constant Node_Id := Unit (Main);
19447 Aux_Id : Entity_Id;
19448
19449 begin
19450 Set_Save_Invocation_Graph_Of_Body (Main);
19451
19452 -- Assume that the main unit does not have a complimentary unit
19453
19454 Aux_Id := Empty;
19455
19456 -- Obtain the complimentary unit of the main unit
19457
19458 if Nkind_In (Main_Unit, N_Generic_Package_Declaration,
19459 N_Generic_Subprogram_Declaration,
19460 N_Package_Declaration,
19461 N_Subprogram_Declaration)
19462 then
19463 Aux_Id := Corresponding_Body (Main_Unit);
19464
19465 elsif Nkind_In (Main_Unit, N_Package_Body,
19466 N_Subprogram_Body,
19467 N_Subprogram_Renaming_Declaration)
19468 then
19469 Aux_Id := Corresponding_Spec (Main_Unit);
19470 end if;
19471
19472 if Present (Aux_Id) then
19473 Set_Save_Invocation_Graph_Of_Body
19474 (Parent (Unit_Declaration_Node (Aux_Id)));
19475 end if;
19476 end Mark_Save_Invocation_Graph_Of_Body;
19477
19478 ----------------------------------
19479 -- Matching_Static_Array_Bounds --
19480 ----------------------------------
19481
19482 function Matching_Static_Array_Bounds
19483 (L_Typ : Node_Id;
19484 R_Typ : Node_Id) return Boolean
19485 is
19486 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
19487 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
19488
19489 L_Index : Node_Id := Empty; -- init to ...
19490 R_Index : Node_Id := Empty; -- ...avoid warnings
19491 L_Low : Node_Id;
19492 L_High : Node_Id;
19493 L_Len : Uint;
19494 R_Low : Node_Id;
19495 R_High : Node_Id;
19496 R_Len : Uint;
19497
19498 begin
19499 if L_Ndims /= R_Ndims then
19500 return False;
19501 end if;
19502
19503 -- Unconstrained types do not have static bounds
19504
19505 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
19506 return False;
19507 end if;
19508
19509 -- First treat specially the first dimension, as the lower bound and
19510 -- length of string literals are not stored like those of arrays.
19511
19512 if Ekind (L_Typ) = E_String_Literal_Subtype then
19513 L_Low := String_Literal_Low_Bound (L_Typ);
19514 L_Len := String_Literal_Length (L_Typ);
19515 else
19516 L_Index := First_Index (L_Typ);
19517 Get_Index_Bounds (L_Index, L_Low, L_High);
19518
19519 if Is_OK_Static_Expression (L_Low)
19520 and then
19521 Is_OK_Static_Expression (L_High)
19522 then
19523 if Expr_Value (L_High) < Expr_Value (L_Low) then
19524 L_Len := Uint_0;
19525 else
19526 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
19527 end if;
19528 else
19529 return False;
19530 end if;
19531 end if;
19532
19533 if Ekind (R_Typ) = E_String_Literal_Subtype then
19534 R_Low := String_Literal_Low_Bound (R_Typ);
19535 R_Len := String_Literal_Length (R_Typ);
19536 else
19537 R_Index := First_Index (R_Typ);
19538 Get_Index_Bounds (R_Index, R_Low, R_High);
19539
19540 if Is_OK_Static_Expression (R_Low)
19541 and then
19542 Is_OK_Static_Expression (R_High)
19543 then
19544 if Expr_Value (R_High) < Expr_Value (R_Low) then
19545 R_Len := Uint_0;
19546 else
19547 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
19548 end if;
19549 else
19550 return False;
19551 end if;
19552 end if;
19553
19554 if (Is_OK_Static_Expression (L_Low)
19555 and then
19556 Is_OK_Static_Expression (R_Low))
19557 and then Expr_Value (L_Low) = Expr_Value (R_Low)
19558 and then L_Len = R_Len
19559 then
19560 null;
19561 else
19562 return False;
19563 end if;
19564
19565 -- Then treat all other dimensions
19566
19567 for Indx in 2 .. L_Ndims loop
19568 Next (L_Index);
19569 Next (R_Index);
19570
19571 Get_Index_Bounds (L_Index, L_Low, L_High);
19572 Get_Index_Bounds (R_Index, R_Low, R_High);
19573
19574 if (Is_OK_Static_Expression (L_Low) and then
19575 Is_OK_Static_Expression (L_High) and then
19576 Is_OK_Static_Expression (R_Low) and then
19577 Is_OK_Static_Expression (R_High))
19578 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
19579 and then
19580 Expr_Value (L_High) = Expr_Value (R_High))
19581 then
19582 null;
19583 else
19584 return False;
19585 end if;
19586 end loop;
19587
19588 -- If we fall through the loop, all indexes matched
19589
19590 return True;
19591 end Matching_Static_Array_Bounds;
19592
19593 -------------------
19594 -- May_Be_Lvalue --
19595 -------------------
19596
19597 function May_Be_Lvalue (N : Node_Id) return Boolean is
19598 P : constant Node_Id := Parent (N);
19599
19600 begin
19601 case Nkind (P) is
19602
19603 -- Test left side of assignment
19604
19605 when N_Assignment_Statement =>
19606 return N = Name (P);
19607
19608 -- Test prefix of component or attribute. Note that the prefix of an
19609 -- explicit or implicit dereference cannot be an l-value. In the case
19610 -- of a 'Read attribute, the reference can be an actual in the
19611 -- argument list of the attribute.
19612
19613 when N_Attribute_Reference =>
19614 return (N = Prefix (P)
19615 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P)))
19616 or else
19617 Attribute_Name (P) = Name_Read;
19618
19619 -- For an expanded name, the name is an lvalue if the expanded name
19620 -- is an lvalue, but the prefix is never an lvalue, since it is just
19621 -- the scope where the name is found.
19622
19623 when N_Expanded_Name =>
19624 if N = Prefix (P) then
19625 return May_Be_Lvalue (P);
19626 else
19627 return False;
19628 end if;
19629
19630 -- For a selected component A.B, A is certainly an lvalue if A.B is.
19631 -- B is a little interesting, if we have A.B := 3, there is some
19632 -- discussion as to whether B is an lvalue or not, we choose to say
19633 -- it is. Note however that A is not an lvalue if it is of an access
19634 -- type since this is an implicit dereference.
19635
19636 when N_Selected_Component =>
19637 if N = Prefix (P)
19638 and then Present (Etype (N))
19639 and then Is_Access_Type (Etype (N))
19640 then
19641 return False;
19642 else
19643 return May_Be_Lvalue (P);
19644 end if;
19645
19646 -- For an indexed component or slice, the index or slice bounds is
19647 -- never an lvalue. The prefix is an lvalue if the indexed component
19648 -- or slice is an lvalue, except if it is an access type, where we
19649 -- have an implicit dereference.
19650
19651 when N_Indexed_Component
19652 | N_Slice
19653 =>
19654 if N /= Prefix (P)
19655 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
19656 then
19657 return False;
19658 else
19659 return May_Be_Lvalue (P);
19660 end if;
19661
19662 -- Prefix of a reference is an lvalue if the reference is an lvalue
19663
19664 when N_Reference =>
19665 return May_Be_Lvalue (P);
19666
19667 -- Prefix of explicit dereference is never an lvalue
19668
19669 when N_Explicit_Dereference =>
19670 return False;
19671
19672 -- Positional parameter for subprogram, entry, or accept call.
19673 -- In older versions of Ada function call arguments are never
19674 -- lvalues. In Ada 2012 functions can have in-out parameters.
19675
19676 when N_Accept_Statement
19677 | N_Entry_Call_Statement
19678 | N_Subprogram_Call
19679 =>
19680 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
19681 return False;
19682 end if;
19683
19684 -- The following mechanism is clumsy and fragile. A single flag
19685 -- set in Resolve_Actuals would be preferable ???
19686
19687 declare
19688 Proc : Entity_Id;
19689 Form : Entity_Id;
19690 Act : Node_Id;
19691
19692 begin
19693 Proc := Get_Subprogram_Entity (P);
19694
19695 if No (Proc) then
19696 return True;
19697 end if;
19698
19699 -- If we are not a list member, something is strange, so be
19700 -- conservative and return True.
19701
19702 if not Is_List_Member (N) then
19703 return True;
19704 end if;
19705
19706 -- We are going to find the right formal by stepping forward
19707 -- through the formals, as we step backwards in the actuals.
19708
19709 Form := First_Formal (Proc);
19710 Act := N;
19711 loop
19712 -- If no formal, something is weird, so be conservative and
19713 -- return True.
19714
19715 if No (Form) then
19716 return True;
19717 end if;
19718
19719 Prev (Act);
19720 exit when No (Act);
19721 Next_Formal (Form);
19722 end loop;
19723
19724 return Ekind (Form) /= E_In_Parameter;
19725 end;
19726
19727 -- Named parameter for procedure or accept call
19728
19729 when N_Parameter_Association =>
19730 declare
19731 Proc : Entity_Id;
19732 Form : Entity_Id;
19733
19734 begin
19735 Proc := Get_Subprogram_Entity (Parent (P));
19736
19737 if No (Proc) then
19738 return True;
19739 end if;
19740
19741 -- Loop through formals to find the one that matches
19742
19743 Form := First_Formal (Proc);
19744 loop
19745 -- If no matching formal, that's peculiar, some kind of
19746 -- previous error, so return True to be conservative.
19747 -- Actually happens with legal code for an unresolved call
19748 -- where we may get the wrong homonym???
19749
19750 if No (Form) then
19751 return True;
19752 end if;
19753
19754 -- Else test for match
19755
19756 if Chars (Form) = Chars (Selector_Name (P)) then
19757 return Ekind (Form) /= E_In_Parameter;
19758 end if;
19759
19760 Next_Formal (Form);
19761 end loop;
19762 end;
19763
19764 -- Test for appearing in a conversion that itself appears in an
19765 -- lvalue context, since this should be an lvalue.
19766
19767 when N_Type_Conversion =>
19768 return May_Be_Lvalue (P);
19769
19770 -- Test for appearance in object renaming declaration
19771
19772 when N_Object_Renaming_Declaration =>
19773 return True;
19774
19775 -- All other references are definitely not lvalues
19776
19777 when others =>
19778 return False;
19779 end case;
19780 end May_Be_Lvalue;
19781
19782 -----------------
19783 -- Might_Raise --
19784 -----------------
19785
19786 function Might_Raise (N : Node_Id) return Boolean is
19787 Result : Boolean := False;
19788
19789 function Process (N : Node_Id) return Traverse_Result;
19790 -- Set Result to True if we find something that could raise an exception
19791
19792 -------------
19793 -- Process --
19794 -------------
19795
19796 function Process (N : Node_Id) return Traverse_Result is
19797 begin
19798 if Nkind_In (N, N_Procedure_Call_Statement,
19799 N_Function_Call,
19800 N_Raise_Statement,
19801 N_Raise_Constraint_Error,
19802 N_Raise_Program_Error,
19803 N_Raise_Storage_Error)
19804 then
19805 Result := True;
19806 return Abandon;
19807 else
19808 return OK;
19809 end if;
19810 end Process;
19811
19812 procedure Set_Result is new Traverse_Proc (Process);
19813
19814 -- Start of processing for Might_Raise
19815
19816 begin
19817 -- False if exceptions can't be propagated
19818
19819 if No_Exception_Handlers_Set then
19820 return False;
19821 end if;
19822
19823 -- If the checks handled by the back end are not disabled, we cannot
19824 -- ensure that no exception will be raised.
19825
19826 if not Access_Checks_Suppressed (Empty)
19827 or else not Discriminant_Checks_Suppressed (Empty)
19828 or else not Range_Checks_Suppressed (Empty)
19829 or else not Index_Checks_Suppressed (Empty)
19830 or else Opt.Stack_Checking_Enabled
19831 then
19832 return True;
19833 end if;
19834
19835 Set_Result (N);
19836 return Result;
19837 end Might_Raise;
19838
19839 --------------------------------
19840 -- Nearest_Enclosing_Instance --
19841 --------------------------------
19842
19843 function Nearest_Enclosing_Instance (E : Entity_Id) return Entity_Id is
19844 Inst : Entity_Id;
19845
19846 begin
19847 Inst := Scope (E);
19848 while Present (Inst) and then Inst /= Standard_Standard loop
19849 if Is_Generic_Instance (Inst) then
19850 return Inst;
19851 end if;
19852
19853 Inst := Scope (Inst);
19854 end loop;
19855
19856 return Empty;
19857 end Nearest_Enclosing_Instance;
19858
19859 ------------------------
19860 -- Needs_Finalization --
19861 ------------------------
19862
19863 function Needs_Finalization (Typ : Entity_Id) return Boolean is
19864 function Has_Some_Controlled_Component
19865 (Input_Typ : Entity_Id) return Boolean;
19866 -- Determine whether type Input_Typ has at least one controlled
19867 -- component.
19868
19869 -----------------------------------
19870 -- Has_Some_Controlled_Component --
19871 -----------------------------------
19872
19873 function Has_Some_Controlled_Component
19874 (Input_Typ : Entity_Id) return Boolean
19875 is
19876 Comp : Entity_Id;
19877
19878 begin
19879 -- When a type is already frozen and has at least one controlled
19880 -- component, or is manually decorated, it is sufficient to inspect
19881 -- flag Has_Controlled_Component.
19882
19883 if Has_Controlled_Component (Input_Typ) then
19884 return True;
19885
19886 -- Otherwise inspect the internals of the type
19887
19888 elsif not Is_Frozen (Input_Typ) then
19889 if Is_Array_Type (Input_Typ) then
19890 return Needs_Finalization (Component_Type (Input_Typ));
19891
19892 elsif Is_Record_Type (Input_Typ) then
19893 Comp := First_Component (Input_Typ);
19894 while Present (Comp) loop
19895 if Needs_Finalization (Etype (Comp)) then
19896 return True;
19897 end if;
19898
19899 Next_Component (Comp);
19900 end loop;
19901 end if;
19902 end if;
19903
19904 return False;
19905 end Has_Some_Controlled_Component;
19906
19907 -- Start of processing for Needs_Finalization
19908
19909 begin
19910 -- Certain run-time configurations and targets do not provide support
19911 -- for controlled types.
19912
19913 if Restriction_Active (No_Finalization) then
19914 return False;
19915
19916 -- C++ types are not considered controlled. It is assumed that the non-
19917 -- Ada side will handle their clean up.
19918
19919 elsif Convention (Typ) = Convention_CPP then
19920 return False;
19921
19922 -- Class-wide types are treated as controlled because derivations from
19923 -- the root type may introduce controlled components.
19924
19925 elsif Is_Class_Wide_Type (Typ) then
19926 return True;
19927
19928 -- Concurrent types are controlled as long as their corresponding record
19929 -- is controlled.
19930
19931 elsif Is_Concurrent_Type (Typ)
19932 and then Present (Corresponding_Record_Type (Typ))
19933 and then Needs_Finalization (Corresponding_Record_Type (Typ))
19934 then
19935 return True;
19936
19937 -- Otherwise the type is controlled when it is either derived from type
19938 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
19939 -- contains at least one controlled component.
19940
19941 else
19942 return
19943 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
19944 end if;
19945 end Needs_Finalization;
19946
19947 ----------------------
19948 -- Needs_One_Actual --
19949 ----------------------
19950
19951 function Needs_One_Actual (E : Entity_Id) return Boolean is
19952 Formal : Entity_Id;
19953
19954 begin
19955 -- Ada 2005 or later, and formals present. The first formal must be
19956 -- of a type that supports prefix notation: a controlling argument,
19957 -- a class-wide type, or an access to such.
19958
19959 if Ada_Version >= Ada_2005
19960 and then Present (First_Formal (E))
19961 and then No (Default_Value (First_Formal (E)))
19962 and then
19963 (Is_Controlling_Formal (First_Formal (E))
19964 or else Is_Class_Wide_Type (Etype (First_Formal (E)))
19965 or else Is_Anonymous_Access_Type (Etype (First_Formal (E))))
19966 then
19967 Formal := Next_Formal (First_Formal (E));
19968 while Present (Formal) loop
19969 if No (Default_Value (Formal)) then
19970 return False;
19971 end if;
19972
19973 Next_Formal (Formal);
19974 end loop;
19975
19976 return True;
19977
19978 -- Ada 83/95 or no formals
19979
19980 else
19981 return False;
19982 end if;
19983 end Needs_One_Actual;
19984
19985 ---------------------------------
19986 -- Needs_Simple_Initialization --
19987 ---------------------------------
19988
19989 function Needs_Simple_Initialization
19990 (Typ : Entity_Id;
19991 Consider_IS : Boolean := True) return Boolean
19992 is
19993 Consider_IS_NS : constant Boolean :=
19994 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
19995
19996 begin
19997 -- Never need initialization if it is suppressed
19998
19999 if Initialization_Suppressed (Typ) then
20000 return False;
20001 end if;
20002
20003 -- Check for private type, in which case test applies to the underlying
20004 -- type of the private type.
20005
20006 if Is_Private_Type (Typ) then
20007 declare
20008 RT : constant Entity_Id := Underlying_Type (Typ);
20009 begin
20010 if Present (RT) then
20011 return Needs_Simple_Initialization (RT);
20012 else
20013 return False;
20014 end if;
20015 end;
20016
20017 -- Scalar type with Default_Value aspect requires initialization
20018
20019 elsif Is_Scalar_Type (Typ) and then Has_Default_Aspect (Typ) then
20020 return True;
20021
20022 -- Cases needing simple initialization are access types, and, if pragma
20023 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
20024 -- types.
20025
20026 elsif Is_Access_Type (Typ)
20027 or else (Consider_IS_NS and then (Is_Scalar_Type (Typ)))
20028 then
20029 return True;
20030
20031 -- If Initialize/Normalize_Scalars is in effect, string objects also
20032 -- need initialization, unless they are created in the course of
20033 -- expanding an aggregate (since in the latter case they will be
20034 -- filled with appropriate initializing values before they are used).
20035
20036 elsif Consider_IS_NS
20037 and then Is_Standard_String_Type (Typ)
20038 and then
20039 (not Is_Itype (Typ)
20040 or else Nkind (Associated_Node_For_Itype (Typ)) /= N_Aggregate)
20041 then
20042 return True;
20043
20044 else
20045 return False;
20046 end if;
20047 end Needs_Simple_Initialization;
20048
20049 -------------------------------------
20050 -- Needs_Variable_Reference_Marker --
20051 -------------------------------------
20052
20053 function Needs_Variable_Reference_Marker
20054 (N : Node_Id;
20055 Calls_OK : Boolean) return Boolean
20056 is
20057 function Within_Suitable_Context (Ref : Node_Id) return Boolean;
20058 -- Deteremine whether variable reference Ref appears within a suitable
20059 -- context that allows the creation of a marker.
20060
20061 -----------------------------
20062 -- Within_Suitable_Context --
20063 -----------------------------
20064
20065 function Within_Suitable_Context (Ref : Node_Id) return Boolean is
20066 Par : Node_Id;
20067
20068 begin
20069 Par := Ref;
20070 while Present (Par) loop
20071
20072 -- The context is not suitable when the reference appears within
20073 -- the formal part of an instantiation which acts as compilation
20074 -- unit because there is no proper list for the insertion of the
20075 -- marker.
20076
20077 if Nkind (Par) = N_Generic_Association
20078 and then Nkind (Parent (Par)) in N_Generic_Instantiation
20079 and then Nkind (Parent (Parent (Par))) = N_Compilation_Unit
20080 then
20081 return False;
20082
20083 -- The context is not suitable when the reference appears within
20084 -- a pragma. If the pragma has run-time semantics, the reference
20085 -- will be reconsidered once the pragma is expanded.
20086
20087 elsif Nkind (Par) = N_Pragma then
20088 return False;
20089
20090 -- The context is not suitable when the reference appears within a
20091 -- subprogram call, and the caller requests this behavior.
20092
20093 elsif not Calls_OK
20094 and then Nkind_In (Par, N_Entry_Call_Statement,
20095 N_Function_Call,
20096 N_Procedure_Call_Statement)
20097 then
20098 return False;
20099
20100 -- Prevent the search from going too far
20101
20102 elsif Is_Body_Or_Package_Declaration (Par) then
20103 exit;
20104 end if;
20105
20106 Par := Parent (Par);
20107 end loop;
20108
20109 return True;
20110 end Within_Suitable_Context;
20111
20112 -- Local variables
20113
20114 Prag : Node_Id;
20115 Var_Id : Entity_Id;
20116
20117 -- Start of processing for Needs_Variable_Reference_Marker
20118
20119 begin
20120 -- No marker needs to be created when switch -gnatH (legacy elaboration
20121 -- checking mode enabled) is in effect because the legacy ABE mechanism
20122 -- does not use markers.
20123
20124 if Legacy_Elaboration_Checks then
20125 return False;
20126
20127 -- No marker needs to be created for ASIS because ABE diagnostics and
20128 -- checks are not performed in this mode.
20129
20130 elsif ASIS_Mode then
20131 return False;
20132
20133 -- No marker needs to be created when the reference is preanalyzed
20134 -- because the marker will be inserted in the wrong place.
20135
20136 elsif Preanalysis_Active then
20137 return False;
20138
20139 -- Only references warrant a marker
20140
20141 elsif not Nkind_In (N, N_Expanded_Name, N_Identifier) then
20142 return False;
20143
20144 -- Only source references warrant a marker
20145
20146 elsif not Comes_From_Source (N) then
20147 return False;
20148
20149 -- No marker needs to be created when the reference is erroneous, left
20150 -- in a bad state, or does not denote a variable.
20151
20152 elsif not (Present (Entity (N))
20153 and then Ekind (Entity (N)) = E_Variable
20154 and then Entity (N) /= Any_Id)
20155 then
20156 return False;
20157 end if;
20158
20159 Var_Id := Entity (N);
20160 Prag := SPARK_Pragma (Var_Id);
20161
20162 -- Both the variable and reference must appear in SPARK_Mode On regions
20163 -- because this elaboration scenario falls under the SPARK rules.
20164
20165 if not (Comes_From_Source (Var_Id)
20166 and then Present (Prag)
20167 and then Get_SPARK_Mode_From_Annotation (Prag) = On
20168 and then Is_SPARK_Mode_On_Node (N))
20169 then
20170 return False;
20171
20172 -- No marker needs to be created when the reference does not appear
20173 -- within a suitable context (see body for details).
20174
20175 -- Performance note: parent traversal
20176
20177 elsif not Within_Suitable_Context (N) then
20178 return False;
20179 end if;
20180
20181 -- At this point it is known that the variable reference will play a
20182 -- role in ABE diagnostics and requires a marker.
20183
20184 return True;
20185 end Needs_Variable_Reference_Marker;
20186
20187 ------------------------
20188 -- New_Copy_List_Tree --
20189 ------------------------
20190
20191 function New_Copy_List_Tree (List : List_Id) return List_Id is
20192 NL : List_Id;
20193 E : Node_Id;
20194
20195 begin
20196 if List = No_List then
20197 return No_List;
20198
20199 else
20200 NL := New_List;
20201 E := First (List);
20202
20203 while Present (E) loop
20204 Append (New_Copy_Tree (E), NL);
20205 E := Next (E);
20206 end loop;
20207
20208 return NL;
20209 end if;
20210 end New_Copy_List_Tree;
20211
20212 -------------------
20213 -- New_Copy_Tree --
20214 -------------------
20215
20216 -- The following tables play a key role in replicating entities and Itypes.
20217 -- They are intentionally declared at the library level rather than within
20218 -- New_Copy_Tree to avoid elaborating them on each call. This performance
20219 -- optimization saves up to 2% of the entire compilation time spent in the
20220 -- front end. Care should be taken to reset the tables on each new call to
20221 -- New_Copy_Tree.
20222
20223 NCT_Table_Max : constant := 511;
20224
20225 subtype NCT_Table_Index is Nat range 0 .. NCT_Table_Max - 1;
20226
20227 function NCT_Table_Hash (Key : Node_Or_Entity_Id) return NCT_Table_Index;
20228 -- Obtain the hash value of node or entity Key
20229
20230 --------------------
20231 -- NCT_Table_Hash --
20232 --------------------
20233
20234 function NCT_Table_Hash (Key : Node_Or_Entity_Id) return NCT_Table_Index is
20235 begin
20236 return NCT_Table_Index (Key mod NCT_Table_Max);
20237 end NCT_Table_Hash;
20238
20239 ----------------------
20240 -- NCT_New_Entities --
20241 ----------------------
20242
20243 -- The following table maps old entities and Itypes to their corresponding
20244 -- new entities and Itypes.
20245
20246 -- Aaa -> Xxx
20247
20248 package NCT_New_Entities is new Simple_HTable (
20249 Header_Num => NCT_Table_Index,
20250 Element => Entity_Id,
20251 No_Element => Empty,
20252 Key => Entity_Id,
20253 Hash => NCT_Table_Hash,
20254 Equal => "=");
20255
20256 ------------------------
20257 -- NCT_Pending_Itypes --
20258 ------------------------
20259
20260 -- The following table maps old Associated_Node_For_Itype nodes to a set of
20261 -- new itypes. Given a set of old Itypes Aaa, Bbb, and Ccc, where all three
20262 -- have the same Associated_Node_For_Itype Ppp, and their corresponding new
20263 -- Itypes Xxx, Yyy, Zzz, the table contains the following mapping:
20264
20265 -- Ppp -> (Xxx, Yyy, Zzz)
20266
20267 -- The set is expressed as an Elist
20268
20269 package NCT_Pending_Itypes is new Simple_HTable (
20270 Header_Num => NCT_Table_Index,
20271 Element => Elist_Id,
20272 No_Element => No_Elist,
20273 Key => Node_Id,
20274 Hash => NCT_Table_Hash,
20275 Equal => "=");
20276
20277 NCT_Tables_In_Use : Boolean := False;
20278 -- This flag keeps track of whether the two tables NCT_New_Entities and
20279 -- NCT_Pending_Itypes are in use. The flag is part of an optimization
20280 -- where certain operations are not performed if the tables are not in
20281 -- use. This saves up to 8% of the entire compilation time spent in the
20282 -- front end.
20283
20284 -------------------
20285 -- New_Copy_Tree --
20286 -------------------
20287
20288 function New_Copy_Tree
20289 (Source : Node_Id;
20290 Map : Elist_Id := No_Elist;
20291 New_Sloc : Source_Ptr := No_Location;
20292 New_Scope : Entity_Id := Empty;
20293 Scopes_In_EWA_OK : Boolean := False) return Node_Id
20294 is
20295 -- This routine performs low-level tree manipulations and needs access
20296 -- to the internals of the tree.
20297
20298 use Atree.Unchecked_Access;
20299 use Atree_Private_Part;
20300
20301 EWA_Level : Nat := 0;
20302 -- This counter keeps track of how many N_Expression_With_Actions nodes
20303 -- are encountered during a depth-first traversal of the subtree. These
20304 -- nodes may define new entities in their Actions lists and thus require
20305 -- special processing.
20306
20307 EWA_Inner_Scope_Level : Nat := 0;
20308 -- This counter keeps track of how many scoping constructs appear within
20309 -- an N_Expression_With_Actions node.
20310
20311 procedure Add_New_Entity (Old_Id : Entity_Id; New_Id : Entity_Id);
20312 pragma Inline (Add_New_Entity);
20313 -- Add an entry in the NCT_New_Entities table which maps key Old_Id to
20314 -- value New_Id. Old_Id is an entity which appears within the Actions
20315 -- list of an N_Expression_With_Actions node, or within an entity map.
20316 -- New_Id is the corresponding new entity generated during Phase 1.
20317
20318 procedure Add_Pending_Itype (Assoc_Nod : Node_Id; Itype : Entity_Id);
20319 pragma Inline (Add_New_Entity);
20320 -- Add an entry in the NCT_Pending_Itypes which maps key Assoc_Nod to
20321 -- value Itype. Assoc_Nod is the associated node of an itype. Itype is
20322 -- an itype.
20323
20324 procedure Build_NCT_Tables (Entity_Map : Elist_Id);
20325 pragma Inline (Build_NCT_Tables);
20326 -- Populate tables NCT_New_Entities and NCT_Pending_Itypes with the
20327 -- information supplied in entity map Entity_Map. The format of the
20328 -- entity map must be as follows:
20329 --
20330 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
20331
20332 function Copy_Any_Node_With_Replacement
20333 (N : Node_Or_Entity_Id) return Node_Or_Entity_Id;
20334 pragma Inline (Copy_Any_Node_With_Replacement);
20335 -- Replicate entity or node N by invoking one of the following routines:
20336 --
20337 -- Copy_Node_With_Replacement
20338 -- Corresponding_Entity
20339
20340 function Copy_Elist_With_Replacement (List : Elist_Id) return Elist_Id;
20341 -- Replicate the elements of entity list List
20342
20343 function Copy_Field_With_Replacement
20344 (Field : Union_Id;
20345 Old_Par : Node_Id := Empty;
20346 New_Par : Node_Id := Empty;
20347 Semantic : Boolean := False) return Union_Id;
20348 -- Replicate field Field by invoking one of the following routines:
20349 --
20350 -- Copy_Elist_With_Replacement
20351 -- Copy_List_With_Replacement
20352 -- Copy_Node_With_Replacement
20353 -- Corresponding_Entity
20354 --
20355 -- If the field is not an entity list, entity, itype, syntactic list,
20356 -- or node, then the field is returned unchanged. The routine always
20357 -- replicates entities, itypes, and valid syntactic fields. Old_Par is
20358 -- the expected parent of a syntactic field. New_Par is the new parent
20359 -- associated with a replicated syntactic field. Flag Semantic should
20360 -- be set when the input is a semantic field.
20361
20362 function Copy_List_With_Replacement (List : List_Id) return List_Id;
20363 -- Replicate the elements of syntactic list List
20364
20365 function Copy_Node_With_Replacement (N : Node_Id) return Node_Id;
20366 -- Replicate node N
20367
20368 function Corresponding_Entity (Id : Entity_Id) return Entity_Id;
20369 pragma Inline (Corresponding_Entity);
20370 -- Return the corresponding new entity of Id generated during Phase 1.
20371 -- If there is no such entity, return Id.
20372
20373 function In_Entity_Map
20374 (Id : Entity_Id;
20375 Entity_Map : Elist_Id) return Boolean;
20376 pragma Inline (In_Entity_Map);
20377 -- Determine whether entity Id is one of the old ids specified in entity
20378 -- map Entity_Map. The format of the entity map must be as follows:
20379 --
20380 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
20381
20382 procedure Update_CFS_Sloc (N : Node_Or_Entity_Id);
20383 pragma Inline (Update_CFS_Sloc);
20384 -- Update the Comes_From_Source and Sloc attributes of node or entity N
20385
20386 procedure Update_First_Real_Statement
20387 (Old_HSS : Node_Id;
20388 New_HSS : Node_Id);
20389 pragma Inline (Update_First_Real_Statement);
20390 -- Update semantic attribute First_Real_Statement of handled sequence of
20391 -- statements New_HSS based on handled sequence of statements Old_HSS.
20392
20393 procedure Update_Named_Associations
20394 (Old_Call : Node_Id;
20395 New_Call : Node_Id);
20396 pragma Inline (Update_Named_Associations);
20397 -- Update semantic chain First/Next_Named_Association of call New_call
20398 -- based on call Old_Call.
20399
20400 procedure Update_New_Entities (Entity_Map : Elist_Id);
20401 pragma Inline (Update_New_Entities);
20402 -- Update the semantic attributes of all new entities generated during
20403 -- Phase 1 that do not appear in entity map Entity_Map. The format of
20404 -- the entity map must be as follows:
20405 --
20406 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
20407
20408 procedure Update_Pending_Itypes
20409 (Old_Assoc : Node_Id;
20410 New_Assoc : Node_Id);
20411 pragma Inline (Update_Pending_Itypes);
20412 -- Update semantic attribute Associated_Node_For_Itype to refer to node
20413 -- New_Assoc for all itypes whose associated node is Old_Assoc.
20414
20415 procedure Update_Semantic_Fields (Id : Entity_Id);
20416 pragma Inline (Update_Semantic_Fields);
20417 -- Subsidiary to Update_New_Entities. Update semantic fields of entity
20418 -- or itype Id.
20419
20420 procedure Visit_Any_Node (N : Node_Or_Entity_Id);
20421 pragma Inline (Visit_Any_Node);
20422 -- Visit entity of node N by invoking one of the following routines:
20423 --
20424 -- Visit_Entity
20425 -- Visit_Itype
20426 -- Visit_Node
20427
20428 procedure Visit_Elist (List : Elist_Id);
20429 -- Visit the elements of entity list List
20430
20431 procedure Visit_Entity (Id : Entity_Id);
20432 -- Visit entity Id. This action may create a new entity of Id and save
20433 -- it in table NCT_New_Entities.
20434
20435 procedure Visit_Field
20436 (Field : Union_Id;
20437 Par_Nod : Node_Id := Empty;
20438 Semantic : Boolean := False);
20439 -- Visit field Field by invoking one of the following routines:
20440 --
20441 -- Visit_Elist
20442 -- Visit_Entity
20443 -- Visit_Itype
20444 -- Visit_List
20445 -- Visit_Node
20446 --
20447 -- If the field is not an entity list, entity, itype, syntactic list,
20448 -- or node, then the field is not visited. The routine always visits
20449 -- valid syntactic fields. Par_Nod is the expected parent of the
20450 -- syntactic field. Flag Semantic should be set when the input is a
20451 -- semantic field.
20452
20453 procedure Visit_Itype (Itype : Entity_Id);
20454 -- Visit itype Itype. This action may create a new entity for Itype and
20455 -- save it in table NCT_New_Entities. In addition, the routine may map
20456 -- the associated node of Itype to the new itype in NCT_Pending_Itypes.
20457
20458 procedure Visit_List (List : List_Id);
20459 -- Visit the elements of syntactic list List
20460
20461 procedure Visit_Node (N : Node_Id);
20462 -- Visit node N
20463
20464 procedure Visit_Semantic_Fields (Id : Entity_Id);
20465 pragma Inline (Visit_Semantic_Fields);
20466 -- Subsidiary to Visit_Entity and Visit_Itype. Visit common semantic
20467 -- fields of entity or itype Id.
20468
20469 --------------------
20470 -- Add_New_Entity --
20471 --------------------
20472
20473 procedure Add_New_Entity (Old_Id : Entity_Id; New_Id : Entity_Id) is
20474 begin
20475 pragma Assert (Present (Old_Id));
20476 pragma Assert (Present (New_Id));
20477 pragma Assert (Nkind (Old_Id) in N_Entity);
20478 pragma Assert (Nkind (New_Id) in N_Entity);
20479
20480 NCT_Tables_In_Use := True;
20481
20482 -- Sanity check the NCT_New_Entities table. No previous mapping with
20483 -- key Old_Id should exist.
20484
20485 pragma Assert (No (NCT_New_Entities.Get (Old_Id)));
20486
20487 -- Establish the mapping
20488
20489 -- Old_Id -> New_Id
20490
20491 NCT_New_Entities.Set (Old_Id, New_Id);
20492 end Add_New_Entity;
20493
20494 -----------------------
20495 -- Add_Pending_Itype --
20496 -----------------------
20497
20498 procedure Add_Pending_Itype (Assoc_Nod : Node_Id; Itype : Entity_Id) is
20499 Itypes : Elist_Id;
20500
20501 begin
20502 pragma Assert (Present (Assoc_Nod));
20503 pragma Assert (Present (Itype));
20504 pragma Assert (Nkind (Itype) in N_Entity);
20505 pragma Assert (Is_Itype (Itype));
20506
20507 NCT_Tables_In_Use := True;
20508
20509 -- It is not possible to sanity check the NCT_Pendint_Itypes table
20510 -- directly because a single node may act as the associated node for
20511 -- multiple itypes.
20512
20513 Itypes := NCT_Pending_Itypes.Get (Assoc_Nod);
20514
20515 if No (Itypes) then
20516 Itypes := New_Elmt_List;
20517 NCT_Pending_Itypes.Set (Assoc_Nod, Itypes);
20518 end if;
20519
20520 -- Establish the mapping
20521
20522 -- Assoc_Nod -> (Itype, ...)
20523
20524 -- Avoid inserting the same itype multiple times. This involves a
20525 -- linear search, however the set of itypes with the same associated
20526 -- node is very small.
20527
20528 Append_Unique_Elmt (Itype, Itypes);
20529 end Add_Pending_Itype;
20530
20531 ----------------------
20532 -- Build_NCT_Tables --
20533 ----------------------
20534
20535 procedure Build_NCT_Tables (Entity_Map : Elist_Id) is
20536 Elmt : Elmt_Id;
20537 Old_Id : Entity_Id;
20538 New_Id : Entity_Id;
20539
20540 begin
20541 -- Nothing to do when there is no entity map
20542
20543 if No (Entity_Map) then
20544 return;
20545 end if;
20546
20547 Elmt := First_Elmt (Entity_Map);
20548 while Present (Elmt) loop
20549
20550 -- Extract the (Old_Id, New_Id) pair from the entity map
20551
20552 Old_Id := Node (Elmt);
20553 Next_Elmt (Elmt);
20554
20555 New_Id := Node (Elmt);
20556 Next_Elmt (Elmt);
20557
20558 -- Establish the following mapping within table NCT_New_Entities
20559
20560 -- Old_Id -> New_Id
20561
20562 Add_New_Entity (Old_Id, New_Id);
20563
20564 -- Establish the following mapping within table NCT_Pending_Itypes
20565 -- when the new entity is an itype.
20566
20567 -- Assoc_Nod -> (New_Id, ...)
20568
20569 -- IMPORTANT: the associated node is that of the old itype because
20570 -- the node will be replicated in Phase 2.
20571
20572 if Is_Itype (Old_Id) then
20573 Add_Pending_Itype
20574 (Assoc_Nod => Associated_Node_For_Itype (Old_Id),
20575 Itype => New_Id);
20576 end if;
20577 end loop;
20578 end Build_NCT_Tables;
20579
20580 ------------------------------------
20581 -- Copy_Any_Node_With_Replacement --
20582 ------------------------------------
20583
20584 function Copy_Any_Node_With_Replacement
20585 (N : Node_Or_Entity_Id) return Node_Or_Entity_Id
20586 is
20587 begin
20588 if Nkind (N) in N_Entity then
20589 return Corresponding_Entity (N);
20590 else
20591 return Copy_Node_With_Replacement (N);
20592 end if;
20593 end Copy_Any_Node_With_Replacement;
20594
20595 ---------------------------------
20596 -- Copy_Elist_With_Replacement --
20597 ---------------------------------
20598
20599 function Copy_Elist_With_Replacement (List : Elist_Id) return Elist_Id is
20600 Elmt : Elmt_Id;
20601 Result : Elist_Id;
20602
20603 begin
20604 -- Copy the contents of the old list. Note that the list itself may
20605 -- be empty, in which case the routine returns a new empty list. This
20606 -- avoids sharing lists between subtrees. The element of an entity
20607 -- list could be an entity or a node, hence the invocation of routine
20608 -- Copy_Any_Node_With_Replacement.
20609
20610 if Present (List) then
20611 Result := New_Elmt_List;
20612
20613 Elmt := First_Elmt (List);
20614 while Present (Elmt) loop
20615 Append_Elmt
20616 (Copy_Any_Node_With_Replacement (Node (Elmt)), Result);
20617
20618 Next_Elmt (Elmt);
20619 end loop;
20620
20621 -- Otherwise the list does not exist
20622
20623 else
20624 Result := No_Elist;
20625 end if;
20626
20627 return Result;
20628 end Copy_Elist_With_Replacement;
20629
20630 ---------------------------------
20631 -- Copy_Field_With_Replacement --
20632 ---------------------------------
20633
20634 function Copy_Field_With_Replacement
20635 (Field : Union_Id;
20636 Old_Par : Node_Id := Empty;
20637 New_Par : Node_Id := Empty;
20638 Semantic : Boolean := False) return Union_Id
20639 is
20640 begin
20641 -- The field is empty
20642
20643 if Field = Union_Id (Empty) then
20644 return Field;
20645
20646 -- The field is an entity/itype/node
20647
20648 elsif Field in Node_Range then
20649 declare
20650 Old_N : constant Node_Id := Node_Id (Field);
20651 Syntactic : constant Boolean := Parent (Old_N) = Old_Par;
20652
20653 New_N : Node_Id;
20654
20655 begin
20656 -- The field is an entity/itype
20657
20658 if Nkind (Old_N) in N_Entity then
20659
20660 -- An entity/itype is always replicated
20661
20662 New_N := Corresponding_Entity (Old_N);
20663
20664 -- Update the parent pointer when the entity is a syntactic
20665 -- field. Note that itypes do not have parent pointers.
20666
20667 if Syntactic and then New_N /= Old_N then
20668 Set_Parent (New_N, New_Par);
20669 end if;
20670
20671 -- The field is a node
20672
20673 else
20674 -- A node is replicated when it is either a syntactic field
20675 -- or when the caller treats it as a semantic attribute.
20676
20677 if Syntactic or else Semantic then
20678 New_N := Copy_Node_With_Replacement (Old_N);
20679
20680 -- Update the parent pointer when the node is a syntactic
20681 -- field.
20682
20683 if Syntactic and then New_N /= Old_N then
20684 Set_Parent (New_N, New_Par);
20685 end if;
20686
20687 -- Otherwise the node is returned unchanged
20688
20689 else
20690 New_N := Old_N;
20691 end if;
20692 end if;
20693
20694 return Union_Id (New_N);
20695 end;
20696
20697 -- The field is an entity list
20698
20699 elsif Field in Elist_Range then
20700 return Union_Id (Copy_Elist_With_Replacement (Elist_Id (Field)));
20701
20702 -- The field is a syntactic list
20703
20704 elsif Field in List_Range then
20705 declare
20706 Old_List : constant List_Id := List_Id (Field);
20707 Syntactic : constant Boolean := Parent (Old_List) = Old_Par;
20708
20709 New_List : List_Id;
20710
20711 begin
20712 -- A list is replicated when it is either a syntactic field or
20713 -- when the caller treats it as a semantic attribute.
20714
20715 if Syntactic or else Semantic then
20716 New_List := Copy_List_With_Replacement (Old_List);
20717
20718 -- Update the parent pointer when the list is a syntactic
20719 -- field.
20720
20721 if Syntactic and then New_List /= Old_List then
20722 Set_Parent (New_List, New_Par);
20723 end if;
20724
20725 -- Otherwise the list is returned unchanged
20726
20727 else
20728 New_List := Old_List;
20729 end if;
20730
20731 return Union_Id (New_List);
20732 end;
20733
20734 -- Otherwise the field denotes an attribute that does not need to be
20735 -- replicated (Chars, literals, etc).
20736
20737 else
20738 return Field;
20739 end if;
20740 end Copy_Field_With_Replacement;
20741
20742 --------------------------------
20743 -- Copy_List_With_Replacement --
20744 --------------------------------
20745
20746 function Copy_List_With_Replacement (List : List_Id) return List_Id is
20747 Elmt : Node_Id;
20748 Result : List_Id;
20749
20750 begin
20751 -- Copy the contents of the old list. Note that the list itself may
20752 -- be empty, in which case the routine returns a new empty list. This
20753 -- avoids sharing lists between subtrees. The element of a syntactic
20754 -- list is always a node, never an entity or itype, hence the call to
20755 -- routine Copy_Node_With_Replacement.
20756
20757 if Present (List) then
20758 Result := New_List;
20759
20760 Elmt := First (List);
20761 while Present (Elmt) loop
20762 Append (Copy_Node_With_Replacement (Elmt), Result);
20763
20764 Next (Elmt);
20765 end loop;
20766
20767 -- Otherwise the list does not exist
20768
20769 else
20770 Result := No_List;
20771 end if;
20772
20773 return Result;
20774 end Copy_List_With_Replacement;
20775
20776 --------------------------------
20777 -- Copy_Node_With_Replacement --
20778 --------------------------------
20779
20780 function Copy_Node_With_Replacement (N : Node_Id) return Node_Id is
20781 Result : Node_Id;
20782
20783 begin
20784 -- Assume that the node must be returned unchanged
20785
20786 Result := N;
20787
20788 if N > Empty_Or_Error then
20789 pragma Assert (Nkind (N) not in N_Entity);
20790
20791 Result := New_Copy (N);
20792
20793 Set_Field1 (Result,
20794 Copy_Field_With_Replacement
20795 (Field => Field1 (Result),
20796 Old_Par => N,
20797 New_Par => Result));
20798
20799 Set_Field2 (Result,
20800 Copy_Field_With_Replacement
20801 (Field => Field2 (Result),
20802 Old_Par => N,
20803 New_Par => Result));
20804
20805 Set_Field3 (Result,
20806 Copy_Field_With_Replacement
20807 (Field => Field3 (Result),
20808 Old_Par => N,
20809 New_Par => Result));
20810
20811 Set_Field4 (Result,
20812 Copy_Field_With_Replacement
20813 (Field => Field4 (Result),
20814 Old_Par => N,
20815 New_Par => Result));
20816
20817 Set_Field5 (Result,
20818 Copy_Field_With_Replacement
20819 (Field => Field5 (Result),
20820 Old_Par => N,
20821 New_Par => Result));
20822
20823 -- Update the Comes_From_Source and Sloc attributes of the node
20824 -- in case the caller has supplied new values.
20825
20826 Update_CFS_Sloc (Result);
20827
20828 -- Update the Associated_Node_For_Itype attribute of all itypes
20829 -- created during Phase 1 whose associated node is N. As a result
20830 -- the Associated_Node_For_Itype refers to the replicated node.
20831 -- No action needs to be taken when the Associated_Node_For_Itype
20832 -- refers to an entity because this was already handled during
20833 -- Phase 1, in Visit_Itype.
20834
20835 Update_Pending_Itypes
20836 (Old_Assoc => N,
20837 New_Assoc => Result);
20838
20839 -- Update the First/Next_Named_Association chain for a replicated
20840 -- call.
20841
20842 if Nkind_In (N, N_Entry_Call_Statement,
20843 N_Function_Call,
20844 N_Procedure_Call_Statement)
20845 then
20846 Update_Named_Associations
20847 (Old_Call => N,
20848 New_Call => Result);
20849
20850 -- Update the Renamed_Object attribute of a replicated object
20851 -- declaration.
20852
20853 elsif Nkind (N) = N_Object_Renaming_Declaration then
20854 Set_Renamed_Object (Defining_Entity (Result), Name (Result));
20855
20856 -- Update the First_Real_Statement attribute of a replicated
20857 -- handled sequence of statements.
20858
20859 elsif Nkind (N) = N_Handled_Sequence_Of_Statements then
20860 Update_First_Real_Statement
20861 (Old_HSS => N,
20862 New_HSS => Result);
20863
20864 -- Update the Chars attribute of identifiers
20865
20866 elsif Nkind (N) = N_Identifier then
20867
20868 -- The Entity field of identifiers that denote aspects is used
20869 -- to store arbitrary expressions (and hence we must check that
20870 -- they reference an actual entity before copying their Chars
20871 -- value).
20872
20873 if Present (Entity (Result))
20874 and then Nkind (Entity (Result)) in N_Entity
20875 then
20876 Set_Chars (Result, Chars (Entity (Result)));
20877 end if;
20878 end if;
20879 end if;
20880
20881 return Result;
20882 end Copy_Node_With_Replacement;
20883
20884 --------------------------
20885 -- Corresponding_Entity --
20886 --------------------------
20887
20888 function Corresponding_Entity (Id : Entity_Id) return Entity_Id is
20889 New_Id : Entity_Id;
20890 Result : Entity_Id;
20891
20892 begin
20893 -- Assume that the entity must be returned unchanged
20894
20895 Result := Id;
20896
20897 if Id > Empty_Or_Error then
20898 pragma Assert (Nkind (Id) in N_Entity);
20899
20900 -- Determine whether the entity has a corresponding new entity
20901 -- generated during Phase 1 and if it does, use it.
20902
20903 if NCT_Tables_In_Use then
20904 New_Id := NCT_New_Entities.Get (Id);
20905
20906 if Present (New_Id) then
20907 Result := New_Id;
20908 end if;
20909 end if;
20910 end if;
20911
20912 return Result;
20913 end Corresponding_Entity;
20914
20915 -------------------
20916 -- In_Entity_Map --
20917 -------------------
20918
20919 function In_Entity_Map
20920 (Id : Entity_Id;
20921 Entity_Map : Elist_Id) return Boolean
20922 is
20923 Elmt : Elmt_Id;
20924 Old_Id : Entity_Id;
20925
20926 begin
20927 -- The entity map contains pairs (Old_Id, New_Id). The advancement
20928 -- step always skips the New_Id portion of the pair.
20929
20930 if Present (Entity_Map) then
20931 Elmt := First_Elmt (Entity_Map);
20932 while Present (Elmt) loop
20933 Old_Id := Node (Elmt);
20934
20935 if Old_Id = Id then
20936 return True;
20937 end if;
20938
20939 Next_Elmt (Elmt);
20940 Next_Elmt (Elmt);
20941 end loop;
20942 end if;
20943
20944 return False;
20945 end In_Entity_Map;
20946
20947 ---------------------
20948 -- Update_CFS_Sloc --
20949 ---------------------
20950
20951 procedure Update_CFS_Sloc (N : Node_Or_Entity_Id) is
20952 begin
20953 -- A new source location defaults the Comes_From_Source attribute
20954
20955 if New_Sloc /= No_Location then
20956 Set_Comes_From_Source (N, Default_Node.Comes_From_Source);
20957 Set_Sloc (N, New_Sloc);
20958 end if;
20959 end Update_CFS_Sloc;
20960
20961 ---------------------------------
20962 -- Update_First_Real_Statement --
20963 ---------------------------------
20964
20965 procedure Update_First_Real_Statement
20966 (Old_HSS : Node_Id;
20967 New_HSS : Node_Id)
20968 is
20969 Old_First_Stmt : constant Node_Id := First_Real_Statement (Old_HSS);
20970
20971 New_Stmt : Node_Id;
20972 Old_Stmt : Node_Id;
20973
20974 begin
20975 -- Recreate the First_Real_Statement attribute of a handled sequence
20976 -- of statements by traversing the statement lists of both sequences
20977 -- in parallel.
20978
20979 if Present (Old_First_Stmt) then
20980 New_Stmt := First (Statements (New_HSS));
20981 Old_Stmt := First (Statements (Old_HSS));
20982 while Present (Old_Stmt) and then Old_Stmt /= Old_First_Stmt loop
20983 Next (New_Stmt);
20984 Next (Old_Stmt);
20985 end loop;
20986
20987 pragma Assert (Present (New_Stmt));
20988 pragma Assert (Present (Old_Stmt));
20989
20990 Set_First_Real_Statement (New_HSS, New_Stmt);
20991 end if;
20992 end Update_First_Real_Statement;
20993
20994 -------------------------------
20995 -- Update_Named_Associations --
20996 -------------------------------
20997
20998 procedure Update_Named_Associations
20999 (Old_Call : Node_Id;
21000 New_Call : Node_Id)
21001 is
21002 New_Act : Node_Id;
21003 New_Next : Node_Id;
21004 Old_Act : Node_Id;
21005 Old_Next : Node_Id;
21006
21007 begin
21008 if No (First_Named_Actual (Old_Call)) then
21009 return;
21010 end if;
21011
21012 -- Recreate the First/Next_Named_Actual chain of a call by traversing
21013 -- the chains of both the old and new calls in parallel.
21014
21015 New_Act := First (Parameter_Associations (New_Call));
21016 Old_Act := First (Parameter_Associations (Old_Call));
21017 while Present (Old_Act) loop
21018 if Nkind (Old_Act) = N_Parameter_Association
21019 and then Explicit_Actual_Parameter (Old_Act)
21020 = First_Named_Actual (Old_Call)
21021 then
21022 Set_First_Named_Actual (New_Call,
21023 Explicit_Actual_Parameter (New_Act));
21024 end if;
21025
21026 if Nkind (Old_Act) = N_Parameter_Association
21027 and then Present (Next_Named_Actual (Old_Act))
21028 then
21029 -- Scan the actual parameter list to find the next suitable
21030 -- named actual. Note that the list may be out of order.
21031
21032 New_Next := First (Parameter_Associations (New_Call));
21033 Old_Next := First (Parameter_Associations (Old_Call));
21034 while Nkind (Old_Next) /= N_Parameter_Association
21035 or else Explicit_Actual_Parameter (Old_Next) /=
21036 Next_Named_Actual (Old_Act)
21037 loop
21038 Next (New_Next);
21039 Next (Old_Next);
21040 end loop;
21041
21042 Set_Next_Named_Actual (New_Act,
21043 Explicit_Actual_Parameter (New_Next));
21044 end if;
21045
21046 Next (New_Act);
21047 Next (Old_Act);
21048 end loop;
21049 end Update_Named_Associations;
21050
21051 -------------------------
21052 -- Update_New_Entities --
21053 -------------------------
21054
21055 procedure Update_New_Entities (Entity_Map : Elist_Id) is
21056 New_Id : Entity_Id := Empty;
21057 Old_Id : Entity_Id := Empty;
21058
21059 begin
21060 if NCT_Tables_In_Use then
21061 NCT_New_Entities.Get_First (Old_Id, New_Id);
21062
21063 -- Update the semantic fields of all new entities created during
21064 -- Phase 1 which were not supplied via an entity map.
21065 -- ??? Is there a better way of distinguishing those?
21066
21067 while Present (Old_Id) and then Present (New_Id) loop
21068 if not (Present (Entity_Map)
21069 and then In_Entity_Map (Old_Id, Entity_Map))
21070 then
21071 Update_Semantic_Fields (New_Id);
21072 end if;
21073
21074 NCT_New_Entities.Get_Next (Old_Id, New_Id);
21075 end loop;
21076 end if;
21077 end Update_New_Entities;
21078
21079 ---------------------------
21080 -- Update_Pending_Itypes --
21081 ---------------------------
21082
21083 procedure Update_Pending_Itypes
21084 (Old_Assoc : Node_Id;
21085 New_Assoc : Node_Id)
21086 is
21087 Item : Elmt_Id;
21088 Itypes : Elist_Id;
21089
21090 begin
21091 if NCT_Tables_In_Use then
21092 Itypes := NCT_Pending_Itypes.Get (Old_Assoc);
21093
21094 -- Update the Associated_Node_For_Itype attribute for all itypes
21095 -- which originally refer to Old_Assoc to designate New_Assoc.
21096
21097 if Present (Itypes) then
21098 Item := First_Elmt (Itypes);
21099 while Present (Item) loop
21100 Set_Associated_Node_For_Itype (Node (Item), New_Assoc);
21101
21102 Next_Elmt (Item);
21103 end loop;
21104 end if;
21105 end if;
21106 end Update_Pending_Itypes;
21107
21108 ----------------------------
21109 -- Update_Semantic_Fields --
21110 ----------------------------
21111
21112 procedure Update_Semantic_Fields (Id : Entity_Id) is
21113 begin
21114 -- Discriminant_Constraint
21115
21116 if Is_Type (Id) and then Has_Discriminants (Base_Type (Id)) then
21117 Set_Discriminant_Constraint (Id, Elist_Id (
21118 Copy_Field_With_Replacement
21119 (Field => Union_Id (Discriminant_Constraint (Id)),
21120 Semantic => True)));
21121 end if;
21122
21123 -- Etype
21124
21125 Set_Etype (Id, Node_Id (
21126 Copy_Field_With_Replacement
21127 (Field => Union_Id (Etype (Id)),
21128 Semantic => True)));
21129
21130 -- First_Index
21131 -- Packed_Array_Impl_Type
21132
21133 if Is_Array_Type (Id) then
21134 if Present (First_Index (Id)) then
21135 Set_First_Index (Id, First (List_Id (
21136 Copy_Field_With_Replacement
21137 (Field => Union_Id (List_Containing (First_Index (Id))),
21138 Semantic => True))));
21139 end if;
21140
21141 if Is_Packed (Id) then
21142 Set_Packed_Array_Impl_Type (Id, Node_Id (
21143 Copy_Field_With_Replacement
21144 (Field => Union_Id (Packed_Array_Impl_Type (Id)),
21145 Semantic => True)));
21146 end if;
21147 end if;
21148
21149 -- Prev_Entity
21150
21151 Set_Prev_Entity (Id, Node_Id (
21152 Copy_Field_With_Replacement
21153 (Field => Union_Id (Prev_Entity (Id)),
21154 Semantic => True)));
21155
21156 -- Next_Entity
21157
21158 Set_Next_Entity (Id, Node_Id (
21159 Copy_Field_With_Replacement
21160 (Field => Union_Id (Next_Entity (Id)),
21161 Semantic => True)));
21162
21163 -- Scalar_Range
21164
21165 if Is_Discrete_Type (Id) then
21166 Set_Scalar_Range (Id, Node_Id (
21167 Copy_Field_With_Replacement
21168 (Field => Union_Id (Scalar_Range (Id)),
21169 Semantic => True)));
21170 end if;
21171
21172 -- Scope
21173
21174 -- Update the scope when the caller specified an explicit one
21175
21176 if Present (New_Scope) then
21177 Set_Scope (Id, New_Scope);
21178 else
21179 Set_Scope (Id, Node_Id (
21180 Copy_Field_With_Replacement
21181 (Field => Union_Id (Scope (Id)),
21182 Semantic => True)));
21183 end if;
21184 end Update_Semantic_Fields;
21185
21186 --------------------
21187 -- Visit_Any_Node --
21188 --------------------
21189
21190 procedure Visit_Any_Node (N : Node_Or_Entity_Id) is
21191 begin
21192 if Nkind (N) in N_Entity then
21193 if Is_Itype (N) then
21194 Visit_Itype (N);
21195 else
21196 Visit_Entity (N);
21197 end if;
21198 else
21199 Visit_Node (N);
21200 end if;
21201 end Visit_Any_Node;
21202
21203 -----------------
21204 -- Visit_Elist --
21205 -----------------
21206
21207 procedure Visit_Elist (List : Elist_Id) is
21208 Elmt : Elmt_Id;
21209
21210 begin
21211 -- The element of an entity list could be an entity, itype, or a
21212 -- node, hence the call to Visit_Any_Node.
21213
21214 if Present (List) then
21215 Elmt := First_Elmt (List);
21216 while Present (Elmt) loop
21217 Visit_Any_Node (Node (Elmt));
21218
21219 Next_Elmt (Elmt);
21220 end loop;
21221 end if;
21222 end Visit_Elist;
21223
21224 ------------------
21225 -- Visit_Entity --
21226 ------------------
21227
21228 procedure Visit_Entity (Id : Entity_Id) is
21229 New_Id : Entity_Id;
21230
21231 begin
21232 pragma Assert (Nkind (Id) in N_Entity);
21233 pragma Assert (not Is_Itype (Id));
21234
21235 -- Nothing to do when the entity is not defined in the Actions list
21236 -- of an N_Expression_With_Actions node.
21237
21238 if EWA_Level = 0 then
21239 return;
21240
21241 -- Nothing to do when the entity is defined in a scoping construct
21242 -- within an N_Expression_With_Actions node, unless the caller has
21243 -- requested their replication.
21244
21245 -- ??? should this restriction be eliminated?
21246
21247 elsif EWA_Inner_Scope_Level > 0 and then not Scopes_In_EWA_OK then
21248 return;
21249
21250 -- Nothing to do when the entity does not denote a construct that
21251 -- may appear within an N_Expression_With_Actions node. Relaxing
21252 -- this restriction leads to a performance penalty.
21253
21254 -- ??? this list is flaky, and may hide dormant bugs
21255 -- Should functions be included???
21256
21257 -- Loop parameters appear within quantified expressions and contain
21258 -- an entity declaration that must be replaced when the expander is
21259 -- active if the expression has been preanalyzed or analyzed.
21260
21261 elsif not Ekind_In (Id, E_Block,
21262 E_Constant,
21263 E_Label,
21264 E_Loop_Parameter,
21265 E_Procedure,
21266 E_Variable)
21267 and then not Is_Type (Id)
21268 then
21269 return;
21270
21271 elsif Ekind (Id) = E_Loop_Parameter
21272 and then No (Etype (Condition (Parent (Parent (Id)))))
21273 then
21274 return;
21275
21276 -- Nothing to do when the entity was already visited
21277
21278 elsif NCT_Tables_In_Use
21279 and then Present (NCT_New_Entities.Get (Id))
21280 then
21281 return;
21282
21283 -- Nothing to do when the declaration node of the entity is not in
21284 -- the subtree being replicated.
21285
21286 elsif not In_Subtree
21287 (N => Declaration_Node (Id),
21288 Root => Source)
21289 then
21290 return;
21291 end if;
21292
21293 -- Create a new entity by directly copying the old entity. This
21294 -- action causes all attributes of the old entity to be inherited.
21295
21296 New_Id := New_Copy (Id);
21297
21298 -- Create a new name for the new entity because the back end needs
21299 -- distinct names for debugging purposes.
21300
21301 Set_Chars (New_Id, New_Internal_Name ('T'));
21302
21303 -- Update the Comes_From_Source and Sloc attributes of the entity in
21304 -- case the caller has supplied new values.
21305
21306 Update_CFS_Sloc (New_Id);
21307
21308 -- Establish the following mapping within table NCT_New_Entities:
21309
21310 -- Id -> New_Id
21311
21312 Add_New_Entity (Id, New_Id);
21313
21314 -- Deal with the semantic fields of entities. The fields are visited
21315 -- because they may mention entities which reside within the subtree
21316 -- being copied.
21317
21318 Visit_Semantic_Fields (Id);
21319 end Visit_Entity;
21320
21321 -----------------
21322 -- Visit_Field --
21323 -----------------
21324
21325 procedure Visit_Field
21326 (Field : Union_Id;
21327 Par_Nod : Node_Id := Empty;
21328 Semantic : Boolean := False)
21329 is
21330 begin
21331 -- The field is empty
21332
21333 if Field = Union_Id (Empty) then
21334 return;
21335
21336 -- The field is an entity/itype/node
21337
21338 elsif Field in Node_Range then
21339 declare
21340 N : constant Node_Id := Node_Id (Field);
21341
21342 begin
21343 -- The field is an entity/itype
21344
21345 if Nkind (N) in N_Entity then
21346
21347 -- Itypes are always visited
21348
21349 if Is_Itype (N) then
21350 Visit_Itype (N);
21351
21352 -- An entity is visited when it is either a syntactic field
21353 -- or when the caller treats it as a semantic attribute.
21354
21355 elsif Parent (N) = Par_Nod or else Semantic then
21356 Visit_Entity (N);
21357 end if;
21358
21359 -- The field is a node
21360
21361 else
21362 -- A node is visited when it is either a syntactic field or
21363 -- when the caller treats it as a semantic attribute.
21364
21365 if Parent (N) = Par_Nod or else Semantic then
21366 Visit_Node (N);
21367 end if;
21368 end if;
21369 end;
21370
21371 -- The field is an entity list
21372
21373 elsif Field in Elist_Range then
21374 Visit_Elist (Elist_Id (Field));
21375
21376 -- The field is a syntax list
21377
21378 elsif Field in List_Range then
21379 declare
21380 List : constant List_Id := List_Id (Field);
21381
21382 begin
21383 -- A syntax list is visited when it is either a syntactic field
21384 -- or when the caller treats it as a semantic attribute.
21385
21386 if Parent (List) = Par_Nod or else Semantic then
21387 Visit_List (List);
21388 end if;
21389 end;
21390
21391 -- Otherwise the field denotes information which does not need to be
21392 -- visited (chars, literals, etc.).
21393
21394 else
21395 null;
21396 end if;
21397 end Visit_Field;
21398
21399 -----------------
21400 -- Visit_Itype --
21401 -----------------
21402
21403 procedure Visit_Itype (Itype : Entity_Id) is
21404 New_Assoc : Node_Id;
21405 New_Itype : Entity_Id;
21406 Old_Assoc : Node_Id;
21407
21408 begin
21409 pragma Assert (Nkind (Itype) in N_Entity);
21410 pragma Assert (Is_Itype (Itype));
21411
21412 -- Itypes that describe the designated type of access to subprograms
21413 -- have the structure of subprogram declarations, with signatures,
21414 -- etc. Either we duplicate the signatures completely, or choose to
21415 -- share such itypes, which is fine because their elaboration will
21416 -- have no side effects.
21417
21418 if Ekind (Itype) = E_Subprogram_Type then
21419 return;
21420
21421 -- Nothing to do if the itype was already visited
21422
21423 elsif NCT_Tables_In_Use
21424 and then Present (NCT_New_Entities.Get (Itype))
21425 then
21426 return;
21427
21428 -- Nothing to do if the associated node of the itype is not within
21429 -- the subtree being replicated.
21430
21431 elsif not In_Subtree
21432 (N => Associated_Node_For_Itype (Itype),
21433 Root => Source)
21434 then
21435 return;
21436 end if;
21437
21438 -- Create a new itype by directly copying the old itype. This action
21439 -- causes all attributes of the old itype to be inherited.
21440
21441 New_Itype := New_Copy (Itype);
21442
21443 -- Create a new name for the new itype because the back end requires
21444 -- distinct names for debugging purposes.
21445
21446 Set_Chars (New_Itype, New_Internal_Name ('T'));
21447
21448 -- Update the Comes_From_Source and Sloc attributes of the itype in
21449 -- case the caller has supplied new values.
21450
21451 Update_CFS_Sloc (New_Itype);
21452
21453 -- Establish the following mapping within table NCT_New_Entities:
21454
21455 -- Itype -> New_Itype
21456
21457 Add_New_Entity (Itype, New_Itype);
21458
21459 -- The new itype must be unfrozen because the resulting subtree may
21460 -- be inserted anywhere and cause an earlier or later freezing.
21461
21462 if Present (Freeze_Node (New_Itype)) then
21463 Set_Freeze_Node (New_Itype, Empty);
21464 Set_Is_Frozen (New_Itype, False);
21465 end if;
21466
21467 -- If a record subtype is simply copied, the entity list will be
21468 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
21469 -- ??? What does this do?
21470
21471 if Ekind_In (Itype, E_Class_Wide_Subtype, E_Record_Subtype) then
21472 Set_Cloned_Subtype (New_Itype, Itype);
21473 end if;
21474
21475 -- The associated node may denote an entity, in which case it may
21476 -- already have a new corresponding entity created during a prior
21477 -- call to Visit_Entity or Visit_Itype for the same subtree.
21478
21479 -- Given
21480 -- Old_Assoc ---------> New_Assoc
21481
21482 -- Created by Visit_Itype
21483 -- Itype -------------> New_Itype
21484 -- ANFI = Old_Assoc ANFI = Old_Assoc < must be updated
21485
21486 -- In the example above, Old_Assoc is an arbitrary entity that was
21487 -- already visited for the same subtree and has a corresponding new
21488 -- entity New_Assoc. Old_Assoc was inherited by New_Itype by virtue
21489 -- of copying entities, however it must be updated to New_Assoc.
21490
21491 Old_Assoc := Associated_Node_For_Itype (Itype);
21492
21493 if Nkind (Old_Assoc) in N_Entity then
21494 if NCT_Tables_In_Use then
21495 New_Assoc := NCT_New_Entities.Get (Old_Assoc);
21496
21497 if Present (New_Assoc) then
21498 Set_Associated_Node_For_Itype (New_Itype, New_Assoc);
21499 end if;
21500 end if;
21501
21502 -- Otherwise the associated node denotes a node. Postpone the update
21503 -- until Phase 2 when the node is replicated. Establish the following
21504 -- mapping within table NCT_Pending_Itypes:
21505
21506 -- Old_Assoc -> (New_Type, ...)
21507
21508 else
21509 Add_Pending_Itype (Old_Assoc, New_Itype);
21510 end if;
21511
21512 -- Deal with the semantic fields of itypes. The fields are visited
21513 -- because they may mention entities that reside within the subtree
21514 -- being copied.
21515
21516 Visit_Semantic_Fields (Itype);
21517 end Visit_Itype;
21518
21519 ----------------
21520 -- Visit_List --
21521 ----------------
21522
21523 procedure Visit_List (List : List_Id) is
21524 Elmt : Node_Id;
21525
21526 begin
21527 -- Note that the element of a syntactic list is always a node, never
21528 -- an entity or itype, hence the call to Visit_Node.
21529
21530 if Present (List) then
21531 Elmt := First (List);
21532 while Present (Elmt) loop
21533 Visit_Node (Elmt);
21534
21535 Next (Elmt);
21536 end loop;
21537 end if;
21538 end Visit_List;
21539
21540 ----------------
21541 -- Visit_Node --
21542 ----------------
21543
21544 procedure Visit_Node (N : Node_Or_Entity_Id) is
21545 begin
21546 pragma Assert (Nkind (N) not in N_Entity);
21547
21548 -- If the node is a quantified expression and expander is active,
21549 -- it contains an implicit declaration that may require a new entity
21550 -- when the condition has already been (pre)analyzed.
21551
21552 if Nkind (N) = N_Expression_With_Actions
21553 or else
21554 (Nkind (N) = N_Quantified_Expression and then Expander_Active)
21555 then
21556 EWA_Level := EWA_Level + 1;
21557
21558 elsif EWA_Level > 0
21559 and then Nkind_In (N, N_Block_Statement,
21560 N_Subprogram_Body,
21561 N_Subprogram_Declaration)
21562 then
21563 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level + 1;
21564 end if;
21565
21566 Visit_Field
21567 (Field => Field1 (N),
21568 Par_Nod => N);
21569
21570 Visit_Field
21571 (Field => Field2 (N),
21572 Par_Nod => N);
21573
21574 Visit_Field
21575 (Field => Field3 (N),
21576 Par_Nod => N);
21577
21578 Visit_Field
21579 (Field => Field4 (N),
21580 Par_Nod => N);
21581
21582 Visit_Field
21583 (Field => Field5 (N),
21584 Par_Nod => N);
21585
21586 if EWA_Level > 0
21587 and then Nkind_In (N, N_Block_Statement,
21588 N_Subprogram_Body,
21589 N_Subprogram_Declaration)
21590 then
21591 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level - 1;
21592
21593 elsif Nkind (N) = N_Expression_With_Actions then
21594 EWA_Level := EWA_Level - 1;
21595 end if;
21596 end Visit_Node;
21597
21598 ---------------------------
21599 -- Visit_Semantic_Fields --
21600 ---------------------------
21601
21602 procedure Visit_Semantic_Fields (Id : Entity_Id) is
21603 begin
21604 pragma Assert (Nkind (Id) in N_Entity);
21605
21606 -- Discriminant_Constraint
21607
21608 if Is_Type (Id) and then Has_Discriminants (Base_Type (Id)) then
21609 Visit_Field
21610 (Field => Union_Id (Discriminant_Constraint (Id)),
21611 Semantic => True);
21612 end if;
21613
21614 -- Etype
21615
21616 Visit_Field
21617 (Field => Union_Id (Etype (Id)),
21618 Semantic => True);
21619
21620 -- First_Index
21621 -- Packed_Array_Impl_Type
21622
21623 if Is_Array_Type (Id) then
21624 if Present (First_Index (Id)) then
21625 Visit_Field
21626 (Field => Union_Id (List_Containing (First_Index (Id))),
21627 Semantic => True);
21628 end if;
21629
21630 if Is_Packed (Id) then
21631 Visit_Field
21632 (Field => Union_Id (Packed_Array_Impl_Type (Id)),
21633 Semantic => True);
21634 end if;
21635 end if;
21636
21637 -- Scalar_Range
21638
21639 if Is_Discrete_Type (Id) then
21640 Visit_Field
21641 (Field => Union_Id (Scalar_Range (Id)),
21642 Semantic => True);
21643 end if;
21644 end Visit_Semantic_Fields;
21645
21646 -- Start of processing for New_Copy_Tree
21647
21648 begin
21649 -- Routine New_Copy_Tree performs a deep copy of a subtree by creating
21650 -- shallow copies for each node within, and then updating the child and
21651 -- parent pointers accordingly. This process is straightforward, however
21652 -- the routine must deal with the following complications:
21653
21654 -- * Entities defined within N_Expression_With_Actions nodes must be
21655 -- replicated rather than shared to avoid introducing two identical
21656 -- symbols within the same scope. Note that no other expression can
21657 -- currently define entities.
21658
21659 -- do
21660 -- Source_Low : ...;
21661 -- Source_High : ...;
21662
21663 -- <reference to Source_Low>
21664 -- <reference to Source_High>
21665 -- in ... end;
21666
21667 -- New_Copy_Tree handles this case by first creating new entities
21668 -- and then updating all existing references to point to these new
21669 -- entities.
21670
21671 -- do
21672 -- New_Low : ...;
21673 -- New_High : ...;
21674
21675 -- <reference to New_Low>
21676 -- <reference to New_High>
21677 -- in ... end;
21678
21679 -- * Itypes defined within the subtree must be replicated to avoid any
21680 -- dependencies on invalid or inaccessible data.
21681
21682 -- subtype Source_Itype is ... range Source_Low .. Source_High;
21683
21684 -- New_Copy_Tree handles this case by first creating a new itype in
21685 -- the same fashion as entities, and then updating various relevant
21686 -- constraints.
21687
21688 -- subtype New_Itype is ... range New_Low .. New_High;
21689
21690 -- * The Associated_Node_For_Itype field of itypes must be updated to
21691 -- reference the proper replicated entity or node.
21692
21693 -- * Semantic fields of entities such as Etype and Scope must be
21694 -- updated to reference the proper replicated entities.
21695
21696 -- * Semantic fields of nodes such as First_Real_Statement must be
21697 -- updated to reference the proper replicated nodes.
21698
21699 -- Finally, quantified expressions contain an implicit delaration for
21700 -- the bound variable. Given that quantified expressions appearing
21701 -- in contracts are copied to create pragmas and eventually checking
21702 -- procedures, a new bound variable must be created for each copy, to
21703 -- prevent multiple declarations of the same symbol.
21704
21705 -- To meet all these demands, routine New_Copy_Tree is split into two
21706 -- phases.
21707
21708 -- Phase 1 traverses the tree in order to locate entities and itypes
21709 -- defined within the subtree. New entities are generated and saved in
21710 -- table NCT_New_Entities. The semantic fields of all new entities and
21711 -- itypes are then updated accordingly.
21712
21713 -- Phase 2 traverses the tree in order to replicate each node. Various
21714 -- semantic fields of nodes and entities are updated accordingly.
21715
21716 -- Preparatory phase. Clear the contents of tables NCT_New_Entities and
21717 -- NCT_Pending_Itypes in case a previous call to New_Copy_Tree left some
21718 -- data inside.
21719
21720 if NCT_Tables_In_Use then
21721 NCT_Tables_In_Use := False;
21722
21723 NCT_New_Entities.Reset;
21724 NCT_Pending_Itypes.Reset;
21725 end if;
21726
21727 -- Populate tables NCT_New_Entities and NCT_Pending_Itypes with data
21728 -- supplied by a linear entity map. The tables offer faster access to
21729 -- the same data.
21730
21731 Build_NCT_Tables (Map);
21732
21733 -- Execute Phase 1. Traverse the subtree and generate new entities for
21734 -- the following cases:
21735
21736 -- * An entity defined within an N_Expression_With_Actions node
21737
21738 -- * An itype referenced within the subtree where the associated node
21739 -- is also in the subtree.
21740
21741 -- All new entities are accessible via table NCT_New_Entities, which
21742 -- contains mappings of the form:
21743
21744 -- Old_Entity -> New_Entity
21745 -- Old_Itype -> New_Itype
21746
21747 -- In addition, the associated nodes of all new itypes are mapped in
21748 -- table NCT_Pending_Itypes:
21749
21750 -- Assoc_Nod -> (New_Itype1, New_Itype2, .., New_ItypeN)
21751
21752 Visit_Any_Node (Source);
21753
21754 -- Update the semantic attributes of all new entities generated during
21755 -- Phase 1 before starting Phase 2. The updates could be performed in
21756 -- routine Corresponding_Entity, however this may cause the same entity
21757 -- to be updated multiple times, effectively generating useless nodes.
21758 -- Keeping the updates separates from Phase 2 ensures that only one set
21759 -- of attributes is generated for an entity at any one time.
21760
21761 Update_New_Entities (Map);
21762
21763 -- Execute Phase 2. Replicate the source subtree one node at a time.
21764 -- The following transformations take place:
21765
21766 -- * References to entities and itypes are updated to refer to the
21767 -- new entities and itypes generated during Phase 1.
21768
21769 -- * All Associated_Node_For_Itype attributes of itypes are updated
21770 -- to refer to the new replicated Associated_Node_For_Itype.
21771
21772 return Copy_Node_With_Replacement (Source);
21773 end New_Copy_Tree;
21774
21775 -------------------------
21776 -- New_External_Entity --
21777 -------------------------
21778
21779 function New_External_Entity
21780 (Kind : Entity_Kind;
21781 Scope_Id : Entity_Id;
21782 Sloc_Value : Source_Ptr;
21783 Related_Id : Entity_Id;
21784 Suffix : Character;
21785 Suffix_Index : Int := 0;
21786 Prefix : Character := ' ') return Entity_Id
21787 is
21788 N : constant Entity_Id :=
21789 Make_Defining_Identifier (Sloc_Value,
21790 New_External_Name
21791 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
21792
21793 begin
21794 Set_Ekind (N, Kind);
21795 Set_Is_Internal (N, True);
21796 Append_Entity (N, Scope_Id);
21797 Set_Public_Status (N);
21798
21799 if Kind in Type_Kind then
21800 Init_Size_Align (N);
21801 end if;
21802
21803 return N;
21804 end New_External_Entity;
21805
21806 -------------------------
21807 -- New_Internal_Entity --
21808 -------------------------
21809
21810 function New_Internal_Entity
21811 (Kind : Entity_Kind;
21812 Scope_Id : Entity_Id;
21813 Sloc_Value : Source_Ptr;
21814 Id_Char : Character) return Entity_Id
21815 is
21816 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
21817
21818 begin
21819 Set_Ekind (N, Kind);
21820 Set_Is_Internal (N, True);
21821 Append_Entity (N, Scope_Id);
21822
21823 if Kind in Type_Kind then
21824 Init_Size_Align (N);
21825 end if;
21826
21827 return N;
21828 end New_Internal_Entity;
21829
21830 -----------------
21831 -- Next_Actual --
21832 -----------------
21833
21834 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
21835 Par : constant Node_Id := Parent (Actual_Id);
21836 N : Node_Id;
21837
21838 begin
21839 -- If we are pointing at a positional parameter, it is a member of a
21840 -- node list (the list of parameters), and the next parameter is the
21841 -- next node on the list, unless we hit a parameter association, then
21842 -- we shift to using the chain whose head is the First_Named_Actual in
21843 -- the parent, and then is threaded using the Next_Named_Actual of the
21844 -- Parameter_Association. All this fiddling is because the original node
21845 -- list is in the textual call order, and what we need is the
21846 -- declaration order.
21847
21848 if Is_List_Member (Actual_Id) then
21849 N := Next (Actual_Id);
21850
21851 if Nkind (N) = N_Parameter_Association then
21852
21853 -- In case of a build-in-place call, the call will no longer be a
21854 -- call; it will have been rewritten.
21855
21856 if Nkind_In (Par, N_Entry_Call_Statement,
21857 N_Function_Call,
21858 N_Procedure_Call_Statement)
21859 then
21860 return First_Named_Actual (Par);
21861
21862 -- In case of a call rewritten in GNATprove mode while "inlining
21863 -- for proof" go to the original call.
21864
21865 elsif Nkind (Par) = N_Null_Statement then
21866 pragma Assert
21867 (GNATprove_Mode
21868 and then
21869 Nkind (Original_Node (Par)) in N_Subprogram_Call);
21870
21871 return First_Named_Actual (Original_Node (Par));
21872 else
21873 return Empty;
21874 end if;
21875 else
21876 return N;
21877 end if;
21878
21879 else
21880 return Next_Named_Actual (Parent (Actual_Id));
21881 end if;
21882 end Next_Actual;
21883
21884 procedure Next_Actual (Actual_Id : in out Node_Id) is
21885 begin
21886 Actual_Id := Next_Actual (Actual_Id);
21887 end Next_Actual;
21888
21889 -----------------
21890 -- Next_Global --
21891 -----------------
21892
21893 function Next_Global (Node : Node_Id) return Node_Id is
21894 begin
21895 -- The global item may either be in a list, or by itself, in which case
21896 -- there is no next global item with the same mode.
21897
21898 if Is_List_Member (Node) then
21899 return Next (Node);
21900 else
21901 return Empty;
21902 end if;
21903 end Next_Global;
21904
21905 procedure Next_Global (Node : in out Node_Id) is
21906 begin
21907 Node := Next_Global (Node);
21908 end Next_Global;
21909
21910 ----------------------------------
21911 -- New_Requires_Transient_Scope --
21912 ----------------------------------
21913
21914 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
21915 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
21916 -- This is called for untagged records and protected types, with
21917 -- nondefaulted discriminants. Returns True if the size of function
21918 -- results is known at the call site, False otherwise. Returns False
21919 -- if there is a variant part that depends on the discriminants of
21920 -- this type, or if there is an array constrained by the discriminants
21921 -- of this type. ???Currently, this is overly conservative (the array
21922 -- could be nested inside some other record that is constrained by
21923 -- nondiscriminants). That is, the recursive calls are too conservative.
21924
21925 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean;
21926 -- Returns True if Typ is a nonlimited record with defaulted
21927 -- discriminants whose max size makes it unsuitable for allocating on
21928 -- the primary stack.
21929
21930 ------------------------------
21931 -- Caller_Known_Size_Record --
21932 ------------------------------
21933
21934 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
21935 pragma Assert (Typ = Underlying_Type (Typ));
21936
21937 begin
21938 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
21939 return False;
21940 end if;
21941
21942 declare
21943 Comp : Entity_Id;
21944
21945 begin
21946 Comp := First_Entity (Typ);
21947 while Present (Comp) loop
21948
21949 -- Only look at E_Component entities. No need to look at
21950 -- E_Discriminant entities, and we must ignore internal
21951 -- subtypes generated for constrained components.
21952
21953 if Ekind (Comp) = E_Component then
21954 declare
21955 Comp_Type : constant Entity_Id :=
21956 Underlying_Type (Etype (Comp));
21957
21958 begin
21959 if Is_Record_Type (Comp_Type)
21960 or else
21961 Is_Protected_Type (Comp_Type)
21962 then
21963 if not Caller_Known_Size_Record (Comp_Type) then
21964 return False;
21965 end if;
21966
21967 elsif Is_Array_Type (Comp_Type) then
21968 if Size_Depends_On_Discriminant (Comp_Type) then
21969 return False;
21970 end if;
21971 end if;
21972 end;
21973 end if;
21974
21975 Next_Entity (Comp);
21976 end loop;
21977 end;
21978
21979 return True;
21980 end Caller_Known_Size_Record;
21981
21982 ------------------------------
21983 -- Large_Max_Size_Mutable --
21984 ------------------------------
21985
21986 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean is
21987 pragma Assert (Typ = Underlying_Type (Typ));
21988
21989 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean;
21990 -- Returns true if the discrete type T has a large range
21991
21992 ----------------------------
21993 -- Is_Large_Discrete_Type --
21994 ----------------------------
21995
21996 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean is
21997 Threshold : constant Int := 16;
21998 -- Arbitrary threshold above which we consider it "large". We want
21999 -- a fairly large threshold, because these large types really
22000 -- shouldn't have default discriminants in the first place, in
22001 -- most cases.
22002
22003 begin
22004 return UI_To_Int (RM_Size (T)) > Threshold;
22005 end Is_Large_Discrete_Type;
22006
22007 -- Start of processing for Large_Max_Size_Mutable
22008
22009 begin
22010 if Is_Record_Type (Typ)
22011 and then not Is_Limited_View (Typ)
22012 and then Has_Defaulted_Discriminants (Typ)
22013 then
22014 -- Loop through the components, looking for an array whose upper
22015 -- bound(s) depends on discriminants, where both the subtype of
22016 -- the discriminant and the index subtype are too large.
22017
22018 declare
22019 Comp : Entity_Id;
22020
22021 begin
22022 Comp := First_Entity (Typ);
22023 while Present (Comp) loop
22024 if Ekind (Comp) = E_Component then
22025 declare
22026 Comp_Type : constant Entity_Id :=
22027 Underlying_Type (Etype (Comp));
22028
22029 Hi : Node_Id;
22030 Indx : Node_Id;
22031 Ityp : Entity_Id;
22032
22033 begin
22034 if Is_Array_Type (Comp_Type) then
22035 Indx := First_Index (Comp_Type);
22036
22037 while Present (Indx) loop
22038 Ityp := Etype (Indx);
22039 Hi := Type_High_Bound (Ityp);
22040
22041 if Nkind (Hi) = N_Identifier
22042 and then Ekind (Entity (Hi)) = E_Discriminant
22043 and then Is_Large_Discrete_Type (Ityp)
22044 and then Is_Large_Discrete_Type
22045 (Etype (Entity (Hi)))
22046 then
22047 return True;
22048 end if;
22049
22050 Next_Index (Indx);
22051 end loop;
22052 end if;
22053 end;
22054 end if;
22055
22056 Next_Entity (Comp);
22057 end loop;
22058 end;
22059 end if;
22060
22061 return False;
22062 end Large_Max_Size_Mutable;
22063
22064 -- Local declarations
22065
22066 Typ : constant Entity_Id := Underlying_Type (Id);
22067
22068 -- Start of processing for New_Requires_Transient_Scope
22069
22070 begin
22071 -- This is a private type which is not completed yet. This can only
22072 -- happen in a default expression (of a formal parameter or of a
22073 -- record component). Do not expand transient scope in this case.
22074
22075 if No (Typ) then
22076 return False;
22077
22078 -- Do not expand transient scope for non-existent procedure return or
22079 -- string literal types.
22080
22081 elsif Typ = Standard_Void_Type
22082 or else Ekind (Typ) = E_String_Literal_Subtype
22083 then
22084 return False;
22085
22086 -- If Typ is a generic formal incomplete type, then we want to look at
22087 -- the actual type.
22088
22089 elsif Ekind (Typ) = E_Record_Subtype
22090 and then Present (Cloned_Subtype (Typ))
22091 then
22092 return New_Requires_Transient_Scope (Cloned_Subtype (Typ));
22093
22094 -- Functions returning specific tagged types may dispatch on result, so
22095 -- their returned value is allocated on the secondary stack, even in the
22096 -- definite case. We must treat nondispatching functions the same way,
22097 -- because access-to-function types can point at both, so the calling
22098 -- conventions must be compatible. Is_Tagged_Type includes controlled
22099 -- types and class-wide types. Controlled type temporaries need
22100 -- finalization.
22101
22102 -- ???It's not clear why we need to return noncontrolled types with
22103 -- controlled components on the secondary stack.
22104
22105 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
22106 return True;
22107
22108 -- Untagged definite subtypes are known size. This includes all
22109 -- elementary [sub]types. Tasks are known size even if they have
22110 -- discriminants. So we return False here, with one exception:
22111 -- For a type like:
22112 -- type T (Last : Natural := 0) is
22113 -- X : String (1 .. Last);
22114 -- end record;
22115 -- we return True. That's because for "P(F(...));", where F returns T,
22116 -- we don't know the size of the result at the call site, so if we
22117 -- allocated it on the primary stack, we would have to allocate the
22118 -- maximum size, which is way too big.
22119
22120 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
22121 return Large_Max_Size_Mutable (Typ);
22122
22123 -- Indefinite (discriminated) untagged record or protected type
22124
22125 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
22126 return not Caller_Known_Size_Record (Typ);
22127
22128 -- Unconstrained array
22129
22130 else
22131 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
22132 return True;
22133 end if;
22134 end New_Requires_Transient_Scope;
22135
22136 ------------------------
22137 -- No_Caching_Enabled --
22138 ------------------------
22139
22140 function No_Caching_Enabled (Id : Entity_Id) return Boolean is
22141 Prag : constant Node_Id := Get_Pragma (Id, Pragma_No_Caching);
22142 Arg1 : Node_Id;
22143
22144 begin
22145 if Present (Prag) then
22146 Arg1 := First (Pragma_Argument_Associations (Prag));
22147
22148 -- The pragma has an optional Boolean expression, the related
22149 -- property is enabled only when the expression evaluates to True.
22150
22151 if Present (Arg1) then
22152 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
22153
22154 -- Otherwise the lack of expression enables the property by
22155 -- default.
22156
22157 else
22158 return True;
22159 end if;
22160
22161 -- The property was never set in the first place
22162
22163 else
22164 return False;
22165 end if;
22166 end No_Caching_Enabled;
22167
22168 --------------------------
22169 -- No_Heap_Finalization --
22170 --------------------------
22171
22172 function No_Heap_Finalization (Typ : Entity_Id) return Boolean is
22173 begin
22174 if Ekind_In (Typ, E_Access_Type, E_General_Access_Type)
22175 and then Is_Library_Level_Entity (Typ)
22176 then
22177 -- A global No_Heap_Finalization pragma applies to all library-level
22178 -- named access-to-object types.
22179
22180 if Present (No_Heap_Finalization_Pragma) then
22181 return True;
22182
22183 -- The library-level named access-to-object type itself is subject to
22184 -- pragma No_Heap_Finalization.
22185
22186 elsif Present (Get_Pragma (Typ, Pragma_No_Heap_Finalization)) then
22187 return True;
22188 end if;
22189 end if;
22190
22191 return False;
22192 end No_Heap_Finalization;
22193
22194 -----------------------
22195 -- Normalize_Actuals --
22196 -----------------------
22197
22198 -- Chain actuals according to formals of subprogram. If there are no named
22199 -- associations, the chain is simply the list of Parameter Associations,
22200 -- since the order is the same as the declaration order. If there are named
22201 -- associations, then the First_Named_Actual field in the N_Function_Call
22202 -- or N_Procedure_Call_Statement node points to the Parameter_Association
22203 -- node for the parameter that comes first in declaration order. The
22204 -- remaining named parameters are then chained in declaration order using
22205 -- Next_Named_Actual.
22206
22207 -- This routine also verifies that the number of actuals is compatible with
22208 -- the number and default values of formals, but performs no type checking
22209 -- (type checking is done by the caller).
22210
22211 -- If the matching succeeds, Success is set to True and the caller proceeds
22212 -- with type-checking. If the match is unsuccessful, then Success is set to
22213 -- False, and the caller attempts a different interpretation, if there is
22214 -- one.
22215
22216 -- If the flag Report is on, the call is not overloaded, and a failure to
22217 -- match can be reported here, rather than in the caller.
22218
22219 procedure Normalize_Actuals
22220 (N : Node_Id;
22221 S : Entity_Id;
22222 Report : Boolean;
22223 Success : out Boolean)
22224 is
22225 Actuals : constant List_Id := Parameter_Associations (N);
22226 Actual : Node_Id := Empty;
22227 Formal : Entity_Id;
22228 Last : Node_Id := Empty;
22229 First_Named : Node_Id := Empty;
22230 Found : Boolean;
22231
22232 Formals_To_Match : Integer := 0;
22233 Actuals_To_Match : Integer := 0;
22234
22235 procedure Chain (A : Node_Id);
22236 -- Add named actual at the proper place in the list, using the
22237 -- Next_Named_Actual link.
22238
22239 function Reporting return Boolean;
22240 -- Determines if an error is to be reported. To report an error, we
22241 -- need Report to be True, and also we do not report errors caused
22242 -- by calls to init procs that occur within other init procs. Such
22243 -- errors must always be cascaded errors, since if all the types are
22244 -- declared correctly, the compiler will certainly build decent calls.
22245
22246 -----------
22247 -- Chain --
22248 -----------
22249
22250 procedure Chain (A : Node_Id) is
22251 begin
22252 if No (Last) then
22253
22254 -- Call node points to first actual in list
22255
22256 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
22257
22258 else
22259 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
22260 end if;
22261
22262 Last := A;
22263 Set_Next_Named_Actual (Last, Empty);
22264 end Chain;
22265
22266 ---------------
22267 -- Reporting --
22268 ---------------
22269
22270 function Reporting return Boolean is
22271 begin
22272 if not Report then
22273 return False;
22274
22275 elsif not Within_Init_Proc then
22276 return True;
22277
22278 elsif Is_Init_Proc (Entity (Name (N))) then
22279 return False;
22280
22281 else
22282 return True;
22283 end if;
22284 end Reporting;
22285
22286 -- Start of processing for Normalize_Actuals
22287
22288 begin
22289 if Is_Access_Type (S) then
22290
22291 -- The name in the call is a function call that returns an access
22292 -- to subprogram. The designated type has the list of formals.
22293
22294 Formal := First_Formal (Designated_Type (S));
22295 else
22296 Formal := First_Formal (S);
22297 end if;
22298
22299 while Present (Formal) loop
22300 Formals_To_Match := Formals_To_Match + 1;
22301 Next_Formal (Formal);
22302 end loop;
22303
22304 -- Find if there is a named association, and verify that no positional
22305 -- associations appear after named ones.
22306
22307 if Present (Actuals) then
22308 Actual := First (Actuals);
22309 end if;
22310
22311 while Present (Actual)
22312 and then Nkind (Actual) /= N_Parameter_Association
22313 loop
22314 Actuals_To_Match := Actuals_To_Match + 1;
22315 Next (Actual);
22316 end loop;
22317
22318 if No (Actual) and Actuals_To_Match = Formals_To_Match then
22319
22320 -- Most common case: positional notation, no defaults
22321
22322 Success := True;
22323 return;
22324
22325 elsif Actuals_To_Match > Formals_To_Match then
22326
22327 -- Too many actuals: will not work
22328
22329 if Reporting then
22330 if Is_Entity_Name (Name (N)) then
22331 Error_Msg_N ("too many arguments in call to&", Name (N));
22332 else
22333 Error_Msg_N ("too many arguments in call", N);
22334 end if;
22335 end if;
22336
22337 Success := False;
22338 return;
22339 end if;
22340
22341 First_Named := Actual;
22342
22343 while Present (Actual) loop
22344 if Nkind (Actual) /= N_Parameter_Association then
22345 Error_Msg_N
22346 ("positional parameters not allowed after named ones", Actual);
22347 Success := False;
22348 return;
22349
22350 else
22351 Actuals_To_Match := Actuals_To_Match + 1;
22352 end if;
22353
22354 Next (Actual);
22355 end loop;
22356
22357 if Present (Actuals) then
22358 Actual := First (Actuals);
22359 end if;
22360
22361 Formal := First_Formal (S);
22362 while Present (Formal) loop
22363
22364 -- Match the formals in order. If the corresponding actual is
22365 -- positional, nothing to do. Else scan the list of named actuals
22366 -- to find the one with the right name.
22367
22368 if Present (Actual)
22369 and then Nkind (Actual) /= N_Parameter_Association
22370 then
22371 Next (Actual);
22372 Actuals_To_Match := Actuals_To_Match - 1;
22373 Formals_To_Match := Formals_To_Match - 1;
22374
22375 else
22376 -- For named parameters, search the list of actuals to find
22377 -- one that matches the next formal name.
22378
22379 Actual := First_Named;
22380 Found := False;
22381 while Present (Actual) loop
22382 if Chars (Selector_Name (Actual)) = Chars (Formal) then
22383 Found := True;
22384 Chain (Actual);
22385 Actuals_To_Match := Actuals_To_Match - 1;
22386 Formals_To_Match := Formals_To_Match - 1;
22387 exit;
22388 end if;
22389
22390 Next (Actual);
22391 end loop;
22392
22393 if not Found then
22394 if Ekind (Formal) /= E_In_Parameter
22395 or else No (Default_Value (Formal))
22396 then
22397 if Reporting then
22398 if (Comes_From_Source (S)
22399 or else Sloc (S) = Standard_Location)
22400 and then Is_Overloadable (S)
22401 then
22402 if No (Actuals)
22403 and then
22404 Nkind_In (Parent (N), N_Procedure_Call_Statement,
22405 N_Function_Call,
22406 N_Parameter_Association)
22407 and then Ekind (S) /= E_Function
22408 then
22409 Set_Etype (N, Etype (S));
22410
22411 else
22412 Error_Msg_Name_1 := Chars (S);
22413 Error_Msg_Sloc := Sloc (S);
22414 Error_Msg_NE
22415 ("missing argument for parameter & "
22416 & "in call to % declared #", N, Formal);
22417 end if;
22418
22419 elsif Is_Overloadable (S) then
22420 Error_Msg_Name_1 := Chars (S);
22421
22422 -- Point to type derivation that generated the
22423 -- operation.
22424
22425 Error_Msg_Sloc := Sloc (Parent (S));
22426
22427 Error_Msg_NE
22428 ("missing argument for parameter & "
22429 & "in call to % (inherited) #", N, Formal);
22430
22431 else
22432 Error_Msg_NE
22433 ("missing argument for parameter &", N, Formal);
22434 end if;
22435 end if;
22436
22437 Success := False;
22438 return;
22439
22440 else
22441 Formals_To_Match := Formals_To_Match - 1;
22442 end if;
22443 end if;
22444 end if;
22445
22446 Next_Formal (Formal);
22447 end loop;
22448
22449 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
22450 Success := True;
22451 return;
22452
22453 else
22454 if Reporting then
22455
22456 -- Find some superfluous named actual that did not get
22457 -- attached to the list of associations.
22458
22459 Actual := First (Actuals);
22460 while Present (Actual) loop
22461 if Nkind (Actual) = N_Parameter_Association
22462 and then Actual /= Last
22463 and then No (Next_Named_Actual (Actual))
22464 then
22465 -- A validity check may introduce a copy of a call that
22466 -- includes an extra actual (for example for an unrelated
22467 -- accessibility check). Check that the extra actual matches
22468 -- some extra formal, which must exist already because
22469 -- subprogram must be frozen at this point.
22470
22471 if Present (Extra_Formals (S))
22472 and then not Comes_From_Source (Actual)
22473 and then Nkind (Actual) = N_Parameter_Association
22474 and then Chars (Extra_Formals (S)) =
22475 Chars (Selector_Name (Actual))
22476 then
22477 null;
22478 else
22479 Error_Msg_N
22480 ("unmatched actual & in call", Selector_Name (Actual));
22481 exit;
22482 end if;
22483 end if;
22484
22485 Next (Actual);
22486 end loop;
22487 end if;
22488
22489 Success := False;
22490 return;
22491 end if;
22492 end Normalize_Actuals;
22493
22494 --------------------------------
22495 -- Note_Possible_Modification --
22496 --------------------------------
22497
22498 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
22499 Modification_Comes_From_Source : constant Boolean :=
22500 Comes_From_Source (Parent (N));
22501
22502 Ent : Entity_Id;
22503 Exp : Node_Id;
22504
22505 begin
22506 -- Loop to find referenced entity, if there is one
22507
22508 Exp := N;
22509 loop
22510 Ent := Empty;
22511
22512 if Is_Entity_Name (Exp) then
22513 Ent := Entity (Exp);
22514
22515 -- If the entity is missing, it is an undeclared identifier,
22516 -- and there is nothing to annotate.
22517
22518 if No (Ent) then
22519 return;
22520 end if;
22521
22522 elsif Nkind (Exp) = N_Explicit_Dereference then
22523 declare
22524 P : constant Node_Id := Prefix (Exp);
22525
22526 begin
22527 -- In formal verification mode, keep track of all reads and
22528 -- writes through explicit dereferences.
22529
22530 if GNATprove_Mode then
22531 SPARK_Specific.Generate_Dereference (N, 'm');
22532 end if;
22533
22534 if Nkind (P) = N_Selected_Component
22535 and then Present (Entry_Formal (Entity (Selector_Name (P))))
22536 then
22537 -- Case of a reference to an entry formal
22538
22539 Ent := Entry_Formal (Entity (Selector_Name (P)));
22540
22541 elsif Nkind (P) = N_Identifier
22542 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
22543 and then Present (Expression (Parent (Entity (P))))
22544 and then Nkind (Expression (Parent (Entity (P)))) =
22545 N_Reference
22546 then
22547 -- Case of a reference to a value on which side effects have
22548 -- been removed.
22549
22550 Exp := Prefix (Expression (Parent (Entity (P))));
22551 goto Continue;
22552
22553 else
22554 return;
22555 end if;
22556 end;
22557
22558 elsif Nkind_In (Exp, N_Type_Conversion,
22559 N_Unchecked_Type_Conversion)
22560 then
22561 Exp := Expression (Exp);
22562 goto Continue;
22563
22564 elsif Nkind_In (Exp, N_Slice,
22565 N_Indexed_Component,
22566 N_Selected_Component)
22567 then
22568 -- Special check, if the prefix is an access type, then return
22569 -- since we are modifying the thing pointed to, not the prefix.
22570 -- When we are expanding, most usually the prefix is replaced
22571 -- by an explicit dereference, and this test is not needed, but
22572 -- in some cases (notably -gnatc mode and generics) when we do
22573 -- not do full expansion, we need this special test.
22574
22575 if Is_Access_Type (Etype (Prefix (Exp))) then
22576 return;
22577
22578 -- Otherwise go to prefix and keep going
22579
22580 else
22581 Exp := Prefix (Exp);
22582 goto Continue;
22583 end if;
22584
22585 -- All other cases, not a modification
22586
22587 else
22588 return;
22589 end if;
22590
22591 -- Now look for entity being referenced
22592
22593 if Present (Ent) then
22594 if Is_Object (Ent) then
22595 if Comes_From_Source (Exp)
22596 or else Modification_Comes_From_Source
22597 then
22598 -- Give warning if pragma unmodified is given and we are
22599 -- sure this is a modification.
22600
22601 if Has_Pragma_Unmodified (Ent) and then Sure then
22602
22603 -- Note that the entity may be present only as a result
22604 -- of pragma Unused.
22605
22606 if Has_Pragma_Unused (Ent) then
22607 Error_Msg_NE ("??pragma Unused given for &!", N, Ent);
22608 else
22609 Error_Msg_NE
22610 ("??pragma Unmodified given for &!", N, Ent);
22611 end if;
22612 end if;
22613
22614 Set_Never_Set_In_Source (Ent, False);
22615 end if;
22616
22617 Set_Is_True_Constant (Ent, False);
22618 Set_Current_Value (Ent, Empty);
22619 Set_Is_Known_Null (Ent, False);
22620
22621 if not Can_Never_Be_Null (Ent) then
22622 Set_Is_Known_Non_Null (Ent, False);
22623 end if;
22624
22625 -- Follow renaming chain
22626
22627 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
22628 and then Present (Renamed_Object (Ent))
22629 then
22630 Exp := Renamed_Object (Ent);
22631
22632 -- If the entity is the loop variable in an iteration over
22633 -- a container, retrieve container expression to indicate
22634 -- possible modification.
22635
22636 if Present (Related_Expression (Ent))
22637 and then Nkind (Parent (Related_Expression (Ent))) =
22638 N_Iterator_Specification
22639 then
22640 Exp := Original_Node (Related_Expression (Ent));
22641 end if;
22642
22643 goto Continue;
22644
22645 -- The expression may be the renaming of a subcomponent of an
22646 -- array or container. The assignment to the subcomponent is
22647 -- a modification of the container.
22648
22649 elsif Comes_From_Source (Original_Node (Exp))
22650 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
22651 N_Indexed_Component)
22652 then
22653 Exp := Prefix (Original_Node (Exp));
22654 goto Continue;
22655 end if;
22656
22657 -- Generate a reference only if the assignment comes from
22658 -- source. This excludes, for example, calls to a dispatching
22659 -- assignment operation when the left-hand side is tagged. In
22660 -- GNATprove mode, we need those references also on generated
22661 -- code, as these are used to compute the local effects of
22662 -- subprograms.
22663
22664 if Modification_Comes_From_Source or GNATprove_Mode then
22665 Generate_Reference (Ent, Exp, 'm');
22666
22667 -- If the target of the assignment is the bound variable
22668 -- in an iterator, indicate that the corresponding array
22669 -- or container is also modified.
22670
22671 if Ada_Version >= Ada_2012
22672 and then Nkind (Parent (Ent)) = N_Iterator_Specification
22673 then
22674 declare
22675 Domain : constant Node_Id := Name (Parent (Ent));
22676
22677 begin
22678 -- TBD : in the full version of the construct, the
22679 -- domain of iteration can be given by an expression.
22680
22681 if Is_Entity_Name (Domain) then
22682 Generate_Reference (Entity (Domain), Exp, 'm');
22683 Set_Is_True_Constant (Entity (Domain), False);
22684 Set_Never_Set_In_Source (Entity (Domain), False);
22685 end if;
22686 end;
22687 end if;
22688 end if;
22689 end if;
22690
22691 Kill_Checks (Ent);
22692
22693 -- If we are sure this is a modification from source, and we know
22694 -- this modifies a constant, then give an appropriate warning.
22695
22696 if Sure
22697 and then Modification_Comes_From_Source
22698 and then Overlays_Constant (Ent)
22699 and then Address_Clause_Overlay_Warnings
22700 then
22701 declare
22702 Addr : constant Node_Id := Address_Clause (Ent);
22703 O_Ent : Entity_Id;
22704 Off : Boolean;
22705
22706 begin
22707 Find_Overlaid_Entity (Addr, O_Ent, Off);
22708
22709 Error_Msg_Sloc := Sloc (Addr);
22710 Error_Msg_NE
22711 ("??constant& may be modified via address clause#",
22712 N, O_Ent);
22713 end;
22714 end if;
22715
22716 return;
22717 end if;
22718
22719 <<Continue>>
22720 null;
22721 end loop;
22722 end Note_Possible_Modification;
22723
22724 -----------------
22725 -- Null_Status --
22726 -----------------
22727
22728 function Null_Status (N : Node_Id) return Null_Status_Kind is
22729 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean;
22730 -- Determine whether definition Def carries a null exclusion
22731
22732 function Null_Status_Of_Entity (Id : Entity_Id) return Null_Status_Kind;
22733 -- Determine the null status of arbitrary entity Id
22734
22735 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind;
22736 -- Determine the null status of type Typ
22737
22738 ---------------------------
22739 -- Is_Null_Excluding_Def --
22740 ---------------------------
22741
22742 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean is
22743 begin
22744 return
22745 Nkind_In (Def, N_Access_Definition,
22746 N_Access_Function_Definition,
22747 N_Access_Procedure_Definition,
22748 N_Access_To_Object_Definition,
22749 N_Component_Definition,
22750 N_Derived_Type_Definition)
22751 and then Null_Exclusion_Present (Def);
22752 end Is_Null_Excluding_Def;
22753
22754 ---------------------------
22755 -- Null_Status_Of_Entity --
22756 ---------------------------
22757
22758 function Null_Status_Of_Entity
22759 (Id : Entity_Id) return Null_Status_Kind
22760 is
22761 Decl : constant Node_Id := Declaration_Node (Id);
22762 Def : Node_Id;
22763
22764 begin
22765 -- The value of an imported or exported entity may be set externally
22766 -- regardless of a null exclusion. As a result, the value cannot be
22767 -- determined statically.
22768
22769 if Is_Imported (Id) or else Is_Exported (Id) then
22770 return Unknown;
22771
22772 elsif Nkind_In (Decl, N_Component_Declaration,
22773 N_Discriminant_Specification,
22774 N_Formal_Object_Declaration,
22775 N_Object_Declaration,
22776 N_Object_Renaming_Declaration,
22777 N_Parameter_Specification)
22778 then
22779 -- A component declaration yields a non-null value when either
22780 -- its component definition or access definition carries a null
22781 -- exclusion.
22782
22783 if Nkind (Decl) = N_Component_Declaration then
22784 Def := Component_Definition (Decl);
22785
22786 if Is_Null_Excluding_Def (Def) then
22787 return Is_Non_Null;
22788 end if;
22789
22790 Def := Access_Definition (Def);
22791
22792 if Present (Def) and then Is_Null_Excluding_Def (Def) then
22793 return Is_Non_Null;
22794 end if;
22795
22796 -- A formal object declaration yields a non-null value if its
22797 -- access definition carries a null exclusion. If the object is
22798 -- default initialized, then the value depends on the expression.
22799
22800 elsif Nkind (Decl) = N_Formal_Object_Declaration then
22801 Def := Access_Definition (Decl);
22802
22803 if Present (Def) and then Is_Null_Excluding_Def (Def) then
22804 return Is_Non_Null;
22805 end if;
22806
22807 -- A constant may yield a null or non-null value depending on its
22808 -- initialization expression.
22809
22810 elsif Ekind (Id) = E_Constant then
22811 return Null_Status (Constant_Value (Id));
22812
22813 -- The construct yields a non-null value when it has a null
22814 -- exclusion.
22815
22816 elsif Null_Exclusion_Present (Decl) then
22817 return Is_Non_Null;
22818
22819 -- An object renaming declaration yields a non-null value if its
22820 -- access definition carries a null exclusion. Otherwise the value
22821 -- depends on the renamed name.
22822
22823 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
22824 Def := Access_Definition (Decl);
22825
22826 if Present (Def) and then Is_Null_Excluding_Def (Def) then
22827 return Is_Non_Null;
22828
22829 else
22830 return Null_Status (Name (Decl));
22831 end if;
22832 end if;
22833 end if;
22834
22835 -- At this point the declaration of the entity does not carry a null
22836 -- exclusion and lacks an initialization expression. Check the status
22837 -- of its type.
22838
22839 return Null_Status_Of_Type (Etype (Id));
22840 end Null_Status_Of_Entity;
22841
22842 -------------------------
22843 -- Null_Status_Of_Type --
22844 -------------------------
22845
22846 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind is
22847 Curr : Entity_Id;
22848 Decl : Node_Id;
22849
22850 begin
22851 -- Traverse the type chain looking for types with null exclusion
22852
22853 Curr := Typ;
22854 while Present (Curr) and then Etype (Curr) /= Curr loop
22855 Decl := Parent (Curr);
22856
22857 -- Guard against itypes which do not always have declarations. A
22858 -- type yields a non-null value if it carries a null exclusion.
22859
22860 if Present (Decl) then
22861 if Nkind (Decl) = N_Full_Type_Declaration
22862 and then Is_Null_Excluding_Def (Type_Definition (Decl))
22863 then
22864 return Is_Non_Null;
22865
22866 elsif Nkind (Decl) = N_Subtype_Declaration
22867 and then Null_Exclusion_Present (Decl)
22868 then
22869 return Is_Non_Null;
22870 end if;
22871 end if;
22872
22873 Curr := Etype (Curr);
22874 end loop;
22875
22876 -- The type chain does not contain any null excluding types
22877
22878 return Unknown;
22879 end Null_Status_Of_Type;
22880
22881 -- Start of processing for Null_Status
22882
22883 begin
22884 -- Prevent cascaded errors or infinite loops when trying to determine
22885 -- the null status of an erroneous construct.
22886
22887 if Error_Posted (N) then
22888 return Unknown;
22889
22890 -- An allocator always creates a non-null value
22891
22892 elsif Nkind (N) = N_Allocator then
22893 return Is_Non_Null;
22894
22895 -- Taking the 'Access of something yields a non-null value
22896
22897 elsif Nkind (N) = N_Attribute_Reference
22898 and then Nam_In (Attribute_Name (N), Name_Access,
22899 Name_Unchecked_Access,
22900 Name_Unrestricted_Access)
22901 then
22902 return Is_Non_Null;
22903
22904 -- "null" yields null
22905
22906 elsif Nkind (N) = N_Null then
22907 return Is_Null;
22908
22909 -- Check the status of the operand of a type conversion
22910
22911 elsif Nkind (N) = N_Type_Conversion then
22912 return Null_Status (Expression (N));
22913
22914 -- The input denotes a reference to an entity. Determine whether the
22915 -- entity or its type yields a null or non-null value.
22916
22917 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
22918 return Null_Status_Of_Entity (Entity (N));
22919 end if;
22920
22921 -- Otherwise it is not possible to determine the null status of the
22922 -- subexpression at compile time without resorting to simple flow
22923 -- analysis.
22924
22925 return Unknown;
22926 end Null_Status;
22927
22928 --------------------------------------
22929 -- Null_To_Null_Address_Convert_OK --
22930 --------------------------------------
22931
22932 function Null_To_Null_Address_Convert_OK
22933 (N : Node_Id;
22934 Typ : Entity_Id := Empty) return Boolean
22935 is
22936 begin
22937 if not Relaxed_RM_Semantics then
22938 return False;
22939 end if;
22940
22941 if Nkind (N) = N_Null then
22942 return Present (Typ) and then Is_Descendant_Of_Address (Typ);
22943
22944 elsif Nkind_In (N, N_Op_Eq, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt, N_Op_Ne)
22945 then
22946 declare
22947 L : constant Node_Id := Left_Opnd (N);
22948 R : constant Node_Id := Right_Opnd (N);
22949
22950 begin
22951 -- We check the Etype of the complementary operand since the
22952 -- N_Null node is not decorated at this stage.
22953
22954 return
22955 ((Nkind (L) = N_Null
22956 and then Is_Descendant_Of_Address (Etype (R)))
22957 or else
22958 (Nkind (R) = N_Null
22959 and then Is_Descendant_Of_Address (Etype (L))));
22960 end;
22961 end if;
22962
22963 return False;
22964 end Null_To_Null_Address_Convert_OK;
22965
22966 ---------------------------------
22967 -- Number_Of_Elements_In_Array --
22968 ---------------------------------
22969
22970 function Number_Of_Elements_In_Array (T : Entity_Id) return Int is
22971 Indx : Node_Id;
22972 Typ : Entity_Id;
22973 Low : Node_Id;
22974 High : Node_Id;
22975 Num : Int := 1;
22976
22977 begin
22978 pragma Assert (Is_Array_Type (T));
22979
22980 Indx := First_Index (T);
22981 while Present (Indx) loop
22982 Typ := Underlying_Type (Etype (Indx));
22983
22984 -- Never look at junk bounds of a generic type
22985
22986 if Is_Generic_Type (Typ) then
22987 return 0;
22988 end if;
22989
22990 -- Check the array bounds are known at compile time and return zero
22991 -- if they are not.
22992
22993 Low := Type_Low_Bound (Typ);
22994 High := Type_High_Bound (Typ);
22995
22996 if not Compile_Time_Known_Value (Low) then
22997 return 0;
22998 elsif not Compile_Time_Known_Value (High) then
22999 return 0;
23000 else
23001 Num :=
23002 Num * UI_To_Int ((Expr_Value (High) - Expr_Value (Low) + 1));
23003 end if;
23004
23005 Next_Index (Indx);
23006 end loop;
23007
23008 return Num;
23009 end Number_Of_Elements_In_Array;
23010
23011 -------------------------
23012 -- Object_Access_Level --
23013 -------------------------
23014
23015 -- Returns the static accessibility level of the view denoted by Obj. Note
23016 -- that the value returned is the result of a call to Scope_Depth. Only
23017 -- scope depths associated with dynamic scopes can actually be returned.
23018 -- Since only relative levels matter for accessibility checking, the fact
23019 -- that the distance between successive levels of accessibility is not
23020 -- always one is immaterial (invariant: if level(E2) is deeper than
23021 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
23022
23023 function Object_Access_Level (Obj : Node_Id) return Uint is
23024 function Is_Interface_Conversion (N : Node_Id) return Boolean;
23025 -- Determine whether N is a construct of the form
23026 -- Some_Type (Operand._tag'Address)
23027 -- This construct appears in the context of dispatching calls.
23028
23029 function Reference_To (Obj : Node_Id) return Node_Id;
23030 -- An explicit dereference is created when removing side effects from
23031 -- expressions for constraint checking purposes. In this case a local
23032 -- access type is created for it. The correct access level is that of
23033 -- the original source node. We detect this case by noting that the
23034 -- prefix of the dereference is created by an object declaration whose
23035 -- initial expression is a reference.
23036
23037 -----------------------------
23038 -- Is_Interface_Conversion --
23039 -----------------------------
23040
23041 function Is_Interface_Conversion (N : Node_Id) return Boolean is
23042 begin
23043 return Nkind (N) = N_Unchecked_Type_Conversion
23044 and then Nkind (Expression (N)) = N_Attribute_Reference
23045 and then Attribute_Name (Expression (N)) = Name_Address;
23046 end Is_Interface_Conversion;
23047
23048 ------------------
23049 -- Reference_To --
23050 ------------------
23051
23052 function Reference_To (Obj : Node_Id) return Node_Id is
23053 Pref : constant Node_Id := Prefix (Obj);
23054 begin
23055 if Is_Entity_Name (Pref)
23056 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
23057 and then Present (Expression (Parent (Entity (Pref))))
23058 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
23059 then
23060 return (Prefix (Expression (Parent (Entity (Pref)))));
23061 else
23062 return Empty;
23063 end if;
23064 end Reference_To;
23065
23066 -- Local variables
23067
23068 E : Entity_Id;
23069
23070 -- Start of processing for Object_Access_Level
23071
23072 begin
23073 if Nkind (Obj) = N_Defining_Identifier
23074 or else Is_Entity_Name (Obj)
23075 then
23076 if Nkind (Obj) = N_Defining_Identifier then
23077 E := Obj;
23078 else
23079 E := Entity (Obj);
23080 end if;
23081
23082 if Is_Prival (E) then
23083 E := Prival_Link (E);
23084 end if;
23085
23086 -- If E is a type then it denotes a current instance. For this case
23087 -- we add one to the normal accessibility level of the type to ensure
23088 -- that current instances are treated as always being deeper than
23089 -- than the level of any visible named access type (see 3.10.2(21)).
23090
23091 if Is_Type (E) then
23092 return Type_Access_Level (E) + 1;
23093
23094 elsif Present (Renamed_Object (E)) then
23095 return Object_Access_Level (Renamed_Object (E));
23096
23097 -- Similarly, if E is a component of the current instance of a
23098 -- protected type, any instance of it is assumed to be at a deeper
23099 -- level than the type. For a protected object (whose type is an
23100 -- anonymous protected type) its components are at the same level
23101 -- as the type itself.
23102
23103 elsif not Is_Overloadable (E)
23104 and then Ekind (Scope (E)) = E_Protected_Type
23105 and then Comes_From_Source (Scope (E))
23106 then
23107 return Type_Access_Level (Scope (E)) + 1;
23108
23109 else
23110 -- Aliased formals of functions take their access level from the
23111 -- point of call, i.e. require a dynamic check. For static check
23112 -- purposes, this is smaller than the level of the subprogram
23113 -- itself. For procedures the aliased makes no difference.
23114
23115 if Is_Formal (E)
23116 and then Is_Aliased (E)
23117 and then Ekind (Scope (E)) = E_Function
23118 then
23119 return Type_Access_Level (Etype (E));
23120
23121 else
23122 return Scope_Depth (Enclosing_Dynamic_Scope (E));
23123 end if;
23124 end if;
23125
23126 elsif Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
23127 if Is_Access_Type (Etype (Prefix (Obj))) then
23128 return Type_Access_Level (Etype (Prefix (Obj)));
23129 else
23130 return Object_Access_Level (Prefix (Obj));
23131 end if;
23132
23133 elsif Nkind (Obj) = N_Explicit_Dereference then
23134
23135 -- If the prefix is a selected access discriminant then we make a
23136 -- recursive call on the prefix, which will in turn check the level
23137 -- of the prefix object of the selected discriminant.
23138
23139 -- In Ada 2012, if the discriminant has implicit dereference and
23140 -- the context is a selected component, treat this as an object of
23141 -- unknown scope (see below). This is necessary in compile-only mode;
23142 -- otherwise expansion will already have transformed the prefix into
23143 -- a temporary.
23144
23145 if Nkind (Prefix (Obj)) = N_Selected_Component
23146 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
23147 and then
23148 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
23149 and then
23150 (not Has_Implicit_Dereference
23151 (Entity (Selector_Name (Prefix (Obj))))
23152 or else Nkind (Parent (Obj)) /= N_Selected_Component)
23153 then
23154 return Object_Access_Level (Prefix (Obj));
23155
23156 -- Detect an interface conversion in the context of a dispatching
23157 -- call. Use the original form of the conversion to find the access
23158 -- level of the operand.
23159
23160 elsif Is_Interface (Etype (Obj))
23161 and then Is_Interface_Conversion (Prefix (Obj))
23162 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
23163 then
23164 return Object_Access_Level (Original_Node (Obj));
23165
23166 elsif not Comes_From_Source (Obj) then
23167 declare
23168 Ref : constant Node_Id := Reference_To (Obj);
23169 begin
23170 if Present (Ref) then
23171 return Object_Access_Level (Ref);
23172 else
23173 return Type_Access_Level (Etype (Prefix (Obj)));
23174 end if;
23175 end;
23176
23177 else
23178 return Type_Access_Level (Etype (Prefix (Obj)));
23179 end if;
23180
23181 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
23182 return Object_Access_Level (Expression (Obj));
23183
23184 elsif Nkind (Obj) = N_Function_Call then
23185
23186 -- Function results are objects, so we get either the access level of
23187 -- the function or, in the case of an indirect call, the level of the
23188 -- access-to-subprogram type. (This code is used for Ada 95, but it
23189 -- looks wrong, because it seems that we should be checking the level
23190 -- of the call itself, even for Ada 95. However, using the Ada 2005
23191 -- version of the code causes regressions in several tests that are
23192 -- compiled with -gnat95. ???)
23193
23194 if Ada_Version < Ada_2005 then
23195 if Is_Entity_Name (Name (Obj)) then
23196 return Subprogram_Access_Level (Entity (Name (Obj)));
23197 else
23198 return Type_Access_Level (Etype (Prefix (Name (Obj))));
23199 end if;
23200
23201 -- For Ada 2005, the level of the result object of a function call is
23202 -- defined to be the level of the call's innermost enclosing master.
23203 -- We determine that by querying the depth of the innermost enclosing
23204 -- dynamic scope.
23205
23206 else
23207 Return_Master_Scope_Depth_Of_Call : declare
23208 function Innermost_Master_Scope_Depth
23209 (N : Node_Id) return Uint;
23210 -- Returns the scope depth of the given node's innermost
23211 -- enclosing dynamic scope (effectively the accessibility
23212 -- level of the innermost enclosing master).
23213
23214 ----------------------------------
23215 -- Innermost_Master_Scope_Depth --
23216 ----------------------------------
23217
23218 function Innermost_Master_Scope_Depth
23219 (N : Node_Id) return Uint
23220 is
23221 Node_Par : Node_Id := Parent (N);
23222
23223 begin
23224 -- Locate the nearest enclosing node (by traversing Parents)
23225 -- that Defining_Entity can be applied to, and return the
23226 -- depth of that entity's nearest enclosing dynamic scope.
23227
23228 while Present (Node_Par) loop
23229 case Nkind (Node_Par) is
23230 when N_Abstract_Subprogram_Declaration
23231 | N_Block_Statement
23232 | N_Body_Stub
23233 | N_Component_Declaration
23234 | N_Entry_Body
23235 | N_Entry_Declaration
23236 | N_Exception_Declaration
23237 | N_Formal_Object_Declaration
23238 | N_Formal_Package_Declaration
23239 | N_Formal_Subprogram_Declaration
23240 | N_Formal_Type_Declaration
23241 | N_Full_Type_Declaration
23242 | N_Function_Specification
23243 | N_Generic_Declaration
23244 | N_Generic_Instantiation
23245 | N_Implicit_Label_Declaration
23246 | N_Incomplete_Type_Declaration
23247 | N_Loop_Parameter_Specification
23248 | N_Number_Declaration
23249 | N_Object_Declaration
23250 | N_Package_Declaration
23251 | N_Package_Specification
23252 | N_Parameter_Specification
23253 | N_Private_Extension_Declaration
23254 | N_Private_Type_Declaration
23255 | N_Procedure_Specification
23256 | N_Proper_Body
23257 | N_Protected_Type_Declaration
23258 | N_Renaming_Declaration
23259 | N_Single_Protected_Declaration
23260 | N_Single_Task_Declaration
23261 | N_Subprogram_Declaration
23262 | N_Subtype_Declaration
23263 | N_Subunit
23264 | N_Task_Type_Declaration
23265 =>
23266 return Scope_Depth
23267 (Nearest_Dynamic_Scope
23268 (Defining_Entity (Node_Par)));
23269
23270 -- For a return statement within a function, return
23271 -- the depth of the function itself. This is not just
23272 -- a small optimization, but matters when analyzing
23273 -- the expression in an expression function before
23274 -- the body is created.
23275
23276 when N_Simple_Return_Statement =>
23277 if Ekind (Current_Scope) = E_Function then
23278 return Scope_Depth (Current_Scope);
23279 end if;
23280
23281 when others =>
23282 null;
23283 end case;
23284
23285 Node_Par := Parent (Node_Par);
23286 end loop;
23287
23288 pragma Assert (False);
23289
23290 -- Should never reach the following return
23291
23292 return Scope_Depth (Current_Scope) + 1;
23293 end Innermost_Master_Scope_Depth;
23294
23295 -- Start of processing for Return_Master_Scope_Depth_Of_Call
23296
23297 begin
23298 return Innermost_Master_Scope_Depth (Obj);
23299 end Return_Master_Scope_Depth_Of_Call;
23300 end if;
23301
23302 -- For convenience we handle qualified expressions, even though they
23303 -- aren't technically object names.
23304
23305 elsif Nkind (Obj) = N_Qualified_Expression then
23306 return Object_Access_Level (Expression (Obj));
23307
23308 -- Ditto for aggregates. They have the level of the temporary that
23309 -- will hold their value.
23310
23311 elsif Nkind (Obj) = N_Aggregate then
23312 return Object_Access_Level (Current_Scope);
23313
23314 -- Otherwise return the scope level of Standard. (If there are cases
23315 -- that fall through to this point they will be treated as having
23316 -- global accessibility for now. ???)
23317
23318 else
23319 return Scope_Depth (Standard_Standard);
23320 end if;
23321 end Object_Access_Level;
23322
23323 ----------------------------------
23324 -- Old_Requires_Transient_Scope --
23325 ----------------------------------
23326
23327 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
23328 Typ : constant Entity_Id := Underlying_Type (Id);
23329
23330 begin
23331 -- This is a private type which is not completed yet. This can only
23332 -- happen in a default expression (of a formal parameter or of a
23333 -- record component). Do not expand transient scope in this case.
23334
23335 if No (Typ) then
23336 return False;
23337
23338 -- Do not expand transient scope for non-existent procedure return
23339
23340 elsif Typ = Standard_Void_Type then
23341 return False;
23342
23343 -- Elementary types do not require a transient scope
23344
23345 elsif Is_Elementary_Type (Typ) then
23346 return False;
23347
23348 -- Generally, indefinite subtypes require a transient scope, since the
23349 -- back end cannot generate temporaries, since this is not a valid type
23350 -- for declaring an object. It might be possible to relax this in the
23351 -- future, e.g. by declaring the maximum possible space for the type.
23352
23353 elsif not Is_Definite_Subtype (Typ) then
23354 return True;
23355
23356 -- Functions returning tagged types may dispatch on result so their
23357 -- returned value is allocated on the secondary stack. Controlled
23358 -- type temporaries need finalization.
23359
23360 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
23361 return True;
23362
23363 -- Record type
23364
23365 elsif Is_Record_Type (Typ) then
23366 declare
23367 Comp : Entity_Id;
23368
23369 begin
23370 Comp := First_Entity (Typ);
23371 while Present (Comp) loop
23372 if Ekind (Comp) = E_Component then
23373
23374 -- ???It's not clear we need a full recursive call to
23375 -- Old_Requires_Transient_Scope here. Note that the
23376 -- following can't happen.
23377
23378 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
23379 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
23380
23381 if Old_Requires_Transient_Scope (Etype (Comp)) then
23382 return True;
23383 end if;
23384 end if;
23385
23386 Next_Entity (Comp);
23387 end loop;
23388 end;
23389
23390 return False;
23391
23392 -- String literal types never require transient scope
23393
23394 elsif Ekind (Typ) = E_String_Literal_Subtype then
23395 return False;
23396
23397 -- Array type. Note that we already know that this is a constrained
23398 -- array, since unconstrained arrays will fail the indefinite test.
23399
23400 elsif Is_Array_Type (Typ) then
23401
23402 -- If component type requires a transient scope, the array does too
23403
23404 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
23405 return True;
23406
23407 -- Otherwise, we only need a transient scope if the size depends on
23408 -- the value of one or more discriminants.
23409
23410 else
23411 return Size_Depends_On_Discriminant (Typ);
23412 end if;
23413
23414 -- All other cases do not require a transient scope
23415
23416 else
23417 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
23418 return False;
23419 end if;
23420 end Old_Requires_Transient_Scope;
23421
23422 ---------------------------------
23423 -- Original_Aspect_Pragma_Name --
23424 ---------------------------------
23425
23426 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
23427 Item : Node_Id;
23428 Item_Nam : Name_Id;
23429
23430 begin
23431 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
23432
23433 Item := N;
23434
23435 -- The pragma was generated to emulate an aspect, use the original
23436 -- aspect specification.
23437
23438 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
23439 Item := Corresponding_Aspect (Item);
23440 end if;
23441
23442 -- Retrieve the name of the aspect/pragma. As assertion pragmas from
23443 -- a generic instantiation might have been rewritten into pragma Check,
23444 -- we look at the original node for Item. Note also that Pre, Pre_Class,
23445 -- Post and Post_Class rewrite their pragma identifier to preserve the
23446 -- original name, so we look at the original node for the identifier.
23447 -- ??? this is kludgey
23448
23449 if Nkind (Item) = N_Pragma then
23450 Item_Nam :=
23451 Chars (Original_Node (Pragma_Identifier (Original_Node (Item))));
23452
23453 else
23454 pragma Assert (Nkind (Item) = N_Aspect_Specification);
23455 Item_Nam := Chars (Identifier (Item));
23456 end if;
23457
23458 -- Deal with 'Class by converting the name to its _XXX form
23459
23460 if Class_Present (Item) then
23461 if Item_Nam = Name_Invariant then
23462 Item_Nam := Name_uInvariant;
23463
23464 elsif Item_Nam = Name_Post then
23465 Item_Nam := Name_uPost;
23466
23467 elsif Item_Nam = Name_Pre then
23468 Item_Nam := Name_uPre;
23469
23470 elsif Nam_In (Item_Nam, Name_Type_Invariant,
23471 Name_Type_Invariant_Class)
23472 then
23473 Item_Nam := Name_uType_Invariant;
23474
23475 -- Nothing to do for other cases (e.g. a Check that derived from
23476 -- Pre_Class and has the flag set). Also we do nothing if the name
23477 -- is already in special _xxx form.
23478
23479 end if;
23480 end if;
23481
23482 return Item_Nam;
23483 end Original_Aspect_Pragma_Name;
23484
23485 --------------------------------------
23486 -- Original_Corresponding_Operation --
23487 --------------------------------------
23488
23489 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
23490 is
23491 Typ : constant Entity_Id := Find_Dispatching_Type (S);
23492
23493 begin
23494 -- If S is an inherited primitive S2 the original corresponding
23495 -- operation of S is the original corresponding operation of S2
23496
23497 if Present (Alias (S))
23498 and then Find_Dispatching_Type (Alias (S)) /= Typ
23499 then
23500 return Original_Corresponding_Operation (Alias (S));
23501
23502 -- If S overrides an inherited subprogram S2 the original corresponding
23503 -- operation of S is the original corresponding operation of S2
23504
23505 elsif Present (Overridden_Operation (S)) then
23506 return Original_Corresponding_Operation (Overridden_Operation (S));
23507
23508 -- otherwise it is S itself
23509
23510 else
23511 return S;
23512 end if;
23513 end Original_Corresponding_Operation;
23514
23515 -------------------
23516 -- Output_Entity --
23517 -------------------
23518
23519 procedure Output_Entity (Id : Entity_Id) is
23520 Scop : Entity_Id;
23521
23522 begin
23523 Scop := Scope (Id);
23524
23525 -- The entity may lack a scope when it is in the process of being
23526 -- analyzed. Use the current scope as an approximation.
23527
23528 if No (Scop) then
23529 Scop := Current_Scope;
23530 end if;
23531
23532 Output_Name (Chars (Id), Scop);
23533 end Output_Entity;
23534
23535 -----------------
23536 -- Output_Name --
23537 -----------------
23538
23539 procedure Output_Name (Nam : Name_Id; Scop : Entity_Id := Current_Scope) is
23540 begin
23541 Write_Str
23542 (Get_Name_String
23543 (Get_Qualified_Name
23544 (Nam => Nam,
23545 Suffix => No_Name,
23546 Scop => Scop)));
23547 Write_Eol;
23548 end Output_Name;
23549
23550 ----------------------
23551 -- Policy_In_Effect --
23552 ----------------------
23553
23554 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
23555 function Policy_In_List (List : Node_Id) return Name_Id;
23556 -- Determine the mode of a policy in a N_Pragma list
23557
23558 --------------------
23559 -- Policy_In_List --
23560 --------------------
23561
23562 function Policy_In_List (List : Node_Id) return Name_Id is
23563 Arg1 : Node_Id;
23564 Arg2 : Node_Id;
23565 Prag : Node_Id;
23566
23567 begin
23568 Prag := List;
23569 while Present (Prag) loop
23570 Arg1 := First (Pragma_Argument_Associations (Prag));
23571 Arg2 := Next (Arg1);
23572
23573 Arg1 := Get_Pragma_Arg (Arg1);
23574 Arg2 := Get_Pragma_Arg (Arg2);
23575
23576 -- The current Check_Policy pragma matches the requested policy or
23577 -- appears in the single argument form (Assertion, policy_id).
23578
23579 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
23580 return Chars (Arg2);
23581 end if;
23582
23583 Prag := Next_Pragma (Prag);
23584 end loop;
23585
23586 return No_Name;
23587 end Policy_In_List;
23588
23589 -- Local variables
23590
23591 Kind : Name_Id;
23592
23593 -- Start of processing for Policy_In_Effect
23594
23595 begin
23596 if not Is_Valid_Assertion_Kind (Policy) then
23597 raise Program_Error;
23598 end if;
23599
23600 -- Inspect all policy pragmas that appear within scopes (if any)
23601
23602 Kind := Policy_In_List (Check_Policy_List);
23603
23604 -- Inspect all configuration policy pragmas (if any)
23605
23606 if Kind = No_Name then
23607 Kind := Policy_In_List (Check_Policy_List_Config);
23608 end if;
23609
23610 -- The context lacks policy pragmas, determine the mode based on whether
23611 -- assertions are enabled at the configuration level. This ensures that
23612 -- the policy is preserved when analyzing generics.
23613
23614 if Kind = No_Name then
23615 if Assertions_Enabled_Config then
23616 Kind := Name_Check;
23617 else
23618 Kind := Name_Ignore;
23619 end if;
23620 end if;
23621
23622 -- In CodePeer mode and GNATprove mode, we need to consider all
23623 -- assertions, unless they are disabled. Force Name_Check on
23624 -- ignored assertions.
23625
23626 if Nam_In (Kind, Name_Ignore, Name_Off)
23627 and then (CodePeer_Mode or GNATprove_Mode)
23628 then
23629 Kind := Name_Check;
23630 end if;
23631
23632 return Kind;
23633 end Policy_In_Effect;
23634
23635 ----------------------------------
23636 -- Predicate_Tests_On_Arguments --
23637 ----------------------------------
23638
23639 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
23640 begin
23641 -- Always test predicates on indirect call
23642
23643 if Ekind (Subp) = E_Subprogram_Type then
23644 return True;
23645
23646 -- Do not test predicates on call to generated default Finalize, since
23647 -- we are not interested in whether something we are finalizing (and
23648 -- typically destroying) satisfies its predicates.
23649
23650 elsif Chars (Subp) = Name_Finalize
23651 and then not Comes_From_Source (Subp)
23652 then
23653 return False;
23654
23655 -- Do not test predicates on any internally generated routines
23656
23657 elsif Is_Internal_Name (Chars (Subp)) then
23658 return False;
23659
23660 -- Do not test predicates on call to Init_Proc, since if needed the
23661 -- predicate test will occur at some other point.
23662
23663 elsif Is_Init_Proc (Subp) then
23664 return False;
23665
23666 -- Do not test predicates on call to predicate function, since this
23667 -- would cause infinite recursion.
23668
23669 elsif Ekind (Subp) = E_Function
23670 and then (Is_Predicate_Function (Subp)
23671 or else
23672 Is_Predicate_Function_M (Subp))
23673 then
23674 return False;
23675
23676 -- For now, no other exceptions
23677
23678 else
23679 return True;
23680 end if;
23681 end Predicate_Tests_On_Arguments;
23682
23683 -----------------------
23684 -- Private_Component --
23685 -----------------------
23686
23687 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
23688 Ancestor : constant Entity_Id := Base_Type (Type_Id);
23689
23690 function Trace_Components
23691 (T : Entity_Id;
23692 Check : Boolean) return Entity_Id;
23693 -- Recursive function that does the work, and checks against circular
23694 -- definition for each subcomponent type.
23695
23696 ----------------------
23697 -- Trace_Components --
23698 ----------------------
23699
23700 function Trace_Components
23701 (T : Entity_Id;
23702 Check : Boolean) return Entity_Id
23703 is
23704 Btype : constant Entity_Id := Base_Type (T);
23705 Component : Entity_Id;
23706 P : Entity_Id;
23707 Candidate : Entity_Id := Empty;
23708
23709 begin
23710 if Check and then Btype = Ancestor then
23711 Error_Msg_N ("circular type definition", Type_Id);
23712 return Any_Type;
23713 end if;
23714
23715 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
23716 if Present (Full_View (Btype))
23717 and then Is_Record_Type (Full_View (Btype))
23718 and then not Is_Frozen (Btype)
23719 then
23720 -- To indicate that the ancestor depends on a private type, the
23721 -- current Btype is sufficient. However, to check for circular
23722 -- definition we must recurse on the full view.
23723
23724 Candidate := Trace_Components (Full_View (Btype), True);
23725
23726 if Candidate = Any_Type then
23727 return Any_Type;
23728 else
23729 return Btype;
23730 end if;
23731
23732 else
23733 return Btype;
23734 end if;
23735
23736 elsif Is_Array_Type (Btype) then
23737 return Trace_Components (Component_Type (Btype), True);
23738
23739 elsif Is_Record_Type (Btype) then
23740 Component := First_Entity (Btype);
23741 while Present (Component)
23742 and then Comes_From_Source (Component)
23743 loop
23744 -- Skip anonymous types generated by constrained components
23745
23746 if not Is_Type (Component) then
23747 P := Trace_Components (Etype (Component), True);
23748
23749 if Present (P) then
23750 if P = Any_Type then
23751 return P;
23752 else
23753 Candidate := P;
23754 end if;
23755 end if;
23756 end if;
23757
23758 Next_Entity (Component);
23759 end loop;
23760
23761 return Candidate;
23762
23763 else
23764 return Empty;
23765 end if;
23766 end Trace_Components;
23767
23768 -- Start of processing for Private_Component
23769
23770 begin
23771 return Trace_Components (Type_Id, False);
23772 end Private_Component;
23773
23774 ---------------------------
23775 -- Primitive_Names_Match --
23776 ---------------------------
23777
23778 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
23779 function Non_Internal_Name (E : Entity_Id) return Name_Id;
23780 -- Given an internal name, returns the corresponding non-internal name
23781
23782 ------------------------
23783 -- Non_Internal_Name --
23784 ------------------------
23785
23786 function Non_Internal_Name (E : Entity_Id) return Name_Id is
23787 begin
23788 Get_Name_String (Chars (E));
23789 Name_Len := Name_Len - 1;
23790 return Name_Find;
23791 end Non_Internal_Name;
23792
23793 -- Start of processing for Primitive_Names_Match
23794
23795 begin
23796 pragma Assert (Present (E1) and then Present (E2));
23797
23798 return Chars (E1) = Chars (E2)
23799 or else
23800 (not Is_Internal_Name (Chars (E1))
23801 and then Is_Internal_Name (Chars (E2))
23802 and then Non_Internal_Name (E2) = Chars (E1))
23803 or else
23804 (not Is_Internal_Name (Chars (E2))
23805 and then Is_Internal_Name (Chars (E1))
23806 and then Non_Internal_Name (E1) = Chars (E2))
23807 or else
23808 (Is_Predefined_Dispatching_Operation (E1)
23809 and then Is_Predefined_Dispatching_Operation (E2)
23810 and then Same_TSS (E1, E2))
23811 or else
23812 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
23813 end Primitive_Names_Match;
23814
23815 -----------------------
23816 -- Process_End_Label --
23817 -----------------------
23818
23819 procedure Process_End_Label
23820 (N : Node_Id;
23821 Typ : Character;
23822 Ent : Entity_Id)
23823 is
23824 Loc : Source_Ptr;
23825 Nam : Node_Id;
23826 Scop : Entity_Id;
23827
23828 Label_Ref : Boolean;
23829 -- Set True if reference to end label itself is required
23830
23831 Endl : Node_Id;
23832 -- Gets set to the operator symbol or identifier that references the
23833 -- entity Ent. For the child unit case, this is the identifier from the
23834 -- designator. For other cases, this is simply Endl.
23835
23836 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
23837 -- N is an identifier node that appears as a parent unit reference in
23838 -- the case where Ent is a child unit. This procedure generates an
23839 -- appropriate cross-reference entry. E is the corresponding entity.
23840
23841 -------------------------
23842 -- Generate_Parent_Ref --
23843 -------------------------
23844
23845 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
23846 begin
23847 -- If names do not match, something weird, skip reference
23848
23849 if Chars (E) = Chars (N) then
23850
23851 -- Generate the reference. We do NOT consider this as a reference
23852 -- for unreferenced symbol purposes.
23853
23854 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
23855
23856 if Style_Check then
23857 Style.Check_Identifier (N, E);
23858 end if;
23859 end if;
23860 end Generate_Parent_Ref;
23861
23862 -- Start of processing for Process_End_Label
23863
23864 begin
23865 -- If no node, ignore. This happens in some error situations, and
23866 -- also for some internally generated structures where no end label
23867 -- references are required in any case.
23868
23869 if No (N) then
23870 return;
23871 end if;
23872
23873 -- Nothing to do if no End_Label, happens for internally generated
23874 -- constructs where we don't want an end label reference anyway. Also
23875 -- nothing to do if Endl is a string literal, which means there was
23876 -- some prior error (bad operator symbol)
23877
23878 Endl := End_Label (N);
23879
23880 if No (Endl) or else Nkind (Endl) = N_String_Literal then
23881 return;
23882 end if;
23883
23884 -- Reference node is not in extended main source unit
23885
23886 if not In_Extended_Main_Source_Unit (N) then
23887
23888 -- Generally we do not collect references except for the extended
23889 -- main source unit. The one exception is the 'e' entry for a
23890 -- package spec, where it is useful for a client to have the
23891 -- ending information to define scopes.
23892
23893 if Typ /= 'e' then
23894 return;
23895
23896 else
23897 Label_Ref := False;
23898
23899 -- For this case, we can ignore any parent references, but we
23900 -- need the package name itself for the 'e' entry.
23901
23902 if Nkind (Endl) = N_Designator then
23903 Endl := Identifier (Endl);
23904 end if;
23905 end if;
23906
23907 -- Reference is in extended main source unit
23908
23909 else
23910 Label_Ref := True;
23911
23912 -- For designator, generate references for the parent entries
23913
23914 if Nkind (Endl) = N_Designator then
23915
23916 -- Generate references for the prefix if the END line comes from
23917 -- source (otherwise we do not need these references) We climb the
23918 -- scope stack to find the expected entities.
23919
23920 if Comes_From_Source (Endl) then
23921 Nam := Name (Endl);
23922 Scop := Current_Scope;
23923 while Nkind (Nam) = N_Selected_Component loop
23924 Scop := Scope (Scop);
23925 exit when No (Scop);
23926 Generate_Parent_Ref (Selector_Name (Nam), Scop);
23927 Nam := Prefix (Nam);
23928 end loop;
23929
23930 if Present (Scop) then
23931 Generate_Parent_Ref (Nam, Scope (Scop));
23932 end if;
23933 end if;
23934
23935 Endl := Identifier (Endl);
23936 end if;
23937 end if;
23938
23939 -- If the end label is not for the given entity, then either we have
23940 -- some previous error, or this is a generic instantiation for which
23941 -- we do not need to make a cross-reference in this case anyway. In
23942 -- either case we simply ignore the call.
23943
23944 if Chars (Ent) /= Chars (Endl) then
23945 return;
23946 end if;
23947
23948 -- If label was really there, then generate a normal reference and then
23949 -- adjust the location in the end label to point past the name (which
23950 -- should almost always be the semicolon).
23951
23952 Loc := Sloc (Endl);
23953
23954 if Comes_From_Source (Endl) then
23955
23956 -- If a label reference is required, then do the style check and
23957 -- generate an l-type cross-reference entry for the label
23958
23959 if Label_Ref then
23960 if Style_Check then
23961 Style.Check_Identifier (Endl, Ent);
23962 end if;
23963
23964 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
23965 end if;
23966
23967 -- Set the location to point past the label (normally this will
23968 -- mean the semicolon immediately following the label). This is
23969 -- done for the sake of the 'e' or 't' entry generated below.
23970
23971 Get_Decoded_Name_String (Chars (Endl));
23972 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
23973
23974 else
23975 -- In SPARK mode, no missing label is allowed for packages and
23976 -- subprogram bodies. Detect those cases by testing whether
23977 -- Process_End_Label was called for a body (Typ = 't') or a package.
23978
23979 if Restriction_Check_Required (SPARK_05)
23980 and then (Typ = 't' or else Ekind (Ent) = E_Package)
23981 then
23982 Error_Msg_Node_1 := Endl;
23983 Check_SPARK_05_Restriction
23984 ("`END &` required", Endl, Force => True);
23985 end if;
23986 end if;
23987
23988 -- Now generate the e/t reference
23989
23990 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
23991
23992 -- Restore Sloc, in case modified above, since we have an identifier
23993 -- and the normal Sloc should be left set in the tree.
23994
23995 Set_Sloc (Endl, Loc);
23996 end Process_End_Label;
23997
23998 --------------------------------
23999 -- Propagate_Concurrent_Flags --
24000 --------------------------------
24001
24002 procedure Propagate_Concurrent_Flags
24003 (Typ : Entity_Id;
24004 Comp_Typ : Entity_Id)
24005 is
24006 begin
24007 if Has_Task (Comp_Typ) then
24008 Set_Has_Task (Typ);
24009 end if;
24010
24011 if Has_Protected (Comp_Typ) then
24012 Set_Has_Protected (Typ);
24013 end if;
24014
24015 if Has_Timing_Event (Comp_Typ) then
24016 Set_Has_Timing_Event (Typ);
24017 end if;
24018 end Propagate_Concurrent_Flags;
24019
24020 ------------------------------
24021 -- Propagate_DIC_Attributes --
24022 ------------------------------
24023
24024 procedure Propagate_DIC_Attributes
24025 (Typ : Entity_Id;
24026 From_Typ : Entity_Id)
24027 is
24028 DIC_Proc : Entity_Id;
24029
24030 begin
24031 if Present (Typ) and then Present (From_Typ) then
24032 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
24033
24034 -- Nothing to do if both the source and the destination denote the
24035 -- same type.
24036
24037 if From_Typ = Typ then
24038 return;
24039
24040 -- Nothing to do when the destination denotes an incomplete type
24041 -- because the DIC is associated with the current instance of a
24042 -- private type, thus it can never apply to an incomplete type.
24043
24044 elsif Is_Incomplete_Type (Typ) then
24045 return;
24046 end if;
24047
24048 DIC_Proc := DIC_Procedure (From_Typ);
24049
24050 -- The setting of the attributes is intentionally conservative. This
24051 -- prevents accidental clobbering of enabled attributes.
24052
24053 if Has_Inherited_DIC (From_Typ)
24054 and then not Has_Inherited_DIC (Typ)
24055 then
24056 Set_Has_Inherited_DIC (Typ);
24057 end if;
24058
24059 if Has_Own_DIC (From_Typ) and then not Has_Own_DIC (Typ) then
24060 Set_Has_Own_DIC (Typ);
24061 end if;
24062
24063 if Present (DIC_Proc) and then No (DIC_Procedure (Typ)) then
24064 Set_DIC_Procedure (Typ, DIC_Proc);
24065 end if;
24066 end if;
24067 end Propagate_DIC_Attributes;
24068
24069 ------------------------------------
24070 -- Propagate_Invariant_Attributes --
24071 ------------------------------------
24072
24073 procedure Propagate_Invariant_Attributes
24074 (Typ : Entity_Id;
24075 From_Typ : Entity_Id)
24076 is
24077 Full_IP : Entity_Id;
24078 Part_IP : Entity_Id;
24079
24080 begin
24081 if Present (Typ) and then Present (From_Typ) then
24082 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
24083
24084 -- Nothing to do if both the source and the destination denote the
24085 -- same type.
24086
24087 if From_Typ = Typ then
24088 return;
24089 end if;
24090
24091 Full_IP := Invariant_Procedure (From_Typ);
24092 Part_IP := Partial_Invariant_Procedure (From_Typ);
24093
24094 -- The setting of the attributes is intentionally conservative. This
24095 -- prevents accidental clobbering of enabled attributes.
24096
24097 if Has_Inheritable_Invariants (From_Typ)
24098 and then not Has_Inheritable_Invariants (Typ)
24099 then
24100 Set_Has_Inheritable_Invariants (Typ);
24101 end if;
24102
24103 if Has_Inherited_Invariants (From_Typ)
24104 and then not Has_Inherited_Invariants (Typ)
24105 then
24106 Set_Has_Inherited_Invariants (Typ);
24107 end if;
24108
24109 if Has_Own_Invariants (From_Typ)
24110 and then not Has_Own_Invariants (Typ)
24111 then
24112 Set_Has_Own_Invariants (Typ);
24113 end if;
24114
24115 if Present (Full_IP) and then No (Invariant_Procedure (Typ)) then
24116 Set_Invariant_Procedure (Typ, Full_IP);
24117 end if;
24118
24119 if Present (Part_IP) and then No (Partial_Invariant_Procedure (Typ))
24120 then
24121 Set_Partial_Invariant_Procedure (Typ, Part_IP);
24122 end if;
24123 end if;
24124 end Propagate_Invariant_Attributes;
24125
24126 ---------------------------------------
24127 -- Record_Possible_Part_Of_Reference --
24128 ---------------------------------------
24129
24130 procedure Record_Possible_Part_Of_Reference
24131 (Var_Id : Entity_Id;
24132 Ref : Node_Id)
24133 is
24134 Encap : constant Entity_Id := Encapsulating_State (Var_Id);
24135 Refs : Elist_Id;
24136
24137 begin
24138 -- The variable is a constituent of a single protected/task type. Such
24139 -- a variable acts as a component of the type and must appear within a
24140 -- specific region (SPARK RM 9(3)). Instead of recording the reference,
24141 -- verify its legality now.
24142
24143 if Present (Encap) and then Is_Single_Concurrent_Object (Encap) then
24144 Check_Part_Of_Reference (Var_Id, Ref);
24145
24146 -- The variable is subject to pragma Part_Of and may eventually become a
24147 -- constituent of a single protected/task type. Record the reference to
24148 -- verify its placement when the contract of the variable is analyzed.
24149
24150 elsif Present (Get_Pragma (Var_Id, Pragma_Part_Of)) then
24151 Refs := Part_Of_References (Var_Id);
24152
24153 if No (Refs) then
24154 Refs := New_Elmt_List;
24155 Set_Part_Of_References (Var_Id, Refs);
24156 end if;
24157
24158 Append_Elmt (Ref, Refs);
24159 end if;
24160 end Record_Possible_Part_Of_Reference;
24161
24162 ----------------
24163 -- Referenced --
24164 ----------------
24165
24166 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
24167 Seen : Boolean := False;
24168
24169 function Is_Reference (N : Node_Id) return Traverse_Result;
24170 -- Determine whether node N denotes a reference to Id. If this is the
24171 -- case, set global flag Seen to True and stop the traversal.
24172
24173 ------------------
24174 -- Is_Reference --
24175 ------------------
24176
24177 function Is_Reference (N : Node_Id) return Traverse_Result is
24178 begin
24179 if Is_Entity_Name (N)
24180 and then Present (Entity (N))
24181 and then Entity (N) = Id
24182 then
24183 Seen := True;
24184 return Abandon;
24185 else
24186 return OK;
24187 end if;
24188 end Is_Reference;
24189
24190 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
24191
24192 -- Start of processing for Referenced
24193
24194 begin
24195 Inspect_Expression (Expr);
24196 return Seen;
24197 end Referenced;
24198
24199 ------------------------------------
24200 -- References_Generic_Formal_Type --
24201 ------------------------------------
24202
24203 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
24204
24205 function Process (N : Node_Id) return Traverse_Result;
24206 -- Process one node in search for generic formal type
24207
24208 -------------
24209 -- Process --
24210 -------------
24211
24212 function Process (N : Node_Id) return Traverse_Result is
24213 begin
24214 if Nkind (N) in N_Has_Entity then
24215 declare
24216 E : constant Entity_Id := Entity (N);
24217 begin
24218 if Present (E) then
24219 if Is_Generic_Type (E) then
24220 return Abandon;
24221 elsif Present (Etype (E))
24222 and then Is_Generic_Type (Etype (E))
24223 then
24224 return Abandon;
24225 end if;
24226 end if;
24227 end;
24228 end if;
24229
24230 return Atree.OK;
24231 end Process;
24232
24233 function Traverse is new Traverse_Func (Process);
24234 -- Traverse tree to look for generic type
24235
24236 begin
24237 if Inside_A_Generic then
24238 return Traverse (N) = Abandon;
24239 else
24240 return False;
24241 end if;
24242 end References_Generic_Formal_Type;
24243
24244 -------------------------------
24245 -- Remove_Entity_And_Homonym --
24246 -------------------------------
24247
24248 procedure Remove_Entity_And_Homonym (Id : Entity_Id) is
24249 begin
24250 Remove_Entity (Id);
24251 Remove_Homonym (Id);
24252 end Remove_Entity_And_Homonym;
24253
24254 --------------------
24255 -- Remove_Homonym --
24256 --------------------
24257
24258 procedure Remove_Homonym (Id : Entity_Id) is
24259 Hom : Entity_Id;
24260 Prev : Entity_Id := Empty;
24261
24262 begin
24263 if Id = Current_Entity (Id) then
24264 if Present (Homonym (Id)) then
24265 Set_Current_Entity (Homonym (Id));
24266 else
24267 Set_Name_Entity_Id (Chars (Id), Empty);
24268 end if;
24269
24270 else
24271 Hom := Current_Entity (Id);
24272 while Present (Hom) and then Hom /= Id loop
24273 Prev := Hom;
24274 Hom := Homonym (Hom);
24275 end loop;
24276
24277 -- If Id is not on the homonym chain, nothing to do
24278
24279 if Present (Hom) then
24280 Set_Homonym (Prev, Homonym (Id));
24281 end if;
24282 end if;
24283 end Remove_Homonym;
24284
24285 ------------------------------
24286 -- Remove_Overloaded_Entity --
24287 ------------------------------
24288
24289 procedure Remove_Overloaded_Entity (Id : Entity_Id) is
24290 procedure Remove_Primitive_Of (Typ : Entity_Id);
24291 -- Remove primitive subprogram Id from the list of primitives that
24292 -- belong to type Typ.
24293
24294 -------------------------
24295 -- Remove_Primitive_Of --
24296 -------------------------
24297
24298 procedure Remove_Primitive_Of (Typ : Entity_Id) is
24299 Prims : Elist_Id;
24300
24301 begin
24302 if Is_Tagged_Type (Typ) then
24303 Prims := Direct_Primitive_Operations (Typ);
24304
24305 if Present (Prims) then
24306 Remove (Prims, Id);
24307 end if;
24308 end if;
24309 end Remove_Primitive_Of;
24310
24311 -- Local variables
24312
24313 Formal : Entity_Id;
24314
24315 -- Start of processing for Remove_Overloaded_Entity
24316
24317 begin
24318 Remove_Entity_And_Homonym (Id);
24319
24320 -- The entity denotes a primitive subprogram. Remove it from the list of
24321 -- primitives of the associated controlling type.
24322
24323 if Ekind_In (Id, E_Function, E_Procedure) and then Is_Primitive (Id) then
24324 Formal := First_Formal (Id);
24325 while Present (Formal) loop
24326 if Is_Controlling_Formal (Formal) then
24327 Remove_Primitive_Of (Etype (Formal));
24328 exit;
24329 end if;
24330
24331 Next_Formal (Formal);
24332 end loop;
24333
24334 if Ekind (Id) = E_Function and then Has_Controlling_Result (Id) then
24335 Remove_Primitive_Of (Etype (Id));
24336 end if;
24337 end if;
24338 end Remove_Overloaded_Entity;
24339
24340 ---------------------
24341 -- Rep_To_Pos_Flag --
24342 ---------------------
24343
24344 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
24345 begin
24346 return New_Occurrence_Of
24347 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
24348 end Rep_To_Pos_Flag;
24349
24350 --------------------
24351 -- Require_Entity --
24352 --------------------
24353
24354 procedure Require_Entity (N : Node_Id) is
24355 begin
24356 if Is_Entity_Name (N) and then No (Entity (N)) then
24357 if Total_Errors_Detected /= 0 then
24358 Set_Entity (N, Any_Id);
24359 else
24360 raise Program_Error;
24361 end if;
24362 end if;
24363 end Require_Entity;
24364
24365 ------------------------------
24366 -- Requires_Transient_Scope --
24367 ------------------------------
24368
24369 -- A transient scope is required when variable-sized temporaries are
24370 -- allocated on the secondary stack, or when finalization actions must be
24371 -- generated before the next instruction.
24372
24373 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
24374 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
24375
24376 begin
24377 if Debug_Flag_QQ then
24378 return Old_Result;
24379 end if;
24380
24381 declare
24382 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
24383
24384 begin
24385 -- Assert that we're not putting things on the secondary stack if we
24386 -- didn't before; we are trying to AVOID secondary stack when
24387 -- possible.
24388
24389 if not Old_Result then
24390 pragma Assert (not New_Result);
24391 null;
24392 end if;
24393
24394 if New_Result /= Old_Result then
24395 Results_Differ (Id, Old_Result, New_Result);
24396 end if;
24397
24398 return New_Result;
24399 end;
24400 end Requires_Transient_Scope;
24401
24402 --------------------
24403 -- Results_Differ --
24404 --------------------
24405
24406 procedure Results_Differ
24407 (Id : Entity_Id;
24408 Old_Val : Boolean;
24409 New_Val : Boolean)
24410 is
24411 begin
24412 if False then -- False to disable; True for debugging
24413 Treepr.Print_Tree_Node (Id);
24414
24415 if Old_Val = New_Val then
24416 raise Program_Error;
24417 end if;
24418 end if;
24419 end Results_Differ;
24420
24421 --------------------------
24422 -- Reset_Analyzed_Flags --
24423 --------------------------
24424
24425 procedure Reset_Analyzed_Flags (N : Node_Id) is
24426 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
24427 -- Function used to reset Analyzed flags in tree. Note that we do
24428 -- not reset Analyzed flags in entities, since there is no need to
24429 -- reanalyze entities, and indeed, it is wrong to do so, since it
24430 -- can result in generating auxiliary stuff more than once.
24431
24432 --------------------
24433 -- Clear_Analyzed --
24434 --------------------
24435
24436 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
24437 begin
24438 if Nkind (N) not in N_Entity then
24439 Set_Analyzed (N, False);
24440 end if;
24441
24442 return OK;
24443 end Clear_Analyzed;
24444
24445 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
24446
24447 -- Start of processing for Reset_Analyzed_Flags
24448
24449 begin
24450 Reset_Analyzed (N);
24451 end Reset_Analyzed_Flags;
24452
24453 ------------------------
24454 -- Restore_SPARK_Mode --
24455 ------------------------
24456
24457 procedure Restore_SPARK_Mode
24458 (Mode : SPARK_Mode_Type;
24459 Prag : Node_Id)
24460 is
24461 begin
24462 SPARK_Mode := Mode;
24463 SPARK_Mode_Pragma := Prag;
24464 end Restore_SPARK_Mode;
24465
24466 --------------------------------
24467 -- Returns_Unconstrained_Type --
24468 --------------------------------
24469
24470 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
24471 begin
24472 return Ekind (Subp) = E_Function
24473 and then not Is_Scalar_Type (Etype (Subp))
24474 and then not Is_Access_Type (Etype (Subp))
24475 and then not Is_Constrained (Etype (Subp));
24476 end Returns_Unconstrained_Type;
24477
24478 ----------------------------
24479 -- Root_Type_Of_Full_View --
24480 ----------------------------
24481
24482 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
24483 Rtyp : constant Entity_Id := Root_Type (T);
24484
24485 begin
24486 -- The root type of the full view may itself be a private type. Keep
24487 -- looking for the ultimate derivation parent.
24488
24489 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
24490 return Root_Type_Of_Full_View (Full_View (Rtyp));
24491 else
24492 return Rtyp;
24493 end if;
24494 end Root_Type_Of_Full_View;
24495
24496 ---------------------------
24497 -- Safe_To_Capture_Value --
24498 ---------------------------
24499
24500 function Safe_To_Capture_Value
24501 (N : Node_Id;
24502 Ent : Entity_Id;
24503 Cond : Boolean := False) return Boolean
24504 is
24505 begin
24506 -- The only entities for which we track constant values are variables
24507 -- which are not renamings, constants, out parameters, and in out
24508 -- parameters, so check if we have this case.
24509
24510 -- Note: it may seem odd to track constant values for constants, but in
24511 -- fact this routine is used for other purposes than simply capturing
24512 -- the value. In particular, the setting of Known[_Non]_Null.
24513
24514 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
24515 or else
24516 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
24517 then
24518 null;
24519
24520 -- For conditionals, we also allow loop parameters and all formals,
24521 -- including in parameters.
24522
24523 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
24524 null;
24525
24526 -- For all other cases, not just unsafe, but impossible to capture
24527 -- Current_Value, since the above are the only entities which have
24528 -- Current_Value fields.
24529
24530 else
24531 return False;
24532 end if;
24533
24534 -- Skip if volatile or aliased, since funny things might be going on in
24535 -- these cases which we cannot necessarily track. Also skip any variable
24536 -- for which an address clause is given, or whose address is taken. Also
24537 -- never capture value of library level variables (an attempt to do so
24538 -- can occur in the case of package elaboration code).
24539
24540 if Treat_As_Volatile (Ent)
24541 or else Is_Aliased (Ent)
24542 or else Present (Address_Clause (Ent))
24543 or else Address_Taken (Ent)
24544 or else (Is_Library_Level_Entity (Ent)
24545 and then Ekind (Ent) = E_Variable)
24546 then
24547 return False;
24548 end if;
24549
24550 -- OK, all above conditions are met. We also require that the scope of
24551 -- the reference be the same as the scope of the entity, not counting
24552 -- packages and blocks and loops.
24553
24554 declare
24555 E_Scope : constant Entity_Id := Scope (Ent);
24556 R_Scope : Entity_Id;
24557
24558 begin
24559 R_Scope := Current_Scope;
24560 while R_Scope /= Standard_Standard loop
24561 exit when R_Scope = E_Scope;
24562
24563 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
24564 return False;
24565 else
24566 R_Scope := Scope (R_Scope);
24567 end if;
24568 end loop;
24569 end;
24570
24571 -- We also require that the reference does not appear in a context
24572 -- where it is not sure to be executed (i.e. a conditional context
24573 -- or an exception handler). We skip this if Cond is True, since the
24574 -- capturing of values from conditional tests handles this ok.
24575
24576 if Cond then
24577 return True;
24578 end if;
24579
24580 declare
24581 Desc : Node_Id;
24582 P : Node_Id;
24583
24584 begin
24585 Desc := N;
24586
24587 -- Seems dubious that case expressions are not handled here ???
24588
24589 P := Parent (N);
24590 while Present (P) loop
24591 if Nkind (P) = N_If_Statement
24592 or else Nkind (P) = N_Case_Statement
24593 or else (Nkind (P) in N_Short_Circuit
24594 and then Desc = Right_Opnd (P))
24595 or else (Nkind (P) = N_If_Expression
24596 and then Desc /= First (Expressions (P)))
24597 or else Nkind (P) = N_Exception_Handler
24598 or else Nkind (P) = N_Selective_Accept
24599 or else Nkind (P) = N_Conditional_Entry_Call
24600 or else Nkind (P) = N_Timed_Entry_Call
24601 or else Nkind (P) = N_Asynchronous_Select
24602 then
24603 return False;
24604
24605 else
24606 Desc := P;
24607 P := Parent (P);
24608
24609 -- A special Ada 2012 case: the original node may be part
24610 -- of the else_actions of a conditional expression, in which
24611 -- case it might not have been expanded yet, and appears in
24612 -- a non-syntactic list of actions. In that case it is clearly
24613 -- not safe to save a value.
24614
24615 if No (P)
24616 and then Is_List_Member (Desc)
24617 and then No (Parent (List_Containing (Desc)))
24618 then
24619 return False;
24620 end if;
24621 end if;
24622 end loop;
24623 end;
24624
24625 -- OK, looks safe to set value
24626
24627 return True;
24628 end Safe_To_Capture_Value;
24629
24630 ---------------
24631 -- Same_Name --
24632 ---------------
24633
24634 function Same_Name (N1, N2 : Node_Id) return Boolean is
24635 K1 : constant Node_Kind := Nkind (N1);
24636 K2 : constant Node_Kind := Nkind (N2);
24637
24638 begin
24639 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
24640 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
24641 then
24642 return Chars (N1) = Chars (N2);
24643
24644 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
24645 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
24646 then
24647 return Same_Name (Selector_Name (N1), Selector_Name (N2))
24648 and then Same_Name (Prefix (N1), Prefix (N2));
24649
24650 else
24651 return False;
24652 end if;
24653 end Same_Name;
24654
24655 -----------------
24656 -- Same_Object --
24657 -----------------
24658
24659 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
24660 N1 : constant Node_Id := Original_Node (Node1);
24661 N2 : constant Node_Id := Original_Node (Node2);
24662 -- We do the tests on original nodes, since we are most interested
24663 -- in the original source, not any expansion that got in the way.
24664
24665 K1 : constant Node_Kind := Nkind (N1);
24666 K2 : constant Node_Kind := Nkind (N2);
24667
24668 begin
24669 -- First case, both are entities with same entity
24670
24671 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
24672 declare
24673 EN1 : constant Entity_Id := Entity (N1);
24674 EN2 : constant Entity_Id := Entity (N2);
24675 begin
24676 if Present (EN1) and then Present (EN2)
24677 and then (Ekind_In (EN1, E_Variable, E_Constant)
24678 or else Is_Formal (EN1))
24679 and then EN1 = EN2
24680 then
24681 return True;
24682 end if;
24683 end;
24684 end if;
24685
24686 -- Second case, selected component with same selector, same record
24687
24688 if K1 = N_Selected_Component
24689 and then K2 = N_Selected_Component
24690 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
24691 then
24692 return Same_Object (Prefix (N1), Prefix (N2));
24693
24694 -- Third case, indexed component with same subscripts, same array
24695
24696 elsif K1 = N_Indexed_Component
24697 and then K2 = N_Indexed_Component
24698 and then Same_Object (Prefix (N1), Prefix (N2))
24699 then
24700 declare
24701 E1, E2 : Node_Id;
24702 begin
24703 E1 := First (Expressions (N1));
24704 E2 := First (Expressions (N2));
24705 while Present (E1) loop
24706 if not Same_Value (E1, E2) then
24707 return False;
24708 else
24709 Next (E1);
24710 Next (E2);
24711 end if;
24712 end loop;
24713
24714 return True;
24715 end;
24716
24717 -- Fourth case, slice of same array with same bounds
24718
24719 elsif K1 = N_Slice
24720 and then K2 = N_Slice
24721 and then Nkind (Discrete_Range (N1)) = N_Range
24722 and then Nkind (Discrete_Range (N2)) = N_Range
24723 and then Same_Value (Low_Bound (Discrete_Range (N1)),
24724 Low_Bound (Discrete_Range (N2)))
24725 and then Same_Value (High_Bound (Discrete_Range (N1)),
24726 High_Bound (Discrete_Range (N2)))
24727 then
24728 return Same_Name (Prefix (N1), Prefix (N2));
24729
24730 -- All other cases, not clearly the same object
24731
24732 else
24733 return False;
24734 end if;
24735 end Same_Object;
24736
24737 ---------------
24738 -- Same_Type --
24739 ---------------
24740
24741 function Same_Type (T1, T2 : Entity_Id) return Boolean is
24742 begin
24743 if T1 = T2 then
24744 return True;
24745
24746 elsif not Is_Constrained (T1)
24747 and then not Is_Constrained (T2)
24748 and then Base_Type (T1) = Base_Type (T2)
24749 then
24750 return True;
24751
24752 -- For now don't bother with case of identical constraints, to be
24753 -- fiddled with later on perhaps (this is only used for optimization
24754 -- purposes, so it is not critical to do a best possible job)
24755
24756 else
24757 return False;
24758 end if;
24759 end Same_Type;
24760
24761 ----------------
24762 -- Same_Value --
24763 ----------------
24764
24765 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
24766 begin
24767 if Compile_Time_Known_Value (Node1)
24768 and then Compile_Time_Known_Value (Node2)
24769 then
24770 -- Handle properly compile-time expressions that are not
24771 -- scalar.
24772
24773 if Is_String_Type (Etype (Node1)) then
24774 return Expr_Value_S (Node1) = Expr_Value_S (Node2);
24775
24776 else
24777 return Expr_Value (Node1) = Expr_Value (Node2);
24778 end if;
24779
24780 elsif Same_Object (Node1, Node2) then
24781 return True;
24782 else
24783 return False;
24784 end if;
24785 end Same_Value;
24786
24787 --------------------
24788 -- Set_SPARK_Mode --
24789 --------------------
24790
24791 procedure Set_SPARK_Mode (Context : Entity_Id) is
24792 begin
24793 -- Do not consider illegal or partially decorated constructs
24794
24795 if Ekind (Context) = E_Void or else Error_Posted (Context) then
24796 null;
24797
24798 elsif Present (SPARK_Pragma (Context)) then
24799 Install_SPARK_Mode
24800 (Mode => Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Context)),
24801 Prag => SPARK_Pragma (Context));
24802 end if;
24803 end Set_SPARK_Mode;
24804
24805 -------------------------
24806 -- Scalar_Part_Present --
24807 -------------------------
24808
24809 function Scalar_Part_Present (Typ : Entity_Id) return Boolean is
24810 Val_Typ : constant Entity_Id := Validated_View (Typ);
24811 Field : Entity_Id;
24812
24813 begin
24814 if Is_Scalar_Type (Val_Typ) then
24815 return True;
24816
24817 elsif Is_Array_Type (Val_Typ) then
24818 return Scalar_Part_Present (Component_Type (Val_Typ));
24819
24820 elsif Is_Record_Type (Val_Typ) then
24821 Field := First_Component_Or_Discriminant (Val_Typ);
24822 while Present (Field) loop
24823 if Scalar_Part_Present (Etype (Field)) then
24824 return True;
24825 end if;
24826
24827 Next_Component_Or_Discriminant (Field);
24828 end loop;
24829 end if;
24830
24831 return False;
24832 end Scalar_Part_Present;
24833
24834 ------------------------
24835 -- Scope_Is_Transient --
24836 ------------------------
24837
24838 function Scope_Is_Transient return Boolean is
24839 begin
24840 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
24841 end Scope_Is_Transient;
24842
24843 ------------------
24844 -- Scope_Within --
24845 ------------------
24846
24847 function Scope_Within
24848 (Inner : Entity_Id;
24849 Outer : Entity_Id) return Boolean
24850 is
24851 Curr : Entity_Id;
24852
24853 begin
24854 Curr := Inner;
24855 while Present (Curr) and then Curr /= Standard_Standard loop
24856 Curr := Scope (Curr);
24857
24858 if Curr = Outer then
24859 return True;
24860
24861 -- A selective accept body appears within a task type, but the
24862 -- enclosing subprogram is the procedure of the task body.
24863
24864 elsif Ekind (Implementation_Base_Type (Curr)) = E_Task_Type
24865 and then
24866 Outer = Task_Body_Procedure (Implementation_Base_Type (Curr))
24867 then
24868 return True;
24869
24870 -- Ditto for the body of a protected operation
24871
24872 elsif Is_Subprogram (Curr)
24873 and then Outer = Protected_Body_Subprogram (Curr)
24874 then
24875 return True;
24876
24877 -- Outside of its scope, a synchronized type may just be private
24878
24879 elsif Is_Private_Type (Curr)
24880 and then Present (Full_View (Curr))
24881 and then Is_Concurrent_Type (Full_View (Curr))
24882 then
24883 return Scope_Within (Full_View (Curr), Outer);
24884 end if;
24885 end loop;
24886
24887 return False;
24888 end Scope_Within;
24889
24890 --------------------------
24891 -- Scope_Within_Or_Same --
24892 --------------------------
24893
24894 function Scope_Within_Or_Same
24895 (Inner : Entity_Id;
24896 Outer : Entity_Id) return Boolean
24897 is
24898 Curr : Entity_Id := Inner;
24899
24900 begin
24901 -- Similar to the above, but check for scope identity first
24902
24903 while Present (Curr) and then Curr /= Standard_Standard loop
24904 if Curr = Outer then
24905 return True;
24906
24907 elsif Ekind (Implementation_Base_Type (Curr)) = E_Task_Type
24908 and then
24909 Outer = Task_Body_Procedure (Implementation_Base_Type (Curr))
24910 then
24911 return True;
24912
24913 elsif Is_Subprogram (Curr)
24914 and then Outer = Protected_Body_Subprogram (Curr)
24915 then
24916 return True;
24917
24918 elsif Is_Private_Type (Curr)
24919 and then Present (Full_View (Curr))
24920 then
24921 if Full_View (Curr) = Outer then
24922 return True;
24923 else
24924 return Scope_Within (Full_View (Curr), Outer);
24925 end if;
24926 end if;
24927
24928 Curr := Scope (Curr);
24929 end loop;
24930
24931 return False;
24932 end Scope_Within_Or_Same;
24933
24934 --------------------
24935 -- Set_Convention --
24936 --------------------
24937
24938 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
24939 begin
24940 Basic_Set_Convention (E, Val);
24941
24942 if Is_Type (E)
24943 and then Is_Access_Subprogram_Type (Base_Type (E))
24944 and then Has_Foreign_Convention (E)
24945 then
24946 Set_Can_Use_Internal_Rep (E, False);
24947 end if;
24948
24949 -- If E is an object, including a component, and the type of E is an
24950 -- anonymous access type with no convention set, then also set the
24951 -- convention of the anonymous access type. We do not do this for
24952 -- anonymous protected types, since protected types always have the
24953 -- default convention.
24954
24955 if Present (Etype (E))
24956 and then (Is_Object (E)
24957
24958 -- Allow E_Void (happens for pragma Convention appearing
24959 -- in the middle of a record applying to a component)
24960
24961 or else Ekind (E) = E_Void)
24962 then
24963 declare
24964 Typ : constant Entity_Id := Etype (E);
24965
24966 begin
24967 if Ekind_In (Typ, E_Anonymous_Access_Type,
24968 E_Anonymous_Access_Subprogram_Type)
24969 and then not Has_Convention_Pragma (Typ)
24970 then
24971 Basic_Set_Convention (Typ, Val);
24972 Set_Has_Convention_Pragma (Typ);
24973
24974 -- And for the access subprogram type, deal similarly with the
24975 -- designated E_Subprogram_Type, which is always internal.
24976
24977 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
24978 declare
24979 Dtype : constant Entity_Id := Designated_Type (Typ);
24980 begin
24981 if Ekind (Dtype) = E_Subprogram_Type
24982 and then not Has_Convention_Pragma (Dtype)
24983 then
24984 Basic_Set_Convention (Dtype, Val);
24985 Set_Has_Convention_Pragma (Dtype);
24986 end if;
24987 end;
24988 end if;
24989 end if;
24990 end;
24991 end if;
24992 end Set_Convention;
24993
24994 ------------------------
24995 -- Set_Current_Entity --
24996 ------------------------
24997
24998 -- The given entity is to be set as the currently visible definition of its
24999 -- associated name (i.e. the Node_Id associated with its name). All we have
25000 -- to do is to get the name from the identifier, and then set the
25001 -- associated Node_Id to point to the given entity.
25002
25003 procedure Set_Current_Entity (E : Entity_Id) is
25004 begin
25005 Set_Name_Entity_Id (Chars (E), E);
25006 end Set_Current_Entity;
25007
25008 ---------------------------
25009 -- Set_Debug_Info_Needed --
25010 ---------------------------
25011
25012 procedure Set_Debug_Info_Needed (T : Entity_Id) is
25013
25014 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
25015 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
25016 -- Used to set debug info in a related node if not set already
25017
25018 --------------------------------------
25019 -- Set_Debug_Info_Needed_If_Not_Set --
25020 --------------------------------------
25021
25022 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
25023 begin
25024 if Present (E) and then not Needs_Debug_Info (E) then
25025 Set_Debug_Info_Needed (E);
25026
25027 -- For a private type, indicate that the full view also needs
25028 -- debug information.
25029
25030 if Is_Type (E)
25031 and then Is_Private_Type (E)
25032 and then Present (Full_View (E))
25033 then
25034 Set_Debug_Info_Needed (Full_View (E));
25035 end if;
25036 end if;
25037 end Set_Debug_Info_Needed_If_Not_Set;
25038
25039 -- Start of processing for Set_Debug_Info_Needed
25040
25041 begin
25042 -- Nothing to do if there is no available entity
25043
25044 if No (T) then
25045 return;
25046
25047 -- Nothing to do for an entity with suppressed debug information
25048
25049 elsif Debug_Info_Off (T) then
25050 return;
25051
25052 -- Nothing to do for an ignored Ghost entity because the entity will be
25053 -- eliminated from the tree.
25054
25055 elsif Is_Ignored_Ghost_Entity (T) then
25056 return;
25057
25058 -- Nothing to do if entity comes from a predefined file. Library files
25059 -- are compiled without debug information, but inlined bodies of these
25060 -- routines may appear in user code, and debug information on them ends
25061 -- up complicating debugging the user code.
25062
25063 elsif In_Inlined_Body and then In_Predefined_Unit (T) then
25064 Set_Needs_Debug_Info (T, False);
25065 end if;
25066
25067 -- Set flag in entity itself. Note that we will go through the following
25068 -- circuitry even if the flag is already set on T. That's intentional,
25069 -- it makes sure that the flag will be set in subsidiary entities.
25070
25071 Set_Needs_Debug_Info (T);
25072
25073 -- Set flag on subsidiary entities if not set already
25074
25075 if Is_Object (T) then
25076 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
25077
25078 elsif Is_Type (T) then
25079 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
25080
25081 if Is_Record_Type (T) then
25082 declare
25083 Ent : Entity_Id := First_Entity (T);
25084 begin
25085 while Present (Ent) loop
25086 Set_Debug_Info_Needed_If_Not_Set (Ent);
25087 Next_Entity (Ent);
25088 end loop;
25089 end;
25090
25091 -- For a class wide subtype, we also need debug information
25092 -- for the equivalent type.
25093
25094 if Ekind (T) = E_Class_Wide_Subtype then
25095 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
25096 end if;
25097
25098 elsif Is_Array_Type (T) then
25099 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
25100
25101 declare
25102 Indx : Node_Id := First_Index (T);
25103 begin
25104 while Present (Indx) loop
25105 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
25106 Indx := Next_Index (Indx);
25107 end loop;
25108 end;
25109
25110 -- For a packed array type, we also need debug information for
25111 -- the type used to represent the packed array. Conversely, we
25112 -- also need it for the former if we need it for the latter.
25113
25114 if Is_Packed (T) then
25115 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
25116 end if;
25117
25118 if Is_Packed_Array_Impl_Type (T) then
25119 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
25120 end if;
25121
25122 elsif Is_Access_Type (T) then
25123 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
25124
25125 elsif Is_Private_Type (T) then
25126 declare
25127 FV : constant Entity_Id := Full_View (T);
25128
25129 begin
25130 Set_Debug_Info_Needed_If_Not_Set (FV);
25131
25132 -- If the full view is itself a derived private type, we need
25133 -- debug information on its underlying type.
25134
25135 if Present (FV)
25136 and then Is_Private_Type (FV)
25137 and then Present (Underlying_Full_View (FV))
25138 then
25139 Set_Needs_Debug_Info (Underlying_Full_View (FV));
25140 end if;
25141 end;
25142
25143 elsif Is_Protected_Type (T) then
25144 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
25145
25146 elsif Is_Scalar_Type (T) then
25147
25148 -- If the subrange bounds are materialized by dedicated constant
25149 -- objects, also include them in the debug info to make sure the
25150 -- debugger can properly use them.
25151
25152 if Present (Scalar_Range (T))
25153 and then Nkind (Scalar_Range (T)) = N_Range
25154 then
25155 declare
25156 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
25157 High_Bnd : constant Node_Id := Type_High_Bound (T);
25158
25159 begin
25160 if Is_Entity_Name (Low_Bnd) then
25161 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
25162 end if;
25163
25164 if Is_Entity_Name (High_Bnd) then
25165 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
25166 end if;
25167 end;
25168 end if;
25169 end if;
25170 end if;
25171 end Set_Debug_Info_Needed;
25172
25173 ----------------------------
25174 -- Set_Entity_With_Checks --
25175 ----------------------------
25176
25177 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
25178 Val_Actual : Entity_Id;
25179 Nod : Node_Id;
25180 Post_Node : Node_Id;
25181
25182 begin
25183 -- Unconditionally set the entity
25184
25185 Set_Entity (N, Val);
25186
25187 -- The node to post on is the selector in the case of an expanded name,
25188 -- and otherwise the node itself.
25189
25190 if Nkind (N) = N_Expanded_Name then
25191 Post_Node := Selector_Name (N);
25192 else
25193 Post_Node := N;
25194 end if;
25195
25196 -- Check for violation of No_Fixed_IO
25197
25198 if Restriction_Check_Required (No_Fixed_IO)
25199 and then
25200 ((RTU_Loaded (Ada_Text_IO)
25201 and then (Is_RTE (Val, RE_Decimal_IO)
25202 or else
25203 Is_RTE (Val, RE_Fixed_IO)))
25204
25205 or else
25206 (RTU_Loaded (Ada_Wide_Text_IO)
25207 and then (Is_RTE (Val, RO_WT_Decimal_IO)
25208 or else
25209 Is_RTE (Val, RO_WT_Fixed_IO)))
25210
25211 or else
25212 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
25213 and then (Is_RTE (Val, RO_WW_Decimal_IO)
25214 or else
25215 Is_RTE (Val, RO_WW_Fixed_IO))))
25216
25217 -- A special extra check, don't complain about a reference from within
25218 -- the Ada.Interrupts package itself!
25219
25220 and then not In_Same_Extended_Unit (N, Val)
25221 then
25222 Check_Restriction (No_Fixed_IO, Post_Node);
25223 end if;
25224
25225 -- Remaining checks are only done on source nodes. Note that we test
25226 -- for violation of No_Fixed_IO even on non-source nodes, because the
25227 -- cases for checking violations of this restriction are instantiations
25228 -- where the reference in the instance has Comes_From_Source False.
25229
25230 if not Comes_From_Source (N) then
25231 return;
25232 end if;
25233
25234 -- Check for violation of No_Abort_Statements, which is triggered by
25235 -- call to Ada.Task_Identification.Abort_Task.
25236
25237 if Restriction_Check_Required (No_Abort_Statements)
25238 and then (Is_RTE (Val, RE_Abort_Task))
25239
25240 -- A special extra check, don't complain about a reference from within
25241 -- the Ada.Task_Identification package itself!
25242
25243 and then not In_Same_Extended_Unit (N, Val)
25244 then
25245 Check_Restriction (No_Abort_Statements, Post_Node);
25246 end if;
25247
25248 if Val = Standard_Long_Long_Integer then
25249 Check_Restriction (No_Long_Long_Integers, Post_Node);
25250 end if;
25251
25252 -- Check for violation of No_Dynamic_Attachment
25253
25254 if Restriction_Check_Required (No_Dynamic_Attachment)
25255 and then RTU_Loaded (Ada_Interrupts)
25256 and then (Is_RTE (Val, RE_Is_Reserved) or else
25257 Is_RTE (Val, RE_Is_Attached) or else
25258 Is_RTE (Val, RE_Current_Handler) or else
25259 Is_RTE (Val, RE_Attach_Handler) or else
25260 Is_RTE (Val, RE_Exchange_Handler) or else
25261 Is_RTE (Val, RE_Detach_Handler) or else
25262 Is_RTE (Val, RE_Reference))
25263
25264 -- A special extra check, don't complain about a reference from within
25265 -- the Ada.Interrupts package itself!
25266
25267 and then not In_Same_Extended_Unit (N, Val)
25268 then
25269 Check_Restriction (No_Dynamic_Attachment, Post_Node);
25270 end if;
25271
25272 -- Check for No_Implementation_Identifiers
25273
25274 if Restriction_Check_Required (No_Implementation_Identifiers) then
25275
25276 -- We have an implementation defined entity if it is marked as
25277 -- implementation defined, or is defined in a package marked as
25278 -- implementation defined. However, library packages themselves
25279 -- are excluded (we don't want to flag Interfaces itself, just
25280 -- the entities within it).
25281
25282 if (Is_Implementation_Defined (Val)
25283 or else
25284 (Present (Scope (Val))
25285 and then Is_Implementation_Defined (Scope (Val))))
25286 and then not (Is_Package_Or_Generic_Package (Val)
25287 and then Is_Library_Level_Entity (Val))
25288 then
25289 Check_Restriction (No_Implementation_Identifiers, Post_Node);
25290 end if;
25291 end if;
25292
25293 -- Do the style check
25294
25295 if Style_Check
25296 and then not Suppress_Style_Checks (Val)
25297 and then not In_Instance
25298 then
25299 if Nkind (N) = N_Identifier then
25300 Nod := N;
25301 elsif Nkind (N) = N_Expanded_Name then
25302 Nod := Selector_Name (N);
25303 else
25304 return;
25305 end if;
25306
25307 -- A special situation arises for derived operations, where we want
25308 -- to do the check against the parent (since the Sloc of the derived
25309 -- operation points to the derived type declaration itself).
25310
25311 Val_Actual := Val;
25312 while not Comes_From_Source (Val_Actual)
25313 and then Nkind (Val_Actual) in N_Entity
25314 and then (Ekind (Val_Actual) = E_Enumeration_Literal
25315 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
25316 and then Present (Alias (Val_Actual))
25317 loop
25318 Val_Actual := Alias (Val_Actual);
25319 end loop;
25320
25321 -- Renaming declarations for generic actuals do not come from source,
25322 -- and have a different name from that of the entity they rename, so
25323 -- there is no style check to perform here.
25324
25325 if Chars (Nod) = Chars (Val_Actual) then
25326 Style.Check_Identifier (Nod, Val_Actual);
25327 end if;
25328 end if;
25329
25330 Set_Entity (N, Val);
25331 end Set_Entity_With_Checks;
25332
25333 ------------------------------
25334 -- Set_Invalid_Scalar_Value --
25335 ------------------------------
25336
25337 procedure Set_Invalid_Scalar_Value
25338 (Scal_Typ : Float_Scalar_Id;
25339 Value : Ureal)
25340 is
25341 Slot : Ureal renames Invalid_Floats (Scal_Typ);
25342
25343 begin
25344 -- Detect an attempt to set a different value for the same scalar type
25345
25346 pragma Assert (Slot = No_Ureal);
25347 Slot := Value;
25348 end Set_Invalid_Scalar_Value;
25349
25350 ------------------------------
25351 -- Set_Invalid_Scalar_Value --
25352 ------------------------------
25353
25354 procedure Set_Invalid_Scalar_Value
25355 (Scal_Typ : Integer_Scalar_Id;
25356 Value : Uint)
25357 is
25358 Slot : Uint renames Invalid_Integers (Scal_Typ);
25359
25360 begin
25361 -- Detect an attempt to set a different value for the same scalar type
25362
25363 pragma Assert (Slot = No_Uint);
25364 Slot := Value;
25365 end Set_Invalid_Scalar_Value;
25366
25367 ------------------------
25368 -- Set_Name_Entity_Id --
25369 ------------------------
25370
25371 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
25372 begin
25373 Set_Name_Table_Int (Id, Int (Val));
25374 end Set_Name_Entity_Id;
25375
25376 ---------------------
25377 -- Set_Next_Actual --
25378 ---------------------
25379
25380 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
25381 begin
25382 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
25383 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
25384 end if;
25385 end Set_Next_Actual;
25386
25387 ----------------------------------
25388 -- Set_Optimize_Alignment_Flags --
25389 ----------------------------------
25390
25391 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
25392 begin
25393 if Optimize_Alignment = 'S' then
25394 Set_Optimize_Alignment_Space (E);
25395 elsif Optimize_Alignment = 'T' then
25396 Set_Optimize_Alignment_Time (E);
25397 end if;
25398 end Set_Optimize_Alignment_Flags;
25399
25400 -----------------------
25401 -- Set_Public_Status --
25402 -----------------------
25403
25404 procedure Set_Public_Status (Id : Entity_Id) is
25405 S : constant Entity_Id := Current_Scope;
25406
25407 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
25408 -- Determines if E is defined within handled statement sequence or
25409 -- an if statement, returns True if so, False otherwise.
25410
25411 ----------------------
25412 -- Within_HSS_Or_If --
25413 ----------------------
25414
25415 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
25416 N : Node_Id;
25417 begin
25418 N := Declaration_Node (E);
25419 loop
25420 N := Parent (N);
25421
25422 if No (N) then
25423 return False;
25424
25425 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
25426 N_If_Statement)
25427 then
25428 return True;
25429 end if;
25430 end loop;
25431 end Within_HSS_Or_If;
25432
25433 -- Start of processing for Set_Public_Status
25434
25435 begin
25436 -- Everything in the scope of Standard is public
25437
25438 if S = Standard_Standard then
25439 Set_Is_Public (Id);
25440
25441 -- Entity is definitely not public if enclosing scope is not public
25442
25443 elsif not Is_Public (S) then
25444 return;
25445
25446 -- An object or function declaration that occurs in a handled sequence
25447 -- of statements or within an if statement is the declaration for a
25448 -- temporary object or local subprogram generated by the expander. It
25449 -- never needs to be made public and furthermore, making it public can
25450 -- cause back end problems.
25451
25452 elsif Nkind_In (Parent (Id), N_Object_Declaration,
25453 N_Function_Specification)
25454 and then Within_HSS_Or_If (Id)
25455 then
25456 return;
25457
25458 -- Entities in public packages or records are public
25459
25460 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
25461 Set_Is_Public (Id);
25462
25463 -- The bounds of an entry family declaration can generate object
25464 -- declarations that are visible to the back-end, e.g. in the
25465 -- the declaration of a composite type that contains tasks.
25466
25467 elsif Is_Concurrent_Type (S)
25468 and then not Has_Completion (S)
25469 and then Nkind (Parent (Id)) = N_Object_Declaration
25470 then
25471 Set_Is_Public (Id);
25472 end if;
25473 end Set_Public_Status;
25474
25475 -----------------------------
25476 -- Set_Referenced_Modified --
25477 -----------------------------
25478
25479 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
25480 Pref : Node_Id;
25481
25482 begin
25483 -- Deal with indexed or selected component where prefix is modified
25484
25485 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
25486 Pref := Prefix (N);
25487
25488 -- If prefix is access type, then it is the designated object that is
25489 -- being modified, which means we have no entity to set the flag on.
25490
25491 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
25492 return;
25493
25494 -- Otherwise chase the prefix
25495
25496 else
25497 Set_Referenced_Modified (Pref, Out_Param);
25498 end if;
25499
25500 -- Otherwise see if we have an entity name (only other case to process)
25501
25502 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
25503 Set_Referenced_As_LHS (Entity (N), not Out_Param);
25504 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
25505 end if;
25506 end Set_Referenced_Modified;
25507
25508 ------------------
25509 -- Set_Rep_Info --
25510 ------------------
25511
25512 procedure Set_Rep_Info (T1 : Entity_Id; T2 : Entity_Id) is
25513 begin
25514 Set_Is_Atomic (T1, Is_Atomic (T2));
25515 Set_Is_Independent (T1, Is_Independent (T2));
25516 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
25517
25518 if Is_Base_Type (T1) then
25519 Set_Is_Volatile (T1, Is_Volatile (T2));
25520 end if;
25521 end Set_Rep_Info;
25522
25523 ----------------------------
25524 -- Set_Scope_Is_Transient --
25525 ----------------------------
25526
25527 procedure Set_Scope_Is_Transient (V : Boolean := True) is
25528 begin
25529 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
25530 end Set_Scope_Is_Transient;
25531
25532 -------------------
25533 -- Set_Size_Info --
25534 -------------------
25535
25536 procedure Set_Size_Info (T1, T2 : Entity_Id) is
25537 begin
25538 -- We copy Esize, but not RM_Size, since in general RM_Size is
25539 -- subtype specific and does not get inherited by all subtypes.
25540
25541 Set_Esize (T1, Esize (T2));
25542 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
25543
25544 if Is_Discrete_Or_Fixed_Point_Type (T1)
25545 and then
25546 Is_Discrete_Or_Fixed_Point_Type (T2)
25547 then
25548 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
25549 end if;
25550
25551 Set_Alignment (T1, Alignment (T2));
25552 end Set_Size_Info;
25553
25554 ------------------------------
25555 -- Should_Ignore_Pragma_Par --
25556 ------------------------------
25557
25558 function Should_Ignore_Pragma_Par (Prag_Name : Name_Id) return Boolean is
25559 pragma Assert (Compiler_State = Parsing);
25560 -- This one can't work during semantic analysis, because we don't have a
25561 -- correct Current_Source_File.
25562
25563 Result : constant Boolean :=
25564 Get_Name_Table_Boolean3 (Prag_Name)
25565 and then not Is_Internal_File_Name
25566 (File_Name (Current_Source_File));
25567 begin
25568 return Result;
25569 end Should_Ignore_Pragma_Par;
25570
25571 ------------------------------
25572 -- Should_Ignore_Pragma_Sem --
25573 ------------------------------
25574
25575 function Should_Ignore_Pragma_Sem (N : Node_Id) return Boolean is
25576 pragma Assert (Compiler_State = Analyzing);
25577 Prag_Name : constant Name_Id := Pragma_Name (N);
25578 Result : constant Boolean :=
25579 Get_Name_Table_Boolean3 (Prag_Name)
25580 and then not In_Internal_Unit (N);
25581
25582 begin
25583 return Result;
25584 end Should_Ignore_Pragma_Sem;
25585
25586 --------------------
25587 -- Static_Boolean --
25588 --------------------
25589
25590 function Static_Boolean (N : Node_Id) return Uint is
25591 begin
25592 Analyze_And_Resolve (N, Standard_Boolean);
25593
25594 if N = Error
25595 or else Error_Posted (N)
25596 or else Etype (N) = Any_Type
25597 then
25598 return No_Uint;
25599 end if;
25600
25601 if Is_OK_Static_Expression (N) then
25602 if not Raises_Constraint_Error (N) then
25603 return Expr_Value (N);
25604 else
25605 return No_Uint;
25606 end if;
25607
25608 elsif Etype (N) = Any_Type then
25609 return No_Uint;
25610
25611 else
25612 Flag_Non_Static_Expr
25613 ("static boolean expression required here", N);
25614 return No_Uint;
25615 end if;
25616 end Static_Boolean;
25617
25618 --------------------
25619 -- Static_Integer --
25620 --------------------
25621
25622 function Static_Integer (N : Node_Id) return Uint is
25623 begin
25624 Analyze_And_Resolve (N, Any_Integer);
25625
25626 if N = Error
25627 or else Error_Posted (N)
25628 or else Etype (N) = Any_Type
25629 then
25630 return No_Uint;
25631 end if;
25632
25633 if Is_OK_Static_Expression (N) then
25634 if not Raises_Constraint_Error (N) then
25635 return Expr_Value (N);
25636 else
25637 return No_Uint;
25638 end if;
25639
25640 elsif Etype (N) = Any_Type then
25641 return No_Uint;
25642
25643 else
25644 Flag_Non_Static_Expr
25645 ("static integer expression required here", N);
25646 return No_Uint;
25647 end if;
25648 end Static_Integer;
25649
25650 --------------------------
25651 -- Statically_Different --
25652 --------------------------
25653
25654 function Statically_Different (E1, E2 : Node_Id) return Boolean is
25655 R1 : constant Node_Id := Get_Referenced_Object (E1);
25656 R2 : constant Node_Id := Get_Referenced_Object (E2);
25657 begin
25658 return Is_Entity_Name (R1)
25659 and then Is_Entity_Name (R2)
25660 and then Entity (R1) /= Entity (R2)
25661 and then not Is_Formal (Entity (R1))
25662 and then not Is_Formal (Entity (R2));
25663 end Statically_Different;
25664
25665 --------------------------------------
25666 -- Subject_To_Loop_Entry_Attributes --
25667 --------------------------------------
25668
25669 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
25670 Stmt : Node_Id;
25671
25672 begin
25673 Stmt := N;
25674
25675 -- The expansion mechanism transform a loop subject to at least one
25676 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
25677 -- the conditional part.
25678
25679 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
25680 and then Nkind (Original_Node (N)) = N_Loop_Statement
25681 then
25682 Stmt := Original_Node (N);
25683 end if;
25684
25685 return
25686 Nkind (Stmt) = N_Loop_Statement
25687 and then Present (Identifier (Stmt))
25688 and then Present (Entity (Identifier (Stmt)))
25689 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
25690 end Subject_To_Loop_Entry_Attributes;
25691
25692 -----------------------------
25693 -- Subprogram_Access_Level --
25694 -----------------------------
25695
25696 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
25697 begin
25698 if Present (Alias (Subp)) then
25699 return Subprogram_Access_Level (Alias (Subp));
25700 else
25701 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
25702 end if;
25703 end Subprogram_Access_Level;
25704
25705 ---------------------
25706 -- Subprogram_Name --
25707 ---------------------
25708
25709 function Subprogram_Name (N : Node_Id) return String is
25710 Buf : Bounded_String;
25711 Ent : Node_Id := N;
25712 Nod : Node_Id;
25713
25714 begin
25715 while Present (Ent) loop
25716 case Nkind (Ent) is
25717 when N_Subprogram_Body =>
25718 Ent := Defining_Unit_Name (Specification (Ent));
25719 exit;
25720
25721 when N_Subprogram_Declaration =>
25722 Nod := Corresponding_Body (Ent);
25723
25724 if Present (Nod) then
25725 Ent := Nod;
25726 else
25727 Ent := Defining_Unit_Name (Specification (Ent));
25728 end if;
25729
25730 exit;
25731
25732 when N_Subprogram_Instantiation
25733 | N_Package_Body
25734 | N_Package_Specification
25735 =>
25736 Ent := Defining_Unit_Name (Ent);
25737 exit;
25738
25739 when N_Protected_Type_Declaration =>
25740 Ent := Corresponding_Body (Ent);
25741 exit;
25742
25743 when N_Protected_Body
25744 | N_Task_Body
25745 =>
25746 Ent := Defining_Identifier (Ent);
25747 exit;
25748
25749 when others =>
25750 null;
25751 end case;
25752
25753 Ent := Parent (Ent);
25754 end loop;
25755
25756 if No (Ent) then
25757 return "unknown subprogram:unknown file:0:0";
25758 end if;
25759
25760 -- If the subprogram is a child unit, use its simple name to start the
25761 -- construction of the fully qualified name.
25762
25763 if Nkind (Ent) = N_Defining_Program_Unit_Name then
25764 Ent := Defining_Identifier (Ent);
25765 end if;
25766
25767 Append_Entity_Name (Buf, Ent);
25768
25769 -- Append homonym number if needed
25770
25771 if Nkind (N) in N_Entity and then Has_Homonym (N) then
25772 declare
25773 H : Entity_Id := Homonym (N);
25774 Nr : Nat := 1;
25775
25776 begin
25777 while Present (H) loop
25778 if Scope (H) = Scope (N) then
25779 Nr := Nr + 1;
25780 end if;
25781
25782 H := Homonym (H);
25783 end loop;
25784
25785 if Nr > 1 then
25786 Append (Buf, '#');
25787 Append (Buf, Nr);
25788 end if;
25789 end;
25790 end if;
25791
25792 -- Append source location of Ent to Buf so that the string will
25793 -- look like "subp:file:line:col".
25794
25795 declare
25796 Loc : constant Source_Ptr := Sloc (Ent);
25797 begin
25798 Append (Buf, ':');
25799 Append (Buf, Reference_Name (Get_Source_File_Index (Loc)));
25800 Append (Buf, ':');
25801 Append (Buf, Nat (Get_Logical_Line_Number (Loc)));
25802 Append (Buf, ':');
25803 Append (Buf, Nat (Get_Column_Number (Loc)));
25804 end;
25805
25806 return +Buf;
25807 end Subprogram_Name;
25808
25809 -------------------------------
25810 -- Support_Atomic_Primitives --
25811 -------------------------------
25812
25813 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
25814 Size : Int;
25815
25816 begin
25817 -- Verify the alignment of Typ is known
25818
25819 if not Known_Alignment (Typ) then
25820 return False;
25821 end if;
25822
25823 if Known_Static_Esize (Typ) then
25824 Size := UI_To_Int (Esize (Typ));
25825
25826 -- If the Esize (Object_Size) is unknown at compile time, look at the
25827 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
25828
25829 elsif Known_Static_RM_Size (Typ) then
25830 Size := UI_To_Int (RM_Size (Typ));
25831
25832 -- Otherwise, the size is considered to be unknown.
25833
25834 else
25835 return False;
25836 end if;
25837
25838 -- Check that the size of the component is 8, 16, 32, or 64 bits and
25839 -- that Typ is properly aligned.
25840
25841 case Size is
25842 when 8 | 16 | 32 | 64 =>
25843 return Size = UI_To_Int (Alignment (Typ)) * 8;
25844
25845 when others =>
25846 return False;
25847 end case;
25848 end Support_Atomic_Primitives;
25849
25850 -----------------
25851 -- Trace_Scope --
25852 -----------------
25853
25854 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
25855 begin
25856 if Debug_Flag_W then
25857 for J in 0 .. Scope_Stack.Last loop
25858 Write_Str (" ");
25859 end loop;
25860
25861 Write_Str (Msg);
25862 Write_Name (Chars (E));
25863 Write_Str (" from ");
25864 Write_Location (Sloc (N));
25865 Write_Eol;
25866 end if;
25867 end Trace_Scope;
25868
25869 -----------------------
25870 -- Transfer_Entities --
25871 -----------------------
25872
25873 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
25874 procedure Set_Public_Status_Of (Id : Entity_Id);
25875 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
25876 -- Set_Public_Status. If successful and Id denotes a record type, set
25877 -- the Is_Public attribute of its fields.
25878
25879 --------------------------
25880 -- Set_Public_Status_Of --
25881 --------------------------
25882
25883 procedure Set_Public_Status_Of (Id : Entity_Id) is
25884 Field : Entity_Id;
25885
25886 begin
25887 if not Is_Public (Id) then
25888 Set_Public_Status (Id);
25889
25890 -- When the input entity is a public record type, ensure that all
25891 -- its internal fields are also exposed to the linker. The fields
25892 -- of a class-wide type are never made public.
25893
25894 if Is_Public (Id)
25895 and then Is_Record_Type (Id)
25896 and then not Is_Class_Wide_Type (Id)
25897 then
25898 Field := First_Entity (Id);
25899 while Present (Field) loop
25900 Set_Is_Public (Field);
25901 Next_Entity (Field);
25902 end loop;
25903 end if;
25904 end if;
25905 end Set_Public_Status_Of;
25906
25907 -- Local variables
25908
25909 Full_Id : Entity_Id;
25910 Id : Entity_Id;
25911
25912 -- Start of processing for Transfer_Entities
25913
25914 begin
25915 Id := First_Entity (From);
25916
25917 if Present (Id) then
25918
25919 -- Merge the entity chain of the source scope with that of the
25920 -- destination scope.
25921
25922 if Present (Last_Entity (To)) then
25923 Link_Entities (Last_Entity (To), Id);
25924 else
25925 Set_First_Entity (To, Id);
25926 end if;
25927
25928 Set_Last_Entity (To, Last_Entity (From));
25929
25930 -- Inspect the entities of the source scope and update their Scope
25931 -- attribute.
25932
25933 while Present (Id) loop
25934 Set_Scope (Id, To);
25935 Set_Public_Status_Of (Id);
25936
25937 -- Handle an internally generated full view for a private type
25938
25939 if Is_Private_Type (Id)
25940 and then Present (Full_View (Id))
25941 and then Is_Itype (Full_View (Id))
25942 then
25943 Full_Id := Full_View (Id);
25944
25945 Set_Scope (Full_Id, To);
25946 Set_Public_Status_Of (Full_Id);
25947 end if;
25948
25949 Next_Entity (Id);
25950 end loop;
25951
25952 Set_First_Entity (From, Empty);
25953 Set_Last_Entity (From, Empty);
25954 end if;
25955 end Transfer_Entities;
25956
25957 ------------------------
25958 -- Traverse_More_Func --
25959 ------------------------
25960
25961 function Traverse_More_Func (Node : Node_Id) return Traverse_Final_Result is
25962
25963 Processing_Itype : Boolean := False;
25964 -- Set to True while traversing the nodes under an Itype, to prevent
25965 -- looping on Itype handling during that traversal.
25966
25967 function Process_More (N : Node_Id) return Traverse_Result;
25968 -- Wrapper over the Process callback to handle parts of the AST that
25969 -- are not normally traversed as syntactic children.
25970
25971 function Traverse_Rec (N : Node_Id) return Traverse_Final_Result;
25972 -- Main recursive traversal implemented as an instantiation of
25973 -- Traverse_Func over a modified Process callback.
25974
25975 ------------------
25976 -- Process_More --
25977 ------------------
25978
25979 function Process_More (N : Node_Id) return Traverse_Result is
25980
25981 procedure Traverse_More (N : Node_Id;
25982 Res : in out Traverse_Result);
25983 procedure Traverse_More (L : List_Id;
25984 Res : in out Traverse_Result);
25985 -- Traverse a node or list and update the traversal result to value
25986 -- Abandon when needed.
25987
25988 -------------------
25989 -- Traverse_More --
25990 -------------------
25991
25992 procedure Traverse_More (N : Node_Id;
25993 Res : in out Traverse_Result)
25994 is
25995 begin
25996 -- Do not process any more nodes if Abandon was reached
25997
25998 if Res = Abandon then
25999 return;
26000 end if;
26001
26002 if Traverse_Rec (N) = Abandon then
26003 Res := Abandon;
26004 end if;
26005 end Traverse_More;
26006
26007 procedure Traverse_More (L : List_Id;
26008 Res : in out Traverse_Result)
26009 is
26010 N : Node_Id := First (L);
26011
26012 begin
26013 -- Do not process any more nodes if Abandon was reached
26014
26015 if Res = Abandon then
26016 return;
26017 end if;
26018
26019 while Present (N) loop
26020 Traverse_More (N, Res);
26021 Next (N);
26022 end loop;
26023 end Traverse_More;
26024
26025 -- Local variables
26026
26027 Node : Node_Id;
26028 Result : Traverse_Result;
26029
26030 -- Start of processing for Process_More
26031
26032 begin
26033 -- Initial callback to Process. Return immediately on Skip/Abandon.
26034 -- Otherwise update the value of Node for further processing of
26035 -- non-syntactic children.
26036
26037 Result := Process (N);
26038
26039 case Result is
26040 when OK => Node := N;
26041 when OK_Orig => Node := Original_Node (N);
26042 when Skip => return Skip;
26043 when Abandon => return Abandon;
26044 end case;
26045
26046 -- Process the relevant semantic children which are a logical part of
26047 -- the AST under this node before returning for the processing of
26048 -- syntactic children.
26049
26050 -- Start with all non-syntactic lists of action nodes
26051
26052 case Nkind (Node) is
26053 when N_Component_Association =>
26054 Traverse_More (Loop_Actions (Node), Result);
26055
26056 when N_Elsif_Part =>
26057 Traverse_More (Condition_Actions (Node), Result);
26058
26059 when N_Short_Circuit =>
26060 Traverse_More (Actions (Node), Result);
26061
26062 when N_Case_Expression_Alternative =>
26063 Traverse_More (Actions (Node), Result);
26064
26065 when N_Iterated_Component_Association =>
26066 Traverse_More (Loop_Actions (Node), Result);
26067
26068 when N_Iteration_Scheme =>
26069 Traverse_More (Condition_Actions (Node), Result);
26070
26071 when N_If_Expression =>
26072 Traverse_More (Then_Actions (Node), Result);
26073 Traverse_More (Else_Actions (Node), Result);
26074
26075 -- Various nodes have a field Actions as a syntactic node,
26076 -- so it will be traversed in the regular syntactic traversal.
26077
26078 when N_Compilation_Unit_Aux
26079 | N_Compound_Statement
26080 | N_Expression_With_Actions
26081 | N_Freeze_Entity
26082 =>
26083 null;
26084
26085 when others =>
26086 null;
26087 end case;
26088
26089 -- If Process_Itypes is True, process unattached nodes which come
26090 -- from Itypes. This only concerns currently ranges of scalar
26091 -- (possibly as index) types. This traversal is protected against
26092 -- looping with Processing_Itype.
26093
26094 if Process_Itypes
26095 and then not Processing_Itype
26096 and then Nkind (Node) in N_Has_Etype
26097 and then Present (Etype (Node))
26098 and then Is_Itype (Etype (Node))
26099 then
26100 declare
26101 Typ : constant Entity_Id := Etype (Node);
26102 begin
26103 Processing_Itype := True;
26104
26105 case Ekind (Typ) is
26106 when Scalar_Kind =>
26107 Traverse_More (Scalar_Range (Typ), Result);
26108
26109 when Array_Kind =>
26110 declare
26111 Index : Node_Id := First_Index (Typ);
26112 Rng : Node_Id;
26113 begin
26114 while Present (Index) loop
26115 if Nkind (Index) in N_Has_Entity then
26116 Rng := Scalar_Range (Entity (Index));
26117 else
26118 Rng := Index;
26119 end if;
26120
26121 Traverse_More (Rng, Result);
26122 Next_Index (Index);
26123 end loop;
26124 end;
26125 when others =>
26126 null;
26127 end case;
26128
26129 Processing_Itype := False;
26130 end;
26131 end if;
26132
26133 return Result;
26134 end Process_More;
26135
26136 -- Define Traverse_Rec as a renaming of the instantiation, as an
26137 -- instantiation cannot complete a previous spec.
26138
26139 function Traverse_Recursive is new Traverse_Func (Process_More);
26140 function Traverse_Rec (N : Node_Id) return Traverse_Final_Result
26141 renames Traverse_Recursive;
26142
26143 -- Start of processing for Traverse_More_Func
26144
26145 begin
26146 return Traverse_Rec (Node);
26147 end Traverse_More_Func;
26148
26149 ------------------------
26150 -- Traverse_More_Proc --
26151 ------------------------
26152
26153 procedure Traverse_More_Proc (Node : Node_Id) is
26154 function Traverse is new Traverse_More_Func (Process, Process_Itypes);
26155 Discard : Traverse_Final_Result;
26156 pragma Warnings (Off, Discard);
26157 begin
26158 Discard := Traverse (Node);
26159 end Traverse_More_Proc;
26160
26161 -----------------------
26162 -- Type_Access_Level --
26163 -----------------------
26164
26165 function Type_Access_Level (Typ : Entity_Id) return Uint is
26166 Btyp : Entity_Id;
26167
26168 begin
26169 Btyp := Base_Type (Typ);
26170
26171 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
26172 -- simply use the level where the type is declared. This is true for
26173 -- stand-alone object declarations, and for anonymous access types
26174 -- associated with components the level is the same as that of the
26175 -- enclosing composite type. However, special treatment is needed for
26176 -- the cases of access parameters, return objects of an anonymous access
26177 -- type, and, in Ada 95, access discriminants of limited types.
26178
26179 if Is_Access_Type (Btyp) then
26180 if Ekind (Btyp) = E_Anonymous_Access_Type then
26181
26182 -- If the type is a nonlocal anonymous access type (such as for
26183 -- an access parameter) we treat it as being declared at the
26184 -- library level to ensure that names such as X.all'access don't
26185 -- fail static accessibility checks.
26186
26187 if not Is_Local_Anonymous_Access (Typ) then
26188 return Scope_Depth (Standard_Standard);
26189
26190 -- If this is a return object, the accessibility level is that of
26191 -- the result subtype of the enclosing function. The test here is
26192 -- little complicated, because we have to account for extended
26193 -- return statements that have been rewritten as blocks, in which
26194 -- case we have to find and the Is_Return_Object attribute of the
26195 -- itype's associated object. It would be nice to find a way to
26196 -- simplify this test, but it doesn't seem worthwhile to add a new
26197 -- flag just for purposes of this test. ???
26198
26199 elsif Ekind (Scope (Btyp)) = E_Return_Statement
26200 or else
26201 (Is_Itype (Btyp)
26202 and then Nkind (Associated_Node_For_Itype (Btyp)) =
26203 N_Object_Declaration
26204 and then Is_Return_Object
26205 (Defining_Identifier
26206 (Associated_Node_For_Itype (Btyp))))
26207 then
26208 declare
26209 Scop : Entity_Id;
26210
26211 begin
26212 Scop := Scope (Scope (Btyp));
26213 while Present (Scop) loop
26214 exit when Ekind (Scop) = E_Function;
26215 Scop := Scope (Scop);
26216 end loop;
26217
26218 -- Treat the return object's type as having the level of the
26219 -- function's result subtype (as per RM05-6.5(5.3/2)).
26220
26221 return Type_Access_Level (Etype (Scop));
26222 end;
26223 end if;
26224 end if;
26225
26226 Btyp := Root_Type (Btyp);
26227
26228 -- The accessibility level of anonymous access types associated with
26229 -- discriminants is that of the current instance of the type, and
26230 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
26231
26232 -- AI-402: access discriminants have accessibility based on the
26233 -- object rather than the type in Ada 2005, so the above paragraph
26234 -- doesn't apply.
26235
26236 -- ??? Needs completion with rules from AI-416
26237
26238 if Ada_Version <= Ada_95
26239 and then Ekind (Typ) = E_Anonymous_Access_Type
26240 and then Present (Associated_Node_For_Itype (Typ))
26241 and then Nkind (Associated_Node_For_Itype (Typ)) =
26242 N_Discriminant_Specification
26243 then
26244 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
26245 end if;
26246 end if;
26247
26248 -- Return library level for a generic formal type. This is done because
26249 -- RM(10.3.2) says that "The statically deeper relationship does not
26250 -- apply to ... a descendant of a generic formal type". Rather than
26251 -- checking at each point where a static accessibility check is
26252 -- performed to see if we are dealing with a formal type, this rule is
26253 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
26254 -- return extreme values for a formal type; Deepest_Type_Access_Level
26255 -- returns Int'Last. By calling the appropriate function from among the
26256 -- two, we ensure that the static accessibility check will pass if we
26257 -- happen to run into a formal type. More specifically, we should call
26258 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
26259 -- call occurs as part of a static accessibility check and the error
26260 -- case is the case where the type's level is too shallow (as opposed
26261 -- to too deep).
26262
26263 if Is_Generic_Type (Root_Type (Btyp)) then
26264 return Scope_Depth (Standard_Standard);
26265 end if;
26266
26267 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
26268 end Type_Access_Level;
26269
26270 ------------------------------------
26271 -- Type_Without_Stream_Operation --
26272 ------------------------------------
26273
26274 function Type_Without_Stream_Operation
26275 (T : Entity_Id;
26276 Op : TSS_Name_Type := TSS_Null) return Entity_Id
26277 is
26278 BT : constant Entity_Id := Base_Type (T);
26279 Op_Missing : Boolean;
26280
26281 begin
26282 if not Restriction_Active (No_Default_Stream_Attributes) then
26283 return Empty;
26284 end if;
26285
26286 if Is_Elementary_Type (T) then
26287 if Op = TSS_Null then
26288 Op_Missing :=
26289 No (TSS (BT, TSS_Stream_Read))
26290 or else No (TSS (BT, TSS_Stream_Write));
26291
26292 else
26293 Op_Missing := No (TSS (BT, Op));
26294 end if;
26295
26296 if Op_Missing then
26297 return T;
26298 else
26299 return Empty;
26300 end if;
26301
26302 elsif Is_Array_Type (T) then
26303 return Type_Without_Stream_Operation (Component_Type (T), Op);
26304
26305 elsif Is_Record_Type (T) then
26306 declare
26307 Comp : Entity_Id;
26308 C_Typ : Entity_Id;
26309
26310 begin
26311 Comp := First_Component (T);
26312 while Present (Comp) loop
26313 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
26314
26315 if Present (C_Typ) then
26316 return C_Typ;
26317 end if;
26318
26319 Next_Component (Comp);
26320 end loop;
26321
26322 return Empty;
26323 end;
26324
26325 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
26326 return Type_Without_Stream_Operation (Full_View (T), Op);
26327 else
26328 return Empty;
26329 end if;
26330 end Type_Without_Stream_Operation;
26331
26332 ---------------------
26333 -- Ultimate_Prefix --
26334 ---------------------
26335
26336 function Ultimate_Prefix (N : Node_Id) return Node_Id is
26337 Pref : Node_Id;
26338
26339 begin
26340 Pref := N;
26341 while Nkind_In (Pref, N_Explicit_Dereference,
26342 N_Indexed_Component,
26343 N_Selected_Component,
26344 N_Slice)
26345 loop
26346 Pref := Prefix (Pref);
26347 end loop;
26348
26349 return Pref;
26350 end Ultimate_Prefix;
26351
26352 ----------------------------
26353 -- Unique_Defining_Entity --
26354 ----------------------------
26355
26356 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
26357 begin
26358 return Unique_Entity (Defining_Entity (N));
26359 end Unique_Defining_Entity;
26360
26361 -------------------
26362 -- Unique_Entity --
26363 -------------------
26364
26365 function Unique_Entity (E : Entity_Id) return Entity_Id is
26366 U : Entity_Id := E;
26367 P : Node_Id;
26368
26369 begin
26370 case Ekind (E) is
26371 when E_Constant =>
26372 if Present (Full_View (E)) then
26373 U := Full_View (E);
26374 end if;
26375
26376 when Entry_Kind =>
26377 if Nkind (Parent (E)) = N_Entry_Body then
26378 declare
26379 Prot_Item : Entity_Id;
26380 Prot_Type : Entity_Id;
26381
26382 begin
26383 if Ekind (E) = E_Entry then
26384 Prot_Type := Scope (E);
26385
26386 -- Bodies of entry families are nested within an extra scope
26387 -- that contains an entry index declaration.
26388
26389 else
26390 Prot_Type := Scope (Scope (E));
26391 end if;
26392
26393 -- A protected type may be declared as a private type, in
26394 -- which case we need to get its full view.
26395
26396 if Is_Private_Type (Prot_Type) then
26397 Prot_Type := Full_View (Prot_Type);
26398 end if;
26399
26400 -- Full view may not be present on error, in which case
26401 -- return E by default.
26402
26403 if Present (Prot_Type) then
26404 pragma Assert (Ekind (Prot_Type) = E_Protected_Type);
26405
26406 -- Traverse the entity list of the protected type and
26407 -- locate an entry declaration which matches the entry
26408 -- body.
26409
26410 Prot_Item := First_Entity (Prot_Type);
26411 while Present (Prot_Item) loop
26412 if Ekind (Prot_Item) in Entry_Kind
26413 and then Corresponding_Body (Parent (Prot_Item)) = E
26414 then
26415 U := Prot_Item;
26416 exit;
26417 end if;
26418
26419 Next_Entity (Prot_Item);
26420 end loop;
26421 end if;
26422 end;
26423 end if;
26424
26425 when Formal_Kind =>
26426 if Present (Spec_Entity (E)) then
26427 U := Spec_Entity (E);
26428 end if;
26429
26430 when E_Package_Body =>
26431 P := Parent (E);
26432
26433 if Nkind (P) = N_Defining_Program_Unit_Name then
26434 P := Parent (P);
26435 end if;
26436
26437 if Nkind (P) = N_Package_Body
26438 and then Present (Corresponding_Spec (P))
26439 then
26440 U := Corresponding_Spec (P);
26441
26442 elsif Nkind (P) = N_Package_Body_Stub
26443 and then Present (Corresponding_Spec_Of_Stub (P))
26444 then
26445 U := Corresponding_Spec_Of_Stub (P);
26446 end if;
26447
26448 when E_Protected_Body =>
26449 P := Parent (E);
26450
26451 if Nkind (P) = N_Protected_Body
26452 and then Present (Corresponding_Spec (P))
26453 then
26454 U := Corresponding_Spec (P);
26455
26456 elsif Nkind (P) = N_Protected_Body_Stub
26457 and then Present (Corresponding_Spec_Of_Stub (P))
26458 then
26459 U := Corresponding_Spec_Of_Stub (P);
26460
26461 if Is_Single_Protected_Object (U) then
26462 U := Etype (U);
26463 end if;
26464 end if;
26465
26466 if Is_Private_Type (U) then
26467 U := Full_View (U);
26468 end if;
26469
26470 when E_Subprogram_Body =>
26471 P := Parent (E);
26472
26473 if Nkind (P) = N_Defining_Program_Unit_Name then
26474 P := Parent (P);
26475 end if;
26476
26477 P := Parent (P);
26478
26479 if Nkind (P) = N_Subprogram_Body
26480 and then Present (Corresponding_Spec (P))
26481 then
26482 U := Corresponding_Spec (P);
26483
26484 elsif Nkind (P) = N_Subprogram_Body_Stub
26485 and then Present (Corresponding_Spec_Of_Stub (P))
26486 then
26487 U := Corresponding_Spec_Of_Stub (P);
26488
26489 elsif Nkind (P) = N_Subprogram_Renaming_Declaration then
26490 U := Corresponding_Spec (P);
26491 end if;
26492
26493 when E_Task_Body =>
26494 P := Parent (E);
26495
26496 if Nkind (P) = N_Task_Body
26497 and then Present (Corresponding_Spec (P))
26498 then
26499 U := Corresponding_Spec (P);
26500
26501 elsif Nkind (P) = N_Task_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_Task_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 Type_Kind =>
26516 if Present (Full_View (E)) then
26517 U := Full_View (E);
26518 end if;
26519
26520 when others =>
26521 null;
26522 end case;
26523
26524 return U;
26525 end Unique_Entity;
26526
26527 -----------------
26528 -- Unique_Name --
26529 -----------------
26530
26531 function Unique_Name (E : Entity_Id) return String is
26532
26533 -- Local subprograms
26534
26535 function Add_Homonym_Suffix (E : Entity_Id) return String;
26536
26537 function This_Name return String;
26538
26539 ------------------------
26540 -- Add_Homonym_Suffix --
26541 ------------------------
26542
26543 function Add_Homonym_Suffix (E : Entity_Id) return String is
26544
26545 -- Names in E_Subprogram_Body or E_Package_Body entities are not
26546 -- reliable, as they may not include the overloading suffix.
26547 -- Instead, when looking for the name of E or one of its enclosing
26548 -- scope, we get the name of the corresponding Unique_Entity.
26549
26550 U : constant Entity_Id := Unique_Entity (E);
26551 Nam : constant String := Get_Name_String (Chars (U));
26552
26553 begin
26554 -- If E has homonyms but is not fully qualified, as done in
26555 -- GNATprove mode, append the homonym number on the fly. Strip the
26556 -- leading space character in the image of natural numbers. Also do
26557 -- not print the homonym value of 1.
26558
26559 if Has_Homonym (U) then
26560 declare
26561 N : constant Pos := Homonym_Number (U);
26562 S : constant String := N'Img;
26563 begin
26564 if N > 1 then
26565 return Nam & "__" & S (2 .. S'Last);
26566 end if;
26567 end;
26568 end if;
26569
26570 return Nam;
26571 end Add_Homonym_Suffix;
26572
26573 ---------------
26574 -- This_Name --
26575 ---------------
26576
26577 function This_Name return String is
26578 begin
26579 return Add_Homonym_Suffix (E);
26580 end This_Name;
26581
26582 -- Local variables
26583
26584 U : constant Entity_Id := Unique_Entity (E);
26585
26586 -- Start of processing for Unique_Name
26587
26588 begin
26589 if E = Standard_Standard
26590 or else Has_Fully_Qualified_Name (E)
26591 then
26592 return This_Name;
26593
26594 elsif Ekind (E) = E_Enumeration_Literal then
26595 return Unique_Name (Etype (E)) & "__" & This_Name;
26596
26597 else
26598 declare
26599 S : constant Entity_Id := Scope (U);
26600 pragma Assert (Present (S));
26601
26602 begin
26603 -- Prefix names of predefined types with standard__, but leave
26604 -- names of user-defined packages and subprograms without prefix
26605 -- (even if technically they are nested in the Standard package).
26606
26607 if S = Standard_Standard then
26608 if Ekind (U) = E_Package or else Is_Subprogram (U) then
26609 return This_Name;
26610 else
26611 return Unique_Name (S) & "__" & This_Name;
26612 end if;
26613
26614 -- For intances of generic subprograms use the name of the related
26615 -- instance and skip the scope of its wrapper package.
26616
26617 elsif Is_Wrapper_Package (S) then
26618 pragma Assert (Scope (S) = Scope (Related_Instance (S)));
26619 -- Wrapper package and the instantiation are in the same scope
26620
26621 declare
26622 Related_Name : constant String :=
26623 Add_Homonym_Suffix (Related_Instance (S));
26624 Enclosing_Name : constant String :=
26625 Unique_Name (Scope (S)) & "__" & Related_Name;
26626
26627 begin
26628 if Is_Subprogram (U)
26629 and then not Is_Generic_Actual_Subprogram (U)
26630 then
26631 return Enclosing_Name;
26632 else
26633 return Enclosing_Name & "__" & This_Name;
26634 end if;
26635 end;
26636
26637 elsif Is_Child_Unit (U) then
26638 return Child_Prefix & Unique_Name (S) & "__" & This_Name;
26639 else
26640 return Unique_Name (S) & "__" & This_Name;
26641 end if;
26642 end;
26643 end if;
26644 end Unique_Name;
26645
26646 ---------------------
26647 -- Unit_Is_Visible --
26648 ---------------------
26649
26650 function Unit_Is_Visible (U : Entity_Id) return Boolean is
26651 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
26652 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
26653
26654 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
26655 -- For a child unit, check whether unit appears in a with_clause
26656 -- of a parent.
26657
26658 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
26659 -- Scan the context clause of one compilation unit looking for a
26660 -- with_clause for the unit in question.
26661
26662 ----------------------------
26663 -- Unit_In_Parent_Context --
26664 ----------------------------
26665
26666 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
26667 begin
26668 if Unit_In_Context (Par_Unit) then
26669 return True;
26670
26671 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
26672 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
26673
26674 else
26675 return False;
26676 end if;
26677 end Unit_In_Parent_Context;
26678
26679 ---------------------
26680 -- Unit_In_Context --
26681 ---------------------
26682
26683 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
26684 Clause : Node_Id;
26685
26686 begin
26687 Clause := First (Context_Items (Comp_Unit));
26688 while Present (Clause) loop
26689 if Nkind (Clause) = N_With_Clause then
26690 if Library_Unit (Clause) = U then
26691 return True;
26692
26693 -- The with_clause may denote a renaming of the unit we are
26694 -- looking for, eg. Text_IO which renames Ada.Text_IO.
26695
26696 elsif
26697 Renamed_Entity (Entity (Name (Clause))) =
26698 Defining_Entity (Unit (U))
26699 then
26700 return True;
26701 end if;
26702 end if;
26703
26704 Next (Clause);
26705 end loop;
26706
26707 return False;
26708 end Unit_In_Context;
26709
26710 -- Start of processing for Unit_Is_Visible
26711
26712 begin
26713 -- The currrent unit is directly visible
26714
26715 if Curr = U then
26716 return True;
26717
26718 elsif Unit_In_Context (Curr) then
26719 return True;
26720
26721 -- If the current unit is a body, check the context of the spec
26722
26723 elsif Nkind (Unit (Curr)) = N_Package_Body
26724 or else
26725 (Nkind (Unit (Curr)) = N_Subprogram_Body
26726 and then not Acts_As_Spec (Unit (Curr)))
26727 then
26728 if Unit_In_Context (Library_Unit (Curr)) then
26729 return True;
26730 end if;
26731 end if;
26732
26733 -- If the spec is a child unit, examine the parents
26734
26735 if Is_Child_Unit (Curr_Entity) then
26736 if Nkind (Unit (Curr)) in N_Unit_Body then
26737 return
26738 Unit_In_Parent_Context
26739 (Parent_Spec (Unit (Library_Unit (Curr))));
26740 else
26741 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
26742 end if;
26743
26744 else
26745 return False;
26746 end if;
26747 end Unit_Is_Visible;
26748
26749 ------------------------------
26750 -- Universal_Interpretation --
26751 ------------------------------
26752
26753 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
26754 Index : Interp_Index;
26755 It : Interp;
26756
26757 begin
26758 -- The argument may be a formal parameter of an operator or subprogram
26759 -- with multiple interpretations, or else an expression for an actual.
26760
26761 if Nkind (Opnd) = N_Defining_Identifier
26762 or else not Is_Overloaded (Opnd)
26763 then
26764 if Etype (Opnd) = Universal_Integer
26765 or else Etype (Opnd) = Universal_Real
26766 then
26767 return Etype (Opnd);
26768 else
26769 return Empty;
26770 end if;
26771
26772 else
26773 Get_First_Interp (Opnd, Index, It);
26774 while Present (It.Typ) loop
26775 if It.Typ = Universal_Integer
26776 or else It.Typ = Universal_Real
26777 then
26778 return It.Typ;
26779 end if;
26780
26781 Get_Next_Interp (Index, It);
26782 end loop;
26783
26784 return Empty;
26785 end if;
26786 end Universal_Interpretation;
26787
26788 ---------------
26789 -- Unqualify --
26790 ---------------
26791
26792 function Unqualify (Expr : Node_Id) return Node_Id is
26793 begin
26794 -- Recurse to handle unlikely case of multiple levels of qualification
26795
26796 if Nkind (Expr) = N_Qualified_Expression then
26797 return Unqualify (Expression (Expr));
26798
26799 -- Normal case, not a qualified expression
26800
26801 else
26802 return Expr;
26803 end if;
26804 end Unqualify;
26805
26806 -----------------
26807 -- Unqual_Conv --
26808 -----------------
26809
26810 function Unqual_Conv (Expr : Node_Id) return Node_Id is
26811 begin
26812 -- Recurse to handle unlikely case of multiple levels of qualification
26813 -- and/or conversion.
26814
26815 if Nkind_In (Expr, N_Qualified_Expression,
26816 N_Type_Conversion,
26817 N_Unchecked_Type_Conversion)
26818 then
26819 return Unqual_Conv (Expression (Expr));
26820
26821 -- Normal case, not a qualified expression
26822
26823 else
26824 return Expr;
26825 end if;
26826 end Unqual_Conv;
26827
26828 --------------------
26829 -- Validated_View --
26830 --------------------
26831
26832 function Validated_View (Typ : Entity_Id) return Entity_Id is
26833 Continue : Boolean;
26834 Val_Typ : Entity_Id;
26835
26836 begin
26837 Continue := True;
26838 Val_Typ := Base_Type (Typ);
26839
26840 -- Obtain the full view of the input type by stripping away concurrency,
26841 -- derivations, and privacy.
26842
26843 while Continue loop
26844 Continue := False;
26845
26846 if Is_Concurrent_Type (Val_Typ) then
26847 if Present (Corresponding_Record_Type (Val_Typ)) then
26848 Continue := True;
26849 Val_Typ := Corresponding_Record_Type (Val_Typ);
26850 end if;
26851
26852 elsif Is_Derived_Type (Val_Typ) then
26853 Continue := True;
26854 Val_Typ := Etype (Val_Typ);
26855
26856 elsif Is_Private_Type (Val_Typ) then
26857 if Present (Underlying_Full_View (Val_Typ)) then
26858 Continue := True;
26859 Val_Typ := Underlying_Full_View (Val_Typ);
26860
26861 elsif Present (Full_View (Val_Typ)) then
26862 Continue := True;
26863 Val_Typ := Full_View (Val_Typ);
26864 end if;
26865 end if;
26866 end loop;
26867
26868 return Val_Typ;
26869 end Validated_View;
26870
26871 -----------------------
26872 -- Visible_Ancestors --
26873 -----------------------
26874
26875 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
26876 List_1 : Elist_Id;
26877 List_2 : Elist_Id;
26878 Elmt : Elmt_Id;
26879
26880 begin
26881 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
26882
26883 -- Collect all the parents and progenitors of Typ. If the full-view of
26884 -- private parents and progenitors is available then it is used to
26885 -- generate the list of visible ancestors; otherwise their partial
26886 -- view is added to the resulting list.
26887
26888 Collect_Parents
26889 (T => Typ,
26890 List => List_1,
26891 Use_Full_View => True);
26892
26893 Collect_Interfaces
26894 (T => Typ,
26895 Ifaces_List => List_2,
26896 Exclude_Parents => True,
26897 Use_Full_View => True);
26898
26899 -- Join the two lists. Avoid duplications because an interface may
26900 -- simultaneously be parent and progenitor of a type.
26901
26902 Elmt := First_Elmt (List_2);
26903 while Present (Elmt) loop
26904 Append_Unique_Elmt (Node (Elmt), List_1);
26905 Next_Elmt (Elmt);
26906 end loop;
26907
26908 return List_1;
26909 end Visible_Ancestors;
26910
26911 ----------------------
26912 -- Within_Init_Proc --
26913 ----------------------
26914
26915 function Within_Init_Proc return Boolean is
26916 S : Entity_Id;
26917
26918 begin
26919 S := Current_Scope;
26920 while not Is_Overloadable (S) loop
26921 if S = Standard_Standard then
26922 return False;
26923 else
26924 S := Scope (S);
26925 end if;
26926 end loop;
26927
26928 return Is_Init_Proc (S);
26929 end Within_Init_Proc;
26930
26931 ---------------------------
26932 -- Within_Protected_Type --
26933 ---------------------------
26934
26935 function Within_Protected_Type (E : Entity_Id) return Boolean is
26936 Scop : Entity_Id := Scope (E);
26937
26938 begin
26939 while Present (Scop) loop
26940 if Ekind (Scop) = E_Protected_Type then
26941 return True;
26942 end if;
26943
26944 Scop := Scope (Scop);
26945 end loop;
26946
26947 return False;
26948 end Within_Protected_Type;
26949
26950 ------------------
26951 -- Within_Scope --
26952 ------------------
26953
26954 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
26955 begin
26956 return Scope_Within_Or_Same (Scope (E), S);
26957 end Within_Scope;
26958
26959 ----------------------------
26960 -- Within_Subprogram_Call --
26961 ----------------------------
26962
26963 function Within_Subprogram_Call (N : Node_Id) return Boolean is
26964 Par : Node_Id;
26965
26966 begin
26967 -- Climb the parent chain looking for a function or procedure call
26968
26969 Par := N;
26970 while Present (Par) loop
26971 if Nkind_In (Par, N_Entry_Call_Statement,
26972 N_Function_Call,
26973 N_Procedure_Call_Statement)
26974 then
26975 return True;
26976
26977 -- Prevent the search from going too far
26978
26979 elsif Is_Body_Or_Package_Declaration (Par) then
26980 exit;
26981 end if;
26982
26983 Par := Parent (Par);
26984 end loop;
26985
26986 return False;
26987 end Within_Subprogram_Call;
26988
26989 ----------------
26990 -- Wrong_Type --
26991 ----------------
26992
26993 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
26994 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
26995 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
26996
26997 Matching_Field : Entity_Id;
26998 -- Entity to give a more precise suggestion on how to write a one-
26999 -- element positional aggregate.
27000
27001 function Has_One_Matching_Field return Boolean;
27002 -- Determines if Expec_Type is a record type with a single component or
27003 -- discriminant whose type matches the found type or is one dimensional
27004 -- array whose component type matches the found type. In the case of
27005 -- one discriminant, we ignore the variant parts. That's not accurate,
27006 -- but good enough for the warning.
27007
27008 ----------------------------
27009 -- Has_One_Matching_Field --
27010 ----------------------------
27011
27012 function Has_One_Matching_Field return Boolean is
27013 E : Entity_Id;
27014
27015 begin
27016 Matching_Field := Empty;
27017
27018 if Is_Array_Type (Expec_Type)
27019 and then Number_Dimensions (Expec_Type) = 1
27020 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
27021 then
27022 -- Use type name if available. This excludes multidimensional
27023 -- arrays and anonymous arrays.
27024
27025 if Comes_From_Source (Expec_Type) then
27026 Matching_Field := Expec_Type;
27027
27028 -- For an assignment, use name of target
27029
27030 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
27031 and then Is_Entity_Name (Name (Parent (Expr)))
27032 then
27033 Matching_Field := Entity (Name (Parent (Expr)));
27034 end if;
27035
27036 return True;
27037
27038 elsif not Is_Record_Type (Expec_Type) then
27039 return False;
27040
27041 else
27042 E := First_Entity (Expec_Type);
27043 loop
27044 if No (E) then
27045 return False;
27046
27047 elsif not Ekind_In (E, E_Discriminant, E_Component)
27048 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
27049 then
27050 Next_Entity (E);
27051
27052 else
27053 exit;
27054 end if;
27055 end loop;
27056
27057 if not Covers (Etype (E), Found_Type) then
27058 return False;
27059
27060 elsif Present (Next_Entity (E))
27061 and then (Ekind (E) = E_Component
27062 or else Ekind (Next_Entity (E)) = E_Discriminant)
27063 then
27064 return False;
27065
27066 else
27067 Matching_Field := E;
27068 return True;
27069 end if;
27070 end if;
27071 end Has_One_Matching_Field;
27072
27073 -- Start of processing for Wrong_Type
27074
27075 begin
27076 -- Don't output message if either type is Any_Type, or if a message
27077 -- has already been posted for this node. We need to do the latter
27078 -- check explicitly (it is ordinarily done in Errout), because we
27079 -- are using ! to force the output of the error messages.
27080
27081 if Expec_Type = Any_Type
27082 or else Found_Type = Any_Type
27083 or else Error_Posted (Expr)
27084 then
27085 return;
27086
27087 -- If one of the types is a Taft-Amendment type and the other it its
27088 -- completion, it must be an illegal use of a TAT in the spec, for
27089 -- which an error was already emitted. Avoid cascaded errors.
27090
27091 elsif Is_Incomplete_Type (Expec_Type)
27092 and then Has_Completion_In_Body (Expec_Type)
27093 and then Full_View (Expec_Type) = Etype (Expr)
27094 then
27095 return;
27096
27097 elsif Is_Incomplete_Type (Etype (Expr))
27098 and then Has_Completion_In_Body (Etype (Expr))
27099 and then Full_View (Etype (Expr)) = Expec_Type
27100 then
27101 return;
27102
27103 -- In an instance, there is an ongoing problem with completion of
27104 -- types derived from private types. Their structure is what Gigi
27105 -- expects, but the Etype is the parent type rather than the
27106 -- derived private type itself. Do not flag error in this case. The
27107 -- private completion is an entity without a parent, like an Itype.
27108 -- Similarly, full and partial views may be incorrect in the instance.
27109 -- There is no simple way to insure that it is consistent ???
27110
27111 -- A similar view discrepancy can happen in an inlined body, for the
27112 -- same reason: inserted body may be outside of the original package
27113 -- and only partial views are visible at the point of insertion.
27114
27115 -- If In_Generic_Actual (Expr) is True then we cannot assume that
27116 -- the successful semantic analysis of the generic guarantees anything
27117 -- useful about type checking of this instance, so we ignore
27118 -- In_Instance in that case. There may be cases where this is not
27119 -- right (the symptom would probably be rejecting something
27120 -- that ought to be accepted) but we don't currently have any
27121 -- concrete examples of this.
27122
27123 elsif (In_Instance and then not In_Generic_Actual (Expr))
27124 or else In_Inlined_Body
27125 then
27126 if Etype (Etype (Expr)) = Etype (Expected_Type)
27127 and then
27128 (Has_Private_Declaration (Expected_Type)
27129 or else Has_Private_Declaration (Etype (Expr)))
27130 and then No (Parent (Expected_Type))
27131 then
27132 return;
27133
27134 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
27135 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
27136 then
27137 return;
27138
27139 elsif Is_Private_Type (Expected_Type)
27140 and then Present (Full_View (Expected_Type))
27141 and then Covers (Full_View (Expected_Type), Etype (Expr))
27142 then
27143 return;
27144
27145 -- Conversely, type of expression may be the private one
27146
27147 elsif Is_Private_Type (Base_Type (Etype (Expr)))
27148 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
27149 then
27150 return;
27151 end if;
27152 end if;
27153
27154 -- An interesting special check. If the expression is parenthesized
27155 -- and its type corresponds to the type of the sole component of the
27156 -- expected record type, or to the component type of the expected one
27157 -- dimensional array type, then assume we have a bad aggregate attempt.
27158
27159 if Nkind (Expr) in N_Subexpr
27160 and then Paren_Count (Expr) /= 0
27161 and then Has_One_Matching_Field
27162 then
27163 Error_Msg_N ("positional aggregate cannot have one component", Expr);
27164
27165 if Present (Matching_Field) then
27166 if Is_Array_Type (Expec_Type) then
27167 Error_Msg_NE
27168 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
27169 else
27170 Error_Msg_NE
27171 ("\write instead `& ='> ...`", Expr, Matching_Field);
27172 end if;
27173 end if;
27174
27175 -- Another special check, if we are looking for a pool-specific access
27176 -- type and we found an E_Access_Attribute_Type, then we have the case
27177 -- of an Access attribute being used in a context which needs a pool-
27178 -- specific type, which is never allowed. The one extra check we make
27179 -- is that the expected designated type covers the Found_Type.
27180
27181 elsif Is_Access_Type (Expec_Type)
27182 and then Ekind (Found_Type) = E_Access_Attribute_Type
27183 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
27184 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
27185 and then Covers
27186 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
27187 then
27188 Error_Msg_N -- CODEFIX
27189 ("result must be general access type!", Expr);
27190 Error_Msg_NE -- CODEFIX
27191 ("add ALL to }!", Expr, Expec_Type);
27192
27193 -- Another special check, if the expected type is an integer type,
27194 -- but the expression is of type System.Address, and the parent is
27195 -- an addition or subtraction operation whose left operand is the
27196 -- expression in question and whose right operand is of an integral
27197 -- type, then this is an attempt at address arithmetic, so give
27198 -- appropriate message.
27199
27200 elsif Is_Integer_Type (Expec_Type)
27201 and then Is_RTE (Found_Type, RE_Address)
27202 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
27203 and then Expr = Left_Opnd (Parent (Expr))
27204 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
27205 then
27206 Error_Msg_N
27207 ("address arithmetic not predefined in package System",
27208 Parent (Expr));
27209 Error_Msg_N
27210 ("\possible missing with/use of System.Storage_Elements",
27211 Parent (Expr));
27212 return;
27213
27214 -- If the expected type is an anonymous access type, as for access
27215 -- parameters and discriminants, the error is on the designated types.
27216
27217 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
27218 if Comes_From_Source (Expec_Type) then
27219 Error_Msg_NE ("expected}!", Expr, Expec_Type);
27220 else
27221 Error_Msg_NE
27222 ("expected an access type with designated}",
27223 Expr, Designated_Type (Expec_Type));
27224 end if;
27225
27226 if Is_Access_Type (Found_Type)
27227 and then not Comes_From_Source (Found_Type)
27228 then
27229 Error_Msg_NE
27230 ("\\found an access type with designated}!",
27231 Expr, Designated_Type (Found_Type));
27232 else
27233 if From_Limited_With (Found_Type) then
27234 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
27235 Error_Msg_Qual_Level := 99;
27236 Error_Msg_NE -- CODEFIX
27237 ("\\missing `WITH &;", Expr, Scope (Found_Type));
27238 Error_Msg_Qual_Level := 0;
27239 else
27240 Error_Msg_NE ("found}!", Expr, Found_Type);
27241 end if;
27242 end if;
27243
27244 -- Normal case of one type found, some other type expected
27245
27246 else
27247 -- If the names of the two types are the same, see if some number
27248 -- of levels of qualification will help. Don't try more than three
27249 -- levels, and if we get to standard, it's no use (and probably
27250 -- represents an error in the compiler) Also do not bother with
27251 -- internal scope names.
27252
27253 declare
27254 Expec_Scope : Entity_Id;
27255 Found_Scope : Entity_Id;
27256
27257 begin
27258 Expec_Scope := Expec_Type;
27259 Found_Scope := Found_Type;
27260
27261 for Levels in Nat range 0 .. 3 loop
27262 if Chars (Expec_Scope) /= Chars (Found_Scope) then
27263 Error_Msg_Qual_Level := Levels;
27264 exit;
27265 end if;
27266
27267 Expec_Scope := Scope (Expec_Scope);
27268 Found_Scope := Scope (Found_Scope);
27269
27270 exit when Expec_Scope = Standard_Standard
27271 or else Found_Scope = Standard_Standard
27272 or else not Comes_From_Source (Expec_Scope)
27273 or else not Comes_From_Source (Found_Scope);
27274 end loop;
27275 end;
27276
27277 if Is_Record_Type (Expec_Type)
27278 and then Present (Corresponding_Remote_Type (Expec_Type))
27279 then
27280 Error_Msg_NE ("expected}!", Expr,
27281 Corresponding_Remote_Type (Expec_Type));
27282 else
27283 Error_Msg_NE ("expected}!", Expr, Expec_Type);
27284 end if;
27285
27286 if Is_Entity_Name (Expr)
27287 and then Is_Package_Or_Generic_Package (Entity (Expr))
27288 then
27289 Error_Msg_N ("\\found package name!", Expr);
27290
27291 elsif Is_Entity_Name (Expr)
27292 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
27293 then
27294 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
27295 Error_Msg_N
27296 ("found procedure name, possibly missing Access attribute!",
27297 Expr);
27298 else
27299 Error_Msg_N
27300 ("\\found procedure name instead of function!", Expr);
27301 end if;
27302
27303 elsif Nkind (Expr) = N_Function_Call
27304 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
27305 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
27306 and then No (Parameter_Associations (Expr))
27307 then
27308 Error_Msg_N
27309 ("found function name, possibly missing Access attribute!",
27310 Expr);
27311
27312 -- Catch common error: a prefix or infix operator which is not
27313 -- directly visible because the type isn't.
27314
27315 elsif Nkind (Expr) in N_Op
27316 and then Is_Overloaded (Expr)
27317 and then not Is_Immediately_Visible (Expec_Type)
27318 and then not Is_Potentially_Use_Visible (Expec_Type)
27319 and then not In_Use (Expec_Type)
27320 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
27321 then
27322 Error_Msg_N
27323 ("operator of the type is not directly visible!", Expr);
27324
27325 elsif Ekind (Found_Type) = E_Void
27326 and then Present (Parent (Found_Type))
27327 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
27328 then
27329 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
27330
27331 else
27332 Error_Msg_NE ("\\found}!", Expr, Found_Type);
27333 end if;
27334
27335 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
27336 -- of the same modular type, and (M1 and M2) = 0 was intended.
27337
27338 if Expec_Type = Standard_Boolean
27339 and then Is_Modular_Integer_Type (Found_Type)
27340 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
27341 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
27342 then
27343 declare
27344 Op : constant Node_Id := Right_Opnd (Parent (Expr));
27345 L : constant Node_Id := Left_Opnd (Op);
27346 R : constant Node_Id := Right_Opnd (Op);
27347
27348 begin
27349 -- The case for the message is when the left operand of the
27350 -- comparison is the same modular type, or when it is an
27351 -- integer literal (or other universal integer expression),
27352 -- which would have been typed as the modular type if the
27353 -- parens had been there.
27354
27355 if (Etype (L) = Found_Type
27356 or else
27357 Etype (L) = Universal_Integer)
27358 and then Is_Integer_Type (Etype (R))
27359 then
27360 Error_Msg_N
27361 ("\\possible missing parens for modular operation", Expr);
27362 end if;
27363 end;
27364 end if;
27365
27366 -- Reset error message qualification indication
27367
27368 Error_Msg_Qual_Level := 0;
27369 end if;
27370 end Wrong_Type;
27371
27372 --------------------------------
27373 -- Yields_Synchronized_Object --
27374 --------------------------------
27375
27376 function Yields_Synchronized_Object (Typ : Entity_Id) return Boolean is
27377 Has_Sync_Comp : Boolean := False;
27378 Id : Entity_Id;
27379
27380 begin
27381 -- An array type yields a synchronized object if its component type
27382 -- yields a synchronized object.
27383
27384 if Is_Array_Type (Typ) then
27385 return Yields_Synchronized_Object (Component_Type (Typ));
27386
27387 -- A descendant of type Ada.Synchronous_Task_Control.Suspension_Object
27388 -- yields a synchronized object by default.
27389
27390 elsif Is_Descendant_Of_Suspension_Object (Typ) then
27391 return True;
27392
27393 -- A protected type yields a synchronized object by default
27394
27395 elsif Is_Protected_Type (Typ) then
27396 return True;
27397
27398 -- A record type or type extension yields a synchronized object when its
27399 -- discriminants (if any) lack default values and all components are of
27400 -- a type that yields a synchronized object.
27401
27402 elsif Is_Record_Type (Typ) then
27403
27404 -- Inspect all entities defined in the scope of the type, looking for
27405 -- components of a type that does not yield a synchronized object or
27406 -- for discriminants with default values.
27407
27408 Id := First_Entity (Typ);
27409 while Present (Id) loop
27410 if Comes_From_Source (Id) then
27411 if Ekind (Id) = E_Component then
27412 if Yields_Synchronized_Object (Etype (Id)) then
27413 Has_Sync_Comp := True;
27414
27415 -- The component does not yield a synchronized object
27416
27417 else
27418 return False;
27419 end if;
27420
27421 elsif Ekind (Id) = E_Discriminant
27422 and then Present (Expression (Parent (Id)))
27423 then
27424 return False;
27425 end if;
27426 end if;
27427
27428 Next_Entity (Id);
27429 end loop;
27430
27431 -- Ensure that the parent type of a type extension yields a
27432 -- synchronized object.
27433
27434 if Etype (Typ) /= Typ
27435 and then not Is_Private_Type (Etype (Typ))
27436 and then not Yields_Synchronized_Object (Etype (Typ))
27437 then
27438 return False;
27439 end if;
27440
27441 -- If we get here, then all discriminants lack default values and all
27442 -- components are of a type that yields a synchronized object.
27443
27444 return Has_Sync_Comp;
27445
27446 -- A synchronized interface type yields a synchronized object by default
27447
27448 elsif Is_Synchronized_Interface (Typ) then
27449 return True;
27450
27451 -- A task type yields a synchronized object by default
27452
27453 elsif Is_Task_Type (Typ) then
27454 return True;
27455
27456 -- A private type yields a synchronized object if its underlying type
27457 -- does.
27458
27459 elsif Is_Private_Type (Typ)
27460 and then Present (Underlying_Type (Typ))
27461 then
27462 return Yields_Synchronized_Object (Underlying_Type (Typ));
27463
27464 -- Otherwise the type does not yield a synchronized object
27465
27466 else
27467 return False;
27468 end if;
27469 end Yields_Synchronized_Object;
27470
27471 ---------------------------
27472 -- Yields_Universal_Type --
27473 ---------------------------
27474
27475 function Yields_Universal_Type (N : Node_Id) return Boolean is
27476 begin
27477 -- Integer and real literals are of a universal type
27478
27479 if Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
27480 return True;
27481
27482 -- The values of certain attributes are of a universal type
27483
27484 elsif Nkind (N) = N_Attribute_Reference then
27485 return
27486 Universal_Type_Attribute (Get_Attribute_Id (Attribute_Name (N)));
27487
27488 -- ??? There are possibly other cases to consider
27489
27490 else
27491 return False;
27492 end if;
27493 end Yields_Universal_Type;
27494
27495 package body Interval_Lists is
27496
27497 function In_Interval
27498 (Value : Uint; Interval : Discrete_Interval) return Boolean;
27499 -- Does the given value lie within the given interval?
27500
27501 -----------------
27502 -- In_Interval --
27503 -----------------
27504 function In_Interval
27505 (Value : Uint; Interval : Discrete_Interval) return Boolean is
27506 begin
27507 return Value >= Interval.Low and then Value <= Interval.High;
27508 end In_Interval;
27509
27510 procedure Check_Consistency (Intervals : Discrete_Interval_List);
27511 -- Check that list is sorted, lacks null intervals, and has gaps
27512 -- between intervals.
27513
27514 ------------------------
27515 -- Check_Consistency --
27516 ------------------------
27517 procedure Check_Consistency (Intervals : Discrete_Interval_List) is
27518 begin
27519 if Serious_Errors_Detected > 0 then
27520 return;
27521 end if;
27522
27523 -- low bound is 1 and high bound equals length
27524 pragma Assert (Intervals'First = 1 and Intervals'Last >= 0);
27525 for Idx in Intervals'Range loop
27526 -- each interval is non-null
27527 pragma Assert (Intervals (Idx).Low <= Intervals (Idx).High);
27528 if Idx /= Intervals'First then
27529 -- intervals are sorted with non-empty gaps between them
27530 pragma Assert
27531 (Intervals (Idx - 1).High < (Intervals (Idx).Low - 1));
27532 null;
27533 end if;
27534 end loop;
27535 end Check_Consistency;
27536
27537 function Chosen_Interval (Choice : Node_Id) return Discrete_Interval;
27538 -- Given an element of a Discrete_Choices list, a
27539 -- Static_Discrete_Predicate list, or an Others_Discrete_Choices
27540 -- list (but not an N_Others_Choice node) return the corresponding
27541 -- interval. If an element that does not represent a single
27542 -- contiguous interval due to a static predicate (or which
27543 -- represents a single contiguous interval whose bounds depend on
27544 -- a static predicate) is encountered, then that is an error on the
27545 -- part of whoever built the list in question.
27546
27547 ---------------------
27548 -- Chosen_Interval --
27549 ---------------------
27550 function Chosen_Interval (Choice : Node_Id) return Discrete_Interval is
27551 begin
27552 case Nkind (Choice) is
27553 when N_Range =>
27554 return (Low => Expr_Value (Low_Bound (Choice)),
27555 High => Expr_Value (High_Bound (Choice)));
27556
27557 when N_Subtype_Indication =>
27558 declare
27559 Range_Exp : constant Node_Id
27560 := Range_Expression (Constraint (Choice));
27561 begin
27562 return (Low => Expr_Value (Low_Bound (Range_Exp)),
27563 High => Expr_Value (High_Bound (Range_Exp)));
27564 end;
27565
27566 when N_Others_Choice =>
27567 raise Program_Error;
27568
27569 when others =>
27570 if Is_Entity_Name (Choice) and then Is_Type (Entity (Choice))
27571 then
27572 return
27573 (Low => Expr_Value (Type_Low_Bound (Entity (Choice))),
27574 High => Expr_Value (Type_High_Bound (Entity (Choice))));
27575 else
27576 -- an expression
27577 return (Low | High => Expr_Value (Choice));
27578 end if;
27579 end case;
27580 end Chosen_Interval;
27581
27582 --------------------
27583 -- Type_Intervals --
27584 --------------------
27585 function Type_Intervals
27586 (Typ : Entity_Id) return Discrete_Interval_List
27587 is
27588 begin
27589 if Has_Static_Predicate (Typ) then
27590 declare
27591 -- No sorting or merging needed
27592 SDP_List : constant List_Id := Static_Discrete_Predicate (Typ);
27593 Range_Or_Expr : Node_Id := First (SDP_List);
27594 Result :
27595 Discrete_Interval_List (1 .. List_Length (SDP_List));
27596 begin
27597 for Idx in Result'Range loop
27598 Result (Idx) := Chosen_Interval (Range_Or_Expr);
27599 Range_Or_Expr := Next (Range_Or_Expr);
27600 end loop;
27601 pragma Assert (not Present (Range_Or_Expr));
27602 Check_Consistency (Result);
27603 return Result;
27604 end;
27605 else
27606 declare
27607 Low : constant Uint := Expr_Value (Type_Low_Bound (Typ));
27608 High : constant Uint := Expr_Value (Type_High_Bound (Typ));
27609 begin
27610 if Low > High then
27611 declare
27612 Null_Array : Discrete_Interval_List (1 .. 0);
27613 begin
27614 return Null_Array;
27615 end;
27616 else
27617 return (1 => (Low => Low, High => High));
27618 end if;
27619 end;
27620 end if;
27621 end Type_Intervals;
27622
27623 procedure Normalize_Interval_List
27624 (List : in out Discrete_Interval_List; Last : out Nat);
27625 -- Perform sorting and merging as required by Check_Consistency.
27626
27627 -----------------------------
27628 -- Normalize_Interval_List --
27629 -----------------------------
27630 procedure Normalize_Interval_List
27631 (List : in out Discrete_Interval_List; Last : out Nat) is
27632
27633 procedure Move_Interval (From, To : Natural);
27634 -- Copy interval from one location to another
27635
27636 function Lt_Interval (Idx1, Idx2 : Natural) return Boolean;
27637 -- Compare two list elements
27638
27639 Temp_0 : Discrete_Interval := (others => Uint_0);
27640 -- cope with Heap_Sort_G idiosyncrasies.
27641
27642 function Read_Interval (From : Natural) return Discrete_Interval;
27643 -- Normal array indexing unless From = 0
27644
27645 -------------------
27646 -- Read_Interval --
27647 -------------------
27648 function Read_Interval (From : Natural) return Discrete_Interval is
27649 begin
27650 if From = 0 then
27651 return Temp_0;
27652 else
27653 return List (Pos (From));
27654 end if;
27655 end Read_Interval;
27656
27657 -------------------
27658 -- Move_Interval --
27659 -------------------
27660 procedure Move_Interval (From, To : Natural) is
27661 Rhs : constant Discrete_Interval := Read_Interval (From);
27662 begin
27663 if To = 0 then
27664 Temp_0 := Rhs;
27665 else
27666 List (Pos (To)) := Rhs;
27667 end if;
27668 end Move_Interval;
27669
27670 -----------------
27671 -- Lt_Interval --
27672 -----------------
27673 function Lt_Interval (Idx1, Idx2 : Natural) return Boolean is
27674 Elem1 : constant Discrete_Interval := Read_Interval (Idx1);
27675 Elem2 : constant Discrete_Interval := Read_Interval (Idx2);
27676 Null_1 : constant Boolean := Elem1.Low > Elem1.High;
27677 Null_2 : constant Boolean := Elem2.Low > Elem2.High;
27678 begin
27679 if Null_1 /= Null_2 then
27680 -- So that sorting moves null intervals to high end
27681 return Null_2;
27682 elsif Elem1.Low /= Elem2.Low then
27683 return Elem1.Low < Elem2.Low;
27684 else
27685 return Elem1.High < Elem2.High;
27686 end if;
27687 end Lt_Interval;
27688
27689 package Interval_Sorting is
27690 new Gnat.Heap_Sort_G (Move_Interval, Lt_Interval);
27691
27692 function Is_Null (Idx : Pos) return Boolean;
27693 -- True iff List (Idx) defines a null range
27694
27695 function Is_Null (Idx : Pos) return Boolean is
27696 begin
27697 return List (Idx).Low > List (Idx).High;
27698 end Is_Null;
27699
27700 procedure Merge_Intervals (Null_Interval_Count : out Nat);
27701 -- Merge contiguous ranges by replacing one with merged range
27702 -- and the other with a null value. Return a count of the
27703 -- null intervals, both preexisting and those introduced by
27704 -- merging.
27705
27706 ---------------------
27707 -- Merge_Intervals --
27708 ---------------------
27709 procedure Merge_Intervals (Null_Interval_Count : out Nat) is
27710 Not_Null : Pos range List'Range;
27711 -- Index of the most recently examined non-null interval
27712
27713 Null_Interval : constant Discrete_Interval
27714 := (Low => Uint_1, High => Uint_0); -- any null range ok here
27715 begin
27716 if List'Length = 0 or else Is_Null (List'First) then
27717 Null_Interval_Count := List'Length;
27718 -- no non-null elements, so no merge candidates
27719 return;
27720 end if;
27721
27722 Null_Interval_Count := 0;
27723 Not_Null := List'First;
27724 for Idx in List'First + 1 .. List'Last loop
27725 if Is_Null (Idx) then
27726 -- all remaining elements are null
27727 Null_Interval_Count :=
27728 Null_Interval_Count + List (Idx .. List'Last)'Length;
27729 return;
27730 elsif List (Idx).Low = List (Not_Null).High + 1 then
27731 -- Merge the two intervals into one; discard the other
27732 List (Not_Null).High := List (Idx).High;
27733 List (Idx) := Null_Interval;
27734 Null_Interval_Count := Null_Interval_Count + 1;
27735 else
27736 pragma Assert (List (Idx).Low > List (Not_Null).High);
27737 Not_Null := Idx;
27738 end if;
27739 end loop;
27740 end Merge_Intervals;
27741 begin
27742 Interval_Sorting.Sort (Natural (List'Last));
27743 declare
27744 Null_Interval_Count : Nat;
27745 begin
27746 Merge_Intervals (Null_Interval_Count);
27747 Last := List'Last - Null_Interval_Count;
27748 if Null_Interval_Count /= 0 then
27749 -- Move null intervals introduced during merging to high end
27750 Interval_Sorting.Sort (Natural (List'Last));
27751 end if;
27752 end;
27753 end Normalize_Interval_List;
27754
27755 ---------------------------
27756 -- Choice_List_Intervals --
27757 ---------------------------
27758 function Choice_List_Intervals
27759 (Discrete_Choices : List_Id) return Discrete_Interval_List
27760 is
27761 function Unmerged_Choice_Count return Nat;
27762 -- The number of intervals before adjacent intervals are merged.
27763
27764 ---------------------------
27765 -- Unmerged_Choice_Count --
27766 ---------------------------
27767 function Unmerged_Choice_Count return Nat is
27768 Choice : Node_Id := First (Discrete_Choices);
27769 Count : Nat := 0;
27770 begin
27771 while Present (Choice) loop
27772 -- Non-contiguous choices involving static predicates
27773 -- have already been normalized away.
27774
27775 if Nkind (Choice) = N_Others_Choice then
27776 Count :=
27777 Count + List_Length (Others_Discrete_Choices (Choice));
27778 else
27779 Count := Count + 1; -- an ordinary expression or range
27780 end if;
27781
27782 Choice := Next (Choice);
27783 end loop;
27784 return Count;
27785 end Unmerged_Choice_Count;
27786
27787 Choice : Node_Id := First (Discrete_Choices);
27788 Result : Discrete_Interval_List (1 .. Unmerged_Choice_Count);
27789 Count : Nat := 0;
27790 begin
27791 while Present (Choice) loop
27792 if Nkind (Choice) = N_Others_Choice then
27793 declare
27794 Others_Choice : Node_Id
27795 := First (Others_Discrete_Choices (Choice));
27796 begin
27797 while Present (Others_Choice) loop
27798 Count := Count + 1;
27799 Result (Count) := Chosen_Interval (Others_Choice);
27800 Others_Choice := Next (Others_Choice);
27801 end loop;
27802 end;
27803 else
27804 Count := Count + 1;
27805 Result (Count) := Chosen_Interval (Choice);
27806 end if;
27807 Choice := Next (Choice);
27808 end loop;
27809 pragma Assert (Count = Result'Last);
27810 Normalize_Interval_List (Result, Count);
27811 Check_Consistency (Result (1 .. Count));
27812 return Result (1 .. Count);
27813 end Choice_List_Intervals;
27814
27815 ---------------
27816 -- Is_Subset --
27817 ---------------
27818 function Is_Subset
27819 (Subset, Of_Set : Discrete_Interval_List) return Boolean
27820 is
27821 -- Returns True iff for each interval of Subset we can find
27822 -- a single interval of Of_Set which contains the Subset interval.
27823 begin
27824 if Of_Set'Length = 0 then
27825 return Subset'Length = 0;
27826 end if;
27827
27828 declare
27829 Set_Index : Pos range Of_Set'Range := Of_Set'First;
27830 begin
27831 for Ss_Idx in Subset'Range loop
27832 while not In_Interval
27833 (Value => Subset (Ss_Idx).Low,
27834 Interval => Of_Set (Set_Index))
27835 loop
27836 if Set_Index = Of_Set'Last then
27837 return False;
27838 end if;
27839 Set_Index := Set_Index + 1;
27840 end loop;
27841
27842 if not In_Interval
27843 (Value => Subset (Ss_Idx).High,
27844 Interval => Of_Set (Set_Index))
27845 then
27846 return False;
27847 end if;
27848 end loop;
27849 end;
27850
27851 return True;
27852 end Is_Subset;
27853
27854 end Interval_Lists;
27855
27856 begin
27857 Erroutc.Subprogram_Name_Ptr := Subprogram_Name'Access;
27858 end Sem_Util;