Daily bump.
[official-gcc.git] / gcc / ada / sem_util.adb
bloba29b286f717ec07e25abb8ea16905d7b8046670a
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-2015, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Util; use Exp_Util;
36 with Fname; use Fname;
37 with Freeze; use Freeze;
38 with Ghost; use Ghost;
39 with Lib; use Lib;
40 with Lib.Xref; use Lib.Xref;
41 with Namet.Sp; use Namet.Sp;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Attr; use Sem_Attr;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch12; use Sem_Ch12;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Disp; use Sem_Disp;
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 -- Global Variables for New_Copy_Tree --
77 ----------------------------------------
79 -- These global variables are used by New_Copy_Tree. See description of the
80 -- body of this subprogram for details. Global variables can be safely used
81 -- by New_Copy_Tree, since there is no case of a recursive call from the
82 -- processing inside New_Copy_Tree.
84 NCT_Hash_Threshold : constant := 20;
85 -- If there are more than this number of pairs of entries in the map, then
86 -- Hash_Tables_Used will be set, and the hash tables will be initialized
87 -- and used for the searches.
89 NCT_Hash_Tables_Used : Boolean := False;
90 -- Set to True if hash tables are in use
92 NCT_Table_Entries : Nat := 0;
93 -- Count entries in table to see if threshold is reached
95 NCT_Hash_Table_Setup : Boolean := False;
96 -- Set to True if hash table contains data. We set this True if we setup
97 -- the hash table with data, and leave it set permanently from then on,
98 -- this is a signal that second and subsequent users of the hash table
99 -- must clear the old entries before reuse.
101 subtype NCT_Header_Num is Int range 0 .. 511;
102 -- Defines range of headers in hash tables (512 headers)
104 -----------------------
105 -- Local Subprograms --
106 -----------------------
108 function Build_Component_Subtype
109 (C : List_Id;
110 Loc : Source_Ptr;
111 T : Entity_Id) return Node_Id;
112 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
113 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
114 -- Loc is the source location, T is the original subtype.
116 function Has_Enabled_Property
117 (Item_Id : Entity_Id;
118 Property : Name_Id) return Boolean;
119 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
120 -- Determine whether an abstract state or a variable denoted by entity
121 -- Item_Id has enabled property Property.
123 function Has_Null_Extension (T : Entity_Id) return Boolean;
124 -- T is a derived tagged type. Check whether the type extension is null.
125 -- If the parent type is fully initialized, T can be treated as such.
127 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
128 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
129 -- with discriminants whose default values are static, examine only the
130 -- components in the selected variant to determine whether all of them
131 -- have a default.
133 ------------------------------
134 -- Abstract_Interface_List --
135 ------------------------------
137 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
138 Nod : Node_Id;
140 begin
141 if Is_Concurrent_Type (Typ) then
143 -- If we are dealing with a synchronized subtype, go to the base
144 -- type, whose declaration has the interface list.
146 -- Shouldn't this be Declaration_Node???
148 Nod := Parent (Base_Type (Typ));
150 if Nkind (Nod) = N_Full_Type_Declaration then
151 return Empty_List;
152 end if;
154 elsif Ekind (Typ) = E_Record_Type_With_Private then
155 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
156 Nod := Type_Definition (Parent (Typ));
158 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
159 if Present (Full_View (Typ))
160 and then
161 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
162 then
163 Nod := Type_Definition (Parent (Full_View (Typ)));
165 -- If the full-view is not available we cannot do anything else
166 -- here (the source has errors).
168 else
169 return Empty_List;
170 end if;
172 -- Support for generic formals with interfaces is still missing ???
174 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
175 return Empty_List;
177 else
178 pragma Assert
179 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
180 Nod := Parent (Typ);
181 end if;
183 elsif Ekind (Typ) = E_Record_Subtype then
184 Nod := Type_Definition (Parent (Etype (Typ)));
186 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
188 -- Recurse, because parent may still be a private extension. Also
189 -- note that the full view of the subtype or the full view of its
190 -- base type may (both) be unavailable.
192 return Abstract_Interface_List (Etype (Typ));
194 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
195 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
196 Nod := Formal_Type_Definition (Parent (Typ));
197 else
198 Nod := Type_Definition (Parent (Typ));
199 end if;
200 end if;
202 return Interface_List (Nod);
203 end Abstract_Interface_List;
205 --------------------------------
206 -- Add_Access_Type_To_Process --
207 --------------------------------
209 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
210 L : Elist_Id;
212 begin
213 Ensure_Freeze_Node (E);
214 L := Access_Types_To_Process (Freeze_Node (E));
216 if No (L) then
217 L := New_Elmt_List;
218 Set_Access_Types_To_Process (Freeze_Node (E), L);
219 end if;
221 Append_Elmt (A, L);
222 end Add_Access_Type_To_Process;
224 --------------------------
225 -- Add_Block_Identifier --
226 --------------------------
228 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
229 Loc : constant Source_Ptr := Sloc (N);
231 begin
232 pragma Assert (Nkind (N) = N_Block_Statement);
234 -- The block already has a label, return its entity
236 if Present (Identifier (N)) then
237 Id := Entity (Identifier (N));
239 -- Create a new block label and set its attributes
241 else
242 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
243 Set_Etype (Id, Standard_Void_Type);
244 Set_Parent (Id, N);
246 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
247 Set_Block_Node (Id, Identifier (N));
248 end if;
249 end Add_Block_Identifier;
251 -----------------------
252 -- Add_Contract_Item --
253 -----------------------
255 procedure Add_Contract_Item (Prag : Node_Id; Id : Entity_Id) is
256 Items : Node_Id := Contract (Id);
258 procedure Add_Classification;
259 -- Prepend Prag to the list of classifications
261 procedure Add_Contract_Test_Case;
262 -- Prepend Prag to the list of contract and test cases
264 procedure Add_Pre_Post_Condition;
265 -- Prepend Prag to the list of pre- and postconditions
267 ------------------------
268 -- Add_Classification --
269 ------------------------
271 procedure Add_Classification is
272 begin
273 Set_Next_Pragma (Prag, Classifications (Items));
274 Set_Classifications (Items, Prag);
275 end Add_Classification;
277 ----------------------------
278 -- Add_Contract_Test_Case --
279 ----------------------------
281 procedure Add_Contract_Test_Case is
282 begin
283 Set_Next_Pragma (Prag, Contract_Test_Cases (Items));
284 Set_Contract_Test_Cases (Items, Prag);
285 end Add_Contract_Test_Case;
287 ----------------------------
288 -- Add_Pre_Post_Condition --
289 ----------------------------
291 procedure Add_Pre_Post_Condition is
292 begin
293 Set_Next_Pragma (Prag, Pre_Post_Conditions (Items));
294 Set_Pre_Post_Conditions (Items, Prag);
295 end Add_Pre_Post_Condition;
297 -- Local variables
299 Prag_Nam : Name_Id;
301 -- Start of processing for Add_Contract_Item
303 begin
304 -- A contract must contain only pragmas
306 pragma Assert (Nkind (Prag) = N_Pragma);
307 Prag_Nam := Pragma_Name (Prag);
309 -- Create a new contract when adding the first item
311 if No (Items) then
312 Items := Make_Contract (Sloc (Id));
313 Set_Contract (Id, Items);
314 end if;
316 -- Contract items related to constants. Applicable pragmas are:
317 -- Part_Of
319 if Ekind (Id) = E_Constant then
320 if Prag_Nam = Name_Part_Of then
321 Add_Classification;
323 -- The pragma is not a proper contract item
325 else
326 raise Program_Error;
327 end if;
329 -- Contract items related to [generic] packages or instantiations. The
330 -- applicable pragmas are:
331 -- Abstract_States
332 -- Initial_Condition
333 -- Initializes
334 -- Part_Of (instantiation only)
336 elsif Ekind_In (Id, E_Generic_Package, E_Package) then
337 if Nam_In (Prag_Nam, Name_Abstract_State,
338 Name_Initial_Condition,
339 Name_Initializes)
340 then
341 Add_Classification;
343 -- Indicator Part_Of must be associated with a package instantiation
345 elsif Prag_Nam = Name_Part_Of and then Is_Generic_Instance (Id) then
346 Add_Classification;
348 -- The pragma is not a proper contract item
350 else
351 raise Program_Error;
352 end if;
354 -- Contract items related to package bodies. The applicable pragmas are:
355 -- Refined_States
357 elsif Ekind (Id) = E_Package_Body then
358 if Prag_Nam = Name_Refined_State then
359 Add_Classification;
361 -- The pragma is not a proper contract item
363 else
364 raise Program_Error;
365 end if;
367 -- Contract items related to subprogram or entry declarations. The
368 -- applicable pragmas are:
369 -- Contract_Cases
370 -- Depends
371 -- Extensions_Visible
372 -- Global
373 -- Postcondition
374 -- Precondition
375 -- Test_Case
377 elsif Ekind_In (Id, E_Entry, E_Entry_Family)
378 or else Is_Generic_Subprogram (Id)
379 or else Is_Subprogram (Id)
380 then
381 if Nam_In (Prag_Nam, Name_Postcondition, Name_Precondition) then
382 Add_Pre_Post_Condition;
384 elsif Nam_In (Prag_Nam, Name_Contract_Cases, Name_Test_Case) then
385 Add_Contract_Test_Case;
387 elsif Nam_In (Prag_Nam, Name_Depends,
388 Name_Extensions_Visible,
389 Name_Global)
390 then
391 Add_Classification;
393 -- The pragma is not a proper contract item
395 else
396 raise Program_Error;
397 end if;
399 -- Contract items related to subprogram bodies. Applicable pragmas are:
400 -- Postcondition
401 -- Precondition
402 -- Refined_Depends
403 -- Refined_Global
404 -- Refined_Post
406 elsif Ekind (Id) = E_Subprogram_Body then
407 if Nam_In (Prag_Nam, Name_Refined_Depends, Name_Refined_Global) then
408 Add_Classification;
410 elsif Nam_In (Prag_Nam, Name_Postcondition,
411 Name_Precondition,
412 Name_Refined_Post)
413 then
414 Add_Pre_Post_Condition;
416 -- The pragma is not a proper contract item
418 else
419 raise Program_Error;
420 end if;
422 -- Contract items related to variables. Applicable pragmas are:
423 -- Async_Readers
424 -- Async_Writers
425 -- Effective_Reads
426 -- Effective_Writes
427 -- Part_Of
429 elsif Ekind (Id) = E_Variable then
430 if Nam_In (Prag_Nam, Name_Async_Readers,
431 Name_Async_Writers,
432 Name_Effective_Reads,
433 Name_Effective_Writes,
434 Name_Part_Of)
435 then
436 Add_Classification;
438 -- The pragma is not a proper contract item
440 else
441 raise Program_Error;
442 end if;
443 end if;
444 end Add_Contract_Item;
446 ----------------------------
447 -- Add_Global_Declaration --
448 ----------------------------
450 procedure Add_Global_Declaration (N : Node_Id) is
451 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
453 begin
454 if No (Declarations (Aux_Node)) then
455 Set_Declarations (Aux_Node, New_List);
456 end if;
458 Append_To (Declarations (Aux_Node), N);
459 Analyze (N);
460 end Add_Global_Declaration;
462 --------------------------------
463 -- Address_Integer_Convert_OK --
464 --------------------------------
466 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
467 begin
468 if Allow_Integer_Address
469 and then ((Is_Descendent_Of_Address (T1)
470 and then Is_Private_Type (T1)
471 and then Is_Integer_Type (T2))
472 or else
473 (Is_Descendent_Of_Address (T2)
474 and then Is_Private_Type (T2)
475 and then Is_Integer_Type (T1)))
476 then
477 return True;
478 else
479 return False;
480 end if;
481 end Address_Integer_Convert_OK;
483 -----------------
484 -- Addressable --
485 -----------------
487 -- For now, just 8/16/32/64. but analyze later if AAMP is special???
489 function Addressable (V : Uint) return Boolean is
490 begin
491 return V = Uint_8 or else
492 V = Uint_16 or else
493 V = Uint_32 or else
494 V = Uint_64;
495 end Addressable;
497 function Addressable (V : Int) return Boolean is
498 begin
499 return V = 8 or else
500 V = 16 or else
501 V = 32 or else
502 V = 64;
503 end Addressable;
505 ---------------------------------
506 -- Aggregate_Constraint_Checks --
507 ---------------------------------
509 procedure Aggregate_Constraint_Checks
510 (Exp : Node_Id;
511 Check_Typ : Entity_Id)
513 Exp_Typ : constant Entity_Id := Etype (Exp);
515 begin
516 if Raises_Constraint_Error (Exp) then
517 return;
518 end if;
520 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
521 -- component's type to force the appropriate accessibility checks.
523 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
524 -- type to force the corresponding run-time check
526 if Is_Access_Type (Check_Typ)
527 and then ((Is_Local_Anonymous_Access (Check_Typ))
528 or else (Can_Never_Be_Null (Check_Typ)
529 and then not Can_Never_Be_Null (Exp_Typ)))
530 then
531 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
532 Analyze_And_Resolve (Exp, Check_Typ);
533 Check_Unset_Reference (Exp);
534 end if;
536 -- This is really expansion activity, so make sure that expansion is
537 -- on and is allowed. In GNATprove mode, we also want check flags to
538 -- be added in the tree, so that the formal verification can rely on
539 -- those to be present. In GNATprove mode for formal verification, some
540 -- treatment typically only done during expansion needs to be performed
541 -- on the tree, but it should not be applied inside generics. Otherwise,
542 -- this breaks the name resolution mechanism for generic instances.
544 if not Expander_Active
545 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
546 then
547 return;
548 end if;
550 -- First check if we have to insert discriminant checks
552 if Has_Discriminants (Exp_Typ) then
553 Apply_Discriminant_Check (Exp, Check_Typ);
555 -- Next emit length checks for array aggregates
557 elsif Is_Array_Type (Exp_Typ) then
558 Apply_Length_Check (Exp, Check_Typ);
560 -- Finally emit scalar and string checks. If we are dealing with a
561 -- scalar literal we need to check by hand because the Etype of
562 -- literals is not necessarily correct.
564 elsif Is_Scalar_Type (Exp_Typ)
565 and then Compile_Time_Known_Value (Exp)
566 then
567 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
568 Apply_Compile_Time_Constraint_Error
569 (Exp, "value not in range of}??", CE_Range_Check_Failed,
570 Ent => Base_Type (Check_Typ),
571 Typ => Base_Type (Check_Typ));
573 elsif Is_Out_Of_Range (Exp, Check_Typ) then
574 Apply_Compile_Time_Constraint_Error
575 (Exp, "value not in range of}??", CE_Range_Check_Failed,
576 Ent => Check_Typ,
577 Typ => Check_Typ);
579 elsif not Range_Checks_Suppressed (Check_Typ) then
580 Apply_Scalar_Range_Check (Exp, Check_Typ);
581 end if;
583 -- Verify that target type is also scalar, to prevent view anomalies
584 -- in instantiations.
586 elsif (Is_Scalar_Type (Exp_Typ)
587 or else Nkind (Exp) = N_String_Literal)
588 and then Is_Scalar_Type (Check_Typ)
589 and then Exp_Typ /= Check_Typ
590 then
591 if Is_Entity_Name (Exp)
592 and then Ekind (Entity (Exp)) = E_Constant
593 then
594 -- If expression is a constant, it is worthwhile checking whether
595 -- it is a bound of the type.
597 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
598 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
599 or else
600 (Is_Entity_Name (Type_High_Bound (Check_Typ))
601 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
602 then
603 return;
605 else
606 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
607 Analyze_And_Resolve (Exp, Check_Typ);
608 Check_Unset_Reference (Exp);
609 end if;
611 -- Could use a comment on this case ???
613 else
614 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
615 Analyze_And_Resolve (Exp, Check_Typ);
616 Check_Unset_Reference (Exp);
617 end if;
619 end if;
620 end Aggregate_Constraint_Checks;
622 -----------------------
623 -- Alignment_In_Bits --
624 -----------------------
626 function Alignment_In_Bits (E : Entity_Id) return Uint is
627 begin
628 return Alignment (E) * System_Storage_Unit;
629 end Alignment_In_Bits;
631 ---------------------------------
632 -- Append_Inherited_Subprogram --
633 ---------------------------------
635 procedure Append_Inherited_Subprogram (S : Entity_Id) is
636 Par : constant Entity_Id := Alias (S);
637 -- The parent subprogram
639 Scop : constant Entity_Id := Scope (Par);
640 -- The scope of definition of the parent subprogram
642 Typ : constant Entity_Id := Defining_Entity (Parent (S));
643 -- The derived type of which S is a primitive operation
645 Decl : Node_Id;
646 Next_E : Entity_Id;
648 begin
649 if Ekind (Current_Scope) = E_Package
650 and then In_Private_Part (Current_Scope)
651 and then Has_Private_Declaration (Typ)
652 and then Is_Tagged_Type (Typ)
653 and then Scop = Current_Scope
654 then
655 -- The inherited operation is available at the earliest place after
656 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
657 -- relevant for type extensions. If the parent operation appears
658 -- after the type extension, the operation is not visible.
660 Decl := First
661 (Visible_Declarations
662 (Package_Specification (Current_Scope)));
663 while Present (Decl) loop
664 if Nkind (Decl) = N_Private_Extension_Declaration
665 and then Defining_Entity (Decl) = Typ
666 then
667 if Sloc (Decl) > Sloc (Par) then
668 Next_E := Next_Entity (Par);
669 Set_Next_Entity (Par, S);
670 Set_Next_Entity (S, Next_E);
671 return;
673 else
674 exit;
675 end if;
676 end if;
678 Next (Decl);
679 end loop;
680 end if;
682 -- If partial view is not a type extension, or it appears before the
683 -- subprogram declaration, insert normally at end of entity list.
685 Append_Entity (S, Current_Scope);
686 end Append_Inherited_Subprogram;
688 -----------------------------------------
689 -- Apply_Compile_Time_Constraint_Error --
690 -----------------------------------------
692 procedure Apply_Compile_Time_Constraint_Error
693 (N : Node_Id;
694 Msg : String;
695 Reason : RT_Exception_Code;
696 Ent : Entity_Id := Empty;
697 Typ : Entity_Id := Empty;
698 Loc : Source_Ptr := No_Location;
699 Rep : Boolean := True;
700 Warn : Boolean := False)
702 Stat : constant Boolean := Is_Static_Expression (N);
703 R_Stat : constant Node_Id :=
704 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
705 Rtyp : Entity_Id;
707 begin
708 if No (Typ) then
709 Rtyp := Etype (N);
710 else
711 Rtyp := Typ;
712 end if;
714 Discard_Node
715 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
717 if not Rep then
718 return;
719 end if;
721 -- Now we replace the node by an N_Raise_Constraint_Error node
722 -- This does not need reanalyzing, so set it as analyzed now.
724 Rewrite (N, R_Stat);
725 Set_Analyzed (N, True);
727 Set_Etype (N, Rtyp);
728 Set_Raises_Constraint_Error (N);
730 -- Now deal with possible local raise handling
732 Possible_Local_Raise (N, Standard_Constraint_Error);
734 -- If the original expression was marked as static, the result is
735 -- still marked as static, but the Raises_Constraint_Error flag is
736 -- always set so that further static evaluation is not attempted.
738 if Stat then
739 Set_Is_Static_Expression (N);
740 end if;
741 end Apply_Compile_Time_Constraint_Error;
743 ---------------------------
744 -- Async_Readers_Enabled --
745 ---------------------------
747 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
748 begin
749 return Has_Enabled_Property (Id, Name_Async_Readers);
750 end Async_Readers_Enabled;
752 ---------------------------
753 -- Async_Writers_Enabled --
754 ---------------------------
756 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
757 begin
758 return Has_Enabled_Property (Id, Name_Async_Writers);
759 end Async_Writers_Enabled;
761 --------------------------------------
762 -- Available_Full_View_Of_Component --
763 --------------------------------------
765 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
766 ST : constant Entity_Id := Scope (T);
767 SCT : constant Entity_Id := Scope (Component_Type (T));
768 begin
769 return In_Open_Scopes (ST)
770 and then In_Open_Scopes (SCT)
771 and then Scope_Depth (ST) >= Scope_Depth (SCT);
772 end Available_Full_View_Of_Component;
774 -------------------
775 -- Bad_Attribute --
776 -------------------
778 procedure Bad_Attribute
779 (N : Node_Id;
780 Nam : Name_Id;
781 Warn : Boolean := False)
783 begin
784 Error_Msg_Warn := Warn;
785 Error_Msg_N ("unrecognized attribute&<<", N);
787 -- Check for possible misspelling
789 Error_Msg_Name_1 := First_Attribute_Name;
790 while Error_Msg_Name_1 <= Last_Attribute_Name loop
791 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
792 Error_Msg_N -- CODEFIX
793 ("\possible misspelling of %<<", N);
794 exit;
795 end if;
797 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
798 end loop;
799 end Bad_Attribute;
801 --------------------------------
802 -- Bad_Predicated_Subtype_Use --
803 --------------------------------
805 procedure Bad_Predicated_Subtype_Use
806 (Msg : String;
807 N : Node_Id;
808 Typ : Entity_Id;
809 Suggest_Static : Boolean := False)
811 Gen : Entity_Id;
813 begin
814 -- Avoid cascaded errors
816 if Error_Posted (N) then
817 return;
818 end if;
820 if Inside_A_Generic then
821 Gen := Current_Scope;
822 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
823 Gen := Scope (Gen);
824 end loop;
826 if No (Gen) then
827 return;
828 end if;
830 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
831 Set_No_Predicate_On_Actual (Typ);
832 end if;
834 elsif Has_Predicates (Typ) then
835 if Is_Generic_Actual_Type (Typ) then
837 -- The restriction on loop parameters is only that the type
838 -- should have no dynamic predicates.
840 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
841 and then not Has_Dynamic_Predicate_Aspect (Typ)
842 and then Is_OK_Static_Subtype (Typ)
843 then
844 return;
845 end if;
847 Gen := Current_Scope;
848 while not Is_Generic_Instance (Gen) loop
849 Gen := Scope (Gen);
850 end loop;
852 pragma Assert (Present (Gen));
854 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
855 Error_Msg_Warn := SPARK_Mode /= On;
856 Error_Msg_FE (Msg & "<<", N, Typ);
857 Error_Msg_F ("\Program_Error [<<", N);
859 Insert_Action (N,
860 Make_Raise_Program_Error (Sloc (N),
861 Reason => PE_Bad_Predicated_Generic_Type));
863 else
864 Error_Msg_FE (Msg & "<<", N, Typ);
865 end if;
867 else
868 Error_Msg_FE (Msg, N, Typ);
869 end if;
871 -- Emit an optional suggestion on how to remedy the error if the
872 -- context warrants it.
874 if Suggest_Static and then Has_Static_Predicate (Typ) then
875 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
876 end if;
877 end if;
878 end Bad_Predicated_Subtype_Use;
880 -----------------------------------------
881 -- Bad_Unordered_Enumeration_Reference --
882 -----------------------------------------
884 function Bad_Unordered_Enumeration_Reference
885 (N : Node_Id;
886 T : Entity_Id) return Boolean
888 begin
889 return Is_Enumeration_Type (T)
890 and then Warn_On_Unordered_Enumeration_Type
891 and then not Is_Generic_Type (T)
892 and then Comes_From_Source (N)
893 and then not Has_Pragma_Ordered (T)
894 and then not In_Same_Extended_Unit (N, T);
895 end Bad_Unordered_Enumeration_Reference;
897 --------------------------
898 -- Build_Actual_Subtype --
899 --------------------------
901 function Build_Actual_Subtype
902 (T : Entity_Id;
903 N : Node_Or_Entity_Id) return Node_Id
905 Loc : Source_Ptr;
906 -- Normally Sloc (N), but may point to corresponding body in some cases
908 Constraints : List_Id;
909 Decl : Node_Id;
910 Discr : Entity_Id;
911 Hi : Node_Id;
912 Lo : Node_Id;
913 Subt : Entity_Id;
914 Disc_Type : Entity_Id;
915 Obj : Node_Id;
917 begin
918 Loc := Sloc (N);
920 if Nkind (N) = N_Defining_Identifier then
921 Obj := New_Occurrence_Of (N, Loc);
923 -- If this is a formal parameter of a subprogram declaration, and
924 -- we are compiling the body, we want the declaration for the
925 -- actual subtype to carry the source position of the body, to
926 -- prevent anomalies in gdb when stepping through the code.
928 if Is_Formal (N) then
929 declare
930 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
931 begin
932 if Nkind (Decl) = N_Subprogram_Declaration
933 and then Present (Corresponding_Body (Decl))
934 then
935 Loc := Sloc (Corresponding_Body (Decl));
936 end if;
937 end;
938 end if;
940 else
941 Obj := N;
942 end if;
944 if Is_Array_Type (T) then
945 Constraints := New_List;
946 for J in 1 .. Number_Dimensions (T) loop
948 -- Build an array subtype declaration with the nominal subtype and
949 -- the bounds of the actual. Add the declaration in front of the
950 -- local declarations for the subprogram, for analysis before any
951 -- reference to the formal in the body.
953 Lo :=
954 Make_Attribute_Reference (Loc,
955 Prefix =>
956 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
957 Attribute_Name => Name_First,
958 Expressions => New_List (
959 Make_Integer_Literal (Loc, J)));
961 Hi :=
962 Make_Attribute_Reference (Loc,
963 Prefix =>
964 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
965 Attribute_Name => Name_Last,
966 Expressions => New_List (
967 Make_Integer_Literal (Loc, J)));
969 Append (Make_Range (Loc, Lo, Hi), Constraints);
970 end loop;
972 -- If the type has unknown discriminants there is no constrained
973 -- subtype to build. This is never called for a formal or for a
974 -- lhs, so returning the type is ok ???
976 elsif Has_Unknown_Discriminants (T) then
977 return T;
979 else
980 Constraints := New_List;
982 -- Type T is a generic derived type, inherit the discriminants from
983 -- the parent type.
985 if Is_Private_Type (T)
986 and then No (Full_View (T))
988 -- T was flagged as an error if it was declared as a formal
989 -- derived type with known discriminants. In this case there
990 -- is no need to look at the parent type since T already carries
991 -- its own discriminants.
993 and then not Error_Posted (T)
994 then
995 Disc_Type := Etype (Base_Type (T));
996 else
997 Disc_Type := T;
998 end if;
1000 Discr := First_Discriminant (Disc_Type);
1001 while Present (Discr) loop
1002 Append_To (Constraints,
1003 Make_Selected_Component (Loc,
1004 Prefix =>
1005 Duplicate_Subexpr_No_Checks (Obj),
1006 Selector_Name => New_Occurrence_Of (Discr, Loc)));
1007 Next_Discriminant (Discr);
1008 end loop;
1009 end if;
1011 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
1012 Set_Is_Internal (Subt);
1014 Decl :=
1015 Make_Subtype_Declaration (Loc,
1016 Defining_Identifier => Subt,
1017 Subtype_Indication =>
1018 Make_Subtype_Indication (Loc,
1019 Subtype_Mark => New_Occurrence_Of (T, Loc),
1020 Constraint =>
1021 Make_Index_Or_Discriminant_Constraint (Loc,
1022 Constraints => Constraints)));
1024 Mark_Rewrite_Insertion (Decl);
1025 return Decl;
1026 end Build_Actual_Subtype;
1028 ---------------------------------------
1029 -- Build_Actual_Subtype_Of_Component --
1030 ---------------------------------------
1032 function Build_Actual_Subtype_Of_Component
1033 (T : Entity_Id;
1034 N : Node_Id) return Node_Id
1036 Loc : constant Source_Ptr := Sloc (N);
1037 P : constant Node_Id := Prefix (N);
1038 D : Elmt_Id;
1039 Id : Node_Id;
1040 Index_Typ : Entity_Id;
1042 Desig_Typ : Entity_Id;
1043 -- This is either a copy of T, or if T is an access type, then it is
1044 -- the directly designated type of this access type.
1046 function Build_Actual_Array_Constraint return List_Id;
1047 -- If one or more of the bounds of the component depends on
1048 -- discriminants, build actual constraint using the discriminants
1049 -- of the prefix.
1051 function Build_Actual_Record_Constraint return List_Id;
1052 -- Similar to previous one, for discriminated components constrained
1053 -- by the discriminant of the enclosing object.
1055 -----------------------------------
1056 -- Build_Actual_Array_Constraint --
1057 -----------------------------------
1059 function Build_Actual_Array_Constraint return List_Id is
1060 Constraints : constant List_Id := New_List;
1061 Indx : Node_Id;
1062 Hi : Node_Id;
1063 Lo : Node_Id;
1064 Old_Hi : Node_Id;
1065 Old_Lo : Node_Id;
1067 begin
1068 Indx := First_Index (Desig_Typ);
1069 while Present (Indx) loop
1070 Old_Lo := Type_Low_Bound (Etype (Indx));
1071 Old_Hi := Type_High_Bound (Etype (Indx));
1073 if Denotes_Discriminant (Old_Lo) then
1074 Lo :=
1075 Make_Selected_Component (Loc,
1076 Prefix => New_Copy_Tree (P),
1077 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1079 else
1080 Lo := New_Copy_Tree (Old_Lo);
1082 -- The new bound will be reanalyzed in the enclosing
1083 -- declaration. For literal bounds that come from a type
1084 -- declaration, the type of the context must be imposed, so
1085 -- insure that analysis will take place. For non-universal
1086 -- types this is not strictly necessary.
1088 Set_Analyzed (Lo, False);
1089 end if;
1091 if Denotes_Discriminant (Old_Hi) then
1092 Hi :=
1093 Make_Selected_Component (Loc,
1094 Prefix => New_Copy_Tree (P),
1095 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1097 else
1098 Hi := New_Copy_Tree (Old_Hi);
1099 Set_Analyzed (Hi, False);
1100 end if;
1102 Append (Make_Range (Loc, Lo, Hi), Constraints);
1103 Next_Index (Indx);
1104 end loop;
1106 return Constraints;
1107 end Build_Actual_Array_Constraint;
1109 ------------------------------------
1110 -- Build_Actual_Record_Constraint --
1111 ------------------------------------
1113 function Build_Actual_Record_Constraint return List_Id is
1114 Constraints : constant List_Id := New_List;
1115 D : Elmt_Id;
1116 D_Val : Node_Id;
1118 begin
1119 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1120 while Present (D) loop
1121 if Denotes_Discriminant (Node (D)) then
1122 D_Val := Make_Selected_Component (Loc,
1123 Prefix => New_Copy_Tree (P),
1124 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1126 else
1127 D_Val := New_Copy_Tree (Node (D));
1128 end if;
1130 Append (D_Val, Constraints);
1131 Next_Elmt (D);
1132 end loop;
1134 return Constraints;
1135 end Build_Actual_Record_Constraint;
1137 -- Start of processing for Build_Actual_Subtype_Of_Component
1139 begin
1140 -- Why the test for Spec_Expression mode here???
1142 if In_Spec_Expression then
1143 return Empty;
1145 -- More comments for the rest of this body would be good ???
1147 elsif Nkind (N) = N_Explicit_Dereference then
1148 if Is_Composite_Type (T)
1149 and then not Is_Constrained (T)
1150 and then not (Is_Class_Wide_Type (T)
1151 and then Is_Constrained (Root_Type (T)))
1152 and then not Has_Unknown_Discriminants (T)
1153 then
1154 -- If the type of the dereference is already constrained, it is an
1155 -- actual subtype.
1157 if Is_Array_Type (Etype (N))
1158 and then Is_Constrained (Etype (N))
1159 then
1160 return Empty;
1161 else
1162 Remove_Side_Effects (P);
1163 return Build_Actual_Subtype (T, N);
1164 end if;
1165 else
1166 return Empty;
1167 end if;
1168 end if;
1170 if Ekind (T) = E_Access_Subtype then
1171 Desig_Typ := Designated_Type (T);
1172 else
1173 Desig_Typ := T;
1174 end if;
1176 if Ekind (Desig_Typ) = E_Array_Subtype then
1177 Id := First_Index (Desig_Typ);
1178 while Present (Id) loop
1179 Index_Typ := Underlying_Type (Etype (Id));
1181 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1182 or else
1183 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1184 then
1185 Remove_Side_Effects (P);
1186 return
1187 Build_Component_Subtype
1188 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1189 end if;
1191 Next_Index (Id);
1192 end loop;
1194 elsif Is_Composite_Type (Desig_Typ)
1195 and then Has_Discriminants (Desig_Typ)
1196 and then not Has_Unknown_Discriminants (Desig_Typ)
1197 then
1198 if Is_Private_Type (Desig_Typ)
1199 and then No (Discriminant_Constraint (Desig_Typ))
1200 then
1201 Desig_Typ := Full_View (Desig_Typ);
1202 end if;
1204 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1205 while Present (D) loop
1206 if Denotes_Discriminant (Node (D)) then
1207 Remove_Side_Effects (P);
1208 return
1209 Build_Component_Subtype (
1210 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1211 end if;
1213 Next_Elmt (D);
1214 end loop;
1215 end if;
1217 -- If none of the above, the actual and nominal subtypes are the same
1219 return Empty;
1220 end Build_Actual_Subtype_Of_Component;
1222 -----------------------------
1223 -- Build_Component_Subtype --
1224 -----------------------------
1226 function Build_Component_Subtype
1227 (C : List_Id;
1228 Loc : Source_Ptr;
1229 T : Entity_Id) return Node_Id
1231 Subt : Entity_Id;
1232 Decl : Node_Id;
1234 begin
1235 -- Unchecked_Union components do not require component subtypes
1237 if Is_Unchecked_Union (T) then
1238 return Empty;
1239 end if;
1241 Subt := Make_Temporary (Loc, 'S');
1242 Set_Is_Internal (Subt);
1244 Decl :=
1245 Make_Subtype_Declaration (Loc,
1246 Defining_Identifier => Subt,
1247 Subtype_Indication =>
1248 Make_Subtype_Indication (Loc,
1249 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1250 Constraint =>
1251 Make_Index_Or_Discriminant_Constraint (Loc,
1252 Constraints => C)));
1254 Mark_Rewrite_Insertion (Decl);
1255 return Decl;
1256 end Build_Component_Subtype;
1258 ----------------------------------
1259 -- Build_Default_Init_Cond_Call --
1260 ----------------------------------
1262 function Build_Default_Init_Cond_Call
1263 (Loc : Source_Ptr;
1264 Obj_Id : Entity_Id;
1265 Typ : Entity_Id) return Node_Id
1267 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1268 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1270 begin
1271 return
1272 Make_Procedure_Call_Statement (Loc,
1273 Name => New_Occurrence_Of (Proc_Id, Loc),
1274 Parameter_Associations => New_List (
1275 Make_Unchecked_Type_Conversion (Loc,
1276 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1277 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1278 end Build_Default_Init_Cond_Call;
1280 ----------------------------------------------
1281 -- Build_Default_Init_Cond_Procedure_Bodies --
1282 ----------------------------------------------
1284 procedure Build_Default_Init_Cond_Procedure_Bodies (Priv_Decls : List_Id) is
1285 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id);
1286 -- If type Typ is subject to pragma Default_Initial_Condition, build the
1287 -- body of the procedure which verifies the assumption of the pragma at
1288 -- run time. The generated body is added after the type declaration.
1290 --------------------------------------------
1291 -- Build_Default_Init_Cond_Procedure_Body --
1292 --------------------------------------------
1294 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id) is
1295 Param_Id : Entity_Id;
1296 -- The entity of the sole formal parameter of the default initial
1297 -- condition procedure.
1299 procedure Replace_Type_Reference (N : Node_Id);
1300 -- Replace a single reference to type Typ with a reference to formal
1301 -- parameter Param_Id.
1303 ----------------------------
1304 -- Replace_Type_Reference --
1305 ----------------------------
1307 procedure Replace_Type_Reference (N : Node_Id) is
1308 begin
1309 Rewrite (N, New_Occurrence_Of (Param_Id, Sloc (N)));
1310 end Replace_Type_Reference;
1312 procedure Replace_Type_References is
1313 new Replace_Type_References_Generic (Replace_Type_Reference);
1315 -- Local variables
1317 GM : constant Ghost_Mode_Type := Ghost_Mode;
1318 Loc : constant Source_Ptr := Sloc (Typ);
1319 Prag : constant Node_Id :=
1320 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1321 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1322 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Proc_Id);
1323 Body_Decl : Node_Id;
1324 Expr : Node_Id;
1325 Stmt : Node_Id;
1327 -- Start of processing for Build_Default_Init_Cond_Procedure_Body
1329 begin
1330 -- The procedure should be generated only for [sub]types subject to
1331 -- pragma Default_Initial_Condition. Types that inherit the pragma do
1332 -- not get this specialized procedure.
1334 pragma Assert (Has_Default_Init_Cond (Typ));
1335 pragma Assert (Present (Prag));
1336 pragma Assert (Present (Proc_Id));
1338 -- Nothing to do if the body was already built
1340 if Present (Corresponding_Body (Spec_Decl)) then
1341 return;
1342 end if;
1344 -- Ensure that the analysis and expansion produce Ghost nodes if the
1345 -- type itself is Ghost.
1347 Set_Ghost_Mode_From_Entity (Typ);
1349 Param_Id := First_Formal (Proc_Id);
1351 -- The pragma has an argument. Note that the argument is analyzed
1352 -- after all references to the current instance of the type are
1353 -- replaced.
1355 if Present (Pragma_Argument_Associations (Prag)) then
1356 Expr :=
1357 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
1359 if Nkind (Expr) = N_Null then
1360 Stmt := Make_Null_Statement (Loc);
1362 -- Preserve the original argument of the pragma by replicating it.
1363 -- Replace all references to the current instance of the type with
1364 -- references to the formal parameter.
1366 else
1367 Expr := New_Copy_Tree (Expr);
1368 Replace_Type_References (Expr, Typ);
1370 -- Generate:
1371 -- pragma Check (Default_Initial_Condition, <Expr>);
1373 Stmt :=
1374 Make_Pragma (Loc,
1375 Pragma_Identifier =>
1376 Make_Identifier (Loc, Name_Check),
1378 Pragma_Argument_Associations => New_List (
1379 Make_Pragma_Argument_Association (Loc,
1380 Expression =>
1381 Make_Identifier (Loc,
1382 Chars => Name_Default_Initial_Condition)),
1383 Make_Pragma_Argument_Association (Loc,
1384 Expression => Expr)));
1385 end if;
1387 -- Otherwise the pragma appears without an argument
1389 else
1390 Stmt := Make_Null_Statement (Loc);
1391 end if;
1393 -- Generate:
1394 -- procedure <Typ>Default_Init_Cond (I : <Typ>) is
1395 -- begin
1396 -- <Stmt>;
1397 -- end <Typ>Default_Init_Cond;
1399 Body_Decl :=
1400 Make_Subprogram_Body (Loc,
1401 Specification =>
1402 Copy_Separate_Tree (Specification (Spec_Decl)),
1403 Declarations => Empty_List,
1404 Handled_Statement_Sequence =>
1405 Make_Handled_Sequence_Of_Statements (Loc,
1406 Statements => New_List (Stmt)));
1408 -- Link the spec and body of the default initial condition procedure
1409 -- to prevent the generation of a duplicate body.
1411 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
1412 Set_Corresponding_Spec (Body_Decl, Proc_Id);
1414 Insert_After_And_Analyze (Declaration_Node (Typ), Body_Decl);
1416 -- Restore the original Ghost mode once analysis and expansion have
1417 -- taken place.
1419 Ghost_Mode := GM;
1420 end Build_Default_Init_Cond_Procedure_Body;
1422 -- Local variables
1424 Decl : Node_Id;
1425 Typ : Entity_Id;
1427 -- Start of processing for Build_Default_Init_Cond_Procedure_Bodies
1429 begin
1430 -- Inspect the private declarations looking for [sub]type declarations
1432 Decl := First (Priv_Decls);
1433 while Present (Decl) loop
1434 if Nkind_In (Decl, N_Full_Type_Declaration,
1435 N_Subtype_Declaration)
1436 then
1437 Typ := Defining_Entity (Decl);
1439 -- Guard against partially decorate types due to previous errors
1441 if Is_Type (Typ) then
1443 -- If the type is subject to pragma Default_Initial_Condition,
1444 -- generate the body of the internal procedure which verifies
1445 -- the assertion of the pragma at run time.
1447 if Has_Default_Init_Cond (Typ) then
1448 Build_Default_Init_Cond_Procedure_Body (Typ);
1450 -- A derived type inherits the default initial condition
1451 -- procedure from its parent type.
1453 elsif Has_Inherited_Default_Init_Cond (Typ) then
1454 Inherit_Default_Init_Cond_Procedure (Typ);
1455 end if;
1456 end if;
1457 end if;
1459 Next (Decl);
1460 end loop;
1461 end Build_Default_Init_Cond_Procedure_Bodies;
1463 ---------------------------------------------------
1464 -- Build_Default_Init_Cond_Procedure_Declaration --
1465 ---------------------------------------------------
1467 procedure Build_Default_Init_Cond_Procedure_Declaration (Typ : Entity_Id) is
1468 GM : constant Ghost_Mode_Type := Ghost_Mode;
1469 Loc : constant Source_Ptr := Sloc (Typ);
1470 Prag : constant Node_Id :=
1471 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1472 Proc_Id : Entity_Id;
1474 begin
1475 -- The procedure should be generated only for types subject to pragma
1476 -- Default_Initial_Condition. Types that inherit the pragma do not get
1477 -- this specialized procedure.
1479 pragma Assert (Has_Default_Init_Cond (Typ));
1480 pragma Assert (Present (Prag));
1482 -- Nothing to do if default initial condition procedure already built
1484 if Present (Default_Init_Cond_Procedure (Typ)) then
1485 return;
1486 end if;
1488 -- Ensure that the analysis and expansion produce Ghost nodes if the
1489 -- type itself is Ghost.
1491 Set_Ghost_Mode_From_Entity (Typ);
1493 Proc_Id :=
1494 Make_Defining_Identifier (Loc,
1495 Chars => New_External_Name (Chars (Typ), "Default_Init_Cond"));
1497 -- Associate default initial condition procedure with the private type
1499 Set_Ekind (Proc_Id, E_Procedure);
1500 Set_Is_Default_Init_Cond_Procedure (Proc_Id);
1501 Set_Default_Init_Cond_Procedure (Typ, Proc_Id);
1503 -- Mark the default initial condition procedure explicitly as Ghost
1504 -- because it does not come from source.
1506 if Ghost_Mode > None then
1507 Set_Is_Ghost_Entity (Proc_Id);
1508 end if;
1510 -- Generate:
1511 -- procedure <Typ>Default_Init_Cond (Inn : <Typ>);
1513 Insert_After_And_Analyze (Prag,
1514 Make_Subprogram_Declaration (Loc,
1515 Specification =>
1516 Make_Procedure_Specification (Loc,
1517 Defining_Unit_Name => Proc_Id,
1518 Parameter_Specifications => New_List (
1519 Make_Parameter_Specification (Loc,
1520 Defining_Identifier => Make_Temporary (Loc, 'I'),
1521 Parameter_Type => New_Occurrence_Of (Typ, Loc))))));
1523 -- Restore the original Ghost mode once analysis and expansion have
1524 -- taken place.
1526 Ghost_Mode := GM;
1527 end Build_Default_Init_Cond_Procedure_Declaration;
1529 ---------------------------
1530 -- Build_Default_Subtype --
1531 ---------------------------
1533 function Build_Default_Subtype
1534 (T : Entity_Id;
1535 N : Node_Id) return Entity_Id
1537 Loc : constant Source_Ptr := Sloc (N);
1538 Disc : Entity_Id;
1540 Bas : Entity_Id;
1541 -- The base type that is to be constrained by the defaults
1543 begin
1544 if not Has_Discriminants (T) or else Is_Constrained (T) then
1545 return T;
1546 end if;
1548 Bas := Base_Type (T);
1550 -- If T is non-private but its base type is private, this is the
1551 -- completion of a subtype declaration whose parent type is private
1552 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1553 -- are to be found in the full view of the base. Check that the private
1554 -- status of T and its base differ.
1556 if Is_Private_Type (Bas)
1557 and then not Is_Private_Type (T)
1558 and then Present (Full_View (Bas))
1559 then
1560 Bas := Full_View (Bas);
1561 end if;
1563 Disc := First_Discriminant (T);
1565 if No (Discriminant_Default_Value (Disc)) then
1566 return T;
1567 end if;
1569 declare
1570 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1571 Constraints : constant List_Id := New_List;
1572 Decl : Node_Id;
1574 begin
1575 while Present (Disc) loop
1576 Append_To (Constraints,
1577 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1578 Next_Discriminant (Disc);
1579 end loop;
1581 Decl :=
1582 Make_Subtype_Declaration (Loc,
1583 Defining_Identifier => Act,
1584 Subtype_Indication =>
1585 Make_Subtype_Indication (Loc,
1586 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1587 Constraint =>
1588 Make_Index_Or_Discriminant_Constraint (Loc,
1589 Constraints => Constraints)));
1591 Insert_Action (N, Decl);
1593 -- If the context is a component declaration the subtype declaration
1594 -- will be analyzed when the enclosing type is frozen, otherwise do
1595 -- it now.
1597 if Ekind (Current_Scope) /= E_Record_Type then
1598 Analyze (Decl);
1599 end if;
1601 return Act;
1602 end;
1603 end Build_Default_Subtype;
1605 --------------------------------------------
1606 -- Build_Discriminal_Subtype_Of_Component --
1607 --------------------------------------------
1609 function Build_Discriminal_Subtype_Of_Component
1610 (T : Entity_Id) return Node_Id
1612 Loc : constant Source_Ptr := Sloc (T);
1613 D : Elmt_Id;
1614 Id : Node_Id;
1616 function Build_Discriminal_Array_Constraint return List_Id;
1617 -- If one or more of the bounds of the component depends on
1618 -- discriminants, build actual constraint using the discriminants
1619 -- of the prefix.
1621 function Build_Discriminal_Record_Constraint return List_Id;
1622 -- Similar to previous one, for discriminated components constrained by
1623 -- the discriminant of the enclosing object.
1625 ----------------------------------------
1626 -- Build_Discriminal_Array_Constraint --
1627 ----------------------------------------
1629 function Build_Discriminal_Array_Constraint return List_Id is
1630 Constraints : constant List_Id := New_List;
1631 Indx : Node_Id;
1632 Hi : Node_Id;
1633 Lo : Node_Id;
1634 Old_Hi : Node_Id;
1635 Old_Lo : Node_Id;
1637 begin
1638 Indx := First_Index (T);
1639 while Present (Indx) loop
1640 Old_Lo := Type_Low_Bound (Etype (Indx));
1641 Old_Hi := Type_High_Bound (Etype (Indx));
1643 if Denotes_Discriminant (Old_Lo) then
1644 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1646 else
1647 Lo := New_Copy_Tree (Old_Lo);
1648 end if;
1650 if Denotes_Discriminant (Old_Hi) then
1651 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1653 else
1654 Hi := New_Copy_Tree (Old_Hi);
1655 end if;
1657 Append (Make_Range (Loc, Lo, Hi), Constraints);
1658 Next_Index (Indx);
1659 end loop;
1661 return Constraints;
1662 end Build_Discriminal_Array_Constraint;
1664 -----------------------------------------
1665 -- Build_Discriminal_Record_Constraint --
1666 -----------------------------------------
1668 function Build_Discriminal_Record_Constraint return List_Id is
1669 Constraints : constant List_Id := New_List;
1670 D : Elmt_Id;
1671 D_Val : Node_Id;
1673 begin
1674 D := First_Elmt (Discriminant_Constraint (T));
1675 while Present (D) loop
1676 if Denotes_Discriminant (Node (D)) then
1677 D_Val :=
1678 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1679 else
1680 D_Val := New_Copy_Tree (Node (D));
1681 end if;
1683 Append (D_Val, Constraints);
1684 Next_Elmt (D);
1685 end loop;
1687 return Constraints;
1688 end Build_Discriminal_Record_Constraint;
1690 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1692 begin
1693 if Ekind (T) = E_Array_Subtype then
1694 Id := First_Index (T);
1695 while Present (Id) loop
1696 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1697 or else
1698 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1699 then
1700 return Build_Component_Subtype
1701 (Build_Discriminal_Array_Constraint, Loc, T);
1702 end if;
1704 Next_Index (Id);
1705 end loop;
1707 elsif Ekind (T) = E_Record_Subtype
1708 and then Has_Discriminants (T)
1709 and then not Has_Unknown_Discriminants (T)
1710 then
1711 D := First_Elmt (Discriminant_Constraint (T));
1712 while Present (D) loop
1713 if Denotes_Discriminant (Node (D)) then
1714 return Build_Component_Subtype
1715 (Build_Discriminal_Record_Constraint, Loc, T);
1716 end if;
1718 Next_Elmt (D);
1719 end loop;
1720 end if;
1722 -- If none of the above, the actual and nominal subtypes are the same
1724 return Empty;
1725 end Build_Discriminal_Subtype_Of_Component;
1727 ------------------------------
1728 -- Build_Elaboration_Entity --
1729 ------------------------------
1731 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1732 Loc : constant Source_Ptr := Sloc (N);
1733 Decl : Node_Id;
1734 Elab_Ent : Entity_Id;
1736 procedure Set_Package_Name (Ent : Entity_Id);
1737 -- Given an entity, sets the fully qualified name of the entity in
1738 -- Name_Buffer, with components separated by double underscores. This
1739 -- is a recursive routine that climbs the scope chain to Standard.
1741 ----------------------
1742 -- Set_Package_Name --
1743 ----------------------
1745 procedure Set_Package_Name (Ent : Entity_Id) is
1746 begin
1747 if Scope (Ent) /= Standard_Standard then
1748 Set_Package_Name (Scope (Ent));
1750 declare
1751 Nam : constant String := Get_Name_String (Chars (Ent));
1752 begin
1753 Name_Buffer (Name_Len + 1) := '_';
1754 Name_Buffer (Name_Len + 2) := '_';
1755 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1756 Name_Len := Name_Len + Nam'Length + 2;
1757 end;
1759 else
1760 Get_Name_String (Chars (Ent));
1761 end if;
1762 end Set_Package_Name;
1764 -- Start of processing for Build_Elaboration_Entity
1766 begin
1767 -- Ignore call if already constructed
1769 if Present (Elaboration_Entity (Spec_Id)) then
1770 return;
1772 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1773 -- no role in analysis.
1775 elsif ASIS_Mode then
1776 return;
1778 -- See if we need elaboration entity. We always need it for the dynamic
1779 -- elaboration model, since it is needed to properly generate the PE
1780 -- exception for access before elaboration.
1782 elsif Dynamic_Elaboration_Checks then
1783 null;
1785 -- For the static model, we don't need the elaboration counter if this
1786 -- unit is sure to have no elaboration code, since that means there
1787 -- is no elaboration unit to be called. Note that we can't just decide
1788 -- after the fact by looking to see whether there was elaboration code,
1789 -- because that's too late to make this decision.
1791 elsif Restriction_Active (No_Elaboration_Code) then
1792 return;
1794 -- Similarly, for the static model, we can skip the elaboration counter
1795 -- if we have the No_Multiple_Elaboration restriction, since for the
1796 -- static model, that's the only purpose of the counter (to avoid
1797 -- multiple elaboration).
1799 elsif Restriction_Active (No_Multiple_Elaboration) then
1800 return;
1801 end if;
1803 -- Here we need the elaboration entity
1805 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1806 -- name with dots replaced by double underscore. We have to manually
1807 -- construct this name, since it will be elaborated in the outer scope,
1808 -- and thus will not have the unit name automatically prepended.
1810 Set_Package_Name (Spec_Id);
1811 Add_Str_To_Name_Buffer ("_E");
1813 -- Create elaboration counter
1815 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1816 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1818 Decl :=
1819 Make_Object_Declaration (Loc,
1820 Defining_Identifier => Elab_Ent,
1821 Object_Definition =>
1822 New_Occurrence_Of (Standard_Short_Integer, Loc),
1823 Expression => Make_Integer_Literal (Loc, Uint_0));
1825 Push_Scope (Standard_Standard);
1826 Add_Global_Declaration (Decl);
1827 Pop_Scope;
1829 -- Reset True_Constant indication, since we will indeed assign a value
1830 -- to the variable in the binder main. We also kill the Current_Value
1831 -- and Last_Assignment fields for the same reason.
1833 Set_Is_True_Constant (Elab_Ent, False);
1834 Set_Current_Value (Elab_Ent, Empty);
1835 Set_Last_Assignment (Elab_Ent, Empty);
1837 -- We do not want any further qualification of the name (if we did not
1838 -- do this, we would pick up the name of the generic package in the case
1839 -- of a library level generic instantiation).
1841 Set_Has_Qualified_Name (Elab_Ent);
1842 Set_Has_Fully_Qualified_Name (Elab_Ent);
1843 end Build_Elaboration_Entity;
1845 --------------------------------
1846 -- Build_Explicit_Dereference --
1847 --------------------------------
1849 procedure Build_Explicit_Dereference
1850 (Expr : Node_Id;
1851 Disc : Entity_Id)
1853 Loc : constant Source_Ptr := Sloc (Expr);
1855 begin
1856 -- An entity of a type with a reference aspect is overloaded with
1857 -- both interpretations: with and without the dereference. Now that
1858 -- the dereference is made explicit, set the type of the node properly,
1859 -- to prevent anomalies in the backend. Same if the expression is an
1860 -- overloaded function call whose return type has a reference aspect.
1862 if Is_Entity_Name (Expr) then
1863 Set_Etype (Expr, Etype (Entity (Expr)));
1865 elsif Nkind (Expr) = N_Function_Call then
1866 Set_Etype (Expr, Etype (Name (Expr)));
1867 end if;
1869 Set_Is_Overloaded (Expr, False);
1871 -- The expression will often be a generalized indexing that yields a
1872 -- container element that is then dereferenced, in which case the
1873 -- generalized indexing call is also non-overloaded.
1875 if Nkind (Expr) = N_Indexed_Component
1876 and then Present (Generalized_Indexing (Expr))
1877 then
1878 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1879 end if;
1881 Rewrite (Expr,
1882 Make_Explicit_Dereference (Loc,
1883 Prefix =>
1884 Make_Selected_Component (Loc,
1885 Prefix => Relocate_Node (Expr),
1886 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1887 Set_Etype (Prefix (Expr), Etype (Disc));
1888 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1889 end Build_Explicit_Dereference;
1891 -----------------------------------
1892 -- Cannot_Raise_Constraint_Error --
1893 -----------------------------------
1895 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1896 begin
1897 if Compile_Time_Known_Value (Expr) then
1898 return True;
1900 elsif Do_Range_Check (Expr) then
1901 return False;
1903 elsif Raises_Constraint_Error (Expr) then
1904 return False;
1906 else
1907 case Nkind (Expr) is
1908 when N_Identifier =>
1909 return True;
1911 when N_Expanded_Name =>
1912 return True;
1914 when N_Selected_Component =>
1915 return not Do_Discriminant_Check (Expr);
1917 when N_Attribute_Reference =>
1918 if Do_Overflow_Check (Expr) then
1919 return False;
1921 elsif No (Expressions (Expr)) then
1922 return True;
1924 else
1925 declare
1926 N : Node_Id;
1928 begin
1929 N := First (Expressions (Expr));
1930 while Present (N) loop
1931 if Cannot_Raise_Constraint_Error (N) then
1932 Next (N);
1933 else
1934 return False;
1935 end if;
1936 end loop;
1938 return True;
1939 end;
1940 end if;
1942 when N_Type_Conversion =>
1943 if Do_Overflow_Check (Expr)
1944 or else Do_Length_Check (Expr)
1945 or else Do_Tag_Check (Expr)
1946 then
1947 return False;
1948 else
1949 return Cannot_Raise_Constraint_Error (Expression (Expr));
1950 end if;
1952 when N_Unchecked_Type_Conversion =>
1953 return Cannot_Raise_Constraint_Error (Expression (Expr));
1955 when N_Unary_Op =>
1956 if Do_Overflow_Check (Expr) then
1957 return False;
1958 else
1959 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1960 end if;
1962 when N_Op_Divide |
1963 N_Op_Mod |
1964 N_Op_Rem
1966 if Do_Division_Check (Expr)
1967 or else
1968 Do_Overflow_Check (Expr)
1969 then
1970 return False;
1971 else
1972 return
1973 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1974 and then
1975 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1976 end if;
1978 when N_Op_Add |
1979 N_Op_And |
1980 N_Op_Concat |
1981 N_Op_Eq |
1982 N_Op_Expon |
1983 N_Op_Ge |
1984 N_Op_Gt |
1985 N_Op_Le |
1986 N_Op_Lt |
1987 N_Op_Multiply |
1988 N_Op_Ne |
1989 N_Op_Or |
1990 N_Op_Rotate_Left |
1991 N_Op_Rotate_Right |
1992 N_Op_Shift_Left |
1993 N_Op_Shift_Right |
1994 N_Op_Shift_Right_Arithmetic |
1995 N_Op_Subtract |
1996 N_Op_Xor
1998 if Do_Overflow_Check (Expr) then
1999 return False;
2000 else
2001 return
2002 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
2003 and then
2004 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
2005 end if;
2007 when others =>
2008 return False;
2009 end case;
2010 end if;
2011 end Cannot_Raise_Constraint_Error;
2013 -----------------------------------------
2014 -- Check_Dynamically_Tagged_Expression --
2015 -----------------------------------------
2017 procedure Check_Dynamically_Tagged_Expression
2018 (Expr : Node_Id;
2019 Typ : Entity_Id;
2020 Related_Nod : Node_Id)
2022 begin
2023 pragma Assert (Is_Tagged_Type (Typ));
2025 -- In order to avoid spurious errors when analyzing the expanded code,
2026 -- this check is done only for nodes that come from source and for
2027 -- actuals of generic instantiations.
2029 if (Comes_From_Source (Related_Nod)
2030 or else In_Generic_Actual (Expr))
2031 and then (Is_Class_Wide_Type (Etype (Expr))
2032 or else Is_Dynamically_Tagged (Expr))
2033 and then Is_Tagged_Type (Typ)
2034 and then not Is_Class_Wide_Type (Typ)
2035 then
2036 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
2037 end if;
2038 end Check_Dynamically_Tagged_Expression;
2040 --------------------------
2041 -- Check_Fully_Declared --
2042 --------------------------
2044 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2045 begin
2046 if Ekind (T) = E_Incomplete_Type then
2048 -- Ada 2005 (AI-50217): If the type is available through a limited
2049 -- with_clause, verify that its full view has been analyzed.
2051 if From_Limited_With (T)
2052 and then Present (Non_Limited_View (T))
2053 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2054 then
2055 -- The non-limited view is fully declared
2057 null;
2059 else
2060 Error_Msg_NE
2061 ("premature usage of incomplete}", N, First_Subtype (T));
2062 end if;
2064 -- Need comments for these tests ???
2066 elsif Has_Private_Component (T)
2067 and then not Is_Generic_Type (Root_Type (T))
2068 and then not In_Spec_Expression
2069 then
2070 -- Special case: if T is the anonymous type created for a single
2071 -- task or protected object, use the name of the source object.
2073 if Is_Concurrent_Type (T)
2074 and then not Comes_From_Source (T)
2075 and then Nkind (N) = N_Object_Declaration
2076 then
2077 Error_Msg_NE
2078 ("type of& has incomplete component",
2079 N, Defining_Identifier (N));
2080 else
2081 Error_Msg_NE
2082 ("premature usage of incomplete}",
2083 N, First_Subtype (T));
2084 end if;
2085 end if;
2086 end Check_Fully_Declared;
2088 -------------------------------------
2089 -- Check_Function_Writable_Actuals --
2090 -------------------------------------
2092 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2093 Writable_Actuals_List : Elist_Id := No_Elist;
2094 Identifiers_List : Elist_Id := No_Elist;
2095 Aggr_Error_Node : Node_Id := Empty;
2096 Error_Node : Node_Id := Empty;
2098 procedure Collect_Identifiers (N : Node_Id);
2099 -- In a single traversal of subtree N collect in Writable_Actuals_List
2100 -- all the actuals of functions with writable actuals, and in the list
2101 -- Identifiers_List collect all the identifiers that are not actuals of
2102 -- functions with writable actuals. If a writable actual is referenced
2103 -- twice as writable actual then Error_Node is set to reference its
2104 -- second occurrence, the error is reported, and the tree traversal
2105 -- is abandoned.
2107 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2108 -- Return the entity associated with the function call
2110 procedure Preanalyze_Without_Errors (N : Node_Id);
2111 -- Preanalyze N without reporting errors. Very dubious, you can't just
2112 -- go analyzing things more than once???
2114 -------------------------
2115 -- Collect_Identifiers --
2116 -------------------------
2118 procedure Collect_Identifiers (N : Node_Id) is
2120 function Check_Node (N : Node_Id) return Traverse_Result;
2121 -- Process a single node during the tree traversal to collect the
2122 -- writable actuals of functions and all the identifiers which are
2123 -- not writable actuals of functions.
2125 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2126 -- Returns True if List has a node whose Entity is Entity (N)
2128 -------------------------
2129 -- Check_Function_Call --
2130 -------------------------
2132 function Check_Node (N : Node_Id) return Traverse_Result is
2133 Is_Writable_Actual : Boolean := False;
2134 Id : Entity_Id;
2136 begin
2137 if Nkind (N) = N_Identifier then
2139 -- No analysis possible if the entity is not decorated
2141 if No (Entity (N)) then
2142 return Skip;
2144 -- Don't collect identifiers of packages, called functions, etc
2146 elsif Ekind_In (Entity (N), E_Package,
2147 E_Function,
2148 E_Procedure,
2149 E_Entry)
2150 then
2151 return Skip;
2153 -- For rewritten nodes, continue the traversal in the original
2154 -- subtree. Needed to handle aggregates in original expressions
2155 -- extracted from the tree by Remove_Side_Effects.
2157 elsif Is_Rewrite_Substitution (N) then
2158 Collect_Identifiers (Original_Node (N));
2159 return Skip;
2161 -- For now we skip aggregate discriminants, since they require
2162 -- performing the analysis in two phases to identify conflicts:
2163 -- first one analyzing discriminants and second one analyzing
2164 -- the rest of components (since at run time, discriminants are
2165 -- evaluated prior to components): too much computation cost
2166 -- to identify a corner case???
2168 elsif Nkind (Parent (N)) = N_Component_Association
2169 and then Nkind_In (Parent (Parent (N)),
2170 N_Aggregate,
2171 N_Extension_Aggregate)
2172 then
2173 declare
2174 Choice : constant Node_Id := First (Choices (Parent (N)));
2176 begin
2177 if Ekind (Entity (N)) = E_Discriminant then
2178 return Skip;
2180 elsif Expression (Parent (N)) = N
2181 and then Nkind (Choice) = N_Identifier
2182 and then Ekind (Entity (Choice)) = E_Discriminant
2183 then
2184 return Skip;
2185 end if;
2186 end;
2188 -- Analyze if N is a writable actual of a function
2190 elsif Nkind (Parent (N)) = N_Function_Call then
2191 declare
2192 Call : constant Node_Id := Parent (N);
2193 Actual : Node_Id;
2194 Formal : Node_Id;
2196 begin
2197 Id := Get_Function_Id (Call);
2199 -- In case of previous error, no check is possible
2201 if No (Id) then
2202 return Abandon;
2203 end if;
2205 if Ekind_In (Id, E_Function, E_Generic_Function)
2206 and then Has_Out_Or_In_Out_Parameter (Id)
2207 then
2208 Formal := First_Formal (Id);
2209 Actual := First_Actual (Call);
2210 while Present (Actual) and then Present (Formal) loop
2211 if Actual = N then
2212 if Ekind_In (Formal, E_Out_Parameter,
2213 E_In_Out_Parameter)
2214 then
2215 Is_Writable_Actual := True;
2216 end if;
2218 exit;
2219 end if;
2221 Next_Formal (Formal);
2222 Next_Actual (Actual);
2223 end loop;
2224 end if;
2225 end;
2226 end if;
2228 if Is_Writable_Actual then
2229 if Contains (Writable_Actuals_List, N) then
2231 -- Report the error on the second occurrence of the
2232 -- identifier. We cannot assume that N is the second
2233 -- occurrence, since Traverse_Func walks through Field2
2234 -- last (see comment in the body of Traverse_Func).
2236 declare
2237 Elmt : Elmt_Id;
2239 begin
2240 Elmt := First_Elmt (Writable_Actuals_List);
2241 while Present (Elmt)
2242 and then Entity (Node (Elmt)) /= Entity (N)
2243 loop
2244 Next_Elmt (Elmt);
2245 end loop;
2247 if Sloc (N) > Sloc (Node (Elmt)) then
2248 Error_Node := N;
2249 else
2250 Error_Node := Node (Elmt);
2251 end if;
2253 Error_Msg_NE
2254 ("value may be affected by call to & "
2255 & "because order of evaluation is arbitrary",
2256 Error_Node, Id);
2257 return Abandon;
2258 end;
2259 end if;
2261 Append_New_Elmt (N, To => Writable_Actuals_List);
2263 else
2264 if Identifiers_List = No_Elist then
2265 Identifiers_List := New_Elmt_List;
2266 end if;
2268 Append_Unique_Elmt (N, Identifiers_List);
2269 end if;
2270 end if;
2272 return OK;
2273 end Check_Node;
2275 --------------
2276 -- Contains --
2277 --------------
2279 function Contains
2280 (List : Elist_Id;
2281 N : Node_Id) return Boolean
2283 pragma Assert (Nkind (N) in N_Has_Entity);
2285 Elmt : Elmt_Id;
2287 begin
2288 if List = No_Elist then
2289 return False;
2290 end if;
2292 Elmt := First_Elmt (List);
2293 while Present (Elmt) loop
2294 if Entity (Node (Elmt)) = Entity (N) then
2295 return True;
2296 else
2297 Next_Elmt (Elmt);
2298 end if;
2299 end loop;
2301 return False;
2302 end Contains;
2304 ------------------
2305 -- Do_Traversal --
2306 ------------------
2308 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2309 -- The traversal procedure
2311 -- Start of processing for Collect_Identifiers
2313 begin
2314 if Present (Error_Node) then
2315 return;
2316 end if;
2318 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2319 return;
2320 end if;
2322 Do_Traversal (N);
2323 end Collect_Identifiers;
2325 ---------------------
2326 -- Get_Function_Id --
2327 ---------------------
2329 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2330 Nam : constant Node_Id := Name (Call);
2331 Id : Entity_Id;
2333 begin
2334 if Nkind (Nam) = N_Explicit_Dereference then
2335 Id := Etype (Nam);
2336 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2338 elsif Nkind (Nam) = N_Selected_Component then
2339 Id := Entity (Selector_Name (Nam));
2341 elsif Nkind (Nam) = N_Indexed_Component then
2342 Id := Entity (Selector_Name (Prefix (Nam)));
2344 else
2345 Id := Entity (Nam);
2346 end if;
2348 return Id;
2349 end Get_Function_Id;
2351 ---------------------------
2352 -- Preanalyze_Expression --
2353 ---------------------------
2355 procedure Preanalyze_Without_Errors (N : Node_Id) is
2356 Status : constant Boolean := Get_Ignore_Errors;
2357 begin
2358 Set_Ignore_Errors (True);
2359 Preanalyze (N);
2360 Set_Ignore_Errors (Status);
2361 end Preanalyze_Without_Errors;
2363 -- Start of processing for Check_Function_Writable_Actuals
2365 begin
2366 -- The check only applies to Ada 2012 code on which Check_Actuals has
2367 -- been set, and only to constructs that have multiple constituents
2368 -- whose order of evaluation is not specified by the language.
2370 if Ada_Version < Ada_2012
2371 or else not Check_Actuals (N)
2372 or else (not (Nkind (N) in N_Op)
2373 and then not (Nkind (N) in N_Membership_Test)
2374 and then not Nkind_In (N, N_Range,
2375 N_Aggregate,
2376 N_Extension_Aggregate,
2377 N_Full_Type_Declaration,
2378 N_Function_Call,
2379 N_Procedure_Call_Statement,
2380 N_Entry_Call_Statement))
2381 or else (Nkind (N) = N_Full_Type_Declaration
2382 and then not Is_Record_Type (Defining_Identifier (N)))
2384 -- In addition, this check only applies to source code, not to code
2385 -- generated by constraint checks.
2387 or else not Comes_From_Source (N)
2388 then
2389 return;
2390 end if;
2392 -- If a construct C has two or more direct constituents that are names
2393 -- or expressions whose evaluation may occur in an arbitrary order, at
2394 -- least one of which contains a function call with an in out or out
2395 -- parameter, then the construct is legal only if: for each name N that
2396 -- is passed as a parameter of mode in out or out to some inner function
2397 -- call C2 (not including the construct C itself), there is no other
2398 -- name anywhere within a direct constituent of the construct C other
2399 -- than the one containing C2, that is known to refer to the same
2400 -- object (RM 6.4.1(6.17/3)).
2402 case Nkind (N) is
2403 when N_Range =>
2404 Collect_Identifiers (Low_Bound (N));
2405 Collect_Identifiers (High_Bound (N));
2407 when N_Op | N_Membership_Test =>
2408 declare
2409 Expr : Node_Id;
2411 begin
2412 Collect_Identifiers (Left_Opnd (N));
2414 if Present (Right_Opnd (N)) then
2415 Collect_Identifiers (Right_Opnd (N));
2416 end if;
2418 if Nkind_In (N, N_In, N_Not_In)
2419 and then Present (Alternatives (N))
2420 then
2421 Expr := First (Alternatives (N));
2422 while Present (Expr) loop
2423 Collect_Identifiers (Expr);
2425 Next (Expr);
2426 end loop;
2427 end if;
2428 end;
2430 when N_Full_Type_Declaration =>
2431 declare
2432 function Get_Record_Part (N : Node_Id) return Node_Id;
2433 -- Return the record part of this record type definition
2435 function Get_Record_Part (N : Node_Id) return Node_Id is
2436 Type_Def : constant Node_Id := Type_Definition (N);
2437 begin
2438 if Nkind (Type_Def) = N_Derived_Type_Definition then
2439 return Record_Extension_Part (Type_Def);
2440 else
2441 return Type_Def;
2442 end if;
2443 end Get_Record_Part;
2445 Comp : Node_Id;
2446 Def_Id : Entity_Id := Defining_Identifier (N);
2447 Rec : Node_Id := Get_Record_Part (N);
2449 begin
2450 -- No need to perform any analysis if the record has no
2451 -- components
2453 if No (Rec) or else No (Component_List (Rec)) then
2454 return;
2455 end if;
2457 -- Collect the identifiers starting from the deepest
2458 -- derivation. Done to report the error in the deepest
2459 -- derivation.
2461 loop
2462 if Present (Component_List (Rec)) then
2463 Comp := First (Component_Items (Component_List (Rec)));
2464 while Present (Comp) loop
2465 if Nkind (Comp) = N_Component_Declaration
2466 and then Present (Expression (Comp))
2467 then
2468 Collect_Identifiers (Expression (Comp));
2469 end if;
2471 Next (Comp);
2472 end loop;
2473 end if;
2475 exit when No (Underlying_Type (Etype (Def_Id)))
2476 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2477 = Def_Id;
2479 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2480 Rec := Get_Record_Part (Parent (Def_Id));
2481 end loop;
2482 end;
2484 when N_Subprogram_Call |
2485 N_Entry_Call_Statement =>
2486 declare
2487 Id : constant Entity_Id := Get_Function_Id (N);
2488 Formal : Node_Id;
2489 Actual : Node_Id;
2491 begin
2492 Formal := First_Formal (Id);
2493 Actual := First_Actual (N);
2494 while Present (Actual) and then Present (Formal) loop
2495 if Ekind_In (Formal, E_Out_Parameter,
2496 E_In_Out_Parameter)
2497 then
2498 Collect_Identifiers (Actual);
2499 end if;
2501 Next_Formal (Formal);
2502 Next_Actual (Actual);
2503 end loop;
2504 end;
2506 when N_Aggregate |
2507 N_Extension_Aggregate =>
2508 declare
2509 Assoc : Node_Id;
2510 Choice : Node_Id;
2511 Comp_Expr : Node_Id;
2513 begin
2514 -- Handle the N_Others_Choice of array aggregates with static
2515 -- bounds. There is no need to perform this analysis in
2516 -- aggregates without static bounds since we cannot evaluate
2517 -- if the N_Others_Choice covers several elements. There is
2518 -- no need to handle the N_Others choice of record aggregates
2519 -- since at this stage it has been already expanded by
2520 -- Resolve_Record_Aggregate.
2522 if Is_Array_Type (Etype (N))
2523 and then Nkind (N) = N_Aggregate
2524 and then Present (Aggregate_Bounds (N))
2525 and then Compile_Time_Known_Bounds (Etype (N))
2526 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2528 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2529 then
2530 declare
2531 Count_Components : Uint := Uint_0;
2532 Num_Components : Uint;
2533 Others_Assoc : Node_Id;
2534 Others_Choice : Node_Id := Empty;
2535 Others_Box_Present : Boolean := False;
2537 begin
2538 -- Count positional associations
2540 if Present (Expressions (N)) then
2541 Comp_Expr := First (Expressions (N));
2542 while Present (Comp_Expr) loop
2543 Count_Components := Count_Components + 1;
2544 Next (Comp_Expr);
2545 end loop;
2546 end if;
2548 -- Count the rest of elements and locate the N_Others
2549 -- choice (if any)
2551 Assoc := First (Component_Associations (N));
2552 while Present (Assoc) loop
2553 Choice := First (Choices (Assoc));
2554 while Present (Choice) loop
2555 if Nkind (Choice) = N_Others_Choice then
2556 Others_Assoc := Assoc;
2557 Others_Choice := Choice;
2558 Others_Box_Present := Box_Present (Assoc);
2560 -- Count several components
2562 elsif Nkind_In (Choice, N_Range,
2563 N_Subtype_Indication)
2564 or else (Is_Entity_Name (Choice)
2565 and then Is_Type (Entity (Choice)))
2566 then
2567 declare
2568 L, H : Node_Id;
2569 begin
2570 Get_Index_Bounds (Choice, L, H);
2571 pragma Assert
2572 (Compile_Time_Known_Value (L)
2573 and then Compile_Time_Known_Value (H));
2574 Count_Components :=
2575 Count_Components
2576 + Expr_Value (H) - Expr_Value (L) + 1;
2577 end;
2579 -- Count single component. No other case available
2580 -- since we are handling an aggregate with static
2581 -- bounds.
2583 else
2584 pragma Assert (Is_OK_Static_Expression (Choice)
2585 or else Nkind (Choice) = N_Identifier
2586 or else Nkind (Choice) = N_Integer_Literal);
2588 Count_Components := Count_Components + 1;
2589 end if;
2591 Next (Choice);
2592 end loop;
2594 Next (Assoc);
2595 end loop;
2597 Num_Components :=
2598 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2599 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2601 pragma Assert (Count_Components <= Num_Components);
2603 -- Handle the N_Others choice if it covers several
2604 -- components
2606 if Present (Others_Choice)
2607 and then (Num_Components - Count_Components) > 1
2608 then
2609 if not Others_Box_Present then
2611 -- At this stage, if expansion is active, the
2612 -- expression of the others choice has not been
2613 -- analyzed. Hence we generate a duplicate and
2614 -- we analyze it silently to have available the
2615 -- minimum decoration required to collect the
2616 -- identifiers.
2618 if not Expander_Active then
2619 Comp_Expr := Expression (Others_Assoc);
2620 else
2621 Comp_Expr :=
2622 New_Copy_Tree (Expression (Others_Assoc));
2623 Preanalyze_Without_Errors (Comp_Expr);
2624 end if;
2626 Collect_Identifiers (Comp_Expr);
2628 if Writable_Actuals_List /= No_Elist then
2630 -- As suggested by Robert, at current stage we
2631 -- report occurrences of this case as warnings.
2633 Error_Msg_N
2634 ("writable function parameter may affect "
2635 & "value in other component because order "
2636 & "of evaluation is unspecified??",
2637 Node (First_Elmt (Writable_Actuals_List)));
2638 end if;
2639 end if;
2640 end if;
2641 end;
2643 -- For an array aggregate, a discrete_choice_list that has
2644 -- a nonstatic range is considered as two or more separate
2645 -- occurrences of the expression (RM 6.4.1(20/3)).
2647 elsif Is_Array_Type (Etype (N))
2648 and then Nkind (N) = N_Aggregate
2649 and then Present (Aggregate_Bounds (N))
2650 and then not Compile_Time_Known_Bounds (Etype (N))
2651 then
2652 -- Collect identifiers found in the dynamic bounds
2654 declare
2655 Count_Components : Natural := 0;
2656 Low, High : Node_Id;
2658 begin
2659 Assoc := First (Component_Associations (N));
2660 while Present (Assoc) loop
2661 Choice := First (Choices (Assoc));
2662 while Present (Choice) loop
2663 if Nkind_In (Choice, N_Range,
2664 N_Subtype_Indication)
2665 or else (Is_Entity_Name (Choice)
2666 and then Is_Type (Entity (Choice)))
2667 then
2668 Get_Index_Bounds (Choice, Low, High);
2670 if not Compile_Time_Known_Value (Low) then
2671 Collect_Identifiers (Low);
2673 if No (Aggr_Error_Node) then
2674 Aggr_Error_Node := Low;
2675 end if;
2676 end if;
2678 if not Compile_Time_Known_Value (High) then
2679 Collect_Identifiers (High);
2681 if No (Aggr_Error_Node) then
2682 Aggr_Error_Node := High;
2683 end if;
2684 end if;
2686 -- The RM rule is violated if there is more than
2687 -- a single choice in a component association.
2689 else
2690 Count_Components := Count_Components + 1;
2692 if No (Aggr_Error_Node)
2693 and then Count_Components > 1
2694 then
2695 Aggr_Error_Node := Choice;
2696 end if;
2698 if not Compile_Time_Known_Value (Choice) then
2699 Collect_Identifiers (Choice);
2700 end if;
2701 end if;
2703 Next (Choice);
2704 end loop;
2706 Next (Assoc);
2707 end loop;
2708 end;
2709 end if;
2711 -- Handle ancestor part of extension aggregates
2713 if Nkind (N) = N_Extension_Aggregate then
2714 Collect_Identifiers (Ancestor_Part (N));
2715 end if;
2717 -- Handle positional associations
2719 if Present (Expressions (N)) then
2720 Comp_Expr := First (Expressions (N));
2721 while Present (Comp_Expr) loop
2722 if not Is_OK_Static_Expression (Comp_Expr) then
2723 Collect_Identifiers (Comp_Expr);
2724 end if;
2726 Next (Comp_Expr);
2727 end loop;
2728 end if;
2730 -- Handle discrete associations
2732 if Present (Component_Associations (N)) then
2733 Assoc := First (Component_Associations (N));
2734 while Present (Assoc) loop
2736 if not Box_Present (Assoc) then
2737 Choice := First (Choices (Assoc));
2738 while Present (Choice) loop
2740 -- For now we skip discriminants since it requires
2741 -- performing the analysis in two phases: first one
2742 -- analyzing discriminants and second one analyzing
2743 -- the rest of components since discriminants are
2744 -- evaluated prior to components: too much extra
2745 -- work to detect a corner case???
2747 if Nkind (Choice) in N_Has_Entity
2748 and then Present (Entity (Choice))
2749 and then Ekind (Entity (Choice)) = E_Discriminant
2750 then
2751 null;
2753 elsif Box_Present (Assoc) then
2754 null;
2756 else
2757 if not Analyzed (Expression (Assoc)) then
2758 Comp_Expr :=
2759 New_Copy_Tree (Expression (Assoc));
2760 Set_Parent (Comp_Expr, Parent (N));
2761 Preanalyze_Without_Errors (Comp_Expr);
2762 else
2763 Comp_Expr := Expression (Assoc);
2764 end if;
2766 Collect_Identifiers (Comp_Expr);
2767 end if;
2769 Next (Choice);
2770 end loop;
2771 end if;
2773 Next (Assoc);
2774 end loop;
2775 end if;
2776 end;
2778 when others =>
2779 return;
2780 end case;
2782 -- No further action needed if we already reported an error
2784 if Present (Error_Node) then
2785 return;
2786 end if;
2788 -- Check violation of RM 6.20/3 in aggregates
2790 if Present (Aggr_Error_Node)
2791 and then Writable_Actuals_List /= No_Elist
2792 then
2793 Error_Msg_N
2794 ("value may be affected by call in other component because they "
2795 & "are evaluated in unspecified order",
2796 Node (First_Elmt (Writable_Actuals_List)));
2797 return;
2798 end if;
2800 -- Check if some writable argument of a function is referenced
2802 if Writable_Actuals_List /= No_Elist
2803 and then Identifiers_List /= No_Elist
2804 then
2805 declare
2806 Elmt_1 : Elmt_Id;
2807 Elmt_2 : Elmt_Id;
2809 begin
2810 Elmt_1 := First_Elmt (Writable_Actuals_List);
2811 while Present (Elmt_1) loop
2812 Elmt_2 := First_Elmt (Identifiers_List);
2813 while Present (Elmt_2) loop
2814 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2815 case Nkind (Parent (Node (Elmt_2))) is
2816 when N_Aggregate |
2817 N_Component_Association |
2818 N_Component_Declaration =>
2819 Error_Msg_N
2820 ("value may be affected by call in other "
2821 & "component because they are evaluated "
2822 & "in unspecified order",
2823 Node (Elmt_2));
2825 when N_In | N_Not_In =>
2826 Error_Msg_N
2827 ("value may be affected by call in other "
2828 & "alternative because they are evaluated "
2829 & "in unspecified order",
2830 Node (Elmt_2));
2832 when others =>
2833 Error_Msg_N
2834 ("value of actual may be affected by call in "
2835 & "other actual because they are evaluated "
2836 & "in unspecified order",
2837 Node (Elmt_2));
2838 end case;
2839 end if;
2841 Next_Elmt (Elmt_2);
2842 end loop;
2844 Next_Elmt (Elmt_1);
2845 end loop;
2846 end;
2847 end if;
2848 end Check_Function_Writable_Actuals;
2850 --------------------------------
2851 -- Check_Implicit_Dereference --
2852 --------------------------------
2854 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2855 Disc : Entity_Id;
2856 Desig : Entity_Id;
2857 Nam : Node_Id;
2859 begin
2860 if Nkind (N) = N_Indexed_Component
2861 and then Present (Generalized_Indexing (N))
2862 then
2863 Nam := Generalized_Indexing (N);
2864 else
2865 Nam := N;
2866 end if;
2868 if Ada_Version < Ada_2012
2869 or else not Has_Implicit_Dereference (Base_Type (Typ))
2870 then
2871 return;
2873 elsif not Comes_From_Source (N)
2874 and then Nkind (N) /= N_Indexed_Component
2875 then
2876 return;
2878 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2879 null;
2881 else
2882 Disc := First_Discriminant (Typ);
2883 while Present (Disc) loop
2884 if Has_Implicit_Dereference (Disc) then
2885 Desig := Designated_Type (Etype (Disc));
2886 Add_One_Interp (Nam, Disc, Desig);
2888 -- If the node is a generalized indexing, add interpretation
2889 -- to that node as well, for subsequent resolution.
2891 if Nkind (N) = N_Indexed_Component then
2892 Add_One_Interp (N, Disc, Desig);
2893 end if;
2895 -- If the operation comes from a generic unit and the context
2896 -- is a selected component, the selector name may be global
2897 -- and set in the instance already. Remove the entity to
2898 -- force resolution of the selected component, and the
2899 -- generation of an explicit dereference if needed.
2901 if In_Instance
2902 and then Nkind (Parent (Nam)) = N_Selected_Component
2903 then
2904 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2905 end if;
2907 exit;
2908 end if;
2910 Next_Discriminant (Disc);
2911 end loop;
2912 end if;
2913 end Check_Implicit_Dereference;
2915 ----------------------------------
2916 -- Check_Internal_Protected_Use --
2917 ----------------------------------
2919 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2920 S : Entity_Id;
2921 Prot : Entity_Id;
2923 begin
2924 S := Current_Scope;
2925 while Present (S) loop
2926 if S = Standard_Standard then
2927 return;
2929 elsif Ekind (S) = E_Function
2930 and then Ekind (Scope (S)) = E_Protected_Type
2931 then
2932 Prot := Scope (S);
2933 exit;
2934 end if;
2936 S := Scope (S);
2937 end loop;
2939 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2941 -- An indirect function call (e.g. a callback within a protected
2942 -- function body) is not statically illegal. If the access type is
2943 -- anonymous and is the type of an access parameter, the scope of Nam
2944 -- will be the protected type, but it is not a protected operation.
2946 if Ekind (Nam) = E_Subprogram_Type
2947 and then
2948 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2949 then
2950 null;
2952 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2953 Error_Msg_N
2954 ("within protected function cannot use protected "
2955 & "procedure in renaming or as generic actual", N);
2957 elsif Nkind (N) = N_Attribute_Reference then
2958 Error_Msg_N
2959 ("within protected function cannot take access of "
2960 & " protected procedure", N);
2962 else
2963 Error_Msg_N
2964 ("within protected function, protected object is constant", N);
2965 Error_Msg_N
2966 ("\cannot call operation that may modify it", N);
2967 end if;
2968 end if;
2969 end Check_Internal_Protected_Use;
2971 ---------------------------------------
2972 -- Check_Later_Vs_Basic_Declarations --
2973 ---------------------------------------
2975 procedure Check_Later_Vs_Basic_Declarations
2976 (Decls : List_Id;
2977 During_Parsing : Boolean)
2979 Body_Sloc : Source_Ptr;
2980 Decl : Node_Id;
2982 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2983 -- Return whether Decl is considered as a declarative item.
2984 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2985 -- When During_Parsing is False, the semantics of SPARK is followed.
2987 -------------------------------
2988 -- Is_Later_Declarative_Item --
2989 -------------------------------
2991 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2992 begin
2993 if Nkind (Decl) in N_Later_Decl_Item then
2994 return True;
2996 elsif Nkind (Decl) = N_Pragma then
2997 return True;
2999 elsif During_Parsing then
3000 return False;
3002 -- In SPARK, a package declaration is not considered as a later
3003 -- declarative item.
3005 elsif Nkind (Decl) = N_Package_Declaration then
3006 return False;
3008 -- In SPARK, a renaming is considered as a later declarative item
3010 elsif Nkind (Decl) in N_Renaming_Declaration then
3011 return True;
3013 else
3014 return False;
3015 end if;
3016 end Is_Later_Declarative_Item;
3018 -- Start of Check_Later_Vs_Basic_Declarations
3020 begin
3021 Decl := First (Decls);
3023 -- Loop through sequence of basic declarative items
3025 Outer : while Present (Decl) loop
3026 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
3027 and then Nkind (Decl) not in N_Body_Stub
3028 then
3029 Next (Decl);
3031 -- Once a body is encountered, we only allow later declarative
3032 -- items. The inner loop checks the rest of the list.
3034 else
3035 Body_Sloc := Sloc (Decl);
3037 Inner : while Present (Decl) loop
3038 if not Is_Later_Declarative_Item (Decl) then
3039 if During_Parsing then
3040 if Ada_Version = Ada_83 then
3041 Error_Msg_Sloc := Body_Sloc;
3042 Error_Msg_N
3043 ("(Ada 83) decl cannot appear after body#", Decl);
3044 end if;
3045 else
3046 Error_Msg_Sloc := Body_Sloc;
3047 Check_SPARK_05_Restriction
3048 ("decl cannot appear after body#", Decl);
3049 end if;
3050 end if;
3052 Next (Decl);
3053 end loop Inner;
3054 end if;
3055 end loop Outer;
3056 end Check_Later_Vs_Basic_Declarations;
3058 -------------------------
3059 -- Check_Nested_Access --
3060 -------------------------
3062 procedure Check_Nested_Access (Ent : Entity_Id) is
3063 Scop : constant Entity_Id := Current_Scope;
3064 Current_Subp : Entity_Id;
3065 Enclosing : Entity_Id;
3067 begin
3068 -- Currently only enabled for VM back-ends for efficiency
3070 if VM_Target /= No_VM
3071 and then Ekind_In (Ent, E_Variable, E_Constant, E_Loop_Parameter)
3072 and then Scope (Ent) /= Empty
3073 and then not Is_Library_Level_Entity (Ent)
3075 -- Comment the exclusion of imported entities ???
3077 and then not Is_Imported (Ent)
3078 then
3079 -- Get current subprogram that is relevant
3081 if Is_Subprogram (Scop)
3082 or else Is_Generic_Subprogram (Scop)
3083 or else Is_Entry (Scop)
3084 then
3085 Current_Subp := Scop;
3086 else
3087 Current_Subp := Current_Subprogram;
3088 end if;
3090 Enclosing := Enclosing_Subprogram (Ent);
3092 -- Set flag if uplevel reference
3094 if Enclosing /= Empty and then Enclosing /= Current_Subp then
3095 Set_Has_Uplevel_Reference (Ent, True);
3096 end if;
3097 end if;
3098 end Check_Nested_Access;
3100 ---------------------------
3101 -- Check_No_Hidden_State --
3102 ---------------------------
3104 procedure Check_No_Hidden_State (Id : Entity_Id) is
3105 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
3106 -- Determine whether the entity of a package denoted by Pkg has a null
3107 -- abstract state.
3109 -----------------------------
3110 -- Has_Null_Abstract_State --
3111 -----------------------------
3113 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
3114 States : constant Elist_Id := Abstract_States (Pkg);
3116 begin
3117 -- Check first available state of related package. A null abstract
3118 -- state always appears as the sole element of the state list.
3120 return
3121 Present (States)
3122 and then Is_Null_State (Node (First_Elmt (States)));
3123 end Has_Null_Abstract_State;
3125 -- Local variables
3127 Context : Entity_Id := Empty;
3128 Not_Visible : Boolean := False;
3129 Scop : Entity_Id;
3131 -- Start of processing for Check_No_Hidden_State
3133 begin
3134 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3136 -- Find the proper context where the object or state appears
3138 Scop := Scope (Id);
3139 while Present (Scop) loop
3140 Context := Scop;
3142 -- Keep track of the context's visibility
3144 Not_Visible := Not_Visible or else In_Private_Part (Context);
3146 -- Prevent the search from going too far
3148 if Context = Standard_Standard then
3149 return;
3151 -- Objects and states that appear immediately within a subprogram or
3152 -- inside a construct nested within a subprogram do not introduce a
3153 -- hidden state. They behave as local variable declarations.
3155 elsif Is_Subprogram (Context) then
3156 return;
3158 -- When examining a package body, use the entity of the spec as it
3159 -- carries the abstract state declarations.
3161 elsif Ekind (Context) = E_Package_Body then
3162 Context := Spec_Entity (Context);
3163 end if;
3165 -- Stop the traversal when a package subject to a null abstract state
3166 -- has been found.
3168 if Ekind_In (Context, E_Generic_Package, E_Package)
3169 and then Has_Null_Abstract_State (Context)
3170 then
3171 exit;
3172 end if;
3174 Scop := Scope (Scop);
3175 end loop;
3177 -- At this point we know that there is at least one package with a null
3178 -- abstract state in visibility. Emit an error message unconditionally
3179 -- if the entity being processed is a state because the placement of the
3180 -- related package is irrelevant. This is not the case for objects as
3181 -- the intermediate context matters.
3183 if Present (Context)
3184 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3185 then
3186 Error_Msg_N ("cannot introduce hidden state &", Id);
3187 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3188 end if;
3189 end Check_No_Hidden_State;
3191 ------------------------------------------
3192 -- Check_Potentially_Blocking_Operation --
3193 ------------------------------------------
3195 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3196 S : Entity_Id;
3198 begin
3199 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3200 -- When pragma Detect_Blocking is active, the run time will raise
3201 -- Program_Error. Here we only issue a warning, since we generally
3202 -- support the use of potentially blocking operations in the absence
3203 -- of the pragma.
3205 -- Indirect blocking through a subprogram call cannot be diagnosed
3206 -- statically without interprocedural analysis, so we do not attempt
3207 -- to do it here.
3209 S := Scope (Current_Scope);
3210 while Present (S) and then S /= Standard_Standard loop
3211 if Is_Protected_Type (S) then
3212 Error_Msg_N
3213 ("potentially blocking operation in protected operation??", N);
3214 return;
3215 end if;
3217 S := Scope (S);
3218 end loop;
3219 end Check_Potentially_Blocking_Operation;
3221 ---------------------------------
3222 -- Check_Result_And_Post_State --
3223 ---------------------------------
3225 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3226 procedure Check_Result_And_Post_State_In_Pragma
3227 (Prag : Node_Id;
3228 Result_Seen : in out Boolean);
3229 -- Determine whether pragma Prag mentions attribute 'Result and whether
3230 -- the pragma contains an expression that evaluates differently in pre-
3231 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3232 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3234 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3235 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3236 -- formal parameter.
3238 -------------------------------------------
3239 -- Check_Result_And_Post_State_In_Pragma --
3240 -------------------------------------------
3242 procedure Check_Result_And_Post_State_In_Pragma
3243 (Prag : Node_Id;
3244 Result_Seen : in out Boolean)
3246 procedure Check_Expression (Expr : Node_Id);
3247 -- Perform the 'Result and post-state checks on a given expression
3249 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3250 -- Attempt to find attribute 'Result in a subtree denoted by N
3252 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3253 -- Determine whether source node N denotes "True" or "False"
3255 function Mentions_Post_State (N : Node_Id) return Boolean;
3256 -- Determine whether a subtree denoted by N mentions any construct
3257 -- that denotes a post-state.
3259 procedure Check_Function_Result is
3260 new Traverse_Proc (Is_Function_Result);
3262 ----------------------
3263 -- Check_Expression --
3264 ----------------------
3266 procedure Check_Expression (Expr : Node_Id) is
3267 begin
3268 if not Is_Trivial_Boolean (Expr) then
3269 Check_Function_Result (Expr);
3271 if not Mentions_Post_State (Expr) then
3272 if Pragma_Name (Prag) = Name_Contract_Cases then
3273 Error_Msg_NE
3274 ("contract case does not check the outcome of calling "
3275 & "&?T?", Expr, Subp_Id);
3277 elsif Pragma_Name (Prag) = Name_Refined_Post then
3278 Error_Msg_NE
3279 ("refined postcondition does not check the outcome of "
3280 & "calling &?T?", Prag, Subp_Id);
3282 else
3283 Error_Msg_NE
3284 ("postcondition does not check the outcome of calling "
3285 & "&?T?", Prag, Subp_Id);
3286 end if;
3287 end if;
3288 end if;
3289 end Check_Expression;
3291 ------------------------
3292 -- Is_Function_Result --
3293 ------------------------
3295 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3296 begin
3297 if Is_Attribute_Result (N) then
3298 Result_Seen := True;
3299 return Abandon;
3301 -- Continue the traversal
3303 else
3304 return OK;
3305 end if;
3306 end Is_Function_Result;
3308 ------------------------
3309 -- Is_Trivial_Boolean --
3310 ------------------------
3312 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3313 begin
3314 return
3315 Comes_From_Source (N)
3316 and then Is_Entity_Name (N)
3317 and then (Entity (N) = Standard_True
3318 or else
3319 Entity (N) = Standard_False);
3320 end Is_Trivial_Boolean;
3322 -------------------------
3323 -- Mentions_Post_State --
3324 -------------------------
3326 function Mentions_Post_State (N : Node_Id) return Boolean is
3327 Post_State_Seen : Boolean := False;
3329 function Is_Post_State (N : Node_Id) return Traverse_Result;
3330 -- Attempt to find a construct that denotes a post-state. If this
3331 -- is the case, set flag Post_State_Seen.
3333 -------------------
3334 -- Is_Post_State --
3335 -------------------
3337 function Is_Post_State (N : Node_Id) return Traverse_Result is
3338 Ent : Entity_Id;
3340 begin
3341 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3342 Post_State_Seen := True;
3343 return Abandon;
3345 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3346 Ent := Entity (N);
3348 -- The entity may be modifiable through an implicit
3349 -- dereference.
3351 if No (Ent)
3352 or else Ekind (Ent) in Assignable_Kind
3353 or else (Is_Access_Type (Etype (Ent))
3354 and then Nkind (Parent (N)) =
3355 N_Selected_Component)
3356 then
3357 Post_State_Seen := True;
3358 return Abandon;
3359 end if;
3361 elsif Nkind (N) = N_Attribute_Reference then
3362 if Attribute_Name (N) = Name_Old then
3363 return Skip;
3365 elsif Attribute_Name (N) = Name_Result then
3366 Post_State_Seen := True;
3367 return Abandon;
3368 end if;
3369 end if;
3371 return OK;
3372 end Is_Post_State;
3374 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3376 -- Start of processing for Mentions_Post_State
3378 begin
3379 Find_Post_State (N);
3381 return Post_State_Seen;
3382 end Mentions_Post_State;
3384 -- Local variables
3386 Expr : constant Node_Id :=
3387 Get_Pragma_Arg
3388 (First (Pragma_Argument_Associations (Prag)));
3389 Nam : constant Name_Id := Pragma_Name (Prag);
3390 CCase : Node_Id;
3392 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3394 begin
3395 -- Examine all consequences
3397 if Nam = Name_Contract_Cases then
3398 CCase := First (Component_Associations (Expr));
3399 while Present (CCase) loop
3400 Check_Expression (Expression (CCase));
3402 Next (CCase);
3403 end loop;
3405 -- Examine the expression of a postcondition
3407 else pragma Assert (Nam_In (Nam, Name_Postcondition,
3408 Name_Refined_Post));
3409 Check_Expression (Expr);
3410 end if;
3411 end Check_Result_And_Post_State_In_Pragma;
3413 --------------------------
3414 -- Has_In_Out_Parameter --
3415 --------------------------
3417 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
3418 Formal : Entity_Id;
3420 begin
3421 -- Traverse the formals looking for an IN OUT parameter
3423 Formal := First_Formal (Subp_Id);
3424 while Present (Formal) loop
3425 if Ekind (Formal) = E_In_Out_Parameter then
3426 return True;
3427 end if;
3429 Next_Formal (Formal);
3430 end loop;
3432 return False;
3433 end Has_In_Out_Parameter;
3435 -- Local variables
3437 Items : constant Node_Id := Contract (Subp_Id);
3438 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3439 Case_Prag : Node_Id := Empty;
3440 Post_Prag : Node_Id := Empty;
3441 Prag : Node_Id;
3442 Seen_In_Case : Boolean := False;
3443 Seen_In_Post : Boolean := False;
3444 Spec_Id : Entity_Id;
3446 -- Start of processing for Check_Result_And_Post_State
3448 begin
3449 -- The lack of attribute 'Result or a post-state is classified as a
3450 -- suspicious contract. Do not perform the check if the corresponding
3451 -- swich is not set.
3453 if not Warn_On_Suspicious_Contract then
3454 return;
3456 -- Nothing to do if there is no contract
3458 elsif No (Items) then
3459 return;
3460 end if;
3462 -- Retrieve the entity of the subprogram spec (if any)
3464 if Nkind (Subp_Decl) = N_Subprogram_Body
3465 and then Present (Corresponding_Spec (Subp_Decl))
3466 then
3467 Spec_Id := Corresponding_Spec (Subp_Decl);
3469 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
3470 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
3471 then
3472 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
3474 else
3475 Spec_Id := Subp_Id;
3476 end if;
3478 -- Examine all postconditions for attribute 'Result and a post-state
3480 Prag := Pre_Post_Conditions (Items);
3481 while Present (Prag) loop
3482 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
3483 Name_Refined_Post)
3484 and then not Error_Posted (Prag)
3485 then
3486 Post_Prag := Prag;
3487 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
3488 end if;
3490 Prag := Next_Pragma (Prag);
3491 end loop;
3493 -- Examine the contract cases of the subprogram for attribute 'Result
3494 -- and a post-state.
3496 Prag := Contract_Test_Cases (Items);
3497 while Present (Prag) loop
3498 if Pragma_Name (Prag) = Name_Contract_Cases
3499 and then not Error_Posted (Prag)
3500 then
3501 Case_Prag := Prag;
3502 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
3503 end if;
3505 Prag := Next_Pragma (Prag);
3506 end loop;
3508 -- Do not emit any errors if the subprogram is not a function
3510 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
3511 null;
3513 -- Regardless of whether the function has postconditions or contract
3514 -- cases, or whether they mention attribute 'Result, an IN OUT formal
3515 -- parameter is always treated as a result.
3517 elsif Has_In_Out_Parameter (Spec_Id) then
3518 null;
3520 -- The function has both a postcondition and contract cases and they do
3521 -- not mention attribute 'Result.
3523 elsif Present (Case_Prag)
3524 and then not Seen_In_Case
3525 and then Present (Post_Prag)
3526 and then not Seen_In_Post
3527 then
3528 Error_Msg_N
3529 ("neither postcondition nor contract cases mention function "
3530 & "result?T?", Post_Prag);
3532 -- The function has contract cases only and they do not mention
3533 -- attribute 'Result.
3535 elsif Present (Case_Prag) and then not Seen_In_Case then
3536 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
3538 -- The function has postconditions only and they do not mention
3539 -- attribute 'Result.
3541 elsif Present (Post_Prag) and then not Seen_In_Post then
3542 Error_Msg_N
3543 ("postcondition does not mention function result?T?", Post_Prag);
3544 end if;
3545 end Check_Result_And_Post_State;
3547 ------------------------------
3548 -- Check_Unprotected_Access --
3549 ------------------------------
3551 procedure Check_Unprotected_Access
3552 (Context : Node_Id;
3553 Expr : Node_Id)
3555 Cont_Encl_Typ : Entity_Id;
3556 Pref_Encl_Typ : Entity_Id;
3558 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3559 -- Check whether Obj is a private component of a protected object.
3560 -- Return the protected type where the component resides, Empty
3561 -- otherwise.
3563 function Is_Public_Operation return Boolean;
3564 -- Verify that the enclosing operation is callable from outside the
3565 -- protected object, to minimize false positives.
3567 ------------------------------
3568 -- Enclosing_Protected_Type --
3569 ------------------------------
3571 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3572 begin
3573 if Is_Entity_Name (Obj) then
3574 declare
3575 Ent : Entity_Id := Entity (Obj);
3577 begin
3578 -- The object can be a renaming of a private component, use
3579 -- the original record component.
3581 if Is_Prival (Ent) then
3582 Ent := Prival_Link (Ent);
3583 end if;
3585 if Is_Protected_Type (Scope (Ent)) then
3586 return Scope (Ent);
3587 end if;
3588 end;
3589 end if;
3591 -- For indexed and selected components, recursively check the prefix
3593 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3594 return Enclosing_Protected_Type (Prefix (Obj));
3596 -- The object does not denote a protected component
3598 else
3599 return Empty;
3600 end if;
3601 end Enclosing_Protected_Type;
3603 -------------------------
3604 -- Is_Public_Operation --
3605 -------------------------
3607 function Is_Public_Operation return Boolean is
3608 S : Entity_Id;
3609 E : Entity_Id;
3611 begin
3612 S := Current_Scope;
3613 while Present (S) and then S /= Pref_Encl_Typ loop
3614 if Scope (S) = Pref_Encl_Typ then
3615 E := First_Entity (Pref_Encl_Typ);
3616 while Present (E)
3617 and then E /= First_Private_Entity (Pref_Encl_Typ)
3618 loop
3619 if E = S then
3620 return True;
3621 end if;
3623 Next_Entity (E);
3624 end loop;
3625 end if;
3627 S := Scope (S);
3628 end loop;
3630 return False;
3631 end Is_Public_Operation;
3633 -- Start of processing for Check_Unprotected_Access
3635 begin
3636 if Nkind (Expr) = N_Attribute_Reference
3637 and then Attribute_Name (Expr) = Name_Unchecked_Access
3638 then
3639 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3640 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3642 -- Check whether we are trying to export a protected component to a
3643 -- context with an equal or lower access level.
3645 if Present (Pref_Encl_Typ)
3646 and then No (Cont_Encl_Typ)
3647 and then Is_Public_Operation
3648 and then Scope_Depth (Pref_Encl_Typ) >=
3649 Object_Access_Level (Context)
3650 then
3651 Error_Msg_N
3652 ("??possible unprotected access to protected data", Expr);
3653 end if;
3654 end if;
3655 end Check_Unprotected_Access;
3657 ------------------------
3658 -- Collect_Interfaces --
3659 ------------------------
3661 procedure Collect_Interfaces
3662 (T : Entity_Id;
3663 Ifaces_List : out Elist_Id;
3664 Exclude_Parents : Boolean := False;
3665 Use_Full_View : Boolean := True)
3667 procedure Collect (Typ : Entity_Id);
3668 -- Subsidiary subprogram used to traverse the whole list
3669 -- of directly and indirectly implemented interfaces
3671 -------------
3672 -- Collect --
3673 -------------
3675 procedure Collect (Typ : Entity_Id) is
3676 Ancestor : Entity_Id;
3677 Full_T : Entity_Id;
3678 Id : Node_Id;
3679 Iface : Entity_Id;
3681 begin
3682 Full_T := Typ;
3684 -- Handle private types and subtypes
3686 if Use_Full_View
3687 and then Is_Private_Type (Typ)
3688 and then Present (Full_View (Typ))
3689 then
3690 Full_T := Full_View (Typ);
3692 if Ekind (Full_T) = E_Record_Subtype then
3693 Full_T := Full_View (Etype (Typ));
3694 end if;
3695 end if;
3697 -- Include the ancestor if we are generating the whole list of
3698 -- abstract interfaces.
3700 if Etype (Full_T) /= Typ
3702 -- Protect the frontend against wrong sources. For example:
3704 -- package P is
3705 -- type A is tagged null record;
3706 -- type B is new A with private;
3707 -- type C is new A with private;
3708 -- private
3709 -- type B is new C with null record;
3710 -- type C is new B with null record;
3711 -- end P;
3713 and then Etype (Full_T) /= T
3714 then
3715 Ancestor := Etype (Full_T);
3716 Collect (Ancestor);
3718 if Is_Interface (Ancestor) and then not Exclude_Parents then
3719 Append_Unique_Elmt (Ancestor, Ifaces_List);
3720 end if;
3721 end if;
3723 -- Traverse the graph of ancestor interfaces
3725 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
3726 Id := First (Abstract_Interface_List (Full_T));
3727 while Present (Id) loop
3728 Iface := Etype (Id);
3730 -- Protect against wrong uses. For example:
3731 -- type I is interface;
3732 -- type O is tagged null record;
3733 -- type Wrong is new I and O with null record; -- ERROR
3735 if Is_Interface (Iface) then
3736 if Exclude_Parents
3737 and then Etype (T) /= T
3738 and then Interface_Present_In_Ancestor (Etype (T), Iface)
3739 then
3740 null;
3741 else
3742 Collect (Iface);
3743 Append_Unique_Elmt (Iface, Ifaces_List);
3744 end if;
3745 end if;
3747 Next (Id);
3748 end loop;
3749 end if;
3750 end Collect;
3752 -- Start of processing for Collect_Interfaces
3754 begin
3755 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
3756 Ifaces_List := New_Elmt_List;
3757 Collect (T);
3758 end Collect_Interfaces;
3760 ----------------------------------
3761 -- Collect_Interface_Components --
3762 ----------------------------------
3764 procedure Collect_Interface_Components
3765 (Tagged_Type : Entity_Id;
3766 Components_List : out Elist_Id)
3768 procedure Collect (Typ : Entity_Id);
3769 -- Subsidiary subprogram used to climb to the parents
3771 -------------
3772 -- Collect --
3773 -------------
3775 procedure Collect (Typ : Entity_Id) is
3776 Tag_Comp : Entity_Id;
3777 Parent_Typ : Entity_Id;
3779 begin
3780 -- Handle private types
3782 if Present (Full_View (Etype (Typ))) then
3783 Parent_Typ := Full_View (Etype (Typ));
3784 else
3785 Parent_Typ := Etype (Typ);
3786 end if;
3788 if Parent_Typ /= Typ
3790 -- Protect the frontend against wrong sources. For example:
3792 -- package P is
3793 -- type A is tagged null record;
3794 -- type B is new A with private;
3795 -- type C is new A with private;
3796 -- private
3797 -- type B is new C with null record;
3798 -- type C is new B with null record;
3799 -- end P;
3801 and then Parent_Typ /= Tagged_Type
3802 then
3803 Collect (Parent_Typ);
3804 end if;
3806 -- Collect the components containing tags of secondary dispatch
3807 -- tables.
3809 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
3810 while Present (Tag_Comp) loop
3811 pragma Assert (Present (Related_Type (Tag_Comp)));
3812 Append_Elmt (Tag_Comp, Components_List);
3814 Tag_Comp := Next_Tag_Component (Tag_Comp);
3815 end loop;
3816 end Collect;
3818 -- Start of processing for Collect_Interface_Components
3820 begin
3821 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
3822 and then Is_Tagged_Type (Tagged_Type));
3824 Components_List := New_Elmt_List;
3825 Collect (Tagged_Type);
3826 end Collect_Interface_Components;
3828 -----------------------------
3829 -- Collect_Interfaces_Info --
3830 -----------------------------
3832 procedure Collect_Interfaces_Info
3833 (T : Entity_Id;
3834 Ifaces_List : out Elist_Id;
3835 Components_List : out Elist_Id;
3836 Tags_List : out Elist_Id)
3838 Comps_List : Elist_Id;
3839 Comp_Elmt : Elmt_Id;
3840 Comp_Iface : Entity_Id;
3841 Iface_Elmt : Elmt_Id;
3842 Iface : Entity_Id;
3844 function Search_Tag (Iface : Entity_Id) return Entity_Id;
3845 -- Search for the secondary tag associated with the interface type
3846 -- Iface that is implemented by T.
3848 ----------------
3849 -- Search_Tag --
3850 ----------------
3852 function Search_Tag (Iface : Entity_Id) return Entity_Id is
3853 ADT : Elmt_Id;
3854 begin
3855 if not Is_CPP_Class (T) then
3856 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
3857 else
3858 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
3859 end if;
3861 while Present (ADT)
3862 and then Is_Tag (Node (ADT))
3863 and then Related_Type (Node (ADT)) /= Iface
3864 loop
3865 -- Skip secondary dispatch table referencing thunks to user
3866 -- defined primitives covered by this interface.
3868 pragma Assert (Has_Suffix (Node (ADT), 'P'));
3869 Next_Elmt (ADT);
3871 -- Skip secondary dispatch tables of Ada types
3873 if not Is_CPP_Class (T) then
3875 -- Skip secondary dispatch table referencing thunks to
3876 -- predefined primitives.
3878 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
3879 Next_Elmt (ADT);
3881 -- Skip secondary dispatch table referencing user-defined
3882 -- primitives covered by this interface.
3884 pragma Assert (Has_Suffix (Node (ADT), 'D'));
3885 Next_Elmt (ADT);
3887 -- Skip secondary dispatch table referencing predefined
3888 -- primitives.
3890 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
3891 Next_Elmt (ADT);
3892 end if;
3893 end loop;
3895 pragma Assert (Is_Tag (Node (ADT)));
3896 return Node (ADT);
3897 end Search_Tag;
3899 -- Start of processing for Collect_Interfaces_Info
3901 begin
3902 Collect_Interfaces (T, Ifaces_List);
3903 Collect_Interface_Components (T, Comps_List);
3905 -- Search for the record component and tag associated with each
3906 -- interface type of T.
3908 Components_List := New_Elmt_List;
3909 Tags_List := New_Elmt_List;
3911 Iface_Elmt := First_Elmt (Ifaces_List);
3912 while Present (Iface_Elmt) loop
3913 Iface := Node (Iface_Elmt);
3915 -- Associate the primary tag component and the primary dispatch table
3916 -- with all the interfaces that are parents of T
3918 if Is_Ancestor (Iface, T, Use_Full_View => True) then
3919 Append_Elmt (First_Tag_Component (T), Components_List);
3920 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
3922 -- Otherwise search for the tag component and secondary dispatch
3923 -- table of Iface
3925 else
3926 Comp_Elmt := First_Elmt (Comps_List);
3927 while Present (Comp_Elmt) loop
3928 Comp_Iface := Related_Type (Node (Comp_Elmt));
3930 if Comp_Iface = Iface
3931 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
3932 then
3933 Append_Elmt (Node (Comp_Elmt), Components_List);
3934 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
3935 exit;
3936 end if;
3938 Next_Elmt (Comp_Elmt);
3939 end loop;
3940 pragma Assert (Present (Comp_Elmt));
3941 end if;
3943 Next_Elmt (Iface_Elmt);
3944 end loop;
3945 end Collect_Interfaces_Info;
3947 ---------------------
3948 -- Collect_Parents --
3949 ---------------------
3951 procedure Collect_Parents
3952 (T : Entity_Id;
3953 List : out Elist_Id;
3954 Use_Full_View : Boolean := True)
3956 Current_Typ : Entity_Id := T;
3957 Parent_Typ : Entity_Id;
3959 begin
3960 List := New_Elmt_List;
3962 -- No action if the if the type has no parents
3964 if T = Etype (T) then
3965 return;
3966 end if;
3968 loop
3969 Parent_Typ := Etype (Current_Typ);
3971 if Is_Private_Type (Parent_Typ)
3972 and then Present (Full_View (Parent_Typ))
3973 and then Use_Full_View
3974 then
3975 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3976 end if;
3978 Append_Elmt (Parent_Typ, List);
3980 exit when Parent_Typ = Current_Typ;
3981 Current_Typ := Parent_Typ;
3982 end loop;
3983 end Collect_Parents;
3985 ----------------------------------
3986 -- Collect_Primitive_Operations --
3987 ----------------------------------
3989 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
3990 B_Type : constant Entity_Id := Base_Type (T);
3991 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
3992 B_Scope : Entity_Id := Scope (B_Type);
3993 Op_List : Elist_Id;
3994 Formal : Entity_Id;
3995 Is_Prim : Boolean;
3996 Is_Type_In_Pkg : Boolean;
3997 Formal_Derived : Boolean := False;
3998 Id : Entity_Id;
4000 function Match (E : Entity_Id) return Boolean;
4001 -- True if E's base type is B_Type, or E is of an anonymous access type
4002 -- and the base type of its designated type is B_Type.
4004 -----------
4005 -- Match --
4006 -----------
4008 function Match (E : Entity_Id) return Boolean is
4009 Etyp : Entity_Id := Etype (E);
4011 begin
4012 if Ekind (Etyp) = E_Anonymous_Access_Type then
4013 Etyp := Designated_Type (Etyp);
4014 end if;
4016 -- In Ada 2012 a primitive operation may have a formal of an
4017 -- incomplete view of the parent type.
4019 return Base_Type (Etyp) = B_Type
4020 or else
4021 (Ada_Version >= Ada_2012
4022 and then Ekind (Etyp) = E_Incomplete_Type
4023 and then Full_View (Etyp) = B_Type);
4024 end Match;
4026 -- Start of processing for Collect_Primitive_Operations
4028 begin
4029 -- For tagged types, the primitive operations are collected as they
4030 -- are declared, and held in an explicit list which is simply returned.
4032 if Is_Tagged_Type (B_Type) then
4033 return Primitive_Operations (B_Type);
4035 -- An untagged generic type that is a derived type inherits the
4036 -- primitive operations of its parent type. Other formal types only
4037 -- have predefined operators, which are not explicitly represented.
4039 elsif Is_Generic_Type (B_Type) then
4040 if Nkind (B_Decl) = N_Formal_Type_Declaration
4041 and then Nkind (Formal_Type_Definition (B_Decl)) =
4042 N_Formal_Derived_Type_Definition
4043 then
4044 Formal_Derived := True;
4045 else
4046 return New_Elmt_List;
4047 end if;
4048 end if;
4050 Op_List := New_Elmt_List;
4052 if B_Scope = Standard_Standard then
4053 if B_Type = Standard_String then
4054 Append_Elmt (Standard_Op_Concat, Op_List);
4056 elsif B_Type = Standard_Wide_String then
4057 Append_Elmt (Standard_Op_Concatw, Op_List);
4059 else
4060 null;
4061 end if;
4063 -- Locate the primitive subprograms of the type
4065 else
4066 -- The primitive operations appear after the base type, except
4067 -- if the derivation happens within the private part of B_Scope
4068 -- and the type is a private type, in which case both the type
4069 -- and some primitive operations may appear before the base
4070 -- type, and the list of candidates starts after the type.
4072 if In_Open_Scopes (B_Scope)
4073 and then Scope (T) = B_Scope
4074 and then In_Private_Part (B_Scope)
4075 then
4076 Id := Next_Entity (T);
4078 -- In Ada 2012, If the type has an incomplete partial view, there
4079 -- may be primitive operations declared before the full view, so
4080 -- we need to start scanning from the incomplete view, which is
4081 -- earlier on the entity chain.
4083 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
4084 and then Present (Incomplete_View (Parent (B_Type)))
4085 then
4086 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
4088 else
4089 Id := Next_Entity (B_Type);
4090 end if;
4092 -- Set flag if this is a type in a package spec
4094 Is_Type_In_Pkg :=
4095 Is_Package_Or_Generic_Package (B_Scope)
4096 and then
4097 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
4098 N_Package_Body;
4100 while Present (Id) loop
4102 -- Test whether the result type or any of the parameter types of
4103 -- each subprogram following the type match that type when the
4104 -- type is declared in a package spec, is a derived type, or the
4105 -- subprogram is marked as primitive. (The Is_Primitive test is
4106 -- needed to find primitives of nonderived types in declarative
4107 -- parts that happen to override the predefined "=" operator.)
4109 -- Note that generic formal subprograms are not considered to be
4110 -- primitive operations and thus are never inherited.
4112 if Is_Overloadable (Id)
4113 and then (Is_Type_In_Pkg
4114 or else Is_Derived_Type (B_Type)
4115 or else Is_Primitive (Id))
4116 and then Nkind (Parent (Parent (Id)))
4117 not in N_Formal_Subprogram_Declaration
4118 then
4119 Is_Prim := False;
4121 if Match (Id) then
4122 Is_Prim := True;
4124 else
4125 Formal := First_Formal (Id);
4126 while Present (Formal) loop
4127 if Match (Formal) then
4128 Is_Prim := True;
4129 exit;
4130 end if;
4132 Next_Formal (Formal);
4133 end loop;
4134 end if;
4136 -- For a formal derived type, the only primitives are the ones
4137 -- inherited from the parent type. Operations appearing in the
4138 -- package declaration are not primitive for it.
4140 if Is_Prim
4141 and then (not Formal_Derived or else Present (Alias (Id)))
4142 then
4143 -- In the special case of an equality operator aliased to
4144 -- an overriding dispatching equality belonging to the same
4145 -- type, we don't include it in the list of primitives.
4146 -- This avoids inheriting multiple equality operators when
4147 -- deriving from untagged private types whose full type is
4148 -- tagged, which can otherwise cause ambiguities. Note that
4149 -- this should only happen for this kind of untagged parent
4150 -- type, since normally dispatching operations are inherited
4151 -- using the type's Primitive_Operations list.
4153 if Chars (Id) = Name_Op_Eq
4154 and then Is_Dispatching_Operation (Id)
4155 and then Present (Alias (Id))
4156 and then Present (Overridden_Operation (Alias (Id)))
4157 and then Base_Type (Etype (First_Entity (Id))) =
4158 Base_Type (Etype (First_Entity (Alias (Id))))
4159 then
4160 null;
4162 -- Include the subprogram in the list of primitives
4164 else
4165 Append_Elmt (Id, Op_List);
4166 end if;
4167 end if;
4168 end if;
4170 Next_Entity (Id);
4172 -- For a type declared in System, some of its operations may
4173 -- appear in the target-specific extension to System.
4175 if No (Id)
4176 and then B_Scope = RTU_Entity (System)
4177 and then Present_System_Aux
4178 then
4179 B_Scope := System_Aux_Id;
4180 Id := First_Entity (System_Aux_Id);
4181 end if;
4182 end loop;
4183 end if;
4185 return Op_List;
4186 end Collect_Primitive_Operations;
4188 -----------------------------------
4189 -- Compile_Time_Constraint_Error --
4190 -----------------------------------
4192 function Compile_Time_Constraint_Error
4193 (N : Node_Id;
4194 Msg : String;
4195 Ent : Entity_Id := Empty;
4196 Loc : Source_Ptr := No_Location;
4197 Warn : Boolean := False) return Node_Id
4199 Msgc : String (1 .. Msg'Length + 3);
4200 -- Copy of message, with room for possible ?? or << and ! at end
4202 Msgl : Natural;
4203 Wmsg : Boolean;
4204 Eloc : Source_Ptr;
4206 -- Start of processing for Compile_Time_Constraint_Error
4208 begin
4209 -- If this is a warning, convert it into an error if we are in code
4210 -- subject to SPARK_Mode being set ON.
4212 Error_Msg_Warn := SPARK_Mode /= On;
4214 -- A static constraint error in an instance body is not a fatal error.
4215 -- we choose to inhibit the message altogether, because there is no
4216 -- obvious node (for now) on which to post it. On the other hand the
4217 -- offending node must be replaced with a constraint_error in any case.
4219 -- No messages are generated if we already posted an error on this node
4221 if not Error_Posted (N) then
4222 if Loc /= No_Location then
4223 Eloc := Loc;
4224 else
4225 Eloc := Sloc (N);
4226 end if;
4228 -- Copy message to Msgc, converting any ? in the message into
4229 -- < instead, so that we have an error in GNATprove mode.
4231 Msgl := Msg'Length;
4233 for J in 1 .. Msgl loop
4234 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
4235 Msgc (J) := '<';
4236 else
4237 Msgc (J) := Msg (J);
4238 end if;
4239 end loop;
4241 -- Message is a warning, even in Ada 95 case
4243 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
4244 Wmsg := True;
4246 -- In Ada 83, all messages are warnings. In the private part and
4247 -- the body of an instance, constraint_checks are only warnings.
4248 -- We also make this a warning if the Warn parameter is set.
4250 elsif Warn
4251 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
4252 then
4253 Msgl := Msgl + 1;
4254 Msgc (Msgl) := '<';
4255 Msgl := Msgl + 1;
4256 Msgc (Msgl) := '<';
4257 Wmsg := True;
4259 elsif In_Instance_Not_Visible then
4260 Msgl := Msgl + 1;
4261 Msgc (Msgl) := '<';
4262 Msgl := Msgl + 1;
4263 Msgc (Msgl) := '<';
4264 Wmsg := True;
4266 -- Otherwise we have a real error message (Ada 95 static case)
4267 -- and we make this an unconditional message. Note that in the
4268 -- warning case we do not make the message unconditional, it seems
4269 -- quite reasonable to delete messages like this (about exceptions
4270 -- that will be raised) in dead code.
4272 else
4273 Wmsg := False;
4274 Msgl := Msgl + 1;
4275 Msgc (Msgl) := '!';
4276 end if;
4278 -- One more test, skip the warning if the related expression is
4279 -- statically unevaluated, since we don't want to warn about what
4280 -- will happen when something is evaluated if it never will be
4281 -- evaluated.
4283 if not Is_Statically_Unevaluated (N) then
4284 Error_Msg_Warn := SPARK_Mode /= On;
4286 if Present (Ent) then
4287 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4288 else
4289 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4290 end if;
4292 if Wmsg then
4294 -- Check whether the context is an Init_Proc
4296 if Inside_Init_Proc then
4297 declare
4298 Conc_Typ : constant Entity_Id :=
4299 Corresponding_Concurrent_Type
4300 (Entity (Parameter_Type (First
4301 (Parameter_Specifications
4302 (Parent (Current_Scope))))));
4304 begin
4305 -- Don't complain if the corresponding concurrent type
4306 -- doesn't come from source (i.e. a single task/protected
4307 -- object).
4309 if Present (Conc_Typ)
4310 and then not Comes_From_Source (Conc_Typ)
4311 then
4312 Error_Msg_NEL
4313 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4315 else
4316 if GNATprove_Mode then
4317 Error_Msg_NEL
4318 ("\& would have been raised for objects of this "
4319 & "type", N, Standard_Constraint_Error, Eloc);
4320 else
4321 Error_Msg_NEL
4322 ("\& will be raised for objects of this type??",
4323 N, Standard_Constraint_Error, Eloc);
4324 end if;
4325 end if;
4326 end;
4328 else
4329 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4330 end if;
4332 else
4333 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4334 Set_Error_Posted (N);
4335 end if;
4336 end if;
4337 end if;
4339 return N;
4340 end Compile_Time_Constraint_Error;
4342 -----------------------
4343 -- Conditional_Delay --
4344 -----------------------
4346 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4347 begin
4348 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4349 Set_Has_Delayed_Freeze (New_Ent);
4350 end if;
4351 end Conditional_Delay;
4353 ----------------------------
4354 -- Contains_Refined_State --
4355 ----------------------------
4357 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4358 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4359 -- Determine whether a dependency list mentions a state with a visible
4360 -- refinement.
4362 function Has_State_In_Global (List : Node_Id) return Boolean;
4363 -- Determine whether a global list mentions a state with a visible
4364 -- refinement.
4366 function Is_Refined_State (Item : Node_Id) return Boolean;
4367 -- Determine whether Item is a reference to an abstract state with a
4368 -- visible refinement.
4370 -----------------------------
4371 -- Has_State_In_Dependency --
4372 -----------------------------
4374 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4375 Clause : Node_Id;
4376 Output : Node_Id;
4378 begin
4379 -- A null dependency list does not mention any states
4381 if Nkind (List) = N_Null then
4382 return False;
4384 -- Dependency clauses appear as component associations of an
4385 -- aggregate.
4387 elsif Nkind (List) = N_Aggregate
4388 and then Present (Component_Associations (List))
4389 then
4390 Clause := First (Component_Associations (List));
4391 while Present (Clause) loop
4393 -- Inspect the outputs of a dependency clause
4395 Output := First (Choices (Clause));
4396 while Present (Output) loop
4397 if Is_Refined_State (Output) then
4398 return True;
4399 end if;
4401 Next (Output);
4402 end loop;
4404 -- Inspect the outputs of a dependency clause
4406 if Is_Refined_State (Expression (Clause)) then
4407 return True;
4408 end if;
4410 Next (Clause);
4411 end loop;
4413 -- If we get here, then none of the dependency clauses mention a
4414 -- state with visible refinement.
4416 return False;
4418 -- An illegal pragma managed to sneak in
4420 else
4421 raise Program_Error;
4422 end if;
4423 end Has_State_In_Dependency;
4425 -------------------------
4426 -- Has_State_In_Global --
4427 -------------------------
4429 function Has_State_In_Global (List : Node_Id) return Boolean is
4430 Item : Node_Id;
4432 begin
4433 -- A null global list does not mention any states
4435 if Nkind (List) = N_Null then
4436 return False;
4438 -- Simple global list or moded global list declaration
4440 elsif Nkind (List) = N_Aggregate then
4442 -- The declaration of a simple global list appear as a collection
4443 -- of expressions.
4445 if Present (Expressions (List)) then
4446 Item := First (Expressions (List));
4447 while Present (Item) loop
4448 if Is_Refined_State (Item) then
4449 return True;
4450 end if;
4452 Next (Item);
4453 end loop;
4455 -- The declaration of a moded global list appears as a collection
4456 -- of component associations where individual choices denote
4457 -- modes.
4459 else
4460 Item := First (Component_Associations (List));
4461 while Present (Item) loop
4462 if Has_State_In_Global (Expression (Item)) then
4463 return True;
4464 end if;
4466 Next (Item);
4467 end loop;
4468 end if;
4470 -- If we get here, then the simple/moded global list did not
4471 -- mention any states with a visible refinement.
4473 return False;
4475 -- Single global item declaration
4477 elsif Is_Entity_Name (List) then
4478 return Is_Refined_State (List);
4480 -- An illegal pragma managed to sneak in
4482 else
4483 raise Program_Error;
4484 end if;
4485 end Has_State_In_Global;
4487 ----------------------
4488 -- Is_Refined_State --
4489 ----------------------
4491 function Is_Refined_State (Item : Node_Id) return Boolean is
4492 Elmt : Node_Id;
4493 Item_Id : Entity_Id;
4495 begin
4496 if Nkind (Item) = N_Null then
4497 return False;
4499 -- States cannot be subject to attribute 'Result. This case arises
4500 -- in dependency relations.
4502 elsif Nkind (Item) = N_Attribute_Reference
4503 and then Attribute_Name (Item) = Name_Result
4504 then
4505 return False;
4507 -- Multiple items appear as an aggregate. This case arises in
4508 -- dependency relations.
4510 elsif Nkind (Item) = N_Aggregate
4511 and then Present (Expressions (Item))
4512 then
4513 Elmt := First (Expressions (Item));
4514 while Present (Elmt) loop
4515 if Is_Refined_State (Elmt) then
4516 return True;
4517 end if;
4519 Next (Elmt);
4520 end loop;
4522 -- If we get here, then none of the inputs or outputs reference a
4523 -- state with visible refinement.
4525 return False;
4527 -- Single item
4529 else
4530 Item_Id := Entity_Of (Item);
4532 return
4533 Present (Item_Id)
4534 and then Ekind (Item_Id) = E_Abstract_State
4535 and then Has_Visible_Refinement (Item_Id);
4536 end if;
4537 end Is_Refined_State;
4539 -- Local variables
4541 Arg : constant Node_Id :=
4542 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4543 Nam : constant Name_Id := Pragma_Name (Prag);
4545 -- Start of processing for Contains_Refined_State
4547 begin
4548 if Nam = Name_Depends then
4549 return Has_State_In_Dependency (Arg);
4551 else pragma Assert (Nam = Name_Global);
4552 return Has_State_In_Global (Arg);
4553 end if;
4554 end Contains_Refined_State;
4556 -------------------------
4557 -- Copy_Component_List --
4558 -------------------------
4560 function Copy_Component_List
4561 (R_Typ : Entity_Id;
4562 Loc : Source_Ptr) return List_Id
4564 Comp : Node_Id;
4565 Comps : constant List_Id := New_List;
4567 begin
4568 Comp := First_Component (Underlying_Type (R_Typ));
4569 while Present (Comp) loop
4570 if Comes_From_Source (Comp) then
4571 declare
4572 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4573 begin
4574 Append_To (Comps,
4575 Make_Component_Declaration (Loc,
4576 Defining_Identifier =>
4577 Make_Defining_Identifier (Loc, Chars (Comp)),
4578 Component_Definition =>
4579 New_Copy_Tree
4580 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4581 end;
4582 end if;
4584 Next_Component (Comp);
4585 end loop;
4587 return Comps;
4588 end Copy_Component_List;
4590 -------------------------
4591 -- Copy_Parameter_List --
4592 -------------------------
4594 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4595 Loc : constant Source_Ptr := Sloc (Subp_Id);
4596 Plist : List_Id;
4597 Formal : Entity_Id;
4599 begin
4600 if No (First_Formal (Subp_Id)) then
4601 return No_List;
4602 else
4603 Plist := New_List;
4604 Formal := First_Formal (Subp_Id);
4605 while Present (Formal) loop
4606 Append_To (Plist,
4607 Make_Parameter_Specification (Loc,
4608 Defining_Identifier =>
4609 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4610 In_Present => In_Present (Parent (Formal)),
4611 Out_Present => Out_Present (Parent (Formal)),
4612 Parameter_Type =>
4613 New_Occurrence_Of (Etype (Formal), Loc),
4614 Expression =>
4615 New_Copy_Tree (Expression (Parent (Formal)))));
4617 Next_Formal (Formal);
4618 end loop;
4619 end if;
4621 return Plist;
4622 end Copy_Parameter_List;
4624 --------------------------
4625 -- Copy_Subprogram_Spec --
4626 --------------------------
4628 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
4629 Def_Id : Node_Id;
4630 Formal_Spec : Node_Id;
4631 Result : Node_Id;
4633 begin
4634 -- The structure of the original tree must be replicated without any
4635 -- alterations. Use New_Copy_Tree for this purpose.
4637 Result := New_Copy_Tree (Spec);
4639 -- Create a new entity for the defining unit name
4641 Def_Id := Defining_Unit_Name (Result);
4642 Set_Defining_Unit_Name (Result,
4643 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
4645 -- Create new entities for the formal parameters
4647 if Present (Parameter_Specifications (Result)) then
4648 Formal_Spec := First (Parameter_Specifications (Result));
4649 while Present (Formal_Spec) loop
4650 Def_Id := Defining_Identifier (Formal_Spec);
4651 Set_Defining_Identifier (Formal_Spec,
4652 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
4654 Next (Formal_Spec);
4655 end loop;
4656 end if;
4658 return Result;
4659 end Copy_Subprogram_Spec;
4661 --------------------------------
4662 -- Corresponding_Generic_Type --
4663 --------------------------------
4665 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
4666 Inst : Entity_Id;
4667 Gen : Entity_Id;
4668 Typ : Entity_Id;
4670 begin
4671 if not Is_Generic_Actual_Type (T) then
4672 return Any_Type;
4674 -- If the actual is the actual of an enclosing instance, resolution
4675 -- was correct in the generic.
4677 elsif Nkind (Parent (T)) = N_Subtype_Declaration
4678 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
4679 and then
4680 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
4681 then
4682 return Any_Type;
4684 else
4685 Inst := Scope (T);
4687 if Is_Wrapper_Package (Inst) then
4688 Inst := Related_Instance (Inst);
4689 end if;
4691 Gen :=
4692 Generic_Parent
4693 (Specification (Unit_Declaration_Node (Inst)));
4695 -- Generic actual has the same name as the corresponding formal
4697 Typ := First_Entity (Gen);
4698 while Present (Typ) loop
4699 if Chars (Typ) = Chars (T) then
4700 return Typ;
4701 end if;
4703 Next_Entity (Typ);
4704 end loop;
4706 return Any_Type;
4707 end if;
4708 end Corresponding_Generic_Type;
4710 ---------------------------
4711 -- Corresponding_Spec_Of --
4712 ---------------------------
4714 function Corresponding_Spec_Of (Decl : Node_Id) return Entity_Id is
4715 begin
4716 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
4717 and then Present (Corresponding_Spec (Decl))
4718 then
4719 return Corresponding_Spec (Decl);
4721 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
4722 and then Present (Corresponding_Spec_Of_Stub (Decl))
4723 then
4724 return Corresponding_Spec_Of_Stub (Decl);
4726 else
4727 return Defining_Entity (Decl);
4728 end if;
4729 end Corresponding_Spec_Of;
4731 -----------------------------
4732 -- Create_Generic_Contract --
4733 -----------------------------
4735 procedure Create_Generic_Contract (Unit : Node_Id) is
4736 Templ : constant Node_Id := Original_Node (Unit);
4737 Templ_Id : constant Entity_Id := Defining_Entity (Templ);
4739 procedure Add_Generic_Contract_Pragma (Prag : Node_Id);
4740 -- Add a single contract-related source pragma Prag to the contract of
4741 -- generic template Templ_Id.
4743 ---------------------------------
4744 -- Add_Generic_Contract_Pragma --
4745 ---------------------------------
4747 procedure Add_Generic_Contract_Pragma (Prag : Node_Id) is
4748 Prag_Templ : Node_Id;
4750 begin
4751 -- Mark the pragma to prevent the premature capture of global
4752 -- references when capturing global references of the context
4753 -- (see Save_References_In_Pragma).
4755 Set_Is_Generic_Contract_Pragma (Prag);
4757 -- Pragmas that apply to a generic subprogram declaration are not
4758 -- part of the semantic structure of the generic template:
4760 -- generic
4761 -- procedure Example (Formal : Integer);
4762 -- pragma Precondition (Formal > 0);
4764 -- Create a generic template for such pragmas and link the template
4765 -- of the pragma with the generic template.
4767 if Nkind (Templ) = N_Generic_Subprogram_Declaration then
4768 Rewrite
4769 (Prag, Copy_Generic_Node (Prag, Empty, Instantiating => False));
4770 Prag_Templ := Original_Node (Prag);
4772 Set_Is_Generic_Contract_Pragma (Prag_Templ);
4773 Add_Contract_Item (Prag_Templ, Templ_Id);
4775 -- Otherwise link the pragma with the generic template
4777 else
4778 Add_Contract_Item (Prag, Templ_Id);
4779 end if;
4780 end Add_Generic_Contract_Pragma;
4782 -- Local variables
4784 Context : constant Node_Id := Parent (Unit);
4785 Decl : Node_Id := Empty;
4787 -- Start of processing for Create_Generic_Contract
4789 begin
4790 -- A generic package declaration carries contract-related source pragmas
4791 -- in its visible declarations.
4793 if Nkind (Templ) = N_Generic_Package_Declaration then
4794 Set_Ekind (Templ_Id, E_Generic_Package);
4796 if Present (Visible_Declarations (Specification (Templ))) then
4797 Decl := First (Visible_Declarations (Specification (Templ)));
4798 end if;
4800 -- A generic package body carries contract-related source pragmas in its
4801 -- declarations.
4803 elsif Nkind (Templ) = N_Package_Body then
4804 Set_Ekind (Templ_Id, E_Package_Body);
4806 if Present (Declarations (Templ)) then
4807 Decl := First (Declarations (Templ));
4808 end if;
4810 -- Generic subprogram declaration
4812 elsif Nkind (Templ) = N_Generic_Subprogram_Declaration then
4813 if Nkind (Specification (Templ)) = N_Function_Specification then
4814 Set_Ekind (Templ_Id, E_Generic_Function);
4815 else
4816 Set_Ekind (Templ_Id, E_Generic_Procedure);
4817 end if;
4819 -- When the generic subprogram acts as a compilation unit, inspect
4820 -- the Pragmas_After list for contract-related source pragmas.
4822 if Nkind (Context) = N_Compilation_Unit then
4823 if Present (Aux_Decls_Node (Context))
4824 and then Present (Pragmas_After (Aux_Decls_Node (Context)))
4825 then
4826 Decl := First (Pragmas_After (Aux_Decls_Node (Context)));
4827 end if;
4829 -- Otherwise inspect the successive declarations for contract-related
4830 -- source pragmas.
4832 else
4833 Decl := Next (Unit);
4834 end if;
4836 -- A generic subprogram body carries contract-related source pragmas in
4837 -- its declarations.
4839 elsif Nkind (Templ) = N_Subprogram_Body then
4840 Set_Ekind (Templ_Id, E_Subprogram_Body);
4842 if Present (Declarations (Templ)) then
4843 Decl := First (Declarations (Templ));
4844 end if;
4845 end if;
4847 -- Inspect the relevant declarations looking for contract-related source
4848 -- pragmas and add them to the contract of the generic unit.
4850 while Present (Decl) loop
4851 if Comes_From_Source (Decl) then
4852 if Nkind (Decl) = N_Pragma then
4854 -- The source pragma is a contract annotation
4856 if Is_Contract_Annotation (Decl) then
4857 Add_Generic_Contract_Pragma (Decl);
4858 end if;
4860 -- The region where a contract-related source pragma may appear
4861 -- ends with the first source non-pragma declaration or statement.
4863 else
4864 exit;
4865 end if;
4866 end if;
4868 Next (Decl);
4869 end loop;
4870 end Create_Generic_Contract;
4872 --------------------
4873 -- Current_Entity --
4874 --------------------
4876 -- The currently visible definition for a given identifier is the
4877 -- one most chained at the start of the visibility chain, i.e. the
4878 -- one that is referenced by the Node_Id value of the name of the
4879 -- given identifier.
4881 function Current_Entity (N : Node_Id) return Entity_Id is
4882 begin
4883 return Get_Name_Entity_Id (Chars (N));
4884 end Current_Entity;
4886 -----------------------------
4887 -- Current_Entity_In_Scope --
4888 -----------------------------
4890 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
4891 E : Entity_Id;
4892 CS : constant Entity_Id := Current_Scope;
4894 Transient_Case : constant Boolean := Scope_Is_Transient;
4896 begin
4897 E := Get_Name_Entity_Id (Chars (N));
4898 while Present (E)
4899 and then Scope (E) /= CS
4900 and then (not Transient_Case or else Scope (E) /= Scope (CS))
4901 loop
4902 E := Homonym (E);
4903 end loop;
4905 return E;
4906 end Current_Entity_In_Scope;
4908 -------------------
4909 -- Current_Scope --
4910 -------------------
4912 function Current_Scope return Entity_Id is
4913 begin
4914 if Scope_Stack.Last = -1 then
4915 return Standard_Standard;
4916 else
4917 declare
4918 C : constant Entity_Id :=
4919 Scope_Stack.Table (Scope_Stack.Last).Entity;
4920 begin
4921 if Present (C) then
4922 return C;
4923 else
4924 return Standard_Standard;
4925 end if;
4926 end;
4927 end if;
4928 end Current_Scope;
4930 ------------------------
4931 -- Current_Subprogram --
4932 ------------------------
4934 function Current_Subprogram return Entity_Id is
4935 Scop : constant Entity_Id := Current_Scope;
4936 begin
4937 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
4938 return Scop;
4939 else
4940 return Enclosing_Subprogram (Scop);
4941 end if;
4942 end Current_Subprogram;
4944 ----------------------------------
4945 -- Deepest_Type_Access_Level --
4946 ----------------------------------
4948 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
4949 begin
4950 if Ekind (Typ) = E_Anonymous_Access_Type
4951 and then not Is_Local_Anonymous_Access (Typ)
4952 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
4953 then
4954 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
4955 -- access type.
4957 return
4958 Scope_Depth (Enclosing_Dynamic_Scope
4959 (Defining_Identifier
4960 (Associated_Node_For_Itype (Typ))));
4962 -- For generic formal type, return Int'Last (infinite).
4963 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
4965 elsif Is_Generic_Type (Root_Type (Typ)) then
4966 return UI_From_Int (Int'Last);
4968 else
4969 return Type_Access_Level (Typ);
4970 end if;
4971 end Deepest_Type_Access_Level;
4973 ---------------------
4974 -- Defining_Entity --
4975 ---------------------
4977 function Defining_Entity (N : Node_Id) return Entity_Id is
4978 K : constant Node_Kind := Nkind (N);
4979 Err : Entity_Id := Empty;
4981 begin
4982 case K is
4983 when
4984 N_Subprogram_Declaration |
4985 N_Abstract_Subprogram_Declaration |
4986 N_Subprogram_Body |
4987 N_Package_Declaration |
4988 N_Subprogram_Renaming_Declaration |
4989 N_Subprogram_Body_Stub |
4990 N_Generic_Subprogram_Declaration |
4991 N_Generic_Package_Declaration |
4992 N_Formal_Subprogram_Declaration |
4993 N_Expression_Function
4995 return Defining_Entity (Specification (N));
4997 when
4998 N_Component_Declaration |
4999 N_Defining_Program_Unit_Name |
5000 N_Discriminant_Specification |
5001 N_Entry_Body |
5002 N_Entry_Declaration |
5003 N_Entry_Index_Specification |
5004 N_Exception_Declaration |
5005 N_Exception_Renaming_Declaration |
5006 N_Formal_Object_Declaration |
5007 N_Formal_Package_Declaration |
5008 N_Formal_Type_Declaration |
5009 N_Full_Type_Declaration |
5010 N_Implicit_Label_Declaration |
5011 N_Incomplete_Type_Declaration |
5012 N_Loop_Parameter_Specification |
5013 N_Number_Declaration |
5014 N_Object_Declaration |
5015 N_Object_Renaming_Declaration |
5016 N_Package_Body_Stub |
5017 N_Parameter_Specification |
5018 N_Private_Extension_Declaration |
5019 N_Private_Type_Declaration |
5020 N_Protected_Body |
5021 N_Protected_Body_Stub |
5022 N_Protected_Type_Declaration |
5023 N_Single_Protected_Declaration |
5024 N_Single_Task_Declaration |
5025 N_Subtype_Declaration |
5026 N_Task_Body |
5027 N_Task_Body_Stub |
5028 N_Task_Type_Declaration
5030 return Defining_Identifier (N);
5032 when N_Subunit =>
5033 return Defining_Entity (Proper_Body (N));
5035 when
5036 N_Function_Instantiation |
5037 N_Function_Specification |
5038 N_Generic_Function_Renaming_Declaration |
5039 N_Generic_Package_Renaming_Declaration |
5040 N_Generic_Procedure_Renaming_Declaration |
5041 N_Package_Body |
5042 N_Package_Instantiation |
5043 N_Package_Renaming_Declaration |
5044 N_Package_Specification |
5045 N_Procedure_Instantiation |
5046 N_Procedure_Specification
5048 declare
5049 Nam : constant Node_Id := Defining_Unit_Name (N);
5051 begin
5052 if Nkind (Nam) in N_Entity then
5053 return Nam;
5055 -- For Error, make up a name and attach to declaration
5056 -- so we can continue semantic analysis
5058 elsif Nam = Error then
5059 Err := Make_Temporary (Sloc (N), 'T');
5060 Set_Defining_Unit_Name (N, Err);
5062 return Err;
5064 -- If not an entity, get defining identifier
5066 else
5067 return Defining_Identifier (Nam);
5068 end if;
5069 end;
5071 when
5072 N_Block_Statement |
5073 N_Loop_Statement
5075 return Entity (Identifier (N));
5077 when others =>
5078 raise Program_Error;
5080 end case;
5081 end Defining_Entity;
5083 --------------------------
5084 -- Denotes_Discriminant --
5085 --------------------------
5087 function Denotes_Discriminant
5088 (N : Node_Id;
5089 Check_Concurrent : Boolean := False) return Boolean
5091 E : Entity_Id;
5093 begin
5094 if not Is_Entity_Name (N) or else No (Entity (N)) then
5095 return False;
5096 else
5097 E := Entity (N);
5098 end if;
5100 -- If we are checking for a protected type, the discriminant may have
5101 -- been rewritten as the corresponding discriminal of the original type
5102 -- or of the corresponding concurrent record, depending on whether we
5103 -- are in the spec or body of the protected type.
5105 return Ekind (E) = E_Discriminant
5106 or else
5107 (Check_Concurrent
5108 and then Ekind (E) = E_In_Parameter
5109 and then Present (Discriminal_Link (E))
5110 and then
5111 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
5112 or else
5113 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
5115 end Denotes_Discriminant;
5117 -------------------------
5118 -- Denotes_Same_Object --
5119 -------------------------
5121 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
5122 Obj1 : Node_Id := A1;
5123 Obj2 : Node_Id := A2;
5125 function Has_Prefix (N : Node_Id) return Boolean;
5126 -- Return True if N has attribute Prefix
5128 function Is_Renaming (N : Node_Id) return Boolean;
5129 -- Return true if N names a renaming entity
5131 function Is_Valid_Renaming (N : Node_Id) return Boolean;
5132 -- For renamings, return False if the prefix of any dereference within
5133 -- the renamed object_name is a variable, or any expression within the
5134 -- renamed object_name contains references to variables or calls on
5135 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
5137 ----------------
5138 -- Has_Prefix --
5139 ----------------
5141 function Has_Prefix (N : Node_Id) return Boolean is
5142 begin
5143 return
5144 Nkind_In (N,
5145 N_Attribute_Reference,
5146 N_Expanded_Name,
5147 N_Explicit_Dereference,
5148 N_Indexed_Component,
5149 N_Reference,
5150 N_Selected_Component,
5151 N_Slice);
5152 end Has_Prefix;
5154 -----------------
5155 -- Is_Renaming --
5156 -----------------
5158 function Is_Renaming (N : Node_Id) return Boolean is
5159 begin
5160 return Is_Entity_Name (N)
5161 and then Present (Renamed_Entity (Entity (N)));
5162 end Is_Renaming;
5164 -----------------------
5165 -- Is_Valid_Renaming --
5166 -----------------------
5168 function Is_Valid_Renaming (N : Node_Id) return Boolean is
5170 function Check_Renaming (N : Node_Id) return Boolean;
5171 -- Recursive function used to traverse all the prefixes of N
5173 function Check_Renaming (N : Node_Id) return Boolean is
5174 begin
5175 if Is_Renaming (N)
5176 and then not Check_Renaming (Renamed_Entity (Entity (N)))
5177 then
5178 return False;
5179 end if;
5181 if Nkind (N) = N_Indexed_Component then
5182 declare
5183 Indx : Node_Id;
5185 begin
5186 Indx := First (Expressions (N));
5187 while Present (Indx) loop
5188 if not Is_OK_Static_Expression (Indx) then
5189 return False;
5190 end if;
5192 Next_Index (Indx);
5193 end loop;
5194 end;
5195 end if;
5197 if Has_Prefix (N) then
5198 declare
5199 P : constant Node_Id := Prefix (N);
5201 begin
5202 if Nkind (N) = N_Explicit_Dereference
5203 and then Is_Variable (P)
5204 then
5205 return False;
5207 elsif Is_Entity_Name (P)
5208 and then Ekind (Entity (P)) = E_Function
5209 then
5210 return False;
5212 elsif Nkind (P) = N_Function_Call then
5213 return False;
5214 end if;
5216 -- Recursion to continue traversing the prefix of the
5217 -- renaming expression
5219 return Check_Renaming (P);
5220 end;
5221 end if;
5223 return True;
5224 end Check_Renaming;
5226 -- Start of processing for Is_Valid_Renaming
5228 begin
5229 return Check_Renaming (N);
5230 end Is_Valid_Renaming;
5232 -- Start of processing for Denotes_Same_Object
5234 begin
5235 -- Both names statically denote the same stand-alone object or parameter
5236 -- (RM 6.4.1(6.5/3))
5238 if Is_Entity_Name (Obj1)
5239 and then Is_Entity_Name (Obj2)
5240 and then Entity (Obj1) = Entity (Obj2)
5241 then
5242 return True;
5243 end if;
5245 -- For renamings, the prefix of any dereference within the renamed
5246 -- object_name is not a variable, and any expression within the
5247 -- renamed object_name contains no references to variables nor
5248 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
5250 if Is_Renaming (Obj1) then
5251 if Is_Valid_Renaming (Obj1) then
5252 Obj1 := Renamed_Entity (Entity (Obj1));
5253 else
5254 return False;
5255 end if;
5256 end if;
5258 if Is_Renaming (Obj2) then
5259 if Is_Valid_Renaming (Obj2) then
5260 Obj2 := Renamed_Entity (Entity (Obj2));
5261 else
5262 return False;
5263 end if;
5264 end if;
5266 -- No match if not same node kind (such cases are handled by
5267 -- Denotes_Same_Prefix)
5269 if Nkind (Obj1) /= Nkind (Obj2) then
5270 return False;
5272 -- After handling valid renamings, one of the two names statically
5273 -- denoted a renaming declaration whose renamed object_name is known
5274 -- to denote the same object as the other (RM 6.4.1(6.10/3))
5276 elsif Is_Entity_Name (Obj1) then
5277 if Is_Entity_Name (Obj2) then
5278 return Entity (Obj1) = Entity (Obj2);
5279 else
5280 return False;
5281 end if;
5283 -- Both names are selected_components, their prefixes are known to
5284 -- denote the same object, and their selector_names denote the same
5285 -- component (RM 6.4.1(6.6/3)).
5287 elsif Nkind (Obj1) = N_Selected_Component then
5288 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5289 and then
5290 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
5292 -- Both names are dereferences and the dereferenced names are known to
5293 -- denote the same object (RM 6.4.1(6.7/3))
5295 elsif Nkind (Obj1) = N_Explicit_Dereference then
5296 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
5298 -- Both names are indexed_components, their prefixes are known to denote
5299 -- the same object, and each of the pairs of corresponding index values
5300 -- are either both static expressions with the same static value or both
5301 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
5303 elsif Nkind (Obj1) = N_Indexed_Component then
5304 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
5305 return False;
5306 else
5307 declare
5308 Indx1 : Node_Id;
5309 Indx2 : Node_Id;
5311 begin
5312 Indx1 := First (Expressions (Obj1));
5313 Indx2 := First (Expressions (Obj2));
5314 while Present (Indx1) loop
5316 -- Indexes must denote the same static value or same object
5318 if Is_OK_Static_Expression (Indx1) then
5319 if not Is_OK_Static_Expression (Indx2) then
5320 return False;
5322 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
5323 return False;
5324 end if;
5326 elsif not Denotes_Same_Object (Indx1, Indx2) then
5327 return False;
5328 end if;
5330 Next (Indx1);
5331 Next (Indx2);
5332 end loop;
5334 return True;
5335 end;
5336 end if;
5338 -- Both names are slices, their prefixes are known to denote the same
5339 -- object, and the two slices have statically matching index constraints
5340 -- (RM 6.4.1(6.9/3))
5342 elsif Nkind (Obj1) = N_Slice
5343 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5344 then
5345 declare
5346 Lo1, Lo2, Hi1, Hi2 : Node_Id;
5348 begin
5349 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
5350 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
5352 -- Check whether bounds are statically identical. There is no
5353 -- attempt to detect partial overlap of slices.
5355 return Denotes_Same_Object (Lo1, Lo2)
5356 and then
5357 Denotes_Same_Object (Hi1, Hi2);
5358 end;
5360 -- In the recursion, literals appear as indexes
5362 elsif Nkind (Obj1) = N_Integer_Literal
5363 and then
5364 Nkind (Obj2) = N_Integer_Literal
5365 then
5366 return Intval (Obj1) = Intval (Obj2);
5368 else
5369 return False;
5370 end if;
5371 end Denotes_Same_Object;
5373 -------------------------
5374 -- Denotes_Same_Prefix --
5375 -------------------------
5377 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
5379 begin
5380 if Is_Entity_Name (A1) then
5381 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
5382 and then not Is_Access_Type (Etype (A1))
5383 then
5384 return Denotes_Same_Object (A1, Prefix (A2))
5385 or else Denotes_Same_Prefix (A1, Prefix (A2));
5386 else
5387 return False;
5388 end if;
5390 elsif Is_Entity_Name (A2) then
5391 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
5393 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
5394 and then
5395 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
5396 then
5397 declare
5398 Root1, Root2 : Node_Id;
5399 Depth1, Depth2 : Int := 0;
5401 begin
5402 Root1 := Prefix (A1);
5403 while not Is_Entity_Name (Root1) loop
5404 if not Nkind_In
5405 (Root1, N_Selected_Component, N_Indexed_Component)
5406 then
5407 return False;
5408 else
5409 Root1 := Prefix (Root1);
5410 end if;
5412 Depth1 := Depth1 + 1;
5413 end loop;
5415 Root2 := Prefix (A2);
5416 while not Is_Entity_Name (Root2) loop
5417 if not Nkind_In (Root2, N_Selected_Component,
5418 N_Indexed_Component)
5419 then
5420 return False;
5421 else
5422 Root2 := Prefix (Root2);
5423 end if;
5425 Depth2 := Depth2 + 1;
5426 end loop;
5428 -- If both have the same depth and they do not denote the same
5429 -- object, they are disjoint and no warning is needed.
5431 if Depth1 = Depth2 then
5432 return False;
5434 elsif Depth1 > Depth2 then
5435 Root1 := Prefix (A1);
5436 for J in 1 .. Depth1 - Depth2 - 1 loop
5437 Root1 := Prefix (Root1);
5438 end loop;
5440 return Denotes_Same_Object (Root1, A2);
5442 else
5443 Root2 := Prefix (A2);
5444 for J in 1 .. Depth2 - Depth1 - 1 loop
5445 Root2 := Prefix (Root2);
5446 end loop;
5448 return Denotes_Same_Object (A1, Root2);
5449 end if;
5450 end;
5452 else
5453 return False;
5454 end if;
5455 end Denotes_Same_Prefix;
5457 ----------------------
5458 -- Denotes_Variable --
5459 ----------------------
5461 function Denotes_Variable (N : Node_Id) return Boolean is
5462 begin
5463 return Is_Variable (N) and then Paren_Count (N) = 0;
5464 end Denotes_Variable;
5466 -----------------------------
5467 -- Depends_On_Discriminant --
5468 -----------------------------
5470 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5471 L : Node_Id;
5472 H : Node_Id;
5474 begin
5475 Get_Index_Bounds (N, L, H);
5476 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5477 end Depends_On_Discriminant;
5479 -------------------------
5480 -- Designate_Same_Unit --
5481 -------------------------
5483 function Designate_Same_Unit
5484 (Name1 : Node_Id;
5485 Name2 : Node_Id) return Boolean
5487 K1 : constant Node_Kind := Nkind (Name1);
5488 K2 : constant Node_Kind := Nkind (Name2);
5490 function Prefix_Node (N : Node_Id) return Node_Id;
5491 -- Returns the parent unit name node of a defining program unit name
5492 -- or the prefix if N is a selected component or an expanded name.
5494 function Select_Node (N : Node_Id) return Node_Id;
5495 -- Returns the defining identifier node of a defining program unit
5496 -- name or the selector node if N is a selected component or an
5497 -- expanded name.
5499 -----------------
5500 -- Prefix_Node --
5501 -----------------
5503 function Prefix_Node (N : Node_Id) return Node_Id is
5504 begin
5505 if Nkind (N) = N_Defining_Program_Unit_Name then
5506 return Name (N);
5507 else
5508 return Prefix (N);
5509 end if;
5510 end Prefix_Node;
5512 -----------------
5513 -- Select_Node --
5514 -----------------
5516 function Select_Node (N : Node_Id) return Node_Id is
5517 begin
5518 if Nkind (N) = N_Defining_Program_Unit_Name then
5519 return Defining_Identifier (N);
5520 else
5521 return Selector_Name (N);
5522 end if;
5523 end Select_Node;
5525 -- Start of processing for Designate_Same_Unit
5527 begin
5528 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
5529 and then
5530 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
5531 then
5532 return Chars (Name1) = Chars (Name2);
5534 elsif Nkind_In (K1, N_Expanded_Name,
5535 N_Selected_Component,
5536 N_Defining_Program_Unit_Name)
5537 and then
5538 Nkind_In (K2, N_Expanded_Name,
5539 N_Selected_Component,
5540 N_Defining_Program_Unit_Name)
5541 then
5542 return
5543 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5544 and then
5545 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5547 else
5548 return False;
5549 end if;
5550 end Designate_Same_Unit;
5552 ------------------------------------------
5553 -- function Dynamic_Accessibility_Level --
5554 ------------------------------------------
5556 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5557 E : Entity_Id;
5558 Loc : constant Source_Ptr := Sloc (Expr);
5560 function Make_Level_Literal (Level : Uint) return Node_Id;
5561 -- Construct an integer literal representing an accessibility level
5562 -- with its type set to Natural.
5564 ------------------------
5565 -- Make_Level_Literal --
5566 ------------------------
5568 function Make_Level_Literal (Level : Uint) return Node_Id is
5569 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5570 begin
5571 Set_Etype (Result, Standard_Natural);
5572 return Result;
5573 end Make_Level_Literal;
5575 -- Start of processing for Dynamic_Accessibility_Level
5577 begin
5578 if Is_Entity_Name (Expr) then
5579 E := Entity (Expr);
5581 if Present (Renamed_Object (E)) then
5582 return Dynamic_Accessibility_Level (Renamed_Object (E));
5583 end if;
5585 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5586 if Present (Extra_Accessibility (E)) then
5587 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5588 end if;
5589 end if;
5590 end if;
5592 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5594 case Nkind (Expr) is
5596 -- For access discriminant, the level of the enclosing object
5598 when N_Selected_Component =>
5599 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5600 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5601 E_Anonymous_Access_Type
5602 then
5603 return Make_Level_Literal (Object_Access_Level (Expr));
5604 end if;
5606 when N_Attribute_Reference =>
5607 case Get_Attribute_Id (Attribute_Name (Expr)) is
5609 -- For X'Access, the level of the prefix X
5611 when Attribute_Access =>
5612 return Make_Level_Literal
5613 (Object_Access_Level (Prefix (Expr)));
5615 -- Treat the unchecked attributes as library-level
5617 when Attribute_Unchecked_Access |
5618 Attribute_Unrestricted_Access =>
5619 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5621 -- No other access-valued attributes
5623 when others =>
5624 raise Program_Error;
5625 end case;
5627 when N_Allocator =>
5629 -- Unimplemented: depends on context. As an actual parameter where
5630 -- formal type is anonymous, use
5631 -- Scope_Depth (Current_Scope) + 1.
5632 -- For other cases, see 3.10.2(14/3) and following. ???
5634 null;
5636 when N_Type_Conversion =>
5637 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5639 -- Handle type conversions introduced for a rename of an
5640 -- Ada 2012 stand-alone object of an anonymous access type.
5642 return Dynamic_Accessibility_Level (Expression (Expr));
5643 end if;
5645 when others =>
5646 null;
5647 end case;
5649 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5650 end Dynamic_Accessibility_Level;
5652 -----------------------------------
5653 -- Effective_Extra_Accessibility --
5654 -----------------------------------
5656 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5657 begin
5658 if Present (Renamed_Object (Id))
5659 and then Is_Entity_Name (Renamed_Object (Id))
5660 then
5661 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5662 else
5663 return Extra_Accessibility (Id);
5664 end if;
5665 end Effective_Extra_Accessibility;
5667 -----------------------------
5668 -- Effective_Reads_Enabled --
5669 -----------------------------
5671 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5672 begin
5673 return Has_Enabled_Property (Id, Name_Effective_Reads);
5674 end Effective_Reads_Enabled;
5676 ------------------------------
5677 -- Effective_Writes_Enabled --
5678 ------------------------------
5680 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5681 begin
5682 return Has_Enabled_Property (Id, Name_Effective_Writes);
5683 end Effective_Writes_Enabled;
5685 ------------------------------
5686 -- Enclosing_Comp_Unit_Node --
5687 ------------------------------
5689 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5690 Current_Node : Node_Id;
5692 begin
5693 Current_Node := N;
5694 while Present (Current_Node)
5695 and then Nkind (Current_Node) /= N_Compilation_Unit
5696 loop
5697 Current_Node := Parent (Current_Node);
5698 end loop;
5700 if Nkind (Current_Node) /= N_Compilation_Unit then
5701 return Empty;
5702 else
5703 return Current_Node;
5704 end if;
5705 end Enclosing_Comp_Unit_Node;
5707 --------------------------
5708 -- Enclosing_CPP_Parent --
5709 --------------------------
5711 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5712 Parent_Typ : Entity_Id := Typ;
5714 begin
5715 while not Is_CPP_Class (Parent_Typ)
5716 and then Etype (Parent_Typ) /= Parent_Typ
5717 loop
5718 Parent_Typ := Etype (Parent_Typ);
5720 if Is_Private_Type (Parent_Typ) then
5721 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5722 end if;
5723 end loop;
5725 pragma Assert (Is_CPP_Class (Parent_Typ));
5726 return Parent_Typ;
5727 end Enclosing_CPP_Parent;
5729 ---------------------------
5730 -- Enclosing_Declaration --
5731 ---------------------------
5733 function Enclosing_Declaration (N : Node_Id) return Node_Id is
5734 Decl : Node_Id := N;
5736 begin
5737 while Present (Decl)
5738 and then not (Nkind (Decl) in N_Declaration
5739 or else
5740 Nkind (Decl) in N_Later_Decl_Item)
5741 loop
5742 Decl := Parent (Decl);
5743 end loop;
5745 return Decl;
5746 end Enclosing_Declaration;
5748 ----------------------------
5749 -- Enclosing_Generic_Body --
5750 ----------------------------
5752 function Enclosing_Generic_Body
5753 (N : Node_Id) return Node_Id
5755 P : Node_Id;
5756 Decl : Node_Id;
5757 Spec : Node_Id;
5759 begin
5760 P := Parent (N);
5761 while Present (P) loop
5762 if Nkind (P) = N_Package_Body
5763 or else Nkind (P) = N_Subprogram_Body
5764 then
5765 Spec := Corresponding_Spec (P);
5767 if Present (Spec) then
5768 Decl := Unit_Declaration_Node (Spec);
5770 if Nkind (Decl) = N_Generic_Package_Declaration
5771 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5772 then
5773 return P;
5774 end if;
5775 end if;
5776 end if;
5778 P := Parent (P);
5779 end loop;
5781 return Empty;
5782 end Enclosing_Generic_Body;
5784 ----------------------------
5785 -- Enclosing_Generic_Unit --
5786 ----------------------------
5788 function Enclosing_Generic_Unit
5789 (N : Node_Id) return Node_Id
5791 P : Node_Id;
5792 Decl : Node_Id;
5793 Spec : Node_Id;
5795 begin
5796 P := Parent (N);
5797 while Present (P) loop
5798 if Nkind (P) = N_Generic_Package_Declaration
5799 or else Nkind (P) = N_Generic_Subprogram_Declaration
5800 then
5801 return P;
5803 elsif Nkind (P) = N_Package_Body
5804 or else Nkind (P) = N_Subprogram_Body
5805 then
5806 Spec := Corresponding_Spec (P);
5808 if Present (Spec) then
5809 Decl := Unit_Declaration_Node (Spec);
5811 if Nkind (Decl) = N_Generic_Package_Declaration
5812 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5813 then
5814 return Decl;
5815 end if;
5816 end if;
5817 end if;
5819 P := Parent (P);
5820 end loop;
5822 return Empty;
5823 end Enclosing_Generic_Unit;
5825 -------------------------------
5826 -- Enclosing_Lib_Unit_Entity --
5827 -------------------------------
5829 function Enclosing_Lib_Unit_Entity
5830 (E : Entity_Id := Current_Scope) return Entity_Id
5832 Unit_Entity : Entity_Id;
5834 begin
5835 -- Look for enclosing library unit entity by following scope links.
5836 -- Equivalent to, but faster than indexing through the scope stack.
5838 Unit_Entity := E;
5839 while (Present (Scope (Unit_Entity))
5840 and then Scope (Unit_Entity) /= Standard_Standard)
5841 and not Is_Child_Unit (Unit_Entity)
5842 loop
5843 Unit_Entity := Scope (Unit_Entity);
5844 end loop;
5846 return Unit_Entity;
5847 end Enclosing_Lib_Unit_Entity;
5849 -----------------------------
5850 -- Enclosing_Lib_Unit_Node --
5851 -----------------------------
5853 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
5854 Encl_Unit : Node_Id;
5856 begin
5857 Encl_Unit := Enclosing_Comp_Unit_Node (N);
5858 while Present (Encl_Unit)
5859 and then Nkind (Unit (Encl_Unit)) = N_Subunit
5860 loop
5861 Encl_Unit := Library_Unit (Encl_Unit);
5862 end loop;
5864 return Encl_Unit;
5865 end Enclosing_Lib_Unit_Node;
5867 -----------------------
5868 -- Enclosing_Package --
5869 -----------------------
5871 function Enclosing_Package (E : Entity_Id) return Entity_Id is
5872 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5874 begin
5875 if Dynamic_Scope = Standard_Standard then
5876 return Standard_Standard;
5878 elsif Dynamic_Scope = Empty then
5879 return Empty;
5881 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
5882 E_Generic_Package)
5883 then
5884 return Dynamic_Scope;
5886 else
5887 return Enclosing_Package (Dynamic_Scope);
5888 end if;
5889 end Enclosing_Package;
5891 -------------------------------------
5892 -- Enclosing_Package_Or_Subprogram --
5893 -------------------------------------
5895 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
5896 S : Entity_Id;
5898 begin
5899 S := Scope (E);
5900 while Present (S) loop
5901 if Is_Package_Or_Generic_Package (S)
5902 or else Ekind (S) = E_Package_Body
5903 then
5904 return S;
5906 elsif Is_Subprogram_Or_Generic_Subprogram (S)
5907 or else Ekind (S) = E_Subprogram_Body
5908 then
5909 return S;
5911 else
5912 S := Scope (S);
5913 end if;
5914 end loop;
5916 return Empty;
5917 end Enclosing_Package_Or_Subprogram;
5919 --------------------------
5920 -- Enclosing_Subprogram --
5921 --------------------------
5923 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
5924 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5926 begin
5927 if Dynamic_Scope = Standard_Standard then
5928 return Empty;
5930 elsif Dynamic_Scope = Empty then
5931 return Empty;
5933 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
5934 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
5936 elsif Ekind (Dynamic_Scope) = E_Block
5937 or else Ekind (Dynamic_Scope) = E_Return_Statement
5938 then
5939 return Enclosing_Subprogram (Dynamic_Scope);
5941 elsif Ekind (Dynamic_Scope) = E_Task_Type then
5942 return Get_Task_Body_Procedure (Dynamic_Scope);
5944 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
5945 and then Present (Full_View (Dynamic_Scope))
5946 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
5947 then
5948 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
5950 -- No body is generated if the protected operation is eliminated
5952 elsif Convention (Dynamic_Scope) = Convention_Protected
5953 and then not Is_Eliminated (Dynamic_Scope)
5954 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
5955 then
5956 return Protected_Body_Subprogram (Dynamic_Scope);
5958 else
5959 return Dynamic_Scope;
5960 end if;
5961 end Enclosing_Subprogram;
5963 ------------------------
5964 -- Ensure_Freeze_Node --
5965 ------------------------
5967 procedure Ensure_Freeze_Node (E : Entity_Id) is
5968 FN : Node_Id;
5969 begin
5970 if No (Freeze_Node (E)) then
5971 FN := Make_Freeze_Entity (Sloc (E));
5972 Set_Has_Delayed_Freeze (E);
5973 Set_Freeze_Node (E, FN);
5974 Set_Access_Types_To_Process (FN, No_Elist);
5975 Set_TSS_Elist (FN, No_Elist);
5976 Set_Entity (FN, E);
5977 end if;
5978 end Ensure_Freeze_Node;
5980 ----------------
5981 -- Enter_Name --
5982 ----------------
5984 procedure Enter_Name (Def_Id : Entity_Id) is
5985 C : constant Entity_Id := Current_Entity (Def_Id);
5986 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
5987 S : constant Entity_Id := Current_Scope;
5989 begin
5990 Generate_Definition (Def_Id);
5992 -- Add new name to current scope declarations. Check for duplicate
5993 -- declaration, which may or may not be a genuine error.
5995 if Present (E) then
5997 -- Case of previous entity entered because of a missing declaration
5998 -- or else a bad subtype indication. Best is to use the new entity,
5999 -- and make the previous one invisible.
6001 if Etype (E) = Any_Type then
6002 Set_Is_Immediately_Visible (E, False);
6004 -- Case of renaming declaration constructed for package instances.
6005 -- if there is an explicit declaration with the same identifier,
6006 -- the renaming is not immediately visible any longer, but remains
6007 -- visible through selected component notation.
6009 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
6010 and then not Comes_From_Source (E)
6011 then
6012 Set_Is_Immediately_Visible (E, False);
6014 -- The new entity may be the package renaming, which has the same
6015 -- same name as a generic formal which has been seen already.
6017 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
6018 and then not Comes_From_Source (Def_Id)
6019 then
6020 Set_Is_Immediately_Visible (E, False);
6022 -- For a fat pointer corresponding to a remote access to subprogram,
6023 -- we use the same identifier as the RAS type, so that the proper
6024 -- name appears in the stub. This type is only retrieved through
6025 -- the RAS type and never by visibility, and is not added to the
6026 -- visibility list (see below).
6028 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
6029 and then Ekind (Def_Id) = E_Record_Type
6030 and then Present (Corresponding_Remote_Type (Def_Id))
6031 then
6032 null;
6034 -- Case of an implicit operation or derived literal. The new entity
6035 -- hides the implicit one, which is removed from all visibility,
6036 -- i.e. the entity list of its scope, and homonym chain of its name.
6038 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
6039 or else Is_Internal (E)
6040 then
6041 declare
6042 Prev : Entity_Id;
6043 Prev_Vis : Entity_Id;
6044 Decl : constant Node_Id := Parent (E);
6046 begin
6047 -- If E is an implicit declaration, it cannot be the first
6048 -- entity in the scope.
6050 Prev := First_Entity (Current_Scope);
6051 while Present (Prev) and then Next_Entity (Prev) /= E loop
6052 Next_Entity (Prev);
6053 end loop;
6055 if No (Prev) then
6057 -- If E is not on the entity chain of the current scope,
6058 -- it is an implicit declaration in the generic formal
6059 -- part of a generic subprogram. When analyzing the body,
6060 -- the generic formals are visible but not on the entity
6061 -- chain of the subprogram. The new entity will become
6062 -- the visible one in the body.
6064 pragma Assert
6065 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
6066 null;
6068 else
6069 Set_Next_Entity (Prev, Next_Entity (E));
6071 if No (Next_Entity (Prev)) then
6072 Set_Last_Entity (Current_Scope, Prev);
6073 end if;
6075 if E = Current_Entity (E) then
6076 Prev_Vis := Empty;
6078 else
6079 Prev_Vis := Current_Entity (E);
6080 while Homonym (Prev_Vis) /= E loop
6081 Prev_Vis := Homonym (Prev_Vis);
6082 end loop;
6083 end if;
6085 if Present (Prev_Vis) then
6087 -- Skip E in the visibility chain
6089 Set_Homonym (Prev_Vis, Homonym (E));
6091 else
6092 Set_Name_Entity_Id (Chars (E), Homonym (E));
6093 end if;
6094 end if;
6095 end;
6097 -- This section of code could use a comment ???
6099 elsif Present (Etype (E))
6100 and then Is_Concurrent_Type (Etype (E))
6101 and then E = Def_Id
6102 then
6103 return;
6105 -- If the homograph is a protected component renaming, it should not
6106 -- be hiding the current entity. Such renamings are treated as weak
6107 -- declarations.
6109 elsif Is_Prival (E) then
6110 Set_Is_Immediately_Visible (E, False);
6112 -- In this case the current entity is a protected component renaming.
6113 -- Perform minimal decoration by setting the scope and return since
6114 -- the prival should not be hiding other visible entities.
6116 elsif Is_Prival (Def_Id) then
6117 Set_Scope (Def_Id, Current_Scope);
6118 return;
6120 -- Analogous to privals, the discriminal generated for an entry index
6121 -- parameter acts as a weak declaration. Perform minimal decoration
6122 -- to avoid bogus errors.
6124 elsif Is_Discriminal (Def_Id)
6125 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
6126 then
6127 Set_Scope (Def_Id, Current_Scope);
6128 return;
6130 -- In the body or private part of an instance, a type extension may
6131 -- introduce a component with the same name as that of an actual. The
6132 -- legality rule is not enforced, but the semantics of the full type
6133 -- with two components of same name are not clear at this point???
6135 elsif In_Instance_Not_Visible then
6136 null;
6138 -- When compiling a package body, some child units may have become
6139 -- visible. They cannot conflict with local entities that hide them.
6141 elsif Is_Child_Unit (E)
6142 and then In_Open_Scopes (Scope (E))
6143 and then not Is_Immediately_Visible (E)
6144 then
6145 null;
6147 -- Conversely, with front-end inlining we may compile the parent body
6148 -- first, and a child unit subsequently. The context is now the
6149 -- parent spec, and body entities are not visible.
6151 elsif Is_Child_Unit (Def_Id)
6152 and then Is_Package_Body_Entity (E)
6153 and then not In_Package_Body (Current_Scope)
6154 then
6155 null;
6157 -- Case of genuine duplicate declaration
6159 else
6160 Error_Msg_Sloc := Sloc (E);
6162 -- If the previous declaration is an incomplete type declaration
6163 -- this may be an attempt to complete it with a private type. The
6164 -- following avoids confusing cascaded errors.
6166 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
6167 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
6168 then
6169 Error_Msg_N
6170 ("incomplete type cannot be completed with a private " &
6171 "declaration", Parent (Def_Id));
6172 Set_Is_Immediately_Visible (E, False);
6173 Set_Full_View (E, Def_Id);
6175 -- An inherited component of a record conflicts with a new
6176 -- discriminant. The discriminant is inserted first in the scope,
6177 -- but the error should be posted on it, not on the component.
6179 elsif Ekind (E) = E_Discriminant
6180 and then Present (Scope (Def_Id))
6181 and then Scope (Def_Id) /= Current_Scope
6182 then
6183 Error_Msg_Sloc := Sloc (Def_Id);
6184 Error_Msg_N ("& conflicts with declaration#", E);
6185 return;
6187 -- If the name of the unit appears in its own context clause, a
6188 -- dummy package with the name has already been created, and the
6189 -- error emitted. Try to continue quietly.
6191 elsif Error_Posted (E)
6192 and then Sloc (E) = No_Location
6193 and then Nkind (Parent (E)) = N_Package_Specification
6194 and then Current_Scope = Standard_Standard
6195 then
6196 Set_Scope (Def_Id, Current_Scope);
6197 return;
6199 else
6200 Error_Msg_N ("& conflicts with declaration#", Def_Id);
6202 -- Avoid cascaded messages with duplicate components in
6203 -- derived types.
6205 if Ekind_In (E, E_Component, E_Discriminant) then
6206 return;
6207 end if;
6208 end if;
6210 if Nkind (Parent (Parent (Def_Id))) =
6211 N_Generic_Subprogram_Declaration
6212 and then Def_Id =
6213 Defining_Entity (Specification (Parent (Parent (Def_Id))))
6214 then
6215 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
6216 end if;
6218 -- If entity is in standard, then we are in trouble, because it
6219 -- means that we have a library package with a duplicated name.
6220 -- That's hard to recover from, so abort.
6222 if S = Standard_Standard then
6223 raise Unrecoverable_Error;
6225 -- Otherwise we continue with the declaration. Having two
6226 -- identical declarations should not cause us too much trouble.
6228 else
6229 null;
6230 end if;
6231 end if;
6232 end if;
6234 -- If we fall through, declaration is OK, at least OK enough to continue
6236 -- If Def_Id is a discriminant or a record component we are in the midst
6237 -- of inheriting components in a derived record definition. Preserve
6238 -- their Ekind and Etype.
6240 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
6241 null;
6243 -- If a type is already set, leave it alone (happens when a type
6244 -- declaration is reanalyzed following a call to the optimizer).
6246 elsif Present (Etype (Def_Id)) then
6247 null;
6249 -- Otherwise, the kind E_Void insures that premature uses of the entity
6250 -- will be detected. Any_Type insures that no cascaded errors will occur
6252 else
6253 Set_Ekind (Def_Id, E_Void);
6254 Set_Etype (Def_Id, Any_Type);
6255 end if;
6257 -- Inherited discriminants and components in derived record types are
6258 -- immediately visible. Itypes are not.
6260 -- Unless the Itype is for a record type with a corresponding remote
6261 -- type (what is that about, it was not commented ???)
6263 if Ekind_In (Def_Id, E_Discriminant, E_Component)
6264 or else
6265 ((not Is_Record_Type (Def_Id)
6266 or else No (Corresponding_Remote_Type (Def_Id)))
6267 and then not Is_Itype (Def_Id))
6268 then
6269 Set_Is_Immediately_Visible (Def_Id);
6270 Set_Current_Entity (Def_Id);
6271 end if;
6273 Set_Homonym (Def_Id, C);
6274 Append_Entity (Def_Id, S);
6275 Set_Public_Status (Def_Id);
6277 -- Declaring a homonym is not allowed in SPARK ...
6279 if Present (C) and then Restriction_Check_Required (SPARK_05) then
6280 declare
6281 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
6282 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
6283 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
6285 begin
6286 -- ... unless the new declaration is in a subprogram, and the
6287 -- visible declaration is a variable declaration or a parameter
6288 -- specification outside that subprogram.
6290 if Present (Enclosing_Subp)
6291 and then Nkind_In (Parent (C), N_Object_Declaration,
6292 N_Parameter_Specification)
6293 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
6294 then
6295 null;
6297 -- ... or the new declaration is in a package, and the visible
6298 -- declaration occurs outside that package.
6300 elsif Present (Enclosing_Pack)
6301 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
6302 then
6303 null;
6305 -- ... or the new declaration is a component declaration in a
6306 -- record type definition.
6308 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
6309 null;
6311 -- Don't issue error for non-source entities
6313 elsif Comes_From_Source (Def_Id)
6314 and then Comes_From_Source (C)
6315 then
6316 Error_Msg_Sloc := Sloc (C);
6317 Check_SPARK_05_Restriction
6318 ("redeclaration of identifier &#", Def_Id);
6319 end if;
6320 end;
6321 end if;
6323 -- Warn if new entity hides an old one
6325 if Warn_On_Hiding and then Present (C)
6327 -- Don't warn for record components since they always have a well
6328 -- defined scope which does not confuse other uses. Note that in
6329 -- some cases, Ekind has not been set yet.
6331 and then Ekind (C) /= E_Component
6332 and then Ekind (C) /= E_Discriminant
6333 and then Nkind (Parent (C)) /= N_Component_Declaration
6334 and then Ekind (Def_Id) /= E_Component
6335 and then Ekind (Def_Id) /= E_Discriminant
6336 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
6338 -- Don't warn for one character variables. It is too common to use
6339 -- such variables as locals and will just cause too many false hits.
6341 and then Length_Of_Name (Chars (C)) /= 1
6343 -- Don't warn for non-source entities
6345 and then Comes_From_Source (C)
6346 and then Comes_From_Source (Def_Id)
6348 -- Don't warn unless entity in question is in extended main source
6350 and then In_Extended_Main_Source_Unit (Def_Id)
6352 -- Finally, the hidden entity must be either immediately visible or
6353 -- use visible (i.e. from a used package).
6355 and then
6356 (Is_Immediately_Visible (C)
6357 or else
6358 Is_Potentially_Use_Visible (C))
6359 then
6360 Error_Msg_Sloc := Sloc (C);
6361 Error_Msg_N ("declaration hides &#?h?", Def_Id);
6362 end if;
6363 end Enter_Name;
6365 ---------------
6366 -- Entity_Of --
6367 ---------------
6369 function Entity_Of (N : Node_Id) return Entity_Id is
6370 Id : Entity_Id;
6372 begin
6373 Id := Empty;
6375 if Is_Entity_Name (N) then
6376 Id := Entity (N);
6378 -- Follow a possible chain of renamings to reach the root renamed
6379 -- object.
6381 while Present (Id) and then Present (Renamed_Object (Id)) loop
6382 if Is_Entity_Name (Renamed_Object (Id)) then
6383 Id := Entity (Renamed_Object (Id));
6384 else
6385 Id := Empty;
6386 exit;
6387 end if;
6388 end loop;
6389 end if;
6391 return Id;
6392 end Entity_Of;
6394 --------------------------
6395 -- Explain_Limited_Type --
6396 --------------------------
6398 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
6399 C : Entity_Id;
6401 begin
6402 -- For array, component type must be limited
6404 if Is_Array_Type (T) then
6405 Error_Msg_Node_2 := T;
6406 Error_Msg_NE
6407 ("\component type& of type& is limited", N, Component_Type (T));
6408 Explain_Limited_Type (Component_Type (T), N);
6410 elsif Is_Record_Type (T) then
6412 -- No need for extra messages if explicit limited record
6414 if Is_Limited_Record (Base_Type (T)) then
6415 return;
6416 end if;
6418 -- Otherwise find a limited component. Check only components that
6419 -- come from source, or inherited components that appear in the
6420 -- source of the ancestor.
6422 C := First_Component (T);
6423 while Present (C) loop
6424 if Is_Limited_Type (Etype (C))
6425 and then
6426 (Comes_From_Source (C)
6427 or else
6428 (Present (Original_Record_Component (C))
6429 and then
6430 Comes_From_Source (Original_Record_Component (C))))
6431 then
6432 Error_Msg_Node_2 := T;
6433 Error_Msg_NE ("\component& of type& has limited type", N, C);
6434 Explain_Limited_Type (Etype (C), N);
6435 return;
6436 end if;
6438 Next_Component (C);
6439 end loop;
6441 -- The type may be declared explicitly limited, even if no component
6442 -- of it is limited, in which case we fall out of the loop.
6443 return;
6444 end if;
6445 end Explain_Limited_Type;
6447 -------------------------------
6448 -- Extensions_Visible_Status --
6449 -------------------------------
6451 function Extensions_Visible_Status
6452 (Id : Entity_Id) return Extensions_Visible_Mode
6454 Arg : Node_Id;
6455 Decl : Node_Id;
6456 Expr : Node_Id;
6457 Prag : Node_Id;
6458 Subp : Entity_Id;
6460 begin
6461 -- When a formal parameter is subject to Extensions_Visible, the pragma
6462 -- is stored in the contract of related subprogram.
6464 if Is_Formal (Id) then
6465 Subp := Scope (Id);
6467 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
6468 Subp := Id;
6470 -- No other construct carries this pragma
6472 else
6473 return Extensions_Visible_None;
6474 end if;
6476 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
6478 -- In certain cases analysis may request the Extensions_Visible status
6479 -- of an expression function before the pragma has been analyzed yet.
6480 -- Inspect the declarative items after the expression function looking
6481 -- for the pragma (if any).
6483 if No (Prag) and then Is_Expression_Function (Subp) then
6484 Decl := Next (Unit_Declaration_Node (Subp));
6485 while Present (Decl) loop
6486 if Nkind (Decl) = N_Pragma
6487 and then Pragma_Name (Decl) = Name_Extensions_Visible
6488 then
6489 Prag := Decl;
6490 exit;
6492 -- A source construct ends the region where Extensions_Visible may
6493 -- appear, stop the traversal. An expanded expression function is
6494 -- no longer a source construct, but it must still be recognized.
6496 elsif Comes_From_Source (Decl)
6497 or else
6498 (Nkind_In (Decl, N_Subprogram_Body,
6499 N_Subprogram_Declaration)
6500 and then Is_Expression_Function (Defining_Entity (Decl)))
6501 then
6502 exit;
6503 end if;
6505 Next (Decl);
6506 end loop;
6507 end if;
6509 -- Extract the value from the Boolean expression (if any)
6511 if Present (Prag) then
6512 Arg := First (Pragma_Argument_Associations (Prag));
6514 if Present (Arg) then
6515 Expr := Get_Pragma_Arg (Arg);
6517 -- When the associated subprogram is an expression function, the
6518 -- argument of the pragma may not have been analyzed.
6520 if not Analyzed (Expr) then
6521 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6522 end if;
6524 -- Guard against cascading errors when the argument of pragma
6525 -- Extensions_Visible is not a valid static Boolean expression.
6527 if Error_Posted (Expr) then
6528 return Extensions_Visible_None;
6530 elsif Is_True (Expr_Value (Expr)) then
6531 return Extensions_Visible_True;
6533 else
6534 return Extensions_Visible_False;
6535 end if;
6537 -- Otherwise the aspect or pragma defaults to True
6539 else
6540 return Extensions_Visible_True;
6541 end if;
6543 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
6544 -- directly specified. In SPARK code, its value defaults to "False".
6546 elsif SPARK_Mode = On then
6547 return Extensions_Visible_False;
6549 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
6550 -- "True".
6552 else
6553 return Extensions_Visible_True;
6554 end if;
6555 end Extensions_Visible_Status;
6557 -----------------
6558 -- Find_Actual --
6559 -----------------
6561 procedure Find_Actual
6562 (N : Node_Id;
6563 Formal : out Entity_Id;
6564 Call : out Node_Id)
6566 Parnt : constant Node_Id := Parent (N);
6567 Actual : Node_Id;
6569 begin
6570 if Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6571 and then N = Prefix (Parnt)
6572 then
6573 Find_Actual (Parnt, Formal, Call);
6574 return;
6576 elsif Nkind (Parnt) = N_Parameter_Association
6577 and then N = Explicit_Actual_Parameter (Parnt)
6578 then
6579 Call := Parent (Parnt);
6581 elsif Nkind (Parnt) in N_Subprogram_Call then
6582 Call := Parnt;
6584 else
6585 Formal := Empty;
6586 Call := Empty;
6587 return;
6588 end if;
6590 -- If we have a call to a subprogram look for the parameter. Note that
6591 -- we exclude overloaded calls, since we don't know enough to be sure
6592 -- of giving the right answer in this case.
6594 if Nkind_In (Call, N_Function_Call, N_Procedure_Call_Statement)
6595 and then Is_Entity_Name (Name (Call))
6596 and then Present (Entity (Name (Call)))
6597 and then Is_Overloadable (Entity (Name (Call)))
6598 and then not Is_Overloaded (Name (Call))
6599 then
6600 -- If node is name in call it is not an actual
6602 if N = Name (Call) then
6603 Call := Empty;
6604 Formal := Empty;
6605 return;
6606 end if;
6608 -- Fall here if we are definitely a parameter
6610 Actual := First_Actual (Call);
6611 Formal := First_Formal (Entity (Name (Call)));
6612 while Present (Formal) and then Present (Actual) loop
6613 if Actual = N then
6614 return;
6616 -- An actual that is the prefix in a prefixed call may have
6617 -- been rewritten in the call, after the deferred reference
6618 -- was collected. Check if sloc and kinds and names match.
6620 elsif Sloc (Actual) = Sloc (N)
6621 and then Nkind (Actual) = N_Identifier
6622 and then Nkind (Actual) = Nkind (N)
6623 and then Chars (Actual) = Chars (N)
6624 then
6625 return;
6627 else
6628 Actual := Next_Actual (Actual);
6629 Formal := Next_Formal (Formal);
6630 end if;
6631 end loop;
6632 end if;
6634 -- Fall through here if we did not find matching actual
6636 Formal := Empty;
6637 Call := Empty;
6638 end Find_Actual;
6640 ---------------------------
6641 -- Find_Body_Discriminal --
6642 ---------------------------
6644 function Find_Body_Discriminal
6645 (Spec_Discriminant : Entity_Id) return Entity_Id
6647 Tsk : Entity_Id;
6648 Disc : Entity_Id;
6650 begin
6651 -- If expansion is suppressed, then the scope can be the concurrent type
6652 -- itself rather than a corresponding concurrent record type.
6654 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6655 Tsk := Scope (Spec_Discriminant);
6657 else
6658 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6660 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6661 end if;
6663 -- Find discriminant of original concurrent type, and use its current
6664 -- discriminal, which is the renaming within the task/protected body.
6666 Disc := First_Discriminant (Tsk);
6667 while Present (Disc) loop
6668 if Chars (Disc) = Chars (Spec_Discriminant) then
6669 return Discriminal (Disc);
6670 end if;
6672 Next_Discriminant (Disc);
6673 end loop;
6675 -- That loop should always succeed in finding a matching entry and
6676 -- returning. Fatal error if not.
6678 raise Program_Error;
6679 end Find_Body_Discriminal;
6681 -------------------------------------
6682 -- Find_Corresponding_Discriminant --
6683 -------------------------------------
6685 function Find_Corresponding_Discriminant
6686 (Id : Node_Id;
6687 Typ : Entity_Id) return Entity_Id
6689 Par_Disc : Entity_Id;
6690 Old_Disc : Entity_Id;
6691 New_Disc : Entity_Id;
6693 begin
6694 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6696 -- The original type may currently be private, and the discriminant
6697 -- only appear on its full view.
6699 if Is_Private_Type (Scope (Par_Disc))
6700 and then not Has_Discriminants (Scope (Par_Disc))
6701 and then Present (Full_View (Scope (Par_Disc)))
6702 then
6703 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6704 else
6705 Old_Disc := First_Discriminant (Scope (Par_Disc));
6706 end if;
6708 if Is_Class_Wide_Type (Typ) then
6709 New_Disc := First_Discriminant (Root_Type (Typ));
6710 else
6711 New_Disc := First_Discriminant (Typ);
6712 end if;
6714 while Present (Old_Disc) and then Present (New_Disc) loop
6715 if Old_Disc = Par_Disc then
6716 return New_Disc;
6717 end if;
6719 Next_Discriminant (Old_Disc);
6720 Next_Discriminant (New_Disc);
6721 end loop;
6723 -- Should always find it
6725 raise Program_Error;
6726 end Find_Corresponding_Discriminant;
6728 ----------------------------------
6729 -- Find_Enclosing_Iterator_Loop --
6730 ----------------------------------
6732 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
6733 Constr : Node_Id;
6734 S : Entity_Id;
6736 begin
6737 -- Traverse the scope chain looking for an iterator loop. Such loops are
6738 -- usually transformed into blocks, hence the use of Original_Node.
6740 S := Id;
6741 while Present (S) and then S /= Standard_Standard loop
6742 if Ekind (S) = E_Loop
6743 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
6744 then
6745 Constr := Original_Node (Label_Construct (Parent (S)));
6747 if Nkind (Constr) = N_Loop_Statement
6748 and then Present (Iteration_Scheme (Constr))
6749 and then Nkind (Iterator_Specification
6750 (Iteration_Scheme (Constr))) =
6751 N_Iterator_Specification
6752 then
6753 return S;
6754 end if;
6755 end if;
6757 S := Scope (S);
6758 end loop;
6760 return Empty;
6761 end Find_Enclosing_Iterator_Loop;
6763 ------------------------------------
6764 -- Find_Loop_In_Conditional_Block --
6765 ------------------------------------
6767 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
6768 Stmt : Node_Id;
6770 begin
6771 Stmt := N;
6773 if Nkind (Stmt) = N_If_Statement then
6774 Stmt := First (Then_Statements (Stmt));
6775 end if;
6777 pragma Assert (Nkind (Stmt) = N_Block_Statement);
6779 -- Inspect the statements of the conditional block. In general the loop
6780 -- should be the first statement in the statement sequence of the block,
6781 -- but the finalization machinery may have introduced extra object
6782 -- declarations.
6784 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
6785 while Present (Stmt) loop
6786 if Nkind (Stmt) = N_Loop_Statement then
6787 return Stmt;
6788 end if;
6790 Next (Stmt);
6791 end loop;
6793 -- The expansion of attribute 'Loop_Entry produced a malformed block
6795 raise Program_Error;
6796 end Find_Loop_In_Conditional_Block;
6798 --------------------------
6799 -- Find_Overlaid_Entity --
6800 --------------------------
6802 procedure Find_Overlaid_Entity
6803 (N : Node_Id;
6804 Ent : out Entity_Id;
6805 Off : out Boolean)
6807 Expr : Node_Id;
6809 begin
6810 -- We are looking for one of the two following forms:
6812 -- for X'Address use Y'Address
6814 -- or
6816 -- Const : constant Address := expr;
6817 -- ...
6818 -- for X'Address use Const;
6820 -- In the second case, the expr is either Y'Address, or recursively a
6821 -- constant that eventually references Y'Address.
6823 Ent := Empty;
6824 Off := False;
6826 if Nkind (N) = N_Attribute_Definition_Clause
6827 and then Chars (N) = Name_Address
6828 then
6829 Expr := Expression (N);
6831 -- This loop checks the form of the expression for Y'Address,
6832 -- using recursion to deal with intermediate constants.
6834 loop
6835 -- Check for Y'Address
6837 if Nkind (Expr) = N_Attribute_Reference
6838 and then Attribute_Name (Expr) = Name_Address
6839 then
6840 Expr := Prefix (Expr);
6841 exit;
6843 -- Check for Const where Const is a constant entity
6845 elsif Is_Entity_Name (Expr)
6846 and then Ekind (Entity (Expr)) = E_Constant
6847 then
6848 Expr := Constant_Value (Entity (Expr));
6850 -- Anything else does not need checking
6852 else
6853 return;
6854 end if;
6855 end loop;
6857 -- This loop checks the form of the prefix for an entity, using
6858 -- recursion to deal with intermediate components.
6860 loop
6861 -- Check for Y where Y is an entity
6863 if Is_Entity_Name (Expr) then
6864 Ent := Entity (Expr);
6865 return;
6867 -- Check for components
6869 elsif
6870 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
6871 then
6872 Expr := Prefix (Expr);
6873 Off := True;
6875 -- Anything else does not need checking
6877 else
6878 return;
6879 end if;
6880 end loop;
6881 end if;
6882 end Find_Overlaid_Entity;
6884 -------------------------
6885 -- Find_Parameter_Type --
6886 -------------------------
6888 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
6889 begin
6890 if Nkind (Param) /= N_Parameter_Specification then
6891 return Empty;
6893 -- For an access parameter, obtain the type from the formal entity
6894 -- itself, because access to subprogram nodes do not carry a type.
6895 -- Shouldn't we always use the formal entity ???
6897 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
6898 return Etype (Defining_Identifier (Param));
6900 else
6901 return Etype (Parameter_Type (Param));
6902 end if;
6903 end Find_Parameter_Type;
6905 -----------------------------------
6906 -- Find_Placement_In_State_Space --
6907 -----------------------------------
6909 procedure Find_Placement_In_State_Space
6910 (Item_Id : Entity_Id;
6911 Placement : out State_Space_Kind;
6912 Pack_Id : out Entity_Id)
6914 Context : Entity_Id;
6916 begin
6917 -- Assume that the item does not appear in the state space of a package
6919 Placement := Not_In_Package;
6920 Pack_Id := Empty;
6922 -- Climb the scope stack and examine the enclosing context
6924 Context := Scope (Item_Id);
6925 while Present (Context) and then Context /= Standard_Standard loop
6926 if Ekind (Context) = E_Package then
6927 Pack_Id := Context;
6929 -- A package body is a cut off point for the traversal as the item
6930 -- cannot be visible to the outside from this point on. Note that
6931 -- this test must be done first as a body is also classified as a
6932 -- private part.
6934 if In_Package_Body (Context) then
6935 Placement := Body_State_Space;
6936 return;
6938 -- The private part of a package is a cut off point for the
6939 -- traversal as the item cannot be visible to the outside from
6940 -- this point on.
6942 elsif In_Private_Part (Context) then
6943 Placement := Private_State_Space;
6944 return;
6946 -- When the item appears in the visible state space of a package,
6947 -- continue to climb the scope stack as this may not be the final
6948 -- state space.
6950 else
6951 Placement := Visible_State_Space;
6953 -- The visible state space of a child unit acts as the proper
6954 -- placement of an item.
6956 if Is_Child_Unit (Context) then
6957 return;
6958 end if;
6959 end if;
6961 -- The item or its enclosing package appear in a construct that has
6962 -- no state space.
6964 else
6965 Placement := Not_In_Package;
6966 return;
6967 end if;
6969 Context := Scope (Context);
6970 end loop;
6971 end Find_Placement_In_State_Space;
6973 ------------------------
6974 -- Find_Specific_Type --
6975 ------------------------
6977 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
6978 Typ : Entity_Id := Root_Type (CW);
6980 begin
6981 if Ekind (Typ) = E_Incomplete_Type then
6982 if From_Limited_With (Typ) then
6983 Typ := Non_Limited_View (Typ);
6984 else
6985 Typ := Full_View (Typ);
6986 end if;
6987 end if;
6989 if Is_Private_Type (Typ)
6990 and then not Is_Tagged_Type (Typ)
6991 and then Present (Full_View (Typ))
6992 then
6993 return Full_View (Typ);
6994 else
6995 return Typ;
6996 end if;
6997 end Find_Specific_Type;
6999 -----------------------------
7000 -- Find_Static_Alternative --
7001 -----------------------------
7003 function Find_Static_Alternative (N : Node_Id) return Node_Id is
7004 Expr : constant Node_Id := Expression (N);
7005 Val : constant Uint := Expr_Value (Expr);
7006 Alt : Node_Id;
7007 Choice : Node_Id;
7009 begin
7010 Alt := First (Alternatives (N));
7012 Search : loop
7013 if Nkind (Alt) /= N_Pragma then
7014 Choice := First (Discrete_Choices (Alt));
7015 while Present (Choice) loop
7017 -- Others choice, always matches
7019 if Nkind (Choice) = N_Others_Choice then
7020 exit Search;
7022 -- Range, check if value is in the range
7024 elsif Nkind (Choice) = N_Range then
7025 exit Search when
7026 Val >= Expr_Value (Low_Bound (Choice))
7027 and then
7028 Val <= Expr_Value (High_Bound (Choice));
7030 -- Choice is a subtype name. Note that we know it must
7031 -- be a static subtype, since otherwise it would have
7032 -- been diagnosed as illegal.
7034 elsif Is_Entity_Name (Choice)
7035 and then Is_Type (Entity (Choice))
7036 then
7037 exit Search when Is_In_Range (Expr, Etype (Choice),
7038 Assume_Valid => False);
7040 -- Choice is a subtype indication
7042 elsif Nkind (Choice) = N_Subtype_Indication then
7043 declare
7044 C : constant Node_Id := Constraint (Choice);
7045 R : constant Node_Id := Range_Expression (C);
7047 begin
7048 exit Search when
7049 Val >= Expr_Value (Low_Bound (R))
7050 and then
7051 Val <= Expr_Value (High_Bound (R));
7052 end;
7054 -- Choice is a simple expression
7056 else
7057 exit Search when Val = Expr_Value (Choice);
7058 end if;
7060 Next (Choice);
7061 end loop;
7062 end if;
7064 Next (Alt);
7065 pragma Assert (Present (Alt));
7066 end loop Search;
7068 -- The above loop *must* terminate by finding a match, since
7069 -- we know the case statement is valid, and the value of the
7070 -- expression is known at compile time. When we fall out of
7071 -- the loop, Alt points to the alternative that we know will
7072 -- be selected at run time.
7074 return Alt;
7075 end Find_Static_Alternative;
7077 ------------------
7078 -- First_Actual --
7079 ------------------
7081 function First_Actual (Node : Node_Id) return Node_Id is
7082 N : Node_Id;
7084 begin
7085 if No (Parameter_Associations (Node)) then
7086 return Empty;
7087 end if;
7089 N := First (Parameter_Associations (Node));
7091 if Nkind (N) = N_Parameter_Association then
7092 return First_Named_Actual (Node);
7093 else
7094 return N;
7095 end if;
7096 end First_Actual;
7098 -----------------------
7099 -- Gather_Components --
7100 -----------------------
7102 procedure Gather_Components
7103 (Typ : Entity_Id;
7104 Comp_List : Node_Id;
7105 Governed_By : List_Id;
7106 Into : Elist_Id;
7107 Report_Errors : out Boolean)
7109 Assoc : Node_Id;
7110 Variant : Node_Id;
7111 Discrete_Choice : Node_Id;
7112 Comp_Item : Node_Id;
7114 Discrim : Entity_Id;
7115 Discrim_Name : Node_Id;
7116 Discrim_Value : Node_Id;
7118 begin
7119 Report_Errors := False;
7121 if No (Comp_List) or else Null_Present (Comp_List) then
7122 return;
7124 elsif Present (Component_Items (Comp_List)) then
7125 Comp_Item := First (Component_Items (Comp_List));
7127 else
7128 Comp_Item := Empty;
7129 end if;
7131 while Present (Comp_Item) loop
7133 -- Skip the tag of a tagged record, the interface tags, as well
7134 -- as all items that are not user components (anonymous types,
7135 -- rep clauses, Parent field, controller field).
7137 if Nkind (Comp_Item) = N_Component_Declaration then
7138 declare
7139 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
7140 begin
7141 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
7142 Append_Elmt (Comp, Into);
7143 end if;
7144 end;
7145 end if;
7147 Next (Comp_Item);
7148 end loop;
7150 if No (Variant_Part (Comp_List)) then
7151 return;
7152 else
7153 Discrim_Name := Name (Variant_Part (Comp_List));
7154 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
7155 end if;
7157 -- Look for the discriminant that governs this variant part.
7158 -- The discriminant *must* be in the Governed_By List
7160 Assoc := First (Governed_By);
7161 Find_Constraint : loop
7162 Discrim := First (Choices (Assoc));
7163 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
7164 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
7165 and then
7166 Chars (Corresponding_Discriminant (Entity (Discrim))) =
7167 Chars (Discrim_Name))
7168 or else Chars (Original_Record_Component (Entity (Discrim)))
7169 = Chars (Discrim_Name);
7171 if No (Next (Assoc)) then
7172 if not Is_Constrained (Typ)
7173 and then Is_Derived_Type (Typ)
7174 and then Present (Stored_Constraint (Typ))
7175 then
7176 -- If the type is a tagged type with inherited discriminants,
7177 -- use the stored constraint on the parent in order to find
7178 -- the values of discriminants that are otherwise hidden by an
7179 -- explicit constraint. Renamed discriminants are handled in
7180 -- the code above.
7182 -- If several parent discriminants are renamed by a single
7183 -- discriminant of the derived type, the call to obtain the
7184 -- Corresponding_Discriminant field only retrieves the last
7185 -- of them. We recover the constraint on the others from the
7186 -- Stored_Constraint as well.
7188 declare
7189 D : Entity_Id;
7190 C : Elmt_Id;
7192 begin
7193 D := First_Discriminant (Etype (Typ));
7194 C := First_Elmt (Stored_Constraint (Typ));
7195 while Present (D) and then Present (C) loop
7196 if Chars (Discrim_Name) = Chars (D) then
7197 if Is_Entity_Name (Node (C))
7198 and then Entity (Node (C)) = Entity (Discrim)
7199 then
7200 -- D is renamed by Discrim, whose value is given in
7201 -- Assoc.
7203 null;
7205 else
7206 Assoc :=
7207 Make_Component_Association (Sloc (Typ),
7208 New_List
7209 (New_Occurrence_Of (D, Sloc (Typ))),
7210 Duplicate_Subexpr_No_Checks (Node (C)));
7211 end if;
7212 exit Find_Constraint;
7213 end if;
7215 Next_Discriminant (D);
7216 Next_Elmt (C);
7217 end loop;
7218 end;
7219 end if;
7220 end if;
7222 if No (Next (Assoc)) then
7223 Error_Msg_NE (" missing value for discriminant&",
7224 First (Governed_By), Discrim_Name);
7225 Report_Errors := True;
7226 return;
7227 end if;
7229 Next (Assoc);
7230 end loop Find_Constraint;
7232 Discrim_Value := Expression (Assoc);
7234 if not Is_OK_Static_Expression (Discrim_Value) then
7236 -- If the variant part is governed by a discriminant of the type
7237 -- this is an error. If the variant part and the discriminant are
7238 -- inherited from an ancestor this is legal (AI05-120) unless the
7239 -- components are being gathered for an aggregate, in which case
7240 -- the caller must check Report_Errors.
7242 if Scope (Original_Record_Component
7243 ((Entity (First (Choices (Assoc)))))) = Typ
7244 then
7245 Error_Msg_FE
7246 ("value for discriminant & must be static!",
7247 Discrim_Value, Discrim);
7248 Why_Not_Static (Discrim_Value);
7249 end if;
7251 Report_Errors := True;
7252 return;
7253 end if;
7255 Search_For_Discriminant_Value : declare
7256 Low : Node_Id;
7257 High : Node_Id;
7259 UI_High : Uint;
7260 UI_Low : Uint;
7261 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
7263 begin
7264 Find_Discrete_Value : while Present (Variant) loop
7265 Discrete_Choice := First (Discrete_Choices (Variant));
7266 while Present (Discrete_Choice) loop
7267 exit Find_Discrete_Value when
7268 Nkind (Discrete_Choice) = N_Others_Choice;
7270 Get_Index_Bounds (Discrete_Choice, Low, High);
7272 UI_Low := Expr_Value (Low);
7273 UI_High := Expr_Value (High);
7275 exit Find_Discrete_Value when
7276 UI_Low <= UI_Discrim_Value
7277 and then
7278 UI_High >= UI_Discrim_Value;
7280 Next (Discrete_Choice);
7281 end loop;
7283 Next_Non_Pragma (Variant);
7284 end loop Find_Discrete_Value;
7285 end Search_For_Discriminant_Value;
7287 if No (Variant) then
7288 Error_Msg_NE
7289 ("value of discriminant & is out of range", Discrim_Value, Discrim);
7290 Report_Errors := True;
7291 return;
7292 end if;
7294 -- If we have found the corresponding choice, recursively add its
7295 -- components to the Into list.
7297 Gather_Components
7298 (Empty, Component_List (Variant), Governed_By, Into, Report_Errors);
7299 end Gather_Components;
7301 ------------------------
7302 -- Get_Actual_Subtype --
7303 ------------------------
7305 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
7306 Typ : constant Entity_Id := Etype (N);
7307 Utyp : Entity_Id := Underlying_Type (Typ);
7308 Decl : Node_Id;
7309 Atyp : Entity_Id;
7311 begin
7312 if No (Utyp) then
7313 Utyp := Typ;
7314 end if;
7316 -- If what we have is an identifier that references a subprogram
7317 -- formal, or a variable or constant object, then we get the actual
7318 -- subtype from the referenced entity if one has been built.
7320 if Nkind (N) = N_Identifier
7321 and then
7322 (Is_Formal (Entity (N))
7323 or else Ekind (Entity (N)) = E_Constant
7324 or else Ekind (Entity (N)) = E_Variable)
7325 and then Present (Actual_Subtype (Entity (N)))
7326 then
7327 return Actual_Subtype (Entity (N));
7329 -- Actual subtype of unchecked union is always itself. We never need
7330 -- the "real" actual subtype. If we did, we couldn't get it anyway
7331 -- because the discriminant is not available. The restrictions on
7332 -- Unchecked_Union are designed to make sure that this is OK.
7334 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
7335 return Typ;
7337 -- Here for the unconstrained case, we must find actual subtype
7338 -- No actual subtype is available, so we must build it on the fly.
7340 -- Checking the type, not the underlying type, for constrainedness
7341 -- seems to be necessary. Maybe all the tests should be on the type???
7343 elsif (not Is_Constrained (Typ))
7344 and then (Is_Array_Type (Utyp)
7345 or else (Is_Record_Type (Utyp)
7346 and then Has_Discriminants (Utyp)))
7347 and then not Has_Unknown_Discriminants (Utyp)
7348 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
7349 then
7350 -- Nothing to do if in spec expression (why not???)
7352 if In_Spec_Expression then
7353 return Typ;
7355 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
7357 -- If the type has no discriminants, there is no subtype to
7358 -- build, even if the underlying type is discriminated.
7360 return Typ;
7362 -- Else build the actual subtype
7364 else
7365 Decl := Build_Actual_Subtype (Typ, N);
7366 Atyp := Defining_Identifier (Decl);
7368 -- If Build_Actual_Subtype generated a new declaration then use it
7370 if Atyp /= Typ then
7372 -- The actual subtype is an Itype, so analyze the declaration,
7373 -- but do not attach it to the tree, to get the type defined.
7375 Set_Parent (Decl, N);
7376 Set_Is_Itype (Atyp);
7377 Analyze (Decl, Suppress => All_Checks);
7378 Set_Associated_Node_For_Itype (Atyp, N);
7379 Set_Has_Delayed_Freeze (Atyp, False);
7381 -- We need to freeze the actual subtype immediately. This is
7382 -- needed, because otherwise this Itype will not get frozen
7383 -- at all, and it is always safe to freeze on creation because
7384 -- any associated types must be frozen at this point.
7386 Freeze_Itype (Atyp, N);
7387 return Atyp;
7389 -- Otherwise we did not build a declaration, so return original
7391 else
7392 return Typ;
7393 end if;
7394 end if;
7396 -- For all remaining cases, the actual subtype is the same as
7397 -- the nominal type.
7399 else
7400 return Typ;
7401 end if;
7402 end Get_Actual_Subtype;
7404 -------------------------------------
7405 -- Get_Actual_Subtype_If_Available --
7406 -------------------------------------
7408 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
7409 Typ : constant Entity_Id := Etype (N);
7411 begin
7412 -- If what we have is an identifier that references a subprogram
7413 -- formal, or a variable or constant object, then we get the actual
7414 -- subtype from the referenced entity if one has been built.
7416 if Nkind (N) = N_Identifier
7417 and then
7418 (Is_Formal (Entity (N))
7419 or else Ekind (Entity (N)) = E_Constant
7420 or else Ekind (Entity (N)) = E_Variable)
7421 and then Present (Actual_Subtype (Entity (N)))
7422 then
7423 return Actual_Subtype (Entity (N));
7425 -- Otherwise the Etype of N is returned unchanged
7427 else
7428 return Typ;
7429 end if;
7430 end Get_Actual_Subtype_If_Available;
7432 ------------------------
7433 -- Get_Body_From_Stub --
7434 ------------------------
7436 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
7437 begin
7438 return Proper_Body (Unit (Library_Unit (N)));
7439 end Get_Body_From_Stub;
7441 ---------------------
7442 -- Get_Cursor_Type --
7443 ---------------------
7445 function Get_Cursor_Type
7446 (Aspect : Node_Id;
7447 Typ : Entity_Id) return Entity_Id
7449 Assoc : Node_Id;
7450 Func : Entity_Id;
7451 First_Op : Entity_Id;
7452 Cursor : Entity_Id;
7454 begin
7455 -- If error already detected, return
7457 if Error_Posted (Aspect) then
7458 return Any_Type;
7459 end if;
7461 -- The cursor type for an Iterable aspect is the return type of a
7462 -- non-overloaded First primitive operation. Locate association for
7463 -- First.
7465 Assoc := First (Component_Associations (Expression (Aspect)));
7466 First_Op := Any_Id;
7467 while Present (Assoc) loop
7468 if Chars (First (Choices (Assoc))) = Name_First then
7469 First_Op := Expression (Assoc);
7470 exit;
7471 end if;
7473 Next (Assoc);
7474 end loop;
7476 if First_Op = Any_Id then
7477 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
7478 return Any_Type;
7479 end if;
7481 Cursor := Any_Type;
7483 -- Locate function with desired name and profile in scope of type
7485 Func := First_Entity (Scope (Typ));
7486 while Present (Func) loop
7487 if Chars (Func) = Chars (First_Op)
7488 and then Ekind (Func) = E_Function
7489 and then Present (First_Formal (Func))
7490 and then Etype (First_Formal (Func)) = Typ
7491 and then No (Next_Formal (First_Formal (Func)))
7492 then
7493 if Cursor /= Any_Type then
7494 Error_Msg_N
7495 ("Operation First for iterable type must be unique", Aspect);
7496 return Any_Type;
7497 else
7498 Cursor := Etype (Func);
7499 end if;
7500 end if;
7502 Next_Entity (Func);
7503 end loop;
7505 -- If not found, no way to resolve remaining primitives.
7507 if Cursor = Any_Type then
7508 Error_Msg_N
7509 ("No legal primitive operation First for Iterable type", Aspect);
7510 end if;
7512 return Cursor;
7513 end Get_Cursor_Type;
7515 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
7516 begin
7517 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
7518 end Get_Cursor_Type;
7520 -------------------------------
7521 -- Get_Default_External_Name --
7522 -------------------------------
7524 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
7525 begin
7526 Get_Decoded_Name_String (Chars (E));
7528 if Opt.External_Name_Imp_Casing = Uppercase then
7529 Set_Casing (All_Upper_Case);
7530 else
7531 Set_Casing (All_Lower_Case);
7532 end if;
7534 return
7535 Make_String_Literal (Sloc (E),
7536 Strval => String_From_Name_Buffer);
7537 end Get_Default_External_Name;
7539 --------------------------
7540 -- Get_Enclosing_Object --
7541 --------------------------
7543 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
7544 begin
7545 if Is_Entity_Name (N) then
7546 return Entity (N);
7547 else
7548 case Nkind (N) is
7549 when N_Indexed_Component |
7550 N_Slice |
7551 N_Selected_Component =>
7553 -- If not generating code, a dereference may be left implicit.
7554 -- In thoses cases, return Empty.
7556 if Is_Access_Type (Etype (Prefix (N))) then
7557 return Empty;
7558 else
7559 return Get_Enclosing_Object (Prefix (N));
7560 end if;
7562 when N_Type_Conversion =>
7563 return Get_Enclosing_Object (Expression (N));
7565 when others =>
7566 return Empty;
7567 end case;
7568 end if;
7569 end Get_Enclosing_Object;
7571 ---------------------------
7572 -- Get_Enum_Lit_From_Pos --
7573 ---------------------------
7575 function Get_Enum_Lit_From_Pos
7576 (T : Entity_Id;
7577 Pos : Uint;
7578 Loc : Source_Ptr) return Node_Id
7580 Btyp : Entity_Id := Base_Type (T);
7581 Lit : Node_Id;
7583 begin
7584 -- In the case where the literal is of type Character, Wide_Character
7585 -- or Wide_Wide_Character or of a type derived from them, there needs
7586 -- to be some special handling since there is no explicit chain of
7587 -- literals to search. Instead, an N_Character_Literal node is created
7588 -- with the appropriate Char_Code and Chars fields.
7590 if Is_Standard_Character_Type (T) then
7591 Set_Character_Literal_Name (UI_To_CC (Pos));
7592 return
7593 Make_Character_Literal (Loc,
7594 Chars => Name_Find,
7595 Char_Literal_Value => Pos);
7597 -- For all other cases, we have a complete table of literals, and
7598 -- we simply iterate through the chain of literal until the one
7599 -- with the desired position value is found.
7601 else
7602 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7603 Btyp := Full_View (Btyp);
7604 end if;
7606 Lit := First_Literal (Btyp);
7607 for J in 1 .. UI_To_Int (Pos) loop
7608 Next_Literal (Lit);
7609 end loop;
7611 return New_Occurrence_Of (Lit, Loc);
7612 end if;
7613 end Get_Enum_Lit_From_Pos;
7615 ------------------------
7616 -- Get_Generic_Entity --
7617 ------------------------
7619 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7620 Ent : constant Entity_Id := Entity (Name (N));
7621 begin
7622 if Present (Renamed_Object (Ent)) then
7623 return Renamed_Object (Ent);
7624 else
7625 return Ent;
7626 end if;
7627 end Get_Generic_Entity;
7629 -------------------------------------
7630 -- Get_Incomplete_View_Of_Ancestor --
7631 -------------------------------------
7633 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7634 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7635 Par_Scope : Entity_Id;
7636 Par_Type : Entity_Id;
7638 begin
7639 -- The incomplete view of an ancestor is only relevant for private
7640 -- derived types in child units.
7642 if not Is_Derived_Type (E)
7643 or else not Is_Child_Unit (Cur_Unit)
7644 then
7645 return Empty;
7647 else
7648 Par_Scope := Scope (Cur_Unit);
7649 if No (Par_Scope) then
7650 return Empty;
7651 end if;
7653 Par_Type := Etype (Base_Type (E));
7655 -- Traverse list of ancestor types until we find one declared in
7656 -- a parent or grandparent unit (two levels seem sufficient).
7658 while Present (Par_Type) loop
7659 if Scope (Par_Type) = Par_Scope
7660 or else Scope (Par_Type) = Scope (Par_Scope)
7661 then
7662 return Par_Type;
7664 elsif not Is_Derived_Type (Par_Type) then
7665 return Empty;
7667 else
7668 Par_Type := Etype (Base_Type (Par_Type));
7669 end if;
7670 end loop;
7672 -- If none found, there is no relevant ancestor type.
7674 return Empty;
7675 end if;
7676 end Get_Incomplete_View_Of_Ancestor;
7678 ----------------------
7679 -- Get_Index_Bounds --
7680 ----------------------
7682 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
7683 Kind : constant Node_Kind := Nkind (N);
7684 R : Node_Id;
7686 begin
7687 if Kind = N_Range then
7688 L := Low_Bound (N);
7689 H := High_Bound (N);
7691 elsif Kind = N_Subtype_Indication then
7692 R := Range_Expression (Constraint (N));
7694 if R = Error then
7695 L := Error;
7696 H := Error;
7697 return;
7699 else
7700 L := Low_Bound (Range_Expression (Constraint (N)));
7701 H := High_Bound (Range_Expression (Constraint (N)));
7702 end if;
7704 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
7705 if Error_Posted (Scalar_Range (Entity (N))) then
7706 L := Error;
7707 H := Error;
7709 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
7710 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
7712 else
7713 L := Low_Bound (Scalar_Range (Entity (N)));
7714 H := High_Bound (Scalar_Range (Entity (N)));
7715 end if;
7717 else
7718 -- N is an expression, indicating a range with one value
7720 L := N;
7721 H := N;
7722 end if;
7723 end Get_Index_Bounds;
7725 ---------------------------------
7726 -- Get_Iterable_Type_Primitive --
7727 ---------------------------------
7729 function Get_Iterable_Type_Primitive
7730 (Typ : Entity_Id;
7731 Nam : Name_Id) return Entity_Id
7733 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
7734 Assoc : Node_Id;
7736 begin
7737 if No (Funcs) then
7738 return Empty;
7740 else
7741 Assoc := First (Component_Associations (Funcs));
7742 while Present (Assoc) loop
7743 if Chars (First (Choices (Assoc))) = Nam then
7744 return Entity (Expression (Assoc));
7745 end if;
7747 Assoc := Next (Assoc);
7748 end loop;
7750 return Empty;
7751 end if;
7752 end Get_Iterable_Type_Primitive;
7754 ----------------------------------
7755 -- Get_Library_Unit_Name_string --
7756 ----------------------------------
7758 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
7759 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
7761 begin
7762 Get_Unit_Name_String (Unit_Name_Id);
7764 -- Remove seven last character (" (spec)" or " (body)")
7766 Name_Len := Name_Len - 7;
7767 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
7768 end Get_Library_Unit_Name_String;
7770 ------------------------
7771 -- Get_Name_Entity_Id --
7772 ------------------------
7774 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
7775 begin
7776 return Entity_Id (Get_Name_Table_Int (Id));
7777 end Get_Name_Entity_Id;
7779 ------------------------------
7780 -- Get_Name_From_CTC_Pragma --
7781 ------------------------------
7783 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
7784 Arg : constant Node_Id :=
7785 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
7786 begin
7787 return Strval (Expr_Value_S (Arg));
7788 end Get_Name_From_CTC_Pragma;
7790 -----------------------
7791 -- Get_Parent_Entity --
7792 -----------------------
7794 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
7795 begin
7796 if Nkind (Unit) = N_Package_Body
7797 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
7798 then
7799 return Defining_Entity
7800 (Specification (Instance_Spec (Original_Node (Unit))));
7801 elsif Nkind (Unit) = N_Package_Instantiation then
7802 return Defining_Entity (Specification (Instance_Spec (Unit)));
7803 else
7804 return Defining_Entity (Unit);
7805 end if;
7806 end Get_Parent_Entity;
7807 -------------------
7808 -- Get_Pragma_Id --
7809 -------------------
7811 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
7812 begin
7813 return Get_Pragma_Id (Pragma_Name (N));
7814 end Get_Pragma_Id;
7816 -----------------------
7817 -- Get_Reason_String --
7818 -----------------------
7820 procedure Get_Reason_String (N : Node_Id) is
7821 begin
7822 if Nkind (N) = N_String_Literal then
7823 Store_String_Chars (Strval (N));
7825 elsif Nkind (N) = N_Op_Concat then
7826 Get_Reason_String (Left_Opnd (N));
7827 Get_Reason_String (Right_Opnd (N));
7829 -- If not of required form, error
7831 else
7832 Error_Msg_N
7833 ("Reason for pragma Warnings has wrong form", N);
7834 Error_Msg_N
7835 ("\must be string literal or concatenation of string literals", N);
7836 return;
7837 end if;
7838 end Get_Reason_String;
7840 ---------------------------
7841 -- Get_Referenced_Object --
7842 ---------------------------
7844 function Get_Referenced_Object (N : Node_Id) return Node_Id is
7845 R : Node_Id;
7847 begin
7848 R := N;
7849 while Is_Entity_Name (R)
7850 and then Present (Renamed_Object (Entity (R)))
7851 loop
7852 R := Renamed_Object (Entity (R));
7853 end loop;
7855 return R;
7856 end Get_Referenced_Object;
7858 ------------------------
7859 -- Get_Renamed_Entity --
7860 ------------------------
7862 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
7863 R : Entity_Id;
7865 begin
7866 R := E;
7867 while Present (Renamed_Entity (R)) loop
7868 R := Renamed_Entity (R);
7869 end loop;
7871 return R;
7872 end Get_Renamed_Entity;
7874 -----------------------
7875 -- Get_Return_Object --
7876 -----------------------
7878 function Get_Return_Object (N : Node_Id) return Entity_Id is
7879 Decl : Node_Id;
7881 begin
7882 Decl := First (Return_Object_Declarations (N));
7883 while Present (Decl) loop
7884 exit when Nkind (Decl) = N_Object_Declaration
7885 and then Is_Return_Object (Defining_Identifier (Decl));
7886 Next (Decl);
7887 end loop;
7889 pragma Assert (Present (Decl));
7890 return Defining_Identifier (Decl);
7891 end Get_Return_Object;
7893 ---------------------------
7894 -- Get_Subprogram_Entity --
7895 ---------------------------
7897 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
7898 Subp : Node_Id;
7899 Subp_Id : Entity_Id;
7901 begin
7902 if Nkind (Nod) = N_Accept_Statement then
7903 Subp := Entry_Direct_Name (Nod);
7905 elsif Nkind (Nod) = N_Slice then
7906 Subp := Prefix (Nod);
7908 else
7909 Subp := Name (Nod);
7910 end if;
7912 -- Strip the subprogram call
7914 loop
7915 if Nkind_In (Subp, N_Explicit_Dereference,
7916 N_Indexed_Component,
7917 N_Selected_Component)
7918 then
7919 Subp := Prefix (Subp);
7921 elsif Nkind_In (Subp, N_Type_Conversion,
7922 N_Unchecked_Type_Conversion)
7923 then
7924 Subp := Expression (Subp);
7926 else
7927 exit;
7928 end if;
7929 end loop;
7931 -- Extract the entity of the subprogram call
7933 if Is_Entity_Name (Subp) then
7934 Subp_Id := Entity (Subp);
7936 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
7937 Subp_Id := Directly_Designated_Type (Subp_Id);
7938 end if;
7940 if Is_Subprogram (Subp_Id) then
7941 return Subp_Id;
7942 else
7943 return Empty;
7944 end if;
7946 -- The search did not find a construct that denotes a subprogram
7948 else
7949 return Empty;
7950 end if;
7951 end Get_Subprogram_Entity;
7953 -----------------------------
7954 -- Get_Task_Body_Procedure --
7955 -----------------------------
7957 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
7958 begin
7959 -- Note: A task type may be the completion of a private type with
7960 -- discriminants. When performing elaboration checks on a task
7961 -- declaration, the current view of the type may be the private one,
7962 -- and the procedure that holds the body of the task is held in its
7963 -- underlying type.
7965 -- This is an odd function, why not have Task_Body_Procedure do
7966 -- the following digging???
7968 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
7969 end Get_Task_Body_Procedure;
7971 -------------------------
7972 -- Get_User_Defined_Eq --
7973 -------------------------
7975 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
7976 Prim : Elmt_Id;
7977 Op : Entity_Id;
7979 begin
7980 Prim := First_Elmt (Collect_Primitive_Operations (E));
7981 while Present (Prim) loop
7982 Op := Node (Prim);
7984 if Chars (Op) = Name_Op_Eq
7985 and then Etype (Op) = Standard_Boolean
7986 and then Etype (First_Formal (Op)) = E
7987 and then Etype (Next_Formal (First_Formal (Op))) = E
7988 then
7989 return Op;
7990 end if;
7992 Next_Elmt (Prim);
7993 end loop;
7995 return Empty;
7996 end Get_User_Defined_Eq;
7998 -----------------------
7999 -- Has_Access_Values --
8000 -----------------------
8002 function Has_Access_Values (T : Entity_Id) return Boolean is
8003 Typ : constant Entity_Id := Underlying_Type (T);
8005 begin
8006 -- Case of a private type which is not completed yet. This can only
8007 -- happen in the case of a generic format type appearing directly, or
8008 -- as a component of the type to which this function is being applied
8009 -- at the top level. Return False in this case, since we certainly do
8010 -- not know that the type contains access types.
8012 if No (Typ) then
8013 return False;
8015 elsif Is_Access_Type (Typ) then
8016 return True;
8018 elsif Is_Array_Type (Typ) then
8019 return Has_Access_Values (Component_Type (Typ));
8021 elsif Is_Record_Type (Typ) then
8022 declare
8023 Comp : Entity_Id;
8025 begin
8026 -- Loop to Check components
8028 Comp := First_Component_Or_Discriminant (Typ);
8029 while Present (Comp) loop
8031 -- Check for access component, tag field does not count, even
8032 -- though it is implemented internally using an access type.
8034 if Has_Access_Values (Etype (Comp))
8035 and then Chars (Comp) /= Name_uTag
8036 then
8037 return True;
8038 end if;
8040 Next_Component_Or_Discriminant (Comp);
8041 end loop;
8042 end;
8044 return False;
8046 else
8047 return False;
8048 end if;
8049 end Has_Access_Values;
8051 ------------------------------
8052 -- Has_Compatible_Alignment --
8053 ------------------------------
8055 function Has_Compatible_Alignment
8056 (Obj : Entity_Id;
8057 Expr : Node_Id) return Alignment_Result
8059 function Has_Compatible_Alignment_Internal
8060 (Obj : Entity_Id;
8061 Expr : Node_Id;
8062 Default : Alignment_Result) return Alignment_Result;
8063 -- This is the internal recursive function that actually does the work.
8064 -- There is one additional parameter, which says what the result should
8065 -- be if no alignment information is found, and there is no definite
8066 -- indication of compatible alignments. At the outer level, this is set
8067 -- to Unknown, but for internal recursive calls in the case where types
8068 -- are known to be correct, it is set to Known_Compatible.
8070 ---------------------------------------
8071 -- Has_Compatible_Alignment_Internal --
8072 ---------------------------------------
8074 function Has_Compatible_Alignment_Internal
8075 (Obj : Entity_Id;
8076 Expr : Node_Id;
8077 Default : Alignment_Result) return Alignment_Result
8079 Result : Alignment_Result := Known_Compatible;
8080 -- Holds the current status of the result. Note that once a value of
8081 -- Known_Incompatible is set, it is sticky and does not get changed
8082 -- to Unknown (the value in Result only gets worse as we go along,
8083 -- never better).
8085 Offs : Uint := No_Uint;
8086 -- Set to a factor of the offset from the base object when Expr is a
8087 -- selected or indexed component, based on Component_Bit_Offset and
8088 -- Component_Size respectively. A negative value is used to represent
8089 -- a value which is not known at compile time.
8091 procedure Check_Prefix;
8092 -- Checks the prefix recursively in the case where the expression
8093 -- is an indexed or selected component.
8095 procedure Set_Result (R : Alignment_Result);
8096 -- If R represents a worse outcome (unknown instead of known
8097 -- compatible, or known incompatible), then set Result to R.
8099 ------------------
8100 -- Check_Prefix --
8101 ------------------
8103 procedure Check_Prefix is
8104 begin
8105 -- The subtlety here is that in doing a recursive call to check
8106 -- the prefix, we have to decide what to do in the case where we
8107 -- don't find any specific indication of an alignment problem.
8109 -- At the outer level, we normally set Unknown as the result in
8110 -- this case, since we can only set Known_Compatible if we really
8111 -- know that the alignment value is OK, but for the recursive
8112 -- call, in the case where the types match, and we have not
8113 -- specified a peculiar alignment for the object, we are only
8114 -- concerned about suspicious rep clauses, the default case does
8115 -- not affect us, since the compiler will, in the absence of such
8116 -- rep clauses, ensure that the alignment is correct.
8118 if Default = Known_Compatible
8119 or else
8120 (Etype (Obj) = Etype (Expr)
8121 and then (Unknown_Alignment (Obj)
8122 or else
8123 Alignment (Obj) = Alignment (Etype (Obj))))
8124 then
8125 Set_Result
8126 (Has_Compatible_Alignment_Internal
8127 (Obj, Prefix (Expr), Known_Compatible));
8129 -- In all other cases, we need a full check on the prefix
8131 else
8132 Set_Result
8133 (Has_Compatible_Alignment_Internal
8134 (Obj, Prefix (Expr), Unknown));
8135 end if;
8136 end Check_Prefix;
8138 ----------------
8139 -- Set_Result --
8140 ----------------
8142 procedure Set_Result (R : Alignment_Result) is
8143 begin
8144 if R > Result then
8145 Result := R;
8146 end if;
8147 end Set_Result;
8149 -- Start of processing for Has_Compatible_Alignment_Internal
8151 begin
8152 -- If Expr is a selected component, we must make sure there is no
8153 -- potentially troublesome component clause, and that the record is
8154 -- not packed.
8156 if Nkind (Expr) = N_Selected_Component then
8158 -- Packed record always generate unknown alignment
8160 if Is_Packed (Etype (Prefix (Expr))) then
8161 Set_Result (Unknown);
8162 end if;
8164 -- Check prefix and component offset
8166 Check_Prefix;
8167 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
8169 -- If Expr is an indexed component, we must make sure there is no
8170 -- potentially troublesome Component_Size clause and that the array
8171 -- is not bit-packed.
8173 elsif Nkind (Expr) = N_Indexed_Component then
8174 declare
8175 Typ : constant Entity_Id := Etype (Prefix (Expr));
8176 Ind : constant Node_Id := First_Index (Typ);
8178 begin
8179 -- Bit packed array always generates unknown alignment
8181 if Is_Bit_Packed_Array (Typ) then
8182 Set_Result (Unknown);
8183 end if;
8185 -- Check prefix and component offset
8187 Check_Prefix;
8188 Offs := Component_Size (Typ);
8190 -- Small optimization: compute the full offset when possible
8192 if Offs /= No_Uint
8193 and then Offs > Uint_0
8194 and then Present (Ind)
8195 and then Nkind (Ind) = N_Range
8196 and then Compile_Time_Known_Value (Low_Bound (Ind))
8197 and then Compile_Time_Known_Value (First (Expressions (Expr)))
8198 then
8199 Offs := Offs * (Expr_Value (First (Expressions (Expr)))
8200 - Expr_Value (Low_Bound ((Ind))));
8201 end if;
8202 end;
8203 end if;
8205 -- If we have a null offset, the result is entirely determined by
8206 -- the base object and has already been computed recursively.
8208 if Offs = Uint_0 then
8209 null;
8211 -- Case where we know the alignment of the object
8213 elsif Known_Alignment (Obj) then
8214 declare
8215 ObjA : constant Uint := Alignment (Obj);
8216 ExpA : Uint := No_Uint;
8217 SizA : Uint := No_Uint;
8219 begin
8220 -- If alignment of Obj is 1, then we are always OK
8222 if ObjA = 1 then
8223 Set_Result (Known_Compatible);
8225 -- Alignment of Obj is greater than 1, so we need to check
8227 else
8228 -- If we have an offset, see if it is compatible
8230 if Offs /= No_Uint and Offs > Uint_0 then
8231 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
8232 Set_Result (Known_Incompatible);
8233 end if;
8235 -- See if Expr is an object with known alignment
8237 elsif Is_Entity_Name (Expr)
8238 and then Known_Alignment (Entity (Expr))
8239 then
8240 ExpA := Alignment (Entity (Expr));
8242 -- Otherwise, we can use the alignment of the type of
8243 -- Expr given that we already checked for
8244 -- discombobulating rep clauses for the cases of indexed
8245 -- and selected components above.
8247 elsif Known_Alignment (Etype (Expr)) then
8248 ExpA := Alignment (Etype (Expr));
8250 -- Otherwise the alignment is unknown
8252 else
8253 Set_Result (Default);
8254 end if;
8256 -- If we got an alignment, see if it is acceptable
8258 if ExpA /= No_Uint and then ExpA < ObjA then
8259 Set_Result (Known_Incompatible);
8260 end if;
8262 -- If Expr is not a piece of a larger object, see if size
8263 -- is given. If so, check that it is not too small for the
8264 -- required alignment.
8266 if Offs /= No_Uint then
8267 null;
8269 -- See if Expr is an object with known size
8271 elsif Is_Entity_Name (Expr)
8272 and then Known_Static_Esize (Entity (Expr))
8273 then
8274 SizA := Esize (Entity (Expr));
8276 -- Otherwise, we check the object size of the Expr type
8278 elsif Known_Static_Esize (Etype (Expr)) then
8279 SizA := Esize (Etype (Expr));
8280 end if;
8282 -- If we got a size, see if it is a multiple of the Obj
8283 -- alignment, if not, then the alignment cannot be
8284 -- acceptable, since the size is always a multiple of the
8285 -- alignment.
8287 if SizA /= No_Uint then
8288 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
8289 Set_Result (Known_Incompatible);
8290 end if;
8291 end if;
8292 end if;
8293 end;
8295 -- If we do not know required alignment, any non-zero offset is a
8296 -- potential problem (but certainly may be OK, so result is unknown).
8298 elsif Offs /= No_Uint then
8299 Set_Result (Unknown);
8301 -- If we can't find the result by direct comparison of alignment
8302 -- values, then there is still one case that we can determine known
8303 -- result, and that is when we can determine that the types are the
8304 -- same, and no alignments are specified. Then we known that the
8305 -- alignments are compatible, even if we don't know the alignment
8306 -- value in the front end.
8308 elsif Etype (Obj) = Etype (Expr) then
8310 -- Types are the same, but we have to check for possible size
8311 -- and alignments on the Expr object that may make the alignment
8312 -- different, even though the types are the same.
8314 if Is_Entity_Name (Expr) then
8316 -- First check alignment of the Expr object. Any alignment less
8317 -- than Maximum_Alignment is worrisome since this is the case
8318 -- where we do not know the alignment of Obj.
8320 if Known_Alignment (Entity (Expr))
8321 and then UI_To_Int (Alignment (Entity (Expr))) <
8322 Ttypes.Maximum_Alignment
8323 then
8324 Set_Result (Unknown);
8326 -- Now check size of Expr object. Any size that is not an
8327 -- even multiple of Maximum_Alignment is also worrisome
8328 -- since it may cause the alignment of the object to be less
8329 -- than the alignment of the type.
8331 elsif Known_Static_Esize (Entity (Expr))
8332 and then
8333 (UI_To_Int (Esize (Entity (Expr))) mod
8334 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
8335 /= 0
8336 then
8337 Set_Result (Unknown);
8339 -- Otherwise same type is decisive
8341 else
8342 Set_Result (Known_Compatible);
8343 end if;
8344 end if;
8346 -- Another case to deal with is when there is an explicit size or
8347 -- alignment clause when the types are not the same. If so, then the
8348 -- result is Unknown. We don't need to do this test if the Default is
8349 -- Unknown, since that result will be set in any case.
8351 elsif Default /= Unknown
8352 and then (Has_Size_Clause (Etype (Expr))
8353 or else
8354 Has_Alignment_Clause (Etype (Expr)))
8355 then
8356 Set_Result (Unknown);
8358 -- If no indication found, set default
8360 else
8361 Set_Result (Default);
8362 end if;
8364 -- Return worst result found
8366 return Result;
8367 end Has_Compatible_Alignment_Internal;
8369 -- Start of processing for Has_Compatible_Alignment
8371 begin
8372 -- If Obj has no specified alignment, then set alignment from the type
8373 -- alignment. Perhaps we should always do this, but for sure we should
8374 -- do it when there is an address clause since we can do more if the
8375 -- alignment is known.
8377 if Unknown_Alignment (Obj) then
8378 Set_Alignment (Obj, Alignment (Etype (Obj)));
8379 end if;
8381 -- Now do the internal call that does all the work
8383 return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
8384 end Has_Compatible_Alignment;
8386 ----------------------
8387 -- Has_Declarations --
8388 ----------------------
8390 function Has_Declarations (N : Node_Id) return Boolean is
8391 begin
8392 return Nkind_In (Nkind (N), N_Accept_Statement,
8393 N_Block_Statement,
8394 N_Compilation_Unit_Aux,
8395 N_Entry_Body,
8396 N_Package_Body,
8397 N_Protected_Body,
8398 N_Subprogram_Body,
8399 N_Task_Body,
8400 N_Package_Specification);
8401 end Has_Declarations;
8403 ---------------------------------
8404 -- Has_Defaulted_Discriminants --
8405 ---------------------------------
8407 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
8408 begin
8409 return Has_Discriminants (Typ)
8410 and then Present (First_Discriminant (Typ))
8411 and then Present (Discriminant_Default_Value
8412 (First_Discriminant (Typ)));
8413 end Has_Defaulted_Discriminants;
8415 -------------------
8416 -- Has_Denormals --
8417 -------------------
8419 function Has_Denormals (E : Entity_Id) return Boolean is
8420 begin
8421 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
8422 end Has_Denormals;
8424 -------------------------------------------
8425 -- Has_Discriminant_Dependent_Constraint --
8426 -------------------------------------------
8428 function Has_Discriminant_Dependent_Constraint
8429 (Comp : Entity_Id) return Boolean
8431 Comp_Decl : constant Node_Id := Parent (Comp);
8432 Subt_Indic : Node_Id;
8433 Constr : Node_Id;
8434 Assn : Node_Id;
8436 begin
8437 -- Discriminants can't depend on discriminants
8439 if Ekind (Comp) = E_Discriminant then
8440 return False;
8442 else
8443 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
8445 if Nkind (Subt_Indic) = N_Subtype_Indication then
8446 Constr := Constraint (Subt_Indic);
8448 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
8449 Assn := First (Constraints (Constr));
8450 while Present (Assn) loop
8451 case Nkind (Assn) is
8452 when N_Subtype_Indication |
8453 N_Range |
8454 N_Identifier
8456 if Depends_On_Discriminant (Assn) then
8457 return True;
8458 end if;
8460 when N_Discriminant_Association =>
8461 if Depends_On_Discriminant (Expression (Assn)) then
8462 return True;
8463 end if;
8465 when others =>
8466 null;
8467 end case;
8469 Next (Assn);
8470 end loop;
8471 end if;
8472 end if;
8473 end if;
8475 return False;
8476 end Has_Discriminant_Dependent_Constraint;
8478 --------------------------
8479 -- Has_Enabled_Property --
8480 --------------------------
8482 function Has_Enabled_Property
8483 (Item_Id : Entity_Id;
8484 Property : Name_Id) return Boolean
8486 function State_Has_Enabled_Property return Boolean;
8487 -- Determine whether a state denoted by Item_Id has the property enabled
8489 function Variable_Has_Enabled_Property return Boolean;
8490 -- Determine whether a variable denoted by Item_Id has the property
8491 -- enabled.
8493 --------------------------------
8494 -- State_Has_Enabled_Property --
8495 --------------------------------
8497 function State_Has_Enabled_Property return Boolean is
8498 Decl : constant Node_Id := Parent (Item_Id);
8499 Opt : Node_Id;
8500 Opt_Nam : Node_Id;
8501 Prop : Node_Id;
8502 Prop_Nam : Node_Id;
8503 Props : Node_Id;
8505 begin
8506 -- The declaration of an external abstract state appears as an
8507 -- extension aggregate. If this is not the case, properties can never
8508 -- be set.
8510 if Nkind (Decl) /= N_Extension_Aggregate then
8511 return False;
8512 end if;
8514 -- When External appears as a simple option, it automatically enables
8515 -- all properties.
8517 Opt := First (Expressions (Decl));
8518 while Present (Opt) loop
8519 if Nkind (Opt) = N_Identifier
8520 and then Chars (Opt) = Name_External
8521 then
8522 return True;
8523 end if;
8525 Next (Opt);
8526 end loop;
8528 -- When External specifies particular properties, inspect those and
8529 -- find the desired one (if any).
8531 Opt := First (Component_Associations (Decl));
8532 while Present (Opt) loop
8533 Opt_Nam := First (Choices (Opt));
8535 if Nkind (Opt_Nam) = N_Identifier
8536 and then Chars (Opt_Nam) = Name_External
8537 then
8538 Props := Expression (Opt);
8540 -- Multiple properties appear as an aggregate
8542 if Nkind (Props) = N_Aggregate then
8544 -- Simple property form
8546 Prop := First (Expressions (Props));
8547 while Present (Prop) loop
8548 if Chars (Prop) = Property then
8549 return True;
8550 end if;
8552 Next (Prop);
8553 end loop;
8555 -- Property with expression form
8557 Prop := First (Component_Associations (Props));
8558 while Present (Prop) loop
8559 Prop_Nam := First (Choices (Prop));
8561 -- The property can be represented in two ways:
8562 -- others => <value>
8563 -- <property> => <value>
8565 if Nkind (Prop_Nam) = N_Others_Choice
8566 or else (Nkind (Prop_Nam) = N_Identifier
8567 and then Chars (Prop_Nam) = Property)
8568 then
8569 return Is_True (Expr_Value (Expression (Prop)));
8570 end if;
8572 Next (Prop);
8573 end loop;
8575 -- Single property
8577 else
8578 return Chars (Props) = Property;
8579 end if;
8580 end if;
8582 Next (Opt);
8583 end loop;
8585 return False;
8586 end State_Has_Enabled_Property;
8588 -----------------------------------
8589 -- Variable_Has_Enabled_Property --
8590 -----------------------------------
8592 function Variable_Has_Enabled_Property return Boolean is
8593 function Is_Enabled (Prag : Node_Id) return Boolean;
8594 -- Determine whether property pragma Prag (if present) denotes an
8595 -- enabled property.
8597 ----------------
8598 -- Is_Enabled --
8599 ----------------
8601 function Is_Enabled (Prag : Node_Id) return Boolean is
8602 Arg2 : Node_Id;
8604 begin
8605 if Present (Prag) then
8606 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
8608 -- The pragma has an optional Boolean expression, the related
8609 -- property is enabled only when the expression evaluates to
8610 -- True.
8612 if Present (Arg2) then
8613 return Is_True (Expr_Value (Get_Pragma_Arg (Arg2)));
8615 -- Otherwise the lack of expression enables the property by
8616 -- default.
8618 else
8619 return True;
8620 end if;
8622 -- The property was never set in the first place
8624 else
8625 return False;
8626 end if;
8627 end Is_Enabled;
8629 -- Local variables
8631 AR : constant Node_Id :=
8632 Get_Pragma (Item_Id, Pragma_Async_Readers);
8633 AW : constant Node_Id :=
8634 Get_Pragma (Item_Id, Pragma_Async_Writers);
8635 ER : constant Node_Id :=
8636 Get_Pragma (Item_Id, Pragma_Effective_Reads);
8637 EW : constant Node_Id :=
8638 Get_Pragma (Item_Id, Pragma_Effective_Writes);
8640 -- Start of processing for Variable_Has_Enabled_Property
8642 begin
8643 -- A non-effectively volatile object can never possess external
8644 -- properties.
8646 if not Is_Effectively_Volatile (Item_Id) then
8647 return False;
8649 -- External properties related to variables come in two flavors -
8650 -- explicit and implicit. The explicit case is characterized by the
8651 -- presence of a property pragma with an optional Boolean flag. The
8652 -- property is enabled when the flag evaluates to True or the flag is
8653 -- missing altogether.
8655 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
8656 return True;
8658 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
8659 return True;
8661 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
8662 return True;
8664 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
8665 return True;
8667 -- The implicit case lacks all property pragmas
8669 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
8670 return True;
8672 else
8673 return False;
8674 end if;
8675 end Variable_Has_Enabled_Property;
8677 -- Start of processing for Has_Enabled_Property
8679 begin
8680 -- Abstract states and variables have a flexible scheme of specifying
8681 -- external properties.
8683 if Ekind (Item_Id) = E_Abstract_State then
8684 return State_Has_Enabled_Property;
8686 elsif Ekind (Item_Id) = E_Variable then
8687 return Variable_Has_Enabled_Property;
8689 -- Otherwise a property is enabled when the related item is effectively
8690 -- volatile.
8692 else
8693 return Is_Effectively_Volatile (Item_Id);
8694 end if;
8695 end Has_Enabled_Property;
8697 --------------------
8698 -- Has_Infinities --
8699 --------------------
8701 function Has_Infinities (E : Entity_Id) return Boolean is
8702 begin
8703 return
8704 Is_Floating_Point_Type (E)
8705 and then Nkind (Scalar_Range (E)) = N_Range
8706 and then Includes_Infinities (Scalar_Range (E));
8707 end Has_Infinities;
8709 --------------------
8710 -- Has_Interfaces --
8711 --------------------
8713 function Has_Interfaces
8714 (T : Entity_Id;
8715 Use_Full_View : Boolean := True) return Boolean
8717 Typ : Entity_Id := Base_Type (T);
8719 begin
8720 -- Handle concurrent types
8722 if Is_Concurrent_Type (Typ) then
8723 Typ := Corresponding_Record_Type (Typ);
8724 end if;
8726 if not Present (Typ)
8727 or else not Is_Record_Type (Typ)
8728 or else not Is_Tagged_Type (Typ)
8729 then
8730 return False;
8731 end if;
8733 -- Handle private types
8735 if Use_Full_View and then Present (Full_View (Typ)) then
8736 Typ := Full_View (Typ);
8737 end if;
8739 -- Handle concurrent record types
8741 if Is_Concurrent_Record_Type (Typ)
8742 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
8743 then
8744 return True;
8745 end if;
8747 loop
8748 if Is_Interface (Typ)
8749 or else
8750 (Is_Record_Type (Typ)
8751 and then Present (Interfaces (Typ))
8752 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
8753 then
8754 return True;
8755 end if;
8757 exit when Etype (Typ) = Typ
8759 -- Handle private types
8761 or else (Present (Full_View (Etype (Typ)))
8762 and then Full_View (Etype (Typ)) = Typ)
8764 -- Protect frontend against wrong sources with cyclic derivations
8766 or else Etype (Typ) = T;
8768 -- Climb to the ancestor type handling private types
8770 if Present (Full_View (Etype (Typ))) then
8771 Typ := Full_View (Etype (Typ));
8772 else
8773 Typ := Etype (Typ);
8774 end if;
8775 end loop;
8777 return False;
8778 end Has_Interfaces;
8780 ---------------------------------
8781 -- Has_No_Obvious_Side_Effects --
8782 ---------------------------------
8784 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
8785 begin
8786 -- For now, just handle literals, constants, and non-volatile
8787 -- variables and expressions combining these with operators or
8788 -- short circuit forms.
8790 if Nkind (N) in N_Numeric_Or_String_Literal then
8791 return True;
8793 elsif Nkind (N) = N_Character_Literal then
8794 return True;
8796 elsif Nkind (N) in N_Unary_Op then
8797 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
8799 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
8800 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
8801 and then
8802 Has_No_Obvious_Side_Effects (Right_Opnd (N));
8804 elsif Nkind (N) = N_Expression_With_Actions
8805 and then Is_Empty_List (Actions (N))
8806 then
8807 return Has_No_Obvious_Side_Effects (Expression (N));
8809 elsif Nkind (N) in N_Has_Entity then
8810 return Present (Entity (N))
8811 and then Ekind_In (Entity (N), E_Variable,
8812 E_Constant,
8813 E_Enumeration_Literal,
8814 E_In_Parameter,
8815 E_Out_Parameter,
8816 E_In_Out_Parameter)
8817 and then not Is_Volatile (Entity (N));
8819 else
8820 return False;
8821 end if;
8822 end Has_No_Obvious_Side_Effects;
8824 ------------------------
8825 -- Has_Null_Exclusion --
8826 ------------------------
8828 function Has_Null_Exclusion (N : Node_Id) return Boolean is
8829 begin
8830 case Nkind (N) is
8831 when N_Access_Definition |
8832 N_Access_Function_Definition |
8833 N_Access_Procedure_Definition |
8834 N_Access_To_Object_Definition |
8835 N_Allocator |
8836 N_Derived_Type_Definition |
8837 N_Function_Specification |
8838 N_Subtype_Declaration =>
8839 return Null_Exclusion_Present (N);
8841 when N_Component_Definition |
8842 N_Formal_Object_Declaration |
8843 N_Object_Renaming_Declaration =>
8844 if Present (Subtype_Mark (N)) then
8845 return Null_Exclusion_Present (N);
8846 else pragma Assert (Present (Access_Definition (N)));
8847 return Null_Exclusion_Present (Access_Definition (N));
8848 end if;
8850 when N_Discriminant_Specification =>
8851 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
8852 return Null_Exclusion_Present (Discriminant_Type (N));
8853 else
8854 return Null_Exclusion_Present (N);
8855 end if;
8857 when N_Object_Declaration =>
8858 if Nkind (Object_Definition (N)) = N_Access_Definition then
8859 return Null_Exclusion_Present (Object_Definition (N));
8860 else
8861 return Null_Exclusion_Present (N);
8862 end if;
8864 when N_Parameter_Specification =>
8865 if Nkind (Parameter_Type (N)) = N_Access_Definition then
8866 return Null_Exclusion_Present (Parameter_Type (N));
8867 else
8868 return Null_Exclusion_Present (N);
8869 end if;
8871 when others =>
8872 return False;
8874 end case;
8875 end Has_Null_Exclusion;
8877 ------------------------
8878 -- Has_Null_Extension --
8879 ------------------------
8881 function Has_Null_Extension (T : Entity_Id) return Boolean is
8882 B : constant Entity_Id := Base_Type (T);
8883 Comps : Node_Id;
8884 Ext : Node_Id;
8886 begin
8887 if Nkind (Parent (B)) = N_Full_Type_Declaration
8888 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
8889 then
8890 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
8892 if Present (Ext) then
8893 if Null_Present (Ext) then
8894 return True;
8895 else
8896 Comps := Component_List (Ext);
8898 -- The null component list is rewritten during analysis to
8899 -- include the parent component. Any other component indicates
8900 -- that the extension was not originally null.
8902 return Null_Present (Comps)
8903 or else No (Next (First (Component_Items (Comps))));
8904 end if;
8905 else
8906 return False;
8907 end if;
8909 else
8910 return False;
8911 end if;
8912 end Has_Null_Extension;
8914 -------------------------------
8915 -- Has_Overriding_Initialize --
8916 -------------------------------
8918 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
8919 BT : constant Entity_Id := Base_Type (T);
8920 P : Elmt_Id;
8922 begin
8923 if Is_Controlled (BT) then
8924 if Is_RTU (Scope (BT), Ada_Finalization) then
8925 return False;
8927 elsif Present (Primitive_Operations (BT)) then
8928 P := First_Elmt (Primitive_Operations (BT));
8929 while Present (P) loop
8930 declare
8931 Init : constant Entity_Id := Node (P);
8932 Formal : constant Entity_Id := First_Formal (Init);
8933 begin
8934 if Ekind (Init) = E_Procedure
8935 and then Chars (Init) = Name_Initialize
8936 and then Comes_From_Source (Init)
8937 and then Present (Formal)
8938 and then Etype (Formal) = BT
8939 and then No (Next_Formal (Formal))
8940 and then (Ada_Version < Ada_2012
8941 or else not Null_Present (Parent (Init)))
8942 then
8943 return True;
8944 end if;
8945 end;
8947 Next_Elmt (P);
8948 end loop;
8949 end if;
8951 -- Here if type itself does not have a non-null Initialize operation:
8952 -- check immediate ancestor.
8954 if Is_Derived_Type (BT)
8955 and then Has_Overriding_Initialize (Etype (BT))
8956 then
8957 return True;
8958 end if;
8959 end if;
8961 return False;
8962 end Has_Overriding_Initialize;
8964 --------------------------------------
8965 -- Has_Preelaborable_Initialization --
8966 --------------------------------------
8968 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
8969 Has_PE : Boolean;
8971 procedure Check_Components (E : Entity_Id);
8972 -- Check component/discriminant chain, sets Has_PE False if a component
8973 -- or discriminant does not meet the preelaborable initialization rules.
8975 ----------------------
8976 -- Check_Components --
8977 ----------------------
8979 procedure Check_Components (E : Entity_Id) is
8980 Ent : Entity_Id;
8981 Exp : Node_Id;
8983 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
8984 -- Returns True if and only if the expression denoted by N does not
8985 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
8987 ---------------------------------
8988 -- Is_Preelaborable_Expression --
8989 ---------------------------------
8991 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
8992 Exp : Node_Id;
8993 Assn : Node_Id;
8994 Choice : Node_Id;
8995 Comp_Type : Entity_Id;
8996 Is_Array_Aggr : Boolean;
8998 begin
8999 if Is_OK_Static_Expression (N) then
9000 return True;
9002 elsif Nkind (N) = N_Null then
9003 return True;
9005 -- Attributes are allowed in general, even if their prefix is a
9006 -- formal type. (It seems that certain attributes known not to be
9007 -- static might not be allowed, but there are no rules to prevent
9008 -- them.)
9010 elsif Nkind (N) = N_Attribute_Reference then
9011 return True;
9013 -- The name of a discriminant evaluated within its parent type is
9014 -- defined to be preelaborable (10.2.1(8)). Note that we test for
9015 -- names that denote discriminals as well as discriminants to
9016 -- catch references occurring within init procs.
9018 elsif Is_Entity_Name (N)
9019 and then
9020 (Ekind (Entity (N)) = E_Discriminant
9021 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
9022 and then Present (Discriminal_Link (Entity (N)))))
9023 then
9024 return True;
9026 elsif Nkind (N) = N_Qualified_Expression then
9027 return Is_Preelaborable_Expression (Expression (N));
9029 -- For aggregates we have to check that each of the associations
9030 -- is preelaborable.
9032 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
9033 Is_Array_Aggr := Is_Array_Type (Etype (N));
9035 if Is_Array_Aggr then
9036 Comp_Type := Component_Type (Etype (N));
9037 end if;
9039 -- Check the ancestor part of extension aggregates, which must
9040 -- be either the name of a type that has preelaborable init or
9041 -- an expression that is preelaborable.
9043 if Nkind (N) = N_Extension_Aggregate then
9044 declare
9045 Anc_Part : constant Node_Id := Ancestor_Part (N);
9047 begin
9048 if Is_Entity_Name (Anc_Part)
9049 and then Is_Type (Entity (Anc_Part))
9050 then
9051 if not Has_Preelaborable_Initialization
9052 (Entity (Anc_Part))
9053 then
9054 return False;
9055 end if;
9057 elsif not Is_Preelaborable_Expression (Anc_Part) then
9058 return False;
9059 end if;
9060 end;
9061 end if;
9063 -- Check positional associations
9065 Exp := First (Expressions (N));
9066 while Present (Exp) loop
9067 if not Is_Preelaborable_Expression (Exp) then
9068 return False;
9069 end if;
9071 Next (Exp);
9072 end loop;
9074 -- Check named associations
9076 Assn := First (Component_Associations (N));
9077 while Present (Assn) loop
9078 Choice := First (Choices (Assn));
9079 while Present (Choice) loop
9080 if Is_Array_Aggr then
9081 if Nkind (Choice) = N_Others_Choice then
9082 null;
9084 elsif Nkind (Choice) = N_Range then
9085 if not Is_OK_Static_Range (Choice) then
9086 return False;
9087 end if;
9089 elsif not Is_OK_Static_Expression (Choice) then
9090 return False;
9091 end if;
9093 else
9094 Comp_Type := Etype (Choice);
9095 end if;
9097 Next (Choice);
9098 end loop;
9100 -- If the association has a <> at this point, then we have
9101 -- to check whether the component's type has preelaborable
9102 -- initialization. Note that this only occurs when the
9103 -- association's corresponding component does not have a
9104 -- default expression, the latter case having already been
9105 -- expanded as an expression for the association.
9107 if Box_Present (Assn) then
9108 if not Has_Preelaborable_Initialization (Comp_Type) then
9109 return False;
9110 end if;
9112 -- In the expression case we check whether the expression
9113 -- is preelaborable.
9115 elsif
9116 not Is_Preelaborable_Expression (Expression (Assn))
9117 then
9118 return False;
9119 end if;
9121 Next (Assn);
9122 end loop;
9124 -- If we get here then aggregate as a whole is preelaborable
9126 return True;
9128 -- All other cases are not preelaborable
9130 else
9131 return False;
9132 end if;
9133 end Is_Preelaborable_Expression;
9135 -- Start of processing for Check_Components
9137 begin
9138 -- Loop through entities of record or protected type
9140 Ent := E;
9141 while Present (Ent) loop
9143 -- We are interested only in components and discriminants
9145 Exp := Empty;
9147 case Ekind (Ent) is
9148 when E_Component =>
9150 -- Get default expression if any. If there is no declaration
9151 -- node, it means we have an internal entity. The parent and
9152 -- tag fields are examples of such entities. For such cases,
9153 -- we just test the type of the entity.
9155 if Present (Declaration_Node (Ent)) then
9156 Exp := Expression (Declaration_Node (Ent));
9157 end if;
9159 when E_Discriminant =>
9161 -- Note: for a renamed discriminant, the Declaration_Node
9162 -- may point to the one from the ancestor, and have a
9163 -- different expression, so use the proper attribute to
9164 -- retrieve the expression from the derived constraint.
9166 Exp := Discriminant_Default_Value (Ent);
9168 when others =>
9169 goto Check_Next_Entity;
9170 end case;
9172 -- A component has PI if it has no default expression and the
9173 -- component type has PI.
9175 if No (Exp) then
9176 if not Has_Preelaborable_Initialization (Etype (Ent)) then
9177 Has_PE := False;
9178 exit;
9179 end if;
9181 -- Require the default expression to be preelaborable
9183 elsif not Is_Preelaborable_Expression (Exp) then
9184 Has_PE := False;
9185 exit;
9186 end if;
9188 <<Check_Next_Entity>>
9189 Next_Entity (Ent);
9190 end loop;
9191 end Check_Components;
9193 -- Start of processing for Has_Preelaborable_Initialization
9195 begin
9196 -- Immediate return if already marked as known preelaborable init. This
9197 -- covers types for which this function has already been called once
9198 -- and returned True (in which case the result is cached), and also
9199 -- types to which a pragma Preelaborable_Initialization applies.
9201 if Known_To_Have_Preelab_Init (E) then
9202 return True;
9203 end if;
9205 -- If the type is a subtype representing a generic actual type, then
9206 -- test whether its base type has preelaborable initialization since
9207 -- the subtype representing the actual does not inherit this attribute
9208 -- from the actual or formal. (but maybe it should???)
9210 if Is_Generic_Actual_Type (E) then
9211 return Has_Preelaborable_Initialization (Base_Type (E));
9212 end if;
9214 -- All elementary types have preelaborable initialization
9216 if Is_Elementary_Type (E) then
9217 Has_PE := True;
9219 -- Array types have PI if the component type has PI
9221 elsif Is_Array_Type (E) then
9222 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
9224 -- A derived type has preelaborable initialization if its parent type
9225 -- has preelaborable initialization and (in the case of a derived record
9226 -- extension) if the non-inherited components all have preelaborable
9227 -- initialization. However, a user-defined controlled type with an
9228 -- overriding Initialize procedure does not have preelaborable
9229 -- initialization.
9231 elsif Is_Derived_Type (E) then
9233 -- If the derived type is a private extension then it doesn't have
9234 -- preelaborable initialization.
9236 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
9237 return False;
9238 end if;
9240 -- First check whether ancestor type has preelaborable initialization
9242 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
9244 -- If OK, check extension components (if any)
9246 if Has_PE and then Is_Record_Type (E) then
9247 Check_Components (First_Entity (E));
9248 end if;
9250 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
9251 -- with a user defined Initialize procedure does not have PI. If
9252 -- the type is untagged, the control primitives come from a component
9253 -- that has already been checked.
9255 if Has_PE
9256 and then Is_Controlled (E)
9257 and then Is_Tagged_Type (E)
9258 and then Has_Overriding_Initialize (E)
9259 then
9260 Has_PE := False;
9261 end if;
9263 -- Private types not derived from a type having preelaborable init and
9264 -- that are not marked with pragma Preelaborable_Initialization do not
9265 -- have preelaborable initialization.
9267 elsif Is_Private_Type (E) then
9268 return False;
9270 -- Record type has PI if it is non private and all components have PI
9272 elsif Is_Record_Type (E) then
9273 Has_PE := True;
9274 Check_Components (First_Entity (E));
9276 -- Protected types must not have entries, and components must meet
9277 -- same set of rules as for record components.
9279 elsif Is_Protected_Type (E) then
9280 if Has_Entries (E) then
9281 Has_PE := False;
9282 else
9283 Has_PE := True;
9284 Check_Components (First_Entity (E));
9285 Check_Components (First_Private_Entity (E));
9286 end if;
9288 -- Type System.Address always has preelaborable initialization
9290 elsif Is_RTE (E, RE_Address) then
9291 Has_PE := True;
9293 -- In all other cases, type does not have preelaborable initialization
9295 else
9296 return False;
9297 end if;
9299 -- If type has preelaborable initialization, cache result
9301 if Has_PE then
9302 Set_Known_To_Have_Preelab_Init (E);
9303 end if;
9305 return Has_PE;
9306 end Has_Preelaborable_Initialization;
9308 ---------------------------
9309 -- Has_Private_Component --
9310 ---------------------------
9312 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
9313 Btype : Entity_Id := Base_Type (Type_Id);
9314 Component : Entity_Id;
9316 begin
9317 if Error_Posted (Type_Id)
9318 or else Error_Posted (Btype)
9319 then
9320 return False;
9321 end if;
9323 if Is_Class_Wide_Type (Btype) then
9324 Btype := Root_Type (Btype);
9325 end if;
9327 if Is_Private_Type (Btype) then
9328 declare
9329 UT : constant Entity_Id := Underlying_Type (Btype);
9330 begin
9331 if No (UT) then
9332 if No (Full_View (Btype)) then
9333 return not Is_Generic_Type (Btype)
9334 and then
9335 not Is_Generic_Type (Root_Type (Btype));
9336 else
9337 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
9338 end if;
9339 else
9340 return not Is_Frozen (UT) and then Has_Private_Component (UT);
9341 end if;
9342 end;
9344 elsif Is_Array_Type (Btype) then
9345 return Has_Private_Component (Component_Type (Btype));
9347 elsif Is_Record_Type (Btype) then
9348 Component := First_Component (Btype);
9349 while Present (Component) loop
9350 if Has_Private_Component (Etype (Component)) then
9351 return True;
9352 end if;
9354 Next_Component (Component);
9355 end loop;
9357 return False;
9359 elsif Is_Protected_Type (Btype)
9360 and then Present (Corresponding_Record_Type (Btype))
9361 then
9362 return Has_Private_Component (Corresponding_Record_Type (Btype));
9364 else
9365 return False;
9366 end if;
9367 end Has_Private_Component;
9369 ----------------------
9370 -- Has_Signed_Zeros --
9371 ----------------------
9373 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
9374 begin
9375 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
9376 end Has_Signed_Zeros;
9378 ------------------------------
9379 -- Has_Significant_Contract --
9380 ------------------------------
9382 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
9383 Subp_Nam : constant Name_Id := Chars (Subp_Id);
9385 begin
9386 -- _Finalizer procedure
9388 if Subp_Nam = Name_uFinalizer then
9389 return False;
9391 -- _Postconditions procedure
9393 elsif Subp_Nam = Name_uPostconditions then
9394 return False;
9396 -- Predicate function
9398 elsif Ekind (Subp_Id) = E_Function
9399 and then Is_Predicate_Function (Subp_Id)
9400 then
9401 return False;
9403 -- TSS subprogram
9405 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
9406 return False;
9408 else
9409 return True;
9410 end if;
9411 end Has_Significant_Contract;
9413 -----------------------------
9414 -- Has_Static_Array_Bounds --
9415 -----------------------------
9417 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
9418 Ndims : constant Nat := Number_Dimensions (Typ);
9420 Index : Node_Id;
9421 Low : Node_Id;
9422 High : Node_Id;
9424 begin
9425 -- Unconstrained types do not have static bounds
9427 if not Is_Constrained (Typ) then
9428 return False;
9429 end if;
9431 -- First treat string literals specially, as the lower bound and length
9432 -- of string literals are not stored like those of arrays.
9434 -- A string literal always has static bounds
9436 if Ekind (Typ) = E_String_Literal_Subtype then
9437 return True;
9438 end if;
9440 -- Treat all dimensions in turn
9442 Index := First_Index (Typ);
9443 for Indx in 1 .. Ndims loop
9445 -- In case of an illegal index which is not a discrete type, return
9446 -- that the type is not static.
9448 if not Is_Discrete_Type (Etype (Index))
9449 or else Etype (Index) = Any_Type
9450 then
9451 return False;
9452 end if;
9454 Get_Index_Bounds (Index, Low, High);
9456 if Error_Posted (Low) or else Error_Posted (High) then
9457 return False;
9458 end if;
9460 if Is_OK_Static_Expression (Low)
9461 and then
9462 Is_OK_Static_Expression (High)
9463 then
9464 null;
9465 else
9466 return False;
9467 end if;
9469 Next (Index);
9470 end loop;
9472 -- If we fall through the loop, all indexes matched
9474 return True;
9475 end Has_Static_Array_Bounds;
9477 ----------------
9478 -- Has_Stream --
9479 ----------------
9481 function Has_Stream (T : Entity_Id) return Boolean is
9482 E : Entity_Id;
9484 begin
9485 if No (T) then
9486 return False;
9488 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
9489 return True;
9491 elsif Is_Array_Type (T) then
9492 return Has_Stream (Component_Type (T));
9494 elsif Is_Record_Type (T) then
9495 E := First_Component (T);
9496 while Present (E) loop
9497 if Has_Stream (Etype (E)) then
9498 return True;
9499 else
9500 Next_Component (E);
9501 end if;
9502 end loop;
9504 return False;
9506 elsif Is_Private_Type (T) then
9507 return Has_Stream (Underlying_Type (T));
9509 else
9510 return False;
9511 end if;
9512 end Has_Stream;
9514 ----------------
9515 -- Has_Suffix --
9516 ----------------
9518 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
9519 begin
9520 Get_Name_String (Chars (E));
9521 return Name_Buffer (Name_Len) = Suffix;
9522 end Has_Suffix;
9524 ----------------
9525 -- Add_Suffix --
9526 ----------------
9528 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
9529 begin
9530 Get_Name_String (Chars (E));
9531 Add_Char_To_Name_Buffer (Suffix);
9532 return Name_Find;
9533 end Add_Suffix;
9535 -------------------
9536 -- Remove_Suffix --
9537 -------------------
9539 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
9540 begin
9541 pragma Assert (Has_Suffix (E, Suffix));
9542 Get_Name_String (Chars (E));
9543 Name_Len := Name_Len - 1;
9544 return Name_Find;
9545 end Remove_Suffix;
9547 --------------------------
9548 -- Has_Tagged_Component --
9549 --------------------------
9551 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
9552 Comp : Entity_Id;
9554 begin
9555 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
9556 return Has_Tagged_Component (Underlying_Type (Typ));
9558 elsif Is_Array_Type (Typ) then
9559 return Has_Tagged_Component (Component_Type (Typ));
9561 elsif Is_Tagged_Type (Typ) then
9562 return True;
9564 elsif Is_Record_Type (Typ) then
9565 Comp := First_Component (Typ);
9566 while Present (Comp) loop
9567 if Has_Tagged_Component (Etype (Comp)) then
9568 return True;
9569 end if;
9571 Next_Component (Comp);
9572 end loop;
9574 return False;
9576 else
9577 return False;
9578 end if;
9579 end Has_Tagged_Component;
9581 ----------------------------
9582 -- Has_Volatile_Component --
9583 ----------------------------
9585 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
9586 Comp : Entity_Id;
9588 begin
9589 if Has_Volatile_Components (Typ) then
9590 return True;
9592 elsif Is_Array_Type (Typ) then
9593 return Is_Volatile (Component_Type (Typ));
9595 elsif Is_Record_Type (Typ) then
9596 Comp := First_Component (Typ);
9597 while Present (Comp) loop
9598 if Is_Volatile_Object (Comp) then
9599 return True;
9600 end if;
9602 Comp := Next_Component (Comp);
9603 end loop;
9604 end if;
9606 return False;
9607 end Has_Volatile_Component;
9609 -------------------------
9610 -- Implementation_Kind --
9611 -------------------------
9613 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
9614 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
9615 Arg : Node_Id;
9616 begin
9617 pragma Assert (Present (Impl_Prag));
9618 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
9619 return Chars (Get_Pragma_Arg (Arg));
9620 end Implementation_Kind;
9622 --------------------------
9623 -- Implements_Interface --
9624 --------------------------
9626 function Implements_Interface
9627 (Typ_Ent : Entity_Id;
9628 Iface_Ent : Entity_Id;
9629 Exclude_Parents : Boolean := False) return Boolean
9631 Ifaces_List : Elist_Id;
9632 Elmt : Elmt_Id;
9633 Iface : Entity_Id := Base_Type (Iface_Ent);
9634 Typ : Entity_Id := Base_Type (Typ_Ent);
9636 begin
9637 if Is_Class_Wide_Type (Typ) then
9638 Typ := Root_Type (Typ);
9639 end if;
9641 if not Has_Interfaces (Typ) then
9642 return False;
9643 end if;
9645 if Is_Class_Wide_Type (Iface) then
9646 Iface := Root_Type (Iface);
9647 end if;
9649 Collect_Interfaces (Typ, Ifaces_List);
9651 Elmt := First_Elmt (Ifaces_List);
9652 while Present (Elmt) loop
9653 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
9654 and then Exclude_Parents
9655 then
9656 null;
9658 elsif Node (Elmt) = Iface then
9659 return True;
9660 end if;
9662 Next_Elmt (Elmt);
9663 end loop;
9665 return False;
9666 end Implements_Interface;
9668 ------------------------------------
9669 -- In_Assertion_Expression_Pragma --
9670 ------------------------------------
9672 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
9673 Par : Node_Id;
9674 Prag : Node_Id := Empty;
9676 begin
9677 -- Climb the parent chain looking for an enclosing pragma
9679 Par := N;
9680 while Present (Par) loop
9681 if Nkind (Par) = N_Pragma then
9682 Prag := Par;
9683 exit;
9685 -- Precondition-like pragmas are expanded into if statements, check
9686 -- the original node instead.
9688 elsif Nkind (Original_Node (Par)) = N_Pragma then
9689 Prag := Original_Node (Par);
9690 exit;
9692 -- The expansion of attribute 'Old generates a constant to capture
9693 -- the result of the prefix. If the parent traversal reaches
9694 -- one of these constants, then the node technically came from a
9695 -- postcondition-like pragma. Note that the Ekind is not tested here
9696 -- because N may be the expression of an object declaration which is
9697 -- currently being analyzed. Such objects carry Ekind of E_Void.
9699 elsif Nkind (Par) = N_Object_Declaration
9700 and then Constant_Present (Par)
9701 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
9702 then
9703 return True;
9705 -- Prevent the search from going too far
9707 elsif Is_Body_Or_Package_Declaration (Par) then
9708 return False;
9709 end if;
9711 Par := Parent (Par);
9712 end loop;
9714 return
9715 Present (Prag)
9716 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
9717 end In_Assertion_Expression_Pragma;
9719 -----------------
9720 -- In_Instance --
9721 -----------------
9723 function In_Instance return Boolean is
9724 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9725 S : Entity_Id;
9727 begin
9728 S := Current_Scope;
9729 while Present (S) and then S /= Standard_Standard loop
9730 if Ekind_In (S, E_Function, E_Package, E_Procedure)
9731 and then Is_Generic_Instance (S)
9732 then
9733 -- A child instance is always compiled in the context of a parent
9734 -- instance. Nevertheless, the actuals are not analyzed in an
9735 -- instance context. We detect this case by examining the current
9736 -- compilation unit, which must be a child instance, and checking
9737 -- that it is not currently on the scope stack.
9739 if Is_Child_Unit (Curr_Unit)
9740 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
9741 N_Package_Instantiation
9742 and then not In_Open_Scopes (Curr_Unit)
9743 then
9744 return False;
9745 else
9746 return True;
9747 end if;
9748 end if;
9750 S := Scope (S);
9751 end loop;
9753 return False;
9754 end In_Instance;
9756 ----------------------
9757 -- In_Instance_Body --
9758 ----------------------
9760 function In_Instance_Body return Boolean is
9761 S : Entity_Id;
9763 begin
9764 S := Current_Scope;
9765 while Present (S) and then S /= Standard_Standard loop
9766 if Ekind_In (S, E_Function, E_Procedure)
9767 and then Is_Generic_Instance (S)
9768 then
9769 return True;
9771 elsif Ekind (S) = E_Package
9772 and then In_Package_Body (S)
9773 and then Is_Generic_Instance (S)
9774 then
9775 return True;
9776 end if;
9778 S := Scope (S);
9779 end loop;
9781 return False;
9782 end In_Instance_Body;
9784 -----------------------------
9785 -- In_Instance_Not_Visible --
9786 -----------------------------
9788 function In_Instance_Not_Visible return Boolean is
9789 S : Entity_Id;
9791 begin
9792 S := Current_Scope;
9793 while Present (S) and then S /= Standard_Standard loop
9794 if Ekind_In (S, E_Function, E_Procedure)
9795 and then Is_Generic_Instance (S)
9796 then
9797 return True;
9799 elsif Ekind (S) = E_Package
9800 and then (In_Package_Body (S) or else In_Private_Part (S))
9801 and then Is_Generic_Instance (S)
9802 then
9803 return True;
9804 end if;
9806 S := Scope (S);
9807 end loop;
9809 return False;
9810 end In_Instance_Not_Visible;
9812 ------------------------------
9813 -- In_Instance_Visible_Part --
9814 ------------------------------
9816 function In_Instance_Visible_Part return Boolean is
9817 S : Entity_Id;
9819 begin
9820 S := Current_Scope;
9821 while Present (S) and then S /= Standard_Standard loop
9822 if Ekind (S) = E_Package
9823 and then Is_Generic_Instance (S)
9824 and then not In_Package_Body (S)
9825 and then not In_Private_Part (S)
9826 then
9827 return True;
9828 end if;
9830 S := Scope (S);
9831 end loop;
9833 return False;
9834 end In_Instance_Visible_Part;
9836 ---------------------
9837 -- In_Package_Body --
9838 ---------------------
9840 function In_Package_Body return Boolean is
9841 S : Entity_Id;
9843 begin
9844 S := Current_Scope;
9845 while Present (S) and then S /= Standard_Standard loop
9846 if Ekind (S) = E_Package and then In_Package_Body (S) then
9847 return True;
9848 else
9849 S := Scope (S);
9850 end if;
9851 end loop;
9853 return False;
9854 end In_Package_Body;
9856 --------------------------------
9857 -- In_Parameter_Specification --
9858 --------------------------------
9860 function In_Parameter_Specification (N : Node_Id) return Boolean is
9861 PN : Node_Id;
9863 begin
9864 PN := Parent (N);
9865 while Present (PN) loop
9866 if Nkind (PN) = N_Parameter_Specification then
9867 return True;
9868 end if;
9870 PN := Parent (PN);
9871 end loop;
9873 return False;
9874 end In_Parameter_Specification;
9876 --------------------------
9877 -- In_Pragma_Expression --
9878 --------------------------
9880 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
9881 P : Node_Id;
9882 begin
9883 P := Parent (N);
9884 loop
9885 if No (P) then
9886 return False;
9887 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
9888 return True;
9889 else
9890 P := Parent (P);
9891 end if;
9892 end loop;
9893 end In_Pragma_Expression;
9895 -------------------------------------
9896 -- In_Reverse_Storage_Order_Object --
9897 -------------------------------------
9899 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
9900 Pref : Node_Id;
9901 Btyp : Entity_Id := Empty;
9903 begin
9904 -- Climb up indexed components
9906 Pref := N;
9907 loop
9908 case Nkind (Pref) is
9909 when N_Selected_Component =>
9910 Pref := Prefix (Pref);
9911 exit;
9913 when N_Indexed_Component =>
9914 Pref := Prefix (Pref);
9916 when others =>
9917 Pref := Empty;
9918 exit;
9919 end case;
9920 end loop;
9922 if Present (Pref) then
9923 Btyp := Base_Type (Etype (Pref));
9924 end if;
9926 return Present (Btyp)
9927 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
9928 and then Reverse_Storage_Order (Btyp);
9929 end In_Reverse_Storage_Order_Object;
9931 --------------------------------------
9932 -- In_Subprogram_Or_Concurrent_Unit --
9933 --------------------------------------
9935 function In_Subprogram_Or_Concurrent_Unit return Boolean is
9936 E : Entity_Id;
9937 K : Entity_Kind;
9939 begin
9940 -- Use scope chain to check successively outer scopes
9942 E := Current_Scope;
9943 loop
9944 K := Ekind (E);
9946 if K in Subprogram_Kind
9947 or else K in Concurrent_Kind
9948 or else K in Generic_Subprogram_Kind
9949 then
9950 return True;
9952 elsif E = Standard_Standard then
9953 return False;
9954 end if;
9956 E := Scope (E);
9957 end loop;
9958 end In_Subprogram_Or_Concurrent_Unit;
9960 ---------------------
9961 -- In_Visible_Part --
9962 ---------------------
9964 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
9965 begin
9966 return Is_Package_Or_Generic_Package (Scope_Id)
9967 and then In_Open_Scopes (Scope_Id)
9968 and then not In_Package_Body (Scope_Id)
9969 and then not In_Private_Part (Scope_Id);
9970 end In_Visible_Part;
9972 --------------------------------
9973 -- Incomplete_Or_Partial_View --
9974 --------------------------------
9976 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
9977 function Inspect_Decls
9978 (Decls : List_Id;
9979 Taft : Boolean := False) return Entity_Id;
9980 -- Check whether a declarative region contains the incomplete or partial
9981 -- view of Id.
9983 -------------------
9984 -- Inspect_Decls --
9985 -------------------
9987 function Inspect_Decls
9988 (Decls : List_Id;
9989 Taft : Boolean := False) return Entity_Id
9991 Decl : Node_Id;
9992 Match : Node_Id;
9994 begin
9995 Decl := First (Decls);
9996 while Present (Decl) loop
9997 Match := Empty;
9999 if Taft then
10000 if Nkind (Decl) = N_Incomplete_Type_Declaration then
10001 Match := Defining_Identifier (Decl);
10002 end if;
10004 else
10005 if Nkind_In (Decl, N_Private_Extension_Declaration,
10006 N_Private_Type_Declaration)
10007 then
10008 Match := Defining_Identifier (Decl);
10009 end if;
10010 end if;
10012 if Present (Match)
10013 and then Present (Full_View (Match))
10014 and then Full_View (Match) = Id
10015 then
10016 return Match;
10017 end if;
10019 Next (Decl);
10020 end loop;
10022 return Empty;
10023 end Inspect_Decls;
10025 -- Local variables
10027 Prev : Entity_Id;
10029 -- Start of processing for Incomplete_Or_Partial_View
10031 begin
10032 -- Deferred constant or incomplete type case
10034 Prev := Current_Entity_In_Scope (Id);
10036 if Present (Prev)
10037 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
10038 and then Present (Full_View (Prev))
10039 and then Full_View (Prev) = Id
10040 then
10041 return Prev;
10042 end if;
10044 -- Private or Taft amendment type case
10046 declare
10047 Pkg : constant Entity_Id := Scope (Id);
10048 Pkg_Decl : Node_Id := Pkg;
10050 begin
10051 if Present (Pkg) and then Ekind (Pkg) = E_Package then
10052 while Nkind (Pkg_Decl) /= N_Package_Specification loop
10053 Pkg_Decl := Parent (Pkg_Decl);
10054 end loop;
10056 -- It is knows that Typ has a private view, look for it in the
10057 -- visible declarations of the enclosing scope. A special case
10058 -- of this is when the two views have been exchanged - the full
10059 -- appears earlier than the private.
10061 if Has_Private_Declaration (Id) then
10062 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
10064 -- Exchanged view case, look in the private declarations
10066 if No (Prev) then
10067 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
10068 end if;
10070 return Prev;
10072 -- Otherwise if this is the package body, then Typ is a potential
10073 -- Taft amendment type. The incomplete view should be located in
10074 -- the private declarations of the enclosing scope.
10076 elsif In_Package_Body (Pkg) then
10077 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
10078 end if;
10079 end if;
10080 end;
10082 -- The type has no incomplete or private view
10084 return Empty;
10085 end Incomplete_Or_Partial_View;
10087 -----------------------------------------
10088 -- Inherit_Default_Init_Cond_Procedure --
10089 -----------------------------------------
10091 procedure Inherit_Default_Init_Cond_Procedure (Typ : Entity_Id) is
10092 Par_Typ : constant Entity_Id := Etype (Typ);
10094 begin
10095 -- A derived type inherits the default initial condition procedure of
10096 -- its parent type.
10098 if No (Default_Init_Cond_Procedure (Typ)) then
10099 Set_Default_Init_Cond_Procedure
10100 (Typ, Default_Init_Cond_Procedure (Par_Typ));
10101 end if;
10102 end Inherit_Default_Init_Cond_Procedure;
10104 ----------------------------
10105 -- Inherit_Rep_Item_Chain --
10106 ----------------------------
10108 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
10109 From_Item : constant Node_Id := First_Rep_Item (From_Typ);
10110 Item : Node_Id := Empty;
10111 Last_Item : Node_Id := Empty;
10113 begin
10114 -- Reach the end of the destination type's chain (if any) and capture
10115 -- the last item.
10117 Item := First_Rep_Item (Typ);
10118 while Present (Item) loop
10120 -- Do not inherit a chain that has been inherited already
10122 if Item = From_Item then
10123 return;
10124 end if;
10126 Last_Item := Item;
10127 Item := Next_Rep_Item (Item);
10128 end loop;
10130 -- When the destination type has a rep item chain, the chain of the
10131 -- source type is appended to it.
10133 if Present (Last_Item) then
10134 Set_Next_Rep_Item (Last_Item, From_Item);
10136 -- Otherwise the destination type directly inherits the rep item chain
10137 -- of the source type (if any).
10139 else
10140 Set_First_Rep_Item (Typ, From_Item);
10141 end if;
10142 end Inherit_Rep_Item_Chain;
10144 ---------------------------------
10145 -- Inherit_Subprogram_Contract --
10146 ---------------------------------
10148 procedure Inherit_Subprogram_Contract
10149 (Subp : Entity_Id;
10150 From_Subp : Entity_Id)
10152 procedure Inherit_Pragma (Prag_Id : Pragma_Id);
10153 -- Propagate a pragma denoted by Prag_Id from From_Subp's contract to
10154 -- Subp's contract.
10156 --------------------
10157 -- Inherit_Pragma --
10158 --------------------
10160 procedure Inherit_Pragma (Prag_Id : Pragma_Id) is
10161 Prag : constant Node_Id := Get_Pragma (From_Subp, Prag_Id);
10162 New_Prag : Node_Id;
10164 begin
10165 -- A pragma cannot be part of more than one First_Pragma/Next_Pragma
10166 -- chains, therefore the node must be replicated. The new pragma is
10167 -- flagged is inherited for distrinction purposes.
10169 if Present (Prag) then
10170 New_Prag := New_Copy_Tree (Prag);
10171 Set_Is_Inherited (New_Prag);
10173 Add_Contract_Item (New_Prag, Subp);
10174 end if;
10175 end Inherit_Pragma;
10177 -- Start of processing for Inherit_Subprogram_Contract
10179 begin
10180 -- Inheritance is carried out only when both entities are subprograms
10181 -- with contracts.
10183 if Is_Subprogram_Or_Generic_Subprogram (Subp)
10184 and then Is_Subprogram_Or_Generic_Subprogram (From_Subp)
10185 and then Present (Contract (From_Subp))
10186 then
10187 Inherit_Pragma (Pragma_Extensions_Visible);
10188 end if;
10189 end Inherit_Subprogram_Contract;
10191 ---------------------------------
10192 -- Insert_Explicit_Dereference --
10193 ---------------------------------
10195 procedure Insert_Explicit_Dereference (N : Node_Id) is
10196 New_Prefix : constant Node_Id := Relocate_Node (N);
10197 Ent : Entity_Id := Empty;
10198 Pref : Node_Id;
10199 I : Interp_Index;
10200 It : Interp;
10201 T : Entity_Id;
10203 begin
10204 Save_Interps (N, New_Prefix);
10206 Rewrite (N,
10207 Make_Explicit_Dereference (Sloc (Parent (N)),
10208 Prefix => New_Prefix));
10210 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
10212 if Is_Overloaded (New_Prefix) then
10214 -- The dereference is also overloaded, and its interpretations are
10215 -- the designated types of the interpretations of the original node.
10217 Set_Etype (N, Any_Type);
10219 Get_First_Interp (New_Prefix, I, It);
10220 while Present (It.Nam) loop
10221 T := It.Typ;
10223 if Is_Access_Type (T) then
10224 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
10225 end if;
10227 Get_Next_Interp (I, It);
10228 end loop;
10230 End_Interp_List;
10232 else
10233 -- Prefix is unambiguous: mark the original prefix (which might
10234 -- Come_From_Source) as a reference, since the new (relocated) one
10235 -- won't be taken into account.
10237 if Is_Entity_Name (New_Prefix) then
10238 Ent := Entity (New_Prefix);
10239 Pref := New_Prefix;
10241 -- For a retrieval of a subcomponent of some composite object,
10242 -- retrieve the ultimate entity if there is one.
10244 elsif Nkind_In (New_Prefix, N_Selected_Component,
10245 N_Indexed_Component)
10246 then
10247 Pref := Prefix (New_Prefix);
10248 while Present (Pref)
10249 and then Nkind_In (Pref, N_Selected_Component,
10250 N_Indexed_Component)
10251 loop
10252 Pref := Prefix (Pref);
10253 end loop;
10255 if Present (Pref) and then Is_Entity_Name (Pref) then
10256 Ent := Entity (Pref);
10257 end if;
10258 end if;
10260 -- Place the reference on the entity node
10262 if Present (Ent) then
10263 Generate_Reference (Ent, Pref);
10264 end if;
10265 end if;
10266 end Insert_Explicit_Dereference;
10268 ------------------------------------------
10269 -- Inspect_Deferred_Constant_Completion --
10270 ------------------------------------------
10272 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
10273 Decl : Node_Id;
10275 begin
10276 Decl := First (Decls);
10277 while Present (Decl) loop
10279 -- Deferred constant signature
10281 if Nkind (Decl) = N_Object_Declaration
10282 and then Constant_Present (Decl)
10283 and then No (Expression (Decl))
10285 -- No need to check internally generated constants
10287 and then Comes_From_Source (Decl)
10289 -- The constant is not completed. A full object declaration or a
10290 -- pragma Import complete a deferred constant.
10292 and then not Has_Completion (Defining_Identifier (Decl))
10293 then
10294 Error_Msg_N
10295 ("constant declaration requires initialization expression",
10296 Defining_Identifier (Decl));
10297 end if;
10299 Decl := Next (Decl);
10300 end loop;
10301 end Inspect_Deferred_Constant_Completion;
10303 -----------------------------
10304 -- Install_Generic_Formals --
10305 -----------------------------
10307 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
10308 E : Entity_Id;
10310 begin
10311 pragma Assert (Is_Generic_Subprogram (Subp_Id));
10313 E := First_Entity (Subp_Id);
10314 while Present (E) loop
10315 Install_Entity (E);
10316 Next_Entity (E);
10317 end loop;
10318 end Install_Generic_Formals;
10320 -----------------------------
10321 -- Is_Actual_Out_Parameter --
10322 -----------------------------
10324 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
10325 Formal : Entity_Id;
10326 Call : Node_Id;
10327 begin
10328 Find_Actual (N, Formal, Call);
10329 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
10330 end Is_Actual_Out_Parameter;
10332 -------------------------
10333 -- Is_Actual_Parameter --
10334 -------------------------
10336 function Is_Actual_Parameter (N : Node_Id) return Boolean is
10337 PK : constant Node_Kind := Nkind (Parent (N));
10339 begin
10340 case PK is
10341 when N_Parameter_Association =>
10342 return N = Explicit_Actual_Parameter (Parent (N));
10344 when N_Subprogram_Call =>
10345 return Is_List_Member (N)
10346 and then
10347 List_Containing (N) = Parameter_Associations (Parent (N));
10349 when others =>
10350 return False;
10351 end case;
10352 end Is_Actual_Parameter;
10354 --------------------------------
10355 -- Is_Actual_Tagged_Parameter --
10356 --------------------------------
10358 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
10359 Formal : Entity_Id;
10360 Call : Node_Id;
10361 begin
10362 Find_Actual (N, Formal, Call);
10363 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
10364 end Is_Actual_Tagged_Parameter;
10366 ---------------------
10367 -- Is_Aliased_View --
10368 ---------------------
10370 function Is_Aliased_View (Obj : Node_Id) return Boolean is
10371 E : Entity_Id;
10373 begin
10374 if Is_Entity_Name (Obj) then
10375 E := Entity (Obj);
10377 return
10378 (Is_Object (E)
10379 and then
10380 (Is_Aliased (E)
10381 or else (Present (Renamed_Object (E))
10382 and then Is_Aliased_View (Renamed_Object (E)))))
10384 or else ((Is_Formal (E)
10385 or else Ekind_In (E, E_Generic_In_Out_Parameter,
10386 E_Generic_In_Parameter))
10387 and then Is_Tagged_Type (Etype (E)))
10389 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
10391 -- Current instance of type, either directly or as rewritten
10392 -- reference to the current object.
10394 or else (Is_Entity_Name (Original_Node (Obj))
10395 and then Present (Entity (Original_Node (Obj)))
10396 and then Is_Type (Entity (Original_Node (Obj))))
10398 or else (Is_Type (E) and then E = Current_Scope)
10400 or else (Is_Incomplete_Or_Private_Type (E)
10401 and then Full_View (E) = Current_Scope)
10403 -- Ada 2012 AI05-0053: the return object of an extended return
10404 -- statement is aliased if its type is immutably limited.
10406 or else (Is_Return_Object (E)
10407 and then Is_Limited_View (Etype (E)));
10409 elsif Nkind (Obj) = N_Selected_Component then
10410 return Is_Aliased (Entity (Selector_Name (Obj)));
10412 elsif Nkind (Obj) = N_Indexed_Component then
10413 return Has_Aliased_Components (Etype (Prefix (Obj)))
10414 or else
10415 (Is_Access_Type (Etype (Prefix (Obj)))
10416 and then Has_Aliased_Components
10417 (Designated_Type (Etype (Prefix (Obj)))));
10419 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
10420 return Is_Tagged_Type (Etype (Obj))
10421 and then Is_Aliased_View (Expression (Obj));
10423 elsif Nkind (Obj) = N_Explicit_Dereference then
10424 return Nkind (Original_Node (Obj)) /= N_Function_Call;
10426 else
10427 return False;
10428 end if;
10429 end Is_Aliased_View;
10431 -------------------------
10432 -- Is_Ancestor_Package --
10433 -------------------------
10435 function Is_Ancestor_Package
10436 (E1 : Entity_Id;
10437 E2 : Entity_Id) return Boolean
10439 Par : Entity_Id;
10441 begin
10442 Par := E2;
10443 while Present (Par) and then Par /= Standard_Standard loop
10444 if Par = E1 then
10445 return True;
10446 end if;
10448 Par := Scope (Par);
10449 end loop;
10451 return False;
10452 end Is_Ancestor_Package;
10454 ----------------------
10455 -- Is_Atomic_Object --
10456 ----------------------
10458 function Is_Atomic_Object (N : Node_Id) return Boolean is
10460 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
10461 -- Determines if given object has atomic components
10463 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
10464 -- If prefix is an implicit dereference, examine designated type
10466 ----------------------
10467 -- Is_Atomic_Prefix --
10468 ----------------------
10470 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
10471 begin
10472 if Is_Access_Type (Etype (N)) then
10473 return
10474 Has_Atomic_Components (Designated_Type (Etype (N)));
10475 else
10476 return Object_Has_Atomic_Components (N);
10477 end if;
10478 end Is_Atomic_Prefix;
10480 ----------------------------------
10481 -- Object_Has_Atomic_Components --
10482 ----------------------------------
10484 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
10485 begin
10486 if Has_Atomic_Components (Etype (N))
10487 or else Is_Atomic (Etype (N))
10488 then
10489 return True;
10491 elsif Is_Entity_Name (N)
10492 and then (Has_Atomic_Components (Entity (N))
10493 or else Is_Atomic (Entity (N)))
10494 then
10495 return True;
10497 elsif Nkind (N) = N_Selected_Component
10498 and then Is_Atomic (Entity (Selector_Name (N)))
10499 then
10500 return True;
10502 elsif Nkind (N) = N_Indexed_Component
10503 or else Nkind (N) = N_Selected_Component
10504 then
10505 return Is_Atomic_Prefix (Prefix (N));
10507 else
10508 return False;
10509 end if;
10510 end Object_Has_Atomic_Components;
10512 -- Start of processing for Is_Atomic_Object
10514 begin
10515 -- Predicate is not relevant to subprograms
10517 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
10518 return False;
10520 elsif Is_Atomic (Etype (N))
10521 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
10522 then
10523 return True;
10525 elsif Nkind (N) = N_Selected_Component
10526 and then Is_Atomic (Entity (Selector_Name (N)))
10527 then
10528 return True;
10530 elsif Nkind (N) = N_Indexed_Component
10531 or else Nkind (N) = N_Selected_Component
10532 then
10533 return Is_Atomic_Prefix (Prefix (N));
10535 else
10536 return False;
10537 end if;
10538 end Is_Atomic_Object;
10540 -----------------------------
10541 -- Is_Atomic_Or_VFA_Object --
10542 -----------------------------
10544 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
10545 begin
10546 return Is_Atomic_Object (N)
10547 or else (Is_Object_Reference (N)
10548 and then Is_Entity_Name (N)
10549 and then (Is_Volatile_Full_Access (Entity (N))
10550 or else
10551 Is_Volatile_Full_Access (Etype (Entity (N)))));
10552 end Is_Atomic_Or_VFA_Object;
10554 -------------------------
10555 -- Is_Attribute_Result --
10556 -------------------------
10558 function Is_Attribute_Result (N : Node_Id) return Boolean is
10559 begin
10560 return Nkind (N) = N_Attribute_Reference
10561 and then Attribute_Name (N) = Name_Result;
10562 end Is_Attribute_Result;
10564 -------------------------
10565 -- Is_Attribute_Update --
10566 -------------------------
10568 function Is_Attribute_Update (N : Node_Id) return Boolean is
10569 begin
10570 return Nkind (N) = N_Attribute_Reference
10571 and then Attribute_Name (N) = Name_Update;
10572 end Is_Attribute_Update;
10574 ------------------------------------
10575 -- Is_Body_Or_Package_Declaration --
10576 ------------------------------------
10578 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
10579 begin
10580 return Nkind_In (N, N_Entry_Body,
10581 N_Package_Body,
10582 N_Package_Declaration,
10583 N_Protected_Body,
10584 N_Subprogram_Body,
10585 N_Task_Body);
10586 end Is_Body_Or_Package_Declaration;
10588 -----------------------
10589 -- Is_Bounded_String --
10590 -----------------------
10592 function Is_Bounded_String (T : Entity_Id) return Boolean is
10593 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
10595 begin
10596 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
10597 -- Super_String, or one of the [Wide_]Wide_ versions. This will
10598 -- be True for all the Bounded_String types in instances of the
10599 -- Generic_Bounded_Length generics, and for types derived from those.
10601 return Present (Under)
10602 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
10603 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
10604 Is_RTE (Root_Type (Under), RO_WW_Super_String));
10605 end Is_Bounded_String;
10607 -------------------------
10608 -- Is_Child_Or_Sibling --
10609 -------------------------
10611 function Is_Child_Or_Sibling
10612 (Pack_1 : Entity_Id;
10613 Pack_2 : Entity_Id) return Boolean
10615 function Distance_From_Standard (Pack : Entity_Id) return Nat;
10616 -- Given an arbitrary package, return the number of "climbs" necessary
10617 -- to reach scope Standard_Standard.
10619 procedure Equalize_Depths
10620 (Pack : in out Entity_Id;
10621 Depth : in out Nat;
10622 Depth_To_Reach : Nat);
10623 -- Given an arbitrary package, its depth and a target depth to reach,
10624 -- climb the scope chain until the said depth is reached. The pointer
10625 -- to the package and its depth a modified during the climb.
10627 ----------------------------
10628 -- Distance_From_Standard --
10629 ----------------------------
10631 function Distance_From_Standard (Pack : Entity_Id) return Nat is
10632 Dist : Nat;
10633 Scop : Entity_Id;
10635 begin
10636 Dist := 0;
10637 Scop := Pack;
10638 while Present (Scop) and then Scop /= Standard_Standard loop
10639 Dist := Dist + 1;
10640 Scop := Scope (Scop);
10641 end loop;
10643 return Dist;
10644 end Distance_From_Standard;
10646 ---------------------
10647 -- Equalize_Depths --
10648 ---------------------
10650 procedure Equalize_Depths
10651 (Pack : in out Entity_Id;
10652 Depth : in out Nat;
10653 Depth_To_Reach : Nat)
10655 begin
10656 -- The package must be at a greater or equal depth
10658 if Depth < Depth_To_Reach then
10659 raise Program_Error;
10660 end if;
10662 -- Climb the scope chain until the desired depth is reached
10664 while Present (Pack) and then Depth /= Depth_To_Reach loop
10665 Pack := Scope (Pack);
10666 Depth := Depth - 1;
10667 end loop;
10668 end Equalize_Depths;
10670 -- Local variables
10672 P_1 : Entity_Id := Pack_1;
10673 P_1_Child : Boolean := False;
10674 P_1_Depth : Nat := Distance_From_Standard (P_1);
10675 P_2 : Entity_Id := Pack_2;
10676 P_2_Child : Boolean := False;
10677 P_2_Depth : Nat := Distance_From_Standard (P_2);
10679 -- Start of processing for Is_Child_Or_Sibling
10681 begin
10682 pragma Assert
10683 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
10685 -- Both packages denote the same entity, therefore they cannot be
10686 -- children or siblings.
10688 if P_1 = P_2 then
10689 return False;
10691 -- One of the packages is at a deeper level than the other. Note that
10692 -- both may still come from differen hierarchies.
10694 -- (root) P_2
10695 -- / \ :
10696 -- X P_2 or X
10697 -- : :
10698 -- P_1 P_1
10700 elsif P_1_Depth > P_2_Depth then
10701 Equalize_Depths
10702 (Pack => P_1,
10703 Depth => P_1_Depth,
10704 Depth_To_Reach => P_2_Depth);
10705 P_1_Child := True;
10707 -- (root) P_1
10708 -- / \ :
10709 -- P_1 X or X
10710 -- : :
10711 -- P_2 P_2
10713 elsif P_2_Depth > P_1_Depth then
10714 Equalize_Depths
10715 (Pack => P_2,
10716 Depth => P_2_Depth,
10717 Depth_To_Reach => P_1_Depth);
10718 P_2_Child := True;
10719 end if;
10721 -- At this stage the package pointers have been elevated to the same
10722 -- depth. If the related entities are the same, then one package is a
10723 -- potential child of the other:
10725 -- P_1
10726 -- :
10727 -- X became P_1 P_2 or vica versa
10728 -- :
10729 -- P_2
10731 if P_1 = P_2 then
10732 if P_1_Child then
10733 return Is_Child_Unit (Pack_1);
10735 else pragma Assert (P_2_Child);
10736 return Is_Child_Unit (Pack_2);
10737 end if;
10739 -- The packages may come from the same package chain or from entirely
10740 -- different hierarcies. To determine this, climb the scope stack until
10741 -- a common root is found.
10743 -- (root) (root 1) (root 2)
10744 -- / \ | |
10745 -- P_1 P_2 P_1 P_2
10747 else
10748 while Present (P_1) and then Present (P_2) loop
10750 -- The two packages may be siblings
10752 if P_1 = P_2 then
10753 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
10754 end if;
10756 P_1 := Scope (P_1);
10757 P_2 := Scope (P_2);
10758 end loop;
10759 end if;
10761 return False;
10762 end Is_Child_Or_Sibling;
10764 -----------------------------
10765 -- Is_Concurrent_Interface --
10766 -----------------------------
10768 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
10769 begin
10770 return Is_Interface (T)
10771 and then
10772 (Is_Protected_Interface (T)
10773 or else Is_Synchronized_Interface (T)
10774 or else Is_Task_Interface (T));
10775 end Is_Concurrent_Interface;
10777 -----------------------
10778 -- Is_Constant_Bound --
10779 -----------------------
10781 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
10782 begin
10783 if Compile_Time_Known_Value (Exp) then
10784 return True;
10786 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
10787 return Is_Constant_Object (Entity (Exp))
10788 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
10790 elsif Nkind (Exp) in N_Binary_Op then
10791 return Is_Constant_Bound (Left_Opnd (Exp))
10792 and then Is_Constant_Bound (Right_Opnd (Exp))
10793 and then Scope (Entity (Exp)) = Standard_Standard;
10795 else
10796 return False;
10797 end if;
10798 end Is_Constant_Bound;
10800 ---------------------------
10801 -- Is_Container_Element --
10802 ---------------------------
10804 function Is_Container_Element (Exp : Node_Id) return Boolean is
10805 Loc : constant Source_Ptr := Sloc (Exp);
10806 Pref : constant Node_Id := Prefix (Exp);
10808 Call : Node_Id;
10809 -- Call to an indexing aspect
10811 Cont_Typ : Entity_Id;
10812 -- The type of the container being accessed
10814 Elem_Typ : Entity_Id;
10815 -- Its element type
10817 Indexing : Entity_Id;
10818 Is_Const : Boolean;
10819 -- Indicates that constant indexing is used, and the element is thus
10820 -- a constant.
10822 Ref_Typ : Entity_Id;
10823 -- The reference type returned by the indexing operation
10825 begin
10826 -- If C is a container, in a context that imposes the element type of
10827 -- that container, the indexing notation C (X) is rewritten as:
10829 -- Indexing (C, X).Discr.all
10831 -- where Indexing is one of the indexing aspects of the container.
10832 -- If the context does not require a reference, the construct can be
10833 -- rewritten as
10835 -- Element (C, X)
10837 -- First, verify that the construct has the proper form
10839 if not Expander_Active then
10840 return False;
10842 elsif Nkind (Pref) /= N_Selected_Component then
10843 return False;
10845 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
10846 return False;
10848 else
10849 Call := Prefix (Pref);
10850 Ref_Typ := Etype (Call);
10851 end if;
10853 if not Has_Implicit_Dereference (Ref_Typ)
10854 or else No (First (Parameter_Associations (Call)))
10855 or else not Is_Entity_Name (Name (Call))
10856 then
10857 return False;
10858 end if;
10860 -- Retrieve type of container object, and its iterator aspects
10862 Cont_Typ := Etype (First (Parameter_Associations (Call)));
10863 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
10864 Is_Const := False;
10866 if No (Indexing) then
10868 -- Container should have at least one indexing operation
10870 return False;
10872 elsif Entity (Name (Call)) /= Entity (Indexing) then
10874 -- This may be a variable indexing operation
10876 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
10878 if No (Indexing)
10879 or else Entity (Name (Call)) /= Entity (Indexing)
10880 then
10881 return False;
10882 end if;
10884 else
10885 Is_Const := True;
10886 end if;
10888 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
10890 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
10891 return False;
10892 end if;
10894 -- Check that the expression is not the target of an assignment, in
10895 -- which case the rewriting is not possible.
10897 if not Is_Const then
10898 declare
10899 Par : Node_Id;
10901 begin
10902 Par := Exp;
10903 while Present (Par)
10904 loop
10905 if Nkind (Parent (Par)) = N_Assignment_Statement
10906 and then Par = Name (Parent (Par))
10907 then
10908 return False;
10910 -- A renaming produces a reference, and the transformation
10911 -- does not apply.
10913 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
10914 return False;
10916 elsif Nkind_In
10917 (Nkind (Parent (Par)), N_Function_Call,
10918 N_Procedure_Call_Statement,
10919 N_Entry_Call_Statement)
10920 then
10921 -- Check that the element is not part of an actual for an
10922 -- in-out parameter.
10924 declare
10925 F : Entity_Id;
10926 A : Node_Id;
10928 begin
10929 F := First_Formal (Entity (Name (Parent (Par))));
10930 A := First (Parameter_Associations (Parent (Par)));
10931 while Present (F) loop
10932 if A = Par and then Ekind (F) /= E_In_Parameter then
10933 return False;
10934 end if;
10936 Next_Formal (F);
10937 Next (A);
10938 end loop;
10939 end;
10941 -- E_In_Parameter in a call: element is not modified.
10943 exit;
10944 end if;
10946 Par := Parent (Par);
10947 end loop;
10948 end;
10949 end if;
10951 -- The expression has the proper form and the context requires the
10952 -- element type. Retrieve the Element function of the container and
10953 -- rewrite the construct as a call to it.
10955 declare
10956 Op : Elmt_Id;
10958 begin
10959 Op := First_Elmt (Primitive_Operations (Cont_Typ));
10960 while Present (Op) loop
10961 exit when Chars (Node (Op)) = Name_Element;
10962 Next_Elmt (Op);
10963 end loop;
10965 if No (Op) then
10966 return False;
10968 else
10969 Rewrite (Exp,
10970 Make_Function_Call (Loc,
10971 Name => New_Occurrence_Of (Node (Op), Loc),
10972 Parameter_Associations => Parameter_Associations (Call)));
10973 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
10974 return True;
10975 end if;
10976 end;
10977 end Is_Container_Element;
10979 ----------------------------
10980 -- Is_Contract_Annotation --
10981 ----------------------------
10983 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
10984 begin
10985 return Is_Package_Contract_Annotation (Item)
10986 or else
10987 Is_Subprogram_Contract_Annotation (Item);
10988 end Is_Contract_Annotation;
10990 --------------------------------------
10991 -- Is_Controlling_Limited_Procedure --
10992 --------------------------------------
10994 function Is_Controlling_Limited_Procedure
10995 (Proc_Nam : Entity_Id) return Boolean
10997 Param_Typ : Entity_Id := Empty;
10999 begin
11000 if Ekind (Proc_Nam) = E_Procedure
11001 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
11002 then
11003 Param_Typ := Etype (Parameter_Type (First (
11004 Parameter_Specifications (Parent (Proc_Nam)))));
11006 -- In this case where an Itype was created, the procedure call has been
11007 -- rewritten.
11009 elsif Present (Associated_Node_For_Itype (Proc_Nam))
11010 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
11011 and then
11012 Present (Parameter_Associations
11013 (Associated_Node_For_Itype (Proc_Nam)))
11014 then
11015 Param_Typ :=
11016 Etype (First (Parameter_Associations
11017 (Associated_Node_For_Itype (Proc_Nam))));
11018 end if;
11020 if Present (Param_Typ) then
11021 return
11022 Is_Interface (Param_Typ)
11023 and then Is_Limited_Record (Param_Typ);
11024 end if;
11026 return False;
11027 end Is_Controlling_Limited_Procedure;
11029 -----------------------------
11030 -- Is_CPP_Constructor_Call --
11031 -----------------------------
11033 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
11034 begin
11035 return Nkind (N) = N_Function_Call
11036 and then Is_CPP_Class (Etype (Etype (N)))
11037 and then Is_Constructor (Entity (Name (N)))
11038 and then Is_Imported (Entity (Name (N)));
11039 end Is_CPP_Constructor_Call;
11041 -------------------------
11042 -- Is_Current_Instance --
11043 -------------------------
11045 function Is_Current_Instance (N : Node_Id) return Boolean is
11046 Typ : constant Entity_Id := Entity (N);
11047 P : Node_Id;
11049 begin
11050 -- Simplest case: entity is a concurrent type and we are currently
11051 -- inside the body. This will eventually be expanded into a
11052 -- call to Self (for tasks) or _object (for protected objects).
11054 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
11055 return True;
11057 else
11058 -- Check whether the context is a (sub)type declaration for the
11059 -- type entity.
11061 P := Parent (N);
11062 while Present (P) loop
11063 if Nkind_In (P, N_Full_Type_Declaration,
11064 N_Private_Type_Declaration,
11065 N_Subtype_Declaration)
11066 and then Comes_From_Source (P)
11067 and then Defining_Entity (P) = Typ
11068 then
11069 return True;
11070 end if;
11072 P := Parent (P);
11073 end loop;
11074 end if;
11076 -- In any other context this is not a current occurrence
11078 return False;
11079 end Is_Current_Instance;
11081 --------------------
11082 -- Is_Declaration --
11083 --------------------
11085 function Is_Declaration (N : Node_Id) return Boolean is
11086 begin
11087 case Nkind (N) is
11088 when N_Abstract_Subprogram_Declaration |
11089 N_Exception_Declaration |
11090 N_Exception_Renaming_Declaration |
11091 N_Full_Type_Declaration |
11092 N_Generic_Function_Renaming_Declaration |
11093 N_Generic_Package_Declaration |
11094 N_Generic_Package_Renaming_Declaration |
11095 N_Generic_Procedure_Renaming_Declaration |
11096 N_Generic_Subprogram_Declaration |
11097 N_Number_Declaration |
11098 N_Object_Declaration |
11099 N_Object_Renaming_Declaration |
11100 N_Package_Declaration |
11101 N_Package_Renaming_Declaration |
11102 N_Private_Extension_Declaration |
11103 N_Private_Type_Declaration |
11104 N_Subprogram_Declaration |
11105 N_Subprogram_Renaming_Declaration |
11106 N_Subtype_Declaration =>
11107 return True;
11109 when others =>
11110 return False;
11111 end case;
11112 end Is_Declaration;
11114 -----------------
11115 -- Is_Delegate --
11116 -----------------
11118 function Is_Delegate (T : Entity_Id) return Boolean is
11119 Desig_Type : Entity_Id;
11121 begin
11122 if VM_Target /= CLI_Target then
11123 return False;
11124 end if;
11126 -- Access-to-subprograms are delegates in CIL
11128 if Ekind (T) = E_Access_Subprogram_Type then
11129 return True;
11130 end if;
11132 if not Is_Access_Type (T) then
11134 -- A delegate is a managed pointer. If no designated type is defined
11135 -- it means that it's not a delegate.
11137 return False;
11138 end if;
11140 Desig_Type := Etype (Directly_Designated_Type (T));
11142 if not Is_Tagged_Type (Desig_Type) then
11143 return False;
11144 end if;
11146 -- Test if the type is inherited from [mscorlib]System.Delegate
11148 while Etype (Desig_Type) /= Desig_Type loop
11149 if Chars (Scope (Desig_Type)) /= No_Name
11150 and then Is_Imported (Scope (Desig_Type))
11151 and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
11152 then
11153 return True;
11154 end if;
11156 Desig_Type := Etype (Desig_Type);
11157 end loop;
11159 return False;
11160 end Is_Delegate;
11162 ----------------------------------------------
11163 -- Is_Dependent_Component_Of_Mutable_Object --
11164 ----------------------------------------------
11166 function Is_Dependent_Component_Of_Mutable_Object
11167 (Object : Node_Id) return Boolean
11169 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
11170 -- Returns True if and only if Comp is declared within a variant part
11172 --------------------------------
11173 -- Is_Declared_Within_Variant --
11174 --------------------------------
11176 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
11177 Comp_Decl : constant Node_Id := Parent (Comp);
11178 Comp_List : constant Node_Id := Parent (Comp_Decl);
11179 begin
11180 return Nkind (Parent (Comp_List)) = N_Variant;
11181 end Is_Declared_Within_Variant;
11183 P : Node_Id;
11184 Prefix_Type : Entity_Id;
11185 P_Aliased : Boolean := False;
11186 Comp : Entity_Id;
11188 Deref : Node_Id := Object;
11189 -- Dereference node, in something like X.all.Y(2)
11191 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
11193 begin
11194 -- Find the dereference node if any
11196 while Nkind_In (Deref, N_Indexed_Component,
11197 N_Selected_Component,
11198 N_Slice)
11199 loop
11200 Deref := Prefix (Deref);
11201 end loop;
11203 -- Ada 2005: If we have a component or slice of a dereference,
11204 -- something like X.all.Y (2), and the type of X is access-to-constant,
11205 -- Is_Variable will return False, because it is indeed a constant
11206 -- view. But it might be a view of a variable object, so we want the
11207 -- following condition to be True in that case.
11209 if Is_Variable (Object)
11210 or else (Ada_Version >= Ada_2005
11211 and then Nkind (Deref) = N_Explicit_Dereference)
11212 then
11213 if Nkind (Object) = N_Selected_Component then
11214 P := Prefix (Object);
11215 Prefix_Type := Etype (P);
11217 if Is_Entity_Name (P) then
11218 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
11219 Prefix_Type := Base_Type (Prefix_Type);
11220 end if;
11222 if Is_Aliased (Entity (P)) then
11223 P_Aliased := True;
11224 end if;
11226 -- A discriminant check on a selected component may be expanded
11227 -- into a dereference when removing side-effects. Recover the
11228 -- original node and its type, which may be unconstrained.
11230 elsif Nkind (P) = N_Explicit_Dereference
11231 and then not (Comes_From_Source (P))
11232 then
11233 P := Original_Node (P);
11234 Prefix_Type := Etype (P);
11236 else
11237 -- Check for prefix being an aliased component???
11239 null;
11241 end if;
11243 -- A heap object is constrained by its initial value
11245 -- Ada 2005 (AI-363): Always assume the object could be mutable in
11246 -- the dereferenced case, since the access value might denote an
11247 -- unconstrained aliased object, whereas in Ada 95 the designated
11248 -- object is guaranteed to be constrained. A worst-case assumption
11249 -- has to apply in Ada 2005 because we can't tell at compile
11250 -- time whether the object is "constrained by its initial value"
11251 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
11252 -- rules (these rules are acknowledged to need fixing).
11254 if Ada_Version < Ada_2005 then
11255 if Is_Access_Type (Prefix_Type)
11256 or else Nkind (P) = N_Explicit_Dereference
11257 then
11258 return False;
11259 end if;
11261 else pragma Assert (Ada_Version >= Ada_2005);
11262 if Is_Access_Type (Prefix_Type) then
11264 -- If the access type is pool-specific, and there is no
11265 -- constrained partial view of the designated type, then the
11266 -- designated object is known to be constrained.
11268 if Ekind (Prefix_Type) = E_Access_Type
11269 and then not Object_Type_Has_Constrained_Partial_View
11270 (Typ => Designated_Type (Prefix_Type),
11271 Scop => Current_Scope)
11272 then
11273 return False;
11275 -- Otherwise (general access type, or there is a constrained
11276 -- partial view of the designated type), we need to check
11277 -- based on the designated type.
11279 else
11280 Prefix_Type := Designated_Type (Prefix_Type);
11281 end if;
11282 end if;
11283 end if;
11285 Comp :=
11286 Original_Record_Component (Entity (Selector_Name (Object)));
11288 -- As per AI-0017, the renaming is illegal in a generic body, even
11289 -- if the subtype is indefinite.
11291 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
11293 if not Is_Constrained (Prefix_Type)
11294 and then (Is_Definite_Subtype (Prefix_Type)
11295 or else
11296 (Is_Generic_Type (Prefix_Type)
11297 and then Ekind (Current_Scope) = E_Generic_Package
11298 and then In_Package_Body (Current_Scope)))
11300 and then (Is_Declared_Within_Variant (Comp)
11301 or else Has_Discriminant_Dependent_Constraint (Comp))
11302 and then (not P_Aliased or else Ada_Version >= Ada_2005)
11303 then
11304 return True;
11306 -- If the prefix is of an access type at this point, then we want
11307 -- to return False, rather than calling this function recursively
11308 -- on the access object (which itself might be a discriminant-
11309 -- dependent component of some other object, but that isn't
11310 -- relevant to checking the object passed to us). This avoids
11311 -- issuing wrong errors when compiling with -gnatc, where there
11312 -- can be implicit dereferences that have not been expanded.
11314 elsif Is_Access_Type (Etype (Prefix (Object))) then
11315 return False;
11317 else
11318 return
11319 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
11320 end if;
11322 elsif Nkind (Object) = N_Indexed_Component
11323 or else Nkind (Object) = N_Slice
11324 then
11325 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
11327 -- A type conversion that Is_Variable is a view conversion:
11328 -- go back to the denoted object.
11330 elsif Nkind (Object) = N_Type_Conversion then
11331 return
11332 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
11333 end if;
11334 end if;
11336 return False;
11337 end Is_Dependent_Component_Of_Mutable_Object;
11339 ---------------------
11340 -- Is_Dereferenced --
11341 ---------------------
11343 function Is_Dereferenced (N : Node_Id) return Boolean is
11344 P : constant Node_Id := Parent (N);
11345 begin
11346 return Nkind_In (P, N_Selected_Component,
11347 N_Explicit_Dereference,
11348 N_Indexed_Component,
11349 N_Slice)
11350 and then Prefix (P) = N;
11351 end Is_Dereferenced;
11353 ----------------------
11354 -- Is_Descendent_Of --
11355 ----------------------
11357 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
11358 T : Entity_Id;
11359 Etyp : Entity_Id;
11361 begin
11362 pragma Assert (Nkind (T1) in N_Entity);
11363 pragma Assert (Nkind (T2) in N_Entity);
11365 T := Base_Type (T1);
11367 -- Immediate return if the types match
11369 if T = T2 then
11370 return True;
11372 -- Comment needed here ???
11374 elsif Ekind (T) = E_Class_Wide_Type then
11375 return Etype (T) = T2;
11377 -- All other cases
11379 else
11380 loop
11381 Etyp := Etype (T);
11383 -- Done if we found the type we are looking for
11385 if Etyp = T2 then
11386 return True;
11388 -- Done if no more derivations to check
11390 elsif T = T1
11391 or else T = Etyp
11392 then
11393 return False;
11395 -- Following test catches error cases resulting from prev errors
11397 elsif No (Etyp) then
11398 return False;
11400 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
11401 return False;
11403 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
11404 return False;
11405 end if;
11407 T := Base_Type (Etyp);
11408 end loop;
11409 end if;
11410 end Is_Descendent_Of;
11412 ---------------------------------------------
11413 -- Is_Double_Precision_Floating_Point_Type --
11414 ---------------------------------------------
11416 function Is_Double_Precision_Floating_Point_Type
11417 (E : Entity_Id) return Boolean is
11418 begin
11419 return Is_Floating_Point_Type (E)
11420 and then Machine_Radix_Value (E) = Uint_2
11421 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
11422 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
11423 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
11424 end Is_Double_Precision_Floating_Point_Type;
11426 -----------------------------
11427 -- Is_Effectively_Volatile --
11428 -----------------------------
11430 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
11431 begin
11432 if Is_Type (Id) then
11434 -- An arbitrary type is effectively volatile when it is subject to
11435 -- pragma Atomic or Volatile.
11437 if Is_Volatile (Id) then
11438 return True;
11440 -- An array type is effectively volatile when it is subject to pragma
11441 -- Atomic_Components or Volatile_Components or its compolent type is
11442 -- effectively volatile.
11444 elsif Is_Array_Type (Id) then
11445 return
11446 Has_Volatile_Components (Id)
11447 or else
11448 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
11450 else
11451 return False;
11452 end if;
11454 -- Otherwise Id denotes an object
11456 else
11457 return
11458 Is_Volatile (Id)
11459 or else Has_Volatile_Components (Id)
11460 or else Is_Effectively_Volatile (Etype (Id));
11461 end if;
11462 end Is_Effectively_Volatile;
11464 ------------------------------------
11465 -- Is_Effectively_Volatile_Object --
11466 ------------------------------------
11468 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
11469 begin
11470 if Is_Entity_Name (N) then
11471 return Is_Effectively_Volatile (Entity (N));
11473 elsif Nkind (N) = N_Expanded_Name then
11474 return Is_Effectively_Volatile (Entity (N));
11476 elsif Nkind (N) = N_Indexed_Component then
11477 return Is_Effectively_Volatile_Object (Prefix (N));
11479 elsif Nkind (N) = N_Selected_Component then
11480 return
11481 Is_Effectively_Volatile_Object (Prefix (N))
11482 or else
11483 Is_Effectively_Volatile_Object (Selector_Name (N));
11485 else
11486 return False;
11487 end if;
11488 end Is_Effectively_Volatile_Object;
11490 ----------------------------
11491 -- Is_Expression_Function --
11492 ----------------------------
11494 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
11495 Decl : Node_Id;
11497 begin
11498 if Ekind (Subp) /= E_Function then
11499 return False;
11501 else
11502 Decl := Unit_Declaration_Node (Subp);
11503 return Nkind (Decl) = N_Subprogram_Declaration
11504 and then
11505 (Nkind (Original_Node (Decl)) = N_Expression_Function
11506 or else
11507 (Present (Corresponding_Body (Decl))
11508 and then
11509 Nkind (Original_Node
11510 (Unit_Declaration_Node
11511 (Corresponding_Body (Decl)))) =
11512 N_Expression_Function));
11513 end if;
11514 end Is_Expression_Function;
11516 -----------------------
11517 -- Is_EVF_Expression --
11518 -----------------------
11520 function Is_EVF_Expression (N : Node_Id) return Boolean is
11521 Orig_N : constant Node_Id := Original_Node (N);
11522 Alt : Node_Id;
11523 Expr : Node_Id;
11524 Id : Entity_Id;
11526 begin
11527 -- Detect a reference to a formal parameter of a specific tagged type
11528 -- whose related subprogram is subject to pragma Expresions_Visible with
11529 -- value "False".
11531 if Is_Entity_Name (N) and then Present (Entity (N)) then
11532 Id := Entity (N);
11534 return
11535 Is_Formal (Id)
11536 and then Is_Specific_Tagged_Type (Etype (Id))
11537 and then Extensions_Visible_Status (Id) =
11538 Extensions_Visible_False;
11540 -- A case expression is an EVF expression when it contains at least one
11541 -- EVF dependent_expression. Note that a case expression may have been
11542 -- expanded, hence the use of Original_Node.
11544 elsif Nkind (Orig_N) = N_Case_Expression then
11545 Alt := First (Alternatives (Orig_N));
11546 while Present (Alt) loop
11547 if Is_EVF_Expression (Expression (Alt)) then
11548 return True;
11549 end if;
11551 Next (Alt);
11552 end loop;
11554 -- An if expression is an EVF expression when it contains at least one
11555 -- EVF dependent_expression. Note that an if expression may have been
11556 -- expanded, hence the use of Original_Node.
11558 elsif Nkind (Orig_N) = N_If_Expression then
11559 Expr := Next (First (Expressions (Orig_N)));
11560 while Present (Expr) loop
11561 if Is_EVF_Expression (Expr) then
11562 return True;
11563 end if;
11565 Next (Expr);
11566 end loop;
11568 -- A qualified expression or a type conversion is an EVF expression when
11569 -- its operand is an EVF expression.
11571 elsif Nkind_In (N, N_Qualified_Expression,
11572 N_Unchecked_Type_Conversion,
11573 N_Type_Conversion)
11574 then
11575 return Is_EVF_Expression (Expression (N));
11577 -- Attributes 'Loop_Entry, 'Old and 'Update are an EVF expression when
11578 -- their prefix denotes an EVF expression.
11580 elsif Nkind (N) = N_Attribute_Reference
11581 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
11582 Name_Old,
11583 Name_Update)
11584 then
11585 return Is_EVF_Expression (Prefix (N));
11586 end if;
11588 return False;
11589 end Is_EVF_Expression;
11591 --------------
11592 -- Is_False --
11593 --------------
11595 function Is_False (U : Uint) return Boolean is
11596 begin
11597 return (U = 0);
11598 end Is_False;
11600 ---------------------------
11601 -- Is_Fixed_Model_Number --
11602 ---------------------------
11604 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
11605 S : constant Ureal := Small_Value (T);
11606 M : Urealp.Save_Mark;
11607 R : Boolean;
11608 begin
11609 M := Urealp.Mark;
11610 R := (U = UR_Trunc (U / S) * S);
11611 Urealp.Release (M);
11612 return R;
11613 end Is_Fixed_Model_Number;
11615 -------------------------------
11616 -- Is_Fully_Initialized_Type --
11617 -------------------------------
11619 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
11620 begin
11621 -- Scalar types
11623 if Is_Scalar_Type (Typ) then
11625 -- A scalar type with an aspect Default_Value is fully initialized
11627 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
11628 -- of a scalar type, but we don't take that into account here, since
11629 -- we don't want these to affect warnings.
11631 return Has_Default_Aspect (Typ);
11633 elsif Is_Access_Type (Typ) then
11634 return True;
11636 elsif Is_Array_Type (Typ) then
11637 if Is_Fully_Initialized_Type (Component_Type (Typ))
11638 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
11639 then
11640 return True;
11641 end if;
11643 -- An interesting case, if we have a constrained type one of whose
11644 -- bounds is known to be null, then there are no elements to be
11645 -- initialized, so all the elements are initialized.
11647 if Is_Constrained (Typ) then
11648 declare
11649 Indx : Node_Id;
11650 Indx_Typ : Entity_Id;
11651 Lbd, Hbd : Node_Id;
11653 begin
11654 Indx := First_Index (Typ);
11655 while Present (Indx) loop
11656 if Etype (Indx) = Any_Type then
11657 return False;
11659 -- If index is a range, use directly
11661 elsif Nkind (Indx) = N_Range then
11662 Lbd := Low_Bound (Indx);
11663 Hbd := High_Bound (Indx);
11665 else
11666 Indx_Typ := Etype (Indx);
11668 if Is_Private_Type (Indx_Typ) then
11669 Indx_Typ := Full_View (Indx_Typ);
11670 end if;
11672 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
11673 return False;
11674 else
11675 Lbd := Type_Low_Bound (Indx_Typ);
11676 Hbd := Type_High_Bound (Indx_Typ);
11677 end if;
11678 end if;
11680 if Compile_Time_Known_Value (Lbd)
11681 and then
11682 Compile_Time_Known_Value (Hbd)
11683 then
11684 if Expr_Value (Hbd) < Expr_Value (Lbd) then
11685 return True;
11686 end if;
11687 end if;
11689 Next_Index (Indx);
11690 end loop;
11691 end;
11692 end if;
11694 -- If no null indexes, then type is not fully initialized
11696 return False;
11698 -- Record types
11700 elsif Is_Record_Type (Typ) then
11701 if Has_Discriminants (Typ)
11702 and then
11703 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
11704 and then Is_Fully_Initialized_Variant (Typ)
11705 then
11706 return True;
11707 end if;
11709 -- We consider bounded string types to be fully initialized, because
11710 -- otherwise we get false alarms when the Data component is not
11711 -- default-initialized.
11713 if Is_Bounded_String (Typ) then
11714 return True;
11715 end if;
11717 -- Controlled records are considered to be fully initialized if
11718 -- there is a user defined Initialize routine. This may not be
11719 -- entirely correct, but as the spec notes, we are guessing here
11720 -- what is best from the point of view of issuing warnings.
11722 if Is_Controlled (Typ) then
11723 declare
11724 Utyp : constant Entity_Id := Underlying_Type (Typ);
11726 begin
11727 if Present (Utyp) then
11728 declare
11729 Init : constant Entity_Id :=
11730 (Find_Optional_Prim_Op
11731 (Underlying_Type (Typ), Name_Initialize));
11733 begin
11734 if Present (Init)
11735 and then Comes_From_Source (Init)
11736 and then not
11737 Is_Predefined_File_Name
11738 (File_Name (Get_Source_File_Index (Sloc (Init))))
11739 then
11740 return True;
11742 elsif Has_Null_Extension (Typ)
11743 and then
11744 Is_Fully_Initialized_Type
11745 (Etype (Base_Type (Typ)))
11746 then
11747 return True;
11748 end if;
11749 end;
11750 end if;
11751 end;
11752 end if;
11754 -- Otherwise see if all record components are initialized
11756 declare
11757 Ent : Entity_Id;
11759 begin
11760 Ent := First_Entity (Typ);
11761 while Present (Ent) loop
11762 if Ekind (Ent) = E_Component
11763 and then (No (Parent (Ent))
11764 or else No (Expression (Parent (Ent))))
11765 and then not Is_Fully_Initialized_Type (Etype (Ent))
11767 -- Special VM case for tag components, which need to be
11768 -- defined in this case, but are never initialized as VMs
11769 -- are using other dispatching mechanisms. Ignore this
11770 -- uninitialized case. Note that this applies both to the
11771 -- uTag entry and the main vtable pointer (CPP_Class case).
11773 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
11774 then
11775 return False;
11776 end if;
11778 Next_Entity (Ent);
11779 end loop;
11780 end;
11782 -- No uninitialized components, so type is fully initialized.
11783 -- Note that this catches the case of no components as well.
11785 return True;
11787 elsif Is_Concurrent_Type (Typ) then
11788 return True;
11790 elsif Is_Private_Type (Typ) then
11791 declare
11792 U : constant Entity_Id := Underlying_Type (Typ);
11794 begin
11795 if No (U) then
11796 return False;
11797 else
11798 return Is_Fully_Initialized_Type (U);
11799 end if;
11800 end;
11802 else
11803 return False;
11804 end if;
11805 end Is_Fully_Initialized_Type;
11807 ----------------------------------
11808 -- Is_Fully_Initialized_Variant --
11809 ----------------------------------
11811 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
11812 Loc : constant Source_Ptr := Sloc (Typ);
11813 Constraints : constant List_Id := New_List;
11814 Components : constant Elist_Id := New_Elmt_List;
11815 Comp_Elmt : Elmt_Id;
11816 Comp_Id : Node_Id;
11817 Comp_List : Node_Id;
11818 Discr : Entity_Id;
11819 Discr_Val : Node_Id;
11821 Report_Errors : Boolean;
11822 pragma Warnings (Off, Report_Errors);
11824 begin
11825 if Serious_Errors_Detected > 0 then
11826 return False;
11827 end if;
11829 if Is_Record_Type (Typ)
11830 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11831 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
11832 then
11833 Comp_List := Component_List (Type_Definition (Parent (Typ)));
11835 Discr := First_Discriminant (Typ);
11836 while Present (Discr) loop
11837 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
11838 Discr_Val := Expression (Parent (Discr));
11840 if Present (Discr_Val)
11841 and then Is_OK_Static_Expression (Discr_Val)
11842 then
11843 Append_To (Constraints,
11844 Make_Component_Association (Loc,
11845 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
11846 Expression => New_Copy (Discr_Val)));
11847 else
11848 return False;
11849 end if;
11850 else
11851 return False;
11852 end if;
11854 Next_Discriminant (Discr);
11855 end loop;
11857 Gather_Components
11858 (Typ => Typ,
11859 Comp_List => Comp_List,
11860 Governed_By => Constraints,
11861 Into => Components,
11862 Report_Errors => Report_Errors);
11864 -- Check that each component present is fully initialized
11866 Comp_Elmt := First_Elmt (Components);
11867 while Present (Comp_Elmt) loop
11868 Comp_Id := Node (Comp_Elmt);
11870 if Ekind (Comp_Id) = E_Component
11871 and then (No (Parent (Comp_Id))
11872 or else No (Expression (Parent (Comp_Id))))
11873 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
11874 then
11875 return False;
11876 end if;
11878 Next_Elmt (Comp_Elmt);
11879 end loop;
11881 return True;
11883 elsif Is_Private_Type (Typ) then
11884 declare
11885 U : constant Entity_Id := Underlying_Type (Typ);
11887 begin
11888 if No (U) then
11889 return False;
11890 else
11891 return Is_Fully_Initialized_Variant (U);
11892 end if;
11893 end;
11895 else
11896 return False;
11897 end if;
11898 end Is_Fully_Initialized_Variant;
11900 ------------------------------------
11901 -- Is_Generic_Declaration_Or_Body --
11902 ------------------------------------
11904 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
11905 Spec_Decl : Node_Id;
11907 begin
11908 -- Package/subprogram body
11910 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
11911 and then Present (Corresponding_Spec (Decl))
11912 then
11913 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
11915 -- Package/subprogram body stub
11917 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
11918 and then Present (Corresponding_Spec_Of_Stub (Decl))
11919 then
11920 Spec_Decl :=
11921 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
11923 -- All other cases
11925 else
11926 Spec_Decl := Decl;
11927 end if;
11929 -- Rather than inspecting the defining entity of the spec declaration,
11930 -- look at its Nkind. This takes care of the case where the analysis of
11931 -- a generic body modifies the Ekind of its spec to allow for recursive
11932 -- calls.
11934 return
11935 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
11936 N_Generic_Subprogram_Declaration);
11937 end Is_Generic_Declaration_Or_Body;
11939 ----------------------------
11940 -- Is_Inherited_Operation --
11941 ----------------------------
11943 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
11944 pragma Assert (Is_Overloadable (E));
11945 Kind : constant Node_Kind := Nkind (Parent (E));
11946 begin
11947 return Kind = N_Full_Type_Declaration
11948 or else Kind = N_Private_Extension_Declaration
11949 or else Kind = N_Subtype_Declaration
11950 or else (Ekind (E) = E_Enumeration_Literal
11951 and then Is_Derived_Type (Etype (E)));
11952 end Is_Inherited_Operation;
11954 -------------------------------------
11955 -- Is_Inherited_Operation_For_Type --
11956 -------------------------------------
11958 function Is_Inherited_Operation_For_Type
11959 (E : Entity_Id;
11960 Typ : Entity_Id) return Boolean
11962 begin
11963 -- Check that the operation has been created by the type declaration
11965 return Is_Inherited_Operation (E)
11966 and then Defining_Identifier (Parent (E)) = Typ;
11967 end Is_Inherited_Operation_For_Type;
11969 -----------------
11970 -- Is_Iterator --
11971 -----------------
11973 function Is_Iterator (Typ : Entity_Id) return Boolean is
11974 Ifaces_List : Elist_Id;
11975 Iface_Elmt : Elmt_Id;
11976 Iface : Entity_Id;
11978 begin
11979 if Is_Class_Wide_Type (Typ)
11980 and then Nam_In (Chars (Etype (Typ)), Name_Forward_Iterator,
11981 Name_Reversible_Iterator)
11982 and then
11983 Is_Predefined_File_Name
11984 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
11985 then
11986 return True;
11988 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
11989 return False;
11991 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
11992 return True;
11994 else
11995 Collect_Interfaces (Typ, Ifaces_List);
11997 Iface_Elmt := First_Elmt (Ifaces_List);
11998 while Present (Iface_Elmt) loop
11999 Iface := Node (Iface_Elmt);
12000 if Chars (Iface) = Name_Forward_Iterator
12001 and then
12002 Is_Predefined_File_Name
12003 (Unit_File_Name (Get_Source_Unit (Iface)))
12004 then
12005 return True;
12006 end if;
12008 Next_Elmt (Iface_Elmt);
12009 end loop;
12011 return False;
12012 end if;
12013 end Is_Iterator;
12015 ------------
12016 -- Is_LHS --
12017 ------------
12019 -- We seem to have a lot of overlapping functions that do similar things
12020 -- (testing for left hand sides or lvalues???).
12022 function Is_LHS (N : Node_Id) return Is_LHS_Result is
12023 P : constant Node_Id := Parent (N);
12025 begin
12026 -- Return True if we are the left hand side of an assignment statement
12028 if Nkind (P) = N_Assignment_Statement then
12029 if Name (P) = N then
12030 return Yes;
12031 else
12032 return No;
12033 end if;
12035 -- Case of prefix of indexed or selected component or slice
12037 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
12038 and then N = Prefix (P)
12039 then
12040 -- Here we have the case where the parent P is N.Q or N(Q .. R).
12041 -- If P is an LHS, then N is also effectively an LHS, but there
12042 -- is an important exception. If N is of an access type, then
12043 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
12044 -- case this makes N.all a left hand side but not N itself.
12046 -- If we don't know the type yet, this is the case where we return
12047 -- Unknown, since the answer depends on the type which is unknown.
12049 if No (Etype (N)) then
12050 return Unknown;
12052 -- We have an Etype set, so we can check it
12054 elsif Is_Access_Type (Etype (N)) then
12055 return No;
12057 -- OK, not access type case, so just test whole expression
12059 else
12060 return Is_LHS (P);
12061 end if;
12063 -- All other cases are not left hand sides
12065 else
12066 return No;
12067 end if;
12068 end Is_LHS;
12070 -----------------------------
12071 -- Is_Library_Level_Entity --
12072 -----------------------------
12074 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
12075 begin
12076 -- The following is a small optimization, and it also properly handles
12077 -- discriminals, which in task bodies might appear in expressions before
12078 -- the corresponding procedure has been created, and which therefore do
12079 -- not have an assigned scope.
12081 if Is_Formal (E) then
12082 return False;
12083 end if;
12085 -- Normal test is simply that the enclosing dynamic scope is Standard
12087 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
12088 end Is_Library_Level_Entity;
12090 --------------------------------
12091 -- Is_Limited_Class_Wide_Type --
12092 --------------------------------
12094 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
12095 begin
12096 return
12097 Is_Class_Wide_Type (Typ)
12098 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
12099 end Is_Limited_Class_Wide_Type;
12101 ---------------------------------
12102 -- Is_Local_Variable_Reference --
12103 ---------------------------------
12105 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
12106 begin
12107 if not Is_Entity_Name (Expr) then
12108 return False;
12110 else
12111 declare
12112 Ent : constant Entity_Id := Entity (Expr);
12113 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
12114 begin
12115 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
12116 return False;
12117 else
12118 return Present (Sub) and then Sub = Current_Subprogram;
12119 end if;
12120 end;
12121 end if;
12122 end Is_Local_Variable_Reference;
12124 -------------------------
12125 -- Is_Object_Reference --
12126 -------------------------
12128 function Is_Object_Reference (N : Node_Id) return Boolean is
12130 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
12131 -- Determine whether N is the name of an internally-generated renaming
12133 --------------------------------------
12134 -- Is_Internally_Generated_Renaming --
12135 --------------------------------------
12137 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
12138 P : Node_Id;
12140 begin
12141 P := N;
12142 while Present (P) loop
12143 if Nkind (P) = N_Object_Renaming_Declaration then
12144 return not Comes_From_Source (P);
12145 elsif Is_List_Member (P) then
12146 return False;
12147 end if;
12149 P := Parent (P);
12150 end loop;
12152 return False;
12153 end Is_Internally_Generated_Renaming;
12155 -- Start of processing for Is_Object_Reference
12157 begin
12158 if Is_Entity_Name (N) then
12159 return Present (Entity (N)) and then Is_Object (Entity (N));
12161 else
12162 case Nkind (N) is
12163 when N_Indexed_Component | N_Slice =>
12164 return
12165 Is_Object_Reference (Prefix (N))
12166 or else Is_Access_Type (Etype (Prefix (N)));
12168 -- In Ada 95, a function call is a constant object; a procedure
12169 -- call is not.
12171 when N_Function_Call =>
12172 return Etype (N) /= Standard_Void_Type;
12174 -- Attributes 'Input, 'Old and 'Result produce objects
12176 when N_Attribute_Reference =>
12177 return
12178 Nam_In
12179 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
12181 when N_Selected_Component =>
12182 return
12183 Is_Object_Reference (Selector_Name (N))
12184 and then
12185 (Is_Object_Reference (Prefix (N))
12186 or else Is_Access_Type (Etype (Prefix (N))));
12188 when N_Explicit_Dereference =>
12189 return True;
12191 -- A view conversion of a tagged object is an object reference
12193 when N_Type_Conversion =>
12194 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
12195 and then Is_Tagged_Type (Etype (Expression (N)))
12196 and then Is_Object_Reference (Expression (N));
12198 -- An unchecked type conversion is considered to be an object if
12199 -- the operand is an object (this construction arises only as a
12200 -- result of expansion activities).
12202 when N_Unchecked_Type_Conversion =>
12203 return True;
12205 -- Allow string literals to act as objects as long as they appear
12206 -- in internally-generated renamings. The expansion of iterators
12207 -- may generate such renamings when the range involves a string
12208 -- literal.
12210 when N_String_Literal =>
12211 return Is_Internally_Generated_Renaming (Parent (N));
12213 -- AI05-0003: In Ada 2012 a qualified expression is a name.
12214 -- This allows disambiguation of function calls and the use
12215 -- of aggregates in more contexts.
12217 when N_Qualified_Expression =>
12218 if Ada_Version < Ada_2012 then
12219 return False;
12220 else
12221 return Is_Object_Reference (Expression (N))
12222 or else Nkind (Expression (N)) = N_Aggregate;
12223 end if;
12225 when others =>
12226 return False;
12227 end case;
12228 end if;
12229 end Is_Object_Reference;
12231 -----------------------------------
12232 -- Is_OK_Variable_For_Out_Formal --
12233 -----------------------------------
12235 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
12236 begin
12237 Note_Possible_Modification (AV, Sure => True);
12239 -- We must reject parenthesized variable names. Comes_From_Source is
12240 -- checked because there are currently cases where the compiler violates
12241 -- this rule (e.g. passing a task object to its controlled Initialize
12242 -- routine). This should be properly documented in sinfo???
12244 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
12245 return False;
12247 -- A variable is always allowed
12249 elsif Is_Variable (AV) then
12250 return True;
12252 -- Generalized indexing operations are rewritten as explicit
12253 -- dereferences, and it is only during resolution that we can
12254 -- check whether the context requires an access_to_variable type.
12256 elsif Nkind (AV) = N_Explicit_Dereference
12257 and then Ada_Version >= Ada_2012
12258 and then Nkind (Original_Node (AV)) = N_Indexed_Component
12259 and then Present (Etype (Original_Node (AV)))
12260 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
12261 then
12262 return not Is_Access_Constant (Etype (Prefix (AV)));
12264 -- Unchecked conversions are allowed only if they come from the
12265 -- generated code, which sometimes uses unchecked conversions for out
12266 -- parameters in cases where code generation is unaffected. We tell
12267 -- source unchecked conversions by seeing if they are rewrites of
12268 -- an original Unchecked_Conversion function call, or of an explicit
12269 -- conversion of a function call or an aggregate (as may happen in the
12270 -- expansion of a packed array aggregate).
12272 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
12273 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
12274 return False;
12276 elsif Comes_From_Source (AV)
12277 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
12278 then
12279 return False;
12281 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
12282 return Is_OK_Variable_For_Out_Formal (Expression (AV));
12284 else
12285 return True;
12286 end if;
12288 -- Normal type conversions are allowed if argument is a variable
12290 elsif Nkind (AV) = N_Type_Conversion then
12291 if Is_Variable (Expression (AV))
12292 and then Paren_Count (Expression (AV)) = 0
12293 then
12294 Note_Possible_Modification (Expression (AV), Sure => True);
12295 return True;
12297 -- We also allow a non-parenthesized expression that raises
12298 -- constraint error if it rewrites what used to be a variable
12300 elsif Raises_Constraint_Error (Expression (AV))
12301 and then Paren_Count (Expression (AV)) = 0
12302 and then Is_Variable (Original_Node (Expression (AV)))
12303 then
12304 return True;
12306 -- Type conversion of something other than a variable
12308 else
12309 return False;
12310 end if;
12312 -- If this node is rewritten, then test the original form, if that is
12313 -- OK, then we consider the rewritten node OK (for example, if the
12314 -- original node is a conversion, then Is_Variable will not be true
12315 -- but we still want to allow the conversion if it converts a variable).
12317 elsif Original_Node (AV) /= AV then
12319 -- In Ada 2012, the explicit dereference may be a rewritten call to a
12320 -- Reference function.
12322 if Ada_Version >= Ada_2012
12323 and then Nkind (Original_Node (AV)) = N_Function_Call
12324 and then
12325 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
12326 then
12327 return True;
12329 else
12330 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
12331 end if;
12333 -- All other non-variables are rejected
12335 else
12336 return False;
12337 end if;
12338 end Is_OK_Variable_For_Out_Formal;
12340 ------------------------------------
12341 -- Is_Package_Contract_Annotation --
12342 ------------------------------------
12344 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
12345 Nam : Name_Id;
12347 begin
12348 if Nkind (Item) = N_Aspect_Specification then
12349 Nam := Chars (Identifier (Item));
12351 else pragma Assert (Nkind (Item) = N_Pragma);
12352 Nam := Pragma_Name (Item);
12353 end if;
12355 return Nam = Name_Abstract_State
12356 or else Nam = Name_Initial_Condition
12357 or else Nam = Name_Initializes
12358 or else Nam = Name_Refined_State;
12359 end Is_Package_Contract_Annotation;
12361 -----------------------------------
12362 -- Is_Partially_Initialized_Type --
12363 -----------------------------------
12365 function Is_Partially_Initialized_Type
12366 (Typ : Entity_Id;
12367 Include_Implicit : Boolean := True) return Boolean
12369 begin
12370 if Is_Scalar_Type (Typ) then
12371 return False;
12373 elsif Is_Access_Type (Typ) then
12374 return Include_Implicit;
12376 elsif Is_Array_Type (Typ) then
12378 -- If component type is partially initialized, so is array type
12380 if Is_Partially_Initialized_Type
12381 (Component_Type (Typ), Include_Implicit)
12382 then
12383 return True;
12385 -- Otherwise we are only partially initialized if we are fully
12386 -- initialized (this is the empty array case, no point in us
12387 -- duplicating that code here).
12389 else
12390 return Is_Fully_Initialized_Type (Typ);
12391 end if;
12393 elsif Is_Record_Type (Typ) then
12395 -- A discriminated type is always partially initialized if in
12396 -- all mode
12398 if Has_Discriminants (Typ) and then Include_Implicit then
12399 return True;
12401 -- A tagged type is always partially initialized
12403 elsif Is_Tagged_Type (Typ) then
12404 return True;
12406 -- Case of non-discriminated record
12408 else
12409 declare
12410 Ent : Entity_Id;
12412 Component_Present : Boolean := False;
12413 -- Set True if at least one component is present. If no
12414 -- components are present, then record type is fully
12415 -- initialized (another odd case, like the null array).
12417 begin
12418 -- Loop through components
12420 Ent := First_Entity (Typ);
12421 while Present (Ent) loop
12422 if Ekind (Ent) = E_Component then
12423 Component_Present := True;
12425 -- If a component has an initialization expression then
12426 -- the enclosing record type is partially initialized
12428 if Present (Parent (Ent))
12429 and then Present (Expression (Parent (Ent)))
12430 then
12431 return True;
12433 -- If a component is of a type which is itself partially
12434 -- initialized, then the enclosing record type is also.
12436 elsif Is_Partially_Initialized_Type
12437 (Etype (Ent), Include_Implicit)
12438 then
12439 return True;
12440 end if;
12441 end if;
12443 Next_Entity (Ent);
12444 end loop;
12446 -- No initialized components found. If we found any components
12447 -- they were all uninitialized so the result is false.
12449 if Component_Present then
12450 return False;
12452 -- But if we found no components, then all the components are
12453 -- initialized so we consider the type to be initialized.
12455 else
12456 return True;
12457 end if;
12458 end;
12459 end if;
12461 -- Concurrent types are always fully initialized
12463 elsif Is_Concurrent_Type (Typ) then
12464 return True;
12466 -- For a private type, go to underlying type. If there is no underlying
12467 -- type then just assume this partially initialized. Not clear if this
12468 -- can happen in a non-error case, but no harm in testing for this.
12470 elsif Is_Private_Type (Typ) then
12471 declare
12472 U : constant Entity_Id := Underlying_Type (Typ);
12473 begin
12474 if No (U) then
12475 return True;
12476 else
12477 return Is_Partially_Initialized_Type (U, Include_Implicit);
12478 end if;
12479 end;
12481 -- For any other type (are there any?) assume partially initialized
12483 else
12484 return True;
12485 end if;
12486 end Is_Partially_Initialized_Type;
12488 ------------------------------------
12489 -- Is_Potentially_Persistent_Type --
12490 ------------------------------------
12492 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
12493 Comp : Entity_Id;
12494 Indx : Node_Id;
12496 begin
12497 -- For private type, test corresponding full type
12499 if Is_Private_Type (T) then
12500 return Is_Potentially_Persistent_Type (Full_View (T));
12502 -- Scalar types are potentially persistent
12504 elsif Is_Scalar_Type (T) then
12505 return True;
12507 -- Record type is potentially persistent if not tagged and the types of
12508 -- all it components are potentially persistent, and no component has
12509 -- an initialization expression.
12511 elsif Is_Record_Type (T)
12512 and then not Is_Tagged_Type (T)
12513 and then not Is_Partially_Initialized_Type (T)
12514 then
12515 Comp := First_Component (T);
12516 while Present (Comp) loop
12517 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
12518 return False;
12519 else
12520 Next_Entity (Comp);
12521 end if;
12522 end loop;
12524 return True;
12526 -- Array type is potentially persistent if its component type is
12527 -- potentially persistent and if all its constraints are static.
12529 elsif Is_Array_Type (T) then
12530 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
12531 return False;
12532 end if;
12534 Indx := First_Index (T);
12535 while Present (Indx) loop
12536 if not Is_OK_Static_Subtype (Etype (Indx)) then
12537 return False;
12538 else
12539 Next_Index (Indx);
12540 end if;
12541 end loop;
12543 return True;
12545 -- All other types are not potentially persistent
12547 else
12548 return False;
12549 end if;
12550 end Is_Potentially_Persistent_Type;
12552 --------------------------------
12553 -- Is_Potentially_Unevaluated --
12554 --------------------------------
12556 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
12557 Par : Node_Id;
12558 Expr : Node_Id;
12560 begin
12561 Expr := N;
12562 Par := Parent (N);
12564 -- A postcondition whose expression is a short-circuit is broken down
12565 -- into individual aspects for better exception reporting. The original
12566 -- short-circuit expression is rewritten as the second operand, and an
12567 -- occurrence of 'Old in that operand is potentially unevaluated.
12568 -- See Sem_ch13.adb for details of this transformation.
12570 if Nkind (Original_Node (Par)) = N_And_Then then
12571 return True;
12572 end if;
12574 while not Nkind_In (Par, N_If_Expression,
12575 N_Case_Expression,
12576 N_And_Then,
12577 N_Or_Else,
12578 N_In,
12579 N_Not_In)
12580 loop
12581 Expr := Par;
12582 Par := Parent (Par);
12584 -- If the context is not an expression, or if is the result of
12585 -- expansion of an enclosing construct (such as another attribute)
12586 -- the predicate does not apply.
12588 if Nkind (Par) not in N_Subexpr
12589 or else not Comes_From_Source (Par)
12590 then
12591 return False;
12592 end if;
12593 end loop;
12595 if Nkind (Par) = N_If_Expression then
12596 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
12598 elsif Nkind (Par) = N_Case_Expression then
12599 return Expr /= Expression (Par);
12601 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
12602 return Expr = Right_Opnd (Par);
12604 elsif Nkind_In (Par, N_In, N_Not_In) then
12605 return Expr /= Left_Opnd (Par);
12607 else
12608 return False;
12609 end if;
12610 end Is_Potentially_Unevaluated;
12612 ---------------------------------
12613 -- Is_Protected_Self_Reference --
12614 ---------------------------------
12616 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
12618 function In_Access_Definition (N : Node_Id) return Boolean;
12619 -- Returns true if N belongs to an access definition
12621 --------------------------
12622 -- In_Access_Definition --
12623 --------------------------
12625 function In_Access_Definition (N : Node_Id) return Boolean is
12626 P : Node_Id;
12628 begin
12629 P := Parent (N);
12630 while Present (P) loop
12631 if Nkind (P) = N_Access_Definition then
12632 return True;
12633 end if;
12635 P := Parent (P);
12636 end loop;
12638 return False;
12639 end In_Access_Definition;
12641 -- Start of processing for Is_Protected_Self_Reference
12643 begin
12644 -- Verify that prefix is analyzed and has the proper form. Note that
12645 -- the attributes Elab_Spec, Elab_Body, Elab_Subp_Body and UET_Address,
12646 -- which also produce the address of an entity, do not analyze their
12647 -- prefix because they denote entities that are not necessarily visible.
12648 -- Neither of them can apply to a protected type.
12650 return Ada_Version >= Ada_2005
12651 and then Is_Entity_Name (N)
12652 and then Present (Entity (N))
12653 and then Is_Protected_Type (Entity (N))
12654 and then In_Open_Scopes (Entity (N))
12655 and then not In_Access_Definition (N);
12656 end Is_Protected_Self_Reference;
12658 -----------------------------
12659 -- Is_RCI_Pkg_Spec_Or_Body --
12660 -----------------------------
12662 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
12664 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
12665 -- Return True if the unit of Cunit is an RCI package declaration
12667 ---------------------------
12668 -- Is_RCI_Pkg_Decl_Cunit --
12669 ---------------------------
12671 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
12672 The_Unit : constant Node_Id := Unit (Cunit);
12674 begin
12675 if Nkind (The_Unit) /= N_Package_Declaration then
12676 return False;
12677 end if;
12679 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
12680 end Is_RCI_Pkg_Decl_Cunit;
12682 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
12684 begin
12685 return Is_RCI_Pkg_Decl_Cunit (Cunit)
12686 or else
12687 (Nkind (Unit (Cunit)) = N_Package_Body
12688 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
12689 end Is_RCI_Pkg_Spec_Or_Body;
12691 -----------------------------------------
12692 -- Is_Remote_Access_To_Class_Wide_Type --
12693 -----------------------------------------
12695 function Is_Remote_Access_To_Class_Wide_Type
12696 (E : Entity_Id) return Boolean
12698 begin
12699 -- A remote access to class-wide type is a general access to object type
12700 -- declared in the visible part of a Remote_Types or Remote_Call_
12701 -- Interface unit.
12703 return Ekind (E) = E_General_Access_Type
12704 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12705 end Is_Remote_Access_To_Class_Wide_Type;
12707 -----------------------------------------
12708 -- Is_Remote_Access_To_Subprogram_Type --
12709 -----------------------------------------
12711 function Is_Remote_Access_To_Subprogram_Type
12712 (E : Entity_Id) return Boolean
12714 begin
12715 return (Ekind (E) = E_Access_Subprogram_Type
12716 or else (Ekind (E) = E_Record_Type
12717 and then Present (Corresponding_Remote_Type (E))))
12718 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12719 end Is_Remote_Access_To_Subprogram_Type;
12721 --------------------
12722 -- Is_Remote_Call --
12723 --------------------
12725 function Is_Remote_Call (N : Node_Id) return Boolean is
12726 begin
12727 if Nkind (N) not in N_Subprogram_Call then
12729 -- An entry call cannot be remote
12731 return False;
12733 elsif Nkind (Name (N)) in N_Has_Entity
12734 and then Is_Remote_Call_Interface (Entity (Name (N)))
12735 then
12736 -- A subprogram declared in the spec of a RCI package is remote
12738 return True;
12740 elsif Nkind (Name (N)) = N_Explicit_Dereference
12741 and then Is_Remote_Access_To_Subprogram_Type
12742 (Etype (Prefix (Name (N))))
12743 then
12744 -- The dereference of a RAS is a remote call
12746 return True;
12748 elsif Present (Controlling_Argument (N))
12749 and then Is_Remote_Access_To_Class_Wide_Type
12750 (Etype (Controlling_Argument (N)))
12751 then
12752 -- Any primitive operation call with a controlling argument of
12753 -- a RACW type is a remote call.
12755 return True;
12756 end if;
12758 -- All other calls are local calls
12760 return False;
12761 end Is_Remote_Call;
12763 ----------------------
12764 -- Is_Renamed_Entry --
12765 ----------------------
12767 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
12768 Orig_Node : Node_Id := Empty;
12769 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
12771 function Is_Entry (Nam : Node_Id) return Boolean;
12772 -- Determine whether Nam is an entry. Traverse selectors if there are
12773 -- nested selected components.
12775 --------------
12776 -- Is_Entry --
12777 --------------
12779 function Is_Entry (Nam : Node_Id) return Boolean is
12780 begin
12781 if Nkind (Nam) = N_Selected_Component then
12782 return Is_Entry (Selector_Name (Nam));
12783 end if;
12785 return Ekind (Entity (Nam)) = E_Entry;
12786 end Is_Entry;
12788 -- Start of processing for Is_Renamed_Entry
12790 begin
12791 if Present (Alias (Proc_Nam)) then
12792 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
12793 end if;
12795 -- Look for a rewritten subprogram renaming declaration
12797 if Nkind (Subp_Decl) = N_Subprogram_Declaration
12798 and then Present (Original_Node (Subp_Decl))
12799 then
12800 Orig_Node := Original_Node (Subp_Decl);
12801 end if;
12803 -- The rewritten subprogram is actually an entry
12805 if Present (Orig_Node)
12806 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
12807 and then Is_Entry (Name (Orig_Node))
12808 then
12809 return True;
12810 end if;
12812 return False;
12813 end Is_Renamed_Entry;
12815 -----------------------------
12816 -- Is_Renaming_Declaration --
12817 -----------------------------
12819 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
12820 begin
12821 case Nkind (N) is
12822 when N_Exception_Renaming_Declaration |
12823 N_Generic_Function_Renaming_Declaration |
12824 N_Generic_Package_Renaming_Declaration |
12825 N_Generic_Procedure_Renaming_Declaration |
12826 N_Object_Renaming_Declaration |
12827 N_Package_Renaming_Declaration |
12828 N_Subprogram_Renaming_Declaration =>
12829 return True;
12831 when others =>
12832 return False;
12833 end case;
12834 end Is_Renaming_Declaration;
12836 ----------------------------
12837 -- Is_Reversible_Iterator --
12838 ----------------------------
12840 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
12841 Ifaces_List : Elist_Id;
12842 Iface_Elmt : Elmt_Id;
12843 Iface : Entity_Id;
12845 begin
12846 if Is_Class_Wide_Type (Typ)
12847 and then Chars (Etype (Typ)) = Name_Reversible_Iterator
12848 and then Is_Predefined_File_Name
12849 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
12850 then
12851 return True;
12853 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
12854 return False;
12856 else
12857 Collect_Interfaces (Typ, Ifaces_List);
12859 Iface_Elmt := First_Elmt (Ifaces_List);
12860 while Present (Iface_Elmt) loop
12861 Iface := Node (Iface_Elmt);
12862 if Chars (Iface) = Name_Reversible_Iterator
12863 and then
12864 Is_Predefined_File_Name
12865 (Unit_File_Name (Get_Source_Unit (Iface)))
12866 then
12867 return True;
12868 end if;
12870 Next_Elmt (Iface_Elmt);
12871 end loop;
12872 end if;
12874 return False;
12875 end Is_Reversible_Iterator;
12877 ----------------------
12878 -- Is_Selector_Name --
12879 ----------------------
12881 function Is_Selector_Name (N : Node_Id) return Boolean is
12882 begin
12883 if not Is_List_Member (N) then
12884 declare
12885 P : constant Node_Id := Parent (N);
12886 begin
12887 return Nkind_In (P, N_Expanded_Name,
12888 N_Generic_Association,
12889 N_Parameter_Association,
12890 N_Selected_Component)
12891 and then Selector_Name (P) = N;
12892 end;
12894 else
12895 declare
12896 L : constant List_Id := List_Containing (N);
12897 P : constant Node_Id := Parent (L);
12898 begin
12899 return (Nkind (P) = N_Discriminant_Association
12900 and then Selector_Names (P) = L)
12901 or else
12902 (Nkind (P) = N_Component_Association
12903 and then Choices (P) = L);
12904 end;
12905 end if;
12906 end Is_Selector_Name;
12908 ---------------------------------------------
12909 -- Is_Single_Precision_Floating_Point_Type --
12910 ---------------------------------------------
12912 function Is_Single_Precision_Floating_Point_Type
12913 (E : Entity_Id) return Boolean is
12914 begin
12915 return Is_Floating_Point_Type (E)
12916 and then Machine_Radix_Value (E) = Uint_2
12917 and then Machine_Mantissa_Value (E) = Uint_24
12918 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
12919 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
12920 end Is_Single_Precision_Floating_Point_Type;
12922 -------------------------------------
12923 -- Is_SPARK_05_Initialization_Expr --
12924 -------------------------------------
12926 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
12927 Is_Ok : Boolean;
12928 Expr : Node_Id;
12929 Comp_Assn : Node_Id;
12930 Orig_N : constant Node_Id := Original_Node (N);
12932 begin
12933 Is_Ok := True;
12935 if not Comes_From_Source (Orig_N) then
12936 goto Done;
12937 end if;
12939 pragma Assert (Nkind (Orig_N) in N_Subexpr);
12941 case Nkind (Orig_N) is
12942 when N_Character_Literal |
12943 N_Integer_Literal |
12944 N_Real_Literal |
12945 N_String_Literal =>
12946 null;
12948 when N_Identifier |
12949 N_Expanded_Name =>
12950 if Is_Entity_Name (Orig_N)
12951 and then Present (Entity (Orig_N)) -- needed in some cases
12952 then
12953 case Ekind (Entity (Orig_N)) is
12954 when E_Constant |
12955 E_Enumeration_Literal |
12956 E_Named_Integer |
12957 E_Named_Real =>
12958 null;
12959 when others =>
12960 if Is_Type (Entity (Orig_N)) then
12961 null;
12962 else
12963 Is_Ok := False;
12964 end if;
12965 end case;
12966 end if;
12968 when N_Qualified_Expression |
12969 N_Type_Conversion =>
12970 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
12972 when N_Unary_Op =>
12973 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12975 when N_Binary_Op |
12976 N_Short_Circuit |
12977 N_Membership_Test =>
12978 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
12979 and then
12980 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12982 when N_Aggregate |
12983 N_Extension_Aggregate =>
12984 if Nkind (Orig_N) = N_Extension_Aggregate then
12985 Is_Ok :=
12986 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
12987 end if;
12989 Expr := First (Expressions (Orig_N));
12990 while Present (Expr) loop
12991 if not Is_SPARK_05_Initialization_Expr (Expr) then
12992 Is_Ok := False;
12993 goto Done;
12994 end if;
12996 Next (Expr);
12997 end loop;
12999 Comp_Assn := First (Component_Associations (Orig_N));
13000 while Present (Comp_Assn) loop
13001 Expr := Expression (Comp_Assn);
13003 -- Note: test for Present here needed for box assocation
13005 if Present (Expr)
13006 and then not Is_SPARK_05_Initialization_Expr (Expr)
13007 then
13008 Is_Ok := False;
13009 goto Done;
13010 end if;
13012 Next (Comp_Assn);
13013 end loop;
13015 when N_Attribute_Reference =>
13016 if Nkind (Prefix (Orig_N)) in N_Subexpr then
13017 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
13018 end if;
13020 Expr := First (Expressions (Orig_N));
13021 while Present (Expr) loop
13022 if not Is_SPARK_05_Initialization_Expr (Expr) then
13023 Is_Ok := False;
13024 goto Done;
13025 end if;
13027 Next (Expr);
13028 end loop;
13030 -- Selected components might be expanded named not yet resolved, so
13031 -- default on the safe side. (Eg on sparklex.ads)
13033 when N_Selected_Component =>
13034 null;
13036 when others =>
13037 Is_Ok := False;
13038 end case;
13040 <<Done>>
13041 return Is_Ok;
13042 end Is_SPARK_05_Initialization_Expr;
13044 ----------------------------------
13045 -- Is_SPARK_05_Object_Reference --
13046 ----------------------------------
13048 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
13049 begin
13050 if Is_Entity_Name (N) then
13051 return Present (Entity (N))
13052 and then
13053 (Ekind_In (Entity (N), E_Constant, E_Variable)
13054 or else Ekind (Entity (N)) in Formal_Kind);
13056 else
13057 case Nkind (N) is
13058 when N_Selected_Component =>
13059 return Is_SPARK_05_Object_Reference (Prefix (N));
13061 when others =>
13062 return False;
13063 end case;
13064 end if;
13065 end Is_SPARK_05_Object_Reference;
13067 -----------------------------
13068 -- Is_Specific_Tagged_Type --
13069 -----------------------------
13071 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
13072 Full_Typ : Entity_Id;
13074 begin
13075 -- Handle private types
13077 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
13078 Full_Typ := Full_View (Typ);
13079 else
13080 Full_Typ := Typ;
13081 end if;
13083 -- A specific tagged type is a non-class-wide tagged type
13085 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
13086 end Is_Specific_Tagged_Type;
13088 ------------------
13089 -- Is_Statement --
13090 ------------------
13092 function Is_Statement (N : Node_Id) return Boolean is
13093 begin
13094 return
13095 Nkind (N) in N_Statement_Other_Than_Procedure_Call
13096 or else Nkind (N) = N_Procedure_Call_Statement;
13097 end Is_Statement;
13099 ---------------------------------------
13100 -- Is_Subprogram_Contract_Annotation --
13101 ---------------------------------------
13103 function Is_Subprogram_Contract_Annotation
13104 (Item : Node_Id) return Boolean
13106 Nam : Name_Id;
13108 begin
13109 if Nkind (Item) = N_Aspect_Specification then
13110 Nam := Chars (Identifier (Item));
13112 else pragma Assert (Nkind (Item) = N_Pragma);
13113 Nam := Pragma_Name (Item);
13114 end if;
13116 return Nam = Name_Contract_Cases
13117 or else Nam = Name_Depends
13118 or else Nam = Name_Extensions_Visible
13119 or else Nam = Name_Global
13120 or else Nam = Name_Post
13121 or else Nam = Name_Post_Class
13122 or else Nam = Name_Postcondition
13123 or else Nam = Name_Pre
13124 or else Nam = Name_Pre_Class
13125 or else Nam = Name_Precondition
13126 or else Nam = Name_Refined_Depends
13127 or else Nam = Name_Refined_Global
13128 or else Nam = Name_Refined_Post
13129 or else Nam = Name_Test_Case;
13130 end Is_Subprogram_Contract_Annotation;
13132 --------------------------------------------------
13133 -- Is_Subprogram_Stub_Without_Prior_Declaration --
13134 --------------------------------------------------
13136 function Is_Subprogram_Stub_Without_Prior_Declaration
13137 (N : Node_Id) return Boolean
13139 begin
13140 -- A subprogram stub without prior declaration serves as declaration for
13141 -- the actual subprogram body. As such, it has an attached defining
13142 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
13144 return Nkind (N) = N_Subprogram_Body_Stub
13145 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
13146 end Is_Subprogram_Stub_Without_Prior_Declaration;
13148 ---------------------------------
13149 -- Is_Synchronized_Tagged_Type --
13150 ---------------------------------
13152 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
13153 Kind : constant Entity_Kind := Ekind (Base_Type (E));
13155 begin
13156 -- A task or protected type derived from an interface is a tagged type.
13157 -- Such a tagged type is called a synchronized tagged type, as are
13158 -- synchronized interfaces and private extensions whose declaration
13159 -- includes the reserved word synchronized.
13161 return (Is_Tagged_Type (E)
13162 and then (Kind = E_Task_Type
13163 or else
13164 Kind = E_Protected_Type))
13165 or else
13166 (Is_Interface (E)
13167 and then Is_Synchronized_Interface (E))
13168 or else
13169 (Ekind (E) = E_Record_Type_With_Private
13170 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
13171 and then (Synchronized_Present (Parent (E))
13172 or else Is_Synchronized_Interface (Etype (E))));
13173 end Is_Synchronized_Tagged_Type;
13175 -----------------
13176 -- Is_Transfer --
13177 -----------------
13179 function Is_Transfer (N : Node_Id) return Boolean is
13180 Kind : constant Node_Kind := Nkind (N);
13182 begin
13183 if Kind = N_Simple_Return_Statement
13184 or else
13185 Kind = N_Extended_Return_Statement
13186 or else
13187 Kind = N_Goto_Statement
13188 or else
13189 Kind = N_Raise_Statement
13190 or else
13191 Kind = N_Requeue_Statement
13192 then
13193 return True;
13195 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
13196 and then No (Condition (N))
13197 then
13198 return True;
13200 elsif Kind = N_Procedure_Call_Statement
13201 and then Is_Entity_Name (Name (N))
13202 and then Present (Entity (Name (N)))
13203 and then No_Return (Entity (Name (N)))
13204 then
13205 return True;
13207 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
13208 return True;
13210 else
13211 return False;
13212 end if;
13213 end Is_Transfer;
13215 -------------
13216 -- Is_True --
13217 -------------
13219 function Is_True (U : Uint) return Boolean is
13220 begin
13221 return (U /= 0);
13222 end Is_True;
13224 --------------------------------------
13225 -- Is_Unchecked_Conversion_Instance --
13226 --------------------------------------
13228 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
13229 Gen_Par : Entity_Id;
13231 begin
13232 -- Look for a function whose generic parent is the predefined intrinsic
13233 -- function Unchecked_Conversion.
13235 if Ekind (Id) = E_Function then
13236 Gen_Par := Generic_Parent (Parent (Id));
13238 return
13239 Present (Gen_Par)
13240 and then Chars (Gen_Par) = Name_Unchecked_Conversion
13241 and then Is_Intrinsic_Subprogram (Gen_Par)
13242 and then Is_Predefined_File_Name
13243 (Unit_File_Name (Get_Source_Unit (Gen_Par)));
13244 end if;
13246 return False;
13247 end Is_Unchecked_Conversion_Instance;
13249 -------------------------------
13250 -- Is_Universal_Numeric_Type --
13251 -------------------------------
13253 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
13254 begin
13255 return T = Universal_Integer or else T = Universal_Real;
13256 end Is_Universal_Numeric_Type;
13258 -------------------
13259 -- Is_Value_Type --
13260 -------------------
13262 function Is_Value_Type (T : Entity_Id) return Boolean is
13263 begin
13264 return VM_Target = CLI_Target
13265 and then Nkind (T) in N_Has_Chars
13266 and then Chars (T) /= No_Name
13267 and then Get_Name_String (Chars (T)) = "valuetype";
13268 end Is_Value_Type;
13270 ----------------------------
13271 -- Is_Variable_Size_Array --
13272 ----------------------------
13274 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
13275 Idx : Node_Id;
13277 begin
13278 pragma Assert (Is_Array_Type (E));
13280 -- Check if some index is initialized with a non-constant value
13282 Idx := First_Index (E);
13283 while Present (Idx) loop
13284 if Nkind (Idx) = N_Range then
13285 if not Is_Constant_Bound (Low_Bound (Idx))
13286 or else not Is_Constant_Bound (High_Bound (Idx))
13287 then
13288 return True;
13289 end if;
13290 end if;
13292 Idx := Next_Index (Idx);
13293 end loop;
13295 return False;
13296 end Is_Variable_Size_Array;
13298 -----------------------------
13299 -- Is_Variable_Size_Record --
13300 -----------------------------
13302 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
13303 Comp : Entity_Id;
13304 Comp_Typ : Entity_Id;
13306 begin
13307 pragma Assert (Is_Record_Type (E));
13309 Comp := First_Entity (E);
13310 while Present (Comp) loop
13311 Comp_Typ := Etype (Comp);
13313 -- Recursive call if the record type has discriminants
13315 if Is_Record_Type (Comp_Typ)
13316 and then Has_Discriminants (Comp_Typ)
13317 and then Is_Variable_Size_Record (Comp_Typ)
13318 then
13319 return True;
13321 elsif Is_Array_Type (Comp_Typ)
13322 and then Is_Variable_Size_Array (Comp_Typ)
13323 then
13324 return True;
13325 end if;
13327 Next_Entity (Comp);
13328 end loop;
13330 return False;
13331 end Is_Variable_Size_Record;
13333 -----------------
13334 -- Is_Variable --
13335 -----------------
13337 function Is_Variable
13338 (N : Node_Id;
13339 Use_Original_Node : Boolean := True) return Boolean
13341 Orig_Node : Node_Id;
13343 function In_Protected_Function (E : Entity_Id) return Boolean;
13344 -- Within a protected function, the private components of the enclosing
13345 -- protected type are constants. A function nested within a (protected)
13346 -- procedure is not itself protected. Within the body of a protected
13347 -- function the current instance of the protected type is a constant.
13349 function Is_Variable_Prefix (P : Node_Id) return Boolean;
13350 -- Prefixes can involve implicit dereferences, in which case we must
13351 -- test for the case of a reference of a constant access type, which can
13352 -- can never be a variable.
13354 ---------------------------
13355 -- In_Protected_Function --
13356 ---------------------------
13358 function In_Protected_Function (E : Entity_Id) return Boolean is
13359 Prot : Entity_Id;
13360 S : Entity_Id;
13362 begin
13363 -- E is the current instance of a type
13365 if Is_Type (E) then
13366 Prot := E;
13368 -- E is an object
13370 else
13371 Prot := Scope (E);
13372 end if;
13374 if not Is_Protected_Type (Prot) then
13375 return False;
13377 else
13378 S := Current_Scope;
13379 while Present (S) and then S /= Prot loop
13380 if Ekind (S) = E_Function and then Scope (S) = Prot then
13381 return True;
13382 end if;
13384 S := Scope (S);
13385 end loop;
13387 return False;
13388 end if;
13389 end In_Protected_Function;
13391 ------------------------
13392 -- Is_Variable_Prefix --
13393 ------------------------
13395 function Is_Variable_Prefix (P : Node_Id) return Boolean is
13396 begin
13397 if Is_Access_Type (Etype (P)) then
13398 return not Is_Access_Constant (Root_Type (Etype (P)));
13400 -- For the case of an indexed component whose prefix has a packed
13401 -- array type, the prefix has been rewritten into a type conversion.
13402 -- Determine variable-ness from the converted expression.
13404 elsif Nkind (P) = N_Type_Conversion
13405 and then not Comes_From_Source (P)
13406 and then Is_Array_Type (Etype (P))
13407 and then Is_Packed (Etype (P))
13408 then
13409 return Is_Variable (Expression (P));
13411 else
13412 return Is_Variable (P);
13413 end if;
13414 end Is_Variable_Prefix;
13416 -- Start of processing for Is_Variable
13418 begin
13419 -- Special check, allow x'Deref(expr) as a variable
13421 if Nkind (N) = N_Attribute_Reference
13422 and then Attribute_Name (N) = Name_Deref
13423 then
13424 return True;
13425 end if;
13427 -- Check if we perform the test on the original node since this may be a
13428 -- test of syntactic categories which must not be disturbed by whatever
13429 -- rewriting might have occurred. For example, an aggregate, which is
13430 -- certainly NOT a variable, could be turned into a variable by
13431 -- expansion.
13433 if Use_Original_Node then
13434 Orig_Node := Original_Node (N);
13435 else
13436 Orig_Node := N;
13437 end if;
13439 -- Definitely OK if Assignment_OK is set. Since this is something that
13440 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
13442 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
13443 return True;
13445 -- Normally we go to the original node, but there is one exception where
13446 -- we use the rewritten node, namely when it is an explicit dereference.
13447 -- The generated code may rewrite a prefix which is an access type with
13448 -- an explicit dereference. The dereference is a variable, even though
13449 -- the original node may not be (since it could be a constant of the
13450 -- access type).
13452 -- In Ada 2005 we have a further case to consider: the prefix may be a
13453 -- function call given in prefix notation. The original node appears to
13454 -- be a selected component, but we need to examine the call.
13456 elsif Nkind (N) = N_Explicit_Dereference
13457 and then Nkind (Orig_Node) /= N_Explicit_Dereference
13458 and then Present (Etype (Orig_Node))
13459 and then Is_Access_Type (Etype (Orig_Node))
13460 then
13461 -- Note that if the prefix is an explicit dereference that does not
13462 -- come from source, we must check for a rewritten function call in
13463 -- prefixed notation before other forms of rewriting, to prevent a
13464 -- compiler crash.
13466 return
13467 (Nkind (Orig_Node) = N_Function_Call
13468 and then not Is_Access_Constant (Etype (Prefix (N))))
13469 or else
13470 Is_Variable_Prefix (Original_Node (Prefix (N)));
13472 -- in Ada 2012, the dereference may have been added for a type with
13473 -- a declared implicit dereference aspect. Check that it is not an
13474 -- access to constant.
13476 elsif Nkind (N) = N_Explicit_Dereference
13477 and then Present (Etype (Orig_Node))
13478 and then Ada_Version >= Ada_2012
13479 and then Has_Implicit_Dereference (Etype (Orig_Node))
13480 then
13481 return not Is_Access_Constant (Etype (Prefix (N)));
13483 -- A function call is never a variable
13485 elsif Nkind (N) = N_Function_Call then
13486 return False;
13488 -- All remaining checks use the original node
13490 elsif Is_Entity_Name (Orig_Node)
13491 and then Present (Entity (Orig_Node))
13492 then
13493 declare
13494 E : constant Entity_Id := Entity (Orig_Node);
13495 K : constant Entity_Kind := Ekind (E);
13497 begin
13498 return (K = E_Variable
13499 and then Nkind (Parent (E)) /= N_Exception_Handler)
13500 or else (K = E_Component
13501 and then not In_Protected_Function (E))
13502 or else K = E_Out_Parameter
13503 or else K = E_In_Out_Parameter
13504 or else K = E_Generic_In_Out_Parameter
13506 -- Current instance of type. If this is a protected type, check
13507 -- we are not within the body of one of its protected functions.
13509 or else (Is_Type (E)
13510 and then In_Open_Scopes (E)
13511 and then not In_Protected_Function (E))
13513 or else (Is_Incomplete_Or_Private_Type (E)
13514 and then In_Open_Scopes (Full_View (E)));
13515 end;
13517 else
13518 case Nkind (Orig_Node) is
13519 when N_Indexed_Component | N_Slice =>
13520 return Is_Variable_Prefix (Prefix (Orig_Node));
13522 when N_Selected_Component =>
13523 return (Is_Variable (Selector_Name (Orig_Node))
13524 and then Is_Variable_Prefix (Prefix (Orig_Node)))
13525 or else
13526 (Nkind (N) = N_Expanded_Name
13527 and then Scope (Entity (N)) = Entity (Prefix (N)));
13529 -- For an explicit dereference, the type of the prefix cannot
13530 -- be an access to constant or an access to subprogram.
13532 when N_Explicit_Dereference =>
13533 declare
13534 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
13535 begin
13536 return Is_Access_Type (Typ)
13537 and then not Is_Access_Constant (Root_Type (Typ))
13538 and then Ekind (Typ) /= E_Access_Subprogram_Type;
13539 end;
13541 -- The type conversion is the case where we do not deal with the
13542 -- context dependent special case of an actual parameter. Thus
13543 -- the type conversion is only considered a variable for the
13544 -- purposes of this routine if the target type is tagged. However,
13545 -- a type conversion is considered to be a variable if it does not
13546 -- come from source (this deals for example with the conversions
13547 -- of expressions to their actual subtypes).
13549 when N_Type_Conversion =>
13550 return Is_Variable (Expression (Orig_Node))
13551 and then
13552 (not Comes_From_Source (Orig_Node)
13553 or else
13554 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
13555 and then
13556 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
13558 -- GNAT allows an unchecked type conversion as a variable. This
13559 -- only affects the generation of internal expanded code, since
13560 -- calls to instantiations of Unchecked_Conversion are never
13561 -- considered variables (since they are function calls).
13563 when N_Unchecked_Type_Conversion =>
13564 return Is_Variable (Expression (Orig_Node));
13566 when others =>
13567 return False;
13568 end case;
13569 end if;
13570 end Is_Variable;
13572 ---------------------------
13573 -- Is_Visibly_Controlled --
13574 ---------------------------
13576 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
13577 Root : constant Entity_Id := Root_Type (T);
13578 begin
13579 return Chars (Scope (Root)) = Name_Finalization
13580 and then Chars (Scope (Scope (Root))) = Name_Ada
13581 and then Scope (Scope (Scope (Root))) = Standard_Standard;
13582 end Is_Visibly_Controlled;
13584 ------------------------
13585 -- Is_Volatile_Object --
13586 ------------------------
13588 function Is_Volatile_Object (N : Node_Id) return Boolean is
13590 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
13591 -- If prefix is an implicit dereference, examine designated type
13593 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
13594 -- Determines if given object has volatile components
13596 ------------------------
13597 -- Is_Volatile_Prefix --
13598 ------------------------
13600 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
13601 Typ : constant Entity_Id := Etype (N);
13603 begin
13604 if Is_Access_Type (Typ) then
13605 declare
13606 Dtyp : constant Entity_Id := Designated_Type (Typ);
13608 begin
13609 return Is_Volatile (Dtyp)
13610 or else Has_Volatile_Components (Dtyp);
13611 end;
13613 else
13614 return Object_Has_Volatile_Components (N);
13615 end if;
13616 end Is_Volatile_Prefix;
13618 ------------------------------------
13619 -- Object_Has_Volatile_Components --
13620 ------------------------------------
13622 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
13623 Typ : constant Entity_Id := Etype (N);
13625 begin
13626 if Is_Volatile (Typ)
13627 or else Has_Volatile_Components (Typ)
13628 then
13629 return True;
13631 elsif Is_Entity_Name (N)
13632 and then (Has_Volatile_Components (Entity (N))
13633 or else Is_Volatile (Entity (N)))
13634 then
13635 return True;
13637 elsif Nkind (N) = N_Indexed_Component
13638 or else Nkind (N) = N_Selected_Component
13639 then
13640 return Is_Volatile_Prefix (Prefix (N));
13642 else
13643 return False;
13644 end if;
13645 end Object_Has_Volatile_Components;
13647 -- Start of processing for Is_Volatile_Object
13649 begin
13650 if Nkind (N) = N_Defining_Identifier then
13651 return Is_Volatile (N) or else Is_Volatile (Etype (N));
13653 elsif Nkind (N) = N_Expanded_Name then
13654 return Is_Volatile_Object (Entity (N));
13656 elsif Is_Volatile (Etype (N))
13657 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
13658 then
13659 return True;
13661 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
13662 and then Is_Volatile_Prefix (Prefix (N))
13663 then
13664 return True;
13666 elsif Nkind (N) = N_Selected_Component
13667 and then Is_Volatile (Entity (Selector_Name (N)))
13668 then
13669 return True;
13671 else
13672 return False;
13673 end if;
13674 end Is_Volatile_Object;
13676 ---------------------------
13677 -- Itype_Has_Declaration --
13678 ---------------------------
13680 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
13681 begin
13682 pragma Assert (Is_Itype (Id));
13683 return Present (Parent (Id))
13684 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
13685 N_Subtype_Declaration)
13686 and then Defining_Entity (Parent (Id)) = Id;
13687 end Itype_Has_Declaration;
13689 -------------------------
13690 -- Kill_Current_Values --
13691 -------------------------
13693 procedure Kill_Current_Values
13694 (Ent : Entity_Id;
13695 Last_Assignment_Only : Boolean := False)
13697 begin
13698 if Is_Assignable (Ent) then
13699 Set_Last_Assignment (Ent, Empty);
13700 end if;
13702 if Is_Object (Ent) then
13703 if not Last_Assignment_Only then
13704 Kill_Checks (Ent);
13705 Set_Current_Value (Ent, Empty);
13707 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
13708 -- for a constant. Once the constant is elaborated, its value is
13709 -- not changed, therefore the associated flags that describe the
13710 -- value should not be modified either.
13712 if Ekind (Ent) = E_Constant then
13713 null;
13715 -- Non-constant entities
13717 else
13718 if not Can_Never_Be_Null (Ent) then
13719 Set_Is_Known_Non_Null (Ent, False);
13720 end if;
13722 Set_Is_Known_Null (Ent, False);
13724 -- Reset the Is_Known_Valid flag unless the type is always
13725 -- valid. This does not apply to a loop parameter because its
13726 -- bounds are defined by the loop header and therefore always
13727 -- valid.
13729 if not Is_Known_Valid (Etype (Ent))
13730 and then Ekind (Ent) /= E_Loop_Parameter
13731 then
13732 Set_Is_Known_Valid (Ent, False);
13733 end if;
13734 end if;
13735 end if;
13736 end if;
13737 end Kill_Current_Values;
13739 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
13740 S : Entity_Id;
13742 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
13743 -- Clear current value for entity E and all entities chained to E
13745 ------------------------------------------
13746 -- Kill_Current_Values_For_Entity_Chain --
13747 ------------------------------------------
13749 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
13750 Ent : Entity_Id;
13751 begin
13752 Ent := E;
13753 while Present (Ent) loop
13754 Kill_Current_Values (Ent, Last_Assignment_Only);
13755 Next_Entity (Ent);
13756 end loop;
13757 end Kill_Current_Values_For_Entity_Chain;
13759 -- Start of processing for Kill_Current_Values
13761 begin
13762 -- Kill all saved checks, a special case of killing saved values
13764 if not Last_Assignment_Only then
13765 Kill_All_Checks;
13766 end if;
13768 -- Loop through relevant scopes, which includes the current scope and
13769 -- any parent scopes if the current scope is a block or a package.
13771 S := Current_Scope;
13772 Scope_Loop : loop
13774 -- Clear current values of all entities in current scope
13776 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
13778 -- If scope is a package, also clear current values of all private
13779 -- entities in the scope.
13781 if Is_Package_Or_Generic_Package (S)
13782 or else Is_Concurrent_Type (S)
13783 then
13784 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
13785 end if;
13787 -- If this is a not a subprogram, deal with parents
13789 if not Is_Subprogram (S) then
13790 S := Scope (S);
13791 exit Scope_Loop when S = Standard_Standard;
13792 else
13793 exit Scope_Loop;
13794 end if;
13795 end loop Scope_Loop;
13796 end Kill_Current_Values;
13798 --------------------------
13799 -- Kill_Size_Check_Code --
13800 --------------------------
13802 procedure Kill_Size_Check_Code (E : Entity_Id) is
13803 begin
13804 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
13805 and then Present (Size_Check_Code (E))
13806 then
13807 Remove (Size_Check_Code (E));
13808 Set_Size_Check_Code (E, Empty);
13809 end if;
13810 end Kill_Size_Check_Code;
13812 --------------------------
13813 -- Known_To_Be_Assigned --
13814 --------------------------
13816 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
13817 P : constant Node_Id := Parent (N);
13819 begin
13820 case Nkind (P) is
13822 -- Test left side of assignment
13824 when N_Assignment_Statement =>
13825 return N = Name (P);
13827 -- Function call arguments are never lvalues
13829 when N_Function_Call =>
13830 return False;
13832 -- Positional parameter for procedure or accept call
13834 when N_Procedure_Call_Statement |
13835 N_Accept_Statement
13837 declare
13838 Proc : Entity_Id;
13839 Form : Entity_Id;
13840 Act : Node_Id;
13842 begin
13843 Proc := Get_Subprogram_Entity (P);
13845 if No (Proc) then
13846 return False;
13847 end if;
13849 -- If we are not a list member, something is strange, so
13850 -- be conservative and return False.
13852 if not Is_List_Member (N) then
13853 return False;
13854 end if;
13856 -- We are going to find the right formal by stepping forward
13857 -- through the formals, as we step backwards in the actuals.
13859 Form := First_Formal (Proc);
13860 Act := N;
13861 loop
13862 -- If no formal, something is weird, so be conservative
13863 -- and return False.
13865 if No (Form) then
13866 return False;
13867 end if;
13869 Prev (Act);
13870 exit when No (Act);
13871 Next_Formal (Form);
13872 end loop;
13874 return Ekind (Form) /= E_In_Parameter;
13875 end;
13877 -- Named parameter for procedure or accept call
13879 when N_Parameter_Association =>
13880 declare
13881 Proc : Entity_Id;
13882 Form : Entity_Id;
13884 begin
13885 Proc := Get_Subprogram_Entity (Parent (P));
13887 if No (Proc) then
13888 return False;
13889 end if;
13891 -- Loop through formals to find the one that matches
13893 Form := First_Formal (Proc);
13894 loop
13895 -- If no matching formal, that's peculiar, some kind of
13896 -- previous error, so return False to be conservative.
13897 -- Actually this also happens in legal code in the case
13898 -- where P is a parameter association for an Extra_Formal???
13900 if No (Form) then
13901 return False;
13902 end if;
13904 -- Else test for match
13906 if Chars (Form) = Chars (Selector_Name (P)) then
13907 return Ekind (Form) /= E_In_Parameter;
13908 end if;
13910 Next_Formal (Form);
13911 end loop;
13912 end;
13914 -- Test for appearing in a conversion that itself appears
13915 -- in an lvalue context, since this should be an lvalue.
13917 when N_Type_Conversion =>
13918 return Known_To_Be_Assigned (P);
13920 -- All other references are definitely not known to be modifications
13922 when others =>
13923 return False;
13925 end case;
13926 end Known_To_Be_Assigned;
13928 ---------------------------
13929 -- Last_Source_Statement --
13930 ---------------------------
13932 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
13933 N : Node_Id;
13935 begin
13936 N := Last (Statements (HSS));
13937 while Present (N) loop
13938 exit when Comes_From_Source (N);
13939 Prev (N);
13940 end loop;
13942 return N;
13943 end Last_Source_Statement;
13945 ----------------------------------
13946 -- Matching_Static_Array_Bounds --
13947 ----------------------------------
13949 function Matching_Static_Array_Bounds
13950 (L_Typ : Node_Id;
13951 R_Typ : Node_Id) return Boolean
13953 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
13954 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
13956 L_Index : Node_Id;
13957 R_Index : Node_Id;
13958 L_Low : Node_Id;
13959 L_High : Node_Id;
13960 L_Len : Uint;
13961 R_Low : Node_Id;
13962 R_High : Node_Id;
13963 R_Len : Uint;
13965 begin
13966 if L_Ndims /= R_Ndims then
13967 return False;
13968 end if;
13970 -- Unconstrained types do not have static bounds
13972 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
13973 return False;
13974 end if;
13976 -- First treat specially the first dimension, as the lower bound and
13977 -- length of string literals are not stored like those of arrays.
13979 if Ekind (L_Typ) = E_String_Literal_Subtype then
13980 L_Low := String_Literal_Low_Bound (L_Typ);
13981 L_Len := String_Literal_Length (L_Typ);
13982 else
13983 L_Index := First_Index (L_Typ);
13984 Get_Index_Bounds (L_Index, L_Low, L_High);
13986 if Is_OK_Static_Expression (L_Low)
13987 and then
13988 Is_OK_Static_Expression (L_High)
13989 then
13990 if Expr_Value (L_High) < Expr_Value (L_Low) then
13991 L_Len := Uint_0;
13992 else
13993 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
13994 end if;
13995 else
13996 return False;
13997 end if;
13998 end if;
14000 if Ekind (R_Typ) = E_String_Literal_Subtype then
14001 R_Low := String_Literal_Low_Bound (R_Typ);
14002 R_Len := String_Literal_Length (R_Typ);
14003 else
14004 R_Index := First_Index (R_Typ);
14005 Get_Index_Bounds (R_Index, R_Low, R_High);
14007 if Is_OK_Static_Expression (R_Low)
14008 and then
14009 Is_OK_Static_Expression (R_High)
14010 then
14011 if Expr_Value (R_High) < Expr_Value (R_Low) then
14012 R_Len := Uint_0;
14013 else
14014 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
14015 end if;
14016 else
14017 return False;
14018 end if;
14019 end if;
14021 if (Is_OK_Static_Expression (L_Low)
14022 and then
14023 Is_OK_Static_Expression (R_Low))
14024 and then Expr_Value (L_Low) = Expr_Value (R_Low)
14025 and then L_Len = R_Len
14026 then
14027 null;
14028 else
14029 return False;
14030 end if;
14032 -- Then treat all other dimensions
14034 for Indx in 2 .. L_Ndims loop
14035 Next (L_Index);
14036 Next (R_Index);
14038 Get_Index_Bounds (L_Index, L_Low, L_High);
14039 Get_Index_Bounds (R_Index, R_Low, R_High);
14041 if (Is_OK_Static_Expression (L_Low) and then
14042 Is_OK_Static_Expression (L_High) and then
14043 Is_OK_Static_Expression (R_Low) and then
14044 Is_OK_Static_Expression (R_High))
14045 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
14046 and then
14047 Expr_Value (L_High) = Expr_Value (R_High))
14048 then
14049 null;
14050 else
14051 return False;
14052 end if;
14053 end loop;
14055 -- If we fall through the loop, all indexes matched
14057 return True;
14058 end Matching_Static_Array_Bounds;
14060 -------------------
14061 -- May_Be_Lvalue --
14062 -------------------
14064 function May_Be_Lvalue (N : Node_Id) return Boolean is
14065 P : constant Node_Id := Parent (N);
14067 begin
14068 case Nkind (P) is
14070 -- Test left side of assignment
14072 when N_Assignment_Statement =>
14073 return N = Name (P);
14075 -- Test prefix of component or attribute. Note that the prefix of an
14076 -- explicit or implicit dereference cannot be an l-value.
14078 when N_Attribute_Reference =>
14079 return N = Prefix (P)
14080 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
14082 -- For an expanded name, the name is an lvalue if the expanded name
14083 -- is an lvalue, but the prefix is never an lvalue, since it is just
14084 -- the scope where the name is found.
14086 when N_Expanded_Name =>
14087 if N = Prefix (P) then
14088 return May_Be_Lvalue (P);
14089 else
14090 return False;
14091 end if;
14093 -- For a selected component A.B, A is certainly an lvalue if A.B is.
14094 -- B is a little interesting, if we have A.B := 3, there is some
14095 -- discussion as to whether B is an lvalue or not, we choose to say
14096 -- it is. Note however that A is not an lvalue if it is of an access
14097 -- type since this is an implicit dereference.
14099 when N_Selected_Component =>
14100 if N = Prefix (P)
14101 and then Present (Etype (N))
14102 and then Is_Access_Type (Etype (N))
14103 then
14104 return False;
14105 else
14106 return May_Be_Lvalue (P);
14107 end if;
14109 -- For an indexed component or slice, the index or slice bounds is
14110 -- never an lvalue. The prefix is an lvalue if the indexed component
14111 -- or slice is an lvalue, except if it is an access type, where we
14112 -- have an implicit dereference.
14114 when N_Indexed_Component | N_Slice =>
14115 if N /= Prefix (P)
14116 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
14117 then
14118 return False;
14119 else
14120 return May_Be_Lvalue (P);
14121 end if;
14123 -- Prefix of a reference is an lvalue if the reference is an lvalue
14125 when N_Reference =>
14126 return May_Be_Lvalue (P);
14128 -- Prefix of explicit dereference is never an lvalue
14130 when N_Explicit_Dereference =>
14131 return False;
14133 -- Positional parameter for subprogram, entry, or accept call.
14134 -- In older versions of Ada function call arguments are never
14135 -- lvalues. In Ada 2012 functions can have in-out parameters.
14137 when N_Subprogram_Call |
14138 N_Entry_Call_Statement |
14139 N_Accept_Statement
14141 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
14142 return False;
14143 end if;
14145 -- The following mechanism is clumsy and fragile. A single flag
14146 -- set in Resolve_Actuals would be preferable ???
14148 declare
14149 Proc : Entity_Id;
14150 Form : Entity_Id;
14151 Act : Node_Id;
14153 begin
14154 Proc := Get_Subprogram_Entity (P);
14156 if No (Proc) then
14157 return True;
14158 end if;
14160 -- If we are not a list member, something is strange, so be
14161 -- conservative and return True.
14163 if not Is_List_Member (N) then
14164 return True;
14165 end if;
14167 -- We are going to find the right formal by stepping forward
14168 -- through the formals, as we step backwards in the actuals.
14170 Form := First_Formal (Proc);
14171 Act := N;
14172 loop
14173 -- If no formal, something is weird, so be conservative and
14174 -- return True.
14176 if No (Form) then
14177 return True;
14178 end if;
14180 Prev (Act);
14181 exit when No (Act);
14182 Next_Formal (Form);
14183 end loop;
14185 return Ekind (Form) /= E_In_Parameter;
14186 end;
14188 -- Named parameter for procedure or accept call
14190 when N_Parameter_Association =>
14191 declare
14192 Proc : Entity_Id;
14193 Form : Entity_Id;
14195 begin
14196 Proc := Get_Subprogram_Entity (Parent (P));
14198 if No (Proc) then
14199 return True;
14200 end if;
14202 -- Loop through formals to find the one that matches
14204 Form := First_Formal (Proc);
14205 loop
14206 -- If no matching formal, that's peculiar, some kind of
14207 -- previous error, so return True to be conservative.
14208 -- Actually happens with legal code for an unresolved call
14209 -- where we may get the wrong homonym???
14211 if No (Form) then
14212 return True;
14213 end if;
14215 -- Else test for match
14217 if Chars (Form) = Chars (Selector_Name (P)) then
14218 return Ekind (Form) /= E_In_Parameter;
14219 end if;
14221 Next_Formal (Form);
14222 end loop;
14223 end;
14225 -- Test for appearing in a conversion that itself appears in an
14226 -- lvalue context, since this should be an lvalue.
14228 when N_Type_Conversion =>
14229 return May_Be_Lvalue (P);
14231 -- Test for appearance in object renaming declaration
14233 when N_Object_Renaming_Declaration =>
14234 return True;
14236 -- All other references are definitely not lvalues
14238 when others =>
14239 return False;
14241 end case;
14242 end May_Be_Lvalue;
14244 -----------------------
14245 -- Mark_Coextensions --
14246 -----------------------
14248 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
14249 Is_Dynamic : Boolean;
14250 -- Indicates whether the context causes nested coextensions to be
14251 -- dynamic or static
14253 function Mark_Allocator (N : Node_Id) return Traverse_Result;
14254 -- Recognize an allocator node and label it as a dynamic coextension
14256 --------------------
14257 -- Mark_Allocator --
14258 --------------------
14260 function Mark_Allocator (N : Node_Id) return Traverse_Result is
14261 begin
14262 if Nkind (N) = N_Allocator then
14263 if Is_Dynamic then
14264 Set_Is_Dynamic_Coextension (N);
14266 -- If the allocator expression is potentially dynamic, it may
14267 -- be expanded out of order and require dynamic allocation
14268 -- anyway, so we treat the coextension itself as dynamic.
14269 -- Potential optimization ???
14271 elsif Nkind (Expression (N)) = N_Qualified_Expression
14272 and then Nkind (Expression (Expression (N))) = N_Op_Concat
14273 then
14274 Set_Is_Dynamic_Coextension (N);
14275 else
14276 Set_Is_Static_Coextension (N);
14277 end if;
14278 end if;
14280 return OK;
14281 end Mark_Allocator;
14283 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
14285 -- Start of processing Mark_Coextensions
14287 begin
14288 case Nkind (Context_Nod) is
14290 -- Comment here ???
14292 when N_Assignment_Statement =>
14293 Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
14295 -- An allocator that is a component of a returned aggregate
14296 -- must be dynamic.
14298 when N_Simple_Return_Statement =>
14299 declare
14300 Expr : constant Node_Id := Expression (Context_Nod);
14301 begin
14302 Is_Dynamic :=
14303 Nkind (Expr) = N_Allocator
14304 or else
14305 (Nkind (Expr) = N_Qualified_Expression
14306 and then Nkind (Expression (Expr)) = N_Aggregate);
14307 end;
14309 -- An alloctor within an object declaration in an extended return
14310 -- statement is of necessity dynamic.
14312 when N_Object_Declaration =>
14313 Is_Dynamic := Nkind (Root_Nod) = N_Allocator
14314 or else
14315 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
14317 -- This routine should not be called for constructs which may not
14318 -- contain coextensions.
14320 when others =>
14321 raise Program_Error;
14322 end case;
14324 Mark_Allocators (Root_Nod);
14325 end Mark_Coextensions;
14327 ----------------------
14328 -- Needs_One_Actual --
14329 ----------------------
14331 function Needs_One_Actual (E : Entity_Id) return Boolean is
14332 Formal : Entity_Id;
14334 begin
14335 -- Ada 2005 or later, and formals present
14337 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
14338 Formal := Next_Formal (First_Formal (E));
14339 while Present (Formal) loop
14340 if No (Default_Value (Formal)) then
14341 return False;
14342 end if;
14344 Next_Formal (Formal);
14345 end loop;
14347 return True;
14349 -- Ada 83/95 or no formals
14351 else
14352 return False;
14353 end if;
14354 end Needs_One_Actual;
14356 ------------------------
14357 -- New_Copy_List_Tree --
14358 ------------------------
14360 function New_Copy_List_Tree (List : List_Id) return List_Id is
14361 NL : List_Id;
14362 E : Node_Id;
14364 begin
14365 if List = No_List then
14366 return No_List;
14368 else
14369 NL := New_List;
14370 E := First (List);
14372 while Present (E) loop
14373 Append (New_Copy_Tree (E), NL);
14374 E := Next (E);
14375 end loop;
14377 return NL;
14378 end if;
14379 end New_Copy_List_Tree;
14381 --------------------------------------------------
14382 -- New_Copy_Tree Auxiliary Data and Subprograms --
14383 --------------------------------------------------
14385 use Atree.Unchecked_Access;
14386 use Atree_Private_Part;
14388 -- Our approach here requires a two pass traversal of the tree. The
14389 -- first pass visits all nodes that eventually will be copied looking
14390 -- for defining Itypes. If any defining Itypes are found, then they are
14391 -- copied, and an entry is added to the replacement map. In the second
14392 -- phase, the tree is copied, using the replacement map to replace any
14393 -- Itype references within the copied tree.
14395 -- The following hash tables are used if the Map supplied has more
14396 -- than hash threshold entries to speed up access to the map. If
14397 -- there are fewer entries, then the map is searched sequentially
14398 -- (because setting up a hash table for only a few entries takes
14399 -- more time than it saves.
14401 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
14402 -- Hash function used for hash operations
14404 -------------------
14405 -- New_Copy_Hash --
14406 -------------------
14408 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
14409 begin
14410 return Nat (E) mod (NCT_Header_Num'Last + 1);
14411 end New_Copy_Hash;
14413 ---------------
14414 -- NCT_Assoc --
14415 ---------------
14417 -- The hash table NCT_Assoc associates old entities in the table
14418 -- with their corresponding new entities (i.e. the pairs of entries
14419 -- presented in the original Map argument are Key-Element pairs).
14421 package NCT_Assoc is new Simple_HTable (
14422 Header_Num => NCT_Header_Num,
14423 Element => Entity_Id,
14424 No_Element => Empty,
14425 Key => Entity_Id,
14426 Hash => New_Copy_Hash,
14427 Equal => Types."=");
14429 ---------------------
14430 -- NCT_Itype_Assoc --
14431 ---------------------
14433 -- The hash table NCT_Itype_Assoc contains entries only for those
14434 -- old nodes which have a non-empty Associated_Node_For_Itype set.
14435 -- The key is the associated node, and the element is the new node
14436 -- itself (NOT the associated node for the new node).
14438 package NCT_Itype_Assoc is new Simple_HTable (
14439 Header_Num => NCT_Header_Num,
14440 Element => Entity_Id,
14441 No_Element => Empty,
14442 Key => Entity_Id,
14443 Hash => New_Copy_Hash,
14444 Equal => Types."=");
14446 -------------------
14447 -- New_Copy_Tree --
14448 -------------------
14450 function New_Copy_Tree
14451 (Source : Node_Id;
14452 Map : Elist_Id := No_Elist;
14453 New_Sloc : Source_Ptr := No_Location;
14454 New_Scope : Entity_Id := Empty) return Node_Id
14456 Actual_Map : Elist_Id := Map;
14457 -- This is the actual map for the copy. It is initialized with the
14458 -- given elements, and then enlarged as required for Itypes that are
14459 -- copied during the first phase of the copy operation. The visit
14460 -- procedures add elements to this map as Itypes are encountered.
14461 -- The reason we cannot use Map directly, is that it may well be
14462 -- (and normally is) initialized to No_Elist, and if we have mapped
14463 -- entities, we have to reset it to point to a real Elist.
14465 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
14466 -- Called during second phase to map entities into their corresponding
14467 -- copies using Actual_Map. If the argument is not an entity, or is not
14468 -- in Actual_Map, then it is returned unchanged.
14470 procedure Build_NCT_Hash_Tables;
14471 -- Builds hash tables (number of elements >= threshold value)
14473 function Copy_Elist_With_Replacement
14474 (Old_Elist : Elist_Id) return Elist_Id;
14475 -- Called during second phase to copy element list doing replacements
14477 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
14478 -- Called during the second phase to process a copied Itype. The actual
14479 -- copy happened during the first phase (so that we could make the entry
14480 -- in the mapping), but we still have to deal with the descendents of
14481 -- the copied Itype and copy them where necessary.
14483 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
14484 -- Called during second phase to copy list doing replacements
14486 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
14487 -- Called during second phase to copy node doing replacements
14489 procedure Visit_Elist (E : Elist_Id);
14490 -- Called during first phase to visit all elements of an Elist
14492 procedure Visit_Field (F : Union_Id; N : Node_Id);
14493 -- Visit a single field, recursing to call Visit_Node or Visit_List
14494 -- if the field is a syntactic descendent of the current node (i.e.
14495 -- its parent is Node N).
14497 procedure Visit_Itype (Old_Itype : Entity_Id);
14498 -- Called during first phase to visit subsidiary fields of a defining
14499 -- Itype, and also create a copy and make an entry in the replacement
14500 -- map for the new copy.
14502 procedure Visit_List (L : List_Id);
14503 -- Called during first phase to visit all elements of a List
14505 procedure Visit_Node (N : Node_Or_Entity_Id);
14506 -- Called during first phase to visit a node and all its subtrees
14508 -----------
14509 -- Assoc --
14510 -----------
14512 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
14513 E : Elmt_Id;
14514 Ent : Entity_Id;
14516 begin
14517 if not Has_Extension (N) or else No (Actual_Map) then
14518 return N;
14520 elsif NCT_Hash_Tables_Used then
14521 Ent := NCT_Assoc.Get (Entity_Id (N));
14523 if Present (Ent) then
14524 return Ent;
14525 else
14526 return N;
14527 end if;
14529 -- No hash table used, do serial search
14531 else
14532 E := First_Elmt (Actual_Map);
14533 while Present (E) loop
14534 if Node (E) = N then
14535 return Node (Next_Elmt (E));
14536 else
14537 E := Next_Elmt (Next_Elmt (E));
14538 end if;
14539 end loop;
14540 end if;
14542 return N;
14543 end Assoc;
14545 ---------------------------
14546 -- Build_NCT_Hash_Tables --
14547 ---------------------------
14549 procedure Build_NCT_Hash_Tables is
14550 Elmt : Elmt_Id;
14551 Ent : Entity_Id;
14552 begin
14553 if NCT_Hash_Table_Setup then
14554 NCT_Assoc.Reset;
14555 NCT_Itype_Assoc.Reset;
14556 end if;
14558 Elmt := First_Elmt (Actual_Map);
14559 while Present (Elmt) loop
14560 Ent := Node (Elmt);
14562 -- Get new entity, and associate old and new
14564 Next_Elmt (Elmt);
14565 NCT_Assoc.Set (Ent, Node (Elmt));
14567 if Is_Type (Ent) then
14568 declare
14569 Anode : constant Entity_Id :=
14570 Associated_Node_For_Itype (Ent);
14572 begin
14573 if Present (Anode) then
14575 -- Enter a link between the associated node of the
14576 -- old Itype and the new Itype, for updating later
14577 -- when node is copied.
14579 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
14580 end if;
14581 end;
14582 end if;
14584 Next_Elmt (Elmt);
14585 end loop;
14587 NCT_Hash_Tables_Used := True;
14588 NCT_Hash_Table_Setup := True;
14589 end Build_NCT_Hash_Tables;
14591 ---------------------------------
14592 -- Copy_Elist_With_Replacement --
14593 ---------------------------------
14595 function Copy_Elist_With_Replacement
14596 (Old_Elist : Elist_Id) return Elist_Id
14598 M : Elmt_Id;
14599 New_Elist : Elist_Id;
14601 begin
14602 if No (Old_Elist) then
14603 return No_Elist;
14605 else
14606 New_Elist := New_Elmt_List;
14608 M := First_Elmt (Old_Elist);
14609 while Present (M) loop
14610 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
14611 Next_Elmt (M);
14612 end loop;
14613 end if;
14615 return New_Elist;
14616 end Copy_Elist_With_Replacement;
14618 ---------------------------------
14619 -- Copy_Itype_With_Replacement --
14620 ---------------------------------
14622 -- This routine exactly parallels its phase one analog Visit_Itype,
14624 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
14625 begin
14626 -- Translate Next_Entity, Scope and Etype fields, in case they
14627 -- reference entities that have been mapped into copies.
14629 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
14630 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
14632 if Present (New_Scope) then
14633 Set_Scope (New_Itype, New_Scope);
14634 else
14635 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
14636 end if;
14638 -- Copy referenced fields
14640 if Is_Discrete_Type (New_Itype) then
14641 Set_Scalar_Range (New_Itype,
14642 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
14644 elsif Has_Discriminants (Base_Type (New_Itype)) then
14645 Set_Discriminant_Constraint (New_Itype,
14646 Copy_Elist_With_Replacement
14647 (Discriminant_Constraint (New_Itype)));
14649 elsif Is_Array_Type (New_Itype) then
14650 if Present (First_Index (New_Itype)) then
14651 Set_First_Index (New_Itype,
14652 First (Copy_List_With_Replacement
14653 (List_Containing (First_Index (New_Itype)))));
14654 end if;
14656 if Is_Packed (New_Itype) then
14657 Set_Packed_Array_Impl_Type (New_Itype,
14658 Copy_Node_With_Replacement
14659 (Packed_Array_Impl_Type (New_Itype)));
14660 end if;
14661 end if;
14662 end Copy_Itype_With_Replacement;
14664 --------------------------------
14665 -- Copy_List_With_Replacement --
14666 --------------------------------
14668 function Copy_List_With_Replacement
14669 (Old_List : List_Id) return List_Id
14671 New_List : List_Id;
14672 E : Node_Id;
14674 begin
14675 if Old_List = No_List then
14676 return No_List;
14678 else
14679 New_List := Empty_List;
14681 E := First (Old_List);
14682 while Present (E) loop
14683 Append (Copy_Node_With_Replacement (E), New_List);
14684 Next (E);
14685 end loop;
14687 return New_List;
14688 end if;
14689 end Copy_List_With_Replacement;
14691 --------------------------------
14692 -- Copy_Node_With_Replacement --
14693 --------------------------------
14695 function Copy_Node_With_Replacement
14696 (Old_Node : Node_Id) return Node_Id
14698 New_Node : Node_Id;
14700 procedure Adjust_Named_Associations
14701 (Old_Node : Node_Id;
14702 New_Node : Node_Id);
14703 -- If a call node has named associations, these are chained through
14704 -- the First_Named_Actual, Next_Named_Actual links. These must be
14705 -- propagated separately to the new parameter list, because these
14706 -- are not syntactic fields.
14708 function Copy_Field_With_Replacement
14709 (Field : Union_Id) return Union_Id;
14710 -- Given Field, which is a field of Old_Node, return a copy of it
14711 -- if it is a syntactic field (i.e. its parent is Node), setting
14712 -- the parent of the copy to poit to New_Node. Otherwise returns
14713 -- the field (possibly mapped if it is an entity).
14715 -------------------------------
14716 -- Adjust_Named_Associations --
14717 -------------------------------
14719 procedure Adjust_Named_Associations
14720 (Old_Node : Node_Id;
14721 New_Node : Node_Id)
14723 Old_E : Node_Id;
14724 New_E : Node_Id;
14726 Old_Next : Node_Id;
14727 New_Next : Node_Id;
14729 begin
14730 Old_E := First (Parameter_Associations (Old_Node));
14731 New_E := First (Parameter_Associations (New_Node));
14732 while Present (Old_E) loop
14733 if Nkind (Old_E) = N_Parameter_Association
14734 and then Present (Next_Named_Actual (Old_E))
14735 then
14736 if First_Named_Actual (Old_Node)
14737 = Explicit_Actual_Parameter (Old_E)
14738 then
14739 Set_First_Named_Actual
14740 (New_Node, Explicit_Actual_Parameter (New_E));
14741 end if;
14743 -- Now scan parameter list from the beginning,to locate
14744 -- next named actual, which can be out of order.
14746 Old_Next := First (Parameter_Associations (Old_Node));
14747 New_Next := First (Parameter_Associations (New_Node));
14749 while Nkind (Old_Next) /= N_Parameter_Association
14750 or else Explicit_Actual_Parameter (Old_Next) /=
14751 Next_Named_Actual (Old_E)
14752 loop
14753 Next (Old_Next);
14754 Next (New_Next);
14755 end loop;
14757 Set_Next_Named_Actual
14758 (New_E, Explicit_Actual_Parameter (New_Next));
14759 end if;
14761 Next (Old_E);
14762 Next (New_E);
14763 end loop;
14764 end Adjust_Named_Associations;
14766 ---------------------------------
14767 -- Copy_Field_With_Replacement --
14768 ---------------------------------
14770 function Copy_Field_With_Replacement
14771 (Field : Union_Id) return Union_Id
14773 begin
14774 if Field = Union_Id (Empty) then
14775 return Field;
14777 elsif Field in Node_Range then
14778 declare
14779 Old_N : constant Node_Id := Node_Id (Field);
14780 New_N : Node_Id;
14782 begin
14783 -- If syntactic field, as indicated by the parent pointer
14784 -- being set, then copy the referenced node recursively.
14786 if Parent (Old_N) = Old_Node then
14787 New_N := Copy_Node_With_Replacement (Old_N);
14789 if New_N /= Old_N then
14790 Set_Parent (New_N, New_Node);
14791 end if;
14793 -- For semantic fields, update possible entity reference
14794 -- from the replacement map.
14796 else
14797 New_N := Assoc (Old_N);
14798 end if;
14800 return Union_Id (New_N);
14801 end;
14803 elsif Field in List_Range then
14804 declare
14805 Old_L : constant List_Id := List_Id (Field);
14806 New_L : List_Id;
14808 begin
14809 -- If syntactic field, as indicated by the parent pointer,
14810 -- then recursively copy the entire referenced list.
14812 if Parent (Old_L) = Old_Node then
14813 New_L := Copy_List_With_Replacement (Old_L);
14814 Set_Parent (New_L, New_Node);
14816 -- For semantic list, just returned unchanged
14818 else
14819 New_L := Old_L;
14820 end if;
14822 return Union_Id (New_L);
14823 end;
14825 -- Anything other than a list or a node is returned unchanged
14827 else
14828 return Field;
14829 end if;
14830 end Copy_Field_With_Replacement;
14832 -- Start of processing for Copy_Node_With_Replacement
14834 begin
14835 if Old_Node <= Empty_Or_Error then
14836 return Old_Node;
14838 elsif Has_Extension (Old_Node) then
14839 return Assoc (Old_Node);
14841 else
14842 New_Node := New_Copy (Old_Node);
14844 -- If the node we are copying is the associated node of a
14845 -- previously copied Itype, then adjust the associated node
14846 -- of the copy of that Itype accordingly.
14848 if Present (Actual_Map) then
14849 declare
14850 E : Elmt_Id;
14851 Ent : Entity_Id;
14853 begin
14854 -- Case of hash table used
14856 if NCT_Hash_Tables_Used then
14857 Ent := NCT_Itype_Assoc.Get (Old_Node);
14859 if Present (Ent) then
14860 Set_Associated_Node_For_Itype (Ent, New_Node);
14861 end if;
14863 -- Case of no hash table used
14865 else
14866 E := First_Elmt (Actual_Map);
14867 while Present (E) loop
14868 if Is_Itype (Node (E))
14869 and then
14870 Old_Node = Associated_Node_For_Itype (Node (E))
14871 then
14872 Set_Associated_Node_For_Itype
14873 (Node (Next_Elmt (E)), New_Node);
14874 end if;
14876 E := Next_Elmt (Next_Elmt (E));
14877 end loop;
14878 end if;
14879 end;
14880 end if;
14882 -- Recursively copy descendents
14884 Set_Field1
14885 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
14886 Set_Field2
14887 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
14888 Set_Field3
14889 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
14890 Set_Field4
14891 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
14892 Set_Field5
14893 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
14895 -- Adjust Sloc of new node if necessary
14897 if New_Sloc /= No_Location then
14898 Set_Sloc (New_Node, New_Sloc);
14900 -- If we adjust the Sloc, then we are essentially making
14901 -- a completely new node, so the Comes_From_Source flag
14902 -- should be reset to the proper default value.
14904 Nodes.Table (New_Node).Comes_From_Source :=
14905 Default_Node.Comes_From_Source;
14906 end if;
14908 -- If the node is call and has named associations,
14909 -- set the corresponding links in the copy.
14911 if (Nkind (Old_Node) = N_Function_Call
14912 or else Nkind (Old_Node) = N_Entry_Call_Statement
14913 or else
14914 Nkind (Old_Node) = N_Procedure_Call_Statement)
14915 and then Present (First_Named_Actual (Old_Node))
14916 then
14917 Adjust_Named_Associations (Old_Node, New_Node);
14918 end if;
14920 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
14921 -- The replacement mechanism applies to entities, and is not used
14922 -- here. Eventually we may need a more general graph-copying
14923 -- routine. For now, do a sequential search to find desired node.
14925 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
14926 and then Present (First_Real_Statement (Old_Node))
14927 then
14928 declare
14929 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
14930 N1, N2 : Node_Id;
14932 begin
14933 N1 := First (Statements (Old_Node));
14934 N2 := First (Statements (New_Node));
14936 while N1 /= Old_F loop
14937 Next (N1);
14938 Next (N2);
14939 end loop;
14941 Set_First_Real_Statement (New_Node, N2);
14942 end;
14943 end if;
14944 end if;
14946 -- All done, return copied node
14948 return New_Node;
14949 end Copy_Node_With_Replacement;
14951 -----------------
14952 -- Visit_Elist --
14953 -----------------
14955 procedure Visit_Elist (E : Elist_Id) is
14956 Elmt : Elmt_Id;
14957 begin
14958 if Present (E) then
14959 Elmt := First_Elmt (E);
14961 while Elmt /= No_Elmt loop
14962 Visit_Node (Node (Elmt));
14963 Next_Elmt (Elmt);
14964 end loop;
14965 end if;
14966 end Visit_Elist;
14968 -----------------
14969 -- Visit_Field --
14970 -----------------
14972 procedure Visit_Field (F : Union_Id; N : Node_Id) is
14973 begin
14974 if F = Union_Id (Empty) then
14975 return;
14977 elsif F in Node_Range then
14979 -- Copy node if it is syntactic, i.e. its parent pointer is
14980 -- set to point to the field that referenced it (certain
14981 -- Itypes will also meet this criterion, which is fine, since
14982 -- these are clearly Itypes that do need to be copied, since
14983 -- we are copying their parent.)
14985 if Parent (Node_Id (F)) = N then
14986 Visit_Node (Node_Id (F));
14987 return;
14989 -- Another case, if we are pointing to an Itype, then we want
14990 -- to copy it if its associated node is somewhere in the tree
14991 -- being copied.
14993 -- Note: the exclusion of self-referential copies is just an
14994 -- optimization, since the search of the already copied list
14995 -- would catch it, but it is a common case (Etype pointing
14996 -- to itself for an Itype that is a base type).
14998 elsif Has_Extension (Node_Id (F))
14999 and then Is_Itype (Entity_Id (F))
15000 and then Node_Id (F) /= N
15001 then
15002 declare
15003 P : Node_Id;
15005 begin
15006 P := Associated_Node_For_Itype (Node_Id (F));
15007 while Present (P) loop
15008 if P = Source then
15009 Visit_Node (Node_Id (F));
15010 return;
15011 else
15012 P := Parent (P);
15013 end if;
15014 end loop;
15016 -- An Itype whose parent is not being copied definitely
15017 -- should NOT be copied, since it does not belong in any
15018 -- sense to the copied subtree.
15020 return;
15021 end;
15022 end if;
15024 elsif F in List_Range and then Parent (List_Id (F)) = N then
15025 Visit_List (List_Id (F));
15026 return;
15027 end if;
15028 end Visit_Field;
15030 -----------------
15031 -- Visit_Itype --
15032 -----------------
15034 procedure Visit_Itype (Old_Itype : Entity_Id) is
15035 New_Itype : Entity_Id;
15036 E : Elmt_Id;
15037 Ent : Entity_Id;
15039 begin
15040 -- Itypes that describe the designated type of access to subprograms
15041 -- have the structure of subprogram declarations, with signatures,
15042 -- etc. Either we duplicate the signatures completely, or choose to
15043 -- share such itypes, which is fine because their elaboration will
15044 -- have no side effects.
15046 if Ekind (Old_Itype) = E_Subprogram_Type then
15047 return;
15048 end if;
15050 New_Itype := New_Copy (Old_Itype);
15052 -- The new Itype has all the attributes of the old one, and
15053 -- we just copy the contents of the entity. However, the back-end
15054 -- needs different names for debugging purposes, so we create a
15055 -- new internal name for it in all cases.
15057 Set_Chars (New_Itype, New_Internal_Name ('T'));
15059 -- If our associated node is an entity that has already been copied,
15060 -- then set the associated node of the copy to point to the right
15061 -- copy. If we have copied an Itype that is itself the associated
15062 -- node of some previously copied Itype, then we set the right
15063 -- pointer in the other direction.
15065 if Present (Actual_Map) then
15067 -- Case of hash tables used
15069 if NCT_Hash_Tables_Used then
15071 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
15073 if Present (Ent) then
15074 Set_Associated_Node_For_Itype (New_Itype, Ent);
15075 end if;
15077 Ent := NCT_Itype_Assoc.Get (Old_Itype);
15078 if Present (Ent) then
15079 Set_Associated_Node_For_Itype (Ent, New_Itype);
15081 -- If the hash table has no association for this Itype and
15082 -- its associated node, enter one now.
15084 else
15085 NCT_Itype_Assoc.Set
15086 (Associated_Node_For_Itype (Old_Itype), New_Itype);
15087 end if;
15089 -- Case of hash tables not used
15091 else
15092 E := First_Elmt (Actual_Map);
15093 while Present (E) loop
15094 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
15095 Set_Associated_Node_For_Itype
15096 (New_Itype, Node (Next_Elmt (E)));
15097 end if;
15099 if Is_Type (Node (E))
15100 and then Old_Itype = Associated_Node_For_Itype (Node (E))
15101 then
15102 Set_Associated_Node_For_Itype
15103 (Node (Next_Elmt (E)), New_Itype);
15104 end if;
15106 E := Next_Elmt (Next_Elmt (E));
15107 end loop;
15108 end if;
15109 end if;
15111 if Present (Freeze_Node (New_Itype)) then
15112 Set_Is_Frozen (New_Itype, False);
15113 Set_Freeze_Node (New_Itype, Empty);
15114 end if;
15116 -- Add new association to map
15118 if No (Actual_Map) then
15119 Actual_Map := New_Elmt_List;
15120 end if;
15122 Append_Elmt (Old_Itype, Actual_Map);
15123 Append_Elmt (New_Itype, Actual_Map);
15125 if NCT_Hash_Tables_Used then
15126 NCT_Assoc.Set (Old_Itype, New_Itype);
15128 else
15129 NCT_Table_Entries := NCT_Table_Entries + 1;
15131 if NCT_Table_Entries > NCT_Hash_Threshold then
15132 Build_NCT_Hash_Tables;
15133 end if;
15134 end if;
15136 -- If a record subtype is simply copied, the entity list will be
15137 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
15139 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
15140 Set_Cloned_Subtype (New_Itype, Old_Itype);
15141 end if;
15143 -- Visit descendents that eventually get copied
15145 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
15147 if Is_Discrete_Type (Old_Itype) then
15148 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
15150 elsif Has_Discriminants (Base_Type (Old_Itype)) then
15151 -- ??? This should involve call to Visit_Field
15152 Visit_Elist (Discriminant_Constraint (Old_Itype));
15154 elsif Is_Array_Type (Old_Itype) then
15155 if Present (First_Index (Old_Itype)) then
15156 Visit_Field (Union_Id (List_Containing
15157 (First_Index (Old_Itype))),
15158 Old_Itype);
15159 end if;
15161 if Is_Packed (Old_Itype) then
15162 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
15163 Old_Itype);
15164 end if;
15165 end if;
15166 end Visit_Itype;
15168 ----------------
15169 -- Visit_List --
15170 ----------------
15172 procedure Visit_List (L : List_Id) is
15173 N : Node_Id;
15174 begin
15175 if L /= No_List then
15176 N := First (L);
15178 while Present (N) loop
15179 Visit_Node (N);
15180 Next (N);
15181 end loop;
15182 end if;
15183 end Visit_List;
15185 ----------------
15186 -- Visit_Node --
15187 ----------------
15189 procedure Visit_Node (N : Node_Or_Entity_Id) is
15191 -- Start of processing for Visit_Node
15193 begin
15194 -- Handle case of an Itype, which must be copied
15196 if Has_Extension (N) and then Is_Itype (N) then
15198 -- Nothing to do if already in the list. This can happen with an
15199 -- Itype entity that appears more than once in the tree.
15200 -- Note that we do not want to visit descendents in this case.
15202 -- Test for already in list when hash table is used
15204 if NCT_Hash_Tables_Used then
15205 if Present (NCT_Assoc.Get (Entity_Id (N))) then
15206 return;
15207 end if;
15209 -- Test for already in list when hash table not used
15211 else
15212 declare
15213 E : Elmt_Id;
15214 begin
15215 if Present (Actual_Map) then
15216 E := First_Elmt (Actual_Map);
15217 while Present (E) loop
15218 if Node (E) = N then
15219 return;
15220 else
15221 E := Next_Elmt (Next_Elmt (E));
15222 end if;
15223 end loop;
15224 end if;
15225 end;
15226 end if;
15228 Visit_Itype (N);
15229 end if;
15231 -- Visit descendents
15233 Visit_Field (Field1 (N), N);
15234 Visit_Field (Field2 (N), N);
15235 Visit_Field (Field3 (N), N);
15236 Visit_Field (Field4 (N), N);
15237 Visit_Field (Field5 (N), N);
15238 end Visit_Node;
15240 -- Start of processing for New_Copy_Tree
15242 begin
15243 Actual_Map := Map;
15245 -- See if we should use hash table
15247 if No (Actual_Map) then
15248 NCT_Hash_Tables_Used := False;
15250 else
15251 declare
15252 Elmt : Elmt_Id;
15254 begin
15255 NCT_Table_Entries := 0;
15257 Elmt := First_Elmt (Actual_Map);
15258 while Present (Elmt) loop
15259 NCT_Table_Entries := NCT_Table_Entries + 1;
15260 Next_Elmt (Elmt);
15261 Next_Elmt (Elmt);
15262 end loop;
15264 if NCT_Table_Entries > NCT_Hash_Threshold then
15265 Build_NCT_Hash_Tables;
15266 else
15267 NCT_Hash_Tables_Used := False;
15268 end if;
15269 end;
15270 end if;
15272 -- Hash table set up if required, now start phase one by visiting
15273 -- top node (we will recursively visit the descendents).
15275 Visit_Node (Source);
15277 -- Now the second phase of the copy can start. First we process
15278 -- all the mapped entities, copying their descendents.
15280 if Present (Actual_Map) then
15281 declare
15282 Elmt : Elmt_Id;
15283 New_Itype : Entity_Id;
15284 begin
15285 Elmt := First_Elmt (Actual_Map);
15286 while Present (Elmt) loop
15287 Next_Elmt (Elmt);
15288 New_Itype := Node (Elmt);
15289 Copy_Itype_With_Replacement (New_Itype);
15290 Next_Elmt (Elmt);
15291 end loop;
15292 end;
15293 end if;
15295 -- Now we can copy the actual tree
15297 return Copy_Node_With_Replacement (Source);
15298 end New_Copy_Tree;
15300 -------------------------
15301 -- New_External_Entity --
15302 -------------------------
15304 function New_External_Entity
15305 (Kind : Entity_Kind;
15306 Scope_Id : Entity_Id;
15307 Sloc_Value : Source_Ptr;
15308 Related_Id : Entity_Id;
15309 Suffix : Character;
15310 Suffix_Index : Nat := 0;
15311 Prefix : Character := ' ') return Entity_Id
15313 N : constant Entity_Id :=
15314 Make_Defining_Identifier (Sloc_Value,
15315 New_External_Name
15316 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
15318 begin
15319 Set_Ekind (N, Kind);
15320 Set_Is_Internal (N, True);
15321 Append_Entity (N, Scope_Id);
15322 Set_Public_Status (N);
15324 if Kind in Type_Kind then
15325 Init_Size_Align (N);
15326 end if;
15328 return N;
15329 end New_External_Entity;
15331 -------------------------
15332 -- New_Internal_Entity --
15333 -------------------------
15335 function New_Internal_Entity
15336 (Kind : Entity_Kind;
15337 Scope_Id : Entity_Id;
15338 Sloc_Value : Source_Ptr;
15339 Id_Char : Character) return Entity_Id
15341 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
15343 begin
15344 Set_Ekind (N, Kind);
15345 Set_Is_Internal (N, True);
15346 Append_Entity (N, Scope_Id);
15348 if Kind in Type_Kind then
15349 Init_Size_Align (N);
15350 end if;
15352 return N;
15353 end New_Internal_Entity;
15355 -----------------
15356 -- Next_Actual --
15357 -----------------
15359 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
15360 N : Node_Id;
15362 begin
15363 -- If we are pointing at a positional parameter, it is a member of a
15364 -- node list (the list of parameters), and the next parameter is the
15365 -- next node on the list, unless we hit a parameter association, then
15366 -- we shift to using the chain whose head is the First_Named_Actual in
15367 -- the parent, and then is threaded using the Next_Named_Actual of the
15368 -- Parameter_Association. All this fiddling is because the original node
15369 -- list is in the textual call order, and what we need is the
15370 -- declaration order.
15372 if Is_List_Member (Actual_Id) then
15373 N := Next (Actual_Id);
15375 if Nkind (N) = N_Parameter_Association then
15376 return First_Named_Actual (Parent (Actual_Id));
15377 else
15378 return N;
15379 end if;
15381 else
15382 return Next_Named_Actual (Parent (Actual_Id));
15383 end if;
15384 end Next_Actual;
15386 procedure Next_Actual (Actual_Id : in out Node_Id) is
15387 begin
15388 Actual_Id := Next_Actual (Actual_Id);
15389 end Next_Actual;
15391 -----------------------
15392 -- Normalize_Actuals --
15393 -----------------------
15395 -- Chain actuals according to formals of subprogram. If there are no named
15396 -- associations, the chain is simply the list of Parameter Associations,
15397 -- since the order is the same as the declaration order. If there are named
15398 -- associations, then the First_Named_Actual field in the N_Function_Call
15399 -- or N_Procedure_Call_Statement node points to the Parameter_Association
15400 -- node for the parameter that comes first in declaration order. The
15401 -- remaining named parameters are then chained in declaration order using
15402 -- Next_Named_Actual.
15404 -- This routine also verifies that the number of actuals is compatible with
15405 -- the number and default values of formals, but performs no type checking
15406 -- (type checking is done by the caller).
15408 -- If the matching succeeds, Success is set to True and the caller proceeds
15409 -- with type-checking. If the match is unsuccessful, then Success is set to
15410 -- False, and the caller attempts a different interpretation, if there is
15411 -- one.
15413 -- If the flag Report is on, the call is not overloaded, and a failure to
15414 -- match can be reported here, rather than in the caller.
15416 procedure Normalize_Actuals
15417 (N : Node_Id;
15418 S : Entity_Id;
15419 Report : Boolean;
15420 Success : out Boolean)
15422 Actuals : constant List_Id := Parameter_Associations (N);
15423 Actual : Node_Id := Empty;
15424 Formal : Entity_Id;
15425 Last : Node_Id := Empty;
15426 First_Named : Node_Id := Empty;
15427 Found : Boolean;
15429 Formals_To_Match : Integer := 0;
15430 Actuals_To_Match : Integer := 0;
15432 procedure Chain (A : Node_Id);
15433 -- Add named actual at the proper place in the list, using the
15434 -- Next_Named_Actual link.
15436 function Reporting return Boolean;
15437 -- Determines if an error is to be reported. To report an error, we
15438 -- need Report to be True, and also we do not report errors caused
15439 -- by calls to init procs that occur within other init procs. Such
15440 -- errors must always be cascaded errors, since if all the types are
15441 -- declared correctly, the compiler will certainly build decent calls.
15443 -----------
15444 -- Chain --
15445 -----------
15447 procedure Chain (A : Node_Id) is
15448 begin
15449 if No (Last) then
15451 -- Call node points to first actual in list
15453 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
15455 else
15456 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
15457 end if;
15459 Last := A;
15460 Set_Next_Named_Actual (Last, Empty);
15461 end Chain;
15463 ---------------
15464 -- Reporting --
15465 ---------------
15467 function Reporting return Boolean is
15468 begin
15469 if not Report then
15470 return False;
15472 elsif not Within_Init_Proc then
15473 return True;
15475 elsif Is_Init_Proc (Entity (Name (N))) then
15476 return False;
15478 else
15479 return True;
15480 end if;
15481 end Reporting;
15483 -- Start of processing for Normalize_Actuals
15485 begin
15486 if Is_Access_Type (S) then
15488 -- The name in the call is a function call that returns an access
15489 -- to subprogram. The designated type has the list of formals.
15491 Formal := First_Formal (Designated_Type (S));
15492 else
15493 Formal := First_Formal (S);
15494 end if;
15496 while Present (Formal) loop
15497 Formals_To_Match := Formals_To_Match + 1;
15498 Next_Formal (Formal);
15499 end loop;
15501 -- Find if there is a named association, and verify that no positional
15502 -- associations appear after named ones.
15504 if Present (Actuals) then
15505 Actual := First (Actuals);
15506 end if;
15508 while Present (Actual)
15509 and then Nkind (Actual) /= N_Parameter_Association
15510 loop
15511 Actuals_To_Match := Actuals_To_Match + 1;
15512 Next (Actual);
15513 end loop;
15515 if No (Actual) and Actuals_To_Match = Formals_To_Match then
15517 -- Most common case: positional notation, no defaults
15519 Success := True;
15520 return;
15522 elsif Actuals_To_Match > Formals_To_Match then
15524 -- Too many actuals: will not work
15526 if Reporting then
15527 if Is_Entity_Name (Name (N)) then
15528 Error_Msg_N ("too many arguments in call to&", Name (N));
15529 else
15530 Error_Msg_N ("too many arguments in call", N);
15531 end if;
15532 end if;
15534 Success := False;
15535 return;
15536 end if;
15538 First_Named := Actual;
15540 while Present (Actual) loop
15541 if Nkind (Actual) /= N_Parameter_Association then
15542 Error_Msg_N
15543 ("positional parameters not allowed after named ones", Actual);
15544 Success := False;
15545 return;
15547 else
15548 Actuals_To_Match := Actuals_To_Match + 1;
15549 end if;
15551 Next (Actual);
15552 end loop;
15554 if Present (Actuals) then
15555 Actual := First (Actuals);
15556 end if;
15558 Formal := First_Formal (S);
15559 while Present (Formal) loop
15561 -- Match the formals in order. If the corresponding actual is
15562 -- positional, nothing to do. Else scan the list of named actuals
15563 -- to find the one with the right name.
15565 if Present (Actual)
15566 and then Nkind (Actual) /= N_Parameter_Association
15567 then
15568 Next (Actual);
15569 Actuals_To_Match := Actuals_To_Match - 1;
15570 Formals_To_Match := Formals_To_Match - 1;
15572 else
15573 -- For named parameters, search the list of actuals to find
15574 -- one that matches the next formal name.
15576 Actual := First_Named;
15577 Found := False;
15578 while Present (Actual) loop
15579 if Chars (Selector_Name (Actual)) = Chars (Formal) then
15580 Found := True;
15581 Chain (Actual);
15582 Actuals_To_Match := Actuals_To_Match - 1;
15583 Formals_To_Match := Formals_To_Match - 1;
15584 exit;
15585 end if;
15587 Next (Actual);
15588 end loop;
15590 if not Found then
15591 if Ekind (Formal) /= E_In_Parameter
15592 or else No (Default_Value (Formal))
15593 then
15594 if Reporting then
15595 if (Comes_From_Source (S)
15596 or else Sloc (S) = Standard_Location)
15597 and then Is_Overloadable (S)
15598 then
15599 if No (Actuals)
15600 and then
15601 Nkind_In (Parent (N), N_Procedure_Call_Statement,
15602 N_Function_Call,
15603 N_Parameter_Association)
15604 and then Ekind (S) /= E_Function
15605 then
15606 Set_Etype (N, Etype (S));
15608 else
15609 Error_Msg_Name_1 := Chars (S);
15610 Error_Msg_Sloc := Sloc (S);
15611 Error_Msg_NE
15612 ("missing argument for parameter & "
15613 & "in call to % declared #", N, Formal);
15614 end if;
15616 elsif Is_Overloadable (S) then
15617 Error_Msg_Name_1 := Chars (S);
15619 -- Point to type derivation that generated the
15620 -- operation.
15622 Error_Msg_Sloc := Sloc (Parent (S));
15624 Error_Msg_NE
15625 ("missing argument for parameter & "
15626 & "in call to % (inherited) #", N, Formal);
15628 else
15629 Error_Msg_NE
15630 ("missing argument for parameter &", N, Formal);
15631 end if;
15632 end if;
15634 Success := False;
15635 return;
15637 else
15638 Formals_To_Match := Formals_To_Match - 1;
15639 end if;
15640 end if;
15641 end if;
15643 Next_Formal (Formal);
15644 end loop;
15646 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
15647 Success := True;
15648 return;
15650 else
15651 if Reporting then
15653 -- Find some superfluous named actual that did not get
15654 -- attached to the list of associations.
15656 Actual := First (Actuals);
15657 while Present (Actual) loop
15658 if Nkind (Actual) = N_Parameter_Association
15659 and then Actual /= Last
15660 and then No (Next_Named_Actual (Actual))
15661 then
15662 Error_Msg_N ("unmatched actual & in call",
15663 Selector_Name (Actual));
15664 exit;
15665 end if;
15667 Next (Actual);
15668 end loop;
15669 end if;
15671 Success := False;
15672 return;
15673 end if;
15674 end Normalize_Actuals;
15676 --------------------------------
15677 -- Note_Possible_Modification --
15678 --------------------------------
15680 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
15681 Modification_Comes_From_Source : constant Boolean :=
15682 Comes_From_Source (Parent (N));
15684 Ent : Entity_Id;
15685 Exp : Node_Id;
15687 begin
15688 -- Loop to find referenced entity, if there is one
15690 Exp := N;
15691 loop
15692 Ent := Empty;
15694 if Is_Entity_Name (Exp) then
15695 Ent := Entity (Exp);
15697 -- If the entity is missing, it is an undeclared identifier,
15698 -- and there is nothing to annotate.
15700 if No (Ent) then
15701 return;
15702 end if;
15704 elsif Nkind (Exp) = N_Explicit_Dereference then
15705 declare
15706 P : constant Node_Id := Prefix (Exp);
15708 begin
15709 -- In formal verification mode, keep track of all reads and
15710 -- writes through explicit dereferences.
15712 if GNATprove_Mode then
15713 SPARK_Specific.Generate_Dereference (N, 'm');
15714 end if;
15716 if Nkind (P) = N_Selected_Component
15717 and then Present (Entry_Formal (Entity (Selector_Name (P))))
15718 then
15719 -- Case of a reference to an entry formal
15721 Ent := Entry_Formal (Entity (Selector_Name (P)));
15723 elsif Nkind (P) = N_Identifier
15724 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
15725 and then Present (Expression (Parent (Entity (P))))
15726 and then Nkind (Expression (Parent (Entity (P)))) =
15727 N_Reference
15728 then
15729 -- Case of a reference to a value on which side effects have
15730 -- been removed.
15732 Exp := Prefix (Expression (Parent (Entity (P))));
15733 goto Continue;
15735 else
15736 return;
15737 end if;
15738 end;
15740 elsif Nkind_In (Exp, N_Type_Conversion,
15741 N_Unchecked_Type_Conversion)
15742 then
15743 Exp := Expression (Exp);
15744 goto Continue;
15746 elsif Nkind_In (Exp, N_Slice,
15747 N_Indexed_Component,
15748 N_Selected_Component)
15749 then
15750 -- Special check, if the prefix is an access type, then return
15751 -- since we are modifying the thing pointed to, not the prefix.
15752 -- When we are expanding, most usually the prefix is replaced
15753 -- by an explicit dereference, and this test is not needed, but
15754 -- in some cases (notably -gnatc mode and generics) when we do
15755 -- not do full expansion, we need this special test.
15757 if Is_Access_Type (Etype (Prefix (Exp))) then
15758 return;
15760 -- Otherwise go to prefix and keep going
15762 else
15763 Exp := Prefix (Exp);
15764 goto Continue;
15765 end if;
15767 -- All other cases, not a modification
15769 else
15770 return;
15771 end if;
15773 -- Now look for entity being referenced
15775 if Present (Ent) then
15776 if Is_Object (Ent) then
15777 if Comes_From_Source (Exp)
15778 or else Modification_Comes_From_Source
15779 then
15780 -- Give warning if pragma unmodified given and we are
15781 -- sure this is a modification.
15783 if Has_Pragma_Unmodified (Ent) and then Sure then
15784 Error_Msg_NE ("??pragma Unmodified given for &!", N, Ent);
15785 end if;
15787 Set_Never_Set_In_Source (Ent, False);
15788 end if;
15790 Set_Is_True_Constant (Ent, False);
15791 Set_Current_Value (Ent, Empty);
15792 Set_Is_Known_Null (Ent, False);
15794 if not Can_Never_Be_Null (Ent) then
15795 Set_Is_Known_Non_Null (Ent, False);
15796 end if;
15798 -- Follow renaming chain
15800 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
15801 and then Present (Renamed_Object (Ent))
15802 then
15803 Exp := Renamed_Object (Ent);
15805 -- If the entity is the loop variable in an iteration over
15806 -- a container, retrieve container expression to indicate
15807 -- possible modification.
15809 if Present (Related_Expression (Ent))
15810 and then Nkind (Parent (Related_Expression (Ent))) =
15811 N_Iterator_Specification
15812 then
15813 Exp := Original_Node (Related_Expression (Ent));
15814 end if;
15816 goto Continue;
15818 -- The expression may be the renaming of a subcomponent of an
15819 -- array or container. The assignment to the subcomponent is
15820 -- a modification of the container.
15822 elsif Comes_From_Source (Original_Node (Exp))
15823 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
15824 N_Indexed_Component)
15825 then
15826 Exp := Prefix (Original_Node (Exp));
15827 goto Continue;
15828 end if;
15830 -- Generate a reference only if the assignment comes from
15831 -- source. This excludes, for example, calls to a dispatching
15832 -- assignment operation when the left-hand side is tagged. In
15833 -- GNATprove mode, we need those references also on generated
15834 -- code, as these are used to compute the local effects of
15835 -- subprograms.
15837 if Modification_Comes_From_Source or GNATprove_Mode then
15838 Generate_Reference (Ent, Exp, 'm');
15840 -- If the target of the assignment is the bound variable
15841 -- in an iterator, indicate that the corresponding array
15842 -- or container is also modified.
15844 if Ada_Version >= Ada_2012
15845 and then Nkind (Parent (Ent)) = N_Iterator_Specification
15846 then
15847 declare
15848 Domain : constant Node_Id := Name (Parent (Ent));
15850 begin
15851 -- TBD : in the full version of the construct, the
15852 -- domain of iteration can be given by an expression.
15854 if Is_Entity_Name (Domain) then
15855 Generate_Reference (Entity (Domain), Exp, 'm');
15856 Set_Is_True_Constant (Entity (Domain), False);
15857 Set_Never_Set_In_Source (Entity (Domain), False);
15858 end if;
15859 end;
15860 end if;
15861 end if;
15863 Check_Nested_Access (Ent);
15864 end if;
15866 Kill_Checks (Ent);
15868 -- If we are sure this is a modification from source, and we know
15869 -- this modifies a constant, then give an appropriate warning.
15871 if Overlays_Constant (Ent)
15872 and then (Modification_Comes_From_Source and Sure)
15873 then
15874 declare
15875 A : constant Node_Id := Address_Clause (Ent);
15876 begin
15877 if Present (A) then
15878 declare
15879 Exp : constant Node_Id := Expression (A);
15880 begin
15881 if Nkind (Exp) = N_Attribute_Reference
15882 and then Attribute_Name (Exp) = Name_Address
15883 and then Is_Entity_Name (Prefix (Exp))
15884 then
15885 Error_Msg_Sloc := Sloc (A);
15886 Error_Msg_NE
15887 ("constant& may be modified via address "
15888 & "clause#??", N, Entity (Prefix (Exp)));
15889 end if;
15890 end;
15891 end if;
15892 end;
15893 end if;
15895 return;
15896 end if;
15898 <<Continue>>
15899 null;
15900 end loop;
15901 end Note_Possible_Modification;
15903 -------------------------
15904 -- Object_Access_Level --
15905 -------------------------
15907 -- Returns the static accessibility level of the view denoted by Obj. Note
15908 -- that the value returned is the result of a call to Scope_Depth. Only
15909 -- scope depths associated with dynamic scopes can actually be returned.
15910 -- Since only relative levels matter for accessibility checking, the fact
15911 -- that the distance between successive levels of accessibility is not
15912 -- always one is immaterial (invariant: if level(E2) is deeper than
15913 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
15915 function Object_Access_Level (Obj : Node_Id) return Uint is
15916 function Is_Interface_Conversion (N : Node_Id) return Boolean;
15917 -- Determine whether N is a construct of the form
15918 -- Some_Type (Operand._tag'Address)
15919 -- This construct appears in the context of dispatching calls.
15921 function Reference_To (Obj : Node_Id) return Node_Id;
15922 -- An explicit dereference is created when removing side-effects from
15923 -- expressions for constraint checking purposes. In this case a local
15924 -- access type is created for it. The correct access level is that of
15925 -- the original source node. We detect this case by noting that the
15926 -- prefix of the dereference is created by an object declaration whose
15927 -- initial expression is a reference.
15929 -----------------------------
15930 -- Is_Interface_Conversion --
15931 -----------------------------
15933 function Is_Interface_Conversion (N : Node_Id) return Boolean is
15934 begin
15935 return Nkind (N) = N_Unchecked_Type_Conversion
15936 and then Nkind (Expression (N)) = N_Attribute_Reference
15937 and then Attribute_Name (Expression (N)) = Name_Address;
15938 end Is_Interface_Conversion;
15940 ------------------
15941 -- Reference_To --
15942 ------------------
15944 function Reference_To (Obj : Node_Id) return Node_Id is
15945 Pref : constant Node_Id := Prefix (Obj);
15946 begin
15947 if Is_Entity_Name (Pref)
15948 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
15949 and then Present (Expression (Parent (Entity (Pref))))
15950 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
15951 then
15952 return (Prefix (Expression (Parent (Entity (Pref)))));
15953 else
15954 return Empty;
15955 end if;
15956 end Reference_To;
15958 -- Local variables
15960 E : Entity_Id;
15962 -- Start of processing for Object_Access_Level
15964 begin
15965 if Nkind (Obj) = N_Defining_Identifier
15966 or else Is_Entity_Name (Obj)
15967 then
15968 if Nkind (Obj) = N_Defining_Identifier then
15969 E := Obj;
15970 else
15971 E := Entity (Obj);
15972 end if;
15974 if Is_Prival (E) then
15975 E := Prival_Link (E);
15976 end if;
15978 -- If E is a type then it denotes a current instance. For this case
15979 -- we add one to the normal accessibility level of the type to ensure
15980 -- that current instances are treated as always being deeper than
15981 -- than the level of any visible named access type (see 3.10.2(21)).
15983 if Is_Type (E) then
15984 return Type_Access_Level (E) + 1;
15986 elsif Present (Renamed_Object (E)) then
15987 return Object_Access_Level (Renamed_Object (E));
15989 -- Similarly, if E is a component of the current instance of a
15990 -- protected type, any instance of it is assumed to be at a deeper
15991 -- level than the type. For a protected object (whose type is an
15992 -- anonymous protected type) its components are at the same level
15993 -- as the type itself.
15995 elsif not Is_Overloadable (E)
15996 and then Ekind (Scope (E)) = E_Protected_Type
15997 and then Comes_From_Source (Scope (E))
15998 then
15999 return Type_Access_Level (Scope (E)) + 1;
16001 else
16002 -- Aliased formals take their access level from the point of call.
16003 -- This is smaller than the level of the subprogram itself.
16005 if Is_Formal (E) and then Is_Aliased (E) then
16006 return Type_Access_Level (Etype (E));
16008 else
16009 return Scope_Depth (Enclosing_Dynamic_Scope (E));
16010 end if;
16011 end if;
16013 elsif Nkind (Obj) = N_Selected_Component then
16014 if Is_Access_Type (Etype (Prefix (Obj))) then
16015 return Type_Access_Level (Etype (Prefix (Obj)));
16016 else
16017 return Object_Access_Level (Prefix (Obj));
16018 end if;
16020 elsif Nkind (Obj) = N_Indexed_Component then
16021 if Is_Access_Type (Etype (Prefix (Obj))) then
16022 return Type_Access_Level (Etype (Prefix (Obj)));
16023 else
16024 return Object_Access_Level (Prefix (Obj));
16025 end if;
16027 elsif Nkind (Obj) = N_Explicit_Dereference then
16029 -- If the prefix is a selected access discriminant then we make a
16030 -- recursive call on the prefix, which will in turn check the level
16031 -- of the prefix object of the selected discriminant.
16033 -- In Ada 2012, if the discriminant has implicit dereference and
16034 -- the context is a selected component, treat this as an object of
16035 -- unknown scope (see below). This is necessary in compile-only mode;
16036 -- otherwise expansion will already have transformed the prefix into
16037 -- a temporary.
16039 if Nkind (Prefix (Obj)) = N_Selected_Component
16040 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
16041 and then
16042 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
16043 and then
16044 (not Has_Implicit_Dereference
16045 (Entity (Selector_Name (Prefix (Obj))))
16046 or else Nkind (Parent (Obj)) /= N_Selected_Component)
16047 then
16048 return Object_Access_Level (Prefix (Obj));
16050 -- Detect an interface conversion in the context of a dispatching
16051 -- call. Use the original form of the conversion to find the access
16052 -- level of the operand.
16054 elsif Is_Interface (Etype (Obj))
16055 and then Is_Interface_Conversion (Prefix (Obj))
16056 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
16057 then
16058 return Object_Access_Level (Original_Node (Obj));
16060 elsif not Comes_From_Source (Obj) then
16061 declare
16062 Ref : constant Node_Id := Reference_To (Obj);
16063 begin
16064 if Present (Ref) then
16065 return Object_Access_Level (Ref);
16066 else
16067 return Type_Access_Level (Etype (Prefix (Obj)));
16068 end if;
16069 end;
16071 else
16072 return Type_Access_Level (Etype (Prefix (Obj)));
16073 end if;
16075 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
16076 return Object_Access_Level (Expression (Obj));
16078 elsif Nkind (Obj) = N_Function_Call then
16080 -- Function results are objects, so we get either the access level of
16081 -- the function or, in the case of an indirect call, the level of the
16082 -- access-to-subprogram type. (This code is used for Ada 95, but it
16083 -- looks wrong, because it seems that we should be checking the level
16084 -- of the call itself, even for Ada 95. However, using the Ada 2005
16085 -- version of the code causes regressions in several tests that are
16086 -- compiled with -gnat95. ???)
16088 if Ada_Version < Ada_2005 then
16089 if Is_Entity_Name (Name (Obj)) then
16090 return Subprogram_Access_Level (Entity (Name (Obj)));
16091 else
16092 return Type_Access_Level (Etype (Prefix (Name (Obj))));
16093 end if;
16095 -- For Ada 2005, the level of the result object of a function call is
16096 -- defined to be the level of the call's innermost enclosing master.
16097 -- We determine that by querying the depth of the innermost enclosing
16098 -- dynamic scope.
16100 else
16101 Return_Master_Scope_Depth_Of_Call : declare
16103 function Innermost_Master_Scope_Depth
16104 (N : Node_Id) return Uint;
16105 -- Returns the scope depth of the given node's innermost
16106 -- enclosing dynamic scope (effectively the accessibility
16107 -- level of the innermost enclosing master).
16109 ----------------------------------
16110 -- Innermost_Master_Scope_Depth --
16111 ----------------------------------
16113 function Innermost_Master_Scope_Depth
16114 (N : Node_Id) return Uint
16116 Node_Par : Node_Id := Parent (N);
16118 begin
16119 -- Locate the nearest enclosing node (by traversing Parents)
16120 -- that Defining_Entity can be applied to, and return the
16121 -- depth of that entity's nearest enclosing dynamic scope.
16123 while Present (Node_Par) loop
16124 case Nkind (Node_Par) is
16125 when N_Component_Declaration |
16126 N_Entry_Declaration |
16127 N_Formal_Object_Declaration |
16128 N_Formal_Type_Declaration |
16129 N_Full_Type_Declaration |
16130 N_Incomplete_Type_Declaration |
16131 N_Loop_Parameter_Specification |
16132 N_Object_Declaration |
16133 N_Protected_Type_Declaration |
16134 N_Private_Extension_Declaration |
16135 N_Private_Type_Declaration |
16136 N_Subtype_Declaration |
16137 N_Function_Specification |
16138 N_Procedure_Specification |
16139 N_Task_Type_Declaration |
16140 N_Body_Stub |
16141 N_Generic_Instantiation |
16142 N_Proper_Body |
16143 N_Implicit_Label_Declaration |
16144 N_Package_Declaration |
16145 N_Single_Task_Declaration |
16146 N_Subprogram_Declaration |
16147 N_Generic_Declaration |
16148 N_Renaming_Declaration |
16149 N_Block_Statement |
16150 N_Formal_Subprogram_Declaration |
16151 N_Abstract_Subprogram_Declaration |
16152 N_Entry_Body |
16153 N_Exception_Declaration |
16154 N_Formal_Package_Declaration |
16155 N_Number_Declaration |
16156 N_Package_Specification |
16157 N_Parameter_Specification |
16158 N_Single_Protected_Declaration |
16159 N_Subunit =>
16161 return Scope_Depth
16162 (Nearest_Dynamic_Scope
16163 (Defining_Entity (Node_Par)));
16165 when others =>
16166 null;
16167 end case;
16169 Node_Par := Parent (Node_Par);
16170 end loop;
16172 pragma Assert (False);
16174 -- Should never reach the following return
16176 return Scope_Depth (Current_Scope) + 1;
16177 end Innermost_Master_Scope_Depth;
16179 -- Start of processing for Return_Master_Scope_Depth_Of_Call
16181 begin
16182 return Innermost_Master_Scope_Depth (Obj);
16183 end Return_Master_Scope_Depth_Of_Call;
16184 end if;
16186 -- For convenience we handle qualified expressions, even though they
16187 -- aren't technically object names.
16189 elsif Nkind (Obj) = N_Qualified_Expression then
16190 return Object_Access_Level (Expression (Obj));
16192 -- Ditto for aggregates. They have the level of the temporary that
16193 -- will hold their value.
16195 elsif Nkind (Obj) = N_Aggregate then
16196 return Object_Access_Level (Current_Scope);
16198 -- Otherwise return the scope level of Standard. (If there are cases
16199 -- that fall through to this point they will be treated as having
16200 -- global accessibility for now. ???)
16202 else
16203 return Scope_Depth (Standard_Standard);
16204 end if;
16205 end Object_Access_Level;
16207 ---------------------------------
16208 -- Original_Aspect_Pragma_Name --
16209 ---------------------------------
16211 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
16212 Item : Node_Id;
16213 Item_Nam : Name_Id;
16215 begin
16216 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
16218 Item := N;
16220 -- The pragma was generated to emulate an aspect, use the original
16221 -- aspect specification.
16223 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
16224 Item := Corresponding_Aspect (Item);
16225 end if;
16227 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
16228 -- Post and Post_Class rewrite their pragma identifier to preserve the
16229 -- original name.
16230 -- ??? this is kludgey
16232 if Nkind (Item) = N_Pragma then
16233 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
16235 else
16236 pragma Assert (Nkind (Item) = N_Aspect_Specification);
16237 Item_Nam := Chars (Identifier (Item));
16238 end if;
16240 -- Deal with 'Class by converting the name to its _XXX form
16242 if Class_Present (Item) then
16243 if Item_Nam = Name_Invariant then
16244 Item_Nam := Name_uInvariant;
16246 elsif Item_Nam = Name_Post then
16247 Item_Nam := Name_uPost;
16249 elsif Item_Nam = Name_Pre then
16250 Item_Nam := Name_uPre;
16252 elsif Nam_In (Item_Nam, Name_Type_Invariant,
16253 Name_Type_Invariant_Class)
16254 then
16255 Item_Nam := Name_uType_Invariant;
16257 -- Nothing to do for other cases (e.g. a Check that derived from
16258 -- Pre_Class and has the flag set). Also we do nothing if the name
16259 -- is already in special _xxx form.
16261 end if;
16262 end if;
16264 return Item_Nam;
16265 end Original_Aspect_Pragma_Name;
16267 --------------------------------------
16268 -- Original_Corresponding_Operation --
16269 --------------------------------------
16271 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
16273 Typ : constant Entity_Id := Find_Dispatching_Type (S);
16275 begin
16276 -- If S is an inherited primitive S2 the original corresponding
16277 -- operation of S is the original corresponding operation of S2
16279 if Present (Alias (S))
16280 and then Find_Dispatching_Type (Alias (S)) /= Typ
16281 then
16282 return Original_Corresponding_Operation (Alias (S));
16284 -- If S overrides an inherited subprogram S2 the original corresponding
16285 -- operation of S is the original corresponding operation of S2
16287 elsif Present (Overridden_Operation (S)) then
16288 return Original_Corresponding_Operation (Overridden_Operation (S));
16290 -- otherwise it is S itself
16292 else
16293 return S;
16294 end if;
16295 end Original_Corresponding_Operation;
16297 ----------------------
16298 -- Policy_In_Effect --
16299 ----------------------
16301 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
16302 function Policy_In_List (List : Node_Id) return Name_Id;
16303 -- Determine the mode of a policy in a N_Pragma list
16305 --------------------
16306 -- Policy_In_List --
16307 --------------------
16309 function Policy_In_List (List : Node_Id) return Name_Id is
16310 Arg1 : Node_Id;
16311 Arg2 : Node_Id;
16312 Prag : Node_Id;
16314 begin
16315 Prag := List;
16316 while Present (Prag) loop
16317 Arg1 := First (Pragma_Argument_Associations (Prag));
16318 Arg2 := Next (Arg1);
16320 Arg1 := Get_Pragma_Arg (Arg1);
16321 Arg2 := Get_Pragma_Arg (Arg2);
16323 -- The current Check_Policy pragma matches the requested policy or
16324 -- appears in the single argument form (Assertion, policy_id).
16326 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
16327 return Chars (Arg2);
16328 end if;
16330 Prag := Next_Pragma (Prag);
16331 end loop;
16333 return No_Name;
16334 end Policy_In_List;
16336 -- Local variables
16338 Kind : Name_Id;
16340 -- Start of processing for Policy_In_Effect
16342 begin
16343 if not Is_Valid_Assertion_Kind (Policy) then
16344 raise Program_Error;
16345 end if;
16347 -- Inspect all policy pragmas that appear within scopes (if any)
16349 Kind := Policy_In_List (Check_Policy_List);
16351 -- Inspect all configuration policy pragmas (if any)
16353 if Kind = No_Name then
16354 Kind := Policy_In_List (Check_Policy_List_Config);
16355 end if;
16357 -- The context lacks policy pragmas, determine the mode based on whether
16358 -- assertions are enabled at the configuration level. This ensures that
16359 -- the policy is preserved when analyzing generics.
16361 if Kind = No_Name then
16362 if Assertions_Enabled_Config then
16363 Kind := Name_Check;
16364 else
16365 Kind := Name_Ignore;
16366 end if;
16367 end if;
16369 return Kind;
16370 end Policy_In_Effect;
16372 ----------------------------------
16373 -- Predicate_Tests_On_Arguments --
16374 ----------------------------------
16376 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
16377 begin
16378 -- Always test predicates on indirect call
16380 if Ekind (Subp) = E_Subprogram_Type then
16381 return True;
16383 -- Do not test predicates on call to generated default Finalize, since
16384 -- we are not interested in whether something we are finalizing (and
16385 -- typically destroying) satisfies its predicates.
16387 elsif Chars (Subp) = Name_Finalize
16388 and then not Comes_From_Source (Subp)
16389 then
16390 return False;
16392 -- Do not test predicates on any internally generated routines
16394 elsif Is_Internal_Name (Chars (Subp)) then
16395 return False;
16397 -- Do not test predicates on call to Init_Proc, since if needed the
16398 -- predicate test will occur at some other point.
16400 elsif Is_Init_Proc (Subp) then
16401 return False;
16403 -- Do not test predicates on call to predicate function, since this
16404 -- would cause infinite recursion.
16406 elsif Ekind (Subp) = E_Function
16407 and then (Is_Predicate_Function (Subp)
16408 or else
16409 Is_Predicate_Function_M (Subp))
16410 then
16411 return False;
16413 -- For now, no other exceptions
16415 else
16416 return True;
16417 end if;
16418 end Predicate_Tests_On_Arguments;
16420 -----------------------
16421 -- Private_Component --
16422 -----------------------
16424 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
16425 Ancestor : constant Entity_Id := Base_Type (Type_Id);
16427 function Trace_Components
16428 (T : Entity_Id;
16429 Check : Boolean) return Entity_Id;
16430 -- Recursive function that does the work, and checks against circular
16431 -- definition for each subcomponent type.
16433 ----------------------
16434 -- Trace_Components --
16435 ----------------------
16437 function Trace_Components
16438 (T : Entity_Id;
16439 Check : Boolean) return Entity_Id
16441 Btype : constant Entity_Id := Base_Type (T);
16442 Component : Entity_Id;
16443 P : Entity_Id;
16444 Candidate : Entity_Id := Empty;
16446 begin
16447 if Check and then Btype = Ancestor then
16448 Error_Msg_N ("circular type definition", Type_Id);
16449 return Any_Type;
16450 end if;
16452 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
16453 if Present (Full_View (Btype))
16454 and then Is_Record_Type (Full_View (Btype))
16455 and then not Is_Frozen (Btype)
16456 then
16457 -- To indicate that the ancestor depends on a private type, the
16458 -- current Btype is sufficient. However, to check for circular
16459 -- definition we must recurse on the full view.
16461 Candidate := Trace_Components (Full_View (Btype), True);
16463 if Candidate = Any_Type then
16464 return Any_Type;
16465 else
16466 return Btype;
16467 end if;
16469 else
16470 return Btype;
16471 end if;
16473 elsif Is_Array_Type (Btype) then
16474 return Trace_Components (Component_Type (Btype), True);
16476 elsif Is_Record_Type (Btype) then
16477 Component := First_Entity (Btype);
16478 while Present (Component)
16479 and then Comes_From_Source (Component)
16480 loop
16481 -- Skip anonymous types generated by constrained components
16483 if not Is_Type (Component) then
16484 P := Trace_Components (Etype (Component), True);
16486 if Present (P) then
16487 if P = Any_Type then
16488 return P;
16489 else
16490 Candidate := P;
16491 end if;
16492 end if;
16493 end if;
16495 Next_Entity (Component);
16496 end loop;
16498 return Candidate;
16500 else
16501 return Empty;
16502 end if;
16503 end Trace_Components;
16505 -- Start of processing for Private_Component
16507 begin
16508 return Trace_Components (Type_Id, False);
16509 end Private_Component;
16511 ---------------------------
16512 -- Primitive_Names_Match --
16513 ---------------------------
16515 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
16517 function Non_Internal_Name (E : Entity_Id) return Name_Id;
16518 -- Given an internal name, returns the corresponding non-internal name
16520 ------------------------
16521 -- Non_Internal_Name --
16522 ------------------------
16524 function Non_Internal_Name (E : Entity_Id) return Name_Id is
16525 begin
16526 Get_Name_String (Chars (E));
16527 Name_Len := Name_Len - 1;
16528 return Name_Find;
16529 end Non_Internal_Name;
16531 -- Start of processing for Primitive_Names_Match
16533 begin
16534 pragma Assert (Present (E1) and then Present (E2));
16536 return Chars (E1) = Chars (E2)
16537 or else
16538 (not Is_Internal_Name (Chars (E1))
16539 and then Is_Internal_Name (Chars (E2))
16540 and then Non_Internal_Name (E2) = Chars (E1))
16541 or else
16542 (not Is_Internal_Name (Chars (E2))
16543 and then Is_Internal_Name (Chars (E1))
16544 and then Non_Internal_Name (E1) = Chars (E2))
16545 or else
16546 (Is_Predefined_Dispatching_Operation (E1)
16547 and then Is_Predefined_Dispatching_Operation (E2)
16548 and then Same_TSS (E1, E2))
16549 or else
16550 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
16551 end Primitive_Names_Match;
16553 -----------------------
16554 -- Process_End_Label --
16555 -----------------------
16557 procedure Process_End_Label
16558 (N : Node_Id;
16559 Typ : Character;
16560 Ent : Entity_Id)
16562 Loc : Source_Ptr;
16563 Nam : Node_Id;
16564 Scop : Entity_Id;
16566 Label_Ref : Boolean;
16567 -- Set True if reference to end label itself is required
16569 Endl : Node_Id;
16570 -- Gets set to the operator symbol or identifier that references the
16571 -- entity Ent. For the child unit case, this is the identifier from the
16572 -- designator. For other cases, this is simply Endl.
16574 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
16575 -- N is an identifier node that appears as a parent unit reference in
16576 -- the case where Ent is a child unit. This procedure generates an
16577 -- appropriate cross-reference entry. E is the corresponding entity.
16579 -------------------------
16580 -- Generate_Parent_Ref --
16581 -------------------------
16583 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
16584 begin
16585 -- If names do not match, something weird, skip reference
16587 if Chars (E) = Chars (N) then
16589 -- Generate the reference. We do NOT consider this as a reference
16590 -- for unreferenced symbol purposes.
16592 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
16594 if Style_Check then
16595 Style.Check_Identifier (N, E);
16596 end if;
16597 end if;
16598 end Generate_Parent_Ref;
16600 -- Start of processing for Process_End_Label
16602 begin
16603 -- If no node, ignore. This happens in some error situations, and
16604 -- also for some internally generated structures where no end label
16605 -- references are required in any case.
16607 if No (N) then
16608 return;
16609 end if;
16611 -- Nothing to do if no End_Label, happens for internally generated
16612 -- constructs where we don't want an end label reference anyway. Also
16613 -- nothing to do if Endl is a string literal, which means there was
16614 -- some prior error (bad operator symbol)
16616 Endl := End_Label (N);
16618 if No (Endl) or else Nkind (Endl) = N_String_Literal then
16619 return;
16620 end if;
16622 -- Reference node is not in extended main source unit
16624 if not In_Extended_Main_Source_Unit (N) then
16626 -- Generally we do not collect references except for the extended
16627 -- main source unit. The one exception is the 'e' entry for a
16628 -- package spec, where it is useful for a client to have the
16629 -- ending information to define scopes.
16631 if Typ /= 'e' then
16632 return;
16634 else
16635 Label_Ref := False;
16637 -- For this case, we can ignore any parent references, but we
16638 -- need the package name itself for the 'e' entry.
16640 if Nkind (Endl) = N_Designator then
16641 Endl := Identifier (Endl);
16642 end if;
16643 end if;
16645 -- Reference is in extended main source unit
16647 else
16648 Label_Ref := True;
16650 -- For designator, generate references for the parent entries
16652 if Nkind (Endl) = N_Designator then
16654 -- Generate references for the prefix if the END line comes from
16655 -- source (otherwise we do not need these references) We climb the
16656 -- scope stack to find the expected entities.
16658 if Comes_From_Source (Endl) then
16659 Nam := Name (Endl);
16660 Scop := Current_Scope;
16661 while Nkind (Nam) = N_Selected_Component loop
16662 Scop := Scope (Scop);
16663 exit when No (Scop);
16664 Generate_Parent_Ref (Selector_Name (Nam), Scop);
16665 Nam := Prefix (Nam);
16666 end loop;
16668 if Present (Scop) then
16669 Generate_Parent_Ref (Nam, Scope (Scop));
16670 end if;
16671 end if;
16673 Endl := Identifier (Endl);
16674 end if;
16675 end if;
16677 -- If the end label is not for the given entity, then either we have
16678 -- some previous error, or this is a generic instantiation for which
16679 -- we do not need to make a cross-reference in this case anyway. In
16680 -- either case we simply ignore the call.
16682 if Chars (Ent) /= Chars (Endl) then
16683 return;
16684 end if;
16686 -- If label was really there, then generate a normal reference and then
16687 -- adjust the location in the end label to point past the name (which
16688 -- should almost always be the semicolon).
16690 Loc := Sloc (Endl);
16692 if Comes_From_Source (Endl) then
16694 -- If a label reference is required, then do the style check and
16695 -- generate an l-type cross-reference entry for the label
16697 if Label_Ref then
16698 if Style_Check then
16699 Style.Check_Identifier (Endl, Ent);
16700 end if;
16702 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
16703 end if;
16705 -- Set the location to point past the label (normally this will
16706 -- mean the semicolon immediately following the label). This is
16707 -- done for the sake of the 'e' or 't' entry generated below.
16709 Get_Decoded_Name_String (Chars (Endl));
16710 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
16712 else
16713 -- In SPARK mode, no missing label is allowed for packages and
16714 -- subprogram bodies. Detect those cases by testing whether
16715 -- Process_End_Label was called for a body (Typ = 't') or a package.
16717 if Restriction_Check_Required (SPARK_05)
16718 and then (Typ = 't' or else Ekind (Ent) = E_Package)
16719 then
16720 Error_Msg_Node_1 := Endl;
16721 Check_SPARK_05_Restriction
16722 ("`END &` required", Endl, Force => True);
16723 end if;
16724 end if;
16726 -- Now generate the e/t reference
16728 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
16730 -- Restore Sloc, in case modified above, since we have an identifier
16731 -- and the normal Sloc should be left set in the tree.
16733 Set_Sloc (Endl, Loc);
16734 end Process_End_Label;
16736 ----------------
16737 -- Referenced --
16738 ----------------
16740 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
16741 Seen : Boolean := False;
16743 function Is_Reference (N : Node_Id) return Traverse_Result;
16744 -- Determine whether node N denotes a reference to Id. If this is the
16745 -- case, set global flag Seen to True and stop the traversal.
16747 ------------------
16748 -- Is_Reference --
16749 ------------------
16751 function Is_Reference (N : Node_Id) return Traverse_Result is
16752 begin
16753 if Is_Entity_Name (N)
16754 and then Present (Entity (N))
16755 and then Entity (N) = Id
16756 then
16757 Seen := True;
16758 return Abandon;
16759 else
16760 return OK;
16761 end if;
16762 end Is_Reference;
16764 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
16766 -- Start of processing for Referenced
16768 begin
16769 Inspect_Expression (Expr);
16770 return Seen;
16771 end Referenced;
16773 ------------------------------------
16774 -- References_Generic_Formal_Type --
16775 ------------------------------------
16777 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
16779 function Process (N : Node_Id) return Traverse_Result;
16780 -- Process one node in search for generic formal type
16782 -------------
16783 -- Process --
16784 -------------
16786 function Process (N : Node_Id) return Traverse_Result is
16787 begin
16788 if Nkind (N) in N_Has_Entity then
16789 declare
16790 E : constant Entity_Id := Entity (N);
16791 begin
16792 if Present (E) then
16793 if Is_Generic_Type (E) then
16794 return Abandon;
16795 elsif Present (Etype (E))
16796 and then Is_Generic_Type (Etype (E))
16797 then
16798 return Abandon;
16799 end if;
16800 end if;
16801 end;
16802 end if;
16804 return Atree.OK;
16805 end Process;
16807 function Traverse is new Traverse_Func (Process);
16808 -- Traverse tree to look for generic type
16810 begin
16811 if Inside_A_Generic then
16812 return Traverse (N) = Abandon;
16813 else
16814 return False;
16815 end if;
16816 end References_Generic_Formal_Type;
16818 --------------------
16819 -- Remove_Homonym --
16820 --------------------
16822 procedure Remove_Homonym (E : Entity_Id) is
16823 Prev : Entity_Id := Empty;
16824 H : Entity_Id;
16826 begin
16827 if E = Current_Entity (E) then
16828 if Present (Homonym (E)) then
16829 Set_Current_Entity (Homonym (E));
16830 else
16831 Set_Name_Entity_Id (Chars (E), Empty);
16832 end if;
16834 else
16835 H := Current_Entity (E);
16836 while Present (H) and then H /= E loop
16837 Prev := H;
16838 H := Homonym (H);
16839 end loop;
16841 -- If E is not on the homonym chain, nothing to do
16843 if Present (H) then
16844 Set_Homonym (Prev, Homonym (E));
16845 end if;
16846 end if;
16847 end Remove_Homonym;
16849 ---------------------
16850 -- Rep_To_Pos_Flag --
16851 ---------------------
16853 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
16854 begin
16855 return New_Occurrence_Of
16856 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
16857 end Rep_To_Pos_Flag;
16859 --------------------
16860 -- Require_Entity --
16861 --------------------
16863 procedure Require_Entity (N : Node_Id) is
16864 begin
16865 if Is_Entity_Name (N) and then No (Entity (N)) then
16866 if Total_Errors_Detected /= 0 then
16867 Set_Entity (N, Any_Id);
16868 else
16869 raise Program_Error;
16870 end if;
16871 end if;
16872 end Require_Entity;
16874 -------------------------------
16875 -- Requires_State_Refinement --
16876 -------------------------------
16878 function Requires_State_Refinement
16879 (Spec_Id : Entity_Id;
16880 Body_Id : Entity_Id) return Boolean
16882 function Mode_Is_Off (Prag : Node_Id) return Boolean;
16883 -- Given pragma SPARK_Mode, determine whether the mode is Off
16885 -----------------
16886 -- Mode_Is_Off --
16887 -----------------
16889 function Mode_Is_Off (Prag : Node_Id) return Boolean is
16890 Mode : Node_Id;
16892 begin
16893 -- The default SPARK mode is On
16895 if No (Prag) then
16896 return False;
16897 end if;
16899 Mode := Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
16901 -- Then the pragma lacks an argument, the default mode is On
16903 if No (Mode) then
16904 return False;
16905 else
16906 return Chars (Mode) = Name_Off;
16907 end if;
16908 end Mode_Is_Off;
16910 -- Start of processing for Requires_State_Refinement
16912 begin
16913 -- A package that does not define at least one abstract state cannot
16914 -- possibly require refinement.
16916 if No (Abstract_States (Spec_Id)) then
16917 return False;
16919 -- The package instroduces a single null state which does not merit
16920 -- refinement.
16922 elsif Has_Null_Abstract_State (Spec_Id) then
16923 return False;
16925 -- Check whether the package body is subject to pragma SPARK_Mode. If
16926 -- it is and the mode is Off, the package body is considered to be in
16927 -- regular Ada and does not require refinement.
16929 elsif Mode_Is_Off (SPARK_Pragma (Body_Id)) then
16930 return False;
16932 -- The body's SPARK_Mode may be inherited from a similar pragma that
16933 -- appears in the private declarations of the spec. The pragma we are
16934 -- interested appears as the second entry in SPARK_Pragma.
16936 elsif Present (SPARK_Pragma (Spec_Id))
16937 and then Mode_Is_Off (Next_Pragma (SPARK_Pragma (Spec_Id)))
16938 then
16939 return False;
16941 -- The spec defines at least one abstract state and the body has no way
16942 -- of circumventing the refinement.
16944 else
16945 return True;
16946 end if;
16947 end Requires_State_Refinement;
16949 ------------------------------
16950 -- Requires_Transient_Scope --
16951 ------------------------------
16953 -- A transient scope is required when variable-sized temporaries are
16954 -- allocated on the secondary stack, or when finalization actions must be
16955 -- generated before the next instruction.
16957 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
16958 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
16959 -- ???We retain the old and new algorithms for Requires_Transient_Scope for
16960 -- the time being. New_Requires_Transient_Scope is used by default; the
16961 -- debug switch -gnatdQ can be used to do Old_Requires_Transient_Scope
16962 -- instead. The intent is to use this temporarily to measure before/after
16963 -- efficiency. Note: when this temporary code is removed, the documentation
16964 -- of dQ in debug.adb should be removed.
16966 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
16967 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
16969 begin
16970 if Debug_Flag_QQ then
16971 return Old_Result;
16972 end if;
16974 declare
16975 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
16977 begin
16978 -- Assert that we're not putting things on the secondary stack if we
16979 -- didn't before; we are trying to AVOID secondary stack when
16980 -- possible.
16982 if not Old_Result then
16983 pragma Assert (not New_Result);
16984 null;
16985 end if;
16987 return New_Result;
16988 end;
16989 end Requires_Transient_Scope;
16991 ----------------------------------
16992 -- Old_Requires_Transient_Scope --
16993 ----------------------------------
16995 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
16996 Typ : constant Entity_Id := Underlying_Type (Id);
16998 begin
16999 -- This is a private type which is not completed yet. This can only
17000 -- happen in a default expression (of a formal parameter or of a
17001 -- record component). Do not expand transient scope in this case
17003 if No (Typ) then
17004 return False;
17006 -- Do not expand transient scope for non-existent procedure return
17008 elsif Typ = Standard_Void_Type then
17009 return False;
17011 -- Elementary types do not require a transient scope
17013 elsif Is_Elementary_Type (Typ) then
17014 return False;
17016 -- Generally, indefinite subtypes require a transient scope, since the
17017 -- back end cannot generate temporaries, since this is not a valid type
17018 -- for declaring an object. It might be possible to relax this in the
17019 -- future, e.g. by declaring the maximum possible space for the type.
17021 elsif not Is_Definite_Subtype (Typ) then
17022 return True;
17024 -- Functions returning tagged types may dispatch on result so their
17025 -- returned value is allocated on the secondary stack. Controlled
17026 -- type temporaries need finalization.
17028 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
17029 return not Is_Value_Type (Typ);
17031 -- Record type
17033 elsif Is_Record_Type (Typ) then
17034 declare
17035 Comp : Entity_Id;
17037 begin
17038 Comp := First_Entity (Typ);
17039 while Present (Comp) loop
17040 if Ekind (Comp) = E_Component then
17042 -- ???It's not clear we need a full recursive call to
17043 -- Old_Requires_Transient_Scope here. Note that the
17044 -- following can't happen.
17046 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
17047 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
17049 if Old_Requires_Transient_Scope (Etype (Comp)) then
17050 return True;
17051 end if;
17052 end if;
17054 Next_Entity (Comp);
17055 end loop;
17056 end;
17058 return False;
17060 -- String literal types never require transient scope
17062 elsif Ekind (Typ) = E_String_Literal_Subtype then
17063 return False;
17065 -- Array type. Note that we already know that this is a constrained
17066 -- array, since unconstrained arrays will fail the indefinite test.
17068 elsif Is_Array_Type (Typ) then
17070 -- If component type requires a transient scope, the array does too
17072 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
17073 return True;
17075 -- Otherwise, we only need a transient scope if the size depends on
17076 -- the value of one or more discriminants.
17078 else
17079 return Size_Depends_On_Discriminant (Typ);
17080 end if;
17082 -- All other cases do not require a transient scope
17084 else
17085 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
17086 return False;
17087 end if;
17088 end Old_Requires_Transient_Scope;
17090 ----------------------------------
17091 -- New_Requires_Transient_Scope --
17092 ----------------------------------
17094 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
17096 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
17097 -- This is called for untagged records and protected types, with
17098 -- nondefaulted discriminants. Returns True if the size of function
17099 -- results is known at the call site, False otherwise. Returns False
17100 -- if there is a variant part that depends on the discriminants of
17101 -- this type, or if there is an array constrained by the discriminants
17102 -- of this type. ???Currently, this is overly conservative (the array
17103 -- could be nested inside some other record that is constrained by
17104 -- nondiscriminants). That is, the recursive calls are too conservative.
17106 function Has_Discrim_Dep_Array (Typ : Entity_Id) return Boolean;
17107 -- True if we find certain discriminant-dependent array
17108 -- subcomponents. This shouldn't be necessary, but without this check,
17109 -- we crash in gimplify. ???
17111 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
17112 pragma Assert (Typ = Underlying_Type (Typ));
17114 begin
17115 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
17116 return False;
17117 end if;
17119 declare
17120 Comp : Entity_Id := First_Entity (Typ);
17122 begin
17123 while Present (Comp) loop
17125 -- Only look at E_Component entities. No need to look at
17126 -- E_Discriminant entities, and we must ignore internal
17127 -- subtypes generated for constrained components.
17129 if Ekind (Comp) = E_Component then
17130 declare
17131 Comp_Type : constant Entity_Id :=
17132 Underlying_Type (Etype (Comp));
17134 begin
17135 if Is_Record_Type (Comp_Type)
17136 or else
17137 Is_Protected_Type (Comp_Type)
17138 then
17139 if not Caller_Known_Size_Record (Comp_Type) then
17140 return False;
17141 end if;
17143 elsif Is_Array_Type (Comp_Type) then
17144 if Size_Depends_On_Discriminant (Comp_Type) then
17145 return False;
17146 end if;
17147 end if;
17148 end;
17149 end if;
17151 Next_Entity (Comp);
17152 end loop;
17153 end;
17155 return True;
17156 end Caller_Known_Size_Record;
17158 function Has_Discrim_Dep_Array (Typ : Entity_Id) return Boolean is
17159 pragma Assert (Typ = Underlying_Type (Typ));
17161 begin
17162 if Is_Array_Type (Typ) then
17163 return Size_Depends_On_Discriminant (Typ);
17164 end if;
17166 if Is_Record_Type (Typ)
17167 or else
17168 Is_Protected_Type (Typ)
17169 then
17170 declare
17171 Comp : Entity_Id := First_Entity (Typ);
17173 begin
17174 while Present (Comp) loop
17176 -- Only look at E_Component entities. No need to look at
17177 -- E_Discriminant entities, and we must ignore internal
17178 -- subtypes generated for constrained components.
17180 if Ekind (Comp) = E_Component then
17181 declare
17182 Comp_Type : constant Entity_Id :=
17183 Underlying_Type (Etype (Comp));
17185 begin
17186 if Has_Discrim_Dep_Array (Comp_Type) then
17187 return True;
17188 end if;
17189 end;
17190 end if;
17192 Next_Entity (Comp);
17193 end loop;
17194 end;
17195 end if;
17197 return False;
17198 end Has_Discrim_Dep_Array;
17200 -- Local declarations
17202 Typ : constant Entity_Id := Underlying_Type (Id);
17204 -- Start of processing for New_Requires_Transient_Scope
17206 begin
17207 -- This is a private type which is not completed yet. This can only
17208 -- happen in a default expression (of a formal parameter or of a
17209 -- record component). Do not expand transient scope in this case
17211 if No (Typ) then
17212 return False;
17214 -- Do not expand transient scope for non-existent procedure return or
17215 -- string literal types.
17217 elsif Typ = Standard_Void_Type
17218 or else Ekind (Typ) = E_String_Literal_Subtype
17219 then
17220 return False;
17222 -- Functions returning tagged types may dispatch on result so their
17223 -- returned value is allocated on the secondary stack, even in the
17224 -- definite case. Is_Tagged_Type includes controlled types and
17225 -- class-wide types. Controlled type temporaries need finalization.
17226 -- ???It's not clear why we need to return noncontrolled types with
17227 -- controlled components on the secondary stack. Also, it's not clear
17228 -- why nonprimitive tagged type functions need the secondary stack,
17229 -- since they can't be called via dispatching.
17231 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
17232 return not Is_Value_Type (Typ);
17234 -- Untagged definite subtypes are known size. This includes all
17235 -- elementary [sub]types. Tasks are known size even if they have
17236 -- discriminants.
17238 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
17239 if Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
17240 if not Has_Discriminants (Typ) then
17241 if Has_Discrim_Dep_Array (Typ) then
17242 return True; -- ???Shouldn't be necessary
17243 end if;
17244 end if;
17245 end if;
17247 return False;
17249 -- Indefinite (discriminated) untagged record or protected type
17251 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
17252 return not Caller_Known_Size_Record (Typ);
17254 -- Unconstrained array
17256 else
17257 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
17258 return True;
17259 end if;
17260 end New_Requires_Transient_Scope;
17262 --------------------------
17263 -- Reset_Analyzed_Flags --
17264 --------------------------
17266 procedure Reset_Analyzed_Flags (N : Node_Id) is
17268 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
17269 -- Function used to reset Analyzed flags in tree. Note that we do
17270 -- not reset Analyzed flags in entities, since there is no need to
17271 -- reanalyze entities, and indeed, it is wrong to do so, since it
17272 -- can result in generating auxiliary stuff more than once.
17274 --------------------
17275 -- Clear_Analyzed --
17276 --------------------
17278 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
17279 begin
17280 if not Has_Extension (N) then
17281 Set_Analyzed (N, False);
17282 end if;
17284 return OK;
17285 end Clear_Analyzed;
17287 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
17289 -- Start of processing for Reset_Analyzed_Flags
17291 begin
17292 Reset_Analyzed (N);
17293 end Reset_Analyzed_Flags;
17295 ------------------------
17296 -- Restore_SPARK_Mode --
17297 ------------------------
17299 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
17300 begin
17301 SPARK_Mode := Mode;
17302 end Restore_SPARK_Mode;
17304 --------------------------------
17305 -- Returns_Unconstrained_Type --
17306 --------------------------------
17308 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
17309 begin
17310 return Ekind (Subp) = E_Function
17311 and then not Is_Scalar_Type (Etype (Subp))
17312 and then not Is_Access_Type (Etype (Subp))
17313 and then not Is_Constrained (Etype (Subp));
17314 end Returns_Unconstrained_Type;
17316 ----------------------------
17317 -- Root_Type_Of_Full_View --
17318 ----------------------------
17320 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
17321 Rtyp : constant Entity_Id := Root_Type (T);
17323 begin
17324 -- The root type of the full view may itself be a private type. Keep
17325 -- looking for the ultimate derivation parent.
17327 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
17328 return Root_Type_Of_Full_View (Full_View (Rtyp));
17329 else
17330 return Rtyp;
17331 end if;
17332 end Root_Type_Of_Full_View;
17334 ---------------------------
17335 -- Safe_To_Capture_Value --
17336 ---------------------------
17338 function Safe_To_Capture_Value
17339 (N : Node_Id;
17340 Ent : Entity_Id;
17341 Cond : Boolean := False) return Boolean
17343 begin
17344 -- The only entities for which we track constant values are variables
17345 -- which are not renamings, constants, out parameters, and in out
17346 -- parameters, so check if we have this case.
17348 -- Note: it may seem odd to track constant values for constants, but in
17349 -- fact this routine is used for other purposes than simply capturing
17350 -- the value. In particular, the setting of Known[_Non]_Null.
17352 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
17353 or else
17354 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
17355 then
17356 null;
17358 -- For conditionals, we also allow loop parameters and all formals,
17359 -- including in parameters.
17361 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
17362 null;
17364 -- For all other cases, not just unsafe, but impossible to capture
17365 -- Current_Value, since the above are the only entities which have
17366 -- Current_Value fields.
17368 else
17369 return False;
17370 end if;
17372 -- Skip if volatile or aliased, since funny things might be going on in
17373 -- these cases which we cannot necessarily track. Also skip any variable
17374 -- for which an address clause is given, or whose address is taken. Also
17375 -- never capture value of library level variables (an attempt to do so
17376 -- can occur in the case of package elaboration code).
17378 if Treat_As_Volatile (Ent)
17379 or else Is_Aliased (Ent)
17380 or else Present (Address_Clause (Ent))
17381 or else Address_Taken (Ent)
17382 or else (Is_Library_Level_Entity (Ent)
17383 and then Ekind (Ent) = E_Variable)
17384 then
17385 return False;
17386 end if;
17388 -- OK, all above conditions are met. We also require that the scope of
17389 -- the reference be the same as the scope of the entity, not counting
17390 -- packages and blocks and loops.
17392 declare
17393 E_Scope : constant Entity_Id := Scope (Ent);
17394 R_Scope : Entity_Id;
17396 begin
17397 R_Scope := Current_Scope;
17398 while R_Scope /= Standard_Standard loop
17399 exit when R_Scope = E_Scope;
17401 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
17402 return False;
17403 else
17404 R_Scope := Scope (R_Scope);
17405 end if;
17406 end loop;
17407 end;
17409 -- We also require that the reference does not appear in a context
17410 -- where it is not sure to be executed (i.e. a conditional context
17411 -- or an exception handler). We skip this if Cond is True, since the
17412 -- capturing of values from conditional tests handles this ok.
17414 if Cond then
17415 return True;
17416 end if;
17418 declare
17419 Desc : Node_Id;
17420 P : Node_Id;
17422 begin
17423 Desc := N;
17425 -- Seems dubious that case expressions are not handled here ???
17427 P := Parent (N);
17428 while Present (P) loop
17429 if Nkind (P) = N_If_Statement
17430 or else Nkind (P) = N_Case_Statement
17431 or else (Nkind (P) in N_Short_Circuit
17432 and then Desc = Right_Opnd (P))
17433 or else (Nkind (P) = N_If_Expression
17434 and then Desc /= First (Expressions (P)))
17435 or else Nkind (P) = N_Exception_Handler
17436 or else Nkind (P) = N_Selective_Accept
17437 or else Nkind (P) = N_Conditional_Entry_Call
17438 or else Nkind (P) = N_Timed_Entry_Call
17439 or else Nkind (P) = N_Asynchronous_Select
17440 then
17441 return False;
17443 else
17444 Desc := P;
17445 P := Parent (P);
17447 -- A special Ada 2012 case: the original node may be part
17448 -- of the else_actions of a conditional expression, in which
17449 -- case it might not have been expanded yet, and appears in
17450 -- a non-syntactic list of actions. In that case it is clearly
17451 -- not safe to save a value.
17453 if No (P)
17454 and then Is_List_Member (Desc)
17455 and then No (Parent (List_Containing (Desc)))
17456 then
17457 return False;
17458 end if;
17459 end if;
17460 end loop;
17461 end;
17463 -- OK, looks safe to set value
17465 return True;
17466 end Safe_To_Capture_Value;
17468 ---------------
17469 -- Same_Name --
17470 ---------------
17472 function Same_Name (N1, N2 : Node_Id) return Boolean is
17473 K1 : constant Node_Kind := Nkind (N1);
17474 K2 : constant Node_Kind := Nkind (N2);
17476 begin
17477 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
17478 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
17479 then
17480 return Chars (N1) = Chars (N2);
17482 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
17483 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
17484 then
17485 return Same_Name (Selector_Name (N1), Selector_Name (N2))
17486 and then Same_Name (Prefix (N1), Prefix (N2));
17488 else
17489 return False;
17490 end if;
17491 end Same_Name;
17493 -----------------
17494 -- Same_Object --
17495 -----------------
17497 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
17498 N1 : constant Node_Id := Original_Node (Node1);
17499 N2 : constant Node_Id := Original_Node (Node2);
17500 -- We do the tests on original nodes, since we are most interested
17501 -- in the original source, not any expansion that got in the way.
17503 K1 : constant Node_Kind := Nkind (N1);
17504 K2 : constant Node_Kind := Nkind (N2);
17506 begin
17507 -- First case, both are entities with same entity
17509 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
17510 declare
17511 EN1 : constant Entity_Id := Entity (N1);
17512 EN2 : constant Entity_Id := Entity (N2);
17513 begin
17514 if Present (EN1) and then Present (EN2)
17515 and then (Ekind_In (EN1, E_Variable, E_Constant)
17516 or else Is_Formal (EN1))
17517 and then EN1 = EN2
17518 then
17519 return True;
17520 end if;
17521 end;
17522 end if;
17524 -- Second case, selected component with same selector, same record
17526 if K1 = N_Selected_Component
17527 and then K2 = N_Selected_Component
17528 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
17529 then
17530 return Same_Object (Prefix (N1), Prefix (N2));
17532 -- Third case, indexed component with same subscripts, same array
17534 elsif K1 = N_Indexed_Component
17535 and then K2 = N_Indexed_Component
17536 and then Same_Object (Prefix (N1), Prefix (N2))
17537 then
17538 declare
17539 E1, E2 : Node_Id;
17540 begin
17541 E1 := First (Expressions (N1));
17542 E2 := First (Expressions (N2));
17543 while Present (E1) loop
17544 if not Same_Value (E1, E2) then
17545 return False;
17546 else
17547 Next (E1);
17548 Next (E2);
17549 end if;
17550 end loop;
17552 return True;
17553 end;
17555 -- Fourth case, slice of same array with same bounds
17557 elsif K1 = N_Slice
17558 and then K2 = N_Slice
17559 and then Nkind (Discrete_Range (N1)) = N_Range
17560 and then Nkind (Discrete_Range (N2)) = N_Range
17561 and then Same_Value (Low_Bound (Discrete_Range (N1)),
17562 Low_Bound (Discrete_Range (N2)))
17563 and then Same_Value (High_Bound (Discrete_Range (N1)),
17564 High_Bound (Discrete_Range (N2)))
17565 then
17566 return Same_Name (Prefix (N1), Prefix (N2));
17568 -- All other cases, not clearly the same object
17570 else
17571 return False;
17572 end if;
17573 end Same_Object;
17575 ---------------
17576 -- Same_Type --
17577 ---------------
17579 function Same_Type (T1, T2 : Entity_Id) return Boolean is
17580 begin
17581 if T1 = T2 then
17582 return True;
17584 elsif not Is_Constrained (T1)
17585 and then not Is_Constrained (T2)
17586 and then Base_Type (T1) = Base_Type (T2)
17587 then
17588 return True;
17590 -- For now don't bother with case of identical constraints, to be
17591 -- fiddled with later on perhaps (this is only used for optimization
17592 -- purposes, so it is not critical to do a best possible job)
17594 else
17595 return False;
17596 end if;
17597 end Same_Type;
17599 ----------------
17600 -- Same_Value --
17601 ----------------
17603 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
17604 begin
17605 if Compile_Time_Known_Value (Node1)
17606 and then Compile_Time_Known_Value (Node2)
17607 and then Expr_Value (Node1) = Expr_Value (Node2)
17608 then
17609 return True;
17610 elsif Same_Object (Node1, Node2) then
17611 return True;
17612 else
17613 return False;
17614 end if;
17615 end Same_Value;
17617 -----------------------------
17618 -- Save_SPARK_Mode_And_Set --
17619 -----------------------------
17621 procedure Save_SPARK_Mode_And_Set
17622 (Context : Entity_Id;
17623 Mode : out SPARK_Mode_Type)
17625 begin
17626 -- Save the current mode in effect
17628 Mode := SPARK_Mode;
17630 -- Do not consider illegal or partially decorated constructs
17632 if Ekind (Context) = E_Void or else Error_Posted (Context) then
17633 null;
17635 elsif Present (SPARK_Pragma (Context)) then
17636 SPARK_Mode := Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Context));
17637 end if;
17638 end Save_SPARK_Mode_And_Set;
17640 -------------------------
17641 -- Scalar_Part_Present --
17642 -------------------------
17644 function Scalar_Part_Present (T : Entity_Id) return Boolean is
17645 C : Entity_Id;
17647 begin
17648 if Is_Scalar_Type (T) then
17649 return True;
17651 elsif Is_Array_Type (T) then
17652 return Scalar_Part_Present (Component_Type (T));
17654 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
17655 C := First_Component_Or_Discriminant (T);
17656 while Present (C) loop
17657 if Scalar_Part_Present (Etype (C)) then
17658 return True;
17659 else
17660 Next_Component_Or_Discriminant (C);
17661 end if;
17662 end loop;
17663 end if;
17665 return False;
17666 end Scalar_Part_Present;
17668 ------------------------
17669 -- Scope_Is_Transient --
17670 ------------------------
17672 function Scope_Is_Transient return Boolean is
17673 begin
17674 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
17675 end Scope_Is_Transient;
17677 ------------------
17678 -- Scope_Within --
17679 ------------------
17681 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
17682 Scop : Entity_Id;
17684 begin
17685 Scop := Scope1;
17686 while Scop /= Standard_Standard loop
17687 Scop := Scope (Scop);
17689 if Scop = Scope2 then
17690 return True;
17691 end if;
17692 end loop;
17694 return False;
17695 end Scope_Within;
17697 --------------------------
17698 -- Scope_Within_Or_Same --
17699 --------------------------
17701 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
17702 Scop : Entity_Id;
17704 begin
17705 Scop := Scope1;
17706 while Scop /= Standard_Standard loop
17707 if Scop = Scope2 then
17708 return True;
17709 else
17710 Scop := Scope (Scop);
17711 end if;
17712 end loop;
17714 return False;
17715 end Scope_Within_Or_Same;
17717 --------------------
17718 -- Set_Convention --
17719 --------------------
17721 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
17722 begin
17723 Basic_Set_Convention (E, Val);
17725 if Is_Type (E)
17726 and then Is_Access_Subprogram_Type (Base_Type (E))
17727 and then Has_Foreign_Convention (E)
17728 then
17730 -- A pragma Convention in an instance may apply to the subtype
17731 -- created for a formal, in which case we have already verified
17732 -- that conventions of actual and formal match and there is nothing
17733 -- to flag on the subtype.
17735 if In_Instance then
17736 null;
17737 else
17738 Set_Can_Use_Internal_Rep (E, False);
17739 end if;
17740 end if;
17742 -- If E is an object or component, and the type of E is an anonymous
17743 -- access type with no convention set, then also set the convention of
17744 -- the anonymous access type. We do not do this for anonymous protected
17745 -- types, since protected types always have the default convention.
17747 if Present (Etype (E))
17748 and then (Is_Object (E)
17749 or else Ekind (E) = E_Component
17751 -- Allow E_Void (happens for pragma Convention appearing
17752 -- in the middle of a record applying to a component)
17754 or else Ekind (E) = E_Void)
17755 then
17756 declare
17757 Typ : constant Entity_Id := Etype (E);
17759 begin
17760 if Ekind_In (Typ, E_Anonymous_Access_Type,
17761 E_Anonymous_Access_Subprogram_Type)
17762 and then not Has_Convention_Pragma (Typ)
17763 then
17764 Basic_Set_Convention (Typ, Val);
17765 Set_Has_Convention_Pragma (Typ);
17767 -- And for the access subprogram type, deal similarly with the
17768 -- designated E_Subprogram_Type if it is also internal (which
17769 -- it always is?)
17771 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
17772 declare
17773 Dtype : constant Entity_Id := Designated_Type (Typ);
17774 begin
17775 if Ekind (Dtype) = E_Subprogram_Type
17776 and then Is_Itype (Dtype)
17777 and then not Has_Convention_Pragma (Dtype)
17778 then
17779 Basic_Set_Convention (Dtype, Val);
17780 Set_Has_Convention_Pragma (Dtype);
17781 end if;
17782 end;
17783 end if;
17784 end if;
17785 end;
17786 end if;
17787 end Set_Convention;
17789 ------------------------
17790 -- Set_Current_Entity --
17791 ------------------------
17793 -- The given entity is to be set as the currently visible definition of its
17794 -- associated name (i.e. the Node_Id associated with its name). All we have
17795 -- to do is to get the name from the identifier, and then set the
17796 -- associated Node_Id to point to the given entity.
17798 procedure Set_Current_Entity (E : Entity_Id) is
17799 begin
17800 Set_Name_Entity_Id (Chars (E), E);
17801 end Set_Current_Entity;
17803 ---------------------------
17804 -- Set_Debug_Info_Needed --
17805 ---------------------------
17807 procedure Set_Debug_Info_Needed (T : Entity_Id) is
17809 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
17810 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
17811 -- Used to set debug info in a related node if not set already
17813 --------------------------------------
17814 -- Set_Debug_Info_Needed_If_Not_Set --
17815 --------------------------------------
17817 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
17818 begin
17819 if Present (E) and then not Needs_Debug_Info (E) then
17820 Set_Debug_Info_Needed (E);
17822 -- For a private type, indicate that the full view also needs
17823 -- debug information.
17825 if Is_Type (E)
17826 and then Is_Private_Type (E)
17827 and then Present (Full_View (E))
17828 then
17829 Set_Debug_Info_Needed (Full_View (E));
17830 end if;
17831 end if;
17832 end Set_Debug_Info_Needed_If_Not_Set;
17834 -- Start of processing for Set_Debug_Info_Needed
17836 begin
17837 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
17838 -- indicates that Debug_Info_Needed is never required for the entity.
17839 -- Nothing to do if entity comes from a predefined file. Library files
17840 -- are compiled without debug information, but inlined bodies of these
17841 -- routines may appear in user code, and debug information on them ends
17842 -- up complicating debugging the user code.
17844 if No (T)
17845 or else Debug_Info_Off (T)
17846 then
17847 return;
17849 elsif In_Inlined_Body
17850 and then Is_Predefined_File_Name
17851 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
17852 then
17853 Set_Needs_Debug_Info (T, False);
17854 end if;
17856 -- Set flag in entity itself. Note that we will go through the following
17857 -- circuitry even if the flag is already set on T. That's intentional,
17858 -- it makes sure that the flag will be set in subsidiary entities.
17860 Set_Needs_Debug_Info (T);
17862 -- Set flag on subsidiary entities if not set already
17864 if Is_Object (T) then
17865 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
17867 elsif Is_Type (T) then
17868 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
17870 if Is_Record_Type (T) then
17871 declare
17872 Ent : Entity_Id := First_Entity (T);
17873 begin
17874 while Present (Ent) loop
17875 Set_Debug_Info_Needed_If_Not_Set (Ent);
17876 Next_Entity (Ent);
17877 end loop;
17878 end;
17880 -- For a class wide subtype, we also need debug information
17881 -- for the equivalent type.
17883 if Ekind (T) = E_Class_Wide_Subtype then
17884 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
17885 end if;
17887 elsif Is_Array_Type (T) then
17888 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
17890 declare
17891 Indx : Node_Id := First_Index (T);
17892 begin
17893 while Present (Indx) loop
17894 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
17895 Indx := Next_Index (Indx);
17896 end loop;
17897 end;
17899 -- For a packed array type, we also need debug information for
17900 -- the type used to represent the packed array. Conversely, we
17901 -- also need it for the former if we need it for the latter.
17903 if Is_Packed (T) then
17904 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
17905 end if;
17907 if Is_Packed_Array_Impl_Type (T) then
17908 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
17909 end if;
17911 elsif Is_Access_Type (T) then
17912 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
17914 elsif Is_Private_Type (T) then
17915 declare
17916 FV : constant Entity_Id := Full_View (T);
17918 begin
17919 Set_Debug_Info_Needed_If_Not_Set (FV);
17921 -- If the full view is itself a derived private type, we need
17922 -- debug information on its underlying type.
17924 if Present (FV)
17925 and then Is_Private_Type (FV)
17926 and then Present (Underlying_Full_View (FV))
17927 then
17928 Set_Needs_Debug_Info (Underlying_Full_View (FV));
17929 end if;
17930 end;
17932 elsif Is_Protected_Type (T) then
17933 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
17935 elsif Is_Scalar_Type (T) then
17937 -- If the subrange bounds are materialized by dedicated constant
17938 -- objects, also include them in the debug info to make sure the
17939 -- debugger can properly use them.
17941 if Present (Scalar_Range (T))
17942 and then Nkind (Scalar_Range (T)) = N_Range
17943 then
17944 declare
17945 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
17946 High_Bnd : constant Node_Id := Type_High_Bound (T);
17948 begin
17949 if Is_Entity_Name (Low_Bnd) then
17950 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
17951 end if;
17953 if Is_Entity_Name (High_Bnd) then
17954 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
17955 end if;
17956 end;
17957 end if;
17958 end if;
17959 end if;
17960 end Set_Debug_Info_Needed;
17962 ----------------------------
17963 -- Set_Entity_With_Checks --
17964 ----------------------------
17966 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
17967 Val_Actual : Entity_Id;
17968 Nod : Node_Id;
17969 Post_Node : Node_Id;
17971 begin
17972 -- Unconditionally set the entity
17974 Set_Entity (N, Val);
17976 -- The node to post on is the selector in the case of an expanded name,
17977 -- and otherwise the node itself.
17979 if Nkind (N) = N_Expanded_Name then
17980 Post_Node := Selector_Name (N);
17981 else
17982 Post_Node := N;
17983 end if;
17985 -- Check for violation of No_Fixed_IO
17987 if Restriction_Check_Required (No_Fixed_IO)
17988 and then
17989 ((RTU_Loaded (Ada_Text_IO)
17990 and then (Is_RTE (Val, RE_Decimal_IO)
17991 or else
17992 Is_RTE (Val, RE_Fixed_IO)))
17994 or else
17995 (RTU_Loaded (Ada_Wide_Text_IO)
17996 and then (Is_RTE (Val, RO_WT_Decimal_IO)
17997 or else
17998 Is_RTE (Val, RO_WT_Fixed_IO)))
18000 or else
18001 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
18002 and then (Is_RTE (Val, RO_WW_Decimal_IO)
18003 or else
18004 Is_RTE (Val, RO_WW_Fixed_IO))))
18006 -- A special extra check, don't complain about a reference from within
18007 -- the Ada.Interrupts package itself!
18009 and then not In_Same_Extended_Unit (N, Val)
18010 then
18011 Check_Restriction (No_Fixed_IO, Post_Node);
18012 end if;
18014 -- Remaining checks are only done on source nodes. Note that we test
18015 -- for violation of No_Fixed_IO even on non-source nodes, because the
18016 -- cases for checking violations of this restriction are instantiations
18017 -- where the reference in the instance has Comes_From_Source False.
18019 if not Comes_From_Source (N) then
18020 return;
18021 end if;
18023 -- Check for violation of No_Abort_Statements, which is triggered by
18024 -- call to Ada.Task_Identification.Abort_Task.
18026 if Restriction_Check_Required (No_Abort_Statements)
18027 and then (Is_RTE (Val, RE_Abort_Task))
18029 -- A special extra check, don't complain about a reference from within
18030 -- the Ada.Task_Identification package itself!
18032 and then not In_Same_Extended_Unit (N, Val)
18033 then
18034 Check_Restriction (No_Abort_Statements, Post_Node);
18035 end if;
18037 if Val = Standard_Long_Long_Integer then
18038 Check_Restriction (No_Long_Long_Integers, Post_Node);
18039 end if;
18041 -- Check for violation of No_Dynamic_Attachment
18043 if Restriction_Check_Required (No_Dynamic_Attachment)
18044 and then RTU_Loaded (Ada_Interrupts)
18045 and then (Is_RTE (Val, RE_Is_Reserved) or else
18046 Is_RTE (Val, RE_Is_Attached) or else
18047 Is_RTE (Val, RE_Current_Handler) or else
18048 Is_RTE (Val, RE_Attach_Handler) or else
18049 Is_RTE (Val, RE_Exchange_Handler) or else
18050 Is_RTE (Val, RE_Detach_Handler) or else
18051 Is_RTE (Val, RE_Reference))
18053 -- A special extra check, don't complain about a reference from within
18054 -- the Ada.Interrupts package itself!
18056 and then not In_Same_Extended_Unit (N, Val)
18057 then
18058 Check_Restriction (No_Dynamic_Attachment, Post_Node);
18059 end if;
18061 -- Check for No_Implementation_Identifiers
18063 if Restriction_Check_Required (No_Implementation_Identifiers) then
18065 -- We have an implementation defined entity if it is marked as
18066 -- implementation defined, or is defined in a package marked as
18067 -- implementation defined. However, library packages themselves
18068 -- are excluded (we don't want to flag Interfaces itself, just
18069 -- the entities within it).
18071 if (Is_Implementation_Defined (Val)
18072 or else
18073 (Present (Scope (Val))
18074 and then Is_Implementation_Defined (Scope (Val))))
18075 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
18076 and then Is_Library_Level_Entity (Val))
18077 then
18078 Check_Restriction (No_Implementation_Identifiers, Post_Node);
18079 end if;
18080 end if;
18082 -- Do the style check
18084 if Style_Check
18085 and then not Suppress_Style_Checks (Val)
18086 and then not In_Instance
18087 then
18088 if Nkind (N) = N_Identifier then
18089 Nod := N;
18090 elsif Nkind (N) = N_Expanded_Name then
18091 Nod := Selector_Name (N);
18092 else
18093 return;
18094 end if;
18096 -- A special situation arises for derived operations, where we want
18097 -- to do the check against the parent (since the Sloc of the derived
18098 -- operation points to the derived type declaration itself).
18100 Val_Actual := Val;
18101 while not Comes_From_Source (Val_Actual)
18102 and then Nkind (Val_Actual) in N_Entity
18103 and then (Ekind (Val_Actual) = E_Enumeration_Literal
18104 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
18105 and then Present (Alias (Val_Actual))
18106 loop
18107 Val_Actual := Alias (Val_Actual);
18108 end loop;
18110 -- Renaming declarations for generic actuals do not come from source,
18111 -- and have a different name from that of the entity they rename, so
18112 -- there is no style check to perform here.
18114 if Chars (Nod) = Chars (Val_Actual) then
18115 Style.Check_Identifier (Nod, Val_Actual);
18116 end if;
18117 end if;
18119 Set_Entity (N, Val);
18120 end Set_Entity_With_Checks;
18122 ------------------------
18123 -- Set_Name_Entity_Id --
18124 ------------------------
18126 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
18127 begin
18128 Set_Name_Table_Int (Id, Int (Val));
18129 end Set_Name_Entity_Id;
18131 ---------------------
18132 -- Set_Next_Actual --
18133 ---------------------
18135 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
18136 begin
18137 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
18138 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
18139 end if;
18140 end Set_Next_Actual;
18142 ----------------------------------
18143 -- Set_Optimize_Alignment_Flags --
18144 ----------------------------------
18146 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
18147 begin
18148 if Optimize_Alignment = 'S' then
18149 Set_Optimize_Alignment_Space (E);
18150 elsif Optimize_Alignment = 'T' then
18151 Set_Optimize_Alignment_Time (E);
18152 end if;
18153 end Set_Optimize_Alignment_Flags;
18155 -----------------------
18156 -- Set_Public_Status --
18157 -----------------------
18159 procedure Set_Public_Status (Id : Entity_Id) is
18160 S : constant Entity_Id := Current_Scope;
18162 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
18163 -- Determines if E is defined within handled statement sequence or
18164 -- an if statement, returns True if so, False otherwise.
18166 ----------------------
18167 -- Within_HSS_Or_If --
18168 ----------------------
18170 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
18171 N : Node_Id;
18172 begin
18173 N := Declaration_Node (E);
18174 loop
18175 N := Parent (N);
18177 if No (N) then
18178 return False;
18180 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
18181 N_If_Statement)
18182 then
18183 return True;
18184 end if;
18185 end loop;
18186 end Within_HSS_Or_If;
18188 -- Start of processing for Set_Public_Status
18190 begin
18191 -- Everything in the scope of Standard is public
18193 if S = Standard_Standard then
18194 Set_Is_Public (Id);
18196 -- Entity is definitely not public if enclosing scope is not public
18198 elsif not Is_Public (S) then
18199 return;
18201 -- An object or function declaration that occurs in a handled sequence
18202 -- of statements or within an if statement is the declaration for a
18203 -- temporary object or local subprogram generated by the expander. It
18204 -- never needs to be made public and furthermore, making it public can
18205 -- cause back end problems.
18207 elsif Nkind_In (Parent (Id), N_Object_Declaration,
18208 N_Function_Specification)
18209 and then Within_HSS_Or_If (Id)
18210 then
18211 return;
18213 -- Entities in public packages or records are public
18215 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
18216 Set_Is_Public (Id);
18218 -- The bounds of an entry family declaration can generate object
18219 -- declarations that are visible to the back-end, e.g. in the
18220 -- the declaration of a composite type that contains tasks.
18222 elsif Is_Concurrent_Type (S)
18223 and then not Has_Completion (S)
18224 and then Nkind (Parent (Id)) = N_Object_Declaration
18225 then
18226 Set_Is_Public (Id);
18227 end if;
18228 end Set_Public_Status;
18230 -----------------------------
18231 -- Set_Referenced_Modified --
18232 -----------------------------
18234 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
18235 Pref : Node_Id;
18237 begin
18238 -- Deal with indexed or selected component where prefix is modified
18240 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
18241 Pref := Prefix (N);
18243 -- If prefix is access type, then it is the designated object that is
18244 -- being modified, which means we have no entity to set the flag on.
18246 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
18247 return;
18249 -- Otherwise chase the prefix
18251 else
18252 Set_Referenced_Modified (Pref, Out_Param);
18253 end if;
18255 -- Otherwise see if we have an entity name (only other case to process)
18257 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18258 Set_Referenced_As_LHS (Entity (N), not Out_Param);
18259 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
18260 end if;
18261 end Set_Referenced_Modified;
18263 ----------------------------
18264 -- Set_Scope_Is_Transient --
18265 ----------------------------
18267 procedure Set_Scope_Is_Transient (V : Boolean := True) is
18268 begin
18269 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
18270 end Set_Scope_Is_Transient;
18272 -------------------
18273 -- Set_Size_Info --
18274 -------------------
18276 procedure Set_Size_Info (T1, T2 : Entity_Id) is
18277 begin
18278 -- We copy Esize, but not RM_Size, since in general RM_Size is
18279 -- subtype specific and does not get inherited by all subtypes.
18281 Set_Esize (T1, Esize (T2));
18282 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
18284 if Is_Discrete_Or_Fixed_Point_Type (T1)
18285 and then
18286 Is_Discrete_Or_Fixed_Point_Type (T2)
18287 then
18288 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
18289 end if;
18291 Set_Alignment (T1, Alignment (T2));
18292 end Set_Size_Info;
18294 --------------------
18295 -- Static_Boolean --
18296 --------------------
18298 function Static_Boolean (N : Node_Id) return Uint is
18299 begin
18300 Analyze_And_Resolve (N, Standard_Boolean);
18302 if N = Error
18303 or else Error_Posted (N)
18304 or else Etype (N) = Any_Type
18305 then
18306 return No_Uint;
18307 end if;
18309 if Is_OK_Static_Expression (N) then
18310 if not Raises_Constraint_Error (N) then
18311 return Expr_Value (N);
18312 else
18313 return No_Uint;
18314 end if;
18316 elsif Etype (N) = Any_Type then
18317 return No_Uint;
18319 else
18320 Flag_Non_Static_Expr
18321 ("static boolean expression required here", N);
18322 return No_Uint;
18323 end if;
18324 end Static_Boolean;
18326 --------------------
18327 -- Static_Integer --
18328 --------------------
18330 function Static_Integer (N : Node_Id) return Uint is
18331 begin
18332 Analyze_And_Resolve (N, Any_Integer);
18334 if N = Error
18335 or else Error_Posted (N)
18336 or else Etype (N) = Any_Type
18337 then
18338 return No_Uint;
18339 end if;
18341 if Is_OK_Static_Expression (N) then
18342 if not Raises_Constraint_Error (N) then
18343 return Expr_Value (N);
18344 else
18345 return No_Uint;
18346 end if;
18348 elsif Etype (N) = Any_Type then
18349 return No_Uint;
18351 else
18352 Flag_Non_Static_Expr
18353 ("static integer expression required here", N);
18354 return No_Uint;
18355 end if;
18356 end Static_Integer;
18358 --------------------------
18359 -- Statically_Different --
18360 --------------------------
18362 function Statically_Different (E1, E2 : Node_Id) return Boolean is
18363 R1 : constant Node_Id := Get_Referenced_Object (E1);
18364 R2 : constant Node_Id := Get_Referenced_Object (E2);
18365 begin
18366 return Is_Entity_Name (R1)
18367 and then Is_Entity_Name (R2)
18368 and then Entity (R1) /= Entity (R2)
18369 and then not Is_Formal (Entity (R1))
18370 and then not Is_Formal (Entity (R2));
18371 end Statically_Different;
18373 --------------------------------------
18374 -- Subject_To_Loop_Entry_Attributes --
18375 --------------------------------------
18377 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
18378 Stmt : Node_Id;
18380 begin
18381 Stmt := N;
18383 -- The expansion mechanism transform a loop subject to at least one
18384 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
18385 -- the conditional part.
18387 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
18388 and then Nkind (Original_Node (N)) = N_Loop_Statement
18389 then
18390 Stmt := Original_Node (N);
18391 end if;
18393 return
18394 Nkind (Stmt) = N_Loop_Statement
18395 and then Present (Identifier (Stmt))
18396 and then Present (Entity (Identifier (Stmt)))
18397 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
18398 end Subject_To_Loop_Entry_Attributes;
18400 -----------------------------
18401 -- Subprogram_Access_Level --
18402 -----------------------------
18404 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
18405 begin
18406 if Present (Alias (Subp)) then
18407 return Subprogram_Access_Level (Alias (Subp));
18408 else
18409 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
18410 end if;
18411 end Subprogram_Access_Level;
18413 -------------------------------
18414 -- Support_Atomic_Primitives --
18415 -------------------------------
18417 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
18418 Size : Int;
18420 begin
18421 -- Verify the alignment of Typ is known
18423 if not Known_Alignment (Typ) then
18424 return False;
18425 end if;
18427 if Known_Static_Esize (Typ) then
18428 Size := UI_To_Int (Esize (Typ));
18430 -- If the Esize (Object_Size) is unknown at compile time, look at the
18431 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
18433 elsif Known_Static_RM_Size (Typ) then
18434 Size := UI_To_Int (RM_Size (Typ));
18436 -- Otherwise, the size is considered to be unknown.
18438 else
18439 return False;
18440 end if;
18442 -- Check that the size of the component is 8, 16, 32 or 64 bits and that
18443 -- Typ is properly aligned.
18445 case Size is
18446 when 8 | 16 | 32 | 64 =>
18447 return Size = UI_To_Int (Alignment (Typ)) * 8;
18448 when others =>
18449 return False;
18450 end case;
18451 end Support_Atomic_Primitives;
18453 -----------------
18454 -- Trace_Scope --
18455 -----------------
18457 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
18458 begin
18459 if Debug_Flag_W then
18460 for J in 0 .. Scope_Stack.Last loop
18461 Write_Str (" ");
18462 end loop;
18464 Write_Str (Msg);
18465 Write_Name (Chars (E));
18466 Write_Str (" from ");
18467 Write_Location (Sloc (N));
18468 Write_Eol;
18469 end if;
18470 end Trace_Scope;
18472 -----------------------
18473 -- Transfer_Entities --
18474 -----------------------
18476 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
18477 procedure Set_Public_Status_Of (Id : Entity_Id);
18478 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
18479 -- Set_Public_Status. If successfull and Id denotes a record type, set
18480 -- the Is_Public attribute of its fields.
18482 --------------------------
18483 -- Set_Public_Status_Of --
18484 --------------------------
18486 procedure Set_Public_Status_Of (Id : Entity_Id) is
18487 Field : Entity_Id;
18489 begin
18490 if not Is_Public (Id) then
18491 Set_Public_Status (Id);
18493 -- When the input entity is a public record type, ensure that all
18494 -- its internal fields are also exposed to the linker. The fields
18495 -- of a class-wide type are never made public.
18497 if Is_Public (Id)
18498 and then Is_Record_Type (Id)
18499 and then not Is_Class_Wide_Type (Id)
18500 then
18501 Field := First_Entity (Id);
18502 while Present (Field) loop
18503 Set_Is_Public (Field);
18504 Next_Entity (Field);
18505 end loop;
18506 end if;
18507 end if;
18508 end Set_Public_Status_Of;
18510 -- Local variables
18512 Full_Id : Entity_Id;
18513 Id : Entity_Id;
18515 -- Start of processing for Transfer_Entities
18517 begin
18518 Id := First_Entity (From);
18520 if Present (Id) then
18522 -- Merge the entity chain of the source scope with that of the
18523 -- destination scope.
18525 if Present (Last_Entity (To)) then
18526 Set_Next_Entity (Last_Entity (To), Id);
18527 else
18528 Set_First_Entity (To, Id);
18529 end if;
18531 Set_Last_Entity (To, Last_Entity (From));
18533 -- Inspect the entities of the source scope and update their Scope
18534 -- attribute.
18536 while Present (Id) loop
18537 Set_Scope (Id, To);
18538 Set_Public_Status_Of (Id);
18540 -- Handle an internally generated full view for a private type
18542 if Is_Private_Type (Id)
18543 and then Present (Full_View (Id))
18544 and then Is_Itype (Full_View (Id))
18545 then
18546 Full_Id := Full_View (Id);
18548 Set_Scope (Full_Id, To);
18549 Set_Public_Status_Of (Full_Id);
18550 end if;
18552 Next_Entity (Id);
18553 end loop;
18555 Set_First_Entity (From, Empty);
18556 Set_Last_Entity (From, Empty);
18557 end if;
18558 end Transfer_Entities;
18560 -----------------------
18561 -- Type_Access_Level --
18562 -----------------------
18564 function Type_Access_Level (Typ : Entity_Id) return Uint is
18565 Btyp : Entity_Id;
18567 begin
18568 Btyp := Base_Type (Typ);
18570 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
18571 -- simply use the level where the type is declared. This is true for
18572 -- stand-alone object declarations, and for anonymous access types
18573 -- associated with components the level is the same as that of the
18574 -- enclosing composite type. However, special treatment is needed for
18575 -- the cases of access parameters, return objects of an anonymous access
18576 -- type, and, in Ada 95, access discriminants of limited types.
18578 if Is_Access_Type (Btyp) then
18579 if Ekind (Btyp) = E_Anonymous_Access_Type then
18581 -- If the type is a nonlocal anonymous access type (such as for
18582 -- an access parameter) we treat it as being declared at the
18583 -- library level to ensure that names such as X.all'access don't
18584 -- fail static accessibility checks.
18586 if not Is_Local_Anonymous_Access (Typ) then
18587 return Scope_Depth (Standard_Standard);
18589 -- If this is a return object, the accessibility level is that of
18590 -- the result subtype of the enclosing function. The test here is
18591 -- little complicated, because we have to account for extended
18592 -- return statements that have been rewritten as blocks, in which
18593 -- case we have to find and the Is_Return_Object attribute of the
18594 -- itype's associated object. It would be nice to find a way to
18595 -- simplify this test, but it doesn't seem worthwhile to add a new
18596 -- flag just for purposes of this test. ???
18598 elsif Ekind (Scope (Btyp)) = E_Return_Statement
18599 or else
18600 (Is_Itype (Btyp)
18601 and then Nkind (Associated_Node_For_Itype (Btyp)) =
18602 N_Object_Declaration
18603 and then Is_Return_Object
18604 (Defining_Identifier
18605 (Associated_Node_For_Itype (Btyp))))
18606 then
18607 declare
18608 Scop : Entity_Id;
18610 begin
18611 Scop := Scope (Scope (Btyp));
18612 while Present (Scop) loop
18613 exit when Ekind (Scop) = E_Function;
18614 Scop := Scope (Scop);
18615 end loop;
18617 -- Treat the return object's type as having the level of the
18618 -- function's result subtype (as per RM05-6.5(5.3/2)).
18620 return Type_Access_Level (Etype (Scop));
18621 end;
18622 end if;
18623 end if;
18625 Btyp := Root_Type (Btyp);
18627 -- The accessibility level of anonymous access types associated with
18628 -- discriminants is that of the current instance of the type, and
18629 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
18631 -- AI-402: access discriminants have accessibility based on the
18632 -- object rather than the type in Ada 2005, so the above paragraph
18633 -- doesn't apply.
18635 -- ??? Needs completion with rules from AI-416
18637 if Ada_Version <= Ada_95
18638 and then Ekind (Typ) = E_Anonymous_Access_Type
18639 and then Present (Associated_Node_For_Itype (Typ))
18640 and then Nkind (Associated_Node_For_Itype (Typ)) =
18641 N_Discriminant_Specification
18642 then
18643 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
18644 end if;
18645 end if;
18647 -- Return library level for a generic formal type. This is done because
18648 -- RM(10.3.2) says that "The statically deeper relationship does not
18649 -- apply to ... a descendant of a generic formal type". Rather than
18650 -- checking at each point where a static accessibility check is
18651 -- performed to see if we are dealing with a formal type, this rule is
18652 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
18653 -- return extreme values for a formal type; Deepest_Type_Access_Level
18654 -- returns Int'Last. By calling the appropriate function from among the
18655 -- two, we ensure that the static accessibility check will pass if we
18656 -- happen to run into a formal type. More specifically, we should call
18657 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
18658 -- call occurs as part of a static accessibility check and the error
18659 -- case is the case where the type's level is too shallow (as opposed
18660 -- to too deep).
18662 if Is_Generic_Type (Root_Type (Btyp)) then
18663 return Scope_Depth (Standard_Standard);
18664 end if;
18666 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
18667 end Type_Access_Level;
18669 ------------------------------------
18670 -- Type_Without_Stream_Operation --
18671 ------------------------------------
18673 function Type_Without_Stream_Operation
18674 (T : Entity_Id;
18675 Op : TSS_Name_Type := TSS_Null) return Entity_Id
18677 BT : constant Entity_Id := Base_Type (T);
18678 Op_Missing : Boolean;
18680 begin
18681 if not Restriction_Active (No_Default_Stream_Attributes) then
18682 return Empty;
18683 end if;
18685 if Is_Elementary_Type (T) then
18686 if Op = TSS_Null then
18687 Op_Missing :=
18688 No (TSS (BT, TSS_Stream_Read))
18689 or else No (TSS (BT, TSS_Stream_Write));
18691 else
18692 Op_Missing := No (TSS (BT, Op));
18693 end if;
18695 if Op_Missing then
18696 return T;
18697 else
18698 return Empty;
18699 end if;
18701 elsif Is_Array_Type (T) then
18702 return Type_Without_Stream_Operation (Component_Type (T), Op);
18704 elsif Is_Record_Type (T) then
18705 declare
18706 Comp : Entity_Id;
18707 C_Typ : Entity_Id;
18709 begin
18710 Comp := First_Component (T);
18711 while Present (Comp) loop
18712 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
18714 if Present (C_Typ) then
18715 return C_Typ;
18716 end if;
18718 Next_Component (Comp);
18719 end loop;
18721 return Empty;
18722 end;
18724 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
18725 return Type_Without_Stream_Operation (Full_View (T), Op);
18726 else
18727 return Empty;
18728 end if;
18729 end Type_Without_Stream_Operation;
18731 ----------------------------
18732 -- Unique_Defining_Entity --
18733 ----------------------------
18735 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
18736 begin
18737 return Unique_Entity (Defining_Entity (N));
18738 end Unique_Defining_Entity;
18740 -------------------
18741 -- Unique_Entity --
18742 -------------------
18744 function Unique_Entity (E : Entity_Id) return Entity_Id is
18745 U : Entity_Id := E;
18746 P : Node_Id;
18748 begin
18749 case Ekind (E) is
18750 when E_Constant =>
18751 if Present (Full_View (E)) then
18752 U := Full_View (E);
18753 end if;
18755 when Type_Kind =>
18756 if Present (Full_View (E)) then
18757 U := Full_View (E);
18758 end if;
18760 when E_Package_Body =>
18761 P := Parent (E);
18763 if Nkind (P) = N_Defining_Program_Unit_Name then
18764 P := Parent (P);
18765 end if;
18767 U := Corresponding_Spec (P);
18769 when E_Subprogram_Body =>
18770 P := Parent (E);
18772 if Nkind (P) = N_Defining_Program_Unit_Name then
18773 P := Parent (P);
18774 end if;
18776 P := Parent (P);
18778 if Nkind (P) = N_Subprogram_Body_Stub then
18779 if Present (Library_Unit (P)) then
18781 -- Get to the function or procedure (generic) entity through
18782 -- the body entity.
18784 U :=
18785 Unique_Entity (Defining_Entity (Get_Body_From_Stub (P)));
18786 end if;
18787 else
18788 U := Corresponding_Spec (P);
18789 end if;
18791 when Formal_Kind =>
18792 if Present (Spec_Entity (E)) then
18793 U := Spec_Entity (E);
18794 end if;
18796 when others =>
18797 null;
18798 end case;
18800 return U;
18801 end Unique_Entity;
18803 -----------------
18804 -- Unique_Name --
18805 -----------------
18807 function Unique_Name (E : Entity_Id) return String is
18809 -- Names of E_Subprogram_Body or E_Package_Body entities are not
18810 -- reliable, as they may not include the overloading suffix. Instead,
18811 -- when looking for the name of E or one of its enclosing scope, we get
18812 -- the name of the corresponding Unique_Entity.
18814 function Get_Scoped_Name (E : Entity_Id) return String;
18815 -- Return the name of E prefixed by all the names of the scopes to which
18816 -- E belongs, except for Standard.
18818 ---------------------
18819 -- Get_Scoped_Name --
18820 ---------------------
18822 function Get_Scoped_Name (E : Entity_Id) return String is
18823 Name : constant String := Get_Name_String (Chars (E));
18824 begin
18825 if Has_Fully_Qualified_Name (E)
18826 or else Scope (E) = Standard_Standard
18827 then
18828 return Name;
18829 else
18830 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
18831 end if;
18832 end Get_Scoped_Name;
18834 -- Start of processing for Unique_Name
18836 begin
18837 if E = Standard_Standard then
18838 return Get_Name_String (Name_Standard);
18840 elsif Scope (E) = Standard_Standard
18841 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
18842 then
18843 return Get_Name_String (Name_Standard) & "__" &
18844 Get_Name_String (Chars (E));
18846 elsif Ekind (E) = E_Enumeration_Literal then
18847 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
18849 else
18850 return Get_Scoped_Name (Unique_Entity (E));
18851 end if;
18852 end Unique_Name;
18854 ---------------------
18855 -- Unit_Is_Visible --
18856 ---------------------
18858 function Unit_Is_Visible (U : Entity_Id) return Boolean is
18859 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
18860 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
18862 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
18863 -- For a child unit, check whether unit appears in a with_clause
18864 -- of a parent.
18866 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
18867 -- Scan the context clause of one compilation unit looking for a
18868 -- with_clause for the unit in question.
18870 ----------------------------
18871 -- Unit_In_Parent_Context --
18872 ----------------------------
18874 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
18875 begin
18876 if Unit_In_Context (Par_Unit) then
18877 return True;
18879 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
18880 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
18882 else
18883 return False;
18884 end if;
18885 end Unit_In_Parent_Context;
18887 ---------------------
18888 -- Unit_In_Context --
18889 ---------------------
18891 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
18892 Clause : Node_Id;
18894 begin
18895 Clause := First (Context_Items (Comp_Unit));
18896 while Present (Clause) loop
18897 if Nkind (Clause) = N_With_Clause then
18898 if Library_Unit (Clause) = U then
18899 return True;
18901 -- The with_clause may denote a renaming of the unit we are
18902 -- looking for, eg. Text_IO which renames Ada.Text_IO.
18904 elsif
18905 Renamed_Entity (Entity (Name (Clause))) =
18906 Defining_Entity (Unit (U))
18907 then
18908 return True;
18909 end if;
18910 end if;
18912 Next (Clause);
18913 end loop;
18915 return False;
18916 end Unit_In_Context;
18918 -- Start of processing for Unit_Is_Visible
18920 begin
18921 -- The currrent unit is directly visible
18923 if Curr = U then
18924 return True;
18926 elsif Unit_In_Context (Curr) then
18927 return True;
18929 -- If the current unit is a body, check the context of the spec
18931 elsif Nkind (Unit (Curr)) = N_Package_Body
18932 or else
18933 (Nkind (Unit (Curr)) = N_Subprogram_Body
18934 and then not Acts_As_Spec (Unit (Curr)))
18935 then
18936 if Unit_In_Context (Library_Unit (Curr)) then
18937 return True;
18938 end if;
18939 end if;
18941 -- If the spec is a child unit, examine the parents
18943 if Is_Child_Unit (Curr_Entity) then
18944 if Nkind (Unit (Curr)) in N_Unit_Body then
18945 return
18946 Unit_In_Parent_Context
18947 (Parent_Spec (Unit (Library_Unit (Curr))));
18948 else
18949 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
18950 end if;
18952 else
18953 return False;
18954 end if;
18955 end Unit_Is_Visible;
18957 ------------------------------
18958 -- Universal_Interpretation --
18959 ------------------------------
18961 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
18962 Index : Interp_Index;
18963 It : Interp;
18965 begin
18966 -- The argument may be a formal parameter of an operator or subprogram
18967 -- with multiple interpretations, or else an expression for an actual.
18969 if Nkind (Opnd) = N_Defining_Identifier
18970 or else not Is_Overloaded (Opnd)
18971 then
18972 if Etype (Opnd) = Universal_Integer
18973 or else Etype (Opnd) = Universal_Real
18974 then
18975 return Etype (Opnd);
18976 else
18977 return Empty;
18978 end if;
18980 else
18981 Get_First_Interp (Opnd, Index, It);
18982 while Present (It.Typ) loop
18983 if It.Typ = Universal_Integer
18984 or else It.Typ = Universal_Real
18985 then
18986 return It.Typ;
18987 end if;
18989 Get_Next_Interp (Index, It);
18990 end loop;
18992 return Empty;
18993 end if;
18994 end Universal_Interpretation;
18996 ---------------
18997 -- Unqualify --
18998 ---------------
19000 function Unqualify (Expr : Node_Id) return Node_Id is
19001 begin
19002 -- Recurse to handle unlikely case of multiple levels of qualification
19004 if Nkind (Expr) = N_Qualified_Expression then
19005 return Unqualify (Expression (Expr));
19007 -- Normal case, not a qualified expression
19009 else
19010 return Expr;
19011 end if;
19012 end Unqualify;
19014 -----------------------
19015 -- Visible_Ancestors --
19016 -----------------------
19018 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
19019 List_1 : Elist_Id;
19020 List_2 : Elist_Id;
19021 Elmt : Elmt_Id;
19023 begin
19024 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
19026 -- Collect all the parents and progenitors of Typ. If the full-view of
19027 -- private parents and progenitors is available then it is used to
19028 -- generate the list of visible ancestors; otherwise their partial
19029 -- view is added to the resulting list.
19031 Collect_Parents
19032 (T => Typ,
19033 List => List_1,
19034 Use_Full_View => True);
19036 Collect_Interfaces
19037 (T => Typ,
19038 Ifaces_List => List_2,
19039 Exclude_Parents => True,
19040 Use_Full_View => True);
19042 -- Join the two lists. Avoid duplications because an interface may
19043 -- simultaneously be parent and progenitor of a type.
19045 Elmt := First_Elmt (List_2);
19046 while Present (Elmt) loop
19047 Append_Unique_Elmt (Node (Elmt), List_1);
19048 Next_Elmt (Elmt);
19049 end loop;
19051 return List_1;
19052 end Visible_Ancestors;
19054 ----------------------
19055 -- Within_Init_Proc --
19056 ----------------------
19058 function Within_Init_Proc return Boolean is
19059 S : Entity_Id;
19061 begin
19062 S := Current_Scope;
19063 while not Is_Overloadable (S) loop
19064 if S = Standard_Standard then
19065 return False;
19066 else
19067 S := Scope (S);
19068 end if;
19069 end loop;
19071 return Is_Init_Proc (S);
19072 end Within_Init_Proc;
19074 ------------------
19075 -- Within_Scope --
19076 ------------------
19078 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
19079 SE : Entity_Id;
19080 begin
19081 SE := Scope (E);
19082 loop
19083 if SE = S then
19084 return True;
19085 elsif SE = Standard_Standard then
19086 return False;
19087 else
19088 SE := Scope (SE);
19089 end if;
19090 end loop;
19091 end Within_Scope;
19093 ----------------
19094 -- Wrong_Type --
19095 ----------------
19097 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
19098 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
19099 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
19101 Matching_Field : Entity_Id;
19102 -- Entity to give a more precise suggestion on how to write a one-
19103 -- element positional aggregate.
19105 function Has_One_Matching_Field return Boolean;
19106 -- Determines if Expec_Type is a record type with a single component or
19107 -- discriminant whose type matches the found type or is one dimensional
19108 -- array whose component type matches the found type. In the case of
19109 -- one discriminant, we ignore the variant parts. That's not accurate,
19110 -- but good enough for the warning.
19112 ----------------------------
19113 -- Has_One_Matching_Field --
19114 ----------------------------
19116 function Has_One_Matching_Field return Boolean is
19117 E : Entity_Id;
19119 begin
19120 Matching_Field := Empty;
19122 if Is_Array_Type (Expec_Type)
19123 and then Number_Dimensions (Expec_Type) = 1
19124 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
19125 then
19126 -- Use type name if available. This excludes multidimensional
19127 -- arrays and anonymous arrays.
19129 if Comes_From_Source (Expec_Type) then
19130 Matching_Field := Expec_Type;
19132 -- For an assignment, use name of target
19134 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
19135 and then Is_Entity_Name (Name (Parent (Expr)))
19136 then
19137 Matching_Field := Entity (Name (Parent (Expr)));
19138 end if;
19140 return True;
19142 elsif not Is_Record_Type (Expec_Type) then
19143 return False;
19145 else
19146 E := First_Entity (Expec_Type);
19147 loop
19148 if No (E) then
19149 return False;
19151 elsif not Ekind_In (E, E_Discriminant, E_Component)
19152 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
19153 then
19154 Next_Entity (E);
19156 else
19157 exit;
19158 end if;
19159 end loop;
19161 if not Covers (Etype (E), Found_Type) then
19162 return False;
19164 elsif Present (Next_Entity (E))
19165 and then (Ekind (E) = E_Component
19166 or else Ekind (Next_Entity (E)) = E_Discriminant)
19167 then
19168 return False;
19170 else
19171 Matching_Field := E;
19172 return True;
19173 end if;
19174 end if;
19175 end Has_One_Matching_Field;
19177 -- Start of processing for Wrong_Type
19179 begin
19180 -- Don't output message if either type is Any_Type, or if a message
19181 -- has already been posted for this node. We need to do the latter
19182 -- check explicitly (it is ordinarily done in Errout), because we
19183 -- are using ! to force the output of the error messages.
19185 if Expec_Type = Any_Type
19186 or else Found_Type = Any_Type
19187 or else Error_Posted (Expr)
19188 then
19189 return;
19191 -- If one of the types is a Taft-Amendment type and the other it its
19192 -- completion, it must be an illegal use of a TAT in the spec, for
19193 -- which an error was already emitted. Avoid cascaded errors.
19195 elsif Is_Incomplete_Type (Expec_Type)
19196 and then Has_Completion_In_Body (Expec_Type)
19197 and then Full_View (Expec_Type) = Etype (Expr)
19198 then
19199 return;
19201 elsif Is_Incomplete_Type (Etype (Expr))
19202 and then Has_Completion_In_Body (Etype (Expr))
19203 and then Full_View (Etype (Expr)) = Expec_Type
19204 then
19205 return;
19207 -- In an instance, there is an ongoing problem with completion of
19208 -- type derived from private types. Their structure is what Gigi
19209 -- expects, but the Etype is the parent type rather than the
19210 -- derived private type itself. Do not flag error in this case. The
19211 -- private completion is an entity without a parent, like an Itype.
19212 -- Similarly, full and partial views may be incorrect in the instance.
19213 -- There is no simple way to insure that it is consistent ???
19215 -- A similar view discrepancy can happen in an inlined body, for the
19216 -- same reason: inserted body may be outside of the original package
19217 -- and only partial views are visible at the point of insertion.
19219 elsif In_Instance or else In_Inlined_Body then
19220 if Etype (Etype (Expr)) = Etype (Expected_Type)
19221 and then
19222 (Has_Private_Declaration (Expected_Type)
19223 or else Has_Private_Declaration (Etype (Expr)))
19224 and then No (Parent (Expected_Type))
19225 then
19226 return;
19228 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
19229 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
19230 then
19231 return;
19233 elsif Is_Private_Type (Expected_Type)
19234 and then Present (Full_View (Expected_Type))
19235 and then Covers (Full_View (Expected_Type), Etype (Expr))
19236 then
19237 return;
19239 -- Conversely, type of expression may be the private one
19241 elsif Is_Private_Type (Base_Type (Etype (Expr)))
19242 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
19243 then
19244 return;
19245 end if;
19246 end if;
19248 -- An interesting special check. If the expression is parenthesized
19249 -- and its type corresponds to the type of the sole component of the
19250 -- expected record type, or to the component type of the expected one
19251 -- dimensional array type, then assume we have a bad aggregate attempt.
19253 if Nkind (Expr) in N_Subexpr
19254 and then Paren_Count (Expr) /= 0
19255 and then Has_One_Matching_Field
19256 then
19257 Error_Msg_N ("positional aggregate cannot have one component", Expr);
19259 if Present (Matching_Field) then
19260 if Is_Array_Type (Expec_Type) then
19261 Error_Msg_NE
19262 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
19263 else
19264 Error_Msg_NE
19265 ("\write instead `& ='> ...`", Expr, Matching_Field);
19266 end if;
19267 end if;
19269 -- Another special check, if we are looking for a pool-specific access
19270 -- type and we found an E_Access_Attribute_Type, then we have the case
19271 -- of an Access attribute being used in a context which needs a pool-
19272 -- specific type, which is never allowed. The one extra check we make
19273 -- is that the expected designated type covers the Found_Type.
19275 elsif Is_Access_Type (Expec_Type)
19276 and then Ekind (Found_Type) = E_Access_Attribute_Type
19277 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
19278 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
19279 and then Covers
19280 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
19281 then
19282 Error_Msg_N -- CODEFIX
19283 ("result must be general access type!", Expr);
19284 Error_Msg_NE -- CODEFIX
19285 ("add ALL to }!", Expr, Expec_Type);
19287 -- Another special check, if the expected type is an integer type,
19288 -- but the expression is of type System.Address, and the parent is
19289 -- an addition or subtraction operation whose left operand is the
19290 -- expression in question and whose right operand is of an integral
19291 -- type, then this is an attempt at address arithmetic, so give
19292 -- appropriate message.
19294 elsif Is_Integer_Type (Expec_Type)
19295 and then Is_RTE (Found_Type, RE_Address)
19296 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
19297 and then Expr = Left_Opnd (Parent (Expr))
19298 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
19299 then
19300 Error_Msg_N
19301 ("address arithmetic not predefined in package System",
19302 Parent (Expr));
19303 Error_Msg_N
19304 ("\possible missing with/use of System.Storage_Elements",
19305 Parent (Expr));
19306 return;
19308 -- If the expected type is an anonymous access type, as for access
19309 -- parameters and discriminants, the error is on the designated types.
19311 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
19312 if Comes_From_Source (Expec_Type) then
19313 Error_Msg_NE ("expected}!", Expr, Expec_Type);
19314 else
19315 Error_Msg_NE
19316 ("expected an access type with designated}",
19317 Expr, Designated_Type (Expec_Type));
19318 end if;
19320 if Is_Access_Type (Found_Type)
19321 and then not Comes_From_Source (Found_Type)
19322 then
19323 Error_Msg_NE
19324 ("\\found an access type with designated}!",
19325 Expr, Designated_Type (Found_Type));
19326 else
19327 if From_Limited_With (Found_Type) then
19328 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
19329 Error_Msg_Qual_Level := 99;
19330 Error_Msg_NE -- CODEFIX
19331 ("\\missing `WITH &;", Expr, Scope (Found_Type));
19332 Error_Msg_Qual_Level := 0;
19333 else
19334 Error_Msg_NE ("found}!", Expr, Found_Type);
19335 end if;
19336 end if;
19338 -- Normal case of one type found, some other type expected
19340 else
19341 -- If the names of the two types are the same, see if some number
19342 -- of levels of qualification will help. Don't try more than three
19343 -- levels, and if we get to standard, it's no use (and probably
19344 -- represents an error in the compiler) Also do not bother with
19345 -- internal scope names.
19347 declare
19348 Expec_Scope : Entity_Id;
19349 Found_Scope : Entity_Id;
19351 begin
19352 Expec_Scope := Expec_Type;
19353 Found_Scope := Found_Type;
19355 for Levels in Int range 0 .. 3 loop
19356 if Chars (Expec_Scope) /= Chars (Found_Scope) then
19357 Error_Msg_Qual_Level := Levels;
19358 exit;
19359 end if;
19361 Expec_Scope := Scope (Expec_Scope);
19362 Found_Scope := Scope (Found_Scope);
19364 exit when Expec_Scope = Standard_Standard
19365 or else Found_Scope = Standard_Standard
19366 or else not Comes_From_Source (Expec_Scope)
19367 or else not Comes_From_Source (Found_Scope);
19368 end loop;
19369 end;
19371 if Is_Record_Type (Expec_Type)
19372 and then Present (Corresponding_Remote_Type (Expec_Type))
19373 then
19374 Error_Msg_NE ("expected}!", Expr,
19375 Corresponding_Remote_Type (Expec_Type));
19376 else
19377 Error_Msg_NE ("expected}!", Expr, Expec_Type);
19378 end if;
19380 if Is_Entity_Name (Expr)
19381 and then Is_Package_Or_Generic_Package (Entity (Expr))
19382 then
19383 Error_Msg_N ("\\found package name!", Expr);
19385 elsif Is_Entity_Name (Expr)
19386 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
19387 then
19388 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
19389 Error_Msg_N
19390 ("found procedure name, possibly missing Access attribute!",
19391 Expr);
19392 else
19393 Error_Msg_N
19394 ("\\found procedure name instead of function!", Expr);
19395 end if;
19397 elsif Nkind (Expr) = N_Function_Call
19398 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
19399 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
19400 and then No (Parameter_Associations (Expr))
19401 then
19402 Error_Msg_N
19403 ("found function name, possibly missing Access attribute!",
19404 Expr);
19406 -- Catch common error: a prefix or infix operator which is not
19407 -- directly visible because the type isn't.
19409 elsif Nkind (Expr) in N_Op
19410 and then Is_Overloaded (Expr)
19411 and then not Is_Immediately_Visible (Expec_Type)
19412 and then not Is_Potentially_Use_Visible (Expec_Type)
19413 and then not In_Use (Expec_Type)
19414 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
19415 then
19416 Error_Msg_N
19417 ("operator of the type is not directly visible!", Expr);
19419 elsif Ekind (Found_Type) = E_Void
19420 and then Present (Parent (Found_Type))
19421 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
19422 then
19423 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
19425 else
19426 Error_Msg_NE ("\\found}!", Expr, Found_Type);
19427 end if;
19429 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
19430 -- of the same modular type, and (M1 and M2) = 0 was intended.
19432 if Expec_Type = Standard_Boolean
19433 and then Is_Modular_Integer_Type (Found_Type)
19434 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
19435 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
19436 then
19437 declare
19438 Op : constant Node_Id := Right_Opnd (Parent (Expr));
19439 L : constant Node_Id := Left_Opnd (Op);
19440 R : constant Node_Id := Right_Opnd (Op);
19442 begin
19443 -- The case for the message is when the left operand of the
19444 -- comparison is the same modular type, or when it is an
19445 -- integer literal (or other universal integer expression),
19446 -- which would have been typed as the modular type if the
19447 -- parens had been there.
19449 if (Etype (L) = Found_Type
19450 or else
19451 Etype (L) = Universal_Integer)
19452 and then Is_Integer_Type (Etype (R))
19453 then
19454 Error_Msg_N
19455 ("\\possible missing parens for modular operation", Expr);
19456 end if;
19457 end;
19458 end if;
19460 -- Reset error message qualification indication
19462 Error_Msg_Qual_Level := 0;
19463 end if;
19464 end Wrong_Type;
19466 end Sem_Util;