[Ada] Update FE check following change in SPARK RM 7.1.3(12)
[official-gcc.git] / gcc / ada / sem_util.adb
blob4cef1baea7d63a425ebcb8a3dcf9a2bfb0802d99
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-2018, 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 ------------------------------------------------------------------------------
26 with Treepr; -- ???For debugging code below
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;
71 with GNAT.HTable; use GNAT.HTable;
73 package body Sem_Util is
75 ---------------------------
76 -- Local Data Structures --
77 ---------------------------
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.
83 Invalid_Binder_Values_Set : Boolean := False;
84 -- This flag prevents multiple attempts to initialize Invalid_Binder_Values
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.
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.
94 -----------------------
95 -- Local Subprograms --
96 -----------------------
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.
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.
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.
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.
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.
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.
136 Is_Non_Null,
137 -- This value indicates that a subexpression is known to have a non-null
138 -- value at compile time.
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.
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.
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.
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.
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.
174 ------------------------------
175 -- Abstract_Interface_List --
176 ------------------------------
178 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
179 Nod : Node_Id;
181 begin
182 if Is_Concurrent_Type (Typ) then
184 -- If we are dealing with a synchronized subtype, go to the base
185 -- type, whose declaration has the interface list.
187 Nod := Declaration_Node (Base_Type (Typ));
189 if Nkind_In (Nod, N_Full_Type_Declaration,
190 N_Private_Type_Declaration)
191 then
192 return Empty_List;
193 end if;
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));
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)));
206 -- If the full-view is not available we cannot do anything else
207 -- here (the source has errors).
209 else
210 return Empty_List;
211 end if;
213 -- Support for generic formals with interfaces is still missing ???
215 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
216 return Empty_List;
218 else
219 pragma Assert
220 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
221 Nod := Parent (Typ);
222 end if;
224 elsif Ekind (Typ) = E_Record_Subtype then
225 Nod := Type_Definition (Parent (Etype (Typ)));
227 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
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.
233 return Abstract_Interface_List (Etype (Typ));
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;
242 -- Otherwise the type is of a kind which does not implement interfaces
244 else
245 return Empty_List;
246 end if;
248 return Interface_List (Nod);
249 end Abstract_Interface_List;
251 --------------------------------
252 -- Add_Access_Type_To_Process --
253 --------------------------------
255 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
256 L : Elist_Id;
258 begin
259 Ensure_Freeze_Node (E);
260 L := Access_Types_To_Process (Freeze_Node (E));
262 if No (L) then
263 L := New_Elmt_List;
264 Set_Access_Types_To_Process (Freeze_Node (E), L);
265 end if;
267 Append_Elmt (A, L);
268 end Add_Access_Type_To_Process;
270 --------------------------
271 -- Add_Block_Identifier --
272 --------------------------
274 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
275 Loc : constant Source_Ptr := Sloc (N);
277 begin
278 pragma Assert (Nkind (N) = N_Block_Statement);
280 -- The block already has a label, return its entity
282 if Present (Identifier (N)) then
283 Id := Entity (Identifier (N));
285 -- Create a new block label and set its attributes
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);
292 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
293 Set_Block_Node (Id, Identifier (N));
294 end if;
295 end Add_Block_Identifier;
297 ----------------------------
298 -- Add_Global_Declaration --
299 ----------------------------
301 procedure Add_Global_Declaration (N : Node_Id) is
302 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
304 begin
305 if No (Declarations (Aux_Node)) then
306 Set_Declarations (Aux_Node, New_List);
307 end if;
309 Append_To (Declarations (Aux_Node), N);
310 Analyze (N);
311 end Add_Global_Declaration;
313 --------------------------------
314 -- Address_Integer_Convert_OK --
315 --------------------------------
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;
334 -------------------
335 -- Address_Value --
336 -------------------
338 function Address_Value (N : Node_Id) return Node_Id is
339 Expr : Node_Id := N;
341 begin
342 loop
343 -- For constant, get constant expression
345 if Is_Entity_Name (Expr)
346 and then Ekind (Entity (Expr)) = E_Constant
347 then
348 Expr := Constant_Value (Entity (Expr));
350 -- For unchecked conversion, get result to convert
352 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
353 Expr := Expression (Expr);
355 -- For (common case) of To_Address call, get argument
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));
363 if Nkind (Expr) = N_Parameter_Association then
364 Expr := Explicit_Actual_Parameter (Expr);
365 end if;
367 -- We finally have the real expression
369 else
370 exit;
371 end if;
372 end loop;
374 return Expr;
375 end Address_Value;
377 -----------------
378 -- Addressable --
379 -----------------
381 -- For now, just 8/16/32/64
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;
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;
399 ---------------------------------
400 -- Aggregate_Constraint_Checks --
401 ---------------------------------
403 procedure Aggregate_Constraint_Checks
404 (Exp : Node_Id;
405 Check_Typ : Entity_Id)
407 Exp_Typ : constant Entity_Id := Etype (Exp);
409 begin
410 if Raises_Constraint_Error (Exp) then
411 return;
412 end if;
414 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
415 -- component's type to force the appropriate accessibility checks.
417 -- Ada 2005 (AI-231): Generate conversion to the null-excluding type to
418 -- force the corresponding run-time check
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;
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.
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;
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;
449 -- First check if we have to insert discriminant checks
451 if Has_Discriminants (Exp_Typ) then
452 Apply_Discriminant_Check (Exp, Check_Typ);
454 -- Next emit length checks for array aggregates
456 elsif Is_Array_Type (Exp_Typ) then
457 Apply_Length_Check (Exp, Check_Typ);
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.
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));
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);
478 elsif not Range_Checks_Suppressed (Check_Typ) then
479 Apply_Scalar_Range_Check (Exp, Check_Typ);
480 end if;
482 -- Verify that target type is also scalar, to prevent view anomalies
483 -- in instantiations.
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.
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;
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;
510 -- Could use a comment on this case ???
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;
518 end if;
519 end Aggregate_Constraint_Checks;
521 -----------------------
522 -- Alignment_In_Bits --
523 -----------------------
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;
530 --------------------------------------
531 -- All_Composite_Constraints_Static --
532 --------------------------------------
534 function All_Composite_Constraints_Static
535 (Constr : Node_Id) return Boolean
537 begin
538 if No (Constr) or else Error_Posted (Constr) then
539 return True;
540 end if;
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;
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));
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;
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);
575 when N_Discriminant_Association =>
576 return All_Composite_Constraints_Static (Expression (Constr));
578 when N_Range_Constraint =>
579 return
580 All_Composite_Constraints_Static (Range_Expression (Constr));
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;
592 Next (One_Cstr);
593 end loop;
594 end;
596 return True;
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));
604 when others =>
605 raise Program_Error;
606 end case;
607 end All_Composite_Constraints_Static;
609 ------------------------
610 -- Append_Entity_Name --
611 ------------------------
613 procedure Append_Entity_Name (Buf : in out Bounded_String; E : Entity_Id) is
614 Temp : Bounded_String;
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.
620 -----------
621 -- Inner --
622 -----------
624 procedure Inner (E : Entity_Id) is
625 Scop : Node_Id;
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.
632 declare
633 E_Name : Bounded_String;
635 begin
636 Append (E_Name, Chars (E));
638 if E_Name.Chars (E_Name.Length) = 'R' then
639 E_Name.Length := E_Name.Length - 1;
640 end if;
642 if Is_Internal_Name (E_Name) then
643 Inner (Scope (E));
644 return;
645 end if;
646 end;
648 Scop := Scope (E);
650 -- Just print entity name if its scope is at the outer level
652 if Scop = Standard_Standard then
653 null;
655 -- If scope comes from source, write scope and entity
657 elsif Comes_From_Source (Scop) then
658 Append_Entity_Name (Temp, Scop);
659 Append (Temp, '.');
661 -- If in wrapper package skip past it
663 elsif Present (Scop) and then Is_Wrapper_Package (Scop) then
664 Append_Entity_Name (Temp, Scope (Scop));
665 Append (Temp, '.');
667 -- Otherwise nothing to output (happens in unnamed block statements)
669 else
670 null;
671 end if;
673 -- Output the name
675 declare
676 E_Name : Bounded_String;
678 begin
679 Append_Unqualified_Decoded (E_Name, Chars (E));
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).
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;
691 -- Adjust casing appropriately (gets name from source if possible)
693 Adjust_Name_Case (E_Name, Sloc (E));
694 Append (Temp, E_Name);
695 end;
696 end Inner;
698 -- Start of processing for Append_Entity_Name
700 begin
701 Inner (E);
702 Append (Buf, Temp);
703 end Append_Entity_Name;
705 ---------------------------------
706 -- Append_Inherited_Subprogram --
707 ---------------------------------
709 procedure Append_Inherited_Subprogram (S : Entity_Id) is
710 Par : constant Entity_Id := Alias (S);
711 -- The parent subprogram
713 Scop : constant Entity_Id := Scope (Par);
714 -- The scope of definition of the parent subprogram
716 Typ : constant Entity_Id := Defining_Entity (Parent (S));
717 -- The derived type of which S is a primitive operation
719 Decl : Node_Id;
720 Next_E : Entity_Id;
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.
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;
747 else
748 exit;
749 end if;
750 end if;
752 Next (Decl);
753 end loop;
754 end if;
756 -- If partial view is not a type extension, or it appears before the
757 -- subprogram declaration, insert normally at end of entity list.
759 Append_Entity (S, Current_Scope);
760 end Append_Inherited_Subprogram;
762 -----------------------------------------
763 -- Apply_Compile_Time_Constraint_Error --
764 -----------------------------------------
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)
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;
781 begin
782 if No (Typ) then
783 Rtyp := Etype (N);
784 else
785 Rtyp := Typ;
786 end if;
788 Discard_Node
789 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
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.
797 if not Rep or GNATprove_Mode then
798 return;
799 end if;
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.
804 Rewrite (N, R_Stat);
805 Set_Analyzed (N, True);
807 Set_Etype (N, Rtyp);
808 Set_Raises_Constraint_Error (N);
810 -- Now deal with possible local raise handling
812 Possible_Local_Raise (N, Standard_Constraint_Error);
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.
818 if Stat then
819 Set_Is_Static_Expression (N);
820 end if;
821 end Apply_Compile_Time_Constraint_Error;
823 ---------------------------
824 -- Async_Readers_Enabled --
825 ---------------------------
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;
832 ---------------------------
833 -- Async_Writers_Enabled --
834 ---------------------------
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;
841 --------------------------------------
842 -- Available_Full_View_Of_Component --
843 --------------------------------------
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;
854 -------------------
855 -- Bad_Attribute --
856 -------------------
858 procedure Bad_Attribute
859 (N : Node_Id;
860 Nam : Name_Id;
861 Warn : Boolean := False)
863 begin
864 Error_Msg_Warn := Warn;
865 Error_Msg_N ("unrecognized attribute&<<", N);
867 -- Check for possible misspelling
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;
877 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
878 end loop;
879 end Bad_Attribute;
881 --------------------------------
882 -- Bad_Predicated_Subtype_Use --
883 --------------------------------
885 procedure Bad_Predicated_Subtype_Use
886 (Msg : String;
887 N : Node_Id;
888 Typ : Entity_Id;
889 Suggest_Static : Boolean := False)
891 Gen : Entity_Id;
893 begin
894 -- Avoid cascaded errors
896 if Error_Posted (N) then
897 return;
898 end if;
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;
906 if No (Gen) then
907 return;
908 end if;
910 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
911 Set_No_Predicate_On_Actual (Typ);
912 end if;
914 elsif Has_Predicates (Typ) then
915 if Is_Generic_Actual_Type (Typ) then
917 -- The restriction on loop parameters is only that the type
918 -- should have no dynamic predicates.
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;
927 Gen := Current_Scope;
928 while not Is_Generic_Instance (Gen) loop
929 Gen := Scope (Gen);
930 end loop;
932 pragma Assert (Present (Gen));
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);
939 Insert_Action (N,
940 Make_Raise_Program_Error (Sloc (N),
941 Reason => PE_Bad_Predicated_Generic_Type));
943 else
944 Error_Msg_FE (Msg & "<<", N, Typ);
945 end if;
947 else
948 Error_Msg_FE (Msg, N, Typ);
949 end if;
951 -- Emit an optional suggestion on how to remedy the error if the
952 -- context warrants it.
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;
960 -----------------------------------------
961 -- Bad_Unordered_Enumeration_Reference --
962 -----------------------------------------
964 function Bad_Unordered_Enumeration_Reference
965 (N : Node_Id;
966 T : Entity_Id) return Boolean
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;
977 ----------------------------
978 -- Begin_Keyword_Location --
979 ----------------------------
981 function Begin_Keyword_Location (N : Node_Id) return Source_Ptr is
982 HSS : Node_Id;
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));
991 HSS := Handled_Statement_Sequence (N);
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.
997 if Comes_From_Source (HSS) then
998 return Sloc (HSS);
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.
1005 elsif Comes_From_Source (N) then
1006 return Sloc (HSS);
1008 -- Otherwise the construct is internal and should carry the location of
1009 -- the original construct which prompted its creation.
1011 else
1012 return Sloc (N);
1013 end if;
1014 end Begin_Keyword_Location;
1016 --------------------------
1017 -- Build_Actual_Subtype --
1018 --------------------------
1020 function Build_Actual_Subtype
1021 (T : Entity_Id;
1022 N : Node_Or_Entity_Id) return Node_Id
1024 Loc : Source_Ptr;
1025 -- Normally Sloc (N), but may point to corresponding body in some cases
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;
1036 begin
1037 Loc := Sloc (N);
1039 if Nkind (N) = N_Defining_Identifier then
1040 Obj := New_Occurrence_Of (N, Loc);
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.
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;
1059 else
1060 Obj := N;
1061 end if;
1063 if Is_Array_Type (T) then
1064 Constraints := New_List;
1065 for J in 1 .. Number_Dimensions (T) loop
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.
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)));
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)));
1088 Append (Make_Range (Loc, Lo, Hi), Constraints);
1089 end loop;
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 ???
1095 elsif Has_Unknown_Discriminants (T) then
1096 return T;
1098 else
1099 Constraints := New_List;
1101 -- Type T is a generic derived type, inherit the discriminants from
1102 -- the parent type.
1104 if Is_Private_Type (T)
1105 and then No (Full_View (T))
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.
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;
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;
1130 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
1131 Set_Is_Internal (Subt);
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)));
1143 Mark_Rewrite_Insertion (Decl);
1144 return Decl;
1145 end Build_Actual_Subtype;
1147 ---------------------------------------
1148 -- Build_Actual_Subtype_Of_Component --
1149 ---------------------------------------
1151 function Build_Actual_Subtype_Of_Component
1152 (T : Entity_Id;
1153 N : Node_Id) return Node_Id
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;
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.
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.
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.
1174 -----------------------------------
1175 -- Build_Actual_Array_Constraint --
1176 -----------------------------------
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;
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));
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));
1198 else
1199 Lo := New_Copy_Tree (Old_Lo);
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.
1207 Set_Analyzed (Lo, False);
1208 end if;
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));
1216 else
1217 Hi := New_Copy_Tree (Old_Hi);
1218 Set_Analyzed (Hi, False);
1219 end if;
1221 Append (Make_Range (Loc, Lo, Hi), Constraints);
1222 Next_Index (Indx);
1223 end loop;
1225 return Constraints;
1226 end Build_Actual_Array_Constraint;
1228 ------------------------------------
1229 -- Build_Actual_Record_Constraint --
1230 ------------------------------------
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;
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));
1245 else
1246 D_Val := New_Copy_Tree (Node (D));
1247 end if;
1249 Append (D_Val, Constraints);
1250 Next_Elmt (D);
1251 end loop;
1253 return Constraints;
1254 end Build_Actual_Record_Constraint;
1256 -- Start of processing for Build_Actual_Subtype_Of_Component
1258 begin
1259 -- Why the test for Spec_Expression mode here???
1261 if In_Spec_Expression then
1262 return Empty;
1264 -- More comments for the rest of this body would be good ???
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.
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;
1289 if Ekind (T) = E_Access_Subtype then
1290 Desig_Typ := Designated_Type (T);
1291 else
1292 Desig_Typ := T;
1293 end if;
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));
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;
1310 Next_Index (Id);
1311 end loop;
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;
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;
1332 Next_Elmt (D);
1333 end loop;
1334 end if;
1336 -- If none of the above, the actual and nominal subtypes are the same
1338 return Empty;
1339 end Build_Actual_Subtype_Of_Component;
1341 ---------------------------------
1342 -- Build_Class_Wide_Clone_Body --
1343 ---------------------------------
1345 procedure Build_Class_Wide_Clone_Body
1346 (Spec_Id : Entity_Id;
1347 Bod : Node_Id)
1349 Loc : constant Source_Ptr := Sloc (Bod);
1350 Clone_Id : constant Entity_Id := Class_Wide_Clone (Spec_Id);
1351 Clone_Body : Node_Id;
1353 begin
1354 -- The declaration of the class-wide clone was created when the
1355 -- corresponding class-wide condition was analyzed.
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));
1364 -- The new operation is internal and overriding indicators do not apply
1365 -- (the original primitive may have carried one).
1367 Set_Must_Override (Specification (Clone_Body), False);
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.
1373 if Nkind (Parent (Bod)) = N_Subunit then
1374 Insert_After (Corresponding_Stub (Parent (Bod)), Clone_Body);
1376 else
1377 Insert_Before (Bod, Clone_Body);
1378 end if;
1380 Analyze (Clone_Body);
1381 end Build_Class_Wide_Clone_Body;
1383 ---------------------------------
1384 -- Build_Class_Wide_Clone_Call --
1385 ---------------------------------
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
1393 Clone_Id : constant Entity_Id := Class_Wide_Clone (Spec_Id);
1394 Par_Type : constant Entity_Id := Find_Dispatching_Type (Spec_Id);
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;
1403 begin
1404 Actuals := Empty_List;
1405 Formal := First_Formal (Spec_Id);
1406 New_F_Spec := First (Parameter_Specifications (Spec));
1408 -- Build parameter association for call to class-wide clone.
1410 while Present (Formal) loop
1411 New_Formal := Defining_Identifier (New_F_Spec);
1413 -- If controlling argument and operation is inherited, add conversion
1414 -- to parent type for the call.
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)));
1424 else
1425 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1426 end if;
1428 Next_Formal (Formal);
1429 Next (New_F_Spec);
1430 end loop;
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;
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))));
1456 return New_Body;
1457 end Build_Class_Wide_Clone_Call;
1459 ---------------------------------
1460 -- Build_Class_Wide_Clone_Decl --
1461 ---------------------------------
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"));
1469 Decl : Node_Id;
1470 Spec : Node_Id;
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);
1478 Decl := Make_Subprogram_Declaration (Loc, Spec);
1479 Append (Decl, List_Containing (Unit_Declaration_Node (Spec_Id)));
1481 -- Link clone to original subprogram, for use when building body and
1482 -- wrapper call to inherited operation.
1484 Set_Class_Wide_Clone (Spec_Id, Clone_Id);
1485 end Build_Class_Wide_Clone_Decl;
1487 -----------------------------
1488 -- Build_Component_Subtype --
1489 -----------------------------
1491 function Build_Component_Subtype
1492 (C : List_Id;
1493 Loc : Source_Ptr;
1494 T : Entity_Id) return Node_Id
1496 Subt : Entity_Id;
1497 Decl : Node_Id;
1499 begin
1500 -- Unchecked_Union components do not require component subtypes
1502 if Is_Unchecked_Union (T) then
1503 return Empty;
1504 end if;
1506 Subt := Make_Temporary (Loc, 'S');
1507 Set_Is_Internal (Subt);
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)));
1519 Mark_Rewrite_Insertion (Decl);
1520 return Decl;
1521 end Build_Component_Subtype;
1523 ---------------------------
1524 -- Build_Default_Subtype --
1525 ---------------------------
1527 function Build_Default_Subtype
1528 (T : Entity_Id;
1529 N : Node_Id) return Entity_Id
1531 Loc : constant Source_Ptr := Sloc (N);
1532 Disc : Entity_Id;
1534 Bas : Entity_Id;
1535 -- The base type that is to be constrained by the defaults
1537 begin
1538 if not Has_Discriminants (T) or else Is_Constrained (T) then
1539 return T;
1540 end if;
1542 Bas := Base_Type (T);
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.
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;
1557 Disc := First_Discriminant (T);
1559 if No (Discriminant_Default_Value (Disc)) then
1560 return T;
1561 end if;
1563 declare
1564 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1565 Constraints : constant List_Id := New_List;
1566 Decl : Node_Id;
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;
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)));
1585 Insert_Action (N, Decl);
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.
1591 if Ekind (Current_Scope) /= E_Record_Type then
1592 Analyze (Decl);
1593 end if;
1595 return Act;
1596 end;
1597 end Build_Default_Subtype;
1599 --------------------------------------------
1600 -- Build_Discriminal_Subtype_Of_Component --
1601 --------------------------------------------
1603 function Build_Discriminal_Subtype_Of_Component
1604 (T : Entity_Id) return Node_Id
1606 Loc : constant Source_Ptr := Sloc (T);
1607 D : Elmt_Id;
1608 Id : Node_Id;
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.
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.
1619 ----------------------------------------
1620 -- Build_Discriminal_Array_Constraint --
1621 ----------------------------------------
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;
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));
1637 if Denotes_Discriminant (Old_Lo) then
1638 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1640 else
1641 Lo := New_Copy_Tree (Old_Lo);
1642 end if;
1644 if Denotes_Discriminant (Old_Hi) then
1645 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1647 else
1648 Hi := New_Copy_Tree (Old_Hi);
1649 end if;
1651 Append (Make_Range (Loc, Lo, Hi), Constraints);
1652 Next_Index (Indx);
1653 end loop;
1655 return Constraints;
1656 end Build_Discriminal_Array_Constraint;
1658 -----------------------------------------
1659 -- Build_Discriminal_Record_Constraint --
1660 -----------------------------------------
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;
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;
1677 Append (D_Val, Constraints);
1678 Next_Elmt (D);
1679 end loop;
1681 return Constraints;
1682 end Build_Discriminal_Record_Constraint;
1684 -- Start of processing for Build_Discriminal_Subtype_Of_Component
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;
1698 Next_Index (Id);
1699 end loop;
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;
1712 Next_Elmt (D);
1713 end loop;
1714 end if;
1716 -- If none of the above, the actual and nominal subtypes are the same
1718 return Empty;
1719 end Build_Discriminal_Subtype_Of_Component;
1721 ------------------------------
1722 -- Build_Elaboration_Entity --
1723 ------------------------------
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;
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.
1735 ----------------------
1736 -- Set_Package_Name --
1737 ----------------------
1739 procedure Set_Package_Name (Ent : Entity_Id) is
1740 begin
1741 if Scope (Ent) /= Standard_Standard then
1742 Set_Package_Name (Scope (Ent));
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;
1753 else
1754 Get_Name_String (Chars (Ent));
1755 end if;
1756 end Set_Package_Name;
1758 -- Start of processing for Build_Elaboration_Entity
1760 begin
1761 -- Ignore call if already constructed
1763 if Present (Elaboration_Entity (Spec_Id)) then
1764 return;
1766 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1767 -- no role in analysis.
1769 elsif ASIS_Mode then
1770 return;
1772 -- Do not generate an elaboration entity in GNATprove move because the
1773 -- elaboration counter is a form of expansion.
1775 elsif GNATprove_Mode then
1776 return;
1778 -- See if we need elaboration entity
1780 -- We always need an elaboration entity when preserving control flow, as
1781 -- we want to remain explicit about the unit's elaboration order.
1783 elsif Opt.Suppress_Control_Flow_Optimizations then
1784 null;
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.
1790 elsif Dynamic_Elaboration_Checks then
1791 null;
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.
1799 elsif Restriction_Active (No_Elaboration_Code) then
1800 return;
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).
1807 elsif Restriction_Active (No_Multiple_Elaboration) then
1808 return;
1809 end if;
1811 -- Here we need the elaboration entity
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.
1818 Set_Package_Name (Spec_Id);
1819 Add_Str_To_Name_Buffer ("_E");
1821 -- Create elaboration counter
1823 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1824 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
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));
1833 Push_Scope (Standard_Standard);
1834 Add_Global_Declaration (Decl);
1835 Pop_Scope;
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.
1841 Set_Is_True_Constant (Elab_Ent, False);
1842 Set_Current_Value (Elab_Ent, Empty);
1843 Set_Last_Assignment (Elab_Ent, Empty);
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).
1849 Set_Has_Qualified_Name (Elab_Ent);
1850 Set_Has_Fully_Qualified_Name (Elab_Ent);
1851 end Build_Elaboration_Entity;
1853 --------------------------------
1854 -- Build_Explicit_Dereference --
1855 --------------------------------
1857 procedure Build_Explicit_Dereference
1858 (Expr : Node_Id;
1859 Disc : Entity_Id)
1861 Loc : constant Source_Ptr := Sloc (Expr);
1862 I : Interp_Index;
1863 It : Interp;
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.
1872 if Is_Entity_Name (Expr) then
1873 Set_Etype (Expr, Etype (Entity (Expr)));
1875 -- The designated entity will not be examined again when resolving
1876 -- the dereference, so generate a reference to it now.
1878 Generate_Reference (Entity (Expr), Expr);
1880 elsif Nkind (Expr) = N_Function_Call then
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.
1886 if Is_Overloaded (Name (Expr)) then
1887 Get_First_Interp (Name (Expr), I, It);
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;
1898 Get_Next_Interp (I, It);
1899 end loop;
1900 end if;
1902 -- Set type of call from resolved function name.
1904 Set_Etype (Expr, Etype (Name (Expr)));
1905 end if;
1907 Set_Is_Overloaded (Expr, False);
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.
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;
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;
1929 ---------------------------
1930 -- Build_Overriding_Spec --
1931 ---------------------------
1933 function Build_Overriding_Spec
1934 (Op : Entity_Id;
1935 Typ : Entity_Id) return Node_Id
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));
1941 Formal_Spec : Node_Id;
1942 Formal_Type : Node_Id;
1943 New_Spec : Node_Id;
1945 begin
1946 New_Spec := Copy_Subprogram_Spec (Spec);
1948 Formal_Spec := First (Parameter_Specifications (New_Spec));
1949 while Present (Formal_Spec) loop
1950 Formal_Type := Parameter_Type (Formal_Spec);
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;
1958 -- Nothing needs to be done for access parameters
1960 Next (Formal_Spec);
1961 end loop;
1963 return New_Spec;
1964 end Build_Overriding_Spec;
1966 -----------------------------------
1967 -- Cannot_Raise_Constraint_Error --
1968 -----------------------------------
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;
1975 elsif Do_Range_Check (Expr) then
1976 return False;
1978 elsif Raises_Constraint_Error (Expr) then
1979 return False;
1981 else
1982 case Nkind (Expr) is
1983 when N_Identifier =>
1984 return True;
1986 when N_Expanded_Name =>
1987 return True;
1989 when N_Selected_Component =>
1990 return not Do_Discriminant_Check (Expr);
1992 when N_Attribute_Reference =>
1993 if Do_Overflow_Check (Expr) then
1994 return False;
1996 elsif No (Expressions (Expr)) then
1997 return True;
1999 else
2000 declare
2001 N : Node_Id;
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;
2013 return True;
2014 end;
2015 end if;
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;
2027 when N_Unchecked_Type_Conversion =>
2028 return Cannot_Raise_Constraint_Error (Expression (Expr));
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;
2037 when N_Op_Divide
2038 | N_Op_Mod
2039 | N_Op_Rem
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;
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
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;
2082 when others =>
2083 return False;
2084 end case;
2085 end if;
2086 end Cannot_Raise_Constraint_Error;
2088 -----------------------------------------
2089 -- Check_Dynamically_Tagged_Expression --
2090 -----------------------------------------
2092 procedure Check_Dynamically_Tagged_Expression
2093 (Expr : Node_Id;
2094 Typ : Entity_Id;
2095 Related_Nod : Node_Id)
2097 begin
2098 pragma Assert (Is_Tagged_Type (Typ));
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.
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;
2114 --------------------------
2115 -- Check_Fully_Declared --
2116 --------------------------
2118 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2119 begin
2120 if Ekind (T) = E_Incomplete_Type then
2122 -- Ada 2005 (AI-50217): If the type is available through a limited
2123 -- with_clause, verify that its full view has been analyzed.
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
2131 null;
2133 else
2134 Error_Msg_NE
2135 ("premature usage of incomplete}", N, First_Subtype (T));
2136 end if;
2138 -- Need comments for these tests ???
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.
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;
2162 -------------------------------------------
2163 -- Check_Function_With_Address_Parameter --
2164 -------------------------------------------
2166 procedure Check_Function_With_Address_Parameter (Subp_Id : Entity_Id) is
2167 F : Entity_Id;
2168 T : Entity_Id;
2170 begin
2171 F := First_Formal (Subp_Id);
2172 while Present (F) loop
2173 T := Etype (F);
2175 if Is_Private_Type (T) and then Present (Full_View (T)) then
2176 T := Full_View (T);
2177 end if;
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;
2184 Next_Formal (F);
2185 end loop;
2186 end Check_Function_With_Address_Parameter;
2188 -------------------------------------
2189 -- Check_Function_Writable_Actuals --
2190 -------------------------------------
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;
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.
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???
2211 -------------------------
2212 -- Collect_Identifiers --
2213 -------------------------
2215 procedure Collect_Identifiers (N : Node_Id) is
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.
2222 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2223 -- Returns True if List has a node whose Entity is Entity (N)
2225 ----------------
2226 -- Check_Node --
2227 ----------------
2229 function Check_Node (N : Node_Id) return Traverse_Result is
2230 Is_Writable_Actual : Boolean := False;
2231 Id : Entity_Id;
2233 begin
2234 if Nkind (N) = N_Identifier then
2236 -- No analysis possible if the entity is not decorated
2238 if No (Entity (N)) then
2239 return Skip;
2241 -- Don't collect identifiers of packages, called functions, etc
2243 elsif Ekind_In (Entity (N), E_Package,
2244 E_Function,
2245 E_Procedure,
2246 E_Entry)
2247 then
2248 return Skip;
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.
2254 elsif Is_Rewrite_Substitution (N) then
2255 Collect_Identifiers (Original_Node (N));
2256 return Skip;
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???
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)));
2273 begin
2274 if Ekind (Entity (N)) = E_Discriminant then
2275 return Skip;
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;
2285 -- Analyze if N is a writable actual of a function
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;
2293 begin
2294 Id := Get_Called_Entity (Call);
2296 -- In case of previous error, no check is possible
2298 if No (Id) then
2299 return Abandon;
2300 end if;
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;
2315 exit;
2316 end if;
2318 Next_Formal (Formal);
2319 Next_Actual (Actual);
2320 end loop;
2321 end if;
2322 end;
2323 end if;
2325 if Is_Writable_Actual then
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).
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;
2338 -- Second occurrence of an elementary type writable actual
2340 elsif Contains (Writable_Actuals_List, N) then
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).
2348 declare
2349 Elmt : Elmt_Id;
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;
2359 if Sloc (N) > Sloc (Node (Elmt)) then
2360 Error_Node := N;
2361 else
2362 Error_Node := Node (Elmt);
2363 end if;
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;
2372 -- First occurrence of a elementary type writable actual
2374 else
2375 Append_New_Elmt (N, To => Writable_Actuals_List);
2376 end if;
2378 else
2379 if Identifiers_List = No_Elist then
2380 Identifiers_List := New_Elmt_List;
2381 end if;
2383 Append_Unique_Elmt (N, Identifiers_List);
2384 end if;
2385 end if;
2387 return OK;
2388 end Check_Node;
2390 --------------
2391 -- Contains --
2392 --------------
2394 function Contains
2395 (List : Elist_Id;
2396 N : Node_Id) return Boolean
2398 pragma Assert (Nkind (N) in N_Has_Entity);
2400 Elmt : Elmt_Id;
2402 begin
2403 if List = No_Elist then
2404 return False;
2405 end if;
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;
2416 return False;
2417 end Contains;
2419 ------------------
2420 -- Do_Traversal --
2421 ------------------
2423 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2424 -- The traversal procedure
2426 -- Start of processing for Collect_Identifiers
2428 begin
2429 if Present (Error_Node) then
2430 return;
2431 end if;
2433 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2434 return;
2435 end if;
2437 Do_Traversal (N);
2438 end Collect_Identifiers;
2440 -------------------------------
2441 -- Preanalyze_Without_Errors --
2442 -------------------------------
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;
2452 -- Start of processing for Check_Function_Writable_Actuals
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.
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)))
2473 -- In addition, this check only applies to source code, not to code
2474 -- generated by constraint checks.
2476 or else not Comes_From_Source (N)
2477 then
2478 return;
2479 end if;
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)).
2491 case Nkind (N) is
2492 when N_Range =>
2493 Collect_Identifiers (Low_Bound (N));
2494 Collect_Identifiers (High_Bound (N));
2496 when N_Membership_Test
2497 | N_Op
2499 declare
2500 Expr : Node_Id;
2502 begin
2503 Collect_Identifiers (Left_Opnd (N));
2505 if Present (Right_Opnd (N)) then
2506 Collect_Identifiers (Right_Opnd (N));
2507 end if;
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);
2516 Next (Expr);
2517 end loop;
2518 end if;
2519 end;
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
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;
2536 Comp : Node_Id;
2537 Def_Id : Entity_Id := Defining_Identifier (N);
2538 Rec : Node_Id := Get_Record_Part (N);
2540 begin
2541 -- No need to perform any analysis if the record has no
2542 -- components
2544 if No (Rec) or else No (Component_List (Rec)) then
2545 return;
2546 end if;
2548 -- Collect the identifiers starting from the deepest
2549 -- derivation. Done to report the error in the deepest
2550 -- derivation.
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;
2562 Next (Comp);
2563 end loop;
2564 end if;
2566 exit when No (Underlying_Type (Etype (Def_Id)))
2567 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2568 = Def_Id;
2570 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2571 Rec := Get_Record_Part (Parent (Def_Id));
2572 end loop;
2573 end;
2575 when N_Entry_Call_Statement
2576 | N_Subprogram_Call
2578 declare
2579 Id : constant Entity_Id := Get_Called_Entity (N);
2580 Formal : Node_Id;
2581 Actual : Node_Id;
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;
2593 Next_Formal (Formal);
2594 Next_Actual (Actual);
2595 end loop;
2596 end;
2598 when N_Aggregate
2599 | N_Extension_Aggregate
2601 declare
2602 Assoc : Node_Id;
2603 Choice : Node_Id;
2604 Comp_Expr : Node_Id;
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.
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)))
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;
2630 begin
2631 -- Count positional associations
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;
2641 -- Count the rest of elements and locate the N_Others
2642 -- choice (if any)
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);
2653 -- Count several components
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;
2672 -- Count single component. No other case available
2673 -- since we are handling an aggregate with static
2674 -- bounds.
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);
2681 Count_Components := Count_Components + 1;
2682 end if;
2684 Next (Choice);
2685 end loop;
2687 Next (Assoc);
2688 end loop;
2690 Num_Components :=
2691 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2692 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2694 pragma Assert (Count_Components <= Num_Components);
2696 -- Handle the N_Others choice if it covers several
2697 -- components
2699 if Present (Others_Choice)
2700 and then (Num_Components - Count_Components) > 1
2701 then
2702 if not Others_Box_Present then
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.
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;
2719 Collect_Identifiers (Comp_Expr);
2721 if Writable_Actuals_List /= No_Elist then
2723 -- As suggested by Robert, at current stage we
2724 -- report occurrences of this case as warnings.
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;
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)).
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
2747 declare
2748 Count_Components : Natural := 0;
2749 Low, High : Node_Id;
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);
2763 if not Compile_Time_Known_Value (Low) then
2764 Collect_Identifiers (Low);
2766 if No (Aggr_Error_Node) then
2767 Aggr_Error_Node := Low;
2768 end if;
2769 end if;
2771 if not Compile_Time_Known_Value (High) then
2772 Collect_Identifiers (High);
2774 if No (Aggr_Error_Node) then
2775 Aggr_Error_Node := High;
2776 end if;
2777 end if;
2779 -- The RM rule is violated if there is more than
2780 -- a single choice in a component association.
2782 else
2783 Count_Components := Count_Components + 1;
2785 if No (Aggr_Error_Node)
2786 and then Count_Components > 1
2787 then
2788 Aggr_Error_Node := Choice;
2789 end if;
2791 if not Compile_Time_Known_Value (Choice) then
2792 Collect_Identifiers (Choice);
2793 end if;
2794 end if;
2796 Next (Choice);
2797 end loop;
2799 Next (Assoc);
2800 end loop;
2801 end;
2802 end if;
2804 -- Handle ancestor part of extension aggregates
2806 if Nkind (N) = N_Extension_Aggregate then
2807 Collect_Identifiers (Ancestor_Part (N));
2808 end if;
2810 -- Handle positional associations
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;
2819 Next (Comp_Expr);
2820 end loop;
2821 end if;
2823 -- Handle discrete associations
2825 if Present (Component_Associations (N)) then
2826 Assoc := First (Component_Associations (N));
2827 while Present (Assoc) loop
2829 if not Box_Present (Assoc) then
2830 Choice := First (Choices (Assoc));
2831 while Present (Choice) loop
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???
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;
2846 elsif Box_Present (Assoc) then
2847 null;
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;
2859 Collect_Identifiers (Comp_Expr);
2860 end if;
2862 Next (Choice);
2863 end loop;
2864 end if;
2866 Next (Assoc);
2867 end loop;
2868 end if;
2869 end;
2871 when others =>
2872 return;
2873 end case;
2875 -- No further action needed if we already reported an error
2877 if Present (Error_Node) then
2878 return;
2879 end if;
2881 -- Check violation of RM 6.20/3 in aggregates
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;
2893 -- Check if some writable argument of a function is referenced
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;
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
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));
2919 when N_In
2920 | N_Not_In
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));
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;
2937 Next_Elmt (Elmt_2);
2938 end loop;
2940 Next_Elmt (Elmt_1);
2941 end loop;
2942 end;
2943 end if;
2944 end Check_Function_Writable_Actuals;
2946 --------------------------------
2947 -- Check_Implicit_Dereference --
2948 --------------------------------
2950 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2951 Disc : Entity_Id;
2952 Desig : Entity_Id;
2953 Nam : Node_Id;
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;
2964 if Ada_Version < Ada_2012
2965 or else not Has_Implicit_Dereference (Base_Type (Typ))
2966 then
2967 return;
2969 elsif not Comes_From_Source (N)
2970 and then Nkind (N) /= N_Indexed_Component
2971 then
2972 return;
2974 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2975 null;
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);
2984 -- If the node is a generalized indexing, add interpretation
2985 -- to that node as well, for subsequent resolution.
2987 if Nkind (N) = N_Indexed_Component then
2988 Add_One_Interp (N, Disc, Desig);
2989 end if;
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.
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;
3003 exit;
3004 end if;
3006 Next_Discriminant (Disc);
3007 end loop;
3008 end if;
3009 end Check_Implicit_Dereference;
3011 ----------------------------------
3012 -- Check_Internal_Protected_Use --
3013 ----------------------------------
3015 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
3016 S : Entity_Id;
3017 Prot : Entity_Id;
3019 begin
3020 Prot := Empty;
3022 S := Current_Scope;
3023 while Present (S) loop
3024 if S = Standard_Standard then
3025 exit;
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;
3034 S := Scope (S);
3035 end loop;
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.
3046 if Ekind (Nam) = E_Subprogram_Type
3047 and then Nkind (Associated_Node_For_Itype (Nam)) =
3048 N_Function_Specification
3049 then
3050 null;
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);
3057 elsif Nkind (N) = N_Attribute_Reference then
3058 Error_Msg_N
3059 ("within protected function cannot take access of protected "
3060 & "procedure", N);
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;
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.
3076 if Convention (Nam) = Convention_Protected then
3077 declare
3078 P : Node_Id;
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;
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.
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;
3110 P := Parent (P);
3111 end loop;
3113 return;
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;
3126 P := Parent (P);
3127 end loop;
3128 end;
3129 end if;
3130 end Check_Internal_Protected_Use;
3132 ---------------------------------------
3133 -- Check_Later_Vs_Basic_Declarations --
3134 ---------------------------------------
3136 procedure Check_Later_Vs_Basic_Declarations
3137 (Decls : List_Id;
3138 During_Parsing : Boolean)
3140 Body_Sloc : Source_Ptr;
3141 Decl : Node_Id;
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.
3148 -------------------------------
3149 -- Is_Later_Declarative_Item --
3150 -------------------------------
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;
3157 elsif Nkind (Decl) = N_Pragma then
3158 return True;
3160 elsif During_Parsing then
3161 return False;
3163 -- In SPARK, a package declaration is not considered as a later
3164 -- declarative item.
3166 elsif Nkind (Decl) = N_Package_Declaration then
3167 return False;
3169 -- In SPARK, a renaming is considered as a later declarative item
3171 elsif Nkind (Decl) in N_Renaming_Declaration then
3172 return True;
3174 else
3175 return False;
3176 end if;
3177 end Is_Later_Declarative_Item;
3179 -- Start of processing for Check_Later_Vs_Basic_Declarations
3181 begin
3182 Decl := First (Decls);
3184 -- Loop through sequence of basic declarative items
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);
3192 -- Once a body is encountered, we only allow later declarative
3193 -- items. The inner loop checks the rest of the list.
3195 else
3196 Body_Sloc := Sloc (Decl);
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;
3213 Next (Decl);
3214 end loop Inner;
3215 end if;
3216 end loop Outer;
3217 end Check_Later_Vs_Basic_Declarations;
3219 ---------------------------
3220 -- Check_No_Hidden_State --
3221 ---------------------------
3223 procedure Check_No_Hidden_State (Id : Entity_Id) is
3224 Context : Entity_Id := Empty;
3225 Not_Visible : Boolean := False;
3226 Scop : Entity_Id;
3228 begin
3229 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3231 -- Find the proper context where the object or state appears
3233 Scop := Scope (Id);
3234 while Present (Scop) loop
3235 Context := Scop;
3237 -- Keep track of the context's visibility
3239 Not_Visible := Not_Visible or else In_Private_Part (Context);
3241 -- Prevent the search from going too far
3243 if Context = Standard_Standard then
3244 return;
3246 -- Objects and states that appear immediately within a subprogram or
3247 -- inside a construct nested within a subprogram do not introduce a
3248 -- hidden state. They behave as local variable declarations.
3250 elsif Is_Subprogram (Context) then
3251 return;
3253 -- When examining a package body, use the entity of the spec as it
3254 -- carries the abstract state declarations.
3256 elsif Ekind (Context) = E_Package_Body then
3257 Context := Spec_Entity (Context);
3258 end if;
3260 -- Stop the traversal when a package subject to a null abstract state
3261 -- has been found.
3263 if Ekind_In (Context, E_Generic_Package, E_Package)
3264 and then Has_Null_Abstract_State (Context)
3265 then
3266 exit;
3267 end if;
3269 Scop := Scope (Scop);
3270 end loop;
3272 -- At this point we know that there is at least one package with a null
3273 -- abstract state in visibility. Emit an error message unconditionally
3274 -- if the entity being processed is a state because the placement of the
3275 -- related package is irrelevant. This is not the case for objects as
3276 -- the intermediate context matters.
3278 if Present (Context)
3279 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3280 then
3281 Error_Msg_N ("cannot introduce hidden state &", Id);
3282 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3283 end if;
3284 end Check_No_Hidden_State;
3286 ----------------------------------------
3287 -- Check_Nonvolatile_Function_Profile --
3288 ----------------------------------------
3290 procedure Check_Nonvolatile_Function_Profile (Func_Id : Entity_Id) is
3291 Formal : Entity_Id;
3293 begin
3294 -- Inspect all formal parameters
3296 Formal := First_Formal (Func_Id);
3297 while Present (Formal) loop
3298 if Is_Effectively_Volatile (Etype (Formal)) then
3299 Error_Msg_NE
3300 ("nonvolatile function & cannot have a volatile parameter",
3301 Formal, Func_Id);
3302 end if;
3304 Next_Formal (Formal);
3305 end loop;
3307 -- Inspect the return type
3309 if Is_Effectively_Volatile (Etype (Func_Id)) then
3310 Error_Msg_NE
3311 ("nonvolatile function & cannot have a volatile return type",
3312 Result_Definition (Parent (Func_Id)), Func_Id);
3313 end if;
3314 end Check_Nonvolatile_Function_Profile;
3316 -----------------------------
3317 -- Check_Part_Of_Reference --
3318 -----------------------------
3320 procedure Check_Part_Of_Reference (Var_Id : Entity_Id; Ref : Node_Id) is
3321 function Is_Enclosing_Package_Body
3322 (Body_Decl : Node_Id;
3323 Obj_Id : Entity_Id) return Boolean;
3324 pragma Inline (Is_Enclosing_Package_Body);
3325 -- Determine whether package body Body_Decl or its corresponding spec
3326 -- immediately encloses the declaration of object Obj_Id.
3328 function Is_Internal_Declaration_Or_Body
3329 (Decl : Node_Id) return Boolean;
3330 pragma Inline (Is_Internal_Declaration_Or_Body);
3331 -- Determine whether declaration or body denoted by Decl is internal
3333 function Is_Single_Declaration_Or_Body
3334 (Decl : Node_Id;
3335 Conc_Typ : Entity_Id) return Boolean;
3336 pragma Inline (Is_Single_Declaration_Or_Body);
3337 -- Determine whether protected/task declaration or body denoted by Decl
3338 -- belongs to single concurrent type Conc_Typ.
3340 function Is_Single_Task_Pragma
3341 (Prag : Node_Id;
3342 Task_Typ : Entity_Id) return Boolean;
3343 pragma Inline (Is_Single_Task_Pragma);
3344 -- Determine whether pragma Prag belongs to single task type Task_Typ
3346 -------------------------------
3347 -- Is_Enclosing_Package_Body --
3348 -------------------------------
3350 function Is_Enclosing_Package_Body
3351 (Body_Decl : Node_Id;
3352 Obj_Id : Entity_Id) return Boolean
3354 Obj_Context : Node_Id;
3356 begin
3357 -- Find the context of the object declaration
3359 Obj_Context := Parent (Declaration_Node (Obj_Id));
3361 if Nkind (Obj_Context) = N_Package_Specification then
3362 Obj_Context := Parent (Obj_Context);
3363 end if;
3365 -- The object appears immediately within the package body
3367 if Obj_Context = Body_Decl then
3368 return True;
3370 -- The object appears immediately within the corresponding spec
3372 elsif Nkind (Obj_Context) = N_Package_Declaration
3373 and then Unit_Declaration_Node (Corresponding_Spec (Body_Decl)) =
3374 Obj_Context
3375 then
3376 return True;
3377 end if;
3379 return False;
3380 end Is_Enclosing_Package_Body;
3382 -------------------------------------
3383 -- Is_Internal_Declaration_Or_Body --
3384 -------------------------------------
3386 function Is_Internal_Declaration_Or_Body
3387 (Decl : Node_Id) return Boolean
3389 begin
3390 if Comes_From_Source (Decl) then
3391 return False;
3393 -- A body generated for an expression function which has not been
3394 -- inserted into the tree yet (In_Spec_Expression is True) is not
3395 -- considered internal.
3397 elsif Nkind (Decl) = N_Subprogram_Body
3398 and then Was_Expression_Function (Decl)
3399 and then not In_Spec_Expression
3400 then
3401 return False;
3402 end if;
3404 return True;
3405 end Is_Internal_Declaration_Or_Body;
3407 -----------------------------------
3408 -- Is_Single_Declaration_Or_Body --
3409 -----------------------------------
3411 function Is_Single_Declaration_Or_Body
3412 (Decl : Node_Id;
3413 Conc_Typ : Entity_Id) return Boolean
3415 Spec_Id : constant Entity_Id := Unique_Defining_Entity (Decl);
3417 begin
3418 return
3419 Present (Anonymous_Object (Spec_Id))
3420 and then Anonymous_Object (Spec_Id) = Conc_Typ;
3421 end Is_Single_Declaration_Or_Body;
3423 ---------------------------
3424 -- Is_Single_Task_Pragma --
3425 ---------------------------
3427 function Is_Single_Task_Pragma
3428 (Prag : Node_Id;
3429 Task_Typ : Entity_Id) return Boolean
3431 Decl : constant Node_Id := Find_Related_Declaration_Or_Body (Prag);
3433 begin
3434 -- To qualify, the pragma must be associated with single task type
3435 -- Task_Typ.
3437 return
3438 Is_Single_Task_Object (Task_Typ)
3439 and then Nkind (Decl) = N_Object_Declaration
3440 and then Defining_Entity (Decl) = Task_Typ;
3441 end Is_Single_Task_Pragma;
3443 -- Local variables
3445 Conc_Obj : constant Entity_Id := Encapsulating_State (Var_Id);
3446 Par : Node_Id;
3447 Prag_Nam : Name_Id;
3448 Prev : Node_Id;
3450 -- Start of processing for Check_Part_Of_Reference
3452 begin
3453 -- Nothing to do when the variable was recorded, but did not become a
3454 -- constituent of a single concurrent type.
3456 if No (Conc_Obj) then
3457 return;
3458 end if;
3460 -- Traverse the parent chain looking for a suitable context for the
3461 -- reference to the concurrent constituent.
3463 Prev := Ref;
3464 Par := Parent (Prev);
3465 while Present (Par) loop
3466 if Nkind (Par) = N_Pragma then
3467 Prag_Nam := Pragma_Name (Par);
3469 -- A concurrent constituent is allowed to appear in pragmas
3470 -- Initial_Condition and Initializes as this is part of the
3471 -- elaboration checks for the constituent (SPARK RM 9(3)).
3473 if Nam_In (Prag_Nam, Name_Initial_Condition, Name_Initializes) then
3474 return;
3476 -- When the reference appears within pragma Depends or Global,
3477 -- check whether the pragma applies to a single task type. Note
3478 -- that the pragma may not encapsulated by the type definition,
3479 -- but this is still a valid context.
3481 elsif Nam_In (Prag_Nam, Name_Depends, Name_Global)
3482 and then Is_Single_Task_Pragma (Par, Conc_Obj)
3483 then
3484 return;
3485 end if;
3487 -- The reference appears somewhere in the definition of a single
3488 -- concurrent type (SPARK RM 9(3)).
3490 elsif Nkind_In (Par, N_Single_Protected_Declaration,
3491 N_Single_Task_Declaration)
3492 and then Defining_Entity (Par) = Conc_Obj
3493 then
3494 return;
3496 -- The reference appears within the declaration or body of a single
3497 -- concurrent type (SPARK RM 9(3)).
3499 elsif Nkind_In (Par, N_Protected_Body,
3500 N_Protected_Type_Declaration,
3501 N_Task_Body,
3502 N_Task_Type_Declaration)
3503 and then Is_Single_Declaration_Or_Body (Par, Conc_Obj)
3504 then
3505 return;
3507 -- The reference appears within the statement list of the object's
3508 -- immediately enclosing package (SPARK RM 9(3)).
3510 elsif Nkind (Par) = N_Package_Body
3511 and then Nkind (Prev) = N_Handled_Sequence_Of_Statements
3512 and then Is_Enclosing_Package_Body (Par, Var_Id)
3513 then
3514 return;
3516 -- The reference has been relocated within an internally generated
3517 -- package or subprogram. Assume that the reference is legal as the
3518 -- real check was already performed in the original context of the
3519 -- reference.
3521 elsif Nkind_In (Par, N_Package_Body,
3522 N_Package_Declaration,
3523 N_Subprogram_Body,
3524 N_Subprogram_Declaration)
3525 and then Is_Internal_Declaration_Or_Body (Par)
3526 then
3527 return;
3529 -- The reference has been relocated to an inlined body for GNATprove.
3530 -- Assume that the reference is legal as the real check was already
3531 -- performed in the original context of the reference.
3533 elsif GNATprove_Mode
3534 and then Nkind (Par) = N_Subprogram_Body
3535 and then Chars (Defining_Entity (Par)) = Name_uParent
3536 then
3537 return;
3538 end if;
3540 Prev := Par;
3541 Par := Parent (Prev);
3542 end loop;
3544 -- At this point it is known that the reference does not appear within a
3545 -- legal context.
3547 Error_Msg_NE
3548 ("reference to variable & cannot appear in this context", Ref, Var_Id);
3549 Error_Msg_Name_1 := Chars (Var_Id);
3551 if Is_Single_Protected_Object (Conc_Obj) then
3552 Error_Msg_NE
3553 ("\% is constituent of single protected type &", Ref, Conc_Obj);
3555 else
3556 Error_Msg_NE
3557 ("\% is constituent of single task type &", Ref, Conc_Obj);
3558 end if;
3559 end Check_Part_Of_Reference;
3561 ------------------------------------------
3562 -- Check_Potentially_Blocking_Operation --
3563 ------------------------------------------
3565 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3566 S : Entity_Id;
3568 begin
3569 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3570 -- When pragma Detect_Blocking is active, the run time will raise
3571 -- Program_Error. Here we only issue a warning, since we generally
3572 -- support the use of potentially blocking operations in the absence
3573 -- of the pragma.
3575 -- Indirect blocking through a subprogram call cannot be diagnosed
3576 -- statically without interprocedural analysis, so we do not attempt
3577 -- to do it here.
3579 S := Scope (Current_Scope);
3580 while Present (S) and then S /= Standard_Standard loop
3581 if Is_Protected_Type (S) then
3582 Error_Msg_N
3583 ("potentially blocking operation in protected operation??", N);
3584 return;
3585 end if;
3587 S := Scope (S);
3588 end loop;
3589 end Check_Potentially_Blocking_Operation;
3591 ------------------------------------
3592 -- Check_Previous_Null_Procedure --
3593 ------------------------------------
3595 procedure Check_Previous_Null_Procedure
3596 (Decl : Node_Id;
3597 Prev : Entity_Id)
3599 begin
3600 if Ekind (Prev) = E_Procedure
3601 and then Nkind (Parent (Prev)) = N_Procedure_Specification
3602 and then Null_Present (Parent (Prev))
3603 then
3604 Error_Msg_Sloc := Sloc (Prev);
3605 Error_Msg_N
3606 ("declaration cannot complete previous null procedure#", Decl);
3607 end if;
3608 end Check_Previous_Null_Procedure;
3610 ---------------------------------
3611 -- Check_Result_And_Post_State --
3612 ---------------------------------
3614 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3615 procedure Check_Result_And_Post_State_In_Pragma
3616 (Prag : Node_Id;
3617 Result_Seen : in out Boolean);
3618 -- Determine whether pragma Prag mentions attribute 'Result and whether
3619 -- the pragma contains an expression that evaluates differently in pre-
3620 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3621 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3623 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3624 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3625 -- formal parameter.
3627 -------------------------------------------
3628 -- Check_Result_And_Post_State_In_Pragma --
3629 -------------------------------------------
3631 procedure Check_Result_And_Post_State_In_Pragma
3632 (Prag : Node_Id;
3633 Result_Seen : in out Boolean)
3635 procedure Check_Conjunct (Expr : Node_Id);
3636 -- Check an individual conjunct in a conjunction of Boolean
3637 -- expressions, connected by "and" or "and then" operators.
3639 procedure Check_Conjuncts (Expr : Node_Id);
3640 -- Apply the post-state check to every conjunct in an expression, in
3641 -- case this is a conjunction of Boolean expressions. Otherwise apply
3642 -- it to the expression as a whole.
3644 procedure Check_Expression (Expr : Node_Id);
3645 -- Perform the 'Result and post-state checks on a given expression
3647 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3648 -- Attempt to find attribute 'Result in a subtree denoted by N
3650 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3651 -- Determine whether source node N denotes "True" or "False"
3653 function Mentions_Post_State (N : Node_Id) return Boolean;
3654 -- Determine whether a subtree denoted by N mentions any construct
3655 -- that denotes a post-state.
3657 procedure Check_Function_Result is
3658 new Traverse_Proc (Is_Function_Result);
3660 --------------------
3661 -- Check_Conjunct --
3662 --------------------
3664 procedure Check_Conjunct (Expr : Node_Id) is
3665 function Adjust_Message (Msg : String) return String;
3666 -- Prepend a prefix to the input message Msg denoting that the
3667 -- message applies to a conjunct in the expression, when this
3668 -- is the case.
3670 function Applied_On_Conjunct return Boolean;
3671 -- Returns True if the message applies to a conjunct in the
3672 -- expression, instead of the whole expression.
3674 function Has_Global_Output (Subp : Entity_Id) return Boolean;
3675 -- Returns True if Subp has an output in its Global contract
3677 function Has_No_Output (Subp : Entity_Id) return Boolean;
3678 -- Returns True if Subp has no declared output: no function
3679 -- result, no output parameter, and no output in its Global
3680 -- contract.
3682 --------------------
3683 -- Adjust_Message --
3684 --------------------
3686 function Adjust_Message (Msg : String) return String is
3687 begin
3688 if Applied_On_Conjunct then
3689 return "conjunct in " & Msg;
3690 else
3691 return Msg;
3692 end if;
3693 end Adjust_Message;
3695 -------------------------
3696 -- Applied_On_Conjunct --
3697 -------------------------
3699 function Applied_On_Conjunct return Boolean is
3700 begin
3701 -- Expr is the conjunct of an enclosing "and" expression
3703 return Nkind (Parent (Expr)) in N_Subexpr
3705 -- or Expr is a conjunct of an enclosing "and then"
3706 -- expression in a postcondition aspect that was split into
3707 -- multiple pragmas. The first conjunct has the "and then"
3708 -- expression as Original_Node, and other conjuncts have
3709 -- Split_PCC set to True.
3711 or else Nkind (Original_Node (Expr)) = N_And_Then
3712 or else Split_PPC (Prag);
3713 end Applied_On_Conjunct;
3715 -----------------------
3716 -- Has_Global_Output --
3717 -----------------------
3719 function Has_Global_Output (Subp : Entity_Id) return Boolean is
3720 Global : constant Node_Id := Get_Pragma (Subp, Pragma_Global);
3721 List : Node_Id;
3722 Assoc : Node_Id;
3724 begin
3725 if No (Global) then
3726 return False;
3727 end if;
3729 List := Expression (Get_Argument (Global, Subp));
3731 -- Empty list (no global items) or single global item
3732 -- declaration (only input items).
3734 if Nkind_In (List, N_Null,
3735 N_Expanded_Name,
3736 N_Identifier,
3737 N_Selected_Component)
3738 then
3739 return False;
3741 -- Simple global list (only input items) or moded global list
3742 -- declaration.
3744 elsif Nkind (List) = N_Aggregate then
3745 if Present (Expressions (List)) then
3746 return False;
3748 else
3749 Assoc := First (Component_Associations (List));
3750 while Present (Assoc) loop
3751 if Chars (First (Choices (Assoc))) /= Name_Input then
3752 return True;
3753 end if;
3755 Next (Assoc);
3756 end loop;
3758 return False;
3759 end if;
3761 -- To accommodate partial decoration of disabled SPARK
3762 -- features, this routine may be called with illegal input.
3763 -- If this is the case, do not raise Program_Error.
3765 else
3766 return False;
3767 end if;
3768 end Has_Global_Output;
3770 -------------------
3771 -- Has_No_Output --
3772 -------------------
3774 function Has_No_Output (Subp : Entity_Id) return Boolean is
3775 Param : Node_Id;
3777 begin
3778 -- A function has its result as output
3780 if Ekind (Subp) = E_Function then
3781 return False;
3782 end if;
3784 -- An OUT or IN OUT parameter is an output
3786 Param := First_Formal (Subp);
3787 while Present (Param) loop
3788 if Ekind_In (Param, E_Out_Parameter, E_In_Out_Parameter) then
3789 return False;
3790 end if;
3792 Next_Formal (Param);
3793 end loop;
3795 -- An item of mode Output or In_Out in the Global contract is
3796 -- an output.
3798 if Has_Global_Output (Subp) then
3799 return False;
3800 end if;
3802 return True;
3803 end Has_No_Output;
3805 -- Local variables
3807 Err_Node : Node_Id;
3808 -- Error node when reporting a warning on a (refined)
3809 -- postcondition.
3811 -- Start of processing for Check_Conjunct
3813 begin
3814 if Applied_On_Conjunct then
3815 Err_Node := Expr;
3816 else
3817 Err_Node := Prag;
3818 end if;
3820 -- Do not report missing reference to outcome in postcondition if
3821 -- either the postcondition is trivially True or False, or if the
3822 -- subprogram is ghost and has no declared output.
3824 if not Is_Trivial_Boolean (Expr)
3825 and then not Mentions_Post_State (Expr)
3826 and then not (Is_Ghost_Entity (Subp_Id)
3827 and then Has_No_Output (Subp_Id))
3828 then
3829 if Pragma_Name (Prag) = Name_Contract_Cases then
3830 Error_Msg_NE (Adjust_Message
3831 ("contract case does not check the outcome of calling "
3832 & "&?T?"), Expr, Subp_Id);
3834 elsif Pragma_Name (Prag) = Name_Refined_Post then
3835 Error_Msg_NE (Adjust_Message
3836 ("refined postcondition does not check the outcome of "
3837 & "calling &?T?"), Err_Node, Subp_Id);
3839 else
3840 Error_Msg_NE (Adjust_Message
3841 ("postcondition does not check the outcome of calling "
3842 & "&?T?"), Err_Node, Subp_Id);
3843 end if;
3844 end if;
3845 end Check_Conjunct;
3847 ---------------------
3848 -- Check_Conjuncts --
3849 ---------------------
3851 procedure Check_Conjuncts (Expr : Node_Id) is
3852 begin
3853 if Nkind_In (Expr, N_Op_And, N_And_Then) then
3854 Check_Conjuncts (Left_Opnd (Expr));
3855 Check_Conjuncts (Right_Opnd (Expr));
3856 else
3857 Check_Conjunct (Expr);
3858 end if;
3859 end Check_Conjuncts;
3861 ----------------------
3862 -- Check_Expression --
3863 ----------------------
3865 procedure Check_Expression (Expr : Node_Id) is
3866 begin
3867 if not Is_Trivial_Boolean (Expr) then
3868 Check_Function_Result (Expr);
3869 Check_Conjuncts (Expr);
3870 end if;
3871 end Check_Expression;
3873 ------------------------
3874 -- Is_Function_Result --
3875 ------------------------
3877 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3878 begin
3879 if Is_Attribute_Result (N) then
3880 Result_Seen := True;
3881 return Abandon;
3883 -- Warn on infinite recursion if call is to current function
3885 elsif Nkind (N) = N_Function_Call
3886 and then Is_Entity_Name (Name (N))
3887 and then Entity (Name (N)) = Subp_Id
3888 and then not Is_Potentially_Unevaluated (N)
3889 then
3890 Error_Msg_NE
3891 ("call to & within its postcondition will lead to infinite "
3892 & "recursion?", N, Subp_Id);
3893 return OK;
3895 -- Continue the traversal
3897 else
3898 return OK;
3899 end if;
3900 end Is_Function_Result;
3902 ------------------------
3903 -- Is_Trivial_Boolean --
3904 ------------------------
3906 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3907 begin
3908 return
3909 Comes_From_Source (N)
3910 and then Is_Entity_Name (N)
3911 and then (Entity (N) = Standard_True
3912 or else
3913 Entity (N) = Standard_False);
3914 end Is_Trivial_Boolean;
3916 -------------------------
3917 -- Mentions_Post_State --
3918 -------------------------
3920 function Mentions_Post_State (N : Node_Id) return Boolean is
3921 Post_State_Seen : Boolean := False;
3923 function Is_Post_State (N : Node_Id) return Traverse_Result;
3924 -- Attempt to find a construct that denotes a post-state. If this
3925 -- is the case, set flag Post_State_Seen.
3927 -------------------
3928 -- Is_Post_State --
3929 -------------------
3931 function Is_Post_State (N : Node_Id) return Traverse_Result is
3932 Ent : Entity_Id;
3934 begin
3935 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3936 Post_State_Seen := True;
3937 return Abandon;
3939 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3940 Ent := Entity (N);
3942 -- Treat an undecorated reference as OK
3944 if No (Ent)
3946 -- A reference to an assignable entity is considered a
3947 -- change in the post-state of a subprogram.
3949 or else Ekind_In (Ent, E_Generic_In_Out_Parameter,
3950 E_In_Out_Parameter,
3951 E_Out_Parameter,
3952 E_Variable)
3954 -- The reference may be modified through a dereference
3956 or else (Is_Access_Type (Etype (Ent))
3957 and then Nkind (Parent (N)) =
3958 N_Selected_Component)
3959 then
3960 Post_State_Seen := True;
3961 return Abandon;
3962 end if;
3964 elsif Nkind (N) = N_Attribute_Reference then
3965 if Attribute_Name (N) = Name_Old then
3966 return Skip;
3968 elsif Attribute_Name (N) = Name_Result then
3969 Post_State_Seen := True;
3970 return Abandon;
3971 end if;
3972 end if;
3974 return OK;
3975 end Is_Post_State;
3977 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3979 -- Start of processing for Mentions_Post_State
3981 begin
3982 Find_Post_State (N);
3984 return Post_State_Seen;
3985 end Mentions_Post_State;
3987 -- Local variables
3989 Expr : constant Node_Id :=
3990 Get_Pragma_Arg
3991 (First (Pragma_Argument_Associations (Prag)));
3992 Nam : constant Name_Id := Pragma_Name (Prag);
3993 CCase : Node_Id;
3995 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3997 begin
3998 -- Examine all consequences
4000 if Nam = Name_Contract_Cases then
4001 CCase := First (Component_Associations (Expr));
4002 while Present (CCase) loop
4003 Check_Expression (Expression (CCase));
4005 Next (CCase);
4006 end loop;
4008 -- Examine the expression of a postcondition
4010 else pragma Assert (Nam_In (Nam, Name_Postcondition,
4011 Name_Refined_Post));
4012 Check_Expression (Expr);
4013 end if;
4014 end Check_Result_And_Post_State_In_Pragma;
4016 --------------------------
4017 -- Has_In_Out_Parameter --
4018 --------------------------
4020 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
4021 Formal : Entity_Id;
4023 begin
4024 -- Traverse the formals looking for an IN OUT parameter
4026 Formal := First_Formal (Subp_Id);
4027 while Present (Formal) loop
4028 if Ekind (Formal) = E_In_Out_Parameter then
4029 return True;
4030 end if;
4032 Next_Formal (Formal);
4033 end loop;
4035 return False;
4036 end Has_In_Out_Parameter;
4038 -- Local variables
4040 Items : constant Node_Id := Contract (Subp_Id);
4041 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
4042 Case_Prag : Node_Id := Empty;
4043 Post_Prag : Node_Id := Empty;
4044 Prag : Node_Id;
4045 Seen_In_Case : Boolean := False;
4046 Seen_In_Post : Boolean := False;
4047 Spec_Id : Entity_Id;
4049 -- Start of processing for Check_Result_And_Post_State
4051 begin
4052 -- The lack of attribute 'Result or a post-state is classified as a
4053 -- suspicious contract. Do not perform the check if the corresponding
4054 -- swich is not set.
4056 if not Warn_On_Suspicious_Contract then
4057 return;
4059 -- Nothing to do if there is no contract
4061 elsif No (Items) then
4062 return;
4063 end if;
4065 -- Retrieve the entity of the subprogram spec (if any)
4067 if Nkind (Subp_Decl) = N_Subprogram_Body
4068 and then Present (Corresponding_Spec (Subp_Decl))
4069 then
4070 Spec_Id := Corresponding_Spec (Subp_Decl);
4072 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
4073 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
4074 then
4075 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
4077 else
4078 Spec_Id := Subp_Id;
4079 end if;
4081 -- Examine all postconditions for attribute 'Result and a post-state
4083 Prag := Pre_Post_Conditions (Items);
4084 while Present (Prag) loop
4085 if Nam_In (Pragma_Name_Unmapped (Prag),
4086 Name_Postcondition, Name_Refined_Post)
4087 and then not Error_Posted (Prag)
4088 then
4089 Post_Prag := Prag;
4090 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
4091 end if;
4093 Prag := Next_Pragma (Prag);
4094 end loop;
4096 -- Examine the contract cases of the subprogram for attribute 'Result
4097 -- and a post-state.
4099 Prag := Contract_Test_Cases (Items);
4100 while Present (Prag) loop
4101 if Pragma_Name (Prag) = Name_Contract_Cases
4102 and then not Error_Posted (Prag)
4103 then
4104 Case_Prag := Prag;
4105 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
4106 end if;
4108 Prag := Next_Pragma (Prag);
4109 end loop;
4111 -- Do not emit any errors if the subprogram is not a function
4113 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
4114 null;
4116 -- Regardless of whether the function has postconditions or contract
4117 -- cases, or whether they mention attribute 'Result, an IN OUT formal
4118 -- parameter is always treated as a result.
4120 elsif Has_In_Out_Parameter (Spec_Id) then
4121 null;
4123 -- The function has both a postcondition and contract cases and they do
4124 -- not mention attribute 'Result.
4126 elsif Present (Case_Prag)
4127 and then not Seen_In_Case
4128 and then Present (Post_Prag)
4129 and then not Seen_In_Post
4130 then
4131 Error_Msg_N
4132 ("neither postcondition nor contract cases mention function "
4133 & "result?T?", Post_Prag);
4135 -- The function has contract cases only and they do not mention
4136 -- attribute 'Result.
4138 elsif Present (Case_Prag) and then not Seen_In_Case then
4139 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
4141 -- The function has postconditions only and they do not mention
4142 -- attribute 'Result.
4144 elsif Present (Post_Prag) and then not Seen_In_Post then
4145 Error_Msg_N
4146 ("postcondition does not mention function result?T?", Post_Prag);
4147 end if;
4148 end Check_Result_And_Post_State;
4150 -----------------------------
4151 -- Check_State_Refinements --
4152 -----------------------------
4154 procedure Check_State_Refinements
4155 (Context : Node_Id;
4156 Is_Main_Unit : Boolean := False)
4158 procedure Check_Package (Pack : Node_Id);
4159 -- Verify that all abstract states of a [generic] package denoted by its
4160 -- declarative node Pack have proper refinement. Recursively verify the
4161 -- visible and private declarations of the [generic] package for other
4162 -- nested packages.
4164 procedure Check_Packages_In (Decls : List_Id);
4165 -- Seek out [generic] package declarations within declarative list Decls
4166 -- and verify the status of their abstract state refinement.
4168 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean;
4169 -- Determine whether construct N is subject to pragma SPARK_Mode Off
4171 -------------------
4172 -- Check_Package --
4173 -------------------
4175 procedure Check_Package (Pack : Node_Id) is
4176 Body_Id : constant Entity_Id := Corresponding_Body (Pack);
4177 Spec : constant Node_Id := Specification (Pack);
4178 States : constant Elist_Id :=
4179 Abstract_States (Defining_Entity (Pack));
4181 State_Elmt : Elmt_Id;
4182 State_Id : Entity_Id;
4184 begin
4185 -- Do not verify proper state refinement when the package is subject
4186 -- to pragma SPARK_Mode Off because this disables the requirement for
4187 -- state refinement.
4189 if SPARK_Mode_Is_Off (Pack) then
4190 null;
4192 -- State refinement can only occur in a completing package body. Do
4193 -- not verify proper state refinement when the body is subject to
4194 -- pragma SPARK_Mode Off because this disables the requirement for
4195 -- state refinement.
4197 elsif Present (Body_Id)
4198 and then SPARK_Mode_Is_Off (Unit_Declaration_Node (Body_Id))
4199 then
4200 null;
4202 -- Do not verify proper state refinement when the package is an
4203 -- instance as this check was already performed in the generic.
4205 elsif Present (Generic_Parent (Spec)) then
4206 null;
4208 -- Otherwise examine the contents of the package
4210 else
4211 if Present (States) then
4212 State_Elmt := First_Elmt (States);
4213 while Present (State_Elmt) loop
4214 State_Id := Node (State_Elmt);
4216 -- Emit an error when a non-null state lacks any form of
4217 -- refinement.
4219 if not Is_Null_State (State_Id)
4220 and then not Has_Null_Refinement (State_Id)
4221 and then not Has_Non_Null_Refinement (State_Id)
4222 then
4223 Error_Msg_N ("state & requires refinement", State_Id);
4224 end if;
4226 Next_Elmt (State_Elmt);
4227 end loop;
4228 end if;
4230 Check_Packages_In (Visible_Declarations (Spec));
4231 Check_Packages_In (Private_Declarations (Spec));
4232 end if;
4233 end Check_Package;
4235 -----------------------
4236 -- Check_Packages_In --
4237 -----------------------
4239 procedure Check_Packages_In (Decls : List_Id) is
4240 Decl : Node_Id;
4242 begin
4243 if Present (Decls) then
4244 Decl := First (Decls);
4245 while Present (Decl) loop
4246 if Nkind_In (Decl, N_Generic_Package_Declaration,
4247 N_Package_Declaration)
4248 then
4249 Check_Package (Decl);
4250 end if;
4252 Next (Decl);
4253 end loop;
4254 end if;
4255 end Check_Packages_In;
4257 -----------------------
4258 -- SPARK_Mode_Is_Off --
4259 -----------------------
4261 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean is
4262 Id : constant Entity_Id := Defining_Entity (N);
4263 Prag : constant Node_Id := SPARK_Pragma (Id);
4265 begin
4266 -- Default the mode to "off" when the context is an instance and all
4267 -- SPARK_Mode pragmas found within are to be ignored.
4269 if Ignore_SPARK_Mode_Pragmas (Id) then
4270 return True;
4272 else
4273 return
4274 Present (Prag)
4275 and then Get_SPARK_Mode_From_Annotation (Prag) = Off;
4276 end if;
4277 end SPARK_Mode_Is_Off;
4279 -- Start of processing for Check_State_Refinements
4281 begin
4282 -- A block may declare a nested package
4284 if Nkind (Context) = N_Block_Statement then
4285 Check_Packages_In (Declarations (Context));
4287 -- An entry, protected, subprogram, or task body may declare a nested
4288 -- package.
4290 elsif Nkind_In (Context, N_Entry_Body,
4291 N_Protected_Body,
4292 N_Subprogram_Body,
4293 N_Task_Body)
4294 then
4295 -- Do not verify proper state refinement when the body is subject to
4296 -- pragma SPARK_Mode Off because this disables the requirement for
4297 -- state refinement.
4299 if not SPARK_Mode_Is_Off (Context) then
4300 Check_Packages_In (Declarations (Context));
4301 end if;
4303 -- A package body may declare a nested package
4305 elsif Nkind (Context) = N_Package_Body then
4306 Check_Package (Unit_Declaration_Node (Corresponding_Spec (Context)));
4308 -- Do not verify proper state refinement when the body is subject to
4309 -- pragma SPARK_Mode Off because this disables the requirement for
4310 -- state refinement.
4312 if not SPARK_Mode_Is_Off (Context) then
4313 Check_Packages_In (Declarations (Context));
4314 end if;
4316 -- A library level [generic] package may declare a nested package
4318 elsif Nkind_In (Context, N_Generic_Package_Declaration,
4319 N_Package_Declaration)
4320 and then Is_Main_Unit
4321 then
4322 Check_Package (Context);
4323 end if;
4324 end Check_State_Refinements;
4326 ------------------------------
4327 -- Check_Unprotected_Access --
4328 ------------------------------
4330 procedure Check_Unprotected_Access
4331 (Context : Node_Id;
4332 Expr : Node_Id)
4334 Cont_Encl_Typ : Entity_Id;
4335 Pref_Encl_Typ : Entity_Id;
4337 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
4338 -- Check whether Obj is a private component of a protected object.
4339 -- Return the protected type where the component resides, Empty
4340 -- otherwise.
4342 function Is_Public_Operation return Boolean;
4343 -- Verify that the enclosing operation is callable from outside the
4344 -- protected object, to minimize false positives.
4346 ------------------------------
4347 -- Enclosing_Protected_Type --
4348 ------------------------------
4350 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
4351 begin
4352 if Is_Entity_Name (Obj) then
4353 declare
4354 Ent : Entity_Id := Entity (Obj);
4356 begin
4357 -- The object can be a renaming of a private component, use
4358 -- the original record component.
4360 if Is_Prival (Ent) then
4361 Ent := Prival_Link (Ent);
4362 end if;
4364 if Is_Protected_Type (Scope (Ent)) then
4365 return Scope (Ent);
4366 end if;
4367 end;
4368 end if;
4370 -- For indexed and selected components, recursively check the prefix
4372 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
4373 return Enclosing_Protected_Type (Prefix (Obj));
4375 -- The object does not denote a protected component
4377 else
4378 return Empty;
4379 end if;
4380 end Enclosing_Protected_Type;
4382 -------------------------
4383 -- Is_Public_Operation --
4384 -------------------------
4386 function Is_Public_Operation return Boolean is
4387 S : Entity_Id;
4388 E : Entity_Id;
4390 begin
4391 S := Current_Scope;
4392 while Present (S) and then S /= Pref_Encl_Typ loop
4393 if Scope (S) = Pref_Encl_Typ then
4394 E := First_Entity (Pref_Encl_Typ);
4395 while Present (E)
4396 and then E /= First_Private_Entity (Pref_Encl_Typ)
4397 loop
4398 if E = S then
4399 return True;
4400 end if;
4402 Next_Entity (E);
4403 end loop;
4404 end if;
4406 S := Scope (S);
4407 end loop;
4409 return False;
4410 end Is_Public_Operation;
4412 -- Start of processing for Check_Unprotected_Access
4414 begin
4415 if Nkind (Expr) = N_Attribute_Reference
4416 and then Attribute_Name (Expr) = Name_Unchecked_Access
4417 then
4418 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
4419 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
4421 -- Check whether we are trying to export a protected component to a
4422 -- context with an equal or lower access level.
4424 if Present (Pref_Encl_Typ)
4425 and then No (Cont_Encl_Typ)
4426 and then Is_Public_Operation
4427 and then Scope_Depth (Pref_Encl_Typ) >=
4428 Object_Access_Level (Context)
4429 then
4430 Error_Msg_N
4431 ("??possible unprotected access to protected data", Expr);
4432 end if;
4433 end if;
4434 end Check_Unprotected_Access;
4436 ------------------------------
4437 -- Check_Unused_Body_States --
4438 ------------------------------
4440 procedure Check_Unused_Body_States (Body_Id : Entity_Id) is
4441 procedure Process_Refinement_Clause
4442 (Clause : Node_Id;
4443 States : Elist_Id);
4444 -- Inspect all constituents of refinement clause Clause and remove any
4445 -- matches from body state list States.
4447 procedure Report_Unused_Body_States (States : Elist_Id);
4448 -- Emit errors for each abstract state or object found in list States
4450 -------------------------------
4451 -- Process_Refinement_Clause --
4452 -------------------------------
4454 procedure Process_Refinement_Clause
4455 (Clause : Node_Id;
4456 States : Elist_Id)
4458 procedure Process_Constituent (Constit : Node_Id);
4459 -- Remove constituent Constit from body state list States
4461 -------------------------
4462 -- Process_Constituent --
4463 -------------------------
4465 procedure Process_Constituent (Constit : Node_Id) is
4466 Constit_Id : Entity_Id;
4468 begin
4469 -- Guard against illegal constituents. Only abstract states and
4470 -- objects can appear on the right hand side of a refinement.
4472 if Is_Entity_Name (Constit) then
4473 Constit_Id := Entity_Of (Constit);
4475 if Present (Constit_Id)
4476 and then Ekind_In (Constit_Id, E_Abstract_State,
4477 E_Constant,
4478 E_Variable)
4479 then
4480 Remove (States, Constit_Id);
4481 end if;
4482 end if;
4483 end Process_Constituent;
4485 -- Local variables
4487 Constit : Node_Id;
4489 -- Start of processing for Process_Refinement_Clause
4491 begin
4492 if Nkind (Clause) = N_Component_Association then
4493 Constit := Expression (Clause);
4495 -- Multiple constituents appear as an aggregate
4497 if Nkind (Constit) = N_Aggregate then
4498 Constit := First (Expressions (Constit));
4499 while Present (Constit) loop
4500 Process_Constituent (Constit);
4501 Next (Constit);
4502 end loop;
4504 -- Various forms of a single constituent
4506 else
4507 Process_Constituent (Constit);
4508 end if;
4509 end if;
4510 end Process_Refinement_Clause;
4512 -------------------------------
4513 -- Report_Unused_Body_States --
4514 -------------------------------
4516 procedure Report_Unused_Body_States (States : Elist_Id) is
4517 Posted : Boolean := False;
4518 State_Elmt : Elmt_Id;
4519 State_Id : Entity_Id;
4521 begin
4522 if Present (States) then
4523 State_Elmt := First_Elmt (States);
4524 while Present (State_Elmt) loop
4525 State_Id := Node (State_Elmt);
4527 -- Constants are part of the hidden state of a package, but the
4528 -- compiler cannot determine whether they have variable input
4529 -- (SPARK RM 7.1.1(2)) and cannot classify them properly as a
4530 -- hidden state. Do not emit an error when a constant does not
4531 -- participate in a state refinement, even though it acts as a
4532 -- hidden state.
4534 if Ekind (State_Id) = E_Constant then
4535 null;
4537 -- Generate an error message of the form:
4539 -- body of package ... has unused hidden states
4540 -- abstract state ... defined at ...
4541 -- variable ... defined at ...
4543 else
4544 if not Posted then
4545 Posted := True;
4546 SPARK_Msg_N
4547 ("body of package & has unused hidden states", Body_Id);
4548 end if;
4550 Error_Msg_Sloc := Sloc (State_Id);
4552 if Ekind (State_Id) = E_Abstract_State then
4553 SPARK_Msg_NE
4554 ("\abstract state & defined #", Body_Id, State_Id);
4556 else
4557 SPARK_Msg_NE ("\variable & defined #", Body_Id, State_Id);
4558 end if;
4559 end if;
4561 Next_Elmt (State_Elmt);
4562 end loop;
4563 end if;
4564 end Report_Unused_Body_States;
4566 -- Local variables
4568 Prag : constant Node_Id := Get_Pragma (Body_Id, Pragma_Refined_State);
4569 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
4570 Clause : Node_Id;
4571 States : Elist_Id;
4573 -- Start of processing for Check_Unused_Body_States
4575 begin
4576 -- Inspect the clauses of pragma Refined_State and determine whether all
4577 -- visible states declared within the package body participate in the
4578 -- refinement.
4580 if Present (Prag) then
4581 Clause := Expression (Get_Argument (Prag, Spec_Id));
4582 States := Collect_Body_States (Body_Id);
4584 -- Multiple non-null state refinements appear as an aggregate
4586 if Nkind (Clause) = N_Aggregate then
4587 Clause := First (Component_Associations (Clause));
4588 while Present (Clause) loop
4589 Process_Refinement_Clause (Clause, States);
4590 Next (Clause);
4591 end loop;
4593 -- Various forms of a single state refinement
4595 else
4596 Process_Refinement_Clause (Clause, States);
4597 end if;
4599 -- Ensure that all abstract states and objects declared in the
4600 -- package body state space are utilized as constituents.
4602 Report_Unused_Body_States (States);
4603 end if;
4604 end Check_Unused_Body_States;
4606 -----------------
4607 -- Choice_List --
4608 -----------------
4610 function Choice_List (N : Node_Id) return List_Id is
4611 begin
4612 if Nkind (N) = N_Iterated_Component_Association then
4613 return Discrete_Choices (N);
4614 else
4615 return Choices (N);
4616 end if;
4617 end Choice_List;
4619 -------------------------
4620 -- Collect_Body_States --
4621 -------------------------
4623 function Collect_Body_States (Body_Id : Entity_Id) return Elist_Id is
4624 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean;
4625 -- Determine whether object Obj_Id is a suitable visible state of a
4626 -- package body.
4628 procedure Collect_Visible_States
4629 (Pack_Id : Entity_Id;
4630 States : in out Elist_Id);
4631 -- Gather the entities of all abstract states and objects declared in
4632 -- the visible state space of package Pack_Id.
4634 ----------------------------
4635 -- Collect_Visible_States --
4636 ----------------------------
4638 procedure Collect_Visible_States
4639 (Pack_Id : Entity_Id;
4640 States : in out Elist_Id)
4642 Item_Id : Entity_Id;
4644 begin
4645 -- Traverse the entity chain of the package and inspect all visible
4646 -- items.
4648 Item_Id := First_Entity (Pack_Id);
4649 while Present (Item_Id) and then not In_Private_Part (Item_Id) loop
4651 -- Do not consider internally generated items as those cannot be
4652 -- named and participate in refinement.
4654 if not Comes_From_Source (Item_Id) then
4655 null;
4657 elsif Ekind (Item_Id) = E_Abstract_State then
4658 Append_New_Elmt (Item_Id, States);
4660 elsif Ekind_In (Item_Id, E_Constant, E_Variable)
4661 and then Is_Visible_Object (Item_Id)
4662 then
4663 Append_New_Elmt (Item_Id, States);
4665 -- Recursively gather the visible states of a nested package
4667 elsif Ekind (Item_Id) = E_Package then
4668 Collect_Visible_States (Item_Id, States);
4669 end if;
4671 Next_Entity (Item_Id);
4672 end loop;
4673 end Collect_Visible_States;
4675 -----------------------
4676 -- Is_Visible_Object --
4677 -----------------------
4679 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean is
4680 begin
4681 -- Objects that map generic formals to their actuals are not visible
4682 -- from outside the generic instantiation.
4684 if Present (Corresponding_Generic_Association
4685 (Declaration_Node (Obj_Id)))
4686 then
4687 return False;
4689 -- Constituents of a single protected/task type act as components of
4690 -- the type and are not visible from outside the type.
4692 elsif Ekind (Obj_Id) = E_Variable
4693 and then Present (Encapsulating_State (Obj_Id))
4694 and then Is_Single_Concurrent_Object (Encapsulating_State (Obj_Id))
4695 then
4696 return False;
4698 else
4699 return True;
4700 end if;
4701 end Is_Visible_Object;
4703 -- Local variables
4705 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
4706 Decl : Node_Id;
4707 Item_Id : Entity_Id;
4708 States : Elist_Id := No_Elist;
4710 -- Start of processing for Collect_Body_States
4712 begin
4713 -- Inspect the declarations of the body looking for source objects,
4714 -- packages and package instantiations. Note that even though this
4715 -- processing is very similar to Collect_Visible_States, a package
4716 -- body does not have a First/Next_Entity list.
4718 Decl := First (Declarations (Body_Decl));
4719 while Present (Decl) loop
4721 -- Capture source objects as internally generated temporaries cannot
4722 -- be named and participate in refinement.
4724 if Nkind (Decl) = N_Object_Declaration then
4725 Item_Id := Defining_Entity (Decl);
4727 if Comes_From_Source (Item_Id)
4728 and then Is_Visible_Object (Item_Id)
4729 then
4730 Append_New_Elmt (Item_Id, States);
4731 end if;
4733 -- Capture the visible abstract states and objects of a source
4734 -- package [instantiation].
4736 elsif Nkind (Decl) = N_Package_Declaration then
4737 Item_Id := Defining_Entity (Decl);
4739 if Comes_From_Source (Item_Id) then
4740 Collect_Visible_States (Item_Id, States);
4741 end if;
4742 end if;
4744 Next (Decl);
4745 end loop;
4747 return States;
4748 end Collect_Body_States;
4750 ------------------------
4751 -- Collect_Interfaces --
4752 ------------------------
4754 procedure Collect_Interfaces
4755 (T : Entity_Id;
4756 Ifaces_List : out Elist_Id;
4757 Exclude_Parents : Boolean := False;
4758 Use_Full_View : Boolean := True)
4760 procedure Collect (Typ : Entity_Id);
4761 -- Subsidiary subprogram used to traverse the whole list
4762 -- of directly and indirectly implemented interfaces
4764 -------------
4765 -- Collect --
4766 -------------
4768 procedure Collect (Typ : Entity_Id) is
4769 Ancestor : Entity_Id;
4770 Full_T : Entity_Id;
4771 Id : Node_Id;
4772 Iface : Entity_Id;
4774 begin
4775 Full_T := Typ;
4777 -- Handle private types and subtypes
4779 if Use_Full_View
4780 and then Is_Private_Type (Typ)
4781 and then Present (Full_View (Typ))
4782 then
4783 Full_T := Full_View (Typ);
4785 if Ekind (Full_T) = E_Record_Subtype then
4786 Full_T := Etype (Typ);
4788 if Present (Full_View (Full_T)) then
4789 Full_T := Full_View (Full_T);
4790 end if;
4791 end if;
4792 end if;
4794 -- Include the ancestor if we are generating the whole list of
4795 -- abstract interfaces.
4797 if Etype (Full_T) /= Typ
4799 -- Protect the frontend against wrong sources. For example:
4801 -- package P is
4802 -- type A is tagged null record;
4803 -- type B is new A with private;
4804 -- type C is new A with private;
4805 -- private
4806 -- type B is new C with null record;
4807 -- type C is new B with null record;
4808 -- end P;
4810 and then Etype (Full_T) /= T
4811 then
4812 Ancestor := Etype (Full_T);
4813 Collect (Ancestor);
4815 if Is_Interface (Ancestor) and then not Exclude_Parents then
4816 Append_Unique_Elmt (Ancestor, Ifaces_List);
4817 end if;
4818 end if;
4820 -- Traverse the graph of ancestor interfaces
4822 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
4823 Id := First (Abstract_Interface_List (Full_T));
4824 while Present (Id) loop
4825 Iface := Etype (Id);
4827 -- Protect against wrong uses. For example:
4828 -- type I is interface;
4829 -- type O is tagged null record;
4830 -- type Wrong is new I and O with null record; -- ERROR
4832 if Is_Interface (Iface) then
4833 if Exclude_Parents
4834 and then Etype (T) /= T
4835 and then Interface_Present_In_Ancestor (Etype (T), Iface)
4836 then
4837 null;
4838 else
4839 Collect (Iface);
4840 Append_Unique_Elmt (Iface, Ifaces_List);
4841 end if;
4842 end if;
4844 Next (Id);
4845 end loop;
4846 end if;
4847 end Collect;
4849 -- Start of processing for Collect_Interfaces
4851 begin
4852 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
4853 Ifaces_List := New_Elmt_List;
4854 Collect (T);
4855 end Collect_Interfaces;
4857 ----------------------------------
4858 -- Collect_Interface_Components --
4859 ----------------------------------
4861 procedure Collect_Interface_Components
4862 (Tagged_Type : Entity_Id;
4863 Components_List : out Elist_Id)
4865 procedure Collect (Typ : Entity_Id);
4866 -- Subsidiary subprogram used to climb to the parents
4868 -------------
4869 -- Collect --
4870 -------------
4872 procedure Collect (Typ : Entity_Id) is
4873 Tag_Comp : Entity_Id;
4874 Parent_Typ : Entity_Id;
4876 begin
4877 -- Handle private types
4879 if Present (Full_View (Etype (Typ))) then
4880 Parent_Typ := Full_View (Etype (Typ));
4881 else
4882 Parent_Typ := Etype (Typ);
4883 end if;
4885 if Parent_Typ /= Typ
4887 -- Protect the frontend against wrong sources. For example:
4889 -- package P is
4890 -- type A is tagged null record;
4891 -- type B is new A with private;
4892 -- type C is new A with private;
4893 -- private
4894 -- type B is new C with null record;
4895 -- type C is new B with null record;
4896 -- end P;
4898 and then Parent_Typ /= Tagged_Type
4899 then
4900 Collect (Parent_Typ);
4901 end if;
4903 -- Collect the components containing tags of secondary dispatch
4904 -- tables.
4906 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
4907 while Present (Tag_Comp) loop
4908 pragma Assert (Present (Related_Type (Tag_Comp)));
4909 Append_Elmt (Tag_Comp, Components_List);
4911 Tag_Comp := Next_Tag_Component (Tag_Comp);
4912 end loop;
4913 end Collect;
4915 -- Start of processing for Collect_Interface_Components
4917 begin
4918 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
4919 and then Is_Tagged_Type (Tagged_Type));
4921 Components_List := New_Elmt_List;
4922 Collect (Tagged_Type);
4923 end Collect_Interface_Components;
4925 -----------------------------
4926 -- Collect_Interfaces_Info --
4927 -----------------------------
4929 procedure Collect_Interfaces_Info
4930 (T : Entity_Id;
4931 Ifaces_List : out Elist_Id;
4932 Components_List : out Elist_Id;
4933 Tags_List : out Elist_Id)
4935 Comps_List : Elist_Id;
4936 Comp_Elmt : Elmt_Id;
4937 Comp_Iface : Entity_Id;
4938 Iface_Elmt : Elmt_Id;
4939 Iface : Entity_Id;
4941 function Search_Tag (Iface : Entity_Id) return Entity_Id;
4942 -- Search for the secondary tag associated with the interface type
4943 -- Iface that is implemented by T.
4945 ----------------
4946 -- Search_Tag --
4947 ----------------
4949 function Search_Tag (Iface : Entity_Id) return Entity_Id is
4950 ADT : Elmt_Id;
4951 begin
4952 if not Is_CPP_Class (T) then
4953 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
4954 else
4955 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
4956 end if;
4958 while Present (ADT)
4959 and then Is_Tag (Node (ADT))
4960 and then Related_Type (Node (ADT)) /= Iface
4961 loop
4962 -- Skip secondary dispatch table referencing thunks to user
4963 -- defined primitives covered by this interface.
4965 pragma Assert (Has_Suffix (Node (ADT), 'P'));
4966 Next_Elmt (ADT);
4968 -- Skip secondary dispatch tables of Ada types
4970 if not Is_CPP_Class (T) then
4972 -- Skip secondary dispatch table referencing thunks to
4973 -- predefined primitives.
4975 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
4976 Next_Elmt (ADT);
4978 -- Skip secondary dispatch table referencing user-defined
4979 -- primitives covered by this interface.
4981 pragma Assert (Has_Suffix (Node (ADT), 'D'));
4982 Next_Elmt (ADT);
4984 -- Skip secondary dispatch table referencing predefined
4985 -- primitives.
4987 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
4988 Next_Elmt (ADT);
4989 end if;
4990 end loop;
4992 pragma Assert (Is_Tag (Node (ADT)));
4993 return Node (ADT);
4994 end Search_Tag;
4996 -- Start of processing for Collect_Interfaces_Info
4998 begin
4999 Collect_Interfaces (T, Ifaces_List);
5000 Collect_Interface_Components (T, Comps_List);
5002 -- Search for the record component and tag associated with each
5003 -- interface type of T.
5005 Components_List := New_Elmt_List;
5006 Tags_List := New_Elmt_List;
5008 Iface_Elmt := First_Elmt (Ifaces_List);
5009 while Present (Iface_Elmt) loop
5010 Iface := Node (Iface_Elmt);
5012 -- Associate the primary tag component and the primary dispatch table
5013 -- with all the interfaces that are parents of T
5015 if Is_Ancestor (Iface, T, Use_Full_View => True) then
5016 Append_Elmt (First_Tag_Component (T), Components_List);
5017 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
5019 -- Otherwise search for the tag component and secondary dispatch
5020 -- table of Iface
5022 else
5023 Comp_Elmt := First_Elmt (Comps_List);
5024 while Present (Comp_Elmt) loop
5025 Comp_Iface := Related_Type (Node (Comp_Elmt));
5027 if Comp_Iface = Iface
5028 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
5029 then
5030 Append_Elmt (Node (Comp_Elmt), Components_List);
5031 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
5032 exit;
5033 end if;
5035 Next_Elmt (Comp_Elmt);
5036 end loop;
5037 pragma Assert (Present (Comp_Elmt));
5038 end if;
5040 Next_Elmt (Iface_Elmt);
5041 end loop;
5042 end Collect_Interfaces_Info;
5044 ---------------------
5045 -- Collect_Parents --
5046 ---------------------
5048 procedure Collect_Parents
5049 (T : Entity_Id;
5050 List : out Elist_Id;
5051 Use_Full_View : Boolean := True)
5053 Current_Typ : Entity_Id := T;
5054 Parent_Typ : Entity_Id;
5056 begin
5057 List := New_Elmt_List;
5059 -- No action if the if the type has no parents
5061 if T = Etype (T) then
5062 return;
5063 end if;
5065 loop
5066 Parent_Typ := Etype (Current_Typ);
5068 if Is_Private_Type (Parent_Typ)
5069 and then Present (Full_View (Parent_Typ))
5070 and then Use_Full_View
5071 then
5072 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5073 end if;
5075 Append_Elmt (Parent_Typ, List);
5077 exit when Parent_Typ = Current_Typ;
5078 Current_Typ := Parent_Typ;
5079 end loop;
5080 end Collect_Parents;
5082 ----------------------------------
5083 -- Collect_Primitive_Operations --
5084 ----------------------------------
5086 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
5087 B_Type : constant Entity_Id := Base_Type (T);
5088 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
5089 B_Scope : Entity_Id := Scope (B_Type);
5090 Op_List : Elist_Id;
5091 Formal : Entity_Id;
5092 Is_Prim : Boolean;
5093 Is_Type_In_Pkg : Boolean;
5094 Formal_Derived : Boolean := False;
5095 Id : Entity_Id;
5097 function Match (E : Entity_Id) return Boolean;
5098 -- True if E's base type is B_Type, or E is of an anonymous access type
5099 -- and the base type of its designated type is B_Type.
5101 -----------
5102 -- Match --
5103 -----------
5105 function Match (E : Entity_Id) return Boolean is
5106 Etyp : Entity_Id := Etype (E);
5108 begin
5109 if Ekind (Etyp) = E_Anonymous_Access_Type then
5110 Etyp := Designated_Type (Etyp);
5111 end if;
5113 -- In Ada 2012 a primitive operation may have a formal of an
5114 -- incomplete view of the parent type.
5116 return Base_Type (Etyp) = B_Type
5117 or else
5118 (Ada_Version >= Ada_2012
5119 and then Ekind (Etyp) = E_Incomplete_Type
5120 and then Full_View (Etyp) = B_Type);
5121 end Match;
5123 -- Start of processing for Collect_Primitive_Operations
5125 begin
5126 -- For tagged types, the primitive operations are collected as they
5127 -- are declared, and held in an explicit list which is simply returned.
5129 if Is_Tagged_Type (B_Type) then
5130 return Primitive_Operations (B_Type);
5132 -- An untagged generic type that is a derived type inherits the
5133 -- primitive operations of its parent type. Other formal types only
5134 -- have predefined operators, which are not explicitly represented.
5136 elsif Is_Generic_Type (B_Type) then
5137 if Nkind (B_Decl) = N_Formal_Type_Declaration
5138 and then Nkind (Formal_Type_Definition (B_Decl)) =
5139 N_Formal_Derived_Type_Definition
5140 then
5141 Formal_Derived := True;
5142 else
5143 return New_Elmt_List;
5144 end if;
5145 end if;
5147 Op_List := New_Elmt_List;
5149 if B_Scope = Standard_Standard then
5150 if B_Type = Standard_String then
5151 Append_Elmt (Standard_Op_Concat, Op_List);
5153 elsif B_Type = Standard_Wide_String then
5154 Append_Elmt (Standard_Op_Concatw, Op_List);
5156 else
5157 null;
5158 end if;
5160 -- Locate the primitive subprograms of the type
5162 else
5163 -- The primitive operations appear after the base type, except
5164 -- if the derivation happens within the private part of B_Scope
5165 -- and the type is a private type, in which case both the type
5166 -- and some primitive operations may appear before the base
5167 -- type, and the list of candidates starts after the type.
5169 if In_Open_Scopes (B_Scope)
5170 and then Scope (T) = B_Scope
5171 and then In_Private_Part (B_Scope)
5172 then
5173 Id := Next_Entity (T);
5175 -- In Ada 2012, If the type has an incomplete partial view, there
5176 -- may be primitive operations declared before the full view, so
5177 -- we need to start scanning from the incomplete view, which is
5178 -- earlier on the entity chain.
5180 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
5181 and then Present (Incomplete_View (Parent (B_Type)))
5182 then
5183 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
5185 -- If T is a derived from a type with an incomplete view declared
5186 -- elsewhere, that incomplete view is irrelevant, we want the
5187 -- operations in the scope of T.
5189 if Scope (Id) /= Scope (B_Type) then
5190 Id := Next_Entity (B_Type);
5191 end if;
5193 else
5194 Id := Next_Entity (B_Type);
5195 end if;
5197 -- Set flag if this is a type in a package spec
5199 Is_Type_In_Pkg :=
5200 Is_Package_Or_Generic_Package (B_Scope)
5201 and then
5202 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
5203 N_Package_Body;
5205 while Present (Id) loop
5207 -- Test whether the result type or any of the parameter types of
5208 -- each subprogram following the type match that type when the
5209 -- type is declared in a package spec, is a derived type, or the
5210 -- subprogram is marked as primitive. (The Is_Primitive test is
5211 -- needed to find primitives of nonderived types in declarative
5212 -- parts that happen to override the predefined "=" operator.)
5214 -- Note that generic formal subprograms are not considered to be
5215 -- primitive operations and thus are never inherited.
5217 if Is_Overloadable (Id)
5218 and then (Is_Type_In_Pkg
5219 or else Is_Derived_Type (B_Type)
5220 or else Is_Primitive (Id))
5221 and then Nkind (Parent (Parent (Id)))
5222 not in N_Formal_Subprogram_Declaration
5223 then
5224 Is_Prim := False;
5226 if Match (Id) then
5227 Is_Prim := True;
5229 else
5230 Formal := First_Formal (Id);
5231 while Present (Formal) loop
5232 if Match (Formal) then
5233 Is_Prim := True;
5234 exit;
5235 end if;
5237 Next_Formal (Formal);
5238 end loop;
5239 end if;
5241 -- For a formal derived type, the only primitives are the ones
5242 -- inherited from the parent type. Operations appearing in the
5243 -- package declaration are not primitive for it.
5245 if Is_Prim
5246 and then (not Formal_Derived or else Present (Alias (Id)))
5247 then
5248 -- In the special case of an equality operator aliased to
5249 -- an overriding dispatching equality belonging to the same
5250 -- type, we don't include it in the list of primitives.
5251 -- This avoids inheriting multiple equality operators when
5252 -- deriving from untagged private types whose full type is
5253 -- tagged, which can otherwise cause ambiguities. Note that
5254 -- this should only happen for this kind of untagged parent
5255 -- type, since normally dispatching operations are inherited
5256 -- using the type's Primitive_Operations list.
5258 if Chars (Id) = Name_Op_Eq
5259 and then Is_Dispatching_Operation (Id)
5260 and then Present (Alias (Id))
5261 and then Present (Overridden_Operation (Alias (Id)))
5262 and then Base_Type (Etype (First_Entity (Id))) =
5263 Base_Type (Etype (First_Entity (Alias (Id))))
5264 then
5265 null;
5267 -- Include the subprogram in the list of primitives
5269 else
5270 Append_Elmt (Id, Op_List);
5271 end if;
5272 end if;
5273 end if;
5275 Next_Entity (Id);
5277 -- For a type declared in System, some of its operations may
5278 -- appear in the target-specific extension to System.
5280 if No (Id)
5281 and then B_Scope = RTU_Entity (System)
5282 and then Present_System_Aux
5283 then
5284 B_Scope := System_Aux_Id;
5285 Id := First_Entity (System_Aux_Id);
5286 end if;
5287 end loop;
5288 end if;
5290 return Op_List;
5291 end Collect_Primitive_Operations;
5293 -----------------------------------
5294 -- Compile_Time_Constraint_Error --
5295 -----------------------------------
5297 function Compile_Time_Constraint_Error
5298 (N : Node_Id;
5299 Msg : String;
5300 Ent : Entity_Id := Empty;
5301 Loc : Source_Ptr := No_Location;
5302 Warn : Boolean := False) return Node_Id
5304 Msgc : String (1 .. Msg'Length + 3);
5305 -- Copy of message, with room for possible ?? or << and ! at end
5307 Msgl : Natural;
5308 Wmsg : Boolean;
5309 Eloc : Source_Ptr;
5311 -- Start of processing for Compile_Time_Constraint_Error
5313 begin
5314 -- If this is a warning, convert it into an error if we are in code
5315 -- subject to SPARK_Mode being set On, unless Warn is True to force a
5316 -- warning. The rationale is that a compile-time constraint error should
5317 -- lead to an error instead of a warning when SPARK_Mode is On, but in
5318 -- a few cases we prefer to issue a warning and generate both a suitable
5319 -- run-time error in GNAT and a suitable check message in GNATprove.
5320 -- Those cases are those that likely correspond to deactivated SPARK
5321 -- code, so that this kind of code can be compiled and analyzed instead
5322 -- of being rejected.
5324 Error_Msg_Warn := Warn or SPARK_Mode /= On;
5326 -- A static constraint error in an instance body is not a fatal error.
5327 -- we choose to inhibit the message altogether, because there is no
5328 -- obvious node (for now) on which to post it. On the other hand the
5329 -- offending node must be replaced with a constraint_error in any case.
5331 -- No messages are generated if we already posted an error on this node
5333 if not Error_Posted (N) then
5334 if Loc /= No_Location then
5335 Eloc := Loc;
5336 else
5337 Eloc := Sloc (N);
5338 end if;
5340 -- Copy message to Msgc, converting any ? in the message into <
5341 -- instead, so that we have an error in GNATprove mode.
5343 Msgl := Msg'Length;
5345 for J in 1 .. Msgl loop
5346 if Msg (J) = '?' and then (J = 1 or else Msg (J - 1) /= ''') then
5347 Msgc (J) := '<';
5348 else
5349 Msgc (J) := Msg (J);
5350 end if;
5351 end loop;
5353 -- Message is a warning, even in Ada 95 case
5355 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
5356 Wmsg := True;
5358 -- In Ada 83, all messages are warnings. In the private part and the
5359 -- body of an instance, constraint_checks are only warnings. We also
5360 -- make this a warning if the Warn parameter is set.
5362 elsif Warn
5363 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
5364 or else In_Instance_Not_Visible
5365 then
5366 Msgl := Msgl + 1;
5367 Msgc (Msgl) := '<';
5368 Msgl := Msgl + 1;
5369 Msgc (Msgl) := '<';
5370 Wmsg := True;
5372 -- Otherwise we have a real error message (Ada 95 static case) and we
5373 -- make this an unconditional message. Note that in the warning case
5374 -- we do not make the message unconditional, it seems reasonable to
5375 -- delete messages like this (about exceptions that will be raised)
5376 -- in dead code.
5378 else
5379 Wmsg := False;
5380 Msgl := Msgl + 1;
5381 Msgc (Msgl) := '!';
5382 end if;
5384 -- One more test, skip the warning if the related expression is
5385 -- statically unevaluated, since we don't want to warn about what
5386 -- will happen when something is evaluated if it never will be
5387 -- evaluated.
5389 if not Is_Statically_Unevaluated (N) then
5390 if Present (Ent) then
5391 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
5392 else
5393 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
5394 end if;
5396 if Wmsg then
5398 -- Check whether the context is an Init_Proc
5400 if Inside_Init_Proc then
5401 declare
5402 Conc_Typ : constant Entity_Id :=
5403 Corresponding_Concurrent_Type
5404 (Entity (Parameter_Type (First
5405 (Parameter_Specifications
5406 (Parent (Current_Scope))))));
5408 begin
5409 -- Don't complain if the corresponding concurrent type
5410 -- doesn't come from source (i.e. a single task/protected
5411 -- object).
5413 if Present (Conc_Typ)
5414 and then not Comes_From_Source (Conc_Typ)
5415 then
5416 Error_Msg_NEL
5417 ("\& [<<", N, Standard_Constraint_Error, Eloc);
5419 else
5420 if GNATprove_Mode then
5421 Error_Msg_NEL
5422 ("\& would have been raised for objects of this "
5423 & "type", N, Standard_Constraint_Error, Eloc);
5424 else
5425 Error_Msg_NEL
5426 ("\& will be raised for objects of this type??",
5427 N, Standard_Constraint_Error, Eloc);
5428 end if;
5429 end if;
5430 end;
5432 else
5433 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
5434 end if;
5436 else
5437 Error_Msg ("\static expression fails Constraint_Check", Eloc);
5438 Set_Error_Posted (N);
5439 end if;
5440 end if;
5441 end if;
5443 return N;
5444 end Compile_Time_Constraint_Error;
5446 -----------------------
5447 -- Conditional_Delay --
5448 -----------------------
5450 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
5451 begin
5452 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
5453 Set_Has_Delayed_Freeze (New_Ent);
5454 end if;
5455 end Conditional_Delay;
5457 -------------------------
5458 -- Copy_Component_List --
5459 -------------------------
5461 function Copy_Component_List
5462 (R_Typ : Entity_Id;
5463 Loc : Source_Ptr) return List_Id
5465 Comp : Node_Id;
5466 Comps : constant List_Id := New_List;
5468 begin
5469 Comp := First_Component (Underlying_Type (R_Typ));
5470 while Present (Comp) loop
5471 if Comes_From_Source (Comp) then
5472 declare
5473 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
5474 begin
5475 Append_To (Comps,
5476 Make_Component_Declaration (Loc,
5477 Defining_Identifier =>
5478 Make_Defining_Identifier (Loc, Chars (Comp)),
5479 Component_Definition =>
5480 New_Copy_Tree
5481 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
5482 end;
5483 end if;
5485 Next_Component (Comp);
5486 end loop;
5488 return Comps;
5489 end Copy_Component_List;
5491 -------------------------
5492 -- Copy_Parameter_List --
5493 -------------------------
5495 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
5496 Loc : constant Source_Ptr := Sloc (Subp_Id);
5497 Plist : List_Id;
5498 Formal : Entity_Id;
5500 begin
5501 if No (First_Formal (Subp_Id)) then
5502 return No_List;
5503 else
5504 Plist := New_List;
5505 Formal := First_Formal (Subp_Id);
5506 while Present (Formal) loop
5507 Append_To (Plist,
5508 Make_Parameter_Specification (Loc,
5509 Defining_Identifier =>
5510 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
5511 In_Present => In_Present (Parent (Formal)),
5512 Out_Present => Out_Present (Parent (Formal)),
5513 Parameter_Type =>
5514 New_Occurrence_Of (Etype (Formal), Loc),
5515 Expression =>
5516 New_Copy_Tree (Expression (Parent (Formal)))));
5518 Next_Formal (Formal);
5519 end loop;
5520 end if;
5522 return Plist;
5523 end Copy_Parameter_List;
5525 ----------------------------
5526 -- Copy_SPARK_Mode_Aspect --
5527 ----------------------------
5529 procedure Copy_SPARK_Mode_Aspect (From : Node_Id; To : Node_Id) is
5530 pragma Assert (not Has_Aspects (To));
5531 Asp : Node_Id;
5533 begin
5534 if Has_Aspects (From) then
5535 Asp := Find_Aspect (Defining_Entity (From), Aspect_SPARK_Mode);
5537 if Present (Asp) then
5538 Set_Aspect_Specifications (To, New_List (New_Copy_Tree (Asp)));
5539 Set_Has_Aspects (To, True);
5540 end if;
5541 end if;
5542 end Copy_SPARK_Mode_Aspect;
5544 --------------------------
5545 -- Copy_Subprogram_Spec --
5546 --------------------------
5548 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
5549 Def_Id : Node_Id;
5550 Formal_Spec : Node_Id;
5551 Result : Node_Id;
5553 begin
5554 -- The structure of the original tree must be replicated without any
5555 -- alterations. Use New_Copy_Tree for this purpose.
5557 Result := New_Copy_Tree (Spec);
5559 -- However, the spec of a null procedure carries the corresponding null
5560 -- statement of the body (created by the parser), and this cannot be
5561 -- shared with the new subprogram spec.
5563 if Nkind (Result) = N_Procedure_Specification then
5564 Set_Null_Statement (Result, Empty);
5565 end if;
5567 -- Create a new entity for the defining unit name
5569 Def_Id := Defining_Unit_Name (Result);
5570 Set_Defining_Unit_Name (Result,
5571 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5573 -- Create new entities for the formal parameters
5575 if Present (Parameter_Specifications (Result)) then
5576 Formal_Spec := First (Parameter_Specifications (Result));
5577 while Present (Formal_Spec) loop
5578 Def_Id := Defining_Identifier (Formal_Spec);
5579 Set_Defining_Identifier (Formal_Spec,
5580 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5582 Next (Formal_Spec);
5583 end loop;
5584 end if;
5586 return Result;
5587 end Copy_Subprogram_Spec;
5589 --------------------------------
5590 -- Corresponding_Generic_Type --
5591 --------------------------------
5593 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
5594 Inst : Entity_Id;
5595 Gen : Entity_Id;
5596 Typ : Entity_Id;
5598 begin
5599 if not Is_Generic_Actual_Type (T) then
5600 return Any_Type;
5602 -- If the actual is the actual of an enclosing instance, resolution
5603 -- was correct in the generic.
5605 elsif Nkind (Parent (T)) = N_Subtype_Declaration
5606 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
5607 and then
5608 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
5609 then
5610 return Any_Type;
5612 else
5613 Inst := Scope (T);
5615 if Is_Wrapper_Package (Inst) then
5616 Inst := Related_Instance (Inst);
5617 end if;
5619 Gen :=
5620 Generic_Parent
5621 (Specification (Unit_Declaration_Node (Inst)));
5623 -- Generic actual has the same name as the corresponding formal
5625 Typ := First_Entity (Gen);
5626 while Present (Typ) loop
5627 if Chars (Typ) = Chars (T) then
5628 return Typ;
5629 end if;
5631 Next_Entity (Typ);
5632 end loop;
5634 return Any_Type;
5635 end if;
5636 end Corresponding_Generic_Type;
5638 --------------------
5639 -- Current_Entity --
5640 --------------------
5642 -- The currently visible definition for a given identifier is the
5643 -- one most chained at the start of the visibility chain, i.e. the
5644 -- one that is referenced by the Node_Id value of the name of the
5645 -- given identifier.
5647 function Current_Entity (N : Node_Id) return Entity_Id is
5648 begin
5649 return Get_Name_Entity_Id (Chars (N));
5650 end Current_Entity;
5652 -----------------------------
5653 -- Current_Entity_In_Scope --
5654 -----------------------------
5656 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
5657 E : Entity_Id;
5658 CS : constant Entity_Id := Current_Scope;
5660 Transient_Case : constant Boolean := Scope_Is_Transient;
5662 begin
5663 E := Get_Name_Entity_Id (Chars (N));
5664 while Present (E)
5665 and then Scope (E) /= CS
5666 and then (not Transient_Case or else Scope (E) /= Scope (CS))
5667 loop
5668 E := Homonym (E);
5669 end loop;
5671 return E;
5672 end Current_Entity_In_Scope;
5674 -------------------
5675 -- Current_Scope --
5676 -------------------
5678 function Current_Scope return Entity_Id is
5679 begin
5680 if Scope_Stack.Last = -1 then
5681 return Standard_Standard;
5682 else
5683 declare
5684 C : constant Entity_Id :=
5685 Scope_Stack.Table (Scope_Stack.Last).Entity;
5686 begin
5687 if Present (C) then
5688 return C;
5689 else
5690 return Standard_Standard;
5691 end if;
5692 end;
5693 end if;
5694 end Current_Scope;
5696 ----------------------------
5697 -- Current_Scope_No_Loops --
5698 ----------------------------
5700 function Current_Scope_No_Loops return Entity_Id is
5701 S : Entity_Id;
5703 begin
5704 -- Examine the scope stack starting from the current scope and skip any
5705 -- internally generated loops.
5707 S := Current_Scope;
5708 while Present (S) and then S /= Standard_Standard loop
5709 if Ekind (S) = E_Loop and then not Comes_From_Source (S) then
5710 S := Scope (S);
5711 else
5712 exit;
5713 end if;
5714 end loop;
5716 return S;
5717 end Current_Scope_No_Loops;
5719 ------------------------
5720 -- Current_Subprogram --
5721 ------------------------
5723 function Current_Subprogram return Entity_Id is
5724 Scop : constant Entity_Id := Current_Scope;
5725 begin
5726 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
5727 return Scop;
5728 else
5729 return Enclosing_Subprogram (Scop);
5730 end if;
5731 end Current_Subprogram;
5733 ----------------------------------
5734 -- Deepest_Type_Access_Level --
5735 ----------------------------------
5737 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
5738 begin
5739 if Ekind (Typ) = E_Anonymous_Access_Type
5740 and then not Is_Local_Anonymous_Access (Typ)
5741 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
5742 then
5743 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
5744 -- access type.
5746 return
5747 Scope_Depth (Enclosing_Dynamic_Scope
5748 (Defining_Identifier
5749 (Associated_Node_For_Itype (Typ))));
5751 -- For generic formal type, return Int'Last (infinite).
5752 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
5754 elsif Is_Generic_Type (Root_Type (Typ)) then
5755 return UI_From_Int (Int'Last);
5757 else
5758 return Type_Access_Level (Typ);
5759 end if;
5760 end Deepest_Type_Access_Level;
5762 ---------------------
5763 -- Defining_Entity --
5764 ---------------------
5766 function Defining_Entity
5767 (N : Node_Id;
5768 Empty_On_Errors : Boolean := False;
5769 Concurrent_Subunit : Boolean := False) return Entity_Id
5771 begin
5772 case Nkind (N) is
5773 when N_Abstract_Subprogram_Declaration
5774 | N_Expression_Function
5775 | N_Formal_Subprogram_Declaration
5776 | N_Generic_Package_Declaration
5777 | N_Generic_Subprogram_Declaration
5778 | N_Package_Declaration
5779 | N_Subprogram_Body
5780 | N_Subprogram_Body_Stub
5781 | N_Subprogram_Declaration
5782 | N_Subprogram_Renaming_Declaration
5784 return Defining_Entity (Specification (N));
5786 when N_Component_Declaration
5787 | N_Defining_Program_Unit_Name
5788 | N_Discriminant_Specification
5789 | N_Entry_Body
5790 | N_Entry_Declaration
5791 | N_Entry_Index_Specification
5792 | N_Exception_Declaration
5793 | N_Exception_Renaming_Declaration
5794 | N_Formal_Object_Declaration
5795 | N_Formal_Package_Declaration
5796 | N_Formal_Type_Declaration
5797 | N_Full_Type_Declaration
5798 | N_Implicit_Label_Declaration
5799 | N_Incomplete_Type_Declaration
5800 | N_Iterator_Specification
5801 | N_Loop_Parameter_Specification
5802 | N_Number_Declaration
5803 | N_Object_Declaration
5804 | N_Object_Renaming_Declaration
5805 | N_Package_Body_Stub
5806 | N_Parameter_Specification
5807 | N_Private_Extension_Declaration
5808 | N_Private_Type_Declaration
5809 | N_Protected_Body
5810 | N_Protected_Body_Stub
5811 | N_Protected_Type_Declaration
5812 | N_Single_Protected_Declaration
5813 | N_Single_Task_Declaration
5814 | N_Subtype_Declaration
5815 | N_Task_Body
5816 | N_Task_Body_Stub
5817 | N_Task_Type_Declaration
5819 return Defining_Identifier (N);
5821 when N_Subunit =>
5822 declare
5823 Bod : constant Node_Id := Proper_Body (N);
5824 Orig_Bod : constant Node_Id := Original_Node (Bod);
5826 begin
5827 -- Retrieve the entity of the original protected or task body
5828 -- if requested by the caller.
5830 if Concurrent_Subunit
5831 and then Nkind (Bod) = N_Null_Statement
5832 and then Nkind_In (Orig_Bod, N_Protected_Body, N_Task_Body)
5833 then
5834 return Defining_Entity (Orig_Bod);
5835 else
5836 return Defining_Entity (Bod);
5837 end if;
5838 end;
5840 when N_Function_Instantiation
5841 | N_Function_Specification
5842 | N_Generic_Function_Renaming_Declaration
5843 | N_Generic_Package_Renaming_Declaration
5844 | N_Generic_Procedure_Renaming_Declaration
5845 | N_Package_Body
5846 | N_Package_Instantiation
5847 | N_Package_Renaming_Declaration
5848 | N_Package_Specification
5849 | N_Procedure_Instantiation
5850 | N_Procedure_Specification
5852 declare
5853 Nam : constant Node_Id := Defining_Unit_Name (N);
5854 Err : Entity_Id := Empty;
5856 begin
5857 if Nkind (Nam) in N_Entity then
5858 return Nam;
5860 -- For Error, make up a name and attach to declaration so we
5861 -- can continue semantic analysis.
5863 elsif Nam = Error then
5864 if Empty_On_Errors then
5865 return Empty;
5866 else
5867 Err := Make_Temporary (Sloc (N), 'T');
5868 Set_Defining_Unit_Name (N, Err);
5870 return Err;
5871 end if;
5873 -- If not an entity, get defining identifier
5875 else
5876 return Defining_Identifier (Nam);
5877 end if;
5878 end;
5880 when N_Block_Statement
5881 | N_Loop_Statement
5883 return Entity (Identifier (N));
5885 when others =>
5886 if Empty_On_Errors then
5887 return Empty;
5888 else
5889 raise Program_Error;
5890 end if;
5891 end case;
5892 end Defining_Entity;
5894 --------------------------
5895 -- Denotes_Discriminant --
5896 --------------------------
5898 function Denotes_Discriminant
5899 (N : Node_Id;
5900 Check_Concurrent : Boolean := False) return Boolean
5902 E : Entity_Id;
5904 begin
5905 if not Is_Entity_Name (N) or else No (Entity (N)) then
5906 return False;
5907 else
5908 E := Entity (N);
5909 end if;
5911 -- If we are checking for a protected type, the discriminant may have
5912 -- been rewritten as the corresponding discriminal of the original type
5913 -- or of the corresponding concurrent record, depending on whether we
5914 -- are in the spec or body of the protected type.
5916 return Ekind (E) = E_Discriminant
5917 or else
5918 (Check_Concurrent
5919 and then Ekind (E) = E_In_Parameter
5920 and then Present (Discriminal_Link (E))
5921 and then
5922 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
5923 or else
5924 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
5925 end Denotes_Discriminant;
5927 -------------------------
5928 -- Denotes_Same_Object --
5929 -------------------------
5931 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
5932 Obj1 : Node_Id := A1;
5933 Obj2 : Node_Id := A2;
5935 function Has_Prefix (N : Node_Id) return Boolean;
5936 -- Return True if N has attribute Prefix
5938 function Is_Renaming (N : Node_Id) return Boolean;
5939 -- Return true if N names a renaming entity
5941 function Is_Valid_Renaming (N : Node_Id) return Boolean;
5942 -- For renamings, return False if the prefix of any dereference within
5943 -- the renamed object_name is a variable, or any expression within the
5944 -- renamed object_name contains references to variables or calls on
5945 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
5947 ----------------
5948 -- Has_Prefix --
5949 ----------------
5951 function Has_Prefix (N : Node_Id) return Boolean is
5952 begin
5953 return
5954 Nkind_In (N,
5955 N_Attribute_Reference,
5956 N_Expanded_Name,
5957 N_Explicit_Dereference,
5958 N_Indexed_Component,
5959 N_Reference,
5960 N_Selected_Component,
5961 N_Slice);
5962 end Has_Prefix;
5964 -----------------
5965 -- Is_Renaming --
5966 -----------------
5968 function Is_Renaming (N : Node_Id) return Boolean is
5969 begin
5970 return Is_Entity_Name (N)
5971 and then Present (Renamed_Entity (Entity (N)));
5972 end Is_Renaming;
5974 -----------------------
5975 -- Is_Valid_Renaming --
5976 -----------------------
5978 function Is_Valid_Renaming (N : Node_Id) return Boolean is
5980 function Check_Renaming (N : Node_Id) return Boolean;
5981 -- Recursive function used to traverse all the prefixes of N
5983 function Check_Renaming (N : Node_Id) return Boolean is
5984 begin
5985 if Is_Renaming (N)
5986 and then not Check_Renaming (Renamed_Entity (Entity (N)))
5987 then
5988 return False;
5989 end if;
5991 if Nkind (N) = N_Indexed_Component then
5992 declare
5993 Indx : Node_Id;
5995 begin
5996 Indx := First (Expressions (N));
5997 while Present (Indx) loop
5998 if not Is_OK_Static_Expression (Indx) then
5999 return False;
6000 end if;
6002 Next_Index (Indx);
6003 end loop;
6004 end;
6005 end if;
6007 if Has_Prefix (N) then
6008 declare
6009 P : constant Node_Id := Prefix (N);
6011 begin
6012 if Nkind (N) = N_Explicit_Dereference
6013 and then Is_Variable (P)
6014 then
6015 return False;
6017 elsif Is_Entity_Name (P)
6018 and then Ekind (Entity (P)) = E_Function
6019 then
6020 return False;
6022 elsif Nkind (P) = N_Function_Call then
6023 return False;
6024 end if;
6026 -- Recursion to continue traversing the prefix of the
6027 -- renaming expression
6029 return Check_Renaming (P);
6030 end;
6031 end if;
6033 return True;
6034 end Check_Renaming;
6036 -- Start of processing for Is_Valid_Renaming
6038 begin
6039 return Check_Renaming (N);
6040 end Is_Valid_Renaming;
6042 -- Start of processing for Denotes_Same_Object
6044 begin
6045 -- Both names statically denote the same stand-alone object or parameter
6046 -- (RM 6.4.1(6.5/3))
6048 if Is_Entity_Name (Obj1)
6049 and then Is_Entity_Name (Obj2)
6050 and then Entity (Obj1) = Entity (Obj2)
6051 then
6052 return True;
6053 end if;
6055 -- For renamings, the prefix of any dereference within the renamed
6056 -- object_name is not a variable, and any expression within the
6057 -- renamed object_name contains no references to variables nor
6058 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
6060 if Is_Renaming (Obj1) then
6061 if Is_Valid_Renaming (Obj1) then
6062 Obj1 := Renamed_Entity (Entity (Obj1));
6063 else
6064 return False;
6065 end if;
6066 end if;
6068 if Is_Renaming (Obj2) then
6069 if Is_Valid_Renaming (Obj2) then
6070 Obj2 := Renamed_Entity (Entity (Obj2));
6071 else
6072 return False;
6073 end if;
6074 end if;
6076 -- No match if not same node kind (such cases are handled by
6077 -- Denotes_Same_Prefix)
6079 if Nkind (Obj1) /= Nkind (Obj2) then
6080 return False;
6082 -- After handling valid renamings, one of the two names statically
6083 -- denoted a renaming declaration whose renamed object_name is known
6084 -- to denote the same object as the other (RM 6.4.1(6.10/3))
6086 elsif Is_Entity_Name (Obj1) then
6087 if Is_Entity_Name (Obj2) then
6088 return Entity (Obj1) = Entity (Obj2);
6089 else
6090 return False;
6091 end if;
6093 -- Both names are selected_components, their prefixes are known to
6094 -- denote the same object, and their selector_names denote the same
6095 -- component (RM 6.4.1(6.6/3)).
6097 elsif Nkind (Obj1) = N_Selected_Component then
6098 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
6099 and then
6100 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
6102 -- Both names are dereferences and the dereferenced names are known to
6103 -- denote the same object (RM 6.4.1(6.7/3))
6105 elsif Nkind (Obj1) = N_Explicit_Dereference then
6106 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
6108 -- Both names are indexed_components, their prefixes are known to denote
6109 -- the same object, and each of the pairs of corresponding index values
6110 -- are either both static expressions with the same static value or both
6111 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
6113 elsif Nkind (Obj1) = N_Indexed_Component then
6114 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
6115 return False;
6116 else
6117 declare
6118 Indx1 : Node_Id;
6119 Indx2 : Node_Id;
6121 begin
6122 Indx1 := First (Expressions (Obj1));
6123 Indx2 := First (Expressions (Obj2));
6124 while Present (Indx1) loop
6126 -- Indexes must denote the same static value or same object
6128 if Is_OK_Static_Expression (Indx1) then
6129 if not Is_OK_Static_Expression (Indx2) then
6130 return False;
6132 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
6133 return False;
6134 end if;
6136 elsif not Denotes_Same_Object (Indx1, Indx2) then
6137 return False;
6138 end if;
6140 Next (Indx1);
6141 Next (Indx2);
6142 end loop;
6144 return True;
6145 end;
6146 end if;
6148 -- Both names are slices, their prefixes are known to denote the same
6149 -- object, and the two slices have statically matching index constraints
6150 -- (RM 6.4.1(6.9/3))
6152 elsif Nkind (Obj1) = N_Slice
6153 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
6154 then
6155 declare
6156 Lo1, Lo2, Hi1, Hi2 : Node_Id;
6158 begin
6159 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
6160 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
6162 -- Check whether bounds are statically identical. There is no
6163 -- attempt to detect partial overlap of slices.
6165 return Denotes_Same_Object (Lo1, Lo2)
6166 and then
6167 Denotes_Same_Object (Hi1, Hi2);
6168 end;
6170 -- In the recursion, literals appear as indexes
6172 elsif Nkind (Obj1) = N_Integer_Literal
6173 and then
6174 Nkind (Obj2) = N_Integer_Literal
6175 then
6176 return Intval (Obj1) = Intval (Obj2);
6178 else
6179 return False;
6180 end if;
6181 end Denotes_Same_Object;
6183 -------------------------
6184 -- Denotes_Same_Prefix --
6185 -------------------------
6187 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
6188 begin
6189 if Is_Entity_Name (A1) then
6190 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
6191 and then not Is_Access_Type (Etype (A1))
6192 then
6193 return Denotes_Same_Object (A1, Prefix (A2))
6194 or else Denotes_Same_Prefix (A1, Prefix (A2));
6195 else
6196 return False;
6197 end if;
6199 elsif Is_Entity_Name (A2) then
6200 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
6202 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
6203 and then
6204 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
6205 then
6206 declare
6207 Root1, Root2 : Node_Id;
6208 Depth1, Depth2 : Nat := 0;
6210 begin
6211 Root1 := Prefix (A1);
6212 while not Is_Entity_Name (Root1) loop
6213 if not Nkind_In
6214 (Root1, N_Selected_Component, N_Indexed_Component)
6215 then
6216 return False;
6217 else
6218 Root1 := Prefix (Root1);
6219 end if;
6221 Depth1 := Depth1 + 1;
6222 end loop;
6224 Root2 := Prefix (A2);
6225 while not Is_Entity_Name (Root2) loop
6226 if not Nkind_In (Root2, N_Selected_Component,
6227 N_Indexed_Component)
6228 then
6229 return False;
6230 else
6231 Root2 := Prefix (Root2);
6232 end if;
6234 Depth2 := Depth2 + 1;
6235 end loop;
6237 -- If both have the same depth and they do not denote the same
6238 -- object, they are disjoint and no warning is needed.
6240 if Depth1 = Depth2 then
6241 return False;
6243 elsif Depth1 > Depth2 then
6244 Root1 := Prefix (A1);
6245 for J in 1 .. Depth1 - Depth2 - 1 loop
6246 Root1 := Prefix (Root1);
6247 end loop;
6249 return Denotes_Same_Object (Root1, A2);
6251 else
6252 Root2 := Prefix (A2);
6253 for J in 1 .. Depth2 - Depth1 - 1 loop
6254 Root2 := Prefix (Root2);
6255 end loop;
6257 return Denotes_Same_Object (A1, Root2);
6258 end if;
6259 end;
6261 else
6262 return False;
6263 end if;
6264 end Denotes_Same_Prefix;
6266 ----------------------
6267 -- Denotes_Variable --
6268 ----------------------
6270 function Denotes_Variable (N : Node_Id) return Boolean is
6271 begin
6272 return Is_Variable (N) and then Paren_Count (N) = 0;
6273 end Denotes_Variable;
6275 -----------------------------
6276 -- Depends_On_Discriminant --
6277 -----------------------------
6279 function Depends_On_Discriminant (N : Node_Id) return Boolean is
6280 L : Node_Id;
6281 H : Node_Id;
6283 begin
6284 Get_Index_Bounds (N, L, H);
6285 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
6286 end Depends_On_Discriminant;
6288 -------------------------
6289 -- Designate_Same_Unit --
6290 -------------------------
6292 function Designate_Same_Unit
6293 (Name1 : Node_Id;
6294 Name2 : Node_Id) return Boolean
6296 K1 : constant Node_Kind := Nkind (Name1);
6297 K2 : constant Node_Kind := Nkind (Name2);
6299 function Prefix_Node (N : Node_Id) return Node_Id;
6300 -- Returns the parent unit name node of a defining program unit name
6301 -- or the prefix if N is a selected component or an expanded name.
6303 function Select_Node (N : Node_Id) return Node_Id;
6304 -- Returns the defining identifier node of a defining program unit
6305 -- name or the selector node if N is a selected component or an
6306 -- expanded name.
6308 -----------------
6309 -- Prefix_Node --
6310 -----------------
6312 function Prefix_Node (N : Node_Id) return Node_Id is
6313 begin
6314 if Nkind (N) = N_Defining_Program_Unit_Name then
6315 return Name (N);
6316 else
6317 return Prefix (N);
6318 end if;
6319 end Prefix_Node;
6321 -----------------
6322 -- Select_Node --
6323 -----------------
6325 function Select_Node (N : Node_Id) return Node_Id is
6326 begin
6327 if Nkind (N) = N_Defining_Program_Unit_Name then
6328 return Defining_Identifier (N);
6329 else
6330 return Selector_Name (N);
6331 end if;
6332 end Select_Node;
6334 -- Start of processing for Designate_Same_Unit
6336 begin
6337 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
6338 and then
6339 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
6340 then
6341 return Chars (Name1) = Chars (Name2);
6343 elsif Nkind_In (K1, N_Expanded_Name,
6344 N_Selected_Component,
6345 N_Defining_Program_Unit_Name)
6346 and then
6347 Nkind_In (K2, N_Expanded_Name,
6348 N_Selected_Component,
6349 N_Defining_Program_Unit_Name)
6350 then
6351 return
6352 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
6353 and then
6354 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
6356 else
6357 return False;
6358 end if;
6359 end Designate_Same_Unit;
6361 ---------------------------------------------
6362 -- Diagnose_Iterated_Component_Association --
6363 ---------------------------------------------
6365 procedure Diagnose_Iterated_Component_Association (N : Node_Id) is
6366 Def_Id : constant Entity_Id := Defining_Identifier (N);
6367 Aggr : Node_Id;
6369 begin
6370 -- Determine whether the iterated component association appears within
6371 -- an aggregate. If this is the case, raise Program_Error because the
6372 -- iterated component association cannot be left in the tree as is and
6373 -- must always be processed by the related aggregate.
6375 Aggr := N;
6376 while Present (Aggr) loop
6377 if Nkind (Aggr) = N_Aggregate then
6378 raise Program_Error;
6380 -- Prevent the search from going too far
6382 elsif Is_Body_Or_Package_Declaration (Aggr) then
6383 exit;
6384 end if;
6386 Aggr := Parent (Aggr);
6387 end loop;
6389 -- At this point it is known that the iterated component association is
6390 -- not within an aggregate. This is really a quantified expression with
6391 -- a missing "all" or "some" quantifier.
6393 Error_Msg_N ("missing quantifier", Def_Id);
6395 -- Rewrite the iterated component association as True to prevent any
6396 -- cascaded errors.
6398 Rewrite (N, New_Occurrence_Of (Standard_True, Sloc (N)));
6399 Analyze (N);
6400 end Diagnose_Iterated_Component_Association;
6402 ---------------------------------
6403 -- Dynamic_Accessibility_Level --
6404 ---------------------------------
6406 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
6407 Loc : constant Source_Ptr := Sloc (Expr);
6409 function Make_Level_Literal (Level : Uint) return Node_Id;
6410 -- Construct an integer literal representing an accessibility level
6411 -- with its type set to Natural.
6413 ------------------------
6414 -- Make_Level_Literal --
6415 ------------------------
6417 function Make_Level_Literal (Level : Uint) return Node_Id is
6418 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
6420 begin
6421 Set_Etype (Result, Standard_Natural);
6422 return Result;
6423 end Make_Level_Literal;
6425 -- Local variables
6427 E : Entity_Id;
6429 -- Start of processing for Dynamic_Accessibility_Level
6431 begin
6432 if Is_Entity_Name (Expr) then
6433 E := Entity (Expr);
6435 if Present (Renamed_Object (E)) then
6436 return Dynamic_Accessibility_Level (Renamed_Object (E));
6437 end if;
6439 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
6440 if Present (Extra_Accessibility (E)) then
6441 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
6442 end if;
6443 end if;
6444 end if;
6446 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
6448 case Nkind (Expr) is
6450 -- For access discriminant, the level of the enclosing object
6452 when N_Selected_Component =>
6453 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
6454 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
6455 E_Anonymous_Access_Type
6456 then
6457 return Make_Level_Literal (Object_Access_Level (Expr));
6458 end if;
6460 when N_Attribute_Reference =>
6461 case Get_Attribute_Id (Attribute_Name (Expr)) is
6463 -- For X'Access, the level of the prefix X
6465 when Attribute_Access =>
6466 return Make_Level_Literal
6467 (Object_Access_Level (Prefix (Expr)));
6469 -- Treat the unchecked attributes as library-level
6471 when Attribute_Unchecked_Access
6472 | Attribute_Unrestricted_Access
6474 return Make_Level_Literal (Scope_Depth (Standard_Standard));
6476 -- No other access-valued attributes
6478 when others =>
6479 raise Program_Error;
6480 end case;
6482 when N_Allocator =>
6484 -- Unimplemented: depends on context. As an actual parameter where
6485 -- formal type is anonymous, use
6486 -- Scope_Depth (Current_Scope) + 1.
6487 -- For other cases, see 3.10.2(14/3) and following. ???
6489 null;
6491 when N_Type_Conversion =>
6492 if not Is_Local_Anonymous_Access (Etype (Expr)) then
6494 -- Handle type conversions introduced for a rename of an
6495 -- Ada 2012 stand-alone object of an anonymous access type.
6497 return Dynamic_Accessibility_Level (Expression (Expr));
6498 end if;
6500 when others =>
6501 null;
6502 end case;
6504 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
6505 end Dynamic_Accessibility_Level;
6507 ------------------------
6508 -- Discriminated_Size --
6509 ------------------------
6511 function Discriminated_Size (Comp : Entity_Id) return Boolean is
6512 function Non_Static_Bound (Bound : Node_Id) return Boolean;
6513 -- Check whether the bound of an index is non-static and does denote
6514 -- a discriminant, in which case any object of the type (protected or
6515 -- otherwise) will have a non-static size.
6517 ----------------------
6518 -- Non_Static_Bound --
6519 ----------------------
6521 function Non_Static_Bound (Bound : Node_Id) return Boolean is
6522 begin
6523 if Is_OK_Static_Expression (Bound) then
6524 return False;
6526 -- If the bound is given by a discriminant it is non-static
6527 -- (A static constraint replaces the reference with the value).
6528 -- In an protected object the discriminant has been replaced by
6529 -- the corresponding discriminal within the protected operation.
6531 elsif Is_Entity_Name (Bound)
6532 and then
6533 (Ekind (Entity (Bound)) = E_Discriminant
6534 or else Present (Discriminal_Link (Entity (Bound))))
6535 then
6536 return False;
6538 else
6539 return True;
6540 end if;
6541 end Non_Static_Bound;
6543 -- Local variables
6545 Typ : constant Entity_Id := Etype (Comp);
6546 Index : Node_Id;
6548 -- Start of processing for Discriminated_Size
6550 begin
6551 if not Is_Array_Type (Typ) then
6552 return False;
6553 end if;
6555 if Ekind (Typ) = E_Array_Subtype then
6556 Index := First_Index (Typ);
6557 while Present (Index) loop
6558 if Non_Static_Bound (Low_Bound (Index))
6559 or else Non_Static_Bound (High_Bound (Index))
6560 then
6561 return False;
6562 end if;
6564 Next_Index (Index);
6565 end loop;
6567 return True;
6568 end if;
6570 return False;
6571 end Discriminated_Size;
6573 -----------------------------------
6574 -- Effective_Extra_Accessibility --
6575 -----------------------------------
6577 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
6578 begin
6579 if Present (Renamed_Object (Id))
6580 and then Is_Entity_Name (Renamed_Object (Id))
6581 then
6582 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
6583 else
6584 return Extra_Accessibility (Id);
6585 end if;
6586 end Effective_Extra_Accessibility;
6588 -----------------------------
6589 -- Effective_Reads_Enabled --
6590 -----------------------------
6592 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
6593 begin
6594 return Has_Enabled_Property (Id, Name_Effective_Reads);
6595 end Effective_Reads_Enabled;
6597 ------------------------------
6598 -- Effective_Writes_Enabled --
6599 ------------------------------
6601 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
6602 begin
6603 return Has_Enabled_Property (Id, Name_Effective_Writes);
6604 end Effective_Writes_Enabled;
6606 ------------------------------
6607 -- Enclosing_Comp_Unit_Node --
6608 ------------------------------
6610 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
6611 Current_Node : Node_Id;
6613 begin
6614 Current_Node := N;
6615 while Present (Current_Node)
6616 and then Nkind (Current_Node) /= N_Compilation_Unit
6617 loop
6618 Current_Node := Parent (Current_Node);
6619 end loop;
6621 if Nkind (Current_Node) /= N_Compilation_Unit then
6622 return Empty;
6623 else
6624 return Current_Node;
6625 end if;
6626 end Enclosing_Comp_Unit_Node;
6628 --------------------------
6629 -- Enclosing_CPP_Parent --
6630 --------------------------
6632 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
6633 Parent_Typ : Entity_Id := Typ;
6635 begin
6636 while not Is_CPP_Class (Parent_Typ)
6637 and then Etype (Parent_Typ) /= Parent_Typ
6638 loop
6639 Parent_Typ := Etype (Parent_Typ);
6641 if Is_Private_Type (Parent_Typ) then
6642 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6643 end if;
6644 end loop;
6646 pragma Assert (Is_CPP_Class (Parent_Typ));
6647 return Parent_Typ;
6648 end Enclosing_CPP_Parent;
6650 ---------------------------
6651 -- Enclosing_Declaration --
6652 ---------------------------
6654 function Enclosing_Declaration (N : Node_Id) return Node_Id is
6655 Decl : Node_Id := N;
6657 begin
6658 while Present (Decl)
6659 and then not (Nkind (Decl) in N_Declaration
6660 or else
6661 Nkind (Decl) in N_Later_Decl_Item
6662 or else
6663 Nkind (Decl) = N_Number_Declaration)
6664 loop
6665 Decl := Parent (Decl);
6666 end loop;
6668 return Decl;
6669 end Enclosing_Declaration;
6671 ----------------------------
6672 -- Enclosing_Generic_Body --
6673 ----------------------------
6675 function Enclosing_Generic_Body
6676 (N : Node_Id) return Node_Id
6678 P : Node_Id;
6679 Decl : Node_Id;
6680 Spec : Node_Id;
6682 begin
6683 P := Parent (N);
6684 while Present (P) loop
6685 if Nkind (P) = N_Package_Body
6686 or else Nkind (P) = N_Subprogram_Body
6687 then
6688 Spec := Corresponding_Spec (P);
6690 if Present (Spec) then
6691 Decl := Unit_Declaration_Node (Spec);
6693 if Nkind (Decl) = N_Generic_Package_Declaration
6694 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6695 then
6696 return P;
6697 end if;
6698 end if;
6699 end if;
6701 P := Parent (P);
6702 end loop;
6704 return Empty;
6705 end Enclosing_Generic_Body;
6707 ----------------------------
6708 -- Enclosing_Generic_Unit --
6709 ----------------------------
6711 function Enclosing_Generic_Unit
6712 (N : Node_Id) return Node_Id
6714 P : Node_Id;
6715 Decl : Node_Id;
6716 Spec : Node_Id;
6718 begin
6719 P := Parent (N);
6720 while Present (P) loop
6721 if Nkind (P) = N_Generic_Package_Declaration
6722 or else Nkind (P) = N_Generic_Subprogram_Declaration
6723 then
6724 return P;
6726 elsif Nkind (P) = N_Package_Body
6727 or else Nkind (P) = N_Subprogram_Body
6728 then
6729 Spec := Corresponding_Spec (P);
6731 if Present (Spec) then
6732 Decl := Unit_Declaration_Node (Spec);
6734 if Nkind (Decl) = N_Generic_Package_Declaration
6735 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6736 then
6737 return Decl;
6738 end if;
6739 end if;
6740 end if;
6742 P := Parent (P);
6743 end loop;
6745 return Empty;
6746 end Enclosing_Generic_Unit;
6748 -------------------------------
6749 -- Enclosing_Lib_Unit_Entity --
6750 -------------------------------
6752 function Enclosing_Lib_Unit_Entity
6753 (E : Entity_Id := Current_Scope) return Entity_Id
6755 Unit_Entity : Entity_Id;
6757 begin
6758 -- Look for enclosing library unit entity by following scope links.
6759 -- Equivalent to, but faster than indexing through the scope stack.
6761 Unit_Entity := E;
6762 while (Present (Scope (Unit_Entity))
6763 and then Scope (Unit_Entity) /= Standard_Standard)
6764 and not Is_Child_Unit (Unit_Entity)
6765 loop
6766 Unit_Entity := Scope (Unit_Entity);
6767 end loop;
6769 return Unit_Entity;
6770 end Enclosing_Lib_Unit_Entity;
6772 -----------------------------
6773 -- Enclosing_Lib_Unit_Node --
6774 -----------------------------
6776 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
6777 Encl_Unit : Node_Id;
6779 begin
6780 Encl_Unit := Enclosing_Comp_Unit_Node (N);
6781 while Present (Encl_Unit)
6782 and then Nkind (Unit (Encl_Unit)) = N_Subunit
6783 loop
6784 Encl_Unit := Library_Unit (Encl_Unit);
6785 end loop;
6787 pragma Assert (Nkind (Encl_Unit) = N_Compilation_Unit);
6788 return Encl_Unit;
6789 end Enclosing_Lib_Unit_Node;
6791 -----------------------
6792 -- Enclosing_Package --
6793 -----------------------
6795 function Enclosing_Package (E : Entity_Id) return Entity_Id is
6796 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6798 begin
6799 if Dynamic_Scope = Standard_Standard then
6800 return Standard_Standard;
6802 elsif Dynamic_Scope = Empty then
6803 return Empty;
6805 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
6806 E_Generic_Package)
6807 then
6808 return Dynamic_Scope;
6810 else
6811 return Enclosing_Package (Dynamic_Scope);
6812 end if;
6813 end Enclosing_Package;
6815 -------------------------------------
6816 -- Enclosing_Package_Or_Subprogram --
6817 -------------------------------------
6819 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
6820 S : Entity_Id;
6822 begin
6823 S := Scope (E);
6824 while Present (S) loop
6825 if Is_Package_Or_Generic_Package (S)
6826 or else Ekind (S) = E_Package_Body
6827 then
6828 return S;
6830 elsif Is_Subprogram_Or_Generic_Subprogram (S)
6831 or else Ekind (S) = E_Subprogram_Body
6832 then
6833 return S;
6835 else
6836 S := Scope (S);
6837 end if;
6838 end loop;
6840 return Empty;
6841 end Enclosing_Package_Or_Subprogram;
6843 --------------------------
6844 -- Enclosing_Subprogram --
6845 --------------------------
6847 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
6848 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6850 begin
6851 if Dynamic_Scope = Standard_Standard then
6852 return Empty;
6854 elsif Dynamic_Scope = Empty then
6855 return Empty;
6857 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
6858 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
6860 elsif Ekind (Dynamic_Scope) = E_Block
6861 or else Ekind (Dynamic_Scope) = E_Return_Statement
6862 then
6863 return Enclosing_Subprogram (Dynamic_Scope);
6865 elsif Ekind (Dynamic_Scope) = E_Task_Type then
6866 return Get_Task_Body_Procedure (Dynamic_Scope);
6868 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
6869 and then Present (Full_View (Dynamic_Scope))
6870 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
6871 then
6872 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
6874 -- No body is generated if the protected operation is eliminated
6876 elsif Convention (Dynamic_Scope) = Convention_Protected
6877 and then not Is_Eliminated (Dynamic_Scope)
6878 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
6879 then
6880 return Protected_Body_Subprogram (Dynamic_Scope);
6882 else
6883 return Dynamic_Scope;
6884 end if;
6885 end Enclosing_Subprogram;
6887 --------------------------
6888 -- End_Keyword_Location --
6889 --------------------------
6891 function End_Keyword_Location (N : Node_Id) return Source_Ptr is
6892 function End_Label_Loc (Nod : Node_Id) return Source_Ptr;
6893 -- Return the source location of Nod's end label according to the
6894 -- following precedence rules:
6896 -- 1) If the end label exists, return its location
6897 -- 2) If Nod exists, return its location
6898 -- 3) Return the location of N
6900 -------------------
6901 -- End_Label_Loc --
6902 -------------------
6904 function End_Label_Loc (Nod : Node_Id) return Source_Ptr is
6905 Label : Node_Id;
6907 begin
6908 if Present (Nod) then
6909 Label := End_Label (Nod);
6911 if Present (Label) then
6912 return Sloc (Label);
6913 else
6914 return Sloc (Nod);
6915 end if;
6917 else
6918 return Sloc (N);
6919 end if;
6920 end End_Label_Loc;
6922 -- Local variables
6924 Owner : Node_Id;
6926 -- Start of processing for End_Keyword_Location
6928 begin
6929 if Nkind_In (N, N_Block_Statement,
6930 N_Entry_Body,
6931 N_Package_Body,
6932 N_Subprogram_Body,
6933 N_Task_Body)
6934 then
6935 Owner := Handled_Statement_Sequence (N);
6937 elsif Nkind (N) = N_Package_Declaration then
6938 Owner := Specification (N);
6940 elsif Nkind (N) = N_Protected_Body then
6941 Owner := N;
6943 elsif Nkind_In (N, N_Protected_Type_Declaration,
6944 N_Single_Protected_Declaration)
6945 then
6946 Owner := Protected_Definition (N);
6948 elsif Nkind_In (N, N_Single_Task_Declaration,
6949 N_Task_Type_Declaration)
6950 then
6951 Owner := Task_Definition (N);
6953 -- This routine should not be called with other contexts
6955 else
6956 pragma Assert (False);
6957 null;
6958 end if;
6960 return End_Label_Loc (Owner);
6961 end End_Keyword_Location;
6963 ------------------------
6964 -- Ensure_Freeze_Node --
6965 ------------------------
6967 procedure Ensure_Freeze_Node (E : Entity_Id) is
6968 FN : Node_Id;
6969 begin
6970 if No (Freeze_Node (E)) then
6971 FN := Make_Freeze_Entity (Sloc (E));
6972 Set_Has_Delayed_Freeze (E);
6973 Set_Freeze_Node (E, FN);
6974 Set_Access_Types_To_Process (FN, No_Elist);
6975 Set_TSS_Elist (FN, No_Elist);
6976 Set_Entity (FN, E);
6977 end if;
6978 end Ensure_Freeze_Node;
6980 ----------------
6981 -- Enter_Name --
6982 ----------------
6984 procedure Enter_Name (Def_Id : Entity_Id) is
6985 C : constant Entity_Id := Current_Entity (Def_Id);
6986 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
6987 S : constant Entity_Id := Current_Scope;
6989 begin
6990 Generate_Definition (Def_Id);
6992 -- Add new name to current scope declarations. Check for duplicate
6993 -- declaration, which may or may not be a genuine error.
6995 if Present (E) then
6997 -- Case of previous entity entered because of a missing declaration
6998 -- or else a bad subtype indication. Best is to use the new entity,
6999 -- and make the previous one invisible.
7001 if Etype (E) = Any_Type then
7002 Set_Is_Immediately_Visible (E, False);
7004 -- Case of renaming declaration constructed for package instances.
7005 -- if there is an explicit declaration with the same identifier,
7006 -- the renaming is not immediately visible any longer, but remains
7007 -- visible through selected component notation.
7009 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
7010 and then not Comes_From_Source (E)
7011 then
7012 Set_Is_Immediately_Visible (E, False);
7014 -- The new entity may be the package renaming, which has the same
7015 -- same name as a generic formal which has been seen already.
7017 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
7018 and then not Comes_From_Source (Def_Id)
7019 then
7020 Set_Is_Immediately_Visible (E, False);
7022 -- For a fat pointer corresponding to a remote access to subprogram,
7023 -- we use the same identifier as the RAS type, so that the proper
7024 -- name appears in the stub. This type is only retrieved through
7025 -- the RAS type and never by visibility, and is not added to the
7026 -- visibility list (see below).
7028 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
7029 and then Ekind (Def_Id) = E_Record_Type
7030 and then Present (Corresponding_Remote_Type (Def_Id))
7031 then
7032 null;
7034 -- Case of an implicit operation or derived literal. The new entity
7035 -- hides the implicit one, which is removed from all visibility,
7036 -- i.e. the entity list of its scope, and homonym chain of its name.
7038 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
7039 or else Is_Internal (E)
7040 then
7041 declare
7042 Decl : constant Node_Id := Parent (E);
7043 Prev : Entity_Id;
7044 Prev_Vis : Entity_Id;
7046 begin
7047 -- If E is an implicit declaration, it cannot be the first
7048 -- entity in the scope.
7050 Prev := First_Entity (Current_Scope);
7051 while Present (Prev) and then Next_Entity (Prev) /= E loop
7052 Next_Entity (Prev);
7053 end loop;
7055 if No (Prev) then
7057 -- If E is not on the entity chain of the current scope,
7058 -- it is an implicit declaration in the generic formal
7059 -- part of a generic subprogram. When analyzing the body,
7060 -- the generic formals are visible but not on the entity
7061 -- chain of the subprogram. The new entity will become
7062 -- the visible one in the body.
7064 pragma Assert
7065 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
7066 null;
7068 else
7069 Link_Entities (Prev, Next_Entity (E));
7071 if No (Next_Entity (Prev)) then
7072 Set_Last_Entity (Current_Scope, Prev);
7073 end if;
7075 if E = Current_Entity (E) then
7076 Prev_Vis := Empty;
7078 else
7079 Prev_Vis := Current_Entity (E);
7080 while Homonym (Prev_Vis) /= E loop
7081 Prev_Vis := Homonym (Prev_Vis);
7082 end loop;
7083 end if;
7085 if Present (Prev_Vis) then
7087 -- Skip E in the visibility chain
7089 Set_Homonym (Prev_Vis, Homonym (E));
7091 else
7092 Set_Name_Entity_Id (Chars (E), Homonym (E));
7093 end if;
7094 end if;
7095 end;
7097 -- This section of code could use a comment ???
7099 elsif Present (Etype (E))
7100 and then Is_Concurrent_Type (Etype (E))
7101 and then E = Def_Id
7102 then
7103 return;
7105 -- If the homograph is a protected component renaming, it should not
7106 -- be hiding the current entity. Such renamings are treated as weak
7107 -- declarations.
7109 elsif Is_Prival (E) then
7110 Set_Is_Immediately_Visible (E, False);
7112 -- In this case the current entity is a protected component renaming.
7113 -- Perform minimal decoration by setting the scope and return since
7114 -- the prival should not be hiding other visible entities.
7116 elsif Is_Prival (Def_Id) then
7117 Set_Scope (Def_Id, Current_Scope);
7118 return;
7120 -- Analogous to privals, the discriminal generated for an entry index
7121 -- parameter acts as a weak declaration. Perform minimal decoration
7122 -- to avoid bogus errors.
7124 elsif Is_Discriminal (Def_Id)
7125 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
7126 then
7127 Set_Scope (Def_Id, Current_Scope);
7128 return;
7130 -- In the body or private part of an instance, a type extension may
7131 -- introduce a component with the same name as that of an actual. The
7132 -- legality rule is not enforced, but the semantics of the full type
7133 -- with two components of same name are not clear at this point???
7135 elsif In_Instance_Not_Visible then
7136 null;
7138 -- When compiling a package body, some child units may have become
7139 -- visible. They cannot conflict with local entities that hide them.
7141 elsif Is_Child_Unit (E)
7142 and then In_Open_Scopes (Scope (E))
7143 and then not Is_Immediately_Visible (E)
7144 then
7145 null;
7147 -- Conversely, with front-end inlining we may compile the parent body
7148 -- first, and a child unit subsequently. The context is now the
7149 -- parent spec, and body entities are not visible.
7151 elsif Is_Child_Unit (Def_Id)
7152 and then Is_Package_Body_Entity (E)
7153 and then not In_Package_Body (Current_Scope)
7154 then
7155 null;
7157 -- Case of genuine duplicate declaration
7159 else
7160 Error_Msg_Sloc := Sloc (E);
7162 -- If the previous declaration is an incomplete type declaration
7163 -- this may be an attempt to complete it with a private type. The
7164 -- following avoids confusing cascaded errors.
7166 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
7167 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
7168 then
7169 Error_Msg_N
7170 ("incomplete type cannot be completed with a private " &
7171 "declaration", Parent (Def_Id));
7172 Set_Is_Immediately_Visible (E, False);
7173 Set_Full_View (E, Def_Id);
7175 -- An inherited component of a record conflicts with a new
7176 -- discriminant. The discriminant is inserted first in the scope,
7177 -- but the error should be posted on it, not on the component.
7179 elsif Ekind (E) = E_Discriminant
7180 and then Present (Scope (Def_Id))
7181 and then Scope (Def_Id) /= Current_Scope
7182 then
7183 Error_Msg_Sloc := Sloc (Def_Id);
7184 Error_Msg_N ("& conflicts with declaration#", E);
7185 return;
7187 -- If the name of the unit appears in its own context clause, a
7188 -- dummy package with the name has already been created, and the
7189 -- error emitted. Try to continue quietly.
7191 elsif Error_Posted (E)
7192 and then Sloc (E) = No_Location
7193 and then Nkind (Parent (E)) = N_Package_Specification
7194 and then Current_Scope = Standard_Standard
7195 then
7196 Set_Scope (Def_Id, Current_Scope);
7197 return;
7199 else
7200 Error_Msg_N ("& conflicts with declaration#", Def_Id);
7202 -- Avoid cascaded messages with duplicate components in
7203 -- derived types.
7205 if Ekind_In (E, E_Component, E_Discriminant) then
7206 return;
7207 end if;
7208 end if;
7210 if Nkind (Parent (Parent (Def_Id))) =
7211 N_Generic_Subprogram_Declaration
7212 and then Def_Id =
7213 Defining_Entity (Specification (Parent (Parent (Def_Id))))
7214 then
7215 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
7216 end if;
7218 -- If entity is in standard, then we are in trouble, because it
7219 -- means that we have a library package with a duplicated name.
7220 -- That's hard to recover from, so abort.
7222 if S = Standard_Standard then
7223 raise Unrecoverable_Error;
7225 -- Otherwise we continue with the declaration. Having two
7226 -- identical declarations should not cause us too much trouble.
7228 else
7229 null;
7230 end if;
7231 end if;
7232 end if;
7234 -- If we fall through, declaration is OK, at least OK enough to continue
7236 -- If Def_Id is a discriminant or a record component we are in the midst
7237 -- of inheriting components in a derived record definition. Preserve
7238 -- their Ekind and Etype.
7240 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
7241 null;
7243 -- If a type is already set, leave it alone (happens when a type
7244 -- declaration is reanalyzed following a call to the optimizer).
7246 elsif Present (Etype (Def_Id)) then
7247 null;
7249 -- Otherwise, the kind E_Void insures that premature uses of the entity
7250 -- will be detected. Any_Type insures that no cascaded errors will occur
7252 else
7253 Set_Ekind (Def_Id, E_Void);
7254 Set_Etype (Def_Id, Any_Type);
7255 end if;
7257 -- Inherited discriminants and components in derived record types are
7258 -- immediately visible. Itypes are not.
7260 -- Unless the Itype is for a record type with a corresponding remote
7261 -- type (what is that about, it was not commented ???)
7263 if Ekind_In (Def_Id, E_Discriminant, E_Component)
7264 or else
7265 ((not Is_Record_Type (Def_Id)
7266 or else No (Corresponding_Remote_Type (Def_Id)))
7267 and then not Is_Itype (Def_Id))
7268 then
7269 Set_Is_Immediately_Visible (Def_Id);
7270 Set_Current_Entity (Def_Id);
7271 end if;
7273 Set_Homonym (Def_Id, C);
7274 Append_Entity (Def_Id, S);
7275 Set_Public_Status (Def_Id);
7277 -- Declaring a homonym is not allowed in SPARK ...
7279 if Present (C) and then Restriction_Check_Required (SPARK_05) then
7280 declare
7281 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
7282 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
7283 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
7285 begin
7286 -- ... unless the new declaration is in a subprogram, and the
7287 -- visible declaration is a variable declaration or a parameter
7288 -- specification outside that subprogram.
7290 if Present (Enclosing_Subp)
7291 and then Nkind_In (Parent (C), N_Object_Declaration,
7292 N_Parameter_Specification)
7293 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
7294 then
7295 null;
7297 -- ... or the new declaration is in a package, and the visible
7298 -- declaration occurs outside that package.
7300 elsif Present (Enclosing_Pack)
7301 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
7302 then
7303 null;
7305 -- ... or the new declaration is a component declaration in a
7306 -- record type definition.
7308 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
7309 null;
7311 -- Don't issue error for non-source entities
7313 elsif Comes_From_Source (Def_Id)
7314 and then Comes_From_Source (C)
7315 then
7316 Error_Msg_Sloc := Sloc (C);
7317 Check_SPARK_05_Restriction
7318 ("redeclaration of identifier &#", Def_Id);
7319 end if;
7320 end;
7321 end if;
7323 -- Warn if new entity hides an old one
7325 if Warn_On_Hiding and then Present (C)
7327 -- Don't warn for record components since they always have a well
7328 -- defined scope which does not confuse other uses. Note that in
7329 -- some cases, Ekind has not been set yet.
7331 and then Ekind (C) /= E_Component
7332 and then Ekind (C) /= E_Discriminant
7333 and then Nkind (Parent (C)) /= N_Component_Declaration
7334 and then Ekind (Def_Id) /= E_Component
7335 and then Ekind (Def_Id) /= E_Discriminant
7336 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
7338 -- Don't warn for one character variables. It is too common to use
7339 -- such variables as locals and will just cause too many false hits.
7341 and then Length_Of_Name (Chars (C)) /= 1
7343 -- Don't warn for non-source entities
7345 and then Comes_From_Source (C)
7346 and then Comes_From_Source (Def_Id)
7348 -- Don't warn unless entity in question is in extended main source
7350 and then In_Extended_Main_Source_Unit (Def_Id)
7352 -- Finally, the hidden entity must be either immediately visible or
7353 -- use visible (i.e. from a used package).
7355 and then
7356 (Is_Immediately_Visible (C)
7357 or else
7358 Is_Potentially_Use_Visible (C))
7359 then
7360 Error_Msg_Sloc := Sloc (C);
7361 Error_Msg_N ("declaration hides &#?h?", Def_Id);
7362 end if;
7363 end Enter_Name;
7365 ---------------
7366 -- Entity_Of --
7367 ---------------
7369 function Entity_Of (N : Node_Id) return Entity_Id is
7370 Id : Entity_Id;
7371 Ren : Node_Id;
7373 begin
7374 -- Assume that the arbitrary node does not have an entity
7376 Id := Empty;
7378 if Is_Entity_Name (N) then
7379 Id := Entity (N);
7381 -- Follow a possible chain of renamings to reach the earliest renamed
7382 -- source object.
7384 while Present (Id)
7385 and then Is_Object (Id)
7386 and then Present (Renamed_Object (Id))
7387 loop
7388 Ren := Renamed_Object (Id);
7390 -- The reference renames an abstract state or a whole object
7392 -- Obj : ...;
7393 -- Ren : ... renames Obj;
7395 if Is_Entity_Name (Ren) then
7396 Id := Entity (Ren);
7398 -- The reference renames a function result. Check the original
7399 -- node in case expansion relocates the function call.
7401 -- Ren : ... renames Func_Call;
7403 elsif Nkind (Original_Node (Ren)) = N_Function_Call then
7404 exit;
7406 -- Otherwise the reference renames something which does not yield
7407 -- an abstract state or a whole object. Treat the reference as not
7408 -- having a proper entity for SPARK legality purposes.
7410 else
7411 Id := Empty;
7412 exit;
7413 end if;
7414 end loop;
7415 end if;
7417 return Id;
7418 end Entity_Of;
7420 --------------------------
7421 -- Examine_Array_Bounds --
7422 --------------------------
7424 procedure Examine_Array_Bounds
7425 (Typ : Entity_Id;
7426 All_Static : out Boolean;
7427 Has_Empty : out Boolean)
7429 function Is_OK_Static_Bound (Bound : Node_Id) return Boolean;
7430 -- Determine whether bound Bound is a suitable static bound
7432 ------------------------
7433 -- Is_OK_Static_Bound --
7434 ------------------------
7436 function Is_OK_Static_Bound (Bound : Node_Id) return Boolean is
7437 begin
7438 return
7439 not Error_Posted (Bound)
7440 and then Is_OK_Static_Expression (Bound);
7441 end Is_OK_Static_Bound;
7443 -- Local variables
7445 Hi_Bound : Node_Id;
7446 Index : Node_Id;
7447 Lo_Bound : Node_Id;
7449 -- Start of processing for Examine_Array_Bounds
7451 begin
7452 -- An unconstrained array type does not have static bounds, and it is
7453 -- not known whether they are empty or not.
7455 if not Is_Constrained (Typ) then
7456 All_Static := False;
7457 Has_Empty := False;
7459 -- A string literal has static bounds, and is not empty as long as it
7460 -- contains at least one character.
7462 elsif Ekind (Typ) = E_String_Literal_Subtype then
7463 All_Static := True;
7464 Has_Empty := String_Literal_Length (Typ) > 0;
7465 end if;
7467 -- Assume that all bounds are static and not empty
7469 All_Static := True;
7470 Has_Empty := False;
7472 -- Examine each index
7474 Index := First_Index (Typ);
7475 while Present (Index) loop
7476 if Is_Discrete_Type (Etype (Index)) then
7477 Get_Index_Bounds (Index, Lo_Bound, Hi_Bound);
7479 if Is_OK_Static_Bound (Lo_Bound)
7480 and then
7481 Is_OK_Static_Bound (Hi_Bound)
7482 then
7483 -- The static bounds produce an empty range
7485 if Is_Null_Range (Lo_Bound, Hi_Bound) then
7486 Has_Empty := True;
7487 end if;
7489 -- Otherwise at least one of the bounds is not static
7491 else
7492 All_Static := False;
7493 end if;
7495 -- Otherwise the index is non-discrete, therefore not static
7497 else
7498 All_Static := False;
7499 end if;
7501 Next_Index (Index);
7502 end loop;
7503 end Examine_Array_Bounds;
7505 --------------------------
7506 -- Explain_Limited_Type --
7507 --------------------------
7509 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
7510 C : Entity_Id;
7512 begin
7513 -- For array, component type must be limited
7515 if Is_Array_Type (T) then
7516 Error_Msg_Node_2 := T;
7517 Error_Msg_NE
7518 ("\component type& of type& is limited", N, Component_Type (T));
7519 Explain_Limited_Type (Component_Type (T), N);
7521 elsif Is_Record_Type (T) then
7523 -- No need for extra messages if explicit limited record
7525 if Is_Limited_Record (Base_Type (T)) then
7526 return;
7527 end if;
7529 -- Otherwise find a limited component. Check only components that
7530 -- come from source, or inherited components that appear in the
7531 -- source of the ancestor.
7533 C := First_Component (T);
7534 while Present (C) loop
7535 if Is_Limited_Type (Etype (C))
7536 and then
7537 (Comes_From_Source (C)
7538 or else
7539 (Present (Original_Record_Component (C))
7540 and then
7541 Comes_From_Source (Original_Record_Component (C))))
7542 then
7543 Error_Msg_Node_2 := T;
7544 Error_Msg_NE ("\component& of type& has limited type", N, C);
7545 Explain_Limited_Type (Etype (C), N);
7546 return;
7547 end if;
7549 Next_Component (C);
7550 end loop;
7552 -- The type may be declared explicitly limited, even if no component
7553 -- of it is limited, in which case we fall out of the loop.
7554 return;
7555 end if;
7556 end Explain_Limited_Type;
7558 ---------------------------------------
7559 -- Expression_Of_Expression_Function --
7560 ---------------------------------------
7562 function Expression_Of_Expression_Function
7563 (Subp : Entity_Id) return Node_Id
7565 Expr_Func : Node_Id;
7567 begin
7568 pragma Assert (Is_Expression_Function_Or_Completion (Subp));
7570 if Nkind (Original_Node (Subprogram_Spec (Subp))) =
7571 N_Expression_Function
7572 then
7573 Expr_Func := Original_Node (Subprogram_Spec (Subp));
7575 elsif Nkind (Original_Node (Subprogram_Body (Subp))) =
7576 N_Expression_Function
7577 then
7578 Expr_Func := Original_Node (Subprogram_Body (Subp));
7580 else
7581 pragma Assert (False);
7582 null;
7583 end if;
7585 return Original_Node (Expression (Expr_Func));
7586 end Expression_Of_Expression_Function;
7588 -------------------------------
7589 -- Extensions_Visible_Status --
7590 -------------------------------
7592 function Extensions_Visible_Status
7593 (Id : Entity_Id) return Extensions_Visible_Mode
7595 Arg : Node_Id;
7596 Decl : Node_Id;
7597 Expr : Node_Id;
7598 Prag : Node_Id;
7599 Subp : Entity_Id;
7601 begin
7602 -- When a formal parameter is subject to Extensions_Visible, the pragma
7603 -- is stored in the contract of related subprogram.
7605 if Is_Formal (Id) then
7606 Subp := Scope (Id);
7608 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
7609 Subp := Id;
7611 -- No other construct carries this pragma
7613 else
7614 return Extensions_Visible_None;
7615 end if;
7617 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
7619 -- In certain cases analysis may request the Extensions_Visible status
7620 -- of an expression function before the pragma has been analyzed yet.
7621 -- Inspect the declarative items after the expression function looking
7622 -- for the pragma (if any).
7624 if No (Prag) and then Is_Expression_Function (Subp) then
7625 Decl := Next (Unit_Declaration_Node (Subp));
7626 while Present (Decl) loop
7627 if Nkind (Decl) = N_Pragma
7628 and then Pragma_Name (Decl) = Name_Extensions_Visible
7629 then
7630 Prag := Decl;
7631 exit;
7633 -- A source construct ends the region where Extensions_Visible may
7634 -- appear, stop the traversal. An expanded expression function is
7635 -- no longer a source construct, but it must still be recognized.
7637 elsif Comes_From_Source (Decl)
7638 or else
7639 (Nkind_In (Decl, N_Subprogram_Body,
7640 N_Subprogram_Declaration)
7641 and then Is_Expression_Function (Defining_Entity (Decl)))
7642 then
7643 exit;
7644 end if;
7646 Next (Decl);
7647 end loop;
7648 end if;
7650 -- Extract the value from the Boolean expression (if any)
7652 if Present (Prag) then
7653 Arg := First (Pragma_Argument_Associations (Prag));
7655 if Present (Arg) then
7656 Expr := Get_Pragma_Arg (Arg);
7658 -- When the associated subprogram is an expression function, the
7659 -- argument of the pragma may not have been analyzed.
7661 if not Analyzed (Expr) then
7662 Preanalyze_And_Resolve (Expr, Standard_Boolean);
7663 end if;
7665 -- Guard against cascading errors when the argument of pragma
7666 -- Extensions_Visible is not a valid static Boolean expression.
7668 if Error_Posted (Expr) then
7669 return Extensions_Visible_None;
7671 elsif Is_True (Expr_Value (Expr)) then
7672 return Extensions_Visible_True;
7674 else
7675 return Extensions_Visible_False;
7676 end if;
7678 -- Otherwise the aspect or pragma defaults to True
7680 else
7681 return Extensions_Visible_True;
7682 end if;
7684 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
7685 -- directly specified. In SPARK code, its value defaults to "False".
7687 elsif SPARK_Mode = On then
7688 return Extensions_Visible_False;
7690 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
7691 -- "True".
7693 else
7694 return Extensions_Visible_True;
7695 end if;
7696 end Extensions_Visible_Status;
7698 -----------------
7699 -- Find_Actual --
7700 -----------------
7702 procedure Find_Actual
7703 (N : Node_Id;
7704 Formal : out Entity_Id;
7705 Call : out Node_Id)
7707 Context : constant Node_Id := Parent (N);
7708 Actual : Node_Id;
7709 Call_Nam : Node_Id;
7711 begin
7712 if Nkind_In (Context, N_Indexed_Component, N_Selected_Component)
7713 and then N = Prefix (Context)
7714 then
7715 Find_Actual (Context, Formal, Call);
7716 return;
7718 elsif Nkind (Context) = N_Parameter_Association
7719 and then N = Explicit_Actual_Parameter (Context)
7720 then
7721 Call := Parent (Context);
7723 elsif Nkind_In (Context, N_Entry_Call_Statement,
7724 N_Function_Call,
7725 N_Procedure_Call_Statement)
7726 then
7727 Call := Context;
7729 else
7730 Formal := Empty;
7731 Call := Empty;
7732 return;
7733 end if;
7735 -- If we have a call to a subprogram look for the parameter. Note that
7736 -- we exclude overloaded calls, since we don't know enough to be sure
7737 -- of giving the right answer in this case.
7739 if Nkind_In (Call, N_Entry_Call_Statement,
7740 N_Function_Call,
7741 N_Procedure_Call_Statement)
7742 then
7743 Call_Nam := Name (Call);
7745 -- A call to a protected or task entry appears as a selected
7746 -- component rather than an expanded name.
7748 if Nkind (Call_Nam) = N_Selected_Component then
7749 Call_Nam := Selector_Name (Call_Nam);
7750 end if;
7752 if Is_Entity_Name (Call_Nam)
7753 and then Present (Entity (Call_Nam))
7754 and then Is_Overloadable (Entity (Call_Nam))
7755 and then not Is_Overloaded (Call_Nam)
7756 then
7757 -- If node is name in call it is not an actual
7759 if N = Call_Nam then
7760 Formal := Empty;
7761 Call := Empty;
7762 return;
7763 end if;
7765 -- Fall here if we are definitely a parameter
7767 Actual := First_Actual (Call);
7768 Formal := First_Formal (Entity (Call_Nam));
7769 while Present (Formal) and then Present (Actual) loop
7770 if Actual = N then
7771 return;
7773 -- An actual that is the prefix in a prefixed call may have
7774 -- been rewritten in the call, after the deferred reference
7775 -- was collected. Check if sloc and kinds and names match.
7777 elsif Sloc (Actual) = Sloc (N)
7778 and then Nkind (Actual) = N_Identifier
7779 and then Nkind (Actual) = Nkind (N)
7780 and then Chars (Actual) = Chars (N)
7781 then
7782 return;
7784 else
7785 Actual := Next_Actual (Actual);
7786 Formal := Next_Formal (Formal);
7787 end if;
7788 end loop;
7789 end if;
7790 end if;
7792 -- Fall through here if we did not find matching actual
7794 Formal := Empty;
7795 Call := Empty;
7796 end Find_Actual;
7798 ---------------------------
7799 -- Find_Body_Discriminal --
7800 ---------------------------
7802 function Find_Body_Discriminal
7803 (Spec_Discriminant : Entity_Id) return Entity_Id
7805 Tsk : Entity_Id;
7806 Disc : Entity_Id;
7808 begin
7809 -- If expansion is suppressed, then the scope can be the concurrent type
7810 -- itself rather than a corresponding concurrent record type.
7812 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
7813 Tsk := Scope (Spec_Discriminant);
7815 else
7816 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
7818 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
7819 end if;
7821 -- Find discriminant of original concurrent type, and use its current
7822 -- discriminal, which is the renaming within the task/protected body.
7824 Disc := First_Discriminant (Tsk);
7825 while Present (Disc) loop
7826 if Chars (Disc) = Chars (Spec_Discriminant) then
7827 return Discriminal (Disc);
7828 end if;
7830 Next_Discriminant (Disc);
7831 end loop;
7833 -- That loop should always succeed in finding a matching entry and
7834 -- returning. Fatal error if not.
7836 raise Program_Error;
7837 end Find_Body_Discriminal;
7839 -------------------------------------
7840 -- Find_Corresponding_Discriminant --
7841 -------------------------------------
7843 function Find_Corresponding_Discriminant
7844 (Id : Node_Id;
7845 Typ : Entity_Id) return Entity_Id
7847 Par_Disc : Entity_Id;
7848 Old_Disc : Entity_Id;
7849 New_Disc : Entity_Id;
7851 begin
7852 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
7854 -- The original type may currently be private, and the discriminant
7855 -- only appear on its full view.
7857 if Is_Private_Type (Scope (Par_Disc))
7858 and then not Has_Discriminants (Scope (Par_Disc))
7859 and then Present (Full_View (Scope (Par_Disc)))
7860 then
7861 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
7862 else
7863 Old_Disc := First_Discriminant (Scope (Par_Disc));
7864 end if;
7866 if Is_Class_Wide_Type (Typ) then
7867 New_Disc := First_Discriminant (Root_Type (Typ));
7868 else
7869 New_Disc := First_Discriminant (Typ);
7870 end if;
7872 while Present (Old_Disc) and then Present (New_Disc) loop
7873 if Old_Disc = Par_Disc then
7874 return New_Disc;
7875 end if;
7877 Next_Discriminant (Old_Disc);
7878 Next_Discriminant (New_Disc);
7879 end loop;
7881 -- Should always find it
7883 raise Program_Error;
7884 end Find_Corresponding_Discriminant;
7886 -------------------
7887 -- Find_DIC_Type --
7888 -------------------
7890 function Find_DIC_Type (Typ : Entity_Id) return Entity_Id is
7891 Curr_Typ : Entity_Id;
7892 -- The current type being examined in the parent hierarchy traversal
7894 DIC_Typ : Entity_Id;
7895 -- The type which carries the DIC pragma. This variable denotes the
7896 -- partial view when private types are involved.
7898 Par_Typ : Entity_Id;
7899 -- The parent type of the current type. This variable denotes the full
7900 -- view when private types are involved.
7902 begin
7903 -- The input type defines its own DIC pragma, therefore it is the owner
7905 if Has_Own_DIC (Typ) then
7906 DIC_Typ := Typ;
7908 -- Otherwise the DIC pragma is inherited from a parent type
7910 else
7911 pragma Assert (Has_Inherited_DIC (Typ));
7913 -- Climb the parent chain
7915 Curr_Typ := Typ;
7916 loop
7917 -- Inspect the parent type. Do not consider subtypes as they
7918 -- inherit the DIC attributes from their base types.
7920 DIC_Typ := Base_Type (Etype (Curr_Typ));
7922 -- Look at the full view of a private type because the type may
7923 -- have a hidden parent introduced in the full view.
7925 Par_Typ := DIC_Typ;
7927 if Is_Private_Type (Par_Typ)
7928 and then Present (Full_View (Par_Typ))
7929 then
7930 Par_Typ := Full_View (Par_Typ);
7931 end if;
7933 -- Stop the climb once the nearest parent type which defines a DIC
7934 -- pragma of its own is encountered or when the root of the parent
7935 -- chain is reached.
7937 exit when Has_Own_DIC (DIC_Typ) or else Curr_Typ = Par_Typ;
7939 Curr_Typ := Par_Typ;
7940 end loop;
7941 end if;
7943 return DIC_Typ;
7944 end Find_DIC_Type;
7946 ----------------------------------
7947 -- Find_Enclosing_Iterator_Loop --
7948 ----------------------------------
7950 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
7951 Constr : Node_Id;
7952 S : Entity_Id;
7954 begin
7955 -- Traverse the scope chain looking for an iterator loop. Such loops are
7956 -- usually transformed into blocks, hence the use of Original_Node.
7958 S := Id;
7959 while Present (S) and then S /= Standard_Standard loop
7960 if Ekind (S) = E_Loop
7961 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
7962 then
7963 Constr := Original_Node (Label_Construct (Parent (S)));
7965 if Nkind (Constr) = N_Loop_Statement
7966 and then Present (Iteration_Scheme (Constr))
7967 and then Nkind (Iterator_Specification
7968 (Iteration_Scheme (Constr))) =
7969 N_Iterator_Specification
7970 then
7971 return S;
7972 end if;
7973 end if;
7975 S := Scope (S);
7976 end loop;
7978 return Empty;
7979 end Find_Enclosing_Iterator_Loop;
7981 --------------------------
7982 -- Find_Enclosing_Scope --
7983 --------------------------
7985 function Find_Enclosing_Scope (N : Node_Id) return Entity_Id is
7986 Par : Node_Id;
7988 begin
7989 -- Examine the parent chain looking for a construct which defines a
7990 -- scope.
7992 Par := Parent (N);
7993 while Present (Par) loop
7994 case Nkind (Par) is
7996 -- The construct denotes a declaration, the proper scope is its
7997 -- entity.
7999 when N_Entry_Declaration
8000 | N_Expression_Function
8001 | N_Full_Type_Declaration
8002 | N_Generic_Package_Declaration
8003 | N_Generic_Subprogram_Declaration
8004 | N_Package_Declaration
8005 | N_Private_Extension_Declaration
8006 | N_Protected_Type_Declaration
8007 | N_Single_Protected_Declaration
8008 | N_Single_Task_Declaration
8009 | N_Subprogram_Declaration
8010 | N_Task_Type_Declaration
8012 return Defining_Entity (Par);
8014 -- The construct denotes a body, the proper scope is the entity of
8015 -- the corresponding spec or that of the body if the body does not
8016 -- complete a previous declaration.
8018 when N_Entry_Body
8019 | N_Package_Body
8020 | N_Protected_Body
8021 | N_Subprogram_Body
8022 | N_Task_Body
8024 return Unique_Defining_Entity (Par);
8026 -- Special cases
8028 -- Blocks carry either a source or an internally-generated scope,
8029 -- unless the block is a byproduct of exception handling.
8031 when N_Block_Statement =>
8032 if not Exception_Junk (Par) then
8033 return Entity (Identifier (Par));
8034 end if;
8036 -- Loops carry an internally-generated scope
8038 when N_Loop_Statement =>
8039 return Entity (Identifier (Par));
8041 -- Extended return statements carry an internally-generated scope
8043 when N_Extended_Return_Statement =>
8044 return Return_Statement_Entity (Par);
8046 -- A traversal from a subunit continues via the corresponding stub
8048 when N_Subunit =>
8049 Par := Corresponding_Stub (Par);
8051 when others =>
8052 null;
8053 end case;
8055 Par := Parent (Par);
8056 end loop;
8058 return Standard_Standard;
8059 end Find_Enclosing_Scope;
8061 ------------------------------------
8062 -- Find_Loop_In_Conditional_Block --
8063 ------------------------------------
8065 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
8066 Stmt : Node_Id;
8068 begin
8069 Stmt := N;
8071 if Nkind (Stmt) = N_If_Statement then
8072 Stmt := First (Then_Statements (Stmt));
8073 end if;
8075 pragma Assert (Nkind (Stmt) = N_Block_Statement);
8077 -- Inspect the statements of the conditional block. In general the loop
8078 -- should be the first statement in the statement sequence of the block,
8079 -- but the finalization machinery may have introduced extra object
8080 -- declarations.
8082 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
8083 while Present (Stmt) loop
8084 if Nkind (Stmt) = N_Loop_Statement then
8085 return Stmt;
8086 end if;
8088 Next (Stmt);
8089 end loop;
8091 -- The expansion of attribute 'Loop_Entry produced a malformed block
8093 raise Program_Error;
8094 end Find_Loop_In_Conditional_Block;
8096 --------------------------
8097 -- Find_Overlaid_Entity --
8098 --------------------------
8100 procedure Find_Overlaid_Entity
8101 (N : Node_Id;
8102 Ent : out Entity_Id;
8103 Off : out Boolean)
8105 Expr : Node_Id;
8107 begin
8108 -- We are looking for one of the two following forms:
8110 -- for X'Address use Y'Address
8112 -- or
8114 -- Const : constant Address := expr;
8115 -- ...
8116 -- for X'Address use Const;
8118 -- In the second case, the expr is either Y'Address, or recursively a
8119 -- constant that eventually references Y'Address.
8121 Ent := Empty;
8122 Off := False;
8124 if Nkind (N) = N_Attribute_Definition_Clause
8125 and then Chars (N) = Name_Address
8126 then
8127 Expr := Expression (N);
8129 -- This loop checks the form of the expression for Y'Address,
8130 -- using recursion to deal with intermediate constants.
8132 loop
8133 -- Check for Y'Address
8135 if Nkind (Expr) = N_Attribute_Reference
8136 and then Attribute_Name (Expr) = Name_Address
8137 then
8138 Expr := Prefix (Expr);
8139 exit;
8141 -- Check for Const where Const is a constant entity
8143 elsif Is_Entity_Name (Expr)
8144 and then Ekind (Entity (Expr)) = E_Constant
8145 then
8146 Expr := Constant_Value (Entity (Expr));
8148 -- Anything else does not need checking
8150 else
8151 return;
8152 end if;
8153 end loop;
8155 -- This loop checks the form of the prefix for an entity, using
8156 -- recursion to deal with intermediate components.
8158 loop
8159 -- Check for Y where Y is an entity
8161 if Is_Entity_Name (Expr) then
8162 Ent := Entity (Expr);
8163 return;
8165 -- Check for components
8167 elsif
8168 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
8169 then
8170 Expr := Prefix (Expr);
8171 Off := True;
8173 -- Anything else does not need checking
8175 else
8176 return;
8177 end if;
8178 end loop;
8179 end if;
8180 end Find_Overlaid_Entity;
8182 -------------------------
8183 -- Find_Parameter_Type --
8184 -------------------------
8186 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
8187 begin
8188 if Nkind (Param) /= N_Parameter_Specification then
8189 return Empty;
8191 -- For an access parameter, obtain the type from the formal entity
8192 -- itself, because access to subprogram nodes do not carry a type.
8193 -- Shouldn't we always use the formal entity ???
8195 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
8196 return Etype (Defining_Identifier (Param));
8198 else
8199 return Etype (Parameter_Type (Param));
8200 end if;
8201 end Find_Parameter_Type;
8203 -----------------------------------
8204 -- Find_Placement_In_State_Space --
8205 -----------------------------------
8207 procedure Find_Placement_In_State_Space
8208 (Item_Id : Entity_Id;
8209 Placement : out State_Space_Kind;
8210 Pack_Id : out Entity_Id)
8212 Context : Entity_Id;
8214 begin
8215 -- Assume that the item does not appear in the state space of a package
8217 Placement := Not_In_Package;
8218 Pack_Id := Empty;
8220 -- Climb the scope stack and examine the enclosing context
8222 Context := Scope (Item_Id);
8223 while Present (Context) and then Context /= Standard_Standard loop
8224 if Is_Package_Or_Generic_Package (Context) then
8225 Pack_Id := Context;
8227 -- A package body is a cut off point for the traversal as the item
8228 -- cannot be visible to the outside from this point on. Note that
8229 -- this test must be done first as a body is also classified as a
8230 -- private part.
8232 if In_Package_Body (Context) then
8233 Placement := Body_State_Space;
8234 return;
8236 -- The private part of a package is a cut off point for the
8237 -- traversal as the item cannot be visible to the outside from
8238 -- this point on.
8240 elsif In_Private_Part (Context) then
8241 Placement := Private_State_Space;
8242 return;
8244 -- When the item appears in the visible state space of a package,
8245 -- continue to climb the scope stack as this may not be the final
8246 -- state space.
8248 else
8249 Placement := Visible_State_Space;
8251 -- The visible state space of a child unit acts as the proper
8252 -- placement of an item.
8254 if Is_Child_Unit (Context) then
8255 return;
8256 end if;
8257 end if;
8259 -- The item or its enclosing package appear in a construct that has
8260 -- no state space.
8262 else
8263 Placement := Not_In_Package;
8264 return;
8265 end if;
8267 Context := Scope (Context);
8268 end loop;
8269 end Find_Placement_In_State_Space;
8271 ------------------------
8272 -- Find_Specific_Type --
8273 ------------------------
8275 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
8276 Typ : Entity_Id := Root_Type (CW);
8278 begin
8279 if Ekind (Typ) = E_Incomplete_Type then
8280 if From_Limited_With (Typ) then
8281 Typ := Non_Limited_View (Typ);
8282 else
8283 Typ := Full_View (Typ);
8284 end if;
8285 end if;
8287 if Is_Private_Type (Typ)
8288 and then not Is_Tagged_Type (Typ)
8289 and then Present (Full_View (Typ))
8290 then
8291 return Full_View (Typ);
8292 else
8293 return Typ;
8294 end if;
8295 end Find_Specific_Type;
8297 -----------------------------
8298 -- Find_Static_Alternative --
8299 -----------------------------
8301 function Find_Static_Alternative (N : Node_Id) return Node_Id is
8302 Expr : constant Node_Id := Expression (N);
8303 Val : constant Uint := Expr_Value (Expr);
8304 Alt : Node_Id;
8305 Choice : Node_Id;
8307 begin
8308 Alt := First (Alternatives (N));
8310 Search : loop
8311 if Nkind (Alt) /= N_Pragma then
8312 Choice := First (Discrete_Choices (Alt));
8313 while Present (Choice) loop
8315 -- Others choice, always matches
8317 if Nkind (Choice) = N_Others_Choice then
8318 exit Search;
8320 -- Range, check if value is in the range
8322 elsif Nkind (Choice) = N_Range then
8323 exit Search when
8324 Val >= Expr_Value (Low_Bound (Choice))
8325 and then
8326 Val <= Expr_Value (High_Bound (Choice));
8328 -- Choice is a subtype name. Note that we know it must
8329 -- be a static subtype, since otherwise it would have
8330 -- been diagnosed as illegal.
8332 elsif Is_Entity_Name (Choice)
8333 and then Is_Type (Entity (Choice))
8334 then
8335 exit Search when Is_In_Range (Expr, Etype (Choice),
8336 Assume_Valid => False);
8338 -- Choice is a subtype indication
8340 elsif Nkind (Choice) = N_Subtype_Indication then
8341 declare
8342 C : constant Node_Id := Constraint (Choice);
8343 R : constant Node_Id := Range_Expression (C);
8345 begin
8346 exit Search when
8347 Val >= Expr_Value (Low_Bound (R))
8348 and then
8349 Val <= Expr_Value (High_Bound (R));
8350 end;
8352 -- Choice is a simple expression
8354 else
8355 exit Search when Val = Expr_Value (Choice);
8356 end if;
8358 Next (Choice);
8359 end loop;
8360 end if;
8362 Next (Alt);
8363 pragma Assert (Present (Alt));
8364 end loop Search;
8366 -- The above loop *must* terminate by finding a match, since we know the
8367 -- case statement is valid, and the value of the expression is known at
8368 -- compile time. When we fall out of the loop, Alt points to the
8369 -- alternative that we know will be selected at run time.
8371 return Alt;
8372 end Find_Static_Alternative;
8374 ------------------
8375 -- First_Actual --
8376 ------------------
8378 function First_Actual (Node : Node_Id) return Node_Id is
8379 N : Node_Id;
8381 begin
8382 if No (Parameter_Associations (Node)) then
8383 return Empty;
8384 end if;
8386 N := First (Parameter_Associations (Node));
8388 if Nkind (N) = N_Parameter_Association then
8389 return First_Named_Actual (Node);
8390 else
8391 return N;
8392 end if;
8393 end First_Actual;
8395 ------------------
8396 -- First_Global --
8397 ------------------
8399 function First_Global
8400 (Subp : Entity_Id;
8401 Global_Mode : Name_Id;
8402 Refined : Boolean := False) return Node_Id
8404 function First_From_Global_List
8405 (List : Node_Id;
8406 Global_Mode : Name_Id := Name_Input) return Entity_Id;
8407 -- Get the first item with suitable mode from List
8409 ----------------------------
8410 -- First_From_Global_List --
8411 ----------------------------
8413 function First_From_Global_List
8414 (List : Node_Id;
8415 Global_Mode : Name_Id := Name_Input) return Entity_Id
8417 Assoc : Node_Id;
8419 begin
8420 -- Empty list (no global items)
8422 if Nkind (List) = N_Null then
8423 return Empty;
8425 -- Single global item declaration (only input items)
8427 elsif Nkind_In (List, N_Expanded_Name,
8428 N_Identifier,
8429 N_Selected_Component)
8430 then
8431 if Global_Mode = Name_Input then
8432 return List;
8433 else
8434 return Empty;
8435 end if;
8437 -- Simple global list (only input items) or moded global list
8438 -- declaration.
8440 elsif Nkind (List) = N_Aggregate then
8441 if Present (Expressions (List)) then
8442 if Global_Mode = Name_Input then
8443 return First (Expressions (List));
8444 else
8445 return Empty;
8446 end if;
8448 else
8449 Assoc := First (Component_Associations (List));
8450 while Present (Assoc) loop
8452 -- When we find the desired mode in an association, call
8453 -- recursively First_From_Global_List as if the mode was
8454 -- Name_Input, in order to reuse the existing machinery
8455 -- for the other cases.
8457 if Chars (First (Choices (Assoc))) = Global_Mode then
8458 return First_From_Global_List (Expression (Assoc));
8459 end if;
8461 Next (Assoc);
8462 end loop;
8464 return Empty;
8465 end if;
8467 -- To accommodate partial decoration of disabled SPARK features,
8468 -- this routine may be called with illegal input. If this is the
8469 -- case, do not raise Program_Error.
8471 else
8472 return Empty;
8473 end if;
8474 end First_From_Global_List;
8476 -- Local variables
8478 Global : Node_Id := Empty;
8479 Body_Id : Entity_Id;
8481 begin
8482 pragma Assert (Global_Mode = Name_Input
8483 or else Global_Mode = Name_Output
8484 or else Global_Mode = Name_In_Out
8485 or else Global_Mode = Name_Proof_In);
8487 -- Retrieve the suitable pragma Global or Refined_Global. In the second
8488 -- case, it can only be located on the body entity.
8490 if Refined then
8491 Body_Id := Subprogram_Body_Entity (Subp);
8492 if Present (Body_Id) then
8493 Global := Get_Pragma (Body_Id, Pragma_Refined_Global);
8494 end if;
8495 else
8496 Global := Get_Pragma (Subp, Pragma_Global);
8497 end if;
8499 -- No corresponding global if pragma is not present
8501 if No (Global) then
8502 return Empty;
8504 -- Otherwise retrieve the corresponding list of items depending on the
8505 -- Global_Mode.
8507 else
8508 return First_From_Global_List
8509 (Expression (Get_Argument (Global, Subp)), Global_Mode);
8510 end if;
8511 end First_Global;
8513 -------------
8514 -- Fix_Msg --
8515 -------------
8517 function Fix_Msg (Id : Entity_Id; Msg : String) return String is
8518 Is_Task : constant Boolean :=
8519 Ekind_In (Id, E_Task_Body, E_Task_Type)
8520 or else Is_Single_Task_Object (Id);
8521 Msg_Last : constant Natural := Msg'Last;
8522 Msg_Index : Natural;
8523 Res : String (Msg'Range) := (others => ' ');
8524 Res_Index : Natural;
8526 begin
8527 -- Copy all characters from the input message Msg to result Res with
8528 -- suitable replacements.
8530 Msg_Index := Msg'First;
8531 Res_Index := Res'First;
8532 while Msg_Index <= Msg_Last loop
8534 -- Replace "subprogram" with a different word
8536 if Msg_Index <= Msg_Last - 10
8537 and then Msg (Msg_Index .. Msg_Index + 9) = "subprogram"
8538 then
8539 if Ekind_In (Id, E_Entry, E_Entry_Family) then
8540 Res (Res_Index .. Res_Index + 4) := "entry";
8541 Res_Index := Res_Index + 5;
8543 elsif Is_Task then
8544 Res (Res_Index .. Res_Index + 8) := "task type";
8545 Res_Index := Res_Index + 9;
8547 else
8548 Res (Res_Index .. Res_Index + 9) := "subprogram";
8549 Res_Index := Res_Index + 10;
8550 end if;
8552 Msg_Index := Msg_Index + 10;
8554 -- Replace "protected" with a different word
8556 elsif Msg_Index <= Msg_Last - 9
8557 and then Msg (Msg_Index .. Msg_Index + 8) = "protected"
8558 and then Is_Task
8559 then
8560 Res (Res_Index .. Res_Index + 3) := "task";
8561 Res_Index := Res_Index + 4;
8562 Msg_Index := Msg_Index + 9;
8564 -- Otherwise copy the character
8566 else
8567 Res (Res_Index) := Msg (Msg_Index);
8568 Msg_Index := Msg_Index + 1;
8569 Res_Index := Res_Index + 1;
8570 end if;
8571 end loop;
8573 return Res (Res'First .. Res_Index - 1);
8574 end Fix_Msg;
8576 -------------------------
8577 -- From_Nested_Package --
8578 -------------------------
8580 function From_Nested_Package (T : Entity_Id) return Boolean is
8581 Pack : constant Entity_Id := Scope (T);
8583 begin
8584 return
8585 Ekind (Pack) = E_Package
8586 and then not Is_Frozen (Pack)
8587 and then not Scope_Within_Or_Same (Current_Scope, Pack)
8588 and then In_Open_Scopes (Scope (Pack));
8589 end From_Nested_Package;
8591 -----------------------
8592 -- Gather_Components --
8593 -----------------------
8595 procedure Gather_Components
8596 (Typ : Entity_Id;
8597 Comp_List : Node_Id;
8598 Governed_By : List_Id;
8599 Into : Elist_Id;
8600 Report_Errors : out Boolean)
8602 Assoc : Node_Id;
8603 Variant : Node_Id;
8604 Discrete_Choice : Node_Id;
8605 Comp_Item : Node_Id;
8607 Discrim : Entity_Id;
8608 Discrim_Name : Node_Id;
8609 Discrim_Value : Node_Id;
8611 begin
8612 Report_Errors := False;
8614 if No (Comp_List) or else Null_Present (Comp_List) then
8615 return;
8617 elsif Present (Component_Items (Comp_List)) then
8618 Comp_Item := First (Component_Items (Comp_List));
8620 else
8621 Comp_Item := Empty;
8622 end if;
8624 while Present (Comp_Item) loop
8626 -- Skip the tag of a tagged record, the interface tags, as well
8627 -- as all items that are not user components (anonymous types,
8628 -- rep clauses, Parent field, controller field).
8630 if Nkind (Comp_Item) = N_Component_Declaration then
8631 declare
8632 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
8633 begin
8634 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
8635 Append_Elmt (Comp, Into);
8636 end if;
8637 end;
8638 end if;
8640 Next (Comp_Item);
8641 end loop;
8643 if No (Variant_Part (Comp_List)) then
8644 return;
8645 else
8646 Discrim_Name := Name (Variant_Part (Comp_List));
8647 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
8648 end if;
8650 -- Look for the discriminant that governs this variant part.
8651 -- The discriminant *must* be in the Governed_By List
8653 Assoc := First (Governed_By);
8654 Find_Constraint : loop
8655 Discrim := First (Choices (Assoc));
8656 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
8657 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
8658 and then
8659 Chars (Corresponding_Discriminant (Entity (Discrim))) =
8660 Chars (Discrim_Name))
8661 or else Chars (Original_Record_Component (Entity (Discrim)))
8662 = Chars (Discrim_Name);
8664 if No (Next (Assoc)) then
8665 if not Is_Constrained (Typ)
8666 and then Is_Derived_Type (Typ)
8667 and then Present (Stored_Constraint (Typ))
8668 then
8669 -- If the type is a tagged type with inherited discriminants,
8670 -- use the stored constraint on the parent in order to find
8671 -- the values of discriminants that are otherwise hidden by an
8672 -- explicit constraint. Renamed discriminants are handled in
8673 -- the code above.
8675 -- If several parent discriminants are renamed by a single
8676 -- discriminant of the derived type, the call to obtain the
8677 -- Corresponding_Discriminant field only retrieves the last
8678 -- of them. We recover the constraint on the others from the
8679 -- Stored_Constraint as well.
8681 declare
8682 D : Entity_Id;
8683 C : Elmt_Id;
8685 begin
8686 D := First_Discriminant (Etype (Typ));
8687 C := First_Elmt (Stored_Constraint (Typ));
8688 while Present (D) and then Present (C) loop
8689 if Chars (Discrim_Name) = Chars (D) then
8690 if Is_Entity_Name (Node (C))
8691 and then Entity (Node (C)) = Entity (Discrim)
8692 then
8693 -- D is renamed by Discrim, whose value is given in
8694 -- Assoc.
8696 null;
8698 else
8699 Assoc :=
8700 Make_Component_Association (Sloc (Typ),
8701 New_List
8702 (New_Occurrence_Of (D, Sloc (Typ))),
8703 Duplicate_Subexpr_No_Checks (Node (C)));
8704 end if;
8705 exit Find_Constraint;
8706 end if;
8708 Next_Discriminant (D);
8709 Next_Elmt (C);
8710 end loop;
8711 end;
8712 end if;
8713 end if;
8715 if No (Next (Assoc)) then
8716 Error_Msg_NE (" missing value for discriminant&",
8717 First (Governed_By), Discrim_Name);
8718 Report_Errors := True;
8719 return;
8720 end if;
8722 Next (Assoc);
8723 end loop Find_Constraint;
8725 Discrim_Value := Expression (Assoc);
8727 if not Is_OK_Static_Expression (Discrim_Value) then
8729 -- If the variant part is governed by a discriminant of the type
8730 -- this is an error. If the variant part and the discriminant are
8731 -- inherited from an ancestor this is legal (AI05-120) unless the
8732 -- components are being gathered for an aggregate, in which case
8733 -- the caller must check Report_Errors.
8735 if Scope (Original_Record_Component
8736 ((Entity (First (Choices (Assoc)))))) = Typ
8737 then
8738 Error_Msg_FE
8739 ("value for discriminant & must be static!",
8740 Discrim_Value, Discrim);
8741 Why_Not_Static (Discrim_Value);
8742 end if;
8744 Report_Errors := True;
8745 return;
8746 end if;
8748 Search_For_Discriminant_Value : declare
8749 Low : Node_Id;
8750 High : Node_Id;
8752 UI_High : Uint;
8753 UI_Low : Uint;
8754 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
8756 begin
8757 Find_Discrete_Value : while Present (Variant) loop
8758 Discrete_Choice := First (Discrete_Choices (Variant));
8759 while Present (Discrete_Choice) loop
8760 exit Find_Discrete_Value when
8761 Nkind (Discrete_Choice) = N_Others_Choice;
8763 Get_Index_Bounds (Discrete_Choice, Low, High);
8765 UI_Low := Expr_Value (Low);
8766 UI_High := Expr_Value (High);
8768 exit Find_Discrete_Value when
8769 UI_Low <= UI_Discrim_Value
8770 and then
8771 UI_High >= UI_Discrim_Value;
8773 Next (Discrete_Choice);
8774 end loop;
8776 Next_Non_Pragma (Variant);
8777 end loop Find_Discrete_Value;
8778 end Search_For_Discriminant_Value;
8780 -- The case statement must include a variant that corresponds to the
8781 -- value of the discriminant, unless the discriminant type has a
8782 -- static predicate. In that case the absence of an others_choice that
8783 -- would cover this value becomes a run-time error (3.8,1 (21.1/2)).
8785 if No (Variant)
8786 and then not Has_Static_Predicate (Etype (Discrim_Name))
8787 then
8788 Error_Msg_NE
8789 ("value of discriminant & is out of range", Discrim_Value, Discrim);
8790 Report_Errors := True;
8791 return;
8792 end if;
8794 -- If we have found the corresponding choice, recursively add its
8795 -- components to the Into list. The nested components are part of
8796 -- the same record type.
8798 if Present (Variant) then
8799 Gather_Components
8800 (Typ, Component_List (Variant), Governed_By, Into, Report_Errors);
8801 end if;
8802 end Gather_Components;
8804 ------------------------
8805 -- Get_Actual_Subtype --
8806 ------------------------
8808 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
8809 Typ : constant Entity_Id := Etype (N);
8810 Utyp : Entity_Id := Underlying_Type (Typ);
8811 Decl : Node_Id;
8812 Atyp : Entity_Id;
8814 begin
8815 if No (Utyp) then
8816 Utyp := Typ;
8817 end if;
8819 -- If what we have is an identifier that references a subprogram
8820 -- formal, or a variable or constant object, then we get the actual
8821 -- subtype from the referenced entity if one has been built.
8823 if Nkind (N) = N_Identifier
8824 and then
8825 (Is_Formal (Entity (N))
8826 or else Ekind (Entity (N)) = E_Constant
8827 or else Ekind (Entity (N)) = E_Variable)
8828 and then Present (Actual_Subtype (Entity (N)))
8829 then
8830 return Actual_Subtype (Entity (N));
8832 -- Actual subtype of unchecked union is always itself. We never need
8833 -- the "real" actual subtype. If we did, we couldn't get it anyway
8834 -- because the discriminant is not available. The restrictions on
8835 -- Unchecked_Union are designed to make sure that this is OK.
8837 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
8838 return Typ;
8840 -- Here for the unconstrained case, we must find actual subtype
8841 -- No actual subtype is available, so we must build it on the fly.
8843 -- Checking the type, not the underlying type, for constrainedness
8844 -- seems to be necessary. Maybe all the tests should be on the type???
8846 elsif (not Is_Constrained (Typ))
8847 and then (Is_Array_Type (Utyp)
8848 or else (Is_Record_Type (Utyp)
8849 and then Has_Discriminants (Utyp)))
8850 and then not Has_Unknown_Discriminants (Utyp)
8851 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
8852 then
8853 -- Nothing to do if in spec expression (why not???)
8855 if In_Spec_Expression then
8856 return Typ;
8858 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
8860 -- If the type has no discriminants, there is no subtype to
8861 -- build, even if the underlying type is discriminated.
8863 return Typ;
8865 -- Else build the actual subtype
8867 else
8868 Decl := Build_Actual_Subtype (Typ, N);
8869 Atyp := Defining_Identifier (Decl);
8871 -- If Build_Actual_Subtype generated a new declaration then use it
8873 if Atyp /= Typ then
8875 -- The actual subtype is an Itype, so analyze the declaration,
8876 -- but do not attach it to the tree, to get the type defined.
8878 Set_Parent (Decl, N);
8879 Set_Is_Itype (Atyp);
8880 Analyze (Decl, Suppress => All_Checks);
8881 Set_Associated_Node_For_Itype (Atyp, N);
8882 Set_Has_Delayed_Freeze (Atyp, False);
8884 -- We need to freeze the actual subtype immediately. This is
8885 -- needed, because otherwise this Itype will not get frozen
8886 -- at all, and it is always safe to freeze on creation because
8887 -- any associated types must be frozen at this point.
8889 Freeze_Itype (Atyp, N);
8890 return Atyp;
8892 -- Otherwise we did not build a declaration, so return original
8894 else
8895 return Typ;
8896 end if;
8897 end if;
8899 -- For all remaining cases, the actual subtype is the same as
8900 -- the nominal type.
8902 else
8903 return Typ;
8904 end if;
8905 end Get_Actual_Subtype;
8907 -------------------------------------
8908 -- Get_Actual_Subtype_If_Available --
8909 -------------------------------------
8911 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
8912 Typ : constant Entity_Id := Etype (N);
8914 begin
8915 -- If what we have is an identifier that references a subprogram
8916 -- formal, or a variable or constant object, then we get the actual
8917 -- subtype from the referenced entity if one has been built.
8919 if Nkind (N) = N_Identifier
8920 and then
8921 (Is_Formal (Entity (N))
8922 or else Ekind (Entity (N)) = E_Constant
8923 or else Ekind (Entity (N)) = E_Variable)
8924 and then Present (Actual_Subtype (Entity (N)))
8925 then
8926 return Actual_Subtype (Entity (N));
8928 -- Otherwise the Etype of N is returned unchanged
8930 else
8931 return Typ;
8932 end if;
8933 end Get_Actual_Subtype_If_Available;
8935 ------------------------
8936 -- Get_Body_From_Stub --
8937 ------------------------
8939 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
8940 begin
8941 return Proper_Body (Unit (Library_Unit (N)));
8942 end Get_Body_From_Stub;
8944 ---------------------
8945 -- Get_Cursor_Type --
8946 ---------------------
8948 function Get_Cursor_Type
8949 (Aspect : Node_Id;
8950 Typ : Entity_Id) return Entity_Id
8952 Assoc : Node_Id;
8953 Func : Entity_Id;
8954 First_Op : Entity_Id;
8955 Cursor : Entity_Id;
8957 begin
8958 -- If error already detected, return
8960 if Error_Posted (Aspect) then
8961 return Any_Type;
8962 end if;
8964 -- The cursor type for an Iterable aspect is the return type of a
8965 -- non-overloaded First primitive operation. Locate association for
8966 -- First.
8968 Assoc := First (Component_Associations (Expression (Aspect)));
8969 First_Op := Any_Id;
8970 while Present (Assoc) loop
8971 if Chars (First (Choices (Assoc))) = Name_First then
8972 First_Op := Expression (Assoc);
8973 exit;
8974 end if;
8976 Next (Assoc);
8977 end loop;
8979 if First_Op = Any_Id then
8980 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
8981 return Any_Type;
8982 end if;
8984 Cursor := Any_Type;
8986 -- Locate function with desired name and profile in scope of type
8987 -- In the rare case where the type is an integer type, a base type
8988 -- is created for it, check that the base type of the first formal
8989 -- of First matches the base type of the domain.
8991 Func := First_Entity (Scope (Typ));
8992 while Present (Func) loop
8993 if Chars (Func) = Chars (First_Op)
8994 and then Ekind (Func) = E_Function
8995 and then Present (First_Formal (Func))
8996 and then Base_Type (Etype (First_Formal (Func))) = Base_Type (Typ)
8997 and then No (Next_Formal (First_Formal (Func)))
8998 then
8999 if Cursor /= Any_Type then
9000 Error_Msg_N
9001 ("Operation First for iterable type must be unique", Aspect);
9002 return Any_Type;
9003 else
9004 Cursor := Etype (Func);
9005 end if;
9006 end if;
9008 Next_Entity (Func);
9009 end loop;
9011 -- If not found, no way to resolve remaining primitives.
9013 if Cursor = Any_Type then
9014 Error_Msg_N
9015 ("No legal primitive operation First for Iterable type", Aspect);
9016 end if;
9018 return Cursor;
9019 end Get_Cursor_Type;
9021 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
9022 begin
9023 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
9024 end Get_Cursor_Type;
9026 -------------------------------
9027 -- Get_Default_External_Name --
9028 -------------------------------
9030 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
9031 begin
9032 Get_Decoded_Name_String (Chars (E));
9034 if Opt.External_Name_Imp_Casing = Uppercase then
9035 Set_Casing (All_Upper_Case);
9036 else
9037 Set_Casing (All_Lower_Case);
9038 end if;
9040 return
9041 Make_String_Literal (Sloc (E),
9042 Strval => String_From_Name_Buffer);
9043 end Get_Default_External_Name;
9045 --------------------------
9046 -- Get_Enclosing_Object --
9047 --------------------------
9049 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
9050 begin
9051 if Is_Entity_Name (N) then
9052 return Entity (N);
9053 else
9054 case Nkind (N) is
9055 when N_Indexed_Component
9056 | N_Selected_Component
9057 | N_Slice
9059 -- If not generating code, a dereference may be left implicit.
9060 -- In thoses cases, return Empty.
9062 if Is_Access_Type (Etype (Prefix (N))) then
9063 return Empty;
9064 else
9065 return Get_Enclosing_Object (Prefix (N));
9066 end if;
9068 when N_Type_Conversion =>
9069 return Get_Enclosing_Object (Expression (N));
9071 when others =>
9072 return Empty;
9073 end case;
9074 end if;
9075 end Get_Enclosing_Object;
9077 ---------------------------
9078 -- Get_Enum_Lit_From_Pos --
9079 ---------------------------
9081 function Get_Enum_Lit_From_Pos
9082 (T : Entity_Id;
9083 Pos : Uint;
9084 Loc : Source_Ptr) return Node_Id
9086 Btyp : Entity_Id := Base_Type (T);
9087 Lit : Node_Id;
9088 LLoc : Source_Ptr;
9090 begin
9091 -- In the case where the literal is of type Character, Wide_Character
9092 -- or Wide_Wide_Character or of a type derived from them, there needs
9093 -- to be some special handling since there is no explicit chain of
9094 -- literals to search. Instead, an N_Character_Literal node is created
9095 -- with the appropriate Char_Code and Chars fields.
9097 if Is_Standard_Character_Type (T) then
9098 Set_Character_Literal_Name (UI_To_CC (Pos));
9100 return
9101 Make_Character_Literal (Loc,
9102 Chars => Name_Find,
9103 Char_Literal_Value => Pos);
9105 -- For all other cases, we have a complete table of literals, and
9106 -- we simply iterate through the chain of literal until the one
9107 -- with the desired position value is found.
9109 else
9110 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
9111 Btyp := Full_View (Btyp);
9112 end if;
9114 Lit := First_Literal (Btyp);
9116 -- Position in the enumeration type starts at 0
9118 if UI_To_Int (Pos) < 0 then
9119 raise Constraint_Error;
9120 end if;
9122 for J in 1 .. UI_To_Int (Pos) loop
9123 Next_Literal (Lit);
9125 -- If Lit is Empty, Pos is not in range, so raise Constraint_Error
9126 -- inside the loop to avoid calling Next_Literal on Empty.
9128 if No (Lit) then
9129 raise Constraint_Error;
9130 end if;
9131 end loop;
9133 -- Create a new node from Lit, with source location provided by Loc
9134 -- if not equal to No_Location, or by copying the source location of
9135 -- Lit otherwise.
9137 LLoc := Loc;
9139 if LLoc = No_Location then
9140 LLoc := Sloc (Lit);
9141 end if;
9143 return New_Occurrence_Of (Lit, LLoc);
9144 end if;
9145 end Get_Enum_Lit_From_Pos;
9147 ------------------------
9148 -- Get_Generic_Entity --
9149 ------------------------
9151 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
9152 Ent : constant Entity_Id := Entity (Name (N));
9153 begin
9154 if Present (Renamed_Object (Ent)) then
9155 return Renamed_Object (Ent);
9156 else
9157 return Ent;
9158 end if;
9159 end Get_Generic_Entity;
9161 -------------------------------------
9162 -- Get_Incomplete_View_Of_Ancestor --
9163 -------------------------------------
9165 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
9166 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9167 Par_Scope : Entity_Id;
9168 Par_Type : Entity_Id;
9170 begin
9171 -- The incomplete view of an ancestor is only relevant for private
9172 -- derived types in child units.
9174 if not Is_Derived_Type (E)
9175 or else not Is_Child_Unit (Cur_Unit)
9176 then
9177 return Empty;
9179 else
9180 Par_Scope := Scope (Cur_Unit);
9181 if No (Par_Scope) then
9182 return Empty;
9183 end if;
9185 Par_Type := Etype (Base_Type (E));
9187 -- Traverse list of ancestor types until we find one declared in
9188 -- a parent or grandparent unit (two levels seem sufficient).
9190 while Present (Par_Type) loop
9191 if Scope (Par_Type) = Par_Scope
9192 or else Scope (Par_Type) = Scope (Par_Scope)
9193 then
9194 return Par_Type;
9196 elsif not Is_Derived_Type (Par_Type) then
9197 return Empty;
9199 else
9200 Par_Type := Etype (Base_Type (Par_Type));
9201 end if;
9202 end loop;
9204 -- If none found, there is no relevant ancestor type.
9206 return Empty;
9207 end if;
9208 end Get_Incomplete_View_Of_Ancestor;
9210 ----------------------
9211 -- Get_Index_Bounds --
9212 ----------------------
9214 procedure Get_Index_Bounds
9215 (N : Node_Id;
9216 L : out Node_Id;
9217 H : out Node_Id;
9218 Use_Full_View : Boolean := False)
9220 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id;
9221 -- Obtain the scalar range of type Typ. If flag Use_Full_View is set and
9222 -- Typ qualifies, the scalar range is obtained from the full view of the
9223 -- type.
9225 --------------------------
9226 -- Scalar_Range_Of_Type --
9227 --------------------------
9229 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id is
9230 T : Entity_Id := Typ;
9232 begin
9233 if Use_Full_View and then Present (Full_View (T)) then
9234 T := Full_View (T);
9235 end if;
9237 return Scalar_Range (T);
9238 end Scalar_Range_Of_Type;
9240 -- Local variables
9242 Kind : constant Node_Kind := Nkind (N);
9243 Rng : Node_Id;
9245 -- Start of processing for Get_Index_Bounds
9247 begin
9248 if Kind = N_Range then
9249 L := Low_Bound (N);
9250 H := High_Bound (N);
9252 elsif Kind = N_Subtype_Indication then
9253 Rng := Range_Expression (Constraint (N));
9255 if Rng = Error then
9256 L := Error;
9257 H := Error;
9258 return;
9260 else
9261 L := Low_Bound (Range_Expression (Constraint (N)));
9262 H := High_Bound (Range_Expression (Constraint (N)));
9263 end if;
9265 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9266 Rng := Scalar_Range_Of_Type (Entity (N));
9268 if Error_Posted (Rng) then
9269 L := Error;
9270 H := Error;
9272 elsif Nkind (Rng) = N_Subtype_Indication then
9273 Get_Index_Bounds (Rng, L, H);
9275 else
9276 L := Low_Bound (Rng);
9277 H := High_Bound (Rng);
9278 end if;
9280 else
9281 -- N is an expression, indicating a range with one value
9283 L := N;
9284 H := N;
9285 end if;
9286 end Get_Index_Bounds;
9288 -----------------------------
9289 -- Get_Interfacing_Aspects --
9290 -----------------------------
9292 procedure Get_Interfacing_Aspects
9293 (Iface_Asp : Node_Id;
9294 Conv_Asp : out Node_Id;
9295 EN_Asp : out Node_Id;
9296 Expo_Asp : out Node_Id;
9297 Imp_Asp : out Node_Id;
9298 LN_Asp : out Node_Id;
9299 Do_Checks : Boolean := False)
9301 procedure Save_Or_Duplication_Error
9302 (Asp : Node_Id;
9303 To : in out Node_Id);
9304 -- Save the value of aspect Asp in node To. If To already has a value,
9305 -- then this is considered a duplicate use of aspect. Emit an error if
9306 -- flag Do_Checks is set.
9308 -------------------------------
9309 -- Save_Or_Duplication_Error --
9310 -------------------------------
9312 procedure Save_Or_Duplication_Error
9313 (Asp : Node_Id;
9314 To : in out Node_Id)
9316 begin
9317 -- Detect an extra aspect and issue an error
9319 if Present (To) then
9320 if Do_Checks then
9321 Error_Msg_Name_1 := Chars (Identifier (Asp));
9322 Error_Msg_Sloc := Sloc (To);
9323 Error_Msg_N ("aspect % previously given #", Asp);
9324 end if;
9326 -- Otherwise capture the aspect
9328 else
9329 To := Asp;
9330 end if;
9331 end Save_Or_Duplication_Error;
9333 -- Local variables
9335 Asp : Node_Id;
9336 Asp_Id : Aspect_Id;
9338 -- The following variables capture each individual aspect
9340 Conv : Node_Id := Empty;
9341 EN : Node_Id := Empty;
9342 Expo : Node_Id := Empty;
9343 Imp : Node_Id := Empty;
9344 LN : Node_Id := Empty;
9346 -- Start of processing for Get_Interfacing_Aspects
9348 begin
9349 -- The input interfacing aspect should reside in an aspect specification
9350 -- list.
9352 pragma Assert (Is_List_Member (Iface_Asp));
9354 -- Examine the aspect specifications of the related entity. Find and
9355 -- capture all interfacing aspects. Detect duplicates and emit errors
9356 -- if applicable.
9358 Asp := First (List_Containing (Iface_Asp));
9359 while Present (Asp) loop
9360 Asp_Id := Get_Aspect_Id (Asp);
9362 if Asp_Id = Aspect_Convention then
9363 Save_Or_Duplication_Error (Asp, Conv);
9365 elsif Asp_Id = Aspect_External_Name then
9366 Save_Or_Duplication_Error (Asp, EN);
9368 elsif Asp_Id = Aspect_Export then
9369 Save_Or_Duplication_Error (Asp, Expo);
9371 elsif Asp_Id = Aspect_Import then
9372 Save_Or_Duplication_Error (Asp, Imp);
9374 elsif Asp_Id = Aspect_Link_Name then
9375 Save_Or_Duplication_Error (Asp, LN);
9376 end if;
9378 Next (Asp);
9379 end loop;
9381 Conv_Asp := Conv;
9382 EN_Asp := EN;
9383 Expo_Asp := Expo;
9384 Imp_Asp := Imp;
9385 LN_Asp := LN;
9386 end Get_Interfacing_Aspects;
9388 ---------------------------------
9389 -- Get_Iterable_Type_Primitive --
9390 ---------------------------------
9392 function Get_Iterable_Type_Primitive
9393 (Typ : Entity_Id;
9394 Nam : Name_Id) return Entity_Id
9396 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
9397 Assoc : Node_Id;
9399 begin
9400 if No (Funcs) then
9401 return Empty;
9403 else
9404 Assoc := First (Component_Associations (Funcs));
9405 while Present (Assoc) loop
9406 if Chars (First (Choices (Assoc))) = Nam then
9407 return Entity (Expression (Assoc));
9408 end if;
9410 Assoc := Next (Assoc);
9411 end loop;
9413 return Empty;
9414 end if;
9415 end Get_Iterable_Type_Primitive;
9417 ----------------------------------
9418 -- Get_Library_Unit_Name_String --
9419 ----------------------------------
9421 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
9422 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
9424 begin
9425 Get_Unit_Name_String (Unit_Name_Id);
9427 -- Remove seven last character (" (spec)" or " (body)")
9429 Name_Len := Name_Len - 7;
9430 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
9431 end Get_Library_Unit_Name_String;
9433 --------------------------
9434 -- Get_Max_Queue_Length --
9435 --------------------------
9437 function Get_Max_Queue_Length (Id : Entity_Id) return Uint is
9438 pragma Assert (Is_Entry (Id));
9439 Prag : constant Entity_Id := Get_Pragma (Id, Pragma_Max_Queue_Length);
9441 begin
9442 -- A value of 0 represents no maximum specified, and entries and entry
9443 -- families with no Max_Queue_Length aspect or pragma default to it.
9445 if not Present (Prag) then
9446 return Uint_0;
9447 end if;
9449 return Intval (Expression (First (Pragma_Argument_Associations (Prag))));
9450 end Get_Max_Queue_Length;
9452 ------------------------
9453 -- Get_Name_Entity_Id --
9454 ------------------------
9456 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
9457 begin
9458 return Entity_Id (Get_Name_Table_Int (Id));
9459 end Get_Name_Entity_Id;
9461 ------------------------------
9462 -- Get_Name_From_CTC_Pragma --
9463 ------------------------------
9465 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
9466 Arg : constant Node_Id :=
9467 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
9468 begin
9469 return Strval (Expr_Value_S (Arg));
9470 end Get_Name_From_CTC_Pragma;
9472 -----------------------
9473 -- Get_Parent_Entity --
9474 -----------------------
9476 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
9477 begin
9478 if Nkind (Unit) = N_Package_Body
9479 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
9480 then
9481 return Defining_Entity
9482 (Specification (Instance_Spec (Original_Node (Unit))));
9483 elsif Nkind (Unit) = N_Package_Instantiation then
9484 return Defining_Entity (Specification (Instance_Spec (Unit)));
9485 else
9486 return Defining_Entity (Unit);
9487 end if;
9488 end Get_Parent_Entity;
9490 -------------------
9491 -- Get_Pragma_Id --
9492 -------------------
9494 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
9495 begin
9496 return Get_Pragma_Id (Pragma_Name_Unmapped (N));
9497 end Get_Pragma_Id;
9499 ------------------------
9500 -- Get_Qualified_Name --
9501 ------------------------
9503 function Get_Qualified_Name
9504 (Id : Entity_Id;
9505 Suffix : Entity_Id := Empty) return Name_Id
9507 Suffix_Nam : Name_Id := No_Name;
9509 begin
9510 if Present (Suffix) then
9511 Suffix_Nam := Chars (Suffix);
9512 end if;
9514 return Get_Qualified_Name (Chars (Id), Suffix_Nam, Scope (Id));
9515 end Get_Qualified_Name;
9517 function Get_Qualified_Name
9518 (Nam : Name_Id;
9519 Suffix : Name_Id := No_Name;
9520 Scop : Entity_Id := Current_Scope) return Name_Id
9522 procedure Add_Scope (S : Entity_Id);
9523 -- Add the fully qualified form of scope S to the name buffer. The
9524 -- format is:
9525 -- s-1__s__
9527 ---------------
9528 -- Add_Scope --
9529 ---------------
9531 procedure Add_Scope (S : Entity_Id) is
9532 begin
9533 if S = Empty then
9534 null;
9536 elsif S = Standard_Standard then
9537 null;
9539 else
9540 Add_Scope (Scope (S));
9541 Get_Name_String_And_Append (Chars (S));
9542 Add_Str_To_Name_Buffer ("__");
9543 end if;
9544 end Add_Scope;
9546 -- Start of processing for Get_Qualified_Name
9548 begin
9549 Name_Len := 0;
9550 Add_Scope (Scop);
9552 -- Append the base name after all scopes have been chained
9554 Get_Name_String_And_Append (Nam);
9556 -- Append the suffix (if present)
9558 if Suffix /= No_Name then
9559 Add_Str_To_Name_Buffer ("__");
9560 Get_Name_String_And_Append (Suffix);
9561 end if;
9563 return Name_Find;
9564 end Get_Qualified_Name;
9566 -----------------------
9567 -- Get_Reason_String --
9568 -----------------------
9570 procedure Get_Reason_String (N : Node_Id) is
9571 begin
9572 if Nkind (N) = N_String_Literal then
9573 Store_String_Chars (Strval (N));
9575 elsif Nkind (N) = N_Op_Concat then
9576 Get_Reason_String (Left_Opnd (N));
9577 Get_Reason_String (Right_Opnd (N));
9579 -- If not of required form, error
9581 else
9582 Error_Msg_N
9583 ("Reason for pragma Warnings has wrong form", N);
9584 Error_Msg_N
9585 ("\must be string literal or concatenation of string literals", N);
9586 return;
9587 end if;
9588 end Get_Reason_String;
9590 --------------------------------
9591 -- Get_Reference_Discriminant --
9592 --------------------------------
9594 function Get_Reference_Discriminant (Typ : Entity_Id) return Entity_Id is
9595 D : Entity_Id;
9597 begin
9598 D := First_Discriminant (Typ);
9599 while Present (D) loop
9600 if Has_Implicit_Dereference (D) then
9601 return D;
9602 end if;
9603 Next_Discriminant (D);
9604 end loop;
9606 return Empty;
9607 end Get_Reference_Discriminant;
9609 ---------------------------
9610 -- Get_Referenced_Object --
9611 ---------------------------
9613 function Get_Referenced_Object (N : Node_Id) return Node_Id is
9614 R : Node_Id;
9616 begin
9617 R := N;
9618 while Is_Entity_Name (R)
9619 and then Present (Renamed_Object (Entity (R)))
9620 loop
9621 R := Renamed_Object (Entity (R));
9622 end loop;
9624 return R;
9625 end Get_Referenced_Object;
9627 ------------------------
9628 -- Get_Renamed_Entity --
9629 ------------------------
9631 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
9632 R : Entity_Id;
9634 begin
9635 R := E;
9636 while Present (Renamed_Entity (R)) loop
9637 R := Renamed_Entity (R);
9638 end loop;
9640 return R;
9641 end Get_Renamed_Entity;
9643 -----------------------
9644 -- Get_Return_Object --
9645 -----------------------
9647 function Get_Return_Object (N : Node_Id) return Entity_Id is
9648 Decl : Node_Id;
9650 begin
9651 Decl := First (Return_Object_Declarations (N));
9652 while Present (Decl) loop
9653 exit when Nkind (Decl) = N_Object_Declaration
9654 and then Is_Return_Object (Defining_Identifier (Decl));
9655 Next (Decl);
9656 end loop;
9658 pragma Assert (Present (Decl));
9659 return Defining_Identifier (Decl);
9660 end Get_Return_Object;
9662 ---------------------------
9663 -- Get_Subprogram_Entity --
9664 ---------------------------
9666 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
9667 Subp : Node_Id;
9668 Subp_Id : Entity_Id;
9670 begin
9671 if Nkind (Nod) = N_Accept_Statement then
9672 Subp := Entry_Direct_Name (Nod);
9674 elsif Nkind (Nod) = N_Slice then
9675 Subp := Prefix (Nod);
9677 else
9678 Subp := Name (Nod);
9679 end if;
9681 -- Strip the subprogram call
9683 loop
9684 if Nkind_In (Subp, N_Explicit_Dereference,
9685 N_Indexed_Component,
9686 N_Selected_Component)
9687 then
9688 Subp := Prefix (Subp);
9690 elsif Nkind_In (Subp, N_Type_Conversion,
9691 N_Unchecked_Type_Conversion)
9692 then
9693 Subp := Expression (Subp);
9695 else
9696 exit;
9697 end if;
9698 end loop;
9700 -- Extract the entity of the subprogram call
9702 if Is_Entity_Name (Subp) then
9703 Subp_Id := Entity (Subp);
9705 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
9706 Subp_Id := Directly_Designated_Type (Subp_Id);
9707 end if;
9709 if Is_Subprogram (Subp_Id) then
9710 return Subp_Id;
9711 else
9712 return Empty;
9713 end if;
9715 -- The search did not find a construct that denotes a subprogram
9717 else
9718 return Empty;
9719 end if;
9720 end Get_Subprogram_Entity;
9722 -----------------------------
9723 -- Get_Task_Body_Procedure --
9724 -----------------------------
9726 function Get_Task_Body_Procedure (E : Entity_Id) return Entity_Id is
9727 begin
9728 -- Note: A task type may be the completion of a private type with
9729 -- discriminants. When performing elaboration checks on a task
9730 -- declaration, the current view of the type may be the private one,
9731 -- and the procedure that holds the body of the task is held in its
9732 -- underlying type.
9734 -- This is an odd function, why not have Task_Body_Procedure do
9735 -- the following digging???
9737 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
9738 end Get_Task_Body_Procedure;
9740 -------------------------
9741 -- Get_User_Defined_Eq --
9742 -------------------------
9744 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
9745 Prim : Elmt_Id;
9746 Op : Entity_Id;
9748 begin
9749 Prim := First_Elmt (Collect_Primitive_Operations (E));
9750 while Present (Prim) loop
9751 Op := Node (Prim);
9753 if Chars (Op) = Name_Op_Eq
9754 and then Etype (Op) = Standard_Boolean
9755 and then Etype (First_Formal (Op)) = E
9756 and then Etype (Next_Formal (First_Formal (Op))) = E
9757 then
9758 return Op;
9759 end if;
9761 Next_Elmt (Prim);
9762 end loop;
9764 return Empty;
9765 end Get_User_Defined_Eq;
9767 ---------------
9768 -- Get_Views --
9769 ---------------
9771 procedure Get_Views
9772 (Typ : Entity_Id;
9773 Priv_Typ : out Entity_Id;
9774 Full_Typ : out Entity_Id;
9775 Full_Base : out Entity_Id;
9776 CRec_Typ : out Entity_Id)
9778 IP_View : Entity_Id;
9780 begin
9781 -- Assume that none of the views can be recovered
9783 Priv_Typ := Empty;
9784 Full_Typ := Empty;
9785 Full_Base := Empty;
9786 CRec_Typ := Empty;
9788 -- The input type is the corresponding record type of a protected or a
9789 -- task type.
9791 if Ekind (Typ) = E_Record_Type
9792 and then Is_Concurrent_Record_Type (Typ)
9793 then
9794 CRec_Typ := Typ;
9795 Full_Typ := Corresponding_Concurrent_Type (CRec_Typ);
9796 Full_Base := Base_Type (Full_Typ);
9797 Priv_Typ := Incomplete_Or_Partial_View (Full_Typ);
9799 -- Otherwise the input type denotes an arbitrary type
9801 else
9802 IP_View := Incomplete_Or_Partial_View (Typ);
9804 -- The input type denotes the full view of a private type
9806 if Present (IP_View) then
9807 Priv_Typ := IP_View;
9808 Full_Typ := Typ;
9810 -- The input type is a private type
9812 elsif Is_Private_Type (Typ) then
9813 Priv_Typ := Typ;
9814 Full_Typ := Full_View (Priv_Typ);
9816 -- Otherwise the input type does not have any views
9818 else
9819 Full_Typ := Typ;
9820 end if;
9822 if Present (Full_Typ) then
9823 Full_Base := Base_Type (Full_Typ);
9825 if Ekind_In (Full_Typ, E_Protected_Type, E_Task_Type) then
9826 CRec_Typ := Corresponding_Record_Type (Full_Typ);
9827 end if;
9828 end if;
9829 end if;
9830 end Get_Views;
9832 -----------------------
9833 -- Has_Access_Values --
9834 -----------------------
9836 function Has_Access_Values (T : Entity_Id) return Boolean is
9837 Typ : constant Entity_Id := Underlying_Type (T);
9839 begin
9840 -- Case of a private type which is not completed yet. This can only
9841 -- happen in the case of a generic format type appearing directly, or
9842 -- as a component of the type to which this function is being applied
9843 -- at the top level. Return False in this case, since we certainly do
9844 -- not know that the type contains access types.
9846 if No (Typ) then
9847 return False;
9849 elsif Is_Access_Type (Typ) then
9850 return True;
9852 elsif Is_Array_Type (Typ) then
9853 return Has_Access_Values (Component_Type (Typ));
9855 elsif Is_Record_Type (Typ) then
9856 declare
9857 Comp : Entity_Id;
9859 begin
9860 -- Loop to Check components
9862 Comp := First_Component_Or_Discriminant (Typ);
9863 while Present (Comp) loop
9865 -- Check for access component, tag field does not count, even
9866 -- though it is implemented internally using an access type.
9868 if Has_Access_Values (Etype (Comp))
9869 and then Chars (Comp) /= Name_uTag
9870 then
9871 return True;
9872 end if;
9874 Next_Component_Or_Discriminant (Comp);
9875 end loop;
9876 end;
9878 return False;
9880 else
9881 return False;
9882 end if;
9883 end Has_Access_Values;
9885 ------------------------------
9886 -- Has_Compatible_Alignment --
9887 ------------------------------
9889 function Has_Compatible_Alignment
9890 (Obj : Entity_Id;
9891 Expr : Node_Id;
9892 Layout_Done : Boolean) return Alignment_Result
9894 function Has_Compatible_Alignment_Internal
9895 (Obj : Entity_Id;
9896 Expr : Node_Id;
9897 Layout_Done : Boolean;
9898 Default : Alignment_Result) return Alignment_Result;
9899 -- This is the internal recursive function that actually does the work.
9900 -- There is one additional parameter, which says what the result should
9901 -- be if no alignment information is found, and there is no definite
9902 -- indication of compatible alignments. At the outer level, this is set
9903 -- to Unknown, but for internal recursive calls in the case where types
9904 -- are known to be correct, it is set to Known_Compatible.
9906 ---------------------------------------
9907 -- Has_Compatible_Alignment_Internal --
9908 ---------------------------------------
9910 function Has_Compatible_Alignment_Internal
9911 (Obj : Entity_Id;
9912 Expr : Node_Id;
9913 Layout_Done : Boolean;
9914 Default : Alignment_Result) return Alignment_Result
9916 Result : Alignment_Result := Known_Compatible;
9917 -- Holds the current status of the result. Note that once a value of
9918 -- Known_Incompatible is set, it is sticky and does not get changed
9919 -- to Unknown (the value in Result only gets worse as we go along,
9920 -- never better).
9922 Offs : Uint := No_Uint;
9923 -- Set to a factor of the offset from the base object when Expr is a
9924 -- selected or indexed component, based on Component_Bit_Offset and
9925 -- Component_Size respectively. A negative value is used to represent
9926 -- a value which is not known at compile time.
9928 procedure Check_Prefix;
9929 -- Checks the prefix recursively in the case where the expression
9930 -- is an indexed or selected component.
9932 procedure Set_Result (R : Alignment_Result);
9933 -- If R represents a worse outcome (unknown instead of known
9934 -- compatible, or known incompatible), then set Result to R.
9936 ------------------
9937 -- Check_Prefix --
9938 ------------------
9940 procedure Check_Prefix is
9941 begin
9942 -- The subtlety here is that in doing a recursive call to check
9943 -- the prefix, we have to decide what to do in the case where we
9944 -- don't find any specific indication of an alignment problem.
9946 -- At the outer level, we normally set Unknown as the result in
9947 -- this case, since we can only set Known_Compatible if we really
9948 -- know that the alignment value is OK, but for the recursive
9949 -- call, in the case where the types match, and we have not
9950 -- specified a peculiar alignment for the object, we are only
9951 -- concerned about suspicious rep clauses, the default case does
9952 -- not affect us, since the compiler will, in the absence of such
9953 -- rep clauses, ensure that the alignment is correct.
9955 if Default = Known_Compatible
9956 or else
9957 (Etype (Obj) = Etype (Expr)
9958 and then (Unknown_Alignment (Obj)
9959 or else
9960 Alignment (Obj) = Alignment (Etype (Obj))))
9961 then
9962 Set_Result
9963 (Has_Compatible_Alignment_Internal
9964 (Obj, Prefix (Expr), Layout_Done, Known_Compatible));
9966 -- In all other cases, we need a full check on the prefix
9968 else
9969 Set_Result
9970 (Has_Compatible_Alignment_Internal
9971 (Obj, Prefix (Expr), Layout_Done, Unknown));
9972 end if;
9973 end Check_Prefix;
9975 ----------------
9976 -- Set_Result --
9977 ----------------
9979 procedure Set_Result (R : Alignment_Result) is
9980 begin
9981 if R > Result then
9982 Result := R;
9983 end if;
9984 end Set_Result;
9986 -- Start of processing for Has_Compatible_Alignment_Internal
9988 begin
9989 -- If Expr is a selected component, we must make sure there is no
9990 -- potentially troublesome component clause and that the record is
9991 -- not packed if the layout is not done.
9993 if Nkind (Expr) = N_Selected_Component then
9995 -- Packing generates unknown alignment if layout is not done
9997 if Is_Packed (Etype (Prefix (Expr))) and then not Layout_Done then
9998 Set_Result (Unknown);
9999 end if;
10001 -- Check prefix and component offset
10003 Check_Prefix;
10004 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
10006 -- If Expr is an indexed component, we must make sure there is no
10007 -- potentially troublesome Component_Size clause and that the array
10008 -- is not bit-packed if the layout is not done.
10010 elsif Nkind (Expr) = N_Indexed_Component then
10011 declare
10012 Typ : constant Entity_Id := Etype (Prefix (Expr));
10014 begin
10015 -- Packing generates unknown alignment if layout is not done
10017 if Is_Bit_Packed_Array (Typ) and then not Layout_Done then
10018 Set_Result (Unknown);
10019 end if;
10021 -- Check prefix and component offset (or at least size)
10023 Check_Prefix;
10024 Offs := Indexed_Component_Bit_Offset (Expr);
10025 if Offs = No_Uint then
10026 Offs := Component_Size (Typ);
10027 end if;
10028 end;
10029 end if;
10031 -- If we have a null offset, the result is entirely determined by
10032 -- the base object and has already been computed recursively.
10034 if Offs = Uint_0 then
10035 null;
10037 -- Case where we know the alignment of the object
10039 elsif Known_Alignment (Obj) then
10040 declare
10041 ObjA : constant Uint := Alignment (Obj);
10042 ExpA : Uint := No_Uint;
10043 SizA : Uint := No_Uint;
10045 begin
10046 -- If alignment of Obj is 1, then we are always OK
10048 if ObjA = 1 then
10049 Set_Result (Known_Compatible);
10051 -- Alignment of Obj is greater than 1, so we need to check
10053 else
10054 -- If we have an offset, see if it is compatible
10056 if Offs /= No_Uint and Offs > Uint_0 then
10057 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
10058 Set_Result (Known_Incompatible);
10059 end if;
10061 -- See if Expr is an object with known alignment
10063 elsif Is_Entity_Name (Expr)
10064 and then Known_Alignment (Entity (Expr))
10065 then
10066 ExpA := Alignment (Entity (Expr));
10068 -- Otherwise, we can use the alignment of the type of
10069 -- Expr given that we already checked for
10070 -- discombobulating rep clauses for the cases of indexed
10071 -- and selected components above.
10073 elsif Known_Alignment (Etype (Expr)) then
10074 ExpA := Alignment (Etype (Expr));
10076 -- Otherwise the alignment is unknown
10078 else
10079 Set_Result (Default);
10080 end if;
10082 -- If we got an alignment, see if it is acceptable
10084 if ExpA /= No_Uint and then ExpA < ObjA then
10085 Set_Result (Known_Incompatible);
10086 end if;
10088 -- If Expr is not a piece of a larger object, see if size
10089 -- is given. If so, check that it is not too small for the
10090 -- required alignment.
10092 if Offs /= No_Uint then
10093 null;
10095 -- See if Expr is an object with known size
10097 elsif Is_Entity_Name (Expr)
10098 and then Known_Static_Esize (Entity (Expr))
10099 then
10100 SizA := Esize (Entity (Expr));
10102 -- Otherwise, we check the object size of the Expr type
10104 elsif Known_Static_Esize (Etype (Expr)) then
10105 SizA := Esize (Etype (Expr));
10106 end if;
10108 -- If we got a size, see if it is a multiple of the Obj
10109 -- alignment, if not, then the alignment cannot be
10110 -- acceptable, since the size is always a multiple of the
10111 -- alignment.
10113 if SizA /= No_Uint then
10114 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
10115 Set_Result (Known_Incompatible);
10116 end if;
10117 end if;
10118 end if;
10119 end;
10121 -- If we do not know required alignment, any non-zero offset is a
10122 -- potential problem (but certainly may be OK, so result is unknown).
10124 elsif Offs /= No_Uint then
10125 Set_Result (Unknown);
10127 -- If we can't find the result by direct comparison of alignment
10128 -- values, then there is still one case that we can determine known
10129 -- result, and that is when we can determine that the types are the
10130 -- same, and no alignments are specified. Then we known that the
10131 -- alignments are compatible, even if we don't know the alignment
10132 -- value in the front end.
10134 elsif Etype (Obj) = Etype (Expr) then
10136 -- Types are the same, but we have to check for possible size
10137 -- and alignments on the Expr object that may make the alignment
10138 -- different, even though the types are the same.
10140 if Is_Entity_Name (Expr) then
10142 -- First check alignment of the Expr object. Any alignment less
10143 -- than Maximum_Alignment is worrisome since this is the case
10144 -- where we do not know the alignment of Obj.
10146 if Known_Alignment (Entity (Expr))
10147 and then UI_To_Int (Alignment (Entity (Expr))) <
10148 Ttypes.Maximum_Alignment
10149 then
10150 Set_Result (Unknown);
10152 -- Now check size of Expr object. Any size that is not an
10153 -- even multiple of Maximum_Alignment is also worrisome
10154 -- since it may cause the alignment of the object to be less
10155 -- than the alignment of the type.
10157 elsif Known_Static_Esize (Entity (Expr))
10158 and then
10159 (UI_To_Int (Esize (Entity (Expr))) mod
10160 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
10161 /= 0
10162 then
10163 Set_Result (Unknown);
10165 -- Otherwise same type is decisive
10167 else
10168 Set_Result (Known_Compatible);
10169 end if;
10170 end if;
10172 -- Another case to deal with is when there is an explicit size or
10173 -- alignment clause when the types are not the same. If so, then the
10174 -- result is Unknown. We don't need to do this test if the Default is
10175 -- Unknown, since that result will be set in any case.
10177 elsif Default /= Unknown
10178 and then (Has_Size_Clause (Etype (Expr))
10179 or else
10180 Has_Alignment_Clause (Etype (Expr)))
10181 then
10182 Set_Result (Unknown);
10184 -- If no indication found, set default
10186 else
10187 Set_Result (Default);
10188 end if;
10190 -- Return worst result found
10192 return Result;
10193 end Has_Compatible_Alignment_Internal;
10195 -- Start of processing for Has_Compatible_Alignment
10197 begin
10198 -- If Obj has no specified alignment, then set alignment from the type
10199 -- alignment. Perhaps we should always do this, but for sure we should
10200 -- do it when there is an address clause since we can do more if the
10201 -- alignment is known.
10203 if Unknown_Alignment (Obj) then
10204 Set_Alignment (Obj, Alignment (Etype (Obj)));
10205 end if;
10207 -- Now do the internal call that does all the work
10209 return
10210 Has_Compatible_Alignment_Internal (Obj, Expr, Layout_Done, Unknown);
10211 end Has_Compatible_Alignment;
10213 ----------------------
10214 -- Has_Declarations --
10215 ----------------------
10217 function Has_Declarations (N : Node_Id) return Boolean is
10218 begin
10219 return Nkind_In (Nkind (N), N_Accept_Statement,
10220 N_Block_Statement,
10221 N_Compilation_Unit_Aux,
10222 N_Entry_Body,
10223 N_Package_Body,
10224 N_Protected_Body,
10225 N_Subprogram_Body,
10226 N_Task_Body,
10227 N_Package_Specification);
10228 end Has_Declarations;
10230 ---------------------------------
10231 -- Has_Defaulted_Discriminants --
10232 ---------------------------------
10234 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10235 begin
10236 return Has_Discriminants (Typ)
10237 and then Present (First_Discriminant (Typ))
10238 and then Present (Discriminant_Default_Value
10239 (First_Discriminant (Typ)));
10240 end Has_Defaulted_Discriminants;
10242 -------------------
10243 -- Has_Denormals --
10244 -------------------
10246 function Has_Denormals (E : Entity_Id) return Boolean is
10247 begin
10248 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
10249 end Has_Denormals;
10251 -------------------------------------------
10252 -- Has_Discriminant_Dependent_Constraint --
10253 -------------------------------------------
10255 function Has_Discriminant_Dependent_Constraint
10256 (Comp : Entity_Id) return Boolean
10258 Comp_Decl : constant Node_Id := Parent (Comp);
10259 Subt_Indic : Node_Id;
10260 Constr : Node_Id;
10261 Assn : Node_Id;
10263 begin
10264 -- Discriminants can't depend on discriminants
10266 if Ekind (Comp) = E_Discriminant then
10267 return False;
10269 else
10270 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
10272 if Nkind (Subt_Indic) = N_Subtype_Indication then
10273 Constr := Constraint (Subt_Indic);
10275 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
10276 Assn := First (Constraints (Constr));
10277 while Present (Assn) loop
10278 case Nkind (Assn) is
10279 when N_Identifier
10280 | N_Range
10281 | N_Subtype_Indication
10283 if Depends_On_Discriminant (Assn) then
10284 return True;
10285 end if;
10287 when N_Discriminant_Association =>
10288 if Depends_On_Discriminant (Expression (Assn)) then
10289 return True;
10290 end if;
10292 when others =>
10293 null;
10294 end case;
10296 Next (Assn);
10297 end loop;
10298 end if;
10299 end if;
10300 end if;
10302 return False;
10303 end Has_Discriminant_Dependent_Constraint;
10305 --------------------------------------
10306 -- Has_Effectively_Volatile_Profile --
10307 --------------------------------------
10309 function Has_Effectively_Volatile_Profile
10310 (Subp_Id : Entity_Id) return Boolean
10312 Formal : Entity_Id;
10314 begin
10315 -- Inspect the formal parameters looking for an effectively volatile
10316 -- type.
10318 Formal := First_Formal (Subp_Id);
10319 while Present (Formal) loop
10320 if Is_Effectively_Volatile (Etype (Formal)) then
10321 return True;
10322 end if;
10324 Next_Formal (Formal);
10325 end loop;
10327 -- Inspect the return type of functions
10329 if Ekind_In (Subp_Id, E_Function, E_Generic_Function)
10330 and then Is_Effectively_Volatile (Etype (Subp_Id))
10331 then
10332 return True;
10333 end if;
10335 return False;
10336 end Has_Effectively_Volatile_Profile;
10338 --------------------------
10339 -- Has_Enabled_Property --
10340 --------------------------
10342 function Has_Enabled_Property
10343 (Item_Id : Entity_Id;
10344 Property : Name_Id) return Boolean
10346 function Protected_Object_Has_Enabled_Property return Boolean;
10347 -- Determine whether a protected object denoted by Item_Id has the
10348 -- property enabled.
10350 function State_Has_Enabled_Property return Boolean;
10351 -- Determine whether a state denoted by Item_Id has the property enabled
10353 function Variable_Has_Enabled_Property return Boolean;
10354 -- Determine whether a variable denoted by Item_Id has the property
10355 -- enabled.
10357 -------------------------------------------
10358 -- Protected_Object_Has_Enabled_Property --
10359 -------------------------------------------
10361 function Protected_Object_Has_Enabled_Property return Boolean is
10362 Constits : constant Elist_Id := Part_Of_Constituents (Item_Id);
10363 Constit_Elmt : Elmt_Id;
10364 Constit_Id : Entity_Id;
10366 begin
10367 -- Protected objects always have the properties Async_Readers and
10368 -- Async_Writers (SPARK RM 7.1.2(16)).
10370 if Property = Name_Async_Readers
10371 or else Property = Name_Async_Writers
10372 then
10373 return True;
10375 -- Protected objects that have Part_Of components also inherit their
10376 -- properties Effective_Reads and Effective_Writes
10377 -- (SPARK RM 7.1.2(16)).
10379 elsif Present (Constits) then
10380 Constit_Elmt := First_Elmt (Constits);
10381 while Present (Constit_Elmt) loop
10382 Constit_Id := Node (Constit_Elmt);
10384 if Has_Enabled_Property (Constit_Id, Property) then
10385 return True;
10386 end if;
10388 Next_Elmt (Constit_Elmt);
10389 end loop;
10390 end if;
10392 return False;
10393 end Protected_Object_Has_Enabled_Property;
10395 --------------------------------
10396 -- State_Has_Enabled_Property --
10397 --------------------------------
10399 function State_Has_Enabled_Property return Boolean is
10400 Decl : constant Node_Id := Parent (Item_Id);
10402 procedure Find_Simple_Properties
10403 (Has_External : out Boolean;
10404 Has_Synchronous : out Boolean);
10405 -- Extract the simple properties associated with declaration Decl
10407 function Is_Enabled_External_Property return Boolean;
10408 -- Determine whether property Property appears within the external
10409 -- property list of declaration Decl, and return its status.
10411 ----------------------------
10412 -- Find_Simple_Properties --
10413 ----------------------------
10415 procedure Find_Simple_Properties
10416 (Has_External : out Boolean;
10417 Has_Synchronous : out Boolean)
10419 Opt : Node_Id;
10421 begin
10422 -- Assume that none of the properties are available
10424 Has_External := False;
10425 Has_Synchronous := False;
10427 Opt := First (Expressions (Decl));
10428 while Present (Opt) loop
10429 if Nkind (Opt) = N_Identifier then
10430 if Chars (Opt) = Name_External then
10431 Has_External := True;
10433 elsif Chars (Opt) = Name_Synchronous then
10434 Has_Synchronous := True;
10435 end if;
10436 end if;
10438 Next (Opt);
10439 end loop;
10440 end Find_Simple_Properties;
10442 ----------------------------------
10443 -- Is_Enabled_External_Property --
10444 ----------------------------------
10446 function Is_Enabled_External_Property return Boolean is
10447 Opt : Node_Id;
10448 Opt_Nam : Node_Id;
10449 Prop : Node_Id;
10450 Prop_Nam : Node_Id;
10451 Props : Node_Id;
10453 begin
10454 Opt := First (Component_Associations (Decl));
10455 while Present (Opt) loop
10456 Opt_Nam := First (Choices (Opt));
10458 if Nkind (Opt_Nam) = N_Identifier
10459 and then Chars (Opt_Nam) = Name_External
10460 then
10461 Props := Expression (Opt);
10463 -- Multiple properties appear as an aggregate
10465 if Nkind (Props) = N_Aggregate then
10467 -- Simple property form
10469 Prop := First (Expressions (Props));
10470 while Present (Prop) loop
10471 if Chars (Prop) = Property then
10472 return True;
10473 end if;
10475 Next (Prop);
10476 end loop;
10478 -- Property with expression form
10480 Prop := First (Component_Associations (Props));
10481 while Present (Prop) loop
10482 Prop_Nam := First (Choices (Prop));
10484 -- The property can be represented in two ways:
10485 -- others => <value>
10486 -- <property> => <value>
10488 if Nkind (Prop_Nam) = N_Others_Choice
10489 or else (Nkind (Prop_Nam) = N_Identifier
10490 and then Chars (Prop_Nam) = Property)
10491 then
10492 return Is_True (Expr_Value (Expression (Prop)));
10493 end if;
10495 Next (Prop);
10496 end loop;
10498 -- Single property
10500 else
10501 return Chars (Props) = Property;
10502 end if;
10503 end if;
10505 Next (Opt);
10506 end loop;
10508 return False;
10509 end Is_Enabled_External_Property;
10511 -- Local variables
10513 Has_External : Boolean;
10514 Has_Synchronous : Boolean;
10516 -- Start of processing for State_Has_Enabled_Property
10518 begin
10519 -- The declaration of an external abstract state appears as an
10520 -- extension aggregate. If this is not the case, properties can
10521 -- never be set.
10523 if Nkind (Decl) /= N_Extension_Aggregate then
10524 return False;
10525 end if;
10527 Find_Simple_Properties (Has_External, Has_Synchronous);
10529 -- Simple option External enables all properties (SPARK RM 7.1.2(2))
10531 if Has_External then
10532 return True;
10534 -- Option External may enable or disable specific properties
10536 elsif Is_Enabled_External_Property then
10537 return True;
10539 -- Simple option Synchronous
10541 -- enables disables
10542 -- Asynch_Readers Effective_Reads
10543 -- Asynch_Writers Effective_Writes
10545 -- Note that both forms of External have higher precedence than
10546 -- Synchronous (SPARK RM 7.1.4(10)).
10548 elsif Has_Synchronous then
10549 return Nam_In (Property, Name_Async_Readers, Name_Async_Writers);
10550 end if;
10552 return False;
10553 end State_Has_Enabled_Property;
10555 -----------------------------------
10556 -- Variable_Has_Enabled_Property --
10557 -----------------------------------
10559 function Variable_Has_Enabled_Property return Boolean is
10560 function Is_Enabled (Prag : Node_Id) return Boolean;
10561 -- Determine whether property pragma Prag (if present) denotes an
10562 -- enabled property.
10564 ----------------
10565 -- Is_Enabled --
10566 ----------------
10568 function Is_Enabled (Prag : Node_Id) return Boolean is
10569 Arg1 : Node_Id;
10571 begin
10572 if Present (Prag) then
10573 Arg1 := First (Pragma_Argument_Associations (Prag));
10575 -- The pragma has an optional Boolean expression, the related
10576 -- property is enabled only when the expression evaluates to
10577 -- True.
10579 if Present (Arg1) then
10580 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
10582 -- Otherwise the lack of expression enables the property by
10583 -- default.
10585 else
10586 return True;
10587 end if;
10589 -- The property was never set in the first place
10591 else
10592 return False;
10593 end if;
10594 end Is_Enabled;
10596 -- Local variables
10598 AR : constant Node_Id :=
10599 Get_Pragma (Item_Id, Pragma_Async_Readers);
10600 AW : constant Node_Id :=
10601 Get_Pragma (Item_Id, Pragma_Async_Writers);
10602 ER : constant Node_Id :=
10603 Get_Pragma (Item_Id, Pragma_Effective_Reads);
10604 EW : constant Node_Id :=
10605 Get_Pragma (Item_Id, Pragma_Effective_Writes);
10607 -- Start of processing for Variable_Has_Enabled_Property
10609 begin
10610 -- A non-effectively volatile object can never possess external
10611 -- properties.
10613 if not Is_Effectively_Volatile (Item_Id) then
10614 return False;
10616 -- External properties related to variables come in two flavors -
10617 -- explicit and implicit. The explicit case is characterized by the
10618 -- presence of a property pragma with an optional Boolean flag. The
10619 -- property is enabled when the flag evaluates to True or the flag is
10620 -- missing altogether.
10622 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
10623 return True;
10625 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
10626 return True;
10628 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
10629 return True;
10631 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
10632 return True;
10634 -- The implicit case lacks all property pragmas
10636 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
10637 if Is_Protected_Type (Etype (Item_Id)) then
10638 return Protected_Object_Has_Enabled_Property;
10639 else
10640 return True;
10641 end if;
10643 else
10644 return False;
10645 end if;
10646 end Variable_Has_Enabled_Property;
10648 -- Start of processing for Has_Enabled_Property
10650 begin
10651 -- Abstract states and variables have a flexible scheme of specifying
10652 -- external properties.
10654 if Ekind (Item_Id) = E_Abstract_State then
10655 return State_Has_Enabled_Property;
10657 elsif Ekind (Item_Id) = E_Variable then
10658 return Variable_Has_Enabled_Property;
10660 -- By default, protected objects only have the properties Async_Readers
10661 -- and Async_Writers. If they have Part_Of components, they also inherit
10662 -- their properties Effective_Reads and Effective_Writes
10663 -- (SPARK RM 7.1.2(16)).
10665 elsif Ekind (Item_Id) = E_Protected_Object then
10666 return Protected_Object_Has_Enabled_Property;
10668 -- Otherwise a property is enabled when the related item is effectively
10669 -- volatile.
10671 else
10672 return Is_Effectively_Volatile (Item_Id);
10673 end if;
10674 end Has_Enabled_Property;
10676 -------------------------------------
10677 -- Has_Full_Default_Initialization --
10678 -------------------------------------
10680 function Has_Full_Default_Initialization (Typ : Entity_Id) return Boolean is
10681 Comp : Entity_Id;
10683 begin
10684 -- A type subject to pragma Default_Initial_Condition may be fully
10685 -- default initialized depending on inheritance and the argument of
10686 -- the pragma. Since any type may act as the full view of a private
10687 -- type, this check must be performed prior to the specialized tests
10688 -- below.
10690 if Has_Fully_Default_Initializing_DIC_Pragma (Typ) then
10691 return True;
10692 end if;
10694 -- A scalar type is fully default initialized if it is subject to aspect
10695 -- Default_Value.
10697 if Is_Scalar_Type (Typ) then
10698 return Has_Default_Aspect (Typ);
10700 -- An array type is fully default initialized if its element type is
10701 -- scalar and the array type carries aspect Default_Component_Value or
10702 -- the element type is fully default initialized.
10704 elsif Is_Array_Type (Typ) then
10705 return
10706 Has_Default_Aspect (Typ)
10707 or else Has_Full_Default_Initialization (Component_Type (Typ));
10709 -- A protected type, record type, or type extension is fully default
10710 -- initialized if all its components either carry an initialization
10711 -- expression or have a type that is fully default initialized. The
10712 -- parent type of a type extension must be fully default initialized.
10714 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
10716 -- Inspect all entities defined in the scope of the type, looking for
10717 -- uninitialized components.
10719 Comp := First_Entity (Typ);
10720 while Present (Comp) loop
10721 if Ekind (Comp) = E_Component
10722 and then Comes_From_Source (Comp)
10723 and then No (Expression (Parent (Comp)))
10724 and then not Has_Full_Default_Initialization (Etype (Comp))
10725 then
10726 return False;
10727 end if;
10729 Next_Entity (Comp);
10730 end loop;
10732 -- Ensure that the parent type of a type extension is fully default
10733 -- initialized.
10735 if Etype (Typ) /= Typ
10736 and then not Has_Full_Default_Initialization (Etype (Typ))
10737 then
10738 return False;
10739 end if;
10741 -- If we get here, then all components and parent portion are fully
10742 -- default initialized.
10744 return True;
10746 -- A task type is fully default initialized by default
10748 elsif Is_Task_Type (Typ) then
10749 return True;
10751 -- Otherwise the type is not fully default initialized
10753 else
10754 return False;
10755 end if;
10756 end Has_Full_Default_Initialization;
10758 -----------------------------------------------
10759 -- Has_Fully_Default_Initializing_DIC_Pragma --
10760 -----------------------------------------------
10762 function Has_Fully_Default_Initializing_DIC_Pragma
10763 (Typ : Entity_Id) return Boolean
10765 Args : List_Id;
10766 Prag : Node_Id;
10768 begin
10769 -- A type that inherits pragma Default_Initial_Condition from a parent
10770 -- type is automatically fully default initialized.
10772 if Has_Inherited_DIC (Typ) then
10773 return True;
10775 -- Otherwise the type is fully default initialized only when the pragma
10776 -- appears without an argument, or the argument is non-null.
10778 elsif Has_Own_DIC (Typ) then
10779 Prag := Get_Pragma (Typ, Pragma_Default_Initial_Condition);
10780 pragma Assert (Present (Prag));
10781 Args := Pragma_Argument_Associations (Prag);
10783 -- The pragma appears without an argument in which case it defaults
10784 -- to True.
10786 if No (Args) then
10787 return True;
10789 -- The pragma appears with a non-null expression
10791 elsif Nkind (Get_Pragma_Arg (First (Args))) /= N_Null then
10792 return True;
10793 end if;
10794 end if;
10796 return False;
10797 end Has_Fully_Default_Initializing_DIC_Pragma;
10799 --------------------
10800 -- Has_Infinities --
10801 --------------------
10803 function Has_Infinities (E : Entity_Id) return Boolean is
10804 begin
10805 return
10806 Is_Floating_Point_Type (E)
10807 and then Nkind (Scalar_Range (E)) = N_Range
10808 and then Includes_Infinities (Scalar_Range (E));
10809 end Has_Infinities;
10811 --------------------
10812 -- Has_Interfaces --
10813 --------------------
10815 function Has_Interfaces
10816 (T : Entity_Id;
10817 Use_Full_View : Boolean := True) return Boolean
10819 Typ : Entity_Id := Base_Type (T);
10821 begin
10822 -- Handle concurrent types
10824 if Is_Concurrent_Type (Typ) then
10825 Typ := Corresponding_Record_Type (Typ);
10826 end if;
10828 if not Present (Typ)
10829 or else not Is_Record_Type (Typ)
10830 or else not Is_Tagged_Type (Typ)
10831 then
10832 return False;
10833 end if;
10835 -- Handle private types
10837 if Use_Full_View and then Present (Full_View (Typ)) then
10838 Typ := Full_View (Typ);
10839 end if;
10841 -- Handle concurrent record types
10843 if Is_Concurrent_Record_Type (Typ)
10844 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
10845 then
10846 return True;
10847 end if;
10849 loop
10850 if Is_Interface (Typ)
10851 or else
10852 (Is_Record_Type (Typ)
10853 and then Present (Interfaces (Typ))
10854 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
10855 then
10856 return True;
10857 end if;
10859 exit when Etype (Typ) = Typ
10861 -- Handle private types
10863 or else (Present (Full_View (Etype (Typ)))
10864 and then Full_View (Etype (Typ)) = Typ)
10866 -- Protect frontend against wrong sources with cyclic derivations
10868 or else Etype (Typ) = T;
10870 -- Climb to the ancestor type handling private types
10872 if Present (Full_View (Etype (Typ))) then
10873 Typ := Full_View (Etype (Typ));
10874 else
10875 Typ := Etype (Typ);
10876 end if;
10877 end loop;
10879 return False;
10880 end Has_Interfaces;
10882 --------------------------
10883 -- Has_Max_Queue_Length --
10884 --------------------------
10886 function Has_Max_Queue_Length (Id : Entity_Id) return Boolean is
10887 begin
10888 return
10889 Ekind (Id) = E_Entry
10890 and then Present (Get_Pragma (Id, Pragma_Max_Queue_Length));
10891 end Has_Max_Queue_Length;
10893 ---------------------------------
10894 -- Has_No_Obvious_Side_Effects --
10895 ---------------------------------
10897 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
10898 begin
10899 -- For now handle literals, constants, and non-volatile variables and
10900 -- expressions combining these with operators or short circuit forms.
10902 if Nkind (N) in N_Numeric_Or_String_Literal then
10903 return True;
10905 elsif Nkind (N) = N_Character_Literal then
10906 return True;
10908 elsif Nkind (N) in N_Unary_Op then
10909 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
10911 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
10912 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
10913 and then
10914 Has_No_Obvious_Side_Effects (Right_Opnd (N));
10916 elsif Nkind (N) = N_Expression_With_Actions
10917 and then Is_Empty_List (Actions (N))
10918 then
10919 return Has_No_Obvious_Side_Effects (Expression (N));
10921 elsif Nkind (N) in N_Has_Entity then
10922 return Present (Entity (N))
10923 and then Ekind_In (Entity (N), E_Variable,
10924 E_Constant,
10925 E_Enumeration_Literal,
10926 E_In_Parameter,
10927 E_Out_Parameter,
10928 E_In_Out_Parameter)
10929 and then not Is_Volatile (Entity (N));
10931 else
10932 return False;
10933 end if;
10934 end Has_No_Obvious_Side_Effects;
10936 -----------------------------
10937 -- Has_Non_Null_Refinement --
10938 -----------------------------
10940 function Has_Non_Null_Refinement (Id : Entity_Id) return Boolean is
10941 Constits : Elist_Id;
10943 begin
10944 pragma Assert (Ekind (Id) = E_Abstract_State);
10945 Constits := Refinement_Constituents (Id);
10947 -- For a refinement to be non-null, the first constituent must be
10948 -- anything other than null.
10950 return
10951 Present (Constits)
10952 and then Nkind (Node (First_Elmt (Constits))) /= N_Null;
10953 end Has_Non_Null_Refinement;
10955 -----------------------------
10956 -- Has_Non_Null_Statements --
10957 -----------------------------
10959 function Has_Non_Null_Statements (L : List_Id) return Boolean is
10960 Node : Node_Id;
10962 begin
10963 if Is_Non_Empty_List (L) then
10964 Node := First (L);
10966 loop
10967 if Nkind (Node) /= N_Null_Statement then
10968 return True;
10969 end if;
10971 Next (Node);
10972 exit when Node = Empty;
10973 end loop;
10974 end if;
10976 return False;
10977 end Has_Non_Null_Statements;
10979 ----------------------------------
10980 -- Has_Non_Trivial_Precondition --
10981 ----------------------------------
10983 function Has_Non_Trivial_Precondition (Subp : Entity_Id) return Boolean is
10984 Pre : constant Node_Id := Find_Aspect (Subp, Aspect_Pre);
10986 begin
10987 return
10988 Present (Pre)
10989 and then Class_Present (Pre)
10990 and then not Is_Entity_Name (Expression (Pre));
10991 end Has_Non_Trivial_Precondition;
10993 -------------------
10994 -- Has_Null_Body --
10995 -------------------
10997 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
10998 Body_Id : Entity_Id;
10999 Decl : Node_Id;
11000 Spec : Node_Id;
11001 Stmt1 : Node_Id;
11002 Stmt2 : Node_Id;
11004 begin
11005 Spec := Parent (Proc_Id);
11006 Decl := Parent (Spec);
11008 -- Retrieve the entity of the procedure body (e.g. invariant proc).
11010 if Nkind (Spec) = N_Procedure_Specification
11011 and then Nkind (Decl) = N_Subprogram_Declaration
11012 then
11013 Body_Id := Corresponding_Body (Decl);
11015 -- The body acts as a spec
11017 else
11018 Body_Id := Proc_Id;
11019 end if;
11021 -- The body will be generated later
11023 if No (Body_Id) then
11024 return False;
11025 end if;
11027 Spec := Parent (Body_Id);
11028 Decl := Parent (Spec);
11030 pragma Assert
11031 (Nkind (Spec) = N_Procedure_Specification
11032 and then Nkind (Decl) = N_Subprogram_Body);
11034 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
11036 -- Look for a null statement followed by an optional return
11037 -- statement.
11039 if Nkind (Stmt1) = N_Null_Statement then
11040 Stmt2 := Next (Stmt1);
11042 if Present (Stmt2) then
11043 return Nkind (Stmt2) = N_Simple_Return_Statement;
11044 else
11045 return True;
11046 end if;
11047 end if;
11049 return False;
11050 end Has_Null_Body;
11052 ------------------------
11053 -- Has_Null_Exclusion --
11054 ------------------------
11056 function Has_Null_Exclusion (N : Node_Id) return Boolean is
11057 begin
11058 case Nkind (N) is
11059 when N_Access_Definition
11060 | N_Access_Function_Definition
11061 | N_Access_Procedure_Definition
11062 | N_Access_To_Object_Definition
11063 | N_Allocator
11064 | N_Derived_Type_Definition
11065 | N_Function_Specification
11066 | N_Subtype_Declaration
11068 return Null_Exclusion_Present (N);
11070 when N_Component_Definition
11071 | N_Formal_Object_Declaration
11072 | N_Object_Renaming_Declaration
11074 if Present (Subtype_Mark (N)) then
11075 return Null_Exclusion_Present (N);
11076 else pragma Assert (Present (Access_Definition (N)));
11077 return Null_Exclusion_Present (Access_Definition (N));
11078 end if;
11080 when N_Discriminant_Specification =>
11081 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
11082 return Null_Exclusion_Present (Discriminant_Type (N));
11083 else
11084 return Null_Exclusion_Present (N);
11085 end if;
11087 when N_Object_Declaration =>
11088 if Nkind (Object_Definition (N)) = N_Access_Definition then
11089 return Null_Exclusion_Present (Object_Definition (N));
11090 else
11091 return Null_Exclusion_Present (N);
11092 end if;
11094 when N_Parameter_Specification =>
11095 if Nkind (Parameter_Type (N)) = N_Access_Definition then
11096 return Null_Exclusion_Present (Parameter_Type (N));
11097 else
11098 return Null_Exclusion_Present (N);
11099 end if;
11101 when others =>
11102 return False;
11103 end case;
11104 end Has_Null_Exclusion;
11106 ------------------------
11107 -- Has_Null_Extension --
11108 ------------------------
11110 function Has_Null_Extension (T : Entity_Id) return Boolean is
11111 B : constant Entity_Id := Base_Type (T);
11112 Comps : Node_Id;
11113 Ext : Node_Id;
11115 begin
11116 if Nkind (Parent (B)) = N_Full_Type_Declaration
11117 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
11118 then
11119 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
11121 if Present (Ext) then
11122 if Null_Present (Ext) then
11123 return True;
11124 else
11125 Comps := Component_List (Ext);
11127 -- The null component list is rewritten during analysis to
11128 -- include the parent component. Any other component indicates
11129 -- that the extension was not originally null.
11131 return Null_Present (Comps)
11132 or else No (Next (First (Component_Items (Comps))));
11133 end if;
11134 else
11135 return False;
11136 end if;
11138 else
11139 return False;
11140 end if;
11141 end Has_Null_Extension;
11143 -------------------------
11144 -- Has_Null_Refinement --
11145 -------------------------
11147 function Has_Null_Refinement (Id : Entity_Id) return Boolean is
11148 Constits : Elist_Id;
11150 begin
11151 pragma Assert (Ekind (Id) = E_Abstract_State);
11152 Constits := Refinement_Constituents (Id);
11154 -- For a refinement to be null, the state's sole constituent must be a
11155 -- null.
11157 return
11158 Present (Constits)
11159 and then Nkind (Node (First_Elmt (Constits))) = N_Null;
11160 end Has_Null_Refinement;
11162 -------------------------------
11163 -- Has_Overriding_Initialize --
11164 -------------------------------
11166 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
11167 BT : constant Entity_Id := Base_Type (T);
11168 P : Elmt_Id;
11170 begin
11171 if Is_Controlled (BT) then
11172 if Is_RTU (Scope (BT), Ada_Finalization) then
11173 return False;
11175 elsif Present (Primitive_Operations (BT)) then
11176 P := First_Elmt (Primitive_Operations (BT));
11177 while Present (P) loop
11178 declare
11179 Init : constant Entity_Id := Node (P);
11180 Formal : constant Entity_Id := First_Formal (Init);
11181 begin
11182 if Ekind (Init) = E_Procedure
11183 and then Chars (Init) = Name_Initialize
11184 and then Comes_From_Source (Init)
11185 and then Present (Formal)
11186 and then Etype (Formal) = BT
11187 and then No (Next_Formal (Formal))
11188 and then (Ada_Version < Ada_2012
11189 or else not Null_Present (Parent (Init)))
11190 then
11191 return True;
11192 end if;
11193 end;
11195 Next_Elmt (P);
11196 end loop;
11197 end if;
11199 -- Here if type itself does not have a non-null Initialize operation:
11200 -- check immediate ancestor.
11202 if Is_Derived_Type (BT)
11203 and then Has_Overriding_Initialize (Etype (BT))
11204 then
11205 return True;
11206 end if;
11207 end if;
11209 return False;
11210 end Has_Overriding_Initialize;
11212 --------------------------------------
11213 -- Has_Preelaborable_Initialization --
11214 --------------------------------------
11216 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
11217 Has_PE : Boolean;
11219 procedure Check_Components (E : Entity_Id);
11220 -- Check component/discriminant chain, sets Has_PE False if a component
11221 -- or discriminant does not meet the preelaborable initialization rules.
11223 ----------------------
11224 -- Check_Components --
11225 ----------------------
11227 procedure Check_Components (E : Entity_Id) is
11228 Ent : Entity_Id;
11229 Exp : Node_Id;
11231 begin
11232 -- Loop through entities of record or protected type
11234 Ent := E;
11235 while Present (Ent) loop
11237 -- We are interested only in components and discriminants
11239 Exp := Empty;
11241 case Ekind (Ent) is
11242 when E_Component =>
11244 -- Get default expression if any. If there is no declaration
11245 -- node, it means we have an internal entity. The parent and
11246 -- tag fields are examples of such entities. For such cases,
11247 -- we just test the type of the entity.
11249 if Present (Declaration_Node (Ent)) then
11250 Exp := Expression (Declaration_Node (Ent));
11251 end if;
11253 when E_Discriminant =>
11255 -- Note: for a renamed discriminant, the Declaration_Node
11256 -- may point to the one from the ancestor, and have a
11257 -- different expression, so use the proper attribute to
11258 -- retrieve the expression from the derived constraint.
11260 Exp := Discriminant_Default_Value (Ent);
11262 when others =>
11263 goto Check_Next_Entity;
11264 end case;
11266 -- A component has PI if it has no default expression and the
11267 -- component type has PI.
11269 if No (Exp) then
11270 if not Has_Preelaborable_Initialization (Etype (Ent)) then
11271 Has_PE := False;
11272 exit;
11273 end if;
11275 -- Require the default expression to be preelaborable
11277 elsif not Is_Preelaborable_Construct (Exp) then
11278 Has_PE := False;
11279 exit;
11280 end if;
11282 <<Check_Next_Entity>>
11283 Next_Entity (Ent);
11284 end loop;
11285 end Check_Components;
11287 -- Start of processing for Has_Preelaborable_Initialization
11289 begin
11290 -- Immediate return if already marked as known preelaborable init. This
11291 -- covers types for which this function has already been called once
11292 -- and returned True (in which case the result is cached), and also
11293 -- types to which a pragma Preelaborable_Initialization applies.
11295 if Known_To_Have_Preelab_Init (E) then
11296 return True;
11297 end if;
11299 -- If the type is a subtype representing a generic actual type, then
11300 -- test whether its base type has preelaborable initialization since
11301 -- the subtype representing the actual does not inherit this attribute
11302 -- from the actual or formal. (but maybe it should???)
11304 if Is_Generic_Actual_Type (E) then
11305 return Has_Preelaborable_Initialization (Base_Type (E));
11306 end if;
11308 -- All elementary types have preelaborable initialization
11310 if Is_Elementary_Type (E) then
11311 Has_PE := True;
11313 -- Array types have PI if the component type has PI
11315 elsif Is_Array_Type (E) then
11316 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
11318 -- A derived type has preelaborable initialization if its parent type
11319 -- has preelaborable initialization and (in the case of a derived record
11320 -- extension) if the non-inherited components all have preelaborable
11321 -- initialization. However, a user-defined controlled type with an
11322 -- overriding Initialize procedure does not have preelaborable
11323 -- initialization.
11325 elsif Is_Derived_Type (E) then
11327 -- If the derived type is a private extension then it doesn't have
11328 -- preelaborable initialization.
11330 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
11331 return False;
11332 end if;
11334 -- First check whether ancestor type has preelaborable initialization
11336 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
11338 -- If OK, check extension components (if any)
11340 if Has_PE and then Is_Record_Type (E) then
11341 Check_Components (First_Entity (E));
11342 end if;
11344 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
11345 -- with a user defined Initialize procedure does not have PI. If
11346 -- the type is untagged, the control primitives come from a component
11347 -- that has already been checked.
11349 if Has_PE
11350 and then Is_Controlled (E)
11351 and then Is_Tagged_Type (E)
11352 and then Has_Overriding_Initialize (E)
11353 then
11354 Has_PE := False;
11355 end if;
11357 -- Private types not derived from a type having preelaborable init and
11358 -- that are not marked with pragma Preelaborable_Initialization do not
11359 -- have preelaborable initialization.
11361 elsif Is_Private_Type (E) then
11362 return False;
11364 -- Record type has PI if it is non private and all components have PI
11366 elsif Is_Record_Type (E) then
11367 Has_PE := True;
11368 Check_Components (First_Entity (E));
11370 -- Protected types must not have entries, and components must meet
11371 -- same set of rules as for record components.
11373 elsif Is_Protected_Type (E) then
11374 if Has_Entries (E) then
11375 Has_PE := False;
11376 else
11377 Has_PE := True;
11378 Check_Components (First_Entity (E));
11379 Check_Components (First_Private_Entity (E));
11380 end if;
11382 -- Type System.Address always has preelaborable initialization
11384 elsif Is_RTE (E, RE_Address) then
11385 Has_PE := True;
11387 -- In all other cases, type does not have preelaborable initialization
11389 else
11390 return False;
11391 end if;
11393 -- If type has preelaborable initialization, cache result
11395 if Has_PE then
11396 Set_Known_To_Have_Preelab_Init (E);
11397 end if;
11399 return Has_PE;
11400 end Has_Preelaborable_Initialization;
11402 ---------------------------
11403 -- Has_Private_Component --
11404 ---------------------------
11406 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
11407 Btype : Entity_Id := Base_Type (Type_Id);
11408 Component : Entity_Id;
11410 begin
11411 if Error_Posted (Type_Id)
11412 or else Error_Posted (Btype)
11413 then
11414 return False;
11415 end if;
11417 if Is_Class_Wide_Type (Btype) then
11418 Btype := Root_Type (Btype);
11419 end if;
11421 if Is_Private_Type (Btype) then
11422 declare
11423 UT : constant Entity_Id := Underlying_Type (Btype);
11424 begin
11425 if No (UT) then
11426 if No (Full_View (Btype)) then
11427 return not Is_Generic_Type (Btype)
11428 and then
11429 not Is_Generic_Type (Root_Type (Btype));
11430 else
11431 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
11432 end if;
11433 else
11434 return not Is_Frozen (UT) and then Has_Private_Component (UT);
11435 end if;
11436 end;
11438 elsif Is_Array_Type (Btype) then
11439 return Has_Private_Component (Component_Type (Btype));
11441 elsif Is_Record_Type (Btype) then
11442 Component := First_Component (Btype);
11443 while Present (Component) loop
11444 if Has_Private_Component (Etype (Component)) then
11445 return True;
11446 end if;
11448 Next_Component (Component);
11449 end loop;
11451 return False;
11453 elsif Is_Protected_Type (Btype)
11454 and then Present (Corresponding_Record_Type (Btype))
11455 then
11456 return Has_Private_Component (Corresponding_Record_Type (Btype));
11458 else
11459 return False;
11460 end if;
11461 end Has_Private_Component;
11463 ----------------------
11464 -- Has_Signed_Zeros --
11465 ----------------------
11467 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
11468 begin
11469 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
11470 end Has_Signed_Zeros;
11472 ------------------------------
11473 -- Has_Significant_Contract --
11474 ------------------------------
11476 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
11477 Subp_Nam : constant Name_Id := Chars (Subp_Id);
11479 begin
11480 -- _Finalizer procedure
11482 if Subp_Nam = Name_uFinalizer then
11483 return False;
11485 -- _Postconditions procedure
11487 elsif Subp_Nam = Name_uPostconditions then
11488 return False;
11490 -- Predicate function
11492 elsif Ekind (Subp_Id) = E_Function
11493 and then Is_Predicate_Function (Subp_Id)
11494 then
11495 return False;
11497 -- TSS subprogram
11499 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
11500 return False;
11502 else
11503 return True;
11504 end if;
11505 end Has_Significant_Contract;
11507 -----------------------------
11508 -- Has_Static_Array_Bounds --
11509 -----------------------------
11511 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
11512 All_Static : Boolean;
11513 Dummy : Boolean;
11515 begin
11516 Examine_Array_Bounds (Typ, All_Static, Dummy);
11518 return All_Static;
11519 end Has_Static_Array_Bounds;
11521 ---------------------------------------
11522 -- Has_Static_Non_Empty_Array_Bounds --
11523 ---------------------------------------
11525 function Has_Static_Non_Empty_Array_Bounds (Typ : Node_Id) return Boolean is
11526 All_Static : Boolean;
11527 Has_Empty : Boolean;
11529 begin
11530 Examine_Array_Bounds (Typ, All_Static, Has_Empty);
11532 return All_Static and not Has_Empty;
11533 end Has_Static_Non_Empty_Array_Bounds;
11535 ----------------
11536 -- Has_Stream --
11537 ----------------
11539 function Has_Stream (T : Entity_Id) return Boolean is
11540 E : Entity_Id;
11542 begin
11543 if No (T) then
11544 return False;
11546 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
11547 return True;
11549 elsif Is_Array_Type (T) then
11550 return Has_Stream (Component_Type (T));
11552 elsif Is_Record_Type (T) then
11553 E := First_Component (T);
11554 while Present (E) loop
11555 if Has_Stream (Etype (E)) then
11556 return True;
11557 else
11558 Next_Component (E);
11559 end if;
11560 end loop;
11562 return False;
11564 elsif Is_Private_Type (T) then
11565 return Has_Stream (Underlying_Type (T));
11567 else
11568 return False;
11569 end if;
11570 end Has_Stream;
11572 ----------------
11573 -- Has_Suffix --
11574 ----------------
11576 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
11577 begin
11578 Get_Name_String (Chars (E));
11579 return Name_Buffer (Name_Len) = Suffix;
11580 end Has_Suffix;
11582 ----------------
11583 -- Add_Suffix --
11584 ----------------
11586 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
11587 begin
11588 Get_Name_String (Chars (E));
11589 Add_Char_To_Name_Buffer (Suffix);
11590 return Name_Find;
11591 end Add_Suffix;
11593 -------------------
11594 -- Remove_Suffix --
11595 -------------------
11597 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
11598 begin
11599 pragma Assert (Has_Suffix (E, Suffix));
11600 Get_Name_String (Chars (E));
11601 Name_Len := Name_Len - 1;
11602 return Name_Find;
11603 end Remove_Suffix;
11605 ----------------------------------
11606 -- Replace_Null_By_Null_Address --
11607 ----------------------------------
11609 procedure Replace_Null_By_Null_Address (N : Node_Id) is
11610 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id);
11611 -- Replace operand Op with a reference to Null_Address when the operand
11612 -- denotes a null Address. Other_Op denotes the other operand.
11614 --------------------------
11615 -- Replace_Null_Operand --
11616 --------------------------
11618 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id) is
11619 begin
11620 -- Check the type of the complementary operand since the N_Null node
11621 -- has not been decorated yet.
11623 if Nkind (Op) = N_Null
11624 and then Is_Descendant_Of_Address (Etype (Other_Op))
11625 then
11626 Rewrite (Op, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (Op)));
11627 end if;
11628 end Replace_Null_Operand;
11630 -- Start of processing for Replace_Null_By_Null_Address
11632 begin
11633 pragma Assert (Relaxed_RM_Semantics);
11634 pragma Assert (Nkind_In (N, N_Null,
11635 N_Op_Eq,
11636 N_Op_Ge,
11637 N_Op_Gt,
11638 N_Op_Le,
11639 N_Op_Lt,
11640 N_Op_Ne));
11642 if Nkind (N) = N_Null then
11643 Rewrite (N, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
11645 else
11646 declare
11647 L : constant Node_Id := Left_Opnd (N);
11648 R : constant Node_Id := Right_Opnd (N);
11650 begin
11651 Replace_Null_Operand (L, Other_Op => R);
11652 Replace_Null_Operand (R, Other_Op => L);
11653 end;
11654 end if;
11655 end Replace_Null_By_Null_Address;
11657 --------------------------
11658 -- Has_Tagged_Component --
11659 --------------------------
11661 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
11662 Comp : Entity_Id;
11664 begin
11665 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
11666 return Has_Tagged_Component (Underlying_Type (Typ));
11668 elsif Is_Array_Type (Typ) then
11669 return Has_Tagged_Component (Component_Type (Typ));
11671 elsif Is_Tagged_Type (Typ) then
11672 return True;
11674 elsif Is_Record_Type (Typ) then
11675 Comp := First_Component (Typ);
11676 while Present (Comp) loop
11677 if Has_Tagged_Component (Etype (Comp)) then
11678 return True;
11679 end if;
11681 Next_Component (Comp);
11682 end loop;
11684 return False;
11686 else
11687 return False;
11688 end if;
11689 end Has_Tagged_Component;
11691 -----------------------------
11692 -- Has_Undefined_Reference --
11693 -----------------------------
11695 function Has_Undefined_Reference (Expr : Node_Id) return Boolean is
11696 Has_Undef_Ref : Boolean := False;
11697 -- Flag set when expression Expr contains at least one undefined
11698 -- reference.
11700 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result;
11701 -- Determine whether N denotes a reference and if it does, whether it is
11702 -- undefined.
11704 ----------------------------
11705 -- Is_Undefined_Reference --
11706 ----------------------------
11708 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result is
11709 begin
11710 if Is_Entity_Name (N)
11711 and then Present (Entity (N))
11712 and then Entity (N) = Any_Id
11713 then
11714 Has_Undef_Ref := True;
11715 return Abandon;
11716 end if;
11718 return OK;
11719 end Is_Undefined_Reference;
11721 procedure Find_Undefined_References is
11722 new Traverse_Proc (Is_Undefined_Reference);
11724 -- Start of processing for Has_Undefined_Reference
11726 begin
11727 Find_Undefined_References (Expr);
11729 return Has_Undef_Ref;
11730 end Has_Undefined_Reference;
11732 ----------------------------
11733 -- Has_Volatile_Component --
11734 ----------------------------
11736 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
11737 Comp : Entity_Id;
11739 begin
11740 if Has_Volatile_Components (Typ) then
11741 return True;
11743 elsif Is_Array_Type (Typ) then
11744 return Is_Volatile (Component_Type (Typ));
11746 elsif Is_Record_Type (Typ) then
11747 Comp := First_Component (Typ);
11748 while Present (Comp) loop
11749 if Is_Volatile_Object (Comp) then
11750 return True;
11751 end if;
11753 Comp := Next_Component (Comp);
11754 end loop;
11755 end if;
11757 return False;
11758 end Has_Volatile_Component;
11760 -------------------------
11761 -- Implementation_Kind --
11762 -------------------------
11764 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
11765 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
11766 Arg : Node_Id;
11767 begin
11768 pragma Assert (Present (Impl_Prag));
11769 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
11770 return Chars (Get_Pragma_Arg (Arg));
11771 end Implementation_Kind;
11773 --------------------------
11774 -- Implements_Interface --
11775 --------------------------
11777 function Implements_Interface
11778 (Typ_Ent : Entity_Id;
11779 Iface_Ent : Entity_Id;
11780 Exclude_Parents : Boolean := False) return Boolean
11782 Ifaces_List : Elist_Id;
11783 Elmt : Elmt_Id;
11784 Iface : Entity_Id := Base_Type (Iface_Ent);
11785 Typ : Entity_Id := Base_Type (Typ_Ent);
11787 begin
11788 if Is_Class_Wide_Type (Typ) then
11789 Typ := Root_Type (Typ);
11790 end if;
11792 if not Has_Interfaces (Typ) then
11793 return False;
11794 end if;
11796 if Is_Class_Wide_Type (Iface) then
11797 Iface := Root_Type (Iface);
11798 end if;
11800 Collect_Interfaces (Typ, Ifaces_List);
11802 Elmt := First_Elmt (Ifaces_List);
11803 while Present (Elmt) loop
11804 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
11805 and then Exclude_Parents
11806 then
11807 null;
11809 elsif Node (Elmt) = Iface then
11810 return True;
11811 end if;
11813 Next_Elmt (Elmt);
11814 end loop;
11816 return False;
11817 end Implements_Interface;
11819 ------------------------------------
11820 -- In_Assertion_Expression_Pragma --
11821 ------------------------------------
11823 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
11824 Par : Node_Id;
11825 Prag : Node_Id := Empty;
11827 begin
11828 -- Climb the parent chain looking for an enclosing pragma
11830 Par := N;
11831 while Present (Par) loop
11832 if Nkind (Par) = N_Pragma then
11833 Prag := Par;
11834 exit;
11836 -- Precondition-like pragmas are expanded into if statements, check
11837 -- the original node instead.
11839 elsif Nkind (Original_Node (Par)) = N_Pragma then
11840 Prag := Original_Node (Par);
11841 exit;
11843 -- The expansion of attribute 'Old generates a constant to capture
11844 -- the result of the prefix. If the parent traversal reaches
11845 -- one of these constants, then the node technically came from a
11846 -- postcondition-like pragma. Note that the Ekind is not tested here
11847 -- because N may be the expression of an object declaration which is
11848 -- currently being analyzed. Such objects carry Ekind of E_Void.
11850 elsif Nkind (Par) = N_Object_Declaration
11851 and then Constant_Present (Par)
11852 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
11853 then
11854 return True;
11856 -- Prevent the search from going too far
11858 elsif Is_Body_Or_Package_Declaration (Par) then
11859 return False;
11860 end if;
11862 Par := Parent (Par);
11863 end loop;
11865 return
11866 Present (Prag)
11867 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
11868 end In_Assertion_Expression_Pragma;
11870 ----------------------
11871 -- In_Generic_Scope --
11872 ----------------------
11874 function In_Generic_Scope (E : Entity_Id) return Boolean is
11875 S : Entity_Id;
11877 begin
11878 S := Scope (E);
11879 while Present (S) and then S /= Standard_Standard loop
11880 if Is_Generic_Unit (S) then
11881 return True;
11882 end if;
11884 S := Scope (S);
11885 end loop;
11887 return False;
11888 end In_Generic_Scope;
11890 -----------------
11891 -- In_Instance --
11892 -----------------
11894 function In_Instance return Boolean is
11895 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
11896 S : Entity_Id;
11898 begin
11899 S := Current_Scope;
11900 while Present (S) and then S /= Standard_Standard loop
11901 if Is_Generic_Instance (S) then
11903 -- A child instance is always compiled in the context of a parent
11904 -- instance. Nevertheless, the actuals are not analyzed in an
11905 -- instance context. We detect this case by examining the current
11906 -- compilation unit, which must be a child instance, and checking
11907 -- that it is not currently on the scope stack.
11909 if Is_Child_Unit (Curr_Unit)
11910 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
11911 N_Package_Instantiation
11912 and then not In_Open_Scopes (Curr_Unit)
11913 then
11914 return False;
11915 else
11916 return True;
11917 end if;
11918 end if;
11920 S := Scope (S);
11921 end loop;
11923 return False;
11924 end In_Instance;
11926 ----------------------
11927 -- In_Instance_Body --
11928 ----------------------
11930 function In_Instance_Body return Boolean is
11931 S : Entity_Id;
11933 begin
11934 S := Current_Scope;
11935 while Present (S) and then S /= Standard_Standard loop
11936 if Ekind_In (S, E_Function, E_Procedure)
11937 and then Is_Generic_Instance (S)
11938 then
11939 return True;
11941 elsif Ekind (S) = E_Package
11942 and then In_Package_Body (S)
11943 and then Is_Generic_Instance (S)
11944 then
11945 return True;
11946 end if;
11948 S := Scope (S);
11949 end loop;
11951 return False;
11952 end In_Instance_Body;
11954 -----------------------------
11955 -- In_Instance_Not_Visible --
11956 -----------------------------
11958 function In_Instance_Not_Visible return Boolean is
11959 S : Entity_Id;
11961 begin
11962 S := Current_Scope;
11963 while Present (S) and then S /= Standard_Standard loop
11964 if Ekind_In (S, E_Function, E_Procedure)
11965 and then Is_Generic_Instance (S)
11966 then
11967 return True;
11969 elsif Ekind (S) = E_Package
11970 and then (In_Package_Body (S) or else In_Private_Part (S))
11971 and then Is_Generic_Instance (S)
11972 then
11973 return True;
11974 end if;
11976 S := Scope (S);
11977 end loop;
11979 return False;
11980 end In_Instance_Not_Visible;
11982 ------------------------------
11983 -- In_Instance_Visible_Part --
11984 ------------------------------
11986 function In_Instance_Visible_Part
11987 (Id : Entity_Id := Current_Scope) return Boolean
11989 Inst : Entity_Id;
11991 begin
11992 Inst := Id;
11993 while Present (Inst) and then Inst /= Standard_Standard loop
11994 if Ekind (Inst) = E_Package
11995 and then Is_Generic_Instance (Inst)
11996 and then not In_Package_Body (Inst)
11997 and then not In_Private_Part (Inst)
11998 then
11999 return True;
12000 end if;
12002 Inst := Scope (Inst);
12003 end loop;
12005 return False;
12006 end In_Instance_Visible_Part;
12008 ---------------------
12009 -- In_Package_Body --
12010 ---------------------
12012 function In_Package_Body return Boolean is
12013 S : Entity_Id;
12015 begin
12016 S := Current_Scope;
12017 while Present (S) and then S /= Standard_Standard loop
12018 if Ekind (S) = E_Package and then In_Package_Body (S) then
12019 return True;
12020 else
12021 S := Scope (S);
12022 end if;
12023 end loop;
12025 return False;
12026 end In_Package_Body;
12028 --------------------------
12029 -- In_Pragma_Expression --
12030 --------------------------
12032 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
12033 P : Node_Id;
12034 begin
12035 P := Parent (N);
12036 loop
12037 if No (P) then
12038 return False;
12039 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
12040 return True;
12041 else
12042 P := Parent (P);
12043 end if;
12044 end loop;
12045 end In_Pragma_Expression;
12047 ---------------------------
12048 -- In_Pre_Post_Condition --
12049 ---------------------------
12051 function In_Pre_Post_Condition (N : Node_Id) return Boolean is
12052 Par : Node_Id;
12053 Prag : Node_Id := Empty;
12054 Prag_Id : Pragma_Id;
12056 begin
12057 -- Climb the parent chain looking for an enclosing pragma
12059 Par := N;
12060 while Present (Par) loop
12061 if Nkind (Par) = N_Pragma then
12062 Prag := Par;
12063 exit;
12065 -- Prevent the search from going too far
12067 elsif Is_Body_Or_Package_Declaration (Par) then
12068 exit;
12069 end if;
12071 Par := Parent (Par);
12072 end loop;
12074 if Present (Prag) then
12075 Prag_Id := Get_Pragma_Id (Prag);
12077 return
12078 Prag_Id = Pragma_Post
12079 or else Prag_Id = Pragma_Post_Class
12080 or else Prag_Id = Pragma_Postcondition
12081 or else Prag_Id = Pragma_Pre
12082 or else Prag_Id = Pragma_Pre_Class
12083 or else Prag_Id = Pragma_Precondition;
12085 -- Otherwise the node is not enclosed by a pre/postcondition pragma
12087 else
12088 return False;
12089 end if;
12090 end In_Pre_Post_Condition;
12092 -------------------------------------
12093 -- In_Reverse_Storage_Order_Object --
12094 -------------------------------------
12096 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
12097 Pref : Node_Id;
12098 Btyp : Entity_Id := Empty;
12100 begin
12101 -- Climb up indexed components
12103 Pref := N;
12104 loop
12105 case Nkind (Pref) is
12106 when N_Selected_Component =>
12107 Pref := Prefix (Pref);
12108 exit;
12110 when N_Indexed_Component =>
12111 Pref := Prefix (Pref);
12113 when others =>
12114 Pref := Empty;
12115 exit;
12116 end case;
12117 end loop;
12119 if Present (Pref) then
12120 Btyp := Base_Type (Etype (Pref));
12121 end if;
12123 return Present (Btyp)
12124 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
12125 and then Reverse_Storage_Order (Btyp);
12126 end In_Reverse_Storage_Order_Object;
12128 ------------------------------
12129 -- In_Same_Declarative_Part --
12130 ------------------------------
12132 function In_Same_Declarative_Part
12133 (Context : Node_Id;
12134 N : Node_Id) return Boolean
12136 Cont : Node_Id := Context;
12137 Nod : Node_Id;
12139 begin
12140 if Nkind (Cont) = N_Compilation_Unit_Aux then
12141 Cont := Parent (Cont);
12142 end if;
12144 Nod := Parent (N);
12145 while Present (Nod) loop
12146 if Nod = Cont then
12147 return True;
12149 elsif Nkind_In (Nod, N_Accept_Statement,
12150 N_Block_Statement,
12151 N_Compilation_Unit,
12152 N_Entry_Body,
12153 N_Package_Body,
12154 N_Package_Declaration,
12155 N_Protected_Body,
12156 N_Subprogram_Body,
12157 N_Task_Body)
12158 then
12159 return False;
12161 elsif Nkind (Nod) = N_Subunit then
12162 Nod := Corresponding_Stub (Nod);
12164 else
12165 Nod := Parent (Nod);
12166 end if;
12167 end loop;
12169 return False;
12170 end In_Same_Declarative_Part;
12172 --------------------------------------
12173 -- In_Subprogram_Or_Concurrent_Unit --
12174 --------------------------------------
12176 function In_Subprogram_Or_Concurrent_Unit return Boolean is
12177 E : Entity_Id;
12178 K : Entity_Kind;
12180 begin
12181 -- Use scope chain to check successively outer scopes
12183 E := Current_Scope;
12184 loop
12185 K := Ekind (E);
12187 if K in Subprogram_Kind
12188 or else K in Concurrent_Kind
12189 or else K in Generic_Subprogram_Kind
12190 then
12191 return True;
12193 elsif E = Standard_Standard then
12194 return False;
12195 end if;
12197 E := Scope (E);
12198 end loop;
12199 end In_Subprogram_Or_Concurrent_Unit;
12201 ----------------
12202 -- In_Subtree --
12203 ----------------
12205 function In_Subtree (N : Node_Id; Root : Node_Id) return Boolean is
12206 Curr : Node_Id;
12208 begin
12209 Curr := N;
12210 while Present (Curr) loop
12211 if Curr = Root then
12212 return True;
12213 end if;
12215 Curr := Parent (Curr);
12216 end loop;
12218 return False;
12219 end In_Subtree;
12221 ----------------
12222 -- In_Subtree --
12223 ----------------
12225 function In_Subtree
12226 (N : Node_Id;
12227 Root1 : Node_Id;
12228 Root2 : Node_Id) return Boolean
12230 Curr : Node_Id;
12232 begin
12233 Curr := N;
12234 while Present (Curr) loop
12235 if Curr = Root1 or else Curr = Root2 then
12236 return True;
12237 end if;
12239 Curr := Parent (Curr);
12240 end loop;
12242 return False;
12243 end In_Subtree;
12245 ---------------------
12246 -- In_Visible_Part --
12247 ---------------------
12249 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
12250 begin
12251 return Is_Package_Or_Generic_Package (Scope_Id)
12252 and then In_Open_Scopes (Scope_Id)
12253 and then not In_Package_Body (Scope_Id)
12254 and then not In_Private_Part (Scope_Id);
12255 end In_Visible_Part;
12257 --------------------------------
12258 -- Incomplete_Or_Partial_View --
12259 --------------------------------
12261 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
12262 function Inspect_Decls
12263 (Decls : List_Id;
12264 Taft : Boolean := False) return Entity_Id;
12265 -- Check whether a declarative region contains the incomplete or partial
12266 -- view of Id.
12268 -------------------
12269 -- Inspect_Decls --
12270 -------------------
12272 function Inspect_Decls
12273 (Decls : List_Id;
12274 Taft : Boolean := False) return Entity_Id
12276 Decl : Node_Id;
12277 Match : Node_Id;
12279 begin
12280 Decl := First (Decls);
12281 while Present (Decl) loop
12282 Match := Empty;
12284 -- The partial view of a Taft-amendment type is an incomplete
12285 -- type.
12287 if Taft then
12288 if Nkind (Decl) = N_Incomplete_Type_Declaration then
12289 Match := Defining_Identifier (Decl);
12290 end if;
12292 -- Otherwise look for a private type whose full view matches the
12293 -- input type. Note that this checks full_type_declaration nodes
12294 -- to account for derivations from a private type where the type
12295 -- declaration hold the partial view and the full view is an
12296 -- itype.
12298 elsif Nkind_In (Decl, N_Full_Type_Declaration,
12299 N_Private_Extension_Declaration,
12300 N_Private_Type_Declaration)
12301 then
12302 Match := Defining_Identifier (Decl);
12303 end if;
12305 -- Guard against unanalyzed entities
12307 if Present (Match)
12308 and then Is_Type (Match)
12309 and then Present (Full_View (Match))
12310 and then Full_View (Match) = Id
12311 then
12312 return Match;
12313 end if;
12315 Next (Decl);
12316 end loop;
12318 return Empty;
12319 end Inspect_Decls;
12321 -- Local variables
12323 Prev : Entity_Id;
12325 -- Start of processing for Incomplete_Or_Partial_View
12327 begin
12328 -- Deferred constant or incomplete type case
12330 Prev := Current_Entity_In_Scope (Id);
12332 if Present (Prev)
12333 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
12334 and then Present (Full_View (Prev))
12335 and then Full_View (Prev) = Id
12336 then
12337 return Prev;
12338 end if;
12340 -- Private or Taft amendment type case
12342 declare
12343 Pkg : constant Entity_Id := Scope (Id);
12344 Pkg_Decl : Node_Id := Pkg;
12346 begin
12347 if Present (Pkg)
12348 and then Ekind_In (Pkg, E_Generic_Package, E_Package)
12349 then
12350 while Nkind (Pkg_Decl) /= N_Package_Specification loop
12351 Pkg_Decl := Parent (Pkg_Decl);
12352 end loop;
12354 -- It is knows that Typ has a private view, look for it in the
12355 -- visible declarations of the enclosing scope. A special case
12356 -- of this is when the two views have been exchanged - the full
12357 -- appears earlier than the private.
12359 if Has_Private_Declaration (Id) then
12360 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
12362 -- Exchanged view case, look in the private declarations
12364 if No (Prev) then
12365 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
12366 end if;
12368 return Prev;
12370 -- Otherwise if this is the package body, then Typ is a potential
12371 -- Taft amendment type. The incomplete view should be located in
12372 -- the private declarations of the enclosing scope.
12374 elsif In_Package_Body (Pkg) then
12375 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
12376 end if;
12377 end if;
12378 end;
12380 -- The type has no incomplete or private view
12382 return Empty;
12383 end Incomplete_Or_Partial_View;
12385 ---------------------------------------
12386 -- Incomplete_View_From_Limited_With --
12387 ---------------------------------------
12389 function Incomplete_View_From_Limited_With
12390 (Typ : Entity_Id) return Entity_Id
12392 begin
12393 -- It might make sense to make this an attribute in Einfo, and set it
12394 -- in Sem_Ch10 in Build_Shadow_Entity. However, we're running short on
12395 -- slots for new attributes, and it seems a bit simpler to just search
12396 -- the Limited_View (if it exists) for an incomplete type whose
12397 -- Non_Limited_View is Typ.
12399 if Ekind (Scope (Typ)) = E_Package
12400 and then Present (Limited_View (Scope (Typ)))
12401 then
12402 declare
12403 Ent : Entity_Id := First_Entity (Limited_View (Scope (Typ)));
12404 begin
12405 while Present (Ent) loop
12406 if Ekind (Ent) in Incomplete_Kind
12407 and then Non_Limited_View (Ent) = Typ
12408 then
12409 return Ent;
12410 end if;
12412 Ent := Next_Entity (Ent);
12413 end loop;
12414 end;
12415 end if;
12417 return Typ;
12418 end Incomplete_View_From_Limited_With;
12420 ----------------------------------
12421 -- Indexed_Component_Bit_Offset --
12422 ----------------------------------
12424 function Indexed_Component_Bit_Offset (N : Node_Id) return Uint is
12425 Exp : constant Node_Id := First (Expressions (N));
12426 Typ : constant Entity_Id := Etype (Prefix (N));
12427 Off : constant Uint := Component_Size (Typ);
12428 Ind : Node_Id;
12430 begin
12431 -- Return early if the component size is not known or variable
12433 if Off = No_Uint or else Off < Uint_0 then
12434 return No_Uint;
12435 end if;
12437 -- Deal with the degenerate case of an empty component
12439 if Off = Uint_0 then
12440 return Off;
12441 end if;
12443 -- Check that both the index value and the low bound are known
12445 if not Compile_Time_Known_Value (Exp) then
12446 return No_Uint;
12447 end if;
12449 Ind := First_Index (Typ);
12450 if No (Ind) then
12451 return No_Uint;
12452 end if;
12454 if Nkind (Ind) = N_Subtype_Indication then
12455 Ind := Constraint (Ind);
12457 if Nkind (Ind) = N_Range_Constraint then
12458 Ind := Range_Expression (Ind);
12459 end if;
12460 end if;
12462 if Nkind (Ind) /= N_Range
12463 or else not Compile_Time_Known_Value (Low_Bound (Ind))
12464 then
12465 return No_Uint;
12466 end if;
12468 -- Return the scaled offset
12470 return Off * (Expr_Value (Exp) - Expr_Value (Low_Bound ((Ind))));
12471 end Indexed_Component_Bit_Offset;
12473 ----------------------------
12474 -- Inherit_Rep_Item_Chain --
12475 ----------------------------
12477 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
12478 Item : Node_Id;
12479 Next_Item : Node_Id;
12481 begin
12482 -- There are several inheritance scenarios to consider depending on
12483 -- whether both types have rep item chains and whether the destination
12484 -- type already inherits part of the source type's rep item chain.
12486 -- 1) The source type lacks a rep item chain
12487 -- From_Typ ---> Empty
12489 -- Typ --------> Item (or Empty)
12491 -- In this case inheritance cannot take place because there are no items
12492 -- to inherit.
12494 -- 2) The destination type lacks a rep item chain
12495 -- From_Typ ---> Item ---> ...
12497 -- Typ --------> Empty
12499 -- Inheritance takes place by setting the First_Rep_Item of the
12500 -- destination type to the First_Rep_Item of the source type.
12501 -- From_Typ ---> Item ---> ...
12502 -- ^
12503 -- Typ -----------+
12505 -- 3.1) Both source and destination types have at least one rep item.
12506 -- The destination type does NOT inherit a rep item from the source
12507 -- type.
12508 -- From_Typ ---> Item ---> Item
12510 -- Typ --------> Item ---> Item
12512 -- Inheritance takes place by setting the Next_Rep_Item of the last item
12513 -- of the destination type to the First_Rep_Item of the source type.
12514 -- From_Typ -------------------> Item ---> Item
12515 -- ^
12516 -- Typ --------> Item ---> Item --+
12518 -- 3.2) Both source and destination types have at least one rep item.
12519 -- The destination type DOES inherit part of the rep item chain of the
12520 -- source type.
12521 -- From_Typ ---> Item ---> Item ---> Item
12522 -- ^
12523 -- Typ --------> Item ------+
12525 -- This rare case arises when the full view of a private extension must
12526 -- inherit the rep item chain from the full view of its parent type and
12527 -- the full view of the parent type contains extra rep items. Currently
12528 -- only invariants may lead to such form of inheritance.
12530 -- type From_Typ is tagged private
12531 -- with Type_Invariant'Class => Item_2;
12533 -- type Typ is new From_Typ with private
12534 -- with Type_Invariant => Item_4;
12536 -- At this point the rep item chains contain the following items
12538 -- From_Typ -----------> Item_2 ---> Item_3
12539 -- ^
12540 -- Typ --------> Item_4 --+
12542 -- The full views of both types may introduce extra invariants
12544 -- type From_Typ is tagged null record
12545 -- with Type_Invariant => Item_1;
12547 -- type Typ is new From_Typ with null record;
12549 -- The full view of Typ would have to inherit any new rep items added to
12550 -- the full view of From_Typ.
12552 -- From_Typ -----------> Item_1 ---> Item_2 ---> Item_3
12553 -- ^
12554 -- Typ --------> Item_4 --+
12556 -- To achieve this form of inheritance, the destination type must first
12557 -- sever the link between its own rep chain and that of the source type,
12558 -- then inheritance 3.1 takes place.
12560 -- Case 1: The source type lacks a rep item chain
12562 if No (First_Rep_Item (From_Typ)) then
12563 return;
12565 -- Case 2: The destination type lacks a rep item chain
12567 elsif No (First_Rep_Item (Typ)) then
12568 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
12570 -- Case 3: Both the source and destination types have at least one rep
12571 -- item. Traverse the rep item chain of the destination type to find the
12572 -- last rep item.
12574 else
12575 Item := Empty;
12576 Next_Item := First_Rep_Item (Typ);
12577 while Present (Next_Item) loop
12579 -- Detect a link between the destination type's rep chain and that
12580 -- of the source type. There are two possibilities:
12582 -- Variant 1
12583 -- Next_Item
12584 -- V
12585 -- From_Typ ---> Item_1 --->
12586 -- ^
12587 -- Typ -----------+
12589 -- Item is Empty
12591 -- Variant 2
12592 -- Next_Item
12593 -- V
12594 -- From_Typ ---> Item_1 ---> Item_2 --->
12595 -- ^
12596 -- Typ --------> Item_3 ------+
12597 -- ^
12598 -- Item
12600 if Has_Rep_Item (From_Typ, Next_Item) then
12601 exit;
12602 end if;
12604 Item := Next_Item;
12605 Next_Item := Next_Rep_Item (Next_Item);
12606 end loop;
12608 -- Inherit the source type's rep item chain
12610 if Present (Item) then
12611 Set_Next_Rep_Item (Item, First_Rep_Item (From_Typ));
12612 else
12613 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
12614 end if;
12615 end if;
12616 end Inherit_Rep_Item_Chain;
12618 ---------------------------------
12619 -- Insert_Explicit_Dereference --
12620 ---------------------------------
12622 procedure Insert_Explicit_Dereference (N : Node_Id) is
12623 New_Prefix : constant Node_Id := Relocate_Node (N);
12624 Ent : Entity_Id := Empty;
12625 Pref : Node_Id;
12626 I : Interp_Index;
12627 It : Interp;
12628 T : Entity_Id;
12630 begin
12631 Save_Interps (N, New_Prefix);
12633 Rewrite (N,
12634 Make_Explicit_Dereference (Sloc (Parent (N)),
12635 Prefix => New_Prefix));
12637 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
12639 if Is_Overloaded (New_Prefix) then
12641 -- The dereference is also overloaded, and its interpretations are
12642 -- the designated types of the interpretations of the original node.
12644 Set_Etype (N, Any_Type);
12646 Get_First_Interp (New_Prefix, I, It);
12647 while Present (It.Nam) loop
12648 T := It.Typ;
12650 if Is_Access_Type (T) then
12651 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
12652 end if;
12654 Get_Next_Interp (I, It);
12655 end loop;
12657 End_Interp_List;
12659 else
12660 -- Prefix is unambiguous: mark the original prefix (which might
12661 -- Come_From_Source) as a reference, since the new (relocated) one
12662 -- won't be taken into account.
12664 if Is_Entity_Name (New_Prefix) then
12665 Ent := Entity (New_Prefix);
12666 Pref := New_Prefix;
12668 -- For a retrieval of a subcomponent of some composite object,
12669 -- retrieve the ultimate entity if there is one.
12671 elsif Nkind_In (New_Prefix, N_Selected_Component,
12672 N_Indexed_Component)
12673 then
12674 Pref := Prefix (New_Prefix);
12675 while Present (Pref)
12676 and then Nkind_In (Pref, N_Selected_Component,
12677 N_Indexed_Component)
12678 loop
12679 Pref := Prefix (Pref);
12680 end loop;
12682 if Present (Pref) and then Is_Entity_Name (Pref) then
12683 Ent := Entity (Pref);
12684 end if;
12685 end if;
12687 -- Place the reference on the entity node
12689 if Present (Ent) then
12690 Generate_Reference (Ent, Pref);
12691 end if;
12692 end if;
12693 end Insert_Explicit_Dereference;
12695 ------------------------------------------
12696 -- Inspect_Deferred_Constant_Completion --
12697 ------------------------------------------
12699 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
12700 Decl : Node_Id;
12702 begin
12703 Decl := First (Decls);
12704 while Present (Decl) loop
12706 -- Deferred constant signature
12708 if Nkind (Decl) = N_Object_Declaration
12709 and then Constant_Present (Decl)
12710 and then No (Expression (Decl))
12712 -- No need to check internally generated constants
12714 and then Comes_From_Source (Decl)
12716 -- The constant is not completed. A full object declaration or a
12717 -- pragma Import complete a deferred constant.
12719 and then not Has_Completion (Defining_Identifier (Decl))
12720 then
12721 Error_Msg_N
12722 ("constant declaration requires initialization expression",
12723 Defining_Identifier (Decl));
12724 end if;
12726 Decl := Next (Decl);
12727 end loop;
12728 end Inspect_Deferred_Constant_Completion;
12730 -------------------------------
12731 -- Install_Elaboration_Model --
12732 -------------------------------
12734 procedure Install_Elaboration_Model (Unit_Id : Entity_Id) is
12735 function Find_Elaboration_Checks_Pragma (L : List_Id) return Node_Id;
12736 -- Try to find pragma Elaboration_Checks in arbitrary list L. Return
12737 -- Empty if there is no such pragma.
12739 ------------------------------------
12740 -- Find_Elaboration_Checks_Pragma --
12741 ------------------------------------
12743 function Find_Elaboration_Checks_Pragma (L : List_Id) return Node_Id is
12744 Item : Node_Id;
12746 begin
12747 Item := First (L);
12748 while Present (Item) loop
12749 if Nkind (Item) = N_Pragma
12750 and then Pragma_Name (Item) = Name_Elaboration_Checks
12751 then
12752 return Item;
12753 end if;
12755 Next (Item);
12756 end loop;
12758 return Empty;
12759 end Find_Elaboration_Checks_Pragma;
12761 -- Local variables
12763 Args : List_Id;
12764 Model : Node_Id;
12765 Prag : Node_Id;
12766 Unit : Node_Id;
12768 -- Start of processing for Install_Elaboration_Model
12770 begin
12771 -- Nothing to do when the unit does not exist
12773 if No (Unit_Id) then
12774 return;
12775 end if;
12777 Unit := Parent (Unit_Declaration_Node (Unit_Id));
12779 -- Nothing to do when the unit is not a library unit
12781 if Nkind (Unit) /= N_Compilation_Unit then
12782 return;
12783 end if;
12785 Prag := Find_Elaboration_Checks_Pragma (Context_Items (Unit));
12787 -- The compilation unit is subject to pragma Elaboration_Checks. Set the
12788 -- elaboration model as specified by the pragma.
12790 if Present (Prag) then
12791 Args := Pragma_Argument_Associations (Prag);
12793 -- Guard against an illegal pragma. The sole argument must be an
12794 -- identifier which specifies either Dynamic or Static model.
12796 if Present (Args) then
12797 Model := Get_Pragma_Arg (First (Args));
12799 if Nkind (Model) = N_Identifier then
12800 Dynamic_Elaboration_Checks := Chars (Model) = Name_Dynamic;
12801 end if;
12802 end if;
12803 end if;
12804 end Install_Elaboration_Model;
12806 -----------------------------
12807 -- Install_Generic_Formals --
12808 -----------------------------
12810 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
12811 E : Entity_Id;
12813 begin
12814 pragma Assert (Is_Generic_Subprogram (Subp_Id));
12816 E := First_Entity (Subp_Id);
12817 while Present (E) loop
12818 Install_Entity (E);
12819 Next_Entity (E);
12820 end loop;
12821 end Install_Generic_Formals;
12823 ------------------------
12824 -- Install_SPARK_Mode --
12825 ------------------------
12827 procedure Install_SPARK_Mode (Mode : SPARK_Mode_Type; Prag : Node_Id) is
12828 begin
12829 SPARK_Mode := Mode;
12830 SPARK_Mode_Pragma := Prag;
12831 end Install_SPARK_Mode;
12833 --------------------------
12834 -- Invalid_Scalar_Value --
12835 --------------------------
12837 function Invalid_Scalar_Value
12838 (Loc : Source_Ptr;
12839 Scal_Typ : Scalar_Id) return Node_Id
12841 function Invalid_Binder_Value return Node_Id;
12842 -- Return a reference to the corresponding invalid value for type
12843 -- Scal_Typ as defined in unit System.Scalar_Values.
12845 function Invalid_Float_Value return Node_Id;
12846 -- Return the invalid value of float type Scal_Typ
12848 function Invalid_Integer_Value return Node_Id;
12849 -- Return the invalid value of integer type Scal_Typ
12851 procedure Set_Invalid_Binder_Values;
12852 -- Set the contents of collection Invalid_Binder_Values
12854 --------------------------
12855 -- Invalid_Binder_Value --
12856 --------------------------
12858 function Invalid_Binder_Value return Node_Id is
12859 Val_Id : Entity_Id;
12861 begin
12862 -- Initialize the collection of invalid binder values the first time
12863 -- around.
12865 Set_Invalid_Binder_Values;
12867 -- Obtain the corresponding variable from System.Scalar_Values which
12868 -- holds the invalid value for this type.
12870 Val_Id := Invalid_Binder_Values (Scal_Typ);
12871 pragma Assert (Present (Val_Id));
12873 return New_Occurrence_Of (Val_Id, Loc);
12874 end Invalid_Binder_Value;
12876 -------------------------
12877 -- Invalid_Float_Value --
12878 -------------------------
12880 function Invalid_Float_Value return Node_Id is
12881 Value : constant Ureal := Invalid_Floats (Scal_Typ);
12883 begin
12884 -- Pragma Invalid_Scalars did not specify an invalid value for this
12885 -- type. Fall back to the value provided by the binder.
12887 if Value = No_Ureal then
12888 return Invalid_Binder_Value;
12889 else
12890 return Make_Real_Literal (Loc, Realval => Value);
12891 end if;
12892 end Invalid_Float_Value;
12894 ---------------------------
12895 -- Invalid_Integer_Value --
12896 ---------------------------
12898 function Invalid_Integer_Value return Node_Id is
12899 Value : constant Uint := Invalid_Integers (Scal_Typ);
12901 begin
12902 -- Pragma Invalid_Scalars did not specify an invalid value for this
12903 -- type. Fall back to the value provided by the binder.
12905 if Value = No_Uint then
12906 return Invalid_Binder_Value;
12907 else
12908 return Make_Integer_Literal (Loc, Intval => Value);
12909 end if;
12910 end Invalid_Integer_Value;
12912 -------------------------------
12913 -- Set_Invalid_Binder_Values --
12914 -------------------------------
12916 procedure Set_Invalid_Binder_Values is
12917 begin
12918 if not Invalid_Binder_Values_Set then
12919 Invalid_Binder_Values_Set := True;
12921 -- Initialize the contents of the collection once since RTE calls
12922 -- are not cheap.
12924 Invalid_Binder_Values :=
12925 (Name_Short_Float => RTE (RE_IS_Isf),
12926 Name_Float => RTE (RE_IS_Ifl),
12927 Name_Long_Float => RTE (RE_IS_Ilf),
12928 Name_Long_Long_Float => RTE (RE_IS_Ill),
12929 Name_Signed_8 => RTE (RE_IS_Is1),
12930 Name_Signed_16 => RTE (RE_IS_Is2),
12931 Name_Signed_32 => RTE (RE_IS_Is4),
12932 Name_Signed_64 => RTE (RE_IS_Is8),
12933 Name_Unsigned_8 => RTE (RE_IS_Iu1),
12934 Name_Unsigned_16 => RTE (RE_IS_Iu2),
12935 Name_Unsigned_32 => RTE (RE_IS_Iu4),
12936 Name_Unsigned_64 => RTE (RE_IS_Iu8));
12937 end if;
12938 end Set_Invalid_Binder_Values;
12940 -- Start of processing for Invalid_Scalar_Value
12942 begin
12943 if Scal_Typ in Float_Scalar_Id then
12944 return Invalid_Float_Value;
12946 else pragma Assert (Scal_Typ in Integer_Scalar_Id);
12947 return Invalid_Integer_Value;
12948 end if;
12949 end Invalid_Scalar_Value;
12951 -----------------------------
12952 -- Is_Actual_Out_Parameter --
12953 -----------------------------
12955 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
12956 Formal : Entity_Id;
12957 Call : Node_Id;
12958 begin
12959 Find_Actual (N, Formal, Call);
12960 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
12961 end Is_Actual_Out_Parameter;
12963 -------------------------
12964 -- Is_Actual_Parameter --
12965 -------------------------
12967 function Is_Actual_Parameter (N : Node_Id) return Boolean is
12968 PK : constant Node_Kind := Nkind (Parent (N));
12970 begin
12971 case PK is
12972 when N_Parameter_Association =>
12973 return N = Explicit_Actual_Parameter (Parent (N));
12975 when N_Subprogram_Call =>
12976 return Is_List_Member (N)
12977 and then
12978 List_Containing (N) = Parameter_Associations (Parent (N));
12980 when others =>
12981 return False;
12982 end case;
12983 end Is_Actual_Parameter;
12985 --------------------------------
12986 -- Is_Actual_Tagged_Parameter --
12987 --------------------------------
12989 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
12990 Formal : Entity_Id;
12991 Call : Node_Id;
12992 begin
12993 Find_Actual (N, Formal, Call);
12994 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
12995 end Is_Actual_Tagged_Parameter;
12997 ---------------------
12998 -- Is_Aliased_View --
12999 ---------------------
13001 function Is_Aliased_View (Obj : Node_Id) return Boolean is
13002 E : Entity_Id;
13004 begin
13005 if Is_Entity_Name (Obj) then
13006 E := Entity (Obj);
13008 return
13009 (Is_Object (E)
13010 and then
13011 (Is_Aliased (E)
13012 or else (Present (Renamed_Object (E))
13013 and then Is_Aliased_View (Renamed_Object (E)))))
13015 or else ((Is_Formal (E) or else Is_Formal_Object (E))
13016 and then Is_Tagged_Type (Etype (E)))
13018 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
13020 -- Current instance of type, either directly or as rewritten
13021 -- reference to the current object.
13023 or else (Is_Entity_Name (Original_Node (Obj))
13024 and then Present (Entity (Original_Node (Obj)))
13025 and then Is_Type (Entity (Original_Node (Obj))))
13027 or else (Is_Type (E) and then E = Current_Scope)
13029 or else (Is_Incomplete_Or_Private_Type (E)
13030 and then Full_View (E) = Current_Scope)
13032 -- Ada 2012 AI05-0053: the return object of an extended return
13033 -- statement is aliased if its type is immutably limited.
13035 or else (Is_Return_Object (E)
13036 and then Is_Limited_View (Etype (E)));
13038 elsif Nkind (Obj) = N_Selected_Component then
13039 return Is_Aliased (Entity (Selector_Name (Obj)));
13041 elsif Nkind (Obj) = N_Indexed_Component then
13042 return Has_Aliased_Components (Etype (Prefix (Obj)))
13043 or else
13044 (Is_Access_Type (Etype (Prefix (Obj)))
13045 and then Has_Aliased_Components
13046 (Designated_Type (Etype (Prefix (Obj)))));
13048 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
13049 return Is_Tagged_Type (Etype (Obj))
13050 and then Is_Aliased_View (Expression (Obj));
13052 elsif Nkind (Obj) = N_Explicit_Dereference then
13053 return Nkind (Original_Node (Obj)) /= N_Function_Call;
13055 else
13056 return False;
13057 end if;
13058 end Is_Aliased_View;
13060 -------------------------
13061 -- Is_Ancestor_Package --
13062 -------------------------
13064 function Is_Ancestor_Package
13065 (E1 : Entity_Id;
13066 E2 : Entity_Id) return Boolean
13068 Par : Entity_Id;
13070 begin
13071 Par := E2;
13072 while Present (Par) and then Par /= Standard_Standard loop
13073 if Par = E1 then
13074 return True;
13075 end if;
13077 Par := Scope (Par);
13078 end loop;
13080 return False;
13081 end Is_Ancestor_Package;
13083 ----------------------
13084 -- Is_Atomic_Object --
13085 ----------------------
13087 function Is_Atomic_Object (N : Node_Id) return Boolean is
13089 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
13090 -- Determines if given object has atomic components
13092 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
13093 -- If prefix is an implicit dereference, examine designated type
13095 ----------------------
13096 -- Is_Atomic_Prefix --
13097 ----------------------
13099 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
13100 begin
13101 if Is_Access_Type (Etype (N)) then
13102 return
13103 Has_Atomic_Components (Designated_Type (Etype (N)));
13104 else
13105 return Object_Has_Atomic_Components (N);
13106 end if;
13107 end Is_Atomic_Prefix;
13109 ----------------------------------
13110 -- Object_Has_Atomic_Components --
13111 ----------------------------------
13113 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
13114 begin
13115 if Has_Atomic_Components (Etype (N))
13116 or else Is_Atomic (Etype (N))
13117 then
13118 return True;
13120 elsif Is_Entity_Name (N)
13121 and then (Has_Atomic_Components (Entity (N))
13122 or else Is_Atomic (Entity (N)))
13123 then
13124 return True;
13126 elsif Nkind (N) = N_Selected_Component
13127 and then Is_Atomic (Entity (Selector_Name (N)))
13128 then
13129 return True;
13131 elsif Nkind (N) = N_Indexed_Component
13132 or else Nkind (N) = N_Selected_Component
13133 then
13134 return Is_Atomic_Prefix (Prefix (N));
13136 else
13137 return False;
13138 end if;
13139 end Object_Has_Atomic_Components;
13141 -- Start of processing for Is_Atomic_Object
13143 begin
13144 -- Predicate is not relevant to subprograms
13146 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
13147 return False;
13149 elsif Is_Atomic (Etype (N))
13150 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
13151 then
13152 return True;
13154 elsif Nkind (N) = N_Selected_Component
13155 and then Is_Atomic (Entity (Selector_Name (N)))
13156 then
13157 return True;
13159 elsif Nkind (N) = N_Indexed_Component
13160 or else Nkind (N) = N_Selected_Component
13161 then
13162 return Is_Atomic_Prefix (Prefix (N));
13164 else
13165 return False;
13166 end if;
13167 end Is_Atomic_Object;
13169 -----------------------------
13170 -- Is_Atomic_Or_VFA_Object --
13171 -----------------------------
13173 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
13174 begin
13175 return Is_Atomic_Object (N)
13176 or else (Is_Object_Reference (N)
13177 and then Is_Entity_Name (N)
13178 and then (Is_Volatile_Full_Access (Entity (N))
13179 or else
13180 Is_Volatile_Full_Access (Etype (Entity (N)))));
13181 end Is_Atomic_Or_VFA_Object;
13183 -------------------------
13184 -- Is_Attribute_Result --
13185 -------------------------
13187 function Is_Attribute_Result (N : Node_Id) return Boolean is
13188 begin
13189 return Nkind (N) = N_Attribute_Reference
13190 and then Attribute_Name (N) = Name_Result;
13191 end Is_Attribute_Result;
13193 -------------------------
13194 -- Is_Attribute_Update --
13195 -------------------------
13197 function Is_Attribute_Update (N : Node_Id) return Boolean is
13198 begin
13199 return Nkind (N) = N_Attribute_Reference
13200 and then Attribute_Name (N) = Name_Update;
13201 end Is_Attribute_Update;
13203 ------------------------------------
13204 -- Is_Body_Or_Package_Declaration --
13205 ------------------------------------
13207 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
13208 begin
13209 return Nkind_In (N, N_Entry_Body,
13210 N_Package_Body,
13211 N_Package_Declaration,
13212 N_Protected_Body,
13213 N_Subprogram_Body,
13214 N_Task_Body);
13215 end Is_Body_Or_Package_Declaration;
13217 -----------------------
13218 -- Is_Bounded_String --
13219 -----------------------
13221 function Is_Bounded_String (T : Entity_Id) return Boolean is
13222 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
13224 begin
13225 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
13226 -- Super_String, or one of the [Wide_]Wide_ versions. This will
13227 -- be True for all the Bounded_String types in instances of the
13228 -- Generic_Bounded_Length generics, and for types derived from those.
13230 return Present (Under)
13231 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
13232 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
13233 Is_RTE (Root_Type (Under), RO_WW_Super_String));
13234 end Is_Bounded_String;
13236 ---------------------
13237 -- Is_CCT_Instance --
13238 ---------------------
13240 function Is_CCT_Instance
13241 (Ref_Id : Entity_Id;
13242 Context_Id : Entity_Id) return Boolean
13244 begin
13245 pragma Assert (Ekind_In (Ref_Id, E_Protected_Type, E_Task_Type));
13247 if Is_Single_Task_Object (Context_Id) then
13248 return Scope_Within_Or_Same (Etype (Context_Id), Ref_Id);
13250 else
13251 pragma Assert (Ekind_In (Context_Id, E_Entry,
13252 E_Entry_Family,
13253 E_Function,
13254 E_Package,
13255 E_Procedure,
13256 E_Protected_Type,
13257 E_Task_Type)
13258 or else
13259 Is_Record_Type (Context_Id));
13260 return Scope_Within_Or_Same (Context_Id, Ref_Id);
13261 end if;
13262 end Is_CCT_Instance;
13264 -------------------------
13265 -- Is_Child_Or_Sibling --
13266 -------------------------
13268 function Is_Child_Or_Sibling
13269 (Pack_1 : Entity_Id;
13270 Pack_2 : Entity_Id) return Boolean
13272 function Distance_From_Standard (Pack : Entity_Id) return Nat;
13273 -- Given an arbitrary package, return the number of "climbs" necessary
13274 -- to reach scope Standard_Standard.
13276 procedure Equalize_Depths
13277 (Pack : in out Entity_Id;
13278 Depth : in out Nat;
13279 Depth_To_Reach : Nat);
13280 -- Given an arbitrary package, its depth and a target depth to reach,
13281 -- climb the scope chain until the said depth is reached. The pointer
13282 -- to the package and its depth a modified during the climb.
13284 ----------------------------
13285 -- Distance_From_Standard --
13286 ----------------------------
13288 function Distance_From_Standard (Pack : Entity_Id) return Nat is
13289 Dist : Nat;
13290 Scop : Entity_Id;
13292 begin
13293 Dist := 0;
13294 Scop := Pack;
13295 while Present (Scop) and then Scop /= Standard_Standard loop
13296 Dist := Dist + 1;
13297 Scop := Scope (Scop);
13298 end loop;
13300 return Dist;
13301 end Distance_From_Standard;
13303 ---------------------
13304 -- Equalize_Depths --
13305 ---------------------
13307 procedure Equalize_Depths
13308 (Pack : in out Entity_Id;
13309 Depth : in out Nat;
13310 Depth_To_Reach : Nat)
13312 begin
13313 -- The package must be at a greater or equal depth
13315 if Depth < Depth_To_Reach then
13316 raise Program_Error;
13317 end if;
13319 -- Climb the scope chain until the desired depth is reached
13321 while Present (Pack) and then Depth /= Depth_To_Reach loop
13322 Pack := Scope (Pack);
13323 Depth := Depth - 1;
13324 end loop;
13325 end Equalize_Depths;
13327 -- Local variables
13329 P_1 : Entity_Id := Pack_1;
13330 P_1_Child : Boolean := False;
13331 P_1_Depth : Nat := Distance_From_Standard (P_1);
13332 P_2 : Entity_Id := Pack_2;
13333 P_2_Child : Boolean := False;
13334 P_2_Depth : Nat := Distance_From_Standard (P_2);
13336 -- Start of processing for Is_Child_Or_Sibling
13338 begin
13339 pragma Assert
13340 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
13342 -- Both packages denote the same entity, therefore they cannot be
13343 -- children or siblings.
13345 if P_1 = P_2 then
13346 return False;
13348 -- One of the packages is at a deeper level than the other. Note that
13349 -- both may still come from different hierarchies.
13351 -- (root) P_2
13352 -- / \ :
13353 -- X P_2 or X
13354 -- : :
13355 -- P_1 P_1
13357 elsif P_1_Depth > P_2_Depth then
13358 Equalize_Depths
13359 (Pack => P_1,
13360 Depth => P_1_Depth,
13361 Depth_To_Reach => P_2_Depth);
13362 P_1_Child := True;
13364 -- (root) P_1
13365 -- / \ :
13366 -- P_1 X or X
13367 -- : :
13368 -- P_2 P_2
13370 elsif P_2_Depth > P_1_Depth then
13371 Equalize_Depths
13372 (Pack => P_2,
13373 Depth => P_2_Depth,
13374 Depth_To_Reach => P_1_Depth);
13375 P_2_Child := True;
13376 end if;
13378 -- At this stage the package pointers have been elevated to the same
13379 -- depth. If the related entities are the same, then one package is a
13380 -- potential child of the other:
13382 -- P_1
13383 -- :
13384 -- X became P_1 P_2 or vice versa
13385 -- :
13386 -- P_2
13388 if P_1 = P_2 then
13389 if P_1_Child then
13390 return Is_Child_Unit (Pack_1);
13392 else pragma Assert (P_2_Child);
13393 return Is_Child_Unit (Pack_2);
13394 end if;
13396 -- The packages may come from the same package chain or from entirely
13397 -- different hierarcies. To determine this, climb the scope stack until
13398 -- a common root is found.
13400 -- (root) (root 1) (root 2)
13401 -- / \ | |
13402 -- P_1 P_2 P_1 P_2
13404 else
13405 while Present (P_1) and then Present (P_2) loop
13407 -- The two packages may be siblings
13409 if P_1 = P_2 then
13410 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
13411 end if;
13413 P_1 := Scope (P_1);
13414 P_2 := Scope (P_2);
13415 end loop;
13416 end if;
13418 return False;
13419 end Is_Child_Or_Sibling;
13421 -----------------------------
13422 -- Is_Concurrent_Interface --
13423 -----------------------------
13425 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
13426 begin
13427 return Is_Interface (T)
13428 and then
13429 (Is_Protected_Interface (T)
13430 or else Is_Synchronized_Interface (T)
13431 or else Is_Task_Interface (T));
13432 end Is_Concurrent_Interface;
13434 -----------------------
13435 -- Is_Constant_Bound --
13436 -----------------------
13438 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
13439 begin
13440 if Compile_Time_Known_Value (Exp) then
13441 return True;
13443 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
13444 return Is_Constant_Object (Entity (Exp))
13445 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
13447 elsif Nkind (Exp) in N_Binary_Op then
13448 return Is_Constant_Bound (Left_Opnd (Exp))
13449 and then Is_Constant_Bound (Right_Opnd (Exp))
13450 and then Scope (Entity (Exp)) = Standard_Standard;
13452 else
13453 return False;
13454 end if;
13455 end Is_Constant_Bound;
13457 ---------------------------
13458 -- Is_Container_Element --
13459 ---------------------------
13461 function Is_Container_Element (Exp : Node_Id) return Boolean is
13462 Loc : constant Source_Ptr := Sloc (Exp);
13463 Pref : constant Node_Id := Prefix (Exp);
13465 Call : Node_Id;
13466 -- Call to an indexing aspect
13468 Cont_Typ : Entity_Id;
13469 -- The type of the container being accessed
13471 Elem_Typ : Entity_Id;
13472 -- Its element type
13474 Indexing : Entity_Id;
13475 Is_Const : Boolean;
13476 -- Indicates that constant indexing is used, and the element is thus
13477 -- a constant.
13479 Ref_Typ : Entity_Id;
13480 -- The reference type returned by the indexing operation
13482 begin
13483 -- If C is a container, in a context that imposes the element type of
13484 -- that container, the indexing notation C (X) is rewritten as:
13486 -- Indexing (C, X).Discr.all
13488 -- where Indexing is one of the indexing aspects of the container.
13489 -- If the context does not require a reference, the construct can be
13490 -- rewritten as
13492 -- Element (C, X)
13494 -- First, verify that the construct has the proper form
13496 if not Expander_Active then
13497 return False;
13499 elsif Nkind (Pref) /= N_Selected_Component then
13500 return False;
13502 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
13503 return False;
13505 else
13506 Call := Prefix (Pref);
13507 Ref_Typ := Etype (Call);
13508 end if;
13510 if not Has_Implicit_Dereference (Ref_Typ)
13511 or else No (First (Parameter_Associations (Call)))
13512 or else not Is_Entity_Name (Name (Call))
13513 then
13514 return False;
13515 end if;
13517 -- Retrieve type of container object, and its iterator aspects
13519 Cont_Typ := Etype (First (Parameter_Associations (Call)));
13520 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
13521 Is_Const := False;
13523 if No (Indexing) then
13525 -- Container should have at least one indexing operation
13527 return False;
13529 elsif Entity (Name (Call)) /= Entity (Indexing) then
13531 -- This may be a variable indexing operation
13533 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
13535 if No (Indexing)
13536 or else Entity (Name (Call)) /= Entity (Indexing)
13537 then
13538 return False;
13539 end if;
13541 else
13542 Is_Const := True;
13543 end if;
13545 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
13547 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
13548 return False;
13549 end if;
13551 -- Check that the expression is not the target of an assignment, in
13552 -- which case the rewriting is not possible.
13554 if not Is_Const then
13555 declare
13556 Par : Node_Id;
13558 begin
13559 Par := Exp;
13560 while Present (Par)
13561 loop
13562 if Nkind (Parent (Par)) = N_Assignment_Statement
13563 and then Par = Name (Parent (Par))
13564 then
13565 return False;
13567 -- A renaming produces a reference, and the transformation
13568 -- does not apply.
13570 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
13571 return False;
13573 elsif Nkind_In
13574 (Nkind (Parent (Par)), N_Function_Call,
13575 N_Procedure_Call_Statement,
13576 N_Entry_Call_Statement)
13577 then
13578 -- Check that the element is not part of an actual for an
13579 -- in-out parameter.
13581 declare
13582 F : Entity_Id;
13583 A : Node_Id;
13585 begin
13586 F := First_Formal (Entity (Name (Parent (Par))));
13587 A := First (Parameter_Associations (Parent (Par)));
13588 while Present (F) loop
13589 if A = Par and then Ekind (F) /= E_In_Parameter then
13590 return False;
13591 end if;
13593 Next_Formal (F);
13594 Next (A);
13595 end loop;
13596 end;
13598 -- E_In_Parameter in a call: element is not modified.
13600 exit;
13601 end if;
13603 Par := Parent (Par);
13604 end loop;
13605 end;
13606 end if;
13608 -- The expression has the proper form and the context requires the
13609 -- element type. Retrieve the Element function of the container and
13610 -- rewrite the construct as a call to it.
13612 declare
13613 Op : Elmt_Id;
13615 begin
13616 Op := First_Elmt (Primitive_Operations (Cont_Typ));
13617 while Present (Op) loop
13618 exit when Chars (Node (Op)) = Name_Element;
13619 Next_Elmt (Op);
13620 end loop;
13622 if No (Op) then
13623 return False;
13625 else
13626 Rewrite (Exp,
13627 Make_Function_Call (Loc,
13628 Name => New_Occurrence_Of (Node (Op), Loc),
13629 Parameter_Associations => Parameter_Associations (Call)));
13630 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
13631 return True;
13632 end if;
13633 end;
13634 end Is_Container_Element;
13636 ----------------------------
13637 -- Is_Contract_Annotation --
13638 ----------------------------
13640 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
13641 begin
13642 return Is_Package_Contract_Annotation (Item)
13643 or else
13644 Is_Subprogram_Contract_Annotation (Item);
13645 end Is_Contract_Annotation;
13647 --------------------------------------
13648 -- Is_Controlling_Limited_Procedure --
13649 --------------------------------------
13651 function Is_Controlling_Limited_Procedure
13652 (Proc_Nam : Entity_Id) return Boolean
13654 Param : Node_Id;
13655 Param_Typ : Entity_Id := Empty;
13657 begin
13658 if Ekind (Proc_Nam) = E_Procedure
13659 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
13660 then
13661 Param :=
13662 Parameter_Type
13663 (First (Parameter_Specifications (Parent (Proc_Nam))));
13665 -- The formal may be an anonymous access type
13667 if Nkind (Param) = N_Access_Definition then
13668 Param_Typ := Entity (Subtype_Mark (Param));
13669 else
13670 Param_Typ := Etype (Param);
13671 end if;
13673 -- In the case where an Itype was created for a dispatchin call, the
13674 -- procedure call has been rewritten. The actual may be an access to
13675 -- interface type in which case it is the designated type that is the
13676 -- controlling type.
13678 elsif Present (Associated_Node_For_Itype (Proc_Nam))
13679 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
13680 and then
13681 Present (Parameter_Associations
13682 (Associated_Node_For_Itype (Proc_Nam)))
13683 then
13684 Param_Typ :=
13685 Etype (First (Parameter_Associations
13686 (Associated_Node_For_Itype (Proc_Nam))));
13688 if Ekind (Param_Typ) = E_Anonymous_Access_Type then
13689 Param_Typ := Directly_Designated_Type (Param_Typ);
13690 end if;
13691 end if;
13693 if Present (Param_Typ) then
13694 return
13695 Is_Interface (Param_Typ)
13696 and then Is_Limited_Record (Param_Typ);
13697 end if;
13699 return False;
13700 end Is_Controlling_Limited_Procedure;
13702 -----------------------------
13703 -- Is_CPP_Constructor_Call --
13704 -----------------------------
13706 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
13707 begin
13708 return Nkind (N) = N_Function_Call
13709 and then Is_CPP_Class (Etype (Etype (N)))
13710 and then Is_Constructor (Entity (Name (N)))
13711 and then Is_Imported (Entity (Name (N)));
13712 end Is_CPP_Constructor_Call;
13714 -------------------------
13715 -- Is_Current_Instance --
13716 -------------------------
13718 function Is_Current_Instance (N : Node_Id) return Boolean is
13719 Typ : constant Entity_Id := Entity (N);
13720 P : Node_Id;
13722 begin
13723 -- Simplest case: entity is a concurrent type and we are currently
13724 -- inside the body. This will eventually be expanded into a call to
13725 -- Self (for tasks) or _object (for protected objects).
13727 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
13728 return True;
13730 else
13731 -- Check whether the context is a (sub)type declaration for the
13732 -- type entity.
13734 P := Parent (N);
13735 while Present (P) loop
13736 if Nkind_In (P, N_Full_Type_Declaration,
13737 N_Private_Type_Declaration,
13738 N_Subtype_Declaration)
13739 and then Comes_From_Source (P)
13740 and then Defining_Entity (P) = Typ
13741 then
13742 return True;
13744 -- A subtype name may appear in an aspect specification for a
13745 -- Predicate_Failure aspect, for which we do not construct a
13746 -- wrapper procedure. The subtype will be replaced by the
13747 -- expression being tested when the corresponding predicate
13748 -- check is expanded.
13750 elsif Nkind (P) = N_Aspect_Specification
13751 and then Nkind (Parent (P)) = N_Subtype_Declaration
13752 then
13753 return True;
13755 elsif Nkind (P) = N_Pragma
13756 and then Get_Pragma_Id (P) = Pragma_Predicate_Failure
13757 then
13758 return True;
13759 end if;
13761 P := Parent (P);
13762 end loop;
13763 end if;
13765 -- In any other context this is not a current occurrence
13767 return False;
13768 end Is_Current_Instance;
13770 --------------------
13771 -- Is_Declaration --
13772 --------------------
13774 function Is_Declaration
13775 (N : Node_Id;
13776 Body_OK : Boolean := True;
13777 Concurrent_OK : Boolean := True;
13778 Formal_OK : Boolean := True;
13779 Generic_OK : Boolean := True;
13780 Instantiation_OK : Boolean := True;
13781 Renaming_OK : Boolean := True;
13782 Stub_OK : Boolean := True;
13783 Subprogram_OK : Boolean := True;
13784 Type_OK : Boolean := True) return Boolean
13786 begin
13787 case Nkind (N) is
13789 -- Body declarations
13791 when N_Proper_Body =>
13792 return Body_OK;
13794 -- Concurrent type declarations
13796 when N_Protected_Type_Declaration
13797 | N_Single_Protected_Declaration
13798 | N_Single_Task_Declaration
13799 | N_Task_Type_Declaration
13801 return Concurrent_OK or Type_OK;
13803 -- Formal declarations
13805 when N_Formal_Abstract_Subprogram_Declaration
13806 | N_Formal_Concrete_Subprogram_Declaration
13807 | N_Formal_Object_Declaration
13808 | N_Formal_Package_Declaration
13809 | N_Formal_Type_Declaration
13811 return Formal_OK;
13813 -- Generic declarations
13815 when N_Generic_Package_Declaration
13816 | N_Generic_Subprogram_Declaration
13818 return Generic_OK;
13820 -- Generic instantiations
13822 when N_Function_Instantiation
13823 | N_Package_Instantiation
13824 | N_Procedure_Instantiation
13826 return Instantiation_OK;
13828 -- Generic renaming declarations
13830 when N_Generic_Renaming_Declaration =>
13831 return Generic_OK or Renaming_OK;
13833 -- Renaming declarations
13835 when N_Exception_Renaming_Declaration
13836 | N_Object_Renaming_Declaration
13837 | N_Package_Renaming_Declaration
13838 | N_Subprogram_Renaming_Declaration
13840 return Renaming_OK;
13842 -- Stub declarations
13844 when N_Body_Stub =>
13845 return Stub_OK;
13847 -- Subprogram declarations
13849 when N_Abstract_Subprogram_Declaration
13850 | N_Entry_Declaration
13851 | N_Expression_Function
13852 | N_Subprogram_Declaration
13854 return Subprogram_OK;
13856 -- Type declarations
13858 when N_Full_Type_Declaration
13859 | N_Incomplete_Type_Declaration
13860 | N_Private_Extension_Declaration
13861 | N_Private_Type_Declaration
13862 | N_Subtype_Declaration
13864 return Type_OK;
13866 -- Miscellaneous
13868 when N_Component_Declaration
13869 | N_Exception_Declaration
13870 | N_Implicit_Label_Declaration
13871 | N_Number_Declaration
13872 | N_Object_Declaration
13873 | N_Package_Declaration
13875 return True;
13877 when others =>
13878 return False;
13879 end case;
13880 end Is_Declaration;
13882 --------------------------------
13883 -- Is_Declared_Within_Variant --
13884 --------------------------------
13886 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
13887 Comp_Decl : constant Node_Id := Parent (Comp);
13888 Comp_List : constant Node_Id := Parent (Comp_Decl);
13889 begin
13890 return Nkind (Parent (Comp_List)) = N_Variant;
13891 end Is_Declared_Within_Variant;
13893 ----------------------------------------------
13894 -- Is_Dependent_Component_Of_Mutable_Object --
13895 ----------------------------------------------
13897 function Is_Dependent_Component_Of_Mutable_Object
13898 (Object : Node_Id) return Boolean
13900 P : Node_Id;
13901 Prefix_Type : Entity_Id;
13902 P_Aliased : Boolean := False;
13903 Comp : Entity_Id;
13905 Deref : Node_Id := Object;
13906 -- Dereference node, in something like X.all.Y(2)
13908 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
13910 begin
13911 -- Find the dereference node if any
13913 while Nkind_In (Deref, N_Indexed_Component,
13914 N_Selected_Component,
13915 N_Slice)
13916 loop
13917 Deref := Prefix (Deref);
13918 end loop;
13920 -- Ada 2005: If we have a component or slice of a dereference,
13921 -- something like X.all.Y (2), and the type of X is access-to-constant,
13922 -- Is_Variable will return False, because it is indeed a constant
13923 -- view. But it might be a view of a variable object, so we want the
13924 -- following condition to be True in that case.
13926 if Is_Variable (Object)
13927 or else (Ada_Version >= Ada_2005
13928 and then Nkind (Deref) = N_Explicit_Dereference)
13929 then
13930 if Nkind (Object) = N_Selected_Component then
13931 P := Prefix (Object);
13932 Prefix_Type := Etype (P);
13934 if Is_Entity_Name (P) then
13935 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
13936 Prefix_Type := Base_Type (Prefix_Type);
13937 end if;
13939 if Is_Aliased (Entity (P)) then
13940 P_Aliased := True;
13941 end if;
13943 -- A discriminant check on a selected component may be expanded
13944 -- into a dereference when removing side effects. Recover the
13945 -- original node and its type, which may be unconstrained.
13947 elsif Nkind (P) = N_Explicit_Dereference
13948 and then not (Comes_From_Source (P))
13949 then
13950 P := Original_Node (P);
13951 Prefix_Type := Etype (P);
13953 else
13954 -- Check for prefix being an aliased component???
13956 null;
13958 end if;
13960 -- A heap object is constrained by its initial value
13962 -- Ada 2005 (AI-363): Always assume the object could be mutable in
13963 -- the dereferenced case, since the access value might denote an
13964 -- unconstrained aliased object, whereas in Ada 95 the designated
13965 -- object is guaranteed to be constrained. A worst-case assumption
13966 -- has to apply in Ada 2005 because we can't tell at compile
13967 -- time whether the object is "constrained by its initial value",
13968 -- despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
13969 -- rules (these rules are acknowledged to need fixing). We don't
13970 -- impose this more stringent checking for earlier Ada versions or
13971 -- when Relaxed_RM_Semantics applies (the latter for CodePeer's
13972 -- benefit, though it's unclear on why using -gnat95 would not be
13973 -- sufficient???).
13975 if Ada_Version < Ada_2005 or else Relaxed_RM_Semantics then
13976 if Is_Access_Type (Prefix_Type)
13977 or else Nkind (P) = N_Explicit_Dereference
13978 then
13979 return False;
13980 end if;
13982 else pragma Assert (Ada_Version >= Ada_2005);
13983 if Is_Access_Type (Prefix_Type) then
13985 -- If the access type is pool-specific, and there is no
13986 -- constrained partial view of the designated type, then the
13987 -- designated object is known to be constrained.
13989 if Ekind (Prefix_Type) = E_Access_Type
13990 and then not Object_Type_Has_Constrained_Partial_View
13991 (Typ => Designated_Type (Prefix_Type),
13992 Scop => Current_Scope)
13993 then
13994 return False;
13996 -- Otherwise (general access type, or there is a constrained
13997 -- partial view of the designated type), we need to check
13998 -- based on the designated type.
14000 else
14001 Prefix_Type := Designated_Type (Prefix_Type);
14002 end if;
14003 end if;
14004 end if;
14006 Comp :=
14007 Original_Record_Component (Entity (Selector_Name (Object)));
14009 -- As per AI-0017, the renaming is illegal in a generic body, even
14010 -- if the subtype is indefinite.
14012 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
14014 if not Is_Constrained (Prefix_Type)
14015 and then (Is_Definite_Subtype (Prefix_Type)
14016 or else
14017 (Is_Generic_Type (Prefix_Type)
14018 and then Ekind (Current_Scope) = E_Generic_Package
14019 and then In_Package_Body (Current_Scope)))
14021 and then (Is_Declared_Within_Variant (Comp)
14022 or else Has_Discriminant_Dependent_Constraint (Comp))
14023 and then (not P_Aliased or else Ada_Version >= Ada_2005)
14024 then
14025 return True;
14027 -- If the prefix is of an access type at this point, then we want
14028 -- to return False, rather than calling this function recursively
14029 -- on the access object (which itself might be a discriminant-
14030 -- dependent component of some other object, but that isn't
14031 -- relevant to checking the object passed to us). This avoids
14032 -- issuing wrong errors when compiling with -gnatc, where there
14033 -- can be implicit dereferences that have not been expanded.
14035 elsif Is_Access_Type (Etype (Prefix (Object))) then
14036 return False;
14038 else
14039 return
14040 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
14041 end if;
14043 elsif Nkind (Object) = N_Indexed_Component
14044 or else Nkind (Object) = N_Slice
14045 then
14046 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
14048 -- A type conversion that Is_Variable is a view conversion:
14049 -- go back to the denoted object.
14051 elsif Nkind (Object) = N_Type_Conversion then
14052 return
14053 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
14054 end if;
14055 end if;
14057 return False;
14058 end Is_Dependent_Component_Of_Mutable_Object;
14060 ---------------------
14061 -- Is_Dereferenced --
14062 ---------------------
14064 function Is_Dereferenced (N : Node_Id) return Boolean is
14065 P : constant Node_Id := Parent (N);
14066 begin
14067 return Nkind_In (P, N_Selected_Component,
14068 N_Explicit_Dereference,
14069 N_Indexed_Component,
14070 N_Slice)
14071 and then Prefix (P) = N;
14072 end Is_Dereferenced;
14074 ----------------------
14075 -- Is_Descendant_Of --
14076 ----------------------
14078 function Is_Descendant_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
14079 T : Entity_Id;
14080 Etyp : Entity_Id;
14082 begin
14083 pragma Assert (Nkind (T1) in N_Entity);
14084 pragma Assert (Nkind (T2) in N_Entity);
14086 T := Base_Type (T1);
14088 -- Immediate return if the types match
14090 if T = T2 then
14091 return True;
14093 -- Comment needed here ???
14095 elsif Ekind (T) = E_Class_Wide_Type then
14096 return Etype (T) = T2;
14098 -- All other cases
14100 else
14101 loop
14102 Etyp := Etype (T);
14104 -- Done if we found the type we are looking for
14106 if Etyp = T2 then
14107 return True;
14109 -- Done if no more derivations to check
14111 elsif T = T1
14112 or else T = Etyp
14113 then
14114 return False;
14116 -- Following test catches error cases resulting from prev errors
14118 elsif No (Etyp) then
14119 return False;
14121 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
14122 return False;
14124 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
14125 return False;
14126 end if;
14128 T := Base_Type (Etyp);
14129 end loop;
14130 end if;
14131 end Is_Descendant_Of;
14133 ----------------------------------------
14134 -- Is_Descendant_Of_Suspension_Object --
14135 ----------------------------------------
14137 function Is_Descendant_Of_Suspension_Object
14138 (Typ : Entity_Id) return Boolean
14140 Cur_Typ : Entity_Id;
14141 Par_Typ : Entity_Id;
14143 begin
14144 -- Climb the type derivation chain checking each parent type against
14145 -- Suspension_Object.
14147 Cur_Typ := Base_Type (Typ);
14148 while Present (Cur_Typ) loop
14149 Par_Typ := Etype (Cur_Typ);
14151 -- The current type is a match
14153 if Is_Suspension_Object (Cur_Typ) then
14154 return True;
14156 -- Stop the traversal once the root of the derivation chain has been
14157 -- reached. In that case the current type is its own base type.
14159 elsif Cur_Typ = Par_Typ then
14160 exit;
14161 end if;
14163 Cur_Typ := Base_Type (Par_Typ);
14164 end loop;
14166 return False;
14167 end Is_Descendant_Of_Suspension_Object;
14169 ---------------------------------------------
14170 -- Is_Double_Precision_Floating_Point_Type --
14171 ---------------------------------------------
14173 function Is_Double_Precision_Floating_Point_Type
14174 (E : Entity_Id) return Boolean is
14175 begin
14176 return Is_Floating_Point_Type (E)
14177 and then Machine_Radix_Value (E) = Uint_2
14178 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
14179 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
14180 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
14181 end Is_Double_Precision_Floating_Point_Type;
14183 -----------------------------
14184 -- Is_Effectively_Volatile --
14185 -----------------------------
14187 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
14188 begin
14189 if Is_Type (Id) then
14191 -- An arbitrary type is effectively volatile when it is subject to
14192 -- pragma Atomic or Volatile.
14194 if Is_Volatile (Id) then
14195 return True;
14197 -- An array type is effectively volatile when it is subject to pragma
14198 -- Atomic_Components or Volatile_Components or its component type is
14199 -- effectively volatile.
14201 elsif Is_Array_Type (Id) then
14202 declare
14203 Anc : Entity_Id := Base_Type (Id);
14204 begin
14205 if Is_Private_Type (Anc) then
14206 Anc := Full_View (Anc);
14207 end if;
14209 -- Test for presence of ancestor, as the full view of a private
14210 -- type may be missing in case of error.
14212 return
14213 Has_Volatile_Components (Id)
14214 or else
14215 (Present (Anc)
14216 and then Is_Effectively_Volatile (Component_Type (Anc)));
14217 end;
14219 -- A protected type is always volatile
14221 elsif Is_Protected_Type (Id) then
14222 return True;
14224 -- A descendant of Ada.Synchronous_Task_Control.Suspension_Object is
14225 -- automatically volatile.
14227 elsif Is_Descendant_Of_Suspension_Object (Id) then
14228 return True;
14230 -- Otherwise the type is not effectively volatile
14232 else
14233 return False;
14234 end if;
14236 -- Otherwise Id denotes an object
14238 else
14239 return
14240 Is_Volatile (Id)
14241 or else Has_Volatile_Components (Id)
14242 or else Is_Effectively_Volatile (Etype (Id));
14243 end if;
14244 end Is_Effectively_Volatile;
14246 ------------------------------------
14247 -- Is_Effectively_Volatile_Object --
14248 ------------------------------------
14250 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
14251 begin
14252 if Is_Entity_Name (N) then
14253 return Is_Effectively_Volatile (Entity (N));
14255 elsif Nkind (N) = N_Indexed_Component then
14256 return Is_Effectively_Volatile_Object (Prefix (N));
14258 elsif Nkind (N) = N_Selected_Component then
14259 return
14260 Is_Effectively_Volatile_Object (Prefix (N))
14261 or else
14262 Is_Effectively_Volatile_Object (Selector_Name (N));
14264 else
14265 return False;
14266 end if;
14267 end Is_Effectively_Volatile_Object;
14269 -------------------
14270 -- Is_Entry_Body --
14271 -------------------
14273 function Is_Entry_Body (Id : Entity_Id) return Boolean is
14274 begin
14275 return
14276 Ekind_In (Id, E_Entry, E_Entry_Family)
14277 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Body;
14278 end Is_Entry_Body;
14280 --------------------------
14281 -- Is_Entry_Declaration --
14282 --------------------------
14284 function Is_Entry_Declaration (Id : Entity_Id) return Boolean is
14285 begin
14286 return
14287 Ekind_In (Id, E_Entry, E_Entry_Family)
14288 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Declaration;
14289 end Is_Entry_Declaration;
14291 ------------------------------------
14292 -- Is_Expanded_Priority_Attribute --
14293 ------------------------------------
14295 function Is_Expanded_Priority_Attribute (E : Entity_Id) return Boolean is
14296 begin
14297 return
14298 Nkind (E) = N_Function_Call
14299 and then not Configurable_Run_Time_Mode
14300 and then (Entity (Name (E)) = RTE (RE_Get_Ceiling)
14301 or else Entity (Name (E)) = RTE (RO_PE_Get_Ceiling));
14302 end Is_Expanded_Priority_Attribute;
14304 ----------------------------
14305 -- Is_Expression_Function --
14306 ----------------------------
14308 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
14309 begin
14310 if Ekind_In (Subp, E_Function, E_Subprogram_Body) then
14311 return
14312 Nkind (Original_Node (Unit_Declaration_Node (Subp))) =
14313 N_Expression_Function;
14314 else
14315 return False;
14316 end if;
14317 end Is_Expression_Function;
14319 ------------------------------------------
14320 -- Is_Expression_Function_Or_Completion --
14321 ------------------------------------------
14323 function Is_Expression_Function_Or_Completion
14324 (Subp : Entity_Id) return Boolean
14326 Subp_Decl : Node_Id;
14328 begin
14329 if Ekind (Subp) = E_Function then
14330 Subp_Decl := Unit_Declaration_Node (Subp);
14332 -- The function declaration is either an expression function or is
14333 -- completed by an expression function body.
14335 return
14336 Is_Expression_Function (Subp)
14337 or else (Nkind (Subp_Decl) = N_Subprogram_Declaration
14338 and then Present (Corresponding_Body (Subp_Decl))
14339 and then Is_Expression_Function
14340 (Corresponding_Body (Subp_Decl)));
14342 elsif Ekind (Subp) = E_Subprogram_Body then
14343 return Is_Expression_Function (Subp);
14345 else
14346 return False;
14347 end if;
14348 end Is_Expression_Function_Or_Completion;
14350 -----------------------
14351 -- Is_EVF_Expression --
14352 -----------------------
14354 function Is_EVF_Expression (N : Node_Id) return Boolean is
14355 Orig_N : constant Node_Id := Original_Node (N);
14356 Alt : Node_Id;
14357 Expr : Node_Id;
14358 Id : Entity_Id;
14360 begin
14361 -- Detect a reference to a formal parameter of a specific tagged type
14362 -- whose related subprogram is subject to pragma Expresions_Visible with
14363 -- value "False".
14365 if Is_Entity_Name (N) and then Present (Entity (N)) then
14366 Id := Entity (N);
14368 return
14369 Is_Formal (Id)
14370 and then Is_Specific_Tagged_Type (Etype (Id))
14371 and then Extensions_Visible_Status (Id) =
14372 Extensions_Visible_False;
14374 -- A case expression is an EVF expression when it contains at least one
14375 -- EVF dependent_expression. Note that a case expression may have been
14376 -- expanded, hence the use of Original_Node.
14378 elsif Nkind (Orig_N) = N_Case_Expression then
14379 Alt := First (Alternatives (Orig_N));
14380 while Present (Alt) loop
14381 if Is_EVF_Expression (Expression (Alt)) then
14382 return True;
14383 end if;
14385 Next (Alt);
14386 end loop;
14388 -- An if expression is an EVF expression when it contains at least one
14389 -- EVF dependent_expression. Note that an if expression may have been
14390 -- expanded, hence the use of Original_Node.
14392 elsif Nkind (Orig_N) = N_If_Expression then
14393 Expr := Next (First (Expressions (Orig_N)));
14394 while Present (Expr) loop
14395 if Is_EVF_Expression (Expr) then
14396 return True;
14397 end if;
14399 Next (Expr);
14400 end loop;
14402 -- A qualified expression or a type conversion is an EVF expression when
14403 -- its operand is an EVF expression.
14405 elsif Nkind_In (N, N_Qualified_Expression,
14406 N_Unchecked_Type_Conversion,
14407 N_Type_Conversion)
14408 then
14409 return Is_EVF_Expression (Expression (N));
14411 -- Attributes 'Loop_Entry, 'Old, and 'Update are EVF expressions when
14412 -- their prefix denotes an EVF expression.
14414 elsif Nkind (N) = N_Attribute_Reference
14415 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
14416 Name_Old,
14417 Name_Update)
14418 then
14419 return Is_EVF_Expression (Prefix (N));
14420 end if;
14422 return False;
14423 end Is_EVF_Expression;
14425 --------------
14426 -- Is_False --
14427 --------------
14429 function Is_False (U : Uint) return Boolean is
14430 begin
14431 return (U = 0);
14432 end Is_False;
14434 ---------------------------
14435 -- Is_Fixed_Model_Number --
14436 ---------------------------
14438 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
14439 S : constant Ureal := Small_Value (T);
14440 M : Urealp.Save_Mark;
14441 R : Boolean;
14443 begin
14444 M := Urealp.Mark;
14445 R := (U = UR_Trunc (U / S) * S);
14446 Urealp.Release (M);
14447 return R;
14448 end Is_Fixed_Model_Number;
14450 -------------------------------
14451 -- Is_Fully_Initialized_Type --
14452 -------------------------------
14454 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
14455 begin
14456 -- Scalar types
14458 if Is_Scalar_Type (Typ) then
14460 -- A scalar type with an aspect Default_Value is fully initialized
14462 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
14463 -- of a scalar type, but we don't take that into account here, since
14464 -- we don't want these to affect warnings.
14466 return Has_Default_Aspect (Typ);
14468 elsif Is_Access_Type (Typ) then
14469 return True;
14471 elsif Is_Array_Type (Typ) then
14472 if Is_Fully_Initialized_Type (Component_Type (Typ))
14473 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
14474 then
14475 return True;
14476 end if;
14478 -- An interesting case, if we have a constrained type one of whose
14479 -- bounds is known to be null, then there are no elements to be
14480 -- initialized, so all the elements are initialized.
14482 if Is_Constrained (Typ) then
14483 declare
14484 Indx : Node_Id;
14485 Indx_Typ : Entity_Id;
14486 Lbd, Hbd : Node_Id;
14488 begin
14489 Indx := First_Index (Typ);
14490 while Present (Indx) loop
14491 if Etype (Indx) = Any_Type then
14492 return False;
14494 -- If index is a range, use directly
14496 elsif Nkind (Indx) = N_Range then
14497 Lbd := Low_Bound (Indx);
14498 Hbd := High_Bound (Indx);
14500 else
14501 Indx_Typ := Etype (Indx);
14503 if Is_Private_Type (Indx_Typ) then
14504 Indx_Typ := Full_View (Indx_Typ);
14505 end if;
14507 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
14508 return False;
14509 else
14510 Lbd := Type_Low_Bound (Indx_Typ);
14511 Hbd := Type_High_Bound (Indx_Typ);
14512 end if;
14513 end if;
14515 if Compile_Time_Known_Value (Lbd)
14516 and then
14517 Compile_Time_Known_Value (Hbd)
14518 then
14519 if Expr_Value (Hbd) < Expr_Value (Lbd) then
14520 return True;
14521 end if;
14522 end if;
14524 Next_Index (Indx);
14525 end loop;
14526 end;
14527 end if;
14529 -- If no null indexes, then type is not fully initialized
14531 return False;
14533 -- Record types
14535 elsif Is_Record_Type (Typ) then
14536 if Has_Discriminants (Typ)
14537 and then
14538 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
14539 and then Is_Fully_Initialized_Variant (Typ)
14540 then
14541 return True;
14542 end if;
14544 -- We consider bounded string types to be fully initialized, because
14545 -- otherwise we get false alarms when the Data component is not
14546 -- default-initialized.
14548 if Is_Bounded_String (Typ) then
14549 return True;
14550 end if;
14552 -- Controlled records are considered to be fully initialized if
14553 -- there is a user defined Initialize routine. This may not be
14554 -- entirely correct, but as the spec notes, we are guessing here
14555 -- what is best from the point of view of issuing warnings.
14557 if Is_Controlled (Typ) then
14558 declare
14559 Utyp : constant Entity_Id := Underlying_Type (Typ);
14561 begin
14562 if Present (Utyp) then
14563 declare
14564 Init : constant Entity_Id :=
14565 (Find_Optional_Prim_Op
14566 (Underlying_Type (Typ), Name_Initialize));
14568 begin
14569 if Present (Init)
14570 and then Comes_From_Source (Init)
14571 and then not In_Predefined_Unit (Init)
14572 then
14573 return True;
14575 elsif Has_Null_Extension (Typ)
14576 and then
14577 Is_Fully_Initialized_Type
14578 (Etype (Base_Type (Typ)))
14579 then
14580 return True;
14581 end if;
14582 end;
14583 end if;
14584 end;
14585 end if;
14587 -- Otherwise see if all record components are initialized
14589 declare
14590 Ent : Entity_Id;
14592 begin
14593 Ent := First_Entity (Typ);
14594 while Present (Ent) loop
14595 if Ekind (Ent) = E_Component
14596 and then (No (Parent (Ent))
14597 or else No (Expression (Parent (Ent))))
14598 and then not Is_Fully_Initialized_Type (Etype (Ent))
14600 -- Special VM case for tag components, which need to be
14601 -- defined in this case, but are never initialized as VMs
14602 -- are using other dispatching mechanisms. Ignore this
14603 -- uninitialized case. Note that this applies both to the
14604 -- uTag entry and the main vtable pointer (CPP_Class case).
14606 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
14607 then
14608 return False;
14609 end if;
14611 Next_Entity (Ent);
14612 end loop;
14613 end;
14615 -- No uninitialized components, so type is fully initialized.
14616 -- Note that this catches the case of no components as well.
14618 return True;
14620 elsif Is_Concurrent_Type (Typ) then
14621 return True;
14623 elsif Is_Private_Type (Typ) then
14624 declare
14625 U : constant Entity_Id := Underlying_Type (Typ);
14627 begin
14628 if No (U) then
14629 return False;
14630 else
14631 return Is_Fully_Initialized_Type (U);
14632 end if;
14633 end;
14635 else
14636 return False;
14637 end if;
14638 end Is_Fully_Initialized_Type;
14640 ----------------------------------
14641 -- Is_Fully_Initialized_Variant --
14642 ----------------------------------
14644 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
14645 Loc : constant Source_Ptr := Sloc (Typ);
14646 Constraints : constant List_Id := New_List;
14647 Components : constant Elist_Id := New_Elmt_List;
14648 Comp_Elmt : Elmt_Id;
14649 Comp_Id : Node_Id;
14650 Comp_List : Node_Id;
14651 Discr : Entity_Id;
14652 Discr_Val : Node_Id;
14654 Report_Errors : Boolean;
14655 pragma Warnings (Off, Report_Errors);
14657 begin
14658 if Serious_Errors_Detected > 0 then
14659 return False;
14660 end if;
14662 if Is_Record_Type (Typ)
14663 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14664 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
14665 then
14666 Comp_List := Component_List (Type_Definition (Parent (Typ)));
14668 Discr := First_Discriminant (Typ);
14669 while Present (Discr) loop
14670 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
14671 Discr_Val := Expression (Parent (Discr));
14673 if Present (Discr_Val)
14674 and then Is_OK_Static_Expression (Discr_Val)
14675 then
14676 Append_To (Constraints,
14677 Make_Component_Association (Loc,
14678 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
14679 Expression => New_Copy (Discr_Val)));
14680 else
14681 return False;
14682 end if;
14683 else
14684 return False;
14685 end if;
14687 Next_Discriminant (Discr);
14688 end loop;
14690 Gather_Components
14691 (Typ => Typ,
14692 Comp_List => Comp_List,
14693 Governed_By => Constraints,
14694 Into => Components,
14695 Report_Errors => Report_Errors);
14697 -- Check that each component present is fully initialized
14699 Comp_Elmt := First_Elmt (Components);
14700 while Present (Comp_Elmt) loop
14701 Comp_Id := Node (Comp_Elmt);
14703 if Ekind (Comp_Id) = E_Component
14704 and then (No (Parent (Comp_Id))
14705 or else No (Expression (Parent (Comp_Id))))
14706 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
14707 then
14708 return False;
14709 end if;
14711 Next_Elmt (Comp_Elmt);
14712 end loop;
14714 return True;
14716 elsif Is_Private_Type (Typ) then
14717 declare
14718 U : constant Entity_Id := Underlying_Type (Typ);
14720 begin
14721 if No (U) then
14722 return False;
14723 else
14724 return Is_Fully_Initialized_Variant (U);
14725 end if;
14726 end;
14728 else
14729 return False;
14730 end if;
14731 end Is_Fully_Initialized_Variant;
14733 ------------------------------------
14734 -- Is_Generic_Declaration_Or_Body --
14735 ------------------------------------
14737 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
14738 Spec_Decl : Node_Id;
14740 begin
14741 -- Package/subprogram body
14743 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
14744 and then Present (Corresponding_Spec (Decl))
14745 then
14746 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
14748 -- Package/subprogram body stub
14750 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
14751 and then Present (Corresponding_Spec_Of_Stub (Decl))
14752 then
14753 Spec_Decl :=
14754 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
14756 -- All other cases
14758 else
14759 Spec_Decl := Decl;
14760 end if;
14762 -- Rather than inspecting the defining entity of the spec declaration,
14763 -- look at its Nkind. This takes care of the case where the analysis of
14764 -- a generic body modifies the Ekind of its spec to allow for recursive
14765 -- calls.
14767 return
14768 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
14769 N_Generic_Subprogram_Declaration);
14770 end Is_Generic_Declaration_Or_Body;
14772 ----------------------------
14773 -- Is_Inherited_Operation --
14774 ----------------------------
14776 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
14777 pragma Assert (Is_Overloadable (E));
14778 Kind : constant Node_Kind := Nkind (Parent (E));
14779 begin
14780 return Kind = N_Full_Type_Declaration
14781 or else Kind = N_Private_Extension_Declaration
14782 or else Kind = N_Subtype_Declaration
14783 or else (Ekind (E) = E_Enumeration_Literal
14784 and then Is_Derived_Type (Etype (E)));
14785 end Is_Inherited_Operation;
14787 -------------------------------------
14788 -- Is_Inherited_Operation_For_Type --
14789 -------------------------------------
14791 function Is_Inherited_Operation_For_Type
14792 (E : Entity_Id;
14793 Typ : Entity_Id) return Boolean
14795 begin
14796 -- Check that the operation has been created by the type declaration
14798 return Is_Inherited_Operation (E)
14799 and then Defining_Identifier (Parent (E)) = Typ;
14800 end Is_Inherited_Operation_For_Type;
14802 --------------------------------------
14803 -- Is_Inlinable_Expression_Function --
14804 --------------------------------------
14806 function Is_Inlinable_Expression_Function
14807 (Subp : Entity_Id) return Boolean
14809 Return_Expr : Node_Id;
14811 begin
14812 if Is_Expression_Function_Or_Completion (Subp)
14813 and then Has_Pragma_Inline_Always (Subp)
14814 and then Needs_No_Actuals (Subp)
14815 and then No (Contract (Subp))
14816 and then not Is_Dispatching_Operation (Subp)
14817 and then Needs_Finalization (Etype (Subp))
14818 and then not Is_Class_Wide_Type (Etype (Subp))
14819 and then not (Has_Invariants (Etype (Subp)))
14820 and then Present (Subprogram_Body (Subp))
14821 and then Was_Expression_Function (Subprogram_Body (Subp))
14822 then
14823 Return_Expr := Expression_Of_Expression_Function (Subp);
14825 -- The returned object must not have a qualified expression and its
14826 -- nominal subtype must be statically compatible with the result
14827 -- subtype of the expression function.
14829 return
14830 Nkind (Return_Expr) = N_Identifier
14831 and then Etype (Return_Expr) = Etype (Subp);
14832 end if;
14834 return False;
14835 end Is_Inlinable_Expression_Function;
14837 -----------------
14838 -- Is_Iterator --
14839 -----------------
14841 function Is_Iterator (Typ : Entity_Id) return Boolean is
14842 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean;
14843 -- Determine whether type Iter_Typ is a predefined forward or reversible
14844 -- iterator.
14846 ----------------------
14847 -- Denotes_Iterator --
14848 ----------------------
14850 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean is
14851 begin
14852 -- Check that the name matches, and that the ultimate ancestor is in
14853 -- a predefined unit, i.e the one that declares iterator interfaces.
14855 return
14856 Nam_In (Chars (Iter_Typ), Name_Forward_Iterator,
14857 Name_Reversible_Iterator)
14858 and then In_Predefined_Unit (Root_Type (Iter_Typ));
14859 end Denotes_Iterator;
14861 -- Local variables
14863 Iface_Elmt : Elmt_Id;
14864 Ifaces : Elist_Id;
14866 -- Start of processing for Is_Iterator
14868 begin
14869 -- The type may be a subtype of a descendant of the proper instance of
14870 -- the predefined interface type, so we must use the root type of the
14871 -- given type. The same is done for Is_Reversible_Iterator.
14873 if Is_Class_Wide_Type (Typ)
14874 and then Denotes_Iterator (Root_Type (Typ))
14875 then
14876 return True;
14878 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
14879 return False;
14881 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
14882 return True;
14884 else
14885 Collect_Interfaces (Typ, Ifaces);
14887 Iface_Elmt := First_Elmt (Ifaces);
14888 while Present (Iface_Elmt) loop
14889 if Denotes_Iterator (Node (Iface_Elmt)) then
14890 return True;
14891 end if;
14893 Next_Elmt (Iface_Elmt);
14894 end loop;
14896 return False;
14897 end if;
14898 end Is_Iterator;
14900 ----------------------------
14901 -- Is_Iterator_Over_Array --
14902 ----------------------------
14904 function Is_Iterator_Over_Array (N : Node_Id) return Boolean is
14905 Container : constant Node_Id := Name (N);
14906 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
14907 begin
14908 return Is_Array_Type (Container_Typ);
14909 end Is_Iterator_Over_Array;
14911 ------------
14912 -- Is_LHS --
14913 ------------
14915 -- We seem to have a lot of overlapping functions that do similar things
14916 -- (testing for left hand sides or lvalues???).
14918 function Is_LHS (N : Node_Id) return Is_LHS_Result is
14919 P : constant Node_Id := Parent (N);
14921 begin
14922 -- Return True if we are the left hand side of an assignment statement
14924 if Nkind (P) = N_Assignment_Statement then
14925 if Name (P) = N then
14926 return Yes;
14927 else
14928 return No;
14929 end if;
14931 -- Case of prefix of indexed or selected component or slice
14933 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
14934 and then N = Prefix (P)
14935 then
14936 -- Here we have the case where the parent P is N.Q or N(Q .. R).
14937 -- If P is an LHS, then N is also effectively an LHS, but there
14938 -- is an important exception. If N is of an access type, then
14939 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
14940 -- case this makes N.all a left hand side but not N itself.
14942 -- If we don't know the type yet, this is the case where we return
14943 -- Unknown, since the answer depends on the type which is unknown.
14945 if No (Etype (N)) then
14946 return Unknown;
14948 -- We have an Etype set, so we can check it
14950 elsif Is_Access_Type (Etype (N)) then
14951 return No;
14953 -- OK, not access type case, so just test whole expression
14955 else
14956 return Is_LHS (P);
14957 end if;
14959 -- All other cases are not left hand sides
14961 else
14962 return No;
14963 end if;
14964 end Is_LHS;
14966 -----------------------------
14967 -- Is_Library_Level_Entity --
14968 -----------------------------
14970 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
14971 begin
14972 -- The following is a small optimization, and it also properly handles
14973 -- discriminals, which in task bodies might appear in expressions before
14974 -- the corresponding procedure has been created, and which therefore do
14975 -- not have an assigned scope.
14977 if Is_Formal (E) then
14978 return False;
14979 end if;
14981 -- Normal test is simply that the enclosing dynamic scope is Standard
14983 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
14984 end Is_Library_Level_Entity;
14986 --------------------------------
14987 -- Is_Limited_Class_Wide_Type --
14988 --------------------------------
14990 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
14991 begin
14992 return
14993 Is_Class_Wide_Type (Typ)
14994 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
14995 end Is_Limited_Class_Wide_Type;
14997 ---------------------------------
14998 -- Is_Local_Variable_Reference --
14999 ---------------------------------
15001 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
15002 begin
15003 if not Is_Entity_Name (Expr) then
15004 return False;
15006 else
15007 declare
15008 Ent : constant Entity_Id := Entity (Expr);
15009 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
15010 begin
15011 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
15012 return False;
15013 else
15014 return Present (Sub) and then Sub = Current_Subprogram;
15015 end if;
15016 end;
15017 end if;
15018 end Is_Local_Variable_Reference;
15020 -----------------------
15021 -- Is_Name_Reference --
15022 -----------------------
15024 function Is_Name_Reference (N : Node_Id) return Boolean is
15025 begin
15026 if Is_Entity_Name (N) then
15027 return Present (Entity (N)) and then Is_Object (Entity (N));
15028 end if;
15030 case Nkind (N) is
15031 when N_Indexed_Component
15032 | N_Slice
15034 return
15035 Is_Name_Reference (Prefix (N))
15036 or else Is_Access_Type (Etype (Prefix (N)));
15038 -- Attributes 'Input, 'Old and 'Result produce objects
15040 when N_Attribute_Reference =>
15041 return
15042 Nam_In (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
15044 when N_Selected_Component =>
15045 return
15046 Is_Name_Reference (Selector_Name (N))
15047 and then
15048 (Is_Name_Reference (Prefix (N))
15049 or else Is_Access_Type (Etype (Prefix (N))));
15051 when N_Explicit_Dereference =>
15052 return True;
15054 -- A view conversion of a tagged name is a name reference
15056 when N_Type_Conversion =>
15057 return
15058 Is_Tagged_Type (Etype (Subtype_Mark (N)))
15059 and then Is_Tagged_Type (Etype (Expression (N)))
15060 and then Is_Name_Reference (Expression (N));
15062 -- An unchecked type conversion is considered to be a name if the
15063 -- operand is a name (this construction arises only as a result of
15064 -- expansion activities).
15066 when N_Unchecked_Type_Conversion =>
15067 return Is_Name_Reference (Expression (N));
15069 when others =>
15070 return False;
15071 end case;
15072 end Is_Name_Reference;
15074 ------------------------------------
15075 -- Is_Non_Preelaborable_Construct --
15076 ------------------------------------
15078 function Is_Non_Preelaborable_Construct (N : Node_Id) return Boolean is
15080 -- NOTE: the routines within Is_Non_Preelaborable_Construct are
15081 -- intentionally unnested to avoid deep indentation of code.
15083 Non_Preelaborable : exception;
15084 -- This exception is raised when the construct violates preelaborability
15085 -- to terminate the recursion.
15087 procedure Visit (Nod : Node_Id);
15088 -- Semantically inspect construct Nod to determine whether it violates
15089 -- preelaborability. This routine raises Non_Preelaborable.
15091 procedure Visit_List (List : List_Id);
15092 pragma Inline (Visit_List);
15093 -- Invoke Visit on each element of list List. This routine raises
15094 -- Non_Preelaborable.
15096 procedure Visit_Pragma (Prag : Node_Id);
15097 pragma Inline (Visit_Pragma);
15098 -- Semantically inspect pragma Prag to determine whether it violates
15099 -- preelaborability. This routine raises Non_Preelaborable.
15101 procedure Visit_Subexpression (Expr : Node_Id);
15102 pragma Inline (Visit_Subexpression);
15103 -- Semantically inspect expression Expr to determine whether it violates
15104 -- preelaborability. This routine raises Non_Preelaborable.
15106 -----------
15107 -- Visit --
15108 -----------
15110 procedure Visit (Nod : Node_Id) is
15111 begin
15112 case Nkind (Nod) is
15114 -- Declarations
15116 when N_Component_Declaration =>
15118 -- Defining_Identifier is left out because it is not relevant
15119 -- for preelaborability.
15121 Visit (Component_Definition (Nod));
15122 Visit (Expression (Nod));
15124 when N_Derived_Type_Definition =>
15126 -- Interface_List is left out because it is not relevant for
15127 -- preelaborability.
15129 Visit (Record_Extension_Part (Nod));
15130 Visit (Subtype_Indication (Nod));
15132 when N_Entry_Declaration =>
15134 -- A protected type with at leat one entry is not preelaborable
15135 -- while task types are never preelaborable. This renders entry
15136 -- declarations non-preelaborable.
15138 raise Non_Preelaborable;
15140 when N_Full_Type_Declaration =>
15142 -- Defining_Identifier and Discriminant_Specifications are left
15143 -- out because they are not relevant for preelaborability.
15145 Visit (Type_Definition (Nod));
15147 when N_Function_Instantiation
15148 | N_Package_Instantiation
15149 | N_Procedure_Instantiation
15151 -- Defining_Unit_Name and Name are left out because they are
15152 -- not relevant for preelaborability.
15154 Visit_List (Generic_Associations (Nod));
15156 when N_Object_Declaration =>
15158 -- Defining_Identifier is left out because it is not relevant
15159 -- for preelaborability.
15161 Visit (Object_Definition (Nod));
15163 if Has_Init_Expression (Nod) then
15164 Visit (Expression (Nod));
15166 elsif not Has_Preelaborable_Initialization
15167 (Etype (Defining_Entity (Nod)))
15168 then
15169 raise Non_Preelaborable;
15170 end if;
15172 when N_Private_Extension_Declaration
15173 | N_Subtype_Declaration
15175 -- Defining_Identifier, Discriminant_Specifications, and
15176 -- Interface_List are left out because they are not relevant
15177 -- for preelaborability.
15179 Visit (Subtype_Indication (Nod));
15181 when N_Protected_Type_Declaration
15182 | N_Single_Protected_Declaration
15184 -- Defining_Identifier, Discriminant_Specifications, and
15185 -- Interface_List are left out because they are not relevant
15186 -- for preelaborability.
15188 Visit (Protected_Definition (Nod));
15190 -- A [single] task type is never preelaborable
15192 when N_Single_Task_Declaration
15193 | N_Task_Type_Declaration
15195 raise Non_Preelaborable;
15197 -- Pragmas
15199 when N_Pragma =>
15200 Visit_Pragma (Nod);
15202 -- Statements
15204 when N_Statement_Other_Than_Procedure_Call =>
15205 if Nkind (Nod) /= N_Null_Statement then
15206 raise Non_Preelaborable;
15207 end if;
15209 -- Subexpressions
15211 when N_Subexpr =>
15212 Visit_Subexpression (Nod);
15214 -- Special
15216 when N_Access_To_Object_Definition =>
15217 Visit (Subtype_Indication (Nod));
15219 when N_Case_Expression_Alternative =>
15220 Visit (Expression (Nod));
15221 Visit_List (Discrete_Choices (Nod));
15223 when N_Component_Definition =>
15224 Visit (Access_Definition (Nod));
15225 Visit (Subtype_Indication (Nod));
15227 when N_Component_List =>
15228 Visit_List (Component_Items (Nod));
15229 Visit (Variant_Part (Nod));
15231 when N_Constrained_Array_Definition =>
15232 Visit_List (Discrete_Subtype_Definitions (Nod));
15233 Visit (Component_Definition (Nod));
15235 when N_Delta_Constraint
15236 | N_Digits_Constraint
15238 -- Delta_Expression and Digits_Expression are left out because
15239 -- they are not relevant for preelaborability.
15241 Visit (Range_Constraint (Nod));
15243 when N_Discriminant_Specification =>
15245 -- Defining_Identifier and Expression are left out because they
15246 -- are not relevant for preelaborability.
15248 Visit (Discriminant_Type (Nod));
15250 when N_Generic_Association =>
15252 -- Selector_Name is left out because it is not relevant for
15253 -- preelaborability.
15255 Visit (Explicit_Generic_Actual_Parameter (Nod));
15257 when N_Index_Or_Discriminant_Constraint =>
15258 Visit_List (Constraints (Nod));
15260 when N_Iterator_Specification =>
15262 -- Defining_Identifier is left out because it is not relevant
15263 -- for preelaborability.
15265 Visit (Name (Nod));
15266 Visit (Subtype_Indication (Nod));
15268 when N_Loop_Parameter_Specification =>
15270 -- Defining_Identifier is left out because it is not relevant
15271 -- for preelaborability.
15273 Visit (Discrete_Subtype_Definition (Nod));
15275 when N_Protected_Definition =>
15277 -- End_Label is left out because it is not relevant for
15278 -- preelaborability.
15280 Visit_List (Private_Declarations (Nod));
15281 Visit_List (Visible_Declarations (Nod));
15283 when N_Range_Constraint =>
15284 Visit (Range_Expression (Nod));
15286 when N_Record_Definition
15287 | N_Variant
15289 -- End_Label, Discrete_Choices, and Interface_List are left out
15290 -- because they are not relevant for preelaborability.
15292 Visit (Component_List (Nod));
15294 when N_Subtype_Indication =>
15296 -- Subtype_Mark is left out because it is not relevant for
15297 -- preelaborability.
15299 Visit (Constraint (Nod));
15301 when N_Unconstrained_Array_Definition =>
15303 -- Subtype_Marks is left out because it is not relevant for
15304 -- preelaborability.
15306 Visit (Component_Definition (Nod));
15308 when N_Variant_Part =>
15310 -- Name is left out because it is not relevant for
15311 -- preelaborability.
15313 Visit_List (Variants (Nod));
15315 -- Default
15317 when others =>
15318 null;
15319 end case;
15320 end Visit;
15322 ----------------
15323 -- Visit_List --
15324 ----------------
15326 procedure Visit_List (List : List_Id) is
15327 Nod : Node_Id;
15329 begin
15330 if Present (List) then
15331 Nod := First (List);
15332 while Present (Nod) loop
15333 Visit (Nod);
15334 Next (Nod);
15335 end loop;
15336 end if;
15337 end Visit_List;
15339 ------------------
15340 -- Visit_Pragma --
15341 ------------------
15343 procedure Visit_Pragma (Prag : Node_Id) is
15344 begin
15345 case Get_Pragma_Id (Prag) is
15346 when Pragma_Assert
15347 | Pragma_Assert_And_Cut
15348 | Pragma_Assume
15349 | Pragma_Async_Readers
15350 | Pragma_Async_Writers
15351 | Pragma_Attribute_Definition
15352 | Pragma_Check
15353 | Pragma_Constant_After_Elaboration
15354 | Pragma_CPU
15355 | Pragma_Deadline_Floor
15356 | Pragma_Dispatching_Domain
15357 | Pragma_Effective_Reads
15358 | Pragma_Effective_Writes
15359 | Pragma_Extensions_Visible
15360 | Pragma_Ghost
15361 | Pragma_Secondary_Stack_Size
15362 | Pragma_Task_Name
15363 | Pragma_Volatile_Function
15365 Visit_List (Pragma_Argument_Associations (Prag));
15367 -- Default
15369 when others =>
15370 null;
15371 end case;
15372 end Visit_Pragma;
15374 -------------------------
15375 -- Visit_Subexpression --
15376 -------------------------
15378 procedure Visit_Subexpression (Expr : Node_Id) is
15379 procedure Visit_Aggregate (Aggr : Node_Id);
15380 pragma Inline (Visit_Aggregate);
15381 -- Semantically inspect aggregate Aggr to determine whether it
15382 -- violates preelaborability.
15384 ---------------------
15385 -- Visit_Aggregate --
15386 ---------------------
15388 procedure Visit_Aggregate (Aggr : Node_Id) is
15389 begin
15390 if not Is_Preelaborable_Aggregate (Aggr) then
15391 raise Non_Preelaborable;
15392 end if;
15393 end Visit_Aggregate;
15395 -- Start of processing for Visit_Subexpression
15397 begin
15398 case Nkind (Expr) is
15399 when N_Allocator
15400 | N_Qualified_Expression
15401 | N_Type_Conversion
15402 | N_Unchecked_Expression
15403 | N_Unchecked_Type_Conversion
15405 -- Subpool_Handle_Name and Subtype_Mark are left out because
15406 -- they are not relevant for preelaborability.
15408 Visit (Expression (Expr));
15410 when N_Aggregate
15411 | N_Extension_Aggregate
15413 Visit_Aggregate (Expr);
15415 when N_Attribute_Reference
15416 | N_Explicit_Dereference
15417 | N_Reference
15419 -- Attribute_Name and Expressions are left out because they are
15420 -- not relevant for preelaborability.
15422 Visit (Prefix (Expr));
15424 when N_Case_Expression =>
15426 -- End_Span is left out because it is not relevant for
15427 -- preelaborability.
15429 Visit_List (Alternatives (Expr));
15430 Visit (Expression (Expr));
15432 when N_Delta_Aggregate =>
15433 Visit_Aggregate (Expr);
15434 Visit (Expression (Expr));
15436 when N_Expression_With_Actions =>
15437 Visit_List (Actions (Expr));
15438 Visit (Expression (Expr));
15440 when N_If_Expression =>
15441 Visit_List (Expressions (Expr));
15443 when N_Quantified_Expression =>
15444 Visit (Condition (Expr));
15445 Visit (Iterator_Specification (Expr));
15446 Visit (Loop_Parameter_Specification (Expr));
15448 when N_Range =>
15449 Visit (High_Bound (Expr));
15450 Visit (Low_Bound (Expr));
15452 when N_Slice =>
15453 Visit (Discrete_Range (Expr));
15454 Visit (Prefix (Expr));
15456 -- Default
15458 when others =>
15460 -- The evaluation of an object name is not preelaborable,
15461 -- unless the name is a static expression (checked further
15462 -- below), or statically denotes a discriminant.
15464 if Is_Entity_Name (Expr) then
15465 Object_Name : declare
15466 Id : constant Entity_Id := Entity (Expr);
15468 begin
15469 if Is_Object (Id) then
15470 if Ekind (Id) = E_Discriminant then
15471 null;
15473 elsif Ekind_In (Id, E_Constant, E_In_Parameter)
15474 and then Present (Discriminal_Link (Id))
15475 then
15476 null;
15478 else
15479 raise Non_Preelaborable;
15480 end if;
15481 end if;
15482 end Object_Name;
15484 -- A non-static expression is not preelaborable
15486 elsif not Is_OK_Static_Expression (Expr) then
15487 raise Non_Preelaborable;
15488 end if;
15489 end case;
15490 end Visit_Subexpression;
15492 -- Start of processing for Is_Non_Preelaborable_Construct
15494 begin
15495 Visit (N);
15497 -- At this point it is known that the construct is preelaborable
15499 return False;
15501 exception
15503 -- The elaboration of the construct performs an action which violates
15504 -- preelaborability.
15506 when Non_Preelaborable =>
15507 return True;
15508 end Is_Non_Preelaborable_Construct;
15510 ---------------------------------
15511 -- Is_Nontrivial_DIC_Procedure --
15512 ---------------------------------
15514 function Is_Nontrivial_DIC_Procedure (Id : Entity_Id) return Boolean is
15515 Body_Decl : Node_Id;
15516 Stmt : Node_Id;
15518 begin
15519 if Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id) then
15520 Body_Decl :=
15521 Unit_Declaration_Node
15522 (Corresponding_Body (Unit_Declaration_Node (Id)));
15524 -- The body of the Default_Initial_Condition procedure must contain
15525 -- at least one statement, otherwise the generation of the subprogram
15526 -- body failed.
15528 pragma Assert (Present (Handled_Statement_Sequence (Body_Decl)));
15530 -- To qualify as nontrivial, the first statement of the procedure
15531 -- must be a check in the form of an if statement. If the original
15532 -- Default_Initial_Condition expression was folded, then the first
15533 -- statement is not a check.
15535 Stmt := First (Statements (Handled_Statement_Sequence (Body_Decl)));
15537 return
15538 Nkind (Stmt) = N_If_Statement
15539 and then Nkind (Original_Node (Stmt)) = N_Pragma;
15540 end if;
15542 return False;
15543 end Is_Nontrivial_DIC_Procedure;
15545 -------------------------
15546 -- Is_Null_Record_Type --
15547 -------------------------
15549 function Is_Null_Record_Type (T : Entity_Id) return Boolean is
15550 Decl : constant Node_Id := Parent (T);
15551 begin
15552 return Nkind (Decl) = N_Full_Type_Declaration
15553 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
15554 and then
15555 (No (Component_List (Type_Definition (Decl)))
15556 or else Null_Present (Component_List (Type_Definition (Decl))));
15557 end Is_Null_Record_Type;
15559 ---------------------
15560 -- Is_Object_Image --
15561 ---------------------
15563 function Is_Object_Image (Prefix : Node_Id) return Boolean is
15564 begin
15565 -- When the type of the prefix is not scalar, then the prefix is not
15566 -- valid in any scenario.
15568 if not Is_Scalar_Type (Etype (Prefix)) then
15569 return False;
15570 end if;
15572 -- Here we test for the case that the prefix is not a type and assume
15573 -- if it is not then it must be a named value or an object reference.
15574 -- This is because the parser always checks that prefixes of attributes
15575 -- are named.
15577 return not (Is_Entity_Name (Prefix) and then Is_Type (Entity (Prefix)));
15578 end Is_Object_Image;
15580 -------------------------
15581 -- Is_Object_Reference --
15582 -------------------------
15584 function Is_Object_Reference (N : Node_Id) return Boolean is
15585 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
15586 -- Determine whether N is the name of an internally-generated renaming
15588 --------------------------------------
15589 -- Is_Internally_Generated_Renaming --
15590 --------------------------------------
15592 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
15593 P : Node_Id;
15595 begin
15596 P := N;
15597 while Present (P) loop
15598 if Nkind (P) = N_Object_Renaming_Declaration then
15599 return not Comes_From_Source (P);
15600 elsif Is_List_Member (P) then
15601 return False;
15602 end if;
15604 P := Parent (P);
15605 end loop;
15607 return False;
15608 end Is_Internally_Generated_Renaming;
15610 -- Start of processing for Is_Object_Reference
15612 begin
15613 if Is_Entity_Name (N) then
15614 return Present (Entity (N)) and then Is_Object (Entity (N));
15616 else
15617 case Nkind (N) is
15618 when N_Indexed_Component
15619 | N_Slice
15621 return
15622 Is_Object_Reference (Prefix (N))
15623 or else Is_Access_Type (Etype (Prefix (N)));
15625 -- In Ada 95, a function call is a constant object; a procedure
15626 -- call is not.
15628 -- Note that predefined operators are functions as well, and so
15629 -- are attributes that are (can be renamed as) functions.
15631 when N_Binary_Op
15632 | N_Function_Call
15633 | N_Unary_Op
15635 return Etype (N) /= Standard_Void_Type;
15637 -- Attributes references 'Loop_Entry, 'Old, and 'Result yield
15638 -- objects, even though they are not functions.
15640 when N_Attribute_Reference =>
15641 return
15642 Nam_In (Attribute_Name (N), Name_Loop_Entry,
15643 Name_Old,
15644 Name_Result)
15645 or else Is_Function_Attribute_Name (Attribute_Name (N));
15647 when N_Selected_Component =>
15648 return
15649 Is_Object_Reference (Selector_Name (N))
15650 and then
15651 (Is_Object_Reference (Prefix (N))
15652 or else Is_Access_Type (Etype (Prefix (N))));
15654 -- An explicit dereference denotes an object, except that a
15655 -- conditional expression gets turned into an explicit dereference
15656 -- in some cases, and conditional expressions are not object
15657 -- names.
15659 when N_Explicit_Dereference =>
15660 return not Nkind_In (Original_Node (N), N_Case_Expression,
15661 N_If_Expression);
15663 -- A view conversion of a tagged object is an object reference
15665 when N_Type_Conversion =>
15666 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
15667 and then Is_Tagged_Type (Etype (Expression (N)))
15668 and then Is_Object_Reference (Expression (N));
15670 -- An unchecked type conversion is considered to be an object if
15671 -- the operand is an object (this construction arises only as a
15672 -- result of expansion activities).
15674 when N_Unchecked_Type_Conversion =>
15675 return True;
15677 -- Allow string literals to act as objects as long as they appear
15678 -- in internally-generated renamings. The expansion of iterators
15679 -- may generate such renamings when the range involves a string
15680 -- literal.
15682 when N_String_Literal =>
15683 return Is_Internally_Generated_Renaming (Parent (N));
15685 -- AI05-0003: In Ada 2012 a qualified expression is a name.
15686 -- This allows disambiguation of function calls and the use
15687 -- of aggregates in more contexts.
15689 when N_Qualified_Expression =>
15690 if Ada_Version < Ada_2012 then
15691 return False;
15692 else
15693 return Is_Object_Reference (Expression (N))
15694 or else Nkind (Expression (N)) = N_Aggregate;
15695 end if;
15697 when others =>
15698 return False;
15699 end case;
15700 end if;
15701 end Is_Object_Reference;
15703 -----------------------------------
15704 -- Is_OK_Variable_For_Out_Formal --
15705 -----------------------------------
15707 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
15708 begin
15709 Note_Possible_Modification (AV, Sure => True);
15711 -- We must reject parenthesized variable names. Comes_From_Source is
15712 -- checked because there are currently cases where the compiler violates
15713 -- this rule (e.g. passing a task object to its controlled Initialize
15714 -- routine). This should be properly documented in sinfo???
15716 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
15717 return False;
15719 -- A variable is always allowed
15721 elsif Is_Variable (AV) then
15722 return True;
15724 -- Generalized indexing operations are rewritten as explicit
15725 -- dereferences, and it is only during resolution that we can
15726 -- check whether the context requires an access_to_variable type.
15728 elsif Nkind (AV) = N_Explicit_Dereference
15729 and then Ada_Version >= Ada_2012
15730 and then Nkind (Original_Node (AV)) = N_Indexed_Component
15731 and then Present (Etype (Original_Node (AV)))
15732 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
15733 then
15734 return not Is_Access_Constant (Etype (Prefix (AV)));
15736 -- Unchecked conversions are allowed only if they come from the
15737 -- generated code, which sometimes uses unchecked conversions for out
15738 -- parameters in cases where code generation is unaffected. We tell
15739 -- source unchecked conversions by seeing if they are rewrites of
15740 -- an original Unchecked_Conversion function call, or of an explicit
15741 -- conversion of a function call or an aggregate (as may happen in the
15742 -- expansion of a packed array aggregate).
15744 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
15745 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
15746 return False;
15748 elsif Comes_From_Source (AV)
15749 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
15750 then
15751 return False;
15753 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
15754 return Is_OK_Variable_For_Out_Formal (Expression (AV));
15756 else
15757 return True;
15758 end if;
15760 -- Normal type conversions are allowed if argument is a variable
15762 elsif Nkind (AV) = N_Type_Conversion then
15763 if Is_Variable (Expression (AV))
15764 and then Paren_Count (Expression (AV)) = 0
15765 then
15766 Note_Possible_Modification (Expression (AV), Sure => True);
15767 return True;
15769 -- We also allow a non-parenthesized expression that raises
15770 -- constraint error if it rewrites what used to be a variable
15772 elsif Raises_Constraint_Error (Expression (AV))
15773 and then Paren_Count (Expression (AV)) = 0
15774 and then Is_Variable (Original_Node (Expression (AV)))
15775 then
15776 return True;
15778 -- Type conversion of something other than a variable
15780 else
15781 return False;
15782 end if;
15784 -- If this node is rewritten, then test the original form, if that is
15785 -- OK, then we consider the rewritten node OK (for example, if the
15786 -- original node is a conversion, then Is_Variable will not be true
15787 -- but we still want to allow the conversion if it converts a variable).
15789 elsif Original_Node (AV) /= AV then
15791 -- In Ada 2012, the explicit dereference may be a rewritten call to a
15792 -- Reference function.
15794 if Ada_Version >= Ada_2012
15795 and then Nkind (Original_Node (AV)) = N_Function_Call
15796 and then
15797 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
15798 then
15800 -- Check that this is not a constant reference.
15802 return not Is_Access_Constant (Etype (Prefix (AV)));
15804 elsif Has_Implicit_Dereference (Etype (Original_Node (AV))) then
15805 return
15806 not Is_Access_Constant (Etype
15807 (Get_Reference_Discriminant (Etype (Original_Node (AV)))));
15809 else
15810 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
15811 end if;
15813 -- All other non-variables are rejected
15815 else
15816 return False;
15817 end if;
15818 end Is_OK_Variable_For_Out_Formal;
15820 ----------------------------
15821 -- Is_OK_Volatile_Context --
15822 ----------------------------
15824 function Is_OK_Volatile_Context
15825 (Context : Node_Id;
15826 Obj_Ref : Node_Id) return Boolean
15828 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
15829 -- Determine whether an arbitrary node denotes a call to a protected
15830 -- entry, function, or procedure in prefixed form where the prefix is
15831 -- Obj_Ref.
15833 function Within_Check (Nod : Node_Id) return Boolean;
15834 -- Determine whether an arbitrary node appears in a check node
15836 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
15837 -- Determine whether an arbitrary entity appears in a volatile function
15839 ---------------------------------
15840 -- Is_Protected_Operation_Call --
15841 ---------------------------------
15843 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
15844 Pref : Node_Id;
15845 Subp : Node_Id;
15847 begin
15848 -- A call to a protected operations retains its selected component
15849 -- form as opposed to other prefixed calls that are transformed in
15850 -- expanded names.
15852 if Nkind (Nod) = N_Selected_Component then
15853 Pref := Prefix (Nod);
15854 Subp := Selector_Name (Nod);
15856 return
15857 Pref = Obj_Ref
15858 and then Present (Etype (Pref))
15859 and then Is_Protected_Type (Etype (Pref))
15860 and then Is_Entity_Name (Subp)
15861 and then Present (Entity (Subp))
15862 and then Ekind_In (Entity (Subp), E_Entry,
15863 E_Entry_Family,
15864 E_Function,
15865 E_Procedure);
15866 else
15867 return False;
15868 end if;
15869 end Is_Protected_Operation_Call;
15871 ------------------
15872 -- Within_Check --
15873 ------------------
15875 function Within_Check (Nod : Node_Id) return Boolean is
15876 Par : Node_Id;
15878 begin
15879 -- Climb the parent chain looking for a check node
15881 Par := Nod;
15882 while Present (Par) loop
15883 if Nkind (Par) in N_Raise_xxx_Error then
15884 return True;
15886 -- Prevent the search from going too far
15888 elsif Is_Body_Or_Package_Declaration (Par) then
15889 exit;
15890 end if;
15892 Par := Parent (Par);
15893 end loop;
15895 return False;
15896 end Within_Check;
15898 ------------------------------
15899 -- Within_Volatile_Function --
15900 ------------------------------
15902 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
15903 Func_Id : Entity_Id;
15905 begin
15906 -- Traverse the scope stack looking for a [generic] function
15908 Func_Id := Id;
15909 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
15910 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
15911 return Is_Volatile_Function (Func_Id);
15912 end if;
15914 Func_Id := Scope (Func_Id);
15915 end loop;
15917 return False;
15918 end Within_Volatile_Function;
15920 -- Local variables
15922 Obj_Id : Entity_Id;
15924 -- Start of processing for Is_OK_Volatile_Context
15926 begin
15927 -- The volatile object appears on either side of an assignment
15929 if Nkind (Context) = N_Assignment_Statement then
15930 return True;
15932 -- The volatile object is part of the initialization expression of
15933 -- another object.
15935 elsif Nkind (Context) = N_Object_Declaration
15936 and then Present (Expression (Context))
15937 and then Expression (Context) = Obj_Ref
15938 then
15939 Obj_Id := Defining_Entity (Context);
15941 -- The volatile object acts as the initialization expression of an
15942 -- extended return statement. This is valid context as long as the
15943 -- function is volatile.
15945 if Is_Return_Object (Obj_Id) then
15946 return Within_Volatile_Function (Obj_Id);
15948 -- Otherwise this is a normal object initialization
15950 else
15951 return True;
15952 end if;
15954 -- The volatile object acts as the name of a renaming declaration
15956 elsif Nkind (Context) = N_Object_Renaming_Declaration
15957 and then Name (Context) = Obj_Ref
15958 then
15959 return True;
15961 -- The volatile object appears as an actual parameter in a call to an
15962 -- instance of Unchecked_Conversion whose result is renamed.
15964 elsif Nkind (Context) = N_Function_Call
15965 and then Is_Entity_Name (Name (Context))
15966 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
15967 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
15968 then
15969 return True;
15971 -- The volatile object is actually the prefix in a protected entry,
15972 -- function, or procedure call.
15974 elsif Is_Protected_Operation_Call (Context) then
15975 return True;
15977 -- The volatile object appears as the expression of a simple return
15978 -- statement that applies to a volatile function.
15980 elsif Nkind (Context) = N_Simple_Return_Statement
15981 and then Expression (Context) = Obj_Ref
15982 then
15983 return
15984 Within_Volatile_Function (Return_Statement_Entity (Context));
15986 -- The volatile object appears as the prefix of a name occurring in a
15987 -- non-interfering context.
15989 elsif Nkind_In (Context, N_Attribute_Reference,
15990 N_Explicit_Dereference,
15991 N_Indexed_Component,
15992 N_Selected_Component,
15993 N_Slice)
15994 and then Prefix (Context) = Obj_Ref
15995 and then Is_OK_Volatile_Context
15996 (Context => Parent (Context),
15997 Obj_Ref => Context)
15998 then
15999 return True;
16001 -- The volatile object appears as the prefix of attributes Address,
16002 -- Alignment, Component_Size, First, First_Bit, Last, Last_Bit, Length,
16003 -- Position, Size, Storage_Size.
16005 elsif Nkind (Context) = N_Attribute_Reference
16006 and then Prefix (Context) = Obj_Ref
16007 and then Nam_In (Attribute_Name (Context), Name_Address,
16008 Name_Alignment,
16009 Name_Component_Size,
16010 Name_First,
16011 Name_First_Bit,
16012 Name_Last,
16013 Name_Last_Bit,
16014 Name_Length,
16015 Name_Position,
16016 Name_Size,
16017 Name_Storage_Size)
16018 then
16019 return True;
16021 -- The volatile object appears as the expression of a type conversion
16022 -- occurring in a non-interfering context.
16024 elsif Nkind_In (Context, N_Type_Conversion,
16025 N_Unchecked_Type_Conversion)
16026 and then Expression (Context) = Obj_Ref
16027 and then Is_OK_Volatile_Context
16028 (Context => Parent (Context),
16029 Obj_Ref => Context)
16030 then
16031 return True;
16033 -- The volatile object appears as the expression in a delay statement
16035 elsif Nkind (Context) in N_Delay_Statement then
16036 return True;
16038 -- Allow references to volatile objects in various checks. This is not a
16039 -- direct SPARK 2014 requirement.
16041 elsif Within_Check (Context) then
16042 return True;
16044 -- Assume that references to effectively volatile objects that appear
16045 -- as actual parameters in a subprogram call are always legal. A full
16046 -- legality check is done when the actuals are resolved (see routine
16047 -- Resolve_Actuals).
16049 elsif Within_Subprogram_Call (Context) then
16050 return True;
16052 -- Otherwise the context is not suitable for an effectively volatile
16053 -- object.
16055 else
16056 return False;
16057 end if;
16058 end Is_OK_Volatile_Context;
16060 ------------------------------------
16061 -- Is_Package_Contract_Annotation --
16062 ------------------------------------
16064 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
16065 Nam : Name_Id;
16067 begin
16068 if Nkind (Item) = N_Aspect_Specification then
16069 Nam := Chars (Identifier (Item));
16071 else pragma Assert (Nkind (Item) = N_Pragma);
16072 Nam := Pragma_Name (Item);
16073 end if;
16075 return Nam = Name_Abstract_State
16076 or else Nam = Name_Initial_Condition
16077 or else Nam = Name_Initializes
16078 or else Nam = Name_Refined_State;
16079 end Is_Package_Contract_Annotation;
16081 -----------------------------------
16082 -- Is_Partially_Initialized_Type --
16083 -----------------------------------
16085 function Is_Partially_Initialized_Type
16086 (Typ : Entity_Id;
16087 Include_Implicit : Boolean := True) return Boolean
16089 begin
16090 if Is_Scalar_Type (Typ) then
16091 return False;
16093 elsif Is_Access_Type (Typ) then
16094 return Include_Implicit;
16096 elsif Is_Array_Type (Typ) then
16098 -- If component type is partially initialized, so is array type
16100 if Is_Partially_Initialized_Type
16101 (Component_Type (Typ), Include_Implicit)
16102 then
16103 return True;
16105 -- Otherwise we are only partially initialized if we are fully
16106 -- initialized (this is the empty array case, no point in us
16107 -- duplicating that code here).
16109 else
16110 return Is_Fully_Initialized_Type (Typ);
16111 end if;
16113 elsif Is_Record_Type (Typ) then
16115 -- A discriminated type is always partially initialized if in
16116 -- all mode
16118 if Has_Discriminants (Typ) and then Include_Implicit then
16119 return True;
16121 -- A tagged type is always partially initialized
16123 elsif Is_Tagged_Type (Typ) then
16124 return True;
16126 -- Case of non-discriminated record
16128 else
16129 declare
16130 Ent : Entity_Id;
16132 Component_Present : Boolean := False;
16133 -- Set True if at least one component is present. If no
16134 -- components are present, then record type is fully
16135 -- initialized (another odd case, like the null array).
16137 begin
16138 -- Loop through components
16140 Ent := First_Entity (Typ);
16141 while Present (Ent) loop
16142 if Ekind (Ent) = E_Component then
16143 Component_Present := True;
16145 -- If a component has an initialization expression then
16146 -- the enclosing record type is partially initialized
16148 if Present (Parent (Ent))
16149 and then Present (Expression (Parent (Ent)))
16150 then
16151 return True;
16153 -- If a component is of a type which is itself partially
16154 -- initialized, then the enclosing record type is also.
16156 elsif Is_Partially_Initialized_Type
16157 (Etype (Ent), Include_Implicit)
16158 then
16159 return True;
16160 end if;
16161 end if;
16163 Next_Entity (Ent);
16164 end loop;
16166 -- No initialized components found. If we found any components
16167 -- they were all uninitialized so the result is false.
16169 if Component_Present then
16170 return False;
16172 -- But if we found no components, then all the components are
16173 -- initialized so we consider the type to be initialized.
16175 else
16176 return True;
16177 end if;
16178 end;
16179 end if;
16181 -- Concurrent types are always fully initialized
16183 elsif Is_Concurrent_Type (Typ) then
16184 return True;
16186 -- For a private type, go to underlying type. If there is no underlying
16187 -- type then just assume this partially initialized. Not clear if this
16188 -- can happen in a non-error case, but no harm in testing for this.
16190 elsif Is_Private_Type (Typ) then
16191 declare
16192 U : constant Entity_Id := Underlying_Type (Typ);
16193 begin
16194 if No (U) then
16195 return True;
16196 else
16197 return Is_Partially_Initialized_Type (U, Include_Implicit);
16198 end if;
16199 end;
16201 -- For any other type (are there any?) assume partially initialized
16203 else
16204 return True;
16205 end if;
16206 end Is_Partially_Initialized_Type;
16208 ------------------------------------
16209 -- Is_Potentially_Persistent_Type --
16210 ------------------------------------
16212 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
16213 Comp : Entity_Id;
16214 Indx : Node_Id;
16216 begin
16217 -- For private type, test corresponding full type
16219 if Is_Private_Type (T) then
16220 return Is_Potentially_Persistent_Type (Full_View (T));
16222 -- Scalar types are potentially persistent
16224 elsif Is_Scalar_Type (T) then
16225 return True;
16227 -- Record type is potentially persistent if not tagged and the types of
16228 -- all it components are potentially persistent, and no component has
16229 -- an initialization expression.
16231 elsif Is_Record_Type (T)
16232 and then not Is_Tagged_Type (T)
16233 and then not Is_Partially_Initialized_Type (T)
16234 then
16235 Comp := First_Component (T);
16236 while Present (Comp) loop
16237 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
16238 return False;
16239 else
16240 Next_Entity (Comp);
16241 end if;
16242 end loop;
16244 return True;
16246 -- Array type is potentially persistent if its component type is
16247 -- potentially persistent and if all its constraints are static.
16249 elsif Is_Array_Type (T) then
16250 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
16251 return False;
16252 end if;
16254 Indx := First_Index (T);
16255 while Present (Indx) loop
16256 if not Is_OK_Static_Subtype (Etype (Indx)) then
16257 return False;
16258 else
16259 Next_Index (Indx);
16260 end if;
16261 end loop;
16263 return True;
16265 -- All other types are not potentially persistent
16267 else
16268 return False;
16269 end if;
16270 end Is_Potentially_Persistent_Type;
16272 --------------------------------
16273 -- Is_Potentially_Unevaluated --
16274 --------------------------------
16276 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
16277 Par : Node_Id;
16278 Expr : Node_Id;
16280 begin
16281 Expr := N;
16282 Par := N;
16284 -- A postcondition whose expression is a short-circuit is broken down
16285 -- into individual aspects for better exception reporting. The original
16286 -- short-circuit expression is rewritten as the second operand, and an
16287 -- occurrence of 'Old in that operand is potentially unevaluated.
16288 -- See sem_ch13.adb for details of this transformation. The reference
16289 -- to 'Old may appear within an expression, so we must look for the
16290 -- enclosing pragma argument in the tree that contains the reference.
16292 while Present (Par)
16293 and then Nkind (Par) /= N_Pragma_Argument_Association
16294 loop
16295 if Nkind (Original_Node (Par)) = N_And_Then then
16296 return True;
16297 end if;
16299 Par := Parent (Par);
16300 end loop;
16302 -- Other cases; 'Old appears within other expression (not the top-level
16303 -- conjunct in a postcondition) with a potentially unevaluated operand.
16305 Par := Parent (Expr);
16306 while not Nkind_In (Par, N_And_Then,
16307 N_Case_Expression,
16308 N_If_Expression,
16309 N_In,
16310 N_Not_In,
16311 N_Or_Else,
16312 N_Quantified_Expression)
16313 loop
16314 Expr := Par;
16315 Par := Parent (Par);
16317 -- If the context is not an expression, or if is the result of
16318 -- expansion of an enclosing construct (such as another attribute)
16319 -- the predicate does not apply.
16321 if Nkind (Par) = N_Case_Expression_Alternative then
16322 null;
16324 elsif Nkind (Par) not in N_Subexpr
16325 or else not Comes_From_Source (Par)
16326 then
16327 return False;
16328 end if;
16329 end loop;
16331 if Nkind (Par) = N_If_Expression then
16332 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
16334 elsif Nkind (Par) = N_Case_Expression then
16335 return Expr /= Expression (Par);
16337 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
16338 return Expr = Right_Opnd (Par);
16340 elsif Nkind_In (Par, N_In, N_Not_In) then
16342 -- If the membership includes several alternatives, only the first is
16343 -- definitely evaluated.
16345 if Present (Alternatives (Par)) then
16346 return Expr /= First (Alternatives (Par));
16348 -- If this is a range membership both bounds are evaluated
16350 else
16351 return False;
16352 end if;
16354 elsif Nkind (Par) = N_Quantified_Expression then
16355 return Expr = Condition (Par);
16357 else
16358 return False;
16359 end if;
16360 end Is_Potentially_Unevaluated;
16362 -----------------------------------------
16363 -- Is_Predefined_Dispatching_Operation --
16364 -----------------------------------------
16366 function Is_Predefined_Dispatching_Operation
16367 (E : Entity_Id) return Boolean
16369 TSS_Name : TSS_Name_Type;
16371 begin
16372 if not Is_Dispatching_Operation (E) then
16373 return False;
16374 end if;
16376 Get_Name_String (Chars (E));
16378 -- Most predefined primitives have internally generated names. Equality
16379 -- must be treated differently; the predefined operation is recognized
16380 -- as a homogeneous binary operator that returns Boolean.
16382 if Name_Len > TSS_Name_Type'Last then
16383 TSS_Name :=
16384 TSS_Name_Type
16385 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
16387 if Nam_In (Chars (E), Name_uAssign, Name_uSize)
16388 or else
16389 (Chars (E) = Name_Op_Eq
16390 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
16391 or else TSS_Name = TSS_Deep_Adjust
16392 or else TSS_Name = TSS_Deep_Finalize
16393 or else TSS_Name = TSS_Stream_Input
16394 or else TSS_Name = TSS_Stream_Output
16395 or else TSS_Name = TSS_Stream_Read
16396 or else TSS_Name = TSS_Stream_Write
16397 or else Is_Predefined_Interface_Primitive (E)
16398 then
16399 return True;
16400 end if;
16401 end if;
16403 return False;
16404 end Is_Predefined_Dispatching_Operation;
16406 ---------------------------------------
16407 -- Is_Predefined_Interface_Primitive --
16408 ---------------------------------------
16410 function Is_Predefined_Interface_Primitive (E : Entity_Id) return Boolean is
16411 begin
16412 -- In VM targets we don't restrict the functionality of this test to
16413 -- compiling in Ada 2005 mode since in VM targets any tagged type has
16414 -- these primitives.
16416 return (Ada_Version >= Ada_2005 or else not Tagged_Type_Expansion)
16417 and then Nam_In (Chars (E), Name_uDisp_Asynchronous_Select,
16418 Name_uDisp_Conditional_Select,
16419 Name_uDisp_Get_Prim_Op_Kind,
16420 Name_uDisp_Get_Task_Id,
16421 Name_uDisp_Requeue,
16422 Name_uDisp_Timed_Select);
16423 end Is_Predefined_Interface_Primitive;
16425 ---------------------------------------
16426 -- Is_Predefined_Internal_Operation --
16427 ---------------------------------------
16429 function Is_Predefined_Internal_Operation
16430 (E : Entity_Id) return Boolean
16432 TSS_Name : TSS_Name_Type;
16434 begin
16435 if not Is_Dispatching_Operation (E) then
16436 return False;
16437 end if;
16439 Get_Name_String (Chars (E));
16441 -- Most predefined primitives have internally generated names. Equality
16442 -- must be treated differently; the predefined operation is recognized
16443 -- as a homogeneous binary operator that returns Boolean.
16445 if Name_Len > TSS_Name_Type'Last then
16446 TSS_Name :=
16447 TSS_Name_Type
16448 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
16450 if Nam_In (Chars (E), Name_uSize, Name_uAssign)
16451 or else
16452 (Chars (E) = Name_Op_Eq
16453 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
16454 or else TSS_Name = TSS_Deep_Adjust
16455 or else TSS_Name = TSS_Deep_Finalize
16456 or else Is_Predefined_Interface_Primitive (E)
16457 then
16458 return True;
16459 end if;
16460 end if;
16462 return False;
16463 end Is_Predefined_Internal_Operation;
16465 --------------------------------
16466 -- Is_Preelaborable_Aggregate --
16467 --------------------------------
16469 function Is_Preelaborable_Aggregate (Aggr : Node_Id) return Boolean is
16470 Aggr_Typ : constant Entity_Id := Etype (Aggr);
16471 Array_Aggr : constant Boolean := Is_Array_Type (Aggr_Typ);
16473 Anc_Part : Node_Id;
16474 Assoc : Node_Id;
16475 Choice : Node_Id;
16476 Comp_Typ : Entity_Id := Empty; -- init to avoid warning
16477 Expr : Node_Id;
16479 begin
16480 if Array_Aggr then
16481 Comp_Typ := Component_Type (Aggr_Typ);
16482 end if;
16484 -- Inspect the ancestor part
16486 if Nkind (Aggr) = N_Extension_Aggregate then
16487 Anc_Part := Ancestor_Part (Aggr);
16489 -- The ancestor denotes a subtype mark
16491 if Is_Entity_Name (Anc_Part)
16492 and then Is_Type (Entity (Anc_Part))
16493 then
16494 if not Has_Preelaborable_Initialization (Entity (Anc_Part)) then
16495 return False;
16496 end if;
16498 -- Otherwise the ancestor denotes an expression
16500 elsif not Is_Preelaborable_Construct (Anc_Part) then
16501 return False;
16502 end if;
16503 end if;
16505 -- Inspect the positional associations
16507 Expr := First (Expressions (Aggr));
16508 while Present (Expr) loop
16509 if not Is_Preelaborable_Construct (Expr) then
16510 return False;
16511 end if;
16513 Next (Expr);
16514 end loop;
16516 -- Inspect the named associations
16518 Assoc := First (Component_Associations (Aggr));
16519 while Present (Assoc) loop
16521 -- Inspect the choices of the current named association
16523 Choice := First (Choices (Assoc));
16524 while Present (Choice) loop
16525 if Array_Aggr then
16527 -- For a choice to be preelaborable, it must denote either a
16528 -- static range or a static expression.
16530 if Nkind (Choice) = N_Others_Choice then
16531 null;
16533 elsif Nkind (Choice) = N_Range then
16534 if not Is_OK_Static_Range (Choice) then
16535 return False;
16536 end if;
16538 elsif not Is_OK_Static_Expression (Choice) then
16539 return False;
16540 end if;
16542 else
16543 Comp_Typ := Etype (Choice);
16544 end if;
16546 Next (Choice);
16547 end loop;
16549 -- The type of the choice must have preelaborable initialization if
16550 -- the association carries a <>.
16552 pragma Assert (Present (Comp_Typ));
16553 if Box_Present (Assoc) then
16554 if not Has_Preelaborable_Initialization (Comp_Typ) then
16555 return False;
16556 end if;
16558 -- The type of the expression must have preelaborable initialization
16560 elsif not Is_Preelaborable_Construct (Expression (Assoc)) then
16561 return False;
16562 end if;
16564 Next (Assoc);
16565 end loop;
16567 -- At this point the aggregate is preelaborable
16569 return True;
16570 end Is_Preelaborable_Aggregate;
16572 --------------------------------
16573 -- Is_Preelaborable_Construct --
16574 --------------------------------
16576 function Is_Preelaborable_Construct (N : Node_Id) return Boolean is
16577 begin
16578 -- Aggregates
16580 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
16581 return Is_Preelaborable_Aggregate (N);
16583 -- Attributes are allowed in general, even if their prefix is a formal
16584 -- type. It seems that certain attributes known not to be static might
16585 -- not be allowed, but there are no rules to prevent them.
16587 elsif Nkind (N) = N_Attribute_Reference then
16588 return True;
16590 -- Expressions
16592 elsif Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
16593 return True;
16595 elsif Nkind (N) = N_Qualified_Expression then
16596 return Is_Preelaborable_Construct (Expression (N));
16598 -- Names are preelaborable when they denote a discriminant of an
16599 -- enclosing type. Discriminals are also considered for this check.
16601 elsif Is_Entity_Name (N)
16602 and then Present (Entity (N))
16603 and then
16604 (Ekind (Entity (N)) = E_Discriminant
16605 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
16606 and then Present (Discriminal_Link (Entity (N)))))
16607 then
16608 return True;
16610 -- Statements
16612 elsif Nkind (N) = N_Null then
16613 return True;
16615 -- Otherwise the construct is not preelaborable
16617 else
16618 return False;
16619 end if;
16620 end Is_Preelaborable_Construct;
16622 ---------------------------------
16623 -- Is_Protected_Self_Reference --
16624 ---------------------------------
16626 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
16628 function In_Access_Definition (N : Node_Id) return Boolean;
16629 -- Returns true if N belongs to an access definition
16631 --------------------------
16632 -- In_Access_Definition --
16633 --------------------------
16635 function In_Access_Definition (N : Node_Id) return Boolean is
16636 P : Node_Id;
16638 begin
16639 P := Parent (N);
16640 while Present (P) loop
16641 if Nkind (P) = N_Access_Definition then
16642 return True;
16643 end if;
16645 P := Parent (P);
16646 end loop;
16648 return False;
16649 end In_Access_Definition;
16651 -- Start of processing for Is_Protected_Self_Reference
16653 begin
16654 -- Verify that prefix is analyzed and has the proper form. Note that
16655 -- the attributes Elab_Spec, Elab_Body, and Elab_Subp_Body, which also
16656 -- produce the address of an entity, do not analyze their prefix
16657 -- because they denote entities that are not necessarily visible.
16658 -- Neither of them can apply to a protected type.
16660 return Ada_Version >= Ada_2005
16661 and then Is_Entity_Name (N)
16662 and then Present (Entity (N))
16663 and then Is_Protected_Type (Entity (N))
16664 and then In_Open_Scopes (Entity (N))
16665 and then not In_Access_Definition (N);
16666 end Is_Protected_Self_Reference;
16668 -----------------------------
16669 -- Is_RCI_Pkg_Spec_Or_Body --
16670 -----------------------------
16672 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
16674 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
16675 -- Return True if the unit of Cunit is an RCI package declaration
16677 ---------------------------
16678 -- Is_RCI_Pkg_Decl_Cunit --
16679 ---------------------------
16681 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
16682 The_Unit : constant Node_Id := Unit (Cunit);
16684 begin
16685 if Nkind (The_Unit) /= N_Package_Declaration then
16686 return False;
16687 end if;
16689 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
16690 end Is_RCI_Pkg_Decl_Cunit;
16692 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
16694 begin
16695 return Is_RCI_Pkg_Decl_Cunit (Cunit)
16696 or else
16697 (Nkind (Unit (Cunit)) = N_Package_Body
16698 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
16699 end Is_RCI_Pkg_Spec_Or_Body;
16701 -----------------------------------------
16702 -- Is_Remote_Access_To_Class_Wide_Type --
16703 -----------------------------------------
16705 function Is_Remote_Access_To_Class_Wide_Type
16706 (E : Entity_Id) return Boolean
16708 begin
16709 -- A remote access to class-wide type is a general access to object type
16710 -- declared in the visible part of a Remote_Types or Remote_Call_
16711 -- Interface unit.
16713 return Ekind (E) = E_General_Access_Type
16714 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
16715 end Is_Remote_Access_To_Class_Wide_Type;
16717 -----------------------------------------
16718 -- Is_Remote_Access_To_Subprogram_Type --
16719 -----------------------------------------
16721 function Is_Remote_Access_To_Subprogram_Type
16722 (E : Entity_Id) return Boolean
16724 begin
16725 return (Ekind (E) = E_Access_Subprogram_Type
16726 or else (Ekind (E) = E_Record_Type
16727 and then Present (Corresponding_Remote_Type (E))))
16728 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
16729 end Is_Remote_Access_To_Subprogram_Type;
16731 --------------------
16732 -- Is_Remote_Call --
16733 --------------------
16735 function Is_Remote_Call (N : Node_Id) return Boolean is
16736 begin
16737 if Nkind (N) not in N_Subprogram_Call then
16739 -- An entry call cannot be remote
16741 return False;
16743 elsif Nkind (Name (N)) in N_Has_Entity
16744 and then Is_Remote_Call_Interface (Entity (Name (N)))
16745 then
16746 -- A subprogram declared in the spec of a RCI package is remote
16748 return True;
16750 elsif Nkind (Name (N)) = N_Explicit_Dereference
16751 and then Is_Remote_Access_To_Subprogram_Type
16752 (Etype (Prefix (Name (N))))
16753 then
16754 -- The dereference of a RAS is a remote call
16756 return True;
16758 elsif Present (Controlling_Argument (N))
16759 and then Is_Remote_Access_To_Class_Wide_Type
16760 (Etype (Controlling_Argument (N)))
16761 then
16762 -- Any primitive operation call with a controlling argument of
16763 -- a RACW type is a remote call.
16765 return True;
16766 end if;
16768 -- All other calls are local calls
16770 return False;
16771 end Is_Remote_Call;
16773 ----------------------
16774 -- Is_Renamed_Entry --
16775 ----------------------
16777 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
16778 Orig_Node : Node_Id := Empty;
16779 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
16781 function Is_Entry (Nam : Node_Id) return Boolean;
16782 -- Determine whether Nam is an entry. Traverse selectors if there are
16783 -- nested selected components.
16785 --------------
16786 -- Is_Entry --
16787 --------------
16789 function Is_Entry (Nam : Node_Id) return Boolean is
16790 begin
16791 if Nkind (Nam) = N_Selected_Component then
16792 return Is_Entry (Selector_Name (Nam));
16793 end if;
16795 return Ekind (Entity (Nam)) = E_Entry;
16796 end Is_Entry;
16798 -- Start of processing for Is_Renamed_Entry
16800 begin
16801 if Present (Alias (Proc_Nam)) then
16802 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
16803 end if;
16805 -- Look for a rewritten subprogram renaming declaration
16807 if Nkind (Subp_Decl) = N_Subprogram_Declaration
16808 and then Present (Original_Node (Subp_Decl))
16809 then
16810 Orig_Node := Original_Node (Subp_Decl);
16811 end if;
16813 -- The rewritten subprogram is actually an entry
16815 if Present (Orig_Node)
16816 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
16817 and then Is_Entry (Name (Orig_Node))
16818 then
16819 return True;
16820 end if;
16822 return False;
16823 end Is_Renamed_Entry;
16825 -----------------------------
16826 -- Is_Renaming_Declaration --
16827 -----------------------------
16829 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
16830 begin
16831 case Nkind (N) is
16832 when N_Exception_Renaming_Declaration
16833 | N_Generic_Function_Renaming_Declaration
16834 | N_Generic_Package_Renaming_Declaration
16835 | N_Generic_Procedure_Renaming_Declaration
16836 | N_Object_Renaming_Declaration
16837 | N_Package_Renaming_Declaration
16838 | N_Subprogram_Renaming_Declaration
16840 return True;
16842 when others =>
16843 return False;
16844 end case;
16845 end Is_Renaming_Declaration;
16847 ----------------------------
16848 -- Is_Reversible_Iterator --
16849 ----------------------------
16851 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
16852 Ifaces_List : Elist_Id;
16853 Iface_Elmt : Elmt_Id;
16854 Iface : Entity_Id;
16856 begin
16857 if Is_Class_Wide_Type (Typ)
16858 and then Chars (Root_Type (Typ)) = Name_Reversible_Iterator
16859 and then In_Predefined_Unit (Root_Type (Typ))
16860 then
16861 return True;
16863 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
16864 return False;
16866 else
16867 Collect_Interfaces (Typ, Ifaces_List);
16869 Iface_Elmt := First_Elmt (Ifaces_List);
16870 while Present (Iface_Elmt) loop
16871 Iface := Node (Iface_Elmt);
16872 if Chars (Iface) = Name_Reversible_Iterator
16873 and then In_Predefined_Unit (Iface)
16874 then
16875 return True;
16876 end if;
16878 Next_Elmt (Iface_Elmt);
16879 end loop;
16880 end if;
16882 return False;
16883 end Is_Reversible_Iterator;
16885 ----------------------
16886 -- Is_Selector_Name --
16887 ----------------------
16889 function Is_Selector_Name (N : Node_Id) return Boolean is
16890 begin
16891 if not Is_List_Member (N) then
16892 declare
16893 P : constant Node_Id := Parent (N);
16894 begin
16895 return Nkind_In (P, N_Expanded_Name,
16896 N_Generic_Association,
16897 N_Parameter_Association,
16898 N_Selected_Component)
16899 and then Selector_Name (P) = N;
16900 end;
16902 else
16903 declare
16904 L : constant List_Id := List_Containing (N);
16905 P : constant Node_Id := Parent (L);
16906 begin
16907 return (Nkind (P) = N_Discriminant_Association
16908 and then Selector_Names (P) = L)
16909 or else
16910 (Nkind (P) = N_Component_Association
16911 and then Choices (P) = L);
16912 end;
16913 end if;
16914 end Is_Selector_Name;
16916 ---------------------------------
16917 -- Is_Single_Concurrent_Object --
16918 ---------------------------------
16920 function Is_Single_Concurrent_Object (Id : Entity_Id) return Boolean is
16921 begin
16922 return
16923 Is_Single_Protected_Object (Id) or else Is_Single_Task_Object (Id);
16924 end Is_Single_Concurrent_Object;
16926 -------------------------------
16927 -- Is_Single_Concurrent_Type --
16928 -------------------------------
16930 function Is_Single_Concurrent_Type (Id : Entity_Id) return Boolean is
16931 begin
16932 return
16933 Ekind_In (Id, E_Protected_Type, E_Task_Type)
16934 and then Is_Single_Concurrent_Type_Declaration
16935 (Declaration_Node (Id));
16936 end Is_Single_Concurrent_Type;
16938 -------------------------------------------
16939 -- Is_Single_Concurrent_Type_Declaration --
16940 -------------------------------------------
16942 function Is_Single_Concurrent_Type_Declaration
16943 (N : Node_Id) return Boolean
16945 begin
16946 return Nkind_In (Original_Node (N), N_Single_Protected_Declaration,
16947 N_Single_Task_Declaration);
16948 end Is_Single_Concurrent_Type_Declaration;
16950 ---------------------------------------------
16951 -- Is_Single_Precision_Floating_Point_Type --
16952 ---------------------------------------------
16954 function Is_Single_Precision_Floating_Point_Type
16955 (E : Entity_Id) return Boolean is
16956 begin
16957 return Is_Floating_Point_Type (E)
16958 and then Machine_Radix_Value (E) = Uint_2
16959 and then Machine_Mantissa_Value (E) = Uint_24
16960 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
16961 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
16962 end Is_Single_Precision_Floating_Point_Type;
16964 --------------------------------
16965 -- Is_Single_Protected_Object --
16966 --------------------------------
16968 function Is_Single_Protected_Object (Id : Entity_Id) return Boolean is
16969 begin
16970 return
16971 Ekind (Id) = E_Variable
16972 and then Ekind (Etype (Id)) = E_Protected_Type
16973 and then Is_Single_Concurrent_Type (Etype (Id));
16974 end Is_Single_Protected_Object;
16976 ---------------------------
16977 -- Is_Single_Task_Object --
16978 ---------------------------
16980 function Is_Single_Task_Object (Id : Entity_Id) return Boolean is
16981 begin
16982 return
16983 Ekind (Id) = E_Variable
16984 and then Ekind (Etype (Id)) = E_Task_Type
16985 and then Is_Single_Concurrent_Type (Etype (Id));
16986 end Is_Single_Task_Object;
16988 -------------------------------------
16989 -- Is_SPARK_05_Initialization_Expr --
16990 -------------------------------------
16992 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
16993 Is_Ok : Boolean;
16994 Expr : Node_Id;
16995 Comp_Assn : Node_Id;
16996 Orig_N : constant Node_Id := Original_Node (N);
16998 begin
16999 Is_Ok := True;
17001 if not Comes_From_Source (Orig_N) then
17002 goto Done;
17003 end if;
17005 pragma Assert (Nkind (Orig_N) in N_Subexpr);
17007 case Nkind (Orig_N) is
17008 when N_Character_Literal
17009 | N_Integer_Literal
17010 | N_Real_Literal
17011 | N_String_Literal
17013 null;
17015 when N_Expanded_Name
17016 | N_Identifier
17018 if Is_Entity_Name (Orig_N)
17019 and then Present (Entity (Orig_N)) -- needed in some cases
17020 then
17021 case Ekind (Entity (Orig_N)) is
17022 when E_Constant
17023 | E_Enumeration_Literal
17024 | E_Named_Integer
17025 | E_Named_Real
17027 null;
17029 when others =>
17030 if Is_Type (Entity (Orig_N)) then
17031 null;
17032 else
17033 Is_Ok := False;
17034 end if;
17035 end case;
17036 end if;
17038 when N_Qualified_Expression
17039 | N_Type_Conversion
17041 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
17043 when N_Unary_Op =>
17044 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
17046 when N_Binary_Op
17047 | N_Membership_Test
17048 | N_Short_Circuit
17050 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
17051 and then
17052 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
17054 when N_Aggregate
17055 | N_Extension_Aggregate
17057 if Nkind (Orig_N) = N_Extension_Aggregate then
17058 Is_Ok :=
17059 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
17060 end if;
17062 Expr := First (Expressions (Orig_N));
17063 while Present (Expr) loop
17064 if not Is_SPARK_05_Initialization_Expr (Expr) then
17065 Is_Ok := False;
17066 goto Done;
17067 end if;
17069 Next (Expr);
17070 end loop;
17072 Comp_Assn := First (Component_Associations (Orig_N));
17073 while Present (Comp_Assn) loop
17074 Expr := Expression (Comp_Assn);
17076 -- Note: test for Present here needed for box assocation
17078 if Present (Expr)
17079 and then not Is_SPARK_05_Initialization_Expr (Expr)
17080 then
17081 Is_Ok := False;
17082 goto Done;
17083 end if;
17085 Next (Comp_Assn);
17086 end loop;
17088 when N_Attribute_Reference =>
17089 if Nkind (Prefix (Orig_N)) in N_Subexpr then
17090 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
17091 end if;
17093 Expr := First (Expressions (Orig_N));
17094 while Present (Expr) loop
17095 if not Is_SPARK_05_Initialization_Expr (Expr) then
17096 Is_Ok := False;
17097 goto Done;
17098 end if;
17100 Next (Expr);
17101 end loop;
17103 -- Selected components might be expanded named not yet resolved, so
17104 -- default on the safe side. (Eg on sparklex.ads)
17106 when N_Selected_Component =>
17107 null;
17109 when others =>
17110 Is_Ok := False;
17111 end case;
17113 <<Done>>
17114 return Is_Ok;
17115 end Is_SPARK_05_Initialization_Expr;
17117 ----------------------------------
17118 -- Is_SPARK_05_Object_Reference --
17119 ----------------------------------
17121 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
17122 begin
17123 if Is_Entity_Name (N) then
17124 return Present (Entity (N))
17125 and then
17126 (Ekind_In (Entity (N), E_Constant, E_Variable)
17127 or else Ekind (Entity (N)) in Formal_Kind);
17129 else
17130 case Nkind (N) is
17131 when N_Selected_Component =>
17132 return Is_SPARK_05_Object_Reference (Prefix (N));
17134 when others =>
17135 return False;
17136 end case;
17137 end if;
17138 end Is_SPARK_05_Object_Reference;
17140 -----------------------------
17141 -- Is_Specific_Tagged_Type --
17142 -----------------------------
17144 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
17145 Full_Typ : Entity_Id;
17147 begin
17148 -- Handle private types
17150 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
17151 Full_Typ := Full_View (Typ);
17152 else
17153 Full_Typ := Typ;
17154 end if;
17156 -- A specific tagged type is a non-class-wide tagged type
17158 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
17159 end Is_Specific_Tagged_Type;
17161 ------------------
17162 -- Is_Statement --
17163 ------------------
17165 function Is_Statement (N : Node_Id) return Boolean is
17166 begin
17167 return
17168 Nkind (N) in N_Statement_Other_Than_Procedure_Call
17169 or else Nkind (N) = N_Procedure_Call_Statement;
17170 end Is_Statement;
17172 ---------------------------------------
17173 -- Is_Subprogram_Contract_Annotation --
17174 ---------------------------------------
17176 function Is_Subprogram_Contract_Annotation
17177 (Item : Node_Id) return Boolean
17179 Nam : Name_Id;
17181 begin
17182 if Nkind (Item) = N_Aspect_Specification then
17183 Nam := Chars (Identifier (Item));
17185 else pragma Assert (Nkind (Item) = N_Pragma);
17186 Nam := Pragma_Name (Item);
17187 end if;
17189 return Nam = Name_Contract_Cases
17190 or else Nam = Name_Depends
17191 or else Nam = Name_Extensions_Visible
17192 or else Nam = Name_Global
17193 or else Nam = Name_Post
17194 or else Nam = Name_Post_Class
17195 or else Nam = Name_Postcondition
17196 or else Nam = Name_Pre
17197 or else Nam = Name_Pre_Class
17198 or else Nam = Name_Precondition
17199 or else Nam = Name_Refined_Depends
17200 or else Nam = Name_Refined_Global
17201 or else Nam = Name_Refined_Post
17202 or else Nam = Name_Test_Case;
17203 end Is_Subprogram_Contract_Annotation;
17205 --------------------------------------------------
17206 -- Is_Subprogram_Stub_Without_Prior_Declaration --
17207 --------------------------------------------------
17209 function Is_Subprogram_Stub_Without_Prior_Declaration
17210 (N : Node_Id) return Boolean
17212 begin
17213 -- A subprogram stub without prior declaration serves as declaration for
17214 -- the actual subprogram body. As such, it has an attached defining
17215 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
17217 return Nkind (N) = N_Subprogram_Body_Stub
17218 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
17219 end Is_Subprogram_Stub_Without_Prior_Declaration;
17221 ---------------------------
17222 -- Is_Suitable_Primitive --
17223 ---------------------------
17225 function Is_Suitable_Primitive (Subp_Id : Entity_Id) return Boolean is
17226 begin
17227 -- The Default_Initial_Condition and invariant procedures must not be
17228 -- treated as primitive operations even when they apply to a tagged
17229 -- type. These routines must not act as targets of dispatching calls
17230 -- because they already utilize class-wide-precondition semantics to
17231 -- handle inheritance and overriding.
17233 if Ekind (Subp_Id) = E_Procedure
17234 and then (Is_DIC_Procedure (Subp_Id)
17235 or else
17236 Is_Invariant_Procedure (Subp_Id))
17237 then
17238 return False;
17239 end if;
17241 return True;
17242 end Is_Suitable_Primitive;
17244 --------------------------
17245 -- Is_Suspension_Object --
17246 --------------------------
17248 function Is_Suspension_Object (Id : Entity_Id) return Boolean is
17249 begin
17250 -- This approach does an exact name match rather than to rely on
17251 -- RTSfind. Routine Is_Effectively_Volatile is used by clients of the
17252 -- front end at point where all auxiliary tables are locked and any
17253 -- modifications to them are treated as violations. Do not tamper with
17254 -- the tables, instead examine the Chars fields of all the scopes of Id.
17256 return
17257 Chars (Id) = Name_Suspension_Object
17258 and then Present (Scope (Id))
17259 and then Chars (Scope (Id)) = Name_Synchronous_Task_Control
17260 and then Present (Scope (Scope (Id)))
17261 and then Chars (Scope (Scope (Id))) = Name_Ada
17262 and then Present (Scope (Scope (Scope (Id))))
17263 and then Scope (Scope (Scope (Id))) = Standard_Standard;
17264 end Is_Suspension_Object;
17266 ----------------------------
17267 -- Is_Synchronized_Object --
17268 ----------------------------
17270 function Is_Synchronized_Object (Id : Entity_Id) return Boolean is
17271 Prag : Node_Id;
17273 begin
17274 if Is_Object (Id) then
17276 -- The object is synchronized if it is of a type that yields a
17277 -- synchronized object.
17279 if Yields_Synchronized_Object (Etype (Id)) then
17280 return True;
17282 -- The object is synchronized if it is atomic and Async_Writers is
17283 -- enabled.
17285 elsif Is_Atomic (Id) and then Async_Writers_Enabled (Id) then
17286 return True;
17288 -- A constant is a synchronized object by default
17290 elsif Ekind (Id) = E_Constant then
17291 return True;
17293 -- A variable is a synchronized object if it is subject to pragma
17294 -- Constant_After_Elaboration.
17296 elsif Ekind (Id) = E_Variable then
17297 Prag := Get_Pragma (Id, Pragma_Constant_After_Elaboration);
17299 return Present (Prag) and then Is_Enabled_Pragma (Prag);
17300 end if;
17301 end if;
17303 -- Otherwise the input is not an object or it does not qualify as a
17304 -- synchronized object.
17306 return False;
17307 end Is_Synchronized_Object;
17309 ---------------------------------
17310 -- Is_Synchronized_Tagged_Type --
17311 ---------------------------------
17313 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
17314 Kind : constant Entity_Kind := Ekind (Base_Type (E));
17316 begin
17317 -- A task or protected type derived from an interface is a tagged type.
17318 -- Such a tagged type is called a synchronized tagged type, as are
17319 -- synchronized interfaces and private extensions whose declaration
17320 -- includes the reserved word synchronized.
17322 return (Is_Tagged_Type (E)
17323 and then (Kind = E_Task_Type
17324 or else
17325 Kind = E_Protected_Type))
17326 or else
17327 (Is_Interface (E)
17328 and then Is_Synchronized_Interface (E))
17329 or else
17330 (Ekind (E) = E_Record_Type_With_Private
17331 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
17332 and then (Synchronized_Present (Parent (E))
17333 or else Is_Synchronized_Interface (Etype (E))));
17334 end Is_Synchronized_Tagged_Type;
17336 -----------------
17337 -- Is_Transfer --
17338 -----------------
17340 function Is_Transfer (N : Node_Id) return Boolean is
17341 Kind : constant Node_Kind := Nkind (N);
17343 begin
17344 if Kind = N_Simple_Return_Statement
17345 or else
17346 Kind = N_Extended_Return_Statement
17347 or else
17348 Kind = N_Goto_Statement
17349 or else
17350 Kind = N_Raise_Statement
17351 or else
17352 Kind = N_Requeue_Statement
17353 then
17354 return True;
17356 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
17357 and then No (Condition (N))
17358 then
17359 return True;
17361 elsif Kind = N_Procedure_Call_Statement
17362 and then Is_Entity_Name (Name (N))
17363 and then Present (Entity (Name (N)))
17364 and then No_Return (Entity (Name (N)))
17365 then
17366 return True;
17368 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
17369 return True;
17371 else
17372 return False;
17373 end if;
17374 end Is_Transfer;
17376 -------------
17377 -- Is_True --
17378 -------------
17380 function Is_True (U : Uint) return Boolean is
17381 begin
17382 return (U /= 0);
17383 end Is_True;
17385 --------------------------------------
17386 -- Is_Unchecked_Conversion_Instance --
17387 --------------------------------------
17389 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
17390 Par : Node_Id;
17392 begin
17393 -- Look for a function whose generic parent is the predefined intrinsic
17394 -- function Unchecked_Conversion, or for one that renames such an
17395 -- instance.
17397 if Ekind (Id) = E_Function then
17398 Par := Parent (Id);
17400 if Nkind (Par) = N_Function_Specification then
17401 Par := Generic_Parent (Par);
17403 if Present (Par) then
17404 return
17405 Chars (Par) = Name_Unchecked_Conversion
17406 and then Is_Intrinsic_Subprogram (Par)
17407 and then In_Predefined_Unit (Par);
17408 else
17409 return
17410 Present (Alias (Id))
17411 and then Is_Unchecked_Conversion_Instance (Alias (Id));
17412 end if;
17413 end if;
17414 end if;
17416 return False;
17417 end Is_Unchecked_Conversion_Instance;
17419 -------------------------------
17420 -- Is_Universal_Numeric_Type --
17421 -------------------------------
17423 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
17424 begin
17425 return T = Universal_Integer or else T = Universal_Real;
17426 end Is_Universal_Numeric_Type;
17428 ------------------------------
17429 -- Is_User_Defined_Equality --
17430 ------------------------------
17432 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean is
17433 begin
17434 return Ekind (Id) = E_Function
17435 and then Chars (Id) = Name_Op_Eq
17436 and then Comes_From_Source (Id)
17438 -- Internally generated equalities have a full type declaration
17439 -- as their parent.
17441 and then Nkind (Parent (Id)) = N_Function_Specification;
17442 end Is_User_Defined_Equality;
17444 --------------------------------------
17445 -- Is_Validation_Variable_Reference --
17446 --------------------------------------
17448 function Is_Validation_Variable_Reference (N : Node_Id) return Boolean is
17449 Var : constant Node_Id := Unqual_Conv (N);
17450 Var_Id : Entity_Id;
17452 begin
17453 Var_Id := Empty;
17455 if Is_Entity_Name (Var) then
17456 Var_Id := Entity (Var);
17457 end if;
17459 return
17460 Present (Var_Id)
17461 and then Ekind (Var_Id) = E_Variable
17462 and then Present (Validated_Object (Var_Id));
17463 end Is_Validation_Variable_Reference;
17465 ----------------------------
17466 -- Is_Variable_Size_Array --
17467 ----------------------------
17469 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
17470 Idx : Node_Id;
17472 begin
17473 pragma Assert (Is_Array_Type (E));
17475 -- Check if some index is initialized with a non-constant value
17477 Idx := First_Index (E);
17478 while Present (Idx) loop
17479 if Nkind (Idx) = N_Range then
17480 if not Is_Constant_Bound (Low_Bound (Idx))
17481 or else not Is_Constant_Bound (High_Bound (Idx))
17482 then
17483 return True;
17484 end if;
17485 end if;
17487 Idx := Next_Index (Idx);
17488 end loop;
17490 return False;
17491 end Is_Variable_Size_Array;
17493 -----------------------------
17494 -- Is_Variable_Size_Record --
17495 -----------------------------
17497 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
17498 Comp : Entity_Id;
17499 Comp_Typ : Entity_Id;
17501 begin
17502 pragma Assert (Is_Record_Type (E));
17504 Comp := First_Entity (E);
17505 while Present (Comp) loop
17506 Comp_Typ := Etype (Comp);
17508 -- Recursive call if the record type has discriminants
17510 if Is_Record_Type (Comp_Typ)
17511 and then Has_Discriminants (Comp_Typ)
17512 and then Is_Variable_Size_Record (Comp_Typ)
17513 then
17514 return True;
17516 elsif Is_Array_Type (Comp_Typ)
17517 and then Is_Variable_Size_Array (Comp_Typ)
17518 then
17519 return True;
17520 end if;
17522 Next_Entity (Comp);
17523 end loop;
17525 return False;
17526 end Is_Variable_Size_Record;
17528 -----------------
17529 -- Is_Variable --
17530 -----------------
17532 function Is_Variable
17533 (N : Node_Id;
17534 Use_Original_Node : Boolean := True) return Boolean
17536 Orig_Node : Node_Id;
17538 function In_Protected_Function (E : Entity_Id) return Boolean;
17539 -- Within a protected function, the private components of the enclosing
17540 -- protected type are constants. A function nested within a (protected)
17541 -- procedure is not itself protected. Within the body of a protected
17542 -- function the current instance of the protected type is a constant.
17544 function Is_Variable_Prefix (P : Node_Id) return Boolean;
17545 -- Prefixes can involve implicit dereferences, in which case we must
17546 -- test for the case of a reference of a constant access type, which can
17547 -- can never be a variable.
17549 ---------------------------
17550 -- In_Protected_Function --
17551 ---------------------------
17553 function In_Protected_Function (E : Entity_Id) return Boolean is
17554 Prot : Entity_Id;
17555 S : Entity_Id;
17557 begin
17558 -- E is the current instance of a type
17560 if Is_Type (E) then
17561 Prot := E;
17563 -- E is an object
17565 else
17566 Prot := Scope (E);
17567 end if;
17569 if not Is_Protected_Type (Prot) then
17570 return False;
17572 else
17573 S := Current_Scope;
17574 while Present (S) and then S /= Prot loop
17575 if Ekind (S) = E_Function and then Scope (S) = Prot then
17576 return True;
17577 end if;
17579 S := Scope (S);
17580 end loop;
17582 return False;
17583 end if;
17584 end In_Protected_Function;
17586 ------------------------
17587 -- Is_Variable_Prefix --
17588 ------------------------
17590 function Is_Variable_Prefix (P : Node_Id) return Boolean is
17591 begin
17592 if Is_Access_Type (Etype (P)) then
17593 return not Is_Access_Constant (Root_Type (Etype (P)));
17595 -- For the case of an indexed component whose prefix has a packed
17596 -- array type, the prefix has been rewritten into a type conversion.
17597 -- Determine variable-ness from the converted expression.
17599 elsif Nkind (P) = N_Type_Conversion
17600 and then not Comes_From_Source (P)
17601 and then Is_Array_Type (Etype (P))
17602 and then Is_Packed (Etype (P))
17603 then
17604 return Is_Variable (Expression (P));
17606 else
17607 return Is_Variable (P);
17608 end if;
17609 end Is_Variable_Prefix;
17611 -- Start of processing for Is_Variable
17613 begin
17614 -- Special check, allow x'Deref(expr) as a variable
17616 if Nkind (N) = N_Attribute_Reference
17617 and then Attribute_Name (N) = Name_Deref
17618 then
17619 return True;
17620 end if;
17622 -- Check if we perform the test on the original node since this may be a
17623 -- test of syntactic categories which must not be disturbed by whatever
17624 -- rewriting might have occurred. For example, an aggregate, which is
17625 -- certainly NOT a variable, could be turned into a variable by
17626 -- expansion.
17628 if Use_Original_Node then
17629 Orig_Node := Original_Node (N);
17630 else
17631 Orig_Node := N;
17632 end if;
17634 -- Definitely OK if Assignment_OK is set. Since this is something that
17635 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
17637 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
17638 return True;
17640 -- Normally we go to the original node, but there is one exception where
17641 -- we use the rewritten node, namely when it is an explicit dereference.
17642 -- The generated code may rewrite a prefix which is an access type with
17643 -- an explicit dereference. The dereference is a variable, even though
17644 -- the original node may not be (since it could be a constant of the
17645 -- access type).
17647 -- In Ada 2005 we have a further case to consider: the prefix may be a
17648 -- function call given in prefix notation. The original node appears to
17649 -- be a selected component, but we need to examine the call.
17651 elsif Nkind (N) = N_Explicit_Dereference
17652 and then Nkind (Orig_Node) /= N_Explicit_Dereference
17653 and then Present (Etype (Orig_Node))
17654 and then Is_Access_Type (Etype (Orig_Node))
17655 then
17656 -- Note that if the prefix is an explicit dereference that does not
17657 -- come from source, we must check for a rewritten function call in
17658 -- prefixed notation before other forms of rewriting, to prevent a
17659 -- compiler crash.
17661 return
17662 (Nkind (Orig_Node) = N_Function_Call
17663 and then not Is_Access_Constant (Etype (Prefix (N))))
17664 or else
17665 Is_Variable_Prefix (Original_Node (Prefix (N)));
17667 -- in Ada 2012, the dereference may have been added for a type with
17668 -- a declared implicit dereference aspect. Check that it is not an
17669 -- access to constant.
17671 elsif Nkind (N) = N_Explicit_Dereference
17672 and then Present (Etype (Orig_Node))
17673 and then Ada_Version >= Ada_2012
17674 and then Has_Implicit_Dereference (Etype (Orig_Node))
17675 then
17676 return not Is_Access_Constant (Etype (Prefix (N)));
17678 -- A function call is never a variable
17680 elsif Nkind (N) = N_Function_Call then
17681 return False;
17683 -- All remaining checks use the original node
17685 elsif Is_Entity_Name (Orig_Node)
17686 and then Present (Entity (Orig_Node))
17687 then
17688 declare
17689 E : constant Entity_Id := Entity (Orig_Node);
17690 K : constant Entity_Kind := Ekind (E);
17692 begin
17693 return (K = E_Variable
17694 and then Nkind (Parent (E)) /= N_Exception_Handler)
17695 or else (K = E_Component
17696 and then not In_Protected_Function (E))
17697 or else K = E_Out_Parameter
17698 or else K = E_In_Out_Parameter
17699 or else K = E_Generic_In_Out_Parameter
17701 -- Current instance of type. If this is a protected type, check
17702 -- we are not within the body of one of its protected functions.
17704 or else (Is_Type (E)
17705 and then In_Open_Scopes (E)
17706 and then not In_Protected_Function (E))
17708 or else (Is_Incomplete_Or_Private_Type (E)
17709 and then In_Open_Scopes (Full_View (E)));
17710 end;
17712 else
17713 case Nkind (Orig_Node) is
17714 when N_Indexed_Component
17715 | N_Slice
17717 return Is_Variable_Prefix (Prefix (Orig_Node));
17719 when N_Selected_Component =>
17720 return (Is_Variable (Selector_Name (Orig_Node))
17721 and then Is_Variable_Prefix (Prefix (Orig_Node)))
17722 or else
17723 (Nkind (N) = N_Expanded_Name
17724 and then Scope (Entity (N)) = Entity (Prefix (N)));
17726 -- For an explicit dereference, the type of the prefix cannot
17727 -- be an access to constant or an access to subprogram.
17729 when N_Explicit_Dereference =>
17730 declare
17731 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
17732 begin
17733 return Is_Access_Type (Typ)
17734 and then not Is_Access_Constant (Root_Type (Typ))
17735 and then Ekind (Typ) /= E_Access_Subprogram_Type;
17736 end;
17738 -- The type conversion is the case where we do not deal with the
17739 -- context dependent special case of an actual parameter. Thus
17740 -- the type conversion is only considered a variable for the
17741 -- purposes of this routine if the target type is tagged. However,
17742 -- a type conversion is considered to be a variable if it does not
17743 -- come from source (this deals for example with the conversions
17744 -- of expressions to their actual subtypes).
17746 when N_Type_Conversion =>
17747 return Is_Variable (Expression (Orig_Node))
17748 and then
17749 (not Comes_From_Source (Orig_Node)
17750 or else
17751 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
17752 and then
17753 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
17755 -- GNAT allows an unchecked type conversion as a variable. This
17756 -- only affects the generation of internal expanded code, since
17757 -- calls to instantiations of Unchecked_Conversion are never
17758 -- considered variables (since they are function calls).
17760 when N_Unchecked_Type_Conversion =>
17761 return Is_Variable (Expression (Orig_Node));
17763 when others =>
17764 return False;
17765 end case;
17766 end if;
17767 end Is_Variable;
17769 ---------------------------
17770 -- Is_Visibly_Controlled --
17771 ---------------------------
17773 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
17774 Root : constant Entity_Id := Root_Type (T);
17775 begin
17776 return Chars (Scope (Root)) = Name_Finalization
17777 and then Chars (Scope (Scope (Root))) = Name_Ada
17778 and then Scope (Scope (Scope (Root))) = Standard_Standard;
17779 end Is_Visibly_Controlled;
17781 --------------------------
17782 -- Is_Volatile_Function --
17783 --------------------------
17785 function Is_Volatile_Function (Func_Id : Entity_Id) return Boolean is
17786 begin
17787 pragma Assert (Ekind_In (Func_Id, E_Function, E_Generic_Function));
17789 -- A function declared within a protected type is volatile
17791 if Is_Protected_Type (Scope (Func_Id)) then
17792 return True;
17794 -- An instance of Ada.Unchecked_Conversion is a volatile function if
17795 -- either the source or the target are effectively volatile.
17797 elsif Is_Unchecked_Conversion_Instance (Func_Id)
17798 and then Has_Effectively_Volatile_Profile (Func_Id)
17799 then
17800 return True;
17802 -- Otherwise the function is treated as volatile if it is subject to
17803 -- enabled pragma Volatile_Function.
17805 else
17806 return
17807 Is_Enabled_Pragma (Get_Pragma (Func_Id, Pragma_Volatile_Function));
17808 end if;
17809 end Is_Volatile_Function;
17811 ------------------------
17812 -- Is_Volatile_Object --
17813 ------------------------
17815 function Is_Volatile_Object (N : Node_Id) return Boolean is
17816 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
17817 -- If prefix is an implicit dereference, examine designated type
17819 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
17820 -- Determines if given object has volatile components
17822 ------------------------
17823 -- Is_Volatile_Prefix --
17824 ------------------------
17826 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
17827 Typ : constant Entity_Id := Etype (N);
17829 begin
17830 if Is_Access_Type (Typ) then
17831 declare
17832 Dtyp : constant Entity_Id := Designated_Type (Typ);
17834 begin
17835 return Is_Volatile (Dtyp)
17836 or else Has_Volatile_Components (Dtyp);
17837 end;
17839 else
17840 return Object_Has_Volatile_Components (N);
17841 end if;
17842 end Is_Volatile_Prefix;
17844 ------------------------------------
17845 -- Object_Has_Volatile_Components --
17846 ------------------------------------
17848 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
17849 Typ : constant Entity_Id := Etype (N);
17851 begin
17852 if Is_Volatile (Typ)
17853 or else Has_Volatile_Components (Typ)
17854 then
17855 return True;
17857 elsif Is_Entity_Name (N)
17858 and then (Has_Volatile_Components (Entity (N))
17859 or else Is_Volatile (Entity (N)))
17860 then
17861 return True;
17863 elsif Nkind (N) = N_Indexed_Component
17864 or else Nkind (N) = N_Selected_Component
17865 then
17866 return Is_Volatile_Prefix (Prefix (N));
17868 else
17869 return False;
17870 end if;
17871 end Object_Has_Volatile_Components;
17873 -- Start of processing for Is_Volatile_Object
17875 begin
17876 if Nkind (N) = N_Defining_Identifier then
17877 return Is_Volatile (N) or else Is_Volatile (Etype (N));
17879 elsif Nkind (N) = N_Expanded_Name then
17880 return Is_Volatile_Object (Entity (N));
17882 elsif Is_Volatile (Etype (N))
17883 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
17884 then
17885 return True;
17887 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
17888 and then Is_Volatile_Prefix (Prefix (N))
17889 then
17890 return True;
17892 elsif Nkind (N) = N_Selected_Component
17893 and then Is_Volatile (Entity (Selector_Name (N)))
17894 then
17895 return True;
17897 else
17898 return False;
17899 end if;
17900 end Is_Volatile_Object;
17902 -----------------------------
17903 -- Iterate_Call_Parameters --
17904 -----------------------------
17906 procedure Iterate_Call_Parameters (Call : Node_Id) is
17907 Actual : Node_Id := First_Actual (Call);
17908 Formal : Entity_Id := First_Formal (Get_Called_Entity (Call));
17910 begin
17911 while Present (Formal) and then Present (Actual) loop
17912 Handle_Parameter (Formal, Actual);
17914 Next_Formal (Formal);
17915 Next_Actual (Actual);
17916 end loop;
17918 pragma Assert (No (Formal));
17919 pragma Assert (No (Actual));
17920 end Iterate_Call_Parameters;
17922 ---------------------------
17923 -- Itype_Has_Declaration --
17924 ---------------------------
17926 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
17927 begin
17928 pragma Assert (Is_Itype (Id));
17929 return Present (Parent (Id))
17930 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
17931 N_Subtype_Declaration)
17932 and then Defining_Entity (Parent (Id)) = Id;
17933 end Itype_Has_Declaration;
17935 -------------------------
17936 -- Kill_Current_Values --
17937 -------------------------
17939 procedure Kill_Current_Values
17940 (Ent : Entity_Id;
17941 Last_Assignment_Only : Boolean := False)
17943 begin
17944 if Is_Assignable (Ent) then
17945 Set_Last_Assignment (Ent, Empty);
17946 end if;
17948 if Is_Object (Ent) then
17949 if not Last_Assignment_Only then
17950 Kill_Checks (Ent);
17951 Set_Current_Value (Ent, Empty);
17953 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
17954 -- for a constant. Once the constant is elaborated, its value is
17955 -- not changed, therefore the associated flags that describe the
17956 -- value should not be modified either.
17958 if Ekind (Ent) = E_Constant then
17959 null;
17961 -- Non-constant entities
17963 else
17964 if not Can_Never_Be_Null (Ent) then
17965 Set_Is_Known_Non_Null (Ent, False);
17966 end if;
17968 Set_Is_Known_Null (Ent, False);
17970 -- Reset the Is_Known_Valid flag unless the type is always
17971 -- valid. This does not apply to a loop parameter because its
17972 -- bounds are defined by the loop header and therefore always
17973 -- valid.
17975 if not Is_Known_Valid (Etype (Ent))
17976 and then Ekind (Ent) /= E_Loop_Parameter
17977 then
17978 Set_Is_Known_Valid (Ent, False);
17979 end if;
17980 end if;
17981 end if;
17982 end if;
17983 end Kill_Current_Values;
17985 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
17986 S : Entity_Id;
17988 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
17989 -- Clear current value for entity E and all entities chained to E
17991 ------------------------------------------
17992 -- Kill_Current_Values_For_Entity_Chain --
17993 ------------------------------------------
17995 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
17996 Ent : Entity_Id;
17997 begin
17998 Ent := E;
17999 while Present (Ent) loop
18000 Kill_Current_Values (Ent, Last_Assignment_Only);
18001 Next_Entity (Ent);
18002 end loop;
18003 end Kill_Current_Values_For_Entity_Chain;
18005 -- Start of processing for Kill_Current_Values
18007 begin
18008 -- Kill all saved checks, a special case of killing saved values
18010 if not Last_Assignment_Only then
18011 Kill_All_Checks;
18012 end if;
18014 -- Loop through relevant scopes, which includes the current scope and
18015 -- any parent scopes if the current scope is a block or a package.
18017 S := Current_Scope;
18018 Scope_Loop : loop
18020 -- Clear current values of all entities in current scope
18022 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
18024 -- If scope is a package, also clear current values of all private
18025 -- entities in the scope.
18027 if Is_Package_Or_Generic_Package (S)
18028 or else Is_Concurrent_Type (S)
18029 then
18030 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
18031 end if;
18033 -- If this is a not a subprogram, deal with parents
18035 if not Is_Subprogram (S) then
18036 S := Scope (S);
18037 exit Scope_Loop when S = Standard_Standard;
18038 else
18039 exit Scope_Loop;
18040 end if;
18041 end loop Scope_Loop;
18042 end Kill_Current_Values;
18044 --------------------------
18045 -- Kill_Size_Check_Code --
18046 --------------------------
18048 procedure Kill_Size_Check_Code (E : Entity_Id) is
18049 begin
18050 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
18051 and then Present (Size_Check_Code (E))
18052 then
18053 Remove (Size_Check_Code (E));
18054 Set_Size_Check_Code (E, Empty);
18055 end if;
18056 end Kill_Size_Check_Code;
18058 --------------------
18059 -- Known_Non_Null --
18060 --------------------
18062 function Known_Non_Null (N : Node_Id) return Boolean is
18063 Status : constant Null_Status_Kind := Null_Status (N);
18065 Id : Entity_Id;
18066 Op : Node_Kind;
18067 Val : Node_Id;
18069 begin
18070 -- The expression yields a non-null value ignoring simple flow analysis
18072 if Status = Is_Non_Null then
18073 return True;
18075 -- Otherwise check whether N is a reference to an entity that appears
18076 -- within a conditional construct.
18078 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18080 -- First check if we are in decisive conditional
18082 Get_Current_Value_Condition (N, Op, Val);
18084 if Known_Null (Val) then
18085 if Op = N_Op_Eq then
18086 return False;
18087 elsif Op = N_Op_Ne then
18088 return True;
18089 end if;
18090 end if;
18092 -- If OK to do replacement, test Is_Known_Non_Null flag
18094 Id := Entity (N);
18096 if OK_To_Do_Constant_Replacement (Id) then
18097 return Is_Known_Non_Null (Id);
18098 end if;
18099 end if;
18101 -- Otherwise it is not possible to determine whether N yields a non-null
18102 -- value.
18104 return False;
18105 end Known_Non_Null;
18107 ----------------
18108 -- Known_Null --
18109 ----------------
18111 function Known_Null (N : Node_Id) return Boolean is
18112 Status : constant Null_Status_Kind := Null_Status (N);
18114 Id : Entity_Id;
18115 Op : Node_Kind;
18116 Val : Node_Id;
18118 begin
18119 -- The expression yields a null value ignoring simple flow analysis
18121 if Status = Is_Null then
18122 return True;
18124 -- Otherwise check whether N is a reference to an entity that appears
18125 -- within a conditional construct.
18127 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18129 -- First check if we are in decisive conditional
18131 Get_Current_Value_Condition (N, Op, Val);
18133 if Known_Null (Val) then
18134 if Op = N_Op_Eq then
18135 return True;
18136 elsif Op = N_Op_Ne then
18137 return False;
18138 end if;
18139 end if;
18141 -- If OK to do replacement, test Is_Known_Null flag
18143 Id := Entity (N);
18145 if OK_To_Do_Constant_Replacement (Id) then
18146 return Is_Known_Null (Id);
18147 end if;
18148 end if;
18150 -- Otherwise it is not possible to determine whether N yields a null
18151 -- value.
18153 return False;
18154 end Known_Null;
18156 --------------------------
18157 -- Known_To_Be_Assigned --
18158 --------------------------
18160 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
18161 P : constant Node_Id := Parent (N);
18163 begin
18164 case Nkind (P) is
18166 -- Test left side of assignment
18168 when N_Assignment_Statement =>
18169 return N = Name (P);
18171 -- Function call arguments are never lvalues
18173 when N_Function_Call =>
18174 return False;
18176 -- Positional parameter for procedure or accept call
18178 when N_Accept_Statement
18179 | N_Procedure_Call_Statement
18181 declare
18182 Proc : Entity_Id;
18183 Form : Entity_Id;
18184 Act : Node_Id;
18186 begin
18187 Proc := Get_Subprogram_Entity (P);
18189 if No (Proc) then
18190 return False;
18191 end if;
18193 -- If we are not a list member, something is strange, so
18194 -- be conservative and return False.
18196 if not Is_List_Member (N) then
18197 return False;
18198 end if;
18200 -- We are going to find the right formal by stepping forward
18201 -- through the formals, as we step backwards in the actuals.
18203 Form := First_Formal (Proc);
18204 Act := N;
18205 loop
18206 -- If no formal, something is weird, so be conservative
18207 -- and return False.
18209 if No (Form) then
18210 return False;
18211 end if;
18213 Prev (Act);
18214 exit when No (Act);
18215 Next_Formal (Form);
18216 end loop;
18218 return Ekind (Form) /= E_In_Parameter;
18219 end;
18221 -- Named parameter for procedure or accept call
18223 when N_Parameter_Association =>
18224 declare
18225 Proc : Entity_Id;
18226 Form : Entity_Id;
18228 begin
18229 Proc := Get_Subprogram_Entity (Parent (P));
18231 if No (Proc) then
18232 return False;
18233 end if;
18235 -- Loop through formals to find the one that matches
18237 Form := First_Formal (Proc);
18238 loop
18239 -- If no matching formal, that's peculiar, some kind of
18240 -- previous error, so return False to be conservative.
18241 -- Actually this also happens in legal code in the case
18242 -- where P is a parameter association for an Extra_Formal???
18244 if No (Form) then
18245 return False;
18246 end if;
18248 -- Else test for match
18250 if Chars (Form) = Chars (Selector_Name (P)) then
18251 return Ekind (Form) /= E_In_Parameter;
18252 end if;
18254 Next_Formal (Form);
18255 end loop;
18256 end;
18258 -- Test for appearing in a conversion that itself appears
18259 -- in an lvalue context, since this should be an lvalue.
18261 when N_Type_Conversion =>
18262 return Known_To_Be_Assigned (P);
18264 -- All other references are definitely not known to be modifications
18266 when others =>
18267 return False;
18268 end case;
18269 end Known_To_Be_Assigned;
18271 ---------------------------
18272 -- Last_Source_Statement --
18273 ---------------------------
18275 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
18276 N : Node_Id;
18278 begin
18279 N := Last (Statements (HSS));
18280 while Present (N) loop
18281 exit when Comes_From_Source (N);
18282 Prev (N);
18283 end loop;
18285 return N;
18286 end Last_Source_Statement;
18288 -----------------------
18289 -- Mark_Coextensions --
18290 -----------------------
18292 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
18293 Is_Dynamic : Boolean;
18294 -- Indicates whether the context causes nested coextensions to be
18295 -- dynamic or static
18297 function Mark_Allocator (N : Node_Id) return Traverse_Result;
18298 -- Recognize an allocator node and label it as a dynamic coextension
18300 --------------------
18301 -- Mark_Allocator --
18302 --------------------
18304 function Mark_Allocator (N : Node_Id) return Traverse_Result is
18305 begin
18306 if Nkind (N) = N_Allocator then
18307 if Is_Dynamic then
18308 Set_Is_Dynamic_Coextension (N);
18310 -- If the allocator expression is potentially dynamic, it may
18311 -- be expanded out of order and require dynamic allocation
18312 -- anyway, so we treat the coextension itself as dynamic.
18313 -- Potential optimization ???
18315 elsif Nkind (Expression (N)) = N_Qualified_Expression
18316 and then Nkind (Expression (Expression (N))) = N_Op_Concat
18317 then
18318 Set_Is_Dynamic_Coextension (N);
18319 else
18320 Set_Is_Static_Coextension (N);
18321 end if;
18322 end if;
18324 return OK;
18325 end Mark_Allocator;
18327 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
18329 -- Start of processing for Mark_Coextensions
18331 begin
18332 -- An allocator that appears on the right-hand side of an assignment is
18333 -- treated as a potentially dynamic coextension when the right-hand side
18334 -- is an allocator or a qualified expression.
18336 -- Obj := new ...'(new Coextension ...);
18338 if Nkind (Context_Nod) = N_Assignment_Statement then
18339 Is_Dynamic :=
18340 Nkind_In (Expression (Context_Nod), N_Allocator,
18341 N_Qualified_Expression);
18343 -- An allocator that appears within the expression of a simple return
18344 -- statement is treated as a potentially dynamic coextension when the
18345 -- expression is either aggregate, allocator, or qualified expression.
18347 -- return (new Coextension ...);
18348 -- return new ...'(new Coextension ...);
18350 elsif Nkind (Context_Nod) = N_Simple_Return_Statement then
18351 Is_Dynamic :=
18352 Nkind_In (Expression (Context_Nod), N_Aggregate,
18353 N_Allocator,
18354 N_Qualified_Expression);
18356 -- An alloctor that appears within the initialization expression of an
18357 -- object declaration is considered a potentially dynamic coextension
18358 -- when the initialization expression is an allocator or a qualified
18359 -- expression.
18361 -- Obj : ... := new ...'(new Coextension ...);
18363 -- A similar case arises when the object declaration is part of an
18364 -- extended return statement.
18366 -- return Obj : ... := new ...'(new Coextension ...);
18367 -- return Obj : ... := (new Coextension ...);
18369 elsif Nkind (Context_Nod) = N_Object_Declaration then
18370 Is_Dynamic :=
18371 Nkind_In (Root_Nod, N_Allocator, N_Qualified_Expression)
18372 or else
18373 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
18375 -- This routine should not be called with constructs that cannot contain
18376 -- coextensions.
18378 else
18379 raise Program_Error;
18380 end if;
18382 Mark_Allocators (Root_Nod);
18383 end Mark_Coextensions;
18385 ---------------------------------
18386 -- Mark_Elaboration_Attributes --
18387 ---------------------------------
18389 procedure Mark_Elaboration_Attributes
18390 (N_Id : Node_Or_Entity_Id;
18391 Checks : Boolean := False;
18392 Level : Boolean := False;
18393 Modes : Boolean := False;
18394 Warnings : Boolean := False)
18396 function Elaboration_Checks_OK
18397 (Target_Id : Entity_Id;
18398 Context_Id : Entity_Id) return Boolean;
18399 -- Determine whether elaboration checks are enabled for target Target_Id
18400 -- which resides within context Context_Id.
18402 procedure Mark_Elaboration_Attributes_Id (Id : Entity_Id);
18403 -- Preserve relevant attributes of the context in arbitrary entity Id
18405 procedure Mark_Elaboration_Attributes_Node (N : Node_Id);
18406 -- Preserve relevant attributes of the context in arbitrary node N
18408 ---------------------------
18409 -- Elaboration_Checks_OK --
18410 ---------------------------
18412 function Elaboration_Checks_OK
18413 (Target_Id : Entity_Id;
18414 Context_Id : Entity_Id) return Boolean
18416 Encl_Scop : Entity_Id;
18418 begin
18419 -- Elaboration checks are suppressed for the target
18421 if Elaboration_Checks_Suppressed (Target_Id) then
18422 return False;
18423 end if;
18425 -- Otherwise elaboration checks are OK for the target, but may be
18426 -- suppressed for the context where the target is declared.
18428 Encl_Scop := Context_Id;
18429 while Present (Encl_Scop) and then Encl_Scop /= Standard_Standard loop
18430 if Elaboration_Checks_Suppressed (Encl_Scop) then
18431 return False;
18432 end if;
18434 Encl_Scop := Scope (Encl_Scop);
18435 end loop;
18437 -- Neither the target nor its declarative context have elaboration
18438 -- checks suppressed.
18440 return True;
18441 end Elaboration_Checks_OK;
18443 ------------------------------------
18444 -- Mark_Elaboration_Attributes_Id --
18445 ------------------------------------
18447 procedure Mark_Elaboration_Attributes_Id (Id : Entity_Id) is
18448 begin
18449 -- Mark the status of elaboration checks in effect. Do not reset the
18450 -- status in case the entity is reanalyzed with checks suppressed.
18452 if Checks and then not Is_Elaboration_Checks_OK_Id (Id) then
18453 Set_Is_Elaboration_Checks_OK_Id (Id,
18454 Elaboration_Checks_OK
18455 (Target_Id => Id,
18456 Context_Id => Scope (Id)));
18457 end if;
18459 -- Mark the status of elaboration warnings in effect. Do not reset
18460 -- the status in case the entity is reanalyzed with warnings off.
18462 if Warnings and then not Is_Elaboration_Warnings_OK_Id (Id) then
18463 Set_Is_Elaboration_Warnings_OK_Id (Id, Elab_Warnings);
18464 end if;
18465 end Mark_Elaboration_Attributes_Id;
18467 --------------------------------------
18468 -- Mark_Elaboration_Attributes_Node --
18469 --------------------------------------
18471 procedure Mark_Elaboration_Attributes_Node (N : Node_Id) is
18472 function Extract_Name (N : Node_Id) return Node_Id;
18473 -- Obtain the Name attribute of call or instantiation N
18475 ------------------
18476 -- Extract_Name --
18477 ------------------
18479 function Extract_Name (N : Node_Id) return Node_Id is
18480 Nam : Node_Id;
18482 begin
18483 Nam := Name (N);
18485 -- A call to an entry family appears in indexed form
18487 if Nkind (Nam) = N_Indexed_Component then
18488 Nam := Prefix (Nam);
18489 end if;
18491 -- The name may also appear in qualified form
18493 if Nkind (Nam) = N_Selected_Component then
18494 Nam := Selector_Name (Nam);
18495 end if;
18497 return Nam;
18498 end Extract_Name;
18500 -- Local variables
18502 Context_Id : Entity_Id;
18503 Nam : Node_Id;
18505 -- Start of processing for Mark_Elaboration_Attributes_Node
18507 begin
18508 -- Mark the status of elaboration checks in effect. Do not reset the
18509 -- status in case the node is reanalyzed with checks suppressed.
18511 if Checks and then not Is_Elaboration_Checks_OK_Node (N) then
18513 -- Assignments, attribute references, and variable references do
18514 -- not have a "declarative" context.
18516 Context_Id := Empty;
18518 -- The status of elaboration checks for calls and instantiations
18519 -- depends on the most recent pragma Suppress/Unsuppress, as well
18520 -- as the suppression status of the context where the target is
18521 -- defined.
18523 -- package Pack is
18524 -- function Func ...;
18525 -- end Pack;
18527 -- with Pack;
18528 -- procedure Main is
18529 -- pragma Suppress (Elaboration_Checks, Pack);
18530 -- X : ... := Pack.Func;
18531 -- ...
18533 -- In the example above, the call to Func has elaboration checks
18534 -- enabled because there is no active general purpose suppression
18535 -- pragma, however the elaboration checks of Pack are explicitly
18536 -- suppressed. As a result the elaboration checks of the call must
18537 -- be disabled in order to preserve this dependency.
18539 if Nkind_In (N, N_Entry_Call_Statement,
18540 N_Function_Call,
18541 N_Function_Instantiation,
18542 N_Package_Instantiation,
18543 N_Procedure_Call_Statement,
18544 N_Procedure_Instantiation)
18545 then
18546 Nam := Extract_Name (N);
18548 if Is_Entity_Name (Nam) and then Present (Entity (Nam)) then
18549 Context_Id := Scope (Entity (Nam));
18550 end if;
18551 end if;
18553 Set_Is_Elaboration_Checks_OK_Node (N,
18554 Elaboration_Checks_OK
18555 (Target_Id => Empty,
18556 Context_Id => Context_Id));
18557 end if;
18559 -- Mark the enclosing level of the node. Do not reset the status in
18560 -- case the node is relocated and reanalyzed.
18562 if Level and then not Is_Declaration_Level_Node (N) then
18563 Set_Is_Declaration_Level_Node (N,
18564 Find_Enclosing_Level (N) = Declaration_Level);
18565 end if;
18567 -- Mark the Ghost and SPARK mode in effect
18569 if Modes then
18570 if Ghost_Mode = Ignore then
18571 Set_Is_Ignored_Ghost_Node (N);
18572 end if;
18574 if SPARK_Mode = On then
18575 Set_Is_SPARK_Mode_On_Node (N);
18576 end if;
18577 end if;
18579 -- Mark the status of elaboration warnings in effect. Do not reset
18580 -- the status in case the node is reanalyzed with warnings off.
18582 if Warnings and then not Is_Elaboration_Warnings_OK_Node (N) then
18583 Set_Is_Elaboration_Warnings_OK_Node (N, Elab_Warnings);
18584 end if;
18585 end Mark_Elaboration_Attributes_Node;
18587 -- Start of processing for Mark_Elaboration_Attributes
18589 begin
18590 -- Do not capture any elaboration-related attributes when switch -gnatH
18591 -- (legacy elaboration checking mode enabled) is in effect because the
18592 -- attributes are useless to the legacy model.
18594 if Legacy_Elaboration_Checks then
18595 return;
18596 end if;
18598 if Nkind (N_Id) in N_Entity then
18599 Mark_Elaboration_Attributes_Id (N_Id);
18600 else
18601 Mark_Elaboration_Attributes_Node (N_Id);
18602 end if;
18603 end Mark_Elaboration_Attributes;
18605 ----------------------------------
18606 -- Matching_Static_Array_Bounds --
18607 ----------------------------------
18609 function Matching_Static_Array_Bounds
18610 (L_Typ : Node_Id;
18611 R_Typ : Node_Id) return Boolean
18613 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
18614 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
18616 L_Index : Node_Id := Empty; -- init to ...
18617 R_Index : Node_Id := Empty; -- ...avoid warnings
18618 L_Low : Node_Id;
18619 L_High : Node_Id;
18620 L_Len : Uint;
18621 R_Low : Node_Id;
18622 R_High : Node_Id;
18623 R_Len : Uint;
18625 begin
18626 if L_Ndims /= R_Ndims then
18627 return False;
18628 end if;
18630 -- Unconstrained types do not have static bounds
18632 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
18633 return False;
18634 end if;
18636 -- First treat specially the first dimension, as the lower bound and
18637 -- length of string literals are not stored like those of arrays.
18639 if Ekind (L_Typ) = E_String_Literal_Subtype then
18640 L_Low := String_Literal_Low_Bound (L_Typ);
18641 L_Len := String_Literal_Length (L_Typ);
18642 else
18643 L_Index := First_Index (L_Typ);
18644 Get_Index_Bounds (L_Index, L_Low, L_High);
18646 if Is_OK_Static_Expression (L_Low)
18647 and then
18648 Is_OK_Static_Expression (L_High)
18649 then
18650 if Expr_Value (L_High) < Expr_Value (L_Low) then
18651 L_Len := Uint_0;
18652 else
18653 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
18654 end if;
18655 else
18656 return False;
18657 end if;
18658 end if;
18660 if Ekind (R_Typ) = E_String_Literal_Subtype then
18661 R_Low := String_Literal_Low_Bound (R_Typ);
18662 R_Len := String_Literal_Length (R_Typ);
18663 else
18664 R_Index := First_Index (R_Typ);
18665 Get_Index_Bounds (R_Index, R_Low, R_High);
18667 if Is_OK_Static_Expression (R_Low)
18668 and then
18669 Is_OK_Static_Expression (R_High)
18670 then
18671 if Expr_Value (R_High) < Expr_Value (R_Low) then
18672 R_Len := Uint_0;
18673 else
18674 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
18675 end if;
18676 else
18677 return False;
18678 end if;
18679 end if;
18681 if (Is_OK_Static_Expression (L_Low)
18682 and then
18683 Is_OK_Static_Expression (R_Low))
18684 and then Expr_Value (L_Low) = Expr_Value (R_Low)
18685 and then L_Len = R_Len
18686 then
18687 null;
18688 else
18689 return False;
18690 end if;
18692 -- Then treat all other dimensions
18694 for Indx in 2 .. L_Ndims loop
18695 Next (L_Index);
18696 Next (R_Index);
18698 Get_Index_Bounds (L_Index, L_Low, L_High);
18699 Get_Index_Bounds (R_Index, R_Low, R_High);
18701 if (Is_OK_Static_Expression (L_Low) and then
18702 Is_OK_Static_Expression (L_High) and then
18703 Is_OK_Static_Expression (R_Low) and then
18704 Is_OK_Static_Expression (R_High))
18705 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
18706 and then
18707 Expr_Value (L_High) = Expr_Value (R_High))
18708 then
18709 null;
18710 else
18711 return False;
18712 end if;
18713 end loop;
18715 -- If we fall through the loop, all indexes matched
18717 return True;
18718 end Matching_Static_Array_Bounds;
18720 -------------------
18721 -- May_Be_Lvalue --
18722 -------------------
18724 function May_Be_Lvalue (N : Node_Id) return Boolean is
18725 P : constant Node_Id := Parent (N);
18727 begin
18728 case Nkind (P) is
18730 -- Test left side of assignment
18732 when N_Assignment_Statement =>
18733 return N = Name (P);
18735 -- Test prefix of component or attribute. Note that the prefix of an
18736 -- explicit or implicit dereference cannot be an l-value. In the case
18737 -- of a 'Read attribute, the reference can be an actual in the
18738 -- argument list of the attribute.
18740 when N_Attribute_Reference =>
18741 return (N = Prefix (P)
18742 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P)))
18743 or else
18744 Attribute_Name (P) = Name_Read;
18746 -- For an expanded name, the name is an lvalue if the expanded name
18747 -- is an lvalue, but the prefix is never an lvalue, since it is just
18748 -- the scope where the name is found.
18750 when N_Expanded_Name =>
18751 if N = Prefix (P) then
18752 return May_Be_Lvalue (P);
18753 else
18754 return False;
18755 end if;
18757 -- For a selected component A.B, A is certainly an lvalue if A.B is.
18758 -- B is a little interesting, if we have A.B := 3, there is some
18759 -- discussion as to whether B is an lvalue or not, we choose to say
18760 -- it is. Note however that A is not an lvalue if it is of an access
18761 -- type since this is an implicit dereference.
18763 when N_Selected_Component =>
18764 if N = Prefix (P)
18765 and then Present (Etype (N))
18766 and then Is_Access_Type (Etype (N))
18767 then
18768 return False;
18769 else
18770 return May_Be_Lvalue (P);
18771 end if;
18773 -- For an indexed component or slice, the index or slice bounds is
18774 -- never an lvalue. The prefix is an lvalue if the indexed component
18775 -- or slice is an lvalue, except if it is an access type, where we
18776 -- have an implicit dereference.
18778 when N_Indexed_Component
18779 | N_Slice
18781 if N /= Prefix (P)
18782 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
18783 then
18784 return False;
18785 else
18786 return May_Be_Lvalue (P);
18787 end if;
18789 -- Prefix of a reference is an lvalue if the reference is an lvalue
18791 when N_Reference =>
18792 return May_Be_Lvalue (P);
18794 -- Prefix of explicit dereference is never an lvalue
18796 when N_Explicit_Dereference =>
18797 return False;
18799 -- Positional parameter for subprogram, entry, or accept call.
18800 -- In older versions of Ada function call arguments are never
18801 -- lvalues. In Ada 2012 functions can have in-out parameters.
18803 when N_Accept_Statement
18804 | N_Entry_Call_Statement
18805 | N_Subprogram_Call
18807 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
18808 return False;
18809 end if;
18811 -- The following mechanism is clumsy and fragile. A single flag
18812 -- set in Resolve_Actuals would be preferable ???
18814 declare
18815 Proc : Entity_Id;
18816 Form : Entity_Id;
18817 Act : Node_Id;
18819 begin
18820 Proc := Get_Subprogram_Entity (P);
18822 if No (Proc) then
18823 return True;
18824 end if;
18826 -- If we are not a list member, something is strange, so be
18827 -- conservative and return True.
18829 if not Is_List_Member (N) then
18830 return True;
18831 end if;
18833 -- We are going to find the right formal by stepping forward
18834 -- through the formals, as we step backwards in the actuals.
18836 Form := First_Formal (Proc);
18837 Act := N;
18838 loop
18839 -- If no formal, something is weird, so be conservative and
18840 -- return True.
18842 if No (Form) then
18843 return True;
18844 end if;
18846 Prev (Act);
18847 exit when No (Act);
18848 Next_Formal (Form);
18849 end loop;
18851 return Ekind (Form) /= E_In_Parameter;
18852 end;
18854 -- Named parameter for procedure or accept call
18856 when N_Parameter_Association =>
18857 declare
18858 Proc : Entity_Id;
18859 Form : Entity_Id;
18861 begin
18862 Proc := Get_Subprogram_Entity (Parent (P));
18864 if No (Proc) then
18865 return True;
18866 end if;
18868 -- Loop through formals to find the one that matches
18870 Form := First_Formal (Proc);
18871 loop
18872 -- If no matching formal, that's peculiar, some kind of
18873 -- previous error, so return True to be conservative.
18874 -- Actually happens with legal code for an unresolved call
18875 -- where we may get the wrong homonym???
18877 if No (Form) then
18878 return True;
18879 end if;
18881 -- Else test for match
18883 if Chars (Form) = Chars (Selector_Name (P)) then
18884 return Ekind (Form) /= E_In_Parameter;
18885 end if;
18887 Next_Formal (Form);
18888 end loop;
18889 end;
18891 -- Test for appearing in a conversion that itself appears in an
18892 -- lvalue context, since this should be an lvalue.
18894 when N_Type_Conversion =>
18895 return May_Be_Lvalue (P);
18897 -- Test for appearance in object renaming declaration
18899 when N_Object_Renaming_Declaration =>
18900 return True;
18902 -- All other references are definitely not lvalues
18904 when others =>
18905 return False;
18906 end case;
18907 end May_Be_Lvalue;
18909 -----------------
18910 -- Might_Raise --
18911 -----------------
18913 function Might_Raise (N : Node_Id) return Boolean is
18914 Result : Boolean := False;
18916 function Process (N : Node_Id) return Traverse_Result;
18917 -- Set Result to True if we find something that could raise an exception
18919 -------------
18920 -- Process --
18921 -------------
18923 function Process (N : Node_Id) return Traverse_Result is
18924 begin
18925 if Nkind_In (N, N_Procedure_Call_Statement,
18926 N_Function_Call,
18927 N_Raise_Statement,
18928 N_Raise_Constraint_Error,
18929 N_Raise_Program_Error,
18930 N_Raise_Storage_Error)
18931 then
18932 Result := True;
18933 return Abandon;
18934 else
18935 return OK;
18936 end if;
18937 end Process;
18939 procedure Set_Result is new Traverse_Proc (Process);
18941 -- Start of processing for Might_Raise
18943 begin
18944 -- False if exceptions can't be propagated
18946 if No_Exception_Handlers_Set then
18947 return False;
18948 end if;
18950 -- If the checks handled by the back end are not disabled, we cannot
18951 -- ensure that no exception will be raised.
18953 if not Access_Checks_Suppressed (Empty)
18954 or else not Discriminant_Checks_Suppressed (Empty)
18955 or else not Range_Checks_Suppressed (Empty)
18956 or else not Index_Checks_Suppressed (Empty)
18957 or else Opt.Stack_Checking_Enabled
18958 then
18959 return True;
18960 end if;
18962 Set_Result (N);
18963 return Result;
18964 end Might_Raise;
18966 --------------------------------
18967 -- Nearest_Enclosing_Instance --
18968 --------------------------------
18970 function Nearest_Enclosing_Instance (E : Entity_Id) return Entity_Id is
18971 Inst : Entity_Id;
18973 begin
18974 Inst := Scope (E);
18975 while Present (Inst) and then Inst /= Standard_Standard loop
18976 if Is_Generic_Instance (Inst) then
18977 return Inst;
18978 end if;
18980 Inst := Scope (Inst);
18981 end loop;
18983 return Empty;
18984 end Nearest_Enclosing_Instance;
18986 ----------------------
18987 -- Needs_One_Actual --
18988 ----------------------
18990 function Needs_One_Actual (E : Entity_Id) return Boolean is
18991 Formal : Entity_Id;
18993 begin
18994 -- Ada 2005 or later, and formals present. The first formal must be
18995 -- of a type that supports prefix notation: a controlling argument,
18996 -- a class-wide type, or an access to such.
18998 if Ada_Version >= Ada_2005
18999 and then Present (First_Formal (E))
19000 and then No (Default_Value (First_Formal (E)))
19001 and then
19002 (Is_Controlling_Formal (First_Formal (E))
19003 or else Is_Class_Wide_Type (Etype (First_Formal (E)))
19004 or else Is_Anonymous_Access_Type (Etype (First_Formal (E))))
19005 then
19006 Formal := Next_Formal (First_Formal (E));
19007 while Present (Formal) loop
19008 if No (Default_Value (Formal)) then
19009 return False;
19010 end if;
19012 Next_Formal (Formal);
19013 end loop;
19015 return True;
19017 -- Ada 83/95 or no formals
19019 else
19020 return False;
19021 end if;
19022 end Needs_One_Actual;
19024 ---------------------------------
19025 -- Needs_Simple_Initialization --
19026 ---------------------------------
19028 function Needs_Simple_Initialization
19029 (Typ : Entity_Id;
19030 Consider_IS : Boolean := True) return Boolean
19032 Consider_IS_NS : constant Boolean :=
19033 Normalize_Scalars or (Initialize_Scalars and Consider_IS);
19035 begin
19036 -- Never need initialization if it is suppressed
19038 if Initialization_Suppressed (Typ) then
19039 return False;
19040 end if;
19042 -- Check for private type, in which case test applies to the underlying
19043 -- type of the private type.
19045 if Is_Private_Type (Typ) then
19046 declare
19047 RT : constant Entity_Id := Underlying_Type (Typ);
19048 begin
19049 if Present (RT) then
19050 return Needs_Simple_Initialization (RT);
19051 else
19052 return False;
19053 end if;
19054 end;
19056 -- Scalar type with Default_Value aspect requires initialization
19058 elsif Is_Scalar_Type (Typ) and then Has_Default_Aspect (Typ) then
19059 return True;
19061 -- Cases needing simple initialization are access types, and, if pragma
19062 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
19063 -- types.
19065 elsif Is_Access_Type (Typ)
19066 or else (Consider_IS_NS and then (Is_Scalar_Type (Typ)))
19067 then
19068 return True;
19070 -- If Initialize/Normalize_Scalars is in effect, string objects also
19071 -- need initialization, unless they are created in the course of
19072 -- expanding an aggregate (since in the latter case they will be
19073 -- filled with appropriate initializing values before they are used).
19075 elsif Consider_IS_NS
19076 and then Is_Standard_String_Type (Typ)
19077 and then
19078 (not Is_Itype (Typ)
19079 or else Nkind (Associated_Node_For_Itype (Typ)) /= N_Aggregate)
19080 then
19081 return True;
19083 else
19084 return False;
19085 end if;
19086 end Needs_Simple_Initialization;
19088 ------------------------
19089 -- New_Copy_List_Tree --
19090 ------------------------
19092 function New_Copy_List_Tree (List : List_Id) return List_Id is
19093 NL : List_Id;
19094 E : Node_Id;
19096 begin
19097 if List = No_List then
19098 return No_List;
19100 else
19101 NL := New_List;
19102 E := First (List);
19104 while Present (E) loop
19105 Append (New_Copy_Tree (E), NL);
19106 E := Next (E);
19107 end loop;
19109 return NL;
19110 end if;
19111 end New_Copy_List_Tree;
19113 -------------------
19114 -- New_Copy_Tree --
19115 -------------------
19117 -- The following tables play a key role in replicating entities and Itypes.
19118 -- They are intentionally declared at the library level rather than within
19119 -- New_Copy_Tree to avoid elaborating them on each call. This performance
19120 -- optimization saves up to 2% of the entire compilation time spent in the
19121 -- front end. Care should be taken to reset the tables on each new call to
19122 -- New_Copy_Tree.
19124 NCT_Table_Max : constant := 511;
19126 subtype NCT_Table_Index is Nat range 0 .. NCT_Table_Max - 1;
19128 function NCT_Table_Hash (Key : Node_Or_Entity_Id) return NCT_Table_Index;
19129 -- Obtain the hash value of node or entity Key
19131 --------------------
19132 -- NCT_Table_Hash --
19133 --------------------
19135 function NCT_Table_Hash (Key : Node_Or_Entity_Id) return NCT_Table_Index is
19136 begin
19137 return NCT_Table_Index (Key mod NCT_Table_Max);
19138 end NCT_Table_Hash;
19140 ----------------------
19141 -- NCT_New_Entities --
19142 ----------------------
19144 -- The following table maps old entities and Itypes to their corresponding
19145 -- new entities and Itypes.
19147 -- Aaa -> Xxx
19149 package NCT_New_Entities is new Simple_HTable (
19150 Header_Num => NCT_Table_Index,
19151 Element => Entity_Id,
19152 No_Element => Empty,
19153 Key => Entity_Id,
19154 Hash => NCT_Table_Hash,
19155 Equal => "=");
19157 ------------------------
19158 -- NCT_Pending_Itypes --
19159 ------------------------
19161 -- The following table maps old Associated_Node_For_Itype nodes to a set of
19162 -- new itypes. Given a set of old Itypes Aaa, Bbb, and Ccc, where all three
19163 -- have the same Associated_Node_For_Itype Ppp, and their corresponding new
19164 -- Itypes Xxx, Yyy, Zzz, the table contains the following mapping:
19166 -- Ppp -> (Xxx, Yyy, Zzz)
19168 -- The set is expressed as an Elist
19170 package NCT_Pending_Itypes is new Simple_HTable (
19171 Header_Num => NCT_Table_Index,
19172 Element => Elist_Id,
19173 No_Element => No_Elist,
19174 Key => Node_Id,
19175 Hash => NCT_Table_Hash,
19176 Equal => "=");
19178 NCT_Tables_In_Use : Boolean := False;
19179 -- This flag keeps track of whether the two tables NCT_New_Entities and
19180 -- NCT_Pending_Itypes are in use. The flag is part of an optimization
19181 -- where certain operations are not performed if the tables are not in
19182 -- use. This saves up to 8% of the entire compilation time spent in the
19183 -- front end.
19185 -------------------
19186 -- New_Copy_Tree --
19187 -------------------
19189 function New_Copy_Tree
19190 (Source : Node_Id;
19191 Map : Elist_Id := No_Elist;
19192 New_Sloc : Source_Ptr := No_Location;
19193 New_Scope : Entity_Id := Empty) return Node_Id
19195 -- This routine performs low-level tree manipulations and needs access
19196 -- to the internals of the tree.
19198 use Atree.Unchecked_Access;
19199 use Atree_Private_Part;
19201 EWA_Level : Nat := 0;
19202 -- This counter keeps track of how many N_Expression_With_Actions nodes
19203 -- are encountered during a depth-first traversal of the subtree. These
19204 -- nodes may define new entities in their Actions lists and thus require
19205 -- special processing.
19207 EWA_Inner_Scope_Level : Nat := 0;
19208 -- This counter keeps track of how many scoping constructs appear within
19209 -- an N_Expression_With_Actions node.
19211 procedure Add_New_Entity (Old_Id : Entity_Id; New_Id : Entity_Id);
19212 pragma Inline (Add_New_Entity);
19213 -- Add an entry in the NCT_New_Entities table which maps key Old_Id to
19214 -- value New_Id. Old_Id is an entity which appears within the Actions
19215 -- list of an N_Expression_With_Actions node, or within an entity map.
19216 -- New_Id is the corresponding new entity generated during Phase 1.
19218 procedure Add_Pending_Itype (Assoc_Nod : Node_Id; Itype : Entity_Id);
19219 pragma Inline (Add_New_Entity);
19220 -- Add an entry in the NCT_Pending_Itypes which maps key Assoc_Nod to
19221 -- value Itype. Assoc_Nod is the associated node of an itype. Itype is
19222 -- an itype.
19224 procedure Build_NCT_Tables (Entity_Map : Elist_Id);
19225 pragma Inline (Build_NCT_Tables);
19226 -- Populate tables NCT_New_Entities and NCT_Pending_Itypes with the
19227 -- information supplied in entity map Entity_Map. The format of the
19228 -- entity map must be as follows:
19230 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
19232 function Copy_Any_Node_With_Replacement
19233 (N : Node_Or_Entity_Id) return Node_Or_Entity_Id;
19234 pragma Inline (Copy_Any_Node_With_Replacement);
19235 -- Replicate entity or node N by invoking one of the following routines:
19237 -- Copy_Node_With_Replacement
19238 -- Corresponding_Entity
19240 function Copy_Elist_With_Replacement (List : Elist_Id) return Elist_Id;
19241 -- Replicate the elements of entity list List
19243 function Copy_Field_With_Replacement
19244 (Field : Union_Id;
19245 Old_Par : Node_Id := Empty;
19246 New_Par : Node_Id := Empty;
19247 Semantic : Boolean := False) return Union_Id;
19248 -- Replicate field Field by invoking one of the following routines:
19250 -- Copy_Elist_With_Replacement
19251 -- Copy_List_With_Replacement
19252 -- Copy_Node_With_Replacement
19253 -- Corresponding_Entity
19255 -- If the field is not an entity list, entity, itype, syntactic list,
19256 -- or node, then the field is returned unchanged. The routine always
19257 -- replicates entities, itypes, and valid syntactic fields. Old_Par is
19258 -- the expected parent of a syntactic field. New_Par is the new parent
19259 -- associated with a replicated syntactic field. Flag Semantic should
19260 -- be set when the input is a semantic field.
19262 function Copy_List_With_Replacement (List : List_Id) return List_Id;
19263 -- Replicate the elements of syntactic list List
19265 function Copy_Node_With_Replacement (N : Node_Id) return Node_Id;
19266 -- Replicate node N
19268 function Corresponding_Entity (Id : Entity_Id) return Entity_Id;
19269 pragma Inline (Corresponding_Entity);
19270 -- Return the corresponding new entity of Id generated during Phase 1.
19271 -- If there is no such entity, return Id.
19273 function In_Entity_Map
19274 (Id : Entity_Id;
19275 Entity_Map : Elist_Id) return Boolean;
19276 pragma Inline (In_Entity_Map);
19277 -- Determine whether entity Id is one of the old ids specified in entity
19278 -- map Entity_Map. The format of the entity map must be as follows:
19280 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
19282 procedure Update_CFS_Sloc (N : Node_Or_Entity_Id);
19283 pragma Inline (Update_CFS_Sloc);
19284 -- Update the Comes_From_Source and Sloc attributes of node or entity N
19286 procedure Update_First_Real_Statement
19287 (Old_HSS : Node_Id;
19288 New_HSS : Node_Id);
19289 pragma Inline (Update_First_Real_Statement);
19290 -- Update semantic attribute First_Real_Statement of handled sequence of
19291 -- statements New_HSS based on handled sequence of statements Old_HSS.
19293 procedure Update_Named_Associations
19294 (Old_Call : Node_Id;
19295 New_Call : Node_Id);
19296 pragma Inline (Update_Named_Associations);
19297 -- Update semantic chain First/Next_Named_Association of call New_call
19298 -- based on call Old_Call.
19300 procedure Update_New_Entities (Entity_Map : Elist_Id);
19301 pragma Inline (Update_New_Entities);
19302 -- Update the semantic attributes of all new entities generated during
19303 -- Phase 1 that do not appear in entity map Entity_Map. The format of
19304 -- the entity map must be as follows:
19306 -- Old_Id1, New_Id1, Old_Id2, New_Id2, .., Old_IdN, New_IdN
19308 procedure Update_Pending_Itypes
19309 (Old_Assoc : Node_Id;
19310 New_Assoc : Node_Id);
19311 pragma Inline (Update_Pending_Itypes);
19312 -- Update semantic attribute Associated_Node_For_Itype to refer to node
19313 -- New_Assoc for all itypes whose associated node is Old_Assoc.
19315 procedure Update_Semantic_Fields (Id : Entity_Id);
19316 pragma Inline (Update_Semantic_Fields);
19317 -- Subsidiary to Update_New_Entities. Update semantic fields of entity
19318 -- or itype Id.
19320 procedure Visit_Any_Node (N : Node_Or_Entity_Id);
19321 pragma Inline (Visit_Any_Node);
19322 -- Visit entity of node N by invoking one of the following routines:
19324 -- Visit_Entity
19325 -- Visit_Itype
19326 -- Visit_Node
19328 procedure Visit_Elist (List : Elist_Id);
19329 -- Visit the elements of entity list List
19331 procedure Visit_Entity (Id : Entity_Id);
19332 -- Visit entity Id. This action may create a new entity of Id and save
19333 -- it in table NCT_New_Entities.
19335 procedure Visit_Field
19336 (Field : Union_Id;
19337 Par_Nod : Node_Id := Empty;
19338 Semantic : Boolean := False);
19339 -- Visit field Field by invoking one of the following routines:
19341 -- Visit_Elist
19342 -- Visit_Entity
19343 -- Visit_Itype
19344 -- Visit_List
19345 -- Visit_Node
19347 -- If the field is not an entity list, entity, itype, syntactic list,
19348 -- or node, then the field is not visited. The routine always visits
19349 -- valid syntactic fields. Par_Nod is the expected parent of the
19350 -- syntactic field. Flag Semantic should be set when the input is a
19351 -- semantic field.
19353 procedure Visit_Itype (Itype : Entity_Id);
19354 -- Visit itype Itype. This action may create a new entity for Itype and
19355 -- save it in table NCT_New_Entities. In addition, the routine may map
19356 -- the associated node of Itype to the new itype in NCT_Pending_Itypes.
19358 procedure Visit_List (List : List_Id);
19359 -- Visit the elements of syntactic list List
19361 procedure Visit_Node (N : Node_Id);
19362 -- Visit node N
19364 procedure Visit_Semantic_Fields (Id : Entity_Id);
19365 pragma Inline (Visit_Semantic_Fields);
19366 -- Subsidiary to Visit_Entity and Visit_Itype. Visit common semantic
19367 -- fields of entity or itype Id.
19369 --------------------
19370 -- Add_New_Entity --
19371 --------------------
19373 procedure Add_New_Entity (Old_Id : Entity_Id; New_Id : Entity_Id) is
19374 begin
19375 pragma Assert (Present (Old_Id));
19376 pragma Assert (Present (New_Id));
19377 pragma Assert (Nkind (Old_Id) in N_Entity);
19378 pragma Assert (Nkind (New_Id) in N_Entity);
19380 NCT_Tables_In_Use := True;
19382 -- Sanity check the NCT_New_Entities table. No previous mapping with
19383 -- key Old_Id should exist.
19385 pragma Assert (No (NCT_New_Entities.Get (Old_Id)));
19387 -- Establish the mapping
19389 -- Old_Id -> New_Id
19391 NCT_New_Entities.Set (Old_Id, New_Id);
19392 end Add_New_Entity;
19394 -----------------------
19395 -- Add_Pending_Itype --
19396 -----------------------
19398 procedure Add_Pending_Itype (Assoc_Nod : Node_Id; Itype : Entity_Id) is
19399 Itypes : Elist_Id;
19401 begin
19402 pragma Assert (Present (Assoc_Nod));
19403 pragma Assert (Present (Itype));
19404 pragma Assert (Nkind (Itype) in N_Entity);
19405 pragma Assert (Is_Itype (Itype));
19407 NCT_Tables_In_Use := True;
19409 -- It is not possible to sanity check the NCT_Pendint_Itypes table
19410 -- directly because a single node may act as the associated node for
19411 -- multiple itypes.
19413 Itypes := NCT_Pending_Itypes.Get (Assoc_Nod);
19415 if No (Itypes) then
19416 Itypes := New_Elmt_List;
19417 NCT_Pending_Itypes.Set (Assoc_Nod, Itypes);
19418 end if;
19420 -- Establish the mapping
19422 -- Assoc_Nod -> (Itype, ...)
19424 -- Avoid inserting the same itype multiple times. This involves a
19425 -- linear search, however the set of itypes with the same associated
19426 -- node is very small.
19428 Append_Unique_Elmt (Itype, Itypes);
19429 end Add_Pending_Itype;
19431 ----------------------
19432 -- Build_NCT_Tables --
19433 ----------------------
19435 procedure Build_NCT_Tables (Entity_Map : Elist_Id) is
19436 Elmt : Elmt_Id;
19437 Old_Id : Entity_Id;
19438 New_Id : Entity_Id;
19440 begin
19441 -- Nothing to do when there is no entity map
19443 if No (Entity_Map) then
19444 return;
19445 end if;
19447 Elmt := First_Elmt (Entity_Map);
19448 while Present (Elmt) loop
19450 -- Extract the (Old_Id, New_Id) pair from the entity map
19452 Old_Id := Node (Elmt);
19453 Next_Elmt (Elmt);
19455 New_Id := Node (Elmt);
19456 Next_Elmt (Elmt);
19458 -- Establish the following mapping within table NCT_New_Entities
19460 -- Old_Id -> New_Id
19462 Add_New_Entity (Old_Id, New_Id);
19464 -- Establish the following mapping within table NCT_Pending_Itypes
19465 -- when the new entity is an itype.
19467 -- Assoc_Nod -> (New_Id, ...)
19469 -- IMPORTANT: the associated node is that of the old itype because
19470 -- the node will be replicated in Phase 2.
19472 if Is_Itype (Old_Id) then
19473 Add_Pending_Itype
19474 (Assoc_Nod => Associated_Node_For_Itype (Old_Id),
19475 Itype => New_Id);
19476 end if;
19477 end loop;
19478 end Build_NCT_Tables;
19480 ------------------------------------
19481 -- Copy_Any_Node_With_Replacement --
19482 ------------------------------------
19484 function Copy_Any_Node_With_Replacement
19485 (N : Node_Or_Entity_Id) return Node_Or_Entity_Id
19487 begin
19488 if Nkind (N) in N_Entity then
19489 return Corresponding_Entity (N);
19490 else
19491 return Copy_Node_With_Replacement (N);
19492 end if;
19493 end Copy_Any_Node_With_Replacement;
19495 ---------------------------------
19496 -- Copy_Elist_With_Replacement --
19497 ---------------------------------
19499 function Copy_Elist_With_Replacement (List : Elist_Id) return Elist_Id is
19500 Elmt : Elmt_Id;
19501 Result : Elist_Id;
19503 begin
19504 -- Copy the contents of the old list. Note that the list itself may
19505 -- be empty, in which case the routine returns a new empty list. This
19506 -- avoids sharing lists between subtrees. The element of an entity
19507 -- list could be an entity or a node, hence the invocation of routine
19508 -- Copy_Any_Node_With_Replacement.
19510 if Present (List) then
19511 Result := New_Elmt_List;
19513 Elmt := First_Elmt (List);
19514 while Present (Elmt) loop
19515 Append_Elmt
19516 (Copy_Any_Node_With_Replacement (Node (Elmt)), Result);
19518 Next_Elmt (Elmt);
19519 end loop;
19521 -- Otherwise the list does not exist
19523 else
19524 Result := No_Elist;
19525 end if;
19527 return Result;
19528 end Copy_Elist_With_Replacement;
19530 ---------------------------------
19531 -- Copy_Field_With_Replacement --
19532 ---------------------------------
19534 function Copy_Field_With_Replacement
19535 (Field : Union_Id;
19536 Old_Par : Node_Id := Empty;
19537 New_Par : Node_Id := Empty;
19538 Semantic : Boolean := False) return Union_Id
19540 begin
19541 -- The field is empty
19543 if Field = Union_Id (Empty) then
19544 return Field;
19546 -- The field is an entity/itype/node
19548 elsif Field in Node_Range then
19549 declare
19550 Old_N : constant Node_Id := Node_Id (Field);
19551 Syntactic : constant Boolean := Parent (Old_N) = Old_Par;
19553 New_N : Node_Id;
19555 begin
19556 -- The field is an entity/itype
19558 if Nkind (Old_N) in N_Entity then
19560 -- An entity/itype is always replicated
19562 New_N := Corresponding_Entity (Old_N);
19564 -- Update the parent pointer when the entity is a syntactic
19565 -- field. Note that itypes do not have parent pointers.
19567 if Syntactic and then New_N /= Old_N then
19568 Set_Parent (New_N, New_Par);
19569 end if;
19571 -- The field is a node
19573 else
19574 -- A node is replicated when it is either a syntactic field
19575 -- or when the caller treats it as a semantic attribute.
19577 if Syntactic or else Semantic then
19578 New_N := Copy_Node_With_Replacement (Old_N);
19580 -- Update the parent pointer when the node is a syntactic
19581 -- field.
19583 if Syntactic and then New_N /= Old_N then
19584 Set_Parent (New_N, New_Par);
19585 end if;
19587 -- Otherwise the node is returned unchanged
19589 else
19590 New_N := Old_N;
19591 end if;
19592 end if;
19594 return Union_Id (New_N);
19595 end;
19597 -- The field is an entity list
19599 elsif Field in Elist_Range then
19600 return Union_Id (Copy_Elist_With_Replacement (Elist_Id (Field)));
19602 -- The field is a syntactic list
19604 elsif Field in List_Range then
19605 declare
19606 Old_List : constant List_Id := List_Id (Field);
19607 Syntactic : constant Boolean := Parent (Old_List) = Old_Par;
19609 New_List : List_Id;
19611 begin
19612 -- A list is replicated when it is either a syntactic field or
19613 -- when the caller treats it as a semantic attribute.
19615 if Syntactic or else Semantic then
19616 New_List := Copy_List_With_Replacement (Old_List);
19618 -- Update the parent pointer when the list is a syntactic
19619 -- field.
19621 if Syntactic and then New_List /= Old_List then
19622 Set_Parent (New_List, New_Par);
19623 end if;
19625 -- Otherwise the list is returned unchanged
19627 else
19628 New_List := Old_List;
19629 end if;
19631 return Union_Id (New_List);
19632 end;
19634 -- Otherwise the field denotes an attribute that does not need to be
19635 -- replicated (Chars, literals, etc).
19637 else
19638 return Field;
19639 end if;
19640 end Copy_Field_With_Replacement;
19642 --------------------------------
19643 -- Copy_List_With_Replacement --
19644 --------------------------------
19646 function Copy_List_With_Replacement (List : List_Id) return List_Id is
19647 Elmt : Node_Id;
19648 Result : List_Id;
19650 begin
19651 -- Copy the contents of the old list. Note that the list itself may
19652 -- be empty, in which case the routine returns a new empty list. This
19653 -- avoids sharing lists between subtrees. The element of a syntactic
19654 -- list is always a node, never an entity or itype, hence the call to
19655 -- routine Copy_Node_With_Replacement.
19657 if Present (List) then
19658 Result := New_List;
19660 Elmt := First (List);
19661 while Present (Elmt) loop
19662 Append (Copy_Node_With_Replacement (Elmt), Result);
19664 Next (Elmt);
19665 end loop;
19667 -- Otherwise the list does not exist
19669 else
19670 Result := No_List;
19671 end if;
19673 return Result;
19674 end Copy_List_With_Replacement;
19676 --------------------------------
19677 -- Copy_Node_With_Replacement --
19678 --------------------------------
19680 function Copy_Node_With_Replacement (N : Node_Id) return Node_Id is
19681 Result : Node_Id;
19683 begin
19684 -- Assume that the node must be returned unchanged
19686 Result := N;
19688 if N > Empty_Or_Error then
19689 pragma Assert (Nkind (N) not in N_Entity);
19691 Result := New_Copy (N);
19693 Set_Field1 (Result,
19694 Copy_Field_With_Replacement
19695 (Field => Field1 (Result),
19696 Old_Par => N,
19697 New_Par => Result));
19699 Set_Field2 (Result,
19700 Copy_Field_With_Replacement
19701 (Field => Field2 (Result),
19702 Old_Par => N,
19703 New_Par => Result));
19705 Set_Field3 (Result,
19706 Copy_Field_With_Replacement
19707 (Field => Field3 (Result),
19708 Old_Par => N,
19709 New_Par => Result));
19711 Set_Field4 (Result,
19712 Copy_Field_With_Replacement
19713 (Field => Field4 (Result),
19714 Old_Par => N,
19715 New_Par => Result));
19717 Set_Field5 (Result,
19718 Copy_Field_With_Replacement
19719 (Field => Field5 (Result),
19720 Old_Par => N,
19721 New_Par => Result));
19723 -- Update the Comes_From_Source and Sloc attributes of the node
19724 -- in case the caller has supplied new values.
19726 Update_CFS_Sloc (Result);
19728 -- Update the Associated_Node_For_Itype attribute of all itypes
19729 -- created during Phase 1 whose associated node is N. As a result
19730 -- the Associated_Node_For_Itype refers to the replicated node.
19731 -- No action needs to be taken when the Associated_Node_For_Itype
19732 -- refers to an entity because this was already handled during
19733 -- Phase 1, in Visit_Itype.
19735 Update_Pending_Itypes
19736 (Old_Assoc => N,
19737 New_Assoc => Result);
19739 -- Update the First/Next_Named_Association chain for a replicated
19740 -- call.
19742 if Nkind_In (N, N_Entry_Call_Statement,
19743 N_Function_Call,
19744 N_Procedure_Call_Statement)
19745 then
19746 Update_Named_Associations
19747 (Old_Call => N,
19748 New_Call => Result);
19750 -- Update the Renamed_Object attribute of a replicated object
19751 -- declaration.
19753 elsif Nkind (N) = N_Object_Renaming_Declaration then
19754 Set_Renamed_Object (Defining_Entity (Result), Name (Result));
19756 -- Update the First_Real_Statement attribute of a replicated
19757 -- handled sequence of statements.
19759 elsif Nkind (N) = N_Handled_Sequence_Of_Statements then
19760 Update_First_Real_Statement
19761 (Old_HSS => N,
19762 New_HSS => Result);
19763 end if;
19764 end if;
19766 return Result;
19767 end Copy_Node_With_Replacement;
19769 --------------------------
19770 -- Corresponding_Entity --
19771 --------------------------
19773 function Corresponding_Entity (Id : Entity_Id) return Entity_Id is
19774 New_Id : Entity_Id;
19775 Result : Entity_Id;
19777 begin
19778 -- Assume that the entity must be returned unchanged
19780 Result := Id;
19782 if Id > Empty_Or_Error then
19783 pragma Assert (Nkind (Id) in N_Entity);
19785 -- Determine whether the entity has a corresponding new entity
19786 -- generated during Phase 1 and if it does, use it.
19788 if NCT_Tables_In_Use then
19789 New_Id := NCT_New_Entities.Get (Id);
19791 if Present (New_Id) then
19792 Result := New_Id;
19793 end if;
19794 end if;
19795 end if;
19797 return Result;
19798 end Corresponding_Entity;
19800 -------------------
19801 -- In_Entity_Map --
19802 -------------------
19804 function In_Entity_Map
19805 (Id : Entity_Id;
19806 Entity_Map : Elist_Id) return Boolean
19808 Elmt : Elmt_Id;
19809 Old_Id : Entity_Id;
19811 begin
19812 -- The entity map contains pairs (Old_Id, New_Id). The advancement
19813 -- step always skips the New_Id portion of the pair.
19815 if Present (Entity_Map) then
19816 Elmt := First_Elmt (Entity_Map);
19817 while Present (Elmt) loop
19818 Old_Id := Node (Elmt);
19820 if Old_Id = Id then
19821 return True;
19822 end if;
19824 Next_Elmt (Elmt);
19825 Next_Elmt (Elmt);
19826 end loop;
19827 end if;
19829 return False;
19830 end In_Entity_Map;
19832 ---------------------
19833 -- Update_CFS_Sloc --
19834 ---------------------
19836 procedure Update_CFS_Sloc (N : Node_Or_Entity_Id) is
19837 begin
19838 -- A new source location defaults the Comes_From_Source attribute
19840 if New_Sloc /= No_Location then
19841 Set_Comes_From_Source (N, Default_Node.Comes_From_Source);
19842 Set_Sloc (N, New_Sloc);
19843 end if;
19844 end Update_CFS_Sloc;
19846 ---------------------------------
19847 -- Update_First_Real_Statement --
19848 ---------------------------------
19850 procedure Update_First_Real_Statement
19851 (Old_HSS : Node_Id;
19852 New_HSS : Node_Id)
19854 Old_First_Stmt : constant Node_Id := First_Real_Statement (Old_HSS);
19856 New_Stmt : Node_Id;
19857 Old_Stmt : Node_Id;
19859 begin
19860 -- Recreate the First_Real_Statement attribute of a handled sequence
19861 -- of statements by traversing the statement lists of both sequences
19862 -- in parallel.
19864 if Present (Old_First_Stmt) then
19865 New_Stmt := First (Statements (New_HSS));
19866 Old_Stmt := First (Statements (Old_HSS));
19867 while Present (Old_Stmt) and then Old_Stmt /= Old_First_Stmt loop
19868 Next (New_Stmt);
19869 Next (Old_Stmt);
19870 end loop;
19872 pragma Assert (Present (New_Stmt));
19873 pragma Assert (Present (Old_Stmt));
19875 Set_First_Real_Statement (New_HSS, New_Stmt);
19876 end if;
19877 end Update_First_Real_Statement;
19879 -------------------------------
19880 -- Update_Named_Associations --
19881 -------------------------------
19883 procedure Update_Named_Associations
19884 (Old_Call : Node_Id;
19885 New_Call : Node_Id)
19887 New_Act : Node_Id;
19888 New_Next : Node_Id;
19889 Old_Act : Node_Id;
19890 Old_Next : Node_Id;
19892 begin
19893 -- Recreate the First/Next_Named_Actual chain of a call by traversing
19894 -- the chains of both the old and new calls in parallel.
19896 New_Act := First (Parameter_Associations (New_Call));
19897 Old_Act := First (Parameter_Associations (Old_Call));
19898 while Present (Old_Act) loop
19899 if Nkind (Old_Act) = N_Parameter_Association
19900 and then Present (Next_Named_Actual (Old_Act))
19901 then
19902 if First_Named_Actual (Old_Call) =
19903 Explicit_Actual_Parameter (Old_Act)
19904 then
19905 Set_First_Named_Actual (New_Call,
19906 Explicit_Actual_Parameter (New_Act));
19907 end if;
19909 -- Scan the actual parameter list to find the next suitable
19910 -- named actual. Note that the list may be out of order.
19912 New_Next := First (Parameter_Associations (New_Call));
19913 Old_Next := First (Parameter_Associations (Old_Call));
19914 while Nkind (Old_Next) /= N_Parameter_Association
19915 or else Explicit_Actual_Parameter (Old_Next) /=
19916 Next_Named_Actual (Old_Act)
19917 loop
19918 Next (New_Next);
19919 Next (Old_Next);
19920 end loop;
19922 Set_Next_Named_Actual (New_Act,
19923 Explicit_Actual_Parameter (New_Next));
19924 end if;
19926 Next (New_Act);
19927 Next (Old_Act);
19928 end loop;
19929 end Update_Named_Associations;
19931 -------------------------
19932 -- Update_New_Entities --
19933 -------------------------
19935 procedure Update_New_Entities (Entity_Map : Elist_Id) is
19936 New_Id : Entity_Id := Empty;
19937 Old_Id : Entity_Id := Empty;
19939 begin
19940 if NCT_Tables_In_Use then
19941 NCT_New_Entities.Get_First (Old_Id, New_Id);
19943 -- Update the semantic fields of all new entities created during
19944 -- Phase 1 which were not supplied via an entity map.
19945 -- ??? Is there a better way of distinguishing those?
19947 while Present (Old_Id) and then Present (New_Id) loop
19948 if not (Present (Entity_Map)
19949 and then In_Entity_Map (Old_Id, Entity_Map))
19950 then
19951 Update_Semantic_Fields (New_Id);
19952 end if;
19954 NCT_New_Entities.Get_Next (Old_Id, New_Id);
19955 end loop;
19956 end if;
19957 end Update_New_Entities;
19959 ---------------------------
19960 -- Update_Pending_Itypes --
19961 ---------------------------
19963 procedure Update_Pending_Itypes
19964 (Old_Assoc : Node_Id;
19965 New_Assoc : Node_Id)
19967 Item : Elmt_Id;
19968 Itypes : Elist_Id;
19970 begin
19971 if NCT_Tables_In_Use then
19972 Itypes := NCT_Pending_Itypes.Get (Old_Assoc);
19974 -- Update the Associated_Node_For_Itype attribute for all itypes
19975 -- which originally refer to Old_Assoc to designate New_Assoc.
19977 if Present (Itypes) then
19978 Item := First_Elmt (Itypes);
19979 while Present (Item) loop
19980 Set_Associated_Node_For_Itype (Node (Item), New_Assoc);
19982 Next_Elmt (Item);
19983 end loop;
19984 end if;
19985 end if;
19986 end Update_Pending_Itypes;
19988 ----------------------------
19989 -- Update_Semantic_Fields --
19990 ----------------------------
19992 procedure Update_Semantic_Fields (Id : Entity_Id) is
19993 begin
19994 -- Discriminant_Constraint
19996 if Is_Type (Id) and then Has_Discriminants (Base_Type (Id)) then
19997 Set_Discriminant_Constraint (Id, Elist_Id (
19998 Copy_Field_With_Replacement
19999 (Field => Union_Id (Discriminant_Constraint (Id)),
20000 Semantic => True)));
20001 end if;
20003 -- Etype
20005 Set_Etype (Id, Node_Id (
20006 Copy_Field_With_Replacement
20007 (Field => Union_Id (Etype (Id)),
20008 Semantic => True)));
20010 -- First_Index
20011 -- Packed_Array_Impl_Type
20013 if Is_Array_Type (Id) then
20014 if Present (First_Index (Id)) then
20015 Set_First_Index (Id, First (List_Id (
20016 Copy_Field_With_Replacement
20017 (Field => Union_Id (List_Containing (First_Index (Id))),
20018 Semantic => True))));
20019 end if;
20021 if Is_Packed (Id) then
20022 Set_Packed_Array_Impl_Type (Id, Node_Id (
20023 Copy_Field_With_Replacement
20024 (Field => Union_Id (Packed_Array_Impl_Type (Id)),
20025 Semantic => True)));
20026 end if;
20027 end if;
20029 -- Prev_Entity
20031 Set_Prev_Entity (Id, Node_Id (
20032 Copy_Field_With_Replacement
20033 (Field => Union_Id (Prev_Entity (Id)),
20034 Semantic => True)));
20036 -- Next_Entity
20038 Set_Next_Entity (Id, Node_Id (
20039 Copy_Field_With_Replacement
20040 (Field => Union_Id (Next_Entity (Id)),
20041 Semantic => True)));
20043 -- Scalar_Range
20045 if Is_Discrete_Type (Id) then
20046 Set_Scalar_Range (Id, Node_Id (
20047 Copy_Field_With_Replacement
20048 (Field => Union_Id (Scalar_Range (Id)),
20049 Semantic => True)));
20050 end if;
20052 -- Scope
20054 -- Update the scope when the caller specified an explicit one
20056 if Present (New_Scope) then
20057 Set_Scope (Id, New_Scope);
20058 else
20059 Set_Scope (Id, Node_Id (
20060 Copy_Field_With_Replacement
20061 (Field => Union_Id (Scope (Id)),
20062 Semantic => True)));
20063 end if;
20064 end Update_Semantic_Fields;
20066 --------------------
20067 -- Visit_Any_Node --
20068 --------------------
20070 procedure Visit_Any_Node (N : Node_Or_Entity_Id) is
20071 begin
20072 if Nkind (N) in N_Entity then
20073 if Is_Itype (N) then
20074 Visit_Itype (N);
20075 else
20076 Visit_Entity (N);
20077 end if;
20078 else
20079 Visit_Node (N);
20080 end if;
20081 end Visit_Any_Node;
20083 -----------------
20084 -- Visit_Elist --
20085 -----------------
20087 procedure Visit_Elist (List : Elist_Id) is
20088 Elmt : Elmt_Id;
20090 begin
20091 -- The element of an entity list could be an entity, itype, or a
20092 -- node, hence the call to Visit_Any_Node.
20094 if Present (List) then
20095 Elmt := First_Elmt (List);
20096 while Present (Elmt) loop
20097 Visit_Any_Node (Node (Elmt));
20099 Next_Elmt (Elmt);
20100 end loop;
20101 end if;
20102 end Visit_Elist;
20104 ------------------
20105 -- Visit_Entity --
20106 ------------------
20108 procedure Visit_Entity (Id : Entity_Id) is
20109 New_Id : Entity_Id;
20111 begin
20112 pragma Assert (Nkind (Id) in N_Entity);
20113 pragma Assert (not Is_Itype (Id));
20115 -- Nothing to do if the entity is not defined in the Actions list of
20116 -- an N_Expression_With_Actions node.
20118 if EWA_Level = 0 then
20119 return;
20121 -- Nothing to do if the entity is defined within a scoping construct
20122 -- of an N_Expression_With_Actions node.
20124 elsif EWA_Inner_Scope_Level > 0 then
20125 return;
20127 -- Nothing to do if the entity is not an object or a type. Relaxing
20128 -- this restriction leads to a performance penalty.
20130 elsif not Ekind_In (Id, E_Constant, E_Variable)
20131 and then not Is_Type (Id)
20132 then
20133 return;
20135 -- Nothing to do if the entity was already visited
20137 elsif NCT_Tables_In_Use
20138 and then Present (NCT_New_Entities.Get (Id))
20139 then
20140 return;
20142 -- Nothing to do if the declaration node of the entity is not within
20143 -- the subtree being replicated.
20145 elsif not In_Subtree
20146 (N => Declaration_Node (Id),
20147 Root => Source)
20148 then
20149 return;
20150 end if;
20152 -- Create a new entity by directly copying the old entity. This
20153 -- action causes all attributes of the old entity to be inherited.
20155 New_Id := New_Copy (Id);
20157 -- Create a new name for the new entity because the back end needs
20158 -- distinct names for debugging purposes.
20160 Set_Chars (New_Id, New_Internal_Name ('T'));
20162 -- Update the Comes_From_Source and Sloc attributes of the entity in
20163 -- case the caller has supplied new values.
20165 Update_CFS_Sloc (New_Id);
20167 -- Establish the following mapping within table NCT_New_Entities:
20169 -- Id -> New_Id
20171 Add_New_Entity (Id, New_Id);
20173 -- Deal with the semantic fields of entities. The fields are visited
20174 -- because they may mention entities which reside within the subtree
20175 -- being copied.
20177 Visit_Semantic_Fields (Id);
20178 end Visit_Entity;
20180 -----------------
20181 -- Visit_Field --
20182 -----------------
20184 procedure Visit_Field
20185 (Field : Union_Id;
20186 Par_Nod : Node_Id := Empty;
20187 Semantic : Boolean := False)
20189 begin
20190 -- The field is empty
20192 if Field = Union_Id (Empty) then
20193 return;
20195 -- The field is an entity/itype/node
20197 elsif Field in Node_Range then
20198 declare
20199 N : constant Node_Id := Node_Id (Field);
20201 begin
20202 -- The field is an entity/itype
20204 if Nkind (N) in N_Entity then
20206 -- Itypes are always visited
20208 if Is_Itype (N) then
20209 Visit_Itype (N);
20211 -- An entity is visited when it is either a syntactic field
20212 -- or when the caller treats it as a semantic attribute.
20214 elsif Parent (N) = Par_Nod or else Semantic then
20215 Visit_Entity (N);
20216 end if;
20218 -- The field is a node
20220 else
20221 -- A node is visited when it is either a syntactic field or
20222 -- when the caller treats it as a semantic attribute.
20224 if Parent (N) = Par_Nod or else Semantic then
20225 Visit_Node (N);
20226 end if;
20227 end if;
20228 end;
20230 -- The field is an entity list
20232 elsif Field in Elist_Range then
20233 Visit_Elist (Elist_Id (Field));
20235 -- The field is a syntax list
20237 elsif Field in List_Range then
20238 declare
20239 List : constant List_Id := List_Id (Field);
20241 begin
20242 -- A syntax list is visited when it is either a syntactic field
20243 -- or when the caller treats it as a semantic attribute.
20245 if Parent (List) = Par_Nod or else Semantic then
20246 Visit_List (List);
20247 end if;
20248 end;
20250 -- Otherwise the field denotes information which does not need to be
20251 -- visited (chars, literals, etc.).
20253 else
20254 null;
20255 end if;
20256 end Visit_Field;
20258 -----------------
20259 -- Visit_Itype --
20260 -----------------
20262 procedure Visit_Itype (Itype : Entity_Id) is
20263 New_Assoc : Node_Id;
20264 New_Itype : Entity_Id;
20265 Old_Assoc : Node_Id;
20267 begin
20268 pragma Assert (Nkind (Itype) in N_Entity);
20269 pragma Assert (Is_Itype (Itype));
20271 -- Itypes that describe the designated type of access to subprograms
20272 -- have the structure of subprogram declarations, with signatures,
20273 -- etc. Either we duplicate the signatures completely, or choose to
20274 -- share such itypes, which is fine because their elaboration will
20275 -- have no side effects.
20277 if Ekind (Itype) = E_Subprogram_Type then
20278 return;
20280 -- Nothing to do if the itype was already visited
20282 elsif NCT_Tables_In_Use
20283 and then Present (NCT_New_Entities.Get (Itype))
20284 then
20285 return;
20287 -- Nothing to do if the associated node of the itype is not within
20288 -- the subtree being replicated.
20290 elsif not In_Subtree
20291 (N => Associated_Node_For_Itype (Itype),
20292 Root => Source)
20293 then
20294 return;
20295 end if;
20297 -- Create a new itype by directly copying the old itype. This action
20298 -- causes all attributes of the old itype to be inherited.
20300 New_Itype := New_Copy (Itype);
20302 -- Create a new name for the new itype because the back end requires
20303 -- distinct names for debugging purposes.
20305 Set_Chars (New_Itype, New_Internal_Name ('T'));
20307 -- Update the Comes_From_Source and Sloc attributes of the itype in
20308 -- case the caller has supplied new values.
20310 Update_CFS_Sloc (New_Itype);
20312 -- Establish the following mapping within table NCT_New_Entities:
20314 -- Itype -> New_Itype
20316 Add_New_Entity (Itype, New_Itype);
20318 -- The new itype must be unfrozen because the resulting subtree may
20319 -- be inserted anywhere and cause an earlier or later freezing.
20321 if Present (Freeze_Node (New_Itype)) then
20322 Set_Freeze_Node (New_Itype, Empty);
20323 Set_Is_Frozen (New_Itype, False);
20324 end if;
20326 -- If a record subtype is simply copied, the entity list will be
20327 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
20328 -- ??? What does this do?
20330 if Ekind_In (Itype, E_Class_Wide_Subtype, E_Record_Subtype) then
20331 Set_Cloned_Subtype (New_Itype, Itype);
20332 end if;
20334 -- The associated node may denote an entity, in which case it may
20335 -- already have a new corresponding entity created during a prior
20336 -- call to Visit_Entity or Visit_Itype for the same subtree.
20338 -- Given
20339 -- Old_Assoc ---------> New_Assoc
20341 -- Created by Visit_Itype
20342 -- Itype -------------> New_Itype
20343 -- ANFI = Old_Assoc ANFI = Old_Assoc < must be updated
20345 -- In the example above, Old_Assoc is an arbitrary entity that was
20346 -- already visited for the same subtree and has a corresponding new
20347 -- entity New_Assoc. Old_Assoc was inherited by New_Itype by virtue
20348 -- of copying entities, however it must be updated to New_Assoc.
20350 Old_Assoc := Associated_Node_For_Itype (Itype);
20352 if Nkind (Old_Assoc) in N_Entity then
20353 if NCT_Tables_In_Use then
20354 New_Assoc := NCT_New_Entities.Get (Old_Assoc);
20356 if Present (New_Assoc) then
20357 Set_Associated_Node_For_Itype (New_Itype, New_Assoc);
20358 end if;
20359 end if;
20361 -- Otherwise the associated node denotes a node. Postpone the update
20362 -- until Phase 2 when the node is replicated. Establish the following
20363 -- mapping within table NCT_Pending_Itypes:
20365 -- Old_Assoc -> (New_Type, ...)
20367 else
20368 Add_Pending_Itype (Old_Assoc, New_Itype);
20369 end if;
20371 -- Deal with the semantic fields of itypes. The fields are visited
20372 -- because they may mention entities that reside within the subtree
20373 -- being copied.
20375 Visit_Semantic_Fields (Itype);
20376 end Visit_Itype;
20378 ----------------
20379 -- Visit_List --
20380 ----------------
20382 procedure Visit_List (List : List_Id) is
20383 Elmt : Node_Id;
20385 begin
20386 -- Note that the element of a syntactic list is always a node, never
20387 -- an entity or itype, hence the call to Visit_Node.
20389 if Present (List) then
20390 Elmt := First (List);
20391 while Present (Elmt) loop
20392 Visit_Node (Elmt);
20394 Next (Elmt);
20395 end loop;
20396 end if;
20397 end Visit_List;
20399 ----------------
20400 -- Visit_Node --
20401 ----------------
20403 procedure Visit_Node (N : Node_Or_Entity_Id) is
20404 begin
20405 pragma Assert (Nkind (N) not in N_Entity);
20407 if Nkind (N) = N_Expression_With_Actions then
20408 EWA_Level := EWA_Level + 1;
20410 elsif EWA_Level > 0
20411 and then Nkind_In (N, N_Block_Statement,
20412 N_Subprogram_Body,
20413 N_Subprogram_Declaration)
20414 then
20415 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level + 1;
20416 end if;
20418 Visit_Field
20419 (Field => Field1 (N),
20420 Par_Nod => N);
20422 Visit_Field
20423 (Field => Field2 (N),
20424 Par_Nod => N);
20426 Visit_Field
20427 (Field => Field3 (N),
20428 Par_Nod => N);
20430 Visit_Field
20431 (Field => Field4 (N),
20432 Par_Nod => N);
20434 Visit_Field
20435 (Field => Field5 (N),
20436 Par_Nod => N);
20438 if EWA_Level > 0
20439 and then Nkind_In (N, N_Block_Statement,
20440 N_Subprogram_Body,
20441 N_Subprogram_Declaration)
20442 then
20443 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level - 1;
20445 elsif Nkind (N) = N_Expression_With_Actions then
20446 EWA_Level := EWA_Level - 1;
20447 end if;
20448 end Visit_Node;
20450 ---------------------------
20451 -- Visit_Semantic_Fields --
20452 ---------------------------
20454 procedure Visit_Semantic_Fields (Id : Entity_Id) is
20455 begin
20456 pragma Assert (Nkind (Id) in N_Entity);
20458 -- Discriminant_Constraint
20460 if Is_Type (Id) and then Has_Discriminants (Base_Type (Id)) then
20461 Visit_Field
20462 (Field => Union_Id (Discriminant_Constraint (Id)),
20463 Semantic => True);
20464 end if;
20466 -- Etype
20468 Visit_Field
20469 (Field => Union_Id (Etype (Id)),
20470 Semantic => True);
20472 -- First_Index
20473 -- Packed_Array_Impl_Type
20475 if Is_Array_Type (Id) then
20476 if Present (First_Index (Id)) then
20477 Visit_Field
20478 (Field => Union_Id (List_Containing (First_Index (Id))),
20479 Semantic => True);
20480 end if;
20482 if Is_Packed (Id) then
20483 Visit_Field
20484 (Field => Union_Id (Packed_Array_Impl_Type (Id)),
20485 Semantic => True);
20486 end if;
20487 end if;
20489 -- Scalar_Range
20491 if Is_Discrete_Type (Id) then
20492 Visit_Field
20493 (Field => Union_Id (Scalar_Range (Id)),
20494 Semantic => True);
20495 end if;
20496 end Visit_Semantic_Fields;
20498 -- Start of processing for New_Copy_Tree
20500 begin
20501 -- Routine New_Copy_Tree performs a deep copy of a subtree by creating
20502 -- shallow copies for each node within, and then updating the child and
20503 -- parent pointers accordingly. This process is straightforward, however
20504 -- the routine must deal with the following complications:
20506 -- * Entities defined within N_Expression_With_Actions nodes must be
20507 -- replicated rather than shared to avoid introducing two identical
20508 -- symbols within the same scope. Note that no other expression can
20509 -- currently define entities.
20511 -- do
20512 -- Source_Low : ...;
20513 -- Source_High : ...;
20515 -- <reference to Source_Low>
20516 -- <reference to Source_High>
20517 -- in ... end;
20519 -- New_Copy_Tree handles this case by first creating new entities
20520 -- and then updating all existing references to point to these new
20521 -- entities.
20523 -- do
20524 -- New_Low : ...;
20525 -- New_High : ...;
20527 -- <reference to New_Low>
20528 -- <reference to New_High>
20529 -- in ... end;
20531 -- * Itypes defined within the subtree must be replicated to avoid any
20532 -- dependencies on invalid or inaccessible data.
20534 -- subtype Source_Itype is ... range Source_Low .. Source_High;
20536 -- New_Copy_Tree handles this case by first creating a new itype in
20537 -- the same fashion as entities, and then updating various relevant
20538 -- constraints.
20540 -- subtype New_Itype is ... range New_Low .. New_High;
20542 -- * The Associated_Node_For_Itype field of itypes must be updated to
20543 -- reference the proper replicated entity or node.
20545 -- * Semantic fields of entities such as Etype and Scope must be
20546 -- updated to reference the proper replicated entities.
20548 -- * Semantic fields of nodes such as First_Real_Statement must be
20549 -- updated to reference the proper replicated nodes.
20551 -- To meet all these demands, routine New_Copy_Tree is split into two
20552 -- phases.
20554 -- Phase 1 traverses the tree in order to locate entities and itypes
20555 -- defined within the subtree. New entities are generated and saved in
20556 -- table NCT_New_Entities. The semantic fields of all new entities and
20557 -- itypes are then updated accordingly.
20559 -- Phase 2 traverses the tree in order to replicate each node. Various
20560 -- semantic fields of nodes and entities are updated accordingly.
20562 -- Preparatory phase. Clear the contents of tables NCT_New_Entities and
20563 -- NCT_Pending_Itypes in case a previous call to New_Copy_Tree left some
20564 -- data inside.
20566 if NCT_Tables_In_Use then
20567 NCT_Tables_In_Use := False;
20569 NCT_New_Entities.Reset;
20570 NCT_Pending_Itypes.Reset;
20571 end if;
20573 -- Populate tables NCT_New_Entities and NCT_Pending_Itypes with data
20574 -- supplied by a linear entity map. The tables offer faster access to
20575 -- the same data.
20577 Build_NCT_Tables (Map);
20579 -- Execute Phase 1. Traverse the subtree and generate new entities for
20580 -- the following cases:
20582 -- * An entity defined within an N_Expression_With_Actions node
20584 -- * An itype referenced within the subtree where the associated node
20585 -- is also in the subtree.
20587 -- All new entities are accessible via table NCT_New_Entities, which
20588 -- contains mappings of the form:
20590 -- Old_Entity -> New_Entity
20591 -- Old_Itype -> New_Itype
20593 -- In addition, the associated nodes of all new itypes are mapped in
20594 -- table NCT_Pending_Itypes:
20596 -- Assoc_Nod -> (New_Itype1, New_Itype2, .., New_ItypeN)
20598 Visit_Any_Node (Source);
20600 -- Update the semantic attributes of all new entities generated during
20601 -- Phase 1 before starting Phase 2. The updates could be performed in
20602 -- routine Corresponding_Entity, however this may cause the same entity
20603 -- to be updated multiple times, effectively generating useless nodes.
20604 -- Keeping the updates separates from Phase 2 ensures that only one set
20605 -- of attributes is generated for an entity at any one time.
20607 Update_New_Entities (Map);
20609 -- Execute Phase 2. Replicate the source subtree one node at a time.
20610 -- The following transformations take place:
20612 -- * References to entities and itypes are updated to refer to the
20613 -- new entities and itypes generated during Phase 1.
20615 -- * All Associated_Node_For_Itype attributes of itypes are updated
20616 -- to refer to the new replicated Associated_Node_For_Itype.
20618 return Copy_Node_With_Replacement (Source);
20619 end New_Copy_Tree;
20621 -------------------------
20622 -- New_External_Entity --
20623 -------------------------
20625 function New_External_Entity
20626 (Kind : Entity_Kind;
20627 Scope_Id : Entity_Id;
20628 Sloc_Value : Source_Ptr;
20629 Related_Id : Entity_Id;
20630 Suffix : Character;
20631 Suffix_Index : Nat := 0;
20632 Prefix : Character := ' ') return Entity_Id
20634 N : constant Entity_Id :=
20635 Make_Defining_Identifier (Sloc_Value,
20636 New_External_Name
20637 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
20639 begin
20640 Set_Ekind (N, Kind);
20641 Set_Is_Internal (N, True);
20642 Append_Entity (N, Scope_Id);
20643 Set_Public_Status (N);
20645 if Kind in Type_Kind then
20646 Init_Size_Align (N);
20647 end if;
20649 return N;
20650 end New_External_Entity;
20652 -------------------------
20653 -- New_Internal_Entity --
20654 -------------------------
20656 function New_Internal_Entity
20657 (Kind : Entity_Kind;
20658 Scope_Id : Entity_Id;
20659 Sloc_Value : Source_Ptr;
20660 Id_Char : Character) return Entity_Id
20662 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
20664 begin
20665 Set_Ekind (N, Kind);
20666 Set_Is_Internal (N, True);
20667 Append_Entity (N, Scope_Id);
20669 if Kind in Type_Kind then
20670 Init_Size_Align (N);
20671 end if;
20673 return N;
20674 end New_Internal_Entity;
20676 -----------------
20677 -- Next_Actual --
20678 -----------------
20680 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
20681 N : Node_Id;
20683 begin
20684 -- If we are pointing at a positional parameter, it is a member of a
20685 -- node list (the list of parameters), and the next parameter is the
20686 -- next node on the list, unless we hit a parameter association, then
20687 -- we shift to using the chain whose head is the First_Named_Actual in
20688 -- the parent, and then is threaded using the Next_Named_Actual of the
20689 -- Parameter_Association. All this fiddling is because the original node
20690 -- list is in the textual call order, and what we need is the
20691 -- declaration order.
20693 if Is_List_Member (Actual_Id) then
20694 N := Next (Actual_Id);
20696 if Nkind (N) = N_Parameter_Association then
20698 -- In case of a build-in-place call, the call will no longer be a
20699 -- call; it will have been rewritten.
20701 if Nkind_In (Parent (Actual_Id), N_Entry_Call_Statement,
20702 N_Function_Call,
20703 N_Procedure_Call_Statement)
20704 then
20705 return First_Named_Actual (Parent (Actual_Id));
20706 else
20707 return Empty;
20708 end if;
20709 else
20710 return N;
20711 end if;
20713 else
20714 return Next_Named_Actual (Parent (Actual_Id));
20715 end if;
20716 end Next_Actual;
20718 procedure Next_Actual (Actual_Id : in out Node_Id) is
20719 begin
20720 Actual_Id := Next_Actual (Actual_Id);
20721 end Next_Actual;
20723 -----------------
20724 -- Next_Global --
20725 -----------------
20727 function Next_Global (Node : Node_Id) return Node_Id is
20728 begin
20729 -- The global item may either be in a list, or by itself, in which case
20730 -- there is no next global item with the same mode.
20732 if Is_List_Member (Node) then
20733 return Next (Node);
20734 else
20735 return Empty;
20736 end if;
20737 end Next_Global;
20739 procedure Next_Global (Node : in out Node_Id) is
20740 begin
20741 Node := Next_Global (Node);
20742 end Next_Global;
20744 ----------------------------------
20745 -- New_Requires_Transient_Scope --
20746 ----------------------------------
20748 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
20749 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
20750 -- This is called for untagged records and protected types, with
20751 -- nondefaulted discriminants. Returns True if the size of function
20752 -- results is known at the call site, False otherwise. Returns False
20753 -- if there is a variant part that depends on the discriminants of
20754 -- this type, or if there is an array constrained by the discriminants
20755 -- of this type. ???Currently, this is overly conservative (the array
20756 -- could be nested inside some other record that is constrained by
20757 -- nondiscriminants). That is, the recursive calls are too conservative.
20759 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean;
20760 -- Returns True if Typ is a nonlimited record with defaulted
20761 -- discriminants whose max size makes it unsuitable for allocating on
20762 -- the primary stack.
20764 ------------------------------
20765 -- Caller_Known_Size_Record --
20766 ------------------------------
20768 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
20769 pragma Assert (Typ = Underlying_Type (Typ));
20771 begin
20772 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
20773 return False;
20774 end if;
20776 declare
20777 Comp : Entity_Id;
20779 begin
20780 Comp := First_Entity (Typ);
20781 while Present (Comp) loop
20783 -- Only look at E_Component entities. No need to look at
20784 -- E_Discriminant entities, and we must ignore internal
20785 -- subtypes generated for constrained components.
20787 if Ekind (Comp) = E_Component then
20788 declare
20789 Comp_Type : constant Entity_Id :=
20790 Underlying_Type (Etype (Comp));
20792 begin
20793 if Is_Record_Type (Comp_Type)
20794 or else
20795 Is_Protected_Type (Comp_Type)
20796 then
20797 if not Caller_Known_Size_Record (Comp_Type) then
20798 return False;
20799 end if;
20801 elsif Is_Array_Type (Comp_Type) then
20802 if Size_Depends_On_Discriminant (Comp_Type) then
20803 return False;
20804 end if;
20805 end if;
20806 end;
20807 end if;
20809 Next_Entity (Comp);
20810 end loop;
20811 end;
20813 return True;
20814 end Caller_Known_Size_Record;
20816 ------------------------------
20817 -- Large_Max_Size_Mutable --
20818 ------------------------------
20820 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean is
20821 pragma Assert (Typ = Underlying_Type (Typ));
20823 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean;
20824 -- Returns true if the discrete type T has a large range
20826 ----------------------------
20827 -- Is_Large_Discrete_Type --
20828 ----------------------------
20830 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean is
20831 Threshold : constant Int := 16;
20832 -- Arbitrary threshold above which we consider it "large". We want
20833 -- a fairly large threshold, because these large types really
20834 -- shouldn't have default discriminants in the first place, in
20835 -- most cases.
20837 begin
20838 return UI_To_Int (RM_Size (T)) > Threshold;
20839 end Is_Large_Discrete_Type;
20841 -- Start of processing for Large_Max_Size_Mutable
20843 begin
20844 if Is_Record_Type (Typ)
20845 and then not Is_Limited_View (Typ)
20846 and then Has_Defaulted_Discriminants (Typ)
20847 then
20848 -- Loop through the components, looking for an array whose upper
20849 -- bound(s) depends on discriminants, where both the subtype of
20850 -- the discriminant and the index subtype are too large.
20852 declare
20853 Comp : Entity_Id;
20855 begin
20856 Comp := First_Entity (Typ);
20857 while Present (Comp) loop
20858 if Ekind (Comp) = E_Component then
20859 declare
20860 Comp_Type : constant Entity_Id :=
20861 Underlying_Type (Etype (Comp));
20863 Hi : Node_Id;
20864 Indx : Node_Id;
20865 Ityp : Entity_Id;
20867 begin
20868 if Is_Array_Type (Comp_Type) then
20869 Indx := First_Index (Comp_Type);
20871 while Present (Indx) loop
20872 Ityp := Etype (Indx);
20873 Hi := Type_High_Bound (Ityp);
20875 if Nkind (Hi) = N_Identifier
20876 and then Ekind (Entity (Hi)) = E_Discriminant
20877 and then Is_Large_Discrete_Type (Ityp)
20878 and then Is_Large_Discrete_Type
20879 (Etype (Entity (Hi)))
20880 then
20881 return True;
20882 end if;
20884 Next_Index (Indx);
20885 end loop;
20886 end if;
20887 end;
20888 end if;
20890 Next_Entity (Comp);
20891 end loop;
20892 end;
20893 end if;
20895 return False;
20896 end Large_Max_Size_Mutable;
20898 -- Local declarations
20900 Typ : constant Entity_Id := Underlying_Type (Id);
20902 -- Start of processing for New_Requires_Transient_Scope
20904 begin
20905 -- This is a private type which is not completed yet. This can only
20906 -- happen in a default expression (of a formal parameter or of a
20907 -- record component). Do not expand transient scope in this case.
20909 if No (Typ) then
20910 return False;
20912 -- Do not expand transient scope for non-existent procedure return or
20913 -- string literal types.
20915 elsif Typ = Standard_Void_Type
20916 or else Ekind (Typ) = E_String_Literal_Subtype
20917 then
20918 return False;
20920 -- If Typ is a generic formal incomplete type, then we want to look at
20921 -- the actual type.
20923 elsif Ekind (Typ) = E_Record_Subtype
20924 and then Present (Cloned_Subtype (Typ))
20925 then
20926 return New_Requires_Transient_Scope (Cloned_Subtype (Typ));
20928 -- Functions returning specific tagged types may dispatch on result, so
20929 -- their returned value is allocated on the secondary stack, even in the
20930 -- definite case. We must treat nondispatching functions the same way,
20931 -- because access-to-function types can point at both, so the calling
20932 -- conventions must be compatible. Is_Tagged_Type includes controlled
20933 -- types and class-wide types. Controlled type temporaries need
20934 -- finalization.
20936 -- ???It's not clear why we need to return noncontrolled types with
20937 -- controlled components on the secondary stack.
20939 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
20940 return True;
20942 -- Untagged definite subtypes are known size. This includes all
20943 -- elementary [sub]types. Tasks are known size even if they have
20944 -- discriminants. So we return False here, with one exception:
20945 -- For a type like:
20946 -- type T (Last : Natural := 0) is
20947 -- X : String (1 .. Last);
20948 -- end record;
20949 -- we return True. That's because for "P(F(...));", where F returns T,
20950 -- we don't know the size of the result at the call site, so if we
20951 -- allocated it on the primary stack, we would have to allocate the
20952 -- maximum size, which is way too big.
20954 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
20955 return Large_Max_Size_Mutable (Typ);
20957 -- Indefinite (discriminated) untagged record or protected type
20959 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
20960 return not Caller_Known_Size_Record (Typ);
20962 -- Unconstrained array
20964 else
20965 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
20966 return True;
20967 end if;
20968 end New_Requires_Transient_Scope;
20970 --------------------------
20971 -- No_Heap_Finalization --
20972 --------------------------
20974 function No_Heap_Finalization (Typ : Entity_Id) return Boolean is
20975 begin
20976 if Ekind_In (Typ, E_Access_Type, E_General_Access_Type)
20977 and then Is_Library_Level_Entity (Typ)
20978 then
20979 -- A global No_Heap_Finalization pragma applies to all library-level
20980 -- named access-to-object types.
20982 if Present (No_Heap_Finalization_Pragma) then
20983 return True;
20985 -- The library-level named access-to-object type itself is subject to
20986 -- pragma No_Heap_Finalization.
20988 elsif Present (Get_Pragma (Typ, Pragma_No_Heap_Finalization)) then
20989 return True;
20990 end if;
20991 end if;
20993 return False;
20994 end No_Heap_Finalization;
20996 -----------------------
20997 -- Normalize_Actuals --
20998 -----------------------
21000 -- Chain actuals according to formals of subprogram. If there are no named
21001 -- associations, the chain is simply the list of Parameter Associations,
21002 -- since the order is the same as the declaration order. If there are named
21003 -- associations, then the First_Named_Actual field in the N_Function_Call
21004 -- or N_Procedure_Call_Statement node points to the Parameter_Association
21005 -- node for the parameter that comes first in declaration order. The
21006 -- remaining named parameters are then chained in declaration order using
21007 -- Next_Named_Actual.
21009 -- This routine also verifies that the number of actuals is compatible with
21010 -- the number and default values of formals, but performs no type checking
21011 -- (type checking is done by the caller).
21013 -- If the matching succeeds, Success is set to True and the caller proceeds
21014 -- with type-checking. If the match is unsuccessful, then Success is set to
21015 -- False, and the caller attempts a different interpretation, if there is
21016 -- one.
21018 -- If the flag Report is on, the call is not overloaded, and a failure to
21019 -- match can be reported here, rather than in the caller.
21021 procedure Normalize_Actuals
21022 (N : Node_Id;
21023 S : Entity_Id;
21024 Report : Boolean;
21025 Success : out Boolean)
21027 Actuals : constant List_Id := Parameter_Associations (N);
21028 Actual : Node_Id := Empty;
21029 Formal : Entity_Id;
21030 Last : Node_Id := Empty;
21031 First_Named : Node_Id := Empty;
21032 Found : Boolean;
21034 Formals_To_Match : Integer := 0;
21035 Actuals_To_Match : Integer := 0;
21037 procedure Chain (A : Node_Id);
21038 -- Add named actual at the proper place in the list, using the
21039 -- Next_Named_Actual link.
21041 function Reporting return Boolean;
21042 -- Determines if an error is to be reported. To report an error, we
21043 -- need Report to be True, and also we do not report errors caused
21044 -- by calls to init procs that occur within other init procs. Such
21045 -- errors must always be cascaded errors, since if all the types are
21046 -- declared correctly, the compiler will certainly build decent calls.
21048 -----------
21049 -- Chain --
21050 -----------
21052 procedure Chain (A : Node_Id) is
21053 begin
21054 if No (Last) then
21056 -- Call node points to first actual in list
21058 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
21060 else
21061 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
21062 end if;
21064 Last := A;
21065 Set_Next_Named_Actual (Last, Empty);
21066 end Chain;
21068 ---------------
21069 -- Reporting --
21070 ---------------
21072 function Reporting return Boolean is
21073 begin
21074 if not Report then
21075 return False;
21077 elsif not Within_Init_Proc then
21078 return True;
21080 elsif Is_Init_Proc (Entity (Name (N))) then
21081 return False;
21083 else
21084 return True;
21085 end if;
21086 end Reporting;
21088 -- Start of processing for Normalize_Actuals
21090 begin
21091 if Is_Access_Type (S) then
21093 -- The name in the call is a function call that returns an access
21094 -- to subprogram. The designated type has the list of formals.
21096 Formal := First_Formal (Designated_Type (S));
21097 else
21098 Formal := First_Formal (S);
21099 end if;
21101 while Present (Formal) loop
21102 Formals_To_Match := Formals_To_Match + 1;
21103 Next_Formal (Formal);
21104 end loop;
21106 -- Find if there is a named association, and verify that no positional
21107 -- associations appear after named ones.
21109 if Present (Actuals) then
21110 Actual := First (Actuals);
21111 end if;
21113 while Present (Actual)
21114 and then Nkind (Actual) /= N_Parameter_Association
21115 loop
21116 Actuals_To_Match := Actuals_To_Match + 1;
21117 Next (Actual);
21118 end loop;
21120 if No (Actual) and Actuals_To_Match = Formals_To_Match then
21122 -- Most common case: positional notation, no defaults
21124 Success := True;
21125 return;
21127 elsif Actuals_To_Match > Formals_To_Match then
21129 -- Too many actuals: will not work
21131 if Reporting then
21132 if Is_Entity_Name (Name (N)) then
21133 Error_Msg_N ("too many arguments in call to&", Name (N));
21134 else
21135 Error_Msg_N ("too many arguments in call", N);
21136 end if;
21137 end if;
21139 Success := False;
21140 return;
21141 end if;
21143 First_Named := Actual;
21145 while Present (Actual) loop
21146 if Nkind (Actual) /= N_Parameter_Association then
21147 Error_Msg_N
21148 ("positional parameters not allowed after named ones", Actual);
21149 Success := False;
21150 return;
21152 else
21153 Actuals_To_Match := Actuals_To_Match + 1;
21154 end if;
21156 Next (Actual);
21157 end loop;
21159 if Present (Actuals) then
21160 Actual := First (Actuals);
21161 end if;
21163 Formal := First_Formal (S);
21164 while Present (Formal) loop
21166 -- Match the formals in order. If the corresponding actual is
21167 -- positional, nothing to do. Else scan the list of named actuals
21168 -- to find the one with the right name.
21170 if Present (Actual)
21171 and then Nkind (Actual) /= N_Parameter_Association
21172 then
21173 Next (Actual);
21174 Actuals_To_Match := Actuals_To_Match - 1;
21175 Formals_To_Match := Formals_To_Match - 1;
21177 else
21178 -- For named parameters, search the list of actuals to find
21179 -- one that matches the next formal name.
21181 Actual := First_Named;
21182 Found := False;
21183 while Present (Actual) loop
21184 if Chars (Selector_Name (Actual)) = Chars (Formal) then
21185 Found := True;
21186 Chain (Actual);
21187 Actuals_To_Match := Actuals_To_Match - 1;
21188 Formals_To_Match := Formals_To_Match - 1;
21189 exit;
21190 end if;
21192 Next (Actual);
21193 end loop;
21195 if not Found then
21196 if Ekind (Formal) /= E_In_Parameter
21197 or else No (Default_Value (Formal))
21198 then
21199 if Reporting then
21200 if (Comes_From_Source (S)
21201 or else Sloc (S) = Standard_Location)
21202 and then Is_Overloadable (S)
21203 then
21204 if No (Actuals)
21205 and then
21206 Nkind_In (Parent (N), N_Procedure_Call_Statement,
21207 N_Function_Call,
21208 N_Parameter_Association)
21209 and then Ekind (S) /= E_Function
21210 then
21211 Set_Etype (N, Etype (S));
21213 else
21214 Error_Msg_Name_1 := Chars (S);
21215 Error_Msg_Sloc := Sloc (S);
21216 Error_Msg_NE
21217 ("missing argument for parameter & "
21218 & "in call to % declared #", N, Formal);
21219 end if;
21221 elsif Is_Overloadable (S) then
21222 Error_Msg_Name_1 := Chars (S);
21224 -- Point to type derivation that generated the
21225 -- operation.
21227 Error_Msg_Sloc := Sloc (Parent (S));
21229 Error_Msg_NE
21230 ("missing argument for parameter & "
21231 & "in call to % (inherited) #", N, Formal);
21233 else
21234 Error_Msg_NE
21235 ("missing argument for parameter &", N, Formal);
21236 end if;
21237 end if;
21239 Success := False;
21240 return;
21242 else
21243 Formals_To_Match := Formals_To_Match - 1;
21244 end if;
21245 end if;
21246 end if;
21248 Next_Formal (Formal);
21249 end loop;
21251 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
21252 Success := True;
21253 return;
21255 else
21256 if Reporting then
21258 -- Find some superfluous named actual that did not get
21259 -- attached to the list of associations.
21261 Actual := First (Actuals);
21262 while Present (Actual) loop
21263 if Nkind (Actual) = N_Parameter_Association
21264 and then Actual /= Last
21265 and then No (Next_Named_Actual (Actual))
21266 then
21267 -- A validity check may introduce a copy of a call that
21268 -- includes an extra actual (for example for an unrelated
21269 -- accessibility check). Check that the extra actual matches
21270 -- some extra formal, which must exist already because
21271 -- subprogram must be frozen at this point.
21273 if Present (Extra_Formals (S))
21274 and then not Comes_From_Source (Actual)
21275 and then Nkind (Actual) = N_Parameter_Association
21276 and then Chars (Extra_Formals (S)) =
21277 Chars (Selector_Name (Actual))
21278 then
21279 null;
21280 else
21281 Error_Msg_N
21282 ("unmatched actual & in call", Selector_Name (Actual));
21283 exit;
21284 end if;
21285 end if;
21287 Next (Actual);
21288 end loop;
21289 end if;
21291 Success := False;
21292 return;
21293 end if;
21294 end Normalize_Actuals;
21296 --------------------------------
21297 -- Note_Possible_Modification --
21298 --------------------------------
21300 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
21301 Modification_Comes_From_Source : constant Boolean :=
21302 Comes_From_Source (Parent (N));
21304 Ent : Entity_Id;
21305 Exp : Node_Id;
21307 begin
21308 -- Loop to find referenced entity, if there is one
21310 Exp := N;
21311 loop
21312 Ent := Empty;
21314 if Is_Entity_Name (Exp) then
21315 Ent := Entity (Exp);
21317 -- If the entity is missing, it is an undeclared identifier,
21318 -- and there is nothing to annotate.
21320 if No (Ent) then
21321 return;
21322 end if;
21324 elsif Nkind (Exp) = N_Explicit_Dereference then
21325 declare
21326 P : constant Node_Id := Prefix (Exp);
21328 begin
21329 -- In formal verification mode, keep track of all reads and
21330 -- writes through explicit dereferences.
21332 if GNATprove_Mode then
21333 SPARK_Specific.Generate_Dereference (N, 'm');
21334 end if;
21336 if Nkind (P) = N_Selected_Component
21337 and then Present (Entry_Formal (Entity (Selector_Name (P))))
21338 then
21339 -- Case of a reference to an entry formal
21341 Ent := Entry_Formal (Entity (Selector_Name (P)));
21343 elsif Nkind (P) = N_Identifier
21344 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
21345 and then Present (Expression (Parent (Entity (P))))
21346 and then Nkind (Expression (Parent (Entity (P)))) =
21347 N_Reference
21348 then
21349 -- Case of a reference to a value on which side effects have
21350 -- been removed.
21352 Exp := Prefix (Expression (Parent (Entity (P))));
21353 goto Continue;
21355 else
21356 return;
21357 end if;
21358 end;
21360 elsif Nkind_In (Exp, N_Type_Conversion,
21361 N_Unchecked_Type_Conversion)
21362 then
21363 Exp := Expression (Exp);
21364 goto Continue;
21366 elsif Nkind_In (Exp, N_Slice,
21367 N_Indexed_Component,
21368 N_Selected_Component)
21369 then
21370 -- Special check, if the prefix is an access type, then return
21371 -- since we are modifying the thing pointed to, not the prefix.
21372 -- When we are expanding, most usually the prefix is replaced
21373 -- by an explicit dereference, and this test is not needed, but
21374 -- in some cases (notably -gnatc mode and generics) when we do
21375 -- not do full expansion, we need this special test.
21377 if Is_Access_Type (Etype (Prefix (Exp))) then
21378 return;
21380 -- Otherwise go to prefix and keep going
21382 else
21383 Exp := Prefix (Exp);
21384 goto Continue;
21385 end if;
21387 -- All other cases, not a modification
21389 else
21390 return;
21391 end if;
21393 -- Now look for entity being referenced
21395 if Present (Ent) then
21396 if Is_Object (Ent) then
21397 if Comes_From_Source (Exp)
21398 or else Modification_Comes_From_Source
21399 then
21400 -- Give warning if pragma unmodified is given and we are
21401 -- sure this is a modification.
21403 if Has_Pragma_Unmodified (Ent) and then Sure then
21405 -- Note that the entity may be present only as a result
21406 -- of pragma Unused.
21408 if Has_Pragma_Unused (Ent) then
21409 Error_Msg_NE ("??pragma Unused given for &!", N, Ent);
21410 else
21411 Error_Msg_NE
21412 ("??pragma Unmodified given for &!", N, Ent);
21413 end if;
21414 end if;
21416 Set_Never_Set_In_Source (Ent, False);
21417 end if;
21419 Set_Is_True_Constant (Ent, False);
21420 Set_Current_Value (Ent, Empty);
21421 Set_Is_Known_Null (Ent, False);
21423 if not Can_Never_Be_Null (Ent) then
21424 Set_Is_Known_Non_Null (Ent, False);
21425 end if;
21427 -- Follow renaming chain
21429 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
21430 and then Present (Renamed_Object (Ent))
21431 then
21432 Exp := Renamed_Object (Ent);
21434 -- If the entity is the loop variable in an iteration over
21435 -- a container, retrieve container expression to indicate
21436 -- possible modification.
21438 if Present (Related_Expression (Ent))
21439 and then Nkind (Parent (Related_Expression (Ent))) =
21440 N_Iterator_Specification
21441 then
21442 Exp := Original_Node (Related_Expression (Ent));
21443 end if;
21445 goto Continue;
21447 -- The expression may be the renaming of a subcomponent of an
21448 -- array or container. The assignment to the subcomponent is
21449 -- a modification of the container.
21451 elsif Comes_From_Source (Original_Node (Exp))
21452 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
21453 N_Indexed_Component)
21454 then
21455 Exp := Prefix (Original_Node (Exp));
21456 goto Continue;
21457 end if;
21459 -- Generate a reference only if the assignment comes from
21460 -- source. This excludes, for example, calls to a dispatching
21461 -- assignment operation when the left-hand side is tagged. In
21462 -- GNATprove mode, we need those references also on generated
21463 -- code, as these are used to compute the local effects of
21464 -- subprograms.
21466 if Modification_Comes_From_Source or GNATprove_Mode then
21467 Generate_Reference (Ent, Exp, 'm');
21469 -- If the target of the assignment is the bound variable
21470 -- in an iterator, indicate that the corresponding array
21471 -- or container is also modified.
21473 if Ada_Version >= Ada_2012
21474 and then Nkind (Parent (Ent)) = N_Iterator_Specification
21475 then
21476 declare
21477 Domain : constant Node_Id := Name (Parent (Ent));
21479 begin
21480 -- TBD : in the full version of the construct, the
21481 -- domain of iteration can be given by an expression.
21483 if Is_Entity_Name (Domain) then
21484 Generate_Reference (Entity (Domain), Exp, 'm');
21485 Set_Is_True_Constant (Entity (Domain), False);
21486 Set_Never_Set_In_Source (Entity (Domain), False);
21487 end if;
21488 end;
21489 end if;
21490 end if;
21491 end if;
21493 Kill_Checks (Ent);
21495 -- If we are sure this is a modification from source, and we know
21496 -- this modifies a constant, then give an appropriate warning.
21498 if Sure
21499 and then Modification_Comes_From_Source
21500 and then Overlays_Constant (Ent)
21501 and then Address_Clause_Overlay_Warnings
21502 then
21503 declare
21504 Addr : constant Node_Id := Address_Clause (Ent);
21505 O_Ent : Entity_Id;
21506 Off : Boolean;
21508 begin
21509 Find_Overlaid_Entity (Addr, O_Ent, Off);
21511 Error_Msg_Sloc := Sloc (Addr);
21512 Error_Msg_NE
21513 ("??constant& may be modified via address clause#",
21514 N, O_Ent);
21515 end;
21516 end if;
21518 return;
21519 end if;
21521 <<Continue>>
21522 null;
21523 end loop;
21524 end Note_Possible_Modification;
21526 -----------------
21527 -- Null_Status --
21528 -----------------
21530 function Null_Status (N : Node_Id) return Null_Status_Kind is
21531 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean;
21532 -- Determine whether definition Def carries a null exclusion
21534 function Null_Status_Of_Entity (Id : Entity_Id) return Null_Status_Kind;
21535 -- Determine the null status of arbitrary entity Id
21537 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind;
21538 -- Determine the null status of type Typ
21540 ---------------------------
21541 -- Is_Null_Excluding_Def --
21542 ---------------------------
21544 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean is
21545 begin
21546 return
21547 Nkind_In (Def, N_Access_Definition,
21548 N_Access_Function_Definition,
21549 N_Access_Procedure_Definition,
21550 N_Access_To_Object_Definition,
21551 N_Component_Definition,
21552 N_Derived_Type_Definition)
21553 and then Null_Exclusion_Present (Def);
21554 end Is_Null_Excluding_Def;
21556 ---------------------------
21557 -- Null_Status_Of_Entity --
21558 ---------------------------
21560 function Null_Status_Of_Entity
21561 (Id : Entity_Id) return Null_Status_Kind
21563 Decl : constant Node_Id := Declaration_Node (Id);
21564 Def : Node_Id;
21566 begin
21567 -- The value of an imported or exported entity may be set externally
21568 -- regardless of a null exclusion. As a result, the value cannot be
21569 -- determined statically.
21571 if Is_Imported (Id) or else Is_Exported (Id) then
21572 return Unknown;
21574 elsif Nkind_In (Decl, N_Component_Declaration,
21575 N_Discriminant_Specification,
21576 N_Formal_Object_Declaration,
21577 N_Object_Declaration,
21578 N_Object_Renaming_Declaration,
21579 N_Parameter_Specification)
21580 then
21581 -- A component declaration yields a non-null value when either
21582 -- its component definition or access definition carries a null
21583 -- exclusion.
21585 if Nkind (Decl) = N_Component_Declaration then
21586 Def := Component_Definition (Decl);
21588 if Is_Null_Excluding_Def (Def) then
21589 return Is_Non_Null;
21590 end if;
21592 Def := Access_Definition (Def);
21594 if Present (Def) and then Is_Null_Excluding_Def (Def) then
21595 return Is_Non_Null;
21596 end if;
21598 -- A formal object declaration yields a non-null value if its
21599 -- access definition carries a null exclusion. If the object is
21600 -- default initialized, then the value depends on the expression.
21602 elsif Nkind (Decl) = N_Formal_Object_Declaration then
21603 Def := Access_Definition (Decl);
21605 if Present (Def) and then Is_Null_Excluding_Def (Def) then
21606 return Is_Non_Null;
21607 end if;
21609 -- A constant may yield a null or non-null value depending on its
21610 -- initialization expression.
21612 elsif Ekind (Id) = E_Constant then
21613 return Null_Status (Constant_Value (Id));
21615 -- The construct yields a non-null value when it has a null
21616 -- exclusion.
21618 elsif Null_Exclusion_Present (Decl) then
21619 return Is_Non_Null;
21621 -- An object renaming declaration yields a non-null value if its
21622 -- access definition carries a null exclusion. Otherwise the value
21623 -- depends on the renamed name.
21625 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
21626 Def := Access_Definition (Decl);
21628 if Present (Def) and then Is_Null_Excluding_Def (Def) then
21629 return Is_Non_Null;
21631 else
21632 return Null_Status (Name (Decl));
21633 end if;
21634 end if;
21635 end if;
21637 -- At this point the declaration of the entity does not carry a null
21638 -- exclusion and lacks an initialization expression. Check the status
21639 -- of its type.
21641 return Null_Status_Of_Type (Etype (Id));
21642 end Null_Status_Of_Entity;
21644 -------------------------
21645 -- Null_Status_Of_Type --
21646 -------------------------
21648 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind is
21649 Curr : Entity_Id;
21650 Decl : Node_Id;
21652 begin
21653 -- Traverse the type chain looking for types with null exclusion
21655 Curr := Typ;
21656 while Present (Curr) and then Etype (Curr) /= Curr loop
21657 Decl := Parent (Curr);
21659 -- Guard against itypes which do not always have declarations. A
21660 -- type yields a non-null value if it carries a null exclusion.
21662 if Present (Decl) then
21663 if Nkind (Decl) = N_Full_Type_Declaration
21664 and then Is_Null_Excluding_Def (Type_Definition (Decl))
21665 then
21666 return Is_Non_Null;
21668 elsif Nkind (Decl) = N_Subtype_Declaration
21669 and then Null_Exclusion_Present (Decl)
21670 then
21671 return Is_Non_Null;
21672 end if;
21673 end if;
21675 Curr := Etype (Curr);
21676 end loop;
21678 -- The type chain does not contain any null excluding types
21680 return Unknown;
21681 end Null_Status_Of_Type;
21683 -- Start of processing for Null_Status
21685 begin
21686 -- An allocator always creates a non-null value
21688 if Nkind (N) = N_Allocator then
21689 return Is_Non_Null;
21691 -- Taking the 'Access of something yields a non-null value
21693 elsif Nkind (N) = N_Attribute_Reference
21694 and then Nam_In (Attribute_Name (N), Name_Access,
21695 Name_Unchecked_Access,
21696 Name_Unrestricted_Access)
21697 then
21698 return Is_Non_Null;
21700 -- "null" yields null
21702 elsif Nkind (N) = N_Null then
21703 return Is_Null;
21705 -- Check the status of the operand of a type conversion
21707 elsif Nkind (N) = N_Type_Conversion then
21708 return Null_Status (Expression (N));
21710 -- The input denotes a reference to an entity. Determine whether the
21711 -- entity or its type yields a null or non-null value.
21713 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
21714 return Null_Status_Of_Entity (Entity (N));
21715 end if;
21717 -- Otherwise it is not possible to determine the null status of the
21718 -- subexpression at compile time without resorting to simple flow
21719 -- analysis.
21721 return Unknown;
21722 end Null_Status;
21724 --------------------------------------
21725 -- Null_To_Null_Address_Convert_OK --
21726 --------------------------------------
21728 function Null_To_Null_Address_Convert_OK
21729 (N : Node_Id;
21730 Typ : Entity_Id := Empty) return Boolean
21732 begin
21733 if not Relaxed_RM_Semantics then
21734 return False;
21735 end if;
21737 if Nkind (N) = N_Null then
21738 return Present (Typ) and then Is_Descendant_Of_Address (Typ);
21740 elsif Nkind_In (N, N_Op_Eq, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt, N_Op_Ne)
21741 then
21742 declare
21743 L : constant Node_Id := Left_Opnd (N);
21744 R : constant Node_Id := Right_Opnd (N);
21746 begin
21747 -- We check the Etype of the complementary operand since the
21748 -- N_Null node is not decorated at this stage.
21750 return
21751 ((Nkind (L) = N_Null
21752 and then Is_Descendant_Of_Address (Etype (R)))
21753 or else
21754 (Nkind (R) = N_Null
21755 and then Is_Descendant_Of_Address (Etype (L))));
21756 end;
21757 end if;
21759 return False;
21760 end Null_To_Null_Address_Convert_OK;
21762 ---------------------------------
21763 -- Number_Of_Elements_In_Array --
21764 ---------------------------------
21766 function Number_Of_Elements_In_Array (T : Entity_Id) return Int is
21767 Indx : Node_Id;
21768 Typ : Entity_Id;
21769 Low : Node_Id;
21770 High : Node_Id;
21771 Num : Int := 1;
21773 begin
21774 pragma Assert (Is_Array_Type (T));
21776 Indx := First_Index (T);
21777 while Present (Indx) loop
21778 Typ := Underlying_Type (Etype (Indx));
21780 -- Never look at junk bounds of a generic type
21782 if Is_Generic_Type (Typ) then
21783 return 0;
21784 end if;
21786 -- Check the array bounds are known at compile time and return zero
21787 -- if they are not.
21789 Low := Type_Low_Bound (Typ);
21790 High := Type_High_Bound (Typ);
21792 if not Compile_Time_Known_Value (Low) then
21793 return 0;
21794 elsif not Compile_Time_Known_Value (High) then
21795 return 0;
21796 else
21797 Num :=
21798 Num * UI_To_Int ((Expr_Value (High) - Expr_Value (Low) + 1));
21799 end if;
21801 Next_Index (Indx);
21802 end loop;
21804 return Num;
21805 end Number_Of_Elements_In_Array;
21807 -------------------------
21808 -- Object_Access_Level --
21809 -------------------------
21811 -- Returns the static accessibility level of the view denoted by Obj. Note
21812 -- that the value returned is the result of a call to Scope_Depth. Only
21813 -- scope depths associated with dynamic scopes can actually be returned.
21814 -- Since only relative levels matter for accessibility checking, the fact
21815 -- that the distance between successive levels of accessibility is not
21816 -- always one is immaterial (invariant: if level(E2) is deeper than
21817 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
21819 function Object_Access_Level (Obj : Node_Id) return Uint is
21820 function Is_Interface_Conversion (N : Node_Id) return Boolean;
21821 -- Determine whether N is a construct of the form
21822 -- Some_Type (Operand._tag'Address)
21823 -- This construct appears in the context of dispatching calls.
21825 function Reference_To (Obj : Node_Id) return Node_Id;
21826 -- An explicit dereference is created when removing side effects from
21827 -- expressions for constraint checking purposes. In this case a local
21828 -- access type is created for it. The correct access level is that of
21829 -- the original source node. We detect this case by noting that the
21830 -- prefix of the dereference is created by an object declaration whose
21831 -- initial expression is a reference.
21833 -----------------------------
21834 -- Is_Interface_Conversion --
21835 -----------------------------
21837 function Is_Interface_Conversion (N : Node_Id) return Boolean is
21838 begin
21839 return Nkind (N) = N_Unchecked_Type_Conversion
21840 and then Nkind (Expression (N)) = N_Attribute_Reference
21841 and then Attribute_Name (Expression (N)) = Name_Address;
21842 end Is_Interface_Conversion;
21844 ------------------
21845 -- Reference_To --
21846 ------------------
21848 function Reference_To (Obj : Node_Id) return Node_Id is
21849 Pref : constant Node_Id := Prefix (Obj);
21850 begin
21851 if Is_Entity_Name (Pref)
21852 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
21853 and then Present (Expression (Parent (Entity (Pref))))
21854 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
21855 then
21856 return (Prefix (Expression (Parent (Entity (Pref)))));
21857 else
21858 return Empty;
21859 end if;
21860 end Reference_To;
21862 -- Local variables
21864 E : Entity_Id;
21866 -- Start of processing for Object_Access_Level
21868 begin
21869 if Nkind (Obj) = N_Defining_Identifier
21870 or else Is_Entity_Name (Obj)
21871 then
21872 if Nkind (Obj) = N_Defining_Identifier then
21873 E := Obj;
21874 else
21875 E := Entity (Obj);
21876 end if;
21878 if Is_Prival (E) then
21879 E := Prival_Link (E);
21880 end if;
21882 -- If E is a type then it denotes a current instance. For this case
21883 -- we add one to the normal accessibility level of the type to ensure
21884 -- that current instances are treated as always being deeper than
21885 -- than the level of any visible named access type (see 3.10.2(21)).
21887 if Is_Type (E) then
21888 return Type_Access_Level (E) + 1;
21890 elsif Present (Renamed_Object (E)) then
21891 return Object_Access_Level (Renamed_Object (E));
21893 -- Similarly, if E is a component of the current instance of a
21894 -- protected type, any instance of it is assumed to be at a deeper
21895 -- level than the type. For a protected object (whose type is an
21896 -- anonymous protected type) its components are at the same level
21897 -- as the type itself.
21899 elsif not Is_Overloadable (E)
21900 and then Ekind (Scope (E)) = E_Protected_Type
21901 and then Comes_From_Source (Scope (E))
21902 then
21903 return Type_Access_Level (Scope (E)) + 1;
21905 else
21906 -- Aliased formals of functions take their access level from the
21907 -- point of call, i.e. require a dynamic check. For static check
21908 -- purposes, this is smaller than the level of the subprogram
21909 -- itself. For procedures the aliased makes no difference.
21911 if Is_Formal (E)
21912 and then Is_Aliased (E)
21913 and then Ekind (Scope (E)) = E_Function
21914 then
21915 return Type_Access_Level (Etype (E));
21917 else
21918 return Scope_Depth (Enclosing_Dynamic_Scope (E));
21919 end if;
21920 end if;
21922 elsif Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
21923 if Is_Access_Type (Etype (Prefix (Obj))) then
21924 return Type_Access_Level (Etype (Prefix (Obj)));
21925 else
21926 return Object_Access_Level (Prefix (Obj));
21927 end if;
21929 elsif Nkind (Obj) = N_Explicit_Dereference then
21931 -- If the prefix is a selected access discriminant then we make a
21932 -- recursive call on the prefix, which will in turn check the level
21933 -- of the prefix object of the selected discriminant.
21935 -- In Ada 2012, if the discriminant has implicit dereference and
21936 -- the context is a selected component, treat this as an object of
21937 -- unknown scope (see below). This is necessary in compile-only mode;
21938 -- otherwise expansion will already have transformed the prefix into
21939 -- a temporary.
21941 if Nkind (Prefix (Obj)) = N_Selected_Component
21942 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
21943 and then
21944 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
21945 and then
21946 (not Has_Implicit_Dereference
21947 (Entity (Selector_Name (Prefix (Obj))))
21948 or else Nkind (Parent (Obj)) /= N_Selected_Component)
21949 then
21950 return Object_Access_Level (Prefix (Obj));
21952 -- Detect an interface conversion in the context of a dispatching
21953 -- call. Use the original form of the conversion to find the access
21954 -- level of the operand.
21956 elsif Is_Interface (Etype (Obj))
21957 and then Is_Interface_Conversion (Prefix (Obj))
21958 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
21959 then
21960 return Object_Access_Level (Original_Node (Obj));
21962 elsif not Comes_From_Source (Obj) then
21963 declare
21964 Ref : constant Node_Id := Reference_To (Obj);
21965 begin
21966 if Present (Ref) then
21967 return Object_Access_Level (Ref);
21968 else
21969 return Type_Access_Level (Etype (Prefix (Obj)));
21970 end if;
21971 end;
21973 else
21974 return Type_Access_Level (Etype (Prefix (Obj)));
21975 end if;
21977 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
21978 return Object_Access_Level (Expression (Obj));
21980 elsif Nkind (Obj) = N_Function_Call then
21982 -- Function results are objects, so we get either the access level of
21983 -- the function or, in the case of an indirect call, the level of the
21984 -- access-to-subprogram type. (This code is used for Ada 95, but it
21985 -- looks wrong, because it seems that we should be checking the level
21986 -- of the call itself, even for Ada 95. However, using the Ada 2005
21987 -- version of the code causes regressions in several tests that are
21988 -- compiled with -gnat95. ???)
21990 if Ada_Version < Ada_2005 then
21991 if Is_Entity_Name (Name (Obj)) then
21992 return Subprogram_Access_Level (Entity (Name (Obj)));
21993 else
21994 return Type_Access_Level (Etype (Prefix (Name (Obj))));
21995 end if;
21997 -- For Ada 2005, the level of the result object of a function call is
21998 -- defined to be the level of the call's innermost enclosing master.
21999 -- We determine that by querying the depth of the innermost enclosing
22000 -- dynamic scope.
22002 else
22003 Return_Master_Scope_Depth_Of_Call : declare
22004 function Innermost_Master_Scope_Depth
22005 (N : Node_Id) return Uint;
22006 -- Returns the scope depth of the given node's innermost
22007 -- enclosing dynamic scope (effectively the accessibility
22008 -- level of the innermost enclosing master).
22010 ----------------------------------
22011 -- Innermost_Master_Scope_Depth --
22012 ----------------------------------
22014 function Innermost_Master_Scope_Depth
22015 (N : Node_Id) return Uint
22017 Node_Par : Node_Id := Parent (N);
22019 begin
22020 -- Locate the nearest enclosing node (by traversing Parents)
22021 -- that Defining_Entity can be applied to, and return the
22022 -- depth of that entity's nearest enclosing dynamic scope.
22024 while Present (Node_Par) loop
22025 case Nkind (Node_Par) is
22026 when N_Abstract_Subprogram_Declaration
22027 | N_Block_Statement
22028 | N_Body_Stub
22029 | N_Component_Declaration
22030 | N_Entry_Body
22031 | N_Entry_Declaration
22032 | N_Exception_Declaration
22033 | N_Formal_Object_Declaration
22034 | N_Formal_Package_Declaration
22035 | N_Formal_Subprogram_Declaration
22036 | N_Formal_Type_Declaration
22037 | N_Full_Type_Declaration
22038 | N_Function_Specification
22039 | N_Generic_Declaration
22040 | N_Generic_Instantiation
22041 | N_Implicit_Label_Declaration
22042 | N_Incomplete_Type_Declaration
22043 | N_Loop_Parameter_Specification
22044 | N_Number_Declaration
22045 | N_Object_Declaration
22046 | N_Package_Declaration
22047 | N_Package_Specification
22048 | N_Parameter_Specification
22049 | N_Private_Extension_Declaration
22050 | N_Private_Type_Declaration
22051 | N_Procedure_Specification
22052 | N_Proper_Body
22053 | N_Protected_Type_Declaration
22054 | N_Renaming_Declaration
22055 | N_Single_Protected_Declaration
22056 | N_Single_Task_Declaration
22057 | N_Subprogram_Declaration
22058 | N_Subtype_Declaration
22059 | N_Subunit
22060 | N_Task_Type_Declaration
22062 return Scope_Depth
22063 (Nearest_Dynamic_Scope
22064 (Defining_Entity (Node_Par)));
22066 -- For a return statement within a function, return
22067 -- the depth of the function itself. This is not just
22068 -- a small optimization, but matters when analyzing
22069 -- the expression in an expression function before
22070 -- the body is created.
22072 when N_Simple_Return_Statement =>
22073 if Ekind (Current_Scope) = E_Function then
22074 return Scope_Depth (Current_Scope);
22075 end if;
22077 when others =>
22078 null;
22079 end case;
22081 Node_Par := Parent (Node_Par);
22082 end loop;
22084 pragma Assert (False);
22086 -- Should never reach the following return
22088 return Scope_Depth (Current_Scope) + 1;
22089 end Innermost_Master_Scope_Depth;
22091 -- Start of processing for Return_Master_Scope_Depth_Of_Call
22093 begin
22094 return Innermost_Master_Scope_Depth (Obj);
22095 end Return_Master_Scope_Depth_Of_Call;
22096 end if;
22098 -- For convenience we handle qualified expressions, even though they
22099 -- aren't technically object names.
22101 elsif Nkind (Obj) = N_Qualified_Expression then
22102 return Object_Access_Level (Expression (Obj));
22104 -- Ditto for aggregates. They have the level of the temporary that
22105 -- will hold their value.
22107 elsif Nkind (Obj) = N_Aggregate then
22108 return Object_Access_Level (Current_Scope);
22110 -- Otherwise return the scope level of Standard. (If there are cases
22111 -- that fall through to this point they will be treated as having
22112 -- global accessibility for now. ???)
22114 else
22115 return Scope_Depth (Standard_Standard);
22116 end if;
22117 end Object_Access_Level;
22119 ----------------------------------
22120 -- Old_Requires_Transient_Scope --
22121 ----------------------------------
22123 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
22124 Typ : constant Entity_Id := Underlying_Type (Id);
22126 begin
22127 -- This is a private type which is not completed yet. This can only
22128 -- happen in a default expression (of a formal parameter or of a
22129 -- record component). Do not expand transient scope in this case.
22131 if No (Typ) then
22132 return False;
22134 -- Do not expand transient scope for non-existent procedure return
22136 elsif Typ = Standard_Void_Type then
22137 return False;
22139 -- Elementary types do not require a transient scope
22141 elsif Is_Elementary_Type (Typ) then
22142 return False;
22144 -- Generally, indefinite subtypes require a transient scope, since the
22145 -- back end cannot generate temporaries, since this is not a valid type
22146 -- for declaring an object. It might be possible to relax this in the
22147 -- future, e.g. by declaring the maximum possible space for the type.
22149 elsif not Is_Definite_Subtype (Typ) then
22150 return True;
22152 -- Functions returning tagged types may dispatch on result so their
22153 -- returned value is allocated on the secondary stack. Controlled
22154 -- type temporaries need finalization.
22156 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
22157 return True;
22159 -- Record type
22161 elsif Is_Record_Type (Typ) then
22162 declare
22163 Comp : Entity_Id;
22165 begin
22166 Comp := First_Entity (Typ);
22167 while Present (Comp) loop
22168 if Ekind (Comp) = E_Component then
22170 -- ???It's not clear we need a full recursive call to
22171 -- Old_Requires_Transient_Scope here. Note that the
22172 -- following can't happen.
22174 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
22175 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
22177 if Old_Requires_Transient_Scope (Etype (Comp)) then
22178 return True;
22179 end if;
22180 end if;
22182 Next_Entity (Comp);
22183 end loop;
22184 end;
22186 return False;
22188 -- String literal types never require transient scope
22190 elsif Ekind (Typ) = E_String_Literal_Subtype then
22191 return False;
22193 -- Array type. Note that we already know that this is a constrained
22194 -- array, since unconstrained arrays will fail the indefinite test.
22196 elsif Is_Array_Type (Typ) then
22198 -- If component type requires a transient scope, the array does too
22200 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
22201 return True;
22203 -- Otherwise, we only need a transient scope if the size depends on
22204 -- the value of one or more discriminants.
22206 else
22207 return Size_Depends_On_Discriminant (Typ);
22208 end if;
22210 -- All other cases do not require a transient scope
22212 else
22213 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
22214 return False;
22215 end if;
22216 end Old_Requires_Transient_Scope;
22218 ---------------------------------
22219 -- Original_Aspect_Pragma_Name --
22220 ---------------------------------
22222 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
22223 Item : Node_Id;
22224 Item_Nam : Name_Id;
22226 begin
22227 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
22229 Item := N;
22231 -- The pragma was generated to emulate an aspect, use the original
22232 -- aspect specification.
22234 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
22235 Item := Corresponding_Aspect (Item);
22236 end if;
22238 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
22239 -- Post and Post_Class rewrite their pragma identifier to preserve the
22240 -- original name.
22241 -- ??? this is kludgey
22243 if Nkind (Item) = N_Pragma then
22244 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
22246 else
22247 pragma Assert (Nkind (Item) = N_Aspect_Specification);
22248 Item_Nam := Chars (Identifier (Item));
22249 end if;
22251 -- Deal with 'Class by converting the name to its _XXX form
22253 if Class_Present (Item) then
22254 if Item_Nam = Name_Invariant then
22255 Item_Nam := Name_uInvariant;
22257 elsif Item_Nam = Name_Post then
22258 Item_Nam := Name_uPost;
22260 elsif Item_Nam = Name_Pre then
22261 Item_Nam := Name_uPre;
22263 elsif Nam_In (Item_Nam, Name_Type_Invariant,
22264 Name_Type_Invariant_Class)
22265 then
22266 Item_Nam := Name_uType_Invariant;
22268 -- Nothing to do for other cases (e.g. a Check that derived from
22269 -- Pre_Class and has the flag set). Also we do nothing if the name
22270 -- is already in special _xxx form.
22272 end if;
22273 end if;
22275 return Item_Nam;
22276 end Original_Aspect_Pragma_Name;
22278 --------------------------------------
22279 -- Original_Corresponding_Operation --
22280 --------------------------------------
22282 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
22284 Typ : constant Entity_Id := Find_Dispatching_Type (S);
22286 begin
22287 -- If S is an inherited primitive S2 the original corresponding
22288 -- operation of S is the original corresponding operation of S2
22290 if Present (Alias (S))
22291 and then Find_Dispatching_Type (Alias (S)) /= Typ
22292 then
22293 return Original_Corresponding_Operation (Alias (S));
22295 -- If S overrides an inherited subprogram S2 the original corresponding
22296 -- operation of S is the original corresponding operation of S2
22298 elsif Present (Overridden_Operation (S)) then
22299 return Original_Corresponding_Operation (Overridden_Operation (S));
22301 -- otherwise it is S itself
22303 else
22304 return S;
22305 end if;
22306 end Original_Corresponding_Operation;
22308 -------------------
22309 -- Output_Entity --
22310 -------------------
22312 procedure Output_Entity (Id : Entity_Id) is
22313 Scop : Entity_Id;
22315 begin
22316 Scop := Scope (Id);
22318 -- The entity may lack a scope when it is in the process of being
22319 -- analyzed. Use the current scope as an approximation.
22321 if No (Scop) then
22322 Scop := Current_Scope;
22323 end if;
22325 Output_Name (Chars (Id), Scop);
22326 end Output_Entity;
22328 -----------------
22329 -- Output_Name --
22330 -----------------
22332 procedure Output_Name (Nam : Name_Id; Scop : Entity_Id := Current_Scope) is
22333 begin
22334 Write_Str
22335 (Get_Name_String
22336 (Get_Qualified_Name
22337 (Nam => Nam,
22338 Suffix => No_Name,
22339 Scop => Scop)));
22340 Write_Eol;
22341 end Output_Name;
22343 ----------------------
22344 -- Policy_In_Effect --
22345 ----------------------
22347 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
22348 function Policy_In_List (List : Node_Id) return Name_Id;
22349 -- Determine the mode of a policy in a N_Pragma list
22351 --------------------
22352 -- Policy_In_List --
22353 --------------------
22355 function Policy_In_List (List : Node_Id) return Name_Id is
22356 Arg1 : Node_Id;
22357 Arg2 : Node_Id;
22358 Prag : Node_Id;
22360 begin
22361 Prag := List;
22362 while Present (Prag) loop
22363 Arg1 := First (Pragma_Argument_Associations (Prag));
22364 Arg2 := Next (Arg1);
22366 Arg1 := Get_Pragma_Arg (Arg1);
22367 Arg2 := Get_Pragma_Arg (Arg2);
22369 -- The current Check_Policy pragma matches the requested policy or
22370 -- appears in the single argument form (Assertion, policy_id).
22372 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
22373 return Chars (Arg2);
22374 end if;
22376 Prag := Next_Pragma (Prag);
22377 end loop;
22379 return No_Name;
22380 end Policy_In_List;
22382 -- Local variables
22384 Kind : Name_Id;
22386 -- Start of processing for Policy_In_Effect
22388 begin
22389 if not Is_Valid_Assertion_Kind (Policy) then
22390 raise Program_Error;
22391 end if;
22393 -- Inspect all policy pragmas that appear within scopes (if any)
22395 Kind := Policy_In_List (Check_Policy_List);
22397 -- Inspect all configuration policy pragmas (if any)
22399 if Kind = No_Name then
22400 Kind := Policy_In_List (Check_Policy_List_Config);
22401 end if;
22403 -- The context lacks policy pragmas, determine the mode based on whether
22404 -- assertions are enabled at the configuration level. This ensures that
22405 -- the policy is preserved when analyzing generics.
22407 if Kind = No_Name then
22408 if Assertions_Enabled_Config then
22409 Kind := Name_Check;
22410 else
22411 Kind := Name_Ignore;
22412 end if;
22413 end if;
22415 return Kind;
22416 end Policy_In_Effect;
22418 ----------------------------------
22419 -- Predicate_Tests_On_Arguments --
22420 ----------------------------------
22422 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
22423 begin
22424 -- Always test predicates on indirect call
22426 if Ekind (Subp) = E_Subprogram_Type then
22427 return True;
22429 -- Do not test predicates on call to generated default Finalize, since
22430 -- we are not interested in whether something we are finalizing (and
22431 -- typically destroying) satisfies its predicates.
22433 elsif Chars (Subp) = Name_Finalize
22434 and then not Comes_From_Source (Subp)
22435 then
22436 return False;
22438 -- Do not test predicates on any internally generated routines
22440 elsif Is_Internal_Name (Chars (Subp)) then
22441 return False;
22443 -- Do not test predicates on call to Init_Proc, since if needed the
22444 -- predicate test will occur at some other point.
22446 elsif Is_Init_Proc (Subp) then
22447 return False;
22449 -- Do not test predicates on call to predicate function, since this
22450 -- would cause infinite recursion.
22452 elsif Ekind (Subp) = E_Function
22453 and then (Is_Predicate_Function (Subp)
22454 or else
22455 Is_Predicate_Function_M (Subp))
22456 then
22457 return False;
22459 -- For now, no other exceptions
22461 else
22462 return True;
22463 end if;
22464 end Predicate_Tests_On_Arguments;
22466 -----------------------
22467 -- Private_Component --
22468 -----------------------
22470 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
22471 Ancestor : constant Entity_Id := Base_Type (Type_Id);
22473 function Trace_Components
22474 (T : Entity_Id;
22475 Check : Boolean) return Entity_Id;
22476 -- Recursive function that does the work, and checks against circular
22477 -- definition for each subcomponent type.
22479 ----------------------
22480 -- Trace_Components --
22481 ----------------------
22483 function Trace_Components
22484 (T : Entity_Id;
22485 Check : Boolean) return Entity_Id
22487 Btype : constant Entity_Id := Base_Type (T);
22488 Component : Entity_Id;
22489 P : Entity_Id;
22490 Candidate : Entity_Id := Empty;
22492 begin
22493 if Check and then Btype = Ancestor then
22494 Error_Msg_N ("circular type definition", Type_Id);
22495 return Any_Type;
22496 end if;
22498 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
22499 if Present (Full_View (Btype))
22500 and then Is_Record_Type (Full_View (Btype))
22501 and then not Is_Frozen (Btype)
22502 then
22503 -- To indicate that the ancestor depends on a private type, the
22504 -- current Btype is sufficient. However, to check for circular
22505 -- definition we must recurse on the full view.
22507 Candidate := Trace_Components (Full_View (Btype), True);
22509 if Candidate = Any_Type then
22510 return Any_Type;
22511 else
22512 return Btype;
22513 end if;
22515 else
22516 return Btype;
22517 end if;
22519 elsif Is_Array_Type (Btype) then
22520 return Trace_Components (Component_Type (Btype), True);
22522 elsif Is_Record_Type (Btype) then
22523 Component := First_Entity (Btype);
22524 while Present (Component)
22525 and then Comes_From_Source (Component)
22526 loop
22527 -- Skip anonymous types generated by constrained components
22529 if not Is_Type (Component) then
22530 P := Trace_Components (Etype (Component), True);
22532 if Present (P) then
22533 if P = Any_Type then
22534 return P;
22535 else
22536 Candidate := P;
22537 end if;
22538 end if;
22539 end if;
22541 Next_Entity (Component);
22542 end loop;
22544 return Candidate;
22546 else
22547 return Empty;
22548 end if;
22549 end Trace_Components;
22551 -- Start of processing for Private_Component
22553 begin
22554 return Trace_Components (Type_Id, False);
22555 end Private_Component;
22557 ---------------------------
22558 -- Primitive_Names_Match --
22559 ---------------------------
22561 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
22562 function Non_Internal_Name (E : Entity_Id) return Name_Id;
22563 -- Given an internal name, returns the corresponding non-internal name
22565 ------------------------
22566 -- Non_Internal_Name --
22567 ------------------------
22569 function Non_Internal_Name (E : Entity_Id) return Name_Id is
22570 begin
22571 Get_Name_String (Chars (E));
22572 Name_Len := Name_Len - 1;
22573 return Name_Find;
22574 end Non_Internal_Name;
22576 -- Start of processing for Primitive_Names_Match
22578 begin
22579 pragma Assert (Present (E1) and then Present (E2));
22581 return Chars (E1) = Chars (E2)
22582 or else
22583 (not Is_Internal_Name (Chars (E1))
22584 and then Is_Internal_Name (Chars (E2))
22585 and then Non_Internal_Name (E2) = Chars (E1))
22586 or else
22587 (not Is_Internal_Name (Chars (E2))
22588 and then Is_Internal_Name (Chars (E1))
22589 and then Non_Internal_Name (E1) = Chars (E2))
22590 or else
22591 (Is_Predefined_Dispatching_Operation (E1)
22592 and then Is_Predefined_Dispatching_Operation (E2)
22593 and then Same_TSS (E1, E2))
22594 or else
22595 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
22596 end Primitive_Names_Match;
22598 -----------------------
22599 -- Process_End_Label --
22600 -----------------------
22602 procedure Process_End_Label
22603 (N : Node_Id;
22604 Typ : Character;
22605 Ent : Entity_Id)
22607 Loc : Source_Ptr;
22608 Nam : Node_Id;
22609 Scop : Entity_Id;
22611 Label_Ref : Boolean;
22612 -- Set True if reference to end label itself is required
22614 Endl : Node_Id;
22615 -- Gets set to the operator symbol or identifier that references the
22616 -- entity Ent. For the child unit case, this is the identifier from the
22617 -- designator. For other cases, this is simply Endl.
22619 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
22620 -- N is an identifier node that appears as a parent unit reference in
22621 -- the case where Ent is a child unit. This procedure generates an
22622 -- appropriate cross-reference entry. E is the corresponding entity.
22624 -------------------------
22625 -- Generate_Parent_Ref --
22626 -------------------------
22628 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
22629 begin
22630 -- If names do not match, something weird, skip reference
22632 if Chars (E) = Chars (N) then
22634 -- Generate the reference. We do NOT consider this as a reference
22635 -- for unreferenced symbol purposes.
22637 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
22639 if Style_Check then
22640 Style.Check_Identifier (N, E);
22641 end if;
22642 end if;
22643 end Generate_Parent_Ref;
22645 -- Start of processing for Process_End_Label
22647 begin
22648 -- If no node, ignore. This happens in some error situations, and
22649 -- also for some internally generated structures where no end label
22650 -- references are required in any case.
22652 if No (N) then
22653 return;
22654 end if;
22656 -- Nothing to do if no End_Label, happens for internally generated
22657 -- constructs where we don't want an end label reference anyway. Also
22658 -- nothing to do if Endl is a string literal, which means there was
22659 -- some prior error (bad operator symbol)
22661 Endl := End_Label (N);
22663 if No (Endl) or else Nkind (Endl) = N_String_Literal then
22664 return;
22665 end if;
22667 -- Reference node is not in extended main source unit
22669 if not In_Extended_Main_Source_Unit (N) then
22671 -- Generally we do not collect references except for the extended
22672 -- main source unit. The one exception is the 'e' entry for a
22673 -- package spec, where it is useful for a client to have the
22674 -- ending information to define scopes.
22676 if Typ /= 'e' then
22677 return;
22679 else
22680 Label_Ref := False;
22682 -- For this case, we can ignore any parent references, but we
22683 -- need the package name itself for the 'e' entry.
22685 if Nkind (Endl) = N_Designator then
22686 Endl := Identifier (Endl);
22687 end if;
22688 end if;
22690 -- Reference is in extended main source unit
22692 else
22693 Label_Ref := True;
22695 -- For designator, generate references for the parent entries
22697 if Nkind (Endl) = N_Designator then
22699 -- Generate references for the prefix if the END line comes from
22700 -- source (otherwise we do not need these references) We climb the
22701 -- scope stack to find the expected entities.
22703 if Comes_From_Source (Endl) then
22704 Nam := Name (Endl);
22705 Scop := Current_Scope;
22706 while Nkind (Nam) = N_Selected_Component loop
22707 Scop := Scope (Scop);
22708 exit when No (Scop);
22709 Generate_Parent_Ref (Selector_Name (Nam), Scop);
22710 Nam := Prefix (Nam);
22711 end loop;
22713 if Present (Scop) then
22714 Generate_Parent_Ref (Nam, Scope (Scop));
22715 end if;
22716 end if;
22718 Endl := Identifier (Endl);
22719 end if;
22720 end if;
22722 -- If the end label is not for the given entity, then either we have
22723 -- some previous error, or this is a generic instantiation for which
22724 -- we do not need to make a cross-reference in this case anyway. In
22725 -- either case we simply ignore the call.
22727 if Chars (Ent) /= Chars (Endl) then
22728 return;
22729 end if;
22731 -- If label was really there, then generate a normal reference and then
22732 -- adjust the location in the end label to point past the name (which
22733 -- should almost always be the semicolon).
22735 Loc := Sloc (Endl);
22737 if Comes_From_Source (Endl) then
22739 -- If a label reference is required, then do the style check and
22740 -- generate an l-type cross-reference entry for the label
22742 if Label_Ref then
22743 if Style_Check then
22744 Style.Check_Identifier (Endl, Ent);
22745 end if;
22747 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
22748 end if;
22750 -- Set the location to point past the label (normally this will
22751 -- mean the semicolon immediately following the label). This is
22752 -- done for the sake of the 'e' or 't' entry generated below.
22754 Get_Decoded_Name_String (Chars (Endl));
22755 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
22757 else
22758 -- In SPARK mode, no missing label is allowed for packages and
22759 -- subprogram bodies. Detect those cases by testing whether
22760 -- Process_End_Label was called for a body (Typ = 't') or a package.
22762 if Restriction_Check_Required (SPARK_05)
22763 and then (Typ = 't' or else Ekind (Ent) = E_Package)
22764 then
22765 Error_Msg_Node_1 := Endl;
22766 Check_SPARK_05_Restriction
22767 ("`END &` required", Endl, Force => True);
22768 end if;
22769 end if;
22771 -- Now generate the e/t reference
22773 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
22775 -- Restore Sloc, in case modified above, since we have an identifier
22776 -- and the normal Sloc should be left set in the tree.
22778 Set_Sloc (Endl, Loc);
22779 end Process_End_Label;
22781 --------------------------------
22782 -- Propagate_Concurrent_Flags --
22783 --------------------------------
22785 procedure Propagate_Concurrent_Flags
22786 (Typ : Entity_Id;
22787 Comp_Typ : Entity_Id)
22789 begin
22790 if Has_Task (Comp_Typ) then
22791 Set_Has_Task (Typ);
22792 end if;
22794 if Has_Protected (Comp_Typ) then
22795 Set_Has_Protected (Typ);
22796 end if;
22798 if Has_Timing_Event (Comp_Typ) then
22799 Set_Has_Timing_Event (Typ);
22800 end if;
22801 end Propagate_Concurrent_Flags;
22803 ------------------------------
22804 -- Propagate_DIC_Attributes --
22805 ------------------------------
22807 procedure Propagate_DIC_Attributes
22808 (Typ : Entity_Id;
22809 From_Typ : Entity_Id)
22811 DIC_Proc : Entity_Id;
22813 begin
22814 if Present (Typ) and then Present (From_Typ) then
22815 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
22817 -- Nothing to do if both the source and the destination denote the
22818 -- same type.
22820 if From_Typ = Typ then
22821 return;
22822 end if;
22824 DIC_Proc := DIC_Procedure (From_Typ);
22826 -- The setting of the attributes is intentionally conservative. This
22827 -- prevents accidental clobbering of enabled attributes.
22829 if Has_Inherited_DIC (From_Typ)
22830 and then not Has_Inherited_DIC (Typ)
22831 then
22832 Set_Has_Inherited_DIC (Typ);
22833 end if;
22835 if Has_Own_DIC (From_Typ) and then not Has_Own_DIC (Typ) then
22836 Set_Has_Own_DIC (Typ);
22837 end if;
22839 if Present (DIC_Proc) and then No (DIC_Procedure (Typ)) then
22840 Set_DIC_Procedure (Typ, DIC_Proc);
22841 end if;
22842 end if;
22843 end Propagate_DIC_Attributes;
22845 ------------------------------------
22846 -- Propagate_Invariant_Attributes --
22847 ------------------------------------
22849 procedure Propagate_Invariant_Attributes
22850 (Typ : Entity_Id;
22851 From_Typ : Entity_Id)
22853 Full_IP : Entity_Id;
22854 Part_IP : Entity_Id;
22856 begin
22857 if Present (Typ) and then Present (From_Typ) then
22858 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
22860 -- Nothing to do if both the source and the destination denote the
22861 -- same type.
22863 if From_Typ = Typ then
22864 return;
22865 end if;
22867 Full_IP := Invariant_Procedure (From_Typ);
22868 Part_IP := Partial_Invariant_Procedure (From_Typ);
22870 -- The setting of the attributes is intentionally conservative. This
22871 -- prevents accidental clobbering of enabled attributes.
22873 if Has_Inheritable_Invariants (From_Typ)
22874 and then not Has_Inheritable_Invariants (Typ)
22875 then
22876 Set_Has_Inheritable_Invariants (Typ, True);
22877 end if;
22879 if Has_Inherited_Invariants (From_Typ)
22880 and then not Has_Inherited_Invariants (Typ)
22881 then
22882 Set_Has_Inherited_Invariants (Typ, True);
22883 end if;
22885 if Has_Own_Invariants (From_Typ)
22886 and then not Has_Own_Invariants (Typ)
22887 then
22888 Set_Has_Own_Invariants (Typ, True);
22889 end if;
22891 if Present (Full_IP) and then No (Invariant_Procedure (Typ)) then
22892 Set_Invariant_Procedure (Typ, Full_IP);
22893 end if;
22895 if Present (Part_IP) and then No (Partial_Invariant_Procedure (Typ))
22896 then
22897 Set_Partial_Invariant_Procedure (Typ, Part_IP);
22898 end if;
22899 end if;
22900 end Propagate_Invariant_Attributes;
22902 ---------------------------------------
22903 -- Record_Possible_Part_Of_Reference --
22904 ---------------------------------------
22906 procedure Record_Possible_Part_Of_Reference
22907 (Var_Id : Entity_Id;
22908 Ref : Node_Id)
22910 Encap : constant Entity_Id := Encapsulating_State (Var_Id);
22911 Refs : Elist_Id;
22913 begin
22914 -- The variable is a constituent of a single protected/task type. Such
22915 -- a variable acts as a component of the type and must appear within a
22916 -- specific region (SPARK RM 9(3)). Instead of recording the reference,
22917 -- verify its legality now.
22919 if Present (Encap) and then Is_Single_Concurrent_Object (Encap) then
22920 Check_Part_Of_Reference (Var_Id, Ref);
22922 -- The variable is subject to pragma Part_Of and may eventually become a
22923 -- constituent of a single protected/task type. Record the reference to
22924 -- verify its placement when the contract of the variable is analyzed.
22926 elsif Present (Get_Pragma (Var_Id, Pragma_Part_Of)) then
22927 Refs := Part_Of_References (Var_Id);
22929 if No (Refs) then
22930 Refs := New_Elmt_List;
22931 Set_Part_Of_References (Var_Id, Refs);
22932 end if;
22934 Append_Elmt (Ref, Refs);
22935 end if;
22936 end Record_Possible_Part_Of_Reference;
22938 ----------------
22939 -- Referenced --
22940 ----------------
22942 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
22943 Seen : Boolean := False;
22945 function Is_Reference (N : Node_Id) return Traverse_Result;
22946 -- Determine whether node N denotes a reference to Id. If this is the
22947 -- case, set global flag Seen to True and stop the traversal.
22949 ------------------
22950 -- Is_Reference --
22951 ------------------
22953 function Is_Reference (N : Node_Id) return Traverse_Result is
22954 begin
22955 if Is_Entity_Name (N)
22956 and then Present (Entity (N))
22957 and then Entity (N) = Id
22958 then
22959 Seen := True;
22960 return Abandon;
22961 else
22962 return OK;
22963 end if;
22964 end Is_Reference;
22966 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
22968 -- Start of processing for Referenced
22970 begin
22971 Inspect_Expression (Expr);
22972 return Seen;
22973 end Referenced;
22975 ------------------------------------
22976 -- References_Generic_Formal_Type --
22977 ------------------------------------
22979 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
22981 function Process (N : Node_Id) return Traverse_Result;
22982 -- Process one node in search for generic formal type
22984 -------------
22985 -- Process --
22986 -------------
22988 function Process (N : Node_Id) return Traverse_Result is
22989 begin
22990 if Nkind (N) in N_Has_Entity then
22991 declare
22992 E : constant Entity_Id := Entity (N);
22993 begin
22994 if Present (E) then
22995 if Is_Generic_Type (E) then
22996 return Abandon;
22997 elsif Present (Etype (E))
22998 and then Is_Generic_Type (Etype (E))
22999 then
23000 return Abandon;
23001 end if;
23002 end if;
23003 end;
23004 end if;
23006 return Atree.OK;
23007 end Process;
23009 function Traverse is new Traverse_Func (Process);
23010 -- Traverse tree to look for generic type
23012 begin
23013 if Inside_A_Generic then
23014 return Traverse (N) = Abandon;
23015 else
23016 return False;
23017 end if;
23018 end References_Generic_Formal_Type;
23020 -------------------------------
23021 -- Remove_Entity_And_Homonym --
23022 -------------------------------
23024 procedure Remove_Entity_And_Homonym (Id : Entity_Id) is
23025 begin
23026 Remove_Entity (Id);
23027 Remove_Homonym (Id);
23028 end Remove_Entity_And_Homonym;
23030 --------------------
23031 -- Remove_Homonym --
23032 --------------------
23034 procedure Remove_Homonym (Id : Entity_Id) is
23035 Hom : Entity_Id;
23036 Prev : Entity_Id := Empty;
23038 begin
23039 if Id = Current_Entity (Id) then
23040 if Present (Homonym (Id)) then
23041 Set_Current_Entity (Homonym (Id));
23042 else
23043 Set_Name_Entity_Id (Chars (Id), Empty);
23044 end if;
23046 else
23047 Hom := Current_Entity (Id);
23048 while Present (Hom) and then Hom /= Id loop
23049 Prev := Hom;
23050 Hom := Homonym (Hom);
23051 end loop;
23053 -- If Id is not on the homonym chain, nothing to do
23055 if Present (Hom) then
23056 Set_Homonym (Prev, Homonym (Id));
23057 end if;
23058 end if;
23059 end Remove_Homonym;
23061 ------------------------------
23062 -- Remove_Overloaded_Entity --
23063 ------------------------------
23065 procedure Remove_Overloaded_Entity (Id : Entity_Id) is
23066 procedure Remove_Primitive_Of (Typ : Entity_Id);
23067 -- Remove primitive subprogram Id from the list of primitives that
23068 -- belong to type Typ.
23070 -------------------------
23071 -- Remove_Primitive_Of --
23072 -------------------------
23074 procedure Remove_Primitive_Of (Typ : Entity_Id) is
23075 Prims : Elist_Id;
23077 begin
23078 if Is_Tagged_Type (Typ) then
23079 Prims := Direct_Primitive_Operations (Typ);
23081 if Present (Prims) then
23082 Remove (Prims, Id);
23083 end if;
23084 end if;
23085 end Remove_Primitive_Of;
23087 -- Local variables
23089 Formal : Entity_Id;
23091 -- Start of processing for Remove_Overloaded_Entity
23093 begin
23094 Remove_Entity_And_Homonym (Id);
23096 -- The entity denotes a primitive subprogram. Remove it from the list of
23097 -- primitives of the associated controlling type.
23099 if Ekind_In (Id, E_Function, E_Procedure) and then Is_Primitive (Id) then
23100 Formal := First_Formal (Id);
23101 while Present (Formal) loop
23102 if Is_Controlling_Formal (Formal) then
23103 Remove_Primitive_Of (Etype (Formal));
23104 exit;
23105 end if;
23107 Next_Formal (Formal);
23108 end loop;
23110 if Ekind (Id) = E_Function and then Has_Controlling_Result (Id) then
23111 Remove_Primitive_Of (Etype (Id));
23112 end if;
23113 end if;
23114 end Remove_Overloaded_Entity;
23116 ---------------------
23117 -- Rep_To_Pos_Flag --
23118 ---------------------
23120 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
23121 begin
23122 return New_Occurrence_Of
23123 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
23124 end Rep_To_Pos_Flag;
23126 --------------------
23127 -- Require_Entity --
23128 --------------------
23130 procedure Require_Entity (N : Node_Id) is
23131 begin
23132 if Is_Entity_Name (N) and then No (Entity (N)) then
23133 if Total_Errors_Detected /= 0 then
23134 Set_Entity (N, Any_Id);
23135 else
23136 raise Program_Error;
23137 end if;
23138 end if;
23139 end Require_Entity;
23141 ------------------------------
23142 -- Requires_Transient_Scope --
23143 ------------------------------
23145 -- A transient scope is required when variable-sized temporaries are
23146 -- allocated on the secondary stack, or when finalization actions must be
23147 -- generated before the next instruction.
23149 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
23150 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
23152 begin
23153 if Debug_Flag_QQ then
23154 return Old_Result;
23155 end if;
23157 declare
23158 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
23160 begin
23161 -- Assert that we're not putting things on the secondary stack if we
23162 -- didn't before; we are trying to AVOID secondary stack when
23163 -- possible.
23165 if not Old_Result then
23166 pragma Assert (not New_Result);
23167 null;
23168 end if;
23170 if New_Result /= Old_Result then
23171 Results_Differ (Id, Old_Result, New_Result);
23172 end if;
23174 return New_Result;
23175 end;
23176 end Requires_Transient_Scope;
23178 --------------------
23179 -- Results_Differ --
23180 --------------------
23182 procedure Results_Differ
23183 (Id : Entity_Id;
23184 Old_Val : Boolean;
23185 New_Val : Boolean)
23187 begin
23188 if False then -- False to disable; True for debugging
23189 Treepr.Print_Tree_Node (Id);
23191 if Old_Val = New_Val then
23192 raise Program_Error;
23193 end if;
23194 end if;
23195 end Results_Differ;
23197 --------------------------
23198 -- Reset_Analyzed_Flags --
23199 --------------------------
23201 procedure Reset_Analyzed_Flags (N : Node_Id) is
23202 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
23203 -- Function used to reset Analyzed flags in tree. Note that we do
23204 -- not reset Analyzed flags in entities, since there is no need to
23205 -- reanalyze entities, and indeed, it is wrong to do so, since it
23206 -- can result in generating auxiliary stuff more than once.
23208 --------------------
23209 -- Clear_Analyzed --
23210 --------------------
23212 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
23213 begin
23214 if Nkind (N) not in N_Entity then
23215 Set_Analyzed (N, False);
23216 end if;
23218 return OK;
23219 end Clear_Analyzed;
23221 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
23223 -- Start of processing for Reset_Analyzed_Flags
23225 begin
23226 Reset_Analyzed (N);
23227 end Reset_Analyzed_Flags;
23229 ------------------------
23230 -- Restore_SPARK_Mode --
23231 ------------------------
23233 procedure Restore_SPARK_Mode
23234 (Mode : SPARK_Mode_Type;
23235 Prag : Node_Id)
23237 begin
23238 SPARK_Mode := Mode;
23239 SPARK_Mode_Pragma := Prag;
23240 end Restore_SPARK_Mode;
23242 --------------------------------
23243 -- Returns_Unconstrained_Type --
23244 --------------------------------
23246 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
23247 begin
23248 return Ekind (Subp) = E_Function
23249 and then not Is_Scalar_Type (Etype (Subp))
23250 and then not Is_Access_Type (Etype (Subp))
23251 and then not Is_Constrained (Etype (Subp));
23252 end Returns_Unconstrained_Type;
23254 ----------------------------
23255 -- Root_Type_Of_Full_View --
23256 ----------------------------
23258 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
23259 Rtyp : constant Entity_Id := Root_Type (T);
23261 begin
23262 -- The root type of the full view may itself be a private type. Keep
23263 -- looking for the ultimate derivation parent.
23265 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
23266 return Root_Type_Of_Full_View (Full_View (Rtyp));
23267 else
23268 return Rtyp;
23269 end if;
23270 end Root_Type_Of_Full_View;
23272 ---------------------------
23273 -- Safe_To_Capture_Value --
23274 ---------------------------
23276 function Safe_To_Capture_Value
23277 (N : Node_Id;
23278 Ent : Entity_Id;
23279 Cond : Boolean := False) return Boolean
23281 begin
23282 -- The only entities for which we track constant values are variables
23283 -- which are not renamings, constants, out parameters, and in out
23284 -- parameters, so check if we have this case.
23286 -- Note: it may seem odd to track constant values for constants, but in
23287 -- fact this routine is used for other purposes than simply capturing
23288 -- the value. In particular, the setting of Known[_Non]_Null.
23290 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
23291 or else
23292 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
23293 then
23294 null;
23296 -- For conditionals, we also allow loop parameters and all formals,
23297 -- including in parameters.
23299 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
23300 null;
23302 -- For all other cases, not just unsafe, but impossible to capture
23303 -- Current_Value, since the above are the only entities which have
23304 -- Current_Value fields.
23306 else
23307 return False;
23308 end if;
23310 -- Skip if volatile or aliased, since funny things might be going on in
23311 -- these cases which we cannot necessarily track. Also skip any variable
23312 -- for which an address clause is given, or whose address is taken. Also
23313 -- never capture value of library level variables (an attempt to do so
23314 -- can occur in the case of package elaboration code).
23316 if Treat_As_Volatile (Ent)
23317 or else Is_Aliased (Ent)
23318 or else Present (Address_Clause (Ent))
23319 or else Address_Taken (Ent)
23320 or else (Is_Library_Level_Entity (Ent)
23321 and then Ekind (Ent) = E_Variable)
23322 then
23323 return False;
23324 end if;
23326 -- OK, all above conditions are met. We also require that the scope of
23327 -- the reference be the same as the scope of the entity, not counting
23328 -- packages and blocks and loops.
23330 declare
23331 E_Scope : constant Entity_Id := Scope (Ent);
23332 R_Scope : Entity_Id;
23334 begin
23335 R_Scope := Current_Scope;
23336 while R_Scope /= Standard_Standard loop
23337 exit when R_Scope = E_Scope;
23339 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
23340 return False;
23341 else
23342 R_Scope := Scope (R_Scope);
23343 end if;
23344 end loop;
23345 end;
23347 -- We also require that the reference does not appear in a context
23348 -- where it is not sure to be executed (i.e. a conditional context
23349 -- or an exception handler). We skip this if Cond is True, since the
23350 -- capturing of values from conditional tests handles this ok.
23352 if Cond then
23353 return True;
23354 end if;
23356 declare
23357 Desc : Node_Id;
23358 P : Node_Id;
23360 begin
23361 Desc := N;
23363 -- Seems dubious that case expressions are not handled here ???
23365 P := Parent (N);
23366 while Present (P) loop
23367 if Nkind (P) = N_If_Statement
23368 or else Nkind (P) = N_Case_Statement
23369 or else (Nkind (P) in N_Short_Circuit
23370 and then Desc = Right_Opnd (P))
23371 or else (Nkind (P) = N_If_Expression
23372 and then Desc /= First (Expressions (P)))
23373 or else Nkind (P) = N_Exception_Handler
23374 or else Nkind (P) = N_Selective_Accept
23375 or else Nkind (P) = N_Conditional_Entry_Call
23376 or else Nkind (P) = N_Timed_Entry_Call
23377 or else Nkind (P) = N_Asynchronous_Select
23378 then
23379 return False;
23381 else
23382 Desc := P;
23383 P := Parent (P);
23385 -- A special Ada 2012 case: the original node may be part
23386 -- of the else_actions of a conditional expression, in which
23387 -- case it might not have been expanded yet, and appears in
23388 -- a non-syntactic list of actions. In that case it is clearly
23389 -- not safe to save a value.
23391 if No (P)
23392 and then Is_List_Member (Desc)
23393 and then No (Parent (List_Containing (Desc)))
23394 then
23395 return False;
23396 end if;
23397 end if;
23398 end loop;
23399 end;
23401 -- OK, looks safe to set value
23403 return True;
23404 end Safe_To_Capture_Value;
23406 ---------------
23407 -- Same_Name --
23408 ---------------
23410 function Same_Name (N1, N2 : Node_Id) return Boolean is
23411 K1 : constant Node_Kind := Nkind (N1);
23412 K2 : constant Node_Kind := Nkind (N2);
23414 begin
23415 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
23416 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
23417 then
23418 return Chars (N1) = Chars (N2);
23420 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
23421 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
23422 then
23423 return Same_Name (Selector_Name (N1), Selector_Name (N2))
23424 and then Same_Name (Prefix (N1), Prefix (N2));
23426 else
23427 return False;
23428 end if;
23429 end Same_Name;
23431 -----------------
23432 -- Same_Object --
23433 -----------------
23435 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
23436 N1 : constant Node_Id := Original_Node (Node1);
23437 N2 : constant Node_Id := Original_Node (Node2);
23438 -- We do the tests on original nodes, since we are most interested
23439 -- in the original source, not any expansion that got in the way.
23441 K1 : constant Node_Kind := Nkind (N1);
23442 K2 : constant Node_Kind := Nkind (N2);
23444 begin
23445 -- First case, both are entities with same entity
23447 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
23448 declare
23449 EN1 : constant Entity_Id := Entity (N1);
23450 EN2 : constant Entity_Id := Entity (N2);
23451 begin
23452 if Present (EN1) and then Present (EN2)
23453 and then (Ekind_In (EN1, E_Variable, E_Constant)
23454 or else Is_Formal (EN1))
23455 and then EN1 = EN2
23456 then
23457 return True;
23458 end if;
23459 end;
23460 end if;
23462 -- Second case, selected component with same selector, same record
23464 if K1 = N_Selected_Component
23465 and then K2 = N_Selected_Component
23466 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
23467 then
23468 return Same_Object (Prefix (N1), Prefix (N2));
23470 -- Third case, indexed component with same subscripts, same array
23472 elsif K1 = N_Indexed_Component
23473 and then K2 = N_Indexed_Component
23474 and then Same_Object (Prefix (N1), Prefix (N2))
23475 then
23476 declare
23477 E1, E2 : Node_Id;
23478 begin
23479 E1 := First (Expressions (N1));
23480 E2 := First (Expressions (N2));
23481 while Present (E1) loop
23482 if not Same_Value (E1, E2) then
23483 return False;
23484 else
23485 Next (E1);
23486 Next (E2);
23487 end if;
23488 end loop;
23490 return True;
23491 end;
23493 -- Fourth case, slice of same array with same bounds
23495 elsif K1 = N_Slice
23496 and then K2 = N_Slice
23497 and then Nkind (Discrete_Range (N1)) = N_Range
23498 and then Nkind (Discrete_Range (N2)) = N_Range
23499 and then Same_Value (Low_Bound (Discrete_Range (N1)),
23500 Low_Bound (Discrete_Range (N2)))
23501 and then Same_Value (High_Bound (Discrete_Range (N1)),
23502 High_Bound (Discrete_Range (N2)))
23503 then
23504 return Same_Name (Prefix (N1), Prefix (N2));
23506 -- All other cases, not clearly the same object
23508 else
23509 return False;
23510 end if;
23511 end Same_Object;
23513 ---------------
23514 -- Same_Type --
23515 ---------------
23517 function Same_Type (T1, T2 : Entity_Id) return Boolean is
23518 begin
23519 if T1 = T2 then
23520 return True;
23522 elsif not Is_Constrained (T1)
23523 and then not Is_Constrained (T2)
23524 and then Base_Type (T1) = Base_Type (T2)
23525 then
23526 return True;
23528 -- For now don't bother with case of identical constraints, to be
23529 -- fiddled with later on perhaps (this is only used for optimization
23530 -- purposes, so it is not critical to do a best possible job)
23532 else
23533 return False;
23534 end if;
23535 end Same_Type;
23537 ----------------
23538 -- Same_Value --
23539 ----------------
23541 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
23542 begin
23543 if Compile_Time_Known_Value (Node1)
23544 and then Compile_Time_Known_Value (Node2)
23545 then
23546 -- Handle properly compile-time expressions that are not
23547 -- scalar.
23549 if Is_String_Type (Etype (Node1)) then
23550 return Expr_Value_S (Node1) = Expr_Value_S (Node2);
23552 else
23553 return Expr_Value (Node1) = Expr_Value (Node2);
23554 end if;
23556 elsif Same_Object (Node1, Node2) then
23557 return True;
23558 else
23559 return False;
23560 end if;
23561 end Same_Value;
23563 --------------------
23564 -- Set_SPARK_Mode --
23565 --------------------
23567 procedure Set_SPARK_Mode (Context : Entity_Id) is
23568 begin
23569 -- Do not consider illegal or partially decorated constructs
23571 if Ekind (Context) = E_Void or else Error_Posted (Context) then
23572 null;
23574 elsif Present (SPARK_Pragma (Context)) then
23575 Install_SPARK_Mode
23576 (Mode => Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Context)),
23577 Prag => SPARK_Pragma (Context));
23578 end if;
23579 end Set_SPARK_Mode;
23581 -------------------------
23582 -- Scalar_Part_Present --
23583 -------------------------
23585 function Scalar_Part_Present (Typ : Entity_Id) return Boolean is
23586 Val_Typ : constant Entity_Id := Validated_View (Typ);
23587 Field : Entity_Id;
23589 begin
23590 if Is_Scalar_Type (Val_Typ) then
23591 return True;
23593 elsif Is_Array_Type (Val_Typ) then
23594 return Scalar_Part_Present (Component_Type (Val_Typ));
23596 elsif Is_Record_Type (Val_Typ) then
23597 Field := First_Component_Or_Discriminant (Val_Typ);
23598 while Present (Field) loop
23599 if Scalar_Part_Present (Etype (Field)) then
23600 return True;
23601 end if;
23603 Next_Component_Or_Discriminant (Field);
23604 end loop;
23605 end if;
23607 return False;
23608 end Scalar_Part_Present;
23610 ------------------------
23611 -- Scope_Is_Transient --
23612 ------------------------
23614 function Scope_Is_Transient return Boolean is
23615 begin
23616 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
23617 end Scope_Is_Transient;
23619 ------------------
23620 -- Scope_Within --
23621 ------------------
23623 function Scope_Within
23624 (Inner : Entity_Id;
23625 Outer : Entity_Id) return Boolean
23627 Curr : Entity_Id;
23629 begin
23630 Curr := Inner;
23631 while Present (Curr) and then Curr /= Standard_Standard loop
23632 Curr := Scope (Curr);
23634 if Curr = Outer then
23635 return True;
23636 end if;
23637 end loop;
23639 return False;
23640 end Scope_Within;
23642 --------------------------
23643 -- Scope_Within_Or_Same --
23644 --------------------------
23646 function Scope_Within_Or_Same
23647 (Inner : Entity_Id;
23648 Outer : Entity_Id) return Boolean
23650 Curr : Entity_Id;
23652 begin
23653 Curr := Inner;
23654 while Present (Curr) and then Curr /= Standard_Standard loop
23655 if Curr = Outer then
23656 return True;
23657 end if;
23659 Curr := Scope (Curr);
23660 end loop;
23662 return False;
23663 end Scope_Within_Or_Same;
23665 --------------------
23666 -- Set_Convention --
23667 --------------------
23669 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
23670 begin
23671 Basic_Set_Convention (E, Val);
23673 if Is_Type (E)
23674 and then Is_Access_Subprogram_Type (Base_Type (E))
23675 and then Has_Foreign_Convention (E)
23676 then
23677 Set_Can_Use_Internal_Rep (E, False);
23678 end if;
23680 -- If E is an object, including a component, and the type of E is an
23681 -- anonymous access type with no convention set, then also set the
23682 -- convention of the anonymous access type. We do not do this for
23683 -- anonymous protected types, since protected types always have the
23684 -- default convention.
23686 if Present (Etype (E))
23687 and then (Is_Object (E)
23689 -- Allow E_Void (happens for pragma Convention appearing
23690 -- in the middle of a record applying to a component)
23692 or else Ekind (E) = E_Void)
23693 then
23694 declare
23695 Typ : constant Entity_Id := Etype (E);
23697 begin
23698 if Ekind_In (Typ, E_Anonymous_Access_Type,
23699 E_Anonymous_Access_Subprogram_Type)
23700 and then not Has_Convention_Pragma (Typ)
23701 then
23702 Basic_Set_Convention (Typ, Val);
23703 Set_Has_Convention_Pragma (Typ);
23705 -- And for the access subprogram type, deal similarly with the
23706 -- designated E_Subprogram_Type, which is always internal.
23708 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
23709 declare
23710 Dtype : constant Entity_Id := Designated_Type (Typ);
23711 begin
23712 if Ekind (Dtype) = E_Subprogram_Type
23713 and then not Has_Convention_Pragma (Dtype)
23714 then
23715 Basic_Set_Convention (Dtype, Val);
23716 Set_Has_Convention_Pragma (Dtype);
23717 end if;
23718 end;
23719 end if;
23720 end if;
23721 end;
23722 end if;
23723 end Set_Convention;
23725 ------------------------
23726 -- Set_Current_Entity --
23727 ------------------------
23729 -- The given entity is to be set as the currently visible definition of its
23730 -- associated name (i.e. the Node_Id associated with its name). All we have
23731 -- to do is to get the name from the identifier, and then set the
23732 -- associated Node_Id to point to the given entity.
23734 procedure Set_Current_Entity (E : Entity_Id) is
23735 begin
23736 Set_Name_Entity_Id (Chars (E), E);
23737 end Set_Current_Entity;
23739 ---------------------------
23740 -- Set_Debug_Info_Needed --
23741 ---------------------------
23743 procedure Set_Debug_Info_Needed (T : Entity_Id) is
23745 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
23746 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
23747 -- Used to set debug info in a related node if not set already
23749 --------------------------------------
23750 -- Set_Debug_Info_Needed_If_Not_Set --
23751 --------------------------------------
23753 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
23754 begin
23755 if Present (E) and then not Needs_Debug_Info (E) then
23756 Set_Debug_Info_Needed (E);
23758 -- For a private type, indicate that the full view also needs
23759 -- debug information.
23761 if Is_Type (E)
23762 and then Is_Private_Type (E)
23763 and then Present (Full_View (E))
23764 then
23765 Set_Debug_Info_Needed (Full_View (E));
23766 end if;
23767 end if;
23768 end Set_Debug_Info_Needed_If_Not_Set;
23770 -- Start of processing for Set_Debug_Info_Needed
23772 begin
23773 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
23774 -- indicates that Debug_Info_Needed is never required for the entity.
23775 -- Nothing to do if entity comes from a predefined file. Library files
23776 -- are compiled without debug information, but inlined bodies of these
23777 -- routines may appear in user code, and debug information on them ends
23778 -- up complicating debugging the user code.
23780 if No (T)
23781 or else Debug_Info_Off (T)
23782 then
23783 return;
23785 elsif In_Inlined_Body and then In_Predefined_Unit (T) then
23786 Set_Needs_Debug_Info (T, False);
23787 end if;
23789 -- Set flag in entity itself. Note that we will go through the following
23790 -- circuitry even if the flag is already set on T. That's intentional,
23791 -- it makes sure that the flag will be set in subsidiary entities.
23793 Set_Needs_Debug_Info (T);
23795 -- Set flag on subsidiary entities if not set already
23797 if Is_Object (T) then
23798 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
23800 elsif Is_Type (T) then
23801 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
23803 if Is_Record_Type (T) then
23804 declare
23805 Ent : Entity_Id := First_Entity (T);
23806 begin
23807 while Present (Ent) loop
23808 Set_Debug_Info_Needed_If_Not_Set (Ent);
23809 Next_Entity (Ent);
23810 end loop;
23811 end;
23813 -- For a class wide subtype, we also need debug information
23814 -- for the equivalent type.
23816 if Ekind (T) = E_Class_Wide_Subtype then
23817 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
23818 end if;
23820 elsif Is_Array_Type (T) then
23821 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
23823 declare
23824 Indx : Node_Id := First_Index (T);
23825 begin
23826 while Present (Indx) loop
23827 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
23828 Indx := Next_Index (Indx);
23829 end loop;
23830 end;
23832 -- For a packed array type, we also need debug information for
23833 -- the type used to represent the packed array. Conversely, we
23834 -- also need it for the former if we need it for the latter.
23836 if Is_Packed (T) then
23837 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
23838 end if;
23840 if Is_Packed_Array_Impl_Type (T) then
23841 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
23842 end if;
23844 elsif Is_Access_Type (T) then
23845 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
23847 elsif Is_Private_Type (T) then
23848 declare
23849 FV : constant Entity_Id := Full_View (T);
23851 begin
23852 Set_Debug_Info_Needed_If_Not_Set (FV);
23854 -- If the full view is itself a derived private type, we need
23855 -- debug information on its underlying type.
23857 if Present (FV)
23858 and then Is_Private_Type (FV)
23859 and then Present (Underlying_Full_View (FV))
23860 then
23861 Set_Needs_Debug_Info (Underlying_Full_View (FV));
23862 end if;
23863 end;
23865 elsif Is_Protected_Type (T) then
23866 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
23868 elsif Is_Scalar_Type (T) then
23870 -- If the subrange bounds are materialized by dedicated constant
23871 -- objects, also include them in the debug info to make sure the
23872 -- debugger can properly use them.
23874 if Present (Scalar_Range (T))
23875 and then Nkind (Scalar_Range (T)) = N_Range
23876 then
23877 declare
23878 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
23879 High_Bnd : constant Node_Id := Type_High_Bound (T);
23881 begin
23882 if Is_Entity_Name (Low_Bnd) then
23883 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
23884 end if;
23886 if Is_Entity_Name (High_Bnd) then
23887 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
23888 end if;
23889 end;
23890 end if;
23891 end if;
23892 end if;
23893 end Set_Debug_Info_Needed;
23895 ----------------------------
23896 -- Set_Entity_With_Checks --
23897 ----------------------------
23899 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
23900 Val_Actual : Entity_Id;
23901 Nod : Node_Id;
23902 Post_Node : Node_Id;
23904 begin
23905 -- Unconditionally set the entity
23907 Set_Entity (N, Val);
23909 -- The node to post on is the selector in the case of an expanded name,
23910 -- and otherwise the node itself.
23912 if Nkind (N) = N_Expanded_Name then
23913 Post_Node := Selector_Name (N);
23914 else
23915 Post_Node := N;
23916 end if;
23918 -- Check for violation of No_Fixed_IO
23920 if Restriction_Check_Required (No_Fixed_IO)
23921 and then
23922 ((RTU_Loaded (Ada_Text_IO)
23923 and then (Is_RTE (Val, RE_Decimal_IO)
23924 or else
23925 Is_RTE (Val, RE_Fixed_IO)))
23927 or else
23928 (RTU_Loaded (Ada_Wide_Text_IO)
23929 and then (Is_RTE (Val, RO_WT_Decimal_IO)
23930 or else
23931 Is_RTE (Val, RO_WT_Fixed_IO)))
23933 or else
23934 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
23935 and then (Is_RTE (Val, RO_WW_Decimal_IO)
23936 or else
23937 Is_RTE (Val, RO_WW_Fixed_IO))))
23939 -- A special extra check, don't complain about a reference from within
23940 -- the Ada.Interrupts package itself!
23942 and then not In_Same_Extended_Unit (N, Val)
23943 then
23944 Check_Restriction (No_Fixed_IO, Post_Node);
23945 end if;
23947 -- Remaining checks are only done on source nodes. Note that we test
23948 -- for violation of No_Fixed_IO even on non-source nodes, because the
23949 -- cases for checking violations of this restriction are instantiations
23950 -- where the reference in the instance has Comes_From_Source False.
23952 if not Comes_From_Source (N) then
23953 return;
23954 end if;
23956 -- Check for violation of No_Abort_Statements, which is triggered by
23957 -- call to Ada.Task_Identification.Abort_Task.
23959 if Restriction_Check_Required (No_Abort_Statements)
23960 and then (Is_RTE (Val, RE_Abort_Task))
23962 -- A special extra check, don't complain about a reference from within
23963 -- the Ada.Task_Identification package itself!
23965 and then not In_Same_Extended_Unit (N, Val)
23966 then
23967 Check_Restriction (No_Abort_Statements, Post_Node);
23968 end if;
23970 if Val = Standard_Long_Long_Integer then
23971 Check_Restriction (No_Long_Long_Integers, Post_Node);
23972 end if;
23974 -- Check for violation of No_Dynamic_Attachment
23976 if Restriction_Check_Required (No_Dynamic_Attachment)
23977 and then RTU_Loaded (Ada_Interrupts)
23978 and then (Is_RTE (Val, RE_Is_Reserved) or else
23979 Is_RTE (Val, RE_Is_Attached) or else
23980 Is_RTE (Val, RE_Current_Handler) or else
23981 Is_RTE (Val, RE_Attach_Handler) or else
23982 Is_RTE (Val, RE_Exchange_Handler) or else
23983 Is_RTE (Val, RE_Detach_Handler) or else
23984 Is_RTE (Val, RE_Reference))
23986 -- A special extra check, don't complain about a reference from within
23987 -- the Ada.Interrupts package itself!
23989 and then not In_Same_Extended_Unit (N, Val)
23990 then
23991 Check_Restriction (No_Dynamic_Attachment, Post_Node);
23992 end if;
23994 -- Check for No_Implementation_Identifiers
23996 if Restriction_Check_Required (No_Implementation_Identifiers) then
23998 -- We have an implementation defined entity if it is marked as
23999 -- implementation defined, or is defined in a package marked as
24000 -- implementation defined. However, library packages themselves
24001 -- are excluded (we don't want to flag Interfaces itself, just
24002 -- the entities within it).
24004 if (Is_Implementation_Defined (Val)
24005 or else
24006 (Present (Scope (Val))
24007 and then Is_Implementation_Defined (Scope (Val))))
24008 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
24009 and then Is_Library_Level_Entity (Val))
24010 then
24011 Check_Restriction (No_Implementation_Identifiers, Post_Node);
24012 end if;
24013 end if;
24015 -- Do the style check
24017 if Style_Check
24018 and then not Suppress_Style_Checks (Val)
24019 and then not In_Instance
24020 then
24021 if Nkind (N) = N_Identifier then
24022 Nod := N;
24023 elsif Nkind (N) = N_Expanded_Name then
24024 Nod := Selector_Name (N);
24025 else
24026 return;
24027 end if;
24029 -- A special situation arises for derived operations, where we want
24030 -- to do the check against the parent (since the Sloc of the derived
24031 -- operation points to the derived type declaration itself).
24033 Val_Actual := Val;
24034 while not Comes_From_Source (Val_Actual)
24035 and then Nkind (Val_Actual) in N_Entity
24036 and then (Ekind (Val_Actual) = E_Enumeration_Literal
24037 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
24038 and then Present (Alias (Val_Actual))
24039 loop
24040 Val_Actual := Alias (Val_Actual);
24041 end loop;
24043 -- Renaming declarations for generic actuals do not come from source,
24044 -- and have a different name from that of the entity they rename, so
24045 -- there is no style check to perform here.
24047 if Chars (Nod) = Chars (Val_Actual) then
24048 Style.Check_Identifier (Nod, Val_Actual);
24049 end if;
24050 end if;
24052 Set_Entity (N, Val);
24053 end Set_Entity_With_Checks;
24055 ------------------------------
24056 -- Set_Invalid_Scalar_Value --
24057 ------------------------------
24059 procedure Set_Invalid_Scalar_Value
24060 (Scal_Typ : Float_Scalar_Id;
24061 Value : Ureal)
24063 Slot : Ureal renames Invalid_Floats (Scal_Typ);
24065 begin
24066 -- Detect an attempt to set a different value for the same scalar type
24068 pragma Assert (Slot = No_Ureal);
24069 Slot := Value;
24070 end Set_Invalid_Scalar_Value;
24072 ------------------------------
24073 -- Set_Invalid_Scalar_Value --
24074 ------------------------------
24076 procedure Set_Invalid_Scalar_Value
24077 (Scal_Typ : Integer_Scalar_Id;
24078 Value : Uint)
24080 Slot : Uint renames Invalid_Integers (Scal_Typ);
24082 begin
24083 -- Detect an attempt to set a different value for the same scalar type
24085 pragma Assert (Slot = No_Uint);
24086 Slot := Value;
24087 end Set_Invalid_Scalar_Value;
24089 ------------------------
24090 -- Set_Name_Entity_Id --
24091 ------------------------
24093 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
24094 begin
24095 Set_Name_Table_Int (Id, Int (Val));
24096 end Set_Name_Entity_Id;
24098 ---------------------
24099 -- Set_Next_Actual --
24100 ---------------------
24102 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
24103 begin
24104 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
24105 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
24106 end if;
24107 end Set_Next_Actual;
24109 ----------------------------------
24110 -- Set_Optimize_Alignment_Flags --
24111 ----------------------------------
24113 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
24114 begin
24115 if Optimize_Alignment = 'S' then
24116 Set_Optimize_Alignment_Space (E);
24117 elsif Optimize_Alignment = 'T' then
24118 Set_Optimize_Alignment_Time (E);
24119 end if;
24120 end Set_Optimize_Alignment_Flags;
24122 -----------------------
24123 -- Set_Public_Status --
24124 -----------------------
24126 procedure Set_Public_Status (Id : Entity_Id) is
24127 S : constant Entity_Id := Current_Scope;
24129 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
24130 -- Determines if E is defined within handled statement sequence or
24131 -- an if statement, returns True if so, False otherwise.
24133 ----------------------
24134 -- Within_HSS_Or_If --
24135 ----------------------
24137 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
24138 N : Node_Id;
24139 begin
24140 N := Declaration_Node (E);
24141 loop
24142 N := Parent (N);
24144 if No (N) then
24145 return False;
24147 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
24148 N_If_Statement)
24149 then
24150 return True;
24151 end if;
24152 end loop;
24153 end Within_HSS_Or_If;
24155 -- Start of processing for Set_Public_Status
24157 begin
24158 -- Everything in the scope of Standard is public
24160 if S = Standard_Standard then
24161 Set_Is_Public (Id);
24163 -- Entity is definitely not public if enclosing scope is not public
24165 elsif not Is_Public (S) then
24166 return;
24168 -- An object or function declaration that occurs in a handled sequence
24169 -- of statements or within an if statement is the declaration for a
24170 -- temporary object or local subprogram generated by the expander. It
24171 -- never needs to be made public and furthermore, making it public can
24172 -- cause back end problems.
24174 elsif Nkind_In (Parent (Id), N_Object_Declaration,
24175 N_Function_Specification)
24176 and then Within_HSS_Or_If (Id)
24177 then
24178 return;
24180 -- Entities in public packages or records are public
24182 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
24183 Set_Is_Public (Id);
24185 -- The bounds of an entry family declaration can generate object
24186 -- declarations that are visible to the back-end, e.g. in the
24187 -- the declaration of a composite type that contains tasks.
24189 elsif Is_Concurrent_Type (S)
24190 and then not Has_Completion (S)
24191 and then Nkind (Parent (Id)) = N_Object_Declaration
24192 then
24193 Set_Is_Public (Id);
24194 end if;
24195 end Set_Public_Status;
24197 -----------------------------
24198 -- Set_Referenced_Modified --
24199 -----------------------------
24201 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
24202 Pref : Node_Id;
24204 begin
24205 -- Deal with indexed or selected component where prefix is modified
24207 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
24208 Pref := Prefix (N);
24210 -- If prefix is access type, then it is the designated object that is
24211 -- being modified, which means we have no entity to set the flag on.
24213 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
24214 return;
24216 -- Otherwise chase the prefix
24218 else
24219 Set_Referenced_Modified (Pref, Out_Param);
24220 end if;
24222 -- Otherwise see if we have an entity name (only other case to process)
24224 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
24225 Set_Referenced_As_LHS (Entity (N), not Out_Param);
24226 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
24227 end if;
24228 end Set_Referenced_Modified;
24230 ------------------
24231 -- Set_Rep_Info --
24232 ------------------
24234 procedure Set_Rep_Info (T1 : Entity_Id; T2 : Entity_Id) is
24235 begin
24236 Set_Is_Atomic (T1, Is_Atomic (T2));
24237 Set_Is_Independent (T1, Is_Independent (T2));
24238 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
24240 if Is_Base_Type (T1) then
24241 Set_Is_Volatile (T1, Is_Volatile (T2));
24242 end if;
24243 end Set_Rep_Info;
24245 ----------------------------
24246 -- Set_Scope_Is_Transient --
24247 ----------------------------
24249 procedure Set_Scope_Is_Transient (V : Boolean := True) is
24250 begin
24251 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
24252 end Set_Scope_Is_Transient;
24254 -------------------
24255 -- Set_Size_Info --
24256 -------------------
24258 procedure Set_Size_Info (T1, T2 : Entity_Id) is
24259 begin
24260 -- We copy Esize, but not RM_Size, since in general RM_Size is
24261 -- subtype specific and does not get inherited by all subtypes.
24263 Set_Esize (T1, Esize (T2));
24264 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
24266 if Is_Discrete_Or_Fixed_Point_Type (T1)
24267 and then
24268 Is_Discrete_Or_Fixed_Point_Type (T2)
24269 then
24270 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
24271 end if;
24273 Set_Alignment (T1, Alignment (T2));
24274 end Set_Size_Info;
24276 ------------------------------
24277 -- Should_Ignore_Pragma_Par --
24278 ------------------------------
24280 function Should_Ignore_Pragma_Par (Prag_Name : Name_Id) return Boolean is
24281 pragma Assert (Compiler_State = Parsing);
24282 -- This one can't work during semantic analysis, because we don't have a
24283 -- correct Current_Source_File.
24285 Result : constant Boolean :=
24286 Get_Name_Table_Boolean3 (Prag_Name)
24287 and then not Is_Internal_File_Name
24288 (File_Name (Current_Source_File));
24289 begin
24290 return Result;
24291 end Should_Ignore_Pragma_Par;
24293 ------------------------------
24294 -- Should_Ignore_Pragma_Sem --
24295 ------------------------------
24297 function Should_Ignore_Pragma_Sem (N : Node_Id) return Boolean is
24298 pragma Assert (Compiler_State = Analyzing);
24299 Prag_Name : constant Name_Id := Pragma_Name (N);
24300 Result : constant Boolean :=
24301 Get_Name_Table_Boolean3 (Prag_Name)
24302 and then not In_Internal_Unit (N);
24304 begin
24305 return Result;
24306 end Should_Ignore_Pragma_Sem;
24308 --------------------
24309 -- Static_Boolean --
24310 --------------------
24312 function Static_Boolean (N : Node_Id) return Uint is
24313 begin
24314 Analyze_And_Resolve (N, Standard_Boolean);
24316 if N = Error
24317 or else Error_Posted (N)
24318 or else Etype (N) = Any_Type
24319 then
24320 return No_Uint;
24321 end if;
24323 if Is_OK_Static_Expression (N) then
24324 if not Raises_Constraint_Error (N) then
24325 return Expr_Value (N);
24326 else
24327 return No_Uint;
24328 end if;
24330 elsif Etype (N) = Any_Type then
24331 return No_Uint;
24333 else
24334 Flag_Non_Static_Expr
24335 ("static boolean expression required here", N);
24336 return No_Uint;
24337 end if;
24338 end Static_Boolean;
24340 --------------------
24341 -- Static_Integer --
24342 --------------------
24344 function Static_Integer (N : Node_Id) return Uint is
24345 begin
24346 Analyze_And_Resolve (N, Any_Integer);
24348 if N = Error
24349 or else Error_Posted (N)
24350 or else Etype (N) = Any_Type
24351 then
24352 return No_Uint;
24353 end if;
24355 if Is_OK_Static_Expression (N) then
24356 if not Raises_Constraint_Error (N) then
24357 return Expr_Value (N);
24358 else
24359 return No_Uint;
24360 end if;
24362 elsif Etype (N) = Any_Type then
24363 return No_Uint;
24365 else
24366 Flag_Non_Static_Expr
24367 ("static integer expression required here", N);
24368 return No_Uint;
24369 end if;
24370 end Static_Integer;
24372 --------------------------
24373 -- Statically_Different --
24374 --------------------------
24376 function Statically_Different (E1, E2 : Node_Id) return Boolean is
24377 R1 : constant Node_Id := Get_Referenced_Object (E1);
24378 R2 : constant Node_Id := Get_Referenced_Object (E2);
24379 begin
24380 return Is_Entity_Name (R1)
24381 and then Is_Entity_Name (R2)
24382 and then Entity (R1) /= Entity (R2)
24383 and then not Is_Formal (Entity (R1))
24384 and then not Is_Formal (Entity (R2));
24385 end Statically_Different;
24387 --------------------------------------
24388 -- Subject_To_Loop_Entry_Attributes --
24389 --------------------------------------
24391 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
24392 Stmt : Node_Id;
24394 begin
24395 Stmt := N;
24397 -- The expansion mechanism transform a loop subject to at least one
24398 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
24399 -- the conditional part.
24401 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
24402 and then Nkind (Original_Node (N)) = N_Loop_Statement
24403 then
24404 Stmt := Original_Node (N);
24405 end if;
24407 return
24408 Nkind (Stmt) = N_Loop_Statement
24409 and then Present (Identifier (Stmt))
24410 and then Present (Entity (Identifier (Stmt)))
24411 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
24412 end Subject_To_Loop_Entry_Attributes;
24414 -----------------------------
24415 -- Subprogram_Access_Level --
24416 -----------------------------
24418 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
24419 begin
24420 if Present (Alias (Subp)) then
24421 return Subprogram_Access_Level (Alias (Subp));
24422 else
24423 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
24424 end if;
24425 end Subprogram_Access_Level;
24427 ---------------------
24428 -- Subprogram_Name --
24429 ---------------------
24431 function Subprogram_Name (N : Node_Id) return String is
24432 Buf : Bounded_String;
24433 Ent : Node_Id := N;
24434 Nod : Node_Id;
24436 begin
24437 while Present (Ent) loop
24438 case Nkind (Ent) is
24439 when N_Subprogram_Body =>
24440 Ent := Defining_Unit_Name (Specification (Ent));
24441 exit;
24443 when N_Subprogram_Declaration =>
24444 Nod := Corresponding_Body (Ent);
24446 if Present (Nod) then
24447 Ent := Nod;
24448 else
24449 Ent := Defining_Unit_Name (Specification (Ent));
24450 end if;
24452 exit;
24454 when N_Subprogram_Instantiation
24455 | N_Package_Body
24456 | N_Package_Specification
24458 Ent := Defining_Unit_Name (Ent);
24459 exit;
24461 when N_Protected_Type_Declaration =>
24462 Ent := Corresponding_Body (Ent);
24463 exit;
24465 when N_Protected_Body
24466 | N_Task_Body
24468 Ent := Defining_Identifier (Ent);
24469 exit;
24471 when others =>
24472 null;
24473 end case;
24475 Ent := Parent (Ent);
24476 end loop;
24478 if No (Ent) then
24479 return "unknown subprogram:unknown file:0:0";
24480 end if;
24482 -- If the subprogram is a child unit, use its simple name to start the
24483 -- construction of the fully qualified name.
24485 if Nkind (Ent) = N_Defining_Program_Unit_Name then
24486 Ent := Defining_Identifier (Ent);
24487 end if;
24489 Append_Entity_Name (Buf, Ent);
24491 -- Append homonym number if needed
24493 if Nkind (N) in N_Entity and then Has_Homonym (N) then
24494 declare
24495 H : Entity_Id := Homonym (N);
24496 Nr : Nat := 1;
24498 begin
24499 while Present (H) loop
24500 if Scope (H) = Scope (N) then
24501 Nr := Nr + 1;
24502 end if;
24504 H := Homonym (H);
24505 end loop;
24507 if Nr > 1 then
24508 Append (Buf, '#');
24509 Append (Buf, Nr);
24510 end if;
24511 end;
24512 end if;
24514 -- Append source location of Ent to Buf so that the string will
24515 -- look like "subp:file:line:col".
24517 declare
24518 Loc : constant Source_Ptr := Sloc (Ent);
24519 begin
24520 Append (Buf, ':');
24521 Append (Buf, Reference_Name (Get_Source_File_Index (Loc)));
24522 Append (Buf, ':');
24523 Append (Buf, Nat (Get_Logical_Line_Number (Loc)));
24524 Append (Buf, ':');
24525 Append (Buf, Nat (Get_Column_Number (Loc)));
24526 end;
24528 return +Buf;
24529 end Subprogram_Name;
24531 -------------------------------
24532 -- Support_Atomic_Primitives --
24533 -------------------------------
24535 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
24536 Size : Int;
24538 begin
24539 -- Verify the alignment of Typ is known
24541 if not Known_Alignment (Typ) then
24542 return False;
24543 end if;
24545 if Known_Static_Esize (Typ) then
24546 Size := UI_To_Int (Esize (Typ));
24548 -- If the Esize (Object_Size) is unknown at compile time, look at the
24549 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
24551 elsif Known_Static_RM_Size (Typ) then
24552 Size := UI_To_Int (RM_Size (Typ));
24554 -- Otherwise, the size is considered to be unknown.
24556 else
24557 return False;
24558 end if;
24560 -- Check that the size of the component is 8, 16, 32, or 64 bits and
24561 -- that Typ is properly aligned.
24563 case Size is
24564 when 8 | 16 | 32 | 64 =>
24565 return Size = UI_To_Int (Alignment (Typ)) * 8;
24567 when others =>
24568 return False;
24569 end case;
24570 end Support_Atomic_Primitives;
24572 -----------------
24573 -- Trace_Scope --
24574 -----------------
24576 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
24577 begin
24578 if Debug_Flag_W then
24579 for J in 0 .. Scope_Stack.Last loop
24580 Write_Str (" ");
24581 end loop;
24583 Write_Str (Msg);
24584 Write_Name (Chars (E));
24585 Write_Str (" from ");
24586 Write_Location (Sloc (N));
24587 Write_Eol;
24588 end if;
24589 end Trace_Scope;
24591 -----------------------
24592 -- Transfer_Entities --
24593 -----------------------
24595 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
24596 procedure Set_Public_Status_Of (Id : Entity_Id);
24597 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
24598 -- Set_Public_Status. If successful and Id denotes a record type, set
24599 -- the Is_Public attribute of its fields.
24601 --------------------------
24602 -- Set_Public_Status_Of --
24603 --------------------------
24605 procedure Set_Public_Status_Of (Id : Entity_Id) is
24606 Field : Entity_Id;
24608 begin
24609 if not Is_Public (Id) then
24610 Set_Public_Status (Id);
24612 -- When the input entity is a public record type, ensure that all
24613 -- its internal fields are also exposed to the linker. The fields
24614 -- of a class-wide type are never made public.
24616 if Is_Public (Id)
24617 and then Is_Record_Type (Id)
24618 and then not Is_Class_Wide_Type (Id)
24619 then
24620 Field := First_Entity (Id);
24621 while Present (Field) loop
24622 Set_Is_Public (Field);
24623 Next_Entity (Field);
24624 end loop;
24625 end if;
24626 end if;
24627 end Set_Public_Status_Of;
24629 -- Local variables
24631 Full_Id : Entity_Id;
24632 Id : Entity_Id;
24634 -- Start of processing for Transfer_Entities
24636 begin
24637 Id := First_Entity (From);
24639 if Present (Id) then
24641 -- Merge the entity chain of the source scope with that of the
24642 -- destination scope.
24644 if Present (Last_Entity (To)) then
24645 Link_Entities (Last_Entity (To), Id);
24646 else
24647 Set_First_Entity (To, Id);
24648 end if;
24650 Set_Last_Entity (To, Last_Entity (From));
24652 -- Inspect the entities of the source scope and update their Scope
24653 -- attribute.
24655 while Present (Id) loop
24656 Set_Scope (Id, To);
24657 Set_Public_Status_Of (Id);
24659 -- Handle an internally generated full view for a private type
24661 if Is_Private_Type (Id)
24662 and then Present (Full_View (Id))
24663 and then Is_Itype (Full_View (Id))
24664 then
24665 Full_Id := Full_View (Id);
24667 Set_Scope (Full_Id, To);
24668 Set_Public_Status_Of (Full_Id);
24669 end if;
24671 Next_Entity (Id);
24672 end loop;
24674 Set_First_Entity (From, Empty);
24675 Set_Last_Entity (From, Empty);
24676 end if;
24677 end Transfer_Entities;
24679 -----------------------
24680 -- Type_Access_Level --
24681 -----------------------
24683 function Type_Access_Level (Typ : Entity_Id) return Uint is
24684 Btyp : Entity_Id;
24686 begin
24687 Btyp := Base_Type (Typ);
24689 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
24690 -- simply use the level where the type is declared. This is true for
24691 -- stand-alone object declarations, and for anonymous access types
24692 -- associated with components the level is the same as that of the
24693 -- enclosing composite type. However, special treatment is needed for
24694 -- the cases of access parameters, return objects of an anonymous access
24695 -- type, and, in Ada 95, access discriminants of limited types.
24697 if Is_Access_Type (Btyp) then
24698 if Ekind (Btyp) = E_Anonymous_Access_Type then
24700 -- If the type is a nonlocal anonymous access type (such as for
24701 -- an access parameter) we treat it as being declared at the
24702 -- library level to ensure that names such as X.all'access don't
24703 -- fail static accessibility checks.
24705 if not Is_Local_Anonymous_Access (Typ) then
24706 return Scope_Depth (Standard_Standard);
24708 -- If this is a return object, the accessibility level is that of
24709 -- the result subtype of the enclosing function. The test here is
24710 -- little complicated, because we have to account for extended
24711 -- return statements that have been rewritten as blocks, in which
24712 -- case we have to find and the Is_Return_Object attribute of the
24713 -- itype's associated object. It would be nice to find a way to
24714 -- simplify this test, but it doesn't seem worthwhile to add a new
24715 -- flag just for purposes of this test. ???
24717 elsif Ekind (Scope (Btyp)) = E_Return_Statement
24718 or else
24719 (Is_Itype (Btyp)
24720 and then Nkind (Associated_Node_For_Itype (Btyp)) =
24721 N_Object_Declaration
24722 and then Is_Return_Object
24723 (Defining_Identifier
24724 (Associated_Node_For_Itype (Btyp))))
24725 then
24726 declare
24727 Scop : Entity_Id;
24729 begin
24730 Scop := Scope (Scope (Btyp));
24731 while Present (Scop) loop
24732 exit when Ekind (Scop) = E_Function;
24733 Scop := Scope (Scop);
24734 end loop;
24736 -- Treat the return object's type as having the level of the
24737 -- function's result subtype (as per RM05-6.5(5.3/2)).
24739 return Type_Access_Level (Etype (Scop));
24740 end;
24741 end if;
24742 end if;
24744 Btyp := Root_Type (Btyp);
24746 -- The accessibility level of anonymous access types associated with
24747 -- discriminants is that of the current instance of the type, and
24748 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
24750 -- AI-402: access discriminants have accessibility based on the
24751 -- object rather than the type in Ada 2005, so the above paragraph
24752 -- doesn't apply.
24754 -- ??? Needs completion with rules from AI-416
24756 if Ada_Version <= Ada_95
24757 and then Ekind (Typ) = E_Anonymous_Access_Type
24758 and then Present (Associated_Node_For_Itype (Typ))
24759 and then Nkind (Associated_Node_For_Itype (Typ)) =
24760 N_Discriminant_Specification
24761 then
24762 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
24763 end if;
24764 end if;
24766 -- Return library level for a generic formal type. This is done because
24767 -- RM(10.3.2) says that "The statically deeper relationship does not
24768 -- apply to ... a descendant of a generic formal type". Rather than
24769 -- checking at each point where a static accessibility check is
24770 -- performed to see if we are dealing with a formal type, this rule is
24771 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
24772 -- return extreme values for a formal type; Deepest_Type_Access_Level
24773 -- returns Int'Last. By calling the appropriate function from among the
24774 -- two, we ensure that the static accessibility check will pass if we
24775 -- happen to run into a formal type. More specifically, we should call
24776 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
24777 -- call occurs as part of a static accessibility check and the error
24778 -- case is the case where the type's level is too shallow (as opposed
24779 -- to too deep).
24781 if Is_Generic_Type (Root_Type (Btyp)) then
24782 return Scope_Depth (Standard_Standard);
24783 end if;
24785 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
24786 end Type_Access_Level;
24788 ------------------------------------
24789 -- Type_Without_Stream_Operation --
24790 ------------------------------------
24792 function Type_Without_Stream_Operation
24793 (T : Entity_Id;
24794 Op : TSS_Name_Type := TSS_Null) return Entity_Id
24796 BT : constant Entity_Id := Base_Type (T);
24797 Op_Missing : Boolean;
24799 begin
24800 if not Restriction_Active (No_Default_Stream_Attributes) then
24801 return Empty;
24802 end if;
24804 if Is_Elementary_Type (T) then
24805 if Op = TSS_Null then
24806 Op_Missing :=
24807 No (TSS (BT, TSS_Stream_Read))
24808 or else No (TSS (BT, TSS_Stream_Write));
24810 else
24811 Op_Missing := No (TSS (BT, Op));
24812 end if;
24814 if Op_Missing then
24815 return T;
24816 else
24817 return Empty;
24818 end if;
24820 elsif Is_Array_Type (T) then
24821 return Type_Without_Stream_Operation (Component_Type (T), Op);
24823 elsif Is_Record_Type (T) then
24824 declare
24825 Comp : Entity_Id;
24826 C_Typ : Entity_Id;
24828 begin
24829 Comp := First_Component (T);
24830 while Present (Comp) loop
24831 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
24833 if Present (C_Typ) then
24834 return C_Typ;
24835 end if;
24837 Next_Component (Comp);
24838 end loop;
24840 return Empty;
24841 end;
24843 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
24844 return Type_Without_Stream_Operation (Full_View (T), Op);
24845 else
24846 return Empty;
24847 end if;
24848 end Type_Without_Stream_Operation;
24850 ----------------------------
24851 -- Unique_Defining_Entity --
24852 ----------------------------
24854 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
24855 begin
24856 return Unique_Entity (Defining_Entity (N));
24857 end Unique_Defining_Entity;
24859 -------------------
24860 -- Unique_Entity --
24861 -------------------
24863 function Unique_Entity (E : Entity_Id) return Entity_Id is
24864 U : Entity_Id := E;
24865 P : Node_Id;
24867 begin
24868 case Ekind (E) is
24869 when E_Constant =>
24870 if Present (Full_View (E)) then
24871 U := Full_View (E);
24872 end if;
24874 when Entry_Kind =>
24875 if Nkind (Parent (E)) = N_Entry_Body then
24876 declare
24877 Prot_Item : Entity_Id;
24878 Prot_Type : Entity_Id;
24880 begin
24881 if Ekind (E) = E_Entry then
24882 Prot_Type := Scope (E);
24884 -- Bodies of entry families are nested within an extra scope
24885 -- that contains an entry index declaration.
24887 else
24888 Prot_Type := Scope (Scope (E));
24889 end if;
24891 -- A protected type may be declared as a private type, in
24892 -- which case we need to get its full view.
24894 if Is_Private_Type (Prot_Type) then
24895 Prot_Type := Full_View (Prot_Type);
24896 end if;
24898 -- Full view may not be present on error, in which case
24899 -- return E by default.
24901 if Present (Prot_Type) then
24902 pragma Assert (Ekind (Prot_Type) = E_Protected_Type);
24904 -- Traverse the entity list of the protected type and
24905 -- locate an entry declaration which matches the entry
24906 -- body.
24908 Prot_Item := First_Entity (Prot_Type);
24909 while Present (Prot_Item) loop
24910 if Ekind (Prot_Item) in Entry_Kind
24911 and then Corresponding_Body (Parent (Prot_Item)) = E
24912 then
24913 U := Prot_Item;
24914 exit;
24915 end if;
24917 Next_Entity (Prot_Item);
24918 end loop;
24919 end if;
24920 end;
24921 end if;
24923 when Formal_Kind =>
24924 if Present (Spec_Entity (E)) then
24925 U := Spec_Entity (E);
24926 end if;
24928 when E_Package_Body =>
24929 P := Parent (E);
24931 if Nkind (P) = N_Defining_Program_Unit_Name then
24932 P := Parent (P);
24933 end if;
24935 if Nkind (P) = N_Package_Body
24936 and then Present (Corresponding_Spec (P))
24937 then
24938 U := Corresponding_Spec (P);
24940 elsif Nkind (P) = N_Package_Body_Stub
24941 and then Present (Corresponding_Spec_Of_Stub (P))
24942 then
24943 U := Corresponding_Spec_Of_Stub (P);
24944 end if;
24946 when E_Protected_Body =>
24947 P := Parent (E);
24949 if Nkind (P) = N_Protected_Body
24950 and then Present (Corresponding_Spec (P))
24951 then
24952 U := Corresponding_Spec (P);
24954 elsif Nkind (P) = N_Protected_Body_Stub
24955 and then Present (Corresponding_Spec_Of_Stub (P))
24956 then
24957 U := Corresponding_Spec_Of_Stub (P);
24959 if Is_Single_Protected_Object (U) then
24960 U := Etype (U);
24961 end if;
24962 end if;
24964 if Is_Private_Type (U) then
24965 U := Full_View (U);
24966 end if;
24968 when E_Subprogram_Body =>
24969 P := Parent (E);
24971 if Nkind (P) = N_Defining_Program_Unit_Name then
24972 P := Parent (P);
24973 end if;
24975 P := Parent (P);
24977 if Nkind (P) = N_Subprogram_Body
24978 and then Present (Corresponding_Spec (P))
24979 then
24980 U := Corresponding_Spec (P);
24982 elsif Nkind (P) = N_Subprogram_Body_Stub
24983 and then Present (Corresponding_Spec_Of_Stub (P))
24984 then
24985 U := Corresponding_Spec_Of_Stub (P);
24987 elsif Nkind (P) = N_Subprogram_Renaming_Declaration then
24988 U := Corresponding_Spec (P);
24989 end if;
24991 when E_Task_Body =>
24992 P := Parent (E);
24994 if Nkind (P) = N_Task_Body
24995 and then Present (Corresponding_Spec (P))
24996 then
24997 U := Corresponding_Spec (P);
24999 elsif Nkind (P) = N_Task_Body_Stub
25000 and then Present (Corresponding_Spec_Of_Stub (P))
25001 then
25002 U := Corresponding_Spec_Of_Stub (P);
25004 if Is_Single_Task_Object (U) then
25005 U := Etype (U);
25006 end if;
25007 end if;
25009 if Is_Private_Type (U) then
25010 U := Full_View (U);
25011 end if;
25013 when Type_Kind =>
25014 if Present (Full_View (E)) then
25015 U := Full_View (E);
25016 end if;
25018 when others =>
25019 null;
25020 end case;
25022 return U;
25023 end Unique_Entity;
25025 -----------------
25026 -- Unique_Name --
25027 -----------------
25029 function Unique_Name (E : Entity_Id) return String is
25031 -- Names in E_Subprogram_Body or E_Package_Body entities are not
25032 -- reliable, as they may not include the overloading suffix. Instead,
25033 -- when looking for the name of E or one of its enclosing scope, we get
25034 -- the name of the corresponding Unique_Entity.
25036 U : constant Entity_Id := Unique_Entity (E);
25038 function This_Name return String;
25040 ---------------
25041 -- This_Name --
25042 ---------------
25044 function This_Name return String is
25045 begin
25046 return Get_Name_String (Chars (U));
25047 end This_Name;
25049 -- Start of processing for Unique_Name
25051 begin
25052 if E = Standard_Standard
25053 or else Has_Fully_Qualified_Name (E)
25054 then
25055 return This_Name;
25057 elsif Ekind (E) = E_Enumeration_Literal then
25058 return Unique_Name (Etype (E)) & "__" & This_Name;
25060 else
25061 declare
25062 S : constant Entity_Id := Scope (U);
25063 pragma Assert (Present (S));
25065 begin
25066 -- Prefix names of predefined types with standard__, but leave
25067 -- names of user-defined packages and subprograms without prefix
25068 -- (even if technically they are nested in the Standard package).
25070 if S = Standard_Standard then
25071 if Ekind (U) = E_Package or else Is_Subprogram (U) then
25072 return This_Name;
25073 else
25074 return Unique_Name (S) & "__" & This_Name;
25075 end if;
25077 -- For intances of generic subprograms use the name of the related
25078 -- instace and skip the scope of its wrapper package.
25080 elsif Is_Wrapper_Package (S) then
25081 pragma Assert (Scope (S) = Scope (Related_Instance (S)));
25082 -- Wrapper package and the instantiation are in the same scope
25084 declare
25085 Enclosing_Name : constant String :=
25086 Unique_Name (Scope (S)) & "__" &
25087 Get_Name_String (Chars (Related_Instance (S)));
25089 begin
25090 if Is_Subprogram (U)
25091 and then not Is_Generic_Actual_Subprogram (U)
25092 then
25093 return Enclosing_Name;
25094 else
25095 return Enclosing_Name & "__" & This_Name;
25096 end if;
25097 end;
25099 else
25100 return Unique_Name (S) & "__" & This_Name;
25101 end if;
25102 end;
25103 end if;
25104 end Unique_Name;
25106 ---------------------
25107 -- Unit_Is_Visible --
25108 ---------------------
25110 function Unit_Is_Visible (U : Entity_Id) return Boolean is
25111 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
25112 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
25114 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
25115 -- For a child unit, check whether unit appears in a with_clause
25116 -- of a parent.
25118 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
25119 -- Scan the context clause of one compilation unit looking for a
25120 -- with_clause for the unit in question.
25122 ----------------------------
25123 -- Unit_In_Parent_Context --
25124 ----------------------------
25126 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
25127 begin
25128 if Unit_In_Context (Par_Unit) then
25129 return True;
25131 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
25132 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
25134 else
25135 return False;
25136 end if;
25137 end Unit_In_Parent_Context;
25139 ---------------------
25140 -- Unit_In_Context --
25141 ---------------------
25143 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
25144 Clause : Node_Id;
25146 begin
25147 Clause := First (Context_Items (Comp_Unit));
25148 while Present (Clause) loop
25149 if Nkind (Clause) = N_With_Clause then
25150 if Library_Unit (Clause) = U then
25151 return True;
25153 -- The with_clause may denote a renaming of the unit we are
25154 -- looking for, eg. Text_IO which renames Ada.Text_IO.
25156 elsif
25157 Renamed_Entity (Entity (Name (Clause))) =
25158 Defining_Entity (Unit (U))
25159 then
25160 return True;
25161 end if;
25162 end if;
25164 Next (Clause);
25165 end loop;
25167 return False;
25168 end Unit_In_Context;
25170 -- Start of processing for Unit_Is_Visible
25172 begin
25173 -- The currrent unit is directly visible
25175 if Curr = U then
25176 return True;
25178 elsif Unit_In_Context (Curr) then
25179 return True;
25181 -- If the current unit is a body, check the context of the spec
25183 elsif Nkind (Unit (Curr)) = N_Package_Body
25184 or else
25185 (Nkind (Unit (Curr)) = N_Subprogram_Body
25186 and then not Acts_As_Spec (Unit (Curr)))
25187 then
25188 if Unit_In_Context (Library_Unit (Curr)) then
25189 return True;
25190 end if;
25191 end if;
25193 -- If the spec is a child unit, examine the parents
25195 if Is_Child_Unit (Curr_Entity) then
25196 if Nkind (Unit (Curr)) in N_Unit_Body then
25197 return
25198 Unit_In_Parent_Context
25199 (Parent_Spec (Unit (Library_Unit (Curr))));
25200 else
25201 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
25202 end if;
25204 else
25205 return False;
25206 end if;
25207 end Unit_Is_Visible;
25209 ------------------------------
25210 -- Universal_Interpretation --
25211 ------------------------------
25213 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
25214 Index : Interp_Index;
25215 It : Interp;
25217 begin
25218 -- The argument may be a formal parameter of an operator or subprogram
25219 -- with multiple interpretations, or else an expression for an actual.
25221 if Nkind (Opnd) = N_Defining_Identifier
25222 or else not Is_Overloaded (Opnd)
25223 then
25224 if Etype (Opnd) = Universal_Integer
25225 or else Etype (Opnd) = Universal_Real
25226 then
25227 return Etype (Opnd);
25228 else
25229 return Empty;
25230 end if;
25232 else
25233 Get_First_Interp (Opnd, Index, It);
25234 while Present (It.Typ) loop
25235 if It.Typ = Universal_Integer
25236 or else It.Typ = Universal_Real
25237 then
25238 return It.Typ;
25239 end if;
25241 Get_Next_Interp (Index, It);
25242 end loop;
25244 return Empty;
25245 end if;
25246 end Universal_Interpretation;
25248 ---------------
25249 -- Unqualify --
25250 ---------------
25252 function Unqualify (Expr : Node_Id) return Node_Id is
25253 begin
25254 -- Recurse to handle unlikely case of multiple levels of qualification
25256 if Nkind (Expr) = N_Qualified_Expression then
25257 return Unqualify (Expression (Expr));
25259 -- Normal case, not a qualified expression
25261 else
25262 return Expr;
25263 end if;
25264 end Unqualify;
25266 -----------------
25267 -- Unqual_Conv --
25268 -----------------
25270 function Unqual_Conv (Expr : Node_Id) return Node_Id is
25271 begin
25272 -- Recurse to handle unlikely case of multiple levels of qualification
25273 -- and/or conversion.
25275 if Nkind_In (Expr, N_Qualified_Expression,
25276 N_Type_Conversion,
25277 N_Unchecked_Type_Conversion)
25278 then
25279 return Unqual_Conv (Expression (Expr));
25281 -- Normal case, not a qualified expression
25283 else
25284 return Expr;
25285 end if;
25286 end Unqual_Conv;
25288 --------------------
25289 -- Validated_View --
25290 --------------------
25292 function Validated_View (Typ : Entity_Id) return Entity_Id is
25293 Continue : Boolean;
25294 Val_Typ : Entity_Id;
25296 begin
25297 Continue := True;
25298 Val_Typ := Base_Type (Typ);
25300 -- Obtain the full view of the input type by stripping away concurrency,
25301 -- derivations, and privacy.
25303 while Continue loop
25304 Continue := False;
25306 if Is_Concurrent_Type (Val_Typ) then
25307 if Present (Corresponding_Record_Type (Val_Typ)) then
25308 Continue := True;
25309 Val_Typ := Corresponding_Record_Type (Val_Typ);
25310 end if;
25312 elsif Is_Derived_Type (Val_Typ) then
25313 Continue := True;
25314 Val_Typ := Etype (Val_Typ);
25316 elsif Is_Private_Type (Val_Typ) then
25317 if Present (Underlying_Full_View (Val_Typ)) then
25318 Continue := True;
25319 Val_Typ := Underlying_Full_View (Val_Typ);
25321 elsif Present (Full_View (Val_Typ)) then
25322 Continue := True;
25323 Val_Typ := Full_View (Val_Typ);
25324 end if;
25325 end if;
25326 end loop;
25328 return Val_Typ;
25329 end Validated_View;
25331 -----------------------
25332 -- Visible_Ancestors --
25333 -----------------------
25335 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
25336 List_1 : Elist_Id;
25337 List_2 : Elist_Id;
25338 Elmt : Elmt_Id;
25340 begin
25341 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
25343 -- Collect all the parents and progenitors of Typ. If the full-view of
25344 -- private parents and progenitors is available then it is used to
25345 -- generate the list of visible ancestors; otherwise their partial
25346 -- view is added to the resulting list.
25348 Collect_Parents
25349 (T => Typ,
25350 List => List_1,
25351 Use_Full_View => True);
25353 Collect_Interfaces
25354 (T => Typ,
25355 Ifaces_List => List_2,
25356 Exclude_Parents => True,
25357 Use_Full_View => True);
25359 -- Join the two lists. Avoid duplications because an interface may
25360 -- simultaneously be parent and progenitor of a type.
25362 Elmt := First_Elmt (List_2);
25363 while Present (Elmt) loop
25364 Append_Unique_Elmt (Node (Elmt), List_1);
25365 Next_Elmt (Elmt);
25366 end loop;
25368 return List_1;
25369 end Visible_Ancestors;
25371 ----------------------
25372 -- Within_Init_Proc --
25373 ----------------------
25375 function Within_Init_Proc return Boolean is
25376 S : Entity_Id;
25378 begin
25379 S := Current_Scope;
25380 while not Is_Overloadable (S) loop
25381 if S = Standard_Standard then
25382 return False;
25383 else
25384 S := Scope (S);
25385 end if;
25386 end loop;
25388 return Is_Init_Proc (S);
25389 end Within_Init_Proc;
25391 ---------------------------
25392 -- Within_Protected_Type --
25393 ---------------------------
25395 function Within_Protected_Type (E : Entity_Id) return Boolean is
25396 Scop : Entity_Id := Scope (E);
25398 begin
25399 while Present (Scop) loop
25400 if Ekind (Scop) = E_Protected_Type then
25401 return True;
25402 end if;
25404 Scop := Scope (Scop);
25405 end loop;
25407 return False;
25408 end Within_Protected_Type;
25410 ------------------
25411 -- Within_Scope --
25412 ------------------
25414 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
25415 begin
25416 return Scope_Within_Or_Same (Scope (E), S);
25417 end Within_Scope;
25419 ----------------------------
25420 -- Within_Subprogram_Call --
25421 ----------------------------
25423 function Within_Subprogram_Call (N : Node_Id) return Boolean is
25424 Par : Node_Id;
25426 begin
25427 -- Climb the parent chain looking for a function or procedure call
25429 Par := N;
25430 while Present (Par) loop
25431 if Nkind_In (Par, N_Entry_Call_Statement,
25432 N_Function_Call,
25433 N_Procedure_Call_Statement)
25434 then
25435 return True;
25437 -- Prevent the search from going too far
25439 elsif Is_Body_Or_Package_Declaration (Par) then
25440 exit;
25441 end if;
25443 Par := Parent (Par);
25444 end loop;
25446 return False;
25447 end Within_Subprogram_Call;
25449 ----------------
25450 -- Wrong_Type --
25451 ----------------
25453 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
25454 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
25455 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
25457 Matching_Field : Entity_Id;
25458 -- Entity to give a more precise suggestion on how to write a one-
25459 -- element positional aggregate.
25461 function Has_One_Matching_Field return Boolean;
25462 -- Determines if Expec_Type is a record type with a single component or
25463 -- discriminant whose type matches the found type or is one dimensional
25464 -- array whose component type matches the found type. In the case of
25465 -- one discriminant, we ignore the variant parts. That's not accurate,
25466 -- but good enough for the warning.
25468 ----------------------------
25469 -- Has_One_Matching_Field --
25470 ----------------------------
25472 function Has_One_Matching_Field return Boolean is
25473 E : Entity_Id;
25475 begin
25476 Matching_Field := Empty;
25478 if Is_Array_Type (Expec_Type)
25479 and then Number_Dimensions (Expec_Type) = 1
25480 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
25481 then
25482 -- Use type name if available. This excludes multidimensional
25483 -- arrays and anonymous arrays.
25485 if Comes_From_Source (Expec_Type) then
25486 Matching_Field := Expec_Type;
25488 -- For an assignment, use name of target
25490 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
25491 and then Is_Entity_Name (Name (Parent (Expr)))
25492 then
25493 Matching_Field := Entity (Name (Parent (Expr)));
25494 end if;
25496 return True;
25498 elsif not Is_Record_Type (Expec_Type) then
25499 return False;
25501 else
25502 E := First_Entity (Expec_Type);
25503 loop
25504 if No (E) then
25505 return False;
25507 elsif not Ekind_In (E, E_Discriminant, E_Component)
25508 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
25509 then
25510 Next_Entity (E);
25512 else
25513 exit;
25514 end if;
25515 end loop;
25517 if not Covers (Etype (E), Found_Type) then
25518 return False;
25520 elsif Present (Next_Entity (E))
25521 and then (Ekind (E) = E_Component
25522 or else Ekind (Next_Entity (E)) = E_Discriminant)
25523 then
25524 return False;
25526 else
25527 Matching_Field := E;
25528 return True;
25529 end if;
25530 end if;
25531 end Has_One_Matching_Field;
25533 -- Start of processing for Wrong_Type
25535 begin
25536 -- Don't output message if either type is Any_Type, or if a message
25537 -- has already been posted for this node. We need to do the latter
25538 -- check explicitly (it is ordinarily done in Errout), because we
25539 -- are using ! to force the output of the error messages.
25541 if Expec_Type = Any_Type
25542 or else Found_Type = Any_Type
25543 or else Error_Posted (Expr)
25544 then
25545 return;
25547 -- If one of the types is a Taft-Amendment type and the other it its
25548 -- completion, it must be an illegal use of a TAT in the spec, for
25549 -- which an error was already emitted. Avoid cascaded errors.
25551 elsif Is_Incomplete_Type (Expec_Type)
25552 and then Has_Completion_In_Body (Expec_Type)
25553 and then Full_View (Expec_Type) = Etype (Expr)
25554 then
25555 return;
25557 elsif Is_Incomplete_Type (Etype (Expr))
25558 and then Has_Completion_In_Body (Etype (Expr))
25559 and then Full_View (Etype (Expr)) = Expec_Type
25560 then
25561 return;
25563 -- In an instance, there is an ongoing problem with completion of
25564 -- type derived from private types. Their structure is what Gigi
25565 -- expects, but the Etype is the parent type rather than the
25566 -- derived private type itself. Do not flag error in this case. The
25567 -- private completion is an entity without a parent, like an Itype.
25568 -- Similarly, full and partial views may be incorrect in the instance.
25569 -- There is no simple way to insure that it is consistent ???
25571 -- A similar view discrepancy can happen in an inlined body, for the
25572 -- same reason: inserted body may be outside of the original package
25573 -- and only partial views are visible at the point of insertion.
25575 elsif In_Instance or else In_Inlined_Body then
25576 if Etype (Etype (Expr)) = Etype (Expected_Type)
25577 and then
25578 (Has_Private_Declaration (Expected_Type)
25579 or else Has_Private_Declaration (Etype (Expr)))
25580 and then No (Parent (Expected_Type))
25581 then
25582 return;
25584 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
25585 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
25586 then
25587 return;
25589 elsif Is_Private_Type (Expected_Type)
25590 and then Present (Full_View (Expected_Type))
25591 and then Covers (Full_View (Expected_Type), Etype (Expr))
25592 then
25593 return;
25595 -- Conversely, type of expression may be the private one
25597 elsif Is_Private_Type (Base_Type (Etype (Expr)))
25598 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
25599 then
25600 return;
25601 end if;
25602 end if;
25604 -- An interesting special check. If the expression is parenthesized
25605 -- and its type corresponds to the type of the sole component of the
25606 -- expected record type, or to the component type of the expected one
25607 -- dimensional array type, then assume we have a bad aggregate attempt.
25609 if Nkind (Expr) in N_Subexpr
25610 and then Paren_Count (Expr) /= 0
25611 and then Has_One_Matching_Field
25612 then
25613 Error_Msg_N ("positional aggregate cannot have one component", Expr);
25615 if Present (Matching_Field) then
25616 if Is_Array_Type (Expec_Type) then
25617 Error_Msg_NE
25618 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
25619 else
25620 Error_Msg_NE
25621 ("\write instead `& ='> ...`", Expr, Matching_Field);
25622 end if;
25623 end if;
25625 -- Another special check, if we are looking for a pool-specific access
25626 -- type and we found an E_Access_Attribute_Type, then we have the case
25627 -- of an Access attribute being used in a context which needs a pool-
25628 -- specific type, which is never allowed. The one extra check we make
25629 -- is that the expected designated type covers the Found_Type.
25631 elsif Is_Access_Type (Expec_Type)
25632 and then Ekind (Found_Type) = E_Access_Attribute_Type
25633 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
25634 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
25635 and then Covers
25636 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
25637 then
25638 Error_Msg_N -- CODEFIX
25639 ("result must be general access type!", Expr);
25640 Error_Msg_NE -- CODEFIX
25641 ("add ALL to }!", Expr, Expec_Type);
25643 -- Another special check, if the expected type is an integer type,
25644 -- but the expression is of type System.Address, and the parent is
25645 -- an addition or subtraction operation whose left operand is the
25646 -- expression in question and whose right operand is of an integral
25647 -- type, then this is an attempt at address arithmetic, so give
25648 -- appropriate message.
25650 elsif Is_Integer_Type (Expec_Type)
25651 and then Is_RTE (Found_Type, RE_Address)
25652 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
25653 and then Expr = Left_Opnd (Parent (Expr))
25654 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
25655 then
25656 Error_Msg_N
25657 ("address arithmetic not predefined in package System",
25658 Parent (Expr));
25659 Error_Msg_N
25660 ("\possible missing with/use of System.Storage_Elements",
25661 Parent (Expr));
25662 return;
25664 -- If the expected type is an anonymous access type, as for access
25665 -- parameters and discriminants, the error is on the designated types.
25667 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
25668 if Comes_From_Source (Expec_Type) then
25669 Error_Msg_NE ("expected}!", Expr, Expec_Type);
25670 else
25671 Error_Msg_NE
25672 ("expected an access type with designated}",
25673 Expr, Designated_Type (Expec_Type));
25674 end if;
25676 if Is_Access_Type (Found_Type)
25677 and then not Comes_From_Source (Found_Type)
25678 then
25679 Error_Msg_NE
25680 ("\\found an access type with designated}!",
25681 Expr, Designated_Type (Found_Type));
25682 else
25683 if From_Limited_With (Found_Type) then
25684 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
25685 Error_Msg_Qual_Level := 99;
25686 Error_Msg_NE -- CODEFIX
25687 ("\\missing `WITH &;", Expr, Scope (Found_Type));
25688 Error_Msg_Qual_Level := 0;
25689 else
25690 Error_Msg_NE ("found}!", Expr, Found_Type);
25691 end if;
25692 end if;
25694 -- Normal case of one type found, some other type expected
25696 else
25697 -- If the names of the two types are the same, see if some number
25698 -- of levels of qualification will help. Don't try more than three
25699 -- levels, and if we get to standard, it's no use (and probably
25700 -- represents an error in the compiler) Also do not bother with
25701 -- internal scope names.
25703 declare
25704 Expec_Scope : Entity_Id;
25705 Found_Scope : Entity_Id;
25707 begin
25708 Expec_Scope := Expec_Type;
25709 Found_Scope := Found_Type;
25711 for Levels in Nat range 0 .. 3 loop
25712 if Chars (Expec_Scope) /= Chars (Found_Scope) then
25713 Error_Msg_Qual_Level := Levels;
25714 exit;
25715 end if;
25717 Expec_Scope := Scope (Expec_Scope);
25718 Found_Scope := Scope (Found_Scope);
25720 exit when Expec_Scope = Standard_Standard
25721 or else Found_Scope = Standard_Standard
25722 or else not Comes_From_Source (Expec_Scope)
25723 or else not Comes_From_Source (Found_Scope);
25724 end loop;
25725 end;
25727 if Is_Record_Type (Expec_Type)
25728 and then Present (Corresponding_Remote_Type (Expec_Type))
25729 then
25730 Error_Msg_NE ("expected}!", Expr,
25731 Corresponding_Remote_Type (Expec_Type));
25732 else
25733 Error_Msg_NE ("expected}!", Expr, Expec_Type);
25734 end if;
25736 if Is_Entity_Name (Expr)
25737 and then Is_Package_Or_Generic_Package (Entity (Expr))
25738 then
25739 Error_Msg_N ("\\found package name!", Expr);
25741 elsif Is_Entity_Name (Expr)
25742 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
25743 then
25744 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
25745 Error_Msg_N
25746 ("found procedure name, possibly missing Access attribute!",
25747 Expr);
25748 else
25749 Error_Msg_N
25750 ("\\found procedure name instead of function!", Expr);
25751 end if;
25753 elsif Nkind (Expr) = N_Function_Call
25754 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
25755 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
25756 and then No (Parameter_Associations (Expr))
25757 then
25758 Error_Msg_N
25759 ("found function name, possibly missing Access attribute!",
25760 Expr);
25762 -- Catch common error: a prefix or infix operator which is not
25763 -- directly visible because the type isn't.
25765 elsif Nkind (Expr) in N_Op
25766 and then Is_Overloaded (Expr)
25767 and then not Is_Immediately_Visible (Expec_Type)
25768 and then not Is_Potentially_Use_Visible (Expec_Type)
25769 and then not In_Use (Expec_Type)
25770 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
25771 then
25772 Error_Msg_N
25773 ("operator of the type is not directly visible!", Expr);
25775 elsif Ekind (Found_Type) = E_Void
25776 and then Present (Parent (Found_Type))
25777 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
25778 then
25779 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
25781 else
25782 Error_Msg_NE ("\\found}!", Expr, Found_Type);
25783 end if;
25785 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
25786 -- of the same modular type, and (M1 and M2) = 0 was intended.
25788 if Expec_Type = Standard_Boolean
25789 and then Is_Modular_Integer_Type (Found_Type)
25790 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
25791 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
25792 then
25793 declare
25794 Op : constant Node_Id := Right_Opnd (Parent (Expr));
25795 L : constant Node_Id := Left_Opnd (Op);
25796 R : constant Node_Id := Right_Opnd (Op);
25798 begin
25799 -- The case for the message is when the left operand of the
25800 -- comparison is the same modular type, or when it is an
25801 -- integer literal (or other universal integer expression),
25802 -- which would have been typed as the modular type if the
25803 -- parens had been there.
25805 if (Etype (L) = Found_Type
25806 or else
25807 Etype (L) = Universal_Integer)
25808 and then Is_Integer_Type (Etype (R))
25809 then
25810 Error_Msg_N
25811 ("\\possible missing parens for modular operation", Expr);
25812 end if;
25813 end;
25814 end if;
25816 -- Reset error message qualification indication
25818 Error_Msg_Qual_Level := 0;
25819 end if;
25820 end Wrong_Type;
25822 --------------------------------
25823 -- Yields_Synchronized_Object --
25824 --------------------------------
25826 function Yields_Synchronized_Object (Typ : Entity_Id) return Boolean is
25827 Has_Sync_Comp : Boolean := False;
25828 Id : Entity_Id;
25830 begin
25831 -- An array type yields a synchronized object if its component type
25832 -- yields a synchronized object.
25834 if Is_Array_Type (Typ) then
25835 return Yields_Synchronized_Object (Component_Type (Typ));
25837 -- A descendant of type Ada.Synchronous_Task_Control.Suspension_Object
25838 -- yields a synchronized object by default.
25840 elsif Is_Descendant_Of_Suspension_Object (Typ) then
25841 return True;
25843 -- A protected type yields a synchronized object by default
25845 elsif Is_Protected_Type (Typ) then
25846 return True;
25848 -- A record type or type extension yields a synchronized object when its
25849 -- discriminants (if any) lack default values and all components are of
25850 -- a type that yelds a synchronized object.
25852 elsif Is_Record_Type (Typ) then
25854 -- Inspect all entities defined in the scope of the type, looking for
25855 -- components of a type that does not yeld a synchronized object or
25856 -- for discriminants with default values.
25858 Id := First_Entity (Typ);
25859 while Present (Id) loop
25860 if Comes_From_Source (Id) then
25861 if Ekind (Id) = E_Component then
25862 if Yields_Synchronized_Object (Etype (Id)) then
25863 Has_Sync_Comp := True;
25865 -- The component does not yield a synchronized object
25867 else
25868 return False;
25869 end if;
25871 elsif Ekind (Id) = E_Discriminant
25872 and then Present (Expression (Parent (Id)))
25873 then
25874 return False;
25875 end if;
25876 end if;
25878 Next_Entity (Id);
25879 end loop;
25881 -- Ensure that the parent type of a type extension yields a
25882 -- synchronized object.
25884 if Etype (Typ) /= Typ
25885 and then not Yields_Synchronized_Object (Etype (Typ))
25886 then
25887 return False;
25888 end if;
25890 -- If we get here, then all discriminants lack default values and all
25891 -- components are of a type that yields a synchronized object.
25893 return Has_Sync_Comp;
25895 -- A synchronized interface type yields a synchronized object by default
25897 elsif Is_Synchronized_Interface (Typ) then
25898 return True;
25900 -- A task type yelds a synchronized object by default
25902 elsif Is_Task_Type (Typ) then
25903 return True;
25905 -- Otherwise the type does not yield a synchronized object
25907 else
25908 return False;
25909 end if;
25910 end Yields_Synchronized_Object;
25912 ---------------------------
25913 -- Yields_Universal_Type --
25914 ---------------------------
25916 function Yields_Universal_Type (N : Node_Id) return Boolean is
25917 begin
25918 -- Integer and real literals are of a universal type
25920 if Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
25921 return True;
25923 -- The values of certain attributes are of a universal type
25925 elsif Nkind (N) = N_Attribute_Reference then
25926 return
25927 Universal_Type_Attribute (Get_Attribute_Id (Attribute_Name (N)));
25929 -- ??? There are possibly other cases to consider
25931 else
25932 return False;
25933 end if;
25934 end Yields_Universal_Type;
25936 begin
25937 Erroutc.Subprogram_Name_Ptr := Subprogram_Name'Access;
25938 end Sem_Util;