4d19c6147143f25fc54df56f9bb86b569a1b7819
[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 Atree; use Atree;
30 with Casing; use Casing;
31 with Checks; use Checks;
32 with Debug; use Debug;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Erroutc; use Erroutc;
36 with Exp_Ch11; use Exp_Ch11;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet.Sp; use Namet.Sp;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Output; use Output;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Attr; use Sem_Attr;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Prag; use Sem_Prag;
58 with Sem_Res; use Sem_Res;
59 with Sem_Warn; use Sem_Warn;
60 with Sem_Type; use Sem_Type;
61 with Sinfo; use Sinfo;
62 with Sinput; use Sinput;
63 with Stand; use Stand;
64 with Style;
65 with Stringt; use Stringt;
66 with Targparm; use Targparm;
67 with Tbuild; use Tbuild;
68 with Ttypes; use Ttypes;
69 with Uname; use Uname;
70
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_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
126 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
127 -- with discriminants whose default values are static, examine only the
128 -- components in the selected variant to determine whether all of them
129 -- have a default.
130
131 type Null_Status_Kind is
132 (Is_Null,
133 -- This value indicates that a subexpression is known to have a null
134 -- value at compile time.
135
136 Is_Non_Null,
137 -- This value indicates that a subexpression is known to have a non-null
138 -- value at compile time.
139
140 Unknown);
141 -- This value indicates that it cannot be determined at compile time
142 -- whether a subexpression yields a null or non-null value.
143
144 function Null_Status (N : Node_Id) return Null_Status_Kind;
145 -- Determine whether subexpression N of an access type yields a null value,
146 -- a non-null value, or the value cannot be determined at compile time. The
147 -- routine does not take simple flow diagnostics into account, it relies on
148 -- static facts such as the presence of null exclusions.
149
150 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
151 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
152 -- ???We retain the old and new algorithms for Requires_Transient_Scope for
153 -- the time being. New_Requires_Transient_Scope is used by default; the
154 -- debug switch -gnatdQ can be used to do Old_Requires_Transient_Scope
155 -- instead. The intent is to use this temporarily to measure before/after
156 -- efficiency. Note: when this temporary code is removed, the documentation
157 -- of dQ in debug.adb should be removed.
158
159 procedure Results_Differ
160 (Id : Entity_Id;
161 Old_Val : Boolean;
162 New_Val : Boolean);
163 -- ???Debugging code. Called when the Old_Val and New_Val differ. This
164 -- routine will be removed eventially when New_Requires_Transient_Scope
165 -- becomes Requires_Transient_Scope and Old_Requires_Transient_Scope is
166 -- eliminated.
167
168 function Subprogram_Name (N : Node_Id) return String;
169 -- Return the fully qualified name of the enclosing subprogram for the
170 -- given node N, with file:line:col information appended, e.g.
171 -- "subp:file:line:col", corresponding to the source location of the
172 -- body of the subprogram.
173
174 ------------------------------
175 -- Abstract_Interface_List --
176 ------------------------------
177
178 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
179 Nod : Node_Id;
180
181 begin
182 if Is_Concurrent_Type (Typ) then
183
184 -- If we are dealing with a synchronized subtype, go to the base
185 -- type, whose declaration has the interface list.
186
187 Nod := Declaration_Node (Base_Type (Typ));
188
189 if Nkind_In (Nod, N_Full_Type_Declaration,
190 N_Private_Type_Declaration)
191 then
192 return Empty_List;
193 end if;
194
195 elsif Ekind (Typ) = E_Record_Type_With_Private then
196 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
197 Nod := Type_Definition (Parent (Typ));
198
199 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
200 if Present (Full_View (Typ))
201 and then
202 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
203 then
204 Nod := Type_Definition (Parent (Full_View (Typ)));
205
206 -- If the full-view is not available we cannot do anything else
207 -- here (the source has errors).
208
209 else
210 return Empty_List;
211 end if;
212
213 -- Support for generic formals with interfaces is still missing ???
214
215 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
216 return Empty_List;
217
218 else
219 pragma Assert
220 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
221 Nod := Parent (Typ);
222 end if;
223
224 elsif Ekind (Typ) = E_Record_Subtype then
225 Nod := Type_Definition (Parent (Etype (Typ)));
226
227 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
228
229 -- Recurse, because parent may still be a private extension. Also
230 -- note that the full view of the subtype or the full view of its
231 -- base type may (both) be unavailable.
232
233 return Abstract_Interface_List (Etype (Typ));
234
235 elsif Ekind (Typ) = E_Record_Type then
236 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
237 Nod := Formal_Type_Definition (Parent (Typ));
238 else
239 Nod := Type_Definition (Parent (Typ));
240 end if;
241
242 -- Otherwise the type is of a kind which does not implement interfaces
243
244 else
245 return Empty_List;
246 end if;
247
248 return Interface_List (Nod);
249 end Abstract_Interface_List;
250
251 --------------------------------
252 -- Add_Access_Type_To_Process --
253 --------------------------------
254
255 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
256 L : Elist_Id;
257
258 begin
259 Ensure_Freeze_Node (E);
260 L := Access_Types_To_Process (Freeze_Node (E));
261
262 if No (L) then
263 L := New_Elmt_List;
264 Set_Access_Types_To_Process (Freeze_Node (E), L);
265 end if;
266
267 Append_Elmt (A, L);
268 end Add_Access_Type_To_Process;
269
270 --------------------------
271 -- Add_Block_Identifier --
272 --------------------------
273
274 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
275 Loc : constant Source_Ptr := Sloc (N);
276
277 begin
278 pragma Assert (Nkind (N) = N_Block_Statement);
279
280 -- The block already has a label, return its entity
281
282 if Present (Identifier (N)) then
283 Id := Entity (Identifier (N));
284
285 -- Create a new block label and set its attributes
286
287 else
288 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
289 Set_Etype (Id, Standard_Void_Type);
290 Set_Parent (Id, N);
291
292 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
293 Set_Block_Node (Id, Identifier (N));
294 end if;
295 end Add_Block_Identifier;
296
297 ----------------------------
298 -- Add_Global_Declaration --
299 ----------------------------
300
301 procedure Add_Global_Declaration (N : Node_Id) is
302 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
303
304 begin
305 if No (Declarations (Aux_Node)) then
306 Set_Declarations (Aux_Node, New_List);
307 end if;
308
309 Append_To (Declarations (Aux_Node), N);
310 Analyze (N);
311 end Add_Global_Declaration;
312
313 --------------------------------
314 -- Address_Integer_Convert_OK --
315 --------------------------------
316
317 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
318 begin
319 if Allow_Integer_Address
320 and then ((Is_Descendant_Of_Address (T1)
321 and then Is_Private_Type (T1)
322 and then Is_Integer_Type (T2))
323 or else
324 (Is_Descendant_Of_Address (T2)
325 and then Is_Private_Type (T2)
326 and then Is_Integer_Type (T1)))
327 then
328 return True;
329 else
330 return False;
331 end if;
332 end Address_Integer_Convert_OK;
333
334 -------------------
335 -- Address_Value --
336 -------------------
337
338 function Address_Value (N : Node_Id) return Node_Id is
339 Expr : Node_Id := N;
340
341 begin
342 loop
343 -- For constant, get constant expression
344
345 if Is_Entity_Name (Expr)
346 and then Ekind (Entity (Expr)) = E_Constant
347 then
348 Expr := Constant_Value (Entity (Expr));
349
350 -- For unchecked conversion, get result to convert
351
352 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
353 Expr := Expression (Expr);
354
355 -- For (common case) of To_Address call, get argument
356
357 elsif Nkind (Expr) = N_Function_Call
358 and then Is_Entity_Name (Name (Expr))
359 and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
360 then
361 Expr := First (Parameter_Associations (Expr));
362
363 if Nkind (Expr) = N_Parameter_Association then
364 Expr := Explicit_Actual_Parameter (Expr);
365 end if;
366
367 -- We finally have the real expression
368
369 else
370 exit;
371 end if;
372 end loop;
373
374 return Expr;
375 end Address_Value;
376
377 -----------------
378 -- Addressable --
379 -----------------
380
381 -- For now, just 8/16/32/64
382
383 function Addressable (V : Uint) return Boolean is
384 begin
385 return V = Uint_8 or else
386 V = Uint_16 or else
387 V = Uint_32 or else
388 V = Uint_64;
389 end Addressable;
390
391 function Addressable (V : Int) return Boolean is
392 begin
393 return V = 8 or else
394 V = 16 or else
395 V = 32 or else
396 V = 64;
397 end Addressable;
398
399 ---------------------------------
400 -- Aggregate_Constraint_Checks --
401 ---------------------------------
402
403 procedure Aggregate_Constraint_Checks
404 (Exp : Node_Id;
405 Check_Typ : Entity_Id)
406 is
407 Exp_Typ : constant Entity_Id := Etype (Exp);
408
409 begin
410 if Raises_Constraint_Error (Exp) then
411 return;
412 end if;
413
414 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
415 -- component's type to force the appropriate accessibility checks.
416
417 -- Ada 2005 (AI-231): Generate conversion to the null-excluding type to
418 -- force the corresponding run-time check
419
420 if Is_Access_Type (Check_Typ)
421 and then Is_Local_Anonymous_Access (Check_Typ)
422 then
423 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
424 Analyze_And_Resolve (Exp, Check_Typ);
425 Check_Unset_Reference (Exp);
426 end if;
427
428 -- What follows is really expansion activity, so check that expansion
429 -- is on and is allowed. In GNATprove mode, we also want check flags to
430 -- be added in the tree, so that the formal verification can rely on
431 -- those to be present. In GNATprove mode for formal verification, some
432 -- treatment typically only done during expansion needs to be performed
433 -- on the tree, but it should not be applied inside generics. Otherwise,
434 -- this breaks the name resolution mechanism for generic instances.
435
436 if not Expander_Active
437 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
438 then
439 return;
440 end if;
441
442 if Is_Access_Type (Check_Typ)
443 and then Can_Never_Be_Null (Check_Typ)
444 and then not Can_Never_Be_Null (Exp_Typ)
445 then
446 Install_Null_Excluding_Check (Exp);
447 end if;
448
449 -- First check if we have to insert discriminant checks
450
451 if Has_Discriminants (Exp_Typ) then
452 Apply_Discriminant_Check (Exp, Check_Typ);
453
454 -- Next emit length checks for array aggregates
455
456 elsif Is_Array_Type (Exp_Typ) then
457 Apply_Length_Check (Exp, Check_Typ);
458
459 -- Finally emit scalar and string checks. If we are dealing with a
460 -- scalar literal we need to check by hand because the Etype of
461 -- literals is not necessarily correct.
462
463 elsif Is_Scalar_Type (Exp_Typ)
464 and then Compile_Time_Known_Value (Exp)
465 then
466 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
467 Apply_Compile_Time_Constraint_Error
468 (Exp, "value not in range of}??", CE_Range_Check_Failed,
469 Ent => Base_Type (Check_Typ),
470 Typ => Base_Type (Check_Typ));
471
472 elsif Is_Out_Of_Range (Exp, Check_Typ) then
473 Apply_Compile_Time_Constraint_Error
474 (Exp, "value not in range of}??", CE_Range_Check_Failed,
475 Ent => Check_Typ,
476 Typ => Check_Typ);
477
478 elsif not Range_Checks_Suppressed (Check_Typ) then
479 Apply_Scalar_Range_Check (Exp, Check_Typ);
480 end if;
481
482 -- Verify that target type is also scalar, to prevent view anomalies
483 -- in instantiations.
484
485 elsif (Is_Scalar_Type (Exp_Typ)
486 or else Nkind (Exp) = N_String_Literal)
487 and then Is_Scalar_Type (Check_Typ)
488 and then Exp_Typ /= Check_Typ
489 then
490 if Is_Entity_Name (Exp)
491 and then Ekind (Entity (Exp)) = E_Constant
492 then
493 -- If expression is a constant, it is worthwhile checking whether
494 -- it is a bound of the type.
495
496 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
497 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
498 or else
499 (Is_Entity_Name (Type_High_Bound (Check_Typ))
500 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
501 then
502 return;
503
504 else
505 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
506 Analyze_And_Resolve (Exp, Check_Typ);
507 Check_Unset_Reference (Exp);
508 end if;
509
510 -- Could use a comment on this case ???
511
512 else
513 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
514 Analyze_And_Resolve (Exp, Check_Typ);
515 Check_Unset_Reference (Exp);
516 end if;
517
518 end if;
519 end Aggregate_Constraint_Checks;
520
521 -----------------------
522 -- Alignment_In_Bits --
523 -----------------------
524
525 function Alignment_In_Bits (E : Entity_Id) return Uint is
526 begin
527 return Alignment (E) * System_Storage_Unit;
528 end Alignment_In_Bits;
529
530 --------------------------------------
531 -- All_Composite_Constraints_Static --
532 --------------------------------------
533
534 function All_Composite_Constraints_Static
535 (Constr : Node_Id) return Boolean
536 is
537 begin
538 if No (Constr) or else Error_Posted (Constr) then
539 return True;
540 end if;
541
542 case Nkind (Constr) is
543 when N_Subexpr =>
544 if Nkind (Constr) in N_Has_Entity
545 and then Present (Entity (Constr))
546 then
547 if Is_Type (Entity (Constr)) then
548 return
549 not Is_Discrete_Type (Entity (Constr))
550 or else Is_OK_Static_Subtype (Entity (Constr));
551 end if;
552
553 elsif Nkind (Constr) = N_Range then
554 return
555 Is_OK_Static_Expression (Low_Bound (Constr))
556 and then
557 Is_OK_Static_Expression (High_Bound (Constr));
558
559 elsif Nkind (Constr) = N_Attribute_Reference
560 and then Attribute_Name (Constr) = Name_Range
561 then
562 return
563 Is_OK_Static_Expression
564 (Type_Low_Bound (Etype (Prefix (Constr))))
565 and then
566 Is_OK_Static_Expression
567 (Type_High_Bound (Etype (Prefix (Constr))));
568 end if;
569
570 return
571 not Present (Etype (Constr)) -- previous error
572 or else not Is_Discrete_Type (Etype (Constr))
573 or else Is_OK_Static_Expression (Constr);
574
575 when N_Discriminant_Association =>
576 return All_Composite_Constraints_Static (Expression (Constr));
577
578 when N_Range_Constraint =>
579 return
580 All_Composite_Constraints_Static (Range_Expression (Constr));
581
582 when N_Index_Or_Discriminant_Constraint =>
583 declare
584 One_Cstr : Entity_Id;
585 begin
586 One_Cstr := First (Constraints (Constr));
587 while Present (One_Cstr) loop
588 if not All_Composite_Constraints_Static (One_Cstr) then
589 return False;
590 end if;
591
592 Next (One_Cstr);
593 end loop;
594 end;
595
596 return True;
597
598 when N_Subtype_Indication =>
599 return
600 All_Composite_Constraints_Static (Subtype_Mark (Constr))
601 and then
602 All_Composite_Constraints_Static (Constraint (Constr));
603
604 when others =>
605 raise Program_Error;
606 end case;
607 end All_Composite_Constraints_Static;
608
609 ------------------------
610 -- Append_Entity_Name --
611 ------------------------
612
613 procedure Append_Entity_Name (Buf : in out Bounded_String; E : Entity_Id) is
614 Temp : Bounded_String;
615
616 procedure Inner (E : Entity_Id);
617 -- Inner recursive routine, keep outer routine nonrecursive to ease
618 -- debugging when we get strange results from this routine.
619
620 -----------
621 -- Inner --
622 -----------
623
624 procedure Inner (E : Entity_Id) is
625 Scop : Node_Id;
626
627 begin
628 -- If entity has an internal name, skip by it, and print its scope.
629 -- Note that we strip a final R from the name before the test; this
630 -- is needed for some cases of instantiations.
631
632 declare
633 E_Name : Bounded_String;
634
635 begin
636 Append (E_Name, Chars (E));
637
638 if E_Name.Chars (E_Name.Length) = 'R' then
639 E_Name.Length := E_Name.Length - 1;
640 end if;
641
642 if Is_Internal_Name (E_Name) then
643 Inner (Scope (E));
644 return;
645 end if;
646 end;
647
648 Scop := Scope (E);
649
650 -- Just print entity name if its scope is at the outer level
651
652 if Scop = Standard_Standard then
653 null;
654
655 -- If scope comes from source, write scope and entity
656
657 elsif Comes_From_Source (Scop) then
658 Append_Entity_Name (Temp, Scop);
659 Append (Temp, '.');
660
661 -- If in wrapper package skip past it
662
663 elsif Present (Scop) and then Is_Wrapper_Package (Scop) then
664 Append_Entity_Name (Temp, Scope (Scop));
665 Append (Temp, '.');
666
667 -- Otherwise nothing to output (happens in unnamed block statements)
668
669 else
670 null;
671 end if;
672
673 -- Output the name
674
675 declare
676 E_Name : Bounded_String;
677
678 begin
679 Append_Unqualified_Decoded (E_Name, Chars (E));
680
681 -- Remove trailing upper-case letters from the name (useful for
682 -- dealing with some cases of internal names generated in the case
683 -- of references from within a generic).
684
685 while E_Name.Length > 1
686 and then E_Name.Chars (E_Name.Length) in 'A' .. 'Z'
687 loop
688 E_Name.Length := E_Name.Length - 1;
689 end loop;
690
691 -- Adjust casing appropriately (gets name from source if possible)
692
693 Adjust_Name_Case (E_Name, Sloc (E));
694 Append (Temp, E_Name);
695 end;
696 end Inner;
697
698 -- Start of processing for Append_Entity_Name
699
700 begin
701 Inner (E);
702 Append (Buf, Temp);
703 end Append_Entity_Name;
704
705 ---------------------------------
706 -- Append_Inherited_Subprogram --
707 ---------------------------------
708
709 procedure Append_Inherited_Subprogram (S : Entity_Id) is
710 Par : constant Entity_Id := Alias (S);
711 -- The parent subprogram
712
713 Scop : constant Entity_Id := Scope (Par);
714 -- The scope of definition of the parent subprogram
715
716 Typ : constant Entity_Id := Defining_Entity (Parent (S));
717 -- The derived type of which S is a primitive operation
718
719 Decl : Node_Id;
720 Next_E : Entity_Id;
721
722 begin
723 if Ekind (Current_Scope) = E_Package
724 and then In_Private_Part (Current_Scope)
725 and then Has_Private_Declaration (Typ)
726 and then Is_Tagged_Type (Typ)
727 and then Scop = Current_Scope
728 then
729 -- The inherited operation is available at the earliest place after
730 -- the derived type declaration (RM 7.3.1 (6/1)). This is only
731 -- relevant for type extensions. If the parent operation appears
732 -- after the type extension, the operation is not visible.
733
734 Decl := First
735 (Visible_Declarations
736 (Package_Specification (Current_Scope)));
737 while Present (Decl) loop
738 if Nkind (Decl) = N_Private_Extension_Declaration
739 and then Defining_Entity (Decl) = Typ
740 then
741 if Sloc (Decl) > Sloc (Par) then
742 Next_E := Next_Entity (Par);
743 Link_Entities (Par, S);
744 Link_Entities (S, Next_E);
745 return;
746
747 else
748 exit;
749 end if;
750 end if;
751
752 Next (Decl);
753 end loop;
754 end if;
755
756 -- If partial view is not a type extension, or it appears before the
757 -- subprogram declaration, insert normally at end of entity list.
758
759 Append_Entity (S, Current_Scope);
760 end Append_Inherited_Subprogram;
761
762 -----------------------------------------
763 -- Apply_Compile_Time_Constraint_Error --
764 -----------------------------------------
765
766 procedure Apply_Compile_Time_Constraint_Error
767 (N : Node_Id;
768 Msg : String;
769 Reason : RT_Exception_Code;
770 Ent : Entity_Id := Empty;
771 Typ : Entity_Id := Empty;
772 Loc : Source_Ptr := No_Location;
773 Rep : Boolean := True;
774 Warn : Boolean := False)
775 is
776 Stat : constant Boolean := Is_Static_Expression (N);
777 R_Stat : constant Node_Id :=
778 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
779 Rtyp : Entity_Id;
780
781 begin
782 if No (Typ) then
783 Rtyp := Etype (N);
784 else
785 Rtyp := Typ;
786 end if;
787
788 Discard_Node
789 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
790
791 -- In GNATprove mode, do not replace the node with an exception raised.
792 -- In such a case, either the call to Compile_Time_Constraint_Error
793 -- issues an error which stops analysis, or it issues a warning in
794 -- a few cases where a suitable check flag is set for GNATprove to
795 -- generate a check message.
796
797 if not Rep or GNATprove_Mode then
798 return;
799 end if;
800
801 -- Now we replace the node by an N_Raise_Constraint_Error node
802 -- This does not need reanalyzing, so set it as analyzed now.
803
804 Rewrite (N, R_Stat);
805 Set_Analyzed (N, True);
806
807 Set_Etype (N, Rtyp);
808 Set_Raises_Constraint_Error (N);
809
810 -- Now deal with possible local raise handling
811
812 Possible_Local_Raise (N, Standard_Constraint_Error);
813
814 -- If the original expression was marked as static, the result is
815 -- still marked as static, but the Raises_Constraint_Error flag is
816 -- always set so that further static evaluation is not attempted.
817
818 if Stat then
819 Set_Is_Static_Expression (N);
820 end if;
821 end Apply_Compile_Time_Constraint_Error;
822
823 ---------------------------
824 -- Async_Readers_Enabled --
825 ---------------------------
826
827 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
828 begin
829 return Has_Enabled_Property (Id, Name_Async_Readers);
830 end Async_Readers_Enabled;
831
832 ---------------------------
833 -- Async_Writers_Enabled --
834 ---------------------------
835
836 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
837 begin
838 return Has_Enabled_Property (Id, Name_Async_Writers);
839 end Async_Writers_Enabled;
840
841 --------------------------------------
842 -- Available_Full_View_Of_Component --
843 --------------------------------------
844
845 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
846 ST : constant Entity_Id := Scope (T);
847 SCT : constant Entity_Id := Scope (Component_Type (T));
848 begin
849 return In_Open_Scopes (ST)
850 and then In_Open_Scopes (SCT)
851 and then Scope_Depth (ST) >= Scope_Depth (SCT);
852 end Available_Full_View_Of_Component;
853
854 -------------------
855 -- Bad_Attribute --
856 -------------------
857
858 procedure Bad_Attribute
859 (N : Node_Id;
860 Nam : Name_Id;
861 Warn : Boolean := False)
862 is
863 begin
864 Error_Msg_Warn := Warn;
865 Error_Msg_N ("unrecognized attribute&<<", N);
866
867 -- Check for possible misspelling
868
869 Error_Msg_Name_1 := First_Attribute_Name;
870 while Error_Msg_Name_1 <= Last_Attribute_Name loop
871 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
872 Error_Msg_N -- CODEFIX
873 ("\possible misspelling of %<<", N);
874 exit;
875 end if;
876
877 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
878 end loop;
879 end Bad_Attribute;
880
881 --------------------------------
882 -- Bad_Predicated_Subtype_Use --
883 --------------------------------
884
885 procedure Bad_Predicated_Subtype_Use
886 (Msg : String;
887 N : Node_Id;
888 Typ : Entity_Id;
889 Suggest_Static : Boolean := False)
890 is
891 Gen : Entity_Id;
892
893 begin
894 -- Avoid cascaded errors
895
896 if Error_Posted (N) then
897 return;
898 end if;
899
900 if Inside_A_Generic then
901 Gen := Current_Scope;
902 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
903 Gen := Scope (Gen);
904 end loop;
905
906 if No (Gen) then
907 return;
908 end if;
909
910 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
911 Set_No_Predicate_On_Actual (Typ);
912 end if;
913
914 elsif Has_Predicates (Typ) then
915 if Is_Generic_Actual_Type (Typ) then
916
917 -- The restriction on loop parameters is only that the type
918 -- should have no dynamic predicates.
919
920 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
921 and then not Has_Dynamic_Predicate_Aspect (Typ)
922 and then Is_OK_Static_Subtype (Typ)
923 then
924 return;
925 end if;
926
927 Gen := Current_Scope;
928 while not Is_Generic_Instance (Gen) loop
929 Gen := Scope (Gen);
930 end loop;
931
932 pragma Assert (Present (Gen));
933
934 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
935 Error_Msg_Warn := SPARK_Mode /= On;
936 Error_Msg_FE (Msg & "<<", N, Typ);
937 Error_Msg_F ("\Program_Error [<<", N);
938
939 Insert_Action (N,
940 Make_Raise_Program_Error (Sloc (N),
941 Reason => PE_Bad_Predicated_Generic_Type));
942
943 else
944 Error_Msg_FE (Msg & "<<", N, Typ);
945 end if;
946
947 else
948 Error_Msg_FE (Msg, N, Typ);
949 end if;
950
951 -- Emit an optional suggestion on how to remedy the error if the
952 -- context warrants it.
953
954 if Suggest_Static and then Has_Static_Predicate (Typ) then
955 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
956 end if;
957 end if;
958 end Bad_Predicated_Subtype_Use;
959
960 -----------------------------------------
961 -- Bad_Unordered_Enumeration_Reference --
962 -----------------------------------------
963
964 function Bad_Unordered_Enumeration_Reference
965 (N : Node_Id;
966 T : Entity_Id) return Boolean
967 is
968 begin
969 return Is_Enumeration_Type (T)
970 and then Warn_On_Unordered_Enumeration_Type
971 and then not Is_Generic_Type (T)
972 and then Comes_From_Source (N)
973 and then not Has_Pragma_Ordered (T)
974 and then not In_Same_Extended_Unit (N, T);
975 end Bad_Unordered_Enumeration_Reference;
976
977 ----------------------------
978 -- Begin_Keyword_Location --
979 ----------------------------
980
981 function Begin_Keyword_Location (N : Node_Id) return Source_Ptr is
982 HSS : Node_Id;
983
984 begin
985 pragma Assert (Nkind_In (N, N_Block_Statement,
986 N_Entry_Body,
987 N_Package_Body,
988 N_Subprogram_Body,
989 N_Task_Body));
990
991 HSS := Handled_Statement_Sequence (N);
992
993 -- When the handled sequence of statements comes from source, the
994 -- location of the "begin" keyword is that of the sequence itself.
995 -- Note that an internal construct may inherit a source sequence.
996
997 if Comes_From_Source (HSS) then
998 return Sloc (HSS);
999
1000 -- The parser generates an internal handled sequence of statements to
1001 -- capture the location of the "begin" keyword if present in the source.
1002 -- Since there are no source statements, the location of the "begin"
1003 -- keyword is effectively that of the "end" keyword.
1004
1005 elsif Comes_From_Source (N) then
1006 return Sloc (HSS);
1007
1008 -- Otherwise the construct is internal and should carry the location of
1009 -- the original construct which prompted its creation.
1010
1011 else
1012 return Sloc (N);
1013 end if;
1014 end Begin_Keyword_Location;
1015
1016 --------------------------
1017 -- Build_Actual_Subtype --
1018 --------------------------
1019
1020 function Build_Actual_Subtype
1021 (T : Entity_Id;
1022 N : Node_Or_Entity_Id) return Node_Id
1023 is
1024 Loc : Source_Ptr;
1025 -- Normally Sloc (N), but may point to corresponding body in some cases
1026
1027 Constraints : List_Id;
1028 Decl : Node_Id;
1029 Discr : Entity_Id;
1030 Hi : Node_Id;
1031 Lo : Node_Id;
1032 Subt : Entity_Id;
1033 Disc_Type : Entity_Id;
1034 Obj : Node_Id;
1035
1036 begin
1037 Loc := Sloc (N);
1038
1039 if Nkind (N) = N_Defining_Identifier then
1040 Obj := New_Occurrence_Of (N, Loc);
1041
1042 -- If this is a formal parameter of a subprogram declaration, and
1043 -- we are compiling the body, we want the declaration for the
1044 -- actual subtype to carry the source position of the body, to
1045 -- prevent anomalies in gdb when stepping through the code.
1046
1047 if Is_Formal (N) then
1048 declare
1049 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
1050 begin
1051 if Nkind (Decl) = N_Subprogram_Declaration
1052 and then Present (Corresponding_Body (Decl))
1053 then
1054 Loc := Sloc (Corresponding_Body (Decl));
1055 end if;
1056 end;
1057 end if;
1058
1059 else
1060 Obj := N;
1061 end if;
1062
1063 if Is_Array_Type (T) then
1064 Constraints := New_List;
1065 for J in 1 .. Number_Dimensions (T) loop
1066
1067 -- Build an array subtype declaration with the nominal subtype and
1068 -- the bounds of the actual. Add the declaration in front of the
1069 -- local declarations for the subprogram, for analysis before any
1070 -- reference to the formal in the body.
1071
1072 Lo :=
1073 Make_Attribute_Reference (Loc,
1074 Prefix =>
1075 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
1076 Attribute_Name => Name_First,
1077 Expressions => New_List (
1078 Make_Integer_Literal (Loc, J)));
1079
1080 Hi :=
1081 Make_Attribute_Reference (Loc,
1082 Prefix =>
1083 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
1084 Attribute_Name => Name_Last,
1085 Expressions => New_List (
1086 Make_Integer_Literal (Loc, J)));
1087
1088 Append (Make_Range (Loc, Lo, Hi), Constraints);
1089 end loop;
1090
1091 -- If the type has unknown discriminants there is no constrained
1092 -- subtype to build. This is never called for a formal or for a
1093 -- lhs, so returning the type is ok ???
1094
1095 elsif Has_Unknown_Discriminants (T) then
1096 return T;
1097
1098 else
1099 Constraints := New_List;
1100
1101 -- Type T is a generic derived type, inherit the discriminants from
1102 -- the parent type.
1103
1104 if Is_Private_Type (T)
1105 and then No (Full_View (T))
1106
1107 -- T was flagged as an error if it was declared as a formal
1108 -- derived type with known discriminants. In this case there
1109 -- is no need to look at the parent type since T already carries
1110 -- its own discriminants.
1111
1112 and then not Error_Posted (T)
1113 then
1114 Disc_Type := Etype (Base_Type (T));
1115 else
1116 Disc_Type := T;
1117 end if;
1118
1119 Discr := First_Discriminant (Disc_Type);
1120 while Present (Discr) loop
1121 Append_To (Constraints,
1122 Make_Selected_Component (Loc,
1123 Prefix =>
1124 Duplicate_Subexpr_No_Checks (Obj),
1125 Selector_Name => New_Occurrence_Of (Discr, Loc)));
1126 Next_Discriminant (Discr);
1127 end loop;
1128 end if;
1129
1130 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
1131 Set_Is_Internal (Subt);
1132
1133 Decl :=
1134 Make_Subtype_Declaration (Loc,
1135 Defining_Identifier => Subt,
1136 Subtype_Indication =>
1137 Make_Subtype_Indication (Loc,
1138 Subtype_Mark => New_Occurrence_Of (T, Loc),
1139 Constraint =>
1140 Make_Index_Or_Discriminant_Constraint (Loc,
1141 Constraints => Constraints)));
1142
1143 Mark_Rewrite_Insertion (Decl);
1144 return Decl;
1145 end Build_Actual_Subtype;
1146
1147 ---------------------------------------
1148 -- Build_Actual_Subtype_Of_Component --
1149 ---------------------------------------
1150
1151 function Build_Actual_Subtype_Of_Component
1152 (T : Entity_Id;
1153 N : Node_Id) return Node_Id
1154 is
1155 Loc : constant Source_Ptr := Sloc (N);
1156 P : constant Node_Id := Prefix (N);
1157 D : Elmt_Id;
1158 Id : Node_Id;
1159 Index_Typ : Entity_Id;
1160
1161 Desig_Typ : Entity_Id;
1162 -- This is either a copy of T, or if T is an access type, then it is
1163 -- the directly designated type of this access type.
1164
1165 function Build_Actual_Array_Constraint return List_Id;
1166 -- If one or more of the bounds of the component depends on
1167 -- discriminants, build actual constraint using the discriminants
1168 -- of the prefix.
1169
1170 function Build_Actual_Record_Constraint return List_Id;
1171 -- Similar to previous one, for discriminated components constrained
1172 -- by the discriminant of the enclosing object.
1173
1174 -----------------------------------
1175 -- Build_Actual_Array_Constraint --
1176 -----------------------------------
1177
1178 function Build_Actual_Array_Constraint return List_Id is
1179 Constraints : constant List_Id := New_List;
1180 Indx : Node_Id;
1181 Hi : Node_Id;
1182 Lo : Node_Id;
1183 Old_Hi : Node_Id;
1184 Old_Lo : Node_Id;
1185
1186 begin
1187 Indx := First_Index (Desig_Typ);
1188 while Present (Indx) loop
1189 Old_Lo := Type_Low_Bound (Etype (Indx));
1190 Old_Hi := Type_High_Bound (Etype (Indx));
1191
1192 if Denotes_Discriminant (Old_Lo) then
1193 Lo :=
1194 Make_Selected_Component (Loc,
1195 Prefix => New_Copy_Tree (P),
1196 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1197
1198 else
1199 Lo := New_Copy_Tree (Old_Lo);
1200
1201 -- The new bound will be reanalyzed in the enclosing
1202 -- declaration. For literal bounds that come from a type
1203 -- declaration, the type of the context must be imposed, so
1204 -- insure that analysis will take place. For non-universal
1205 -- types this is not strictly necessary.
1206
1207 Set_Analyzed (Lo, False);
1208 end if;
1209
1210 if Denotes_Discriminant (Old_Hi) then
1211 Hi :=
1212 Make_Selected_Component (Loc,
1213 Prefix => New_Copy_Tree (P),
1214 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1215
1216 else
1217 Hi := New_Copy_Tree (Old_Hi);
1218 Set_Analyzed (Hi, False);
1219 end if;
1220
1221 Append (Make_Range (Loc, Lo, Hi), Constraints);
1222 Next_Index (Indx);
1223 end loop;
1224
1225 return Constraints;
1226 end Build_Actual_Array_Constraint;
1227
1228 ------------------------------------
1229 -- Build_Actual_Record_Constraint --
1230 ------------------------------------
1231
1232 function Build_Actual_Record_Constraint return List_Id is
1233 Constraints : constant List_Id := New_List;
1234 D : Elmt_Id;
1235 D_Val : Node_Id;
1236
1237 begin
1238 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1239 while Present (D) loop
1240 if Denotes_Discriminant (Node (D)) then
1241 D_Val := Make_Selected_Component (Loc,
1242 Prefix => New_Copy_Tree (P),
1243 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1244
1245 else
1246 D_Val := New_Copy_Tree (Node (D));
1247 end if;
1248
1249 Append (D_Val, Constraints);
1250 Next_Elmt (D);
1251 end loop;
1252
1253 return Constraints;
1254 end Build_Actual_Record_Constraint;
1255
1256 -- Start of processing for Build_Actual_Subtype_Of_Component
1257
1258 begin
1259 -- Why the test for Spec_Expression mode here???
1260
1261 if In_Spec_Expression then
1262 return Empty;
1263
1264 -- More comments for the rest of this body would be good ???
1265
1266 elsif Nkind (N) = N_Explicit_Dereference then
1267 if Is_Composite_Type (T)
1268 and then not Is_Constrained (T)
1269 and then not (Is_Class_Wide_Type (T)
1270 and then Is_Constrained (Root_Type (T)))
1271 and then not Has_Unknown_Discriminants (T)
1272 then
1273 -- If the type of the dereference is already constrained, it is an
1274 -- actual subtype.
1275
1276 if Is_Array_Type (Etype (N))
1277 and then Is_Constrained (Etype (N))
1278 then
1279 return Empty;
1280 else
1281 Remove_Side_Effects (P);
1282 return Build_Actual_Subtype (T, N);
1283 end if;
1284 else
1285 return Empty;
1286 end if;
1287 end if;
1288
1289 if Ekind (T) = E_Access_Subtype then
1290 Desig_Typ := Designated_Type (T);
1291 else
1292 Desig_Typ := T;
1293 end if;
1294
1295 if Ekind (Desig_Typ) = E_Array_Subtype then
1296 Id := First_Index (Desig_Typ);
1297 while Present (Id) loop
1298 Index_Typ := Underlying_Type (Etype (Id));
1299
1300 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1301 or else
1302 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1303 then
1304 Remove_Side_Effects (P);
1305 return
1306 Build_Component_Subtype
1307 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1308 end if;
1309
1310 Next_Index (Id);
1311 end loop;
1312
1313 elsif Is_Composite_Type (Desig_Typ)
1314 and then Has_Discriminants (Desig_Typ)
1315 and then not Has_Unknown_Discriminants (Desig_Typ)
1316 then
1317 if Is_Private_Type (Desig_Typ)
1318 and then No (Discriminant_Constraint (Desig_Typ))
1319 then
1320 Desig_Typ := Full_View (Desig_Typ);
1321 end if;
1322
1323 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1324 while Present (D) loop
1325 if Denotes_Discriminant (Node (D)) then
1326 Remove_Side_Effects (P);
1327 return
1328 Build_Component_Subtype (
1329 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1330 end if;
1331
1332 Next_Elmt (D);
1333 end loop;
1334 end if;
1335
1336 -- If none of the above, the actual and nominal subtypes are the same
1337
1338 return Empty;
1339 end Build_Actual_Subtype_Of_Component;
1340
1341 ---------------------------------
1342 -- Build_Class_Wide_Clone_Body --
1343 ---------------------------------
1344
1345 procedure Build_Class_Wide_Clone_Body
1346 (Spec_Id : Entity_Id;
1347 Bod : Node_Id)
1348 is
1349 Loc : constant Source_Ptr := Sloc (Bod);
1350 Clone_Id : constant Entity_Id := Class_Wide_Clone (Spec_Id);
1351 Clone_Body : Node_Id;
1352
1353 begin
1354 -- The declaration of the class-wide clone was created when the
1355 -- corresponding class-wide condition was analyzed.
1356
1357 Clone_Body :=
1358 Make_Subprogram_Body (Loc,
1359 Specification =>
1360 Copy_Subprogram_Spec (Parent (Clone_Id)),
1361 Declarations => Declarations (Bod),
1362 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
1363
1364 -- The new operation is internal and overriding indicators do not apply
1365 -- (the original primitive may have carried one).
1366
1367 Set_Must_Override (Specification (Clone_Body), False);
1368
1369 -- If the subprogram body is the proper body of a stub, insert the
1370 -- subprogram after the stub, i.e. the same declarative region as
1371 -- the original sugprogram.
1372
1373 if Nkind (Parent (Bod)) = N_Subunit then
1374 Insert_After (Corresponding_Stub (Parent (Bod)), Clone_Body);
1375
1376 else
1377 Insert_Before (Bod, Clone_Body);
1378 end if;
1379
1380 Analyze (Clone_Body);
1381 end Build_Class_Wide_Clone_Body;
1382
1383 ---------------------------------
1384 -- Build_Class_Wide_Clone_Call --
1385 ---------------------------------
1386
1387 function Build_Class_Wide_Clone_Call
1388 (Loc : Source_Ptr;
1389 Decls : List_Id;
1390 Spec_Id : Entity_Id;
1391 Spec : Node_Id) return Node_Id
1392 is
1393 Clone_Id : constant Entity_Id := Class_Wide_Clone (Spec_Id);
1394 Par_Type : constant Entity_Id := Find_Dispatching_Type (Spec_Id);
1395
1396 Actuals : List_Id;
1397 Call : Node_Id;
1398 Formal : Entity_Id;
1399 New_Body : Node_Id;
1400 New_F_Spec : Entity_Id;
1401 New_Formal : Entity_Id;
1402
1403 begin
1404 Actuals := Empty_List;
1405 Formal := First_Formal (Spec_Id);
1406 New_F_Spec := First (Parameter_Specifications (Spec));
1407
1408 -- Build parameter association for call to class-wide clone.
1409
1410 while Present (Formal) loop
1411 New_Formal := Defining_Identifier (New_F_Spec);
1412
1413 -- If controlling argument and operation is inherited, add conversion
1414 -- to parent type for the call.
1415
1416 if Etype (Formal) = Par_Type
1417 and then not Is_Empty_List (Decls)
1418 then
1419 Append_To (Actuals,
1420 Make_Type_Conversion (Loc,
1421 New_Occurrence_Of (Par_Type, Loc),
1422 New_Occurrence_Of (New_Formal, Loc)));
1423
1424 else
1425 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1426 end if;
1427
1428 Next_Formal (Formal);
1429 Next (New_F_Spec);
1430 end loop;
1431
1432 if Ekind (Spec_Id) = E_Procedure then
1433 Call :=
1434 Make_Procedure_Call_Statement (Loc,
1435 Name => New_Occurrence_Of (Clone_Id, Loc),
1436 Parameter_Associations => Actuals);
1437 else
1438 Call :=
1439 Make_Simple_Return_Statement (Loc,
1440 Expression =>
1441 Make_Function_Call (Loc,
1442 Name => New_Occurrence_Of (Clone_Id, Loc),
1443 Parameter_Associations => Actuals));
1444 end if;
1445
1446 New_Body :=
1447 Make_Subprogram_Body (Loc,
1448 Specification =>
1449 Copy_Subprogram_Spec (Spec),
1450 Declarations => Decls,
1451 Handled_Statement_Sequence =>
1452 Make_Handled_Sequence_Of_Statements (Loc,
1453 Statements => New_List (Call),
1454 End_Label => Make_Identifier (Loc, Chars (Spec_Id))));
1455
1456 return New_Body;
1457 end Build_Class_Wide_Clone_Call;
1458
1459 ---------------------------------
1460 -- Build_Class_Wide_Clone_Decl --
1461 ---------------------------------
1462
1463 procedure Build_Class_Wide_Clone_Decl (Spec_Id : Entity_Id) is
1464 Loc : constant Source_Ptr := Sloc (Spec_Id);
1465 Clone_Id : constant Entity_Id :=
1466 Make_Defining_Identifier (Loc,
1467 New_External_Name (Chars (Spec_Id), Suffix => "CL"));
1468
1469 Decl : Node_Id;
1470 Spec : Node_Id;
1471
1472 begin
1473 Spec := Copy_Subprogram_Spec (Parent (Spec_Id));
1474 Set_Must_Override (Spec, False);
1475 Set_Must_Not_Override (Spec, False);
1476 Set_Defining_Unit_Name (Spec, Clone_Id);
1477
1478 Decl := Make_Subprogram_Declaration (Loc, Spec);
1479 Append (Decl, List_Containing (Unit_Declaration_Node (Spec_Id)));
1480
1481 -- Link clone to original subprogram, for use when building body and
1482 -- wrapper call to inherited operation.
1483
1484 Set_Class_Wide_Clone (Spec_Id, Clone_Id);
1485 end Build_Class_Wide_Clone_Decl;
1486
1487 -----------------------------
1488 -- Build_Component_Subtype --
1489 -----------------------------
1490
1491 function Build_Component_Subtype
1492 (C : List_Id;
1493 Loc : Source_Ptr;
1494 T : Entity_Id) return Node_Id
1495 is
1496 Subt : Entity_Id;
1497 Decl : Node_Id;
1498
1499 begin
1500 -- Unchecked_Union components do not require component subtypes
1501
1502 if Is_Unchecked_Union (T) then
1503 return Empty;
1504 end if;
1505
1506 Subt := Make_Temporary (Loc, 'S');
1507 Set_Is_Internal (Subt);
1508
1509 Decl :=
1510 Make_Subtype_Declaration (Loc,
1511 Defining_Identifier => Subt,
1512 Subtype_Indication =>
1513 Make_Subtype_Indication (Loc,
1514 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1515 Constraint =>
1516 Make_Index_Or_Discriminant_Constraint (Loc,
1517 Constraints => C)));
1518
1519 Mark_Rewrite_Insertion (Decl);
1520 return Decl;
1521 end Build_Component_Subtype;
1522
1523 ---------------------------
1524 -- Build_Default_Subtype --
1525 ---------------------------
1526
1527 function Build_Default_Subtype
1528 (T : Entity_Id;
1529 N : Node_Id) return Entity_Id
1530 is
1531 Loc : constant Source_Ptr := Sloc (N);
1532 Disc : Entity_Id;
1533
1534 Bas : Entity_Id;
1535 -- The base type that is to be constrained by the defaults
1536
1537 begin
1538 if not Has_Discriminants (T) or else Is_Constrained (T) then
1539 return T;
1540 end if;
1541
1542 Bas := Base_Type (T);
1543
1544 -- If T is non-private but its base type is private, this is the
1545 -- completion of a subtype declaration whose parent type is private
1546 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1547 -- are to be found in the full view of the base. Check that the private
1548 -- status of T and its base differ.
1549
1550 if Is_Private_Type (Bas)
1551 and then not Is_Private_Type (T)
1552 and then Present (Full_View (Bas))
1553 then
1554 Bas := Full_View (Bas);
1555 end if;
1556
1557 Disc := First_Discriminant (T);
1558
1559 if No (Discriminant_Default_Value (Disc)) then
1560 return T;
1561 end if;
1562
1563 declare
1564 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1565 Constraints : constant List_Id := New_List;
1566 Decl : Node_Id;
1567
1568 begin
1569 while Present (Disc) loop
1570 Append_To (Constraints,
1571 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1572 Next_Discriminant (Disc);
1573 end loop;
1574
1575 Decl :=
1576 Make_Subtype_Declaration (Loc,
1577 Defining_Identifier => Act,
1578 Subtype_Indication =>
1579 Make_Subtype_Indication (Loc,
1580 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1581 Constraint =>
1582 Make_Index_Or_Discriminant_Constraint (Loc,
1583 Constraints => Constraints)));
1584
1585 Insert_Action (N, Decl);
1586
1587 -- If the context is a component declaration the subtype declaration
1588 -- will be analyzed when the enclosing type is frozen, otherwise do
1589 -- it now.
1590
1591 if Ekind (Current_Scope) /= E_Record_Type then
1592 Analyze (Decl);
1593 end if;
1594
1595 return Act;
1596 end;
1597 end Build_Default_Subtype;
1598
1599 --------------------------------------------
1600 -- Build_Discriminal_Subtype_Of_Component --
1601 --------------------------------------------
1602
1603 function Build_Discriminal_Subtype_Of_Component
1604 (T : Entity_Id) return Node_Id
1605 is
1606 Loc : constant Source_Ptr := Sloc (T);
1607 D : Elmt_Id;
1608 Id : Node_Id;
1609
1610 function Build_Discriminal_Array_Constraint return List_Id;
1611 -- If one or more of the bounds of the component depends on
1612 -- discriminants, build actual constraint using the discriminants
1613 -- of the prefix.
1614
1615 function Build_Discriminal_Record_Constraint return List_Id;
1616 -- Similar to previous one, for discriminated components constrained by
1617 -- the discriminant of the enclosing object.
1618
1619 ----------------------------------------
1620 -- Build_Discriminal_Array_Constraint --
1621 ----------------------------------------
1622
1623 function Build_Discriminal_Array_Constraint return List_Id is
1624 Constraints : constant List_Id := New_List;
1625 Indx : Node_Id;
1626 Hi : Node_Id;
1627 Lo : Node_Id;
1628 Old_Hi : Node_Id;
1629 Old_Lo : Node_Id;
1630
1631 begin
1632 Indx := First_Index (T);
1633 while Present (Indx) loop
1634 Old_Lo := Type_Low_Bound (Etype (Indx));
1635 Old_Hi := Type_High_Bound (Etype (Indx));
1636
1637 if Denotes_Discriminant (Old_Lo) then
1638 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1639
1640 else
1641 Lo := New_Copy_Tree (Old_Lo);
1642 end if;
1643
1644 if Denotes_Discriminant (Old_Hi) then
1645 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1646
1647 else
1648 Hi := New_Copy_Tree (Old_Hi);
1649 end if;
1650
1651 Append (Make_Range (Loc, Lo, Hi), Constraints);
1652 Next_Index (Indx);
1653 end loop;
1654
1655 return Constraints;
1656 end Build_Discriminal_Array_Constraint;
1657
1658 -----------------------------------------
1659 -- Build_Discriminal_Record_Constraint --
1660 -----------------------------------------
1661
1662 function Build_Discriminal_Record_Constraint return List_Id is
1663 Constraints : constant List_Id := New_List;
1664 D : Elmt_Id;
1665 D_Val : Node_Id;
1666
1667 begin
1668 D := First_Elmt (Discriminant_Constraint (T));
1669 while Present (D) loop
1670 if Denotes_Discriminant (Node (D)) then
1671 D_Val :=
1672 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1673 else
1674 D_Val := New_Copy_Tree (Node (D));
1675 end if;
1676
1677 Append (D_Val, Constraints);
1678 Next_Elmt (D);
1679 end loop;
1680
1681 return Constraints;
1682 end Build_Discriminal_Record_Constraint;
1683
1684 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1685
1686 begin
1687 if Ekind (T) = E_Array_Subtype then
1688 Id := First_Index (T);
1689 while Present (Id) loop
1690 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1691 or else
1692 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1693 then
1694 return Build_Component_Subtype
1695 (Build_Discriminal_Array_Constraint, Loc, T);
1696 end if;
1697
1698 Next_Index (Id);
1699 end loop;
1700
1701 elsif Ekind (T) = E_Record_Subtype
1702 and then Has_Discriminants (T)
1703 and then not Has_Unknown_Discriminants (T)
1704 then
1705 D := First_Elmt (Discriminant_Constraint (T));
1706 while Present (D) loop
1707 if Denotes_Discriminant (Node (D)) then
1708 return Build_Component_Subtype
1709 (Build_Discriminal_Record_Constraint, Loc, T);
1710 end if;
1711
1712 Next_Elmt (D);
1713 end loop;
1714 end if;
1715
1716 -- If none of the above, the actual and nominal subtypes are the same
1717
1718 return Empty;
1719 end Build_Discriminal_Subtype_Of_Component;
1720
1721 ------------------------------
1722 -- Build_Elaboration_Entity --
1723 ------------------------------
1724
1725 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1726 Loc : constant Source_Ptr := Sloc (N);
1727 Decl : Node_Id;
1728 Elab_Ent : Entity_Id;
1729
1730 procedure Set_Package_Name (Ent : Entity_Id);
1731 -- Given an entity, sets the fully qualified name of the entity in
1732 -- Name_Buffer, with components separated by double underscores. This
1733 -- is a recursive routine that climbs the scope chain to Standard.
1734
1735 ----------------------
1736 -- Set_Package_Name --
1737 ----------------------
1738
1739 procedure Set_Package_Name (Ent : Entity_Id) is
1740 begin
1741 if Scope (Ent) /= Standard_Standard then
1742 Set_Package_Name (Scope (Ent));
1743
1744 declare
1745 Nam : constant String := Get_Name_String (Chars (Ent));
1746 begin
1747 Name_Buffer (Name_Len + 1) := '_';
1748 Name_Buffer (Name_Len + 2) := '_';
1749 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1750 Name_Len := Name_Len + Nam'Length + 2;
1751 end;
1752
1753 else
1754 Get_Name_String (Chars (Ent));
1755 end if;
1756 end Set_Package_Name;
1757
1758 -- Start of processing for Build_Elaboration_Entity
1759
1760 begin
1761 -- Ignore call if already constructed
1762
1763 if Present (Elaboration_Entity (Spec_Id)) then
1764 return;
1765
1766 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1767 -- no role in analysis.
1768
1769 elsif ASIS_Mode then
1770 return;
1771
1772 -- Do not generate an elaboration entity in GNATprove move because the
1773 -- elaboration counter is a form of expansion.
1774
1775 elsif GNATprove_Mode then
1776 return;
1777
1778 -- See if we need elaboration entity
1779
1780 -- We always need an elaboration entity when preserving control flow, as
1781 -- we want to remain explicit about the unit's elaboration order.
1782
1783 elsif Opt.Suppress_Control_Flow_Optimizations then
1784 null;
1785
1786 -- We always need an elaboration entity for the dynamic elaboration
1787 -- model, since it is needed to properly generate the PE exception for
1788 -- access before elaboration.
1789
1790 elsif Dynamic_Elaboration_Checks then
1791 null;
1792
1793 -- For the static model, we don't need the elaboration counter if this
1794 -- unit is sure to have no elaboration code, since that means there
1795 -- is no elaboration unit to be called. Note that we can't just decide
1796 -- after the fact by looking to see whether there was elaboration code,
1797 -- because that's too late to make this decision.
1798
1799 elsif Restriction_Active (No_Elaboration_Code) then
1800 return;
1801
1802 -- Similarly, for the static model, we can skip the elaboration counter
1803 -- if we have the No_Multiple_Elaboration restriction, since for the
1804 -- static model, that's the only purpose of the counter (to avoid
1805 -- multiple elaboration).
1806
1807 elsif Restriction_Active (No_Multiple_Elaboration) then
1808 return;
1809 end if;
1810
1811 -- Here we need the elaboration entity
1812
1813 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1814 -- name with dots replaced by double underscore. We have to manually
1815 -- construct this name, since it will be elaborated in the outer scope,
1816 -- and thus will not have the unit name automatically prepended.
1817
1818 Set_Package_Name (Spec_Id);
1819 Add_Str_To_Name_Buffer ("_E");
1820
1821 -- Create elaboration counter
1822
1823 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1824 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1825
1826 Decl :=
1827 Make_Object_Declaration (Loc,
1828 Defining_Identifier => Elab_Ent,
1829 Object_Definition =>
1830 New_Occurrence_Of (Standard_Short_Integer, Loc),
1831 Expression => Make_Integer_Literal (Loc, Uint_0));
1832
1833 Push_Scope (Standard_Standard);
1834 Add_Global_Declaration (Decl);
1835 Pop_Scope;
1836
1837 -- Reset True_Constant indication, since we will indeed assign a value
1838 -- to the variable in the binder main. We also kill the Current_Value
1839 -- and Last_Assignment fields for the same reason.
1840
1841 Set_Is_True_Constant (Elab_Ent, False);
1842 Set_Current_Value (Elab_Ent, Empty);
1843 Set_Last_Assignment (Elab_Ent, Empty);
1844
1845 -- We do not want any further qualification of the name (if we did not
1846 -- do this, we would pick up the name of the generic package in the case
1847 -- of a library level generic instantiation).
1848
1849 Set_Has_Qualified_Name (Elab_Ent);
1850 Set_Has_Fully_Qualified_Name (Elab_Ent);
1851 end Build_Elaboration_Entity;
1852
1853 --------------------------------
1854 -- Build_Explicit_Dereference --
1855 --------------------------------
1856
1857 procedure Build_Explicit_Dereference
1858 (Expr : Node_Id;
1859 Disc : Entity_Id)
1860 is
1861 Loc : constant Source_Ptr := Sloc (Expr);
1862 I : Interp_Index;
1863 It : Interp;
1864
1865 begin
1866 -- An entity of a type with a reference aspect is overloaded with
1867 -- both interpretations: with and without the dereference. Now that
1868 -- the dereference is made explicit, set the type of the node properly,
1869 -- to prevent anomalies in the backend. Same if the expression is an
1870 -- overloaded function call whose return type has a reference aspect.
1871
1872 if Is_Entity_Name (Expr) then
1873 Set_Etype (Expr, Etype (Entity (Expr)));
1874
1875 -- The designated entity will not be examined again when resolving
1876 -- the dereference, so generate a reference to it now.
1877
1878 Generate_Reference (Entity (Expr), Expr);
1879
1880 elsif Nkind (Expr) = N_Function_Call then
1881
1882 -- If the name of the indexing function is overloaded, locate the one
1883 -- whose return type has an implicit dereference on the desired
1884 -- discriminant, and set entity and type of function call.
1885
1886 if Is_Overloaded (Name (Expr)) then
1887 Get_First_Interp (Name (Expr), I, It);
1888
1889 while Present (It.Nam) loop
1890 if Ekind ((It.Typ)) = E_Record_Type
1891 and then First_Entity ((It.Typ)) = Disc
1892 then
1893 Set_Entity (Name (Expr), It.Nam);
1894 Set_Etype (Name (Expr), Etype (It.Nam));
1895 exit;
1896 end if;
1897
1898 Get_Next_Interp (I, It);
1899 end loop;
1900 end if;
1901
1902 -- Set type of call from resolved function name.
1903
1904 Set_Etype (Expr, Etype (Name (Expr)));
1905 end if;
1906
1907 Set_Is_Overloaded (Expr, False);
1908
1909 -- The expression will often be a generalized indexing that yields a
1910 -- container element that is then dereferenced, in which case the
1911 -- generalized indexing call is also non-overloaded.
1912
1913 if Nkind (Expr) = N_Indexed_Component
1914 and then Present (Generalized_Indexing (Expr))
1915 then
1916 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1917 end if;
1918
1919 Rewrite (Expr,
1920 Make_Explicit_Dereference (Loc,
1921 Prefix =>
1922 Make_Selected_Component (Loc,
1923 Prefix => Relocate_Node (Expr),
1924 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1925 Set_Etype (Prefix (Expr), Etype (Disc));
1926 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1927 end Build_Explicit_Dereference;
1928
1929 ---------------------------
1930 -- Build_Overriding_Spec --
1931 ---------------------------
1932
1933 function Build_Overriding_Spec
1934 (Op : Entity_Id;
1935 Typ : Entity_Id) return Node_Id
1936 is
1937 Loc : constant Source_Ptr := Sloc (Typ);
1938 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Op);
1939 Spec : constant Node_Id := Specification (Unit_Declaration_Node (Op));
1940
1941 Formal_Spec : Node_Id;
1942 Formal_Type : Node_Id;
1943 New_Spec : Node_Id;
1944
1945 begin
1946 New_Spec := Copy_Subprogram_Spec (Spec);
1947
1948 Formal_Spec := First (Parameter_Specifications (New_Spec));
1949 while Present (Formal_Spec) loop
1950 Formal_Type := Parameter_Type (Formal_Spec);
1951
1952 if Is_Entity_Name (Formal_Type)
1953 and then Entity (Formal_Type) = Par_Typ
1954 then
1955 Rewrite (Formal_Type, New_Occurrence_Of (Typ, Loc));
1956 end if;
1957
1958 -- Nothing needs to be done for access parameters
1959
1960 Next (Formal_Spec);
1961 end loop;
1962
1963 return New_Spec;
1964 end Build_Overriding_Spec;
1965
1966 -----------------------------------
1967 -- Cannot_Raise_Constraint_Error --
1968 -----------------------------------
1969
1970 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1971 begin
1972 if Compile_Time_Known_Value (Expr) then
1973 return True;
1974
1975 elsif Do_Range_Check (Expr) then
1976 return False;
1977
1978 elsif Raises_Constraint_Error (Expr) then
1979 return False;
1980
1981 else
1982 case Nkind (Expr) is
1983 when N_Identifier =>
1984 return True;
1985
1986 when N_Expanded_Name =>
1987 return True;
1988
1989 when N_Selected_Component =>
1990 return not Do_Discriminant_Check (Expr);
1991
1992 when N_Attribute_Reference =>
1993 if Do_Overflow_Check (Expr) then
1994 return False;
1995
1996 elsif No (Expressions (Expr)) then
1997 return True;
1998
1999 else
2000 declare
2001 N : Node_Id;
2002
2003 begin
2004 N := First (Expressions (Expr));
2005 while Present (N) loop
2006 if Cannot_Raise_Constraint_Error (N) then
2007 Next (N);
2008 else
2009 return False;
2010 end if;
2011 end loop;
2012
2013 return True;
2014 end;
2015 end if;
2016
2017 when N_Type_Conversion =>
2018 if Do_Overflow_Check (Expr)
2019 or else Do_Length_Check (Expr)
2020 or else Do_Tag_Check (Expr)
2021 then
2022 return False;
2023 else
2024 return Cannot_Raise_Constraint_Error (Expression (Expr));
2025 end if;
2026
2027 when N_Unchecked_Type_Conversion =>
2028 return Cannot_Raise_Constraint_Error (Expression (Expr));
2029
2030 when N_Unary_Op =>
2031 if Do_Overflow_Check (Expr) then
2032 return False;
2033 else
2034 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2035 end if;
2036
2037 when N_Op_Divide
2038 | N_Op_Mod
2039 | N_Op_Rem
2040 =>
2041 if Do_Division_Check (Expr)
2042 or else
2043 Do_Overflow_Check (Expr)
2044 then
2045 return False;
2046 else
2047 return
2048 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
2049 and then
2050 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2051 end if;
2052
2053 when N_Op_Add
2054 | N_Op_And
2055 | N_Op_Concat
2056 | N_Op_Eq
2057 | N_Op_Expon
2058 | N_Op_Ge
2059 | N_Op_Gt
2060 | N_Op_Le
2061 | N_Op_Lt
2062 | N_Op_Multiply
2063 | N_Op_Ne
2064 | N_Op_Or
2065 | N_Op_Rotate_Left
2066 | N_Op_Rotate_Right
2067 | N_Op_Shift_Left
2068 | N_Op_Shift_Right
2069 | N_Op_Shift_Right_Arithmetic
2070 | N_Op_Subtract
2071 | N_Op_Xor
2072 =>
2073 if Do_Overflow_Check (Expr) then
2074 return False;
2075 else
2076 return
2077 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
2078 and then
2079 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2080 end if;
2081
2082 when others =>
2083 return False;
2084 end case;
2085 end if;
2086 end Cannot_Raise_Constraint_Error;
2087
2088 -----------------------------------------
2089 -- Check_Dynamically_Tagged_Expression --
2090 -----------------------------------------
2091
2092 procedure Check_Dynamically_Tagged_Expression
2093 (Expr : Node_Id;
2094 Typ : Entity_Id;
2095 Related_Nod : Node_Id)
2096 is
2097 begin
2098 pragma Assert (Is_Tagged_Type (Typ));
2099
2100 -- In order to avoid spurious errors when analyzing the expanded code,
2101 -- this check is done only for nodes that come from source and for
2102 -- actuals of generic instantiations.
2103
2104 if (Comes_From_Source (Related_Nod)
2105 or else In_Generic_Actual (Expr))
2106 and then (Is_Class_Wide_Type (Etype (Expr))
2107 or else Is_Dynamically_Tagged (Expr))
2108 and then not Is_Class_Wide_Type (Typ)
2109 then
2110 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
2111 end if;
2112 end Check_Dynamically_Tagged_Expression;
2113
2114 --------------------------
2115 -- Check_Fully_Declared --
2116 --------------------------
2117
2118 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2119 begin
2120 if Ekind (T) = E_Incomplete_Type then
2121
2122 -- Ada 2005 (AI-50217): If the type is available through a limited
2123 -- with_clause, verify that its full view has been analyzed.
2124
2125 if From_Limited_With (T)
2126 and then Present (Non_Limited_View (T))
2127 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2128 then
2129 -- The non-limited view is fully declared
2130
2131 null;
2132
2133 else
2134 Error_Msg_NE
2135 ("premature usage of incomplete}", N, First_Subtype (T));
2136 end if;
2137
2138 -- Need comments for these tests ???
2139
2140 elsif Has_Private_Component (T)
2141 and then not Is_Generic_Type (Root_Type (T))
2142 and then not In_Spec_Expression
2143 then
2144 -- Special case: if T is the anonymous type created for a single
2145 -- task or protected object, use the name of the source object.
2146
2147 if Is_Concurrent_Type (T)
2148 and then not Comes_From_Source (T)
2149 and then Nkind (N) = N_Object_Declaration
2150 then
2151 Error_Msg_NE
2152 ("type of& has incomplete component",
2153 N, Defining_Identifier (N));
2154 else
2155 Error_Msg_NE
2156 ("premature usage of incomplete}",
2157 N, First_Subtype (T));
2158 end if;
2159 end if;
2160 end Check_Fully_Declared;
2161
2162 -------------------------------------------
2163 -- Check_Function_With_Address_Parameter --
2164 -------------------------------------------
2165
2166 procedure Check_Function_With_Address_Parameter (Subp_Id : Entity_Id) is
2167 F : Entity_Id;
2168 T : Entity_Id;
2169
2170 begin
2171 F := First_Formal (Subp_Id);
2172 while Present (F) loop
2173 T := Etype (F);
2174
2175 if Is_Private_Type (T) and then Present (Full_View (T)) then
2176 T := Full_View (T);
2177 end if;
2178
2179 if Is_Descendant_Of_Address (T) or else Is_Limited_Type (T) then
2180 Set_Is_Pure (Subp_Id, False);
2181 exit;
2182 end if;
2183
2184 Next_Formal (F);
2185 end loop;
2186 end Check_Function_With_Address_Parameter;
2187
2188 -------------------------------------
2189 -- Check_Function_Writable_Actuals --
2190 -------------------------------------
2191
2192 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2193 Writable_Actuals_List : Elist_Id := No_Elist;
2194 Identifiers_List : Elist_Id := No_Elist;
2195 Aggr_Error_Node : Node_Id := Empty;
2196 Error_Node : Node_Id := Empty;
2197
2198 procedure Collect_Identifiers (N : Node_Id);
2199 -- In a single traversal of subtree N collect in Writable_Actuals_List
2200 -- all the actuals of functions with writable actuals, and in the list
2201 -- Identifiers_List collect all the identifiers that are not actuals of
2202 -- functions with writable actuals. If a writable actual is referenced
2203 -- twice as writable actual then Error_Node is set to reference its
2204 -- second occurrence, the error is reported, and the tree traversal
2205 -- is abandoned.
2206
2207 procedure Preanalyze_Without_Errors (N : Node_Id);
2208 -- Preanalyze N without reporting errors. Very dubious, you can't just
2209 -- go analyzing things more than once???
2210
2211 -------------------------
2212 -- Collect_Identifiers --
2213 -------------------------
2214
2215 procedure Collect_Identifiers (N : Node_Id) is
2216
2217 function Check_Node (N : Node_Id) return Traverse_Result;
2218 -- Process a single node during the tree traversal to collect the
2219 -- writable actuals of functions and all the identifiers which are
2220 -- not writable actuals of functions.
2221
2222 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2223 -- Returns True if List has a node whose Entity is Entity (N)
2224
2225 ----------------
2226 -- Check_Node --
2227 ----------------
2228
2229 function Check_Node (N : Node_Id) return Traverse_Result is
2230 Is_Writable_Actual : Boolean := False;
2231 Id : Entity_Id;
2232
2233 begin
2234 if Nkind (N) = N_Identifier then
2235
2236 -- No analysis possible if the entity is not decorated
2237
2238 if No (Entity (N)) then
2239 return Skip;
2240
2241 -- Don't collect identifiers of packages, called functions, etc
2242
2243 elsif Ekind_In (Entity (N), E_Package,
2244 E_Function,
2245 E_Procedure,
2246 E_Entry)
2247 then
2248 return Skip;
2249
2250 -- For rewritten nodes, continue the traversal in the original
2251 -- subtree. Needed to handle aggregates in original expressions
2252 -- extracted from the tree by Remove_Side_Effects.
2253
2254 elsif Is_Rewrite_Substitution (N) then
2255 Collect_Identifiers (Original_Node (N));
2256 return Skip;
2257
2258 -- For now we skip aggregate discriminants, since they require
2259 -- performing the analysis in two phases to identify conflicts:
2260 -- first one analyzing discriminants and second one analyzing
2261 -- the rest of components (since at run time, discriminants are
2262 -- evaluated prior to components): too much computation cost
2263 -- to identify a corner case???
2264
2265 elsif Nkind (Parent (N)) = N_Component_Association
2266 and then Nkind_In (Parent (Parent (N)),
2267 N_Aggregate,
2268 N_Extension_Aggregate)
2269 then
2270 declare
2271 Choice : constant Node_Id := First (Choices (Parent (N)));
2272
2273 begin
2274 if Ekind (Entity (N)) = E_Discriminant then
2275 return Skip;
2276
2277 elsif Expression (Parent (N)) = N
2278 and then Nkind (Choice) = N_Identifier
2279 and then Ekind (Entity (Choice)) = E_Discriminant
2280 then
2281 return Skip;
2282 end if;
2283 end;
2284
2285 -- Analyze if N is a writable actual of a function
2286
2287 elsif Nkind (Parent (N)) = N_Function_Call then
2288 declare
2289 Call : constant Node_Id := Parent (N);
2290 Actual : Node_Id;
2291 Formal : Node_Id;
2292
2293 begin
2294 Id := Get_Called_Entity (Call);
2295
2296 -- In case of previous error, no check is possible
2297
2298 if No (Id) then
2299 return Abandon;
2300 end if;
2301
2302 if Ekind_In (Id, E_Function, E_Generic_Function)
2303 and then Has_Out_Or_In_Out_Parameter (Id)
2304 then
2305 Formal := First_Formal (Id);
2306 Actual := First_Actual (Call);
2307 while Present (Actual) and then Present (Formal) loop
2308 if Actual = N then
2309 if Ekind_In (Formal, E_Out_Parameter,
2310 E_In_Out_Parameter)
2311 then
2312 Is_Writable_Actual := True;
2313 end if;
2314
2315 exit;
2316 end if;
2317
2318 Next_Formal (Formal);
2319 Next_Actual (Actual);
2320 end loop;
2321 end if;
2322 end;
2323 end if;
2324
2325 if Is_Writable_Actual then
2326
2327 -- Skip checking the error in non-elementary types since
2328 -- RM 6.4.1(6.15/3) is restricted to elementary types, but
2329 -- store this actual in Writable_Actuals_List since it is
2330 -- needed to perform checks on other constructs that have
2331 -- arbitrary order of evaluation (for example, aggregates).
2332
2333 if not Is_Elementary_Type (Etype (N)) then
2334 if not Contains (Writable_Actuals_List, N) then
2335 Append_New_Elmt (N, To => Writable_Actuals_List);
2336 end if;
2337
2338 -- Second occurrence of an elementary type writable actual
2339
2340 elsif Contains (Writable_Actuals_List, N) then
2341
2342 -- Report the error on the second occurrence of the
2343 -- identifier. We cannot assume that N is the second
2344 -- occurrence (according to their location in the
2345 -- sources), since Traverse_Func walks through Field2
2346 -- last (see comment in the body of Traverse_Func).
2347
2348 declare
2349 Elmt : Elmt_Id;
2350
2351 begin
2352 Elmt := First_Elmt (Writable_Actuals_List);
2353 while Present (Elmt)
2354 and then Entity (Node (Elmt)) /= Entity (N)
2355 loop
2356 Next_Elmt (Elmt);
2357 end loop;
2358
2359 if Sloc (N) > Sloc (Node (Elmt)) then
2360 Error_Node := N;
2361 else
2362 Error_Node := Node (Elmt);
2363 end if;
2364
2365 Error_Msg_NE
2366 ("value may be affected by call to & "
2367 & "because order of evaluation is arbitrary",
2368 Error_Node, Id);
2369 return Abandon;
2370 end;
2371
2372 -- First occurrence of a elementary type writable actual
2373
2374 else
2375 Append_New_Elmt (N, To => Writable_Actuals_List);
2376 end if;
2377
2378 else
2379 if Identifiers_List = No_Elist then
2380 Identifiers_List := New_Elmt_List;
2381 end if;
2382
2383 Append_Unique_Elmt (N, Identifiers_List);
2384 end if;
2385 end if;
2386
2387 return OK;
2388 end Check_Node;
2389
2390 --------------
2391 -- Contains --
2392 --------------
2393
2394 function Contains
2395 (List : Elist_Id;
2396 N : Node_Id) return Boolean
2397 is
2398 pragma Assert (Nkind (N) in N_Has_Entity);
2399
2400 Elmt : Elmt_Id;
2401
2402 begin
2403 if List = No_Elist then
2404 return False;
2405 end if;
2406
2407 Elmt := First_Elmt (List);
2408 while Present (Elmt) loop
2409 if Entity (Node (Elmt)) = Entity (N) then
2410 return True;
2411 else
2412 Next_Elmt (Elmt);
2413 end if;
2414 end loop;
2415
2416 return False;
2417 end Contains;
2418
2419 ------------------
2420 -- Do_Traversal --
2421 ------------------
2422
2423 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2424 -- The traversal procedure
2425
2426 -- Start of processing for Collect_Identifiers
2427
2428 begin
2429 if Present (Error_Node) then
2430 return;
2431 end if;
2432
2433 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2434 return;
2435 end if;
2436
2437 Do_Traversal (N);
2438 end Collect_Identifiers;
2439
2440 -------------------------------
2441 -- Preanalyze_Without_Errors --
2442 -------------------------------
2443
2444 procedure Preanalyze_Without_Errors (N : Node_Id) is
2445 Status : constant Boolean := Get_Ignore_Errors;
2446 begin
2447 Set_Ignore_Errors (True);
2448 Preanalyze (N);
2449 Set_Ignore_Errors (Status);
2450 end Preanalyze_Without_Errors;
2451
2452 -- Start of processing for Check_Function_Writable_Actuals
2453
2454 begin
2455 -- The check only applies to Ada 2012 code on which Check_Actuals has
2456 -- been set, and only to constructs that have multiple constituents
2457 -- whose order of evaluation is not specified by the language.
2458
2459 if Ada_Version < Ada_2012
2460 or else not Check_Actuals (N)
2461 or else (not (Nkind (N) in N_Op)
2462 and then not (Nkind (N) in N_Membership_Test)
2463 and then not Nkind_In (N, N_Range,
2464 N_Aggregate,
2465 N_Extension_Aggregate,
2466 N_Full_Type_Declaration,
2467 N_Function_Call,
2468 N_Procedure_Call_Statement,
2469 N_Entry_Call_Statement))
2470 or else (Nkind (N) = N_Full_Type_Declaration
2471 and then not Is_Record_Type (Defining_Identifier (N)))
2472
2473 -- In addition, this check only applies to source code, not to code
2474 -- generated by constraint checks.
2475
2476 or else not Comes_From_Source (N)
2477 then
2478 return;
2479 end if;
2480
2481 -- If a construct C has two or more direct constituents that are names
2482 -- or expressions whose evaluation may occur in an arbitrary order, at
2483 -- least one of which contains a function call with an in out or out
2484 -- parameter, then the construct is legal only if: for each name N that
2485 -- is passed as a parameter of mode in out or out to some inner function
2486 -- call C2 (not including the construct C itself), there is no other
2487 -- name anywhere within a direct constituent of the construct C other
2488 -- than the one containing C2, that is known to refer to the same
2489 -- object (RM 6.4.1(6.17/3)).
2490
2491 case Nkind (N) is
2492 when N_Range =>
2493 Collect_Identifiers (Low_Bound (N));
2494 Collect_Identifiers (High_Bound (N));
2495
2496 when N_Membership_Test
2497 | N_Op
2498 =>
2499 declare
2500 Expr : Node_Id;
2501
2502 begin
2503 Collect_Identifiers (Left_Opnd (N));
2504
2505 if Present (Right_Opnd (N)) then
2506 Collect_Identifiers (Right_Opnd (N));
2507 end if;
2508
2509 if Nkind_In (N, N_In, N_Not_In)
2510 and then Present (Alternatives (N))
2511 then
2512 Expr := First (Alternatives (N));
2513 while Present (Expr) loop
2514 Collect_Identifiers (Expr);
2515
2516 Next (Expr);
2517 end loop;
2518 end if;
2519 end;
2520
2521 when N_Full_Type_Declaration =>
2522 declare
2523 function Get_Record_Part (N : Node_Id) return Node_Id;
2524 -- Return the record part of this record type definition
2525
2526 function Get_Record_Part (N : Node_Id) return Node_Id is
2527 Type_Def : constant Node_Id := Type_Definition (N);
2528 begin
2529 if Nkind (Type_Def) = N_Derived_Type_Definition then
2530 return Record_Extension_Part (Type_Def);
2531 else
2532 return Type_Def;
2533 end if;
2534 end Get_Record_Part;
2535
2536 Comp : Node_Id;
2537 Def_Id : Entity_Id := Defining_Identifier (N);
2538 Rec : Node_Id := Get_Record_Part (N);
2539
2540 begin
2541 -- No need to perform any analysis if the record has no
2542 -- components
2543
2544 if No (Rec) or else No (Component_List (Rec)) then
2545 return;
2546 end if;
2547
2548 -- Collect the identifiers starting from the deepest
2549 -- derivation. Done to report the error in the deepest
2550 -- derivation.
2551
2552 loop
2553 if Present (Component_List (Rec)) then
2554 Comp := First (Component_Items (Component_List (Rec)));
2555 while Present (Comp) loop
2556 if Nkind (Comp) = N_Component_Declaration
2557 and then Present (Expression (Comp))
2558 then
2559 Collect_Identifiers (Expression (Comp));
2560 end if;
2561
2562 Next (Comp);
2563 end loop;
2564 end if;
2565
2566 exit when No (Underlying_Type (Etype (Def_Id)))
2567 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2568 = Def_Id;
2569
2570 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2571 Rec := Get_Record_Part (Parent (Def_Id));
2572 end loop;
2573 end;
2574
2575 when N_Entry_Call_Statement
2576 | N_Subprogram_Call
2577 =>
2578 declare
2579 Id : constant Entity_Id := Get_Called_Entity (N);
2580 Formal : Node_Id;
2581 Actual : Node_Id;
2582
2583 begin
2584 Formal := First_Formal (Id);
2585 Actual := First_Actual (N);
2586 while Present (Actual) and then Present (Formal) loop
2587 if Ekind_In (Formal, E_Out_Parameter,
2588 E_In_Out_Parameter)
2589 then
2590 Collect_Identifiers (Actual);
2591 end if;
2592
2593 Next_Formal (Formal);
2594 Next_Actual (Actual);
2595 end loop;
2596 end;
2597
2598 when N_Aggregate
2599 | N_Extension_Aggregate
2600 =>
2601 declare
2602 Assoc : Node_Id;
2603 Choice : Node_Id;
2604 Comp_Expr : Node_Id;
2605
2606 begin
2607 -- Handle the N_Others_Choice of array aggregates with static
2608 -- bounds. There is no need to perform this analysis in
2609 -- aggregates without static bounds since we cannot evaluate
2610 -- if the N_Others_Choice covers several elements. There is
2611 -- no need to handle the N_Others choice of record aggregates
2612 -- since at this stage it has been already expanded by
2613 -- Resolve_Record_Aggregate.
2614
2615 if Is_Array_Type (Etype (N))
2616 and then Nkind (N) = N_Aggregate
2617 and then Present (Aggregate_Bounds (N))
2618 and then Compile_Time_Known_Bounds (Etype (N))
2619 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2620 >
2621 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2622 then
2623 declare
2624 Count_Components : Uint := Uint_0;
2625 Num_Components : Uint;
2626 Others_Assoc : Node_Id;
2627 Others_Choice : Node_Id := Empty;
2628 Others_Box_Present : Boolean := False;
2629
2630 begin
2631 -- Count positional associations
2632
2633 if Present (Expressions (N)) then
2634 Comp_Expr := First (Expressions (N));
2635 while Present (Comp_Expr) loop
2636 Count_Components := Count_Components + 1;
2637 Next (Comp_Expr);
2638 end loop;
2639 end if;
2640
2641 -- Count the rest of elements and locate the N_Others
2642 -- choice (if any)
2643
2644 Assoc := First (Component_Associations (N));
2645 while Present (Assoc) loop
2646 Choice := First (Choices (Assoc));
2647 while Present (Choice) loop
2648 if Nkind (Choice) = N_Others_Choice then
2649 Others_Assoc := Assoc;
2650 Others_Choice := Choice;
2651 Others_Box_Present := Box_Present (Assoc);
2652
2653 -- Count several components
2654
2655 elsif Nkind_In (Choice, N_Range,
2656 N_Subtype_Indication)
2657 or else (Is_Entity_Name (Choice)
2658 and then Is_Type (Entity (Choice)))
2659 then
2660 declare
2661 L, H : Node_Id;
2662 begin
2663 Get_Index_Bounds (Choice, L, H);
2664 pragma Assert
2665 (Compile_Time_Known_Value (L)
2666 and then Compile_Time_Known_Value (H));
2667 Count_Components :=
2668 Count_Components
2669 + Expr_Value (H) - Expr_Value (L) + 1;
2670 end;
2671
2672 -- Count single component. No other case available
2673 -- since we are handling an aggregate with static
2674 -- bounds.
2675
2676 else
2677 pragma Assert (Is_OK_Static_Expression (Choice)
2678 or else Nkind (Choice) = N_Identifier
2679 or else Nkind (Choice) = N_Integer_Literal);
2680
2681 Count_Components := Count_Components + 1;
2682 end if;
2683
2684 Next (Choice);
2685 end loop;
2686
2687 Next (Assoc);
2688 end loop;
2689
2690 Num_Components :=
2691 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2692 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2693
2694 pragma Assert (Count_Components <= Num_Components);
2695
2696 -- Handle the N_Others choice if it covers several
2697 -- components
2698
2699 if Present (Others_Choice)
2700 and then (Num_Components - Count_Components) > 1
2701 then
2702 if not Others_Box_Present then
2703
2704 -- At this stage, if expansion is active, the
2705 -- expression of the others choice has not been
2706 -- analyzed. Hence we generate a duplicate and
2707 -- we analyze it silently to have available the
2708 -- minimum decoration required to collect the
2709 -- identifiers.
2710
2711 if not Expander_Active then
2712 Comp_Expr := Expression (Others_Assoc);
2713 else
2714 Comp_Expr :=
2715 New_Copy_Tree (Expression (Others_Assoc));
2716 Preanalyze_Without_Errors (Comp_Expr);
2717 end if;
2718
2719 Collect_Identifiers (Comp_Expr);
2720
2721 if Writable_Actuals_List /= No_Elist then
2722
2723 -- As suggested by Robert, at current stage we
2724 -- report occurrences of this case as warnings.
2725
2726 Error_Msg_N
2727 ("writable function parameter may affect "
2728 & "value in other component because order "
2729 & "of evaluation is unspecified??",
2730 Node (First_Elmt (Writable_Actuals_List)));
2731 end if;
2732 end if;
2733 end if;
2734 end;
2735
2736 -- For an array aggregate, a discrete_choice_list that has
2737 -- a nonstatic range is considered as two or more separate
2738 -- occurrences of the expression (RM 6.4.1(20/3)).
2739
2740 elsif Is_Array_Type (Etype (N))
2741 and then Nkind (N) = N_Aggregate
2742 and then Present (Aggregate_Bounds (N))
2743 and then not Compile_Time_Known_Bounds (Etype (N))
2744 then
2745 -- Collect identifiers found in the dynamic bounds
2746
2747 declare
2748 Count_Components : Natural := 0;
2749 Low, High : Node_Id;
2750
2751 begin
2752 Assoc := First (Component_Associations (N));
2753 while Present (Assoc) loop
2754 Choice := First (Choices (Assoc));
2755 while Present (Choice) loop
2756 if Nkind_In (Choice, N_Range,
2757 N_Subtype_Indication)
2758 or else (Is_Entity_Name (Choice)
2759 and then Is_Type (Entity (Choice)))
2760 then
2761 Get_Index_Bounds (Choice, Low, High);
2762
2763 if not Compile_Time_Known_Value (Low) then
2764 Collect_Identifiers (Low);
2765
2766 if No (Aggr_Error_Node) then
2767 Aggr_Error_Node := Low;
2768 end if;
2769 end if;
2770
2771 if not Compile_Time_Known_Value (High) then
2772 Collect_Identifiers (High);
2773
2774 if No (Aggr_Error_Node) then
2775 Aggr_Error_Node := High;
2776 end if;
2777 end if;
2778
2779 -- The RM rule is violated if there is more than
2780 -- a single choice in a component association.
2781
2782 else
2783 Count_Components := Count_Components + 1;
2784
2785 if No (Aggr_Error_Node)
2786 and then Count_Components > 1
2787 then
2788 Aggr_Error_Node := Choice;
2789 end if;
2790
2791 if not Compile_Time_Known_Value (Choice) then
2792 Collect_Identifiers (Choice);
2793 end if;
2794 end if;
2795
2796 Next (Choice);
2797 end loop;
2798
2799 Next (Assoc);
2800 end loop;
2801 end;
2802 end if;
2803
2804 -- Handle ancestor part of extension aggregates
2805
2806 if Nkind (N) = N_Extension_Aggregate then
2807 Collect_Identifiers (Ancestor_Part (N));
2808 end if;
2809
2810 -- Handle positional associations
2811
2812 if Present (Expressions (N)) then
2813 Comp_Expr := First (Expressions (N));
2814 while Present (Comp_Expr) loop
2815 if not Is_OK_Static_Expression (Comp_Expr) then
2816 Collect_Identifiers (Comp_Expr);
2817 end if;
2818
2819 Next (Comp_Expr);
2820 end loop;
2821 end if;
2822
2823 -- Handle discrete associations
2824
2825 if Present (Component_Associations (N)) then
2826 Assoc := First (Component_Associations (N));
2827 while Present (Assoc) loop
2828
2829 if not Box_Present (Assoc) then
2830 Choice := First (Choices (Assoc));
2831 while Present (Choice) loop
2832
2833 -- For now we skip discriminants since it requires
2834 -- performing the analysis in two phases: first one
2835 -- analyzing discriminants and second one analyzing
2836 -- the rest of components since discriminants are
2837 -- evaluated prior to components: too much extra
2838 -- work to detect a corner case???
2839
2840 if Nkind (Choice) in N_Has_Entity
2841 and then Present (Entity (Choice))
2842 and then Ekind (Entity (Choice)) = E_Discriminant
2843 then
2844 null;
2845
2846 elsif Box_Present (Assoc) then
2847 null;
2848
2849 else
2850 if not Analyzed (Expression (Assoc)) then
2851 Comp_Expr :=
2852 New_Copy_Tree (Expression (Assoc));
2853 Set_Parent (Comp_Expr, Parent (N));
2854 Preanalyze_Without_Errors (Comp_Expr);
2855 else
2856 Comp_Expr := Expression (Assoc);
2857 end if;
2858
2859 Collect_Identifiers (Comp_Expr);
2860 end if;
2861
2862 Next (Choice);
2863 end loop;
2864 end if;
2865
2866 Next (Assoc);
2867 end loop;
2868 end if;
2869 end;
2870
2871 when others =>
2872 return;
2873 end case;
2874
2875 -- No further action needed if we already reported an error
2876
2877 if Present (Error_Node) then
2878 return;
2879 end if;
2880
2881 -- Check violation of RM 6.20/3 in aggregates
2882
2883 if Present (Aggr_Error_Node)
2884 and then Writable_Actuals_List /= No_Elist
2885 then
2886 Error_Msg_N
2887 ("value may be affected by call in other component because they "
2888 & "are evaluated in unspecified order",
2889 Node (First_Elmt (Writable_Actuals_List)));
2890 return;
2891 end if;
2892
2893 -- Check if some writable argument of a function is referenced
2894
2895 if Writable_Actuals_List /= No_Elist
2896 and then Identifiers_List /= No_Elist
2897 then
2898 declare
2899 Elmt_1 : Elmt_Id;
2900 Elmt_2 : Elmt_Id;
2901
2902 begin
2903 Elmt_1 := First_Elmt (Writable_Actuals_List);
2904 while Present (Elmt_1) loop
2905 Elmt_2 := First_Elmt (Identifiers_List);
2906 while Present (Elmt_2) loop
2907 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2908 case Nkind (Parent (Node (Elmt_2))) is
2909 when N_Aggregate
2910 | N_Component_Association
2911 | N_Component_Declaration
2912 =>
2913 Error_Msg_N
2914 ("value may be affected by call in other "
2915 & "component because they are evaluated "
2916 & "in unspecified order",
2917 Node (Elmt_2));
2918
2919 when N_In
2920 | N_Not_In
2921 =>
2922 Error_Msg_N
2923 ("value may be affected by call in other "
2924 & "alternative because they are evaluated "
2925 & "in unspecified order",
2926 Node (Elmt_2));
2927
2928 when others =>
2929 Error_Msg_N
2930 ("value of actual may be affected by call in "
2931 & "other actual because they are evaluated "
2932 & "in unspecified order",
2933 Node (Elmt_2));
2934 end case;
2935 end if;
2936
2937 Next_Elmt (Elmt_2);
2938 end loop;
2939
2940 Next_Elmt (Elmt_1);
2941 end loop;
2942 end;
2943 end if;
2944 end Check_Function_Writable_Actuals;
2945
2946 --------------------------------
2947 -- Check_Implicit_Dereference --
2948 --------------------------------
2949
2950 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2951 Disc : Entity_Id;
2952 Desig : Entity_Id;
2953 Nam : Node_Id;
2954
2955 begin
2956 if Nkind (N) = N_Indexed_Component
2957 and then Present (Generalized_Indexing (N))
2958 then
2959 Nam := Generalized_Indexing (N);
2960 else
2961 Nam := N;
2962 end if;
2963
2964 if Ada_Version < Ada_2012
2965 or else not Has_Implicit_Dereference (Base_Type (Typ))
2966 then
2967 return;
2968
2969 elsif not Comes_From_Source (N)
2970 and then Nkind (N) /= N_Indexed_Component
2971 then
2972 return;
2973
2974 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2975 null;
2976
2977 else
2978 Disc := First_Discriminant (Typ);
2979 while Present (Disc) loop
2980 if Has_Implicit_Dereference (Disc) then
2981 Desig := Designated_Type (Etype (Disc));
2982 Add_One_Interp (Nam, Disc, Desig);
2983
2984 -- If the node is a generalized indexing, add interpretation
2985 -- to that node as well, for subsequent resolution.
2986
2987 if Nkind (N) = N_Indexed_Component then
2988 Add_One_Interp (N, Disc, Desig);
2989 end if;
2990
2991 -- If the operation comes from a generic unit and the context
2992 -- is a selected component, the selector name may be global
2993 -- and set in the instance already. Remove the entity to
2994 -- force resolution of the selected component, and the
2995 -- generation of an explicit dereference if needed.
2996
2997 if In_Instance
2998 and then Nkind (Parent (Nam)) = N_Selected_Component
2999 then
3000 Set_Entity (Selector_Name (Parent (Nam)), Empty);
3001 end if;
3002
3003 exit;
3004 end if;
3005
3006 Next_Discriminant (Disc);
3007 end loop;
3008 end if;
3009 end Check_Implicit_Dereference;
3010
3011 ----------------------------------
3012 -- Check_Internal_Protected_Use --
3013 ----------------------------------
3014
3015 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
3016 S : Entity_Id;
3017 Prot : Entity_Id;
3018
3019 begin
3020 Prot := Empty;
3021
3022 S := Current_Scope;
3023 while Present (S) loop
3024 if S = Standard_Standard then
3025 exit;
3026
3027 elsif Ekind (S) = E_Function
3028 and then Ekind (Scope (S)) = E_Protected_Type
3029 then
3030 Prot := Scope (S);
3031 exit;
3032 end if;
3033
3034 S := Scope (S);
3035 end loop;
3036
3037 if Present (Prot)
3038 and then Scope (Nam) = Prot
3039 and then Ekind (Nam) /= E_Function
3040 then
3041 -- An indirect function call (e.g. a callback within a protected
3042 -- function body) is not statically illegal. If the access type is
3043 -- anonymous and is the type of an access parameter, the scope of Nam
3044 -- will be the protected type, but it is not a protected operation.
3045
3046 if Ekind (Nam) = E_Subprogram_Type
3047 and then Nkind (Associated_Node_For_Itype (Nam)) =
3048 N_Function_Specification
3049 then
3050 null;
3051
3052 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
3053 Error_Msg_N
3054 ("within protected function cannot use protected procedure in "
3055 & "renaming or as generic actual", N);
3056
3057 elsif Nkind (N) = N_Attribute_Reference then
3058 Error_Msg_N
3059 ("within protected function cannot take access of protected "
3060 & "procedure", N);
3061
3062 else
3063 Error_Msg_N
3064 ("within protected function, protected object is constant", N);
3065 Error_Msg_N
3066 ("\cannot call operation that may modify it", N);
3067 end if;
3068 end if;
3069
3070 -- Verify that an internal call does not appear within a precondition
3071 -- of a protected operation. This implements AI12-0166.
3072 -- The precondition aspect has been rewritten as a pragma Precondition
3073 -- and we check whether the scope of the called subprogram is the same
3074 -- as that of the entity to which the aspect applies.
3075
3076 if Convention (Nam) = Convention_Protected then
3077 declare
3078 P : Node_Id;
3079
3080 begin
3081 P := Parent (N);
3082 while Present (P) loop
3083 if Nkind (P) = N_Pragma
3084 and then Chars (Pragma_Identifier (P)) = Name_Precondition
3085 and then From_Aspect_Specification (P)
3086 and then
3087 Scope (Entity (Corresponding_Aspect (P))) = Scope (Nam)
3088 then
3089 Error_Msg_N
3090 ("internal call cannot appear in precondition of "
3091 & "protected operation", N);
3092 return;
3093
3094 elsif Nkind (P) = N_Pragma
3095 and then Chars (Pragma_Identifier (P)) = Name_Contract_Cases
3096 then
3097 -- Check whether call is in a case guard. It is legal in a
3098 -- consequence.
3099
3100 P := N;
3101 while Present (P) loop
3102 if Nkind (Parent (P)) = N_Component_Association
3103 and then P /= Expression (Parent (P))
3104 then
3105 Error_Msg_N
3106 ("internal call cannot appear in case guard in a "
3107 & "contract case", N);
3108 end if;
3109
3110 P := Parent (P);
3111 end loop;
3112
3113 return;
3114
3115 elsif Nkind (P) = N_Parameter_Specification
3116 and then Scope (Current_Scope) = Scope (Nam)
3117 and then Nkind_In (Parent (P), N_Entry_Declaration,
3118 N_Subprogram_Declaration)
3119 then
3120 Error_Msg_N
3121 ("internal call cannot appear in default for formal of "
3122 & "protected operation", N);
3123 return;
3124 end if;
3125
3126 P := Parent (P);
3127 end loop;
3128 end;
3129 end if;
3130 end Check_Internal_Protected_Use;
3131
3132 ---------------------------------------
3133 -- Check_Later_Vs_Basic_Declarations --
3134 ---------------------------------------
3135
3136 procedure Check_Later_Vs_Basic_Declarations
3137 (Decls : List_Id;
3138 During_Parsing : Boolean)
3139 is
3140 Body_Sloc : Source_Ptr;
3141 Decl : Node_Id;
3142
3143 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
3144 -- Return whether Decl is considered as a declarative item.
3145 -- When During_Parsing is True, the semantics of Ada 83 is followed.
3146 -- When During_Parsing is False, the semantics of SPARK is followed.
3147
3148 -------------------------------
3149 -- Is_Later_Declarative_Item --
3150 -------------------------------
3151
3152 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
3153 begin
3154 if Nkind (Decl) in N_Later_Decl_Item then
3155 return True;
3156
3157 elsif Nkind (Decl) = N_Pragma then
3158 return True;
3159
3160 elsif During_Parsing then
3161 return False;
3162
3163 -- In SPARK, a package declaration is not considered as a later
3164 -- declarative item.
3165
3166 elsif Nkind (Decl) = N_Package_Declaration then
3167 return False;
3168
3169 -- In SPARK, a renaming is considered as a later declarative item
3170
3171 elsif Nkind (Decl) in N_Renaming_Declaration then
3172 return True;
3173
3174 else
3175 return False;
3176 end if;
3177 end Is_Later_Declarative_Item;
3178
3179 -- Start of processing for Check_Later_Vs_Basic_Declarations
3180
3181 begin
3182 Decl := First (Decls);
3183
3184 -- Loop through sequence of basic declarative items
3185
3186 Outer : while Present (Decl) loop
3187 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
3188 and then Nkind (Decl) not in N_Body_Stub
3189 then
3190 Next (Decl);
3191
3192 -- Once a body is encountered, we only allow later declarative
3193 -- items. The inner loop checks the rest of the list.
3194
3195 else
3196 Body_Sloc := Sloc (Decl);
3197
3198 Inner : while Present (Decl) loop
3199 if not Is_Later_Declarative_Item (Decl) then
3200 if During_Parsing then
3201 if Ada_Version = Ada_83 then
3202 Error_Msg_Sloc := Body_Sloc;
3203 Error_Msg_N
3204 ("(Ada 83) decl cannot appear after body#", Decl);
3205 end if;
3206 else
3207 Error_Msg_Sloc := Body_Sloc;
3208 Check_SPARK_05_Restriction
3209 ("decl cannot appear after body#", Decl);
3210 end if;
3211 end if;
3212
3213 Next (Decl);
3214 end loop Inner;
3215 end if;
3216 end loop Outer;
3217 end Check_Later_Vs_Basic_Declarations;
3218
3219 ---------------------------
3220 -- Check_No_Hidden_State --
3221 ---------------------------
3222
3223 procedure Check_No_Hidden_State (Id : Entity_Id) is
3224 Context : Entity_Id := Empty;
3225 Not_Visible : Boolean := False;
3226 Scop : Entity_Id;
3227
3228 begin
3229 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3230
3231 -- Nothing to do for internally-generated abstract states and variables
3232 -- because they do not represent the hidden state of the source unit.
3233
3234 if not Comes_From_Source (Id) then
3235 return;
3236 end if;
3237
3238 -- Find the proper context where the object or state appears
3239
3240 Scop := Scope (Id);
3241 while Present (Scop) loop
3242 Context := Scop;
3243
3244 -- Keep track of the context's visibility
3245
3246 Not_Visible := Not_Visible or else In_Private_Part (Context);
3247
3248 -- Prevent the search from going too far
3249
3250 if Context = Standard_Standard then
3251 return;
3252
3253 -- Objects and states that appear immediately within a subprogram or
3254 -- inside a construct nested within a subprogram do not introduce a
3255 -- hidden state. They behave as local variable declarations.
3256
3257 elsif Is_Subprogram (Context) then
3258 return;
3259
3260 -- When examining a package body, use the entity of the spec as it
3261 -- carries the abstract state declarations.
3262
3263 elsif Ekind (Context) = E_Package_Body then
3264 Context := Spec_Entity (Context);
3265 end if;
3266
3267 -- Stop the traversal when a package subject to a null abstract state
3268 -- has been found.
3269
3270 if Ekind_In (Context, E_Generic_Package, E_Package)
3271 and then Has_Null_Abstract_State (Context)
3272 then
3273 exit;
3274 end if;
3275
3276 Scop := Scope (Scop);
3277 end loop;
3278
3279 -- At this point we know that there is at least one package with a null
3280 -- abstract state in visibility. Emit an error message unconditionally
3281 -- if the entity being processed is a state because the placement of the
3282 -- related package is irrelevant. This is not the case for objects as
3283 -- the intermediate context matters.
3284
3285 if Present (Context)
3286 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3287 then
3288 Error_Msg_N ("cannot introduce hidden state &", Id);
3289 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3290 end if;
3291 end Check_No_Hidden_State;
3292
3293 ----------------------------------------
3294 -- Check_Nonvolatile_Function_Profile --
3295 ----------------------------------------
3296
3297 procedure Check_Nonvolatile_Function_Profile (Func_Id : Entity_Id) is
3298 Formal : Entity_Id;
3299
3300 begin
3301 -- Inspect all formal parameters
3302
3303 Formal := First_Formal (Func_Id);
3304 while Present (Formal) loop
3305 if Is_Effectively_Volatile (Etype (Formal)) then
3306 Error_Msg_NE
3307 ("nonvolatile function & cannot have a volatile parameter",
3308 Formal, Func_Id);
3309 end if;
3310
3311 Next_Formal (Formal);
3312 end loop;
3313
3314 -- Inspect the return type
3315
3316 if Is_Effectively_Volatile (Etype (Func_Id)) then
3317 Error_Msg_NE
3318 ("nonvolatile function & cannot have a volatile return type",
3319 Result_Definition (Parent (Func_Id)), Func_Id);
3320 end if;
3321 end Check_Nonvolatile_Function_Profile;
3322
3323 -----------------------------
3324 -- Check_Part_Of_Reference --
3325 -----------------------------
3326
3327 procedure Check_Part_Of_Reference (Var_Id : Entity_Id; Ref : Node_Id) is
3328 function Is_Enclosing_Package_Body
3329 (Body_Decl : Node_Id;
3330 Obj_Id : Entity_Id) return Boolean;
3331 pragma Inline (Is_Enclosing_Package_Body);
3332 -- Determine whether package body Body_Decl or its corresponding spec
3333 -- immediately encloses the declaration of object Obj_Id.
3334
3335 function Is_Internal_Declaration_Or_Body
3336 (Decl : Node_Id) return Boolean;
3337 pragma Inline (Is_Internal_Declaration_Or_Body);
3338 -- Determine whether declaration or body denoted by Decl is internal
3339
3340 function Is_Single_Declaration_Or_Body
3341 (Decl : Node_Id;
3342 Conc_Typ : Entity_Id) return Boolean;
3343 pragma Inline (Is_Single_Declaration_Or_Body);
3344 -- Determine whether protected/task declaration or body denoted by Decl
3345 -- belongs to single concurrent type Conc_Typ.
3346
3347 function Is_Single_Task_Pragma
3348 (Prag : Node_Id;
3349 Task_Typ : Entity_Id) return Boolean;
3350 pragma Inline (Is_Single_Task_Pragma);
3351 -- Determine whether pragma Prag belongs to single task type Task_Typ
3352
3353 -------------------------------
3354 -- Is_Enclosing_Package_Body --
3355 -------------------------------
3356
3357 function Is_Enclosing_Package_Body
3358 (Body_Decl : Node_Id;
3359 Obj_Id : Entity_Id) return Boolean
3360 is
3361 Obj_Context : Node_Id;
3362
3363 begin
3364 -- Find the context of the object declaration
3365
3366 Obj_Context := Parent (Declaration_Node (Obj_Id));
3367
3368 if Nkind (Obj_Context) = N_Package_Specification then
3369 Obj_Context := Parent (Obj_Context);
3370 end if;
3371
3372 -- The object appears immediately within the package body
3373
3374 if Obj_Context = Body_Decl then
3375 return True;
3376
3377 -- The object appears immediately within the corresponding spec
3378
3379 elsif Nkind (Obj_Context) = N_Package_Declaration
3380 and then Unit_Declaration_Node (Corresponding_Spec (Body_Decl)) =
3381 Obj_Context
3382 then
3383 return True;
3384 end if;
3385
3386 return False;
3387 end Is_Enclosing_Package_Body;
3388
3389 -------------------------------------
3390 -- Is_Internal_Declaration_Or_Body --
3391 -------------------------------------
3392
3393 function Is_Internal_Declaration_Or_Body
3394 (Decl : Node_Id) return Boolean
3395 is
3396 begin
3397 if Comes_From_Source (Decl) then
3398 return False;
3399
3400 -- A body generated for an expression function which has not been
3401 -- inserted into the tree yet (In_Spec_Expression is True) is not
3402 -- considered internal.
3403
3404 elsif Nkind (Decl) = N_Subprogram_Body
3405 and then Was_Expression_Function (Decl)
3406 and then not In_Spec_Expression
3407 then
3408 return False;
3409 end if;
3410
3411 return True;
3412 end Is_Internal_Declaration_Or_Body;
3413
3414 -----------------------------------
3415 -- Is_Single_Declaration_Or_Body --
3416 -----------------------------------
3417
3418 function Is_Single_Declaration_Or_Body
3419 (Decl : Node_Id;
3420 Conc_Typ : Entity_Id) return Boolean
3421 is
3422 Spec_Id : constant Entity_Id := Unique_Defining_Entity (Decl);
3423
3424 begin
3425 return
3426 Present (Anonymous_Object (Spec_Id))
3427 and then Anonymous_Object (Spec_Id) = Conc_Typ;
3428 end Is_Single_Declaration_Or_Body;
3429
3430 ---------------------------
3431 -- Is_Single_Task_Pragma --
3432 ---------------------------
3433
3434 function Is_Single_Task_Pragma
3435 (Prag : Node_Id;
3436 Task_Typ : Entity_Id) return Boolean
3437 is
3438 Decl : constant Node_Id := Find_Related_Declaration_Or_Body (Prag);
3439
3440 begin
3441 -- To qualify, the pragma must be associated with single task type
3442 -- Task_Typ.
3443
3444 return
3445 Is_Single_Task_Object (Task_Typ)
3446 and then Nkind (Decl) = N_Object_Declaration
3447 and then Defining_Entity (Decl) = Task_Typ;
3448 end Is_Single_Task_Pragma;
3449
3450 -- Local variables
3451
3452 Conc_Obj : constant Entity_Id := Encapsulating_State (Var_Id);
3453 Par : Node_Id;
3454 Prag_Nam : Name_Id;
3455 Prev : Node_Id;
3456
3457 -- Start of processing for Check_Part_Of_Reference
3458
3459 begin
3460 -- Nothing to do when the variable was recorded, but did not become a
3461 -- constituent of a single concurrent type.
3462
3463 if No (Conc_Obj) then
3464 return;
3465 end if;
3466
3467 -- Traverse the parent chain looking for a suitable context for the
3468 -- reference to the concurrent constituent.
3469
3470 Prev := Ref;
3471 Par := Parent (Prev);
3472 while Present (Par) loop
3473 if Nkind (Par) = N_Pragma then
3474 Prag_Nam := Pragma_Name (Par);
3475
3476 -- A concurrent constituent is allowed to appear in pragmas
3477 -- Initial_Condition and Initializes as this is part of the
3478 -- elaboration checks for the constituent (SPARK RM 9(3)).
3479
3480 if Nam_In (Prag_Nam, Name_Initial_Condition, Name_Initializes) then
3481 return;
3482
3483 -- When the reference appears within pragma Depends or Global,
3484 -- check whether the pragma applies to a single task type. Note
3485 -- that the pragma may not encapsulated by the type definition,
3486 -- but this is still a valid context.
3487
3488 elsif Nam_In (Prag_Nam, Name_Depends, Name_Global)
3489 and then Is_Single_Task_Pragma (Par, Conc_Obj)
3490 then
3491 return;
3492 end if;
3493
3494 -- The reference appears somewhere in the definition of a single
3495 -- concurrent type (SPARK RM 9(3)).
3496
3497 elsif Nkind_In (Par, N_Single_Protected_Declaration,
3498 N_Single_Task_Declaration)
3499 and then Defining_Entity (Par) = Conc_Obj
3500 then
3501 return;
3502
3503 -- The reference appears within the declaration or body of a single
3504 -- concurrent type (SPARK RM 9(3)).
3505
3506 elsif Nkind_In (Par, N_Protected_Body,
3507 N_Protected_Type_Declaration,
3508 N_Task_Body,
3509 N_Task_Type_Declaration)
3510 and then Is_Single_Declaration_Or_Body (Par, Conc_Obj)
3511 then
3512 return;
3513
3514 -- The reference appears within the statement list of the object's
3515 -- immediately enclosing package (SPARK RM 9(3)).
3516
3517 elsif Nkind (Par) = N_Package_Body
3518 and then Nkind (Prev) = N_Handled_Sequence_Of_Statements
3519 and then Is_Enclosing_Package_Body (Par, Var_Id)
3520 then
3521 return;
3522
3523 -- The reference has been relocated within an internally generated
3524 -- package or subprogram. Assume that the reference is legal as the
3525 -- real check was already performed in the original context of the
3526 -- reference.
3527
3528 elsif Nkind_In (Par, N_Package_Body,
3529 N_Package_Declaration,
3530 N_Subprogram_Body,
3531 N_Subprogram_Declaration)
3532 and then Is_Internal_Declaration_Or_Body (Par)
3533 then
3534 return;
3535
3536 -- The reference has been relocated to an inlined body for GNATprove.
3537 -- Assume that the reference is legal as the real check was already
3538 -- performed in the original context of the reference.
3539
3540 elsif GNATprove_Mode
3541 and then Nkind (Par) = N_Subprogram_Body
3542 and then Chars (Defining_Entity (Par)) = Name_uParent
3543 then
3544 return;
3545 end if;
3546
3547 Prev := Par;
3548 Par := Parent (Prev);
3549 end loop;
3550
3551 -- At this point it is known that the reference does not appear within a
3552 -- legal context.
3553
3554 Error_Msg_NE
3555 ("reference to variable & cannot appear in this context", Ref, Var_Id);
3556 Error_Msg_Name_1 := Chars (Var_Id);
3557
3558 if Is_Single_Protected_Object (Conc_Obj) then
3559 Error_Msg_NE
3560 ("\% is constituent of single protected type &", Ref, Conc_Obj);
3561
3562 else
3563 Error_Msg_NE
3564 ("\% is constituent of single task type &", Ref, Conc_Obj);
3565 end if;
3566 end Check_Part_Of_Reference;
3567
3568 ------------------------------------------
3569 -- Check_Potentially_Blocking_Operation --
3570 ------------------------------------------
3571
3572 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3573 S : Entity_Id;
3574
3575 begin
3576 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3577 -- When pragma Detect_Blocking is active, the run time will raise
3578 -- Program_Error. Here we only issue a warning, since we generally
3579 -- support the use of potentially blocking operations in the absence
3580 -- of the pragma.
3581
3582 -- Indirect blocking through a subprogram call cannot be diagnosed
3583 -- statically without interprocedural analysis, so we do not attempt
3584 -- to do it here.
3585
3586 S := Scope (Current_Scope);
3587 while Present (S) and then S /= Standard_Standard loop
3588 if Is_Protected_Type (S) then
3589 Error_Msg_N
3590 ("potentially blocking operation in protected operation??", N);
3591 return;
3592 end if;
3593
3594 S := Scope (S);
3595 end loop;
3596 end Check_Potentially_Blocking_Operation;
3597
3598 ------------------------------------
3599 -- Check_Previous_Null_Procedure --
3600 ------------------------------------
3601
3602 procedure Check_Previous_Null_Procedure
3603 (Decl : Node_Id;
3604 Prev : Entity_Id)
3605 is
3606 begin
3607 if Ekind (Prev) = E_Procedure
3608 and then Nkind (Parent (Prev)) = N_Procedure_Specification
3609 and then Null_Present (Parent (Prev))
3610 then
3611 Error_Msg_Sloc := Sloc (Prev);
3612 Error_Msg_N
3613 ("declaration cannot complete previous null procedure#", Decl);
3614 end if;
3615 end Check_Previous_Null_Procedure;
3616
3617 ---------------------------------
3618 -- Check_Result_And_Post_State --
3619 ---------------------------------
3620
3621 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3622 procedure Check_Result_And_Post_State_In_Pragma
3623 (Prag : Node_Id;
3624 Result_Seen : in out Boolean);
3625 -- Determine whether pragma Prag mentions attribute 'Result and whether
3626 -- the pragma contains an expression that evaluates differently in pre-
3627 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3628 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3629
3630 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3631 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3632 -- formal parameter.
3633
3634 -------------------------------------------
3635 -- Check_Result_And_Post_State_In_Pragma --
3636 -------------------------------------------
3637
3638 procedure Check_Result_And_Post_State_In_Pragma
3639 (Prag : Node_Id;
3640 Result_Seen : in out Boolean)
3641 is
3642 procedure Check_Conjunct (Expr : Node_Id);
3643 -- Check an individual conjunct in a conjunction of Boolean
3644 -- expressions, connected by "and" or "and then" operators.
3645
3646 procedure Check_Conjuncts (Expr : Node_Id);
3647 -- Apply the post-state check to every conjunct in an expression, in
3648 -- case this is a conjunction of Boolean expressions. Otherwise apply
3649 -- it to the expression as a whole.
3650
3651 procedure Check_Expression (Expr : Node_Id);
3652 -- Perform the 'Result and post-state checks on a given expression
3653
3654 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3655 -- Attempt to find attribute 'Result in a subtree denoted by N
3656
3657 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3658 -- Determine whether source node N denotes "True" or "False"
3659
3660 function Mentions_Post_State (N : Node_Id) return Boolean;
3661 -- Determine whether a subtree denoted by N mentions any construct
3662 -- that denotes a post-state.
3663
3664 procedure Check_Function_Result is
3665 new Traverse_Proc (Is_Function_Result);
3666
3667 --------------------
3668 -- Check_Conjunct --
3669 --------------------
3670
3671 procedure Check_Conjunct (Expr : Node_Id) is
3672 function Adjust_Message (Msg : String) return String;
3673 -- Prepend a prefix to the input message Msg denoting that the
3674 -- message applies to a conjunct in the expression, when this
3675 -- is the case.
3676
3677 function Applied_On_Conjunct return Boolean;
3678 -- Returns True if the message applies to a conjunct in the
3679 -- expression, instead of the whole expression.
3680
3681 function Has_Global_Output (Subp : Entity_Id) return Boolean;
3682 -- Returns True if Subp has an output in its Global contract
3683
3684 function Has_No_Output (Subp : Entity_Id) return Boolean;
3685 -- Returns True if Subp has no declared output: no function
3686 -- result, no output parameter, and no output in its Global
3687 -- contract.
3688
3689 --------------------
3690 -- Adjust_Message --
3691 --------------------
3692
3693 function Adjust_Message (Msg : String) return String is
3694 begin
3695 if Applied_On_Conjunct then
3696 return "conjunct in " & Msg;
3697 else
3698 return Msg;
3699 end if;
3700 end Adjust_Message;
3701
3702 -------------------------
3703 -- Applied_On_Conjunct --
3704 -------------------------
3705
3706 function Applied_On_Conjunct return Boolean is
3707 begin
3708 -- Expr is the conjunct of an enclosing "and" expression
3709
3710 return Nkind (Parent (Expr)) in N_Subexpr
3711
3712 -- or Expr is a conjunct of an enclosing "and then"
3713 -- expression in a postcondition aspect that was split into
3714 -- multiple pragmas. The first conjunct has the "and then"
3715 -- expression as Original_Node, and other conjuncts have
3716 -- Split_PCC set to True.
3717
3718 or else Nkind (Original_Node (Expr)) = N_And_Then
3719 or else Split_PPC (Prag);
3720 end Applied_On_Conjunct;
3721
3722 -----------------------
3723 -- Has_Global_Output --
3724 -----------------------
3725
3726 function Has_Global_Output (Subp : Entity_Id) return Boolean is
3727 Global : constant Node_Id := Get_Pragma (Subp, Pragma_Global);
3728 List : Node_Id;
3729 Assoc : Node_Id;
3730
3731 begin
3732 if No (Global) then
3733 return False;
3734 end if;
3735
3736 List := Expression (Get_Argument (Global, Subp));
3737
3738 -- Empty list (no global items) or single global item
3739 -- declaration (only input items).
3740
3741 if Nkind_In (List, N_Null,
3742 N_Expanded_Name,
3743 N_Identifier,
3744 N_Selected_Component)
3745 then
3746 return False;
3747
3748 -- Simple global list (only input items) or moded global list
3749 -- declaration.
3750
3751 elsif Nkind (List) = N_Aggregate then
3752 if Present (Expressions (List)) then
3753 return False;
3754
3755 else
3756 Assoc := First (Component_Associations (List));
3757 while Present (Assoc) loop
3758 if Chars (First (Choices (Assoc))) /= Name_Input then
3759 return True;
3760 end if;
3761
3762 Next (Assoc);
3763 end loop;
3764
3765 return False;
3766 end if;
3767
3768 -- To accommodate partial decoration of disabled SPARK
3769 -- features, this routine may be called with illegal input.
3770 -- If this is the case, do not raise Program_Error.
3771
3772 else
3773 return False;
3774 end if;
3775 end Has_Global_Output;
3776
3777 -------------------
3778 -- Has_No_Output --
3779 -------------------
3780
3781 function Has_No_Output (Subp : Entity_Id) return Boolean is
3782 Param : Node_Id;
3783
3784 begin
3785 -- A function has its result as output
3786
3787 if Ekind (Subp) = E_Function then
3788 return False;
3789 end if;
3790
3791 -- An OUT or IN OUT parameter is an output
3792
3793 Param := First_Formal (Subp);
3794 while Present (Param) loop
3795 if Ekind_In (Param, E_Out_Parameter, E_In_Out_Parameter) then
3796 return False;
3797 end if;
3798
3799 Next_Formal (Param);
3800 end loop;
3801
3802 -- An item of mode Output or In_Out in the Global contract is
3803 -- an output.
3804
3805 if Has_Global_Output (Subp) then
3806 return False;
3807 end if;
3808
3809 return True;
3810 end Has_No_Output;
3811
3812 -- Local variables
3813
3814 Err_Node : Node_Id;
3815 -- Error node when reporting a warning on a (refined)
3816 -- postcondition.
3817
3818 -- Start of processing for Check_Conjunct
3819
3820 begin
3821 if Applied_On_Conjunct then
3822 Err_Node := Expr;
3823 else
3824 Err_Node := Prag;
3825 end if;
3826
3827 -- Do not report missing reference to outcome in postcondition if
3828 -- either the postcondition is trivially True or False, or if the
3829 -- subprogram is ghost and has no declared output.
3830
3831 if not Is_Trivial_Boolean (Expr)
3832 and then not Mentions_Post_State (Expr)
3833 and then not (Is_Ghost_Entity (Subp_Id)
3834 and then Has_No_Output (Subp_Id))
3835 then
3836 if Pragma_Name (Prag) = Name_Contract_Cases then
3837 Error_Msg_NE (Adjust_Message
3838 ("contract case does not check the outcome of calling "
3839 & "&?T?"), Expr, Subp_Id);
3840
3841 elsif Pragma_Name (Prag) = Name_Refined_Post then
3842 Error_Msg_NE (Adjust_Message
3843 ("refined postcondition does not check the outcome of "
3844 & "calling &?T?"), Err_Node, Subp_Id);
3845
3846 else
3847 Error_Msg_NE (Adjust_Message
3848 ("postcondition does not check the outcome of calling "
3849 & "&?T?"), Err_Node, Subp_Id);
3850 end if;
3851 end if;
3852 end Check_Conjunct;
3853
3854 ---------------------
3855 -- Check_Conjuncts --
3856 ---------------------
3857
3858 procedure Check_Conjuncts (Expr : Node_Id) is
3859 begin
3860 if Nkind_In (Expr, N_Op_And, N_And_Then) then
3861 Check_Conjuncts (Left_Opnd (Expr));
3862 Check_Conjuncts (Right_Opnd (Expr));
3863 else
3864 Check_Conjunct (Expr);
3865 end if;
3866 end Check_Conjuncts;
3867
3868 ----------------------
3869 -- Check_Expression --
3870 ----------------------
3871
3872 procedure Check_Expression (Expr : Node_Id) is
3873 begin
3874 if not Is_Trivial_Boolean (Expr) then
3875 Check_Function_Result (Expr);
3876 Check_Conjuncts (Expr);
3877 end if;
3878 end Check_Expression;
3879
3880 ------------------------
3881 -- Is_Function_Result --
3882 ------------------------
3883
3884 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3885 begin
3886 if Is_Attribute_Result (N) then
3887 Result_Seen := True;
3888 return Abandon;
3889
3890 -- Warn on infinite recursion if call is to current function
3891
3892 elsif Nkind (N) = N_Function_Call
3893 and then Is_Entity_Name (Name (N))
3894 and then Entity (Name (N)) = Subp_Id
3895 and then not Is_Potentially_Unevaluated (N)
3896 then
3897 Error_Msg_NE
3898 ("call to & within its postcondition will lead to infinite "
3899 & "recursion?", N, Subp_Id);
3900 return OK;
3901
3902 -- Continue the traversal
3903
3904 else
3905 return OK;
3906 end if;
3907 end Is_Function_Result;
3908
3909 ------------------------
3910 -- Is_Trivial_Boolean --
3911 ------------------------
3912
3913 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3914 begin
3915 return
3916 Comes_From_Source (N)
3917 and then Is_Entity_Name (N)
3918 and then (Entity (N) = Standard_True
3919 or else
3920 Entity (N) = Standard_False);
3921 end Is_Trivial_Boolean;
3922
3923 -------------------------
3924 -- Mentions_Post_State --
3925 -------------------------
3926
3927 function Mentions_Post_State (N : Node_Id) return Boolean is
3928 Post_State_Seen : Boolean := False;
3929
3930 function Is_Post_State (N : Node_Id) return Traverse_Result;
3931 -- Attempt to find a construct that denotes a post-state. If this
3932 -- is the case, set flag Post_State_Seen.
3933
3934 -------------------
3935 -- Is_Post_State --
3936 -------------------
3937
3938 function Is_Post_State (N : Node_Id) return Traverse_Result is
3939 Ent : Entity_Id;
3940
3941 begin
3942 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3943 Post_State_Seen := True;
3944 return Abandon;
3945
3946 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3947 Ent := Entity (N);
3948
3949 -- Treat an undecorated reference as OK
3950
3951 if No (Ent)
3952
3953 -- A reference to an assignable entity is considered a
3954 -- change in the post-state of a subprogram.
3955
3956 or else Ekind_In (Ent, E_Generic_In_Out_Parameter,
3957 E_In_Out_Parameter,
3958 E_Out_Parameter,
3959 E_Variable)
3960
3961 -- The reference may be modified through a dereference
3962
3963 or else (Is_Access_Type (Etype (Ent))
3964 and then Nkind (Parent (N)) =
3965 N_Selected_Component)
3966 then
3967 Post_State_Seen := True;
3968 return Abandon;
3969 end if;
3970
3971 elsif Nkind (N) = N_Attribute_Reference then
3972 if Attribute_Name (N) = Name_Old then
3973 return Skip;
3974
3975 elsif Attribute_Name (N) = Name_Result then
3976 Post_State_Seen := True;
3977 return Abandon;
3978 end if;
3979 end if;
3980
3981 return OK;
3982 end Is_Post_State;
3983
3984 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3985
3986 -- Start of processing for Mentions_Post_State
3987
3988 begin
3989 Find_Post_State (N);
3990
3991 return Post_State_Seen;
3992 end Mentions_Post_State;
3993
3994 -- Local variables
3995
3996 Expr : constant Node_Id :=
3997 Get_Pragma_Arg
3998 (First (Pragma_Argument_Associations (Prag)));
3999 Nam : constant Name_Id := Pragma_Name (Prag);
4000 CCase : Node_Id;
4001
4002 -- Start of processing for Check_Result_And_Post_State_In_Pragma
4003
4004 begin
4005 -- Examine all consequences
4006
4007 if Nam = Name_Contract_Cases then
4008 CCase := First (Component_Associations (Expr));
4009 while Present (CCase) loop
4010 Check_Expression (Expression (CCase));
4011
4012 Next (CCase);
4013 end loop;
4014
4015 -- Examine the expression of a postcondition
4016
4017 else pragma Assert (Nam_In (Nam, Name_Postcondition,
4018 Name_Refined_Post));
4019 Check_Expression (Expr);
4020 end if;
4021 end Check_Result_And_Post_State_In_Pragma;
4022
4023 --------------------------
4024 -- Has_In_Out_Parameter --
4025 --------------------------
4026
4027 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
4028 Formal : Entity_Id;
4029
4030 begin
4031 -- Traverse the formals looking for an IN OUT parameter
4032
4033 Formal := First_Formal (Subp_Id);
4034 while Present (Formal) loop
4035 if Ekind (Formal) = E_In_Out_Parameter then
4036 return True;
4037 end if;
4038
4039 Next_Formal (Formal);
4040 end loop;
4041
4042 return False;
4043 end Has_In_Out_Parameter;
4044
4045 -- Local variables
4046
4047 Items : constant Node_Id := Contract (Subp_Id);
4048 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
4049 Case_Prag : Node_Id := Empty;
4050 Post_Prag : Node_Id := Empty;
4051 Prag : Node_Id;
4052 Seen_In_Case : Boolean := False;
4053 Seen_In_Post : Boolean := False;
4054 Spec_Id : Entity_Id;
4055
4056 -- Start of processing for Check_Result_And_Post_State
4057
4058 begin
4059 -- The lack of attribute 'Result or a post-state is classified as a
4060 -- suspicious contract. Do not perform the check if the corresponding
4061 -- swich is not set.
4062
4063 if not Warn_On_Suspicious_Contract then
4064 return;
4065
4066 -- Nothing to do if there is no contract
4067
4068 elsif No (Items) then
4069 return;
4070 end if;
4071
4072 -- Retrieve the entity of the subprogram spec (if any)
4073
4074 if Nkind (Subp_Decl) = N_Subprogram_Body
4075 and then Present (Corresponding_Spec (Subp_Decl))
4076 then
4077 Spec_Id := Corresponding_Spec (Subp_Decl);
4078
4079 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
4080 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
4081 then
4082 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
4083
4084 else
4085 Spec_Id := Subp_Id;
4086 end if;
4087
4088 -- Examine all postconditions for attribute 'Result and a post-state
4089
4090 Prag := Pre_Post_Conditions (Items);
4091 while Present (Prag) loop
4092 if Nam_In (Pragma_Name_Unmapped (Prag),
4093 Name_Postcondition, Name_Refined_Post)
4094 and then not Error_Posted (Prag)
4095 then
4096 Post_Prag := Prag;
4097 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
4098 end if;
4099
4100 Prag := Next_Pragma (Prag);
4101 end loop;
4102
4103 -- Examine the contract cases of the subprogram for attribute 'Result
4104 -- and a post-state.
4105
4106 Prag := Contract_Test_Cases (Items);
4107 while Present (Prag) loop
4108 if Pragma_Name (Prag) = Name_Contract_Cases
4109 and then not Error_Posted (Prag)
4110 then
4111 Case_Prag := Prag;
4112 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
4113 end if;
4114
4115 Prag := Next_Pragma (Prag);
4116 end loop;
4117
4118 -- Do not emit any errors if the subprogram is not a function
4119
4120 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
4121 null;
4122
4123 -- Regardless of whether the function has postconditions or contract
4124 -- cases, or whether they mention attribute 'Result, an IN OUT formal
4125 -- parameter is always treated as a result.
4126
4127 elsif Has_In_Out_Parameter (Spec_Id) then
4128 null;
4129
4130 -- The function has both a postcondition and contract cases and they do
4131 -- not mention attribute 'Result.
4132
4133 elsif Present (Case_Prag)
4134 and then not Seen_In_Case
4135 and then Present (Post_Prag)
4136 and then not Seen_In_Post
4137 then
4138 Error_Msg_N
4139 ("neither postcondition nor contract cases mention function "
4140 & "result?T?", Post_Prag);
4141
4142 -- The function has contract cases only and they do not mention
4143 -- attribute 'Result.
4144
4145 elsif Present (Case_Prag) and then not Seen_In_Case then
4146 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
4147
4148 -- The function has postconditions only and they do not mention
4149 -- attribute 'Result.
4150
4151 elsif Present (Post_Prag) and then not Seen_In_Post then
4152 Error_Msg_N
4153 ("postcondition does not mention function result?T?", Post_Prag);
4154 end if;
4155 end Check_Result_And_Post_State;
4156
4157 -----------------------------
4158 -- Check_State_Refinements --
4159 -----------------------------
4160
4161 procedure Check_State_Refinements
4162 (Context : Node_Id;
4163 Is_Main_Unit : Boolean := False)
4164 is
4165 procedure Check_Package (Pack : Node_Id);
4166 -- Verify that all abstract states of a [generic] package denoted by its
4167 -- declarative node Pack have proper refinement. Recursively verify the
4168 -- visible and private declarations of the [generic] package for other
4169 -- nested packages.
4170
4171 procedure Check_Packages_In (Decls : List_Id);
4172 -- Seek out [generic] package declarations within declarative list Decls
4173 -- and verify the status of their abstract state refinement.
4174
4175 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean;
4176 -- Determine whether construct N is subject to pragma SPARK_Mode Off
4177
4178 -------------------
4179 -- Check_Package --
4180 -------------------
4181
4182 procedure Check_Package (Pack : Node_Id) is
4183 Body_Id : constant Entity_Id := Corresponding_Body (Pack);
4184 Spec : constant Node_Id := Specification (Pack);
4185 States : constant Elist_Id :=
4186 Abstract_States (Defining_Entity (Pack));
4187
4188 State_Elmt : Elmt_Id;
4189 State_Id : Entity_Id;
4190
4191 begin
4192 -- Do not verify proper state refinement when the package is subject
4193 -- to pragma SPARK_Mode Off because this disables the requirement for
4194 -- state refinement.
4195
4196 if SPARK_Mode_Is_Off (Pack) then
4197 null;
4198
4199 -- State refinement can only occur in a completing package body. Do
4200 -- not verify proper state refinement when the body is subject to
4201 -- pragma SPARK_Mode Off because this disables the requirement for
4202 -- state refinement.
4203
4204 elsif Present (Body_Id)
4205 and then SPARK_Mode_Is_Off (Unit_Declaration_Node (Body_Id))
4206 then
4207 null;
4208
4209 -- Do not verify proper state refinement when the package is an
4210 -- instance as this check was already performed in the generic.
4211
4212 elsif Present (Generic_Parent (Spec)) then
4213 null;
4214
4215 -- Otherwise examine the contents of the package
4216
4217 else
4218 if Present (States) then
4219 State_Elmt := First_Elmt (States);
4220 while Present (State_Elmt) loop
4221 State_Id := Node (State_Elmt);
4222
4223 -- Emit an error when a non-null state lacks any form of
4224 -- refinement.
4225
4226 if not Is_Null_State (State_Id)
4227 and then not Has_Null_Refinement (State_Id)
4228 and then not Has_Non_Null_Refinement (State_Id)
4229 then
4230 Error_Msg_N ("state & requires refinement", State_Id);
4231 end if;
4232
4233 Next_Elmt (State_Elmt);
4234 end loop;
4235 end if;
4236
4237 Check_Packages_In (Visible_Declarations (Spec));
4238 Check_Packages_In (Private_Declarations (Spec));
4239 end if;
4240 end Check_Package;
4241
4242 -----------------------
4243 -- Check_Packages_In --
4244 -----------------------
4245
4246 procedure Check_Packages_In (Decls : List_Id) is
4247 Decl : Node_Id;
4248
4249 begin
4250 if Present (Decls) then
4251 Decl := First (Decls);
4252 while Present (Decl) loop
4253 if Nkind_In (Decl, N_Generic_Package_Declaration,
4254 N_Package_Declaration)
4255 then
4256 Check_Package (Decl);
4257 end if;
4258
4259 Next (Decl);
4260 end loop;
4261 end if;
4262 end Check_Packages_In;
4263
4264 -----------------------
4265 -- SPARK_Mode_Is_Off --
4266 -----------------------
4267
4268 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean is
4269 Id : constant Entity_Id := Defining_Entity (N);
4270 Prag : constant Node_Id := SPARK_Pragma (Id);
4271
4272 begin
4273 -- Default the mode to "off" when the context is an instance and all
4274 -- SPARK_Mode pragmas found within are to be ignored.
4275
4276 if Ignore_SPARK_Mode_Pragmas (Id) then
4277 return True;
4278
4279 else
4280 return
4281 Present (Prag)
4282 and then Get_SPARK_Mode_From_Annotation (Prag) = Off;
4283 end if;
4284 end SPARK_Mode_Is_Off;
4285
4286 -- Start of processing for Check_State_Refinements
4287
4288 begin
4289 -- A block may declare a nested package
4290
4291 if Nkind (Context) = N_Block_Statement then
4292 Check_Packages_In (Declarations (Context));
4293
4294 -- An entry, protected, subprogram, or task body may declare a nested
4295 -- package.
4296
4297 elsif Nkind_In (Context, N_Entry_Body,
4298 N_Protected_Body,
4299 N_Subprogram_Body,
4300 N_Task_Body)
4301 then
4302 -- Do not verify proper state refinement when the body is subject to
4303 -- pragma SPARK_Mode Off because this disables the requirement for
4304 -- state refinement.
4305
4306 if not SPARK_Mode_Is_Off (Context) then
4307 Check_Packages_In (Declarations (Context));
4308 end if;
4309
4310 -- A package body may declare a nested package
4311
4312 elsif Nkind (Context) = N_Package_Body then
4313 Check_Package (Unit_Declaration_Node (Corresponding_Spec (Context)));
4314
4315 -- Do not verify proper state refinement when the body is subject to
4316 -- pragma SPARK_Mode Off because this disables the requirement for
4317 -- state refinement.
4318
4319 if not SPARK_Mode_Is_Off (Context) then
4320 Check_Packages_In (Declarations (Context));
4321 end if;
4322
4323 -- A library level [generic] package may declare a nested package
4324
4325 elsif Nkind_In (Context, N_Generic_Package_Declaration,
4326 N_Package_Declaration)
4327 and then Is_Main_Unit
4328 then
4329 Check_Package (Context);
4330 end if;
4331 end Check_State_Refinements;
4332
4333 ------------------------------
4334 -- Check_Unprotected_Access --
4335 ------------------------------
4336
4337 procedure Check_Unprotected_Access
4338 (Context : Node_Id;
4339 Expr : Node_Id)
4340 is
4341 Cont_Encl_Typ : Entity_Id;
4342 Pref_Encl_Typ : Entity_Id;
4343
4344 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
4345 -- Check whether Obj is a private component of a protected object.
4346 -- Return the protected type where the component resides, Empty
4347 -- otherwise.
4348
4349 function Is_Public_Operation return Boolean;
4350 -- Verify that the enclosing operation is callable from outside the
4351 -- protected object, to minimize false positives.
4352
4353 ------------------------------
4354 -- Enclosing_Protected_Type --
4355 ------------------------------
4356
4357 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
4358 begin
4359 if Is_Entity_Name (Obj) then
4360 declare
4361 Ent : Entity_Id := Entity (Obj);
4362
4363 begin
4364 -- The object can be a renaming of a private component, use
4365 -- the original record component.
4366
4367 if Is_Prival (Ent) then
4368 Ent := Prival_Link (Ent);
4369 end if;
4370
4371 if Is_Protected_Type (Scope (Ent)) then
4372 return Scope (Ent);
4373 end if;
4374 end;
4375 end if;
4376
4377 -- For indexed and selected components, recursively check the prefix
4378
4379 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
4380 return Enclosing_Protected_Type (Prefix (Obj));
4381
4382 -- The object does not denote a protected component
4383
4384 else
4385 return Empty;
4386 end if;
4387 end Enclosing_Protected_Type;
4388
4389 -------------------------
4390 -- Is_Public_Operation --
4391 -------------------------
4392
4393 function Is_Public_Operation return Boolean is
4394 S : Entity_Id;
4395 E : Entity_Id;
4396
4397 begin
4398 S := Current_Scope;
4399 while Present (S) and then S /= Pref_Encl_Typ loop
4400 if Scope (S) = Pref_Encl_Typ then
4401 E := First_Entity (Pref_Encl_Typ);
4402 while Present (E)
4403 and then E /= First_Private_Entity (Pref_Encl_Typ)
4404 loop
4405 if E = S then
4406 return True;
4407 end if;
4408
4409 Next_Entity (E);
4410 end loop;
4411 end if;
4412
4413 S := Scope (S);
4414 end loop;
4415
4416 return False;
4417 end Is_Public_Operation;
4418
4419 -- Start of processing for Check_Unprotected_Access
4420
4421 begin
4422 if Nkind (Expr) = N_Attribute_Reference
4423 and then Attribute_Name (Expr) = Name_Unchecked_Access
4424 then
4425 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
4426 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
4427
4428 -- Check whether we are trying to export a protected component to a
4429 -- context with an equal or lower access level.
4430
4431 if Present (Pref_Encl_Typ)
4432 and then No (Cont_Encl_Typ)
4433 and then Is_Public_Operation
4434 and then Scope_Depth (Pref_Encl_Typ) >=
4435 Object_Access_Level (Context)
4436 then
4437 Error_Msg_N
4438 ("??possible unprotected access to protected data", Expr);
4439 end if;
4440 end if;
4441 end Check_Unprotected_Access;
4442
4443 ------------------------------
4444 -- Check_Unused_Body_States --
4445 ------------------------------
4446
4447 procedure Check_Unused_Body_States (Body_Id : Entity_Id) is
4448 procedure Process_Refinement_Clause
4449 (Clause : Node_Id;
4450 States : Elist_Id);
4451 -- Inspect all constituents of refinement clause Clause and remove any
4452 -- matches from body state list States.
4453
4454 procedure Report_Unused_Body_States (States : Elist_Id);
4455 -- Emit errors for each abstract state or object found in list States
4456
4457 -------------------------------
4458 -- Process_Refinement_Clause --
4459 -------------------------------
4460
4461 procedure Process_Refinement_Clause
4462 (Clause : Node_Id;
4463 States : Elist_Id)
4464 is
4465 procedure Process_Constituent (Constit : Node_Id);
4466 -- Remove constituent Constit from body state list States
4467
4468 -------------------------
4469 -- Process_Constituent --
4470 -------------------------
4471
4472 procedure Process_Constituent (Constit : Node_Id) is
4473 Constit_Id : Entity_Id;
4474
4475 begin
4476 -- Guard against illegal constituents. Only abstract states and
4477 -- objects can appear on the right hand side of a refinement.
4478
4479 if Is_Entity_Name (Constit) then
4480 Constit_Id := Entity_Of (Constit);
4481
4482 if Present (Constit_Id)
4483 and then Ekind_In (Constit_Id, E_Abstract_State,
4484 E_Constant,
4485 E_Variable)
4486 then
4487 Remove (States, Constit_Id);
4488 end if;
4489 end if;
4490 end Process_Constituent;
4491
4492 -- Local variables
4493
4494 Constit : Node_Id;
4495
4496 -- Start of processing for Process_Refinement_Clause
4497
4498 begin
4499 if Nkind (Clause) = N_Component_Association then
4500 Constit := Expression (Clause);
4501
4502 -- Multiple constituents appear as an aggregate
4503
4504 if Nkind (Constit) = N_Aggregate then
4505 Constit := First (Expressions (Constit));
4506 while Present (Constit) loop
4507 Process_Constituent (Constit);
4508 Next (Constit);
4509 end loop;
4510
4511 -- Various forms of a single constituent
4512
4513 else
4514 Process_Constituent (Constit);
4515 end if;
4516 end if;
4517 end Process_Refinement_Clause;
4518
4519 -------------------------------
4520 -- Report_Unused_Body_States --
4521 -------------------------------
4522
4523 procedure Report_Unused_Body_States (States : Elist_Id) is
4524 Posted : Boolean := False;
4525 State_Elmt : Elmt_Id;
4526 State_Id : Entity_Id;
4527
4528 begin
4529 if Present (States) then
4530 State_Elmt := First_Elmt (States);
4531 while Present (State_Elmt) loop
4532 State_Id := Node (State_Elmt);
4533
4534 -- Constants are part of the hidden state of a package, but the
4535 -- compiler cannot determine whether they have variable input
4536 -- (SPARK RM 7.1.1(2)) and cannot classify them properly as a
4537 -- hidden state. Do not emit an error when a constant does not
4538 -- participate in a state refinement, even though it acts as a
4539 -- hidden state.
4540
4541 if Ekind (State_Id) = E_Constant then
4542 null;
4543
4544 -- Generate an error message of the form:
4545
4546 -- body of package ... has unused hidden states
4547 -- abstract state ... defined at ...
4548 -- variable ... defined at ...
4549
4550 else
4551 if not Posted then
4552 Posted := True;
4553 SPARK_Msg_N
4554 ("body of package & has unused hidden states", Body_Id);
4555 end if;
4556
4557 Error_Msg_Sloc := Sloc (State_Id);
4558
4559 if Ekind (State_Id) = E_Abstract_State then
4560 SPARK_Msg_NE
4561 ("\abstract state & defined #", Body_Id, State_Id);
4562
4563 else
4564 SPARK_Msg_NE ("\variable & defined #", Body_Id, State_Id);
4565 end if;
4566 end if;
4567
4568 Next_Elmt (State_Elmt);
4569 end loop;
4570 end if;
4571 end Report_Unused_Body_States;
4572
4573 -- Local variables
4574
4575 Prag : constant Node_Id := Get_Pragma (Body_Id, Pragma_Refined_State);
4576 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
4577 Clause : Node_Id;
4578 States : Elist_Id;
4579
4580 -- Start of processing for Check_Unused_Body_States
4581
4582 begin
4583 -- Inspect the clauses of pragma Refined_State and determine whether all
4584 -- visible states declared within the package body participate in the
4585 -- refinement.
4586
4587 if Present (Prag) then
4588 Clause := Expression (Get_Argument (Prag, Spec_Id));
4589 States := Collect_Body_States (Body_Id);
4590
4591 -- Multiple non-null state refinements appear as an aggregate
4592
4593 if Nkind (Clause) = N_Aggregate then
4594 Clause := First (Component_Associations (Clause));
4595 while Present (Clause) loop
4596 Process_Refinement_Clause (Clause, States);
4597 Next (Clause);
4598 end loop;
4599
4600 -- Various forms of a single state refinement
4601
4602 else
4603 Process_Refinement_Clause (Clause, States);
4604 end if;
4605
4606 -- Ensure that all abstract states and objects declared in the
4607 -- package body state space are utilized as constituents.
4608
4609 Report_Unused_Body_States (States);
4610 end if;
4611 end Check_Unused_Body_States;
4612
4613 -----------------
4614 -- Choice_List --
4615 -----------------
4616
4617 function Choice_List (N : Node_Id) return List_Id is
4618 begin
4619 if Nkind (N) = N_Iterated_Component_Association then
4620 return Discrete_Choices (N);
4621 else
4622 return Choices (N);
4623 end if;
4624 end Choice_List;
4625
4626 -------------------------
4627 -- Collect_Body_States --
4628 -------------------------
4629
4630 function Collect_Body_States (Body_Id : Entity_Id) return Elist_Id is
4631 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean;
4632 -- Determine whether object Obj_Id is a suitable visible state of a
4633 -- package body.
4634
4635 procedure Collect_Visible_States
4636 (Pack_Id : Entity_Id;
4637 States : in out Elist_Id);
4638 -- Gather the entities of all abstract states and objects declared in
4639 -- the visible state space of package Pack_Id.
4640
4641 ----------------------------
4642 -- Collect_Visible_States --
4643 ----------------------------
4644
4645 procedure Collect_Visible_States
4646 (Pack_Id : Entity_Id;
4647 States : in out Elist_Id)
4648 is
4649 Item_Id : Entity_Id;
4650
4651 begin
4652 -- Traverse the entity chain of the package and inspect all visible
4653 -- items.
4654
4655 Item_Id := First_Entity (Pack_Id);
4656 while Present (Item_Id) and then not In_Private_Part (Item_Id) loop
4657
4658 -- Do not consider internally generated items as those cannot be
4659 -- named and participate in refinement.
4660
4661 if not Comes_From_Source (Item_Id) then
4662 null;
4663
4664 elsif Ekind (Item_Id) = E_Abstract_State then
4665 Append_New_Elmt (Item_Id, States);
4666
4667 elsif Ekind_In (Item_Id, E_Constant, E_Variable)
4668 and then Is_Visible_Object (Item_Id)
4669 then
4670 Append_New_Elmt (Item_Id, States);
4671
4672 -- Recursively gather the visible states of a nested package
4673
4674 elsif Ekind (Item_Id) = E_Package then
4675 Collect_Visible_States (Item_Id, States);
4676 end if;
4677
4678 Next_Entity (Item_Id);
4679 end loop;
4680 end Collect_Visible_States;
4681
4682 -----------------------
4683 -- Is_Visible_Object --
4684 -----------------------
4685
4686 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean is
4687 begin
4688 -- Objects that map generic formals to their actuals are not visible
4689 -- from outside the generic instantiation.
4690
4691 if Present (Corresponding_Generic_Association
4692 (Declaration_Node (Obj_Id)))
4693 then
4694 return False;
4695
4696 -- Constituents of a single protected/task type act as components of
4697 -- the type and are not visible from outside the type.
4698
4699 elsif Ekind (Obj_Id) = E_Variable
4700 and then Present (Encapsulating_State (Obj_Id))
4701 and then Is_Single_Concurrent_Object (Encapsulating_State (Obj_Id))
4702 then
4703 return False;
4704
4705 else
4706 return True;
4707 end if;
4708 end Is_Visible_Object;
4709
4710 -- Local variables
4711
4712 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
4713 Decl : Node_Id;
4714 Item_Id : Entity_Id;
4715 States : Elist_Id := No_Elist;
4716
4717 -- Start of processing for Collect_Body_States
4718
4719 begin
4720 -- Inspect the declarations of the body looking for source objects,
4721 -- packages and package instantiations. Note that even though this
4722 -- processing is very similar to Collect_Visible_States, a package
4723 -- body does not have a First/Next_Entity list.
4724
4725 Decl := First (Declarations (Body_Decl));
4726 while Present (Decl) loop
4727
4728 -- Capture source objects as internally generated temporaries cannot
4729 -- be named and participate in refinement.
4730
4731 if Nkind (Decl) = N_Object_Declaration then
4732 Item_Id := Defining_Entity (Decl);
4733
4734 if Comes_From_Source (Item_Id)
4735 and then Is_Visible_Object (Item_Id)
4736 then
4737 Append_New_Elmt (Item_Id, States);
4738 end if;
4739
4740 -- Capture the visible abstract states and objects of a source
4741 -- package [instantiation].
4742
4743 elsif Nkind (Decl) = N_Package_Declaration then
4744 Item_Id := Defining_Entity (Decl);
4745
4746 if Comes_From_Source (Item_Id) then
4747 Collect_Visible_States (Item_Id, States);
4748 end if;
4749 end if;
4750
4751 Next (Decl);
4752 end loop;
4753
4754 return States;
4755 end Collect_Body_States;
4756
4757 ------------------------
4758 -- Collect_Interfaces --
4759 ------------------------
4760
4761 procedure Collect_Interfaces
4762 (T : Entity_Id;
4763 Ifaces_List : out Elist_Id;
4764 Exclude_Parents : Boolean := False;
4765 Use_Full_View : Boolean := True)
4766 is
4767 procedure Collect (Typ : Entity_Id);
4768 -- Subsidiary subprogram used to traverse the whole list
4769 -- of directly and indirectly implemented interfaces
4770
4771 -------------
4772 -- Collect --
4773 -------------
4774
4775 procedure Collect (Typ : Entity_Id) is
4776 Ancestor : Entity_Id;
4777 Full_T : Entity_Id;
4778 Id : Node_Id;
4779 Iface : Entity_Id;
4780
4781 begin
4782 Full_T := Typ;
4783
4784 -- Handle private types and subtypes
4785
4786 if Use_Full_View
4787 and then Is_Private_Type (Typ)
4788 and then Present (Full_View (Typ))
4789 then
4790 Full_T := Full_View (Typ);
4791
4792 if Ekind (Full_T) = E_Record_Subtype then
4793 Full_T := Etype (Typ);
4794
4795 if Present (Full_View (Full_T)) then
4796 Full_T := Full_View (Full_T);
4797 end if;
4798 end if;
4799 end if;
4800
4801 -- Include the ancestor if we are generating the whole list of
4802 -- abstract interfaces.
4803
4804 if Etype (Full_T) /= Typ
4805
4806 -- Protect the frontend against wrong sources. For example:
4807
4808 -- package P is
4809 -- type A is tagged null record;
4810 -- type B is new A with private;
4811 -- type C is new A with private;
4812 -- private
4813 -- type B is new C with null record;
4814 -- type C is new B with null record;
4815 -- end P;
4816
4817 and then Etype (Full_T) /= T
4818 then
4819 Ancestor := Etype (Full_T);
4820 Collect (Ancestor);
4821
4822 if Is_Interface (Ancestor) and then not Exclude_Parents then
4823 Append_Unique_Elmt (Ancestor, Ifaces_List);
4824 end if;
4825 end if;
4826
4827 -- Traverse the graph of ancestor interfaces
4828
4829 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
4830 Id := First (Abstract_Interface_List (Full_T));
4831 while Present (Id) loop
4832 Iface := Etype (Id);
4833
4834 -- Protect against wrong uses. For example:
4835 -- type I is interface;
4836 -- type O is tagged null record;
4837 -- type Wrong is new I and O with null record; -- ERROR
4838
4839 if Is_Interface (Iface) then
4840 if Exclude_Parents
4841 and then Etype (T) /= T
4842 and then Interface_Present_In_Ancestor (Etype (T), Iface)
4843 then
4844 null;
4845 else
4846 Collect (Iface);
4847 Append_Unique_Elmt (Iface, Ifaces_List);
4848 end if;
4849 end if;
4850
4851 Next (Id);
4852 end loop;
4853 end if;
4854 end Collect;
4855
4856 -- Start of processing for Collect_Interfaces
4857
4858 begin
4859 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
4860 Ifaces_List := New_Elmt_List;
4861 Collect (T);
4862 end Collect_Interfaces;
4863
4864 ----------------------------------
4865 -- Collect_Interface_Components --
4866 ----------------------------------
4867
4868 procedure Collect_Interface_Components
4869 (Tagged_Type : Entity_Id;
4870 Components_List : out Elist_Id)
4871 is
4872 procedure Collect (Typ : Entity_Id);
4873 -- Subsidiary subprogram used to climb to the parents
4874
4875 -------------
4876 -- Collect --
4877 -------------
4878
4879 procedure Collect (Typ : Entity_Id) is
4880 Tag_Comp : Entity_Id;
4881 Parent_Typ : Entity_Id;
4882
4883 begin
4884 -- Handle private types
4885
4886 if Present (Full_View (Etype (Typ))) then
4887 Parent_Typ := Full_View (Etype (Typ));
4888 else
4889 Parent_Typ := Etype (Typ);
4890 end if;
4891
4892 if Parent_Typ /= Typ
4893
4894 -- Protect the frontend against wrong sources. For example:
4895
4896 -- package P is
4897 -- type A is tagged null record;
4898 -- type B is new A with private;
4899 -- type C is new A with private;
4900 -- private
4901 -- type B is new C with null record;
4902 -- type C is new B with null record;
4903 -- end P;
4904
4905 and then Parent_Typ /= Tagged_Type
4906 then
4907 Collect (Parent_Typ);
4908 end if;
4909
4910 -- Collect the components containing tags of secondary dispatch
4911 -- tables.
4912
4913 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
4914 while Present (Tag_Comp) loop
4915 pragma Assert (Present (Related_Type (Tag_Comp)));
4916 Append_Elmt (Tag_Comp, Components_List);
4917
4918 Tag_Comp := Next_Tag_Component (Tag_Comp);
4919 end loop;
4920 end Collect;
4921
4922 -- Start of processing for Collect_Interface_Components
4923
4924 begin
4925 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
4926 and then Is_Tagged_Type (Tagged_Type));
4927
4928 Components_List := New_Elmt_List;
4929 Collect (Tagged_Type);
4930 end Collect_Interface_Components;
4931
4932 -----------------------------
4933 -- Collect_Interfaces_Info --
4934 -----------------------------
4935
4936 procedure Collect_Interfaces_Info
4937 (T : Entity_Id;
4938 Ifaces_List : out Elist_Id;
4939 Components_List : out Elist_Id;
4940 Tags_List : out Elist_Id)
4941 is
4942 Comps_List : Elist_Id;
4943 Comp_Elmt : Elmt_Id;
4944 Comp_Iface : Entity_Id;
4945 Iface_Elmt : Elmt_Id;
4946 Iface : Entity_Id;
4947
4948 function Search_Tag (Iface : Entity_Id) return Entity_Id;
4949 -- Search for the secondary tag associated with the interface type
4950 -- Iface that is implemented by T.
4951
4952 ----------------
4953 -- Search_Tag --
4954 ----------------
4955
4956 function Search_Tag (Iface : Entity_Id) return Entity_Id is
4957 ADT : Elmt_Id;
4958 begin
4959 if not Is_CPP_Class (T) then
4960 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
4961 else
4962 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
4963 end if;
4964
4965 while Present (ADT)
4966 and then Is_Tag (Node (ADT))
4967 and then Related_Type (Node (ADT)) /= Iface
4968 loop
4969 -- Skip secondary dispatch table referencing thunks to user
4970 -- defined primitives covered by this interface.
4971
4972 pragma Assert (Has_Suffix (Node (ADT), 'P'));
4973 Next_Elmt (ADT);
4974
4975 -- Skip secondary dispatch tables of Ada types
4976
4977 if not Is_CPP_Class (T) then
4978
4979 -- Skip secondary dispatch table referencing thunks to
4980 -- predefined primitives.
4981
4982 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
4983 Next_Elmt (ADT);
4984
4985 -- Skip secondary dispatch table referencing user-defined
4986 -- primitives covered by this interface.
4987
4988 pragma Assert (Has_Suffix (Node (ADT), 'D'));
4989 Next_Elmt (ADT);
4990
4991 -- Skip secondary dispatch table referencing predefined
4992 -- primitives.
4993
4994 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
4995 Next_Elmt (ADT);
4996 end if;
4997 end loop;
4998
4999 pragma Assert (Is_Tag (Node (ADT)));
5000 return Node (ADT);
5001 end Search_Tag;
5002
5003 -- Start of processing for Collect_Interfaces_Info
5004
5005 begin
5006 Collect_Interfaces (T, Ifaces_List);
5007 Collect_Interface_Components (T, Comps_List);
5008
5009 -- Search for the record component and tag associated with each
5010 -- interface type of T.
5011
5012 Components_List := New_Elmt_List;
5013 Tags_List := New_Elmt_List;
5014
5015 Iface_Elmt := First_Elmt (Ifaces_List);
5016 while Present (Iface_Elmt) loop
5017 Iface := Node (Iface_Elmt);
5018
5019 -- Associate the primary tag component and the primary dispatch table
5020 -- with all the interfaces that are parents of T
5021
5022 if Is_Ancestor (Iface, T, Use_Full_View => True) then
5023 Append_Elmt (First_Tag_Component (T), Components_List);
5024 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
5025
5026 -- Otherwise search for the tag component and secondary dispatch
5027 -- table of Iface
5028
5029 else
5030 Comp_Elmt := First_Elmt (Comps_List);
5031 while Present (Comp_Elmt) loop
5032 Comp_Iface := Related_Type (Node (Comp_Elmt));
5033
5034 if Comp_Iface = Iface
5035 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
5036 then
5037 Append_Elmt (Node (Comp_Elmt), Components_List);
5038 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
5039 exit;
5040 end if;
5041
5042 Next_Elmt (Comp_Elmt);
5043 end loop;
5044 pragma Assert (Present (Comp_Elmt));
5045 end if;
5046
5047 Next_Elmt (Iface_Elmt);
5048 end loop;
5049 end Collect_Interfaces_Info;
5050
5051 ---------------------
5052 -- Collect_Parents --
5053 ---------------------
5054
5055 procedure Collect_Parents
5056 (T : Entity_Id;
5057 List : out Elist_Id;
5058 Use_Full_View : Boolean := True)
5059 is
5060 Current_Typ : Entity_Id := T;
5061 Parent_Typ : Entity_Id;
5062
5063 begin
5064 List := New_Elmt_List;
5065
5066 -- No action if the if the type has no parents
5067
5068 if T = Etype (T) then
5069 return;
5070 end if;
5071
5072 loop
5073 Parent_Typ := Etype (Current_Typ);
5074
5075 if Is_Private_Type (Parent_Typ)
5076 and then Present (Full_View (Parent_Typ))
5077 and then Use_Full_View
5078 then
5079 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5080 end if;
5081
5082 Append_Elmt (Parent_Typ, List);
5083
5084 exit when Parent_Typ = Current_Typ;
5085 Current_Typ := Parent_Typ;
5086 end loop;
5087 end Collect_Parents;
5088
5089 ----------------------------------
5090 -- Collect_Primitive_Operations --
5091 ----------------------------------
5092
5093 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
5094 B_Type : constant Entity_Id := Base_Type (T);
5095
5096 function Match (E : Entity_Id) return Boolean;
5097 -- True if E's base type is B_Type, or E is of an anonymous access type
5098 -- and the base type of its designated type is B_Type.
5099
5100 -----------
5101 -- Match --
5102 -----------
5103
5104 function Match (E : Entity_Id) return Boolean is
5105 Etyp : Entity_Id := Etype (E);
5106
5107 begin
5108 if Ekind (Etyp) = E_Anonymous_Access_Type then
5109 Etyp := Designated_Type (Etyp);
5110 end if;
5111
5112 -- In Ada 2012 a primitive operation may have a formal of an
5113 -- incomplete view of the parent type.
5114
5115 return Base_Type (Etyp) = B_Type
5116 or else
5117 (Ada_Version >= Ada_2012
5118 and then Ekind (Etyp) = E_Incomplete_Type
5119 and then Full_View (Etyp) = B_Type);
5120 end Match;
5121
5122 -- Local variables
5123
5124 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
5125 B_Scope : Entity_Id := Scope (B_Type);
5126 Op_List : Elist_Id;
5127 Eq_Prims_List : Elist_Id := No_Elist;
5128 Formal : Entity_Id;
5129 Is_Prim : Boolean;
5130 Is_Type_In_Pkg : Boolean;
5131 Formal_Derived : Boolean := False;
5132 Id : Entity_Id;
5133
5134 -- Start of processing for Collect_Primitive_Operations
5135
5136 begin
5137 -- For tagged types, the primitive operations are collected as they
5138 -- are declared, and held in an explicit list which is simply returned.
5139
5140 if Is_Tagged_Type (B_Type) then
5141 return Primitive_Operations (B_Type);
5142
5143 -- An untagged generic type that is a derived type inherits the
5144 -- primitive operations of its parent type. Other formal types only
5145 -- have predefined operators, which are not explicitly represented.
5146
5147 elsif Is_Generic_Type (B_Type) then
5148 if Nkind (B_Decl) = N_Formal_Type_Declaration
5149 and then Nkind (Formal_Type_Definition (B_Decl)) =
5150 N_Formal_Derived_Type_Definition
5151 then
5152 Formal_Derived := True;
5153 else
5154 return New_Elmt_List;
5155 end if;
5156 end if;
5157
5158 Op_List := New_Elmt_List;
5159
5160 if B_Scope = Standard_Standard then
5161 if B_Type = Standard_String then
5162 Append_Elmt (Standard_Op_Concat, Op_List);
5163
5164 elsif B_Type = Standard_Wide_String then
5165 Append_Elmt (Standard_Op_Concatw, Op_List);
5166
5167 else
5168 null;
5169 end if;
5170
5171 -- Locate the primitive subprograms of the type
5172
5173 else
5174 -- The primitive operations appear after the base type, except if the
5175 -- derivation happens within the private part of B_Scope and the type
5176 -- is a private type, in which case both the type and some primitive
5177 -- operations may appear before the base type, and the list of
5178 -- candidates starts after the type.
5179
5180 if In_Open_Scopes (B_Scope)
5181 and then Scope (T) = B_Scope
5182 and then In_Private_Part (B_Scope)
5183 then
5184 Id := Next_Entity (T);
5185
5186 -- In Ada 2012, If the type has an incomplete partial view, there may
5187 -- be primitive operations declared before the full view, so we need
5188 -- to start scanning from the incomplete view, which is earlier on
5189 -- the entity chain.
5190
5191 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
5192 and then Present (Incomplete_View (Parent (B_Type)))
5193 then
5194 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
5195
5196 -- If T is a derived from a type with an incomplete view declared
5197 -- elsewhere, that incomplete view is irrelevant, we want the
5198 -- operations in the scope of T.
5199
5200 if Scope (Id) /= Scope (B_Type) then
5201 Id := Next_Entity (B_Type);
5202 end if;
5203
5204 else
5205 Id := Next_Entity (B_Type);
5206 end if;
5207
5208 -- Set flag if this is a type in a package spec
5209
5210 Is_Type_In_Pkg :=
5211 Is_Package_Or_Generic_Package (B_Scope)
5212 and then
5213 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
5214 N_Package_Body;
5215
5216 while Present (Id) loop
5217
5218 -- Test whether the result type or any of the parameter types of
5219 -- each subprogram following the type match that type when the
5220 -- type is declared in a package spec, is a derived type, or the
5221 -- subprogram is marked as primitive. (The Is_Primitive test is
5222 -- needed to find primitives of nonderived types in declarative
5223 -- parts that happen to override the predefined "=" operator.)
5224
5225 -- Note that generic formal subprograms are not considered to be
5226 -- primitive operations and thus are never inherited.
5227
5228 if Is_Overloadable (Id)
5229 and then (Is_Type_In_Pkg
5230 or else Is_Derived_Type (B_Type)
5231 or else Is_Primitive (Id))
5232 and then Nkind (Parent (Parent (Id)))
5233 not in N_Formal_Subprogram_Declaration
5234 then
5235 Is_Prim := False;
5236
5237 if Match (Id) then
5238 Is_Prim := True;
5239
5240 else
5241 Formal := First_Formal (Id);
5242 while Present (Formal) loop
5243 if Match (Formal) then
5244 Is_Prim := True;
5245 exit;
5246 end if;
5247
5248 Next_Formal (Formal);
5249 end loop;
5250 end if;
5251
5252 -- For a formal derived type, the only primitives are the ones
5253 -- inherited from the parent type. Operations appearing in the
5254 -- package declaration are not primitive for it.
5255
5256 if Is_Prim
5257 and then (not Formal_Derived or else Present (Alias (Id)))
5258 then
5259 -- In the special case of an equality operator aliased to
5260 -- an overriding dispatching equality belonging to the same
5261 -- type, we don't include it in the list of primitives.
5262 -- This avoids inheriting multiple equality operators when
5263 -- deriving from untagged private types whose full type is
5264 -- tagged, which can otherwise cause ambiguities. Note that
5265 -- this should only happen for this kind of untagged parent
5266 -- type, since normally dispatching operations are inherited
5267 -- using the type's Primitive_Operations list.
5268
5269 if Chars (Id) = Name_Op_Eq
5270 and then Is_Dispatching_Operation (Id)
5271 and then Present (Alias (Id))
5272 and then Present (Overridden_Operation (Alias (Id)))
5273 and then Base_Type (Etype (First_Entity (Id))) =
5274 Base_Type (Etype (First_Entity (Alias (Id))))
5275 then
5276 null;
5277
5278 -- Include the subprogram in the list of primitives
5279
5280 else
5281 Append_Elmt (Id, Op_List);
5282
5283 -- Save collected equality primitives for later filtering
5284 -- (if we are processing a private type for which we can
5285 -- collect several candidates).
5286
5287 if Inherits_From_Tagged_Full_View (T)
5288 and then Chars (Id) = Name_Op_Eq
5289 and then Etype (First_Formal (Id)) =
5290 Etype (Next_Formal (First_Formal (Id)))
5291 then
5292 if No (Eq_Prims_List) then
5293 Eq_Prims_List := New_Elmt_List;
5294 end if;
5295
5296 Append_Elmt (Id, Eq_Prims_List);
5297 end if;
5298 end if;
5299 end if;
5300 end if;
5301
5302 Next_Entity (Id);
5303
5304 -- For a type declared in System, some of its operations may
5305 -- appear in the target-specific extension to System.
5306
5307 if No (Id)
5308 and then B_Scope = RTU_Entity (System)
5309 and then Present_System_Aux
5310 then
5311 B_Scope := System_Aux_Id;
5312 Id := First_Entity (System_Aux_Id);
5313 end if;
5314 end loop;
5315
5316 -- Filter collected equality primitives
5317
5318 if Inherits_From_Tagged_Full_View (T)
5319 and then Present (Eq_Prims_List)
5320 then
5321 declare
5322 First : constant Elmt_Id := First_Elmt (Eq_Prims_List);
5323 Second : Elmt_Id;
5324
5325 begin
5326 pragma Assert (No (Next_Elmt (First))
5327 or else No (Next_Elmt (Next_Elmt (First))));
5328
5329 -- No action needed if we have collected a single equality
5330 -- primitive
5331
5332 if Present (Next_Elmt (First)) then
5333 Second := Next_Elmt (First);
5334
5335 if Is_Dispatching_Operation
5336 (Ultimate_Alias (Node (First)))
5337 then
5338 Remove (Op_List, Node (First));
5339
5340 elsif Is_Dispatching_Operation
5341 (Ultimate_Alias (Node (Second)))
5342 then
5343 Remove (Op_List, Node (Second));
5344
5345 else
5346 pragma Assert (False);
5347 raise Program_Error;
5348 end if;
5349 end if;
5350 end;
5351 end if;
5352 end if;
5353
5354 return Op_List;
5355 end Collect_Primitive_Operations;
5356
5357 -----------------------------------
5358 -- Compile_Time_Constraint_Error --
5359 -----------------------------------
5360
5361 function Compile_Time_Constraint_Error
5362 (N : Node_Id;
5363 Msg : String;
5364 Ent : Entity_Id := Empty;
5365 Loc : Source_Ptr := No_Location;
5366 Warn : Boolean := False) return Node_Id
5367 is
5368 Msgc : String (1 .. Msg'Length + 3);
5369 -- Copy of message, with room for possible ?? or << and ! at end
5370
5371 Msgl : Natural;
5372 Wmsg : Boolean;
5373 Eloc : Source_Ptr;
5374
5375 -- Start of processing for Compile_Time_Constraint_Error
5376
5377 begin
5378 -- If this is a warning, convert it into an error if we are in code
5379 -- subject to SPARK_Mode being set On, unless Warn is True to force a
5380 -- warning. The rationale is that a compile-time constraint error should
5381 -- lead to an error instead of a warning when SPARK_Mode is On, but in
5382 -- a few cases we prefer to issue a warning and generate both a suitable
5383 -- run-time error in GNAT and a suitable check message in GNATprove.
5384 -- Those cases are those that likely correspond to deactivated SPARK
5385 -- code, so that this kind of code can be compiled and analyzed instead
5386 -- of being rejected.
5387
5388 Error_Msg_Warn := Warn or SPARK_Mode /= On;
5389
5390 -- A static constraint error in an instance body is not a fatal error.
5391 -- we choose to inhibit the message altogether, because there is no
5392 -- obvious node (for now) on which to post it. On the other hand the
5393 -- offending node must be replaced with a constraint_error in any case.
5394
5395 -- No messages are generated if we already posted an error on this node
5396
5397 if not Error_Posted (N) then
5398 if Loc /= No_Location then
5399 Eloc := Loc;
5400 else
5401 Eloc := Sloc (N);
5402 end if;
5403
5404 -- Copy message to Msgc, converting any ? in the message into <
5405 -- instead, so that we have an error in GNATprove mode.
5406
5407 Msgl := Msg'Length;
5408
5409 for J in 1 .. Msgl loop
5410 if Msg (J) = '?' and then (J = 1 or else Msg (J - 1) /= ''') then
5411 Msgc (J) := '<';
5412 else
5413 Msgc (J) := Msg (J);
5414 end if;
5415 end loop;
5416
5417 -- Message is a warning, even in Ada 95 case
5418
5419 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
5420 Wmsg := True;
5421
5422 -- In Ada 83, all messages are warnings. In the private part and the
5423 -- body of an instance, constraint_checks are only warnings. We also
5424 -- make this a warning if the Warn parameter is set.
5425
5426 elsif Warn
5427 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
5428 or else In_Instance_Not_Visible
5429 then
5430 Msgl := Msgl + 1;
5431 Msgc (Msgl) := '<';
5432 Msgl := Msgl + 1;
5433 Msgc (Msgl) := '<';
5434 Wmsg := True;
5435
5436 -- Otherwise we have a real error message (Ada 95 static case) and we
5437 -- make this an unconditional message. Note that in the warning case
5438 -- we do not make the message unconditional, it seems reasonable to
5439 -- delete messages like this (about exceptions that will be raised)
5440 -- in dead code.
5441
5442 else
5443 Wmsg := False;
5444 Msgl := Msgl + 1;
5445 Msgc (Msgl) := '!';
5446 end if;
5447
5448 -- One more test, skip the warning if the related expression is
5449 -- statically unevaluated, since we don't want to warn about what
5450 -- will happen when something is evaluated if it never will be
5451 -- evaluated.
5452
5453 if not Is_Statically_Unevaluated (N) then
5454 if Present (Ent) then
5455 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
5456 else
5457 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
5458 end if;
5459
5460 if Wmsg then
5461
5462 -- Check whether the context is an Init_Proc
5463
5464 if Inside_Init_Proc then
5465 declare
5466 Conc_Typ : constant Entity_Id :=
5467 Corresponding_Concurrent_Type
5468 (Entity (Parameter_Type (First
5469 (Parameter_Specifications
5470 (Parent (Current_Scope))))));
5471
5472 begin
5473 -- Don't complain if the corresponding concurrent type
5474 -- doesn't come from source (i.e. a single task/protected
5475 -- object).
5476
5477 if Present (Conc_Typ)
5478 and then not Comes_From_Source (Conc_Typ)
5479 then
5480 Error_Msg_NEL
5481 ("\& [<<", N, Standard_Constraint_Error, Eloc);
5482
5483 else
5484 if GNATprove_Mode then
5485 Error_Msg_NEL
5486 ("\& would have been raised for objects of this "
5487 & "type", N, Standard_Constraint_Error, Eloc);
5488 else
5489 Error_Msg_NEL
5490 ("\& will be raised for objects of this type??",
5491 N, Standard_Constraint_Error, Eloc);
5492 end if;
5493 end if;
5494 end;
5495
5496 else
5497 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
5498 end if;
5499
5500 else
5501 Error_Msg ("\static expression fails Constraint_Check", Eloc);
5502 Set_Error_Posted (N);
5503 end if;
5504 end if;
5505 end if;
5506
5507 return N;
5508 end Compile_Time_Constraint_Error;
5509
5510 -----------------------
5511 -- Conditional_Delay --
5512 -----------------------
5513
5514 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
5515 begin
5516 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
5517 Set_Has_Delayed_Freeze (New_Ent);
5518 end if;
5519 end Conditional_Delay;
5520
5521 -------------------------
5522 -- Copy_Component_List --
5523 -------------------------
5524
5525 function Copy_Component_List
5526 (R_Typ : Entity_Id;
5527 Loc : Source_Ptr) return List_Id
5528 is
5529 Comp : Node_Id;
5530 Comps : constant List_Id := New_List;
5531
5532 begin
5533 Comp := First_Component (Underlying_Type (R_Typ));
5534 while Present (Comp) loop
5535 if Comes_From_Source (Comp) then
5536 declare
5537 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
5538 begin
5539 Append_To (Comps,
5540 Make_Component_Declaration (Loc,
5541 Defining_Identifier =>
5542 Make_Defining_Identifier (Loc, Chars (Comp)),
5543 Component_Definition =>
5544 New_Copy_Tree
5545 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
5546 end;
5547 end if;
5548
5549 Next_Component (Comp);
5550 end loop;
5551
5552 return Comps;
5553 end Copy_Component_List;
5554
5555 -------------------------
5556 -- Copy_Parameter_List --
5557 -------------------------
5558
5559 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
5560 Loc : constant Source_Ptr := Sloc (Subp_Id);
5561 Plist : List_Id;
5562 Formal : Entity_Id;
5563
5564 begin
5565 if No (First_Formal (Subp_Id)) then
5566 return No_List;
5567 else
5568 Plist := New_List;
5569 Formal := First_Formal (Subp_Id);
5570 while Present (Formal) loop
5571 Append_To (Plist,
5572 Make_Parameter_Specification (Loc,
5573 Defining_Identifier =>
5574 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
5575 In_Present => In_Present (Parent (Formal)),
5576 Out_Present => Out_Present (Parent (Formal)),
5577 Parameter_Type =>
5578 New_Occurrence_Of (Etype (Formal), Loc),
5579 Expression =>
5580 New_Copy_Tree (Expression (Parent (Formal)))));
5581
5582 Next_Formal (Formal);
5583 end loop;
5584 end if;
5585
5586 return Plist;
5587 end Copy_Parameter_List;
5588
5589 ----------------------------
5590 -- Copy_SPARK_Mode_Aspect --
5591 ----------------------------
5592
5593 procedure Copy_SPARK_Mode_Aspect (From : Node_Id; To : Node_Id) is
5594 pragma Assert (not Has_Aspects (To));
5595 Asp : Node_Id;
5596
5597 begin
5598 if Has_Aspects (From) then
5599 Asp := Find_Aspect (Defining_Entity (From), Aspect_SPARK_Mode);
5600
5601 if Present (Asp) then
5602 Set_Aspect_Specifications (To, New_List (New_Copy_Tree (Asp)));
5603 Set_Has_Aspects (To, True);
5604 end if;
5605 end if;
5606 end Copy_SPARK_Mode_Aspect;
5607
5608 --------------------------
5609 -- Copy_Subprogram_Spec --
5610 --------------------------
5611
5612 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
5613 Def_Id : Node_Id;
5614 Formal_Spec : Node_Id;
5615 Result : Node_Id;
5616
5617 begin
5618 -- The structure of the original tree must be replicated without any
5619 -- alterations. Use New_Copy_Tree for this purpose.
5620
5621 Result := New_Copy_Tree (Spec);
5622
5623 -- However, the spec of a null procedure carries the corresponding null
5624 -- statement of the body (created by the parser), and this cannot be
5625 -- shared with the new subprogram spec.
5626
5627 if Nkind (Result) = N_Procedure_Specification then
5628 Set_Null_Statement (Result, Empty);
5629 end if;
5630
5631 -- Create a new entity for the defining unit name
5632
5633 Def_Id := Defining_Unit_Name (Result);
5634 Set_Defining_Unit_Name (Result,
5635 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5636
5637 -- Create new entities for the formal parameters
5638
5639 if Present (Parameter_Specifications (Result)) then
5640 Formal_Spec := First (Parameter_Specifications (Result));
5641 while Present (Formal_Spec) loop
5642 Def_Id := Defining_Identifier (Formal_Spec);
5643 Set_Defining_Identifier (Formal_Spec,
5644 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5645
5646 Next (Formal_Spec);
5647 end loop;
5648 end if;
5649
5650 return Result;
5651 end Copy_Subprogram_Spec;
5652
5653 --------------------------------
5654 -- Corresponding_Generic_Type --
5655 --------------------------------
5656
5657 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
5658 Inst : Entity_Id;
5659 Gen : Entity_Id;
5660 Typ : Entity_Id;
5661
5662 begin
5663 if not Is_Generic_Actual_Type (T) then
5664 return Any_Type;
5665
5666 -- If the actual is the actual of an enclosing instance, resolution
5667 -- was correct in the generic.
5668
5669 elsif Nkind (Parent (T)) = N_Subtype_Declaration
5670 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
5671 and then
5672 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
5673 then
5674 return Any_Type;
5675
5676 else
5677 Inst := Scope (T);
5678
5679 if Is_Wrapper_Package (Inst) then
5680 Inst := Related_Instance (Inst);
5681 end if;
5682
5683 Gen :=
5684 Generic_Parent
5685 (Specification (Unit_Declaration_Node (Inst)));
5686
5687 -- Generic actual has the same name as the corresponding formal
5688
5689 Typ := First_Entity (Gen);
5690 while Present (Typ) loop
5691 if Chars (Typ) = Chars (T) then
5692 return Typ;
5693 end if;
5694
5695 Next_Entity (Typ);
5696 end loop;
5697
5698 return Any_Type;
5699 end if;
5700 end Corresponding_Generic_Type;
5701
5702 --------------------
5703 -- Current_Entity --
5704 --------------------
5705
5706 -- The currently visible definition for a given identifier is the
5707 -- one most chained at the start of the visibility chain, i.e. the
5708 -- one that is referenced by the Node_Id value of the name of the
5709 -- given identifier.
5710
5711 function Current_Entity (N : Node_Id) return Entity_Id is
5712 begin
5713 return Get_Name_Entity_Id (Chars (N));
5714 end Current_Entity;
5715
5716 -----------------------------
5717 -- Current_Entity_In_Scope --
5718 -----------------------------
5719
5720 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
5721 E : Entity_Id;
5722 CS : constant Entity_Id := Current_Scope;
5723
5724 Transient_Case : constant Boolean := Scope_Is_Transient;
5725
5726 begin
5727 E := Get_Name_Entity_Id (Chars (N));
5728 while Present (E)
5729 and then Scope (E) /= CS
5730 and then (not Transient_Case or else Scope (E) /= Scope (CS))
5731 loop
5732 E := Homonym (E);
5733 end loop;
5734
5735 return E;
5736 end Current_Entity_In_Scope;
5737
5738 -------------------
5739 -- Current_Scope --
5740 -------------------
5741
5742 function Current_Scope return Entity_Id is
5743 begin
5744 if Scope_Stack.Last = -1 then
5745 return Standard_Standard;
5746 else
5747 declare
5748 C : constant Entity_Id :=
5749 Scope_Stack.Table (Scope_Stack.Last).Entity;
5750 begin
5751 if Present (C) then
5752 return C;
5753 else
5754 return Standard_Standard;
5755 end if;
5756 end;
5757 end if;
5758 end Current_Scope;
5759
5760 ----------------------------
5761 -- Current_Scope_No_Loops --
5762 ----------------------------
5763
5764 function Current_Scope_No_Loops return Entity_Id is
5765 S : Entity_Id;
5766
5767 begin
5768 -- Examine the scope stack starting from the current scope and skip any
5769 -- internally generated loops.
5770
5771 S := Current_Scope;
5772 while Present (S) and then S /= Standard_Standard loop
5773 if Ekind (S) = E_Loop and then not Comes_From_Source (S) then
5774 S := Scope (S);
5775 else
5776 exit;
5777 end if;
5778 end loop;
5779
5780 return S;
5781 end Current_Scope_No_Loops;
5782
5783 ------------------------
5784 -- Current_Subprogram --
5785 ------------------------
5786
5787 function Current_Subprogram return Entity_Id is
5788 Scop : constant Entity_Id := Current_Scope;
5789 begin
5790 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
5791 return Scop;
5792 else
5793 return Enclosing_Subprogram (Scop);
5794 end if;
5795 end Current_Subprogram;
5796
5797 ----------------------------------
5798 -- Deepest_Type_Access_Level --
5799 ----------------------------------
5800
5801 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
5802 begin
5803 if Ekind (Typ) = E_Anonymous_Access_Type
5804 and then not Is_Local_Anonymous_Access (Typ)
5805 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
5806 then
5807 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
5808 -- access type.
5809
5810 return
5811 Scope_Depth (Enclosing_Dynamic_Scope
5812 (Defining_Identifier
5813 (Associated_Node_For_Itype (Typ))));
5814
5815 -- For generic formal type, return Int'Last (infinite).
5816 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
5817
5818 elsif Is_Generic_Type (Root_Type (Typ)) then
5819 return UI_From_Int (Int'Last);
5820
5821 else
5822 return Type_Access_Level (Typ);
5823 end if;
5824 end Deepest_Type_Access_Level;
5825
5826 ---------------------
5827 -- Defining_Entity --
5828 ---------------------
5829
5830 function Defining_Entity
5831 (N : Node_Id;
5832 Empty_On_Errors : Boolean := False;
5833 Concurrent_Subunit : Boolean := False) return Entity_Id
5834 is
5835 begin
5836 case Nkind (N) is
5837 when N_Abstract_Subprogram_Declaration
5838 | N_Expression_Function
5839 | N_Formal_Subprogram_Declaration
5840 | N_Generic_Package_Declaration
5841 | N_Generic_Subprogram_Declaration
5842 | N_Package_Declaration
5843 | N_Subprogram_Body
5844 | N_Subprogram_Body_Stub
5845 | N_Subprogram_Declaration
5846 | N_Subprogram_Renaming_Declaration
5847 =>
5848 return Defining_Entity (Specification (N));
5849
5850 when N_Component_Declaration
5851 | N_Defining_Program_Unit_Name
5852 | N_Discriminant_Specification
5853 | N_Entry_Body
5854 | N_Entry_Declaration
5855 | N_Entry_Index_Specification
5856 | N_Exception_Declaration
5857 | N_Exception_Renaming_Declaration
5858 | N_Formal_Object_Declaration
5859 | N_Formal_Package_Declaration
5860 | N_Formal_Type_Declaration
5861 | N_Full_Type_Declaration
5862 | N_Implicit_Label_Declaration
5863 | N_Incomplete_Type_Declaration
5864 | N_Iterator_Specification
5865 | N_Loop_Parameter_Specification
5866 | N_Number_Declaration
5867 | N_Object_Declaration
5868 | N_Object_Renaming_Declaration
5869 | N_Package_Body_Stub
5870 | N_Parameter_Specification
5871 | N_Private_Extension_Declaration
5872 | N_Private_Type_Declaration
5873 | N_Protected_Body
5874 | N_Protected_Body_Stub
5875 | N_Protected_Type_Declaration
5876 | N_Single_Protected_Declaration
5877 | N_Single_Task_Declaration
5878 | N_Subtype_Declaration
5879 | N_Task_Body
5880 | N_Task_Body_Stub
5881 | N_Task_Type_Declaration
5882 =>
5883 return Defining_Identifier (N);
5884
5885 when N_Subunit =>
5886 declare
5887 Bod : constant Node_Id := Proper_Body (N);
5888 Orig_Bod : constant Node_Id := Original_Node (Bod);
5889
5890 begin
5891 -- Retrieve the entity of the original protected or task body
5892 -- if requested by the caller.
5893
5894 if Concurrent_Subunit
5895 and then Nkind (Bod) = N_Null_Statement
5896 and then Nkind_In (Orig_Bod, N_Protected_Body, N_Task_Body)
5897 then
5898 return Defining_Entity (Orig_Bod);
5899 else
5900 return Defining_Entity (Bod);
5901 end if;
5902 end;
5903
5904 when N_Function_Instantiation
5905 | N_Function_Specification
5906 | N_Generic_Function_Renaming_Declaration
5907 | N_Generic_Package_Renaming_Declaration
5908 | N_Generic_Procedure_Renaming_Declaration
5909 | N_Package_Body
5910 | N_Package_Instantiation
5911 | N_Package_Renaming_Declaration
5912 | N_Package_Specification
5913 | N_Procedure_Instantiation
5914 | N_Procedure_Specification
5915 =>
5916 declare
5917 Nam : constant Node_Id := Defining_Unit_Name (N);
5918 Err : Entity_Id := Empty;
5919
5920 begin
5921 if Nkind (Nam) in N_Entity then
5922 return Nam;
5923
5924 -- For Error, make up a name and attach to declaration so we
5925 -- can continue semantic analysis.
5926
5927 elsif Nam = Error then
5928 if Empty_On_Errors then
5929 return Empty;
5930 else
5931 Err := Make_Temporary (Sloc (N), 'T');
5932 Set_Defining_Unit_Name (N, Err);
5933
5934 return Err;
5935 end if;
5936
5937 -- If not an entity, get defining identifier
5938
5939 else
5940 return Defining_Identifier (Nam);
5941 end if;
5942 end;
5943
5944 when N_Block_Statement
5945 | N_Loop_Statement
5946 =>
5947 return Entity (Identifier (N));
5948
5949 when others =>
5950 if Empty_On_Errors then
5951 return Empty;
5952 else
5953 raise Program_Error;
5954 end if;
5955 end case;
5956 end Defining_Entity;
5957
5958 --------------------------
5959 -- Denotes_Discriminant --
5960 --------------------------
5961
5962 function Denotes_Discriminant
5963 (N : Node_Id;
5964 Check_Concurrent : Boolean := False) return Boolean
5965 is
5966 E : Entity_Id;
5967
5968 begin
5969 if not Is_Entity_Name (N) or else No (Entity (N)) then
5970 return False;
5971 else
5972 E := Entity (N);
5973 end if;
5974
5975 -- If we are checking for a protected type, the discriminant may have
5976 -- been rewritten as the corresponding discriminal of the original type
5977 -- or of the corresponding concurrent record, depending on whether we
5978 -- are in the spec or body of the protected type.
5979
5980 return Ekind (E) = E_Discriminant
5981 or else
5982 (Check_Concurrent
5983 and then Ekind (E) = E_In_Parameter
5984 and then Present (Discriminal_Link (E))
5985 and then
5986 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
5987 or else
5988 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
5989 end Denotes_Discriminant;
5990
5991 -------------------------
5992 -- Denotes_Same_Object --
5993 -------------------------
5994
5995 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
5996 function Is_Renaming (N : Node_Id) return Boolean;
5997 -- Return true if N names a renaming entity
5998
5999 function Is_Valid_Renaming (N : Node_Id) return Boolean;
6000 -- For renamings, return False if the prefix of any dereference within
6001 -- the renamed object_name is a variable, or any expression within the
6002 -- renamed object_name contains references to variables or calls on
6003 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
6004
6005 -----------------
6006 -- Is_Renaming --
6007 -----------------
6008
6009 function Is_Renaming (N : Node_Id) return Boolean is
6010 begin
6011 return
6012 Is_Entity_Name (N) and then Present (Renamed_Entity (Entity (N)));
6013 end Is_Renaming;
6014
6015 -----------------------
6016 -- Is_Valid_Renaming --
6017 -----------------------
6018
6019 function Is_Valid_Renaming (N : Node_Id) return Boolean is
6020 function Check_Renaming (N : Node_Id) return Boolean;
6021 -- Recursive function used to traverse all the prefixes of N
6022
6023 --------------------
6024 -- Check_Renaming --
6025 --------------------
6026
6027 function Check_Renaming (N : Node_Id) return Boolean is
6028 begin
6029 if Is_Renaming (N)
6030 and then not Check_Renaming (Renamed_Entity (Entity (N)))
6031 then
6032 return False;
6033 end if;
6034
6035 if Nkind (N) = N_Indexed_Component then
6036 declare
6037 Indx : Node_Id;
6038
6039 begin
6040 Indx := First (Expressions (N));
6041 while Present (Indx) loop
6042 if not Is_OK_Static_Expression (Indx) then
6043 return False;
6044 end if;
6045
6046 Next_Index (Indx);
6047 end loop;
6048 end;
6049 end if;
6050
6051 if Has_Prefix (N) then
6052 declare
6053 P : constant Node_Id := Prefix (N);
6054
6055 begin
6056 if Nkind (N) = N_Explicit_Dereference
6057 and then Is_Variable (P)
6058 then
6059 return False;
6060
6061 elsif Is_Entity_Name (P)
6062 and then Ekind (Entity (P)) = E_Function
6063 then
6064 return False;
6065
6066 elsif Nkind (P) = N_Function_Call then
6067 return False;
6068 end if;
6069
6070 -- Recursion to continue traversing the prefix of the
6071 -- renaming expression
6072
6073 return Check_Renaming (P);
6074 end;
6075 end if;
6076
6077 return True;
6078 end Check_Renaming;
6079
6080 -- Start of processing for Is_Valid_Renaming
6081
6082 begin
6083 return Check_Renaming (N);
6084 end Is_Valid_Renaming;
6085
6086 -- Local variables
6087
6088 Obj1 : Node_Id := A1;
6089 Obj2 : Node_Id := A2;
6090
6091 -- Start of processing for Denotes_Same_Object
6092
6093 begin
6094 -- Both names statically denote the same stand-alone object or parameter
6095 -- (RM 6.4.1(6.5/3))
6096
6097 if Is_Entity_Name (Obj1)
6098 and then Is_Entity_Name (Obj2)
6099 and then Entity (Obj1) = Entity (Obj2)
6100 then
6101 return True;
6102 end if;
6103
6104 -- For renamings, the prefix of any dereference within the renamed
6105 -- object_name is not a variable, and any expression within the
6106 -- renamed object_name contains no references to variables nor
6107 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
6108
6109 if Is_Renaming (Obj1) then
6110 if Is_Valid_Renaming (Obj1) then
6111 Obj1 := Renamed_Entity (Entity (Obj1));
6112 else
6113 return False;
6114 end if;
6115 end if;
6116
6117 if Is_Renaming (Obj2) then
6118 if Is_Valid_Renaming (Obj2) then
6119 Obj2 := Renamed_Entity (Entity (Obj2));
6120 else
6121 return False;
6122 end if;
6123 end if;
6124
6125 -- No match if not same node kind (such cases are handled by
6126 -- Denotes_Same_Prefix)
6127
6128 if Nkind (Obj1) /= Nkind (Obj2) then
6129 return False;
6130
6131 -- After handling valid renamings, one of the two names statically
6132 -- denoted a renaming declaration whose renamed object_name is known
6133 -- to denote the same object as the other (RM 6.4.1(6.10/3))
6134
6135 elsif Is_Entity_Name (Obj1) then
6136 if Is_Entity_Name (Obj2) then
6137 return Entity (Obj1) = Entity (Obj2);
6138 else
6139 return False;
6140 end if;
6141
6142 -- Both names are selected_components, their prefixes are known to
6143 -- denote the same object, and their selector_names denote the same
6144 -- component (RM 6.4.1(6.6/3)).
6145
6146 elsif Nkind (Obj1) = N_Selected_Component then
6147 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
6148 and then
6149 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
6150
6151 -- Both names are dereferences and the dereferenced names are known to
6152 -- denote the same object (RM 6.4.1(6.7/3))
6153
6154 elsif Nkind (Obj1) = N_Explicit_Dereference then
6155 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
6156
6157 -- Both names are indexed_components, their prefixes are known to denote
6158 -- the same object, and each of the pairs of corresponding index values
6159 -- are either both static expressions with the same static value or both
6160 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
6161
6162 elsif Nkind (Obj1) = N_Indexed_Component then
6163 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
6164 return False;
6165 else
6166 declare
6167 Indx1 : Node_Id;
6168 Indx2 : Node_Id;
6169
6170 begin
6171 Indx1 := First (Expressions (Obj1));
6172 Indx2 := First (Expressions (Obj2));
6173 while Present (Indx1) loop
6174
6175 -- Indexes must denote the same static value or same object
6176
6177 if Is_OK_Static_Expression (Indx1) then
6178 if not Is_OK_Static_Expression (Indx2) then
6179 return False;
6180
6181 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
6182 return False;
6183 end if;
6184
6185 elsif not Denotes_Same_Object (Indx1, Indx2) then
6186 return False;
6187 end if;
6188
6189 Next (Indx1);
6190 Next (Indx2);
6191 end loop;
6192
6193 return True;
6194 end;
6195 end if;
6196
6197 -- Both names are slices, their prefixes are known to denote the same
6198 -- object, and the two slices have statically matching index constraints
6199 -- (RM 6.4.1(6.9/3))
6200
6201 elsif Nkind (Obj1) = N_Slice
6202 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
6203 then
6204 declare
6205 Lo1, Lo2, Hi1, Hi2 : Node_Id;
6206
6207 begin
6208 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
6209 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
6210
6211 -- Check whether bounds are statically identical. There is no
6212 -- attempt to detect partial overlap of slices.
6213
6214 return Denotes_Same_Object (Lo1, Lo2)
6215 and then
6216 Denotes_Same_Object (Hi1, Hi2);
6217 end;
6218
6219 -- In the recursion, literals appear as indexes
6220
6221 elsif Nkind (Obj1) = N_Integer_Literal
6222 and then
6223 Nkind (Obj2) = N_Integer_Literal
6224 then
6225 return Intval (Obj1) = Intval (Obj2);
6226
6227 else
6228 return False;
6229 end if;
6230 end Denotes_Same_Object;
6231
6232 -------------------------
6233 -- Denotes_Same_Prefix --
6234 -------------------------
6235
6236 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
6237 begin
6238 if Is_Entity_Name (A1) then
6239 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
6240 and then not Is_Access_Type (Etype (A1))
6241 then
6242 return Denotes_Same_Object (A1, Prefix (A2))
6243 or else Denotes_Same_Prefix (A1, Prefix (A2));
6244 else
6245 return False;
6246 end if;
6247
6248 elsif Is_Entity_Name (A2) then
6249 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
6250
6251 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
6252 and then
6253 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
6254 then
6255 declare
6256 Root1, Root2 : Node_Id;
6257 Depth1, Depth2 : Nat := 0;
6258
6259 begin
6260 Root1 := Prefix (A1);
6261 while not Is_Entity_Name (Root1) loop
6262 if not Nkind_In
6263 (Root1, N_Selected_Component, N_Indexed_Component)
6264 then
6265 return False;
6266 else
6267 Root1 := Prefix (Root1);
6268 end if;
6269
6270 Depth1 := Depth1 + 1;
6271 end loop;
6272
6273 Root2 := Prefix (A2);
6274 while not Is_Entity_Name (Root2) loop
6275 if not Nkind_In (Root2, N_Selected_Component,
6276 N_Indexed_Component)
6277 then
6278 return False;
6279 else
6280 Root2 := Prefix (Root2);
6281 end if;
6282
6283 Depth2 := Depth2 + 1;
6284 end loop;
6285
6286 -- If both have the same depth and they do not denote the same
6287 -- object, they are disjoint and no warning is needed.
6288
6289 if Depth1 = Depth2 then
6290 return False;
6291
6292 elsif Depth1 > Depth2 then
6293 Root1 := Prefix (A1);
6294 for J in 1 .. Depth1 - Depth2 - 1 loop
6295 Root1 := Prefix (Root1);
6296 end loop;
6297
6298 return Denotes_Same_Object (Root1, A2);
6299
6300 else
6301 Root2 := Prefix (A2);
6302 for J in 1 .. Depth2 - Depth1 - 1 loop
6303 Root2 := Prefix (Root2);
6304 end loop;
6305
6306 return Denotes_Same_Object (A1, Root2);
6307 end if;
6308 end;
6309
6310 else
6311 return False;
6312 end if;
6313 end Denotes_Same_Prefix;
6314
6315 ----------------------
6316 -- Denotes_Variable --
6317 ----------------------
6318
6319 function Denotes_Variable (N : Node_Id) return Boolean is
6320 begin
6321 return Is_Variable (N) and then Paren_Count (N) = 0;
6322 end Denotes_Variable;
6323
6324 -----------------------------
6325 -- Depends_On_Discriminant --
6326 -----------------------------
6327
6328 function Depends_On_Discriminant (N : Node_Id) return Boolean is
6329 L : Node_Id;
6330 H : Node_Id;
6331
6332 begin
6333 Get_Index_Bounds (N, L, H);
6334 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
6335 end Depends_On_Discriminant;
6336
6337 -------------------------
6338 -- Designate_Same_Unit --
6339 -------------------------
6340
6341 function Designate_Same_Unit
6342 (Name1 : Node_Id;
6343 Name2 : Node_Id) return Boolean
6344 is
6345 K1 : constant Node_Kind := Nkind (Name1);
6346 K2 : constant Node_Kind := Nkind (Name2);
6347
6348 function Prefix_Node (N : Node_Id) return Node_Id;
6349 -- Returns the parent unit name node of a defining program unit name
6350 -- or the prefix if N is a selected component or an expanded name.
6351
6352 function Select_Node (N : Node_Id) return Node_Id;
6353 -- Returns the defining identifier node of a defining program unit
6354 -- name or the selector node if N is a selected component or an
6355 -- expanded name.
6356
6357 -----------------
6358 -- Prefix_Node --
6359 -----------------
6360
6361 function Prefix_Node (N : Node_Id) return Node_Id is
6362 begin
6363 if Nkind (N) = N_Defining_Program_Unit_Name then
6364 return Name (N);
6365 else
6366 return Prefix (N);
6367 end if;
6368 end Prefix_Node;
6369
6370 -----------------
6371 -- Select_Node --
6372 -----------------
6373
6374 function Select_Node (N : Node_Id) return Node_Id is
6375 begin
6376 if Nkind (N) = N_Defining_Program_Unit_Name then
6377 return Defining_Identifier (N);
6378 else
6379 return Selector_Name (N);
6380 end if;
6381 end Select_Node;
6382
6383 -- Start of processing for Designate_Same_Unit
6384
6385 begin
6386 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
6387 and then
6388 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
6389 then
6390 return Chars (Name1) = Chars (Name2);
6391
6392 elsif Nkind_In (K1, N_Expanded_Name,
6393 N_Selected_Component,
6394 N_Defining_Program_Unit_Name)
6395 and then
6396 Nkind_In (K2, N_Expanded_Name,
6397 N_Selected_Component,
6398 N_Defining_Program_Unit_Name)
6399 then
6400 return
6401 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
6402 and then
6403 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
6404
6405 else
6406 return False;
6407 end if;
6408 end Designate_Same_Unit;
6409
6410 ---------------------------------------------
6411 -- Diagnose_Iterated_Component_Association --
6412 ---------------------------------------------
6413
6414 procedure Diagnose_Iterated_Component_Association (N : Node_Id) is
6415 Def_Id : constant Entity_Id := Defining_Identifier (N);
6416 Aggr : Node_Id;
6417
6418 begin
6419 -- Determine whether the iterated component association appears within
6420 -- an aggregate. If this is the case, raise Program_Error because the
6421 -- iterated component association cannot be left in the tree as is and
6422 -- must always be processed by the related aggregate.
6423
6424 Aggr := N;
6425 while Present (Aggr) loop
6426 if Nkind (Aggr) = N_Aggregate then
6427 raise Program_Error;
6428
6429 -- Prevent the search from going too far
6430
6431 elsif Is_Body_Or_Package_Declaration (Aggr) then
6432 exit;
6433 end if;
6434
6435 Aggr := Parent (Aggr);
6436 end loop;
6437
6438 -- At this point it is known that the iterated component association is
6439 -- not within an aggregate. This is really a quantified expression with
6440 -- a missing "all" or "some" quantifier.
6441
6442 Error_Msg_N ("missing quantifier", Def_Id);
6443
6444 -- Rewrite the iterated component association as True to prevent any
6445 -- cascaded errors.
6446
6447 Rewrite (N, New_Occurrence_Of (Standard_True, Sloc (N)));
6448 Analyze (N);
6449 end Diagnose_Iterated_Component_Association;
6450
6451 ---------------------------------
6452 -- Dynamic_Accessibility_Level --
6453 ---------------------------------
6454
6455 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
6456 Loc : constant Source_Ptr := Sloc (Expr);
6457
6458 function Make_Level_Literal (Level : Uint) return Node_Id;
6459 -- Construct an integer literal representing an accessibility level
6460 -- with its type set to Natural.
6461
6462 ------------------------
6463 -- Make_Level_Literal --
6464 ------------------------
6465
6466 function Make_Level_Literal (Level : Uint) return Node_Id is
6467 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
6468
6469 begin
6470 Set_Etype (Result, Standard_Natural);
6471 return Result;
6472 end Make_Level_Literal;
6473
6474 -- Local variables
6475
6476 E : Entity_Id;
6477
6478 -- Start of processing for Dynamic_Accessibility_Level
6479
6480 begin
6481 if Is_Entity_Name (Expr) then
6482 E := Entity (Expr);
6483
6484 if Present (Renamed_Object (E)) then
6485 return Dynamic_Accessibility_Level (Renamed_Object (E));
6486 end if;
6487
6488 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
6489 if Present (Extra_Accessibility (E)) then
6490 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
6491 end if;
6492 end if;
6493 end if;
6494
6495 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
6496
6497 case Nkind (Expr) is
6498
6499 -- For access discriminant, the level of the enclosing object
6500
6501 when N_Selected_Component =>
6502 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
6503 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
6504 E_Anonymous_Access_Type
6505 then
6506 return Make_Level_Literal (Object_Access_Level (Expr));
6507 end if;
6508
6509 when N_Attribute_Reference =>
6510 case Get_Attribute_Id (Attribute_Name (Expr)) is
6511
6512 -- For X'Access, the level of the prefix X
6513
6514 when Attribute_Access =>
6515 return Make_Level_Literal
6516 (Object_Access_Level (Prefix (Expr)));
6517
6518 -- Treat the unchecked attributes as library-level
6519
6520 when Attribute_Unchecked_Access
6521 | Attribute_Unrestricted_Access
6522 =>
6523 return Make_Level_Literal (Scope_Depth (Standard_Standard));
6524
6525 -- No other access-valued attributes
6526
6527 when others =>
6528 raise Program_Error;
6529 end case;
6530
6531 when N_Allocator =>
6532
6533 -- Unimplemented: depends on context. As an actual parameter where
6534 -- formal type is anonymous, use
6535 -- Scope_Depth (Current_Scope) + 1.
6536 -- For other cases, see 3.10.2(14/3) and following. ???
6537
6538 null;
6539
6540 when N_Type_Conversion =>
6541 if not Is_Local_Anonymous_Access (Etype (Expr)) then
6542
6543 -- Handle type conversions introduced for a rename of an
6544 -- Ada 2012 stand-alone object of an anonymous access type.
6545
6546 return Dynamic_Accessibility_Level (Expression (Expr));
6547 end if;
6548
6549 when others =>
6550 null;
6551 end case;
6552
6553 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
6554 end Dynamic_Accessibility_Level;
6555
6556 ------------------------
6557 -- Discriminated_Size --
6558 ------------------------
6559
6560 function Discriminated_Size (Comp : Entity_Id) return Boolean is
6561 function Non_Static_Bound (Bound : Node_Id) return Boolean;
6562 -- Check whether the bound of an index is non-static and does denote
6563 -- a discriminant, in which case any object of the type (protected or
6564 -- otherwise) will have a non-static size.
6565
6566 ----------------------
6567 -- Non_Static_Bound --
6568 ----------------------
6569
6570 function Non_Static_Bound (Bound : Node_Id) return Boolean is
6571 begin
6572 if Is_OK_Static_Expression (Bound) then
6573 return False;
6574
6575 -- If the bound is given by a discriminant it is non-static
6576 -- (A static constraint replaces the reference with the value).
6577 -- In an protected object the discriminant has been replaced by
6578 -- the corresponding discriminal within the protected operation.
6579
6580 elsif Is_Entity_Name (Bound)
6581 and then
6582 (Ekind (Entity (Bound)) = E_Discriminant
6583 or else Present (Discriminal_Link (Entity (Bound))))
6584 then
6585 return False;
6586
6587 else
6588 return True;
6589 end if;
6590 end Non_Static_Bound;
6591
6592 -- Local variables
6593
6594 Typ : constant Entity_Id := Etype (Comp);
6595 Index : Node_Id;
6596
6597 -- Start of processing for Discriminated_Size
6598
6599 begin
6600 if not Is_Array_Type (Typ) then
6601 return False;
6602 end if;
6603
6604 if Ekind (Typ) = E_Array_Subtype then
6605 Index := First_Index (Typ);
6606 while Present (Index) loop
6607 if Non_Static_Bound (Low_Bound (Index))
6608 or else Non_Static_Bound (High_Bound (Index))
6609 then
6610 return False;
6611 end if;
6612
6613 Next_Index (Index);
6614 end loop;
6615
6616 return True;
6617 end if;
6618
6619 return False;
6620 end Discriminated_Size;
6621
6622 -----------------------------------
6623 -- Effective_Extra_Accessibility --
6624 -----------------------------------
6625
6626 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
6627 begin
6628 if Present (Renamed_Object (Id))
6629 and then Is_Entity_Name (Renamed_Object (Id))
6630 then
6631 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
6632 else
6633 return Extra_Accessibility (Id);
6634 end if;
6635 end Effective_Extra_Accessibility;
6636
6637 -----------------------------
6638 -- Effective_Reads_Enabled --
6639 -----------------------------
6640
6641 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
6642 begin
6643 return Has_Enabled_Property (Id, Name_Effective_Reads);
6644 end Effective_Reads_Enabled;
6645
6646 ------------------------------
6647 -- Effective_Writes_Enabled --
6648 ------------------------------
6649
6650 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
6651 begin
6652 return Has_Enabled_Property (Id, Name_Effective_Writes);
6653 end Effective_Writes_Enabled;
6654
6655 ------------------------------
6656 -- Enclosing_Comp_Unit_Node --
6657 ------------------------------
6658
6659 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
6660 Current_Node : Node_Id;
6661
6662 begin
6663 Current_Node := N;
6664 while Present (Current_Node)
6665 and then Nkind (Current_Node) /= N_Compilation_Unit
6666 loop
6667 Current_Node := Parent (Current_Node);
6668 end loop;
6669
6670 if Nkind (Current_Node) /= N_Compilation_Unit then
6671 return Empty;
6672 else
6673 return Current_Node;
6674 end if;
6675 end Enclosing_Comp_Unit_Node;
6676
6677 --------------------------
6678 -- Enclosing_CPP_Parent --
6679 --------------------------
6680
6681 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
6682 Parent_Typ : Entity_Id := Typ;
6683
6684 begin
6685 while not Is_CPP_Class (Parent_Typ)
6686 and then Etype (Parent_Typ) /= Parent_Typ
6687 loop
6688 Parent_Typ := Etype (Parent_Typ);
6689
6690 if Is_Private_Type (Parent_Typ) then
6691 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6692 end if;
6693 end loop;
6694
6695 pragma Assert (Is_CPP_Class (Parent_Typ));
6696 return Parent_Typ;
6697 end Enclosing_CPP_Parent;
6698
6699 ---------------------------
6700 -- Enclosing_Declaration --
6701 ---------------------------
6702
6703 function Enclosing_Declaration (N : Node_Id) return Node_Id is
6704 Decl : Node_Id := N;
6705
6706 begin
6707 while Present (Decl)
6708 and then not (Nkind (Decl) in N_Declaration
6709 or else
6710 Nkind (Decl) in N_Later_Decl_Item
6711 or else
6712 Nkind (Decl) = N_Number_Declaration)
6713 loop
6714 Decl := Parent (Decl);
6715 end loop;
6716
6717 return Decl;
6718 end Enclosing_Declaration;
6719
6720 ----------------------------
6721 -- Enclosing_Generic_Body --
6722 ----------------------------
6723
6724 function Enclosing_Generic_Body (N : Node_Id) return Node_Id is
6725 Par : Node_Id;
6726 Spec_Id : Entity_Id;
6727
6728 begin
6729 Par := Parent (N);
6730 while Present (Par) loop
6731 if Nkind_In (Par, N_Package_Body, N_Subprogram_Body) then
6732 Spec_Id := Corresponding_Spec (Par);
6733
6734 if Present (Spec_Id)
6735 and then Nkind_In (Unit_Declaration_Node (Spec_Id),
6736 N_Generic_Package_Declaration,
6737 N_Generic_Subprogram_Declaration)
6738 then
6739 return Par;
6740 end if;
6741 end if;
6742
6743 Par := Parent (Par);
6744 end loop;
6745
6746 return Empty;
6747 end Enclosing_Generic_Body;
6748
6749 ----------------------------
6750 -- Enclosing_Generic_Unit --
6751 ----------------------------
6752
6753 function Enclosing_Generic_Unit (N : Node_Id) return Node_Id is
6754 Par : Node_Id;
6755 Spec_Decl : Node_Id;
6756 Spec_Id : Entity_Id;
6757
6758 begin
6759 Par := Parent (N);
6760 while Present (Par) loop
6761 if Nkind_In (Par, N_Generic_Package_Declaration,
6762 N_Generic_Subprogram_Declaration)
6763 then
6764 return Par;
6765
6766 elsif Nkind_In (Par, N_Package_Body, N_Subprogram_Body) then
6767 Spec_Id := Corresponding_Spec (Par);
6768
6769 if Present (Spec_Id) then
6770 Spec_Decl := Unit_Declaration_Node (Spec_Id);
6771
6772 if Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
6773 N_Generic_Subprogram_Declaration)
6774 then
6775 return Spec_Decl;
6776 end if;
6777 end if;
6778 end if;
6779
6780 Par := Parent (Par);
6781 end loop;
6782
6783 return Empty;
6784 end Enclosing_Generic_Unit;
6785
6786 -------------------------------
6787 -- Enclosing_Lib_Unit_Entity --
6788 -------------------------------
6789
6790 function Enclosing_Lib_Unit_Entity
6791 (E : Entity_Id := Current_Scope) return Entity_Id
6792 is
6793 Unit_Entity : Entity_Id;
6794
6795 begin
6796 -- Look for enclosing library unit entity by following scope links.
6797 -- Equivalent to, but faster than indexing through the scope stack.
6798
6799 Unit_Entity := E;
6800 while (Present (Scope (Unit_Entity))
6801 and then Scope (Unit_Entity) /= Standard_Standard)
6802 and not Is_Child_Unit (Unit_Entity)
6803 loop
6804 Unit_Entity := Scope (Unit_Entity);
6805 end loop;
6806
6807 return Unit_Entity;
6808 end Enclosing_Lib_Unit_Entity;
6809
6810 -----------------------------
6811 -- Enclosing_Lib_Unit_Node --
6812 -----------------------------
6813
6814 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
6815 Encl_Unit : Node_Id;
6816
6817 begin
6818 Encl_Unit := Enclosing_Comp_Unit_Node (N);
6819 while Present (Encl_Unit)
6820 and then Nkind (Unit (Encl_Unit)) = N_Subunit
6821 loop
6822 Encl_Unit := Library_Unit (Encl_Unit);
6823 end loop;
6824
6825 pragma Assert (Nkind (Encl_Unit) = N_Compilation_Unit);
6826 return Encl_Unit;
6827 end Enclosing_Lib_Unit_Node;
6828
6829 -----------------------
6830 -- Enclosing_Package --
6831 -----------------------
6832
6833 function Enclosing_Package (E : Entity_Id) return Entity_Id is
6834 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6835
6836 begin
6837 if Dynamic_Scope = Standard_Standard then
6838 return Standard_Standard;
6839
6840 elsif Dynamic_Scope = Empty then
6841 return Empty;
6842
6843 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
6844 E_Generic_Package)
6845 then
6846 return Dynamic_Scope;
6847
6848 else
6849 return Enclosing_Package (Dynamic_Scope);
6850 end if;
6851 end Enclosing_Package;
6852
6853 -------------------------------------
6854 -- Enclosing_Package_Or_Subprogram --
6855 -------------------------------------
6856
6857 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
6858 S : Entity_Id;
6859
6860 begin
6861 S := Scope (E);
6862 while Present (S) loop
6863 if Is_Package_Or_Generic_Package (S)
6864 or else Ekind (S) = E_Package_Body
6865 then
6866 return S;
6867
6868 elsif Is_Subprogram_Or_Generic_Subprogram (S)
6869 or else Ekind (S) = E_Subprogram_Body
6870 then
6871 return S;
6872
6873 else
6874 S := Scope (S);
6875 end if;
6876 end loop;
6877
6878 return Empty;
6879 end Enclosing_Package_Or_Subprogram;
6880
6881 --------------------------
6882 -- Enclosing_Subprogram --
6883 --------------------------
6884
6885 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
6886 Dyn_Scop : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6887
6888 begin
6889 if Dyn_Scop = Standard_Standard then
6890 return Empty;
6891
6892 elsif Dyn_Scop = Empty then
6893 return Empty;
6894
6895 elsif Ekind (Dyn_Scop) = E_Subprogram_Body then
6896 return Corresponding_Spec (Parent (Parent (Dyn_Scop)));
6897
6898 elsif Ekind_In (Dyn_Scop, E_Block, E_Return_Statement) then
6899 return Enclosing_Subprogram (Dyn_Scop);
6900
6901 elsif Ekind (Dyn_Scop) = E_Entry then
6902
6903 -- For a task entry, return the enclosing subprogram of the
6904 -- task itself.
6905
6906 if Ekind (Scope (Dyn_Scop)) = E_Task_Type then
6907 return Enclosing_Subprogram (Dyn_Scop);
6908
6909 -- A protected entry is rewritten as a protected procedure which is
6910 -- the desired enclosing subprogram. This is relevant when unnesting
6911 -- a procedure local to an entry body.
6912
6913 else
6914 return Protected_Body_Subprogram (Dyn_Scop);
6915 end if;
6916
6917 elsif Ekind (Dyn_Scop) = E_Task_Type then
6918 return Get_Task_Body_Procedure (Dyn_Scop);
6919
6920 -- The scope may appear as a private type or as a private extension
6921 -- whose completion is a task or protected type.
6922
6923 elsif Ekind_In (Dyn_Scop, E_Limited_Private_Type,
6924 E_Record_Type_With_Private)
6925 and then Present (Full_View (Dyn_Scop))
6926 and then Ekind_In (Full_View (Dyn_Scop), E_Task_Type, E_Protected_Type)
6927 then
6928 return Get_Task_Body_Procedure (Full_View (Dyn_Scop));
6929
6930 -- No body is generated if the protected operation is eliminated
6931
6932 elsif Convention (Dyn_Scop) = Convention_Protected
6933 and then not Is_Eliminated (Dyn_Scop)
6934 and then Present (Protected_Body_Subprogram (Dyn_Scop))
6935 then
6936 return Protected_Body_Subprogram (Dyn_Scop);
6937
6938 else
6939 return Dyn_Scop;
6940 end if;
6941 end Enclosing_Subprogram;
6942
6943 --------------------------
6944 -- End_Keyword_Location --
6945 --------------------------
6946
6947 function End_Keyword_Location (N : Node_Id) return Source_Ptr is
6948 function End_Label_Loc (Nod : Node_Id) return Source_Ptr;
6949 -- Return the source location of Nod's end label according to the
6950 -- following precedence rules:
6951 --
6952 -- 1) If the end label exists, return its location
6953 -- 2) If Nod exists, return its location
6954 -- 3) Return the location of N
6955
6956 -------------------
6957 -- End_Label_Loc --
6958 -------------------
6959
6960 function End_Label_Loc (Nod : Node_Id) return Source_Ptr is
6961 Label : Node_Id;
6962
6963 begin
6964 if Present (Nod) then
6965 Label := End_Label (Nod);
6966
6967 if Present (Label) then
6968 return Sloc (Label);
6969 else
6970 return Sloc (Nod);
6971 end if;
6972
6973 else
6974 return Sloc (N);
6975 end if;
6976 end End_Label_Loc;
6977
6978 -- Local variables
6979
6980 Owner : Node_Id;
6981
6982 -- Start of processing for End_Keyword_Location
6983
6984 begin
6985 if Nkind_In (N, N_Block_Statement,
6986 N_Entry_Body,
6987 N_Package_Body,
6988 N_Subprogram_Body,
6989 N_Task_Body)
6990 then
6991 Owner := Handled_Statement_Sequence (N);
6992
6993 elsif Nkind (N) = N_Package_Declaration then
6994 Owner := Specification (N);
6995
6996 elsif Nkind (N) = N_Protected_Body then
6997 Owner := N;
6998
6999 elsif Nkind_In (N, N_Protected_Type_Declaration,
7000 N_Single_Protected_Declaration)
7001 then
7002 Owner := Protected_Definition (N);
7003
7004 elsif Nkind_In (N, N_Single_Task_Declaration,
7005 N_Task_Type_Declaration)
7006 then
7007 Owner := Task_Definition (N);
7008
7009 -- This routine should not be called with other contexts
7010
7011 else
7012 pragma Assert (False);
7013 null;
7014 end if;
7015
7016 return End_Label_Loc (Owner);
7017 end End_Keyword_Location;
7018
7019 ------------------------
7020 -- Ensure_Freeze_Node --
7021 ------------------------
7022
7023 procedure Ensure_Freeze_Node (E : Entity_Id) is
7024 FN : Node_Id;
7025 begin
7026 if No (Freeze_Node (E)) then
7027 FN := Make_Freeze_Entity (Sloc (E));
7028 Set_Has_Delayed_Freeze (E);
7029 Set_Freeze_Node (E, FN);
7030 Set_Access_Types_To_Process (FN, No_Elist);
7031 Set_TSS_Elist (FN, No_Elist);
7032 Set_Entity (FN, E);
7033 end if;
7034 end Ensure_Freeze_Node;
7035
7036 ----------------
7037 -- Enter_Name --
7038 ----------------
7039
7040 procedure Enter_Name (Def_Id : Entity_Id) is
7041 C : constant Entity_Id := Current_Entity (Def_Id);
7042 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
7043 S : constant Entity_Id := Current_Scope;
7044
7045 begin
7046 Generate_Definition (Def_Id);
7047
7048 -- Add new name to current scope declarations. Check for duplicate
7049 -- declaration, which may or may not be a genuine error.
7050
7051 if Present (E) then
7052
7053 -- Case of previous entity entered because of a missing declaration
7054 -- or else a bad subtype indication. Best is to use the new entity,
7055 -- and make the previous one invisible.
7056
7057 if Etype (E) = Any_Type then
7058 Set_Is_Immediately_Visible (E, False);
7059
7060 -- Case of renaming declaration constructed for package instances.
7061 -- if there is an explicit declaration with the same identifier,
7062 -- the renaming is not immediately visible any longer, but remains
7063 -- visible through selected component notation.
7064
7065 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
7066 and then not Comes_From_Source (E)
7067 then
7068 Set_Is_Immediately_Visible (E, False);
7069
7070 -- The new entity may be the package renaming, which has the same
7071 -- same name as a generic formal which has been seen already.
7072
7073 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
7074 and then not Comes_From_Source (Def_Id)
7075 then
7076 Set_Is_Immediately_Visible (E, False);
7077
7078 -- For a fat pointer corresponding to a remote access to subprogram,
7079 -- we use the same identifier as the RAS type, so that the proper
7080 -- name appears in the stub. This type is only retrieved through
7081 -- the RAS type and never by visibility, and is not added to the
7082 -- visibility list (see below).
7083
7084 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
7085 and then Ekind (Def_Id) = E_Record_Type
7086 and then Present (Corresponding_Remote_Type (Def_Id))
7087 then
7088 null;
7089
7090 -- Case of an implicit operation or derived literal. The new entity
7091 -- hides the implicit one, which is removed from all visibility,
7092 -- i.e. the entity list of its scope, and homonym chain of its name.
7093
7094 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
7095 or else Is_Internal (E)
7096 then
7097 declare
7098 Decl : constant Node_Id := Parent (E);
7099 Prev : Entity_Id;
7100 Prev_Vis : Entity_Id;
7101
7102 begin
7103 -- If E is an implicit declaration, it cannot be the first
7104 -- entity in the scope.
7105
7106 Prev := First_Entity (Current_Scope);
7107 while Present (Prev) and then Next_Entity (Prev) /= E loop
7108 Next_Entity (Prev);
7109 end loop;
7110
7111 if No (Prev) then
7112
7113 -- If E is not on the entity chain of the current scope,
7114 -- it is an implicit declaration in the generic formal
7115 -- part of a generic subprogram. When analyzing the body,
7116 -- the generic formals are visible but not on the entity
7117 -- chain of the subprogram. The new entity will become
7118 -- the visible one in the body.
7119
7120 pragma Assert
7121 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
7122 null;
7123
7124 else
7125 Link_Entities (Prev, Next_Entity (E));
7126
7127 if No (Next_Entity (Prev)) then
7128 Set_Last_Entity (Current_Scope, Prev);
7129 end if;
7130
7131 if E = Current_Entity (E) then
7132 Prev_Vis := Empty;
7133
7134 else
7135 Prev_Vis := Current_Entity (E);
7136 while Homonym (Prev_Vis) /= E loop
7137 Prev_Vis := Homonym (Prev_Vis);
7138 end loop;
7139 end if;
7140
7141 if Present (Prev_Vis) then
7142
7143 -- Skip E in the visibility chain
7144
7145 Set_Homonym (Prev_Vis, Homonym (E));
7146
7147 else
7148 Set_Name_Entity_Id (Chars (E), Homonym (E));
7149 end if;
7150 end if;
7151 end;
7152
7153 -- This section of code could use a comment ???
7154
7155 elsif Present (Etype (E))
7156 and then Is_Concurrent_Type (Etype (E))
7157 and then E = Def_Id
7158 then
7159 return;
7160
7161 -- If the homograph is a protected component renaming, it should not
7162 -- be hiding the current entity. Such renamings are treated as weak
7163 -- declarations.
7164
7165 elsif Is_Prival (E) then
7166 Set_Is_Immediately_Visible (E, False);
7167
7168 -- In this case the current entity is a protected component renaming.
7169 -- Perform minimal decoration by setting the scope and return since
7170 -- the prival should not be hiding other visible entities.
7171
7172 elsif Is_Prival (Def_Id) then
7173 Set_Scope (Def_Id, Current_Scope);
7174 return;
7175
7176 -- Analogous to privals, the discriminal generated for an entry index
7177 -- parameter acts as a weak declaration. Perform minimal decoration
7178 -- to avoid bogus errors.
7179
7180 elsif Is_Discriminal (Def_Id)
7181 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
7182 then
7183 Set_Scope (Def_Id, Current_Scope);
7184 return;
7185
7186 -- In the body or private part of an instance, a type extension may
7187 -- introduce a component with the same name as that of an actual. The
7188 -- legality rule is not enforced, but the semantics of the full type
7189 -- with two components of same name are not clear at this point???
7190
7191 elsif In_Instance_Not_Visible then
7192 null;
7193
7194 -- When compiling a package body, some child units may have become
7195 -- visible. They cannot conflict with local entities that hide them.
7196
7197 elsif Is_Child_Unit (E)
7198 and then In_Open_Scopes (Scope (E))
7199 and then not Is_Immediately_Visible (E)
7200 then
7201 null;
7202
7203 -- Conversely, with front-end inlining we may compile the parent body
7204 -- first, and a child unit subsequently. The context is now the
7205 -- parent spec, and body entities are not visible.
7206
7207 elsif Is_Child_Unit (Def_Id)
7208 and then Is_Package_Body_Entity (E)
7209 and then not In_Package_Body (Current_Scope)
7210 then
7211 null;
7212
7213 -- Case of genuine duplicate declaration
7214
7215 else
7216 Error_Msg_Sloc := Sloc (E);
7217
7218 -- If the previous declaration is an incomplete type declaration
7219 -- this may be an attempt to complete it with a private type. The
7220 -- following avoids confusing cascaded errors.
7221
7222 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
7223 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
7224 then
7225 Error_Msg_N
7226 ("incomplete type cannot be completed with a private " &
7227 "declaration", Parent (Def_Id));
7228 Set_Is_Immediately_Visible (E, False);
7229 Set_Full_View (E, Def_Id);
7230
7231 -- An inherited component of a record conflicts with a new
7232 -- discriminant. The discriminant is inserted first in the scope,
7233 -- but the error should be posted on it, not on the component.
7234
7235 elsif Ekind (E) = E_Discriminant
7236 and then Present (Scope (Def_Id))
7237 and then Scope (Def_Id) /= Current_Scope
7238 then
7239 Error_Msg_Sloc := Sloc (Def_Id);
7240 Error_Msg_N ("& conflicts with declaration#", E);
7241 return;
7242
7243 -- If the name of the unit appears in its own context clause, a
7244 -- dummy package with the name has already been created, and the
7245 -- error emitted. Try to continue quietly.
7246
7247 elsif Error_Posted (E)
7248 and then Sloc (E) = No_Location
7249 and then Nkind (Parent (E)) = N_Package_Specification
7250 and then Current_Scope = Standard_Standard
7251 then
7252 Set_Scope (Def_Id, Current_Scope);
7253 return;
7254
7255 else
7256 Error_Msg_N ("& conflicts with declaration#", Def_Id);
7257
7258 -- Avoid cascaded messages with duplicate components in
7259 -- derived types.
7260
7261 if Ekind_In (E, E_Component, E_Discriminant) then
7262 return;
7263 end if;
7264 end if;
7265
7266 if Nkind (Parent (Parent (Def_Id))) =
7267 N_Generic_Subprogram_Declaration
7268 and then Def_Id =
7269 Defining_Entity (Specification (Parent (Parent (Def_Id))))
7270 then
7271 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
7272 end if;
7273
7274 -- If entity is in standard, then we are in trouble, because it
7275 -- means that we have a library package with a duplicated name.
7276 -- That's hard to recover from, so abort.
7277
7278 if S = Standard_Standard then
7279 raise Unrecoverable_Error;
7280
7281 -- Otherwise we continue with the declaration. Having two
7282 -- identical declarations should not cause us too much trouble.
7283
7284 else
7285 null;
7286 end if;
7287 end if;
7288 end if;
7289
7290 -- If we fall through, declaration is OK, at least OK enough to continue
7291
7292 -- If Def_Id is a discriminant or a record component we are in the midst
7293 -- of inheriting components in a derived record definition. Preserve
7294 -- their Ekind and Etype.
7295
7296 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
7297 null;
7298
7299 -- If a type is already set, leave it alone (happens when a type
7300 -- declaration is reanalyzed following a call to the optimizer).
7301
7302 elsif Present (Etype (Def_Id)) then
7303 null;
7304
7305 -- Otherwise, the kind E_Void insures that premature uses of the entity
7306 -- will be detected. Any_Type insures that no cascaded errors will occur
7307
7308 else
7309 Set_Ekind (Def_Id, E_Void);
7310 Set_Etype (Def_Id, Any_Type);
7311 end if;
7312
7313 -- Inherited discriminants and components in derived record types are
7314 -- immediately visible. Itypes are not.
7315
7316 -- Unless the Itype is for a record type with a corresponding remote
7317 -- type (what is that about, it was not commented ???)
7318
7319 if Ekind_In (Def_Id, E_Discriminant, E_Component)
7320 or else
7321 ((not Is_Record_Type (Def_Id)
7322 or else No (Corresponding_Remote_Type (Def_Id)))
7323 and then not Is_Itype (Def_Id))
7324 then
7325 Set_Is_Immediately_Visible (Def_Id);
7326 Set_Current_Entity (Def_Id);
7327 end if;
7328
7329 Set_Homonym (Def_Id, C);
7330 Append_Entity (Def_Id, S);
7331 Set_Public_Status (Def_Id);
7332
7333 -- Declaring a homonym is not allowed in SPARK ...
7334
7335 if Present (C) and then Restriction_Check_Required (SPARK_05) then
7336 declare
7337 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
7338 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
7339 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
7340
7341 begin
7342 -- ... unless the new declaration is in a subprogram, and the
7343 -- visible declaration is a variable declaration or a parameter
7344 -- specification outside that subprogram.
7345
7346 if Present (Enclosing_Subp)
7347 and then Nkind_In (Parent (C), N_Object_Declaration,
7348 N_Parameter_Specification)
7349 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
7350 then
7351 null;
7352
7353 -- ... or the new declaration is in a package, and the visible
7354 -- declaration occurs outside that package.
7355
7356 elsif Present (Enclosing_Pack)
7357 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
7358 then
7359 null;
7360
7361 -- ... or the new declaration is a component declaration in a
7362 -- record type definition.
7363
7364 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
7365 null;
7366
7367 -- Don't issue error for non-source entities
7368
7369 elsif Comes_From_Source (Def_Id)
7370 and then Comes_From_Source (C)
7371 then
7372 Error_Msg_Sloc := Sloc (C);
7373 Check_SPARK_05_Restriction
7374 ("redeclaration of identifier &#", Def_Id);
7375 end if;
7376 end;
7377 end if;
7378
7379 -- Warn if new entity hides an old one
7380
7381 if Warn_On_Hiding and then Present (C)
7382
7383 -- Don't warn for record components since they always have a well
7384 -- defined scope which does not confuse other uses. Note that in
7385 -- some cases, Ekind has not been set yet.
7386
7387 and then Ekind (C) /= E_Component
7388 and then Ekind (C) /= E_Discriminant
7389 and then Nkind (Parent (C)) /= N_Component_Declaration
7390 and then Ekind (Def_Id) /= E_Component
7391 and then Ekind (Def_Id) /= E_Discriminant
7392 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
7393
7394 -- Don't warn for one character variables. It is too common to use
7395 -- such variables as locals and will just cause too many false hits.
7396
7397 and then Length_Of_Name (Chars (C)) /= 1
7398
7399 -- Don't warn for non-source entities
7400
7401 and then Comes_From_Source (C)
7402 and then Comes_From_Source (Def_Id)
7403
7404 -- Don't warn unless entity in question is in extended main source
7405
7406 and then In_Extended_Main_Source_Unit (Def_Id)
7407
7408 -- Finally, the hidden entity must be either immediately visible or
7409 -- use visible (i.e. from a used package).
7410
7411 and then
7412 (Is_Immediately_Visible (C)
7413 or else
7414 Is_Potentially_Use_Visible (C))
7415 then
7416 Error_Msg_Sloc := Sloc (C);
7417 Error_Msg_N ("declaration hides &#?h?", Def_Id);
7418 end if;
7419 end Enter_Name;
7420
7421 ---------------
7422 -- Entity_Of --
7423 ---------------
7424
7425 function Entity_Of (N : Node_Id) return Entity_Id is
7426 Id : Entity_Id;
7427 Ren : Node_Id;
7428
7429 begin
7430 -- Assume that the arbitrary node does not have an entity
7431
7432 Id := Empty;
7433
7434 if Is_Entity_Name (N) then
7435 Id := Entity (N);
7436
7437 -- Follow a possible chain of renamings to reach the earliest renamed
7438 -- source object.
7439
7440 while Present (Id)
7441 and then Is_Object (Id)
7442 and then Present (Renamed_Object (Id))
7443 loop
7444 Ren := Renamed_Object (Id);
7445
7446 -- The reference renames an abstract state or a whole object
7447
7448 -- Obj : ...;
7449 -- Ren : ... renames Obj;
7450
7451 if Is_Entity_Name (Ren) then
7452
7453 -- Do not follow a renaming that goes through a generic formal,
7454 -- because these entities are hidden and must not be referenced
7455 -- from outside the generic.
7456
7457 if Is_Hidden (Entity (Ren)) then
7458 exit;
7459
7460 else
7461 Id := Entity (Ren);
7462 end if;
7463
7464 -- The reference renames a function result. Check the original
7465 -- node in case expansion relocates the function call.
7466
7467 -- Ren : ... renames Func_Call;
7468
7469 elsif Nkind (Original_Node (Ren)) = N_Function_Call then
7470 exit;
7471
7472 -- Otherwise the reference renames something which does not yield
7473 -- an abstract state or a whole object. Treat the reference as not
7474 -- having a proper entity for SPARK legality purposes.
7475
7476 else
7477 Id := Empty;
7478 exit;
7479 end if;
7480 end loop;
7481 end if;
7482
7483 return Id;
7484 end Entity_Of;
7485
7486 --------------------------
7487 -- Examine_Array_Bounds --
7488 --------------------------
7489
7490 procedure Examine_Array_Bounds
7491 (Typ : Entity_Id;
7492 All_Static : out Boolean;
7493 Has_Empty : out Boolean)
7494 is
7495 function Is_OK_Static_Bound (Bound : Node_Id) return Boolean;
7496 -- Determine whether bound Bound is a suitable static bound
7497
7498 ------------------------
7499 -- Is_OK_Static_Bound --
7500 ------------------------
7501
7502 function Is_OK_Static_Bound (Bound : Node_Id) return Boolean is
7503 begin
7504 return
7505 not Error_Posted (Bound)
7506 and then Is_OK_Static_Expression (Bound);
7507 end Is_OK_Static_Bound;
7508
7509 -- Local variables
7510
7511 Hi_Bound : Node_Id;
7512 Index : Node_Id;
7513 Lo_Bound : Node_Id;
7514
7515 -- Start of processing for Examine_Array_Bounds
7516
7517 begin
7518 -- An unconstrained array type does not have static bounds, and it is
7519 -- not known whether they are empty or not.
7520
7521 if not Is_Constrained (Typ) then
7522 All_Static := False;
7523 Has_Empty := False;
7524
7525 -- A string literal has static bounds, and is not empty as long as it
7526 -- contains at least one character.
7527
7528 elsif Ekind (Typ) = E_String_Literal_Subtype then
7529 All_Static := True;
7530 Has_Empty := String_Literal_Length (Typ) > 0;
7531 end if;
7532
7533 -- Assume that all bounds are static and not empty
7534
7535 All_Static := True;
7536 Has_Empty := False;
7537
7538 -- Examine each index
7539
7540 Index := First_Index (Typ);
7541 while Present (Index) loop
7542 if Is_Discrete_Type (Etype (Index)) then
7543 Get_Index_Bounds (Index, Lo_Bound, Hi_Bound);
7544
7545 if Is_OK_Static_Bound (Lo_Bound)
7546 and then
7547 Is_OK_Static_Bound (Hi_Bound)
7548 then
7549 -- The static bounds produce an empty range
7550
7551 if Is_Null_Range (Lo_Bound, Hi_Bound) then
7552 Has_Empty := True;
7553 end if;
7554
7555 -- Otherwise at least one of the bounds is not static
7556
7557 else
7558 All_Static := False;
7559 end if;
7560
7561 -- Otherwise the index is non-discrete, therefore not static
7562
7563 else
7564 All_Static := False;
7565 end if;
7566
7567 Next_Index (Index);
7568 end loop;
7569 end Examine_Array_Bounds;
7570
7571 -------------------
7572 -- Exceptions_OK --
7573 -------------------
7574
7575 function Exceptions_OK return Boolean is
7576 begin
7577 return
7578 not (Restriction_Active (No_Exception_Handlers) or else
7579 Restriction_Active (No_Exception_Propagation) or else
7580 Restriction_Active (No_Exceptions));
7581 end Exceptions_OK;
7582
7583 --------------------------
7584 -- Explain_Limited_Type --
7585 --------------------------
7586
7587 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
7588 C : Entity_Id;
7589
7590 begin
7591 -- For array, component type must be limited
7592
7593 if Is_Array_Type (T) then
7594 Error_Msg_Node_2 := T;
7595 Error_Msg_NE
7596 ("\component type& of type& is limited", N, Component_Type (T));
7597 Explain_Limited_Type (Component_Type (T), N);
7598
7599 elsif Is_Record_Type (T) then
7600
7601 -- No need for extra messages if explicit limited record
7602
7603 if Is_Limited_Record (Base_Type (T)) then
7604 return;
7605 end if;
7606
7607 -- Otherwise find a limited component. Check only components that
7608 -- come from source, or inherited components that appear in the
7609 -- source of the ancestor.
7610
7611 C := First_Component (T);
7612 while Present (C) loop
7613 if Is_Limited_Type (Etype (C))
7614 and then
7615 (Comes_From_Source (C)
7616 or else
7617 (Present (Original_Record_Component (C))
7618 and then
7619 Comes_From_Source (Original_Record_Component (C))))
7620 then
7621 Error_Msg_Node_2 := T;
7622 Error_Msg_NE ("\component& of type& has limited type", N, C);
7623 Explain_Limited_Type (Etype (C), N);
7624 return;
7625 end if;
7626
7627 Next_Component (C);
7628 end loop;
7629
7630 -- The type may be declared explicitly limited, even if no component
7631 -- of it is limited, in which case we fall out of the loop.
7632 return;
7633 end if;
7634 end Explain_Limited_Type;
7635
7636 ---------------------------------------
7637 -- Expression_Of_Expression_Function --
7638 ---------------------------------------
7639
7640 function Expression_Of_Expression_Function
7641 (Subp : Entity_Id) return Node_Id
7642 is
7643 Expr_Func : Node_Id;
7644
7645 begin
7646 pragma Assert (Is_Expression_Function_Or_Completion (Subp));
7647
7648 if Nkind (Original_Node (Subprogram_Spec (Subp))) =
7649 N_Expression_Function
7650 then
7651 Expr_Func := Original_Node (Subprogram_Spec (Subp));
7652
7653 elsif Nkind (Original_Node (Subprogram_Body (Subp))) =
7654 N_Expression_Function
7655 then
7656 Expr_Func := Original_Node (Subprogram_Body (Subp));
7657
7658 else
7659 pragma Assert (False);
7660 null;
7661 end if;
7662
7663 return Original_Node (Expression (Expr_Func));
7664 end Expression_Of_Expression_Function;
7665
7666 -------------------------------
7667 -- Extensions_Visible_Status --
7668 -------------------------------
7669
7670 function Extensions_Visible_Status
7671 (Id : Entity_Id) return Extensions_Visible_Mode
7672 is
7673 Arg : Node_Id;
7674 Decl : Node_Id;
7675 Expr : Node_Id;
7676 Prag : Node_Id;
7677 Subp : Entity_Id;
7678
7679 begin
7680 -- When a formal parameter is subject to Extensions_Visible, the pragma
7681 -- is stored in the contract of related subprogram.
7682
7683 if Is_Formal (Id) then
7684 Subp := Scope (Id);
7685
7686 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
7687 Subp := Id;
7688
7689 -- No other construct carries this pragma
7690
7691 else
7692 return Extensions_Visible_None;
7693 end if;
7694
7695 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
7696
7697 -- In certain cases analysis may request the Extensions_Visible status
7698 -- of an expression function before the pragma has been analyzed yet.
7699 -- Inspect the declarative items after the expression function looking
7700 -- for the pragma (if any).
7701
7702 if No (Prag) and then Is_Expression_Function (Subp) then
7703 Decl := Next (Unit_Declaration_Node (Subp));
7704 while Present (Decl) loop
7705 if Nkind (Decl) = N_Pragma
7706 and then Pragma_Name (Decl) = Name_Extensions_Visible
7707 then
7708 Prag := Decl;
7709 exit;
7710
7711 -- A source construct ends the region where Extensions_Visible may
7712 -- appear, stop the traversal. An expanded expression function is
7713 -- no longer a source construct, but it must still be recognized.
7714
7715 elsif Comes_From_Source (Decl)
7716 or else
7717 (Nkind_In (Decl, N_Subprogram_Body,
7718 N_Subprogram_Declaration)
7719 and then Is_Expression_Function (Defining_Entity (Decl)))
7720 then
7721 exit;
7722 end if;
7723
7724 Next (Decl);
7725 end loop;
7726 end if;
7727
7728 -- Extract the value from the Boolean expression (if any)
7729
7730 if Present (Prag) then
7731 Arg := First (Pragma_Argument_Associations (Prag));
7732
7733 if Present (Arg) then
7734 Expr := Get_Pragma_Arg (Arg);
7735
7736 -- When the associated subprogram is an expression function, the
7737 -- argument of the pragma may not have been analyzed.
7738
7739 if not Analyzed (Expr) then
7740 Preanalyze_And_Resolve (Expr, Standard_Boolean);
7741 end if;
7742
7743 -- Guard against cascading errors when the argument of pragma
7744 -- Extensions_Visible is not a valid static Boolean expression.
7745
7746 if Error_Posted (Expr) then
7747 return Extensions_Visible_None;
7748
7749 elsif Is_True (Expr_Value (Expr)) then
7750 return Extensions_Visible_True;
7751
7752 else
7753 return Extensions_Visible_False;
7754 end if;
7755
7756 -- Otherwise the aspect or pragma defaults to True
7757
7758 else
7759 return Extensions_Visible_True;
7760 end if;
7761
7762 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
7763 -- directly specified. In SPARK code, its value defaults to "False".
7764
7765 elsif SPARK_Mode = On then
7766 return Extensions_Visible_False;
7767
7768 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
7769 -- "True".
7770
7771 else
7772 return Extensions_Visible_True;
7773 end if;
7774 end Extensions_Visible_Status;
7775
7776 -----------------
7777 -- Find_Actual --
7778 -----------------
7779
7780 procedure Find_Actual
7781 (N : Node_Id;
7782 Formal : out Entity_Id;
7783 Call : out Node_Id)
7784 is
7785 Context : constant Node_Id := Parent (N);
7786 Actual : Node_Id;
7787 Call_Nam : Node_Id;
7788
7789 begin
7790 if Nkind_In (Context, N_Indexed_Component, N_Selected_Component)
7791 and then N = Prefix (Context)
7792 then
7793 Find_Actual (Context, Formal, Call);
7794 return;
7795
7796 elsif Nkind (Context) = N_Parameter_Association
7797 and then N = Explicit_Actual_Parameter (Context)
7798 then
7799 Call := Parent (Context);
7800
7801 elsif Nkind_In (Context, N_Entry_Call_Statement,
7802 N_Function_Call,
7803 N_Procedure_Call_Statement)
7804 then
7805 Call := Context;
7806
7807 else
7808 Formal := Empty;
7809 Call := Empty;
7810 return;
7811 end if;
7812
7813 -- If we have a call to a subprogram look for the parameter. Note that
7814 -- we exclude overloaded calls, since we don't know enough to be sure
7815 -- of giving the right answer in this case.
7816
7817 if Nkind_In (Call, N_Entry_Call_Statement,
7818 N_Function_Call,
7819 N_Procedure_Call_Statement)
7820 then
7821 Call_Nam := Name (Call);
7822
7823 -- A call to a protected or task entry appears as a selected
7824 -- component rather than an expanded name.
7825
7826 if Nkind (Call_Nam) = N_Selected_Component then
7827 Call_Nam := Selector_Name (Call_Nam);
7828 end if;
7829
7830 if Is_Entity_Name (Call_Nam)
7831 and then Present (Entity (Call_Nam))
7832 and then Is_Overloadable (Entity (Call_Nam))
7833 and then not Is_Overloaded (Call_Nam)
7834 then
7835 -- If node is name in call it is not an actual
7836
7837 if N = Call_Nam then
7838 Formal := Empty;
7839 Call := Empty;
7840 return;
7841 end if;
7842
7843 -- Fall here if we are definitely a parameter
7844
7845 Actual := First_Actual (Call);
7846 Formal := First_Formal (Entity (Call_Nam));
7847 while Present (Formal) and then Present (Actual) loop
7848 if Actual = N then
7849 return;
7850
7851 -- An actual that is the prefix in a prefixed call may have
7852 -- been rewritten in the call, after the deferred reference
7853 -- was collected. Check if sloc and kinds and names match.
7854
7855 elsif Sloc (Actual) = Sloc (N)
7856 and then Nkind (Actual) = N_Identifier
7857 and then Nkind (Actual) = Nkind (N)
7858 and then Chars (Actual) = Chars (N)
7859 then
7860 return;
7861
7862 else
7863 Actual := Next_Actual (Actual);
7864 Formal := Next_Formal (Formal);
7865 end if;
7866 end loop;
7867 end if;
7868 end if;
7869
7870 -- Fall through here if we did not find matching actual
7871
7872 Formal := Empty;
7873 Call := Empty;
7874 end Find_Actual;
7875
7876 ---------------------------
7877 -- Find_Body_Discriminal --
7878 ---------------------------
7879
7880 function Find_Body_Discriminal
7881 (Spec_Discriminant : Entity_Id) return Entity_Id
7882 is
7883 Tsk : Entity_Id;
7884 Disc : Entity_Id;
7885
7886 begin
7887 -- If expansion is suppressed, then the scope can be the concurrent type
7888 -- itself rather than a corresponding concurrent record type.
7889
7890 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
7891 Tsk := Scope (Spec_Discriminant);
7892
7893 else
7894 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
7895
7896 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
7897 end if;
7898
7899 -- Find discriminant of original concurrent type, and use its current
7900 -- discriminal, which is the renaming within the task/protected body.
7901
7902 Disc := First_Discriminant (Tsk);
7903 while Present (Disc) loop
7904 if Chars (Disc) = Chars (Spec_Discriminant) then
7905 return Discriminal (Disc);
7906 end if;
7907
7908 Next_Discriminant (Disc);
7909 end loop;
7910
7911 -- That loop should always succeed in finding a matching entry and
7912 -- returning. Fatal error if not.
7913
7914 raise Program_Error;
7915 end Find_Body_Discriminal;
7916
7917 -------------------------------------
7918 -- Find_Corresponding_Discriminant --
7919 -------------------------------------
7920
7921 function Find_Corresponding_Discriminant
7922 (Id : Node_Id;
7923 Typ : Entity_Id) return Entity_Id
7924 is
7925 Par_Disc : Entity_Id;
7926 Old_Disc : Entity_Id;
7927 New_Disc : Entity_Id;
7928
7929 begin
7930 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
7931
7932 -- The original type may currently be private, and the discriminant
7933 -- only appear on its full view.
7934
7935 if Is_Private_Type (Scope (Par_Disc))
7936 and then not Has_Discriminants (Scope (Par_Disc))
7937 and then Present (Full_View (Scope (Par_Disc)))
7938 then
7939 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
7940 else
7941 Old_Disc := First_Discriminant (Scope (Par_Disc));
7942 end if;
7943
7944 if Is_Class_Wide_Type (Typ) then
7945 New_Disc := First_Discriminant (Root_Type (Typ));
7946 else
7947 New_Disc := First_Discriminant (Typ);
7948 end if;
7949
7950 while Present (Old_Disc) and then Present (New_Disc) loop
7951 if Old_Disc = Par_Disc then
7952 return New_Disc;
7953 end if;
7954
7955 Next_Discriminant (Old_Disc);
7956 Next_Discriminant (New_Disc);
7957 end loop;
7958
7959 -- Should always find it
7960
7961 raise Program_Error;
7962 end Find_Corresponding_Discriminant;
7963
7964 -------------------
7965 -- Find_DIC_Type --
7966 -------------------
7967
7968 function Find_DIC_Type (Typ : Entity_Id) return Entity_Id is
7969 Curr_Typ : Entity_Id;
7970 -- The current type being examined in the parent hierarchy traversal
7971
7972 DIC_Typ : Entity_Id;
7973 -- The type which carries the DIC pragma. This variable denotes the
7974 -- partial view when private types are involved.
7975
7976 Par_Typ : Entity_Id;
7977 -- The parent type of the current type. This variable denotes the full
7978 -- view when private types are involved.
7979
7980 begin
7981 -- The input type defines its own DIC pragma, therefore it is the owner
7982
7983 if Has_Own_DIC (Typ) then
7984 DIC_Typ := Typ;
7985
7986 -- Otherwise the DIC pragma is inherited from a parent type
7987
7988 else
7989 pragma Assert (Has_Inherited_DIC (Typ));
7990
7991 -- Climb the parent chain
7992
7993 Curr_Typ := Typ;
7994 loop
7995 -- Inspect the parent type. Do not consider subtypes as they
7996 -- inherit the DIC attributes from their base types.
7997
7998 DIC_Typ := Base_Type (Etype (Curr_Typ));
7999
8000 -- Look at the full view of a private type because the type may
8001 -- have a hidden parent introduced in the full view.
8002
8003 Par_Typ := DIC_Typ;
8004
8005 if Is_Private_Type (Par_Typ)
8006 and then Present (Full_View (Par_Typ))
8007 then
8008 Par_Typ := Full_View (Par_Typ);
8009 end if;
8010
8011 -- Stop the climb once the nearest parent type which defines a DIC
8012 -- pragma of its own is encountered or when the root of the parent
8013 -- chain is reached.
8014
8015 exit when Has_Own_DIC (DIC_Typ) or else Curr_Typ = Par_Typ;
8016
8017 Curr_Typ := Par_Typ;
8018 end loop;
8019 end if;
8020
8021 return DIC_Typ;
8022 end Find_DIC_Type;
8023
8024 ----------------------------------
8025 -- Find_Enclosing_Iterator_Loop --
8026 ----------------------------------
8027
8028 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
8029 Constr : Node_Id;
8030 S : Entity_Id;
8031
8032 begin
8033 -- Traverse the scope chain looking for an iterator loop. Such loops are
8034 -- usually transformed into blocks, hence the use of Original_Node.
8035
8036 S := Id;
8037 while Present (S) and then S /= Standard_Standard loop
8038 if Ekind (S) = E_Loop
8039 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
8040 then
8041 Constr := Original_Node (Label_Construct (Parent (S)));
8042
8043 if Nkind (Constr) = N_Loop_Statement
8044 and then Present (Iteration_Scheme (Constr))
8045 and then Nkind (Iterator_Specification
8046 (Iteration_Scheme (Constr))) =
8047 N_Iterator_Specification
8048 then
8049 return S;
8050 end if;
8051 end if;
8052
8053 S := Scope (S);
8054 end loop;
8055
8056 return Empty;
8057 end Find_Enclosing_Iterator_Loop;
8058
8059 --------------------------
8060 -- Find_Enclosing_Scope --
8061 --------------------------
8062
8063 function Find_Enclosing_Scope (N : Node_Id) return Entity_Id is
8064 Par : Node_Id;
8065
8066 begin
8067 -- Examine the parent chain looking for a construct which defines a
8068 -- scope.
8069
8070 Par := Parent (N);
8071 while Present (Par) loop
8072 case Nkind (Par) is
8073
8074 -- The construct denotes a declaration, the proper scope is its
8075 -- entity.
8076
8077 when N_Entry_Declaration
8078 | N_Expression_Function
8079 | N_Full_Type_Declaration
8080 | N_Generic_Package_Declaration
8081 | N_Generic_Subprogram_Declaration
8082 | N_Package_Declaration
8083 | N_Private_Extension_Declaration
8084 | N_Protected_Type_Declaration
8085 | N_Single_Protected_Declaration
8086 | N_Single_Task_Declaration
8087 | N_Subprogram_Declaration
8088 | N_Task_Type_Declaration
8089 =>
8090 return Defining_Entity (Par);
8091
8092 -- The construct denotes a body, the proper scope is the entity of
8093 -- the corresponding spec or that of the body if the body does not
8094 -- complete a previous declaration.
8095
8096 when N_Entry_Body
8097 | N_Package_Body
8098 | N_Protected_Body
8099 | N_Subprogram_Body
8100 | N_Task_Body
8101 =>
8102 return Unique_Defining_Entity (Par);
8103
8104 -- Special cases
8105
8106 -- Blocks carry either a source or an internally-generated scope,
8107 -- unless the block is a byproduct of exception handling.
8108
8109 when N_Block_Statement =>
8110 if not Exception_Junk (Par) then
8111 return Entity (Identifier (Par));
8112 end if;
8113
8114 -- Loops carry an internally-generated scope
8115
8116 when N_Loop_Statement =>
8117 return Entity (Identifier (Par));
8118
8119 -- Extended return statements carry an internally-generated scope
8120
8121 when N_Extended_Return_Statement =>
8122 return Return_Statement_Entity (Par);
8123
8124 -- A traversal from a subunit continues via the corresponding stub
8125
8126 when N_Subunit =>
8127 Par := Corresponding_Stub (Par);
8128
8129 when others =>
8130 null;
8131 end case;
8132
8133 Par := Parent (Par);
8134 end loop;
8135
8136 return Standard_Standard;
8137 end Find_Enclosing_Scope;
8138
8139 ------------------------------------
8140 -- Find_Loop_In_Conditional_Block --
8141 ------------------------------------
8142
8143 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
8144 Stmt : Node_Id;
8145
8146 begin
8147 Stmt := N;
8148
8149 if Nkind (Stmt) = N_If_Statement then
8150 Stmt := First (Then_Statements (Stmt));
8151 end if;
8152
8153 pragma Assert (Nkind (Stmt) = N_Block_Statement);
8154
8155 -- Inspect the statements of the conditional block. In general the loop
8156 -- should be the first statement in the statement sequence of the block,
8157 -- but the finalization machinery may have introduced extra object
8158 -- declarations.
8159
8160 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
8161 while Present (Stmt) loop
8162 if Nkind (Stmt) = N_Loop_Statement then
8163 return Stmt;
8164 end if;
8165
8166 Next (Stmt);
8167 end loop;
8168
8169 -- The expansion of attribute 'Loop_Entry produced a malformed block
8170
8171 raise Program_Error;
8172 end Find_Loop_In_Conditional_Block;
8173
8174 --------------------------
8175 -- Find_Overlaid_Entity --
8176 --------------------------
8177
8178 procedure Find_Overlaid_Entity
8179 (N : Node_Id;
8180 Ent : out Entity_Id;
8181 Off : out Boolean)
8182 is
8183 Expr : Node_Id;
8184
8185 begin
8186 -- We are looking for one of the two following forms:
8187
8188 -- for X'Address use Y'Address
8189
8190 -- or
8191
8192 -- Const : constant Address := expr;
8193 -- ...
8194 -- for X'Address use Const;
8195
8196 -- In the second case, the expr is either Y'Address, or recursively a
8197 -- constant that eventually references Y'Address.
8198
8199 Ent := Empty;
8200 Off := False;
8201
8202 if Nkind (N) = N_Attribute_Definition_Clause
8203 and then Chars (N) = Name_Address
8204 then
8205 Expr := Expression (N);
8206
8207 -- This loop checks the form of the expression for Y'Address,
8208 -- using recursion to deal with intermediate constants.
8209
8210 loop
8211 -- Check for Y'Address
8212
8213 if Nkind (Expr) = N_Attribute_Reference
8214 and then Attribute_Name (Expr) = Name_Address
8215 then
8216 Expr := Prefix (Expr);
8217 exit;
8218
8219 -- Check for Const where Const is a constant entity
8220
8221 elsif Is_Entity_Name (Expr)
8222 and then Ekind (Entity (Expr)) = E_Constant
8223 then
8224 Expr := Constant_Value (Entity (Expr));
8225
8226 -- Anything else does not need checking
8227
8228 else
8229 return;
8230 end if;
8231 end loop;
8232
8233 -- This loop checks the form of the prefix for an entity, using
8234 -- recursion to deal with intermediate components.
8235
8236 loop
8237 -- Check for Y where Y is an entity
8238
8239 if Is_Entity_Name (Expr) then
8240 Ent := Entity (Expr);
8241 return;
8242
8243 -- Check for components
8244
8245 elsif
8246 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
8247 then
8248 Expr := Prefix (Expr);
8249 Off := True;
8250
8251 -- Anything else does not need checking
8252
8253 else
8254 return;
8255 end if;
8256 end loop;
8257 end if;
8258 end Find_Overlaid_Entity;
8259
8260 -------------------------
8261 -- Find_Parameter_Type --
8262 -------------------------
8263
8264 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
8265 begin
8266 if Nkind (Param) /= N_Parameter_Specification then
8267 return Empty;
8268
8269 -- For an access parameter, obtain the type from the formal entity
8270 -- itself, because access to subprogram nodes do not carry a type.
8271 -- Shouldn't we always use the formal entity ???
8272
8273 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
8274 return Etype (Defining_Identifier (Param));
8275
8276 else
8277 return Etype (Parameter_Type (Param));
8278 end if;
8279 end Find_Parameter_Type;
8280
8281 -----------------------------------
8282 -- Find_Placement_In_State_Space --
8283 -----------------------------------
8284
8285 procedure Find_Placement_In_State_Space
8286 (Item_Id : Entity_Id;
8287 Placement : out State_Space_Kind;
8288 Pack_Id : out Entity_Id)
8289 is
8290 Context : Entity_Id;
8291
8292 begin
8293 -- Assume that the item does not appear in the state space of a package
8294
8295 Placement := Not_In_Package;
8296 Pack_Id := Empty;
8297
8298 -- Climb the scope stack and examine the enclosing context
8299
8300 Context := Scope (Item_Id);
8301 while Present (Context) and then Context /= Standard_Standard loop
8302 if Is_Package_Or_Generic_Package (Context) then
8303 Pack_Id := Context;
8304
8305 -- A package body is a cut off point for the traversal as the item
8306 -- cannot be visible to the outside from this point on. Note that
8307 -- this test must be done first as a body is also classified as a
8308 -- private part.
8309
8310 if In_Package_Body (Context) then
8311 Placement := Body_State_Space;
8312 return;
8313
8314 -- The private part of a package is a cut off point for the
8315 -- traversal as the item cannot be visible to the outside from
8316 -- this point on.
8317
8318 elsif In_Private_Part (Context) then
8319 Placement := Private_State_Space;
8320 return;
8321
8322 -- When the item appears in the visible state space of a package,
8323 -- continue to climb the scope stack as this may not be the final
8324 -- state space.
8325
8326 else
8327 Placement := Visible_State_Space;
8328
8329 -- The visible state space of a child unit acts as the proper
8330 -- placement of an item.
8331
8332 if Is_Child_Unit (Context) then
8333 return;
8334 end if;
8335 end if;
8336
8337 -- The item or its enclosing package appear in a construct that has
8338 -- no state space.
8339
8340 else
8341 Placement := Not_In_Package;
8342 return;
8343 end if;
8344
8345 Context := Scope (Context);
8346 end loop;
8347 end Find_Placement_In_State_Space;
8348
8349 -----------------------
8350 -- Find_Primitive_Eq --
8351 -----------------------
8352
8353 function Find_Primitive_Eq (Typ : Entity_Id) return Entity_Id is
8354 function Find_Eq_Prim (Prims_List : Elist_Id) return Entity_Id;
8355 -- Search for the equality primitive; return Empty if the primitive is
8356 -- not found.
8357
8358 ------------------
8359 -- Find_Eq_Prim --
8360 ------------------
8361
8362 function Find_Eq_Prim (Prims_List : Elist_Id) return Entity_Id is
8363 Prim : Entity_Id;
8364 Prim_Elmt : Elmt_Id;
8365
8366 begin
8367 Prim_Elmt := First_Elmt (Prims_List);
8368 while Present (Prim_Elmt) loop
8369 Prim := Node (Prim_Elmt);
8370
8371 -- Locate primitive equality with the right signature
8372
8373 if Chars (Prim) = Name_Op_Eq
8374 and then Etype (First_Formal (Prim)) =
8375 Etype (Next_Formal (First_Formal (Prim)))
8376 and then Base_Type (Etype (Prim)) = Standard_Boolean
8377 then
8378 return Prim;
8379 end if;
8380
8381 Next_Elmt (Prim_Elmt);
8382 end loop;
8383
8384 return Empty;
8385 end Find_Eq_Prim;
8386
8387 -- Local Variables
8388
8389 Eq_Prim : Entity_Id;
8390 Full_Type : Entity_Id;
8391
8392 -- Start of processing for Find_Primitive_Eq
8393
8394 begin
8395 if Is_Private_Type (Typ) then
8396 Full_Type := Underlying_Type (Typ);
8397 else
8398 Full_Type := Typ;
8399 end if;
8400
8401 if No (Full_Type) then
8402 return Empty;
8403 end if;
8404
8405 Full_Type := Base_Type (Full_Type);
8406
8407 -- When the base type itself is private, use the full view
8408
8409 if Is_Private_Type (Full_Type) then
8410 Full_Type := Underlying_Type (Full_Type);
8411 end if;
8412
8413 if Is_Class_Wide_Type (Full_Type) then
8414 Full_Type := Root_Type (Full_Type);
8415 end if;
8416
8417 if not Is_Tagged_Type (Full_Type) then
8418 Eq_Prim := Find_Eq_Prim (Collect_Primitive_Operations (Typ));
8419
8420 -- If this is an untagged private type completed with a derivation of
8421 -- an untagged private type whose full view is a tagged type, we use
8422 -- the primitive operations of the private parent type (since it does
8423 -- not have a full view, and also because its equality primitive may
8424 -- have been overridden in its untagged full view). If no equality was
8425 -- defined for it then take its dispatching equality primitive.
8426
8427 elsif Inherits_From_Tagged_Full_View (Typ) then
8428 Eq_Prim := Find_Eq_Prim (Collect_Primitive_Operations (Typ));
8429
8430 if No (Eq_Prim) then
8431 Eq_Prim := Find_Eq_Prim (Primitive_Operations (Full_Type));
8432 end if;
8433
8434 else
8435 Eq_Prim := Find_Eq_Prim (Primitive_Operations (Full_Type));
8436 end if;
8437
8438 return Eq_Prim;
8439 end Find_Primitive_Eq;
8440
8441 ------------------------
8442 -- Find_Specific_Type --
8443 ------------------------
8444
8445 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
8446 Typ : Entity_Id := Root_Type (CW);
8447
8448 begin
8449 if Ekind (Typ) = E_Incomplete_Type then
8450 if From_Limited_With (Typ) then
8451 Typ := Non_Limited_View (Typ);
8452 else
8453 Typ := Full_View (Typ);
8454 end if;
8455 end if;
8456
8457 if Is_Private_Type (Typ)
8458 and then not Is_Tagged_Type (Typ)
8459 and then Present (Full_View (Typ))
8460 then
8461 return Full_View (Typ);
8462 else
8463 return Typ;
8464 end if;
8465 end Find_Specific_Type;
8466
8467 -----------------------------
8468 -- Find_Static_Alternative --
8469 -----------------------------
8470
8471 function Find_Static_Alternative (N : Node_Id) return Node_Id is
8472 Expr : constant Node_Id := Expression (N);
8473 Val : constant Uint := Expr_Value (Expr);
8474 Alt : Node_Id;
8475 Choice : Node_Id;
8476
8477 begin
8478 Alt := First (Alternatives (N));
8479
8480 Search : loop
8481 if Nkind (Alt) /= N_Pragma then
8482 Choice := First (Discrete_Choices (Alt));
8483 while Present (Choice) loop
8484
8485 -- Others choice, always matches
8486
8487 if Nkind (Choice) = N_Others_Choice then
8488 exit Search;
8489
8490 -- Range, check if value is in the range
8491
8492 elsif Nkind (Choice) = N_Range then
8493 exit Search when
8494 Val >= Expr_Value (Low_Bound (Choice))
8495 and then
8496 Val <= Expr_Value (High_Bound (Choice));
8497
8498 -- Choice is a subtype name. Note that we know it must
8499 -- be a static subtype, since otherwise it would have
8500 -- been diagnosed as illegal.
8501
8502 elsif Is_Entity_Name (Choice)
8503 and then Is_Type (Entity (Choice))
8504 then
8505 exit Search when Is_In_Range (Expr, Etype (Choice),
8506 Assume_Valid => False);
8507
8508 -- Choice is a subtype indication
8509
8510 elsif Nkind (Choice) = N_Subtype_Indication then
8511 declare
8512 C : constant Node_Id := Constraint (Choice);
8513 R : constant Node_Id := Range_Expression (C);
8514
8515 begin
8516 exit Search when
8517 Val >= Expr_Value (Low_Bound (R))
8518 and then
8519 Val <= Expr_Value (High_Bound (R));
8520 end;
8521
8522 -- Choice is a simple expression
8523
8524 else
8525 exit Search when Val = Expr_Value (Choice);
8526 end if;
8527
8528 Next (Choice);
8529 end loop;
8530 end if;
8531
8532 Next (Alt);
8533 pragma Assert (Present (Alt));
8534 end loop Search;
8535
8536 -- The above loop *must* terminate by finding a match, since we know the
8537 -- case statement is valid, and the value of the expression is known at
8538 -- compile time. When we fall out of the loop, Alt points to the
8539 -- alternative that we know will be selected at run time.
8540
8541 return Alt;
8542 end Find_Static_Alternative;
8543
8544 ------------------
8545 -- First_Actual --
8546 ------------------
8547
8548 function First_Actual (Node : Node_Id) return Node_Id is
8549 N : Node_Id;
8550
8551 begin
8552 if No (Parameter_Associations (Node)) then
8553 return Empty;
8554 end if;
8555
8556 N := First (Parameter_Associations (Node));
8557
8558 if Nkind (N) = N_Parameter_Association then
8559 return First_Named_Actual (Node);
8560 else
8561 return N;
8562 end if;
8563 end First_Actual;
8564
8565 ------------------
8566 -- First_Global --
8567 ------------------
8568
8569 function First_Global
8570 (Subp : Entity_Id;
8571 Global_Mode : Name_Id;
8572 Refined : Boolean := False) return Node_Id
8573 is
8574 function First_From_Global_List
8575 (List : Node_Id;
8576 Global_Mode : Name_Id := Name_Input) return Entity_Id;
8577 -- Get the first item with suitable mode from List
8578
8579 ----------------------------
8580 -- First_From_Global_List --
8581 ----------------------------
8582
8583 function First_From_Global_List
8584 (List : Node_Id;
8585 Global_Mode : Name_Id := Name_Input) return Entity_Id
8586 is
8587 Assoc : Node_Id;
8588
8589 begin
8590 -- Empty list (no global items)
8591
8592 if Nkind (List) = N_Null then
8593 return Empty;
8594
8595 -- Single global item declaration (only input items)
8596
8597 elsif Nkind_In (List, N_Expanded_Name, N_Identifier) then
8598 if Global_Mode = Name_Input then
8599 return List;
8600 else
8601 return Empty;
8602 end if;
8603
8604 -- Simple global list (only input items) or moded global list
8605 -- declaration.
8606
8607 elsif Nkind (List) = N_Aggregate then
8608 if Present (Expressions (List)) then
8609 if Global_Mode = Name_Input then
8610 return First (Expressions (List));
8611 else
8612 return Empty;
8613 end if;
8614
8615 else
8616 Assoc := First (Component_Associations (List));
8617 while Present (Assoc) loop
8618
8619 -- When we find the desired mode in an association, call
8620 -- recursively First_From_Global_List as if the mode was
8621 -- Name_Input, in order to reuse the existing machinery
8622 -- for the other cases.
8623
8624 if Chars (First (Choices (Assoc))) = Global_Mode then
8625 return First_From_Global_List (Expression (Assoc));
8626 end if;
8627
8628 Next (Assoc);
8629 end loop;
8630
8631 return Empty;
8632 end if;
8633
8634 -- To accommodate partial decoration of disabled SPARK features,
8635 -- this routine may be called with illegal input. If this is the
8636 -- case, do not raise Program_Error.
8637
8638 else
8639 return Empty;
8640 end if;
8641 end First_From_Global_List;
8642
8643 -- Local variables
8644
8645 Global : Node_Id := Empty;
8646 Body_Id : Entity_Id;
8647
8648 begin
8649 pragma Assert (Nam_In (Global_Mode, Name_In_Out,
8650 Name_Input,
8651 Name_Output,
8652 Name_Proof_In));
8653
8654 -- Retrieve the suitable pragma Global or Refined_Global. In the second
8655 -- case, it can only be located on the body entity.
8656
8657 if Refined then
8658 Body_Id := Subprogram_Body_Entity (Subp);
8659 if Present (Body_Id) then
8660 Global := Get_Pragma (Body_Id, Pragma_Refined_Global);
8661 end if;
8662 else
8663 Global := Get_Pragma (Subp, Pragma_Global);
8664 end if;
8665
8666 -- No corresponding global if pragma is not present
8667
8668 if No (Global) then
8669 return Empty;
8670
8671 -- Otherwise retrieve the corresponding list of items depending on the
8672 -- Global_Mode.
8673
8674 else
8675 return First_From_Global_List
8676 (Expression (Get_Argument (Global, Subp)), Global_Mode);
8677 end if;
8678 end First_Global;
8679
8680 -------------
8681 -- Fix_Msg --
8682 -------------
8683
8684 function Fix_Msg (Id : Entity_Id; Msg : String) return String is
8685 Is_Task : constant Boolean :=
8686 Ekind_In (Id, E_Task_Body, E_Task_Type)
8687 or else Is_Single_Task_Object (Id);
8688 Msg_Last : constant Natural := Msg'Last;
8689 Msg_Index : Natural;
8690 Res : String (Msg'Range) := (others => ' ');
8691 Res_Index : Natural;
8692
8693 begin
8694 -- Copy all characters from the input message Msg to result Res with
8695 -- suitable replacements.
8696
8697 Msg_Index := Msg'First;
8698 Res_Index := Res'First;
8699 while Msg_Index <= Msg_Last loop
8700
8701 -- Replace "subprogram" with a different word
8702
8703 if Msg_Index <= Msg_Last - 10
8704 and then Msg (Msg_Index .. Msg_Index + 9) = "subprogram"
8705 then
8706 if Ekind_In (Id, E_Entry, E_Entry_Family) then
8707 Res (Res_Index .. Res_Index + 4) := "entry";
8708 Res_Index := Res_Index + 5;
8709
8710 elsif Is_Task then
8711 Res (Res_Index .. Res_Index + 8) := "task type";
8712 Res_Index := Res_Index + 9;
8713
8714 else
8715 Res (Res_Index .. Res_Index + 9) := "subprogram";
8716 Res_Index := Res_Index + 10;
8717 end if;
8718
8719 Msg_Index := Msg_Index + 10;
8720
8721 -- Replace "protected" with a different word
8722
8723 elsif Msg_Index <= Msg_Last - 9
8724 and then Msg (Msg_Index .. Msg_Index + 8) = "protected"
8725 and then Is_Task
8726 then
8727 Res (Res_Index .. Res_Index + 3) := "task";
8728 Res_Index := Res_Index + 4;
8729 Msg_Index := Msg_Index + 9;
8730
8731 -- Otherwise copy the character
8732
8733 else
8734 Res (Res_Index) := Msg (Msg_Index);
8735 Msg_Index := Msg_Index + 1;
8736 Res_Index := Res_Index + 1;
8737 end if;
8738 end loop;
8739
8740 return Res (Res'First .. Res_Index - 1);
8741 end Fix_Msg;
8742
8743 -------------------------
8744 -- From_Nested_Package --
8745 -------------------------
8746
8747 function From_Nested_Package (T : Entity_Id) return Boolean is
8748 Pack : constant Entity_Id := Scope (T);
8749
8750 begin
8751 return
8752 Ekind (Pack) = E_Package
8753 and then not Is_Frozen (Pack)
8754 and then not Scope_Within_Or_Same (Current_Scope, Pack)
8755 and then In_Open_Scopes (Scope (Pack));
8756 end From_Nested_Package;
8757
8758 -----------------------
8759 -- Gather_Components --
8760 -----------------------
8761
8762 procedure Gather_Components
8763 (Typ : Entity_Id;
8764 Comp_List : Node_Id;
8765 Governed_By : List_Id;
8766 Into : Elist_Id;
8767 Report_Errors : out Boolean)
8768 is
8769 Assoc : Node_Id;
8770 Variant : Node_Id;
8771 Discrete_Choice : Node_Id;
8772 Comp_Item : Node_Id;
8773
8774 Discrim : Entity_Id;
8775 Discrim_Name : Node_Id;
8776 Discrim_Value : Node_Id;
8777
8778 begin
8779 Report_Errors := False;
8780
8781 if No (Comp_List) or else Null_Present (Comp_List) then
8782 return;
8783
8784 elsif Present (Component_Items (Comp_List)) then
8785 Comp_Item := First (Component_Items (Comp_List));
8786
8787 else
8788 Comp_Item := Empty;
8789 end if;
8790
8791 while Present (Comp_Item) loop
8792
8793 -- Skip the tag of a tagged record, the interface tags, as well
8794 -- as all items that are not user components (anonymous types,
8795 -- rep clauses, Parent field, controller field).
8796
8797 if Nkind (Comp_Item) = N_Component_Declaration then
8798 declare
8799 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
8800 begin
8801 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
8802 Append_Elmt (Comp, Into);
8803 end if;
8804 end;
8805 end if;
8806
8807 Next (Comp_Item);
8808 end loop;
8809
8810 if No (Variant_Part (Comp_List)) then
8811 return;
8812 else
8813 Discrim_Name := Name (Variant_Part (Comp_List));
8814 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
8815 end if;
8816
8817 -- Look for the discriminant that governs this variant part.
8818 -- The discriminant *must* be in the Governed_By List
8819
8820 Assoc := First (Governed_By);
8821 Find_Constraint : loop
8822 Discrim := First (Choices (Assoc));
8823 exit Find_Constraint when
8824 Chars (Discrim_Name) = Chars (Discrim)
8825 or else
8826 (Present (Corresponding_Discriminant (Entity (Discrim)))
8827 and then Chars (Corresponding_Discriminant
8828 (Entity (Discrim))) = Chars (Discrim_Name))
8829 or else
8830 Chars (Original_Record_Component (Entity (Discrim))) =
8831 Chars (Discrim_Name);
8832
8833 if No (Next (Assoc)) then
8834 if not Is_Constrained (Typ) and then Is_Derived_Type (Typ) then
8835
8836 -- If the type is a tagged type with inherited discriminants,
8837 -- use the stored constraint on the parent in order to find
8838 -- the values of discriminants that are otherwise hidden by an
8839 -- explicit constraint. Renamed discriminants are handled in
8840 -- the code above.
8841
8842 -- If several parent discriminants are renamed by a single
8843 -- discriminant of the derived type, the call to obtain the
8844 -- Corresponding_Discriminant field only retrieves the last
8845 -- of them. We recover the constraint on the others from the
8846 -- Stored_Constraint as well.
8847
8848 -- An inherited discriminant may have been constrained in a
8849 -- later ancestor (not the immediate parent) so we must examine
8850 -- the stored constraint of all of them to locate the inherited
8851 -- value.
8852
8853 declare
8854 C : Elmt_Id;
8855 D : Entity_Id;
8856 T : Entity_Id := Typ;
8857
8858 begin
8859 while Is_Derived_Type (T) loop
8860 if Present (Stored_Constraint (T)) then
8861 D := First_Discriminant (Etype (T));
8862 C := First_Elmt (Stored_Constraint (T));
8863 while Present (D) and then Present (C) loop
8864 if Chars (Discrim_Name) = Chars (D) then
8865 if Is_Entity_Name (Node (C))
8866 and then Entity (Node (C)) = Entity (Discrim)
8867 then
8868 -- D is renamed by Discrim, whose value is
8869 -- given in Assoc.
8870
8871 null;
8872
8873 else
8874 Assoc :=
8875 Make_Component_Association (Sloc (Typ),
8876 New_List
8877 (New_Occurrence_Of (D, Sloc (Typ))),
8878 Duplicate_Subexpr_No_Checks (Node (C)));
8879 end if;
8880
8881 exit Find_Constraint;
8882 end if;
8883
8884 Next_Discriminant (D);
8885 Next_Elmt (C);
8886 end loop;
8887 end if;
8888
8889 -- Discriminant may be inherited from ancestor
8890
8891 T := Etype (T);
8892 end loop;
8893 end;
8894 end if;
8895 end if;
8896
8897 if No (Next (Assoc)) then
8898 Error_Msg_NE
8899 (" missing value for discriminant&",
8900 First (Governed_By), Discrim_Name);
8901
8902 Report_Errors := True;
8903 return;
8904 end if;
8905
8906 Next (Assoc);
8907 end loop Find_Constraint;
8908
8909 Discrim_Value := Expression (Assoc);
8910
8911 if not Is_OK_Static_Expression (Discrim_Value) then
8912
8913 -- If the variant part is governed by a discriminant of the type
8914 -- this is an error. If the variant part and the discriminant are
8915 -- inherited from an ancestor this is legal (AI05-120) unless the
8916 -- components are being gathered for an aggregate, in which case
8917 -- the caller must check Report_Errors.
8918
8919 if Scope (Original_Record_Component
8920 ((Entity (First (Choices (Assoc)))))) = Typ
8921 then
8922 Error_Msg_FE
8923 ("value for discriminant & must be static!",
8924 Discrim_Value, Discrim);
8925 Why_Not_Static (Discrim_Value);
8926 end if;
8927
8928 Report_Errors := True;
8929 return;
8930 end if;
8931
8932 Search_For_Discriminant_Value : declare
8933 Low : Node_Id;
8934 High : Node_Id;
8935
8936 UI_High : Uint;
8937 UI_Low : Uint;
8938 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
8939
8940 begin
8941 Find_Discrete_Value : while Present (Variant) loop
8942 Discrete_Choice := First (Discrete_Choices (Variant));
8943 while Present (Discrete_Choice) loop
8944 exit Find_Discrete_Value when
8945 Nkind (Discrete_Choice) = N_Others_Choice;
8946
8947 Get_Index_Bounds (Discrete_Choice, Low, High);
8948
8949 UI_Low := Expr_Value (Low);
8950 UI_High := Expr_Value (High);
8951
8952 exit Find_Discrete_Value when
8953 UI_Low <= UI_Discrim_Value
8954 and then
8955 UI_High >= UI_Discrim_Value;
8956
8957 Next (Discrete_Choice);
8958 end loop;
8959
8960 Next_Non_Pragma (Variant);
8961 end loop Find_Discrete_Value;
8962 end Search_For_Discriminant_Value;
8963
8964 -- The case statement must include a variant that corresponds to the
8965 -- value of the discriminant, unless the discriminant type has a
8966 -- static predicate. In that case the absence of an others_choice that
8967 -- would cover this value becomes a run-time error (3.8,1 (21.1/2)).
8968
8969 if No (Variant)
8970 and then not Has_Static_Predicate (Etype (Discrim_Name))
8971 then
8972 Error_Msg_NE
8973 ("value of discriminant & is out of range", Discrim_Value, Discrim);
8974 Report_Errors := True;
8975 return;
8976 end if;
8977
8978 -- If we have found the corresponding choice, recursively add its
8979 -- components to the Into list. The nested components are part of
8980 -- the same record type.
8981
8982 if Present (Variant) then
8983 Gather_Components
8984 (Typ, Component_List (Variant), Governed_By, Into, Report_Errors);
8985 end if;
8986 end Gather_Components;
8987
8988 ------------------------
8989 -- Get_Actual_Subtype --
8990 ------------------------
8991
8992 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
8993 Typ : constant Entity_Id := Etype (N);
8994 Utyp : Entity_Id := Underlying_Type (Typ);
8995 Decl : Node_Id;
8996 Atyp : Entity_Id;
8997
8998 begin
8999 if No (Utyp) then
9000 Utyp := Typ;
9001 end if;
9002
9003 -- If what we have is an identifier that references a subprogram
9004 -- formal, or a variable or constant object, then we get the actual
9005 -- subtype from the referenced entity if one has been built.
9006
9007 if Nkind (N) = N_Identifier
9008 and then
9009 (Is_Formal (Entity (N))
9010 or else Ekind (Entity (N)) = E_Constant
9011 or else Ekind (Entity (N)) = E_Variable)
9012 and then Present (Actual_Subtype (Entity (N)))
9013 then
9014 return Actual_Subtype (Entity (N));
9015
9016 -- Actual subtype of unchecked union is always itself. We never need
9017 -- the "real" actual subtype. If we did, we couldn't get it anyway
9018 -- because the discriminant is not available. The restrictions on
9019 -- Unchecked_Union are designed to make sure that this is OK.
9020
9021 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
9022 return Typ;
9023
9024 -- Here for the unconstrained case, we must find actual subtype
9025 -- No actual subtype is available, so we must build it on the fly.
9026
9027 -- Checking the type, not the underlying type, for constrainedness
9028 -- seems to be necessary. Maybe all the tests should be on the type???
9029
9030 elsif (not Is_Constrained (Typ))
9031 and then (Is_Array_Type (Utyp)
9032 or else (Is_Record_Type (Utyp)
9033 and then Has_Discriminants (Utyp)))
9034 and then not Has_Unknown_Discriminants (Utyp)
9035 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
9036 then
9037 -- Nothing to do if in spec expression (why not???)
9038
9039 if In_Spec_Expression then
9040 return Typ;
9041
9042 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
9043
9044 -- If the type has no discriminants, there is no subtype to
9045 -- build, even if the underlying type is discriminated.
9046
9047 return Typ;
9048
9049 -- Else build the actual subtype
9050
9051 else
9052 Decl := Build_Actual_Subtype (Typ, N);
9053
9054 -- The call may yield a declaration, or just return the entity
9055
9056 if Decl = Typ then
9057 return Typ;
9058 end if;
9059
9060 Atyp := Defining_Identifier (Decl);
9061
9062 -- If Build_Actual_Subtype generated a new declaration then use it
9063
9064 if Atyp /= Typ then
9065
9066 -- The actual subtype is an Itype, so analyze the declaration,
9067 -- but do not attach it to the tree, to get the type defined.
9068
9069 Set_Parent (Decl, N);
9070 Set_Is_Itype (Atyp);
9071 Analyze (Decl, Suppress => All_Checks);
9072 Set_Associated_Node_For_Itype (Atyp, N);
9073 Set_Has_Delayed_Freeze (Atyp, False);
9074
9075 -- We need to freeze the actual subtype immediately. This is
9076 -- needed, because otherwise this Itype will not get frozen
9077 -- at all, and it is always safe to freeze on creation because
9078 -- any associated types must be frozen at this point.
9079
9080 Freeze_Itype (Atyp, N);
9081 return Atyp;
9082
9083 -- Otherwise we did not build a declaration, so return original
9084
9085 else
9086 return Typ;
9087 end if;
9088 end if;
9089
9090 -- For all remaining cases, the actual subtype is the same as
9091 -- the nominal type.
9092
9093 else
9094 return Typ;
9095 end if;
9096 end Get_Actual_Subtype;
9097
9098 -------------------------------------
9099 -- Get_Actual_Subtype_If_Available --
9100 -------------------------------------
9101
9102 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
9103 Typ : constant Entity_Id := Etype (N);
9104
9105 begin
9106 -- If what we have is an identifier that references a subprogram
9107 -- formal, or a variable or constant object, then we get the actual
9108 -- subtype from the referenced entity if one has been built.
9109
9110 if Nkind (N) = N_Identifier
9111 and then
9112 (Is_Formal (Entity (N))
9113 or else Ekind (Entity (N)) = E_Constant
9114 or else Ekind (Entity (N)) = E_Variable)
9115 and then Present (Actual_Subtype (Entity (N)))
9116 then
9117 return Actual_Subtype (Entity (N));
9118
9119 -- Otherwise the Etype of N is returned unchanged
9120
9121 else
9122 return Typ;
9123 end if;
9124 end Get_Actual_Subtype_If_Available;
9125
9126 ------------------------
9127 -- Get_Body_From_Stub --
9128 ------------------------
9129
9130 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
9131 begin
9132 return Proper_Body (Unit (Library_Unit (N)));
9133 end Get_Body_From_Stub;
9134
9135 ---------------------
9136 -- Get_Cursor_Type --
9137 ---------------------
9138
9139 function Get_Cursor_Type
9140 (Aspect : Node_Id;
9141 Typ : Entity_Id) return Entity_Id
9142 is
9143 Assoc : Node_Id;
9144 Func : Entity_Id;
9145 First_Op : Entity_Id;
9146 Cursor : Entity_Id;
9147
9148 begin
9149 -- If error already detected, return
9150
9151 if Error_Posted (Aspect) then
9152 return Any_Type;
9153 end if;
9154
9155 -- The cursor type for an Iterable aspect is the return type of a
9156 -- non-overloaded First primitive operation. Locate association for
9157 -- First.
9158
9159 Assoc := First (Component_Associations (Expression (Aspect)));
9160 First_Op := Any_Id;
9161 while Present (Assoc) loop
9162 if Chars (First (Choices (Assoc))) = Name_First then
9163 First_Op := Expression (Assoc);
9164 exit;
9165 end if;
9166
9167 Next (Assoc);
9168 end loop;
9169
9170 if First_Op = Any_Id then
9171 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
9172 return Any_Type;
9173
9174 elsif not Analyzed (First_Op) then
9175 Analyze (First_Op);
9176 end if;
9177
9178 Cursor := Any_Type;
9179
9180 -- Locate function with desired name and profile in scope of type
9181 -- In the rare case where the type is an integer type, a base type
9182 -- is created for it, check that the base type of the first formal
9183 -- of First matches the base type of the domain.
9184
9185 Func := First_Entity (Scope (Typ));
9186 while Present (Func) loop
9187 if Chars (Func) = Chars (First_Op)
9188 and then Ekind (Func) = E_Function
9189 and then Present (First_Formal (Func))
9190 and then Base_Type (Etype (First_Formal (Func))) = Base_Type (Typ)
9191 and then No (Next_Formal (First_Formal (Func)))
9192 then
9193 if Cursor /= Any_Type then
9194 Error_Msg_N
9195 ("Operation First for iterable type must be unique", Aspect);
9196 return Any_Type;
9197 else
9198 Cursor := Etype (Func);
9199 end if;
9200 end if;
9201
9202 Next_Entity (Func);
9203 end loop;
9204
9205 -- If not found, no way to resolve remaining primitives
9206
9207 if Cursor = Any_Type then
9208 Error_Msg_N
9209 ("primitive operation for Iterable type must appear in the same "
9210 & "list of declarations as the type", Aspect);
9211 end if;
9212
9213 return Cursor;
9214 end Get_Cursor_Type;
9215
9216 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
9217 begin
9218 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
9219 end Get_Cursor_Type;
9220
9221 -------------------------------
9222 -- Get_Default_External_Name --
9223 -------------------------------
9224
9225 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
9226 begin
9227 Get_Decoded_Name_String (Chars (E));
9228
9229 if Opt.External_Name_Imp_Casing = Uppercase then
9230 Set_Casing (All_Upper_Case);
9231 else
9232 Set_Casing (All_Lower_Case);
9233 end if;
9234
9235 return
9236 Make_String_Literal (Sloc (E),
9237 Strval => String_From_Name_Buffer);
9238 end Get_Default_External_Name;
9239
9240 --------------------------
9241 -- Get_Enclosing_Object --
9242 --------------------------
9243
9244 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
9245 begin
9246 if Is_Entity_Name (N) then
9247 return Entity (N);
9248 else
9249 case Nkind (N) is
9250 when N_Indexed_Component
9251 | N_Selected_Component
9252 | N_Slice
9253 =>
9254 -- If not generating code, a dereference may be left implicit.
9255 -- In thoses cases, return Empty.
9256
9257 if Is_Access_Type (Etype (Prefix (N))) then
9258 return Empty;
9259 else
9260 return Get_Enclosing_Object (Prefix (N));
9261 end if;
9262
9263 when N_Type_Conversion =>
9264 return Get_Enclosing_Object (Expression (N));
9265
9266 when others =>
9267 return Empty;
9268 end case;
9269 end if;
9270 end Get_Enclosing_Object;
9271
9272 ---------------------------
9273 -- Get_Enum_Lit_From_Pos --
9274 ---------------------------
9275
9276 function Get_Enum_Lit_From_Pos
9277 (T : Entity_Id;
9278 Pos : Uint;
9279 Loc : Source_Ptr) return Node_Id
9280 is
9281 Btyp : Entity_Id := Base_Type (T);
9282 Lit : Node_Id;
9283 LLoc : Source_Ptr;
9284
9285 begin
9286 -- In the case where the literal is of type Character, Wide_Character
9287 -- or Wide_Wide_Character or of a type derived from them, there needs
9288 -- to be some special handling since there is no explicit chain of
9289 -- literals to search. Instead, an N_Character_Literal node is created
9290 -- with the appropriate Char_Code and Chars fields.
9291
9292 if Is_Standard_Character_Type (T) then
9293 Set_Character_Literal_Name (UI_To_CC (Pos));
9294
9295 return
9296 Make_Character_Literal (Loc,
9297 Chars => Name_Find,
9298 Char_Literal_Value => Pos);
9299
9300 -- For all other cases, we have a complete table of literals, and
9301 -- we simply iterate through the chain of literal until the one
9302 -- with the desired position value is found.
9303
9304 else
9305 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
9306 Btyp := Full_View (Btyp);
9307 end if;
9308
9309 Lit := First_Literal (Btyp);
9310
9311 -- Position in the enumeration type starts at 0
9312
9313 if UI_To_Int (Pos) < 0 then
9314 raise Constraint_Error;
9315 end if;
9316
9317 for J in 1 .. UI_To_Int (Pos) loop
9318 Next_Literal (Lit);
9319
9320 -- If Lit is Empty, Pos is not in range, so raise Constraint_Error
9321 -- inside the loop to avoid calling Next_Literal on Empty.
9322
9323 if No (Lit) then
9324 raise Constraint_Error;
9325 end if;
9326 end loop;
9327
9328 -- Create a new node from Lit, with source location provided by Loc
9329 -- if not equal to No_Location, or by copying the source location of
9330 -- Lit otherwise.
9331
9332 LLoc := Loc;
9333
9334 if LLoc = No_Location then
9335 LLoc := Sloc (Lit);
9336 end if;
9337
9338 return New_Occurrence_Of (Lit, LLoc);
9339 end if;
9340 end Get_Enum_Lit_From_Pos;
9341
9342 ------------------------
9343 -- Get_Generic_Entity --
9344 ------------------------
9345
9346 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
9347 Ent : constant Entity_Id := Entity (Name (N));
9348 begin
9349 if Present (Renamed_Object (Ent)) then
9350 return Renamed_Object (Ent);
9351 else
9352 return Ent;
9353 end if;
9354 end Get_Generic_Entity;
9355
9356 -------------------------------------
9357 -- Get_Incomplete_View_Of_Ancestor --
9358 -------------------------------------
9359
9360 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
9361 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9362 Par_Scope : Entity_Id;
9363 Par_Type : Entity_Id;
9364
9365 begin
9366 -- The incomplete view of an ancestor is only relevant for private
9367 -- derived types in child units.
9368
9369 if not Is_Derived_Type (E)
9370 or else not Is_Child_Unit (Cur_Unit)
9371 then
9372 return Empty;
9373
9374 else
9375 Par_Scope := Scope (Cur_Unit);
9376 if No (Par_Scope) then
9377 return Empty;
9378 end if;
9379
9380 Par_Type := Etype (Base_Type (E));
9381
9382 -- Traverse list of ancestor types until we find one declared in
9383 -- a parent or grandparent unit (two levels seem sufficient).
9384
9385 while Present (Par_Type) loop
9386 if Scope (Par_Type) = Par_Scope
9387 or else Scope (Par_Type) = Scope (Par_Scope)
9388 then
9389 return Par_Type;
9390
9391 elsif not Is_Derived_Type (Par_Type) then
9392 return Empty;
9393
9394 else
9395 Par_Type := Etype (Base_Type (Par_Type));
9396 end if;
9397 end loop;
9398
9399 -- If none found, there is no relevant ancestor type.
9400
9401 return Empty;
9402 end if;
9403 end Get_Incomplete_View_Of_Ancestor;
9404
9405 ----------------------
9406 -- Get_Index_Bounds --
9407 ----------------------
9408
9409 procedure Get_Index_Bounds
9410 (N : Node_Id;
9411 L : out Node_Id;
9412 H : out Node_Id;
9413 Use_Full_View : Boolean := False)
9414 is
9415 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id;
9416 -- Obtain the scalar range of type Typ. If flag Use_Full_View is set and
9417 -- Typ qualifies, the scalar range is obtained from the full view of the
9418 -- type.
9419
9420 --------------------------
9421 -- Scalar_Range_Of_Type --
9422 --------------------------
9423
9424 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id is
9425 T : Entity_Id := Typ;
9426
9427 begin
9428 if Use_Full_View and then Present (Full_View (T)) then
9429 T := Full_View (T);
9430 end if;
9431
9432 return Scalar_Range (T);
9433 end Scalar_Range_Of_Type;
9434
9435 -- Local variables
9436
9437 Kind : constant Node_Kind := Nkind (N);
9438 Rng : Node_Id;
9439
9440 -- Start of processing for Get_Index_Bounds
9441
9442 begin
9443 if Kind = N_Range then
9444 L := Low_Bound (N);
9445 H := High_Bound (N);
9446
9447 elsif Kind = N_Subtype_Indication then
9448 Rng := Range_Expression (Constraint (N));
9449
9450 if Rng = Error then
9451 L := Error;
9452 H := Error;
9453 return;
9454
9455 else
9456 L := Low_Bound (Range_Expression (Constraint (N)));
9457 H := High_Bound (Range_Expression (Constraint (N)));
9458 end if;
9459
9460 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9461 Rng := Scalar_Range_Of_Type (Entity (N));
9462
9463 if Error_Posted (Rng) then
9464 L := Error;
9465 H := Error;
9466
9467 elsif Nkind (Rng) = N_Subtype_Indication then
9468 Get_Index_Bounds (Rng, L, H);
9469
9470 else
9471 L := Low_Bound (Rng);
9472 H := High_Bound (Rng);
9473 end if;
9474
9475 else
9476 -- N is an expression, indicating a range with one value
9477
9478 L := N;
9479 H := N;
9480 end if;
9481 end Get_Index_Bounds;
9482
9483 -----------------------------
9484 -- Get_Interfacing_Aspects --
9485 -----------------------------
9486
9487 procedure Get_Interfacing_Aspects
9488 (Iface_Asp : Node_Id;
9489 Conv_Asp : out Node_Id;
9490 EN_Asp : out Node_Id;
9491 Expo_Asp : out Node_Id;
9492 Imp_Asp : out Node_Id;
9493 LN_Asp : out Node_Id;
9494 Do_Checks : Boolean := False)
9495 is
9496 procedure Save_Or_Duplication_Error
9497 (Asp : Node_Id;
9498 To : in out Node_Id);
9499 -- Save the value of aspect Asp in node To. If To already has a value,
9500 -- then this is considered a duplicate use of aspect. Emit an error if
9501 -- flag Do_Checks is set.
9502
9503 -------------------------------
9504 -- Save_Or_Duplication_Error --
9505 -------------------------------
9506
9507 procedure Save_Or_Duplication_Error
9508 (Asp : Node_Id;
9509 To : in out Node_Id)
9510 is
9511 begin
9512 -- Detect an extra aspect and issue an error
9513
9514 if Present (To) then
9515 if Do_Checks then
9516 Error_Msg_Name_1 := Chars (Identifier (Asp));
9517 Error_Msg_Sloc := Sloc (To);
9518 Error_Msg_N ("aspect % previously given #", Asp);
9519 end if;
9520
9521 -- Otherwise capture the aspect
9522
9523 else
9524 To := Asp;
9525 end if;
9526 end Save_Or_Duplication_Error;
9527
9528 -- Local variables
9529
9530 Asp : Node_Id;
9531 Asp_Id : Aspect_Id;
9532
9533 -- The following variables capture each individual aspect
9534
9535 Conv : Node_Id := Empty;
9536 EN : Node_Id := Empty;
9537 Expo : Node_Id := Empty;
9538 Imp : Node_Id := Empty;
9539 LN : Node_Id := Empty;
9540
9541 -- Start of processing for Get_Interfacing_Aspects
9542
9543 begin
9544 -- The input interfacing aspect should reside in an aspect specification
9545 -- list.
9546
9547 pragma Assert (Is_List_Member (Iface_Asp));
9548
9549 -- Examine the aspect specifications of the related entity. Find and
9550 -- capture all interfacing aspects. Detect duplicates and emit errors
9551 -- if applicable.
9552
9553 Asp := First (List_Containing (Iface_Asp));
9554 while Present (Asp) loop
9555 Asp_Id := Get_Aspect_Id (Asp);
9556
9557 if Asp_Id = Aspect_Convention then
9558 Save_Or_Duplication_Error (Asp, Conv);
9559
9560 elsif Asp_Id = Aspect_External_Name then
9561 Save_Or_Duplication_Error (Asp, EN);
9562
9563 elsif Asp_Id = Aspect_Export then
9564 Save_Or_Duplication_Error (Asp, Expo);
9565
9566 elsif Asp_Id = Aspect_Import then
9567 Save_Or_Duplication_Error (Asp, Imp);
9568
9569 elsif Asp_Id = Aspect_Link_Name then
9570 Save_Or_Duplication_Error (Asp, LN);
9571 end if;
9572
9573 Next (Asp);
9574 end loop;
9575
9576 Conv_Asp := Conv;
9577 EN_Asp := EN;
9578 Expo_Asp := Expo;
9579 Imp_Asp := Imp;
9580 LN_Asp := LN;
9581 end Get_Interfacing_Aspects;
9582
9583 ---------------------------------
9584 -- Get_Iterable_Type_Primitive --
9585 ---------------------------------
9586
9587 function Get_Iterable_Type_Primitive
9588 (Typ : Entity_Id;
9589 Nam : Name_Id) return Entity_Id
9590 is
9591 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
9592 Assoc : Node_Id;
9593
9594 begin
9595 if No (Funcs) then
9596 return Empty;
9597
9598 else
9599 Assoc := First (Component_Associations (Funcs));
9600 while Present (Assoc) loop
9601 if Chars (First (Choices (Assoc))) = Nam then
9602 return Entity (Expression (Assoc));
9603 end if;
9604
9605 Assoc := Next (Assoc);
9606 end loop;
9607
9608 return Empty;
9609 end if;
9610 end Get_Iterable_Type_Primitive;
9611
9612 ----------------------------------
9613 -- Get_Library_Unit_Name_String --
9614 ----------------------------------
9615
9616 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
9617 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
9618
9619 begin
9620 Get_Unit_Name_String (Unit_Name_Id);
9621
9622 -- Remove seven last character (" (spec)" or " (body)")
9623
9624 Name_Len := Name_Len - 7;
9625 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
9626 end Get_Library_Unit_Name_String;
9627
9628 --------------------------
9629 -- Get_Max_Queue_Length --
9630 --------------------------
9631
9632 function Get_Max_Queue_Length (Id : Entity_Id) return Uint is
9633 pragma Assert (Is_Entry (Id));
9634 Prag : constant Entity_Id := Get_Pragma (Id, Pragma_Max_Queue_Length);
9635
9636 begin
9637 -- A value of 0 represents no maximum specified, and entries and entry
9638 -- families with no Max_Queue_Length aspect or pragma default to it.
9639
9640 if not Present (Prag) then
9641 return Uint_0;
9642 end if;
9643
9644 return Intval (Expression (First (Pragma_Argument_Associations (Prag))));
9645 end Get_Max_Queue_Length;
9646
9647 ------------------------
9648 -- Get_Name_Entity_Id --
9649 ------------------------
9650
9651 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
9652 begin
9653 return Entity_Id (Get_Name_Table_Int (Id));
9654 end Get_Name_Entity_Id;
9655
9656 ------------------------------
9657 -- Get_Name_From_CTC_Pragma --
9658 ------------------------------
9659
9660 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
9661 Arg : constant Node_Id :=
9662 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
9663 begin
9664 return Strval (Expr_Value_S (Arg));
9665 end Get_Name_From_CTC_Pragma;
9666
9667 -----------------------
9668 -- Get_Parent_Entity --
9669 -----------------------
9670
9671 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
9672 begin
9673 if Nkind (Unit) = N_Package_Body
9674 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
9675 then
9676 return Defining_Entity
9677 (Specification (Instance_Spec (Original_Node (Unit))));
9678 elsif Nkind (Unit) = N_Package_Instantiation then
9679 return Defining_Entity (Specification (Instance_Spec (Unit)));
9680 else
9681 return Defining_Entity (Unit);
9682 end if;
9683 end Get_Parent_Entity;
9684
9685 -------------------
9686 -- Get_Pragma_Id --
9687 -------------------
9688
9689 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
9690 begin
9691 return Get_Pragma_Id (Pragma_Name_Unmapped (N));
9692 end Get_Pragma_Id;
9693
9694 ------------------------
9695 -- Get_Qualified_Name --
9696 ------------------------
9697
9698 function Get_Qualified_Name
9699 (Id : Entity_Id;
9700 Suffix : Entity_Id := Empty) return Name_Id
9701 is
9702 Suffix_Nam : Name_Id := No_Name;
9703
9704 begin
9705 if Present (Suffix) then
9706 Suffix_Nam := Chars (Suffix);
9707 end if;
9708
9709 return Get_Qualified_Name (Chars (Id), Suffix_Nam, Scope (Id));
9710 end Get_Qualified_Name;
9711
9712 function Get_Qualified_Name
9713 (Nam : Name_Id;
9714 Suffix : Name_Id := No_Name;
9715 Scop : Entity_Id := Current_Scope) return Name_Id
9716 is
9717 procedure Add_Scope (S : Entity_Id);
9718 -- Add the fully qualified form of scope S to the name buffer. The
9719 -- format is:
9720 -- s-1__s__
9721
9722 ---------------
9723 -- Add_Scope --
9724 ---------------
9725
9726 procedure Add_Scope (S : Entity_Id) is
9727 begin
9728 if S = Empty then
9729 null;
9730
9731 elsif S = Standard_Standard then
9732 null;
9733
9734 else
9735 Add_Scope (Scope (S));
9736 Get_Name_String_And_Append (Chars (S));
9737 Add_Str_To_Name_Buffer ("__");
9738 end if;
9739 end Add_Scope;
9740
9741 -- Start of processing for Get_Qualified_Name
9742
9743 begin
9744 Name_Len := 0;
9745 Add_Scope (Scop);
9746
9747 -- Append the base name after all scopes have been chained
9748
9749 Get_Name_String_And_Append (Nam);
9750
9751 -- Append the suffix (if present)
9752
9753 if Suffix /= No_Name then
9754 Add_Str_To_Name_Buffer ("__");
9755 Get_Name_String_And_Append (Suffix);
9756 end if;
9757
9758 return Name_Find;
9759 end Get_Qualified_Name;
9760
9761 -----------------------
9762 -- Get_Reason_String --
9763 -----------------------
9764
9765 procedure Get_Reason_String (N : Node_Id) is
9766 begin
9767 if Nkind (N) = N_String_Literal then
9768 Store_String_Chars (Strval (N));
9769
9770 elsif Nkind (N) = N_Op_Concat then
9771 Get_Reason_String (Left_Opnd (N));
9772 Get_Reason_String (Right_Opnd (N));
9773
9774 -- If not of required form, error
9775
9776 else
9777 Error_Msg_N
9778 ("Reason for pragma Warnings has wrong form", N);
9779 Error_Msg_N
9780 ("\must be string literal or concatenation of string literals", N);
9781 return;
9782 end if;
9783 end Get_Reason_String;
9784
9785 --------------------------------
9786 -- Get_Reference_Discriminant --
9787 --------------------------------
9788
9789 function Get_Reference_Discriminant (Typ : Entity_Id) return Entity_Id is
9790 D : Entity_Id;
9791
9792 begin
9793 D := First_Discriminant (Typ);
9794 while Present (D) loop
9795 if Has_Implicit_Dereference (D) then
9796 return D;
9797 end if;
9798 Next_Discriminant (D);
9799 end loop;
9800
9801 return Empty;
9802 end Get_Reference_Discriminant;
9803
9804 ---------------------------
9805 -- Get_Referenced_Object --
9806 ---------------------------
9807
9808 function Get_Referenced_Object (N : Node_Id) return Node_Id is
9809 R : Node_Id;
9810
9811 begin
9812 R := N;
9813 while Is_Entity_Name (R)
9814 and then Present (Renamed_Object (Entity (R)))
9815 loop
9816 R := Renamed_Object (Entity (R));
9817 end loop;
9818
9819 return R;
9820 end Get_Referenced_Object;
9821
9822 ------------------------
9823 -- Get_Renamed_Entity --
9824 ------------------------
9825
9826 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
9827 R : Entity_Id;
9828
9829 begin
9830 R := E;
9831 while Present (Renamed_Entity (R)) loop
9832 R := Renamed_Entity (R);
9833 end loop;
9834
9835 return R;
9836 end Get_Renamed_Entity;
9837
9838 -----------------------
9839 -- Get_Return_Object --
9840 -----------------------
9841
9842 function Get_Return_Object (N : Node_Id) return Entity_Id is
9843 Decl : Node_Id;
9844
9845 begin
9846 Decl := First (Return_Object_Declarations (N));
9847 while Present (Decl) loop
9848 exit when Nkind (Decl) = N_Object_Declaration
9849 and then Is_Return_Object (Defining_Identifier (Decl));
9850 Next (Decl);
9851 end loop;
9852
9853 pragma Assert (Present (Decl));
9854 return Defining_Identifier (Decl);
9855 end Get_Return_Object;
9856
9857 ---------------------------
9858 -- Get_Subprogram_Entity --
9859 ---------------------------
9860
9861 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
9862 Subp : Node_Id;
9863 Subp_Id : Entity_Id;
9864
9865 begin
9866 if Nkind (Nod) = N_Accept_Statement then
9867 Subp := Entry_Direct_Name (Nod);
9868
9869 elsif Nkind (Nod) = N_Slice then
9870 Subp := Prefix (Nod);
9871
9872 else
9873 Subp := Name (Nod);
9874 end if;
9875
9876 -- Strip the subprogram call
9877
9878 loop
9879 if Nkind_In (Subp, N_Explicit_Dereference,
9880 N_Indexed_Component,
9881 N_Selected_Component)
9882 then
9883 Subp := Prefix (Subp);
9884
9885 elsif Nkind_In (Subp, N_Type_Conversion,
9886 N_Unchecked_Type_Conversion)
9887 then
9888 Subp := Expression (Subp);
9889
9890 else
9891 exit;
9892 end if;
9893 end loop;
9894
9895 -- Extract the entity of the subprogram call
9896
9897 if Is_Entity_Name (Subp) then
9898 Subp_Id := Entity (Subp);
9899
9900 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
9901 Subp_Id := Directly_Designated_Type (Subp_Id);
9902 end if;
9903
9904 if Is_Subprogram (Subp_Id) then
9905 return Subp_Id;
9906 else
9907 return Empty;
9908 end if;
9909
9910 -- The search did not find a construct that denotes a subprogram
9911
9912 else
9913 return Empty;
9914 end if;
9915 end Get_Subprogram_Entity;
9916
9917 -----------------------------
9918 -- Get_Task_Body_Procedure --
9919 -----------------------------
9920
9921 function Get_Task_Body_Procedure (E : Entity_Id) return Entity_Id is
9922 begin
9923 -- Note: A task type may be the completion of a private type with
9924 -- discriminants. When performing elaboration checks on a task
9925 -- declaration, the current view of the type may be the private one,
9926 -- and the procedure that holds the body of the task is held in its
9927 -- underlying type.
9928
9929 -- This is an odd function, why not have Task_Body_Procedure do
9930 -- the following digging???
9931
9932 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
9933 end Get_Task_Body_Procedure;
9934
9935 -------------------------
9936 -- Get_User_Defined_Eq --
9937 -------------------------
9938
9939 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
9940 Prim : Elmt_Id;
9941 Op : Entity_Id;
9942
9943 begin
9944 Prim := First_Elmt (Collect_Primitive_Operations (E));
9945 while Present (Prim) loop
9946 Op := Node (Prim);
9947
9948 if Chars (Op) = Name_Op_Eq
9949 and then Etype (Op) = Standard_Boolean
9950 and then Etype (First_Formal (Op)) = E
9951 and then Etype (Next_Formal (First_Formal (Op))) = E
9952 then
9953 return Op;
9954 end if;
9955
9956 Next_Elmt (Prim);
9957 end loop;
9958
9959 return Empty;
9960 end Get_User_Defined_Eq;
9961
9962 ---------------
9963 -- Get_Views --
9964 ---------------
9965
9966 procedure Get_Views
9967 (Typ : Entity_Id;
9968 Priv_Typ : out Entity_Id;
9969 Full_Typ : out Entity_Id;
9970 Full_Base : out Entity_Id;
9971 CRec_Typ : out Entity_Id)
9972 is
9973 IP_View : Entity_Id;
9974
9975 begin
9976 -- Assume that none of the views can be recovered
9977
9978 Priv_Typ := Empty;
9979 Full_Typ := Empty;
9980 Full_Base := Empty;
9981 CRec_Typ := Empty;
9982
9983 -- The input type is the corresponding record type of a protected or a
9984 -- task type.
9985
9986 if Ekind (Typ) = E_Record_Type
9987 and then Is_Concurrent_Record_Type (Typ)
9988 then
9989 CRec_Typ := Typ;
9990 Full_Typ := Corresponding_Concurrent_Type (CRec_Typ);
9991 Full_Base := Base_Type (Full_Typ);
9992 Priv_Typ := Incomplete_Or_Partial_View (Full_Typ);
9993
9994 -- Otherwise the input type denotes an arbitrary type
9995
9996 else
9997 IP_View := Incomplete_Or_Partial_View (Typ);
9998
9999 -- The input type denotes the full view of a private type
10000
10001 if Present (IP_View) then
10002 Priv_Typ := IP_View;
10003 Full_Typ := Typ;
10004
10005 -- The input type is a private type
10006
10007 elsif Is_Private_Type (Typ) then
10008 Priv_Typ := Typ;
10009 Full_Typ := Full_View (Priv_Typ);
10010
10011 -- Otherwise the input type does not have any views
10012
10013 else
10014 Full_Typ := Typ;
10015 end if;
10016
10017 if Present (Full_Typ) then
10018 Full_Base := Base_Type (Full_Typ);
10019
10020 if Ekind_In (Full_Typ, E_Protected_Type, E_Task_Type) then
10021 CRec_Typ := Corresponding_Record_Type (Full_Typ);
10022 end if;
10023 end if;
10024 end if;
10025 end Get_Views;
10026
10027 -----------------------
10028 -- Has_Access_Values --
10029 -----------------------
10030
10031 function Has_Access_Values (T : Entity_Id) return Boolean is
10032 Typ : constant Entity_Id := Underlying_Type (T);
10033
10034 begin
10035 -- Case of a private type which is not completed yet. This can only
10036 -- happen in the case of a generic format type appearing directly, or
10037 -- as a component of the type to which this function is being applied
10038 -- at the top level. Return False in this case, since we certainly do
10039 -- not know that the type contains access types.
10040
10041 if No (Typ) then
10042 return False;
10043
10044 elsif Is_Access_Type (Typ) then
10045 return True;
10046
10047 elsif Is_Array_Type (Typ) then
10048 return Has_Access_Values (Component_Type (Typ));
10049
10050 elsif Is_Record_Type (Typ) then
10051 declare
10052 Comp : Entity_Id;
10053
10054 begin
10055 -- Loop to Check components
10056
10057 Comp := First_Component_Or_Discriminant (Typ);
10058 while Present (Comp) loop
10059
10060 -- Check for access component, tag field does not count, even
10061 -- though it is implemented internally using an access type.
10062
10063 if Has_Access_Values (Etype (Comp))
10064 and then Chars (Comp) /= Name_uTag
10065 then
10066 return True;
10067 end if;
10068
10069 Next_Component_Or_Discriminant (Comp);
10070 end loop;
10071 end;
10072
10073 return False;
10074
10075 else
10076 return False;
10077 end if;
10078 end Has_Access_Values;
10079
10080 ------------------------------
10081 -- Has_Compatible_Alignment --
10082 ------------------------------
10083
10084 function Has_Compatible_Alignment
10085 (Obj : Entity_Id;
10086 Expr : Node_Id;
10087 Layout_Done : Boolean) return Alignment_Result
10088 is
10089 function Has_Compatible_Alignment_Internal
10090 (Obj : Entity_Id;
10091 Expr : Node_Id;
10092 Layout_Done : Boolean;
10093 Default : Alignment_Result) return Alignment_Result;
10094 -- This is the internal recursive function that actually does the work.
10095 -- There is one additional parameter, which says what the result should
10096 -- be if no alignment information is found, and there is no definite
10097 -- indication of compatible alignments. At the outer level, this is set
10098 -- to Unknown, but for internal recursive calls in the case where types
10099 -- are known to be correct, it is set to Known_Compatible.
10100
10101 ---------------------------------------
10102 -- Has_Compatible_Alignment_Internal --
10103 ---------------------------------------
10104
10105 function Has_Compatible_Alignment_Internal
10106 (Obj : Entity_Id;
10107 Expr : Node_Id;
10108 Layout_Done : Boolean;
10109 Default : Alignment_Result) return Alignment_Result
10110 is
10111 Result : Alignment_Result := Known_Compatible;
10112 -- Holds the current status of the result. Note that once a value of
10113 -- Known_Incompatible is set, it is sticky and does not get changed
10114 -- to Unknown (the value in Result only gets worse as we go along,
10115 -- never better).
10116
10117 Offs : Uint := No_Uint;
10118 -- Set to a factor of the offset from the base object when Expr is a
10119 -- selected or indexed component, based on Component_Bit_Offset and
10120 -- Component_Size respectively. A negative value is used to represent
10121 -- a value which is not known at compile time.
10122
10123 procedure Check_Prefix;
10124 -- Checks the prefix recursively in the case where the expression
10125 -- is an indexed or selected component.
10126
10127 procedure Set_Result (R : Alignment_Result);
10128 -- If R represents a worse outcome (unknown instead of known
10129 -- compatible, or known incompatible), then set Result to R.
10130
10131 ------------------
10132 -- Check_Prefix --
10133 ------------------
10134
10135 procedure Check_Prefix is
10136 begin
10137 -- The subtlety here is that in doing a recursive call to check
10138 -- the prefix, we have to decide what to do in the case where we
10139 -- don't find any specific indication of an alignment problem.
10140
10141 -- At the outer level, we normally set Unknown as the result in
10142 -- this case, since we can only set Known_Compatible if we really
10143 -- know that the alignment value is OK, but for the recursive
10144 -- call, in the case where the types match, and we have not
10145 -- specified a peculiar alignment for the object, we are only
10146 -- concerned about suspicious rep clauses, the default case does
10147 -- not affect us, since the compiler will, in the absence of such
10148 -- rep clauses, ensure that the alignment is correct.
10149
10150 if Default = Known_Compatible
10151 or else
10152 (Etype (Obj) = Etype (Expr)
10153 and then (Unknown_Alignment (Obj)
10154 or else
10155 Alignment (Obj) = Alignment (Etype (Obj))))
10156 then
10157 Set_Result
10158 (Has_Compatible_Alignment_Internal
10159 (Obj, Prefix (Expr), Layout_Done, Known_Compatible));
10160
10161 -- In all other cases, we need a full check on the prefix
10162
10163 else
10164 Set_Result
10165 (Has_Compatible_Alignment_Internal
10166 (Obj, Prefix (Expr), Layout_Done, Unknown));
10167 end if;
10168 end Check_Prefix;
10169
10170 ----------------
10171 -- Set_Result --
10172 ----------------
10173
10174 procedure Set_Result (R : Alignment_Result) is
10175 begin
10176 if R > Result then
10177 Result := R;
10178 end if;
10179 end Set_Result;
10180
10181 -- Start of processing for Has_Compatible_Alignment_Internal
10182
10183 begin
10184 -- If Expr is a selected component, we must make sure there is no
10185 -- potentially troublesome component clause and that the record is
10186 -- not packed if the layout is not done.
10187
10188 if Nkind (Expr) = N_Selected_Component then
10189
10190 -- Packing generates unknown alignment if layout is not done
10191
10192 if Is_Packed (Etype (Prefix (Expr))) and then not Layout_Done then
10193 Set_Result (Unknown);
10194 end if;
10195
10196 -- Check prefix and component offset
10197
10198 Check_Prefix;
10199 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
10200
10201 -- If Expr is an indexed component, we must make sure there is no
10202 -- potentially troublesome Component_Size clause and that the array
10203 -- is not bit-packed if the layout is not done.
10204
10205 elsif Nkind (Expr) = N_Indexed_Component then
10206 declare
10207 Typ : constant Entity_Id := Etype (Prefix (Expr));
10208
10209 begin
10210 -- Packing generates unknown alignment if layout is not done
10211
10212 if Is_Bit_Packed_Array (Typ) and then not Layout_Done then
10213 Set_Result (Unknown);
10214 end if;
10215
10216 -- Check prefix and component offset (or at least size)
10217
10218 Check_Prefix;
10219 Offs := Indexed_Component_Bit_Offset (Expr);
10220 if Offs = No_Uint then
10221 Offs := Component_Size (Typ);
10222 end if;
10223 end;
10224 end if;
10225
10226 -- If we have a null offset, the result is entirely determined by
10227 -- the base object and has already been computed recursively.
10228
10229 if Offs = Uint_0 then
10230 null;
10231
10232 -- Case where we know the alignment of the object
10233
10234 elsif Known_Alignment (Obj) then
10235 declare
10236 ObjA : constant Uint := Alignment (Obj);
10237 ExpA : Uint := No_Uint;
10238 SizA : Uint := No_Uint;
10239
10240 begin
10241 -- If alignment of Obj is 1, then we are always OK
10242
10243 if ObjA = 1 then
10244 Set_Result (Known_Compatible);
10245
10246 -- Alignment of Obj is greater than 1, so we need to check
10247
10248 else
10249 -- If we have an offset, see if it is compatible
10250
10251 if Offs /= No_Uint and Offs > Uint_0 then
10252 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
10253 Set_Result (Known_Incompatible);
10254 end if;
10255
10256 -- See if Expr is an object with known alignment
10257
10258 elsif Is_Entity_Name (Expr)
10259 and then Known_Alignment (Entity (Expr))
10260 then
10261 ExpA := Alignment (Entity (Expr));
10262
10263 -- Otherwise, we can use the alignment of the type of
10264 -- Expr given that we already checked for
10265 -- discombobulating rep clauses for the cases of indexed
10266 -- and selected components above.
10267
10268 elsif Known_Alignment (Etype (Expr)) then
10269 ExpA := Alignment (Etype (Expr));
10270
10271 -- Otherwise the alignment is unknown
10272
10273 else
10274 Set_Result (Default);
10275 end if;
10276
10277 -- If we got an alignment, see if it is acceptable
10278
10279 if ExpA /= No_Uint and then ExpA < ObjA then
10280 Set_Result (Known_Incompatible);
10281 end if;
10282
10283 -- If Expr is not a piece of a larger object, see if size
10284 -- is given. If so, check that it is not too small for the
10285 -- required alignment.
10286
10287 if Offs /= No_Uint then
10288 null;
10289
10290 -- See if Expr is an object with known size
10291
10292 elsif Is_Entity_Name (Expr)
10293 and then Known_Static_Esize (Entity (Expr))
10294 then
10295 SizA := Esize (Entity (Expr));
10296
10297 -- Otherwise, we check the object size of the Expr type
10298
10299 elsif Known_Static_Esize (Etype (Expr)) then
10300 SizA := Esize (Etype (Expr));
10301 end if;
10302
10303 -- If we got a size, see if it is a multiple of the Obj
10304 -- alignment, if not, then the alignment cannot be
10305 -- acceptable, since the size is always a multiple of the
10306 -- alignment.
10307
10308 if SizA /= No_Uint then
10309 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
10310 Set_Result (Known_Incompatible);
10311 end if;
10312 end if;
10313 end if;
10314 end;
10315
10316 -- If we do not know required alignment, any non-zero offset is a
10317 -- potential problem (but certainly may be OK, so result is unknown).
10318
10319 elsif Offs /= No_Uint then
10320 Set_Result (Unknown);
10321
10322 -- If we can't find the result by direct comparison of alignment
10323 -- values, then there is still one case that we can determine known
10324 -- result, and that is when we can determine that the types are the
10325 -- same, and no alignments are specified. Then we known that the
10326 -- alignments are compatible, even if we don't know the alignment
10327 -- value in the front end.
10328
10329 elsif Etype (Obj) = Etype (Expr) then
10330
10331 -- Types are the same, but we have to check for possible size
10332 -- and alignments on the Expr object that may make the alignment
10333 -- different, even though the types are the same.
10334
10335 if Is_Entity_Name (Expr) then
10336
10337 -- First check alignment of the Expr object. Any alignment less
10338 -- than Maximum_Alignment is worrisome since this is the case
10339 -- where we do not know the alignment of Obj.
10340
10341 if Known_Alignment (Entity (Expr))
10342 and then UI_To_Int (Alignment (Entity (Expr))) <
10343 Ttypes.Maximum_Alignment
10344 then
10345 Set_Result (Unknown);
10346
10347 -- Now check size of Expr object. Any size that is not an
10348 -- even multiple of Maximum_Alignment is also worrisome
10349 -- since it may cause the alignment of the object to be less
10350 -- than the alignment of the type.
10351
10352 elsif Known_Static_Esize (Entity (Expr))
10353 and then
10354 (UI_To_Int (Esize (Entity (Expr))) mod
10355 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
10356 /= 0
10357 then
10358 Set_Result (Unknown);
10359
10360 -- Otherwise same type is decisive
10361
10362 else
10363 Set_Result (Known_Compatible);
10364 end if;
10365 end if;
10366
10367 -- Another case to deal with is when there is an explicit size or
10368 -- alignment clause when the types are not the same. If so, then the
10369 -- result is Unknown. We don't need to do this test if the Default is
10370 -- Unknown, since that result will be set in any case.
10371
10372 elsif Default /= Unknown
10373 and then (Has_Size_Clause (Etype (Expr))
10374 or else
10375 Has_Alignment_Clause (Etype (Expr)))
10376 then
10377 Set_Result (Unknown);
10378
10379 -- If no indication found, set default
10380
10381 else
10382 Set_Result (Default);
10383 end if;
10384
10385 -- Return worst result found
10386
10387 return Result;
10388 end Has_Compatible_Alignment_Internal;
10389
10390 -- Start of processing for Has_Compatible_Alignment
10391
10392 begin
10393 -- If Obj has no specified alignment, then set alignment from the type
10394 -- alignment. Perhaps we should always do this, but for sure we should
10395 -- do it when there is an address clause since we can do more if the
10396 -- alignment is known.
10397
10398 if Unknown_Alignment (Obj) then
10399 Set_Alignment (Obj, Alignment (Etype (Obj)));
10400 end if;
10401
10402 -- Now do the internal call that does all the work
10403
10404 return
10405 Has_Compatible_Alignment_Internal (Obj, Expr, Layout_Done, Unknown);
10406 end Has_Compatible_Alignment;
10407
10408 ----------------------
10409 -- Has_Declarations --
10410 ----------------------
10411
10412 function Has_Declarations (N : Node_Id) return Boolean is
10413 begin
10414 return Nkind_In (Nkind (N), N_Accept_Statement,
10415 N_Block_Statement,
10416 N_Compilation_Unit_Aux,
10417 N_Entry_Body,
10418 N_Package_Body,
10419 N_Protected_Body,
10420 N_Subprogram_Body,
10421 N_Task_Body,
10422 N_Package_Specification);
10423 end Has_Declarations;
10424
10425 ---------------------------------
10426 -- Has_Defaulted_Discriminants --
10427 ---------------------------------
10428
10429 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10430 begin
10431 return Has_Discriminants (Typ)
10432 and then Present (First_Discriminant (Typ))
10433 and then Present (Discriminant_Default_Value
10434 (First_Discriminant (Typ)));
10435 end Has_Defaulted_Discriminants;
10436
10437 -------------------
10438 -- Has_Denormals --
10439 -------------------
10440
10441 function Has_Denormals (E : Entity_Id) return Boolean is
10442 begin
10443 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
10444 end Has_Denormals;
10445
10446 -------------------------------------------
10447 -- Has_Discriminant_Dependent_Constraint --
10448 -------------------------------------------
10449
10450 function Has_Discriminant_Dependent_Constraint
10451 (Comp : Entity_Id) return Boolean
10452 is
10453 Comp_Decl : constant Node_Id := Parent (Comp);
10454 Subt_Indic : Node_Id;
10455 Constr : Node_Id;
10456 Assn : Node_Id;
10457
10458 begin
10459 -- Discriminants can't depend on discriminants
10460
10461 if Ekind (Comp) = E_Discriminant then
10462 return False;
10463
10464 else
10465 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
10466
10467 if Nkind (Subt_Indic) = N_Subtype_Indication then
10468 Constr := Constraint (Subt_Indic);
10469
10470 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
10471 Assn := First (Constraints (Constr));
10472 while Present (Assn) loop
10473 case Nkind (Assn) is
10474 when N_Identifier
10475 | N_Range
10476 | N_Subtype_Indication
10477 =>
10478 if Depends_On_Discriminant (Assn) then
10479 return True;
10480 end if;
10481
10482 when N_Discriminant_Association =>
10483 if Depends_On_Discriminant (Expression (Assn)) then
10484 return True;
10485 end if;
10486
10487 when others =>
10488 null;
10489 end case;
10490
10491 Next (Assn);
10492 end loop;
10493 end if;
10494 end if;
10495 end if;
10496
10497 return False;
10498 end Has_Discriminant_Dependent_Constraint;
10499
10500 --------------------------------------
10501 -- Has_Effectively_Volatile_Profile --
10502 --------------------------------------
10503
10504 function Has_Effectively_Volatile_Profile
10505 (Subp_Id : Entity_Id) return Boolean
10506 is
10507 Formal : Entity_Id;
10508
10509 begin
10510 -- Inspect the formal parameters looking for an effectively volatile
10511 -- type.
10512
10513 Formal := First_Formal (Subp_Id);
10514 while Present (Formal) loop
10515 if Is_Effectively_Volatile (Etype (Formal)) then
10516 return True;
10517 end if;
10518
10519 Next_Formal (Formal);
10520 end loop;
10521
10522 -- Inspect the return type of functions
10523
10524 if Ekind_In (Subp_Id, E_Function, E_Generic_Function)
10525 and then Is_Effectively_Volatile (Etype (Subp_Id))
10526 then
10527 return True;
10528 end if;
10529
10530 return False;
10531 end Has_Effectively_Volatile_Profile;
10532
10533 --------------------------
10534 -- Has_Enabled_Property --
10535 --------------------------
10536
10537 function Has_Enabled_Property
10538 (Item_Id : Entity_Id;
10539 Property : Name_Id) return Boolean
10540 is
10541 function Protected_Object_Has_Enabled_Property return Boolean;
10542 -- Determine whether a protected object denoted by Item_Id has the
10543 -- property enabled.
10544
10545 function State_Has_Enabled_Property return Boolean;
10546 -- Determine whether a state denoted by Item_Id has the property enabled
10547
10548 function Variable_Has_Enabled_Property return Boolean;
10549 -- Determine whether a variable denoted by Item_Id has the property
10550 -- enabled.
10551
10552 -------------------------------------------
10553 -- Protected_Object_Has_Enabled_Property --
10554 -------------------------------------------
10555
10556 function Protected_Object_Has_Enabled_Property return Boolean is
10557 Constits : constant Elist_Id := Part_Of_Constituents (Item_Id);
10558 Constit_Elmt : Elmt_Id;
10559 Constit_Id : Entity_Id;
10560
10561 begin
10562 -- Protected objects always have the properties Async_Readers and
10563 -- Async_Writers (SPARK RM 7.1.2(16)).
10564
10565 if Property = Name_Async_Readers
10566 or else Property = Name_Async_Writers
10567 then
10568 return True;
10569
10570 -- Protected objects that have Part_Of components also inherit their
10571 -- properties Effective_Reads and Effective_Writes
10572 -- (SPARK RM 7.1.2(16)).
10573
10574 elsif Present (Constits) then
10575 Constit_Elmt := First_Elmt (Constits);
10576 while Present (Constit_Elmt) loop
10577 Constit_Id := Node (Constit_Elmt);
10578
10579 if Has_Enabled_Property (Constit_Id, Property) then
10580 return True;
10581 end if;
10582
10583 Next_Elmt (Constit_Elmt);
10584 end loop;
10585 end if;
10586
10587 return False;
10588 end Protected_Object_Has_Enabled_Property;
10589
10590 --------------------------------
10591 -- State_Has_Enabled_Property --
10592 --------------------------------
10593
10594 function State_Has_Enabled_Property return Boolean is
10595 Decl : constant Node_Id := Parent (Item_Id);
10596
10597 procedure Find_Simple_Properties
10598 (Has_External : out Boolean;
10599 Has_Synchronous : out Boolean);
10600 -- Extract the simple properties associated with declaration Decl
10601
10602 function Is_Enabled_External_Property return Boolean;
10603 -- Determine whether property Property appears within the external
10604 -- property list of declaration Decl, and return its status.
10605
10606 ----------------------------
10607 -- Find_Simple_Properties --
10608 ----------------------------
10609
10610 procedure Find_Simple_Properties
10611 (Has_External : out Boolean;
10612 Has_Synchronous : out Boolean)
10613 is
10614 Opt : Node_Id;
10615
10616 begin
10617 -- Assume that none of the properties are available
10618
10619 Has_External := False;
10620 Has_Synchronous := False;
10621
10622 Opt := First (Expressions (Decl));
10623 while Present (Opt) loop
10624 if Nkind (Opt) = N_Identifier then
10625 if Chars (Opt) = Name_External then
10626 Has_External := True;
10627
10628 elsif Chars (Opt) = Name_Synchronous then
10629 Has_Synchronous := True;
10630 end if;
10631 end if;
10632
10633 Next (Opt);
10634 end loop;
10635 end Find_Simple_Properties;
10636
10637 ----------------------------------
10638 -- Is_Enabled_External_Property --
10639 ----------------------------------
10640
10641 function Is_Enabled_External_Property return Boolean is
10642 Opt : Node_Id;
10643 Opt_Nam : Node_Id;
10644 Prop : Node_Id;
10645 Prop_Nam : Node_Id;
10646 Props : Node_Id;
10647
10648 begin
10649 Opt := First (Component_Associations (Decl));
10650 while Present (Opt) loop
10651 Opt_Nam := First (Choices (Opt));
10652
10653 if Nkind (Opt_Nam) = N_Identifier
10654 and then Chars (Opt_Nam) = Name_External
10655 then
10656 Props := Expression (Opt);
10657
10658 -- Multiple properties appear as an aggregate
10659
10660 if Nkind (Props) = N_Aggregate then
10661
10662 -- Simple property form
10663
10664 Prop := First (Expressions (Props));
10665 while Present (Prop) loop
10666 if Chars (Prop) = Property then
10667 return True;
10668 end if;
10669
10670 Next (Prop);
10671 end loop;
10672
10673 -- Property with expression form
10674
10675 Prop := First (Component_Associations (Props));
10676 while Present (Prop) loop
10677 Prop_Nam := First (Choices (Prop));
10678
10679 -- The property can be represented in two ways:
10680 -- others => <value>
10681 -- <property> => <value>
10682
10683 if Nkind (Prop_Nam) = N_Others_Choice
10684 or else (Nkind (Prop_Nam) = N_Identifier
10685 and then Chars (Prop_Nam) = Property)
10686 then
10687 return Is_True (Expr_Value (Expression (Prop)));
10688 end if;
10689
10690 Next (Prop);
10691 end loop;
10692
10693 -- Single property
10694
10695 else
10696 return Chars (Props) = Property;
10697 end if;
10698 end if;
10699
10700 Next (Opt);
10701 end loop;
10702
10703 return False;
10704 end Is_Enabled_External_Property;
10705
10706 -- Local variables
10707
10708 Has_External : Boolean;
10709 Has_Synchronous : Boolean;
10710
10711 -- Start of processing for State_Has_Enabled_Property
10712
10713 begin
10714 -- The declaration of an external abstract state appears as an
10715 -- extension aggregate. If this is not the case, properties can
10716 -- never be set.
10717
10718 if Nkind (Decl) /= N_Extension_Aggregate then
10719 return False;
10720 end if;
10721
10722 Find_Simple_Properties (Has_External, Has_Synchronous);
10723
10724 -- Simple option External enables all properties (SPARK RM 7.1.2(2))
10725
10726 if Has_External then
10727 return True;
10728
10729 -- Option External may enable or disable specific properties
10730
10731 elsif Is_Enabled_External_Property then
10732 return True;
10733
10734 -- Simple option Synchronous
10735 --
10736 -- enables disables
10737 -- Asynch_Readers Effective_Reads
10738 -- Asynch_Writers Effective_Writes
10739 --
10740 -- Note that both forms of External have higher precedence than
10741 -- Synchronous (SPARK RM 7.1.4(9)).
10742
10743 elsif Has_Synchronous then
10744 return Nam_In (Property, Name_Async_Readers, Name_Async_Writers);
10745 end if;
10746
10747 return False;
10748 end State_Has_Enabled_Property;
10749
10750 -----------------------------------
10751 -- Variable_Has_Enabled_Property --
10752 -----------------------------------
10753
10754 function Variable_Has_Enabled_Property return Boolean is
10755 function Is_Enabled (Prag : Node_Id) return Boolean;
10756 -- Determine whether property pragma Prag (if present) denotes an
10757 -- enabled property.
10758
10759 ----------------
10760 -- Is_Enabled --
10761 ----------------
10762
10763 function Is_Enabled (Prag : Node_Id) return Boolean is
10764 Arg1 : Node_Id;
10765
10766 begin
10767 if Present (Prag) then
10768 Arg1 := First (Pragma_Argument_Associations (Prag));
10769
10770 -- The pragma has an optional Boolean expression, the related
10771 -- property is enabled only when the expression evaluates to
10772 -- True.
10773
10774 if Present (Arg1) then
10775 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
10776
10777 -- Otherwise the lack of expression enables the property by
10778 -- default.
10779
10780 else
10781 return True;
10782 end if;
10783
10784 -- The property was never set in the first place
10785
10786 else
10787 return False;
10788 end if;
10789 end Is_Enabled;
10790
10791 -- Local variables
10792
10793 AR : constant Node_Id :=
10794 Get_Pragma (Item_Id, Pragma_Async_Readers);
10795 AW : constant Node_Id :=
10796 Get_Pragma (Item_Id, Pragma_Async_Writers);
10797 ER : constant Node_Id :=
10798 Get_Pragma (Item_Id, Pragma_Effective_Reads);
10799 EW : constant Node_Id :=
10800 Get_Pragma (Item_Id, Pragma_Effective_Writes);
10801
10802 -- Start of processing for Variable_Has_Enabled_Property
10803
10804 begin
10805 -- A non-effectively volatile object can never possess external
10806 -- properties.
10807
10808 if not Is_Effectively_Volatile (Item_Id) then
10809 return False;
10810
10811 -- External properties related to variables come in two flavors -
10812 -- explicit and implicit. The explicit case is characterized by the
10813 -- presence of a property pragma with an optional Boolean flag. The
10814 -- property is enabled when the flag evaluates to True or the flag is
10815 -- missing altogether.
10816
10817 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
10818 return True;
10819
10820 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
10821 return True;
10822
10823 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
10824 return True;
10825
10826 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
10827 return True;
10828
10829 -- The implicit case lacks all property pragmas
10830
10831 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
10832 if Is_Protected_Type (Etype (Item_Id)) then
10833 return Protected_Object_Has_Enabled_Property;
10834 else
10835 return True;
10836 end if;
10837
10838 else
10839 return False;
10840 end if;
10841 end Variable_Has_Enabled_Property;
10842
10843 -- Start of processing for Has_Enabled_Property
10844
10845 begin
10846 -- Abstract states and variables have a flexible scheme of specifying
10847 -- external properties.
10848
10849 if Ekind (Item_Id) = E_Abstract_State then
10850 return State_Has_Enabled_Property;
10851
10852 elsif Ekind (Item_Id) = E_Variable then
10853 return Variable_Has_Enabled_Property;
10854
10855 -- By default, protected objects only have the properties Async_Readers
10856 -- and Async_Writers. If they have Part_Of components, they also inherit
10857 -- their properties Effective_Reads and Effective_Writes
10858 -- (SPARK RM 7.1.2(16)).
10859
10860 elsif Ekind (Item_Id) = E_Protected_Object then
10861 return Protected_Object_Has_Enabled_Property;
10862
10863 -- Otherwise a property is enabled when the related item is effectively
10864 -- volatile.
10865
10866 else
10867 return Is_Effectively_Volatile (Item_Id);
10868 end if;
10869 end Has_Enabled_Property;
10870
10871 -------------------------------------
10872 -- Has_Full_Default_Initialization --
10873 -------------------------------------
10874
10875 function Has_Full_Default_Initialization (Typ : Entity_Id) return Boolean is
10876 Comp : Entity_Id;
10877
10878 begin
10879 -- A type subject to pragma Default_Initial_Condition may be fully
10880 -- default initialized depending on inheritance and the argument of
10881 -- the pragma. Since any type may act as the full view of a private
10882 -- type, this check must be performed prior to the specialized tests
10883 -- below.
10884
10885 if Has_Fully_Default_Initializing_DIC_Pragma (Typ) then
10886 return True;
10887 end if;
10888
10889 -- A scalar type is fully default initialized if it is subject to aspect
10890 -- Default_Value.
10891
10892 if Is_Scalar_Type (Typ) then
10893 return Has_Default_Aspect (Typ);
10894
10895 -- An access type is fully default initialized by default
10896
10897 elsif Is_Access_Type (Typ) then
10898 return True;
10899
10900 -- An array type is fully default initialized if its element type is
10901 -- scalar and the array type carries aspect Default_Component_Value or
10902 -- the element type is fully default initialized.
10903
10904 elsif Is_Array_Type (Typ) then
10905 return
10906 Has_Default_Aspect (Typ)
10907 or else Has_Full_Default_Initialization (Component_Type (Typ));
10908
10909 -- A protected type, record type, or type extension is fully default
10910 -- initialized if all its components either carry an initialization
10911 -- expression or have a type that is fully default initialized. The
10912 -- parent type of a type extension must be fully default initialized.
10913
10914 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
10915
10916 -- Inspect all entities defined in the scope of the type, looking for
10917 -- uninitialized components.
10918
10919 Comp := First_Entity (Typ);
10920 while Present (Comp) loop
10921 if Ekind (Comp) = E_Component
10922 and then Comes_From_Source (Comp)
10923 and then No (Expression (Parent (Comp)))
10924 and then not Has_Full_Default_Initialization (Etype (Comp))
10925 then
10926 return False;
10927 end if;
10928
10929 Next_Entity (Comp);
10930 end loop;
10931
10932 -- Ensure that the parent type of a type extension is fully default
10933 -- initialized.
10934
10935 if Etype (Typ) /= Typ
10936 and then not Has_Full_Default_Initialization (Etype (Typ))
10937 then
10938 return False;
10939 end if;
10940
10941 -- If we get here, then all components and parent portion are fully
10942 -- default initialized.
10943
10944 return True;
10945
10946 -- A task type is fully default initialized by default
10947
10948 elsif Is_Task_Type (Typ) then
10949 return True;
10950
10951 -- Otherwise the type is not fully default initialized
10952
10953 else
10954 return False;
10955 end if;
10956 end Has_Full_Default_Initialization;
10957
10958 -----------------------------------------------
10959 -- Has_Fully_Default_Initializing_DIC_Pragma --
10960 -----------------------------------------------
10961
10962 function Has_Fully_Default_Initializing_DIC_Pragma
10963 (Typ : Entity_Id) return Boolean
10964 is
10965 Args : List_Id;
10966 Prag : Node_Id;
10967
10968 begin
10969 -- A type that inherits pragma Default_Initial_Condition from a parent
10970 -- type is automatically fully default initialized.
10971
10972 if Has_Inherited_DIC (Typ) then
10973 return True;
10974
10975 -- Otherwise the type is fully default initialized only when the pragma
10976 -- appears without an argument, or the argument is non-null.
10977
10978 elsif Has_Own_DIC (Typ) then
10979 Prag := Get_Pragma (Typ, Pragma_Default_Initial_Condition);
10980 pragma Assert (Present (Prag));
10981 Args := Pragma_Argument_Associations (Prag);
10982
10983 -- The pragma appears without an argument in which case it defaults
10984 -- to True.
10985
10986 if No (Args) then
10987 return True;
10988
10989 -- The pragma appears with a non-null expression
10990
10991 elsif Nkind (Get_Pragma_Arg (First (Args))) /= N_Null then
10992 return True;
10993 end if;
10994 end if;
10995
10996 return False;
10997 end Has_Fully_Default_Initializing_DIC_Pragma;
10998
10999 --------------------
11000 -- Has_Infinities --
11001 --------------------
11002
11003 function Has_Infinities (E : Entity_Id) return Boolean is
11004 begin
11005 return
11006 Is_Floating_Point_Type (E)
11007 and then Nkind (Scalar_Range (E)) = N_Range
11008 and then Includes_Infinities (Scalar_Range (E));
11009 end Has_Infinities;
11010
11011 --------------------
11012 -- Has_Interfaces --
11013 --------------------
11014
11015 function Has_Interfaces
11016 (T : Entity_Id;
11017 Use_Full_View : Boolean := True) return Boolean
11018 is
11019 Typ : Entity_Id := Base_Type (T);
11020
11021 begin
11022 -- Handle concurrent types
11023
11024 if Is_Concurrent_Type (Typ) then
11025 Typ := Corresponding_Record_Type (Typ);
11026 end if;
11027
11028 if not Present (Typ)
11029 or else not Is_Record_Type (Typ)
11030 or else not Is_Tagged_Type (Typ)
11031 then
11032 return False;
11033 end if;
11034
11035 -- Handle private types
11036
11037 if Use_Full_View and then Present (Full_View (Typ)) then
11038 Typ := Full_View (Typ);
11039 end if;
11040
11041 -- Handle concurrent record types
11042
11043 if Is_Concurrent_Record_Type (Typ)
11044 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
11045 then
11046 return True;
11047 end if;
11048
11049 loop
11050 if Is_Interface (Typ)
11051 or else
11052 (Is_Record_Type (Typ)
11053 and then Present (Interfaces (Typ))
11054 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
11055 then
11056 return True;
11057 end if;
11058
11059 exit when Etype (Typ) = Typ
11060
11061 -- Handle private types
11062
11063 or else (Present (Full_View (Etype (Typ)))
11064 and then Full_View (Etype (Typ)) = Typ)
11065
11066 -- Protect frontend against wrong sources with cyclic derivations
11067
11068 or else Etype (Typ) = T;
11069
11070 -- Climb to the ancestor type handling private types
11071
11072 if Present (Full_View (Etype (Typ))) then
11073 Typ := Full_View (Etype (Typ));
11074 else
11075 Typ := Etype (Typ);
11076 end if;
11077 end loop;
11078
11079 return False;
11080 end Has_Interfaces;
11081
11082 --------------------------
11083 -- Has_Max_Queue_Length --
11084 --------------------------
11085
11086 function Has_Max_Queue_Length (Id : Entity_Id) return Boolean is
11087 begin
11088 return
11089 Ekind (Id) = E_Entry
11090 and then Present (Get_Pragma (Id, Pragma_Max_Queue_Length));
11091 end Has_Max_Queue_Length;
11092
11093 ---------------------------------
11094 -- Has_No_Obvious_Side_Effects --
11095 ---------------------------------
11096
11097 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
11098 begin
11099 -- For now handle literals, constants, and non-volatile variables and
11100 -- expressions combining these with operators or short circuit forms.
11101
11102 if Nkind (N) in N_Numeric_Or_String_Literal then
11103 return True;
11104
11105 elsif Nkind (N) = N_Character_Literal then
11106 return True;
11107
11108 elsif Nkind (N) in N_Unary_Op then
11109 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
11110
11111 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
11112 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
11113 and then
11114 Has_No_Obvious_Side_Effects (Right_Opnd (N));
11115
11116 elsif Nkind (N) = N_Expression_With_Actions
11117 and then Is_Empty_List (Actions (N))
11118 then
11119 return Has_No_Obvious_Side_Effects (Expression (N));
11120
11121 elsif Nkind (N) in N_Has_Entity then
11122 return Present (Entity (N))
11123 and then Ekind_In (Entity (N), E_Variable,
11124 E_Constant,
11125 E_Enumeration_Literal,
11126 E_In_Parameter,
11127 E_Out_Parameter,
11128 E_In_Out_Parameter)
11129 and then not Is_Volatile (Entity (N));
11130
11131 else
11132 return False;
11133 end if;
11134 end Has_No_Obvious_Side_Effects;
11135
11136 -----------------------------
11137 -- Has_Non_Null_Refinement --
11138 -----------------------------
11139
11140 function Has_Non_Null_Refinement (Id : Entity_Id) return Boolean is
11141 Constits : Elist_Id;
11142
11143 begin
11144 pragma Assert (Ekind (Id) = E_Abstract_State);
11145 Constits := Refinement_Constituents (Id);
11146
11147 -- For a refinement to be non-null, the first constituent must be
11148 -- anything other than null.
11149
11150 return
11151 Present (Constits)
11152 and then Nkind (Node (First_Elmt (Constits))) /= N_Null;
11153 end Has_Non_Null_Refinement;
11154
11155 -----------------------------
11156 -- Has_Non_Null_Statements --
11157 -----------------------------
11158
11159 function Has_Non_Null_Statements (L : List_Id) return Boolean is
11160 Node : Node_Id;
11161
11162 begin
11163 if Is_Non_Empty_List (L) then
11164 Node := First (L);
11165
11166 loop
11167 if Nkind (Node) /= N_Null_Statement then
11168 return True;
11169 end if;
11170
11171 Next (Node);
11172 exit when Node = Empty;
11173 end loop;
11174 end if;
11175
11176 return False;
11177 end Has_Non_Null_Statements;
11178
11179 ----------------------------------
11180 -- Has_Non_Trivial_Precondition --
11181 ----------------------------------
11182
11183 function Has_Non_Trivial_Precondition (Subp : Entity_Id) return Boolean is
11184 Pre : constant Node_Id := Find_Aspect (Subp, Aspect_Pre);
11185
11186 begin
11187 return
11188 Present (Pre)
11189 and then Class_Present (Pre)
11190 and then not Is_Entity_Name (Expression (Pre));
11191 end Has_Non_Trivial_Precondition;
11192
11193 -------------------
11194 -- Has_Null_Body --
11195 -------------------
11196
11197 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
11198 Body_Id : Entity_Id;
11199 Decl : Node_Id;
11200 Spec : Node_Id;
11201 Stmt1 : Node_Id;
11202 Stmt2 : Node_Id;
11203
11204 begin
11205 Spec := Parent (Proc_Id);
11206 Decl := Parent (Spec);
11207
11208 -- Retrieve the entity of the procedure body (e.g. invariant proc).
11209
11210 if Nkind (Spec) = N_Procedure_Specification
11211 and then Nkind (Decl) = N_Subprogram_Declaration
11212 then
11213 Body_Id := Corresponding_Body (Decl);
11214
11215 -- The body acts as a spec
11216
11217 else
11218 Body_Id := Proc_Id;
11219 end if;
11220
11221 -- The body will be generated later
11222
11223 if No (Body_Id) then
11224 return False;
11225 end if;
11226
11227 Spec := Parent (Body_Id);
11228 Decl := Parent (Spec);
11229
11230 pragma Assert
11231 (Nkind (Spec) = N_Procedure_Specification
11232 and then Nkind (Decl) = N_Subprogram_Body);
11233
11234 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
11235
11236 -- Look for a null statement followed by an optional return
11237 -- statement.
11238
11239 if Nkind (Stmt1) = N_Null_Statement then
11240 Stmt2 := Next (Stmt1);
11241
11242 if Present (Stmt2) then
11243 return Nkind (Stmt2) = N_Simple_Return_Statement;
11244 else
11245 return True;
11246 end if;
11247 end if;
11248
11249 return False;
11250 end Has_Null_Body;
11251
11252 ------------------------
11253 -- Has_Null_Exclusion --
11254 ------------------------
11255
11256 function Has_Null_Exclusion (N : Node_Id) return Boolean is
11257 begin
11258 case Nkind (N) is
11259 when N_Access_Definition
11260 | N_Access_Function_Definition
11261 | N_Access_Procedure_Definition
11262 | N_Access_To_Object_Definition
11263 | N_Allocator
11264 | N_Derived_Type_Definition
11265 | N_Function_Specification
11266 | N_Subtype_Declaration
11267 =>
11268 return Null_Exclusion_Present (N);
11269
11270 when N_Component_Definition
11271 | N_Formal_Object_Declaration
11272 | N_Object_Renaming_Declaration
11273 =>
11274 if Present (Subtype_Mark (N)) then
11275 return Null_Exclusion_Present (N);
11276 else pragma Assert (Present (Access_Definition (N)));
11277 return Null_Exclusion_Present (Access_Definition (N));
11278 end if;
11279
11280 when N_Discriminant_Specification =>
11281 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
11282 return Null_Exclusion_Present (Discriminant_Type (N));
11283 else
11284 return Null_Exclusion_Present (N);
11285 end if;
11286
11287 when N_Object_Declaration =>
11288 if Nkind (Object_Definition (N)) = N_Access_Definition then
11289 return Null_Exclusion_Present (Object_Definition (N));
11290 else
11291 return Null_Exclusion_Present (N);
11292 end if;
11293
11294 when N_Parameter_Specification =>
11295 if Nkind (Parameter_Type (N)) = N_Access_Definition then
11296 return Null_Exclusion_Present (Parameter_Type (N));
11297 else
11298 return Null_Exclusion_Present (N);
11299 end if;
11300
11301 when others =>
11302 return False;
11303 end case;
11304 end Has_Null_Exclusion;
11305
11306 ------------------------
11307 -- Has_Null_Extension --
11308 ------------------------
11309
11310 function Has_Null_Extension (T : Entity_Id) return Boolean is
11311 B : constant Entity_Id := Base_Type (T);
11312 Comps : Node_Id;
11313 Ext : Node_Id;
11314
11315 begin
11316 if Nkind (Parent (B)) = N_Full_Type_Declaration
11317 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
11318 then
11319 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
11320
11321 if Present (Ext) then
11322 if Null_Present (Ext) then
11323 return True;
11324 else
11325 Comps := Component_List (Ext);
11326
11327 -- The null component list is rewritten during analysis to
11328 -- include the parent component. Any other component indicates
11329 -- that the extension was not originally null.
11330
11331 return Null_Present (Comps)
11332 or else No (Next (First (Component_Items (Comps))));
11333 end if;
11334 else
11335 return False;
11336 end if;
11337
11338 else
11339 return False;
11340 end if;
11341 end Has_Null_Extension;
11342
11343 -------------------------
11344 -- Has_Null_Refinement --
11345 -------------------------
11346
11347 function Has_Null_Refinement (Id : Entity_Id) return Boolean is
11348 Constits : Elist_Id;
11349
11350 begin
11351 pragma Assert (Ekind (Id) = E_Abstract_State);
11352 Constits := Refinement_Constituents (Id);
11353
11354 -- For a refinement to be null, the state's sole constituent must be a
11355 -- null.
11356
11357 return
11358 Present (Constits)
11359 and then Nkind (Node (First_Elmt (Constits))) = N_Null;
11360 end Has_Null_Refinement;
11361
11362 -------------------------------
11363 -- Has_Overriding_Initialize --
11364 -------------------------------
11365
11366 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
11367 BT : constant Entity_Id := Base_Type (T);
11368 P : Elmt_Id;
11369
11370 begin
11371 if Is_Controlled (BT) then
11372 if Is_RTU (Scope (BT), Ada_Finalization) then
11373 return False;
11374
11375 elsif Present (Primitive_Operations (BT)) then
11376 P := First_Elmt (Primitive_Operations (BT));
11377 while Present (P) loop
11378 declare
11379 Init : constant Entity_Id := Node (P);
11380 Formal : constant Entity_Id := First_Formal (Init);
11381 begin
11382 if Ekind (Init) = E_Procedure
11383 and then Chars (Init) = Name_Initialize
11384 and then Comes_From_Source (Init)
11385 and then Present (Formal)
11386 and then Etype (Formal) = BT
11387 and then No (Next_Formal (Formal))
11388 and then (Ada_Version < Ada_2012
11389 or else not Null_Present (Parent (Init)))
11390 then
11391 return True;
11392 end if;
11393 end;
11394
11395 Next_Elmt (P);
11396 end loop;
11397 end if;
11398
11399 -- Here if type itself does not have a non-null Initialize operation:
11400 -- check immediate ancestor.
11401
11402 if Is_Derived_Type (BT)
11403 and then Has_Overriding_Initialize (Etype (BT))
11404 then
11405 return True;
11406 end if;
11407 end if;
11408
11409 return False;
11410 end Has_Overriding_Initialize;
11411
11412 --------------------------------------
11413 -- Has_Preelaborable_Initialization --
11414 --------------------------------------
11415
11416 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
11417 Has_PE : Boolean;
11418
11419 procedure Check_Components (E : Entity_Id);
11420 -- Check component/discriminant chain, sets Has_PE False if a component
11421 -- or discriminant does not meet the preelaborable initialization rules.
11422
11423 ----------------------
11424 -- Check_Components --
11425 ----------------------
11426
11427 procedure Check_Components (E : Entity_Id) is
11428 Ent : Entity_Id;
11429 Exp : Node_Id;
11430
11431 begin
11432 -- Loop through entities of record or protected type
11433
11434 Ent := E;
11435 while Present (Ent) loop
11436
11437 -- We are interested only in components and discriminants
11438
11439 Exp := Empty;
11440
11441 case Ekind (Ent) is
11442 when E_Component =>
11443
11444 -- Get default expression if any. If there is no declaration
11445 -- node, it means we have an internal entity. The parent and
11446 -- tag fields are examples of such entities. For such cases,
11447 -- we just test the type of the entity.
11448
11449 if Present (Declaration_Node (Ent)) then
11450 Exp := Expression (Declaration_Node (Ent));
11451 end if;
11452
11453 when E_Discriminant =>
11454
11455 -- Note: for a renamed discriminant, the Declaration_Node
11456 -- may point to the one from the ancestor, and have a
11457 -- different expression, so use the proper attribute to
11458 -- retrieve the expression from the derived constraint.
11459
11460 Exp := Discriminant_Default_Value (Ent);
11461
11462 when others =>
11463 goto Check_Next_Entity;
11464 end case;
11465
11466 -- A component has PI if it has no default expression and the
11467 -- component type has PI.
11468
11469 if No (Exp) then
11470 if not Has_Preelaborable_Initialization (Etype (Ent)) then
11471 Has_PE := False;
11472 exit;
11473 end if;
11474
11475 -- Require the default expression to be preelaborable
11476
11477 elsif not Is_Preelaborable_Construct (Exp) then
11478 Has_PE := False;
11479 exit;
11480 end if;
11481
11482 <<Check_Next_Entity>>
11483 Next_Entity (Ent);
11484 end loop;
11485 end Check_Components;
11486
11487 -- Start of processing for Has_Preelaborable_Initialization
11488
11489 begin
11490 -- Immediate return if already marked as known preelaborable init. This
11491 -- covers types for which this function has already been called once
11492 -- and returned True (in which case the result is cached), and also
11493 -- types to which a pragma Preelaborable_Initialization applies.
11494
11495 if Known_To_Have_Preelab_Init (E) then
11496 return True;
11497 end if;
11498
11499 -- If the type is a subtype representing a generic actual type, then
11500 -- test whether its base type has preelaborable initialization since
11501 -- the subtype representing the actual does not inherit this attribute
11502 -- from the actual or formal. (but maybe it should???)
11503
11504 if Is_Generic_Actual_Type (E) then
11505 return Has_Preelaborable_Initialization (Base_Type (E));
11506 end if;
11507
11508 -- All elementary types have preelaborable initialization
11509
11510 if Is_Elementary_Type (E) then
11511 Has_PE := True;
11512
11513 -- Array types have PI if the component type has PI
11514
11515 elsif Is_Array_Type (E) then
11516 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
11517
11518 -- A derived type has preelaborable initialization if its parent type
11519 -- has preelaborable initialization and (in the case of a derived record
11520 -- extension) if the non-inherited components all have preelaborable
11521 -- initialization. However, a user-defined controlled type with an
11522 -- overriding Initialize procedure does not have preelaborable
11523 -- initialization.
11524
11525 elsif Is_Derived_Type (E) then
11526
11527 -- If the derived type is a private extension then it doesn't have
11528 -- preelaborable initialization.
11529
11530 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
11531 return False;
11532 end if;
11533
11534 -- First check whether ancestor type has preelaborable initialization
11535
11536 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
11537
11538 -- If OK, check extension components (if any)
11539
11540 if Has_PE and then Is_Record_Type (E) then
11541 Check_Components (First_Entity (E));
11542 end if;
11543
11544 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
11545 -- with a user defined Initialize procedure does not have PI. If
11546 -- the type is untagged, the control primitives come from a component
11547 -- that has already been checked.
11548
11549 if Has_PE
11550 and then Is_Controlled (E)
11551 and then Is_Tagged_Type (E)
11552 and then Has_Overriding_Initialize (E)
11553 then
11554 Has_PE := False;
11555 end if;
11556
11557 -- Private types not derived from a type having preelaborable init and
11558 -- that are not marked with pragma Preelaborable_Initialization do not
11559 -- have preelaborable initialization.
11560
11561 elsif Is_Private_Type (E) then
11562 return False;
11563
11564 -- Record type has PI if it is non private and all components have PI
11565
11566 elsif Is_Record_Type (E) then
11567 Has_PE := True;
11568 Check_Components (First_Entity (E));
11569
11570 -- Protected types must not have entries, and components must meet
11571 -- same set of rules as for record components.
11572
11573 elsif Is_Protected_Type (E) then
11574 if Has_Entries (E) then
11575 Has_PE := False;
11576 else
11577 Has_PE := True;
11578 Check_Components (First_Entity (E));
11579 Check_Components (First_Private_Entity (E));
11580 end if;
11581
11582 -- Type System.Address always has preelaborable initialization
11583
11584 elsif Is_RTE (E, RE_Address) then
11585 Has_PE := True;
11586
11587 -- In all other cases, type does not have preelaborable initialization
11588
11589 else
11590 return False;
11591 end if;
11592
11593 -- If type has preelaborable initialization, cache result
11594
11595 if Has_PE then
11596 Set_Known_To_Have_Preelab_Init (E);
11597 end if;
11598
11599 return Has_PE;
11600 end Has_Preelaborable_Initialization;
11601
11602 ----------------
11603 -- Has_Prefix --
11604 ----------------
11605
11606 function Has_Prefix (N : Node_Id) return Boolean is
11607 begin
11608 return
11609 Nkind_In (N, N_Attribute_Reference,
11610 N_Expanded_Name,
11611 N_Explicit_Dereference,
11612 N_Indexed_Component,
11613 N_Reference,
11614 N_Selected_Component,
11615 N_Slice);
11616 end Has_Prefix;
11617
11618 ---------------------------
11619 -- Has_Private_Component --
11620 ---------------------------
11621
11622 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
11623 Btype : Entity_Id := Base_Type (Type_Id);
11624 Component : Entity_Id;
11625
11626 begin
11627 if Error_Posted (Type_Id)
11628 or else Error_Posted (Btype)
11629 then
11630 return False;
11631 end if;
11632
11633 if Is_Class_Wide_Type (Btype) then
11634 Btype := Root_Type (Btype);
11635 end if;
11636
11637 if Is_Private_Type (Btype) then
11638 declare
11639 UT : constant Entity_Id := Underlying_Type (Btype);
11640 begin
11641 if No (UT) then
11642 if No (Full_View (Btype)) then
11643 return not Is_Generic_Type (Btype)
11644 and then
11645 not Is_Generic_Type (Root_Type (Btype));
11646 else
11647 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
11648 end if;
11649 else
11650 return not Is_Frozen (UT) and then Has_Private_Component (UT);
11651 end if;
11652 end;
11653
11654 elsif Is_Array_Type (Btype) then
11655 return Has_Private_Component (Component_Type (Btype));
11656
11657 elsif Is_Record_Type (Btype) then
11658 Component := First_Component (Btype);
11659 while Present (Component) loop
11660 if Has_Private_Component (Etype (Component)) then
11661 return True;
11662 end if;
11663
11664 Next_Component (Component);
11665 end loop;
11666
11667 return False;
11668
11669 elsif Is_Protected_Type (Btype)
11670 and then Present (Corresponding_Record_Type (Btype))
11671 then
11672 return Has_Private_Component (Corresponding_Record_Type (Btype));
11673
11674 else
11675 return False;
11676 end if;
11677 end Has_Private_Component;
11678
11679 ----------------------
11680 -- Has_Signed_Zeros --
11681 ----------------------
11682
11683 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
11684 begin
11685 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
11686 end Has_Signed_Zeros;
11687
11688 ------------------------------
11689 -- Has_Significant_Contract --
11690 ------------------------------
11691
11692 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
11693 Subp_Nam : constant Name_Id := Chars (Subp_Id);
11694
11695 begin
11696 -- _Finalizer procedure
11697
11698 if Subp_Nam = Name_uFinalizer then
11699 return False;
11700
11701 -- _Postconditions procedure
11702
11703 elsif Subp_Nam = Name_uPostconditions then
11704 return False;
11705
11706 -- Predicate function
11707
11708 elsif Ekind (Subp_Id) = E_Function
11709 and then Is_Predicate_Function (Subp_Id)
11710 then
11711 return False;
11712
11713 -- TSS subprogram
11714
11715 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
11716 return False;
11717
11718 else
11719 return True;
11720 end if;
11721 end Has_Significant_Contract;
11722
11723 -----------------------------
11724 -- Has_Static_Array_Bounds --
11725 -----------------------------
11726
11727 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
11728 All_Static : Boolean;
11729 Dummy : Boolean;
11730
11731 begin
11732 Examine_Array_Bounds (Typ, All_Static, Dummy);
11733
11734 return All_Static;
11735 end Has_Static_Array_Bounds;
11736
11737 ---------------------------------------
11738 -- Has_Static_Non_Empty_Array_Bounds --
11739 ---------------------------------------
11740
11741 function Has_Static_Non_Empty_Array_Bounds (Typ : Node_Id) return Boolean is
11742 All_Static : Boolean;
11743 Has_Empty : Boolean;
11744
11745 begin
11746 Examine_Array_Bounds (Typ, All_Static, Has_Empty);
11747
11748 return All_Static and not Has_Empty;
11749 end Has_Static_Non_Empty_Array_Bounds;
11750
11751 ----------------
11752 -- Has_Stream --
11753 ----------------
11754
11755 function Has_Stream (T : Entity_Id) return Boolean is
11756 E : Entity_Id;
11757
11758 begin
11759 if No (T) then
11760 return False;
11761
11762 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
11763 return True;
11764
11765 elsif Is_Array_Type (T) then
11766 return Has_Stream (Component_Type (T));
11767
11768 elsif Is_Record_Type (T) then
11769 E := First_Component (T);
11770 while Present (E) loop
11771 if Has_Stream (Etype (E)) then
11772 return True;
11773 else
11774 Next_Component (E);
11775 end if;
11776 end loop;
11777
11778 return False;
11779
11780 elsif Is_Private_Type (T) then
11781 return Has_Stream (Underlying_Type (T));
11782
11783 else
11784 return False;
11785 end if;
11786 end Has_Stream;
11787
11788 ----------------
11789 -- Has_Suffix --
11790 ----------------
11791
11792 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
11793 begin
11794 Get_Name_String (Chars (E));
11795 return Name_Buffer (Name_Len) = Suffix;
11796 end Has_Suffix;
11797
11798 ----------------
11799 -- Add_Suffix --
11800 ----------------
11801
11802 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
11803 begin
11804 Get_Name_String (Chars (E));
11805 Add_Char_To_Name_Buffer (Suffix);
11806 return Name_Find;
11807 end Add_Suffix;
11808
11809 -------------------
11810 -- Remove_Suffix --
11811 -------------------
11812
11813 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
11814 begin
11815 pragma Assert (Has_Suffix (E, Suffix));
11816 Get_Name_String (Chars (E));
11817 Name_Len := Name_Len - 1;
11818 return Name_Find;
11819 end Remove_Suffix;
11820
11821 ----------------------------------
11822 -- Replace_Null_By_Null_Address --
11823 ----------------------------------
11824
11825 procedure Replace_Null_By_Null_Address (N : Node_Id) is
11826 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id);
11827 -- Replace operand Op with a reference to Null_Address when the operand
11828 -- denotes a null Address. Other_Op denotes the other operand.
11829
11830 --------------------------
11831 -- Replace_Null_Operand --
11832 --------------------------
11833
11834 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id) is
11835 begin
11836 -- Check the type of the complementary operand since the N_Null node
11837 -- has not been decorated yet.
11838
11839 if Nkind (Op) = N_Null
11840 and then Is_Descendant_Of_Address (Etype (Other_Op))
11841 then
11842 Rewrite (Op, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (Op)));
11843 end if;
11844 end Replace_Null_Operand;
11845
11846 -- Start of processing for Replace_Null_By_Null_Address
11847
11848 begin
11849 pragma Assert (Relaxed_RM_Semantics);
11850 pragma Assert (Nkind_In (N, N_Null,
11851 N_Op_Eq,
11852 N_Op_Ge,
11853 N_Op_Gt,
11854 N_Op_Le,
11855 N_Op_Lt,
11856 N_Op_Ne));
11857
11858 if Nkind (N) = N_Null then
11859 Rewrite (N, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
11860
11861 else
11862 declare
11863 L : constant Node_Id := Left_Opnd (N);
11864 R : constant Node_Id := Right_Opnd (N);
11865
11866 begin
11867 Replace_Null_Operand (L, Other_Op => R);
11868 Replace_Null_Operand (R, Other_Op => L);
11869 end;
11870 end if;
11871 end Replace_Null_By_Null_Address;
11872
11873 --------------------------
11874 -- Has_Tagged_Component --
11875 --------------------------
11876
11877 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
11878 Comp : Entity_Id;
11879
11880 begin
11881 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
11882 return Has_Tagged_Component (Underlying_Type (Typ));
11883
11884 elsif Is_Array_Type (Typ) then
11885 return Has_Tagged_Component (Component_Type (Typ));
11886
11887 elsif Is_Tagged_Type (Typ) then
11888 return True;
11889
11890 elsif Is_Record_Type (Typ) then
11891 Comp := First_Component (Typ);
11892 while Present (Comp) loop
11893 if Has_Tagged_Component (Etype (Comp)) then
11894 return True;
11895 end if;
11896
11897 Next_Component (Comp);
11898 end loop;
11899
11900 return False;
11901
11902 else
11903 return False;
11904 end if;
11905 end Has_Tagged_Component;
11906
11907 -----------------------------
11908 -- Has_Undefined_Reference --
11909 -----------------------------
11910
11911 function Has_Undefined_Reference (Expr : Node_Id) return Boolean is
11912 Has_Undef_Ref : Boolean := False;
11913 -- Flag set when expression Expr contains at least one undefined
11914 -- reference.
11915
11916 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result;
11917 -- Determine whether N denotes a reference and if it does, whether it is
11918 -- undefined.
11919
11920 ----------------------------
11921 -- Is_Undefined_Reference --
11922 ----------------------------
11923
11924 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result is
11925 begin
11926 if Is_Entity_Name (N)
11927 and then Present (Entity (N))
11928 and then Entity (N) = Any_Id
11929 then
11930 Has_Undef_Ref := True;
11931 return Abandon;
11932 end if;
11933
11934 return OK;
11935 end Is_Undefined_Reference;
11936
11937 procedure Find_Undefined_References is
11938 new Traverse_Proc (Is_Undefined_Reference);
11939
11940 -- Start of processing for Has_Undefined_Reference
11941
11942 begin
11943 Find_Undefined_References (Expr);
11944
11945 return Has_Undef_Ref;
11946 end Has_Undefined_Reference;
11947
11948 ----------------------------
11949 -- Has_Volatile_Component --
11950 ----------------------------
11951
11952 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
11953 Comp : Entity_Id;
11954
11955 begin
11956 if Has_Volatile_Components (Typ) then
11957 return True;
11958
11959 elsif Is_Array_Type (Typ) then
11960 return Is_Volatile (Component_Type (Typ));
11961
11962 elsif Is_Record_Type (Typ) then
11963 Comp := First_Component (Typ);
11964 while Present (Comp) loop
11965 if Is_Volatile_Object (Comp) then
11966 return True;
11967 end if;
11968
11969 Comp := Next_Component (Comp);
11970 end loop;
11971 end if;
11972
11973 return False;
11974 end Has_Volatile_Component;
11975
11976 -------------------------
11977 -- Implementation_Kind --
11978 -------------------------
11979
11980 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
11981 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
11982 Arg : Node_Id;
11983 begin
11984 pragma Assert (Present (Impl_Prag));
11985 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
11986 return Chars (Get_Pragma_Arg (Arg));
11987 end Implementation_Kind;
11988
11989 --------------------------
11990 -- Implements_Interface --
11991 --------------------------
11992
11993 function Implements_Interface
11994 (Typ_Ent : Entity_Id;
11995 Iface_Ent : Entity_Id;
11996 Exclude_Parents : Boolean := False) return Boolean
11997 is
11998 Ifaces_List : Elist_Id;
11999 Elmt : Elmt_Id;
12000 Iface : Entity_Id := Base_Type (Iface_Ent);
12001 Typ : Entity_Id := Base_Type (Typ_Ent);
12002
12003 begin
12004 if Is_Class_Wide_Type (Typ) then
12005 Typ := Root_Type (Typ);
12006 end if;
12007
12008 if not Has_Interfaces (Typ) then
12009 return False;
12010 end if;
12011
12012 if Is_Class_Wide_Type (Iface) then
12013 Iface := Root_Type (Iface);
12014 end if;
12015
12016 Collect_Interfaces (Typ, Ifaces_List);
12017
12018 Elmt := First_Elmt (Ifaces_List);
12019 while Present (Elmt) loop
12020 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
12021 and then Exclude_Parents
12022 then
12023 null;
12024
12025 elsif Node (Elmt) = Iface then
12026 return True;
12027 end if;
12028
12029 Next_Elmt (Elmt);
12030 end loop;
12031
12032 return False;
12033 end Implements_Interface;
12034
12035 ------------------------------------
12036 -- In_Assertion_Expression_Pragma --
12037 ------------------------------------
12038
12039 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
12040 Par : Node_Id;
12041 Prag : Node_Id := Empty;
12042
12043 begin
12044 -- Climb the parent chain looking for an enclosing pragma
12045
12046 Par := N;
12047 while Present (Par) loop
12048 if Nkind (Par) = N_Pragma then
12049 Prag := Par;
12050 exit;
12051
12052 -- Precondition-like pragmas are expanded into if statements, check
12053 -- the original node instead.
12054
12055 elsif Nkind (Original_Node (Par)) = N_Pragma then
12056 Prag := Original_Node (Par);
12057 exit;
12058
12059 -- The expansion of attribute 'Old generates a constant to capture
12060 -- the result of the prefix. If the parent traversal reaches
12061 -- one of these constants, then the node technically came from a
12062 -- postcondition-like pragma. Note that the Ekind is not tested here
12063 -- because N may be the expression of an object declaration which is
12064 -- currently being analyzed. Such objects carry Ekind of E_Void.
12065
12066 elsif Nkind (Par) = N_Object_Declaration
12067 and then Constant_Present (Par)
12068 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
12069 then
12070 return True;
12071
12072 -- Prevent the search from going too far
12073
12074 elsif Is_Body_Or_Package_Declaration (Par) then
12075 return False;
12076 end if;
12077
12078 Par := Parent (Par);
12079 end loop;
12080
12081 return
12082 Present (Prag)
12083 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
12084 end In_Assertion_Expression_Pragma;
12085
12086 ----------------------
12087 -- In_Generic_Scope --
12088 ----------------------
12089
12090 function In_Generic_Scope (E : Entity_Id) return Boolean is
12091 S : Entity_Id;
12092
12093 begin
12094 S := Scope (E);
12095 while Present (S) and then S /= Standard_Standard loop
12096 if Is_Generic_Unit (S) then
12097 return True;
12098 end if;
12099
12100 S := Scope (S);
12101 end loop;
12102
12103 return False;
12104 end In_Generic_Scope;
12105
12106 -----------------
12107 -- In_Instance --
12108 -----------------
12109
12110 function In_Instance return Boolean is
12111 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
12112 S : Entity_Id;
12113
12114 begin
12115 S := Current_Scope;
12116 while Present (S) and then S /= Standard_Standard loop
12117 if Is_Generic_Instance (S) then
12118
12119 -- A child instance is always compiled in the context of a parent
12120 -- instance. Nevertheless, the actuals are not analyzed in an
12121 -- instance context. We detect this case by examining the current
12122 -- compilation unit, which must be a child instance, and checking
12123 -- that it is not currently on the scope stack.
12124
12125 if Is_Child_Unit (Curr_Unit)
12126 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
12127 N_Package_Instantiation
12128 and then not In_Open_Scopes (Curr_Unit)
12129 then
12130 return False;
12131 else
12132 return True;
12133 end if;
12134 end if;
12135
12136 S := Scope (S);
12137 end loop;
12138
12139 return False;
12140 end In_Instance;
12141
12142 ----------------------
12143 -- In_Instance_Body --
12144 ----------------------
12145
12146 function In_Instance_Body return Boolean is
12147 S : Entity_Id;
12148
12149 begin
12150 S := Current_Scope;
12151 while Present (S) and then S /= Standard_Standard loop
12152 if Ekind_In (S, E_Function, E_Procedure)
12153 and then Is_Generic_Instance (S)
12154 then
12155 return True;
12156
12157 elsif Ekind (S) = E_Package
12158 and then In_Package_Body (S)
12159 and then Is_Generic_Instance (S)
12160 then
12161 return True;
12162 end if;
12163
12164 S := Scope (S);
12165 end loop;
12166
12167 return False;
12168 end In_Instance_Body;
12169
12170 -----------------------------
12171 -- In_Instance_Not_Visible --
12172 -----------------------------
12173
12174 function In_Instance_Not_Visible return Boolean is
12175 S : Entity_Id;
12176
12177 begin
12178 S := Current_Scope;
12179 while Present (S) and then S /= Standard_Standard loop
12180 if Ekind_In (S, E_Function, E_Procedure)
12181 and then Is_Generic_Instance (S)
12182 then
12183 return True;
12184
12185 elsif Ekind (S) = E_Package
12186 and then (In_Package_Body (S) or else In_Private_Part (S))
12187 and then Is_Generic_Instance (S)
12188 then
12189 return True;
12190 end if;
12191
12192 S := Scope (S);
12193 end loop;
12194
12195 return False;
12196 end In_Instance_Not_Visible;
12197
12198 ------------------------------
12199 -- In_Instance_Visible_Part --
12200 ------------------------------
12201
12202 function In_Instance_Visible_Part
12203 (Id : Entity_Id := Current_Scope) return Boolean
12204 is
12205 Inst : Entity_Id;
12206
12207 begin
12208 Inst := Id;
12209 while Present (Inst) and then Inst /= Standard_Standard loop
12210 if Ekind (Inst) = E_Package
12211 and then Is_Generic_Instance (Inst)
12212 and then not In_Package_Body (Inst)
12213 and then not In_Private_Part (Inst)
12214 then
12215 return True;
12216 end if;
12217
12218 Inst := Scope (Inst);
12219 end loop;
12220
12221 return False;
12222 end In_Instance_Visible_Part;
12223
12224 ---------------------
12225 -- In_Package_Body --
12226 ---------------------
12227
12228 function In_Package_Body return Boolean is
12229 S : Entity_Id;
12230
12231 begin
12232 S := Current_Scope;
12233 while Present (S) and then S /= Standard_Standard loop
12234 if Ekind (S) = E_Package and then In_Package_Body (S) then
12235 return True;
12236 else
12237 S := Scope (S);
12238 end if;
12239 end loop;
12240
12241 return False;
12242 end In_Package_Body;
12243
12244 --------------------------
12245 -- In_Pragma_Expression --
12246 --------------------------
12247
12248 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
12249 P : Node_Id;
12250 begin
12251 P := Parent (N);
12252 loop
12253 if No (P) then
12254 return False;
12255 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
12256 return True;
12257 else
12258 P := Parent (P);
12259 end if;
12260 end loop;
12261 end In_Pragma_Expression;
12262
12263 ---------------------------
12264 -- In_Pre_Post_Condition --
12265 ---------------------------
12266
12267 function In_Pre_Post_Condition (N : Node_Id) return Boolean is
12268 Par : Node_Id;
12269 Prag : Node_Id := Empty;
12270 Prag_Id : Pragma_Id;
12271
12272 begin
12273 -- Climb the parent chain looking for an enclosing pragma
12274
12275 Par := N;
12276 while Present (Par) loop
12277 if Nkind (Par) = N_Pragma then
12278 Prag := Par;
12279 exit;
12280
12281 -- Prevent the search from going too far
12282
12283 elsif Is_Body_Or_Package_Declaration (Par) then
12284 exit;
12285 end if;
12286
12287 Par := Parent (Par);
12288 end loop;
12289
12290 if Present (Prag) then
12291 Prag_Id := Get_Pragma_Id (Prag);
12292
12293 return
12294 Prag_Id = Pragma_Post
12295 or else Prag_Id = Pragma_Post_Class
12296 or else Prag_Id = Pragma_Postcondition
12297 or else Prag_Id = Pragma_Pre
12298 or else Prag_Id = Pragma_Pre_Class
12299 or else Prag_Id = Pragma_Precondition;
12300
12301 -- Otherwise the node is not enclosed by a pre/postcondition pragma
12302
12303 else
12304 return False;
12305 end if;
12306 end In_Pre_Post_Condition;
12307
12308 -------------------------------------
12309 -- In_Reverse_Storage_Order_Object --
12310 -------------------------------------
12311
12312 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
12313 Pref : Node_Id;
12314 Btyp : Entity_Id := Empty;
12315
12316 begin
12317 -- Climb up indexed components
12318
12319 Pref := N;
12320 loop
12321 case Nkind (Pref) is
12322 when N_Selected_Component =>
12323 Pref := Prefix (Pref);
12324 exit;
12325
12326 when N_Indexed_Component =>
12327 Pref := Prefix (Pref);
12328
12329 when others =>
12330 Pref := Empty;
12331 exit;
12332 end case;
12333 end loop;
12334
12335 if Present (Pref) then
12336 Btyp := Base_Type (Etype (Pref));
12337 end if;
12338
12339 return Present (Btyp)
12340 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
12341 and then Reverse_Storage_Order (Btyp);
12342 end In_Reverse_Storage_Order_Object;
12343
12344 ------------------------------
12345 -- In_Same_Declarative_Part --
12346 ------------------------------
12347
12348 function In_Same_Declarative_Part
12349 (Context : Node_Id;
12350 N : Node_Id) return Boolean
12351 is
12352 Cont : Node_Id := Context;
12353 Nod : Node_Id;
12354
12355 begin
12356 if Nkind (Cont) = N_Compilation_Unit_Aux then
12357 Cont := Parent (Cont);
12358 end if;
12359
12360 Nod := Parent (N);
12361 while Present (Nod) loop
12362 if Nod = Cont then
12363 return True;
12364
12365 elsif Nkind_In (Nod, N_Accept_Statement,
12366 N_Block_Statement,
12367 N_Compilation_Unit,
12368 N_Entry_Body,
12369 N_Package_Body,
12370 N_Package_Declaration,
12371 N_Protected_Body,
12372 N_Subprogram_Body,
12373 N_Task_Body)
12374 then
12375 return False;
12376
12377 elsif Nkind (Nod) = N_Subunit then
12378 Nod := Corresponding_Stub (Nod);
12379
12380 else
12381 Nod := Parent (Nod);
12382 end if;
12383 end loop;
12384
12385 return False;
12386 end In_Same_Declarative_Part;
12387
12388 --------------------------------------
12389 -- In_Subprogram_Or_Concurrent_Unit --
12390 --------------------------------------
12391
12392 function In_Subprogram_Or_Concurrent_Unit return Boolean is
12393 E : Entity_Id;
12394 K : Entity_Kind;
12395
12396 begin
12397 -- Use scope chain to check successively outer scopes
12398
12399 E := Current_Scope;
12400 loop
12401 K := Ekind (E);
12402
12403 if K in Subprogram_Kind
12404 or else K in Concurrent_Kind
12405 or else K in Generic_Subprogram_Kind
12406 then
12407 return True;
12408
12409 elsif E = Standard_Standard then
12410 return False;
12411 end if;
12412
12413 E := Scope (E);
12414 end loop;
12415 end In_Subprogram_Or_Concurrent_Unit;
12416
12417 ----------------
12418 -- In_Subtree --
12419 ----------------
12420
12421 function In_Subtree (N : Node_Id; Root : Node_Id) return Boolean is
12422 Curr : Node_Id;
12423
12424 begin
12425 Curr := N;
12426 while Present (Curr) loop
12427 if Curr = Root then
12428 return True;
12429 end if;
12430
12431 Curr := Parent (Curr);
12432 end loop;
12433
12434 return False;
12435 end In_Subtree;
12436
12437 ----------------
12438 -- In_Subtree --
12439 ----------------
12440
12441 function In_Subtree
12442 (N : Node_Id;
12443 Root1 : Node_Id;
12444 Root2 : Node_Id) return Boolean
12445 is
12446 Curr : Node_Id;
12447
12448 begin
12449 Curr := N;
12450 while Present (Curr) loop
12451 if Curr = Root1 or else Curr = Root2 then
12452 return True;
12453 end if;
12454
12455 Curr := Parent (Curr);
12456 end loop;
12457
12458 return False;
12459 end In_Subtree;
12460
12461 ---------------------
12462 -- In_Visible_Part --
12463 ---------------------
12464
12465 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
12466 begin
12467 return Is_Package_Or_Generic_Package (Scope_Id)
12468 and then In_Open_Scopes (Scope_Id)
12469 and then not In_Package_Body (Scope_Id)
12470 and then not In_Private_Part (Scope_Id);
12471 end In_Visible_Part;
12472
12473 --------------------------------
12474 -- Incomplete_Or_Partial_View --
12475 --------------------------------
12476
12477 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
12478 function Inspect_Decls
12479 (Decls : List_Id;
12480 Taft : Boolean := False) return Entity_Id;
12481 -- Check whether a declarative region contains the incomplete or partial
12482 -- view of Id.
12483
12484 -------------------
12485 -- Inspect_Decls --
12486 -------------------
12487
12488 function Inspect_Decls
12489 (Decls : List_Id;
12490 Taft : Boolean := False) return Entity_Id
12491 is
12492 Decl : Node_Id;
12493 Match : Node_Id;
12494
12495 begin
12496 Decl := First (Decls);
12497 while Present (Decl) loop
12498 Match := Empty;
12499
12500 -- The partial view of a Taft-amendment type is an incomplete
12501 -- type.
12502
12503 if Taft then
12504 if Nkind (Decl) = N_Incomplete_Type_Declaration then
12505 Match := Defining_Identifier (Decl);
12506 end if;
12507
12508 -- Otherwise look for a private type whose full view matches the
12509 -- input type. Note that this checks full_type_declaration nodes
12510 -- to account for derivations from a private type where the type
12511 -- declaration hold the partial view and the full view is an
12512 -- itype.
12513
12514 elsif Nkind_In (Decl, N_Full_Type_Declaration,
12515 N_Private_Extension_Declaration,
12516 N_Private_Type_Declaration)
12517 then
12518 Match := Defining_Identifier (Decl);
12519 end if;
12520
12521 -- Guard against unanalyzed entities
12522
12523 if Present (Match)
12524 and then Is_Type (Match)
12525 and then Present (Full_View (Match))
12526 and then Full_View (Match) = Id
12527 then
12528 return Match;
12529 end if;
12530
12531 Next (Decl);
12532 end loop;
12533
12534 return Empty;
12535 end Inspect_Decls;
12536
12537 -- Local variables
12538
12539 Prev : Entity_Id;
12540
12541 -- Start of processing for Incomplete_Or_Partial_View
12542
12543 begin
12544 -- Deferred constant or incomplete type case
12545
12546 Prev := Current_Entity_In_Scope (Id);
12547
12548 if Present (Prev)
12549 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
12550 and then Present (Full_View (Prev))
12551 and then Full_View (Prev) = Id
12552 then
12553 return Prev;
12554 end if;
12555
12556 -- Private or Taft amendment type case
12557
12558 declare
12559 Pkg : constant Entity_Id := Scope (Id);
12560 Pkg_Decl : Node_Id := Pkg;
12561
12562 begin
12563 if Present (Pkg)
12564 and then Ekind_In (Pkg, E_Generic_Package, E_Package)
12565 then
12566 while Nkind (Pkg_Decl) /= N_Package_Specification loop
12567 Pkg_Decl := Parent (Pkg_Decl);
12568 end loop;
12569
12570 -- It is knows that Typ has a private view, look for it in the
12571 -- visible declarations of the enclosing scope. A special case
12572 -- of this is when the two views have been exchanged - the full
12573 -- appears earlier than the private.
12574
12575 if Has_Private_Declaration (Id) then
12576 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
12577
12578 -- Exchanged view case, look in the private declarations
12579
12580 if No (Prev) then
12581 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
12582 end if;
12583
12584 return Prev;
12585
12586 -- Otherwise if this is the package body, then Typ is a potential
12587 -- Taft amendment type. The incomplete view should be located in
12588 -- the private declarations of the enclosing scope.
12589
12590 elsif In_Package_Body (Pkg) then
12591 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
12592 end if;
12593 end if;
12594 end;
12595
12596 -- The type has no incomplete or private view
12597
12598 return Empty;
12599 end Incomplete_Or_Partial_View;
12600
12601 ---------------------------------------
12602 -- Incomplete_View_From_Limited_With --
12603 ---------------------------------------
12604
12605 function Incomplete_View_From_Limited_With
12606 (Typ : Entity_Id) return Entity_Id
12607 is
12608 begin
12609 -- It might make sense to make this an attribute in Einfo, and set it
12610 -- in Sem_Ch10 in Build_Shadow_Entity. However, we're running short on
12611 -- slots for new attributes, and it seems a bit simpler to just search
12612 -- the Limited_View (if it exists) for an incomplete type whose
12613 -- Non_Limited_View is Typ.
12614
12615 if Ekind (Scope (Typ)) = E_Package
12616 and then Present (Limited_View (Scope (Typ)))
12617 then
12618 declare
12619 Ent : Entity_Id := First_Entity (Limited_View (Scope (Typ)));
12620 begin
12621 while Present (Ent) loop
12622 if Ekind (Ent) in Incomplete_Kind
12623 and then Non_Limited_View (Ent) = Typ
12624 then
12625 return Ent;
12626 end if;
12627
12628 Ent := Next_Entity (Ent);
12629 end loop;
12630 end;
12631 end if;
12632
12633 return Typ;
12634 end Incomplete_View_From_Limited_With;
12635
12636 ----------------------------------
12637 -- Indexed_Component_Bit_Offset --
12638 ----------------------------------
12639
12640 function Indexed_Component_Bit_Offset (N : Node_Id) return Uint is
12641 Exp : constant Node_Id := First (Expressions (N));
12642 Typ : constant Entity_Id := Etype (Prefix (N));
12643 Off : constant Uint := Component_Size (Typ);
12644 Ind : Node_Id;
12645
12646 begin
12647 -- Return early if the component size is not known or variable
12648
12649 if Off = No_Uint or else Off < Uint_0 then
12650 return No_Uint;
12651 end if;
12652
12653 -- Deal with the degenerate case of an empty component
12654
12655 if Off = Uint_0 then
12656 return Off;
12657 end if;
12658
12659 -- Check that both the index value and the low bound are known
12660
12661 if not Compile_Time_Known_Value (Exp) then
12662 return No_Uint;
12663 end if;
12664
12665 Ind := First_Index (Typ);
12666 if No (Ind) then
12667 return No_Uint;
12668 end if;
12669
12670 if Nkind (Ind) = N_Subtype_Indication then
12671 Ind := Constraint (Ind);
12672
12673 if Nkind (Ind) = N_Range_Constraint then
12674 Ind := Range_Expression (Ind);
12675 end if;
12676 end if;
12677
12678 if Nkind (Ind) /= N_Range
12679 or else not Compile_Time_Known_Value (Low_Bound (Ind))
12680 then
12681 return No_Uint;
12682 end if;
12683
12684 -- Return the scaled offset
12685
12686 return Off * (Expr_Value (Exp) - Expr_Value (Low_Bound ((Ind))));
12687 end Indexed_Component_Bit_Offset;
12688
12689 ----------------------------
12690 -- Inherit_Rep_Item_Chain --
12691 ----------------------------
12692
12693 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
12694 Item : Node_Id;
12695 Next_Item : Node_Id;
12696
12697 begin
12698 -- There are several inheritance scenarios to consider depending on
12699 -- whether both types have rep item chains and whether the destination
12700 -- type already inherits part of the source type's rep item chain.
12701
12702 -- 1) The source type lacks a rep item chain
12703 -- From_Typ ---> Empty
12704 --
12705 -- Typ --------> Item (or Empty)
12706
12707 -- In this case inheritance cannot take place because there are no items
12708 -- to inherit.
12709
12710 -- 2) The destination type lacks a rep item chain
12711 -- From_Typ ---> Item ---> ...
12712 --
12713 -- Typ --------> Empty
12714
12715 -- Inheritance takes place by setting the First_Rep_Item of the
12716 -- destination type to the First_Rep_Item of the source type.
12717 -- From_Typ ---> Item ---> ...
12718 -- ^
12719 -- Typ -----------+
12720
12721 -- 3.1) Both source and destination types have at least one rep item.
12722 -- The destination type does NOT inherit a rep item from the source
12723 -- type.
12724 -- From_Typ ---> Item ---> Item
12725 --
12726 -- Typ --------> Item ---> Item
12727
12728 -- Inheritance takes place by setting the Next_Rep_Item of the last item
12729 -- of the destination type to the First_Rep_Item of the source type.
12730 -- From_Typ -------------------> Item ---> Item
12731 -- ^
12732 -- Typ --------> Item ---> Item --+
12733
12734 -- 3.2) Both source and destination types have at least one rep item.
12735 -- The destination type DOES inherit part of the rep item chain of the
12736 -- source type.
12737 -- From_Typ ---> Item ---> Item ---> Item
12738 -- ^
12739 -- Typ --------> Item ------+
12740
12741 -- This rare case arises when the full view of a private extension must
12742 -- inherit the rep item chain from the full view of its parent type and
12743 -- the full view of the parent type contains extra rep items. Currently
12744 -- only invariants may lead to such form of inheritance.
12745
12746 -- type From_Typ is tagged private
12747 -- with Type_Invariant'Class => Item_2;
12748
12749 -- type Typ is new From_Typ with private
12750 -- with Type_Invariant => Item_4;
12751
12752 -- At this point the rep item chains contain the following items
12753
12754 -- From_Typ -----------> Item_2 ---> Item_3
12755 -- ^
12756 -- Typ --------> Item_4 --+
12757
12758 -- The full views of both types may introduce extra invariants
12759
12760 -- type From_Typ is tagged null record
12761 -- with Type_Invariant => Item_1;
12762
12763 -- type Typ is new From_Typ with null record;
12764
12765 -- The full view of Typ would have to inherit any new rep items added to
12766 -- the full view of From_Typ.
12767
12768 -- From_Typ -----------> Item_1 ---> Item_2 ---> Item_3
12769 -- ^
12770 -- Typ --------> Item_4 --+
12771
12772 -- To achieve this form of inheritance, the destination type must first
12773 -- sever the link between its own rep chain and that of the source type,
12774 -- then inheritance 3.1 takes place.
12775
12776 -- Case 1: The source type lacks a rep item chain
12777
12778 if No (First_Rep_Item (From_Typ)) then
12779 return;
12780
12781 -- Case 2: The destination type lacks a rep item chain
12782
12783 elsif No (First_Rep_Item (Typ)) then
12784 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
12785
12786 -- Case 3: Both the source and destination types have at least one rep
12787 -- item. Traverse the rep item chain of the destination type to find the
12788 -- last rep item.
12789
12790 else
12791 Item := Empty;
12792 Next_Item := First_Rep_Item (Typ);
12793 while Present (Next_Item) loop
12794
12795 -- Detect a link between the destination type's rep chain and that
12796 -- of the source type. There are two possibilities:
12797
12798 -- Variant 1
12799 -- Next_Item
12800 -- V
12801 -- From_Typ ---> Item_1 --->
12802 -- ^
12803 -- Typ -----------+
12804 --
12805 -- Item is Empty
12806
12807 -- Variant 2
12808 -- Next_Item
12809 -- V
12810 -- From_Typ ---> Item_1 ---> Item_2 --->
12811 -- ^
12812 -- Typ --------> Item_3 ------+
12813 -- ^
12814 -- Item
12815
12816 if Has_Rep_Item (From_Typ, Next_Item) then
12817 exit;
12818 end if;
12819
12820 Item := Next_Item;
12821 Next_Item := Next_Rep_Item (Next_Item);
12822 end loop;
12823
12824 -- Inherit the source type's rep item chain
12825
12826 if Present (Item) then
12827 Set_Next_Rep_Item (Item, First_Rep_Item (From_Typ));
12828 else
12829 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
12830 end if;
12831 end if;
12832 end Inherit_Rep_Item_Chain;
12833
12834 ------------------------------------
12835 -- Inherits_From_Tagged_Full_View --
12836 ------------------------------------
12837
12838 function Inherits_From_Tagged_Full_View (Typ : Entity_Id) return Boolean is
12839 begin
12840 return Is_Private_Type (Typ)
12841 and then Present (Full_View (Typ))
12842 and then Is_Private_Type (Full_View (Typ))
12843 and then not Is_Tagged_Type (Full_View (Typ))
12844 and then Present (Underlying_Type (Full_View (Typ)))
12845 and then Is_Tagged_Type (Underlying_Type (Full_View (Typ)));
12846 end Inherits_From_Tagged_Full_View;
12847
12848 ---------------------------------
12849 -- Insert_Explicit_Dereference --
12850 ---------------------------------
12851
12852 procedure Insert_Explicit_Dereference (N : Node_Id) is
12853 New_Prefix : constant Node_Id := Relocate_Node (N);
12854 Ent : Entity_Id := Empty;
12855 Pref : Node_Id;
12856 I : Interp_Index;
12857 It : Interp;
12858 T : Entity_Id;
12859
12860 begin
12861 Save_Interps (N, New_Prefix);
12862
12863 Rewrite (N,
12864 Make_Explicit_Dereference (Sloc (Parent (N)),
12865 Prefix => New_Prefix));
12866
12867 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
12868
12869 if Is_Overloaded (New_Prefix) then
12870
12871 -- The dereference is also overloaded, and its interpretations are
12872 -- the designated types of the interpretations of the original node.
12873
12874 Set_Etype (N, Any_Type);
12875
12876 Get_First_Interp (New_Prefix, I, It);
12877 while Present (It.Nam) loop
12878 T := It.Typ;
12879
12880 if Is_Access_Type (T) then
12881 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
12882 end if;
12883
12884 Get_Next_Interp (I, It);
12885 end loop;
12886
12887 End_Interp_List;
12888
12889 else
12890 -- Prefix is unambiguous: mark the original prefix (which might
12891 -- Come_From_Source) as a reference, since the new (relocated) one
12892 -- won't be taken into account.
12893
12894 if Is_Entity_Name (New_Prefix) then
12895 Ent := Entity (New_Prefix);
12896 Pref := New_Prefix;
12897
12898 -- For a retrieval of a subcomponent of some composite object,
12899 -- retrieve the ultimate entity if there is one.
12900
12901 elsif Nkind_In (New_Prefix, N_Selected_Component,
12902 N_Indexed_Component)
12903 then
12904 Pref := Prefix (New_Prefix);
12905 while Present (Pref)
12906 and then Nkind_In (Pref, N_Selected_Component,
12907 N_Indexed_Component)
12908 loop
12909 Pref := Prefix (Pref);
12910 end loop;
12911
12912 if Present (Pref) and then Is_Entity_Name (Pref) then
12913 Ent := Entity (Pref);
12914 end if;
12915 end if;
12916
12917 -- Place the reference on the entity node
12918
12919 if Present (Ent) then
12920 Generate_Reference (Ent, Pref);
12921 end if;
12922 end if;
12923 end Insert_Explicit_Dereference;
12924
12925 ------------------------------------------
12926 -- Inspect_Deferred_Constant_Completion --
12927 ------------------------------------------
12928
12929 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
12930 Decl : Node_Id;
12931
12932 begin
12933 Decl := First (Decls);
12934 while Present (Decl) loop
12935
12936 -- Deferred constant signature
12937
12938 if Nkind (Decl) = N_Object_Declaration
12939 and then Constant_Present (Decl)
12940 and then No (Expression (Decl))
12941
12942 -- No need to check internally generated constants
12943
12944 and then Comes_From_Source (Decl)
12945
12946 -- The constant is not completed. A full object declaration or a
12947 -- pragma Import complete a deferred constant.
12948
12949 and then not Has_Completion (Defining_Identifier (Decl))
12950 then
12951 Error_Msg_N
12952 ("constant declaration requires initialization expression",
12953 Defining_Identifier (Decl));
12954 end if;
12955
12956 Decl := Next (Decl);
12957 end loop;
12958 end Inspect_Deferred_Constant_Completion;
12959
12960 -------------------------------
12961 -- Install_Elaboration_Model --
12962 -------------------------------
12963
12964 procedure Install_Elaboration_Model (Unit_Id : Entity_Id) is
12965 function Find_Elaboration_Checks_Pragma (L : List_Id) return Node_Id;
12966 -- Try to find pragma Elaboration_Checks in arbitrary list L. Return
12967 -- Empty if there is no such pragma.
12968
12969 ------------------------------------
12970 -- Find_Elaboration_Checks_Pragma --
12971 ------------------------------------
12972
12973 function Find_Elaboration_Checks_Pragma (L : List_Id) return Node_Id is
12974 Item : Node_Id;
12975
12976 begin
12977 Item := First (L);
12978 while Present (Item) loop
12979 if Nkind (Item) = N_Pragma
12980 and then Pragma_Name (Item) = Name_Elaboration_Checks
12981 then
12982 return Item;
12983 end if;
12984
12985 Next (Item);
12986 end loop;
12987
12988 return Empty;
12989 end Find_Elaboration_Checks_Pragma;
12990
12991 -- Local variables
12992
12993 Args : List_Id;
12994 Model : Node_Id;
12995 Prag : Node_Id;
12996 Unit : Node_Id;
12997
12998 -- Start of processing for Install_Elaboration_Model
12999
13000 begin
13001 -- Nothing to do when the unit does not exist
13002
13003 if No (Unit_Id) then
13004 return;
13005 end if;
13006
13007 Unit := Parent (Unit_Declaration_Node (Unit_Id));
13008
13009 -- Nothing to do when the unit is not a library unit
13010
13011 if Nkind (Unit) /= N_Compilation_Unit then
13012 return;
13013 end if;
13014
13015 Prag := Find_Elaboration_Checks_Pragma (Context_Items (Unit));
13016
13017 -- The compilation unit is subject to pragma Elaboration_Checks. Set the
13018 -- elaboration model as specified by the pragma.
13019
13020 if Present (Prag) then
13021 Args := Pragma_Argument_Associations (Prag);
13022
13023 -- Guard against an illegal pragma. The sole argument must be an
13024 -- identifier which specifies either Dynamic or Static model.
13025
13026 if Present (Args) then
13027 Model := Get_Pragma_Arg (First (Args));
13028
13029 if Nkind (Model) = N_Identifier then
13030 Dynamic_Elaboration_Checks := Chars (Model) = Name_Dynamic;
13031 end if;
13032 end if;
13033 end if;
13034 end Install_Elaboration_Model;
13035
13036 -----------------------------
13037 -- Install_Generic_Formals --
13038 -----------------------------
13039
13040 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
13041 E : Entity_Id;
13042
13043 begin
13044 pragma Assert (Is_Generic_Subprogram (Subp_Id));
13045
13046 E := First_Entity (Subp_Id);
13047 while Present (E) loop
13048 Install_Entity (E);
13049 Next_Entity (E);
13050 end loop;
13051 end Install_Generic_Formals;
13052
13053 ------------------------
13054 -- Install_SPARK_Mode --
13055 ------------------------
13056
13057 procedure Install_SPARK_Mode (Mode : SPARK_Mode_Type; Prag : Node_Id) is
13058 begin
13059 SPARK_Mode := Mode;
13060 SPARK_Mode_Pragma := Prag;
13061 end Install_SPARK_Mode;
13062
13063 --------------------------
13064 -- Invalid_Scalar_Value --
13065 --------------------------
13066
13067 function Invalid_Scalar_Value
13068 (Loc : Source_Ptr;
13069 Scal_Typ : Scalar_Id) return Node_Id
13070 is
13071 function Invalid_Binder_Value return Node_Id;
13072 -- Return a reference to the corresponding invalid value for type
13073 -- Scal_Typ as defined in unit System.Scalar_Values.
13074
13075 function Invalid_Float_Value return Node_Id;
13076 -- Return the invalid value of float type Scal_Typ
13077
13078 function Invalid_Integer_Value return Node_Id;
13079 -- Return the invalid value of integer type Scal_Typ
13080
13081 procedure Set_Invalid_Binder_Values;
13082 -- Set the contents of collection Invalid_Binder_Values
13083
13084 --------------------------
13085 -- Invalid_Binder_Value --
13086 --------------------------
13087
13088 function Invalid_Binder_Value return Node_Id is
13089 Val_Id : Entity_Id;
13090
13091 begin
13092 -- Initialize the collection of invalid binder values the first time
13093 -- around.
13094
13095 Set_Invalid_Binder_Values;
13096
13097 -- Obtain the corresponding variable from System.Scalar_Values which
13098 -- holds the invalid value for this type.
13099
13100 Val_Id := Invalid_Binder_Values (Scal_Typ);
13101 pragma Assert (Present (Val_Id));
13102
13103 return New_Occurrence_Of (Val_Id, Loc);
13104 end Invalid_Binder_Value;
13105
13106 -------------------------
13107 -- Invalid_Float_Value --
13108 -------------------------
13109
13110 function Invalid_Float_Value return Node_Id is
13111 Value : constant Ureal := Invalid_Floats (Scal_Typ);
13112
13113 begin
13114 -- Pragma Invalid_Scalars did not specify an invalid value for this
13115 -- type. Fall back to the value provided by the binder.
13116
13117 if Value = No_Ureal then
13118 return Invalid_Binder_Value;
13119 else
13120 return Make_Real_Literal (Loc, Realval => Value);
13121 end if;
13122 end Invalid_Float_Value;
13123
13124 ---------------------------
13125 -- Invalid_Integer_Value --
13126 ---------------------------
13127
13128 function Invalid_Integer_Value return Node_Id is
13129 Value : constant Uint := Invalid_Integers (Scal_Typ);
13130
13131 begin
13132 -- Pragma Invalid_Scalars did not specify an invalid value for this
13133 -- type. Fall back to the value provided by the binder.
13134
13135 if Value = No_Uint then
13136 return Invalid_Binder_Value;
13137 else
13138 return Make_Integer_Literal (Loc, Intval => Value);
13139 end if;
13140 end Invalid_Integer_Value;
13141
13142 -------------------------------
13143 -- Set_Invalid_Binder_Values --
13144 -------------------------------
13145
13146 procedure Set_Invalid_Binder_Values is
13147 begin
13148 if not Invalid_Binder_Values_Set then
13149 Invalid_Binder_Values_Set := True;
13150
13151 -- Initialize the contents of the collection once since RTE calls
13152 -- are not cheap.
13153
13154 Invalid_Binder_Values :=
13155 (Name_Short_Float => RTE (RE_IS_Isf),
13156 Name_Float => RTE (RE_IS_Ifl),
13157 Name_Long_Float => RTE (RE_IS_Ilf),
13158 Name_Long_Long_Float => RTE (RE_IS_Ill),
13159 Name_Signed_8 => RTE (RE_IS_Is1),
13160 Name_Signed_16 => RTE (RE_IS_Is2),
13161 Name_Signed_32 => RTE (RE_IS_Is4),
13162 Name_Signed_64 => RTE (RE_IS_Is8),
13163 Name_Unsigned_8 => RTE (RE_IS_Iu1),
13164 Name_Unsigned_16 => RTE (RE_IS_Iu2),
13165 Name_Unsigned_32 => RTE (RE_IS_Iu4),
13166 Name_Unsigned_64 => RTE (RE_IS_Iu8));
13167 end if;
13168 end Set_Invalid_Binder_Values;
13169
13170 -- Start of processing for Invalid_Scalar_Value
13171
13172 begin
13173 if Scal_Typ in Float_Scalar_Id then
13174 return Invalid_Float_Value;
13175
13176 else pragma Assert (Scal_Typ in Integer_Scalar_Id);
13177 return Invalid_Integer_Value;
13178 end if;
13179 end Invalid_Scalar_Value;
13180
13181 -----------------------------
13182 -- Is_Actual_Out_Parameter --
13183 -----------------------------
13184
13185 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
13186 Formal : Entity_Id;
13187 Call : Node_Id;
13188 begin
13189 Find_Actual (N, Formal, Call);
13190 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
13191 end Is_Actual_Out_Parameter;
13192
13193 -------------------------
13194 -- Is_Actual_Parameter --
13195 -------------------------
13196
13197 function Is_Actual_Parameter (N : Node_Id) return Boolean is
13198 PK : constant Node_Kind := Nkind (Parent (N));
13199
13200 begin
13201 case PK is
13202 when N_Parameter_Association =>
13203 return N = Explicit_Actual_Parameter (Parent (N));
13204
13205 when N_Subprogram_Call =>
13206 return Is_List_Member (N)
13207 and then
13208 List_Containing (N) = Parameter_Associations (Parent (N));
13209
13210 when others =>
13211 return False;
13212 end case;
13213 end Is_Actual_Parameter;
13214
13215 --------------------------------
13216 -- Is_Actual_Tagged_Parameter --
13217 --------------------------------
13218
13219 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
13220 Formal : Entity_Id;
13221 Call : Node_Id;
13222 begin
13223 Find_Actual (N, Formal, Call);
13224 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
13225 end Is_Actual_Tagged_Parameter;
13226
13227 ---------------------
13228 -- Is_Aliased_View --
13229 ---------------------
13230
13231 function Is_Aliased_View (Obj : Node_Id) return Boolean is
13232 E : Entity_Id;
13233
13234 begin
13235 if Is_Entity_Name (Obj) then
13236 E := Entity (Obj);
13237
13238 return
13239 (Is_Object (E)
13240 and then
13241 (Is_Aliased (E)
13242 or else (Present (Renamed_Object (E))
13243 and then Is_Aliased_View (Renamed_Object (E)))))
13244
13245 or else ((Is_Formal (E) or else Is_Formal_Object (E))
13246 and then Is_Tagged_Type (Etype (E)))
13247
13248 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
13249
13250 -- Current instance of type, either directly or as rewritten
13251 -- reference to the current object.
13252
13253 or else (Is_Entity_Name (Original_Node (Obj))
13254 and then Present (Entity (Original_Node (Obj)))
13255 and then Is_Type (Entity (Original_Node (Obj))))
13256
13257 or else (Is_Type (E) and then E = Current_Scope)
13258
13259 or else (Is_Incomplete_Or_Private_Type (E)
13260 and then Full_View (E) = Current_Scope)
13261
13262 -- Ada 2012 AI05-0053: the return object of an extended return
13263 -- statement is aliased if its type is immutably limited.
13264
13265 or else (Is_Return_Object (E)
13266 and then Is_Limited_View (Etype (E)));
13267
13268 elsif Nkind (Obj) = N_Selected_Component then
13269 return Is_Aliased (Entity (Selector_Name (Obj)));
13270
13271 elsif Nkind (Obj) = N_Indexed_Component then
13272 return Has_Aliased_Components (Etype (Prefix (Obj)))
13273 or else
13274 (Is_Access_Type (Etype (Prefix (Obj)))
13275 and then Has_Aliased_Components
13276 (Designated_Type (Etype (Prefix (Obj)))));
13277
13278 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
13279 return Is_Tagged_Type (Etype (Obj))
13280 and then Is_Aliased_View (Expression (Obj));
13281
13282 elsif Nkind (Obj) = N_Explicit_Dereference then
13283 return Nkind (Original_Node (Obj)) /= N_Function_Call;
13284
13285 else
13286 return False;
13287 end if;
13288 end Is_Aliased_View;
13289
13290 -------------------------
13291 -- Is_Ancestor_Package --
13292 -------------------------
13293
13294 function Is_Ancestor_Package
13295 (E1 : Entity_Id;
13296 E2 : Entity_Id) return Boolean
13297 is
13298 Par : Entity_Id;
13299
13300 begin
13301 Par := E2;
13302 while Present (Par) and then Par /= Standard_Standard loop
13303 if Par = E1 then
13304 return True;
13305 end if;
13306
13307 Par := Scope (Par);
13308 end loop;
13309
13310 return False;
13311 end Is_Ancestor_Package;
13312
13313 ----------------------
13314 -- Is_Atomic_Object --
13315 ----------------------
13316
13317 function Is_Atomic_Object (N : Node_Id) return Boolean is
13318 function Is_Atomic_Entity (Id : Entity_Id) return Boolean;
13319 pragma Inline (Is_Atomic_Entity);
13320 -- Determine whether arbitrary entity Id is either atomic or has atomic
13321 -- components.
13322
13323 function Is_Atomic_Prefix (Pref : Node_Id) return Boolean;
13324 -- Determine whether prefix Pref of an indexed or selected component is
13325 -- an atomic object.
13326
13327 ----------------------
13328 -- Is_Atomic_Entity --
13329 ----------------------
13330
13331 function Is_Atomic_Entity (Id : Entity_Id) return Boolean is
13332 begin
13333 return Is_Atomic (Id) or else Has_Atomic_Components (Id);
13334 end Is_Atomic_Entity;
13335
13336 ----------------------
13337 -- Is_Atomic_Prefix --
13338 ----------------------
13339
13340 function Is_Atomic_Prefix (Pref : Node_Id) return Boolean is
13341 Typ : constant Entity_Id := Etype (Pref);
13342
13343 begin
13344 if Is_Access_Type (Typ) then
13345 return Has_Atomic_Components (Designated_Type (Typ));
13346
13347 elsif Is_Atomic_Entity (Typ) then
13348 return True;
13349
13350 elsif Is_Entity_Name (Pref)
13351 and then Is_Atomic_Entity (Entity (Pref))
13352 then
13353 return True;
13354
13355 elsif Nkind (Pref) = N_Indexed_Component then
13356 return Is_Atomic_Prefix (Prefix (Pref));
13357
13358 elsif Nkind (Pref) = N_Selected_Component then
13359 return
13360 Is_Atomic_Prefix (Prefix (Pref))
13361 or else Is_Atomic (Entity (Selector_Name (Pref)));
13362 end if;
13363
13364 return False;
13365 end Is_Atomic_Prefix;
13366
13367 -- Start of processing for Is_Atomic_Object
13368
13369 begin
13370 if Is_Entity_Name (N) then
13371 return Is_Atomic_Object_Entity (Entity (N));
13372
13373 elsif Nkind (N) = N_Indexed_Component then
13374 return Is_Atomic (Etype (N)) or else Is_Atomic_Prefix (Prefix (N));
13375
13376 elsif Nkind (N) = N_Selected_Component then
13377 return
13378 Is_Atomic (Etype (N))
13379 or else Is_Atomic_Prefix (Prefix (N))
13380 or else Is_Atomic (Entity (Selector_Name (N)));
13381 end if;
13382
13383 return False;
13384 end Is_Atomic_Object;
13385
13386 -----------------------------
13387 -- Is_Atomic_Object_Entity --
13388 -----------------------------
13389
13390 function Is_Atomic_Object_Entity (Id : Entity_Id) return Boolean is
13391 begin
13392 return
13393 Is_Object (Id)
13394 and then (Is_Atomic (Id) or else Is_Atomic (Etype (Id)));
13395 end Is_Atomic_Object_Entity;
13396
13397 -----------------------------
13398 -- Is_Atomic_Or_VFA_Object --
13399 -----------------------------
13400
13401 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
13402 begin
13403 return Is_Atomic_Object (N)
13404 or else (Is_Object_Reference (N)
13405 and then Is_Entity_Name (N)
13406 and then (Is_Volatile_Full_Access (Entity (N))
13407 or else
13408 Is_Volatile_Full_Access (Etype (Entity (N)))));
13409 end Is_Atomic_Or_VFA_Object;
13410
13411 -------------------------
13412 -- Is_Attribute_Result --
13413 -------------------------
13414
13415 function Is_Attribute_Result (N : Node_Id) return Boolean is
13416 begin
13417 return Nkind (N) = N_Attribute_Reference
13418 and then Attribute_Name (N) = Name_Result;
13419 end Is_Attribute_Result;
13420
13421 -------------------------
13422 -- Is_Attribute_Update --
13423 -------------------------
13424
13425 function Is_Attribute_Update (N : Node_Id) return Boolean is
13426 begin
13427 return Nkind (N) = N_Attribute_Reference
13428 and then Attribute_Name (N) = Name_Update;
13429 end Is_Attribute_Update;
13430
13431 ------------------------------------
13432 -- Is_Body_Or_Package_Declaration --
13433 ------------------------------------
13434
13435 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
13436 begin
13437 return Is_Body (N) or else Nkind (N) = N_Package_Declaration;
13438 end Is_Body_Or_Package_Declaration;
13439
13440 -----------------------
13441 -- Is_Bounded_String --
13442 -----------------------
13443
13444 function Is_Bounded_String (T : Entity_Id) return Boolean is
13445 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
13446
13447 begin
13448 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
13449 -- Super_String, or one of the [Wide_]Wide_ versions. This will
13450 -- be True for all the Bounded_String types in instances of the
13451 -- Generic_Bounded_Length generics, and for types derived from those.
13452
13453 return Present (Under)
13454 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
13455 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
13456 Is_RTE (Root_Type (Under), RO_WW_Super_String));
13457 end Is_Bounded_String;
13458
13459 ---------------------
13460 -- Is_CCT_Instance --
13461 ---------------------
13462
13463 function Is_CCT_Instance
13464 (Ref_Id : Entity_Id;
13465 Context_Id : Entity_Id) return Boolean
13466 is
13467 begin
13468 pragma Assert (Ekind_In (Ref_Id, E_Protected_Type, E_Task_Type));
13469
13470 if Is_Single_Task_Object (Context_Id) then
13471 return Scope_Within_Or_Same (Etype (Context_Id), Ref_Id);
13472
13473 else
13474 pragma Assert (Ekind_In (Context_Id, E_Entry,
13475 E_Entry_Family,
13476 E_Function,
13477 E_Package,
13478 E_Procedure,
13479 E_Protected_Type,
13480 E_Task_Type)
13481 or else
13482 Is_Record_Type (Context_Id));
13483 return Scope_Within_Or_Same (Context_Id, Ref_Id);
13484 end if;
13485 end Is_CCT_Instance;
13486
13487 -------------------------
13488 -- Is_Child_Or_Sibling --
13489 -------------------------
13490
13491 function Is_Child_Or_Sibling
13492 (Pack_1 : Entity_Id;
13493 Pack_2 : Entity_Id) return Boolean
13494 is
13495 function Distance_From_Standard (Pack : Entity_Id) return Nat;
13496 -- Given an arbitrary package, return the number of "climbs" necessary
13497 -- to reach scope Standard_Standard.
13498
13499 procedure Equalize_Depths
13500 (Pack : in out Entity_Id;
13501 Depth : in out Nat;
13502 Depth_To_Reach : Nat);
13503 -- Given an arbitrary package, its depth and a target depth to reach,
13504 -- climb the scope chain until the said depth is reached. The pointer
13505 -- to the package and its depth a modified during the climb.
13506
13507 ----------------------------
13508 -- Distance_From_Standard --
13509 ----------------------------
13510
13511 function Distance_From_Standard (Pack : Entity_Id) return Nat is
13512 Dist : Nat;
13513 Scop : Entity_Id;
13514
13515 begin
13516 Dist := 0;
13517 Scop := Pack;
13518 while Present (Scop) and then Scop /= Standard_Standard loop
13519 Dist := Dist + 1;
13520 Scop := Scope (Scop);
13521 end loop;
13522
13523 return Dist;
13524 end Distance_From_Standard;
13525
13526 ---------------------
13527 -- Equalize_Depths --
13528 ---------------------
13529
13530 procedure Equalize_Depths
13531 (Pack : in out Entity_Id;
13532 Depth : in out Nat;
13533 Depth_To_Reach : Nat)
13534 is
13535 begin
13536 -- The package must be at a greater or equal depth
13537
13538 if Depth < Depth_To_Reach then
13539 raise Program_Error;
13540 end if;
13541
13542 -- Climb the scope chain until the desired depth is reached
13543
13544 while Present (Pack) and then Depth /= Depth_To_Reach loop
13545 Pack := Scope (Pack);
13546 Depth := Depth - 1;
13547 end loop;
13548 end Equalize_Depths;
13549
13550 -- Local variables
13551
13552 P_1 : Entity_Id := Pack_1;
13553 P_1_Child : Boolean := False;
13554 P_1_Depth : Nat := Distance_From_Standard (P_1);
13555 P_2 : Entity_Id := Pack_2;
13556 P_2_Child : Boolean := False;
13557 P_2_Depth : Nat := Distance_From_Standard (P_2);
13558
13559 -- Start of processing for Is_Child_Or_Sibling
13560
13561 begin
13562 pragma Assert
13563 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
13564
13565 -- Both packages denote the same entity, therefore they cannot be
13566 -- children or siblings.
13567
13568 if P_1 = P_2 then
13569 return False;
13570
13571 -- One of the packages is at a deeper level than the other. Note that
13572 -- both may still come from different hierarchies.
13573
13574 -- (root) P_2
13575 -- / \ :
13576 -- X P_2 or X
13577 -- : :
13578 -- P_1 P_1
13579
13580 elsif P_1_Depth > P_2_Depth then
13581 Equalize_Depths
13582 (Pack => P_1,
13583 Depth => P_1_Depth,
13584 Depth_To_Reach => P_2_Depth);
13585 P_1_Child := True;
13586
13587 -- (root) P_1
13588 -- / \ :
13589 -- P_1 X or X
13590 -- : :
13591 -- P_2 P_2
13592
13593 elsif P_2_Depth > P_1_Depth then
13594 Equalize_Depths
13595 (Pack => P_2,
13596 Depth => P_2_Depth,
13597 Depth_To_Reach => P_1_Depth);
13598 P_2_Child := True;
13599 end if;
13600
13601 -- At this stage the package pointers have been elevated to the same
13602 -- depth. If the related entities are the same, then one package is a
13603 -- potential child of the other:
13604
13605 -- P_1
13606 -- :
13607 -- X became P_1 P_2 or vice versa
13608 -- :
13609 -- P_2
13610
13611 if P_1 = P_2 then
13612 if P_1_Child then
13613 return Is_Child_Unit (Pack_1);
13614
13615 else pragma Assert (P_2_Child);
13616 return Is_Child_Unit (Pack_2);
13617 end if;
13618
13619 -- The packages may come from the same package chain or from entirely
13620 -- different hierarcies. To determine this, climb the scope stack until
13621 -- a common root is found.
13622
13623 -- (root) (root 1) (root 2)
13624 -- / \ | |
13625 -- P_1 P_2 P_1 P_2
13626
13627 else
13628 while Present (P_1) and then Present (P_2) loop
13629
13630 -- The two packages may be siblings
13631
13632 if P_1 = P_2 then
13633 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
13634 end if;
13635
13636 P_1 := Scope (P_1);
13637 P_2 := Scope (P_2);
13638 end loop;
13639 end if;
13640
13641 return False;
13642 end Is_Child_Or_Sibling;
13643
13644 -----------------------------
13645 -- Is_Concurrent_Interface --
13646 -----------------------------
13647
13648 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
13649 begin
13650 return Is_Interface (T)
13651 and then
13652 (Is_Protected_Interface (T)
13653 or else Is_Synchronized_Interface (T)
13654 or else Is_Task_Interface (T));
13655 end Is_Concurrent_Interface;
13656
13657 -----------------------
13658 -- Is_Constant_Bound --
13659 -----------------------
13660
13661 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
13662 begin
13663 if Compile_Time_Known_Value (Exp) then
13664 return True;
13665
13666 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
13667 return Is_Constant_Object (Entity (Exp))
13668 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
13669
13670 elsif Nkind (Exp) in N_Binary_Op then
13671 return Is_Constant_Bound (Left_Opnd (Exp))
13672 and then Is_Constant_Bound (Right_Opnd (Exp))
13673 and then Scope (Entity (Exp)) = Standard_Standard;
13674
13675 else
13676 return False;
13677 end if;
13678 end Is_Constant_Bound;
13679
13680 ---------------------------
13681 -- Is_Container_Element --
13682 ---------------------------
13683
13684 function Is_Container_Element (Exp : Node_Id) return Boolean is
13685 Loc : constant Source_Ptr := Sloc (Exp);
13686 Pref : constant Node_Id := Prefix (Exp);
13687
13688 Call : Node_Id;
13689 -- Call to an indexing aspect
13690
13691 Cont_Typ : Entity_Id;
13692 -- The type of the container being accessed
13693
13694 Elem_Typ : Entity_Id;
13695 -- Its element type
13696
13697 Indexing : Entity_Id;
13698 Is_Const : Boolean;
13699 -- Indicates that constant indexing is used, and the element is thus
13700 -- a constant.
13701
13702 Ref_Typ : Entity_Id;
13703 -- The reference type returned by the indexing operation
13704
13705 begin
13706 -- If C is a container, in a context that imposes the element type of
13707 -- that container, the indexing notation C (X) is rewritten as:
13708
13709 -- Indexing (C, X).Discr.all
13710
13711 -- where Indexing is one of the indexing aspects of the container.
13712 -- If the context does not require a reference, the construct can be
13713 -- rewritten as
13714
13715 -- Element (C, X)
13716
13717 -- First, verify that the construct has the proper form
13718
13719 if not Expander_Active then
13720 return False;
13721
13722 elsif Nkind (Pref) /= N_Selected_Component then
13723 return False;
13724
13725 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
13726 return False;
13727
13728 else
13729 Call := Prefix (Pref);
13730 Ref_Typ := Etype (Call);
13731 end if;
13732
13733 if not Has_Implicit_Dereference (Ref_Typ)
13734 or else No (First (Parameter_Associations (Call)))
13735 or else not Is_Entity_Name (Name (Call))
13736 then
13737 return False;
13738 end if;
13739
13740 -- Retrieve type of container object, and its iterator aspects
13741
13742 Cont_Typ := Etype (First (Parameter_Associations (Call)));
13743 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
13744 Is_Const := False;
13745
13746 if No (Indexing) then
13747
13748 -- Container should have at least one indexing operation
13749
13750 return False;
13751
13752 elsif Entity (Name (Call)) /= Entity (Indexing) then
13753
13754 -- This may be a variable indexing operation
13755
13756 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
13757
13758 if No (Indexing)
13759 or else Entity (Name (Call)) /= Entity (Indexing)
13760 then
13761 return False;
13762 end if;
13763
13764 else
13765 Is_Const := True;
13766 end if;
13767
13768 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
13769
13770 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
13771 return False;
13772 end if;
13773
13774 -- Check that the expression is not the target of an assignment, in
13775 -- which case the rewriting is not possible.
13776
13777 if not Is_Const then
13778 declare
13779 Par : Node_Id;
13780
13781 begin
13782 Par := Exp;
13783 while Present (Par)
13784 loop
13785 if Nkind (Parent (Par)) = N_Assignment_Statement
13786 and then Par = Name (Parent (Par))
13787 then
13788 return False;
13789
13790 -- A renaming produces a reference, and the transformation
13791 -- does not apply.
13792
13793 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
13794 return False;
13795
13796 elsif Nkind_In
13797 (Nkind (Parent (Par)), N_Function_Call,
13798 N_Procedure_Call_Statement,
13799 N_Entry_Call_Statement)
13800 then
13801 -- Check that the element is not part of an actual for an
13802 -- in-out parameter.
13803
13804 declare
13805 F : Entity_Id;
13806 A : Node_Id;
13807
13808 begin
13809 F := First_Formal (Entity (Name (Parent (Par))));
13810 A := First (Parameter_Associations (Parent (Par)));
13811 while Present (F) loop
13812 if A = Par and then Ekind (F) /= E_In_Parameter then
13813 return False;
13814 end if;
13815
13816 Next_Formal (F);
13817 Next (A);
13818 end loop;
13819 end;
13820
13821 -- E_In_Parameter in a call: element is not modified.
13822
13823 exit;
13824 end if;
13825
13826 Par := Parent (Par);
13827 end loop;
13828 end;
13829 end if;
13830
13831 -- The expression has the proper form and the context requires the
13832 -- element type. Retrieve the Element function of the container and
13833 -- rewrite the construct as a call to it.
13834
13835 declare
13836 Op : Elmt_Id;
13837
13838 begin
13839 Op := First_Elmt (Primitive_Operations (Cont_Typ));
13840 while Present (Op) loop
13841 exit when Chars (Node (Op)) = Name_Element;
13842 Next_Elmt (Op);
13843 end loop;
13844
13845 if No (Op) then
13846 return False;
13847
13848 else
13849 Rewrite (Exp,
13850 Make_Function_Call (Loc,
13851 Name => New_Occurrence_Of (Node (Op), Loc),
13852 Parameter_Associations => Parameter_Associations (Call)));
13853 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
13854 return True;
13855 end if;
13856 end;
13857 end Is_Container_Element;
13858
13859 ----------------------------
13860 -- Is_Contract_Annotation --
13861 ----------------------------
13862
13863 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
13864 begin
13865 return Is_Package_Contract_Annotation (Item)
13866 or else
13867 Is_Subprogram_Contract_Annotation (Item);
13868 end Is_Contract_Annotation;
13869
13870 --------------------------------------
13871 -- Is_Controlling_Limited_Procedure --
13872 --------------------------------------
13873
13874 function Is_Controlling_Limited_Procedure
13875 (Proc_Nam : Entity_Id) return Boolean
13876 is
13877 Param : Node_Id;
13878 Param_Typ : Entity_Id := Empty;
13879
13880 begin
13881 if Ekind (Proc_Nam) = E_Procedure
13882 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
13883 then
13884 Param :=
13885 Parameter_Type
13886 (First (Parameter_Specifications (Parent (Proc_Nam))));
13887
13888 -- The formal may be an anonymous access type
13889
13890 if Nkind (Param) = N_Access_Definition then
13891 Param_Typ := Entity (Subtype_Mark (Param));
13892 else
13893 Param_Typ := Etype (Param);
13894 end if;
13895
13896 -- In the case where an Itype was created for a dispatchin call, the
13897 -- procedure call has been rewritten. The actual may be an access to
13898 -- interface type in which case it is the designated type that is the
13899 -- controlling type.
13900
13901 elsif Present (Associated_Node_For_Itype (Proc_Nam))
13902 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
13903 and then
13904 Present (Parameter_Associations
13905 (Associated_Node_For_Itype (Proc_Nam)))
13906 then
13907 Param_Typ :=
13908 Etype (First (Parameter_Associations
13909 (Associated_Node_For_Itype (Proc_Nam))));
13910
13911 if Ekind (Param_Typ) = E_Anonymous_Access_Type then
13912 Param_Typ := Directly_Designated_Type (Param_Typ);
13913 end if;
13914 end if;
13915
13916 if Present (Param_Typ) then
13917 return
13918 Is_Interface (Param_Typ)
13919 and then Is_Limited_Record (Param_Typ);
13920 end if;
13921
13922 return False;
13923 end Is_Controlling_Limited_Procedure;
13924
13925 -----------------------------
13926 -- Is_CPP_Constructor_Call --
13927 -----------------------------
13928
13929 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
13930 begin
13931 return Nkind (N) = N_Function_Call
13932 and then Is_CPP_Class (Etype (Etype (N)))
13933 and then Is_Constructor (Entity (Name (N)))
13934 and then Is_Imported (Entity (Name (N)));
13935 end Is_CPP_Constructor_Call;
13936
13937 -------------------------
13938 -- Is_Current_Instance --
13939 -------------------------
13940
13941 function Is_Current_Instance (N : Node_Id) return Boolean is
13942 Typ : constant Entity_Id := Entity (N);
13943 P : Node_Id;
13944
13945 begin
13946 -- Simplest case: entity is a concurrent type and we are currently
13947 -- inside the body. This will eventually be expanded into a call to
13948 -- Self (for tasks) or _object (for protected objects).
13949
13950 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
13951 return True;
13952
13953 else
13954 -- Check whether the context is a (sub)type declaration for the
13955 -- type entity.
13956
13957 P := Parent (N);
13958 while Present (P) loop
13959 if Nkind_In (P, N_Full_Type_Declaration,
13960 N_Private_Type_Declaration,
13961 N_Subtype_Declaration)
13962 and then Comes_From_Source (P)
13963 and then Defining_Entity (P) = Typ
13964 then
13965 return True;
13966
13967 -- A subtype name may appear in an aspect specification for a
13968 -- Predicate_Failure aspect, for which we do not construct a
13969 -- wrapper procedure. The subtype will be replaced by the
13970 -- expression being tested when the corresponding predicate
13971 -- check is expanded.
13972
13973 elsif Nkind (P) = N_Aspect_Specification
13974 and then Nkind (Parent (P)) = N_Subtype_Declaration
13975 then
13976 return True;
13977
13978 elsif Nkind (P) = N_Pragma
13979 and then Get_Pragma_Id (P) = Pragma_Predicate_Failure
13980 then
13981 return True;
13982 end if;
13983
13984 P := Parent (P);
13985 end loop;
13986 end if;
13987
13988 -- In any other context this is not a current occurrence
13989
13990 return False;
13991 end Is_Current_Instance;
13992
13993 --------------------
13994 -- Is_Declaration --
13995 --------------------
13996
13997 function Is_Declaration
13998 (N : Node_Id;
13999 Body_OK : Boolean := True;
14000 Concurrent_OK : Boolean := True;
14001 Formal_OK : Boolean := True;
14002 Generic_OK : Boolean := True;
14003 Instantiation_OK : Boolean := True;
14004 Renaming_OK : Boolean := True;
14005 Stub_OK : Boolean := True;
14006 Subprogram_OK : Boolean := True;
14007 Type_OK : Boolean := True) return Boolean
14008 is
14009 begin
14010 case Nkind (N) is
14011
14012 -- Body declarations
14013
14014 when N_Proper_Body =>
14015 return Body_OK;
14016
14017 -- Concurrent type declarations
14018
14019 when N_Protected_Type_Declaration
14020 | N_Single_Protected_Declaration
14021 | N_Single_Task_Declaration
14022 | N_Task_Type_Declaration
14023 =>
14024 return Concurrent_OK or Type_OK;
14025
14026 -- Formal declarations
14027
14028 when N_Formal_Abstract_Subprogram_Declaration
14029 | N_Formal_Concrete_Subprogram_Declaration
14030 | N_Formal_Object_Declaration
14031 | N_Formal_Package_Declaration
14032 | N_Formal_Type_Declaration
14033 =>
14034 return Formal_OK;
14035
14036 -- Generic declarations
14037
14038 when N_Generic_Package_Declaration
14039 | N_Generic_Subprogram_Declaration
14040 =>
14041 return Generic_OK;
14042
14043 -- Generic instantiations
14044
14045 when N_Function_Instantiation
14046 | N_Package_Instantiation
14047 | N_Procedure_Instantiation
14048 =>
14049 return Instantiation_OK;
14050
14051 -- Generic renaming declarations
14052
14053 when N_Generic_Renaming_Declaration =>
14054 return Generic_OK or Renaming_OK;
14055
14056 -- Renaming declarations
14057
14058 when N_Exception_Renaming_Declaration
14059 | N_Object_Renaming_Declaration
14060 | N_Package_Renaming_Declaration
14061 | N_Subprogram_Renaming_Declaration
14062 =>
14063 return Renaming_OK;
14064
14065 -- Stub declarations
14066
14067 when N_Body_Stub =>
14068 return Stub_OK;
14069
14070 -- Subprogram declarations
14071
14072 when N_Abstract_Subprogram_Declaration
14073 | N_Entry_Declaration
14074 | N_Expression_Function
14075 | N_Subprogram_Declaration
14076 =>
14077 return Subprogram_OK;
14078
14079 -- Type declarations
14080
14081 when N_Full_Type_Declaration
14082 | N_Incomplete_Type_Declaration
14083 | N_Private_Extension_Declaration
14084 | N_Private_Type_Declaration
14085 | N_Subtype_Declaration
14086 =>
14087 return Type_OK;
14088
14089 -- Miscellaneous
14090
14091 when N_Component_Declaration
14092 | N_Exception_Declaration
14093 | N_Implicit_Label_Declaration
14094 | N_Number_Declaration
14095 | N_Object_Declaration
14096 | N_Package_Declaration
14097 =>
14098 return True;
14099
14100 when others =>
14101 return False;
14102 end case;
14103 end Is_Declaration;
14104
14105 --------------------------------
14106 -- Is_Declared_Within_Variant --
14107 --------------------------------
14108
14109 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
14110 Comp_Decl : constant Node_Id := Parent (Comp);
14111 Comp_List : constant Node_Id := Parent (Comp_Decl);
14112 begin
14113 return Nkind (Parent (Comp_List)) = N_Variant;
14114 end Is_Declared_Within_Variant;
14115
14116 ----------------------------------------------
14117 -- Is_Dependent_Component_Of_Mutable_Object --
14118 ----------------------------------------------
14119
14120 function Is_Dependent_Component_Of_Mutable_Object
14121 (Object : Node_Id) return Boolean
14122 is
14123 P : Node_Id;
14124 Prefix_Type : Entity_Id;
14125 P_Aliased : Boolean := False;
14126 Comp : Entity_Id;
14127
14128 Deref : Node_Id := Object;
14129 -- Dereference node, in something like X.all.Y(2)
14130
14131 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
14132
14133 begin
14134 -- Find the dereference node if any
14135
14136 while Nkind_In (Deref, N_Indexed_Component,
14137 N_Selected_Component,
14138 N_Slice)
14139 loop
14140 Deref := Prefix (Deref);
14141 end loop;
14142
14143 -- If the prefix is a qualified expression of a variable, then function
14144 -- Is_Variable will return False for that because a qualified expression
14145 -- denotes a constant view, so we need to get the name being qualified
14146 -- so we can test below whether that's a variable (or a dereference).
14147
14148 if Nkind (Deref) = N_Qualified_Expression then
14149 Deref := Expression (Deref);
14150 end if;
14151
14152 -- Ada 2005: If we have a component or slice of a dereference, something
14153 -- like X.all.Y (2) and the type of X is access-to-constant, Is_Variable
14154 -- will return False, because it is indeed a constant view. But it might
14155 -- be a view of a variable object, so we want the following condition to
14156 -- be True in that case.
14157
14158 if Is_Variable (Object)
14159 or else Is_Variable (Deref)
14160 or else (Ada_Version >= Ada_2005
14161 and then (Nkind (Deref) = N_Explicit_Dereference
14162 or else Is_Access_Type (Etype (Deref))))
14163 then
14164 if Nkind (Object) = N_Selected_Component then
14165
14166 -- If the selector is not a component, then we definitely return
14167 -- False (it could be a function selector in a prefix form call
14168 -- occurring in an iterator specification).
14169
14170 if not Ekind_In (Entity (Selector_Name (Object)), E_Component,
14171 E_Discriminant)
14172 then
14173 return False;
14174 end if;
14175
14176 -- Get the original node of the prefix in case it has been
14177 -- rewritten, which can occur, for example, in qualified
14178 -- expression cases. Also, a discriminant check on a selected
14179 -- component may be expanded into a dereference when removing
14180 -- side effects, and the subtype of the original node may be
14181 -- unconstrained.
14182
14183 P := Original_Node (Prefix (Object));
14184 Prefix_Type := Etype (P);
14185
14186 -- If the prefix is a qualified expression, we want to look at its
14187 -- operand.
14188
14189 if Nkind (P) = N_Qualified_Expression then
14190 P := Expression (P);
14191 Prefix_Type := Etype (P);
14192 end if;
14193
14194 if Is_Entity_Name (P) then
14195 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
14196 Prefix_Type := Base_Type (Prefix_Type);
14197 end if;
14198
14199 if Is_Aliased (Entity (P)) then
14200 P_Aliased := True;
14201 end if;
14202
14203 -- For explicit dereferences we get the access prefix so we can
14204 -- treat this similarly to implicit dereferences and examine the
14205 -- kind of the access type and its designated subtype further
14206 -- below.
14207
14208 elsif Nkind (P) = N_Explicit_Dereference then
14209 P := Prefix (P);
14210 Prefix_Type := Etype (P);
14211
14212 else
14213 -- Check for prefix being an aliased component???
14214
14215 null;
14216 end if;
14217
14218 -- A heap object is constrained by its initial value
14219
14220 -- Ada 2005 (AI-363): Always assume the object could be mutable in
14221 -- the dereferenced case, since the access value might denote an
14222 -- unconstrained aliased object, whereas in Ada 95 the designated
14223 -- object is guaranteed to be constrained. A worst-case assumption
14224 -- has to apply in Ada 2005 because we can't tell at compile
14225 -- time whether the object is "constrained by its initial value",
14226 -- despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
14227 -- rules (these rules are acknowledged to need fixing). We don't
14228 -- impose this more stringent checking for earlier Ada versions or
14229 -- when Relaxed_RM_Semantics applies (the latter for CodePeer's
14230 -- benefit, though it's unclear on why using -gnat95 would not be
14231 -- sufficient???).
14232
14233 if Ada_Version < Ada_2005 or else Relaxed_RM_Semantics then
14234 if Is_Access_Type (Prefix_Type)
14235 or else Nkind (P) = N_Explicit_Dereference
14236 then
14237 return False;
14238 end if;
14239
14240 else pragma Assert (Ada_Version >= Ada_2005);
14241 if Is_Access_Type (Prefix_Type) then
14242 -- We need to make sure we have the base subtype, in case
14243 -- this is actually an access subtype (whose Ekind will be
14244 -- E_Access_Subtype).
14245
14246 Prefix_Type := Etype (Prefix_Type);
14247
14248 -- If the access type is pool-specific, and there is no
14249 -- constrained partial view of the designated type, then the
14250 -- designated object is known to be constrained. If it's a
14251 -- formal access type and the renaming is in the generic
14252 -- spec, we also treat it as pool-specific (known to be
14253 -- constrained), but assume the worst if in the generic body
14254 -- (see RM 3.3(23.3/3)).
14255
14256 if Ekind (Prefix_Type) = E_Access_Type
14257 and then (not Is_Generic_Type (Prefix_Type)
14258 or else not In_Generic_Body (Current_Scope))
14259 and then not Object_Type_Has_Constrained_Partial_View
14260 (Typ => Designated_Type (Prefix_Type),
14261 Scop => Current_Scope)
14262 then
14263 return False;
14264
14265 -- Otherwise (general access type, or there is a constrained
14266 -- partial view of the designated type), we need to check
14267 -- based on the designated type.
14268
14269 else
14270 Prefix_Type := Designated_Type (Prefix_Type);
14271 end if;
14272 end if;
14273 end if;
14274
14275 Comp :=
14276 Original_Record_Component (Entity (Selector_Name (Object)));
14277
14278 -- As per AI-0017, the renaming is illegal in a generic body, even
14279 -- if the subtype is indefinite (only applies to prefixes of an
14280 -- untagged formal type, see RM 3.3 (23.11/3)).
14281
14282 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
14283
14284 if not Is_Constrained (Prefix_Type)
14285 and then (Is_Definite_Subtype (Prefix_Type)
14286 or else
14287 (not Is_Tagged_Type (Prefix_Type)
14288 and then Is_Generic_Type (Prefix_Type)
14289 and then In_Generic_Body (Current_Scope)))
14290
14291 and then (Is_Declared_Within_Variant (Comp)
14292 or else Has_Discriminant_Dependent_Constraint (Comp))
14293 and then (not P_Aliased or else Ada_Version >= Ada_2005)
14294 then
14295 return True;
14296
14297 -- If the prefix is of an access type at this point, then we want
14298 -- to return False, rather than calling this function recursively
14299 -- on the access object (which itself might be a discriminant-
14300 -- dependent component of some other object, but that isn't
14301 -- relevant to checking the object passed to us). This avoids
14302 -- issuing wrong errors when compiling with -gnatc, where there
14303 -- can be implicit dereferences that have not been expanded.
14304
14305 elsif Is_Access_Type (Etype (Prefix (Object))) then
14306 return False;
14307
14308 else
14309 return
14310 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
14311 end if;
14312
14313 elsif Nkind (Object) = N_Indexed_Component
14314 or else Nkind (Object) = N_Slice
14315 then
14316 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
14317
14318 -- A type conversion that Is_Variable is a view conversion:
14319 -- go back to the denoted object.
14320
14321 elsif Nkind (Object) = N_Type_Conversion then
14322 return
14323 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
14324 end if;
14325 end if;
14326
14327 return False;
14328 end Is_Dependent_Component_Of_Mutable_Object;
14329
14330 ---------------------
14331 -- Is_Dereferenced --
14332 ---------------------
14333
14334 function Is_Dereferenced (N : Node_Id) return Boolean is
14335 P : constant Node_Id := Parent (N);
14336 begin
14337 return Nkind_In (P, N_Selected_Component,
14338 N_Explicit_Dereference,
14339 N_Indexed_Component,
14340 N_Slice)
14341 and then Prefix (P) = N;
14342 end Is_Dereferenced;
14343
14344 ----------------------
14345 -- Is_Descendant_Of --
14346 ----------------------
14347
14348 function Is_Descendant_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
14349 T : Entity_Id;
14350 Etyp : Entity_Id;
14351
14352 begin
14353 pragma Assert (Nkind (T1) in N_Entity);
14354 pragma Assert (Nkind (T2) in N_Entity);
14355
14356 T := Base_Type (T1);
14357
14358 -- Immediate return if the types match
14359
14360 if T = T2 then
14361 return True;
14362
14363 -- Comment needed here ???
14364
14365 elsif Ekind (T) = E_Class_Wide_Type then
14366 return Etype (T) = T2;
14367
14368 -- All other cases
14369
14370 else
14371 loop
14372 Etyp := Etype (T);
14373
14374 -- Done if we found the type we are looking for
14375
14376 if Etyp = T2 then
14377 return True;
14378
14379 -- Done if no more derivations to check
14380
14381 elsif T = T1
14382 or else T = Etyp
14383 then
14384 return False;
14385
14386 -- Following test catches error cases resulting from prev errors
14387
14388 elsif No (Etyp) then
14389 return False;
14390
14391 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
14392 return False;
14393
14394 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
14395 return False;
14396 end if;
14397
14398 T := Base_Type (Etyp);
14399 end loop;
14400 end if;
14401 end Is_Descendant_Of;
14402
14403 ----------------------------------------
14404 -- Is_Descendant_Of_Suspension_Object --
14405 ----------------------------------------
14406
14407 function Is_Descendant_Of_Suspension_Object
14408 (Typ : Entity_Id) return Boolean
14409 is
14410 Cur_Typ : Entity_Id;
14411 Par_Typ : Entity_Id;
14412
14413 begin
14414 -- Climb the type derivation chain checking each parent type against
14415 -- Suspension_Object.
14416
14417 Cur_Typ := Base_Type (Typ);
14418 while Present (Cur_Typ) loop
14419 Par_Typ := Etype (Cur_Typ);
14420
14421 -- The current type is a match
14422
14423 if Is_Suspension_Object (Cur_Typ) then
14424 return True;
14425
14426 -- Stop the traversal once the root of the derivation chain has been
14427 -- reached. In that case the current type is its own base type.
14428
14429 elsif Cur_Typ = Par_Typ then
14430 exit;
14431 end if;
14432
14433 Cur_Typ := Base_Type (Par_Typ);
14434 end loop;
14435
14436 return False;
14437 end Is_Descendant_Of_Suspension_Object;
14438
14439 ---------------------------------------------
14440 -- Is_Double_Precision_Floating_Point_Type --
14441 ---------------------------------------------
14442
14443 function Is_Double_Precision_Floating_Point_Type
14444 (E : Entity_Id) return Boolean is
14445 begin
14446 return Is_Floating_Point_Type (E)
14447 and then Machine_Radix_Value (E) = Uint_2
14448 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
14449 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
14450 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
14451 end Is_Double_Precision_Floating_Point_Type;
14452
14453 -----------------------------
14454 -- Is_Effectively_Volatile --
14455 -----------------------------
14456
14457 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
14458 begin
14459 if Is_Type (Id) then
14460
14461 -- An arbitrary type is effectively volatile when it is subject to
14462 -- pragma Atomic or Volatile.
14463
14464 if Is_Volatile (Id) then
14465 return True;
14466
14467 -- An array type is effectively volatile when it is subject to pragma
14468 -- Atomic_Components or Volatile_Components or its component type is
14469 -- effectively volatile.
14470
14471 elsif Is_Array_Type (Id) then
14472 declare
14473 Anc : Entity_Id := Base_Type (Id);
14474 begin
14475 if Is_Private_Type (Anc) then
14476 Anc := Full_View (Anc);
14477 end if;
14478
14479 -- Test for presence of ancestor, as the full view of a private
14480 -- type may be missing in case of error.
14481
14482 return
14483 Has_Volatile_Components (Id)
14484 or else
14485 (Present (Anc)
14486 and then Is_Effectively_Volatile (Component_Type (Anc)));
14487 end;
14488
14489 -- A protected type is always volatile
14490
14491 elsif Is_Protected_Type (Id) then
14492 return True;
14493
14494 -- A descendant of Ada.Synchronous_Task_Control.Suspension_Object is
14495 -- automatically volatile.
14496
14497 elsif Is_Descendant_Of_Suspension_Object (Id) then
14498 return True;
14499
14500 -- Otherwise the type is not effectively volatile
14501
14502 else
14503 return False;
14504 end if;
14505
14506 -- Otherwise Id denotes an object
14507
14508 else
14509 return
14510 Is_Volatile (Id)
14511 or else Has_Volatile_Components (Id)
14512 or else Is_Effectively_Volatile (Etype (Id));
14513 end if;
14514 end Is_Effectively_Volatile;
14515
14516 ------------------------------------
14517 -- Is_Effectively_Volatile_Object --
14518 ------------------------------------
14519
14520 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
14521 begin
14522 if Is_Entity_Name (N) then
14523 return Is_Effectively_Volatile (Entity (N));
14524
14525 elsif Nkind (N) = N_Indexed_Component then
14526 return Is_Effectively_Volatile_Object (Prefix (N));
14527
14528 elsif Nkind (N) = N_Selected_Component then
14529 return
14530 Is_Effectively_Volatile_Object (Prefix (N))
14531 or else
14532 Is_Effectively_Volatile_Object (Selector_Name (N));
14533
14534 else
14535 return False;
14536 end if;
14537 end Is_Effectively_Volatile_Object;
14538
14539 -------------------
14540 -- Is_Entry_Body --
14541 -------------------
14542
14543 function Is_Entry_Body (Id : Entity_Id) return Boolean is
14544 begin
14545 return
14546 Ekind_In (Id, E_Entry, E_Entry_Family)
14547 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Body;
14548 end Is_Entry_Body;
14549
14550 --------------------------
14551 -- Is_Entry_Declaration --
14552 --------------------------
14553
14554 function Is_Entry_Declaration (Id : Entity_Id) return Boolean is
14555 begin
14556 return
14557 Ekind_In (Id, E_Entry, E_Entry_Family)
14558 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Declaration;
14559 end Is_Entry_Declaration;
14560
14561 ------------------------------------
14562 -- Is_Expanded_Priority_Attribute --
14563 ------------------------------------
14564
14565 function Is_Expanded_Priority_Attribute (E : Entity_Id) return Boolean is
14566 begin
14567 return
14568 Nkind (E) = N_Function_Call
14569 and then not Configurable_Run_Time_Mode
14570 and then (Entity (Name (E)) = RTE (RE_Get_Ceiling)
14571 or else Entity (Name (E)) = RTE (RO_PE_Get_Ceiling));
14572 end Is_Expanded_Priority_Attribute;
14573
14574 ----------------------------
14575 -- Is_Expression_Function --
14576 ----------------------------
14577
14578 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
14579 begin
14580 if Ekind_In (Subp, E_Function, E_Subprogram_Body) then
14581 return
14582 Nkind (Original_Node (Unit_Declaration_Node (Subp))) =
14583 N_Expression_Function;
14584 else
14585 return False;
14586 end if;
14587 end Is_Expression_Function;
14588
14589 ------------------------------------------
14590 -- Is_Expression_Function_Or_Completion --
14591 ------------------------------------------
14592
14593 function Is_Expression_Function_Or_Completion
14594 (Subp : Entity_Id) return Boolean
14595 is
14596 Subp_Decl : Node_Id;
14597
14598 begin
14599 if Ekind (Subp) = E_Function then
14600 Subp_Decl := Unit_Declaration_Node (Subp);
14601
14602 -- The function declaration is either an expression function or is
14603 -- completed by an expression function body.
14604
14605 return
14606 Is_Expression_Function (Subp)
14607 or else (Nkind (Subp_Decl) = N_Subprogram_Declaration
14608 and then Present (Corresponding_Body (Subp_Decl))
14609 and then Is_Expression_Function
14610 (Corresponding_Body (Subp_Decl)));
14611
14612 elsif Ekind (Subp) = E_Subprogram_Body then
14613 return Is_Expression_Function (Subp);
14614
14615 else
14616 return False;
14617 end if;
14618 end Is_Expression_Function_Or_Completion;
14619
14620 -----------------------
14621 -- Is_EVF_Expression --
14622 -----------------------
14623
14624 function Is_EVF_Expression (N : Node_Id) return Boolean is
14625 Orig_N : constant Node_Id := Original_Node (N);
14626 Alt : Node_Id;
14627 Expr : Node_Id;
14628 Id : Entity_Id;
14629
14630 begin
14631 -- Detect a reference to a formal parameter of a specific tagged type
14632 -- whose related subprogram is subject to pragma Expresions_Visible with
14633 -- value "False".
14634
14635 if Is_Entity_Name (N) and then Present (Entity (N)) then
14636 Id := Entity (N);
14637
14638 return
14639 Is_Formal (Id)
14640 and then Is_Specific_Tagged_Type (Etype (Id))
14641 and then Extensions_Visible_Status (Id) =
14642 Extensions_Visible_False;
14643
14644 -- A case expression is an EVF expression when it contains at least one
14645 -- EVF dependent_expression. Note that a case expression may have been
14646 -- expanded, hence the use of Original_Node.
14647
14648 elsif Nkind (Orig_N) = N_Case_Expression then
14649 Alt := First (Alternatives (Orig_N));
14650 while Present (Alt) loop
14651 if Is_EVF_Expression (Expression (Alt)) then
14652 return True;
14653 end if;
14654
14655 Next (Alt);
14656 end loop;
14657
14658 -- An if expression is an EVF expression when it contains at least one
14659 -- EVF dependent_expression. Note that an if expression may have been
14660 -- expanded, hence the use of Original_Node.
14661
14662 elsif Nkind (Orig_N) = N_If_Expression then
14663 Expr := Next (First (Expressions (Orig_N)));
14664 while Present (Expr) loop
14665 if Is_EVF_Expression (Expr) then
14666 return True;
14667 end if;
14668
14669 Next (Expr);
14670 end loop;
14671
14672 -- A qualified expression or a type conversion is an EVF expression when
14673 -- its operand is an EVF expression.
14674
14675 elsif Nkind_In (N, N_Qualified_Expression,
14676 N_Unchecked_Type_Conversion,
14677 N_Type_Conversion)
14678 then
14679 return Is_EVF_Expression (Expression (N));
14680
14681 -- Attributes 'Loop_Entry, 'Old, and 'Update are EVF expressions when
14682 -- their prefix denotes an EVF expression.
14683
14684 elsif Nkind (N) = N_Attribute_Reference
14685 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
14686 Name_Old,
14687 Name_Update)
14688 then
14689 return Is_EVF_Expression (Prefix (N));
14690 end if;
14691
14692 return False;
14693 end Is_EVF_Expression;
14694
14695 --------------
14696 -- Is_False --
14697 --------------
14698
14699 function Is_False (U : Uint) return Boolean is
14700 begin
14701 return (U = 0);
14702 end Is_False;
14703
14704 ---------------------------
14705 -- Is_Fixed_Model_Number --
14706 ---------------------------
14707
14708 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
14709 S : constant Ureal := Small_Value (T);
14710 M : Urealp.Save_Mark;
14711 R : Boolean;
14712
14713 begin
14714 M := Urealp.Mark;
14715 R := (U = UR_Trunc (U / S) * S);
14716 Urealp.Release (M);
14717 return R;
14718 end Is_Fixed_Model_Number;
14719
14720 -------------------------------
14721 -- Is_Fully_Initialized_Type --
14722 -------------------------------
14723
14724 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
14725 begin
14726 -- Scalar types
14727
14728 if Is_Scalar_Type (Typ) then
14729
14730 -- A scalar type with an aspect Default_Value is fully initialized
14731
14732 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
14733 -- of a scalar type, but we don't take that into account here, since
14734 -- we don't want these to affect warnings.
14735
14736 return Has_Default_Aspect (Typ);
14737
14738 elsif Is_Access_Type (Typ) then
14739 return True;
14740
14741 elsif Is_Array_Type (Typ) then
14742 if Is_Fully_Initialized_Type (Component_Type (Typ))
14743 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
14744 then
14745 return True;
14746 end if;
14747
14748 -- An interesting case, if we have a constrained type one of whose
14749 -- bounds is known to be null, then there are no elements to be
14750 -- initialized, so all the elements are initialized.
14751
14752 if Is_Constrained (Typ) then
14753 declare
14754 Indx : Node_Id;
14755 Indx_Typ : Entity_Id;
14756 Lbd, Hbd : Node_Id;
14757
14758 begin
14759 Indx := First_Index (Typ);
14760 while Present (Indx) loop
14761 if Etype (Indx) = Any_Type then
14762 return False;
14763
14764 -- If index is a range, use directly
14765
14766 elsif Nkind (Indx) = N_Range then
14767 Lbd := Low_Bound (Indx);
14768 Hbd := High_Bound (Indx);
14769
14770 else
14771 Indx_Typ := Etype (Indx);
14772
14773 if Is_Private_Type (Indx_Typ) then
14774 Indx_Typ := Full_View (Indx_Typ);
14775 end if;
14776
14777 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
14778 return False;
14779 else
14780 Lbd := Type_Low_Bound (Indx_Typ);
14781 Hbd := Type_High_Bound (Indx_Typ);
14782 end if;
14783 end if;
14784
14785 if Compile_Time_Known_Value (Lbd)
14786 and then
14787 Compile_Time_Known_Value (Hbd)
14788 then
14789 if Expr_Value (Hbd) < Expr_Value (Lbd) then
14790 return True;
14791 end if;
14792 end if;
14793
14794 Next_Index (Indx);
14795 end loop;
14796 end;
14797 end if;
14798
14799 -- If no null indexes, then type is not fully initialized
14800
14801 return False;
14802
14803 -- Record types
14804
14805 elsif Is_Record_Type (Typ) then
14806 if Has_Discriminants (Typ)
14807 and then
14808 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
14809 and then Is_Fully_Initialized_Variant (Typ)
14810 then
14811 return True;
14812 end if;
14813
14814 -- We consider bounded string types to be fully initialized, because
14815 -- otherwise we get false alarms when the Data component is not
14816 -- default-initialized.
14817
14818 if Is_Bounded_String (Typ) then
14819 return True;
14820 end if;
14821
14822 -- Controlled records are considered to be fully initialized if
14823 -- there is a user defined Initialize routine. This may not be
14824 -- entirely correct, but as the spec notes, we are guessing here
14825 -- what is best from the point of view of issuing warnings.
14826
14827 if Is_Controlled (Typ) then
14828 declare
14829 Utyp : constant Entity_Id := Underlying_Type (Typ);
14830
14831 begin
14832 if Present (Utyp) then
14833 declare
14834 Init : constant Entity_Id :=
14835 (Find_Optional_Prim_Op
14836 (Underlying_Type (Typ), Name_Initialize));
14837
14838 begin
14839 if Present (Init)
14840 and then Comes_From_Source (Init)
14841 and then not In_Predefined_Unit (Init)
14842 then
14843 return True;
14844
14845 elsif Has_Null_Extension (Typ)
14846 and then
14847 Is_Fully_Initialized_Type
14848 (Etype (Base_Type (Typ)))
14849 then
14850 return True;
14851 end if;
14852 end;
14853 end if;
14854 end;
14855 end if;
14856
14857 -- Otherwise see if all record components are initialized
14858
14859 declare
14860 Ent : Entity_Id;
14861
14862 begin
14863 Ent := First_Entity (Typ);
14864 while Present (Ent) loop
14865 if Ekind (Ent) = E_Component
14866 and then (No (Parent (Ent))
14867 or else No (Expression (Parent (Ent))))
14868 and then not Is_Fully_Initialized_Type (Etype (Ent))
14869
14870 -- Special VM case for tag components, which need to be
14871 -- defined in this case, but are never initialized as VMs
14872 -- are using other dispatching mechanisms. Ignore this
14873 -- uninitialized case. Note that this applies both to the
14874 -- uTag entry and the main vtable pointer (CPP_Class case).
14875
14876 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
14877 then
14878 return False;
14879 end if;
14880
14881 Next_Entity (Ent);
14882 end loop;
14883 end;
14884
14885 -- No uninitialized components, so type is fully initialized.
14886 -- Note that this catches the case of no components as well.
14887
14888 return True;
14889
14890 elsif Is_Concurrent_Type (Typ) then
14891 return True;
14892
14893 elsif Is_Private_Type (Typ) then
14894 declare
14895 U : constant Entity_Id := Underlying_Type (Typ);
14896
14897 begin
14898 if No (U) then
14899 return False;
14900 else
14901 return Is_Fully_Initialized_Type (U);
14902 end if;
14903 end;
14904
14905 else
14906 return False;
14907 end if;
14908 end Is_Fully_Initialized_Type;
14909
14910 ----------------------------------
14911 -- Is_Fully_Initialized_Variant --
14912 ----------------------------------
14913
14914 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
14915 Loc : constant Source_Ptr := Sloc (Typ);
14916 Constraints : constant List_Id := New_List;
14917 Components : constant Elist_Id := New_Elmt_List;
14918 Comp_Elmt : Elmt_Id;
14919 Comp_Id : Node_Id;
14920 Comp_List : Node_Id;
14921 Discr : Entity_Id;
14922 Discr_Val : Node_Id;
14923
14924 Report_Errors : Boolean;
14925 pragma Warnings (Off, Report_Errors);
14926
14927 begin
14928 if Serious_Errors_Detected > 0 then
14929 return False;
14930 end if;
14931
14932 if Is_Record_Type (Typ)
14933 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14934 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
14935 then
14936 Comp_List := Component_List (Type_Definition (Parent (Typ)));
14937
14938 Discr := First_Discriminant (Typ);
14939 while Present (Discr) loop
14940 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
14941 Discr_Val := Expression (Parent (Discr));
14942
14943 if Present (Discr_Val)
14944 and then Is_OK_Static_Expression (Discr_Val)
14945 then
14946 Append_To (Constraints,
14947 Make_Component_Association (Loc,
14948 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
14949 Expression => New_Copy (Discr_Val)));
14950 else
14951 return False;
14952 end if;
14953 else
14954 return False;
14955 end if;
14956
14957 Next_Discriminant (Discr);
14958 end loop;
14959
14960 Gather_Components
14961 (Typ => Typ,
14962 Comp_List => Comp_List,
14963 Governed_By => Constraints,
14964 Into => Components,
14965 Report_Errors => Report_Errors);
14966
14967 -- Check that each component present is fully initialized
14968
14969 Comp_Elmt := First_Elmt (Components);
14970 while Present (Comp_Elmt) loop
14971 Comp_Id := Node (Comp_Elmt);
14972
14973 if Ekind (Comp_Id) = E_Component
14974 and then (No (Parent (Comp_Id))
14975 or else No (Expression (Parent (Comp_Id))))
14976 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
14977 then
14978 return False;
14979 end if;
14980
14981 Next_Elmt (Comp_Elmt);
14982 end loop;
14983
14984 return True;
14985
14986 elsif Is_Private_Type (Typ) then
14987 declare
14988 U : constant Entity_Id := Underlying_Type (Typ);
14989
14990 begin
14991 if No (U) then
14992 return False;
14993 else
14994 return Is_Fully_Initialized_Variant (U);
14995 end if;
14996 end;
14997
14998 else
14999 return False;
15000 end if;
15001 end Is_Fully_Initialized_Variant;
15002
15003 ------------------------------------
15004 -- Is_Generic_Declaration_Or_Body --
15005 ------------------------------------
15006
15007 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
15008 Spec_Decl : Node_Id;
15009
15010 begin
15011 -- Package/subprogram body
15012
15013 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
15014 and then Present (Corresponding_Spec (Decl))
15015 then
15016 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
15017
15018 -- Package/subprogram body stub
15019
15020 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
15021 and then Present (Corresponding_Spec_Of_Stub (Decl))
15022 then
15023 Spec_Decl :=
15024 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
15025
15026 -- All other cases
15027
15028 else
15029 Spec_Decl := Decl;
15030 end if;
15031
15032 -- Rather than inspecting the defining entity of the spec declaration,
15033 -- look at its Nkind. This takes care of the case where the analysis of
15034 -- a generic body modifies the Ekind of its spec to allow for recursive
15035 -- calls.
15036
15037 return
15038 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
15039 N_Generic_Subprogram_Declaration);
15040 end Is_Generic_Declaration_Or_Body;
15041
15042 ----------------------------
15043 -- Is_Inherited_Operation --
15044 ----------------------------
15045
15046 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
15047 pragma Assert (Is_Overloadable (E));
15048 Kind : constant Node_Kind := Nkind (Parent (E));
15049 begin
15050 return Kind = N_Full_Type_Declaration
15051 or else Kind = N_Private_Extension_Declaration
15052 or else Kind = N_Subtype_Declaration
15053 or else (Ekind (E) = E_Enumeration_Literal
15054 and then Is_Derived_Type (Etype (E)));
15055 end Is_Inherited_Operation;
15056
15057 -------------------------------------
15058 -- Is_Inherited_Operation_For_Type --
15059 -------------------------------------
15060
15061 function Is_Inherited_Operation_For_Type
15062 (E : Entity_Id;
15063 Typ : Entity_Id) return Boolean
15064 is
15065 begin
15066 -- Check that the operation has been created by the type declaration
15067
15068 return Is_Inherited_Operation (E)
15069 and then Defining_Identifier (Parent (E)) = Typ;
15070 end Is_Inherited_Operation_For_Type;
15071
15072 --------------------------------------
15073 -- Is_Inlinable_Expression_Function --
15074 --------------------------------------
15075
15076 function Is_Inlinable_Expression_Function
15077 (Subp : Entity_Id) return Boolean
15078 is
15079 Return_Expr : Node_Id;
15080
15081 begin
15082 if Is_Expression_Function_Or_Completion (Subp)
15083 and then Has_Pragma_Inline_Always (Subp)
15084 and then Needs_No_Actuals (Subp)
15085 and then No (Contract (Subp))
15086 and then not Is_Dispatching_Operation (Subp)
15087 and then Needs_Finalization (Etype (Subp))
15088 and then not Is_Class_Wide_Type (Etype (Subp))
15089 and then not (Has_Invariants (Etype (Subp)))
15090 and then Present (Subprogram_Body (Subp))
15091 and then Was_Expression_Function (Subprogram_Body (Subp))
15092 then
15093 Return_Expr := Expression_Of_Expression_Function (Subp);
15094
15095 -- The returned object must not have a qualified expression and its
15096 -- nominal subtype must be statically compatible with the result
15097 -- subtype of the expression function.
15098
15099 return
15100 Nkind (Return_Expr) = N_Identifier
15101 and then Etype (Return_Expr) = Etype (Subp);
15102 end if;
15103
15104 return False;
15105 end Is_Inlinable_Expression_Function;
15106
15107 -----------------
15108 -- Is_Iterator --
15109 -----------------
15110
15111 function Is_Iterator (Typ : Entity_Id) return Boolean is
15112 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean;
15113 -- Determine whether type Iter_Typ is a predefined forward or reversible
15114 -- iterator.
15115
15116 ----------------------
15117 -- Denotes_Iterator --
15118 ----------------------
15119
15120 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean is
15121 begin
15122 -- Check that the name matches, and that the ultimate ancestor is in
15123 -- a predefined unit, i.e the one that declares iterator interfaces.
15124
15125 return
15126 Nam_In (Chars (Iter_Typ), Name_Forward_Iterator,
15127 Name_Reversible_Iterator)
15128 and then In_Predefined_Unit (Root_Type (Iter_Typ));
15129 end Denotes_Iterator;
15130
15131 -- Local variables
15132
15133 Iface_Elmt : Elmt_Id;
15134 Ifaces : Elist_Id;
15135
15136 -- Start of processing for Is_Iterator
15137
15138 begin
15139 -- The type may be a subtype of a descendant of the proper instance of
15140 -- the predefined interface type, so we must use the root type of the
15141 -- given type. The same is done for Is_Reversible_Iterator.
15142
15143 if Is_Class_Wide_Type (Typ)
15144 and then Denotes_Iterator (Root_Type (Typ))
15145 then
15146 return True;
15147
15148 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
15149 return False;
15150
15151 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
15152 return True;
15153
15154 else
15155 Collect_Interfaces (Typ, Ifaces);
15156
15157 Iface_Elmt := First_Elmt (Ifaces);
15158 while Present (Iface_Elmt) loop
15159 if Denotes_Iterator (Node (Iface_Elmt)) then
15160 return True;
15161 end if;
15162
15163 Next_Elmt (Iface_Elmt);
15164 end loop;
15165
15166 return False;
15167 end if;
15168 end Is_Iterator;
15169
15170 ----------------------------
15171 -- Is_Iterator_Over_Array --
15172 ----------------------------
15173
15174 function Is_Iterator_Over_Array (N : Node_Id) return Boolean is
15175 Container : constant Node_Id := Name (N);
15176 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
15177 begin
15178 return Is_Array_Type (Container_Typ);
15179 end Is_Iterator_Over_Array;
15180
15181 ------------
15182 -- Is_LHS --
15183 ------------
15184
15185 -- We seem to have a lot of overlapping functions that do similar things
15186 -- (testing for left hand sides or lvalues???).
15187
15188 function Is_LHS (N : Node_Id) return Is_LHS_Result is
15189 P : constant Node_Id := Parent (N);
15190
15191 begin
15192 -- Return True if we are the left hand side of an assignment statement
15193
15194 if Nkind (P) = N_Assignment_Statement then
15195 if Name (P) = N then
15196 return Yes;
15197 else
15198 return No;
15199 end if;
15200
15201 -- Case of prefix of indexed or selected component or slice
15202
15203 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
15204 and then N = Prefix (P)
15205 then
15206 -- Here we have the case where the parent P is N.Q or N(Q .. R).
15207 -- If P is an LHS, then N is also effectively an LHS, but there
15208 -- is an important exception. If N is of an access type, then
15209 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
15210 -- case this makes N.all a left hand side but not N itself.
15211
15212 -- If we don't know the type yet, this is the case where we return
15213 -- Unknown, since the answer depends on the type which is unknown.
15214
15215 if No (Etype (N)) then
15216 return Unknown;
15217
15218 -- We have an Etype set, so we can check it
15219
15220 elsif Is_Access_Type (Etype (N)) then
15221 return No;
15222
15223 -- OK, not access type case, so just test whole expression
15224
15225 else
15226 return Is_LHS (P);
15227 end if;
15228
15229 -- All other cases are not left hand sides
15230
15231 else
15232 return No;
15233 end if;
15234 end Is_LHS;
15235
15236 -----------------------------
15237 -- Is_Library_Level_Entity --
15238 -----------------------------
15239
15240 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
15241 begin
15242 -- The following is a small optimization, and it also properly handles
15243 -- discriminals, which in task bodies might appear in expressions before
15244 -- the corresponding procedure has been created, and which therefore do
15245 -- not have an assigned scope.
15246
15247 if Is_Formal (E) then
15248 return False;
15249 end if;
15250
15251 -- Normal test is simply that the enclosing dynamic scope is Standard
15252
15253 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
15254 end Is_Library_Level_Entity;
15255
15256 --------------------------------
15257 -- Is_Limited_Class_Wide_Type --
15258 --------------------------------
15259
15260 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
15261 begin
15262 return
15263 Is_Class_Wide_Type (Typ)
15264 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
15265 end Is_Limited_Class_Wide_Type;
15266
15267 ---------------------------------
15268 -- Is_Local_Variable_Reference --
15269 ---------------------------------
15270
15271 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
15272 begin
15273 if not Is_Entity_Name (Expr) then
15274 return False;
15275
15276 else
15277 declare
15278 Ent : constant Entity_Id := Entity (Expr);
15279 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
15280 begin
15281 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
15282 return False;
15283 else
15284 return Present (Sub) and then Sub = Current_Subprogram;
15285 end if;
15286 end;
15287 end if;
15288 end Is_Local_Variable_Reference;
15289
15290 -----------------------
15291 -- Is_Name_Reference --
15292 -----------------------
15293
15294 function Is_Name_Reference (N : Node_Id) return Boolean is
15295 begin
15296 if Is_Entity_Name (N) then
15297 return Present (Entity (N)) and then Is_Object (Entity (N));
15298 end if;
15299
15300 case Nkind (N) is
15301 when N_Indexed_Component
15302 | N_Slice
15303 =>
15304 return
15305 Is_Name_Reference (Prefix (N))
15306 or else Is_Access_Type (Etype (Prefix (N)));
15307
15308 -- Attributes 'Input, 'Old and 'Result produce objects
15309
15310 when N_Attribute_Reference =>
15311 return
15312 Nam_In (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
15313
15314 when N_Selected_Component =>
15315 return
15316 Is_Name_Reference (Selector_Name (N))
15317 and then
15318 (Is_Name_Reference (Prefix (N))
15319 or else Is_Access_Type (Etype (Prefix (N))));
15320
15321 when N_Explicit_Dereference =>
15322 return True;
15323
15324 -- A view conversion of a tagged name is a name reference
15325
15326 when N_Type_Conversion =>
15327 return
15328 Is_Tagged_Type (Etype (Subtype_Mark (N)))
15329 and then Is_Tagged_Type (Etype (Expression (N)))
15330 and then Is_Name_Reference (Expression (N));
15331
15332 -- An unchecked type conversion is considered to be a name if the
15333 -- operand is a name (this construction arises only as a result of
15334 -- expansion activities).
15335
15336 when N_Unchecked_Type_Conversion =>
15337 return Is_Name_Reference (Expression (N));
15338
15339 when others =>
15340 return False;
15341 end case;
15342 end Is_Name_Reference;
15343
15344 ------------------------------------
15345 -- Is_Non_Preelaborable_Construct --
15346 ------------------------------------
15347
15348 function Is_Non_Preelaborable_Construct (N : Node_Id) return Boolean is
15349
15350 -- NOTE: the routines within Is_Non_Preelaborable_Construct are
15351 -- intentionally unnested to avoid deep indentation of code.
15352
15353 Non_Preelaborable : exception;
15354 -- This exception is raised when the construct violates preelaborability
15355 -- to terminate the recursion.
15356
15357 procedure Visit (Nod : Node_Id);
15358 -- Semantically inspect construct Nod to determine whether it violates
15359 -- preelaborability. This routine raises Non_Preelaborable.
15360
15361 procedure Visit_List (List : List_Id);
15362 pragma Inline (Visit_List);
15363 -- Invoke Visit on each element of list List. This routine raises
15364 -- Non_Preelaborable.
15365
15366 procedure Visit_Pragma (Prag : Node_Id);
15367 pragma Inline (Visit_Pragma);
15368 -- Semantically inspect pragma Prag to determine whether it violates
15369 -- preelaborability. This routine raises Non_Preelaborable.
15370
15371 procedure Visit_Subexpression (Expr : Node_Id);
15372 pragma Inline (Visit_Subexpression);
15373 -- Semantically inspect expression Expr to determine whether it violates
15374 -- preelaborability. This routine raises Non_Preelaborable.
15375
15376 -----------
15377 -- Visit --
15378 -----------
15379
15380 procedure Visit (Nod : Node_Id) is
15381 begin
15382 case Nkind (Nod) is
15383
15384 -- Declarations
15385
15386 when N_Component_Declaration =>
15387
15388 -- Defining_Identifier is left out because it is not relevant
15389 -- for preelaborability.
15390
15391 Visit (Component_Definition (Nod));
15392 Visit (Expression (Nod));
15393
15394 when N_Derived_Type_Definition =>
15395
15396 -- Interface_List is left out because it is not relevant for
15397 -- preelaborability.
15398
15399 Visit (Record_Extension_Part (Nod));
15400 Visit (Subtype_Indication (Nod));
15401
15402 when N_Entry_Declaration =>
15403
15404 -- A protected type with at leat one entry is not preelaborable
15405 -- while task types are never preelaborable. This renders entry
15406 -- declarations non-preelaborable.
15407
15408 raise Non_Preelaborable;
15409
15410 when N_Full_Type_Declaration =>
15411
15412 -- Defining_Identifier and Discriminant_Specifications are left
15413 -- out because they are not relevant for preelaborability.
15414
15415 Visit (Type_Definition (Nod));
15416
15417 when N_Function_Instantiation
15418 | N_Package_Instantiation
15419 | N_Procedure_Instantiation
15420 =>
15421 -- Defining_Unit_Name and Name are left out because they are
15422 -- not relevant for preelaborability.
15423
15424 Visit_List (Generic_Associations (Nod));
15425
15426 when N_Object_Declaration =>
15427
15428 -- Defining_Identifier is left out because it is not relevant
15429 -- for preelaborability.
15430
15431 Visit (Object_Definition (Nod));
15432
15433 if Has_Init_Expression (Nod) then
15434 Visit (Expression (Nod));
15435
15436 elsif not Has_Preelaborable_Initialization
15437 (Etype (Defining_Entity (Nod)))
15438 then
15439 raise Non_Preelaborable;
15440 end if;
15441
15442 when N_Private_Extension_Declaration
15443 | N_Subtype_Declaration
15444 =>
15445 -- Defining_Identifier, Discriminant_Specifications, and
15446 -- Interface_List are left out because they are not relevant
15447 -- for preelaborability.
15448
15449 Visit (Subtype_Indication (Nod));
15450
15451 when N_Protected_Type_Declaration
15452 | N_Single_Protected_Declaration
15453 =>
15454 -- Defining_Identifier, Discriminant_Specifications, and
15455 -- Interface_List are left out because they are not relevant
15456 -- for preelaborability.
15457
15458 Visit (Protected_Definition (Nod));
15459
15460 -- A [single] task type is never preelaborable
15461
15462 when N_Single_Task_Declaration
15463 | N_Task_Type_Declaration
15464 =>
15465 raise Non_Preelaborable;
15466
15467 -- Pragmas
15468
15469 when N_Pragma =>
15470 Visit_Pragma (Nod);
15471
15472 -- Statements
15473
15474 when N_Statement_Other_Than_Procedure_Call =>
15475 if Nkind (Nod) /= N_Null_Statement then
15476 raise Non_Preelaborable;
15477 end if;
15478
15479 -- Subexpressions
15480
15481 when N_Subexpr =>
15482 Visit_Subexpression (Nod);
15483
15484 -- Special
15485
15486 when N_Access_To_Object_Definition =>
15487 Visit (Subtype_Indication (Nod));
15488
15489 when N_Case_Expression_Alternative =>
15490 Visit (Expression (Nod));
15491 Visit_List (Discrete_Choices (Nod));
15492
15493 when N_Component_Definition =>
15494 Visit (Access_Definition (Nod));
15495 Visit (Subtype_Indication (Nod));
15496
15497 when N_Component_List =>
15498 Visit_List (Component_Items (Nod));
15499 Visit (Variant_Part (Nod));
15500
15501 when N_Constrained_Array_Definition =>
15502 Visit_List (Discrete_Subtype_Definitions (Nod));
15503 Visit (Component_Definition (Nod));
15504
15505 when N_Delta_Constraint
15506 | N_Digits_Constraint
15507 =>
15508 -- Delta_Expression and Digits_Expression are left out because
15509 -- they are not relevant for preelaborability.
15510
15511 Visit (Range_Constraint (Nod));
15512
15513 when N_Discriminant_Specification =>
15514
15515 -- Defining_Identifier and Expression are left out because they
15516 -- are not relevant for preelaborability.
15517
15518 Visit (Discriminant_Type (Nod));
15519
15520 when N_Generic_Association =>
15521
15522 -- Selector_Name is left out because it is not relevant for
15523 -- preelaborability.
15524
15525 Visit (Explicit_Generic_Actual_Parameter (Nod));
15526
15527 when N_Index_Or_Discriminant_Constraint =>
15528 Visit_List (Constraints (Nod));
15529
15530 when N_Iterator_Specification =>
15531
15532 -- Defining_Identifier is left out because it is not relevant
15533 -- for preelaborability.
15534
15535 Visit (Name (Nod));
15536 Visit (Subtype_Indication (Nod));
15537
15538 when N_Loop_Parameter_Specification =>
15539
15540 -- Defining_Identifier is left out because it is not relevant
15541 -- for preelaborability.
15542
15543 Visit (Discrete_Subtype_Definition (Nod));
15544
15545 when N_Protected_Definition =>
15546
15547 -- End_Label is left out because it is not relevant for
15548 -- preelaborability.
15549
15550 Visit_List (Private_Declarations (Nod));
15551 Visit_List (Visible_Declarations (Nod));
15552
15553 when N_Range_Constraint =>
15554 Visit (Range_Expression (Nod));
15555
15556 when N_Record_Definition
15557 | N_Variant
15558 =>
15559 -- End_Label, Discrete_Choices, and Interface_List are left out
15560 -- because they are not relevant for preelaborability.
15561
15562 Visit (Component_List (Nod));
15563
15564 when N_Subtype_Indication =>
15565
15566 -- Subtype_Mark is left out because it is not relevant for
15567 -- preelaborability.
15568
15569 Visit (Constraint (Nod));
15570
15571 when N_Unconstrained_Array_Definition =>
15572
15573 -- Subtype_Marks is left out because it is not relevant for
15574 -- preelaborability.
15575
15576 Visit (Component_Definition (Nod));
15577
15578 when N_Variant_Part =>
15579
15580 -- Name is left out because it is not relevant for
15581 -- preelaborability.
15582
15583 Visit_List (Variants (Nod));
15584
15585 -- Default
15586
15587 when others =>
15588 null;
15589 end case;
15590 end Visit;
15591
15592 ----------------
15593 -- Visit_List --
15594 ----------------
15595
15596 procedure Visit_List (List : List_Id) is
15597 Nod : Node_Id;
15598
15599 begin
15600 if Present (List) then
15601 Nod := First (List);
15602 while Present (Nod) loop
15603 Visit (Nod);
15604 Next (Nod);
15605 end loop;
15606 end if;
15607 end Visit_List;
15608
15609 ------------------
15610 -- Visit_Pragma --
15611 ------------------
15612
15613 procedure Visit_Pragma (Prag : Node_Id) is
15614 begin
15615 case Get_Pragma_Id (Prag) is
15616 when Pragma_Assert
15617 | Pragma_Assert_And_Cut
15618 | Pragma_Assume
15619 | Pragma_Async_Readers
15620 | Pragma_Async_Writers
15621 | Pragma_Attribute_Definition
15622 | Pragma_Check
15623 | Pragma_Constant_After_Elaboration
15624 | Pragma_CPU
15625 | Pragma_Deadline_Floor
15626 | Pragma_Dispatching_Domain
15627 | Pragma_Effective_Reads
15628 | Pragma_Effective_Writes
15629 | Pragma_Extensions_Visible
15630 | Pragma_Ghost
15631 | Pragma_Secondary_Stack_Size
15632 | Pragma_Task_Name
15633 | Pragma_Volatile_Function
15634 =>
15635 Visit_List (Pragma_Argument_Associations (Prag));
15636
15637 -- Default
15638
15639 when others =>
15640 null;
15641 end case;
15642 end Visit_Pragma;
15643
15644 -------------------------
15645 -- Visit_Subexpression --
15646 -------------------------
15647
15648 procedure Visit_Subexpression (Expr : Node_Id) is
15649 procedure Visit_Aggregate (Aggr : Node_Id);
15650 pragma Inline (Visit_Aggregate);
15651 -- Semantically inspect aggregate Aggr to determine whether it
15652 -- violates preelaborability.
15653
15654 ---------------------
15655 -- Visit_Aggregate --
15656 ---------------------
15657
15658 procedure Visit_Aggregate (Aggr : Node_Id) is
15659 begin
15660 if not Is_Preelaborable_Aggregate (Aggr) then
15661 raise Non_Preelaborable;
15662 end if;
15663 end Visit_Aggregate;
15664
15665 -- Start of processing for Visit_Subexpression
15666
15667 begin
15668 case Nkind (Expr) is
15669 when N_Allocator
15670 | N_Qualified_Expression
15671 | N_Type_Conversion
15672 | N_Unchecked_Expression
15673 | N_Unchecked_Type_Conversion
15674 =>
15675 -- Subpool_Handle_Name and Subtype_Mark are left out because
15676 -- they are not relevant for preelaborability.
15677
15678 Visit (Expression (Expr));
15679
15680 when N_Aggregate
15681 | N_Extension_Aggregate
15682 =>
15683 Visit_Aggregate (Expr);
15684
15685 when N_Attribute_Reference
15686 | N_Explicit_Dereference
15687 | N_Reference
15688 =>
15689 -- Attribute_Name and Expressions are left out because they are
15690 -- not relevant for preelaborability.
15691
15692 Visit (Prefix (Expr));
15693
15694 when N_Case_Expression =>
15695
15696 -- End_Span is left out because it is not relevant for
15697 -- preelaborability.
15698
15699 Visit_List (Alternatives (Expr));
15700 Visit (Expression (Expr));
15701
15702 when N_Delta_Aggregate =>
15703 Visit_Aggregate (Expr);
15704 Visit (Expression (Expr));
15705
15706 when N_Expression_With_Actions =>
15707 Visit_List (Actions (Expr));
15708 Visit (Expression (Expr));
15709
15710 when N_If_Expression =>
15711 Visit_List (Expressions (Expr));
15712
15713 when N_Quantified_Expression =>
15714 Visit (Condition (Expr));
15715 Visit (Iterator_Specification (Expr));
15716 Visit (Loop_Parameter_Specification (Expr));
15717
15718 when N_Range =>
15719 Visit (High_Bound (Expr));
15720 Visit (Low_Bound (Expr));
15721
15722 when N_Slice =>
15723 Visit (Discrete_Range (Expr));
15724 Visit (Prefix (Expr));
15725
15726 -- Default
15727
15728 when others =>
15729
15730 -- The evaluation of an object name is not preelaborable,
15731 -- unless the name is a static expression (checked further
15732 -- below), or statically denotes a discriminant.
15733
15734 if Is_Entity_Name (Expr) then
15735 Object_Name : declare
15736 Id : constant Entity_Id := Entity (Expr);
15737
15738 begin
15739 if Is_Object (Id) then
15740 if Ekind (Id) = E_Discriminant then
15741 null;
15742
15743 elsif Ekind_In (Id, E_Constant, E_In_Parameter)
15744 and then Present (Discriminal_Link (Id))
15745 then
15746 null;
15747
15748 else
15749 raise Non_Preelaborable;
15750 end if;
15751 end if;
15752 end Object_Name;
15753
15754 -- A non-static expression is not preelaborable
15755
15756 elsif not Is_OK_Static_Expression (Expr) then
15757 raise Non_Preelaborable;
15758 end if;
15759 end case;
15760 end Visit_Subexpression;
15761
15762 -- Start of processing for Is_Non_Preelaborable_Construct
15763
15764 begin
15765 Visit (N);
15766
15767 -- At this point it is known that the construct is preelaborable
15768
15769 return False;
15770
15771 exception
15772
15773 -- The elaboration of the construct performs an action which violates
15774 -- preelaborability.
15775
15776 when Non_Preelaborable =>
15777 return True;
15778 end Is_Non_Preelaborable_Construct;
15779
15780 ---------------------------------
15781 -- Is_Nontrivial_DIC_Procedure --
15782 ---------------------------------
15783
15784 function Is_Nontrivial_DIC_Procedure (Id : Entity_Id) return Boolean is
15785 Body_Decl : Node_Id;
15786 Stmt : Node_Id;
15787
15788 begin
15789 if Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id) then
15790 Body_Decl :=
15791 Unit_Declaration_Node
15792 (Corresponding_Body (Unit_Declaration_Node (Id)));
15793
15794 -- The body of the Default_Initial_Condition procedure must contain
15795 -- at least one statement, otherwise the generation of the subprogram
15796 -- body failed.
15797
15798 pragma Assert (Present (Handled_Statement_Sequence (Body_Decl)));
15799
15800 -- To qualify as nontrivial, the first statement of the procedure
15801 -- must be a check in the form of an if statement. If the original
15802 -- Default_Initial_Condition expression was folded, then the first
15803 -- statement is not a check.
15804
15805 Stmt := First (Statements (Handled_Statement_Sequence (Body_Decl)));
15806
15807 return
15808 Nkind (Stmt) = N_If_Statement
15809 and then Nkind (Original_Node (Stmt)) = N_Pragma;
15810 end if;
15811
15812 return False;
15813 end Is_Nontrivial_DIC_Procedure;
15814
15815 -------------------------
15816 -- Is_Null_Record_Type --
15817 -------------------------
15818
15819 function Is_Null_Record_Type (T : Entity_Id) return Boolean is
15820 Decl : constant Node_Id := Parent (T);
15821 begin
15822 return Nkind (Decl) = N_Full_Type_Declaration
15823 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
15824 and then
15825 (No (Component_List (Type_Definition (Decl)))
15826 or else Null_Present (Component_List (Type_Definition (Decl))));
15827 end Is_Null_Record_Type;
15828
15829 ---------------------
15830 -- Is_Object_Image --
15831 ---------------------
15832
15833 function Is_Object_Image (Prefix : Node_Id) return Boolean is
15834 begin
15835 -- When the type of the prefix is not scalar, then the prefix is not
15836 -- valid in any scenario.
15837
15838 if not Is_Scalar_Type (Etype (Prefix)) then
15839 return False;
15840 end if;
15841
15842 -- Here we test for the case that the prefix is not a type and assume
15843 -- if it is not then it must be a named value or an object reference.
15844 -- This is because the parser always checks that prefixes of attributes
15845 -- are named.
15846
15847 return not (Is_Entity_Name (Prefix) and then Is_Type (Entity (Prefix)));
15848 end Is_Object_Image;
15849
15850 -------------------------
15851 -- Is_Object_Reference --
15852 -------------------------
15853
15854 function Is_Object_Reference (N : Node_Id) return Boolean is
15855 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
15856 -- Determine whether N is the name of an internally-generated renaming
15857
15858 --------------------------------------
15859 -- Is_Internally_Generated_Renaming --
15860 --------------------------------------
15861
15862 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
15863 P : Node_Id;
15864
15865 begin
15866 P := N;
15867 while Present (P) loop
15868 if Nkind (P) = N_Object_Renaming_Declaration then
15869 return not Comes_From_Source (P);
15870 elsif Is_List_Member (P) then
15871 return False;
15872 end if;
15873
15874 P := Parent (P);
15875 end loop;
15876
15877 return False;
15878 end Is_Internally_Generated_Renaming;
15879
15880 -- Start of processing for Is_Object_Reference
15881
15882 begin
15883 if Is_Entity_Name (N) then
15884 return Present (Entity (N)) and then Is_Object (Entity (N));
15885
15886 else
15887 case Nkind (N) is
15888 when N_Indexed_Component
15889 | N_Slice
15890 =>
15891 return
15892 Is_Object_Reference (Prefix (N))
15893 or else Is_Access_Type (Etype (Prefix (N)));
15894
15895 -- In Ada 95, a function call is a constant object; a procedure
15896 -- call is not.
15897
15898 -- Note that predefined operators are functions as well, and so
15899 -- are attributes that are (can be renamed as) functions.
15900
15901 when N_Binary_Op
15902 | N_Function_Call
15903 | N_Unary_Op
15904 =>
15905 return Etype (N) /= Standard_Void_Type;
15906
15907 -- Attributes references 'Loop_Entry, 'Old, and 'Result yield
15908 -- objects, even though they are not functions.
15909
15910 when N_Attribute_Reference =>
15911 return
15912 Nam_In (Attribute_Name (N), Name_Loop_Entry,
15913 Name_Old,
15914 Name_Result)
15915 or else Is_Function_Attribute_Name (Attribute_Name (N));
15916
15917 when N_Selected_Component =>
15918 return
15919 Is_Object_Reference (Selector_Name (N))
15920 and then
15921 (Is_Object_Reference (Prefix (N))
15922 or else Is_Access_Type (Etype (Prefix (N))));
15923
15924 -- An explicit dereference denotes an object, except that a
15925 -- conditional expression gets turned into an explicit dereference
15926 -- in some cases, and conditional expressions are not object
15927 -- names.
15928
15929 when N_Explicit_Dereference =>
15930 return not Nkind_In (Original_Node (N), N_Case_Expression,
15931 N_If_Expression);
15932
15933 -- A view conversion of a tagged object is an object reference
15934
15935 when N_Type_Conversion =>
15936 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
15937 and then Is_Tagged_Type (Etype (Expression (N)))
15938 and then Is_Object_Reference (Expression (N));
15939
15940 -- An unchecked type conversion is considered to be an object if
15941 -- the operand is an object (this construction arises only as a
15942 -- result of expansion activities).
15943
15944 when N_Unchecked_Type_Conversion =>
15945 return True;
15946
15947 -- Allow string literals to act as objects as long as they appear
15948 -- in internally-generated renamings. The expansion of iterators
15949 -- may generate such renamings when the range involves a string
15950 -- literal.
15951
15952 when N_String_Literal =>
15953 return Is_Internally_Generated_Renaming (Parent (N));
15954
15955 -- AI05-0003: In Ada 2012 a qualified expression is a name.
15956 -- This allows disambiguation of function calls and the use
15957 -- of aggregates in more contexts.
15958
15959 when N_Qualified_Expression =>
15960 if Ada_Version < Ada_2012 then
15961 return False;
15962 else
15963 return Is_Object_Reference (Expression (N))
15964 or else Nkind (Expression (N)) = N_Aggregate;
15965 end if;
15966
15967 when others =>
15968 return False;
15969 end case;
15970 end if;
15971 end Is_Object_Reference;
15972
15973 -----------------------------------
15974 -- Is_OK_Variable_For_Out_Formal --
15975 -----------------------------------
15976
15977 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
15978 begin
15979 Note_Possible_Modification (AV, Sure => True);
15980
15981 -- We must reject parenthesized variable names. Comes_From_Source is
15982 -- checked because there are currently cases where the compiler violates
15983 -- this rule (e.g. passing a task object to its controlled Initialize
15984 -- routine). This should be properly documented in sinfo???
15985
15986 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
15987 return False;
15988
15989 -- A variable is always allowed
15990
15991 elsif Is_Variable (AV) then
15992 return True;
15993
15994 -- Generalized indexing operations are rewritten as explicit
15995 -- dereferences, and it is only during resolution that we can
15996 -- check whether the context requires an access_to_variable type.
15997
15998 elsif Nkind (AV) = N_Explicit_Dereference
15999 and then Ada_Version >= Ada_2012
16000 and then Nkind (Original_Node (AV)) = N_Indexed_Component
16001 and then Present (Etype (Original_Node (AV)))
16002 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
16003 then
16004 return not Is_Access_Constant (Etype (Prefix (AV)));
16005
16006 -- Unchecked conversions are allowed only if they come from the
16007 -- generated code, which sometimes uses unchecked conversions for out
16008 -- parameters in cases where code generation is unaffected. We tell
16009 -- source unchecked conversions by seeing if they are rewrites of
16010 -- an original Unchecked_Conversion function call, or of an explicit
16011 -- conversion of a function call or an aggregate (as may happen in the
16012 -- expansion of a packed array aggregate).
16013
16014 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
16015 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
16016 return False;
16017
16018 elsif Comes_From_Source (AV)
16019 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
16020 then
16021 return False;
16022
16023 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
16024 return Is_OK_Variable_For_Out_Formal (Expression (AV));
16025
16026 else
16027 return True;
16028 end if;
16029
16030 -- Normal type conversions are allowed if argument is a variable
16031
16032 elsif Nkind (AV) = N_Type_Conversion then
16033 if Is_Variable (Expression (AV))
16034 and then Paren_Count (Expression (AV)) = 0
16035 then
16036 Note_Possible_Modification (Expression (AV), Sure => True);
16037 return True;
16038
16039 -- We also allow a non-parenthesized expression that raises
16040 -- constraint error if it rewrites what used to be a variable
16041
16042 elsif Raises_Constraint_Error (Expression (AV))
16043 and then Paren_Count (Expression (AV)) = 0
16044 and then Is_Variable (Original_Node (Expression (AV)))
16045 then
16046 return True;
16047
16048 -- Type conversion of something other than a variable
16049
16050 else
16051 return False;
16052 end if;
16053
16054 -- If this node is rewritten, then test the original form, if that is
16055 -- OK, then we consider the rewritten node OK (for example, if the
16056 -- original node is a conversion, then Is_Variable will not be true
16057 -- but we still want to allow the conversion if it converts a variable).
16058
16059 elsif Is_Rewrite_Substitution (AV) then
16060
16061 -- In Ada 2012, the explicit dereference may be a rewritten call to a
16062 -- Reference function.
16063
16064 if Ada_Version >= Ada_2012
16065 and then Nkind (Original_Node (AV)) = N_Function_Call
16066 and then
16067 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
16068 then
16069
16070 -- Check that this is not a constant reference.
16071
16072 return not Is_Access_Constant (Etype (Prefix (AV)));
16073
16074 elsif Has_Implicit_Dereference (Etype (Original_Node (AV))) then
16075 return
16076 not Is_Access_Constant (Etype
16077 (Get_Reference_Discriminant (Etype (Original_Node (AV)))));
16078
16079 else
16080 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
16081 end if;
16082
16083 -- All other non-variables are rejected
16084
16085 else
16086 return False;
16087 end if;
16088 end Is_OK_Variable_For_Out_Formal;
16089
16090 ----------------------------
16091 -- Is_OK_Volatile_Context --
16092 ----------------------------
16093
16094 function Is_OK_Volatile_Context
16095 (Context : Node_Id;
16096 Obj_Ref : Node_Id) return Boolean
16097 is
16098 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
16099 -- Determine whether an arbitrary node denotes a call to a protected
16100 -- entry, function, or procedure in prefixed form where the prefix is
16101 -- Obj_Ref.
16102
16103 function Within_Check (Nod : Node_Id) return Boolean;
16104 -- Determine whether an arbitrary node appears in a check node
16105
16106 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
16107 -- Determine whether an arbitrary entity appears in a volatile function
16108
16109 ---------------------------------
16110 -- Is_Protected_Operation_Call --
16111 ---------------------------------
16112
16113 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
16114 Pref : Node_Id;
16115 Subp : Node_Id;
16116
16117 begin
16118 -- A call to a protected operations retains its selected component
16119 -- form as opposed to other prefixed calls that are transformed in
16120 -- expanded names.
16121
16122 if Nkind (Nod) = N_Selected_Component then
16123 Pref := Prefix (Nod);
16124 Subp := Selector_Name (Nod);
16125
16126 return
16127 Pref = Obj_Ref
16128 and then Present (Etype (Pref))
16129 and then Is_Protected_Type (Etype (Pref))
16130 and then Is_Entity_Name (Subp)
16131 and then Present (Entity (Subp))
16132 and then Ekind_In (Entity (Subp), E_Entry,
16133 E_Entry_Family,
16134 E_Function,
16135 E_Procedure);
16136 else
16137 return False;
16138 end if;
16139 end Is_Protected_Operation_Call;
16140
16141 ------------------
16142 -- Within_Check --
16143 ------------------
16144
16145 function Within_Check (Nod : Node_Id) return Boolean is
16146 Par : Node_Id;
16147
16148 begin
16149 -- Climb the parent chain looking for a check node
16150
16151 Par := Nod;
16152 while Present (Par) loop
16153 if Nkind (Par) in N_Raise_xxx_Error then
16154 return True;
16155
16156 -- Prevent the search from going too far
16157
16158 elsif Is_Body_Or_Package_Declaration (Par) then
16159 exit;
16160 end if;
16161
16162 Par := Parent (Par);
16163 end loop;
16164
16165 return False;
16166 end Within_Check;
16167
16168 ------------------------------
16169 -- Within_Volatile_Function --
16170 ------------------------------
16171
16172 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
16173 Func_Id : Entity_Id;
16174
16175 begin
16176 -- Traverse the scope stack looking for a [generic] function
16177
16178 Func_Id := Id;
16179 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
16180 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
16181 return Is_Volatile_Function (Func_Id);
16182 end if;
16183
16184 Func_Id := Scope (Func_Id);
16185 end loop;
16186
16187 return False;
16188 end Within_Volatile_Function;
16189
16190 -- Local variables
16191
16192 Obj_Id : Entity_Id;
16193
16194 -- Start of processing for Is_OK_Volatile_Context
16195
16196 begin
16197 -- The volatile object appears on either side of an assignment
16198
16199 if Nkind (Context) = N_Assignment_Statement then
16200 return True;
16201
16202 -- The volatile object is part of the initialization expression of
16203 -- another object.
16204
16205 elsif Nkind (Context) = N_Object_Declaration
16206 and then Present (Expression (Context))
16207 and then Expression (Context) = Obj_Ref
16208 then
16209 Obj_Id := Defining_Entity (Context);
16210
16211 -- The volatile object acts as the initialization expression of an
16212 -- extended return statement. This is valid context as long as the
16213 -- function is volatile.
16214
16215 if Is_Return_Object (Obj_Id) then
16216 return Within_Volatile_Function (Obj_Id);
16217
16218 -- Otherwise this is a normal object initialization
16219
16220 else
16221 return True;
16222 end if;
16223
16224 -- The volatile object acts as the name of a renaming declaration
16225
16226 elsif Nkind (Context) = N_Object_Renaming_Declaration
16227 and then Name (Context) = Obj_Ref
16228 then
16229 return True;
16230
16231 -- The volatile object appears as an actual parameter in a call to an
16232 -- instance of Unchecked_Conversion whose result is renamed.
16233
16234 elsif Nkind (Context) = N_Function_Call
16235 and then Is_Entity_Name (Name (Context))
16236 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
16237 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
16238 then
16239 return True;
16240
16241 -- The volatile object is actually the prefix in a protected entry,
16242 -- function, or procedure call.
16243
16244 elsif Is_Protected_Operation_Call (Context) then
16245 return True;
16246
16247 -- The volatile object appears as the expression of a simple return
16248 -- statement that applies to a volatile function.
16249
16250 elsif Nkind (Context) = N_Simple_Return_Statement
16251 and then Expression (Context) = Obj_Ref
16252 then
16253 return
16254 Within_Volatile_Function (Return_Statement_Entity (Context));
16255
16256 -- The volatile object appears as the prefix of a name occurring in a
16257 -- non-interfering context.
16258
16259 elsif Nkind_In (Context, N_Attribute_Reference,
16260 N_Explicit_Dereference,
16261 N_Indexed_Component,
16262 N_Selected_Component,
16263 N_Slice)
16264 and then Prefix (Context) = Obj_Ref
16265 and then Is_OK_Volatile_Context
16266 (Context => Parent (Context),
16267 Obj_Ref => Context)
16268 then
16269 return True;
16270
16271 -- The volatile object appears as the prefix of attributes Address,
16272 -- Alignment, Component_Size, First, First_Bit, Last, Last_Bit, Length,
16273 -- Position, Size, Storage_Size.
16274
16275 elsif Nkind (Context) = N_Attribute_Reference
16276 and then Prefix (Context) = Obj_Ref
16277 and then Nam_In (Attribute_Name (Context), Name_Address,
16278 Name_Alignment,
16279 Name_Component_Size,
16280 Name_First,
16281 Name_First_Bit,
16282 Name_Last,
16283 Name_Last_Bit,
16284 Name_Length,
16285 Name_Position,
16286 Name_Size,
16287 Name_Storage_Size)
16288 then
16289 return True;
16290
16291 -- The volatile object appears as the expression of a type conversion
16292 -- occurring in a non-interfering context.
16293
16294 elsif Nkind_In (Context, N_Type_Conversion,
16295 N_Unchecked_Type_Conversion)
16296 and then Expression (Context) = Obj_Ref
16297 and then Is_OK_Volatile_Context
16298 (Context => Parent (Context),
16299 Obj_Ref => Context)
16300 then
16301 return True;
16302
16303 -- The volatile object appears as the expression in a delay statement
16304
16305 elsif Nkind (Context) in N_Delay_Statement then
16306 return True;
16307
16308 -- Allow references to volatile objects in various checks. This is not a
16309 -- direct SPARK 2014 requirement.
16310
16311 elsif Within_Check (Context) then
16312 return True;
16313
16314 -- Assume that references to effectively volatile objects that appear
16315 -- as actual parameters in a subprogram call are always legal. A full
16316 -- legality check is done when the actuals are resolved (see routine
16317 -- Resolve_Actuals).
16318
16319 elsif Within_Subprogram_Call (Context) then
16320 return True;
16321
16322 -- Otherwise the context is not suitable for an effectively volatile
16323 -- object.
16324
16325 else
16326 return False;
16327 end if;
16328 end Is_OK_Volatile_Context;
16329
16330 ------------------------------------
16331 -- Is_Package_Contract_Annotation --
16332 ------------------------------------
16333
16334 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
16335 Nam : Name_Id;
16336
16337 begin
16338 if Nkind (Item) = N_Aspect_Specification then
16339 Nam := Chars (Identifier (Item));
16340
16341 else pragma Assert (Nkind (Item) = N_Pragma);
16342 Nam := Pragma_Name (Item);
16343 end if;
16344
16345 return Nam = Name_Abstract_State
16346 or else Nam = Name_Initial_Condition
16347 or else Nam = Name_Initializes
16348 or else Nam = Name_Refined_State;
16349 end Is_Package_Contract_Annotation;
16350
16351 -----------------------------------
16352 -- Is_Partially_Initialized_Type --
16353 -----------------------------------
16354
16355 function Is_Partially_Initialized_Type
16356 (Typ : Entity_Id;
16357 Include_Implicit : Boolean := True) return Boolean
16358 is
16359 begin
16360 if Is_Scalar_Type (Typ) then
16361 return False;
16362
16363 elsif Is_Access_Type (Typ) then
16364 return Include_Implicit;
16365
16366 elsif Is_Array_Type (Typ) then
16367
16368 -- If component type is partially initialized, so is array type
16369
16370 if Is_Partially_Initialized_Type
16371 (Component_Type (Typ), Include_Implicit)
16372 then
16373 return True;
16374
16375 -- Otherwise we are only partially initialized if we are fully
16376 -- initialized (this is the empty array case, no point in us
16377 -- duplicating that code here).
16378
16379 else
16380 return Is_Fully_Initialized_Type (Typ);
16381 end if;
16382
16383 elsif Is_Record_Type (Typ) then
16384
16385 -- A discriminated type is always partially initialized if in
16386 -- all mode
16387
16388 if Has_Discriminants (Typ) and then Include_Implicit then
16389 return True;
16390
16391 -- A tagged type is always partially initialized
16392
16393 elsif Is_Tagged_Type (Typ) then
16394 return True;
16395
16396 -- Case of non-discriminated record
16397
16398 else
16399 declare
16400 Ent : Entity_Id;
16401
16402 Component_Present : Boolean := False;
16403 -- Set True if at least one component is present. If no
16404 -- components are present, then record type is fully
16405 -- initialized (another odd case, like the null array).
16406
16407 begin
16408 -- Loop through components
16409
16410 Ent := First_Entity (Typ);
16411 while Present (Ent) loop
16412 if Ekind (Ent) = E_Component then
16413 Component_Present := True;
16414
16415 -- If a component has an initialization expression then
16416 -- the enclosing record type is partially initialized
16417
16418 if Present (Parent (Ent))
16419 and then Present (Expression (Parent (Ent)))
16420 then
16421 return True;
16422
16423 -- If a component is of a type which is itself partially
16424 -- initialized, then the enclosing record type is also.
16425
16426 elsif Is_Partially_Initialized_Type
16427 (Etype (Ent), Include_Implicit)
16428 then
16429 return True;
16430 end if;
16431 end if;
16432
16433 Next_Entity (Ent);
16434 end loop;
16435
16436 -- No initialized components found. If we found any components
16437 -- they were all uninitialized so the result is false.
16438
16439 if Component_Present then
16440 return False;
16441
16442 -- But if we found no components, then all the components are
16443 -- initialized so we consider the type to be initialized.
16444
16445 else
16446 return True;
16447 end if;
16448 end;
16449 end if;
16450
16451 -- Concurrent types are always fully initialized
16452
16453 elsif Is_Concurrent_Type (Typ) then
16454 return True;
16455
16456 -- For a private type, go to underlying type. If there is no underlying
16457 -- type then just assume this partially initialized. Not clear if this
16458 -- can happen in a non-error case, but no harm in testing for this.
16459
16460 elsif Is_Private_Type (Typ) then
16461 declare
16462 U : constant Entity_Id := Underlying_Type (Typ);
16463 begin
16464 if No (U) then
16465 return True;
16466 else
16467 return Is_Partially_Initialized_Type (U, Include_Implicit);
16468 end if;
16469 end;
16470
16471 -- For any other type (are there any?) assume partially initialized
16472
16473 else
16474 return True;
16475 end if;
16476 end Is_Partially_Initialized_Type;
16477
16478 ------------------------------------
16479 -- Is_Potentially_Persistent_Type --
16480 ------------------------------------
16481
16482 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
16483 Comp : Entity_Id;
16484 Indx : Node_Id;
16485
16486 begin
16487 -- For private type, test corresponding full type
16488
16489 if Is_Private_Type (T) then
16490 return Is_Potentially_Persistent_Type (Full_View (T));
16491
16492 -- Scalar types are potentially persistent
16493
16494 elsif Is_Scalar_Type (T) then
16495 return True;
16496
16497 -- Record type is potentially persistent if not tagged and the types of
16498 -- all it components are potentially persistent, and no component has
16499 -- an initialization expression.
16500
16501 elsif Is_Record_Type (T)
16502 and then not Is_Tagged_Type (T)
16503 and then not Is_Partially_Initialized_Type (T)
16504 then
16505 Comp := First_Component (T);
16506 while Present (Comp) loop
16507 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
16508 return False;
16509 else
16510 Next_Entity (Comp);
16511 end if;
16512 end loop;
16513
16514 return True;
16515
16516 -- Array type is potentially persistent if its component type is
16517 -- potentially persistent and if all its constraints are static.
16518
16519 elsif Is_Array_Type (T) then
16520 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
16521 return False;
16522 end if;
16523
16524 Indx := First_Index (T);
16525 while Present (Indx) loop
16526 if not Is_OK_Static_Subtype (Etype (Indx)) then
16527 return False;
16528 else
16529 Next_Index (Indx);
16530 end if;
16531 end loop;
16532
16533 return True;
16534
16535 -- All other types are not potentially persistent
16536
16537 else
16538 return False;
16539 end if;
16540 end Is_Potentially_Persistent_Type;
16541
16542 --------------------------------
16543 -- Is_Potentially_Unevaluated --
16544 --------------------------------
16545
16546 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
16547 Par : Node_Id;
16548 Expr : Node_Id;
16549
16550 begin
16551 Expr := N;
16552 Par := N;
16553
16554 -- A postcondition whose expression is a short-circuit is broken down
16555 -- into individual aspects for better exception reporting. The original
16556 -- short-circuit expression is rewritten as the second operand, and an
16557 -- occurrence of 'Old in that operand is potentially unevaluated.
16558 -- See sem_ch13.adb for details of this transformation. The reference
16559 -- to 'Old may appear within an expression, so we must look for the
16560 -- enclosing pragma argument in the tree that contains the reference.
16561
16562 while Present (Par)
16563 and then Nkind (Par) /= N_Pragma_Argument_Association
16564 loop
16565 if Is_Rewrite_Substitution (Par)
16566 and then Nkind (Original_Node (Par)) = N_And_Then
16567 then
16568 return True;
16569 end if;
16570
16571 Par := Parent (Par);
16572 end loop;
16573
16574 -- Other cases; 'Old appears within other expression (not the top-level
16575 -- conjunct in a postcondition) with a potentially unevaluated operand.
16576
16577 Par := Parent (Expr);
16578 while not Nkind_In (Par, N_And_Then,
16579 N_Case_Expression,
16580 N_If_Expression,
16581 N_In,
16582 N_Not_In,
16583 N_Or_Else,
16584 N_Quantified_Expression)
16585 loop
16586 Expr := Par;
16587 Par := Parent (Par);
16588
16589 -- If the context is not an expression, or if is the result of
16590 -- expansion of an enclosing construct (such as another attribute)
16591 -- the predicate does not apply.
16592
16593 if Nkind (Par) = N_Case_Expression_Alternative then
16594 null;
16595
16596 elsif Nkind (Par) not in N_Subexpr
16597 or else not Comes_From_Source (Par)
16598 then
16599 return False;
16600 end if;
16601 end loop;
16602
16603 if Nkind (Par) = N_If_Expression then
16604 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
16605
16606 elsif Nkind (Par) = N_Case_Expression then
16607 return Expr /= Expression (Par);
16608
16609 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
16610 return Expr = Right_Opnd (Par);
16611
16612 elsif Nkind_In (Par, N_In, N_Not_In) then
16613
16614 -- If the membership includes several alternatives, only the first is
16615 -- definitely evaluated.
16616
16617 if Present (Alternatives (Par)) then
16618 return Expr /= First (Alternatives (Par));
16619
16620 -- If this is a range membership both bounds are evaluated
16621
16622 else
16623 return False;
16624 end if;
16625
16626 elsif Nkind (Par) = N_Quantified_Expression then
16627 return Expr = Condition (Par);
16628
16629 else
16630 return False;
16631 end if;
16632 end Is_Potentially_Unevaluated;
16633
16634 -----------------------------------------
16635 -- Is_Predefined_Dispatching_Operation --
16636 -----------------------------------------
16637
16638 function Is_Predefined_Dispatching_Operation
16639 (E : Entity_Id) return Boolean
16640 is
16641 TSS_Name : TSS_Name_Type;
16642
16643 begin
16644 if not Is_Dispatching_Operation (E) then
16645 return False;
16646 end if;
16647
16648 Get_Name_String (Chars (E));
16649
16650 -- Most predefined primitives have internally generated names. Equality
16651 -- must be treated differently; the predefined operation is recognized
16652 -- as a homogeneous binary operator that returns Boolean.
16653
16654 if Name_Len > TSS_Name_Type'Last then
16655 TSS_Name :=
16656 TSS_Name_Type
16657 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
16658
16659 if Nam_In (Chars (E), Name_uAssign, Name_uSize)
16660 or else
16661 (Chars (E) = Name_Op_Eq
16662 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
16663 or else TSS_Name = TSS_Deep_Adjust
16664 or else TSS_Name = TSS_Deep_Finalize
16665 or else TSS_Name = TSS_Stream_Input
16666 or else TSS_Name = TSS_Stream_Output
16667 or else TSS_Name = TSS_Stream_Read
16668 or else TSS_Name = TSS_Stream_Write
16669 or else Is_Predefined_Interface_Primitive (E)
16670 then
16671 return True;
16672 end if;
16673 end if;
16674
16675 return False;
16676 end Is_Predefined_Dispatching_Operation;
16677
16678 ---------------------------------------
16679 -- Is_Predefined_Interface_Primitive --
16680 ---------------------------------------
16681
16682 function Is_Predefined_Interface_Primitive (E : Entity_Id) return Boolean is
16683 begin
16684 -- In VM targets we don't restrict the functionality of this test to
16685 -- compiling in Ada 2005 mode since in VM targets any tagged type has
16686 -- these primitives.
16687
16688 return (Ada_Version >= Ada_2005 or else not Tagged_Type_Expansion)
16689 and then Nam_In (Chars (E), Name_uDisp_Asynchronous_Select,
16690 Name_uDisp_Conditional_Select,
16691 Name_uDisp_Get_Prim_Op_Kind,
16692 Name_uDisp_Get_Task_Id,
16693 Name_uDisp_Requeue,
16694 Name_uDisp_Timed_Select);
16695 end Is_Predefined_Interface_Primitive;
16696
16697 ---------------------------------------
16698 -- Is_Predefined_Internal_Operation --
16699 ---------------------------------------
16700
16701 function Is_Predefined_Internal_Operation
16702 (E : Entity_Id) return Boolean
16703 is
16704 TSS_Name : TSS_Name_Type;
16705
16706 begin
16707 if not Is_Dispatching_Operation (E) then
16708 return False;
16709 end if;
16710
16711 Get_Name_String (Chars (E));
16712
16713 -- Most predefined primitives have internally generated names. Equality
16714 -- must be treated differently; the predefined operation is recognized
16715 -- as a homogeneous binary operator that returns Boolean.
16716
16717 if Name_Len > TSS_Name_Type'Last then
16718 TSS_Name :=
16719 TSS_Name_Type
16720 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
16721
16722 if Nam_In (Chars (E), Name_uSize, Name_uAssign)
16723 or else
16724 (Chars (E) = Name_Op_Eq
16725 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
16726 or else TSS_Name = TSS_Deep_Adjust
16727 or else TSS_Name = TSS_Deep_Finalize
16728 or else Is_Predefined_Interface_Primitive (E)
16729 then
16730 return True;
16731 end if;
16732 end if;
16733
16734 return False;
16735 end Is_Predefined_Internal_Operation;
16736
16737 --------------------------------
16738 -- Is_Preelaborable_Aggregate --
16739 --------------------------------
16740
16741 function Is_Preelaborable_Aggregate (Aggr : Node_Id) return Boolean is
16742 Aggr_Typ : constant Entity_Id := Etype (Aggr);
16743 Array_Aggr : constant Boolean := Is_Array_Type (Aggr_Typ);
16744
16745 Anc_Part : Node_Id;
16746 Assoc : Node_Id;
16747 Choice : Node_Id;
16748 Comp_Typ : Entity_Id := Empty; -- init to avoid warning
16749 Expr : Node_Id;
16750
16751 begin
16752 if Array_Aggr then
16753 Comp_Typ := Component_Type (Aggr_Typ);
16754 end if;
16755
16756 -- Inspect the ancestor part
16757
16758 if Nkind (Aggr) = N_Extension_Aggregate then
16759 Anc_Part := Ancestor_Part (Aggr);
16760
16761 -- The ancestor denotes a subtype mark
16762
16763 if Is_Entity_Name (Anc_Part)
16764 and then Is_Type (Entity (Anc_Part))
16765 then
16766 if not Has_Preelaborable_Initialization (Entity (Anc_Part)) then
16767 return False;
16768 end if;
16769
16770 -- Otherwise the ancestor denotes an expression
16771
16772 elsif not Is_Preelaborable_Construct (Anc_Part) then
16773 return False;
16774 end if;
16775 end if;
16776
16777 -- Inspect the positional associations
16778
16779 Expr := First (Expressions (Aggr));
16780 while Present (Expr) loop
16781 if not Is_Preelaborable_Construct (Expr) then
16782 return False;
16783 end if;
16784
16785 Next (Expr);
16786 end loop;
16787
16788 -- Inspect the named associations
16789
16790 Assoc := First (Component_Associations (Aggr));
16791 while Present (Assoc) loop
16792
16793 -- Inspect the choices of the current named association
16794
16795 Choice := First (Choices (Assoc));
16796 while Present (Choice) loop
16797 if Array_Aggr then
16798
16799 -- For a choice to be preelaborable, it must denote either a
16800 -- static range or a static expression.
16801
16802 if Nkind (Choice) = N_Others_Choice then
16803 null;
16804
16805 elsif Nkind (Choice) = N_Range then
16806 if not Is_OK_Static_Range (Choice) then
16807 return False;
16808 end if;
16809
16810 elsif not Is_OK_Static_Expression (Choice) then
16811 return False;
16812 end if;
16813
16814 else
16815 Comp_Typ := Etype (Choice);
16816 end if;
16817
16818 Next (Choice);
16819 end loop;
16820
16821 -- The type of the choice must have preelaborable initialization if
16822 -- the association carries a <>.
16823
16824 pragma Assert (Present (Comp_Typ));
16825 if Box_Present (Assoc) then
16826 if not Has_Preelaborable_Initialization (Comp_Typ) then
16827 return False;
16828 end if;
16829
16830 -- The type of the expression must have preelaborable initialization
16831
16832 elsif not Is_Preelaborable_Construct (Expression (Assoc)) then
16833 return False;
16834 end if;
16835
16836 Next (Assoc);
16837 end loop;
16838
16839 -- At this point the aggregate is preelaborable
16840
16841 return True;
16842 end Is_Preelaborable_Aggregate;
16843
16844 --------------------------------
16845 -- Is_Preelaborable_Construct --
16846 --------------------------------
16847
16848 function Is_Preelaborable_Construct (N : Node_Id) return Boolean is
16849 begin
16850 -- Aggregates
16851
16852 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
16853 return Is_Preelaborable_Aggregate (N);
16854
16855 -- Attributes are allowed in general, even if their prefix is a formal
16856 -- type. It seems that certain attributes known not to be static might
16857 -- not be allowed, but there are no rules to prevent them.
16858
16859 elsif Nkind (N) = N_Attribute_Reference then
16860 return True;
16861
16862 -- Expressions
16863
16864 elsif Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
16865 return True;
16866
16867 elsif Nkind (N) = N_Qualified_Expression then
16868 return Is_Preelaborable_Construct (Expression (N));
16869
16870 -- Names are preelaborable when they denote a discriminant of an
16871 -- enclosing type. Discriminals are also considered for this check.
16872
16873 elsif Is_Entity_Name (N)
16874 and then Present (Entity (N))
16875 and then
16876 (Ekind (Entity (N)) = E_Discriminant
16877 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
16878 and then Present (Discriminal_Link (Entity (N)))))
16879 then
16880 return True;
16881
16882 -- Statements
16883
16884 elsif Nkind (N) = N_Null then
16885 return True;
16886
16887 -- Otherwise the construct is not preelaborable
16888
16889 else
16890 return False;
16891 end if;
16892 end Is_Preelaborable_Construct;
16893
16894 ---------------------------------
16895 -- Is_Protected_Self_Reference --
16896 ---------------------------------
16897
16898 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
16899
16900 function In_Access_Definition (N : Node_Id) return Boolean;
16901 -- Returns true if N belongs to an access definition
16902
16903 --------------------------
16904 -- In_Access_Definition --
16905 --------------------------
16906
16907 function In_Access_Definition (N : Node_Id) return Boolean is
16908 P : Node_Id;
16909
16910 begin
16911 P := Parent (N);
16912 while Present (P) loop
16913 if Nkind (P) = N_Access_Definition then
16914 return True;
16915 end if;
16916
16917 P := Parent (P);
16918 end loop;
16919
16920 return False;
16921 end In_Access_Definition;
16922
16923 -- Start of processing for Is_Protected_Self_Reference
16924
16925 begin
16926 -- Verify that prefix is analyzed and has the proper form. Note that
16927 -- the attributes Elab_Spec, Elab_Body, and Elab_Subp_Body, which also
16928 -- produce the address of an entity, do not analyze their prefix
16929 -- because they denote entities that are not necessarily visible.
16930 -- Neither of them can apply to a protected type.
16931
16932 return Ada_Version >= Ada_2005
16933 and then Is_Entity_Name (N)
16934 and then Present (Entity (N))
16935 and then Is_Protected_Type (Entity (N))
16936 and then In_Open_Scopes (Entity (N))
16937 and then not In_Access_Definition (N);
16938 end Is_Protected_Self_Reference;
16939
16940 -----------------------------
16941 -- Is_RCI_Pkg_Spec_Or_Body --
16942 -----------------------------
16943
16944 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
16945
16946 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
16947 -- Return True if the unit of Cunit is an RCI package declaration
16948
16949 ---------------------------
16950 -- Is_RCI_Pkg_Decl_Cunit --
16951 ---------------------------
16952
16953 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
16954 The_Unit : constant Node_Id := Unit (Cunit);
16955
16956 begin
16957 if Nkind (The_Unit) /= N_Package_Declaration then
16958 return False;
16959 end if;
16960
16961 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
16962 end Is_RCI_Pkg_Decl_Cunit;
16963
16964 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
16965
16966 begin
16967 return Is_RCI_Pkg_Decl_Cunit (Cunit)
16968 or else
16969 (Nkind (Unit (Cunit)) = N_Package_Body
16970 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
16971 end Is_RCI_Pkg_Spec_Or_Body;
16972
16973 -----------------------------------------
16974 -- Is_Remote_Access_To_Class_Wide_Type --
16975 -----------------------------------------
16976
16977 function Is_Remote_Access_To_Class_Wide_Type
16978 (E : Entity_Id) return Boolean
16979 is
16980 begin
16981 -- A remote access to class-wide type is a general access to object type
16982 -- declared in the visible part of a Remote_Types or Remote_Call_
16983 -- Interface unit.
16984
16985 return Ekind (E) = E_General_Access_Type
16986 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
16987 end Is_Remote_Access_To_Class_Wide_Type;
16988
16989 -----------------------------------------
16990 -- Is_Remote_Access_To_Subprogram_Type --
16991 -----------------------------------------
16992
16993 function Is_Remote_Access_To_Subprogram_Type
16994 (E : Entity_Id) return Boolean
16995 is
16996 begin
16997 return (Ekind (E) = E_Access_Subprogram_Type
16998 or else (Ekind (E) = E_Record_Type
16999 and then Present (Corresponding_Remote_Type (E))))
17000 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
17001 end Is_Remote_Access_To_Subprogram_Type;
17002
17003 --------------------
17004 -- Is_Remote_Call --
17005 --------------------
17006
17007 function Is_Remote_Call (N : Node_Id) return Boolean is
17008 begin
17009 if Nkind (N) not in N_Subprogram_Call then
17010
17011 -- An entry call cannot be remote
17012
17013 return False;
17014
17015 elsif Nkind (Name (N)) in N_Has_Entity
17016 and then Is_Remote_Call_Interface (Entity (Name (N)))
17017 then
17018 -- A subprogram declared in the spec of a RCI package is remote
17019
17020 return True;
17021
17022 elsif Nkind (Name (N)) = N_Explicit_Dereference
17023 and then Is_Remote_Access_To_Subprogram_Type
17024 (Etype (Prefix (Name (N))))
17025 then
17026 -- The dereference of a RAS is a remote call
17027
17028 return True;
17029
17030 elsif Present (Controlling_Argument (N))
17031 and then Is_Remote_Access_To_Class_Wide_Type
17032 (Etype (Controlling_Argument (N)))
17033 then
17034 -- Any primitive operation call with a controlling argument of
17035 -- a RACW type is a remote call.
17036
17037 return True;
17038 end if;
17039
17040 -- All other calls are local calls
17041
17042 return False;
17043 end Is_Remote_Call;
17044
17045 ----------------------
17046 -- Is_Renamed_Entry --
17047 ----------------------
17048
17049 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
17050 Orig_Node : Node_Id := Empty;
17051 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
17052
17053 function Is_Entry (Nam : Node_Id) return Boolean;
17054 -- Determine whether Nam is an entry. Traverse selectors if there are
17055 -- nested selected components.
17056
17057 --------------
17058 -- Is_Entry --
17059 --------------
17060
17061 function Is_Entry (Nam : Node_Id) return Boolean is
17062 begin
17063 if Nkind (Nam) = N_Selected_Component then
17064 return Is_Entry (Selector_Name (Nam));
17065 end if;
17066
17067 return Ekind (Entity (Nam)) = E_Entry;
17068 end Is_Entry;
17069
17070 -- Start of processing for Is_Renamed_Entry
17071
17072 begin
17073 if Present (Alias (Proc_Nam)) then
17074 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
17075 end if;
17076
17077 -- Look for a rewritten subprogram renaming declaration
17078
17079 if Nkind (Subp_Decl) = N_Subprogram_Declaration
17080 and then Present (Original_Node (Subp_Decl))
17081 then
17082 Orig_Node := Original_Node (Subp_Decl);
17083 end if;
17084
17085 -- The rewritten subprogram is actually an entry
17086
17087 if Present (Orig_Node)
17088 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
17089 and then Is_Entry (Name (Orig_Node))
17090 then
17091 return True;
17092 end if;
17093
17094 return False;
17095 end Is_Renamed_Entry;
17096
17097 -----------------------------
17098 -- Is_Renaming_Declaration --
17099 -----------------------------
17100
17101 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
17102 begin
17103 case Nkind (N) is
17104 when N_Exception_Renaming_Declaration
17105 | N_Generic_Function_Renaming_Declaration
17106 | N_Generic_Package_Renaming_Declaration
17107 | N_Generic_Procedure_Renaming_Declaration
17108 | N_Object_Renaming_Declaration
17109 | N_Package_Renaming_Declaration
17110 | N_Subprogram_Renaming_Declaration
17111 =>
17112 return True;
17113
17114 when others =>
17115 return False;
17116 end case;
17117 end Is_Renaming_Declaration;
17118
17119 ----------------------------
17120 -- Is_Reversible_Iterator --
17121 ----------------------------
17122
17123 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
17124 Ifaces_List : Elist_Id;
17125 Iface_Elmt : Elmt_Id;
17126 Iface : Entity_Id;
17127
17128 begin
17129 if Is_Class_Wide_Type (Typ)
17130 and then Chars (Root_Type (Typ)) = Name_Reversible_Iterator
17131 and then In_Predefined_Unit (Root_Type (Typ))
17132 then
17133 return True;
17134
17135 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
17136 return False;
17137
17138 else
17139 Collect_Interfaces (Typ, Ifaces_List);
17140
17141 Iface_Elmt := First_Elmt (Ifaces_List);
17142 while Present (Iface_Elmt) loop
17143 Iface := Node (Iface_Elmt);
17144 if Chars (Iface) = Name_Reversible_Iterator
17145 and then In_Predefined_Unit (Iface)
17146 then
17147 return True;
17148 end if;
17149
17150 Next_Elmt (Iface_Elmt);
17151 end loop;
17152 end if;
17153
17154 return False;
17155 end Is_Reversible_Iterator;
17156
17157 ----------------------
17158 -- Is_Selector_Name --
17159 ----------------------
17160
17161 function Is_Selector_Name (N : Node_Id) return Boolean is
17162 begin
17163 if not Is_List_Member (N) then
17164 declare
17165 P : constant Node_Id := Parent (N);
17166 begin
17167 return Nkind_In (P, N_Expanded_Name,
17168 N_Generic_Association,
17169 N_Parameter_Association,
17170 N_Selected_Component)
17171 and then Selector_Name (P) = N;
17172 end;
17173
17174 else
17175 declare
17176 L : constant List_Id := List_Containing (N);
17177 P : constant Node_Id := Parent (L);
17178 begin
17179 return (Nkind (P) = N_Discriminant_Association
17180 and then Selector_Names (P) = L)
17181 or else
17182 (Nkind (P) = N_Component_Association
17183 and then Choices (P) = L);
17184 end;
17185 end if;
17186 end Is_Selector_Name;
17187
17188 ---------------------------------
17189 -- Is_Single_Concurrent_Object --
17190 ---------------------------------
17191
17192 function Is_Single_Concurrent_Object (Id : Entity_Id) return Boolean is
17193 begin
17194 return
17195 Is_Single_Protected_Object (Id) or else Is_Single_Task_Object (Id);
17196 end Is_Single_Concurrent_Object;
17197
17198 -------------------------------
17199 -- Is_Single_Concurrent_Type --
17200 -------------------------------
17201
17202 function Is_Single_Concurrent_Type (Id : Entity_Id) return Boolean is
17203 begin
17204 return
17205 Ekind_In (Id, E_Protected_Type, E_Task_Type)
17206 and then Is_Single_Concurrent_Type_Declaration
17207 (Declaration_Node (Id));
17208 end Is_Single_Concurrent_Type;
17209
17210 -------------------------------------------
17211 -- Is_Single_Concurrent_Type_Declaration --
17212 -------------------------------------------
17213
17214 function Is_Single_Concurrent_Type_Declaration
17215 (N : Node_Id) return Boolean
17216 is
17217 begin
17218 return Nkind_In (Original_Node (N), N_Single_Protected_Declaration,
17219 N_Single_Task_Declaration);
17220 end Is_Single_Concurrent_Type_Declaration;
17221
17222 ---------------------------------------------
17223 -- Is_Single_Precision_Floating_Point_Type --
17224 ---------------------------------------------
17225
17226 function Is_Single_Precision_Floating_Point_Type
17227 (E : Entity_Id) return Boolean is
17228 begin
17229 return Is_Floating_Point_Type (E)
17230 and then Machine_Radix_Value (E) = Uint_2
17231 and then Machine_Mantissa_Value (E) = Uint_24
17232 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
17233 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
17234 end Is_Single_Precision_Floating_Point_Type;
17235
17236 --------------------------------
17237 -- Is_Single_Protected_Object --
17238 --------------------------------
17239
17240 function Is_Single_Protected_Object (Id : Entity_Id) return Boolean is
17241 begin
17242 return
17243 Ekind (Id) = E_Variable
17244 and then Ekind (Etype (Id)) = E_Protected_Type
17245 and then Is_Single_Concurrent_Type (Etype (Id));
17246 end Is_Single_Protected_Object;
17247
17248 ---------------------------
17249 -- Is_Single_Task_Object --
17250 ---------------------------
17251
17252 function Is_Single_Task_Object (Id : Entity_Id) return Boolean is
17253 begin
17254 return
17255 Ekind (Id) = E_Variable
17256 and then Ekind (Etype (Id)) = E_Task_Type
17257 and then Is_Single_Concurrent_Type (Etype (Id));
17258 end Is_Single_Task_Object;
17259
17260 -------------------------------------
17261 -- Is_SPARK_05_Initialization_Expr --
17262 -------------------------------------
17263
17264 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
17265 Is_Ok : Boolean;
17266 Expr : Node_Id;
17267 Comp_Assn : Node_Id;
17268 Orig_N : constant Node_Id := Original_Node (N);
17269
17270 begin
17271 Is_Ok := True;
17272
17273 if not Comes_From_Source (Orig_N) then
17274 goto Done;
17275 end if;
17276
17277 pragma Assert (Nkind (Orig_N) in N_Subexpr);
17278
17279 case Nkind (Orig_N) is
17280 when N_Character_Literal
17281 | N_Integer_Literal
17282 | N_Real_Literal
17283 | N_String_Literal
17284 =>
17285 null;
17286
17287 when N_Expanded_Name
17288 | N_Identifier
17289 =>
17290 if Is_Entity_Name (Orig_N)
17291 and then Present (Entity (Orig_N)) -- needed in some cases
17292 then
17293 case Ekind (Entity (Orig_N)) is
17294 when E_Constant
17295 | E_Enumeration_Literal
17296 | E_Named_Integer
17297 | E_Named_Real
17298 =>
17299 null;
17300
17301 when others =>
17302 if Is_Type (Entity (Orig_N)) then
17303 null;
17304 else
17305 Is_Ok := False;
17306 end if;
17307 end case;
17308 end if;
17309
17310 when N_Qualified_Expression
17311 | N_Type_Conversion
17312 =>
17313 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
17314
17315 when N_Unary_Op =>
17316 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
17317
17318 when N_Binary_Op
17319 | N_Membership_Test
17320 | N_Short_Circuit
17321 =>
17322 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
17323 and then
17324 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
17325
17326 when N_Aggregate
17327 | N_Extension_Aggregate
17328 =>
17329 if Nkind (Orig_N) = N_Extension_Aggregate then
17330 Is_Ok :=
17331 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
17332 end if;
17333
17334 Expr := First (Expressions (Orig_N));
17335 while Present (Expr) loop
17336 if not Is_SPARK_05_Initialization_Expr (Expr) then
17337 Is_Ok := False;
17338 goto Done;
17339 end if;
17340
17341 Next (Expr);
17342 end loop;
17343
17344 Comp_Assn := First (Component_Associations (Orig_N));
17345 while Present (Comp_Assn) loop
17346 Expr := Expression (Comp_Assn);
17347
17348 -- Note: test for Present here needed for box assocation
17349
17350 if Present (Expr)
17351 and then not Is_SPARK_05_Initialization_Expr (Expr)
17352 then
17353 Is_Ok := False;
17354 goto Done;
17355 end if;
17356
17357 Next (Comp_Assn);
17358 end loop;
17359
17360 when N_Attribute_Reference =>
17361 if Nkind (Prefix (Orig_N)) in N_Subexpr then
17362 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
17363 end if;
17364
17365 Expr := First (Expressions (Orig_N));
17366 while Present (Expr) loop
17367 if not Is_SPARK_05_Initialization_Expr (Expr) then
17368 Is_Ok := False;
17369 goto Done;
17370 end if;
17371
17372 Next (Expr);
17373 end loop;
17374
17375 -- Selected components might be expanded named not yet resolved, so
17376 -- default on the safe side. (Eg on sparklex.ads)
17377
17378 when N_Selected_Component =>
17379 null;
17380
17381 when others =>
17382 Is_Ok := False;
17383 end case;
17384
17385 <<Done>>
17386 return Is_Ok;
17387 end Is_SPARK_05_Initialization_Expr;
17388
17389 ----------------------------------
17390 -- Is_SPARK_05_Object_Reference --
17391 ----------------------------------
17392
17393 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
17394 begin
17395 if Is_Entity_Name (N) then
17396 return Present (Entity (N))
17397 and then
17398 (Ekind_In (Entity (N), E_Constant, E_Variable)
17399 or else Ekind (Entity (N)) in Formal_Kind);
17400
17401 else
17402 case Nkind (N) is
17403 when N_Selected_Component =>
17404 return Is_SPARK_05_Object_Reference (Prefix (N));
17405
17406 when others =>
17407 return False;
17408 end case;
17409 end if;
17410 end Is_SPARK_05_Object_Reference;
17411
17412 -----------------------------
17413 -- Is_Specific_Tagged_Type --
17414 -----------------------------
17415
17416 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
17417 Full_Typ : Entity_Id;
17418
17419 begin
17420 -- Handle private types
17421
17422 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
17423 Full_Typ := Full_View (Typ);
17424 else
17425 Full_Typ := Typ;
17426 end if;
17427
17428 -- A specific tagged type is a non-class-wide tagged type
17429
17430 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
17431 end Is_Specific_Tagged_Type;
17432
17433 ------------------
17434 -- Is_Statement --
17435 ------------------
17436
17437 function Is_Statement (N : Node_Id) return Boolean is
17438 begin
17439 return
17440 Nkind (N) in N_Statement_Other_Than_Procedure_Call
17441 or else Nkind (N) = N_Procedure_Call_Statement;
17442 end Is_Statement;
17443
17444 ---------------------------------------
17445 -- Is_Subprogram_Contract_Annotation --
17446 ---------------------------------------
17447
17448 function Is_Subprogram_Contract_Annotation
17449 (Item : Node_Id) return Boolean
17450 is
17451 Nam : Name_Id;
17452
17453 begin
17454 if Nkind (Item) = N_Aspect_Specification then
17455 Nam := Chars (Identifier (Item));
17456
17457 else pragma Assert (Nkind (Item) = N_Pragma);
17458 Nam := Pragma_Name (Item);
17459 end if;
17460
17461 return Nam = Name_Contract_Cases
17462 or else Nam = Name_Depends
17463 or else Nam = Name_Extensions_Visible
17464 or else Nam = Name_Global
17465 or else Nam = Name_Post
17466 or else Nam = Name_Post_Class
17467 or else Nam = Name_Postcondition
17468 or else Nam = Name_Pre
17469 or else Nam = Name_Pre_Class
17470 or else Nam = Name_Precondition
17471 or else Nam = Name_Refined_Depends
17472 or else Nam = Name_Refined_Global
17473 or else Nam = Name_Refined_Post
17474 or else Nam = Name_Test_Case;
17475 end Is_Subprogram_Contract_Annotation;
17476
17477 --------------------------------------------------
17478 -- Is_Subprogram_Stub_Without_Prior_Declaration --
17479 --------------------------------------------------
17480
17481 function Is_Subprogram_Stub_Without_Prior_Declaration
17482 (N : Node_Id) return Boolean
17483 is
17484 begin
17485 pragma Assert (Nkind (N) = N_Subprogram_Body_Stub);
17486
17487 case Ekind (Defining_Entity (N)) is
17488
17489 -- A subprogram stub without prior declaration serves as declaration
17490 -- for the actual subprogram body. As such, it has an attached
17491 -- defining entity of E_Function or E_Procedure.
17492
17493 when E_Function
17494 | E_Procedure
17495 =>
17496 return True;
17497
17498 -- Otherwise, it is completes a [generic] subprogram declaration
17499
17500 when E_Generic_Function
17501 | E_Generic_Procedure
17502 | E_Subprogram_Body
17503 =>
17504 return False;
17505
17506 when others =>
17507 raise Program_Error;
17508 end case;
17509 end Is_Subprogram_Stub_Without_Prior_Declaration;
17510
17511 ---------------------------
17512 -- Is_Suitable_Primitive --
17513 ---------------------------
17514
17515 function Is_Suitable_Primitive (Subp_Id : Entity_Id) return Boolean is
17516 begin
17517 -- The Default_Initial_Condition and invariant procedures must not be
17518 -- treated as primitive operations even when they apply to a tagged
17519 -- type. These routines must not act as targets of dispatching calls
17520 -- because they already utilize class-wide-precondition semantics to
17521 -- handle inheritance and overriding.
17522
17523 if Ekind (Subp_Id) = E_Procedure
17524 and then (Is_DIC_Procedure (Subp_Id)
17525 or else
17526 Is_Invariant_Procedure (Subp_Id))
17527 then
17528 return False;
17529 end if;
17530
17531 return True;
17532 end Is_Suitable_Primitive;
17533
17534 --------------------------
17535 -- Is_Suspension_Object --
17536 --------------------------
17537
17538 function Is_Suspension_Object (Id : Entity_Id) return Boolean is
17539 begin
17540 -- This approach does an exact name match rather than to rely on
17541 -- RTSfind. Routine Is_Effectively_Volatile is used by clients of the
17542 -- front end at point where all auxiliary tables are locked and any
17543 -- modifications to them are treated as violations. Do not tamper with
17544 -- the tables, instead examine the Chars fields of all the scopes of Id.
17545
17546 return
17547 Chars (Id) = Name_Suspension_Object
17548 and then Present (Scope (Id))
17549 and then Chars (Scope (Id)) = Name_Synchronous_Task_Control
17550 and then Present (Scope (Scope (Id)))
17551 and then Chars (Scope (Scope (Id))) = Name_Ada
17552 and then Present (Scope (Scope (Scope (Id))))
17553 and then Scope (Scope (Scope (Id))) = Standard_Standard;
17554 end Is_Suspension_Object;
17555
17556 ----------------------------
17557 -- Is_Synchronized_Object --
17558 ----------------------------
17559
17560 function Is_Synchronized_Object (Id : Entity_Id) return Boolean is
17561 Prag : Node_Id;
17562
17563 begin
17564 if Is_Object (Id) then
17565
17566 -- The object is synchronized if it is of a type that yields a
17567 -- synchronized object.
17568
17569 if Yields_Synchronized_Object (Etype (Id)) then
17570 return True;
17571
17572 -- The object is synchronized if it is atomic and Async_Writers is
17573 -- enabled.
17574
17575 elsif Is_Atomic_Object_Entity (Id)
17576 and then Async_Writers_Enabled (Id)
17577 then
17578 return True;
17579
17580 -- A constant is a synchronized object by default
17581
17582 elsif Ekind (Id) = E_Constant then
17583 return True;
17584
17585 -- A variable is a synchronized object if it is subject to pragma
17586 -- Constant_After_Elaboration.
17587
17588 elsif Ekind (Id) = E_Variable then
17589 Prag := Get_Pragma (Id, Pragma_Constant_After_Elaboration);
17590
17591 return Present (Prag) and then Is_Enabled_Pragma (Prag);
17592 end if;
17593 end if;
17594
17595 -- Otherwise the input is not an object or it does not qualify as a
17596 -- synchronized object.
17597
17598 return False;
17599 end Is_Synchronized_Object;
17600
17601 ---------------------------------
17602 -- Is_Synchronized_Tagged_Type --
17603 ---------------------------------
17604
17605 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
17606 Kind : constant Entity_Kind := Ekind (Base_Type (E));
17607
17608 begin
17609 -- A task or protected type derived from an interface is a tagged type.
17610 -- Such a tagged type is called a synchronized tagged type, as are
17611 -- synchronized interfaces and private extensions whose declaration
17612 -- includes the reserved word synchronized.
17613
17614 return (Is_Tagged_Type (E)
17615 and then (Kind = E_Task_Type
17616 or else
17617 Kind = E_Protected_Type))
17618 or else
17619 (Is_Interface (E)
17620 and then Is_Synchronized_Interface (E))
17621 or else
17622 (Ekind (E) = E_Record_Type_With_Private
17623 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
17624 and then (Synchronized_Present (Parent (E))
17625 or else Is_Synchronized_Interface (Etype (E))));
17626 end Is_Synchronized_Tagged_Type;
17627
17628 -----------------
17629 -- Is_Transfer --
17630 -----------------
17631
17632 function Is_Transfer (N : Node_Id) return Boolean is
17633 Kind : constant Node_Kind := Nkind (N);
17634
17635 begin
17636 if Kind = N_Simple_Return_Statement
17637 or else
17638 Kind = N_Extended_Return_Statement
17639 or else
17640 Kind = N_Goto_Statement
17641 or else
17642 Kind = N_Raise_Statement
17643 or else
17644 Kind = N_Requeue_Statement
17645 then
17646 return True;
17647
17648 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
17649 and then No (Condition (N))
17650 then
17651 return True;
17652
17653 elsif Kind = N_Procedure_Call_Statement
17654 and then Is_Entity_Name (Name (N))
17655 and then Present (Entity (Name (N)))
17656 and then No_Return (Entity (Name (N)))
17657 then
17658 return True;
17659
17660 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
17661 return True;
17662
17663 else
17664 return False;
17665 end if;
17666 end Is_Transfer;
17667
17668 -------------
17669 -- Is_True --
17670 -------------
17671
17672 function Is_True (U : Uint) return Boolean is
17673 begin
17674 return (U /= 0);
17675 end Is_True;
17676
17677 --------------------------------------
17678 -- Is_Unchecked_Conversion_Instance --
17679 --------------------------------------
17680
17681 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
17682 Par : Node_Id;
17683
17684 begin
17685 -- Look for a function whose generic parent is the predefined intrinsic
17686 -- function Unchecked_Conversion, or for one that renames such an
17687 -- instance.
17688
17689 if Ekind (Id) = E_Function then
17690 Par := Parent (Id);
17691
17692 if Nkind (Par) = N_Function_Specification then
17693 Par := Generic_Parent (Par);
17694
17695 if Present (Par) then
17696 return
17697 Chars (Par) = Name_Unchecked_Conversion
17698 and then Is_Intrinsic_Subprogram (Par)
17699 and then In_Predefined_Unit (Par);
17700 else
17701 return
17702 Present (Alias (Id))
17703 and then Is_Unchecked_Conversion_Instance (Alias (Id));
17704 end if;
17705 end if;
17706 end if;
17707
17708 return False;
17709 end Is_Unchecked_Conversion_Instance;
17710
17711 -------------------------------
17712 -- Is_Universal_Numeric_Type --
17713 -------------------------------
17714
17715 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
17716 begin
17717 return T = Universal_Integer or else T = Universal_Real;
17718 end Is_Universal_Numeric_Type;
17719
17720 ------------------------------
17721 -- Is_User_Defined_Equality --
17722 ------------------------------
17723
17724 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean is
17725 begin
17726 return Ekind (Id) = E_Function
17727 and then Chars (Id) = Name_Op_Eq
17728 and then Comes_From_Source (Id)
17729
17730 -- Internally generated equalities have a full type declaration
17731 -- as their parent.
17732
17733 and then Nkind (Parent (Id)) = N_Function_Specification;
17734 end Is_User_Defined_Equality;
17735
17736 --------------------------------------
17737 -- Is_Validation_Variable_Reference --
17738 --------------------------------------
17739
17740 function Is_Validation_Variable_Reference (N : Node_Id) return Boolean is
17741 Var : constant Node_Id := Unqual_Conv (N);
17742 Var_Id : Entity_Id;
17743
17744 begin
17745 Var_Id := Empty;
17746
17747 if Is_Entity_Name (Var) then
17748 Var_Id := Entity (Var);
17749 end if;
17750
17751 return
17752 Present (Var_Id)
17753 and then Ekind (Var_Id) = E_Variable
17754 and then Present (Validated_Object (Var_Id));
17755 end Is_Validation_Variable_Reference;
17756
17757 ----------------------------
17758 -- Is_Variable_Size_Array --
17759 ----------------------------
17760
17761 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
17762 Idx : Node_Id;
17763
17764 begin
17765 pragma Assert (Is_Array_Type (E));
17766
17767 -- Check if some index is initialized with a non-constant value
17768
17769 Idx := First_Index (E);
17770 while Present (Idx) loop
17771 if Nkind (Idx) = N_Range then
17772 if not Is_Constant_Bound (Low_Bound (Idx))
17773 or else not Is_Constant_Bound (High_Bound (Idx))
17774 then
17775 return True;
17776 end if;
17777 end if;
17778
17779 Idx := Next_Index (Idx);
17780 end loop;
17781
17782 return False;
17783 end Is_Variable_Size_Array;
17784
17785 -----------------------------
17786 -- Is_Variable_Size_Record --
17787 -----------------------------
17788
17789 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
17790 Comp : Entity_Id;
17791 Comp_Typ : Entity_Id;
17792
17793 begin
17794 pragma Assert (Is_Record_Type (E));
17795
17796 Comp := First_Component (E);
17797 while Present (Comp) loop
17798 Comp_Typ := Underlying_Type (Etype (Comp));
17799
17800 -- Recursive call if the record type has discriminants
17801
17802 if Is_Record_Type (Comp_Typ)
17803 and then Has_Discriminants (Comp_Typ)
17804 and then Is_Variable_Size_Record (Comp_Typ)
17805 then
17806 return True;
17807
17808 elsif Is_Array_Type (Comp_Typ)
17809 and then Is_Variable_Size_Array (Comp_Typ)
17810 then
17811 return True;
17812 end if;
17813
17814 Next_Component (Comp);
17815 end loop;
17816
17817 return False;
17818 end Is_Variable_Size_Record;
17819
17820 -----------------
17821 -- Is_Variable --
17822 -----------------
17823
17824 function Is_Variable
17825 (N : Node_Id;
17826 Use_Original_Node : Boolean := True) return Boolean
17827 is
17828 Orig_Node : Node_Id;
17829
17830 function In_Protected_Function (E : Entity_Id) return Boolean;
17831 -- Within a protected function, the private components of the enclosing
17832 -- protected type are constants. A function nested within a (protected)
17833 -- procedure is not itself protected. Within the body of a protected
17834 -- function the current instance of the protected type is a constant.
17835
17836 function Is_Variable_Prefix (P : Node_Id) return Boolean;
17837 -- Prefixes can involve implicit dereferences, in which case we must
17838 -- test for the case of a reference of a constant access type, which can
17839 -- can never be a variable.
17840
17841 ---------------------------
17842 -- In_Protected_Function --
17843 ---------------------------
17844
17845 function In_Protected_Function (E : Entity_Id) return Boolean is
17846 Prot : Entity_Id;
17847 S : Entity_Id;
17848
17849 begin
17850 -- E is the current instance of a type
17851
17852 if Is_Type (E) then
17853 Prot := E;
17854
17855 -- E is an object
17856
17857 else
17858 Prot := Scope (E);
17859 end if;
17860
17861 if not Is_Protected_Type (Prot) then
17862 return False;
17863
17864 else
17865 S := Current_Scope;
17866 while Present (S) and then S /= Prot loop
17867 if Ekind (S) = E_Function and then Scope (S) = Prot then
17868 return True;
17869 end if;
17870
17871 S := Scope (S);
17872 end loop;
17873
17874 return False;
17875 end if;
17876 end In_Protected_Function;
17877
17878 ------------------------
17879 -- Is_Variable_Prefix --
17880 ------------------------
17881
17882 function Is_Variable_Prefix (P : Node_Id) return Boolean is
17883 begin
17884 if Is_Access_Type (Etype (P)) then
17885 return not Is_Access_Constant (Root_Type (Etype (P)));
17886
17887 -- For the case of an indexed component whose prefix has a packed
17888 -- array type, the prefix has been rewritten into a type conversion.
17889 -- Determine variable-ness from the converted expression.
17890
17891 elsif Nkind (P) = N_Type_Conversion
17892 and then not Comes_From_Source (P)
17893 and then Is_Array_Type (Etype (P))
17894 and then Is_Packed (Etype (P))
17895 then
17896 return Is_Variable (Expression (P));
17897
17898 else
17899 return Is_Variable (P);
17900 end if;
17901 end Is_Variable_Prefix;
17902
17903 -- Start of processing for Is_Variable
17904
17905 begin
17906 -- Special check, allow x'Deref(expr) as a variable
17907
17908 if Nkind (N) = N_Attribute_Reference
17909 and then Attribute_Name (N) = Name_Deref
17910 then
17911 return True;
17912 end if;
17913
17914 -- Check if we perform the test on the original node since this may be a
17915 -- test of syntactic categories which must not be disturbed by whatever
17916 -- rewriting might have occurred. For example, an aggregate, which is
17917 -- certainly NOT a variable, could be turned into a variable by
17918 -- expansion.
17919
17920 if Use_Original_Node then
17921 Orig_Node := Original_Node (N);
17922 else
17923 Orig_Node := N;
17924 end if;
17925
17926 -- Definitely OK if Assignment_OK is set. Since this is something that
17927 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
17928
17929 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
17930 return True;
17931
17932 -- Normally we go to the original node, but there is one exception where
17933 -- we use the rewritten node, namely when it is an explicit dereference.
17934 -- The generated code may rewrite a prefix which is an access type with
17935 -- an explicit dereference. The dereference is a variable, even though
17936 -- the original node may not be (since it could be a constant of the
17937 -- access type).
17938
17939 -- In Ada 2005 we have a further case to consider: the prefix may be a
17940 -- function call given in prefix notation. The original node appears to
17941 -- be a selected component, but we need to examine the call.
17942
17943 elsif Nkind (N) = N_Explicit_Dereference
17944 and then Nkind (Orig_Node) /= N_Explicit_Dereference
17945 and then Present (Etype (Orig_Node))
17946 and then Is_Access_Type (Etype (Orig_Node))
17947 then
17948 -- Note that if the prefix is an explicit dereference that does not
17949 -- come from source, we must check for a rewritten function call in
17950 -- prefixed notation before other forms of rewriting, to prevent a
17951 -- compiler crash.
17952
17953 return
17954 (Nkind (Orig_Node) = N_Function_Call
17955 and then not Is_Access_Constant (Etype (Prefix (N))))
17956 or else
17957 Is_Variable_Prefix (Original_Node (Prefix (N)));
17958
17959 -- in Ada 2012, the dereference may have been added for a type with
17960 -- a declared implicit dereference aspect. Check that it is not an
17961 -- access to constant.
17962
17963 elsif Nkind (N) = N_Explicit_Dereference
17964 and then Present (Etype (Orig_Node))
17965 and then Ada_Version >= Ada_2012
17966 and then Has_Implicit_Dereference (Etype (Orig_Node))
17967 then
17968 return not Is_Access_Constant (Etype (Prefix (N)));
17969
17970 -- A function call is never a variable
17971
17972 elsif Nkind (N) = N_Function_Call then
17973 return False;
17974
17975 -- All remaining checks use the original node
17976
17977 elsif Is_Entity_Name (Orig_Node)
17978 and then Present (Entity (Orig_Node))
17979 then
17980 declare
17981 E : constant Entity_Id := Entity (Orig_Node);
17982 K : constant Entity_Kind := Ekind (E);
17983
17984 begin
17985 if Is_Loop_Parameter (E) then
17986 return False;
17987 end if;
17988
17989 return (K = E_Variable
17990 and then Nkind (Parent (E)) /= N_Exception_Handler)
17991 or else (K = E_Component
17992 and then not In_Protected_Function (E))
17993 or else K = E_Out_Parameter
17994 or else K = E_In_Out_Parameter
17995 or else K = E_Generic_In_Out_Parameter
17996
17997 -- Current instance of type. If this is a protected type, check
17998 -- we are not within the body of one of its protected functions.
17999
18000 or else (Is_Type (E)
18001 and then In_Open_Scopes (E)
18002 and then not In_Protected_Function (E))
18003
18004 or else (Is_Incomplete_Or_Private_Type (E)
18005 and then In_Open_Scopes (Full_View (E)));
18006 end;
18007
18008 else
18009 case Nkind (Orig_Node) is
18010 when N_Indexed_Component
18011 | N_Slice
18012 =>
18013 return Is_Variable_Prefix (Prefix (Orig_Node));
18014
18015 when N_Selected_Component =>
18016 return (Is_Variable (Selector_Name (Orig_Node))
18017 and then Is_Variable_Prefix (Prefix (Orig_Node)))
18018 or else
18019 (Nkind (N) = N_Expanded_Name
18020 and then Scope (Entity (N)) = Entity (Prefix (N)));
18021
18022 -- For an explicit dereference, the type of the prefix cannot
18023 -- be an access to constant or an access to subprogram.
18024
18025 when N_Explicit_Dereference =>
18026 declare
18027 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
18028 begin
18029 return Is_Access_Type (Typ)
18030 and then not Is_Access_Constant (Root_Type (Typ))
18031 and then Ekind (Typ) /= E_Access_Subprogram_Type;
18032 end;
18033
18034 -- The type conversion is the case where we do not deal with the
18035 -- context dependent special case of an actual parameter. Thus
18036 -- the type conversion is only considered a variable for the
18037 -- purposes of this routine if the target type is tagged. However,
18038 -- a type conversion is considered to be a variable if it does not
18039 -- come from source (this deals for example with the conversions
18040 -- of expressions to their actual subtypes).
18041
18042 when N_Type_Conversion =>
18043 return Is_Variable (Expression (Orig_Node))
18044 and then
18045 (not Comes_From_Source (Orig_Node)
18046 or else
18047 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
18048 and then
18049 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
18050
18051 -- GNAT allows an unchecked type conversion as a variable. This
18052 -- only affects the generation of internal expanded code, since
18053 -- calls to instantiations of Unchecked_Conversion are never
18054 -- considered variables (since they are function calls).
18055
18056 when N_Unchecked_Type_Conversion =>
18057 return Is_Variable (Expression (Orig_Node));
18058
18059 when others =>
18060 return False;
18061 end case;
18062 end if;
18063 end Is_Variable;
18064
18065 ---------------------------
18066 -- Is_Visibly_Controlled --
18067 ---------------------------
18068
18069 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
18070 Root : constant Entity_Id := Root_Type (T);
18071 begin
18072 return Chars (Scope (Root)) = Name_Finalization
18073 and then Chars (Scope (Scope (Root))) = Name_Ada
18074 and then Scope (Scope (Scope (Root))) = Standard_Standard;
18075 end Is_Visibly_Controlled;
18076
18077 --------------------------
18078 -- Is_Volatile_Function --
18079 --------------------------
18080
18081 function Is_Volatile_Function (Func_Id : Entity_Id) return Boolean is
18082 begin
18083 pragma Assert (Ekind_In (Func_Id, E_Function, E_Generic_Function));
18084
18085 -- A function declared within a protected type is volatile
18086
18087 if Is_Protected_Type (Scope (Func_Id)) then
18088 return True;
18089
18090 -- An instance of Ada.Unchecked_Conversion is a volatile function if
18091 -- either the source or the target are effectively volatile.
18092
18093 elsif Is_Unchecked_Conversion_Instance (Func_Id)
18094 and then Has_Effectively_Volatile_Profile (Func_Id)
18095 then
18096 return True;
18097
18098 -- Otherwise the function is treated as volatile if it is subject to
18099 -- enabled pragma Volatile_Function.
18100
18101 else
18102 return
18103 Is_Enabled_Pragma (Get_Pragma (Func_Id, Pragma_Volatile_Function));
18104 end if;
18105 end Is_Volatile_Function;
18106
18107 ------------------------
18108 -- Is_Volatile_Object --
18109 ------------------------
18110
18111 function Is_Volatile_Object (N : Node_Id) return Boolean is
18112 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
18113 -- If prefix is an implicit dereference, examine designated type
18114
18115 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
18116 -- Determines if given object has volatile components
18117
18118 ------------------------
18119 -- Is_Volatile_Prefix --
18120 ------------------------
18121
18122 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
18123 Typ : constant Entity_Id := Etype (N);
18124
18125 begin
18126 if Is_Access_Type (Typ) then
18127 declare
18128 Dtyp : constant Entity_Id := Designated_Type (Typ);
18129
18130 begin
18131 return Is_Volatile (Dtyp)
18132 or else Has_Volatile_Components (Dtyp);
18133 end;
18134
18135 else
18136 return Object_Has_Volatile_Components (N);
18137 end if;
18138 end Is_Volatile_Prefix;
18139
18140 ------------------------------------
18141 -- Object_Has_Volatile_Components --
18142 ------------------------------------
18143
18144 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
18145 Typ : constant Entity_Id := Etype (N);
18146
18147 begin
18148 if Is_Volatile (Typ)
18149 or else Has_Volatile_Components (Typ)
18150 then
18151 return True;
18152
18153 elsif Is_Entity_Name (N)
18154 and then (Has_Volatile_Components (Entity (N))
18155 or else Is_Volatile (Entity (N)))
18156 then
18157 return True;
18158
18159 elsif Nkind (N) = N_Indexed_Component
18160 or else Nkind (N) = N_Selected_Component
18161 then
18162 return Is_Volatile_Prefix (Prefix (N));
18163
18164 else
18165 return False;
18166 end if;
18167 end Object_Has_Volatile_Components;
18168
18169 -- Start of processing for Is_Volatile_Object
18170
18171 begin
18172 if Nkind (N) = N_Defining_Identifier then
18173 return Is_Volatile (N) or else Is_Volatile (Etype (N));
18174
18175 elsif Nkind (N) = N_Expanded_Name then
18176 return Is_Volatile_Object (Entity (N));
18177
18178 elsif Is_Volatile (Etype (N))
18179 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
18180 then
18181 return True;
18182
18183 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
18184 and then Is_Volatile_Prefix (Prefix (N))
18185 then
18186 return True;
18187
18188 elsif Nkind (N) = N_Selected_Component
18189 and then Is_Volatile (Entity (Selector_Name (N)))
18190 then
18191 return True;
18192
18193 else
18194 return False;
18195 end if;
18196 end Is_Volatile_Object;
18197
18198 -----------------------------
18199 -- Iterate_Call_Parameters --
18200 -----------------------------
18201
18202 procedure Iterate_Call_Parameters (Call : Node_Id) is
18203 Actual : Node_Id := First_Actual (Call);
18204 Formal : Entity_Id := First_Formal (Get_Called_Entity (Call));
18205
18206 begin
18207 while Present (Formal) and then Present (Actual) loop
18208 Handle_Parameter (Formal, Actual);
18209
18210 Next_Formal (Formal);
18211 Next_Actual (Actual);
18212 end loop;
18213
18214 pragma Assert (No (Formal));
18215 pragma Assert (No (Actual));
18216 end Iterate_Call_Parameters;
18217
18218 ---------------------------
18219 -- Itype_Has_Declaration --
18220 ---------------------------
18221
18222 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
18223 begin
18224 pragma Assert (Is_Itype (Id));
18225 return Present (Parent (Id))
18226 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
18227 N_Subtype_Declaration)
18228 and then Defining_Entity (Parent (Id)) = Id;
18229 end Itype_Has_Declaration;
18230
18231 -------------------------
18232 -- Kill_Current_Values --
18233 -------------------------
18234
18235 procedure Kill_Current_Values
18236 (Ent : Entity_Id;
18237 Last_Assignment_Only : Boolean := False)
18238 is
18239 begin
18240 if Is_Assignable (Ent) then
18241 Set_Last_Assignment (Ent, Empty);
18242 end if;
18243
18244 if Is_Object (Ent) then
18245 if not Last_Assignment_Only then
18246 Kill_Checks (Ent);
18247 Set_Current_Value (Ent, Empty);
18248
18249 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
18250 -- for a constant. Once the constant is elaborated, its value is
18251 -- not changed, therefore the associated flags that describe the
18252 -- value should not be modified either.
18253
18254 if Ekind (Ent) = E_Constant then
18255 null;
18256
18257 -- Non-constant entities
18258
18259 else
18260 if not Can_Never_Be_Null (Ent) then
18261 Set_Is_Known_Non_Null (Ent, False);
18262 end if;
18263
18264 Set_Is_Known_Null (Ent, False);
18265
18266 -- Reset the Is_Known_Valid flag unless the type is always
18267 -- valid. This does not apply to a loop parameter because its
18268 -- bounds are defined by the loop header and therefore always
18269 -- valid.
18270
18271 if not Is_Known_Valid (Etype (Ent))
18272 and then Ekind (Ent) /= E_Loop_Parameter
18273 then
18274 Set_Is_Known_Valid (Ent, False);
18275 end if;
18276 end if;
18277 end if;
18278 end if;
18279 end Kill_Current_Values;
18280
18281 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
18282 S : Entity_Id;
18283
18284 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
18285 -- Clear current value for entity E and all entities chained to E
18286
18287 ------------------------------------------
18288 -- Kill_Current_Values_For_Entity_Chain --
18289 ------------------------------------------
18290
18291 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
18292 Ent : Entity_Id;
18293 begin
18294 Ent := E;
18295 while Present (Ent) loop
18296 Kill_Current_Values (Ent, Last_Assignment_Only);
18297 Next_Entity (Ent);
18298 end loop;
18299 end Kill_Current_Values_For_Entity_Chain;
18300
18301 -- Start of processing for Kill_Current_Values
18302
18303 begin
18304 -- Kill all saved checks, a special case of killing saved values
18305
18306 if not Last_Assignment_Only then
18307 Kill_All_Checks;
18308 end if;
18309
18310 -- Loop through relevant scopes, which includes the current scope and
18311 -- any parent scopes if the current scope is a block or a package.
18312
18313 S := Current_Scope;
18314 Scope_Loop : loop
18315
18316 -- Clear current values of all entities in current scope
18317
18318 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
18319
18320 -- If scope is a package, also clear current values of all private
18321 -- entities in the scope.
18322
18323 if Is_Package_Or_Generic_Package (S)
18324 or else Is_Concurrent_Type (S)
18325 then
18326 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
18327 end if;
18328
18329 -- If this is a not a subprogram, deal with parents
18330
18331 if not Is_Subprogram (S) then
18332 S := Scope (S);
18333 exit Scope_Loop when S = Standard_Standard;
18334 else
18335 exit Scope_Loop;
18336 end if;
18337 end loop Scope_Loop;
18338 end Kill_Current_Values;
18339
18340 --------------------------
18341 -- Kill_Size_Check_Code --
18342 --------------------------
18343
18344 procedure Kill_Size_Check_Code (E : Entity_Id) is
18345 begin
18346 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
18347 and then Present (Size_Check_Code (E))
18348 then
18349 Remove (Size_Check_Code (E));
18350 Set_Size_Check_Code (E, Empty);
18351 end if;
18352 end Kill_Size_Check_Code;
18353
18354 --------------------
18355 -- Known_Non_Null --
18356 --------------------
18357
18358 function Known_Non_Null (N : Node_Id) return Boolean is
18359 Status : constant Null_Status_Kind := Null_Status (N);
18360
18361 Id : Entity_Id;
18362 Op : Node_Kind;
18363 Val : Node_Id;
18364
18365 begin
18366 -- The expression yields a non-null value ignoring simple flow analysis
18367
18368 if Status = Is_Non_Null then
18369 return True;
18370
18371 -- Otherwise check whether N is a reference to an entity that appears
18372 -- within a conditional construct.
18373
18374 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18375
18376 -- First check if we are in decisive conditional
18377
18378 Get_Current_Value_Condition (N, Op, Val);
18379
18380 if Known_Null (Val) then
18381 if Op = N_Op_Eq then
18382 return False;
18383 elsif Op = N_Op_Ne then
18384 return True;
18385 end if;
18386 end if;
18387
18388 -- If OK to do replacement, test Is_Known_Non_Null flag
18389
18390 Id := Entity (N);
18391
18392 if OK_To_Do_Constant_Replacement (Id) then
18393 return Is_Known_Non_Null (Id);
18394 end if;
18395 end if;
18396
18397 -- Otherwise it is not possible to determine whether N yields a non-null
18398 -- value.
18399
18400 return False;
18401 end Known_Non_Null;
18402
18403 ----------------
18404 -- Known_Null --
18405 ----------------
18406
18407 function Known_Null (N : Node_Id) return Boolean is
18408 Status : constant Null_Status_Kind := Null_Status (N);
18409
18410 Id : Entity_Id;
18411 Op : Node_Kind;
18412 Val : Node_Id;
18413
18414 begin
18415 -- The expression yields a null value ignoring simple flow analysis
18416
18417 if Status = Is_Null then
18418 return True;
18419
18420 -- Otherwise check whether N is a reference to an entity that appears
18421 -- within a conditional construct.
18422
18423 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18424
18425 -- First check if we are in decisive conditional
18426
18427 Get_Current_Value_Condition (N, Op, Val);
18428
18429 if Known_Null (Val) then
18430 if Op = N_Op_Eq then
18431 return True;
18432 elsif Op = N_Op_Ne then
18433 return False;
18434 end if;
18435 end if;
18436
18437 -- If OK to do replacement, test Is_Known_Null flag
18438
18439 Id := Entity (N);
18440
18441 if OK_To_Do_Constant_Replacement (Id) then
18442 return Is_Known_Null (Id);
18443 end if;
18444 end if;
18445
18446 -- Otherwise it is not possible to determine whether N yields a null
18447 -- value.
18448
18449 return False;
18450 end Known_Null;
18451
18452 --------------------------
18453 -- Known_To_Be_Assigned --
18454 --------------------------
18455
18456 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
18457 P : constant Node_Id := Parent (N);
18458
18459 begin
18460 case Nkind (P) is
18461
18462 -- Test left side of assignment
18463
18464 when N_Assignment_Statement =>
18465 return N = Name (P);
18466
18467 -- Function call arguments are never lvalues
18468
18469 when N_Function_Call =>
18470 return False;
18471
18472 -- Positional parameter for procedure or accept call
18473
18474 when N_Accept_Statement
18475 | N_Procedure_Call_Statement
18476 =>
18477 declare
18478 Proc : Entity_Id;
18479 Form : Entity_Id;
18480 Act : Node_Id;
18481
18482 begin
18483 Proc := Get_Subprogram_Entity (P);
18484
18485 if No (Proc) then
18486 return False;
18487 end if;
18488
18489 -- If we are not a list member, something is strange, so
18490 -- be conservative and return False.
18491
18492 if not Is_List_Member (N) then
18493 return False;
18494 end if;
18495
18496 -- We are going to find the right formal by stepping forward
18497 -- through the formals, as we step backwards in the actuals.
18498
18499 Form := First_Formal (Proc);
18500 Act := N;
18501 loop
18502 -- If no formal, something is weird, so be conservative
18503 -- and return False.
18504
18505 if No (Form) then
18506 return False;
18507 end if;
18508
18509 Prev (Act);
18510 exit when No (Act);
18511 Next_Formal (Form);
18512 end loop;
18513
18514 return Ekind (Form) /= E_In_Parameter;
18515 end;
18516
18517 -- Named parameter for procedure or accept call
18518
18519 when N_Parameter_Association =>
18520 declare
18521 Proc : Entity_Id;
18522 Form : Entity_Id;
18523
18524 begin
18525 Proc := Get_Subprogram_Entity (Parent (P));
18526
18527 if No (Proc) then
18528 return False;
18529 end if;
18530
18531 -- Loop through formals to find the one that matches
18532
18533 Form := First_Formal (Proc);
18534 loop
18535 -- If no matching formal, that's peculiar, some kind of
18536 -- previous error, so return False to be conservative.
18537 -- Actually this also happens in legal code in the case
18538 -- where P is a parameter association for an Extra_Formal???
18539
18540 if No (Form) then
18541 return False;
18542 end if;
18543
18544 -- Else test for match
18545
18546 if Chars (Form) = Chars (Selector_Name (P)) then
18547 return Ekind (Form) /= E_In_Parameter;
18548 end if;
18549
18550 Next_Formal (Form);
18551 end loop;
18552 end;
18553
18554 -- Test for appearing in a conversion that itself appears
18555 -- in an lvalue context, since this should be an lvalue.
18556
18557 when N_Type_Conversion =>
18558 return Known_To_Be_Assigned (P);
18559
18560 -- All other references are definitely not known to be modifications
18561
18562 when others =>
18563 return False;
18564 end case;
18565 end Known_To_Be_Assigned;
18566
18567 ---------------------------
18568 -- Last_Source_Statement --
18569 ---------------------------
18570
18571 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
18572 N : Node_Id;
18573
18574 begin
18575 N := Last (Statements (HSS));
18576 while Present (N) loop
18577 exit when Comes_From_Source (N);
18578 Prev (N);
18579 end loop;
18580
18581 return N;
18582 end Last_Source_Statement;
18583
18584 -----------------------
18585 -- Mark_Coextensions --
18586 -----------------------
18587
18588 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
18589 Is_Dynamic : Boolean;
18590 -- Indicates whether the context causes nested coextensions to be
18591 -- dynamic or static
18592
18593 function Mark_Allocator (N : Node_Id) return Traverse_Result;
18594 -- Recognize an allocator node and label it as a dynamic coextension
18595
18596 --------------------
18597 -- Mark_Allocator --
18598 --------------------
18599
18600 function Mark_Allocator (N : Node_Id) return Traverse_Result is
18601 begin
18602 if Nkind (N) = N_Allocator then
18603 if Is_Dynamic then
18604 Set_Is_Static_Coextension (N, False);
18605 Set_Is_Dynamic_Coextension (N);
18606
18607 -- If the allocator expression is potentially dynamic, it may
18608 -- be expanded out of order and require dynamic allocation
18609 -- anyway, so we treat the coextension itself as dynamic.
18610 -- Potential optimization ???
18611
18612 elsif Nkind (Expression (N)) = N_Qualified_Expression
18613 and then Nkind (Expression (Expression (N))) = N_Op_Concat
18614 then
18615 Set_Is_Static_Coextension (N, False);
18616 Set_Is_Dynamic_Coextension (N);
18617 else
18618 Set_Is_Dynamic_Coextension (N, False);
18619 Set_Is_Static_Coextension (N);
18620 end if;
18621 end if;
18622
18623 return OK;
18624 end Mark_Allocator;
18625
18626 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
18627
18628 -- Start of processing for Mark_Coextensions
18629
18630 begin
18631 -- An allocator that appears on the right-hand side of an assignment is
18632 -- treated as a potentially dynamic coextension when the right-hand side
18633 -- is an allocator or a qualified expression.
18634
18635 -- Obj := new ...'(new Coextension ...);
18636
18637 if Nkind (Context_Nod) = N_Assignment_Statement then
18638 Is_Dynamic :=
18639 Nkind_In (Expression (Context_Nod), N_Allocator,
18640 N_Qualified_Expression);
18641
18642 -- An allocator that appears within the expression of a simple return
18643 -- statement is treated as a potentially dynamic coextension when the
18644 -- expression is either aggregate, allocator, or qualified expression.
18645
18646 -- return (new Coextension ...);
18647 -- return new ...'(new Coextension ...);
18648
18649 elsif Nkind (Context_Nod) = N_Simple_Return_Statement then
18650 Is_Dynamic :=
18651 Nkind_In (Expression (Context_Nod), N_Aggregate,
18652 N_Allocator,
18653 N_Qualified_Expression);
18654
18655 -- An alloctor that appears within the initialization expression of an
18656 -- object declaration is considered a potentially dynamic coextension
18657 -- when the initialization expression is an allocator or a qualified
18658 -- expression.
18659
18660 -- Obj : ... := new ...'(new Coextension ...);
18661
18662 -- A similar case arises when the object declaration is part of an
18663 -- extended return statement.
18664
18665 -- return Obj : ... := new ...'(new Coextension ...);
18666 -- return Obj : ... := (new Coextension ...);
18667
18668 elsif Nkind (Context_Nod) = N_Object_Declaration then
18669 Is_Dynamic :=
18670 Nkind_In (Root_Nod, N_Allocator, N_Qualified_Expression)
18671 or else
18672 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
18673
18674 -- This routine should not be called with constructs that cannot contain
18675 -- coextensions.
18676
18677 else
18678 raise Program_Error;
18679 end if;
18680
18681 Mark_Allocators (Root_Nod);
18682 end Mark_Coextensions;
18683
18684 ---------------------------------
18685 -- Mark_Elaboration_Attributes --
18686 ---------------------------------
18687
18688 procedure Mark_Elaboration_Attributes
18689 (N_Id : Node_Or_Entity_Id;
18690 Checks : Boolean := False;
18691 Level : Boolean := False;
18692 Modes : Boolean := False;
18693 Warnings : Boolean := False)
18694 is
18695 function Elaboration_Checks_OK
18696 (Target_Id : Entity_Id;
18697 Context_Id : Entity_Id) return Boolean;
18698 -- Determine whether elaboration checks are enabled for target Target_Id
18699 -- which resides within context Context_Id.
18700
18701 procedure Mark_Elaboration_Attributes_Id (Id : Entity_Id);
18702 -- Preserve relevant attributes of the context in arbitrary entity Id
18703
18704 procedure Mark_Elaboration_Attributes_Node (N : Node_Id);
18705 -- Preserve relevant attributes of the context in arbitrary node N
18706
18707 ---------------------------
18708 -- Elaboration_Checks_OK --
18709 ---------------------------
18710
18711 function Elaboration_Checks_OK
18712 (Target_Id : Entity_Id;
18713 Context_Id : Entity_Id) return Boolean
18714 is
18715 Encl_Scop : Entity_Id;
18716
18717 begin
18718 -- Elaboration checks are suppressed for the target
18719
18720 if Elaboration_Checks_Suppressed (Target_Id) then
18721 return False;
18722 end if;
18723
18724 -- Otherwise elaboration checks are OK for the target, but may be
18725 -- suppressed for the context where the target is declared.
18726
18727 Encl_Scop := Context_Id;
18728 while Present (Encl_Scop) and then Encl_Scop /= Standard_Standard loop
18729 if Elaboration_Checks_Suppressed (Encl_Scop) then
18730 return False;
18731 end if;
18732
18733 Encl_Scop := Scope (Encl_Scop);
18734 end loop;
18735
18736 -- Neither the target nor its declarative context have elaboration
18737 -- checks suppressed.
18738
18739 return True;
18740 end Elaboration_Checks_OK;
18741
18742 ------------------------------------
18743 -- Mark_Elaboration_Attributes_Id --
18744 ------------------------------------
18745
18746 procedure Mark_Elaboration_Attributes_Id (Id : Entity_Id) is
18747 begin
18748 -- Mark the status of elaboration checks in effect. Do not reset the
18749 -- status in case the entity is reanalyzed with checks suppressed.
18750
18751 if Checks and then not Is_Elaboration_Checks_OK_Id (Id) then
18752 Set_Is_Elaboration_Checks_OK_Id (Id,
18753 Elaboration_Checks_OK
18754 (Target_Id => Id,
18755 Context_Id => Scope (Id)));
18756 end if;
18757
18758 -- Mark the status of elaboration warnings in effect. Do not reset
18759 -- the status in case the entity is reanalyzed with warnings off.
18760
18761 if Warnings and then not Is_Elaboration_Warnings_OK_Id (Id) then
18762 Set_Is_Elaboration_Warnings_OK_Id (Id, Elab_Warnings);
18763 end if;
18764 end Mark_Elaboration_Attributes_Id;
18765
18766 --------------------------------------
18767 -- Mark_Elaboration_Attributes_Node --
18768 --------------------------------------
18769
18770 procedure Mark_Elaboration_Attributes_Node (N : Node_Id) is
18771 function Extract_Name (N : Node_Id) return Node_Id;
18772 -- Obtain the Name attribute of call or instantiation N
18773
18774 ------------------
18775 -- Extract_Name --
18776 ------------------
18777
18778 function Extract_Name (N : Node_Id) return Node_Id is
18779 Nam : Node_Id;
18780
18781 begin
18782 Nam := Name (N);
18783
18784 -- A call to an entry family appears in indexed form
18785
18786 if Nkind (Nam) = N_Indexed_Component then
18787 Nam := Prefix (Nam);
18788 end if;
18789
18790 -- The name may also appear in qualified form
18791
18792 if Nkind (Nam) = N_Selected_Component then
18793 Nam := Selector_Name (Nam);
18794 end if;
18795
18796 return Nam;
18797 end Extract_Name;
18798
18799 -- Local variables
18800
18801 Context_Id : Entity_Id;
18802 Nam : Node_Id;
18803
18804 -- Start of processing for Mark_Elaboration_Attributes_Node
18805
18806 begin
18807 -- Mark the status of elaboration checks in effect. Do not reset the
18808 -- status in case the node is reanalyzed with checks suppressed.
18809
18810 if Checks and then not Is_Elaboration_Checks_OK_Node (N) then
18811
18812 -- Assignments, attribute references, and variable references do
18813 -- not have a "declarative" context.
18814
18815 Context_Id := Empty;
18816
18817 -- The status of elaboration checks for calls and instantiations
18818 -- depends on the most recent pragma Suppress/Unsuppress, as well
18819 -- as the suppression status of the context where the target is
18820 -- defined.
18821
18822 -- package Pack is
18823 -- function Func ...;
18824 -- end Pack;
18825
18826 -- with Pack;
18827 -- procedure Main is
18828 -- pragma Suppress (Elaboration_Checks, Pack);
18829 -- X : ... := Pack.Func;
18830 -- ...
18831
18832 -- In the example above, the call to Func has elaboration checks
18833 -- enabled because there is no active general purpose suppression
18834 -- pragma, however the elaboration checks of Pack are explicitly
18835 -- suppressed. As a result the elaboration checks of the call must
18836 -- be disabled in order to preserve this dependency.
18837
18838 if Nkind_In (N, N_Entry_Call_Statement,
18839 N_Function_Call,
18840 N_Function_Instantiation,
18841 N_Package_Instantiation,
18842 N_Procedure_Call_Statement,
18843 N_Procedure_Instantiation)
18844 then
18845 Nam := Extract_Name (N);
18846
18847 if Is_Entity_Name (Nam) and then Present (Entity (Nam)) then
18848 Context_Id := Scope (Entity (Nam));
18849 end if;
18850 end if;
18851
18852 Set_Is_Elaboration_Checks_OK_Node (N,
18853 Elaboration_Checks_OK
18854 (Target_Id => Empty,
18855 Context_Id => Context_Id));
18856 end if;
18857
18858 -- Mark the enclosing level of the node. Do not reset the status in
18859 -- case the node is relocated and reanalyzed.
18860
18861 if Level and then not Is_Declaration_Level_Node (N) then
18862 Set_Is_Declaration_Level_Node (N,
18863 Find_Enclosing_Level (N) = Declaration_Level);
18864 end if;
18865
18866 -- Mark the Ghost and SPARK mode in effect
18867
18868 if Modes then
18869 if Ghost_Mode = Ignore then
18870 Set_Is_Ignored_Ghost_Node (N);
18871 end if;
18872
18873 if SPARK_Mode = On then
18874 Set_Is_SPARK_Mode_On_Node (N);
18875 end if;
18876 end if;
18877
18878 -- Mark the status of elaboration warnings in effect. Do not reset
18879 -- the status in case the node is reanalyzed with warnings off.
18880
18881 if Warnings and then not Is_Elaboration_Warnings_OK_Node (N) then
18882 Set_Is_Elaboration_Warnings_OK_Node (N, Elab_Warnings);
18883 end if;
18884 end Mark_Elaboration_Attributes_Node;
18885
18886 -- Start of processing for Mark_Elaboration_Attributes
18887
18888 begin
18889 -- Do not capture any elaboration-related attributes when switch -gnatH
18890 -- (legacy elaboration checking mode enabled) is in effect because the
18891 -- attributes are useless to the legacy model.
18892
18893 if Legacy_Elaboration_Checks then
18894 return;
18895 end if;
18896
18897 if Nkind (N_Id) in N_Entity then
18898 Mark_Elaboration_Attributes_Id (N_Id);
18899 else
18900 Mark_Elaboration_Attributes_Node (N_Id);
18901 end if;
18902 end Mark_Elaboration_Attributes;
18903
18904 ----------------------------------------
18905 -- Mark_Save_Invocation_Graph_Of_Body --
18906 ----------------------------------------
18907
18908 procedure Mark_Save_Invocation_Graph_Of_Body is
18909 Main : constant Node_Id := Cunit (Main_Unit);
18910 Main_Unit : constant Node_Id := Unit (Main);
18911 Aux_Id : Entity_Id;
18912
18913 begin
18914 Set_Save_Invocation_Graph_Of_Body (Main);
18915
18916 -- Assume that the main unit does not have a complimentary unit
18917
18918 Aux_Id := Empty;
18919
18920 -- Obtain the complimentary unit of the main unit
18921
18922 if Nkind_In (Main_Unit, N_Generic_Package_Declaration,
18923 N_Generic_Subprogram_Declaration,
18924 N_Package_Declaration,
18925 N_Subprogram_Declaration)
18926 then
18927 Aux_Id := Corresponding_Body (Main_Unit);
18928
18929 elsif Nkind_In (Main_Unit, N_Package_Body,
18930 N_Subprogram_Body,
18931 N_Subprogram_Renaming_Declaration)
18932 then
18933 Aux_Id := Corresponding_Spec (Main_Unit);
18934 end if;
18935
18936 if Present (Aux_Id) then
18937 Set_Save_Invocation_Graph_Of_Body
18938 (Parent (Unit_Declaration_Node (Aux_Id)));
18939 end if;
18940 end Mark_Save_Invocation_Graph_Of_Body;
18941
18942 ----------------------------------
18943 -- Matching_Static_Array_Bounds --
18944 ----------------------------------
18945
18946 function Matching_Static_Array_Bounds
18947 (L_Typ : Node_Id;
18948 R_Typ : Node_Id) return Boolean
18949 is
18950 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
18951 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
18952
18953 L_Index : Node_Id := Empty; -- init to ...
18954 R_Index : Node_Id := Empty; -- ...avoid warnings
18955 L_Low : Node_Id;
18956 L_High : Node_Id;
18957 L_Len : Uint;
18958 R_Low : Node_Id;
18959 R_High : Node_Id;
18960 R_Len : Uint;
18961
18962 begin
18963 if L_Ndims /= R_Ndims then
18964 return False;
18965 end if;
18966
18967 -- Unconstrained types do not have static bounds
18968
18969 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
18970 return False;
18971 end if;
18972
18973 -- First treat specially the first dimension, as the lower bound and
18974 -- length of string literals are not stored like those of arrays.
18975
18976 if Ekind (L_Typ) = E_String_Literal_Subtype then
18977 L_Low := String_Literal_Low_Bound (L_Typ);
18978 L_Len := String_Literal_Length (L_Typ);
18979 else
18980 L_Index := First_Index (L_Typ);
18981 Get_Index_Bounds (L_Index, L_Low, L_High);
18982
18983 if Is_OK_Static_Expression (L_Low)
18984 and then
18985 Is_OK_Static_Expression (L_High)
18986 then
18987 if Expr_Value (L_High) < Expr_Value (L_Low) then
18988 L_Len := Uint_0;
18989 else
18990 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
18991 end if;
18992 else
18993 return False;
18994 end if;
18995 end if;
18996
18997 if Ekind (R_Typ) = E_String_Literal_Subtype then
18998 R_Low := String_Literal_Low_Bound (R_Typ);
18999 R_Len := String_Literal_Length (R_Typ);
19000 else
19001 R_Index := First_Index (R_Typ);
19002 Get_Index_Bounds (R_Index, R_Low, R_High);
19003
19004 if Is_OK_Static_Expression (R_Low)
19005 and then
19006 Is_OK_Static_Expression (R_High)
19007 then
19008 if Expr_Value (R_High) < Expr_Value (R_Low) then
19009 R_Len := Uint_0;
19010 else
19011 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
19012 end if;
19013 else
19014 return False;
19015 end if;
19016 end if;
19017
19018 if (Is_OK_Static_Expression (L_Low)
19019 and then
19020 Is_OK_Static_Expression (R_Low))
19021 and then Expr_Value (L_Low) = Expr_Value (R_Low)
19022 and then L_Len = R_Len
19023 then
19024 null;
19025 else
19026 return False;
19027 end if;
19028
19029 -- Then treat all other dimensions
19030
19031 for Indx in 2 .. L_Ndims loop
19032 Next (L_Index);
19033 Next (R_Index);
19034
19035 Get_Index_Bounds (L_Index, L_Low, L_High);
19036 Get_Index_Bounds (R_Index, R_Low, R_High);
19037
19038 if (Is_OK_Static_Expression (L_Low) and then
19039 Is_OK_Static_Expression (L_High) and then
19040 Is_OK_Static_Expression (R_Low) and then
19041 Is_OK_Static_Expression (R_High))
19042 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
19043 and then
19044 Expr_Value (L_High) = Expr_Value (R_High))
19045 then
19046 null;
19047 else
19048 return False;
19049 end if;
19050 end loop;
19051
19052 -- If we fall through the loop, all indexes matched
19053
19054 return True;
19055 end Matching_Static_Array_Bounds;
19056
19057 -------------------
19058 -- May_Be_Lvalue --
19059 -------------------
19060
19061 function May_Be_Lvalue (N : Node_Id) return Boolean is
19062 P : constant Node_Id := Parent (N);
19063
19064 begin
19065 case Nkind (P) is
19066
19067 -- Test left side of assignment
19068
19069 when N_Assignment_Statement =>
19070 return N = Name (P);
19071
19072 -- Test prefix of component or attribute. Note that the prefix of an
19073 -- explicit or implicit dereference cannot be an l-value. In the case
19074 -- of a 'Read attribute, the reference can be an actual in the
19075 -- argument list of the attribute.
19076
19077 when N_Attribute_Reference =>
19078 return (N = Prefix (P)
19079 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P)))
19080 or else
19081 Attribute_Name (P) = Name_Read;
19082
19083 -- For an expanded name, the name is an lvalue if the expanded name
19084 -- is an lvalue, but the prefix is never an lvalue, since it is just
19085 -- the scope where the name is found.
19086
19087 when N_Expanded_Name =>
19088 if N = Prefix (P) then
19089 return May_Be_Lvalue (P);
19090 else
19091 return False;
19092 end if;
19093
19094 -- For a selected component A.B, A is certainly an lvalue if A.B is.
19095 -- B is a little interesting, if we have A.B := 3, there is some
19096 -- discussion as to whether B is an lvalue or not, we choose to say
19097 -- it is. Note however that A is not an lvalue if it is of an access
19098 -- type since this is an implicit dereference.
19099
19100 when N_Selected_Component =>
19101 if N = Prefix (P)
19102 and then Present (Etype (N))
19103 and then Is_Access_Type (Etype (N))
19104 then
19105 return False;
19106 else
19107 return May_Be_Lvalue (P);
19108 end if;
19109
19110 -- For an indexed component or slice, the index or slice bounds is
19111 -- never an lvalue. The prefix is an lvalue if the indexed component
19112 -- or slice is an lvalue, except if it is an access type, where we
19113 -- have an implicit dereference.
19114
19115 when N_Indexed_Component
19116 | N_Slice
19117 =>
19118 if N /= Prefix (P)
19119 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
19120 then
19121 return False;
19122 else
19123 return May_Be_Lvalue (P);
19124 end if;
19125
19126 -- Prefix of a reference is an lvalue if the reference is an lvalue
19127
19128 when N_Reference =>
19129 return May_Be_Lvalue (P);
19130
19131 -- Prefix of explicit dereference is never an lvalue
19132
19133 when N_Explicit_Dereference =>
19134 return False;
19135
19136 -- Positional parameter for subprogram, entry, or accept call.
19137 -- In older versions of Ada function call arguments are never
19138 -- lvalues. In Ada 2012 functions can have in-out parameters.
19139
19140 when N_Accept_Statement
19141 | N_Entry_Call_Statement
19142 | N_Subprogram_Call
19143 =>
19144 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
19145 return False;
19146 end if;
19147
19148 -- The following mechanism is clumsy and fragile. A single flag
19149 -- set in Resolve_Actuals would be preferable ???
19150
19151 declare
19152 Proc : Entity_Id;
19153 Form : Entity_Id;
19154 Act : Node_Id;
19155
19156 begin
19157 Proc := Get_Subprogram_Entity (P);
19158
19159 if No (Proc) then
19160 return True;
19161 end if;
19162
19163 -- If we are not a list member, something is strange, so be
19164 -- conservative and return True.
19165
19166 if not Is_List_Member (N) then
19167 return True;
19168 end if;
19169
19170 -- We are going to find the right formal by stepping forward
19171 -- through the formals, as we step backwards in the actuals.
19172
19173 Form := First_Formal (Proc);
19174 Act := N;
19175 loop
19176 -- If no formal, something is weird, so be conservative and
19177 -- return True.
19178
19179 if No (Form) then
19180 return True;
19181 end if;
19182
19183 Prev (Act);
19184 exit when No (Act);
19185 Next_Formal (Form);
19186 end loop;
19187
19188 return Ekind (Form) /= E_In_Parameter;
19189 end;
19190
19191 -- Named parameter for procedure or accept call
19192
19193 when N_Parameter_Association =>
19194 declare
19195 Proc : Entity_Id;
19196 Form : Entity_Id;
19197
19198 begin
19199 Proc := Get_Subprogram_Entity (Parent (P));
19200
19201 if No (Proc) then
19202 return True;
19203 end if;
19204
19205 -- Loop through formals to find the one that matches
19206
19207 Form := First_Formal (Proc);
19208 loop
19209 -- If no matching formal, that's peculiar, some kind of
19210 -- previous error, so return True to be conservative.
19211 -- Actually happens with legal code for an unresolved call
19212 -- where we may get the wrong homonym???
19213
19214 if No (Form) then
19215 return True;
19216 end if;
19217
19218 -- Else test for match
19219
19220 if Chars (Form) = Chars (Selector_Name (P)) then
19221 return Ekind (Form) /= E_In_Parameter;
19222 end if;
19223
19224 Next_Formal (Form);
19225 end loop;
19226 end;
19227
19228 -- Test for appearing in a conversion that itself appears in an
19229 -- lvalue context, since this should be an lvalue.
19230
19231 when N_Type_Conversion =>
19232 return May_Be_Lvalue (P);
19233
19234 -- Test for appearance in object renaming declaration
19235
19236 when N_Object_Renaming_Declaration =>
19237 return True;
19238
19239 -- All other references are definitely not lvalues
19240
19241 when others =>
19242 return False;
19243 end case;
19244 end May_Be_Lvalue;
19245
19246 -----------------
19247 -- Might_Raise --
19248 -----------------
19249
19250 function Might_Raise (N : Node_Id) return Boolean is
19251 Result : Boolean := False;
19252
19253 function Process (N : Node_Id) return Traverse_Result;
19254 -- Set Result to True if we find something that could raise an exception
19255
19256 -------------
19257 -- Process --
19258 -------------
19259
19260 function Process (N : Node_Id) return Traverse_Result is
19261 begin
19262 if Nkind_In (N, N_Procedure_Call_Statement,
19263 N_Function_Call,
19264 N_Raise_Statement,
19265 N_Raise_Constraint_Error,
19266 N_Raise_Program_Error,
19267 N_Raise_Storage_Error)
19268 then
19269 Result := True;
19270 return Abandon;
19271 else
19272 return OK;
19273 end if;
19274 end Process;
19275
19276 procedure Set_Result is new Traverse_Proc (Process);
19277
19278 -- Start of processing for Might_Raise
19279
19280 begin
19281 -- False if exceptions can't be propagated
19282
19283 if No_Exception_Handlers_Set then
19284 return False;
19285 end if;
19286
19287 -- If the checks handled by the back end are not disabled, we cannot
19288 -- ensure that no exception will be raised.
19289
19290 if not Access_Checks_Suppressed (Empty)
19291 or else not Discriminant_Checks_Suppressed (Empty)
19292 or else not Range_Checks_Suppressed (Empty)
19293 or else not Index_Checks_Suppressed (Empty)
19294 or else Opt.Stack_Checking_Enabled
19295 then
19296 return True;
19297 end if;
19298
19299 Set_Result (N);
19300 return Result;
19301 end Might_Raise;
19302
19303 --------------------------------
19304 -- Nearest_Enclosing_Instance --
19305 --------------------------------
19306
19307 function Nearest_Enclosing_Instance (E : Entity_Id) return Entity_Id is
19308 Inst : Entity_Id;
19309
19310 begin
19311 Inst := Scope (E);
19312 while Present (Inst) and then Inst /= Standard_Standard loop
19313 if Is_Generic_Instance (Inst) then
19314 return Inst;
19315 end if;
19316
19317 Inst := Scope (Inst);
19318 end loop;
19319
19320 return Empty;
19321 end Nearest_Enclosing_Instance;
19322
19323 ----------------------
19324 -- Needs_One_Actual --
19325 ----------------------
19326
19327 function Needs_One_Actual (E : Entity_Id) return Boolean is
19328 Formal : Entity_Id;
19329
19330 begin
19331 -- Ada 2005 or later, and formals present. The first formal must be
19332 -- of a type that supports prefix notation: a controlling argument,
19333 -- a class-wide type, or an access to such.
19334
19335 if Ada_Version >= Ada_2005
19336 and then Present (First_Formal (E))
19337 and then No (Default_Value (First_Formal (E)))
19338 and then
19339 (Is_Controlling_Formal (First_Formal (E))
19340 or else Is_Class_Wide_Type (Etype (First_Formal (E)))
19341 or else Is_Anonymous_Access_Type (Etype (First_Formal (E))))
19342 then
19343 Formal := Next_Formal (First_Formal (E));
19344 while Present (Formal) loop
19345 if No (Default_Value (Formal)) then
19346 return False;
19347 end if;
19348
19349 Next_Formal (Formal);
19350 end loop;
19351
19352 return True;
19353
19354 -- Ada 83/95 or no formals
19355
19356 else
19357 return False;
19358 end if;
19359 end Needs_One_Actual;
19360
19361 ---------------------------------
19362 -- Needs_Simple_Initialization --
19363 ---------------------------------
19364
19365 function Needs_Simple_Initialization
19366 (Typ : Entity_Id;
19367 Consider_IS : Boolean := True) return Boolean
19368 is
19369 Consider_IS_NS : constant Boolean :=
19370 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
19371
19372 begin
19373 -- Never need initialization if it is suppressed
19374
19375 if Initialization_Suppressed (Typ) then
19376 return False;
19377 end if;
19378
19379 -- Check for private type, in which case test applies to the underlying
19380 -- type of the private type.
19381
19382 if Is_Private_Type (Typ) then
19383 declare
19384 RT : constant Entity_Id := Underlying_Type (Typ);
19385 begin
19386 if Present (RT) then
19387 return Needs_Simple_Initialization (RT);
19388 else
19389 return False;
19390 end if;
19391 end;
19392
19393 -- Scalar type with Default_Value aspect requires initialization
19394
19395 elsif Is_Scalar_Type (Typ) and then Has_Default_Aspect (Typ) then
19396 return True;
19397
19398 -- Cases needing simple initialization are access types, and, if pragma
19399 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
19400 -- types.
19401
19402 elsif Is_Access_Type (Typ)
19403 or else (Consider_IS_NS and then (Is_Scalar_Type (Typ)))
19404 then
19405 return True;
19406
19407 -- If Initialize/Normalize_Scalars is in effect, string objects also
19408 -- need initialization, unless they are created in the course of
19409 -- expanding an aggregate (since in the latter case they will be
19410 -- filled with appropriate initializing values before they are used).
19411
19412 elsif Consider_IS_NS
19413 and then Is_Standard_String_Type (Typ)
19414 and then
19415 (not Is_Itype (Typ)
19416 or else Nkind (Associated_Node_For_Itype (Typ)) /= N_Aggregate)
19417 then
19418 return True;
19419
19420 else
19421 return False;
19422 end if;
19423 end Needs_Simple_Initialization;
19424
19425 -------------------------------------
19426 -- Needs_Variable_Reference_Marker --
19427 -------------------------------------
19428
19429 function Needs_Variable_Reference_Marker
19430 (N : Node_Id;
19431 Calls_OK : Boolean) return Boolean
19432 is
19433 function Within_Suitable_Context (Ref : Node_Id) return Boolean;
19434 -- Deteremine whether variable reference Ref appears within a suitable
19435 -- context that allows the creation of a marker.
19436
19437 -----------------------------
19438 -- Within_Suitable_Context --
19439 -----------------------------
19440
19441 function Within_Suitable_Context (Ref : Node_Id) return Boolean is
19442 Par : Node_Id;
19443
19444 begin
19445 Par := Ref;
19446 while Present (Par) loop
19447
19448 -- The context is not suitable when the reference appears within
19449 -- the formal part of an instantiation which acts as compilation
19450 -- unit because there is no proper list for the insertion of the
19451 -- marker.
19452
19453 if Nkind (Par) = N_Generic_Association
19454 and then Nkind (Parent (Par)) in N_Generic_Instantiation
19455 and then Nkind (Parent (Parent (Par))) = N_Compilation_Unit
19456 then
19457 return False;
19458
19459 -- The context is not suitable when the reference appears within
19460 -- a pragma. If the pragma has run-time semantics, the reference
19461 -- will be reconsidered once the pragma is expanded.
19462
19463 elsif Nkind (Par) = N_Pragma then
19464 return False;
19465
19466 -- The context is not suitable when the reference appears within a
19467 -- subprogram call, and the caller requests this behavior.
19468
19469 elsif not Calls_OK
19470 and then Nkind_In (Par, N_Entry_Call_Statement,
19471 N_Function_Call,
19472 N_Procedure_Call_Statement)
19473 then
19474 return False;
19475
19476 -- Prevent the search from going too far
19477
19478 elsif Is_Body_Or_Package_Declaration (Par) then
19479 exit;
19480 end if;
19481
19482 Par := Parent (Par);
19483 end loop;
19484
19485 return True;
19486 end Within_Suitable_Context;
19487
19488 -- Local variables
19489
19490 Prag : Node_Id;
19491 Var_Id : Entity_Id;
19492
19493 -- Start of processing for Needs_Variable_Reference_Marker
19494
19495 begin
19496 -- No marker needs to be created when switch -gnatH (legacy elaboration
19497 -- checking mode enabled) is in effect because the legacy ABE mechanism
19498 -- does not use markers.
19499
19500 if Legacy_Elaboration_Checks then
19501 return False;
19502
19503 -- No marker needs to be created for ASIS because ABE diagnostics and
19504 -- checks are not performed in this mode.
19505
19506 elsif ASIS_Mode then
19507 return False;
19508
19509 -- No marker needs to be created when the reference is preanalyzed
19510 -- because the marker will be inserted in the wrong place.
19511
19512 elsif Preanalysis_Active then
19513 return False;
19514
19515 -- Only references warrant a marker
19516
19517 elsif not Nkind_In (N, N_Expanded_Name, N_Identifier) then
19518 return False;
19519
19520 -- Only source references warrant a marker
19521
19522 elsif not Comes_From_Source (N) then
19523 return False;
19524
19525 -- No marker needs to be created when the reference is erroneous, left
19526 -- in a bad state, or does not denote a variable.
19527
19528 elsif not (Present (Entity (N))
19529 and then Ekind (Entity (N)) = E_Variable
19530 and then Entity (N) /= Any_Id)
19531 then
19532 return False;
19533 end if;
19534
19535 Var_Id := Entity (N);
19536 Prag := SPARK_Pragma (Var_Id);
19537
19538 -- Both the variable and reference must appear in SPARK_Mode On regions
19539 -- because this elaboration scenario falls under the SPARK rules.
19540
19541 if not (Comes_From_Source (Var_Id)
19542 and then Present (Prag)
19543 and then Get_SPARK_Mode_From_Annotation (Prag) = On
19544 and then Is_SPARK_Mode_On_Node (N))
19545 then
19546 return False;
19547
19548 -- No marker needs to be created when the reference does not appear
19549 -- within a suitable context (see body for details).
19550
19551 -- Performance note: parent traversal
19552
19553 elsif not Within_Suitable_Context (N) then
19554 return False;
19555 end if;
19556
19557 -- At this point it is known that the variable reference will play a
19558 -- role in ABE diagnostics and requires a marker.
19559
19560 return True;
19561 end Needs_Variable_Reference_Marker;
19562
19563 ------------------------
19564 -- New_Copy_List_Tree --
19565 ------------------------
19566
19567 function New_Copy_List_Tree (List : List_Id) return List_Id is
19568 NL : List_Id;
19569 E : Node_Id;
19570
19571 begin
19572 if List = No_List then
19573 return No_List;
19574
19575 else
19576 NL := New_List;
19577 E := First (List);
19578
19579 while Present (E) loop
19580 Append (New_Copy_Tree (E), NL);
19581 E := Next (E);
19582 end loop;
19583
19584 return NL;
19585 end if;
19586 end New_Copy_List_Tree;
19587
19588 -------------------
19589 -- New_Copy_Tree --
19590 -------------------
19591
19592 -- The following tables play a key role in replicating entities and Itypes.
19593 -- They are intentionally declared at the library level rather than within
19594 -- New_Copy_Tree to avoid elaborating them on each call. This performance
19595 -- optimization saves up to 2% of the entire compilation time spent in the
19596 -- front end. Care should be taken to reset the tables on each new call to
19597 -- New_Copy_Tree.
19598
19599 NCT_Table_Max : constant := 511;
19600
19601 subtype NCT_Table_Index is Nat range 0 .. NCT_Table_Max - 1;
19602
19603 function NCT_Table_Hash (Key : Node_Or_Entity_Id) return NCT_Table_Index;
19604 -- Obtain the hash value of node or entity Key
19605
19606 --------------------
19607 -- NCT_Table_Hash --
19608 --------------------
19609
19610 function NCT_Table_Hash (Key : Node_Or_Entity_Id) return NCT_Table_Index is
19611 begin
19612 return NCT_Table_Index (Key mod NCT_Table_Max);
19613 end NCT_Table_Hash;
19614
19615 ----------------------
19616 -- NCT_New_Entities --
19617 ----------------------
19618
19619 -- The following table maps old entities and Itypes to their corresponding
19620 -- new entities and Itypes.
19621
19622 -- Aaa -> Xxx
19623
19624 package NCT_New_Entities is new Simple_HTable (
19625 Header_Num => NCT_Table_Index,
19626 Element => Entity_Id,
19627 No_Element => Empty,
19628 Key => Entity_Id,
19629 Hash => NCT_Table_Hash,
19630 Equal => "=");
19631
19632 ------------------------
19633 -- NCT_Pending_Itypes --
19634 ------------------------
19635
19636 -- The following table maps old Associated_Node_For_Itype nodes to a set of
19637 -- new itypes. Given a set of old Itypes Aaa, Bbb, and Ccc, where all three
19638 -- have the same Associated_Node_For_Itype Ppp, and their corresponding new
19639 -- Itypes Xxx, Yyy, Zzz, the table contains the following mapping:
19640
19641 -- Ppp -> (Xxx, Yyy, Zzz)
19642
19643 -- The set is expressed as an Elist
19644
19645 package NCT_Pending_Itypes is new Simple_HTable (
19646 Header_Num => NCT_Table_Index,
19647 Element => Elist_Id,
19648 No_Element => No_Elist,
19649 Key => Node_Id,
19650 Hash => NCT_Table_Hash,
19651 Equal => "=");
19652
19653 NCT_Tables_In_Use : Boolean := False;
19654 -- This flag keeps track of whether the two tables NCT_New_Entities and
19655 -- NCT_Pending_Itypes are in use. The flag is part of an optimization
19656 -- where certain operations are not performed if the tables are not in
19657 -- use. This saves up to 8% of the entire compilation time spent in the
19658 -- front end.
19659
19660 -------------------
19661 -- New_Copy_Tree --
19662 -------------------
19663
19664 function New_Copy_Tree
19665 (Source : Node_Id;
19666 Map : Elist_Id := No_Elist;
19667 New_Sloc : Source_Ptr := No_Location;
19668 New_Scope : Entity_Id := Empty;
19669 Scopes_In_EWA_OK : Boolean := False) return Node_Id
19670 is
19671 -- This routine performs low-level tree manipulations and needs access
19672 -- to the internals of the tree.
19673
19674 use Atree.Unchecked_Access;
19675 use Atree_Private_Part;
19676
19677 EWA_Level : Nat := 0;
19678 -- This counter keeps track of how many N_Expression_With_Actions nodes
19679 -- are encountered during a depth-first traversal of the subtree. These
19680 -- nodes may define new entities in their Actions lists and thus require
19681 -- special processing.
19682
19683 EWA_Inner_Scope_Level : Nat := 0;
19684 -- This counter keeps track of how many scoping constructs appear within
19685 -- an N_Expression_With_Actions node.
19686
19687 procedure Add_New_Entity (Old_Id : Entity_Id; New_Id : Entity_Id);
19688 pragma Inline (Add_New_Entity);
19689 -- Add an entry in the NCT_New_Entities table which maps key Old_Id to
19690 -- value New_Id. Old_Id is an entity which appears within the Actions
19691 -- list of an N_Expression_With_Actions node, or within an entity map.
19692 -- New_Id is the corresponding new entity generated during Phase 1.
19693
19694 procedure Add_Pending_Itype (Assoc_Nod : Node_Id; Itype : Entity_Id);
19695 pragma Inline (Add_New_Entity);
19696 -- Add an entry in the NCT_Pending_Itypes which maps key Assoc_Nod to
19697 -- value Itype. Assoc_Nod is the associated node of an itype. Itype is
19698 -- an itype.
19699
19700 procedure Build_NCT_Tables (Entity_Map : Elist_Id);
19701 pragma Inline (Build_NCT_Tables);
19702 -- Populate tables NCT_New_Entities and NCT_Pending_Itypes with the
19703 -- information supplied in entity map Entity_Map. The format of the
19704 -- entity map must be as follows:
19705 --
19706 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
19707
19708 function Copy_Any_Node_With_Replacement
19709 (N : Node_Or_Entity_Id) return Node_Or_Entity_Id;
19710 pragma Inline (Copy_Any_Node_With_Replacement);
19711 -- Replicate entity or node N by invoking one of the following routines:
19712 --
19713 -- Copy_Node_With_Replacement
19714 -- Corresponding_Entity
19715
19716 function Copy_Elist_With_Replacement (List : Elist_Id) return Elist_Id;
19717 -- Replicate the elements of entity list List
19718
19719 function Copy_Field_With_Replacement
19720 (Field : Union_Id;
19721 Old_Par : Node_Id := Empty;
19722 New_Par : Node_Id := Empty;
19723 Semantic : Boolean := False) return Union_Id;
19724 -- Replicate field Field by invoking one of the following routines:
19725 --
19726 -- Copy_Elist_With_Replacement
19727 -- Copy_List_With_Replacement
19728 -- Copy_Node_With_Replacement
19729 -- Corresponding_Entity
19730 --
19731 -- If the field is not an entity list, entity, itype, syntactic list,
19732 -- or node, then the field is returned unchanged. The routine always
19733 -- replicates entities, itypes, and valid syntactic fields. Old_Par is
19734 -- the expected parent of a syntactic field. New_Par is the new parent
19735 -- associated with a replicated syntactic field. Flag Semantic should
19736 -- be set when the input is a semantic field.
19737
19738 function Copy_List_With_Replacement (List : List_Id) return List_Id;
19739 -- Replicate the elements of syntactic list List
19740
19741 function Copy_Node_With_Replacement (N : Node_Id) return Node_Id;
19742 -- Replicate node N
19743
19744 function Corresponding_Entity (Id : Entity_Id) return Entity_Id;
19745 pragma Inline (Corresponding_Entity);
19746 -- Return the corresponding new entity of Id generated during Phase 1.
19747 -- If there is no such entity, return Id.
19748
19749 function In_Entity_Map
19750 (Id : Entity_Id;
19751 Entity_Map : Elist_Id) return Boolean;
19752 pragma Inline (In_Entity_Map);
19753 -- Determine whether entity Id is one of the old ids specified in entity
19754 -- map Entity_Map. The format of the entity map must be as follows:
19755 --
19756 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
19757
19758 procedure Update_CFS_Sloc (N : Node_Or_Entity_Id);
19759 pragma Inline (Update_CFS_Sloc);
19760 -- Update the Comes_From_Source and Sloc attributes of node or entity N
19761
19762 procedure Update_First_Real_Statement
19763 (Old_HSS : Node_Id;
19764 New_HSS : Node_Id);
19765 pragma Inline (Update_First_Real_Statement);
19766 -- Update semantic attribute First_Real_Statement of handled sequence of
19767 -- statements New_HSS based on handled sequence of statements Old_HSS.
19768
19769 procedure Update_Named_Associations
19770 (Old_Call : Node_Id;
19771 New_Call : Node_Id);
19772 pragma Inline (Update_Named_Associations);
19773 -- Update semantic chain First/Next_Named_Association of call New_call
19774 -- based on call Old_Call.
19775
19776 procedure Update_New_Entities (Entity_Map : Elist_Id);
19777 pragma Inline (Update_New_Entities);
19778 -- Update the semantic attributes of all new entities generated during
19779 -- Phase 1 that do not appear in entity map Entity_Map. The format of
19780 -- the entity map must be as follows:
19781 --
19782 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
19783
19784 procedure Update_Pending_Itypes
19785 (Old_Assoc : Node_Id;
19786 New_Assoc : Node_Id);
19787 pragma Inline (Update_Pending_Itypes);
19788 -- Update semantic attribute Associated_Node_For_Itype to refer to node
19789 -- New_Assoc for all itypes whose associated node is Old_Assoc.
19790
19791 procedure Update_Semantic_Fields (Id : Entity_Id);
19792 pragma Inline (Update_Semantic_Fields);
19793 -- Subsidiary to Update_New_Entities. Update semantic fields of entity
19794 -- or itype Id.
19795
19796 procedure Visit_Any_Node (N : Node_Or_Entity_Id);
19797 pragma Inline (Visit_Any_Node);
19798 -- Visit entity of node N by invoking one of the following routines:
19799 --
19800 -- Visit_Entity
19801 -- Visit_Itype
19802 -- Visit_Node
19803
19804 procedure Visit_Elist (List : Elist_Id);
19805 -- Visit the elements of entity list List
19806
19807 procedure Visit_Entity (Id : Entity_Id);
19808 -- Visit entity Id. This action may create a new entity of Id and save
19809 -- it in table NCT_New_Entities.
19810
19811 procedure Visit_Field
19812 (Field : Union_Id;
19813 Par_Nod : Node_Id := Empty;
19814 Semantic : Boolean := False);
19815 -- Visit field Field by invoking one of the following routines:
19816 --
19817 -- Visit_Elist
19818 -- Visit_Entity
19819 -- Visit_Itype
19820 -- Visit_List
19821 -- Visit_Node
19822 --
19823 -- If the field is not an entity list, entity, itype, syntactic list,
19824 -- or node, then the field is not visited. The routine always visits
19825 -- valid syntactic fields. Par_Nod is the expected parent of the
19826 -- syntactic field. Flag Semantic should be set when the input is a
19827 -- semantic field.
19828
19829 procedure Visit_Itype (Itype : Entity_Id);
19830 -- Visit itype Itype. This action may create a new entity for Itype and
19831 -- save it in table NCT_New_Entities. In addition, the routine may map
19832 -- the associated node of Itype to the new itype in NCT_Pending_Itypes.
19833
19834 procedure Visit_List (List : List_Id);
19835 -- Visit the elements of syntactic list List
19836
19837 procedure Visit_Node (N : Node_Id);
19838 -- Visit node N
19839
19840 procedure Visit_Semantic_Fields (Id : Entity_Id);
19841 pragma Inline (Visit_Semantic_Fields);
19842 -- Subsidiary to Visit_Entity and Visit_Itype. Visit common semantic
19843 -- fields of entity or itype Id.
19844
19845 --------------------
19846 -- Add_New_Entity --
19847 --------------------
19848
19849 procedure Add_New_Entity (Old_Id : Entity_Id; New_Id : Entity_Id) is
19850 begin
19851 pragma Assert (Present (Old_Id));
19852 pragma Assert (Present (New_Id));
19853 pragma Assert (Nkind (Old_Id) in N_Entity);
19854 pragma Assert (Nkind (New_Id) in N_Entity);
19855
19856 NCT_Tables_In_Use := True;
19857
19858 -- Sanity check the NCT_New_Entities table. No previous mapping with
19859 -- key Old_Id should exist.
19860
19861 pragma Assert (No (NCT_New_Entities.Get (Old_Id)));
19862
19863 -- Establish the mapping
19864
19865 -- Old_Id -> New_Id
19866
19867 NCT_New_Entities.Set (Old_Id, New_Id);
19868 end Add_New_Entity;
19869
19870 -----------------------
19871 -- Add_Pending_Itype --
19872 -----------------------
19873
19874 procedure Add_Pending_Itype (Assoc_Nod : Node_Id; Itype : Entity_Id) is
19875 Itypes : Elist_Id;
19876
19877 begin
19878 pragma Assert (Present (Assoc_Nod));
19879 pragma Assert (Present (Itype));
19880 pragma Assert (Nkind (Itype) in N_Entity);
19881 pragma Assert (Is_Itype (Itype));
19882
19883 NCT_Tables_In_Use := True;
19884
19885 -- It is not possible to sanity check the NCT_Pendint_Itypes table
19886 -- directly because a single node may act as the associated node for
19887 -- multiple itypes.
19888
19889 Itypes := NCT_Pending_Itypes.Get (Assoc_Nod);
19890
19891 if No (Itypes) then
19892 Itypes := New_Elmt_List;
19893 NCT_Pending_Itypes.Set (Assoc_Nod, Itypes);
19894 end if;
19895
19896 -- Establish the mapping
19897
19898 -- Assoc_Nod -> (Itype, ...)
19899
19900 -- Avoid inserting the same itype multiple times. This involves a
19901 -- linear search, however the set of itypes with the same associated
19902 -- node is very small.
19903
19904 Append_Unique_Elmt (Itype, Itypes);
19905 end Add_Pending_Itype;
19906
19907 ----------------------
19908 -- Build_NCT_Tables --
19909 ----------------------
19910
19911 procedure Build_NCT_Tables (Entity_Map : Elist_Id) is
19912 Elmt : Elmt_Id;
19913 Old_Id : Entity_Id;
19914 New_Id : Entity_Id;
19915
19916 begin
19917 -- Nothing to do when there is no entity map
19918
19919 if No (Entity_Map) then
19920 return;
19921 end if;
19922
19923 Elmt := First_Elmt (Entity_Map);
19924 while Present (Elmt) loop
19925
19926 -- Extract the (Old_Id, New_Id) pair from the entity map
19927
19928 Old_Id := Node (Elmt);
19929 Next_Elmt (Elmt);
19930
19931 New_Id := Node (Elmt);
19932 Next_Elmt (Elmt);
19933
19934 -- Establish the following mapping within table NCT_New_Entities
19935
19936 -- Old_Id -> New_Id
19937
19938 Add_New_Entity (Old_Id, New_Id);
19939
19940 -- Establish the following mapping within table NCT_Pending_Itypes
19941 -- when the new entity is an itype.
19942
19943 -- Assoc_Nod -> (New_Id, ...)
19944
19945 -- IMPORTANT: the associated node is that of the old itype because
19946 -- the node will be replicated in Phase 2.
19947
19948 if Is_Itype (Old_Id) then
19949 Add_Pending_Itype
19950 (Assoc_Nod => Associated_Node_For_Itype (Old_Id),
19951 Itype => New_Id);
19952 end if;
19953 end loop;
19954 end Build_NCT_Tables;
19955
19956 ------------------------------------
19957 -- Copy_Any_Node_With_Replacement --
19958 ------------------------------------
19959
19960 function Copy_Any_Node_With_Replacement
19961 (N : Node_Or_Entity_Id) return Node_Or_Entity_Id
19962 is
19963 begin
19964 if Nkind (N) in N_Entity then
19965 return Corresponding_Entity (N);
19966 else
19967 return Copy_Node_With_Replacement (N);
19968 end if;
19969 end Copy_Any_Node_With_Replacement;
19970
19971 ---------------------------------
19972 -- Copy_Elist_With_Replacement --
19973 ---------------------------------
19974
19975 function Copy_Elist_With_Replacement (List : Elist_Id) return Elist_Id is
19976 Elmt : Elmt_Id;
19977 Result : Elist_Id;
19978
19979 begin
19980 -- Copy the contents of the old list. Note that the list itself may
19981 -- be empty, in which case the routine returns a new empty list. This
19982 -- avoids sharing lists between subtrees. The element of an entity
19983 -- list could be an entity or a node, hence the invocation of routine
19984 -- Copy_Any_Node_With_Replacement.
19985
19986 if Present (List) then
19987 Result := New_Elmt_List;
19988
19989 Elmt := First_Elmt (List);
19990 while Present (Elmt) loop
19991 Append_Elmt
19992 (Copy_Any_Node_With_Replacement (Node (Elmt)), Result);
19993
19994 Next_Elmt (Elmt);
19995 end loop;
19996
19997 -- Otherwise the list does not exist
19998
19999 else
20000 Result := No_Elist;
20001 end if;
20002
20003 return Result;
20004 end Copy_Elist_With_Replacement;
20005
20006 ---------------------------------
20007 -- Copy_Field_With_Replacement --
20008 ---------------------------------
20009
20010 function Copy_Field_With_Replacement
20011 (Field : Union_Id;
20012 Old_Par : Node_Id := Empty;
20013 New_Par : Node_Id := Empty;
20014 Semantic : Boolean := False) return Union_Id
20015 is
20016 begin
20017 -- The field is empty
20018
20019 if Field = Union_Id (Empty) then
20020 return Field;
20021
20022 -- The field is an entity/itype/node
20023
20024 elsif Field in Node_Range then
20025 declare
20026 Old_N : constant Node_Id := Node_Id (Field);
20027 Syntactic : constant Boolean := Parent (Old_N) = Old_Par;
20028
20029 New_N : Node_Id;
20030
20031 begin
20032 -- The field is an entity/itype
20033
20034 if Nkind (Old_N) in N_Entity then
20035
20036 -- An entity/itype is always replicated
20037
20038 New_N := Corresponding_Entity (Old_N);
20039
20040 -- Update the parent pointer when the entity is a syntactic
20041 -- field. Note that itypes do not have parent pointers.
20042
20043 if Syntactic and then New_N /= Old_N then
20044 Set_Parent (New_N, New_Par);
20045 end if;
20046
20047 -- The field is a node
20048
20049 else
20050 -- A node is replicated when it is either a syntactic field
20051 -- or when the caller treats it as a semantic attribute.
20052
20053 if Syntactic or else Semantic then
20054 New_N := Copy_Node_With_Replacement (Old_N);
20055
20056 -- Update the parent pointer when the node is a syntactic
20057 -- field.
20058
20059 if Syntactic and then New_N /= Old_N then
20060 Set_Parent (New_N, New_Par);
20061 end if;
20062
20063 -- Otherwise the node is returned unchanged
20064
20065 else
20066 New_N := Old_N;
20067 end if;
20068 end if;
20069
20070 return Union_Id (New_N);
20071 end;
20072
20073 -- The field is an entity list
20074
20075 elsif Field in Elist_Range then
20076 return Union_Id (Copy_Elist_With_Replacement (Elist_Id (Field)));
20077
20078 -- The field is a syntactic list
20079
20080 elsif Field in List_Range then
20081 declare
20082 Old_List : constant List_Id := List_Id (Field);
20083 Syntactic : constant Boolean := Parent (Old_List) = Old_Par;
20084
20085 New_List : List_Id;
20086
20087 begin
20088 -- A list is replicated when it is either a syntactic field or
20089 -- when the caller treats it as a semantic attribute.
20090
20091 if Syntactic or else Semantic then
20092 New_List := Copy_List_With_Replacement (Old_List);
20093
20094 -- Update the parent pointer when the list is a syntactic
20095 -- field.
20096
20097 if Syntactic and then New_List /= Old_List then
20098 Set_Parent (New_List, New_Par);
20099 end if;
20100
20101 -- Otherwise the list is returned unchanged
20102
20103 else
20104 New_List := Old_List;
20105 end if;
20106
20107 return Union_Id (New_List);
20108 end;
20109
20110 -- Otherwise the field denotes an attribute that does not need to be
20111 -- replicated (Chars, literals, etc).
20112
20113 else
20114 return Field;
20115 end if;
20116 end Copy_Field_With_Replacement;
20117
20118 --------------------------------
20119 -- Copy_List_With_Replacement --
20120 --------------------------------
20121
20122 function Copy_List_With_Replacement (List : List_Id) return List_Id is
20123 Elmt : Node_Id;
20124 Result : List_Id;
20125
20126 begin
20127 -- Copy the contents of the old list. Note that the list itself may
20128 -- be empty, in which case the routine returns a new empty list. This
20129 -- avoids sharing lists between subtrees. The element of a syntactic
20130 -- list is always a node, never an entity or itype, hence the call to
20131 -- routine Copy_Node_With_Replacement.
20132
20133 if Present (List) then
20134 Result := New_List;
20135
20136 Elmt := First (List);
20137 while Present (Elmt) loop
20138 Append (Copy_Node_With_Replacement (Elmt), Result);
20139
20140 Next (Elmt);
20141 end loop;
20142
20143 -- Otherwise the list does not exist
20144
20145 else
20146 Result := No_List;
20147 end if;
20148
20149 return Result;
20150 end Copy_List_With_Replacement;
20151
20152 --------------------------------
20153 -- Copy_Node_With_Replacement --
20154 --------------------------------
20155
20156 function Copy_Node_With_Replacement (N : Node_Id) return Node_Id is
20157 Result : Node_Id;
20158
20159 begin
20160 -- Assume that the node must be returned unchanged
20161
20162 Result := N;
20163
20164 if N > Empty_Or_Error then
20165 pragma Assert (Nkind (N) not in N_Entity);
20166
20167 Result := New_Copy (N);
20168
20169 Set_Field1 (Result,
20170 Copy_Field_With_Replacement
20171 (Field => Field1 (Result),
20172 Old_Par => N,
20173 New_Par => Result));
20174
20175 Set_Field2 (Result,
20176 Copy_Field_With_Replacement
20177 (Field => Field2 (Result),
20178 Old_Par => N,
20179 New_Par => Result));
20180
20181 Set_Field3 (Result,
20182 Copy_Field_With_Replacement
20183 (Field => Field3 (Result),
20184 Old_Par => N,
20185 New_Par => Result));
20186
20187 Set_Field4 (Result,
20188 Copy_Field_With_Replacement
20189 (Field => Field4 (Result),
20190 Old_Par => N,
20191 New_Par => Result));
20192
20193 Set_Field5 (Result,
20194 Copy_Field_With_Replacement
20195 (Field => Field5 (Result),
20196 Old_Par => N,
20197 New_Par => Result));
20198
20199 -- Update the Comes_From_Source and Sloc attributes of the node
20200 -- in case the caller has supplied new values.
20201
20202 Update_CFS_Sloc (Result);
20203
20204 -- Update the Associated_Node_For_Itype attribute of all itypes
20205 -- created during Phase 1 whose associated node is N. As a result
20206 -- the Associated_Node_For_Itype refers to the replicated node.
20207 -- No action needs to be taken when the Associated_Node_For_Itype
20208 -- refers to an entity because this was already handled during
20209 -- Phase 1, in Visit_Itype.
20210
20211 Update_Pending_Itypes
20212 (Old_Assoc => N,
20213 New_Assoc => Result);
20214
20215 -- Update the First/Next_Named_Association chain for a replicated
20216 -- call.
20217
20218 if Nkind_In (N, N_Entry_Call_Statement,
20219 N_Function_Call,
20220 N_Procedure_Call_Statement)
20221 then
20222 Update_Named_Associations
20223 (Old_Call => N,
20224 New_Call => Result);
20225
20226 -- Update the Renamed_Object attribute of a replicated object
20227 -- declaration.
20228
20229 elsif Nkind (N) = N_Object_Renaming_Declaration then
20230 Set_Renamed_Object (Defining_Entity (Result), Name (Result));
20231
20232 -- Update the First_Real_Statement attribute of a replicated
20233 -- handled sequence of statements.
20234
20235 elsif Nkind (N) = N_Handled_Sequence_Of_Statements then
20236 Update_First_Real_Statement
20237 (Old_HSS => N,
20238 New_HSS => Result);
20239 end if;
20240 end if;
20241
20242 return Result;
20243 end Copy_Node_With_Replacement;
20244
20245 --------------------------
20246 -- Corresponding_Entity --
20247 --------------------------
20248
20249 function Corresponding_Entity (Id : Entity_Id) return Entity_Id is
20250 New_Id : Entity_Id;
20251 Result : Entity_Id;
20252
20253 begin
20254 -- Assume that the entity must be returned unchanged
20255
20256 Result := Id;
20257
20258 if Id > Empty_Or_Error then
20259 pragma Assert (Nkind (Id) in N_Entity);
20260
20261 -- Determine whether the entity has a corresponding new entity
20262 -- generated during Phase 1 and if it does, use it.
20263
20264 if NCT_Tables_In_Use then
20265 New_Id := NCT_New_Entities.Get (Id);
20266
20267 if Present (New_Id) then
20268 Result := New_Id;
20269 end if;
20270 end if;
20271 end if;
20272
20273 return Result;
20274 end Corresponding_Entity;
20275
20276 -------------------
20277 -- In_Entity_Map --
20278 -------------------
20279
20280 function In_Entity_Map
20281 (Id : Entity_Id;
20282 Entity_Map : Elist_Id) return Boolean
20283 is
20284 Elmt : Elmt_Id;
20285 Old_Id : Entity_Id;
20286
20287 begin
20288 -- The entity map contains pairs (Old_Id, New_Id). The advancement
20289 -- step always skips the New_Id portion of the pair.
20290
20291 if Present (Entity_Map) then
20292 Elmt := First_Elmt (Entity_Map);
20293 while Present (Elmt) loop
20294 Old_Id := Node (Elmt);
20295
20296 if Old_Id = Id then
20297 return True;
20298 end if;
20299
20300 Next_Elmt (Elmt);
20301 Next_Elmt (Elmt);
20302 end loop;
20303 end if;
20304
20305 return False;
20306 end In_Entity_Map;
20307
20308 ---------------------
20309 -- Update_CFS_Sloc --
20310 ---------------------
20311
20312 procedure Update_CFS_Sloc (N : Node_Or_Entity_Id) is
20313 begin
20314 -- A new source location defaults the Comes_From_Source attribute
20315
20316 if New_Sloc /= No_Location then
20317 Set_Comes_From_Source (N, Default_Node.Comes_From_Source);
20318 Set_Sloc (N, New_Sloc);
20319 end if;
20320 end Update_CFS_Sloc;
20321
20322 ---------------------------------
20323 -- Update_First_Real_Statement --
20324 ---------------------------------
20325
20326 procedure Update_First_Real_Statement
20327 (Old_HSS : Node_Id;
20328 New_HSS : Node_Id)
20329 is
20330 Old_First_Stmt : constant Node_Id := First_Real_Statement (Old_HSS);
20331
20332 New_Stmt : Node_Id;
20333 Old_Stmt : Node_Id;
20334
20335 begin
20336 -- Recreate the First_Real_Statement attribute of a handled sequence
20337 -- of statements by traversing the statement lists of both sequences
20338 -- in parallel.
20339
20340 if Present (Old_First_Stmt) then
20341 New_Stmt := First (Statements (New_HSS));
20342 Old_Stmt := First (Statements (Old_HSS));
20343 while Present (Old_Stmt) and then Old_Stmt /= Old_First_Stmt loop
20344 Next (New_Stmt);
20345 Next (Old_Stmt);
20346 end loop;
20347
20348 pragma Assert (Present (New_Stmt));
20349 pragma Assert (Present (Old_Stmt));
20350
20351 Set_First_Real_Statement (New_HSS, New_Stmt);
20352 end if;
20353 end Update_First_Real_Statement;
20354
20355 -------------------------------
20356 -- Update_Named_Associations --
20357 -------------------------------
20358
20359 procedure Update_Named_Associations
20360 (Old_Call : Node_Id;
20361 New_Call : Node_Id)
20362 is
20363 New_Act : Node_Id;
20364 New_Next : Node_Id;
20365 Old_Act : Node_Id;
20366 Old_Next : Node_Id;
20367
20368 begin
20369 -- Recreate the First/Next_Named_Actual chain of a call by traversing
20370 -- the chains of both the old and new calls in parallel.
20371
20372 New_Act := First (Parameter_Associations (New_Call));
20373 Old_Act := First (Parameter_Associations (Old_Call));
20374 while Present (Old_Act) loop
20375 if Nkind (Old_Act) = N_Parameter_Association
20376 and then Present (Next_Named_Actual (Old_Act))
20377 then
20378 if First_Named_Actual (Old_Call) =
20379 Explicit_Actual_Parameter (Old_Act)
20380 then
20381 Set_First_Named_Actual (New_Call,
20382 Explicit_Actual_Parameter (New_Act));
20383 end if;
20384
20385 -- Scan the actual parameter list to find the next suitable
20386 -- named actual. Note that the list may be out of order.
20387
20388 New_Next := First (Parameter_Associations (New_Call));
20389 Old_Next := First (Parameter_Associations (Old_Call));
20390 while Nkind (Old_Next) /= N_Parameter_Association
20391 or else Explicit_Actual_Parameter (Old_Next) /=
20392 Next_Named_Actual (Old_Act)
20393 loop
20394 Next (New_Next);
20395 Next (Old_Next);
20396 end loop;
20397
20398 Set_Next_Named_Actual (New_Act,
20399 Explicit_Actual_Parameter (New_Next));
20400 end if;
20401
20402 Next (New_Act);
20403 Next (Old_Act);
20404 end loop;
20405 end Update_Named_Associations;
20406
20407 -------------------------
20408 -- Update_New_Entities --
20409 -------------------------
20410
20411 procedure Update_New_Entities (Entity_Map : Elist_Id) is
20412 New_Id : Entity_Id := Empty;
20413 Old_Id : Entity_Id := Empty;
20414
20415 begin
20416 if NCT_Tables_In_Use then
20417 NCT_New_Entities.Get_First (Old_Id, New_Id);
20418
20419 -- Update the semantic fields of all new entities created during
20420 -- Phase 1 which were not supplied via an entity map.
20421 -- ??? Is there a better way of distinguishing those?
20422
20423 while Present (Old_Id) and then Present (New_Id) loop
20424 if not (Present (Entity_Map)
20425 and then In_Entity_Map (Old_Id, Entity_Map))
20426 then
20427 Update_Semantic_Fields (New_Id);
20428 end if;
20429
20430 NCT_New_Entities.Get_Next (Old_Id, New_Id);
20431 end loop;
20432 end if;
20433 end Update_New_Entities;
20434
20435 ---------------------------
20436 -- Update_Pending_Itypes --
20437 ---------------------------
20438
20439 procedure Update_Pending_Itypes
20440 (Old_Assoc : Node_Id;
20441 New_Assoc : Node_Id)
20442 is
20443 Item : Elmt_Id;
20444 Itypes : Elist_Id;
20445
20446 begin
20447 if NCT_Tables_In_Use then
20448 Itypes := NCT_Pending_Itypes.Get (Old_Assoc);
20449
20450 -- Update the Associated_Node_For_Itype attribute for all itypes
20451 -- which originally refer to Old_Assoc to designate New_Assoc.
20452
20453 if Present (Itypes) then
20454 Item := First_Elmt (Itypes);
20455 while Present (Item) loop
20456 Set_Associated_Node_For_Itype (Node (Item), New_Assoc);
20457
20458 Next_Elmt (Item);
20459 end loop;
20460 end if;
20461 end if;
20462 end Update_Pending_Itypes;
20463
20464 ----------------------------
20465 -- Update_Semantic_Fields --
20466 ----------------------------
20467
20468 procedure Update_Semantic_Fields (Id : Entity_Id) is
20469 begin
20470 -- Discriminant_Constraint
20471
20472 if Is_Type (Id) and then Has_Discriminants (Base_Type (Id)) then
20473 Set_Discriminant_Constraint (Id, Elist_Id (
20474 Copy_Field_With_Replacement
20475 (Field => Union_Id (Discriminant_Constraint (Id)),
20476 Semantic => True)));
20477 end if;
20478
20479 -- Etype
20480
20481 Set_Etype (Id, Node_Id (
20482 Copy_Field_With_Replacement
20483 (Field => Union_Id (Etype (Id)),
20484 Semantic => True)));
20485
20486 -- First_Index
20487 -- Packed_Array_Impl_Type
20488
20489 if Is_Array_Type (Id) then
20490 if Present (First_Index (Id)) then
20491 Set_First_Index (Id, First (List_Id (
20492 Copy_Field_With_Replacement
20493 (Field => Union_Id (List_Containing (First_Index (Id))),
20494 Semantic => True))));
20495 end if;
20496
20497 if Is_Packed (Id) then
20498 Set_Packed_Array_Impl_Type (Id, Node_Id (
20499 Copy_Field_With_Replacement
20500 (Field => Union_Id (Packed_Array_Impl_Type (Id)),
20501 Semantic => True)));
20502 end if;
20503 end if;
20504
20505 -- Prev_Entity
20506
20507 Set_Prev_Entity (Id, Node_Id (
20508 Copy_Field_With_Replacement
20509 (Field => Union_Id (Prev_Entity (Id)),
20510 Semantic => True)));
20511
20512 -- Next_Entity
20513
20514 Set_Next_Entity (Id, Node_Id (
20515 Copy_Field_With_Replacement
20516 (Field => Union_Id (Next_Entity (Id)),
20517 Semantic => True)));
20518
20519 -- Scalar_Range
20520
20521 if Is_Discrete_Type (Id) then
20522 Set_Scalar_Range (Id, Node_Id (
20523 Copy_Field_With_Replacement
20524 (Field => Union_Id (Scalar_Range (Id)),
20525 Semantic => True)));
20526 end if;
20527
20528 -- Scope
20529
20530 -- Update the scope when the caller specified an explicit one
20531
20532 if Present (New_Scope) then
20533 Set_Scope (Id, New_Scope);
20534 else
20535 Set_Scope (Id, Node_Id (
20536 Copy_Field_With_Replacement
20537 (Field => Union_Id (Scope (Id)),
20538 Semantic => True)));
20539 end if;
20540 end Update_Semantic_Fields;
20541
20542 --------------------
20543 -- Visit_Any_Node --
20544 --------------------
20545
20546 procedure Visit_Any_Node (N : Node_Or_Entity_Id) is
20547 begin
20548 if Nkind (N) in N_Entity then
20549 if Is_Itype (N) then
20550 Visit_Itype (N);
20551 else
20552 Visit_Entity (N);
20553 end if;
20554 else
20555 Visit_Node (N);
20556 end if;
20557 end Visit_Any_Node;
20558
20559 -----------------
20560 -- Visit_Elist --
20561 -----------------
20562
20563 procedure Visit_Elist (List : Elist_Id) is
20564 Elmt : Elmt_Id;
20565
20566 begin
20567 -- The element of an entity list could be an entity, itype, or a
20568 -- node, hence the call to Visit_Any_Node.
20569
20570 if Present (List) then
20571 Elmt := First_Elmt (List);
20572 while Present (Elmt) loop
20573 Visit_Any_Node (Node (Elmt));
20574
20575 Next_Elmt (Elmt);
20576 end loop;
20577 end if;
20578 end Visit_Elist;
20579
20580 ------------------
20581 -- Visit_Entity --
20582 ------------------
20583
20584 procedure Visit_Entity (Id : Entity_Id) is
20585 New_Id : Entity_Id;
20586
20587 begin
20588 pragma Assert (Nkind (Id) in N_Entity);
20589 pragma Assert (not Is_Itype (Id));
20590
20591 -- Nothing to do when the entity is not defined in the Actions list
20592 -- of an N_Expression_With_Actions node.
20593
20594 if EWA_Level = 0 then
20595 return;
20596
20597 -- Nothing to do when the entity is defined in a scoping construct
20598 -- within an N_Expression_With_Actions node, unless the caller has
20599 -- requested their replication.
20600
20601 -- ??? should this restriction be eliminated?
20602
20603 elsif EWA_Inner_Scope_Level > 0 and then not Scopes_In_EWA_OK then
20604 return;
20605
20606 -- Nothing to do when the entity does not denote a construct that
20607 -- may appear within an N_Expression_With_Actions node. Relaxing
20608 -- this restriction leads to a performance penalty.
20609
20610 -- ??? this list is flaky, and may hide dormant bugs
20611
20612 elsif not Ekind_In (Id, E_Block,
20613 E_Constant,
20614 E_Label,
20615 E_Procedure,
20616 E_Variable)
20617 and then not Is_Type (Id)
20618 then
20619 return;
20620
20621 -- Nothing to do when the entity was already visited
20622
20623 elsif NCT_Tables_In_Use
20624 and then Present (NCT_New_Entities.Get (Id))
20625 then
20626 return;
20627
20628 -- Nothing to do when the declaration node of the entity is not in
20629 -- the subtree being replicated.
20630
20631 elsif not In_Subtree
20632 (N => Declaration_Node (Id),
20633 Root => Source)
20634 then
20635 return;
20636 end if;
20637
20638 -- Create a new entity by directly copying the old entity. This
20639 -- action causes all attributes of the old entity to be inherited.
20640
20641 New_Id := New_Copy (Id);
20642
20643 -- Create a new name for the new entity because the back end needs
20644 -- distinct names for debugging purposes.
20645
20646 Set_Chars (New_Id, New_Internal_Name ('T'));
20647
20648 -- Update the Comes_From_Source and Sloc attributes of the entity in
20649 -- case the caller has supplied new values.
20650
20651 Update_CFS_Sloc (New_Id);
20652
20653 -- Establish the following mapping within table NCT_New_Entities:
20654
20655 -- Id -> New_Id
20656
20657 Add_New_Entity (Id, New_Id);
20658
20659 -- Deal with the semantic fields of entities. The fields are visited
20660 -- because they may mention entities which reside within the subtree
20661 -- being copied.
20662
20663 Visit_Semantic_Fields (Id);
20664 end Visit_Entity;
20665
20666 -----------------
20667 -- Visit_Field --
20668 -----------------
20669
20670 procedure Visit_Field
20671 (Field : Union_Id;
20672 Par_Nod : Node_Id := Empty;
20673 Semantic : Boolean := False)
20674 is
20675 begin
20676 -- The field is empty
20677
20678 if Field = Union_Id (Empty) then
20679 return;
20680
20681 -- The field is an entity/itype/node
20682
20683 elsif Field in Node_Range then
20684 declare
20685 N : constant Node_Id := Node_Id (Field);
20686
20687 begin
20688 -- The field is an entity/itype
20689
20690 if Nkind (N) in N_Entity then
20691
20692 -- Itypes are always visited
20693
20694 if Is_Itype (N) then
20695 Visit_Itype (N);
20696
20697 -- An entity is visited when it is either a syntactic field
20698 -- or when the caller treats it as a semantic attribute.
20699
20700 elsif Parent (N) = Par_Nod or else Semantic then
20701 Visit_Entity (N);
20702 end if;
20703
20704 -- The field is a node
20705
20706 else
20707 -- A node is visited when it is either a syntactic field or
20708 -- when the caller treats it as a semantic attribute.
20709
20710 if Parent (N) = Par_Nod or else Semantic then
20711 Visit_Node (N);
20712 end if;
20713 end if;
20714 end;
20715
20716 -- The field is an entity list
20717
20718 elsif Field in Elist_Range then
20719 Visit_Elist (Elist_Id (Field));
20720
20721 -- The field is a syntax list
20722
20723 elsif Field in List_Range then
20724 declare
20725 List : constant List_Id := List_Id (Field);
20726
20727 begin
20728 -- A syntax list is visited when it is either a syntactic field
20729 -- or when the caller treats it as a semantic attribute.
20730
20731 if Parent (List) = Par_Nod or else Semantic then
20732 Visit_List (List);
20733 end if;
20734 end;
20735
20736 -- Otherwise the field denotes information which does not need to be
20737 -- visited (chars, literals, etc.).
20738
20739 else
20740 null;
20741 end if;
20742 end Visit_Field;
20743
20744 -----------------
20745 -- Visit_Itype --
20746 -----------------
20747
20748 procedure Visit_Itype (Itype : Entity_Id) is
20749 New_Assoc : Node_Id;
20750 New_Itype : Entity_Id;
20751 Old_Assoc : Node_Id;
20752
20753 begin
20754 pragma Assert (Nkind (Itype) in N_Entity);
20755 pragma Assert (Is_Itype (Itype));
20756
20757 -- Itypes that describe the designated type of access to subprograms
20758 -- have the structure of subprogram declarations, with signatures,
20759 -- etc. Either we duplicate the signatures completely, or choose to
20760 -- share such itypes, which is fine because their elaboration will
20761 -- have no side effects.
20762
20763 if Ekind (Itype) = E_Subprogram_Type then
20764 return;
20765
20766 -- Nothing to do if the itype was already visited
20767
20768 elsif NCT_Tables_In_Use
20769 and then Present (NCT_New_Entities.Get (Itype))
20770 then
20771 return;
20772
20773 -- Nothing to do if the associated node of the itype is not within
20774 -- the subtree being replicated.
20775
20776 elsif not In_Subtree
20777 (N => Associated_Node_For_Itype (Itype),
20778 Root => Source)
20779 then
20780 return;
20781 end if;
20782
20783 -- Create a new itype by directly copying the old itype. This action
20784 -- causes all attributes of the old itype to be inherited.
20785
20786 New_Itype := New_Copy (Itype);
20787
20788 -- Create a new name for the new itype because the back end requires
20789 -- distinct names for debugging purposes.
20790
20791 Set_Chars (New_Itype, New_Internal_Name ('T'));
20792
20793 -- Update the Comes_From_Source and Sloc attributes of the itype in
20794 -- case the caller has supplied new values.
20795
20796 Update_CFS_Sloc (New_Itype);
20797
20798 -- Establish the following mapping within table NCT_New_Entities:
20799
20800 -- Itype -> New_Itype
20801
20802 Add_New_Entity (Itype, New_Itype);
20803
20804 -- The new itype must be unfrozen because the resulting subtree may
20805 -- be inserted anywhere and cause an earlier or later freezing.
20806
20807 if Present (Freeze_Node (New_Itype)) then
20808 Set_Freeze_Node (New_Itype, Empty);
20809 Set_Is_Frozen (New_Itype, False);
20810 end if;
20811
20812 -- If a record subtype is simply copied, the entity list will be
20813 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
20814 -- ??? What does this do?
20815
20816 if Ekind_In (Itype, E_Class_Wide_Subtype, E_Record_Subtype) then
20817 Set_Cloned_Subtype (New_Itype, Itype);
20818 end if;
20819
20820 -- The associated node may denote an entity, in which case it may
20821 -- already have a new corresponding entity created during a prior
20822 -- call to Visit_Entity or Visit_Itype for the same subtree.
20823
20824 -- Given
20825 -- Old_Assoc ---------> New_Assoc
20826
20827 -- Created by Visit_Itype
20828 -- Itype -------------> New_Itype
20829 -- ANFI = Old_Assoc ANFI = Old_Assoc < must be updated
20830
20831 -- In the example above, Old_Assoc is an arbitrary entity that was
20832 -- already visited for the same subtree and has a corresponding new
20833 -- entity New_Assoc. Old_Assoc was inherited by New_Itype by virtue
20834 -- of copying entities, however it must be updated to New_Assoc.
20835
20836 Old_Assoc := Associated_Node_For_Itype (Itype);
20837
20838 if Nkind (Old_Assoc) in N_Entity then
20839 if NCT_Tables_In_Use then
20840 New_Assoc := NCT_New_Entities.Get (Old_Assoc);
20841
20842 if Present (New_Assoc) then
20843 Set_Associated_Node_For_Itype (New_Itype, New_Assoc);
20844 end if;
20845 end if;
20846
20847 -- Otherwise the associated node denotes a node. Postpone the update
20848 -- until Phase 2 when the node is replicated. Establish the following
20849 -- mapping within table NCT_Pending_Itypes:
20850
20851 -- Old_Assoc -> (New_Type, ...)
20852
20853 else
20854 Add_Pending_Itype (Old_Assoc, New_Itype);
20855 end if;
20856
20857 -- Deal with the semantic fields of itypes. The fields are visited
20858 -- because they may mention entities that reside within the subtree
20859 -- being copied.
20860
20861 Visit_Semantic_Fields (Itype);
20862 end Visit_Itype;
20863
20864 ----------------
20865 -- Visit_List --
20866 ----------------
20867
20868 procedure Visit_List (List : List_Id) is
20869 Elmt : Node_Id;
20870
20871 begin
20872 -- Note that the element of a syntactic list is always a node, never
20873 -- an entity or itype, hence the call to Visit_Node.
20874
20875 if Present (List) then
20876 Elmt := First (List);
20877 while Present (Elmt) loop
20878 Visit_Node (Elmt);
20879
20880 Next (Elmt);
20881 end loop;
20882 end if;
20883 end Visit_List;
20884
20885 ----------------
20886 -- Visit_Node --
20887 ----------------
20888
20889 procedure Visit_Node (N : Node_Or_Entity_Id) is
20890 begin
20891 pragma Assert (Nkind (N) not in N_Entity);
20892
20893 if Nkind (N) = N_Expression_With_Actions then
20894 EWA_Level := EWA_Level + 1;
20895
20896 elsif EWA_Level > 0
20897 and then Nkind_In (N, N_Block_Statement,
20898 N_Subprogram_Body,
20899 N_Subprogram_Declaration)
20900 then
20901 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level + 1;
20902 end if;
20903
20904 Visit_Field
20905 (Field => Field1 (N),
20906 Par_Nod => N);
20907
20908 Visit_Field
20909 (Field => Field2 (N),
20910 Par_Nod => N);
20911
20912 Visit_Field
20913 (Field => Field3 (N),
20914 Par_Nod => N);
20915
20916 Visit_Field
20917 (Field => Field4 (N),
20918 Par_Nod => N);
20919
20920 Visit_Field
20921 (Field => Field5 (N),
20922 Par_Nod => N);
20923
20924 if EWA_Level > 0
20925 and then Nkind_In (N, N_Block_Statement,
20926 N_Subprogram_Body,
20927 N_Subprogram_Declaration)
20928 then
20929 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level - 1;
20930
20931 elsif Nkind (N) = N_Expression_With_Actions then
20932 EWA_Level := EWA_Level - 1;
20933 end if;
20934 end Visit_Node;
20935
20936 ---------------------------
20937 -- Visit_Semantic_Fields --
20938 ---------------------------
20939
20940 procedure Visit_Semantic_Fields (Id : Entity_Id) is
20941 begin
20942 pragma Assert (Nkind (Id) in N_Entity);
20943
20944 -- Discriminant_Constraint
20945
20946 if Is_Type (Id) and then Has_Discriminants (Base_Type (Id)) then
20947 Visit_Field
20948 (Field => Union_Id (Discriminant_Constraint (Id)),
20949 Semantic => True);
20950 end if;
20951
20952 -- Etype
20953
20954 Visit_Field
20955 (Field => Union_Id (Etype (Id)),
20956 Semantic => True);
20957
20958 -- First_Index
20959 -- Packed_Array_Impl_Type
20960
20961 if Is_Array_Type (Id) then
20962 if Present (First_Index (Id)) then
20963 Visit_Field
20964 (Field => Union_Id (List_Containing (First_Index (Id))),
20965 Semantic => True);
20966 end if;
20967
20968 if Is_Packed (Id) then
20969 Visit_Field
20970 (Field => Union_Id (Packed_Array_Impl_Type (Id)),
20971 Semantic => True);
20972 end if;
20973 end if;
20974
20975 -- Scalar_Range
20976
20977 if Is_Discrete_Type (Id) then
20978 Visit_Field
20979 (Field => Union_Id (Scalar_Range (Id)),
20980 Semantic => True);
20981 end if;
20982 end Visit_Semantic_Fields;
20983
20984 -- Start of processing for New_Copy_Tree
20985
20986 begin
20987 -- Routine New_Copy_Tree performs a deep copy of a subtree by creating
20988 -- shallow copies for each node within, and then updating the child and
20989 -- parent pointers accordingly. This process is straightforward, however
20990 -- the routine must deal with the following complications:
20991
20992 -- * Entities defined within N_Expression_With_Actions nodes must be
20993 -- replicated rather than shared to avoid introducing two identical
20994 -- symbols within the same scope. Note that no other expression can
20995 -- currently define entities.
20996
20997 -- do
20998 -- Source_Low : ...;
20999 -- Source_High : ...;
21000
21001 -- <reference to Source_Low>
21002 -- <reference to Source_High>
21003 -- in ... end;
21004
21005 -- New_Copy_Tree handles this case by first creating new entities
21006 -- and then updating all existing references to point to these new
21007 -- entities.
21008
21009 -- do
21010 -- New_Low : ...;
21011 -- New_High : ...;
21012
21013 -- <reference to New_Low>
21014 -- <reference to New_High>
21015 -- in ... end;
21016
21017 -- * Itypes defined within the subtree must be replicated to avoid any
21018 -- dependencies on invalid or inaccessible data.
21019
21020 -- subtype Source_Itype is ... range Source_Low .. Source_High;
21021
21022 -- New_Copy_Tree handles this case by first creating a new itype in
21023 -- the same fashion as entities, and then updating various relevant
21024 -- constraints.
21025
21026 -- subtype New_Itype is ... range New_Low .. New_High;
21027
21028 -- * The Associated_Node_For_Itype field of itypes must be updated to
21029 -- reference the proper replicated entity or node.
21030
21031 -- * Semantic fields of entities such as Etype and Scope must be
21032 -- updated to reference the proper replicated entities.
21033
21034 -- * Semantic fields of nodes such as First_Real_Statement must be
21035 -- updated to reference the proper replicated nodes.
21036
21037 -- To meet all these demands, routine New_Copy_Tree is split into two
21038 -- phases.
21039
21040 -- Phase 1 traverses the tree in order to locate entities and itypes
21041 -- defined within the subtree. New entities are generated and saved in
21042 -- table NCT_New_Entities. The semantic fields of all new entities and
21043 -- itypes are then updated accordingly.
21044
21045 -- Phase 2 traverses the tree in order to replicate each node. Various
21046 -- semantic fields of nodes and entities are updated accordingly.
21047
21048 -- Preparatory phase. Clear the contents of tables NCT_New_Entities and
21049 -- NCT_Pending_Itypes in case a previous call to New_Copy_Tree left some
21050 -- data inside.
21051
21052 if NCT_Tables_In_Use then
21053 NCT_Tables_In_Use := False;
21054
21055 NCT_New_Entities.Reset;
21056 NCT_Pending_Itypes.Reset;
21057 end if;
21058
21059 -- Populate tables NCT_New_Entities and NCT_Pending_Itypes with data
21060 -- supplied by a linear entity map. The tables offer faster access to
21061 -- the same data.
21062
21063 Build_NCT_Tables (Map);
21064
21065 -- Execute Phase 1. Traverse the subtree and generate new entities for
21066 -- the following cases:
21067
21068 -- * An entity defined within an N_Expression_With_Actions node
21069
21070 -- * An itype referenced within the subtree where the associated node
21071 -- is also in the subtree.
21072
21073 -- All new entities are accessible via table NCT_New_Entities, which
21074 -- contains mappings of the form:
21075
21076 -- Old_Entity -> New_Entity
21077 -- Old_Itype -> New_Itype
21078
21079 -- In addition, the associated nodes of all new itypes are mapped in
21080 -- table NCT_Pending_Itypes:
21081
21082 -- Assoc_Nod -> (New_Itype1, New_Itype2, .., New_ItypeN)
21083
21084 Visit_Any_Node (Source);
21085
21086 -- Update the semantic attributes of all new entities generated during
21087 -- Phase 1 before starting Phase 2. The updates could be performed in
21088 -- routine Corresponding_Entity, however this may cause the same entity
21089 -- to be updated multiple times, effectively generating useless nodes.
21090 -- Keeping the updates separates from Phase 2 ensures that only one set
21091 -- of attributes is generated for an entity at any one time.
21092
21093 Update_New_Entities (Map);
21094
21095 -- Execute Phase 2. Replicate the source subtree one node at a time.
21096 -- The following transformations take place:
21097
21098 -- * References to entities and itypes are updated to refer to the
21099 -- new entities and itypes generated during Phase 1.
21100
21101 -- * All Associated_Node_For_Itype attributes of itypes are updated
21102 -- to refer to the new replicated Associated_Node_For_Itype.
21103
21104 return Copy_Node_With_Replacement (Source);
21105 end New_Copy_Tree;
21106
21107 -------------------------
21108 -- New_External_Entity --
21109 -------------------------
21110
21111 function New_External_Entity
21112 (Kind : Entity_Kind;
21113 Scope_Id : Entity_Id;
21114 Sloc_Value : Source_Ptr;
21115 Related_Id : Entity_Id;
21116 Suffix : Character;
21117 Suffix_Index : Int := 0;
21118 Prefix : Character := ' ') return Entity_Id
21119 is
21120 N : constant Entity_Id :=
21121 Make_Defining_Identifier (Sloc_Value,
21122 New_External_Name
21123 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
21124
21125 begin
21126 Set_Ekind (N, Kind);
21127 Set_Is_Internal (N, True);
21128 Append_Entity (N, Scope_Id);
21129 Set_Public_Status (N);
21130
21131 if Kind in Type_Kind then
21132 Init_Size_Align (N);
21133 end if;
21134
21135 return N;
21136 end New_External_Entity;
21137
21138 -------------------------
21139 -- New_Internal_Entity --
21140 -------------------------
21141
21142 function New_Internal_Entity
21143 (Kind : Entity_Kind;
21144 Scope_Id : Entity_Id;
21145 Sloc_Value : Source_Ptr;
21146 Id_Char : Character) return Entity_Id
21147 is
21148 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
21149
21150 begin
21151 Set_Ekind (N, Kind);
21152 Set_Is_Internal (N, True);
21153 Append_Entity (N, Scope_Id);
21154
21155 if Kind in Type_Kind then
21156 Init_Size_Align (N);
21157 end if;
21158
21159 return N;
21160 end New_Internal_Entity;
21161
21162 -----------------
21163 -- Next_Actual --
21164 -----------------
21165
21166 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
21167 Par : constant Node_Id := Parent (Actual_Id);
21168 N : Node_Id;
21169
21170 begin
21171 -- If we are pointing at a positional parameter, it is a member of a
21172 -- node list (the list of parameters), and the next parameter is the
21173 -- next node on the list, unless we hit a parameter association, then
21174 -- we shift to using the chain whose head is the First_Named_Actual in
21175 -- the parent, and then is threaded using the Next_Named_Actual of the
21176 -- Parameter_Association. All this fiddling is because the original node
21177 -- list is in the textual call order, and what we need is the
21178 -- declaration order.
21179
21180 if Is_List_Member (Actual_Id) then
21181 N := Next (Actual_Id);
21182
21183 if Nkind (N) = N_Parameter_Association then
21184
21185 -- In case of a build-in-place call, the call will no longer be a
21186 -- call; it will have been rewritten.
21187
21188 if Nkind_In (Par, N_Entry_Call_Statement,
21189 N_Function_Call,
21190 N_Procedure_Call_Statement)
21191 then
21192 return First_Named_Actual (Par);
21193
21194 -- In case of a call rewritten in GNATprove mode while "inlining
21195 -- for proof" go to the original call.
21196
21197 elsif Nkind (Par) = N_Null_Statement then
21198 pragma Assert
21199 (GNATprove_Mode
21200 and then
21201 Nkind (Original_Node (Par)) in N_Subprogram_Call);
21202
21203 return First_Named_Actual (Original_Node (Par));
21204 else
21205 return Empty;
21206 end if;
21207 else
21208 return N;
21209 end if;
21210
21211 else
21212 return Next_Named_Actual (Parent (Actual_Id));
21213 end if;
21214 end Next_Actual;
21215
21216 procedure Next_Actual (Actual_Id : in out Node_Id) is
21217 begin
21218 Actual_Id := Next_Actual (Actual_Id);
21219 end Next_Actual;
21220
21221 -----------------
21222 -- Next_Global --
21223 -----------------
21224
21225 function Next_Global (Node : Node_Id) return Node_Id is
21226 begin
21227 -- The global item may either be in a list, or by itself, in which case
21228 -- there is no next global item with the same mode.
21229
21230 if Is_List_Member (Node) then
21231 return Next (Node);
21232 else
21233 return Empty;
21234 end if;
21235 end Next_Global;
21236
21237 procedure Next_Global (Node : in out Node_Id) is
21238 begin
21239 Node := Next_Global (Node);
21240 end Next_Global;
21241
21242 ----------------------------------
21243 -- New_Requires_Transient_Scope --
21244 ----------------------------------
21245
21246 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
21247 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
21248 -- This is called for untagged records and protected types, with
21249 -- nondefaulted discriminants. Returns True if the size of function
21250 -- results is known at the call site, False otherwise. Returns False
21251 -- if there is a variant part that depends on the discriminants of
21252 -- this type, or if there is an array constrained by the discriminants
21253 -- of this type. ???Currently, this is overly conservative (the array
21254 -- could be nested inside some other record that is constrained by
21255 -- nondiscriminants). That is, the recursive calls are too conservative.
21256
21257 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean;
21258 -- Returns True if Typ is a nonlimited record with defaulted
21259 -- discriminants whose max size makes it unsuitable for allocating on
21260 -- the primary stack.
21261
21262 ------------------------------
21263 -- Caller_Known_Size_Record --
21264 ------------------------------
21265
21266 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
21267 pragma Assert (Typ = Underlying_Type (Typ));
21268
21269 begin
21270 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
21271 return False;
21272 end if;
21273
21274 declare
21275 Comp : Entity_Id;
21276
21277 begin
21278 Comp := First_Entity (Typ);
21279 while Present (Comp) loop
21280
21281 -- Only look at E_Component entities. No need to look at
21282 -- E_Discriminant entities, and we must ignore internal
21283 -- subtypes generated for constrained components.
21284
21285 if Ekind (Comp) = E_Component then
21286 declare
21287 Comp_Type : constant Entity_Id :=
21288 Underlying_Type (Etype (Comp));
21289
21290 begin
21291 if Is_Record_Type (Comp_Type)
21292 or else
21293 Is_Protected_Type (Comp_Type)
21294 then
21295 if not Caller_Known_Size_Record (Comp_Type) then
21296 return False;
21297 end if;
21298
21299 elsif Is_Array_Type (Comp_Type) then
21300 if Size_Depends_On_Discriminant (Comp_Type) then
21301 return False;
21302 end if;
21303 end if;
21304 end;
21305 end if;
21306
21307 Next_Entity (Comp);
21308 end loop;
21309 end;
21310
21311 return True;
21312 end Caller_Known_Size_Record;
21313
21314 ------------------------------
21315 -- Large_Max_Size_Mutable --
21316 ------------------------------
21317
21318 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean is
21319 pragma Assert (Typ = Underlying_Type (Typ));
21320
21321 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean;
21322 -- Returns true if the discrete type T has a large range
21323
21324 ----------------------------
21325 -- Is_Large_Discrete_Type --
21326 ----------------------------
21327
21328 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean is
21329 Threshold : constant Int := 16;
21330 -- Arbitrary threshold above which we consider it "large". We want
21331 -- a fairly large threshold, because these large types really
21332 -- shouldn't have default discriminants in the first place, in
21333 -- most cases.
21334
21335 begin
21336 return UI_To_Int (RM_Size (T)) > Threshold;
21337 end Is_Large_Discrete_Type;
21338
21339 -- Start of processing for Large_Max_Size_Mutable
21340
21341 begin
21342 if Is_Record_Type (Typ)
21343 and then not Is_Limited_View (Typ)
21344 and then Has_Defaulted_Discriminants (Typ)
21345 then
21346 -- Loop through the components, looking for an array whose upper
21347 -- bound(s) depends on discriminants, where both the subtype of
21348 -- the discriminant and the index subtype are too large.
21349
21350 declare
21351 Comp : Entity_Id;
21352
21353 begin
21354 Comp := First_Entity (Typ);
21355 while Present (Comp) loop
21356 if Ekind (Comp) = E_Component then
21357 declare
21358 Comp_Type : constant Entity_Id :=
21359 Underlying_Type (Etype (Comp));
21360
21361 Hi : Node_Id;
21362 Indx : Node_Id;
21363 Ityp : Entity_Id;
21364
21365 begin
21366 if Is_Array_Type (Comp_Type) then
21367 Indx := First_Index (Comp_Type);
21368
21369 while Present (Indx) loop
21370 Ityp := Etype (Indx);
21371 Hi := Type_High_Bound (Ityp);
21372
21373 if Nkind (Hi) = N_Identifier
21374 and then Ekind (Entity (Hi)) = E_Discriminant
21375 and then Is_Large_Discrete_Type (Ityp)
21376 and then Is_Large_Discrete_Type
21377 (Etype (Entity (Hi)))
21378 then
21379 return True;
21380 end if;
21381
21382 Next_Index (Indx);
21383 end loop;
21384 end if;
21385 end;
21386 end if;
21387
21388 Next_Entity (Comp);
21389 end loop;
21390 end;
21391 end if;
21392
21393 return False;
21394 end Large_Max_Size_Mutable;
21395
21396 -- Local declarations
21397
21398 Typ : constant Entity_Id := Underlying_Type (Id);
21399
21400 -- Start of processing for New_Requires_Transient_Scope
21401
21402 begin
21403 -- This is a private type which is not completed yet. This can only
21404 -- happen in a default expression (of a formal parameter or of a
21405 -- record component). Do not expand transient scope in this case.
21406
21407 if No (Typ) then
21408 return False;
21409
21410 -- Do not expand transient scope for non-existent procedure return or
21411 -- string literal types.
21412
21413 elsif Typ = Standard_Void_Type
21414 or else Ekind (Typ) = E_String_Literal_Subtype
21415 then
21416 return False;
21417
21418 -- If Typ is a generic formal incomplete type, then we want to look at
21419 -- the actual type.
21420
21421 elsif Ekind (Typ) = E_Record_Subtype
21422 and then Present (Cloned_Subtype (Typ))
21423 then
21424 return New_Requires_Transient_Scope (Cloned_Subtype (Typ));
21425
21426 -- Functions returning specific tagged types may dispatch on result, so
21427 -- their returned value is allocated on the secondary stack, even in the
21428 -- definite case. We must treat nondispatching functions the same way,
21429 -- because access-to-function types can point at both, so the calling
21430 -- conventions must be compatible. Is_Tagged_Type includes controlled
21431 -- types and class-wide types. Controlled type temporaries need
21432 -- finalization.
21433
21434 -- ???It's not clear why we need to return noncontrolled types with
21435 -- controlled components on the secondary stack.
21436
21437 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
21438 return True;
21439
21440 -- Untagged definite subtypes are known size. This includes all
21441 -- elementary [sub]types. Tasks are known size even if they have
21442 -- discriminants. So we return False here, with one exception:
21443 -- For a type like:
21444 -- type T (Last : Natural := 0) is
21445 -- X : String (1 .. Last);
21446 -- end record;
21447 -- we return True. That's because for "P(F(...));", where F returns T,
21448 -- we don't know the size of the result at the call site, so if we
21449 -- allocated it on the primary stack, we would have to allocate the
21450 -- maximum size, which is way too big.
21451
21452 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
21453 return Large_Max_Size_Mutable (Typ);
21454
21455 -- Indefinite (discriminated) untagged record or protected type
21456
21457 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
21458 return not Caller_Known_Size_Record (Typ);
21459
21460 -- Unconstrained array
21461
21462 else
21463 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
21464 return True;
21465 end if;
21466 end New_Requires_Transient_Scope;
21467
21468 --------------------------
21469 -- No_Heap_Finalization --
21470 --------------------------
21471
21472 function No_Heap_Finalization (Typ : Entity_Id) return Boolean is
21473 begin
21474 if Ekind_In (Typ, E_Access_Type, E_General_Access_Type)
21475 and then Is_Library_Level_Entity (Typ)
21476 then
21477 -- A global No_Heap_Finalization pragma applies to all library-level
21478 -- named access-to-object types.
21479
21480 if Present (No_Heap_Finalization_Pragma) then
21481 return True;
21482
21483 -- The library-level named access-to-object type itself is subject to
21484 -- pragma No_Heap_Finalization.
21485
21486 elsif Present (Get_Pragma (Typ, Pragma_No_Heap_Finalization)) then
21487 return True;
21488 end if;
21489 end if;
21490
21491 return False;
21492 end No_Heap_Finalization;
21493
21494 -----------------------
21495 -- Normalize_Actuals --
21496 -----------------------
21497
21498 -- Chain actuals according to formals of subprogram. If there are no named
21499 -- associations, the chain is simply the list of Parameter Associations,
21500 -- since the order is the same as the declaration order. If there are named
21501 -- associations, then the First_Named_Actual field in the N_Function_Call
21502 -- or N_Procedure_Call_Statement node points to the Parameter_Association
21503 -- node for the parameter that comes first in declaration order. The
21504 -- remaining named parameters are then chained in declaration order using
21505 -- Next_Named_Actual.
21506
21507 -- This routine also verifies that the number of actuals is compatible with
21508 -- the number and default values of formals, but performs no type checking
21509 -- (type checking is done by the caller).
21510
21511 -- If the matching succeeds, Success is set to True and the caller proceeds
21512 -- with type-checking. If the match is unsuccessful, then Success is set to
21513 -- False, and the caller attempts a different interpretation, if there is
21514 -- one.
21515
21516 -- If the flag Report is on, the call is not overloaded, and a failure to
21517 -- match can be reported here, rather than in the caller.
21518
21519 procedure Normalize_Actuals
21520 (N : Node_Id;
21521 S : Entity_Id;
21522 Report : Boolean;
21523 Success : out Boolean)
21524 is
21525 Actuals : constant List_Id := Parameter_Associations (N);
21526 Actual : Node_Id := Empty;
21527 Formal : Entity_Id;
21528 Last : Node_Id := Empty;
21529 First_Named : Node_Id := Empty;
21530 Found : Boolean;
21531
21532 Formals_To_Match : Integer := 0;
21533 Actuals_To_Match : Integer := 0;
21534
21535 procedure Chain (A : Node_Id);
21536 -- Add named actual at the proper place in the list, using the
21537 -- Next_Named_Actual link.
21538
21539 function Reporting return Boolean;
21540 -- Determines if an error is to be reported. To report an error, we
21541 -- need Report to be True, and also we do not report errors caused
21542 -- by calls to init procs that occur within other init procs. Such
21543 -- errors must always be cascaded errors, since if all the types are
21544 -- declared correctly, the compiler will certainly build decent calls.
21545
21546 -----------
21547 -- Chain --
21548 -----------
21549
21550 procedure Chain (A : Node_Id) is
21551 begin
21552 if No (Last) then
21553
21554 -- Call node points to first actual in list
21555
21556 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
21557
21558 else
21559 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
21560 end if;
21561
21562 Last := A;
21563 Set_Next_Named_Actual (Last, Empty);
21564 end Chain;
21565
21566 ---------------
21567 -- Reporting --
21568 ---------------
21569
21570 function Reporting return Boolean is
21571 begin
21572 if not Report then
21573 return False;
21574
21575 elsif not Within_Init_Proc then
21576 return True;
21577
21578 elsif Is_Init_Proc (Entity (Name (N))) then
21579 return False;
21580
21581 else
21582 return True;
21583 end if;
21584 end Reporting;
21585
21586 -- Start of processing for Normalize_Actuals
21587
21588 begin
21589 if Is_Access_Type (S) then
21590
21591 -- The name in the call is a function call that returns an access
21592 -- to subprogram. The designated type has the list of formals.
21593
21594 Formal := First_Formal (Designated_Type (S));
21595 else
21596 Formal := First_Formal (S);
21597 end if;
21598
21599 while Present (Formal) loop
21600 Formals_To_Match := Formals_To_Match + 1;
21601 Next_Formal (Formal);
21602 end loop;
21603
21604 -- Find if there is a named association, and verify that no positional
21605 -- associations appear after named ones.
21606
21607 if Present (Actuals) then
21608 Actual := First (Actuals);
21609 end if;
21610
21611 while Present (Actual)
21612 and then Nkind (Actual) /= N_Parameter_Association
21613 loop
21614 Actuals_To_Match := Actuals_To_Match + 1;
21615 Next (Actual);
21616 end loop;
21617
21618 if No (Actual) and Actuals_To_Match = Formals_To_Match then
21619
21620 -- Most common case: positional notation, no defaults
21621
21622 Success := True;
21623 return;
21624
21625 elsif Actuals_To_Match > Formals_To_Match then
21626
21627 -- Too many actuals: will not work
21628
21629 if Reporting then
21630 if Is_Entity_Name (Name (N)) then
21631 Error_Msg_N ("too many arguments in call to&", Name (N));
21632 else
21633 Error_Msg_N ("too many arguments in call", N);
21634 end if;
21635 end if;
21636
21637 Success := False;
21638 return;
21639 end if;
21640
21641 First_Named := Actual;
21642
21643 while Present (Actual) loop
21644 if Nkind (Actual) /= N_Parameter_Association then
21645 Error_Msg_N
21646 ("positional parameters not allowed after named ones", Actual);
21647 Success := False;
21648 return;
21649
21650 else
21651 Actuals_To_Match := Actuals_To_Match + 1;
21652 end if;
21653
21654 Next (Actual);
21655 end loop;
21656
21657 if Present (Actuals) then
21658 Actual := First (Actuals);
21659 end if;
21660
21661 Formal := First_Formal (S);
21662 while Present (Formal) loop
21663
21664 -- Match the formals in order. If the corresponding actual is
21665 -- positional, nothing to do. Else scan the list of named actuals
21666 -- to find the one with the right name.
21667
21668 if Present (Actual)
21669 and then Nkind (Actual) /= N_Parameter_Association
21670 then
21671 Next (Actual);
21672 Actuals_To_Match := Actuals_To_Match - 1;
21673 Formals_To_Match := Formals_To_Match - 1;
21674
21675 else
21676 -- For named parameters, search the list of actuals to find
21677 -- one that matches the next formal name.
21678
21679 Actual := First_Named;
21680 Found := False;
21681 while Present (Actual) loop
21682 if Chars (Selector_Name (Actual)) = Chars (Formal) then
21683 Found := True;
21684 Chain (Actual);
21685 Actuals_To_Match := Actuals_To_Match - 1;
21686 Formals_To_Match := Formals_To_Match - 1;
21687 exit;
21688 end if;
21689
21690 Next (Actual);
21691 end loop;
21692
21693 if not Found then
21694 if Ekind (Formal) /= E_In_Parameter
21695 or else No (Default_Value (Formal))
21696 then
21697 if Reporting then
21698 if (Comes_From_Source (S)
21699 or else Sloc (S) = Standard_Location)
21700 and then Is_Overloadable (S)
21701 then
21702 if No (Actuals)
21703 and then
21704 Nkind_In (Parent (N), N_Procedure_Call_Statement,
21705 N_Function_Call,
21706 N_Parameter_Association)
21707 and then Ekind (S) /= E_Function
21708 then
21709 Set_Etype (N, Etype (S));
21710
21711 else
21712 Error_Msg_Name_1 := Chars (S);
21713 Error_Msg_Sloc := Sloc (S);
21714 Error_Msg_NE
21715 ("missing argument for parameter & "
21716 & "in call to % declared #", N, Formal);
21717 end if;
21718
21719 elsif Is_Overloadable (S) then
21720 Error_Msg_Name_1 := Chars (S);
21721
21722 -- Point to type derivation that generated the
21723 -- operation.
21724
21725 Error_Msg_Sloc := Sloc (Parent (S));
21726
21727 Error_Msg_NE
21728 ("missing argument for parameter & "
21729 & "in call to % (inherited) #", N, Formal);
21730
21731 else
21732 Error_Msg_NE
21733 ("missing argument for parameter &", N, Formal);
21734 end if;
21735 end if;
21736
21737 Success := False;
21738 return;
21739
21740 else
21741 Formals_To_Match := Formals_To_Match - 1;
21742 end if;
21743 end if;
21744 end if;
21745
21746 Next_Formal (Formal);
21747 end loop;
21748
21749 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
21750 Success := True;
21751 return;
21752
21753 else
21754 if Reporting then
21755
21756 -- Find some superfluous named actual that did not get
21757 -- attached to the list of associations.
21758
21759 Actual := First (Actuals);
21760 while Present (Actual) loop
21761 if Nkind (Actual) = N_Parameter_Association
21762 and then Actual /= Last
21763 and then No (Next_Named_Actual (Actual))
21764 then
21765 -- A validity check may introduce a copy of a call that
21766 -- includes an extra actual (for example for an unrelated
21767 -- accessibility check). Check that the extra actual matches
21768 -- some extra formal, which must exist already because
21769 -- subprogram must be frozen at this point.
21770
21771 if Present (Extra_Formals (S))
21772 and then not Comes_From_Source (Actual)
21773 and then Nkind (Actual) = N_Parameter_Association
21774 and then Chars (Extra_Formals (S)) =
21775 Chars (Selector_Name (Actual))
21776 then
21777 null;
21778 else
21779 Error_Msg_N
21780 ("unmatched actual & in call", Selector_Name (Actual));
21781 exit;
21782 end if;
21783 end if;
21784
21785 Next (Actual);
21786 end loop;
21787 end if;
21788
21789 Success := False;
21790 return;
21791 end if;
21792 end Normalize_Actuals;
21793
21794 --------------------------------
21795 -- Note_Possible_Modification --
21796 --------------------------------
21797
21798 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
21799 Modification_Comes_From_Source : constant Boolean :=
21800 Comes_From_Source (Parent (N));
21801
21802 Ent : Entity_Id;
21803 Exp : Node_Id;
21804
21805 begin
21806 -- Loop to find referenced entity, if there is one
21807
21808 Exp := N;
21809 loop
21810 Ent := Empty;
21811
21812 if Is_Entity_Name (Exp) then
21813 Ent := Entity (Exp);
21814
21815 -- If the entity is missing, it is an undeclared identifier,
21816 -- and there is nothing to annotate.
21817
21818 if No (Ent) then
21819 return;
21820 end if;
21821
21822 elsif Nkind (Exp) = N_Explicit_Dereference then
21823 declare
21824 P : constant Node_Id := Prefix (Exp);
21825
21826 begin
21827 -- In formal verification mode, keep track of all reads and
21828 -- writes through explicit dereferences.
21829
21830 if GNATprove_Mode then
21831 SPARK_Specific.Generate_Dereference (N, 'm');
21832 end if;
21833
21834 if Nkind (P) = N_Selected_Component
21835 and then Present (Entry_Formal (Entity (Selector_Name (P))))
21836 then
21837 -- Case of a reference to an entry formal
21838
21839 Ent := Entry_Formal (Entity (Selector_Name (P)));
21840
21841 elsif Nkind (P) = N_Identifier
21842 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
21843 and then Present (Expression (Parent (Entity (P))))
21844 and then Nkind (Expression (Parent (Entity (P)))) =
21845 N_Reference
21846 then
21847 -- Case of a reference to a value on which side effects have
21848 -- been removed.
21849
21850 Exp := Prefix (Expression (Parent (Entity (P))));
21851 goto Continue;
21852
21853 else
21854 return;
21855 end if;
21856 end;
21857
21858 elsif Nkind_In (Exp, N_Type_Conversion,
21859 N_Unchecked_Type_Conversion)
21860 then
21861 Exp := Expression (Exp);
21862 goto Continue;
21863
21864 elsif Nkind_In (Exp, N_Slice,
21865 N_Indexed_Component,
21866 N_Selected_Component)
21867 then
21868 -- Special check, if the prefix is an access type, then return
21869 -- since we are modifying the thing pointed to, not the prefix.
21870 -- When we are expanding, most usually the prefix is replaced
21871 -- by an explicit dereference, and this test is not needed, but
21872 -- in some cases (notably -gnatc mode and generics) when we do
21873 -- not do full expansion, we need this special test.
21874
21875 if Is_Access_Type (Etype (Prefix (Exp))) then
21876 return;
21877
21878 -- Otherwise go to prefix and keep going
21879
21880 else
21881 Exp := Prefix (Exp);
21882 goto Continue;
21883 end if;
21884
21885 -- All other cases, not a modification
21886
21887 else
21888 return;
21889 end if;
21890
21891 -- Now look for entity being referenced
21892
21893 if Present (Ent) then
21894 if Is_Object (Ent) then
21895 if Comes_From_Source (Exp)
21896 or else Modification_Comes_From_Source
21897 then
21898 -- Give warning if pragma unmodified is given and we are
21899 -- sure this is a modification.
21900
21901 if Has_Pragma_Unmodified (Ent) and then Sure then
21902
21903 -- Note that the entity may be present only as a result
21904 -- of pragma Unused.
21905
21906 if Has_Pragma_Unused (Ent) then
21907 Error_Msg_NE ("??pragma Unused given for &!", N, Ent);
21908 else
21909 Error_Msg_NE
21910 ("??pragma Unmodified given for &!", N, Ent);
21911 end if;
21912 end if;
21913
21914 Set_Never_Set_In_Source (Ent, False);
21915 end if;
21916
21917 Set_Is_True_Constant (Ent, False);
21918 Set_Current_Value (Ent, Empty);
21919 Set_Is_Known_Null (Ent, False);
21920
21921 if not Can_Never_Be_Null (Ent) then
21922 Set_Is_Known_Non_Null (Ent, False);
21923 end if;
21924
21925 -- Follow renaming chain
21926
21927 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
21928 and then Present (Renamed_Object (Ent))
21929 then
21930 Exp := Renamed_Object (Ent);
21931
21932 -- If the entity is the loop variable in an iteration over
21933 -- a container, retrieve container expression to indicate
21934 -- possible modification.
21935
21936 if Present (Related_Expression (Ent))
21937 and then Nkind (Parent (Related_Expression (Ent))) =
21938 N_Iterator_Specification
21939 then
21940 Exp := Original_Node (Related_Expression (Ent));
21941 end if;
21942
21943 goto Continue;
21944
21945 -- The expression may be the renaming of a subcomponent of an
21946 -- array or container. The assignment to the subcomponent is
21947 -- a modification of the container.
21948
21949 elsif Comes_From_Source (Original_Node (Exp))
21950 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
21951 N_Indexed_Component)
21952 then
21953 Exp := Prefix (Original_Node (Exp));
21954 goto Continue;
21955 end if;
21956
21957 -- Generate a reference only if the assignment comes from
21958 -- source. This excludes, for example, calls to a dispatching
21959 -- assignment operation when the left-hand side is tagged. In
21960 -- GNATprove mode, we need those references also on generated
21961 -- code, as these are used to compute the local effects of
21962 -- subprograms.
21963
21964 if Modification_Comes_From_Source or GNATprove_Mode then
21965 Generate_Reference (Ent, Exp, 'm');
21966
21967 -- If the target of the assignment is the bound variable
21968 -- in an iterator, indicate that the corresponding array
21969 -- or container is also modified.
21970
21971 if Ada_Version >= Ada_2012
21972 and then Nkind (Parent (Ent)) = N_Iterator_Specification
21973 then
21974 declare
21975 Domain : constant Node_Id := Name (Parent (Ent));
21976
21977 begin
21978 -- TBD : in the full version of the construct, the
21979 -- domain of iteration can be given by an expression.
21980
21981 if Is_Entity_Name (Domain) then
21982 Generate_Reference (Entity (Domain), Exp, 'm');
21983 Set_Is_True_Constant (Entity (Domain), False);
21984 Set_Never_Set_In_Source (Entity (Domain), False);
21985 end if;
21986 end;
21987 end if;
21988 end if;
21989 end if;
21990
21991 Kill_Checks (Ent);
21992
21993 -- If we are sure this is a modification from source, and we know
21994 -- this modifies a constant, then give an appropriate warning.
21995
21996 if Sure
21997 and then Modification_Comes_From_Source
21998 and then Overlays_Constant (Ent)
21999 and then Address_Clause_Overlay_Warnings
22000 then
22001 declare
22002 Addr : constant Node_Id := Address_Clause (Ent);
22003 O_Ent : Entity_Id;
22004 Off : Boolean;
22005
22006 begin
22007 Find_Overlaid_Entity (Addr, O_Ent, Off);
22008
22009 Error_Msg_Sloc := Sloc (Addr);
22010 Error_Msg_NE
22011 ("??constant& may be modified via address clause#",
22012 N, O_Ent);
22013 end;
22014 end if;
22015
22016 return;
22017 end if;
22018
22019 <<Continue>>
22020 null;
22021 end loop;
22022 end Note_Possible_Modification;
22023
22024 -----------------
22025 -- Null_Status --
22026 -----------------
22027
22028 function Null_Status (N : Node_Id) return Null_Status_Kind is
22029 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean;
22030 -- Determine whether definition Def carries a null exclusion
22031
22032 function Null_Status_Of_Entity (Id : Entity_Id) return Null_Status_Kind;
22033 -- Determine the null status of arbitrary entity Id
22034
22035 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind;
22036 -- Determine the null status of type Typ
22037
22038 ---------------------------
22039 -- Is_Null_Excluding_Def --
22040 ---------------------------
22041
22042 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean is
22043 begin
22044 return
22045 Nkind_In (Def, N_Access_Definition,
22046 N_Access_Function_Definition,
22047 N_Access_Procedure_Definition,
22048 N_Access_To_Object_Definition,
22049 N_Component_Definition,
22050 N_Derived_Type_Definition)
22051 and then Null_Exclusion_Present (Def);
22052 end Is_Null_Excluding_Def;
22053
22054 ---------------------------
22055 -- Null_Status_Of_Entity --
22056 ---------------------------
22057
22058 function Null_Status_Of_Entity
22059 (Id : Entity_Id) return Null_Status_Kind
22060 is
22061 Decl : constant Node_Id := Declaration_Node (Id);
22062 Def : Node_Id;
22063
22064 begin
22065 -- The value of an imported or exported entity may be set externally
22066 -- regardless of a null exclusion. As a result, the value cannot be
22067 -- determined statically.
22068
22069 if Is_Imported (Id) or else Is_Exported (Id) then
22070 return Unknown;
22071
22072 elsif Nkind_In (Decl, N_Component_Declaration,
22073 N_Discriminant_Specification,
22074 N_Formal_Object_Declaration,
22075 N_Object_Declaration,
22076 N_Object_Renaming_Declaration,
22077 N_Parameter_Specification)
22078 then
22079 -- A component declaration yields a non-null value when either
22080 -- its component definition or access definition carries a null
22081 -- exclusion.
22082
22083 if Nkind (Decl) = N_Component_Declaration then
22084 Def := Component_Definition (Decl);
22085
22086 if Is_Null_Excluding_Def (Def) then
22087 return Is_Non_Null;
22088 end if;
22089
22090 Def := Access_Definition (Def);
22091
22092 if Present (Def) and then Is_Null_Excluding_Def (Def) then
22093 return Is_Non_Null;
22094 end if;
22095
22096 -- A formal object declaration yields a non-null value if its
22097 -- access definition carries a null exclusion. If the object is
22098 -- default initialized, then the value depends on the expression.
22099
22100 elsif Nkind (Decl) = N_Formal_Object_Declaration then
22101 Def := Access_Definition (Decl);
22102
22103 if Present (Def) and then Is_Null_Excluding_Def (Def) then
22104 return Is_Non_Null;
22105 end if;
22106
22107 -- A constant may yield a null or non-null value depending on its
22108 -- initialization expression.
22109
22110 elsif Ekind (Id) = E_Constant then
22111 return Null_Status (Constant_Value (Id));
22112
22113 -- The construct yields a non-null value when it has a null
22114 -- exclusion.
22115
22116 elsif Null_Exclusion_Present (Decl) then
22117 return Is_Non_Null;
22118
22119 -- An object renaming declaration yields a non-null value if its
22120 -- access definition carries a null exclusion. Otherwise the value
22121 -- depends on the renamed name.
22122
22123 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
22124 Def := Access_Definition (Decl);
22125
22126 if Present (Def) and then Is_Null_Excluding_Def (Def) then
22127 return Is_Non_Null;
22128
22129 else
22130 return Null_Status (Name (Decl));
22131 end if;
22132 end if;
22133 end if;
22134
22135 -- At this point the declaration of the entity does not carry a null
22136 -- exclusion and lacks an initialization expression. Check the status
22137 -- of its type.
22138
22139 return Null_Status_Of_Type (Etype (Id));
22140 end Null_Status_Of_Entity;
22141
22142 -------------------------
22143 -- Null_Status_Of_Type --
22144 -------------------------
22145
22146 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind is
22147 Curr : Entity_Id;
22148 Decl : Node_Id;
22149
22150 begin
22151 -- Traverse the type chain looking for types with null exclusion
22152
22153 Curr := Typ;
22154 while Present (Curr) and then Etype (Curr) /= Curr loop
22155 Decl := Parent (Curr);
22156
22157 -- Guard against itypes which do not always have declarations. A
22158 -- type yields a non-null value if it carries a null exclusion.
22159
22160 if Present (Decl) then
22161 if Nkind (Decl) = N_Full_Type_Declaration
22162 and then Is_Null_Excluding_Def (Type_Definition (Decl))
22163 then
22164 return Is_Non_Null;
22165
22166 elsif Nkind (Decl) = N_Subtype_Declaration
22167 and then Null_Exclusion_Present (Decl)
22168 then
22169 return Is_Non_Null;
22170 end if;
22171 end if;
22172
22173 Curr := Etype (Curr);
22174 end loop;
22175
22176 -- The type chain does not contain any null excluding types
22177
22178 return Unknown;
22179 end Null_Status_Of_Type;
22180
22181 -- Start of processing for Null_Status
22182
22183 begin
22184 -- An allocator always creates a non-null value
22185
22186 if Nkind (N) = N_Allocator then
22187 return Is_Non_Null;
22188
22189 -- Taking the 'Access of something yields a non-null value
22190
22191 elsif Nkind (N) = N_Attribute_Reference
22192 and then Nam_In (Attribute_Name (N), Name_Access,
22193 Name_Unchecked_Access,
22194 Name_Unrestricted_Access)
22195 then
22196 return Is_Non_Null;
22197
22198 -- "null" yields null
22199
22200 elsif Nkind (N) = N_Null then
22201 return Is_Null;
22202
22203 -- Check the status of the operand of a type conversion
22204
22205 elsif Nkind (N) = N_Type_Conversion then
22206 return Null_Status (Expression (N));
22207
22208 -- The input denotes a reference to an entity. Determine whether the
22209 -- entity or its type yields a null or non-null value.
22210
22211 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
22212 return Null_Status_Of_Entity (Entity (N));
22213 end if;
22214
22215 -- Otherwise it is not possible to determine the null status of the
22216 -- subexpression at compile time without resorting to simple flow
22217 -- analysis.
22218
22219 return Unknown;
22220 end Null_Status;
22221
22222 --------------------------------------
22223 -- Null_To_Null_Address_Convert_OK --
22224 --------------------------------------
22225
22226 function Null_To_Null_Address_Convert_OK
22227 (N : Node_Id;
22228 Typ : Entity_Id := Empty) return Boolean
22229 is
22230 begin
22231 if not Relaxed_RM_Semantics then
22232 return False;
22233 end if;
22234
22235 if Nkind (N) = N_Null then
22236 return Present (Typ) and then Is_Descendant_Of_Address (Typ);
22237
22238 elsif Nkind_In (N, N_Op_Eq, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt, N_Op_Ne)
22239 then
22240 declare
22241 L : constant Node_Id := Left_Opnd (N);
22242 R : constant Node_Id := Right_Opnd (N);
22243
22244 begin
22245 -- We check the Etype of the complementary operand since the
22246 -- N_Null node is not decorated at this stage.
22247
22248 return
22249 ((Nkind (L) = N_Null
22250 and then Is_Descendant_Of_Address (Etype (R)))
22251 or else
22252 (Nkind (R) = N_Null
22253 and then Is_Descendant_Of_Address (Etype (L))));
22254 end;
22255 end if;
22256
22257 return False;
22258 end Null_To_Null_Address_Convert_OK;
22259
22260 ---------------------------------
22261 -- Number_Of_Elements_In_Array --
22262 ---------------------------------
22263
22264 function Number_Of_Elements_In_Array (T : Entity_Id) return Int is
22265 Indx : Node_Id;
22266 Typ : Entity_Id;
22267 Low : Node_Id;
22268 High : Node_Id;
22269 Num : Int := 1;
22270
22271 begin
22272 pragma Assert (Is_Array_Type (T));
22273
22274 Indx := First_Index (T);
22275 while Present (Indx) loop
22276 Typ := Underlying_Type (Etype (Indx));
22277
22278 -- Never look at junk bounds of a generic type
22279
22280 if Is_Generic_Type (Typ) then
22281 return 0;
22282 end if;
22283
22284 -- Check the array bounds are known at compile time and return zero
22285 -- if they are not.
22286
22287 Low := Type_Low_Bound (Typ);
22288 High := Type_High_Bound (Typ);
22289
22290 if not Compile_Time_Known_Value (Low) then
22291 return 0;
22292 elsif not Compile_Time_Known_Value (High) then
22293 return 0;
22294 else
22295 Num :=
22296 Num * UI_To_Int ((Expr_Value (High) - Expr_Value (Low) + 1));
22297 end if;
22298
22299 Next_Index (Indx);
22300 end loop;
22301
22302 return Num;
22303 end Number_Of_Elements_In_Array;
22304
22305 -------------------------
22306 -- Object_Access_Level --
22307 -------------------------
22308
22309 -- Returns the static accessibility level of the view denoted by Obj. Note
22310 -- that the value returned is the result of a call to Scope_Depth. Only
22311 -- scope depths associated with dynamic scopes can actually be returned.
22312 -- Since only relative levels matter for accessibility checking, the fact
22313 -- that the distance between successive levels of accessibility is not
22314 -- always one is immaterial (invariant: if level(E2) is deeper than
22315 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
22316
22317 function Object_Access_Level (Obj : Node_Id) return Uint is
22318 function Is_Interface_Conversion (N : Node_Id) return Boolean;
22319 -- Determine whether N is a construct of the form
22320 -- Some_Type (Operand._tag'Address)
22321 -- This construct appears in the context of dispatching calls.
22322
22323 function Reference_To (Obj : Node_Id) return Node_Id;
22324 -- An explicit dereference is created when removing side effects from
22325 -- expressions for constraint checking purposes. In this case a local
22326 -- access type is created for it. The correct access level is that of
22327 -- the original source node. We detect this case by noting that the
22328 -- prefix of the dereference is created by an object declaration whose
22329 -- initial expression is a reference.
22330
22331 -----------------------------
22332 -- Is_Interface_Conversion --
22333 -----------------------------
22334
22335 function Is_Interface_Conversion (N : Node_Id) return Boolean is
22336 begin
22337 return Nkind (N) = N_Unchecked_Type_Conversion
22338 and then Nkind (Expression (N)) = N_Attribute_Reference
22339 and then Attribute_Name (Expression (N)) = Name_Address;
22340 end Is_Interface_Conversion;
22341
22342 ------------------
22343 -- Reference_To --
22344 ------------------
22345
22346 function Reference_To (Obj : Node_Id) return Node_Id is
22347 Pref : constant Node_Id := Prefix (Obj);
22348 begin
22349 if Is_Entity_Name (Pref)
22350 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
22351 and then Present (Expression (Parent (Entity (Pref))))
22352 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
22353 then
22354 return (Prefix (Expression (Parent (Entity (Pref)))));
22355 else
22356 return Empty;
22357 end if;
22358 end Reference_To;
22359
22360 -- Local variables
22361
22362 E : Entity_Id;
22363
22364 -- Start of processing for Object_Access_Level
22365
22366 begin
22367 if Nkind (Obj) = N_Defining_Identifier
22368 or else Is_Entity_Name (Obj)
22369 then
22370 if Nkind (Obj) = N_Defining_Identifier then
22371 E := Obj;
22372 else
22373 E := Entity (Obj);
22374 end if;
22375
22376 if Is_Prival (E) then
22377 E := Prival_Link (E);
22378 end if;
22379
22380 -- If E is a type then it denotes a current instance. For this case
22381 -- we add one to the normal accessibility level of the type to ensure
22382 -- that current instances are treated as always being deeper than
22383 -- than the level of any visible named access type (see 3.10.2(21)).
22384
22385 if Is_Type (E) then
22386 return Type_Access_Level (E) + 1;
22387
22388 elsif Present (Renamed_Object (E)) then
22389 return Object_Access_Level (Renamed_Object (E));
22390
22391 -- Similarly, if E is a component of the current instance of a
22392 -- protected type, any instance of it is assumed to be at a deeper
22393 -- level than the type. For a protected object (whose type is an
22394 -- anonymous protected type) its components are at the same level
22395 -- as the type itself.
22396
22397 elsif not Is_Overloadable (E)
22398 and then Ekind (Scope (E)) = E_Protected_Type
22399 and then Comes_From_Source (Scope (E))
22400 then
22401 return Type_Access_Level (Scope (E)) + 1;
22402
22403 else
22404 -- Aliased formals of functions take their access level from the
22405 -- point of call, i.e. require a dynamic check. For static check
22406 -- purposes, this is smaller than the level of the subprogram
22407 -- itself. For procedures the aliased makes no difference.
22408
22409 if Is_Formal (E)
22410 and then Is_Aliased (E)
22411 and then Ekind (Scope (E)) = E_Function
22412 then
22413 return Type_Access_Level (Etype (E));
22414
22415 else
22416 return Scope_Depth (Enclosing_Dynamic_Scope (E));
22417 end if;
22418 end if;
22419
22420 elsif Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
22421 if Is_Access_Type (Etype (Prefix (Obj))) then
22422 return Type_Access_Level (Etype (Prefix (Obj)));
22423 else
22424 return Object_Access_Level (Prefix (Obj));
22425 end if;
22426
22427 elsif Nkind (Obj) = N_Explicit_Dereference then
22428
22429 -- If the prefix is a selected access discriminant then we make a
22430 -- recursive call on the prefix, which will in turn check the level
22431 -- of the prefix object of the selected discriminant.
22432
22433 -- In Ada 2012, if the discriminant has implicit dereference and
22434 -- the context is a selected component, treat this as an object of
22435 -- unknown scope (see below). This is necessary in compile-only mode;
22436 -- otherwise expansion will already have transformed the prefix into
22437 -- a temporary.
22438
22439 if Nkind (Prefix (Obj)) = N_Selected_Component
22440 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
22441 and then
22442 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
22443 and then
22444 (not Has_Implicit_Dereference
22445 (Entity (Selector_Name (Prefix (Obj))))
22446 or else Nkind (Parent (Obj)) /= N_Selected_Component)
22447 then
22448 return Object_Access_Level (Prefix (Obj));
22449
22450 -- Detect an interface conversion in the context of a dispatching
22451 -- call. Use the original form of the conversion to find the access
22452 -- level of the operand.
22453
22454 elsif Is_Interface (Etype (Obj))
22455 and then Is_Interface_Conversion (Prefix (Obj))
22456 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
22457 then
22458 return Object_Access_Level (Original_Node (Obj));
22459
22460 elsif not Comes_From_Source (Obj) then
22461 declare
22462 Ref : constant Node_Id := Reference_To (Obj);
22463 begin
22464 if Present (Ref) then
22465 return Object_Access_Level (Ref);
22466 else
22467 return Type_Access_Level (Etype (Prefix (Obj)));
22468 end if;
22469 end;
22470
22471 else
22472 return Type_Access_Level (Etype (Prefix (Obj)));
22473 end if;
22474
22475 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
22476 return Object_Access_Level (Expression (Obj));
22477
22478 elsif Nkind (Obj) = N_Function_Call then
22479
22480 -- Function results are objects, so we get either the access level of
22481 -- the function or, in the case of an indirect call, the level of the
22482 -- access-to-subprogram type. (This code is used for Ada 95, but it
22483 -- looks wrong, because it seems that we should be checking the level
22484 -- of the call itself, even for Ada 95. However, using the Ada 2005
22485 -- version of the code causes regressions in several tests that are
22486 -- compiled with -gnat95. ???)
22487
22488 if Ada_Version < Ada_2005 then
22489 if Is_Entity_Name (Name (Obj)) then
22490 return Subprogram_Access_Level (Entity (Name (Obj)));
22491 else
22492 return Type_Access_Level (Etype (Prefix (Name (Obj))));
22493 end if;
22494
22495 -- For Ada 2005, the level of the result object of a function call is
22496 -- defined to be the level of the call's innermost enclosing master.
22497 -- We determine that by querying the depth of the innermost enclosing
22498 -- dynamic scope.
22499
22500 else
22501 Return_Master_Scope_Depth_Of_Call : declare
22502 function Innermost_Master_Scope_Depth
22503 (N : Node_Id) return Uint;
22504 -- Returns the scope depth of the given node's innermost
22505 -- enclosing dynamic scope (effectively the accessibility
22506 -- level of the innermost enclosing master).
22507
22508 ----------------------------------
22509 -- Innermost_Master_Scope_Depth --
22510 ----------------------------------
22511
22512 function Innermost_Master_Scope_Depth
22513 (N : Node_Id) return Uint
22514 is
22515 Node_Par : Node_Id := Parent (N);
22516
22517 begin
22518 -- Locate the nearest enclosing node (by traversing Parents)
22519 -- that Defining_Entity can be applied to, and return the
22520 -- depth of that entity's nearest enclosing dynamic scope.
22521
22522 while Present (Node_Par) loop
22523 case Nkind (Node_Par) is
22524 when N_Abstract_Subprogram_Declaration
22525 | N_Block_Statement
22526 | N_Body_Stub
22527 | N_Component_Declaration
22528 | N_Entry_Body
22529 | N_Entry_Declaration
22530 | N_Exception_Declaration
22531 | N_Formal_Object_Declaration
22532 | N_Formal_Package_Declaration
22533 | N_Formal_Subprogram_Declaration
22534 | N_Formal_Type_Declaration
22535 | N_Full_Type_Declaration
22536 | N_Function_Specification
22537 | N_Generic_Declaration
22538 | N_Generic_Instantiation
22539 | N_Implicit_Label_Declaration
22540 | N_Incomplete_Type_Declaration
22541 | N_Loop_Parameter_Specification
22542 | N_Number_Declaration
22543 | N_Object_Declaration
22544 | N_Package_Declaration
22545 | N_Package_Specification
22546 | N_Parameter_Specification
22547 | N_Private_Extension_Declaration
22548 | N_Private_Type_Declaration
22549 | N_Procedure_Specification
22550 | N_Proper_Body
22551 | N_Protected_Type_Declaration
22552 | N_Renaming_Declaration
22553 | N_Single_Protected_Declaration
22554 | N_Single_Task_Declaration
22555 | N_Subprogram_Declaration
22556 | N_Subtype_Declaration
22557 | N_Subunit
22558 | N_Task_Type_Declaration
22559 =>
22560 return Scope_Depth
22561 (Nearest_Dynamic_Scope
22562 (Defining_Entity (Node_Par)));
22563
22564 -- For a return statement within a function, return
22565 -- the depth of the function itself. This is not just
22566 -- a small optimization, but matters when analyzing
22567 -- the expression in an expression function before
22568 -- the body is created.
22569
22570 when N_Simple_Return_Statement =>
22571 if Ekind (Current_Scope) = E_Function then
22572 return Scope_Depth (Current_Scope);
22573 end if;
22574
22575 when others =>
22576 null;
22577 end case;
22578
22579 Node_Par := Parent (Node_Par);
22580 end loop;
22581
22582 pragma Assert (False);
22583
22584 -- Should never reach the following return
22585
22586 return Scope_Depth (Current_Scope) + 1;
22587 end Innermost_Master_Scope_Depth;
22588
22589 -- Start of processing for Return_Master_Scope_Depth_Of_Call
22590
22591 begin
22592 return Innermost_Master_Scope_Depth (Obj);
22593 end Return_Master_Scope_Depth_Of_Call;
22594 end if;
22595
22596 -- For convenience we handle qualified expressions, even though they
22597 -- aren't technically object names.
22598
22599 elsif Nkind (Obj) = N_Qualified_Expression then
22600 return Object_Access_Level (Expression (Obj));
22601
22602 -- Ditto for aggregates. They have the level of the temporary that
22603 -- will hold their value.
22604
22605 elsif Nkind (Obj) = N_Aggregate then
22606 return Object_Access_Level (Current_Scope);
22607
22608 -- Otherwise return the scope level of Standard. (If there are cases
22609 -- that fall through to this point they will be treated as having
22610 -- global accessibility for now. ???)
22611
22612 else
22613 return Scope_Depth (Standard_Standard);
22614 end if;
22615 end Object_Access_Level;
22616
22617 ----------------------------------
22618 -- Old_Requires_Transient_Scope --
22619 ----------------------------------
22620
22621 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
22622 Typ : constant Entity_Id := Underlying_Type (Id);
22623
22624 begin
22625 -- This is a private type which is not completed yet. This can only
22626 -- happen in a default expression (of a formal parameter or of a
22627 -- record component). Do not expand transient scope in this case.
22628
22629 if No (Typ) then
22630 return False;
22631
22632 -- Do not expand transient scope for non-existent procedure return
22633
22634 elsif Typ = Standard_Void_Type then
22635 return False;
22636
22637 -- Elementary types do not require a transient scope
22638
22639 elsif Is_Elementary_Type (Typ) then
22640 return False;
22641
22642 -- Generally, indefinite subtypes require a transient scope, since the
22643 -- back end cannot generate temporaries, since this is not a valid type
22644 -- for declaring an object. It might be possible to relax this in the
22645 -- future, e.g. by declaring the maximum possible space for the type.
22646
22647 elsif not Is_Definite_Subtype (Typ) then
22648 return True;
22649
22650 -- Functions returning tagged types may dispatch on result so their
22651 -- returned value is allocated on the secondary stack. Controlled
22652 -- type temporaries need finalization.
22653
22654 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
22655 return True;
22656
22657 -- Record type
22658
22659 elsif Is_Record_Type (Typ) then
22660 declare
22661 Comp : Entity_Id;
22662
22663 begin
22664 Comp := First_Entity (Typ);
22665 while Present (Comp) loop
22666 if Ekind (Comp) = E_Component then
22667
22668 -- ???It's not clear we need a full recursive call to
22669 -- Old_Requires_Transient_Scope here. Note that the
22670 -- following can't happen.
22671
22672 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
22673 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
22674
22675 if Old_Requires_Transient_Scope (Etype (Comp)) then
22676 return True;
22677 end if;
22678 end if;
22679
22680 Next_Entity (Comp);
22681 end loop;
22682 end;
22683
22684 return False;
22685
22686 -- String literal types never require transient scope
22687
22688 elsif Ekind (Typ) = E_String_Literal_Subtype then
22689 return False;
22690
22691 -- Array type. Note that we already know that this is a constrained
22692 -- array, since unconstrained arrays will fail the indefinite test.
22693
22694 elsif Is_Array_Type (Typ) then
22695
22696 -- If component type requires a transient scope, the array does too
22697
22698 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
22699 return True;
22700
22701 -- Otherwise, we only need a transient scope if the size depends on
22702 -- the value of one or more discriminants.
22703
22704 else
22705 return Size_Depends_On_Discriminant (Typ);
22706 end if;
22707
22708 -- All other cases do not require a transient scope
22709
22710 else
22711 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
22712 return False;
22713 end if;
22714 end Old_Requires_Transient_Scope;
22715
22716 ---------------------------------
22717 -- Original_Aspect_Pragma_Name --
22718 ---------------------------------
22719
22720 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
22721 Item : Node_Id;
22722 Item_Nam : Name_Id;
22723
22724 begin
22725 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
22726
22727 Item := N;
22728
22729 -- The pragma was generated to emulate an aspect, use the original
22730 -- aspect specification.
22731
22732 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
22733 Item := Corresponding_Aspect (Item);
22734 end if;
22735
22736 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
22737 -- Post and Post_Class rewrite their pragma identifier to preserve the
22738 -- original name.
22739 -- ??? this is kludgey
22740
22741 if Nkind (Item) = N_Pragma then
22742 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
22743
22744 else
22745 pragma Assert (Nkind (Item) = N_Aspect_Specification);
22746 Item_Nam := Chars (Identifier (Item));
22747 end if;
22748
22749 -- Deal with 'Class by converting the name to its _XXX form
22750
22751 if Class_Present (Item) then
22752 if Item_Nam = Name_Invariant then
22753 Item_Nam := Name_uInvariant;
22754
22755 elsif Item_Nam = Name_Post then
22756 Item_Nam := Name_uPost;
22757
22758 elsif Item_Nam = Name_Pre then
22759 Item_Nam := Name_uPre;
22760
22761 elsif Nam_In (Item_Nam, Name_Type_Invariant,
22762 Name_Type_Invariant_Class)
22763 then
22764 Item_Nam := Name_uType_Invariant;
22765
22766 -- Nothing to do for other cases (e.g. a Check that derived from
22767 -- Pre_Class and has the flag set). Also we do nothing if the name
22768 -- is already in special _xxx form.
22769
22770 end if;
22771 end if;
22772
22773 return Item_Nam;
22774 end Original_Aspect_Pragma_Name;
22775
22776 --------------------------------------
22777 -- Original_Corresponding_Operation --
22778 --------------------------------------
22779
22780 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
22781 is
22782 Typ : constant Entity_Id := Find_Dispatching_Type (S);
22783
22784 begin
22785 -- If S is an inherited primitive S2 the original corresponding
22786 -- operation of S is the original corresponding operation of S2
22787
22788 if Present (Alias (S))
22789 and then Find_Dispatching_Type (Alias (S)) /= Typ
22790 then
22791 return Original_Corresponding_Operation (Alias (S));
22792
22793 -- If S overrides an inherited subprogram S2 the original corresponding
22794 -- operation of S is the original corresponding operation of S2
22795
22796 elsif Present (Overridden_Operation (S)) then
22797 return Original_Corresponding_Operation (Overridden_Operation (S));
22798
22799 -- otherwise it is S itself
22800
22801 else
22802 return S;
22803 end if;
22804 end Original_Corresponding_Operation;
22805
22806 -------------------
22807 -- Output_Entity --
22808 -------------------
22809
22810 procedure Output_Entity (Id : Entity_Id) is
22811 Scop : Entity_Id;
22812
22813 begin
22814 Scop := Scope (Id);
22815
22816 -- The entity may lack a scope when it is in the process of being
22817 -- analyzed. Use the current scope as an approximation.
22818
22819 if No (Scop) then
22820 Scop := Current_Scope;
22821 end if;
22822
22823 Output_Name (Chars (Id), Scop);
22824 end Output_Entity;
22825
22826 -----------------
22827 -- Output_Name --
22828 -----------------
22829
22830 procedure Output_Name (Nam : Name_Id; Scop : Entity_Id := Current_Scope) is
22831 begin
22832 Write_Str
22833 (Get_Name_String
22834 (Get_Qualified_Name
22835 (Nam => Nam,
22836 Suffix => No_Name,
22837 Scop => Scop)));
22838 Write_Eol;
22839 end Output_Name;
22840
22841 ----------------------
22842 -- Policy_In_Effect --
22843 ----------------------
22844
22845 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
22846 function Policy_In_List (List : Node_Id) return Name_Id;
22847 -- Determine the mode of a policy in a N_Pragma list
22848
22849 --------------------
22850 -- Policy_In_List --
22851 --------------------
22852
22853 function Policy_In_List (List : Node_Id) return Name_Id is
22854 Arg1 : Node_Id;
22855 Arg2 : Node_Id;
22856 Prag : Node_Id;
22857
22858 begin
22859 Prag := List;
22860 while Present (Prag) loop
22861 Arg1 := First (Pragma_Argument_Associations (Prag));
22862 Arg2 := Next (Arg1);
22863
22864 Arg1 := Get_Pragma_Arg (Arg1);
22865 Arg2 := Get_Pragma_Arg (Arg2);
22866
22867 -- The current Check_Policy pragma matches the requested policy or
22868 -- appears in the single argument form (Assertion, policy_id).
22869
22870 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
22871 return Chars (Arg2);
22872 end if;
22873
22874 Prag := Next_Pragma (Prag);
22875 end loop;
22876
22877 return No_Name;
22878 end Policy_In_List;
22879
22880 -- Local variables
22881
22882 Kind : Name_Id;
22883
22884 -- Start of processing for Policy_In_Effect
22885
22886 begin
22887 if not Is_Valid_Assertion_Kind (Policy) then
22888 raise Program_Error;
22889 end if;
22890
22891 -- Inspect all policy pragmas that appear within scopes (if any)
22892
22893 Kind := Policy_In_List (Check_Policy_List);
22894
22895 -- Inspect all configuration policy pragmas (if any)
22896
22897 if Kind = No_Name then
22898 Kind := Policy_In_List (Check_Policy_List_Config);
22899 end if;
22900
22901 -- The context lacks policy pragmas, determine the mode based on whether
22902 -- assertions are enabled at the configuration level. This ensures that
22903 -- the policy is preserved when analyzing generics.
22904
22905 if Kind = No_Name then
22906 if Assertions_Enabled_Config then
22907 Kind := Name_Check;
22908 else
22909 Kind := Name_Ignore;
22910 end if;
22911 end if;
22912
22913 -- In CodePeer mode and GNATprove mode, we need to consider all
22914 -- assertions, unless they are disabled. Force Name_Check on
22915 -- ignored assertions.
22916
22917 if Nam_In (Kind, Name_Ignore, Name_Off)
22918 and then (CodePeer_Mode or GNATprove_Mode)
22919 then
22920 Kind := Name_Check;
22921 end if;
22922
22923 return Kind;
22924 end Policy_In_Effect;
22925
22926 ----------------------------------
22927 -- Predicate_Tests_On_Arguments --
22928 ----------------------------------
22929
22930 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
22931 begin
22932 -- Always test predicates on indirect call
22933
22934 if Ekind (Subp) = E_Subprogram_Type then
22935 return True;
22936
22937 -- Do not test predicates on call to generated default Finalize, since
22938 -- we are not interested in whether something we are finalizing (and
22939 -- typically destroying) satisfies its predicates.
22940
22941 elsif Chars (Subp) = Name_Finalize
22942 and then not Comes_From_Source (Subp)
22943 then
22944 return False;
22945
22946 -- Do not test predicates on any internally generated routines
22947
22948 elsif Is_Internal_Name (Chars (Subp)) then
22949 return False;
22950
22951 -- Do not test predicates on call to Init_Proc, since if needed the
22952 -- predicate test will occur at some other point.
22953
22954 elsif Is_Init_Proc (Subp) then
22955 return False;
22956
22957 -- Do not test predicates on call to predicate function, since this
22958 -- would cause infinite recursion.
22959
22960 elsif Ekind (Subp) = E_Function
22961 and then (Is_Predicate_Function (Subp)
22962 or else
22963 Is_Predicate_Function_M (Subp))
22964 then
22965 return False;
22966
22967 -- For now, no other exceptions
22968
22969 else
22970 return True;
22971 end if;
22972 end Predicate_Tests_On_Arguments;
22973
22974 -----------------------
22975 -- Private_Component --
22976 -----------------------
22977
22978 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
22979 Ancestor : constant Entity_Id := Base_Type (Type_Id);
22980
22981 function Trace_Components
22982 (T : Entity_Id;
22983 Check : Boolean) return Entity_Id;
22984 -- Recursive function that does the work, and checks against circular
22985 -- definition for each subcomponent type.
22986
22987 ----------------------
22988 -- Trace_Components --
22989 ----------------------
22990
22991 function Trace_Components
22992 (T : Entity_Id;
22993 Check : Boolean) return Entity_Id
22994 is
22995 Btype : constant Entity_Id := Base_Type (T);
22996 Component : Entity_Id;
22997 P : Entity_Id;
22998 Candidate : Entity_Id := Empty;
22999
23000 begin
23001 if Check and then Btype = Ancestor then
23002 Error_Msg_N ("circular type definition", Type_Id);
23003 return Any_Type;
23004 end if;
23005
23006 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
23007 if Present (Full_View (Btype))
23008 and then Is_Record_Type (Full_View (Btype))
23009 and then not Is_Frozen (Btype)
23010 then
23011 -- To indicate that the ancestor depends on a private type, the
23012 -- current Btype is sufficient. However, to check for circular
23013 -- definition we must recurse on the full view.
23014
23015 Candidate := Trace_Components (Full_View (Btype), True);
23016
23017 if Candidate = Any_Type then
23018 return Any_Type;
23019 else
23020 return Btype;
23021 end if;
23022
23023 else
23024 return Btype;
23025 end if;
23026
23027 elsif Is_Array_Type (Btype) then
23028 return Trace_Components (Component_Type (Btype), True);
23029
23030 elsif Is_Record_Type (Btype) then
23031 Component := First_Entity (Btype);
23032 while Present (Component)
23033 and then Comes_From_Source (Component)
23034 loop
23035 -- Skip anonymous types generated by constrained components
23036
23037 if not Is_Type (Component) then
23038 P := Trace_Components (Etype (Component), True);
23039
23040 if Present (P) then
23041 if P = Any_Type then
23042 return P;
23043 else
23044 Candidate := P;
23045 end if;
23046 end if;
23047 end if;
23048
23049 Next_Entity (Component);
23050 end loop;
23051
23052 return Candidate;
23053
23054 else
23055 return Empty;
23056 end if;
23057 end Trace_Components;
23058
23059 -- Start of processing for Private_Component
23060
23061 begin
23062 return Trace_Components (Type_Id, False);
23063 end Private_Component;
23064
23065 ---------------------------
23066 -- Primitive_Names_Match --
23067 ---------------------------
23068
23069 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
23070 function Non_Internal_Name (E : Entity_Id) return Name_Id;
23071 -- Given an internal name, returns the corresponding non-internal name
23072
23073 ------------------------
23074 -- Non_Internal_Name --
23075 ------------------------
23076
23077 function Non_Internal_Name (E : Entity_Id) return Name_Id is
23078 begin
23079 Get_Name_String (Chars (E));
23080 Name_Len := Name_Len - 1;
23081 return Name_Find;
23082 end Non_Internal_Name;
23083
23084 -- Start of processing for Primitive_Names_Match
23085
23086 begin
23087 pragma Assert (Present (E1) and then Present (E2));
23088
23089 return Chars (E1) = Chars (E2)
23090 or else
23091 (not Is_Internal_Name (Chars (E1))
23092 and then Is_Internal_Name (Chars (E2))
23093 and then Non_Internal_Name (E2) = Chars (E1))
23094 or else
23095 (not Is_Internal_Name (Chars (E2))
23096 and then Is_Internal_Name (Chars (E1))
23097 and then Non_Internal_Name (E1) = Chars (E2))
23098 or else
23099 (Is_Predefined_Dispatching_Operation (E1)
23100 and then Is_Predefined_Dispatching_Operation (E2)
23101 and then Same_TSS (E1, E2))
23102 or else
23103 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
23104 end Primitive_Names_Match;
23105
23106 -----------------------
23107 -- Process_End_Label --
23108 -----------------------
23109
23110 procedure Process_End_Label
23111 (N : Node_Id;
23112 Typ : Character;
23113 Ent : Entity_Id)
23114 is
23115 Loc : Source_Ptr;
23116 Nam : Node_Id;
23117 Scop : Entity_Id;
23118
23119 Label_Ref : Boolean;
23120 -- Set True if reference to end label itself is required
23121
23122 Endl : Node_Id;
23123 -- Gets set to the operator symbol or identifier that references the
23124 -- entity Ent. For the child unit case, this is the identifier from the
23125 -- designator. For other cases, this is simply Endl.
23126
23127 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
23128 -- N is an identifier node that appears as a parent unit reference in
23129 -- the case where Ent is a child unit. This procedure generates an
23130 -- appropriate cross-reference entry. E is the corresponding entity.
23131
23132 -------------------------
23133 -- Generate_Parent_Ref --
23134 -------------------------
23135
23136 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
23137 begin
23138 -- If names do not match, something weird, skip reference
23139
23140 if Chars (E) = Chars (N) then
23141
23142 -- Generate the reference. We do NOT consider this as a reference
23143 -- for unreferenced symbol purposes.
23144
23145 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
23146
23147 if Style_Check then
23148 Style.Check_Identifier (N, E);
23149 end if;
23150 end if;
23151 end Generate_Parent_Ref;
23152
23153 -- Start of processing for Process_End_Label
23154
23155 begin
23156 -- If no node, ignore. This happens in some error situations, and
23157 -- also for some internally generated structures where no end label
23158 -- references are required in any case.
23159
23160 if No (N) then
23161 return;
23162 end if;
23163
23164 -- Nothing to do if no End_Label, happens for internally generated
23165 -- constructs where we don't want an end label reference anyway. Also
23166 -- nothing to do if Endl is a string literal, which means there was
23167 -- some prior error (bad operator symbol)
23168
23169 Endl := End_Label (N);
23170
23171 if No (Endl) or else Nkind (Endl) = N_String_Literal then
23172 return;
23173 end if;
23174
23175 -- Reference node is not in extended main source unit
23176
23177 if not In_Extended_Main_Source_Unit (N) then
23178
23179 -- Generally we do not collect references except for the extended
23180 -- main source unit. The one exception is the 'e' entry for a
23181 -- package spec, where it is useful for a client to have the
23182 -- ending information to define scopes.
23183
23184 if Typ /= 'e' then
23185 return;
23186
23187 else
23188 Label_Ref := False;
23189
23190 -- For this case, we can ignore any parent references, but we
23191 -- need the package name itself for the 'e' entry.
23192
23193 if Nkind (Endl) = N_Designator then
23194 Endl := Identifier (Endl);
23195 end if;
23196 end if;
23197
23198 -- Reference is in extended main source unit
23199
23200 else
23201 Label_Ref := True;
23202
23203 -- For designator, generate references for the parent entries
23204
23205 if Nkind (Endl) = N_Designator then
23206
23207 -- Generate references for the prefix if the END line comes from
23208 -- source (otherwise we do not need these references) We climb the
23209 -- scope stack to find the expected entities.
23210
23211 if Comes_From_Source (Endl) then
23212 Nam := Name (Endl);
23213 Scop := Current_Scope;
23214 while Nkind (Nam) = N_Selected_Component loop
23215 Scop := Scope (Scop);
23216 exit when No (Scop);
23217 Generate_Parent_Ref (Selector_Name (Nam), Scop);
23218 Nam := Prefix (Nam);
23219 end loop;
23220
23221 if Present (Scop) then
23222 Generate_Parent_Ref (Nam, Scope (Scop));
23223 end if;
23224 end if;
23225
23226 Endl := Identifier (Endl);
23227 end if;
23228 end if;
23229
23230 -- If the end label is not for the given entity, then either we have
23231 -- some previous error, or this is a generic instantiation for which
23232 -- we do not need to make a cross-reference in this case anyway. In
23233 -- either case we simply ignore the call.
23234
23235 if Chars (Ent) /= Chars (Endl) then
23236 return;
23237 end if;
23238
23239 -- If label was really there, then generate a normal reference and then
23240 -- adjust the location in the end label to point past the name (which
23241 -- should almost always be the semicolon).
23242
23243 Loc := Sloc (Endl);
23244
23245 if Comes_From_Source (Endl) then
23246
23247 -- If a label reference is required, then do the style check and
23248 -- generate an l-type cross-reference entry for the label
23249
23250 if Label_Ref then
23251 if Style_Check then
23252 Style.Check_Identifier (Endl, Ent);
23253 end if;
23254
23255 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
23256 end if;
23257
23258 -- Set the location to point past the label (normally this will
23259 -- mean the semicolon immediately following the label). This is
23260 -- done for the sake of the 'e' or 't' entry generated below.
23261
23262 Get_Decoded_Name_String (Chars (Endl));
23263 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
23264
23265 else
23266 -- In SPARK mode, no missing label is allowed for packages and
23267 -- subprogram bodies. Detect those cases by testing whether
23268 -- Process_End_Label was called for a body (Typ = 't') or a package.
23269
23270 if Restriction_Check_Required (SPARK_05)
23271 and then (Typ = 't' or else Ekind (Ent) = E_Package)
23272 then
23273 Error_Msg_Node_1 := Endl;
23274 Check_SPARK_05_Restriction
23275 ("`END &` required", Endl, Force => True);
23276 end if;
23277 end if;
23278
23279 -- Now generate the e/t reference
23280
23281 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
23282
23283 -- Restore Sloc, in case modified above, since we have an identifier
23284 -- and the normal Sloc should be left set in the tree.
23285
23286 Set_Sloc (Endl, Loc);
23287 end Process_End_Label;
23288
23289 --------------------------------
23290 -- Propagate_Concurrent_Flags --
23291 --------------------------------
23292
23293 procedure Propagate_Concurrent_Flags
23294 (Typ : Entity_Id;
23295 Comp_Typ : Entity_Id)
23296 is
23297 begin
23298 if Has_Task (Comp_Typ) then
23299 Set_Has_Task (Typ);
23300 end if;
23301
23302 if Has_Protected (Comp_Typ) then
23303 Set_Has_Protected (Typ);
23304 end if;
23305
23306 if Has_Timing_Event (Comp_Typ) then
23307 Set_Has_Timing_Event (Typ);
23308 end if;
23309 end Propagate_Concurrent_Flags;
23310
23311 ------------------------------
23312 -- Propagate_DIC_Attributes --
23313 ------------------------------
23314
23315 procedure Propagate_DIC_Attributes
23316 (Typ : Entity_Id;
23317 From_Typ : Entity_Id)
23318 is
23319 DIC_Proc : Entity_Id;
23320
23321 begin
23322 if Present (Typ) and then Present (From_Typ) then
23323 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
23324
23325 -- Nothing to do if both the source and the destination denote the
23326 -- same type.
23327
23328 if From_Typ = Typ then
23329 return;
23330 end if;
23331
23332 DIC_Proc := DIC_Procedure (From_Typ);
23333
23334 -- The setting of the attributes is intentionally conservative. This
23335 -- prevents accidental clobbering of enabled attributes.
23336
23337 if Has_Inherited_DIC (From_Typ)
23338 and then not Has_Inherited_DIC (Typ)
23339 then
23340 Set_Has_Inherited_DIC (Typ);
23341 end if;
23342
23343 if Has_Own_DIC (From_Typ) and then not Has_Own_DIC (Typ) then
23344 Set_Has_Own_DIC (Typ);
23345 end if;
23346
23347 if Present (DIC_Proc) and then No (DIC_Procedure (Typ)) then
23348 Set_DIC_Procedure (Typ, DIC_Proc);
23349 end if;
23350 end if;
23351 end Propagate_DIC_Attributes;
23352
23353 ------------------------------------
23354 -- Propagate_Invariant_Attributes --
23355 ------------------------------------
23356
23357 procedure Propagate_Invariant_Attributes
23358 (Typ : Entity_Id;
23359 From_Typ : Entity_Id)
23360 is
23361 Full_IP : Entity_Id;
23362 Part_IP : Entity_Id;
23363
23364 begin
23365 if Present (Typ) and then Present (From_Typ) then
23366 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
23367
23368 -- Nothing to do if both the source and the destination denote the
23369 -- same type.
23370
23371 if From_Typ = Typ then
23372 return;
23373 end if;
23374
23375 Full_IP := Invariant_Procedure (From_Typ);
23376 Part_IP := Partial_Invariant_Procedure (From_Typ);
23377
23378 -- The setting of the attributes is intentionally conservative. This
23379 -- prevents accidental clobbering of enabled attributes.
23380
23381 if Has_Inheritable_Invariants (From_Typ)
23382 and then not Has_Inheritable_Invariants (Typ)
23383 then
23384 Set_Has_Inheritable_Invariants (Typ);
23385 end if;
23386
23387 if Has_Inherited_Invariants (From_Typ)
23388 and then not Has_Inherited_Invariants (Typ)
23389 then
23390 Set_Has_Inherited_Invariants (Typ);
23391 end if;
23392
23393 if Has_Own_Invariants (From_Typ)
23394 and then not Has_Own_Invariants (Typ)
23395 then
23396 Set_Has_Own_Invariants (Typ);
23397 end if;
23398
23399 if Present (Full_IP) and then No (Invariant_Procedure (Typ)) then
23400 Set_Invariant_Procedure (Typ, Full_IP);
23401 end if;
23402
23403 if Present (Part_IP) and then No (Partial_Invariant_Procedure (Typ))
23404 then
23405 Set_Partial_Invariant_Procedure (Typ, Part_IP);
23406 end if;
23407 end if;
23408 end Propagate_Invariant_Attributes;
23409
23410 ---------------------------------------
23411 -- Record_Possible_Part_Of_Reference --
23412 ---------------------------------------
23413
23414 procedure Record_Possible_Part_Of_Reference
23415 (Var_Id : Entity_Id;
23416 Ref : Node_Id)
23417 is
23418 Encap : constant Entity_Id := Encapsulating_State (Var_Id);
23419 Refs : Elist_Id;
23420
23421 begin
23422 -- The variable is a constituent of a single protected/task type. Such
23423 -- a variable acts as a component of the type and must appear within a
23424 -- specific region (SPARK RM 9(3)). Instead of recording the reference,
23425 -- verify its legality now.
23426
23427 if Present (Encap) and then Is_Single_Concurrent_Object (Encap) then
23428 Check_Part_Of_Reference (Var_Id, Ref);
23429
23430 -- The variable is subject to pragma Part_Of and may eventually become a
23431 -- constituent of a single protected/task type. Record the reference to
23432 -- verify its placement when the contract of the variable is analyzed.
23433
23434 elsif Present (Get_Pragma (Var_Id, Pragma_Part_Of)) then
23435 Refs := Part_Of_References (Var_Id);
23436
23437 if No (Refs) then
23438 Refs := New_Elmt_List;
23439 Set_Part_Of_References (Var_Id, Refs);
23440 end if;
23441
23442 Append_Elmt (Ref, Refs);
23443 end if;
23444 end Record_Possible_Part_Of_Reference;
23445
23446 ----------------
23447 -- Referenced --
23448 ----------------
23449
23450 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
23451 Seen : Boolean := False;
23452
23453 function Is_Reference (N : Node_Id) return Traverse_Result;
23454 -- Determine whether node N denotes a reference to Id. If this is the
23455 -- case, set global flag Seen to True and stop the traversal.
23456
23457 ------------------
23458 -- Is_Reference --
23459 ------------------
23460
23461 function Is_Reference (N : Node_Id) return Traverse_Result is
23462 begin
23463 if Is_Entity_Name (N)
23464 and then Present (Entity (N))
23465 and then Entity (N) = Id
23466 then
23467 Seen := True;
23468 return Abandon;
23469 else
23470 return OK;
23471 end if;
23472 end Is_Reference;
23473
23474 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
23475
23476 -- Start of processing for Referenced
23477
23478 begin
23479 Inspect_Expression (Expr);
23480 return Seen;
23481 end Referenced;
23482
23483 ------------------------------------
23484 -- References_Generic_Formal_Type --
23485 ------------------------------------
23486
23487 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
23488
23489 function Process (N : Node_Id) return Traverse_Result;
23490 -- Process one node in search for generic formal type
23491
23492 -------------
23493 -- Process --
23494 -------------
23495
23496 function Process (N : Node_Id) return Traverse_Result is
23497 begin
23498 if Nkind (N) in N_Has_Entity then
23499 declare
23500 E : constant Entity_Id := Entity (N);
23501 begin
23502 if Present (E) then
23503 if Is_Generic_Type (E) then
23504 return Abandon;
23505 elsif Present (Etype (E))
23506 and then Is_Generic_Type (Etype (E))
23507 then
23508 return Abandon;
23509 end if;
23510 end if;
23511 end;
23512 end if;
23513
23514 return Atree.OK;
23515 end Process;
23516
23517 function Traverse is new Traverse_Func (Process);
23518 -- Traverse tree to look for generic type
23519
23520 begin
23521 if Inside_A_Generic then
23522 return Traverse (N) = Abandon;
23523 else
23524 return False;
23525 end if;
23526 end References_Generic_Formal_Type;
23527
23528 -------------------------------
23529 -- Remove_Entity_And_Homonym --
23530 -------------------------------
23531
23532 procedure Remove_Entity_And_Homonym (Id : Entity_Id) is
23533 begin
23534 Remove_Entity (Id);
23535 Remove_Homonym (Id);
23536 end Remove_Entity_And_Homonym;
23537
23538 --------------------
23539 -- Remove_Homonym --
23540 --------------------
23541
23542 procedure Remove_Homonym (Id : Entity_Id) is
23543 Hom : Entity_Id;
23544 Prev : Entity_Id := Empty;
23545
23546 begin
23547 if Id = Current_Entity (Id) then
23548 if Present (Homonym (Id)) then
23549 Set_Current_Entity (Homonym (Id));
23550 else
23551 Set_Name_Entity_Id (Chars (Id), Empty);
23552 end if;
23553
23554 else
23555 Hom := Current_Entity (Id);
23556 while Present (Hom) and then Hom /= Id loop
23557 Prev := Hom;
23558 Hom := Homonym (Hom);
23559 end loop;
23560
23561 -- If Id is not on the homonym chain, nothing to do
23562
23563 if Present (Hom) then
23564 Set_Homonym (Prev, Homonym (Id));
23565 end if;
23566 end if;
23567 end Remove_Homonym;
23568
23569 ------------------------------
23570 -- Remove_Overloaded_Entity --
23571 ------------------------------
23572
23573 procedure Remove_Overloaded_Entity (Id : Entity_Id) is
23574 procedure Remove_Primitive_Of (Typ : Entity_Id);
23575 -- Remove primitive subprogram Id from the list of primitives that
23576 -- belong to type Typ.
23577
23578 -------------------------
23579 -- Remove_Primitive_Of --
23580 -------------------------
23581
23582 procedure Remove_Primitive_Of (Typ : Entity_Id) is
23583 Prims : Elist_Id;
23584
23585 begin
23586 if Is_Tagged_Type (Typ) then
23587 Prims := Direct_Primitive_Operations (Typ);
23588
23589 if Present (Prims) then
23590 Remove (Prims, Id);
23591 end if;
23592 end if;
23593 end Remove_Primitive_Of;
23594
23595 -- Local variables
23596
23597 Formal : Entity_Id;
23598
23599 -- Start of processing for Remove_Overloaded_Entity
23600
23601 begin
23602 Remove_Entity_And_Homonym (Id);
23603
23604 -- The entity denotes a primitive subprogram. Remove it from the list of
23605 -- primitives of the associated controlling type.
23606
23607 if Ekind_In (Id, E_Function, E_Procedure) and then Is_Primitive (Id) then
23608 Formal := First_Formal (Id);
23609 while Present (Formal) loop
23610 if Is_Controlling_Formal (Formal) then
23611 Remove_Primitive_Of (Etype (Formal));
23612 exit;
23613 end if;
23614
23615 Next_Formal (Formal);
23616 end loop;
23617
23618 if Ekind (Id) = E_Function and then Has_Controlling_Result (Id) then
23619 Remove_Primitive_Of (Etype (Id));
23620 end if;
23621 end if;
23622 end Remove_Overloaded_Entity;
23623
23624 ---------------------
23625 -- Rep_To_Pos_Flag --
23626 ---------------------
23627
23628 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
23629 begin
23630 return New_Occurrence_Of
23631 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
23632 end Rep_To_Pos_Flag;
23633
23634 --------------------
23635 -- Require_Entity --
23636 --------------------
23637
23638 procedure Require_Entity (N : Node_Id) is
23639 begin
23640 if Is_Entity_Name (N) and then No (Entity (N)) then
23641 if Total_Errors_Detected /= 0 then
23642 Set_Entity (N, Any_Id);
23643 else
23644 raise Program_Error;
23645 end if;
23646 end if;
23647 end Require_Entity;
23648
23649 ------------------------------
23650 -- Requires_Transient_Scope --
23651 ------------------------------
23652
23653 -- A transient scope is required when variable-sized temporaries are
23654 -- allocated on the secondary stack, or when finalization actions must be
23655 -- generated before the next instruction.
23656
23657 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
23658 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
23659
23660 begin
23661 if Debug_Flag_QQ then
23662 return Old_Result;
23663 end if;
23664
23665 declare
23666 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
23667
23668 begin
23669 -- Assert that we're not putting things on the secondary stack if we
23670 -- didn't before; we are trying to AVOID secondary stack when
23671 -- possible.
23672
23673 if not Old_Result then
23674 pragma Assert (not New_Result);
23675 null;
23676 end if;
23677
23678 if New_Result /= Old_Result then
23679 Results_Differ (Id, Old_Result, New_Result);
23680 end if;
23681
23682 return New_Result;
23683 end;
23684 end Requires_Transient_Scope;
23685
23686 --------------------
23687 -- Results_Differ --
23688 --------------------
23689
23690 procedure Results_Differ
23691 (Id : Entity_Id;
23692 Old_Val : Boolean;
23693 New_Val : Boolean)
23694 is
23695 begin
23696 if False then -- False to disable; True for debugging
23697 Treepr.Print_Tree_Node (Id);
23698
23699 if Old_Val = New_Val then
23700 raise Program_Error;
23701 end if;
23702 end if;
23703 end Results_Differ;
23704
23705 --------------------------
23706 -- Reset_Analyzed_Flags --
23707 --------------------------
23708
23709 procedure Reset_Analyzed_Flags (N : Node_Id) is
23710 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
23711 -- Function used to reset Analyzed flags in tree. Note that we do
23712 -- not reset Analyzed flags in entities, since there is no need to
23713 -- reanalyze entities, and indeed, it is wrong to do so, since it
23714 -- can result in generating auxiliary stuff more than once.
23715
23716 --------------------
23717 -- Clear_Analyzed --
23718 --------------------
23719
23720 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
23721 begin
23722 if Nkind (N) not in N_Entity then
23723 Set_Analyzed (N, False);
23724 end if;
23725
23726 return OK;
23727 end Clear_Analyzed;
23728
23729 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
23730
23731 -- Start of processing for Reset_Analyzed_Flags
23732
23733 begin
23734 Reset_Analyzed (N);
23735 end Reset_Analyzed_Flags;
23736
23737 ------------------------
23738 -- Restore_SPARK_Mode --
23739 ------------------------
23740
23741 procedure Restore_SPARK_Mode
23742 (Mode : SPARK_Mode_Type;
23743 Prag : Node_Id)
23744 is
23745 begin
23746 SPARK_Mode := Mode;
23747 SPARK_Mode_Pragma := Prag;
23748 end Restore_SPARK_Mode;
23749
23750 --------------------------------
23751 -- Returns_Unconstrained_Type --
23752 --------------------------------
23753
23754 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
23755 begin
23756 return Ekind (Subp) = E_Function
23757 and then not Is_Scalar_Type (Etype (Subp))
23758 and then not Is_Access_Type (Etype (Subp))
23759 and then not Is_Constrained (Etype (Subp));
23760 end Returns_Unconstrained_Type;
23761
23762 ----------------------------
23763 -- Root_Type_Of_Full_View --
23764 ----------------------------
23765
23766 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
23767 Rtyp : constant Entity_Id := Root_Type (T);
23768
23769 begin
23770 -- The root type of the full view may itself be a private type. Keep
23771 -- looking for the ultimate derivation parent.
23772
23773 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
23774 return Root_Type_Of_Full_View (Full_View (Rtyp));
23775 else
23776 return Rtyp;
23777 end if;
23778 end Root_Type_Of_Full_View;
23779
23780 ---------------------------
23781 -- Safe_To_Capture_Value --
23782 ---------------------------
23783
23784 function Safe_To_Capture_Value
23785 (N : Node_Id;
23786 Ent : Entity_Id;
23787 Cond : Boolean := False) return Boolean
23788 is
23789 begin
23790 -- The only entities for which we track constant values are variables
23791 -- which are not renamings, constants, out parameters, and in out
23792 -- parameters, so check if we have this case.
23793
23794 -- Note: it may seem odd to track constant values for constants, but in
23795 -- fact this routine is used for other purposes than simply capturing
23796 -- the value. In particular, the setting of Known[_Non]_Null.
23797
23798 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
23799 or else
23800 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
23801 then
23802 null;
23803
23804 -- For conditionals, we also allow loop parameters and all formals,
23805 -- including in parameters.
23806
23807 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
23808 null;
23809
23810 -- For all other cases, not just unsafe, but impossible to capture
23811 -- Current_Value, since the above are the only entities which have
23812 -- Current_Value fields.
23813
23814 else
23815 return False;
23816 end if;
23817
23818 -- Skip if volatile or aliased, since funny things might be going on in
23819 -- these cases which we cannot necessarily track. Also skip any variable
23820 -- for which an address clause is given, or whose address is taken. Also
23821 -- never capture value of library level variables (an attempt to do so
23822 -- can occur in the case of package elaboration code).
23823
23824 if Treat_As_Volatile (Ent)
23825 or else Is_Aliased (Ent)
23826 or else Present (Address_Clause (Ent))
23827 or else Address_Taken (Ent)
23828 or else (Is_Library_Level_Entity (Ent)
23829 and then Ekind (Ent) = E_Variable)
23830 then
23831 return False;
23832 end if;
23833
23834 -- OK, all above conditions are met. We also require that the scope of
23835 -- the reference be the same as the scope of the entity, not counting
23836 -- packages and blocks and loops.
23837
23838 declare
23839 E_Scope : constant Entity_Id := Scope (Ent);
23840 R_Scope : Entity_Id;
23841
23842 begin
23843 R_Scope := Current_Scope;
23844 while R_Scope /= Standard_Standard loop
23845 exit when R_Scope = E_Scope;
23846
23847 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
23848 return False;
23849 else
23850 R_Scope := Scope (R_Scope);
23851 end if;
23852 end loop;
23853 end;
23854
23855 -- We also require that the reference does not appear in a context
23856 -- where it is not sure to be executed (i.e. a conditional context
23857 -- or an exception handler). We skip this if Cond is True, since the
23858 -- capturing of values from conditional tests handles this ok.
23859
23860 if Cond then
23861 return True;
23862 end if;
23863
23864 declare
23865 Desc : Node_Id;
23866 P : Node_Id;
23867
23868 begin
23869 Desc := N;
23870
23871 -- Seems dubious that case expressions are not handled here ???
23872
23873 P := Parent (N);
23874 while Present (P) loop
23875 if Nkind (P) = N_If_Statement
23876 or else Nkind (P) = N_Case_Statement
23877 or else (Nkind (P) in N_Short_Circuit
23878 and then Desc = Right_Opnd (P))
23879 or else (Nkind (P) = N_If_Expression
23880 and then Desc /= First (Expressions (P)))
23881 or else Nkind (P) = N_Exception_Handler
23882 or else Nkind (P) = N_Selective_Accept
23883 or else Nkind (P) = N_Conditional_Entry_Call
23884 or else Nkind (P) = N_Timed_Entry_Call
23885 or else Nkind (P) = N_Asynchronous_Select
23886 then
23887 return False;
23888
23889 else
23890 Desc := P;
23891 P := Parent (P);
23892
23893 -- A special Ada 2012 case: the original node may be part
23894 -- of the else_actions of a conditional expression, in which
23895 -- case it might not have been expanded yet, and appears in
23896 -- a non-syntactic list of actions. In that case it is clearly
23897 -- not safe to save a value.
23898
23899 if No (P)
23900 and then Is_List_Member (Desc)
23901 and then No (Parent (List_Containing (Desc)))
23902 then
23903 return False;
23904 end if;
23905 end if;
23906 end loop;
23907 end;
23908
23909 -- OK, looks safe to set value
23910
23911 return True;
23912 end Safe_To_Capture_Value;
23913
23914 ---------------
23915 -- Same_Name --
23916 ---------------
23917
23918 function Same_Name (N1, N2 : Node_Id) return Boolean is
23919 K1 : constant Node_Kind := Nkind (N1);
23920 K2 : constant Node_Kind := Nkind (N2);
23921
23922 begin
23923 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
23924 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
23925 then
23926 return Chars (N1) = Chars (N2);
23927
23928 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
23929 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
23930 then
23931 return Same_Name (Selector_Name (N1), Selector_Name (N2))
23932 and then Same_Name (Prefix (N1), Prefix (N2));
23933
23934 else
23935 return False;
23936 end if;
23937 end Same_Name;
23938
23939 -----------------
23940 -- Same_Object --
23941 -----------------
23942
23943 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
23944 N1 : constant Node_Id := Original_Node (Node1);
23945 N2 : constant Node_Id := Original_Node (Node2);
23946 -- We do the tests on original nodes, since we are most interested
23947 -- in the original source, not any expansion that got in the way.
23948
23949 K1 : constant Node_Kind := Nkind (N1);
23950 K2 : constant Node_Kind := Nkind (N2);
23951
23952 begin
23953 -- First case, both are entities with same entity
23954
23955 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
23956 declare
23957 EN1 : constant Entity_Id := Entity (N1);
23958 EN2 : constant Entity_Id := Entity (N2);
23959 begin
23960 if Present (EN1) and then Present (EN2)
23961 and then (Ekind_In (EN1, E_Variable, E_Constant)
23962 or else Is_Formal (EN1))
23963 and then EN1 = EN2
23964 then
23965 return True;
23966 end if;
23967 end;
23968 end if;
23969
23970 -- Second case, selected component with same selector, same record
23971
23972 if K1 = N_Selected_Component
23973 and then K2 = N_Selected_Component
23974 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
23975 then
23976 return Same_Object (Prefix (N1), Prefix (N2));
23977
23978 -- Third case, indexed component with same subscripts, same array
23979
23980 elsif K1 = N_Indexed_Component
23981 and then K2 = N_Indexed_Component
23982 and then Same_Object (Prefix (N1), Prefix (N2))
23983 then
23984 declare
23985 E1, E2 : Node_Id;
23986 begin
23987 E1 := First (Expressions (N1));
23988 E2 := First (Expressions (N2));
23989 while Present (E1) loop
23990 if not Same_Value (E1, E2) then
23991 return False;
23992 else
23993 Next (E1);
23994 Next (E2);
23995 end if;
23996 end loop;
23997
23998 return True;
23999 end;
24000
24001 -- Fourth case, slice of same array with same bounds
24002
24003 elsif K1 = N_Slice
24004 and then K2 = N_Slice
24005 and then Nkind (Discrete_Range (N1)) = N_Range
24006 and then Nkind (Discrete_Range (N2)) = N_Range
24007 and then Same_Value (Low_Bound (Discrete_Range (N1)),
24008 Low_Bound (Discrete_Range (N2)))
24009 and then Same_Value (High_Bound (Discrete_Range (N1)),
24010 High_Bound (Discrete_Range (N2)))
24011 then
24012 return Same_Name (Prefix (N1), Prefix (N2));
24013
24014 -- All other cases, not clearly the same object
24015
24016 else
24017 return False;
24018 end if;
24019 end Same_Object;
24020
24021 ---------------
24022 -- Same_Type --
24023 ---------------
24024
24025 function Same_Type (T1, T2 : Entity_Id) return Boolean is
24026 begin
24027 if T1 = T2 then
24028 return True;
24029
24030 elsif not Is_Constrained (T1)
24031 and then not Is_Constrained (T2)
24032 and then Base_Type (T1) = Base_Type (T2)
24033 then
24034 return True;
24035
24036 -- For now don't bother with case of identical constraints, to be
24037 -- fiddled with later on perhaps (this is only used for optimization
24038 -- purposes, so it is not critical to do a best possible job)
24039
24040 else
24041 return False;
24042 end if;
24043 end Same_Type;
24044
24045 ----------------
24046 -- Same_Value --
24047 ----------------
24048
24049 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
24050 begin
24051 if Compile_Time_Known_Value (Node1)
24052 and then Compile_Time_Known_Value (Node2)
24053 then
24054 -- Handle properly compile-time expressions that are not
24055 -- scalar.
24056
24057 if Is_String_Type (Etype (Node1)) then
24058 return Expr_Value_S (Node1) = Expr_Value_S (Node2);
24059
24060 else
24061 return Expr_Value (Node1) = Expr_Value (Node2);
24062 end if;
24063
24064 elsif Same_Object (Node1, Node2) then
24065 return True;
24066 else
24067 return False;
24068 end if;
24069 end Same_Value;
24070
24071 --------------------
24072 -- Set_SPARK_Mode --
24073 --------------------
24074
24075 procedure Set_SPARK_Mode (Context : Entity_Id) is
24076 begin
24077 -- Do not consider illegal or partially decorated constructs
24078
24079 if Ekind (Context) = E_Void or else Error_Posted (Context) then
24080 null;
24081
24082 elsif Present (SPARK_Pragma (Context)) then
24083 Install_SPARK_Mode
24084 (Mode => Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Context)),
24085 Prag => SPARK_Pragma (Context));
24086 end if;
24087 end Set_SPARK_Mode;
24088
24089 -------------------------
24090 -- Scalar_Part_Present --
24091 -------------------------
24092
24093 function Scalar_Part_Present (Typ : Entity_Id) return Boolean is
24094 Val_Typ : constant Entity_Id := Validated_View (Typ);
24095 Field : Entity_Id;
24096
24097 begin
24098 if Is_Scalar_Type (Val_Typ) then
24099 return True;
24100
24101 elsif Is_Array_Type (Val_Typ) then
24102 return Scalar_Part_Present (Component_Type (Val_Typ));
24103
24104 elsif Is_Record_Type (Val_Typ) then
24105 Field := First_Component_Or_Discriminant (Val_Typ);
24106 while Present (Field) loop
24107 if Scalar_Part_Present (Etype (Field)) then
24108 return True;
24109 end if;
24110
24111 Next_Component_Or_Discriminant (Field);
24112 end loop;
24113 end if;
24114
24115 return False;
24116 end Scalar_Part_Present;
24117
24118 ------------------------
24119 -- Scope_Is_Transient --
24120 ------------------------
24121
24122 function Scope_Is_Transient return Boolean is
24123 begin
24124 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
24125 end Scope_Is_Transient;
24126
24127 ------------------
24128 -- Scope_Within --
24129 ------------------
24130
24131 function Scope_Within
24132 (Inner : Entity_Id;
24133 Outer : Entity_Id) return Boolean
24134 is
24135 Curr : Entity_Id;
24136
24137 begin
24138 Curr := Inner;
24139 while Present (Curr) and then Curr /= Standard_Standard loop
24140 Curr := Scope (Curr);
24141
24142 if Curr = Outer then
24143 return True;
24144
24145 -- A selective accept body appears within a task type, but the
24146 -- enclosing subprogram is the procedure of the task body.
24147
24148 elsif Ekind (Curr) = E_Task_Type
24149 and then Outer = Task_Body_Procedure (Curr)
24150 then
24151 return True;
24152
24153 -- Ditto for the body of a protected operation
24154
24155 elsif Is_Subprogram (Curr)
24156 and then Outer = Protected_Body_Subprogram (Curr)
24157 then
24158 return True;
24159
24160 -- Outside of its scope, a synchronized type may just be private
24161
24162 elsif Is_Private_Type (Curr)
24163 and then Present (Full_View (Curr))
24164 and then Is_Concurrent_Type (Full_View (Curr))
24165 then
24166 return Scope_Within (Full_View (Curr), Outer);
24167 end if;
24168 end loop;
24169
24170 return False;
24171 end Scope_Within;
24172
24173 --------------------------
24174 -- Scope_Within_Or_Same --
24175 --------------------------
24176
24177 function Scope_Within_Or_Same
24178 (Inner : Entity_Id;
24179 Outer : Entity_Id) return Boolean
24180 is
24181 Curr : Entity_Id;
24182
24183 begin
24184 Curr := Inner;
24185 while Present (Curr) and then Curr /= Standard_Standard loop
24186 if Curr = Outer then
24187 return True;
24188 end if;
24189
24190 Curr := Scope (Curr);
24191 end loop;
24192
24193 return False;
24194 end Scope_Within_Or_Same;
24195
24196 --------------------
24197 -- Set_Convention --
24198 --------------------
24199
24200 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
24201 begin
24202 Basic_Set_Convention (E, Val);
24203
24204 if Is_Type (E)
24205 and then Is_Access_Subprogram_Type (Base_Type (E))
24206 and then Has_Foreign_Convention (E)
24207 then
24208 Set_Can_Use_Internal_Rep (E, False);
24209 end if;
24210
24211 -- If E is an object, including a component, and the type of E is an
24212 -- anonymous access type with no convention set, then also set the
24213 -- convention of the anonymous access type. We do not do this for
24214 -- anonymous protected types, since protected types always have the
24215 -- default convention.
24216
24217 if Present (Etype (E))
24218 and then (Is_Object (E)
24219
24220 -- Allow E_Void (happens for pragma Convention appearing
24221 -- in the middle of a record applying to a component)
24222
24223 or else Ekind (E) = E_Void)
24224 then
24225 declare
24226 Typ : constant Entity_Id := Etype (E);
24227
24228 begin
24229 if Ekind_In (Typ, E_Anonymous_Access_Type,
24230 E_Anonymous_Access_Subprogram_Type)
24231 and then not Has_Convention_Pragma (Typ)
24232 then
24233 Basic_Set_Convention (Typ, Val);
24234 Set_Has_Convention_Pragma (Typ);
24235
24236 -- And for the access subprogram type, deal similarly with the
24237 -- designated E_Subprogram_Type, which is always internal.
24238
24239 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
24240 declare
24241 Dtype : constant Entity_Id := Designated_Type (Typ);
24242 begin
24243 if Ekind (Dtype) = E_Subprogram_Type
24244 and then not Has_Convention_Pragma (Dtype)
24245 then
24246 Basic_Set_Convention (Dtype, Val);
24247 Set_Has_Convention_Pragma (Dtype);
24248 end if;
24249 end;
24250 end if;
24251 end if;
24252 end;
24253 end if;
24254 end Set_Convention;
24255
24256 ------------------------
24257 -- Set_Current_Entity --
24258 ------------------------
24259
24260 -- The given entity is to be set as the currently visible definition of its
24261 -- associated name (i.e. the Node_Id associated with its name). All we have
24262 -- to do is to get the name from the identifier, and then set the
24263 -- associated Node_Id to point to the given entity.
24264
24265 procedure Set_Current_Entity (E : Entity_Id) is
24266 begin
24267 Set_Name_Entity_Id (Chars (E), E);
24268 end Set_Current_Entity;
24269
24270 ---------------------------
24271 -- Set_Debug_Info_Needed --
24272 ---------------------------
24273
24274 procedure Set_Debug_Info_Needed (T : Entity_Id) is
24275
24276 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
24277 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
24278 -- Used to set debug info in a related node if not set already
24279
24280 --------------------------------------
24281 -- Set_Debug_Info_Needed_If_Not_Set --
24282 --------------------------------------
24283
24284 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
24285 begin
24286 if Present (E) and then not Needs_Debug_Info (E) then
24287 Set_Debug_Info_Needed (E);
24288
24289 -- For a private type, indicate that the full view also needs
24290 -- debug information.
24291
24292 if Is_Type (E)
24293 and then Is_Private_Type (E)
24294 and then Present (Full_View (E))
24295 then
24296 Set_Debug_Info_Needed (Full_View (E));
24297 end if;
24298 end if;
24299 end Set_Debug_Info_Needed_If_Not_Set;
24300
24301 -- Start of processing for Set_Debug_Info_Needed
24302
24303 begin
24304 -- Nothing to do if there is no available entity
24305
24306 if No (T) then
24307 return;
24308
24309 -- Nothing to do for an entity with suppressed debug information
24310
24311 elsif Debug_Info_Off (T) then
24312 return;
24313
24314 -- Nothing to do for an ignored Ghost entity because the entity will be
24315 -- eliminated from the tree.
24316
24317 elsif Is_Ignored_Ghost_Entity (T) then
24318 return;
24319
24320 -- Nothing to do if entity comes from a predefined file. Library files
24321 -- are compiled without debug information, but inlined bodies of these
24322 -- routines may appear in user code, and debug information on them ends
24323 -- up complicating debugging the user code.
24324
24325 elsif In_Inlined_Body and then In_Predefined_Unit (T) then
24326 Set_Needs_Debug_Info (T, False);
24327 end if;
24328
24329 -- Set flag in entity itself. Note that we will go through the following
24330 -- circuitry even if the flag is already set on T. That's intentional,
24331 -- it makes sure that the flag will be set in subsidiary entities.
24332
24333 Set_Needs_Debug_Info (T);
24334
24335 -- Set flag on subsidiary entities if not set already
24336
24337 if Is_Object (T) then
24338 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
24339
24340 elsif Is_Type (T) then
24341 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
24342
24343 if Is_Record_Type (T) then
24344 declare
24345 Ent : Entity_Id := First_Entity (T);
24346 begin
24347 while Present (Ent) loop
24348 Set_Debug_Info_Needed_If_Not_Set (Ent);
24349 Next_Entity (Ent);
24350 end loop;
24351 end;
24352
24353 -- For a class wide subtype, we also need debug information
24354 -- for the equivalent type.
24355
24356 if Ekind (T) = E_Class_Wide_Subtype then
24357 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
24358 end if;
24359
24360 elsif Is_Array_Type (T) then
24361 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
24362
24363 declare
24364 Indx : Node_Id := First_Index (T);
24365 begin
24366 while Present (Indx) loop
24367 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
24368 Indx := Next_Index (Indx);
24369 end loop;
24370 end;
24371
24372 -- For a packed array type, we also need debug information for
24373 -- the type used to represent the packed array. Conversely, we
24374 -- also need it for the former if we need it for the latter.
24375
24376 if Is_Packed (T) then
24377 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
24378 end if;
24379
24380 if Is_Packed_Array_Impl_Type (T) then
24381 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
24382 end if;
24383
24384 elsif Is_Access_Type (T) then
24385 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
24386
24387 elsif Is_Private_Type (T) then
24388 declare
24389 FV : constant Entity_Id := Full_View (T);
24390
24391 begin
24392 Set_Debug_Info_Needed_If_Not_Set (FV);
24393
24394 -- If the full view is itself a derived private type, we need
24395 -- debug information on its underlying type.
24396
24397 if Present (FV)
24398 and then Is_Private_Type (FV)
24399 and then Present (Underlying_Full_View (FV))
24400 then
24401 Set_Needs_Debug_Info (Underlying_Full_View (FV));
24402 end if;
24403 end;
24404
24405 elsif Is_Protected_Type (T) then
24406 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
24407
24408 elsif Is_Scalar_Type (T) then
24409
24410 -- If the subrange bounds are materialized by dedicated constant
24411 -- objects, also include them in the debug info to make sure the
24412 -- debugger can properly use them.
24413
24414 if Present (Scalar_Range (T))
24415 and then Nkind (Scalar_Range (T)) = N_Range
24416 then
24417 declare
24418 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
24419 High_Bnd : constant Node_Id := Type_High_Bound (T);
24420
24421 begin
24422 if Is_Entity_Name (Low_Bnd) then
24423 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
24424 end if;
24425
24426 if Is_Entity_Name (High_Bnd) then
24427 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
24428 end if;
24429 end;
24430 end if;
24431 end if;
24432 end if;
24433 end Set_Debug_Info_Needed;
24434
24435 ----------------------------
24436 -- Set_Entity_With_Checks --
24437 ----------------------------
24438
24439 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
24440 Val_Actual : Entity_Id;
24441 Nod : Node_Id;
24442 Post_Node : Node_Id;
24443
24444 begin
24445 -- Unconditionally set the entity
24446
24447 Set_Entity (N, Val);
24448
24449 -- The node to post on is the selector in the case of an expanded name,
24450 -- and otherwise the node itself.
24451
24452 if Nkind (N) = N_Expanded_Name then
24453 Post_Node := Selector_Name (N);
24454 else
24455 Post_Node := N;
24456 end if;
24457
24458 -- Check for violation of No_Fixed_IO
24459
24460 if Restriction_Check_Required (No_Fixed_IO)
24461 and then
24462 ((RTU_Loaded (Ada_Text_IO)
24463 and then (Is_RTE (Val, RE_Decimal_IO)
24464 or else
24465 Is_RTE (Val, RE_Fixed_IO)))
24466
24467 or else
24468 (RTU_Loaded (Ada_Wide_Text_IO)
24469 and then (Is_RTE (Val, RO_WT_Decimal_IO)
24470 or else
24471 Is_RTE (Val, RO_WT_Fixed_IO)))
24472
24473 or else
24474 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
24475 and then (Is_RTE (Val, RO_WW_Decimal_IO)
24476 or else
24477 Is_RTE (Val, RO_WW_Fixed_IO))))
24478
24479 -- A special extra check, don't complain about a reference from within
24480 -- the Ada.Interrupts package itself!
24481
24482 and then not In_Same_Extended_Unit (N, Val)
24483 then
24484 Check_Restriction (No_Fixed_IO, Post_Node);
24485 end if;
24486
24487 -- Remaining checks are only done on source nodes. Note that we test
24488 -- for violation of No_Fixed_IO even on non-source nodes, because the
24489 -- cases for checking violations of this restriction are instantiations
24490 -- where the reference in the instance has Comes_From_Source False.
24491
24492 if not Comes_From_Source (N) then
24493 return;
24494 end if;
24495
24496 -- Check for violation of No_Abort_Statements, which is triggered by
24497 -- call to Ada.Task_Identification.Abort_Task.
24498
24499 if Restriction_Check_Required (No_Abort_Statements)
24500 and then (Is_RTE (Val, RE_Abort_Task))
24501
24502 -- A special extra check, don't complain about a reference from within
24503 -- the Ada.Task_Identification package itself!
24504
24505 and then not In_Same_Extended_Unit (N, Val)
24506 then
24507 Check_Restriction (No_Abort_Statements, Post_Node);
24508 end if;
24509
24510 if Val = Standard_Long_Long_Integer then
24511 Check_Restriction (No_Long_Long_Integers, Post_Node);
24512 end if;
24513
24514 -- Check for violation of No_Dynamic_Attachment
24515
24516 if Restriction_Check_Required (No_Dynamic_Attachment)
24517 and then RTU_Loaded (Ada_Interrupts)
24518 and then (Is_RTE (Val, RE_Is_Reserved) or else
24519 Is_RTE (Val, RE_Is_Attached) or else
24520 Is_RTE (Val, RE_Current_Handler) or else
24521 Is_RTE (Val, RE_Attach_Handler) or else
24522 Is_RTE (Val, RE_Exchange_Handler) or else
24523 Is_RTE (Val, RE_Detach_Handler) or else
24524 Is_RTE (Val, RE_Reference))
24525
24526 -- A special extra check, don't complain about a reference from within
24527 -- the Ada.Interrupts package itself!
24528
24529 and then not In_Same_Extended_Unit (N, Val)
24530 then
24531 Check_Restriction (No_Dynamic_Attachment, Post_Node);
24532 end if;
24533
24534 -- Check for No_Implementation_Identifiers
24535
24536 if Restriction_Check_Required (No_Implementation_Identifiers) then
24537
24538 -- We have an implementation defined entity if it is marked as
24539 -- implementation defined, or is defined in a package marked as
24540 -- implementation defined. However, library packages themselves
24541 -- are excluded (we don't want to flag Interfaces itself, just
24542 -- the entities within it).
24543
24544 if (Is_Implementation_Defined (Val)
24545 or else
24546 (Present (Scope (Val))
24547 and then Is_Implementation_Defined (Scope (Val))))
24548 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
24549 and then Is_Library_Level_Entity (Val))
24550 then
24551 Check_Restriction (No_Implementation_Identifiers, Post_Node);
24552 end if;
24553 end if;
24554
24555 -- Do the style check
24556
24557 if Style_Check
24558 and then not Suppress_Style_Checks (Val)
24559 and then not In_Instance
24560 then
24561 if Nkind (N) = N_Identifier then
24562 Nod := N;
24563 elsif Nkind (N) = N_Expanded_Name then
24564 Nod := Selector_Name (N);
24565 else
24566 return;
24567 end if;
24568
24569 -- A special situation arises for derived operations, where we want
24570 -- to do the check against the parent (since the Sloc of the derived
24571 -- operation points to the derived type declaration itself).
24572
24573 Val_Actual := Val;
24574 while not Comes_From_Source (Val_Actual)
24575 and then Nkind (Val_Actual) in N_Entity
24576 and then (Ekind (Val_Actual) = E_Enumeration_Literal
24577 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
24578 and then Present (Alias (Val_Actual))
24579 loop
24580 Val_Actual := Alias (Val_Actual);
24581 end loop;
24582
24583 -- Renaming declarations for generic actuals do not come from source,
24584 -- and have a different name from that of the entity they rename, so
24585 -- there is no style check to perform here.
24586
24587 if Chars (Nod) = Chars (Val_Actual) then
24588 Style.Check_Identifier (Nod, Val_Actual);
24589 end if;
24590 end if;
24591
24592 Set_Entity (N, Val);
24593 end Set_Entity_With_Checks;
24594
24595 ------------------------------
24596 -- Set_Invalid_Scalar_Value --
24597 ------------------------------
24598
24599 procedure Set_Invalid_Scalar_Value
24600 (Scal_Typ : Float_Scalar_Id;
24601 Value : Ureal)
24602 is
24603 Slot : Ureal renames Invalid_Floats (Scal_Typ);
24604
24605 begin
24606 -- Detect an attempt to set a different value for the same scalar type
24607
24608 pragma Assert (Slot = No_Ureal);
24609 Slot := Value;
24610 end Set_Invalid_Scalar_Value;
24611
24612 ------------------------------
24613 -- Set_Invalid_Scalar_Value --
24614 ------------------------------
24615
24616 procedure Set_Invalid_Scalar_Value
24617 (Scal_Typ : Integer_Scalar_Id;
24618 Value : Uint)
24619 is
24620 Slot : Uint renames Invalid_Integers (Scal_Typ);
24621
24622 begin
24623 -- Detect an attempt to set a different value for the same scalar type
24624
24625 pragma Assert (Slot = No_Uint);
24626 Slot := Value;
24627 end Set_Invalid_Scalar_Value;
24628
24629 ------------------------
24630 -- Set_Name_Entity_Id --
24631 ------------------------
24632
24633 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
24634 begin
24635 Set_Name_Table_Int (Id, Int (Val));
24636 end Set_Name_Entity_Id;
24637
24638 ---------------------
24639 -- Set_Next_Actual --
24640 ---------------------
24641
24642 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
24643 begin
24644 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
24645 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
24646 end if;
24647 end Set_Next_Actual;
24648
24649 ----------------------------------
24650 -- Set_Optimize_Alignment_Flags --
24651 ----------------------------------
24652
24653 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
24654 begin
24655 if Optimize_Alignment = 'S' then
24656 Set_Optimize_Alignment_Space (E);
24657 elsif Optimize_Alignment = 'T' then
24658 Set_Optimize_Alignment_Time (E);
24659 end if;
24660 end Set_Optimize_Alignment_Flags;
24661
24662 -----------------------
24663 -- Set_Public_Status --
24664 -----------------------
24665
24666 procedure Set_Public_Status (Id : Entity_Id) is
24667 S : constant Entity_Id := Current_Scope;
24668
24669 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
24670 -- Determines if E is defined within handled statement sequence or
24671 -- an if statement, returns True if so, False otherwise.
24672
24673 ----------------------
24674 -- Within_HSS_Or_If --
24675 ----------------------
24676
24677 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
24678 N : Node_Id;
24679 begin
24680 N := Declaration_Node (E);
24681 loop
24682 N := Parent (N);
24683
24684 if No (N) then
24685 return False;
24686
24687 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
24688 N_If_Statement)
24689 then
24690 return True;
24691 end if;
24692 end loop;
24693 end Within_HSS_Or_If;
24694
24695 -- Start of processing for Set_Public_Status
24696
24697 begin
24698 -- Everything in the scope of Standard is public
24699
24700 if S = Standard_Standard then
24701 Set_Is_Public (Id);
24702
24703 -- Entity is definitely not public if enclosing scope is not public
24704
24705 elsif not Is_Public (S) then
24706 return;
24707
24708 -- An object or function declaration that occurs in a handled sequence
24709 -- of statements or within an if statement is the declaration for a
24710 -- temporary object or local subprogram generated by the expander. It
24711 -- never needs to be made public and furthermore, making it public can
24712 -- cause back end problems.
24713
24714 elsif Nkind_In (Parent (Id), N_Object_Declaration,
24715 N_Function_Specification)
24716 and then Within_HSS_Or_If (Id)
24717 then
24718 return;
24719
24720 -- Entities in public packages or records are public
24721
24722 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
24723 Set_Is_Public (Id);
24724
24725 -- The bounds of an entry family declaration can generate object
24726 -- declarations that are visible to the back-end, e.g. in the
24727 -- the declaration of a composite type that contains tasks.
24728
24729 elsif Is_Concurrent_Type (S)
24730 and then not Has_Completion (S)
24731 and then Nkind (Parent (Id)) = N_Object_Declaration
24732 then
24733 Set_Is_Public (Id);
24734 end if;
24735 end Set_Public_Status;
24736
24737 -----------------------------
24738 -- Set_Referenced_Modified --
24739 -----------------------------
24740
24741 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
24742 Pref : Node_Id;
24743
24744 begin
24745 -- Deal with indexed or selected component where prefix is modified
24746
24747 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
24748 Pref := Prefix (N);
24749
24750 -- If prefix is access type, then it is the designated object that is
24751 -- being modified, which means we have no entity to set the flag on.
24752
24753 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
24754 return;
24755
24756 -- Otherwise chase the prefix
24757
24758 else
24759 Set_Referenced_Modified (Pref, Out_Param);
24760 end if;
24761
24762 -- Otherwise see if we have an entity name (only other case to process)
24763
24764 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
24765 Set_Referenced_As_LHS (Entity (N), not Out_Param);
24766 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
24767 end if;
24768 end Set_Referenced_Modified;
24769
24770 ------------------
24771 -- Set_Rep_Info --
24772 ------------------
24773
24774 procedure Set_Rep_Info (T1 : Entity_Id; T2 : Entity_Id) is
24775 begin
24776 Set_Is_Atomic (T1, Is_Atomic (T2));
24777 Set_Is_Independent (T1, Is_Independent (T2));
24778 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
24779
24780 if Is_Base_Type (T1) then
24781 Set_Is_Volatile (T1, Is_Volatile (T2));
24782 end if;
24783 end Set_Rep_Info;
24784
24785 ----------------------------
24786 -- Set_Scope_Is_Transient --
24787 ----------------------------
24788
24789 procedure Set_Scope_Is_Transient (V : Boolean := True) is
24790 begin
24791 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
24792 end Set_Scope_Is_Transient;
24793
24794 -------------------
24795 -- Set_Size_Info --
24796 -------------------
24797
24798 procedure Set_Size_Info (T1, T2 : Entity_Id) is
24799 begin
24800 -- We copy Esize, but not RM_Size, since in general RM_Size is
24801 -- subtype specific and does not get inherited by all subtypes.
24802
24803 Set_Esize (T1, Esize (T2));
24804 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
24805
24806 if Is_Discrete_Or_Fixed_Point_Type (T1)
24807 and then
24808 Is_Discrete_Or_Fixed_Point_Type (T2)
24809 then
24810 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
24811 end if;
24812
24813 Set_Alignment (T1, Alignment (T2));
24814 end Set_Size_Info;
24815
24816 ------------------------------
24817 -- Should_Ignore_Pragma_Par --
24818 ------------------------------
24819
24820 function Should_Ignore_Pragma_Par (Prag_Name : Name_Id) return Boolean is
24821 pragma Assert (Compiler_State = Parsing);
24822 -- This one can't work during semantic analysis, because we don't have a
24823 -- correct Current_Source_File.
24824
24825 Result : constant Boolean :=
24826 Get_Name_Table_Boolean3 (Prag_Name)
24827 and then not Is_Internal_File_Name
24828 (File_Name (Current_Source_File));
24829 begin
24830 return Result;
24831 end Should_Ignore_Pragma_Par;
24832
24833 ------------------------------
24834 -- Should_Ignore_Pragma_Sem --
24835 ------------------------------
24836
24837 function Should_Ignore_Pragma_Sem (N : Node_Id) return Boolean is
24838 pragma Assert (Compiler_State = Analyzing);
24839 Prag_Name : constant Name_Id := Pragma_Name (N);
24840 Result : constant Boolean :=
24841 Get_Name_Table_Boolean3 (Prag_Name)
24842 and then not In_Internal_Unit (N);
24843
24844 begin
24845 return Result;
24846 end Should_Ignore_Pragma_Sem;
24847
24848 --------------------
24849 -- Static_Boolean --
24850 --------------------
24851
24852 function Static_Boolean (N : Node_Id) return Uint is
24853 begin
24854 Analyze_And_Resolve (N, Standard_Boolean);
24855
24856 if N = Error
24857 or else Error_Posted (N)
24858 or else Etype (N) = Any_Type
24859 then
24860 return No_Uint;
24861 end if;
24862
24863 if Is_OK_Static_Expression (N) then
24864 if not Raises_Constraint_Error (N) then
24865 return Expr_Value (N);
24866 else
24867 return No_Uint;
24868 end if;
24869
24870 elsif Etype (N) = Any_Type then
24871 return No_Uint;
24872
24873 else
24874 Flag_Non_Static_Expr
24875 ("static boolean expression required here", N);
24876 return No_Uint;
24877 end if;
24878 end Static_Boolean;
24879
24880 --------------------
24881 -- Static_Integer --
24882 --------------------
24883
24884 function Static_Integer (N : Node_Id) return Uint is
24885 begin
24886 Analyze_And_Resolve (N, Any_Integer);
24887
24888 if N = Error
24889 or else Error_Posted (N)
24890 or else Etype (N) = Any_Type
24891 then
24892 return No_Uint;
24893 end if;
24894
24895 if Is_OK_Static_Expression (N) then
24896 if not Raises_Constraint_Error (N) then
24897 return Expr_Value (N);
24898 else
24899 return No_Uint;
24900 end if;
24901
24902 elsif Etype (N) = Any_Type then
24903 return No_Uint;
24904
24905 else
24906 Flag_Non_Static_Expr
24907 ("static integer expression required here", N);
24908 return No_Uint;
24909 end if;
24910 end Static_Integer;
24911
24912 --------------------------
24913 -- Statically_Different --
24914 --------------------------
24915
24916 function Statically_Different (E1, E2 : Node_Id) return Boolean is
24917 R1 : constant Node_Id := Get_Referenced_Object (E1);
24918 R2 : constant Node_Id := Get_Referenced_Object (E2);
24919 begin
24920 return Is_Entity_Name (R1)
24921 and then Is_Entity_Name (R2)
24922 and then Entity (R1) /= Entity (R2)
24923 and then not Is_Formal (Entity (R1))
24924 and then not Is_Formal (Entity (R2));
24925 end Statically_Different;
24926
24927 --------------------------------------
24928 -- Subject_To_Loop_Entry_Attributes --
24929 --------------------------------------
24930
24931 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
24932 Stmt : Node_Id;
24933
24934 begin
24935 Stmt := N;
24936
24937 -- The expansion mechanism transform a loop subject to at least one
24938 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
24939 -- the conditional part.
24940
24941 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
24942 and then Nkind (Original_Node (N)) = N_Loop_Statement
24943 then
24944 Stmt := Original_Node (N);
24945 end if;
24946
24947 return
24948 Nkind (Stmt) = N_Loop_Statement
24949 and then Present (Identifier (Stmt))
24950 and then Present (Entity (Identifier (Stmt)))
24951 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
24952 end Subject_To_Loop_Entry_Attributes;
24953
24954 -----------------------------
24955 -- Subprogram_Access_Level --
24956 -----------------------------
24957
24958 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
24959 begin
24960 if Present (Alias (Subp)) then
24961 return Subprogram_Access_Level (Alias (Subp));
24962 else
24963 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
24964 end if;
24965 end Subprogram_Access_Level;
24966
24967 ---------------------
24968 -- Subprogram_Name --
24969 ---------------------
24970
24971 function Subprogram_Name (N : Node_Id) return String is
24972 Buf : Bounded_String;
24973 Ent : Node_Id := N;
24974 Nod : Node_Id;
24975
24976 begin
24977 while Present (Ent) loop
24978 case Nkind (Ent) is
24979 when N_Subprogram_Body =>
24980 Ent := Defining_Unit_Name (Specification (Ent));
24981 exit;
24982
24983 when N_Subprogram_Declaration =>
24984 Nod := Corresponding_Body (Ent);
24985
24986 if Present (Nod) then
24987 Ent := Nod;
24988 else
24989 Ent := Defining_Unit_Name (Specification (Ent));
24990 end if;
24991
24992 exit;
24993
24994 when N_Subprogram_Instantiation
24995 | N_Package_Body
24996 | N_Package_Specification
24997 =>
24998 Ent := Defining_Unit_Name (Ent);
24999 exit;
25000
25001 when N_Protected_Type_Declaration =>
25002 Ent := Corresponding_Body (Ent);
25003 exit;
25004
25005 when N_Protected_Body
25006 | N_Task_Body
25007 =>
25008 Ent := Defining_Identifier (Ent);
25009 exit;
25010
25011 when others =>
25012 null;
25013 end case;
25014
25015 Ent := Parent (Ent);
25016 end loop;
25017
25018 if No (Ent) then
25019 return "unknown subprogram:unknown file:0:0";
25020 end if;
25021
25022 -- If the subprogram is a child unit, use its simple name to start the
25023 -- construction of the fully qualified name.
25024
25025 if Nkind (Ent) = N_Defining_Program_Unit_Name then
25026 Ent := Defining_Identifier (Ent);
25027 end if;
25028
25029 Append_Entity_Name (Buf, Ent);
25030
25031 -- Append homonym number if needed
25032
25033 if Nkind (N) in N_Entity and then Has_Homonym (N) then
25034 declare
25035 H : Entity_Id := Homonym (N);
25036 Nr : Nat := 1;
25037
25038 begin
25039 while Present (H) loop
25040 if Scope (H) = Scope (N) then
25041 Nr := Nr + 1;
25042 end if;
25043
25044 H := Homonym (H);
25045 end loop;
25046
25047 if Nr > 1 then
25048 Append (Buf, '#');
25049 Append (Buf, Nr);
25050 end if;
25051 end;
25052 end if;
25053
25054 -- Append source location of Ent to Buf so that the string will
25055 -- look like "subp:file:line:col".
25056
25057 declare
25058 Loc : constant Source_Ptr := Sloc (Ent);
25059 begin
25060 Append (Buf, ':');
25061 Append (Buf, Reference_Name (Get_Source_File_Index (Loc)));
25062 Append (Buf, ':');
25063 Append (Buf, Nat (Get_Logical_Line_Number (Loc)));
25064 Append (Buf, ':');
25065 Append (Buf, Nat (Get_Column_Number (Loc)));
25066 end;
25067
25068 return +Buf;
25069 end Subprogram_Name;
25070
25071 -------------------------------
25072 -- Support_Atomic_Primitives --
25073 -------------------------------
25074
25075 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
25076 Size : Int;
25077
25078 begin
25079 -- Verify the alignment of Typ is known
25080
25081 if not Known_Alignment (Typ) then
25082 return False;
25083 end if;
25084
25085 if Known_Static_Esize (Typ) then
25086 Size := UI_To_Int (Esize (Typ));
25087
25088 -- If the Esize (Object_Size) is unknown at compile time, look at the
25089 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
25090
25091 elsif Known_Static_RM_Size (Typ) then
25092 Size := UI_To_Int (RM_Size (Typ));
25093
25094 -- Otherwise, the size is considered to be unknown.
25095
25096 else
25097 return False;
25098 end if;
25099
25100 -- Check that the size of the component is 8, 16, 32, or 64 bits and
25101 -- that Typ is properly aligned.
25102
25103 case Size is
25104 when 8 | 16 | 32 | 64 =>
25105 return Size = UI_To_Int (Alignment (Typ)) * 8;
25106
25107 when others =>
25108 return False;
25109 end case;
25110 end Support_Atomic_Primitives;
25111
25112 -----------------
25113 -- Trace_Scope --
25114 -----------------
25115
25116 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
25117 begin
25118 if Debug_Flag_W then
25119 for J in 0 .. Scope_Stack.Last loop
25120 Write_Str (" ");
25121 end loop;
25122
25123 Write_Str (Msg);
25124 Write_Name (Chars (E));
25125 Write_Str (" from ");
25126 Write_Location (Sloc (N));
25127 Write_Eol;
25128 end if;
25129 end Trace_Scope;
25130
25131 -----------------------
25132 -- Transfer_Entities --
25133 -----------------------
25134
25135 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
25136 procedure Set_Public_Status_Of (Id : Entity_Id);
25137 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
25138 -- Set_Public_Status. If successful and Id denotes a record type, set
25139 -- the Is_Public attribute of its fields.
25140
25141 --------------------------
25142 -- Set_Public_Status_Of --
25143 --------------------------
25144
25145 procedure Set_Public_Status_Of (Id : Entity_Id) is
25146 Field : Entity_Id;
25147
25148 begin
25149 if not Is_Public (Id) then
25150 Set_Public_Status (Id);
25151
25152 -- When the input entity is a public record type, ensure that all
25153 -- its internal fields are also exposed to the linker. The fields
25154 -- of a class-wide type are never made public.
25155
25156 if Is_Public (Id)
25157 and then Is_Record_Type (Id)
25158 and then not Is_Class_Wide_Type (Id)
25159 then
25160 Field := First_Entity (Id);
25161 while Present (Field) loop
25162 Set_Is_Public (Field);
25163 Next_Entity (Field);
25164 end loop;
25165 end if;
25166 end if;
25167 end Set_Public_Status_Of;
25168
25169 -- Local variables
25170
25171 Full_Id : Entity_Id;
25172 Id : Entity_Id;
25173
25174 -- Start of processing for Transfer_Entities
25175
25176 begin
25177 Id := First_Entity (From);
25178
25179 if Present (Id) then
25180
25181 -- Merge the entity chain of the source scope with that of the
25182 -- destination scope.
25183
25184 if Present (Last_Entity (To)) then
25185 Link_Entities (Last_Entity (To), Id);
25186 else
25187 Set_First_Entity (To, Id);
25188 end if;
25189
25190 Set_Last_Entity (To, Last_Entity (From));
25191
25192 -- Inspect the entities of the source scope and update their Scope
25193 -- attribute.
25194
25195 while Present (Id) loop
25196 Set_Scope (Id, To);
25197 Set_Public_Status_Of (Id);
25198
25199 -- Handle an internally generated full view for a private type
25200
25201 if Is_Private_Type (Id)
25202 and then Present (Full_View (Id))
25203 and then Is_Itype (Full_View (Id))
25204 then
25205 Full_Id := Full_View (Id);
25206
25207 Set_Scope (Full_Id, To);
25208 Set_Public_Status_Of (Full_Id);
25209 end if;
25210
25211 Next_Entity (Id);
25212 end loop;
25213
25214 Set_First_Entity (From, Empty);
25215 Set_Last_Entity (From, Empty);
25216 end if;
25217 end Transfer_Entities;
25218
25219 -----------------------
25220 -- Type_Access_Level --
25221 -----------------------
25222
25223 function Type_Access_Level (Typ : Entity_Id) return Uint is
25224 Btyp : Entity_Id;
25225
25226 begin
25227 Btyp := Base_Type (Typ);
25228
25229 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
25230 -- simply use the level where the type is declared. This is true for
25231 -- stand-alone object declarations, and for anonymous access types
25232 -- associated with components the level is the same as that of the
25233 -- enclosing composite type. However, special treatment is needed for
25234 -- the cases of access parameters, return objects of an anonymous access
25235 -- type, and, in Ada 95, access discriminants of limited types.
25236
25237 if Is_Access_Type (Btyp) then
25238 if Ekind (Btyp) = E_Anonymous_Access_Type then
25239
25240 -- If the type is a nonlocal anonymous access type (such as for
25241 -- an access parameter) we treat it as being declared at the
25242 -- library level to ensure that names such as X.all'access don't
25243 -- fail static accessibility checks.
25244
25245 if not Is_Local_Anonymous_Access (Typ) then
25246 return Scope_Depth (Standard_Standard);
25247
25248 -- If this is a return object, the accessibility level is that of
25249 -- the result subtype of the enclosing function. The test here is
25250 -- little complicated, because we have to account for extended
25251 -- return statements that have been rewritten as blocks, in which
25252 -- case we have to find and the Is_Return_Object attribute of the
25253 -- itype's associated object. It would be nice to find a way to
25254 -- simplify this test, but it doesn't seem worthwhile to add a new
25255 -- flag just for purposes of this test. ???
25256
25257 elsif Ekind (Scope (Btyp)) = E_Return_Statement
25258 or else
25259 (Is_Itype (Btyp)
25260 and then Nkind (Associated_Node_For_Itype (Btyp)) =
25261 N_Object_Declaration
25262 and then Is_Return_Object
25263 (Defining_Identifier
25264 (Associated_Node_For_Itype (Btyp))))
25265 then
25266 declare
25267 Scop : Entity_Id;
25268
25269 begin
25270 Scop := Scope (Scope (Btyp));
25271 while Present (Scop) loop
25272 exit when Ekind (Scop) = E_Function;
25273 Scop := Scope (Scop);
25274 end loop;
25275
25276 -- Treat the return object's type as having the level of the
25277 -- function's result subtype (as per RM05-6.5(5.3/2)).
25278
25279 return Type_Access_Level (Etype (Scop));
25280 end;
25281 end if;
25282 end if;
25283
25284 Btyp := Root_Type (Btyp);
25285
25286 -- The accessibility level of anonymous access types associated with
25287 -- discriminants is that of the current instance of the type, and
25288 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
25289
25290 -- AI-402: access discriminants have accessibility based on the
25291 -- object rather than the type in Ada 2005, so the above paragraph
25292 -- doesn't apply.
25293
25294 -- ??? Needs completion with rules from AI-416
25295
25296 if Ada_Version <= Ada_95
25297 and then Ekind (Typ) = E_Anonymous_Access_Type
25298 and then Present (Associated_Node_For_Itype (Typ))
25299 and then Nkind (Associated_Node_For_Itype (Typ)) =
25300 N_Discriminant_Specification
25301 then
25302 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
25303 end if;
25304 end if;
25305
25306 -- Return library level for a generic formal type. This is done because
25307 -- RM(10.3.2) says that "The statically deeper relationship does not
25308 -- apply to ... a descendant of a generic formal type". Rather than
25309 -- checking at each point where a static accessibility check is
25310 -- performed to see if we are dealing with a formal type, this rule is
25311 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
25312 -- return extreme values for a formal type; Deepest_Type_Access_Level
25313 -- returns Int'Last. By calling the appropriate function from among the
25314 -- two, we ensure that the static accessibility check will pass if we
25315 -- happen to run into a formal type. More specifically, we should call
25316 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
25317 -- call occurs as part of a static accessibility check and the error
25318 -- case is the case where the type's level is too shallow (as opposed
25319 -- to too deep).
25320
25321 if Is_Generic_Type (Root_Type (Btyp)) then
25322 return Scope_Depth (Standard_Standard);
25323 end if;
25324
25325 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
25326 end Type_Access_Level;
25327
25328 ------------------------------------
25329 -- Type_Without_Stream_Operation --
25330 ------------------------------------
25331
25332 function Type_Without_Stream_Operation
25333 (T : Entity_Id;
25334 Op : TSS_Name_Type := TSS_Null) return Entity_Id
25335 is
25336 BT : constant Entity_Id := Base_Type (T);
25337 Op_Missing : Boolean;
25338
25339 begin
25340 if not Restriction_Active (No_Default_Stream_Attributes) then
25341 return Empty;
25342 end if;
25343
25344 if Is_Elementary_Type (T) then
25345 if Op = TSS_Null then
25346 Op_Missing :=
25347 No (TSS (BT, TSS_Stream_Read))
25348 or else No (TSS (BT, TSS_Stream_Write));
25349
25350 else
25351 Op_Missing := No (TSS (BT, Op));
25352 end if;
25353
25354 if Op_Missing then
25355 return T;
25356 else
25357 return Empty;
25358 end if;
25359
25360 elsif Is_Array_Type (T) then
25361 return Type_Without_Stream_Operation (Component_Type (T), Op);
25362
25363 elsif Is_Record_Type (T) then
25364 declare
25365 Comp : Entity_Id;
25366 C_Typ : Entity_Id;
25367
25368 begin
25369 Comp := First_Component (T);
25370 while Present (Comp) loop
25371 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
25372
25373 if Present (C_Typ) then
25374 return C_Typ;
25375 end if;
25376
25377 Next_Component (Comp);
25378 end loop;
25379
25380 return Empty;
25381 end;
25382
25383 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
25384 return Type_Without_Stream_Operation (Full_View (T), Op);
25385 else
25386 return Empty;
25387 end if;
25388 end Type_Without_Stream_Operation;
25389
25390 ---------------------
25391 -- Ultimate_Prefix --
25392 ---------------------
25393
25394 function Ultimate_Prefix (N : Node_Id) return Node_Id is
25395 Pref : Node_Id;
25396
25397 begin
25398 Pref := N;
25399 while Nkind_In (Pref, N_Explicit_Dereference,
25400 N_Indexed_Component,
25401 N_Selected_Component,
25402 N_Slice)
25403 loop
25404 Pref := Prefix (Pref);
25405 end loop;
25406
25407 return Pref;
25408 end Ultimate_Prefix;
25409
25410 ----------------------------
25411 -- Unique_Defining_Entity --
25412 ----------------------------
25413
25414 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
25415 begin
25416 return Unique_Entity (Defining_Entity (N));
25417 end Unique_Defining_Entity;
25418
25419 -------------------
25420 -- Unique_Entity --
25421 -------------------
25422
25423 function Unique_Entity (E : Entity_Id) return Entity_Id is
25424 U : Entity_Id := E;
25425 P : Node_Id;
25426
25427 begin
25428 case Ekind (E) is
25429 when E_Constant =>
25430 if Present (Full_View (E)) then
25431 U := Full_View (E);
25432 end if;
25433
25434 when Entry_Kind =>
25435 if Nkind (Parent (E)) = N_Entry_Body then
25436 declare
25437 Prot_Item : Entity_Id;
25438 Prot_Type : Entity_Id;
25439
25440 begin
25441 if Ekind (E) = E_Entry then
25442 Prot_Type := Scope (E);
25443
25444 -- Bodies of entry families are nested within an extra scope
25445 -- that contains an entry index declaration.
25446
25447 else
25448 Prot_Type := Scope (Scope (E));
25449 end if;
25450
25451 -- A protected type may be declared as a private type, in
25452 -- which case we need to get its full view.
25453
25454 if Is_Private_Type (Prot_Type) then
25455 Prot_Type := Full_View (Prot_Type);
25456 end if;
25457
25458 -- Full view may not be present on error, in which case
25459 -- return E by default.
25460
25461 if Present (Prot_Type) then
25462 pragma Assert (Ekind (Prot_Type) = E_Protected_Type);
25463
25464 -- Traverse the entity list of the protected type and
25465 -- locate an entry declaration which matches the entry
25466 -- body.
25467
25468 Prot_Item := First_Entity (Prot_Type);
25469 while Present (Prot_Item) loop
25470 if Ekind (Prot_Item) in Entry_Kind
25471 and then Corresponding_Body (Parent (Prot_Item)) = E
25472 then
25473 U := Prot_Item;
25474 exit;
25475 end if;
25476
25477 Next_Entity (Prot_Item);
25478 end loop;
25479 end if;
25480 end;
25481 end if;
25482
25483 when Formal_Kind =>
25484 if Present (Spec_Entity (E)) then
25485 U := Spec_Entity (E);
25486 end if;
25487
25488 when E_Package_Body =>
25489 P := Parent (E);
25490
25491 if Nkind (P) = N_Defining_Program_Unit_Name then
25492 P := Parent (P);
25493 end if;
25494
25495 if Nkind (P) = N_Package_Body
25496 and then Present (Corresponding_Spec (P))
25497 then
25498 U := Corresponding_Spec (P);
25499
25500 elsif Nkind (P) = N_Package_Body_Stub
25501 and then Present (Corresponding_Spec_Of_Stub (P))
25502 then
25503 U := Corresponding_Spec_Of_Stub (P);
25504 end if;
25505
25506 when E_Protected_Body =>
25507 P := Parent (E);
25508
25509 if Nkind (P) = N_Protected_Body
25510 and then Present (Corresponding_Spec (P))
25511 then
25512 U := Corresponding_Spec (P);
25513
25514 elsif Nkind (P) = N_Protected_Body_Stub
25515 and then Present (Corresponding_Spec_Of_Stub (P))
25516 then
25517 U := Corresponding_Spec_Of_Stub (P);
25518
25519 if Is_Single_Protected_Object (U) then
25520 U := Etype (U);
25521 end if;
25522 end if;
25523
25524 if Is_Private_Type (U) then
25525 U := Full_View (U);
25526 end if;
25527
25528 when E_Subprogram_Body =>
25529 P := Parent (E);
25530
25531 if Nkind (P) = N_Defining_Program_Unit_Name then
25532 P := Parent (P);
25533 end if;
25534
25535 P := Parent (P);
25536
25537 if Nkind (P) = N_Subprogram_Body
25538 and then Present (Corresponding_Spec (P))
25539 then
25540 U := Corresponding_Spec (P);
25541
25542 elsif Nkind (P) = N_Subprogram_Body_Stub
25543 and then Present (Corresponding_Spec_Of_Stub (P))
25544 then
25545 U := Corresponding_Spec_Of_Stub (P);
25546
25547 elsif Nkind (P) = N_Subprogram_Renaming_Declaration then
25548 U := Corresponding_Spec (P);
25549 end if;
25550
25551 when E_Task_Body =>
25552 P := Parent (E);
25553
25554 if Nkind (P) = N_Task_Body
25555 and then Present (Corresponding_Spec (P))
25556 then
25557 U := Corresponding_Spec (P);
25558
25559 elsif Nkind (P) = N_Task_Body_Stub
25560 and then Present (Corresponding_Spec_Of_Stub (P))
25561 then
25562 U := Corresponding_Spec_Of_Stub (P);
25563
25564 if Is_Single_Task_Object (U) then
25565 U := Etype (U);
25566 end if;
25567 end if;
25568
25569 if Is_Private_Type (U) then
25570 U := Full_View (U);
25571 end if;
25572
25573 when Type_Kind =>
25574 if Present (Full_View (E)) then
25575 U := Full_View (E);
25576 end if;
25577
25578 when others =>
25579 null;
25580 end case;
25581
25582 return U;
25583 end Unique_Entity;
25584
25585 -----------------
25586 -- Unique_Name --
25587 -----------------
25588
25589 function Unique_Name (E : Entity_Id) return String is
25590
25591 -- Names in E_Subprogram_Body or E_Package_Body entities are not
25592 -- reliable, as they may not include the overloading suffix. Instead,
25593 -- when looking for the name of E or one of its enclosing scope, we get
25594 -- the name of the corresponding Unique_Entity.
25595
25596 U : constant Entity_Id := Unique_Entity (E);
25597
25598 function This_Name return String;
25599
25600 ---------------
25601 -- This_Name --
25602 ---------------
25603
25604 function This_Name return String is
25605 begin
25606 return Get_Name_String (Chars (U));
25607 end This_Name;
25608
25609 -- Start of processing for Unique_Name
25610
25611 begin
25612 if E = Standard_Standard
25613 or else Has_Fully_Qualified_Name (E)
25614 then
25615 return This_Name;
25616
25617 elsif Ekind (E) = E_Enumeration_Literal then
25618 return Unique_Name (Etype (E)) & "__" & This_Name;
25619
25620 else
25621 declare
25622 S : constant Entity_Id := Scope (U);
25623 pragma Assert (Present (S));
25624
25625 begin
25626 -- Prefix names of predefined types with standard__, but leave
25627 -- names of user-defined packages and subprograms without prefix
25628 -- (even if technically they are nested in the Standard package).
25629
25630 if S = Standard_Standard then
25631 if Ekind (U) = E_Package or else Is_Subprogram (U) then
25632 return This_Name;
25633 else
25634 return Unique_Name (S) & "__" & This_Name;
25635 end if;
25636
25637 -- For intances of generic subprograms use the name of the related
25638 -- instace and skip the scope of its wrapper package.
25639
25640 elsif Is_Wrapper_Package (S) then
25641 pragma Assert (Scope (S) = Scope (Related_Instance (S)));
25642 -- Wrapper package and the instantiation are in the same scope
25643
25644 declare
25645 Enclosing_Name : constant String :=
25646 Unique_Name (Scope (S)) & "__" &
25647 Get_Name_String (Chars (Related_Instance (S)));
25648
25649 begin
25650 if Is_Subprogram (U)
25651 and then not Is_Generic_Actual_Subprogram (U)
25652 then
25653 return Enclosing_Name;
25654 else
25655 return Enclosing_Name & "__" & This_Name;
25656 end if;
25657 end;
25658
25659 else
25660 return Unique_Name (S) & "__" & This_Name;
25661 end if;
25662 end;
25663 end if;
25664 end Unique_Name;
25665
25666 ---------------------
25667 -- Unit_Is_Visible --
25668 ---------------------
25669
25670 function Unit_Is_Visible (U : Entity_Id) return Boolean is
25671 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
25672 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
25673
25674 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
25675 -- For a child unit, check whether unit appears in a with_clause
25676 -- of a parent.
25677
25678 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
25679 -- Scan the context clause of one compilation unit looking for a
25680 -- with_clause for the unit in question.
25681
25682 ----------------------------
25683 -- Unit_In_Parent_Context --
25684 ----------------------------
25685
25686 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
25687 begin
25688 if Unit_In_Context (Par_Unit) then
25689 return True;
25690
25691 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
25692 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
25693
25694 else
25695 return False;
25696 end if;
25697 end Unit_In_Parent_Context;
25698
25699 ---------------------
25700 -- Unit_In_Context --
25701 ---------------------
25702
25703 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
25704 Clause : Node_Id;
25705
25706 begin
25707 Clause := First (Context_Items (Comp_Unit));
25708 while Present (Clause) loop
25709 if Nkind (Clause) = N_With_Clause then
25710 if Library_Unit (Clause) = U then
25711 return True;
25712
25713 -- The with_clause may denote a renaming of the unit we are
25714 -- looking for, eg. Text_IO which renames Ada.Text_IO.
25715
25716 elsif
25717 Renamed_Entity (Entity (Name (Clause))) =
25718 Defining_Entity (Unit (U))
25719 then
25720 return True;
25721 end if;
25722 end if;
25723
25724 Next (Clause);
25725 end loop;
25726
25727 return False;
25728 end Unit_In_Context;
25729
25730 -- Start of processing for Unit_Is_Visible
25731
25732 begin
25733 -- The currrent unit is directly visible
25734
25735 if Curr = U then
25736 return True;
25737
25738 elsif Unit_In_Context (Curr) then
25739 return True;
25740
25741 -- If the current unit is a body, check the context of the spec
25742
25743 elsif Nkind (Unit (Curr)) = N_Package_Body
25744 or else
25745 (Nkind (Unit (Curr)) = N_Subprogram_Body
25746 and then not Acts_As_Spec (Unit (Curr)))
25747 then
25748 if Unit_In_Context (Library_Unit (Curr)) then
25749 return True;
25750 end if;
25751 end if;
25752
25753 -- If the spec is a child unit, examine the parents
25754
25755 if Is_Child_Unit (Curr_Entity) then
25756 if Nkind (Unit (Curr)) in N_Unit_Body then
25757 return
25758 Unit_In_Parent_Context
25759 (Parent_Spec (Unit (Library_Unit (Curr))));
25760 else
25761 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
25762 end if;
25763
25764 else
25765 return False;
25766 end if;
25767 end Unit_Is_Visible;
25768
25769 ------------------------------
25770 -- Universal_Interpretation --
25771 ------------------------------
25772
25773 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
25774 Index : Interp_Index;
25775 It : Interp;
25776
25777 begin
25778 -- The argument may be a formal parameter of an operator or subprogram
25779 -- with multiple interpretations, or else an expression for an actual.
25780
25781 if Nkind (Opnd) = N_Defining_Identifier
25782 or else not Is_Overloaded (Opnd)
25783 then
25784 if Etype (Opnd) = Universal_Integer
25785 or else Etype (Opnd) = Universal_Real
25786 then
25787 return Etype (Opnd);
25788 else
25789 return Empty;
25790 end if;
25791
25792 else
25793 Get_First_Interp (Opnd, Index, It);
25794 while Present (It.Typ) loop
25795 if It.Typ = Universal_Integer
25796 or else It.Typ = Universal_Real
25797 then
25798 return It.Typ;
25799 end if;
25800
25801 Get_Next_Interp (Index, It);
25802 end loop;
25803
25804 return Empty;
25805 end if;
25806 end Universal_Interpretation;
25807
25808 ---------------
25809 -- Unqualify --
25810 ---------------
25811
25812 function Unqualify (Expr : Node_Id) return Node_Id is
25813 begin
25814 -- Recurse to handle unlikely case of multiple levels of qualification
25815
25816 if Nkind (Expr) = N_Qualified_Expression then
25817 return Unqualify (Expression (Expr));
25818
25819 -- Normal case, not a qualified expression
25820
25821 else
25822 return Expr;
25823 end if;
25824 end Unqualify;
25825
25826 -----------------
25827 -- Unqual_Conv --
25828 -----------------
25829
25830 function Unqual_Conv (Expr : Node_Id) return Node_Id is
25831 begin
25832 -- Recurse to handle unlikely case of multiple levels of qualification
25833 -- and/or conversion.
25834
25835 if Nkind_In (Expr, N_Qualified_Expression,
25836 N_Type_Conversion,
25837 N_Unchecked_Type_Conversion)
25838 then
25839 return Unqual_Conv (Expression (Expr));
25840
25841 -- Normal case, not a qualified expression
25842
25843 else
25844 return Expr;
25845 end if;
25846 end Unqual_Conv;
25847
25848 --------------------
25849 -- Validated_View --
25850 --------------------
25851
25852 function Validated_View (Typ : Entity_Id) return Entity_Id is
25853 Continue : Boolean;
25854 Val_Typ : Entity_Id;
25855
25856 begin
25857 Continue := True;
25858 Val_Typ := Base_Type (Typ);
25859
25860 -- Obtain the full view of the input type by stripping away concurrency,
25861 -- derivations, and privacy.
25862
25863 while Continue loop
25864 Continue := False;
25865
25866 if Is_Concurrent_Type (Val_Typ) then
25867 if Present (Corresponding_Record_Type (Val_Typ)) then
25868 Continue := True;
25869 Val_Typ := Corresponding_Record_Type (Val_Typ);
25870 end if;
25871
25872 elsif Is_Derived_Type (Val_Typ) then
25873 Continue := True;
25874 Val_Typ := Etype (Val_Typ);
25875
25876 elsif Is_Private_Type (Val_Typ) then
25877 if Present (Underlying_Full_View (Val_Typ)) then
25878 Continue := True;
25879 Val_Typ := Underlying_Full_View (Val_Typ);
25880
25881 elsif Present (Full_View (Val_Typ)) then
25882 Continue := True;
25883 Val_Typ := Full_View (Val_Typ);
25884 end if;
25885 end if;
25886 end loop;
25887
25888 return Val_Typ;
25889 end Validated_View;
25890
25891 -----------------------
25892 -- Visible_Ancestors --
25893 -----------------------
25894
25895 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
25896 List_1 : Elist_Id;
25897 List_2 : Elist_Id;
25898 Elmt : Elmt_Id;
25899
25900 begin
25901 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
25902
25903 -- Collect all the parents and progenitors of Typ. If the full-view of
25904 -- private parents and progenitors is available then it is used to
25905 -- generate the list of visible ancestors; otherwise their partial
25906 -- view is added to the resulting list.
25907
25908 Collect_Parents
25909 (T => Typ,
25910 List => List_1,
25911 Use_Full_View => True);
25912
25913 Collect_Interfaces
25914 (T => Typ,
25915 Ifaces_List => List_2,
25916 Exclude_Parents => True,
25917 Use_Full_View => True);
25918
25919 -- Join the two lists. Avoid duplications because an interface may
25920 -- simultaneously be parent and progenitor of a type.
25921
25922 Elmt := First_Elmt (List_2);
25923 while Present (Elmt) loop
25924 Append_Unique_Elmt (Node (Elmt), List_1);
25925 Next_Elmt (Elmt);
25926 end loop;
25927
25928 return List_1;
25929 end Visible_Ancestors;
25930
25931 ----------------------
25932 -- Within_Init_Proc --
25933 ----------------------
25934
25935 function Within_Init_Proc return Boolean is
25936 S : Entity_Id;
25937
25938 begin
25939 S := Current_Scope;
25940 while not Is_Overloadable (S) loop
25941 if S = Standard_Standard then
25942 return False;
25943 else
25944 S := Scope (S);
25945 end if;
25946 end loop;
25947
25948 return Is_Init_Proc (S);
25949 end Within_Init_Proc;
25950
25951 ---------------------------
25952 -- Within_Protected_Type --
25953 ---------------------------
25954
25955 function Within_Protected_Type (E : Entity_Id) return Boolean is
25956 Scop : Entity_Id := Scope (E);
25957
25958 begin
25959 while Present (Scop) loop
25960 if Ekind (Scop) = E_Protected_Type then
25961 return True;
25962 end if;
25963
25964 Scop := Scope (Scop);
25965 end loop;
25966
25967 return False;
25968 end Within_Protected_Type;
25969
25970 ------------------
25971 -- Within_Scope --
25972 ------------------
25973
25974 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
25975 begin
25976 return Scope_Within_Or_Same (Scope (E), S);
25977 end Within_Scope;
25978
25979 ----------------------------
25980 -- Within_Subprogram_Call --
25981 ----------------------------
25982
25983 function Within_Subprogram_Call (N : Node_Id) return Boolean is
25984 Par : Node_Id;
25985
25986 begin
25987 -- Climb the parent chain looking for a function or procedure call
25988
25989 Par := N;
25990 while Present (Par) loop
25991 if Nkind_In (Par, N_Entry_Call_Statement,
25992 N_Function_Call,
25993 N_Procedure_Call_Statement)
25994 then
25995 return True;
25996
25997 -- Prevent the search from going too far
25998
25999 elsif Is_Body_Or_Package_Declaration (Par) then
26000 exit;
26001 end if;
26002
26003 Par := Parent (Par);
26004 end loop;
26005
26006 return False;
26007 end Within_Subprogram_Call;
26008
26009 ----------------
26010 -- Wrong_Type --
26011 ----------------
26012
26013 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
26014 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
26015 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
26016
26017 Matching_Field : Entity_Id;
26018 -- Entity to give a more precise suggestion on how to write a one-
26019 -- element positional aggregate.
26020
26021 function Has_One_Matching_Field return Boolean;
26022 -- Determines if Expec_Type is a record type with a single component or
26023 -- discriminant whose type matches the found type or is one dimensional
26024 -- array whose component type matches the found type. In the case of
26025 -- one discriminant, we ignore the variant parts. That's not accurate,
26026 -- but good enough for the warning.
26027
26028 ----------------------------
26029 -- Has_One_Matching_Field --
26030 ----------------------------
26031
26032 function Has_One_Matching_Field return Boolean is
26033 E : Entity_Id;
26034
26035 begin
26036 Matching_Field := Empty;
26037
26038 if Is_Array_Type (Expec_Type)
26039 and then Number_Dimensions (Expec_Type) = 1
26040 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
26041 then
26042 -- Use type name if available. This excludes multidimensional
26043 -- arrays and anonymous arrays.
26044
26045 if Comes_From_Source (Expec_Type) then
26046 Matching_Field := Expec_Type;
26047
26048 -- For an assignment, use name of target
26049
26050 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
26051 and then Is_Entity_Name (Name (Parent (Expr)))
26052 then
26053 Matching_Field := Entity (Name (Parent (Expr)));
26054 end if;
26055
26056 return True;
26057
26058 elsif not Is_Record_Type (Expec_Type) then
26059 return False;
26060
26061 else
26062 E := First_Entity (Expec_Type);
26063 loop
26064 if No (E) then
26065 return False;
26066
26067 elsif not Ekind_In (E, E_Discriminant, E_Component)
26068 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
26069 then
26070 Next_Entity (E);
26071
26072 else
26073 exit;
26074 end if;
26075 end loop;
26076
26077 if not Covers (Etype (E), Found_Type) then
26078 return False;
26079
26080 elsif Present (Next_Entity (E))
26081 and then (Ekind (E) = E_Component
26082 or else Ekind (Next_Entity (E)) = E_Discriminant)
26083 then
26084 return False;
26085
26086 else
26087 Matching_Field := E;
26088 return True;
26089 end if;
26090 end if;
26091 end Has_One_Matching_Field;
26092
26093 -- Start of processing for Wrong_Type
26094
26095 begin
26096 -- Don't output message if either type is Any_Type, or if a message
26097 -- has already been posted for this node. We need to do the latter
26098 -- check explicitly (it is ordinarily done in Errout), because we
26099 -- are using ! to force the output of the error messages.
26100
26101 if Expec_Type = Any_Type
26102 or else Found_Type = Any_Type
26103 or else Error_Posted (Expr)
26104 then
26105 return;
26106
26107 -- If one of the types is a Taft-Amendment type and the other it its
26108 -- completion, it must be an illegal use of a TAT in the spec, for
26109 -- which an error was already emitted. Avoid cascaded errors.
26110
26111 elsif Is_Incomplete_Type (Expec_Type)
26112 and then Has_Completion_In_Body (Expec_Type)
26113 and then Full_View (Expec_Type) = Etype (Expr)
26114 then
26115 return;
26116
26117 elsif Is_Incomplete_Type (Etype (Expr))
26118 and then Has_Completion_In_Body (Etype (Expr))
26119 and then Full_View (Etype (Expr)) = Expec_Type
26120 then
26121 return;
26122
26123 -- In an instance, there is an ongoing problem with completion of
26124 -- type derived from private types. Their structure is what Gigi
26125 -- expects, but the Etype is the parent type rather than the
26126 -- derived private type itself. Do not flag error in this case. The
26127 -- private completion is an entity without a parent, like an Itype.
26128 -- Similarly, full and partial views may be incorrect in the instance.
26129 -- There is no simple way to insure that it is consistent ???
26130
26131 -- A similar view discrepancy can happen in an inlined body, for the
26132 -- same reason: inserted body may be outside of the original package
26133 -- and only partial views are visible at the point of insertion.
26134
26135 elsif In_Instance or else In_Inlined_Body then
26136 if Etype (Etype (Expr)) = Etype (Expected_Type)
26137 and then
26138 (Has_Private_Declaration (Expected_Type)
26139 or else Has_Private_Declaration (Etype (Expr)))
26140 and then No (Parent (Expected_Type))
26141 then
26142 return;
26143
26144 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
26145 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
26146 then
26147 return;
26148
26149 elsif Is_Private_Type (Expected_Type)
26150 and then Present (Full_View (Expected_Type))
26151 and then Covers (Full_View (Expected_Type), Etype (Expr))
26152 then
26153 return;
26154
26155 -- Conversely, type of expression may be the private one
26156
26157 elsif Is_Private_Type (Base_Type (Etype (Expr)))
26158 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
26159 then
26160 return;
26161 end if;
26162 end if;
26163
26164 -- An interesting special check. If the expression is parenthesized
26165 -- and its type corresponds to the type of the sole component of the
26166 -- expected record type, or to the component type of the expected one
26167 -- dimensional array type, then assume we have a bad aggregate attempt.
26168
26169 if Nkind (Expr) in N_Subexpr
26170 and then Paren_Count (Expr) /= 0
26171 and then Has_One_Matching_Field
26172 then
26173 Error_Msg_N ("positional aggregate cannot have one component", Expr);
26174
26175 if Present (Matching_Field) then
26176 if Is_Array_Type (Expec_Type) then
26177 Error_Msg_NE
26178 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
26179 else
26180 Error_Msg_NE
26181 ("\write instead `& ='> ...`", Expr, Matching_Field);
26182 end if;
26183 end if;
26184
26185 -- Another special check, if we are looking for a pool-specific access
26186 -- type and we found an E_Access_Attribute_Type, then we have the case
26187 -- of an Access attribute being used in a context which needs a pool-
26188 -- specific type, which is never allowed. The one extra check we make
26189 -- is that the expected designated type covers the Found_Type.
26190
26191 elsif Is_Access_Type (Expec_Type)
26192 and then Ekind (Found_Type) = E_Access_Attribute_Type
26193 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
26194 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
26195 and then Covers
26196 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
26197 then
26198 Error_Msg_N -- CODEFIX
26199 ("result must be general access type!", Expr);
26200 Error_Msg_NE -- CODEFIX
26201 ("add ALL to }!", Expr, Expec_Type);
26202
26203 -- Another special check, if the expected type is an integer type,
26204 -- but the expression is of type System.Address, and the parent is
26205 -- an addition or subtraction operation whose left operand is the
26206 -- expression in question and whose right operand is of an integral
26207 -- type, then this is an attempt at address arithmetic, so give
26208 -- appropriate message.
26209
26210 elsif Is_Integer_Type (Expec_Type)
26211 and then Is_RTE (Found_Type, RE_Address)
26212 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
26213 and then Expr = Left_Opnd (Parent (Expr))
26214 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
26215 then
26216 Error_Msg_N
26217 ("address arithmetic not predefined in package System",
26218 Parent (Expr));
26219 Error_Msg_N
26220 ("\possible missing with/use of System.Storage_Elements",
26221 Parent (Expr));
26222 return;
26223
26224 -- If the expected type is an anonymous access type, as for access
26225 -- parameters and discriminants, the error is on the designated types.
26226
26227 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
26228 if Comes_From_Source (Expec_Type) then
26229 Error_Msg_NE ("expected}!", Expr, Expec_Type);
26230 else
26231 Error_Msg_NE
26232 ("expected an access type with designated}",
26233 Expr, Designated_Type (Expec_Type));
26234 end if;
26235
26236 if Is_Access_Type (Found_Type)
26237 and then not Comes_From_Source (Found_Type)
26238 then
26239 Error_Msg_NE
26240 ("\\found an access type with designated}!",
26241 Expr, Designated_Type (Found_Type));
26242 else
26243 if From_Limited_With (Found_Type) then
26244 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
26245 Error_Msg_Qual_Level := 99;
26246 Error_Msg_NE -- CODEFIX
26247 ("\\missing `WITH &;", Expr, Scope (Found_Type));
26248 Error_Msg_Qual_Level := 0;
26249 else
26250 Error_Msg_NE ("found}!", Expr, Found_Type);
26251 end if;
26252 end if;
26253
26254 -- Normal case of one type found, some other type expected
26255
26256 else
26257 -- If the names of the two types are the same, see if some number
26258 -- of levels of qualification will help. Don't try more than three
26259 -- levels, and if we get to standard, it's no use (and probably
26260 -- represents an error in the compiler) Also do not bother with
26261 -- internal scope names.
26262
26263 declare
26264 Expec_Scope : Entity_Id;
26265 Found_Scope : Entity_Id;
26266
26267 begin
26268 Expec_Scope := Expec_Type;
26269 Found_Scope := Found_Type;
26270
26271 for Levels in Nat range 0 .. 3 loop
26272 if Chars (Expec_Scope) /= Chars (Found_Scope) then
26273 Error_Msg_Qual_Level := Levels;
26274 exit;
26275 end if;
26276
26277 Expec_Scope := Scope (Expec_Scope);
26278 Found_Scope := Scope (Found_Scope);
26279
26280 exit when Expec_Scope = Standard_Standard
26281 or else Found_Scope = Standard_Standard
26282 or else not Comes_From_Source (Expec_Scope)
26283 or else not Comes_From_Source (Found_Scope);
26284 end loop;
26285 end;
26286
26287 if Is_Record_Type (Expec_Type)
26288 and then Present (Corresponding_Remote_Type (Expec_Type))
26289 then
26290 Error_Msg_NE ("expected}!", Expr,
26291 Corresponding_Remote_Type (Expec_Type));
26292 else
26293 Error_Msg_NE ("expected}!", Expr, Expec_Type);
26294 end if;
26295
26296 if Is_Entity_Name (Expr)
26297 and then Is_Package_Or_Generic_Package (Entity (Expr))
26298 then
26299 Error_Msg_N ("\\found package name!", Expr);
26300
26301 elsif Is_Entity_Name (Expr)
26302 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
26303 then
26304 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
26305 Error_Msg_N
26306 ("found procedure name, possibly missing Access attribute!",
26307 Expr);
26308 else
26309 Error_Msg_N
26310 ("\\found procedure name instead of function!", Expr);
26311 end if;
26312
26313 elsif Nkind (Expr) = N_Function_Call
26314 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
26315 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
26316 and then No (Parameter_Associations (Expr))
26317 then
26318 Error_Msg_N
26319 ("found function name, possibly missing Access attribute!",
26320 Expr);
26321
26322 -- Catch common error: a prefix or infix operator which is not
26323 -- directly visible because the type isn't.
26324
26325 elsif Nkind (Expr) in N_Op
26326 and then Is_Overloaded (Expr)
26327 and then not Is_Immediately_Visible (Expec_Type)
26328 and then not Is_Potentially_Use_Visible (Expec_Type)
26329 and then not In_Use (Expec_Type)
26330 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
26331 then
26332 Error_Msg_N
26333 ("operator of the type is not directly visible!", Expr);
26334
26335 elsif Ekind (Found_Type) = E_Void
26336 and then Present (Parent (Found_Type))
26337 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
26338 then
26339 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
26340
26341 else
26342 Error_Msg_NE ("\\found}!", Expr, Found_Type);
26343 end if;
26344
26345 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
26346 -- of the same modular type, and (M1 and M2) = 0 was intended.
26347
26348 if Expec_Type = Standard_Boolean
26349 and then Is_Modular_Integer_Type (Found_Type)
26350 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
26351 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
26352 then
26353 declare
26354 Op : constant Node_Id := Right_Opnd (Parent (Expr));
26355 L : constant Node_Id := Left_Opnd (Op);
26356 R : constant Node_Id := Right_Opnd (Op);
26357
26358 begin
26359 -- The case for the message is when the left operand of the
26360 -- comparison is the same modular type, or when it is an
26361 -- integer literal (or other universal integer expression),
26362 -- which would have been typed as the modular type if the
26363 -- parens had been there.
26364
26365 if (Etype (L) = Found_Type
26366 or else
26367 Etype (L) = Universal_Integer)
26368 and then Is_Integer_Type (Etype (R))
26369 then
26370 Error_Msg_N
26371 ("\\possible missing parens for modular operation", Expr);
26372 end if;
26373 end;
26374 end if;
26375
26376 -- Reset error message qualification indication
26377
26378 Error_Msg_Qual_Level := 0;
26379 end if;
26380 end Wrong_Type;
26381
26382 --------------------------------
26383 -- Yields_Synchronized_Object --
26384 --------------------------------
26385
26386 function Yields_Synchronized_Object (Typ : Entity_Id) return Boolean is
26387 Has_Sync_Comp : Boolean := False;
26388 Id : Entity_Id;
26389
26390 begin
26391 -- An array type yields a synchronized object if its component type
26392 -- yields a synchronized object.
26393
26394 if Is_Array_Type (Typ) then
26395 return Yields_Synchronized_Object (Component_Type (Typ));
26396
26397 -- A descendant of type Ada.Synchronous_Task_Control.Suspension_Object
26398 -- yields a synchronized object by default.
26399
26400 elsif Is_Descendant_Of_Suspension_Object (Typ) then
26401 return True;
26402
26403 -- A protected type yields a synchronized object by default
26404
26405 elsif Is_Protected_Type (Typ) then
26406 return True;
26407
26408 -- A record type or type extension yields a synchronized object when its
26409 -- discriminants (if any) lack default values and all components are of
26410 -- a type that yields a synchronized object.
26411
26412 elsif Is_Record_Type (Typ) then
26413
26414 -- Inspect all entities defined in the scope of the type, looking for
26415 -- components of a type that does not yield a synchronized object or
26416 -- for discriminants with default values.
26417
26418 Id := First_Entity (Typ);
26419 while Present (Id) loop
26420 if Comes_From_Source (Id) then
26421 if Ekind (Id) = E_Component then
26422 if Yields_Synchronized_Object (Etype (Id)) then
26423 Has_Sync_Comp := True;
26424
26425 -- The component does not yield a synchronized object
26426
26427 else
26428 return False;
26429 end if;
26430
26431 elsif Ekind (Id) = E_Discriminant
26432 and then Present (Expression (Parent (Id)))
26433 then
26434 return False;
26435 end if;
26436 end if;
26437
26438 Next_Entity (Id);
26439 end loop;
26440
26441 -- Ensure that the parent type of a type extension yields a
26442 -- synchronized object.
26443
26444 if Etype (Typ) /= Typ
26445 and then not Is_Private_Type (Etype (Typ))
26446 and then not Yields_Synchronized_Object (Etype (Typ))
26447 then
26448 return False;
26449 end if;
26450
26451 -- If we get here, then all discriminants lack default values and all
26452 -- components are of a type that yields a synchronized object.
26453
26454 return Has_Sync_Comp;
26455
26456 -- A synchronized interface type yields a synchronized object by default
26457
26458 elsif Is_Synchronized_Interface (Typ) then
26459 return True;
26460
26461 -- A task type yields a synchronized object by default
26462
26463 elsif Is_Task_Type (Typ) then
26464 return True;
26465
26466 -- A private type yields a synchronized object if its underlying type
26467 -- does.
26468
26469 elsif Is_Private_Type (Typ)
26470 and then Present (Underlying_Type (Typ))
26471 then
26472 return Yields_Synchronized_Object (Underlying_Type (Typ));
26473
26474 -- Otherwise the type does not yield a synchronized object
26475
26476 else
26477 return False;
26478 end if;
26479 end Yields_Synchronized_Object;
26480
26481 ---------------------------
26482 -- Yields_Universal_Type --
26483 ---------------------------
26484
26485 function Yields_Universal_Type (N : Node_Id) return Boolean is
26486 begin
26487 -- Integer and real literals are of a universal type
26488
26489 if Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
26490 return True;
26491
26492 -- The values of certain attributes are of a universal type
26493
26494 elsif Nkind (N) = N_Attribute_Reference then
26495 return
26496 Universal_Type_Attribute (Get_Attribute_Id (Attribute_Name (N)));
26497
26498 -- ??? There are possibly other cases to consider
26499
26500 else
26501 return False;
26502 end if;
26503 end Yields_Universal_Type;
26504
26505 begin
26506 Erroutc.Subprogram_Name_Ptr := Subprogram_Name'Access;
26507 end Sem_Util;