2015-03-02 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_util.adb
blobd9ab705bd13e10946934ee565e45e2ff7d8bea64
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 Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Namet.Sp; use Namet.Sp;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Output; use Output;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Attr; use Sem_Attr;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Res; use Sem_Res;
57 with Sem_Warn; use Sem_Warn;
58 with Sem_Type; use Sem_Type;
59 with Sinfo; use Sinfo;
60 with Sinput; use Sinput;
61 with Stand; use Stand;
62 with Style;
63 with Stringt; use Stringt;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Ttypes; use Ttypes;
67 with Uname; use Uname;
69 with GNAT.HTable; use GNAT.HTable;
71 package body Sem_Util is
73 ----------------------------------------
74 -- Global Variables for New_Copy_Tree --
75 ----------------------------------------
77 -- These global variables are used by New_Copy_Tree. See description of the
78 -- body of this subprogram for details. Global variables can be safely used
79 -- by New_Copy_Tree, since there is no case of a recursive call from the
80 -- processing inside New_Copy_Tree.
82 NCT_Hash_Threshold : constant := 20;
83 -- If there are more than this number of pairs of entries in the map, then
84 -- Hash_Tables_Used will be set, and the hash tables will be initialized
85 -- and used for the searches.
87 NCT_Hash_Tables_Used : Boolean := False;
88 -- Set to True if hash tables are in use
90 NCT_Table_Entries : Nat := 0;
91 -- Count entries in table to see if threshold is reached
93 NCT_Hash_Table_Setup : Boolean := False;
94 -- Set to True if hash table contains data. We set this True if we setup
95 -- the hash table with data, and leave it set permanently from then on,
96 -- this is a signal that second and subsequent users of the hash table
97 -- must clear the old entries before reuse.
99 subtype NCT_Header_Num is Int range 0 .. 511;
100 -- Defines range of headers in hash tables (512 headers)
102 -----------------------
103 -- Local Subprograms --
104 -----------------------
106 function Build_Component_Subtype
107 (C : List_Id;
108 Loc : Source_Ptr;
109 T : Entity_Id) return Node_Id;
110 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
111 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
112 -- Loc is the source location, T is the original subtype.
114 function Has_Enabled_Property
115 (Item_Id : Entity_Id;
116 Property : Name_Id) return Boolean;
117 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
118 -- Determine whether an abstract state or a variable denoted by entity
119 -- Item_Id has enabled property Property.
121 function Has_Null_Extension (T : Entity_Id) return Boolean;
122 -- T is a derived tagged type. Check whether the type extension is null.
123 -- If the parent type is fully initialized, T can be treated as such.
125 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
126 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
127 -- with discriminants whose default values are static, examine only the
128 -- components in the selected variant to determine whether all of them
129 -- have a default.
131 ------------------------------
132 -- Abstract_Interface_List --
133 ------------------------------
135 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
136 Nod : Node_Id;
138 begin
139 if Is_Concurrent_Type (Typ) then
141 -- If we are dealing with a synchronized subtype, go to the base
142 -- type, whose declaration has the interface list.
144 -- Shouldn't this be Declaration_Node???
146 Nod := Parent (Base_Type (Typ));
148 if Nkind (Nod) = N_Full_Type_Declaration then
149 return Empty_List;
150 end if;
152 elsif Ekind (Typ) = E_Record_Type_With_Private then
153 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
154 Nod := Type_Definition (Parent (Typ));
156 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
157 if Present (Full_View (Typ))
158 and then
159 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
160 then
161 Nod := Type_Definition (Parent (Full_View (Typ)));
163 -- If the full-view is not available we cannot do anything else
164 -- here (the source has errors).
166 else
167 return Empty_List;
168 end if;
170 -- Support for generic formals with interfaces is still missing ???
172 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
173 return Empty_List;
175 else
176 pragma Assert
177 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
178 Nod := Parent (Typ);
179 end if;
181 elsif Ekind (Typ) = E_Record_Subtype then
182 Nod := Type_Definition (Parent (Etype (Typ)));
184 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
186 -- Recurse, because parent may still be a private extension. Also
187 -- note that the full view of the subtype or the full view of its
188 -- base type may (both) be unavailable.
190 return Abstract_Interface_List (Etype (Typ));
192 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
193 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
194 Nod := Formal_Type_Definition (Parent (Typ));
195 else
196 Nod := Type_Definition (Parent (Typ));
197 end if;
198 end if;
200 return Interface_List (Nod);
201 end Abstract_Interface_List;
203 --------------------------------
204 -- Add_Access_Type_To_Process --
205 --------------------------------
207 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
208 L : Elist_Id;
210 begin
211 Ensure_Freeze_Node (E);
212 L := Access_Types_To_Process (Freeze_Node (E));
214 if No (L) then
215 L := New_Elmt_List;
216 Set_Access_Types_To_Process (Freeze_Node (E), L);
217 end if;
219 Append_Elmt (A, L);
220 end Add_Access_Type_To_Process;
222 --------------------------
223 -- Add_Block_Identifier --
224 --------------------------
226 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
227 Loc : constant Source_Ptr := Sloc (N);
229 begin
230 pragma Assert (Nkind (N) = N_Block_Statement);
232 -- The block already has a label, return its entity
234 if Present (Identifier (N)) then
235 Id := Entity (Identifier (N));
237 -- Create a new block label and set its attributes
239 else
240 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
241 Set_Etype (Id, Standard_Void_Type);
242 Set_Parent (Id, N);
244 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
245 Set_Block_Node (Id, Identifier (N));
246 end if;
247 end Add_Block_Identifier;
249 -----------------------
250 -- Add_Contract_Item --
251 -----------------------
253 procedure Add_Contract_Item (Prag : Node_Id; Id : Entity_Id) is
254 Items : Node_Id := Contract (Id);
256 procedure Add_Classification;
257 -- Prepend Prag to the list of classifications
259 procedure Add_Contract_Test_Case;
260 -- Prepend Prag to the list of contract and test cases
262 procedure Add_Pre_Post_Condition;
263 -- Prepend Prag to the list of pre- and postconditions
265 ------------------------
266 -- Add_Classification --
267 ------------------------
269 procedure Add_Classification is
270 begin
271 Set_Next_Pragma (Prag, Classifications (Items));
272 Set_Classifications (Items, Prag);
273 end Add_Classification;
275 ----------------------------
276 -- Add_Contract_Test_Case --
277 ----------------------------
279 procedure Add_Contract_Test_Case is
280 begin
281 Set_Next_Pragma (Prag, Contract_Test_Cases (Items));
282 Set_Contract_Test_Cases (Items, Prag);
283 end Add_Contract_Test_Case;
285 ----------------------------
286 -- Add_Pre_Post_Condition --
287 ----------------------------
289 procedure Add_Pre_Post_Condition is
290 begin
291 Set_Next_Pragma (Prag, Pre_Post_Conditions (Items));
292 Set_Pre_Post_Conditions (Items, Prag);
293 end Add_Pre_Post_Condition;
295 -- Local variables
297 Prag_Nam : Name_Id;
299 -- Start of processing for Add_Contract_Item
301 begin
302 -- A contract must contain only pragmas
304 pragma Assert (Nkind (Prag) = N_Pragma);
305 Prag_Nam := Pragma_Name (Prag);
307 -- Create a new contract when adding the first item
309 if No (Items) then
310 Items := Make_Contract (Sloc (Id));
311 Set_Contract (Id, Items);
312 end if;
314 -- Contract items related to [generic] packages or instantiations. The
315 -- applicable pragmas are:
316 -- Abstract_States
317 -- Initial_Condition
318 -- Initializes
319 -- Part_Of (instantiation only)
321 if Ekind_In (Id, E_Generic_Package, E_Package) then
322 if Nam_In (Prag_Nam, Name_Abstract_State,
323 Name_Initial_Condition,
324 Name_Initializes)
325 then
326 Add_Classification;
328 -- Indicator Part_Of must be associated with a package instantiation
330 elsif Prag_Nam = Name_Part_Of and then Is_Generic_Instance (Id) then
331 Add_Classification;
333 -- The pragma is not a proper contract item
335 else
336 raise Program_Error;
337 end if;
339 -- Contract items related to package bodies. The applicable pragmas are:
340 -- Refined_States
342 elsif Ekind (Id) = E_Package_Body then
343 if Prag_Nam = Name_Refined_State then
344 Add_Classification;
346 -- The pragma is not a proper contract item
348 else
349 raise Program_Error;
350 end if;
352 -- Contract items related to subprogram or entry declarations. The
353 -- applicable pragmas are:
354 -- Contract_Cases
355 -- Depends
356 -- Extensions_Visible
357 -- Global
358 -- Postcondition
359 -- Precondition
360 -- Test_Case
362 elsif Ekind_In (Id, E_Entry, E_Entry_Family)
363 or else Is_Generic_Subprogram (Id)
364 or else Is_Subprogram (Id)
365 then
366 if Nam_In (Prag_Nam, Name_Postcondition, Name_Precondition) then
367 Add_Pre_Post_Condition;
369 elsif Nam_In (Prag_Nam, Name_Contract_Cases, Name_Test_Case) then
370 Add_Contract_Test_Case;
372 elsif Nam_In (Prag_Nam, Name_Depends,
373 Name_Extensions_Visible,
374 Name_Global)
375 then
376 Add_Classification;
378 -- The pragma is not a proper contract item
380 else
381 raise Program_Error;
382 end if;
384 -- Contract items related to subprogram bodies. Applicable pragmas are:
385 -- Postcondition
386 -- Precondition
387 -- Refined_Depends
388 -- Refined_Global
389 -- Refined_Post
391 elsif Ekind (Id) = E_Subprogram_Body then
392 if Nam_In (Prag_Nam, Name_Refined_Depends, Name_Refined_Global) then
393 Add_Classification;
395 elsif Nam_In (Prag_Nam, Name_Postcondition,
396 Name_Precondition,
397 Name_Refined_Post)
398 then
399 Add_Pre_Post_Condition;
401 -- The pragma is not a proper contract item
403 else
404 raise Program_Error;
405 end if;
407 -- Contract items related to variables. Applicable pragmas are:
408 -- Async_Readers
409 -- Async_Writers
410 -- Effective_Reads
411 -- Effective_Writes
412 -- Part_Of
414 elsif Ekind (Id) = E_Variable then
415 if Nam_In (Prag_Nam, Name_Async_Readers,
416 Name_Async_Writers,
417 Name_Effective_Reads,
418 Name_Effective_Writes,
419 Name_Part_Of)
420 then
421 Add_Classification;
423 -- The pragma is not a proper contract item
425 else
426 raise Program_Error;
427 end if;
428 end if;
429 end Add_Contract_Item;
431 ----------------------------
432 -- Add_Global_Declaration --
433 ----------------------------
435 procedure Add_Global_Declaration (N : Node_Id) is
436 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
438 begin
439 if No (Declarations (Aux_Node)) then
440 Set_Declarations (Aux_Node, New_List);
441 end if;
443 Append_To (Declarations (Aux_Node), N);
444 Analyze (N);
445 end Add_Global_Declaration;
447 --------------------------------
448 -- Address_Integer_Convert_OK --
449 --------------------------------
451 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
452 begin
453 if Allow_Integer_Address
454 and then ((Is_Descendent_Of_Address (T1)
455 and then Is_Private_Type (T1)
456 and then Is_Integer_Type (T2))
457 or else
458 (Is_Descendent_Of_Address (T2)
459 and then Is_Private_Type (T2)
460 and then Is_Integer_Type (T1)))
461 then
462 return True;
463 else
464 return False;
465 end if;
466 end Address_Integer_Convert_OK;
468 -----------------
469 -- Addressable --
470 -----------------
472 -- For now, just 8/16/32/64. but analyze later if AAMP is special???
474 function Addressable (V : Uint) return Boolean is
475 begin
476 return V = Uint_8 or else
477 V = Uint_16 or else
478 V = Uint_32 or else
479 V = Uint_64;
480 end Addressable;
482 function Addressable (V : Int) return Boolean is
483 begin
484 return V = 8 or else
485 V = 16 or else
486 V = 32 or else
487 V = 64;
488 end Addressable;
490 ---------------------------------
491 -- Aggregate_Constraint_Checks --
492 ---------------------------------
494 procedure Aggregate_Constraint_Checks
495 (Exp : Node_Id;
496 Check_Typ : Entity_Id)
498 Exp_Typ : constant Entity_Id := Etype (Exp);
500 begin
501 if Raises_Constraint_Error (Exp) then
502 return;
503 end if;
505 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
506 -- component's type to force the appropriate accessibility checks.
508 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
509 -- type to force the corresponding run-time check
511 if Is_Access_Type (Check_Typ)
512 and then ((Is_Local_Anonymous_Access (Check_Typ))
513 or else (Can_Never_Be_Null (Check_Typ)
514 and then not Can_Never_Be_Null (Exp_Typ)))
515 then
516 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
517 Analyze_And_Resolve (Exp, Check_Typ);
518 Check_Unset_Reference (Exp);
519 end if;
521 -- This is really expansion activity, so make sure that expansion is
522 -- on and is allowed. In GNATprove mode, we also want check flags to
523 -- be added in the tree, so that the formal verification can rely on
524 -- those to be present. In GNATprove mode for formal verification, some
525 -- treatment typically only done during expansion needs to be performed
526 -- on the tree, but it should not be applied inside generics. Otherwise,
527 -- this breaks the name resolution mechanism for generic instances.
529 if not Expander_Active
530 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
531 then
532 return;
533 end if;
535 -- First check if we have to insert discriminant checks
537 if Has_Discriminants (Exp_Typ) then
538 Apply_Discriminant_Check (Exp, Check_Typ);
540 -- Next emit length checks for array aggregates
542 elsif Is_Array_Type (Exp_Typ) then
543 Apply_Length_Check (Exp, Check_Typ);
545 -- Finally emit scalar and string checks. If we are dealing with a
546 -- scalar literal we need to check by hand because the Etype of
547 -- literals is not necessarily correct.
549 elsif Is_Scalar_Type (Exp_Typ)
550 and then Compile_Time_Known_Value (Exp)
551 then
552 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
553 Apply_Compile_Time_Constraint_Error
554 (Exp, "value not in range of}??", CE_Range_Check_Failed,
555 Ent => Base_Type (Check_Typ),
556 Typ => Base_Type (Check_Typ));
558 elsif Is_Out_Of_Range (Exp, Check_Typ) then
559 Apply_Compile_Time_Constraint_Error
560 (Exp, "value not in range of}??", CE_Range_Check_Failed,
561 Ent => Check_Typ,
562 Typ => Check_Typ);
564 elsif not Range_Checks_Suppressed (Check_Typ) then
565 Apply_Scalar_Range_Check (Exp, Check_Typ);
566 end if;
568 -- Verify that target type is also scalar, to prevent view anomalies
569 -- in instantiations.
571 elsif (Is_Scalar_Type (Exp_Typ)
572 or else Nkind (Exp) = N_String_Literal)
573 and then Is_Scalar_Type (Check_Typ)
574 and then Exp_Typ /= Check_Typ
575 then
576 if Is_Entity_Name (Exp)
577 and then Ekind (Entity (Exp)) = E_Constant
578 then
579 -- If expression is a constant, it is worthwhile checking whether
580 -- it is a bound of the type.
582 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
583 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
584 or else
585 (Is_Entity_Name (Type_High_Bound (Check_Typ))
586 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
587 then
588 return;
590 else
591 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
592 Analyze_And_Resolve (Exp, Check_Typ);
593 Check_Unset_Reference (Exp);
594 end if;
596 -- Could use a comment on this case ???
598 else
599 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
600 Analyze_And_Resolve (Exp, Check_Typ);
601 Check_Unset_Reference (Exp);
602 end if;
604 end if;
605 end Aggregate_Constraint_Checks;
607 -----------------------
608 -- Alignment_In_Bits --
609 -----------------------
611 function Alignment_In_Bits (E : Entity_Id) return Uint is
612 begin
613 return Alignment (E) * System_Storage_Unit;
614 end Alignment_In_Bits;
616 ---------------------------------
617 -- Append_Inherited_Subprogram --
618 ---------------------------------
620 procedure Append_Inherited_Subprogram (S : Entity_Id) is
621 Par : constant Entity_Id := Alias (S);
622 -- The parent subprogram
624 Scop : constant Entity_Id := Scope (Par);
625 -- The scope of definition of the parent subprogram
627 Typ : constant Entity_Id := Defining_Entity (Parent (S));
628 -- The derived type of which S is a primitive operation
630 Decl : Node_Id;
631 Next_E : Entity_Id;
633 begin
634 if Ekind (Current_Scope) = E_Package
635 and then In_Private_Part (Current_Scope)
636 and then Has_Private_Declaration (Typ)
637 and then Is_Tagged_Type (Typ)
638 and then Scop = Current_Scope
639 then
640 -- The inherited operation is available at the earliest place after
641 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
642 -- relevant for type extensions. If the parent operation appears
643 -- after the type extension, the operation is not visible.
645 Decl := First
646 (Visible_Declarations
647 (Package_Specification (Current_Scope)));
648 while Present (Decl) loop
649 if Nkind (Decl) = N_Private_Extension_Declaration
650 and then Defining_Entity (Decl) = Typ
651 then
652 if Sloc (Decl) > Sloc (Par) then
653 Next_E := Next_Entity (Par);
654 Set_Next_Entity (Par, S);
655 Set_Next_Entity (S, Next_E);
656 return;
658 else
659 exit;
660 end if;
661 end if;
663 Next (Decl);
664 end loop;
665 end if;
667 -- If partial view is not a type extension, or it appears before the
668 -- subprogram declaration, insert normally at end of entity list.
670 Append_Entity (S, Current_Scope);
671 end Append_Inherited_Subprogram;
673 -----------------------------------------
674 -- Apply_Compile_Time_Constraint_Error --
675 -----------------------------------------
677 procedure Apply_Compile_Time_Constraint_Error
678 (N : Node_Id;
679 Msg : String;
680 Reason : RT_Exception_Code;
681 Ent : Entity_Id := Empty;
682 Typ : Entity_Id := Empty;
683 Loc : Source_Ptr := No_Location;
684 Rep : Boolean := True;
685 Warn : Boolean := False)
687 Stat : constant Boolean := Is_Static_Expression (N);
688 R_Stat : constant Node_Id :=
689 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
690 Rtyp : Entity_Id;
692 begin
693 if No (Typ) then
694 Rtyp := Etype (N);
695 else
696 Rtyp := Typ;
697 end if;
699 Discard_Node
700 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
702 if not Rep then
703 return;
704 end if;
706 -- Now we replace the node by an N_Raise_Constraint_Error node
707 -- This does not need reanalyzing, so set it as analyzed now.
709 Rewrite (N, R_Stat);
710 Set_Analyzed (N, True);
712 Set_Etype (N, Rtyp);
713 Set_Raises_Constraint_Error (N);
715 -- Now deal with possible local raise handling
717 Possible_Local_Raise (N, Standard_Constraint_Error);
719 -- If the original expression was marked as static, the result is
720 -- still marked as static, but the Raises_Constraint_Error flag is
721 -- always set so that further static evaluation is not attempted.
723 if Stat then
724 Set_Is_Static_Expression (N);
725 end if;
726 end Apply_Compile_Time_Constraint_Error;
728 ---------------------------
729 -- Async_Readers_Enabled --
730 ---------------------------
732 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
733 begin
734 return Has_Enabled_Property (Id, Name_Async_Readers);
735 end Async_Readers_Enabled;
737 ---------------------------
738 -- Async_Writers_Enabled --
739 ---------------------------
741 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
742 begin
743 return Has_Enabled_Property (Id, Name_Async_Writers);
744 end Async_Writers_Enabled;
746 --------------------------------------
747 -- Available_Full_View_Of_Component --
748 --------------------------------------
750 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
751 ST : constant Entity_Id := Scope (T);
752 SCT : constant Entity_Id := Scope (Component_Type (T));
753 begin
754 return In_Open_Scopes (ST)
755 and then In_Open_Scopes (SCT)
756 and then Scope_Depth (ST) >= Scope_Depth (SCT);
757 end Available_Full_View_Of_Component;
759 -------------------
760 -- Bad_Attribute --
761 -------------------
763 procedure Bad_Attribute
764 (N : Node_Id;
765 Nam : Name_Id;
766 Warn : Boolean := False)
768 begin
769 Error_Msg_Warn := Warn;
770 Error_Msg_N ("unrecognized attribute&<<", N);
772 -- Check for possible misspelling
774 Error_Msg_Name_1 := First_Attribute_Name;
775 while Error_Msg_Name_1 <= Last_Attribute_Name loop
776 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
777 Error_Msg_N -- CODEFIX
778 ("\possible misspelling of %<<", N);
779 exit;
780 end if;
782 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
783 end loop;
784 end Bad_Attribute;
786 --------------------------------
787 -- Bad_Predicated_Subtype_Use --
788 --------------------------------
790 procedure Bad_Predicated_Subtype_Use
791 (Msg : String;
792 N : Node_Id;
793 Typ : Entity_Id;
794 Suggest_Static : Boolean := False)
796 Gen : Entity_Id;
798 begin
799 -- Avoid cascaded errors
801 if Error_Posted (N) then
802 return;
803 end if;
805 if Inside_A_Generic then
806 Gen := Current_Scope;
807 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
808 Gen := Scope (Gen);
809 end loop;
811 if No (Gen) then
812 return;
813 end if;
815 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
816 Set_No_Predicate_On_Actual (Typ);
817 end if;
819 elsif Has_Predicates (Typ) then
820 if Is_Generic_Actual_Type (Typ) then
822 -- The restriction on loop parameters is only that the type
823 -- should have no dynamic predicates.
825 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
826 and then not Has_Dynamic_Predicate_Aspect (Typ)
827 and then Is_OK_Static_Subtype (Typ)
828 then
829 return;
830 end if;
832 Gen := Current_Scope;
833 while not Is_Generic_Instance (Gen) loop
834 Gen := Scope (Gen);
835 end loop;
837 pragma Assert (Present (Gen));
839 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
840 Error_Msg_Warn := SPARK_Mode /= On;
841 Error_Msg_FE (Msg & "<<", N, Typ);
842 Error_Msg_F ("\Program_Error [<<", N);
844 Insert_Action (N,
845 Make_Raise_Program_Error (Sloc (N),
846 Reason => PE_Bad_Predicated_Generic_Type));
848 else
849 Error_Msg_FE (Msg & "<<", N, Typ);
850 end if;
852 else
853 Error_Msg_FE (Msg, N, Typ);
854 end if;
856 -- Emit an optional suggestion on how to remedy the error if the
857 -- context warrants it.
859 if Suggest_Static and then Has_Static_Predicate (Typ) then
860 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
861 end if;
862 end if;
863 end Bad_Predicated_Subtype_Use;
865 -----------------------------------------
866 -- Bad_Unordered_Enumeration_Reference --
867 -----------------------------------------
869 function Bad_Unordered_Enumeration_Reference
870 (N : Node_Id;
871 T : Entity_Id) return Boolean
873 begin
874 return Is_Enumeration_Type (T)
875 and then Warn_On_Unordered_Enumeration_Type
876 and then not Is_Generic_Type (T)
877 and then Comes_From_Source (N)
878 and then not Has_Pragma_Ordered (T)
879 and then not In_Same_Extended_Unit (N, T);
880 end Bad_Unordered_Enumeration_Reference;
882 --------------------------
883 -- Build_Actual_Subtype --
884 --------------------------
886 function Build_Actual_Subtype
887 (T : Entity_Id;
888 N : Node_Or_Entity_Id) return Node_Id
890 Loc : Source_Ptr;
891 -- Normally Sloc (N), but may point to corresponding body in some cases
893 Constraints : List_Id;
894 Decl : Node_Id;
895 Discr : Entity_Id;
896 Hi : Node_Id;
897 Lo : Node_Id;
898 Subt : Entity_Id;
899 Disc_Type : Entity_Id;
900 Obj : Node_Id;
902 begin
903 Loc := Sloc (N);
905 if Nkind (N) = N_Defining_Identifier then
906 Obj := New_Occurrence_Of (N, Loc);
908 -- If this is a formal parameter of a subprogram declaration, and
909 -- we are compiling the body, we want the declaration for the
910 -- actual subtype to carry the source position of the body, to
911 -- prevent anomalies in gdb when stepping through the code.
913 if Is_Formal (N) then
914 declare
915 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
916 begin
917 if Nkind (Decl) = N_Subprogram_Declaration
918 and then Present (Corresponding_Body (Decl))
919 then
920 Loc := Sloc (Corresponding_Body (Decl));
921 end if;
922 end;
923 end if;
925 else
926 Obj := N;
927 end if;
929 if Is_Array_Type (T) then
930 Constraints := New_List;
931 for J in 1 .. Number_Dimensions (T) loop
933 -- Build an array subtype declaration with the nominal subtype and
934 -- the bounds of the actual. Add the declaration in front of the
935 -- local declarations for the subprogram, for analysis before any
936 -- reference to the formal in the body.
938 Lo :=
939 Make_Attribute_Reference (Loc,
940 Prefix =>
941 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
942 Attribute_Name => Name_First,
943 Expressions => New_List (
944 Make_Integer_Literal (Loc, J)));
946 Hi :=
947 Make_Attribute_Reference (Loc,
948 Prefix =>
949 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
950 Attribute_Name => Name_Last,
951 Expressions => New_List (
952 Make_Integer_Literal (Loc, J)));
954 Append (Make_Range (Loc, Lo, Hi), Constraints);
955 end loop;
957 -- If the type has unknown discriminants there is no constrained
958 -- subtype to build. This is never called for a formal or for a
959 -- lhs, so returning the type is ok ???
961 elsif Has_Unknown_Discriminants (T) then
962 return T;
964 else
965 Constraints := New_List;
967 -- Type T is a generic derived type, inherit the discriminants from
968 -- the parent type.
970 if Is_Private_Type (T)
971 and then No (Full_View (T))
973 -- T was flagged as an error if it was declared as a formal
974 -- derived type with known discriminants. In this case there
975 -- is no need to look at the parent type since T already carries
976 -- its own discriminants.
978 and then not Error_Posted (T)
979 then
980 Disc_Type := Etype (Base_Type (T));
981 else
982 Disc_Type := T;
983 end if;
985 Discr := First_Discriminant (Disc_Type);
986 while Present (Discr) loop
987 Append_To (Constraints,
988 Make_Selected_Component (Loc,
989 Prefix =>
990 Duplicate_Subexpr_No_Checks (Obj),
991 Selector_Name => New_Occurrence_Of (Discr, Loc)));
992 Next_Discriminant (Discr);
993 end loop;
994 end if;
996 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
997 Set_Is_Internal (Subt);
999 Decl :=
1000 Make_Subtype_Declaration (Loc,
1001 Defining_Identifier => Subt,
1002 Subtype_Indication =>
1003 Make_Subtype_Indication (Loc,
1004 Subtype_Mark => New_Occurrence_Of (T, Loc),
1005 Constraint =>
1006 Make_Index_Or_Discriminant_Constraint (Loc,
1007 Constraints => Constraints)));
1009 Mark_Rewrite_Insertion (Decl);
1010 return Decl;
1011 end Build_Actual_Subtype;
1013 ---------------------------------------
1014 -- Build_Actual_Subtype_Of_Component --
1015 ---------------------------------------
1017 function Build_Actual_Subtype_Of_Component
1018 (T : Entity_Id;
1019 N : Node_Id) return Node_Id
1021 Loc : constant Source_Ptr := Sloc (N);
1022 P : constant Node_Id := Prefix (N);
1023 D : Elmt_Id;
1024 Id : Node_Id;
1025 Index_Typ : Entity_Id;
1027 Desig_Typ : Entity_Id;
1028 -- This is either a copy of T, or if T is an access type, then it is
1029 -- the directly designated type of this access type.
1031 function Build_Actual_Array_Constraint return List_Id;
1032 -- If one or more of the bounds of the component depends on
1033 -- discriminants, build actual constraint using the discriminants
1034 -- of the prefix.
1036 function Build_Actual_Record_Constraint return List_Id;
1037 -- Similar to previous one, for discriminated components constrained
1038 -- by the discriminant of the enclosing object.
1040 -----------------------------------
1041 -- Build_Actual_Array_Constraint --
1042 -----------------------------------
1044 function Build_Actual_Array_Constraint return List_Id is
1045 Constraints : constant List_Id := New_List;
1046 Indx : Node_Id;
1047 Hi : Node_Id;
1048 Lo : Node_Id;
1049 Old_Hi : Node_Id;
1050 Old_Lo : Node_Id;
1052 begin
1053 Indx := First_Index (Desig_Typ);
1054 while Present (Indx) loop
1055 Old_Lo := Type_Low_Bound (Etype (Indx));
1056 Old_Hi := Type_High_Bound (Etype (Indx));
1058 if Denotes_Discriminant (Old_Lo) then
1059 Lo :=
1060 Make_Selected_Component (Loc,
1061 Prefix => New_Copy_Tree (P),
1062 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1064 else
1065 Lo := New_Copy_Tree (Old_Lo);
1067 -- The new bound will be reanalyzed in the enclosing
1068 -- declaration. For literal bounds that come from a type
1069 -- declaration, the type of the context must be imposed, so
1070 -- insure that analysis will take place. For non-universal
1071 -- types this is not strictly necessary.
1073 Set_Analyzed (Lo, False);
1074 end if;
1076 if Denotes_Discriminant (Old_Hi) then
1077 Hi :=
1078 Make_Selected_Component (Loc,
1079 Prefix => New_Copy_Tree (P),
1080 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1082 else
1083 Hi := New_Copy_Tree (Old_Hi);
1084 Set_Analyzed (Hi, False);
1085 end if;
1087 Append (Make_Range (Loc, Lo, Hi), Constraints);
1088 Next_Index (Indx);
1089 end loop;
1091 return Constraints;
1092 end Build_Actual_Array_Constraint;
1094 ------------------------------------
1095 -- Build_Actual_Record_Constraint --
1096 ------------------------------------
1098 function Build_Actual_Record_Constraint return List_Id is
1099 Constraints : constant List_Id := New_List;
1100 D : Elmt_Id;
1101 D_Val : Node_Id;
1103 begin
1104 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1105 while Present (D) loop
1106 if Denotes_Discriminant (Node (D)) then
1107 D_Val := Make_Selected_Component (Loc,
1108 Prefix => New_Copy_Tree (P),
1109 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1111 else
1112 D_Val := New_Copy_Tree (Node (D));
1113 end if;
1115 Append (D_Val, Constraints);
1116 Next_Elmt (D);
1117 end loop;
1119 return Constraints;
1120 end Build_Actual_Record_Constraint;
1122 -- Start of processing for Build_Actual_Subtype_Of_Component
1124 begin
1125 -- Why the test for Spec_Expression mode here???
1127 if In_Spec_Expression then
1128 return Empty;
1130 -- More comments for the rest of this body would be good ???
1132 elsif Nkind (N) = N_Explicit_Dereference then
1133 if Is_Composite_Type (T)
1134 and then not Is_Constrained (T)
1135 and then not (Is_Class_Wide_Type (T)
1136 and then Is_Constrained (Root_Type (T)))
1137 and then not Has_Unknown_Discriminants (T)
1138 then
1139 -- If the type of the dereference is already constrained, it is an
1140 -- actual subtype.
1142 if Is_Array_Type (Etype (N))
1143 and then Is_Constrained (Etype (N))
1144 then
1145 return Empty;
1146 else
1147 Remove_Side_Effects (P);
1148 return Build_Actual_Subtype (T, N);
1149 end if;
1150 else
1151 return Empty;
1152 end if;
1153 end if;
1155 if Ekind (T) = E_Access_Subtype then
1156 Desig_Typ := Designated_Type (T);
1157 else
1158 Desig_Typ := T;
1159 end if;
1161 if Ekind (Desig_Typ) = E_Array_Subtype then
1162 Id := First_Index (Desig_Typ);
1163 while Present (Id) loop
1164 Index_Typ := Underlying_Type (Etype (Id));
1166 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1167 or else
1168 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1169 then
1170 Remove_Side_Effects (P);
1171 return
1172 Build_Component_Subtype
1173 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1174 end if;
1176 Next_Index (Id);
1177 end loop;
1179 elsif Is_Composite_Type (Desig_Typ)
1180 and then Has_Discriminants (Desig_Typ)
1181 and then not Has_Unknown_Discriminants (Desig_Typ)
1182 then
1183 if Is_Private_Type (Desig_Typ)
1184 and then No (Discriminant_Constraint (Desig_Typ))
1185 then
1186 Desig_Typ := Full_View (Desig_Typ);
1187 end if;
1189 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1190 while Present (D) loop
1191 if Denotes_Discriminant (Node (D)) then
1192 Remove_Side_Effects (P);
1193 return
1194 Build_Component_Subtype (
1195 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1196 end if;
1198 Next_Elmt (D);
1199 end loop;
1200 end if;
1202 -- If none of the above, the actual and nominal subtypes are the same
1204 return Empty;
1205 end Build_Actual_Subtype_Of_Component;
1207 -----------------------------
1208 -- Build_Component_Subtype --
1209 -----------------------------
1211 function Build_Component_Subtype
1212 (C : List_Id;
1213 Loc : Source_Ptr;
1214 T : Entity_Id) return Node_Id
1216 Subt : Entity_Id;
1217 Decl : Node_Id;
1219 begin
1220 -- Unchecked_Union components do not require component subtypes
1222 if Is_Unchecked_Union (T) then
1223 return Empty;
1224 end if;
1226 Subt := Make_Temporary (Loc, 'S');
1227 Set_Is_Internal (Subt);
1229 Decl :=
1230 Make_Subtype_Declaration (Loc,
1231 Defining_Identifier => Subt,
1232 Subtype_Indication =>
1233 Make_Subtype_Indication (Loc,
1234 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1235 Constraint =>
1236 Make_Index_Or_Discriminant_Constraint (Loc,
1237 Constraints => C)));
1239 Mark_Rewrite_Insertion (Decl);
1240 return Decl;
1241 end Build_Component_Subtype;
1243 ----------------------------------
1244 -- Build_Default_Init_Cond_Call --
1245 ----------------------------------
1247 function Build_Default_Init_Cond_Call
1248 (Loc : Source_Ptr;
1249 Obj_Id : Entity_Id;
1250 Typ : Entity_Id) return Node_Id
1252 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1253 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1255 begin
1256 return
1257 Make_Procedure_Call_Statement (Loc,
1258 Name => New_Occurrence_Of (Proc_Id, Loc),
1259 Parameter_Associations => New_List (
1260 Make_Unchecked_Type_Conversion (Loc,
1261 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1262 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1263 end Build_Default_Init_Cond_Call;
1265 ----------------------------------------------
1266 -- Build_Default_Init_Cond_Procedure_Bodies --
1267 ----------------------------------------------
1269 procedure Build_Default_Init_Cond_Procedure_Bodies (Priv_Decls : List_Id) is
1270 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id);
1271 -- If type Typ is subject to pragma Default_Initial_Condition, build the
1272 -- body of the procedure which verifies the assumption of the pragma at
1273 -- run time. The generated body is added after the type declaration.
1275 --------------------------------------------
1276 -- Build_Default_Init_Cond_Procedure_Body --
1277 --------------------------------------------
1279 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id) is
1280 Param_Id : Entity_Id;
1281 -- The entity of the sole formal parameter of the default initial
1282 -- condition procedure.
1284 procedure Replace_Type_Reference (N : Node_Id);
1285 -- Replace a single reference to type Typ with a reference to formal
1286 -- parameter Param_Id.
1288 ----------------------------
1289 -- Replace_Type_Reference --
1290 ----------------------------
1292 procedure Replace_Type_Reference (N : Node_Id) is
1293 begin
1294 Rewrite (N, New_Occurrence_Of (Param_Id, Sloc (N)));
1295 end Replace_Type_Reference;
1297 procedure Replace_Type_References is
1298 new Replace_Type_References_Generic (Replace_Type_Reference);
1300 -- Local variables
1302 Loc : constant Source_Ptr := Sloc (Typ);
1303 Prag : constant Node_Id :=
1304 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1305 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1306 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Proc_Id);
1307 Body_Decl : Node_Id;
1308 Expr : Node_Id;
1309 Stmt : Node_Id;
1311 -- Start of processing for Build_Default_Init_Cond_Procedure_Body
1313 begin
1314 -- The procedure should be generated only for [sub]types subject to
1315 -- pragma Default_Initial_Condition. Types that inherit the pragma do
1316 -- not get this specialized procedure.
1318 pragma Assert (Has_Default_Init_Cond (Typ));
1319 pragma Assert (Present (Prag));
1320 pragma Assert (Present (Proc_Id));
1322 -- Nothing to do if the body was already built
1324 if Present (Corresponding_Body (Spec_Decl)) then
1325 return;
1326 end if;
1328 Param_Id := First_Formal (Proc_Id);
1330 -- The pragma has an argument. Note that the argument is analyzed
1331 -- after all references to the current instance of the type are
1332 -- replaced.
1334 if Present (Pragma_Argument_Associations (Prag)) then
1335 Expr :=
1336 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
1338 if Nkind (Expr) = N_Null then
1339 Stmt := Make_Null_Statement (Loc);
1341 -- Preserve the original argument of the pragma by replicating it.
1342 -- Replace all references to the current instance of the type with
1343 -- references to the formal parameter.
1345 else
1346 Expr := New_Copy_Tree (Expr);
1347 Replace_Type_References (Expr, Typ);
1349 -- Generate:
1350 -- pragma Check (Default_Initial_Condition, <Expr>);
1352 Stmt :=
1353 Make_Pragma (Loc,
1354 Pragma_Identifier =>
1355 Make_Identifier (Loc, Name_Check),
1357 Pragma_Argument_Associations => New_List (
1358 Make_Pragma_Argument_Association (Loc,
1359 Expression =>
1360 Make_Identifier (Loc,
1361 Chars => Name_Default_Initial_Condition)),
1362 Make_Pragma_Argument_Association (Loc,
1363 Expression => Expr)));
1364 end if;
1366 -- Otherwise the pragma appears without an argument
1368 else
1369 Stmt := Make_Null_Statement (Loc);
1370 end if;
1372 -- Generate:
1373 -- procedure <Typ>Default_Init_Cond (I : <Typ>) is
1374 -- begin
1375 -- <Stmt>;
1376 -- end <Typ>Default_Init_Cond;
1378 Body_Decl :=
1379 Make_Subprogram_Body (Loc,
1380 Specification =>
1381 Copy_Separate_Tree (Specification (Spec_Decl)),
1382 Declarations => Empty_List,
1383 Handled_Statement_Sequence =>
1384 Make_Handled_Sequence_Of_Statements (Loc,
1385 Statements => New_List (Stmt)));
1387 -- Link the spec and body of the default initial condition procedure
1388 -- to prevent the generation of a duplicate body.
1390 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
1391 Set_Corresponding_Spec (Body_Decl, Proc_Id);
1393 Insert_After_And_Analyze (Declaration_Node (Typ), Body_Decl);
1394 end Build_Default_Init_Cond_Procedure_Body;
1396 -- Local variables
1398 Decl : Node_Id;
1399 Typ : Entity_Id;
1401 -- Start of processing for Build_Default_Init_Cond_Procedure_Bodies
1403 begin
1404 -- Inspect the private declarations looking for [sub]type declarations
1406 Decl := First (Priv_Decls);
1407 while Present (Decl) loop
1408 if Nkind_In (Decl, N_Full_Type_Declaration,
1409 N_Subtype_Declaration)
1410 then
1411 Typ := Defining_Entity (Decl);
1413 -- Guard against partially decorate types due to previous errors
1415 if Is_Type (Typ) then
1417 -- If the type is subject to pragma Default_Initial_Condition,
1418 -- generate the body of the internal procedure which verifies
1419 -- the assertion of the pragma at run time.
1421 if Has_Default_Init_Cond (Typ) then
1422 Build_Default_Init_Cond_Procedure_Body (Typ);
1424 -- A derived type inherits the default initial condition
1425 -- procedure from its parent type.
1427 elsif Has_Inherited_Default_Init_Cond (Typ) then
1428 Inherit_Default_Init_Cond_Procedure (Typ);
1429 end if;
1430 end if;
1431 end if;
1433 Next (Decl);
1434 end loop;
1435 end Build_Default_Init_Cond_Procedure_Bodies;
1437 ---------------------------------------------------
1438 -- Build_Default_Init_Cond_Procedure_Declaration --
1439 ---------------------------------------------------
1441 procedure Build_Default_Init_Cond_Procedure_Declaration (Typ : Entity_Id) is
1442 Loc : constant Source_Ptr := Sloc (Typ);
1443 Prag : constant Node_Id :=
1444 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1445 Proc_Id : Entity_Id;
1447 begin
1448 -- The procedure should be generated only for types subject to pragma
1449 -- Default_Initial_Condition. Types that inherit the pragma do not get
1450 -- this specialized procedure.
1452 pragma Assert (Has_Default_Init_Cond (Typ));
1453 pragma Assert (Present (Prag));
1455 -- Nothing to do if default initial condition procedure already built
1457 if Present (Default_Init_Cond_Procedure (Typ)) then
1458 return;
1459 end if;
1461 Proc_Id :=
1462 Make_Defining_Identifier (Loc,
1463 Chars => New_External_Name (Chars (Typ), "Default_Init_Cond"));
1465 -- Associate default initial condition procedure with the private type
1467 Set_Ekind (Proc_Id, E_Procedure);
1468 Set_Is_Default_Init_Cond_Procedure (Proc_Id);
1469 Set_Default_Init_Cond_Procedure (Typ, Proc_Id);
1471 -- Generate:
1472 -- procedure <Typ>Default_Init_Cond (Inn : <Typ>);
1474 Insert_After_And_Analyze (Prag,
1475 Make_Subprogram_Declaration (Loc,
1476 Specification =>
1477 Make_Procedure_Specification (Loc,
1478 Defining_Unit_Name => Proc_Id,
1479 Parameter_Specifications => New_List (
1480 Make_Parameter_Specification (Loc,
1481 Defining_Identifier => Make_Temporary (Loc, 'I'),
1482 Parameter_Type => New_Occurrence_Of (Typ, Loc))))));
1483 end Build_Default_Init_Cond_Procedure_Declaration;
1485 ---------------------------
1486 -- Build_Default_Subtype --
1487 ---------------------------
1489 function Build_Default_Subtype
1490 (T : Entity_Id;
1491 N : Node_Id) return Entity_Id
1493 Loc : constant Source_Ptr := Sloc (N);
1494 Disc : Entity_Id;
1496 Bas : Entity_Id;
1497 -- The base type that is to be constrained by the defaults
1499 begin
1500 if not Has_Discriminants (T) or else Is_Constrained (T) then
1501 return T;
1502 end if;
1504 Bas := Base_Type (T);
1506 -- If T is non-private but its base type is private, this is the
1507 -- completion of a subtype declaration whose parent type is private
1508 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1509 -- are to be found in the full view of the base. Check that the private
1510 -- status of T and its base differ.
1512 if Is_Private_Type (Bas)
1513 and then not Is_Private_Type (T)
1514 and then Present (Full_View (Bas))
1515 then
1516 Bas := Full_View (Bas);
1517 end if;
1519 Disc := First_Discriminant (T);
1521 if No (Discriminant_Default_Value (Disc)) then
1522 return T;
1523 end if;
1525 declare
1526 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1527 Constraints : constant List_Id := New_List;
1528 Decl : Node_Id;
1530 begin
1531 while Present (Disc) loop
1532 Append_To (Constraints,
1533 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1534 Next_Discriminant (Disc);
1535 end loop;
1537 Decl :=
1538 Make_Subtype_Declaration (Loc,
1539 Defining_Identifier => Act,
1540 Subtype_Indication =>
1541 Make_Subtype_Indication (Loc,
1542 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1543 Constraint =>
1544 Make_Index_Or_Discriminant_Constraint (Loc,
1545 Constraints => Constraints)));
1547 Insert_Action (N, Decl);
1548 Analyze (Decl);
1549 return Act;
1550 end;
1551 end Build_Default_Subtype;
1553 --------------------------------------------
1554 -- Build_Discriminal_Subtype_Of_Component --
1555 --------------------------------------------
1557 function Build_Discriminal_Subtype_Of_Component
1558 (T : Entity_Id) return Node_Id
1560 Loc : constant Source_Ptr := Sloc (T);
1561 D : Elmt_Id;
1562 Id : Node_Id;
1564 function Build_Discriminal_Array_Constraint return List_Id;
1565 -- If one or more of the bounds of the component depends on
1566 -- discriminants, build actual constraint using the discriminants
1567 -- of the prefix.
1569 function Build_Discriminal_Record_Constraint return List_Id;
1570 -- Similar to previous one, for discriminated components constrained by
1571 -- the discriminant of the enclosing object.
1573 ----------------------------------------
1574 -- Build_Discriminal_Array_Constraint --
1575 ----------------------------------------
1577 function Build_Discriminal_Array_Constraint return List_Id is
1578 Constraints : constant List_Id := New_List;
1579 Indx : Node_Id;
1580 Hi : Node_Id;
1581 Lo : Node_Id;
1582 Old_Hi : Node_Id;
1583 Old_Lo : Node_Id;
1585 begin
1586 Indx := First_Index (T);
1587 while Present (Indx) loop
1588 Old_Lo := Type_Low_Bound (Etype (Indx));
1589 Old_Hi := Type_High_Bound (Etype (Indx));
1591 if Denotes_Discriminant (Old_Lo) then
1592 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1594 else
1595 Lo := New_Copy_Tree (Old_Lo);
1596 end if;
1598 if Denotes_Discriminant (Old_Hi) then
1599 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1601 else
1602 Hi := New_Copy_Tree (Old_Hi);
1603 end if;
1605 Append (Make_Range (Loc, Lo, Hi), Constraints);
1606 Next_Index (Indx);
1607 end loop;
1609 return Constraints;
1610 end Build_Discriminal_Array_Constraint;
1612 -----------------------------------------
1613 -- Build_Discriminal_Record_Constraint --
1614 -----------------------------------------
1616 function Build_Discriminal_Record_Constraint return List_Id is
1617 Constraints : constant List_Id := New_List;
1618 D : Elmt_Id;
1619 D_Val : Node_Id;
1621 begin
1622 D := First_Elmt (Discriminant_Constraint (T));
1623 while Present (D) loop
1624 if Denotes_Discriminant (Node (D)) then
1625 D_Val :=
1626 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1627 else
1628 D_Val := New_Copy_Tree (Node (D));
1629 end if;
1631 Append (D_Val, Constraints);
1632 Next_Elmt (D);
1633 end loop;
1635 return Constraints;
1636 end Build_Discriminal_Record_Constraint;
1638 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1640 begin
1641 if Ekind (T) = E_Array_Subtype then
1642 Id := First_Index (T);
1643 while Present (Id) loop
1644 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1645 or else
1646 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1647 then
1648 return Build_Component_Subtype
1649 (Build_Discriminal_Array_Constraint, Loc, T);
1650 end if;
1652 Next_Index (Id);
1653 end loop;
1655 elsif Ekind (T) = E_Record_Subtype
1656 and then Has_Discriminants (T)
1657 and then not Has_Unknown_Discriminants (T)
1658 then
1659 D := First_Elmt (Discriminant_Constraint (T));
1660 while Present (D) loop
1661 if Denotes_Discriminant (Node (D)) then
1662 return Build_Component_Subtype
1663 (Build_Discriminal_Record_Constraint, Loc, T);
1664 end if;
1666 Next_Elmt (D);
1667 end loop;
1668 end if;
1670 -- If none of the above, the actual and nominal subtypes are the same
1672 return Empty;
1673 end Build_Discriminal_Subtype_Of_Component;
1675 ------------------------------
1676 -- Build_Elaboration_Entity --
1677 ------------------------------
1679 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1680 Loc : constant Source_Ptr := Sloc (N);
1681 Decl : Node_Id;
1682 Elab_Ent : Entity_Id;
1684 procedure Set_Package_Name (Ent : Entity_Id);
1685 -- Given an entity, sets the fully qualified name of the entity in
1686 -- Name_Buffer, with components separated by double underscores. This
1687 -- is a recursive routine that climbs the scope chain to Standard.
1689 ----------------------
1690 -- Set_Package_Name --
1691 ----------------------
1693 procedure Set_Package_Name (Ent : Entity_Id) is
1694 begin
1695 if Scope (Ent) /= Standard_Standard then
1696 Set_Package_Name (Scope (Ent));
1698 declare
1699 Nam : constant String := Get_Name_String (Chars (Ent));
1700 begin
1701 Name_Buffer (Name_Len + 1) := '_';
1702 Name_Buffer (Name_Len + 2) := '_';
1703 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1704 Name_Len := Name_Len + Nam'Length + 2;
1705 end;
1707 else
1708 Get_Name_String (Chars (Ent));
1709 end if;
1710 end Set_Package_Name;
1712 -- Start of processing for Build_Elaboration_Entity
1714 begin
1715 -- Ignore call if already constructed
1717 if Present (Elaboration_Entity (Spec_Id)) then
1718 return;
1720 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1721 -- no role in analysis.
1723 elsif ASIS_Mode then
1724 return;
1726 -- See if we need elaboration entity. We always need it for the dynamic
1727 -- elaboration model, since it is needed to properly generate the PE
1728 -- exception for access before elaboration.
1730 elsif Dynamic_Elaboration_Checks then
1731 null;
1733 -- For the static model, we don't need the elaboration counter if this
1734 -- unit is sure to have no elaboration code, since that means there
1735 -- is no elaboration unit to be called. Note that we can't just decide
1736 -- after the fact by looking to see whether there was elaboration code,
1737 -- because that's too late to make this decision.
1739 elsif Restriction_Active (No_Elaboration_Code) then
1740 return;
1742 -- Similarly, for the static model, we can skip the elaboration counter
1743 -- if we have the No_Multiple_Elaboration restriction, since for the
1744 -- static model, that's the only purpose of the counter (to avoid
1745 -- multiple elaboration).
1747 elsif Restriction_Active (No_Multiple_Elaboration) then
1748 return;
1749 end if;
1751 -- Here we need the elaboration entity
1753 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1754 -- name with dots replaced by double underscore. We have to manually
1755 -- construct this name, since it will be elaborated in the outer scope,
1756 -- and thus will not have the unit name automatically prepended.
1758 Set_Package_Name (Spec_Id);
1759 Add_Str_To_Name_Buffer ("_E");
1761 -- Create elaboration counter
1763 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1764 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1766 Decl :=
1767 Make_Object_Declaration (Loc,
1768 Defining_Identifier => Elab_Ent,
1769 Object_Definition =>
1770 New_Occurrence_Of (Standard_Short_Integer, Loc),
1771 Expression => Make_Integer_Literal (Loc, Uint_0));
1773 Push_Scope (Standard_Standard);
1774 Add_Global_Declaration (Decl);
1775 Pop_Scope;
1777 -- Reset True_Constant indication, since we will indeed assign a value
1778 -- to the variable in the binder main. We also kill the Current_Value
1779 -- and Last_Assignment fields for the same reason.
1781 Set_Is_True_Constant (Elab_Ent, False);
1782 Set_Current_Value (Elab_Ent, Empty);
1783 Set_Last_Assignment (Elab_Ent, Empty);
1785 -- We do not want any further qualification of the name (if we did not
1786 -- do this, we would pick up the name of the generic package in the case
1787 -- of a library level generic instantiation).
1789 Set_Has_Qualified_Name (Elab_Ent);
1790 Set_Has_Fully_Qualified_Name (Elab_Ent);
1791 end Build_Elaboration_Entity;
1793 --------------------------------
1794 -- Build_Explicit_Dereference --
1795 --------------------------------
1797 procedure Build_Explicit_Dereference
1798 (Expr : Node_Id;
1799 Disc : Entity_Id)
1801 Loc : constant Source_Ptr := Sloc (Expr);
1803 begin
1804 -- An entity of a type with a reference aspect is overloaded with
1805 -- both interpretations: with and without the dereference. Now that
1806 -- the dereference is made explicit, set the type of the node properly,
1807 -- to prevent anomalies in the backend. Same if the expression is an
1808 -- overloaded function call whose return type has a reference aspect.
1810 if Is_Entity_Name (Expr) then
1811 Set_Etype (Expr, Etype (Entity (Expr)));
1813 elsif Nkind (Expr) = N_Function_Call then
1814 Set_Etype (Expr, Etype (Name (Expr)));
1815 end if;
1817 Set_Is_Overloaded (Expr, False);
1819 -- The expression will often be a generalized indexing that yields a
1820 -- container element that is then dereferenced, in which case the
1821 -- generalized indexing call is also non-overloaded.
1823 if Nkind (Expr) = N_Indexed_Component
1824 and then Present (Generalized_Indexing (Expr))
1825 then
1826 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1827 end if;
1829 Rewrite (Expr,
1830 Make_Explicit_Dereference (Loc,
1831 Prefix =>
1832 Make_Selected_Component (Loc,
1833 Prefix => Relocate_Node (Expr),
1834 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1835 Set_Etype (Prefix (Expr), Etype (Disc));
1836 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1837 end Build_Explicit_Dereference;
1839 -----------------------------------
1840 -- Cannot_Raise_Constraint_Error --
1841 -----------------------------------
1843 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1844 begin
1845 if Compile_Time_Known_Value (Expr) then
1846 return True;
1848 elsif Do_Range_Check (Expr) then
1849 return False;
1851 elsif Raises_Constraint_Error (Expr) then
1852 return False;
1854 else
1855 case Nkind (Expr) is
1856 when N_Identifier =>
1857 return True;
1859 when N_Expanded_Name =>
1860 return True;
1862 when N_Selected_Component =>
1863 return not Do_Discriminant_Check (Expr);
1865 when N_Attribute_Reference =>
1866 if Do_Overflow_Check (Expr) then
1867 return False;
1869 elsif No (Expressions (Expr)) then
1870 return True;
1872 else
1873 declare
1874 N : Node_Id;
1876 begin
1877 N := First (Expressions (Expr));
1878 while Present (N) loop
1879 if Cannot_Raise_Constraint_Error (N) then
1880 Next (N);
1881 else
1882 return False;
1883 end if;
1884 end loop;
1886 return True;
1887 end;
1888 end if;
1890 when N_Type_Conversion =>
1891 if Do_Overflow_Check (Expr)
1892 or else Do_Length_Check (Expr)
1893 or else Do_Tag_Check (Expr)
1894 then
1895 return False;
1896 else
1897 return Cannot_Raise_Constraint_Error (Expression (Expr));
1898 end if;
1900 when N_Unchecked_Type_Conversion =>
1901 return Cannot_Raise_Constraint_Error (Expression (Expr));
1903 when N_Unary_Op =>
1904 if Do_Overflow_Check (Expr) then
1905 return False;
1906 else
1907 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1908 end if;
1910 when N_Op_Divide |
1911 N_Op_Mod |
1912 N_Op_Rem
1914 if Do_Division_Check (Expr)
1915 or else
1916 Do_Overflow_Check (Expr)
1917 then
1918 return False;
1919 else
1920 return
1921 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1922 and then
1923 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1924 end if;
1926 when N_Op_Add |
1927 N_Op_And |
1928 N_Op_Concat |
1929 N_Op_Eq |
1930 N_Op_Expon |
1931 N_Op_Ge |
1932 N_Op_Gt |
1933 N_Op_Le |
1934 N_Op_Lt |
1935 N_Op_Multiply |
1936 N_Op_Ne |
1937 N_Op_Or |
1938 N_Op_Rotate_Left |
1939 N_Op_Rotate_Right |
1940 N_Op_Shift_Left |
1941 N_Op_Shift_Right |
1942 N_Op_Shift_Right_Arithmetic |
1943 N_Op_Subtract |
1944 N_Op_Xor
1946 if Do_Overflow_Check (Expr) then
1947 return False;
1948 else
1949 return
1950 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1951 and then
1952 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1953 end if;
1955 when others =>
1956 return False;
1957 end case;
1958 end if;
1959 end Cannot_Raise_Constraint_Error;
1961 -----------------------------------------
1962 -- Check_Dynamically_Tagged_Expression --
1963 -----------------------------------------
1965 procedure Check_Dynamically_Tagged_Expression
1966 (Expr : Node_Id;
1967 Typ : Entity_Id;
1968 Related_Nod : Node_Id)
1970 begin
1971 pragma Assert (Is_Tagged_Type (Typ));
1973 -- In order to avoid spurious errors when analyzing the expanded code,
1974 -- this check is done only for nodes that come from source and for
1975 -- actuals of generic instantiations.
1977 if (Comes_From_Source (Related_Nod)
1978 or else In_Generic_Actual (Expr))
1979 and then (Is_Class_Wide_Type (Etype (Expr))
1980 or else Is_Dynamically_Tagged (Expr))
1981 and then Is_Tagged_Type (Typ)
1982 and then not Is_Class_Wide_Type (Typ)
1983 then
1984 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
1985 end if;
1986 end Check_Dynamically_Tagged_Expression;
1988 --------------------------
1989 -- Check_Fully_Declared --
1990 --------------------------
1992 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
1993 begin
1994 if Ekind (T) = E_Incomplete_Type then
1996 -- Ada 2005 (AI-50217): If the type is available through a limited
1997 -- with_clause, verify that its full view has been analyzed.
1999 if From_Limited_With (T)
2000 and then Present (Non_Limited_View (T))
2001 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2002 then
2003 -- The non-limited view is fully declared
2005 null;
2007 else
2008 Error_Msg_NE
2009 ("premature usage of incomplete}", N, First_Subtype (T));
2010 end if;
2012 -- Need comments for these tests ???
2014 elsif Has_Private_Component (T)
2015 and then not Is_Generic_Type (Root_Type (T))
2016 and then not In_Spec_Expression
2017 then
2018 -- Special case: if T is the anonymous type created for a single
2019 -- task or protected object, use the name of the source object.
2021 if Is_Concurrent_Type (T)
2022 and then not Comes_From_Source (T)
2023 and then Nkind (N) = N_Object_Declaration
2024 then
2025 Error_Msg_NE
2026 ("type of& has incomplete component",
2027 N, Defining_Identifier (N));
2028 else
2029 Error_Msg_NE
2030 ("premature usage of incomplete}",
2031 N, First_Subtype (T));
2032 end if;
2033 end if;
2034 end Check_Fully_Declared;
2036 -------------------------------------
2037 -- Check_Function_Writable_Actuals --
2038 -------------------------------------
2040 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2041 Writable_Actuals_List : Elist_Id := No_Elist;
2042 Identifiers_List : Elist_Id := No_Elist;
2043 Error_Node : Node_Id := Empty;
2045 procedure Collect_Identifiers (N : Node_Id);
2046 -- In a single traversal of subtree N collect in Writable_Actuals_List
2047 -- all the actuals of functions with writable actuals, and in the list
2048 -- Identifiers_List collect all the identifiers that are not actuals of
2049 -- functions with writable actuals. If a writable actual is referenced
2050 -- twice as writable actual then Error_Node is set to reference its
2051 -- second occurrence, the error is reported, and the tree traversal
2052 -- is abandoned.
2054 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2055 -- Return the entity associated with the function call
2057 procedure Preanalyze_Without_Errors (N : Node_Id);
2058 -- Preanalyze N without reporting errors. Very dubious, you can't just
2059 -- go analyzing things more than once???
2061 -------------------------
2062 -- Collect_Identifiers --
2063 -------------------------
2065 procedure Collect_Identifiers (N : Node_Id) is
2067 function Check_Node (N : Node_Id) return Traverse_Result;
2068 -- Process a single node during the tree traversal to collect the
2069 -- writable actuals of functions and all the identifiers which are
2070 -- not writable actuals of functions.
2072 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2073 -- Returns True if List has a node whose Entity is Entity (N)
2075 -------------------------
2076 -- Check_Function_Call --
2077 -------------------------
2079 function Check_Node (N : Node_Id) return Traverse_Result is
2080 Is_Writable_Actual : Boolean := False;
2081 Id : Entity_Id;
2083 begin
2084 if Nkind (N) = N_Identifier then
2086 -- No analysis possible if the entity is not decorated
2088 if No (Entity (N)) then
2089 return Skip;
2091 -- Don't collect identifiers of packages, called functions, etc
2093 elsif Ekind_In (Entity (N), E_Package,
2094 E_Function,
2095 E_Procedure,
2096 E_Entry)
2097 then
2098 return Skip;
2100 -- Analyze if N is a writable actual of a function
2102 elsif Nkind (Parent (N)) = N_Function_Call then
2103 declare
2104 Call : constant Node_Id := Parent (N);
2105 Actual : Node_Id;
2106 Formal : Node_Id;
2108 begin
2109 Id := Get_Function_Id (Call);
2111 -- In case of previous error, no check is possible
2113 if No (Id) then
2114 return Abandon;
2115 end if;
2117 Formal := First_Formal (Id);
2118 Actual := First_Actual (Call);
2119 while Present (Actual) and then Present (Formal) loop
2120 if Actual = N then
2121 if Ekind_In (Formal, E_Out_Parameter,
2122 E_In_Out_Parameter)
2123 then
2124 Is_Writable_Actual := True;
2125 end if;
2127 exit;
2128 end if;
2130 Next_Formal (Formal);
2131 Next_Actual (Actual);
2132 end loop;
2133 end;
2134 end if;
2136 if Is_Writable_Actual then
2137 if Contains (Writable_Actuals_List, N) then
2138 Error_Msg_NE
2139 ("value may be affected by call to& "
2140 & "because order of evaluation is arbitrary", N, Id);
2141 Error_Node := N;
2142 return Abandon;
2143 end if;
2145 Append_New_Elmt (N, To => Writable_Actuals_List);
2147 else
2148 if Identifiers_List = No_Elist then
2149 Identifiers_List := New_Elmt_List;
2150 end if;
2152 Append_Unique_Elmt (N, Identifiers_List);
2153 end if;
2154 end if;
2156 return OK;
2157 end Check_Node;
2159 --------------
2160 -- Contains --
2161 --------------
2163 function Contains
2164 (List : Elist_Id;
2165 N : Node_Id) return Boolean
2167 pragma Assert (Nkind (N) in N_Has_Entity);
2169 Elmt : Elmt_Id;
2171 begin
2172 if List = No_Elist then
2173 return False;
2174 end if;
2176 Elmt := First_Elmt (List);
2177 while Present (Elmt) loop
2178 if Entity (Node (Elmt)) = Entity (N) then
2179 return True;
2180 else
2181 Next_Elmt (Elmt);
2182 end if;
2183 end loop;
2185 return False;
2186 end Contains;
2188 ------------------
2189 -- Do_Traversal --
2190 ------------------
2192 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2193 -- The traversal procedure
2195 -- Start of processing for Collect_Identifiers
2197 begin
2198 if Present (Error_Node) then
2199 return;
2200 end if;
2202 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2203 return;
2204 end if;
2206 Do_Traversal (N);
2207 end Collect_Identifiers;
2209 ---------------------
2210 -- Get_Function_Id --
2211 ---------------------
2213 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2214 Nam : constant Node_Id := Name (Call);
2215 Id : Entity_Id;
2217 begin
2218 if Nkind (Nam) = N_Explicit_Dereference then
2219 Id := Etype (Nam);
2220 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2222 elsif Nkind (Nam) = N_Selected_Component then
2223 Id := Entity (Selector_Name (Nam));
2225 elsif Nkind (Nam) = N_Indexed_Component then
2226 Id := Entity (Selector_Name (Prefix (Nam)));
2228 else
2229 Id := Entity (Nam);
2230 end if;
2232 return Id;
2233 end Get_Function_Id;
2235 ---------------------------
2236 -- Preanalyze_Expression --
2237 ---------------------------
2239 procedure Preanalyze_Without_Errors (N : Node_Id) is
2240 Status : constant Boolean := Get_Ignore_Errors;
2241 begin
2242 Set_Ignore_Errors (True);
2243 Preanalyze (N);
2244 Set_Ignore_Errors (Status);
2245 end Preanalyze_Without_Errors;
2247 -- Start of processing for Check_Function_Writable_Actuals
2249 begin
2250 -- The check only applies to Ada 2012 code, and only to constructs that
2251 -- have multiple constituents whose order of evaluation is not specified
2252 -- by the language.
2254 if Ada_Version < Ada_2012
2255 or else (not (Nkind (N) in N_Op)
2256 and then not (Nkind (N) in N_Membership_Test)
2257 and then not Nkind_In (N, N_Range,
2258 N_Aggregate,
2259 N_Extension_Aggregate,
2260 N_Full_Type_Declaration,
2261 N_Function_Call,
2262 N_Procedure_Call_Statement,
2263 N_Entry_Call_Statement))
2264 or else (Nkind (N) = N_Full_Type_Declaration
2265 and then not Is_Record_Type (Defining_Identifier (N)))
2267 -- In addition, this check only applies to source code, not to code
2268 -- generated by constraint checks.
2270 or else not Comes_From_Source (N)
2271 then
2272 return;
2273 end if;
2275 -- If a construct C has two or more direct constituents that are names
2276 -- or expressions whose evaluation may occur in an arbitrary order, at
2277 -- least one of which contains a function call with an in out or out
2278 -- parameter, then the construct is legal only if: for each name N that
2279 -- is passed as a parameter of mode in out or out to some inner function
2280 -- call C2 (not including the construct C itself), there is no other
2281 -- name anywhere within a direct constituent of the construct C other
2282 -- than the one containing C2, that is known to refer to the same
2283 -- object (RM 6.4.1(6.17/3)).
2285 case Nkind (N) is
2286 when N_Range =>
2287 Collect_Identifiers (Low_Bound (N));
2288 Collect_Identifiers (High_Bound (N));
2290 when N_Op | N_Membership_Test =>
2291 declare
2292 Expr : Node_Id;
2294 begin
2295 Collect_Identifiers (Left_Opnd (N));
2297 if Present (Right_Opnd (N)) then
2298 Collect_Identifiers (Right_Opnd (N));
2299 end if;
2301 if Nkind_In (N, N_In, N_Not_In)
2302 and then Present (Alternatives (N))
2303 then
2304 Expr := First (Alternatives (N));
2305 while Present (Expr) loop
2306 Collect_Identifiers (Expr);
2308 Next (Expr);
2309 end loop;
2310 end if;
2311 end;
2313 when N_Full_Type_Declaration =>
2314 declare
2315 function Get_Record_Part (N : Node_Id) return Node_Id;
2316 -- Return the record part of this record type definition
2318 function Get_Record_Part (N : Node_Id) return Node_Id is
2319 Type_Def : constant Node_Id := Type_Definition (N);
2320 begin
2321 if Nkind (Type_Def) = N_Derived_Type_Definition then
2322 return Record_Extension_Part (Type_Def);
2323 else
2324 return Type_Def;
2325 end if;
2326 end Get_Record_Part;
2328 Comp : Node_Id;
2329 Def_Id : Entity_Id := Defining_Identifier (N);
2330 Rec : Node_Id := Get_Record_Part (N);
2332 begin
2333 -- No need to perform any analysis if the record has no
2334 -- components
2336 if No (Rec) or else No (Component_List (Rec)) then
2337 return;
2338 end if;
2340 -- Collect the identifiers starting from the deepest
2341 -- derivation. Done to report the error in the deepest
2342 -- derivation.
2344 loop
2345 if Present (Component_List (Rec)) then
2346 Comp := First (Component_Items (Component_List (Rec)));
2347 while Present (Comp) loop
2348 if Nkind (Comp) = N_Component_Declaration
2349 and then Present (Expression (Comp))
2350 then
2351 Collect_Identifiers (Expression (Comp));
2352 end if;
2354 Next (Comp);
2355 end loop;
2356 end if;
2358 exit when No (Underlying_Type (Etype (Def_Id)))
2359 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2360 = Def_Id;
2362 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2363 Rec := Get_Record_Part (Parent (Def_Id));
2364 end loop;
2365 end;
2367 when N_Subprogram_Call |
2368 N_Entry_Call_Statement =>
2369 declare
2370 Id : constant Entity_Id := Get_Function_Id (N);
2371 Formal : Node_Id;
2372 Actual : Node_Id;
2374 begin
2375 Formal := First_Formal (Id);
2376 Actual := First_Actual (N);
2377 while Present (Actual) and then Present (Formal) loop
2378 if Ekind_In (Formal, E_Out_Parameter,
2379 E_In_Out_Parameter)
2380 then
2381 Collect_Identifiers (Actual);
2382 end if;
2384 Next_Formal (Formal);
2385 Next_Actual (Actual);
2386 end loop;
2387 end;
2389 when N_Aggregate |
2390 N_Extension_Aggregate =>
2391 declare
2392 Assoc : Node_Id;
2393 Choice : Node_Id;
2394 Comp_Expr : Node_Id;
2396 begin
2397 -- Handle the N_Others_Choice of array aggregates with static
2398 -- bounds. There is no need to perform this analysis in
2399 -- aggregates without static bounds since we cannot evaluate
2400 -- if the N_Others_Choice covers several elements. There is
2401 -- no need to handle the N_Others choice of record aggregates
2402 -- since at this stage it has been already expanded by
2403 -- Resolve_Record_Aggregate.
2405 if Is_Array_Type (Etype (N))
2406 and then Nkind (N) = N_Aggregate
2407 and then Present (Aggregate_Bounds (N))
2408 and then Compile_Time_Known_Bounds (Etype (N))
2409 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2411 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2412 then
2413 declare
2414 Count_Components : Uint := Uint_0;
2415 Num_Components : Uint;
2416 Others_Assoc : Node_Id;
2417 Others_Choice : Node_Id := Empty;
2418 Others_Box_Present : Boolean := False;
2420 begin
2421 -- Count positional associations
2423 if Present (Expressions (N)) then
2424 Comp_Expr := First (Expressions (N));
2425 while Present (Comp_Expr) loop
2426 Count_Components := Count_Components + 1;
2427 Next (Comp_Expr);
2428 end loop;
2429 end if;
2431 -- Count the rest of elements and locate the N_Others
2432 -- choice (if any)
2434 Assoc := First (Component_Associations (N));
2435 while Present (Assoc) loop
2436 Choice := First (Choices (Assoc));
2437 while Present (Choice) loop
2438 if Nkind (Choice) = N_Others_Choice then
2439 Others_Assoc := Assoc;
2440 Others_Choice := Choice;
2441 Others_Box_Present := Box_Present (Assoc);
2443 -- Count several components
2445 elsif Nkind_In (Choice, N_Range,
2446 N_Subtype_Indication)
2447 or else (Is_Entity_Name (Choice)
2448 and then Is_Type (Entity (Choice)))
2449 then
2450 declare
2451 L, H : Node_Id;
2452 begin
2453 Get_Index_Bounds (Choice, L, H);
2454 pragma Assert
2455 (Compile_Time_Known_Value (L)
2456 and then Compile_Time_Known_Value (H));
2457 Count_Components :=
2458 Count_Components
2459 + Expr_Value (H) - Expr_Value (L) + 1;
2460 end;
2462 -- Count single component. No other case available
2463 -- since we are handling an aggregate with static
2464 -- bounds.
2466 else
2467 pragma Assert (Is_OK_Static_Expression (Choice)
2468 or else Nkind (Choice) = N_Identifier
2469 or else Nkind (Choice) = N_Integer_Literal);
2471 Count_Components := Count_Components + 1;
2472 end if;
2474 Next (Choice);
2475 end loop;
2477 Next (Assoc);
2478 end loop;
2480 Num_Components :=
2481 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2482 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2484 pragma Assert (Count_Components <= Num_Components);
2486 -- Handle the N_Others choice if it covers several
2487 -- components
2489 if Present (Others_Choice)
2490 and then (Num_Components - Count_Components) > 1
2491 then
2492 if not Others_Box_Present then
2494 -- At this stage, if expansion is active, the
2495 -- expression of the others choice has not been
2496 -- analyzed. Hence we generate a duplicate and
2497 -- we analyze it silently to have available the
2498 -- minimum decoration required to collect the
2499 -- identifiers.
2501 if not Expander_Active then
2502 Comp_Expr := Expression (Others_Assoc);
2503 else
2504 Comp_Expr :=
2505 New_Copy_Tree (Expression (Others_Assoc));
2506 Preanalyze_Without_Errors (Comp_Expr);
2507 end if;
2509 Collect_Identifiers (Comp_Expr);
2511 if Writable_Actuals_List /= No_Elist then
2513 -- As suggested by Robert, at current stage we
2514 -- report occurrences of this case as warnings.
2516 Error_Msg_N
2517 ("writable function parameter may affect "
2518 & "value in other component because order "
2519 & "of evaluation is unspecified??",
2520 Node (First_Elmt (Writable_Actuals_List)));
2521 end if;
2522 end if;
2523 end if;
2524 end;
2525 end if;
2527 -- Handle ancestor part of extension aggregates
2529 if Nkind (N) = N_Extension_Aggregate then
2530 Collect_Identifiers (Ancestor_Part (N));
2531 end if;
2533 -- Handle positional associations
2535 if Present (Expressions (N)) then
2536 Comp_Expr := First (Expressions (N));
2537 while Present (Comp_Expr) loop
2538 if not Is_OK_Static_Expression (Comp_Expr) then
2539 Collect_Identifiers (Comp_Expr);
2540 end if;
2542 Next (Comp_Expr);
2543 end loop;
2544 end if;
2546 -- Handle discrete associations
2548 if Present (Component_Associations (N)) then
2549 Assoc := First (Component_Associations (N));
2550 while Present (Assoc) loop
2552 if not Box_Present (Assoc) then
2553 Choice := First (Choices (Assoc));
2554 while Present (Choice) loop
2556 -- For now we skip discriminants since it requires
2557 -- performing the analysis in two phases: first one
2558 -- analyzing discriminants and second one analyzing
2559 -- the rest of components since discriminants are
2560 -- evaluated prior to components: too much extra
2561 -- work to detect a corner case???
2563 if Nkind (Choice) in N_Has_Entity
2564 and then Present (Entity (Choice))
2565 and then Ekind (Entity (Choice)) = E_Discriminant
2566 then
2567 null;
2569 elsif Box_Present (Assoc) then
2570 null;
2572 else
2573 if not Analyzed (Expression (Assoc)) then
2574 Comp_Expr :=
2575 New_Copy_Tree (Expression (Assoc));
2576 Set_Parent (Comp_Expr, Parent (N));
2577 Preanalyze_Without_Errors (Comp_Expr);
2578 else
2579 Comp_Expr := Expression (Assoc);
2580 end if;
2582 Collect_Identifiers (Comp_Expr);
2583 end if;
2585 Next (Choice);
2586 end loop;
2587 end if;
2589 Next (Assoc);
2590 end loop;
2591 end if;
2592 end;
2594 when others =>
2595 return;
2596 end case;
2598 -- No further action needed if we already reported an error
2600 if Present (Error_Node) then
2601 return;
2602 end if;
2604 -- Check if some writable argument of a function is referenced
2606 if Writable_Actuals_List /= No_Elist
2607 and then Identifiers_List /= No_Elist
2608 then
2609 declare
2610 Elmt_1 : Elmt_Id;
2611 Elmt_2 : Elmt_Id;
2613 begin
2614 Elmt_1 := First_Elmt (Writable_Actuals_List);
2615 while Present (Elmt_1) loop
2616 Elmt_2 := First_Elmt (Identifiers_List);
2617 while Present (Elmt_2) loop
2618 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2619 case Nkind (Parent (Node (Elmt_2))) is
2620 when N_Aggregate |
2621 N_Component_Association |
2622 N_Component_Declaration =>
2623 Error_Msg_N
2624 ("value may be affected by call in other "
2625 & "component because they are evaluated "
2626 & "in unspecified order",
2627 Node (Elmt_2));
2629 when N_In | N_Not_In =>
2630 Error_Msg_N
2631 ("value may be affected by call in other "
2632 & "alternative because they are evaluated "
2633 & "in unspecified order",
2634 Node (Elmt_2));
2636 when others =>
2637 Error_Msg_N
2638 ("value of actual may be affected by call in "
2639 & "other actual because they are evaluated "
2640 & "in unspecified order",
2641 Node (Elmt_2));
2642 end case;
2643 end if;
2645 Next_Elmt (Elmt_2);
2646 end loop;
2648 Next_Elmt (Elmt_1);
2649 end loop;
2650 end;
2651 end if;
2652 end Check_Function_Writable_Actuals;
2654 --------------------------------
2655 -- Check_Implicit_Dereference --
2656 --------------------------------
2658 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2659 Disc : Entity_Id;
2660 Desig : Entity_Id;
2661 Nam : Node_Id;
2663 begin
2664 if Nkind (N) = N_Indexed_Component
2665 and then Present (Generalized_Indexing (N))
2666 then
2667 Nam := Generalized_Indexing (N);
2668 else
2669 Nam := N;
2670 end if;
2672 if Ada_Version < Ada_2012
2673 or else not Has_Implicit_Dereference (Base_Type (Typ))
2674 then
2675 return;
2677 elsif not Comes_From_Source (N)
2678 and then Nkind (N) /= N_Indexed_Component
2679 then
2680 return;
2682 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2683 null;
2685 else
2686 Disc := First_Discriminant (Typ);
2687 while Present (Disc) loop
2688 if Has_Implicit_Dereference (Disc) then
2689 Desig := Designated_Type (Etype (Disc));
2690 Add_One_Interp (Nam, Disc, Desig);
2692 -- If the node is a generalized indexing, add interpretation
2693 -- to that node as well, for subsequent resolution.
2695 if Nkind (N) = N_Indexed_Component then
2696 Add_One_Interp (N, Disc, Desig);
2697 end if;
2699 -- If the operation comes from a generic unit and the context
2700 -- is a selected component, the selector name may be global
2701 -- and set in the instance already. Remove the entity to
2702 -- force resolution of the selected component, and the
2703 -- generation of an explicit dereference if needed.
2705 if In_Instance
2706 and then Nkind (Parent (Nam)) = N_Selected_Component
2707 then
2708 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2709 end if;
2711 exit;
2712 end if;
2714 Next_Discriminant (Disc);
2715 end loop;
2716 end if;
2717 end Check_Implicit_Dereference;
2719 ----------------------------------
2720 -- Check_Internal_Protected_Use --
2721 ----------------------------------
2723 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2724 S : Entity_Id;
2725 Prot : Entity_Id;
2727 begin
2728 S := Current_Scope;
2729 while Present (S) loop
2730 if S = Standard_Standard then
2731 return;
2733 elsif Ekind (S) = E_Function
2734 and then Ekind (Scope (S)) = E_Protected_Type
2735 then
2736 Prot := Scope (S);
2737 exit;
2738 end if;
2740 S := Scope (S);
2741 end loop;
2743 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2745 -- An indirect function call (e.g. a callback within a protected
2746 -- function body) is not statically illegal. If the access type is
2747 -- anonymous and is the type of an access parameter, the scope of Nam
2748 -- will be the protected type, but it is not a protected operation.
2750 if Ekind (Nam) = E_Subprogram_Type
2751 and then
2752 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2753 then
2754 null;
2756 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2757 Error_Msg_N
2758 ("within protected function cannot use protected "
2759 & "procedure in renaming or as generic actual", N);
2761 elsif Nkind (N) = N_Attribute_Reference then
2762 Error_Msg_N
2763 ("within protected function cannot take access of "
2764 & " protected procedure", N);
2766 else
2767 Error_Msg_N
2768 ("within protected function, protected object is constant", N);
2769 Error_Msg_N
2770 ("\cannot call operation that may modify it", N);
2771 end if;
2772 end if;
2773 end Check_Internal_Protected_Use;
2775 ---------------------------------------
2776 -- Check_Later_Vs_Basic_Declarations --
2777 ---------------------------------------
2779 procedure Check_Later_Vs_Basic_Declarations
2780 (Decls : List_Id;
2781 During_Parsing : Boolean)
2783 Body_Sloc : Source_Ptr;
2784 Decl : Node_Id;
2786 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2787 -- Return whether Decl is considered as a declarative item.
2788 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2789 -- When During_Parsing is False, the semantics of SPARK is followed.
2791 -------------------------------
2792 -- Is_Later_Declarative_Item --
2793 -------------------------------
2795 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2796 begin
2797 if Nkind (Decl) in N_Later_Decl_Item then
2798 return True;
2800 elsif Nkind (Decl) = N_Pragma then
2801 return True;
2803 elsif During_Parsing then
2804 return False;
2806 -- In SPARK, a package declaration is not considered as a later
2807 -- declarative item.
2809 elsif Nkind (Decl) = N_Package_Declaration then
2810 return False;
2812 -- In SPARK, a renaming is considered as a later declarative item
2814 elsif Nkind (Decl) in N_Renaming_Declaration then
2815 return True;
2817 else
2818 return False;
2819 end if;
2820 end Is_Later_Declarative_Item;
2822 -- Start of Check_Later_Vs_Basic_Declarations
2824 begin
2825 Decl := First (Decls);
2827 -- Loop through sequence of basic declarative items
2829 Outer : while Present (Decl) loop
2830 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
2831 and then Nkind (Decl) not in N_Body_Stub
2832 then
2833 Next (Decl);
2835 -- Once a body is encountered, we only allow later declarative
2836 -- items. The inner loop checks the rest of the list.
2838 else
2839 Body_Sloc := Sloc (Decl);
2841 Inner : while Present (Decl) loop
2842 if not Is_Later_Declarative_Item (Decl) then
2843 if During_Parsing then
2844 if Ada_Version = Ada_83 then
2845 Error_Msg_Sloc := Body_Sloc;
2846 Error_Msg_N
2847 ("(Ada 83) decl cannot appear after body#", Decl);
2848 end if;
2849 else
2850 Error_Msg_Sloc := Body_Sloc;
2851 Check_SPARK_05_Restriction
2852 ("decl cannot appear after body#", Decl);
2853 end if;
2854 end if;
2856 Next (Decl);
2857 end loop Inner;
2858 end if;
2859 end loop Outer;
2860 end Check_Later_Vs_Basic_Declarations;
2862 -------------------------
2863 -- Check_Nested_Access --
2864 -------------------------
2866 procedure Check_Nested_Access (Ent : Entity_Id) is
2867 Scop : constant Entity_Id := Current_Scope;
2868 Current_Subp : Entity_Id;
2869 Enclosing : Entity_Id;
2871 begin
2872 -- Currently only enabled for VM back-ends for efficiency, should we
2873 -- enable it more systematically ???
2875 -- Check for Is_Imported needs commenting below ???
2877 if VM_Target /= No_VM
2878 and then Ekind_In (Ent, E_Variable, E_Constant, E_Loop_Parameter)
2879 and then Scope (Ent) /= Empty
2880 and then not Is_Library_Level_Entity (Ent)
2881 and then not Is_Imported (Ent)
2882 then
2883 if Is_Subprogram (Scop)
2884 or else Is_Generic_Subprogram (Scop)
2885 or else Is_Entry (Scop)
2886 then
2887 Current_Subp := Scop;
2888 else
2889 Current_Subp := Current_Subprogram;
2890 end if;
2892 Enclosing := Enclosing_Subprogram (Ent);
2894 if Enclosing /= Empty and then Enclosing /= Current_Subp then
2895 Set_Has_Up_Level_Access (Ent, True);
2896 end if;
2897 end if;
2898 end Check_Nested_Access;
2900 ---------------------------
2901 -- Check_No_Hidden_State --
2902 ---------------------------
2904 procedure Check_No_Hidden_State (Id : Entity_Id) is
2905 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
2906 -- Determine whether the entity of a package denoted by Pkg has a null
2907 -- abstract state.
2909 -----------------------------
2910 -- Has_Null_Abstract_State --
2911 -----------------------------
2913 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
2914 States : constant Elist_Id := Abstract_States (Pkg);
2916 begin
2917 -- Check first available state of related package. A null abstract
2918 -- state always appears as the sole element of the state list.
2920 return
2921 Present (States)
2922 and then Is_Null_State (Node (First_Elmt (States)));
2923 end Has_Null_Abstract_State;
2925 -- Local variables
2927 Context : Entity_Id := Empty;
2928 Not_Visible : Boolean := False;
2929 Scop : Entity_Id;
2931 -- Start of processing for Check_No_Hidden_State
2933 begin
2934 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
2936 -- Find the proper context where the object or state appears
2938 Scop := Scope (Id);
2939 while Present (Scop) loop
2940 Context := Scop;
2942 -- Keep track of the context's visibility
2944 Not_Visible := Not_Visible or else In_Private_Part (Context);
2946 -- Prevent the search from going too far
2948 if Context = Standard_Standard then
2949 return;
2951 -- Objects and states that appear immediately within a subprogram or
2952 -- inside a construct nested within a subprogram do not introduce a
2953 -- hidden state. They behave as local variable declarations.
2955 elsif Is_Subprogram (Context) then
2956 return;
2958 -- When examining a package body, use the entity of the spec as it
2959 -- carries the abstract state declarations.
2961 elsif Ekind (Context) = E_Package_Body then
2962 Context := Spec_Entity (Context);
2963 end if;
2965 -- Stop the traversal when a package subject to a null abstract state
2966 -- has been found.
2968 if Ekind_In (Context, E_Generic_Package, E_Package)
2969 and then Has_Null_Abstract_State (Context)
2970 then
2971 exit;
2972 end if;
2974 Scop := Scope (Scop);
2975 end loop;
2977 -- At this point we know that there is at least one package with a null
2978 -- abstract state in visibility. Emit an error message unconditionally
2979 -- if the entity being processed is a state because the placement of the
2980 -- related package is irrelevant. This is not the case for objects as
2981 -- the intermediate context matters.
2983 if Present (Context)
2984 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
2985 then
2986 Error_Msg_N ("cannot introduce hidden state &", Id);
2987 Error_Msg_NE ("\package & has null abstract state", Id, Context);
2988 end if;
2989 end Check_No_Hidden_State;
2991 ------------------------------------------
2992 -- Check_Potentially_Blocking_Operation --
2993 ------------------------------------------
2995 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
2996 S : Entity_Id;
2998 begin
2999 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3000 -- When pragma Detect_Blocking is active, the run time will raise
3001 -- Program_Error. Here we only issue a warning, since we generally
3002 -- support the use of potentially blocking operations in the absence
3003 -- of the pragma.
3005 -- Indirect blocking through a subprogram call cannot be diagnosed
3006 -- statically without interprocedural analysis, so we do not attempt
3007 -- to do it here.
3009 S := Scope (Current_Scope);
3010 while Present (S) and then S /= Standard_Standard loop
3011 if Is_Protected_Type (S) then
3012 Error_Msg_N
3013 ("potentially blocking operation in protected operation??", N);
3014 return;
3015 end if;
3017 S := Scope (S);
3018 end loop;
3019 end Check_Potentially_Blocking_Operation;
3021 ---------------------------------
3022 -- Check_Result_And_Post_State --
3023 ---------------------------------
3025 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3026 procedure Check_Result_And_Post_State_In_Pragma
3027 (Prag : Node_Id;
3028 Result_Seen : in out Boolean);
3029 -- Determine whether pragma Prag mentions attribute 'Result and whether
3030 -- the pragma contains an expression that evaluates differently in pre-
3031 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3032 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3034 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3035 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3036 -- formal parameter.
3038 -------------------------------------------
3039 -- Check_Result_And_Post_State_In_Pragma --
3040 -------------------------------------------
3042 procedure Check_Result_And_Post_State_In_Pragma
3043 (Prag : Node_Id;
3044 Result_Seen : in out Boolean)
3046 procedure Check_Expression (Expr : Node_Id);
3047 -- Perform the 'Result and post-state checks on a given expression
3049 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3050 -- Attempt to find attribute 'Result in a subtree denoted by N
3052 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3053 -- Determine whether source node N denotes "True" or "False"
3055 function Mentions_Post_State (N : Node_Id) return Boolean;
3056 -- Determine whether a subtree denoted by N mentions any construct
3057 -- that denotes a post-state.
3059 procedure Check_Function_Result is
3060 new Traverse_Proc (Is_Function_Result);
3062 ----------------------
3063 -- Check_Expression --
3064 ----------------------
3066 procedure Check_Expression (Expr : Node_Id) is
3067 begin
3068 if not Is_Trivial_Boolean (Expr) then
3069 Check_Function_Result (Expr);
3071 if not Mentions_Post_State (Expr) then
3072 if Pragma_Name (Prag) = Name_Contract_Cases then
3073 Error_Msg_NE
3074 ("contract case does not check the outcome of calling "
3075 & "&?T?", Expr, Subp_Id);
3077 elsif Pragma_Name (Prag) = Name_Refined_Post then
3078 Error_Msg_NE
3079 ("refined postcondition does not check the outcome of "
3080 & "calling &?T?", Prag, Subp_Id);
3082 else
3083 Error_Msg_NE
3084 ("postcondition does not check the outcome of calling "
3085 & "&?T?", Prag, Subp_Id);
3086 end if;
3087 end if;
3088 end if;
3089 end Check_Expression;
3091 ------------------------
3092 -- Is_Function_Result --
3093 ------------------------
3095 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3096 begin
3097 if Is_Attribute_Result (N) then
3098 Result_Seen := True;
3099 return Abandon;
3101 -- Continue the traversal
3103 else
3104 return OK;
3105 end if;
3106 end Is_Function_Result;
3108 ------------------------
3109 -- Is_Trivial_Boolean --
3110 ------------------------
3112 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3113 begin
3114 return
3115 Comes_From_Source (N)
3116 and then Is_Entity_Name (N)
3117 and then (Entity (N) = Standard_True
3118 or else
3119 Entity (N) = Standard_False);
3120 end Is_Trivial_Boolean;
3122 -------------------------
3123 -- Mentions_Post_State --
3124 -------------------------
3126 function Mentions_Post_State (N : Node_Id) return Boolean is
3127 Post_State_Seen : Boolean := False;
3129 function Is_Post_State (N : Node_Id) return Traverse_Result;
3130 -- Attempt to find a construct that denotes a post-state. If this
3131 -- is the case, set flag Post_State_Seen.
3133 -------------------
3134 -- Is_Post_State --
3135 -------------------
3137 function Is_Post_State (N : Node_Id) return Traverse_Result is
3138 Ent : Entity_Id;
3140 begin
3141 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3142 Post_State_Seen := True;
3143 return Abandon;
3145 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3146 Ent := Entity (N);
3148 -- The entity may be modifiable through an implicit
3149 -- dereference.
3151 if No (Ent)
3152 or else Ekind (Ent) in Assignable_Kind
3153 or else (Is_Access_Type (Etype (Ent))
3154 and then Nkind (Parent (N)) =
3155 N_Selected_Component)
3156 then
3157 Post_State_Seen := True;
3158 return Abandon;
3159 end if;
3161 elsif Nkind (N) = N_Attribute_Reference then
3162 if Attribute_Name (N) = Name_Old then
3163 return Skip;
3165 elsif Attribute_Name (N) = Name_Result then
3166 Post_State_Seen := True;
3167 return Abandon;
3168 end if;
3169 end if;
3171 return OK;
3172 end Is_Post_State;
3174 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3176 -- Start of processing for Mentions_Post_State
3178 begin
3179 Find_Post_State (N);
3181 return Post_State_Seen;
3182 end Mentions_Post_State;
3184 -- Local variables
3186 Expr : constant Node_Id :=
3187 Get_Pragma_Arg
3188 (First (Pragma_Argument_Associations (Prag)));
3189 Nam : constant Name_Id := Pragma_Name (Prag);
3190 CCase : Node_Id;
3192 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3194 begin
3195 -- Examine all consequences
3197 if Nam = Name_Contract_Cases then
3198 CCase := First (Component_Associations (Expr));
3199 while Present (CCase) loop
3200 Check_Expression (Expression (CCase));
3202 Next (CCase);
3203 end loop;
3205 -- Examine the expression of a postcondition
3207 else pragma Assert (Nam_In (Nam, Name_Postcondition,
3208 Name_Refined_Post));
3209 Check_Expression (Expr);
3210 end if;
3211 end Check_Result_And_Post_State_In_Pragma;
3213 --------------------------
3214 -- Has_In_Out_Parameter --
3215 --------------------------
3217 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
3218 Formal : Entity_Id;
3220 begin
3221 -- Traverse the formals looking for an IN OUT parameter
3223 Formal := First_Formal (Subp_Id);
3224 while Present (Formal) loop
3225 if Ekind (Formal) = E_In_Out_Parameter then
3226 return True;
3227 end if;
3229 Next_Formal (Formal);
3230 end loop;
3232 return False;
3233 end Has_In_Out_Parameter;
3235 -- Local variables
3237 Items : constant Node_Id := Contract (Subp_Id);
3238 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3239 Case_Prag : Node_Id := Empty;
3240 Post_Prag : Node_Id := Empty;
3241 Prag : Node_Id;
3242 Seen_In_Case : Boolean := False;
3243 Seen_In_Post : Boolean := False;
3244 Spec_Id : Entity_Id;
3246 -- Start of processing for Check_Result_And_Post_State
3248 begin
3249 -- The lack of attribute 'Result or a post-state is classified as a
3250 -- suspicious contract. Do not perform the check if the corresponding
3251 -- swich is not set.
3253 if not Warn_On_Suspicious_Contract then
3254 return;
3256 -- Nothing to do if there is no contract
3258 elsif No (Items) then
3259 return;
3260 end if;
3262 -- Retrieve the entity of the subprogram spec (if any)
3264 if Nkind (Subp_Decl) = N_Subprogram_Body
3265 and then Present (Corresponding_Spec (Subp_Decl))
3266 then
3267 Spec_Id := Corresponding_Spec (Subp_Decl);
3269 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
3270 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
3271 then
3272 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
3274 else
3275 Spec_Id := Subp_Id;
3276 end if;
3278 -- Examine all postconditions for attribute 'Result and a post-state
3280 Prag := Pre_Post_Conditions (Items);
3281 while Present (Prag) loop
3282 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
3283 Name_Refined_Post)
3284 and then not Error_Posted (Prag)
3285 then
3286 Post_Prag := Prag;
3287 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
3288 end if;
3290 Prag := Next_Pragma (Prag);
3291 end loop;
3293 -- Examine the contract cases of the subprogram for attribute 'Result
3294 -- and a post-state.
3296 Prag := Contract_Test_Cases (Items);
3297 while Present (Prag) loop
3298 if Pragma_Name (Prag) = Name_Contract_Cases
3299 and then not Error_Posted (Prag)
3300 then
3301 Case_Prag := Prag;
3302 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
3303 end if;
3305 Prag := Next_Pragma (Prag);
3306 end loop;
3308 -- Do not emit any errors if the subprogram is not a function
3310 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
3311 null;
3313 -- Regardless of whether the function has postconditions or contract
3314 -- cases, or whether they mention attribute 'Result, an IN OUT formal
3315 -- parameter is always treated as a result.
3317 elsif Has_In_Out_Parameter (Spec_Id) then
3318 null;
3320 -- The function has both a postcondition and contract cases and they do
3321 -- not mention attribute 'Result.
3323 elsif Present (Case_Prag)
3324 and then not Seen_In_Case
3325 and then Present (Post_Prag)
3326 and then not Seen_In_Post
3327 then
3328 Error_Msg_N
3329 ("neither postcondition nor contract cases mention function "
3330 & "result?T?", Post_Prag);
3332 -- The function has contract cases only and they do not mention
3333 -- attribute 'Result.
3335 elsif Present (Case_Prag) and then not Seen_In_Case then
3336 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
3338 -- The function has postconditions only and they do not mention
3339 -- attribute 'Result.
3341 elsif Present (Post_Prag) and then not Seen_In_Post then
3342 Error_Msg_N
3343 ("postcondition does not mention function result?T?", Post_Prag);
3344 end if;
3345 end Check_Result_And_Post_State;
3347 ------------------------------
3348 -- Check_Unprotected_Access --
3349 ------------------------------
3351 procedure Check_Unprotected_Access
3352 (Context : Node_Id;
3353 Expr : Node_Id)
3355 Cont_Encl_Typ : Entity_Id;
3356 Pref_Encl_Typ : Entity_Id;
3358 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3359 -- Check whether Obj is a private component of a protected object.
3360 -- Return the protected type where the component resides, Empty
3361 -- otherwise.
3363 function Is_Public_Operation return Boolean;
3364 -- Verify that the enclosing operation is callable from outside the
3365 -- protected object, to minimize false positives.
3367 ------------------------------
3368 -- Enclosing_Protected_Type --
3369 ------------------------------
3371 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3372 begin
3373 if Is_Entity_Name (Obj) then
3374 declare
3375 Ent : Entity_Id := Entity (Obj);
3377 begin
3378 -- The object can be a renaming of a private component, use
3379 -- the original record component.
3381 if Is_Prival (Ent) then
3382 Ent := Prival_Link (Ent);
3383 end if;
3385 if Is_Protected_Type (Scope (Ent)) then
3386 return Scope (Ent);
3387 end if;
3388 end;
3389 end if;
3391 -- For indexed and selected components, recursively check the prefix
3393 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3394 return Enclosing_Protected_Type (Prefix (Obj));
3396 -- The object does not denote a protected component
3398 else
3399 return Empty;
3400 end if;
3401 end Enclosing_Protected_Type;
3403 -------------------------
3404 -- Is_Public_Operation --
3405 -------------------------
3407 function Is_Public_Operation return Boolean is
3408 S : Entity_Id;
3409 E : Entity_Id;
3411 begin
3412 S := Current_Scope;
3413 while Present (S) and then S /= Pref_Encl_Typ loop
3414 if Scope (S) = Pref_Encl_Typ then
3415 E := First_Entity (Pref_Encl_Typ);
3416 while Present (E)
3417 and then E /= First_Private_Entity (Pref_Encl_Typ)
3418 loop
3419 if E = S then
3420 return True;
3421 end if;
3423 Next_Entity (E);
3424 end loop;
3425 end if;
3427 S := Scope (S);
3428 end loop;
3430 return False;
3431 end Is_Public_Operation;
3433 -- Start of processing for Check_Unprotected_Access
3435 begin
3436 if Nkind (Expr) = N_Attribute_Reference
3437 and then Attribute_Name (Expr) = Name_Unchecked_Access
3438 then
3439 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3440 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3442 -- Check whether we are trying to export a protected component to a
3443 -- context with an equal or lower access level.
3445 if Present (Pref_Encl_Typ)
3446 and then No (Cont_Encl_Typ)
3447 and then Is_Public_Operation
3448 and then Scope_Depth (Pref_Encl_Typ) >=
3449 Object_Access_Level (Context)
3450 then
3451 Error_Msg_N
3452 ("??possible unprotected access to protected data", Expr);
3453 end if;
3454 end if;
3455 end Check_Unprotected_Access;
3457 ------------------------
3458 -- Collect_Interfaces --
3459 ------------------------
3461 procedure Collect_Interfaces
3462 (T : Entity_Id;
3463 Ifaces_List : out Elist_Id;
3464 Exclude_Parents : Boolean := False;
3465 Use_Full_View : Boolean := True)
3467 procedure Collect (Typ : Entity_Id);
3468 -- Subsidiary subprogram used to traverse the whole list
3469 -- of directly and indirectly implemented interfaces
3471 -------------
3472 -- Collect --
3473 -------------
3475 procedure Collect (Typ : Entity_Id) is
3476 Ancestor : Entity_Id;
3477 Full_T : Entity_Id;
3478 Id : Node_Id;
3479 Iface : Entity_Id;
3481 begin
3482 Full_T := Typ;
3484 -- Handle private types
3486 if Use_Full_View
3487 and then Is_Private_Type (Typ)
3488 and then Present (Full_View (Typ))
3489 then
3490 Full_T := Full_View (Typ);
3491 end if;
3493 -- Include the ancestor if we are generating the whole list of
3494 -- abstract interfaces.
3496 if Etype (Full_T) /= Typ
3498 -- Protect the frontend against wrong sources. For example:
3500 -- package P is
3501 -- type A is tagged null record;
3502 -- type B is new A with private;
3503 -- type C is new A with private;
3504 -- private
3505 -- type B is new C with null record;
3506 -- type C is new B with null record;
3507 -- end P;
3509 and then Etype (Full_T) /= T
3510 then
3511 Ancestor := Etype (Full_T);
3512 Collect (Ancestor);
3514 if Is_Interface (Ancestor) and then not Exclude_Parents then
3515 Append_Unique_Elmt (Ancestor, Ifaces_List);
3516 end if;
3517 end if;
3519 -- Traverse the graph of ancestor interfaces
3521 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
3522 Id := First (Abstract_Interface_List (Full_T));
3523 while Present (Id) loop
3524 Iface := Etype (Id);
3526 -- Protect against wrong uses. For example:
3527 -- type I is interface;
3528 -- type O is tagged null record;
3529 -- type Wrong is new I and O with null record; -- ERROR
3531 if Is_Interface (Iface) then
3532 if Exclude_Parents
3533 and then Etype (T) /= T
3534 and then Interface_Present_In_Ancestor (Etype (T), Iface)
3535 then
3536 null;
3537 else
3538 Collect (Iface);
3539 Append_Unique_Elmt (Iface, Ifaces_List);
3540 end if;
3541 end if;
3543 Next (Id);
3544 end loop;
3545 end if;
3546 end Collect;
3548 -- Start of processing for Collect_Interfaces
3550 begin
3551 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
3552 Ifaces_List := New_Elmt_List;
3553 Collect (T);
3554 end Collect_Interfaces;
3556 ----------------------------------
3557 -- Collect_Interface_Components --
3558 ----------------------------------
3560 procedure Collect_Interface_Components
3561 (Tagged_Type : Entity_Id;
3562 Components_List : out Elist_Id)
3564 procedure Collect (Typ : Entity_Id);
3565 -- Subsidiary subprogram used to climb to the parents
3567 -------------
3568 -- Collect --
3569 -------------
3571 procedure Collect (Typ : Entity_Id) is
3572 Tag_Comp : Entity_Id;
3573 Parent_Typ : Entity_Id;
3575 begin
3576 -- Handle private types
3578 if Present (Full_View (Etype (Typ))) then
3579 Parent_Typ := Full_View (Etype (Typ));
3580 else
3581 Parent_Typ := Etype (Typ);
3582 end if;
3584 if Parent_Typ /= Typ
3586 -- Protect the frontend against wrong sources. For example:
3588 -- package P is
3589 -- type A is tagged null record;
3590 -- type B is new A with private;
3591 -- type C is new A with private;
3592 -- private
3593 -- type B is new C with null record;
3594 -- type C is new B with null record;
3595 -- end P;
3597 and then Parent_Typ /= Tagged_Type
3598 then
3599 Collect (Parent_Typ);
3600 end if;
3602 -- Collect the components containing tags of secondary dispatch
3603 -- tables.
3605 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
3606 while Present (Tag_Comp) loop
3607 pragma Assert (Present (Related_Type (Tag_Comp)));
3608 Append_Elmt (Tag_Comp, Components_List);
3610 Tag_Comp := Next_Tag_Component (Tag_Comp);
3611 end loop;
3612 end Collect;
3614 -- Start of processing for Collect_Interface_Components
3616 begin
3617 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
3618 and then Is_Tagged_Type (Tagged_Type));
3620 Components_List := New_Elmt_List;
3621 Collect (Tagged_Type);
3622 end Collect_Interface_Components;
3624 -----------------------------
3625 -- Collect_Interfaces_Info --
3626 -----------------------------
3628 procedure Collect_Interfaces_Info
3629 (T : Entity_Id;
3630 Ifaces_List : out Elist_Id;
3631 Components_List : out Elist_Id;
3632 Tags_List : out Elist_Id)
3634 Comps_List : Elist_Id;
3635 Comp_Elmt : Elmt_Id;
3636 Comp_Iface : Entity_Id;
3637 Iface_Elmt : Elmt_Id;
3638 Iface : Entity_Id;
3640 function Search_Tag (Iface : Entity_Id) return Entity_Id;
3641 -- Search for the secondary tag associated with the interface type
3642 -- Iface that is implemented by T.
3644 ----------------
3645 -- Search_Tag --
3646 ----------------
3648 function Search_Tag (Iface : Entity_Id) return Entity_Id is
3649 ADT : Elmt_Id;
3650 begin
3651 if not Is_CPP_Class (T) then
3652 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
3653 else
3654 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
3655 end if;
3657 while Present (ADT)
3658 and then Is_Tag (Node (ADT))
3659 and then Related_Type (Node (ADT)) /= Iface
3660 loop
3661 -- Skip secondary dispatch table referencing thunks to user
3662 -- defined primitives covered by this interface.
3664 pragma Assert (Has_Suffix (Node (ADT), 'P'));
3665 Next_Elmt (ADT);
3667 -- Skip secondary dispatch tables of Ada types
3669 if not Is_CPP_Class (T) then
3671 -- Skip secondary dispatch table referencing thunks to
3672 -- predefined primitives.
3674 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
3675 Next_Elmt (ADT);
3677 -- Skip secondary dispatch table referencing user-defined
3678 -- primitives covered by this interface.
3680 pragma Assert (Has_Suffix (Node (ADT), 'D'));
3681 Next_Elmt (ADT);
3683 -- Skip secondary dispatch table referencing predefined
3684 -- primitives.
3686 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
3687 Next_Elmt (ADT);
3688 end if;
3689 end loop;
3691 pragma Assert (Is_Tag (Node (ADT)));
3692 return Node (ADT);
3693 end Search_Tag;
3695 -- Start of processing for Collect_Interfaces_Info
3697 begin
3698 Collect_Interfaces (T, Ifaces_List);
3699 Collect_Interface_Components (T, Comps_List);
3701 -- Search for the record component and tag associated with each
3702 -- interface type of T.
3704 Components_List := New_Elmt_List;
3705 Tags_List := New_Elmt_List;
3707 Iface_Elmt := First_Elmt (Ifaces_List);
3708 while Present (Iface_Elmt) loop
3709 Iface := Node (Iface_Elmt);
3711 -- Associate the primary tag component and the primary dispatch table
3712 -- with all the interfaces that are parents of T
3714 if Is_Ancestor (Iface, T, Use_Full_View => True) then
3715 Append_Elmt (First_Tag_Component (T), Components_List);
3716 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
3718 -- Otherwise search for the tag component and secondary dispatch
3719 -- table of Iface
3721 else
3722 Comp_Elmt := First_Elmt (Comps_List);
3723 while Present (Comp_Elmt) loop
3724 Comp_Iface := Related_Type (Node (Comp_Elmt));
3726 if Comp_Iface = Iface
3727 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
3728 then
3729 Append_Elmt (Node (Comp_Elmt), Components_List);
3730 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
3731 exit;
3732 end if;
3734 Next_Elmt (Comp_Elmt);
3735 end loop;
3736 pragma Assert (Present (Comp_Elmt));
3737 end if;
3739 Next_Elmt (Iface_Elmt);
3740 end loop;
3741 end Collect_Interfaces_Info;
3743 ---------------------
3744 -- Collect_Parents --
3745 ---------------------
3747 procedure Collect_Parents
3748 (T : Entity_Id;
3749 List : out Elist_Id;
3750 Use_Full_View : Boolean := True)
3752 Current_Typ : Entity_Id := T;
3753 Parent_Typ : Entity_Id;
3755 begin
3756 List := New_Elmt_List;
3758 -- No action if the if the type has no parents
3760 if T = Etype (T) then
3761 return;
3762 end if;
3764 loop
3765 Parent_Typ := Etype (Current_Typ);
3767 if Is_Private_Type (Parent_Typ)
3768 and then Present (Full_View (Parent_Typ))
3769 and then Use_Full_View
3770 then
3771 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3772 end if;
3774 Append_Elmt (Parent_Typ, List);
3776 exit when Parent_Typ = Current_Typ;
3777 Current_Typ := Parent_Typ;
3778 end loop;
3779 end Collect_Parents;
3781 ----------------------------------
3782 -- Collect_Primitive_Operations --
3783 ----------------------------------
3785 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
3786 B_Type : constant Entity_Id := Base_Type (T);
3787 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
3788 B_Scope : Entity_Id := Scope (B_Type);
3789 Op_List : Elist_Id;
3790 Formal : Entity_Id;
3791 Is_Prim : Boolean;
3792 Is_Type_In_Pkg : Boolean;
3793 Formal_Derived : Boolean := False;
3794 Id : Entity_Id;
3796 function Match (E : Entity_Id) return Boolean;
3797 -- True if E's base type is B_Type, or E is of an anonymous access type
3798 -- and the base type of its designated type is B_Type.
3800 -----------
3801 -- Match --
3802 -----------
3804 function Match (E : Entity_Id) return Boolean is
3805 Etyp : Entity_Id := Etype (E);
3807 begin
3808 if Ekind (Etyp) = E_Anonymous_Access_Type then
3809 Etyp := Designated_Type (Etyp);
3810 end if;
3812 -- In Ada 2012 a primitive operation may have a formal of an
3813 -- incomplete view of the parent type.
3815 return Base_Type (Etyp) = B_Type
3816 or else
3817 (Ada_Version >= Ada_2012
3818 and then Ekind (Etyp) = E_Incomplete_Type
3819 and then Full_View (Etyp) = B_Type);
3820 end Match;
3822 -- Start of processing for Collect_Primitive_Operations
3824 begin
3825 -- For tagged types, the primitive operations are collected as they
3826 -- are declared, and held in an explicit list which is simply returned.
3828 if Is_Tagged_Type (B_Type) then
3829 return Primitive_Operations (B_Type);
3831 -- An untagged generic type that is a derived type inherits the
3832 -- primitive operations of its parent type. Other formal types only
3833 -- have predefined operators, which are not explicitly represented.
3835 elsif Is_Generic_Type (B_Type) then
3836 if Nkind (B_Decl) = N_Formal_Type_Declaration
3837 and then Nkind (Formal_Type_Definition (B_Decl)) =
3838 N_Formal_Derived_Type_Definition
3839 then
3840 Formal_Derived := True;
3841 else
3842 return New_Elmt_List;
3843 end if;
3844 end if;
3846 Op_List := New_Elmt_List;
3848 if B_Scope = Standard_Standard then
3849 if B_Type = Standard_String then
3850 Append_Elmt (Standard_Op_Concat, Op_List);
3852 elsif B_Type = Standard_Wide_String then
3853 Append_Elmt (Standard_Op_Concatw, Op_List);
3855 else
3856 null;
3857 end if;
3859 -- Locate the primitive subprograms of the type
3861 else
3862 -- The primitive operations appear after the base type, except
3863 -- if the derivation happens within the private part of B_Scope
3864 -- and the type is a private type, in which case both the type
3865 -- and some primitive operations may appear before the base
3866 -- type, and the list of candidates starts after the type.
3868 if In_Open_Scopes (B_Scope)
3869 and then Scope (T) = B_Scope
3870 and then In_Private_Part (B_Scope)
3871 then
3872 Id := Next_Entity (T);
3874 -- In Ada 2012, If the type has an incomplete partial view, there
3875 -- may be primitive operations declared before the full view, so
3876 -- we need to start scanning from the incomplete view, which is
3877 -- earlier on the entity chain.
3879 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
3880 and then Present (Incomplete_View (Parent (B_Type)))
3881 then
3882 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
3884 else
3885 Id := Next_Entity (B_Type);
3886 end if;
3888 -- Set flag if this is a type in a package spec
3890 Is_Type_In_Pkg :=
3891 Is_Package_Or_Generic_Package (B_Scope)
3892 and then
3893 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
3894 N_Package_Body;
3896 while Present (Id) loop
3898 -- Test whether the result type or any of the parameter types of
3899 -- each subprogram following the type match that type when the
3900 -- type is declared in a package spec, is a derived type, or the
3901 -- subprogram is marked as primitive. (The Is_Primitive test is
3902 -- needed to find primitives of nonderived types in declarative
3903 -- parts that happen to override the predefined "=" operator.)
3905 -- Note that generic formal subprograms are not considered to be
3906 -- primitive operations and thus are never inherited.
3908 if Is_Overloadable (Id)
3909 and then (Is_Type_In_Pkg
3910 or else Is_Derived_Type (B_Type)
3911 or else Is_Primitive (Id))
3912 and then Nkind (Parent (Parent (Id)))
3913 not in N_Formal_Subprogram_Declaration
3914 then
3915 Is_Prim := False;
3917 if Match (Id) then
3918 Is_Prim := True;
3920 else
3921 Formal := First_Formal (Id);
3922 while Present (Formal) loop
3923 if Match (Formal) then
3924 Is_Prim := True;
3925 exit;
3926 end if;
3928 Next_Formal (Formal);
3929 end loop;
3930 end if;
3932 -- For a formal derived type, the only primitives are the ones
3933 -- inherited from the parent type. Operations appearing in the
3934 -- package declaration are not primitive for it.
3936 if Is_Prim
3937 and then (not Formal_Derived or else Present (Alias (Id)))
3938 then
3939 -- In the special case of an equality operator aliased to
3940 -- an overriding dispatching equality belonging to the same
3941 -- type, we don't include it in the list of primitives.
3942 -- This avoids inheriting multiple equality operators when
3943 -- deriving from untagged private types whose full type is
3944 -- tagged, which can otherwise cause ambiguities. Note that
3945 -- this should only happen for this kind of untagged parent
3946 -- type, since normally dispatching operations are inherited
3947 -- using the type's Primitive_Operations list.
3949 if Chars (Id) = Name_Op_Eq
3950 and then Is_Dispatching_Operation (Id)
3951 and then Present (Alias (Id))
3952 and then Present (Overridden_Operation (Alias (Id)))
3953 and then Base_Type (Etype (First_Entity (Id))) =
3954 Base_Type (Etype (First_Entity (Alias (Id))))
3955 then
3956 null;
3958 -- Include the subprogram in the list of primitives
3960 else
3961 Append_Elmt (Id, Op_List);
3962 end if;
3963 end if;
3964 end if;
3966 Next_Entity (Id);
3968 -- For a type declared in System, some of its operations may
3969 -- appear in the target-specific extension to System.
3971 if No (Id)
3972 and then B_Scope = RTU_Entity (System)
3973 and then Present_System_Aux
3974 then
3975 B_Scope := System_Aux_Id;
3976 Id := First_Entity (System_Aux_Id);
3977 end if;
3978 end loop;
3979 end if;
3981 return Op_List;
3982 end Collect_Primitive_Operations;
3984 -----------------------------------
3985 -- Compile_Time_Constraint_Error --
3986 -----------------------------------
3988 function Compile_Time_Constraint_Error
3989 (N : Node_Id;
3990 Msg : String;
3991 Ent : Entity_Id := Empty;
3992 Loc : Source_Ptr := No_Location;
3993 Warn : Boolean := False) return Node_Id
3995 Msgc : String (1 .. Msg'Length + 3);
3996 -- Copy of message, with room for possible ?? or << and ! at end
3998 Msgl : Natural;
3999 Wmsg : Boolean;
4000 Eloc : Source_Ptr;
4002 -- Start of processing for Compile_Time_Constraint_Error
4004 begin
4005 -- If this is a warning, convert it into an error if we are in code
4006 -- subject to SPARK_Mode being set ON.
4008 Error_Msg_Warn := SPARK_Mode /= On;
4010 -- A static constraint error in an instance body is not a fatal error.
4011 -- we choose to inhibit the message altogether, because there is no
4012 -- obvious node (for now) on which to post it. On the other hand the
4013 -- offending node must be replaced with a constraint_error in any case.
4015 -- No messages are generated if we already posted an error on this node
4017 if not Error_Posted (N) then
4018 if Loc /= No_Location then
4019 Eloc := Loc;
4020 else
4021 Eloc := Sloc (N);
4022 end if;
4024 -- Copy message to Msgc, converting any ? in the message into
4025 -- < instead, so that we have an error in GNATprove mode.
4027 Msgl := Msg'Length;
4029 for J in 1 .. Msgl loop
4030 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
4031 Msgc (J) := '<';
4032 else
4033 Msgc (J) := Msg (J);
4034 end if;
4035 end loop;
4037 -- Message is a warning, even in Ada 95 case
4039 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
4040 Wmsg := True;
4042 -- In Ada 83, all messages are warnings. In the private part and
4043 -- the body of an instance, constraint_checks are only warnings.
4044 -- We also make this a warning if the Warn parameter is set.
4046 elsif Warn
4047 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
4048 then
4049 Msgl := Msgl + 1;
4050 Msgc (Msgl) := '<';
4051 Msgl := Msgl + 1;
4052 Msgc (Msgl) := '<';
4053 Wmsg := True;
4055 elsif In_Instance_Not_Visible then
4056 Msgl := Msgl + 1;
4057 Msgc (Msgl) := '<';
4058 Msgl := Msgl + 1;
4059 Msgc (Msgl) := '<';
4060 Wmsg := True;
4062 -- Otherwise we have a real error message (Ada 95 static case)
4063 -- and we make this an unconditional message. Note that in the
4064 -- warning case we do not make the message unconditional, it seems
4065 -- quite reasonable to delete messages like this (about exceptions
4066 -- that will be raised) in dead code.
4068 else
4069 Wmsg := False;
4070 Msgl := Msgl + 1;
4071 Msgc (Msgl) := '!';
4072 end if;
4074 -- One more test, skip the warning if the related expression is
4075 -- statically unevaluated, since we don't want to warn about what
4076 -- will happen when something is evaluated if it never will be
4077 -- evaluated.
4079 if not Is_Statically_Unevaluated (N) then
4080 Error_Msg_Warn := SPARK_Mode /= On;
4082 if Present (Ent) then
4083 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4084 else
4085 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4086 end if;
4088 if Wmsg then
4090 -- Check whether the context is an Init_Proc
4092 if Inside_Init_Proc then
4093 declare
4094 Conc_Typ : constant Entity_Id :=
4095 Corresponding_Concurrent_Type
4096 (Entity (Parameter_Type (First
4097 (Parameter_Specifications
4098 (Parent (Current_Scope))))));
4100 begin
4101 -- Don't complain if the corresponding concurrent type
4102 -- doesn't come from source (i.e. a single task/protected
4103 -- object).
4105 if Present (Conc_Typ)
4106 and then not Comes_From_Source (Conc_Typ)
4107 then
4108 Error_Msg_NEL
4109 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4111 else
4112 if GNATprove_Mode then
4113 Error_Msg_NEL
4114 ("\& would have been raised for objects of this "
4115 & "type", N, Standard_Constraint_Error, Eloc);
4116 else
4117 Error_Msg_NEL
4118 ("\& will be raised for objects of this type??",
4119 N, Standard_Constraint_Error, Eloc);
4120 end if;
4121 end if;
4122 end;
4124 else
4125 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4126 end if;
4128 else
4129 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4130 Set_Error_Posted (N);
4131 end if;
4132 end if;
4133 end if;
4135 return N;
4136 end Compile_Time_Constraint_Error;
4138 -----------------------
4139 -- Conditional_Delay --
4140 -----------------------
4142 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4143 begin
4144 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4145 Set_Has_Delayed_Freeze (New_Ent);
4146 end if;
4147 end Conditional_Delay;
4149 ----------------------------
4150 -- Contains_Refined_State --
4151 ----------------------------
4153 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4154 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4155 -- Determine whether a dependency list mentions a state with a visible
4156 -- refinement.
4158 function Has_State_In_Global (List : Node_Id) return Boolean;
4159 -- Determine whether a global list mentions a state with a visible
4160 -- refinement.
4162 function Is_Refined_State (Item : Node_Id) return Boolean;
4163 -- Determine whether Item is a reference to an abstract state with a
4164 -- visible refinement.
4166 -----------------------------
4167 -- Has_State_In_Dependency --
4168 -----------------------------
4170 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4171 Clause : Node_Id;
4172 Output : Node_Id;
4174 begin
4175 -- A null dependency list does not mention any states
4177 if Nkind (List) = N_Null then
4178 return False;
4180 -- Dependency clauses appear as component associations of an
4181 -- aggregate.
4183 elsif Nkind (List) = N_Aggregate
4184 and then Present (Component_Associations (List))
4185 then
4186 Clause := First (Component_Associations (List));
4187 while Present (Clause) loop
4189 -- Inspect the outputs of a dependency clause
4191 Output := First (Choices (Clause));
4192 while Present (Output) loop
4193 if Is_Refined_State (Output) then
4194 return True;
4195 end if;
4197 Next (Output);
4198 end loop;
4200 -- Inspect the outputs of a dependency clause
4202 if Is_Refined_State (Expression (Clause)) then
4203 return True;
4204 end if;
4206 Next (Clause);
4207 end loop;
4209 -- If we get here, then none of the dependency clauses mention a
4210 -- state with visible refinement.
4212 return False;
4214 -- An illegal pragma managed to sneak in
4216 else
4217 raise Program_Error;
4218 end if;
4219 end Has_State_In_Dependency;
4221 -------------------------
4222 -- Has_State_In_Global --
4223 -------------------------
4225 function Has_State_In_Global (List : Node_Id) return Boolean is
4226 Item : Node_Id;
4228 begin
4229 -- A null global list does not mention any states
4231 if Nkind (List) = N_Null then
4232 return False;
4234 -- Simple global list or moded global list declaration
4236 elsif Nkind (List) = N_Aggregate then
4238 -- The declaration of a simple global list appear as a collection
4239 -- of expressions.
4241 if Present (Expressions (List)) then
4242 Item := First (Expressions (List));
4243 while Present (Item) loop
4244 if Is_Refined_State (Item) then
4245 return True;
4246 end if;
4248 Next (Item);
4249 end loop;
4251 -- The declaration of a moded global list appears as a collection
4252 -- of component associations where individual choices denote
4253 -- modes.
4255 else
4256 Item := First (Component_Associations (List));
4257 while Present (Item) loop
4258 if Has_State_In_Global (Expression (Item)) then
4259 return True;
4260 end if;
4262 Next (Item);
4263 end loop;
4264 end if;
4266 -- If we get here, then the simple/moded global list did not
4267 -- mention any states with a visible refinement.
4269 return False;
4271 -- Single global item declaration
4273 elsif Is_Entity_Name (List) then
4274 return Is_Refined_State (List);
4276 -- An illegal pragma managed to sneak in
4278 else
4279 raise Program_Error;
4280 end if;
4281 end Has_State_In_Global;
4283 ----------------------
4284 -- Is_Refined_State --
4285 ----------------------
4287 function Is_Refined_State (Item : Node_Id) return Boolean is
4288 Elmt : Node_Id;
4289 Item_Id : Entity_Id;
4291 begin
4292 if Nkind (Item) = N_Null then
4293 return False;
4295 -- States cannot be subject to attribute 'Result. This case arises
4296 -- in dependency relations.
4298 elsif Nkind (Item) = N_Attribute_Reference
4299 and then Attribute_Name (Item) = Name_Result
4300 then
4301 return False;
4303 -- Multiple items appear as an aggregate. This case arises in
4304 -- dependency relations.
4306 elsif Nkind (Item) = N_Aggregate
4307 and then Present (Expressions (Item))
4308 then
4309 Elmt := First (Expressions (Item));
4310 while Present (Elmt) loop
4311 if Is_Refined_State (Elmt) then
4312 return True;
4313 end if;
4315 Next (Elmt);
4316 end loop;
4318 -- If we get here, then none of the inputs or outputs reference a
4319 -- state with visible refinement.
4321 return False;
4323 -- Single item
4325 else
4326 Item_Id := Entity_Of (Item);
4328 return
4329 Present (Item_Id)
4330 and then Ekind (Item_Id) = E_Abstract_State
4331 and then Has_Visible_Refinement (Item_Id);
4332 end if;
4333 end Is_Refined_State;
4335 -- Local variables
4337 Arg : constant Node_Id :=
4338 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4339 Nam : constant Name_Id := Pragma_Name (Prag);
4341 -- Start of processing for Contains_Refined_State
4343 begin
4344 if Nam = Name_Depends then
4345 return Has_State_In_Dependency (Arg);
4347 else pragma Assert (Nam = Name_Global);
4348 return Has_State_In_Global (Arg);
4349 end if;
4350 end Contains_Refined_State;
4352 -------------------------
4353 -- Copy_Component_List --
4354 -------------------------
4356 function Copy_Component_List
4357 (R_Typ : Entity_Id;
4358 Loc : Source_Ptr) return List_Id
4360 Comp : Node_Id;
4361 Comps : constant List_Id := New_List;
4363 begin
4364 Comp := First_Component (Underlying_Type (R_Typ));
4365 while Present (Comp) loop
4366 if Comes_From_Source (Comp) then
4367 declare
4368 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4369 begin
4370 Append_To (Comps,
4371 Make_Component_Declaration (Loc,
4372 Defining_Identifier =>
4373 Make_Defining_Identifier (Loc, Chars (Comp)),
4374 Component_Definition =>
4375 New_Copy_Tree
4376 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4377 end;
4378 end if;
4380 Next_Component (Comp);
4381 end loop;
4383 return Comps;
4384 end Copy_Component_List;
4386 -------------------------
4387 -- Copy_Parameter_List --
4388 -------------------------
4390 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4391 Loc : constant Source_Ptr := Sloc (Subp_Id);
4392 Plist : List_Id;
4393 Formal : Entity_Id;
4395 begin
4396 if No (First_Formal (Subp_Id)) then
4397 return No_List;
4398 else
4399 Plist := New_List;
4400 Formal := First_Formal (Subp_Id);
4401 while Present (Formal) loop
4402 Append
4403 (Make_Parameter_Specification (Loc,
4404 Defining_Identifier =>
4405 Make_Defining_Identifier (Sloc (Formal),
4406 Chars => Chars (Formal)),
4407 In_Present => In_Present (Parent (Formal)),
4408 Out_Present => Out_Present (Parent (Formal)),
4409 Parameter_Type =>
4410 New_Occurrence_Of (Etype (Formal), Loc),
4411 Expression =>
4412 New_Copy_Tree (Expression (Parent (Formal)))),
4413 Plist);
4415 Next_Formal (Formal);
4416 end loop;
4417 end if;
4419 return Plist;
4420 end Copy_Parameter_List;
4422 --------------------------------
4423 -- Corresponding_Generic_Type --
4424 --------------------------------
4426 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
4427 Inst : Entity_Id;
4428 Gen : Entity_Id;
4429 Typ : Entity_Id;
4431 begin
4432 if not Is_Generic_Actual_Type (T) then
4433 return Any_Type;
4435 -- If the actual is the actual of an enclosing instance, resolution
4436 -- was correct in the generic.
4438 elsif Nkind (Parent (T)) = N_Subtype_Declaration
4439 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
4440 and then
4441 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
4442 then
4443 return Any_Type;
4445 else
4446 Inst := Scope (T);
4448 if Is_Wrapper_Package (Inst) then
4449 Inst := Related_Instance (Inst);
4450 end if;
4452 Gen :=
4453 Generic_Parent
4454 (Specification (Unit_Declaration_Node (Inst)));
4456 -- Generic actual has the same name as the corresponding formal
4458 Typ := First_Entity (Gen);
4459 while Present (Typ) loop
4460 if Chars (Typ) = Chars (T) then
4461 return Typ;
4462 end if;
4464 Next_Entity (Typ);
4465 end loop;
4467 return Any_Type;
4468 end if;
4469 end Corresponding_Generic_Type;
4471 ---------------------------
4472 -- Corresponding_Spec_Of --
4473 ---------------------------
4475 function Corresponding_Spec_Of (Subp_Decl : Node_Id) return Entity_Id is
4476 begin
4477 if Nkind (Subp_Decl) = N_Subprogram_Body
4478 and then Present (Corresponding_Spec (Subp_Decl))
4479 then
4480 return Corresponding_Spec (Subp_Decl);
4482 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
4483 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
4484 then
4485 return Corresponding_Spec_Of_Stub (Subp_Decl);
4487 else
4488 return Defining_Entity (Subp_Decl);
4489 end if;
4490 end Corresponding_Spec_Of;
4492 --------------------
4493 -- Current_Entity --
4494 --------------------
4496 -- The currently visible definition for a given identifier is the
4497 -- one most chained at the start of the visibility chain, i.e. the
4498 -- one that is referenced by the Node_Id value of the name of the
4499 -- given identifier.
4501 function Current_Entity (N : Node_Id) return Entity_Id is
4502 begin
4503 return Get_Name_Entity_Id (Chars (N));
4504 end Current_Entity;
4506 -----------------------------
4507 -- Current_Entity_In_Scope --
4508 -----------------------------
4510 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
4511 E : Entity_Id;
4512 CS : constant Entity_Id := Current_Scope;
4514 Transient_Case : constant Boolean := Scope_Is_Transient;
4516 begin
4517 E := Get_Name_Entity_Id (Chars (N));
4518 while Present (E)
4519 and then Scope (E) /= CS
4520 and then (not Transient_Case or else Scope (E) /= Scope (CS))
4521 loop
4522 E := Homonym (E);
4523 end loop;
4525 return E;
4526 end Current_Entity_In_Scope;
4528 -------------------
4529 -- Current_Scope --
4530 -------------------
4532 function Current_Scope return Entity_Id is
4533 begin
4534 if Scope_Stack.Last = -1 then
4535 return Standard_Standard;
4536 else
4537 declare
4538 C : constant Entity_Id :=
4539 Scope_Stack.Table (Scope_Stack.Last).Entity;
4540 begin
4541 if Present (C) then
4542 return C;
4543 else
4544 return Standard_Standard;
4545 end if;
4546 end;
4547 end if;
4548 end Current_Scope;
4550 ------------------------
4551 -- Current_Subprogram --
4552 ------------------------
4554 function Current_Subprogram return Entity_Id is
4555 Scop : constant Entity_Id := Current_Scope;
4556 begin
4557 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
4558 return Scop;
4559 else
4560 return Enclosing_Subprogram (Scop);
4561 end if;
4562 end Current_Subprogram;
4564 ----------------------------------
4565 -- Deepest_Type_Access_Level --
4566 ----------------------------------
4568 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
4569 begin
4570 if Ekind (Typ) = E_Anonymous_Access_Type
4571 and then not Is_Local_Anonymous_Access (Typ)
4572 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
4573 then
4574 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
4575 -- access type.
4577 return
4578 Scope_Depth (Enclosing_Dynamic_Scope
4579 (Defining_Identifier
4580 (Associated_Node_For_Itype (Typ))));
4582 -- For generic formal type, return Int'Last (infinite).
4583 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
4585 elsif Is_Generic_Type (Root_Type (Typ)) then
4586 return UI_From_Int (Int'Last);
4588 else
4589 return Type_Access_Level (Typ);
4590 end if;
4591 end Deepest_Type_Access_Level;
4593 ---------------------
4594 -- Defining_Entity --
4595 ---------------------
4597 function Defining_Entity (N : Node_Id) return Entity_Id is
4598 K : constant Node_Kind := Nkind (N);
4599 Err : Entity_Id := Empty;
4601 begin
4602 case K is
4603 when
4604 N_Subprogram_Declaration |
4605 N_Abstract_Subprogram_Declaration |
4606 N_Subprogram_Body |
4607 N_Package_Declaration |
4608 N_Subprogram_Renaming_Declaration |
4609 N_Subprogram_Body_Stub |
4610 N_Generic_Subprogram_Declaration |
4611 N_Generic_Package_Declaration |
4612 N_Formal_Subprogram_Declaration |
4613 N_Expression_Function
4615 return Defining_Entity (Specification (N));
4617 when
4618 N_Component_Declaration |
4619 N_Defining_Program_Unit_Name |
4620 N_Discriminant_Specification |
4621 N_Entry_Body |
4622 N_Entry_Declaration |
4623 N_Entry_Index_Specification |
4624 N_Exception_Declaration |
4625 N_Exception_Renaming_Declaration |
4626 N_Formal_Object_Declaration |
4627 N_Formal_Package_Declaration |
4628 N_Formal_Type_Declaration |
4629 N_Full_Type_Declaration |
4630 N_Implicit_Label_Declaration |
4631 N_Incomplete_Type_Declaration |
4632 N_Loop_Parameter_Specification |
4633 N_Number_Declaration |
4634 N_Object_Declaration |
4635 N_Object_Renaming_Declaration |
4636 N_Package_Body_Stub |
4637 N_Parameter_Specification |
4638 N_Private_Extension_Declaration |
4639 N_Private_Type_Declaration |
4640 N_Protected_Body |
4641 N_Protected_Body_Stub |
4642 N_Protected_Type_Declaration |
4643 N_Single_Protected_Declaration |
4644 N_Single_Task_Declaration |
4645 N_Subtype_Declaration |
4646 N_Task_Body |
4647 N_Task_Body_Stub |
4648 N_Task_Type_Declaration
4650 return Defining_Identifier (N);
4652 when N_Subunit =>
4653 return Defining_Entity (Proper_Body (N));
4655 when
4656 N_Function_Instantiation |
4657 N_Function_Specification |
4658 N_Generic_Function_Renaming_Declaration |
4659 N_Generic_Package_Renaming_Declaration |
4660 N_Generic_Procedure_Renaming_Declaration |
4661 N_Package_Body |
4662 N_Package_Instantiation |
4663 N_Package_Renaming_Declaration |
4664 N_Package_Specification |
4665 N_Procedure_Instantiation |
4666 N_Procedure_Specification
4668 declare
4669 Nam : constant Node_Id := Defining_Unit_Name (N);
4671 begin
4672 if Nkind (Nam) in N_Entity then
4673 return Nam;
4675 -- For Error, make up a name and attach to declaration
4676 -- so we can continue semantic analysis
4678 elsif Nam = Error then
4679 Err := Make_Temporary (Sloc (N), 'T');
4680 Set_Defining_Unit_Name (N, Err);
4682 return Err;
4684 -- If not an entity, get defining identifier
4686 else
4687 return Defining_Identifier (Nam);
4688 end if;
4689 end;
4691 when
4692 N_Block_Statement |
4693 N_Loop_Statement
4695 return Entity (Identifier (N));
4697 when others =>
4698 raise Program_Error;
4700 end case;
4701 end Defining_Entity;
4703 --------------------------
4704 -- Denotes_Discriminant --
4705 --------------------------
4707 function Denotes_Discriminant
4708 (N : Node_Id;
4709 Check_Concurrent : Boolean := False) return Boolean
4711 E : Entity_Id;
4713 begin
4714 if not Is_Entity_Name (N) or else No (Entity (N)) then
4715 return False;
4716 else
4717 E := Entity (N);
4718 end if;
4720 -- If we are checking for a protected type, the discriminant may have
4721 -- been rewritten as the corresponding discriminal of the original type
4722 -- or of the corresponding concurrent record, depending on whether we
4723 -- are in the spec or body of the protected type.
4725 return Ekind (E) = E_Discriminant
4726 or else
4727 (Check_Concurrent
4728 and then Ekind (E) = E_In_Parameter
4729 and then Present (Discriminal_Link (E))
4730 and then
4731 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
4732 or else
4733 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
4735 end Denotes_Discriminant;
4737 -------------------------
4738 -- Denotes_Same_Object --
4739 -------------------------
4741 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
4742 Obj1 : Node_Id := A1;
4743 Obj2 : Node_Id := A2;
4745 function Has_Prefix (N : Node_Id) return Boolean;
4746 -- Return True if N has attribute Prefix
4748 function Is_Renaming (N : Node_Id) return Boolean;
4749 -- Return true if N names a renaming entity
4751 function Is_Valid_Renaming (N : Node_Id) return Boolean;
4752 -- For renamings, return False if the prefix of any dereference within
4753 -- the renamed object_name is a variable, or any expression within the
4754 -- renamed object_name contains references to variables or calls on
4755 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
4757 ----------------
4758 -- Has_Prefix --
4759 ----------------
4761 function Has_Prefix (N : Node_Id) return Boolean is
4762 begin
4763 return
4764 Nkind_In (N,
4765 N_Attribute_Reference,
4766 N_Expanded_Name,
4767 N_Explicit_Dereference,
4768 N_Indexed_Component,
4769 N_Reference,
4770 N_Selected_Component,
4771 N_Slice);
4772 end Has_Prefix;
4774 -----------------
4775 -- Is_Renaming --
4776 -----------------
4778 function Is_Renaming (N : Node_Id) return Boolean is
4779 begin
4780 return Is_Entity_Name (N)
4781 and then Present (Renamed_Entity (Entity (N)));
4782 end Is_Renaming;
4784 -----------------------
4785 -- Is_Valid_Renaming --
4786 -----------------------
4788 function Is_Valid_Renaming (N : Node_Id) return Boolean is
4790 function Check_Renaming (N : Node_Id) return Boolean;
4791 -- Recursive function used to traverse all the prefixes of N
4793 function Check_Renaming (N : Node_Id) return Boolean is
4794 begin
4795 if Is_Renaming (N)
4796 and then not Check_Renaming (Renamed_Entity (Entity (N)))
4797 then
4798 return False;
4799 end if;
4801 if Nkind (N) = N_Indexed_Component then
4802 declare
4803 Indx : Node_Id;
4805 begin
4806 Indx := First (Expressions (N));
4807 while Present (Indx) loop
4808 if not Is_OK_Static_Expression (Indx) then
4809 return False;
4810 end if;
4812 Next_Index (Indx);
4813 end loop;
4814 end;
4815 end if;
4817 if Has_Prefix (N) then
4818 declare
4819 P : constant Node_Id := Prefix (N);
4821 begin
4822 if Nkind (N) = N_Explicit_Dereference
4823 and then Is_Variable (P)
4824 then
4825 return False;
4827 elsif Is_Entity_Name (P)
4828 and then Ekind (Entity (P)) = E_Function
4829 then
4830 return False;
4832 elsif Nkind (P) = N_Function_Call then
4833 return False;
4834 end if;
4836 -- Recursion to continue traversing the prefix of the
4837 -- renaming expression
4839 return Check_Renaming (P);
4840 end;
4841 end if;
4843 return True;
4844 end Check_Renaming;
4846 -- Start of processing for Is_Valid_Renaming
4848 begin
4849 return Check_Renaming (N);
4850 end Is_Valid_Renaming;
4852 -- Start of processing for Denotes_Same_Object
4854 begin
4855 -- Both names statically denote the same stand-alone object or parameter
4856 -- (RM 6.4.1(6.5/3))
4858 if Is_Entity_Name (Obj1)
4859 and then Is_Entity_Name (Obj2)
4860 and then Entity (Obj1) = Entity (Obj2)
4861 then
4862 return True;
4863 end if;
4865 -- For renamings, the prefix of any dereference within the renamed
4866 -- object_name is not a variable, and any expression within the
4867 -- renamed object_name contains no references to variables nor
4868 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
4870 if Is_Renaming (Obj1) then
4871 if Is_Valid_Renaming (Obj1) then
4872 Obj1 := Renamed_Entity (Entity (Obj1));
4873 else
4874 return False;
4875 end if;
4876 end if;
4878 if Is_Renaming (Obj2) then
4879 if Is_Valid_Renaming (Obj2) then
4880 Obj2 := Renamed_Entity (Entity (Obj2));
4881 else
4882 return False;
4883 end if;
4884 end if;
4886 -- No match if not same node kind (such cases are handled by
4887 -- Denotes_Same_Prefix)
4889 if Nkind (Obj1) /= Nkind (Obj2) then
4890 return False;
4892 -- After handling valid renamings, one of the two names statically
4893 -- denoted a renaming declaration whose renamed object_name is known
4894 -- to denote the same object as the other (RM 6.4.1(6.10/3))
4896 elsif Is_Entity_Name (Obj1) then
4897 if Is_Entity_Name (Obj2) then
4898 return Entity (Obj1) = Entity (Obj2);
4899 else
4900 return False;
4901 end if;
4903 -- Both names are selected_components, their prefixes are known to
4904 -- denote the same object, and their selector_names denote the same
4905 -- component (RM 6.4.1(6.6/3)
4907 elsif Nkind (Obj1) = N_Selected_Component then
4908 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4909 and then
4910 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
4912 -- Both names are dereferences and the dereferenced names are known to
4913 -- denote the same object (RM 6.4.1(6.7/3))
4915 elsif Nkind (Obj1) = N_Explicit_Dereference then
4916 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
4918 -- Both names are indexed_components, their prefixes are known to denote
4919 -- the same object, and each of the pairs of corresponding index values
4920 -- are either both static expressions with the same static value or both
4921 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
4923 elsif Nkind (Obj1) = N_Indexed_Component then
4924 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
4925 return False;
4926 else
4927 declare
4928 Indx1 : Node_Id;
4929 Indx2 : Node_Id;
4931 begin
4932 Indx1 := First (Expressions (Obj1));
4933 Indx2 := First (Expressions (Obj2));
4934 while Present (Indx1) loop
4936 -- Indexes must denote the same static value or same object
4938 if Is_OK_Static_Expression (Indx1) then
4939 if not Is_OK_Static_Expression (Indx2) then
4940 return False;
4942 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
4943 return False;
4944 end if;
4946 elsif not Denotes_Same_Object (Indx1, Indx2) then
4947 return False;
4948 end if;
4950 Next (Indx1);
4951 Next (Indx2);
4952 end loop;
4954 return True;
4955 end;
4956 end if;
4958 -- Both names are slices, their prefixes are known to denote the same
4959 -- object, and the two slices have statically matching index constraints
4960 -- (RM 6.4.1(6.9/3))
4962 elsif Nkind (Obj1) = N_Slice
4963 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4964 then
4965 declare
4966 Lo1, Lo2, Hi1, Hi2 : Node_Id;
4968 begin
4969 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
4970 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
4972 -- Check whether bounds are statically identical. There is no
4973 -- attempt to detect partial overlap of slices.
4975 return Denotes_Same_Object (Lo1, Lo2)
4976 and then
4977 Denotes_Same_Object (Hi1, Hi2);
4978 end;
4980 -- In the recursion, literals appear as indexes
4982 elsif Nkind (Obj1) = N_Integer_Literal
4983 and then
4984 Nkind (Obj2) = N_Integer_Literal
4985 then
4986 return Intval (Obj1) = Intval (Obj2);
4988 else
4989 return False;
4990 end if;
4991 end Denotes_Same_Object;
4993 -------------------------
4994 -- Denotes_Same_Prefix --
4995 -------------------------
4997 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
4999 begin
5000 if Is_Entity_Name (A1) then
5001 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
5002 and then not Is_Access_Type (Etype (A1))
5003 then
5004 return Denotes_Same_Object (A1, Prefix (A2))
5005 or else Denotes_Same_Prefix (A1, Prefix (A2));
5006 else
5007 return False;
5008 end if;
5010 elsif Is_Entity_Name (A2) then
5011 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
5013 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
5014 and then
5015 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
5016 then
5017 declare
5018 Root1, Root2 : Node_Id;
5019 Depth1, Depth2 : Int := 0;
5021 begin
5022 Root1 := Prefix (A1);
5023 while not Is_Entity_Name (Root1) loop
5024 if not Nkind_In
5025 (Root1, N_Selected_Component, N_Indexed_Component)
5026 then
5027 return False;
5028 else
5029 Root1 := Prefix (Root1);
5030 end if;
5032 Depth1 := Depth1 + 1;
5033 end loop;
5035 Root2 := Prefix (A2);
5036 while not Is_Entity_Name (Root2) loop
5037 if not Nkind_In (Root2, N_Selected_Component,
5038 N_Indexed_Component)
5039 then
5040 return False;
5041 else
5042 Root2 := Prefix (Root2);
5043 end if;
5045 Depth2 := Depth2 + 1;
5046 end loop;
5048 -- If both have the same depth and they do not denote the same
5049 -- object, they are disjoint and no warning is needed.
5051 if Depth1 = Depth2 then
5052 return False;
5054 elsif Depth1 > Depth2 then
5055 Root1 := Prefix (A1);
5056 for J in 1 .. Depth1 - Depth2 - 1 loop
5057 Root1 := Prefix (Root1);
5058 end loop;
5060 return Denotes_Same_Object (Root1, A2);
5062 else
5063 Root2 := Prefix (A2);
5064 for J in 1 .. Depth2 - Depth1 - 1 loop
5065 Root2 := Prefix (Root2);
5066 end loop;
5068 return Denotes_Same_Object (A1, Root2);
5069 end if;
5070 end;
5072 else
5073 return False;
5074 end if;
5075 end Denotes_Same_Prefix;
5077 ----------------------
5078 -- Denotes_Variable --
5079 ----------------------
5081 function Denotes_Variable (N : Node_Id) return Boolean is
5082 begin
5083 return Is_Variable (N) and then Paren_Count (N) = 0;
5084 end Denotes_Variable;
5086 -----------------------------
5087 -- Depends_On_Discriminant --
5088 -----------------------------
5090 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5091 L : Node_Id;
5092 H : Node_Id;
5094 begin
5095 Get_Index_Bounds (N, L, H);
5096 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5097 end Depends_On_Discriminant;
5099 -------------------------
5100 -- Designate_Same_Unit --
5101 -------------------------
5103 function Designate_Same_Unit
5104 (Name1 : Node_Id;
5105 Name2 : Node_Id) return Boolean
5107 K1 : constant Node_Kind := Nkind (Name1);
5108 K2 : constant Node_Kind := Nkind (Name2);
5110 function Prefix_Node (N : Node_Id) return Node_Id;
5111 -- Returns the parent unit name node of a defining program unit name
5112 -- or the prefix if N is a selected component or an expanded name.
5114 function Select_Node (N : Node_Id) return Node_Id;
5115 -- Returns the defining identifier node of a defining program unit
5116 -- name or the selector node if N is a selected component or an
5117 -- expanded name.
5119 -----------------
5120 -- Prefix_Node --
5121 -----------------
5123 function Prefix_Node (N : Node_Id) return Node_Id is
5124 begin
5125 if Nkind (N) = N_Defining_Program_Unit_Name then
5126 return Name (N);
5127 else
5128 return Prefix (N);
5129 end if;
5130 end Prefix_Node;
5132 -----------------
5133 -- Select_Node --
5134 -----------------
5136 function Select_Node (N : Node_Id) return Node_Id is
5137 begin
5138 if Nkind (N) = N_Defining_Program_Unit_Name then
5139 return Defining_Identifier (N);
5140 else
5141 return Selector_Name (N);
5142 end if;
5143 end Select_Node;
5145 -- Start of processing for Designate_Same_Unit
5147 begin
5148 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
5149 and then
5150 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
5151 then
5152 return Chars (Name1) = Chars (Name2);
5154 elsif Nkind_In (K1, N_Expanded_Name,
5155 N_Selected_Component,
5156 N_Defining_Program_Unit_Name)
5157 and then
5158 Nkind_In (K2, N_Expanded_Name,
5159 N_Selected_Component,
5160 N_Defining_Program_Unit_Name)
5161 then
5162 return
5163 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5164 and then
5165 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5167 else
5168 return False;
5169 end if;
5170 end Designate_Same_Unit;
5172 ------------------------------------------
5173 -- function Dynamic_Accessibility_Level --
5174 ------------------------------------------
5176 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5177 E : Entity_Id;
5178 Loc : constant Source_Ptr := Sloc (Expr);
5180 function Make_Level_Literal (Level : Uint) return Node_Id;
5181 -- Construct an integer literal representing an accessibility level
5182 -- with its type set to Natural.
5184 ------------------------
5185 -- Make_Level_Literal --
5186 ------------------------
5188 function Make_Level_Literal (Level : Uint) return Node_Id is
5189 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5190 begin
5191 Set_Etype (Result, Standard_Natural);
5192 return Result;
5193 end Make_Level_Literal;
5195 -- Start of processing for Dynamic_Accessibility_Level
5197 begin
5198 if Is_Entity_Name (Expr) then
5199 E := Entity (Expr);
5201 if Present (Renamed_Object (E)) then
5202 return Dynamic_Accessibility_Level (Renamed_Object (E));
5203 end if;
5205 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5206 if Present (Extra_Accessibility (E)) then
5207 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5208 end if;
5209 end if;
5210 end if;
5212 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5214 case Nkind (Expr) is
5216 -- For access discriminant, the level of the enclosing object
5218 when N_Selected_Component =>
5219 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5220 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5221 E_Anonymous_Access_Type
5222 then
5223 return Make_Level_Literal (Object_Access_Level (Expr));
5224 end if;
5226 when N_Attribute_Reference =>
5227 case Get_Attribute_Id (Attribute_Name (Expr)) is
5229 -- For X'Access, the level of the prefix X
5231 when Attribute_Access =>
5232 return Make_Level_Literal
5233 (Object_Access_Level (Prefix (Expr)));
5235 -- Treat the unchecked attributes as library-level
5237 when Attribute_Unchecked_Access |
5238 Attribute_Unrestricted_Access =>
5239 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5241 -- No other access-valued attributes
5243 when others =>
5244 raise Program_Error;
5245 end case;
5247 when N_Allocator =>
5249 -- Unimplemented: depends on context. As an actual parameter where
5250 -- formal type is anonymous, use
5251 -- Scope_Depth (Current_Scope) + 1.
5252 -- For other cases, see 3.10.2(14/3) and following. ???
5254 null;
5256 when N_Type_Conversion =>
5257 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5259 -- Handle type conversions introduced for a rename of an
5260 -- Ada 2012 stand-alone object of an anonymous access type.
5262 return Dynamic_Accessibility_Level (Expression (Expr));
5263 end if;
5265 when others =>
5266 null;
5267 end case;
5269 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5270 end Dynamic_Accessibility_Level;
5272 -----------------------------------
5273 -- Effective_Extra_Accessibility --
5274 -----------------------------------
5276 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5277 begin
5278 if Present (Renamed_Object (Id))
5279 and then Is_Entity_Name (Renamed_Object (Id))
5280 then
5281 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5282 else
5283 return Extra_Accessibility (Id);
5284 end if;
5285 end Effective_Extra_Accessibility;
5287 -----------------------------
5288 -- Effective_Reads_Enabled --
5289 -----------------------------
5291 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5292 begin
5293 return Has_Enabled_Property (Id, Name_Effective_Reads);
5294 end Effective_Reads_Enabled;
5296 ------------------------------
5297 -- Effective_Writes_Enabled --
5298 ------------------------------
5300 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5301 begin
5302 return Has_Enabled_Property (Id, Name_Effective_Writes);
5303 end Effective_Writes_Enabled;
5305 ------------------------------
5306 -- Enclosing_Comp_Unit_Node --
5307 ------------------------------
5309 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5310 Current_Node : Node_Id;
5312 begin
5313 Current_Node := N;
5314 while Present (Current_Node)
5315 and then Nkind (Current_Node) /= N_Compilation_Unit
5316 loop
5317 Current_Node := Parent (Current_Node);
5318 end loop;
5320 if Nkind (Current_Node) /= N_Compilation_Unit then
5321 return Empty;
5322 else
5323 return Current_Node;
5324 end if;
5325 end Enclosing_Comp_Unit_Node;
5327 --------------------------
5328 -- Enclosing_CPP_Parent --
5329 --------------------------
5331 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5332 Parent_Typ : Entity_Id := Typ;
5334 begin
5335 while not Is_CPP_Class (Parent_Typ)
5336 and then Etype (Parent_Typ) /= Parent_Typ
5337 loop
5338 Parent_Typ := Etype (Parent_Typ);
5340 if Is_Private_Type (Parent_Typ) then
5341 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5342 end if;
5343 end loop;
5345 pragma Assert (Is_CPP_Class (Parent_Typ));
5346 return Parent_Typ;
5347 end Enclosing_CPP_Parent;
5349 ----------------------------
5350 -- Enclosing_Generic_Body --
5351 ----------------------------
5353 function Enclosing_Generic_Body
5354 (N : Node_Id) return Node_Id
5356 P : Node_Id;
5357 Decl : Node_Id;
5358 Spec : Node_Id;
5360 begin
5361 P := Parent (N);
5362 while Present (P) loop
5363 if Nkind (P) = N_Package_Body
5364 or else Nkind (P) = N_Subprogram_Body
5365 then
5366 Spec := Corresponding_Spec (P);
5368 if Present (Spec) then
5369 Decl := Unit_Declaration_Node (Spec);
5371 if Nkind (Decl) = N_Generic_Package_Declaration
5372 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5373 then
5374 return P;
5375 end if;
5376 end if;
5377 end if;
5379 P := Parent (P);
5380 end loop;
5382 return Empty;
5383 end Enclosing_Generic_Body;
5385 ----------------------------
5386 -- Enclosing_Generic_Unit --
5387 ----------------------------
5389 function Enclosing_Generic_Unit
5390 (N : Node_Id) return Node_Id
5392 P : Node_Id;
5393 Decl : Node_Id;
5394 Spec : Node_Id;
5396 begin
5397 P := Parent (N);
5398 while Present (P) loop
5399 if Nkind (P) = N_Generic_Package_Declaration
5400 or else Nkind (P) = N_Generic_Subprogram_Declaration
5401 then
5402 return P;
5404 elsif Nkind (P) = N_Package_Body
5405 or else Nkind (P) = N_Subprogram_Body
5406 then
5407 Spec := Corresponding_Spec (P);
5409 if Present (Spec) then
5410 Decl := Unit_Declaration_Node (Spec);
5412 if Nkind (Decl) = N_Generic_Package_Declaration
5413 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5414 then
5415 return Decl;
5416 end if;
5417 end if;
5418 end if;
5420 P := Parent (P);
5421 end loop;
5423 return Empty;
5424 end Enclosing_Generic_Unit;
5426 -------------------------------
5427 -- Enclosing_Lib_Unit_Entity --
5428 -------------------------------
5430 function Enclosing_Lib_Unit_Entity
5431 (E : Entity_Id := Current_Scope) return Entity_Id
5433 Unit_Entity : Entity_Id;
5435 begin
5436 -- Look for enclosing library unit entity by following scope links.
5437 -- Equivalent to, but faster than indexing through the scope stack.
5439 Unit_Entity := E;
5440 while (Present (Scope (Unit_Entity))
5441 and then Scope (Unit_Entity) /= Standard_Standard)
5442 and not Is_Child_Unit (Unit_Entity)
5443 loop
5444 Unit_Entity := Scope (Unit_Entity);
5445 end loop;
5447 return Unit_Entity;
5448 end Enclosing_Lib_Unit_Entity;
5450 -----------------------
5451 -- Enclosing_Package --
5452 -----------------------
5454 function Enclosing_Package (E : Entity_Id) return Entity_Id is
5455 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5457 begin
5458 if Dynamic_Scope = Standard_Standard then
5459 return Standard_Standard;
5461 elsif Dynamic_Scope = Empty then
5462 return Empty;
5464 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
5465 E_Generic_Package)
5466 then
5467 return Dynamic_Scope;
5469 else
5470 return Enclosing_Package (Dynamic_Scope);
5471 end if;
5472 end Enclosing_Package;
5474 --------------------------
5475 -- Enclosing_Subprogram --
5476 --------------------------
5478 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
5479 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5481 begin
5482 if Dynamic_Scope = Standard_Standard then
5483 return Empty;
5485 elsif Dynamic_Scope = Empty then
5486 return Empty;
5488 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
5489 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
5491 elsif Ekind (Dynamic_Scope) = E_Block
5492 or else Ekind (Dynamic_Scope) = E_Return_Statement
5493 then
5494 return Enclosing_Subprogram (Dynamic_Scope);
5496 elsif Ekind (Dynamic_Scope) = E_Task_Type then
5497 return Get_Task_Body_Procedure (Dynamic_Scope);
5499 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
5500 and then Present (Full_View (Dynamic_Scope))
5501 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
5502 then
5503 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
5505 -- No body is generated if the protected operation is eliminated
5507 elsif Convention (Dynamic_Scope) = Convention_Protected
5508 and then not Is_Eliminated (Dynamic_Scope)
5509 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
5510 then
5511 return Protected_Body_Subprogram (Dynamic_Scope);
5513 else
5514 return Dynamic_Scope;
5515 end if;
5516 end Enclosing_Subprogram;
5518 ------------------------
5519 -- Ensure_Freeze_Node --
5520 ------------------------
5522 procedure Ensure_Freeze_Node (E : Entity_Id) is
5523 FN : Node_Id;
5524 begin
5525 if No (Freeze_Node (E)) then
5526 FN := Make_Freeze_Entity (Sloc (E));
5527 Set_Has_Delayed_Freeze (E);
5528 Set_Freeze_Node (E, FN);
5529 Set_Access_Types_To_Process (FN, No_Elist);
5530 Set_TSS_Elist (FN, No_Elist);
5531 Set_Entity (FN, E);
5532 end if;
5533 end Ensure_Freeze_Node;
5535 ----------------
5536 -- Enter_Name --
5537 ----------------
5539 procedure Enter_Name (Def_Id : Entity_Id) is
5540 C : constant Entity_Id := Current_Entity (Def_Id);
5541 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
5542 S : constant Entity_Id := Current_Scope;
5544 begin
5545 Generate_Definition (Def_Id);
5547 -- Add new name to current scope declarations. Check for duplicate
5548 -- declaration, which may or may not be a genuine error.
5550 if Present (E) then
5552 -- Case of previous entity entered because of a missing declaration
5553 -- or else a bad subtype indication. Best is to use the new entity,
5554 -- and make the previous one invisible.
5556 if Etype (E) = Any_Type then
5557 Set_Is_Immediately_Visible (E, False);
5559 -- Case of renaming declaration constructed for package instances.
5560 -- if there is an explicit declaration with the same identifier,
5561 -- the renaming is not immediately visible any longer, but remains
5562 -- visible through selected component notation.
5564 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
5565 and then not Comes_From_Source (E)
5566 then
5567 Set_Is_Immediately_Visible (E, False);
5569 -- The new entity may be the package renaming, which has the same
5570 -- same name as a generic formal which has been seen already.
5572 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
5573 and then not Comes_From_Source (Def_Id)
5574 then
5575 Set_Is_Immediately_Visible (E, False);
5577 -- For a fat pointer corresponding to a remote access to subprogram,
5578 -- we use the same identifier as the RAS type, so that the proper
5579 -- name appears in the stub. This type is only retrieved through
5580 -- the RAS type and never by visibility, and is not added to the
5581 -- visibility list (see below).
5583 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
5584 and then Ekind (Def_Id) = E_Record_Type
5585 and then Present (Corresponding_Remote_Type (Def_Id))
5586 then
5587 null;
5589 -- Case of an implicit operation or derived literal. The new entity
5590 -- hides the implicit one, which is removed from all visibility,
5591 -- i.e. the entity list of its scope, and homonym chain of its name.
5593 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
5594 or else Is_Internal (E)
5595 then
5596 declare
5597 Prev : Entity_Id;
5598 Prev_Vis : Entity_Id;
5599 Decl : constant Node_Id := Parent (E);
5601 begin
5602 -- If E is an implicit declaration, it cannot be the first
5603 -- entity in the scope.
5605 Prev := First_Entity (Current_Scope);
5606 while Present (Prev) and then Next_Entity (Prev) /= E loop
5607 Next_Entity (Prev);
5608 end loop;
5610 if No (Prev) then
5612 -- If E is not on the entity chain of the current scope,
5613 -- it is an implicit declaration in the generic formal
5614 -- part of a generic subprogram. When analyzing the body,
5615 -- the generic formals are visible but not on the entity
5616 -- chain of the subprogram. The new entity will become
5617 -- the visible one in the body.
5619 pragma Assert
5620 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
5621 null;
5623 else
5624 Set_Next_Entity (Prev, Next_Entity (E));
5626 if No (Next_Entity (Prev)) then
5627 Set_Last_Entity (Current_Scope, Prev);
5628 end if;
5630 if E = Current_Entity (E) then
5631 Prev_Vis := Empty;
5633 else
5634 Prev_Vis := Current_Entity (E);
5635 while Homonym (Prev_Vis) /= E loop
5636 Prev_Vis := Homonym (Prev_Vis);
5637 end loop;
5638 end if;
5640 if Present (Prev_Vis) then
5642 -- Skip E in the visibility chain
5644 Set_Homonym (Prev_Vis, Homonym (E));
5646 else
5647 Set_Name_Entity_Id (Chars (E), Homonym (E));
5648 end if;
5649 end if;
5650 end;
5652 -- This section of code could use a comment ???
5654 elsif Present (Etype (E))
5655 and then Is_Concurrent_Type (Etype (E))
5656 and then E = Def_Id
5657 then
5658 return;
5660 -- If the homograph is a protected component renaming, it should not
5661 -- be hiding the current entity. Such renamings are treated as weak
5662 -- declarations.
5664 elsif Is_Prival (E) then
5665 Set_Is_Immediately_Visible (E, False);
5667 -- In this case the current entity is a protected component renaming.
5668 -- Perform minimal decoration by setting the scope and return since
5669 -- the prival should not be hiding other visible entities.
5671 elsif Is_Prival (Def_Id) then
5672 Set_Scope (Def_Id, Current_Scope);
5673 return;
5675 -- Analogous to privals, the discriminal generated for an entry index
5676 -- parameter acts as a weak declaration. Perform minimal decoration
5677 -- to avoid bogus errors.
5679 elsif Is_Discriminal (Def_Id)
5680 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
5681 then
5682 Set_Scope (Def_Id, Current_Scope);
5683 return;
5685 -- In the body or private part of an instance, a type extension may
5686 -- introduce a component with the same name as that of an actual. The
5687 -- legality rule is not enforced, but the semantics of the full type
5688 -- with two components of same name are not clear at this point???
5690 elsif In_Instance_Not_Visible then
5691 null;
5693 -- When compiling a package body, some child units may have become
5694 -- visible. They cannot conflict with local entities that hide them.
5696 elsif Is_Child_Unit (E)
5697 and then In_Open_Scopes (Scope (E))
5698 and then not Is_Immediately_Visible (E)
5699 then
5700 null;
5702 -- Conversely, with front-end inlining we may compile the parent body
5703 -- first, and a child unit subsequently. The context is now the
5704 -- parent spec, and body entities are not visible.
5706 elsif Is_Child_Unit (Def_Id)
5707 and then Is_Package_Body_Entity (E)
5708 and then not In_Package_Body (Current_Scope)
5709 then
5710 null;
5712 -- Case of genuine duplicate declaration
5714 else
5715 Error_Msg_Sloc := Sloc (E);
5717 -- If the previous declaration is an incomplete type declaration
5718 -- this may be an attempt to complete it with a private type. The
5719 -- following avoids confusing cascaded errors.
5721 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
5722 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
5723 then
5724 Error_Msg_N
5725 ("incomplete type cannot be completed with a private " &
5726 "declaration", Parent (Def_Id));
5727 Set_Is_Immediately_Visible (E, False);
5728 Set_Full_View (E, Def_Id);
5730 -- An inherited component of a record conflicts with a new
5731 -- discriminant. The discriminant is inserted first in the scope,
5732 -- but the error should be posted on it, not on the component.
5734 elsif Ekind (E) = E_Discriminant
5735 and then Present (Scope (Def_Id))
5736 and then Scope (Def_Id) /= Current_Scope
5737 then
5738 Error_Msg_Sloc := Sloc (Def_Id);
5739 Error_Msg_N ("& conflicts with declaration#", E);
5740 return;
5742 -- If the name of the unit appears in its own context clause, a
5743 -- dummy package with the name has already been created, and the
5744 -- error emitted. Try to continue quietly.
5746 elsif Error_Posted (E)
5747 and then Sloc (E) = No_Location
5748 and then Nkind (Parent (E)) = N_Package_Specification
5749 and then Current_Scope = Standard_Standard
5750 then
5751 Set_Scope (Def_Id, Current_Scope);
5752 return;
5754 else
5755 Error_Msg_N ("& conflicts with declaration#", Def_Id);
5757 -- Avoid cascaded messages with duplicate components in
5758 -- derived types.
5760 if Ekind_In (E, E_Component, E_Discriminant) then
5761 return;
5762 end if;
5763 end if;
5765 if Nkind (Parent (Parent (Def_Id))) =
5766 N_Generic_Subprogram_Declaration
5767 and then Def_Id =
5768 Defining_Entity (Specification (Parent (Parent (Def_Id))))
5769 then
5770 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
5771 end if;
5773 -- If entity is in standard, then we are in trouble, because it
5774 -- means that we have a library package with a duplicated name.
5775 -- That's hard to recover from, so abort.
5777 if S = Standard_Standard then
5778 raise Unrecoverable_Error;
5780 -- Otherwise we continue with the declaration. Having two
5781 -- identical declarations should not cause us too much trouble.
5783 else
5784 null;
5785 end if;
5786 end if;
5787 end if;
5789 -- If we fall through, declaration is OK, at least OK enough to continue
5791 -- If Def_Id is a discriminant or a record component we are in the midst
5792 -- of inheriting components in a derived record definition. Preserve
5793 -- their Ekind and Etype.
5795 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
5796 null;
5798 -- If a type is already set, leave it alone (happens when a type
5799 -- declaration is reanalyzed following a call to the optimizer).
5801 elsif Present (Etype (Def_Id)) then
5802 null;
5804 -- Otherwise, the kind E_Void insures that premature uses of the entity
5805 -- will be detected. Any_Type insures that no cascaded errors will occur
5807 else
5808 Set_Ekind (Def_Id, E_Void);
5809 Set_Etype (Def_Id, Any_Type);
5810 end if;
5812 -- Inherited discriminants and components in derived record types are
5813 -- immediately visible. Itypes are not.
5815 -- Unless the Itype is for a record type with a corresponding remote
5816 -- type (what is that about, it was not commented ???)
5818 if Ekind_In (Def_Id, E_Discriminant, E_Component)
5819 or else
5820 ((not Is_Record_Type (Def_Id)
5821 or else No (Corresponding_Remote_Type (Def_Id)))
5822 and then not Is_Itype (Def_Id))
5823 then
5824 Set_Is_Immediately_Visible (Def_Id);
5825 Set_Current_Entity (Def_Id);
5826 end if;
5828 Set_Homonym (Def_Id, C);
5829 Append_Entity (Def_Id, S);
5830 Set_Public_Status (Def_Id);
5832 -- Declaring a homonym is not allowed in SPARK ...
5834 if Present (C) and then Restriction_Check_Required (SPARK_05) then
5835 declare
5836 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
5837 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
5838 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
5840 begin
5841 -- ... unless the new declaration is in a subprogram, and the
5842 -- visible declaration is a variable declaration or a parameter
5843 -- specification outside that subprogram.
5845 if Present (Enclosing_Subp)
5846 and then Nkind_In (Parent (C), N_Object_Declaration,
5847 N_Parameter_Specification)
5848 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
5849 then
5850 null;
5852 -- ... or the new declaration is in a package, and the visible
5853 -- declaration occurs outside that package.
5855 elsif Present (Enclosing_Pack)
5856 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
5857 then
5858 null;
5860 -- ... or the new declaration is a component declaration in a
5861 -- record type definition.
5863 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
5864 null;
5866 -- Don't issue error for non-source entities
5868 elsif Comes_From_Source (Def_Id)
5869 and then Comes_From_Source (C)
5870 then
5871 Error_Msg_Sloc := Sloc (C);
5872 Check_SPARK_05_Restriction
5873 ("redeclaration of identifier &#", Def_Id);
5874 end if;
5875 end;
5876 end if;
5878 -- Warn if new entity hides an old one
5880 if Warn_On_Hiding and then Present (C)
5882 -- Don't warn for record components since they always have a well
5883 -- defined scope which does not confuse other uses. Note that in
5884 -- some cases, Ekind has not been set yet.
5886 and then Ekind (C) /= E_Component
5887 and then Ekind (C) /= E_Discriminant
5888 and then Nkind (Parent (C)) /= N_Component_Declaration
5889 and then Ekind (Def_Id) /= E_Component
5890 and then Ekind (Def_Id) /= E_Discriminant
5891 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
5893 -- Don't warn for one character variables. It is too common to use
5894 -- such variables as locals and will just cause too many false hits.
5896 and then Length_Of_Name (Chars (C)) /= 1
5898 -- Don't warn for non-source entities
5900 and then Comes_From_Source (C)
5901 and then Comes_From_Source (Def_Id)
5903 -- Don't warn unless entity in question is in extended main source
5905 and then In_Extended_Main_Source_Unit (Def_Id)
5907 -- Finally, the hidden entity must be either immediately visible or
5908 -- use visible (i.e. from a used package).
5910 and then
5911 (Is_Immediately_Visible (C)
5912 or else
5913 Is_Potentially_Use_Visible (C))
5914 then
5915 Error_Msg_Sloc := Sloc (C);
5916 Error_Msg_N ("declaration hides &#?h?", Def_Id);
5917 end if;
5918 end Enter_Name;
5920 ---------------
5921 -- Entity_Of --
5922 ---------------
5924 function Entity_Of (N : Node_Id) return Entity_Id is
5925 Id : Entity_Id;
5927 begin
5928 Id := Empty;
5930 if Is_Entity_Name (N) then
5931 Id := Entity (N);
5933 -- Follow a possible chain of renamings to reach the root renamed
5934 -- object.
5936 while Present (Id) and then Present (Renamed_Object (Id)) loop
5937 if Is_Entity_Name (Renamed_Object (Id)) then
5938 Id := Entity (Renamed_Object (Id));
5939 else
5940 Id := Empty;
5941 exit;
5942 end if;
5943 end loop;
5944 end if;
5946 return Id;
5947 end Entity_Of;
5949 --------------------------
5950 -- Explain_Limited_Type --
5951 --------------------------
5953 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
5954 C : Entity_Id;
5956 begin
5957 -- For array, component type must be limited
5959 if Is_Array_Type (T) then
5960 Error_Msg_Node_2 := T;
5961 Error_Msg_NE
5962 ("\component type& of type& is limited", N, Component_Type (T));
5963 Explain_Limited_Type (Component_Type (T), N);
5965 elsif Is_Record_Type (T) then
5967 -- No need for extra messages if explicit limited record
5969 if Is_Limited_Record (Base_Type (T)) then
5970 return;
5971 end if;
5973 -- Otherwise find a limited component. Check only components that
5974 -- come from source, or inherited components that appear in the
5975 -- source of the ancestor.
5977 C := First_Component (T);
5978 while Present (C) loop
5979 if Is_Limited_Type (Etype (C))
5980 and then
5981 (Comes_From_Source (C)
5982 or else
5983 (Present (Original_Record_Component (C))
5984 and then
5985 Comes_From_Source (Original_Record_Component (C))))
5986 then
5987 Error_Msg_Node_2 := T;
5988 Error_Msg_NE ("\component& of type& has limited type", N, C);
5989 Explain_Limited_Type (Etype (C), N);
5990 return;
5991 end if;
5993 Next_Component (C);
5994 end loop;
5996 -- The type may be declared explicitly limited, even if no component
5997 -- of it is limited, in which case we fall out of the loop.
5998 return;
5999 end if;
6000 end Explain_Limited_Type;
6002 -------------------------------
6003 -- Extensions_Visible_Status --
6004 -------------------------------
6006 function Extensions_Visible_Status
6007 (Id : Entity_Id) return Extensions_Visible_Mode
6009 Arg : Node_Id;
6010 Decl : Node_Id;
6011 Expr : Node_Id;
6012 Prag : Node_Id;
6013 Subp : Entity_Id;
6015 begin
6016 -- When a formal parameter is subject to Extensions_Visible, the pragma
6017 -- is stored in the contract of related subprogram.
6019 if Is_Formal (Id) then
6020 Subp := Scope (Id);
6022 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
6023 Subp := Id;
6025 -- No other construct carries this pragma
6027 else
6028 return Extensions_Visible_None;
6029 end if;
6031 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
6033 -- In certain cases analysis may request the Extensions_Visible status
6034 -- of an expression function before the pragma has been analyzed yet.
6035 -- Inspect the declarative items after the expression function looking
6036 -- for the pragma (if any).
6038 if No (Prag) and then Is_Expression_Function (Subp) then
6039 Decl := Next (Unit_Declaration_Node (Subp));
6040 while Present (Decl) loop
6041 if Nkind (Decl) = N_Pragma
6042 and then Pragma_Name (Decl) = Name_Extensions_Visible
6043 then
6044 Prag := Decl;
6045 exit;
6047 -- A source construct ends the region where Extensions_Visible may
6048 -- appear, stop the traversal. An expanded expression function is
6049 -- no longer a source construct, but it must still be recognized.
6051 elsif Comes_From_Source (Decl)
6052 or else
6053 (Nkind_In (Decl, N_Subprogram_Body,
6054 N_Subprogram_Declaration)
6055 and then Is_Expression_Function (Defining_Entity (Decl)))
6056 then
6057 exit;
6058 end if;
6060 Next (Decl);
6061 end loop;
6062 end if;
6064 -- Extract the value from the Boolean expression (if any)
6066 if Present (Prag) then
6067 Arg := First (Pragma_Argument_Associations (Prag));
6069 if Present (Arg) then
6070 Expr := Get_Pragma_Arg (Arg);
6072 -- When the associated subprogram is an expression function, the
6073 -- argument of the pragma may not have been analyzed.
6075 if not Analyzed (Expr) then
6076 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6077 end if;
6079 -- Guard against cascading errors when the argument of pragma
6080 -- Extensions_Visible is not a valid static Boolean expression.
6082 if Error_Posted (Expr) then
6083 return Extensions_Visible_None;
6085 elsif Is_True (Expr_Value (Expr)) then
6086 return Extensions_Visible_True;
6088 else
6089 return Extensions_Visible_False;
6090 end if;
6092 -- Otherwise the aspect or pragma defaults to True
6094 else
6095 return Extensions_Visible_True;
6096 end if;
6098 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
6099 -- directly specified. In SPARK code, its value defaults to "False".
6101 elsif SPARK_Mode = On then
6102 return Extensions_Visible_False;
6104 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
6105 -- "True".
6107 else
6108 return Extensions_Visible_True;
6109 end if;
6110 end Extensions_Visible_Status;
6112 -----------------
6113 -- Find_Actual --
6114 -----------------
6116 procedure Find_Actual
6117 (N : Node_Id;
6118 Formal : out Entity_Id;
6119 Call : out Node_Id)
6121 Parnt : constant Node_Id := Parent (N);
6122 Actual : Node_Id;
6124 begin
6125 if Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6126 and then N = Prefix (Parnt)
6127 then
6128 Find_Actual (Parnt, Formal, Call);
6129 return;
6131 elsif Nkind (Parnt) = N_Parameter_Association
6132 and then N = Explicit_Actual_Parameter (Parnt)
6133 then
6134 Call := Parent (Parnt);
6136 elsif Nkind (Parnt) in N_Subprogram_Call then
6137 Call := Parnt;
6139 else
6140 Formal := Empty;
6141 Call := Empty;
6142 return;
6143 end if;
6145 -- If we have a call to a subprogram look for the parameter. Note that
6146 -- we exclude overloaded calls, since we don't know enough to be sure
6147 -- of giving the right answer in this case.
6149 if Nkind_In (Call, N_Function_Call, N_Procedure_Call_Statement)
6150 and then Is_Entity_Name (Name (Call))
6151 and then Present (Entity (Name (Call)))
6152 and then Is_Overloadable (Entity (Name (Call)))
6153 and then not Is_Overloaded (Name (Call))
6154 then
6155 -- If node is name in call it is not an actual
6157 if N = Name (Call) then
6158 Call := Empty;
6159 Formal := Empty;
6160 return;
6161 end if;
6163 -- Fall here if we are definitely a parameter
6165 Actual := First_Actual (Call);
6166 Formal := First_Formal (Entity (Name (Call)));
6167 while Present (Formal) and then Present (Actual) loop
6168 if Actual = N then
6169 return;
6171 -- An actual that is the prefix in a prefixed call may have
6172 -- been rewritten in the call, after the deferred reference
6173 -- was collected. Check if sloc and kinds and names match.
6175 elsif Sloc (Actual) = Sloc (N)
6176 and then Nkind (Actual) = N_Identifier
6177 and then Nkind (Actual) = Nkind (N)
6178 and then Chars (Actual) = Chars (N)
6179 then
6180 return;
6182 else
6183 Actual := Next_Actual (Actual);
6184 Formal := Next_Formal (Formal);
6185 end if;
6186 end loop;
6187 end if;
6189 -- Fall through here if we did not find matching actual
6191 Formal := Empty;
6192 Call := Empty;
6193 end Find_Actual;
6195 ---------------------------
6196 -- Find_Body_Discriminal --
6197 ---------------------------
6199 function Find_Body_Discriminal
6200 (Spec_Discriminant : Entity_Id) return Entity_Id
6202 Tsk : Entity_Id;
6203 Disc : Entity_Id;
6205 begin
6206 -- If expansion is suppressed, then the scope can be the concurrent type
6207 -- itself rather than a corresponding concurrent record type.
6209 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6210 Tsk := Scope (Spec_Discriminant);
6212 else
6213 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6215 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6216 end if;
6218 -- Find discriminant of original concurrent type, and use its current
6219 -- discriminal, which is the renaming within the task/protected body.
6221 Disc := First_Discriminant (Tsk);
6222 while Present (Disc) loop
6223 if Chars (Disc) = Chars (Spec_Discriminant) then
6224 return Discriminal (Disc);
6225 end if;
6227 Next_Discriminant (Disc);
6228 end loop;
6230 -- That loop should always succeed in finding a matching entry and
6231 -- returning. Fatal error if not.
6233 raise Program_Error;
6234 end Find_Body_Discriminal;
6236 -------------------------------------
6237 -- Find_Corresponding_Discriminant --
6238 -------------------------------------
6240 function Find_Corresponding_Discriminant
6241 (Id : Node_Id;
6242 Typ : Entity_Id) return Entity_Id
6244 Par_Disc : Entity_Id;
6245 Old_Disc : Entity_Id;
6246 New_Disc : Entity_Id;
6248 begin
6249 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6251 -- The original type may currently be private, and the discriminant
6252 -- only appear on its full view.
6254 if Is_Private_Type (Scope (Par_Disc))
6255 and then not Has_Discriminants (Scope (Par_Disc))
6256 and then Present (Full_View (Scope (Par_Disc)))
6257 then
6258 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6259 else
6260 Old_Disc := First_Discriminant (Scope (Par_Disc));
6261 end if;
6263 if Is_Class_Wide_Type (Typ) then
6264 New_Disc := First_Discriminant (Root_Type (Typ));
6265 else
6266 New_Disc := First_Discriminant (Typ);
6267 end if;
6269 while Present (Old_Disc) and then Present (New_Disc) loop
6270 if Old_Disc = Par_Disc then
6271 return New_Disc;
6272 end if;
6274 Next_Discriminant (Old_Disc);
6275 Next_Discriminant (New_Disc);
6276 end loop;
6278 -- Should always find it
6280 raise Program_Error;
6281 end Find_Corresponding_Discriminant;
6283 ----------------------------------
6284 -- Find_Enclosing_Iterator_Loop --
6285 ----------------------------------
6287 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
6288 Constr : Node_Id;
6289 S : Entity_Id;
6291 begin
6292 -- Traverse the scope chain looking for an iterator loop. Such loops are
6293 -- usually transformed into blocks, hence the use of Original_Node.
6295 S := Id;
6296 while Present (S) and then S /= Standard_Standard loop
6297 if Ekind (S) = E_Loop
6298 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
6299 then
6300 Constr := Original_Node (Label_Construct (Parent (S)));
6302 if Nkind (Constr) = N_Loop_Statement
6303 and then Present (Iteration_Scheme (Constr))
6304 and then Nkind (Iterator_Specification
6305 (Iteration_Scheme (Constr))) =
6306 N_Iterator_Specification
6307 then
6308 return S;
6309 end if;
6310 end if;
6312 S := Scope (S);
6313 end loop;
6315 return Empty;
6316 end Find_Enclosing_Iterator_Loop;
6318 ------------------------------------
6319 -- Find_Loop_In_Conditional_Block --
6320 ------------------------------------
6322 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
6323 Stmt : Node_Id;
6325 begin
6326 Stmt := N;
6328 if Nkind (Stmt) = N_If_Statement then
6329 Stmt := First (Then_Statements (Stmt));
6330 end if;
6332 pragma Assert (Nkind (Stmt) = N_Block_Statement);
6334 -- Inspect the statements of the conditional block. In general the loop
6335 -- should be the first statement in the statement sequence of the block,
6336 -- but the finalization machinery may have introduced extra object
6337 -- declarations.
6339 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
6340 while Present (Stmt) loop
6341 if Nkind (Stmt) = N_Loop_Statement then
6342 return Stmt;
6343 end if;
6345 Next (Stmt);
6346 end loop;
6348 -- The expansion of attribute 'Loop_Entry produced a malformed block
6350 raise Program_Error;
6351 end Find_Loop_In_Conditional_Block;
6353 --------------------------
6354 -- Find_Overlaid_Entity --
6355 --------------------------
6357 procedure Find_Overlaid_Entity
6358 (N : Node_Id;
6359 Ent : out Entity_Id;
6360 Off : out Boolean)
6362 Expr : Node_Id;
6364 begin
6365 -- We are looking for one of the two following forms:
6367 -- for X'Address use Y'Address
6369 -- or
6371 -- Const : constant Address := expr;
6372 -- ...
6373 -- for X'Address use Const;
6375 -- In the second case, the expr is either Y'Address, or recursively a
6376 -- constant that eventually references Y'Address.
6378 Ent := Empty;
6379 Off := False;
6381 if Nkind (N) = N_Attribute_Definition_Clause
6382 and then Chars (N) = Name_Address
6383 then
6384 Expr := Expression (N);
6386 -- This loop checks the form of the expression for Y'Address,
6387 -- using recursion to deal with intermediate constants.
6389 loop
6390 -- Check for Y'Address
6392 if Nkind (Expr) = N_Attribute_Reference
6393 and then Attribute_Name (Expr) = Name_Address
6394 then
6395 Expr := Prefix (Expr);
6396 exit;
6398 -- Check for Const where Const is a constant entity
6400 elsif Is_Entity_Name (Expr)
6401 and then Ekind (Entity (Expr)) = E_Constant
6402 then
6403 Expr := Constant_Value (Entity (Expr));
6405 -- Anything else does not need checking
6407 else
6408 return;
6409 end if;
6410 end loop;
6412 -- This loop checks the form of the prefix for an entity, using
6413 -- recursion to deal with intermediate components.
6415 loop
6416 -- Check for Y where Y is an entity
6418 if Is_Entity_Name (Expr) then
6419 Ent := Entity (Expr);
6420 return;
6422 -- Check for components
6424 elsif
6425 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
6426 then
6427 Expr := Prefix (Expr);
6428 Off := True;
6430 -- Anything else does not need checking
6432 else
6433 return;
6434 end if;
6435 end loop;
6436 end if;
6437 end Find_Overlaid_Entity;
6439 -------------------------
6440 -- Find_Parameter_Type --
6441 -------------------------
6443 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
6444 begin
6445 if Nkind (Param) /= N_Parameter_Specification then
6446 return Empty;
6448 -- For an access parameter, obtain the type from the formal entity
6449 -- itself, because access to subprogram nodes do not carry a type.
6450 -- Shouldn't we always use the formal entity ???
6452 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
6453 return Etype (Defining_Identifier (Param));
6455 else
6456 return Etype (Parameter_Type (Param));
6457 end if;
6458 end Find_Parameter_Type;
6460 -----------------------------------
6461 -- Find_Placement_In_State_Space --
6462 -----------------------------------
6464 procedure Find_Placement_In_State_Space
6465 (Item_Id : Entity_Id;
6466 Placement : out State_Space_Kind;
6467 Pack_Id : out Entity_Id)
6469 Context : Entity_Id;
6471 begin
6472 -- Assume that the item does not appear in the state space of a package
6474 Placement := Not_In_Package;
6475 Pack_Id := Empty;
6477 -- Climb the scope stack and examine the enclosing context
6479 Context := Scope (Item_Id);
6480 while Present (Context) and then Context /= Standard_Standard loop
6481 if Ekind (Context) = E_Package then
6482 Pack_Id := Context;
6484 -- A package body is a cut off point for the traversal as the item
6485 -- cannot be visible to the outside from this point on. Note that
6486 -- this test must be done first as a body is also classified as a
6487 -- private part.
6489 if In_Package_Body (Context) then
6490 Placement := Body_State_Space;
6491 return;
6493 -- The private part of a package is a cut off point for the
6494 -- traversal as the item cannot be visible to the outside from
6495 -- this point on.
6497 elsif In_Private_Part (Context) then
6498 Placement := Private_State_Space;
6499 return;
6501 -- When the item appears in the visible state space of a package,
6502 -- continue to climb the scope stack as this may not be the final
6503 -- state space.
6505 else
6506 Placement := Visible_State_Space;
6508 -- The visible state space of a child unit acts as the proper
6509 -- placement of an item.
6511 if Is_Child_Unit (Context) then
6512 return;
6513 end if;
6514 end if;
6516 -- The item or its enclosing package appear in a construct that has
6517 -- no state space.
6519 else
6520 Placement := Not_In_Package;
6521 return;
6522 end if;
6524 Context := Scope (Context);
6525 end loop;
6526 end Find_Placement_In_State_Space;
6528 ------------------------
6529 -- Find_Specific_Type --
6530 ------------------------
6532 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
6533 Typ : Entity_Id := Root_Type (CW);
6535 begin
6536 if Ekind (Typ) = E_Incomplete_Type then
6537 if From_Limited_With (Typ) then
6538 Typ := Non_Limited_View (Typ);
6539 else
6540 Typ := Full_View (Typ);
6541 end if;
6542 end if;
6544 if Is_Private_Type (Typ)
6545 and then not Is_Tagged_Type (Typ)
6546 and then Present (Full_View (Typ))
6547 then
6548 return Full_View (Typ);
6549 else
6550 return Typ;
6551 end if;
6552 end Find_Specific_Type;
6554 -----------------------------
6555 -- Find_Static_Alternative --
6556 -----------------------------
6558 function Find_Static_Alternative (N : Node_Id) return Node_Id is
6559 Expr : constant Node_Id := Expression (N);
6560 Val : constant Uint := Expr_Value (Expr);
6561 Alt : Node_Id;
6562 Choice : Node_Id;
6564 begin
6565 Alt := First (Alternatives (N));
6567 Search : loop
6568 if Nkind (Alt) /= N_Pragma then
6569 Choice := First (Discrete_Choices (Alt));
6570 while Present (Choice) loop
6572 -- Others choice, always matches
6574 if Nkind (Choice) = N_Others_Choice then
6575 exit Search;
6577 -- Range, check if value is in the range
6579 elsif Nkind (Choice) = N_Range then
6580 exit Search when
6581 Val >= Expr_Value (Low_Bound (Choice))
6582 and then
6583 Val <= Expr_Value (High_Bound (Choice));
6585 -- Choice is a subtype name. Note that we know it must
6586 -- be a static subtype, since otherwise it would have
6587 -- been diagnosed as illegal.
6589 elsif Is_Entity_Name (Choice)
6590 and then Is_Type (Entity (Choice))
6591 then
6592 exit Search when Is_In_Range (Expr, Etype (Choice),
6593 Assume_Valid => False);
6595 -- Choice is a subtype indication
6597 elsif Nkind (Choice) = N_Subtype_Indication then
6598 declare
6599 C : constant Node_Id := Constraint (Choice);
6600 R : constant Node_Id := Range_Expression (C);
6602 begin
6603 exit Search when
6604 Val >= Expr_Value (Low_Bound (R))
6605 and then
6606 Val <= Expr_Value (High_Bound (R));
6607 end;
6609 -- Choice is a simple expression
6611 else
6612 exit Search when Val = Expr_Value (Choice);
6613 end if;
6615 Next (Choice);
6616 end loop;
6617 end if;
6619 Next (Alt);
6620 pragma Assert (Present (Alt));
6621 end loop Search;
6623 -- The above loop *must* terminate by finding a match, since
6624 -- we know the case statement is valid, and the value of the
6625 -- expression is known at compile time. When we fall out of
6626 -- the loop, Alt points to the alternative that we know will
6627 -- be selected at run time.
6629 return Alt;
6630 end Find_Static_Alternative;
6632 ------------------
6633 -- First_Actual --
6634 ------------------
6636 function First_Actual (Node : Node_Id) return Node_Id is
6637 N : Node_Id;
6639 begin
6640 if No (Parameter_Associations (Node)) then
6641 return Empty;
6642 end if;
6644 N := First (Parameter_Associations (Node));
6646 if Nkind (N) = N_Parameter_Association then
6647 return First_Named_Actual (Node);
6648 else
6649 return N;
6650 end if;
6651 end First_Actual;
6653 -----------------------
6654 -- Gather_Components --
6655 -----------------------
6657 procedure Gather_Components
6658 (Typ : Entity_Id;
6659 Comp_List : Node_Id;
6660 Governed_By : List_Id;
6661 Into : Elist_Id;
6662 Report_Errors : out Boolean)
6664 Assoc : Node_Id;
6665 Variant : Node_Id;
6666 Discrete_Choice : Node_Id;
6667 Comp_Item : Node_Id;
6669 Discrim : Entity_Id;
6670 Discrim_Name : Node_Id;
6671 Discrim_Value : Node_Id;
6673 begin
6674 Report_Errors := False;
6676 if No (Comp_List) or else Null_Present (Comp_List) then
6677 return;
6679 elsif Present (Component_Items (Comp_List)) then
6680 Comp_Item := First (Component_Items (Comp_List));
6682 else
6683 Comp_Item := Empty;
6684 end if;
6686 while Present (Comp_Item) loop
6688 -- Skip the tag of a tagged record, the interface tags, as well
6689 -- as all items that are not user components (anonymous types,
6690 -- rep clauses, Parent field, controller field).
6692 if Nkind (Comp_Item) = N_Component_Declaration then
6693 declare
6694 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
6695 begin
6696 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
6697 Append_Elmt (Comp, Into);
6698 end if;
6699 end;
6700 end if;
6702 Next (Comp_Item);
6703 end loop;
6705 if No (Variant_Part (Comp_List)) then
6706 return;
6707 else
6708 Discrim_Name := Name (Variant_Part (Comp_List));
6709 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
6710 end if;
6712 -- Look for the discriminant that governs this variant part.
6713 -- The discriminant *must* be in the Governed_By List
6715 Assoc := First (Governed_By);
6716 Find_Constraint : loop
6717 Discrim := First (Choices (Assoc));
6718 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
6719 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
6720 and then
6721 Chars (Corresponding_Discriminant (Entity (Discrim))) =
6722 Chars (Discrim_Name))
6723 or else Chars (Original_Record_Component (Entity (Discrim)))
6724 = Chars (Discrim_Name);
6726 if No (Next (Assoc)) then
6727 if not Is_Constrained (Typ)
6728 and then Is_Derived_Type (Typ)
6729 and then Present (Stored_Constraint (Typ))
6730 then
6731 -- If the type is a tagged type with inherited discriminants,
6732 -- use the stored constraint on the parent in order to find
6733 -- the values of discriminants that are otherwise hidden by an
6734 -- explicit constraint. Renamed discriminants are handled in
6735 -- the code above.
6737 -- If several parent discriminants are renamed by a single
6738 -- discriminant of the derived type, the call to obtain the
6739 -- Corresponding_Discriminant field only retrieves the last
6740 -- of them. We recover the constraint on the others from the
6741 -- Stored_Constraint as well.
6743 declare
6744 D : Entity_Id;
6745 C : Elmt_Id;
6747 begin
6748 D := First_Discriminant (Etype (Typ));
6749 C := First_Elmt (Stored_Constraint (Typ));
6750 while Present (D) and then Present (C) loop
6751 if Chars (Discrim_Name) = Chars (D) then
6752 if Is_Entity_Name (Node (C))
6753 and then Entity (Node (C)) = Entity (Discrim)
6754 then
6755 -- D is renamed by Discrim, whose value is given in
6756 -- Assoc.
6758 null;
6760 else
6761 Assoc :=
6762 Make_Component_Association (Sloc (Typ),
6763 New_List
6764 (New_Occurrence_Of (D, Sloc (Typ))),
6765 Duplicate_Subexpr_No_Checks (Node (C)));
6766 end if;
6767 exit Find_Constraint;
6768 end if;
6770 Next_Discriminant (D);
6771 Next_Elmt (C);
6772 end loop;
6773 end;
6774 end if;
6775 end if;
6777 if No (Next (Assoc)) then
6778 Error_Msg_NE (" missing value for discriminant&",
6779 First (Governed_By), Discrim_Name);
6780 Report_Errors := True;
6781 return;
6782 end if;
6784 Next (Assoc);
6785 end loop Find_Constraint;
6787 Discrim_Value := Expression (Assoc);
6789 if not Is_OK_Static_Expression (Discrim_Value) then
6791 -- If the variant part is governed by a discriminant of the type
6792 -- this is an error. If the variant part and the discriminant are
6793 -- inherited from an ancestor this is legal (AI05-120) unless the
6794 -- components are being gathered for an aggregate, in which case
6795 -- the caller must check Report_Errors.
6797 if Scope (Original_Record_Component
6798 ((Entity (First (Choices (Assoc)))))) = Typ
6799 then
6800 Error_Msg_FE
6801 ("value for discriminant & must be static!",
6802 Discrim_Value, Discrim);
6803 Why_Not_Static (Discrim_Value);
6804 end if;
6806 Report_Errors := True;
6807 return;
6808 end if;
6810 Search_For_Discriminant_Value : declare
6811 Low : Node_Id;
6812 High : Node_Id;
6814 UI_High : Uint;
6815 UI_Low : Uint;
6816 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
6818 begin
6819 Find_Discrete_Value : while Present (Variant) loop
6820 Discrete_Choice := First (Discrete_Choices (Variant));
6821 while Present (Discrete_Choice) loop
6822 exit Find_Discrete_Value when
6823 Nkind (Discrete_Choice) = N_Others_Choice;
6825 Get_Index_Bounds (Discrete_Choice, Low, High);
6827 UI_Low := Expr_Value (Low);
6828 UI_High := Expr_Value (High);
6830 exit Find_Discrete_Value when
6831 UI_Low <= UI_Discrim_Value
6832 and then
6833 UI_High >= UI_Discrim_Value;
6835 Next (Discrete_Choice);
6836 end loop;
6838 Next_Non_Pragma (Variant);
6839 end loop Find_Discrete_Value;
6840 end Search_For_Discriminant_Value;
6842 if No (Variant) then
6843 Error_Msg_NE
6844 ("value of discriminant & is out of range", Discrim_Value, Discrim);
6845 Report_Errors := True;
6846 return;
6847 end if;
6849 -- If we have found the corresponding choice, recursively add its
6850 -- components to the Into list.
6852 Gather_Components
6853 (Empty, Component_List (Variant), Governed_By, Into, Report_Errors);
6854 end Gather_Components;
6856 ------------------------
6857 -- Get_Actual_Subtype --
6858 ------------------------
6860 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
6861 Typ : constant Entity_Id := Etype (N);
6862 Utyp : Entity_Id := Underlying_Type (Typ);
6863 Decl : Node_Id;
6864 Atyp : Entity_Id;
6866 begin
6867 if No (Utyp) then
6868 Utyp := Typ;
6869 end if;
6871 -- If what we have is an identifier that references a subprogram
6872 -- formal, or a variable or constant object, then we get the actual
6873 -- subtype from the referenced entity if one has been built.
6875 if Nkind (N) = N_Identifier
6876 and then
6877 (Is_Formal (Entity (N))
6878 or else Ekind (Entity (N)) = E_Constant
6879 or else Ekind (Entity (N)) = E_Variable)
6880 and then Present (Actual_Subtype (Entity (N)))
6881 then
6882 return Actual_Subtype (Entity (N));
6884 -- Actual subtype of unchecked union is always itself. We never need
6885 -- the "real" actual subtype. If we did, we couldn't get it anyway
6886 -- because the discriminant is not available. The restrictions on
6887 -- Unchecked_Union are designed to make sure that this is OK.
6889 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
6890 return Typ;
6892 -- Here for the unconstrained case, we must find actual subtype
6893 -- No actual subtype is available, so we must build it on the fly.
6895 -- Checking the type, not the underlying type, for constrainedness
6896 -- seems to be necessary. Maybe all the tests should be on the type???
6898 elsif (not Is_Constrained (Typ))
6899 and then (Is_Array_Type (Utyp)
6900 or else (Is_Record_Type (Utyp)
6901 and then Has_Discriminants (Utyp)))
6902 and then not Has_Unknown_Discriminants (Utyp)
6903 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
6904 then
6905 -- Nothing to do if in spec expression (why not???)
6907 if In_Spec_Expression then
6908 return Typ;
6910 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
6912 -- If the type has no discriminants, there is no subtype to
6913 -- build, even if the underlying type is discriminated.
6915 return Typ;
6917 -- Else build the actual subtype
6919 else
6920 Decl := Build_Actual_Subtype (Typ, N);
6921 Atyp := Defining_Identifier (Decl);
6923 -- If Build_Actual_Subtype generated a new declaration then use it
6925 if Atyp /= Typ then
6927 -- The actual subtype is an Itype, so analyze the declaration,
6928 -- but do not attach it to the tree, to get the type defined.
6930 Set_Parent (Decl, N);
6931 Set_Is_Itype (Atyp);
6932 Analyze (Decl, Suppress => All_Checks);
6933 Set_Associated_Node_For_Itype (Atyp, N);
6934 Set_Has_Delayed_Freeze (Atyp, False);
6936 -- We need to freeze the actual subtype immediately. This is
6937 -- needed, because otherwise this Itype will not get frozen
6938 -- at all, and it is always safe to freeze on creation because
6939 -- any associated types must be frozen at this point.
6941 Freeze_Itype (Atyp, N);
6942 return Atyp;
6944 -- Otherwise we did not build a declaration, so return original
6946 else
6947 return Typ;
6948 end if;
6949 end if;
6951 -- For all remaining cases, the actual subtype is the same as
6952 -- the nominal type.
6954 else
6955 return Typ;
6956 end if;
6957 end Get_Actual_Subtype;
6959 -------------------------------------
6960 -- Get_Actual_Subtype_If_Available --
6961 -------------------------------------
6963 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
6964 Typ : constant Entity_Id := Etype (N);
6966 begin
6967 -- If what we have is an identifier that references a subprogram
6968 -- formal, or a variable or constant object, then we get the actual
6969 -- subtype from the referenced entity if one has been built.
6971 if Nkind (N) = N_Identifier
6972 and then
6973 (Is_Formal (Entity (N))
6974 or else Ekind (Entity (N)) = E_Constant
6975 or else Ekind (Entity (N)) = E_Variable)
6976 and then Present (Actual_Subtype (Entity (N)))
6977 then
6978 return Actual_Subtype (Entity (N));
6980 -- Otherwise the Etype of N is returned unchanged
6982 else
6983 return Typ;
6984 end if;
6985 end Get_Actual_Subtype_If_Available;
6987 ------------------------
6988 -- Get_Body_From_Stub --
6989 ------------------------
6991 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
6992 begin
6993 return Proper_Body (Unit (Library_Unit (N)));
6994 end Get_Body_From_Stub;
6996 ---------------------
6997 -- Get_Cursor_Type --
6998 ---------------------
7000 function Get_Cursor_Type
7001 (Aspect : Node_Id;
7002 Typ : Entity_Id) return Entity_Id
7004 Assoc : Node_Id;
7005 Func : Entity_Id;
7006 First_Op : Entity_Id;
7007 Cursor : Entity_Id;
7009 begin
7010 -- If error already detected, return
7012 if Error_Posted (Aspect) then
7013 return Any_Type;
7014 end if;
7016 -- The cursor type for an Iterable aspect is the return type of a
7017 -- non-overloaded First primitive operation. Locate association for
7018 -- First.
7020 Assoc := First (Component_Associations (Expression (Aspect)));
7021 First_Op := Any_Id;
7022 while Present (Assoc) loop
7023 if Chars (First (Choices (Assoc))) = Name_First then
7024 First_Op := Expression (Assoc);
7025 exit;
7026 end if;
7028 Next (Assoc);
7029 end loop;
7031 if First_Op = Any_Id then
7032 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
7033 return Any_Type;
7034 end if;
7036 Cursor := Any_Type;
7038 -- Locate function with desired name and profile in scope of type
7040 Func := First_Entity (Scope (Typ));
7041 while Present (Func) loop
7042 if Chars (Func) = Chars (First_Op)
7043 and then Ekind (Func) = E_Function
7044 and then Present (First_Formal (Func))
7045 and then Etype (First_Formal (Func)) = Typ
7046 and then No (Next_Formal (First_Formal (Func)))
7047 then
7048 if Cursor /= Any_Type then
7049 Error_Msg_N
7050 ("Operation First for iterable type must be unique", Aspect);
7051 return Any_Type;
7052 else
7053 Cursor := Etype (Func);
7054 end if;
7055 end if;
7057 Next_Entity (Func);
7058 end loop;
7060 -- If not found, no way to resolve remaining primitives.
7062 if Cursor = Any_Type then
7063 Error_Msg_N
7064 ("No legal primitive operation First for Iterable type", Aspect);
7065 end if;
7067 return Cursor;
7068 end Get_Cursor_Type;
7070 -------------------------------
7071 -- Get_Default_External_Name --
7072 -------------------------------
7074 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
7075 begin
7076 Get_Decoded_Name_String (Chars (E));
7078 if Opt.External_Name_Imp_Casing = Uppercase then
7079 Set_Casing (All_Upper_Case);
7080 else
7081 Set_Casing (All_Lower_Case);
7082 end if;
7084 return
7085 Make_String_Literal (Sloc (E),
7086 Strval => String_From_Name_Buffer);
7087 end Get_Default_External_Name;
7089 --------------------------
7090 -- Get_Enclosing_Object --
7091 --------------------------
7093 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
7094 begin
7095 if Is_Entity_Name (N) then
7096 return Entity (N);
7097 else
7098 case Nkind (N) is
7099 when N_Indexed_Component |
7100 N_Slice |
7101 N_Selected_Component =>
7103 -- If not generating code, a dereference may be left implicit.
7104 -- In thoses cases, return Empty.
7106 if Is_Access_Type (Etype (Prefix (N))) then
7107 return Empty;
7108 else
7109 return Get_Enclosing_Object (Prefix (N));
7110 end if;
7112 when N_Type_Conversion =>
7113 return Get_Enclosing_Object (Expression (N));
7115 when others =>
7116 return Empty;
7117 end case;
7118 end if;
7119 end Get_Enclosing_Object;
7121 ---------------------------
7122 -- Get_Enum_Lit_From_Pos --
7123 ---------------------------
7125 function Get_Enum_Lit_From_Pos
7126 (T : Entity_Id;
7127 Pos : Uint;
7128 Loc : Source_Ptr) return Node_Id
7130 Btyp : Entity_Id := Base_Type (T);
7131 Lit : Node_Id;
7133 begin
7134 -- In the case where the literal is of type Character, Wide_Character
7135 -- or Wide_Wide_Character or of a type derived from them, there needs
7136 -- to be some special handling since there is no explicit chain of
7137 -- literals to search. Instead, an N_Character_Literal node is created
7138 -- with the appropriate Char_Code and Chars fields.
7140 if Is_Standard_Character_Type (T) then
7141 Set_Character_Literal_Name (UI_To_CC (Pos));
7142 return
7143 Make_Character_Literal (Loc,
7144 Chars => Name_Find,
7145 Char_Literal_Value => Pos);
7147 -- For all other cases, we have a complete table of literals, and
7148 -- we simply iterate through the chain of literal until the one
7149 -- with the desired position value is found.
7151 else
7152 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7153 Btyp := Full_View (Btyp);
7154 end if;
7156 Lit := First_Literal (Btyp);
7157 for J in 1 .. UI_To_Int (Pos) loop
7158 Next_Literal (Lit);
7159 end loop;
7161 return New_Occurrence_Of (Lit, Loc);
7162 end if;
7163 end Get_Enum_Lit_From_Pos;
7165 ------------------------
7166 -- Get_Generic_Entity --
7167 ------------------------
7169 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7170 Ent : constant Entity_Id := Entity (Name (N));
7171 begin
7172 if Present (Renamed_Object (Ent)) then
7173 return Renamed_Object (Ent);
7174 else
7175 return Ent;
7176 end if;
7177 end Get_Generic_Entity;
7179 -------------------------------------
7180 -- Get_Incomplete_View_Of_Ancestor --
7181 -------------------------------------
7183 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7184 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7185 Par_Scope : Entity_Id;
7186 Par_Type : Entity_Id;
7188 begin
7189 -- The incomplete view of an ancestor is only relevant for private
7190 -- derived types in child units.
7192 if not Is_Derived_Type (E)
7193 or else not Is_Child_Unit (Cur_Unit)
7194 then
7195 return Empty;
7197 else
7198 Par_Scope := Scope (Cur_Unit);
7199 if No (Par_Scope) then
7200 return Empty;
7201 end if;
7203 Par_Type := Etype (Base_Type (E));
7205 -- Traverse list of ancestor types until we find one declared in
7206 -- a parent or grandparent unit (two levels seem sufficient).
7208 while Present (Par_Type) loop
7209 if Scope (Par_Type) = Par_Scope
7210 or else Scope (Par_Type) = Scope (Par_Scope)
7211 then
7212 return Par_Type;
7214 elsif not Is_Derived_Type (Par_Type) then
7215 return Empty;
7217 else
7218 Par_Type := Etype (Base_Type (Par_Type));
7219 end if;
7220 end loop;
7222 -- If none found, there is no relevant ancestor type.
7224 return Empty;
7225 end if;
7226 end Get_Incomplete_View_Of_Ancestor;
7228 ----------------------
7229 -- Get_Index_Bounds --
7230 ----------------------
7232 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
7233 Kind : constant Node_Kind := Nkind (N);
7234 R : Node_Id;
7236 begin
7237 if Kind = N_Range then
7238 L := Low_Bound (N);
7239 H := High_Bound (N);
7241 elsif Kind = N_Subtype_Indication then
7242 R := Range_Expression (Constraint (N));
7244 if R = Error then
7245 L := Error;
7246 H := Error;
7247 return;
7249 else
7250 L := Low_Bound (Range_Expression (Constraint (N)));
7251 H := High_Bound (Range_Expression (Constraint (N)));
7252 end if;
7254 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
7255 if Error_Posted (Scalar_Range (Entity (N))) then
7256 L := Error;
7257 H := Error;
7259 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
7260 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
7262 else
7263 L := Low_Bound (Scalar_Range (Entity (N)));
7264 H := High_Bound (Scalar_Range (Entity (N)));
7265 end if;
7267 else
7268 -- N is an expression, indicating a range with one value
7270 L := N;
7271 H := N;
7272 end if;
7273 end Get_Index_Bounds;
7275 ---------------------------------
7276 -- Get_Iterable_Type_Primitive --
7277 ---------------------------------
7279 function Get_Iterable_Type_Primitive
7280 (Typ : Entity_Id;
7281 Nam : Name_Id) return Entity_Id
7283 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
7284 Assoc : Node_Id;
7286 begin
7287 if No (Funcs) then
7288 return Empty;
7290 else
7291 Assoc := First (Component_Associations (Funcs));
7292 while Present (Assoc) loop
7293 if Chars (First (Choices (Assoc))) = Nam then
7294 return Entity (Expression (Assoc));
7295 end if;
7297 Assoc := Next (Assoc);
7298 end loop;
7300 return Empty;
7301 end if;
7302 end Get_Iterable_Type_Primitive;
7304 ----------------------------------
7305 -- Get_Library_Unit_Name_string --
7306 ----------------------------------
7308 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
7309 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
7311 begin
7312 Get_Unit_Name_String (Unit_Name_Id);
7314 -- Remove seven last character (" (spec)" or " (body)")
7316 Name_Len := Name_Len - 7;
7317 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
7318 end Get_Library_Unit_Name_String;
7320 ------------------------
7321 -- Get_Name_Entity_Id --
7322 ------------------------
7324 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
7325 begin
7326 return Entity_Id (Get_Name_Table_Int (Id));
7327 end Get_Name_Entity_Id;
7329 ------------------------------
7330 -- Get_Name_From_CTC_Pragma --
7331 ------------------------------
7333 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
7334 Arg : constant Node_Id :=
7335 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
7336 begin
7337 return Strval (Expr_Value_S (Arg));
7338 end Get_Name_From_CTC_Pragma;
7340 -----------------------
7341 -- Get_Parent_Entity --
7342 -----------------------
7344 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
7345 begin
7346 if Nkind (Unit) = N_Package_Body
7347 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
7348 then
7349 return Defining_Entity
7350 (Specification (Instance_Spec (Original_Node (Unit))));
7351 elsif Nkind (Unit) = N_Package_Instantiation then
7352 return Defining_Entity (Specification (Instance_Spec (Unit)));
7353 else
7354 return Defining_Entity (Unit);
7355 end if;
7356 end Get_Parent_Entity;
7357 -------------------
7358 -- Get_Pragma_Id --
7359 -------------------
7361 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
7362 begin
7363 return Get_Pragma_Id (Pragma_Name (N));
7364 end Get_Pragma_Id;
7366 -----------------------
7367 -- Get_Reason_String --
7368 -----------------------
7370 procedure Get_Reason_String (N : Node_Id) is
7371 begin
7372 if Nkind (N) = N_String_Literal then
7373 Store_String_Chars (Strval (N));
7375 elsif Nkind (N) = N_Op_Concat then
7376 Get_Reason_String (Left_Opnd (N));
7377 Get_Reason_String (Right_Opnd (N));
7379 -- If not of required form, error
7381 else
7382 Error_Msg_N
7383 ("Reason for pragma Warnings has wrong form", N);
7384 Error_Msg_N
7385 ("\must be string literal or concatenation of string literals", N);
7386 return;
7387 end if;
7388 end Get_Reason_String;
7390 ---------------------------
7391 -- Get_Referenced_Object --
7392 ---------------------------
7394 function Get_Referenced_Object (N : Node_Id) return Node_Id is
7395 R : Node_Id;
7397 begin
7398 R := N;
7399 while Is_Entity_Name (R)
7400 and then Present (Renamed_Object (Entity (R)))
7401 loop
7402 R := Renamed_Object (Entity (R));
7403 end loop;
7405 return R;
7406 end Get_Referenced_Object;
7408 ------------------------
7409 -- Get_Renamed_Entity --
7410 ------------------------
7412 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
7413 R : Entity_Id;
7415 begin
7416 R := E;
7417 while Present (Renamed_Entity (R)) loop
7418 R := Renamed_Entity (R);
7419 end loop;
7421 return R;
7422 end Get_Renamed_Entity;
7424 -------------------------
7425 -- Get_Subprogram_Body --
7426 -------------------------
7428 function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
7429 Decl : Node_Id;
7431 begin
7432 Decl := Unit_Declaration_Node (E);
7434 if Nkind (Decl) = N_Subprogram_Body then
7435 return Decl;
7437 -- The below comment is bad, because it is possible for
7438 -- Nkind (Decl) to be an N_Subprogram_Body_Stub ???
7440 else -- Nkind (Decl) = N_Subprogram_Declaration
7442 if Present (Corresponding_Body (Decl)) then
7443 return Unit_Declaration_Node (Corresponding_Body (Decl));
7445 -- Imported subprogram case
7447 else
7448 return Empty;
7449 end if;
7450 end if;
7451 end Get_Subprogram_Body;
7453 ---------------------------
7454 -- Get_Subprogram_Entity --
7455 ---------------------------
7457 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
7458 Subp : Node_Id;
7459 Subp_Id : Entity_Id;
7461 begin
7462 if Nkind (Nod) = N_Accept_Statement then
7463 Subp := Entry_Direct_Name (Nod);
7465 elsif Nkind (Nod) = N_Slice then
7466 Subp := Prefix (Nod);
7468 else
7469 Subp := Name (Nod);
7470 end if;
7472 -- Strip the subprogram call
7474 loop
7475 if Nkind_In (Subp, N_Explicit_Dereference,
7476 N_Indexed_Component,
7477 N_Selected_Component)
7478 then
7479 Subp := Prefix (Subp);
7481 elsif Nkind_In (Subp, N_Type_Conversion,
7482 N_Unchecked_Type_Conversion)
7483 then
7484 Subp := Expression (Subp);
7486 else
7487 exit;
7488 end if;
7489 end loop;
7491 -- Extract the entity of the subprogram call
7493 if Is_Entity_Name (Subp) then
7494 Subp_Id := Entity (Subp);
7496 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
7497 Subp_Id := Directly_Designated_Type (Subp_Id);
7498 end if;
7500 if Is_Subprogram (Subp_Id) then
7501 return Subp_Id;
7502 else
7503 return Empty;
7504 end if;
7506 -- The search did not find a construct that denotes a subprogram
7508 else
7509 return Empty;
7510 end if;
7511 end Get_Subprogram_Entity;
7513 -----------------------------
7514 -- Get_Task_Body_Procedure --
7515 -----------------------------
7517 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
7518 begin
7519 -- Note: A task type may be the completion of a private type with
7520 -- discriminants. When performing elaboration checks on a task
7521 -- declaration, the current view of the type may be the private one,
7522 -- and the procedure that holds the body of the task is held in its
7523 -- underlying type.
7525 -- This is an odd function, why not have Task_Body_Procedure do
7526 -- the following digging???
7528 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
7529 end Get_Task_Body_Procedure;
7531 -----------------------
7532 -- Has_Access_Values --
7533 -----------------------
7535 function Has_Access_Values (T : Entity_Id) return Boolean is
7536 Typ : constant Entity_Id := Underlying_Type (T);
7538 begin
7539 -- Case of a private type which is not completed yet. This can only
7540 -- happen in the case of a generic format type appearing directly, or
7541 -- as a component of the type to which this function is being applied
7542 -- at the top level. Return False in this case, since we certainly do
7543 -- not know that the type contains access types.
7545 if No (Typ) then
7546 return False;
7548 elsif Is_Access_Type (Typ) then
7549 return True;
7551 elsif Is_Array_Type (Typ) then
7552 return Has_Access_Values (Component_Type (Typ));
7554 elsif Is_Record_Type (Typ) then
7555 declare
7556 Comp : Entity_Id;
7558 begin
7559 -- Loop to Check components
7561 Comp := First_Component_Or_Discriminant (Typ);
7562 while Present (Comp) loop
7564 -- Check for access component, tag field does not count, even
7565 -- though it is implemented internally using an access type.
7567 if Has_Access_Values (Etype (Comp))
7568 and then Chars (Comp) /= Name_uTag
7569 then
7570 return True;
7571 end if;
7573 Next_Component_Or_Discriminant (Comp);
7574 end loop;
7575 end;
7577 return False;
7579 else
7580 return False;
7581 end if;
7582 end Has_Access_Values;
7584 ------------------------------
7585 -- Has_Compatible_Alignment --
7586 ------------------------------
7588 function Has_Compatible_Alignment
7589 (Obj : Entity_Id;
7590 Expr : Node_Id) return Alignment_Result
7592 function Has_Compatible_Alignment_Internal
7593 (Obj : Entity_Id;
7594 Expr : Node_Id;
7595 Default : Alignment_Result) return Alignment_Result;
7596 -- This is the internal recursive function that actually does the work.
7597 -- There is one additional parameter, which says what the result should
7598 -- be if no alignment information is found, and there is no definite
7599 -- indication of compatible alignments. At the outer level, this is set
7600 -- to Unknown, but for internal recursive calls in the case where types
7601 -- are known to be correct, it is set to Known_Compatible.
7603 ---------------------------------------
7604 -- Has_Compatible_Alignment_Internal --
7605 ---------------------------------------
7607 function Has_Compatible_Alignment_Internal
7608 (Obj : Entity_Id;
7609 Expr : Node_Id;
7610 Default : Alignment_Result) return Alignment_Result
7612 Result : Alignment_Result := Known_Compatible;
7613 -- Holds the current status of the result. Note that once a value of
7614 -- Known_Incompatible is set, it is sticky and does not get changed
7615 -- to Unknown (the value in Result only gets worse as we go along,
7616 -- never better).
7618 Offs : Uint := No_Uint;
7619 -- Set to a factor of the offset from the base object when Expr is a
7620 -- selected or indexed component, based on Component_Bit_Offset and
7621 -- Component_Size respectively. A negative value is used to represent
7622 -- a value which is not known at compile time.
7624 procedure Check_Prefix;
7625 -- Checks the prefix recursively in the case where the expression
7626 -- is an indexed or selected component.
7628 procedure Set_Result (R : Alignment_Result);
7629 -- If R represents a worse outcome (unknown instead of known
7630 -- compatible, or known incompatible), then set Result to R.
7632 ------------------
7633 -- Check_Prefix --
7634 ------------------
7636 procedure Check_Prefix is
7637 begin
7638 -- The subtlety here is that in doing a recursive call to check
7639 -- the prefix, we have to decide what to do in the case where we
7640 -- don't find any specific indication of an alignment problem.
7642 -- At the outer level, we normally set Unknown as the result in
7643 -- this case, since we can only set Known_Compatible if we really
7644 -- know that the alignment value is OK, but for the recursive
7645 -- call, in the case where the types match, and we have not
7646 -- specified a peculiar alignment for the object, we are only
7647 -- concerned about suspicious rep clauses, the default case does
7648 -- not affect us, since the compiler will, in the absence of such
7649 -- rep clauses, ensure that the alignment is correct.
7651 if Default = Known_Compatible
7652 or else
7653 (Etype (Obj) = Etype (Expr)
7654 and then (Unknown_Alignment (Obj)
7655 or else
7656 Alignment (Obj) = Alignment (Etype (Obj))))
7657 then
7658 Set_Result
7659 (Has_Compatible_Alignment_Internal
7660 (Obj, Prefix (Expr), Known_Compatible));
7662 -- In all other cases, we need a full check on the prefix
7664 else
7665 Set_Result
7666 (Has_Compatible_Alignment_Internal
7667 (Obj, Prefix (Expr), Unknown));
7668 end if;
7669 end Check_Prefix;
7671 ----------------
7672 -- Set_Result --
7673 ----------------
7675 procedure Set_Result (R : Alignment_Result) is
7676 begin
7677 if R > Result then
7678 Result := R;
7679 end if;
7680 end Set_Result;
7682 -- Start of processing for Has_Compatible_Alignment_Internal
7684 begin
7685 -- If Expr is a selected component, we must make sure there is no
7686 -- potentially troublesome component clause, and that the record is
7687 -- not packed.
7689 if Nkind (Expr) = N_Selected_Component then
7691 -- Packed record always generate unknown alignment
7693 if Is_Packed (Etype (Prefix (Expr))) then
7694 Set_Result (Unknown);
7695 end if;
7697 -- Check prefix and component offset
7699 Check_Prefix;
7700 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
7702 -- If Expr is an indexed component, we must make sure there is no
7703 -- potentially troublesome Component_Size clause and that the array
7704 -- is not bit-packed.
7706 elsif Nkind (Expr) = N_Indexed_Component then
7707 declare
7708 Typ : constant Entity_Id := Etype (Prefix (Expr));
7709 Ind : constant Node_Id := First_Index (Typ);
7711 begin
7712 -- Bit packed array always generates unknown alignment
7714 if Is_Bit_Packed_Array (Typ) then
7715 Set_Result (Unknown);
7716 end if;
7718 -- Check prefix and component offset
7720 Check_Prefix;
7721 Offs := Component_Size (Typ);
7723 -- Small optimization: compute the full offset when possible
7725 if Offs /= No_Uint
7726 and then Offs > Uint_0
7727 and then Present (Ind)
7728 and then Nkind (Ind) = N_Range
7729 and then Compile_Time_Known_Value (Low_Bound (Ind))
7730 and then Compile_Time_Known_Value (First (Expressions (Expr)))
7731 then
7732 Offs := Offs * (Expr_Value (First (Expressions (Expr)))
7733 - Expr_Value (Low_Bound ((Ind))));
7734 end if;
7735 end;
7736 end if;
7738 -- If we have a null offset, the result is entirely determined by
7739 -- the base object and has already been computed recursively.
7741 if Offs = Uint_0 then
7742 null;
7744 -- Case where we know the alignment of the object
7746 elsif Known_Alignment (Obj) then
7747 declare
7748 ObjA : constant Uint := Alignment (Obj);
7749 ExpA : Uint := No_Uint;
7750 SizA : Uint := No_Uint;
7752 begin
7753 -- If alignment of Obj is 1, then we are always OK
7755 if ObjA = 1 then
7756 Set_Result (Known_Compatible);
7758 -- Alignment of Obj is greater than 1, so we need to check
7760 else
7761 -- If we have an offset, see if it is compatible
7763 if Offs /= No_Uint and Offs > Uint_0 then
7764 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
7765 Set_Result (Known_Incompatible);
7766 end if;
7768 -- See if Expr is an object with known alignment
7770 elsif Is_Entity_Name (Expr)
7771 and then Known_Alignment (Entity (Expr))
7772 then
7773 ExpA := Alignment (Entity (Expr));
7775 -- Otherwise, we can use the alignment of the type of
7776 -- Expr given that we already checked for
7777 -- discombobulating rep clauses for the cases of indexed
7778 -- and selected components above.
7780 elsif Known_Alignment (Etype (Expr)) then
7781 ExpA := Alignment (Etype (Expr));
7783 -- Otherwise the alignment is unknown
7785 else
7786 Set_Result (Default);
7787 end if;
7789 -- If we got an alignment, see if it is acceptable
7791 if ExpA /= No_Uint and then ExpA < ObjA then
7792 Set_Result (Known_Incompatible);
7793 end if;
7795 -- If Expr is not a piece of a larger object, see if size
7796 -- is given. If so, check that it is not too small for the
7797 -- required alignment.
7799 if Offs /= No_Uint then
7800 null;
7802 -- See if Expr is an object with known size
7804 elsif Is_Entity_Name (Expr)
7805 and then Known_Static_Esize (Entity (Expr))
7806 then
7807 SizA := Esize (Entity (Expr));
7809 -- Otherwise, we check the object size of the Expr type
7811 elsif Known_Static_Esize (Etype (Expr)) then
7812 SizA := Esize (Etype (Expr));
7813 end if;
7815 -- If we got a size, see if it is a multiple of the Obj
7816 -- alignment, if not, then the alignment cannot be
7817 -- acceptable, since the size is always a multiple of the
7818 -- alignment.
7820 if SizA /= No_Uint then
7821 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
7822 Set_Result (Known_Incompatible);
7823 end if;
7824 end if;
7825 end if;
7826 end;
7828 -- If we do not know required alignment, any non-zero offset is a
7829 -- potential problem (but certainly may be OK, so result is unknown).
7831 elsif Offs /= No_Uint then
7832 Set_Result (Unknown);
7834 -- If we can't find the result by direct comparison of alignment
7835 -- values, then there is still one case that we can determine known
7836 -- result, and that is when we can determine that the types are the
7837 -- same, and no alignments are specified. Then we known that the
7838 -- alignments are compatible, even if we don't know the alignment
7839 -- value in the front end.
7841 elsif Etype (Obj) = Etype (Expr) then
7843 -- Types are the same, but we have to check for possible size
7844 -- and alignments on the Expr object that may make the alignment
7845 -- different, even though the types are the same.
7847 if Is_Entity_Name (Expr) then
7849 -- First check alignment of the Expr object. Any alignment less
7850 -- than Maximum_Alignment is worrisome since this is the case
7851 -- where we do not know the alignment of Obj.
7853 if Known_Alignment (Entity (Expr))
7854 and then UI_To_Int (Alignment (Entity (Expr))) <
7855 Ttypes.Maximum_Alignment
7856 then
7857 Set_Result (Unknown);
7859 -- Now check size of Expr object. Any size that is not an
7860 -- even multiple of Maximum_Alignment is also worrisome
7861 -- since it may cause the alignment of the object to be less
7862 -- than the alignment of the type.
7864 elsif Known_Static_Esize (Entity (Expr))
7865 and then
7866 (UI_To_Int (Esize (Entity (Expr))) mod
7867 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
7868 /= 0
7869 then
7870 Set_Result (Unknown);
7872 -- Otherwise same type is decisive
7874 else
7875 Set_Result (Known_Compatible);
7876 end if;
7877 end if;
7879 -- Another case to deal with is when there is an explicit size or
7880 -- alignment clause when the types are not the same. If so, then the
7881 -- result is Unknown. We don't need to do this test if the Default is
7882 -- Unknown, since that result will be set in any case.
7884 elsif Default /= Unknown
7885 and then (Has_Size_Clause (Etype (Expr))
7886 or else
7887 Has_Alignment_Clause (Etype (Expr)))
7888 then
7889 Set_Result (Unknown);
7891 -- If no indication found, set default
7893 else
7894 Set_Result (Default);
7895 end if;
7897 -- Return worst result found
7899 return Result;
7900 end Has_Compatible_Alignment_Internal;
7902 -- Start of processing for Has_Compatible_Alignment
7904 begin
7905 -- If Obj has no specified alignment, then set alignment from the type
7906 -- alignment. Perhaps we should always do this, but for sure we should
7907 -- do it when there is an address clause since we can do more if the
7908 -- alignment is known.
7910 if Unknown_Alignment (Obj) then
7911 Set_Alignment (Obj, Alignment (Etype (Obj)));
7912 end if;
7914 -- Now do the internal call that does all the work
7916 return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
7917 end Has_Compatible_Alignment;
7919 ----------------------
7920 -- Has_Declarations --
7921 ----------------------
7923 function Has_Declarations (N : Node_Id) return Boolean is
7924 begin
7925 return Nkind_In (Nkind (N), N_Accept_Statement,
7926 N_Block_Statement,
7927 N_Compilation_Unit_Aux,
7928 N_Entry_Body,
7929 N_Package_Body,
7930 N_Protected_Body,
7931 N_Subprogram_Body,
7932 N_Task_Body,
7933 N_Package_Specification);
7934 end Has_Declarations;
7936 ---------------------------------
7937 -- Has_Defaulted_Discriminants --
7938 ---------------------------------
7940 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
7941 begin
7942 return Has_Discriminants (Typ)
7943 and then Present (First_Discriminant (Typ))
7944 and then Present (Discriminant_Default_Value
7945 (First_Discriminant (Typ)));
7946 end Has_Defaulted_Discriminants;
7948 -------------------
7949 -- Has_Denormals --
7950 -------------------
7952 function Has_Denormals (E : Entity_Id) return Boolean is
7953 begin
7954 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
7955 end Has_Denormals;
7957 -------------------------------------------
7958 -- Has_Discriminant_Dependent_Constraint --
7959 -------------------------------------------
7961 function Has_Discriminant_Dependent_Constraint
7962 (Comp : Entity_Id) return Boolean
7964 Comp_Decl : constant Node_Id := Parent (Comp);
7965 Subt_Indic : Node_Id;
7966 Constr : Node_Id;
7967 Assn : Node_Id;
7969 begin
7970 -- Discriminants can't depend on discriminants
7972 if Ekind (Comp) = E_Discriminant then
7973 return False;
7975 else
7976 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
7978 if Nkind (Subt_Indic) = N_Subtype_Indication then
7979 Constr := Constraint (Subt_Indic);
7981 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
7982 Assn := First (Constraints (Constr));
7983 while Present (Assn) loop
7984 case Nkind (Assn) is
7985 when N_Subtype_Indication |
7986 N_Range |
7987 N_Identifier
7989 if Depends_On_Discriminant (Assn) then
7990 return True;
7991 end if;
7993 when N_Discriminant_Association =>
7994 if Depends_On_Discriminant (Expression (Assn)) then
7995 return True;
7996 end if;
7998 when others =>
7999 null;
8000 end case;
8002 Next (Assn);
8003 end loop;
8004 end if;
8005 end if;
8006 end if;
8008 return False;
8009 end Has_Discriminant_Dependent_Constraint;
8011 --------------------------
8012 -- Has_Enabled_Property --
8013 --------------------------
8015 function Has_Enabled_Property
8016 (Item_Id : Entity_Id;
8017 Property : Name_Id) return Boolean
8019 function State_Has_Enabled_Property return Boolean;
8020 -- Determine whether a state denoted by Item_Id has the property enabled
8022 function Variable_Has_Enabled_Property return Boolean;
8023 -- Determine whether a variable denoted by Item_Id has the property
8024 -- enabled.
8026 --------------------------------
8027 -- State_Has_Enabled_Property --
8028 --------------------------------
8030 function State_Has_Enabled_Property return Boolean is
8031 Decl : constant Node_Id := Parent (Item_Id);
8032 Opt : Node_Id;
8033 Opt_Nam : Node_Id;
8034 Prop : Node_Id;
8035 Prop_Nam : Node_Id;
8036 Props : Node_Id;
8038 begin
8039 -- The declaration of an external abstract state appears as an
8040 -- extension aggregate. If this is not the case, properties can never
8041 -- be set.
8043 if Nkind (Decl) /= N_Extension_Aggregate then
8044 return False;
8045 end if;
8047 -- When External appears as a simple option, it automatically enables
8048 -- all properties.
8050 Opt := First (Expressions (Decl));
8051 while Present (Opt) loop
8052 if Nkind (Opt) = N_Identifier
8053 and then Chars (Opt) = Name_External
8054 then
8055 return True;
8056 end if;
8058 Next (Opt);
8059 end loop;
8061 -- When External specifies particular properties, inspect those and
8062 -- find the desired one (if any).
8064 Opt := First (Component_Associations (Decl));
8065 while Present (Opt) loop
8066 Opt_Nam := First (Choices (Opt));
8068 if Nkind (Opt_Nam) = N_Identifier
8069 and then Chars (Opt_Nam) = Name_External
8070 then
8071 Props := Expression (Opt);
8073 -- Multiple properties appear as an aggregate
8075 if Nkind (Props) = N_Aggregate then
8077 -- Simple property form
8079 Prop := First (Expressions (Props));
8080 while Present (Prop) loop
8081 if Chars (Prop) = Property then
8082 return True;
8083 end if;
8085 Next (Prop);
8086 end loop;
8088 -- Property with expression form
8090 Prop := First (Component_Associations (Props));
8091 while Present (Prop) loop
8092 Prop_Nam := First (Choices (Prop));
8094 -- The property can be represented in two ways:
8095 -- others => <value>
8096 -- <property> => <value>
8098 if Nkind (Prop_Nam) = N_Others_Choice
8099 or else (Nkind (Prop_Nam) = N_Identifier
8100 and then Chars (Prop_Nam) = Property)
8101 then
8102 return Is_True (Expr_Value (Expression (Prop)));
8103 end if;
8105 Next (Prop);
8106 end loop;
8108 -- Single property
8110 else
8111 return Chars (Props) = Property;
8112 end if;
8113 end if;
8115 Next (Opt);
8116 end loop;
8118 return False;
8119 end State_Has_Enabled_Property;
8121 -----------------------------------
8122 -- Variable_Has_Enabled_Property --
8123 -----------------------------------
8125 function Variable_Has_Enabled_Property return Boolean is
8126 function Is_Enabled (Prag : Node_Id) return Boolean;
8127 -- Determine whether property pragma Prag (if present) denotes an
8128 -- enabled property.
8130 ----------------
8131 -- Is_Enabled --
8132 ----------------
8134 function Is_Enabled (Prag : Node_Id) return Boolean is
8135 Arg2 : Node_Id;
8137 begin
8138 if Present (Prag) then
8139 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
8141 -- The pragma has an optional Boolean expression, the related
8142 -- property is enabled only when the expression evaluates to
8143 -- True.
8145 if Present (Arg2) then
8146 return Is_True (Expr_Value (Get_Pragma_Arg (Arg2)));
8148 -- Otherwise the lack of expression enables the property by
8149 -- default.
8151 else
8152 return True;
8153 end if;
8155 -- The property was never set in the first place
8157 else
8158 return False;
8159 end if;
8160 end Is_Enabled;
8162 -- Local variables
8164 AR : constant Node_Id :=
8165 Get_Pragma (Item_Id, Pragma_Async_Readers);
8166 AW : constant Node_Id :=
8167 Get_Pragma (Item_Id, Pragma_Async_Writers);
8168 ER : constant Node_Id :=
8169 Get_Pragma (Item_Id, Pragma_Effective_Reads);
8170 EW : constant Node_Id :=
8171 Get_Pragma (Item_Id, Pragma_Effective_Writes);
8173 -- Start of processing for Variable_Has_Enabled_Property
8175 begin
8176 -- A non-effectively volatile object can never possess external
8177 -- properties.
8179 if not Is_Effectively_Volatile (Item_Id) then
8180 return False;
8182 -- External properties related to variables come in two flavors -
8183 -- explicit and implicit. The explicit case is characterized by the
8184 -- presence of a property pragma with an optional Boolean flag. The
8185 -- property is enabled when the flag evaluates to True or the flag is
8186 -- missing altogether.
8188 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
8189 return True;
8191 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
8192 return True;
8194 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
8195 return True;
8197 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
8198 return True;
8200 -- The implicit case lacks all property pragmas
8202 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
8203 return True;
8205 else
8206 return False;
8207 end if;
8208 end Variable_Has_Enabled_Property;
8210 -- Start of processing for Has_Enabled_Property
8212 begin
8213 -- Abstract states and variables have a flexible scheme of specifying
8214 -- external properties.
8216 if Ekind (Item_Id) = E_Abstract_State then
8217 return State_Has_Enabled_Property;
8219 elsif Ekind (Item_Id) = E_Variable then
8220 return Variable_Has_Enabled_Property;
8222 -- Otherwise a property is enabled when the related item is effectively
8223 -- volatile.
8225 else
8226 return Is_Effectively_Volatile (Item_Id);
8227 end if;
8228 end Has_Enabled_Property;
8230 --------------------
8231 -- Has_Infinities --
8232 --------------------
8234 function Has_Infinities (E : Entity_Id) return Boolean is
8235 begin
8236 return
8237 Is_Floating_Point_Type (E)
8238 and then Nkind (Scalar_Range (E)) = N_Range
8239 and then Includes_Infinities (Scalar_Range (E));
8240 end Has_Infinities;
8242 --------------------
8243 -- Has_Interfaces --
8244 --------------------
8246 function Has_Interfaces
8247 (T : Entity_Id;
8248 Use_Full_View : Boolean := True) return Boolean
8250 Typ : Entity_Id := Base_Type (T);
8252 begin
8253 -- Handle concurrent types
8255 if Is_Concurrent_Type (Typ) then
8256 Typ := Corresponding_Record_Type (Typ);
8257 end if;
8259 if not Present (Typ)
8260 or else not Is_Record_Type (Typ)
8261 or else not Is_Tagged_Type (Typ)
8262 then
8263 return False;
8264 end if;
8266 -- Handle private types
8268 if Use_Full_View and then Present (Full_View (Typ)) then
8269 Typ := Full_View (Typ);
8270 end if;
8272 -- Handle concurrent record types
8274 if Is_Concurrent_Record_Type (Typ)
8275 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
8276 then
8277 return True;
8278 end if;
8280 loop
8281 if Is_Interface (Typ)
8282 or else
8283 (Is_Record_Type (Typ)
8284 and then Present (Interfaces (Typ))
8285 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
8286 then
8287 return True;
8288 end if;
8290 exit when Etype (Typ) = Typ
8292 -- Handle private types
8294 or else (Present (Full_View (Etype (Typ)))
8295 and then Full_View (Etype (Typ)) = Typ)
8297 -- Protect frontend against wrong sources with cyclic derivations
8299 or else Etype (Typ) = T;
8301 -- Climb to the ancestor type handling private types
8303 if Present (Full_View (Etype (Typ))) then
8304 Typ := Full_View (Etype (Typ));
8305 else
8306 Typ := Etype (Typ);
8307 end if;
8308 end loop;
8310 return False;
8311 end Has_Interfaces;
8313 ---------------------------------
8314 -- Has_No_Obvious_Side_Effects --
8315 ---------------------------------
8317 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
8318 begin
8319 -- For now, just handle literals, constants, and non-volatile
8320 -- variables and expressions combining these with operators or
8321 -- short circuit forms.
8323 if Nkind (N) in N_Numeric_Or_String_Literal then
8324 return True;
8326 elsif Nkind (N) = N_Character_Literal then
8327 return True;
8329 elsif Nkind (N) in N_Unary_Op then
8330 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
8332 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
8333 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
8334 and then
8335 Has_No_Obvious_Side_Effects (Right_Opnd (N));
8337 elsif Nkind (N) = N_Expression_With_Actions
8338 and then Is_Empty_List (Actions (N))
8339 then
8340 return Has_No_Obvious_Side_Effects (Expression (N));
8342 elsif Nkind (N) in N_Has_Entity then
8343 return Present (Entity (N))
8344 and then Ekind_In (Entity (N), E_Variable,
8345 E_Constant,
8346 E_Enumeration_Literal,
8347 E_In_Parameter,
8348 E_Out_Parameter,
8349 E_In_Out_Parameter)
8350 and then not Is_Volatile (Entity (N));
8352 else
8353 return False;
8354 end if;
8355 end Has_No_Obvious_Side_Effects;
8357 ------------------------
8358 -- Has_Null_Exclusion --
8359 ------------------------
8361 function Has_Null_Exclusion (N : Node_Id) return Boolean is
8362 begin
8363 case Nkind (N) is
8364 when N_Access_Definition |
8365 N_Access_Function_Definition |
8366 N_Access_Procedure_Definition |
8367 N_Access_To_Object_Definition |
8368 N_Allocator |
8369 N_Derived_Type_Definition |
8370 N_Function_Specification |
8371 N_Subtype_Declaration =>
8372 return Null_Exclusion_Present (N);
8374 when N_Component_Definition |
8375 N_Formal_Object_Declaration |
8376 N_Object_Renaming_Declaration =>
8377 if Present (Subtype_Mark (N)) then
8378 return Null_Exclusion_Present (N);
8379 else pragma Assert (Present (Access_Definition (N)));
8380 return Null_Exclusion_Present (Access_Definition (N));
8381 end if;
8383 when N_Discriminant_Specification =>
8384 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
8385 return Null_Exclusion_Present (Discriminant_Type (N));
8386 else
8387 return Null_Exclusion_Present (N);
8388 end if;
8390 when N_Object_Declaration =>
8391 if Nkind (Object_Definition (N)) = N_Access_Definition then
8392 return Null_Exclusion_Present (Object_Definition (N));
8393 else
8394 return Null_Exclusion_Present (N);
8395 end if;
8397 when N_Parameter_Specification =>
8398 if Nkind (Parameter_Type (N)) = N_Access_Definition then
8399 return Null_Exclusion_Present (Parameter_Type (N));
8400 else
8401 return Null_Exclusion_Present (N);
8402 end if;
8404 when others =>
8405 return False;
8407 end case;
8408 end Has_Null_Exclusion;
8410 ------------------------
8411 -- Has_Null_Extension --
8412 ------------------------
8414 function Has_Null_Extension (T : Entity_Id) return Boolean is
8415 B : constant Entity_Id := Base_Type (T);
8416 Comps : Node_Id;
8417 Ext : Node_Id;
8419 begin
8420 if Nkind (Parent (B)) = N_Full_Type_Declaration
8421 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
8422 then
8423 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
8425 if Present (Ext) then
8426 if Null_Present (Ext) then
8427 return True;
8428 else
8429 Comps := Component_List (Ext);
8431 -- The null component list is rewritten during analysis to
8432 -- include the parent component. Any other component indicates
8433 -- that the extension was not originally null.
8435 return Null_Present (Comps)
8436 or else No (Next (First (Component_Items (Comps))));
8437 end if;
8438 else
8439 return False;
8440 end if;
8442 else
8443 return False;
8444 end if;
8445 end Has_Null_Extension;
8447 -------------------------------
8448 -- Has_Overriding_Initialize --
8449 -------------------------------
8451 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
8452 BT : constant Entity_Id := Base_Type (T);
8453 P : Elmt_Id;
8455 begin
8456 if Is_Controlled (BT) then
8457 if Is_RTU (Scope (BT), Ada_Finalization) then
8458 return False;
8460 elsif Present (Primitive_Operations (BT)) then
8461 P := First_Elmt (Primitive_Operations (BT));
8462 while Present (P) loop
8463 declare
8464 Init : constant Entity_Id := Node (P);
8465 Formal : constant Entity_Id := First_Formal (Init);
8466 begin
8467 if Ekind (Init) = E_Procedure
8468 and then Chars (Init) = Name_Initialize
8469 and then Comes_From_Source (Init)
8470 and then Present (Formal)
8471 and then Etype (Formal) = BT
8472 and then No (Next_Formal (Formal))
8473 and then (Ada_Version < Ada_2012
8474 or else not Null_Present (Parent (Init)))
8475 then
8476 return True;
8477 end if;
8478 end;
8480 Next_Elmt (P);
8481 end loop;
8482 end if;
8484 -- Here if type itself does not have a non-null Initialize operation:
8485 -- check immediate ancestor.
8487 if Is_Derived_Type (BT)
8488 and then Has_Overriding_Initialize (Etype (BT))
8489 then
8490 return True;
8491 end if;
8492 end if;
8494 return False;
8495 end Has_Overriding_Initialize;
8497 --------------------------------------
8498 -- Has_Preelaborable_Initialization --
8499 --------------------------------------
8501 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
8502 Has_PE : Boolean;
8504 procedure Check_Components (E : Entity_Id);
8505 -- Check component/discriminant chain, sets Has_PE False if a component
8506 -- or discriminant does not meet the preelaborable initialization rules.
8508 ----------------------
8509 -- Check_Components --
8510 ----------------------
8512 procedure Check_Components (E : Entity_Id) is
8513 Ent : Entity_Id;
8514 Exp : Node_Id;
8516 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
8517 -- Returns True if and only if the expression denoted by N does not
8518 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
8520 ---------------------------------
8521 -- Is_Preelaborable_Expression --
8522 ---------------------------------
8524 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
8525 Exp : Node_Id;
8526 Assn : Node_Id;
8527 Choice : Node_Id;
8528 Comp_Type : Entity_Id;
8529 Is_Array_Aggr : Boolean;
8531 begin
8532 if Is_OK_Static_Expression (N) then
8533 return True;
8535 elsif Nkind (N) = N_Null then
8536 return True;
8538 -- Attributes are allowed in general, even if their prefix is a
8539 -- formal type. (It seems that certain attributes known not to be
8540 -- static might not be allowed, but there are no rules to prevent
8541 -- them.)
8543 elsif Nkind (N) = N_Attribute_Reference then
8544 return True;
8546 -- The name of a discriminant evaluated within its parent type is
8547 -- defined to be preelaborable (10.2.1(8)). Note that we test for
8548 -- names that denote discriminals as well as discriminants to
8549 -- catch references occurring within init procs.
8551 elsif Is_Entity_Name (N)
8552 and then
8553 (Ekind (Entity (N)) = E_Discriminant
8554 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
8555 and then Present (Discriminal_Link (Entity (N)))))
8556 then
8557 return True;
8559 elsif Nkind (N) = N_Qualified_Expression then
8560 return Is_Preelaborable_Expression (Expression (N));
8562 -- For aggregates we have to check that each of the associations
8563 -- is preelaborable.
8565 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8566 Is_Array_Aggr := Is_Array_Type (Etype (N));
8568 if Is_Array_Aggr then
8569 Comp_Type := Component_Type (Etype (N));
8570 end if;
8572 -- Check the ancestor part of extension aggregates, which must
8573 -- be either the name of a type that has preelaborable init or
8574 -- an expression that is preelaborable.
8576 if Nkind (N) = N_Extension_Aggregate then
8577 declare
8578 Anc_Part : constant Node_Id := Ancestor_Part (N);
8580 begin
8581 if Is_Entity_Name (Anc_Part)
8582 and then Is_Type (Entity (Anc_Part))
8583 then
8584 if not Has_Preelaborable_Initialization
8585 (Entity (Anc_Part))
8586 then
8587 return False;
8588 end if;
8590 elsif not Is_Preelaborable_Expression (Anc_Part) then
8591 return False;
8592 end if;
8593 end;
8594 end if;
8596 -- Check positional associations
8598 Exp := First (Expressions (N));
8599 while Present (Exp) loop
8600 if not Is_Preelaborable_Expression (Exp) then
8601 return False;
8602 end if;
8604 Next (Exp);
8605 end loop;
8607 -- Check named associations
8609 Assn := First (Component_Associations (N));
8610 while Present (Assn) loop
8611 Choice := First (Choices (Assn));
8612 while Present (Choice) loop
8613 if Is_Array_Aggr then
8614 if Nkind (Choice) = N_Others_Choice then
8615 null;
8617 elsif Nkind (Choice) = N_Range then
8618 if not Is_OK_Static_Range (Choice) then
8619 return False;
8620 end if;
8622 elsif not Is_OK_Static_Expression (Choice) then
8623 return False;
8624 end if;
8626 else
8627 Comp_Type := Etype (Choice);
8628 end if;
8630 Next (Choice);
8631 end loop;
8633 -- If the association has a <> at this point, then we have
8634 -- to check whether the component's type has preelaborable
8635 -- initialization. Note that this only occurs when the
8636 -- association's corresponding component does not have a
8637 -- default expression, the latter case having already been
8638 -- expanded as an expression for the association.
8640 if Box_Present (Assn) then
8641 if not Has_Preelaborable_Initialization (Comp_Type) then
8642 return False;
8643 end if;
8645 -- In the expression case we check whether the expression
8646 -- is preelaborable.
8648 elsif
8649 not Is_Preelaborable_Expression (Expression (Assn))
8650 then
8651 return False;
8652 end if;
8654 Next (Assn);
8655 end loop;
8657 -- If we get here then aggregate as a whole is preelaborable
8659 return True;
8661 -- All other cases are not preelaborable
8663 else
8664 return False;
8665 end if;
8666 end Is_Preelaborable_Expression;
8668 -- Start of processing for Check_Components
8670 begin
8671 -- Loop through entities of record or protected type
8673 Ent := E;
8674 while Present (Ent) loop
8676 -- We are interested only in components and discriminants
8678 Exp := Empty;
8680 case Ekind (Ent) is
8681 when E_Component =>
8683 -- Get default expression if any. If there is no declaration
8684 -- node, it means we have an internal entity. The parent and
8685 -- tag fields are examples of such entities. For such cases,
8686 -- we just test the type of the entity.
8688 if Present (Declaration_Node (Ent)) then
8689 Exp := Expression (Declaration_Node (Ent));
8690 end if;
8692 when E_Discriminant =>
8694 -- Note: for a renamed discriminant, the Declaration_Node
8695 -- may point to the one from the ancestor, and have a
8696 -- different expression, so use the proper attribute to
8697 -- retrieve the expression from the derived constraint.
8699 Exp := Discriminant_Default_Value (Ent);
8701 when others =>
8702 goto Check_Next_Entity;
8703 end case;
8705 -- A component has PI if it has no default expression and the
8706 -- component type has PI.
8708 if No (Exp) then
8709 if not Has_Preelaborable_Initialization (Etype (Ent)) then
8710 Has_PE := False;
8711 exit;
8712 end if;
8714 -- Require the default expression to be preelaborable
8716 elsif not Is_Preelaborable_Expression (Exp) then
8717 Has_PE := False;
8718 exit;
8719 end if;
8721 <<Check_Next_Entity>>
8722 Next_Entity (Ent);
8723 end loop;
8724 end Check_Components;
8726 -- Start of processing for Has_Preelaborable_Initialization
8728 begin
8729 -- Immediate return if already marked as known preelaborable init. This
8730 -- covers types for which this function has already been called once
8731 -- and returned True (in which case the result is cached), and also
8732 -- types to which a pragma Preelaborable_Initialization applies.
8734 if Known_To_Have_Preelab_Init (E) then
8735 return True;
8736 end if;
8738 -- If the type is a subtype representing a generic actual type, then
8739 -- test whether its base type has preelaborable initialization since
8740 -- the subtype representing the actual does not inherit this attribute
8741 -- from the actual or formal. (but maybe it should???)
8743 if Is_Generic_Actual_Type (E) then
8744 return Has_Preelaborable_Initialization (Base_Type (E));
8745 end if;
8747 -- All elementary types have preelaborable initialization
8749 if Is_Elementary_Type (E) then
8750 Has_PE := True;
8752 -- Array types have PI if the component type has PI
8754 elsif Is_Array_Type (E) then
8755 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
8757 -- A derived type has preelaborable initialization if its parent type
8758 -- has preelaborable initialization and (in the case of a derived record
8759 -- extension) if the non-inherited components all have preelaborable
8760 -- initialization. However, a user-defined controlled type with an
8761 -- overriding Initialize procedure does not have preelaborable
8762 -- initialization.
8764 elsif Is_Derived_Type (E) then
8766 -- If the derived type is a private extension then it doesn't have
8767 -- preelaborable initialization.
8769 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
8770 return False;
8771 end if;
8773 -- First check whether ancestor type has preelaborable initialization
8775 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
8777 -- If OK, check extension components (if any)
8779 if Has_PE and then Is_Record_Type (E) then
8780 Check_Components (First_Entity (E));
8781 end if;
8783 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
8784 -- with a user defined Initialize procedure does not have PI. If
8785 -- the type is untagged, the control primitives come from a component
8786 -- that has already been checked.
8788 if Has_PE
8789 and then Is_Controlled (E)
8790 and then Is_Tagged_Type (E)
8791 and then Has_Overriding_Initialize (E)
8792 then
8793 Has_PE := False;
8794 end if;
8796 -- Private types not derived from a type having preelaborable init and
8797 -- that are not marked with pragma Preelaborable_Initialization do not
8798 -- have preelaborable initialization.
8800 elsif Is_Private_Type (E) then
8801 return False;
8803 -- Record type has PI if it is non private and all components have PI
8805 elsif Is_Record_Type (E) then
8806 Has_PE := True;
8807 Check_Components (First_Entity (E));
8809 -- Protected types must not have entries, and components must meet
8810 -- same set of rules as for record components.
8812 elsif Is_Protected_Type (E) then
8813 if Has_Entries (E) then
8814 Has_PE := False;
8815 else
8816 Has_PE := True;
8817 Check_Components (First_Entity (E));
8818 Check_Components (First_Private_Entity (E));
8819 end if;
8821 -- Type System.Address always has preelaborable initialization
8823 elsif Is_RTE (E, RE_Address) then
8824 Has_PE := True;
8826 -- In all other cases, type does not have preelaborable initialization
8828 else
8829 return False;
8830 end if;
8832 -- If type has preelaborable initialization, cache result
8834 if Has_PE then
8835 Set_Known_To_Have_Preelab_Init (E);
8836 end if;
8838 return Has_PE;
8839 end Has_Preelaborable_Initialization;
8841 ---------------------------
8842 -- Has_Private_Component --
8843 ---------------------------
8845 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
8846 Btype : Entity_Id := Base_Type (Type_Id);
8847 Component : Entity_Id;
8849 begin
8850 if Error_Posted (Type_Id)
8851 or else Error_Posted (Btype)
8852 then
8853 return False;
8854 end if;
8856 if Is_Class_Wide_Type (Btype) then
8857 Btype := Root_Type (Btype);
8858 end if;
8860 if Is_Private_Type (Btype) then
8861 declare
8862 UT : constant Entity_Id := Underlying_Type (Btype);
8863 begin
8864 if No (UT) then
8865 if No (Full_View (Btype)) then
8866 return not Is_Generic_Type (Btype)
8867 and then
8868 not Is_Generic_Type (Root_Type (Btype));
8869 else
8870 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
8871 end if;
8872 else
8873 return not Is_Frozen (UT) and then Has_Private_Component (UT);
8874 end if;
8875 end;
8877 elsif Is_Array_Type (Btype) then
8878 return Has_Private_Component (Component_Type (Btype));
8880 elsif Is_Record_Type (Btype) then
8881 Component := First_Component (Btype);
8882 while Present (Component) loop
8883 if Has_Private_Component (Etype (Component)) then
8884 return True;
8885 end if;
8887 Next_Component (Component);
8888 end loop;
8890 return False;
8892 elsif Is_Protected_Type (Btype)
8893 and then Present (Corresponding_Record_Type (Btype))
8894 then
8895 return Has_Private_Component (Corresponding_Record_Type (Btype));
8897 else
8898 return False;
8899 end if;
8900 end Has_Private_Component;
8902 ----------------------
8903 -- Has_Signed_Zeros --
8904 ----------------------
8906 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
8907 begin
8908 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
8909 end Has_Signed_Zeros;
8911 ------------------------------
8912 -- Has_Significant_Contract --
8913 ------------------------------
8915 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
8916 Subp_Nam : constant Name_Id := Chars (Subp_Id);
8918 begin
8919 -- _Finalizer procedure
8921 if Subp_Nam = Name_uFinalizer then
8922 return False;
8924 -- _Postconditions procedure
8926 elsif Subp_Nam = Name_uPostconditions then
8927 return False;
8929 -- Predicate function
8931 elsif Ekind (Subp_Id) = E_Function
8932 and then Is_Predicate_Function (Subp_Id)
8933 then
8934 return False;
8936 -- TSS subprogram
8938 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
8939 return False;
8941 else
8942 return True;
8943 end if;
8944 end Has_Significant_Contract;
8946 -----------------------------
8947 -- Has_Static_Array_Bounds --
8948 -----------------------------
8950 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
8951 Ndims : constant Nat := Number_Dimensions (Typ);
8953 Index : Node_Id;
8954 Low : Node_Id;
8955 High : Node_Id;
8957 begin
8958 -- Unconstrained types do not have static bounds
8960 if not Is_Constrained (Typ) then
8961 return False;
8962 end if;
8964 -- First treat string literals specially, as the lower bound and length
8965 -- of string literals are not stored like those of arrays.
8967 -- A string literal always has static bounds
8969 if Ekind (Typ) = E_String_Literal_Subtype then
8970 return True;
8971 end if;
8973 -- Treat all dimensions in turn
8975 Index := First_Index (Typ);
8976 for Indx in 1 .. Ndims loop
8978 -- In case of an illegal index which is not a discrete type, return
8979 -- that the type is not static.
8981 if not Is_Discrete_Type (Etype (Index))
8982 or else Etype (Index) = Any_Type
8983 then
8984 return False;
8985 end if;
8987 Get_Index_Bounds (Index, Low, High);
8989 if Error_Posted (Low) or else Error_Posted (High) then
8990 return False;
8991 end if;
8993 if Is_OK_Static_Expression (Low)
8994 and then
8995 Is_OK_Static_Expression (High)
8996 then
8997 null;
8998 else
8999 return False;
9000 end if;
9002 Next (Index);
9003 end loop;
9005 -- If we fall through the loop, all indexes matched
9007 return True;
9008 end Has_Static_Array_Bounds;
9010 ----------------
9011 -- Has_Stream --
9012 ----------------
9014 function Has_Stream (T : Entity_Id) return Boolean is
9015 E : Entity_Id;
9017 begin
9018 if No (T) then
9019 return False;
9021 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
9022 return True;
9024 elsif Is_Array_Type (T) then
9025 return Has_Stream (Component_Type (T));
9027 elsif Is_Record_Type (T) then
9028 E := First_Component (T);
9029 while Present (E) loop
9030 if Has_Stream (Etype (E)) then
9031 return True;
9032 else
9033 Next_Component (E);
9034 end if;
9035 end loop;
9037 return False;
9039 elsif Is_Private_Type (T) then
9040 return Has_Stream (Underlying_Type (T));
9042 else
9043 return False;
9044 end if;
9045 end Has_Stream;
9047 ----------------
9048 -- Has_Suffix --
9049 ----------------
9051 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
9052 begin
9053 Get_Name_String (Chars (E));
9054 return Name_Buffer (Name_Len) = Suffix;
9055 end Has_Suffix;
9057 ----------------
9058 -- Add_Suffix --
9059 ----------------
9061 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
9062 begin
9063 Get_Name_String (Chars (E));
9064 Add_Char_To_Name_Buffer (Suffix);
9065 return Name_Find;
9066 end Add_Suffix;
9068 -------------------
9069 -- Remove_Suffix --
9070 -------------------
9072 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
9073 begin
9074 pragma Assert (Has_Suffix (E, Suffix));
9075 Get_Name_String (Chars (E));
9076 Name_Len := Name_Len - 1;
9077 return Name_Find;
9078 end Remove_Suffix;
9080 --------------------------
9081 -- Has_Tagged_Component --
9082 --------------------------
9084 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
9085 Comp : Entity_Id;
9087 begin
9088 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
9089 return Has_Tagged_Component (Underlying_Type (Typ));
9091 elsif Is_Array_Type (Typ) then
9092 return Has_Tagged_Component (Component_Type (Typ));
9094 elsif Is_Tagged_Type (Typ) then
9095 return True;
9097 elsif Is_Record_Type (Typ) then
9098 Comp := First_Component (Typ);
9099 while Present (Comp) loop
9100 if Has_Tagged_Component (Etype (Comp)) then
9101 return True;
9102 end if;
9104 Next_Component (Comp);
9105 end loop;
9107 return False;
9109 else
9110 return False;
9111 end if;
9112 end Has_Tagged_Component;
9114 ----------------------------
9115 -- Has_Volatile_Component --
9116 ----------------------------
9118 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
9119 Comp : Entity_Id;
9121 begin
9122 if Has_Volatile_Components (Typ) then
9123 return True;
9125 elsif Is_Array_Type (Typ) then
9126 return Is_Volatile (Component_Type (Typ));
9128 elsif Is_Record_Type (Typ) then
9129 Comp := First_Component (Typ);
9130 while Present (Comp) loop
9131 if Is_Volatile_Object (Comp) then
9132 return True;
9133 end if;
9135 Comp := Next_Component (Comp);
9136 end loop;
9137 end if;
9139 return False;
9140 end Has_Volatile_Component;
9142 -------------------------
9143 -- Implementation_Kind --
9144 -------------------------
9146 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
9147 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
9148 Arg : Node_Id;
9149 begin
9150 pragma Assert (Present (Impl_Prag));
9151 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
9152 return Chars (Get_Pragma_Arg (Arg));
9153 end Implementation_Kind;
9155 --------------------------
9156 -- Implements_Interface --
9157 --------------------------
9159 function Implements_Interface
9160 (Typ_Ent : Entity_Id;
9161 Iface_Ent : Entity_Id;
9162 Exclude_Parents : Boolean := False) return Boolean
9164 Ifaces_List : Elist_Id;
9165 Elmt : Elmt_Id;
9166 Iface : Entity_Id := Base_Type (Iface_Ent);
9167 Typ : Entity_Id := Base_Type (Typ_Ent);
9169 begin
9170 if Is_Class_Wide_Type (Typ) then
9171 Typ := Root_Type (Typ);
9172 end if;
9174 if not Has_Interfaces (Typ) then
9175 return False;
9176 end if;
9178 if Is_Class_Wide_Type (Iface) then
9179 Iface := Root_Type (Iface);
9180 end if;
9182 Collect_Interfaces (Typ, Ifaces_List);
9184 Elmt := First_Elmt (Ifaces_List);
9185 while Present (Elmt) loop
9186 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
9187 and then Exclude_Parents
9188 then
9189 null;
9191 elsif Node (Elmt) = Iface then
9192 return True;
9193 end if;
9195 Next_Elmt (Elmt);
9196 end loop;
9198 return False;
9199 end Implements_Interface;
9201 ------------------------------------
9202 -- In_Assertion_Expression_Pragma --
9203 ------------------------------------
9205 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
9206 Par : Node_Id;
9207 Prag : Node_Id := Empty;
9209 begin
9210 -- Climb the parent chain looking for an enclosing pragma
9212 Par := N;
9213 while Present (Par) loop
9214 if Nkind (Par) = N_Pragma then
9215 Prag := Par;
9216 exit;
9218 -- Precondition-like pragmas are expanded into if statements, check
9219 -- the original node instead.
9221 elsif Nkind (Original_Node (Par)) = N_Pragma then
9222 Prag := Original_Node (Par);
9223 exit;
9225 -- The expansion of attribute 'Old generates a constant to capture
9226 -- the result of the prefix. If the parent traversal reaches
9227 -- one of these constants, then the node technically came from a
9228 -- postcondition-like pragma. Note that the Ekind is not tested here
9229 -- because N may be the expression of an object declaration which is
9230 -- currently being analyzed. Such objects carry Ekind of E_Void.
9232 elsif Nkind (Par) = N_Object_Declaration
9233 and then Constant_Present (Par)
9234 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
9235 then
9236 return True;
9238 -- Prevent the search from going too far
9240 elsif Is_Body_Or_Package_Declaration (Par) then
9241 return False;
9242 end if;
9244 Par := Parent (Par);
9245 end loop;
9247 return
9248 Present (Prag)
9249 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
9250 end In_Assertion_Expression_Pragma;
9252 -----------------
9253 -- In_Instance --
9254 -----------------
9256 function In_Instance return Boolean is
9257 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9258 S : Entity_Id;
9260 begin
9261 S := Current_Scope;
9262 while Present (S) and then S /= Standard_Standard loop
9263 if Ekind_In (S, E_Function, E_Package, E_Procedure)
9264 and then Is_Generic_Instance (S)
9265 then
9266 -- A child instance is always compiled in the context of a parent
9267 -- instance. Nevertheless, the actuals are not analyzed in an
9268 -- instance context. We detect this case by examining the current
9269 -- compilation unit, which must be a child instance, and checking
9270 -- that it is not currently on the scope stack.
9272 if Is_Child_Unit (Curr_Unit)
9273 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
9274 N_Package_Instantiation
9275 and then not In_Open_Scopes (Curr_Unit)
9276 then
9277 return False;
9278 else
9279 return True;
9280 end if;
9281 end if;
9283 S := Scope (S);
9284 end loop;
9286 return False;
9287 end In_Instance;
9289 ----------------------
9290 -- In_Instance_Body --
9291 ----------------------
9293 function In_Instance_Body return Boolean is
9294 S : Entity_Id;
9296 begin
9297 S := Current_Scope;
9298 while Present (S) and then S /= Standard_Standard loop
9299 if Ekind_In (S, E_Function, E_Procedure)
9300 and then Is_Generic_Instance (S)
9301 then
9302 return True;
9304 elsif Ekind (S) = E_Package
9305 and then In_Package_Body (S)
9306 and then Is_Generic_Instance (S)
9307 then
9308 return True;
9309 end if;
9311 S := Scope (S);
9312 end loop;
9314 return False;
9315 end In_Instance_Body;
9317 -----------------------------
9318 -- In_Instance_Not_Visible --
9319 -----------------------------
9321 function In_Instance_Not_Visible return Boolean is
9322 S : Entity_Id;
9324 begin
9325 S := Current_Scope;
9326 while Present (S) and then S /= Standard_Standard loop
9327 if Ekind_In (S, E_Function, E_Procedure)
9328 and then Is_Generic_Instance (S)
9329 then
9330 return True;
9332 elsif Ekind (S) = E_Package
9333 and then (In_Package_Body (S) or else In_Private_Part (S))
9334 and then Is_Generic_Instance (S)
9335 then
9336 return True;
9337 end if;
9339 S := Scope (S);
9340 end loop;
9342 return False;
9343 end In_Instance_Not_Visible;
9345 ------------------------------
9346 -- In_Instance_Visible_Part --
9347 ------------------------------
9349 function In_Instance_Visible_Part return Boolean is
9350 S : Entity_Id;
9352 begin
9353 S := Current_Scope;
9354 while Present (S) and then S /= Standard_Standard loop
9355 if Ekind (S) = E_Package
9356 and then Is_Generic_Instance (S)
9357 and then not In_Package_Body (S)
9358 and then not In_Private_Part (S)
9359 then
9360 return True;
9361 end if;
9363 S := Scope (S);
9364 end loop;
9366 return False;
9367 end In_Instance_Visible_Part;
9369 ---------------------
9370 -- In_Package_Body --
9371 ---------------------
9373 function In_Package_Body return Boolean is
9374 S : Entity_Id;
9376 begin
9377 S := Current_Scope;
9378 while Present (S) and then S /= Standard_Standard loop
9379 if Ekind (S) = E_Package and then In_Package_Body (S) then
9380 return True;
9381 else
9382 S := Scope (S);
9383 end if;
9384 end loop;
9386 return False;
9387 end In_Package_Body;
9389 --------------------------------
9390 -- In_Parameter_Specification --
9391 --------------------------------
9393 function In_Parameter_Specification (N : Node_Id) return Boolean is
9394 PN : Node_Id;
9396 begin
9397 PN := Parent (N);
9398 while Present (PN) loop
9399 if Nkind (PN) = N_Parameter_Specification then
9400 return True;
9401 end if;
9403 PN := Parent (PN);
9404 end loop;
9406 return False;
9407 end In_Parameter_Specification;
9409 --------------------------
9410 -- In_Pragma_Expression --
9411 --------------------------
9413 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
9414 P : Node_Id;
9415 begin
9416 P := Parent (N);
9417 loop
9418 if No (P) then
9419 return False;
9420 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
9421 return True;
9422 else
9423 P := Parent (P);
9424 end if;
9425 end loop;
9426 end In_Pragma_Expression;
9428 -------------------------------------
9429 -- In_Reverse_Storage_Order_Object --
9430 -------------------------------------
9432 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
9433 Pref : Node_Id;
9434 Btyp : Entity_Id := Empty;
9436 begin
9437 -- Climb up indexed components
9439 Pref := N;
9440 loop
9441 case Nkind (Pref) is
9442 when N_Selected_Component =>
9443 Pref := Prefix (Pref);
9444 exit;
9446 when N_Indexed_Component =>
9447 Pref := Prefix (Pref);
9449 when others =>
9450 Pref := Empty;
9451 exit;
9452 end case;
9453 end loop;
9455 if Present (Pref) then
9456 Btyp := Base_Type (Etype (Pref));
9457 end if;
9459 return Present (Btyp)
9460 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
9461 and then Reverse_Storage_Order (Btyp);
9462 end In_Reverse_Storage_Order_Object;
9464 --------------------------------------
9465 -- In_Subprogram_Or_Concurrent_Unit --
9466 --------------------------------------
9468 function In_Subprogram_Or_Concurrent_Unit return Boolean is
9469 E : Entity_Id;
9470 K : Entity_Kind;
9472 begin
9473 -- Use scope chain to check successively outer scopes
9475 E := Current_Scope;
9476 loop
9477 K := Ekind (E);
9479 if K in Subprogram_Kind
9480 or else K in Concurrent_Kind
9481 or else K in Generic_Subprogram_Kind
9482 then
9483 return True;
9485 elsif E = Standard_Standard then
9486 return False;
9487 end if;
9489 E := Scope (E);
9490 end loop;
9491 end In_Subprogram_Or_Concurrent_Unit;
9493 ---------------------
9494 -- In_Visible_Part --
9495 ---------------------
9497 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
9498 begin
9499 return Is_Package_Or_Generic_Package (Scope_Id)
9500 and then In_Open_Scopes (Scope_Id)
9501 and then not In_Package_Body (Scope_Id)
9502 and then not In_Private_Part (Scope_Id);
9503 end In_Visible_Part;
9505 --------------------------------
9506 -- Incomplete_Or_Partial_View --
9507 --------------------------------
9509 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
9510 function Inspect_Decls
9511 (Decls : List_Id;
9512 Taft : Boolean := False) return Entity_Id;
9513 -- Check whether a declarative region contains the incomplete or partial
9514 -- view of Id.
9516 -------------------
9517 -- Inspect_Decls --
9518 -------------------
9520 function Inspect_Decls
9521 (Decls : List_Id;
9522 Taft : Boolean := False) return Entity_Id
9524 Decl : Node_Id;
9525 Match : Node_Id;
9527 begin
9528 Decl := First (Decls);
9529 while Present (Decl) loop
9530 Match := Empty;
9532 if Taft then
9533 if Nkind (Decl) = N_Incomplete_Type_Declaration then
9534 Match := Defining_Identifier (Decl);
9535 end if;
9537 else
9538 if Nkind_In (Decl, N_Private_Extension_Declaration,
9539 N_Private_Type_Declaration)
9540 then
9541 Match := Defining_Identifier (Decl);
9542 end if;
9543 end if;
9545 if Present (Match)
9546 and then Present (Full_View (Match))
9547 and then Full_View (Match) = Id
9548 then
9549 return Match;
9550 end if;
9552 Next (Decl);
9553 end loop;
9555 return Empty;
9556 end Inspect_Decls;
9558 -- Local variables
9560 Prev : Entity_Id;
9562 -- Start of processing for Incomplete_Or_Partial_View
9564 begin
9565 -- Deferred constant or incomplete type case
9567 Prev := Current_Entity_In_Scope (Id);
9569 if Present (Prev)
9570 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
9571 and then Present (Full_View (Prev))
9572 and then Full_View (Prev) = Id
9573 then
9574 return Prev;
9575 end if;
9577 -- Private or Taft amendment type case
9579 declare
9580 Pkg : constant Entity_Id := Scope (Id);
9581 Pkg_Decl : Node_Id := Pkg;
9583 begin
9584 if Present (Pkg) and then Ekind (Pkg) = E_Package then
9585 while Nkind (Pkg_Decl) /= N_Package_Specification loop
9586 Pkg_Decl := Parent (Pkg_Decl);
9587 end loop;
9589 -- It is knows that Typ has a private view, look for it in the
9590 -- visible declarations of the enclosing scope. A special case
9591 -- of this is when the two views have been exchanged - the full
9592 -- appears earlier than the private.
9594 if Has_Private_Declaration (Id) then
9595 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
9597 -- Exchanged view case, look in the private declarations
9599 if No (Prev) then
9600 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
9601 end if;
9603 return Prev;
9605 -- Otherwise if this is the package body, then Typ is a potential
9606 -- Taft amendment type. The incomplete view should be located in
9607 -- the private declarations of the enclosing scope.
9609 elsif In_Package_Body (Pkg) then
9610 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
9611 end if;
9612 end if;
9613 end;
9615 -- The type has no incomplete or private view
9617 return Empty;
9618 end Incomplete_Or_Partial_View;
9620 -----------------------------------------
9621 -- Inherit_Default_Init_Cond_Procedure --
9622 -----------------------------------------
9624 procedure Inherit_Default_Init_Cond_Procedure (Typ : Entity_Id) is
9625 Par_Typ : constant Entity_Id := Etype (Typ);
9627 begin
9628 -- A derived type inherits the default initial condition procedure of
9629 -- its parent type.
9631 if No (Default_Init_Cond_Procedure (Typ)) then
9632 Set_Default_Init_Cond_Procedure
9633 (Typ, Default_Init_Cond_Procedure (Par_Typ));
9634 end if;
9635 end Inherit_Default_Init_Cond_Procedure;
9637 ----------------------------
9638 -- Inherit_Rep_Item_Chain --
9639 ----------------------------
9641 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
9642 From_Item : constant Node_Id := First_Rep_Item (From_Typ);
9643 Item : Node_Id := Empty;
9644 Last_Item : Node_Id := Empty;
9646 begin
9647 -- Reach the end of the destination type's chain (if any) and capture
9648 -- the last item.
9650 Item := First_Rep_Item (Typ);
9651 while Present (Item) loop
9653 -- Do not inherit a chain that has been inherited already
9655 if Item = From_Item then
9656 return;
9657 end if;
9659 Last_Item := Item;
9660 Item := Next_Rep_Item (Item);
9661 end loop;
9663 -- When the destination type has a rep item chain, the chain of the
9664 -- source type is appended to it.
9666 if Present (Last_Item) then
9667 Set_Next_Rep_Item (Last_Item, From_Item);
9669 -- Otherwise the destination type directly inherits the rep item chain
9670 -- of the source type (if any).
9672 else
9673 Set_First_Rep_Item (Typ, From_Item);
9674 end if;
9675 end Inherit_Rep_Item_Chain;
9677 ---------------------------------
9678 -- Inherit_Subprogram_Contract --
9679 ---------------------------------
9681 procedure Inherit_Subprogram_Contract
9682 (Subp : Entity_Id;
9683 From_Subp : Entity_Id)
9685 procedure Inherit_Pragma (Prag_Id : Pragma_Id);
9686 -- Propagate a pragma denoted by Prag_Id from From_Subp's contract to
9687 -- Subp's contract.
9689 --------------------
9690 -- Inherit_Pragma --
9691 --------------------
9693 procedure Inherit_Pragma (Prag_Id : Pragma_Id) is
9694 Prag : constant Node_Id := Get_Pragma (From_Subp, Prag_Id);
9695 New_Prag : Node_Id;
9697 begin
9698 -- A pragma cannot be part of more than one First_Pragma/Next_Pragma
9699 -- chains, therefore the node must be replicated. The new pragma is
9700 -- flagged is inherited for distrinction purposes.
9702 if Present (Prag) then
9703 New_Prag := New_Copy_Tree (Prag);
9704 Set_Is_Inherited (New_Prag);
9706 Add_Contract_Item (New_Prag, Subp);
9707 end if;
9708 end Inherit_Pragma;
9710 -- Start of processing for Inherit_Subprogram_Contract
9712 begin
9713 -- Inheritance is carried out only when both entities are subprograms
9714 -- with contracts.
9716 if Is_Subprogram_Or_Generic_Subprogram (Subp)
9717 and then Is_Subprogram_Or_Generic_Subprogram (From_Subp)
9718 and then Present (Contract (From_Subp))
9719 then
9720 Inherit_Pragma (Pragma_Extensions_Visible);
9721 end if;
9722 end Inherit_Subprogram_Contract;
9724 ---------------------------------
9725 -- Insert_Explicit_Dereference --
9726 ---------------------------------
9728 procedure Insert_Explicit_Dereference (N : Node_Id) is
9729 New_Prefix : constant Node_Id := Relocate_Node (N);
9730 Ent : Entity_Id := Empty;
9731 Pref : Node_Id;
9732 I : Interp_Index;
9733 It : Interp;
9734 T : Entity_Id;
9736 begin
9737 Save_Interps (N, New_Prefix);
9739 Rewrite (N,
9740 Make_Explicit_Dereference (Sloc (Parent (N)),
9741 Prefix => New_Prefix));
9743 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
9745 if Is_Overloaded (New_Prefix) then
9747 -- The dereference is also overloaded, and its interpretations are
9748 -- the designated types of the interpretations of the original node.
9750 Set_Etype (N, Any_Type);
9752 Get_First_Interp (New_Prefix, I, It);
9753 while Present (It.Nam) loop
9754 T := It.Typ;
9756 if Is_Access_Type (T) then
9757 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
9758 end if;
9760 Get_Next_Interp (I, It);
9761 end loop;
9763 End_Interp_List;
9765 else
9766 -- Prefix is unambiguous: mark the original prefix (which might
9767 -- Come_From_Source) as a reference, since the new (relocated) one
9768 -- won't be taken into account.
9770 if Is_Entity_Name (New_Prefix) then
9771 Ent := Entity (New_Prefix);
9772 Pref := New_Prefix;
9774 -- For a retrieval of a subcomponent of some composite object,
9775 -- retrieve the ultimate entity if there is one.
9777 elsif Nkind_In (New_Prefix, N_Selected_Component,
9778 N_Indexed_Component)
9779 then
9780 Pref := Prefix (New_Prefix);
9781 while Present (Pref)
9782 and then Nkind_In (Pref, N_Selected_Component,
9783 N_Indexed_Component)
9784 loop
9785 Pref := Prefix (Pref);
9786 end loop;
9788 if Present (Pref) and then Is_Entity_Name (Pref) then
9789 Ent := Entity (Pref);
9790 end if;
9791 end if;
9793 -- Place the reference on the entity node
9795 if Present (Ent) then
9796 Generate_Reference (Ent, Pref);
9797 end if;
9798 end if;
9799 end Insert_Explicit_Dereference;
9801 ------------------------------------------
9802 -- Inspect_Deferred_Constant_Completion --
9803 ------------------------------------------
9805 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
9806 Decl : Node_Id;
9808 begin
9809 Decl := First (Decls);
9810 while Present (Decl) loop
9812 -- Deferred constant signature
9814 if Nkind (Decl) = N_Object_Declaration
9815 and then Constant_Present (Decl)
9816 and then No (Expression (Decl))
9818 -- No need to check internally generated constants
9820 and then Comes_From_Source (Decl)
9822 -- The constant is not completed. A full object declaration or a
9823 -- pragma Import complete a deferred constant.
9825 and then not Has_Completion (Defining_Identifier (Decl))
9826 then
9827 Error_Msg_N
9828 ("constant declaration requires initialization expression",
9829 Defining_Identifier (Decl));
9830 end if;
9832 Decl := Next (Decl);
9833 end loop;
9834 end Inspect_Deferred_Constant_Completion;
9836 -----------------------------
9837 -- Install_Generic_Formals --
9838 -----------------------------
9840 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
9841 E : Entity_Id;
9843 begin
9844 pragma Assert (Is_Generic_Subprogram (Subp_Id));
9846 E := First_Entity (Subp_Id);
9847 while Present (E) loop
9848 Install_Entity (E);
9849 Next_Entity (E);
9850 end loop;
9851 end Install_Generic_Formals;
9853 -----------------------------
9854 -- Is_Actual_Out_Parameter --
9855 -----------------------------
9857 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
9858 Formal : Entity_Id;
9859 Call : Node_Id;
9860 begin
9861 Find_Actual (N, Formal, Call);
9862 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
9863 end Is_Actual_Out_Parameter;
9865 -------------------------
9866 -- Is_Actual_Parameter --
9867 -------------------------
9869 function Is_Actual_Parameter (N : Node_Id) return Boolean is
9870 PK : constant Node_Kind := Nkind (Parent (N));
9872 begin
9873 case PK is
9874 when N_Parameter_Association =>
9875 return N = Explicit_Actual_Parameter (Parent (N));
9877 when N_Subprogram_Call =>
9878 return Is_List_Member (N)
9879 and then
9880 List_Containing (N) = Parameter_Associations (Parent (N));
9882 when others =>
9883 return False;
9884 end case;
9885 end Is_Actual_Parameter;
9887 --------------------------------
9888 -- Is_Actual_Tagged_Parameter --
9889 --------------------------------
9891 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
9892 Formal : Entity_Id;
9893 Call : Node_Id;
9894 begin
9895 Find_Actual (N, Formal, Call);
9896 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
9897 end Is_Actual_Tagged_Parameter;
9899 ---------------------
9900 -- Is_Aliased_View --
9901 ---------------------
9903 function Is_Aliased_View (Obj : Node_Id) return Boolean is
9904 E : Entity_Id;
9906 begin
9907 if Is_Entity_Name (Obj) then
9908 E := Entity (Obj);
9910 return
9911 (Is_Object (E)
9912 and then
9913 (Is_Aliased (E)
9914 or else (Present (Renamed_Object (E))
9915 and then Is_Aliased_View (Renamed_Object (E)))))
9917 or else ((Is_Formal (E)
9918 or else Ekind_In (E, E_Generic_In_Out_Parameter,
9919 E_Generic_In_Parameter))
9920 and then Is_Tagged_Type (Etype (E)))
9922 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
9924 -- Current instance of type, either directly or as rewritten
9925 -- reference to the current object.
9927 or else (Is_Entity_Name (Original_Node (Obj))
9928 and then Present (Entity (Original_Node (Obj)))
9929 and then Is_Type (Entity (Original_Node (Obj))))
9931 or else (Is_Type (E) and then E = Current_Scope)
9933 or else (Is_Incomplete_Or_Private_Type (E)
9934 and then Full_View (E) = Current_Scope)
9936 -- Ada 2012 AI05-0053: the return object of an extended return
9937 -- statement is aliased if its type is immutably limited.
9939 or else (Is_Return_Object (E)
9940 and then Is_Limited_View (Etype (E)));
9942 elsif Nkind (Obj) = N_Selected_Component then
9943 return Is_Aliased (Entity (Selector_Name (Obj)));
9945 elsif Nkind (Obj) = N_Indexed_Component then
9946 return Has_Aliased_Components (Etype (Prefix (Obj)))
9947 or else
9948 (Is_Access_Type (Etype (Prefix (Obj)))
9949 and then Has_Aliased_Components
9950 (Designated_Type (Etype (Prefix (Obj)))));
9952 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
9953 return Is_Tagged_Type (Etype (Obj))
9954 and then Is_Aliased_View (Expression (Obj));
9956 elsif Nkind (Obj) = N_Explicit_Dereference then
9957 return Nkind (Original_Node (Obj)) /= N_Function_Call;
9959 else
9960 return False;
9961 end if;
9962 end Is_Aliased_View;
9964 -------------------------
9965 -- Is_Ancestor_Package --
9966 -------------------------
9968 function Is_Ancestor_Package
9969 (E1 : Entity_Id;
9970 E2 : Entity_Id) return Boolean
9972 Par : Entity_Id;
9974 begin
9975 Par := E2;
9976 while Present (Par) and then Par /= Standard_Standard loop
9977 if Par = E1 then
9978 return True;
9979 end if;
9981 Par := Scope (Par);
9982 end loop;
9984 return False;
9985 end Is_Ancestor_Package;
9987 ----------------------
9988 -- Is_Atomic_Object --
9989 ----------------------
9991 function Is_Atomic_Object (N : Node_Id) return Boolean is
9993 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
9994 -- Determines if given object has atomic components
9996 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
9997 -- If prefix is an implicit dereference, examine designated type
9999 ----------------------
10000 -- Is_Atomic_Prefix --
10001 ----------------------
10003 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
10004 begin
10005 if Is_Access_Type (Etype (N)) then
10006 return
10007 Has_Atomic_Components (Designated_Type (Etype (N)));
10008 else
10009 return Object_Has_Atomic_Components (N);
10010 end if;
10011 end Is_Atomic_Prefix;
10013 ----------------------------------
10014 -- Object_Has_Atomic_Components --
10015 ----------------------------------
10017 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
10018 begin
10019 if Has_Atomic_Components (Etype (N))
10020 or else Is_Atomic (Etype (N))
10021 then
10022 return True;
10024 elsif Is_Entity_Name (N)
10025 and then (Has_Atomic_Components (Entity (N))
10026 or else Is_Atomic (Entity (N)))
10027 then
10028 return True;
10030 elsif Nkind (N) = N_Selected_Component
10031 and then Is_Atomic (Entity (Selector_Name (N)))
10032 then
10033 return True;
10035 elsif Nkind (N) = N_Indexed_Component
10036 or else Nkind (N) = N_Selected_Component
10037 then
10038 return Is_Atomic_Prefix (Prefix (N));
10040 else
10041 return False;
10042 end if;
10043 end Object_Has_Atomic_Components;
10045 -- Start of processing for Is_Atomic_Object
10047 begin
10048 -- Predicate is not relevant to subprograms
10050 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
10051 return False;
10053 elsif Is_Atomic (Etype (N))
10054 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
10055 then
10056 return True;
10058 elsif Nkind (N) = N_Selected_Component
10059 and then Is_Atomic (Entity (Selector_Name (N)))
10060 then
10061 return True;
10063 elsif Nkind (N) = N_Indexed_Component
10064 or else Nkind (N) = N_Selected_Component
10065 then
10066 return Is_Atomic_Prefix (Prefix (N));
10068 else
10069 return False;
10070 end if;
10071 end Is_Atomic_Object;
10073 -------------------------
10074 -- Is_Attribute_Result --
10075 -------------------------
10077 function Is_Attribute_Result (N : Node_Id) return Boolean is
10078 begin
10079 return Nkind (N) = N_Attribute_Reference
10080 and then Attribute_Name (N) = Name_Result;
10081 end Is_Attribute_Result;
10083 ------------------------------------
10084 -- Is_Body_Or_Package_Declaration --
10085 ------------------------------------
10087 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
10088 begin
10089 return Nkind_In (N, N_Entry_Body,
10090 N_Package_Body,
10091 N_Package_Declaration,
10092 N_Protected_Body,
10093 N_Subprogram_Body,
10094 N_Task_Body);
10095 end Is_Body_Or_Package_Declaration;
10097 -----------------------
10098 -- Is_Bounded_String --
10099 -----------------------
10101 function Is_Bounded_String (T : Entity_Id) return Boolean is
10102 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
10104 begin
10105 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
10106 -- Super_String, or one of the [Wide_]Wide_ versions. This will
10107 -- be True for all the Bounded_String types in instances of the
10108 -- Generic_Bounded_Length generics, and for types derived from those.
10110 return Present (Under)
10111 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
10112 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
10113 Is_RTE (Root_Type (Under), RO_WW_Super_String));
10114 end Is_Bounded_String;
10116 -------------------------
10117 -- Is_Child_Or_Sibling --
10118 -------------------------
10120 function Is_Child_Or_Sibling
10121 (Pack_1 : Entity_Id;
10122 Pack_2 : Entity_Id) return Boolean
10124 function Distance_From_Standard (Pack : Entity_Id) return Nat;
10125 -- Given an arbitrary package, return the number of "climbs" necessary
10126 -- to reach scope Standard_Standard.
10128 procedure Equalize_Depths
10129 (Pack : in out Entity_Id;
10130 Depth : in out Nat;
10131 Depth_To_Reach : Nat);
10132 -- Given an arbitrary package, its depth and a target depth to reach,
10133 -- climb the scope chain until the said depth is reached. The pointer
10134 -- to the package and its depth a modified during the climb.
10136 ----------------------------
10137 -- Distance_From_Standard --
10138 ----------------------------
10140 function Distance_From_Standard (Pack : Entity_Id) return Nat is
10141 Dist : Nat;
10142 Scop : Entity_Id;
10144 begin
10145 Dist := 0;
10146 Scop := Pack;
10147 while Present (Scop) and then Scop /= Standard_Standard loop
10148 Dist := Dist + 1;
10149 Scop := Scope (Scop);
10150 end loop;
10152 return Dist;
10153 end Distance_From_Standard;
10155 ---------------------
10156 -- Equalize_Depths --
10157 ---------------------
10159 procedure Equalize_Depths
10160 (Pack : in out Entity_Id;
10161 Depth : in out Nat;
10162 Depth_To_Reach : Nat)
10164 begin
10165 -- The package must be at a greater or equal depth
10167 if Depth < Depth_To_Reach then
10168 raise Program_Error;
10169 end if;
10171 -- Climb the scope chain until the desired depth is reached
10173 while Present (Pack) and then Depth /= Depth_To_Reach loop
10174 Pack := Scope (Pack);
10175 Depth := Depth - 1;
10176 end loop;
10177 end Equalize_Depths;
10179 -- Local variables
10181 P_1 : Entity_Id := Pack_1;
10182 P_1_Child : Boolean := False;
10183 P_1_Depth : Nat := Distance_From_Standard (P_1);
10184 P_2 : Entity_Id := Pack_2;
10185 P_2_Child : Boolean := False;
10186 P_2_Depth : Nat := Distance_From_Standard (P_2);
10188 -- Start of processing for Is_Child_Or_Sibling
10190 begin
10191 pragma Assert
10192 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
10194 -- Both packages denote the same entity, therefore they cannot be
10195 -- children or siblings.
10197 if P_1 = P_2 then
10198 return False;
10200 -- One of the packages is at a deeper level than the other. Note that
10201 -- both may still come from differen hierarchies.
10203 -- (root) P_2
10204 -- / \ :
10205 -- X P_2 or X
10206 -- : :
10207 -- P_1 P_1
10209 elsif P_1_Depth > P_2_Depth then
10210 Equalize_Depths
10211 (Pack => P_1,
10212 Depth => P_1_Depth,
10213 Depth_To_Reach => P_2_Depth);
10214 P_1_Child := True;
10216 -- (root) P_1
10217 -- / \ :
10218 -- P_1 X or X
10219 -- : :
10220 -- P_2 P_2
10222 elsif P_2_Depth > P_1_Depth then
10223 Equalize_Depths
10224 (Pack => P_2,
10225 Depth => P_2_Depth,
10226 Depth_To_Reach => P_1_Depth);
10227 P_2_Child := True;
10228 end if;
10230 -- At this stage the package pointers have been elevated to the same
10231 -- depth. If the related entities are the same, then one package is a
10232 -- potential child of the other:
10234 -- P_1
10235 -- :
10236 -- X became P_1 P_2 or vica versa
10237 -- :
10238 -- P_2
10240 if P_1 = P_2 then
10241 if P_1_Child then
10242 return Is_Child_Unit (Pack_1);
10244 else pragma Assert (P_2_Child);
10245 return Is_Child_Unit (Pack_2);
10246 end if;
10248 -- The packages may come from the same package chain or from entirely
10249 -- different hierarcies. To determine this, climb the scope stack until
10250 -- a common root is found.
10252 -- (root) (root 1) (root 2)
10253 -- / \ | |
10254 -- P_1 P_2 P_1 P_2
10256 else
10257 while Present (P_1) and then Present (P_2) loop
10259 -- The two packages may be siblings
10261 if P_1 = P_2 then
10262 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
10263 end if;
10265 P_1 := Scope (P_1);
10266 P_2 := Scope (P_2);
10267 end loop;
10268 end if;
10270 return False;
10271 end Is_Child_Or_Sibling;
10273 -----------------------------
10274 -- Is_Concurrent_Interface --
10275 -----------------------------
10277 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
10278 begin
10279 return Is_Interface (T)
10280 and then
10281 (Is_Protected_Interface (T)
10282 or else Is_Synchronized_Interface (T)
10283 or else Is_Task_Interface (T));
10284 end Is_Concurrent_Interface;
10286 ---------------------------
10287 -- Is_Container_Element --
10288 ---------------------------
10290 function Is_Container_Element (Exp : Node_Id) return Boolean is
10291 Loc : constant Source_Ptr := Sloc (Exp);
10292 Pref : constant Node_Id := Prefix (Exp);
10294 Call : Node_Id;
10295 -- Call to an indexing aspect
10297 Cont_Typ : Entity_Id;
10298 -- The type of the container being accessed
10300 Elem_Typ : Entity_Id;
10301 -- Its element type
10303 Indexing : Entity_Id;
10304 Is_Const : Boolean;
10305 -- Indicates that constant indexing is used, and the element is thus
10306 -- a constant.
10308 Ref_Typ : Entity_Id;
10309 -- The reference type returned by the indexing operation
10311 begin
10312 -- If C is a container, in a context that imposes the element type of
10313 -- that container, the indexing notation C (X) is rewritten as:
10315 -- Indexing (C, X).Discr.all
10317 -- where Indexing is one of the indexing aspects of the container.
10318 -- If the context does not require a reference, the construct can be
10319 -- rewritten as
10321 -- Element (C, X)
10323 -- First, verify that the construct has the proper form
10325 if not Expander_Active then
10326 return False;
10328 elsif Nkind (Pref) /= N_Selected_Component then
10329 return False;
10331 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
10332 return False;
10334 else
10335 Call := Prefix (Pref);
10336 Ref_Typ := Etype (Call);
10337 end if;
10339 if not Has_Implicit_Dereference (Ref_Typ)
10340 or else No (First (Parameter_Associations (Call)))
10341 or else not Is_Entity_Name (Name (Call))
10342 then
10343 return False;
10344 end if;
10346 -- Retrieve type of container object, and its iterator aspects
10348 Cont_Typ := Etype (First (Parameter_Associations (Call)));
10349 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
10350 Is_Const := False;
10352 if No (Indexing) then
10354 -- Container should have at least one indexing operation
10356 return False;
10358 elsif Entity (Name (Call)) /= Entity (Indexing) then
10360 -- This may be a variable indexing operation
10362 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
10364 if No (Indexing)
10365 or else Entity (Name (Call)) /= Entity (Indexing)
10366 then
10367 return False;
10368 end if;
10370 else
10371 Is_Const := True;
10372 end if;
10374 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
10376 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
10377 return False;
10378 end if;
10380 -- Check that the expression is not the target of an assignment, in
10381 -- which case the rewriting is not possible.
10383 if not Is_Const then
10384 declare
10385 Par : Node_Id;
10387 begin
10388 Par := Exp;
10389 while Present (Par)
10390 loop
10391 if Nkind (Parent (Par)) = N_Assignment_Statement
10392 and then Par = Name (Parent (Par))
10393 then
10394 return False;
10396 -- A renaming produces a reference, and the transformation
10397 -- does not apply.
10399 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
10400 return False;
10402 elsif Nkind_In
10403 (Nkind (Parent (Par)), N_Function_Call,
10404 N_Procedure_Call_Statement,
10405 N_Entry_Call_Statement)
10406 then
10407 -- Check that the element is not part of an actual for an
10408 -- in-out parameter.
10410 declare
10411 F : Entity_Id;
10412 A : Node_Id;
10414 begin
10415 F := First_Formal (Entity (Name (Parent (Par))));
10416 A := First (Parameter_Associations (Parent (Par)));
10417 while Present (F) loop
10418 if A = Par and then Ekind (F) /= E_In_Parameter then
10419 return False;
10420 end if;
10422 Next_Formal (F);
10423 Next (A);
10424 end loop;
10425 end;
10427 -- E_In_Parameter in a call: element is not modified.
10429 exit;
10430 end if;
10432 Par := Parent (Par);
10433 end loop;
10434 end;
10435 end if;
10437 -- The expression has the proper form and the context requires the
10438 -- element type. Retrieve the Element function of the container and
10439 -- rewrite the construct as a call to it.
10441 declare
10442 Op : Elmt_Id;
10444 begin
10445 Op := First_Elmt (Primitive_Operations (Cont_Typ));
10446 while Present (Op) loop
10447 exit when Chars (Node (Op)) = Name_Element;
10448 Next_Elmt (Op);
10449 end loop;
10451 if No (Op) then
10452 return False;
10454 else
10455 Rewrite (Exp,
10456 Make_Function_Call (Loc,
10457 Name => New_Occurrence_Of (Node (Op), Loc),
10458 Parameter_Associations => Parameter_Associations (Call)));
10459 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
10460 return True;
10461 end if;
10462 end;
10463 end Is_Container_Element;
10465 -----------------------
10466 -- Is_Constant_Bound --
10467 -----------------------
10469 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
10470 begin
10471 if Compile_Time_Known_Value (Exp) then
10472 return True;
10474 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
10475 return Is_Constant_Object (Entity (Exp))
10476 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
10478 elsif Nkind (Exp) in N_Binary_Op then
10479 return Is_Constant_Bound (Left_Opnd (Exp))
10480 and then Is_Constant_Bound (Right_Opnd (Exp))
10481 and then Scope (Entity (Exp)) = Standard_Standard;
10483 else
10484 return False;
10485 end if;
10486 end Is_Constant_Bound;
10488 --------------------------------------
10489 -- Is_Controlling_Limited_Procedure --
10490 --------------------------------------
10492 function Is_Controlling_Limited_Procedure
10493 (Proc_Nam : Entity_Id) return Boolean
10495 Param_Typ : Entity_Id := Empty;
10497 begin
10498 if Ekind (Proc_Nam) = E_Procedure
10499 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
10500 then
10501 Param_Typ := Etype (Parameter_Type (First (
10502 Parameter_Specifications (Parent (Proc_Nam)))));
10504 -- In this case where an Itype was created, the procedure call has been
10505 -- rewritten.
10507 elsif Present (Associated_Node_For_Itype (Proc_Nam))
10508 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
10509 and then
10510 Present (Parameter_Associations
10511 (Associated_Node_For_Itype (Proc_Nam)))
10512 then
10513 Param_Typ :=
10514 Etype (First (Parameter_Associations
10515 (Associated_Node_For_Itype (Proc_Nam))));
10516 end if;
10518 if Present (Param_Typ) then
10519 return
10520 Is_Interface (Param_Typ)
10521 and then Is_Limited_Record (Param_Typ);
10522 end if;
10524 return False;
10525 end Is_Controlling_Limited_Procedure;
10527 -----------------------------
10528 -- Is_CPP_Constructor_Call --
10529 -----------------------------
10531 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
10532 begin
10533 return Nkind (N) = N_Function_Call
10534 and then Is_CPP_Class (Etype (Etype (N)))
10535 and then Is_Constructor (Entity (Name (N)))
10536 and then Is_Imported (Entity (Name (N)));
10537 end Is_CPP_Constructor_Call;
10539 --------------------
10540 -- Is_Declaration --
10541 --------------------
10543 function Is_Declaration (N : Node_Id) return Boolean is
10544 begin
10545 case Nkind (N) is
10546 when N_Abstract_Subprogram_Declaration |
10547 N_Exception_Declaration |
10548 N_Exception_Renaming_Declaration |
10549 N_Full_Type_Declaration |
10550 N_Generic_Function_Renaming_Declaration |
10551 N_Generic_Package_Declaration |
10552 N_Generic_Package_Renaming_Declaration |
10553 N_Generic_Procedure_Renaming_Declaration |
10554 N_Generic_Subprogram_Declaration |
10555 N_Number_Declaration |
10556 N_Object_Declaration |
10557 N_Object_Renaming_Declaration |
10558 N_Package_Declaration |
10559 N_Package_Renaming_Declaration |
10560 N_Private_Extension_Declaration |
10561 N_Private_Type_Declaration |
10562 N_Subprogram_Declaration |
10563 N_Subprogram_Renaming_Declaration |
10564 N_Subtype_Declaration =>
10565 return True;
10567 when others =>
10568 return False;
10569 end case;
10570 end Is_Declaration;
10572 -----------------
10573 -- Is_Delegate --
10574 -----------------
10576 function Is_Delegate (T : Entity_Id) return Boolean is
10577 Desig_Type : Entity_Id;
10579 begin
10580 if VM_Target /= CLI_Target then
10581 return False;
10582 end if;
10584 -- Access-to-subprograms are delegates in CIL
10586 if Ekind (T) = E_Access_Subprogram_Type then
10587 return True;
10588 end if;
10590 if not Is_Access_Type (T) then
10592 -- A delegate is a managed pointer. If no designated type is defined
10593 -- it means that it's not a delegate.
10595 return False;
10596 end if;
10598 Desig_Type := Etype (Directly_Designated_Type (T));
10600 if not Is_Tagged_Type (Desig_Type) then
10601 return False;
10602 end if;
10604 -- Test if the type is inherited from [mscorlib]System.Delegate
10606 while Etype (Desig_Type) /= Desig_Type loop
10607 if Chars (Scope (Desig_Type)) /= No_Name
10608 and then Is_Imported (Scope (Desig_Type))
10609 and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
10610 then
10611 return True;
10612 end if;
10614 Desig_Type := Etype (Desig_Type);
10615 end loop;
10617 return False;
10618 end Is_Delegate;
10620 ----------------------------------------------
10621 -- Is_Dependent_Component_Of_Mutable_Object --
10622 ----------------------------------------------
10624 function Is_Dependent_Component_Of_Mutable_Object
10625 (Object : Node_Id) return Boolean
10627 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
10628 -- Returns True if and only if Comp is declared within a variant part
10630 --------------------------------
10631 -- Is_Declared_Within_Variant --
10632 --------------------------------
10634 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
10635 Comp_Decl : constant Node_Id := Parent (Comp);
10636 Comp_List : constant Node_Id := Parent (Comp_Decl);
10637 begin
10638 return Nkind (Parent (Comp_List)) = N_Variant;
10639 end Is_Declared_Within_Variant;
10641 P : Node_Id;
10642 Prefix_Type : Entity_Id;
10643 P_Aliased : Boolean := False;
10644 Comp : Entity_Id;
10646 Deref : Node_Id := Object;
10647 -- Dereference node, in something like X.all.Y(2)
10649 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
10651 begin
10652 -- Find the dereference node if any
10654 while Nkind_In (Deref, N_Indexed_Component,
10655 N_Selected_Component,
10656 N_Slice)
10657 loop
10658 Deref := Prefix (Deref);
10659 end loop;
10661 -- Ada 2005: If we have a component or slice of a dereference,
10662 -- something like X.all.Y (2), and the type of X is access-to-constant,
10663 -- Is_Variable will return False, because it is indeed a constant
10664 -- view. But it might be a view of a variable object, so we want the
10665 -- following condition to be True in that case.
10667 if Is_Variable (Object)
10668 or else (Ada_Version >= Ada_2005
10669 and then Nkind (Deref) = N_Explicit_Dereference)
10670 then
10671 if Nkind (Object) = N_Selected_Component then
10672 P := Prefix (Object);
10673 Prefix_Type := Etype (P);
10675 if Is_Entity_Name (P) then
10676 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
10677 Prefix_Type := Base_Type (Prefix_Type);
10678 end if;
10680 if Is_Aliased (Entity (P)) then
10681 P_Aliased := True;
10682 end if;
10684 -- A discriminant check on a selected component may be expanded
10685 -- into a dereference when removing side-effects. Recover the
10686 -- original node and its type, which may be unconstrained.
10688 elsif Nkind (P) = N_Explicit_Dereference
10689 and then not (Comes_From_Source (P))
10690 then
10691 P := Original_Node (P);
10692 Prefix_Type := Etype (P);
10694 else
10695 -- Check for prefix being an aliased component???
10697 null;
10699 end if;
10701 -- A heap object is constrained by its initial value
10703 -- Ada 2005 (AI-363): Always assume the object could be mutable in
10704 -- the dereferenced case, since the access value might denote an
10705 -- unconstrained aliased object, whereas in Ada 95 the designated
10706 -- object is guaranteed to be constrained. A worst-case assumption
10707 -- has to apply in Ada 2005 because we can't tell at compile
10708 -- time whether the object is "constrained by its initial value"
10709 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
10710 -- rules (these rules are acknowledged to need fixing).
10712 if Ada_Version < Ada_2005 then
10713 if Is_Access_Type (Prefix_Type)
10714 or else Nkind (P) = N_Explicit_Dereference
10715 then
10716 return False;
10717 end if;
10719 else pragma Assert (Ada_Version >= Ada_2005);
10720 if Is_Access_Type (Prefix_Type) then
10722 -- If the access type is pool-specific, and there is no
10723 -- constrained partial view of the designated type, then the
10724 -- designated object is known to be constrained.
10726 if Ekind (Prefix_Type) = E_Access_Type
10727 and then not Object_Type_Has_Constrained_Partial_View
10728 (Typ => Designated_Type (Prefix_Type),
10729 Scop => Current_Scope)
10730 then
10731 return False;
10733 -- Otherwise (general access type, or there is a constrained
10734 -- partial view of the designated type), we need to check
10735 -- based on the designated type.
10737 else
10738 Prefix_Type := Designated_Type (Prefix_Type);
10739 end if;
10740 end if;
10741 end if;
10743 Comp :=
10744 Original_Record_Component (Entity (Selector_Name (Object)));
10746 -- As per AI-0017, the renaming is illegal in a generic body, even
10747 -- if the subtype is indefinite.
10749 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
10751 if not Is_Constrained (Prefix_Type)
10752 and then (not Is_Indefinite_Subtype (Prefix_Type)
10753 or else
10754 (Is_Generic_Type (Prefix_Type)
10755 and then Ekind (Current_Scope) = E_Generic_Package
10756 and then In_Package_Body (Current_Scope)))
10758 and then (Is_Declared_Within_Variant (Comp)
10759 or else Has_Discriminant_Dependent_Constraint (Comp))
10760 and then (not P_Aliased or else Ada_Version >= Ada_2005)
10761 then
10762 return True;
10764 -- If the prefix is of an access type at this point, then we want
10765 -- to return False, rather than calling this function recursively
10766 -- on the access object (which itself might be a discriminant-
10767 -- dependent component of some other object, but that isn't
10768 -- relevant to checking the object passed to us). This avoids
10769 -- issuing wrong errors when compiling with -gnatc, where there
10770 -- can be implicit dereferences that have not been expanded.
10772 elsif Is_Access_Type (Etype (Prefix (Object))) then
10773 return False;
10775 else
10776 return
10777 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10778 end if;
10780 elsif Nkind (Object) = N_Indexed_Component
10781 or else Nkind (Object) = N_Slice
10782 then
10783 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10785 -- A type conversion that Is_Variable is a view conversion:
10786 -- go back to the denoted object.
10788 elsif Nkind (Object) = N_Type_Conversion then
10789 return
10790 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
10791 end if;
10792 end if;
10794 return False;
10795 end Is_Dependent_Component_Of_Mutable_Object;
10797 ---------------------
10798 -- Is_Dereferenced --
10799 ---------------------
10801 function Is_Dereferenced (N : Node_Id) return Boolean is
10802 P : constant Node_Id := Parent (N);
10803 begin
10804 return Nkind_In (P, N_Selected_Component,
10805 N_Explicit_Dereference,
10806 N_Indexed_Component,
10807 N_Slice)
10808 and then Prefix (P) = N;
10809 end Is_Dereferenced;
10811 ----------------------
10812 -- Is_Descendent_Of --
10813 ----------------------
10815 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
10816 T : Entity_Id;
10817 Etyp : Entity_Id;
10819 begin
10820 pragma Assert (Nkind (T1) in N_Entity);
10821 pragma Assert (Nkind (T2) in N_Entity);
10823 T := Base_Type (T1);
10825 -- Immediate return if the types match
10827 if T = T2 then
10828 return True;
10830 -- Comment needed here ???
10832 elsif Ekind (T) = E_Class_Wide_Type then
10833 return Etype (T) = T2;
10835 -- All other cases
10837 else
10838 loop
10839 Etyp := Etype (T);
10841 -- Done if we found the type we are looking for
10843 if Etyp = T2 then
10844 return True;
10846 -- Done if no more derivations to check
10848 elsif T = T1
10849 or else T = Etyp
10850 then
10851 return False;
10853 -- Following test catches error cases resulting from prev errors
10855 elsif No (Etyp) then
10856 return False;
10858 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
10859 return False;
10861 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
10862 return False;
10863 end if;
10865 T := Base_Type (Etyp);
10866 end loop;
10867 end if;
10868 end Is_Descendent_Of;
10870 -----------------------------
10871 -- Is_Effectively_Volatile --
10872 -----------------------------
10874 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
10875 begin
10876 if Is_Type (Id) then
10878 -- An arbitrary type is effectively volatile when it is subject to
10879 -- pragma Atomic or Volatile.
10881 if Is_Volatile (Id) then
10882 return True;
10884 -- An array type is effectively volatile when it is subject to pragma
10885 -- Atomic_Components or Volatile_Components or its compolent type is
10886 -- effectively volatile.
10888 elsif Is_Array_Type (Id) then
10889 return
10890 Has_Volatile_Components (Id)
10891 or else
10892 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
10894 else
10895 return False;
10896 end if;
10898 -- Otherwise Id denotes an object
10900 else
10901 return
10902 Is_Volatile (Id)
10903 or else Has_Volatile_Components (Id)
10904 or else Is_Effectively_Volatile (Etype (Id));
10905 end if;
10906 end Is_Effectively_Volatile;
10908 ------------------------------------
10909 -- Is_Effectively_Volatile_Object --
10910 ------------------------------------
10912 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
10913 begin
10914 if Is_Entity_Name (N) then
10915 return Is_Effectively_Volatile (Entity (N));
10917 elsif Nkind (N) = N_Expanded_Name then
10918 return Is_Effectively_Volatile (Entity (N));
10920 elsif Nkind (N) = N_Indexed_Component then
10921 return Is_Effectively_Volatile_Object (Prefix (N));
10923 elsif Nkind (N) = N_Selected_Component then
10924 return
10925 Is_Effectively_Volatile_Object (Prefix (N))
10926 or else
10927 Is_Effectively_Volatile_Object (Selector_Name (N));
10929 else
10930 return False;
10931 end if;
10932 end Is_Effectively_Volatile_Object;
10934 ----------------------------
10935 -- Is_Expression_Function --
10936 ----------------------------
10938 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
10939 Decl : Node_Id;
10941 begin
10942 if Ekind (Subp) /= E_Function then
10943 return False;
10945 else
10946 Decl := Unit_Declaration_Node (Subp);
10947 return Nkind (Decl) = N_Subprogram_Declaration
10948 and then
10949 (Nkind (Original_Node (Decl)) = N_Expression_Function
10950 or else
10951 (Present (Corresponding_Body (Decl))
10952 and then
10953 Nkind (Original_Node
10954 (Unit_Declaration_Node
10955 (Corresponding_Body (Decl)))) =
10956 N_Expression_Function));
10957 end if;
10958 end Is_Expression_Function;
10960 -----------------------
10961 -- Is_EVF_Expression --
10962 -----------------------
10964 function Is_EVF_Expression (N : Node_Id) return Boolean is
10965 Orig_N : constant Node_Id := Original_Node (N);
10966 Alt : Node_Id;
10967 Expr : Node_Id;
10968 Id : Entity_Id;
10970 begin
10971 -- Detect a reference to a formal parameter of a specific tagged type
10972 -- whose related subprogram is subject to pragma Expresions_Visible with
10973 -- value "False".
10975 if Is_Entity_Name (N) and then Present (Entity (N)) then
10976 Id := Entity (N);
10978 return
10979 Is_Formal (Id)
10980 and then Is_Specific_Tagged_Type (Etype (Id))
10981 and then Extensions_Visible_Status (Id) =
10982 Extensions_Visible_False;
10984 -- A case expression is an EVF expression when it contains at least one
10985 -- EVF dependent_expression. Note that a case expression may have been
10986 -- expanded, hence the use of Original_Node.
10988 elsif Nkind (Orig_N) = N_Case_Expression then
10989 Alt := First (Alternatives (Orig_N));
10990 while Present (Alt) loop
10991 if Is_EVF_Expression (Expression (Alt)) then
10992 return True;
10993 end if;
10995 Next (Alt);
10996 end loop;
10998 -- An if expression is an EVF expression when it contains at least one
10999 -- EVF dependent_expression. Note that an if expression may have been
11000 -- expanded, hence the use of Original_Node.
11002 elsif Nkind (Orig_N) = N_If_Expression then
11003 Expr := Next (First (Expressions (Orig_N)));
11004 while Present (Expr) loop
11005 if Is_EVF_Expression (Expr) then
11006 return True;
11007 end if;
11009 Next (Expr);
11010 end loop;
11012 -- A qualified expression or a type conversion is an EVF expression when
11013 -- its operand is an EVF expression.
11015 elsif Nkind_In (N, N_Qualified_Expression,
11016 N_Unchecked_Type_Conversion,
11017 N_Type_Conversion)
11018 then
11019 return Is_EVF_Expression (Expression (N));
11021 -- Attributes 'Loop_Entry, 'Old and 'Update are an EVF expression when
11022 -- their prefix denotes an EVF expression.
11024 elsif Nkind (N) = N_Attribute_Reference
11025 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
11026 Name_Old,
11027 Name_Update)
11028 then
11029 return Is_EVF_Expression (Prefix (N));
11030 end if;
11032 return False;
11033 end Is_EVF_Expression;
11035 --------------
11036 -- Is_False --
11037 --------------
11039 function Is_False (U : Uint) return Boolean is
11040 begin
11041 return (U = 0);
11042 end Is_False;
11044 ---------------------------
11045 -- Is_Fixed_Model_Number --
11046 ---------------------------
11048 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
11049 S : constant Ureal := Small_Value (T);
11050 M : Urealp.Save_Mark;
11051 R : Boolean;
11052 begin
11053 M := Urealp.Mark;
11054 R := (U = UR_Trunc (U / S) * S);
11055 Urealp.Release (M);
11056 return R;
11057 end Is_Fixed_Model_Number;
11059 -------------------------------
11060 -- Is_Fully_Initialized_Type --
11061 -------------------------------
11063 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
11064 begin
11065 -- Scalar types
11067 if Is_Scalar_Type (Typ) then
11069 -- A scalar type with an aspect Default_Value is fully initialized
11071 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
11072 -- of a scalar type, but we don't take that into account here, since
11073 -- we don't want these to affect warnings.
11075 return Has_Default_Aspect (Typ);
11077 elsif Is_Access_Type (Typ) then
11078 return True;
11080 elsif Is_Array_Type (Typ) then
11081 if Is_Fully_Initialized_Type (Component_Type (Typ))
11082 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
11083 then
11084 return True;
11085 end if;
11087 -- An interesting case, if we have a constrained type one of whose
11088 -- bounds is known to be null, then there are no elements to be
11089 -- initialized, so all the elements are initialized.
11091 if Is_Constrained (Typ) then
11092 declare
11093 Indx : Node_Id;
11094 Indx_Typ : Entity_Id;
11095 Lbd, Hbd : Node_Id;
11097 begin
11098 Indx := First_Index (Typ);
11099 while Present (Indx) loop
11100 if Etype (Indx) = Any_Type then
11101 return False;
11103 -- If index is a range, use directly
11105 elsif Nkind (Indx) = N_Range then
11106 Lbd := Low_Bound (Indx);
11107 Hbd := High_Bound (Indx);
11109 else
11110 Indx_Typ := Etype (Indx);
11112 if Is_Private_Type (Indx_Typ) then
11113 Indx_Typ := Full_View (Indx_Typ);
11114 end if;
11116 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
11117 return False;
11118 else
11119 Lbd := Type_Low_Bound (Indx_Typ);
11120 Hbd := Type_High_Bound (Indx_Typ);
11121 end if;
11122 end if;
11124 if Compile_Time_Known_Value (Lbd)
11125 and then
11126 Compile_Time_Known_Value (Hbd)
11127 then
11128 if Expr_Value (Hbd) < Expr_Value (Lbd) then
11129 return True;
11130 end if;
11131 end if;
11133 Next_Index (Indx);
11134 end loop;
11135 end;
11136 end if;
11138 -- If no null indexes, then type is not fully initialized
11140 return False;
11142 -- Record types
11144 elsif Is_Record_Type (Typ) then
11145 if Has_Discriminants (Typ)
11146 and then
11147 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
11148 and then Is_Fully_Initialized_Variant (Typ)
11149 then
11150 return True;
11151 end if;
11153 -- We consider bounded string types to be fully initialized, because
11154 -- otherwise we get false alarms when the Data component is not
11155 -- default-initialized.
11157 if Is_Bounded_String (Typ) then
11158 return True;
11159 end if;
11161 -- Controlled records are considered to be fully initialized if
11162 -- there is a user defined Initialize routine. This may not be
11163 -- entirely correct, but as the spec notes, we are guessing here
11164 -- what is best from the point of view of issuing warnings.
11166 if Is_Controlled (Typ) then
11167 declare
11168 Utyp : constant Entity_Id := Underlying_Type (Typ);
11170 begin
11171 if Present (Utyp) then
11172 declare
11173 Init : constant Entity_Id :=
11174 (Find_Prim_Op
11175 (Underlying_Type (Typ), Name_Initialize));
11177 begin
11178 if Present (Init)
11179 and then Comes_From_Source (Init)
11180 and then not
11181 Is_Predefined_File_Name
11182 (File_Name (Get_Source_File_Index (Sloc (Init))))
11183 then
11184 return True;
11186 elsif Has_Null_Extension (Typ)
11187 and then
11188 Is_Fully_Initialized_Type
11189 (Etype (Base_Type (Typ)))
11190 then
11191 return True;
11192 end if;
11193 end;
11194 end if;
11195 end;
11196 end if;
11198 -- Otherwise see if all record components are initialized
11200 declare
11201 Ent : Entity_Id;
11203 begin
11204 Ent := First_Entity (Typ);
11205 while Present (Ent) loop
11206 if Ekind (Ent) = E_Component
11207 and then (No (Parent (Ent))
11208 or else No (Expression (Parent (Ent))))
11209 and then not Is_Fully_Initialized_Type (Etype (Ent))
11211 -- Special VM case for tag components, which need to be
11212 -- defined in this case, but are never initialized as VMs
11213 -- are using other dispatching mechanisms. Ignore this
11214 -- uninitialized case. Note that this applies both to the
11215 -- uTag entry and the main vtable pointer (CPP_Class case).
11217 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
11218 then
11219 return False;
11220 end if;
11222 Next_Entity (Ent);
11223 end loop;
11224 end;
11226 -- No uninitialized components, so type is fully initialized.
11227 -- Note that this catches the case of no components as well.
11229 return True;
11231 elsif Is_Concurrent_Type (Typ) then
11232 return True;
11234 elsif Is_Private_Type (Typ) then
11235 declare
11236 U : constant Entity_Id := Underlying_Type (Typ);
11238 begin
11239 if No (U) then
11240 return False;
11241 else
11242 return Is_Fully_Initialized_Type (U);
11243 end if;
11244 end;
11246 else
11247 return False;
11248 end if;
11249 end Is_Fully_Initialized_Type;
11251 ----------------------------------
11252 -- Is_Fully_Initialized_Variant --
11253 ----------------------------------
11255 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
11256 Loc : constant Source_Ptr := Sloc (Typ);
11257 Constraints : constant List_Id := New_List;
11258 Components : constant Elist_Id := New_Elmt_List;
11259 Comp_Elmt : Elmt_Id;
11260 Comp_Id : Node_Id;
11261 Comp_List : Node_Id;
11262 Discr : Entity_Id;
11263 Discr_Val : Node_Id;
11265 Report_Errors : Boolean;
11266 pragma Warnings (Off, Report_Errors);
11268 begin
11269 if Serious_Errors_Detected > 0 then
11270 return False;
11271 end if;
11273 if Is_Record_Type (Typ)
11274 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11275 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
11276 then
11277 Comp_List := Component_List (Type_Definition (Parent (Typ)));
11279 Discr := First_Discriminant (Typ);
11280 while Present (Discr) loop
11281 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
11282 Discr_Val := Expression (Parent (Discr));
11284 if Present (Discr_Val)
11285 and then Is_OK_Static_Expression (Discr_Val)
11286 then
11287 Append_To (Constraints,
11288 Make_Component_Association (Loc,
11289 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
11290 Expression => New_Copy (Discr_Val)));
11291 else
11292 return False;
11293 end if;
11294 else
11295 return False;
11296 end if;
11298 Next_Discriminant (Discr);
11299 end loop;
11301 Gather_Components
11302 (Typ => Typ,
11303 Comp_List => Comp_List,
11304 Governed_By => Constraints,
11305 Into => Components,
11306 Report_Errors => Report_Errors);
11308 -- Check that each component present is fully initialized
11310 Comp_Elmt := First_Elmt (Components);
11311 while Present (Comp_Elmt) loop
11312 Comp_Id := Node (Comp_Elmt);
11314 if Ekind (Comp_Id) = E_Component
11315 and then (No (Parent (Comp_Id))
11316 or else No (Expression (Parent (Comp_Id))))
11317 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
11318 then
11319 return False;
11320 end if;
11322 Next_Elmt (Comp_Elmt);
11323 end loop;
11325 return True;
11327 elsif Is_Private_Type (Typ) then
11328 declare
11329 U : constant Entity_Id := Underlying_Type (Typ);
11331 begin
11332 if No (U) then
11333 return False;
11334 else
11335 return Is_Fully_Initialized_Variant (U);
11336 end if;
11337 end;
11339 else
11340 return False;
11341 end if;
11342 end Is_Fully_Initialized_Variant;
11344 ----------------------------
11345 -- Is_Inherited_Operation --
11346 ----------------------------
11348 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
11349 pragma Assert (Is_Overloadable (E));
11350 Kind : constant Node_Kind := Nkind (Parent (E));
11351 begin
11352 return Kind = N_Full_Type_Declaration
11353 or else Kind = N_Private_Extension_Declaration
11354 or else Kind = N_Subtype_Declaration
11355 or else (Ekind (E) = E_Enumeration_Literal
11356 and then Is_Derived_Type (Etype (E)));
11357 end Is_Inherited_Operation;
11359 -------------------------------------
11360 -- Is_Inherited_Operation_For_Type --
11361 -------------------------------------
11363 function Is_Inherited_Operation_For_Type
11364 (E : Entity_Id;
11365 Typ : Entity_Id) return Boolean
11367 begin
11368 -- Check that the operation has been created by the type declaration
11370 return Is_Inherited_Operation (E)
11371 and then Defining_Identifier (Parent (E)) = Typ;
11372 end Is_Inherited_Operation_For_Type;
11374 -----------------
11375 -- Is_Iterator --
11376 -----------------
11378 function Is_Iterator (Typ : Entity_Id) return Boolean is
11379 Ifaces_List : Elist_Id;
11380 Iface_Elmt : Elmt_Id;
11381 Iface : Entity_Id;
11383 begin
11384 if Is_Class_Wide_Type (Typ)
11385 and then Nam_In (Chars (Etype (Typ)), Name_Forward_Iterator,
11386 Name_Reversible_Iterator)
11387 and then
11388 Is_Predefined_File_Name
11389 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
11390 then
11391 return True;
11393 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
11394 return False;
11396 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
11397 return True;
11399 else
11400 Collect_Interfaces (Typ, Ifaces_List);
11402 Iface_Elmt := First_Elmt (Ifaces_List);
11403 while Present (Iface_Elmt) loop
11404 Iface := Node (Iface_Elmt);
11405 if Chars (Iface) = Name_Forward_Iterator
11406 and then
11407 Is_Predefined_File_Name
11408 (Unit_File_Name (Get_Source_Unit (Iface)))
11409 then
11410 return True;
11411 end if;
11413 Next_Elmt (Iface_Elmt);
11414 end loop;
11416 return False;
11417 end if;
11418 end Is_Iterator;
11420 ------------
11421 -- Is_LHS --
11422 ------------
11424 -- We seem to have a lot of overlapping functions that do similar things
11425 -- (testing for left hand sides or lvalues???).
11427 function Is_LHS (N : Node_Id) return Is_LHS_Result is
11428 P : constant Node_Id := Parent (N);
11430 begin
11431 -- Return True if we are the left hand side of an assignment statement
11433 if Nkind (P) = N_Assignment_Statement then
11434 if Name (P) = N then
11435 return Yes;
11436 else
11437 return No;
11438 end if;
11440 -- Case of prefix of indexed or selected component or slice
11442 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
11443 and then N = Prefix (P)
11444 then
11445 -- Here we have the case where the parent P is N.Q or N(Q .. R).
11446 -- If P is an LHS, then N is also effectively an LHS, but there
11447 -- is an important exception. If N is of an access type, then
11448 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
11449 -- case this makes N.all a left hand side but not N itself.
11451 -- If we don't know the type yet, this is the case where we return
11452 -- Unknown, since the answer depends on the type which is unknown.
11454 if No (Etype (N)) then
11455 return Unknown;
11457 -- We have an Etype set, so we can check it
11459 elsif Is_Access_Type (Etype (N)) then
11460 return No;
11462 -- OK, not access type case, so just test whole expression
11464 else
11465 return Is_LHS (P);
11466 end if;
11468 -- All other cases are not left hand sides
11470 else
11471 return No;
11472 end if;
11473 end Is_LHS;
11475 -----------------------------
11476 -- Is_Library_Level_Entity --
11477 -----------------------------
11479 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
11480 begin
11481 -- The following is a small optimization, and it also properly handles
11482 -- discriminals, which in task bodies might appear in expressions before
11483 -- the corresponding procedure has been created, and which therefore do
11484 -- not have an assigned scope.
11486 if Is_Formal (E) then
11487 return False;
11488 end if;
11490 -- Normal test is simply that the enclosing dynamic scope is Standard
11492 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
11493 end Is_Library_Level_Entity;
11495 --------------------------------
11496 -- Is_Limited_Class_Wide_Type --
11497 --------------------------------
11499 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
11500 begin
11501 return
11502 Is_Class_Wide_Type (Typ)
11503 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
11504 end Is_Limited_Class_Wide_Type;
11506 ---------------------------------
11507 -- Is_Local_Variable_Reference --
11508 ---------------------------------
11510 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
11511 begin
11512 if not Is_Entity_Name (Expr) then
11513 return False;
11515 else
11516 declare
11517 Ent : constant Entity_Id := Entity (Expr);
11518 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
11519 begin
11520 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
11521 return False;
11522 else
11523 return Present (Sub) and then Sub = Current_Subprogram;
11524 end if;
11525 end;
11526 end if;
11527 end Is_Local_Variable_Reference;
11529 -------------------------
11530 -- Is_Object_Reference --
11531 -------------------------
11533 function Is_Object_Reference (N : Node_Id) return Boolean is
11535 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
11536 -- Determine whether N is the name of an internally-generated renaming
11538 --------------------------------------
11539 -- Is_Internally_Generated_Renaming --
11540 --------------------------------------
11542 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
11543 P : Node_Id;
11545 begin
11546 P := N;
11547 while Present (P) loop
11548 if Nkind (P) = N_Object_Renaming_Declaration then
11549 return not Comes_From_Source (P);
11550 elsif Is_List_Member (P) then
11551 return False;
11552 end if;
11554 P := Parent (P);
11555 end loop;
11557 return False;
11558 end Is_Internally_Generated_Renaming;
11560 -- Start of processing for Is_Object_Reference
11562 begin
11563 if Is_Entity_Name (N) then
11564 return Present (Entity (N)) and then Is_Object (Entity (N));
11566 else
11567 case Nkind (N) is
11568 when N_Indexed_Component | N_Slice =>
11569 return
11570 Is_Object_Reference (Prefix (N))
11571 or else Is_Access_Type (Etype (Prefix (N)));
11573 -- In Ada 95, a function call is a constant object; a procedure
11574 -- call is not.
11576 when N_Function_Call =>
11577 return Etype (N) /= Standard_Void_Type;
11579 -- Attributes 'Input, 'Old and 'Result produce objects
11581 when N_Attribute_Reference =>
11582 return
11583 Nam_In
11584 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
11586 when N_Selected_Component =>
11587 return
11588 Is_Object_Reference (Selector_Name (N))
11589 and then
11590 (Is_Object_Reference (Prefix (N))
11591 or else Is_Access_Type (Etype (Prefix (N))));
11593 when N_Explicit_Dereference =>
11594 return True;
11596 -- A view conversion of a tagged object is an object reference
11598 when N_Type_Conversion =>
11599 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
11600 and then Is_Tagged_Type (Etype (Expression (N)))
11601 and then Is_Object_Reference (Expression (N));
11603 -- An unchecked type conversion is considered to be an object if
11604 -- the operand is an object (this construction arises only as a
11605 -- result of expansion activities).
11607 when N_Unchecked_Type_Conversion =>
11608 return True;
11610 -- Allow string literals to act as objects as long as they appear
11611 -- in internally-generated renamings. The expansion of iterators
11612 -- may generate such renamings when the range involves a string
11613 -- literal.
11615 when N_String_Literal =>
11616 return Is_Internally_Generated_Renaming (Parent (N));
11618 -- AI05-0003: In Ada 2012 a qualified expression is a name.
11619 -- This allows disambiguation of function calls and the use
11620 -- of aggregates in more contexts.
11622 when N_Qualified_Expression =>
11623 if Ada_Version < Ada_2012 then
11624 return False;
11625 else
11626 return Is_Object_Reference (Expression (N))
11627 or else Nkind (Expression (N)) = N_Aggregate;
11628 end if;
11630 when others =>
11631 return False;
11632 end case;
11633 end if;
11634 end Is_Object_Reference;
11636 -----------------------------------
11637 -- Is_OK_Variable_For_Out_Formal --
11638 -----------------------------------
11640 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
11641 begin
11642 Note_Possible_Modification (AV, Sure => True);
11644 -- We must reject parenthesized variable names. Comes_From_Source is
11645 -- checked because there are currently cases where the compiler violates
11646 -- this rule (e.g. passing a task object to its controlled Initialize
11647 -- routine). This should be properly documented in sinfo???
11649 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
11650 return False;
11652 -- A variable is always allowed
11654 elsif Is_Variable (AV) then
11655 return True;
11657 -- Generalized indexing operations are rewritten as explicit
11658 -- dereferences, and it is only during resolution that we can
11659 -- check whether the context requires an access_to_variable type.
11661 elsif Nkind (AV) = N_Explicit_Dereference
11662 and then Ada_Version >= Ada_2012
11663 and then Nkind (Original_Node (AV)) = N_Indexed_Component
11664 and then Present (Etype (Original_Node (AV)))
11665 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
11666 then
11667 return not Is_Access_Constant (Etype (Prefix (AV)));
11669 -- Unchecked conversions are allowed only if they come from the
11670 -- generated code, which sometimes uses unchecked conversions for out
11671 -- parameters in cases where code generation is unaffected. We tell
11672 -- source unchecked conversions by seeing if they are rewrites of
11673 -- an original Unchecked_Conversion function call, or of an explicit
11674 -- conversion of a function call or an aggregate (as may happen in the
11675 -- expansion of a packed array aggregate).
11677 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
11678 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
11679 return False;
11681 elsif Comes_From_Source (AV)
11682 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
11683 then
11684 return False;
11686 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
11687 return Is_OK_Variable_For_Out_Formal (Expression (AV));
11689 else
11690 return True;
11691 end if;
11693 -- Normal type conversions are allowed if argument is a variable
11695 elsif Nkind (AV) = N_Type_Conversion then
11696 if Is_Variable (Expression (AV))
11697 and then Paren_Count (Expression (AV)) = 0
11698 then
11699 Note_Possible_Modification (Expression (AV), Sure => True);
11700 return True;
11702 -- We also allow a non-parenthesized expression that raises
11703 -- constraint error if it rewrites what used to be a variable
11705 elsif Raises_Constraint_Error (Expression (AV))
11706 and then Paren_Count (Expression (AV)) = 0
11707 and then Is_Variable (Original_Node (Expression (AV)))
11708 then
11709 return True;
11711 -- Type conversion of something other than a variable
11713 else
11714 return False;
11715 end if;
11717 -- If this node is rewritten, then test the original form, if that is
11718 -- OK, then we consider the rewritten node OK (for example, if the
11719 -- original node is a conversion, then Is_Variable will not be true
11720 -- but we still want to allow the conversion if it converts a variable).
11722 elsif Original_Node (AV) /= AV then
11724 -- In Ada 2012, the explicit dereference may be a rewritten call to a
11725 -- Reference function.
11727 if Ada_Version >= Ada_2012
11728 and then Nkind (Original_Node (AV)) = N_Function_Call
11729 and then
11730 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
11731 then
11732 return True;
11734 else
11735 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
11736 end if;
11738 -- All other non-variables are rejected
11740 else
11741 return False;
11742 end if;
11743 end Is_OK_Variable_For_Out_Formal;
11745 -----------------------------------
11746 -- Is_Partially_Initialized_Type --
11747 -----------------------------------
11749 function Is_Partially_Initialized_Type
11750 (Typ : Entity_Id;
11751 Include_Implicit : Boolean := True) return Boolean
11753 begin
11754 if Is_Scalar_Type (Typ) then
11755 return False;
11757 elsif Is_Access_Type (Typ) then
11758 return Include_Implicit;
11760 elsif Is_Array_Type (Typ) then
11762 -- If component type is partially initialized, so is array type
11764 if Is_Partially_Initialized_Type
11765 (Component_Type (Typ), Include_Implicit)
11766 then
11767 return True;
11769 -- Otherwise we are only partially initialized if we are fully
11770 -- initialized (this is the empty array case, no point in us
11771 -- duplicating that code here).
11773 else
11774 return Is_Fully_Initialized_Type (Typ);
11775 end if;
11777 elsif Is_Record_Type (Typ) then
11779 -- A discriminated type is always partially initialized if in
11780 -- all mode
11782 if Has_Discriminants (Typ) and then Include_Implicit then
11783 return True;
11785 -- A tagged type is always partially initialized
11787 elsif Is_Tagged_Type (Typ) then
11788 return True;
11790 -- Case of non-discriminated record
11792 else
11793 declare
11794 Ent : Entity_Id;
11796 Component_Present : Boolean := False;
11797 -- Set True if at least one component is present. If no
11798 -- components are present, then record type is fully
11799 -- initialized (another odd case, like the null array).
11801 begin
11802 -- Loop through components
11804 Ent := First_Entity (Typ);
11805 while Present (Ent) loop
11806 if Ekind (Ent) = E_Component then
11807 Component_Present := True;
11809 -- If a component has an initialization expression then
11810 -- the enclosing record type is partially initialized
11812 if Present (Parent (Ent))
11813 and then Present (Expression (Parent (Ent)))
11814 then
11815 return True;
11817 -- If a component is of a type which is itself partially
11818 -- initialized, then the enclosing record type is also.
11820 elsif Is_Partially_Initialized_Type
11821 (Etype (Ent), Include_Implicit)
11822 then
11823 return True;
11824 end if;
11825 end if;
11827 Next_Entity (Ent);
11828 end loop;
11830 -- No initialized components found. If we found any components
11831 -- they were all uninitialized so the result is false.
11833 if Component_Present then
11834 return False;
11836 -- But if we found no components, then all the components are
11837 -- initialized so we consider the type to be initialized.
11839 else
11840 return True;
11841 end if;
11842 end;
11843 end if;
11845 -- Concurrent types are always fully initialized
11847 elsif Is_Concurrent_Type (Typ) then
11848 return True;
11850 -- For a private type, go to underlying type. If there is no underlying
11851 -- type then just assume this partially initialized. Not clear if this
11852 -- can happen in a non-error case, but no harm in testing for this.
11854 elsif Is_Private_Type (Typ) then
11855 declare
11856 U : constant Entity_Id := Underlying_Type (Typ);
11857 begin
11858 if No (U) then
11859 return True;
11860 else
11861 return Is_Partially_Initialized_Type (U, Include_Implicit);
11862 end if;
11863 end;
11865 -- For any other type (are there any?) assume partially initialized
11867 else
11868 return True;
11869 end if;
11870 end Is_Partially_Initialized_Type;
11872 ------------------------------------
11873 -- Is_Potentially_Persistent_Type --
11874 ------------------------------------
11876 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
11877 Comp : Entity_Id;
11878 Indx : Node_Id;
11880 begin
11881 -- For private type, test corresponding full type
11883 if Is_Private_Type (T) then
11884 return Is_Potentially_Persistent_Type (Full_View (T));
11886 -- Scalar types are potentially persistent
11888 elsif Is_Scalar_Type (T) then
11889 return True;
11891 -- Record type is potentially persistent if not tagged and the types of
11892 -- all it components are potentially persistent, and no component has
11893 -- an initialization expression.
11895 elsif Is_Record_Type (T)
11896 and then not Is_Tagged_Type (T)
11897 and then not Is_Partially_Initialized_Type (T)
11898 then
11899 Comp := First_Component (T);
11900 while Present (Comp) loop
11901 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
11902 return False;
11903 else
11904 Next_Entity (Comp);
11905 end if;
11906 end loop;
11908 return True;
11910 -- Array type is potentially persistent if its component type is
11911 -- potentially persistent and if all its constraints are static.
11913 elsif Is_Array_Type (T) then
11914 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
11915 return False;
11916 end if;
11918 Indx := First_Index (T);
11919 while Present (Indx) loop
11920 if not Is_OK_Static_Subtype (Etype (Indx)) then
11921 return False;
11922 else
11923 Next_Index (Indx);
11924 end if;
11925 end loop;
11927 return True;
11929 -- All other types are not potentially persistent
11931 else
11932 return False;
11933 end if;
11934 end Is_Potentially_Persistent_Type;
11936 --------------------------------
11937 -- Is_Potentially_Unevaluated --
11938 --------------------------------
11940 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
11941 Par : Node_Id;
11942 Expr : Node_Id;
11944 begin
11945 Expr := N;
11946 Par := Parent (N);
11948 -- A postcondition whose expression is a short-circuit is broken down
11949 -- into individual aspects for better exception reporting. The original
11950 -- short-circuit expression is rewritten as the second operand, and an
11951 -- occurrence of 'Old in that operand is potentially unevaluated.
11952 -- See Sem_ch13.adb for details of this transformation.
11954 if Nkind (Original_Node (Par)) = N_And_Then then
11955 return True;
11956 end if;
11958 while not Nkind_In (Par, N_If_Expression,
11959 N_Case_Expression,
11960 N_And_Then,
11961 N_Or_Else,
11962 N_In,
11963 N_Not_In)
11964 loop
11965 Expr := Par;
11966 Par := Parent (Par);
11968 -- If the context is not an expression, or if is the result of
11969 -- expansion of an enclosing construct (such as another attribute)
11970 -- the predicate does not apply.
11972 if Nkind (Par) not in N_Subexpr
11973 or else not Comes_From_Source (Par)
11974 then
11975 return False;
11976 end if;
11977 end loop;
11979 if Nkind (Par) = N_If_Expression then
11980 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
11982 elsif Nkind (Par) = N_Case_Expression then
11983 return Expr /= Expression (Par);
11985 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
11986 return Expr = Right_Opnd (Par);
11988 elsif Nkind_In (Par, N_In, N_Not_In) then
11989 return Expr /= Left_Opnd (Par);
11991 else
11992 return False;
11993 end if;
11994 end Is_Potentially_Unevaluated;
11996 ---------------------------------
11997 -- Is_Protected_Self_Reference --
11998 ---------------------------------
12000 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
12002 function In_Access_Definition (N : Node_Id) return Boolean;
12003 -- Returns true if N belongs to an access definition
12005 --------------------------
12006 -- In_Access_Definition --
12007 --------------------------
12009 function In_Access_Definition (N : Node_Id) return Boolean is
12010 P : Node_Id;
12012 begin
12013 P := Parent (N);
12014 while Present (P) loop
12015 if Nkind (P) = N_Access_Definition then
12016 return True;
12017 end if;
12019 P := Parent (P);
12020 end loop;
12022 return False;
12023 end In_Access_Definition;
12025 -- Start of processing for Is_Protected_Self_Reference
12027 begin
12028 -- Verify that prefix is analyzed and has the proper form. Note that
12029 -- the attributes Elab_Spec, Elab_Body, Elab_Subp_Body and UET_Address,
12030 -- which also produce the address of an entity, do not analyze their
12031 -- prefix because they denote entities that are not necessarily visible.
12032 -- Neither of them can apply to a protected type.
12034 return Ada_Version >= Ada_2005
12035 and then Is_Entity_Name (N)
12036 and then Present (Entity (N))
12037 and then Is_Protected_Type (Entity (N))
12038 and then In_Open_Scopes (Entity (N))
12039 and then not In_Access_Definition (N);
12040 end Is_Protected_Self_Reference;
12042 -----------------------------
12043 -- Is_RCI_Pkg_Spec_Or_Body --
12044 -----------------------------
12046 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
12048 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
12049 -- Return True if the unit of Cunit is an RCI package declaration
12051 ---------------------------
12052 -- Is_RCI_Pkg_Decl_Cunit --
12053 ---------------------------
12055 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
12056 The_Unit : constant Node_Id := Unit (Cunit);
12058 begin
12059 if Nkind (The_Unit) /= N_Package_Declaration then
12060 return False;
12061 end if;
12063 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
12064 end Is_RCI_Pkg_Decl_Cunit;
12066 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
12068 begin
12069 return Is_RCI_Pkg_Decl_Cunit (Cunit)
12070 or else
12071 (Nkind (Unit (Cunit)) = N_Package_Body
12072 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
12073 end Is_RCI_Pkg_Spec_Or_Body;
12075 -----------------------------------------
12076 -- Is_Remote_Access_To_Class_Wide_Type --
12077 -----------------------------------------
12079 function Is_Remote_Access_To_Class_Wide_Type
12080 (E : Entity_Id) return Boolean
12082 begin
12083 -- A remote access to class-wide type is a general access to object type
12084 -- declared in the visible part of a Remote_Types or Remote_Call_
12085 -- Interface unit.
12087 return Ekind (E) = E_General_Access_Type
12088 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12089 end Is_Remote_Access_To_Class_Wide_Type;
12091 -----------------------------------------
12092 -- Is_Remote_Access_To_Subprogram_Type --
12093 -----------------------------------------
12095 function Is_Remote_Access_To_Subprogram_Type
12096 (E : Entity_Id) return Boolean
12098 begin
12099 return (Ekind (E) = E_Access_Subprogram_Type
12100 or else (Ekind (E) = E_Record_Type
12101 and then Present (Corresponding_Remote_Type (E))))
12102 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12103 end Is_Remote_Access_To_Subprogram_Type;
12105 --------------------
12106 -- Is_Remote_Call --
12107 --------------------
12109 function Is_Remote_Call (N : Node_Id) return Boolean is
12110 begin
12111 if Nkind (N) not in N_Subprogram_Call then
12113 -- An entry call cannot be remote
12115 return False;
12117 elsif Nkind (Name (N)) in N_Has_Entity
12118 and then Is_Remote_Call_Interface (Entity (Name (N)))
12119 then
12120 -- A subprogram declared in the spec of a RCI package is remote
12122 return True;
12124 elsif Nkind (Name (N)) = N_Explicit_Dereference
12125 and then Is_Remote_Access_To_Subprogram_Type
12126 (Etype (Prefix (Name (N))))
12127 then
12128 -- The dereference of a RAS is a remote call
12130 return True;
12132 elsif Present (Controlling_Argument (N))
12133 and then Is_Remote_Access_To_Class_Wide_Type
12134 (Etype (Controlling_Argument (N)))
12135 then
12136 -- Any primitive operation call with a controlling argument of
12137 -- a RACW type is a remote call.
12139 return True;
12140 end if;
12142 -- All other calls are local calls
12144 return False;
12145 end Is_Remote_Call;
12147 ----------------------
12148 -- Is_Renamed_Entry --
12149 ----------------------
12151 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
12152 Orig_Node : Node_Id := Empty;
12153 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
12155 function Is_Entry (Nam : Node_Id) return Boolean;
12156 -- Determine whether Nam is an entry. Traverse selectors if there are
12157 -- nested selected components.
12159 --------------
12160 -- Is_Entry --
12161 --------------
12163 function Is_Entry (Nam : Node_Id) return Boolean is
12164 begin
12165 if Nkind (Nam) = N_Selected_Component then
12166 return Is_Entry (Selector_Name (Nam));
12167 end if;
12169 return Ekind (Entity (Nam)) = E_Entry;
12170 end Is_Entry;
12172 -- Start of processing for Is_Renamed_Entry
12174 begin
12175 if Present (Alias (Proc_Nam)) then
12176 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
12177 end if;
12179 -- Look for a rewritten subprogram renaming declaration
12181 if Nkind (Subp_Decl) = N_Subprogram_Declaration
12182 and then Present (Original_Node (Subp_Decl))
12183 then
12184 Orig_Node := Original_Node (Subp_Decl);
12185 end if;
12187 -- The rewritten subprogram is actually an entry
12189 if Present (Orig_Node)
12190 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
12191 and then Is_Entry (Name (Orig_Node))
12192 then
12193 return True;
12194 end if;
12196 return False;
12197 end Is_Renamed_Entry;
12199 ----------------------------
12200 -- Is_Reversible_Iterator --
12201 ----------------------------
12203 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
12204 Ifaces_List : Elist_Id;
12205 Iface_Elmt : Elmt_Id;
12206 Iface : Entity_Id;
12208 begin
12209 if Is_Class_Wide_Type (Typ)
12210 and then Chars (Etype (Typ)) = Name_Reversible_Iterator
12211 and then Is_Predefined_File_Name
12212 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
12213 then
12214 return True;
12216 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
12217 return False;
12219 else
12220 Collect_Interfaces (Typ, Ifaces_List);
12222 Iface_Elmt := First_Elmt (Ifaces_List);
12223 while Present (Iface_Elmt) loop
12224 Iface := Node (Iface_Elmt);
12225 if Chars (Iface) = Name_Reversible_Iterator
12226 and then
12227 Is_Predefined_File_Name
12228 (Unit_File_Name (Get_Source_Unit (Iface)))
12229 then
12230 return True;
12231 end if;
12233 Next_Elmt (Iface_Elmt);
12234 end loop;
12235 end if;
12237 return False;
12238 end Is_Reversible_Iterator;
12240 ----------------------
12241 -- Is_Selector_Name --
12242 ----------------------
12244 function Is_Selector_Name (N : Node_Id) return Boolean is
12245 begin
12246 if not Is_List_Member (N) then
12247 declare
12248 P : constant Node_Id := Parent (N);
12249 begin
12250 return Nkind_In (P, N_Expanded_Name,
12251 N_Generic_Association,
12252 N_Parameter_Association,
12253 N_Selected_Component)
12254 and then Selector_Name (P) = N;
12255 end;
12257 else
12258 declare
12259 L : constant List_Id := List_Containing (N);
12260 P : constant Node_Id := Parent (L);
12261 begin
12262 return (Nkind (P) = N_Discriminant_Association
12263 and then Selector_Names (P) = L)
12264 or else
12265 (Nkind (P) = N_Component_Association
12266 and then Choices (P) = L);
12267 end;
12268 end if;
12269 end Is_Selector_Name;
12271 -------------------------------------
12272 -- Is_SPARK_05_Initialization_Expr --
12273 -------------------------------------
12275 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
12276 Is_Ok : Boolean;
12277 Expr : Node_Id;
12278 Comp_Assn : Node_Id;
12279 Orig_N : constant Node_Id := Original_Node (N);
12281 begin
12282 Is_Ok := True;
12284 if not Comes_From_Source (Orig_N) then
12285 goto Done;
12286 end if;
12288 pragma Assert (Nkind (Orig_N) in N_Subexpr);
12290 case Nkind (Orig_N) is
12291 when N_Character_Literal |
12292 N_Integer_Literal |
12293 N_Real_Literal |
12294 N_String_Literal =>
12295 null;
12297 when N_Identifier |
12298 N_Expanded_Name =>
12299 if Is_Entity_Name (Orig_N)
12300 and then Present (Entity (Orig_N)) -- needed in some cases
12301 then
12302 case Ekind (Entity (Orig_N)) is
12303 when E_Constant |
12304 E_Enumeration_Literal |
12305 E_Named_Integer |
12306 E_Named_Real =>
12307 null;
12308 when others =>
12309 if Is_Type (Entity (Orig_N)) then
12310 null;
12311 else
12312 Is_Ok := False;
12313 end if;
12314 end case;
12315 end if;
12317 when N_Qualified_Expression |
12318 N_Type_Conversion =>
12319 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
12321 when N_Unary_Op =>
12322 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12324 when N_Binary_Op |
12325 N_Short_Circuit |
12326 N_Membership_Test =>
12327 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
12328 and then
12329 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12331 when N_Aggregate |
12332 N_Extension_Aggregate =>
12333 if Nkind (Orig_N) = N_Extension_Aggregate then
12334 Is_Ok :=
12335 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
12336 end if;
12338 Expr := First (Expressions (Orig_N));
12339 while Present (Expr) loop
12340 if not Is_SPARK_05_Initialization_Expr (Expr) then
12341 Is_Ok := False;
12342 goto Done;
12343 end if;
12345 Next (Expr);
12346 end loop;
12348 Comp_Assn := First (Component_Associations (Orig_N));
12349 while Present (Comp_Assn) loop
12350 Expr := Expression (Comp_Assn);
12352 -- Note: test for Present here needed for box assocation
12354 if Present (Expr)
12355 and then not Is_SPARK_05_Initialization_Expr (Expr)
12356 then
12357 Is_Ok := False;
12358 goto Done;
12359 end if;
12361 Next (Comp_Assn);
12362 end loop;
12364 when N_Attribute_Reference =>
12365 if Nkind (Prefix (Orig_N)) in N_Subexpr then
12366 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
12367 end if;
12369 Expr := First (Expressions (Orig_N));
12370 while Present (Expr) loop
12371 if not Is_SPARK_05_Initialization_Expr (Expr) then
12372 Is_Ok := False;
12373 goto Done;
12374 end if;
12376 Next (Expr);
12377 end loop;
12379 -- Selected components might be expanded named not yet resolved, so
12380 -- default on the safe side. (Eg on sparklex.ads)
12382 when N_Selected_Component =>
12383 null;
12385 when others =>
12386 Is_Ok := False;
12387 end case;
12389 <<Done>>
12390 return Is_Ok;
12391 end Is_SPARK_05_Initialization_Expr;
12393 ----------------------------------
12394 -- Is_SPARK_05_Object_Reference --
12395 ----------------------------------
12397 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
12398 begin
12399 if Is_Entity_Name (N) then
12400 return Present (Entity (N))
12401 and then
12402 (Ekind_In (Entity (N), E_Constant, E_Variable)
12403 or else Ekind (Entity (N)) in Formal_Kind);
12405 else
12406 case Nkind (N) is
12407 when N_Selected_Component =>
12408 return Is_SPARK_05_Object_Reference (Prefix (N));
12410 when others =>
12411 return False;
12412 end case;
12413 end if;
12414 end Is_SPARK_05_Object_Reference;
12416 -----------------------------
12417 -- Is_Specific_Tagged_Type --
12418 -----------------------------
12420 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
12421 Full_Typ : Entity_Id;
12423 begin
12424 -- Handle private types
12426 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
12427 Full_Typ := Full_View (Typ);
12428 else
12429 Full_Typ := Typ;
12430 end if;
12432 -- A specific tagged type is a non-class-wide tagged type
12434 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
12435 end Is_Specific_Tagged_Type;
12437 ------------------
12438 -- Is_Statement --
12439 ------------------
12441 function Is_Statement (N : Node_Id) return Boolean is
12442 begin
12443 return
12444 Nkind (N) in N_Statement_Other_Than_Procedure_Call
12445 or else Nkind (N) = N_Procedure_Call_Statement;
12446 end Is_Statement;
12448 --------------------------------------------------
12449 -- Is_Subprogram_Stub_Without_Prior_Declaration --
12450 --------------------------------------------------
12452 function Is_Subprogram_Stub_Without_Prior_Declaration
12453 (N : Node_Id) return Boolean
12455 begin
12456 -- A subprogram stub without prior declaration serves as declaration for
12457 -- the actual subprogram body. As such, it has an attached defining
12458 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
12460 return Nkind (N) = N_Subprogram_Body_Stub
12461 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
12462 end Is_Subprogram_Stub_Without_Prior_Declaration;
12464 ---------------------------------
12465 -- Is_Synchronized_Tagged_Type --
12466 ---------------------------------
12468 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
12469 Kind : constant Entity_Kind := Ekind (Base_Type (E));
12471 begin
12472 -- A task or protected type derived from an interface is a tagged type.
12473 -- Such a tagged type is called a synchronized tagged type, as are
12474 -- synchronized interfaces and private extensions whose declaration
12475 -- includes the reserved word synchronized.
12477 return (Is_Tagged_Type (E)
12478 and then (Kind = E_Task_Type
12479 or else
12480 Kind = E_Protected_Type))
12481 or else
12482 (Is_Interface (E)
12483 and then Is_Synchronized_Interface (E))
12484 or else
12485 (Ekind (E) = E_Record_Type_With_Private
12486 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
12487 and then (Synchronized_Present (Parent (E))
12488 or else Is_Synchronized_Interface (Etype (E))));
12489 end Is_Synchronized_Tagged_Type;
12491 -----------------
12492 -- Is_Transfer --
12493 -----------------
12495 function Is_Transfer (N : Node_Id) return Boolean is
12496 Kind : constant Node_Kind := Nkind (N);
12498 begin
12499 if Kind = N_Simple_Return_Statement
12500 or else
12501 Kind = N_Extended_Return_Statement
12502 or else
12503 Kind = N_Goto_Statement
12504 or else
12505 Kind = N_Raise_Statement
12506 or else
12507 Kind = N_Requeue_Statement
12508 then
12509 return True;
12511 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
12512 and then No (Condition (N))
12513 then
12514 return True;
12516 elsif Kind = N_Procedure_Call_Statement
12517 and then Is_Entity_Name (Name (N))
12518 and then Present (Entity (Name (N)))
12519 and then No_Return (Entity (Name (N)))
12520 then
12521 return True;
12523 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
12524 return True;
12526 else
12527 return False;
12528 end if;
12529 end Is_Transfer;
12531 -------------
12532 -- Is_True --
12533 -------------
12535 function Is_True (U : Uint) return Boolean is
12536 begin
12537 return (U /= 0);
12538 end Is_True;
12540 --------------------------------------
12541 -- Is_Unchecked_Conversion_Instance --
12542 --------------------------------------
12544 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
12545 Gen_Par : Entity_Id;
12547 begin
12548 -- Look for a function whose generic parent is the predefined intrinsic
12549 -- function Unchecked_Conversion.
12551 if Ekind (Id) = E_Function then
12552 Gen_Par := Generic_Parent (Parent (Id));
12554 return
12555 Present (Gen_Par)
12556 and then Chars (Gen_Par) = Name_Unchecked_Conversion
12557 and then Is_Intrinsic_Subprogram (Gen_Par)
12558 and then Is_Predefined_File_Name
12559 (Unit_File_Name (Get_Source_Unit (Gen_Par)));
12560 end if;
12562 return False;
12563 end Is_Unchecked_Conversion_Instance;
12565 -------------------------------
12566 -- Is_Universal_Numeric_Type --
12567 -------------------------------
12569 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
12570 begin
12571 return T = Universal_Integer or else T = Universal_Real;
12572 end Is_Universal_Numeric_Type;
12574 -------------------
12575 -- Is_Value_Type --
12576 -------------------
12578 function Is_Value_Type (T : Entity_Id) return Boolean is
12579 begin
12580 return VM_Target = CLI_Target
12581 and then Nkind (T) in N_Has_Chars
12582 and then Chars (T) /= No_Name
12583 and then Get_Name_String (Chars (T)) = "valuetype";
12584 end Is_Value_Type;
12586 ----------------------------
12587 -- Is_Variable_Size_Array --
12588 ----------------------------
12590 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
12591 Idx : Node_Id;
12593 begin
12594 pragma Assert (Is_Array_Type (E));
12596 -- Check if some index is initialized with a non-constant value
12598 Idx := First_Index (E);
12599 while Present (Idx) loop
12600 if Nkind (Idx) = N_Range then
12601 if not Is_Constant_Bound (Low_Bound (Idx))
12602 or else not Is_Constant_Bound (High_Bound (Idx))
12603 then
12604 return True;
12605 end if;
12606 end if;
12608 Idx := Next_Index (Idx);
12609 end loop;
12611 return False;
12612 end Is_Variable_Size_Array;
12614 -----------------------------
12615 -- Is_Variable_Size_Record --
12616 -----------------------------
12618 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
12619 Comp : Entity_Id;
12620 Comp_Typ : Entity_Id;
12622 begin
12623 pragma Assert (Is_Record_Type (E));
12625 Comp := First_Entity (E);
12626 while Present (Comp) loop
12627 Comp_Typ := Etype (Comp);
12629 -- Recursive call if the record type has discriminants
12631 if Is_Record_Type (Comp_Typ)
12632 and then Has_Discriminants (Comp_Typ)
12633 and then Is_Variable_Size_Record (Comp_Typ)
12634 then
12635 return True;
12637 elsif Is_Array_Type (Comp_Typ)
12638 and then Is_Variable_Size_Array (Comp_Typ)
12639 then
12640 return True;
12641 end if;
12643 Next_Entity (Comp);
12644 end loop;
12646 return False;
12647 end Is_Variable_Size_Record;
12649 -----------------
12650 -- Is_Variable --
12651 -----------------
12653 function Is_Variable
12654 (N : Node_Id;
12655 Use_Original_Node : Boolean := True) return Boolean
12657 Orig_Node : Node_Id;
12659 function In_Protected_Function (E : Entity_Id) return Boolean;
12660 -- Within a protected function, the private components of the enclosing
12661 -- protected type are constants. A function nested within a (protected)
12662 -- procedure is not itself protected. Within the body of a protected
12663 -- function the current instance of the protected type is a constant.
12665 function Is_Variable_Prefix (P : Node_Id) return Boolean;
12666 -- Prefixes can involve implicit dereferences, in which case we must
12667 -- test for the case of a reference of a constant access type, which can
12668 -- can never be a variable.
12670 ---------------------------
12671 -- In_Protected_Function --
12672 ---------------------------
12674 function In_Protected_Function (E : Entity_Id) return Boolean is
12675 Prot : Entity_Id;
12676 S : Entity_Id;
12678 begin
12679 -- E is the current instance of a type
12681 if Is_Type (E) then
12682 Prot := E;
12684 -- E is an object
12686 else
12687 Prot := Scope (E);
12688 end if;
12690 if not Is_Protected_Type (Prot) then
12691 return False;
12693 else
12694 S := Current_Scope;
12695 while Present (S) and then S /= Prot loop
12696 if Ekind (S) = E_Function and then Scope (S) = Prot then
12697 return True;
12698 end if;
12700 S := Scope (S);
12701 end loop;
12703 return False;
12704 end if;
12705 end In_Protected_Function;
12707 ------------------------
12708 -- Is_Variable_Prefix --
12709 ------------------------
12711 function Is_Variable_Prefix (P : Node_Id) return Boolean is
12712 begin
12713 if Is_Access_Type (Etype (P)) then
12714 return not Is_Access_Constant (Root_Type (Etype (P)));
12716 -- For the case of an indexed component whose prefix has a packed
12717 -- array type, the prefix has been rewritten into a type conversion.
12718 -- Determine variable-ness from the converted expression.
12720 elsif Nkind (P) = N_Type_Conversion
12721 and then not Comes_From_Source (P)
12722 and then Is_Array_Type (Etype (P))
12723 and then Is_Packed (Etype (P))
12724 then
12725 return Is_Variable (Expression (P));
12727 else
12728 return Is_Variable (P);
12729 end if;
12730 end Is_Variable_Prefix;
12732 -- Start of processing for Is_Variable
12734 begin
12735 -- Check if we perform the test on the original node since this may be a
12736 -- test of syntactic categories which must not be disturbed by whatever
12737 -- rewriting might have occurred. For example, an aggregate, which is
12738 -- certainly NOT a variable, could be turned into a variable by
12739 -- expansion.
12741 if Use_Original_Node then
12742 Orig_Node := Original_Node (N);
12743 else
12744 Orig_Node := N;
12745 end if;
12747 -- Definitely OK if Assignment_OK is set. Since this is something that
12748 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
12750 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
12751 return True;
12753 -- Normally we go to the original node, but there is one exception where
12754 -- we use the rewritten node, namely when it is an explicit dereference.
12755 -- The generated code may rewrite a prefix which is an access type with
12756 -- an explicit dereference. The dereference is a variable, even though
12757 -- the original node may not be (since it could be a constant of the
12758 -- access type).
12760 -- In Ada 2005 we have a further case to consider: the prefix may be a
12761 -- function call given in prefix notation. The original node appears to
12762 -- be a selected component, but we need to examine the call.
12764 elsif Nkind (N) = N_Explicit_Dereference
12765 and then Nkind (Orig_Node) /= N_Explicit_Dereference
12766 and then Present (Etype (Orig_Node))
12767 and then Is_Access_Type (Etype (Orig_Node))
12768 then
12769 -- Note that if the prefix is an explicit dereference that does not
12770 -- come from source, we must check for a rewritten function call in
12771 -- prefixed notation before other forms of rewriting, to prevent a
12772 -- compiler crash.
12774 return
12775 (Nkind (Orig_Node) = N_Function_Call
12776 and then not Is_Access_Constant (Etype (Prefix (N))))
12777 or else
12778 Is_Variable_Prefix (Original_Node (Prefix (N)));
12780 -- in Ada 2012, the dereference may have been added for a type with
12781 -- a declared implicit dereference aspect. Check that it is not an
12782 -- access to constant.
12784 elsif Nkind (N) = N_Explicit_Dereference
12785 and then Present (Etype (Orig_Node))
12786 and then Ada_Version >= Ada_2012
12787 and then Has_Implicit_Dereference (Etype (Orig_Node))
12788 then
12789 return not Is_Access_Constant (Etype (Prefix (N)));
12791 -- A function call is never a variable
12793 elsif Nkind (N) = N_Function_Call then
12794 return False;
12796 -- All remaining checks use the original node
12798 elsif Is_Entity_Name (Orig_Node)
12799 and then Present (Entity (Orig_Node))
12800 then
12801 declare
12802 E : constant Entity_Id := Entity (Orig_Node);
12803 K : constant Entity_Kind := Ekind (E);
12805 begin
12806 return (K = E_Variable
12807 and then Nkind (Parent (E)) /= N_Exception_Handler)
12808 or else (K = E_Component
12809 and then not In_Protected_Function (E))
12810 or else K = E_Out_Parameter
12811 or else K = E_In_Out_Parameter
12812 or else K = E_Generic_In_Out_Parameter
12814 -- Current instance of type. If this is a protected type, check
12815 -- we are not within the body of one of its protected functions.
12817 or else (Is_Type (E)
12818 and then In_Open_Scopes (E)
12819 and then not In_Protected_Function (E))
12821 or else (Is_Incomplete_Or_Private_Type (E)
12822 and then In_Open_Scopes (Full_View (E)));
12823 end;
12825 else
12826 case Nkind (Orig_Node) is
12827 when N_Indexed_Component | N_Slice =>
12828 return Is_Variable_Prefix (Prefix (Orig_Node));
12830 when N_Selected_Component =>
12831 return (Is_Variable (Selector_Name (Orig_Node))
12832 and then Is_Variable_Prefix (Prefix (Orig_Node)))
12833 or else
12834 (Nkind (N) = N_Expanded_Name
12835 and then Scope (Entity (N)) = Entity (Prefix (N)));
12837 -- For an explicit dereference, the type of the prefix cannot
12838 -- be an access to constant or an access to subprogram.
12840 when N_Explicit_Dereference =>
12841 declare
12842 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
12843 begin
12844 return Is_Access_Type (Typ)
12845 and then not Is_Access_Constant (Root_Type (Typ))
12846 and then Ekind (Typ) /= E_Access_Subprogram_Type;
12847 end;
12849 -- The type conversion is the case where we do not deal with the
12850 -- context dependent special case of an actual parameter. Thus
12851 -- the type conversion is only considered a variable for the
12852 -- purposes of this routine if the target type is tagged. However,
12853 -- a type conversion is considered to be a variable if it does not
12854 -- come from source (this deals for example with the conversions
12855 -- of expressions to their actual subtypes).
12857 when N_Type_Conversion =>
12858 return Is_Variable (Expression (Orig_Node))
12859 and then
12860 (not Comes_From_Source (Orig_Node)
12861 or else
12862 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
12863 and then
12864 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
12866 -- GNAT allows an unchecked type conversion as a variable. This
12867 -- only affects the generation of internal expanded code, since
12868 -- calls to instantiations of Unchecked_Conversion are never
12869 -- considered variables (since they are function calls).
12871 when N_Unchecked_Type_Conversion =>
12872 return Is_Variable (Expression (Orig_Node));
12874 when others =>
12875 return False;
12876 end case;
12877 end if;
12878 end Is_Variable;
12880 ---------------------------
12881 -- Is_Visibly_Controlled --
12882 ---------------------------
12884 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
12885 Root : constant Entity_Id := Root_Type (T);
12886 begin
12887 return Chars (Scope (Root)) = Name_Finalization
12888 and then Chars (Scope (Scope (Root))) = Name_Ada
12889 and then Scope (Scope (Scope (Root))) = Standard_Standard;
12890 end Is_Visibly_Controlled;
12892 ------------------------
12893 -- Is_Volatile_Object --
12894 ------------------------
12896 function Is_Volatile_Object (N : Node_Id) return Boolean is
12898 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
12899 -- If prefix is an implicit dereference, examine designated type
12901 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
12902 -- Determines if given object has volatile components
12904 ------------------------
12905 -- Is_Volatile_Prefix --
12906 ------------------------
12908 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
12909 Typ : constant Entity_Id := Etype (N);
12911 begin
12912 if Is_Access_Type (Typ) then
12913 declare
12914 Dtyp : constant Entity_Id := Designated_Type (Typ);
12916 begin
12917 return Is_Volatile (Dtyp)
12918 or else Has_Volatile_Components (Dtyp);
12919 end;
12921 else
12922 return Object_Has_Volatile_Components (N);
12923 end if;
12924 end Is_Volatile_Prefix;
12926 ------------------------------------
12927 -- Object_Has_Volatile_Components --
12928 ------------------------------------
12930 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
12931 Typ : constant Entity_Id := Etype (N);
12933 begin
12934 if Is_Volatile (Typ)
12935 or else Has_Volatile_Components (Typ)
12936 then
12937 return True;
12939 elsif Is_Entity_Name (N)
12940 and then (Has_Volatile_Components (Entity (N))
12941 or else Is_Volatile (Entity (N)))
12942 then
12943 return True;
12945 elsif Nkind (N) = N_Indexed_Component
12946 or else Nkind (N) = N_Selected_Component
12947 then
12948 return Is_Volatile_Prefix (Prefix (N));
12950 else
12951 return False;
12952 end if;
12953 end Object_Has_Volatile_Components;
12955 -- Start of processing for Is_Volatile_Object
12957 begin
12958 if Nkind (N) = N_Defining_Identifier then
12959 return Is_Volatile (N) or else Is_Volatile (Etype (N));
12961 elsif Nkind (N) = N_Expanded_Name then
12962 return Is_Volatile_Object (Entity (N));
12964 elsif Is_Volatile (Etype (N))
12965 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
12966 then
12967 return True;
12969 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
12970 and then Is_Volatile_Prefix (Prefix (N))
12971 then
12972 return True;
12974 elsif Nkind (N) = N_Selected_Component
12975 and then Is_Volatile (Entity (Selector_Name (N)))
12976 then
12977 return True;
12979 else
12980 return False;
12981 end if;
12982 end Is_Volatile_Object;
12984 ---------------------------
12985 -- Itype_Has_Declaration --
12986 ---------------------------
12988 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
12989 begin
12990 pragma Assert (Is_Itype (Id));
12991 return Present (Parent (Id))
12992 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
12993 N_Subtype_Declaration)
12994 and then Defining_Entity (Parent (Id)) = Id;
12995 end Itype_Has_Declaration;
12997 -------------------------
12998 -- Kill_Current_Values --
12999 -------------------------
13001 procedure Kill_Current_Values
13002 (Ent : Entity_Id;
13003 Last_Assignment_Only : Boolean := False)
13005 begin
13006 if Is_Assignable (Ent) then
13007 Set_Last_Assignment (Ent, Empty);
13008 end if;
13010 if Is_Object (Ent) then
13011 if not Last_Assignment_Only then
13012 Kill_Checks (Ent);
13013 Set_Current_Value (Ent, Empty);
13015 if not Can_Never_Be_Null (Ent) then
13016 Set_Is_Known_Non_Null (Ent, False);
13017 end if;
13019 Set_Is_Known_Null (Ent, False);
13021 -- Reset Is_Known_Valid unless type is always valid, or if we have
13022 -- a loop parameter (loop parameters are always valid, since their
13023 -- bounds are defined by the bounds given in the loop header).
13025 if not Is_Known_Valid (Etype (Ent))
13026 and then Ekind (Ent) /= E_Loop_Parameter
13027 then
13028 Set_Is_Known_Valid (Ent, False);
13029 end if;
13030 end if;
13031 end if;
13032 end Kill_Current_Values;
13034 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
13035 S : Entity_Id;
13037 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
13038 -- Clear current value for entity E and all entities chained to E
13040 ------------------------------------------
13041 -- Kill_Current_Values_For_Entity_Chain --
13042 ------------------------------------------
13044 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
13045 Ent : Entity_Id;
13046 begin
13047 Ent := E;
13048 while Present (Ent) loop
13049 Kill_Current_Values (Ent, Last_Assignment_Only);
13050 Next_Entity (Ent);
13051 end loop;
13052 end Kill_Current_Values_For_Entity_Chain;
13054 -- Start of processing for Kill_Current_Values
13056 begin
13057 -- Kill all saved checks, a special case of killing saved values
13059 if not Last_Assignment_Only then
13060 Kill_All_Checks;
13061 end if;
13063 -- Loop through relevant scopes, which includes the current scope and
13064 -- any parent scopes if the current scope is a block or a package.
13066 S := Current_Scope;
13067 Scope_Loop : loop
13069 -- Clear current values of all entities in current scope
13071 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
13073 -- If scope is a package, also clear current values of all private
13074 -- entities in the scope.
13076 if Is_Package_Or_Generic_Package (S)
13077 or else Is_Concurrent_Type (S)
13078 then
13079 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
13080 end if;
13082 -- If this is a not a subprogram, deal with parents
13084 if not Is_Subprogram (S) then
13085 S := Scope (S);
13086 exit Scope_Loop when S = Standard_Standard;
13087 else
13088 exit Scope_Loop;
13089 end if;
13090 end loop Scope_Loop;
13091 end Kill_Current_Values;
13093 --------------------------
13094 -- Kill_Size_Check_Code --
13095 --------------------------
13097 procedure Kill_Size_Check_Code (E : Entity_Id) is
13098 begin
13099 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
13100 and then Present (Size_Check_Code (E))
13101 then
13102 Remove (Size_Check_Code (E));
13103 Set_Size_Check_Code (E, Empty);
13104 end if;
13105 end Kill_Size_Check_Code;
13107 --------------------------
13108 -- Known_To_Be_Assigned --
13109 --------------------------
13111 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
13112 P : constant Node_Id := Parent (N);
13114 begin
13115 case Nkind (P) is
13117 -- Test left side of assignment
13119 when N_Assignment_Statement =>
13120 return N = Name (P);
13122 -- Function call arguments are never lvalues
13124 when N_Function_Call =>
13125 return False;
13127 -- Positional parameter for procedure or accept call
13129 when N_Procedure_Call_Statement |
13130 N_Accept_Statement
13132 declare
13133 Proc : Entity_Id;
13134 Form : Entity_Id;
13135 Act : Node_Id;
13137 begin
13138 Proc := Get_Subprogram_Entity (P);
13140 if No (Proc) then
13141 return False;
13142 end if;
13144 -- If we are not a list member, something is strange, so
13145 -- be conservative and return False.
13147 if not Is_List_Member (N) then
13148 return False;
13149 end if;
13151 -- We are going to find the right formal by stepping forward
13152 -- through the formals, as we step backwards in the actuals.
13154 Form := First_Formal (Proc);
13155 Act := N;
13156 loop
13157 -- If no formal, something is weird, so be conservative
13158 -- and return False.
13160 if No (Form) then
13161 return False;
13162 end if;
13164 Prev (Act);
13165 exit when No (Act);
13166 Next_Formal (Form);
13167 end loop;
13169 return Ekind (Form) /= E_In_Parameter;
13170 end;
13172 -- Named parameter for procedure or accept call
13174 when N_Parameter_Association =>
13175 declare
13176 Proc : Entity_Id;
13177 Form : Entity_Id;
13179 begin
13180 Proc := Get_Subprogram_Entity (Parent (P));
13182 if No (Proc) then
13183 return False;
13184 end if;
13186 -- Loop through formals to find the one that matches
13188 Form := First_Formal (Proc);
13189 loop
13190 -- If no matching formal, that's peculiar, some kind of
13191 -- previous error, so return False to be conservative.
13192 -- Actually this also happens in legal code in the case
13193 -- where P is a parameter association for an Extra_Formal???
13195 if No (Form) then
13196 return False;
13197 end if;
13199 -- Else test for match
13201 if Chars (Form) = Chars (Selector_Name (P)) then
13202 return Ekind (Form) /= E_In_Parameter;
13203 end if;
13205 Next_Formal (Form);
13206 end loop;
13207 end;
13209 -- Test for appearing in a conversion that itself appears
13210 -- in an lvalue context, since this should be an lvalue.
13212 when N_Type_Conversion =>
13213 return Known_To_Be_Assigned (P);
13215 -- All other references are definitely not known to be modifications
13217 when others =>
13218 return False;
13220 end case;
13221 end Known_To_Be_Assigned;
13223 ---------------------------
13224 -- Last_Source_Statement --
13225 ---------------------------
13227 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
13228 N : Node_Id;
13230 begin
13231 N := Last (Statements (HSS));
13232 while Present (N) loop
13233 exit when Comes_From_Source (N);
13234 Prev (N);
13235 end loop;
13237 return N;
13238 end Last_Source_Statement;
13240 ----------------------------------
13241 -- Matching_Static_Array_Bounds --
13242 ----------------------------------
13244 function Matching_Static_Array_Bounds
13245 (L_Typ : Node_Id;
13246 R_Typ : Node_Id) return Boolean
13248 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
13249 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
13251 L_Index : Node_Id;
13252 R_Index : Node_Id;
13253 L_Low : Node_Id;
13254 L_High : Node_Id;
13255 L_Len : Uint;
13256 R_Low : Node_Id;
13257 R_High : Node_Id;
13258 R_Len : Uint;
13260 begin
13261 if L_Ndims /= R_Ndims then
13262 return False;
13263 end if;
13265 -- Unconstrained types do not have static bounds
13267 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
13268 return False;
13269 end if;
13271 -- First treat specially the first dimension, as the lower bound and
13272 -- length of string literals are not stored like those of arrays.
13274 if Ekind (L_Typ) = E_String_Literal_Subtype then
13275 L_Low := String_Literal_Low_Bound (L_Typ);
13276 L_Len := String_Literal_Length (L_Typ);
13277 else
13278 L_Index := First_Index (L_Typ);
13279 Get_Index_Bounds (L_Index, L_Low, L_High);
13281 if Is_OK_Static_Expression (L_Low)
13282 and then
13283 Is_OK_Static_Expression (L_High)
13284 then
13285 if Expr_Value (L_High) < Expr_Value (L_Low) then
13286 L_Len := Uint_0;
13287 else
13288 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
13289 end if;
13290 else
13291 return False;
13292 end if;
13293 end if;
13295 if Ekind (R_Typ) = E_String_Literal_Subtype then
13296 R_Low := String_Literal_Low_Bound (R_Typ);
13297 R_Len := String_Literal_Length (R_Typ);
13298 else
13299 R_Index := First_Index (R_Typ);
13300 Get_Index_Bounds (R_Index, R_Low, R_High);
13302 if Is_OK_Static_Expression (R_Low)
13303 and then
13304 Is_OK_Static_Expression (R_High)
13305 then
13306 if Expr_Value (R_High) < Expr_Value (R_Low) then
13307 R_Len := Uint_0;
13308 else
13309 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
13310 end if;
13311 else
13312 return False;
13313 end if;
13314 end if;
13316 if (Is_OK_Static_Expression (L_Low)
13317 and then
13318 Is_OK_Static_Expression (R_Low))
13319 and then Expr_Value (L_Low) = Expr_Value (R_Low)
13320 and then L_Len = R_Len
13321 then
13322 null;
13323 else
13324 return False;
13325 end if;
13327 -- Then treat all other dimensions
13329 for Indx in 2 .. L_Ndims loop
13330 Next (L_Index);
13331 Next (R_Index);
13333 Get_Index_Bounds (L_Index, L_Low, L_High);
13334 Get_Index_Bounds (R_Index, R_Low, R_High);
13336 if (Is_OK_Static_Expression (L_Low) and then
13337 Is_OK_Static_Expression (L_High) and then
13338 Is_OK_Static_Expression (R_Low) and then
13339 Is_OK_Static_Expression (R_High))
13340 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
13341 and then
13342 Expr_Value (L_High) = Expr_Value (R_High))
13343 then
13344 null;
13345 else
13346 return False;
13347 end if;
13348 end loop;
13350 -- If we fall through the loop, all indexes matched
13352 return True;
13353 end Matching_Static_Array_Bounds;
13355 -------------------
13356 -- May_Be_Lvalue --
13357 -------------------
13359 function May_Be_Lvalue (N : Node_Id) return Boolean is
13360 P : constant Node_Id := Parent (N);
13362 begin
13363 case Nkind (P) is
13365 -- Test left side of assignment
13367 when N_Assignment_Statement =>
13368 return N = Name (P);
13370 -- Test prefix of component or attribute. Note that the prefix of an
13371 -- explicit or implicit dereference cannot be an l-value.
13373 when N_Attribute_Reference =>
13374 return N = Prefix (P)
13375 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
13377 -- For an expanded name, the name is an lvalue if the expanded name
13378 -- is an lvalue, but the prefix is never an lvalue, since it is just
13379 -- the scope where the name is found.
13381 when N_Expanded_Name =>
13382 if N = Prefix (P) then
13383 return May_Be_Lvalue (P);
13384 else
13385 return False;
13386 end if;
13388 -- For a selected component A.B, A is certainly an lvalue if A.B is.
13389 -- B is a little interesting, if we have A.B := 3, there is some
13390 -- discussion as to whether B is an lvalue or not, we choose to say
13391 -- it is. Note however that A is not an lvalue if it is of an access
13392 -- type since this is an implicit dereference.
13394 when N_Selected_Component =>
13395 if N = Prefix (P)
13396 and then Present (Etype (N))
13397 and then Is_Access_Type (Etype (N))
13398 then
13399 return False;
13400 else
13401 return May_Be_Lvalue (P);
13402 end if;
13404 -- For an indexed component or slice, the index or slice bounds is
13405 -- never an lvalue. The prefix is an lvalue if the indexed component
13406 -- or slice is an lvalue, except if it is an access type, where we
13407 -- have an implicit dereference.
13409 when N_Indexed_Component | N_Slice =>
13410 if N /= Prefix (P)
13411 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
13412 then
13413 return False;
13414 else
13415 return May_Be_Lvalue (P);
13416 end if;
13418 -- Prefix of a reference is an lvalue if the reference is an lvalue
13420 when N_Reference =>
13421 return May_Be_Lvalue (P);
13423 -- Prefix of explicit dereference is never an lvalue
13425 when N_Explicit_Dereference =>
13426 return False;
13428 -- Positional parameter for subprogram, entry, or accept call.
13429 -- In older versions of Ada function call arguments are never
13430 -- lvalues. In Ada 2012 functions can have in-out parameters.
13432 when N_Subprogram_Call |
13433 N_Entry_Call_Statement |
13434 N_Accept_Statement
13436 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
13437 return False;
13438 end if;
13440 -- The following mechanism is clumsy and fragile. A single flag
13441 -- set in Resolve_Actuals would be preferable ???
13443 declare
13444 Proc : Entity_Id;
13445 Form : Entity_Id;
13446 Act : Node_Id;
13448 begin
13449 Proc := Get_Subprogram_Entity (P);
13451 if No (Proc) then
13452 return True;
13453 end if;
13455 -- If we are not a list member, something is strange, so be
13456 -- conservative and return True.
13458 if not Is_List_Member (N) then
13459 return True;
13460 end if;
13462 -- We are going to find the right formal by stepping forward
13463 -- through the formals, as we step backwards in the actuals.
13465 Form := First_Formal (Proc);
13466 Act := N;
13467 loop
13468 -- If no formal, something is weird, so be conservative and
13469 -- return True.
13471 if No (Form) then
13472 return True;
13473 end if;
13475 Prev (Act);
13476 exit when No (Act);
13477 Next_Formal (Form);
13478 end loop;
13480 return Ekind (Form) /= E_In_Parameter;
13481 end;
13483 -- Named parameter for procedure or accept call
13485 when N_Parameter_Association =>
13486 declare
13487 Proc : Entity_Id;
13488 Form : Entity_Id;
13490 begin
13491 Proc := Get_Subprogram_Entity (Parent (P));
13493 if No (Proc) then
13494 return True;
13495 end if;
13497 -- Loop through formals to find the one that matches
13499 Form := First_Formal (Proc);
13500 loop
13501 -- If no matching formal, that's peculiar, some kind of
13502 -- previous error, so return True to be conservative.
13503 -- Actually happens with legal code for an unresolved call
13504 -- where we may get the wrong homonym???
13506 if No (Form) then
13507 return True;
13508 end if;
13510 -- Else test for match
13512 if Chars (Form) = Chars (Selector_Name (P)) then
13513 return Ekind (Form) /= E_In_Parameter;
13514 end if;
13516 Next_Formal (Form);
13517 end loop;
13518 end;
13520 -- Test for appearing in a conversion that itself appears in an
13521 -- lvalue context, since this should be an lvalue.
13523 when N_Type_Conversion =>
13524 return May_Be_Lvalue (P);
13526 -- Test for appearance in object renaming declaration
13528 when N_Object_Renaming_Declaration =>
13529 return True;
13531 -- All other references are definitely not lvalues
13533 when others =>
13534 return False;
13536 end case;
13537 end May_Be_Lvalue;
13539 -----------------------
13540 -- Mark_Coextensions --
13541 -----------------------
13543 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
13544 Is_Dynamic : Boolean;
13545 -- Indicates whether the context causes nested coextensions to be
13546 -- dynamic or static
13548 function Mark_Allocator (N : Node_Id) return Traverse_Result;
13549 -- Recognize an allocator node and label it as a dynamic coextension
13551 --------------------
13552 -- Mark_Allocator --
13553 --------------------
13555 function Mark_Allocator (N : Node_Id) return Traverse_Result is
13556 begin
13557 if Nkind (N) = N_Allocator then
13558 if Is_Dynamic then
13559 Set_Is_Dynamic_Coextension (N);
13561 -- If the allocator expression is potentially dynamic, it may
13562 -- be expanded out of order and require dynamic allocation
13563 -- anyway, so we treat the coextension itself as dynamic.
13564 -- Potential optimization ???
13566 elsif Nkind (Expression (N)) = N_Qualified_Expression
13567 and then Nkind (Expression (Expression (N))) = N_Op_Concat
13568 then
13569 Set_Is_Dynamic_Coextension (N);
13570 else
13571 Set_Is_Static_Coextension (N);
13572 end if;
13573 end if;
13575 return OK;
13576 end Mark_Allocator;
13578 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
13580 -- Start of processing Mark_Coextensions
13582 begin
13583 case Nkind (Context_Nod) is
13585 -- Comment here ???
13587 when N_Assignment_Statement =>
13588 Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
13590 -- An allocator that is a component of a returned aggregate
13591 -- must be dynamic.
13593 when N_Simple_Return_Statement =>
13594 declare
13595 Expr : constant Node_Id := Expression (Context_Nod);
13596 begin
13597 Is_Dynamic :=
13598 Nkind (Expr) = N_Allocator
13599 or else
13600 (Nkind (Expr) = N_Qualified_Expression
13601 and then Nkind (Expression (Expr)) = N_Aggregate);
13602 end;
13604 -- An alloctor within an object declaration in an extended return
13605 -- statement is of necessity dynamic.
13607 when N_Object_Declaration =>
13608 Is_Dynamic := Nkind (Root_Nod) = N_Allocator
13609 or else
13610 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
13612 -- This routine should not be called for constructs which may not
13613 -- contain coextensions.
13615 when others =>
13616 raise Program_Error;
13617 end case;
13619 Mark_Allocators (Root_Nod);
13620 end Mark_Coextensions;
13622 ----------------------
13623 -- Needs_One_Actual --
13624 ----------------------
13626 function Needs_One_Actual (E : Entity_Id) return Boolean is
13627 Formal : Entity_Id;
13629 begin
13630 -- Ada 2005 or later, and formals present
13632 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
13633 Formal := Next_Formal (First_Formal (E));
13634 while Present (Formal) loop
13635 if No (Default_Value (Formal)) then
13636 return False;
13637 end if;
13639 Next_Formal (Formal);
13640 end loop;
13642 return True;
13644 -- Ada 83/95 or no formals
13646 else
13647 return False;
13648 end if;
13649 end Needs_One_Actual;
13651 ------------------------
13652 -- New_Copy_List_Tree --
13653 ------------------------
13655 function New_Copy_List_Tree (List : List_Id) return List_Id is
13656 NL : List_Id;
13657 E : Node_Id;
13659 begin
13660 if List = No_List then
13661 return No_List;
13663 else
13664 NL := New_List;
13665 E := First (List);
13667 while Present (E) loop
13668 Append (New_Copy_Tree (E), NL);
13669 E := Next (E);
13670 end loop;
13672 return NL;
13673 end if;
13674 end New_Copy_List_Tree;
13676 --------------------------------------------------
13677 -- New_Copy_Tree Auxiliary Data and Subprograms --
13678 --------------------------------------------------
13680 use Atree.Unchecked_Access;
13681 use Atree_Private_Part;
13683 -- Our approach here requires a two pass traversal of the tree. The
13684 -- first pass visits all nodes that eventually will be copied looking
13685 -- for defining Itypes. If any defining Itypes are found, then they are
13686 -- copied, and an entry is added to the replacement map. In the second
13687 -- phase, the tree is copied, using the replacement map to replace any
13688 -- Itype references within the copied tree.
13690 -- The following hash tables are used if the Map supplied has more
13691 -- than hash threshold entries to speed up access to the map. If
13692 -- there are fewer entries, then the map is searched sequentially
13693 -- (because setting up a hash table for only a few entries takes
13694 -- more time than it saves.
13696 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
13697 -- Hash function used for hash operations
13699 -------------------
13700 -- New_Copy_Hash --
13701 -------------------
13703 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
13704 begin
13705 return Nat (E) mod (NCT_Header_Num'Last + 1);
13706 end New_Copy_Hash;
13708 ---------------
13709 -- NCT_Assoc --
13710 ---------------
13712 -- The hash table NCT_Assoc associates old entities in the table
13713 -- with their corresponding new entities (i.e. the pairs of entries
13714 -- presented in the original Map argument are Key-Element pairs).
13716 package NCT_Assoc is new Simple_HTable (
13717 Header_Num => NCT_Header_Num,
13718 Element => Entity_Id,
13719 No_Element => Empty,
13720 Key => Entity_Id,
13721 Hash => New_Copy_Hash,
13722 Equal => Types."=");
13724 ---------------------
13725 -- NCT_Itype_Assoc --
13726 ---------------------
13728 -- The hash table NCT_Itype_Assoc contains entries only for those
13729 -- old nodes which have a non-empty Associated_Node_For_Itype set.
13730 -- The key is the associated node, and the element is the new node
13731 -- itself (NOT the associated node for the new node).
13733 package NCT_Itype_Assoc is new Simple_HTable (
13734 Header_Num => NCT_Header_Num,
13735 Element => Entity_Id,
13736 No_Element => Empty,
13737 Key => Entity_Id,
13738 Hash => New_Copy_Hash,
13739 Equal => Types."=");
13741 -------------------
13742 -- New_Copy_Tree --
13743 -------------------
13745 function New_Copy_Tree
13746 (Source : Node_Id;
13747 Map : Elist_Id := No_Elist;
13748 New_Sloc : Source_Ptr := No_Location;
13749 New_Scope : Entity_Id := Empty) return Node_Id
13751 Actual_Map : Elist_Id := Map;
13752 -- This is the actual map for the copy. It is initialized with the
13753 -- given elements, and then enlarged as required for Itypes that are
13754 -- copied during the first phase of the copy operation. The visit
13755 -- procedures add elements to this map as Itypes are encountered.
13756 -- The reason we cannot use Map directly, is that it may well be
13757 -- (and normally is) initialized to No_Elist, and if we have mapped
13758 -- entities, we have to reset it to point to a real Elist.
13760 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
13761 -- Called during second phase to map entities into their corresponding
13762 -- copies using Actual_Map. If the argument is not an entity, or is not
13763 -- in Actual_Map, then it is returned unchanged.
13765 procedure Build_NCT_Hash_Tables;
13766 -- Builds hash tables (number of elements >= threshold value)
13768 function Copy_Elist_With_Replacement
13769 (Old_Elist : Elist_Id) return Elist_Id;
13770 -- Called during second phase to copy element list doing replacements
13772 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
13773 -- Called during the second phase to process a copied Itype. The actual
13774 -- copy happened during the first phase (so that we could make the entry
13775 -- in the mapping), but we still have to deal with the descendents of
13776 -- the copied Itype and copy them where necessary.
13778 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
13779 -- Called during second phase to copy list doing replacements
13781 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
13782 -- Called during second phase to copy node doing replacements
13784 procedure Visit_Elist (E : Elist_Id);
13785 -- Called during first phase to visit all elements of an Elist
13787 procedure Visit_Field (F : Union_Id; N : Node_Id);
13788 -- Visit a single field, recursing to call Visit_Node or Visit_List
13789 -- if the field is a syntactic descendent of the current node (i.e.
13790 -- its parent is Node N).
13792 procedure Visit_Itype (Old_Itype : Entity_Id);
13793 -- Called during first phase to visit subsidiary fields of a defining
13794 -- Itype, and also create a copy and make an entry in the replacement
13795 -- map for the new copy.
13797 procedure Visit_List (L : List_Id);
13798 -- Called during first phase to visit all elements of a List
13800 procedure Visit_Node (N : Node_Or_Entity_Id);
13801 -- Called during first phase to visit a node and all its subtrees
13803 -----------
13804 -- Assoc --
13805 -----------
13807 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
13808 E : Elmt_Id;
13809 Ent : Entity_Id;
13811 begin
13812 if not Has_Extension (N) or else No (Actual_Map) then
13813 return N;
13815 elsif NCT_Hash_Tables_Used then
13816 Ent := NCT_Assoc.Get (Entity_Id (N));
13818 if Present (Ent) then
13819 return Ent;
13820 else
13821 return N;
13822 end if;
13824 -- No hash table used, do serial search
13826 else
13827 E := First_Elmt (Actual_Map);
13828 while Present (E) loop
13829 if Node (E) = N then
13830 return Node (Next_Elmt (E));
13831 else
13832 E := Next_Elmt (Next_Elmt (E));
13833 end if;
13834 end loop;
13835 end if;
13837 return N;
13838 end Assoc;
13840 ---------------------------
13841 -- Build_NCT_Hash_Tables --
13842 ---------------------------
13844 procedure Build_NCT_Hash_Tables is
13845 Elmt : Elmt_Id;
13846 Ent : Entity_Id;
13847 begin
13848 if NCT_Hash_Table_Setup then
13849 NCT_Assoc.Reset;
13850 NCT_Itype_Assoc.Reset;
13851 end if;
13853 Elmt := First_Elmt (Actual_Map);
13854 while Present (Elmt) loop
13855 Ent := Node (Elmt);
13857 -- Get new entity, and associate old and new
13859 Next_Elmt (Elmt);
13860 NCT_Assoc.Set (Ent, Node (Elmt));
13862 if Is_Type (Ent) then
13863 declare
13864 Anode : constant Entity_Id :=
13865 Associated_Node_For_Itype (Ent);
13867 begin
13868 if Present (Anode) then
13870 -- Enter a link between the associated node of the
13871 -- old Itype and the new Itype, for updating later
13872 -- when node is copied.
13874 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
13875 end if;
13876 end;
13877 end if;
13879 Next_Elmt (Elmt);
13880 end loop;
13882 NCT_Hash_Tables_Used := True;
13883 NCT_Hash_Table_Setup := True;
13884 end Build_NCT_Hash_Tables;
13886 ---------------------------------
13887 -- Copy_Elist_With_Replacement --
13888 ---------------------------------
13890 function Copy_Elist_With_Replacement
13891 (Old_Elist : Elist_Id) return Elist_Id
13893 M : Elmt_Id;
13894 New_Elist : Elist_Id;
13896 begin
13897 if No (Old_Elist) then
13898 return No_Elist;
13900 else
13901 New_Elist := New_Elmt_List;
13903 M := First_Elmt (Old_Elist);
13904 while Present (M) loop
13905 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
13906 Next_Elmt (M);
13907 end loop;
13908 end if;
13910 return New_Elist;
13911 end Copy_Elist_With_Replacement;
13913 ---------------------------------
13914 -- Copy_Itype_With_Replacement --
13915 ---------------------------------
13917 -- This routine exactly parallels its phase one analog Visit_Itype,
13919 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
13920 begin
13921 -- Translate Next_Entity, Scope and Etype fields, in case they
13922 -- reference entities that have been mapped into copies.
13924 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
13925 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
13927 if Present (New_Scope) then
13928 Set_Scope (New_Itype, New_Scope);
13929 else
13930 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
13931 end if;
13933 -- Copy referenced fields
13935 if Is_Discrete_Type (New_Itype) then
13936 Set_Scalar_Range (New_Itype,
13937 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
13939 elsif Has_Discriminants (Base_Type (New_Itype)) then
13940 Set_Discriminant_Constraint (New_Itype,
13941 Copy_Elist_With_Replacement
13942 (Discriminant_Constraint (New_Itype)));
13944 elsif Is_Array_Type (New_Itype) then
13945 if Present (First_Index (New_Itype)) then
13946 Set_First_Index (New_Itype,
13947 First (Copy_List_With_Replacement
13948 (List_Containing (First_Index (New_Itype)))));
13949 end if;
13951 if Is_Packed (New_Itype) then
13952 Set_Packed_Array_Impl_Type (New_Itype,
13953 Copy_Node_With_Replacement
13954 (Packed_Array_Impl_Type (New_Itype)));
13955 end if;
13956 end if;
13957 end Copy_Itype_With_Replacement;
13959 --------------------------------
13960 -- Copy_List_With_Replacement --
13961 --------------------------------
13963 function Copy_List_With_Replacement
13964 (Old_List : List_Id) return List_Id
13966 New_List : List_Id;
13967 E : Node_Id;
13969 begin
13970 if Old_List = No_List then
13971 return No_List;
13973 else
13974 New_List := Empty_List;
13976 E := First (Old_List);
13977 while Present (E) loop
13978 Append (Copy_Node_With_Replacement (E), New_List);
13979 Next (E);
13980 end loop;
13982 return New_List;
13983 end if;
13984 end Copy_List_With_Replacement;
13986 --------------------------------
13987 -- Copy_Node_With_Replacement --
13988 --------------------------------
13990 function Copy_Node_With_Replacement
13991 (Old_Node : Node_Id) return Node_Id
13993 New_Node : Node_Id;
13995 procedure Adjust_Named_Associations
13996 (Old_Node : Node_Id;
13997 New_Node : Node_Id);
13998 -- If a call node has named associations, these are chained through
13999 -- the First_Named_Actual, Next_Named_Actual links. These must be
14000 -- propagated separately to the new parameter list, because these
14001 -- are not syntactic fields.
14003 function Copy_Field_With_Replacement
14004 (Field : Union_Id) return Union_Id;
14005 -- Given Field, which is a field of Old_Node, return a copy of it
14006 -- if it is a syntactic field (i.e. its parent is Node), setting
14007 -- the parent of the copy to poit to New_Node. Otherwise returns
14008 -- the field (possibly mapped if it is an entity).
14010 -------------------------------
14011 -- Adjust_Named_Associations --
14012 -------------------------------
14014 procedure Adjust_Named_Associations
14015 (Old_Node : Node_Id;
14016 New_Node : Node_Id)
14018 Old_E : Node_Id;
14019 New_E : Node_Id;
14021 Old_Next : Node_Id;
14022 New_Next : Node_Id;
14024 begin
14025 Old_E := First (Parameter_Associations (Old_Node));
14026 New_E := First (Parameter_Associations (New_Node));
14027 while Present (Old_E) loop
14028 if Nkind (Old_E) = N_Parameter_Association
14029 and then Present (Next_Named_Actual (Old_E))
14030 then
14031 if First_Named_Actual (Old_Node)
14032 = Explicit_Actual_Parameter (Old_E)
14033 then
14034 Set_First_Named_Actual
14035 (New_Node, Explicit_Actual_Parameter (New_E));
14036 end if;
14038 -- Now scan parameter list from the beginning,to locate
14039 -- next named actual, which can be out of order.
14041 Old_Next := First (Parameter_Associations (Old_Node));
14042 New_Next := First (Parameter_Associations (New_Node));
14044 while Nkind (Old_Next) /= N_Parameter_Association
14045 or else Explicit_Actual_Parameter (Old_Next)
14046 /= Next_Named_Actual (Old_E)
14047 loop
14048 Next (Old_Next);
14049 Next (New_Next);
14050 end loop;
14052 Set_Next_Named_Actual
14053 (New_E, Explicit_Actual_Parameter (New_Next));
14054 end if;
14056 Next (Old_E);
14057 Next (New_E);
14058 end loop;
14059 end Adjust_Named_Associations;
14061 ---------------------------------
14062 -- Copy_Field_With_Replacement --
14063 ---------------------------------
14065 function Copy_Field_With_Replacement
14066 (Field : Union_Id) return Union_Id
14068 begin
14069 if Field = Union_Id (Empty) then
14070 return Field;
14072 elsif Field in Node_Range then
14073 declare
14074 Old_N : constant Node_Id := Node_Id (Field);
14075 New_N : Node_Id;
14077 begin
14078 -- If syntactic field, as indicated by the parent pointer
14079 -- being set, then copy the referenced node recursively.
14081 if Parent (Old_N) = Old_Node then
14082 New_N := Copy_Node_With_Replacement (Old_N);
14084 if New_N /= Old_N then
14085 Set_Parent (New_N, New_Node);
14086 end if;
14088 -- For semantic fields, update possible entity reference
14089 -- from the replacement map.
14091 else
14092 New_N := Assoc (Old_N);
14093 end if;
14095 return Union_Id (New_N);
14096 end;
14098 elsif Field in List_Range then
14099 declare
14100 Old_L : constant List_Id := List_Id (Field);
14101 New_L : List_Id;
14103 begin
14104 -- If syntactic field, as indicated by the parent pointer,
14105 -- then recursively copy the entire referenced list.
14107 if Parent (Old_L) = Old_Node then
14108 New_L := Copy_List_With_Replacement (Old_L);
14109 Set_Parent (New_L, New_Node);
14111 -- For semantic list, just returned unchanged
14113 else
14114 New_L := Old_L;
14115 end if;
14117 return Union_Id (New_L);
14118 end;
14120 -- Anything other than a list or a node is returned unchanged
14122 else
14123 return Field;
14124 end if;
14125 end Copy_Field_With_Replacement;
14127 -- Start of processing for Copy_Node_With_Replacement
14129 begin
14130 if Old_Node <= Empty_Or_Error then
14131 return Old_Node;
14133 elsif Has_Extension (Old_Node) then
14134 return Assoc (Old_Node);
14136 else
14137 New_Node := New_Copy (Old_Node);
14139 -- If the node we are copying is the associated node of a
14140 -- previously copied Itype, then adjust the associated node
14141 -- of the copy of that Itype accordingly.
14143 if Present (Actual_Map) then
14144 declare
14145 E : Elmt_Id;
14146 Ent : Entity_Id;
14148 begin
14149 -- Case of hash table used
14151 if NCT_Hash_Tables_Used then
14152 Ent := NCT_Itype_Assoc.Get (Old_Node);
14154 if Present (Ent) then
14155 Set_Associated_Node_For_Itype (Ent, New_Node);
14156 end if;
14158 -- Case of no hash table used
14160 else
14161 E := First_Elmt (Actual_Map);
14162 while Present (E) loop
14163 if Is_Itype (Node (E))
14164 and then
14165 Old_Node = Associated_Node_For_Itype (Node (E))
14166 then
14167 Set_Associated_Node_For_Itype
14168 (Node (Next_Elmt (E)), New_Node);
14169 end if;
14171 E := Next_Elmt (Next_Elmt (E));
14172 end loop;
14173 end if;
14174 end;
14175 end if;
14177 -- Recursively copy descendents
14179 Set_Field1
14180 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
14181 Set_Field2
14182 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
14183 Set_Field3
14184 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
14185 Set_Field4
14186 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
14187 Set_Field5
14188 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
14190 -- Adjust Sloc of new node if necessary
14192 if New_Sloc /= No_Location then
14193 Set_Sloc (New_Node, New_Sloc);
14195 -- If we adjust the Sloc, then we are essentially making
14196 -- a completely new node, so the Comes_From_Source flag
14197 -- should be reset to the proper default value.
14199 Nodes.Table (New_Node).Comes_From_Source :=
14200 Default_Node.Comes_From_Source;
14201 end if;
14203 -- If the node is call and has named associations,
14204 -- set the corresponding links in the copy.
14206 if (Nkind (Old_Node) = N_Function_Call
14207 or else Nkind (Old_Node) = N_Entry_Call_Statement
14208 or else
14209 Nkind (Old_Node) = N_Procedure_Call_Statement)
14210 and then Present (First_Named_Actual (Old_Node))
14211 then
14212 Adjust_Named_Associations (Old_Node, New_Node);
14213 end if;
14215 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
14216 -- The replacement mechanism applies to entities, and is not used
14217 -- here. Eventually we may need a more general graph-copying
14218 -- routine. For now, do a sequential search to find desired node.
14220 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
14221 and then Present (First_Real_Statement (Old_Node))
14222 then
14223 declare
14224 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
14225 N1, N2 : Node_Id;
14227 begin
14228 N1 := First (Statements (Old_Node));
14229 N2 := First (Statements (New_Node));
14231 while N1 /= Old_F loop
14232 Next (N1);
14233 Next (N2);
14234 end loop;
14236 Set_First_Real_Statement (New_Node, N2);
14237 end;
14238 end if;
14239 end if;
14241 -- All done, return copied node
14243 return New_Node;
14244 end Copy_Node_With_Replacement;
14246 -----------------
14247 -- Visit_Elist --
14248 -----------------
14250 procedure Visit_Elist (E : Elist_Id) is
14251 Elmt : Elmt_Id;
14252 begin
14253 if Present (E) then
14254 Elmt := First_Elmt (E);
14256 while Elmt /= No_Elmt loop
14257 Visit_Node (Node (Elmt));
14258 Next_Elmt (Elmt);
14259 end loop;
14260 end if;
14261 end Visit_Elist;
14263 -----------------
14264 -- Visit_Field --
14265 -----------------
14267 procedure Visit_Field (F : Union_Id; N : Node_Id) is
14268 begin
14269 if F = Union_Id (Empty) then
14270 return;
14272 elsif F in Node_Range then
14274 -- Copy node if it is syntactic, i.e. its parent pointer is
14275 -- set to point to the field that referenced it (certain
14276 -- Itypes will also meet this criterion, which is fine, since
14277 -- these are clearly Itypes that do need to be copied, since
14278 -- we are copying their parent.)
14280 if Parent (Node_Id (F)) = N then
14281 Visit_Node (Node_Id (F));
14282 return;
14284 -- Another case, if we are pointing to an Itype, then we want
14285 -- to copy it if its associated node is somewhere in the tree
14286 -- being copied.
14288 -- Note: the exclusion of self-referential copies is just an
14289 -- optimization, since the search of the already copied list
14290 -- would catch it, but it is a common case (Etype pointing
14291 -- to itself for an Itype that is a base type).
14293 elsif Has_Extension (Node_Id (F))
14294 and then Is_Itype (Entity_Id (F))
14295 and then Node_Id (F) /= N
14296 then
14297 declare
14298 P : Node_Id;
14300 begin
14301 P := Associated_Node_For_Itype (Node_Id (F));
14302 while Present (P) loop
14303 if P = Source then
14304 Visit_Node (Node_Id (F));
14305 return;
14306 else
14307 P := Parent (P);
14308 end if;
14309 end loop;
14311 -- An Itype whose parent is not being copied definitely
14312 -- should NOT be copied, since it does not belong in any
14313 -- sense to the copied subtree.
14315 return;
14316 end;
14317 end if;
14319 elsif F in List_Range and then Parent (List_Id (F)) = N then
14320 Visit_List (List_Id (F));
14321 return;
14322 end if;
14323 end Visit_Field;
14325 -----------------
14326 -- Visit_Itype --
14327 -----------------
14329 procedure Visit_Itype (Old_Itype : Entity_Id) is
14330 New_Itype : Entity_Id;
14331 E : Elmt_Id;
14332 Ent : Entity_Id;
14334 begin
14335 -- Itypes that describe the designated type of access to subprograms
14336 -- have the structure of subprogram declarations, with signatures,
14337 -- etc. Either we duplicate the signatures completely, or choose to
14338 -- share such itypes, which is fine because their elaboration will
14339 -- have no side effects.
14341 if Ekind (Old_Itype) = E_Subprogram_Type then
14342 return;
14343 end if;
14345 New_Itype := New_Copy (Old_Itype);
14347 -- The new Itype has all the attributes of the old one, and
14348 -- we just copy the contents of the entity. However, the back-end
14349 -- needs different names for debugging purposes, so we create a
14350 -- new internal name for it in all cases.
14352 Set_Chars (New_Itype, New_Internal_Name ('T'));
14354 -- If our associated node is an entity that has already been copied,
14355 -- then set the associated node of the copy to point to the right
14356 -- copy. If we have copied an Itype that is itself the associated
14357 -- node of some previously copied Itype, then we set the right
14358 -- pointer in the other direction.
14360 if Present (Actual_Map) then
14362 -- Case of hash tables used
14364 if NCT_Hash_Tables_Used then
14366 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
14368 if Present (Ent) then
14369 Set_Associated_Node_For_Itype (New_Itype, Ent);
14370 end if;
14372 Ent := NCT_Itype_Assoc.Get (Old_Itype);
14373 if Present (Ent) then
14374 Set_Associated_Node_For_Itype (Ent, New_Itype);
14376 -- If the hash table has no association for this Itype and
14377 -- its associated node, enter one now.
14379 else
14380 NCT_Itype_Assoc.Set
14381 (Associated_Node_For_Itype (Old_Itype), New_Itype);
14382 end if;
14384 -- Case of hash tables not used
14386 else
14387 E := First_Elmt (Actual_Map);
14388 while Present (E) loop
14389 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
14390 Set_Associated_Node_For_Itype
14391 (New_Itype, Node (Next_Elmt (E)));
14392 end if;
14394 if Is_Type (Node (E))
14395 and then Old_Itype = Associated_Node_For_Itype (Node (E))
14396 then
14397 Set_Associated_Node_For_Itype
14398 (Node (Next_Elmt (E)), New_Itype);
14399 end if;
14401 E := Next_Elmt (Next_Elmt (E));
14402 end loop;
14403 end if;
14404 end if;
14406 if Present (Freeze_Node (New_Itype)) then
14407 Set_Is_Frozen (New_Itype, False);
14408 Set_Freeze_Node (New_Itype, Empty);
14409 end if;
14411 -- Add new association to map
14413 if No (Actual_Map) then
14414 Actual_Map := New_Elmt_List;
14415 end if;
14417 Append_Elmt (Old_Itype, Actual_Map);
14418 Append_Elmt (New_Itype, Actual_Map);
14420 if NCT_Hash_Tables_Used then
14421 NCT_Assoc.Set (Old_Itype, New_Itype);
14423 else
14424 NCT_Table_Entries := NCT_Table_Entries + 1;
14426 if NCT_Table_Entries > NCT_Hash_Threshold then
14427 Build_NCT_Hash_Tables;
14428 end if;
14429 end if;
14431 -- If a record subtype is simply copied, the entity list will be
14432 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
14434 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
14435 Set_Cloned_Subtype (New_Itype, Old_Itype);
14436 end if;
14438 -- Visit descendents that eventually get copied
14440 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
14442 if Is_Discrete_Type (Old_Itype) then
14443 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
14445 elsif Has_Discriminants (Base_Type (Old_Itype)) then
14446 -- ??? This should involve call to Visit_Field
14447 Visit_Elist (Discriminant_Constraint (Old_Itype));
14449 elsif Is_Array_Type (Old_Itype) then
14450 if Present (First_Index (Old_Itype)) then
14451 Visit_Field (Union_Id (List_Containing
14452 (First_Index (Old_Itype))),
14453 Old_Itype);
14454 end if;
14456 if Is_Packed (Old_Itype) then
14457 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
14458 Old_Itype);
14459 end if;
14460 end if;
14461 end Visit_Itype;
14463 ----------------
14464 -- Visit_List --
14465 ----------------
14467 procedure Visit_List (L : List_Id) is
14468 N : Node_Id;
14469 begin
14470 if L /= No_List then
14471 N := First (L);
14473 while Present (N) loop
14474 Visit_Node (N);
14475 Next (N);
14476 end loop;
14477 end if;
14478 end Visit_List;
14480 ----------------
14481 -- Visit_Node --
14482 ----------------
14484 procedure Visit_Node (N : Node_Or_Entity_Id) is
14486 -- Start of processing for Visit_Node
14488 begin
14489 -- Handle case of an Itype, which must be copied
14491 if Has_Extension (N) and then Is_Itype (N) then
14493 -- Nothing to do if already in the list. This can happen with an
14494 -- Itype entity that appears more than once in the tree.
14495 -- Note that we do not want to visit descendents in this case.
14497 -- Test for already in list when hash table is used
14499 if NCT_Hash_Tables_Used then
14500 if Present (NCT_Assoc.Get (Entity_Id (N))) then
14501 return;
14502 end if;
14504 -- Test for already in list when hash table not used
14506 else
14507 declare
14508 E : Elmt_Id;
14509 begin
14510 if Present (Actual_Map) then
14511 E := First_Elmt (Actual_Map);
14512 while Present (E) loop
14513 if Node (E) = N then
14514 return;
14515 else
14516 E := Next_Elmt (Next_Elmt (E));
14517 end if;
14518 end loop;
14519 end if;
14520 end;
14521 end if;
14523 Visit_Itype (N);
14524 end if;
14526 -- Visit descendents
14528 Visit_Field (Field1 (N), N);
14529 Visit_Field (Field2 (N), N);
14530 Visit_Field (Field3 (N), N);
14531 Visit_Field (Field4 (N), N);
14532 Visit_Field (Field5 (N), N);
14533 end Visit_Node;
14535 -- Start of processing for New_Copy_Tree
14537 begin
14538 Actual_Map := Map;
14540 -- See if we should use hash table
14542 if No (Actual_Map) then
14543 NCT_Hash_Tables_Used := False;
14545 else
14546 declare
14547 Elmt : Elmt_Id;
14549 begin
14550 NCT_Table_Entries := 0;
14552 Elmt := First_Elmt (Actual_Map);
14553 while Present (Elmt) loop
14554 NCT_Table_Entries := NCT_Table_Entries + 1;
14555 Next_Elmt (Elmt);
14556 Next_Elmt (Elmt);
14557 end loop;
14559 if NCT_Table_Entries > NCT_Hash_Threshold then
14560 Build_NCT_Hash_Tables;
14561 else
14562 NCT_Hash_Tables_Used := False;
14563 end if;
14564 end;
14565 end if;
14567 -- Hash table set up if required, now start phase one by visiting
14568 -- top node (we will recursively visit the descendents).
14570 Visit_Node (Source);
14572 -- Now the second phase of the copy can start. First we process
14573 -- all the mapped entities, copying their descendents.
14575 if Present (Actual_Map) then
14576 declare
14577 Elmt : Elmt_Id;
14578 New_Itype : Entity_Id;
14579 begin
14580 Elmt := First_Elmt (Actual_Map);
14581 while Present (Elmt) loop
14582 Next_Elmt (Elmt);
14583 New_Itype := Node (Elmt);
14584 Copy_Itype_With_Replacement (New_Itype);
14585 Next_Elmt (Elmt);
14586 end loop;
14587 end;
14588 end if;
14590 -- Now we can copy the actual tree
14592 return Copy_Node_With_Replacement (Source);
14593 end New_Copy_Tree;
14595 -------------------------
14596 -- New_External_Entity --
14597 -------------------------
14599 function New_External_Entity
14600 (Kind : Entity_Kind;
14601 Scope_Id : Entity_Id;
14602 Sloc_Value : Source_Ptr;
14603 Related_Id : Entity_Id;
14604 Suffix : Character;
14605 Suffix_Index : Nat := 0;
14606 Prefix : Character := ' ') return Entity_Id
14608 N : constant Entity_Id :=
14609 Make_Defining_Identifier (Sloc_Value,
14610 New_External_Name
14611 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
14613 begin
14614 Set_Ekind (N, Kind);
14615 Set_Is_Internal (N, True);
14616 Append_Entity (N, Scope_Id);
14617 Set_Public_Status (N);
14619 if Kind in Type_Kind then
14620 Init_Size_Align (N);
14621 end if;
14623 return N;
14624 end New_External_Entity;
14626 -------------------------
14627 -- New_Internal_Entity --
14628 -------------------------
14630 function New_Internal_Entity
14631 (Kind : Entity_Kind;
14632 Scope_Id : Entity_Id;
14633 Sloc_Value : Source_Ptr;
14634 Id_Char : Character) return Entity_Id
14636 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
14638 begin
14639 Set_Ekind (N, Kind);
14640 Set_Is_Internal (N, True);
14641 Append_Entity (N, Scope_Id);
14643 if Kind in Type_Kind then
14644 Init_Size_Align (N);
14645 end if;
14647 return N;
14648 end New_Internal_Entity;
14650 -----------------
14651 -- Next_Actual --
14652 -----------------
14654 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
14655 N : Node_Id;
14657 begin
14658 -- If we are pointing at a positional parameter, it is a member of a
14659 -- node list (the list of parameters), and the next parameter is the
14660 -- next node on the list, unless we hit a parameter association, then
14661 -- we shift to using the chain whose head is the First_Named_Actual in
14662 -- the parent, and then is threaded using the Next_Named_Actual of the
14663 -- Parameter_Association. All this fiddling is because the original node
14664 -- list is in the textual call order, and what we need is the
14665 -- declaration order.
14667 if Is_List_Member (Actual_Id) then
14668 N := Next (Actual_Id);
14670 if Nkind (N) = N_Parameter_Association then
14671 return First_Named_Actual (Parent (Actual_Id));
14672 else
14673 return N;
14674 end if;
14676 else
14677 return Next_Named_Actual (Parent (Actual_Id));
14678 end if;
14679 end Next_Actual;
14681 procedure Next_Actual (Actual_Id : in out Node_Id) is
14682 begin
14683 Actual_Id := Next_Actual (Actual_Id);
14684 end Next_Actual;
14686 -----------------------
14687 -- Normalize_Actuals --
14688 -----------------------
14690 -- Chain actuals according to formals of subprogram. If there are no named
14691 -- associations, the chain is simply the list of Parameter Associations,
14692 -- since the order is the same as the declaration order. If there are named
14693 -- associations, then the First_Named_Actual field in the N_Function_Call
14694 -- or N_Procedure_Call_Statement node points to the Parameter_Association
14695 -- node for the parameter that comes first in declaration order. The
14696 -- remaining named parameters are then chained in declaration order using
14697 -- Next_Named_Actual.
14699 -- This routine also verifies that the number of actuals is compatible with
14700 -- the number and default values of formals, but performs no type checking
14701 -- (type checking is done by the caller).
14703 -- If the matching succeeds, Success is set to True and the caller proceeds
14704 -- with type-checking. If the match is unsuccessful, then Success is set to
14705 -- False, and the caller attempts a different interpretation, if there is
14706 -- one.
14708 -- If the flag Report is on, the call is not overloaded, and a failure to
14709 -- match can be reported here, rather than in the caller.
14711 procedure Normalize_Actuals
14712 (N : Node_Id;
14713 S : Entity_Id;
14714 Report : Boolean;
14715 Success : out Boolean)
14717 Actuals : constant List_Id := Parameter_Associations (N);
14718 Actual : Node_Id := Empty;
14719 Formal : Entity_Id;
14720 Last : Node_Id := Empty;
14721 First_Named : Node_Id := Empty;
14722 Found : Boolean;
14724 Formals_To_Match : Integer := 0;
14725 Actuals_To_Match : Integer := 0;
14727 procedure Chain (A : Node_Id);
14728 -- Add named actual at the proper place in the list, using the
14729 -- Next_Named_Actual link.
14731 function Reporting return Boolean;
14732 -- Determines if an error is to be reported. To report an error, we
14733 -- need Report to be True, and also we do not report errors caused
14734 -- by calls to init procs that occur within other init procs. Such
14735 -- errors must always be cascaded errors, since if all the types are
14736 -- declared correctly, the compiler will certainly build decent calls.
14738 -----------
14739 -- Chain --
14740 -----------
14742 procedure Chain (A : Node_Id) is
14743 begin
14744 if No (Last) then
14746 -- Call node points to first actual in list
14748 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
14750 else
14751 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
14752 end if;
14754 Last := A;
14755 Set_Next_Named_Actual (Last, Empty);
14756 end Chain;
14758 ---------------
14759 -- Reporting --
14760 ---------------
14762 function Reporting return Boolean is
14763 begin
14764 if not Report then
14765 return False;
14767 elsif not Within_Init_Proc then
14768 return True;
14770 elsif Is_Init_Proc (Entity (Name (N))) then
14771 return False;
14773 else
14774 return True;
14775 end if;
14776 end Reporting;
14778 -- Start of processing for Normalize_Actuals
14780 begin
14781 if Is_Access_Type (S) then
14783 -- The name in the call is a function call that returns an access
14784 -- to subprogram. The designated type has the list of formals.
14786 Formal := First_Formal (Designated_Type (S));
14787 else
14788 Formal := First_Formal (S);
14789 end if;
14791 while Present (Formal) loop
14792 Formals_To_Match := Formals_To_Match + 1;
14793 Next_Formal (Formal);
14794 end loop;
14796 -- Find if there is a named association, and verify that no positional
14797 -- associations appear after named ones.
14799 if Present (Actuals) then
14800 Actual := First (Actuals);
14801 end if;
14803 while Present (Actual)
14804 and then Nkind (Actual) /= N_Parameter_Association
14805 loop
14806 Actuals_To_Match := Actuals_To_Match + 1;
14807 Next (Actual);
14808 end loop;
14810 if No (Actual) and Actuals_To_Match = Formals_To_Match then
14812 -- Most common case: positional notation, no defaults
14814 Success := True;
14815 return;
14817 elsif Actuals_To_Match > Formals_To_Match then
14819 -- Too many actuals: will not work
14821 if Reporting then
14822 if Is_Entity_Name (Name (N)) then
14823 Error_Msg_N ("too many arguments in call to&", Name (N));
14824 else
14825 Error_Msg_N ("too many arguments in call", N);
14826 end if;
14827 end if;
14829 Success := False;
14830 return;
14831 end if;
14833 First_Named := Actual;
14835 while Present (Actual) loop
14836 if Nkind (Actual) /= N_Parameter_Association then
14837 Error_Msg_N
14838 ("positional parameters not allowed after named ones", Actual);
14839 Success := False;
14840 return;
14842 else
14843 Actuals_To_Match := Actuals_To_Match + 1;
14844 end if;
14846 Next (Actual);
14847 end loop;
14849 if Present (Actuals) then
14850 Actual := First (Actuals);
14851 end if;
14853 Formal := First_Formal (S);
14854 while Present (Formal) loop
14856 -- Match the formals in order. If the corresponding actual is
14857 -- positional, nothing to do. Else scan the list of named actuals
14858 -- to find the one with the right name.
14860 if Present (Actual)
14861 and then Nkind (Actual) /= N_Parameter_Association
14862 then
14863 Next (Actual);
14864 Actuals_To_Match := Actuals_To_Match - 1;
14865 Formals_To_Match := Formals_To_Match - 1;
14867 else
14868 -- For named parameters, search the list of actuals to find
14869 -- one that matches the next formal name.
14871 Actual := First_Named;
14872 Found := False;
14873 while Present (Actual) loop
14874 if Chars (Selector_Name (Actual)) = Chars (Formal) then
14875 Found := True;
14876 Chain (Actual);
14877 Actuals_To_Match := Actuals_To_Match - 1;
14878 Formals_To_Match := Formals_To_Match - 1;
14879 exit;
14880 end if;
14882 Next (Actual);
14883 end loop;
14885 if not Found then
14886 if Ekind (Formal) /= E_In_Parameter
14887 or else No (Default_Value (Formal))
14888 then
14889 if Reporting then
14890 if (Comes_From_Source (S)
14891 or else Sloc (S) = Standard_Location)
14892 and then Is_Overloadable (S)
14893 then
14894 if No (Actuals)
14895 and then
14896 Nkind_In (Parent (N), N_Procedure_Call_Statement,
14897 N_Function_Call,
14898 N_Parameter_Association)
14899 and then Ekind (S) /= E_Function
14900 then
14901 Set_Etype (N, Etype (S));
14903 else
14904 Error_Msg_Name_1 := Chars (S);
14905 Error_Msg_Sloc := Sloc (S);
14906 Error_Msg_NE
14907 ("missing argument for parameter & "
14908 & "in call to % declared #", N, Formal);
14909 end if;
14911 elsif Is_Overloadable (S) then
14912 Error_Msg_Name_1 := Chars (S);
14914 -- Point to type derivation that generated the
14915 -- operation.
14917 Error_Msg_Sloc := Sloc (Parent (S));
14919 Error_Msg_NE
14920 ("missing argument for parameter & "
14921 & "in call to % (inherited) #", N, Formal);
14923 else
14924 Error_Msg_NE
14925 ("missing argument for parameter &", N, Formal);
14926 end if;
14927 end if;
14929 Success := False;
14930 return;
14932 else
14933 Formals_To_Match := Formals_To_Match - 1;
14934 end if;
14935 end if;
14936 end if;
14938 Next_Formal (Formal);
14939 end loop;
14941 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
14942 Success := True;
14943 return;
14945 else
14946 if Reporting then
14948 -- Find some superfluous named actual that did not get
14949 -- attached to the list of associations.
14951 Actual := First (Actuals);
14952 while Present (Actual) loop
14953 if Nkind (Actual) = N_Parameter_Association
14954 and then Actual /= Last
14955 and then No (Next_Named_Actual (Actual))
14956 then
14957 Error_Msg_N ("unmatched actual & in call",
14958 Selector_Name (Actual));
14959 exit;
14960 end if;
14962 Next (Actual);
14963 end loop;
14964 end if;
14966 Success := False;
14967 return;
14968 end if;
14969 end Normalize_Actuals;
14971 --------------------------------
14972 -- Note_Possible_Modification --
14973 --------------------------------
14975 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
14976 Modification_Comes_From_Source : constant Boolean :=
14977 Comes_From_Source (Parent (N));
14979 Ent : Entity_Id;
14980 Exp : Node_Id;
14982 begin
14983 -- Loop to find referenced entity, if there is one
14985 Exp := N;
14986 loop
14987 Ent := Empty;
14989 if Is_Entity_Name (Exp) then
14990 Ent := Entity (Exp);
14992 -- If the entity is missing, it is an undeclared identifier,
14993 -- and there is nothing to annotate.
14995 if No (Ent) then
14996 return;
14997 end if;
14999 elsif Nkind (Exp) = N_Explicit_Dereference then
15000 declare
15001 P : constant Node_Id := Prefix (Exp);
15003 begin
15004 -- In formal verification mode, keep track of all reads and
15005 -- writes through explicit dereferences.
15007 if GNATprove_Mode then
15008 SPARK_Specific.Generate_Dereference (N, 'm');
15009 end if;
15011 if Nkind (P) = N_Selected_Component
15012 and then Present (Entry_Formal (Entity (Selector_Name (P))))
15013 then
15014 -- Case of a reference to an entry formal
15016 Ent := Entry_Formal (Entity (Selector_Name (P)));
15018 elsif Nkind (P) = N_Identifier
15019 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
15020 and then Present (Expression (Parent (Entity (P))))
15021 and then Nkind (Expression (Parent (Entity (P)))) =
15022 N_Reference
15023 then
15024 -- Case of a reference to a value on which side effects have
15025 -- been removed.
15027 Exp := Prefix (Expression (Parent (Entity (P))));
15028 goto Continue;
15030 else
15031 return;
15032 end if;
15033 end;
15035 elsif Nkind_In (Exp, N_Type_Conversion,
15036 N_Unchecked_Type_Conversion)
15037 then
15038 Exp := Expression (Exp);
15039 goto Continue;
15041 elsif Nkind_In (Exp, N_Slice,
15042 N_Indexed_Component,
15043 N_Selected_Component)
15044 then
15045 -- Special check, if the prefix is an access type, then return
15046 -- since we are modifying the thing pointed to, not the prefix.
15047 -- When we are expanding, most usually the prefix is replaced
15048 -- by an explicit dereference, and this test is not needed, but
15049 -- in some cases (notably -gnatc mode and generics) when we do
15050 -- not do full expansion, we need this special test.
15052 if Is_Access_Type (Etype (Prefix (Exp))) then
15053 return;
15055 -- Otherwise go to prefix and keep going
15057 else
15058 Exp := Prefix (Exp);
15059 goto Continue;
15060 end if;
15062 -- All other cases, not a modification
15064 else
15065 return;
15066 end if;
15068 -- Now look for entity being referenced
15070 if Present (Ent) then
15071 if Is_Object (Ent) then
15072 if Comes_From_Source (Exp)
15073 or else Modification_Comes_From_Source
15074 then
15075 -- Give warning if pragma unmodified given and we are
15076 -- sure this is a modification.
15078 if Has_Pragma_Unmodified (Ent) and then Sure then
15079 Error_Msg_NE ("??pragma Unmodified given for &!", N, Ent);
15080 end if;
15082 Set_Never_Set_In_Source (Ent, False);
15083 end if;
15085 Set_Is_True_Constant (Ent, False);
15086 Set_Current_Value (Ent, Empty);
15087 Set_Is_Known_Null (Ent, False);
15089 if not Can_Never_Be_Null (Ent) then
15090 Set_Is_Known_Non_Null (Ent, False);
15091 end if;
15093 -- Follow renaming chain
15095 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
15096 and then Present (Renamed_Object (Ent))
15097 then
15098 Exp := Renamed_Object (Ent);
15100 -- If the entity is the loop variable in an iteration over
15101 -- a container, retrieve container expression to indicate
15102 -- possible modificastion.
15104 if Present (Related_Expression (Ent))
15105 and then Nkind (Parent (Related_Expression (Ent))) =
15106 N_Iterator_Specification
15107 then
15108 Exp := Original_Node (Related_Expression (Ent));
15109 end if;
15111 goto Continue;
15113 -- The expression may be the renaming of a subcomponent of an
15114 -- array or container. The assignment to the subcomponent is
15115 -- a modification of the container.
15117 elsif Comes_From_Source (Original_Node (Exp))
15118 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
15119 N_Indexed_Component)
15120 then
15121 Exp := Prefix (Original_Node (Exp));
15122 goto Continue;
15123 end if;
15125 -- Generate a reference only if the assignment comes from
15126 -- source. This excludes, for example, calls to a dispatching
15127 -- assignment operation when the left-hand side is tagged. In
15128 -- GNATprove mode, we need those references also on generated
15129 -- code, as these are used to compute the local effects of
15130 -- subprograms.
15132 if Modification_Comes_From_Source or GNATprove_Mode then
15133 Generate_Reference (Ent, Exp, 'm');
15135 -- If the target of the assignment is the bound variable
15136 -- in an iterator, indicate that the corresponding array
15137 -- or container is also modified.
15139 if Ada_Version >= Ada_2012
15140 and then Nkind (Parent (Ent)) = N_Iterator_Specification
15141 then
15142 declare
15143 Domain : constant Node_Id := Name (Parent (Ent));
15145 begin
15146 -- TBD : in the full version of the construct, the
15147 -- domain of iteration can be given by an expression.
15149 if Is_Entity_Name (Domain) then
15150 Generate_Reference (Entity (Domain), Exp, 'm');
15151 Set_Is_True_Constant (Entity (Domain), False);
15152 Set_Never_Set_In_Source (Entity (Domain), False);
15153 end if;
15154 end;
15155 end if;
15156 end if;
15158 Check_Nested_Access (Ent);
15159 end if;
15161 Kill_Checks (Ent);
15163 -- If we are sure this is a modification from source, and we know
15164 -- this modifies a constant, then give an appropriate warning.
15166 if Overlays_Constant (Ent)
15167 and then (Modification_Comes_From_Source and Sure)
15168 then
15169 declare
15170 A : constant Node_Id := Address_Clause (Ent);
15171 begin
15172 if Present (A) then
15173 declare
15174 Exp : constant Node_Id := Expression (A);
15175 begin
15176 if Nkind (Exp) = N_Attribute_Reference
15177 and then Attribute_Name (Exp) = Name_Address
15178 and then Is_Entity_Name (Prefix (Exp))
15179 then
15180 Error_Msg_Sloc := Sloc (A);
15181 Error_Msg_NE
15182 ("constant& may be modified via address "
15183 & "clause#??", N, Entity (Prefix (Exp)));
15184 end if;
15185 end;
15186 end if;
15187 end;
15188 end if;
15190 return;
15191 end if;
15193 <<Continue>>
15194 null;
15195 end loop;
15196 end Note_Possible_Modification;
15198 -------------------------
15199 -- Object_Access_Level --
15200 -------------------------
15202 -- Returns the static accessibility level of the view denoted by Obj. Note
15203 -- that the value returned is the result of a call to Scope_Depth. Only
15204 -- scope depths associated with dynamic scopes can actually be returned.
15205 -- Since only relative levels matter for accessibility checking, the fact
15206 -- that the distance between successive levels of accessibility is not
15207 -- always one is immaterial (invariant: if level(E2) is deeper than
15208 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
15210 function Object_Access_Level (Obj : Node_Id) return Uint is
15211 function Is_Interface_Conversion (N : Node_Id) return Boolean;
15212 -- Determine whether N is a construct of the form
15213 -- Some_Type (Operand._tag'Address)
15214 -- This construct appears in the context of dispatching calls.
15216 function Reference_To (Obj : Node_Id) return Node_Id;
15217 -- An explicit dereference is created when removing side-effects from
15218 -- expressions for constraint checking purposes. In this case a local
15219 -- access type is created for it. The correct access level is that of
15220 -- the original source node. We detect this case by noting that the
15221 -- prefix of the dereference is created by an object declaration whose
15222 -- initial expression is a reference.
15224 -----------------------------
15225 -- Is_Interface_Conversion --
15226 -----------------------------
15228 function Is_Interface_Conversion (N : Node_Id) return Boolean is
15229 begin
15230 return Nkind (N) = N_Unchecked_Type_Conversion
15231 and then Nkind (Expression (N)) = N_Attribute_Reference
15232 and then Attribute_Name (Expression (N)) = Name_Address;
15233 end Is_Interface_Conversion;
15235 ------------------
15236 -- Reference_To --
15237 ------------------
15239 function Reference_To (Obj : Node_Id) return Node_Id is
15240 Pref : constant Node_Id := Prefix (Obj);
15241 begin
15242 if Is_Entity_Name (Pref)
15243 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
15244 and then Present (Expression (Parent (Entity (Pref))))
15245 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
15246 then
15247 return (Prefix (Expression (Parent (Entity (Pref)))));
15248 else
15249 return Empty;
15250 end if;
15251 end Reference_To;
15253 -- Local variables
15255 E : Entity_Id;
15257 -- Start of processing for Object_Access_Level
15259 begin
15260 if Nkind (Obj) = N_Defining_Identifier
15261 or else Is_Entity_Name (Obj)
15262 then
15263 if Nkind (Obj) = N_Defining_Identifier then
15264 E := Obj;
15265 else
15266 E := Entity (Obj);
15267 end if;
15269 if Is_Prival (E) then
15270 E := Prival_Link (E);
15271 end if;
15273 -- If E is a type then it denotes a current instance. For this case
15274 -- we add one to the normal accessibility level of the type to ensure
15275 -- that current instances are treated as always being deeper than
15276 -- than the level of any visible named access type (see 3.10.2(21)).
15278 if Is_Type (E) then
15279 return Type_Access_Level (E) + 1;
15281 elsif Present (Renamed_Object (E)) then
15282 return Object_Access_Level (Renamed_Object (E));
15284 -- Similarly, if E is a component of the current instance of a
15285 -- protected type, any instance of it is assumed to be at a deeper
15286 -- level than the type. For a protected object (whose type is an
15287 -- anonymous protected type) its components are at the same level
15288 -- as the type itself.
15290 elsif not Is_Overloadable (E)
15291 and then Ekind (Scope (E)) = E_Protected_Type
15292 and then Comes_From_Source (Scope (E))
15293 then
15294 return Type_Access_Level (Scope (E)) + 1;
15296 else
15297 -- Aliased formals take their access level from the point of call.
15298 -- This is smaller than the level of the subprogram itself.
15300 if Is_Formal (E) and then Is_Aliased (E) then
15301 return Type_Access_Level (Etype (E));
15303 else
15304 return Scope_Depth (Enclosing_Dynamic_Scope (E));
15305 end if;
15306 end if;
15308 elsif Nkind (Obj) = N_Selected_Component then
15309 if Is_Access_Type (Etype (Prefix (Obj))) then
15310 return Type_Access_Level (Etype (Prefix (Obj)));
15311 else
15312 return Object_Access_Level (Prefix (Obj));
15313 end if;
15315 elsif Nkind (Obj) = N_Indexed_Component then
15316 if Is_Access_Type (Etype (Prefix (Obj))) then
15317 return Type_Access_Level (Etype (Prefix (Obj)));
15318 else
15319 return Object_Access_Level (Prefix (Obj));
15320 end if;
15322 elsif Nkind (Obj) = N_Explicit_Dereference then
15324 -- If the prefix is a selected access discriminant then we make a
15325 -- recursive call on the prefix, which will in turn check the level
15326 -- of the prefix object of the selected discriminant.
15328 -- In Ada 2012, if the discriminant has implicit dereference and
15329 -- the context is a selected component, treat this as an object of
15330 -- unknown scope (see below). This is necessary in compile-only mode;
15331 -- otherwise expansion will already have transformed the prefix into
15332 -- a temporary.
15334 if Nkind (Prefix (Obj)) = N_Selected_Component
15335 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
15336 and then
15337 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
15338 and then
15339 (not Has_Implicit_Dereference
15340 (Entity (Selector_Name (Prefix (Obj))))
15341 or else Nkind (Parent (Obj)) /= N_Selected_Component)
15342 then
15343 return Object_Access_Level (Prefix (Obj));
15345 -- Detect an interface conversion in the context of a dispatching
15346 -- call. Use the original form of the conversion to find the access
15347 -- level of the operand.
15349 elsif Is_Interface (Etype (Obj))
15350 and then Is_Interface_Conversion (Prefix (Obj))
15351 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
15352 then
15353 return Object_Access_Level (Original_Node (Obj));
15355 elsif not Comes_From_Source (Obj) then
15356 declare
15357 Ref : constant Node_Id := Reference_To (Obj);
15358 begin
15359 if Present (Ref) then
15360 return Object_Access_Level (Ref);
15361 else
15362 return Type_Access_Level (Etype (Prefix (Obj)));
15363 end if;
15364 end;
15366 else
15367 return Type_Access_Level (Etype (Prefix (Obj)));
15368 end if;
15370 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
15371 return Object_Access_Level (Expression (Obj));
15373 elsif Nkind (Obj) = N_Function_Call then
15375 -- Function results are objects, so we get either the access level of
15376 -- the function or, in the case of an indirect call, the level of the
15377 -- access-to-subprogram type. (This code is used for Ada 95, but it
15378 -- looks wrong, because it seems that we should be checking the level
15379 -- of the call itself, even for Ada 95. However, using the Ada 2005
15380 -- version of the code causes regressions in several tests that are
15381 -- compiled with -gnat95. ???)
15383 if Ada_Version < Ada_2005 then
15384 if Is_Entity_Name (Name (Obj)) then
15385 return Subprogram_Access_Level (Entity (Name (Obj)));
15386 else
15387 return Type_Access_Level (Etype (Prefix (Name (Obj))));
15388 end if;
15390 -- For Ada 2005, the level of the result object of a function call is
15391 -- defined to be the level of the call's innermost enclosing master.
15392 -- We determine that by querying the depth of the innermost enclosing
15393 -- dynamic scope.
15395 else
15396 Return_Master_Scope_Depth_Of_Call : declare
15398 function Innermost_Master_Scope_Depth
15399 (N : Node_Id) return Uint;
15400 -- Returns the scope depth of the given node's innermost
15401 -- enclosing dynamic scope (effectively the accessibility
15402 -- level of the innermost enclosing master).
15404 ----------------------------------
15405 -- Innermost_Master_Scope_Depth --
15406 ----------------------------------
15408 function Innermost_Master_Scope_Depth
15409 (N : Node_Id) return Uint
15411 Node_Par : Node_Id := Parent (N);
15413 begin
15414 -- Locate the nearest enclosing node (by traversing Parents)
15415 -- that Defining_Entity can be applied to, and return the
15416 -- depth of that entity's nearest enclosing dynamic scope.
15418 while Present (Node_Par) loop
15419 case Nkind (Node_Par) is
15420 when N_Component_Declaration |
15421 N_Entry_Declaration |
15422 N_Formal_Object_Declaration |
15423 N_Formal_Type_Declaration |
15424 N_Full_Type_Declaration |
15425 N_Incomplete_Type_Declaration |
15426 N_Loop_Parameter_Specification |
15427 N_Object_Declaration |
15428 N_Protected_Type_Declaration |
15429 N_Private_Extension_Declaration |
15430 N_Private_Type_Declaration |
15431 N_Subtype_Declaration |
15432 N_Function_Specification |
15433 N_Procedure_Specification |
15434 N_Task_Type_Declaration |
15435 N_Body_Stub |
15436 N_Generic_Instantiation |
15437 N_Proper_Body |
15438 N_Implicit_Label_Declaration |
15439 N_Package_Declaration |
15440 N_Single_Task_Declaration |
15441 N_Subprogram_Declaration |
15442 N_Generic_Declaration |
15443 N_Renaming_Declaration |
15444 N_Block_Statement |
15445 N_Formal_Subprogram_Declaration |
15446 N_Abstract_Subprogram_Declaration |
15447 N_Entry_Body |
15448 N_Exception_Declaration |
15449 N_Formal_Package_Declaration |
15450 N_Number_Declaration |
15451 N_Package_Specification |
15452 N_Parameter_Specification |
15453 N_Single_Protected_Declaration |
15454 N_Subunit =>
15456 return Scope_Depth
15457 (Nearest_Dynamic_Scope
15458 (Defining_Entity (Node_Par)));
15460 when others =>
15461 null;
15462 end case;
15464 Node_Par := Parent (Node_Par);
15465 end loop;
15467 pragma Assert (False);
15469 -- Should never reach the following return
15471 return Scope_Depth (Current_Scope) + 1;
15472 end Innermost_Master_Scope_Depth;
15474 -- Start of processing for Return_Master_Scope_Depth_Of_Call
15476 begin
15477 return Innermost_Master_Scope_Depth (Obj);
15478 end Return_Master_Scope_Depth_Of_Call;
15479 end if;
15481 -- For convenience we handle qualified expressions, even though they
15482 -- aren't technically object names.
15484 elsif Nkind (Obj) = N_Qualified_Expression then
15485 return Object_Access_Level (Expression (Obj));
15487 -- Ditto for aggregates. They have the level of the temporary that
15488 -- will hold their value.
15490 elsif Nkind (Obj) = N_Aggregate then
15491 return Object_Access_Level (Current_Scope);
15493 -- Otherwise return the scope level of Standard. (If there are cases
15494 -- that fall through to this point they will be treated as having
15495 -- global accessibility for now. ???)
15497 else
15498 return Scope_Depth (Standard_Standard);
15499 end if;
15500 end Object_Access_Level;
15502 ---------------------------------
15503 -- Original_Aspect_Pragma_Name --
15504 ---------------------------------
15506 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
15507 Item : Node_Id;
15508 Item_Nam : Name_Id;
15510 begin
15511 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
15513 Item := N;
15515 -- The pragma was generated to emulate an aspect, use the original
15516 -- aspect specification.
15518 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
15519 Item := Corresponding_Aspect (Item);
15520 end if;
15522 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
15523 -- Post and Post_Class rewrite their pragma identifier to preserve the
15524 -- original name.
15525 -- ??? this is kludgey
15527 if Nkind (Item) = N_Pragma then
15528 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
15530 else
15531 pragma Assert (Nkind (Item) = N_Aspect_Specification);
15532 Item_Nam := Chars (Identifier (Item));
15533 end if;
15535 -- Deal with 'Class by converting the name to its _XXX form
15537 if Class_Present (Item) then
15538 if Item_Nam = Name_Invariant then
15539 Item_Nam := Name_uInvariant;
15541 elsif Item_Nam = Name_Post then
15542 Item_Nam := Name_uPost;
15544 elsif Item_Nam = Name_Pre then
15545 Item_Nam := Name_uPre;
15547 elsif Nam_In (Item_Nam, Name_Type_Invariant,
15548 Name_Type_Invariant_Class)
15549 then
15550 Item_Nam := Name_uType_Invariant;
15552 -- Nothing to do for other cases (e.g. a Check that derived from
15553 -- Pre_Class and has the flag set). Also we do nothing if the name
15554 -- is already in special _xxx form.
15556 end if;
15557 end if;
15559 return Item_Nam;
15560 end Original_Aspect_Pragma_Name;
15562 --------------------------------------
15563 -- Original_Corresponding_Operation --
15564 --------------------------------------
15566 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
15568 Typ : constant Entity_Id := Find_Dispatching_Type (S);
15570 begin
15571 -- If S is an inherited primitive S2 the original corresponding
15572 -- operation of S is the original corresponding operation of S2
15574 if Present (Alias (S))
15575 and then Find_Dispatching_Type (Alias (S)) /= Typ
15576 then
15577 return Original_Corresponding_Operation (Alias (S));
15579 -- If S overrides an inherited subprogram S2 the original corresponding
15580 -- operation of S is the original corresponding operation of S2
15582 elsif Present (Overridden_Operation (S)) then
15583 return Original_Corresponding_Operation (Overridden_Operation (S));
15585 -- otherwise it is S itself
15587 else
15588 return S;
15589 end if;
15590 end Original_Corresponding_Operation;
15592 ----------------------
15593 -- Policy_In_Effect --
15594 ----------------------
15596 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
15597 function Policy_In_List (List : Node_Id) return Name_Id;
15598 -- Determine the the mode of a policy in a N_Pragma list
15600 --------------------
15601 -- Policy_In_List --
15602 --------------------
15604 function Policy_In_List (List : Node_Id) return Name_Id is
15605 Arg : Node_Id;
15606 Expr : Node_Id;
15607 Prag : Node_Id;
15609 begin
15610 Prag := List;
15611 while Present (Prag) loop
15612 Arg := First (Pragma_Argument_Associations (Prag));
15613 Expr := Get_Pragma_Arg (Arg);
15615 -- The current Check_Policy pragma matches the requested policy,
15616 -- return the second argument which denotes the policy identifier.
15618 if Chars (Expr) = Policy then
15619 return Chars (Get_Pragma_Arg (Next (Arg)));
15620 end if;
15622 Prag := Next_Pragma (Prag);
15623 end loop;
15625 return No_Name;
15626 end Policy_In_List;
15628 -- Local variables
15630 Kind : Name_Id;
15632 -- Start of processing for Policy_In_Effect
15634 begin
15635 if not Is_Valid_Assertion_Kind (Policy) then
15636 raise Program_Error;
15637 end if;
15639 -- Inspect all policy pragmas that appear within scopes (if any)
15641 Kind := Policy_In_List (Check_Policy_List);
15643 -- Inspect all configuration policy pragmas (if any)
15645 if Kind = No_Name then
15646 Kind := Policy_In_List (Check_Policy_List_Config);
15647 end if;
15649 -- The context lacks policy pragmas, determine the mode based on whether
15650 -- assertions are enabled at the configuration level. This ensures that
15651 -- the policy is preserved when analyzing generics.
15653 if Kind = No_Name then
15654 if Assertions_Enabled_Config then
15655 Kind := Name_Check;
15656 else
15657 Kind := Name_Ignore;
15658 end if;
15659 end if;
15661 return Kind;
15662 end Policy_In_Effect;
15664 ----------------------------------
15665 -- Predicate_Tests_On_Arguments --
15666 ----------------------------------
15668 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
15669 begin
15670 -- Always test predicates on indirect call
15672 if Ekind (Subp) = E_Subprogram_Type then
15673 return True;
15675 -- Do not test predicates on call to generated default Finalize, since
15676 -- we are not interested in whether something we are finalizing (and
15677 -- typically destroying) satisfies its predicates.
15679 elsif Chars (Subp) = Name_Finalize
15680 and then not Comes_From_Source (Subp)
15681 then
15682 return False;
15684 -- Do not test predicates on any internally generated routines
15686 elsif Is_Internal_Name (Chars (Subp)) then
15687 return False;
15689 -- Do not test predicates on call to Init_Proc, since if needed the
15690 -- predicate test will occur at some other point.
15692 elsif Is_Init_Proc (Subp) then
15693 return False;
15695 -- Do not test predicates on call to predicate function, since this
15696 -- would cause infinite recursion.
15698 elsif Ekind (Subp) = E_Function
15699 and then (Is_Predicate_Function (Subp)
15700 or else
15701 Is_Predicate_Function_M (Subp))
15702 then
15703 return False;
15705 -- For now, no other exceptions
15707 else
15708 return True;
15709 end if;
15710 end Predicate_Tests_On_Arguments;
15712 -----------------------
15713 -- Private_Component --
15714 -----------------------
15716 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
15717 Ancestor : constant Entity_Id := Base_Type (Type_Id);
15719 function Trace_Components
15720 (T : Entity_Id;
15721 Check : Boolean) return Entity_Id;
15722 -- Recursive function that does the work, and checks against circular
15723 -- definition for each subcomponent type.
15725 ----------------------
15726 -- Trace_Components --
15727 ----------------------
15729 function Trace_Components
15730 (T : Entity_Id;
15731 Check : Boolean) return Entity_Id
15733 Btype : constant Entity_Id := Base_Type (T);
15734 Component : Entity_Id;
15735 P : Entity_Id;
15736 Candidate : Entity_Id := Empty;
15738 begin
15739 if Check and then Btype = Ancestor then
15740 Error_Msg_N ("circular type definition", Type_Id);
15741 return Any_Type;
15742 end if;
15744 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
15745 if Present (Full_View (Btype))
15746 and then Is_Record_Type (Full_View (Btype))
15747 and then not Is_Frozen (Btype)
15748 then
15749 -- To indicate that the ancestor depends on a private type, the
15750 -- current Btype is sufficient. However, to check for circular
15751 -- definition we must recurse on the full view.
15753 Candidate := Trace_Components (Full_View (Btype), True);
15755 if Candidate = Any_Type then
15756 return Any_Type;
15757 else
15758 return Btype;
15759 end if;
15761 else
15762 return Btype;
15763 end if;
15765 elsif Is_Array_Type (Btype) then
15766 return Trace_Components (Component_Type (Btype), True);
15768 elsif Is_Record_Type (Btype) then
15769 Component := First_Entity (Btype);
15770 while Present (Component)
15771 and then Comes_From_Source (Component)
15772 loop
15773 -- Skip anonymous types generated by constrained components
15775 if not Is_Type (Component) then
15776 P := Trace_Components (Etype (Component), True);
15778 if Present (P) then
15779 if P = Any_Type then
15780 return P;
15781 else
15782 Candidate := P;
15783 end if;
15784 end if;
15785 end if;
15787 Next_Entity (Component);
15788 end loop;
15790 return Candidate;
15792 else
15793 return Empty;
15794 end if;
15795 end Trace_Components;
15797 -- Start of processing for Private_Component
15799 begin
15800 return Trace_Components (Type_Id, False);
15801 end Private_Component;
15803 ---------------------------
15804 -- Primitive_Names_Match --
15805 ---------------------------
15807 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
15809 function Non_Internal_Name (E : Entity_Id) return Name_Id;
15810 -- Given an internal name, returns the corresponding non-internal name
15812 ------------------------
15813 -- Non_Internal_Name --
15814 ------------------------
15816 function Non_Internal_Name (E : Entity_Id) return Name_Id is
15817 begin
15818 Get_Name_String (Chars (E));
15819 Name_Len := Name_Len - 1;
15820 return Name_Find;
15821 end Non_Internal_Name;
15823 -- Start of processing for Primitive_Names_Match
15825 begin
15826 pragma Assert (Present (E1) and then Present (E2));
15828 return Chars (E1) = Chars (E2)
15829 or else
15830 (not Is_Internal_Name (Chars (E1))
15831 and then Is_Internal_Name (Chars (E2))
15832 and then Non_Internal_Name (E2) = Chars (E1))
15833 or else
15834 (not Is_Internal_Name (Chars (E2))
15835 and then Is_Internal_Name (Chars (E1))
15836 and then Non_Internal_Name (E1) = Chars (E2))
15837 or else
15838 (Is_Predefined_Dispatching_Operation (E1)
15839 and then Is_Predefined_Dispatching_Operation (E2)
15840 and then Same_TSS (E1, E2))
15841 or else
15842 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
15843 end Primitive_Names_Match;
15845 -----------------------
15846 -- Process_End_Label --
15847 -----------------------
15849 procedure Process_End_Label
15850 (N : Node_Id;
15851 Typ : Character;
15852 Ent : Entity_Id)
15854 Loc : Source_Ptr;
15855 Nam : Node_Id;
15856 Scop : Entity_Id;
15858 Label_Ref : Boolean;
15859 -- Set True if reference to end label itself is required
15861 Endl : Node_Id;
15862 -- Gets set to the operator symbol or identifier that references the
15863 -- entity Ent. For the child unit case, this is the identifier from the
15864 -- designator. For other cases, this is simply Endl.
15866 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
15867 -- N is an identifier node that appears as a parent unit reference in
15868 -- the case where Ent is a child unit. This procedure generates an
15869 -- appropriate cross-reference entry. E is the corresponding entity.
15871 -------------------------
15872 -- Generate_Parent_Ref --
15873 -------------------------
15875 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
15876 begin
15877 -- If names do not match, something weird, skip reference
15879 if Chars (E) = Chars (N) then
15881 -- Generate the reference. We do NOT consider this as a reference
15882 -- for unreferenced symbol purposes.
15884 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
15886 if Style_Check then
15887 Style.Check_Identifier (N, E);
15888 end if;
15889 end if;
15890 end Generate_Parent_Ref;
15892 -- Start of processing for Process_End_Label
15894 begin
15895 -- If no node, ignore. This happens in some error situations, and
15896 -- also for some internally generated structures where no end label
15897 -- references are required in any case.
15899 if No (N) then
15900 return;
15901 end if;
15903 -- Nothing to do if no End_Label, happens for internally generated
15904 -- constructs where we don't want an end label reference anyway. Also
15905 -- nothing to do if Endl is a string literal, which means there was
15906 -- some prior error (bad operator symbol)
15908 Endl := End_Label (N);
15910 if No (Endl) or else Nkind (Endl) = N_String_Literal then
15911 return;
15912 end if;
15914 -- Reference node is not in extended main source unit
15916 if not In_Extended_Main_Source_Unit (N) then
15918 -- Generally we do not collect references except for the extended
15919 -- main source unit. The one exception is the 'e' entry for a
15920 -- package spec, where it is useful for a client to have the
15921 -- ending information to define scopes.
15923 if Typ /= 'e' then
15924 return;
15926 else
15927 Label_Ref := False;
15929 -- For this case, we can ignore any parent references, but we
15930 -- need the package name itself for the 'e' entry.
15932 if Nkind (Endl) = N_Designator then
15933 Endl := Identifier (Endl);
15934 end if;
15935 end if;
15937 -- Reference is in extended main source unit
15939 else
15940 Label_Ref := True;
15942 -- For designator, generate references for the parent entries
15944 if Nkind (Endl) = N_Designator then
15946 -- Generate references for the prefix if the END line comes from
15947 -- source (otherwise we do not need these references) We climb the
15948 -- scope stack to find the expected entities.
15950 if Comes_From_Source (Endl) then
15951 Nam := Name (Endl);
15952 Scop := Current_Scope;
15953 while Nkind (Nam) = N_Selected_Component loop
15954 Scop := Scope (Scop);
15955 exit when No (Scop);
15956 Generate_Parent_Ref (Selector_Name (Nam), Scop);
15957 Nam := Prefix (Nam);
15958 end loop;
15960 if Present (Scop) then
15961 Generate_Parent_Ref (Nam, Scope (Scop));
15962 end if;
15963 end if;
15965 Endl := Identifier (Endl);
15966 end if;
15967 end if;
15969 -- If the end label is not for the given entity, then either we have
15970 -- some previous error, or this is a generic instantiation for which
15971 -- we do not need to make a cross-reference in this case anyway. In
15972 -- either case we simply ignore the call.
15974 if Chars (Ent) /= Chars (Endl) then
15975 return;
15976 end if;
15978 -- If label was really there, then generate a normal reference and then
15979 -- adjust the location in the end label to point past the name (which
15980 -- should almost always be the semicolon).
15982 Loc := Sloc (Endl);
15984 if Comes_From_Source (Endl) then
15986 -- If a label reference is required, then do the style check and
15987 -- generate an l-type cross-reference entry for the label
15989 if Label_Ref then
15990 if Style_Check then
15991 Style.Check_Identifier (Endl, Ent);
15992 end if;
15994 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
15995 end if;
15997 -- Set the location to point past the label (normally this will
15998 -- mean the semicolon immediately following the label). This is
15999 -- done for the sake of the 'e' or 't' entry generated below.
16001 Get_Decoded_Name_String (Chars (Endl));
16002 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
16004 else
16005 -- In SPARK mode, no missing label is allowed for packages and
16006 -- subprogram bodies. Detect those cases by testing whether
16007 -- Process_End_Label was called for a body (Typ = 't') or a package.
16009 if Restriction_Check_Required (SPARK_05)
16010 and then (Typ = 't' or else Ekind (Ent) = E_Package)
16011 then
16012 Error_Msg_Node_1 := Endl;
16013 Check_SPARK_05_Restriction
16014 ("`END &` required", Endl, Force => True);
16015 end if;
16016 end if;
16018 -- Now generate the e/t reference
16020 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
16022 -- Restore Sloc, in case modified above, since we have an identifier
16023 -- and the normal Sloc should be left set in the tree.
16025 Set_Sloc (Endl, Loc);
16026 end Process_End_Label;
16028 ----------------
16029 -- Referenced --
16030 ----------------
16032 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
16033 Seen : Boolean := False;
16035 function Is_Reference (N : Node_Id) return Traverse_Result;
16036 -- Determine whether node N denotes a reference to Id. If this is the
16037 -- case, set global flag Seen to True and stop the traversal.
16039 ------------------
16040 -- Is_Reference --
16041 ------------------
16043 function Is_Reference (N : Node_Id) return Traverse_Result is
16044 begin
16045 if Is_Entity_Name (N)
16046 and then Present (Entity (N))
16047 and then Entity (N) = Id
16048 then
16049 Seen := True;
16050 return Abandon;
16051 else
16052 return OK;
16053 end if;
16054 end Is_Reference;
16056 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
16058 -- Start of processing for Referenced
16060 begin
16061 Inspect_Expression (Expr);
16062 return Seen;
16063 end Referenced;
16065 ------------------------------------
16066 -- References_Generic_Formal_Type --
16067 ------------------------------------
16069 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
16071 function Process (N : Node_Id) return Traverse_Result;
16072 -- Process one node in search for generic formal type
16074 -------------
16075 -- Process --
16076 -------------
16078 function Process (N : Node_Id) return Traverse_Result is
16079 begin
16080 if Nkind (N) in N_Has_Entity then
16081 declare
16082 E : constant Entity_Id := Entity (N);
16083 begin
16084 if Present (E) then
16085 if Is_Generic_Type (E) then
16086 return Abandon;
16087 elsif Present (Etype (E))
16088 and then Is_Generic_Type (Etype (E))
16089 then
16090 return Abandon;
16091 end if;
16092 end if;
16093 end;
16094 end if;
16096 return Atree.OK;
16097 end Process;
16099 function Traverse is new Traverse_Func (Process);
16100 -- Traverse tree to look for generic type
16102 begin
16103 if Inside_A_Generic then
16104 return Traverse (N) = Abandon;
16105 else
16106 return False;
16107 end if;
16108 end References_Generic_Formal_Type;
16110 --------------------
16111 -- Remove_Homonym --
16112 --------------------
16114 procedure Remove_Homonym (E : Entity_Id) is
16115 Prev : Entity_Id := Empty;
16116 H : Entity_Id;
16118 begin
16119 if E = Current_Entity (E) then
16120 if Present (Homonym (E)) then
16121 Set_Current_Entity (Homonym (E));
16122 else
16123 Set_Name_Entity_Id (Chars (E), Empty);
16124 end if;
16126 else
16127 H := Current_Entity (E);
16128 while Present (H) and then H /= E loop
16129 Prev := H;
16130 H := Homonym (H);
16131 end loop;
16133 -- If E is not on the homonym chain, nothing to do
16135 if Present (H) then
16136 Set_Homonym (Prev, Homonym (E));
16137 end if;
16138 end if;
16139 end Remove_Homonym;
16141 ---------------------
16142 -- Rep_To_Pos_Flag --
16143 ---------------------
16145 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
16146 begin
16147 return New_Occurrence_Of
16148 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
16149 end Rep_To_Pos_Flag;
16151 --------------------
16152 -- Require_Entity --
16153 --------------------
16155 procedure Require_Entity (N : Node_Id) is
16156 begin
16157 if Is_Entity_Name (N) and then No (Entity (N)) then
16158 if Total_Errors_Detected /= 0 then
16159 Set_Entity (N, Any_Id);
16160 else
16161 raise Program_Error;
16162 end if;
16163 end if;
16164 end Require_Entity;
16166 -------------------------------
16167 -- Requires_State_Refinement --
16168 -------------------------------
16170 function Requires_State_Refinement
16171 (Spec_Id : Entity_Id;
16172 Body_Id : Entity_Id) return Boolean
16174 function Mode_Is_Off (Prag : Node_Id) return Boolean;
16175 -- Given pragma SPARK_Mode, determine whether the mode is Off
16177 -----------------
16178 -- Mode_Is_Off --
16179 -----------------
16181 function Mode_Is_Off (Prag : Node_Id) return Boolean is
16182 Mode : Node_Id;
16184 begin
16185 -- The default SPARK mode is On
16187 if No (Prag) then
16188 return False;
16189 end if;
16191 Mode := Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
16193 -- Then the pragma lacks an argument, the default mode is On
16195 if No (Mode) then
16196 return False;
16197 else
16198 return Chars (Mode) = Name_Off;
16199 end if;
16200 end Mode_Is_Off;
16202 -- Start of processing for Requires_State_Refinement
16204 begin
16205 -- A package that does not define at least one abstract state cannot
16206 -- possibly require refinement.
16208 if No (Abstract_States (Spec_Id)) then
16209 return False;
16211 -- The package instroduces a single null state which does not merit
16212 -- refinement.
16214 elsif Has_Null_Abstract_State (Spec_Id) then
16215 return False;
16217 -- Check whether the package body is subject to pragma SPARK_Mode. If
16218 -- it is and the mode is Off, the package body is considered to be in
16219 -- regular Ada and does not require refinement.
16221 elsif Mode_Is_Off (SPARK_Pragma (Body_Id)) then
16222 return False;
16224 -- The body's SPARK_Mode may be inherited from a similar pragma that
16225 -- appears in the private declarations of the spec. The pragma we are
16226 -- interested appears as the second entry in SPARK_Pragma.
16228 elsif Present (SPARK_Pragma (Spec_Id))
16229 and then Mode_Is_Off (Next_Pragma (SPARK_Pragma (Spec_Id)))
16230 then
16231 return False;
16233 -- The spec defines at least one abstract state and the body has no way
16234 -- of circumventing the refinement.
16236 else
16237 return True;
16238 end if;
16239 end Requires_State_Refinement;
16241 ------------------------------
16242 -- Requires_Transient_Scope --
16243 ------------------------------
16245 -- A transient scope is required when variable-sized temporaries are
16246 -- allocated in the primary or secondary stack, or when finalization
16247 -- actions must be generated before the next instruction.
16249 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
16250 Typ : constant Entity_Id := Underlying_Type (Id);
16252 -- Start of processing for Requires_Transient_Scope
16254 begin
16255 -- This is a private type which is not completed yet. This can only
16256 -- happen in a default expression (of a formal parameter or of a
16257 -- record component). Do not expand transient scope in this case
16259 if No (Typ) then
16260 return False;
16262 -- Do not expand transient scope for non-existent procedure return
16264 elsif Typ = Standard_Void_Type then
16265 return False;
16267 -- Elementary types do not require a transient scope
16269 elsif Is_Elementary_Type (Typ) then
16270 return False;
16272 -- Generally, indefinite subtypes require a transient scope, since the
16273 -- back end cannot generate temporaries, since this is not a valid type
16274 -- for declaring an object. It might be possible to relax this in the
16275 -- future, e.g. by declaring the maximum possible space for the type.
16277 elsif Is_Indefinite_Subtype (Typ) then
16278 return True;
16280 -- Functions returning tagged types may dispatch on result so their
16281 -- returned value is allocated on the secondary stack. Controlled
16282 -- type temporaries need finalization.
16284 elsif Is_Tagged_Type (Typ)
16285 or else Has_Controlled_Component (Typ)
16286 then
16287 return not Is_Value_Type (Typ);
16289 -- Record type
16291 elsif Is_Record_Type (Typ) then
16292 declare
16293 Comp : Entity_Id;
16294 begin
16295 Comp := First_Entity (Typ);
16296 while Present (Comp) loop
16297 if Ekind (Comp) = E_Component
16298 and then Requires_Transient_Scope (Etype (Comp))
16299 then
16300 return True;
16301 else
16302 Next_Entity (Comp);
16303 end if;
16304 end loop;
16305 end;
16307 return False;
16309 -- String literal types never require transient scope
16311 elsif Ekind (Typ) = E_String_Literal_Subtype then
16312 return False;
16314 -- Array type. Note that we already know that this is a constrained
16315 -- array, since unconstrained arrays will fail the indefinite test.
16317 elsif Is_Array_Type (Typ) then
16319 -- If component type requires a transient scope, the array does too
16321 if Requires_Transient_Scope (Component_Type (Typ)) then
16322 return True;
16324 -- Otherwise, we only need a transient scope if the size depends on
16325 -- the value of one or more discriminants.
16327 else
16328 return Size_Depends_On_Discriminant (Typ);
16329 end if;
16331 -- All other cases do not require a transient scope
16333 else
16334 return False;
16335 end if;
16336 end Requires_Transient_Scope;
16338 --------------------------
16339 -- Reset_Analyzed_Flags --
16340 --------------------------
16342 procedure Reset_Analyzed_Flags (N : Node_Id) is
16344 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
16345 -- Function used to reset Analyzed flags in tree. Note that we do
16346 -- not reset Analyzed flags in entities, since there is no need to
16347 -- reanalyze entities, and indeed, it is wrong to do so, since it
16348 -- can result in generating auxiliary stuff more than once.
16350 --------------------
16351 -- Clear_Analyzed --
16352 --------------------
16354 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
16355 begin
16356 if not Has_Extension (N) then
16357 Set_Analyzed (N, False);
16358 end if;
16360 return OK;
16361 end Clear_Analyzed;
16363 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
16365 -- Start of processing for Reset_Analyzed_Flags
16367 begin
16368 Reset_Analyzed (N);
16369 end Reset_Analyzed_Flags;
16371 ------------------------
16372 -- Restore_SPARK_Mode --
16373 ------------------------
16375 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
16376 begin
16377 SPARK_Mode := Mode;
16378 end Restore_SPARK_Mode;
16380 --------------------------------
16381 -- Returns_Unconstrained_Type --
16382 --------------------------------
16384 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
16385 begin
16386 return Ekind (Subp) = E_Function
16387 and then not Is_Scalar_Type (Etype (Subp))
16388 and then not Is_Access_Type (Etype (Subp))
16389 and then not Is_Constrained (Etype (Subp));
16390 end Returns_Unconstrained_Type;
16392 ----------------------------
16393 -- Root_Type_Of_Full_View --
16394 ----------------------------
16396 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
16397 Rtyp : constant Entity_Id := Root_Type (T);
16399 begin
16400 -- The root type of the full view may itself be a private type. Keep
16401 -- looking for the ultimate derivation parent.
16403 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
16404 return Root_Type_Of_Full_View (Full_View (Rtyp));
16405 else
16406 return Rtyp;
16407 end if;
16408 end Root_Type_Of_Full_View;
16410 ---------------------------
16411 -- Safe_To_Capture_Value --
16412 ---------------------------
16414 function Safe_To_Capture_Value
16415 (N : Node_Id;
16416 Ent : Entity_Id;
16417 Cond : Boolean := False) return Boolean
16419 begin
16420 -- The only entities for which we track constant values are variables
16421 -- which are not renamings, constants, out parameters, and in out
16422 -- parameters, so check if we have this case.
16424 -- Note: it may seem odd to track constant values for constants, but in
16425 -- fact this routine is used for other purposes than simply capturing
16426 -- the value. In particular, the setting of Known[_Non]_Null.
16428 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
16429 or else
16430 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
16431 then
16432 null;
16434 -- For conditionals, we also allow loop parameters and all formals,
16435 -- including in parameters.
16437 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
16438 null;
16440 -- For all other cases, not just unsafe, but impossible to capture
16441 -- Current_Value, since the above are the only entities which have
16442 -- Current_Value fields.
16444 else
16445 return False;
16446 end if;
16448 -- Skip if volatile or aliased, since funny things might be going on in
16449 -- these cases which we cannot necessarily track. Also skip any variable
16450 -- for which an address clause is given, or whose address is taken. Also
16451 -- never capture value of library level variables (an attempt to do so
16452 -- can occur in the case of package elaboration code).
16454 if Treat_As_Volatile (Ent)
16455 or else Is_Aliased (Ent)
16456 or else Present (Address_Clause (Ent))
16457 or else Address_Taken (Ent)
16458 or else (Is_Library_Level_Entity (Ent)
16459 and then Ekind (Ent) = E_Variable)
16460 then
16461 return False;
16462 end if;
16464 -- OK, all above conditions are met. We also require that the scope of
16465 -- the reference be the same as the scope of the entity, not counting
16466 -- packages and blocks and loops.
16468 declare
16469 E_Scope : constant Entity_Id := Scope (Ent);
16470 R_Scope : Entity_Id;
16472 begin
16473 R_Scope := Current_Scope;
16474 while R_Scope /= Standard_Standard loop
16475 exit when R_Scope = E_Scope;
16477 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
16478 return False;
16479 else
16480 R_Scope := Scope (R_Scope);
16481 end if;
16482 end loop;
16483 end;
16485 -- We also require that the reference does not appear in a context
16486 -- where it is not sure to be executed (i.e. a conditional context
16487 -- or an exception handler). We skip this if Cond is True, since the
16488 -- capturing of values from conditional tests handles this ok.
16490 if Cond then
16491 return True;
16492 end if;
16494 declare
16495 Desc : Node_Id;
16496 P : Node_Id;
16498 begin
16499 Desc := N;
16501 -- Seems dubious that case expressions are not handled here ???
16503 P := Parent (N);
16504 while Present (P) loop
16505 if Nkind (P) = N_If_Statement
16506 or else Nkind (P) = N_Case_Statement
16507 or else (Nkind (P) in N_Short_Circuit
16508 and then Desc = Right_Opnd (P))
16509 or else (Nkind (P) = N_If_Expression
16510 and then Desc /= First (Expressions (P)))
16511 or else Nkind (P) = N_Exception_Handler
16512 or else Nkind (P) = N_Selective_Accept
16513 or else Nkind (P) = N_Conditional_Entry_Call
16514 or else Nkind (P) = N_Timed_Entry_Call
16515 or else Nkind (P) = N_Asynchronous_Select
16516 then
16517 return False;
16519 else
16520 Desc := P;
16521 P := Parent (P);
16523 -- A special Ada 2012 case: the original node may be part
16524 -- of the else_actions of a conditional expression, in which
16525 -- case it might not have been expanded yet, and appears in
16526 -- a non-syntactic list of actions. In that case it is clearly
16527 -- not safe to save a value.
16529 if No (P)
16530 and then Is_List_Member (Desc)
16531 and then No (Parent (List_Containing (Desc)))
16532 then
16533 return False;
16534 end if;
16535 end if;
16536 end loop;
16537 end;
16539 -- OK, looks safe to set value
16541 return True;
16542 end Safe_To_Capture_Value;
16544 ---------------
16545 -- Same_Name --
16546 ---------------
16548 function Same_Name (N1, N2 : Node_Id) return Boolean is
16549 K1 : constant Node_Kind := Nkind (N1);
16550 K2 : constant Node_Kind := Nkind (N2);
16552 begin
16553 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
16554 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
16555 then
16556 return Chars (N1) = Chars (N2);
16558 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
16559 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
16560 then
16561 return Same_Name (Selector_Name (N1), Selector_Name (N2))
16562 and then Same_Name (Prefix (N1), Prefix (N2));
16564 else
16565 return False;
16566 end if;
16567 end Same_Name;
16569 -----------------
16570 -- Same_Object --
16571 -----------------
16573 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
16574 N1 : constant Node_Id := Original_Node (Node1);
16575 N2 : constant Node_Id := Original_Node (Node2);
16576 -- We do the tests on original nodes, since we are most interested
16577 -- in the original source, not any expansion that got in the way.
16579 K1 : constant Node_Kind := Nkind (N1);
16580 K2 : constant Node_Kind := Nkind (N2);
16582 begin
16583 -- First case, both are entities with same entity
16585 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
16586 declare
16587 EN1 : constant Entity_Id := Entity (N1);
16588 EN2 : constant Entity_Id := Entity (N2);
16589 begin
16590 if Present (EN1) and then Present (EN2)
16591 and then (Ekind_In (EN1, E_Variable, E_Constant)
16592 or else Is_Formal (EN1))
16593 and then EN1 = EN2
16594 then
16595 return True;
16596 end if;
16597 end;
16598 end if;
16600 -- Second case, selected component with same selector, same record
16602 if K1 = N_Selected_Component
16603 and then K2 = N_Selected_Component
16604 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
16605 then
16606 return Same_Object (Prefix (N1), Prefix (N2));
16608 -- Third case, indexed component with same subscripts, same array
16610 elsif K1 = N_Indexed_Component
16611 and then K2 = N_Indexed_Component
16612 and then Same_Object (Prefix (N1), Prefix (N2))
16613 then
16614 declare
16615 E1, E2 : Node_Id;
16616 begin
16617 E1 := First (Expressions (N1));
16618 E2 := First (Expressions (N2));
16619 while Present (E1) loop
16620 if not Same_Value (E1, E2) then
16621 return False;
16622 else
16623 Next (E1);
16624 Next (E2);
16625 end if;
16626 end loop;
16628 return True;
16629 end;
16631 -- Fourth case, slice of same array with same bounds
16633 elsif K1 = N_Slice
16634 and then K2 = N_Slice
16635 and then Nkind (Discrete_Range (N1)) = N_Range
16636 and then Nkind (Discrete_Range (N2)) = N_Range
16637 and then Same_Value (Low_Bound (Discrete_Range (N1)),
16638 Low_Bound (Discrete_Range (N2)))
16639 and then Same_Value (High_Bound (Discrete_Range (N1)),
16640 High_Bound (Discrete_Range (N2)))
16641 then
16642 return Same_Name (Prefix (N1), Prefix (N2));
16644 -- All other cases, not clearly the same object
16646 else
16647 return False;
16648 end if;
16649 end Same_Object;
16651 ---------------
16652 -- Same_Type --
16653 ---------------
16655 function Same_Type (T1, T2 : Entity_Id) return Boolean is
16656 begin
16657 if T1 = T2 then
16658 return True;
16660 elsif not Is_Constrained (T1)
16661 and then not Is_Constrained (T2)
16662 and then Base_Type (T1) = Base_Type (T2)
16663 then
16664 return True;
16666 -- For now don't bother with case of identical constraints, to be
16667 -- fiddled with later on perhaps (this is only used for optimization
16668 -- purposes, so it is not critical to do a best possible job)
16670 else
16671 return False;
16672 end if;
16673 end Same_Type;
16675 ----------------
16676 -- Same_Value --
16677 ----------------
16679 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
16680 begin
16681 if Compile_Time_Known_Value (Node1)
16682 and then Compile_Time_Known_Value (Node2)
16683 and then Expr_Value (Node1) = Expr_Value (Node2)
16684 then
16685 return True;
16686 elsif Same_Object (Node1, Node2) then
16687 return True;
16688 else
16689 return False;
16690 end if;
16691 end Same_Value;
16693 -----------------------------
16694 -- Save_SPARK_Mode_And_Set --
16695 -----------------------------
16697 procedure Save_SPARK_Mode_And_Set
16698 (Context : Entity_Id;
16699 Mode : out SPARK_Mode_Type)
16701 begin
16702 -- Save the current mode in effect
16704 Mode := SPARK_Mode;
16706 -- Do not consider illegal or partially decorated constructs
16708 if Ekind (Context) = E_Void or else Error_Posted (Context) then
16709 null;
16711 elsif Present (SPARK_Pragma (Context)) then
16712 SPARK_Mode := Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Context));
16713 end if;
16714 end Save_SPARK_Mode_And_Set;
16716 -------------------------
16717 -- Scalar_Part_Present --
16718 -------------------------
16720 function Scalar_Part_Present (T : Entity_Id) return Boolean is
16721 C : Entity_Id;
16723 begin
16724 if Is_Scalar_Type (T) then
16725 return True;
16727 elsif Is_Array_Type (T) then
16728 return Scalar_Part_Present (Component_Type (T));
16730 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
16731 C := First_Component_Or_Discriminant (T);
16732 while Present (C) loop
16733 if Scalar_Part_Present (Etype (C)) then
16734 return True;
16735 else
16736 Next_Component_Or_Discriminant (C);
16737 end if;
16738 end loop;
16739 end if;
16741 return False;
16742 end Scalar_Part_Present;
16744 ------------------------
16745 -- Scope_Is_Transient --
16746 ------------------------
16748 function Scope_Is_Transient return Boolean is
16749 begin
16750 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
16751 end Scope_Is_Transient;
16753 ------------------
16754 -- Scope_Within --
16755 ------------------
16757 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
16758 Scop : Entity_Id;
16760 begin
16761 Scop := Scope1;
16762 while Scop /= Standard_Standard loop
16763 Scop := Scope (Scop);
16765 if Scop = Scope2 then
16766 return True;
16767 end if;
16768 end loop;
16770 return False;
16771 end Scope_Within;
16773 --------------------------
16774 -- Scope_Within_Or_Same --
16775 --------------------------
16777 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
16778 Scop : Entity_Id;
16780 begin
16781 Scop := Scope1;
16782 while Scop /= Standard_Standard loop
16783 if Scop = Scope2 then
16784 return True;
16785 else
16786 Scop := Scope (Scop);
16787 end if;
16788 end loop;
16790 return False;
16791 end Scope_Within_Or_Same;
16793 --------------------
16794 -- Set_Convention --
16795 --------------------
16797 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
16798 begin
16799 Basic_Set_Convention (E, Val);
16801 if Is_Type (E)
16802 and then Is_Access_Subprogram_Type (Base_Type (E))
16803 and then Has_Foreign_Convention (E)
16804 then
16805 Set_Can_Use_Internal_Rep (E, False);
16806 end if;
16808 -- If E is an object or component, and the type of E is an anonymous
16809 -- access type with no convention set, then also set the convention of
16810 -- the anonymous access type. We do not do this for anonymous protected
16811 -- types, since protected types always have the default convention.
16813 if Present (Etype (E))
16814 and then (Is_Object (E)
16815 or else Ekind (E) = E_Component
16817 -- Allow E_Void (happens for pragma Convention appearing
16818 -- in the middle of a record applying to a component)
16820 or else Ekind (E) = E_Void)
16821 then
16822 declare
16823 Typ : constant Entity_Id := Etype (E);
16825 begin
16826 if Ekind_In (Typ, E_Anonymous_Access_Type,
16827 E_Anonymous_Access_Subprogram_Type)
16828 and then not Has_Convention_Pragma (Typ)
16829 then
16830 Basic_Set_Convention (Typ, Val);
16831 Set_Has_Convention_Pragma (Typ);
16833 -- And for the access subprogram type, deal similarly with the
16834 -- designated E_Subprogram_Type if it is also internal (which
16835 -- it always is?)
16837 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
16838 declare
16839 Dtype : constant Entity_Id := Designated_Type (Typ);
16840 begin
16841 if Ekind (Dtype) = E_Subprogram_Type
16842 and then Is_Itype (Dtype)
16843 and then not Has_Convention_Pragma (Dtype)
16844 then
16845 Basic_Set_Convention (Dtype, Val);
16846 Set_Has_Convention_Pragma (Dtype);
16847 end if;
16848 end;
16849 end if;
16850 end if;
16851 end;
16852 end if;
16853 end Set_Convention;
16855 ------------------------
16856 -- Set_Current_Entity --
16857 ------------------------
16859 -- The given entity is to be set as the currently visible definition of its
16860 -- associated name (i.e. the Node_Id associated with its name). All we have
16861 -- to do is to get the name from the identifier, and then set the
16862 -- associated Node_Id to point to the given entity.
16864 procedure Set_Current_Entity (E : Entity_Id) is
16865 begin
16866 Set_Name_Entity_Id (Chars (E), E);
16867 end Set_Current_Entity;
16869 ---------------------------
16870 -- Set_Debug_Info_Needed --
16871 ---------------------------
16873 procedure Set_Debug_Info_Needed (T : Entity_Id) is
16875 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
16876 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
16877 -- Used to set debug info in a related node if not set already
16879 --------------------------------------
16880 -- Set_Debug_Info_Needed_If_Not_Set --
16881 --------------------------------------
16883 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
16884 begin
16885 if Present (E) and then not Needs_Debug_Info (E) then
16886 Set_Debug_Info_Needed (E);
16888 -- For a private type, indicate that the full view also needs
16889 -- debug information.
16891 if Is_Type (E)
16892 and then Is_Private_Type (E)
16893 and then Present (Full_View (E))
16894 then
16895 Set_Debug_Info_Needed (Full_View (E));
16896 end if;
16897 end if;
16898 end Set_Debug_Info_Needed_If_Not_Set;
16900 -- Start of processing for Set_Debug_Info_Needed
16902 begin
16903 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
16904 -- indicates that Debug_Info_Needed is never required for the entity.
16905 -- Nothing to do if entity comes from a predefined file. Library files
16906 -- are compiled without debug information, but inlined bodies of these
16907 -- routines may appear in user code, and debug information on them ends
16908 -- up complicating debugging the user code.
16910 if No (T)
16911 or else Debug_Info_Off (T)
16912 then
16913 return;
16915 elsif In_Inlined_Body
16916 and then Is_Predefined_File_Name
16917 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
16918 then
16919 Set_Needs_Debug_Info (T, False);
16920 end if;
16922 -- Set flag in entity itself. Note that we will go through the following
16923 -- circuitry even if the flag is already set on T. That's intentional,
16924 -- it makes sure that the flag will be set in subsidiary entities.
16926 Set_Needs_Debug_Info (T);
16928 -- Set flag on subsidiary entities if not set already
16930 if Is_Object (T) then
16931 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
16933 elsif Is_Type (T) then
16934 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
16936 if Is_Record_Type (T) then
16937 declare
16938 Ent : Entity_Id := First_Entity (T);
16939 begin
16940 while Present (Ent) loop
16941 Set_Debug_Info_Needed_If_Not_Set (Ent);
16942 Next_Entity (Ent);
16943 end loop;
16944 end;
16946 -- For a class wide subtype, we also need debug information
16947 -- for the equivalent type.
16949 if Ekind (T) = E_Class_Wide_Subtype then
16950 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
16951 end if;
16953 elsif Is_Array_Type (T) then
16954 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
16956 declare
16957 Indx : Node_Id := First_Index (T);
16958 begin
16959 while Present (Indx) loop
16960 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
16961 Indx := Next_Index (Indx);
16962 end loop;
16963 end;
16965 -- For a packed array type, we also need debug information for
16966 -- the type used to represent the packed array. Conversely, we
16967 -- also need it for the former if we need it for the latter.
16969 if Is_Packed (T) then
16970 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
16971 end if;
16973 if Is_Packed_Array_Impl_Type (T) then
16974 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
16975 end if;
16977 elsif Is_Access_Type (T) then
16978 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
16980 elsif Is_Private_Type (T) then
16981 Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
16983 elsif Is_Protected_Type (T) then
16984 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
16986 elsif Is_Scalar_Type (T) then
16988 -- If the subrange bounds are materialized by dedicated constant
16989 -- objects, also include them in the debug info to make sure the
16990 -- debugger can properly use them.
16992 if Present (Scalar_Range (T))
16993 and then Nkind (Scalar_Range (T)) = N_Range
16994 then
16995 declare
16996 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
16997 High_Bnd : constant Node_Id := Type_High_Bound (T);
16999 begin
17000 if Is_Entity_Name (Low_Bnd) then
17001 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
17002 end if;
17004 if Is_Entity_Name (High_Bnd) then
17005 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
17006 end if;
17007 end;
17008 end if;
17009 end if;
17010 end if;
17011 end Set_Debug_Info_Needed;
17013 ----------------------------
17014 -- Set_Entity_With_Checks --
17015 ----------------------------
17017 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
17018 Val_Actual : Entity_Id;
17019 Nod : Node_Id;
17020 Post_Node : Node_Id;
17022 begin
17023 -- Unconditionally set the entity
17025 Set_Entity (N, Val);
17027 -- The node to post on is the selector in the case of an expanded name,
17028 -- and otherwise the node itself.
17030 if Nkind (N) = N_Expanded_Name then
17031 Post_Node := Selector_Name (N);
17032 else
17033 Post_Node := N;
17034 end if;
17036 -- Check for violation of No_Fixed_IO
17038 if Restriction_Check_Required (No_Fixed_IO)
17039 and then
17040 ((RTU_Loaded (Ada_Text_IO)
17041 and then (Is_RTE (Val, RE_Decimal_IO)
17042 or else
17043 Is_RTE (Val, RE_Fixed_IO)))
17045 or else
17046 (RTU_Loaded (Ada_Wide_Text_IO)
17047 and then (Is_RTE (Val, RO_WT_Decimal_IO)
17048 or else
17049 Is_RTE (Val, RO_WT_Fixed_IO)))
17051 or else
17052 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
17053 and then (Is_RTE (Val, RO_WW_Decimal_IO)
17054 or else
17055 Is_RTE (Val, RO_WW_Fixed_IO))))
17057 -- A special extra check, don't complain about a reference from within
17058 -- the Ada.Interrupts package itself!
17060 and then not In_Same_Extended_Unit (N, Val)
17061 then
17062 Check_Restriction (No_Fixed_IO, Post_Node);
17063 end if;
17065 -- Remaining checks are only done on source nodes. Note that we test
17066 -- for violation of No_Fixed_IO even on non-source nodes, because the
17067 -- cases for checking violations of this restriction are instantiations
17068 -- where the reference in the instance has Comes_From_Source False.
17070 if not Comes_From_Source (N) then
17071 return;
17072 end if;
17074 -- Check for violation of No_Abort_Statements, which is triggered by
17075 -- call to Ada.Task_Identification.Abort_Task.
17077 if Restriction_Check_Required (No_Abort_Statements)
17078 and then (Is_RTE (Val, RE_Abort_Task))
17080 -- A special extra check, don't complain about a reference from within
17081 -- the Ada.Task_Identification package itself!
17083 and then not In_Same_Extended_Unit (N, Val)
17084 then
17085 Check_Restriction (No_Abort_Statements, Post_Node);
17086 end if;
17088 if Val = Standard_Long_Long_Integer then
17089 Check_Restriction (No_Long_Long_Integers, Post_Node);
17090 end if;
17092 -- Check for violation of No_Dynamic_Attachment
17094 if Restriction_Check_Required (No_Dynamic_Attachment)
17095 and then RTU_Loaded (Ada_Interrupts)
17096 and then (Is_RTE (Val, RE_Is_Reserved) or else
17097 Is_RTE (Val, RE_Is_Attached) or else
17098 Is_RTE (Val, RE_Current_Handler) or else
17099 Is_RTE (Val, RE_Attach_Handler) or else
17100 Is_RTE (Val, RE_Exchange_Handler) or else
17101 Is_RTE (Val, RE_Detach_Handler) or else
17102 Is_RTE (Val, RE_Reference))
17104 -- A special extra check, don't complain about a reference from within
17105 -- the Ada.Interrupts package itself!
17107 and then not In_Same_Extended_Unit (N, Val)
17108 then
17109 Check_Restriction (No_Dynamic_Attachment, Post_Node);
17110 end if;
17112 -- Check for No_Implementation_Identifiers
17114 if Restriction_Check_Required (No_Implementation_Identifiers) then
17116 -- We have an implementation defined entity if it is marked as
17117 -- implementation defined, or is defined in a package marked as
17118 -- implementation defined. However, library packages themselves
17119 -- are excluded (we don't want to flag Interfaces itself, just
17120 -- the entities within it).
17122 if (Is_Implementation_Defined (Val)
17123 or else
17124 (Present (Scope (Val))
17125 and then Is_Implementation_Defined (Scope (Val))))
17126 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
17127 and then Is_Library_Level_Entity (Val))
17128 then
17129 Check_Restriction (No_Implementation_Identifiers, Post_Node);
17130 end if;
17131 end if;
17133 -- Do the style check
17135 if Style_Check
17136 and then not Suppress_Style_Checks (Val)
17137 and then not In_Instance
17138 then
17139 if Nkind (N) = N_Identifier then
17140 Nod := N;
17141 elsif Nkind (N) = N_Expanded_Name then
17142 Nod := Selector_Name (N);
17143 else
17144 return;
17145 end if;
17147 -- A special situation arises for derived operations, where we want
17148 -- to do the check against the parent (since the Sloc of the derived
17149 -- operation points to the derived type declaration itself).
17151 Val_Actual := Val;
17152 while not Comes_From_Source (Val_Actual)
17153 and then Nkind (Val_Actual) in N_Entity
17154 and then (Ekind (Val_Actual) = E_Enumeration_Literal
17155 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
17156 and then Present (Alias (Val_Actual))
17157 loop
17158 Val_Actual := Alias (Val_Actual);
17159 end loop;
17161 -- Renaming declarations for generic actuals do not come from source,
17162 -- and have a different name from that of the entity they rename, so
17163 -- there is no style check to perform here.
17165 if Chars (Nod) = Chars (Val_Actual) then
17166 Style.Check_Identifier (Nod, Val_Actual);
17167 end if;
17168 end if;
17170 Set_Entity (N, Val);
17171 end Set_Entity_With_Checks;
17173 ------------------------
17174 -- Set_Name_Entity_Id --
17175 ------------------------
17177 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
17178 begin
17179 Set_Name_Table_Int (Id, Int (Val));
17180 end Set_Name_Entity_Id;
17182 ---------------------
17183 -- Set_Next_Actual --
17184 ---------------------
17186 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
17187 begin
17188 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
17189 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
17190 end if;
17191 end Set_Next_Actual;
17193 ----------------------------------
17194 -- Set_Optimize_Alignment_Flags --
17195 ----------------------------------
17197 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
17198 begin
17199 if Optimize_Alignment = 'S' then
17200 Set_Optimize_Alignment_Space (E);
17201 elsif Optimize_Alignment = 'T' then
17202 Set_Optimize_Alignment_Time (E);
17203 end if;
17204 end Set_Optimize_Alignment_Flags;
17206 -----------------------
17207 -- Set_Public_Status --
17208 -----------------------
17210 procedure Set_Public_Status (Id : Entity_Id) is
17211 S : constant Entity_Id := Current_Scope;
17213 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
17214 -- Determines if E is defined within handled statement sequence or
17215 -- an if statement, returns True if so, False otherwise.
17217 ----------------------
17218 -- Within_HSS_Or_If --
17219 ----------------------
17221 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
17222 N : Node_Id;
17223 begin
17224 N := Declaration_Node (E);
17225 loop
17226 N := Parent (N);
17228 if No (N) then
17229 return False;
17231 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
17232 N_If_Statement)
17233 then
17234 return True;
17235 end if;
17236 end loop;
17237 end Within_HSS_Or_If;
17239 -- Start of processing for Set_Public_Status
17241 begin
17242 -- Everything in the scope of Standard is public
17244 if S = Standard_Standard then
17245 Set_Is_Public (Id);
17247 -- Entity is definitely not public if enclosing scope is not public
17249 elsif not Is_Public (S) then
17250 return;
17252 -- An object or function declaration that occurs in a handled sequence
17253 -- of statements or within an if statement is the declaration for a
17254 -- temporary object or local subprogram generated by the expander. It
17255 -- never needs to be made public and furthermore, making it public can
17256 -- cause back end problems.
17258 elsif Nkind_In (Parent (Id), N_Object_Declaration,
17259 N_Function_Specification)
17260 and then Within_HSS_Or_If (Id)
17261 then
17262 return;
17264 -- Entities in public packages or records are public
17266 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
17267 Set_Is_Public (Id);
17269 -- The bounds of an entry family declaration can generate object
17270 -- declarations that are visible to the back-end, e.g. in the
17271 -- the declaration of a composite type that contains tasks.
17273 elsif Is_Concurrent_Type (S)
17274 and then not Has_Completion (S)
17275 and then Nkind (Parent (Id)) = N_Object_Declaration
17276 then
17277 Set_Is_Public (Id);
17278 end if;
17279 end Set_Public_Status;
17281 -----------------------------
17282 -- Set_Referenced_Modified --
17283 -----------------------------
17285 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
17286 Pref : Node_Id;
17288 begin
17289 -- Deal with indexed or selected component where prefix is modified
17291 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
17292 Pref := Prefix (N);
17294 -- If prefix is access type, then it is the designated object that is
17295 -- being modified, which means we have no entity to set the flag on.
17297 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
17298 return;
17300 -- Otherwise chase the prefix
17302 else
17303 Set_Referenced_Modified (Pref, Out_Param);
17304 end if;
17306 -- Otherwise see if we have an entity name (only other case to process)
17308 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
17309 Set_Referenced_As_LHS (Entity (N), not Out_Param);
17310 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
17311 end if;
17312 end Set_Referenced_Modified;
17314 ----------------------------
17315 -- Set_Scope_Is_Transient --
17316 ----------------------------
17318 procedure Set_Scope_Is_Transient (V : Boolean := True) is
17319 begin
17320 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
17321 end Set_Scope_Is_Transient;
17323 -------------------
17324 -- Set_Size_Info --
17325 -------------------
17327 procedure Set_Size_Info (T1, T2 : Entity_Id) is
17328 begin
17329 -- We copy Esize, but not RM_Size, since in general RM_Size is
17330 -- subtype specific and does not get inherited by all subtypes.
17332 Set_Esize (T1, Esize (T2));
17333 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
17335 if Is_Discrete_Or_Fixed_Point_Type (T1)
17336 and then
17337 Is_Discrete_Or_Fixed_Point_Type (T2)
17338 then
17339 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
17340 end if;
17342 Set_Alignment (T1, Alignment (T2));
17343 end Set_Size_Info;
17345 --------------------
17346 -- Static_Boolean --
17347 --------------------
17349 function Static_Boolean (N : Node_Id) return Uint is
17350 begin
17351 Analyze_And_Resolve (N, Standard_Boolean);
17353 if N = Error
17354 or else Error_Posted (N)
17355 or else Etype (N) = Any_Type
17356 then
17357 return No_Uint;
17358 end if;
17360 if Is_OK_Static_Expression (N) then
17361 if not Raises_Constraint_Error (N) then
17362 return Expr_Value (N);
17363 else
17364 return No_Uint;
17365 end if;
17367 elsif Etype (N) = Any_Type then
17368 return No_Uint;
17370 else
17371 Flag_Non_Static_Expr
17372 ("static boolean expression required here", N);
17373 return No_Uint;
17374 end if;
17375 end Static_Boolean;
17377 --------------------
17378 -- Static_Integer --
17379 --------------------
17381 function Static_Integer (N : Node_Id) return Uint is
17382 begin
17383 Analyze_And_Resolve (N, Any_Integer);
17385 if N = Error
17386 or else Error_Posted (N)
17387 or else Etype (N) = Any_Type
17388 then
17389 return No_Uint;
17390 end if;
17392 if Is_OK_Static_Expression (N) then
17393 if not Raises_Constraint_Error (N) then
17394 return Expr_Value (N);
17395 else
17396 return No_Uint;
17397 end if;
17399 elsif Etype (N) = Any_Type then
17400 return No_Uint;
17402 else
17403 Flag_Non_Static_Expr
17404 ("static integer expression required here", N);
17405 return No_Uint;
17406 end if;
17407 end Static_Integer;
17409 --------------------------
17410 -- Statically_Different --
17411 --------------------------
17413 function Statically_Different (E1, E2 : Node_Id) return Boolean is
17414 R1 : constant Node_Id := Get_Referenced_Object (E1);
17415 R2 : constant Node_Id := Get_Referenced_Object (E2);
17416 begin
17417 return Is_Entity_Name (R1)
17418 and then Is_Entity_Name (R2)
17419 and then Entity (R1) /= Entity (R2)
17420 and then not Is_Formal (Entity (R1))
17421 and then not Is_Formal (Entity (R2));
17422 end Statically_Different;
17424 --------------------------------------
17425 -- Subject_To_Loop_Entry_Attributes --
17426 --------------------------------------
17428 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
17429 Stmt : Node_Id;
17431 begin
17432 Stmt := N;
17434 -- The expansion mechanism transform a loop subject to at least one
17435 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
17436 -- the conditional part.
17438 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
17439 and then Nkind (Original_Node (N)) = N_Loop_Statement
17440 then
17441 Stmt := Original_Node (N);
17442 end if;
17444 return
17445 Nkind (Stmt) = N_Loop_Statement
17446 and then Present (Identifier (Stmt))
17447 and then Present (Entity (Identifier (Stmt)))
17448 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
17449 end Subject_To_Loop_Entry_Attributes;
17451 -----------------------------
17452 -- Subprogram_Access_Level --
17453 -----------------------------
17455 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
17456 begin
17457 if Present (Alias (Subp)) then
17458 return Subprogram_Access_Level (Alias (Subp));
17459 else
17460 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
17461 end if;
17462 end Subprogram_Access_Level;
17464 -------------------------------
17465 -- Support_Atomic_Primitives --
17466 -------------------------------
17468 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
17469 Size : Int;
17471 begin
17472 -- Verify the alignment of Typ is known
17474 if not Known_Alignment (Typ) then
17475 return False;
17476 end if;
17478 if Known_Static_Esize (Typ) then
17479 Size := UI_To_Int (Esize (Typ));
17481 -- If the Esize (Object_Size) is unknown at compile time, look at the
17482 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
17484 elsif Known_Static_RM_Size (Typ) then
17485 Size := UI_To_Int (RM_Size (Typ));
17487 -- Otherwise, the size is considered to be unknown.
17489 else
17490 return False;
17491 end if;
17493 -- Check that the size of the component is 8, 16, 32 or 64 bits and that
17494 -- Typ is properly aligned.
17496 case Size is
17497 when 8 | 16 | 32 | 64 =>
17498 return Size = UI_To_Int (Alignment (Typ)) * 8;
17499 when others =>
17500 return False;
17501 end case;
17502 end Support_Atomic_Primitives;
17504 -----------------
17505 -- Trace_Scope --
17506 -----------------
17508 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
17509 begin
17510 if Debug_Flag_W then
17511 for J in 0 .. Scope_Stack.Last loop
17512 Write_Str (" ");
17513 end loop;
17515 Write_Str (Msg);
17516 Write_Name (Chars (E));
17517 Write_Str (" from ");
17518 Write_Location (Sloc (N));
17519 Write_Eol;
17520 end if;
17521 end Trace_Scope;
17523 -----------------------
17524 -- Transfer_Entities --
17525 -----------------------
17527 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
17528 procedure Set_Public_Status_Of (Id : Entity_Id);
17529 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
17530 -- Set_Public_Status. If successfull and Id denotes a record type, set
17531 -- the Is_Public attribute of its fields.
17533 --------------------------
17534 -- Set_Public_Status_Of --
17535 --------------------------
17537 procedure Set_Public_Status_Of (Id : Entity_Id) is
17538 Field : Entity_Id;
17540 begin
17541 if not Is_Public (Id) then
17542 Set_Public_Status (Id);
17544 -- When the input entity is a public record type, ensure that all
17545 -- its internal fields are also exposed to the linker. The fields
17546 -- of a class-wide type are never made public.
17548 if Is_Public (Id)
17549 and then Is_Record_Type (Id)
17550 and then not Is_Class_Wide_Type (Id)
17551 then
17552 Field := First_Entity (Id);
17553 while Present (Field) loop
17554 Set_Is_Public (Field);
17555 Next_Entity (Field);
17556 end loop;
17557 end if;
17558 end if;
17559 end Set_Public_Status_Of;
17561 -- Local variables
17563 Full_Id : Entity_Id;
17564 Id : Entity_Id;
17566 -- Start of processing for Transfer_Entities
17568 begin
17569 Id := First_Entity (From);
17571 if Present (Id) then
17573 -- Merge the entity chain of the source scope with that of the
17574 -- destination scope.
17576 if Present (Last_Entity (To)) then
17577 Set_Next_Entity (Last_Entity (To), Id);
17578 else
17579 Set_First_Entity (To, Id);
17580 end if;
17582 Set_Last_Entity (To, Last_Entity (From));
17584 -- Inspect the entities of the source scope and update their Scope
17585 -- attribute.
17587 while Present (Id) loop
17588 Set_Scope (Id, To);
17589 Set_Public_Status_Of (Id);
17591 -- Handle an internally generated full view for a private type
17593 if Is_Private_Type (Id)
17594 and then Present (Full_View (Id))
17595 and then Is_Itype (Full_View (Id))
17596 then
17597 Full_Id := Full_View (Id);
17599 Set_Scope (Full_Id, To);
17600 Set_Public_Status_Of (Full_Id);
17601 end if;
17603 Next_Entity (Id);
17604 end loop;
17606 Set_First_Entity (From, Empty);
17607 Set_Last_Entity (From, Empty);
17608 end if;
17609 end Transfer_Entities;
17611 -----------------------
17612 -- Type_Access_Level --
17613 -----------------------
17615 function Type_Access_Level (Typ : Entity_Id) return Uint is
17616 Btyp : Entity_Id;
17618 begin
17619 Btyp := Base_Type (Typ);
17621 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
17622 -- simply use the level where the type is declared. This is true for
17623 -- stand-alone object declarations, and for anonymous access types
17624 -- associated with components the level is the same as that of the
17625 -- enclosing composite type. However, special treatment is needed for
17626 -- the cases of access parameters, return objects of an anonymous access
17627 -- type, and, in Ada 95, access discriminants of limited types.
17629 if Is_Access_Type (Btyp) then
17630 if Ekind (Btyp) = E_Anonymous_Access_Type then
17632 -- If the type is a nonlocal anonymous access type (such as for
17633 -- an access parameter) we treat it as being declared at the
17634 -- library level to ensure that names such as X.all'access don't
17635 -- fail static accessibility checks.
17637 if not Is_Local_Anonymous_Access (Typ) then
17638 return Scope_Depth (Standard_Standard);
17640 -- If this is a return object, the accessibility level is that of
17641 -- the result subtype of the enclosing function. The test here is
17642 -- little complicated, because we have to account for extended
17643 -- return statements that have been rewritten as blocks, in which
17644 -- case we have to find and the Is_Return_Object attribute of the
17645 -- itype's associated object. It would be nice to find a way to
17646 -- simplify this test, but it doesn't seem worthwhile to add a new
17647 -- flag just for purposes of this test. ???
17649 elsif Ekind (Scope (Btyp)) = E_Return_Statement
17650 or else
17651 (Is_Itype (Btyp)
17652 and then Nkind (Associated_Node_For_Itype (Btyp)) =
17653 N_Object_Declaration
17654 and then Is_Return_Object
17655 (Defining_Identifier
17656 (Associated_Node_For_Itype (Btyp))))
17657 then
17658 declare
17659 Scop : Entity_Id;
17661 begin
17662 Scop := Scope (Scope (Btyp));
17663 while Present (Scop) loop
17664 exit when Ekind (Scop) = E_Function;
17665 Scop := Scope (Scop);
17666 end loop;
17668 -- Treat the return object's type as having the level of the
17669 -- function's result subtype (as per RM05-6.5(5.3/2)).
17671 return Type_Access_Level (Etype (Scop));
17672 end;
17673 end if;
17674 end if;
17676 Btyp := Root_Type (Btyp);
17678 -- The accessibility level of anonymous access types associated with
17679 -- discriminants is that of the current instance of the type, and
17680 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
17682 -- AI-402: access discriminants have accessibility based on the
17683 -- object rather than the type in Ada 2005, so the above paragraph
17684 -- doesn't apply.
17686 -- ??? Needs completion with rules from AI-416
17688 if Ada_Version <= Ada_95
17689 and then Ekind (Typ) = E_Anonymous_Access_Type
17690 and then Present (Associated_Node_For_Itype (Typ))
17691 and then Nkind (Associated_Node_For_Itype (Typ)) =
17692 N_Discriminant_Specification
17693 then
17694 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
17695 end if;
17696 end if;
17698 -- Return library level for a generic formal type. This is done because
17699 -- RM(10.3.2) says that "The statically deeper relationship does not
17700 -- apply to ... a descendant of a generic formal type". Rather than
17701 -- checking at each point where a static accessibility check is
17702 -- performed to see if we are dealing with a formal type, this rule is
17703 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
17704 -- return extreme values for a formal type; Deepest_Type_Access_Level
17705 -- returns Int'Last. By calling the appropriate function from among the
17706 -- two, we ensure that the static accessibility check will pass if we
17707 -- happen to run into a formal type. More specifically, we should call
17708 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
17709 -- call occurs as part of a static accessibility check and the error
17710 -- case is the case where the type's level is too shallow (as opposed
17711 -- to too deep).
17713 if Is_Generic_Type (Root_Type (Btyp)) then
17714 return Scope_Depth (Standard_Standard);
17715 end if;
17717 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
17718 end Type_Access_Level;
17720 ------------------------------------
17721 -- Type_Without_Stream_Operation --
17722 ------------------------------------
17724 function Type_Without_Stream_Operation
17725 (T : Entity_Id;
17726 Op : TSS_Name_Type := TSS_Null) return Entity_Id
17728 BT : constant Entity_Id := Base_Type (T);
17729 Op_Missing : Boolean;
17731 begin
17732 if not Restriction_Active (No_Default_Stream_Attributes) then
17733 return Empty;
17734 end if;
17736 if Is_Elementary_Type (T) then
17737 if Op = TSS_Null then
17738 Op_Missing :=
17739 No (TSS (BT, TSS_Stream_Read))
17740 or else No (TSS (BT, TSS_Stream_Write));
17742 else
17743 Op_Missing := No (TSS (BT, Op));
17744 end if;
17746 if Op_Missing then
17747 return T;
17748 else
17749 return Empty;
17750 end if;
17752 elsif Is_Array_Type (T) then
17753 return Type_Without_Stream_Operation (Component_Type (T), Op);
17755 elsif Is_Record_Type (T) then
17756 declare
17757 Comp : Entity_Id;
17758 C_Typ : Entity_Id;
17760 begin
17761 Comp := First_Component (T);
17762 while Present (Comp) loop
17763 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
17765 if Present (C_Typ) then
17766 return C_Typ;
17767 end if;
17769 Next_Component (Comp);
17770 end loop;
17772 return Empty;
17773 end;
17775 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
17776 return Type_Without_Stream_Operation (Full_View (T), Op);
17777 else
17778 return Empty;
17779 end if;
17780 end Type_Without_Stream_Operation;
17782 ----------------------------
17783 -- Unique_Defining_Entity --
17784 ----------------------------
17786 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
17787 begin
17788 return Unique_Entity (Defining_Entity (N));
17789 end Unique_Defining_Entity;
17791 -------------------
17792 -- Unique_Entity --
17793 -------------------
17795 function Unique_Entity (E : Entity_Id) return Entity_Id is
17796 U : Entity_Id := E;
17797 P : Node_Id;
17799 begin
17800 case Ekind (E) is
17801 when E_Constant =>
17802 if Present (Full_View (E)) then
17803 U := Full_View (E);
17804 end if;
17806 when Type_Kind =>
17807 if Present (Full_View (E)) then
17808 U := Full_View (E);
17809 end if;
17811 when E_Package_Body =>
17812 P := Parent (E);
17814 if Nkind (P) = N_Defining_Program_Unit_Name then
17815 P := Parent (P);
17816 end if;
17818 U := Corresponding_Spec (P);
17820 when E_Subprogram_Body =>
17821 P := Parent (E);
17823 if Nkind (P) = N_Defining_Program_Unit_Name then
17824 P := Parent (P);
17825 end if;
17827 P := Parent (P);
17829 if Nkind (P) = N_Subprogram_Body_Stub then
17830 if Present (Library_Unit (P)) then
17832 -- Get to the function or procedure (generic) entity through
17833 -- the body entity.
17835 U :=
17836 Unique_Entity (Defining_Entity (Get_Body_From_Stub (P)));
17837 end if;
17838 else
17839 U := Corresponding_Spec (P);
17840 end if;
17842 when Formal_Kind =>
17843 if Present (Spec_Entity (E)) then
17844 U := Spec_Entity (E);
17845 end if;
17847 when others =>
17848 null;
17849 end case;
17851 return U;
17852 end Unique_Entity;
17854 -----------------
17855 -- Unique_Name --
17856 -----------------
17858 function Unique_Name (E : Entity_Id) return String is
17860 -- Names of E_Subprogram_Body or E_Package_Body entities are not
17861 -- reliable, as they may not include the overloading suffix. Instead,
17862 -- when looking for the name of E or one of its enclosing scope, we get
17863 -- the name of the corresponding Unique_Entity.
17865 function Get_Scoped_Name (E : Entity_Id) return String;
17866 -- Return the name of E prefixed by all the names of the scopes to which
17867 -- E belongs, except for Standard.
17869 ---------------------
17870 -- Get_Scoped_Name --
17871 ---------------------
17873 function Get_Scoped_Name (E : Entity_Id) return String is
17874 Name : constant String := Get_Name_String (Chars (E));
17875 begin
17876 if Has_Fully_Qualified_Name (E)
17877 or else Scope (E) = Standard_Standard
17878 then
17879 return Name;
17880 else
17881 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
17882 end if;
17883 end Get_Scoped_Name;
17885 -- Start of processing for Unique_Name
17887 begin
17888 if E = Standard_Standard then
17889 return Get_Name_String (Name_Standard);
17891 elsif Scope (E) = Standard_Standard
17892 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
17893 then
17894 return Get_Name_String (Name_Standard) & "__" &
17895 Get_Name_String (Chars (E));
17897 elsif Ekind (E) = E_Enumeration_Literal then
17898 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
17900 else
17901 return Get_Scoped_Name (Unique_Entity (E));
17902 end if;
17903 end Unique_Name;
17905 ---------------------
17906 -- Unit_Is_Visible --
17907 ---------------------
17909 function Unit_Is_Visible (U : Entity_Id) return Boolean is
17910 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
17911 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
17913 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
17914 -- For a child unit, check whether unit appears in a with_clause
17915 -- of a parent.
17917 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
17918 -- Scan the context clause of one compilation unit looking for a
17919 -- with_clause for the unit in question.
17921 ----------------------------
17922 -- Unit_In_Parent_Context --
17923 ----------------------------
17925 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
17926 begin
17927 if Unit_In_Context (Par_Unit) then
17928 return True;
17930 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
17931 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
17933 else
17934 return False;
17935 end if;
17936 end Unit_In_Parent_Context;
17938 ---------------------
17939 -- Unit_In_Context --
17940 ---------------------
17942 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
17943 Clause : Node_Id;
17945 begin
17946 Clause := First (Context_Items (Comp_Unit));
17947 while Present (Clause) loop
17948 if Nkind (Clause) = N_With_Clause then
17949 if Library_Unit (Clause) = U then
17950 return True;
17952 -- The with_clause may denote a renaming of the unit we are
17953 -- looking for, eg. Text_IO which renames Ada.Text_IO.
17955 elsif
17956 Renamed_Entity (Entity (Name (Clause))) =
17957 Defining_Entity (Unit (U))
17958 then
17959 return True;
17960 end if;
17961 end if;
17963 Next (Clause);
17964 end loop;
17966 return False;
17967 end Unit_In_Context;
17969 -- Start of processing for Unit_Is_Visible
17971 begin
17972 -- The currrent unit is directly visible
17974 if Curr = U then
17975 return True;
17977 elsif Unit_In_Context (Curr) then
17978 return True;
17980 -- If the current unit is a body, check the context of the spec
17982 elsif Nkind (Unit (Curr)) = N_Package_Body
17983 or else
17984 (Nkind (Unit (Curr)) = N_Subprogram_Body
17985 and then not Acts_As_Spec (Unit (Curr)))
17986 then
17987 if Unit_In_Context (Library_Unit (Curr)) then
17988 return True;
17989 end if;
17990 end if;
17992 -- If the spec is a child unit, examine the parents
17994 if Is_Child_Unit (Curr_Entity) then
17995 if Nkind (Unit (Curr)) in N_Unit_Body then
17996 return
17997 Unit_In_Parent_Context
17998 (Parent_Spec (Unit (Library_Unit (Curr))));
17999 else
18000 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
18001 end if;
18003 else
18004 return False;
18005 end if;
18006 end Unit_Is_Visible;
18008 ------------------------------
18009 -- Universal_Interpretation --
18010 ------------------------------
18012 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
18013 Index : Interp_Index;
18014 It : Interp;
18016 begin
18017 -- The argument may be a formal parameter of an operator or subprogram
18018 -- with multiple interpretations, or else an expression for an actual.
18020 if Nkind (Opnd) = N_Defining_Identifier
18021 or else not Is_Overloaded (Opnd)
18022 then
18023 if Etype (Opnd) = Universal_Integer
18024 or else Etype (Opnd) = Universal_Real
18025 then
18026 return Etype (Opnd);
18027 else
18028 return Empty;
18029 end if;
18031 else
18032 Get_First_Interp (Opnd, Index, It);
18033 while Present (It.Typ) loop
18034 if It.Typ = Universal_Integer
18035 or else It.Typ = Universal_Real
18036 then
18037 return It.Typ;
18038 end if;
18040 Get_Next_Interp (Index, It);
18041 end loop;
18043 return Empty;
18044 end if;
18045 end Universal_Interpretation;
18047 ---------------
18048 -- Unqualify --
18049 ---------------
18051 function Unqualify (Expr : Node_Id) return Node_Id is
18052 begin
18053 -- Recurse to handle unlikely case of multiple levels of qualification
18055 if Nkind (Expr) = N_Qualified_Expression then
18056 return Unqualify (Expression (Expr));
18058 -- Normal case, not a qualified expression
18060 else
18061 return Expr;
18062 end if;
18063 end Unqualify;
18065 -----------------------
18066 -- Visible_Ancestors --
18067 -----------------------
18069 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
18070 List_1 : Elist_Id;
18071 List_2 : Elist_Id;
18072 Elmt : Elmt_Id;
18074 begin
18075 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
18077 -- Collect all the parents and progenitors of Typ. If the full-view of
18078 -- private parents and progenitors is available then it is used to
18079 -- generate the list of visible ancestors; otherwise their partial
18080 -- view is added to the resulting list.
18082 Collect_Parents
18083 (T => Typ,
18084 List => List_1,
18085 Use_Full_View => True);
18087 Collect_Interfaces
18088 (T => Typ,
18089 Ifaces_List => List_2,
18090 Exclude_Parents => True,
18091 Use_Full_View => True);
18093 -- Join the two lists. Avoid duplications because an interface may
18094 -- simultaneously be parent and progenitor of a type.
18096 Elmt := First_Elmt (List_2);
18097 while Present (Elmt) loop
18098 Append_Unique_Elmt (Node (Elmt), List_1);
18099 Next_Elmt (Elmt);
18100 end loop;
18102 return List_1;
18103 end Visible_Ancestors;
18105 ----------------------
18106 -- Within_Init_Proc --
18107 ----------------------
18109 function Within_Init_Proc return Boolean is
18110 S : Entity_Id;
18112 begin
18113 S := Current_Scope;
18114 while not Is_Overloadable (S) loop
18115 if S = Standard_Standard then
18116 return False;
18117 else
18118 S := Scope (S);
18119 end if;
18120 end loop;
18122 return Is_Init_Proc (S);
18123 end Within_Init_Proc;
18125 ------------------
18126 -- Within_Scope --
18127 ------------------
18129 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
18130 SE : Entity_Id;
18131 begin
18132 SE := Scope (E);
18133 loop
18134 if SE = S then
18135 return True;
18136 elsif SE = Standard_Standard then
18137 return False;
18138 else
18139 SE := Scope (SE);
18140 end if;
18141 end loop;
18142 end Within_Scope;
18144 ----------------
18145 -- Wrong_Type --
18146 ----------------
18148 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
18149 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
18150 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
18152 Matching_Field : Entity_Id;
18153 -- Entity to give a more precise suggestion on how to write a one-
18154 -- element positional aggregate.
18156 function Has_One_Matching_Field return Boolean;
18157 -- Determines if Expec_Type is a record type with a single component or
18158 -- discriminant whose type matches the found type or is one dimensional
18159 -- array whose component type matches the found type. In the case of
18160 -- one discriminant, we ignore the variant parts. That's not accurate,
18161 -- but good enough for the warning.
18163 ----------------------------
18164 -- Has_One_Matching_Field --
18165 ----------------------------
18167 function Has_One_Matching_Field return Boolean is
18168 E : Entity_Id;
18170 begin
18171 Matching_Field := Empty;
18173 if Is_Array_Type (Expec_Type)
18174 and then Number_Dimensions (Expec_Type) = 1
18175 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
18176 then
18177 -- Use type name if available. This excludes multidimensional
18178 -- arrays and anonymous arrays.
18180 if Comes_From_Source (Expec_Type) then
18181 Matching_Field := Expec_Type;
18183 -- For an assignment, use name of target
18185 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
18186 and then Is_Entity_Name (Name (Parent (Expr)))
18187 then
18188 Matching_Field := Entity (Name (Parent (Expr)));
18189 end if;
18191 return True;
18193 elsif not Is_Record_Type (Expec_Type) then
18194 return False;
18196 else
18197 E := First_Entity (Expec_Type);
18198 loop
18199 if No (E) then
18200 return False;
18202 elsif not Ekind_In (E, E_Discriminant, E_Component)
18203 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
18204 then
18205 Next_Entity (E);
18207 else
18208 exit;
18209 end if;
18210 end loop;
18212 if not Covers (Etype (E), Found_Type) then
18213 return False;
18215 elsif Present (Next_Entity (E))
18216 and then (Ekind (E) = E_Component
18217 or else Ekind (Next_Entity (E)) = E_Discriminant)
18218 then
18219 return False;
18221 else
18222 Matching_Field := E;
18223 return True;
18224 end if;
18225 end if;
18226 end Has_One_Matching_Field;
18228 -- Start of processing for Wrong_Type
18230 begin
18231 -- Don't output message if either type is Any_Type, or if a message
18232 -- has already been posted for this node. We need to do the latter
18233 -- check explicitly (it is ordinarily done in Errout), because we
18234 -- are using ! to force the output of the error messages.
18236 if Expec_Type = Any_Type
18237 or else Found_Type = Any_Type
18238 or else Error_Posted (Expr)
18239 then
18240 return;
18242 -- If one of the types is a Taft-Amendment type and the other it its
18243 -- completion, it must be an illegal use of a TAT in the spec, for
18244 -- which an error was already emitted. Avoid cascaded errors.
18246 elsif Is_Incomplete_Type (Expec_Type)
18247 and then Has_Completion_In_Body (Expec_Type)
18248 and then Full_View (Expec_Type) = Etype (Expr)
18249 then
18250 return;
18252 elsif Is_Incomplete_Type (Etype (Expr))
18253 and then Has_Completion_In_Body (Etype (Expr))
18254 and then Full_View (Etype (Expr)) = Expec_Type
18255 then
18256 return;
18258 -- In an instance, there is an ongoing problem with completion of
18259 -- type derived from private types. Their structure is what Gigi
18260 -- expects, but the Etype is the parent type rather than the
18261 -- derived private type itself. Do not flag error in this case. The
18262 -- private completion is an entity without a parent, like an Itype.
18263 -- Similarly, full and partial views may be incorrect in the instance.
18264 -- There is no simple way to insure that it is consistent ???
18266 -- A similar view discrepancy can happen in an inlined body, for the
18267 -- same reason: inserted body may be outside of the original package
18268 -- and only partial views are visible at the point of insertion.
18270 elsif In_Instance or else In_Inlined_Body then
18271 if Etype (Etype (Expr)) = Etype (Expected_Type)
18272 and then
18273 (Has_Private_Declaration (Expected_Type)
18274 or else Has_Private_Declaration (Etype (Expr)))
18275 and then No (Parent (Expected_Type))
18276 then
18277 return;
18279 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
18280 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
18281 then
18282 return;
18284 elsif Is_Private_Type (Expected_Type)
18285 and then Present (Full_View (Expected_Type))
18286 and then Covers (Full_View (Expected_Type), Etype (Expr))
18287 then
18288 return;
18289 end if;
18290 end if;
18292 -- An interesting special check. If the expression is parenthesized
18293 -- and its type corresponds to the type of the sole component of the
18294 -- expected record type, or to the component type of the expected one
18295 -- dimensional array type, then assume we have a bad aggregate attempt.
18297 if Nkind (Expr) in N_Subexpr
18298 and then Paren_Count (Expr) /= 0
18299 and then Has_One_Matching_Field
18300 then
18301 Error_Msg_N ("positional aggregate cannot have one component", Expr);
18302 if Present (Matching_Field) then
18303 if Is_Array_Type (Expec_Type) then
18304 Error_Msg_NE
18305 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
18307 else
18308 Error_Msg_NE
18309 ("\write instead `& ='> ...`", Expr, Matching_Field);
18310 end if;
18311 end if;
18313 -- Another special check, if we are looking for a pool-specific access
18314 -- type and we found an E_Access_Attribute_Type, then we have the case
18315 -- of an Access attribute being used in a context which needs a pool-
18316 -- specific type, which is never allowed. The one extra check we make
18317 -- is that the expected designated type covers the Found_Type.
18319 elsif Is_Access_Type (Expec_Type)
18320 and then Ekind (Found_Type) = E_Access_Attribute_Type
18321 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
18322 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
18323 and then Covers
18324 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
18325 then
18326 Error_Msg_N -- CODEFIX
18327 ("result must be general access type!", Expr);
18328 Error_Msg_NE -- CODEFIX
18329 ("add ALL to }!", Expr, Expec_Type);
18331 -- Another special check, if the expected type is an integer type,
18332 -- but the expression is of type System.Address, and the parent is
18333 -- an addition or subtraction operation whose left operand is the
18334 -- expression in question and whose right operand is of an integral
18335 -- type, then this is an attempt at address arithmetic, so give
18336 -- appropriate message.
18338 elsif Is_Integer_Type (Expec_Type)
18339 and then Is_RTE (Found_Type, RE_Address)
18340 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
18341 and then Expr = Left_Opnd (Parent (Expr))
18342 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
18343 then
18344 Error_Msg_N
18345 ("address arithmetic not predefined in package System",
18346 Parent (Expr));
18347 Error_Msg_N
18348 ("\possible missing with/use of System.Storage_Elements",
18349 Parent (Expr));
18350 return;
18352 -- If the expected type is an anonymous access type, as for access
18353 -- parameters and discriminants, the error is on the designated types.
18355 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
18356 if Comes_From_Source (Expec_Type) then
18357 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18358 else
18359 Error_Msg_NE
18360 ("expected an access type with designated}",
18361 Expr, Designated_Type (Expec_Type));
18362 end if;
18364 if Is_Access_Type (Found_Type)
18365 and then not Comes_From_Source (Found_Type)
18366 then
18367 Error_Msg_NE
18368 ("\\found an access type with designated}!",
18369 Expr, Designated_Type (Found_Type));
18370 else
18371 if From_Limited_With (Found_Type) then
18372 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
18373 Error_Msg_Qual_Level := 99;
18374 Error_Msg_NE -- CODEFIX
18375 ("\\missing `WITH &;", Expr, Scope (Found_Type));
18376 Error_Msg_Qual_Level := 0;
18377 else
18378 Error_Msg_NE ("found}!", Expr, Found_Type);
18379 end if;
18380 end if;
18382 -- Normal case of one type found, some other type expected
18384 else
18385 -- If the names of the two types are the same, see if some number
18386 -- of levels of qualification will help. Don't try more than three
18387 -- levels, and if we get to standard, it's no use (and probably
18388 -- represents an error in the compiler) Also do not bother with
18389 -- internal scope names.
18391 declare
18392 Expec_Scope : Entity_Id;
18393 Found_Scope : Entity_Id;
18395 begin
18396 Expec_Scope := Expec_Type;
18397 Found_Scope := Found_Type;
18399 for Levels in Int range 0 .. 3 loop
18400 if Chars (Expec_Scope) /= Chars (Found_Scope) then
18401 Error_Msg_Qual_Level := Levels;
18402 exit;
18403 end if;
18405 Expec_Scope := Scope (Expec_Scope);
18406 Found_Scope := Scope (Found_Scope);
18408 exit when Expec_Scope = Standard_Standard
18409 or else Found_Scope = Standard_Standard
18410 or else not Comes_From_Source (Expec_Scope)
18411 or else not Comes_From_Source (Found_Scope);
18412 end loop;
18413 end;
18415 if Is_Record_Type (Expec_Type)
18416 and then Present (Corresponding_Remote_Type (Expec_Type))
18417 then
18418 Error_Msg_NE ("expected}!", Expr,
18419 Corresponding_Remote_Type (Expec_Type));
18420 else
18421 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18422 end if;
18424 if Is_Entity_Name (Expr)
18425 and then Is_Package_Or_Generic_Package (Entity (Expr))
18426 then
18427 Error_Msg_N ("\\found package name!", Expr);
18429 elsif Is_Entity_Name (Expr)
18430 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
18431 then
18432 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
18433 Error_Msg_N
18434 ("found procedure name, possibly missing Access attribute!",
18435 Expr);
18436 else
18437 Error_Msg_N
18438 ("\\found procedure name instead of function!", Expr);
18439 end if;
18441 elsif Nkind (Expr) = N_Function_Call
18442 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
18443 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
18444 and then No (Parameter_Associations (Expr))
18445 then
18446 Error_Msg_N
18447 ("found function name, possibly missing Access attribute!",
18448 Expr);
18450 -- Catch common error: a prefix or infix operator which is not
18451 -- directly visible because the type isn't.
18453 elsif Nkind (Expr) in N_Op
18454 and then Is_Overloaded (Expr)
18455 and then not Is_Immediately_Visible (Expec_Type)
18456 and then not Is_Potentially_Use_Visible (Expec_Type)
18457 and then not In_Use (Expec_Type)
18458 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
18459 then
18460 Error_Msg_N
18461 ("operator of the type is not directly visible!", Expr);
18463 elsif Ekind (Found_Type) = E_Void
18464 and then Present (Parent (Found_Type))
18465 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
18466 then
18467 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
18469 else
18470 Error_Msg_NE ("\\found}!", Expr, Found_Type);
18471 end if;
18473 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
18474 -- of the same modular type, and (M1 and M2) = 0 was intended.
18476 if Expec_Type = Standard_Boolean
18477 and then Is_Modular_Integer_Type (Found_Type)
18478 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
18479 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
18480 then
18481 declare
18482 Op : constant Node_Id := Right_Opnd (Parent (Expr));
18483 L : constant Node_Id := Left_Opnd (Op);
18484 R : constant Node_Id := Right_Opnd (Op);
18486 begin
18487 -- The case for the message is when the left operand of the
18488 -- comparison is the same modular type, or when it is an
18489 -- integer literal (or other universal integer expression),
18490 -- which would have been typed as the modular type if the
18491 -- parens had been there.
18493 if (Etype (L) = Found_Type
18494 or else
18495 Etype (L) = Universal_Integer)
18496 and then Is_Integer_Type (Etype (R))
18497 then
18498 Error_Msg_N
18499 ("\\possible missing parens for modular operation", Expr);
18500 end if;
18501 end;
18502 end if;
18504 -- Reset error message qualification indication
18506 Error_Msg_Qual_Level := 0;
18507 end if;
18508 end Wrong_Type;
18510 end Sem_Util;