debug.adb: Document new debug flag -gnatd.1.
[official-gcc.git] / gcc / ada / sem_util.adb
blobee5db0017610cbe5048d39ae253ae3d5522e3153
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_Unst; use Exp_Unst;
36 with Exp_Util; use Exp_Util;
37 with Fname; use Fname;
38 with Freeze; use Freeze;
39 with Lib; use Lib;
40 with Lib.Xref; use Lib.Xref;
41 with Namet.Sp; use Namet.Sp;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Attr; use Sem_Attr;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Prag; use Sem_Prag;
57 with Sem_Res; use Sem_Res;
58 with Sem_Warn; use Sem_Warn;
59 with Sem_Type; use Sem_Type;
60 with Sinfo; use Sinfo;
61 with Sinput; use Sinput;
62 with Stand; use Stand;
63 with Style;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uname; use Uname;
70 with GNAT.HTable; use GNAT.HTable;
72 package body Sem_Util is
74 ----------------------------------------
75 -- Global Variables for New_Copy_Tree --
76 ----------------------------------------
78 -- These global variables are used by New_Copy_Tree. See description of the
79 -- body of this subprogram for details. Global variables can be safely used
80 -- by New_Copy_Tree, since there is no case of a recursive call from the
81 -- processing inside New_Copy_Tree.
83 NCT_Hash_Threshold : constant := 20;
84 -- If there are more than this number of pairs of entries in the map, then
85 -- Hash_Tables_Used will be set, and the hash tables will be initialized
86 -- and used for the searches.
88 NCT_Hash_Tables_Used : Boolean := False;
89 -- Set to True if hash tables are in use
91 NCT_Table_Entries : Nat := 0;
92 -- Count entries in table to see if threshold is reached
94 NCT_Hash_Table_Setup : Boolean := False;
95 -- Set to True if hash table contains data. We set this True if we setup
96 -- the hash table with data, and leave it set permanently from then on,
97 -- this is a signal that second and subsequent users of the hash table
98 -- must clear the old entries before reuse.
100 subtype NCT_Header_Num is Int range 0 .. 511;
101 -- Defines range of headers in hash tables (512 headers)
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 function Build_Component_Subtype
108 (C : List_Id;
109 Loc : Source_Ptr;
110 T : Entity_Id) return Node_Id;
111 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
112 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
113 -- Loc is the source location, T is the original subtype.
115 function Has_Enabled_Property
116 (Item_Id : Entity_Id;
117 Property : Name_Id) return Boolean;
118 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
119 -- Determine whether an abstract state or a variable denoted by entity
120 -- Item_Id has enabled property Property.
122 function Has_Null_Extension (T : Entity_Id) return Boolean;
123 -- T is a derived tagged type. Check whether the type extension is null.
124 -- If the parent type is fully initialized, T can be treated as such.
126 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
127 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
128 -- with discriminants whose default values are static, examine only the
129 -- components in the selected variant to determine whether all of them
130 -- have a default.
132 ------------------------------
133 -- Abstract_Interface_List --
134 ------------------------------
136 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
137 Nod : Node_Id;
139 begin
140 if Is_Concurrent_Type (Typ) then
142 -- If we are dealing with a synchronized subtype, go to the base
143 -- type, whose declaration has the interface list.
145 -- Shouldn't this be Declaration_Node???
147 Nod := Parent (Base_Type (Typ));
149 if Nkind (Nod) = N_Full_Type_Declaration then
150 return Empty_List;
151 end if;
153 elsif Ekind (Typ) = E_Record_Type_With_Private then
154 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
155 Nod := Type_Definition (Parent (Typ));
157 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
158 if Present (Full_View (Typ))
159 and then
160 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
161 then
162 Nod := Type_Definition (Parent (Full_View (Typ)));
164 -- If the full-view is not available we cannot do anything else
165 -- here (the source has errors).
167 else
168 return Empty_List;
169 end if;
171 -- Support for generic formals with interfaces is still missing ???
173 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
174 return Empty_List;
176 else
177 pragma Assert
178 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
179 Nod := Parent (Typ);
180 end if;
182 elsif Ekind (Typ) = E_Record_Subtype then
183 Nod := Type_Definition (Parent (Etype (Typ)));
185 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
187 -- Recurse, because parent may still be a private extension. Also
188 -- note that the full view of the subtype or the full view of its
189 -- base type may (both) be unavailable.
191 return Abstract_Interface_List (Etype (Typ));
193 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
194 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
195 Nod := Formal_Type_Definition (Parent (Typ));
196 else
197 Nod := Type_Definition (Parent (Typ));
198 end if;
199 end if;
201 return Interface_List (Nod);
202 end Abstract_Interface_List;
204 --------------------------------
205 -- Add_Access_Type_To_Process --
206 --------------------------------
208 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
209 L : Elist_Id;
211 begin
212 Ensure_Freeze_Node (E);
213 L := Access_Types_To_Process (Freeze_Node (E));
215 if No (L) then
216 L := New_Elmt_List;
217 Set_Access_Types_To_Process (Freeze_Node (E), L);
218 end if;
220 Append_Elmt (A, L);
221 end Add_Access_Type_To_Process;
223 --------------------------
224 -- Add_Block_Identifier --
225 --------------------------
227 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
228 Loc : constant Source_Ptr := Sloc (N);
230 begin
231 pragma Assert (Nkind (N) = N_Block_Statement);
233 -- The block already has a label, return its entity
235 if Present (Identifier (N)) then
236 Id := Entity (Identifier (N));
238 -- Create a new block label and set its attributes
240 else
241 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
242 Set_Etype (Id, Standard_Void_Type);
243 Set_Parent (Id, N);
245 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
246 Set_Block_Node (Id, Identifier (N));
247 end if;
248 end Add_Block_Identifier;
250 -----------------------
251 -- Add_Contract_Item --
252 -----------------------
254 procedure Add_Contract_Item (Prag : Node_Id; Id : Entity_Id) is
255 Items : Node_Id := Contract (Id);
257 procedure Add_Classification;
258 -- Prepend Prag to the list of classifications
260 procedure Add_Contract_Test_Case;
261 -- Prepend Prag to the list of contract and test cases
263 procedure Add_Pre_Post_Condition;
264 -- Prepend Prag to the list of pre- and postconditions
266 ------------------------
267 -- Add_Classification --
268 ------------------------
270 procedure Add_Classification is
271 begin
272 Set_Next_Pragma (Prag, Classifications (Items));
273 Set_Classifications (Items, Prag);
274 end Add_Classification;
276 ----------------------------
277 -- Add_Contract_Test_Case --
278 ----------------------------
280 procedure Add_Contract_Test_Case is
281 begin
282 Set_Next_Pragma (Prag, Contract_Test_Cases (Items));
283 Set_Contract_Test_Cases (Items, Prag);
284 end Add_Contract_Test_Case;
286 ----------------------------
287 -- Add_Pre_Post_Condition --
288 ----------------------------
290 procedure Add_Pre_Post_Condition is
291 begin
292 Set_Next_Pragma (Prag, Pre_Post_Conditions (Items));
293 Set_Pre_Post_Conditions (Items, Prag);
294 end Add_Pre_Post_Condition;
296 -- Local variables
298 Prag_Nam : Name_Id;
300 -- Start of processing for Add_Contract_Item
302 begin
303 -- A contract must contain only pragmas
305 pragma Assert (Nkind (Prag) = N_Pragma);
306 Prag_Nam := Pragma_Name (Prag);
308 -- Create a new contract when adding the first item
310 if No (Items) then
311 Items := Make_Contract (Sloc (Id));
312 Set_Contract (Id, Items);
313 end if;
315 -- Contract items related to [generic] packages or instantiations. The
316 -- applicable pragmas are:
317 -- Abstract_States
318 -- Initial_Condition
319 -- Initializes
320 -- Part_Of (instantiation only)
322 if Ekind_In (Id, E_Generic_Package, E_Package) then
323 if Nam_In (Prag_Nam, Name_Abstract_State,
324 Name_Initial_Condition,
325 Name_Initializes)
326 then
327 Add_Classification;
329 -- Indicator Part_Of must be associated with a package instantiation
331 elsif Prag_Nam = Name_Part_Of and then Is_Generic_Instance (Id) then
332 Add_Classification;
334 -- The pragma is not a proper contract item
336 else
337 raise Program_Error;
338 end if;
340 -- Contract items related to package bodies. The applicable pragmas are:
341 -- Refined_States
343 elsif Ekind (Id) = E_Package_Body then
344 if Prag_Nam = Name_Refined_State then
345 Add_Classification;
347 -- The pragma is not a proper contract item
349 else
350 raise Program_Error;
351 end if;
353 -- Contract items related to subprogram or entry declarations. The
354 -- applicable pragmas are:
355 -- Contract_Cases
356 -- Depends
357 -- Extensions_Visible
358 -- Global
359 -- Postcondition
360 -- Precondition
361 -- Test_Case
363 elsif Ekind_In (Id, E_Entry, E_Entry_Family)
364 or else Is_Generic_Subprogram (Id)
365 or else Is_Subprogram (Id)
366 then
367 if Nam_In (Prag_Nam, Name_Postcondition, Name_Precondition) then
368 Add_Pre_Post_Condition;
370 elsif Nam_In (Prag_Nam, Name_Contract_Cases, Name_Test_Case) then
371 Add_Contract_Test_Case;
373 elsif Nam_In (Prag_Nam, Name_Depends,
374 Name_Extensions_Visible,
375 Name_Global)
376 then
377 Add_Classification;
379 -- The pragma is not a proper contract item
381 else
382 raise Program_Error;
383 end if;
385 -- Contract items related to subprogram bodies. Applicable pragmas are:
386 -- Postcondition
387 -- Precondition
388 -- Refined_Depends
389 -- Refined_Global
390 -- Refined_Post
392 elsif Ekind (Id) = E_Subprogram_Body then
393 if Nam_In (Prag_Nam, Name_Refined_Depends, Name_Refined_Global) then
394 Add_Classification;
396 elsif Nam_In (Prag_Nam, Name_Postcondition,
397 Name_Precondition,
398 Name_Refined_Post)
399 then
400 Add_Pre_Post_Condition;
402 -- The pragma is not a proper contract item
404 else
405 raise Program_Error;
406 end if;
408 -- Contract items related to variables. Applicable pragmas are:
409 -- Async_Readers
410 -- Async_Writers
411 -- Effective_Reads
412 -- Effective_Writes
413 -- Part_Of
415 elsif Ekind (Id) = E_Variable then
416 if Nam_In (Prag_Nam, Name_Async_Readers,
417 Name_Async_Writers,
418 Name_Effective_Reads,
419 Name_Effective_Writes,
420 Name_Part_Of)
421 then
422 Add_Classification;
424 -- The pragma is not a proper contract item
426 else
427 raise Program_Error;
428 end if;
429 end if;
430 end Add_Contract_Item;
432 ----------------------------
433 -- Add_Global_Declaration --
434 ----------------------------
436 procedure Add_Global_Declaration (N : Node_Id) is
437 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
439 begin
440 if No (Declarations (Aux_Node)) then
441 Set_Declarations (Aux_Node, New_List);
442 end if;
444 Append_To (Declarations (Aux_Node), N);
445 Analyze (N);
446 end Add_Global_Declaration;
448 --------------------------------
449 -- Address_Integer_Convert_OK --
450 --------------------------------
452 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
453 begin
454 if Allow_Integer_Address
455 and then ((Is_Descendent_Of_Address (T1)
456 and then Is_Private_Type (T1)
457 and then Is_Integer_Type (T2))
458 or else
459 (Is_Descendent_Of_Address (T2)
460 and then Is_Private_Type (T2)
461 and then Is_Integer_Type (T1)))
462 then
463 return True;
464 else
465 return False;
466 end if;
467 end Address_Integer_Convert_OK;
469 -----------------
470 -- Addressable --
471 -----------------
473 -- For now, just 8/16/32/64. but analyze later if AAMP is special???
475 function Addressable (V : Uint) return Boolean is
476 begin
477 return V = Uint_8 or else
478 V = Uint_16 or else
479 V = Uint_32 or else
480 V = Uint_64;
481 end Addressable;
483 function Addressable (V : Int) return Boolean is
484 begin
485 return V = 8 or else
486 V = 16 or else
487 V = 32 or else
488 V = 64;
489 end Addressable;
491 ---------------------------------
492 -- Aggregate_Constraint_Checks --
493 ---------------------------------
495 procedure Aggregate_Constraint_Checks
496 (Exp : Node_Id;
497 Check_Typ : Entity_Id)
499 Exp_Typ : constant Entity_Id := Etype (Exp);
501 begin
502 if Raises_Constraint_Error (Exp) then
503 return;
504 end if;
506 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
507 -- component's type to force the appropriate accessibility checks.
509 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
510 -- type to force the corresponding run-time check
512 if Is_Access_Type (Check_Typ)
513 and then ((Is_Local_Anonymous_Access (Check_Typ))
514 or else (Can_Never_Be_Null (Check_Typ)
515 and then not Can_Never_Be_Null (Exp_Typ)))
516 then
517 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
518 Analyze_And_Resolve (Exp, Check_Typ);
519 Check_Unset_Reference (Exp);
520 end if;
522 -- This is really expansion activity, so make sure that expansion is
523 -- on and is allowed. In GNATprove mode, we also want check flags to
524 -- be added in the tree, so that the formal verification can rely on
525 -- those to be present. In GNATprove mode for formal verification, some
526 -- treatment typically only done during expansion needs to be performed
527 -- on the tree, but it should not be applied inside generics. Otherwise,
528 -- this breaks the name resolution mechanism for generic instances.
530 if not Expander_Active
531 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
532 then
533 return;
534 end if;
536 -- First check if we have to insert discriminant checks
538 if Has_Discriminants (Exp_Typ) then
539 Apply_Discriminant_Check (Exp, Check_Typ);
541 -- Next emit length checks for array aggregates
543 elsif Is_Array_Type (Exp_Typ) then
544 Apply_Length_Check (Exp, Check_Typ);
546 -- Finally emit scalar and string checks. If we are dealing with a
547 -- scalar literal we need to check by hand because the Etype of
548 -- literals is not necessarily correct.
550 elsif Is_Scalar_Type (Exp_Typ)
551 and then Compile_Time_Known_Value (Exp)
552 then
553 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
554 Apply_Compile_Time_Constraint_Error
555 (Exp, "value not in range of}??", CE_Range_Check_Failed,
556 Ent => Base_Type (Check_Typ),
557 Typ => Base_Type (Check_Typ));
559 elsif Is_Out_Of_Range (Exp, Check_Typ) then
560 Apply_Compile_Time_Constraint_Error
561 (Exp, "value not in range of}??", CE_Range_Check_Failed,
562 Ent => Check_Typ,
563 Typ => Check_Typ);
565 elsif not Range_Checks_Suppressed (Check_Typ) then
566 Apply_Scalar_Range_Check (Exp, Check_Typ);
567 end if;
569 -- Verify that target type is also scalar, to prevent view anomalies
570 -- in instantiations.
572 elsif (Is_Scalar_Type (Exp_Typ)
573 or else Nkind (Exp) = N_String_Literal)
574 and then Is_Scalar_Type (Check_Typ)
575 and then Exp_Typ /= Check_Typ
576 then
577 if Is_Entity_Name (Exp)
578 and then Ekind (Entity (Exp)) = E_Constant
579 then
580 -- If expression is a constant, it is worthwhile checking whether
581 -- it is a bound of the type.
583 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
584 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
585 or else
586 (Is_Entity_Name (Type_High_Bound (Check_Typ))
587 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
588 then
589 return;
591 else
592 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
593 Analyze_And_Resolve (Exp, Check_Typ);
594 Check_Unset_Reference (Exp);
595 end if;
597 -- Could use a comment on this case ???
599 else
600 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
601 Analyze_And_Resolve (Exp, Check_Typ);
602 Check_Unset_Reference (Exp);
603 end if;
605 end if;
606 end Aggregate_Constraint_Checks;
608 -----------------------
609 -- Alignment_In_Bits --
610 -----------------------
612 function Alignment_In_Bits (E : Entity_Id) return Uint is
613 begin
614 return Alignment (E) * System_Storage_Unit;
615 end Alignment_In_Bits;
617 ---------------------------------
618 -- Append_Inherited_Subprogram --
619 ---------------------------------
621 procedure Append_Inherited_Subprogram (S : Entity_Id) is
622 Par : constant Entity_Id := Alias (S);
623 -- The parent subprogram
625 Scop : constant Entity_Id := Scope (Par);
626 -- The scope of definition of the parent subprogram
628 Typ : constant Entity_Id := Defining_Entity (Parent (S));
629 -- The derived type of which S is a primitive operation
631 Decl : Node_Id;
632 Next_E : Entity_Id;
634 begin
635 if Ekind (Current_Scope) = E_Package
636 and then In_Private_Part (Current_Scope)
637 and then Has_Private_Declaration (Typ)
638 and then Is_Tagged_Type (Typ)
639 and then Scop = Current_Scope
640 then
641 -- The inherited operation is available at the earliest place after
642 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
643 -- relevant for type extensions. If the parent operation appears
644 -- after the type extension, the operation is not visible.
646 Decl := First
647 (Visible_Declarations
648 (Package_Specification (Current_Scope)));
649 while Present (Decl) loop
650 if Nkind (Decl) = N_Private_Extension_Declaration
651 and then Defining_Entity (Decl) = Typ
652 then
653 if Sloc (Decl) > Sloc (Par) then
654 Next_E := Next_Entity (Par);
655 Set_Next_Entity (Par, S);
656 Set_Next_Entity (S, Next_E);
657 return;
659 else
660 exit;
661 end if;
662 end if;
664 Next (Decl);
665 end loop;
666 end if;
668 -- If partial view is not a type extension, or it appears before the
669 -- subprogram declaration, insert normally at end of entity list.
671 Append_Entity (S, Current_Scope);
672 end Append_Inherited_Subprogram;
674 -----------------------------------------
675 -- Apply_Compile_Time_Constraint_Error --
676 -----------------------------------------
678 procedure Apply_Compile_Time_Constraint_Error
679 (N : Node_Id;
680 Msg : String;
681 Reason : RT_Exception_Code;
682 Ent : Entity_Id := Empty;
683 Typ : Entity_Id := Empty;
684 Loc : Source_Ptr := No_Location;
685 Rep : Boolean := True;
686 Warn : Boolean := False)
688 Stat : constant Boolean := Is_Static_Expression (N);
689 R_Stat : constant Node_Id :=
690 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
691 Rtyp : Entity_Id;
693 begin
694 if No (Typ) then
695 Rtyp := Etype (N);
696 else
697 Rtyp := Typ;
698 end if;
700 Discard_Node
701 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
703 if not Rep then
704 return;
705 end if;
707 -- Now we replace the node by an N_Raise_Constraint_Error node
708 -- This does not need reanalyzing, so set it as analyzed now.
710 Rewrite (N, R_Stat);
711 Set_Analyzed (N, True);
713 Set_Etype (N, Rtyp);
714 Set_Raises_Constraint_Error (N);
716 -- Now deal with possible local raise handling
718 Possible_Local_Raise (N, Standard_Constraint_Error);
720 -- If the original expression was marked as static, the result is
721 -- still marked as static, but the Raises_Constraint_Error flag is
722 -- always set so that further static evaluation is not attempted.
724 if Stat then
725 Set_Is_Static_Expression (N);
726 end if;
727 end Apply_Compile_Time_Constraint_Error;
729 ---------------------------
730 -- Async_Readers_Enabled --
731 ---------------------------
733 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
734 begin
735 return Has_Enabled_Property (Id, Name_Async_Readers);
736 end Async_Readers_Enabled;
738 ---------------------------
739 -- Async_Writers_Enabled --
740 ---------------------------
742 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
743 begin
744 return Has_Enabled_Property (Id, Name_Async_Writers);
745 end Async_Writers_Enabled;
747 --------------------------------------
748 -- Available_Full_View_Of_Component --
749 --------------------------------------
751 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
752 ST : constant Entity_Id := Scope (T);
753 SCT : constant Entity_Id := Scope (Component_Type (T));
754 begin
755 return In_Open_Scopes (ST)
756 and then In_Open_Scopes (SCT)
757 and then Scope_Depth (ST) >= Scope_Depth (SCT);
758 end Available_Full_View_Of_Component;
760 -------------------
761 -- Bad_Attribute --
762 -------------------
764 procedure Bad_Attribute
765 (N : Node_Id;
766 Nam : Name_Id;
767 Warn : Boolean := False)
769 begin
770 Error_Msg_Warn := Warn;
771 Error_Msg_N ("unrecognized attribute&<<", N);
773 -- Check for possible misspelling
775 Error_Msg_Name_1 := First_Attribute_Name;
776 while Error_Msg_Name_1 <= Last_Attribute_Name loop
777 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
778 Error_Msg_N -- CODEFIX
779 ("\possible misspelling of %<<", N);
780 exit;
781 end if;
783 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
784 end loop;
785 end Bad_Attribute;
787 --------------------------------
788 -- Bad_Predicated_Subtype_Use --
789 --------------------------------
791 procedure Bad_Predicated_Subtype_Use
792 (Msg : String;
793 N : Node_Id;
794 Typ : Entity_Id;
795 Suggest_Static : Boolean := False)
797 Gen : Entity_Id;
799 begin
800 -- Avoid cascaded errors
802 if Error_Posted (N) then
803 return;
804 end if;
806 if Inside_A_Generic then
807 Gen := Current_Scope;
808 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
809 Gen := Scope (Gen);
810 end loop;
812 if No (Gen) then
813 return;
814 end if;
816 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
817 Set_No_Predicate_On_Actual (Typ);
818 end if;
820 elsif Has_Predicates (Typ) then
821 if Is_Generic_Actual_Type (Typ) then
823 -- The restriction on loop parameters is only that the type
824 -- should have no dynamic predicates.
826 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
827 and then not Has_Dynamic_Predicate_Aspect (Typ)
828 and then Is_OK_Static_Subtype (Typ)
829 then
830 return;
831 end if;
833 Gen := Current_Scope;
834 while not Is_Generic_Instance (Gen) loop
835 Gen := Scope (Gen);
836 end loop;
838 pragma Assert (Present (Gen));
840 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
841 Error_Msg_Warn := SPARK_Mode /= On;
842 Error_Msg_FE (Msg & "<<", N, Typ);
843 Error_Msg_F ("\Program_Error [<<", N);
845 Insert_Action (N,
846 Make_Raise_Program_Error (Sloc (N),
847 Reason => PE_Bad_Predicated_Generic_Type));
849 else
850 Error_Msg_FE (Msg & "<<", N, Typ);
851 end if;
853 else
854 Error_Msg_FE (Msg, N, Typ);
855 end if;
857 -- Emit an optional suggestion on how to remedy the error if the
858 -- context warrants it.
860 if Suggest_Static and then Has_Static_Predicate (Typ) then
861 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
862 end if;
863 end if;
864 end Bad_Predicated_Subtype_Use;
866 -----------------------------------------
867 -- Bad_Unordered_Enumeration_Reference --
868 -----------------------------------------
870 function Bad_Unordered_Enumeration_Reference
871 (N : Node_Id;
872 T : Entity_Id) return Boolean
874 begin
875 return Is_Enumeration_Type (T)
876 and then Warn_On_Unordered_Enumeration_Type
877 and then not Is_Generic_Type (T)
878 and then Comes_From_Source (N)
879 and then not Has_Pragma_Ordered (T)
880 and then not In_Same_Extended_Unit (N, T);
881 end Bad_Unordered_Enumeration_Reference;
883 --------------------------
884 -- Build_Actual_Subtype --
885 --------------------------
887 function Build_Actual_Subtype
888 (T : Entity_Id;
889 N : Node_Or_Entity_Id) return Node_Id
891 Loc : Source_Ptr;
892 -- Normally Sloc (N), but may point to corresponding body in some cases
894 Constraints : List_Id;
895 Decl : Node_Id;
896 Discr : Entity_Id;
897 Hi : Node_Id;
898 Lo : Node_Id;
899 Subt : Entity_Id;
900 Disc_Type : Entity_Id;
901 Obj : Node_Id;
903 begin
904 Loc := Sloc (N);
906 if Nkind (N) = N_Defining_Identifier then
907 Obj := New_Occurrence_Of (N, Loc);
909 -- If this is a formal parameter of a subprogram declaration, and
910 -- we are compiling the body, we want the declaration for the
911 -- actual subtype to carry the source position of the body, to
912 -- prevent anomalies in gdb when stepping through the code.
914 if Is_Formal (N) then
915 declare
916 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
917 begin
918 if Nkind (Decl) = N_Subprogram_Declaration
919 and then Present (Corresponding_Body (Decl))
920 then
921 Loc := Sloc (Corresponding_Body (Decl));
922 end if;
923 end;
924 end if;
926 else
927 Obj := N;
928 end if;
930 if Is_Array_Type (T) then
931 Constraints := New_List;
932 for J in 1 .. Number_Dimensions (T) loop
934 -- Build an array subtype declaration with the nominal subtype and
935 -- the bounds of the actual. Add the declaration in front of the
936 -- local declarations for the subprogram, for analysis before any
937 -- reference to the formal in the body.
939 Lo :=
940 Make_Attribute_Reference (Loc,
941 Prefix =>
942 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
943 Attribute_Name => Name_First,
944 Expressions => New_List (
945 Make_Integer_Literal (Loc, J)));
947 Hi :=
948 Make_Attribute_Reference (Loc,
949 Prefix =>
950 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
951 Attribute_Name => Name_Last,
952 Expressions => New_List (
953 Make_Integer_Literal (Loc, J)));
955 Append (Make_Range (Loc, Lo, Hi), Constraints);
956 end loop;
958 -- If the type has unknown discriminants there is no constrained
959 -- subtype to build. This is never called for a formal or for a
960 -- lhs, so returning the type is ok ???
962 elsif Has_Unknown_Discriminants (T) then
963 return T;
965 else
966 Constraints := New_List;
968 -- Type T is a generic derived type, inherit the discriminants from
969 -- the parent type.
971 if Is_Private_Type (T)
972 and then No (Full_View (T))
974 -- T was flagged as an error if it was declared as a formal
975 -- derived type with known discriminants. In this case there
976 -- is no need to look at the parent type since T already carries
977 -- its own discriminants.
979 and then not Error_Posted (T)
980 then
981 Disc_Type := Etype (Base_Type (T));
982 else
983 Disc_Type := T;
984 end if;
986 Discr := First_Discriminant (Disc_Type);
987 while Present (Discr) loop
988 Append_To (Constraints,
989 Make_Selected_Component (Loc,
990 Prefix =>
991 Duplicate_Subexpr_No_Checks (Obj),
992 Selector_Name => New_Occurrence_Of (Discr, Loc)));
993 Next_Discriminant (Discr);
994 end loop;
995 end if;
997 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
998 Set_Is_Internal (Subt);
1000 Decl :=
1001 Make_Subtype_Declaration (Loc,
1002 Defining_Identifier => Subt,
1003 Subtype_Indication =>
1004 Make_Subtype_Indication (Loc,
1005 Subtype_Mark => New_Occurrence_Of (T, Loc),
1006 Constraint =>
1007 Make_Index_Or_Discriminant_Constraint (Loc,
1008 Constraints => Constraints)));
1010 Mark_Rewrite_Insertion (Decl);
1011 return Decl;
1012 end Build_Actual_Subtype;
1014 ---------------------------------------
1015 -- Build_Actual_Subtype_Of_Component --
1016 ---------------------------------------
1018 function Build_Actual_Subtype_Of_Component
1019 (T : Entity_Id;
1020 N : Node_Id) return Node_Id
1022 Loc : constant Source_Ptr := Sloc (N);
1023 P : constant Node_Id := Prefix (N);
1024 D : Elmt_Id;
1025 Id : Node_Id;
1026 Index_Typ : Entity_Id;
1028 Desig_Typ : Entity_Id;
1029 -- This is either a copy of T, or if T is an access type, then it is
1030 -- the directly designated type of this access type.
1032 function Build_Actual_Array_Constraint return List_Id;
1033 -- If one or more of the bounds of the component depends on
1034 -- discriminants, build actual constraint using the discriminants
1035 -- of the prefix.
1037 function Build_Actual_Record_Constraint return List_Id;
1038 -- Similar to previous one, for discriminated components constrained
1039 -- by the discriminant of the enclosing object.
1041 -----------------------------------
1042 -- Build_Actual_Array_Constraint --
1043 -----------------------------------
1045 function Build_Actual_Array_Constraint return List_Id is
1046 Constraints : constant List_Id := New_List;
1047 Indx : Node_Id;
1048 Hi : Node_Id;
1049 Lo : Node_Id;
1050 Old_Hi : Node_Id;
1051 Old_Lo : Node_Id;
1053 begin
1054 Indx := First_Index (Desig_Typ);
1055 while Present (Indx) loop
1056 Old_Lo := Type_Low_Bound (Etype (Indx));
1057 Old_Hi := Type_High_Bound (Etype (Indx));
1059 if Denotes_Discriminant (Old_Lo) then
1060 Lo :=
1061 Make_Selected_Component (Loc,
1062 Prefix => New_Copy_Tree (P),
1063 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1065 else
1066 Lo := New_Copy_Tree (Old_Lo);
1068 -- The new bound will be reanalyzed in the enclosing
1069 -- declaration. For literal bounds that come from a type
1070 -- declaration, the type of the context must be imposed, so
1071 -- insure that analysis will take place. For non-universal
1072 -- types this is not strictly necessary.
1074 Set_Analyzed (Lo, False);
1075 end if;
1077 if Denotes_Discriminant (Old_Hi) then
1078 Hi :=
1079 Make_Selected_Component (Loc,
1080 Prefix => New_Copy_Tree (P),
1081 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1083 else
1084 Hi := New_Copy_Tree (Old_Hi);
1085 Set_Analyzed (Hi, False);
1086 end if;
1088 Append (Make_Range (Loc, Lo, Hi), Constraints);
1089 Next_Index (Indx);
1090 end loop;
1092 return Constraints;
1093 end Build_Actual_Array_Constraint;
1095 ------------------------------------
1096 -- Build_Actual_Record_Constraint --
1097 ------------------------------------
1099 function Build_Actual_Record_Constraint return List_Id is
1100 Constraints : constant List_Id := New_List;
1101 D : Elmt_Id;
1102 D_Val : Node_Id;
1104 begin
1105 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1106 while Present (D) loop
1107 if Denotes_Discriminant (Node (D)) then
1108 D_Val := Make_Selected_Component (Loc,
1109 Prefix => New_Copy_Tree (P),
1110 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1112 else
1113 D_Val := New_Copy_Tree (Node (D));
1114 end if;
1116 Append (D_Val, Constraints);
1117 Next_Elmt (D);
1118 end loop;
1120 return Constraints;
1121 end Build_Actual_Record_Constraint;
1123 -- Start of processing for Build_Actual_Subtype_Of_Component
1125 begin
1126 -- Why the test for Spec_Expression mode here???
1128 if In_Spec_Expression then
1129 return Empty;
1131 -- More comments for the rest of this body would be good ???
1133 elsif Nkind (N) = N_Explicit_Dereference then
1134 if Is_Composite_Type (T)
1135 and then not Is_Constrained (T)
1136 and then not (Is_Class_Wide_Type (T)
1137 and then Is_Constrained (Root_Type (T)))
1138 and then not Has_Unknown_Discriminants (T)
1139 then
1140 -- If the type of the dereference is already constrained, it is an
1141 -- actual subtype.
1143 if Is_Array_Type (Etype (N))
1144 and then Is_Constrained (Etype (N))
1145 then
1146 return Empty;
1147 else
1148 Remove_Side_Effects (P);
1149 return Build_Actual_Subtype (T, N);
1150 end if;
1151 else
1152 return Empty;
1153 end if;
1154 end if;
1156 if Ekind (T) = E_Access_Subtype then
1157 Desig_Typ := Designated_Type (T);
1158 else
1159 Desig_Typ := T;
1160 end if;
1162 if Ekind (Desig_Typ) = E_Array_Subtype then
1163 Id := First_Index (Desig_Typ);
1164 while Present (Id) loop
1165 Index_Typ := Underlying_Type (Etype (Id));
1167 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1168 or else
1169 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1170 then
1171 Remove_Side_Effects (P);
1172 return
1173 Build_Component_Subtype
1174 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1175 end if;
1177 Next_Index (Id);
1178 end loop;
1180 elsif Is_Composite_Type (Desig_Typ)
1181 and then Has_Discriminants (Desig_Typ)
1182 and then not Has_Unknown_Discriminants (Desig_Typ)
1183 then
1184 if Is_Private_Type (Desig_Typ)
1185 and then No (Discriminant_Constraint (Desig_Typ))
1186 then
1187 Desig_Typ := Full_View (Desig_Typ);
1188 end if;
1190 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1191 while Present (D) loop
1192 if Denotes_Discriminant (Node (D)) then
1193 Remove_Side_Effects (P);
1194 return
1195 Build_Component_Subtype (
1196 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1197 end if;
1199 Next_Elmt (D);
1200 end loop;
1201 end if;
1203 -- If none of the above, the actual and nominal subtypes are the same
1205 return Empty;
1206 end Build_Actual_Subtype_Of_Component;
1208 -----------------------------
1209 -- Build_Component_Subtype --
1210 -----------------------------
1212 function Build_Component_Subtype
1213 (C : List_Id;
1214 Loc : Source_Ptr;
1215 T : Entity_Id) return Node_Id
1217 Subt : Entity_Id;
1218 Decl : Node_Id;
1220 begin
1221 -- Unchecked_Union components do not require component subtypes
1223 if Is_Unchecked_Union (T) then
1224 return Empty;
1225 end if;
1227 Subt := Make_Temporary (Loc, 'S');
1228 Set_Is_Internal (Subt);
1230 Decl :=
1231 Make_Subtype_Declaration (Loc,
1232 Defining_Identifier => Subt,
1233 Subtype_Indication =>
1234 Make_Subtype_Indication (Loc,
1235 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1236 Constraint =>
1237 Make_Index_Or_Discriminant_Constraint (Loc,
1238 Constraints => C)));
1240 Mark_Rewrite_Insertion (Decl);
1241 return Decl;
1242 end Build_Component_Subtype;
1244 ----------------------------------
1245 -- Build_Default_Init_Cond_Call --
1246 ----------------------------------
1248 function Build_Default_Init_Cond_Call
1249 (Loc : Source_Ptr;
1250 Obj_Id : Entity_Id;
1251 Typ : Entity_Id) return Node_Id
1253 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1254 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1256 begin
1257 return
1258 Make_Procedure_Call_Statement (Loc,
1259 Name => New_Occurrence_Of (Proc_Id, Loc),
1260 Parameter_Associations => New_List (
1261 Make_Unchecked_Type_Conversion (Loc,
1262 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1263 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1264 end Build_Default_Init_Cond_Call;
1266 ----------------------------------------------
1267 -- Build_Default_Init_Cond_Procedure_Bodies --
1268 ----------------------------------------------
1270 procedure Build_Default_Init_Cond_Procedure_Bodies (Priv_Decls : List_Id) is
1271 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id);
1272 -- If type Typ is subject to pragma Default_Initial_Condition, build the
1273 -- body of the procedure which verifies the assumption of the pragma at
1274 -- run time. The generated body is added after the type declaration.
1276 --------------------------------------------
1277 -- Build_Default_Init_Cond_Procedure_Body --
1278 --------------------------------------------
1280 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id) is
1281 Param_Id : Entity_Id;
1282 -- The entity of the sole formal parameter of the default initial
1283 -- condition procedure.
1285 procedure Replace_Type_Reference (N : Node_Id);
1286 -- Replace a single reference to type Typ with a reference to formal
1287 -- parameter Param_Id.
1289 ----------------------------
1290 -- Replace_Type_Reference --
1291 ----------------------------
1293 procedure Replace_Type_Reference (N : Node_Id) is
1294 begin
1295 Rewrite (N, New_Occurrence_Of (Param_Id, Sloc (N)));
1296 end Replace_Type_Reference;
1298 procedure Replace_Type_References is
1299 new Replace_Type_References_Generic (Replace_Type_Reference);
1301 -- Local variables
1303 Loc : constant Source_Ptr := Sloc (Typ);
1304 Prag : constant Node_Id :=
1305 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1306 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1307 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Proc_Id);
1308 Body_Decl : Node_Id;
1309 Expr : Node_Id;
1310 Stmt : Node_Id;
1312 -- Start of processing for Build_Default_Init_Cond_Procedure_Body
1314 begin
1315 -- The procedure should be generated only for [sub]types subject to
1316 -- pragma Default_Initial_Condition. Types that inherit the pragma do
1317 -- not get this specialized procedure.
1319 pragma Assert (Has_Default_Init_Cond (Typ));
1320 pragma Assert (Present (Prag));
1321 pragma Assert (Present (Proc_Id));
1323 -- Nothing to do if the body was already built
1325 if Present (Corresponding_Body (Spec_Decl)) then
1326 return;
1327 end if;
1329 Param_Id := First_Formal (Proc_Id);
1331 -- The pragma has an argument. Note that the argument is analyzed
1332 -- after all references to the current instance of the type are
1333 -- replaced.
1335 if Present (Pragma_Argument_Associations (Prag)) then
1336 Expr :=
1337 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
1339 if Nkind (Expr) = N_Null then
1340 Stmt := Make_Null_Statement (Loc);
1342 -- Preserve the original argument of the pragma by replicating it.
1343 -- Replace all references to the current instance of the type with
1344 -- references to the formal parameter.
1346 else
1347 Expr := New_Copy_Tree (Expr);
1348 Replace_Type_References (Expr, Typ);
1350 -- Generate:
1351 -- pragma Check (Default_Initial_Condition, <Expr>);
1353 Stmt :=
1354 Make_Pragma (Loc,
1355 Pragma_Identifier =>
1356 Make_Identifier (Loc, Name_Check),
1358 Pragma_Argument_Associations => New_List (
1359 Make_Pragma_Argument_Association (Loc,
1360 Expression =>
1361 Make_Identifier (Loc,
1362 Chars => Name_Default_Initial_Condition)),
1363 Make_Pragma_Argument_Association (Loc,
1364 Expression => Expr)));
1365 end if;
1367 -- Otherwise the pragma appears without an argument
1369 else
1370 Stmt := Make_Null_Statement (Loc);
1371 end if;
1373 -- Generate:
1374 -- procedure <Typ>Default_Init_Cond (I : <Typ>) is
1375 -- begin
1376 -- <Stmt>;
1377 -- end <Typ>Default_Init_Cond;
1379 Body_Decl :=
1380 Make_Subprogram_Body (Loc,
1381 Specification =>
1382 Copy_Separate_Tree (Specification (Spec_Decl)),
1383 Declarations => Empty_List,
1384 Handled_Statement_Sequence =>
1385 Make_Handled_Sequence_Of_Statements (Loc,
1386 Statements => New_List (Stmt)));
1388 -- Link the spec and body of the default initial condition procedure
1389 -- to prevent the generation of a duplicate body.
1391 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
1392 Set_Corresponding_Spec (Body_Decl, Proc_Id);
1394 Insert_After_And_Analyze (Declaration_Node (Typ), Body_Decl);
1395 end Build_Default_Init_Cond_Procedure_Body;
1397 -- Local variables
1399 Decl : Node_Id;
1400 Typ : Entity_Id;
1402 -- Start of processing for Build_Default_Init_Cond_Procedure_Bodies
1404 begin
1405 -- Inspect the private declarations looking for [sub]type declarations
1407 Decl := First (Priv_Decls);
1408 while Present (Decl) loop
1409 if Nkind_In (Decl, N_Full_Type_Declaration,
1410 N_Subtype_Declaration)
1411 then
1412 Typ := Defining_Entity (Decl);
1414 -- Guard against partially decorate types due to previous errors
1416 if Is_Type (Typ) then
1418 -- If the type is subject to pragma Default_Initial_Condition,
1419 -- generate the body of the internal procedure which verifies
1420 -- the assertion of the pragma at run time.
1422 if Has_Default_Init_Cond (Typ) then
1423 Build_Default_Init_Cond_Procedure_Body (Typ);
1425 -- A derived type inherits the default initial condition
1426 -- procedure from its parent type.
1428 elsif Has_Inherited_Default_Init_Cond (Typ) then
1429 Inherit_Default_Init_Cond_Procedure (Typ);
1430 end if;
1431 end if;
1432 end if;
1434 Next (Decl);
1435 end loop;
1436 end Build_Default_Init_Cond_Procedure_Bodies;
1438 ---------------------------------------------------
1439 -- Build_Default_Init_Cond_Procedure_Declaration --
1440 ---------------------------------------------------
1442 procedure Build_Default_Init_Cond_Procedure_Declaration (Typ : Entity_Id) is
1443 Loc : constant Source_Ptr := Sloc (Typ);
1444 Prag : constant Node_Id :=
1445 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1446 Proc_Id : Entity_Id;
1448 begin
1449 -- The procedure should be generated only for types subject to pragma
1450 -- Default_Initial_Condition. Types that inherit the pragma do not get
1451 -- this specialized procedure.
1453 pragma Assert (Has_Default_Init_Cond (Typ));
1454 pragma Assert (Present (Prag));
1456 -- Nothing to do if default initial condition procedure already built
1458 if Present (Default_Init_Cond_Procedure (Typ)) then
1459 return;
1460 end if;
1462 Proc_Id :=
1463 Make_Defining_Identifier (Loc,
1464 Chars => New_External_Name (Chars (Typ), "Default_Init_Cond"));
1466 -- Associate default initial condition procedure with the private type
1468 Set_Ekind (Proc_Id, E_Procedure);
1469 Set_Is_Default_Init_Cond_Procedure (Proc_Id);
1470 Set_Default_Init_Cond_Procedure (Typ, Proc_Id);
1472 -- Generate:
1473 -- procedure <Typ>Default_Init_Cond (Inn : <Typ>);
1475 Insert_After_And_Analyze (Prag,
1476 Make_Subprogram_Declaration (Loc,
1477 Specification =>
1478 Make_Procedure_Specification (Loc,
1479 Defining_Unit_Name => Proc_Id,
1480 Parameter_Specifications => New_List (
1481 Make_Parameter_Specification (Loc,
1482 Defining_Identifier => Make_Temporary (Loc, 'I'),
1483 Parameter_Type => New_Occurrence_Of (Typ, Loc))))));
1484 end Build_Default_Init_Cond_Procedure_Declaration;
1486 ---------------------------
1487 -- Build_Default_Subtype --
1488 ---------------------------
1490 function Build_Default_Subtype
1491 (T : Entity_Id;
1492 N : Node_Id) return Entity_Id
1494 Loc : constant Source_Ptr := Sloc (N);
1495 Disc : Entity_Id;
1497 Bas : Entity_Id;
1498 -- The base type that is to be constrained by the defaults
1500 begin
1501 if not Has_Discriminants (T) or else Is_Constrained (T) then
1502 return T;
1503 end if;
1505 Bas := Base_Type (T);
1507 -- If T is non-private but its base type is private, this is the
1508 -- completion of a subtype declaration whose parent type is private
1509 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1510 -- are to be found in the full view of the base. Check that the private
1511 -- status of T and its base differ.
1513 if Is_Private_Type (Bas)
1514 and then not Is_Private_Type (T)
1515 and then Present (Full_View (Bas))
1516 then
1517 Bas := Full_View (Bas);
1518 end if;
1520 Disc := First_Discriminant (T);
1522 if No (Discriminant_Default_Value (Disc)) then
1523 return T;
1524 end if;
1526 declare
1527 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1528 Constraints : constant List_Id := New_List;
1529 Decl : Node_Id;
1531 begin
1532 while Present (Disc) loop
1533 Append_To (Constraints,
1534 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1535 Next_Discriminant (Disc);
1536 end loop;
1538 Decl :=
1539 Make_Subtype_Declaration (Loc,
1540 Defining_Identifier => Act,
1541 Subtype_Indication =>
1542 Make_Subtype_Indication (Loc,
1543 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1544 Constraint =>
1545 Make_Index_Or_Discriminant_Constraint (Loc,
1546 Constraints => Constraints)));
1548 Insert_Action (N, Decl);
1549 Analyze (Decl);
1550 return Act;
1551 end;
1552 end Build_Default_Subtype;
1554 --------------------------------------------
1555 -- Build_Discriminal_Subtype_Of_Component --
1556 --------------------------------------------
1558 function Build_Discriminal_Subtype_Of_Component
1559 (T : Entity_Id) return Node_Id
1561 Loc : constant Source_Ptr := Sloc (T);
1562 D : Elmt_Id;
1563 Id : Node_Id;
1565 function Build_Discriminal_Array_Constraint return List_Id;
1566 -- If one or more of the bounds of the component depends on
1567 -- discriminants, build actual constraint using the discriminants
1568 -- of the prefix.
1570 function Build_Discriminal_Record_Constraint return List_Id;
1571 -- Similar to previous one, for discriminated components constrained by
1572 -- the discriminant of the enclosing object.
1574 ----------------------------------------
1575 -- Build_Discriminal_Array_Constraint --
1576 ----------------------------------------
1578 function Build_Discriminal_Array_Constraint return List_Id is
1579 Constraints : constant List_Id := New_List;
1580 Indx : Node_Id;
1581 Hi : Node_Id;
1582 Lo : Node_Id;
1583 Old_Hi : Node_Id;
1584 Old_Lo : Node_Id;
1586 begin
1587 Indx := First_Index (T);
1588 while Present (Indx) loop
1589 Old_Lo := Type_Low_Bound (Etype (Indx));
1590 Old_Hi := Type_High_Bound (Etype (Indx));
1592 if Denotes_Discriminant (Old_Lo) then
1593 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1595 else
1596 Lo := New_Copy_Tree (Old_Lo);
1597 end if;
1599 if Denotes_Discriminant (Old_Hi) then
1600 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1602 else
1603 Hi := New_Copy_Tree (Old_Hi);
1604 end if;
1606 Append (Make_Range (Loc, Lo, Hi), Constraints);
1607 Next_Index (Indx);
1608 end loop;
1610 return Constraints;
1611 end Build_Discriminal_Array_Constraint;
1613 -----------------------------------------
1614 -- Build_Discriminal_Record_Constraint --
1615 -----------------------------------------
1617 function Build_Discriminal_Record_Constraint return List_Id is
1618 Constraints : constant List_Id := New_List;
1619 D : Elmt_Id;
1620 D_Val : Node_Id;
1622 begin
1623 D := First_Elmt (Discriminant_Constraint (T));
1624 while Present (D) loop
1625 if Denotes_Discriminant (Node (D)) then
1626 D_Val :=
1627 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1628 else
1629 D_Val := New_Copy_Tree (Node (D));
1630 end if;
1632 Append (D_Val, Constraints);
1633 Next_Elmt (D);
1634 end loop;
1636 return Constraints;
1637 end Build_Discriminal_Record_Constraint;
1639 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1641 begin
1642 if Ekind (T) = E_Array_Subtype then
1643 Id := First_Index (T);
1644 while Present (Id) loop
1645 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1646 or else
1647 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1648 then
1649 return Build_Component_Subtype
1650 (Build_Discriminal_Array_Constraint, Loc, T);
1651 end if;
1653 Next_Index (Id);
1654 end loop;
1656 elsif Ekind (T) = E_Record_Subtype
1657 and then Has_Discriminants (T)
1658 and then not Has_Unknown_Discriminants (T)
1659 then
1660 D := First_Elmt (Discriminant_Constraint (T));
1661 while Present (D) loop
1662 if Denotes_Discriminant (Node (D)) then
1663 return Build_Component_Subtype
1664 (Build_Discriminal_Record_Constraint, Loc, T);
1665 end if;
1667 Next_Elmt (D);
1668 end loop;
1669 end if;
1671 -- If none of the above, the actual and nominal subtypes are the same
1673 return Empty;
1674 end Build_Discriminal_Subtype_Of_Component;
1676 ------------------------------
1677 -- Build_Elaboration_Entity --
1678 ------------------------------
1680 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1681 Loc : constant Source_Ptr := Sloc (N);
1682 Decl : Node_Id;
1683 Elab_Ent : Entity_Id;
1685 procedure Set_Package_Name (Ent : Entity_Id);
1686 -- Given an entity, sets the fully qualified name of the entity in
1687 -- Name_Buffer, with components separated by double underscores. This
1688 -- is a recursive routine that climbs the scope chain to Standard.
1690 ----------------------
1691 -- Set_Package_Name --
1692 ----------------------
1694 procedure Set_Package_Name (Ent : Entity_Id) is
1695 begin
1696 if Scope (Ent) /= Standard_Standard then
1697 Set_Package_Name (Scope (Ent));
1699 declare
1700 Nam : constant String := Get_Name_String (Chars (Ent));
1701 begin
1702 Name_Buffer (Name_Len + 1) := '_';
1703 Name_Buffer (Name_Len + 2) := '_';
1704 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1705 Name_Len := Name_Len + Nam'Length + 2;
1706 end;
1708 else
1709 Get_Name_String (Chars (Ent));
1710 end if;
1711 end Set_Package_Name;
1713 -- Start of processing for Build_Elaboration_Entity
1715 begin
1716 -- Ignore call if already constructed
1718 if Present (Elaboration_Entity (Spec_Id)) then
1719 return;
1721 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1722 -- no role in analysis.
1724 elsif ASIS_Mode then
1725 return;
1727 -- See if we need elaboration entity. We always need it for the dynamic
1728 -- elaboration model, since it is needed to properly generate the PE
1729 -- exception for access before elaboration.
1731 elsif Dynamic_Elaboration_Checks then
1732 null;
1734 -- For the static model, we don't need the elaboration counter if this
1735 -- unit is sure to have no elaboration code, since that means there
1736 -- is no elaboration unit to be called. Note that we can't just decide
1737 -- after the fact by looking to see whether there was elaboration code,
1738 -- because that's too late to make this decision.
1740 elsif Restriction_Active (No_Elaboration_Code) then
1741 return;
1743 -- Similarly, for the static model, we can skip the elaboration counter
1744 -- if we have the No_Multiple_Elaboration restriction, since for the
1745 -- static model, that's the only purpose of the counter (to avoid
1746 -- multiple elaboration).
1748 elsif Restriction_Active (No_Multiple_Elaboration) then
1749 return;
1750 end if;
1752 -- Here we need the elaboration entity
1754 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1755 -- name with dots replaced by double underscore. We have to manually
1756 -- construct this name, since it will be elaborated in the outer scope,
1757 -- and thus will not have the unit name automatically prepended.
1759 Set_Package_Name (Spec_Id);
1760 Add_Str_To_Name_Buffer ("_E");
1762 -- Create elaboration counter
1764 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1765 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1767 Decl :=
1768 Make_Object_Declaration (Loc,
1769 Defining_Identifier => Elab_Ent,
1770 Object_Definition =>
1771 New_Occurrence_Of (Standard_Short_Integer, Loc),
1772 Expression => Make_Integer_Literal (Loc, Uint_0));
1774 Push_Scope (Standard_Standard);
1775 Add_Global_Declaration (Decl);
1776 Pop_Scope;
1778 -- Reset True_Constant indication, since we will indeed assign a value
1779 -- to the variable in the binder main. We also kill the Current_Value
1780 -- and Last_Assignment fields for the same reason.
1782 Set_Is_True_Constant (Elab_Ent, False);
1783 Set_Current_Value (Elab_Ent, Empty);
1784 Set_Last_Assignment (Elab_Ent, Empty);
1786 -- We do not want any further qualification of the name (if we did not
1787 -- do this, we would pick up the name of the generic package in the case
1788 -- of a library level generic instantiation).
1790 Set_Has_Qualified_Name (Elab_Ent);
1791 Set_Has_Fully_Qualified_Name (Elab_Ent);
1792 end Build_Elaboration_Entity;
1794 --------------------------------
1795 -- Build_Explicit_Dereference --
1796 --------------------------------
1798 procedure Build_Explicit_Dereference
1799 (Expr : Node_Id;
1800 Disc : Entity_Id)
1802 Loc : constant Source_Ptr := Sloc (Expr);
1804 begin
1805 -- An entity of a type with a reference aspect is overloaded with
1806 -- both interpretations: with and without the dereference. Now that
1807 -- the dereference is made explicit, set the type of the node properly,
1808 -- to prevent anomalies in the backend. Same if the expression is an
1809 -- overloaded function call whose return type has a reference aspect.
1811 if Is_Entity_Name (Expr) then
1812 Set_Etype (Expr, Etype (Entity (Expr)));
1814 elsif Nkind (Expr) = N_Function_Call then
1815 Set_Etype (Expr, Etype (Name (Expr)));
1816 end if;
1818 Set_Is_Overloaded (Expr, False);
1820 -- The expression will often be a generalized indexing that yields a
1821 -- container element that is then dereferenced, in which case the
1822 -- generalized indexing call is also non-overloaded.
1824 if Nkind (Expr) = N_Indexed_Component
1825 and then Present (Generalized_Indexing (Expr))
1826 then
1827 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1828 end if;
1830 Rewrite (Expr,
1831 Make_Explicit_Dereference (Loc,
1832 Prefix =>
1833 Make_Selected_Component (Loc,
1834 Prefix => Relocate_Node (Expr),
1835 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1836 Set_Etype (Prefix (Expr), Etype (Disc));
1837 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1838 end Build_Explicit_Dereference;
1840 -----------------------------------
1841 -- Cannot_Raise_Constraint_Error --
1842 -----------------------------------
1844 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1845 begin
1846 if Compile_Time_Known_Value (Expr) then
1847 return True;
1849 elsif Do_Range_Check (Expr) then
1850 return False;
1852 elsif Raises_Constraint_Error (Expr) then
1853 return False;
1855 else
1856 case Nkind (Expr) is
1857 when N_Identifier =>
1858 return True;
1860 when N_Expanded_Name =>
1861 return True;
1863 when N_Selected_Component =>
1864 return not Do_Discriminant_Check (Expr);
1866 when N_Attribute_Reference =>
1867 if Do_Overflow_Check (Expr) then
1868 return False;
1870 elsif No (Expressions (Expr)) then
1871 return True;
1873 else
1874 declare
1875 N : Node_Id;
1877 begin
1878 N := First (Expressions (Expr));
1879 while Present (N) loop
1880 if Cannot_Raise_Constraint_Error (N) then
1881 Next (N);
1882 else
1883 return False;
1884 end if;
1885 end loop;
1887 return True;
1888 end;
1889 end if;
1891 when N_Type_Conversion =>
1892 if Do_Overflow_Check (Expr)
1893 or else Do_Length_Check (Expr)
1894 or else Do_Tag_Check (Expr)
1895 then
1896 return False;
1897 else
1898 return Cannot_Raise_Constraint_Error (Expression (Expr));
1899 end if;
1901 when N_Unchecked_Type_Conversion =>
1902 return Cannot_Raise_Constraint_Error (Expression (Expr));
1904 when N_Unary_Op =>
1905 if Do_Overflow_Check (Expr) then
1906 return False;
1907 else
1908 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1909 end if;
1911 when N_Op_Divide |
1912 N_Op_Mod |
1913 N_Op_Rem
1915 if Do_Division_Check (Expr)
1916 or else
1917 Do_Overflow_Check (Expr)
1918 then
1919 return False;
1920 else
1921 return
1922 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1923 and then
1924 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1925 end if;
1927 when N_Op_Add |
1928 N_Op_And |
1929 N_Op_Concat |
1930 N_Op_Eq |
1931 N_Op_Expon |
1932 N_Op_Ge |
1933 N_Op_Gt |
1934 N_Op_Le |
1935 N_Op_Lt |
1936 N_Op_Multiply |
1937 N_Op_Ne |
1938 N_Op_Or |
1939 N_Op_Rotate_Left |
1940 N_Op_Rotate_Right |
1941 N_Op_Shift_Left |
1942 N_Op_Shift_Right |
1943 N_Op_Shift_Right_Arithmetic |
1944 N_Op_Subtract |
1945 N_Op_Xor
1947 if Do_Overflow_Check (Expr) then
1948 return False;
1949 else
1950 return
1951 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1952 and then
1953 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1954 end if;
1956 when others =>
1957 return False;
1958 end case;
1959 end if;
1960 end Cannot_Raise_Constraint_Error;
1962 -----------------------------------------
1963 -- Check_Dynamically_Tagged_Expression --
1964 -----------------------------------------
1966 procedure Check_Dynamically_Tagged_Expression
1967 (Expr : Node_Id;
1968 Typ : Entity_Id;
1969 Related_Nod : Node_Id)
1971 begin
1972 pragma Assert (Is_Tagged_Type (Typ));
1974 -- In order to avoid spurious errors when analyzing the expanded code,
1975 -- this check is done only for nodes that come from source and for
1976 -- actuals of generic instantiations.
1978 if (Comes_From_Source (Related_Nod)
1979 or else In_Generic_Actual (Expr))
1980 and then (Is_Class_Wide_Type (Etype (Expr))
1981 or else Is_Dynamically_Tagged (Expr))
1982 and then Is_Tagged_Type (Typ)
1983 and then not Is_Class_Wide_Type (Typ)
1984 then
1985 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
1986 end if;
1987 end Check_Dynamically_Tagged_Expression;
1989 --------------------------
1990 -- Check_Fully_Declared --
1991 --------------------------
1993 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
1994 begin
1995 if Ekind (T) = E_Incomplete_Type then
1997 -- Ada 2005 (AI-50217): If the type is available through a limited
1998 -- with_clause, verify that its full view has been analyzed.
2000 if From_Limited_With (T)
2001 and then Present (Non_Limited_View (T))
2002 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2003 then
2004 -- The non-limited view is fully declared
2006 null;
2008 else
2009 Error_Msg_NE
2010 ("premature usage of incomplete}", N, First_Subtype (T));
2011 end if;
2013 -- Need comments for these tests ???
2015 elsif Has_Private_Component (T)
2016 and then not Is_Generic_Type (Root_Type (T))
2017 and then not In_Spec_Expression
2018 then
2019 -- Special case: if T is the anonymous type created for a single
2020 -- task or protected object, use the name of the source object.
2022 if Is_Concurrent_Type (T)
2023 and then not Comes_From_Source (T)
2024 and then Nkind (N) = N_Object_Declaration
2025 then
2026 Error_Msg_NE
2027 ("type of& has incomplete component",
2028 N, Defining_Identifier (N));
2029 else
2030 Error_Msg_NE
2031 ("premature usage of incomplete}",
2032 N, First_Subtype (T));
2033 end if;
2034 end if;
2035 end Check_Fully_Declared;
2037 -------------------------------------
2038 -- Check_Function_Writable_Actuals --
2039 -------------------------------------
2041 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2042 Writable_Actuals_List : Elist_Id := No_Elist;
2043 Identifiers_List : Elist_Id := No_Elist;
2044 Error_Node : Node_Id := Empty;
2046 procedure Collect_Identifiers (N : Node_Id);
2047 -- In a single traversal of subtree N collect in Writable_Actuals_List
2048 -- all the actuals of functions with writable actuals, and in the list
2049 -- Identifiers_List collect all the identifiers that are not actuals of
2050 -- functions with writable actuals. If a writable actual is referenced
2051 -- twice as writable actual then Error_Node is set to reference its
2052 -- second occurrence, the error is reported, and the tree traversal
2053 -- is abandoned.
2055 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2056 -- Return the entity associated with the function call
2058 procedure Preanalyze_Without_Errors (N : Node_Id);
2059 -- Preanalyze N without reporting errors. Very dubious, you can't just
2060 -- go analyzing things more than once???
2062 -------------------------
2063 -- Collect_Identifiers --
2064 -------------------------
2066 procedure Collect_Identifiers (N : Node_Id) is
2068 function Check_Node (N : Node_Id) return Traverse_Result;
2069 -- Process a single node during the tree traversal to collect the
2070 -- writable actuals of functions and all the identifiers which are
2071 -- not writable actuals of functions.
2073 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2074 -- Returns True if List has a node whose Entity is Entity (N)
2076 -------------------------
2077 -- Check_Function_Call --
2078 -------------------------
2080 function Check_Node (N : Node_Id) return Traverse_Result is
2081 Is_Writable_Actual : Boolean := False;
2082 Id : Entity_Id;
2084 begin
2085 if Nkind (N) = N_Identifier then
2087 -- No analysis possible if the entity is not decorated
2089 if No (Entity (N)) then
2090 return Skip;
2092 -- Don't collect identifiers of packages, called functions, etc
2094 elsif Ekind_In (Entity (N), E_Package,
2095 E_Function,
2096 E_Procedure,
2097 E_Entry)
2098 then
2099 return Skip;
2101 -- Analyze if N is a writable actual of a function
2103 elsif Nkind (Parent (N)) = N_Function_Call then
2104 declare
2105 Call : constant Node_Id := Parent (N);
2106 Actual : Node_Id;
2107 Formal : Node_Id;
2109 begin
2110 Id := Get_Function_Id (Call);
2112 -- In case of previous error, no check is possible
2114 if No (Id) then
2115 return Abandon;
2116 end if;
2118 Formal := First_Formal (Id);
2119 Actual := First_Actual (Call);
2120 while Present (Actual) and then Present (Formal) loop
2121 if Actual = N then
2122 if Ekind_In (Formal, E_Out_Parameter,
2123 E_In_Out_Parameter)
2124 then
2125 Is_Writable_Actual := True;
2126 end if;
2128 exit;
2129 end if;
2131 Next_Formal (Formal);
2132 Next_Actual (Actual);
2133 end loop;
2134 end;
2135 end if;
2137 if Is_Writable_Actual then
2138 if Contains (Writable_Actuals_List, N) then
2139 Error_Msg_NE
2140 ("value may be affected by call to& "
2141 & "because order of evaluation is arbitrary", N, Id);
2142 Error_Node := N;
2143 return Abandon;
2144 end if;
2146 Append_New_Elmt (N, To => Writable_Actuals_List);
2148 else
2149 if Identifiers_List = No_Elist then
2150 Identifiers_List := New_Elmt_List;
2151 end if;
2153 Append_Unique_Elmt (N, Identifiers_List);
2154 end if;
2155 end if;
2157 return OK;
2158 end Check_Node;
2160 --------------
2161 -- Contains --
2162 --------------
2164 function Contains
2165 (List : Elist_Id;
2166 N : Node_Id) return Boolean
2168 pragma Assert (Nkind (N) in N_Has_Entity);
2170 Elmt : Elmt_Id;
2172 begin
2173 if List = No_Elist then
2174 return False;
2175 end if;
2177 Elmt := First_Elmt (List);
2178 while Present (Elmt) loop
2179 if Entity (Node (Elmt)) = Entity (N) then
2180 return True;
2181 else
2182 Next_Elmt (Elmt);
2183 end if;
2184 end loop;
2186 return False;
2187 end Contains;
2189 ------------------
2190 -- Do_Traversal --
2191 ------------------
2193 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2194 -- The traversal procedure
2196 -- Start of processing for Collect_Identifiers
2198 begin
2199 if Present (Error_Node) then
2200 return;
2201 end if;
2203 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2204 return;
2205 end if;
2207 Do_Traversal (N);
2208 end Collect_Identifiers;
2210 ---------------------
2211 -- Get_Function_Id --
2212 ---------------------
2214 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2215 Nam : constant Node_Id := Name (Call);
2216 Id : Entity_Id;
2218 begin
2219 if Nkind (Nam) = N_Explicit_Dereference then
2220 Id := Etype (Nam);
2221 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2223 elsif Nkind (Nam) = N_Selected_Component then
2224 Id := Entity (Selector_Name (Nam));
2226 elsif Nkind (Nam) = N_Indexed_Component then
2227 Id := Entity (Selector_Name (Prefix (Nam)));
2229 else
2230 Id := Entity (Nam);
2231 end if;
2233 return Id;
2234 end Get_Function_Id;
2236 ---------------------------
2237 -- Preanalyze_Expression --
2238 ---------------------------
2240 procedure Preanalyze_Without_Errors (N : Node_Id) is
2241 Status : constant Boolean := Get_Ignore_Errors;
2242 begin
2243 Set_Ignore_Errors (True);
2244 Preanalyze (N);
2245 Set_Ignore_Errors (Status);
2246 end Preanalyze_Without_Errors;
2248 -- Start of processing for Check_Function_Writable_Actuals
2250 begin
2251 -- The check only applies to Ada 2012 code, and only to constructs that
2252 -- have multiple constituents whose order of evaluation is not specified
2253 -- by the language.
2255 if Ada_Version < Ada_2012
2256 or else (not (Nkind (N) in N_Op)
2257 and then not (Nkind (N) in N_Membership_Test)
2258 and then not Nkind_In (N, N_Range,
2259 N_Aggregate,
2260 N_Extension_Aggregate,
2261 N_Full_Type_Declaration,
2262 N_Function_Call,
2263 N_Procedure_Call_Statement,
2264 N_Entry_Call_Statement))
2265 or else (Nkind (N) = N_Full_Type_Declaration
2266 and then not Is_Record_Type (Defining_Identifier (N)))
2268 -- In addition, this check only applies to source code, not to code
2269 -- generated by constraint checks.
2271 or else not Comes_From_Source (N)
2272 then
2273 return;
2274 end if;
2276 -- If a construct C has two or more direct constituents that are names
2277 -- or expressions whose evaluation may occur in an arbitrary order, at
2278 -- least one of which contains a function call with an in out or out
2279 -- parameter, then the construct is legal only if: for each name N that
2280 -- is passed as a parameter of mode in out or out to some inner function
2281 -- call C2 (not including the construct C itself), there is no other
2282 -- name anywhere within a direct constituent of the construct C other
2283 -- than the one containing C2, that is known to refer to the same
2284 -- object (RM 6.4.1(6.17/3)).
2286 case Nkind (N) is
2287 when N_Range =>
2288 Collect_Identifiers (Low_Bound (N));
2289 Collect_Identifiers (High_Bound (N));
2291 when N_Op | N_Membership_Test =>
2292 declare
2293 Expr : Node_Id;
2295 begin
2296 Collect_Identifiers (Left_Opnd (N));
2298 if Present (Right_Opnd (N)) then
2299 Collect_Identifiers (Right_Opnd (N));
2300 end if;
2302 if Nkind_In (N, N_In, N_Not_In)
2303 and then Present (Alternatives (N))
2304 then
2305 Expr := First (Alternatives (N));
2306 while Present (Expr) loop
2307 Collect_Identifiers (Expr);
2309 Next (Expr);
2310 end loop;
2311 end if;
2312 end;
2314 when N_Full_Type_Declaration =>
2315 declare
2316 function Get_Record_Part (N : Node_Id) return Node_Id;
2317 -- Return the record part of this record type definition
2319 function Get_Record_Part (N : Node_Id) return Node_Id is
2320 Type_Def : constant Node_Id := Type_Definition (N);
2321 begin
2322 if Nkind (Type_Def) = N_Derived_Type_Definition then
2323 return Record_Extension_Part (Type_Def);
2324 else
2325 return Type_Def;
2326 end if;
2327 end Get_Record_Part;
2329 Comp : Node_Id;
2330 Def_Id : Entity_Id := Defining_Identifier (N);
2331 Rec : Node_Id := Get_Record_Part (N);
2333 begin
2334 -- No need to perform any analysis if the record has no
2335 -- components
2337 if No (Rec) or else No (Component_List (Rec)) then
2338 return;
2339 end if;
2341 -- Collect the identifiers starting from the deepest
2342 -- derivation. Done to report the error in the deepest
2343 -- derivation.
2345 loop
2346 if Present (Component_List (Rec)) then
2347 Comp := First (Component_Items (Component_List (Rec)));
2348 while Present (Comp) loop
2349 if Nkind (Comp) = N_Component_Declaration
2350 and then Present (Expression (Comp))
2351 then
2352 Collect_Identifiers (Expression (Comp));
2353 end if;
2355 Next (Comp);
2356 end loop;
2357 end if;
2359 exit when No (Underlying_Type (Etype (Def_Id)))
2360 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2361 = Def_Id;
2363 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2364 Rec := Get_Record_Part (Parent (Def_Id));
2365 end loop;
2366 end;
2368 when N_Subprogram_Call |
2369 N_Entry_Call_Statement =>
2370 declare
2371 Id : constant Entity_Id := Get_Function_Id (N);
2372 Formal : Node_Id;
2373 Actual : Node_Id;
2375 begin
2376 Formal := First_Formal (Id);
2377 Actual := First_Actual (N);
2378 while Present (Actual) and then Present (Formal) loop
2379 if Ekind_In (Formal, E_Out_Parameter,
2380 E_In_Out_Parameter)
2381 then
2382 Collect_Identifiers (Actual);
2383 end if;
2385 Next_Formal (Formal);
2386 Next_Actual (Actual);
2387 end loop;
2388 end;
2390 when N_Aggregate |
2391 N_Extension_Aggregate =>
2392 declare
2393 Assoc : Node_Id;
2394 Choice : Node_Id;
2395 Comp_Expr : Node_Id;
2397 begin
2398 -- Handle the N_Others_Choice of array aggregates with static
2399 -- bounds. There is no need to perform this analysis in
2400 -- aggregates without static bounds since we cannot evaluate
2401 -- if the N_Others_Choice covers several elements. There is
2402 -- no need to handle the N_Others choice of record aggregates
2403 -- since at this stage it has been already expanded by
2404 -- Resolve_Record_Aggregate.
2406 if Is_Array_Type (Etype (N))
2407 and then Nkind (N) = N_Aggregate
2408 and then Present (Aggregate_Bounds (N))
2409 and then Compile_Time_Known_Bounds (Etype (N))
2410 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2412 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2413 then
2414 declare
2415 Count_Components : Uint := Uint_0;
2416 Num_Components : Uint;
2417 Others_Assoc : Node_Id;
2418 Others_Choice : Node_Id := Empty;
2419 Others_Box_Present : Boolean := False;
2421 begin
2422 -- Count positional associations
2424 if Present (Expressions (N)) then
2425 Comp_Expr := First (Expressions (N));
2426 while Present (Comp_Expr) loop
2427 Count_Components := Count_Components + 1;
2428 Next (Comp_Expr);
2429 end loop;
2430 end if;
2432 -- Count the rest of elements and locate the N_Others
2433 -- choice (if any)
2435 Assoc := First (Component_Associations (N));
2436 while Present (Assoc) loop
2437 Choice := First (Choices (Assoc));
2438 while Present (Choice) loop
2439 if Nkind (Choice) = N_Others_Choice then
2440 Others_Assoc := Assoc;
2441 Others_Choice := Choice;
2442 Others_Box_Present := Box_Present (Assoc);
2444 -- Count several components
2446 elsif Nkind_In (Choice, N_Range,
2447 N_Subtype_Indication)
2448 or else (Is_Entity_Name (Choice)
2449 and then Is_Type (Entity (Choice)))
2450 then
2451 declare
2452 L, H : Node_Id;
2453 begin
2454 Get_Index_Bounds (Choice, L, H);
2455 pragma Assert
2456 (Compile_Time_Known_Value (L)
2457 and then Compile_Time_Known_Value (H));
2458 Count_Components :=
2459 Count_Components
2460 + Expr_Value (H) - Expr_Value (L) + 1;
2461 end;
2463 -- Count single component. No other case available
2464 -- since we are handling an aggregate with static
2465 -- bounds.
2467 else
2468 pragma Assert (Is_OK_Static_Expression (Choice)
2469 or else Nkind (Choice) = N_Identifier
2470 or else Nkind (Choice) = N_Integer_Literal);
2472 Count_Components := Count_Components + 1;
2473 end if;
2475 Next (Choice);
2476 end loop;
2478 Next (Assoc);
2479 end loop;
2481 Num_Components :=
2482 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2483 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2485 pragma Assert (Count_Components <= Num_Components);
2487 -- Handle the N_Others choice if it covers several
2488 -- components
2490 if Present (Others_Choice)
2491 and then (Num_Components - Count_Components) > 1
2492 then
2493 if not Others_Box_Present then
2495 -- At this stage, if expansion is active, the
2496 -- expression of the others choice has not been
2497 -- analyzed. Hence we generate a duplicate and
2498 -- we analyze it silently to have available the
2499 -- minimum decoration required to collect the
2500 -- identifiers.
2502 if not Expander_Active then
2503 Comp_Expr := Expression (Others_Assoc);
2504 else
2505 Comp_Expr :=
2506 New_Copy_Tree (Expression (Others_Assoc));
2507 Preanalyze_Without_Errors (Comp_Expr);
2508 end if;
2510 Collect_Identifiers (Comp_Expr);
2512 if Writable_Actuals_List /= No_Elist then
2514 -- As suggested by Robert, at current stage we
2515 -- report occurrences of this case as warnings.
2517 Error_Msg_N
2518 ("writable function parameter may affect "
2519 & "value in other component because order "
2520 & "of evaluation is unspecified??",
2521 Node (First_Elmt (Writable_Actuals_List)));
2522 end if;
2523 end if;
2524 end if;
2525 end;
2526 end if;
2528 -- Handle ancestor part of extension aggregates
2530 if Nkind (N) = N_Extension_Aggregate then
2531 Collect_Identifiers (Ancestor_Part (N));
2532 end if;
2534 -- Handle positional associations
2536 if Present (Expressions (N)) then
2537 Comp_Expr := First (Expressions (N));
2538 while Present (Comp_Expr) loop
2539 if not Is_OK_Static_Expression (Comp_Expr) then
2540 Collect_Identifiers (Comp_Expr);
2541 end if;
2543 Next (Comp_Expr);
2544 end loop;
2545 end if;
2547 -- Handle discrete associations
2549 if Present (Component_Associations (N)) then
2550 Assoc := First (Component_Associations (N));
2551 while Present (Assoc) loop
2553 if not Box_Present (Assoc) then
2554 Choice := First (Choices (Assoc));
2555 while Present (Choice) loop
2557 -- For now we skip discriminants since it requires
2558 -- performing the analysis in two phases: first one
2559 -- analyzing discriminants and second one analyzing
2560 -- the rest of components since discriminants are
2561 -- evaluated prior to components: too much extra
2562 -- work to detect a corner case???
2564 if Nkind (Choice) in N_Has_Entity
2565 and then Present (Entity (Choice))
2566 and then Ekind (Entity (Choice)) = E_Discriminant
2567 then
2568 null;
2570 elsif Box_Present (Assoc) then
2571 null;
2573 else
2574 if not Analyzed (Expression (Assoc)) then
2575 Comp_Expr :=
2576 New_Copy_Tree (Expression (Assoc));
2577 Set_Parent (Comp_Expr, Parent (N));
2578 Preanalyze_Without_Errors (Comp_Expr);
2579 else
2580 Comp_Expr := Expression (Assoc);
2581 end if;
2583 Collect_Identifiers (Comp_Expr);
2584 end if;
2586 Next (Choice);
2587 end loop;
2588 end if;
2590 Next (Assoc);
2591 end loop;
2592 end if;
2593 end;
2595 when others =>
2596 return;
2597 end case;
2599 -- No further action needed if we already reported an error
2601 if Present (Error_Node) then
2602 return;
2603 end if;
2605 -- Check if some writable argument of a function is referenced
2607 if Writable_Actuals_List /= No_Elist
2608 and then Identifiers_List /= No_Elist
2609 then
2610 declare
2611 Elmt_1 : Elmt_Id;
2612 Elmt_2 : Elmt_Id;
2614 begin
2615 Elmt_1 := First_Elmt (Writable_Actuals_List);
2616 while Present (Elmt_1) loop
2617 Elmt_2 := First_Elmt (Identifiers_List);
2618 while Present (Elmt_2) loop
2619 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2620 case Nkind (Parent (Node (Elmt_2))) is
2621 when N_Aggregate |
2622 N_Component_Association |
2623 N_Component_Declaration =>
2624 Error_Msg_N
2625 ("value may be affected by call in other "
2626 & "component because they are evaluated "
2627 & "in unspecified order",
2628 Node (Elmt_2));
2630 when N_In | N_Not_In =>
2631 Error_Msg_N
2632 ("value may be affected by call in other "
2633 & "alternative because they are evaluated "
2634 & "in unspecified order",
2635 Node (Elmt_2));
2637 when others =>
2638 Error_Msg_N
2639 ("value of actual may be affected by call in "
2640 & "other actual because they are evaluated "
2641 & "in unspecified order",
2642 Node (Elmt_2));
2643 end case;
2644 end if;
2646 Next_Elmt (Elmt_2);
2647 end loop;
2649 Next_Elmt (Elmt_1);
2650 end loop;
2651 end;
2652 end if;
2653 end Check_Function_Writable_Actuals;
2655 --------------------------------
2656 -- Check_Implicit_Dereference --
2657 --------------------------------
2659 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2660 Disc : Entity_Id;
2661 Desig : Entity_Id;
2662 Nam : Node_Id;
2664 begin
2665 if Nkind (N) = N_Indexed_Component
2666 and then Present (Generalized_Indexing (N))
2667 then
2668 Nam := Generalized_Indexing (N);
2669 else
2670 Nam := N;
2671 end if;
2673 if Ada_Version < Ada_2012
2674 or else not Has_Implicit_Dereference (Base_Type (Typ))
2675 then
2676 return;
2678 elsif not Comes_From_Source (N)
2679 and then Nkind (N) /= N_Indexed_Component
2680 then
2681 return;
2683 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2684 null;
2686 else
2687 Disc := First_Discriminant (Typ);
2688 while Present (Disc) loop
2689 if Has_Implicit_Dereference (Disc) then
2690 Desig := Designated_Type (Etype (Disc));
2691 Add_One_Interp (Nam, Disc, Desig);
2693 -- If the node is a generalized indexing, add interpretation
2694 -- to that node as well, for subsequent resolution.
2696 if Nkind (N) = N_Indexed_Component then
2697 Add_One_Interp (N, Disc, Desig);
2698 end if;
2700 -- If the operation comes from a generic unit and the context
2701 -- is a selected component, the selector name may be global
2702 -- and set in the instance already. Remove the entity to
2703 -- force resolution of the selected component, and the
2704 -- generation of an explicit dereference if needed.
2706 if In_Instance
2707 and then Nkind (Parent (Nam)) = N_Selected_Component
2708 then
2709 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2710 end if;
2712 exit;
2713 end if;
2715 Next_Discriminant (Disc);
2716 end loop;
2717 end if;
2718 end Check_Implicit_Dereference;
2720 ----------------------------------
2721 -- Check_Internal_Protected_Use --
2722 ----------------------------------
2724 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2725 S : Entity_Id;
2726 Prot : Entity_Id;
2728 begin
2729 S := Current_Scope;
2730 while Present (S) loop
2731 if S = Standard_Standard then
2732 return;
2734 elsif Ekind (S) = E_Function
2735 and then Ekind (Scope (S)) = E_Protected_Type
2736 then
2737 Prot := Scope (S);
2738 exit;
2739 end if;
2741 S := Scope (S);
2742 end loop;
2744 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2746 -- An indirect function call (e.g. a callback within a protected
2747 -- function body) is not statically illegal. If the access type is
2748 -- anonymous and is the type of an access parameter, the scope of Nam
2749 -- will be the protected type, but it is not a protected operation.
2751 if Ekind (Nam) = E_Subprogram_Type
2752 and then
2753 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2754 then
2755 null;
2757 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2758 Error_Msg_N
2759 ("within protected function cannot use protected "
2760 & "procedure in renaming or as generic actual", N);
2762 elsif Nkind (N) = N_Attribute_Reference then
2763 Error_Msg_N
2764 ("within protected function cannot take access of "
2765 & " protected procedure", N);
2767 else
2768 Error_Msg_N
2769 ("within protected function, protected object is constant", N);
2770 Error_Msg_N
2771 ("\cannot call operation that may modify it", N);
2772 end if;
2773 end if;
2774 end Check_Internal_Protected_Use;
2776 ---------------------------------------
2777 -- Check_Later_Vs_Basic_Declarations --
2778 ---------------------------------------
2780 procedure Check_Later_Vs_Basic_Declarations
2781 (Decls : List_Id;
2782 During_Parsing : Boolean)
2784 Body_Sloc : Source_Ptr;
2785 Decl : Node_Id;
2787 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2788 -- Return whether Decl is considered as a declarative item.
2789 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2790 -- When During_Parsing is False, the semantics of SPARK is followed.
2792 -------------------------------
2793 -- Is_Later_Declarative_Item --
2794 -------------------------------
2796 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2797 begin
2798 if Nkind (Decl) in N_Later_Decl_Item then
2799 return True;
2801 elsif Nkind (Decl) = N_Pragma then
2802 return True;
2804 elsif During_Parsing then
2805 return False;
2807 -- In SPARK, a package declaration is not considered as a later
2808 -- declarative item.
2810 elsif Nkind (Decl) = N_Package_Declaration then
2811 return False;
2813 -- In SPARK, a renaming is considered as a later declarative item
2815 elsif Nkind (Decl) in N_Renaming_Declaration then
2816 return True;
2818 else
2819 return False;
2820 end if;
2821 end Is_Later_Declarative_Item;
2823 -- Start of Check_Later_Vs_Basic_Declarations
2825 begin
2826 Decl := First (Decls);
2828 -- Loop through sequence of basic declarative items
2830 Outer : while Present (Decl) loop
2831 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
2832 and then Nkind (Decl) not in N_Body_Stub
2833 then
2834 Next (Decl);
2836 -- Once a body is encountered, we only allow later declarative
2837 -- items. The inner loop checks the rest of the list.
2839 else
2840 Body_Sloc := Sloc (Decl);
2842 Inner : while Present (Decl) loop
2843 if not Is_Later_Declarative_Item (Decl) then
2844 if During_Parsing then
2845 if Ada_Version = Ada_83 then
2846 Error_Msg_Sloc := Body_Sloc;
2847 Error_Msg_N
2848 ("(Ada 83) decl cannot appear after body#", Decl);
2849 end if;
2850 else
2851 Error_Msg_Sloc := Body_Sloc;
2852 Check_SPARK_05_Restriction
2853 ("decl cannot appear after body#", Decl);
2854 end if;
2855 end if;
2857 Next (Decl);
2858 end loop Inner;
2859 end if;
2860 end loop Outer;
2861 end Check_Later_Vs_Basic_Declarations;
2863 -------------------------
2864 -- Check_Nested_Access --
2865 -------------------------
2867 procedure Check_Nested_Access (N : Node_Id; Ent : Entity_Id) is
2868 Scop : constant Entity_Id := Current_Scope;
2869 Current_Subp : Entity_Id;
2870 Enclosing : Entity_Id;
2872 begin
2873 -- Currently only enabled for VM back-ends for efficiency, should we
2874 -- enable it more systematically? Probably not unless someone actually
2875 -- needs it. It will be needed for C generation and is activated if the
2876 -- Opt.Unnest_Subprogram_Mode flag is set True.
2878 if (VM_Target /= No_VM or else Unnest_Subprogram_Mode)
2879 and then Scope (Ent) /= Empty
2880 and then not Is_Library_Level_Entity (Ent)
2882 -- Comment the exclusion of imported entities ???
2884 and then not Is_Imported (Ent)
2885 then
2886 -- For VM case, we are only interested in variables, constants,
2887 -- and loop parameters. For general nested procedure usage, we
2888 -- allow types as well.
2890 if Ekind_In (Ent, E_Variable, E_Constant, E_Loop_Parameter) then
2891 null;
2892 elsif not (Unnest_Subprogram_Mode and then Is_Type (Ent)) then
2893 return;
2894 end if;
2896 -- Get current subprogram that is relevant
2898 if Is_Subprogram (Scop)
2899 or else Is_Generic_Subprogram (Scop)
2900 or else Is_Entry (Scop)
2901 then
2902 Current_Subp := Scop;
2903 else
2904 Current_Subp := Current_Subprogram;
2905 end if;
2907 Enclosing := Enclosing_Subprogram (Ent);
2909 -- Set flag if uplevel reference
2911 if Enclosing /= Empty and then Enclosing /= Current_Subp then
2912 if Is_Type (Ent) then
2913 Check_Uplevel_Reference_To_Type (Ent);
2914 else
2915 Set_Has_Uplevel_Reference (Ent, True);
2917 if Unnest_Subprogram_Mode then
2918 Set_Has_Uplevel_Reference (Current_Subp, True);
2919 Note_Uplevel_Reference (N, Enclosing);
2920 end if;
2921 end if;
2922 end if;
2923 end if;
2924 end Check_Nested_Access;
2926 ---------------------------
2927 -- Check_No_Hidden_State --
2928 ---------------------------
2930 procedure Check_No_Hidden_State (Id : Entity_Id) is
2931 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
2932 -- Determine whether the entity of a package denoted by Pkg has a null
2933 -- abstract state.
2935 -----------------------------
2936 -- Has_Null_Abstract_State --
2937 -----------------------------
2939 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
2940 States : constant Elist_Id := Abstract_States (Pkg);
2942 begin
2943 -- Check first available state of related package. A null abstract
2944 -- state always appears as the sole element of the state list.
2946 return
2947 Present (States)
2948 and then Is_Null_State (Node (First_Elmt (States)));
2949 end Has_Null_Abstract_State;
2951 -- Local variables
2953 Context : Entity_Id := Empty;
2954 Not_Visible : Boolean := False;
2955 Scop : Entity_Id;
2957 -- Start of processing for Check_No_Hidden_State
2959 begin
2960 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
2962 -- Find the proper context where the object or state appears
2964 Scop := Scope (Id);
2965 while Present (Scop) loop
2966 Context := Scop;
2968 -- Keep track of the context's visibility
2970 Not_Visible := Not_Visible or else In_Private_Part (Context);
2972 -- Prevent the search from going too far
2974 if Context = Standard_Standard then
2975 return;
2977 -- Objects and states that appear immediately within a subprogram or
2978 -- inside a construct nested within a subprogram do not introduce a
2979 -- hidden state. They behave as local variable declarations.
2981 elsif Is_Subprogram (Context) then
2982 return;
2984 -- When examining a package body, use the entity of the spec as it
2985 -- carries the abstract state declarations.
2987 elsif Ekind (Context) = E_Package_Body then
2988 Context := Spec_Entity (Context);
2989 end if;
2991 -- Stop the traversal when a package subject to a null abstract state
2992 -- has been found.
2994 if Ekind_In (Context, E_Generic_Package, E_Package)
2995 and then Has_Null_Abstract_State (Context)
2996 then
2997 exit;
2998 end if;
3000 Scop := Scope (Scop);
3001 end loop;
3003 -- At this point we know that there is at least one package with a null
3004 -- abstract state in visibility. Emit an error message unconditionally
3005 -- if the entity being processed is a state because the placement of the
3006 -- related package is irrelevant. This is not the case for objects as
3007 -- the intermediate context matters.
3009 if Present (Context)
3010 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3011 then
3012 Error_Msg_N ("cannot introduce hidden state &", Id);
3013 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3014 end if;
3015 end Check_No_Hidden_State;
3017 ------------------------------------------
3018 -- Check_Potentially_Blocking_Operation --
3019 ------------------------------------------
3021 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3022 S : Entity_Id;
3024 begin
3025 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3026 -- When pragma Detect_Blocking is active, the run time will raise
3027 -- Program_Error. Here we only issue a warning, since we generally
3028 -- support the use of potentially blocking operations in the absence
3029 -- of the pragma.
3031 -- Indirect blocking through a subprogram call cannot be diagnosed
3032 -- statically without interprocedural analysis, so we do not attempt
3033 -- to do it here.
3035 S := Scope (Current_Scope);
3036 while Present (S) and then S /= Standard_Standard loop
3037 if Is_Protected_Type (S) then
3038 Error_Msg_N
3039 ("potentially blocking operation in protected operation??", N);
3040 return;
3041 end if;
3043 S := Scope (S);
3044 end loop;
3045 end Check_Potentially_Blocking_Operation;
3047 ---------------------------------
3048 -- Check_Result_And_Post_State --
3049 ---------------------------------
3051 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3052 procedure Check_Result_And_Post_State_In_Pragma
3053 (Prag : Node_Id;
3054 Result_Seen : in out Boolean);
3055 -- Determine whether pragma Prag mentions attribute 'Result and whether
3056 -- the pragma contains an expression that evaluates differently in pre-
3057 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3058 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3060 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3061 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3062 -- formal parameter.
3064 -------------------------------------------
3065 -- Check_Result_And_Post_State_In_Pragma --
3066 -------------------------------------------
3068 procedure Check_Result_And_Post_State_In_Pragma
3069 (Prag : Node_Id;
3070 Result_Seen : in out Boolean)
3072 procedure Check_Expression (Expr : Node_Id);
3073 -- Perform the 'Result and post-state checks on a given expression
3075 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3076 -- Attempt to find attribute 'Result in a subtree denoted by N
3078 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3079 -- Determine whether source node N denotes "True" or "False"
3081 function Mentions_Post_State (N : Node_Id) return Boolean;
3082 -- Determine whether a subtree denoted by N mentions any construct
3083 -- that denotes a post-state.
3085 procedure Check_Function_Result is
3086 new Traverse_Proc (Is_Function_Result);
3088 ----------------------
3089 -- Check_Expression --
3090 ----------------------
3092 procedure Check_Expression (Expr : Node_Id) is
3093 begin
3094 if not Is_Trivial_Boolean (Expr) then
3095 Check_Function_Result (Expr);
3097 if not Mentions_Post_State (Expr) then
3098 if Pragma_Name (Prag) = Name_Contract_Cases then
3099 Error_Msg_NE
3100 ("contract case does not check the outcome of calling "
3101 & "&?T?", Expr, Subp_Id);
3103 elsif Pragma_Name (Prag) = Name_Refined_Post then
3104 Error_Msg_NE
3105 ("refined postcondition does not check the outcome of "
3106 & "calling &?T?", Prag, Subp_Id);
3108 else
3109 Error_Msg_NE
3110 ("postcondition does not check the outcome of calling "
3111 & "&?T?", Prag, Subp_Id);
3112 end if;
3113 end if;
3114 end if;
3115 end Check_Expression;
3117 ------------------------
3118 -- Is_Function_Result --
3119 ------------------------
3121 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3122 begin
3123 if Is_Attribute_Result (N) then
3124 Result_Seen := True;
3125 return Abandon;
3127 -- Continue the traversal
3129 else
3130 return OK;
3131 end if;
3132 end Is_Function_Result;
3134 ------------------------
3135 -- Is_Trivial_Boolean --
3136 ------------------------
3138 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3139 begin
3140 return
3141 Comes_From_Source (N)
3142 and then Is_Entity_Name (N)
3143 and then (Entity (N) = Standard_True
3144 or else
3145 Entity (N) = Standard_False);
3146 end Is_Trivial_Boolean;
3148 -------------------------
3149 -- Mentions_Post_State --
3150 -------------------------
3152 function Mentions_Post_State (N : Node_Id) return Boolean is
3153 Post_State_Seen : Boolean := False;
3155 function Is_Post_State (N : Node_Id) return Traverse_Result;
3156 -- Attempt to find a construct that denotes a post-state. If this
3157 -- is the case, set flag Post_State_Seen.
3159 -------------------
3160 -- Is_Post_State --
3161 -------------------
3163 function Is_Post_State (N : Node_Id) return Traverse_Result is
3164 Ent : Entity_Id;
3166 begin
3167 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3168 Post_State_Seen := True;
3169 return Abandon;
3171 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3172 Ent := Entity (N);
3174 -- The entity may be modifiable through an implicit
3175 -- dereference.
3177 if No (Ent)
3178 or else Ekind (Ent) in Assignable_Kind
3179 or else (Is_Access_Type (Etype (Ent))
3180 and then Nkind (Parent (N)) =
3181 N_Selected_Component)
3182 then
3183 Post_State_Seen := True;
3184 return Abandon;
3185 end if;
3187 elsif Nkind (N) = N_Attribute_Reference then
3188 if Attribute_Name (N) = Name_Old then
3189 return Skip;
3191 elsif Attribute_Name (N) = Name_Result then
3192 Post_State_Seen := True;
3193 return Abandon;
3194 end if;
3195 end if;
3197 return OK;
3198 end Is_Post_State;
3200 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3202 -- Start of processing for Mentions_Post_State
3204 begin
3205 Find_Post_State (N);
3207 return Post_State_Seen;
3208 end Mentions_Post_State;
3210 -- Local variables
3212 Expr : constant Node_Id :=
3213 Get_Pragma_Arg
3214 (First (Pragma_Argument_Associations (Prag)));
3215 Nam : constant Name_Id := Pragma_Name (Prag);
3216 CCase : Node_Id;
3218 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3220 begin
3221 -- Examine all consequences
3223 if Nam = Name_Contract_Cases then
3224 CCase := First (Component_Associations (Expr));
3225 while Present (CCase) loop
3226 Check_Expression (Expression (CCase));
3228 Next (CCase);
3229 end loop;
3231 -- Examine the expression of a postcondition
3233 else pragma Assert (Nam_In (Nam, Name_Postcondition,
3234 Name_Refined_Post));
3235 Check_Expression (Expr);
3236 end if;
3237 end Check_Result_And_Post_State_In_Pragma;
3239 --------------------------
3240 -- Has_In_Out_Parameter --
3241 --------------------------
3243 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
3244 Formal : Entity_Id;
3246 begin
3247 -- Traverse the formals looking for an IN OUT parameter
3249 Formal := First_Formal (Subp_Id);
3250 while Present (Formal) loop
3251 if Ekind (Formal) = E_In_Out_Parameter then
3252 return True;
3253 end if;
3255 Next_Formal (Formal);
3256 end loop;
3258 return False;
3259 end Has_In_Out_Parameter;
3261 -- Local variables
3263 Items : constant Node_Id := Contract (Subp_Id);
3264 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3265 Case_Prag : Node_Id := Empty;
3266 Post_Prag : Node_Id := Empty;
3267 Prag : Node_Id;
3268 Seen_In_Case : Boolean := False;
3269 Seen_In_Post : Boolean := False;
3270 Spec_Id : Entity_Id;
3272 -- Start of processing for Check_Result_And_Post_State
3274 begin
3275 -- The lack of attribute 'Result or a post-state is classified as a
3276 -- suspicious contract. Do not perform the check if the corresponding
3277 -- swich is not set.
3279 if not Warn_On_Suspicious_Contract then
3280 return;
3282 -- Nothing to do if there is no contract
3284 elsif No (Items) then
3285 return;
3286 end if;
3288 -- Retrieve the entity of the subprogram spec (if any)
3290 if Nkind (Subp_Decl) = N_Subprogram_Body
3291 and then Present (Corresponding_Spec (Subp_Decl))
3292 then
3293 Spec_Id := Corresponding_Spec (Subp_Decl);
3295 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
3296 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
3297 then
3298 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
3300 else
3301 Spec_Id := Subp_Id;
3302 end if;
3304 -- Examine all postconditions for attribute 'Result and a post-state
3306 Prag := Pre_Post_Conditions (Items);
3307 while Present (Prag) loop
3308 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
3309 Name_Refined_Post)
3310 and then not Error_Posted (Prag)
3311 then
3312 Post_Prag := Prag;
3313 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
3314 end if;
3316 Prag := Next_Pragma (Prag);
3317 end loop;
3319 -- Examine the contract cases of the subprogram for attribute 'Result
3320 -- and a post-state.
3322 Prag := Contract_Test_Cases (Items);
3323 while Present (Prag) loop
3324 if Pragma_Name (Prag) = Name_Contract_Cases
3325 and then not Error_Posted (Prag)
3326 then
3327 Case_Prag := Prag;
3328 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
3329 end if;
3331 Prag := Next_Pragma (Prag);
3332 end loop;
3334 -- Do not emit any errors if the subprogram is not a function
3336 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
3337 null;
3339 -- Regardless of whether the function has postconditions or contract
3340 -- cases, or whether they mention attribute 'Result, an IN OUT formal
3341 -- parameter is always treated as a result.
3343 elsif Has_In_Out_Parameter (Spec_Id) then
3344 null;
3346 -- The function has both a postcondition and contract cases and they do
3347 -- not mention attribute 'Result.
3349 elsif Present (Case_Prag)
3350 and then not Seen_In_Case
3351 and then Present (Post_Prag)
3352 and then not Seen_In_Post
3353 then
3354 Error_Msg_N
3355 ("neither postcondition nor contract cases mention function "
3356 & "result?T?", Post_Prag);
3358 -- The function has contract cases only and they do not mention
3359 -- attribute 'Result.
3361 elsif Present (Case_Prag) and then not Seen_In_Case then
3362 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
3364 -- The function has postconditions only and they do not mention
3365 -- attribute 'Result.
3367 elsif Present (Post_Prag) and then not Seen_In_Post then
3368 Error_Msg_N
3369 ("postcondition does not mention function result?T?", Post_Prag);
3370 end if;
3371 end Check_Result_And_Post_State;
3373 ------------------------------
3374 -- Check_Unprotected_Access --
3375 ------------------------------
3377 procedure Check_Unprotected_Access
3378 (Context : Node_Id;
3379 Expr : Node_Id)
3381 Cont_Encl_Typ : Entity_Id;
3382 Pref_Encl_Typ : Entity_Id;
3384 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3385 -- Check whether Obj is a private component of a protected object.
3386 -- Return the protected type where the component resides, Empty
3387 -- otherwise.
3389 function Is_Public_Operation return Boolean;
3390 -- Verify that the enclosing operation is callable from outside the
3391 -- protected object, to minimize false positives.
3393 ------------------------------
3394 -- Enclosing_Protected_Type --
3395 ------------------------------
3397 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3398 begin
3399 if Is_Entity_Name (Obj) then
3400 declare
3401 Ent : Entity_Id := Entity (Obj);
3403 begin
3404 -- The object can be a renaming of a private component, use
3405 -- the original record component.
3407 if Is_Prival (Ent) then
3408 Ent := Prival_Link (Ent);
3409 end if;
3411 if Is_Protected_Type (Scope (Ent)) then
3412 return Scope (Ent);
3413 end if;
3414 end;
3415 end if;
3417 -- For indexed and selected components, recursively check the prefix
3419 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3420 return Enclosing_Protected_Type (Prefix (Obj));
3422 -- The object does not denote a protected component
3424 else
3425 return Empty;
3426 end if;
3427 end Enclosing_Protected_Type;
3429 -------------------------
3430 -- Is_Public_Operation --
3431 -------------------------
3433 function Is_Public_Operation return Boolean is
3434 S : Entity_Id;
3435 E : Entity_Id;
3437 begin
3438 S := Current_Scope;
3439 while Present (S) and then S /= Pref_Encl_Typ loop
3440 if Scope (S) = Pref_Encl_Typ then
3441 E := First_Entity (Pref_Encl_Typ);
3442 while Present (E)
3443 and then E /= First_Private_Entity (Pref_Encl_Typ)
3444 loop
3445 if E = S then
3446 return True;
3447 end if;
3449 Next_Entity (E);
3450 end loop;
3451 end if;
3453 S := Scope (S);
3454 end loop;
3456 return False;
3457 end Is_Public_Operation;
3459 -- Start of processing for Check_Unprotected_Access
3461 begin
3462 if Nkind (Expr) = N_Attribute_Reference
3463 and then Attribute_Name (Expr) = Name_Unchecked_Access
3464 then
3465 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3466 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3468 -- Check whether we are trying to export a protected component to a
3469 -- context with an equal or lower access level.
3471 if Present (Pref_Encl_Typ)
3472 and then No (Cont_Encl_Typ)
3473 and then Is_Public_Operation
3474 and then Scope_Depth (Pref_Encl_Typ) >=
3475 Object_Access_Level (Context)
3476 then
3477 Error_Msg_N
3478 ("??possible unprotected access to protected data", Expr);
3479 end if;
3480 end if;
3481 end Check_Unprotected_Access;
3483 ------------------------
3484 -- Collect_Interfaces --
3485 ------------------------
3487 procedure Collect_Interfaces
3488 (T : Entity_Id;
3489 Ifaces_List : out Elist_Id;
3490 Exclude_Parents : Boolean := False;
3491 Use_Full_View : Boolean := True)
3493 procedure Collect (Typ : Entity_Id);
3494 -- Subsidiary subprogram used to traverse the whole list
3495 -- of directly and indirectly implemented interfaces
3497 -------------
3498 -- Collect --
3499 -------------
3501 procedure Collect (Typ : Entity_Id) is
3502 Ancestor : Entity_Id;
3503 Full_T : Entity_Id;
3504 Id : Node_Id;
3505 Iface : Entity_Id;
3507 begin
3508 Full_T := Typ;
3510 -- Handle private types
3512 if Use_Full_View
3513 and then Is_Private_Type (Typ)
3514 and then Present (Full_View (Typ))
3515 then
3516 Full_T := Full_View (Typ);
3517 end if;
3519 -- Include the ancestor if we are generating the whole list of
3520 -- abstract interfaces.
3522 if Etype (Full_T) /= Typ
3524 -- Protect the frontend against wrong sources. For example:
3526 -- package P is
3527 -- type A is tagged null record;
3528 -- type B is new A with private;
3529 -- type C is new A with private;
3530 -- private
3531 -- type B is new C with null record;
3532 -- type C is new B with null record;
3533 -- end P;
3535 and then Etype (Full_T) /= T
3536 then
3537 Ancestor := Etype (Full_T);
3538 Collect (Ancestor);
3540 if Is_Interface (Ancestor) and then not Exclude_Parents then
3541 Append_Unique_Elmt (Ancestor, Ifaces_List);
3542 end if;
3543 end if;
3545 -- Traverse the graph of ancestor interfaces
3547 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
3548 Id := First (Abstract_Interface_List (Full_T));
3549 while Present (Id) loop
3550 Iface := Etype (Id);
3552 -- Protect against wrong uses. For example:
3553 -- type I is interface;
3554 -- type O is tagged null record;
3555 -- type Wrong is new I and O with null record; -- ERROR
3557 if Is_Interface (Iface) then
3558 if Exclude_Parents
3559 and then Etype (T) /= T
3560 and then Interface_Present_In_Ancestor (Etype (T), Iface)
3561 then
3562 null;
3563 else
3564 Collect (Iface);
3565 Append_Unique_Elmt (Iface, Ifaces_List);
3566 end if;
3567 end if;
3569 Next (Id);
3570 end loop;
3571 end if;
3572 end Collect;
3574 -- Start of processing for Collect_Interfaces
3576 begin
3577 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
3578 Ifaces_List := New_Elmt_List;
3579 Collect (T);
3580 end Collect_Interfaces;
3582 ----------------------------------
3583 -- Collect_Interface_Components --
3584 ----------------------------------
3586 procedure Collect_Interface_Components
3587 (Tagged_Type : Entity_Id;
3588 Components_List : out Elist_Id)
3590 procedure Collect (Typ : Entity_Id);
3591 -- Subsidiary subprogram used to climb to the parents
3593 -------------
3594 -- Collect --
3595 -------------
3597 procedure Collect (Typ : Entity_Id) is
3598 Tag_Comp : Entity_Id;
3599 Parent_Typ : Entity_Id;
3601 begin
3602 -- Handle private types
3604 if Present (Full_View (Etype (Typ))) then
3605 Parent_Typ := Full_View (Etype (Typ));
3606 else
3607 Parent_Typ := Etype (Typ);
3608 end if;
3610 if Parent_Typ /= Typ
3612 -- Protect the frontend against wrong sources. For example:
3614 -- package P is
3615 -- type A is tagged null record;
3616 -- type B is new A with private;
3617 -- type C is new A with private;
3618 -- private
3619 -- type B is new C with null record;
3620 -- type C is new B with null record;
3621 -- end P;
3623 and then Parent_Typ /= Tagged_Type
3624 then
3625 Collect (Parent_Typ);
3626 end if;
3628 -- Collect the components containing tags of secondary dispatch
3629 -- tables.
3631 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
3632 while Present (Tag_Comp) loop
3633 pragma Assert (Present (Related_Type (Tag_Comp)));
3634 Append_Elmt (Tag_Comp, Components_List);
3636 Tag_Comp := Next_Tag_Component (Tag_Comp);
3637 end loop;
3638 end Collect;
3640 -- Start of processing for Collect_Interface_Components
3642 begin
3643 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
3644 and then Is_Tagged_Type (Tagged_Type));
3646 Components_List := New_Elmt_List;
3647 Collect (Tagged_Type);
3648 end Collect_Interface_Components;
3650 -----------------------------
3651 -- Collect_Interfaces_Info --
3652 -----------------------------
3654 procedure Collect_Interfaces_Info
3655 (T : Entity_Id;
3656 Ifaces_List : out Elist_Id;
3657 Components_List : out Elist_Id;
3658 Tags_List : out Elist_Id)
3660 Comps_List : Elist_Id;
3661 Comp_Elmt : Elmt_Id;
3662 Comp_Iface : Entity_Id;
3663 Iface_Elmt : Elmt_Id;
3664 Iface : Entity_Id;
3666 function Search_Tag (Iface : Entity_Id) return Entity_Id;
3667 -- Search for the secondary tag associated with the interface type
3668 -- Iface that is implemented by T.
3670 ----------------
3671 -- Search_Tag --
3672 ----------------
3674 function Search_Tag (Iface : Entity_Id) return Entity_Id is
3675 ADT : Elmt_Id;
3676 begin
3677 if not Is_CPP_Class (T) then
3678 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
3679 else
3680 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
3681 end if;
3683 while Present (ADT)
3684 and then Is_Tag (Node (ADT))
3685 and then Related_Type (Node (ADT)) /= Iface
3686 loop
3687 -- Skip secondary dispatch table referencing thunks to user
3688 -- defined primitives covered by this interface.
3690 pragma Assert (Has_Suffix (Node (ADT), 'P'));
3691 Next_Elmt (ADT);
3693 -- Skip secondary dispatch tables of Ada types
3695 if not Is_CPP_Class (T) then
3697 -- Skip secondary dispatch table referencing thunks to
3698 -- predefined primitives.
3700 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
3701 Next_Elmt (ADT);
3703 -- Skip secondary dispatch table referencing user-defined
3704 -- primitives covered by this interface.
3706 pragma Assert (Has_Suffix (Node (ADT), 'D'));
3707 Next_Elmt (ADT);
3709 -- Skip secondary dispatch table referencing predefined
3710 -- primitives.
3712 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
3713 Next_Elmt (ADT);
3714 end if;
3715 end loop;
3717 pragma Assert (Is_Tag (Node (ADT)));
3718 return Node (ADT);
3719 end Search_Tag;
3721 -- Start of processing for Collect_Interfaces_Info
3723 begin
3724 Collect_Interfaces (T, Ifaces_List);
3725 Collect_Interface_Components (T, Comps_List);
3727 -- Search for the record component and tag associated with each
3728 -- interface type of T.
3730 Components_List := New_Elmt_List;
3731 Tags_List := New_Elmt_List;
3733 Iface_Elmt := First_Elmt (Ifaces_List);
3734 while Present (Iface_Elmt) loop
3735 Iface := Node (Iface_Elmt);
3737 -- Associate the primary tag component and the primary dispatch table
3738 -- with all the interfaces that are parents of T
3740 if Is_Ancestor (Iface, T, Use_Full_View => True) then
3741 Append_Elmt (First_Tag_Component (T), Components_List);
3742 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
3744 -- Otherwise search for the tag component and secondary dispatch
3745 -- table of Iface
3747 else
3748 Comp_Elmt := First_Elmt (Comps_List);
3749 while Present (Comp_Elmt) loop
3750 Comp_Iface := Related_Type (Node (Comp_Elmt));
3752 if Comp_Iface = Iface
3753 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
3754 then
3755 Append_Elmt (Node (Comp_Elmt), Components_List);
3756 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
3757 exit;
3758 end if;
3760 Next_Elmt (Comp_Elmt);
3761 end loop;
3762 pragma Assert (Present (Comp_Elmt));
3763 end if;
3765 Next_Elmt (Iface_Elmt);
3766 end loop;
3767 end Collect_Interfaces_Info;
3769 ---------------------
3770 -- Collect_Parents --
3771 ---------------------
3773 procedure Collect_Parents
3774 (T : Entity_Id;
3775 List : out Elist_Id;
3776 Use_Full_View : Boolean := True)
3778 Current_Typ : Entity_Id := T;
3779 Parent_Typ : Entity_Id;
3781 begin
3782 List := New_Elmt_List;
3784 -- No action if the if the type has no parents
3786 if T = Etype (T) then
3787 return;
3788 end if;
3790 loop
3791 Parent_Typ := Etype (Current_Typ);
3793 if Is_Private_Type (Parent_Typ)
3794 and then Present (Full_View (Parent_Typ))
3795 and then Use_Full_View
3796 then
3797 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3798 end if;
3800 Append_Elmt (Parent_Typ, List);
3802 exit when Parent_Typ = Current_Typ;
3803 Current_Typ := Parent_Typ;
3804 end loop;
3805 end Collect_Parents;
3807 ----------------------------------
3808 -- Collect_Primitive_Operations --
3809 ----------------------------------
3811 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
3812 B_Type : constant Entity_Id := Base_Type (T);
3813 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
3814 B_Scope : Entity_Id := Scope (B_Type);
3815 Op_List : Elist_Id;
3816 Formal : Entity_Id;
3817 Is_Prim : Boolean;
3818 Is_Type_In_Pkg : Boolean;
3819 Formal_Derived : Boolean := False;
3820 Id : Entity_Id;
3822 function Match (E : Entity_Id) return Boolean;
3823 -- True if E's base type is B_Type, or E is of an anonymous access type
3824 -- and the base type of its designated type is B_Type.
3826 -----------
3827 -- Match --
3828 -----------
3830 function Match (E : Entity_Id) return Boolean is
3831 Etyp : Entity_Id := Etype (E);
3833 begin
3834 if Ekind (Etyp) = E_Anonymous_Access_Type then
3835 Etyp := Designated_Type (Etyp);
3836 end if;
3838 -- In Ada 2012 a primitive operation may have a formal of an
3839 -- incomplete view of the parent type.
3841 return Base_Type (Etyp) = B_Type
3842 or else
3843 (Ada_Version >= Ada_2012
3844 and then Ekind (Etyp) = E_Incomplete_Type
3845 and then Full_View (Etyp) = B_Type);
3846 end Match;
3848 -- Start of processing for Collect_Primitive_Operations
3850 begin
3851 -- For tagged types, the primitive operations are collected as they
3852 -- are declared, and held in an explicit list which is simply returned.
3854 if Is_Tagged_Type (B_Type) then
3855 return Primitive_Operations (B_Type);
3857 -- An untagged generic type that is a derived type inherits the
3858 -- primitive operations of its parent type. Other formal types only
3859 -- have predefined operators, which are not explicitly represented.
3861 elsif Is_Generic_Type (B_Type) then
3862 if Nkind (B_Decl) = N_Formal_Type_Declaration
3863 and then Nkind (Formal_Type_Definition (B_Decl)) =
3864 N_Formal_Derived_Type_Definition
3865 then
3866 Formal_Derived := True;
3867 else
3868 return New_Elmt_List;
3869 end if;
3870 end if;
3872 Op_List := New_Elmt_List;
3874 if B_Scope = Standard_Standard then
3875 if B_Type = Standard_String then
3876 Append_Elmt (Standard_Op_Concat, Op_List);
3878 elsif B_Type = Standard_Wide_String then
3879 Append_Elmt (Standard_Op_Concatw, Op_List);
3881 else
3882 null;
3883 end if;
3885 -- Locate the primitive subprograms of the type
3887 else
3888 -- The primitive operations appear after the base type, except
3889 -- if the derivation happens within the private part of B_Scope
3890 -- and the type is a private type, in which case both the type
3891 -- and some primitive operations may appear before the base
3892 -- type, and the list of candidates starts after the type.
3894 if In_Open_Scopes (B_Scope)
3895 and then Scope (T) = B_Scope
3896 and then In_Private_Part (B_Scope)
3897 then
3898 Id := Next_Entity (T);
3900 -- In Ada 2012, If the type has an incomplete partial view, there
3901 -- may be primitive operations declared before the full view, so
3902 -- we need to start scanning from the incomplete view, which is
3903 -- earlier on the entity chain.
3905 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
3906 and then Present (Incomplete_View (Parent (B_Type)))
3907 then
3908 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
3910 else
3911 Id := Next_Entity (B_Type);
3912 end if;
3914 -- Set flag if this is a type in a package spec
3916 Is_Type_In_Pkg :=
3917 Is_Package_Or_Generic_Package (B_Scope)
3918 and then
3919 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
3920 N_Package_Body;
3922 while Present (Id) loop
3924 -- Test whether the result type or any of the parameter types of
3925 -- each subprogram following the type match that type when the
3926 -- type is declared in a package spec, is a derived type, or the
3927 -- subprogram is marked as primitive. (The Is_Primitive test is
3928 -- needed to find primitives of nonderived types in declarative
3929 -- parts that happen to override the predefined "=" operator.)
3931 -- Note that generic formal subprograms are not considered to be
3932 -- primitive operations and thus are never inherited.
3934 if Is_Overloadable (Id)
3935 and then (Is_Type_In_Pkg
3936 or else Is_Derived_Type (B_Type)
3937 or else Is_Primitive (Id))
3938 and then Nkind (Parent (Parent (Id)))
3939 not in N_Formal_Subprogram_Declaration
3940 then
3941 Is_Prim := False;
3943 if Match (Id) then
3944 Is_Prim := True;
3946 else
3947 Formal := First_Formal (Id);
3948 while Present (Formal) loop
3949 if Match (Formal) then
3950 Is_Prim := True;
3951 exit;
3952 end if;
3954 Next_Formal (Formal);
3955 end loop;
3956 end if;
3958 -- For a formal derived type, the only primitives are the ones
3959 -- inherited from the parent type. Operations appearing in the
3960 -- package declaration are not primitive for it.
3962 if Is_Prim
3963 and then (not Formal_Derived or else Present (Alias (Id)))
3964 then
3965 -- In the special case of an equality operator aliased to
3966 -- an overriding dispatching equality belonging to the same
3967 -- type, we don't include it in the list of primitives.
3968 -- This avoids inheriting multiple equality operators when
3969 -- deriving from untagged private types whose full type is
3970 -- tagged, which can otherwise cause ambiguities. Note that
3971 -- this should only happen for this kind of untagged parent
3972 -- type, since normally dispatching operations are inherited
3973 -- using the type's Primitive_Operations list.
3975 if Chars (Id) = Name_Op_Eq
3976 and then Is_Dispatching_Operation (Id)
3977 and then Present (Alias (Id))
3978 and then Present (Overridden_Operation (Alias (Id)))
3979 and then Base_Type (Etype (First_Entity (Id))) =
3980 Base_Type (Etype (First_Entity (Alias (Id))))
3981 then
3982 null;
3984 -- Include the subprogram in the list of primitives
3986 else
3987 Append_Elmt (Id, Op_List);
3988 end if;
3989 end if;
3990 end if;
3992 Next_Entity (Id);
3994 -- For a type declared in System, some of its operations may
3995 -- appear in the target-specific extension to System.
3997 if No (Id)
3998 and then B_Scope = RTU_Entity (System)
3999 and then Present_System_Aux
4000 then
4001 B_Scope := System_Aux_Id;
4002 Id := First_Entity (System_Aux_Id);
4003 end if;
4004 end loop;
4005 end if;
4007 return Op_List;
4008 end Collect_Primitive_Operations;
4010 -----------------------------------
4011 -- Compile_Time_Constraint_Error --
4012 -----------------------------------
4014 function Compile_Time_Constraint_Error
4015 (N : Node_Id;
4016 Msg : String;
4017 Ent : Entity_Id := Empty;
4018 Loc : Source_Ptr := No_Location;
4019 Warn : Boolean := False) return Node_Id
4021 Msgc : String (1 .. Msg'Length + 3);
4022 -- Copy of message, with room for possible ?? or << and ! at end
4024 Msgl : Natural;
4025 Wmsg : Boolean;
4026 Eloc : Source_Ptr;
4028 -- Start of processing for Compile_Time_Constraint_Error
4030 begin
4031 -- If this is a warning, convert it into an error if we are in code
4032 -- subject to SPARK_Mode being set ON.
4034 Error_Msg_Warn := SPARK_Mode /= On;
4036 -- A static constraint error in an instance body is not a fatal error.
4037 -- we choose to inhibit the message altogether, because there is no
4038 -- obvious node (for now) on which to post it. On the other hand the
4039 -- offending node must be replaced with a constraint_error in any case.
4041 -- No messages are generated if we already posted an error on this node
4043 if not Error_Posted (N) then
4044 if Loc /= No_Location then
4045 Eloc := Loc;
4046 else
4047 Eloc := Sloc (N);
4048 end if;
4050 -- Copy message to Msgc, converting any ? in the message into
4051 -- < instead, so that we have an error in GNATprove mode.
4053 Msgl := Msg'Length;
4055 for J in 1 .. Msgl loop
4056 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
4057 Msgc (J) := '<';
4058 else
4059 Msgc (J) := Msg (J);
4060 end if;
4061 end loop;
4063 -- Message is a warning, even in Ada 95 case
4065 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
4066 Wmsg := True;
4068 -- In Ada 83, all messages are warnings. In the private part and
4069 -- the body of an instance, constraint_checks are only warnings.
4070 -- We also make this a warning if the Warn parameter is set.
4072 elsif Warn
4073 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
4074 then
4075 Msgl := Msgl + 1;
4076 Msgc (Msgl) := '<';
4077 Msgl := Msgl + 1;
4078 Msgc (Msgl) := '<';
4079 Wmsg := True;
4081 elsif In_Instance_Not_Visible then
4082 Msgl := Msgl + 1;
4083 Msgc (Msgl) := '<';
4084 Msgl := Msgl + 1;
4085 Msgc (Msgl) := '<';
4086 Wmsg := True;
4088 -- Otherwise we have a real error message (Ada 95 static case)
4089 -- and we make this an unconditional message. Note that in the
4090 -- warning case we do not make the message unconditional, it seems
4091 -- quite reasonable to delete messages like this (about exceptions
4092 -- that will be raised) in dead code.
4094 else
4095 Wmsg := False;
4096 Msgl := Msgl + 1;
4097 Msgc (Msgl) := '!';
4098 end if;
4100 -- One more test, skip the warning if the related expression is
4101 -- statically unevaluated, since we don't want to warn about what
4102 -- will happen when something is evaluated if it never will be
4103 -- evaluated.
4105 if not Is_Statically_Unevaluated (N) then
4106 Error_Msg_Warn := SPARK_Mode /= On;
4108 if Present (Ent) then
4109 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4110 else
4111 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4112 end if;
4114 if Wmsg then
4116 -- Check whether the context is an Init_Proc
4118 if Inside_Init_Proc then
4119 declare
4120 Conc_Typ : constant Entity_Id :=
4121 Corresponding_Concurrent_Type
4122 (Entity (Parameter_Type (First
4123 (Parameter_Specifications
4124 (Parent (Current_Scope))))));
4126 begin
4127 -- Don't complain if the corresponding concurrent type
4128 -- doesn't come from source (i.e. a single task/protected
4129 -- object).
4131 if Present (Conc_Typ)
4132 and then not Comes_From_Source (Conc_Typ)
4133 then
4134 Error_Msg_NEL
4135 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4137 else
4138 if GNATprove_Mode then
4139 Error_Msg_NEL
4140 ("\& would have been raised for objects of this "
4141 & "type", N, Standard_Constraint_Error, Eloc);
4142 else
4143 Error_Msg_NEL
4144 ("\& will be raised for objects of this type??",
4145 N, Standard_Constraint_Error, Eloc);
4146 end if;
4147 end if;
4148 end;
4150 else
4151 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4152 end if;
4154 else
4155 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4156 Set_Error_Posted (N);
4157 end if;
4158 end if;
4159 end if;
4161 return N;
4162 end Compile_Time_Constraint_Error;
4164 -----------------------
4165 -- Conditional_Delay --
4166 -----------------------
4168 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4169 begin
4170 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4171 Set_Has_Delayed_Freeze (New_Ent);
4172 end if;
4173 end Conditional_Delay;
4175 ----------------------------
4176 -- Contains_Refined_State --
4177 ----------------------------
4179 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4180 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4181 -- Determine whether a dependency list mentions a state with a visible
4182 -- refinement.
4184 function Has_State_In_Global (List : Node_Id) return Boolean;
4185 -- Determine whether a global list mentions a state with a visible
4186 -- refinement.
4188 function Is_Refined_State (Item : Node_Id) return Boolean;
4189 -- Determine whether Item is a reference to an abstract state with a
4190 -- visible refinement.
4192 -----------------------------
4193 -- Has_State_In_Dependency --
4194 -----------------------------
4196 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4197 Clause : Node_Id;
4198 Output : Node_Id;
4200 begin
4201 -- A null dependency list does not mention any states
4203 if Nkind (List) = N_Null then
4204 return False;
4206 -- Dependency clauses appear as component associations of an
4207 -- aggregate.
4209 elsif Nkind (List) = N_Aggregate
4210 and then Present (Component_Associations (List))
4211 then
4212 Clause := First (Component_Associations (List));
4213 while Present (Clause) loop
4215 -- Inspect the outputs of a dependency clause
4217 Output := First (Choices (Clause));
4218 while Present (Output) loop
4219 if Is_Refined_State (Output) then
4220 return True;
4221 end if;
4223 Next (Output);
4224 end loop;
4226 -- Inspect the outputs of a dependency clause
4228 if Is_Refined_State (Expression (Clause)) then
4229 return True;
4230 end if;
4232 Next (Clause);
4233 end loop;
4235 -- If we get here, then none of the dependency clauses mention a
4236 -- state with visible refinement.
4238 return False;
4240 -- An illegal pragma managed to sneak in
4242 else
4243 raise Program_Error;
4244 end if;
4245 end Has_State_In_Dependency;
4247 -------------------------
4248 -- Has_State_In_Global --
4249 -------------------------
4251 function Has_State_In_Global (List : Node_Id) return Boolean is
4252 Item : Node_Id;
4254 begin
4255 -- A null global list does not mention any states
4257 if Nkind (List) = N_Null then
4258 return False;
4260 -- Simple global list or moded global list declaration
4262 elsif Nkind (List) = N_Aggregate then
4264 -- The declaration of a simple global list appear as a collection
4265 -- of expressions.
4267 if Present (Expressions (List)) then
4268 Item := First (Expressions (List));
4269 while Present (Item) loop
4270 if Is_Refined_State (Item) then
4271 return True;
4272 end if;
4274 Next (Item);
4275 end loop;
4277 -- The declaration of a moded global list appears as a collection
4278 -- of component associations where individual choices denote
4279 -- modes.
4281 else
4282 Item := First (Component_Associations (List));
4283 while Present (Item) loop
4284 if Has_State_In_Global (Expression (Item)) then
4285 return True;
4286 end if;
4288 Next (Item);
4289 end loop;
4290 end if;
4292 -- If we get here, then the simple/moded global list did not
4293 -- mention any states with a visible refinement.
4295 return False;
4297 -- Single global item declaration
4299 elsif Is_Entity_Name (List) then
4300 return Is_Refined_State (List);
4302 -- An illegal pragma managed to sneak in
4304 else
4305 raise Program_Error;
4306 end if;
4307 end Has_State_In_Global;
4309 ----------------------
4310 -- Is_Refined_State --
4311 ----------------------
4313 function Is_Refined_State (Item : Node_Id) return Boolean is
4314 Elmt : Node_Id;
4315 Item_Id : Entity_Id;
4317 begin
4318 if Nkind (Item) = N_Null then
4319 return False;
4321 -- States cannot be subject to attribute 'Result. This case arises
4322 -- in dependency relations.
4324 elsif Nkind (Item) = N_Attribute_Reference
4325 and then Attribute_Name (Item) = Name_Result
4326 then
4327 return False;
4329 -- Multiple items appear as an aggregate. This case arises in
4330 -- dependency relations.
4332 elsif Nkind (Item) = N_Aggregate
4333 and then Present (Expressions (Item))
4334 then
4335 Elmt := First (Expressions (Item));
4336 while Present (Elmt) loop
4337 if Is_Refined_State (Elmt) then
4338 return True;
4339 end if;
4341 Next (Elmt);
4342 end loop;
4344 -- If we get here, then none of the inputs or outputs reference a
4345 -- state with visible refinement.
4347 return False;
4349 -- Single item
4351 else
4352 Item_Id := Entity_Of (Item);
4354 return
4355 Present (Item_Id)
4356 and then Ekind (Item_Id) = E_Abstract_State
4357 and then Has_Visible_Refinement (Item_Id);
4358 end if;
4359 end Is_Refined_State;
4361 -- Local variables
4363 Arg : constant Node_Id :=
4364 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4365 Nam : constant Name_Id := Pragma_Name (Prag);
4367 -- Start of processing for Contains_Refined_State
4369 begin
4370 if Nam = Name_Depends then
4371 return Has_State_In_Dependency (Arg);
4373 else pragma Assert (Nam = Name_Global);
4374 return Has_State_In_Global (Arg);
4375 end if;
4376 end Contains_Refined_State;
4378 -------------------------
4379 -- Copy_Component_List --
4380 -------------------------
4382 function Copy_Component_List
4383 (R_Typ : Entity_Id;
4384 Loc : Source_Ptr) return List_Id
4386 Comp : Node_Id;
4387 Comps : constant List_Id := New_List;
4389 begin
4390 Comp := First_Component (Underlying_Type (R_Typ));
4391 while Present (Comp) loop
4392 if Comes_From_Source (Comp) then
4393 declare
4394 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4395 begin
4396 Append_To (Comps,
4397 Make_Component_Declaration (Loc,
4398 Defining_Identifier =>
4399 Make_Defining_Identifier (Loc, Chars (Comp)),
4400 Component_Definition =>
4401 New_Copy_Tree
4402 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4403 end;
4404 end if;
4406 Next_Component (Comp);
4407 end loop;
4409 return Comps;
4410 end Copy_Component_List;
4412 -------------------------
4413 -- Copy_Parameter_List --
4414 -------------------------
4416 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4417 Loc : constant Source_Ptr := Sloc (Subp_Id);
4418 Plist : List_Id;
4419 Formal : Entity_Id;
4421 begin
4422 if No (First_Formal (Subp_Id)) then
4423 return No_List;
4424 else
4425 Plist := New_List;
4426 Formal := First_Formal (Subp_Id);
4427 while Present (Formal) loop
4428 Append
4429 (Make_Parameter_Specification (Loc,
4430 Defining_Identifier =>
4431 Make_Defining_Identifier (Sloc (Formal),
4432 Chars => Chars (Formal)),
4433 In_Present => In_Present (Parent (Formal)),
4434 Out_Present => Out_Present (Parent (Formal)),
4435 Parameter_Type =>
4436 New_Occurrence_Of (Etype (Formal), Loc),
4437 Expression =>
4438 New_Copy_Tree (Expression (Parent (Formal)))),
4439 Plist);
4441 Next_Formal (Formal);
4442 end loop;
4443 end if;
4445 return Plist;
4446 end Copy_Parameter_List;
4448 --------------------------------
4449 -- Corresponding_Generic_Type --
4450 --------------------------------
4452 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
4453 Inst : Entity_Id;
4454 Gen : Entity_Id;
4455 Typ : Entity_Id;
4457 begin
4458 if not Is_Generic_Actual_Type (T) then
4459 return Any_Type;
4461 -- If the actual is the actual of an enclosing instance, resolution
4462 -- was correct in the generic.
4464 elsif Nkind (Parent (T)) = N_Subtype_Declaration
4465 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
4466 and then
4467 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
4468 then
4469 return Any_Type;
4471 else
4472 Inst := Scope (T);
4474 if Is_Wrapper_Package (Inst) then
4475 Inst := Related_Instance (Inst);
4476 end if;
4478 Gen :=
4479 Generic_Parent
4480 (Specification (Unit_Declaration_Node (Inst)));
4482 -- Generic actual has the same name as the corresponding formal
4484 Typ := First_Entity (Gen);
4485 while Present (Typ) loop
4486 if Chars (Typ) = Chars (T) then
4487 return Typ;
4488 end if;
4490 Next_Entity (Typ);
4491 end loop;
4493 return Any_Type;
4494 end if;
4495 end Corresponding_Generic_Type;
4497 ---------------------------
4498 -- Corresponding_Spec_Of --
4499 ---------------------------
4501 function Corresponding_Spec_Of (Subp_Decl : Node_Id) return Entity_Id is
4502 begin
4503 if Nkind (Subp_Decl) = N_Subprogram_Body
4504 and then Present (Corresponding_Spec (Subp_Decl))
4505 then
4506 return Corresponding_Spec (Subp_Decl);
4508 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
4509 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
4510 then
4511 return Corresponding_Spec_Of_Stub (Subp_Decl);
4513 else
4514 return Defining_Entity (Subp_Decl);
4515 end if;
4516 end Corresponding_Spec_Of;
4518 --------------------
4519 -- Current_Entity --
4520 --------------------
4522 -- The currently visible definition for a given identifier is the
4523 -- one most chained at the start of the visibility chain, i.e. the
4524 -- one that is referenced by the Node_Id value of the name of the
4525 -- given identifier.
4527 function Current_Entity (N : Node_Id) return Entity_Id is
4528 begin
4529 return Get_Name_Entity_Id (Chars (N));
4530 end Current_Entity;
4532 -----------------------------
4533 -- Current_Entity_In_Scope --
4534 -----------------------------
4536 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
4537 E : Entity_Id;
4538 CS : constant Entity_Id := Current_Scope;
4540 Transient_Case : constant Boolean := Scope_Is_Transient;
4542 begin
4543 E := Get_Name_Entity_Id (Chars (N));
4544 while Present (E)
4545 and then Scope (E) /= CS
4546 and then (not Transient_Case or else Scope (E) /= Scope (CS))
4547 loop
4548 E := Homonym (E);
4549 end loop;
4551 return E;
4552 end Current_Entity_In_Scope;
4554 -------------------
4555 -- Current_Scope --
4556 -------------------
4558 function Current_Scope return Entity_Id is
4559 begin
4560 if Scope_Stack.Last = -1 then
4561 return Standard_Standard;
4562 else
4563 declare
4564 C : constant Entity_Id :=
4565 Scope_Stack.Table (Scope_Stack.Last).Entity;
4566 begin
4567 if Present (C) then
4568 return C;
4569 else
4570 return Standard_Standard;
4571 end if;
4572 end;
4573 end if;
4574 end Current_Scope;
4576 ------------------------
4577 -- Current_Subprogram --
4578 ------------------------
4580 function Current_Subprogram return Entity_Id is
4581 Scop : constant Entity_Id := Current_Scope;
4582 begin
4583 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
4584 return Scop;
4585 else
4586 return Enclosing_Subprogram (Scop);
4587 end if;
4588 end Current_Subprogram;
4590 ----------------------------------
4591 -- Deepest_Type_Access_Level --
4592 ----------------------------------
4594 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
4595 begin
4596 if Ekind (Typ) = E_Anonymous_Access_Type
4597 and then not Is_Local_Anonymous_Access (Typ)
4598 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
4599 then
4600 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
4601 -- access type.
4603 return
4604 Scope_Depth (Enclosing_Dynamic_Scope
4605 (Defining_Identifier
4606 (Associated_Node_For_Itype (Typ))));
4608 -- For generic formal type, return Int'Last (infinite).
4609 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
4611 elsif Is_Generic_Type (Root_Type (Typ)) then
4612 return UI_From_Int (Int'Last);
4614 else
4615 return Type_Access_Level (Typ);
4616 end if;
4617 end Deepest_Type_Access_Level;
4619 ---------------------
4620 -- Defining_Entity --
4621 ---------------------
4623 function Defining_Entity (N : Node_Id) return Entity_Id is
4624 K : constant Node_Kind := Nkind (N);
4625 Err : Entity_Id := Empty;
4627 begin
4628 case K is
4629 when
4630 N_Subprogram_Declaration |
4631 N_Abstract_Subprogram_Declaration |
4632 N_Subprogram_Body |
4633 N_Package_Declaration |
4634 N_Subprogram_Renaming_Declaration |
4635 N_Subprogram_Body_Stub |
4636 N_Generic_Subprogram_Declaration |
4637 N_Generic_Package_Declaration |
4638 N_Formal_Subprogram_Declaration |
4639 N_Expression_Function
4641 return Defining_Entity (Specification (N));
4643 when
4644 N_Component_Declaration |
4645 N_Defining_Program_Unit_Name |
4646 N_Discriminant_Specification |
4647 N_Entry_Body |
4648 N_Entry_Declaration |
4649 N_Entry_Index_Specification |
4650 N_Exception_Declaration |
4651 N_Exception_Renaming_Declaration |
4652 N_Formal_Object_Declaration |
4653 N_Formal_Package_Declaration |
4654 N_Formal_Type_Declaration |
4655 N_Full_Type_Declaration |
4656 N_Implicit_Label_Declaration |
4657 N_Incomplete_Type_Declaration |
4658 N_Loop_Parameter_Specification |
4659 N_Number_Declaration |
4660 N_Object_Declaration |
4661 N_Object_Renaming_Declaration |
4662 N_Package_Body_Stub |
4663 N_Parameter_Specification |
4664 N_Private_Extension_Declaration |
4665 N_Private_Type_Declaration |
4666 N_Protected_Body |
4667 N_Protected_Body_Stub |
4668 N_Protected_Type_Declaration |
4669 N_Single_Protected_Declaration |
4670 N_Single_Task_Declaration |
4671 N_Subtype_Declaration |
4672 N_Task_Body |
4673 N_Task_Body_Stub |
4674 N_Task_Type_Declaration
4676 return Defining_Identifier (N);
4678 when N_Subunit =>
4679 return Defining_Entity (Proper_Body (N));
4681 when
4682 N_Function_Instantiation |
4683 N_Function_Specification |
4684 N_Generic_Function_Renaming_Declaration |
4685 N_Generic_Package_Renaming_Declaration |
4686 N_Generic_Procedure_Renaming_Declaration |
4687 N_Package_Body |
4688 N_Package_Instantiation |
4689 N_Package_Renaming_Declaration |
4690 N_Package_Specification |
4691 N_Procedure_Instantiation |
4692 N_Procedure_Specification
4694 declare
4695 Nam : constant Node_Id := Defining_Unit_Name (N);
4697 begin
4698 if Nkind (Nam) in N_Entity then
4699 return Nam;
4701 -- For Error, make up a name and attach to declaration
4702 -- so we can continue semantic analysis
4704 elsif Nam = Error then
4705 Err := Make_Temporary (Sloc (N), 'T');
4706 Set_Defining_Unit_Name (N, Err);
4708 return Err;
4710 -- If not an entity, get defining identifier
4712 else
4713 return Defining_Identifier (Nam);
4714 end if;
4715 end;
4717 when
4718 N_Block_Statement |
4719 N_Loop_Statement
4721 return Entity (Identifier (N));
4723 when others =>
4724 raise Program_Error;
4726 end case;
4727 end Defining_Entity;
4729 --------------------------
4730 -- Denotes_Discriminant --
4731 --------------------------
4733 function Denotes_Discriminant
4734 (N : Node_Id;
4735 Check_Concurrent : Boolean := False) return Boolean
4737 E : Entity_Id;
4739 begin
4740 if not Is_Entity_Name (N) or else No (Entity (N)) then
4741 return False;
4742 else
4743 E := Entity (N);
4744 end if;
4746 -- If we are checking for a protected type, the discriminant may have
4747 -- been rewritten as the corresponding discriminal of the original type
4748 -- or of the corresponding concurrent record, depending on whether we
4749 -- are in the spec or body of the protected type.
4751 return Ekind (E) = E_Discriminant
4752 or else
4753 (Check_Concurrent
4754 and then Ekind (E) = E_In_Parameter
4755 and then Present (Discriminal_Link (E))
4756 and then
4757 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
4758 or else
4759 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
4761 end Denotes_Discriminant;
4763 -------------------------
4764 -- Denotes_Same_Object --
4765 -------------------------
4767 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
4768 Obj1 : Node_Id := A1;
4769 Obj2 : Node_Id := A2;
4771 function Has_Prefix (N : Node_Id) return Boolean;
4772 -- Return True if N has attribute Prefix
4774 function Is_Renaming (N : Node_Id) return Boolean;
4775 -- Return true if N names a renaming entity
4777 function Is_Valid_Renaming (N : Node_Id) return Boolean;
4778 -- For renamings, return False if the prefix of any dereference within
4779 -- the renamed object_name is a variable, or any expression within the
4780 -- renamed object_name contains references to variables or calls on
4781 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
4783 ----------------
4784 -- Has_Prefix --
4785 ----------------
4787 function Has_Prefix (N : Node_Id) return Boolean is
4788 begin
4789 return
4790 Nkind_In (N,
4791 N_Attribute_Reference,
4792 N_Expanded_Name,
4793 N_Explicit_Dereference,
4794 N_Indexed_Component,
4795 N_Reference,
4796 N_Selected_Component,
4797 N_Slice);
4798 end Has_Prefix;
4800 -----------------
4801 -- Is_Renaming --
4802 -----------------
4804 function Is_Renaming (N : Node_Id) return Boolean is
4805 begin
4806 return Is_Entity_Name (N)
4807 and then Present (Renamed_Entity (Entity (N)));
4808 end Is_Renaming;
4810 -----------------------
4811 -- Is_Valid_Renaming --
4812 -----------------------
4814 function Is_Valid_Renaming (N : Node_Id) return Boolean is
4816 function Check_Renaming (N : Node_Id) return Boolean;
4817 -- Recursive function used to traverse all the prefixes of N
4819 function Check_Renaming (N : Node_Id) return Boolean is
4820 begin
4821 if Is_Renaming (N)
4822 and then not Check_Renaming (Renamed_Entity (Entity (N)))
4823 then
4824 return False;
4825 end if;
4827 if Nkind (N) = N_Indexed_Component then
4828 declare
4829 Indx : Node_Id;
4831 begin
4832 Indx := First (Expressions (N));
4833 while Present (Indx) loop
4834 if not Is_OK_Static_Expression (Indx) then
4835 return False;
4836 end if;
4838 Next_Index (Indx);
4839 end loop;
4840 end;
4841 end if;
4843 if Has_Prefix (N) then
4844 declare
4845 P : constant Node_Id := Prefix (N);
4847 begin
4848 if Nkind (N) = N_Explicit_Dereference
4849 and then Is_Variable (P)
4850 then
4851 return False;
4853 elsif Is_Entity_Name (P)
4854 and then Ekind (Entity (P)) = E_Function
4855 then
4856 return False;
4858 elsif Nkind (P) = N_Function_Call then
4859 return False;
4860 end if;
4862 -- Recursion to continue traversing the prefix of the
4863 -- renaming expression
4865 return Check_Renaming (P);
4866 end;
4867 end if;
4869 return True;
4870 end Check_Renaming;
4872 -- Start of processing for Is_Valid_Renaming
4874 begin
4875 return Check_Renaming (N);
4876 end Is_Valid_Renaming;
4878 -- Start of processing for Denotes_Same_Object
4880 begin
4881 -- Both names statically denote the same stand-alone object or parameter
4882 -- (RM 6.4.1(6.5/3))
4884 if Is_Entity_Name (Obj1)
4885 and then Is_Entity_Name (Obj2)
4886 and then Entity (Obj1) = Entity (Obj2)
4887 then
4888 return True;
4889 end if;
4891 -- For renamings, the prefix of any dereference within the renamed
4892 -- object_name is not a variable, and any expression within the
4893 -- renamed object_name contains no references to variables nor
4894 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
4896 if Is_Renaming (Obj1) then
4897 if Is_Valid_Renaming (Obj1) then
4898 Obj1 := Renamed_Entity (Entity (Obj1));
4899 else
4900 return False;
4901 end if;
4902 end if;
4904 if Is_Renaming (Obj2) then
4905 if Is_Valid_Renaming (Obj2) then
4906 Obj2 := Renamed_Entity (Entity (Obj2));
4907 else
4908 return False;
4909 end if;
4910 end if;
4912 -- No match if not same node kind (such cases are handled by
4913 -- Denotes_Same_Prefix)
4915 if Nkind (Obj1) /= Nkind (Obj2) then
4916 return False;
4918 -- After handling valid renamings, one of the two names statically
4919 -- denoted a renaming declaration whose renamed object_name is known
4920 -- to denote the same object as the other (RM 6.4.1(6.10/3))
4922 elsif Is_Entity_Name (Obj1) then
4923 if Is_Entity_Name (Obj2) then
4924 return Entity (Obj1) = Entity (Obj2);
4925 else
4926 return False;
4927 end if;
4929 -- Both names are selected_components, their prefixes are known to
4930 -- denote the same object, and their selector_names denote the same
4931 -- component (RM 6.4.1(6.6/3)
4933 elsif Nkind (Obj1) = N_Selected_Component then
4934 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4935 and then
4936 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
4938 -- Both names are dereferences and the dereferenced names are known to
4939 -- denote the same object (RM 6.4.1(6.7/3))
4941 elsif Nkind (Obj1) = N_Explicit_Dereference then
4942 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
4944 -- Both names are indexed_components, their prefixes are known to denote
4945 -- the same object, and each of the pairs of corresponding index values
4946 -- are either both static expressions with the same static value or both
4947 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
4949 elsif Nkind (Obj1) = N_Indexed_Component then
4950 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
4951 return False;
4952 else
4953 declare
4954 Indx1 : Node_Id;
4955 Indx2 : Node_Id;
4957 begin
4958 Indx1 := First (Expressions (Obj1));
4959 Indx2 := First (Expressions (Obj2));
4960 while Present (Indx1) loop
4962 -- Indexes must denote the same static value or same object
4964 if Is_OK_Static_Expression (Indx1) then
4965 if not Is_OK_Static_Expression (Indx2) then
4966 return False;
4968 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
4969 return False;
4970 end if;
4972 elsif not Denotes_Same_Object (Indx1, Indx2) then
4973 return False;
4974 end if;
4976 Next (Indx1);
4977 Next (Indx2);
4978 end loop;
4980 return True;
4981 end;
4982 end if;
4984 -- Both names are slices, their prefixes are known to denote the same
4985 -- object, and the two slices have statically matching index constraints
4986 -- (RM 6.4.1(6.9/3))
4988 elsif Nkind (Obj1) = N_Slice
4989 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4990 then
4991 declare
4992 Lo1, Lo2, Hi1, Hi2 : Node_Id;
4994 begin
4995 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
4996 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
4998 -- Check whether bounds are statically identical. There is no
4999 -- attempt to detect partial overlap of slices.
5001 return Denotes_Same_Object (Lo1, Lo2)
5002 and then
5003 Denotes_Same_Object (Hi1, Hi2);
5004 end;
5006 -- In the recursion, literals appear as indexes
5008 elsif Nkind (Obj1) = N_Integer_Literal
5009 and then
5010 Nkind (Obj2) = N_Integer_Literal
5011 then
5012 return Intval (Obj1) = Intval (Obj2);
5014 else
5015 return False;
5016 end if;
5017 end Denotes_Same_Object;
5019 -------------------------
5020 -- Denotes_Same_Prefix --
5021 -------------------------
5023 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
5025 begin
5026 if Is_Entity_Name (A1) then
5027 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
5028 and then not Is_Access_Type (Etype (A1))
5029 then
5030 return Denotes_Same_Object (A1, Prefix (A2))
5031 or else Denotes_Same_Prefix (A1, Prefix (A2));
5032 else
5033 return False;
5034 end if;
5036 elsif Is_Entity_Name (A2) then
5037 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
5039 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
5040 and then
5041 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
5042 then
5043 declare
5044 Root1, Root2 : Node_Id;
5045 Depth1, Depth2 : Int := 0;
5047 begin
5048 Root1 := Prefix (A1);
5049 while not Is_Entity_Name (Root1) loop
5050 if not Nkind_In
5051 (Root1, N_Selected_Component, N_Indexed_Component)
5052 then
5053 return False;
5054 else
5055 Root1 := Prefix (Root1);
5056 end if;
5058 Depth1 := Depth1 + 1;
5059 end loop;
5061 Root2 := Prefix (A2);
5062 while not Is_Entity_Name (Root2) loop
5063 if not Nkind_In (Root2, N_Selected_Component,
5064 N_Indexed_Component)
5065 then
5066 return False;
5067 else
5068 Root2 := Prefix (Root2);
5069 end if;
5071 Depth2 := Depth2 + 1;
5072 end loop;
5074 -- If both have the same depth and they do not denote the same
5075 -- object, they are disjoint and no warning is needed.
5077 if Depth1 = Depth2 then
5078 return False;
5080 elsif Depth1 > Depth2 then
5081 Root1 := Prefix (A1);
5082 for J in 1 .. Depth1 - Depth2 - 1 loop
5083 Root1 := Prefix (Root1);
5084 end loop;
5086 return Denotes_Same_Object (Root1, A2);
5088 else
5089 Root2 := Prefix (A2);
5090 for J in 1 .. Depth2 - Depth1 - 1 loop
5091 Root2 := Prefix (Root2);
5092 end loop;
5094 return Denotes_Same_Object (A1, Root2);
5095 end if;
5096 end;
5098 else
5099 return False;
5100 end if;
5101 end Denotes_Same_Prefix;
5103 ----------------------
5104 -- Denotes_Variable --
5105 ----------------------
5107 function Denotes_Variable (N : Node_Id) return Boolean is
5108 begin
5109 return Is_Variable (N) and then Paren_Count (N) = 0;
5110 end Denotes_Variable;
5112 -----------------------------
5113 -- Depends_On_Discriminant --
5114 -----------------------------
5116 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5117 L : Node_Id;
5118 H : Node_Id;
5120 begin
5121 Get_Index_Bounds (N, L, H);
5122 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5123 end Depends_On_Discriminant;
5125 -------------------------
5126 -- Designate_Same_Unit --
5127 -------------------------
5129 function Designate_Same_Unit
5130 (Name1 : Node_Id;
5131 Name2 : Node_Id) return Boolean
5133 K1 : constant Node_Kind := Nkind (Name1);
5134 K2 : constant Node_Kind := Nkind (Name2);
5136 function Prefix_Node (N : Node_Id) return Node_Id;
5137 -- Returns the parent unit name node of a defining program unit name
5138 -- or the prefix if N is a selected component or an expanded name.
5140 function Select_Node (N : Node_Id) return Node_Id;
5141 -- Returns the defining identifier node of a defining program unit
5142 -- name or the selector node if N is a selected component or an
5143 -- expanded name.
5145 -----------------
5146 -- Prefix_Node --
5147 -----------------
5149 function Prefix_Node (N : Node_Id) return Node_Id is
5150 begin
5151 if Nkind (N) = N_Defining_Program_Unit_Name then
5152 return Name (N);
5153 else
5154 return Prefix (N);
5155 end if;
5156 end Prefix_Node;
5158 -----------------
5159 -- Select_Node --
5160 -----------------
5162 function Select_Node (N : Node_Id) return Node_Id is
5163 begin
5164 if Nkind (N) = N_Defining_Program_Unit_Name then
5165 return Defining_Identifier (N);
5166 else
5167 return Selector_Name (N);
5168 end if;
5169 end Select_Node;
5171 -- Start of processing for Designate_Same_Unit
5173 begin
5174 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
5175 and then
5176 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
5177 then
5178 return Chars (Name1) = Chars (Name2);
5180 elsif Nkind_In (K1, N_Expanded_Name,
5181 N_Selected_Component,
5182 N_Defining_Program_Unit_Name)
5183 and then
5184 Nkind_In (K2, N_Expanded_Name,
5185 N_Selected_Component,
5186 N_Defining_Program_Unit_Name)
5187 then
5188 return
5189 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5190 and then
5191 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5193 else
5194 return False;
5195 end if;
5196 end Designate_Same_Unit;
5198 ------------------------------------------
5199 -- function Dynamic_Accessibility_Level --
5200 ------------------------------------------
5202 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5203 E : Entity_Id;
5204 Loc : constant Source_Ptr := Sloc (Expr);
5206 function Make_Level_Literal (Level : Uint) return Node_Id;
5207 -- Construct an integer literal representing an accessibility level
5208 -- with its type set to Natural.
5210 ------------------------
5211 -- Make_Level_Literal --
5212 ------------------------
5214 function Make_Level_Literal (Level : Uint) return Node_Id is
5215 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5216 begin
5217 Set_Etype (Result, Standard_Natural);
5218 return Result;
5219 end Make_Level_Literal;
5221 -- Start of processing for Dynamic_Accessibility_Level
5223 begin
5224 if Is_Entity_Name (Expr) then
5225 E := Entity (Expr);
5227 if Present (Renamed_Object (E)) then
5228 return Dynamic_Accessibility_Level (Renamed_Object (E));
5229 end if;
5231 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5232 if Present (Extra_Accessibility (E)) then
5233 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5234 end if;
5235 end if;
5236 end if;
5238 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5240 case Nkind (Expr) is
5242 -- For access discriminant, the level of the enclosing object
5244 when N_Selected_Component =>
5245 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5246 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5247 E_Anonymous_Access_Type
5248 then
5249 return Make_Level_Literal (Object_Access_Level (Expr));
5250 end if;
5252 when N_Attribute_Reference =>
5253 case Get_Attribute_Id (Attribute_Name (Expr)) is
5255 -- For X'Access, the level of the prefix X
5257 when Attribute_Access =>
5258 return Make_Level_Literal
5259 (Object_Access_Level (Prefix (Expr)));
5261 -- Treat the unchecked attributes as library-level
5263 when Attribute_Unchecked_Access |
5264 Attribute_Unrestricted_Access =>
5265 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5267 -- No other access-valued attributes
5269 when others =>
5270 raise Program_Error;
5271 end case;
5273 when N_Allocator =>
5275 -- Unimplemented: depends on context. As an actual parameter where
5276 -- formal type is anonymous, use
5277 -- Scope_Depth (Current_Scope) + 1.
5278 -- For other cases, see 3.10.2(14/3) and following. ???
5280 null;
5282 when N_Type_Conversion =>
5283 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5285 -- Handle type conversions introduced for a rename of an
5286 -- Ada 2012 stand-alone object of an anonymous access type.
5288 return Dynamic_Accessibility_Level (Expression (Expr));
5289 end if;
5291 when others =>
5292 null;
5293 end case;
5295 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5296 end Dynamic_Accessibility_Level;
5298 -----------------------------------
5299 -- Effective_Extra_Accessibility --
5300 -----------------------------------
5302 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5303 begin
5304 if Present (Renamed_Object (Id))
5305 and then Is_Entity_Name (Renamed_Object (Id))
5306 then
5307 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5308 else
5309 return Extra_Accessibility (Id);
5310 end if;
5311 end Effective_Extra_Accessibility;
5313 -----------------------------
5314 -- Effective_Reads_Enabled --
5315 -----------------------------
5317 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5318 begin
5319 return Has_Enabled_Property (Id, Name_Effective_Reads);
5320 end Effective_Reads_Enabled;
5322 ------------------------------
5323 -- Effective_Writes_Enabled --
5324 ------------------------------
5326 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5327 begin
5328 return Has_Enabled_Property (Id, Name_Effective_Writes);
5329 end Effective_Writes_Enabled;
5331 ------------------------------
5332 -- Enclosing_Comp_Unit_Node --
5333 ------------------------------
5335 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5336 Current_Node : Node_Id;
5338 begin
5339 Current_Node := N;
5340 while Present (Current_Node)
5341 and then Nkind (Current_Node) /= N_Compilation_Unit
5342 loop
5343 Current_Node := Parent (Current_Node);
5344 end loop;
5346 if Nkind (Current_Node) /= N_Compilation_Unit then
5347 return Empty;
5348 else
5349 return Current_Node;
5350 end if;
5351 end Enclosing_Comp_Unit_Node;
5353 --------------------------
5354 -- Enclosing_CPP_Parent --
5355 --------------------------
5357 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5358 Parent_Typ : Entity_Id := Typ;
5360 begin
5361 while not Is_CPP_Class (Parent_Typ)
5362 and then Etype (Parent_Typ) /= Parent_Typ
5363 loop
5364 Parent_Typ := Etype (Parent_Typ);
5366 if Is_Private_Type (Parent_Typ) then
5367 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5368 end if;
5369 end loop;
5371 pragma Assert (Is_CPP_Class (Parent_Typ));
5372 return Parent_Typ;
5373 end Enclosing_CPP_Parent;
5375 ----------------------------
5376 -- Enclosing_Generic_Body --
5377 ----------------------------
5379 function Enclosing_Generic_Body
5380 (N : Node_Id) return Node_Id
5382 P : Node_Id;
5383 Decl : Node_Id;
5384 Spec : Node_Id;
5386 begin
5387 P := Parent (N);
5388 while Present (P) loop
5389 if Nkind (P) = N_Package_Body
5390 or else Nkind (P) = N_Subprogram_Body
5391 then
5392 Spec := Corresponding_Spec (P);
5394 if Present (Spec) then
5395 Decl := Unit_Declaration_Node (Spec);
5397 if Nkind (Decl) = N_Generic_Package_Declaration
5398 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5399 then
5400 return P;
5401 end if;
5402 end if;
5403 end if;
5405 P := Parent (P);
5406 end loop;
5408 return Empty;
5409 end Enclosing_Generic_Body;
5411 ----------------------------
5412 -- Enclosing_Generic_Unit --
5413 ----------------------------
5415 function Enclosing_Generic_Unit
5416 (N : Node_Id) return Node_Id
5418 P : Node_Id;
5419 Decl : Node_Id;
5420 Spec : Node_Id;
5422 begin
5423 P := Parent (N);
5424 while Present (P) loop
5425 if Nkind (P) = N_Generic_Package_Declaration
5426 or else Nkind (P) = N_Generic_Subprogram_Declaration
5427 then
5428 return P;
5430 elsif Nkind (P) = N_Package_Body
5431 or else Nkind (P) = N_Subprogram_Body
5432 then
5433 Spec := Corresponding_Spec (P);
5435 if Present (Spec) then
5436 Decl := Unit_Declaration_Node (Spec);
5438 if Nkind (Decl) = N_Generic_Package_Declaration
5439 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5440 then
5441 return Decl;
5442 end if;
5443 end if;
5444 end if;
5446 P := Parent (P);
5447 end loop;
5449 return Empty;
5450 end Enclosing_Generic_Unit;
5452 -------------------------------
5453 -- Enclosing_Lib_Unit_Entity --
5454 -------------------------------
5456 function Enclosing_Lib_Unit_Entity
5457 (E : Entity_Id := Current_Scope) return Entity_Id
5459 Unit_Entity : Entity_Id;
5461 begin
5462 -- Look for enclosing library unit entity by following scope links.
5463 -- Equivalent to, but faster than indexing through the scope stack.
5465 Unit_Entity := E;
5466 while (Present (Scope (Unit_Entity))
5467 and then Scope (Unit_Entity) /= Standard_Standard)
5468 and not Is_Child_Unit (Unit_Entity)
5469 loop
5470 Unit_Entity := Scope (Unit_Entity);
5471 end loop;
5473 return Unit_Entity;
5474 end Enclosing_Lib_Unit_Entity;
5476 -----------------------
5477 -- Enclosing_Package --
5478 -----------------------
5480 function Enclosing_Package (E : Entity_Id) return Entity_Id is
5481 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5483 begin
5484 if Dynamic_Scope = Standard_Standard then
5485 return Standard_Standard;
5487 elsif Dynamic_Scope = Empty then
5488 return Empty;
5490 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
5491 E_Generic_Package)
5492 then
5493 return Dynamic_Scope;
5495 else
5496 return Enclosing_Package (Dynamic_Scope);
5497 end if;
5498 end Enclosing_Package;
5500 --------------------------
5501 -- Enclosing_Subprogram --
5502 --------------------------
5504 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
5505 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5507 begin
5508 if Dynamic_Scope = Standard_Standard then
5509 return Empty;
5511 elsif Dynamic_Scope = Empty then
5512 return Empty;
5514 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
5515 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
5517 elsif Ekind (Dynamic_Scope) = E_Block
5518 or else Ekind (Dynamic_Scope) = E_Return_Statement
5519 then
5520 return Enclosing_Subprogram (Dynamic_Scope);
5522 elsif Ekind (Dynamic_Scope) = E_Task_Type then
5523 return Get_Task_Body_Procedure (Dynamic_Scope);
5525 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
5526 and then Present (Full_View (Dynamic_Scope))
5527 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
5528 then
5529 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
5531 -- No body is generated if the protected operation is eliminated
5533 elsif Convention (Dynamic_Scope) = Convention_Protected
5534 and then not Is_Eliminated (Dynamic_Scope)
5535 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
5536 then
5537 return Protected_Body_Subprogram (Dynamic_Scope);
5539 else
5540 return Dynamic_Scope;
5541 end if;
5542 end Enclosing_Subprogram;
5544 ------------------------
5545 -- Ensure_Freeze_Node --
5546 ------------------------
5548 procedure Ensure_Freeze_Node (E : Entity_Id) is
5549 FN : Node_Id;
5550 begin
5551 if No (Freeze_Node (E)) then
5552 FN := Make_Freeze_Entity (Sloc (E));
5553 Set_Has_Delayed_Freeze (E);
5554 Set_Freeze_Node (E, FN);
5555 Set_Access_Types_To_Process (FN, No_Elist);
5556 Set_TSS_Elist (FN, No_Elist);
5557 Set_Entity (FN, E);
5558 end if;
5559 end Ensure_Freeze_Node;
5561 ----------------
5562 -- Enter_Name --
5563 ----------------
5565 procedure Enter_Name (Def_Id : Entity_Id) is
5566 C : constant Entity_Id := Current_Entity (Def_Id);
5567 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
5568 S : constant Entity_Id := Current_Scope;
5570 begin
5571 Generate_Definition (Def_Id);
5573 -- Add new name to current scope declarations. Check for duplicate
5574 -- declaration, which may or may not be a genuine error.
5576 if Present (E) then
5578 -- Case of previous entity entered because of a missing declaration
5579 -- or else a bad subtype indication. Best is to use the new entity,
5580 -- and make the previous one invisible.
5582 if Etype (E) = Any_Type then
5583 Set_Is_Immediately_Visible (E, False);
5585 -- Case of renaming declaration constructed for package instances.
5586 -- if there is an explicit declaration with the same identifier,
5587 -- the renaming is not immediately visible any longer, but remains
5588 -- visible through selected component notation.
5590 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
5591 and then not Comes_From_Source (E)
5592 then
5593 Set_Is_Immediately_Visible (E, False);
5595 -- The new entity may be the package renaming, which has the same
5596 -- same name as a generic formal which has been seen already.
5598 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
5599 and then not Comes_From_Source (Def_Id)
5600 then
5601 Set_Is_Immediately_Visible (E, False);
5603 -- For a fat pointer corresponding to a remote access to subprogram,
5604 -- we use the same identifier as the RAS type, so that the proper
5605 -- name appears in the stub. This type is only retrieved through
5606 -- the RAS type and never by visibility, and is not added to the
5607 -- visibility list (see below).
5609 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
5610 and then Ekind (Def_Id) = E_Record_Type
5611 and then Present (Corresponding_Remote_Type (Def_Id))
5612 then
5613 null;
5615 -- Case of an implicit operation or derived literal. The new entity
5616 -- hides the implicit one, which is removed from all visibility,
5617 -- i.e. the entity list of its scope, and homonym chain of its name.
5619 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
5620 or else Is_Internal (E)
5621 then
5622 declare
5623 Prev : Entity_Id;
5624 Prev_Vis : Entity_Id;
5625 Decl : constant Node_Id := Parent (E);
5627 begin
5628 -- If E is an implicit declaration, it cannot be the first
5629 -- entity in the scope.
5631 Prev := First_Entity (Current_Scope);
5632 while Present (Prev) and then Next_Entity (Prev) /= E loop
5633 Next_Entity (Prev);
5634 end loop;
5636 if No (Prev) then
5638 -- If E is not on the entity chain of the current scope,
5639 -- it is an implicit declaration in the generic formal
5640 -- part of a generic subprogram. When analyzing the body,
5641 -- the generic formals are visible but not on the entity
5642 -- chain of the subprogram. The new entity will become
5643 -- the visible one in the body.
5645 pragma Assert
5646 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
5647 null;
5649 else
5650 Set_Next_Entity (Prev, Next_Entity (E));
5652 if No (Next_Entity (Prev)) then
5653 Set_Last_Entity (Current_Scope, Prev);
5654 end if;
5656 if E = Current_Entity (E) then
5657 Prev_Vis := Empty;
5659 else
5660 Prev_Vis := Current_Entity (E);
5661 while Homonym (Prev_Vis) /= E loop
5662 Prev_Vis := Homonym (Prev_Vis);
5663 end loop;
5664 end if;
5666 if Present (Prev_Vis) then
5668 -- Skip E in the visibility chain
5670 Set_Homonym (Prev_Vis, Homonym (E));
5672 else
5673 Set_Name_Entity_Id (Chars (E), Homonym (E));
5674 end if;
5675 end if;
5676 end;
5678 -- This section of code could use a comment ???
5680 elsif Present (Etype (E))
5681 and then Is_Concurrent_Type (Etype (E))
5682 and then E = Def_Id
5683 then
5684 return;
5686 -- If the homograph is a protected component renaming, it should not
5687 -- be hiding the current entity. Such renamings are treated as weak
5688 -- declarations.
5690 elsif Is_Prival (E) then
5691 Set_Is_Immediately_Visible (E, False);
5693 -- In this case the current entity is a protected component renaming.
5694 -- Perform minimal decoration by setting the scope and return since
5695 -- the prival should not be hiding other visible entities.
5697 elsif Is_Prival (Def_Id) then
5698 Set_Scope (Def_Id, Current_Scope);
5699 return;
5701 -- Analogous to privals, the discriminal generated for an entry index
5702 -- parameter acts as a weak declaration. Perform minimal decoration
5703 -- to avoid bogus errors.
5705 elsif Is_Discriminal (Def_Id)
5706 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
5707 then
5708 Set_Scope (Def_Id, Current_Scope);
5709 return;
5711 -- In the body or private part of an instance, a type extension may
5712 -- introduce a component with the same name as that of an actual. The
5713 -- legality rule is not enforced, but the semantics of the full type
5714 -- with two components of same name are not clear at this point???
5716 elsif In_Instance_Not_Visible then
5717 null;
5719 -- When compiling a package body, some child units may have become
5720 -- visible. They cannot conflict with local entities that hide them.
5722 elsif Is_Child_Unit (E)
5723 and then In_Open_Scopes (Scope (E))
5724 and then not Is_Immediately_Visible (E)
5725 then
5726 null;
5728 -- Conversely, with front-end inlining we may compile the parent body
5729 -- first, and a child unit subsequently. The context is now the
5730 -- parent spec, and body entities are not visible.
5732 elsif Is_Child_Unit (Def_Id)
5733 and then Is_Package_Body_Entity (E)
5734 and then not In_Package_Body (Current_Scope)
5735 then
5736 null;
5738 -- Case of genuine duplicate declaration
5740 else
5741 Error_Msg_Sloc := Sloc (E);
5743 -- If the previous declaration is an incomplete type declaration
5744 -- this may be an attempt to complete it with a private type. The
5745 -- following avoids confusing cascaded errors.
5747 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
5748 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
5749 then
5750 Error_Msg_N
5751 ("incomplete type cannot be completed with a private " &
5752 "declaration", Parent (Def_Id));
5753 Set_Is_Immediately_Visible (E, False);
5754 Set_Full_View (E, Def_Id);
5756 -- An inherited component of a record conflicts with a new
5757 -- discriminant. The discriminant is inserted first in the scope,
5758 -- but the error should be posted on it, not on the component.
5760 elsif Ekind (E) = E_Discriminant
5761 and then Present (Scope (Def_Id))
5762 and then Scope (Def_Id) /= Current_Scope
5763 then
5764 Error_Msg_Sloc := Sloc (Def_Id);
5765 Error_Msg_N ("& conflicts with declaration#", E);
5766 return;
5768 -- If the name of the unit appears in its own context clause, a
5769 -- dummy package with the name has already been created, and the
5770 -- error emitted. Try to continue quietly.
5772 elsif Error_Posted (E)
5773 and then Sloc (E) = No_Location
5774 and then Nkind (Parent (E)) = N_Package_Specification
5775 and then Current_Scope = Standard_Standard
5776 then
5777 Set_Scope (Def_Id, Current_Scope);
5778 return;
5780 else
5781 Error_Msg_N ("& conflicts with declaration#", Def_Id);
5783 -- Avoid cascaded messages with duplicate components in
5784 -- derived types.
5786 if Ekind_In (E, E_Component, E_Discriminant) then
5787 return;
5788 end if;
5789 end if;
5791 if Nkind (Parent (Parent (Def_Id))) =
5792 N_Generic_Subprogram_Declaration
5793 and then Def_Id =
5794 Defining_Entity (Specification (Parent (Parent (Def_Id))))
5795 then
5796 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
5797 end if;
5799 -- If entity is in standard, then we are in trouble, because it
5800 -- means that we have a library package with a duplicated name.
5801 -- That's hard to recover from, so abort.
5803 if S = Standard_Standard then
5804 raise Unrecoverable_Error;
5806 -- Otherwise we continue with the declaration. Having two
5807 -- identical declarations should not cause us too much trouble.
5809 else
5810 null;
5811 end if;
5812 end if;
5813 end if;
5815 -- If we fall through, declaration is OK, at least OK enough to continue
5817 -- If Def_Id is a discriminant or a record component we are in the midst
5818 -- of inheriting components in a derived record definition. Preserve
5819 -- their Ekind and Etype.
5821 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
5822 null;
5824 -- If a type is already set, leave it alone (happens when a type
5825 -- declaration is reanalyzed following a call to the optimizer).
5827 elsif Present (Etype (Def_Id)) then
5828 null;
5830 -- Otherwise, the kind E_Void insures that premature uses of the entity
5831 -- will be detected. Any_Type insures that no cascaded errors will occur
5833 else
5834 Set_Ekind (Def_Id, E_Void);
5835 Set_Etype (Def_Id, Any_Type);
5836 end if;
5838 -- Inherited discriminants and components in derived record types are
5839 -- immediately visible. Itypes are not.
5841 -- Unless the Itype is for a record type with a corresponding remote
5842 -- type (what is that about, it was not commented ???)
5844 if Ekind_In (Def_Id, E_Discriminant, E_Component)
5845 or else
5846 ((not Is_Record_Type (Def_Id)
5847 or else No (Corresponding_Remote_Type (Def_Id)))
5848 and then not Is_Itype (Def_Id))
5849 then
5850 Set_Is_Immediately_Visible (Def_Id);
5851 Set_Current_Entity (Def_Id);
5852 end if;
5854 Set_Homonym (Def_Id, C);
5855 Append_Entity (Def_Id, S);
5856 Set_Public_Status (Def_Id);
5858 -- Declaring a homonym is not allowed in SPARK ...
5860 if Present (C) and then Restriction_Check_Required (SPARK_05) then
5861 declare
5862 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
5863 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
5864 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
5866 begin
5867 -- ... unless the new declaration is in a subprogram, and the
5868 -- visible declaration is a variable declaration or a parameter
5869 -- specification outside that subprogram.
5871 if Present (Enclosing_Subp)
5872 and then Nkind_In (Parent (C), N_Object_Declaration,
5873 N_Parameter_Specification)
5874 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
5875 then
5876 null;
5878 -- ... or the new declaration is in a package, and the visible
5879 -- declaration occurs outside that package.
5881 elsif Present (Enclosing_Pack)
5882 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
5883 then
5884 null;
5886 -- ... or the new declaration is a component declaration in a
5887 -- record type definition.
5889 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
5890 null;
5892 -- Don't issue error for non-source entities
5894 elsif Comes_From_Source (Def_Id)
5895 and then Comes_From_Source (C)
5896 then
5897 Error_Msg_Sloc := Sloc (C);
5898 Check_SPARK_05_Restriction
5899 ("redeclaration of identifier &#", Def_Id);
5900 end if;
5901 end;
5902 end if;
5904 -- Warn if new entity hides an old one
5906 if Warn_On_Hiding and then Present (C)
5908 -- Don't warn for record components since they always have a well
5909 -- defined scope which does not confuse other uses. Note that in
5910 -- some cases, Ekind has not been set yet.
5912 and then Ekind (C) /= E_Component
5913 and then Ekind (C) /= E_Discriminant
5914 and then Nkind (Parent (C)) /= N_Component_Declaration
5915 and then Ekind (Def_Id) /= E_Component
5916 and then Ekind (Def_Id) /= E_Discriminant
5917 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
5919 -- Don't warn for one character variables. It is too common to use
5920 -- such variables as locals and will just cause too many false hits.
5922 and then Length_Of_Name (Chars (C)) /= 1
5924 -- Don't warn for non-source entities
5926 and then Comes_From_Source (C)
5927 and then Comes_From_Source (Def_Id)
5929 -- Don't warn unless entity in question is in extended main source
5931 and then In_Extended_Main_Source_Unit (Def_Id)
5933 -- Finally, the hidden entity must be either immediately visible or
5934 -- use visible (i.e. from a used package).
5936 and then
5937 (Is_Immediately_Visible (C)
5938 or else
5939 Is_Potentially_Use_Visible (C))
5940 then
5941 Error_Msg_Sloc := Sloc (C);
5942 Error_Msg_N ("declaration hides &#?h?", Def_Id);
5943 end if;
5944 end Enter_Name;
5946 ---------------
5947 -- Entity_Of --
5948 ---------------
5950 function Entity_Of (N : Node_Id) return Entity_Id is
5951 Id : Entity_Id;
5953 begin
5954 Id := Empty;
5956 if Is_Entity_Name (N) then
5957 Id := Entity (N);
5959 -- Follow a possible chain of renamings to reach the root renamed
5960 -- object.
5962 while Present (Id) and then Present (Renamed_Object (Id)) loop
5963 if Is_Entity_Name (Renamed_Object (Id)) then
5964 Id := Entity (Renamed_Object (Id));
5965 else
5966 Id := Empty;
5967 exit;
5968 end if;
5969 end loop;
5970 end if;
5972 return Id;
5973 end Entity_Of;
5975 --------------------------
5976 -- Explain_Limited_Type --
5977 --------------------------
5979 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
5980 C : Entity_Id;
5982 begin
5983 -- For array, component type must be limited
5985 if Is_Array_Type (T) then
5986 Error_Msg_Node_2 := T;
5987 Error_Msg_NE
5988 ("\component type& of type& is limited", N, Component_Type (T));
5989 Explain_Limited_Type (Component_Type (T), N);
5991 elsif Is_Record_Type (T) then
5993 -- No need for extra messages if explicit limited record
5995 if Is_Limited_Record (Base_Type (T)) then
5996 return;
5997 end if;
5999 -- Otherwise find a limited component. Check only components that
6000 -- come from source, or inherited components that appear in the
6001 -- source of the ancestor.
6003 C := First_Component (T);
6004 while Present (C) loop
6005 if Is_Limited_Type (Etype (C))
6006 and then
6007 (Comes_From_Source (C)
6008 or else
6009 (Present (Original_Record_Component (C))
6010 and then
6011 Comes_From_Source (Original_Record_Component (C))))
6012 then
6013 Error_Msg_Node_2 := T;
6014 Error_Msg_NE ("\component& of type& has limited type", N, C);
6015 Explain_Limited_Type (Etype (C), N);
6016 return;
6017 end if;
6019 Next_Component (C);
6020 end loop;
6022 -- The type may be declared explicitly limited, even if no component
6023 -- of it is limited, in which case we fall out of the loop.
6024 return;
6025 end if;
6026 end Explain_Limited_Type;
6028 -------------------------------
6029 -- Extensions_Visible_Status --
6030 -------------------------------
6032 function Extensions_Visible_Status
6033 (Id : Entity_Id) return Extensions_Visible_Mode
6035 Arg : Node_Id;
6036 Decl : Node_Id;
6037 Expr : Node_Id;
6038 Prag : Node_Id;
6039 Subp : Entity_Id;
6041 begin
6042 -- When a formal parameter is subject to Extensions_Visible, the pragma
6043 -- is stored in the contract of related subprogram.
6045 if Is_Formal (Id) then
6046 Subp := Scope (Id);
6048 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
6049 Subp := Id;
6051 -- No other construct carries this pragma
6053 else
6054 return Extensions_Visible_None;
6055 end if;
6057 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
6059 -- In certain cases analysis may request the Extensions_Visible status
6060 -- of an expression function before the pragma has been analyzed yet.
6061 -- Inspect the declarative items after the expression function looking
6062 -- for the pragma (if any).
6064 if No (Prag) and then Is_Expression_Function (Subp) then
6065 Decl := Next (Unit_Declaration_Node (Subp));
6066 while Present (Decl) loop
6067 if Nkind (Decl) = N_Pragma
6068 and then Pragma_Name (Decl) = Name_Extensions_Visible
6069 then
6070 Prag := Decl;
6071 exit;
6073 -- A source construct ends the region where Extensions_Visible may
6074 -- appear, stop the traversal. An expanded expression function is
6075 -- no longer a source construct, but it must still be recognized.
6077 elsif Comes_From_Source (Decl)
6078 or else
6079 (Nkind_In (Decl, N_Subprogram_Body,
6080 N_Subprogram_Declaration)
6081 and then Is_Expression_Function (Defining_Entity (Decl)))
6082 then
6083 exit;
6084 end if;
6086 Next (Decl);
6087 end loop;
6088 end if;
6090 -- Extract the value from the Boolean expression (if any)
6092 if Present (Prag) then
6093 Arg := First (Pragma_Argument_Associations (Prag));
6095 if Present (Arg) then
6096 Expr := Get_Pragma_Arg (Arg);
6098 -- When the associated subprogram is an expression function, the
6099 -- argument of the pragma may not have been analyzed.
6101 if not Analyzed (Expr) then
6102 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6103 end if;
6105 -- Guard against cascading errors when the argument of pragma
6106 -- Extensions_Visible is not a valid static Boolean expression.
6108 if Error_Posted (Expr) then
6109 return Extensions_Visible_None;
6111 elsif Is_True (Expr_Value (Expr)) then
6112 return Extensions_Visible_True;
6114 else
6115 return Extensions_Visible_False;
6116 end if;
6118 -- Otherwise the aspect or pragma defaults to True
6120 else
6121 return Extensions_Visible_True;
6122 end if;
6124 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
6125 -- directly specified. In SPARK code, its value defaults to "False".
6127 elsif SPARK_Mode = On then
6128 return Extensions_Visible_False;
6130 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
6131 -- "True".
6133 else
6134 return Extensions_Visible_True;
6135 end if;
6136 end Extensions_Visible_Status;
6138 -----------------
6139 -- Find_Actual --
6140 -----------------
6142 procedure Find_Actual
6143 (N : Node_Id;
6144 Formal : out Entity_Id;
6145 Call : out Node_Id)
6147 Parnt : constant Node_Id := Parent (N);
6148 Actual : Node_Id;
6150 begin
6151 if Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6152 and then N = Prefix (Parnt)
6153 then
6154 Find_Actual (Parnt, Formal, Call);
6155 return;
6157 elsif Nkind (Parnt) = N_Parameter_Association
6158 and then N = Explicit_Actual_Parameter (Parnt)
6159 then
6160 Call := Parent (Parnt);
6162 elsif Nkind (Parnt) in N_Subprogram_Call then
6163 Call := Parnt;
6165 else
6166 Formal := Empty;
6167 Call := Empty;
6168 return;
6169 end if;
6171 -- If we have a call to a subprogram look for the parameter. Note that
6172 -- we exclude overloaded calls, since we don't know enough to be sure
6173 -- of giving the right answer in this case.
6175 if Nkind_In (Call, N_Function_Call, N_Procedure_Call_Statement)
6176 and then Is_Entity_Name (Name (Call))
6177 and then Present (Entity (Name (Call)))
6178 and then Is_Overloadable (Entity (Name (Call)))
6179 and then not Is_Overloaded (Name (Call))
6180 then
6181 -- If node is name in call it is not an actual
6183 if N = Name (Call) then
6184 Call := Empty;
6185 Formal := Empty;
6186 return;
6187 end if;
6189 -- Fall here if we are definitely a parameter
6191 Actual := First_Actual (Call);
6192 Formal := First_Formal (Entity (Name (Call)));
6193 while Present (Formal) and then Present (Actual) loop
6194 if Actual = N then
6195 return;
6197 -- An actual that is the prefix in a prefixed call may have
6198 -- been rewritten in the call, after the deferred reference
6199 -- was collected. Check if sloc and kinds and names match.
6201 elsif Sloc (Actual) = Sloc (N)
6202 and then Nkind (Actual) = N_Identifier
6203 and then Nkind (Actual) = Nkind (N)
6204 and then Chars (Actual) = Chars (N)
6205 then
6206 return;
6208 else
6209 Actual := Next_Actual (Actual);
6210 Formal := Next_Formal (Formal);
6211 end if;
6212 end loop;
6213 end if;
6215 -- Fall through here if we did not find matching actual
6217 Formal := Empty;
6218 Call := Empty;
6219 end Find_Actual;
6221 ---------------------------
6222 -- Find_Body_Discriminal --
6223 ---------------------------
6225 function Find_Body_Discriminal
6226 (Spec_Discriminant : Entity_Id) return Entity_Id
6228 Tsk : Entity_Id;
6229 Disc : Entity_Id;
6231 begin
6232 -- If expansion is suppressed, then the scope can be the concurrent type
6233 -- itself rather than a corresponding concurrent record type.
6235 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6236 Tsk := Scope (Spec_Discriminant);
6238 else
6239 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6241 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6242 end if;
6244 -- Find discriminant of original concurrent type, and use its current
6245 -- discriminal, which is the renaming within the task/protected body.
6247 Disc := First_Discriminant (Tsk);
6248 while Present (Disc) loop
6249 if Chars (Disc) = Chars (Spec_Discriminant) then
6250 return Discriminal (Disc);
6251 end if;
6253 Next_Discriminant (Disc);
6254 end loop;
6256 -- That loop should always succeed in finding a matching entry and
6257 -- returning. Fatal error if not.
6259 raise Program_Error;
6260 end Find_Body_Discriminal;
6262 -------------------------------------
6263 -- Find_Corresponding_Discriminant --
6264 -------------------------------------
6266 function Find_Corresponding_Discriminant
6267 (Id : Node_Id;
6268 Typ : Entity_Id) return Entity_Id
6270 Par_Disc : Entity_Id;
6271 Old_Disc : Entity_Id;
6272 New_Disc : Entity_Id;
6274 begin
6275 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6277 -- The original type may currently be private, and the discriminant
6278 -- only appear on its full view.
6280 if Is_Private_Type (Scope (Par_Disc))
6281 and then not Has_Discriminants (Scope (Par_Disc))
6282 and then Present (Full_View (Scope (Par_Disc)))
6283 then
6284 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6285 else
6286 Old_Disc := First_Discriminant (Scope (Par_Disc));
6287 end if;
6289 if Is_Class_Wide_Type (Typ) then
6290 New_Disc := First_Discriminant (Root_Type (Typ));
6291 else
6292 New_Disc := First_Discriminant (Typ);
6293 end if;
6295 while Present (Old_Disc) and then Present (New_Disc) loop
6296 if Old_Disc = Par_Disc then
6297 return New_Disc;
6298 end if;
6300 Next_Discriminant (Old_Disc);
6301 Next_Discriminant (New_Disc);
6302 end loop;
6304 -- Should always find it
6306 raise Program_Error;
6307 end Find_Corresponding_Discriminant;
6309 ----------------------------------
6310 -- Find_Enclosing_Iterator_Loop --
6311 ----------------------------------
6313 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
6314 Constr : Node_Id;
6315 S : Entity_Id;
6317 begin
6318 -- Traverse the scope chain looking for an iterator loop. Such loops are
6319 -- usually transformed into blocks, hence the use of Original_Node.
6321 S := Id;
6322 while Present (S) and then S /= Standard_Standard loop
6323 if Ekind (S) = E_Loop
6324 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
6325 then
6326 Constr := Original_Node (Label_Construct (Parent (S)));
6328 if Nkind (Constr) = N_Loop_Statement
6329 and then Present (Iteration_Scheme (Constr))
6330 and then Nkind (Iterator_Specification
6331 (Iteration_Scheme (Constr))) =
6332 N_Iterator_Specification
6333 then
6334 return S;
6335 end if;
6336 end if;
6338 S := Scope (S);
6339 end loop;
6341 return Empty;
6342 end Find_Enclosing_Iterator_Loop;
6344 ------------------------------------
6345 -- Find_Loop_In_Conditional_Block --
6346 ------------------------------------
6348 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
6349 Stmt : Node_Id;
6351 begin
6352 Stmt := N;
6354 if Nkind (Stmt) = N_If_Statement then
6355 Stmt := First (Then_Statements (Stmt));
6356 end if;
6358 pragma Assert (Nkind (Stmt) = N_Block_Statement);
6360 -- Inspect the statements of the conditional block. In general the loop
6361 -- should be the first statement in the statement sequence of the block,
6362 -- but the finalization machinery may have introduced extra object
6363 -- declarations.
6365 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
6366 while Present (Stmt) loop
6367 if Nkind (Stmt) = N_Loop_Statement then
6368 return Stmt;
6369 end if;
6371 Next (Stmt);
6372 end loop;
6374 -- The expansion of attribute 'Loop_Entry produced a malformed block
6376 raise Program_Error;
6377 end Find_Loop_In_Conditional_Block;
6379 --------------------------
6380 -- Find_Overlaid_Entity --
6381 --------------------------
6383 procedure Find_Overlaid_Entity
6384 (N : Node_Id;
6385 Ent : out Entity_Id;
6386 Off : out Boolean)
6388 Expr : Node_Id;
6390 begin
6391 -- We are looking for one of the two following forms:
6393 -- for X'Address use Y'Address
6395 -- or
6397 -- Const : constant Address := expr;
6398 -- ...
6399 -- for X'Address use Const;
6401 -- In the second case, the expr is either Y'Address, or recursively a
6402 -- constant that eventually references Y'Address.
6404 Ent := Empty;
6405 Off := False;
6407 if Nkind (N) = N_Attribute_Definition_Clause
6408 and then Chars (N) = Name_Address
6409 then
6410 Expr := Expression (N);
6412 -- This loop checks the form of the expression for Y'Address,
6413 -- using recursion to deal with intermediate constants.
6415 loop
6416 -- Check for Y'Address
6418 if Nkind (Expr) = N_Attribute_Reference
6419 and then Attribute_Name (Expr) = Name_Address
6420 then
6421 Expr := Prefix (Expr);
6422 exit;
6424 -- Check for Const where Const is a constant entity
6426 elsif Is_Entity_Name (Expr)
6427 and then Ekind (Entity (Expr)) = E_Constant
6428 then
6429 Expr := Constant_Value (Entity (Expr));
6431 -- Anything else does not need checking
6433 else
6434 return;
6435 end if;
6436 end loop;
6438 -- This loop checks the form of the prefix for an entity, using
6439 -- recursion to deal with intermediate components.
6441 loop
6442 -- Check for Y where Y is an entity
6444 if Is_Entity_Name (Expr) then
6445 Ent := Entity (Expr);
6446 return;
6448 -- Check for components
6450 elsif
6451 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
6452 then
6453 Expr := Prefix (Expr);
6454 Off := True;
6456 -- Anything else does not need checking
6458 else
6459 return;
6460 end if;
6461 end loop;
6462 end if;
6463 end Find_Overlaid_Entity;
6465 -------------------------
6466 -- Find_Parameter_Type --
6467 -------------------------
6469 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
6470 begin
6471 if Nkind (Param) /= N_Parameter_Specification then
6472 return Empty;
6474 -- For an access parameter, obtain the type from the formal entity
6475 -- itself, because access to subprogram nodes do not carry a type.
6476 -- Shouldn't we always use the formal entity ???
6478 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
6479 return Etype (Defining_Identifier (Param));
6481 else
6482 return Etype (Parameter_Type (Param));
6483 end if;
6484 end Find_Parameter_Type;
6486 -----------------------------------
6487 -- Find_Placement_In_State_Space --
6488 -----------------------------------
6490 procedure Find_Placement_In_State_Space
6491 (Item_Id : Entity_Id;
6492 Placement : out State_Space_Kind;
6493 Pack_Id : out Entity_Id)
6495 Context : Entity_Id;
6497 begin
6498 -- Assume that the item does not appear in the state space of a package
6500 Placement := Not_In_Package;
6501 Pack_Id := Empty;
6503 -- Climb the scope stack and examine the enclosing context
6505 Context := Scope (Item_Id);
6506 while Present (Context) and then Context /= Standard_Standard loop
6507 if Ekind (Context) = E_Package then
6508 Pack_Id := Context;
6510 -- A package body is a cut off point for the traversal as the item
6511 -- cannot be visible to the outside from this point on. Note that
6512 -- this test must be done first as a body is also classified as a
6513 -- private part.
6515 if In_Package_Body (Context) then
6516 Placement := Body_State_Space;
6517 return;
6519 -- The private part of a package is a cut off point for the
6520 -- traversal as the item cannot be visible to the outside from
6521 -- this point on.
6523 elsif In_Private_Part (Context) then
6524 Placement := Private_State_Space;
6525 return;
6527 -- When the item appears in the visible state space of a package,
6528 -- continue to climb the scope stack as this may not be the final
6529 -- state space.
6531 else
6532 Placement := Visible_State_Space;
6534 -- The visible state space of a child unit acts as the proper
6535 -- placement of an item.
6537 if Is_Child_Unit (Context) then
6538 return;
6539 end if;
6540 end if;
6542 -- The item or its enclosing package appear in a construct that has
6543 -- no state space.
6545 else
6546 Placement := Not_In_Package;
6547 return;
6548 end if;
6550 Context := Scope (Context);
6551 end loop;
6552 end Find_Placement_In_State_Space;
6554 ------------------------
6555 -- Find_Specific_Type --
6556 ------------------------
6558 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
6559 Typ : Entity_Id := Root_Type (CW);
6561 begin
6562 if Ekind (Typ) = E_Incomplete_Type then
6563 if From_Limited_With (Typ) then
6564 Typ := Non_Limited_View (Typ);
6565 else
6566 Typ := Full_View (Typ);
6567 end if;
6568 end if;
6570 if Is_Private_Type (Typ)
6571 and then not Is_Tagged_Type (Typ)
6572 and then Present (Full_View (Typ))
6573 then
6574 return Full_View (Typ);
6575 else
6576 return Typ;
6577 end if;
6578 end Find_Specific_Type;
6580 -----------------------------
6581 -- Find_Static_Alternative --
6582 -----------------------------
6584 function Find_Static_Alternative (N : Node_Id) return Node_Id is
6585 Expr : constant Node_Id := Expression (N);
6586 Val : constant Uint := Expr_Value (Expr);
6587 Alt : Node_Id;
6588 Choice : Node_Id;
6590 begin
6591 Alt := First (Alternatives (N));
6593 Search : loop
6594 if Nkind (Alt) /= N_Pragma then
6595 Choice := First (Discrete_Choices (Alt));
6596 while Present (Choice) loop
6598 -- Others choice, always matches
6600 if Nkind (Choice) = N_Others_Choice then
6601 exit Search;
6603 -- Range, check if value is in the range
6605 elsif Nkind (Choice) = N_Range then
6606 exit Search when
6607 Val >= Expr_Value (Low_Bound (Choice))
6608 and then
6609 Val <= Expr_Value (High_Bound (Choice));
6611 -- Choice is a subtype name. Note that we know it must
6612 -- be a static subtype, since otherwise it would have
6613 -- been diagnosed as illegal.
6615 elsif Is_Entity_Name (Choice)
6616 and then Is_Type (Entity (Choice))
6617 then
6618 exit Search when Is_In_Range (Expr, Etype (Choice),
6619 Assume_Valid => False);
6621 -- Choice is a subtype indication
6623 elsif Nkind (Choice) = N_Subtype_Indication then
6624 declare
6625 C : constant Node_Id := Constraint (Choice);
6626 R : constant Node_Id := Range_Expression (C);
6628 begin
6629 exit Search when
6630 Val >= Expr_Value (Low_Bound (R))
6631 and then
6632 Val <= Expr_Value (High_Bound (R));
6633 end;
6635 -- Choice is a simple expression
6637 else
6638 exit Search when Val = Expr_Value (Choice);
6639 end if;
6641 Next (Choice);
6642 end loop;
6643 end if;
6645 Next (Alt);
6646 pragma Assert (Present (Alt));
6647 end loop Search;
6649 -- The above loop *must* terminate by finding a match, since
6650 -- we know the case statement is valid, and the value of the
6651 -- expression is known at compile time. When we fall out of
6652 -- the loop, Alt points to the alternative that we know will
6653 -- be selected at run time.
6655 return Alt;
6656 end Find_Static_Alternative;
6658 ------------------
6659 -- First_Actual --
6660 ------------------
6662 function First_Actual (Node : Node_Id) return Node_Id is
6663 N : Node_Id;
6665 begin
6666 if No (Parameter_Associations (Node)) then
6667 return Empty;
6668 end if;
6670 N := First (Parameter_Associations (Node));
6672 if Nkind (N) = N_Parameter_Association then
6673 return First_Named_Actual (Node);
6674 else
6675 return N;
6676 end if;
6677 end First_Actual;
6679 -----------------------
6680 -- Gather_Components --
6681 -----------------------
6683 procedure Gather_Components
6684 (Typ : Entity_Id;
6685 Comp_List : Node_Id;
6686 Governed_By : List_Id;
6687 Into : Elist_Id;
6688 Report_Errors : out Boolean)
6690 Assoc : Node_Id;
6691 Variant : Node_Id;
6692 Discrete_Choice : Node_Id;
6693 Comp_Item : Node_Id;
6695 Discrim : Entity_Id;
6696 Discrim_Name : Node_Id;
6697 Discrim_Value : Node_Id;
6699 begin
6700 Report_Errors := False;
6702 if No (Comp_List) or else Null_Present (Comp_List) then
6703 return;
6705 elsif Present (Component_Items (Comp_List)) then
6706 Comp_Item := First (Component_Items (Comp_List));
6708 else
6709 Comp_Item := Empty;
6710 end if;
6712 while Present (Comp_Item) loop
6714 -- Skip the tag of a tagged record, the interface tags, as well
6715 -- as all items that are not user components (anonymous types,
6716 -- rep clauses, Parent field, controller field).
6718 if Nkind (Comp_Item) = N_Component_Declaration then
6719 declare
6720 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
6721 begin
6722 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
6723 Append_Elmt (Comp, Into);
6724 end if;
6725 end;
6726 end if;
6728 Next (Comp_Item);
6729 end loop;
6731 if No (Variant_Part (Comp_List)) then
6732 return;
6733 else
6734 Discrim_Name := Name (Variant_Part (Comp_List));
6735 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
6736 end if;
6738 -- Look for the discriminant that governs this variant part.
6739 -- The discriminant *must* be in the Governed_By List
6741 Assoc := First (Governed_By);
6742 Find_Constraint : loop
6743 Discrim := First (Choices (Assoc));
6744 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
6745 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
6746 and then
6747 Chars (Corresponding_Discriminant (Entity (Discrim))) =
6748 Chars (Discrim_Name))
6749 or else Chars (Original_Record_Component (Entity (Discrim)))
6750 = Chars (Discrim_Name);
6752 if No (Next (Assoc)) then
6753 if not Is_Constrained (Typ)
6754 and then Is_Derived_Type (Typ)
6755 and then Present (Stored_Constraint (Typ))
6756 then
6757 -- If the type is a tagged type with inherited discriminants,
6758 -- use the stored constraint on the parent in order to find
6759 -- the values of discriminants that are otherwise hidden by an
6760 -- explicit constraint. Renamed discriminants are handled in
6761 -- the code above.
6763 -- If several parent discriminants are renamed by a single
6764 -- discriminant of the derived type, the call to obtain the
6765 -- Corresponding_Discriminant field only retrieves the last
6766 -- of them. We recover the constraint on the others from the
6767 -- Stored_Constraint as well.
6769 declare
6770 D : Entity_Id;
6771 C : Elmt_Id;
6773 begin
6774 D := First_Discriminant (Etype (Typ));
6775 C := First_Elmt (Stored_Constraint (Typ));
6776 while Present (D) and then Present (C) loop
6777 if Chars (Discrim_Name) = Chars (D) then
6778 if Is_Entity_Name (Node (C))
6779 and then Entity (Node (C)) = Entity (Discrim)
6780 then
6781 -- D is renamed by Discrim, whose value is given in
6782 -- Assoc.
6784 null;
6786 else
6787 Assoc :=
6788 Make_Component_Association (Sloc (Typ),
6789 New_List
6790 (New_Occurrence_Of (D, Sloc (Typ))),
6791 Duplicate_Subexpr_No_Checks (Node (C)));
6792 end if;
6793 exit Find_Constraint;
6794 end if;
6796 Next_Discriminant (D);
6797 Next_Elmt (C);
6798 end loop;
6799 end;
6800 end if;
6801 end if;
6803 if No (Next (Assoc)) then
6804 Error_Msg_NE (" missing value for discriminant&",
6805 First (Governed_By), Discrim_Name);
6806 Report_Errors := True;
6807 return;
6808 end if;
6810 Next (Assoc);
6811 end loop Find_Constraint;
6813 Discrim_Value := Expression (Assoc);
6815 if not Is_OK_Static_Expression (Discrim_Value) then
6817 -- If the variant part is governed by a discriminant of the type
6818 -- this is an error. If the variant part and the discriminant are
6819 -- inherited from an ancestor this is legal (AI05-120) unless the
6820 -- components are being gathered for an aggregate, in which case
6821 -- the caller must check Report_Errors.
6823 if Scope (Original_Record_Component
6824 ((Entity (First (Choices (Assoc)))))) = Typ
6825 then
6826 Error_Msg_FE
6827 ("value for discriminant & must be static!",
6828 Discrim_Value, Discrim);
6829 Why_Not_Static (Discrim_Value);
6830 end if;
6832 Report_Errors := True;
6833 return;
6834 end if;
6836 Search_For_Discriminant_Value : declare
6837 Low : Node_Id;
6838 High : Node_Id;
6840 UI_High : Uint;
6841 UI_Low : Uint;
6842 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
6844 begin
6845 Find_Discrete_Value : while Present (Variant) loop
6846 Discrete_Choice := First (Discrete_Choices (Variant));
6847 while Present (Discrete_Choice) loop
6848 exit Find_Discrete_Value when
6849 Nkind (Discrete_Choice) = N_Others_Choice;
6851 Get_Index_Bounds (Discrete_Choice, Low, High);
6853 UI_Low := Expr_Value (Low);
6854 UI_High := Expr_Value (High);
6856 exit Find_Discrete_Value when
6857 UI_Low <= UI_Discrim_Value
6858 and then
6859 UI_High >= UI_Discrim_Value;
6861 Next (Discrete_Choice);
6862 end loop;
6864 Next_Non_Pragma (Variant);
6865 end loop Find_Discrete_Value;
6866 end Search_For_Discriminant_Value;
6868 if No (Variant) then
6869 Error_Msg_NE
6870 ("value of discriminant & is out of range", Discrim_Value, Discrim);
6871 Report_Errors := True;
6872 return;
6873 end if;
6875 -- If we have found the corresponding choice, recursively add its
6876 -- components to the Into list.
6878 Gather_Components
6879 (Empty, Component_List (Variant), Governed_By, Into, Report_Errors);
6880 end Gather_Components;
6882 ------------------------
6883 -- Get_Actual_Subtype --
6884 ------------------------
6886 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
6887 Typ : constant Entity_Id := Etype (N);
6888 Utyp : Entity_Id := Underlying_Type (Typ);
6889 Decl : Node_Id;
6890 Atyp : Entity_Id;
6892 begin
6893 if No (Utyp) then
6894 Utyp := Typ;
6895 end if;
6897 -- If what we have is an identifier that references a subprogram
6898 -- formal, or a variable or constant object, then we get the actual
6899 -- subtype from the referenced entity if one has been built.
6901 if Nkind (N) = N_Identifier
6902 and then
6903 (Is_Formal (Entity (N))
6904 or else Ekind (Entity (N)) = E_Constant
6905 or else Ekind (Entity (N)) = E_Variable)
6906 and then Present (Actual_Subtype (Entity (N)))
6907 then
6908 return Actual_Subtype (Entity (N));
6910 -- Actual subtype of unchecked union is always itself. We never need
6911 -- the "real" actual subtype. If we did, we couldn't get it anyway
6912 -- because the discriminant is not available. The restrictions on
6913 -- Unchecked_Union are designed to make sure that this is OK.
6915 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
6916 return Typ;
6918 -- Here for the unconstrained case, we must find actual subtype
6919 -- No actual subtype is available, so we must build it on the fly.
6921 -- Checking the type, not the underlying type, for constrainedness
6922 -- seems to be necessary. Maybe all the tests should be on the type???
6924 elsif (not Is_Constrained (Typ))
6925 and then (Is_Array_Type (Utyp)
6926 or else (Is_Record_Type (Utyp)
6927 and then Has_Discriminants (Utyp)))
6928 and then not Has_Unknown_Discriminants (Utyp)
6929 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
6930 then
6931 -- Nothing to do if in spec expression (why not???)
6933 if In_Spec_Expression then
6934 return Typ;
6936 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
6938 -- If the type has no discriminants, there is no subtype to
6939 -- build, even if the underlying type is discriminated.
6941 return Typ;
6943 -- Else build the actual subtype
6945 else
6946 Decl := Build_Actual_Subtype (Typ, N);
6947 Atyp := Defining_Identifier (Decl);
6949 -- If Build_Actual_Subtype generated a new declaration then use it
6951 if Atyp /= Typ then
6953 -- The actual subtype is an Itype, so analyze the declaration,
6954 -- but do not attach it to the tree, to get the type defined.
6956 Set_Parent (Decl, N);
6957 Set_Is_Itype (Atyp);
6958 Analyze (Decl, Suppress => All_Checks);
6959 Set_Associated_Node_For_Itype (Atyp, N);
6960 Set_Has_Delayed_Freeze (Atyp, False);
6962 -- We need to freeze the actual subtype immediately. This is
6963 -- needed, because otherwise this Itype will not get frozen
6964 -- at all, and it is always safe to freeze on creation because
6965 -- any associated types must be frozen at this point.
6967 Freeze_Itype (Atyp, N);
6968 return Atyp;
6970 -- Otherwise we did not build a declaration, so return original
6972 else
6973 return Typ;
6974 end if;
6975 end if;
6977 -- For all remaining cases, the actual subtype is the same as
6978 -- the nominal type.
6980 else
6981 return Typ;
6982 end if;
6983 end Get_Actual_Subtype;
6985 -------------------------------------
6986 -- Get_Actual_Subtype_If_Available --
6987 -------------------------------------
6989 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
6990 Typ : constant Entity_Id := Etype (N);
6992 begin
6993 -- If what we have is an identifier that references a subprogram
6994 -- formal, or a variable or constant object, then we get the actual
6995 -- subtype from the referenced entity if one has been built.
6997 if Nkind (N) = N_Identifier
6998 and then
6999 (Is_Formal (Entity (N))
7000 or else Ekind (Entity (N)) = E_Constant
7001 or else Ekind (Entity (N)) = E_Variable)
7002 and then Present (Actual_Subtype (Entity (N)))
7003 then
7004 return Actual_Subtype (Entity (N));
7006 -- Otherwise the Etype of N is returned unchanged
7008 else
7009 return Typ;
7010 end if;
7011 end Get_Actual_Subtype_If_Available;
7013 ------------------------
7014 -- Get_Body_From_Stub --
7015 ------------------------
7017 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
7018 begin
7019 return Proper_Body (Unit (Library_Unit (N)));
7020 end Get_Body_From_Stub;
7022 ---------------------
7023 -- Get_Cursor_Type --
7024 ---------------------
7026 function Get_Cursor_Type
7027 (Aspect : Node_Id;
7028 Typ : Entity_Id) return Entity_Id
7030 Assoc : Node_Id;
7031 Func : Entity_Id;
7032 First_Op : Entity_Id;
7033 Cursor : Entity_Id;
7035 begin
7036 -- If error already detected, return
7038 if Error_Posted (Aspect) then
7039 return Any_Type;
7040 end if;
7042 -- The cursor type for an Iterable aspect is the return type of a
7043 -- non-overloaded First primitive operation. Locate association for
7044 -- First.
7046 Assoc := First (Component_Associations (Expression (Aspect)));
7047 First_Op := Any_Id;
7048 while Present (Assoc) loop
7049 if Chars (First (Choices (Assoc))) = Name_First then
7050 First_Op := Expression (Assoc);
7051 exit;
7052 end if;
7054 Next (Assoc);
7055 end loop;
7057 if First_Op = Any_Id then
7058 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
7059 return Any_Type;
7060 end if;
7062 Cursor := Any_Type;
7064 -- Locate function with desired name and profile in scope of type
7066 Func := First_Entity (Scope (Typ));
7067 while Present (Func) loop
7068 if Chars (Func) = Chars (First_Op)
7069 and then Ekind (Func) = E_Function
7070 and then Present (First_Formal (Func))
7071 and then Etype (First_Formal (Func)) = Typ
7072 and then No (Next_Formal (First_Formal (Func)))
7073 then
7074 if Cursor /= Any_Type then
7075 Error_Msg_N
7076 ("Operation First for iterable type must be unique", Aspect);
7077 return Any_Type;
7078 else
7079 Cursor := Etype (Func);
7080 end if;
7081 end if;
7083 Next_Entity (Func);
7084 end loop;
7086 -- If not found, no way to resolve remaining primitives.
7088 if Cursor = Any_Type then
7089 Error_Msg_N
7090 ("No legal primitive operation First for Iterable type", Aspect);
7091 end if;
7093 return Cursor;
7094 end Get_Cursor_Type;
7096 -------------------------------
7097 -- Get_Default_External_Name --
7098 -------------------------------
7100 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
7101 begin
7102 Get_Decoded_Name_String (Chars (E));
7104 if Opt.External_Name_Imp_Casing = Uppercase then
7105 Set_Casing (All_Upper_Case);
7106 else
7107 Set_Casing (All_Lower_Case);
7108 end if;
7110 return
7111 Make_String_Literal (Sloc (E),
7112 Strval => String_From_Name_Buffer);
7113 end Get_Default_External_Name;
7115 --------------------------
7116 -- Get_Enclosing_Object --
7117 --------------------------
7119 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
7120 begin
7121 if Is_Entity_Name (N) then
7122 return Entity (N);
7123 else
7124 case Nkind (N) is
7125 when N_Indexed_Component |
7126 N_Slice |
7127 N_Selected_Component =>
7129 -- If not generating code, a dereference may be left implicit.
7130 -- In thoses cases, return Empty.
7132 if Is_Access_Type (Etype (Prefix (N))) then
7133 return Empty;
7134 else
7135 return Get_Enclosing_Object (Prefix (N));
7136 end if;
7138 when N_Type_Conversion =>
7139 return Get_Enclosing_Object (Expression (N));
7141 when others =>
7142 return Empty;
7143 end case;
7144 end if;
7145 end Get_Enclosing_Object;
7147 ---------------------------
7148 -- Get_Enum_Lit_From_Pos --
7149 ---------------------------
7151 function Get_Enum_Lit_From_Pos
7152 (T : Entity_Id;
7153 Pos : Uint;
7154 Loc : Source_Ptr) return Node_Id
7156 Btyp : Entity_Id := Base_Type (T);
7157 Lit : Node_Id;
7159 begin
7160 -- In the case where the literal is of type Character, Wide_Character
7161 -- or Wide_Wide_Character or of a type derived from them, there needs
7162 -- to be some special handling since there is no explicit chain of
7163 -- literals to search. Instead, an N_Character_Literal node is created
7164 -- with the appropriate Char_Code and Chars fields.
7166 if Is_Standard_Character_Type (T) then
7167 Set_Character_Literal_Name (UI_To_CC (Pos));
7168 return
7169 Make_Character_Literal (Loc,
7170 Chars => Name_Find,
7171 Char_Literal_Value => Pos);
7173 -- For all other cases, we have a complete table of literals, and
7174 -- we simply iterate through the chain of literal until the one
7175 -- with the desired position value is found.
7177 else
7178 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7179 Btyp := Full_View (Btyp);
7180 end if;
7182 Lit := First_Literal (Btyp);
7183 for J in 1 .. UI_To_Int (Pos) loop
7184 Next_Literal (Lit);
7185 end loop;
7187 return New_Occurrence_Of (Lit, Loc);
7188 end if;
7189 end Get_Enum_Lit_From_Pos;
7191 ------------------------
7192 -- Get_Generic_Entity --
7193 ------------------------
7195 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7196 Ent : constant Entity_Id := Entity (Name (N));
7197 begin
7198 if Present (Renamed_Object (Ent)) then
7199 return Renamed_Object (Ent);
7200 else
7201 return Ent;
7202 end if;
7203 end Get_Generic_Entity;
7205 -------------------------------------
7206 -- Get_Incomplete_View_Of_Ancestor --
7207 -------------------------------------
7209 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7210 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7211 Par_Scope : Entity_Id;
7212 Par_Type : Entity_Id;
7214 begin
7215 -- The incomplete view of an ancestor is only relevant for private
7216 -- derived types in child units.
7218 if not Is_Derived_Type (E)
7219 or else not Is_Child_Unit (Cur_Unit)
7220 then
7221 return Empty;
7223 else
7224 Par_Scope := Scope (Cur_Unit);
7225 if No (Par_Scope) then
7226 return Empty;
7227 end if;
7229 Par_Type := Etype (Base_Type (E));
7231 -- Traverse list of ancestor types until we find one declared in
7232 -- a parent or grandparent unit (two levels seem sufficient).
7234 while Present (Par_Type) loop
7235 if Scope (Par_Type) = Par_Scope
7236 or else Scope (Par_Type) = Scope (Par_Scope)
7237 then
7238 return Par_Type;
7240 elsif not Is_Derived_Type (Par_Type) then
7241 return Empty;
7243 else
7244 Par_Type := Etype (Base_Type (Par_Type));
7245 end if;
7246 end loop;
7248 -- If none found, there is no relevant ancestor type.
7250 return Empty;
7251 end if;
7252 end Get_Incomplete_View_Of_Ancestor;
7254 ----------------------
7255 -- Get_Index_Bounds --
7256 ----------------------
7258 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
7259 Kind : constant Node_Kind := Nkind (N);
7260 R : Node_Id;
7262 begin
7263 if Kind = N_Range then
7264 L := Low_Bound (N);
7265 H := High_Bound (N);
7267 elsif Kind = N_Subtype_Indication then
7268 R := Range_Expression (Constraint (N));
7270 if R = Error then
7271 L := Error;
7272 H := Error;
7273 return;
7275 else
7276 L := Low_Bound (Range_Expression (Constraint (N)));
7277 H := High_Bound (Range_Expression (Constraint (N)));
7278 end if;
7280 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
7281 if Error_Posted (Scalar_Range (Entity (N))) then
7282 L := Error;
7283 H := Error;
7285 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
7286 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
7288 else
7289 L := Low_Bound (Scalar_Range (Entity (N)));
7290 H := High_Bound (Scalar_Range (Entity (N)));
7291 end if;
7293 else
7294 -- N is an expression, indicating a range with one value
7296 L := N;
7297 H := N;
7298 end if;
7299 end Get_Index_Bounds;
7301 ---------------------------------
7302 -- Get_Iterable_Type_Primitive --
7303 ---------------------------------
7305 function Get_Iterable_Type_Primitive
7306 (Typ : Entity_Id;
7307 Nam : Name_Id) return Entity_Id
7309 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
7310 Assoc : Node_Id;
7312 begin
7313 if No (Funcs) then
7314 return Empty;
7316 else
7317 Assoc := First (Component_Associations (Funcs));
7318 while Present (Assoc) loop
7319 if Chars (First (Choices (Assoc))) = Nam then
7320 return Entity (Expression (Assoc));
7321 end if;
7323 Assoc := Next (Assoc);
7324 end loop;
7326 return Empty;
7327 end if;
7328 end Get_Iterable_Type_Primitive;
7330 ----------------------------------
7331 -- Get_Library_Unit_Name_string --
7332 ----------------------------------
7334 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
7335 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
7337 begin
7338 Get_Unit_Name_String (Unit_Name_Id);
7340 -- Remove seven last character (" (spec)" or " (body)")
7342 Name_Len := Name_Len - 7;
7343 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
7344 end Get_Library_Unit_Name_String;
7346 ------------------------
7347 -- Get_Name_Entity_Id --
7348 ------------------------
7350 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
7351 begin
7352 return Entity_Id (Get_Name_Table_Int (Id));
7353 end Get_Name_Entity_Id;
7355 ------------------------------
7356 -- Get_Name_From_CTC_Pragma --
7357 ------------------------------
7359 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
7360 Arg : constant Node_Id :=
7361 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
7362 begin
7363 return Strval (Expr_Value_S (Arg));
7364 end Get_Name_From_CTC_Pragma;
7366 -----------------------
7367 -- Get_Parent_Entity --
7368 -----------------------
7370 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
7371 begin
7372 if Nkind (Unit) = N_Package_Body
7373 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
7374 then
7375 return Defining_Entity
7376 (Specification (Instance_Spec (Original_Node (Unit))));
7377 elsif Nkind (Unit) = N_Package_Instantiation then
7378 return Defining_Entity (Specification (Instance_Spec (Unit)));
7379 else
7380 return Defining_Entity (Unit);
7381 end if;
7382 end Get_Parent_Entity;
7383 -------------------
7384 -- Get_Pragma_Id --
7385 -------------------
7387 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
7388 begin
7389 return Get_Pragma_Id (Pragma_Name (N));
7390 end Get_Pragma_Id;
7392 -----------------------
7393 -- Get_Reason_String --
7394 -----------------------
7396 procedure Get_Reason_String (N : Node_Id) is
7397 begin
7398 if Nkind (N) = N_String_Literal then
7399 Store_String_Chars (Strval (N));
7401 elsif Nkind (N) = N_Op_Concat then
7402 Get_Reason_String (Left_Opnd (N));
7403 Get_Reason_String (Right_Opnd (N));
7405 -- If not of required form, error
7407 else
7408 Error_Msg_N
7409 ("Reason for pragma Warnings has wrong form", N);
7410 Error_Msg_N
7411 ("\must be string literal or concatenation of string literals", N);
7412 return;
7413 end if;
7414 end Get_Reason_String;
7416 ---------------------------
7417 -- Get_Referenced_Object --
7418 ---------------------------
7420 function Get_Referenced_Object (N : Node_Id) return Node_Id is
7421 R : Node_Id;
7423 begin
7424 R := N;
7425 while Is_Entity_Name (R)
7426 and then Present (Renamed_Object (Entity (R)))
7427 loop
7428 R := Renamed_Object (Entity (R));
7429 end loop;
7431 return R;
7432 end Get_Referenced_Object;
7434 ------------------------
7435 -- Get_Renamed_Entity --
7436 ------------------------
7438 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
7439 R : Entity_Id;
7441 begin
7442 R := E;
7443 while Present (Renamed_Entity (R)) loop
7444 R := Renamed_Entity (R);
7445 end loop;
7447 return R;
7448 end Get_Renamed_Entity;
7450 -------------------------
7451 -- Get_Subprogram_Body --
7452 -------------------------
7454 function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
7455 Decl : Node_Id;
7457 begin
7458 Decl := Unit_Declaration_Node (E);
7460 if Nkind (Decl) = N_Subprogram_Body then
7461 return Decl;
7463 -- The below comment is bad, because it is possible for
7464 -- Nkind (Decl) to be an N_Subprogram_Body_Stub ???
7466 else -- Nkind (Decl) = N_Subprogram_Declaration
7468 if Present (Corresponding_Body (Decl)) then
7469 return Unit_Declaration_Node (Corresponding_Body (Decl));
7471 -- Imported subprogram case
7473 else
7474 return Empty;
7475 end if;
7476 end if;
7477 end Get_Subprogram_Body;
7479 ---------------------------
7480 -- Get_Subprogram_Entity --
7481 ---------------------------
7483 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
7484 Subp : Node_Id;
7485 Subp_Id : Entity_Id;
7487 begin
7488 if Nkind (Nod) = N_Accept_Statement then
7489 Subp := Entry_Direct_Name (Nod);
7491 elsif Nkind (Nod) = N_Slice then
7492 Subp := Prefix (Nod);
7494 else
7495 Subp := Name (Nod);
7496 end if;
7498 -- Strip the subprogram call
7500 loop
7501 if Nkind_In (Subp, N_Explicit_Dereference,
7502 N_Indexed_Component,
7503 N_Selected_Component)
7504 then
7505 Subp := Prefix (Subp);
7507 elsif Nkind_In (Subp, N_Type_Conversion,
7508 N_Unchecked_Type_Conversion)
7509 then
7510 Subp := Expression (Subp);
7512 else
7513 exit;
7514 end if;
7515 end loop;
7517 -- Extract the entity of the subprogram call
7519 if Is_Entity_Name (Subp) then
7520 Subp_Id := Entity (Subp);
7522 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
7523 Subp_Id := Directly_Designated_Type (Subp_Id);
7524 end if;
7526 if Is_Subprogram (Subp_Id) then
7527 return Subp_Id;
7528 else
7529 return Empty;
7530 end if;
7532 -- The search did not find a construct that denotes a subprogram
7534 else
7535 return Empty;
7536 end if;
7537 end Get_Subprogram_Entity;
7539 -----------------------------
7540 -- Get_Task_Body_Procedure --
7541 -----------------------------
7543 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
7544 begin
7545 -- Note: A task type may be the completion of a private type with
7546 -- discriminants. When performing elaboration checks on a task
7547 -- declaration, the current view of the type may be the private one,
7548 -- and the procedure that holds the body of the task is held in its
7549 -- underlying type.
7551 -- This is an odd function, why not have Task_Body_Procedure do
7552 -- the following digging???
7554 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
7555 end Get_Task_Body_Procedure;
7557 -----------------------
7558 -- Has_Access_Values --
7559 -----------------------
7561 function Has_Access_Values (T : Entity_Id) return Boolean is
7562 Typ : constant Entity_Id := Underlying_Type (T);
7564 begin
7565 -- Case of a private type which is not completed yet. This can only
7566 -- happen in the case of a generic format type appearing directly, or
7567 -- as a component of the type to which this function is being applied
7568 -- at the top level. Return False in this case, since we certainly do
7569 -- not know that the type contains access types.
7571 if No (Typ) then
7572 return False;
7574 elsif Is_Access_Type (Typ) then
7575 return True;
7577 elsif Is_Array_Type (Typ) then
7578 return Has_Access_Values (Component_Type (Typ));
7580 elsif Is_Record_Type (Typ) then
7581 declare
7582 Comp : Entity_Id;
7584 begin
7585 -- Loop to Check components
7587 Comp := First_Component_Or_Discriminant (Typ);
7588 while Present (Comp) loop
7590 -- Check for access component, tag field does not count, even
7591 -- though it is implemented internally using an access type.
7593 if Has_Access_Values (Etype (Comp))
7594 and then Chars (Comp) /= Name_uTag
7595 then
7596 return True;
7597 end if;
7599 Next_Component_Or_Discriminant (Comp);
7600 end loop;
7601 end;
7603 return False;
7605 else
7606 return False;
7607 end if;
7608 end Has_Access_Values;
7610 ------------------------------
7611 -- Has_Compatible_Alignment --
7612 ------------------------------
7614 function Has_Compatible_Alignment
7615 (Obj : Entity_Id;
7616 Expr : Node_Id) return Alignment_Result
7618 function Has_Compatible_Alignment_Internal
7619 (Obj : Entity_Id;
7620 Expr : Node_Id;
7621 Default : Alignment_Result) return Alignment_Result;
7622 -- This is the internal recursive function that actually does the work.
7623 -- There is one additional parameter, which says what the result should
7624 -- be if no alignment information is found, and there is no definite
7625 -- indication of compatible alignments. At the outer level, this is set
7626 -- to Unknown, but for internal recursive calls in the case where types
7627 -- are known to be correct, it is set to Known_Compatible.
7629 ---------------------------------------
7630 -- Has_Compatible_Alignment_Internal --
7631 ---------------------------------------
7633 function Has_Compatible_Alignment_Internal
7634 (Obj : Entity_Id;
7635 Expr : Node_Id;
7636 Default : Alignment_Result) return Alignment_Result
7638 Result : Alignment_Result := Known_Compatible;
7639 -- Holds the current status of the result. Note that once a value of
7640 -- Known_Incompatible is set, it is sticky and does not get changed
7641 -- to Unknown (the value in Result only gets worse as we go along,
7642 -- never better).
7644 Offs : Uint := No_Uint;
7645 -- Set to a factor of the offset from the base object when Expr is a
7646 -- selected or indexed component, based on Component_Bit_Offset and
7647 -- Component_Size respectively. A negative value is used to represent
7648 -- a value which is not known at compile time.
7650 procedure Check_Prefix;
7651 -- Checks the prefix recursively in the case where the expression
7652 -- is an indexed or selected component.
7654 procedure Set_Result (R : Alignment_Result);
7655 -- If R represents a worse outcome (unknown instead of known
7656 -- compatible, or known incompatible), then set Result to R.
7658 ------------------
7659 -- Check_Prefix --
7660 ------------------
7662 procedure Check_Prefix is
7663 begin
7664 -- The subtlety here is that in doing a recursive call to check
7665 -- the prefix, we have to decide what to do in the case where we
7666 -- don't find any specific indication of an alignment problem.
7668 -- At the outer level, we normally set Unknown as the result in
7669 -- this case, since we can only set Known_Compatible if we really
7670 -- know that the alignment value is OK, but for the recursive
7671 -- call, in the case where the types match, and we have not
7672 -- specified a peculiar alignment for the object, we are only
7673 -- concerned about suspicious rep clauses, the default case does
7674 -- not affect us, since the compiler will, in the absence of such
7675 -- rep clauses, ensure that the alignment is correct.
7677 if Default = Known_Compatible
7678 or else
7679 (Etype (Obj) = Etype (Expr)
7680 and then (Unknown_Alignment (Obj)
7681 or else
7682 Alignment (Obj) = Alignment (Etype (Obj))))
7683 then
7684 Set_Result
7685 (Has_Compatible_Alignment_Internal
7686 (Obj, Prefix (Expr), Known_Compatible));
7688 -- In all other cases, we need a full check on the prefix
7690 else
7691 Set_Result
7692 (Has_Compatible_Alignment_Internal
7693 (Obj, Prefix (Expr), Unknown));
7694 end if;
7695 end Check_Prefix;
7697 ----------------
7698 -- Set_Result --
7699 ----------------
7701 procedure Set_Result (R : Alignment_Result) is
7702 begin
7703 if R > Result then
7704 Result := R;
7705 end if;
7706 end Set_Result;
7708 -- Start of processing for Has_Compatible_Alignment_Internal
7710 begin
7711 -- If Expr is a selected component, we must make sure there is no
7712 -- potentially troublesome component clause, and that the record is
7713 -- not packed.
7715 if Nkind (Expr) = N_Selected_Component then
7717 -- Packed record always generate unknown alignment
7719 if Is_Packed (Etype (Prefix (Expr))) then
7720 Set_Result (Unknown);
7721 end if;
7723 -- Check prefix and component offset
7725 Check_Prefix;
7726 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
7728 -- If Expr is an indexed component, we must make sure there is no
7729 -- potentially troublesome Component_Size clause and that the array
7730 -- is not bit-packed.
7732 elsif Nkind (Expr) = N_Indexed_Component then
7733 declare
7734 Typ : constant Entity_Id := Etype (Prefix (Expr));
7735 Ind : constant Node_Id := First_Index (Typ);
7737 begin
7738 -- Bit packed array always generates unknown alignment
7740 if Is_Bit_Packed_Array (Typ) then
7741 Set_Result (Unknown);
7742 end if;
7744 -- Check prefix and component offset
7746 Check_Prefix;
7747 Offs := Component_Size (Typ);
7749 -- Small optimization: compute the full offset when possible
7751 if Offs /= No_Uint
7752 and then Offs > Uint_0
7753 and then Present (Ind)
7754 and then Nkind (Ind) = N_Range
7755 and then Compile_Time_Known_Value (Low_Bound (Ind))
7756 and then Compile_Time_Known_Value (First (Expressions (Expr)))
7757 then
7758 Offs := Offs * (Expr_Value (First (Expressions (Expr)))
7759 - Expr_Value (Low_Bound ((Ind))));
7760 end if;
7761 end;
7762 end if;
7764 -- If we have a null offset, the result is entirely determined by
7765 -- the base object and has already been computed recursively.
7767 if Offs = Uint_0 then
7768 null;
7770 -- Case where we know the alignment of the object
7772 elsif Known_Alignment (Obj) then
7773 declare
7774 ObjA : constant Uint := Alignment (Obj);
7775 ExpA : Uint := No_Uint;
7776 SizA : Uint := No_Uint;
7778 begin
7779 -- If alignment of Obj is 1, then we are always OK
7781 if ObjA = 1 then
7782 Set_Result (Known_Compatible);
7784 -- Alignment of Obj is greater than 1, so we need to check
7786 else
7787 -- If we have an offset, see if it is compatible
7789 if Offs /= No_Uint and Offs > Uint_0 then
7790 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
7791 Set_Result (Known_Incompatible);
7792 end if;
7794 -- See if Expr is an object with known alignment
7796 elsif Is_Entity_Name (Expr)
7797 and then Known_Alignment (Entity (Expr))
7798 then
7799 ExpA := Alignment (Entity (Expr));
7801 -- Otherwise, we can use the alignment of the type of
7802 -- Expr given that we already checked for
7803 -- discombobulating rep clauses for the cases of indexed
7804 -- and selected components above.
7806 elsif Known_Alignment (Etype (Expr)) then
7807 ExpA := Alignment (Etype (Expr));
7809 -- Otherwise the alignment is unknown
7811 else
7812 Set_Result (Default);
7813 end if;
7815 -- If we got an alignment, see if it is acceptable
7817 if ExpA /= No_Uint and then ExpA < ObjA then
7818 Set_Result (Known_Incompatible);
7819 end if;
7821 -- If Expr is not a piece of a larger object, see if size
7822 -- is given. If so, check that it is not too small for the
7823 -- required alignment.
7825 if Offs /= No_Uint then
7826 null;
7828 -- See if Expr is an object with known size
7830 elsif Is_Entity_Name (Expr)
7831 and then Known_Static_Esize (Entity (Expr))
7832 then
7833 SizA := Esize (Entity (Expr));
7835 -- Otherwise, we check the object size of the Expr type
7837 elsif Known_Static_Esize (Etype (Expr)) then
7838 SizA := Esize (Etype (Expr));
7839 end if;
7841 -- If we got a size, see if it is a multiple of the Obj
7842 -- alignment, if not, then the alignment cannot be
7843 -- acceptable, since the size is always a multiple of the
7844 -- alignment.
7846 if SizA /= No_Uint then
7847 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
7848 Set_Result (Known_Incompatible);
7849 end if;
7850 end if;
7851 end if;
7852 end;
7854 -- If we do not know required alignment, any non-zero offset is a
7855 -- potential problem (but certainly may be OK, so result is unknown).
7857 elsif Offs /= No_Uint then
7858 Set_Result (Unknown);
7860 -- If we can't find the result by direct comparison of alignment
7861 -- values, then there is still one case that we can determine known
7862 -- result, and that is when we can determine that the types are the
7863 -- same, and no alignments are specified. Then we known that the
7864 -- alignments are compatible, even if we don't know the alignment
7865 -- value in the front end.
7867 elsif Etype (Obj) = Etype (Expr) then
7869 -- Types are the same, but we have to check for possible size
7870 -- and alignments on the Expr object that may make the alignment
7871 -- different, even though the types are the same.
7873 if Is_Entity_Name (Expr) then
7875 -- First check alignment of the Expr object. Any alignment less
7876 -- than Maximum_Alignment is worrisome since this is the case
7877 -- where we do not know the alignment of Obj.
7879 if Known_Alignment (Entity (Expr))
7880 and then UI_To_Int (Alignment (Entity (Expr))) <
7881 Ttypes.Maximum_Alignment
7882 then
7883 Set_Result (Unknown);
7885 -- Now check size of Expr object. Any size that is not an
7886 -- even multiple of Maximum_Alignment is also worrisome
7887 -- since it may cause the alignment of the object to be less
7888 -- than the alignment of the type.
7890 elsif Known_Static_Esize (Entity (Expr))
7891 and then
7892 (UI_To_Int (Esize (Entity (Expr))) mod
7893 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
7894 /= 0
7895 then
7896 Set_Result (Unknown);
7898 -- Otherwise same type is decisive
7900 else
7901 Set_Result (Known_Compatible);
7902 end if;
7903 end if;
7905 -- Another case to deal with is when there is an explicit size or
7906 -- alignment clause when the types are not the same. If so, then the
7907 -- result is Unknown. We don't need to do this test if the Default is
7908 -- Unknown, since that result will be set in any case.
7910 elsif Default /= Unknown
7911 and then (Has_Size_Clause (Etype (Expr))
7912 or else
7913 Has_Alignment_Clause (Etype (Expr)))
7914 then
7915 Set_Result (Unknown);
7917 -- If no indication found, set default
7919 else
7920 Set_Result (Default);
7921 end if;
7923 -- Return worst result found
7925 return Result;
7926 end Has_Compatible_Alignment_Internal;
7928 -- Start of processing for Has_Compatible_Alignment
7930 begin
7931 -- If Obj has no specified alignment, then set alignment from the type
7932 -- alignment. Perhaps we should always do this, but for sure we should
7933 -- do it when there is an address clause since we can do more if the
7934 -- alignment is known.
7936 if Unknown_Alignment (Obj) then
7937 Set_Alignment (Obj, Alignment (Etype (Obj)));
7938 end if;
7940 -- Now do the internal call that does all the work
7942 return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
7943 end Has_Compatible_Alignment;
7945 ----------------------
7946 -- Has_Declarations --
7947 ----------------------
7949 function Has_Declarations (N : Node_Id) return Boolean is
7950 begin
7951 return Nkind_In (Nkind (N), N_Accept_Statement,
7952 N_Block_Statement,
7953 N_Compilation_Unit_Aux,
7954 N_Entry_Body,
7955 N_Package_Body,
7956 N_Protected_Body,
7957 N_Subprogram_Body,
7958 N_Task_Body,
7959 N_Package_Specification);
7960 end Has_Declarations;
7962 ---------------------------------
7963 -- Has_Defaulted_Discriminants --
7964 ---------------------------------
7966 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
7967 begin
7968 return Has_Discriminants (Typ)
7969 and then Present (First_Discriminant (Typ))
7970 and then Present (Discriminant_Default_Value
7971 (First_Discriminant (Typ)));
7972 end Has_Defaulted_Discriminants;
7974 -------------------
7975 -- Has_Denormals --
7976 -------------------
7978 function Has_Denormals (E : Entity_Id) return Boolean is
7979 begin
7980 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
7981 end Has_Denormals;
7983 -------------------------------------------
7984 -- Has_Discriminant_Dependent_Constraint --
7985 -------------------------------------------
7987 function Has_Discriminant_Dependent_Constraint
7988 (Comp : Entity_Id) return Boolean
7990 Comp_Decl : constant Node_Id := Parent (Comp);
7991 Subt_Indic : Node_Id;
7992 Constr : Node_Id;
7993 Assn : Node_Id;
7995 begin
7996 -- Discriminants can't depend on discriminants
7998 if Ekind (Comp) = E_Discriminant then
7999 return False;
8001 else
8002 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
8004 if Nkind (Subt_Indic) = N_Subtype_Indication then
8005 Constr := Constraint (Subt_Indic);
8007 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
8008 Assn := First (Constraints (Constr));
8009 while Present (Assn) loop
8010 case Nkind (Assn) is
8011 when N_Subtype_Indication |
8012 N_Range |
8013 N_Identifier
8015 if Depends_On_Discriminant (Assn) then
8016 return True;
8017 end if;
8019 when N_Discriminant_Association =>
8020 if Depends_On_Discriminant (Expression (Assn)) then
8021 return True;
8022 end if;
8024 when others =>
8025 null;
8026 end case;
8028 Next (Assn);
8029 end loop;
8030 end if;
8031 end if;
8032 end if;
8034 return False;
8035 end Has_Discriminant_Dependent_Constraint;
8037 --------------------------
8038 -- Has_Enabled_Property --
8039 --------------------------
8041 function Has_Enabled_Property
8042 (Item_Id : Entity_Id;
8043 Property : Name_Id) return Boolean
8045 function State_Has_Enabled_Property return Boolean;
8046 -- Determine whether a state denoted by Item_Id has the property enabled
8048 function Variable_Has_Enabled_Property return Boolean;
8049 -- Determine whether a variable denoted by Item_Id has the property
8050 -- enabled.
8052 --------------------------------
8053 -- State_Has_Enabled_Property --
8054 --------------------------------
8056 function State_Has_Enabled_Property return Boolean is
8057 Decl : constant Node_Id := Parent (Item_Id);
8058 Opt : Node_Id;
8059 Opt_Nam : Node_Id;
8060 Prop : Node_Id;
8061 Prop_Nam : Node_Id;
8062 Props : Node_Id;
8064 begin
8065 -- The declaration of an external abstract state appears as an
8066 -- extension aggregate. If this is not the case, properties can never
8067 -- be set.
8069 if Nkind (Decl) /= N_Extension_Aggregate then
8070 return False;
8071 end if;
8073 -- When External appears as a simple option, it automatically enables
8074 -- all properties.
8076 Opt := First (Expressions (Decl));
8077 while Present (Opt) loop
8078 if Nkind (Opt) = N_Identifier
8079 and then Chars (Opt) = Name_External
8080 then
8081 return True;
8082 end if;
8084 Next (Opt);
8085 end loop;
8087 -- When External specifies particular properties, inspect those and
8088 -- find the desired one (if any).
8090 Opt := First (Component_Associations (Decl));
8091 while Present (Opt) loop
8092 Opt_Nam := First (Choices (Opt));
8094 if Nkind (Opt_Nam) = N_Identifier
8095 and then Chars (Opt_Nam) = Name_External
8096 then
8097 Props := Expression (Opt);
8099 -- Multiple properties appear as an aggregate
8101 if Nkind (Props) = N_Aggregate then
8103 -- Simple property form
8105 Prop := First (Expressions (Props));
8106 while Present (Prop) loop
8107 if Chars (Prop) = Property then
8108 return True;
8109 end if;
8111 Next (Prop);
8112 end loop;
8114 -- Property with expression form
8116 Prop := First (Component_Associations (Props));
8117 while Present (Prop) loop
8118 Prop_Nam := First (Choices (Prop));
8120 -- The property can be represented in two ways:
8121 -- others => <value>
8122 -- <property> => <value>
8124 if Nkind (Prop_Nam) = N_Others_Choice
8125 or else (Nkind (Prop_Nam) = N_Identifier
8126 and then Chars (Prop_Nam) = Property)
8127 then
8128 return Is_True (Expr_Value (Expression (Prop)));
8129 end if;
8131 Next (Prop);
8132 end loop;
8134 -- Single property
8136 else
8137 return Chars (Props) = Property;
8138 end if;
8139 end if;
8141 Next (Opt);
8142 end loop;
8144 return False;
8145 end State_Has_Enabled_Property;
8147 -----------------------------------
8148 -- Variable_Has_Enabled_Property --
8149 -----------------------------------
8151 function Variable_Has_Enabled_Property return Boolean is
8152 function Is_Enabled (Prag : Node_Id) return Boolean;
8153 -- Determine whether property pragma Prag (if present) denotes an
8154 -- enabled property.
8156 ----------------
8157 -- Is_Enabled --
8158 ----------------
8160 function Is_Enabled (Prag : Node_Id) return Boolean is
8161 Arg2 : Node_Id;
8163 begin
8164 if Present (Prag) then
8165 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
8167 -- The pragma has an optional Boolean expression, the related
8168 -- property is enabled only when the expression evaluates to
8169 -- True.
8171 if Present (Arg2) then
8172 return Is_True (Expr_Value (Get_Pragma_Arg (Arg2)));
8174 -- Otherwise the lack of expression enables the property by
8175 -- default.
8177 else
8178 return True;
8179 end if;
8181 -- The property was never set in the first place
8183 else
8184 return False;
8185 end if;
8186 end Is_Enabled;
8188 -- Local variables
8190 AR : constant Node_Id :=
8191 Get_Pragma (Item_Id, Pragma_Async_Readers);
8192 AW : constant Node_Id :=
8193 Get_Pragma (Item_Id, Pragma_Async_Writers);
8194 ER : constant Node_Id :=
8195 Get_Pragma (Item_Id, Pragma_Effective_Reads);
8196 EW : constant Node_Id :=
8197 Get_Pragma (Item_Id, Pragma_Effective_Writes);
8199 -- Start of processing for Variable_Has_Enabled_Property
8201 begin
8202 -- A non-effectively volatile object can never possess external
8203 -- properties.
8205 if not Is_Effectively_Volatile (Item_Id) then
8206 return False;
8208 -- External properties related to variables come in two flavors -
8209 -- explicit and implicit. The explicit case is characterized by the
8210 -- presence of a property pragma with an optional Boolean flag. The
8211 -- property is enabled when the flag evaluates to True or the flag is
8212 -- missing altogether.
8214 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
8215 return True;
8217 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
8218 return True;
8220 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
8221 return True;
8223 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
8224 return True;
8226 -- The implicit case lacks all property pragmas
8228 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
8229 return True;
8231 else
8232 return False;
8233 end if;
8234 end Variable_Has_Enabled_Property;
8236 -- Start of processing for Has_Enabled_Property
8238 begin
8239 -- Abstract states and variables have a flexible scheme of specifying
8240 -- external properties.
8242 if Ekind (Item_Id) = E_Abstract_State then
8243 return State_Has_Enabled_Property;
8245 elsif Ekind (Item_Id) = E_Variable then
8246 return Variable_Has_Enabled_Property;
8248 -- Otherwise a property is enabled when the related item is effectively
8249 -- volatile.
8251 else
8252 return Is_Effectively_Volatile (Item_Id);
8253 end if;
8254 end Has_Enabled_Property;
8256 --------------------
8257 -- Has_Infinities --
8258 --------------------
8260 function Has_Infinities (E : Entity_Id) return Boolean is
8261 begin
8262 return
8263 Is_Floating_Point_Type (E)
8264 and then Nkind (Scalar_Range (E)) = N_Range
8265 and then Includes_Infinities (Scalar_Range (E));
8266 end Has_Infinities;
8268 --------------------
8269 -- Has_Interfaces --
8270 --------------------
8272 function Has_Interfaces
8273 (T : Entity_Id;
8274 Use_Full_View : Boolean := True) return Boolean
8276 Typ : Entity_Id := Base_Type (T);
8278 begin
8279 -- Handle concurrent types
8281 if Is_Concurrent_Type (Typ) then
8282 Typ := Corresponding_Record_Type (Typ);
8283 end if;
8285 if not Present (Typ)
8286 or else not Is_Record_Type (Typ)
8287 or else not Is_Tagged_Type (Typ)
8288 then
8289 return False;
8290 end if;
8292 -- Handle private types
8294 if Use_Full_View and then Present (Full_View (Typ)) then
8295 Typ := Full_View (Typ);
8296 end if;
8298 -- Handle concurrent record types
8300 if Is_Concurrent_Record_Type (Typ)
8301 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
8302 then
8303 return True;
8304 end if;
8306 loop
8307 if Is_Interface (Typ)
8308 or else
8309 (Is_Record_Type (Typ)
8310 and then Present (Interfaces (Typ))
8311 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
8312 then
8313 return True;
8314 end if;
8316 exit when Etype (Typ) = Typ
8318 -- Handle private types
8320 or else (Present (Full_View (Etype (Typ)))
8321 and then Full_View (Etype (Typ)) = Typ)
8323 -- Protect frontend against wrong sources with cyclic derivations
8325 or else Etype (Typ) = T;
8327 -- Climb to the ancestor type handling private types
8329 if Present (Full_View (Etype (Typ))) then
8330 Typ := Full_View (Etype (Typ));
8331 else
8332 Typ := Etype (Typ);
8333 end if;
8334 end loop;
8336 return False;
8337 end Has_Interfaces;
8339 ---------------------------------
8340 -- Has_No_Obvious_Side_Effects --
8341 ---------------------------------
8343 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
8344 begin
8345 -- For now, just handle literals, constants, and non-volatile
8346 -- variables and expressions combining these with operators or
8347 -- short circuit forms.
8349 if Nkind (N) in N_Numeric_Or_String_Literal then
8350 return True;
8352 elsif Nkind (N) = N_Character_Literal then
8353 return True;
8355 elsif Nkind (N) in N_Unary_Op then
8356 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
8358 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
8359 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
8360 and then
8361 Has_No_Obvious_Side_Effects (Right_Opnd (N));
8363 elsif Nkind (N) = N_Expression_With_Actions
8364 and then Is_Empty_List (Actions (N))
8365 then
8366 return Has_No_Obvious_Side_Effects (Expression (N));
8368 elsif Nkind (N) in N_Has_Entity then
8369 return Present (Entity (N))
8370 and then Ekind_In (Entity (N), E_Variable,
8371 E_Constant,
8372 E_Enumeration_Literal,
8373 E_In_Parameter,
8374 E_Out_Parameter,
8375 E_In_Out_Parameter)
8376 and then not Is_Volatile (Entity (N));
8378 else
8379 return False;
8380 end if;
8381 end Has_No_Obvious_Side_Effects;
8383 ------------------------
8384 -- Has_Null_Exclusion --
8385 ------------------------
8387 function Has_Null_Exclusion (N : Node_Id) return Boolean is
8388 begin
8389 case Nkind (N) is
8390 when N_Access_Definition |
8391 N_Access_Function_Definition |
8392 N_Access_Procedure_Definition |
8393 N_Access_To_Object_Definition |
8394 N_Allocator |
8395 N_Derived_Type_Definition |
8396 N_Function_Specification |
8397 N_Subtype_Declaration =>
8398 return Null_Exclusion_Present (N);
8400 when N_Component_Definition |
8401 N_Formal_Object_Declaration |
8402 N_Object_Renaming_Declaration =>
8403 if Present (Subtype_Mark (N)) then
8404 return Null_Exclusion_Present (N);
8405 else pragma Assert (Present (Access_Definition (N)));
8406 return Null_Exclusion_Present (Access_Definition (N));
8407 end if;
8409 when N_Discriminant_Specification =>
8410 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
8411 return Null_Exclusion_Present (Discriminant_Type (N));
8412 else
8413 return Null_Exclusion_Present (N);
8414 end if;
8416 when N_Object_Declaration =>
8417 if Nkind (Object_Definition (N)) = N_Access_Definition then
8418 return Null_Exclusion_Present (Object_Definition (N));
8419 else
8420 return Null_Exclusion_Present (N);
8421 end if;
8423 when N_Parameter_Specification =>
8424 if Nkind (Parameter_Type (N)) = N_Access_Definition then
8425 return Null_Exclusion_Present (Parameter_Type (N));
8426 else
8427 return Null_Exclusion_Present (N);
8428 end if;
8430 when others =>
8431 return False;
8433 end case;
8434 end Has_Null_Exclusion;
8436 ------------------------
8437 -- Has_Null_Extension --
8438 ------------------------
8440 function Has_Null_Extension (T : Entity_Id) return Boolean is
8441 B : constant Entity_Id := Base_Type (T);
8442 Comps : Node_Id;
8443 Ext : Node_Id;
8445 begin
8446 if Nkind (Parent (B)) = N_Full_Type_Declaration
8447 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
8448 then
8449 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
8451 if Present (Ext) then
8452 if Null_Present (Ext) then
8453 return True;
8454 else
8455 Comps := Component_List (Ext);
8457 -- The null component list is rewritten during analysis to
8458 -- include the parent component. Any other component indicates
8459 -- that the extension was not originally null.
8461 return Null_Present (Comps)
8462 or else No (Next (First (Component_Items (Comps))));
8463 end if;
8464 else
8465 return False;
8466 end if;
8468 else
8469 return False;
8470 end if;
8471 end Has_Null_Extension;
8473 -------------------------------
8474 -- Has_Overriding_Initialize --
8475 -------------------------------
8477 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
8478 BT : constant Entity_Id := Base_Type (T);
8479 P : Elmt_Id;
8481 begin
8482 if Is_Controlled (BT) then
8483 if Is_RTU (Scope (BT), Ada_Finalization) then
8484 return False;
8486 elsif Present (Primitive_Operations (BT)) then
8487 P := First_Elmt (Primitive_Operations (BT));
8488 while Present (P) loop
8489 declare
8490 Init : constant Entity_Id := Node (P);
8491 Formal : constant Entity_Id := First_Formal (Init);
8492 begin
8493 if Ekind (Init) = E_Procedure
8494 and then Chars (Init) = Name_Initialize
8495 and then Comes_From_Source (Init)
8496 and then Present (Formal)
8497 and then Etype (Formal) = BT
8498 and then No (Next_Formal (Formal))
8499 and then (Ada_Version < Ada_2012
8500 or else not Null_Present (Parent (Init)))
8501 then
8502 return True;
8503 end if;
8504 end;
8506 Next_Elmt (P);
8507 end loop;
8508 end if;
8510 -- Here if type itself does not have a non-null Initialize operation:
8511 -- check immediate ancestor.
8513 if Is_Derived_Type (BT)
8514 and then Has_Overriding_Initialize (Etype (BT))
8515 then
8516 return True;
8517 end if;
8518 end if;
8520 return False;
8521 end Has_Overriding_Initialize;
8523 --------------------------------------
8524 -- Has_Preelaborable_Initialization --
8525 --------------------------------------
8527 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
8528 Has_PE : Boolean;
8530 procedure Check_Components (E : Entity_Id);
8531 -- Check component/discriminant chain, sets Has_PE False if a component
8532 -- or discriminant does not meet the preelaborable initialization rules.
8534 ----------------------
8535 -- Check_Components --
8536 ----------------------
8538 procedure Check_Components (E : Entity_Id) is
8539 Ent : Entity_Id;
8540 Exp : Node_Id;
8542 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
8543 -- Returns True if and only if the expression denoted by N does not
8544 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
8546 ---------------------------------
8547 -- Is_Preelaborable_Expression --
8548 ---------------------------------
8550 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
8551 Exp : Node_Id;
8552 Assn : Node_Id;
8553 Choice : Node_Id;
8554 Comp_Type : Entity_Id;
8555 Is_Array_Aggr : Boolean;
8557 begin
8558 if Is_OK_Static_Expression (N) then
8559 return True;
8561 elsif Nkind (N) = N_Null then
8562 return True;
8564 -- Attributes are allowed in general, even if their prefix is a
8565 -- formal type. (It seems that certain attributes known not to be
8566 -- static might not be allowed, but there are no rules to prevent
8567 -- them.)
8569 elsif Nkind (N) = N_Attribute_Reference then
8570 return True;
8572 -- The name of a discriminant evaluated within its parent type is
8573 -- defined to be preelaborable (10.2.1(8)). Note that we test for
8574 -- names that denote discriminals as well as discriminants to
8575 -- catch references occurring within init procs.
8577 elsif Is_Entity_Name (N)
8578 and then
8579 (Ekind (Entity (N)) = E_Discriminant
8580 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
8581 and then Present (Discriminal_Link (Entity (N)))))
8582 then
8583 return True;
8585 elsif Nkind (N) = N_Qualified_Expression then
8586 return Is_Preelaborable_Expression (Expression (N));
8588 -- For aggregates we have to check that each of the associations
8589 -- is preelaborable.
8591 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8592 Is_Array_Aggr := Is_Array_Type (Etype (N));
8594 if Is_Array_Aggr then
8595 Comp_Type := Component_Type (Etype (N));
8596 end if;
8598 -- Check the ancestor part of extension aggregates, which must
8599 -- be either the name of a type that has preelaborable init or
8600 -- an expression that is preelaborable.
8602 if Nkind (N) = N_Extension_Aggregate then
8603 declare
8604 Anc_Part : constant Node_Id := Ancestor_Part (N);
8606 begin
8607 if Is_Entity_Name (Anc_Part)
8608 and then Is_Type (Entity (Anc_Part))
8609 then
8610 if not Has_Preelaborable_Initialization
8611 (Entity (Anc_Part))
8612 then
8613 return False;
8614 end if;
8616 elsif not Is_Preelaborable_Expression (Anc_Part) then
8617 return False;
8618 end if;
8619 end;
8620 end if;
8622 -- Check positional associations
8624 Exp := First (Expressions (N));
8625 while Present (Exp) loop
8626 if not Is_Preelaborable_Expression (Exp) then
8627 return False;
8628 end if;
8630 Next (Exp);
8631 end loop;
8633 -- Check named associations
8635 Assn := First (Component_Associations (N));
8636 while Present (Assn) loop
8637 Choice := First (Choices (Assn));
8638 while Present (Choice) loop
8639 if Is_Array_Aggr then
8640 if Nkind (Choice) = N_Others_Choice then
8641 null;
8643 elsif Nkind (Choice) = N_Range then
8644 if not Is_OK_Static_Range (Choice) then
8645 return False;
8646 end if;
8648 elsif not Is_OK_Static_Expression (Choice) then
8649 return False;
8650 end if;
8652 else
8653 Comp_Type := Etype (Choice);
8654 end if;
8656 Next (Choice);
8657 end loop;
8659 -- If the association has a <> at this point, then we have
8660 -- to check whether the component's type has preelaborable
8661 -- initialization. Note that this only occurs when the
8662 -- association's corresponding component does not have a
8663 -- default expression, the latter case having already been
8664 -- expanded as an expression for the association.
8666 if Box_Present (Assn) then
8667 if not Has_Preelaborable_Initialization (Comp_Type) then
8668 return False;
8669 end if;
8671 -- In the expression case we check whether the expression
8672 -- is preelaborable.
8674 elsif
8675 not Is_Preelaborable_Expression (Expression (Assn))
8676 then
8677 return False;
8678 end if;
8680 Next (Assn);
8681 end loop;
8683 -- If we get here then aggregate as a whole is preelaborable
8685 return True;
8687 -- All other cases are not preelaborable
8689 else
8690 return False;
8691 end if;
8692 end Is_Preelaborable_Expression;
8694 -- Start of processing for Check_Components
8696 begin
8697 -- Loop through entities of record or protected type
8699 Ent := E;
8700 while Present (Ent) loop
8702 -- We are interested only in components and discriminants
8704 Exp := Empty;
8706 case Ekind (Ent) is
8707 when E_Component =>
8709 -- Get default expression if any. If there is no declaration
8710 -- node, it means we have an internal entity. The parent and
8711 -- tag fields are examples of such entities. For such cases,
8712 -- we just test the type of the entity.
8714 if Present (Declaration_Node (Ent)) then
8715 Exp := Expression (Declaration_Node (Ent));
8716 end if;
8718 when E_Discriminant =>
8720 -- Note: for a renamed discriminant, the Declaration_Node
8721 -- may point to the one from the ancestor, and have a
8722 -- different expression, so use the proper attribute to
8723 -- retrieve the expression from the derived constraint.
8725 Exp := Discriminant_Default_Value (Ent);
8727 when others =>
8728 goto Check_Next_Entity;
8729 end case;
8731 -- A component has PI if it has no default expression and the
8732 -- component type has PI.
8734 if No (Exp) then
8735 if not Has_Preelaborable_Initialization (Etype (Ent)) then
8736 Has_PE := False;
8737 exit;
8738 end if;
8740 -- Require the default expression to be preelaborable
8742 elsif not Is_Preelaborable_Expression (Exp) then
8743 Has_PE := False;
8744 exit;
8745 end if;
8747 <<Check_Next_Entity>>
8748 Next_Entity (Ent);
8749 end loop;
8750 end Check_Components;
8752 -- Start of processing for Has_Preelaborable_Initialization
8754 begin
8755 -- Immediate return if already marked as known preelaborable init. This
8756 -- covers types for which this function has already been called once
8757 -- and returned True (in which case the result is cached), and also
8758 -- types to which a pragma Preelaborable_Initialization applies.
8760 if Known_To_Have_Preelab_Init (E) then
8761 return True;
8762 end if;
8764 -- If the type is a subtype representing a generic actual type, then
8765 -- test whether its base type has preelaborable initialization since
8766 -- the subtype representing the actual does not inherit this attribute
8767 -- from the actual or formal. (but maybe it should???)
8769 if Is_Generic_Actual_Type (E) then
8770 return Has_Preelaborable_Initialization (Base_Type (E));
8771 end if;
8773 -- All elementary types have preelaborable initialization
8775 if Is_Elementary_Type (E) then
8776 Has_PE := True;
8778 -- Array types have PI if the component type has PI
8780 elsif Is_Array_Type (E) then
8781 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
8783 -- A derived type has preelaborable initialization if its parent type
8784 -- has preelaborable initialization and (in the case of a derived record
8785 -- extension) if the non-inherited components all have preelaborable
8786 -- initialization. However, a user-defined controlled type with an
8787 -- overriding Initialize procedure does not have preelaborable
8788 -- initialization.
8790 elsif Is_Derived_Type (E) then
8792 -- If the derived type is a private extension then it doesn't have
8793 -- preelaborable initialization.
8795 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
8796 return False;
8797 end if;
8799 -- First check whether ancestor type has preelaborable initialization
8801 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
8803 -- If OK, check extension components (if any)
8805 if Has_PE and then Is_Record_Type (E) then
8806 Check_Components (First_Entity (E));
8807 end if;
8809 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
8810 -- with a user defined Initialize procedure does not have PI. If
8811 -- the type is untagged, the control primitives come from a component
8812 -- that has already been checked.
8814 if Has_PE
8815 and then Is_Controlled (E)
8816 and then Is_Tagged_Type (E)
8817 and then Has_Overriding_Initialize (E)
8818 then
8819 Has_PE := False;
8820 end if;
8822 -- Private types not derived from a type having preelaborable init and
8823 -- that are not marked with pragma Preelaborable_Initialization do not
8824 -- have preelaborable initialization.
8826 elsif Is_Private_Type (E) then
8827 return False;
8829 -- Record type has PI if it is non private and all components have PI
8831 elsif Is_Record_Type (E) then
8832 Has_PE := True;
8833 Check_Components (First_Entity (E));
8835 -- Protected types must not have entries, and components must meet
8836 -- same set of rules as for record components.
8838 elsif Is_Protected_Type (E) then
8839 if Has_Entries (E) then
8840 Has_PE := False;
8841 else
8842 Has_PE := True;
8843 Check_Components (First_Entity (E));
8844 Check_Components (First_Private_Entity (E));
8845 end if;
8847 -- Type System.Address always has preelaborable initialization
8849 elsif Is_RTE (E, RE_Address) then
8850 Has_PE := True;
8852 -- In all other cases, type does not have preelaborable initialization
8854 else
8855 return False;
8856 end if;
8858 -- If type has preelaborable initialization, cache result
8860 if Has_PE then
8861 Set_Known_To_Have_Preelab_Init (E);
8862 end if;
8864 return Has_PE;
8865 end Has_Preelaborable_Initialization;
8867 ---------------------------
8868 -- Has_Private_Component --
8869 ---------------------------
8871 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
8872 Btype : Entity_Id := Base_Type (Type_Id);
8873 Component : Entity_Id;
8875 begin
8876 if Error_Posted (Type_Id)
8877 or else Error_Posted (Btype)
8878 then
8879 return False;
8880 end if;
8882 if Is_Class_Wide_Type (Btype) then
8883 Btype := Root_Type (Btype);
8884 end if;
8886 if Is_Private_Type (Btype) then
8887 declare
8888 UT : constant Entity_Id := Underlying_Type (Btype);
8889 begin
8890 if No (UT) then
8891 if No (Full_View (Btype)) then
8892 return not Is_Generic_Type (Btype)
8893 and then
8894 not Is_Generic_Type (Root_Type (Btype));
8895 else
8896 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
8897 end if;
8898 else
8899 return not Is_Frozen (UT) and then Has_Private_Component (UT);
8900 end if;
8901 end;
8903 elsif Is_Array_Type (Btype) then
8904 return Has_Private_Component (Component_Type (Btype));
8906 elsif Is_Record_Type (Btype) then
8907 Component := First_Component (Btype);
8908 while Present (Component) loop
8909 if Has_Private_Component (Etype (Component)) then
8910 return True;
8911 end if;
8913 Next_Component (Component);
8914 end loop;
8916 return False;
8918 elsif Is_Protected_Type (Btype)
8919 and then Present (Corresponding_Record_Type (Btype))
8920 then
8921 return Has_Private_Component (Corresponding_Record_Type (Btype));
8923 else
8924 return False;
8925 end if;
8926 end Has_Private_Component;
8928 ----------------------
8929 -- Has_Signed_Zeros --
8930 ----------------------
8932 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
8933 begin
8934 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
8935 end Has_Signed_Zeros;
8937 ------------------------------
8938 -- Has_Significant_Contract --
8939 ------------------------------
8941 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
8942 Subp_Nam : constant Name_Id := Chars (Subp_Id);
8944 begin
8945 -- _Finalizer procedure
8947 if Subp_Nam = Name_uFinalizer then
8948 return False;
8950 -- _Postconditions procedure
8952 elsif Subp_Nam = Name_uPostconditions then
8953 return False;
8955 -- Predicate function
8957 elsif Ekind (Subp_Id) = E_Function
8958 and then Is_Predicate_Function (Subp_Id)
8959 then
8960 return False;
8962 -- TSS subprogram
8964 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
8965 return False;
8967 else
8968 return True;
8969 end if;
8970 end Has_Significant_Contract;
8972 -----------------------------
8973 -- Has_Static_Array_Bounds --
8974 -----------------------------
8976 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
8977 Ndims : constant Nat := Number_Dimensions (Typ);
8979 Index : Node_Id;
8980 Low : Node_Id;
8981 High : Node_Id;
8983 begin
8984 -- Unconstrained types do not have static bounds
8986 if not Is_Constrained (Typ) then
8987 return False;
8988 end if;
8990 -- First treat string literals specially, as the lower bound and length
8991 -- of string literals are not stored like those of arrays.
8993 -- A string literal always has static bounds
8995 if Ekind (Typ) = E_String_Literal_Subtype then
8996 return True;
8997 end if;
8999 -- Treat all dimensions in turn
9001 Index := First_Index (Typ);
9002 for Indx in 1 .. Ndims loop
9004 -- In case of an illegal index which is not a discrete type, return
9005 -- that the type is not static.
9007 if not Is_Discrete_Type (Etype (Index))
9008 or else Etype (Index) = Any_Type
9009 then
9010 return False;
9011 end if;
9013 Get_Index_Bounds (Index, Low, High);
9015 if Error_Posted (Low) or else Error_Posted (High) then
9016 return False;
9017 end if;
9019 if Is_OK_Static_Expression (Low)
9020 and then
9021 Is_OK_Static_Expression (High)
9022 then
9023 null;
9024 else
9025 return False;
9026 end if;
9028 Next (Index);
9029 end loop;
9031 -- If we fall through the loop, all indexes matched
9033 return True;
9034 end Has_Static_Array_Bounds;
9036 ----------------
9037 -- Has_Stream --
9038 ----------------
9040 function Has_Stream (T : Entity_Id) return Boolean is
9041 E : Entity_Id;
9043 begin
9044 if No (T) then
9045 return False;
9047 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
9048 return True;
9050 elsif Is_Array_Type (T) then
9051 return Has_Stream (Component_Type (T));
9053 elsif Is_Record_Type (T) then
9054 E := First_Component (T);
9055 while Present (E) loop
9056 if Has_Stream (Etype (E)) then
9057 return True;
9058 else
9059 Next_Component (E);
9060 end if;
9061 end loop;
9063 return False;
9065 elsif Is_Private_Type (T) then
9066 return Has_Stream (Underlying_Type (T));
9068 else
9069 return False;
9070 end if;
9071 end Has_Stream;
9073 ----------------
9074 -- Has_Suffix --
9075 ----------------
9077 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
9078 begin
9079 Get_Name_String (Chars (E));
9080 return Name_Buffer (Name_Len) = Suffix;
9081 end Has_Suffix;
9083 ----------------
9084 -- Add_Suffix --
9085 ----------------
9087 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
9088 begin
9089 Get_Name_String (Chars (E));
9090 Add_Char_To_Name_Buffer (Suffix);
9091 return Name_Find;
9092 end Add_Suffix;
9094 -------------------
9095 -- Remove_Suffix --
9096 -------------------
9098 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
9099 begin
9100 pragma Assert (Has_Suffix (E, Suffix));
9101 Get_Name_String (Chars (E));
9102 Name_Len := Name_Len - 1;
9103 return Name_Find;
9104 end Remove_Suffix;
9106 --------------------------
9107 -- Has_Tagged_Component --
9108 --------------------------
9110 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
9111 Comp : Entity_Id;
9113 begin
9114 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
9115 return Has_Tagged_Component (Underlying_Type (Typ));
9117 elsif Is_Array_Type (Typ) then
9118 return Has_Tagged_Component (Component_Type (Typ));
9120 elsif Is_Tagged_Type (Typ) then
9121 return True;
9123 elsif Is_Record_Type (Typ) then
9124 Comp := First_Component (Typ);
9125 while Present (Comp) loop
9126 if Has_Tagged_Component (Etype (Comp)) then
9127 return True;
9128 end if;
9130 Next_Component (Comp);
9131 end loop;
9133 return False;
9135 else
9136 return False;
9137 end if;
9138 end Has_Tagged_Component;
9140 ----------------------------
9141 -- Has_Volatile_Component --
9142 ----------------------------
9144 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
9145 Comp : Entity_Id;
9147 begin
9148 if Has_Volatile_Components (Typ) then
9149 return True;
9151 elsif Is_Array_Type (Typ) then
9152 return Is_Volatile (Component_Type (Typ));
9154 elsif Is_Record_Type (Typ) then
9155 Comp := First_Component (Typ);
9156 while Present (Comp) loop
9157 if Is_Volatile_Object (Comp) then
9158 return True;
9159 end if;
9161 Comp := Next_Component (Comp);
9162 end loop;
9163 end if;
9165 return False;
9166 end Has_Volatile_Component;
9168 -------------------------
9169 -- Implementation_Kind --
9170 -------------------------
9172 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
9173 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
9174 Arg : Node_Id;
9175 begin
9176 pragma Assert (Present (Impl_Prag));
9177 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
9178 return Chars (Get_Pragma_Arg (Arg));
9179 end Implementation_Kind;
9181 --------------------------
9182 -- Implements_Interface --
9183 --------------------------
9185 function Implements_Interface
9186 (Typ_Ent : Entity_Id;
9187 Iface_Ent : Entity_Id;
9188 Exclude_Parents : Boolean := False) return Boolean
9190 Ifaces_List : Elist_Id;
9191 Elmt : Elmt_Id;
9192 Iface : Entity_Id := Base_Type (Iface_Ent);
9193 Typ : Entity_Id := Base_Type (Typ_Ent);
9195 begin
9196 if Is_Class_Wide_Type (Typ) then
9197 Typ := Root_Type (Typ);
9198 end if;
9200 if not Has_Interfaces (Typ) then
9201 return False;
9202 end if;
9204 if Is_Class_Wide_Type (Iface) then
9205 Iface := Root_Type (Iface);
9206 end if;
9208 Collect_Interfaces (Typ, Ifaces_List);
9210 Elmt := First_Elmt (Ifaces_List);
9211 while Present (Elmt) loop
9212 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
9213 and then Exclude_Parents
9214 then
9215 null;
9217 elsif Node (Elmt) = Iface then
9218 return True;
9219 end if;
9221 Next_Elmt (Elmt);
9222 end loop;
9224 return False;
9225 end Implements_Interface;
9227 ------------------------------------
9228 -- In_Assertion_Expression_Pragma --
9229 ------------------------------------
9231 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
9232 Par : Node_Id;
9233 Prag : Node_Id := Empty;
9235 begin
9236 -- Climb the parent chain looking for an enclosing pragma
9238 Par := N;
9239 while Present (Par) loop
9240 if Nkind (Par) = N_Pragma then
9241 Prag := Par;
9242 exit;
9244 -- Precondition-like pragmas are expanded into if statements, check
9245 -- the original node instead.
9247 elsif Nkind (Original_Node (Par)) = N_Pragma then
9248 Prag := Original_Node (Par);
9249 exit;
9251 -- The expansion of attribute 'Old generates a constant to capture
9252 -- the result of the prefix. If the parent traversal reaches
9253 -- one of these constants, then the node technically came from a
9254 -- postcondition-like pragma. Note that the Ekind is not tested here
9255 -- because N may be the expression of an object declaration which is
9256 -- currently being analyzed. Such objects carry Ekind of E_Void.
9258 elsif Nkind (Par) = N_Object_Declaration
9259 and then Constant_Present (Par)
9260 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
9261 then
9262 return True;
9264 -- Prevent the search from going too far
9266 elsif Is_Body_Or_Package_Declaration (Par) then
9267 return False;
9268 end if;
9270 Par := Parent (Par);
9271 end loop;
9273 return
9274 Present (Prag)
9275 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
9276 end In_Assertion_Expression_Pragma;
9278 -----------------
9279 -- In_Instance --
9280 -----------------
9282 function In_Instance return Boolean is
9283 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9284 S : Entity_Id;
9286 begin
9287 S := Current_Scope;
9288 while Present (S) and then S /= Standard_Standard loop
9289 if Ekind_In (S, E_Function, E_Package, E_Procedure)
9290 and then Is_Generic_Instance (S)
9291 then
9292 -- A child instance is always compiled in the context of a parent
9293 -- instance. Nevertheless, the actuals are not analyzed in an
9294 -- instance context. We detect this case by examining the current
9295 -- compilation unit, which must be a child instance, and checking
9296 -- that it is not currently on the scope stack.
9298 if Is_Child_Unit (Curr_Unit)
9299 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
9300 N_Package_Instantiation
9301 and then not In_Open_Scopes (Curr_Unit)
9302 then
9303 return False;
9304 else
9305 return True;
9306 end if;
9307 end if;
9309 S := Scope (S);
9310 end loop;
9312 return False;
9313 end In_Instance;
9315 ----------------------
9316 -- In_Instance_Body --
9317 ----------------------
9319 function In_Instance_Body return Boolean is
9320 S : Entity_Id;
9322 begin
9323 S := Current_Scope;
9324 while Present (S) and then S /= Standard_Standard loop
9325 if Ekind_In (S, E_Function, E_Procedure)
9326 and then Is_Generic_Instance (S)
9327 then
9328 return True;
9330 elsif Ekind (S) = E_Package
9331 and then In_Package_Body (S)
9332 and then Is_Generic_Instance (S)
9333 then
9334 return True;
9335 end if;
9337 S := Scope (S);
9338 end loop;
9340 return False;
9341 end In_Instance_Body;
9343 -----------------------------
9344 -- In_Instance_Not_Visible --
9345 -----------------------------
9347 function In_Instance_Not_Visible return Boolean is
9348 S : Entity_Id;
9350 begin
9351 S := Current_Scope;
9352 while Present (S) and then S /= Standard_Standard loop
9353 if Ekind_In (S, E_Function, E_Procedure)
9354 and then Is_Generic_Instance (S)
9355 then
9356 return True;
9358 elsif Ekind (S) = E_Package
9359 and then (In_Package_Body (S) or else In_Private_Part (S))
9360 and then Is_Generic_Instance (S)
9361 then
9362 return True;
9363 end if;
9365 S := Scope (S);
9366 end loop;
9368 return False;
9369 end In_Instance_Not_Visible;
9371 ------------------------------
9372 -- In_Instance_Visible_Part --
9373 ------------------------------
9375 function In_Instance_Visible_Part return Boolean is
9376 S : Entity_Id;
9378 begin
9379 S := Current_Scope;
9380 while Present (S) and then S /= Standard_Standard loop
9381 if Ekind (S) = E_Package
9382 and then Is_Generic_Instance (S)
9383 and then not In_Package_Body (S)
9384 and then not In_Private_Part (S)
9385 then
9386 return True;
9387 end if;
9389 S := Scope (S);
9390 end loop;
9392 return False;
9393 end In_Instance_Visible_Part;
9395 ---------------------
9396 -- In_Package_Body --
9397 ---------------------
9399 function In_Package_Body return Boolean is
9400 S : Entity_Id;
9402 begin
9403 S := Current_Scope;
9404 while Present (S) and then S /= Standard_Standard loop
9405 if Ekind (S) = E_Package and then In_Package_Body (S) then
9406 return True;
9407 else
9408 S := Scope (S);
9409 end if;
9410 end loop;
9412 return False;
9413 end In_Package_Body;
9415 --------------------------------
9416 -- In_Parameter_Specification --
9417 --------------------------------
9419 function In_Parameter_Specification (N : Node_Id) return Boolean is
9420 PN : Node_Id;
9422 begin
9423 PN := Parent (N);
9424 while Present (PN) loop
9425 if Nkind (PN) = N_Parameter_Specification then
9426 return True;
9427 end if;
9429 PN := Parent (PN);
9430 end loop;
9432 return False;
9433 end In_Parameter_Specification;
9435 --------------------------
9436 -- In_Pragma_Expression --
9437 --------------------------
9439 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
9440 P : Node_Id;
9441 begin
9442 P := Parent (N);
9443 loop
9444 if No (P) then
9445 return False;
9446 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
9447 return True;
9448 else
9449 P := Parent (P);
9450 end if;
9451 end loop;
9452 end In_Pragma_Expression;
9454 -------------------------------------
9455 -- In_Reverse_Storage_Order_Object --
9456 -------------------------------------
9458 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
9459 Pref : Node_Id;
9460 Btyp : Entity_Id := Empty;
9462 begin
9463 -- Climb up indexed components
9465 Pref := N;
9466 loop
9467 case Nkind (Pref) is
9468 when N_Selected_Component =>
9469 Pref := Prefix (Pref);
9470 exit;
9472 when N_Indexed_Component =>
9473 Pref := Prefix (Pref);
9475 when others =>
9476 Pref := Empty;
9477 exit;
9478 end case;
9479 end loop;
9481 if Present (Pref) then
9482 Btyp := Base_Type (Etype (Pref));
9483 end if;
9485 return Present (Btyp)
9486 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
9487 and then Reverse_Storage_Order (Btyp);
9488 end In_Reverse_Storage_Order_Object;
9490 --------------------------------------
9491 -- In_Subprogram_Or_Concurrent_Unit --
9492 --------------------------------------
9494 function In_Subprogram_Or_Concurrent_Unit return Boolean is
9495 E : Entity_Id;
9496 K : Entity_Kind;
9498 begin
9499 -- Use scope chain to check successively outer scopes
9501 E := Current_Scope;
9502 loop
9503 K := Ekind (E);
9505 if K in Subprogram_Kind
9506 or else K in Concurrent_Kind
9507 or else K in Generic_Subprogram_Kind
9508 then
9509 return True;
9511 elsif E = Standard_Standard then
9512 return False;
9513 end if;
9515 E := Scope (E);
9516 end loop;
9517 end In_Subprogram_Or_Concurrent_Unit;
9519 ---------------------
9520 -- In_Visible_Part --
9521 ---------------------
9523 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
9524 begin
9525 return Is_Package_Or_Generic_Package (Scope_Id)
9526 and then In_Open_Scopes (Scope_Id)
9527 and then not In_Package_Body (Scope_Id)
9528 and then not In_Private_Part (Scope_Id);
9529 end In_Visible_Part;
9531 --------------------------------
9532 -- Incomplete_Or_Partial_View --
9533 --------------------------------
9535 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
9536 function Inspect_Decls
9537 (Decls : List_Id;
9538 Taft : Boolean := False) return Entity_Id;
9539 -- Check whether a declarative region contains the incomplete or partial
9540 -- view of Id.
9542 -------------------
9543 -- Inspect_Decls --
9544 -------------------
9546 function Inspect_Decls
9547 (Decls : List_Id;
9548 Taft : Boolean := False) return Entity_Id
9550 Decl : Node_Id;
9551 Match : Node_Id;
9553 begin
9554 Decl := First (Decls);
9555 while Present (Decl) loop
9556 Match := Empty;
9558 if Taft then
9559 if Nkind (Decl) = N_Incomplete_Type_Declaration then
9560 Match := Defining_Identifier (Decl);
9561 end if;
9563 else
9564 if Nkind_In (Decl, N_Private_Extension_Declaration,
9565 N_Private_Type_Declaration)
9566 then
9567 Match := Defining_Identifier (Decl);
9568 end if;
9569 end if;
9571 if Present (Match)
9572 and then Present (Full_View (Match))
9573 and then Full_View (Match) = Id
9574 then
9575 return Match;
9576 end if;
9578 Next (Decl);
9579 end loop;
9581 return Empty;
9582 end Inspect_Decls;
9584 -- Local variables
9586 Prev : Entity_Id;
9588 -- Start of processing for Incomplete_Or_Partial_View
9590 begin
9591 -- Deferred constant or incomplete type case
9593 Prev := Current_Entity_In_Scope (Id);
9595 if Present (Prev)
9596 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
9597 and then Present (Full_View (Prev))
9598 and then Full_View (Prev) = Id
9599 then
9600 return Prev;
9601 end if;
9603 -- Private or Taft amendment type case
9605 declare
9606 Pkg : constant Entity_Id := Scope (Id);
9607 Pkg_Decl : Node_Id := Pkg;
9609 begin
9610 if Present (Pkg) and then Ekind (Pkg) = E_Package then
9611 while Nkind (Pkg_Decl) /= N_Package_Specification loop
9612 Pkg_Decl := Parent (Pkg_Decl);
9613 end loop;
9615 -- It is knows that Typ has a private view, look for it in the
9616 -- visible declarations of the enclosing scope. A special case
9617 -- of this is when the two views have been exchanged - the full
9618 -- appears earlier than the private.
9620 if Has_Private_Declaration (Id) then
9621 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
9623 -- Exchanged view case, look in the private declarations
9625 if No (Prev) then
9626 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
9627 end if;
9629 return Prev;
9631 -- Otherwise if this is the package body, then Typ is a potential
9632 -- Taft amendment type. The incomplete view should be located in
9633 -- the private declarations of the enclosing scope.
9635 elsif In_Package_Body (Pkg) then
9636 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
9637 end if;
9638 end if;
9639 end;
9641 -- The type has no incomplete or private view
9643 return Empty;
9644 end Incomplete_Or_Partial_View;
9646 -----------------------------------------
9647 -- Inherit_Default_Init_Cond_Procedure --
9648 -----------------------------------------
9650 procedure Inherit_Default_Init_Cond_Procedure (Typ : Entity_Id) is
9651 Par_Typ : constant Entity_Id := Etype (Typ);
9653 begin
9654 -- A derived type inherits the default initial condition procedure of
9655 -- its parent type.
9657 if No (Default_Init_Cond_Procedure (Typ)) then
9658 Set_Default_Init_Cond_Procedure
9659 (Typ, Default_Init_Cond_Procedure (Par_Typ));
9660 end if;
9661 end Inherit_Default_Init_Cond_Procedure;
9663 ----------------------------
9664 -- Inherit_Rep_Item_Chain --
9665 ----------------------------
9667 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
9668 From_Item : constant Node_Id := First_Rep_Item (From_Typ);
9669 Item : Node_Id := Empty;
9670 Last_Item : Node_Id := Empty;
9672 begin
9673 -- Reach the end of the destination type's chain (if any) and capture
9674 -- the last item.
9676 Item := First_Rep_Item (Typ);
9677 while Present (Item) loop
9679 -- Do not inherit a chain that has been inherited already
9681 if Item = From_Item then
9682 return;
9683 end if;
9685 Last_Item := Item;
9686 Item := Next_Rep_Item (Item);
9687 end loop;
9689 -- When the destination type has a rep item chain, the chain of the
9690 -- source type is appended to it.
9692 if Present (Last_Item) then
9693 Set_Next_Rep_Item (Last_Item, From_Item);
9695 -- Otherwise the destination type directly inherits the rep item chain
9696 -- of the source type (if any).
9698 else
9699 Set_First_Rep_Item (Typ, From_Item);
9700 end if;
9701 end Inherit_Rep_Item_Chain;
9703 ---------------------------------
9704 -- Inherit_Subprogram_Contract --
9705 ---------------------------------
9707 procedure Inherit_Subprogram_Contract
9708 (Subp : Entity_Id;
9709 From_Subp : Entity_Id)
9711 procedure Inherit_Pragma (Prag_Id : Pragma_Id);
9712 -- Propagate a pragma denoted by Prag_Id from From_Subp's contract to
9713 -- Subp's contract.
9715 --------------------
9716 -- Inherit_Pragma --
9717 --------------------
9719 procedure Inherit_Pragma (Prag_Id : Pragma_Id) is
9720 Prag : constant Node_Id := Get_Pragma (From_Subp, Prag_Id);
9721 New_Prag : Node_Id;
9723 begin
9724 -- A pragma cannot be part of more than one First_Pragma/Next_Pragma
9725 -- chains, therefore the node must be replicated. The new pragma is
9726 -- flagged is inherited for distrinction purposes.
9728 if Present (Prag) then
9729 New_Prag := New_Copy_Tree (Prag);
9730 Set_Is_Inherited (New_Prag);
9732 Add_Contract_Item (New_Prag, Subp);
9733 end if;
9734 end Inherit_Pragma;
9736 -- Start of processing for Inherit_Subprogram_Contract
9738 begin
9739 -- Inheritance is carried out only when both entities are subprograms
9740 -- with contracts.
9742 if Is_Subprogram_Or_Generic_Subprogram (Subp)
9743 and then Is_Subprogram_Or_Generic_Subprogram (From_Subp)
9744 and then Present (Contract (From_Subp))
9745 then
9746 Inherit_Pragma (Pragma_Extensions_Visible);
9747 end if;
9748 end Inherit_Subprogram_Contract;
9750 ---------------------------------
9751 -- Insert_Explicit_Dereference --
9752 ---------------------------------
9754 procedure Insert_Explicit_Dereference (N : Node_Id) is
9755 New_Prefix : constant Node_Id := Relocate_Node (N);
9756 Ent : Entity_Id := Empty;
9757 Pref : Node_Id;
9758 I : Interp_Index;
9759 It : Interp;
9760 T : Entity_Id;
9762 begin
9763 Save_Interps (N, New_Prefix);
9765 Rewrite (N,
9766 Make_Explicit_Dereference (Sloc (Parent (N)),
9767 Prefix => New_Prefix));
9769 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
9771 if Is_Overloaded (New_Prefix) then
9773 -- The dereference is also overloaded, and its interpretations are
9774 -- the designated types of the interpretations of the original node.
9776 Set_Etype (N, Any_Type);
9778 Get_First_Interp (New_Prefix, I, It);
9779 while Present (It.Nam) loop
9780 T := It.Typ;
9782 if Is_Access_Type (T) then
9783 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
9784 end if;
9786 Get_Next_Interp (I, It);
9787 end loop;
9789 End_Interp_List;
9791 else
9792 -- Prefix is unambiguous: mark the original prefix (which might
9793 -- Come_From_Source) as a reference, since the new (relocated) one
9794 -- won't be taken into account.
9796 if Is_Entity_Name (New_Prefix) then
9797 Ent := Entity (New_Prefix);
9798 Pref := New_Prefix;
9800 -- For a retrieval of a subcomponent of some composite object,
9801 -- retrieve the ultimate entity if there is one.
9803 elsif Nkind_In (New_Prefix, N_Selected_Component,
9804 N_Indexed_Component)
9805 then
9806 Pref := Prefix (New_Prefix);
9807 while Present (Pref)
9808 and then Nkind_In (Pref, N_Selected_Component,
9809 N_Indexed_Component)
9810 loop
9811 Pref := Prefix (Pref);
9812 end loop;
9814 if Present (Pref) and then Is_Entity_Name (Pref) then
9815 Ent := Entity (Pref);
9816 end if;
9817 end if;
9819 -- Place the reference on the entity node
9821 if Present (Ent) then
9822 Generate_Reference (Ent, Pref);
9823 end if;
9824 end if;
9825 end Insert_Explicit_Dereference;
9827 ------------------------------------------
9828 -- Inspect_Deferred_Constant_Completion --
9829 ------------------------------------------
9831 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
9832 Decl : Node_Id;
9834 begin
9835 Decl := First (Decls);
9836 while Present (Decl) loop
9838 -- Deferred constant signature
9840 if Nkind (Decl) = N_Object_Declaration
9841 and then Constant_Present (Decl)
9842 and then No (Expression (Decl))
9844 -- No need to check internally generated constants
9846 and then Comes_From_Source (Decl)
9848 -- The constant is not completed. A full object declaration or a
9849 -- pragma Import complete a deferred constant.
9851 and then not Has_Completion (Defining_Identifier (Decl))
9852 then
9853 Error_Msg_N
9854 ("constant declaration requires initialization expression",
9855 Defining_Identifier (Decl));
9856 end if;
9858 Decl := Next (Decl);
9859 end loop;
9860 end Inspect_Deferred_Constant_Completion;
9862 -----------------------------
9863 -- Install_Generic_Formals --
9864 -----------------------------
9866 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
9867 E : Entity_Id;
9869 begin
9870 pragma Assert (Is_Generic_Subprogram (Subp_Id));
9872 E := First_Entity (Subp_Id);
9873 while Present (E) loop
9874 Install_Entity (E);
9875 Next_Entity (E);
9876 end loop;
9877 end Install_Generic_Formals;
9879 -----------------------------
9880 -- Is_Actual_Out_Parameter --
9881 -----------------------------
9883 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
9884 Formal : Entity_Id;
9885 Call : Node_Id;
9886 begin
9887 Find_Actual (N, Formal, Call);
9888 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
9889 end Is_Actual_Out_Parameter;
9891 -------------------------
9892 -- Is_Actual_Parameter --
9893 -------------------------
9895 function Is_Actual_Parameter (N : Node_Id) return Boolean is
9896 PK : constant Node_Kind := Nkind (Parent (N));
9898 begin
9899 case PK is
9900 when N_Parameter_Association =>
9901 return N = Explicit_Actual_Parameter (Parent (N));
9903 when N_Subprogram_Call =>
9904 return Is_List_Member (N)
9905 and then
9906 List_Containing (N) = Parameter_Associations (Parent (N));
9908 when others =>
9909 return False;
9910 end case;
9911 end Is_Actual_Parameter;
9913 --------------------------------
9914 -- Is_Actual_Tagged_Parameter --
9915 --------------------------------
9917 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
9918 Formal : Entity_Id;
9919 Call : Node_Id;
9920 begin
9921 Find_Actual (N, Formal, Call);
9922 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
9923 end Is_Actual_Tagged_Parameter;
9925 ---------------------
9926 -- Is_Aliased_View --
9927 ---------------------
9929 function Is_Aliased_View (Obj : Node_Id) return Boolean is
9930 E : Entity_Id;
9932 begin
9933 if Is_Entity_Name (Obj) then
9934 E := Entity (Obj);
9936 return
9937 (Is_Object (E)
9938 and then
9939 (Is_Aliased (E)
9940 or else (Present (Renamed_Object (E))
9941 and then Is_Aliased_View (Renamed_Object (E)))))
9943 or else ((Is_Formal (E)
9944 or else Ekind_In (E, E_Generic_In_Out_Parameter,
9945 E_Generic_In_Parameter))
9946 and then Is_Tagged_Type (Etype (E)))
9948 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
9950 -- Current instance of type, either directly or as rewritten
9951 -- reference to the current object.
9953 or else (Is_Entity_Name (Original_Node (Obj))
9954 and then Present (Entity (Original_Node (Obj)))
9955 and then Is_Type (Entity (Original_Node (Obj))))
9957 or else (Is_Type (E) and then E = Current_Scope)
9959 or else (Is_Incomplete_Or_Private_Type (E)
9960 and then Full_View (E) = Current_Scope)
9962 -- Ada 2012 AI05-0053: the return object of an extended return
9963 -- statement is aliased if its type is immutably limited.
9965 or else (Is_Return_Object (E)
9966 and then Is_Limited_View (Etype (E)));
9968 elsif Nkind (Obj) = N_Selected_Component then
9969 return Is_Aliased (Entity (Selector_Name (Obj)));
9971 elsif Nkind (Obj) = N_Indexed_Component then
9972 return Has_Aliased_Components (Etype (Prefix (Obj)))
9973 or else
9974 (Is_Access_Type (Etype (Prefix (Obj)))
9975 and then Has_Aliased_Components
9976 (Designated_Type (Etype (Prefix (Obj)))));
9978 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
9979 return Is_Tagged_Type (Etype (Obj))
9980 and then Is_Aliased_View (Expression (Obj));
9982 elsif Nkind (Obj) = N_Explicit_Dereference then
9983 return Nkind (Original_Node (Obj)) /= N_Function_Call;
9985 else
9986 return False;
9987 end if;
9988 end Is_Aliased_View;
9990 -------------------------
9991 -- Is_Ancestor_Package --
9992 -------------------------
9994 function Is_Ancestor_Package
9995 (E1 : Entity_Id;
9996 E2 : Entity_Id) return Boolean
9998 Par : Entity_Id;
10000 begin
10001 Par := E2;
10002 while Present (Par) and then Par /= Standard_Standard loop
10003 if Par = E1 then
10004 return True;
10005 end if;
10007 Par := Scope (Par);
10008 end loop;
10010 return False;
10011 end Is_Ancestor_Package;
10013 ----------------------
10014 -- Is_Atomic_Object --
10015 ----------------------
10017 function Is_Atomic_Object (N : Node_Id) return Boolean is
10019 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
10020 -- Determines if given object has atomic components
10022 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
10023 -- If prefix is an implicit dereference, examine designated type
10025 ----------------------
10026 -- Is_Atomic_Prefix --
10027 ----------------------
10029 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
10030 begin
10031 if Is_Access_Type (Etype (N)) then
10032 return
10033 Has_Atomic_Components (Designated_Type (Etype (N)));
10034 else
10035 return Object_Has_Atomic_Components (N);
10036 end if;
10037 end Is_Atomic_Prefix;
10039 ----------------------------------
10040 -- Object_Has_Atomic_Components --
10041 ----------------------------------
10043 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
10044 begin
10045 if Has_Atomic_Components (Etype (N))
10046 or else Is_Atomic (Etype (N))
10047 then
10048 return True;
10050 elsif Is_Entity_Name (N)
10051 and then (Has_Atomic_Components (Entity (N))
10052 or else Is_Atomic (Entity (N)))
10053 then
10054 return True;
10056 elsif Nkind (N) = N_Selected_Component
10057 and then Is_Atomic (Entity (Selector_Name (N)))
10058 then
10059 return True;
10061 elsif Nkind (N) = N_Indexed_Component
10062 or else Nkind (N) = N_Selected_Component
10063 then
10064 return Is_Atomic_Prefix (Prefix (N));
10066 else
10067 return False;
10068 end if;
10069 end Object_Has_Atomic_Components;
10071 -- Start of processing for Is_Atomic_Object
10073 begin
10074 -- Predicate is not relevant to subprograms
10076 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
10077 return False;
10079 elsif Is_Atomic (Etype (N))
10080 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
10081 then
10082 return True;
10084 elsif Nkind (N) = N_Selected_Component
10085 and then Is_Atomic (Entity (Selector_Name (N)))
10086 then
10087 return True;
10089 elsif Nkind (N) = N_Indexed_Component
10090 or else Nkind (N) = N_Selected_Component
10091 then
10092 return Is_Atomic_Prefix (Prefix (N));
10094 else
10095 return False;
10096 end if;
10097 end Is_Atomic_Object;
10099 -------------------------
10100 -- Is_Attribute_Result --
10101 -------------------------
10103 function Is_Attribute_Result (N : Node_Id) return Boolean is
10104 begin
10105 return Nkind (N) = N_Attribute_Reference
10106 and then Attribute_Name (N) = Name_Result;
10107 end Is_Attribute_Result;
10109 ------------------------------------
10110 -- Is_Body_Or_Package_Declaration --
10111 ------------------------------------
10113 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
10114 begin
10115 return Nkind_In (N, N_Entry_Body,
10116 N_Package_Body,
10117 N_Package_Declaration,
10118 N_Protected_Body,
10119 N_Subprogram_Body,
10120 N_Task_Body);
10121 end Is_Body_Or_Package_Declaration;
10123 -----------------------
10124 -- Is_Bounded_String --
10125 -----------------------
10127 function Is_Bounded_String (T : Entity_Id) return Boolean is
10128 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
10130 begin
10131 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
10132 -- Super_String, or one of the [Wide_]Wide_ versions. This will
10133 -- be True for all the Bounded_String types in instances of the
10134 -- Generic_Bounded_Length generics, and for types derived from those.
10136 return Present (Under)
10137 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
10138 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
10139 Is_RTE (Root_Type (Under), RO_WW_Super_String));
10140 end Is_Bounded_String;
10142 -------------------------
10143 -- Is_Child_Or_Sibling --
10144 -------------------------
10146 function Is_Child_Or_Sibling
10147 (Pack_1 : Entity_Id;
10148 Pack_2 : Entity_Id) return Boolean
10150 function Distance_From_Standard (Pack : Entity_Id) return Nat;
10151 -- Given an arbitrary package, return the number of "climbs" necessary
10152 -- to reach scope Standard_Standard.
10154 procedure Equalize_Depths
10155 (Pack : in out Entity_Id;
10156 Depth : in out Nat;
10157 Depth_To_Reach : Nat);
10158 -- Given an arbitrary package, its depth and a target depth to reach,
10159 -- climb the scope chain until the said depth is reached. The pointer
10160 -- to the package and its depth a modified during the climb.
10162 ----------------------------
10163 -- Distance_From_Standard --
10164 ----------------------------
10166 function Distance_From_Standard (Pack : Entity_Id) return Nat is
10167 Dist : Nat;
10168 Scop : Entity_Id;
10170 begin
10171 Dist := 0;
10172 Scop := Pack;
10173 while Present (Scop) and then Scop /= Standard_Standard loop
10174 Dist := Dist + 1;
10175 Scop := Scope (Scop);
10176 end loop;
10178 return Dist;
10179 end Distance_From_Standard;
10181 ---------------------
10182 -- Equalize_Depths --
10183 ---------------------
10185 procedure Equalize_Depths
10186 (Pack : in out Entity_Id;
10187 Depth : in out Nat;
10188 Depth_To_Reach : Nat)
10190 begin
10191 -- The package must be at a greater or equal depth
10193 if Depth < Depth_To_Reach then
10194 raise Program_Error;
10195 end if;
10197 -- Climb the scope chain until the desired depth is reached
10199 while Present (Pack) and then Depth /= Depth_To_Reach loop
10200 Pack := Scope (Pack);
10201 Depth := Depth - 1;
10202 end loop;
10203 end Equalize_Depths;
10205 -- Local variables
10207 P_1 : Entity_Id := Pack_1;
10208 P_1_Child : Boolean := False;
10209 P_1_Depth : Nat := Distance_From_Standard (P_1);
10210 P_2 : Entity_Id := Pack_2;
10211 P_2_Child : Boolean := False;
10212 P_2_Depth : Nat := Distance_From_Standard (P_2);
10214 -- Start of processing for Is_Child_Or_Sibling
10216 begin
10217 pragma Assert
10218 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
10220 -- Both packages denote the same entity, therefore they cannot be
10221 -- children or siblings.
10223 if P_1 = P_2 then
10224 return False;
10226 -- One of the packages is at a deeper level than the other. Note that
10227 -- both may still come from differen hierarchies.
10229 -- (root) P_2
10230 -- / \ :
10231 -- X P_2 or X
10232 -- : :
10233 -- P_1 P_1
10235 elsif P_1_Depth > P_2_Depth then
10236 Equalize_Depths
10237 (Pack => P_1,
10238 Depth => P_1_Depth,
10239 Depth_To_Reach => P_2_Depth);
10240 P_1_Child := True;
10242 -- (root) P_1
10243 -- / \ :
10244 -- P_1 X or X
10245 -- : :
10246 -- P_2 P_2
10248 elsif P_2_Depth > P_1_Depth then
10249 Equalize_Depths
10250 (Pack => P_2,
10251 Depth => P_2_Depth,
10252 Depth_To_Reach => P_1_Depth);
10253 P_2_Child := True;
10254 end if;
10256 -- At this stage the package pointers have been elevated to the same
10257 -- depth. If the related entities are the same, then one package is a
10258 -- potential child of the other:
10260 -- P_1
10261 -- :
10262 -- X became P_1 P_2 or vica versa
10263 -- :
10264 -- P_2
10266 if P_1 = P_2 then
10267 if P_1_Child then
10268 return Is_Child_Unit (Pack_1);
10270 else pragma Assert (P_2_Child);
10271 return Is_Child_Unit (Pack_2);
10272 end if;
10274 -- The packages may come from the same package chain or from entirely
10275 -- different hierarcies. To determine this, climb the scope stack until
10276 -- a common root is found.
10278 -- (root) (root 1) (root 2)
10279 -- / \ | |
10280 -- P_1 P_2 P_1 P_2
10282 else
10283 while Present (P_1) and then Present (P_2) loop
10285 -- The two packages may be siblings
10287 if P_1 = P_2 then
10288 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
10289 end if;
10291 P_1 := Scope (P_1);
10292 P_2 := Scope (P_2);
10293 end loop;
10294 end if;
10296 return False;
10297 end Is_Child_Or_Sibling;
10299 -----------------------------
10300 -- Is_Concurrent_Interface --
10301 -----------------------------
10303 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
10304 begin
10305 return Is_Interface (T)
10306 and then
10307 (Is_Protected_Interface (T)
10308 or else Is_Synchronized_Interface (T)
10309 or else Is_Task_Interface (T));
10310 end Is_Concurrent_Interface;
10312 ---------------------------
10313 -- Is_Container_Element --
10314 ---------------------------
10316 function Is_Container_Element (Exp : Node_Id) return Boolean is
10317 Loc : constant Source_Ptr := Sloc (Exp);
10318 Pref : constant Node_Id := Prefix (Exp);
10320 Call : Node_Id;
10321 -- Call to an indexing aspect
10323 Cont_Typ : Entity_Id;
10324 -- The type of the container being accessed
10326 Elem_Typ : Entity_Id;
10327 -- Its element type
10329 Indexing : Entity_Id;
10330 Is_Const : Boolean;
10331 -- Indicates that constant indexing is used, and the element is thus
10332 -- a constant.
10334 Ref_Typ : Entity_Id;
10335 -- The reference type returned by the indexing operation
10337 begin
10338 -- If C is a container, in a context that imposes the element type of
10339 -- that container, the indexing notation C (X) is rewritten as:
10341 -- Indexing (C, X).Discr.all
10343 -- where Indexing is one of the indexing aspects of the container.
10344 -- If the context does not require a reference, the construct can be
10345 -- rewritten as
10347 -- Element (C, X)
10349 -- First, verify that the construct has the proper form
10351 if not Expander_Active then
10352 return False;
10354 elsif Nkind (Pref) /= N_Selected_Component then
10355 return False;
10357 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
10358 return False;
10360 else
10361 Call := Prefix (Pref);
10362 Ref_Typ := Etype (Call);
10363 end if;
10365 if not Has_Implicit_Dereference (Ref_Typ)
10366 or else No (First (Parameter_Associations (Call)))
10367 or else not Is_Entity_Name (Name (Call))
10368 then
10369 return False;
10370 end if;
10372 -- Retrieve type of container object, and its iterator aspects
10374 Cont_Typ := Etype (First (Parameter_Associations (Call)));
10375 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
10376 Is_Const := False;
10378 if No (Indexing) then
10380 -- Container should have at least one indexing operation
10382 return False;
10384 elsif Entity (Name (Call)) /= Entity (Indexing) then
10386 -- This may be a variable indexing operation
10388 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
10390 if No (Indexing)
10391 or else Entity (Name (Call)) /= Entity (Indexing)
10392 then
10393 return False;
10394 end if;
10396 else
10397 Is_Const := True;
10398 end if;
10400 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
10402 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
10403 return False;
10404 end if;
10406 -- Check that the expression is not the target of an assignment, in
10407 -- which case the rewriting is not possible.
10409 if not Is_Const then
10410 declare
10411 Par : Node_Id;
10413 begin
10414 Par := Exp;
10415 while Present (Par)
10416 loop
10417 if Nkind (Parent (Par)) = N_Assignment_Statement
10418 and then Par = Name (Parent (Par))
10419 then
10420 return False;
10422 -- A renaming produces a reference, and the transformation
10423 -- does not apply.
10425 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
10426 return False;
10428 elsif Nkind_In
10429 (Nkind (Parent (Par)), N_Function_Call,
10430 N_Procedure_Call_Statement,
10431 N_Entry_Call_Statement)
10432 then
10433 -- Check that the element is not part of an actual for an
10434 -- in-out parameter.
10436 declare
10437 F : Entity_Id;
10438 A : Node_Id;
10440 begin
10441 F := First_Formal (Entity (Name (Parent (Par))));
10442 A := First (Parameter_Associations (Parent (Par)));
10443 while Present (F) loop
10444 if A = Par and then Ekind (F) /= E_In_Parameter then
10445 return False;
10446 end if;
10448 Next_Formal (F);
10449 Next (A);
10450 end loop;
10451 end;
10453 -- E_In_Parameter in a call: element is not modified.
10455 exit;
10456 end if;
10458 Par := Parent (Par);
10459 end loop;
10460 end;
10461 end if;
10463 -- The expression has the proper form and the context requires the
10464 -- element type. Retrieve the Element function of the container and
10465 -- rewrite the construct as a call to it.
10467 declare
10468 Op : Elmt_Id;
10470 begin
10471 Op := First_Elmt (Primitive_Operations (Cont_Typ));
10472 while Present (Op) loop
10473 exit when Chars (Node (Op)) = Name_Element;
10474 Next_Elmt (Op);
10475 end loop;
10477 if No (Op) then
10478 return False;
10480 else
10481 Rewrite (Exp,
10482 Make_Function_Call (Loc,
10483 Name => New_Occurrence_Of (Node (Op), Loc),
10484 Parameter_Associations => Parameter_Associations (Call)));
10485 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
10486 return True;
10487 end if;
10488 end;
10489 end Is_Container_Element;
10491 -----------------------
10492 -- Is_Constant_Bound --
10493 -----------------------
10495 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
10496 begin
10497 if Compile_Time_Known_Value (Exp) then
10498 return True;
10500 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
10501 return Is_Constant_Object (Entity (Exp))
10502 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
10504 elsif Nkind (Exp) in N_Binary_Op then
10505 return Is_Constant_Bound (Left_Opnd (Exp))
10506 and then Is_Constant_Bound (Right_Opnd (Exp))
10507 and then Scope (Entity (Exp)) = Standard_Standard;
10509 else
10510 return False;
10511 end if;
10512 end Is_Constant_Bound;
10514 --------------------------------------
10515 -- Is_Controlling_Limited_Procedure --
10516 --------------------------------------
10518 function Is_Controlling_Limited_Procedure
10519 (Proc_Nam : Entity_Id) return Boolean
10521 Param_Typ : Entity_Id := Empty;
10523 begin
10524 if Ekind (Proc_Nam) = E_Procedure
10525 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
10526 then
10527 Param_Typ := Etype (Parameter_Type (First (
10528 Parameter_Specifications (Parent (Proc_Nam)))));
10530 -- In this case where an Itype was created, the procedure call has been
10531 -- rewritten.
10533 elsif Present (Associated_Node_For_Itype (Proc_Nam))
10534 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
10535 and then
10536 Present (Parameter_Associations
10537 (Associated_Node_For_Itype (Proc_Nam)))
10538 then
10539 Param_Typ :=
10540 Etype (First (Parameter_Associations
10541 (Associated_Node_For_Itype (Proc_Nam))));
10542 end if;
10544 if Present (Param_Typ) then
10545 return
10546 Is_Interface (Param_Typ)
10547 and then Is_Limited_Record (Param_Typ);
10548 end if;
10550 return False;
10551 end Is_Controlling_Limited_Procedure;
10553 -----------------------------
10554 -- Is_CPP_Constructor_Call --
10555 -----------------------------
10557 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
10558 begin
10559 return Nkind (N) = N_Function_Call
10560 and then Is_CPP_Class (Etype (Etype (N)))
10561 and then Is_Constructor (Entity (Name (N)))
10562 and then Is_Imported (Entity (Name (N)));
10563 end Is_CPP_Constructor_Call;
10565 --------------------
10566 -- Is_Declaration --
10567 --------------------
10569 function Is_Declaration (N : Node_Id) return Boolean is
10570 begin
10571 case Nkind (N) is
10572 when N_Abstract_Subprogram_Declaration |
10573 N_Exception_Declaration |
10574 N_Exception_Renaming_Declaration |
10575 N_Full_Type_Declaration |
10576 N_Generic_Function_Renaming_Declaration |
10577 N_Generic_Package_Declaration |
10578 N_Generic_Package_Renaming_Declaration |
10579 N_Generic_Procedure_Renaming_Declaration |
10580 N_Generic_Subprogram_Declaration |
10581 N_Number_Declaration |
10582 N_Object_Declaration |
10583 N_Object_Renaming_Declaration |
10584 N_Package_Declaration |
10585 N_Package_Renaming_Declaration |
10586 N_Private_Extension_Declaration |
10587 N_Private_Type_Declaration |
10588 N_Subprogram_Declaration |
10589 N_Subprogram_Renaming_Declaration |
10590 N_Subtype_Declaration =>
10591 return True;
10593 when others =>
10594 return False;
10595 end case;
10596 end Is_Declaration;
10598 -----------------
10599 -- Is_Delegate --
10600 -----------------
10602 function Is_Delegate (T : Entity_Id) return Boolean is
10603 Desig_Type : Entity_Id;
10605 begin
10606 if VM_Target /= CLI_Target then
10607 return False;
10608 end if;
10610 -- Access-to-subprograms are delegates in CIL
10612 if Ekind (T) = E_Access_Subprogram_Type then
10613 return True;
10614 end if;
10616 if not Is_Access_Type (T) then
10618 -- A delegate is a managed pointer. If no designated type is defined
10619 -- it means that it's not a delegate.
10621 return False;
10622 end if;
10624 Desig_Type := Etype (Directly_Designated_Type (T));
10626 if not Is_Tagged_Type (Desig_Type) then
10627 return False;
10628 end if;
10630 -- Test if the type is inherited from [mscorlib]System.Delegate
10632 while Etype (Desig_Type) /= Desig_Type loop
10633 if Chars (Scope (Desig_Type)) /= No_Name
10634 and then Is_Imported (Scope (Desig_Type))
10635 and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
10636 then
10637 return True;
10638 end if;
10640 Desig_Type := Etype (Desig_Type);
10641 end loop;
10643 return False;
10644 end Is_Delegate;
10646 ----------------------------------------------
10647 -- Is_Dependent_Component_Of_Mutable_Object --
10648 ----------------------------------------------
10650 function Is_Dependent_Component_Of_Mutable_Object
10651 (Object : Node_Id) return Boolean
10653 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
10654 -- Returns True if and only if Comp is declared within a variant part
10656 --------------------------------
10657 -- Is_Declared_Within_Variant --
10658 --------------------------------
10660 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
10661 Comp_Decl : constant Node_Id := Parent (Comp);
10662 Comp_List : constant Node_Id := Parent (Comp_Decl);
10663 begin
10664 return Nkind (Parent (Comp_List)) = N_Variant;
10665 end Is_Declared_Within_Variant;
10667 P : Node_Id;
10668 Prefix_Type : Entity_Id;
10669 P_Aliased : Boolean := False;
10670 Comp : Entity_Id;
10672 Deref : Node_Id := Object;
10673 -- Dereference node, in something like X.all.Y(2)
10675 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
10677 begin
10678 -- Find the dereference node if any
10680 while Nkind_In (Deref, N_Indexed_Component,
10681 N_Selected_Component,
10682 N_Slice)
10683 loop
10684 Deref := Prefix (Deref);
10685 end loop;
10687 -- Ada 2005: If we have a component or slice of a dereference,
10688 -- something like X.all.Y (2), and the type of X is access-to-constant,
10689 -- Is_Variable will return False, because it is indeed a constant
10690 -- view. But it might be a view of a variable object, so we want the
10691 -- following condition to be True in that case.
10693 if Is_Variable (Object)
10694 or else (Ada_Version >= Ada_2005
10695 and then Nkind (Deref) = N_Explicit_Dereference)
10696 then
10697 if Nkind (Object) = N_Selected_Component then
10698 P := Prefix (Object);
10699 Prefix_Type := Etype (P);
10701 if Is_Entity_Name (P) then
10702 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
10703 Prefix_Type := Base_Type (Prefix_Type);
10704 end if;
10706 if Is_Aliased (Entity (P)) then
10707 P_Aliased := True;
10708 end if;
10710 -- A discriminant check on a selected component may be expanded
10711 -- into a dereference when removing side-effects. Recover the
10712 -- original node and its type, which may be unconstrained.
10714 elsif Nkind (P) = N_Explicit_Dereference
10715 and then not (Comes_From_Source (P))
10716 then
10717 P := Original_Node (P);
10718 Prefix_Type := Etype (P);
10720 else
10721 -- Check for prefix being an aliased component???
10723 null;
10725 end if;
10727 -- A heap object is constrained by its initial value
10729 -- Ada 2005 (AI-363): Always assume the object could be mutable in
10730 -- the dereferenced case, since the access value might denote an
10731 -- unconstrained aliased object, whereas in Ada 95 the designated
10732 -- object is guaranteed to be constrained. A worst-case assumption
10733 -- has to apply in Ada 2005 because we can't tell at compile
10734 -- time whether the object is "constrained by its initial value"
10735 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
10736 -- rules (these rules are acknowledged to need fixing).
10738 if Ada_Version < Ada_2005 then
10739 if Is_Access_Type (Prefix_Type)
10740 or else Nkind (P) = N_Explicit_Dereference
10741 then
10742 return False;
10743 end if;
10745 else pragma Assert (Ada_Version >= Ada_2005);
10746 if Is_Access_Type (Prefix_Type) then
10748 -- If the access type is pool-specific, and there is no
10749 -- constrained partial view of the designated type, then the
10750 -- designated object is known to be constrained.
10752 if Ekind (Prefix_Type) = E_Access_Type
10753 and then not Object_Type_Has_Constrained_Partial_View
10754 (Typ => Designated_Type (Prefix_Type),
10755 Scop => Current_Scope)
10756 then
10757 return False;
10759 -- Otherwise (general access type, or there is a constrained
10760 -- partial view of the designated type), we need to check
10761 -- based on the designated type.
10763 else
10764 Prefix_Type := Designated_Type (Prefix_Type);
10765 end if;
10766 end if;
10767 end if;
10769 Comp :=
10770 Original_Record_Component (Entity (Selector_Name (Object)));
10772 -- As per AI-0017, the renaming is illegal in a generic body, even
10773 -- if the subtype is indefinite.
10775 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
10777 if not Is_Constrained (Prefix_Type)
10778 and then (not Is_Indefinite_Subtype (Prefix_Type)
10779 or else
10780 (Is_Generic_Type (Prefix_Type)
10781 and then Ekind (Current_Scope) = E_Generic_Package
10782 and then In_Package_Body (Current_Scope)))
10784 and then (Is_Declared_Within_Variant (Comp)
10785 or else Has_Discriminant_Dependent_Constraint (Comp))
10786 and then (not P_Aliased or else Ada_Version >= Ada_2005)
10787 then
10788 return True;
10790 -- If the prefix is of an access type at this point, then we want
10791 -- to return False, rather than calling this function recursively
10792 -- on the access object (which itself might be a discriminant-
10793 -- dependent component of some other object, but that isn't
10794 -- relevant to checking the object passed to us). This avoids
10795 -- issuing wrong errors when compiling with -gnatc, where there
10796 -- can be implicit dereferences that have not been expanded.
10798 elsif Is_Access_Type (Etype (Prefix (Object))) then
10799 return False;
10801 else
10802 return
10803 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10804 end if;
10806 elsif Nkind (Object) = N_Indexed_Component
10807 or else Nkind (Object) = N_Slice
10808 then
10809 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10811 -- A type conversion that Is_Variable is a view conversion:
10812 -- go back to the denoted object.
10814 elsif Nkind (Object) = N_Type_Conversion then
10815 return
10816 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
10817 end if;
10818 end if;
10820 return False;
10821 end Is_Dependent_Component_Of_Mutable_Object;
10823 ---------------------
10824 -- Is_Dereferenced --
10825 ---------------------
10827 function Is_Dereferenced (N : Node_Id) return Boolean is
10828 P : constant Node_Id := Parent (N);
10829 begin
10830 return Nkind_In (P, N_Selected_Component,
10831 N_Explicit_Dereference,
10832 N_Indexed_Component,
10833 N_Slice)
10834 and then Prefix (P) = N;
10835 end Is_Dereferenced;
10837 ----------------------
10838 -- Is_Descendent_Of --
10839 ----------------------
10841 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
10842 T : Entity_Id;
10843 Etyp : Entity_Id;
10845 begin
10846 pragma Assert (Nkind (T1) in N_Entity);
10847 pragma Assert (Nkind (T2) in N_Entity);
10849 T := Base_Type (T1);
10851 -- Immediate return if the types match
10853 if T = T2 then
10854 return True;
10856 -- Comment needed here ???
10858 elsif Ekind (T) = E_Class_Wide_Type then
10859 return Etype (T) = T2;
10861 -- All other cases
10863 else
10864 loop
10865 Etyp := Etype (T);
10867 -- Done if we found the type we are looking for
10869 if Etyp = T2 then
10870 return True;
10872 -- Done if no more derivations to check
10874 elsif T = T1
10875 or else T = Etyp
10876 then
10877 return False;
10879 -- Following test catches error cases resulting from prev errors
10881 elsif No (Etyp) then
10882 return False;
10884 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
10885 return False;
10887 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
10888 return False;
10889 end if;
10891 T := Base_Type (Etyp);
10892 end loop;
10893 end if;
10894 end Is_Descendent_Of;
10896 -----------------------------
10897 -- Is_Effectively_Volatile --
10898 -----------------------------
10900 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
10901 begin
10902 if Is_Type (Id) then
10904 -- An arbitrary type is effectively volatile when it is subject to
10905 -- pragma Atomic or Volatile.
10907 if Is_Volatile (Id) then
10908 return True;
10910 -- An array type is effectively volatile when it is subject to pragma
10911 -- Atomic_Components or Volatile_Components or its compolent type is
10912 -- effectively volatile.
10914 elsif Is_Array_Type (Id) then
10915 return
10916 Has_Volatile_Components (Id)
10917 or else
10918 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
10920 else
10921 return False;
10922 end if;
10924 -- Otherwise Id denotes an object
10926 else
10927 return
10928 Is_Volatile (Id)
10929 or else Has_Volatile_Components (Id)
10930 or else Is_Effectively_Volatile (Etype (Id));
10931 end if;
10932 end Is_Effectively_Volatile;
10934 ------------------------------------
10935 -- Is_Effectively_Volatile_Object --
10936 ------------------------------------
10938 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
10939 begin
10940 if Is_Entity_Name (N) then
10941 return Is_Effectively_Volatile (Entity (N));
10943 elsif Nkind (N) = N_Expanded_Name then
10944 return Is_Effectively_Volatile (Entity (N));
10946 elsif Nkind (N) = N_Indexed_Component then
10947 return Is_Effectively_Volatile_Object (Prefix (N));
10949 elsif Nkind (N) = N_Selected_Component then
10950 return
10951 Is_Effectively_Volatile_Object (Prefix (N))
10952 or else
10953 Is_Effectively_Volatile_Object (Selector_Name (N));
10955 else
10956 return False;
10957 end if;
10958 end Is_Effectively_Volatile_Object;
10960 ----------------------------
10961 -- Is_Expression_Function --
10962 ----------------------------
10964 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
10965 Decl : Node_Id;
10967 begin
10968 if Ekind (Subp) /= E_Function then
10969 return False;
10971 else
10972 Decl := Unit_Declaration_Node (Subp);
10973 return Nkind (Decl) = N_Subprogram_Declaration
10974 and then
10975 (Nkind (Original_Node (Decl)) = N_Expression_Function
10976 or else
10977 (Present (Corresponding_Body (Decl))
10978 and then
10979 Nkind (Original_Node
10980 (Unit_Declaration_Node
10981 (Corresponding_Body (Decl)))) =
10982 N_Expression_Function));
10983 end if;
10984 end Is_Expression_Function;
10986 -----------------------
10987 -- Is_EVF_Expression --
10988 -----------------------
10990 function Is_EVF_Expression (N : Node_Id) return Boolean is
10991 Orig_N : constant Node_Id := Original_Node (N);
10992 Alt : Node_Id;
10993 Expr : Node_Id;
10994 Id : Entity_Id;
10996 begin
10997 -- Detect a reference to a formal parameter of a specific tagged type
10998 -- whose related subprogram is subject to pragma Expresions_Visible with
10999 -- value "False".
11001 if Is_Entity_Name (N) and then Present (Entity (N)) then
11002 Id := Entity (N);
11004 return
11005 Is_Formal (Id)
11006 and then Is_Specific_Tagged_Type (Etype (Id))
11007 and then Extensions_Visible_Status (Id) =
11008 Extensions_Visible_False;
11010 -- A case expression is an EVF expression when it contains at least one
11011 -- EVF dependent_expression. Note that a case expression may have been
11012 -- expanded, hence the use of Original_Node.
11014 elsif Nkind (Orig_N) = N_Case_Expression then
11015 Alt := First (Alternatives (Orig_N));
11016 while Present (Alt) loop
11017 if Is_EVF_Expression (Expression (Alt)) then
11018 return True;
11019 end if;
11021 Next (Alt);
11022 end loop;
11024 -- An if expression is an EVF expression when it contains at least one
11025 -- EVF dependent_expression. Note that an if expression may have been
11026 -- expanded, hence the use of Original_Node.
11028 elsif Nkind (Orig_N) = N_If_Expression then
11029 Expr := Next (First (Expressions (Orig_N)));
11030 while Present (Expr) loop
11031 if Is_EVF_Expression (Expr) then
11032 return True;
11033 end if;
11035 Next (Expr);
11036 end loop;
11038 -- A qualified expression or a type conversion is an EVF expression when
11039 -- its operand is an EVF expression.
11041 elsif Nkind_In (N, N_Qualified_Expression,
11042 N_Unchecked_Type_Conversion,
11043 N_Type_Conversion)
11044 then
11045 return Is_EVF_Expression (Expression (N));
11047 -- Attributes 'Loop_Entry, 'Old and 'Update are an EVF expression when
11048 -- their prefix denotes an EVF expression.
11050 elsif Nkind (N) = N_Attribute_Reference
11051 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
11052 Name_Old,
11053 Name_Update)
11054 then
11055 return Is_EVF_Expression (Prefix (N));
11056 end if;
11058 return False;
11059 end Is_EVF_Expression;
11061 --------------
11062 -- Is_False --
11063 --------------
11065 function Is_False (U : Uint) return Boolean is
11066 begin
11067 return (U = 0);
11068 end Is_False;
11070 ---------------------------
11071 -- Is_Fixed_Model_Number --
11072 ---------------------------
11074 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
11075 S : constant Ureal := Small_Value (T);
11076 M : Urealp.Save_Mark;
11077 R : Boolean;
11078 begin
11079 M := Urealp.Mark;
11080 R := (U = UR_Trunc (U / S) * S);
11081 Urealp.Release (M);
11082 return R;
11083 end Is_Fixed_Model_Number;
11085 -------------------------------
11086 -- Is_Fully_Initialized_Type --
11087 -------------------------------
11089 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
11090 begin
11091 -- Scalar types
11093 if Is_Scalar_Type (Typ) then
11095 -- A scalar type with an aspect Default_Value is fully initialized
11097 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
11098 -- of a scalar type, but we don't take that into account here, since
11099 -- we don't want these to affect warnings.
11101 return Has_Default_Aspect (Typ);
11103 elsif Is_Access_Type (Typ) then
11104 return True;
11106 elsif Is_Array_Type (Typ) then
11107 if Is_Fully_Initialized_Type (Component_Type (Typ))
11108 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
11109 then
11110 return True;
11111 end if;
11113 -- An interesting case, if we have a constrained type one of whose
11114 -- bounds is known to be null, then there are no elements to be
11115 -- initialized, so all the elements are initialized.
11117 if Is_Constrained (Typ) then
11118 declare
11119 Indx : Node_Id;
11120 Indx_Typ : Entity_Id;
11121 Lbd, Hbd : Node_Id;
11123 begin
11124 Indx := First_Index (Typ);
11125 while Present (Indx) loop
11126 if Etype (Indx) = Any_Type then
11127 return False;
11129 -- If index is a range, use directly
11131 elsif Nkind (Indx) = N_Range then
11132 Lbd := Low_Bound (Indx);
11133 Hbd := High_Bound (Indx);
11135 else
11136 Indx_Typ := Etype (Indx);
11138 if Is_Private_Type (Indx_Typ) then
11139 Indx_Typ := Full_View (Indx_Typ);
11140 end if;
11142 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
11143 return False;
11144 else
11145 Lbd := Type_Low_Bound (Indx_Typ);
11146 Hbd := Type_High_Bound (Indx_Typ);
11147 end if;
11148 end if;
11150 if Compile_Time_Known_Value (Lbd)
11151 and then
11152 Compile_Time_Known_Value (Hbd)
11153 then
11154 if Expr_Value (Hbd) < Expr_Value (Lbd) then
11155 return True;
11156 end if;
11157 end if;
11159 Next_Index (Indx);
11160 end loop;
11161 end;
11162 end if;
11164 -- If no null indexes, then type is not fully initialized
11166 return False;
11168 -- Record types
11170 elsif Is_Record_Type (Typ) then
11171 if Has_Discriminants (Typ)
11172 and then
11173 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
11174 and then Is_Fully_Initialized_Variant (Typ)
11175 then
11176 return True;
11177 end if;
11179 -- We consider bounded string types to be fully initialized, because
11180 -- otherwise we get false alarms when the Data component is not
11181 -- default-initialized.
11183 if Is_Bounded_String (Typ) then
11184 return True;
11185 end if;
11187 -- Controlled records are considered to be fully initialized if
11188 -- there is a user defined Initialize routine. This may not be
11189 -- entirely correct, but as the spec notes, we are guessing here
11190 -- what is best from the point of view of issuing warnings.
11192 if Is_Controlled (Typ) then
11193 declare
11194 Utyp : constant Entity_Id := Underlying_Type (Typ);
11196 begin
11197 if Present (Utyp) then
11198 declare
11199 Init : constant Entity_Id :=
11200 (Find_Prim_Op
11201 (Underlying_Type (Typ), Name_Initialize));
11203 begin
11204 if Present (Init)
11205 and then Comes_From_Source (Init)
11206 and then not
11207 Is_Predefined_File_Name
11208 (File_Name (Get_Source_File_Index (Sloc (Init))))
11209 then
11210 return True;
11212 elsif Has_Null_Extension (Typ)
11213 and then
11214 Is_Fully_Initialized_Type
11215 (Etype (Base_Type (Typ)))
11216 then
11217 return True;
11218 end if;
11219 end;
11220 end if;
11221 end;
11222 end if;
11224 -- Otherwise see if all record components are initialized
11226 declare
11227 Ent : Entity_Id;
11229 begin
11230 Ent := First_Entity (Typ);
11231 while Present (Ent) loop
11232 if Ekind (Ent) = E_Component
11233 and then (No (Parent (Ent))
11234 or else No (Expression (Parent (Ent))))
11235 and then not Is_Fully_Initialized_Type (Etype (Ent))
11237 -- Special VM case for tag components, which need to be
11238 -- defined in this case, but are never initialized as VMs
11239 -- are using other dispatching mechanisms. Ignore this
11240 -- uninitialized case. Note that this applies both to the
11241 -- uTag entry and the main vtable pointer (CPP_Class case).
11243 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
11244 then
11245 return False;
11246 end if;
11248 Next_Entity (Ent);
11249 end loop;
11250 end;
11252 -- No uninitialized components, so type is fully initialized.
11253 -- Note that this catches the case of no components as well.
11255 return True;
11257 elsif Is_Concurrent_Type (Typ) then
11258 return True;
11260 elsif Is_Private_Type (Typ) then
11261 declare
11262 U : constant Entity_Id := Underlying_Type (Typ);
11264 begin
11265 if No (U) then
11266 return False;
11267 else
11268 return Is_Fully_Initialized_Type (U);
11269 end if;
11270 end;
11272 else
11273 return False;
11274 end if;
11275 end Is_Fully_Initialized_Type;
11277 ----------------------------------
11278 -- Is_Fully_Initialized_Variant --
11279 ----------------------------------
11281 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
11282 Loc : constant Source_Ptr := Sloc (Typ);
11283 Constraints : constant List_Id := New_List;
11284 Components : constant Elist_Id := New_Elmt_List;
11285 Comp_Elmt : Elmt_Id;
11286 Comp_Id : Node_Id;
11287 Comp_List : Node_Id;
11288 Discr : Entity_Id;
11289 Discr_Val : Node_Id;
11291 Report_Errors : Boolean;
11292 pragma Warnings (Off, Report_Errors);
11294 begin
11295 if Serious_Errors_Detected > 0 then
11296 return False;
11297 end if;
11299 if Is_Record_Type (Typ)
11300 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11301 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
11302 then
11303 Comp_List := Component_List (Type_Definition (Parent (Typ)));
11305 Discr := First_Discriminant (Typ);
11306 while Present (Discr) loop
11307 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
11308 Discr_Val := Expression (Parent (Discr));
11310 if Present (Discr_Val)
11311 and then Is_OK_Static_Expression (Discr_Val)
11312 then
11313 Append_To (Constraints,
11314 Make_Component_Association (Loc,
11315 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
11316 Expression => New_Copy (Discr_Val)));
11317 else
11318 return False;
11319 end if;
11320 else
11321 return False;
11322 end if;
11324 Next_Discriminant (Discr);
11325 end loop;
11327 Gather_Components
11328 (Typ => Typ,
11329 Comp_List => Comp_List,
11330 Governed_By => Constraints,
11331 Into => Components,
11332 Report_Errors => Report_Errors);
11334 -- Check that each component present is fully initialized
11336 Comp_Elmt := First_Elmt (Components);
11337 while Present (Comp_Elmt) loop
11338 Comp_Id := Node (Comp_Elmt);
11340 if Ekind (Comp_Id) = E_Component
11341 and then (No (Parent (Comp_Id))
11342 or else No (Expression (Parent (Comp_Id))))
11343 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
11344 then
11345 return False;
11346 end if;
11348 Next_Elmt (Comp_Elmt);
11349 end loop;
11351 return True;
11353 elsif Is_Private_Type (Typ) then
11354 declare
11355 U : constant Entity_Id := Underlying_Type (Typ);
11357 begin
11358 if No (U) then
11359 return False;
11360 else
11361 return Is_Fully_Initialized_Variant (U);
11362 end if;
11363 end;
11365 else
11366 return False;
11367 end if;
11368 end Is_Fully_Initialized_Variant;
11370 ----------------------------
11371 -- Is_Inherited_Operation --
11372 ----------------------------
11374 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
11375 pragma Assert (Is_Overloadable (E));
11376 Kind : constant Node_Kind := Nkind (Parent (E));
11377 begin
11378 return Kind = N_Full_Type_Declaration
11379 or else Kind = N_Private_Extension_Declaration
11380 or else Kind = N_Subtype_Declaration
11381 or else (Ekind (E) = E_Enumeration_Literal
11382 and then Is_Derived_Type (Etype (E)));
11383 end Is_Inherited_Operation;
11385 -------------------------------------
11386 -- Is_Inherited_Operation_For_Type --
11387 -------------------------------------
11389 function Is_Inherited_Operation_For_Type
11390 (E : Entity_Id;
11391 Typ : Entity_Id) return Boolean
11393 begin
11394 -- Check that the operation has been created by the type declaration
11396 return Is_Inherited_Operation (E)
11397 and then Defining_Identifier (Parent (E)) = Typ;
11398 end Is_Inherited_Operation_For_Type;
11400 -----------------
11401 -- Is_Iterator --
11402 -----------------
11404 function Is_Iterator (Typ : Entity_Id) return Boolean is
11405 Ifaces_List : Elist_Id;
11406 Iface_Elmt : Elmt_Id;
11407 Iface : Entity_Id;
11409 begin
11410 if Is_Class_Wide_Type (Typ)
11411 and then Nam_In (Chars (Etype (Typ)), Name_Forward_Iterator,
11412 Name_Reversible_Iterator)
11413 and then
11414 Is_Predefined_File_Name
11415 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
11416 then
11417 return True;
11419 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
11420 return False;
11422 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
11423 return True;
11425 else
11426 Collect_Interfaces (Typ, Ifaces_List);
11428 Iface_Elmt := First_Elmt (Ifaces_List);
11429 while Present (Iface_Elmt) loop
11430 Iface := Node (Iface_Elmt);
11431 if Chars (Iface) = Name_Forward_Iterator
11432 and then
11433 Is_Predefined_File_Name
11434 (Unit_File_Name (Get_Source_Unit (Iface)))
11435 then
11436 return True;
11437 end if;
11439 Next_Elmt (Iface_Elmt);
11440 end loop;
11442 return False;
11443 end if;
11444 end Is_Iterator;
11446 ------------
11447 -- Is_LHS --
11448 ------------
11450 -- We seem to have a lot of overlapping functions that do similar things
11451 -- (testing for left hand sides or lvalues???).
11453 function Is_LHS (N : Node_Id) return Is_LHS_Result is
11454 P : constant Node_Id := Parent (N);
11456 begin
11457 -- Return True if we are the left hand side of an assignment statement
11459 if Nkind (P) = N_Assignment_Statement then
11460 if Name (P) = N then
11461 return Yes;
11462 else
11463 return No;
11464 end if;
11466 -- Case of prefix of indexed or selected component or slice
11468 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
11469 and then N = Prefix (P)
11470 then
11471 -- Here we have the case where the parent P is N.Q or N(Q .. R).
11472 -- If P is an LHS, then N is also effectively an LHS, but there
11473 -- is an important exception. If N is of an access type, then
11474 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
11475 -- case this makes N.all a left hand side but not N itself.
11477 -- If we don't know the type yet, this is the case where we return
11478 -- Unknown, since the answer depends on the type which is unknown.
11480 if No (Etype (N)) then
11481 return Unknown;
11483 -- We have an Etype set, so we can check it
11485 elsif Is_Access_Type (Etype (N)) then
11486 return No;
11488 -- OK, not access type case, so just test whole expression
11490 else
11491 return Is_LHS (P);
11492 end if;
11494 -- All other cases are not left hand sides
11496 else
11497 return No;
11498 end if;
11499 end Is_LHS;
11501 -----------------------------
11502 -- Is_Library_Level_Entity --
11503 -----------------------------
11505 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
11506 begin
11507 -- The following is a small optimization, and it also properly handles
11508 -- discriminals, which in task bodies might appear in expressions before
11509 -- the corresponding procedure has been created, and which therefore do
11510 -- not have an assigned scope.
11512 if Is_Formal (E) then
11513 return False;
11514 end if;
11516 -- Normal test is simply that the enclosing dynamic scope is Standard
11518 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
11519 end Is_Library_Level_Entity;
11521 --------------------------------
11522 -- Is_Limited_Class_Wide_Type --
11523 --------------------------------
11525 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
11526 begin
11527 return
11528 Is_Class_Wide_Type (Typ)
11529 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
11530 end Is_Limited_Class_Wide_Type;
11532 ---------------------------------
11533 -- Is_Local_Variable_Reference --
11534 ---------------------------------
11536 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
11537 begin
11538 if not Is_Entity_Name (Expr) then
11539 return False;
11541 else
11542 declare
11543 Ent : constant Entity_Id := Entity (Expr);
11544 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
11545 begin
11546 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
11547 return False;
11548 else
11549 return Present (Sub) and then Sub = Current_Subprogram;
11550 end if;
11551 end;
11552 end if;
11553 end Is_Local_Variable_Reference;
11555 -------------------------
11556 -- Is_Object_Reference --
11557 -------------------------
11559 function Is_Object_Reference (N : Node_Id) return Boolean is
11561 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
11562 -- Determine whether N is the name of an internally-generated renaming
11564 --------------------------------------
11565 -- Is_Internally_Generated_Renaming --
11566 --------------------------------------
11568 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
11569 P : Node_Id;
11571 begin
11572 P := N;
11573 while Present (P) loop
11574 if Nkind (P) = N_Object_Renaming_Declaration then
11575 return not Comes_From_Source (P);
11576 elsif Is_List_Member (P) then
11577 return False;
11578 end if;
11580 P := Parent (P);
11581 end loop;
11583 return False;
11584 end Is_Internally_Generated_Renaming;
11586 -- Start of processing for Is_Object_Reference
11588 begin
11589 if Is_Entity_Name (N) then
11590 return Present (Entity (N)) and then Is_Object (Entity (N));
11592 else
11593 case Nkind (N) is
11594 when N_Indexed_Component | N_Slice =>
11595 return
11596 Is_Object_Reference (Prefix (N))
11597 or else Is_Access_Type (Etype (Prefix (N)));
11599 -- In Ada 95, a function call is a constant object; a procedure
11600 -- call is not.
11602 when N_Function_Call =>
11603 return Etype (N) /= Standard_Void_Type;
11605 -- Attributes 'Input, 'Old and 'Result produce objects
11607 when N_Attribute_Reference =>
11608 return
11609 Nam_In
11610 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
11612 when N_Selected_Component =>
11613 return
11614 Is_Object_Reference (Selector_Name (N))
11615 and then
11616 (Is_Object_Reference (Prefix (N))
11617 or else Is_Access_Type (Etype (Prefix (N))));
11619 when N_Explicit_Dereference =>
11620 return True;
11622 -- A view conversion of a tagged object is an object reference
11624 when N_Type_Conversion =>
11625 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
11626 and then Is_Tagged_Type (Etype (Expression (N)))
11627 and then Is_Object_Reference (Expression (N));
11629 -- An unchecked type conversion is considered to be an object if
11630 -- the operand is an object (this construction arises only as a
11631 -- result of expansion activities).
11633 when N_Unchecked_Type_Conversion =>
11634 return True;
11636 -- Allow string literals to act as objects as long as they appear
11637 -- in internally-generated renamings. The expansion of iterators
11638 -- may generate such renamings when the range involves a string
11639 -- literal.
11641 when N_String_Literal =>
11642 return Is_Internally_Generated_Renaming (Parent (N));
11644 -- AI05-0003: In Ada 2012 a qualified expression is a name.
11645 -- This allows disambiguation of function calls and the use
11646 -- of aggregates in more contexts.
11648 when N_Qualified_Expression =>
11649 if Ada_Version < Ada_2012 then
11650 return False;
11651 else
11652 return Is_Object_Reference (Expression (N))
11653 or else Nkind (Expression (N)) = N_Aggregate;
11654 end if;
11656 when others =>
11657 return False;
11658 end case;
11659 end if;
11660 end Is_Object_Reference;
11662 -----------------------------------
11663 -- Is_OK_Variable_For_Out_Formal --
11664 -----------------------------------
11666 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
11667 begin
11668 Note_Possible_Modification (AV, Sure => True);
11670 -- We must reject parenthesized variable names. Comes_From_Source is
11671 -- checked because there are currently cases where the compiler violates
11672 -- this rule (e.g. passing a task object to its controlled Initialize
11673 -- routine). This should be properly documented in sinfo???
11675 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
11676 return False;
11678 -- A variable is always allowed
11680 elsif Is_Variable (AV) then
11681 return True;
11683 -- Generalized indexing operations are rewritten as explicit
11684 -- dereferences, and it is only during resolution that we can
11685 -- check whether the context requires an access_to_variable type.
11687 elsif Nkind (AV) = N_Explicit_Dereference
11688 and then Ada_Version >= Ada_2012
11689 and then Nkind (Original_Node (AV)) = N_Indexed_Component
11690 and then Present (Etype (Original_Node (AV)))
11691 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
11692 then
11693 return not Is_Access_Constant (Etype (Prefix (AV)));
11695 -- Unchecked conversions are allowed only if they come from the
11696 -- generated code, which sometimes uses unchecked conversions for out
11697 -- parameters in cases where code generation is unaffected. We tell
11698 -- source unchecked conversions by seeing if they are rewrites of
11699 -- an original Unchecked_Conversion function call, or of an explicit
11700 -- conversion of a function call or an aggregate (as may happen in the
11701 -- expansion of a packed array aggregate).
11703 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
11704 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
11705 return False;
11707 elsif Comes_From_Source (AV)
11708 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
11709 then
11710 return False;
11712 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
11713 return Is_OK_Variable_For_Out_Formal (Expression (AV));
11715 else
11716 return True;
11717 end if;
11719 -- Normal type conversions are allowed if argument is a variable
11721 elsif Nkind (AV) = N_Type_Conversion then
11722 if Is_Variable (Expression (AV))
11723 and then Paren_Count (Expression (AV)) = 0
11724 then
11725 Note_Possible_Modification (Expression (AV), Sure => True);
11726 return True;
11728 -- We also allow a non-parenthesized expression that raises
11729 -- constraint error if it rewrites what used to be a variable
11731 elsif Raises_Constraint_Error (Expression (AV))
11732 and then Paren_Count (Expression (AV)) = 0
11733 and then Is_Variable (Original_Node (Expression (AV)))
11734 then
11735 return True;
11737 -- Type conversion of something other than a variable
11739 else
11740 return False;
11741 end if;
11743 -- If this node is rewritten, then test the original form, if that is
11744 -- OK, then we consider the rewritten node OK (for example, if the
11745 -- original node is a conversion, then Is_Variable will not be true
11746 -- but we still want to allow the conversion if it converts a variable).
11748 elsif Original_Node (AV) /= AV then
11750 -- In Ada 2012, the explicit dereference may be a rewritten call to a
11751 -- Reference function.
11753 if Ada_Version >= Ada_2012
11754 and then Nkind (Original_Node (AV)) = N_Function_Call
11755 and then
11756 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
11757 then
11758 return True;
11760 else
11761 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
11762 end if;
11764 -- All other non-variables are rejected
11766 else
11767 return False;
11768 end if;
11769 end Is_OK_Variable_For_Out_Formal;
11771 -----------------------------------
11772 -- Is_Partially_Initialized_Type --
11773 -----------------------------------
11775 function Is_Partially_Initialized_Type
11776 (Typ : Entity_Id;
11777 Include_Implicit : Boolean := True) return Boolean
11779 begin
11780 if Is_Scalar_Type (Typ) then
11781 return False;
11783 elsif Is_Access_Type (Typ) then
11784 return Include_Implicit;
11786 elsif Is_Array_Type (Typ) then
11788 -- If component type is partially initialized, so is array type
11790 if Is_Partially_Initialized_Type
11791 (Component_Type (Typ), Include_Implicit)
11792 then
11793 return True;
11795 -- Otherwise we are only partially initialized if we are fully
11796 -- initialized (this is the empty array case, no point in us
11797 -- duplicating that code here).
11799 else
11800 return Is_Fully_Initialized_Type (Typ);
11801 end if;
11803 elsif Is_Record_Type (Typ) then
11805 -- A discriminated type is always partially initialized if in
11806 -- all mode
11808 if Has_Discriminants (Typ) and then Include_Implicit then
11809 return True;
11811 -- A tagged type is always partially initialized
11813 elsif Is_Tagged_Type (Typ) then
11814 return True;
11816 -- Case of non-discriminated record
11818 else
11819 declare
11820 Ent : Entity_Id;
11822 Component_Present : Boolean := False;
11823 -- Set True if at least one component is present. If no
11824 -- components are present, then record type is fully
11825 -- initialized (another odd case, like the null array).
11827 begin
11828 -- Loop through components
11830 Ent := First_Entity (Typ);
11831 while Present (Ent) loop
11832 if Ekind (Ent) = E_Component then
11833 Component_Present := True;
11835 -- If a component has an initialization expression then
11836 -- the enclosing record type is partially initialized
11838 if Present (Parent (Ent))
11839 and then Present (Expression (Parent (Ent)))
11840 then
11841 return True;
11843 -- If a component is of a type which is itself partially
11844 -- initialized, then the enclosing record type is also.
11846 elsif Is_Partially_Initialized_Type
11847 (Etype (Ent), Include_Implicit)
11848 then
11849 return True;
11850 end if;
11851 end if;
11853 Next_Entity (Ent);
11854 end loop;
11856 -- No initialized components found. If we found any components
11857 -- they were all uninitialized so the result is false.
11859 if Component_Present then
11860 return False;
11862 -- But if we found no components, then all the components are
11863 -- initialized so we consider the type to be initialized.
11865 else
11866 return True;
11867 end if;
11868 end;
11869 end if;
11871 -- Concurrent types are always fully initialized
11873 elsif Is_Concurrent_Type (Typ) then
11874 return True;
11876 -- For a private type, go to underlying type. If there is no underlying
11877 -- type then just assume this partially initialized. Not clear if this
11878 -- can happen in a non-error case, but no harm in testing for this.
11880 elsif Is_Private_Type (Typ) then
11881 declare
11882 U : constant Entity_Id := Underlying_Type (Typ);
11883 begin
11884 if No (U) then
11885 return True;
11886 else
11887 return Is_Partially_Initialized_Type (U, Include_Implicit);
11888 end if;
11889 end;
11891 -- For any other type (are there any?) assume partially initialized
11893 else
11894 return True;
11895 end if;
11896 end Is_Partially_Initialized_Type;
11898 ------------------------------------
11899 -- Is_Potentially_Persistent_Type --
11900 ------------------------------------
11902 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
11903 Comp : Entity_Id;
11904 Indx : Node_Id;
11906 begin
11907 -- For private type, test corresponding full type
11909 if Is_Private_Type (T) then
11910 return Is_Potentially_Persistent_Type (Full_View (T));
11912 -- Scalar types are potentially persistent
11914 elsif Is_Scalar_Type (T) then
11915 return True;
11917 -- Record type is potentially persistent if not tagged and the types of
11918 -- all it components are potentially persistent, and no component has
11919 -- an initialization expression.
11921 elsif Is_Record_Type (T)
11922 and then not Is_Tagged_Type (T)
11923 and then not Is_Partially_Initialized_Type (T)
11924 then
11925 Comp := First_Component (T);
11926 while Present (Comp) loop
11927 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
11928 return False;
11929 else
11930 Next_Entity (Comp);
11931 end if;
11932 end loop;
11934 return True;
11936 -- Array type is potentially persistent if its component type is
11937 -- potentially persistent and if all its constraints are static.
11939 elsif Is_Array_Type (T) then
11940 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
11941 return False;
11942 end if;
11944 Indx := First_Index (T);
11945 while Present (Indx) loop
11946 if not Is_OK_Static_Subtype (Etype (Indx)) then
11947 return False;
11948 else
11949 Next_Index (Indx);
11950 end if;
11951 end loop;
11953 return True;
11955 -- All other types are not potentially persistent
11957 else
11958 return False;
11959 end if;
11960 end Is_Potentially_Persistent_Type;
11962 --------------------------------
11963 -- Is_Potentially_Unevaluated --
11964 --------------------------------
11966 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
11967 Par : Node_Id;
11968 Expr : Node_Id;
11970 begin
11971 Expr := N;
11972 Par := Parent (N);
11974 -- A postcondition whose expression is a short-circuit is broken down
11975 -- into individual aspects for better exception reporting. The original
11976 -- short-circuit expression is rewritten as the second operand, and an
11977 -- occurrence of 'Old in that operand is potentially unevaluated.
11978 -- See Sem_ch13.adb for details of this transformation.
11980 if Nkind (Original_Node (Par)) = N_And_Then then
11981 return True;
11982 end if;
11984 while not Nkind_In (Par, N_If_Expression,
11985 N_Case_Expression,
11986 N_And_Then,
11987 N_Or_Else,
11988 N_In,
11989 N_Not_In)
11990 loop
11991 Expr := Par;
11992 Par := Parent (Par);
11994 -- If the context is not an expression, or if is the result of
11995 -- expansion of an enclosing construct (such as another attribute)
11996 -- the predicate does not apply.
11998 if Nkind (Par) not in N_Subexpr
11999 or else not Comes_From_Source (Par)
12000 then
12001 return False;
12002 end if;
12003 end loop;
12005 if Nkind (Par) = N_If_Expression then
12006 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
12008 elsif Nkind (Par) = N_Case_Expression then
12009 return Expr /= Expression (Par);
12011 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
12012 return Expr = Right_Opnd (Par);
12014 elsif Nkind_In (Par, N_In, N_Not_In) then
12015 return Expr /= Left_Opnd (Par);
12017 else
12018 return False;
12019 end if;
12020 end Is_Potentially_Unevaluated;
12022 ---------------------------------
12023 -- Is_Protected_Self_Reference --
12024 ---------------------------------
12026 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
12028 function In_Access_Definition (N : Node_Id) return Boolean;
12029 -- Returns true if N belongs to an access definition
12031 --------------------------
12032 -- In_Access_Definition --
12033 --------------------------
12035 function In_Access_Definition (N : Node_Id) return Boolean is
12036 P : Node_Id;
12038 begin
12039 P := Parent (N);
12040 while Present (P) loop
12041 if Nkind (P) = N_Access_Definition then
12042 return True;
12043 end if;
12045 P := Parent (P);
12046 end loop;
12048 return False;
12049 end In_Access_Definition;
12051 -- Start of processing for Is_Protected_Self_Reference
12053 begin
12054 -- Verify that prefix is analyzed and has the proper form. Note that
12055 -- the attributes Elab_Spec, Elab_Body, Elab_Subp_Body and UET_Address,
12056 -- which also produce the address of an entity, do not analyze their
12057 -- prefix because they denote entities that are not necessarily visible.
12058 -- Neither of them can apply to a protected type.
12060 return Ada_Version >= Ada_2005
12061 and then Is_Entity_Name (N)
12062 and then Present (Entity (N))
12063 and then Is_Protected_Type (Entity (N))
12064 and then In_Open_Scopes (Entity (N))
12065 and then not In_Access_Definition (N);
12066 end Is_Protected_Self_Reference;
12068 -----------------------------
12069 -- Is_RCI_Pkg_Spec_Or_Body --
12070 -----------------------------
12072 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
12074 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
12075 -- Return True if the unit of Cunit is an RCI package declaration
12077 ---------------------------
12078 -- Is_RCI_Pkg_Decl_Cunit --
12079 ---------------------------
12081 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
12082 The_Unit : constant Node_Id := Unit (Cunit);
12084 begin
12085 if Nkind (The_Unit) /= N_Package_Declaration then
12086 return False;
12087 end if;
12089 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
12090 end Is_RCI_Pkg_Decl_Cunit;
12092 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
12094 begin
12095 return Is_RCI_Pkg_Decl_Cunit (Cunit)
12096 or else
12097 (Nkind (Unit (Cunit)) = N_Package_Body
12098 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
12099 end Is_RCI_Pkg_Spec_Or_Body;
12101 -----------------------------------------
12102 -- Is_Remote_Access_To_Class_Wide_Type --
12103 -----------------------------------------
12105 function Is_Remote_Access_To_Class_Wide_Type
12106 (E : Entity_Id) return Boolean
12108 begin
12109 -- A remote access to class-wide type is a general access to object type
12110 -- declared in the visible part of a Remote_Types or Remote_Call_
12111 -- Interface unit.
12113 return Ekind (E) = E_General_Access_Type
12114 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12115 end Is_Remote_Access_To_Class_Wide_Type;
12117 -----------------------------------------
12118 -- Is_Remote_Access_To_Subprogram_Type --
12119 -----------------------------------------
12121 function Is_Remote_Access_To_Subprogram_Type
12122 (E : Entity_Id) return Boolean
12124 begin
12125 return (Ekind (E) = E_Access_Subprogram_Type
12126 or else (Ekind (E) = E_Record_Type
12127 and then Present (Corresponding_Remote_Type (E))))
12128 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12129 end Is_Remote_Access_To_Subprogram_Type;
12131 --------------------
12132 -- Is_Remote_Call --
12133 --------------------
12135 function Is_Remote_Call (N : Node_Id) return Boolean is
12136 begin
12137 if Nkind (N) not in N_Subprogram_Call then
12139 -- An entry call cannot be remote
12141 return False;
12143 elsif Nkind (Name (N)) in N_Has_Entity
12144 and then Is_Remote_Call_Interface (Entity (Name (N)))
12145 then
12146 -- A subprogram declared in the spec of a RCI package is remote
12148 return True;
12150 elsif Nkind (Name (N)) = N_Explicit_Dereference
12151 and then Is_Remote_Access_To_Subprogram_Type
12152 (Etype (Prefix (Name (N))))
12153 then
12154 -- The dereference of a RAS is a remote call
12156 return True;
12158 elsif Present (Controlling_Argument (N))
12159 and then Is_Remote_Access_To_Class_Wide_Type
12160 (Etype (Controlling_Argument (N)))
12161 then
12162 -- Any primitive operation call with a controlling argument of
12163 -- a RACW type is a remote call.
12165 return True;
12166 end if;
12168 -- All other calls are local calls
12170 return False;
12171 end Is_Remote_Call;
12173 ----------------------
12174 -- Is_Renamed_Entry --
12175 ----------------------
12177 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
12178 Orig_Node : Node_Id := Empty;
12179 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
12181 function Is_Entry (Nam : Node_Id) return Boolean;
12182 -- Determine whether Nam is an entry. Traverse selectors if there are
12183 -- nested selected components.
12185 --------------
12186 -- Is_Entry --
12187 --------------
12189 function Is_Entry (Nam : Node_Id) return Boolean is
12190 begin
12191 if Nkind (Nam) = N_Selected_Component then
12192 return Is_Entry (Selector_Name (Nam));
12193 end if;
12195 return Ekind (Entity (Nam)) = E_Entry;
12196 end Is_Entry;
12198 -- Start of processing for Is_Renamed_Entry
12200 begin
12201 if Present (Alias (Proc_Nam)) then
12202 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
12203 end if;
12205 -- Look for a rewritten subprogram renaming declaration
12207 if Nkind (Subp_Decl) = N_Subprogram_Declaration
12208 and then Present (Original_Node (Subp_Decl))
12209 then
12210 Orig_Node := Original_Node (Subp_Decl);
12211 end if;
12213 -- The rewritten subprogram is actually an entry
12215 if Present (Orig_Node)
12216 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
12217 and then Is_Entry (Name (Orig_Node))
12218 then
12219 return True;
12220 end if;
12222 return False;
12223 end Is_Renamed_Entry;
12225 ----------------------------
12226 -- Is_Reversible_Iterator --
12227 ----------------------------
12229 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
12230 Ifaces_List : Elist_Id;
12231 Iface_Elmt : Elmt_Id;
12232 Iface : Entity_Id;
12234 begin
12235 if Is_Class_Wide_Type (Typ)
12236 and then Chars (Etype (Typ)) = Name_Reversible_Iterator
12237 and then Is_Predefined_File_Name
12238 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
12239 then
12240 return True;
12242 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
12243 return False;
12245 else
12246 Collect_Interfaces (Typ, Ifaces_List);
12248 Iface_Elmt := First_Elmt (Ifaces_List);
12249 while Present (Iface_Elmt) loop
12250 Iface := Node (Iface_Elmt);
12251 if Chars (Iface) = Name_Reversible_Iterator
12252 and then
12253 Is_Predefined_File_Name
12254 (Unit_File_Name (Get_Source_Unit (Iface)))
12255 then
12256 return True;
12257 end if;
12259 Next_Elmt (Iface_Elmt);
12260 end loop;
12261 end if;
12263 return False;
12264 end Is_Reversible_Iterator;
12266 ----------------------
12267 -- Is_Selector_Name --
12268 ----------------------
12270 function Is_Selector_Name (N : Node_Id) return Boolean is
12271 begin
12272 if not Is_List_Member (N) then
12273 declare
12274 P : constant Node_Id := Parent (N);
12275 begin
12276 return Nkind_In (P, N_Expanded_Name,
12277 N_Generic_Association,
12278 N_Parameter_Association,
12279 N_Selected_Component)
12280 and then Selector_Name (P) = N;
12281 end;
12283 else
12284 declare
12285 L : constant List_Id := List_Containing (N);
12286 P : constant Node_Id := Parent (L);
12287 begin
12288 return (Nkind (P) = N_Discriminant_Association
12289 and then Selector_Names (P) = L)
12290 or else
12291 (Nkind (P) = N_Component_Association
12292 and then Choices (P) = L);
12293 end;
12294 end if;
12295 end Is_Selector_Name;
12297 -------------------------------------
12298 -- Is_SPARK_05_Initialization_Expr --
12299 -------------------------------------
12301 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
12302 Is_Ok : Boolean;
12303 Expr : Node_Id;
12304 Comp_Assn : Node_Id;
12305 Orig_N : constant Node_Id := Original_Node (N);
12307 begin
12308 Is_Ok := True;
12310 if not Comes_From_Source (Orig_N) then
12311 goto Done;
12312 end if;
12314 pragma Assert (Nkind (Orig_N) in N_Subexpr);
12316 case Nkind (Orig_N) is
12317 when N_Character_Literal |
12318 N_Integer_Literal |
12319 N_Real_Literal |
12320 N_String_Literal =>
12321 null;
12323 when N_Identifier |
12324 N_Expanded_Name =>
12325 if Is_Entity_Name (Orig_N)
12326 and then Present (Entity (Orig_N)) -- needed in some cases
12327 then
12328 case Ekind (Entity (Orig_N)) is
12329 when E_Constant |
12330 E_Enumeration_Literal |
12331 E_Named_Integer |
12332 E_Named_Real =>
12333 null;
12334 when others =>
12335 if Is_Type (Entity (Orig_N)) then
12336 null;
12337 else
12338 Is_Ok := False;
12339 end if;
12340 end case;
12341 end if;
12343 when N_Qualified_Expression |
12344 N_Type_Conversion =>
12345 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
12347 when N_Unary_Op =>
12348 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12350 when N_Binary_Op |
12351 N_Short_Circuit |
12352 N_Membership_Test =>
12353 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
12354 and then
12355 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12357 when N_Aggregate |
12358 N_Extension_Aggregate =>
12359 if Nkind (Orig_N) = N_Extension_Aggregate then
12360 Is_Ok :=
12361 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
12362 end if;
12364 Expr := First (Expressions (Orig_N));
12365 while Present (Expr) loop
12366 if not Is_SPARK_05_Initialization_Expr (Expr) then
12367 Is_Ok := False;
12368 goto Done;
12369 end if;
12371 Next (Expr);
12372 end loop;
12374 Comp_Assn := First (Component_Associations (Orig_N));
12375 while Present (Comp_Assn) loop
12376 Expr := Expression (Comp_Assn);
12378 -- Note: test for Present here needed for box assocation
12380 if Present (Expr)
12381 and then not Is_SPARK_05_Initialization_Expr (Expr)
12382 then
12383 Is_Ok := False;
12384 goto Done;
12385 end if;
12387 Next (Comp_Assn);
12388 end loop;
12390 when N_Attribute_Reference =>
12391 if Nkind (Prefix (Orig_N)) in N_Subexpr then
12392 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
12393 end if;
12395 Expr := First (Expressions (Orig_N));
12396 while Present (Expr) loop
12397 if not Is_SPARK_05_Initialization_Expr (Expr) then
12398 Is_Ok := False;
12399 goto Done;
12400 end if;
12402 Next (Expr);
12403 end loop;
12405 -- Selected components might be expanded named not yet resolved, so
12406 -- default on the safe side. (Eg on sparklex.ads)
12408 when N_Selected_Component =>
12409 null;
12411 when others =>
12412 Is_Ok := False;
12413 end case;
12415 <<Done>>
12416 return Is_Ok;
12417 end Is_SPARK_05_Initialization_Expr;
12419 ----------------------------------
12420 -- Is_SPARK_05_Object_Reference --
12421 ----------------------------------
12423 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
12424 begin
12425 if Is_Entity_Name (N) then
12426 return Present (Entity (N))
12427 and then
12428 (Ekind_In (Entity (N), E_Constant, E_Variable)
12429 or else Ekind (Entity (N)) in Formal_Kind);
12431 else
12432 case Nkind (N) is
12433 when N_Selected_Component =>
12434 return Is_SPARK_05_Object_Reference (Prefix (N));
12436 when others =>
12437 return False;
12438 end case;
12439 end if;
12440 end Is_SPARK_05_Object_Reference;
12442 -----------------------------
12443 -- Is_Specific_Tagged_Type --
12444 -----------------------------
12446 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
12447 Full_Typ : Entity_Id;
12449 begin
12450 -- Handle private types
12452 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
12453 Full_Typ := Full_View (Typ);
12454 else
12455 Full_Typ := Typ;
12456 end if;
12458 -- A specific tagged type is a non-class-wide tagged type
12460 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
12461 end Is_Specific_Tagged_Type;
12463 ------------------
12464 -- Is_Statement --
12465 ------------------
12467 function Is_Statement (N : Node_Id) return Boolean is
12468 begin
12469 return
12470 Nkind (N) in N_Statement_Other_Than_Procedure_Call
12471 or else Nkind (N) = N_Procedure_Call_Statement;
12472 end Is_Statement;
12474 --------------------------------------------------
12475 -- Is_Subprogram_Stub_Without_Prior_Declaration --
12476 --------------------------------------------------
12478 function Is_Subprogram_Stub_Without_Prior_Declaration
12479 (N : Node_Id) return Boolean
12481 begin
12482 -- A subprogram stub without prior declaration serves as declaration for
12483 -- the actual subprogram body. As such, it has an attached defining
12484 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
12486 return Nkind (N) = N_Subprogram_Body_Stub
12487 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
12488 end Is_Subprogram_Stub_Without_Prior_Declaration;
12490 ---------------------------------
12491 -- Is_Synchronized_Tagged_Type --
12492 ---------------------------------
12494 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
12495 Kind : constant Entity_Kind := Ekind (Base_Type (E));
12497 begin
12498 -- A task or protected type derived from an interface is a tagged type.
12499 -- Such a tagged type is called a synchronized tagged type, as are
12500 -- synchronized interfaces and private extensions whose declaration
12501 -- includes the reserved word synchronized.
12503 return (Is_Tagged_Type (E)
12504 and then (Kind = E_Task_Type
12505 or else
12506 Kind = E_Protected_Type))
12507 or else
12508 (Is_Interface (E)
12509 and then Is_Synchronized_Interface (E))
12510 or else
12511 (Ekind (E) = E_Record_Type_With_Private
12512 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
12513 and then (Synchronized_Present (Parent (E))
12514 or else Is_Synchronized_Interface (Etype (E))));
12515 end Is_Synchronized_Tagged_Type;
12517 -----------------
12518 -- Is_Transfer --
12519 -----------------
12521 function Is_Transfer (N : Node_Id) return Boolean is
12522 Kind : constant Node_Kind := Nkind (N);
12524 begin
12525 if Kind = N_Simple_Return_Statement
12526 or else
12527 Kind = N_Extended_Return_Statement
12528 or else
12529 Kind = N_Goto_Statement
12530 or else
12531 Kind = N_Raise_Statement
12532 or else
12533 Kind = N_Requeue_Statement
12534 then
12535 return True;
12537 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
12538 and then No (Condition (N))
12539 then
12540 return True;
12542 elsif Kind = N_Procedure_Call_Statement
12543 and then Is_Entity_Name (Name (N))
12544 and then Present (Entity (Name (N)))
12545 and then No_Return (Entity (Name (N)))
12546 then
12547 return True;
12549 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
12550 return True;
12552 else
12553 return False;
12554 end if;
12555 end Is_Transfer;
12557 -------------
12558 -- Is_True --
12559 -------------
12561 function Is_True (U : Uint) return Boolean is
12562 begin
12563 return (U /= 0);
12564 end Is_True;
12566 --------------------------------------
12567 -- Is_Unchecked_Conversion_Instance --
12568 --------------------------------------
12570 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
12571 Gen_Par : Entity_Id;
12573 begin
12574 -- Look for a function whose generic parent is the predefined intrinsic
12575 -- function Unchecked_Conversion.
12577 if Ekind (Id) = E_Function then
12578 Gen_Par := Generic_Parent (Parent (Id));
12580 return
12581 Present (Gen_Par)
12582 and then Chars (Gen_Par) = Name_Unchecked_Conversion
12583 and then Is_Intrinsic_Subprogram (Gen_Par)
12584 and then Is_Predefined_File_Name
12585 (Unit_File_Name (Get_Source_Unit (Gen_Par)));
12586 end if;
12588 return False;
12589 end Is_Unchecked_Conversion_Instance;
12591 -------------------------------
12592 -- Is_Universal_Numeric_Type --
12593 -------------------------------
12595 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
12596 begin
12597 return T = Universal_Integer or else T = Universal_Real;
12598 end Is_Universal_Numeric_Type;
12600 -------------------
12601 -- Is_Value_Type --
12602 -------------------
12604 function Is_Value_Type (T : Entity_Id) return Boolean is
12605 begin
12606 return VM_Target = CLI_Target
12607 and then Nkind (T) in N_Has_Chars
12608 and then Chars (T) /= No_Name
12609 and then Get_Name_String (Chars (T)) = "valuetype";
12610 end Is_Value_Type;
12612 ----------------------------
12613 -- Is_Variable_Size_Array --
12614 ----------------------------
12616 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
12617 Idx : Node_Id;
12619 begin
12620 pragma Assert (Is_Array_Type (E));
12622 -- Check if some index is initialized with a non-constant value
12624 Idx := First_Index (E);
12625 while Present (Idx) loop
12626 if Nkind (Idx) = N_Range then
12627 if not Is_Constant_Bound (Low_Bound (Idx))
12628 or else not Is_Constant_Bound (High_Bound (Idx))
12629 then
12630 return True;
12631 end if;
12632 end if;
12634 Idx := Next_Index (Idx);
12635 end loop;
12637 return False;
12638 end Is_Variable_Size_Array;
12640 -----------------------------
12641 -- Is_Variable_Size_Record --
12642 -----------------------------
12644 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
12645 Comp : Entity_Id;
12646 Comp_Typ : Entity_Id;
12648 begin
12649 pragma Assert (Is_Record_Type (E));
12651 Comp := First_Entity (E);
12652 while Present (Comp) loop
12653 Comp_Typ := Etype (Comp);
12655 -- Recursive call if the record type has discriminants
12657 if Is_Record_Type (Comp_Typ)
12658 and then Has_Discriminants (Comp_Typ)
12659 and then Is_Variable_Size_Record (Comp_Typ)
12660 then
12661 return True;
12663 elsif Is_Array_Type (Comp_Typ)
12664 and then Is_Variable_Size_Array (Comp_Typ)
12665 then
12666 return True;
12667 end if;
12669 Next_Entity (Comp);
12670 end loop;
12672 return False;
12673 end Is_Variable_Size_Record;
12675 -----------------
12676 -- Is_Variable --
12677 -----------------
12679 function Is_Variable
12680 (N : Node_Id;
12681 Use_Original_Node : Boolean := True) return Boolean
12683 Orig_Node : Node_Id;
12685 function In_Protected_Function (E : Entity_Id) return Boolean;
12686 -- Within a protected function, the private components of the enclosing
12687 -- protected type are constants. A function nested within a (protected)
12688 -- procedure is not itself protected. Within the body of a protected
12689 -- function the current instance of the protected type is a constant.
12691 function Is_Variable_Prefix (P : Node_Id) return Boolean;
12692 -- Prefixes can involve implicit dereferences, in which case we must
12693 -- test for the case of a reference of a constant access type, which can
12694 -- can never be a variable.
12696 ---------------------------
12697 -- In_Protected_Function --
12698 ---------------------------
12700 function In_Protected_Function (E : Entity_Id) return Boolean is
12701 Prot : Entity_Id;
12702 S : Entity_Id;
12704 begin
12705 -- E is the current instance of a type
12707 if Is_Type (E) then
12708 Prot := E;
12710 -- E is an object
12712 else
12713 Prot := Scope (E);
12714 end if;
12716 if not Is_Protected_Type (Prot) then
12717 return False;
12719 else
12720 S := Current_Scope;
12721 while Present (S) and then S /= Prot loop
12722 if Ekind (S) = E_Function and then Scope (S) = Prot then
12723 return True;
12724 end if;
12726 S := Scope (S);
12727 end loop;
12729 return False;
12730 end if;
12731 end In_Protected_Function;
12733 ------------------------
12734 -- Is_Variable_Prefix --
12735 ------------------------
12737 function Is_Variable_Prefix (P : Node_Id) return Boolean is
12738 begin
12739 if Is_Access_Type (Etype (P)) then
12740 return not Is_Access_Constant (Root_Type (Etype (P)));
12742 -- For the case of an indexed component whose prefix has a packed
12743 -- array type, the prefix has been rewritten into a type conversion.
12744 -- Determine variable-ness from the converted expression.
12746 elsif Nkind (P) = N_Type_Conversion
12747 and then not Comes_From_Source (P)
12748 and then Is_Array_Type (Etype (P))
12749 and then Is_Packed (Etype (P))
12750 then
12751 return Is_Variable (Expression (P));
12753 else
12754 return Is_Variable (P);
12755 end if;
12756 end Is_Variable_Prefix;
12758 -- Start of processing for Is_Variable
12760 begin
12761 -- Check if we perform the test on the original node since this may be a
12762 -- test of syntactic categories which must not be disturbed by whatever
12763 -- rewriting might have occurred. For example, an aggregate, which is
12764 -- certainly NOT a variable, could be turned into a variable by
12765 -- expansion.
12767 if Use_Original_Node then
12768 Orig_Node := Original_Node (N);
12769 else
12770 Orig_Node := N;
12771 end if;
12773 -- Definitely OK if Assignment_OK is set. Since this is something that
12774 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
12776 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
12777 return True;
12779 -- Normally we go to the original node, but there is one exception where
12780 -- we use the rewritten node, namely when it is an explicit dereference.
12781 -- The generated code may rewrite a prefix which is an access type with
12782 -- an explicit dereference. The dereference is a variable, even though
12783 -- the original node may not be (since it could be a constant of the
12784 -- access type).
12786 -- In Ada 2005 we have a further case to consider: the prefix may be a
12787 -- function call given in prefix notation. The original node appears to
12788 -- be a selected component, but we need to examine the call.
12790 elsif Nkind (N) = N_Explicit_Dereference
12791 and then Nkind (Orig_Node) /= N_Explicit_Dereference
12792 and then Present (Etype (Orig_Node))
12793 and then Is_Access_Type (Etype (Orig_Node))
12794 then
12795 -- Note that if the prefix is an explicit dereference that does not
12796 -- come from source, we must check for a rewritten function call in
12797 -- prefixed notation before other forms of rewriting, to prevent a
12798 -- compiler crash.
12800 return
12801 (Nkind (Orig_Node) = N_Function_Call
12802 and then not Is_Access_Constant (Etype (Prefix (N))))
12803 or else
12804 Is_Variable_Prefix (Original_Node (Prefix (N)));
12806 -- in Ada 2012, the dereference may have been added for a type with
12807 -- a declared implicit dereference aspect. Check that it is not an
12808 -- access to constant.
12810 elsif Nkind (N) = N_Explicit_Dereference
12811 and then Present (Etype (Orig_Node))
12812 and then Ada_Version >= Ada_2012
12813 and then Has_Implicit_Dereference (Etype (Orig_Node))
12814 then
12815 return not Is_Access_Constant (Etype (Prefix (N)));
12817 -- A function call is never a variable
12819 elsif Nkind (N) = N_Function_Call then
12820 return False;
12822 -- All remaining checks use the original node
12824 elsif Is_Entity_Name (Orig_Node)
12825 and then Present (Entity (Orig_Node))
12826 then
12827 declare
12828 E : constant Entity_Id := Entity (Orig_Node);
12829 K : constant Entity_Kind := Ekind (E);
12831 begin
12832 return (K = E_Variable
12833 and then Nkind (Parent (E)) /= N_Exception_Handler)
12834 or else (K = E_Component
12835 and then not In_Protected_Function (E))
12836 or else K = E_Out_Parameter
12837 or else K = E_In_Out_Parameter
12838 or else K = E_Generic_In_Out_Parameter
12840 -- Current instance of type. If this is a protected type, check
12841 -- we are not within the body of one of its protected functions.
12843 or else (Is_Type (E)
12844 and then In_Open_Scopes (E)
12845 and then not In_Protected_Function (E))
12847 or else (Is_Incomplete_Or_Private_Type (E)
12848 and then In_Open_Scopes (Full_View (E)));
12849 end;
12851 else
12852 case Nkind (Orig_Node) is
12853 when N_Indexed_Component | N_Slice =>
12854 return Is_Variable_Prefix (Prefix (Orig_Node));
12856 when N_Selected_Component =>
12857 return (Is_Variable (Selector_Name (Orig_Node))
12858 and then Is_Variable_Prefix (Prefix (Orig_Node)))
12859 or else
12860 (Nkind (N) = N_Expanded_Name
12861 and then Scope (Entity (N)) = Entity (Prefix (N)));
12863 -- For an explicit dereference, the type of the prefix cannot
12864 -- be an access to constant or an access to subprogram.
12866 when N_Explicit_Dereference =>
12867 declare
12868 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
12869 begin
12870 return Is_Access_Type (Typ)
12871 and then not Is_Access_Constant (Root_Type (Typ))
12872 and then Ekind (Typ) /= E_Access_Subprogram_Type;
12873 end;
12875 -- The type conversion is the case where we do not deal with the
12876 -- context dependent special case of an actual parameter. Thus
12877 -- the type conversion is only considered a variable for the
12878 -- purposes of this routine if the target type is tagged. However,
12879 -- a type conversion is considered to be a variable if it does not
12880 -- come from source (this deals for example with the conversions
12881 -- of expressions to their actual subtypes).
12883 when N_Type_Conversion =>
12884 return Is_Variable (Expression (Orig_Node))
12885 and then
12886 (not Comes_From_Source (Orig_Node)
12887 or else
12888 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
12889 and then
12890 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
12892 -- GNAT allows an unchecked type conversion as a variable. This
12893 -- only affects the generation of internal expanded code, since
12894 -- calls to instantiations of Unchecked_Conversion are never
12895 -- considered variables (since they are function calls).
12897 when N_Unchecked_Type_Conversion =>
12898 return Is_Variable (Expression (Orig_Node));
12900 when others =>
12901 return False;
12902 end case;
12903 end if;
12904 end Is_Variable;
12906 ---------------------------
12907 -- Is_Visibly_Controlled --
12908 ---------------------------
12910 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
12911 Root : constant Entity_Id := Root_Type (T);
12912 begin
12913 return Chars (Scope (Root)) = Name_Finalization
12914 and then Chars (Scope (Scope (Root))) = Name_Ada
12915 and then Scope (Scope (Scope (Root))) = Standard_Standard;
12916 end Is_Visibly_Controlled;
12918 ------------------------
12919 -- Is_Volatile_Object --
12920 ------------------------
12922 function Is_Volatile_Object (N : Node_Id) return Boolean is
12924 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
12925 -- If prefix is an implicit dereference, examine designated type
12927 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
12928 -- Determines if given object has volatile components
12930 ------------------------
12931 -- Is_Volatile_Prefix --
12932 ------------------------
12934 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
12935 Typ : constant Entity_Id := Etype (N);
12937 begin
12938 if Is_Access_Type (Typ) then
12939 declare
12940 Dtyp : constant Entity_Id := Designated_Type (Typ);
12942 begin
12943 return Is_Volatile (Dtyp)
12944 or else Has_Volatile_Components (Dtyp);
12945 end;
12947 else
12948 return Object_Has_Volatile_Components (N);
12949 end if;
12950 end Is_Volatile_Prefix;
12952 ------------------------------------
12953 -- Object_Has_Volatile_Components --
12954 ------------------------------------
12956 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
12957 Typ : constant Entity_Id := Etype (N);
12959 begin
12960 if Is_Volatile (Typ)
12961 or else Has_Volatile_Components (Typ)
12962 then
12963 return True;
12965 elsif Is_Entity_Name (N)
12966 and then (Has_Volatile_Components (Entity (N))
12967 or else Is_Volatile (Entity (N)))
12968 then
12969 return True;
12971 elsif Nkind (N) = N_Indexed_Component
12972 or else Nkind (N) = N_Selected_Component
12973 then
12974 return Is_Volatile_Prefix (Prefix (N));
12976 else
12977 return False;
12978 end if;
12979 end Object_Has_Volatile_Components;
12981 -- Start of processing for Is_Volatile_Object
12983 begin
12984 if Nkind (N) = N_Defining_Identifier then
12985 return Is_Volatile (N) or else Is_Volatile (Etype (N));
12987 elsif Nkind (N) = N_Expanded_Name then
12988 return Is_Volatile_Object (Entity (N));
12990 elsif Is_Volatile (Etype (N))
12991 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
12992 then
12993 return True;
12995 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
12996 and then Is_Volatile_Prefix (Prefix (N))
12997 then
12998 return True;
13000 elsif Nkind (N) = N_Selected_Component
13001 and then Is_Volatile (Entity (Selector_Name (N)))
13002 then
13003 return True;
13005 else
13006 return False;
13007 end if;
13008 end Is_Volatile_Object;
13010 ---------------------------
13011 -- Itype_Has_Declaration --
13012 ---------------------------
13014 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
13015 begin
13016 pragma Assert (Is_Itype (Id));
13017 return Present (Parent (Id))
13018 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
13019 N_Subtype_Declaration)
13020 and then Defining_Entity (Parent (Id)) = Id;
13021 end Itype_Has_Declaration;
13023 -------------------------
13024 -- Kill_Current_Values --
13025 -------------------------
13027 procedure Kill_Current_Values
13028 (Ent : Entity_Id;
13029 Last_Assignment_Only : Boolean := False)
13031 begin
13032 if Is_Assignable (Ent) then
13033 Set_Last_Assignment (Ent, Empty);
13034 end if;
13036 if Is_Object (Ent) then
13037 if not Last_Assignment_Only then
13038 Kill_Checks (Ent);
13039 Set_Current_Value (Ent, Empty);
13041 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
13042 -- for a constant. Once the constant is elaborated, its value is
13043 -- not changed, therefore the associated flags that describe the
13044 -- value should not be modified either.
13046 if Ekind (Ent) = E_Constant then
13047 null;
13049 -- Non-constant entities
13051 else
13052 if not Can_Never_Be_Null (Ent) then
13053 Set_Is_Known_Non_Null (Ent, False);
13054 end if;
13056 Set_Is_Known_Null (Ent, False);
13058 -- Reset the Is_Known_Valid flag unless the type is always
13059 -- valid. This does not apply to a loop parameter because its
13060 -- bounds are defined by the loop header and therefore always
13061 -- valid.
13063 if not Is_Known_Valid (Etype (Ent))
13064 and then Ekind (Ent) /= E_Loop_Parameter
13065 then
13066 Set_Is_Known_Valid (Ent, False);
13067 end if;
13068 end if;
13069 end if;
13070 end if;
13071 end Kill_Current_Values;
13073 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
13074 S : Entity_Id;
13076 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
13077 -- Clear current value for entity E and all entities chained to E
13079 ------------------------------------------
13080 -- Kill_Current_Values_For_Entity_Chain --
13081 ------------------------------------------
13083 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
13084 Ent : Entity_Id;
13085 begin
13086 Ent := E;
13087 while Present (Ent) loop
13088 Kill_Current_Values (Ent, Last_Assignment_Only);
13089 Next_Entity (Ent);
13090 end loop;
13091 end Kill_Current_Values_For_Entity_Chain;
13093 -- Start of processing for Kill_Current_Values
13095 begin
13096 -- Kill all saved checks, a special case of killing saved values
13098 if not Last_Assignment_Only then
13099 Kill_All_Checks;
13100 end if;
13102 -- Loop through relevant scopes, which includes the current scope and
13103 -- any parent scopes if the current scope is a block or a package.
13105 S := Current_Scope;
13106 Scope_Loop : loop
13108 -- Clear current values of all entities in current scope
13110 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
13112 -- If scope is a package, also clear current values of all private
13113 -- entities in the scope.
13115 if Is_Package_Or_Generic_Package (S)
13116 or else Is_Concurrent_Type (S)
13117 then
13118 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
13119 end if;
13121 -- If this is a not a subprogram, deal with parents
13123 if not Is_Subprogram (S) then
13124 S := Scope (S);
13125 exit Scope_Loop when S = Standard_Standard;
13126 else
13127 exit Scope_Loop;
13128 end if;
13129 end loop Scope_Loop;
13130 end Kill_Current_Values;
13132 --------------------------
13133 -- Kill_Size_Check_Code --
13134 --------------------------
13136 procedure Kill_Size_Check_Code (E : Entity_Id) is
13137 begin
13138 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
13139 and then Present (Size_Check_Code (E))
13140 then
13141 Remove (Size_Check_Code (E));
13142 Set_Size_Check_Code (E, Empty);
13143 end if;
13144 end Kill_Size_Check_Code;
13146 --------------------------
13147 -- Known_To_Be_Assigned --
13148 --------------------------
13150 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
13151 P : constant Node_Id := Parent (N);
13153 begin
13154 case Nkind (P) is
13156 -- Test left side of assignment
13158 when N_Assignment_Statement =>
13159 return N = Name (P);
13161 -- Function call arguments are never lvalues
13163 when N_Function_Call =>
13164 return False;
13166 -- Positional parameter for procedure or accept call
13168 when N_Procedure_Call_Statement |
13169 N_Accept_Statement
13171 declare
13172 Proc : Entity_Id;
13173 Form : Entity_Id;
13174 Act : Node_Id;
13176 begin
13177 Proc := Get_Subprogram_Entity (P);
13179 if No (Proc) then
13180 return False;
13181 end if;
13183 -- If we are not a list member, something is strange, so
13184 -- be conservative and return False.
13186 if not Is_List_Member (N) then
13187 return False;
13188 end if;
13190 -- We are going to find the right formal by stepping forward
13191 -- through the formals, as we step backwards in the actuals.
13193 Form := First_Formal (Proc);
13194 Act := N;
13195 loop
13196 -- If no formal, something is weird, so be conservative
13197 -- and return False.
13199 if No (Form) then
13200 return False;
13201 end if;
13203 Prev (Act);
13204 exit when No (Act);
13205 Next_Formal (Form);
13206 end loop;
13208 return Ekind (Form) /= E_In_Parameter;
13209 end;
13211 -- Named parameter for procedure or accept call
13213 when N_Parameter_Association =>
13214 declare
13215 Proc : Entity_Id;
13216 Form : Entity_Id;
13218 begin
13219 Proc := Get_Subprogram_Entity (Parent (P));
13221 if No (Proc) then
13222 return False;
13223 end if;
13225 -- Loop through formals to find the one that matches
13227 Form := First_Formal (Proc);
13228 loop
13229 -- If no matching formal, that's peculiar, some kind of
13230 -- previous error, so return False to be conservative.
13231 -- Actually this also happens in legal code in the case
13232 -- where P is a parameter association for an Extra_Formal???
13234 if No (Form) then
13235 return False;
13236 end if;
13238 -- Else test for match
13240 if Chars (Form) = Chars (Selector_Name (P)) then
13241 return Ekind (Form) /= E_In_Parameter;
13242 end if;
13244 Next_Formal (Form);
13245 end loop;
13246 end;
13248 -- Test for appearing in a conversion that itself appears
13249 -- in an lvalue context, since this should be an lvalue.
13251 when N_Type_Conversion =>
13252 return Known_To_Be_Assigned (P);
13254 -- All other references are definitely not known to be modifications
13256 when others =>
13257 return False;
13259 end case;
13260 end Known_To_Be_Assigned;
13262 ---------------------------
13263 -- Last_Source_Statement --
13264 ---------------------------
13266 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
13267 N : Node_Id;
13269 begin
13270 N := Last (Statements (HSS));
13271 while Present (N) loop
13272 exit when Comes_From_Source (N);
13273 Prev (N);
13274 end loop;
13276 return N;
13277 end Last_Source_Statement;
13279 ----------------------------------
13280 -- Matching_Static_Array_Bounds --
13281 ----------------------------------
13283 function Matching_Static_Array_Bounds
13284 (L_Typ : Node_Id;
13285 R_Typ : Node_Id) return Boolean
13287 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
13288 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
13290 L_Index : Node_Id;
13291 R_Index : Node_Id;
13292 L_Low : Node_Id;
13293 L_High : Node_Id;
13294 L_Len : Uint;
13295 R_Low : Node_Id;
13296 R_High : Node_Id;
13297 R_Len : Uint;
13299 begin
13300 if L_Ndims /= R_Ndims then
13301 return False;
13302 end if;
13304 -- Unconstrained types do not have static bounds
13306 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
13307 return False;
13308 end if;
13310 -- First treat specially the first dimension, as the lower bound and
13311 -- length of string literals are not stored like those of arrays.
13313 if Ekind (L_Typ) = E_String_Literal_Subtype then
13314 L_Low := String_Literal_Low_Bound (L_Typ);
13315 L_Len := String_Literal_Length (L_Typ);
13316 else
13317 L_Index := First_Index (L_Typ);
13318 Get_Index_Bounds (L_Index, L_Low, L_High);
13320 if Is_OK_Static_Expression (L_Low)
13321 and then
13322 Is_OK_Static_Expression (L_High)
13323 then
13324 if Expr_Value (L_High) < Expr_Value (L_Low) then
13325 L_Len := Uint_0;
13326 else
13327 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
13328 end if;
13329 else
13330 return False;
13331 end if;
13332 end if;
13334 if Ekind (R_Typ) = E_String_Literal_Subtype then
13335 R_Low := String_Literal_Low_Bound (R_Typ);
13336 R_Len := String_Literal_Length (R_Typ);
13337 else
13338 R_Index := First_Index (R_Typ);
13339 Get_Index_Bounds (R_Index, R_Low, R_High);
13341 if Is_OK_Static_Expression (R_Low)
13342 and then
13343 Is_OK_Static_Expression (R_High)
13344 then
13345 if Expr_Value (R_High) < Expr_Value (R_Low) then
13346 R_Len := Uint_0;
13347 else
13348 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
13349 end if;
13350 else
13351 return False;
13352 end if;
13353 end if;
13355 if (Is_OK_Static_Expression (L_Low)
13356 and then
13357 Is_OK_Static_Expression (R_Low))
13358 and then Expr_Value (L_Low) = Expr_Value (R_Low)
13359 and then L_Len = R_Len
13360 then
13361 null;
13362 else
13363 return False;
13364 end if;
13366 -- Then treat all other dimensions
13368 for Indx in 2 .. L_Ndims loop
13369 Next (L_Index);
13370 Next (R_Index);
13372 Get_Index_Bounds (L_Index, L_Low, L_High);
13373 Get_Index_Bounds (R_Index, R_Low, R_High);
13375 if (Is_OK_Static_Expression (L_Low) and then
13376 Is_OK_Static_Expression (L_High) and then
13377 Is_OK_Static_Expression (R_Low) and then
13378 Is_OK_Static_Expression (R_High))
13379 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
13380 and then
13381 Expr_Value (L_High) = Expr_Value (R_High))
13382 then
13383 null;
13384 else
13385 return False;
13386 end if;
13387 end loop;
13389 -- If we fall through the loop, all indexes matched
13391 return True;
13392 end Matching_Static_Array_Bounds;
13394 -------------------
13395 -- May_Be_Lvalue --
13396 -------------------
13398 function May_Be_Lvalue (N : Node_Id) return Boolean is
13399 P : constant Node_Id := Parent (N);
13401 begin
13402 case Nkind (P) is
13404 -- Test left side of assignment
13406 when N_Assignment_Statement =>
13407 return N = Name (P);
13409 -- Test prefix of component or attribute. Note that the prefix of an
13410 -- explicit or implicit dereference cannot be an l-value.
13412 when N_Attribute_Reference =>
13413 return N = Prefix (P)
13414 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
13416 -- For an expanded name, the name is an lvalue if the expanded name
13417 -- is an lvalue, but the prefix is never an lvalue, since it is just
13418 -- the scope where the name is found.
13420 when N_Expanded_Name =>
13421 if N = Prefix (P) then
13422 return May_Be_Lvalue (P);
13423 else
13424 return False;
13425 end if;
13427 -- For a selected component A.B, A is certainly an lvalue if A.B is.
13428 -- B is a little interesting, if we have A.B := 3, there is some
13429 -- discussion as to whether B is an lvalue or not, we choose to say
13430 -- it is. Note however that A is not an lvalue if it is of an access
13431 -- type since this is an implicit dereference.
13433 when N_Selected_Component =>
13434 if N = Prefix (P)
13435 and then Present (Etype (N))
13436 and then Is_Access_Type (Etype (N))
13437 then
13438 return False;
13439 else
13440 return May_Be_Lvalue (P);
13441 end if;
13443 -- For an indexed component or slice, the index or slice bounds is
13444 -- never an lvalue. The prefix is an lvalue if the indexed component
13445 -- or slice is an lvalue, except if it is an access type, where we
13446 -- have an implicit dereference.
13448 when N_Indexed_Component | N_Slice =>
13449 if N /= Prefix (P)
13450 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
13451 then
13452 return False;
13453 else
13454 return May_Be_Lvalue (P);
13455 end if;
13457 -- Prefix of a reference is an lvalue if the reference is an lvalue
13459 when N_Reference =>
13460 return May_Be_Lvalue (P);
13462 -- Prefix of explicit dereference is never an lvalue
13464 when N_Explicit_Dereference =>
13465 return False;
13467 -- Positional parameter for subprogram, entry, or accept call.
13468 -- In older versions of Ada function call arguments are never
13469 -- lvalues. In Ada 2012 functions can have in-out parameters.
13471 when N_Subprogram_Call |
13472 N_Entry_Call_Statement |
13473 N_Accept_Statement
13475 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
13476 return False;
13477 end if;
13479 -- The following mechanism is clumsy and fragile. A single flag
13480 -- set in Resolve_Actuals would be preferable ???
13482 declare
13483 Proc : Entity_Id;
13484 Form : Entity_Id;
13485 Act : Node_Id;
13487 begin
13488 Proc := Get_Subprogram_Entity (P);
13490 if No (Proc) then
13491 return True;
13492 end if;
13494 -- If we are not a list member, something is strange, so be
13495 -- conservative and return True.
13497 if not Is_List_Member (N) then
13498 return True;
13499 end if;
13501 -- We are going to find the right formal by stepping forward
13502 -- through the formals, as we step backwards in the actuals.
13504 Form := First_Formal (Proc);
13505 Act := N;
13506 loop
13507 -- If no formal, something is weird, so be conservative and
13508 -- return True.
13510 if No (Form) then
13511 return True;
13512 end if;
13514 Prev (Act);
13515 exit when No (Act);
13516 Next_Formal (Form);
13517 end loop;
13519 return Ekind (Form) /= E_In_Parameter;
13520 end;
13522 -- Named parameter for procedure or accept call
13524 when N_Parameter_Association =>
13525 declare
13526 Proc : Entity_Id;
13527 Form : Entity_Id;
13529 begin
13530 Proc := Get_Subprogram_Entity (Parent (P));
13532 if No (Proc) then
13533 return True;
13534 end if;
13536 -- Loop through formals to find the one that matches
13538 Form := First_Formal (Proc);
13539 loop
13540 -- If no matching formal, that's peculiar, some kind of
13541 -- previous error, so return True to be conservative.
13542 -- Actually happens with legal code for an unresolved call
13543 -- where we may get the wrong homonym???
13545 if No (Form) then
13546 return True;
13547 end if;
13549 -- Else test for match
13551 if Chars (Form) = Chars (Selector_Name (P)) then
13552 return Ekind (Form) /= E_In_Parameter;
13553 end if;
13555 Next_Formal (Form);
13556 end loop;
13557 end;
13559 -- Test for appearing in a conversion that itself appears in an
13560 -- lvalue context, since this should be an lvalue.
13562 when N_Type_Conversion =>
13563 return May_Be_Lvalue (P);
13565 -- Test for appearance in object renaming declaration
13567 when N_Object_Renaming_Declaration =>
13568 return True;
13570 -- All other references are definitely not lvalues
13572 when others =>
13573 return False;
13575 end case;
13576 end May_Be_Lvalue;
13578 -----------------------
13579 -- Mark_Coextensions --
13580 -----------------------
13582 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
13583 Is_Dynamic : Boolean;
13584 -- Indicates whether the context causes nested coextensions to be
13585 -- dynamic or static
13587 function Mark_Allocator (N : Node_Id) return Traverse_Result;
13588 -- Recognize an allocator node and label it as a dynamic coextension
13590 --------------------
13591 -- Mark_Allocator --
13592 --------------------
13594 function Mark_Allocator (N : Node_Id) return Traverse_Result is
13595 begin
13596 if Nkind (N) = N_Allocator then
13597 if Is_Dynamic then
13598 Set_Is_Dynamic_Coextension (N);
13600 -- If the allocator expression is potentially dynamic, it may
13601 -- be expanded out of order and require dynamic allocation
13602 -- anyway, so we treat the coextension itself as dynamic.
13603 -- Potential optimization ???
13605 elsif Nkind (Expression (N)) = N_Qualified_Expression
13606 and then Nkind (Expression (Expression (N))) = N_Op_Concat
13607 then
13608 Set_Is_Dynamic_Coextension (N);
13609 else
13610 Set_Is_Static_Coextension (N);
13611 end if;
13612 end if;
13614 return OK;
13615 end Mark_Allocator;
13617 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
13619 -- Start of processing Mark_Coextensions
13621 begin
13622 case Nkind (Context_Nod) is
13624 -- Comment here ???
13626 when N_Assignment_Statement =>
13627 Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
13629 -- An allocator that is a component of a returned aggregate
13630 -- must be dynamic.
13632 when N_Simple_Return_Statement =>
13633 declare
13634 Expr : constant Node_Id := Expression (Context_Nod);
13635 begin
13636 Is_Dynamic :=
13637 Nkind (Expr) = N_Allocator
13638 or else
13639 (Nkind (Expr) = N_Qualified_Expression
13640 and then Nkind (Expression (Expr)) = N_Aggregate);
13641 end;
13643 -- An alloctor within an object declaration in an extended return
13644 -- statement is of necessity dynamic.
13646 when N_Object_Declaration =>
13647 Is_Dynamic := Nkind (Root_Nod) = N_Allocator
13648 or else
13649 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
13651 -- This routine should not be called for constructs which may not
13652 -- contain coextensions.
13654 when others =>
13655 raise Program_Error;
13656 end case;
13658 Mark_Allocators (Root_Nod);
13659 end Mark_Coextensions;
13661 ----------------------
13662 -- Needs_One_Actual --
13663 ----------------------
13665 function Needs_One_Actual (E : Entity_Id) return Boolean is
13666 Formal : Entity_Id;
13668 begin
13669 -- Ada 2005 or later, and formals present
13671 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
13672 Formal := Next_Formal (First_Formal (E));
13673 while Present (Formal) loop
13674 if No (Default_Value (Formal)) then
13675 return False;
13676 end if;
13678 Next_Formal (Formal);
13679 end loop;
13681 return True;
13683 -- Ada 83/95 or no formals
13685 else
13686 return False;
13687 end if;
13688 end Needs_One_Actual;
13690 ------------------------
13691 -- New_Copy_List_Tree --
13692 ------------------------
13694 function New_Copy_List_Tree (List : List_Id) return List_Id is
13695 NL : List_Id;
13696 E : Node_Id;
13698 begin
13699 if List = No_List then
13700 return No_List;
13702 else
13703 NL := New_List;
13704 E := First (List);
13706 while Present (E) loop
13707 Append (New_Copy_Tree (E), NL);
13708 E := Next (E);
13709 end loop;
13711 return NL;
13712 end if;
13713 end New_Copy_List_Tree;
13715 --------------------------------------------------
13716 -- New_Copy_Tree Auxiliary Data and Subprograms --
13717 --------------------------------------------------
13719 use Atree.Unchecked_Access;
13720 use Atree_Private_Part;
13722 -- Our approach here requires a two pass traversal of the tree. The
13723 -- first pass visits all nodes that eventually will be copied looking
13724 -- for defining Itypes. If any defining Itypes are found, then they are
13725 -- copied, and an entry is added to the replacement map. In the second
13726 -- phase, the tree is copied, using the replacement map to replace any
13727 -- Itype references within the copied tree.
13729 -- The following hash tables are used if the Map supplied has more
13730 -- than hash threshold entries to speed up access to the map. If
13731 -- there are fewer entries, then the map is searched sequentially
13732 -- (because setting up a hash table for only a few entries takes
13733 -- more time than it saves.
13735 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
13736 -- Hash function used for hash operations
13738 -------------------
13739 -- New_Copy_Hash --
13740 -------------------
13742 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
13743 begin
13744 return Nat (E) mod (NCT_Header_Num'Last + 1);
13745 end New_Copy_Hash;
13747 ---------------
13748 -- NCT_Assoc --
13749 ---------------
13751 -- The hash table NCT_Assoc associates old entities in the table
13752 -- with their corresponding new entities (i.e. the pairs of entries
13753 -- presented in the original Map argument are Key-Element pairs).
13755 package NCT_Assoc is new Simple_HTable (
13756 Header_Num => NCT_Header_Num,
13757 Element => Entity_Id,
13758 No_Element => Empty,
13759 Key => Entity_Id,
13760 Hash => New_Copy_Hash,
13761 Equal => Types."=");
13763 ---------------------
13764 -- NCT_Itype_Assoc --
13765 ---------------------
13767 -- The hash table NCT_Itype_Assoc contains entries only for those
13768 -- old nodes which have a non-empty Associated_Node_For_Itype set.
13769 -- The key is the associated node, and the element is the new node
13770 -- itself (NOT the associated node for the new node).
13772 package NCT_Itype_Assoc is new Simple_HTable (
13773 Header_Num => NCT_Header_Num,
13774 Element => Entity_Id,
13775 No_Element => Empty,
13776 Key => Entity_Id,
13777 Hash => New_Copy_Hash,
13778 Equal => Types."=");
13780 -------------------
13781 -- New_Copy_Tree --
13782 -------------------
13784 function New_Copy_Tree
13785 (Source : Node_Id;
13786 Map : Elist_Id := No_Elist;
13787 New_Sloc : Source_Ptr := No_Location;
13788 New_Scope : Entity_Id := Empty) return Node_Id
13790 Actual_Map : Elist_Id := Map;
13791 -- This is the actual map for the copy. It is initialized with the
13792 -- given elements, and then enlarged as required for Itypes that are
13793 -- copied during the first phase of the copy operation. The visit
13794 -- procedures add elements to this map as Itypes are encountered.
13795 -- The reason we cannot use Map directly, is that it may well be
13796 -- (and normally is) initialized to No_Elist, and if we have mapped
13797 -- entities, we have to reset it to point to a real Elist.
13799 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
13800 -- Called during second phase to map entities into their corresponding
13801 -- copies using Actual_Map. If the argument is not an entity, or is not
13802 -- in Actual_Map, then it is returned unchanged.
13804 procedure Build_NCT_Hash_Tables;
13805 -- Builds hash tables (number of elements >= threshold value)
13807 function Copy_Elist_With_Replacement
13808 (Old_Elist : Elist_Id) return Elist_Id;
13809 -- Called during second phase to copy element list doing replacements
13811 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
13812 -- Called during the second phase to process a copied Itype. The actual
13813 -- copy happened during the first phase (so that we could make the entry
13814 -- in the mapping), but we still have to deal with the descendents of
13815 -- the copied Itype and copy them where necessary.
13817 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
13818 -- Called during second phase to copy list doing replacements
13820 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
13821 -- Called during second phase to copy node doing replacements
13823 procedure Visit_Elist (E : Elist_Id);
13824 -- Called during first phase to visit all elements of an Elist
13826 procedure Visit_Field (F : Union_Id; N : Node_Id);
13827 -- Visit a single field, recursing to call Visit_Node or Visit_List
13828 -- if the field is a syntactic descendent of the current node (i.e.
13829 -- its parent is Node N).
13831 procedure Visit_Itype (Old_Itype : Entity_Id);
13832 -- Called during first phase to visit subsidiary fields of a defining
13833 -- Itype, and also create a copy and make an entry in the replacement
13834 -- map for the new copy.
13836 procedure Visit_List (L : List_Id);
13837 -- Called during first phase to visit all elements of a List
13839 procedure Visit_Node (N : Node_Or_Entity_Id);
13840 -- Called during first phase to visit a node and all its subtrees
13842 -----------
13843 -- Assoc --
13844 -----------
13846 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
13847 E : Elmt_Id;
13848 Ent : Entity_Id;
13850 begin
13851 if not Has_Extension (N) or else No (Actual_Map) then
13852 return N;
13854 elsif NCT_Hash_Tables_Used then
13855 Ent := NCT_Assoc.Get (Entity_Id (N));
13857 if Present (Ent) then
13858 return Ent;
13859 else
13860 return N;
13861 end if;
13863 -- No hash table used, do serial search
13865 else
13866 E := First_Elmt (Actual_Map);
13867 while Present (E) loop
13868 if Node (E) = N then
13869 return Node (Next_Elmt (E));
13870 else
13871 E := Next_Elmt (Next_Elmt (E));
13872 end if;
13873 end loop;
13874 end if;
13876 return N;
13877 end Assoc;
13879 ---------------------------
13880 -- Build_NCT_Hash_Tables --
13881 ---------------------------
13883 procedure Build_NCT_Hash_Tables is
13884 Elmt : Elmt_Id;
13885 Ent : Entity_Id;
13886 begin
13887 if NCT_Hash_Table_Setup then
13888 NCT_Assoc.Reset;
13889 NCT_Itype_Assoc.Reset;
13890 end if;
13892 Elmt := First_Elmt (Actual_Map);
13893 while Present (Elmt) loop
13894 Ent := Node (Elmt);
13896 -- Get new entity, and associate old and new
13898 Next_Elmt (Elmt);
13899 NCT_Assoc.Set (Ent, Node (Elmt));
13901 if Is_Type (Ent) then
13902 declare
13903 Anode : constant Entity_Id :=
13904 Associated_Node_For_Itype (Ent);
13906 begin
13907 if Present (Anode) then
13909 -- Enter a link between the associated node of the
13910 -- old Itype and the new Itype, for updating later
13911 -- when node is copied.
13913 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
13914 end if;
13915 end;
13916 end if;
13918 Next_Elmt (Elmt);
13919 end loop;
13921 NCT_Hash_Tables_Used := True;
13922 NCT_Hash_Table_Setup := True;
13923 end Build_NCT_Hash_Tables;
13925 ---------------------------------
13926 -- Copy_Elist_With_Replacement --
13927 ---------------------------------
13929 function Copy_Elist_With_Replacement
13930 (Old_Elist : Elist_Id) return Elist_Id
13932 M : Elmt_Id;
13933 New_Elist : Elist_Id;
13935 begin
13936 if No (Old_Elist) then
13937 return No_Elist;
13939 else
13940 New_Elist := New_Elmt_List;
13942 M := First_Elmt (Old_Elist);
13943 while Present (M) loop
13944 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
13945 Next_Elmt (M);
13946 end loop;
13947 end if;
13949 return New_Elist;
13950 end Copy_Elist_With_Replacement;
13952 ---------------------------------
13953 -- Copy_Itype_With_Replacement --
13954 ---------------------------------
13956 -- This routine exactly parallels its phase one analog Visit_Itype,
13958 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
13959 begin
13960 -- Translate Next_Entity, Scope and Etype fields, in case they
13961 -- reference entities that have been mapped into copies.
13963 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
13964 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
13966 if Present (New_Scope) then
13967 Set_Scope (New_Itype, New_Scope);
13968 else
13969 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
13970 end if;
13972 -- Copy referenced fields
13974 if Is_Discrete_Type (New_Itype) then
13975 Set_Scalar_Range (New_Itype,
13976 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
13978 elsif Has_Discriminants (Base_Type (New_Itype)) then
13979 Set_Discriminant_Constraint (New_Itype,
13980 Copy_Elist_With_Replacement
13981 (Discriminant_Constraint (New_Itype)));
13983 elsif Is_Array_Type (New_Itype) then
13984 if Present (First_Index (New_Itype)) then
13985 Set_First_Index (New_Itype,
13986 First (Copy_List_With_Replacement
13987 (List_Containing (First_Index (New_Itype)))));
13988 end if;
13990 if Is_Packed (New_Itype) then
13991 Set_Packed_Array_Impl_Type (New_Itype,
13992 Copy_Node_With_Replacement
13993 (Packed_Array_Impl_Type (New_Itype)));
13994 end if;
13995 end if;
13996 end Copy_Itype_With_Replacement;
13998 --------------------------------
13999 -- Copy_List_With_Replacement --
14000 --------------------------------
14002 function Copy_List_With_Replacement
14003 (Old_List : List_Id) return List_Id
14005 New_List : List_Id;
14006 E : Node_Id;
14008 begin
14009 if Old_List = No_List then
14010 return No_List;
14012 else
14013 New_List := Empty_List;
14015 E := First (Old_List);
14016 while Present (E) loop
14017 Append (Copy_Node_With_Replacement (E), New_List);
14018 Next (E);
14019 end loop;
14021 return New_List;
14022 end if;
14023 end Copy_List_With_Replacement;
14025 --------------------------------
14026 -- Copy_Node_With_Replacement --
14027 --------------------------------
14029 function Copy_Node_With_Replacement
14030 (Old_Node : Node_Id) return Node_Id
14032 New_Node : Node_Id;
14034 procedure Adjust_Named_Associations
14035 (Old_Node : Node_Id;
14036 New_Node : Node_Id);
14037 -- If a call node has named associations, these are chained through
14038 -- the First_Named_Actual, Next_Named_Actual links. These must be
14039 -- propagated separately to the new parameter list, because these
14040 -- are not syntactic fields.
14042 function Copy_Field_With_Replacement
14043 (Field : Union_Id) return Union_Id;
14044 -- Given Field, which is a field of Old_Node, return a copy of it
14045 -- if it is a syntactic field (i.e. its parent is Node), setting
14046 -- the parent of the copy to poit to New_Node. Otherwise returns
14047 -- the field (possibly mapped if it is an entity).
14049 -------------------------------
14050 -- Adjust_Named_Associations --
14051 -------------------------------
14053 procedure Adjust_Named_Associations
14054 (Old_Node : Node_Id;
14055 New_Node : Node_Id)
14057 Old_E : Node_Id;
14058 New_E : Node_Id;
14060 Old_Next : Node_Id;
14061 New_Next : Node_Id;
14063 begin
14064 Old_E := First (Parameter_Associations (Old_Node));
14065 New_E := First (Parameter_Associations (New_Node));
14066 while Present (Old_E) loop
14067 if Nkind (Old_E) = N_Parameter_Association
14068 and then Present (Next_Named_Actual (Old_E))
14069 then
14070 if First_Named_Actual (Old_Node)
14071 = Explicit_Actual_Parameter (Old_E)
14072 then
14073 Set_First_Named_Actual
14074 (New_Node, Explicit_Actual_Parameter (New_E));
14075 end if;
14077 -- Now scan parameter list from the beginning,to locate
14078 -- next named actual, which can be out of order.
14080 Old_Next := First (Parameter_Associations (Old_Node));
14081 New_Next := First (Parameter_Associations (New_Node));
14083 while Nkind (Old_Next) /= N_Parameter_Association
14084 or else Explicit_Actual_Parameter (Old_Next)
14085 /= Next_Named_Actual (Old_E)
14086 loop
14087 Next (Old_Next);
14088 Next (New_Next);
14089 end loop;
14091 Set_Next_Named_Actual
14092 (New_E, Explicit_Actual_Parameter (New_Next));
14093 end if;
14095 Next (Old_E);
14096 Next (New_E);
14097 end loop;
14098 end Adjust_Named_Associations;
14100 ---------------------------------
14101 -- Copy_Field_With_Replacement --
14102 ---------------------------------
14104 function Copy_Field_With_Replacement
14105 (Field : Union_Id) return Union_Id
14107 begin
14108 if Field = Union_Id (Empty) then
14109 return Field;
14111 elsif Field in Node_Range then
14112 declare
14113 Old_N : constant Node_Id := Node_Id (Field);
14114 New_N : Node_Id;
14116 begin
14117 -- If syntactic field, as indicated by the parent pointer
14118 -- being set, then copy the referenced node recursively.
14120 if Parent (Old_N) = Old_Node then
14121 New_N := Copy_Node_With_Replacement (Old_N);
14123 if New_N /= Old_N then
14124 Set_Parent (New_N, New_Node);
14125 end if;
14127 -- For semantic fields, update possible entity reference
14128 -- from the replacement map.
14130 else
14131 New_N := Assoc (Old_N);
14132 end if;
14134 return Union_Id (New_N);
14135 end;
14137 elsif Field in List_Range then
14138 declare
14139 Old_L : constant List_Id := List_Id (Field);
14140 New_L : List_Id;
14142 begin
14143 -- If syntactic field, as indicated by the parent pointer,
14144 -- then recursively copy the entire referenced list.
14146 if Parent (Old_L) = Old_Node then
14147 New_L := Copy_List_With_Replacement (Old_L);
14148 Set_Parent (New_L, New_Node);
14150 -- For semantic list, just returned unchanged
14152 else
14153 New_L := Old_L;
14154 end if;
14156 return Union_Id (New_L);
14157 end;
14159 -- Anything other than a list or a node is returned unchanged
14161 else
14162 return Field;
14163 end if;
14164 end Copy_Field_With_Replacement;
14166 -- Start of processing for Copy_Node_With_Replacement
14168 begin
14169 if Old_Node <= Empty_Or_Error then
14170 return Old_Node;
14172 elsif Has_Extension (Old_Node) then
14173 return Assoc (Old_Node);
14175 else
14176 New_Node := New_Copy (Old_Node);
14178 -- If the node we are copying is the associated node of a
14179 -- previously copied Itype, then adjust the associated node
14180 -- of the copy of that Itype accordingly.
14182 if Present (Actual_Map) then
14183 declare
14184 E : Elmt_Id;
14185 Ent : Entity_Id;
14187 begin
14188 -- Case of hash table used
14190 if NCT_Hash_Tables_Used then
14191 Ent := NCT_Itype_Assoc.Get (Old_Node);
14193 if Present (Ent) then
14194 Set_Associated_Node_For_Itype (Ent, New_Node);
14195 end if;
14197 -- Case of no hash table used
14199 else
14200 E := First_Elmt (Actual_Map);
14201 while Present (E) loop
14202 if Is_Itype (Node (E))
14203 and then
14204 Old_Node = Associated_Node_For_Itype (Node (E))
14205 then
14206 Set_Associated_Node_For_Itype
14207 (Node (Next_Elmt (E)), New_Node);
14208 end if;
14210 E := Next_Elmt (Next_Elmt (E));
14211 end loop;
14212 end if;
14213 end;
14214 end if;
14216 -- Recursively copy descendents
14218 Set_Field1
14219 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
14220 Set_Field2
14221 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
14222 Set_Field3
14223 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
14224 Set_Field4
14225 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
14226 Set_Field5
14227 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
14229 -- Adjust Sloc of new node if necessary
14231 if New_Sloc /= No_Location then
14232 Set_Sloc (New_Node, New_Sloc);
14234 -- If we adjust the Sloc, then we are essentially making
14235 -- a completely new node, so the Comes_From_Source flag
14236 -- should be reset to the proper default value.
14238 Nodes.Table (New_Node).Comes_From_Source :=
14239 Default_Node.Comes_From_Source;
14240 end if;
14242 -- If the node is call and has named associations,
14243 -- set the corresponding links in the copy.
14245 if (Nkind (Old_Node) = N_Function_Call
14246 or else Nkind (Old_Node) = N_Entry_Call_Statement
14247 or else
14248 Nkind (Old_Node) = N_Procedure_Call_Statement)
14249 and then Present (First_Named_Actual (Old_Node))
14250 then
14251 Adjust_Named_Associations (Old_Node, New_Node);
14252 end if;
14254 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
14255 -- The replacement mechanism applies to entities, and is not used
14256 -- here. Eventually we may need a more general graph-copying
14257 -- routine. For now, do a sequential search to find desired node.
14259 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
14260 and then Present (First_Real_Statement (Old_Node))
14261 then
14262 declare
14263 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
14264 N1, N2 : Node_Id;
14266 begin
14267 N1 := First (Statements (Old_Node));
14268 N2 := First (Statements (New_Node));
14270 while N1 /= Old_F loop
14271 Next (N1);
14272 Next (N2);
14273 end loop;
14275 Set_First_Real_Statement (New_Node, N2);
14276 end;
14277 end if;
14278 end if;
14280 -- All done, return copied node
14282 return New_Node;
14283 end Copy_Node_With_Replacement;
14285 -----------------
14286 -- Visit_Elist --
14287 -----------------
14289 procedure Visit_Elist (E : Elist_Id) is
14290 Elmt : Elmt_Id;
14291 begin
14292 if Present (E) then
14293 Elmt := First_Elmt (E);
14295 while Elmt /= No_Elmt loop
14296 Visit_Node (Node (Elmt));
14297 Next_Elmt (Elmt);
14298 end loop;
14299 end if;
14300 end Visit_Elist;
14302 -----------------
14303 -- Visit_Field --
14304 -----------------
14306 procedure Visit_Field (F : Union_Id; N : Node_Id) is
14307 begin
14308 if F = Union_Id (Empty) then
14309 return;
14311 elsif F in Node_Range then
14313 -- Copy node if it is syntactic, i.e. its parent pointer is
14314 -- set to point to the field that referenced it (certain
14315 -- Itypes will also meet this criterion, which is fine, since
14316 -- these are clearly Itypes that do need to be copied, since
14317 -- we are copying their parent.)
14319 if Parent (Node_Id (F)) = N then
14320 Visit_Node (Node_Id (F));
14321 return;
14323 -- Another case, if we are pointing to an Itype, then we want
14324 -- to copy it if its associated node is somewhere in the tree
14325 -- being copied.
14327 -- Note: the exclusion of self-referential copies is just an
14328 -- optimization, since the search of the already copied list
14329 -- would catch it, but it is a common case (Etype pointing
14330 -- to itself for an Itype that is a base type).
14332 elsif Has_Extension (Node_Id (F))
14333 and then Is_Itype (Entity_Id (F))
14334 and then Node_Id (F) /= N
14335 then
14336 declare
14337 P : Node_Id;
14339 begin
14340 P := Associated_Node_For_Itype (Node_Id (F));
14341 while Present (P) loop
14342 if P = Source then
14343 Visit_Node (Node_Id (F));
14344 return;
14345 else
14346 P := Parent (P);
14347 end if;
14348 end loop;
14350 -- An Itype whose parent is not being copied definitely
14351 -- should NOT be copied, since it does not belong in any
14352 -- sense to the copied subtree.
14354 return;
14355 end;
14356 end if;
14358 elsif F in List_Range and then Parent (List_Id (F)) = N then
14359 Visit_List (List_Id (F));
14360 return;
14361 end if;
14362 end Visit_Field;
14364 -----------------
14365 -- Visit_Itype --
14366 -----------------
14368 procedure Visit_Itype (Old_Itype : Entity_Id) is
14369 New_Itype : Entity_Id;
14370 E : Elmt_Id;
14371 Ent : Entity_Id;
14373 begin
14374 -- Itypes that describe the designated type of access to subprograms
14375 -- have the structure of subprogram declarations, with signatures,
14376 -- etc. Either we duplicate the signatures completely, or choose to
14377 -- share such itypes, which is fine because their elaboration will
14378 -- have no side effects.
14380 if Ekind (Old_Itype) = E_Subprogram_Type then
14381 return;
14382 end if;
14384 New_Itype := New_Copy (Old_Itype);
14386 -- The new Itype has all the attributes of the old one, and
14387 -- we just copy the contents of the entity. However, the back-end
14388 -- needs different names for debugging purposes, so we create a
14389 -- new internal name for it in all cases.
14391 Set_Chars (New_Itype, New_Internal_Name ('T'));
14393 -- If our associated node is an entity that has already been copied,
14394 -- then set the associated node of the copy to point to the right
14395 -- copy. If we have copied an Itype that is itself the associated
14396 -- node of some previously copied Itype, then we set the right
14397 -- pointer in the other direction.
14399 if Present (Actual_Map) then
14401 -- Case of hash tables used
14403 if NCT_Hash_Tables_Used then
14405 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
14407 if Present (Ent) then
14408 Set_Associated_Node_For_Itype (New_Itype, Ent);
14409 end if;
14411 Ent := NCT_Itype_Assoc.Get (Old_Itype);
14412 if Present (Ent) then
14413 Set_Associated_Node_For_Itype (Ent, New_Itype);
14415 -- If the hash table has no association for this Itype and
14416 -- its associated node, enter one now.
14418 else
14419 NCT_Itype_Assoc.Set
14420 (Associated_Node_For_Itype (Old_Itype), New_Itype);
14421 end if;
14423 -- Case of hash tables not used
14425 else
14426 E := First_Elmt (Actual_Map);
14427 while Present (E) loop
14428 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
14429 Set_Associated_Node_For_Itype
14430 (New_Itype, Node (Next_Elmt (E)));
14431 end if;
14433 if Is_Type (Node (E))
14434 and then Old_Itype = Associated_Node_For_Itype (Node (E))
14435 then
14436 Set_Associated_Node_For_Itype
14437 (Node (Next_Elmt (E)), New_Itype);
14438 end if;
14440 E := Next_Elmt (Next_Elmt (E));
14441 end loop;
14442 end if;
14443 end if;
14445 if Present (Freeze_Node (New_Itype)) then
14446 Set_Is_Frozen (New_Itype, False);
14447 Set_Freeze_Node (New_Itype, Empty);
14448 end if;
14450 -- Add new association to map
14452 if No (Actual_Map) then
14453 Actual_Map := New_Elmt_List;
14454 end if;
14456 Append_Elmt (Old_Itype, Actual_Map);
14457 Append_Elmt (New_Itype, Actual_Map);
14459 if NCT_Hash_Tables_Used then
14460 NCT_Assoc.Set (Old_Itype, New_Itype);
14462 else
14463 NCT_Table_Entries := NCT_Table_Entries + 1;
14465 if NCT_Table_Entries > NCT_Hash_Threshold then
14466 Build_NCT_Hash_Tables;
14467 end if;
14468 end if;
14470 -- If a record subtype is simply copied, the entity list will be
14471 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
14473 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
14474 Set_Cloned_Subtype (New_Itype, Old_Itype);
14475 end if;
14477 -- Visit descendents that eventually get copied
14479 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
14481 if Is_Discrete_Type (Old_Itype) then
14482 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
14484 elsif Has_Discriminants (Base_Type (Old_Itype)) then
14485 -- ??? This should involve call to Visit_Field
14486 Visit_Elist (Discriminant_Constraint (Old_Itype));
14488 elsif Is_Array_Type (Old_Itype) then
14489 if Present (First_Index (Old_Itype)) then
14490 Visit_Field (Union_Id (List_Containing
14491 (First_Index (Old_Itype))),
14492 Old_Itype);
14493 end if;
14495 if Is_Packed (Old_Itype) then
14496 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
14497 Old_Itype);
14498 end if;
14499 end if;
14500 end Visit_Itype;
14502 ----------------
14503 -- Visit_List --
14504 ----------------
14506 procedure Visit_List (L : List_Id) is
14507 N : Node_Id;
14508 begin
14509 if L /= No_List then
14510 N := First (L);
14512 while Present (N) loop
14513 Visit_Node (N);
14514 Next (N);
14515 end loop;
14516 end if;
14517 end Visit_List;
14519 ----------------
14520 -- Visit_Node --
14521 ----------------
14523 procedure Visit_Node (N : Node_Or_Entity_Id) is
14525 -- Start of processing for Visit_Node
14527 begin
14528 -- Handle case of an Itype, which must be copied
14530 if Has_Extension (N) and then Is_Itype (N) then
14532 -- Nothing to do if already in the list. This can happen with an
14533 -- Itype entity that appears more than once in the tree.
14534 -- Note that we do not want to visit descendents in this case.
14536 -- Test for already in list when hash table is used
14538 if NCT_Hash_Tables_Used then
14539 if Present (NCT_Assoc.Get (Entity_Id (N))) then
14540 return;
14541 end if;
14543 -- Test for already in list when hash table not used
14545 else
14546 declare
14547 E : Elmt_Id;
14548 begin
14549 if Present (Actual_Map) then
14550 E := First_Elmt (Actual_Map);
14551 while Present (E) loop
14552 if Node (E) = N then
14553 return;
14554 else
14555 E := Next_Elmt (Next_Elmt (E));
14556 end if;
14557 end loop;
14558 end if;
14559 end;
14560 end if;
14562 Visit_Itype (N);
14563 end if;
14565 -- Visit descendents
14567 Visit_Field (Field1 (N), N);
14568 Visit_Field (Field2 (N), N);
14569 Visit_Field (Field3 (N), N);
14570 Visit_Field (Field4 (N), N);
14571 Visit_Field (Field5 (N), N);
14572 end Visit_Node;
14574 -- Start of processing for New_Copy_Tree
14576 begin
14577 Actual_Map := Map;
14579 -- See if we should use hash table
14581 if No (Actual_Map) then
14582 NCT_Hash_Tables_Used := False;
14584 else
14585 declare
14586 Elmt : Elmt_Id;
14588 begin
14589 NCT_Table_Entries := 0;
14591 Elmt := First_Elmt (Actual_Map);
14592 while Present (Elmt) loop
14593 NCT_Table_Entries := NCT_Table_Entries + 1;
14594 Next_Elmt (Elmt);
14595 Next_Elmt (Elmt);
14596 end loop;
14598 if NCT_Table_Entries > NCT_Hash_Threshold then
14599 Build_NCT_Hash_Tables;
14600 else
14601 NCT_Hash_Tables_Used := False;
14602 end if;
14603 end;
14604 end if;
14606 -- Hash table set up if required, now start phase one by visiting
14607 -- top node (we will recursively visit the descendents).
14609 Visit_Node (Source);
14611 -- Now the second phase of the copy can start. First we process
14612 -- all the mapped entities, copying their descendents.
14614 if Present (Actual_Map) then
14615 declare
14616 Elmt : Elmt_Id;
14617 New_Itype : Entity_Id;
14618 begin
14619 Elmt := First_Elmt (Actual_Map);
14620 while Present (Elmt) loop
14621 Next_Elmt (Elmt);
14622 New_Itype := Node (Elmt);
14623 Copy_Itype_With_Replacement (New_Itype);
14624 Next_Elmt (Elmt);
14625 end loop;
14626 end;
14627 end if;
14629 -- Now we can copy the actual tree
14631 return Copy_Node_With_Replacement (Source);
14632 end New_Copy_Tree;
14634 -------------------------
14635 -- New_External_Entity --
14636 -------------------------
14638 function New_External_Entity
14639 (Kind : Entity_Kind;
14640 Scope_Id : Entity_Id;
14641 Sloc_Value : Source_Ptr;
14642 Related_Id : Entity_Id;
14643 Suffix : Character;
14644 Suffix_Index : Nat := 0;
14645 Prefix : Character := ' ') return Entity_Id
14647 N : constant Entity_Id :=
14648 Make_Defining_Identifier (Sloc_Value,
14649 New_External_Name
14650 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
14652 begin
14653 Set_Ekind (N, Kind);
14654 Set_Is_Internal (N, True);
14655 Append_Entity (N, Scope_Id);
14656 Set_Public_Status (N);
14658 if Kind in Type_Kind then
14659 Init_Size_Align (N);
14660 end if;
14662 return N;
14663 end New_External_Entity;
14665 -------------------------
14666 -- New_Internal_Entity --
14667 -------------------------
14669 function New_Internal_Entity
14670 (Kind : Entity_Kind;
14671 Scope_Id : Entity_Id;
14672 Sloc_Value : Source_Ptr;
14673 Id_Char : Character) return Entity_Id
14675 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
14677 begin
14678 Set_Ekind (N, Kind);
14679 Set_Is_Internal (N, True);
14680 Append_Entity (N, Scope_Id);
14682 if Kind in Type_Kind then
14683 Init_Size_Align (N);
14684 end if;
14686 return N;
14687 end New_Internal_Entity;
14689 -----------------
14690 -- Next_Actual --
14691 -----------------
14693 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
14694 N : Node_Id;
14696 begin
14697 -- If we are pointing at a positional parameter, it is a member of a
14698 -- node list (the list of parameters), and the next parameter is the
14699 -- next node on the list, unless we hit a parameter association, then
14700 -- we shift to using the chain whose head is the First_Named_Actual in
14701 -- the parent, and then is threaded using the Next_Named_Actual of the
14702 -- Parameter_Association. All this fiddling is because the original node
14703 -- list is in the textual call order, and what we need is the
14704 -- declaration order.
14706 if Is_List_Member (Actual_Id) then
14707 N := Next (Actual_Id);
14709 if Nkind (N) = N_Parameter_Association then
14710 return First_Named_Actual (Parent (Actual_Id));
14711 else
14712 return N;
14713 end if;
14715 else
14716 return Next_Named_Actual (Parent (Actual_Id));
14717 end if;
14718 end Next_Actual;
14720 procedure Next_Actual (Actual_Id : in out Node_Id) is
14721 begin
14722 Actual_Id := Next_Actual (Actual_Id);
14723 end Next_Actual;
14725 -----------------------
14726 -- Normalize_Actuals --
14727 -----------------------
14729 -- Chain actuals according to formals of subprogram. If there are no named
14730 -- associations, the chain is simply the list of Parameter Associations,
14731 -- since the order is the same as the declaration order. If there are named
14732 -- associations, then the First_Named_Actual field in the N_Function_Call
14733 -- or N_Procedure_Call_Statement node points to the Parameter_Association
14734 -- node for the parameter that comes first in declaration order. The
14735 -- remaining named parameters are then chained in declaration order using
14736 -- Next_Named_Actual.
14738 -- This routine also verifies that the number of actuals is compatible with
14739 -- the number and default values of formals, but performs no type checking
14740 -- (type checking is done by the caller).
14742 -- If the matching succeeds, Success is set to True and the caller proceeds
14743 -- with type-checking. If the match is unsuccessful, then Success is set to
14744 -- False, and the caller attempts a different interpretation, if there is
14745 -- one.
14747 -- If the flag Report is on, the call is not overloaded, and a failure to
14748 -- match can be reported here, rather than in the caller.
14750 procedure Normalize_Actuals
14751 (N : Node_Id;
14752 S : Entity_Id;
14753 Report : Boolean;
14754 Success : out Boolean)
14756 Actuals : constant List_Id := Parameter_Associations (N);
14757 Actual : Node_Id := Empty;
14758 Formal : Entity_Id;
14759 Last : Node_Id := Empty;
14760 First_Named : Node_Id := Empty;
14761 Found : Boolean;
14763 Formals_To_Match : Integer := 0;
14764 Actuals_To_Match : Integer := 0;
14766 procedure Chain (A : Node_Id);
14767 -- Add named actual at the proper place in the list, using the
14768 -- Next_Named_Actual link.
14770 function Reporting return Boolean;
14771 -- Determines if an error is to be reported. To report an error, we
14772 -- need Report to be True, and also we do not report errors caused
14773 -- by calls to init procs that occur within other init procs. Such
14774 -- errors must always be cascaded errors, since if all the types are
14775 -- declared correctly, the compiler will certainly build decent calls.
14777 -----------
14778 -- Chain --
14779 -----------
14781 procedure Chain (A : Node_Id) is
14782 begin
14783 if No (Last) then
14785 -- Call node points to first actual in list
14787 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
14789 else
14790 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
14791 end if;
14793 Last := A;
14794 Set_Next_Named_Actual (Last, Empty);
14795 end Chain;
14797 ---------------
14798 -- Reporting --
14799 ---------------
14801 function Reporting return Boolean is
14802 begin
14803 if not Report then
14804 return False;
14806 elsif not Within_Init_Proc then
14807 return True;
14809 elsif Is_Init_Proc (Entity (Name (N))) then
14810 return False;
14812 else
14813 return True;
14814 end if;
14815 end Reporting;
14817 -- Start of processing for Normalize_Actuals
14819 begin
14820 if Is_Access_Type (S) then
14822 -- The name in the call is a function call that returns an access
14823 -- to subprogram. The designated type has the list of formals.
14825 Formal := First_Formal (Designated_Type (S));
14826 else
14827 Formal := First_Formal (S);
14828 end if;
14830 while Present (Formal) loop
14831 Formals_To_Match := Formals_To_Match + 1;
14832 Next_Formal (Formal);
14833 end loop;
14835 -- Find if there is a named association, and verify that no positional
14836 -- associations appear after named ones.
14838 if Present (Actuals) then
14839 Actual := First (Actuals);
14840 end if;
14842 while Present (Actual)
14843 and then Nkind (Actual) /= N_Parameter_Association
14844 loop
14845 Actuals_To_Match := Actuals_To_Match + 1;
14846 Next (Actual);
14847 end loop;
14849 if No (Actual) and Actuals_To_Match = Formals_To_Match then
14851 -- Most common case: positional notation, no defaults
14853 Success := True;
14854 return;
14856 elsif Actuals_To_Match > Formals_To_Match then
14858 -- Too many actuals: will not work
14860 if Reporting then
14861 if Is_Entity_Name (Name (N)) then
14862 Error_Msg_N ("too many arguments in call to&", Name (N));
14863 else
14864 Error_Msg_N ("too many arguments in call", N);
14865 end if;
14866 end if;
14868 Success := False;
14869 return;
14870 end if;
14872 First_Named := Actual;
14874 while Present (Actual) loop
14875 if Nkind (Actual) /= N_Parameter_Association then
14876 Error_Msg_N
14877 ("positional parameters not allowed after named ones", Actual);
14878 Success := False;
14879 return;
14881 else
14882 Actuals_To_Match := Actuals_To_Match + 1;
14883 end if;
14885 Next (Actual);
14886 end loop;
14888 if Present (Actuals) then
14889 Actual := First (Actuals);
14890 end if;
14892 Formal := First_Formal (S);
14893 while Present (Formal) loop
14895 -- Match the formals in order. If the corresponding actual is
14896 -- positional, nothing to do. Else scan the list of named actuals
14897 -- to find the one with the right name.
14899 if Present (Actual)
14900 and then Nkind (Actual) /= N_Parameter_Association
14901 then
14902 Next (Actual);
14903 Actuals_To_Match := Actuals_To_Match - 1;
14904 Formals_To_Match := Formals_To_Match - 1;
14906 else
14907 -- For named parameters, search the list of actuals to find
14908 -- one that matches the next formal name.
14910 Actual := First_Named;
14911 Found := False;
14912 while Present (Actual) loop
14913 if Chars (Selector_Name (Actual)) = Chars (Formal) then
14914 Found := True;
14915 Chain (Actual);
14916 Actuals_To_Match := Actuals_To_Match - 1;
14917 Formals_To_Match := Formals_To_Match - 1;
14918 exit;
14919 end if;
14921 Next (Actual);
14922 end loop;
14924 if not Found then
14925 if Ekind (Formal) /= E_In_Parameter
14926 or else No (Default_Value (Formal))
14927 then
14928 if Reporting then
14929 if (Comes_From_Source (S)
14930 or else Sloc (S) = Standard_Location)
14931 and then Is_Overloadable (S)
14932 then
14933 if No (Actuals)
14934 and then
14935 Nkind_In (Parent (N), N_Procedure_Call_Statement,
14936 N_Function_Call,
14937 N_Parameter_Association)
14938 and then Ekind (S) /= E_Function
14939 then
14940 Set_Etype (N, Etype (S));
14942 else
14943 Error_Msg_Name_1 := Chars (S);
14944 Error_Msg_Sloc := Sloc (S);
14945 Error_Msg_NE
14946 ("missing argument for parameter & "
14947 & "in call to % declared #", N, Formal);
14948 end if;
14950 elsif Is_Overloadable (S) then
14951 Error_Msg_Name_1 := Chars (S);
14953 -- Point to type derivation that generated the
14954 -- operation.
14956 Error_Msg_Sloc := Sloc (Parent (S));
14958 Error_Msg_NE
14959 ("missing argument for parameter & "
14960 & "in call to % (inherited) #", N, Formal);
14962 else
14963 Error_Msg_NE
14964 ("missing argument for parameter &", N, Formal);
14965 end if;
14966 end if;
14968 Success := False;
14969 return;
14971 else
14972 Formals_To_Match := Formals_To_Match - 1;
14973 end if;
14974 end if;
14975 end if;
14977 Next_Formal (Formal);
14978 end loop;
14980 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
14981 Success := True;
14982 return;
14984 else
14985 if Reporting then
14987 -- Find some superfluous named actual that did not get
14988 -- attached to the list of associations.
14990 Actual := First (Actuals);
14991 while Present (Actual) loop
14992 if Nkind (Actual) = N_Parameter_Association
14993 and then Actual /= Last
14994 and then No (Next_Named_Actual (Actual))
14995 then
14996 Error_Msg_N ("unmatched actual & in call",
14997 Selector_Name (Actual));
14998 exit;
14999 end if;
15001 Next (Actual);
15002 end loop;
15003 end if;
15005 Success := False;
15006 return;
15007 end if;
15008 end Normalize_Actuals;
15010 --------------------------------
15011 -- Note_Possible_Modification --
15012 --------------------------------
15014 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
15015 Modification_Comes_From_Source : constant Boolean :=
15016 Comes_From_Source (Parent (N));
15018 Ent : Entity_Id;
15019 Exp : Node_Id;
15021 begin
15022 -- Loop to find referenced entity, if there is one
15024 Exp := N;
15025 loop
15026 Ent := Empty;
15028 if Is_Entity_Name (Exp) then
15029 Ent := Entity (Exp);
15031 -- If the entity is missing, it is an undeclared identifier,
15032 -- and there is nothing to annotate.
15034 if No (Ent) then
15035 return;
15036 end if;
15038 elsif Nkind (Exp) = N_Explicit_Dereference then
15039 declare
15040 P : constant Node_Id := Prefix (Exp);
15042 begin
15043 -- In formal verification mode, keep track of all reads and
15044 -- writes through explicit dereferences.
15046 if GNATprove_Mode then
15047 SPARK_Specific.Generate_Dereference (N, 'm');
15048 end if;
15050 if Nkind (P) = N_Selected_Component
15051 and then Present (Entry_Formal (Entity (Selector_Name (P))))
15052 then
15053 -- Case of a reference to an entry formal
15055 Ent := Entry_Formal (Entity (Selector_Name (P)));
15057 elsif Nkind (P) = N_Identifier
15058 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
15059 and then Present (Expression (Parent (Entity (P))))
15060 and then Nkind (Expression (Parent (Entity (P)))) =
15061 N_Reference
15062 then
15063 -- Case of a reference to a value on which side effects have
15064 -- been removed.
15066 Exp := Prefix (Expression (Parent (Entity (P))));
15067 goto Continue;
15069 else
15070 return;
15071 end if;
15072 end;
15074 elsif Nkind_In (Exp, N_Type_Conversion,
15075 N_Unchecked_Type_Conversion)
15076 then
15077 Exp := Expression (Exp);
15078 goto Continue;
15080 elsif Nkind_In (Exp, N_Slice,
15081 N_Indexed_Component,
15082 N_Selected_Component)
15083 then
15084 -- Special check, if the prefix is an access type, then return
15085 -- since we are modifying the thing pointed to, not the prefix.
15086 -- When we are expanding, most usually the prefix is replaced
15087 -- by an explicit dereference, and this test is not needed, but
15088 -- in some cases (notably -gnatc mode and generics) when we do
15089 -- not do full expansion, we need this special test.
15091 if Is_Access_Type (Etype (Prefix (Exp))) then
15092 return;
15094 -- Otherwise go to prefix and keep going
15096 else
15097 Exp := Prefix (Exp);
15098 goto Continue;
15099 end if;
15101 -- All other cases, not a modification
15103 else
15104 return;
15105 end if;
15107 -- Now look for entity being referenced
15109 if Present (Ent) then
15110 if Is_Object (Ent) then
15111 if Comes_From_Source (Exp)
15112 or else Modification_Comes_From_Source
15113 then
15114 -- Give warning if pragma unmodified given and we are
15115 -- sure this is a modification.
15117 if Has_Pragma_Unmodified (Ent) and then Sure then
15118 Error_Msg_NE ("??pragma Unmodified given for &!", N, Ent);
15119 end if;
15121 Set_Never_Set_In_Source (Ent, False);
15122 end if;
15124 Set_Is_True_Constant (Ent, False);
15125 Set_Current_Value (Ent, Empty);
15126 Set_Is_Known_Null (Ent, False);
15128 if not Can_Never_Be_Null (Ent) then
15129 Set_Is_Known_Non_Null (Ent, False);
15130 end if;
15132 -- Follow renaming chain
15134 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
15135 and then Present (Renamed_Object (Ent))
15136 then
15137 Exp := Renamed_Object (Ent);
15139 -- If the entity is the loop variable in an iteration over
15140 -- a container, retrieve container expression to indicate
15141 -- possible modificastion.
15143 if Present (Related_Expression (Ent))
15144 and then Nkind (Parent (Related_Expression (Ent))) =
15145 N_Iterator_Specification
15146 then
15147 Exp := Original_Node (Related_Expression (Ent));
15148 end if;
15150 goto Continue;
15152 -- The expression may be the renaming of a subcomponent of an
15153 -- array or container. The assignment to the subcomponent is
15154 -- a modification of the container.
15156 elsif Comes_From_Source (Original_Node (Exp))
15157 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
15158 N_Indexed_Component)
15159 then
15160 Exp := Prefix (Original_Node (Exp));
15161 goto Continue;
15162 end if;
15164 -- Generate a reference only if the assignment comes from
15165 -- source. This excludes, for example, calls to a dispatching
15166 -- assignment operation when the left-hand side is tagged. In
15167 -- GNATprove mode, we need those references also on generated
15168 -- code, as these are used to compute the local effects of
15169 -- subprograms.
15171 if Modification_Comes_From_Source or GNATprove_Mode then
15172 Generate_Reference (Ent, Exp, 'm');
15174 -- If the target of the assignment is the bound variable
15175 -- in an iterator, indicate that the corresponding array
15176 -- or container is also modified.
15178 if Ada_Version >= Ada_2012
15179 and then Nkind (Parent (Ent)) = N_Iterator_Specification
15180 then
15181 declare
15182 Domain : constant Node_Id := Name (Parent (Ent));
15184 begin
15185 -- TBD : in the full version of the construct, the
15186 -- domain of iteration can be given by an expression.
15188 if Is_Entity_Name (Domain) then
15189 Generate_Reference (Entity (Domain), Exp, 'm');
15190 Set_Is_True_Constant (Entity (Domain), False);
15191 Set_Never_Set_In_Source (Entity (Domain), False);
15192 end if;
15193 end;
15194 end if;
15195 end if;
15197 Check_Nested_Access (N, Ent);
15198 end if;
15200 Kill_Checks (Ent);
15202 -- If we are sure this is a modification from source, and we know
15203 -- this modifies a constant, then give an appropriate warning.
15205 if Overlays_Constant (Ent)
15206 and then (Modification_Comes_From_Source and Sure)
15207 then
15208 declare
15209 A : constant Node_Id := Address_Clause (Ent);
15210 begin
15211 if Present (A) then
15212 declare
15213 Exp : constant Node_Id := Expression (A);
15214 begin
15215 if Nkind (Exp) = N_Attribute_Reference
15216 and then Attribute_Name (Exp) = Name_Address
15217 and then Is_Entity_Name (Prefix (Exp))
15218 then
15219 Error_Msg_Sloc := Sloc (A);
15220 Error_Msg_NE
15221 ("constant& may be modified via address "
15222 & "clause#??", N, Entity (Prefix (Exp)));
15223 end if;
15224 end;
15225 end if;
15226 end;
15227 end if;
15229 return;
15230 end if;
15232 <<Continue>>
15233 null;
15234 end loop;
15235 end Note_Possible_Modification;
15237 -------------------------
15238 -- Object_Access_Level --
15239 -------------------------
15241 -- Returns the static accessibility level of the view denoted by Obj. Note
15242 -- that the value returned is the result of a call to Scope_Depth. Only
15243 -- scope depths associated with dynamic scopes can actually be returned.
15244 -- Since only relative levels matter for accessibility checking, the fact
15245 -- that the distance between successive levels of accessibility is not
15246 -- always one is immaterial (invariant: if level(E2) is deeper than
15247 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
15249 function Object_Access_Level (Obj : Node_Id) return Uint is
15250 function Is_Interface_Conversion (N : Node_Id) return Boolean;
15251 -- Determine whether N is a construct of the form
15252 -- Some_Type (Operand._tag'Address)
15253 -- This construct appears in the context of dispatching calls.
15255 function Reference_To (Obj : Node_Id) return Node_Id;
15256 -- An explicit dereference is created when removing side-effects from
15257 -- expressions for constraint checking purposes. In this case a local
15258 -- access type is created for it. The correct access level is that of
15259 -- the original source node. We detect this case by noting that the
15260 -- prefix of the dereference is created by an object declaration whose
15261 -- initial expression is a reference.
15263 -----------------------------
15264 -- Is_Interface_Conversion --
15265 -----------------------------
15267 function Is_Interface_Conversion (N : Node_Id) return Boolean is
15268 begin
15269 return Nkind (N) = N_Unchecked_Type_Conversion
15270 and then Nkind (Expression (N)) = N_Attribute_Reference
15271 and then Attribute_Name (Expression (N)) = Name_Address;
15272 end Is_Interface_Conversion;
15274 ------------------
15275 -- Reference_To --
15276 ------------------
15278 function Reference_To (Obj : Node_Id) return Node_Id is
15279 Pref : constant Node_Id := Prefix (Obj);
15280 begin
15281 if Is_Entity_Name (Pref)
15282 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
15283 and then Present (Expression (Parent (Entity (Pref))))
15284 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
15285 then
15286 return (Prefix (Expression (Parent (Entity (Pref)))));
15287 else
15288 return Empty;
15289 end if;
15290 end Reference_To;
15292 -- Local variables
15294 E : Entity_Id;
15296 -- Start of processing for Object_Access_Level
15298 begin
15299 if Nkind (Obj) = N_Defining_Identifier
15300 or else Is_Entity_Name (Obj)
15301 then
15302 if Nkind (Obj) = N_Defining_Identifier then
15303 E := Obj;
15304 else
15305 E := Entity (Obj);
15306 end if;
15308 if Is_Prival (E) then
15309 E := Prival_Link (E);
15310 end if;
15312 -- If E is a type then it denotes a current instance. For this case
15313 -- we add one to the normal accessibility level of the type to ensure
15314 -- that current instances are treated as always being deeper than
15315 -- than the level of any visible named access type (see 3.10.2(21)).
15317 if Is_Type (E) then
15318 return Type_Access_Level (E) + 1;
15320 elsif Present (Renamed_Object (E)) then
15321 return Object_Access_Level (Renamed_Object (E));
15323 -- Similarly, if E is a component of the current instance of a
15324 -- protected type, any instance of it is assumed to be at a deeper
15325 -- level than the type. For a protected object (whose type is an
15326 -- anonymous protected type) its components are at the same level
15327 -- as the type itself.
15329 elsif not Is_Overloadable (E)
15330 and then Ekind (Scope (E)) = E_Protected_Type
15331 and then Comes_From_Source (Scope (E))
15332 then
15333 return Type_Access_Level (Scope (E)) + 1;
15335 else
15336 -- Aliased formals take their access level from the point of call.
15337 -- This is smaller than the level of the subprogram itself.
15339 if Is_Formal (E) and then Is_Aliased (E) then
15340 return Type_Access_Level (Etype (E));
15342 else
15343 return Scope_Depth (Enclosing_Dynamic_Scope (E));
15344 end if;
15345 end if;
15347 elsif Nkind (Obj) = N_Selected_Component then
15348 if Is_Access_Type (Etype (Prefix (Obj))) then
15349 return Type_Access_Level (Etype (Prefix (Obj)));
15350 else
15351 return Object_Access_Level (Prefix (Obj));
15352 end if;
15354 elsif Nkind (Obj) = N_Indexed_Component then
15355 if Is_Access_Type (Etype (Prefix (Obj))) then
15356 return Type_Access_Level (Etype (Prefix (Obj)));
15357 else
15358 return Object_Access_Level (Prefix (Obj));
15359 end if;
15361 elsif Nkind (Obj) = N_Explicit_Dereference then
15363 -- If the prefix is a selected access discriminant then we make a
15364 -- recursive call on the prefix, which will in turn check the level
15365 -- of the prefix object of the selected discriminant.
15367 -- In Ada 2012, if the discriminant has implicit dereference and
15368 -- the context is a selected component, treat this as an object of
15369 -- unknown scope (see below). This is necessary in compile-only mode;
15370 -- otherwise expansion will already have transformed the prefix into
15371 -- a temporary.
15373 if Nkind (Prefix (Obj)) = N_Selected_Component
15374 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
15375 and then
15376 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
15377 and then
15378 (not Has_Implicit_Dereference
15379 (Entity (Selector_Name (Prefix (Obj))))
15380 or else Nkind (Parent (Obj)) /= N_Selected_Component)
15381 then
15382 return Object_Access_Level (Prefix (Obj));
15384 -- Detect an interface conversion in the context of a dispatching
15385 -- call. Use the original form of the conversion to find the access
15386 -- level of the operand.
15388 elsif Is_Interface (Etype (Obj))
15389 and then Is_Interface_Conversion (Prefix (Obj))
15390 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
15391 then
15392 return Object_Access_Level (Original_Node (Obj));
15394 elsif not Comes_From_Source (Obj) then
15395 declare
15396 Ref : constant Node_Id := Reference_To (Obj);
15397 begin
15398 if Present (Ref) then
15399 return Object_Access_Level (Ref);
15400 else
15401 return Type_Access_Level (Etype (Prefix (Obj)));
15402 end if;
15403 end;
15405 else
15406 return Type_Access_Level (Etype (Prefix (Obj)));
15407 end if;
15409 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
15410 return Object_Access_Level (Expression (Obj));
15412 elsif Nkind (Obj) = N_Function_Call then
15414 -- Function results are objects, so we get either the access level of
15415 -- the function or, in the case of an indirect call, the level of the
15416 -- access-to-subprogram type. (This code is used for Ada 95, but it
15417 -- looks wrong, because it seems that we should be checking the level
15418 -- of the call itself, even for Ada 95. However, using the Ada 2005
15419 -- version of the code causes regressions in several tests that are
15420 -- compiled with -gnat95. ???)
15422 if Ada_Version < Ada_2005 then
15423 if Is_Entity_Name (Name (Obj)) then
15424 return Subprogram_Access_Level (Entity (Name (Obj)));
15425 else
15426 return Type_Access_Level (Etype (Prefix (Name (Obj))));
15427 end if;
15429 -- For Ada 2005, the level of the result object of a function call is
15430 -- defined to be the level of the call's innermost enclosing master.
15431 -- We determine that by querying the depth of the innermost enclosing
15432 -- dynamic scope.
15434 else
15435 Return_Master_Scope_Depth_Of_Call : declare
15437 function Innermost_Master_Scope_Depth
15438 (N : Node_Id) return Uint;
15439 -- Returns the scope depth of the given node's innermost
15440 -- enclosing dynamic scope (effectively the accessibility
15441 -- level of the innermost enclosing master).
15443 ----------------------------------
15444 -- Innermost_Master_Scope_Depth --
15445 ----------------------------------
15447 function Innermost_Master_Scope_Depth
15448 (N : Node_Id) return Uint
15450 Node_Par : Node_Id := Parent (N);
15452 begin
15453 -- Locate the nearest enclosing node (by traversing Parents)
15454 -- that Defining_Entity can be applied to, and return the
15455 -- depth of that entity's nearest enclosing dynamic scope.
15457 while Present (Node_Par) loop
15458 case Nkind (Node_Par) is
15459 when N_Component_Declaration |
15460 N_Entry_Declaration |
15461 N_Formal_Object_Declaration |
15462 N_Formal_Type_Declaration |
15463 N_Full_Type_Declaration |
15464 N_Incomplete_Type_Declaration |
15465 N_Loop_Parameter_Specification |
15466 N_Object_Declaration |
15467 N_Protected_Type_Declaration |
15468 N_Private_Extension_Declaration |
15469 N_Private_Type_Declaration |
15470 N_Subtype_Declaration |
15471 N_Function_Specification |
15472 N_Procedure_Specification |
15473 N_Task_Type_Declaration |
15474 N_Body_Stub |
15475 N_Generic_Instantiation |
15476 N_Proper_Body |
15477 N_Implicit_Label_Declaration |
15478 N_Package_Declaration |
15479 N_Single_Task_Declaration |
15480 N_Subprogram_Declaration |
15481 N_Generic_Declaration |
15482 N_Renaming_Declaration |
15483 N_Block_Statement |
15484 N_Formal_Subprogram_Declaration |
15485 N_Abstract_Subprogram_Declaration |
15486 N_Entry_Body |
15487 N_Exception_Declaration |
15488 N_Formal_Package_Declaration |
15489 N_Number_Declaration |
15490 N_Package_Specification |
15491 N_Parameter_Specification |
15492 N_Single_Protected_Declaration |
15493 N_Subunit =>
15495 return Scope_Depth
15496 (Nearest_Dynamic_Scope
15497 (Defining_Entity (Node_Par)));
15499 when others =>
15500 null;
15501 end case;
15503 Node_Par := Parent (Node_Par);
15504 end loop;
15506 pragma Assert (False);
15508 -- Should never reach the following return
15510 return Scope_Depth (Current_Scope) + 1;
15511 end Innermost_Master_Scope_Depth;
15513 -- Start of processing for Return_Master_Scope_Depth_Of_Call
15515 begin
15516 return Innermost_Master_Scope_Depth (Obj);
15517 end Return_Master_Scope_Depth_Of_Call;
15518 end if;
15520 -- For convenience we handle qualified expressions, even though they
15521 -- aren't technically object names.
15523 elsif Nkind (Obj) = N_Qualified_Expression then
15524 return Object_Access_Level (Expression (Obj));
15526 -- Ditto for aggregates. They have the level of the temporary that
15527 -- will hold their value.
15529 elsif Nkind (Obj) = N_Aggregate then
15530 return Object_Access_Level (Current_Scope);
15532 -- Otherwise return the scope level of Standard. (If there are cases
15533 -- that fall through to this point they will be treated as having
15534 -- global accessibility for now. ???)
15536 else
15537 return Scope_Depth (Standard_Standard);
15538 end if;
15539 end Object_Access_Level;
15541 ---------------------------------
15542 -- Original_Aspect_Pragma_Name --
15543 ---------------------------------
15545 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
15546 Item : Node_Id;
15547 Item_Nam : Name_Id;
15549 begin
15550 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
15552 Item := N;
15554 -- The pragma was generated to emulate an aspect, use the original
15555 -- aspect specification.
15557 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
15558 Item := Corresponding_Aspect (Item);
15559 end if;
15561 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
15562 -- Post and Post_Class rewrite their pragma identifier to preserve the
15563 -- original name.
15564 -- ??? this is kludgey
15566 if Nkind (Item) = N_Pragma then
15567 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
15569 else
15570 pragma Assert (Nkind (Item) = N_Aspect_Specification);
15571 Item_Nam := Chars (Identifier (Item));
15572 end if;
15574 -- Deal with 'Class by converting the name to its _XXX form
15576 if Class_Present (Item) then
15577 if Item_Nam = Name_Invariant then
15578 Item_Nam := Name_uInvariant;
15580 elsif Item_Nam = Name_Post then
15581 Item_Nam := Name_uPost;
15583 elsif Item_Nam = Name_Pre then
15584 Item_Nam := Name_uPre;
15586 elsif Nam_In (Item_Nam, Name_Type_Invariant,
15587 Name_Type_Invariant_Class)
15588 then
15589 Item_Nam := Name_uType_Invariant;
15591 -- Nothing to do for other cases (e.g. a Check that derived from
15592 -- Pre_Class and has the flag set). Also we do nothing if the name
15593 -- is already in special _xxx form.
15595 end if;
15596 end if;
15598 return Item_Nam;
15599 end Original_Aspect_Pragma_Name;
15601 --------------------------------------
15602 -- Original_Corresponding_Operation --
15603 --------------------------------------
15605 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
15607 Typ : constant Entity_Id := Find_Dispatching_Type (S);
15609 begin
15610 -- If S is an inherited primitive S2 the original corresponding
15611 -- operation of S is the original corresponding operation of S2
15613 if Present (Alias (S))
15614 and then Find_Dispatching_Type (Alias (S)) /= Typ
15615 then
15616 return Original_Corresponding_Operation (Alias (S));
15618 -- If S overrides an inherited subprogram S2 the original corresponding
15619 -- operation of S is the original corresponding operation of S2
15621 elsif Present (Overridden_Operation (S)) then
15622 return Original_Corresponding_Operation (Overridden_Operation (S));
15624 -- otherwise it is S itself
15626 else
15627 return S;
15628 end if;
15629 end Original_Corresponding_Operation;
15631 ----------------------
15632 -- Policy_In_Effect --
15633 ----------------------
15635 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
15636 function Policy_In_List (List : Node_Id) return Name_Id;
15637 -- Determine the the mode of a policy in a N_Pragma list
15639 --------------------
15640 -- Policy_In_List --
15641 --------------------
15643 function Policy_In_List (List : Node_Id) return Name_Id is
15644 Arg : Node_Id;
15645 Expr : Node_Id;
15646 Prag : Node_Id;
15648 begin
15649 Prag := List;
15650 while Present (Prag) loop
15651 Arg := First (Pragma_Argument_Associations (Prag));
15652 Expr := Get_Pragma_Arg (Arg);
15654 -- The current Check_Policy pragma matches the requested policy,
15655 -- return the second argument which denotes the policy identifier.
15657 if Chars (Expr) = Policy then
15658 return Chars (Get_Pragma_Arg (Next (Arg)));
15659 end if;
15661 Prag := Next_Pragma (Prag);
15662 end loop;
15664 return No_Name;
15665 end Policy_In_List;
15667 -- Local variables
15669 Kind : Name_Id;
15671 -- Start of processing for Policy_In_Effect
15673 begin
15674 if not Is_Valid_Assertion_Kind (Policy) then
15675 raise Program_Error;
15676 end if;
15678 -- Inspect all policy pragmas that appear within scopes (if any)
15680 Kind := Policy_In_List (Check_Policy_List);
15682 -- Inspect all configuration policy pragmas (if any)
15684 if Kind = No_Name then
15685 Kind := Policy_In_List (Check_Policy_List_Config);
15686 end if;
15688 -- The context lacks policy pragmas, determine the mode based on whether
15689 -- assertions are enabled at the configuration level. This ensures that
15690 -- the policy is preserved when analyzing generics.
15692 if Kind = No_Name then
15693 if Assertions_Enabled_Config then
15694 Kind := Name_Check;
15695 else
15696 Kind := Name_Ignore;
15697 end if;
15698 end if;
15700 return Kind;
15701 end Policy_In_Effect;
15703 ----------------------------------
15704 -- Predicate_Tests_On_Arguments --
15705 ----------------------------------
15707 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
15708 begin
15709 -- Always test predicates on indirect call
15711 if Ekind (Subp) = E_Subprogram_Type then
15712 return True;
15714 -- Do not test predicates on call to generated default Finalize, since
15715 -- we are not interested in whether something we are finalizing (and
15716 -- typically destroying) satisfies its predicates.
15718 elsif Chars (Subp) = Name_Finalize
15719 and then not Comes_From_Source (Subp)
15720 then
15721 return False;
15723 -- Do not test predicates on any internally generated routines
15725 elsif Is_Internal_Name (Chars (Subp)) then
15726 return False;
15728 -- Do not test predicates on call to Init_Proc, since if needed the
15729 -- predicate test will occur at some other point.
15731 elsif Is_Init_Proc (Subp) then
15732 return False;
15734 -- Do not test predicates on call to predicate function, since this
15735 -- would cause infinite recursion.
15737 elsif Ekind (Subp) = E_Function
15738 and then (Is_Predicate_Function (Subp)
15739 or else
15740 Is_Predicate_Function_M (Subp))
15741 then
15742 return False;
15744 -- For now, no other exceptions
15746 else
15747 return True;
15748 end if;
15749 end Predicate_Tests_On_Arguments;
15751 -----------------------
15752 -- Private_Component --
15753 -----------------------
15755 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
15756 Ancestor : constant Entity_Id := Base_Type (Type_Id);
15758 function Trace_Components
15759 (T : Entity_Id;
15760 Check : Boolean) return Entity_Id;
15761 -- Recursive function that does the work, and checks against circular
15762 -- definition for each subcomponent type.
15764 ----------------------
15765 -- Trace_Components --
15766 ----------------------
15768 function Trace_Components
15769 (T : Entity_Id;
15770 Check : Boolean) return Entity_Id
15772 Btype : constant Entity_Id := Base_Type (T);
15773 Component : Entity_Id;
15774 P : Entity_Id;
15775 Candidate : Entity_Id := Empty;
15777 begin
15778 if Check and then Btype = Ancestor then
15779 Error_Msg_N ("circular type definition", Type_Id);
15780 return Any_Type;
15781 end if;
15783 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
15784 if Present (Full_View (Btype))
15785 and then Is_Record_Type (Full_View (Btype))
15786 and then not Is_Frozen (Btype)
15787 then
15788 -- To indicate that the ancestor depends on a private type, the
15789 -- current Btype is sufficient. However, to check for circular
15790 -- definition we must recurse on the full view.
15792 Candidate := Trace_Components (Full_View (Btype), True);
15794 if Candidate = Any_Type then
15795 return Any_Type;
15796 else
15797 return Btype;
15798 end if;
15800 else
15801 return Btype;
15802 end if;
15804 elsif Is_Array_Type (Btype) then
15805 return Trace_Components (Component_Type (Btype), True);
15807 elsif Is_Record_Type (Btype) then
15808 Component := First_Entity (Btype);
15809 while Present (Component)
15810 and then Comes_From_Source (Component)
15811 loop
15812 -- Skip anonymous types generated by constrained components
15814 if not Is_Type (Component) then
15815 P := Trace_Components (Etype (Component), True);
15817 if Present (P) then
15818 if P = Any_Type then
15819 return P;
15820 else
15821 Candidate := P;
15822 end if;
15823 end if;
15824 end if;
15826 Next_Entity (Component);
15827 end loop;
15829 return Candidate;
15831 else
15832 return Empty;
15833 end if;
15834 end Trace_Components;
15836 -- Start of processing for Private_Component
15838 begin
15839 return Trace_Components (Type_Id, False);
15840 end Private_Component;
15842 ---------------------------
15843 -- Primitive_Names_Match --
15844 ---------------------------
15846 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
15848 function Non_Internal_Name (E : Entity_Id) return Name_Id;
15849 -- Given an internal name, returns the corresponding non-internal name
15851 ------------------------
15852 -- Non_Internal_Name --
15853 ------------------------
15855 function Non_Internal_Name (E : Entity_Id) return Name_Id is
15856 begin
15857 Get_Name_String (Chars (E));
15858 Name_Len := Name_Len - 1;
15859 return Name_Find;
15860 end Non_Internal_Name;
15862 -- Start of processing for Primitive_Names_Match
15864 begin
15865 pragma Assert (Present (E1) and then Present (E2));
15867 return Chars (E1) = Chars (E2)
15868 or else
15869 (not Is_Internal_Name (Chars (E1))
15870 and then Is_Internal_Name (Chars (E2))
15871 and then Non_Internal_Name (E2) = Chars (E1))
15872 or else
15873 (not Is_Internal_Name (Chars (E2))
15874 and then Is_Internal_Name (Chars (E1))
15875 and then Non_Internal_Name (E1) = Chars (E2))
15876 or else
15877 (Is_Predefined_Dispatching_Operation (E1)
15878 and then Is_Predefined_Dispatching_Operation (E2)
15879 and then Same_TSS (E1, E2))
15880 or else
15881 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
15882 end Primitive_Names_Match;
15884 -----------------------
15885 -- Process_End_Label --
15886 -----------------------
15888 procedure Process_End_Label
15889 (N : Node_Id;
15890 Typ : Character;
15891 Ent : Entity_Id)
15893 Loc : Source_Ptr;
15894 Nam : Node_Id;
15895 Scop : Entity_Id;
15897 Label_Ref : Boolean;
15898 -- Set True if reference to end label itself is required
15900 Endl : Node_Id;
15901 -- Gets set to the operator symbol or identifier that references the
15902 -- entity Ent. For the child unit case, this is the identifier from the
15903 -- designator. For other cases, this is simply Endl.
15905 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
15906 -- N is an identifier node that appears as a parent unit reference in
15907 -- the case where Ent is a child unit. This procedure generates an
15908 -- appropriate cross-reference entry. E is the corresponding entity.
15910 -------------------------
15911 -- Generate_Parent_Ref --
15912 -------------------------
15914 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
15915 begin
15916 -- If names do not match, something weird, skip reference
15918 if Chars (E) = Chars (N) then
15920 -- Generate the reference. We do NOT consider this as a reference
15921 -- for unreferenced symbol purposes.
15923 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
15925 if Style_Check then
15926 Style.Check_Identifier (N, E);
15927 end if;
15928 end if;
15929 end Generate_Parent_Ref;
15931 -- Start of processing for Process_End_Label
15933 begin
15934 -- If no node, ignore. This happens in some error situations, and
15935 -- also for some internally generated structures where no end label
15936 -- references are required in any case.
15938 if No (N) then
15939 return;
15940 end if;
15942 -- Nothing to do if no End_Label, happens for internally generated
15943 -- constructs where we don't want an end label reference anyway. Also
15944 -- nothing to do if Endl is a string literal, which means there was
15945 -- some prior error (bad operator symbol)
15947 Endl := End_Label (N);
15949 if No (Endl) or else Nkind (Endl) = N_String_Literal then
15950 return;
15951 end if;
15953 -- Reference node is not in extended main source unit
15955 if not In_Extended_Main_Source_Unit (N) then
15957 -- Generally we do not collect references except for the extended
15958 -- main source unit. The one exception is the 'e' entry for a
15959 -- package spec, where it is useful for a client to have the
15960 -- ending information to define scopes.
15962 if Typ /= 'e' then
15963 return;
15965 else
15966 Label_Ref := False;
15968 -- For this case, we can ignore any parent references, but we
15969 -- need the package name itself for the 'e' entry.
15971 if Nkind (Endl) = N_Designator then
15972 Endl := Identifier (Endl);
15973 end if;
15974 end if;
15976 -- Reference is in extended main source unit
15978 else
15979 Label_Ref := True;
15981 -- For designator, generate references for the parent entries
15983 if Nkind (Endl) = N_Designator then
15985 -- Generate references for the prefix if the END line comes from
15986 -- source (otherwise we do not need these references) We climb the
15987 -- scope stack to find the expected entities.
15989 if Comes_From_Source (Endl) then
15990 Nam := Name (Endl);
15991 Scop := Current_Scope;
15992 while Nkind (Nam) = N_Selected_Component loop
15993 Scop := Scope (Scop);
15994 exit when No (Scop);
15995 Generate_Parent_Ref (Selector_Name (Nam), Scop);
15996 Nam := Prefix (Nam);
15997 end loop;
15999 if Present (Scop) then
16000 Generate_Parent_Ref (Nam, Scope (Scop));
16001 end if;
16002 end if;
16004 Endl := Identifier (Endl);
16005 end if;
16006 end if;
16008 -- If the end label is not for the given entity, then either we have
16009 -- some previous error, or this is a generic instantiation for which
16010 -- we do not need to make a cross-reference in this case anyway. In
16011 -- either case we simply ignore the call.
16013 if Chars (Ent) /= Chars (Endl) then
16014 return;
16015 end if;
16017 -- If label was really there, then generate a normal reference and then
16018 -- adjust the location in the end label to point past the name (which
16019 -- should almost always be the semicolon).
16021 Loc := Sloc (Endl);
16023 if Comes_From_Source (Endl) then
16025 -- If a label reference is required, then do the style check and
16026 -- generate an l-type cross-reference entry for the label
16028 if Label_Ref then
16029 if Style_Check then
16030 Style.Check_Identifier (Endl, Ent);
16031 end if;
16033 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
16034 end if;
16036 -- Set the location to point past the label (normally this will
16037 -- mean the semicolon immediately following the label). This is
16038 -- done for the sake of the 'e' or 't' entry generated below.
16040 Get_Decoded_Name_String (Chars (Endl));
16041 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
16043 else
16044 -- In SPARK mode, no missing label is allowed for packages and
16045 -- subprogram bodies. Detect those cases by testing whether
16046 -- Process_End_Label was called for a body (Typ = 't') or a package.
16048 if Restriction_Check_Required (SPARK_05)
16049 and then (Typ = 't' or else Ekind (Ent) = E_Package)
16050 then
16051 Error_Msg_Node_1 := Endl;
16052 Check_SPARK_05_Restriction
16053 ("`END &` required", Endl, Force => True);
16054 end if;
16055 end if;
16057 -- Now generate the e/t reference
16059 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
16061 -- Restore Sloc, in case modified above, since we have an identifier
16062 -- and the normal Sloc should be left set in the tree.
16064 Set_Sloc (Endl, Loc);
16065 end Process_End_Label;
16067 ----------------
16068 -- Referenced --
16069 ----------------
16071 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
16072 Seen : Boolean := False;
16074 function Is_Reference (N : Node_Id) return Traverse_Result;
16075 -- Determine whether node N denotes a reference to Id. If this is the
16076 -- case, set global flag Seen to True and stop the traversal.
16078 ------------------
16079 -- Is_Reference --
16080 ------------------
16082 function Is_Reference (N : Node_Id) return Traverse_Result is
16083 begin
16084 if Is_Entity_Name (N)
16085 and then Present (Entity (N))
16086 and then Entity (N) = Id
16087 then
16088 Seen := True;
16089 return Abandon;
16090 else
16091 return OK;
16092 end if;
16093 end Is_Reference;
16095 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
16097 -- Start of processing for Referenced
16099 begin
16100 Inspect_Expression (Expr);
16101 return Seen;
16102 end Referenced;
16104 ------------------------------------
16105 -- References_Generic_Formal_Type --
16106 ------------------------------------
16108 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
16110 function Process (N : Node_Id) return Traverse_Result;
16111 -- Process one node in search for generic formal type
16113 -------------
16114 -- Process --
16115 -------------
16117 function Process (N : Node_Id) return Traverse_Result is
16118 begin
16119 if Nkind (N) in N_Has_Entity then
16120 declare
16121 E : constant Entity_Id := Entity (N);
16122 begin
16123 if Present (E) then
16124 if Is_Generic_Type (E) then
16125 return Abandon;
16126 elsif Present (Etype (E))
16127 and then Is_Generic_Type (Etype (E))
16128 then
16129 return Abandon;
16130 end if;
16131 end if;
16132 end;
16133 end if;
16135 return Atree.OK;
16136 end Process;
16138 function Traverse is new Traverse_Func (Process);
16139 -- Traverse tree to look for generic type
16141 begin
16142 if Inside_A_Generic then
16143 return Traverse (N) = Abandon;
16144 else
16145 return False;
16146 end if;
16147 end References_Generic_Formal_Type;
16149 --------------------
16150 -- Remove_Homonym --
16151 --------------------
16153 procedure Remove_Homonym (E : Entity_Id) is
16154 Prev : Entity_Id := Empty;
16155 H : Entity_Id;
16157 begin
16158 if E = Current_Entity (E) then
16159 if Present (Homonym (E)) then
16160 Set_Current_Entity (Homonym (E));
16161 else
16162 Set_Name_Entity_Id (Chars (E), Empty);
16163 end if;
16165 else
16166 H := Current_Entity (E);
16167 while Present (H) and then H /= E loop
16168 Prev := H;
16169 H := Homonym (H);
16170 end loop;
16172 -- If E is not on the homonym chain, nothing to do
16174 if Present (H) then
16175 Set_Homonym (Prev, Homonym (E));
16176 end if;
16177 end if;
16178 end Remove_Homonym;
16180 ---------------------
16181 -- Rep_To_Pos_Flag --
16182 ---------------------
16184 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
16185 begin
16186 return New_Occurrence_Of
16187 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
16188 end Rep_To_Pos_Flag;
16190 --------------------
16191 -- Require_Entity --
16192 --------------------
16194 procedure Require_Entity (N : Node_Id) is
16195 begin
16196 if Is_Entity_Name (N) and then No (Entity (N)) then
16197 if Total_Errors_Detected /= 0 then
16198 Set_Entity (N, Any_Id);
16199 else
16200 raise Program_Error;
16201 end if;
16202 end if;
16203 end Require_Entity;
16205 -------------------------------
16206 -- Requires_State_Refinement --
16207 -------------------------------
16209 function Requires_State_Refinement
16210 (Spec_Id : Entity_Id;
16211 Body_Id : Entity_Id) return Boolean
16213 function Mode_Is_Off (Prag : Node_Id) return Boolean;
16214 -- Given pragma SPARK_Mode, determine whether the mode is Off
16216 -----------------
16217 -- Mode_Is_Off --
16218 -----------------
16220 function Mode_Is_Off (Prag : Node_Id) return Boolean is
16221 Mode : Node_Id;
16223 begin
16224 -- The default SPARK mode is On
16226 if No (Prag) then
16227 return False;
16228 end if;
16230 Mode := Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
16232 -- Then the pragma lacks an argument, the default mode is On
16234 if No (Mode) then
16235 return False;
16236 else
16237 return Chars (Mode) = Name_Off;
16238 end if;
16239 end Mode_Is_Off;
16241 -- Start of processing for Requires_State_Refinement
16243 begin
16244 -- A package that does not define at least one abstract state cannot
16245 -- possibly require refinement.
16247 if No (Abstract_States (Spec_Id)) then
16248 return False;
16250 -- The package instroduces a single null state which does not merit
16251 -- refinement.
16253 elsif Has_Null_Abstract_State (Spec_Id) then
16254 return False;
16256 -- Check whether the package body is subject to pragma SPARK_Mode. If
16257 -- it is and the mode is Off, the package body is considered to be in
16258 -- regular Ada and does not require refinement.
16260 elsif Mode_Is_Off (SPARK_Pragma (Body_Id)) then
16261 return False;
16263 -- The body's SPARK_Mode may be inherited from a similar pragma that
16264 -- appears in the private declarations of the spec. The pragma we are
16265 -- interested appears as the second entry in SPARK_Pragma.
16267 elsif Present (SPARK_Pragma (Spec_Id))
16268 and then Mode_Is_Off (Next_Pragma (SPARK_Pragma (Spec_Id)))
16269 then
16270 return False;
16272 -- The spec defines at least one abstract state and the body has no way
16273 -- of circumventing the refinement.
16275 else
16276 return True;
16277 end if;
16278 end Requires_State_Refinement;
16280 ------------------------------
16281 -- Requires_Transient_Scope --
16282 ------------------------------
16284 -- A transient scope is required when variable-sized temporaries are
16285 -- allocated in the primary or secondary stack, or when finalization
16286 -- actions must be generated before the next instruction.
16288 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
16289 Typ : constant Entity_Id := Underlying_Type (Id);
16291 -- Start of processing for Requires_Transient_Scope
16293 begin
16294 -- This is a private type which is not completed yet. This can only
16295 -- happen in a default expression (of a formal parameter or of a
16296 -- record component). Do not expand transient scope in this case
16298 if No (Typ) then
16299 return False;
16301 -- Do not expand transient scope for non-existent procedure return
16303 elsif Typ = Standard_Void_Type then
16304 return False;
16306 -- Elementary types do not require a transient scope
16308 elsif Is_Elementary_Type (Typ) then
16309 return False;
16311 -- Generally, indefinite subtypes require a transient scope, since the
16312 -- back end cannot generate temporaries, since this is not a valid type
16313 -- for declaring an object. It might be possible to relax this in the
16314 -- future, e.g. by declaring the maximum possible space for the type.
16316 elsif Is_Indefinite_Subtype (Typ) then
16317 return True;
16319 -- Functions returning tagged types may dispatch on result so their
16320 -- returned value is allocated on the secondary stack. Controlled
16321 -- type temporaries need finalization.
16323 elsif Is_Tagged_Type (Typ)
16324 or else Has_Controlled_Component (Typ)
16325 then
16326 return not Is_Value_Type (Typ);
16328 -- Record type
16330 elsif Is_Record_Type (Typ) then
16331 declare
16332 Comp : Entity_Id;
16333 begin
16334 Comp := First_Entity (Typ);
16335 while Present (Comp) loop
16336 if Ekind (Comp) = E_Component
16337 and then Requires_Transient_Scope (Etype (Comp))
16338 then
16339 return True;
16340 else
16341 Next_Entity (Comp);
16342 end if;
16343 end loop;
16344 end;
16346 return False;
16348 -- String literal types never require transient scope
16350 elsif Ekind (Typ) = E_String_Literal_Subtype then
16351 return False;
16353 -- Array type. Note that we already know that this is a constrained
16354 -- array, since unconstrained arrays will fail the indefinite test.
16356 elsif Is_Array_Type (Typ) then
16358 -- If component type requires a transient scope, the array does too
16360 if Requires_Transient_Scope (Component_Type (Typ)) then
16361 return True;
16363 -- Otherwise, we only need a transient scope if the size depends on
16364 -- the value of one or more discriminants.
16366 else
16367 return Size_Depends_On_Discriminant (Typ);
16368 end if;
16370 -- All other cases do not require a transient scope
16372 else
16373 return False;
16374 end if;
16375 end Requires_Transient_Scope;
16377 --------------------------
16378 -- Reset_Analyzed_Flags --
16379 --------------------------
16381 procedure Reset_Analyzed_Flags (N : Node_Id) is
16383 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
16384 -- Function used to reset Analyzed flags in tree. Note that we do
16385 -- not reset Analyzed flags in entities, since there is no need to
16386 -- reanalyze entities, and indeed, it is wrong to do so, since it
16387 -- can result in generating auxiliary stuff more than once.
16389 --------------------
16390 -- Clear_Analyzed --
16391 --------------------
16393 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
16394 begin
16395 if not Has_Extension (N) then
16396 Set_Analyzed (N, False);
16397 end if;
16399 return OK;
16400 end Clear_Analyzed;
16402 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
16404 -- Start of processing for Reset_Analyzed_Flags
16406 begin
16407 Reset_Analyzed (N);
16408 end Reset_Analyzed_Flags;
16410 ------------------------
16411 -- Restore_SPARK_Mode --
16412 ------------------------
16414 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
16415 begin
16416 SPARK_Mode := Mode;
16417 end Restore_SPARK_Mode;
16419 --------------------------------
16420 -- Returns_Unconstrained_Type --
16421 --------------------------------
16423 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
16424 begin
16425 return Ekind (Subp) = E_Function
16426 and then not Is_Scalar_Type (Etype (Subp))
16427 and then not Is_Access_Type (Etype (Subp))
16428 and then not Is_Constrained (Etype (Subp));
16429 end Returns_Unconstrained_Type;
16431 ----------------------------
16432 -- Root_Type_Of_Full_View --
16433 ----------------------------
16435 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
16436 Rtyp : constant Entity_Id := Root_Type (T);
16438 begin
16439 -- The root type of the full view may itself be a private type. Keep
16440 -- looking for the ultimate derivation parent.
16442 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
16443 return Root_Type_Of_Full_View (Full_View (Rtyp));
16444 else
16445 return Rtyp;
16446 end if;
16447 end Root_Type_Of_Full_View;
16449 ---------------------------
16450 -- Safe_To_Capture_Value --
16451 ---------------------------
16453 function Safe_To_Capture_Value
16454 (N : Node_Id;
16455 Ent : Entity_Id;
16456 Cond : Boolean := False) return Boolean
16458 begin
16459 -- The only entities for which we track constant values are variables
16460 -- which are not renamings, constants, out parameters, and in out
16461 -- parameters, so check if we have this case.
16463 -- Note: it may seem odd to track constant values for constants, but in
16464 -- fact this routine is used for other purposes than simply capturing
16465 -- the value. In particular, the setting of Known[_Non]_Null.
16467 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
16468 or else
16469 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
16470 then
16471 null;
16473 -- For conditionals, we also allow loop parameters and all formals,
16474 -- including in parameters.
16476 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
16477 null;
16479 -- For all other cases, not just unsafe, but impossible to capture
16480 -- Current_Value, since the above are the only entities which have
16481 -- Current_Value fields.
16483 else
16484 return False;
16485 end if;
16487 -- Skip if volatile or aliased, since funny things might be going on in
16488 -- these cases which we cannot necessarily track. Also skip any variable
16489 -- for which an address clause is given, or whose address is taken. Also
16490 -- never capture value of library level variables (an attempt to do so
16491 -- can occur in the case of package elaboration code).
16493 if Treat_As_Volatile (Ent)
16494 or else Is_Aliased (Ent)
16495 or else Present (Address_Clause (Ent))
16496 or else Address_Taken (Ent)
16497 or else (Is_Library_Level_Entity (Ent)
16498 and then Ekind (Ent) = E_Variable)
16499 then
16500 return False;
16501 end if;
16503 -- OK, all above conditions are met. We also require that the scope of
16504 -- the reference be the same as the scope of the entity, not counting
16505 -- packages and blocks and loops.
16507 declare
16508 E_Scope : constant Entity_Id := Scope (Ent);
16509 R_Scope : Entity_Id;
16511 begin
16512 R_Scope := Current_Scope;
16513 while R_Scope /= Standard_Standard loop
16514 exit when R_Scope = E_Scope;
16516 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
16517 return False;
16518 else
16519 R_Scope := Scope (R_Scope);
16520 end if;
16521 end loop;
16522 end;
16524 -- We also require that the reference does not appear in a context
16525 -- where it is not sure to be executed (i.e. a conditional context
16526 -- or an exception handler). We skip this if Cond is True, since the
16527 -- capturing of values from conditional tests handles this ok.
16529 if Cond then
16530 return True;
16531 end if;
16533 declare
16534 Desc : Node_Id;
16535 P : Node_Id;
16537 begin
16538 Desc := N;
16540 -- Seems dubious that case expressions are not handled here ???
16542 P := Parent (N);
16543 while Present (P) loop
16544 if Nkind (P) = N_If_Statement
16545 or else Nkind (P) = N_Case_Statement
16546 or else (Nkind (P) in N_Short_Circuit
16547 and then Desc = Right_Opnd (P))
16548 or else (Nkind (P) = N_If_Expression
16549 and then Desc /= First (Expressions (P)))
16550 or else Nkind (P) = N_Exception_Handler
16551 or else Nkind (P) = N_Selective_Accept
16552 or else Nkind (P) = N_Conditional_Entry_Call
16553 or else Nkind (P) = N_Timed_Entry_Call
16554 or else Nkind (P) = N_Asynchronous_Select
16555 then
16556 return False;
16558 else
16559 Desc := P;
16560 P := Parent (P);
16562 -- A special Ada 2012 case: the original node may be part
16563 -- of the else_actions of a conditional expression, in which
16564 -- case it might not have been expanded yet, and appears in
16565 -- a non-syntactic list of actions. In that case it is clearly
16566 -- not safe to save a value.
16568 if No (P)
16569 and then Is_List_Member (Desc)
16570 and then No (Parent (List_Containing (Desc)))
16571 then
16572 return False;
16573 end if;
16574 end if;
16575 end loop;
16576 end;
16578 -- OK, looks safe to set value
16580 return True;
16581 end Safe_To_Capture_Value;
16583 ---------------
16584 -- Same_Name --
16585 ---------------
16587 function Same_Name (N1, N2 : Node_Id) return Boolean is
16588 K1 : constant Node_Kind := Nkind (N1);
16589 K2 : constant Node_Kind := Nkind (N2);
16591 begin
16592 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
16593 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
16594 then
16595 return Chars (N1) = Chars (N2);
16597 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
16598 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
16599 then
16600 return Same_Name (Selector_Name (N1), Selector_Name (N2))
16601 and then Same_Name (Prefix (N1), Prefix (N2));
16603 else
16604 return False;
16605 end if;
16606 end Same_Name;
16608 -----------------
16609 -- Same_Object --
16610 -----------------
16612 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
16613 N1 : constant Node_Id := Original_Node (Node1);
16614 N2 : constant Node_Id := Original_Node (Node2);
16615 -- We do the tests on original nodes, since we are most interested
16616 -- in the original source, not any expansion that got in the way.
16618 K1 : constant Node_Kind := Nkind (N1);
16619 K2 : constant Node_Kind := Nkind (N2);
16621 begin
16622 -- First case, both are entities with same entity
16624 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
16625 declare
16626 EN1 : constant Entity_Id := Entity (N1);
16627 EN2 : constant Entity_Id := Entity (N2);
16628 begin
16629 if Present (EN1) and then Present (EN2)
16630 and then (Ekind_In (EN1, E_Variable, E_Constant)
16631 or else Is_Formal (EN1))
16632 and then EN1 = EN2
16633 then
16634 return True;
16635 end if;
16636 end;
16637 end if;
16639 -- Second case, selected component with same selector, same record
16641 if K1 = N_Selected_Component
16642 and then K2 = N_Selected_Component
16643 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
16644 then
16645 return Same_Object (Prefix (N1), Prefix (N2));
16647 -- Third case, indexed component with same subscripts, same array
16649 elsif K1 = N_Indexed_Component
16650 and then K2 = N_Indexed_Component
16651 and then Same_Object (Prefix (N1), Prefix (N2))
16652 then
16653 declare
16654 E1, E2 : Node_Id;
16655 begin
16656 E1 := First (Expressions (N1));
16657 E2 := First (Expressions (N2));
16658 while Present (E1) loop
16659 if not Same_Value (E1, E2) then
16660 return False;
16661 else
16662 Next (E1);
16663 Next (E2);
16664 end if;
16665 end loop;
16667 return True;
16668 end;
16670 -- Fourth case, slice of same array with same bounds
16672 elsif K1 = N_Slice
16673 and then K2 = N_Slice
16674 and then Nkind (Discrete_Range (N1)) = N_Range
16675 and then Nkind (Discrete_Range (N2)) = N_Range
16676 and then Same_Value (Low_Bound (Discrete_Range (N1)),
16677 Low_Bound (Discrete_Range (N2)))
16678 and then Same_Value (High_Bound (Discrete_Range (N1)),
16679 High_Bound (Discrete_Range (N2)))
16680 then
16681 return Same_Name (Prefix (N1), Prefix (N2));
16683 -- All other cases, not clearly the same object
16685 else
16686 return False;
16687 end if;
16688 end Same_Object;
16690 ---------------
16691 -- Same_Type --
16692 ---------------
16694 function Same_Type (T1, T2 : Entity_Id) return Boolean is
16695 begin
16696 if T1 = T2 then
16697 return True;
16699 elsif not Is_Constrained (T1)
16700 and then not Is_Constrained (T2)
16701 and then Base_Type (T1) = Base_Type (T2)
16702 then
16703 return True;
16705 -- For now don't bother with case of identical constraints, to be
16706 -- fiddled with later on perhaps (this is only used for optimization
16707 -- purposes, so it is not critical to do a best possible job)
16709 else
16710 return False;
16711 end if;
16712 end Same_Type;
16714 ----------------
16715 -- Same_Value --
16716 ----------------
16718 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
16719 begin
16720 if Compile_Time_Known_Value (Node1)
16721 and then Compile_Time_Known_Value (Node2)
16722 and then Expr_Value (Node1) = Expr_Value (Node2)
16723 then
16724 return True;
16725 elsif Same_Object (Node1, Node2) then
16726 return True;
16727 else
16728 return False;
16729 end if;
16730 end Same_Value;
16732 -----------------------------
16733 -- Save_SPARK_Mode_And_Set --
16734 -----------------------------
16736 procedure Save_SPARK_Mode_And_Set
16737 (Context : Entity_Id;
16738 Mode : out SPARK_Mode_Type)
16740 begin
16741 -- Save the current mode in effect
16743 Mode := SPARK_Mode;
16745 -- Do not consider illegal or partially decorated constructs
16747 if Ekind (Context) = E_Void or else Error_Posted (Context) then
16748 null;
16750 elsif Present (SPARK_Pragma (Context)) then
16751 SPARK_Mode := Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Context));
16752 end if;
16753 end Save_SPARK_Mode_And_Set;
16755 -------------------------
16756 -- Scalar_Part_Present --
16757 -------------------------
16759 function Scalar_Part_Present (T : Entity_Id) return Boolean is
16760 C : Entity_Id;
16762 begin
16763 if Is_Scalar_Type (T) then
16764 return True;
16766 elsif Is_Array_Type (T) then
16767 return Scalar_Part_Present (Component_Type (T));
16769 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
16770 C := First_Component_Or_Discriminant (T);
16771 while Present (C) loop
16772 if Scalar_Part_Present (Etype (C)) then
16773 return True;
16774 else
16775 Next_Component_Or_Discriminant (C);
16776 end if;
16777 end loop;
16778 end if;
16780 return False;
16781 end Scalar_Part_Present;
16783 ------------------------
16784 -- Scope_Is_Transient --
16785 ------------------------
16787 function Scope_Is_Transient return Boolean is
16788 begin
16789 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
16790 end Scope_Is_Transient;
16792 ------------------
16793 -- Scope_Within --
16794 ------------------
16796 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
16797 Scop : Entity_Id;
16799 begin
16800 Scop := Scope1;
16801 while Scop /= Standard_Standard loop
16802 Scop := Scope (Scop);
16804 if Scop = Scope2 then
16805 return True;
16806 end if;
16807 end loop;
16809 return False;
16810 end Scope_Within;
16812 --------------------------
16813 -- Scope_Within_Or_Same --
16814 --------------------------
16816 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
16817 Scop : Entity_Id;
16819 begin
16820 Scop := Scope1;
16821 while Scop /= Standard_Standard loop
16822 if Scop = Scope2 then
16823 return True;
16824 else
16825 Scop := Scope (Scop);
16826 end if;
16827 end loop;
16829 return False;
16830 end Scope_Within_Or_Same;
16832 --------------------
16833 -- Set_Convention --
16834 --------------------
16836 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
16837 begin
16838 Basic_Set_Convention (E, Val);
16840 if Is_Type (E)
16841 and then Is_Access_Subprogram_Type (Base_Type (E))
16842 and then Has_Foreign_Convention (E)
16843 then
16844 Set_Can_Use_Internal_Rep (E, False);
16845 end if;
16847 -- If E is an object or component, and the type of E is an anonymous
16848 -- access type with no convention set, then also set the convention of
16849 -- the anonymous access type. We do not do this for anonymous protected
16850 -- types, since protected types always have the default convention.
16852 if Present (Etype (E))
16853 and then (Is_Object (E)
16854 or else Ekind (E) = E_Component
16856 -- Allow E_Void (happens for pragma Convention appearing
16857 -- in the middle of a record applying to a component)
16859 or else Ekind (E) = E_Void)
16860 then
16861 declare
16862 Typ : constant Entity_Id := Etype (E);
16864 begin
16865 if Ekind_In (Typ, E_Anonymous_Access_Type,
16866 E_Anonymous_Access_Subprogram_Type)
16867 and then not Has_Convention_Pragma (Typ)
16868 then
16869 Basic_Set_Convention (Typ, Val);
16870 Set_Has_Convention_Pragma (Typ);
16872 -- And for the access subprogram type, deal similarly with the
16873 -- designated E_Subprogram_Type if it is also internal (which
16874 -- it always is?)
16876 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
16877 declare
16878 Dtype : constant Entity_Id := Designated_Type (Typ);
16879 begin
16880 if Ekind (Dtype) = E_Subprogram_Type
16881 and then Is_Itype (Dtype)
16882 and then not Has_Convention_Pragma (Dtype)
16883 then
16884 Basic_Set_Convention (Dtype, Val);
16885 Set_Has_Convention_Pragma (Dtype);
16886 end if;
16887 end;
16888 end if;
16889 end if;
16890 end;
16891 end if;
16892 end Set_Convention;
16894 ------------------------
16895 -- Set_Current_Entity --
16896 ------------------------
16898 -- The given entity is to be set as the currently visible definition of its
16899 -- associated name (i.e. the Node_Id associated with its name). All we have
16900 -- to do is to get the name from the identifier, and then set the
16901 -- associated Node_Id to point to the given entity.
16903 procedure Set_Current_Entity (E : Entity_Id) is
16904 begin
16905 Set_Name_Entity_Id (Chars (E), E);
16906 end Set_Current_Entity;
16908 ---------------------------
16909 -- Set_Debug_Info_Needed --
16910 ---------------------------
16912 procedure Set_Debug_Info_Needed (T : Entity_Id) is
16914 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
16915 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
16916 -- Used to set debug info in a related node if not set already
16918 --------------------------------------
16919 -- Set_Debug_Info_Needed_If_Not_Set --
16920 --------------------------------------
16922 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
16923 begin
16924 if Present (E) and then not Needs_Debug_Info (E) then
16925 Set_Debug_Info_Needed (E);
16927 -- For a private type, indicate that the full view also needs
16928 -- debug information.
16930 if Is_Type (E)
16931 and then Is_Private_Type (E)
16932 and then Present (Full_View (E))
16933 then
16934 Set_Debug_Info_Needed (Full_View (E));
16935 end if;
16936 end if;
16937 end Set_Debug_Info_Needed_If_Not_Set;
16939 -- Start of processing for Set_Debug_Info_Needed
16941 begin
16942 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
16943 -- indicates that Debug_Info_Needed is never required for the entity.
16944 -- Nothing to do if entity comes from a predefined file. Library files
16945 -- are compiled without debug information, but inlined bodies of these
16946 -- routines may appear in user code, and debug information on them ends
16947 -- up complicating debugging the user code.
16949 if No (T)
16950 or else Debug_Info_Off (T)
16951 then
16952 return;
16954 elsif In_Inlined_Body
16955 and then Is_Predefined_File_Name
16956 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
16957 then
16958 Set_Needs_Debug_Info (T, False);
16959 end if;
16961 -- Set flag in entity itself. Note that we will go through the following
16962 -- circuitry even if the flag is already set on T. That's intentional,
16963 -- it makes sure that the flag will be set in subsidiary entities.
16965 Set_Needs_Debug_Info (T);
16967 -- Set flag on subsidiary entities if not set already
16969 if Is_Object (T) then
16970 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
16972 elsif Is_Type (T) then
16973 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
16975 if Is_Record_Type (T) then
16976 declare
16977 Ent : Entity_Id := First_Entity (T);
16978 begin
16979 while Present (Ent) loop
16980 Set_Debug_Info_Needed_If_Not_Set (Ent);
16981 Next_Entity (Ent);
16982 end loop;
16983 end;
16985 -- For a class wide subtype, we also need debug information
16986 -- for the equivalent type.
16988 if Ekind (T) = E_Class_Wide_Subtype then
16989 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
16990 end if;
16992 elsif Is_Array_Type (T) then
16993 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
16995 declare
16996 Indx : Node_Id := First_Index (T);
16997 begin
16998 while Present (Indx) loop
16999 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
17000 Indx := Next_Index (Indx);
17001 end loop;
17002 end;
17004 -- For a packed array type, we also need debug information for
17005 -- the type used to represent the packed array. Conversely, we
17006 -- also need it for the former if we need it for the latter.
17008 if Is_Packed (T) then
17009 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
17010 end if;
17012 if Is_Packed_Array_Impl_Type (T) then
17013 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
17014 end if;
17016 elsif Is_Access_Type (T) then
17017 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
17019 elsif Is_Private_Type (T) then
17020 Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
17022 elsif Is_Protected_Type (T) then
17023 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
17025 elsif Is_Scalar_Type (T) then
17027 -- If the subrange bounds are materialized by dedicated constant
17028 -- objects, also include them in the debug info to make sure the
17029 -- debugger can properly use them.
17031 if Present (Scalar_Range (T))
17032 and then Nkind (Scalar_Range (T)) = N_Range
17033 then
17034 declare
17035 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
17036 High_Bnd : constant Node_Id := Type_High_Bound (T);
17038 begin
17039 if Is_Entity_Name (Low_Bnd) then
17040 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
17041 end if;
17043 if Is_Entity_Name (High_Bnd) then
17044 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
17045 end if;
17046 end;
17047 end if;
17048 end if;
17049 end if;
17050 end Set_Debug_Info_Needed;
17052 ----------------------------
17053 -- Set_Entity_With_Checks --
17054 ----------------------------
17056 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
17057 Val_Actual : Entity_Id;
17058 Nod : Node_Id;
17059 Post_Node : Node_Id;
17061 begin
17062 -- Unconditionally set the entity
17064 Set_Entity (N, Val);
17066 -- The node to post on is the selector in the case of an expanded name,
17067 -- and otherwise the node itself.
17069 if Nkind (N) = N_Expanded_Name then
17070 Post_Node := Selector_Name (N);
17071 else
17072 Post_Node := N;
17073 end if;
17075 -- Check for violation of No_Fixed_IO
17077 if Restriction_Check_Required (No_Fixed_IO)
17078 and then
17079 ((RTU_Loaded (Ada_Text_IO)
17080 and then (Is_RTE (Val, RE_Decimal_IO)
17081 or else
17082 Is_RTE (Val, RE_Fixed_IO)))
17084 or else
17085 (RTU_Loaded (Ada_Wide_Text_IO)
17086 and then (Is_RTE (Val, RO_WT_Decimal_IO)
17087 or else
17088 Is_RTE (Val, RO_WT_Fixed_IO)))
17090 or else
17091 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
17092 and then (Is_RTE (Val, RO_WW_Decimal_IO)
17093 or else
17094 Is_RTE (Val, RO_WW_Fixed_IO))))
17096 -- A special extra check, don't complain about a reference from within
17097 -- the Ada.Interrupts package itself!
17099 and then not In_Same_Extended_Unit (N, Val)
17100 then
17101 Check_Restriction (No_Fixed_IO, Post_Node);
17102 end if;
17104 -- Remaining checks are only done on source nodes. Note that we test
17105 -- for violation of No_Fixed_IO even on non-source nodes, because the
17106 -- cases for checking violations of this restriction are instantiations
17107 -- where the reference in the instance has Comes_From_Source False.
17109 if not Comes_From_Source (N) then
17110 return;
17111 end if;
17113 -- Check for violation of No_Abort_Statements, which is triggered by
17114 -- call to Ada.Task_Identification.Abort_Task.
17116 if Restriction_Check_Required (No_Abort_Statements)
17117 and then (Is_RTE (Val, RE_Abort_Task))
17119 -- A special extra check, don't complain about a reference from within
17120 -- the Ada.Task_Identification package itself!
17122 and then not In_Same_Extended_Unit (N, Val)
17123 then
17124 Check_Restriction (No_Abort_Statements, Post_Node);
17125 end if;
17127 if Val = Standard_Long_Long_Integer then
17128 Check_Restriction (No_Long_Long_Integers, Post_Node);
17129 end if;
17131 -- Check for violation of No_Dynamic_Attachment
17133 if Restriction_Check_Required (No_Dynamic_Attachment)
17134 and then RTU_Loaded (Ada_Interrupts)
17135 and then (Is_RTE (Val, RE_Is_Reserved) or else
17136 Is_RTE (Val, RE_Is_Attached) or else
17137 Is_RTE (Val, RE_Current_Handler) or else
17138 Is_RTE (Val, RE_Attach_Handler) or else
17139 Is_RTE (Val, RE_Exchange_Handler) or else
17140 Is_RTE (Val, RE_Detach_Handler) or else
17141 Is_RTE (Val, RE_Reference))
17143 -- A special extra check, don't complain about a reference from within
17144 -- the Ada.Interrupts package itself!
17146 and then not In_Same_Extended_Unit (N, Val)
17147 then
17148 Check_Restriction (No_Dynamic_Attachment, Post_Node);
17149 end if;
17151 -- Check for No_Implementation_Identifiers
17153 if Restriction_Check_Required (No_Implementation_Identifiers) then
17155 -- We have an implementation defined entity if it is marked as
17156 -- implementation defined, or is defined in a package marked as
17157 -- implementation defined. However, library packages themselves
17158 -- are excluded (we don't want to flag Interfaces itself, just
17159 -- the entities within it).
17161 if (Is_Implementation_Defined (Val)
17162 or else
17163 (Present (Scope (Val))
17164 and then Is_Implementation_Defined (Scope (Val))))
17165 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
17166 and then Is_Library_Level_Entity (Val))
17167 then
17168 Check_Restriction (No_Implementation_Identifiers, Post_Node);
17169 end if;
17170 end if;
17172 -- Do the style check
17174 if Style_Check
17175 and then not Suppress_Style_Checks (Val)
17176 and then not In_Instance
17177 then
17178 if Nkind (N) = N_Identifier then
17179 Nod := N;
17180 elsif Nkind (N) = N_Expanded_Name then
17181 Nod := Selector_Name (N);
17182 else
17183 return;
17184 end if;
17186 -- A special situation arises for derived operations, where we want
17187 -- to do the check against the parent (since the Sloc of the derived
17188 -- operation points to the derived type declaration itself).
17190 Val_Actual := Val;
17191 while not Comes_From_Source (Val_Actual)
17192 and then Nkind (Val_Actual) in N_Entity
17193 and then (Ekind (Val_Actual) = E_Enumeration_Literal
17194 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
17195 and then Present (Alias (Val_Actual))
17196 loop
17197 Val_Actual := Alias (Val_Actual);
17198 end loop;
17200 -- Renaming declarations for generic actuals do not come from source,
17201 -- and have a different name from that of the entity they rename, so
17202 -- there is no style check to perform here.
17204 if Chars (Nod) = Chars (Val_Actual) then
17205 Style.Check_Identifier (Nod, Val_Actual);
17206 end if;
17207 end if;
17209 Set_Entity (N, Val);
17210 end Set_Entity_With_Checks;
17212 ------------------------
17213 -- Set_Name_Entity_Id --
17214 ------------------------
17216 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
17217 begin
17218 Set_Name_Table_Int (Id, Int (Val));
17219 end Set_Name_Entity_Id;
17221 ---------------------
17222 -- Set_Next_Actual --
17223 ---------------------
17225 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
17226 begin
17227 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
17228 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
17229 end if;
17230 end Set_Next_Actual;
17232 ----------------------------------
17233 -- Set_Optimize_Alignment_Flags --
17234 ----------------------------------
17236 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
17237 begin
17238 if Optimize_Alignment = 'S' then
17239 Set_Optimize_Alignment_Space (E);
17240 elsif Optimize_Alignment = 'T' then
17241 Set_Optimize_Alignment_Time (E);
17242 end if;
17243 end Set_Optimize_Alignment_Flags;
17245 -----------------------
17246 -- Set_Public_Status --
17247 -----------------------
17249 procedure Set_Public_Status (Id : Entity_Id) is
17250 S : constant Entity_Id := Current_Scope;
17252 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
17253 -- Determines if E is defined within handled statement sequence or
17254 -- an if statement, returns True if so, False otherwise.
17256 ----------------------
17257 -- Within_HSS_Or_If --
17258 ----------------------
17260 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
17261 N : Node_Id;
17262 begin
17263 N := Declaration_Node (E);
17264 loop
17265 N := Parent (N);
17267 if No (N) then
17268 return False;
17270 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
17271 N_If_Statement)
17272 then
17273 return True;
17274 end if;
17275 end loop;
17276 end Within_HSS_Or_If;
17278 -- Start of processing for Set_Public_Status
17280 begin
17281 -- Everything in the scope of Standard is public
17283 if S = Standard_Standard then
17284 Set_Is_Public (Id);
17286 -- Entity is definitely not public if enclosing scope is not public
17288 elsif not Is_Public (S) then
17289 return;
17291 -- An object or function declaration that occurs in a handled sequence
17292 -- of statements or within an if statement is the declaration for a
17293 -- temporary object or local subprogram generated by the expander. It
17294 -- never needs to be made public and furthermore, making it public can
17295 -- cause back end problems.
17297 elsif Nkind_In (Parent (Id), N_Object_Declaration,
17298 N_Function_Specification)
17299 and then Within_HSS_Or_If (Id)
17300 then
17301 return;
17303 -- Entities in public packages or records are public
17305 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
17306 Set_Is_Public (Id);
17308 -- The bounds of an entry family declaration can generate object
17309 -- declarations that are visible to the back-end, e.g. in the
17310 -- the declaration of a composite type that contains tasks.
17312 elsif Is_Concurrent_Type (S)
17313 and then not Has_Completion (S)
17314 and then Nkind (Parent (Id)) = N_Object_Declaration
17315 then
17316 Set_Is_Public (Id);
17317 end if;
17318 end Set_Public_Status;
17320 -----------------------------
17321 -- Set_Referenced_Modified --
17322 -----------------------------
17324 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
17325 Pref : Node_Id;
17327 begin
17328 -- Deal with indexed or selected component where prefix is modified
17330 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
17331 Pref := Prefix (N);
17333 -- If prefix is access type, then it is the designated object that is
17334 -- being modified, which means we have no entity to set the flag on.
17336 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
17337 return;
17339 -- Otherwise chase the prefix
17341 else
17342 Set_Referenced_Modified (Pref, Out_Param);
17343 end if;
17345 -- Otherwise see if we have an entity name (only other case to process)
17347 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
17348 Set_Referenced_As_LHS (Entity (N), not Out_Param);
17349 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
17350 end if;
17351 end Set_Referenced_Modified;
17353 ----------------------------
17354 -- Set_Scope_Is_Transient --
17355 ----------------------------
17357 procedure Set_Scope_Is_Transient (V : Boolean := True) is
17358 begin
17359 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
17360 end Set_Scope_Is_Transient;
17362 -------------------
17363 -- Set_Size_Info --
17364 -------------------
17366 procedure Set_Size_Info (T1, T2 : Entity_Id) is
17367 begin
17368 -- We copy Esize, but not RM_Size, since in general RM_Size is
17369 -- subtype specific and does not get inherited by all subtypes.
17371 Set_Esize (T1, Esize (T2));
17372 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
17374 if Is_Discrete_Or_Fixed_Point_Type (T1)
17375 and then
17376 Is_Discrete_Or_Fixed_Point_Type (T2)
17377 then
17378 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
17379 end if;
17381 Set_Alignment (T1, Alignment (T2));
17382 end Set_Size_Info;
17384 --------------------
17385 -- Static_Boolean --
17386 --------------------
17388 function Static_Boolean (N : Node_Id) return Uint is
17389 begin
17390 Analyze_And_Resolve (N, Standard_Boolean);
17392 if N = Error
17393 or else Error_Posted (N)
17394 or else Etype (N) = Any_Type
17395 then
17396 return No_Uint;
17397 end if;
17399 if Is_OK_Static_Expression (N) then
17400 if not Raises_Constraint_Error (N) then
17401 return Expr_Value (N);
17402 else
17403 return No_Uint;
17404 end if;
17406 elsif Etype (N) = Any_Type then
17407 return No_Uint;
17409 else
17410 Flag_Non_Static_Expr
17411 ("static boolean expression required here", N);
17412 return No_Uint;
17413 end if;
17414 end Static_Boolean;
17416 --------------------
17417 -- Static_Integer --
17418 --------------------
17420 function Static_Integer (N : Node_Id) return Uint is
17421 begin
17422 Analyze_And_Resolve (N, Any_Integer);
17424 if N = Error
17425 or else Error_Posted (N)
17426 or else Etype (N) = Any_Type
17427 then
17428 return No_Uint;
17429 end if;
17431 if Is_OK_Static_Expression (N) then
17432 if not Raises_Constraint_Error (N) then
17433 return Expr_Value (N);
17434 else
17435 return No_Uint;
17436 end if;
17438 elsif Etype (N) = Any_Type then
17439 return No_Uint;
17441 else
17442 Flag_Non_Static_Expr
17443 ("static integer expression required here", N);
17444 return No_Uint;
17445 end if;
17446 end Static_Integer;
17448 --------------------------
17449 -- Statically_Different --
17450 --------------------------
17452 function Statically_Different (E1, E2 : Node_Id) return Boolean is
17453 R1 : constant Node_Id := Get_Referenced_Object (E1);
17454 R2 : constant Node_Id := Get_Referenced_Object (E2);
17455 begin
17456 return Is_Entity_Name (R1)
17457 and then Is_Entity_Name (R2)
17458 and then Entity (R1) /= Entity (R2)
17459 and then not Is_Formal (Entity (R1))
17460 and then not Is_Formal (Entity (R2));
17461 end Statically_Different;
17463 --------------------------------------
17464 -- Subject_To_Loop_Entry_Attributes --
17465 --------------------------------------
17467 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
17468 Stmt : Node_Id;
17470 begin
17471 Stmt := N;
17473 -- The expansion mechanism transform a loop subject to at least one
17474 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
17475 -- the conditional part.
17477 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
17478 and then Nkind (Original_Node (N)) = N_Loop_Statement
17479 then
17480 Stmt := Original_Node (N);
17481 end if;
17483 return
17484 Nkind (Stmt) = N_Loop_Statement
17485 and then Present (Identifier (Stmt))
17486 and then Present (Entity (Identifier (Stmt)))
17487 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
17488 end Subject_To_Loop_Entry_Attributes;
17490 -----------------------------
17491 -- Subprogram_Access_Level --
17492 -----------------------------
17494 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
17495 begin
17496 if Present (Alias (Subp)) then
17497 return Subprogram_Access_Level (Alias (Subp));
17498 else
17499 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
17500 end if;
17501 end Subprogram_Access_Level;
17503 -------------------------------
17504 -- Support_Atomic_Primitives --
17505 -------------------------------
17507 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
17508 Size : Int;
17510 begin
17511 -- Verify the alignment of Typ is known
17513 if not Known_Alignment (Typ) then
17514 return False;
17515 end if;
17517 if Known_Static_Esize (Typ) then
17518 Size := UI_To_Int (Esize (Typ));
17520 -- If the Esize (Object_Size) is unknown at compile time, look at the
17521 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
17523 elsif Known_Static_RM_Size (Typ) then
17524 Size := UI_To_Int (RM_Size (Typ));
17526 -- Otherwise, the size is considered to be unknown.
17528 else
17529 return False;
17530 end if;
17532 -- Check that the size of the component is 8, 16, 32 or 64 bits and that
17533 -- Typ is properly aligned.
17535 case Size is
17536 when 8 | 16 | 32 | 64 =>
17537 return Size = UI_To_Int (Alignment (Typ)) * 8;
17538 when others =>
17539 return False;
17540 end case;
17541 end Support_Atomic_Primitives;
17543 -----------------
17544 -- Trace_Scope --
17545 -----------------
17547 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
17548 begin
17549 if Debug_Flag_W then
17550 for J in 0 .. Scope_Stack.Last loop
17551 Write_Str (" ");
17552 end loop;
17554 Write_Str (Msg);
17555 Write_Name (Chars (E));
17556 Write_Str (" from ");
17557 Write_Location (Sloc (N));
17558 Write_Eol;
17559 end if;
17560 end Trace_Scope;
17562 -----------------------
17563 -- Transfer_Entities --
17564 -----------------------
17566 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
17567 procedure Set_Public_Status_Of (Id : Entity_Id);
17568 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
17569 -- Set_Public_Status. If successfull and Id denotes a record type, set
17570 -- the Is_Public attribute of its fields.
17572 --------------------------
17573 -- Set_Public_Status_Of --
17574 --------------------------
17576 procedure Set_Public_Status_Of (Id : Entity_Id) is
17577 Field : Entity_Id;
17579 begin
17580 if not Is_Public (Id) then
17581 Set_Public_Status (Id);
17583 -- When the input entity is a public record type, ensure that all
17584 -- its internal fields are also exposed to the linker. The fields
17585 -- of a class-wide type are never made public.
17587 if Is_Public (Id)
17588 and then Is_Record_Type (Id)
17589 and then not Is_Class_Wide_Type (Id)
17590 then
17591 Field := First_Entity (Id);
17592 while Present (Field) loop
17593 Set_Is_Public (Field);
17594 Next_Entity (Field);
17595 end loop;
17596 end if;
17597 end if;
17598 end Set_Public_Status_Of;
17600 -- Local variables
17602 Full_Id : Entity_Id;
17603 Id : Entity_Id;
17605 -- Start of processing for Transfer_Entities
17607 begin
17608 Id := First_Entity (From);
17610 if Present (Id) then
17612 -- Merge the entity chain of the source scope with that of the
17613 -- destination scope.
17615 if Present (Last_Entity (To)) then
17616 Set_Next_Entity (Last_Entity (To), Id);
17617 else
17618 Set_First_Entity (To, Id);
17619 end if;
17621 Set_Last_Entity (To, Last_Entity (From));
17623 -- Inspect the entities of the source scope and update their Scope
17624 -- attribute.
17626 while Present (Id) loop
17627 Set_Scope (Id, To);
17628 Set_Public_Status_Of (Id);
17630 -- Handle an internally generated full view for a private type
17632 if Is_Private_Type (Id)
17633 and then Present (Full_View (Id))
17634 and then Is_Itype (Full_View (Id))
17635 then
17636 Full_Id := Full_View (Id);
17638 Set_Scope (Full_Id, To);
17639 Set_Public_Status_Of (Full_Id);
17640 end if;
17642 Next_Entity (Id);
17643 end loop;
17645 Set_First_Entity (From, Empty);
17646 Set_Last_Entity (From, Empty);
17647 end if;
17648 end Transfer_Entities;
17650 -----------------------
17651 -- Type_Access_Level --
17652 -----------------------
17654 function Type_Access_Level (Typ : Entity_Id) return Uint is
17655 Btyp : Entity_Id;
17657 begin
17658 Btyp := Base_Type (Typ);
17660 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
17661 -- simply use the level where the type is declared. This is true for
17662 -- stand-alone object declarations, and for anonymous access types
17663 -- associated with components the level is the same as that of the
17664 -- enclosing composite type. However, special treatment is needed for
17665 -- the cases of access parameters, return objects of an anonymous access
17666 -- type, and, in Ada 95, access discriminants of limited types.
17668 if Is_Access_Type (Btyp) then
17669 if Ekind (Btyp) = E_Anonymous_Access_Type then
17671 -- If the type is a nonlocal anonymous access type (such as for
17672 -- an access parameter) we treat it as being declared at the
17673 -- library level to ensure that names such as X.all'access don't
17674 -- fail static accessibility checks.
17676 if not Is_Local_Anonymous_Access (Typ) then
17677 return Scope_Depth (Standard_Standard);
17679 -- If this is a return object, the accessibility level is that of
17680 -- the result subtype of the enclosing function. The test here is
17681 -- little complicated, because we have to account for extended
17682 -- return statements that have been rewritten as blocks, in which
17683 -- case we have to find and the Is_Return_Object attribute of the
17684 -- itype's associated object. It would be nice to find a way to
17685 -- simplify this test, but it doesn't seem worthwhile to add a new
17686 -- flag just for purposes of this test. ???
17688 elsif Ekind (Scope (Btyp)) = E_Return_Statement
17689 or else
17690 (Is_Itype (Btyp)
17691 and then Nkind (Associated_Node_For_Itype (Btyp)) =
17692 N_Object_Declaration
17693 and then Is_Return_Object
17694 (Defining_Identifier
17695 (Associated_Node_For_Itype (Btyp))))
17696 then
17697 declare
17698 Scop : Entity_Id;
17700 begin
17701 Scop := Scope (Scope (Btyp));
17702 while Present (Scop) loop
17703 exit when Ekind (Scop) = E_Function;
17704 Scop := Scope (Scop);
17705 end loop;
17707 -- Treat the return object's type as having the level of the
17708 -- function's result subtype (as per RM05-6.5(5.3/2)).
17710 return Type_Access_Level (Etype (Scop));
17711 end;
17712 end if;
17713 end if;
17715 Btyp := Root_Type (Btyp);
17717 -- The accessibility level of anonymous access types associated with
17718 -- discriminants is that of the current instance of the type, and
17719 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
17721 -- AI-402: access discriminants have accessibility based on the
17722 -- object rather than the type in Ada 2005, so the above paragraph
17723 -- doesn't apply.
17725 -- ??? Needs completion with rules from AI-416
17727 if Ada_Version <= Ada_95
17728 and then Ekind (Typ) = E_Anonymous_Access_Type
17729 and then Present (Associated_Node_For_Itype (Typ))
17730 and then Nkind (Associated_Node_For_Itype (Typ)) =
17731 N_Discriminant_Specification
17732 then
17733 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
17734 end if;
17735 end if;
17737 -- Return library level for a generic formal type. This is done because
17738 -- RM(10.3.2) says that "The statically deeper relationship does not
17739 -- apply to ... a descendant of a generic formal type". Rather than
17740 -- checking at each point where a static accessibility check is
17741 -- performed to see if we are dealing with a formal type, this rule is
17742 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
17743 -- return extreme values for a formal type; Deepest_Type_Access_Level
17744 -- returns Int'Last. By calling the appropriate function from among the
17745 -- two, we ensure that the static accessibility check will pass if we
17746 -- happen to run into a formal type. More specifically, we should call
17747 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
17748 -- call occurs as part of a static accessibility check and the error
17749 -- case is the case where the type's level is too shallow (as opposed
17750 -- to too deep).
17752 if Is_Generic_Type (Root_Type (Btyp)) then
17753 return Scope_Depth (Standard_Standard);
17754 end if;
17756 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
17757 end Type_Access_Level;
17759 ------------------------------------
17760 -- Type_Without_Stream_Operation --
17761 ------------------------------------
17763 function Type_Without_Stream_Operation
17764 (T : Entity_Id;
17765 Op : TSS_Name_Type := TSS_Null) return Entity_Id
17767 BT : constant Entity_Id := Base_Type (T);
17768 Op_Missing : Boolean;
17770 begin
17771 if not Restriction_Active (No_Default_Stream_Attributes) then
17772 return Empty;
17773 end if;
17775 if Is_Elementary_Type (T) then
17776 if Op = TSS_Null then
17777 Op_Missing :=
17778 No (TSS (BT, TSS_Stream_Read))
17779 or else No (TSS (BT, TSS_Stream_Write));
17781 else
17782 Op_Missing := No (TSS (BT, Op));
17783 end if;
17785 if Op_Missing then
17786 return T;
17787 else
17788 return Empty;
17789 end if;
17791 elsif Is_Array_Type (T) then
17792 return Type_Without_Stream_Operation (Component_Type (T), Op);
17794 elsif Is_Record_Type (T) then
17795 declare
17796 Comp : Entity_Id;
17797 C_Typ : Entity_Id;
17799 begin
17800 Comp := First_Component (T);
17801 while Present (Comp) loop
17802 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
17804 if Present (C_Typ) then
17805 return C_Typ;
17806 end if;
17808 Next_Component (Comp);
17809 end loop;
17811 return Empty;
17812 end;
17814 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
17815 return Type_Without_Stream_Operation (Full_View (T), Op);
17816 else
17817 return Empty;
17818 end if;
17819 end Type_Without_Stream_Operation;
17821 ----------------------------
17822 -- Unique_Defining_Entity --
17823 ----------------------------
17825 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
17826 begin
17827 return Unique_Entity (Defining_Entity (N));
17828 end Unique_Defining_Entity;
17830 -------------------
17831 -- Unique_Entity --
17832 -------------------
17834 function Unique_Entity (E : Entity_Id) return Entity_Id is
17835 U : Entity_Id := E;
17836 P : Node_Id;
17838 begin
17839 case Ekind (E) is
17840 when E_Constant =>
17841 if Present (Full_View (E)) then
17842 U := Full_View (E);
17843 end if;
17845 when Type_Kind =>
17846 if Present (Full_View (E)) then
17847 U := Full_View (E);
17848 end if;
17850 when E_Package_Body =>
17851 P := Parent (E);
17853 if Nkind (P) = N_Defining_Program_Unit_Name then
17854 P := Parent (P);
17855 end if;
17857 U := Corresponding_Spec (P);
17859 when E_Subprogram_Body =>
17860 P := Parent (E);
17862 if Nkind (P) = N_Defining_Program_Unit_Name then
17863 P := Parent (P);
17864 end if;
17866 P := Parent (P);
17868 if Nkind (P) = N_Subprogram_Body_Stub then
17869 if Present (Library_Unit (P)) then
17871 -- Get to the function or procedure (generic) entity through
17872 -- the body entity.
17874 U :=
17875 Unique_Entity (Defining_Entity (Get_Body_From_Stub (P)));
17876 end if;
17877 else
17878 U := Corresponding_Spec (P);
17879 end if;
17881 when Formal_Kind =>
17882 if Present (Spec_Entity (E)) then
17883 U := Spec_Entity (E);
17884 end if;
17886 when others =>
17887 null;
17888 end case;
17890 return U;
17891 end Unique_Entity;
17893 -----------------
17894 -- Unique_Name --
17895 -----------------
17897 function Unique_Name (E : Entity_Id) return String is
17899 -- Names of E_Subprogram_Body or E_Package_Body entities are not
17900 -- reliable, as they may not include the overloading suffix. Instead,
17901 -- when looking for the name of E or one of its enclosing scope, we get
17902 -- the name of the corresponding Unique_Entity.
17904 function Get_Scoped_Name (E : Entity_Id) return String;
17905 -- Return the name of E prefixed by all the names of the scopes to which
17906 -- E belongs, except for Standard.
17908 ---------------------
17909 -- Get_Scoped_Name --
17910 ---------------------
17912 function Get_Scoped_Name (E : Entity_Id) return String is
17913 Name : constant String := Get_Name_String (Chars (E));
17914 begin
17915 if Has_Fully_Qualified_Name (E)
17916 or else Scope (E) = Standard_Standard
17917 then
17918 return Name;
17919 else
17920 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
17921 end if;
17922 end Get_Scoped_Name;
17924 -- Start of processing for Unique_Name
17926 begin
17927 if E = Standard_Standard then
17928 return Get_Name_String (Name_Standard);
17930 elsif Scope (E) = Standard_Standard
17931 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
17932 then
17933 return Get_Name_String (Name_Standard) & "__" &
17934 Get_Name_String (Chars (E));
17936 elsif Ekind (E) = E_Enumeration_Literal then
17937 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
17939 else
17940 return Get_Scoped_Name (Unique_Entity (E));
17941 end if;
17942 end Unique_Name;
17944 ---------------------
17945 -- Unit_Is_Visible --
17946 ---------------------
17948 function Unit_Is_Visible (U : Entity_Id) return Boolean is
17949 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
17950 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
17952 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
17953 -- For a child unit, check whether unit appears in a with_clause
17954 -- of a parent.
17956 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
17957 -- Scan the context clause of one compilation unit looking for a
17958 -- with_clause for the unit in question.
17960 ----------------------------
17961 -- Unit_In_Parent_Context --
17962 ----------------------------
17964 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
17965 begin
17966 if Unit_In_Context (Par_Unit) then
17967 return True;
17969 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
17970 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
17972 else
17973 return False;
17974 end if;
17975 end Unit_In_Parent_Context;
17977 ---------------------
17978 -- Unit_In_Context --
17979 ---------------------
17981 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
17982 Clause : Node_Id;
17984 begin
17985 Clause := First (Context_Items (Comp_Unit));
17986 while Present (Clause) loop
17987 if Nkind (Clause) = N_With_Clause then
17988 if Library_Unit (Clause) = U then
17989 return True;
17991 -- The with_clause may denote a renaming of the unit we are
17992 -- looking for, eg. Text_IO which renames Ada.Text_IO.
17994 elsif
17995 Renamed_Entity (Entity (Name (Clause))) =
17996 Defining_Entity (Unit (U))
17997 then
17998 return True;
17999 end if;
18000 end if;
18002 Next (Clause);
18003 end loop;
18005 return False;
18006 end Unit_In_Context;
18008 -- Start of processing for Unit_Is_Visible
18010 begin
18011 -- The currrent unit is directly visible
18013 if Curr = U then
18014 return True;
18016 elsif Unit_In_Context (Curr) then
18017 return True;
18019 -- If the current unit is a body, check the context of the spec
18021 elsif Nkind (Unit (Curr)) = N_Package_Body
18022 or else
18023 (Nkind (Unit (Curr)) = N_Subprogram_Body
18024 and then not Acts_As_Spec (Unit (Curr)))
18025 then
18026 if Unit_In_Context (Library_Unit (Curr)) then
18027 return True;
18028 end if;
18029 end if;
18031 -- If the spec is a child unit, examine the parents
18033 if Is_Child_Unit (Curr_Entity) then
18034 if Nkind (Unit (Curr)) in N_Unit_Body then
18035 return
18036 Unit_In_Parent_Context
18037 (Parent_Spec (Unit (Library_Unit (Curr))));
18038 else
18039 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
18040 end if;
18042 else
18043 return False;
18044 end if;
18045 end Unit_Is_Visible;
18047 ------------------------------
18048 -- Universal_Interpretation --
18049 ------------------------------
18051 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
18052 Index : Interp_Index;
18053 It : Interp;
18055 begin
18056 -- The argument may be a formal parameter of an operator or subprogram
18057 -- with multiple interpretations, or else an expression for an actual.
18059 if Nkind (Opnd) = N_Defining_Identifier
18060 or else not Is_Overloaded (Opnd)
18061 then
18062 if Etype (Opnd) = Universal_Integer
18063 or else Etype (Opnd) = Universal_Real
18064 then
18065 return Etype (Opnd);
18066 else
18067 return Empty;
18068 end if;
18070 else
18071 Get_First_Interp (Opnd, Index, It);
18072 while Present (It.Typ) loop
18073 if It.Typ = Universal_Integer
18074 or else It.Typ = Universal_Real
18075 then
18076 return It.Typ;
18077 end if;
18079 Get_Next_Interp (Index, It);
18080 end loop;
18082 return Empty;
18083 end if;
18084 end Universal_Interpretation;
18086 ---------------
18087 -- Unqualify --
18088 ---------------
18090 function Unqualify (Expr : Node_Id) return Node_Id is
18091 begin
18092 -- Recurse to handle unlikely case of multiple levels of qualification
18094 if Nkind (Expr) = N_Qualified_Expression then
18095 return Unqualify (Expression (Expr));
18097 -- Normal case, not a qualified expression
18099 else
18100 return Expr;
18101 end if;
18102 end Unqualify;
18104 -----------------------
18105 -- Visible_Ancestors --
18106 -----------------------
18108 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
18109 List_1 : Elist_Id;
18110 List_2 : Elist_Id;
18111 Elmt : Elmt_Id;
18113 begin
18114 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
18116 -- Collect all the parents and progenitors of Typ. If the full-view of
18117 -- private parents and progenitors is available then it is used to
18118 -- generate the list of visible ancestors; otherwise their partial
18119 -- view is added to the resulting list.
18121 Collect_Parents
18122 (T => Typ,
18123 List => List_1,
18124 Use_Full_View => True);
18126 Collect_Interfaces
18127 (T => Typ,
18128 Ifaces_List => List_2,
18129 Exclude_Parents => True,
18130 Use_Full_View => True);
18132 -- Join the two lists. Avoid duplications because an interface may
18133 -- simultaneously be parent and progenitor of a type.
18135 Elmt := First_Elmt (List_2);
18136 while Present (Elmt) loop
18137 Append_Unique_Elmt (Node (Elmt), List_1);
18138 Next_Elmt (Elmt);
18139 end loop;
18141 return List_1;
18142 end Visible_Ancestors;
18144 ----------------------
18145 -- Within_Init_Proc --
18146 ----------------------
18148 function Within_Init_Proc return Boolean is
18149 S : Entity_Id;
18151 begin
18152 S := Current_Scope;
18153 while not Is_Overloadable (S) loop
18154 if S = Standard_Standard then
18155 return False;
18156 else
18157 S := Scope (S);
18158 end if;
18159 end loop;
18161 return Is_Init_Proc (S);
18162 end Within_Init_Proc;
18164 ------------------
18165 -- Within_Scope --
18166 ------------------
18168 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
18169 SE : Entity_Id;
18170 begin
18171 SE := Scope (E);
18172 loop
18173 if SE = S then
18174 return True;
18175 elsif SE = Standard_Standard then
18176 return False;
18177 else
18178 SE := Scope (SE);
18179 end if;
18180 end loop;
18181 end Within_Scope;
18183 ----------------
18184 -- Wrong_Type --
18185 ----------------
18187 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
18188 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
18189 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
18191 Matching_Field : Entity_Id;
18192 -- Entity to give a more precise suggestion on how to write a one-
18193 -- element positional aggregate.
18195 function Has_One_Matching_Field return Boolean;
18196 -- Determines if Expec_Type is a record type with a single component or
18197 -- discriminant whose type matches the found type or is one dimensional
18198 -- array whose component type matches the found type. In the case of
18199 -- one discriminant, we ignore the variant parts. That's not accurate,
18200 -- but good enough for the warning.
18202 ----------------------------
18203 -- Has_One_Matching_Field --
18204 ----------------------------
18206 function Has_One_Matching_Field return Boolean is
18207 E : Entity_Id;
18209 begin
18210 Matching_Field := Empty;
18212 if Is_Array_Type (Expec_Type)
18213 and then Number_Dimensions (Expec_Type) = 1
18214 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
18215 then
18216 -- Use type name if available. This excludes multidimensional
18217 -- arrays and anonymous arrays.
18219 if Comes_From_Source (Expec_Type) then
18220 Matching_Field := Expec_Type;
18222 -- For an assignment, use name of target
18224 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
18225 and then Is_Entity_Name (Name (Parent (Expr)))
18226 then
18227 Matching_Field := Entity (Name (Parent (Expr)));
18228 end if;
18230 return True;
18232 elsif not Is_Record_Type (Expec_Type) then
18233 return False;
18235 else
18236 E := First_Entity (Expec_Type);
18237 loop
18238 if No (E) then
18239 return False;
18241 elsif not Ekind_In (E, E_Discriminant, E_Component)
18242 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
18243 then
18244 Next_Entity (E);
18246 else
18247 exit;
18248 end if;
18249 end loop;
18251 if not Covers (Etype (E), Found_Type) then
18252 return False;
18254 elsif Present (Next_Entity (E))
18255 and then (Ekind (E) = E_Component
18256 or else Ekind (Next_Entity (E)) = E_Discriminant)
18257 then
18258 return False;
18260 else
18261 Matching_Field := E;
18262 return True;
18263 end if;
18264 end if;
18265 end Has_One_Matching_Field;
18267 -- Start of processing for Wrong_Type
18269 begin
18270 -- Don't output message if either type is Any_Type, or if a message
18271 -- has already been posted for this node. We need to do the latter
18272 -- check explicitly (it is ordinarily done in Errout), because we
18273 -- are using ! to force the output of the error messages.
18275 if Expec_Type = Any_Type
18276 or else Found_Type = Any_Type
18277 or else Error_Posted (Expr)
18278 then
18279 return;
18281 -- If one of the types is a Taft-Amendment type and the other it its
18282 -- completion, it must be an illegal use of a TAT in the spec, for
18283 -- which an error was already emitted. Avoid cascaded errors.
18285 elsif Is_Incomplete_Type (Expec_Type)
18286 and then Has_Completion_In_Body (Expec_Type)
18287 and then Full_View (Expec_Type) = Etype (Expr)
18288 then
18289 return;
18291 elsif Is_Incomplete_Type (Etype (Expr))
18292 and then Has_Completion_In_Body (Etype (Expr))
18293 and then Full_View (Etype (Expr)) = Expec_Type
18294 then
18295 return;
18297 -- In an instance, there is an ongoing problem with completion of
18298 -- type derived from private types. Their structure is what Gigi
18299 -- expects, but the Etype is the parent type rather than the
18300 -- derived private type itself. Do not flag error in this case. The
18301 -- private completion is an entity without a parent, like an Itype.
18302 -- Similarly, full and partial views may be incorrect in the instance.
18303 -- There is no simple way to insure that it is consistent ???
18305 -- A similar view discrepancy can happen in an inlined body, for the
18306 -- same reason: inserted body may be outside of the original package
18307 -- and only partial views are visible at the point of insertion.
18309 elsif In_Instance or else In_Inlined_Body then
18310 if Etype (Etype (Expr)) = Etype (Expected_Type)
18311 and then
18312 (Has_Private_Declaration (Expected_Type)
18313 or else Has_Private_Declaration (Etype (Expr)))
18314 and then No (Parent (Expected_Type))
18315 then
18316 return;
18318 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
18319 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
18320 then
18321 return;
18323 elsif Is_Private_Type (Expected_Type)
18324 and then Present (Full_View (Expected_Type))
18325 and then Covers (Full_View (Expected_Type), Etype (Expr))
18326 then
18327 return;
18328 end if;
18329 end if;
18331 -- An interesting special check. If the expression is parenthesized
18332 -- and its type corresponds to the type of the sole component of the
18333 -- expected record type, or to the component type of the expected one
18334 -- dimensional array type, then assume we have a bad aggregate attempt.
18336 if Nkind (Expr) in N_Subexpr
18337 and then Paren_Count (Expr) /= 0
18338 and then Has_One_Matching_Field
18339 then
18340 Error_Msg_N ("positional aggregate cannot have one component", Expr);
18341 if Present (Matching_Field) then
18342 if Is_Array_Type (Expec_Type) then
18343 Error_Msg_NE
18344 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
18346 else
18347 Error_Msg_NE
18348 ("\write instead `& ='> ...`", Expr, Matching_Field);
18349 end if;
18350 end if;
18352 -- Another special check, if we are looking for a pool-specific access
18353 -- type and we found an E_Access_Attribute_Type, then we have the case
18354 -- of an Access attribute being used in a context which needs a pool-
18355 -- specific type, which is never allowed. The one extra check we make
18356 -- is that the expected designated type covers the Found_Type.
18358 elsif Is_Access_Type (Expec_Type)
18359 and then Ekind (Found_Type) = E_Access_Attribute_Type
18360 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
18361 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
18362 and then Covers
18363 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
18364 then
18365 Error_Msg_N -- CODEFIX
18366 ("result must be general access type!", Expr);
18367 Error_Msg_NE -- CODEFIX
18368 ("add ALL to }!", Expr, Expec_Type);
18370 -- Another special check, if the expected type is an integer type,
18371 -- but the expression is of type System.Address, and the parent is
18372 -- an addition or subtraction operation whose left operand is the
18373 -- expression in question and whose right operand is of an integral
18374 -- type, then this is an attempt at address arithmetic, so give
18375 -- appropriate message.
18377 elsif Is_Integer_Type (Expec_Type)
18378 and then Is_RTE (Found_Type, RE_Address)
18379 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
18380 and then Expr = Left_Opnd (Parent (Expr))
18381 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
18382 then
18383 Error_Msg_N
18384 ("address arithmetic not predefined in package System",
18385 Parent (Expr));
18386 Error_Msg_N
18387 ("\possible missing with/use of System.Storage_Elements",
18388 Parent (Expr));
18389 return;
18391 -- If the expected type is an anonymous access type, as for access
18392 -- parameters and discriminants, the error is on the designated types.
18394 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
18395 if Comes_From_Source (Expec_Type) then
18396 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18397 else
18398 Error_Msg_NE
18399 ("expected an access type with designated}",
18400 Expr, Designated_Type (Expec_Type));
18401 end if;
18403 if Is_Access_Type (Found_Type)
18404 and then not Comes_From_Source (Found_Type)
18405 then
18406 Error_Msg_NE
18407 ("\\found an access type with designated}!",
18408 Expr, Designated_Type (Found_Type));
18409 else
18410 if From_Limited_With (Found_Type) then
18411 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
18412 Error_Msg_Qual_Level := 99;
18413 Error_Msg_NE -- CODEFIX
18414 ("\\missing `WITH &;", Expr, Scope (Found_Type));
18415 Error_Msg_Qual_Level := 0;
18416 else
18417 Error_Msg_NE ("found}!", Expr, Found_Type);
18418 end if;
18419 end if;
18421 -- Normal case of one type found, some other type expected
18423 else
18424 -- If the names of the two types are the same, see if some number
18425 -- of levels of qualification will help. Don't try more than three
18426 -- levels, and if we get to standard, it's no use (and probably
18427 -- represents an error in the compiler) Also do not bother with
18428 -- internal scope names.
18430 declare
18431 Expec_Scope : Entity_Id;
18432 Found_Scope : Entity_Id;
18434 begin
18435 Expec_Scope := Expec_Type;
18436 Found_Scope := Found_Type;
18438 for Levels in Int range 0 .. 3 loop
18439 if Chars (Expec_Scope) /= Chars (Found_Scope) then
18440 Error_Msg_Qual_Level := Levels;
18441 exit;
18442 end if;
18444 Expec_Scope := Scope (Expec_Scope);
18445 Found_Scope := Scope (Found_Scope);
18447 exit when Expec_Scope = Standard_Standard
18448 or else Found_Scope = Standard_Standard
18449 or else not Comes_From_Source (Expec_Scope)
18450 or else not Comes_From_Source (Found_Scope);
18451 end loop;
18452 end;
18454 if Is_Record_Type (Expec_Type)
18455 and then Present (Corresponding_Remote_Type (Expec_Type))
18456 then
18457 Error_Msg_NE ("expected}!", Expr,
18458 Corresponding_Remote_Type (Expec_Type));
18459 else
18460 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18461 end if;
18463 if Is_Entity_Name (Expr)
18464 and then Is_Package_Or_Generic_Package (Entity (Expr))
18465 then
18466 Error_Msg_N ("\\found package name!", Expr);
18468 elsif Is_Entity_Name (Expr)
18469 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
18470 then
18471 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
18472 Error_Msg_N
18473 ("found procedure name, possibly missing Access attribute!",
18474 Expr);
18475 else
18476 Error_Msg_N
18477 ("\\found procedure name instead of function!", Expr);
18478 end if;
18480 elsif Nkind (Expr) = N_Function_Call
18481 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
18482 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
18483 and then No (Parameter_Associations (Expr))
18484 then
18485 Error_Msg_N
18486 ("found function name, possibly missing Access attribute!",
18487 Expr);
18489 -- Catch common error: a prefix or infix operator which is not
18490 -- directly visible because the type isn't.
18492 elsif Nkind (Expr) in N_Op
18493 and then Is_Overloaded (Expr)
18494 and then not Is_Immediately_Visible (Expec_Type)
18495 and then not Is_Potentially_Use_Visible (Expec_Type)
18496 and then not In_Use (Expec_Type)
18497 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
18498 then
18499 Error_Msg_N
18500 ("operator of the type is not directly visible!", Expr);
18502 elsif Ekind (Found_Type) = E_Void
18503 and then Present (Parent (Found_Type))
18504 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
18505 then
18506 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
18508 else
18509 Error_Msg_NE ("\\found}!", Expr, Found_Type);
18510 end if;
18512 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
18513 -- of the same modular type, and (M1 and M2) = 0 was intended.
18515 if Expec_Type = Standard_Boolean
18516 and then Is_Modular_Integer_Type (Found_Type)
18517 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
18518 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
18519 then
18520 declare
18521 Op : constant Node_Id := Right_Opnd (Parent (Expr));
18522 L : constant Node_Id := Left_Opnd (Op);
18523 R : constant Node_Id := Right_Opnd (Op);
18525 begin
18526 -- The case for the message is when the left operand of the
18527 -- comparison is the same modular type, or when it is an
18528 -- integer literal (or other universal integer expression),
18529 -- which would have been typed as the modular type if the
18530 -- parens had been there.
18532 if (Etype (L) = Found_Type
18533 or else
18534 Etype (L) = Universal_Integer)
18535 and then Is_Integer_Type (Etype (R))
18536 then
18537 Error_Msg_N
18538 ("\\possible missing parens for modular operation", Expr);
18539 end if;
18540 end;
18541 end if;
18543 -- Reset error message qualification indication
18545 Error_Msg_Qual_Level := 0;
18546 end if;
18547 end Wrong_Type;
18549 end Sem_Util;