2015-01-06 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_util.adb
bloba93139e3d1aa4cd43f47dade55dcbae197c6ee2d
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-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Util; use Exp_Util;
36 with Fname; use Fname;
37 with Freeze; use Freeze;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Namet.Sp; use Namet.Sp;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Output; use Output;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Attr; use Sem_Attr;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch13; use Sem_Ch13;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Prag; use Sem_Prag;
55 with Sem_Res; use Sem_Res;
56 with Sem_Warn; use Sem_Warn;
57 with Sem_Type; use Sem_Type;
58 with Sinfo; use Sinfo;
59 with Sinput; use Sinput;
60 with Stand; use Stand;
61 with Style;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Ttypes; use Ttypes;
66 with Uname; use Uname;
68 with GNAT.HTable; use GNAT.HTable;
70 package body Sem_Util is
72 ----------------------------------------
73 -- Global_Variables for New_Copy_Tree --
74 ----------------------------------------
76 -- These global variables are used by New_Copy_Tree. See description of the
77 -- body of this subprogram for details. Global variables can be safely used
78 -- by New_Copy_Tree, since there is no case of a recursive call from the
79 -- processing inside New_Copy_Tree.
81 NCT_Hash_Threshold : constant := 20;
82 -- If there are more than this number of pairs of entries in the map, then
83 -- Hash_Tables_Used will be set, and the hash tables will be initialized
84 -- and used for the searches.
86 NCT_Hash_Tables_Used : Boolean := False;
87 -- Set to True if hash tables are in use
89 NCT_Table_Entries : Nat := 0;
90 -- Count entries in table to see if threshold is reached
92 NCT_Hash_Table_Setup : Boolean := False;
93 -- Set to True if hash table contains data. We set this True if we setup
94 -- the hash table with data, and leave it set permanently from then on,
95 -- this is a signal that second and subsequent users of the hash table
96 -- must clear the old entries before reuse.
98 subtype NCT_Header_Num is Int range 0 .. 511;
99 -- Defines range of headers in hash tables (512 headers)
101 -----------------------
102 -- Local Subprograms --
103 -----------------------
105 function Build_Component_Subtype
106 (C : List_Id;
107 Loc : Source_Ptr;
108 T : Entity_Id) return Node_Id;
109 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
110 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
111 -- Loc is the source location, T is the original subtype.
113 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
114 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
115 -- with discriminants whose default values are static, examine only the
116 -- components in the selected variant to determine whether all of them
117 -- have a default.
119 function Has_Enabled_Property
120 (Item_Id : Entity_Id;
121 Property : Name_Id) return Boolean;
122 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
123 -- Determine whether an abstract state or a variable denoted by entity
124 -- Item_Id has enabled property Property.
126 function Has_Null_Extension (T : Entity_Id) return Boolean;
127 -- T is a derived tagged type. Check whether the type extension is null.
128 -- If the parent type is fully initialized, T can be treated as such.
130 ------------------------------
131 -- Abstract_Interface_List --
132 ------------------------------
134 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
135 Nod : Node_Id;
137 begin
138 if Is_Concurrent_Type (Typ) then
140 -- If we are dealing with a synchronized subtype, go to the base
141 -- type, whose declaration has the interface list.
143 -- Shouldn't this be Declaration_Node???
145 Nod := Parent (Base_Type (Typ));
147 if Nkind (Nod) = N_Full_Type_Declaration then
148 return Empty_List;
149 end if;
151 elsif Ekind (Typ) = E_Record_Type_With_Private then
152 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
153 Nod := Type_Definition (Parent (Typ));
155 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
156 if Present (Full_View (Typ))
157 and then
158 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
159 then
160 Nod := Type_Definition (Parent (Full_View (Typ)));
162 -- If the full-view is not available we cannot do anything else
163 -- here (the source has errors).
165 else
166 return Empty_List;
167 end if;
169 -- Support for generic formals with interfaces is still missing ???
171 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
172 return Empty_List;
174 else
175 pragma Assert
176 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
177 Nod := Parent (Typ);
178 end if;
180 elsif Ekind (Typ) = E_Record_Subtype then
181 Nod := Type_Definition (Parent (Etype (Typ)));
183 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
185 -- Recurse, because parent may still be a private extension. Also
186 -- note that the full view of the subtype or the full view of its
187 -- base type may (both) be unavailable.
189 return Abstract_Interface_List (Etype (Typ));
191 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
192 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
193 Nod := Formal_Type_Definition (Parent (Typ));
194 else
195 Nod := Type_Definition (Parent (Typ));
196 end if;
197 end if;
199 return Interface_List (Nod);
200 end Abstract_Interface_List;
202 --------------------------------
203 -- Add_Access_Type_To_Process --
204 --------------------------------
206 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
207 L : Elist_Id;
209 begin
210 Ensure_Freeze_Node (E);
211 L := Access_Types_To_Process (Freeze_Node (E));
213 if No (L) then
214 L := New_Elmt_List;
215 Set_Access_Types_To_Process (Freeze_Node (E), L);
216 end if;
218 Append_Elmt (A, L);
219 end Add_Access_Type_To_Process;
221 --------------------------
222 -- Add_Block_Identifier --
223 --------------------------
225 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
226 Loc : constant Source_Ptr := Sloc (N);
228 begin
229 pragma Assert (Nkind (N) = N_Block_Statement);
231 -- The block already has a label, return its entity
233 if Present (Identifier (N)) then
234 Id := Entity (Identifier (N));
236 -- Create a new block label and set its attributes
238 else
239 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
240 Set_Etype (Id, Standard_Void_Type);
241 Set_Parent (Id, N);
243 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
244 Set_Block_Node (Id, Identifier (N));
245 end if;
246 end Add_Block_Identifier;
248 -----------------------
249 -- Add_Contract_Item --
250 -----------------------
252 procedure Add_Contract_Item (Prag : Node_Id; Id : Entity_Id) is
253 Items : constant Node_Id := Contract (Id);
255 procedure Add_Classification;
256 -- Prepend Prag to the list of classifications
258 procedure Add_Contract_Test_Case;
259 -- Prepend Prag to the list of contract and test cases
261 procedure Add_Pre_Post_Condition;
262 -- Prepend Prag to the list of pre- and postconditions
264 ------------------------
265 -- Add_Classification --
266 ------------------------
268 procedure Add_Classification is
269 begin
270 Set_Next_Pragma (Prag, Classifications (Items));
271 Set_Classifications (Items, Prag);
272 end Add_Classification;
274 ----------------------------
275 -- Add_Contract_Test_Case --
276 ----------------------------
278 procedure Add_Contract_Test_Case is
279 begin
280 Set_Next_Pragma (Prag, Contract_Test_Cases (Items));
281 Set_Contract_Test_Cases (Items, Prag);
282 end Add_Contract_Test_Case;
284 ----------------------------
285 -- Add_Pre_Post_Condition --
286 ----------------------------
288 procedure Add_Pre_Post_Condition is
289 begin
290 Set_Next_Pragma (Prag, Pre_Post_Conditions (Items));
291 Set_Pre_Post_Conditions (Items, Prag);
292 end Add_Pre_Post_Condition;
294 -- Local variables
296 Nam : Name_Id;
297 PPC : Node_Id;
299 -- Start of processing for Add_Contract_Item
301 begin
302 -- The related context must have a contract and the item to be added
303 -- must be a pragma.
305 pragma Assert (Present (Items));
306 pragma Assert (Nkind (Prag) = N_Pragma);
308 Nam := Original_Aspect_Name (Prag);
310 -- Contract items related to [generic] packages or instantiations. The
311 -- applicable pragmas are:
312 -- Abstract_States
313 -- Initial_Condition
314 -- Initializes
315 -- Part_Of (instantiation only)
317 if Ekind_In (Id, E_Generic_Package, E_Package) then
318 if Nam_In (Nam, Name_Abstract_State,
319 Name_Initial_Condition,
320 Name_Initializes)
321 then
322 Add_Classification;
324 -- Indicator Part_Of must be associated with a package instantiation
326 elsif Nam = Name_Part_Of and then Is_Generic_Instance (Id) then
327 Add_Classification;
329 -- The pragma is not a proper contract item
331 else
332 raise Program_Error;
333 end if;
335 -- Contract items related to package bodies. The applicable pragmas are:
336 -- Refined_States
338 elsif Ekind (Id) = E_Package_Body then
339 if Nam = Name_Refined_State then
340 Add_Classification;
342 -- The pragma is not a proper contract item
344 else
345 raise Program_Error;
346 end if;
348 -- Contract items related to subprogram or entry declarations. The
349 -- applicable pragmas are:
350 -- Contract_Cases
351 -- Depends
352 -- Extensions_Visible
353 -- Global
354 -- Post
355 -- Postcondition
356 -- Pre
357 -- Precondition
358 -- Test_Case
360 elsif Ekind_In (Id, E_Entry, E_Entry_Family)
361 or else Is_Generic_Subprogram (Id)
362 or else Is_Subprogram (Id)
363 then
364 if Nam_In (Nam, Name_Pre,
365 Name_Precondition,
366 Name_uPre,
367 Name_Post,
368 Name_Postcondition,
369 Name_uPost)
370 then
371 -- Before we add a precondition or postcondition to the list, make
372 -- sure we do not have a disallowed duplicate, which can happen if
373 -- we use a pragma for Pre[_Class] or Post[_Class] instead of the
374 -- corresponding aspect.
376 if not From_Aspect_Specification (Prag)
377 and then Nam_In (Nam, Name_Pre,
378 Name_uPre,
379 Name_Post,
380 Name_Post_Class)
381 then
382 PPC := Pre_Post_Conditions (Items);
383 while Present (PPC) loop
384 if not Split_PPC (PPC)
385 and then Original_Aspect_Name (PPC) = Nam
386 then
387 Error_Msg_Sloc := Sloc (PPC);
388 Error_Msg_NE
389 ("duplication of aspect for & given#", Prag, Id);
390 return;
391 end if;
393 PPC := Next_Pragma (PPC);
394 end loop;
395 end if;
397 Add_Pre_Post_Condition;
399 elsif Nam_In (Nam, Name_Contract_Cases, Name_Test_Case) then
400 Add_Contract_Test_Case;
402 elsif Nam_In (Nam, Name_Depends,
403 Name_Extensions_Visible,
404 Name_Global)
405 then
406 Add_Classification;
408 -- The pragma is not a proper contract item
410 else
411 raise Program_Error;
412 end if;
414 -- Contract items related to subprogram bodies. Applicable pragmas are:
415 -- Refined_Depends
416 -- Refined_Global
417 -- Refined_Post
419 elsif Ekind (Id) = E_Subprogram_Body then
420 if Nam_In (Nam, Name_Refined_Depends, Name_Refined_Global) then
421 Add_Classification;
423 elsif Nam = Name_Refined_Post then
424 Add_Pre_Post_Condition;
426 -- The pragma is not a proper contract item
428 else
429 raise Program_Error;
430 end if;
432 -- Contract items related to variables. Applicable pragmas are:
433 -- Async_Readers
434 -- Async_Writers
435 -- Effective_Reads
436 -- Effective_Writes
437 -- Part_Of
439 elsif Ekind (Id) = E_Variable then
440 if Nam_In (Nam, Name_Async_Readers,
441 Name_Async_Writers,
442 Name_Effective_Reads,
443 Name_Effective_Writes,
444 Name_Part_Of)
445 then
446 Add_Classification;
448 -- The pragma is not a proper contract item
450 else
451 raise Program_Error;
452 end if;
453 end if;
454 end Add_Contract_Item;
456 ----------------------------
457 -- Add_Global_Declaration --
458 ----------------------------
460 procedure Add_Global_Declaration (N : Node_Id) is
461 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
463 begin
464 if No (Declarations (Aux_Node)) then
465 Set_Declarations (Aux_Node, New_List);
466 end if;
468 Append_To (Declarations (Aux_Node), N);
469 Analyze (N);
470 end Add_Global_Declaration;
472 --------------------------------
473 -- Address_Integer_Convert_OK --
474 --------------------------------
476 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
477 begin
478 if Allow_Integer_Address
479 and then ((Is_Descendent_Of_Address (T1)
480 and then Is_Private_Type (T1)
481 and then Is_Integer_Type (T2))
482 or else
483 (Is_Descendent_Of_Address (T2)
484 and then Is_Private_Type (T2)
485 and then Is_Integer_Type (T1)))
486 then
487 return True;
488 else
489 return False;
490 end if;
491 end Address_Integer_Convert_OK;
493 -----------------
494 -- Addressable --
495 -----------------
497 -- For now, just 8/16/32/64. but analyze later if AAMP is special???
499 function Addressable (V : Uint) return Boolean is
500 begin
501 return V = Uint_8 or else
502 V = Uint_16 or else
503 V = Uint_32 or else
504 V = Uint_64;
505 end Addressable;
507 function Addressable (V : Int) return Boolean is
508 begin
509 return V = 8 or else
510 V = 16 or else
511 V = 32 or else
512 V = 64;
513 end Addressable;
515 ---------------------------------
516 -- Aggregate_Constraint_Checks --
517 ---------------------------------
519 procedure Aggregate_Constraint_Checks
520 (Exp : Node_Id;
521 Check_Typ : Entity_Id)
523 Exp_Typ : constant Entity_Id := Etype (Exp);
525 begin
526 if Raises_Constraint_Error (Exp) then
527 return;
528 end if;
530 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
531 -- component's type to force the appropriate accessibility checks.
533 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
534 -- type to force the corresponding run-time check
536 if Is_Access_Type (Check_Typ)
537 and then ((Is_Local_Anonymous_Access (Check_Typ))
538 or else (Can_Never_Be_Null (Check_Typ)
539 and then not Can_Never_Be_Null (Exp_Typ)))
540 then
541 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
542 Analyze_And_Resolve (Exp, Check_Typ);
543 Check_Unset_Reference (Exp);
544 end if;
546 -- This is really expansion activity, so make sure that expansion is
547 -- on and is allowed. In GNATprove mode, we also want check flags to
548 -- be added in the tree, so that the formal verification can rely on
549 -- those to be present. In GNATprove mode for formal verification, some
550 -- treatment typically only done during expansion needs to be performed
551 -- on the tree, but it should not be applied inside generics. Otherwise,
552 -- this breaks the name resolution mechanism for generic instances.
554 if not Expander_Active
555 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
556 then
557 return;
558 end if;
560 -- First check if we have to insert discriminant checks
562 if Has_Discriminants (Exp_Typ) then
563 Apply_Discriminant_Check (Exp, Check_Typ);
565 -- Next emit length checks for array aggregates
567 elsif Is_Array_Type (Exp_Typ) then
568 Apply_Length_Check (Exp, Check_Typ);
570 -- Finally emit scalar and string checks. If we are dealing with a
571 -- scalar literal we need to check by hand because the Etype of
572 -- literals is not necessarily correct.
574 elsif Is_Scalar_Type (Exp_Typ)
575 and then Compile_Time_Known_Value (Exp)
576 then
577 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
578 Apply_Compile_Time_Constraint_Error
579 (Exp, "value not in range of}??", CE_Range_Check_Failed,
580 Ent => Base_Type (Check_Typ),
581 Typ => Base_Type (Check_Typ));
583 elsif Is_Out_Of_Range (Exp, Check_Typ) then
584 Apply_Compile_Time_Constraint_Error
585 (Exp, "value not in range of}??", CE_Range_Check_Failed,
586 Ent => Check_Typ,
587 Typ => Check_Typ);
589 elsif not Range_Checks_Suppressed (Check_Typ) then
590 Apply_Scalar_Range_Check (Exp, Check_Typ);
591 end if;
593 -- Verify that target type is also scalar, to prevent view anomalies
594 -- in instantiations.
596 elsif (Is_Scalar_Type (Exp_Typ)
597 or else Nkind (Exp) = N_String_Literal)
598 and then Is_Scalar_Type (Check_Typ)
599 and then Exp_Typ /= Check_Typ
600 then
601 if Is_Entity_Name (Exp)
602 and then Ekind (Entity (Exp)) = E_Constant
603 then
604 -- If expression is a constant, it is worthwhile checking whether
605 -- it is a bound of the type.
607 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
608 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
609 or else
610 (Is_Entity_Name (Type_High_Bound (Check_Typ))
611 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
612 then
613 return;
615 else
616 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
617 Analyze_And_Resolve (Exp, Check_Typ);
618 Check_Unset_Reference (Exp);
619 end if;
621 -- Could use a comment on this case ???
623 else
624 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
625 Analyze_And_Resolve (Exp, Check_Typ);
626 Check_Unset_Reference (Exp);
627 end if;
629 end if;
630 end Aggregate_Constraint_Checks;
632 -----------------------
633 -- Alignment_In_Bits --
634 -----------------------
636 function Alignment_In_Bits (E : Entity_Id) return Uint is
637 begin
638 return Alignment (E) * System_Storage_Unit;
639 end Alignment_In_Bits;
641 ---------------------------------
642 -- Append_Inherited_Subprogram --
643 ---------------------------------
645 procedure Append_Inherited_Subprogram (S : Entity_Id) is
646 Par : constant Entity_Id := Alias (S);
647 -- The parent subprogram
649 Scop : constant Entity_Id := Scope (Par);
650 -- The scope of definition of the parent subprogram
652 Typ : constant Entity_Id := Defining_Entity (Parent (S));
653 -- The derived type of which S is a primitive operation
655 Decl : Node_Id;
656 Next_E : Entity_Id;
658 begin
659 if Ekind (Current_Scope) = E_Package
660 and then In_Private_Part (Current_Scope)
661 and then Has_Private_Declaration (Typ)
662 and then Is_Tagged_Type (Typ)
663 and then Scop = Current_Scope
664 then
665 -- The inherited operation is available at the earliest place after
666 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
667 -- relevant for type extensions. If the parent operation appears
668 -- after the type extension, the operation is not visible.
670 Decl := First
671 (Visible_Declarations
672 (Package_Specification (Current_Scope)));
673 while Present (Decl) loop
674 if Nkind (Decl) = N_Private_Extension_Declaration
675 and then Defining_Entity (Decl) = Typ
676 then
677 if Sloc (Decl) > Sloc (Par) then
678 Next_E := Next_Entity (Par);
679 Set_Next_Entity (Par, S);
680 Set_Next_Entity (S, Next_E);
681 return;
683 else
684 exit;
685 end if;
686 end if;
688 Next (Decl);
689 end loop;
690 end if;
692 -- If partial view is not a type extension, or it appears before the
693 -- subprogram declaration, insert normally at end of entity list.
695 Append_Entity (S, Current_Scope);
696 end Append_Inherited_Subprogram;
698 -----------------------------------------
699 -- Apply_Compile_Time_Constraint_Error --
700 -----------------------------------------
702 procedure Apply_Compile_Time_Constraint_Error
703 (N : Node_Id;
704 Msg : String;
705 Reason : RT_Exception_Code;
706 Ent : Entity_Id := Empty;
707 Typ : Entity_Id := Empty;
708 Loc : Source_Ptr := No_Location;
709 Rep : Boolean := True;
710 Warn : Boolean := False)
712 Stat : constant Boolean := Is_Static_Expression (N);
713 R_Stat : constant Node_Id :=
714 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
715 Rtyp : Entity_Id;
717 begin
718 if No (Typ) then
719 Rtyp := Etype (N);
720 else
721 Rtyp := Typ;
722 end if;
724 Discard_Node
725 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
727 if not Rep then
728 return;
729 end if;
731 -- Now we replace the node by an N_Raise_Constraint_Error node
732 -- This does not need reanalyzing, so set it as analyzed now.
734 Rewrite (N, R_Stat);
735 Set_Analyzed (N, True);
737 Set_Etype (N, Rtyp);
738 Set_Raises_Constraint_Error (N);
740 -- Now deal with possible local raise handling
742 Possible_Local_Raise (N, Standard_Constraint_Error);
744 -- If the original expression was marked as static, the result is
745 -- still marked as static, but the Raises_Constraint_Error flag is
746 -- always set so that further static evaluation is not attempted.
748 if Stat then
749 Set_Is_Static_Expression (N);
750 end if;
751 end Apply_Compile_Time_Constraint_Error;
753 ---------------------------
754 -- Async_Readers_Enabled --
755 ---------------------------
757 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
758 begin
759 return Has_Enabled_Property (Id, Name_Async_Readers);
760 end Async_Readers_Enabled;
762 ---------------------------
763 -- Async_Writers_Enabled --
764 ---------------------------
766 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
767 begin
768 return Has_Enabled_Property (Id, Name_Async_Writers);
769 end Async_Writers_Enabled;
771 --------------------------------------
772 -- Available_Full_View_Of_Component --
773 --------------------------------------
775 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
776 ST : constant Entity_Id := Scope (T);
777 SCT : constant Entity_Id := Scope (Component_Type (T));
778 begin
779 return In_Open_Scopes (ST)
780 and then In_Open_Scopes (SCT)
781 and then Scope_Depth (ST) >= Scope_Depth (SCT);
782 end Available_Full_View_Of_Component;
784 -------------------
785 -- Bad_Attribute --
786 -------------------
788 procedure Bad_Attribute
789 (N : Node_Id;
790 Nam : Name_Id;
791 Warn : Boolean := False)
793 begin
794 Error_Msg_Warn := Warn;
795 Error_Msg_N ("unrecognized attribute&<<", N);
797 -- Check for possible misspelling
799 Error_Msg_Name_1 := First_Attribute_Name;
800 while Error_Msg_Name_1 <= Last_Attribute_Name loop
801 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
802 Error_Msg_N -- CODEFIX
803 ("\possible misspelling of %<<", N);
804 exit;
805 end if;
807 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
808 end loop;
809 end Bad_Attribute;
811 --------------------------------
812 -- Bad_Predicated_Subtype_Use --
813 --------------------------------
815 procedure Bad_Predicated_Subtype_Use
816 (Msg : String;
817 N : Node_Id;
818 Typ : Entity_Id;
819 Suggest_Static : Boolean := False)
821 Gen : Entity_Id;
823 begin
824 -- Avoid cascaded errors
826 if Error_Posted (N) then
827 return;
828 end if;
830 if Inside_A_Generic then
831 Gen := Current_Scope;
832 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
833 Gen := Scope (Gen);
834 end loop;
836 if No (Gen) then
837 return;
838 end if;
840 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
841 Set_No_Predicate_On_Actual (Typ);
842 end if;
844 elsif Has_Predicates (Typ) then
845 if Is_Generic_Actual_Type (Typ) then
847 -- The restriction on loop parameters is only that the type
848 -- should have no dynamic predicates.
850 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
851 and then not Has_Dynamic_Predicate_Aspect (Typ)
852 and then Is_OK_Static_Subtype (Typ)
853 then
854 return;
855 end if;
857 Gen := Current_Scope;
858 while not Is_Generic_Instance (Gen) loop
859 Gen := Scope (Gen);
860 end loop;
862 pragma Assert (Present (Gen));
864 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
865 Error_Msg_Warn := SPARK_Mode /= On;
866 Error_Msg_FE (Msg & "<<", N, Typ);
867 Error_Msg_F ("\Program_Error [<<", N);
869 Insert_Action (N,
870 Make_Raise_Program_Error (Sloc (N),
871 Reason => PE_Bad_Predicated_Generic_Type));
873 else
874 Error_Msg_FE (Msg & "<<", N, Typ);
875 end if;
877 else
878 Error_Msg_FE (Msg, N, Typ);
879 end if;
881 -- Emit an optional suggestion on how to remedy the error if the
882 -- context warrants it.
884 if Suggest_Static and then Has_Static_Predicate (Typ) then
885 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
886 end if;
887 end if;
888 end Bad_Predicated_Subtype_Use;
890 -----------------------------------------
891 -- Bad_Unordered_Enumeration_Reference --
892 -----------------------------------------
894 function Bad_Unordered_Enumeration_Reference
895 (N : Node_Id;
896 T : Entity_Id) return Boolean
898 begin
899 return Is_Enumeration_Type (T)
900 and then Warn_On_Unordered_Enumeration_Type
901 and then not Is_Generic_Type (T)
902 and then Comes_From_Source (N)
903 and then not Has_Pragma_Ordered (T)
904 and then not In_Same_Extended_Unit (N, T);
905 end Bad_Unordered_Enumeration_Reference;
907 --------------------------
908 -- Build_Actual_Subtype --
909 --------------------------
911 function Build_Actual_Subtype
912 (T : Entity_Id;
913 N : Node_Or_Entity_Id) return Node_Id
915 Loc : Source_Ptr;
916 -- Normally Sloc (N), but may point to corresponding body in some cases
918 Constraints : List_Id;
919 Decl : Node_Id;
920 Discr : Entity_Id;
921 Hi : Node_Id;
922 Lo : Node_Id;
923 Subt : Entity_Id;
924 Disc_Type : Entity_Id;
925 Obj : Node_Id;
927 begin
928 Loc := Sloc (N);
930 if Nkind (N) = N_Defining_Identifier then
931 Obj := New_Occurrence_Of (N, Loc);
933 -- If this is a formal parameter of a subprogram declaration, and
934 -- we are compiling the body, we want the declaration for the
935 -- actual subtype to carry the source position of the body, to
936 -- prevent anomalies in gdb when stepping through the code.
938 if Is_Formal (N) then
939 declare
940 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
941 begin
942 if Nkind (Decl) = N_Subprogram_Declaration
943 and then Present (Corresponding_Body (Decl))
944 then
945 Loc := Sloc (Corresponding_Body (Decl));
946 end if;
947 end;
948 end if;
950 else
951 Obj := N;
952 end if;
954 if Is_Array_Type (T) then
955 Constraints := New_List;
956 for J in 1 .. Number_Dimensions (T) loop
958 -- Build an array subtype declaration with the nominal subtype and
959 -- the bounds of the actual. Add the declaration in front of the
960 -- local declarations for the subprogram, for analysis before any
961 -- reference to the formal in the body.
963 Lo :=
964 Make_Attribute_Reference (Loc,
965 Prefix =>
966 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
967 Attribute_Name => Name_First,
968 Expressions => New_List (
969 Make_Integer_Literal (Loc, J)));
971 Hi :=
972 Make_Attribute_Reference (Loc,
973 Prefix =>
974 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
975 Attribute_Name => Name_Last,
976 Expressions => New_List (
977 Make_Integer_Literal (Loc, J)));
979 Append (Make_Range (Loc, Lo, Hi), Constraints);
980 end loop;
982 -- If the type has unknown discriminants there is no constrained
983 -- subtype to build. This is never called for a formal or for a
984 -- lhs, so returning the type is ok ???
986 elsif Has_Unknown_Discriminants (T) then
987 return T;
989 else
990 Constraints := New_List;
992 -- Type T is a generic derived type, inherit the discriminants from
993 -- the parent type.
995 if Is_Private_Type (T)
996 and then No (Full_View (T))
998 -- T was flagged as an error if it was declared as a formal
999 -- derived type with known discriminants. In this case there
1000 -- is no need to look at the parent type since T already carries
1001 -- its own discriminants.
1003 and then not Error_Posted (T)
1004 then
1005 Disc_Type := Etype (Base_Type (T));
1006 else
1007 Disc_Type := T;
1008 end if;
1010 Discr := First_Discriminant (Disc_Type);
1011 while Present (Discr) loop
1012 Append_To (Constraints,
1013 Make_Selected_Component (Loc,
1014 Prefix =>
1015 Duplicate_Subexpr_No_Checks (Obj),
1016 Selector_Name => New_Occurrence_Of (Discr, Loc)));
1017 Next_Discriminant (Discr);
1018 end loop;
1019 end if;
1021 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
1022 Set_Is_Internal (Subt);
1024 Decl :=
1025 Make_Subtype_Declaration (Loc,
1026 Defining_Identifier => Subt,
1027 Subtype_Indication =>
1028 Make_Subtype_Indication (Loc,
1029 Subtype_Mark => New_Occurrence_Of (T, Loc),
1030 Constraint =>
1031 Make_Index_Or_Discriminant_Constraint (Loc,
1032 Constraints => Constraints)));
1034 Mark_Rewrite_Insertion (Decl);
1035 return Decl;
1036 end Build_Actual_Subtype;
1038 ---------------------------------------
1039 -- Build_Actual_Subtype_Of_Component --
1040 ---------------------------------------
1042 function Build_Actual_Subtype_Of_Component
1043 (T : Entity_Id;
1044 N : Node_Id) return Node_Id
1046 Loc : constant Source_Ptr := Sloc (N);
1047 P : constant Node_Id := Prefix (N);
1048 D : Elmt_Id;
1049 Id : Node_Id;
1050 Index_Typ : Entity_Id;
1052 Desig_Typ : Entity_Id;
1053 -- This is either a copy of T, or if T is an access type, then it is
1054 -- the directly designated type of this access type.
1056 function Build_Actual_Array_Constraint return List_Id;
1057 -- If one or more of the bounds of the component depends on
1058 -- discriminants, build actual constraint using the discriminants
1059 -- of the prefix.
1061 function Build_Actual_Record_Constraint return List_Id;
1062 -- Similar to previous one, for discriminated components constrained
1063 -- by the discriminant of the enclosing object.
1065 -----------------------------------
1066 -- Build_Actual_Array_Constraint --
1067 -----------------------------------
1069 function Build_Actual_Array_Constraint return List_Id is
1070 Constraints : constant List_Id := New_List;
1071 Indx : Node_Id;
1072 Hi : Node_Id;
1073 Lo : Node_Id;
1074 Old_Hi : Node_Id;
1075 Old_Lo : Node_Id;
1077 begin
1078 Indx := First_Index (Desig_Typ);
1079 while Present (Indx) loop
1080 Old_Lo := Type_Low_Bound (Etype (Indx));
1081 Old_Hi := Type_High_Bound (Etype (Indx));
1083 if Denotes_Discriminant (Old_Lo) then
1084 Lo :=
1085 Make_Selected_Component (Loc,
1086 Prefix => New_Copy_Tree (P),
1087 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1089 else
1090 Lo := New_Copy_Tree (Old_Lo);
1092 -- The new bound will be reanalyzed in the enclosing
1093 -- declaration. For literal bounds that come from a type
1094 -- declaration, the type of the context must be imposed, so
1095 -- insure that analysis will take place. For non-universal
1096 -- types this is not strictly necessary.
1098 Set_Analyzed (Lo, False);
1099 end if;
1101 if Denotes_Discriminant (Old_Hi) then
1102 Hi :=
1103 Make_Selected_Component (Loc,
1104 Prefix => New_Copy_Tree (P),
1105 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1107 else
1108 Hi := New_Copy_Tree (Old_Hi);
1109 Set_Analyzed (Hi, False);
1110 end if;
1112 Append (Make_Range (Loc, Lo, Hi), Constraints);
1113 Next_Index (Indx);
1114 end loop;
1116 return Constraints;
1117 end Build_Actual_Array_Constraint;
1119 ------------------------------------
1120 -- Build_Actual_Record_Constraint --
1121 ------------------------------------
1123 function Build_Actual_Record_Constraint return List_Id is
1124 Constraints : constant List_Id := New_List;
1125 D : Elmt_Id;
1126 D_Val : Node_Id;
1128 begin
1129 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1130 while Present (D) loop
1131 if Denotes_Discriminant (Node (D)) then
1132 D_Val := Make_Selected_Component (Loc,
1133 Prefix => New_Copy_Tree (P),
1134 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1136 else
1137 D_Val := New_Copy_Tree (Node (D));
1138 end if;
1140 Append (D_Val, Constraints);
1141 Next_Elmt (D);
1142 end loop;
1144 return Constraints;
1145 end Build_Actual_Record_Constraint;
1147 -- Start of processing for Build_Actual_Subtype_Of_Component
1149 begin
1150 -- Why the test for Spec_Expression mode here???
1152 if In_Spec_Expression then
1153 return Empty;
1155 -- More comments for the rest of this body would be good ???
1157 elsif Nkind (N) = N_Explicit_Dereference then
1158 if Is_Composite_Type (T)
1159 and then not Is_Constrained (T)
1160 and then not (Is_Class_Wide_Type (T)
1161 and then Is_Constrained (Root_Type (T)))
1162 and then not Has_Unknown_Discriminants (T)
1163 then
1164 -- If the type of the dereference is already constrained, it is an
1165 -- actual subtype.
1167 if Is_Array_Type (Etype (N))
1168 and then Is_Constrained (Etype (N))
1169 then
1170 return Empty;
1171 else
1172 Remove_Side_Effects (P);
1173 return Build_Actual_Subtype (T, N);
1174 end if;
1175 else
1176 return Empty;
1177 end if;
1178 end if;
1180 if Ekind (T) = E_Access_Subtype then
1181 Desig_Typ := Designated_Type (T);
1182 else
1183 Desig_Typ := T;
1184 end if;
1186 if Ekind (Desig_Typ) = E_Array_Subtype then
1187 Id := First_Index (Desig_Typ);
1188 while Present (Id) loop
1189 Index_Typ := Underlying_Type (Etype (Id));
1191 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1192 or else
1193 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1194 then
1195 Remove_Side_Effects (P);
1196 return
1197 Build_Component_Subtype
1198 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1199 end if;
1201 Next_Index (Id);
1202 end loop;
1204 elsif Is_Composite_Type (Desig_Typ)
1205 and then Has_Discriminants (Desig_Typ)
1206 and then not Has_Unknown_Discriminants (Desig_Typ)
1207 then
1208 if Is_Private_Type (Desig_Typ)
1209 and then No (Discriminant_Constraint (Desig_Typ))
1210 then
1211 Desig_Typ := Full_View (Desig_Typ);
1212 end if;
1214 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1215 while Present (D) loop
1216 if Denotes_Discriminant (Node (D)) then
1217 Remove_Side_Effects (P);
1218 return
1219 Build_Component_Subtype (
1220 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1221 end if;
1223 Next_Elmt (D);
1224 end loop;
1225 end if;
1227 -- If none of the above, the actual and nominal subtypes are the same
1229 return Empty;
1230 end Build_Actual_Subtype_Of_Component;
1232 -----------------------------
1233 -- Build_Component_Subtype --
1234 -----------------------------
1236 function Build_Component_Subtype
1237 (C : List_Id;
1238 Loc : Source_Ptr;
1239 T : Entity_Id) return Node_Id
1241 Subt : Entity_Id;
1242 Decl : Node_Id;
1244 begin
1245 -- Unchecked_Union components do not require component subtypes
1247 if Is_Unchecked_Union (T) then
1248 return Empty;
1249 end if;
1251 Subt := Make_Temporary (Loc, 'S');
1252 Set_Is_Internal (Subt);
1254 Decl :=
1255 Make_Subtype_Declaration (Loc,
1256 Defining_Identifier => Subt,
1257 Subtype_Indication =>
1258 Make_Subtype_Indication (Loc,
1259 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1260 Constraint =>
1261 Make_Index_Or_Discriminant_Constraint (Loc,
1262 Constraints => C)));
1264 Mark_Rewrite_Insertion (Decl);
1265 return Decl;
1266 end Build_Component_Subtype;
1268 ----------------------------------
1269 -- Build_Default_Init_Cond_Call --
1270 ----------------------------------
1272 function Build_Default_Init_Cond_Call
1273 (Loc : Source_Ptr;
1274 Obj_Id : Entity_Id;
1275 Typ : Entity_Id) return Node_Id
1277 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1278 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1280 begin
1281 return
1282 Make_Procedure_Call_Statement (Loc,
1283 Name => New_Occurrence_Of (Proc_Id, Loc),
1284 Parameter_Associations => New_List (
1285 Make_Unchecked_Type_Conversion (Loc,
1286 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1287 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1288 end Build_Default_Init_Cond_Call;
1290 ----------------------------------------------
1291 -- Build_Default_Init_Cond_Procedure_Bodies --
1292 ----------------------------------------------
1294 procedure Build_Default_Init_Cond_Procedure_Bodies (Priv_Decls : List_Id) is
1295 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id);
1296 -- If type Typ is subject to pragma Default_Initial_Condition, build the
1297 -- body of the procedure which verifies the assumption of the pragma at
1298 -- run time. The generated body is added after the type declaration.
1300 --------------------------------------------
1301 -- Build_Default_Init_Cond_Procedure_Body --
1302 --------------------------------------------
1304 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id) is
1305 Param_Id : Entity_Id;
1306 -- The entity of the sole formal parameter of the default initial
1307 -- condition procedure.
1309 procedure Replace_Type_Reference (N : Node_Id);
1310 -- Replace a single reference to type Typ with a reference to formal
1311 -- parameter Param_Id.
1313 ----------------------------
1314 -- Replace_Type_Reference --
1315 ----------------------------
1317 procedure Replace_Type_Reference (N : Node_Id) is
1318 begin
1319 Rewrite (N, New_Occurrence_Of (Param_Id, Sloc (N)));
1320 end Replace_Type_Reference;
1322 procedure Replace_Type_References is
1323 new Replace_Type_References_Generic (Replace_Type_Reference);
1325 -- Local variables
1327 Loc : constant Source_Ptr := Sloc (Typ);
1328 Prag : constant Node_Id :=
1329 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1330 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1331 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Proc_Id);
1332 Body_Decl : Node_Id;
1333 Expr : Node_Id;
1334 Stmt : Node_Id;
1336 -- Start of processing for Build_Default_Init_Cond_Procedure_Body
1338 begin
1339 -- The procedure should be generated only for [sub]types subject to
1340 -- pragma Default_Initial_Condition. Types that inherit the pragma do
1341 -- not get this specialized procedure.
1343 pragma Assert (Has_Default_Init_Cond (Typ));
1344 pragma Assert (Present (Prag));
1345 pragma Assert (Present (Proc_Id));
1347 -- Nothing to do if the body was already built
1349 if Present (Corresponding_Body (Spec_Decl)) then
1350 return;
1351 end if;
1353 Param_Id := First_Formal (Proc_Id);
1355 -- The pragma has an argument. Note that the argument is analyzed
1356 -- after all references to the current instance of the type are
1357 -- replaced.
1359 if Present (Pragma_Argument_Associations (Prag)) then
1360 Expr :=
1361 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
1363 if Nkind (Expr) = N_Null then
1364 Stmt := Make_Null_Statement (Loc);
1366 -- Preserve the original argument of the pragma by replicating it.
1367 -- Replace all references to the current instance of the type with
1368 -- references to the formal parameter.
1370 else
1371 Expr := New_Copy_Tree (Expr);
1372 Replace_Type_References (Expr, Typ);
1374 -- Generate:
1375 -- pragma Check (Default_Initial_Condition, <Expr>);
1377 Stmt :=
1378 Make_Pragma (Loc,
1379 Pragma_Identifier =>
1380 Make_Identifier (Loc, Name_Check),
1382 Pragma_Argument_Associations => New_List (
1383 Make_Pragma_Argument_Association (Loc,
1384 Expression =>
1385 Make_Identifier (Loc,
1386 Chars => Name_Default_Initial_Condition)),
1387 Make_Pragma_Argument_Association (Loc,
1388 Expression => Expr)));
1389 end if;
1391 -- Otherwise the pragma appears without an argument
1393 else
1394 Stmt := Make_Null_Statement (Loc);
1395 end if;
1397 -- Generate:
1398 -- procedure <Typ>Default_Init_Cond (I : <Typ>) is
1399 -- begin
1400 -- <Stmt>;
1401 -- end <Typ>Default_Init_Cond;
1403 Body_Decl :=
1404 Make_Subprogram_Body (Loc,
1405 Specification =>
1406 Copy_Separate_Tree (Specification (Spec_Decl)),
1407 Declarations => Empty_List,
1408 Handled_Statement_Sequence =>
1409 Make_Handled_Sequence_Of_Statements (Loc,
1410 Statements => New_List (Stmt)));
1412 -- Link the spec and body of the default initial condition procedure
1413 -- to prevent the generation of a duplicate body.
1415 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
1416 Set_Corresponding_Spec (Body_Decl, Proc_Id);
1418 Insert_After_And_Analyze (Declaration_Node (Typ), Body_Decl);
1419 end Build_Default_Init_Cond_Procedure_Body;
1421 -- Local variables
1423 Decl : Node_Id;
1424 Typ : Entity_Id;
1426 -- Start of processing for Build_Default_Init_Cond_Procedure_Bodies
1428 begin
1429 -- Inspect the private declarations looking for [sub]type declarations
1431 Decl := First (Priv_Decls);
1432 while Present (Decl) loop
1433 if Nkind_In (Decl, N_Full_Type_Declaration,
1434 N_Subtype_Declaration)
1435 then
1436 Typ := Defining_Entity (Decl);
1438 -- Guard against partially decorate types due to previous errors
1440 if Is_Type (Typ) then
1442 -- If the type is subject to pragma Default_Initial_Condition,
1443 -- generate the body of the internal procedure which verifies
1444 -- the assertion of the pragma at run time.
1446 if Has_Default_Init_Cond (Typ) then
1447 Build_Default_Init_Cond_Procedure_Body (Typ);
1449 -- A derived type inherits the default initial condition
1450 -- procedure from its parent type.
1452 elsif Has_Inherited_Default_Init_Cond (Typ) then
1453 Inherit_Default_Init_Cond_Procedure (Typ);
1454 end if;
1455 end if;
1456 end if;
1458 Next (Decl);
1459 end loop;
1460 end Build_Default_Init_Cond_Procedure_Bodies;
1462 ---------------------------------------------------
1463 -- Build_Default_Init_Cond_Procedure_Declaration --
1464 ---------------------------------------------------
1466 procedure Build_Default_Init_Cond_Procedure_Declaration (Typ : Entity_Id) is
1467 Loc : constant Source_Ptr := Sloc (Typ);
1468 Prag : constant Node_Id :=
1469 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1470 Proc_Id : Entity_Id;
1472 begin
1473 -- The procedure should be generated only for types subject to pragma
1474 -- Default_Initial_Condition. Types that inherit the pragma do not get
1475 -- this specialized procedure.
1477 pragma Assert (Has_Default_Init_Cond (Typ));
1478 pragma Assert (Present (Prag));
1480 -- Nothing to do if default initial condition procedure already built
1482 if Present (Default_Init_Cond_Procedure (Typ)) then
1483 return;
1484 end if;
1486 Proc_Id :=
1487 Make_Defining_Identifier (Loc,
1488 Chars => New_External_Name (Chars (Typ), "Default_Init_Cond"));
1490 -- Associate default initial condition procedure with the private type
1492 Set_Ekind (Proc_Id, E_Procedure);
1493 Set_Is_Default_Init_Cond_Procedure (Proc_Id);
1494 Set_Default_Init_Cond_Procedure (Typ, Proc_Id);
1496 -- Generate:
1497 -- procedure <Typ>Default_Init_Cond (Inn : <Typ>);
1499 Insert_After_And_Analyze (Prag,
1500 Make_Subprogram_Declaration (Loc,
1501 Specification =>
1502 Make_Procedure_Specification (Loc,
1503 Defining_Unit_Name => Proc_Id,
1504 Parameter_Specifications => New_List (
1505 Make_Parameter_Specification (Loc,
1506 Defining_Identifier => Make_Temporary (Loc, 'I'),
1507 Parameter_Type => New_Occurrence_Of (Typ, Loc))))));
1508 end Build_Default_Init_Cond_Procedure_Declaration;
1510 ---------------------------
1511 -- Build_Default_Subtype --
1512 ---------------------------
1514 function Build_Default_Subtype
1515 (T : Entity_Id;
1516 N : Node_Id) return Entity_Id
1518 Loc : constant Source_Ptr := Sloc (N);
1519 Disc : Entity_Id;
1521 Bas : Entity_Id;
1522 -- The base type that is to be constrained by the defaults
1524 begin
1525 if not Has_Discriminants (T) or else Is_Constrained (T) then
1526 return T;
1527 end if;
1529 Bas := Base_Type (T);
1531 -- If T is non-private but its base type is private, this is the
1532 -- completion of a subtype declaration whose parent type is private
1533 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1534 -- are to be found in the full view of the base. Check that the private
1535 -- status of T and its base differ.
1537 if Is_Private_Type (Bas)
1538 and then not Is_Private_Type (T)
1539 and then Present (Full_View (Bas))
1540 then
1541 Bas := Full_View (Bas);
1542 end if;
1544 Disc := First_Discriminant (T);
1546 if No (Discriminant_Default_Value (Disc)) then
1547 return T;
1548 end if;
1550 declare
1551 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1552 Constraints : constant List_Id := New_List;
1553 Decl : Node_Id;
1555 begin
1556 while Present (Disc) loop
1557 Append_To (Constraints,
1558 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1559 Next_Discriminant (Disc);
1560 end loop;
1562 Decl :=
1563 Make_Subtype_Declaration (Loc,
1564 Defining_Identifier => Act,
1565 Subtype_Indication =>
1566 Make_Subtype_Indication (Loc,
1567 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1568 Constraint =>
1569 Make_Index_Or_Discriminant_Constraint (Loc,
1570 Constraints => Constraints)));
1572 Insert_Action (N, Decl);
1573 Analyze (Decl);
1574 return Act;
1575 end;
1576 end Build_Default_Subtype;
1578 --------------------------------------------
1579 -- Build_Discriminal_Subtype_Of_Component --
1580 --------------------------------------------
1582 function Build_Discriminal_Subtype_Of_Component
1583 (T : Entity_Id) return Node_Id
1585 Loc : constant Source_Ptr := Sloc (T);
1586 D : Elmt_Id;
1587 Id : Node_Id;
1589 function Build_Discriminal_Array_Constraint return List_Id;
1590 -- If one or more of the bounds of the component depends on
1591 -- discriminants, build actual constraint using the discriminants
1592 -- of the prefix.
1594 function Build_Discriminal_Record_Constraint return List_Id;
1595 -- Similar to previous one, for discriminated components constrained by
1596 -- the discriminant of the enclosing object.
1598 ----------------------------------------
1599 -- Build_Discriminal_Array_Constraint --
1600 ----------------------------------------
1602 function Build_Discriminal_Array_Constraint return List_Id is
1603 Constraints : constant List_Id := New_List;
1604 Indx : Node_Id;
1605 Hi : Node_Id;
1606 Lo : Node_Id;
1607 Old_Hi : Node_Id;
1608 Old_Lo : Node_Id;
1610 begin
1611 Indx := First_Index (T);
1612 while Present (Indx) loop
1613 Old_Lo := Type_Low_Bound (Etype (Indx));
1614 Old_Hi := Type_High_Bound (Etype (Indx));
1616 if Denotes_Discriminant (Old_Lo) then
1617 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1619 else
1620 Lo := New_Copy_Tree (Old_Lo);
1621 end if;
1623 if Denotes_Discriminant (Old_Hi) then
1624 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1626 else
1627 Hi := New_Copy_Tree (Old_Hi);
1628 end if;
1630 Append (Make_Range (Loc, Lo, Hi), Constraints);
1631 Next_Index (Indx);
1632 end loop;
1634 return Constraints;
1635 end Build_Discriminal_Array_Constraint;
1637 -----------------------------------------
1638 -- Build_Discriminal_Record_Constraint --
1639 -----------------------------------------
1641 function Build_Discriminal_Record_Constraint return List_Id is
1642 Constraints : constant List_Id := New_List;
1643 D : Elmt_Id;
1644 D_Val : Node_Id;
1646 begin
1647 D := First_Elmt (Discriminant_Constraint (T));
1648 while Present (D) loop
1649 if Denotes_Discriminant (Node (D)) then
1650 D_Val :=
1651 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1652 else
1653 D_Val := New_Copy_Tree (Node (D));
1654 end if;
1656 Append (D_Val, Constraints);
1657 Next_Elmt (D);
1658 end loop;
1660 return Constraints;
1661 end Build_Discriminal_Record_Constraint;
1663 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1665 begin
1666 if Ekind (T) = E_Array_Subtype then
1667 Id := First_Index (T);
1668 while Present (Id) loop
1669 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1670 or else
1671 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1672 then
1673 return Build_Component_Subtype
1674 (Build_Discriminal_Array_Constraint, Loc, T);
1675 end if;
1677 Next_Index (Id);
1678 end loop;
1680 elsif Ekind (T) = E_Record_Subtype
1681 and then Has_Discriminants (T)
1682 and then not Has_Unknown_Discriminants (T)
1683 then
1684 D := First_Elmt (Discriminant_Constraint (T));
1685 while Present (D) loop
1686 if Denotes_Discriminant (Node (D)) then
1687 return Build_Component_Subtype
1688 (Build_Discriminal_Record_Constraint, Loc, T);
1689 end if;
1691 Next_Elmt (D);
1692 end loop;
1693 end if;
1695 -- If none of the above, the actual and nominal subtypes are the same
1697 return Empty;
1698 end Build_Discriminal_Subtype_Of_Component;
1700 ------------------------------
1701 -- Build_Elaboration_Entity --
1702 ------------------------------
1704 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1705 Loc : constant Source_Ptr := Sloc (N);
1706 Decl : Node_Id;
1707 Elab_Ent : Entity_Id;
1709 procedure Set_Package_Name (Ent : Entity_Id);
1710 -- Given an entity, sets the fully qualified name of the entity in
1711 -- Name_Buffer, with components separated by double underscores. This
1712 -- is a recursive routine that climbs the scope chain to Standard.
1714 ----------------------
1715 -- Set_Package_Name --
1716 ----------------------
1718 procedure Set_Package_Name (Ent : Entity_Id) is
1719 begin
1720 if Scope (Ent) /= Standard_Standard then
1721 Set_Package_Name (Scope (Ent));
1723 declare
1724 Nam : constant String := Get_Name_String (Chars (Ent));
1725 begin
1726 Name_Buffer (Name_Len + 1) := '_';
1727 Name_Buffer (Name_Len + 2) := '_';
1728 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1729 Name_Len := Name_Len + Nam'Length + 2;
1730 end;
1732 else
1733 Get_Name_String (Chars (Ent));
1734 end if;
1735 end Set_Package_Name;
1737 -- Start of processing for Build_Elaboration_Entity
1739 begin
1740 -- Ignore call if already constructed
1742 if Present (Elaboration_Entity (Spec_Id)) then
1743 return;
1745 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1746 -- no role in analysis.
1748 elsif ASIS_Mode then
1749 return;
1751 -- See if we need elaboration entity. We always need it for the dynamic
1752 -- elaboration model, since it is needed to properly generate the PE
1753 -- exception for access before elaboration.
1755 elsif Dynamic_Elaboration_Checks then
1756 null;
1758 -- For the static model, we don't need the elaboration counter if this
1759 -- unit is sure to have no elaboration code, since that means there
1760 -- is no elaboration unit to be called. Note that we can't just decide
1761 -- after the fact by looking to see whether there was elaboration code,
1762 -- because that's too late to make this decision.
1764 elsif Restriction_Active (No_Elaboration_Code) then
1765 return;
1767 -- Similarly, for the static model, we can skip the elaboration counter
1768 -- if we have the No_Multiple_Elaboration restriction, since for the
1769 -- static model, that's the only purpose of the counter (to avoid
1770 -- multiple elaboration).
1772 elsif Restriction_Active (No_Multiple_Elaboration) then
1773 return;
1774 end if;
1776 -- Here we need the elaboration entity
1778 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1779 -- name with dots replaced by double underscore. We have to manually
1780 -- construct this name, since it will be elaborated in the outer scope,
1781 -- and thus will not have the unit name automatically prepended.
1783 Set_Package_Name (Spec_Id);
1784 Add_Str_To_Name_Buffer ("_E");
1786 -- Create elaboration counter
1788 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1789 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1791 Decl :=
1792 Make_Object_Declaration (Loc,
1793 Defining_Identifier => Elab_Ent,
1794 Object_Definition =>
1795 New_Occurrence_Of (Standard_Short_Integer, Loc),
1796 Expression => Make_Integer_Literal (Loc, Uint_0));
1798 Push_Scope (Standard_Standard);
1799 Add_Global_Declaration (Decl);
1800 Pop_Scope;
1802 -- Reset True_Constant indication, since we will indeed assign a value
1803 -- to the variable in the binder main. We also kill the Current_Value
1804 -- and Last_Assignment fields for the same reason.
1806 Set_Is_True_Constant (Elab_Ent, False);
1807 Set_Current_Value (Elab_Ent, Empty);
1808 Set_Last_Assignment (Elab_Ent, Empty);
1810 -- We do not want any further qualification of the name (if we did not
1811 -- do this, we would pick up the name of the generic package in the case
1812 -- of a library level generic instantiation).
1814 Set_Has_Qualified_Name (Elab_Ent);
1815 Set_Has_Fully_Qualified_Name (Elab_Ent);
1816 end Build_Elaboration_Entity;
1818 --------------------------------
1819 -- Build_Explicit_Dereference --
1820 --------------------------------
1822 procedure Build_Explicit_Dereference
1823 (Expr : Node_Id;
1824 Disc : Entity_Id)
1826 Loc : constant Source_Ptr := Sloc (Expr);
1828 begin
1829 -- An entity of a type with a reference aspect is overloaded with
1830 -- both interpretations: with and without the dereference. Now that
1831 -- the dereference is made explicit, set the type of the node properly,
1832 -- to prevent anomalies in the backend. Same if the expression is an
1833 -- overloaded function call whose return type has a reference aspect.
1835 if Is_Entity_Name (Expr) then
1836 Set_Etype (Expr, Etype (Entity (Expr)));
1838 elsif Nkind (Expr) = N_Function_Call then
1839 Set_Etype (Expr, Etype (Name (Expr)));
1840 end if;
1842 Set_Is_Overloaded (Expr, False);
1844 -- The expression will often be a generalized indexing that yields a
1845 -- container element that is then dereferenced, in which case the
1846 -- generalized indexing call is also non-overloaded.
1848 if Nkind (Expr) = N_Indexed_Component
1849 and then Present (Generalized_Indexing (Expr))
1850 then
1851 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1852 end if;
1854 Rewrite (Expr,
1855 Make_Explicit_Dereference (Loc,
1856 Prefix =>
1857 Make_Selected_Component (Loc,
1858 Prefix => Relocate_Node (Expr),
1859 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1860 Set_Etype (Prefix (Expr), Etype (Disc));
1861 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1862 end Build_Explicit_Dereference;
1864 -----------------------------------
1865 -- Cannot_Raise_Constraint_Error --
1866 -----------------------------------
1868 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1869 begin
1870 if Compile_Time_Known_Value (Expr) then
1871 return True;
1873 elsif Do_Range_Check (Expr) then
1874 return False;
1876 elsif Raises_Constraint_Error (Expr) then
1877 return False;
1879 else
1880 case Nkind (Expr) is
1881 when N_Identifier =>
1882 return True;
1884 when N_Expanded_Name =>
1885 return True;
1887 when N_Selected_Component =>
1888 return not Do_Discriminant_Check (Expr);
1890 when N_Attribute_Reference =>
1891 if Do_Overflow_Check (Expr) then
1892 return False;
1894 elsif No (Expressions (Expr)) then
1895 return True;
1897 else
1898 declare
1899 N : Node_Id;
1901 begin
1902 N := First (Expressions (Expr));
1903 while Present (N) loop
1904 if Cannot_Raise_Constraint_Error (N) then
1905 Next (N);
1906 else
1907 return False;
1908 end if;
1909 end loop;
1911 return True;
1912 end;
1913 end if;
1915 when N_Type_Conversion =>
1916 if Do_Overflow_Check (Expr)
1917 or else Do_Length_Check (Expr)
1918 or else Do_Tag_Check (Expr)
1919 then
1920 return False;
1921 else
1922 return Cannot_Raise_Constraint_Error (Expression (Expr));
1923 end if;
1925 when N_Unchecked_Type_Conversion =>
1926 return Cannot_Raise_Constraint_Error (Expression (Expr));
1928 when N_Unary_Op =>
1929 if Do_Overflow_Check (Expr) then
1930 return False;
1931 else
1932 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1933 end if;
1935 when N_Op_Divide |
1936 N_Op_Mod |
1937 N_Op_Rem
1939 if Do_Division_Check (Expr)
1940 or else
1941 Do_Overflow_Check (Expr)
1942 then
1943 return False;
1944 else
1945 return
1946 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1947 and then
1948 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1949 end if;
1951 when N_Op_Add |
1952 N_Op_And |
1953 N_Op_Concat |
1954 N_Op_Eq |
1955 N_Op_Expon |
1956 N_Op_Ge |
1957 N_Op_Gt |
1958 N_Op_Le |
1959 N_Op_Lt |
1960 N_Op_Multiply |
1961 N_Op_Ne |
1962 N_Op_Or |
1963 N_Op_Rotate_Left |
1964 N_Op_Rotate_Right |
1965 N_Op_Shift_Left |
1966 N_Op_Shift_Right |
1967 N_Op_Shift_Right_Arithmetic |
1968 N_Op_Subtract |
1969 N_Op_Xor
1971 if Do_Overflow_Check (Expr) then
1972 return False;
1973 else
1974 return
1975 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1976 and then
1977 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1978 end if;
1980 when others =>
1981 return False;
1982 end case;
1983 end if;
1984 end Cannot_Raise_Constraint_Error;
1986 -----------------------------------------
1987 -- Check_Dynamically_Tagged_Expression --
1988 -----------------------------------------
1990 procedure Check_Dynamically_Tagged_Expression
1991 (Expr : Node_Id;
1992 Typ : Entity_Id;
1993 Related_Nod : Node_Id)
1995 begin
1996 pragma Assert (Is_Tagged_Type (Typ));
1998 -- In order to avoid spurious errors when analyzing the expanded code,
1999 -- this check is done only for nodes that come from source and for
2000 -- actuals of generic instantiations.
2002 if (Comes_From_Source (Related_Nod)
2003 or else In_Generic_Actual (Expr))
2004 and then (Is_Class_Wide_Type (Etype (Expr))
2005 or else Is_Dynamically_Tagged (Expr))
2006 and then Is_Tagged_Type (Typ)
2007 and then not Is_Class_Wide_Type (Typ)
2008 then
2009 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
2010 end if;
2011 end Check_Dynamically_Tagged_Expression;
2013 --------------------------
2014 -- Check_Fully_Declared --
2015 --------------------------
2017 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2018 begin
2019 if Ekind (T) = E_Incomplete_Type then
2021 -- Ada 2005 (AI-50217): If the type is available through a limited
2022 -- with_clause, verify that its full view has been analyzed.
2024 if From_Limited_With (T)
2025 and then Present (Non_Limited_View (T))
2026 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2027 then
2028 -- The non-limited view is fully declared
2030 null;
2032 else
2033 Error_Msg_NE
2034 ("premature usage of incomplete}", N, First_Subtype (T));
2035 end if;
2037 -- Need comments for these tests ???
2039 elsif Has_Private_Component (T)
2040 and then not Is_Generic_Type (Root_Type (T))
2041 and then not In_Spec_Expression
2042 then
2043 -- Special case: if T is the anonymous type created for a single
2044 -- task or protected object, use the name of the source object.
2046 if Is_Concurrent_Type (T)
2047 and then not Comes_From_Source (T)
2048 and then Nkind (N) = N_Object_Declaration
2049 then
2050 Error_Msg_NE
2051 ("type of& has incomplete component",
2052 N, Defining_Identifier (N));
2053 else
2054 Error_Msg_NE
2055 ("premature usage of incomplete}",
2056 N, First_Subtype (T));
2057 end if;
2058 end if;
2059 end Check_Fully_Declared;
2061 -------------------------------------
2062 -- Check_Function_Writable_Actuals --
2063 -------------------------------------
2065 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2066 Writable_Actuals_List : Elist_Id := No_Elist;
2067 Identifiers_List : Elist_Id := No_Elist;
2068 Error_Node : Node_Id := Empty;
2070 procedure Collect_Identifiers (N : Node_Id);
2071 -- In a single traversal of subtree N collect in Writable_Actuals_List
2072 -- all the actuals of functions with writable actuals, and in the list
2073 -- Identifiers_List collect all the identifiers that are not actuals of
2074 -- functions with writable actuals. If a writable actual is referenced
2075 -- twice as writable actual then Error_Node is set to reference its
2076 -- second occurrence, the error is reported, and the tree traversal
2077 -- is abandoned.
2079 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2080 -- Return the entity associated with the function call
2082 procedure Preanalyze_Without_Errors (N : Node_Id);
2083 -- Preanalyze N without reporting errors. Very dubious, you can't just
2084 -- go analyzing things more than once???
2086 -------------------------
2087 -- Collect_Identifiers --
2088 -------------------------
2090 procedure Collect_Identifiers (N : Node_Id) is
2092 function Check_Node (N : Node_Id) return Traverse_Result;
2093 -- Process a single node during the tree traversal to collect the
2094 -- writable actuals of functions and all the identifiers which are
2095 -- not writable actuals of functions.
2097 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2098 -- Returns True if List has a node whose Entity is Entity (N)
2100 -------------------------
2101 -- Check_Function_Call --
2102 -------------------------
2104 function Check_Node (N : Node_Id) return Traverse_Result is
2105 Is_Writable_Actual : Boolean := False;
2106 Id : Entity_Id;
2108 begin
2109 if Nkind (N) = N_Identifier then
2111 -- No analysis possible if the entity is not decorated
2113 if No (Entity (N)) then
2114 return Skip;
2116 -- Don't collect identifiers of packages, called functions, etc
2118 elsif Ekind_In (Entity (N), E_Package,
2119 E_Function,
2120 E_Procedure,
2121 E_Entry)
2122 then
2123 return Skip;
2125 -- Analyze if N is a writable actual of a function
2127 elsif Nkind (Parent (N)) = N_Function_Call then
2128 declare
2129 Call : constant Node_Id := Parent (N);
2130 Actual : Node_Id;
2131 Formal : Node_Id;
2133 begin
2134 Id := Get_Function_Id (Call);
2136 -- In case of previous error, no check is posible.
2138 if No (Id) then
2139 return Abandon;
2140 end if;
2142 Formal := First_Formal (Id);
2143 Actual := First_Actual (Call);
2144 while Present (Actual) and then Present (Formal) loop
2145 if Actual = N then
2146 if Ekind_In (Formal, E_Out_Parameter,
2147 E_In_Out_Parameter)
2148 then
2149 Is_Writable_Actual := True;
2150 end if;
2152 exit;
2153 end if;
2155 Next_Formal (Formal);
2156 Next_Actual (Actual);
2157 end loop;
2158 end;
2159 end if;
2161 if Is_Writable_Actual then
2162 if Contains (Writable_Actuals_List, N) then
2163 Error_Msg_NE
2164 ("value may be affected by call to& "
2165 & "because order of evaluation is arbitrary", N, Id);
2166 Error_Node := N;
2167 return Abandon;
2168 end if;
2170 Append_New_Elmt (N, To => Writable_Actuals_List);
2172 else
2173 if Identifiers_List = No_Elist then
2174 Identifiers_List := New_Elmt_List;
2175 end if;
2177 Append_Unique_Elmt (N, Identifiers_List);
2178 end if;
2179 end if;
2181 return OK;
2182 end Check_Node;
2184 --------------
2185 -- Contains --
2186 --------------
2188 function Contains
2189 (List : Elist_Id;
2190 N : Node_Id) return Boolean
2192 pragma Assert (Nkind (N) in N_Has_Entity);
2194 Elmt : Elmt_Id;
2196 begin
2197 if List = No_Elist then
2198 return False;
2199 end if;
2201 Elmt := First_Elmt (List);
2202 while Present (Elmt) loop
2203 if Entity (Node (Elmt)) = Entity (N) then
2204 return True;
2205 else
2206 Next_Elmt (Elmt);
2207 end if;
2208 end loop;
2210 return False;
2211 end Contains;
2213 ------------------
2214 -- Do_Traversal --
2215 ------------------
2217 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2218 -- The traversal procedure
2220 -- Start of processing for Collect_Identifiers
2222 begin
2223 if Present (Error_Node) then
2224 return;
2225 end if;
2227 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2228 return;
2229 end if;
2231 Do_Traversal (N);
2232 end Collect_Identifiers;
2234 ---------------------
2235 -- Get_Function_Id --
2236 ---------------------
2238 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2239 Nam : constant Node_Id := Name (Call);
2240 Id : Entity_Id;
2242 begin
2243 if Nkind (Nam) = N_Explicit_Dereference then
2244 Id := Etype (Nam);
2245 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2247 elsif Nkind (Nam) = N_Selected_Component then
2248 Id := Entity (Selector_Name (Nam));
2250 elsif Nkind (Nam) = N_Indexed_Component then
2251 Id := Entity (Selector_Name (Prefix (Nam)));
2253 else
2254 Id := Entity (Nam);
2255 end if;
2257 return Id;
2258 end Get_Function_Id;
2260 ---------------------------
2261 -- Preanalyze_Expression --
2262 ---------------------------
2264 procedure Preanalyze_Without_Errors (N : Node_Id) is
2265 Status : constant Boolean := Get_Ignore_Errors;
2266 begin
2267 Set_Ignore_Errors (True);
2268 Preanalyze (N);
2269 Set_Ignore_Errors (Status);
2270 end Preanalyze_Without_Errors;
2272 -- Start of processing for Check_Function_Writable_Actuals
2274 begin
2275 -- The check only applies to Ada 2012 code, and only to constructs that
2276 -- have multiple constituents whose order of evaluation is not specified
2277 -- by the language.
2279 if Ada_Version < Ada_2012
2280 or else (not (Nkind (N) in N_Op)
2281 and then not (Nkind (N) in N_Membership_Test)
2282 and then not Nkind_In (N, N_Range,
2283 N_Aggregate,
2284 N_Extension_Aggregate,
2285 N_Full_Type_Declaration,
2286 N_Function_Call,
2287 N_Procedure_Call_Statement,
2288 N_Entry_Call_Statement))
2289 or else (Nkind (N) = N_Full_Type_Declaration
2290 and then not Is_Record_Type (Defining_Identifier (N)))
2292 -- In addition, this check only applies to source code, not to code
2293 -- generated by constraint checks.
2295 or else not Comes_From_Source (N)
2296 then
2297 return;
2298 end if;
2300 -- If a construct C has two or more direct constituents that are names
2301 -- or expressions whose evaluation may occur in an arbitrary order, at
2302 -- least one of which contains a function call with an in out or out
2303 -- parameter, then the construct is legal only if: for each name N that
2304 -- is passed as a parameter of mode in out or out to some inner function
2305 -- call C2 (not including the construct C itself), there is no other
2306 -- name anywhere within a direct constituent of the construct C other
2307 -- than the one containing C2, that is known to refer to the same
2308 -- object (RM 6.4.1(6.17/3)).
2310 case Nkind (N) is
2311 when N_Range =>
2312 Collect_Identifiers (Low_Bound (N));
2313 Collect_Identifiers (High_Bound (N));
2315 when N_Op | N_Membership_Test =>
2316 declare
2317 Expr : Node_Id;
2319 begin
2320 Collect_Identifiers (Left_Opnd (N));
2322 if Present (Right_Opnd (N)) then
2323 Collect_Identifiers (Right_Opnd (N));
2324 end if;
2326 if Nkind_In (N, N_In, N_Not_In)
2327 and then Present (Alternatives (N))
2328 then
2329 Expr := First (Alternatives (N));
2330 while Present (Expr) loop
2331 Collect_Identifiers (Expr);
2333 Next (Expr);
2334 end loop;
2335 end if;
2336 end;
2338 when N_Full_Type_Declaration =>
2339 declare
2340 function Get_Record_Part (N : Node_Id) return Node_Id;
2341 -- Return the record part of this record type definition
2343 function Get_Record_Part (N : Node_Id) return Node_Id is
2344 Type_Def : constant Node_Id := Type_Definition (N);
2345 begin
2346 if Nkind (Type_Def) = N_Derived_Type_Definition then
2347 return Record_Extension_Part (Type_Def);
2348 else
2349 return Type_Def;
2350 end if;
2351 end Get_Record_Part;
2353 Comp : Node_Id;
2354 Def_Id : Entity_Id := Defining_Identifier (N);
2355 Rec : Node_Id := Get_Record_Part (N);
2357 begin
2358 -- No need to perform any analysis if the record has no
2359 -- components
2361 if No (Rec) or else No (Component_List (Rec)) then
2362 return;
2363 end if;
2365 -- Collect the identifiers starting from the deepest
2366 -- derivation. Done to report the error in the deepest
2367 -- derivation.
2369 loop
2370 if Present (Component_List (Rec)) then
2371 Comp := First (Component_Items (Component_List (Rec)));
2372 while Present (Comp) loop
2373 if Nkind (Comp) = N_Component_Declaration
2374 and then Present (Expression (Comp))
2375 then
2376 Collect_Identifiers (Expression (Comp));
2377 end if;
2379 Next (Comp);
2380 end loop;
2381 end if;
2383 exit when No (Underlying_Type (Etype (Def_Id)))
2384 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2385 = Def_Id;
2387 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2388 Rec := Get_Record_Part (Parent (Def_Id));
2389 end loop;
2390 end;
2392 when N_Subprogram_Call |
2393 N_Entry_Call_Statement =>
2394 declare
2395 Id : constant Entity_Id := Get_Function_Id (N);
2396 Formal : Node_Id;
2397 Actual : Node_Id;
2399 begin
2400 Formal := First_Formal (Id);
2401 Actual := First_Actual (N);
2402 while Present (Actual) and then Present (Formal) loop
2403 if Ekind_In (Formal, E_Out_Parameter,
2404 E_In_Out_Parameter)
2405 then
2406 Collect_Identifiers (Actual);
2407 end if;
2409 Next_Formal (Formal);
2410 Next_Actual (Actual);
2411 end loop;
2412 end;
2414 when N_Aggregate |
2415 N_Extension_Aggregate =>
2416 declare
2417 Assoc : Node_Id;
2418 Choice : Node_Id;
2419 Comp_Expr : Node_Id;
2421 begin
2422 -- Handle the N_Others_Choice of array aggregates with static
2423 -- bounds. There is no need to perform this analysis in
2424 -- aggregates without static bounds since we cannot evaluate
2425 -- if the N_Others_Choice covers several elements. There is
2426 -- no need to handle the N_Others choice of record aggregates
2427 -- since at this stage it has been already expanded by
2428 -- Resolve_Record_Aggregate.
2430 if Is_Array_Type (Etype (N))
2431 and then Nkind (N) = N_Aggregate
2432 and then Present (Aggregate_Bounds (N))
2433 and then Compile_Time_Known_Bounds (Etype (N))
2434 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2436 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2437 then
2438 declare
2439 Count_Components : Uint := Uint_0;
2440 Num_Components : Uint;
2441 Others_Assoc : Node_Id;
2442 Others_Choice : Node_Id := Empty;
2443 Others_Box_Present : Boolean := False;
2445 begin
2446 -- Count positional associations
2448 if Present (Expressions (N)) then
2449 Comp_Expr := First (Expressions (N));
2450 while Present (Comp_Expr) loop
2451 Count_Components := Count_Components + 1;
2452 Next (Comp_Expr);
2453 end loop;
2454 end if;
2456 -- Count the rest of elements and locate the N_Others
2457 -- choice (if any)
2459 Assoc := First (Component_Associations (N));
2460 while Present (Assoc) loop
2461 Choice := First (Choices (Assoc));
2462 while Present (Choice) loop
2463 if Nkind (Choice) = N_Others_Choice then
2464 Others_Assoc := Assoc;
2465 Others_Choice := Choice;
2466 Others_Box_Present := Box_Present (Assoc);
2468 -- Count several components
2470 elsif Nkind_In (Choice, N_Range,
2471 N_Subtype_Indication)
2472 or else (Is_Entity_Name (Choice)
2473 and then Is_Type (Entity (Choice)))
2474 then
2475 declare
2476 L, H : Node_Id;
2477 begin
2478 Get_Index_Bounds (Choice, L, H);
2479 pragma Assert
2480 (Compile_Time_Known_Value (L)
2481 and then Compile_Time_Known_Value (H));
2482 Count_Components :=
2483 Count_Components
2484 + Expr_Value (H) - Expr_Value (L) + 1;
2485 end;
2487 -- Count single component. No other case available
2488 -- since we are handling an aggregate with static
2489 -- bounds.
2491 else
2492 pragma Assert (Is_OK_Static_Expression (Choice)
2493 or else Nkind (Choice) = N_Identifier
2494 or else Nkind (Choice) = N_Integer_Literal);
2496 Count_Components := Count_Components + 1;
2497 end if;
2499 Next (Choice);
2500 end loop;
2502 Next (Assoc);
2503 end loop;
2505 Num_Components :=
2506 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2507 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2509 pragma Assert (Count_Components <= Num_Components);
2511 -- Handle the N_Others choice if it covers several
2512 -- components
2514 if Present (Others_Choice)
2515 and then (Num_Components - Count_Components) > 1
2516 then
2517 if not Others_Box_Present then
2519 -- At this stage, if expansion is active, the
2520 -- expression of the others choice has not been
2521 -- analyzed. Hence we generate a duplicate and
2522 -- we analyze it silently to have available the
2523 -- minimum decoration required to collect the
2524 -- identifiers.
2526 if not Expander_Active then
2527 Comp_Expr := Expression (Others_Assoc);
2528 else
2529 Comp_Expr :=
2530 New_Copy_Tree (Expression (Others_Assoc));
2531 Preanalyze_Without_Errors (Comp_Expr);
2532 end if;
2534 Collect_Identifiers (Comp_Expr);
2536 if Writable_Actuals_List /= No_Elist then
2538 -- As suggested by Robert, at current stage we
2539 -- report occurrences of this case as warnings.
2541 Error_Msg_N
2542 ("writable function parameter may affect "
2543 & "value in other component because order "
2544 & "of evaluation is unspecified??",
2545 Node (First_Elmt (Writable_Actuals_List)));
2546 end if;
2547 end if;
2548 end if;
2549 end;
2550 end if;
2552 -- Handle ancestor part of extension aggregates
2554 if Nkind (N) = N_Extension_Aggregate then
2555 Collect_Identifiers (Ancestor_Part (N));
2556 end if;
2558 -- Handle positional associations
2560 if Present (Expressions (N)) then
2561 Comp_Expr := First (Expressions (N));
2562 while Present (Comp_Expr) loop
2563 if not Is_OK_Static_Expression (Comp_Expr) then
2564 Collect_Identifiers (Comp_Expr);
2565 end if;
2567 Next (Comp_Expr);
2568 end loop;
2569 end if;
2571 -- Handle discrete associations
2573 if Present (Component_Associations (N)) then
2574 Assoc := First (Component_Associations (N));
2575 while Present (Assoc) loop
2577 if not Box_Present (Assoc) then
2578 Choice := First (Choices (Assoc));
2579 while Present (Choice) loop
2581 -- For now we skip discriminants since it requires
2582 -- performing the analysis in two phases: first one
2583 -- analyzing discriminants and second one analyzing
2584 -- the rest of components since discriminants are
2585 -- evaluated prior to components: too much extra
2586 -- work to detect a corner case???
2588 if Nkind (Choice) in N_Has_Entity
2589 and then Present (Entity (Choice))
2590 and then Ekind (Entity (Choice)) = E_Discriminant
2591 then
2592 null;
2594 elsif Box_Present (Assoc) then
2595 null;
2597 else
2598 if not Analyzed (Expression (Assoc)) then
2599 Comp_Expr :=
2600 New_Copy_Tree (Expression (Assoc));
2601 Set_Parent (Comp_Expr, Parent (N));
2602 Preanalyze_Without_Errors (Comp_Expr);
2603 else
2604 Comp_Expr := Expression (Assoc);
2605 end if;
2607 Collect_Identifiers (Comp_Expr);
2608 end if;
2610 Next (Choice);
2611 end loop;
2612 end if;
2614 Next (Assoc);
2615 end loop;
2616 end if;
2617 end;
2619 when others =>
2620 return;
2621 end case;
2623 -- No further action needed if we already reported an error
2625 if Present (Error_Node) then
2626 return;
2627 end if;
2629 -- Check if some writable argument of a function is referenced
2631 if Writable_Actuals_List /= No_Elist
2632 and then Identifiers_List /= No_Elist
2633 then
2634 declare
2635 Elmt_1 : Elmt_Id;
2636 Elmt_2 : Elmt_Id;
2638 begin
2639 Elmt_1 := First_Elmt (Writable_Actuals_List);
2640 while Present (Elmt_1) loop
2641 Elmt_2 := First_Elmt (Identifiers_List);
2642 while Present (Elmt_2) loop
2643 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2644 case Nkind (Parent (Node (Elmt_2))) is
2645 when N_Aggregate |
2646 N_Component_Association |
2647 N_Component_Declaration =>
2648 Error_Msg_N
2649 ("value may be affected by call in other "
2650 & "component because they are evaluated "
2651 & "in unspecified order",
2652 Node (Elmt_2));
2654 when N_In | N_Not_In =>
2655 Error_Msg_N
2656 ("value may be affected by call in other "
2657 & "alternative because they are evaluated "
2658 & "in unspecified order",
2659 Node (Elmt_2));
2661 when others =>
2662 Error_Msg_N
2663 ("value of actual may be affected by call in "
2664 & "other actual because they are evaluated "
2665 & "in unspecified order",
2666 Node (Elmt_2));
2667 end case;
2668 end if;
2670 Next_Elmt (Elmt_2);
2671 end loop;
2673 Next_Elmt (Elmt_1);
2674 end loop;
2675 end;
2676 end if;
2677 end Check_Function_Writable_Actuals;
2679 ----------------------------
2680 -- Check_Ghost_Completion --
2681 ----------------------------
2683 procedure Check_Ghost_Completion
2684 (Partial_View : Entity_Id;
2685 Full_View : Entity_Id)
2687 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
2689 begin
2690 -- The Ghost policy in effect at the point of declaration and at the
2691 -- point of completion must match (SPARK RM 6.9(15)).
2693 if Is_Checked_Ghost_Entity (Partial_View)
2694 and then Policy = Name_Ignore
2695 then
2696 Error_Msg_Sloc := Sloc (Full_View);
2698 Error_Msg_N ("incompatible ghost policies in effect", Partial_View);
2699 Error_Msg_N ("\& declared with ghost policy Check", Partial_View);
2700 Error_Msg_N ("\& completed # with ghost policy Ignore", Partial_View);
2702 elsif Is_Ignored_Ghost_Entity (Partial_View)
2703 and then Policy = Name_Check
2704 then
2705 Error_Msg_Sloc := Sloc (Full_View);
2707 Error_Msg_N ("incompatible ghost policies in effect", Partial_View);
2708 Error_Msg_N ("\& declared with ghost policy Ignore", Partial_View);
2709 Error_Msg_N ("\& completed # with ghost policy Check", Partial_View);
2710 end if;
2711 end Check_Ghost_Completion;
2713 ----------------------------
2714 -- Check_Ghost_Derivation --
2715 ----------------------------
2717 procedure Check_Ghost_Derivation (Typ : Entity_Id) is
2718 Parent_Typ : constant Entity_Id := Etype (Typ);
2719 Iface : Entity_Id;
2720 Iface_Elmt : Elmt_Id;
2722 begin
2723 -- Allow untagged derivations from predefined types such as Integer as
2724 -- those are not Ghost by definition.
2726 if Is_Scalar_Type (Typ) and then Parent_Typ = Base_Type (Typ) then
2727 null;
2729 -- The parent type of a Ghost type extension must be Ghost
2731 elsif not Is_Ghost_Entity (Parent_Typ) then
2732 Error_Msg_N ("type extension & cannot be ghost", Typ);
2733 Error_Msg_NE ("\parent type & is not ghost", Typ, Parent_Typ);
2734 return;
2735 end if;
2737 -- All progenitors (if any) must be Ghost as well
2739 if Is_Tagged_Type (Typ) and then Present (Interfaces (Typ)) then
2740 Iface_Elmt := First_Elmt (Interfaces (Typ));
2741 while Present (Iface_Elmt) loop
2742 Iface := Node (Iface_Elmt);
2744 if not Is_Ghost_Entity (Iface) then
2745 Error_Msg_N ("type extension & cannot be ghost", Typ);
2746 Error_Msg_NE ("\interface type & is not ghost", Typ, Iface);
2747 return;
2748 end if;
2750 Next_Elmt (Iface_Elmt);
2751 end loop;
2752 end if;
2753 end Check_Ghost_Derivation;
2755 --------------------------------
2756 -- Check_Implicit_Dereference --
2757 --------------------------------
2759 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2760 Disc : Entity_Id;
2761 Desig : Entity_Id;
2762 Nam : Node_Id;
2764 begin
2765 if Nkind (N) = N_Indexed_Component
2766 and then Present (Generalized_Indexing (N))
2767 then
2768 Nam := Generalized_Indexing (N);
2769 else
2770 Nam := N;
2771 end if;
2773 if Ada_Version < Ada_2012
2774 or else not Has_Implicit_Dereference (Base_Type (Typ))
2775 then
2776 return;
2778 elsif not Comes_From_Source (N)
2779 and then Nkind (N) /= N_Indexed_Component
2780 then
2781 return;
2783 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2784 null;
2786 else
2787 Disc := First_Discriminant (Typ);
2788 while Present (Disc) loop
2789 if Has_Implicit_Dereference (Disc) then
2790 Desig := Designated_Type (Etype (Disc));
2791 Add_One_Interp (Nam, Disc, Desig);
2793 -- If the node is a generalized indexing, add interpretation
2794 -- to that node as well, for subsequent resolution.
2796 if Nkind (N) = N_Indexed_Component then
2797 Add_One_Interp (N, Disc, Desig);
2798 end if;
2800 -- If the operation comes from a generic unit and the context
2801 -- is a selected component, the selector name may be global
2802 -- and set in the instance already. Remove the entity to
2803 -- force resolution of the selected component, and the
2804 -- generation of an explicit dereference if needed.
2806 if In_Instance
2807 and then Nkind (Parent (Nam)) = N_Selected_Component
2808 then
2809 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2810 end if;
2812 exit;
2813 end if;
2815 Next_Discriminant (Disc);
2816 end loop;
2817 end if;
2818 end Check_Implicit_Dereference;
2820 ----------------------------------
2821 -- Check_Internal_Protected_Use --
2822 ----------------------------------
2824 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2825 S : Entity_Id;
2826 Prot : Entity_Id;
2828 begin
2829 S := Current_Scope;
2830 while Present (S) loop
2831 if S = Standard_Standard then
2832 return;
2834 elsif Ekind (S) = E_Function
2835 and then Ekind (Scope (S)) = E_Protected_Type
2836 then
2837 Prot := Scope (S);
2838 exit;
2839 end if;
2841 S := Scope (S);
2842 end loop;
2844 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2846 -- An indirect function call (e.g. a callback within a protected
2847 -- function body) is not statically illegal. If the access type is
2848 -- anonymous and is the type of an access parameter, the scope of Nam
2849 -- will be the protected type, but it is not a protected operation.
2851 if Ekind (Nam) = E_Subprogram_Type
2852 and then
2853 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2854 then
2855 null;
2857 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2858 Error_Msg_N
2859 ("within protected function cannot use protected "
2860 & "procedure in renaming or as generic actual", N);
2862 elsif Nkind (N) = N_Attribute_Reference then
2863 Error_Msg_N
2864 ("within protected function cannot take access of "
2865 & " protected procedure", N);
2867 else
2868 Error_Msg_N
2869 ("within protected function, protected object is constant", N);
2870 Error_Msg_N
2871 ("\cannot call operation that may modify it", N);
2872 end if;
2873 end if;
2874 end Check_Internal_Protected_Use;
2876 ---------------------------------------
2877 -- Check_Later_Vs_Basic_Declarations --
2878 ---------------------------------------
2880 procedure Check_Later_Vs_Basic_Declarations
2881 (Decls : List_Id;
2882 During_Parsing : Boolean)
2884 Body_Sloc : Source_Ptr;
2885 Decl : Node_Id;
2887 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2888 -- Return whether Decl is considered as a declarative item.
2889 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2890 -- When During_Parsing is False, the semantics of SPARK is followed.
2892 -------------------------------
2893 -- Is_Later_Declarative_Item --
2894 -------------------------------
2896 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2897 begin
2898 if Nkind (Decl) in N_Later_Decl_Item then
2899 return True;
2901 elsif Nkind (Decl) = N_Pragma then
2902 return True;
2904 elsif During_Parsing then
2905 return False;
2907 -- In SPARK, a package declaration is not considered as a later
2908 -- declarative item.
2910 elsif Nkind (Decl) = N_Package_Declaration then
2911 return False;
2913 -- In SPARK, a renaming is considered as a later declarative item
2915 elsif Nkind (Decl) in N_Renaming_Declaration then
2916 return True;
2918 else
2919 return False;
2920 end if;
2921 end Is_Later_Declarative_Item;
2923 -- Start of Check_Later_Vs_Basic_Declarations
2925 begin
2926 Decl := First (Decls);
2928 -- Loop through sequence of basic declarative items
2930 Outer : while Present (Decl) loop
2931 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
2932 and then Nkind (Decl) not in N_Body_Stub
2933 then
2934 Next (Decl);
2936 -- Once a body is encountered, we only allow later declarative
2937 -- items. The inner loop checks the rest of the list.
2939 else
2940 Body_Sloc := Sloc (Decl);
2942 Inner : while Present (Decl) loop
2943 if not Is_Later_Declarative_Item (Decl) then
2944 if During_Parsing then
2945 if Ada_Version = Ada_83 then
2946 Error_Msg_Sloc := Body_Sloc;
2947 Error_Msg_N
2948 ("(Ada 83) decl cannot appear after body#", Decl);
2949 end if;
2950 else
2951 Error_Msg_Sloc := Body_Sloc;
2952 Check_SPARK_05_Restriction
2953 ("decl cannot appear after body#", Decl);
2954 end if;
2955 end if;
2957 Next (Decl);
2958 end loop Inner;
2959 end if;
2960 end loop Outer;
2961 end Check_Later_Vs_Basic_Declarations;
2963 -------------------------
2964 -- Check_Nested_Access --
2965 -------------------------
2967 procedure Check_Nested_Access (Ent : Entity_Id) is
2968 Scop : constant Entity_Id := Current_Scope;
2969 Current_Subp : Entity_Id;
2970 Enclosing : Entity_Id;
2972 begin
2973 -- Currently only enabled for VM back-ends for efficiency, should we
2974 -- enable it more systematically ???
2976 -- Check for Is_Imported needs commenting below ???
2978 if VM_Target /= No_VM
2979 and then Ekind_In (Ent, E_Variable, E_Constant, E_Loop_Parameter)
2980 and then Scope (Ent) /= Empty
2981 and then not Is_Library_Level_Entity (Ent)
2982 and then not Is_Imported (Ent)
2983 then
2984 if Is_Subprogram (Scop)
2985 or else Is_Generic_Subprogram (Scop)
2986 or else Is_Entry (Scop)
2987 then
2988 Current_Subp := Scop;
2989 else
2990 Current_Subp := Current_Subprogram;
2991 end if;
2993 Enclosing := Enclosing_Subprogram (Ent);
2995 if Enclosing /= Empty and then Enclosing /= Current_Subp then
2996 Set_Has_Up_Level_Access (Ent, True);
2997 end if;
2998 end if;
2999 end Check_Nested_Access;
3001 ---------------------------
3002 -- Check_No_Hidden_State --
3003 ---------------------------
3005 procedure Check_No_Hidden_State (Id : Entity_Id) is
3006 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
3007 -- Determine whether the entity of a package denoted by Pkg has a null
3008 -- abstract state.
3010 -----------------------------
3011 -- Has_Null_Abstract_State --
3012 -----------------------------
3014 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
3015 States : constant Elist_Id := Abstract_States (Pkg);
3017 begin
3018 -- Check first available state of related package. A null abstract
3019 -- state always appears as the sole element of the state list.
3021 return
3022 Present (States)
3023 and then Is_Null_State (Node (First_Elmt (States)));
3024 end Has_Null_Abstract_State;
3026 -- Local variables
3028 Context : Entity_Id := Empty;
3029 Not_Visible : Boolean := False;
3030 Scop : Entity_Id;
3032 -- Start of processing for Check_No_Hidden_State
3034 begin
3035 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3037 -- Find the proper context where the object or state appears
3039 Scop := Scope (Id);
3040 while Present (Scop) loop
3041 Context := Scop;
3043 -- Keep track of the context's visibility
3045 Not_Visible := Not_Visible or else In_Private_Part (Context);
3047 -- Prevent the search from going too far
3049 if Context = Standard_Standard then
3050 return;
3052 -- Objects and states that appear immediately within a subprogram or
3053 -- inside a construct nested within a subprogram do not introduce a
3054 -- hidden state. They behave as local variable declarations.
3056 elsif Is_Subprogram (Context) then
3057 return;
3059 -- When examining a package body, use the entity of the spec as it
3060 -- carries the abstract state declarations.
3062 elsif Ekind (Context) = E_Package_Body then
3063 Context := Spec_Entity (Context);
3064 end if;
3066 -- Stop the traversal when a package subject to a null abstract state
3067 -- has been found.
3069 if Ekind_In (Context, E_Generic_Package, E_Package)
3070 and then Has_Null_Abstract_State (Context)
3071 then
3072 exit;
3073 end if;
3075 Scop := Scope (Scop);
3076 end loop;
3078 -- At this point we know that there is at least one package with a null
3079 -- abstract state in visibility. Emit an error message unconditionally
3080 -- if the entity being processed is a state because the placement of the
3081 -- related package is irrelevant. This is not the case for objects as
3082 -- the intermediate context matters.
3084 if Present (Context)
3085 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3086 then
3087 Error_Msg_N ("cannot introduce hidden state &", Id);
3088 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3089 end if;
3090 end Check_No_Hidden_State;
3092 ------------------------------------------
3093 -- Check_Potentially_Blocking_Operation --
3094 ------------------------------------------
3096 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3097 S : Entity_Id;
3099 begin
3100 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3101 -- When pragma Detect_Blocking is active, the run time will raise
3102 -- Program_Error. Here we only issue a warning, since we generally
3103 -- support the use of potentially blocking operations in the absence
3104 -- of the pragma.
3106 -- Indirect blocking through a subprogram call cannot be diagnosed
3107 -- statically without interprocedural analysis, so we do not attempt
3108 -- to do it here.
3110 S := Scope (Current_Scope);
3111 while Present (S) and then S /= Standard_Standard loop
3112 if Is_Protected_Type (S) then
3113 Error_Msg_N
3114 ("potentially blocking operation in protected operation??", N);
3115 return;
3116 end if;
3118 S := Scope (S);
3119 end loop;
3120 end Check_Potentially_Blocking_Operation;
3122 ---------------------------------
3123 -- Check_Result_And_Post_State --
3124 ---------------------------------
3126 procedure Check_Result_And_Post_State
3127 (Prag : Node_Id;
3128 Result_Seen : in out Boolean)
3130 procedure Check_Expression (Expr : Node_Id);
3131 -- Perform the 'Result and post-state checks on a given expression
3133 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3134 -- Attempt to find attribute 'Result in a subtree denoted by N
3136 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3137 -- Determine whether source node N denotes "True" or "False"
3139 function Mentions_Post_State (N : Node_Id) return Boolean;
3140 -- Determine whether a subtree denoted by N mentions any construct that
3141 -- denotes a post-state.
3143 procedure Check_Function_Result is
3144 new Traverse_Proc (Is_Function_Result);
3146 ----------------------
3147 -- Check_Expression --
3148 ----------------------
3150 procedure Check_Expression (Expr : Node_Id) is
3151 begin
3152 if not Is_Trivial_Boolean (Expr) then
3153 Check_Function_Result (Expr);
3155 if not Mentions_Post_State (Expr) then
3156 if Pragma_Name (Prag) = Name_Contract_Cases then
3157 Error_Msg_N
3158 ("contract case refers only to pre-state?T?", Expr);
3160 elsif Pragma_Name (Prag) = Name_Refined_Post then
3161 Error_Msg_N
3162 ("refined postcondition refers only to pre-state?T?",
3163 Prag);
3165 else
3166 Error_Msg_N
3167 ("postcondition refers only to pre-state?T?", Prag);
3168 end if;
3169 end if;
3170 end if;
3171 end Check_Expression;
3173 ------------------------
3174 -- Is_Function_Result --
3175 ------------------------
3177 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3178 begin
3179 if Is_Attribute_Result (N) then
3180 Result_Seen := True;
3181 return Abandon;
3183 -- Continue the traversal
3185 else
3186 return OK;
3187 end if;
3188 end Is_Function_Result;
3190 ------------------------
3191 -- Is_Trivial_Boolean --
3192 ------------------------
3194 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3195 begin
3196 return
3197 Comes_From_Source (N)
3198 and then Is_Entity_Name (N)
3199 and then (Entity (N) = Standard_True
3200 or else
3201 Entity (N) = Standard_False);
3202 end Is_Trivial_Boolean;
3204 -------------------------
3205 -- Mentions_Post_State --
3206 -------------------------
3208 function Mentions_Post_State (N : Node_Id) return Boolean is
3209 Post_State_Seen : Boolean := False;
3211 function Is_Post_State (N : Node_Id) return Traverse_Result;
3212 -- Attempt to find a construct that denotes a post-state. If this is
3213 -- the case, set flag Post_State_Seen.
3215 -------------------
3216 -- Is_Post_State --
3217 -------------------
3219 function Is_Post_State (N : Node_Id) return Traverse_Result is
3220 Ent : Entity_Id;
3222 begin
3223 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3224 Post_State_Seen := True;
3225 return Abandon;
3227 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3228 Ent := Entity (N);
3230 -- The entity may be modifiable through an implicit dereference
3232 if No (Ent)
3233 or else Ekind (Ent) in Assignable_Kind
3234 or else (Is_Access_Type (Etype (Ent))
3235 and then Nkind (Parent (N)) = N_Selected_Component)
3236 then
3237 Post_State_Seen := True;
3238 return Abandon;
3239 end if;
3241 elsif Nkind (N) = N_Attribute_Reference then
3242 if Attribute_Name (N) = Name_Old then
3243 return Skip;
3245 elsif Attribute_Name (N) = Name_Result then
3246 Post_State_Seen := True;
3247 return Abandon;
3248 end if;
3249 end if;
3251 return OK;
3252 end Is_Post_State;
3254 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3256 -- Start of processing for Mentions_Post_State
3258 begin
3259 Find_Post_State (N);
3261 return Post_State_Seen;
3262 end Mentions_Post_State;
3264 -- Local variables
3266 Expr : constant Node_Id :=
3267 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
3268 Nam : constant Name_Id := Pragma_Name (Prag);
3269 CCase : Node_Id;
3271 -- Start of processing for Check_Result_And_Post_State
3273 begin
3274 -- Examine all consequences
3276 if Nam = Name_Contract_Cases then
3277 CCase := First (Component_Associations (Expr));
3278 while Present (CCase) loop
3279 Check_Expression (Expression (CCase));
3281 Next (CCase);
3282 end loop;
3284 -- Examine the expression of a postcondition
3286 else pragma Assert (Nam_In (Nam, Name_Postcondition, Name_Refined_Post));
3287 Check_Expression (Expr);
3288 end if;
3289 end Check_Result_And_Post_State;
3291 ------------------------------
3292 -- Check_Unprotected_Access --
3293 ------------------------------
3295 procedure Check_Unprotected_Access
3296 (Context : Node_Id;
3297 Expr : Node_Id)
3299 Cont_Encl_Typ : Entity_Id;
3300 Pref_Encl_Typ : Entity_Id;
3302 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3303 -- Check whether Obj is a private component of a protected object.
3304 -- Return the protected type where the component resides, Empty
3305 -- otherwise.
3307 function Is_Public_Operation return Boolean;
3308 -- Verify that the enclosing operation is callable from outside the
3309 -- protected object, to minimize false positives.
3311 ------------------------------
3312 -- Enclosing_Protected_Type --
3313 ------------------------------
3315 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3316 begin
3317 if Is_Entity_Name (Obj) then
3318 declare
3319 Ent : Entity_Id := Entity (Obj);
3321 begin
3322 -- The object can be a renaming of a private component, use
3323 -- the original record component.
3325 if Is_Prival (Ent) then
3326 Ent := Prival_Link (Ent);
3327 end if;
3329 if Is_Protected_Type (Scope (Ent)) then
3330 return Scope (Ent);
3331 end if;
3332 end;
3333 end if;
3335 -- For indexed and selected components, recursively check the prefix
3337 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3338 return Enclosing_Protected_Type (Prefix (Obj));
3340 -- The object does not denote a protected component
3342 else
3343 return Empty;
3344 end if;
3345 end Enclosing_Protected_Type;
3347 -------------------------
3348 -- Is_Public_Operation --
3349 -------------------------
3351 function Is_Public_Operation return Boolean is
3352 S : Entity_Id;
3353 E : Entity_Id;
3355 begin
3356 S := Current_Scope;
3357 while Present (S) and then S /= Pref_Encl_Typ loop
3358 if Scope (S) = Pref_Encl_Typ then
3359 E := First_Entity (Pref_Encl_Typ);
3360 while Present (E)
3361 and then E /= First_Private_Entity (Pref_Encl_Typ)
3362 loop
3363 if E = S then
3364 return True;
3365 end if;
3367 Next_Entity (E);
3368 end loop;
3369 end if;
3371 S := Scope (S);
3372 end loop;
3374 return False;
3375 end Is_Public_Operation;
3377 -- Start of processing for Check_Unprotected_Access
3379 begin
3380 if Nkind (Expr) = N_Attribute_Reference
3381 and then Attribute_Name (Expr) = Name_Unchecked_Access
3382 then
3383 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3384 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3386 -- Check whether we are trying to export a protected component to a
3387 -- context with an equal or lower access level.
3389 if Present (Pref_Encl_Typ)
3390 and then No (Cont_Encl_Typ)
3391 and then Is_Public_Operation
3392 and then Scope_Depth (Pref_Encl_Typ) >=
3393 Object_Access_Level (Context)
3394 then
3395 Error_Msg_N
3396 ("??possible unprotected access to protected data", Expr);
3397 end if;
3398 end if;
3399 end Check_Unprotected_Access;
3401 ------------------------
3402 -- Collect_Interfaces --
3403 ------------------------
3405 procedure Collect_Interfaces
3406 (T : Entity_Id;
3407 Ifaces_List : out Elist_Id;
3408 Exclude_Parents : Boolean := False;
3409 Use_Full_View : Boolean := True)
3411 procedure Collect (Typ : Entity_Id);
3412 -- Subsidiary subprogram used to traverse the whole list
3413 -- of directly and indirectly implemented interfaces
3415 -------------
3416 -- Collect --
3417 -------------
3419 procedure Collect (Typ : Entity_Id) is
3420 Ancestor : Entity_Id;
3421 Full_T : Entity_Id;
3422 Id : Node_Id;
3423 Iface : Entity_Id;
3425 begin
3426 Full_T := Typ;
3428 -- Handle private types
3430 if Use_Full_View
3431 and then Is_Private_Type (Typ)
3432 and then Present (Full_View (Typ))
3433 then
3434 Full_T := Full_View (Typ);
3435 end if;
3437 -- Include the ancestor if we are generating the whole list of
3438 -- abstract interfaces.
3440 if Etype (Full_T) /= Typ
3442 -- Protect the frontend against wrong sources. For example:
3444 -- package P is
3445 -- type A is tagged null record;
3446 -- type B is new A with private;
3447 -- type C is new A with private;
3448 -- private
3449 -- type B is new C with null record;
3450 -- type C is new B with null record;
3451 -- end P;
3453 and then Etype (Full_T) /= T
3454 then
3455 Ancestor := Etype (Full_T);
3456 Collect (Ancestor);
3458 if Is_Interface (Ancestor) and then not Exclude_Parents then
3459 Append_Unique_Elmt (Ancestor, Ifaces_List);
3460 end if;
3461 end if;
3463 -- Traverse the graph of ancestor interfaces
3465 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
3466 Id := First (Abstract_Interface_List (Full_T));
3467 while Present (Id) loop
3468 Iface := Etype (Id);
3470 -- Protect against wrong uses. For example:
3471 -- type I is interface;
3472 -- type O is tagged null record;
3473 -- type Wrong is new I and O with null record; -- ERROR
3475 if Is_Interface (Iface) then
3476 if Exclude_Parents
3477 and then Etype (T) /= T
3478 and then Interface_Present_In_Ancestor (Etype (T), Iface)
3479 then
3480 null;
3481 else
3482 Collect (Iface);
3483 Append_Unique_Elmt (Iface, Ifaces_List);
3484 end if;
3485 end if;
3487 Next (Id);
3488 end loop;
3489 end if;
3490 end Collect;
3492 -- Start of processing for Collect_Interfaces
3494 begin
3495 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
3496 Ifaces_List := New_Elmt_List;
3497 Collect (T);
3498 end Collect_Interfaces;
3500 ----------------------------------
3501 -- Collect_Interface_Components --
3502 ----------------------------------
3504 procedure Collect_Interface_Components
3505 (Tagged_Type : Entity_Id;
3506 Components_List : out Elist_Id)
3508 procedure Collect (Typ : Entity_Id);
3509 -- Subsidiary subprogram used to climb to the parents
3511 -------------
3512 -- Collect --
3513 -------------
3515 procedure Collect (Typ : Entity_Id) is
3516 Tag_Comp : Entity_Id;
3517 Parent_Typ : Entity_Id;
3519 begin
3520 -- Handle private types
3522 if Present (Full_View (Etype (Typ))) then
3523 Parent_Typ := Full_View (Etype (Typ));
3524 else
3525 Parent_Typ := Etype (Typ);
3526 end if;
3528 if Parent_Typ /= Typ
3530 -- Protect the frontend against wrong sources. For example:
3532 -- package P is
3533 -- type A is tagged null record;
3534 -- type B is new A with private;
3535 -- type C is new A with private;
3536 -- private
3537 -- type B is new C with null record;
3538 -- type C is new B with null record;
3539 -- end P;
3541 and then Parent_Typ /= Tagged_Type
3542 then
3543 Collect (Parent_Typ);
3544 end if;
3546 -- Collect the components containing tags of secondary dispatch
3547 -- tables.
3549 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
3550 while Present (Tag_Comp) loop
3551 pragma Assert (Present (Related_Type (Tag_Comp)));
3552 Append_Elmt (Tag_Comp, Components_List);
3554 Tag_Comp := Next_Tag_Component (Tag_Comp);
3555 end loop;
3556 end Collect;
3558 -- Start of processing for Collect_Interface_Components
3560 begin
3561 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
3562 and then Is_Tagged_Type (Tagged_Type));
3564 Components_List := New_Elmt_List;
3565 Collect (Tagged_Type);
3566 end Collect_Interface_Components;
3568 -----------------------------
3569 -- Collect_Interfaces_Info --
3570 -----------------------------
3572 procedure Collect_Interfaces_Info
3573 (T : Entity_Id;
3574 Ifaces_List : out Elist_Id;
3575 Components_List : out Elist_Id;
3576 Tags_List : out Elist_Id)
3578 Comps_List : Elist_Id;
3579 Comp_Elmt : Elmt_Id;
3580 Comp_Iface : Entity_Id;
3581 Iface_Elmt : Elmt_Id;
3582 Iface : Entity_Id;
3584 function Search_Tag (Iface : Entity_Id) return Entity_Id;
3585 -- Search for the secondary tag associated with the interface type
3586 -- Iface that is implemented by T.
3588 ----------------
3589 -- Search_Tag --
3590 ----------------
3592 function Search_Tag (Iface : Entity_Id) return Entity_Id is
3593 ADT : Elmt_Id;
3594 begin
3595 if not Is_CPP_Class (T) then
3596 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
3597 else
3598 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
3599 end if;
3601 while Present (ADT)
3602 and then Is_Tag (Node (ADT))
3603 and then Related_Type (Node (ADT)) /= Iface
3604 loop
3605 -- Skip secondary dispatch table referencing thunks to user
3606 -- defined primitives covered by this interface.
3608 pragma Assert (Has_Suffix (Node (ADT), 'P'));
3609 Next_Elmt (ADT);
3611 -- Skip secondary dispatch tables of Ada types
3613 if not Is_CPP_Class (T) then
3615 -- Skip secondary dispatch table referencing thunks to
3616 -- predefined primitives.
3618 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
3619 Next_Elmt (ADT);
3621 -- Skip secondary dispatch table referencing user-defined
3622 -- primitives covered by this interface.
3624 pragma Assert (Has_Suffix (Node (ADT), 'D'));
3625 Next_Elmt (ADT);
3627 -- Skip secondary dispatch table referencing predefined
3628 -- primitives.
3630 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
3631 Next_Elmt (ADT);
3632 end if;
3633 end loop;
3635 pragma Assert (Is_Tag (Node (ADT)));
3636 return Node (ADT);
3637 end Search_Tag;
3639 -- Start of processing for Collect_Interfaces_Info
3641 begin
3642 Collect_Interfaces (T, Ifaces_List);
3643 Collect_Interface_Components (T, Comps_List);
3645 -- Search for the record component and tag associated with each
3646 -- interface type of T.
3648 Components_List := New_Elmt_List;
3649 Tags_List := New_Elmt_List;
3651 Iface_Elmt := First_Elmt (Ifaces_List);
3652 while Present (Iface_Elmt) loop
3653 Iface := Node (Iface_Elmt);
3655 -- Associate the primary tag component and the primary dispatch table
3656 -- with all the interfaces that are parents of T
3658 if Is_Ancestor (Iface, T, Use_Full_View => True) then
3659 Append_Elmt (First_Tag_Component (T), Components_List);
3660 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
3662 -- Otherwise search for the tag component and secondary dispatch
3663 -- table of Iface
3665 else
3666 Comp_Elmt := First_Elmt (Comps_List);
3667 while Present (Comp_Elmt) loop
3668 Comp_Iface := Related_Type (Node (Comp_Elmt));
3670 if Comp_Iface = Iface
3671 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
3672 then
3673 Append_Elmt (Node (Comp_Elmt), Components_List);
3674 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
3675 exit;
3676 end if;
3678 Next_Elmt (Comp_Elmt);
3679 end loop;
3680 pragma Assert (Present (Comp_Elmt));
3681 end if;
3683 Next_Elmt (Iface_Elmt);
3684 end loop;
3685 end Collect_Interfaces_Info;
3687 ---------------------
3688 -- Collect_Parents --
3689 ---------------------
3691 procedure Collect_Parents
3692 (T : Entity_Id;
3693 List : out Elist_Id;
3694 Use_Full_View : Boolean := True)
3696 Current_Typ : Entity_Id := T;
3697 Parent_Typ : Entity_Id;
3699 begin
3700 List := New_Elmt_List;
3702 -- No action if the if the type has no parents
3704 if T = Etype (T) then
3705 return;
3706 end if;
3708 loop
3709 Parent_Typ := Etype (Current_Typ);
3711 if Is_Private_Type (Parent_Typ)
3712 and then Present (Full_View (Parent_Typ))
3713 and then Use_Full_View
3714 then
3715 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3716 end if;
3718 Append_Elmt (Parent_Typ, List);
3720 exit when Parent_Typ = Current_Typ;
3721 Current_Typ := Parent_Typ;
3722 end loop;
3723 end Collect_Parents;
3725 ----------------------------------
3726 -- Collect_Primitive_Operations --
3727 ----------------------------------
3729 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
3730 B_Type : constant Entity_Id := Base_Type (T);
3731 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
3732 B_Scope : Entity_Id := Scope (B_Type);
3733 Op_List : Elist_Id;
3734 Formal : Entity_Id;
3735 Is_Prim : Boolean;
3736 Is_Type_In_Pkg : Boolean;
3737 Formal_Derived : Boolean := False;
3738 Id : Entity_Id;
3740 function Match (E : Entity_Id) return Boolean;
3741 -- True if E's base type is B_Type, or E is of an anonymous access type
3742 -- and the base type of its designated type is B_Type.
3744 -----------
3745 -- Match --
3746 -----------
3748 function Match (E : Entity_Id) return Boolean is
3749 Etyp : Entity_Id := Etype (E);
3751 begin
3752 if Ekind (Etyp) = E_Anonymous_Access_Type then
3753 Etyp := Designated_Type (Etyp);
3754 end if;
3756 -- In Ada 2012 a primitive operation may have a formal of an
3757 -- incomplete view of the parent type.
3759 return Base_Type (Etyp) = B_Type
3760 or else
3761 (Ada_Version >= Ada_2012
3762 and then Ekind (Etyp) = E_Incomplete_Type
3763 and then Full_View (Etyp) = B_Type);
3764 end Match;
3766 -- Start of processing for Collect_Primitive_Operations
3768 begin
3769 -- For tagged types, the primitive operations are collected as they
3770 -- are declared, and held in an explicit list which is simply returned.
3772 if Is_Tagged_Type (B_Type) then
3773 return Primitive_Operations (B_Type);
3775 -- An untagged generic type that is a derived type inherits the
3776 -- primitive operations of its parent type. Other formal types only
3777 -- have predefined operators, which are not explicitly represented.
3779 elsif Is_Generic_Type (B_Type) then
3780 if Nkind (B_Decl) = N_Formal_Type_Declaration
3781 and then Nkind (Formal_Type_Definition (B_Decl)) =
3782 N_Formal_Derived_Type_Definition
3783 then
3784 Formal_Derived := True;
3785 else
3786 return New_Elmt_List;
3787 end if;
3788 end if;
3790 Op_List := New_Elmt_List;
3792 if B_Scope = Standard_Standard then
3793 if B_Type = Standard_String then
3794 Append_Elmt (Standard_Op_Concat, Op_List);
3796 elsif B_Type = Standard_Wide_String then
3797 Append_Elmt (Standard_Op_Concatw, Op_List);
3799 else
3800 null;
3801 end if;
3803 -- Locate the primitive subprograms of the type
3805 else
3806 -- The primitive operations appear after the base type, except
3807 -- if the derivation happens within the private part of B_Scope
3808 -- and the type is a private type, in which case both the type
3809 -- and some primitive operations may appear before the base
3810 -- type, and the list of candidates starts after the type.
3812 if In_Open_Scopes (B_Scope)
3813 and then Scope (T) = B_Scope
3814 and then In_Private_Part (B_Scope)
3815 then
3816 Id := Next_Entity (T);
3818 -- In Ada 2012, If the type has an incomplete partial view, there
3819 -- may be primitive operations declared before the full view, so
3820 -- we need to start scanning from the incomplete view, which is
3821 -- earlier on the entity chain.
3823 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
3824 and then Present (Incomplete_View (Parent (B_Type)))
3825 then
3826 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
3828 else
3829 Id := Next_Entity (B_Type);
3830 end if;
3832 -- Set flag if this is a type in a package spec
3834 Is_Type_In_Pkg :=
3835 Is_Package_Or_Generic_Package (B_Scope)
3836 and then
3837 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
3838 N_Package_Body;
3840 while Present (Id) loop
3842 -- Test whether the result type or any of the parameter types of
3843 -- each subprogram following the type match that type when the
3844 -- type is declared in a package spec, is a derived type, or the
3845 -- subprogram is marked as primitive. (The Is_Primitive test is
3846 -- needed to find primitives of nonderived types in declarative
3847 -- parts that happen to override the predefined "=" operator.)
3849 -- Note that generic formal subprograms are not considered to be
3850 -- primitive operations and thus are never inherited.
3852 if Is_Overloadable (Id)
3853 and then (Is_Type_In_Pkg
3854 or else Is_Derived_Type (B_Type)
3855 or else Is_Primitive (Id))
3856 and then Nkind (Parent (Parent (Id)))
3857 not in N_Formal_Subprogram_Declaration
3858 then
3859 Is_Prim := False;
3861 if Match (Id) then
3862 Is_Prim := True;
3864 else
3865 Formal := First_Formal (Id);
3866 while Present (Formal) loop
3867 if Match (Formal) then
3868 Is_Prim := True;
3869 exit;
3870 end if;
3872 Next_Formal (Formal);
3873 end loop;
3874 end if;
3876 -- For a formal derived type, the only primitives are the ones
3877 -- inherited from the parent type. Operations appearing in the
3878 -- package declaration are not primitive for it.
3880 if Is_Prim
3881 and then (not Formal_Derived or else Present (Alias (Id)))
3882 then
3883 -- In the special case of an equality operator aliased to
3884 -- an overriding dispatching equality belonging to the same
3885 -- type, we don't include it in the list of primitives.
3886 -- This avoids inheriting multiple equality operators when
3887 -- deriving from untagged private types whose full type is
3888 -- tagged, which can otherwise cause ambiguities. Note that
3889 -- this should only happen for this kind of untagged parent
3890 -- type, since normally dispatching operations are inherited
3891 -- using the type's Primitive_Operations list.
3893 if Chars (Id) = Name_Op_Eq
3894 and then Is_Dispatching_Operation (Id)
3895 and then Present (Alias (Id))
3896 and then Present (Overridden_Operation (Alias (Id)))
3897 and then Base_Type (Etype (First_Entity (Id))) =
3898 Base_Type (Etype (First_Entity (Alias (Id))))
3899 then
3900 null;
3902 -- Include the subprogram in the list of primitives
3904 else
3905 Append_Elmt (Id, Op_List);
3906 end if;
3907 end if;
3908 end if;
3910 Next_Entity (Id);
3912 -- For a type declared in System, some of its operations may
3913 -- appear in the target-specific extension to System.
3915 if No (Id)
3916 and then B_Scope = RTU_Entity (System)
3917 and then Present_System_Aux
3918 then
3919 B_Scope := System_Aux_Id;
3920 Id := First_Entity (System_Aux_Id);
3921 end if;
3922 end loop;
3923 end if;
3925 return Op_List;
3926 end Collect_Primitive_Operations;
3928 -----------------------------------
3929 -- Compile_Time_Constraint_Error --
3930 -----------------------------------
3932 function Compile_Time_Constraint_Error
3933 (N : Node_Id;
3934 Msg : String;
3935 Ent : Entity_Id := Empty;
3936 Loc : Source_Ptr := No_Location;
3937 Warn : Boolean := False) return Node_Id
3939 Msgc : String (1 .. Msg'Length + 3);
3940 -- Copy of message, with room for possible ?? or << and ! at end
3942 Msgl : Natural;
3943 Wmsg : Boolean;
3944 Eloc : Source_Ptr;
3946 -- Start of processing for Compile_Time_Constraint_Error
3948 begin
3949 -- If this is a warning, convert it into an error if we are in code
3950 -- subject to SPARK_Mode being set ON.
3952 Error_Msg_Warn := SPARK_Mode /= On;
3954 -- A static constraint error in an instance body is not a fatal error.
3955 -- we choose to inhibit the message altogether, because there is no
3956 -- obvious node (for now) on which to post it. On the other hand the
3957 -- offending node must be replaced with a constraint_error in any case.
3959 -- No messages are generated if we already posted an error on this node
3961 if not Error_Posted (N) then
3962 if Loc /= No_Location then
3963 Eloc := Loc;
3964 else
3965 Eloc := Sloc (N);
3966 end if;
3968 -- Copy message to Msgc, converting any ? in the message into
3969 -- < instead, so that we have an error in GNATprove mode.
3971 Msgl := Msg'Length;
3973 for J in 1 .. Msgl loop
3974 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
3975 Msgc (J) := '<';
3976 else
3977 Msgc (J) := Msg (J);
3978 end if;
3979 end loop;
3981 -- Message is a warning, even in Ada 95 case
3983 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
3984 Wmsg := True;
3986 -- In Ada 83, all messages are warnings. In the private part and
3987 -- the body of an instance, constraint_checks are only warnings.
3988 -- We also make this a warning if the Warn parameter is set.
3990 elsif Warn
3991 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
3992 then
3993 Msgl := Msgl + 1;
3994 Msgc (Msgl) := '<';
3995 Msgl := Msgl + 1;
3996 Msgc (Msgl) := '<';
3997 Wmsg := True;
3999 elsif In_Instance_Not_Visible then
4000 Msgl := Msgl + 1;
4001 Msgc (Msgl) := '<';
4002 Msgl := Msgl + 1;
4003 Msgc (Msgl) := '<';
4004 Wmsg := True;
4006 -- Otherwise we have a real error message (Ada 95 static case)
4007 -- and we make this an unconditional message. Note that in the
4008 -- warning case we do not make the message unconditional, it seems
4009 -- quite reasonable to delete messages like this (about exceptions
4010 -- that will be raised) in dead code.
4012 else
4013 Wmsg := False;
4014 Msgl := Msgl + 1;
4015 Msgc (Msgl) := '!';
4016 end if;
4018 -- One more test, skip the warning if the related expression is
4019 -- statically unevaluated, since we don't want to warn about what
4020 -- will happen when something is evaluated if it never will be
4021 -- evaluated.
4023 if not Is_Statically_Unevaluated (N) then
4024 Error_Msg_Warn := SPARK_Mode /= On;
4026 if Present (Ent) then
4027 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4028 else
4029 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4030 end if;
4032 if Wmsg then
4034 -- Check whether the context is an Init_Proc
4036 if Inside_Init_Proc then
4037 declare
4038 Conc_Typ : constant Entity_Id :=
4039 Corresponding_Concurrent_Type
4040 (Entity (Parameter_Type (First
4041 (Parameter_Specifications
4042 (Parent (Current_Scope))))));
4044 begin
4045 -- Don't complain if the corresponding concurrent type
4046 -- doesn't come from source (i.e. a single task/protected
4047 -- object).
4049 if Present (Conc_Typ)
4050 and then not Comes_From_Source (Conc_Typ)
4051 then
4052 Error_Msg_NEL
4053 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4055 else
4056 if GNATprove_Mode then
4057 Error_Msg_NEL
4058 ("\& would have been raised for objects of this "
4059 & "type", N, Standard_Constraint_Error, Eloc);
4060 else
4061 Error_Msg_NEL
4062 ("\& will be raised for objects of this type??",
4063 N, Standard_Constraint_Error, Eloc);
4064 end if;
4065 end if;
4066 end;
4068 else
4069 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4070 end if;
4072 else
4073 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4074 Set_Error_Posted (N);
4075 end if;
4076 end if;
4077 end if;
4079 return N;
4080 end Compile_Time_Constraint_Error;
4082 -----------------------
4083 -- Conditional_Delay --
4084 -----------------------
4086 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4087 begin
4088 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4089 Set_Has_Delayed_Freeze (New_Ent);
4090 end if;
4091 end Conditional_Delay;
4093 ----------------------------
4094 -- Contains_Refined_State --
4095 ----------------------------
4097 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4098 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4099 -- Determine whether a dependency list mentions a state with a visible
4100 -- refinement.
4102 function Has_State_In_Global (List : Node_Id) return Boolean;
4103 -- Determine whether a global list mentions a state with a visible
4104 -- refinement.
4106 function Is_Refined_State (Item : Node_Id) return Boolean;
4107 -- Determine whether Item is a reference to an abstract state with a
4108 -- visible refinement.
4110 -----------------------------
4111 -- Has_State_In_Dependency --
4112 -----------------------------
4114 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4115 Clause : Node_Id;
4116 Output : Node_Id;
4118 begin
4119 -- A null dependency list does not mention any states
4121 if Nkind (List) = N_Null then
4122 return False;
4124 -- Dependency clauses appear as component associations of an
4125 -- aggregate.
4127 elsif Nkind (List) = N_Aggregate
4128 and then Present (Component_Associations (List))
4129 then
4130 Clause := First (Component_Associations (List));
4131 while Present (Clause) loop
4133 -- Inspect the outputs of a dependency clause
4135 Output := First (Choices (Clause));
4136 while Present (Output) loop
4137 if Is_Refined_State (Output) then
4138 return True;
4139 end if;
4141 Next (Output);
4142 end loop;
4144 -- Inspect the outputs of a dependency clause
4146 if Is_Refined_State (Expression (Clause)) then
4147 return True;
4148 end if;
4150 Next (Clause);
4151 end loop;
4153 -- If we get here, then none of the dependency clauses mention a
4154 -- state with visible refinement.
4156 return False;
4158 -- An illegal pragma managed to sneak in
4160 else
4161 raise Program_Error;
4162 end if;
4163 end Has_State_In_Dependency;
4165 -------------------------
4166 -- Has_State_In_Global --
4167 -------------------------
4169 function Has_State_In_Global (List : Node_Id) return Boolean is
4170 Item : Node_Id;
4172 begin
4173 -- A null global list does not mention any states
4175 if Nkind (List) = N_Null then
4176 return False;
4178 -- Simple global list or moded global list declaration
4180 elsif Nkind (List) = N_Aggregate then
4182 -- The declaration of a simple global list appear as a collection
4183 -- of expressions.
4185 if Present (Expressions (List)) then
4186 Item := First (Expressions (List));
4187 while Present (Item) loop
4188 if Is_Refined_State (Item) then
4189 return True;
4190 end if;
4192 Next (Item);
4193 end loop;
4195 -- The declaration of a moded global list appears as a collection
4196 -- of component associations where individual choices denote
4197 -- modes.
4199 else
4200 Item := First (Component_Associations (List));
4201 while Present (Item) loop
4202 if Has_State_In_Global (Expression (Item)) then
4203 return True;
4204 end if;
4206 Next (Item);
4207 end loop;
4208 end if;
4210 -- If we get here, then the simple/moded global list did not
4211 -- mention any states with a visible refinement.
4213 return False;
4215 -- Single global item declaration
4217 elsif Is_Entity_Name (List) then
4218 return Is_Refined_State (List);
4220 -- An illegal pragma managed to sneak in
4222 else
4223 raise Program_Error;
4224 end if;
4225 end Has_State_In_Global;
4227 ----------------------
4228 -- Is_Refined_State --
4229 ----------------------
4231 function Is_Refined_State (Item : Node_Id) return Boolean is
4232 Elmt : Node_Id;
4233 Item_Id : Entity_Id;
4235 begin
4236 if Nkind (Item) = N_Null then
4237 return False;
4239 -- States cannot be subject to attribute 'Result. This case arises
4240 -- in dependency relations.
4242 elsif Nkind (Item) = N_Attribute_Reference
4243 and then Attribute_Name (Item) = Name_Result
4244 then
4245 return False;
4247 -- Multiple items appear as an aggregate. This case arises in
4248 -- dependency relations.
4250 elsif Nkind (Item) = N_Aggregate
4251 and then Present (Expressions (Item))
4252 then
4253 Elmt := First (Expressions (Item));
4254 while Present (Elmt) loop
4255 if Is_Refined_State (Elmt) then
4256 return True;
4257 end if;
4259 Next (Elmt);
4260 end loop;
4262 -- If we get here, then none of the inputs or outputs reference a
4263 -- state with visible refinement.
4265 return False;
4267 -- Single item
4269 else
4270 Item_Id := Entity_Of (Item);
4272 return
4273 Present (Item_Id)
4274 and then Ekind (Item_Id) = E_Abstract_State
4275 and then Has_Visible_Refinement (Item_Id);
4276 end if;
4277 end Is_Refined_State;
4279 -- Local variables
4281 Arg : constant Node_Id :=
4282 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4283 Nam : constant Name_Id := Pragma_Name (Prag);
4285 -- Start of processing for Contains_Refined_State
4287 begin
4288 if Nam = Name_Depends then
4289 return Has_State_In_Dependency (Arg);
4291 else pragma Assert (Nam = Name_Global);
4292 return Has_State_In_Global (Arg);
4293 end if;
4294 end Contains_Refined_State;
4296 -------------------------
4297 -- Copy_Component_List --
4298 -------------------------
4300 function Copy_Component_List
4301 (R_Typ : Entity_Id;
4302 Loc : Source_Ptr) return List_Id
4304 Comp : Node_Id;
4305 Comps : constant List_Id := New_List;
4307 begin
4308 Comp := First_Component (Underlying_Type (R_Typ));
4309 while Present (Comp) loop
4310 if Comes_From_Source (Comp) then
4311 declare
4312 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4313 begin
4314 Append_To (Comps,
4315 Make_Component_Declaration (Loc,
4316 Defining_Identifier =>
4317 Make_Defining_Identifier (Loc, Chars (Comp)),
4318 Component_Definition =>
4319 New_Copy_Tree
4320 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4321 end;
4322 end if;
4324 Next_Component (Comp);
4325 end loop;
4327 return Comps;
4328 end Copy_Component_List;
4330 -------------------------
4331 -- Copy_Parameter_List --
4332 -------------------------
4334 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4335 Loc : constant Source_Ptr := Sloc (Subp_Id);
4336 Plist : List_Id;
4337 Formal : Entity_Id;
4339 begin
4340 if No (First_Formal (Subp_Id)) then
4341 return No_List;
4342 else
4343 Plist := New_List;
4344 Formal := First_Formal (Subp_Id);
4345 while Present (Formal) loop
4346 Append
4347 (Make_Parameter_Specification (Loc,
4348 Defining_Identifier =>
4349 Make_Defining_Identifier (Sloc (Formal),
4350 Chars => Chars (Formal)),
4351 In_Present => In_Present (Parent (Formal)),
4352 Out_Present => Out_Present (Parent (Formal)),
4353 Parameter_Type =>
4354 New_Occurrence_Of (Etype (Formal), Loc),
4355 Expression =>
4356 New_Copy_Tree (Expression (Parent (Formal)))),
4357 Plist);
4359 Next_Formal (Formal);
4360 end loop;
4361 end if;
4363 return Plist;
4364 end Copy_Parameter_List;
4366 --------------------------------
4367 -- Corresponding_Generic_Type --
4368 --------------------------------
4370 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
4371 Inst : Entity_Id;
4372 Gen : Entity_Id;
4373 Typ : Entity_Id;
4375 begin
4376 if not Is_Generic_Actual_Type (T) then
4377 return Any_Type;
4379 -- If the actual is the actual of an enclosing instance, resolution
4380 -- was correct in the generic.
4382 elsif Nkind (Parent (T)) = N_Subtype_Declaration
4383 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
4384 and then
4385 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
4386 then
4387 return Any_Type;
4389 else
4390 Inst := Scope (T);
4392 if Is_Wrapper_Package (Inst) then
4393 Inst := Related_Instance (Inst);
4394 end if;
4396 Gen :=
4397 Generic_Parent
4398 (Specification (Unit_Declaration_Node (Inst)));
4400 -- Generic actual has the same name as the corresponding formal
4402 Typ := First_Entity (Gen);
4403 while Present (Typ) loop
4404 if Chars (Typ) = Chars (T) then
4405 return Typ;
4406 end if;
4408 Next_Entity (Typ);
4409 end loop;
4411 return Any_Type;
4412 end if;
4413 end Corresponding_Generic_Type;
4415 --------------------
4416 -- Current_Entity --
4417 --------------------
4419 -- The currently visible definition for a given identifier is the
4420 -- one most chained at the start of the visibility chain, i.e. the
4421 -- one that is referenced by the Node_Id value of the name of the
4422 -- given identifier.
4424 function Current_Entity (N : Node_Id) return Entity_Id is
4425 begin
4426 return Get_Name_Entity_Id (Chars (N));
4427 end Current_Entity;
4429 -----------------------------
4430 -- Current_Entity_In_Scope --
4431 -----------------------------
4433 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
4434 E : Entity_Id;
4435 CS : constant Entity_Id := Current_Scope;
4437 Transient_Case : constant Boolean := Scope_Is_Transient;
4439 begin
4440 E := Get_Name_Entity_Id (Chars (N));
4441 while Present (E)
4442 and then Scope (E) /= CS
4443 and then (not Transient_Case or else Scope (E) /= Scope (CS))
4444 loop
4445 E := Homonym (E);
4446 end loop;
4448 return E;
4449 end Current_Entity_In_Scope;
4451 -------------------
4452 -- Current_Scope --
4453 -------------------
4455 function Current_Scope return Entity_Id is
4456 begin
4457 if Scope_Stack.Last = -1 then
4458 return Standard_Standard;
4459 else
4460 declare
4461 C : constant Entity_Id :=
4462 Scope_Stack.Table (Scope_Stack.Last).Entity;
4463 begin
4464 if Present (C) then
4465 return C;
4466 else
4467 return Standard_Standard;
4468 end if;
4469 end;
4470 end if;
4471 end Current_Scope;
4473 ------------------------
4474 -- Current_Subprogram --
4475 ------------------------
4477 function Current_Subprogram return Entity_Id is
4478 Scop : constant Entity_Id := Current_Scope;
4479 begin
4480 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
4481 return Scop;
4482 else
4483 return Enclosing_Subprogram (Scop);
4484 end if;
4485 end Current_Subprogram;
4487 ----------------------------------
4488 -- Deepest_Type_Access_Level --
4489 ----------------------------------
4491 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
4492 begin
4493 if Ekind (Typ) = E_Anonymous_Access_Type
4494 and then not Is_Local_Anonymous_Access (Typ)
4495 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
4496 then
4497 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
4498 -- access type.
4500 return
4501 Scope_Depth (Enclosing_Dynamic_Scope
4502 (Defining_Identifier
4503 (Associated_Node_For_Itype (Typ))));
4505 -- For generic formal type, return Int'Last (infinite).
4506 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
4508 elsif Is_Generic_Type (Root_Type (Typ)) then
4509 return UI_From_Int (Int'Last);
4511 else
4512 return Type_Access_Level (Typ);
4513 end if;
4514 end Deepest_Type_Access_Level;
4516 ---------------------
4517 -- Defining_Entity --
4518 ---------------------
4520 function Defining_Entity (N : Node_Id) return Entity_Id is
4521 K : constant Node_Kind := Nkind (N);
4522 Err : Entity_Id := Empty;
4524 begin
4525 case K is
4526 when
4527 N_Subprogram_Declaration |
4528 N_Abstract_Subprogram_Declaration |
4529 N_Subprogram_Body |
4530 N_Package_Declaration |
4531 N_Subprogram_Renaming_Declaration |
4532 N_Subprogram_Body_Stub |
4533 N_Generic_Subprogram_Declaration |
4534 N_Generic_Package_Declaration |
4535 N_Formal_Subprogram_Declaration |
4536 N_Expression_Function
4538 return Defining_Entity (Specification (N));
4540 when
4541 N_Component_Declaration |
4542 N_Defining_Program_Unit_Name |
4543 N_Discriminant_Specification |
4544 N_Entry_Body |
4545 N_Entry_Declaration |
4546 N_Entry_Index_Specification |
4547 N_Exception_Declaration |
4548 N_Exception_Renaming_Declaration |
4549 N_Formal_Object_Declaration |
4550 N_Formal_Package_Declaration |
4551 N_Formal_Type_Declaration |
4552 N_Full_Type_Declaration |
4553 N_Implicit_Label_Declaration |
4554 N_Incomplete_Type_Declaration |
4555 N_Loop_Parameter_Specification |
4556 N_Number_Declaration |
4557 N_Object_Declaration |
4558 N_Object_Renaming_Declaration |
4559 N_Package_Body_Stub |
4560 N_Parameter_Specification |
4561 N_Private_Extension_Declaration |
4562 N_Private_Type_Declaration |
4563 N_Protected_Body |
4564 N_Protected_Body_Stub |
4565 N_Protected_Type_Declaration |
4566 N_Single_Protected_Declaration |
4567 N_Single_Task_Declaration |
4568 N_Subtype_Declaration |
4569 N_Task_Body |
4570 N_Task_Body_Stub |
4571 N_Task_Type_Declaration
4573 return Defining_Identifier (N);
4575 when N_Subunit =>
4576 return Defining_Entity (Proper_Body (N));
4578 when
4579 N_Function_Instantiation |
4580 N_Function_Specification |
4581 N_Generic_Function_Renaming_Declaration |
4582 N_Generic_Package_Renaming_Declaration |
4583 N_Generic_Procedure_Renaming_Declaration |
4584 N_Package_Body |
4585 N_Package_Instantiation |
4586 N_Package_Renaming_Declaration |
4587 N_Package_Specification |
4588 N_Procedure_Instantiation |
4589 N_Procedure_Specification
4591 declare
4592 Nam : constant Node_Id := Defining_Unit_Name (N);
4594 begin
4595 if Nkind (Nam) in N_Entity then
4596 return Nam;
4598 -- For Error, make up a name and attach to declaration
4599 -- so we can continue semantic analysis
4601 elsif Nam = Error then
4602 Err := Make_Temporary (Sloc (N), 'T');
4603 Set_Defining_Unit_Name (N, Err);
4605 return Err;
4607 -- If not an entity, get defining identifier
4609 else
4610 return Defining_Identifier (Nam);
4611 end if;
4612 end;
4614 when
4615 N_Block_Statement |
4616 N_Loop_Statement
4618 return Entity (Identifier (N));
4620 when others =>
4621 raise Program_Error;
4623 end case;
4624 end Defining_Entity;
4626 --------------------------
4627 -- Denotes_Discriminant --
4628 --------------------------
4630 function Denotes_Discriminant
4631 (N : Node_Id;
4632 Check_Concurrent : Boolean := False) return Boolean
4634 E : Entity_Id;
4636 begin
4637 if not Is_Entity_Name (N) or else No (Entity (N)) then
4638 return False;
4639 else
4640 E := Entity (N);
4641 end if;
4643 -- If we are checking for a protected type, the discriminant may have
4644 -- been rewritten as the corresponding discriminal of the original type
4645 -- or of the corresponding concurrent record, depending on whether we
4646 -- are in the spec or body of the protected type.
4648 return Ekind (E) = E_Discriminant
4649 or else
4650 (Check_Concurrent
4651 and then Ekind (E) = E_In_Parameter
4652 and then Present (Discriminal_Link (E))
4653 and then
4654 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
4655 or else
4656 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
4658 end Denotes_Discriminant;
4660 -------------------------
4661 -- Denotes_Same_Object --
4662 -------------------------
4664 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
4665 Obj1 : Node_Id := A1;
4666 Obj2 : Node_Id := A2;
4668 function Has_Prefix (N : Node_Id) return Boolean;
4669 -- Return True if N has attribute Prefix
4671 function Is_Renaming (N : Node_Id) return Boolean;
4672 -- Return true if N names a renaming entity
4674 function Is_Valid_Renaming (N : Node_Id) return Boolean;
4675 -- For renamings, return False if the prefix of any dereference within
4676 -- the renamed object_name is a variable, or any expression within the
4677 -- renamed object_name contains references to variables or calls on
4678 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
4680 ----------------
4681 -- Has_Prefix --
4682 ----------------
4684 function Has_Prefix (N : Node_Id) return Boolean is
4685 begin
4686 return
4687 Nkind_In (N,
4688 N_Attribute_Reference,
4689 N_Expanded_Name,
4690 N_Explicit_Dereference,
4691 N_Indexed_Component,
4692 N_Reference,
4693 N_Selected_Component,
4694 N_Slice);
4695 end Has_Prefix;
4697 -----------------
4698 -- Is_Renaming --
4699 -----------------
4701 function Is_Renaming (N : Node_Id) return Boolean is
4702 begin
4703 return Is_Entity_Name (N)
4704 and then Present (Renamed_Entity (Entity (N)));
4705 end Is_Renaming;
4707 -----------------------
4708 -- Is_Valid_Renaming --
4709 -----------------------
4711 function Is_Valid_Renaming (N : Node_Id) return Boolean is
4713 function Check_Renaming (N : Node_Id) return Boolean;
4714 -- Recursive function used to traverse all the prefixes of N
4716 function Check_Renaming (N : Node_Id) return Boolean is
4717 begin
4718 if Is_Renaming (N)
4719 and then not Check_Renaming (Renamed_Entity (Entity (N)))
4720 then
4721 return False;
4722 end if;
4724 if Nkind (N) = N_Indexed_Component then
4725 declare
4726 Indx : Node_Id;
4728 begin
4729 Indx := First (Expressions (N));
4730 while Present (Indx) loop
4731 if not Is_OK_Static_Expression (Indx) then
4732 return False;
4733 end if;
4735 Next_Index (Indx);
4736 end loop;
4737 end;
4738 end if;
4740 if Has_Prefix (N) then
4741 declare
4742 P : constant Node_Id := Prefix (N);
4744 begin
4745 if Nkind (N) = N_Explicit_Dereference
4746 and then Is_Variable (P)
4747 then
4748 return False;
4750 elsif Is_Entity_Name (P)
4751 and then Ekind (Entity (P)) = E_Function
4752 then
4753 return False;
4755 elsif Nkind (P) = N_Function_Call then
4756 return False;
4757 end if;
4759 -- Recursion to continue traversing the prefix of the
4760 -- renaming expression
4762 return Check_Renaming (P);
4763 end;
4764 end if;
4766 return True;
4767 end Check_Renaming;
4769 -- Start of processing for Is_Valid_Renaming
4771 begin
4772 return Check_Renaming (N);
4773 end Is_Valid_Renaming;
4775 -- Start of processing for Denotes_Same_Object
4777 begin
4778 -- Both names statically denote the same stand-alone object or parameter
4779 -- (RM 6.4.1(6.5/3))
4781 if Is_Entity_Name (Obj1)
4782 and then Is_Entity_Name (Obj2)
4783 and then Entity (Obj1) = Entity (Obj2)
4784 then
4785 return True;
4786 end if;
4788 -- For renamings, the prefix of any dereference within the renamed
4789 -- object_name is not a variable, and any expression within the
4790 -- renamed object_name contains no references to variables nor
4791 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
4793 if Is_Renaming (Obj1) then
4794 if Is_Valid_Renaming (Obj1) then
4795 Obj1 := Renamed_Entity (Entity (Obj1));
4796 else
4797 return False;
4798 end if;
4799 end if;
4801 if Is_Renaming (Obj2) then
4802 if Is_Valid_Renaming (Obj2) then
4803 Obj2 := Renamed_Entity (Entity (Obj2));
4804 else
4805 return False;
4806 end if;
4807 end if;
4809 -- No match if not same node kind (such cases are handled by
4810 -- Denotes_Same_Prefix)
4812 if Nkind (Obj1) /= Nkind (Obj2) then
4813 return False;
4815 -- After handling valid renamings, one of the two names statically
4816 -- denoted a renaming declaration whose renamed object_name is known
4817 -- to denote the same object as the other (RM 6.4.1(6.10/3))
4819 elsif Is_Entity_Name (Obj1) then
4820 if Is_Entity_Name (Obj2) then
4821 return Entity (Obj1) = Entity (Obj2);
4822 else
4823 return False;
4824 end if;
4826 -- Both names are selected_components, their prefixes are known to
4827 -- denote the same object, and their selector_names denote the same
4828 -- component (RM 6.4.1(6.6/3)
4830 elsif Nkind (Obj1) = N_Selected_Component then
4831 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4832 and then
4833 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
4835 -- Both names are dereferences and the dereferenced names are known to
4836 -- denote the same object (RM 6.4.1(6.7/3))
4838 elsif Nkind (Obj1) = N_Explicit_Dereference then
4839 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
4841 -- Both names are indexed_components, their prefixes are known to denote
4842 -- the same object, and each of the pairs of corresponding index values
4843 -- are either both static expressions with the same static value or both
4844 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
4846 elsif Nkind (Obj1) = N_Indexed_Component then
4847 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
4848 return False;
4849 else
4850 declare
4851 Indx1 : Node_Id;
4852 Indx2 : Node_Id;
4854 begin
4855 Indx1 := First (Expressions (Obj1));
4856 Indx2 := First (Expressions (Obj2));
4857 while Present (Indx1) loop
4859 -- Indexes must denote the same static value or same object
4861 if Is_OK_Static_Expression (Indx1) then
4862 if not Is_OK_Static_Expression (Indx2) then
4863 return False;
4865 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
4866 return False;
4867 end if;
4869 elsif not Denotes_Same_Object (Indx1, Indx2) then
4870 return False;
4871 end if;
4873 Next (Indx1);
4874 Next (Indx2);
4875 end loop;
4877 return True;
4878 end;
4879 end if;
4881 -- Both names are slices, their prefixes are known to denote the same
4882 -- object, and the two slices have statically matching index constraints
4883 -- (RM 6.4.1(6.9/3))
4885 elsif Nkind (Obj1) = N_Slice
4886 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4887 then
4888 declare
4889 Lo1, Lo2, Hi1, Hi2 : Node_Id;
4891 begin
4892 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
4893 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
4895 -- Check whether bounds are statically identical. There is no
4896 -- attempt to detect partial overlap of slices.
4898 return Denotes_Same_Object (Lo1, Lo2)
4899 and then
4900 Denotes_Same_Object (Hi1, Hi2);
4901 end;
4903 -- In the recursion, literals appear as indexes
4905 elsif Nkind (Obj1) = N_Integer_Literal
4906 and then
4907 Nkind (Obj2) = N_Integer_Literal
4908 then
4909 return Intval (Obj1) = Intval (Obj2);
4911 else
4912 return False;
4913 end if;
4914 end Denotes_Same_Object;
4916 -------------------------
4917 -- Denotes_Same_Prefix --
4918 -------------------------
4920 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
4922 begin
4923 if Is_Entity_Name (A1) then
4924 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
4925 and then not Is_Access_Type (Etype (A1))
4926 then
4927 return Denotes_Same_Object (A1, Prefix (A2))
4928 or else Denotes_Same_Prefix (A1, Prefix (A2));
4929 else
4930 return False;
4931 end if;
4933 elsif Is_Entity_Name (A2) then
4934 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
4936 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
4937 and then
4938 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
4939 then
4940 declare
4941 Root1, Root2 : Node_Id;
4942 Depth1, Depth2 : Int := 0;
4944 begin
4945 Root1 := Prefix (A1);
4946 while not Is_Entity_Name (Root1) loop
4947 if not Nkind_In
4948 (Root1, N_Selected_Component, N_Indexed_Component)
4949 then
4950 return False;
4951 else
4952 Root1 := Prefix (Root1);
4953 end if;
4955 Depth1 := Depth1 + 1;
4956 end loop;
4958 Root2 := Prefix (A2);
4959 while not Is_Entity_Name (Root2) loop
4960 if not Nkind_In (Root2, N_Selected_Component,
4961 N_Indexed_Component)
4962 then
4963 return False;
4964 else
4965 Root2 := Prefix (Root2);
4966 end if;
4968 Depth2 := Depth2 + 1;
4969 end loop;
4971 -- If both have the same depth and they do not denote the same
4972 -- object, they are disjoint and no warning is needed.
4974 if Depth1 = Depth2 then
4975 return False;
4977 elsif Depth1 > Depth2 then
4978 Root1 := Prefix (A1);
4979 for J in 1 .. Depth1 - Depth2 - 1 loop
4980 Root1 := Prefix (Root1);
4981 end loop;
4983 return Denotes_Same_Object (Root1, A2);
4985 else
4986 Root2 := Prefix (A2);
4987 for J in 1 .. Depth2 - Depth1 - 1 loop
4988 Root2 := Prefix (Root2);
4989 end loop;
4991 return Denotes_Same_Object (A1, Root2);
4992 end if;
4993 end;
4995 else
4996 return False;
4997 end if;
4998 end Denotes_Same_Prefix;
5000 ----------------------
5001 -- Denotes_Variable --
5002 ----------------------
5004 function Denotes_Variable (N : Node_Id) return Boolean is
5005 begin
5006 return Is_Variable (N) and then Paren_Count (N) = 0;
5007 end Denotes_Variable;
5009 -----------------------------
5010 -- Depends_On_Discriminant --
5011 -----------------------------
5013 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5014 L : Node_Id;
5015 H : Node_Id;
5017 begin
5018 Get_Index_Bounds (N, L, H);
5019 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5020 end Depends_On_Discriminant;
5022 -------------------------
5023 -- Designate_Same_Unit --
5024 -------------------------
5026 function Designate_Same_Unit
5027 (Name1 : Node_Id;
5028 Name2 : Node_Id) return Boolean
5030 K1 : constant Node_Kind := Nkind (Name1);
5031 K2 : constant Node_Kind := Nkind (Name2);
5033 function Prefix_Node (N : Node_Id) return Node_Id;
5034 -- Returns the parent unit name node of a defining program unit name
5035 -- or the prefix if N is a selected component or an expanded name.
5037 function Select_Node (N : Node_Id) return Node_Id;
5038 -- Returns the defining identifier node of a defining program unit
5039 -- name or the selector node if N is a selected component or an
5040 -- expanded name.
5042 -----------------
5043 -- Prefix_Node --
5044 -----------------
5046 function Prefix_Node (N : Node_Id) return Node_Id is
5047 begin
5048 if Nkind (N) = N_Defining_Program_Unit_Name then
5049 return Name (N);
5050 else
5051 return Prefix (N);
5052 end if;
5053 end Prefix_Node;
5055 -----------------
5056 -- Select_Node --
5057 -----------------
5059 function Select_Node (N : Node_Id) return Node_Id is
5060 begin
5061 if Nkind (N) = N_Defining_Program_Unit_Name then
5062 return Defining_Identifier (N);
5063 else
5064 return Selector_Name (N);
5065 end if;
5066 end Select_Node;
5068 -- Start of processing for Designate_Next_Unit
5070 begin
5071 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
5072 and then
5073 (K2 = N_Identifier or else K2 = N_Defining_Identifier)
5074 then
5075 return Chars (Name1) = Chars (Name2);
5077 elsif
5078 (K1 = N_Expanded_Name or else
5079 K1 = N_Selected_Component or else
5080 K1 = N_Defining_Program_Unit_Name)
5081 and then
5082 (K2 = N_Expanded_Name or else
5083 K2 = N_Selected_Component or else
5084 K2 = N_Defining_Program_Unit_Name)
5085 then
5086 return
5087 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5088 and then
5089 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5091 else
5092 return False;
5093 end if;
5094 end Designate_Same_Unit;
5096 ------------------------------------------
5097 -- function Dynamic_Accessibility_Level --
5098 ------------------------------------------
5100 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5101 E : Entity_Id;
5102 Loc : constant Source_Ptr := Sloc (Expr);
5104 function Make_Level_Literal (Level : Uint) return Node_Id;
5105 -- Construct an integer literal representing an accessibility level
5106 -- with its type set to Natural.
5108 ------------------------
5109 -- Make_Level_Literal --
5110 ------------------------
5112 function Make_Level_Literal (Level : Uint) return Node_Id is
5113 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5114 begin
5115 Set_Etype (Result, Standard_Natural);
5116 return Result;
5117 end Make_Level_Literal;
5119 -- Start of processing for Dynamic_Accessibility_Level
5121 begin
5122 if Is_Entity_Name (Expr) then
5123 E := Entity (Expr);
5125 if Present (Renamed_Object (E)) then
5126 return Dynamic_Accessibility_Level (Renamed_Object (E));
5127 end if;
5129 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5130 if Present (Extra_Accessibility (E)) then
5131 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5132 end if;
5133 end if;
5134 end if;
5136 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5138 case Nkind (Expr) is
5140 -- For access discriminant, the level of the enclosing object
5142 when N_Selected_Component =>
5143 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5144 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5145 E_Anonymous_Access_Type
5146 then
5147 return Make_Level_Literal (Object_Access_Level (Expr));
5148 end if;
5150 when N_Attribute_Reference =>
5151 case Get_Attribute_Id (Attribute_Name (Expr)) is
5153 -- For X'Access, the level of the prefix X
5155 when Attribute_Access =>
5156 return Make_Level_Literal
5157 (Object_Access_Level (Prefix (Expr)));
5159 -- Treat the unchecked attributes as library-level
5161 when Attribute_Unchecked_Access |
5162 Attribute_Unrestricted_Access =>
5163 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5165 -- No other access-valued attributes
5167 when others =>
5168 raise Program_Error;
5169 end case;
5171 when N_Allocator =>
5173 -- Unimplemented: depends on context. As an actual parameter where
5174 -- formal type is anonymous, use
5175 -- Scope_Depth (Current_Scope) + 1.
5176 -- For other cases, see 3.10.2(14/3) and following. ???
5178 null;
5180 when N_Type_Conversion =>
5181 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5183 -- Handle type conversions introduced for a rename of an
5184 -- Ada 2012 stand-alone object of an anonymous access type.
5186 return Dynamic_Accessibility_Level (Expression (Expr));
5187 end if;
5189 when others =>
5190 null;
5191 end case;
5193 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5194 end Dynamic_Accessibility_Level;
5196 -----------------------------------
5197 -- Effective_Extra_Accessibility --
5198 -----------------------------------
5200 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5201 begin
5202 if Present (Renamed_Object (Id))
5203 and then Is_Entity_Name (Renamed_Object (Id))
5204 then
5205 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5206 else
5207 return Extra_Accessibility (Id);
5208 end if;
5209 end Effective_Extra_Accessibility;
5211 -----------------------------
5212 -- Effective_Reads_Enabled --
5213 -----------------------------
5215 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5216 begin
5217 return Has_Enabled_Property (Id, Name_Effective_Reads);
5218 end Effective_Reads_Enabled;
5220 ------------------------------
5221 -- Effective_Writes_Enabled --
5222 ------------------------------
5224 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5225 begin
5226 return Has_Enabled_Property (Id, Name_Effective_Writes);
5227 end Effective_Writes_Enabled;
5229 ------------------------------
5230 -- Enclosing_Comp_Unit_Node --
5231 ------------------------------
5233 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5234 Current_Node : Node_Id;
5236 begin
5237 Current_Node := N;
5238 while Present (Current_Node)
5239 and then Nkind (Current_Node) /= N_Compilation_Unit
5240 loop
5241 Current_Node := Parent (Current_Node);
5242 end loop;
5244 if Nkind (Current_Node) /= N_Compilation_Unit then
5245 return Empty;
5246 else
5247 return Current_Node;
5248 end if;
5249 end Enclosing_Comp_Unit_Node;
5251 --------------------------
5252 -- Enclosing_CPP_Parent --
5253 --------------------------
5255 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5256 Parent_Typ : Entity_Id := Typ;
5258 begin
5259 while not Is_CPP_Class (Parent_Typ)
5260 and then Etype (Parent_Typ) /= Parent_Typ
5261 loop
5262 Parent_Typ := Etype (Parent_Typ);
5264 if Is_Private_Type (Parent_Typ) then
5265 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5266 end if;
5267 end loop;
5269 pragma Assert (Is_CPP_Class (Parent_Typ));
5270 return Parent_Typ;
5271 end Enclosing_CPP_Parent;
5273 ----------------------------
5274 -- Enclosing_Generic_Body --
5275 ----------------------------
5277 function Enclosing_Generic_Body
5278 (N : Node_Id) return Node_Id
5280 P : Node_Id;
5281 Decl : Node_Id;
5282 Spec : Node_Id;
5284 begin
5285 P := Parent (N);
5286 while Present (P) loop
5287 if Nkind (P) = N_Package_Body
5288 or else Nkind (P) = N_Subprogram_Body
5289 then
5290 Spec := Corresponding_Spec (P);
5292 if Present (Spec) then
5293 Decl := Unit_Declaration_Node (Spec);
5295 if Nkind (Decl) = N_Generic_Package_Declaration
5296 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5297 then
5298 return P;
5299 end if;
5300 end if;
5301 end if;
5303 P := Parent (P);
5304 end loop;
5306 return Empty;
5307 end Enclosing_Generic_Body;
5309 ----------------------------
5310 -- Enclosing_Generic_Unit --
5311 ----------------------------
5313 function Enclosing_Generic_Unit
5314 (N : Node_Id) return Node_Id
5316 P : Node_Id;
5317 Decl : Node_Id;
5318 Spec : Node_Id;
5320 begin
5321 P := Parent (N);
5322 while Present (P) loop
5323 if Nkind (P) = N_Generic_Package_Declaration
5324 or else Nkind (P) = N_Generic_Subprogram_Declaration
5325 then
5326 return P;
5328 elsif Nkind (P) = N_Package_Body
5329 or else Nkind (P) = N_Subprogram_Body
5330 then
5331 Spec := Corresponding_Spec (P);
5333 if Present (Spec) then
5334 Decl := Unit_Declaration_Node (Spec);
5336 if Nkind (Decl) = N_Generic_Package_Declaration
5337 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5338 then
5339 return Decl;
5340 end if;
5341 end if;
5342 end if;
5344 P := Parent (P);
5345 end loop;
5347 return Empty;
5348 end Enclosing_Generic_Unit;
5350 -------------------------------
5351 -- Enclosing_Lib_Unit_Entity --
5352 -------------------------------
5354 function Enclosing_Lib_Unit_Entity
5355 (E : Entity_Id := Current_Scope) return Entity_Id
5357 Unit_Entity : Entity_Id;
5359 begin
5360 -- Look for enclosing library unit entity by following scope links.
5361 -- Equivalent to, but faster than indexing through the scope stack.
5363 Unit_Entity := E;
5364 while (Present (Scope (Unit_Entity))
5365 and then Scope (Unit_Entity) /= Standard_Standard)
5366 and not Is_Child_Unit (Unit_Entity)
5367 loop
5368 Unit_Entity := Scope (Unit_Entity);
5369 end loop;
5371 return Unit_Entity;
5372 end Enclosing_Lib_Unit_Entity;
5374 -----------------------
5375 -- Enclosing_Package --
5376 -----------------------
5378 function Enclosing_Package (E : Entity_Id) return Entity_Id is
5379 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5381 begin
5382 if Dynamic_Scope = Standard_Standard then
5383 return Standard_Standard;
5385 elsif Dynamic_Scope = Empty then
5386 return Empty;
5388 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
5389 E_Generic_Package)
5390 then
5391 return Dynamic_Scope;
5393 else
5394 return Enclosing_Package (Dynamic_Scope);
5395 end if;
5396 end Enclosing_Package;
5398 --------------------------
5399 -- Enclosing_Subprogram --
5400 --------------------------
5402 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
5403 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5405 begin
5406 if Dynamic_Scope = Standard_Standard then
5407 return Empty;
5409 elsif Dynamic_Scope = Empty then
5410 return Empty;
5412 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
5413 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
5415 elsif Ekind (Dynamic_Scope) = E_Block
5416 or else Ekind (Dynamic_Scope) = E_Return_Statement
5417 then
5418 return Enclosing_Subprogram (Dynamic_Scope);
5420 elsif Ekind (Dynamic_Scope) = E_Task_Type then
5421 return Get_Task_Body_Procedure (Dynamic_Scope);
5423 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
5424 and then Present (Full_View (Dynamic_Scope))
5425 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
5426 then
5427 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
5429 -- No body is generated if the protected operation is eliminated
5431 elsif Convention (Dynamic_Scope) = Convention_Protected
5432 and then not Is_Eliminated (Dynamic_Scope)
5433 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
5434 then
5435 return Protected_Body_Subprogram (Dynamic_Scope);
5437 else
5438 return Dynamic_Scope;
5439 end if;
5440 end Enclosing_Subprogram;
5442 ------------------------
5443 -- Ensure_Freeze_Node --
5444 ------------------------
5446 procedure Ensure_Freeze_Node (E : Entity_Id) is
5447 FN : Node_Id;
5448 begin
5449 if No (Freeze_Node (E)) then
5450 FN := Make_Freeze_Entity (Sloc (E));
5451 Set_Has_Delayed_Freeze (E);
5452 Set_Freeze_Node (E, FN);
5453 Set_Access_Types_To_Process (FN, No_Elist);
5454 Set_TSS_Elist (FN, No_Elist);
5455 Set_Entity (FN, E);
5456 end if;
5457 end Ensure_Freeze_Node;
5459 ----------------
5460 -- Enter_Name --
5461 ----------------
5463 procedure Enter_Name (Def_Id : Entity_Id) is
5464 C : constant Entity_Id := Current_Entity (Def_Id);
5465 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
5466 S : constant Entity_Id := Current_Scope;
5468 begin
5469 Generate_Definition (Def_Id);
5471 -- Add new name to current scope declarations. Check for duplicate
5472 -- declaration, which may or may not be a genuine error.
5474 if Present (E) then
5476 -- Case of previous entity entered because of a missing declaration
5477 -- or else a bad subtype indication. Best is to use the new entity,
5478 -- and make the previous one invisible.
5480 if Etype (E) = Any_Type then
5481 Set_Is_Immediately_Visible (E, False);
5483 -- Case of renaming declaration constructed for package instances.
5484 -- if there is an explicit declaration with the same identifier,
5485 -- the renaming is not immediately visible any longer, but remains
5486 -- visible through selected component notation.
5488 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
5489 and then not Comes_From_Source (E)
5490 then
5491 Set_Is_Immediately_Visible (E, False);
5493 -- The new entity may be the package renaming, which has the same
5494 -- same name as a generic formal which has been seen already.
5496 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
5497 and then not Comes_From_Source (Def_Id)
5498 then
5499 Set_Is_Immediately_Visible (E, False);
5501 -- For a fat pointer corresponding to a remote access to subprogram,
5502 -- we use the same identifier as the RAS type, so that the proper
5503 -- name appears in the stub. This type is only retrieved through
5504 -- the RAS type and never by visibility, and is not added to the
5505 -- visibility list (see below).
5507 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
5508 and then Ekind (Def_Id) = E_Record_Type
5509 and then Present (Corresponding_Remote_Type (Def_Id))
5510 then
5511 null;
5513 -- Case of an implicit operation or derived literal. The new entity
5514 -- hides the implicit one, which is removed from all visibility,
5515 -- i.e. the entity list of its scope, and homonym chain of its name.
5517 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
5518 or else Is_Internal (E)
5519 then
5520 declare
5521 Prev : Entity_Id;
5522 Prev_Vis : Entity_Id;
5523 Decl : constant Node_Id := Parent (E);
5525 begin
5526 -- If E is an implicit declaration, it cannot be the first
5527 -- entity in the scope.
5529 Prev := First_Entity (Current_Scope);
5530 while Present (Prev) and then Next_Entity (Prev) /= E loop
5531 Next_Entity (Prev);
5532 end loop;
5534 if No (Prev) then
5536 -- If E is not on the entity chain of the current scope,
5537 -- it is an implicit declaration in the generic formal
5538 -- part of a generic subprogram. When analyzing the body,
5539 -- the generic formals are visible but not on the entity
5540 -- chain of the subprogram. The new entity will become
5541 -- the visible one in the body.
5543 pragma Assert
5544 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
5545 null;
5547 else
5548 Set_Next_Entity (Prev, Next_Entity (E));
5550 if No (Next_Entity (Prev)) then
5551 Set_Last_Entity (Current_Scope, Prev);
5552 end if;
5554 if E = Current_Entity (E) then
5555 Prev_Vis := Empty;
5557 else
5558 Prev_Vis := Current_Entity (E);
5559 while Homonym (Prev_Vis) /= E loop
5560 Prev_Vis := Homonym (Prev_Vis);
5561 end loop;
5562 end if;
5564 if Present (Prev_Vis) then
5566 -- Skip E in the visibility chain
5568 Set_Homonym (Prev_Vis, Homonym (E));
5570 else
5571 Set_Name_Entity_Id (Chars (E), Homonym (E));
5572 end if;
5573 end if;
5574 end;
5576 -- This section of code could use a comment ???
5578 elsif Present (Etype (E))
5579 and then Is_Concurrent_Type (Etype (E))
5580 and then E = Def_Id
5581 then
5582 return;
5584 -- If the homograph is a protected component renaming, it should not
5585 -- be hiding the current entity. Such renamings are treated as weak
5586 -- declarations.
5588 elsif Is_Prival (E) then
5589 Set_Is_Immediately_Visible (E, False);
5591 -- In this case the current entity is a protected component renaming.
5592 -- Perform minimal decoration by setting the scope and return since
5593 -- the prival should not be hiding other visible entities.
5595 elsif Is_Prival (Def_Id) then
5596 Set_Scope (Def_Id, Current_Scope);
5597 return;
5599 -- Analogous to privals, the discriminal generated for an entry index
5600 -- parameter acts as a weak declaration. Perform minimal decoration
5601 -- to avoid bogus errors.
5603 elsif Is_Discriminal (Def_Id)
5604 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
5605 then
5606 Set_Scope (Def_Id, Current_Scope);
5607 return;
5609 -- In the body or private part of an instance, a type extension may
5610 -- introduce a component with the same name as that of an actual. The
5611 -- legality rule is not enforced, but the semantics of the full type
5612 -- with two components of same name are not clear at this point???
5614 elsif In_Instance_Not_Visible then
5615 null;
5617 -- When compiling a package body, some child units may have become
5618 -- visible. They cannot conflict with local entities that hide them.
5620 elsif Is_Child_Unit (E)
5621 and then In_Open_Scopes (Scope (E))
5622 and then not Is_Immediately_Visible (E)
5623 then
5624 null;
5626 -- Conversely, with front-end inlining we may compile the parent body
5627 -- first, and a child unit subsequently. The context is now the
5628 -- parent spec, and body entities are not visible.
5630 elsif Is_Child_Unit (Def_Id)
5631 and then Is_Package_Body_Entity (E)
5632 and then not In_Package_Body (Current_Scope)
5633 then
5634 null;
5636 -- Case of genuine duplicate declaration
5638 else
5639 Error_Msg_Sloc := Sloc (E);
5641 -- If the previous declaration is an incomplete type declaration
5642 -- this may be an attempt to complete it with a private type. The
5643 -- following avoids confusing cascaded errors.
5645 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
5646 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
5647 then
5648 Error_Msg_N
5649 ("incomplete type cannot be completed with a private " &
5650 "declaration", Parent (Def_Id));
5651 Set_Is_Immediately_Visible (E, False);
5652 Set_Full_View (E, Def_Id);
5654 -- An inherited component of a record conflicts with a new
5655 -- discriminant. The discriminant is inserted first in the scope,
5656 -- but the error should be posted on it, not on the component.
5658 elsif Ekind (E) = E_Discriminant
5659 and then Present (Scope (Def_Id))
5660 and then Scope (Def_Id) /= Current_Scope
5661 then
5662 Error_Msg_Sloc := Sloc (Def_Id);
5663 Error_Msg_N ("& conflicts with declaration#", E);
5664 return;
5666 -- If the name of the unit appears in its own context clause, a
5667 -- dummy package with the name has already been created, and the
5668 -- error emitted. Try to continue quietly.
5670 elsif Error_Posted (E)
5671 and then Sloc (E) = No_Location
5672 and then Nkind (Parent (E)) = N_Package_Specification
5673 and then Current_Scope = Standard_Standard
5674 then
5675 Set_Scope (Def_Id, Current_Scope);
5676 return;
5678 else
5679 Error_Msg_N ("& conflicts with declaration#", Def_Id);
5681 -- Avoid cascaded messages with duplicate components in
5682 -- derived types.
5684 if Ekind_In (E, E_Component, E_Discriminant) then
5685 return;
5686 end if;
5687 end if;
5689 if Nkind (Parent (Parent (Def_Id))) =
5690 N_Generic_Subprogram_Declaration
5691 and then Def_Id =
5692 Defining_Entity (Specification (Parent (Parent (Def_Id))))
5693 then
5694 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
5695 end if;
5697 -- If entity is in standard, then we are in trouble, because it
5698 -- means that we have a library package with a duplicated name.
5699 -- That's hard to recover from, so abort.
5701 if S = Standard_Standard then
5702 raise Unrecoverable_Error;
5704 -- Otherwise we continue with the declaration. Having two
5705 -- identical declarations should not cause us too much trouble.
5707 else
5708 null;
5709 end if;
5710 end if;
5711 end if;
5713 -- If we fall through, declaration is OK, at least OK enough to continue
5715 -- If Def_Id is a discriminant or a record component we are in the midst
5716 -- of inheriting components in a derived record definition. Preserve
5717 -- their Ekind and Etype.
5719 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
5720 null;
5722 -- If a type is already set, leave it alone (happens when a type
5723 -- declaration is reanalyzed following a call to the optimizer).
5725 elsif Present (Etype (Def_Id)) then
5726 null;
5728 -- Otherwise, the kind E_Void insures that premature uses of the entity
5729 -- will be detected. Any_Type insures that no cascaded errors will occur
5731 else
5732 Set_Ekind (Def_Id, E_Void);
5733 Set_Etype (Def_Id, Any_Type);
5734 end if;
5736 -- Inherited discriminants and components in derived record types are
5737 -- immediately visible. Itypes are not.
5739 -- Unless the Itype is for a record type with a corresponding remote
5740 -- type (what is that about, it was not commented ???)
5742 if Ekind_In (Def_Id, E_Discriminant, E_Component)
5743 or else
5744 ((not Is_Record_Type (Def_Id)
5745 or else No (Corresponding_Remote_Type (Def_Id)))
5746 and then not Is_Itype (Def_Id))
5747 then
5748 Set_Is_Immediately_Visible (Def_Id);
5749 Set_Current_Entity (Def_Id);
5750 end if;
5752 Set_Homonym (Def_Id, C);
5753 Append_Entity (Def_Id, S);
5754 Set_Public_Status (Def_Id);
5756 -- Declaring a homonym is not allowed in SPARK ...
5758 if Present (C) and then Restriction_Check_Required (SPARK_05) then
5759 declare
5760 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
5761 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
5762 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
5764 begin
5765 -- ... unless the new declaration is in a subprogram, and the
5766 -- visible declaration is a variable declaration or a parameter
5767 -- specification outside that subprogram.
5769 if Present (Enclosing_Subp)
5770 and then Nkind_In (Parent (C), N_Object_Declaration,
5771 N_Parameter_Specification)
5772 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
5773 then
5774 null;
5776 -- ... or the new declaration is in a package, and the visible
5777 -- declaration occurs outside that package.
5779 elsif Present (Enclosing_Pack)
5780 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
5781 then
5782 null;
5784 -- ... or the new declaration is a component declaration in a
5785 -- record type definition.
5787 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
5788 null;
5790 -- Don't issue error for non-source entities
5792 elsif Comes_From_Source (Def_Id)
5793 and then Comes_From_Source (C)
5794 then
5795 Error_Msg_Sloc := Sloc (C);
5796 Check_SPARK_05_Restriction
5797 ("redeclaration of identifier &#", Def_Id);
5798 end if;
5799 end;
5800 end if;
5802 -- Warn if new entity hides an old one
5804 if Warn_On_Hiding and then Present (C)
5806 -- Don't warn for record components since they always have a well
5807 -- defined scope which does not confuse other uses. Note that in
5808 -- some cases, Ekind has not been set yet.
5810 and then Ekind (C) /= E_Component
5811 and then Ekind (C) /= E_Discriminant
5812 and then Nkind (Parent (C)) /= N_Component_Declaration
5813 and then Ekind (Def_Id) /= E_Component
5814 and then Ekind (Def_Id) /= E_Discriminant
5815 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
5817 -- Don't warn for one character variables. It is too common to use
5818 -- such variables as locals and will just cause too many false hits.
5820 and then Length_Of_Name (Chars (C)) /= 1
5822 -- Don't warn for non-source entities
5824 and then Comes_From_Source (C)
5825 and then Comes_From_Source (Def_Id)
5827 -- Don't warn unless entity in question is in extended main source
5829 and then In_Extended_Main_Source_Unit (Def_Id)
5831 -- Finally, the hidden entity must be either immediately visible or
5832 -- use visible (i.e. from a used package).
5834 and then
5835 (Is_Immediately_Visible (C)
5836 or else
5837 Is_Potentially_Use_Visible (C))
5838 then
5839 Error_Msg_Sloc := Sloc (C);
5840 Error_Msg_N ("declaration hides &#?h?", Def_Id);
5841 end if;
5842 end Enter_Name;
5844 ---------------
5845 -- Entity_Of --
5846 ---------------
5848 function Entity_Of (N : Node_Id) return Entity_Id is
5849 Id : Entity_Id;
5851 begin
5852 Id := Empty;
5854 if Is_Entity_Name (N) then
5855 Id := Entity (N);
5857 -- Follow a possible chain of renamings to reach the root renamed
5858 -- object.
5860 while Present (Id) and then Present (Renamed_Object (Id)) loop
5861 if Is_Entity_Name (Renamed_Object (Id)) then
5862 Id := Entity (Renamed_Object (Id));
5863 else
5864 Id := Empty;
5865 exit;
5866 end if;
5867 end loop;
5868 end if;
5870 return Id;
5871 end Entity_Of;
5873 --------------------------
5874 -- Explain_Limited_Type --
5875 --------------------------
5877 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
5878 C : Entity_Id;
5880 begin
5881 -- For array, component type must be limited
5883 if Is_Array_Type (T) then
5884 Error_Msg_Node_2 := T;
5885 Error_Msg_NE
5886 ("\component type& of type& is limited", N, Component_Type (T));
5887 Explain_Limited_Type (Component_Type (T), N);
5889 elsif Is_Record_Type (T) then
5891 -- No need for extra messages if explicit limited record
5893 if Is_Limited_Record (Base_Type (T)) then
5894 return;
5895 end if;
5897 -- Otherwise find a limited component. Check only components that
5898 -- come from source, or inherited components that appear in the
5899 -- source of the ancestor.
5901 C := First_Component (T);
5902 while Present (C) loop
5903 if Is_Limited_Type (Etype (C))
5904 and then
5905 (Comes_From_Source (C)
5906 or else
5907 (Present (Original_Record_Component (C))
5908 and then
5909 Comes_From_Source (Original_Record_Component (C))))
5910 then
5911 Error_Msg_Node_2 := T;
5912 Error_Msg_NE ("\component& of type& has limited type", N, C);
5913 Explain_Limited_Type (Etype (C), N);
5914 return;
5915 end if;
5917 Next_Component (C);
5918 end loop;
5920 -- The type may be declared explicitly limited, even if no component
5921 -- of it is limited, in which case we fall out of the loop.
5922 return;
5923 end if;
5924 end Explain_Limited_Type;
5926 -------------------------------
5927 -- Extensions_Visible_Status --
5928 -------------------------------
5930 function Extensions_Visible_Status
5931 (Id : Entity_Id) return Extensions_Visible_Mode
5933 Arg : Node_Id;
5934 Decl : Node_Id;
5935 Expr : Node_Id;
5936 Prag : Node_Id;
5937 Subp : Entity_Id;
5939 begin
5940 -- When a formal parameter is subject to Extensions_Visible, the pragma
5941 -- is stored in the contract of related subprogram.
5943 if Is_Formal (Id) then
5944 Subp := Scope (Id);
5946 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
5947 Subp := Id;
5949 -- No other construct carries this pragma
5951 else
5952 return Extensions_Visible_None;
5953 end if;
5955 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
5957 -- In certain cases analysis may request the Extensions_Visible status
5958 -- of an expression function before the pragma has been analyzed yet.
5959 -- Inspect the declarative items after the expression function looking
5960 -- for the pragma (if any).
5962 if No (Prag) and then Is_Expression_Function (Subp) then
5963 Decl := Next (Unit_Declaration_Node (Subp));
5964 while Present (Decl) loop
5965 if Nkind (Decl) = N_Pragma
5966 and then Pragma_Name (Decl) = Name_Extensions_Visible
5967 then
5968 Prag := Decl;
5969 exit;
5971 -- A source construct ends the region where Extensions_Visible may
5972 -- appear, stop the traversal. An expanded expression function is
5973 -- no longer a source construct, but it must still be recognized.
5975 elsif Comes_From_Source (Decl)
5976 or else
5977 (Nkind_In (Decl, N_Subprogram_Body,
5978 N_Subprogram_Declaration)
5979 and then Is_Expression_Function (Defining_Entity (Decl)))
5980 then
5981 exit;
5982 end if;
5984 Next (Decl);
5985 end loop;
5986 end if;
5988 -- Extract the value from the Boolean expression (if any)
5990 if Present (Prag) then
5991 Arg := First (Pragma_Argument_Associations (Prag));
5993 if Present (Arg) then
5994 Expr := Get_Pragma_Arg (Arg);
5996 -- When the associated subprogram is an expression function, the
5997 -- argument of the pragma may not have been analyzed.
5999 if not Analyzed (Expr) then
6000 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6001 end if;
6003 -- Guard against cascading errors when the argument of pragma
6004 -- Extensions_Visible is not a valid static Boolean expression.
6006 if Error_Posted (Expr) then
6007 return Extensions_Visible_None;
6009 elsif Is_True (Expr_Value (Expr)) then
6010 return Extensions_Visible_True;
6012 else
6013 return Extensions_Visible_False;
6014 end if;
6016 -- Otherwise the aspect or pragma defaults to True
6018 else
6019 return Extensions_Visible_True;
6020 end if;
6022 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
6023 -- directly specified. In SPARK code, its value defaults to "False".
6025 elsif SPARK_Mode = On then
6026 return Extensions_Visible_False;
6028 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
6029 -- "True".
6031 else
6032 return Extensions_Visible_True;
6033 end if;
6034 end Extensions_Visible_Status;
6036 -----------------
6037 -- Find_Actual --
6038 -----------------
6040 procedure Find_Actual
6041 (N : Node_Id;
6042 Formal : out Entity_Id;
6043 Call : out Node_Id)
6045 Parnt : constant Node_Id := Parent (N);
6046 Actual : Node_Id;
6048 begin
6049 if Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6050 and then N = Prefix (Parnt)
6051 then
6052 Find_Actual (Parnt, Formal, Call);
6053 return;
6055 elsif Nkind (Parnt) = N_Parameter_Association
6056 and then N = Explicit_Actual_Parameter (Parnt)
6057 then
6058 Call := Parent (Parnt);
6060 elsif Nkind (Parnt) in N_Subprogram_Call then
6061 Call := Parnt;
6063 else
6064 Formal := Empty;
6065 Call := Empty;
6066 return;
6067 end if;
6069 -- If we have a call to a subprogram look for the parameter. Note that
6070 -- we exclude overloaded calls, since we don't know enough to be sure
6071 -- of giving the right answer in this case.
6073 if Nkind_In (Call, N_Function_Call, N_Procedure_Call_Statement)
6074 and then Is_Entity_Name (Name (Call))
6075 and then Present (Entity (Name (Call)))
6076 and then Is_Overloadable (Entity (Name (Call)))
6077 and then not Is_Overloaded (Name (Call))
6078 then
6079 -- Fall here if we are definitely a parameter
6081 Actual := First_Actual (Call);
6082 Formal := First_Formal (Entity (Name (Call)));
6083 while Present (Formal) and then Present (Actual) loop
6084 if Actual = N then
6085 return;
6087 -- An actual that is the prefix in a prefixed call may have
6088 -- been rewritten in the call, after the deferred reference
6089 -- was collected. Check if sloc and kinds and names match.
6091 elsif Sloc (Actual) = Sloc (N)
6092 and then Nkind (Actual) = N_Identifier
6093 and then Nkind (Actual) = Nkind (N)
6094 and then Chars (Actual) = Chars (N)
6095 then
6096 return;
6098 else
6099 Actual := Next_Actual (Actual);
6100 Formal := Next_Formal (Formal);
6101 end if;
6102 end loop;
6103 end if;
6105 -- Fall through here if we did not find matching actual
6107 Formal := Empty;
6108 Call := Empty;
6109 end Find_Actual;
6111 ---------------------------
6112 -- Find_Body_Discriminal --
6113 ---------------------------
6115 function Find_Body_Discriminal
6116 (Spec_Discriminant : Entity_Id) return Entity_Id
6118 Tsk : Entity_Id;
6119 Disc : Entity_Id;
6121 begin
6122 -- If expansion is suppressed, then the scope can be the concurrent type
6123 -- itself rather than a corresponding concurrent record type.
6125 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6126 Tsk := Scope (Spec_Discriminant);
6128 else
6129 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6131 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6132 end if;
6134 -- Find discriminant of original concurrent type, and use its current
6135 -- discriminal, which is the renaming within the task/protected body.
6137 Disc := First_Discriminant (Tsk);
6138 while Present (Disc) loop
6139 if Chars (Disc) = Chars (Spec_Discriminant) then
6140 return Discriminal (Disc);
6141 end if;
6143 Next_Discriminant (Disc);
6144 end loop;
6146 -- That loop should always succeed in finding a matching entry and
6147 -- returning. Fatal error if not.
6149 raise Program_Error;
6150 end Find_Body_Discriminal;
6152 -------------------------------------
6153 -- Find_Corresponding_Discriminant --
6154 -------------------------------------
6156 function Find_Corresponding_Discriminant
6157 (Id : Node_Id;
6158 Typ : Entity_Id) return Entity_Id
6160 Par_Disc : Entity_Id;
6161 Old_Disc : Entity_Id;
6162 New_Disc : Entity_Id;
6164 begin
6165 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6167 -- The original type may currently be private, and the discriminant
6168 -- only appear on its full view.
6170 if Is_Private_Type (Scope (Par_Disc))
6171 and then not Has_Discriminants (Scope (Par_Disc))
6172 and then Present (Full_View (Scope (Par_Disc)))
6173 then
6174 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6175 else
6176 Old_Disc := First_Discriminant (Scope (Par_Disc));
6177 end if;
6179 if Is_Class_Wide_Type (Typ) then
6180 New_Disc := First_Discriminant (Root_Type (Typ));
6181 else
6182 New_Disc := First_Discriminant (Typ);
6183 end if;
6185 while Present (Old_Disc) and then Present (New_Disc) loop
6186 if Old_Disc = Par_Disc then
6187 return New_Disc;
6188 end if;
6190 Next_Discriminant (Old_Disc);
6191 Next_Discriminant (New_Disc);
6192 end loop;
6194 -- Should always find it
6196 raise Program_Error;
6197 end Find_Corresponding_Discriminant;
6199 ----------------------------------
6200 -- Find_Enclosing_Iterator_Loop --
6201 ----------------------------------
6203 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
6204 Constr : Node_Id;
6205 S : Entity_Id;
6207 begin
6208 -- Traverse the scope chain looking for an iterator loop. Such loops are
6209 -- usually transformed into blocks, hence the use of Original_Node.
6211 S := Id;
6212 while Present (S) and then S /= Standard_Standard loop
6213 if Ekind (S) = E_Loop
6214 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
6215 then
6216 Constr := Original_Node (Label_Construct (Parent (S)));
6218 if Nkind (Constr) = N_Loop_Statement
6219 and then Present (Iteration_Scheme (Constr))
6220 and then Nkind (Iterator_Specification
6221 (Iteration_Scheme (Constr))) =
6222 N_Iterator_Specification
6223 then
6224 return S;
6225 end if;
6226 end if;
6228 S := Scope (S);
6229 end loop;
6231 return Empty;
6232 end Find_Enclosing_Iterator_Loop;
6234 ------------------------------------
6235 -- Find_Loop_In_Conditional_Block --
6236 ------------------------------------
6238 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
6239 Stmt : Node_Id;
6241 begin
6242 Stmt := N;
6244 if Nkind (Stmt) = N_If_Statement then
6245 Stmt := First (Then_Statements (Stmt));
6246 end if;
6248 pragma Assert (Nkind (Stmt) = N_Block_Statement);
6250 -- Inspect the statements of the conditional block. In general the loop
6251 -- should be the first statement in the statement sequence of the block,
6252 -- but the finalization machinery may have introduced extra object
6253 -- declarations.
6255 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
6256 while Present (Stmt) loop
6257 if Nkind (Stmt) = N_Loop_Statement then
6258 return Stmt;
6259 end if;
6261 Next (Stmt);
6262 end loop;
6264 -- The expansion of attribute 'Loop_Entry produced a malformed block
6266 raise Program_Error;
6267 end Find_Loop_In_Conditional_Block;
6269 --------------------------
6270 -- Find_Overlaid_Entity --
6271 --------------------------
6273 procedure Find_Overlaid_Entity
6274 (N : Node_Id;
6275 Ent : out Entity_Id;
6276 Off : out Boolean)
6278 Expr : Node_Id;
6280 begin
6281 -- We are looking for one of the two following forms:
6283 -- for X'Address use Y'Address
6285 -- or
6287 -- Const : constant Address := expr;
6288 -- ...
6289 -- for X'Address use Const;
6291 -- In the second case, the expr is either Y'Address, or recursively a
6292 -- constant that eventually references Y'Address.
6294 Ent := Empty;
6295 Off := False;
6297 if Nkind (N) = N_Attribute_Definition_Clause
6298 and then Chars (N) = Name_Address
6299 then
6300 Expr := Expression (N);
6302 -- This loop checks the form of the expression for Y'Address,
6303 -- using recursion to deal with intermediate constants.
6305 loop
6306 -- Check for Y'Address
6308 if Nkind (Expr) = N_Attribute_Reference
6309 and then Attribute_Name (Expr) = Name_Address
6310 then
6311 Expr := Prefix (Expr);
6312 exit;
6314 -- Check for Const where Const is a constant entity
6316 elsif Is_Entity_Name (Expr)
6317 and then Ekind (Entity (Expr)) = E_Constant
6318 then
6319 Expr := Constant_Value (Entity (Expr));
6321 -- Anything else does not need checking
6323 else
6324 return;
6325 end if;
6326 end loop;
6328 -- This loop checks the form of the prefix for an entity, using
6329 -- recursion to deal with intermediate components.
6331 loop
6332 -- Check for Y where Y is an entity
6334 if Is_Entity_Name (Expr) then
6335 Ent := Entity (Expr);
6336 return;
6338 -- Check for components
6340 elsif
6341 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
6342 then
6343 Expr := Prefix (Expr);
6344 Off := True;
6346 -- Anything else does not need checking
6348 else
6349 return;
6350 end if;
6351 end loop;
6352 end if;
6353 end Find_Overlaid_Entity;
6355 -------------------------
6356 -- Find_Parameter_Type --
6357 -------------------------
6359 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
6360 begin
6361 if Nkind (Param) /= N_Parameter_Specification then
6362 return Empty;
6364 -- For an access parameter, obtain the type from the formal entity
6365 -- itself, because access to subprogram nodes do not carry a type.
6366 -- Shouldn't we always use the formal entity ???
6368 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
6369 return Etype (Defining_Identifier (Param));
6371 else
6372 return Etype (Parameter_Type (Param));
6373 end if;
6374 end Find_Parameter_Type;
6376 -----------------------------------
6377 -- Find_Placement_In_State_Space --
6378 -----------------------------------
6380 procedure Find_Placement_In_State_Space
6381 (Item_Id : Entity_Id;
6382 Placement : out State_Space_Kind;
6383 Pack_Id : out Entity_Id)
6385 Context : Entity_Id;
6387 begin
6388 -- Assume that the item does not appear in the state space of a package
6390 Placement := Not_In_Package;
6391 Pack_Id := Empty;
6393 -- Climb the scope stack and examine the enclosing context
6395 Context := Scope (Item_Id);
6396 while Present (Context) and then Context /= Standard_Standard loop
6397 if Ekind (Context) = E_Package then
6398 Pack_Id := Context;
6400 -- A package body is a cut off point for the traversal as the item
6401 -- cannot be visible to the outside from this point on. Note that
6402 -- this test must be done first as a body is also classified as a
6403 -- private part.
6405 if In_Package_Body (Context) then
6406 Placement := Body_State_Space;
6407 return;
6409 -- The private part of a package is a cut off point for the
6410 -- traversal as the item cannot be visible to the outside from
6411 -- this point on.
6413 elsif In_Private_Part (Context) then
6414 Placement := Private_State_Space;
6415 return;
6417 -- When the item appears in the visible state space of a package,
6418 -- continue to climb the scope stack as this may not be the final
6419 -- state space.
6421 else
6422 Placement := Visible_State_Space;
6424 -- The visible state space of a child unit acts as the proper
6425 -- placement of an item.
6427 if Is_Child_Unit (Context) then
6428 return;
6429 end if;
6430 end if;
6432 -- The item or its enclosing package appear in a construct that has
6433 -- no state space.
6435 else
6436 Placement := Not_In_Package;
6437 return;
6438 end if;
6440 Context := Scope (Context);
6441 end loop;
6442 end Find_Placement_In_State_Space;
6444 ------------------------
6445 -- Find_Specific_Type --
6446 ------------------------
6448 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
6449 Typ : Entity_Id := Root_Type (CW);
6451 begin
6452 if Ekind (Typ) = E_Incomplete_Type then
6453 if From_Limited_With (Typ) then
6454 Typ := Non_Limited_View (Typ);
6455 else
6456 Typ := Full_View (Typ);
6457 end if;
6458 end if;
6460 if Is_Private_Type (Typ)
6461 and then not Is_Tagged_Type (Typ)
6462 and then Present (Full_View (Typ))
6463 then
6464 return Full_View (Typ);
6465 else
6466 return Typ;
6467 end if;
6468 end Find_Specific_Type;
6470 -----------------------------
6471 -- Find_Static_Alternative --
6472 -----------------------------
6474 function Find_Static_Alternative (N : Node_Id) return Node_Id is
6475 Expr : constant Node_Id := Expression (N);
6476 Val : constant Uint := Expr_Value (Expr);
6477 Alt : Node_Id;
6478 Choice : Node_Id;
6480 begin
6481 Alt := First (Alternatives (N));
6483 Search : loop
6484 if Nkind (Alt) /= N_Pragma then
6485 Choice := First (Discrete_Choices (Alt));
6486 while Present (Choice) loop
6488 -- Others choice, always matches
6490 if Nkind (Choice) = N_Others_Choice then
6491 exit Search;
6493 -- Range, check if value is in the range
6495 elsif Nkind (Choice) = N_Range then
6496 exit Search when
6497 Val >= Expr_Value (Low_Bound (Choice))
6498 and then
6499 Val <= Expr_Value (High_Bound (Choice));
6501 -- Choice is a subtype name. Note that we know it must
6502 -- be a static subtype, since otherwise it would have
6503 -- been diagnosed as illegal.
6505 elsif Is_Entity_Name (Choice)
6506 and then Is_Type (Entity (Choice))
6507 then
6508 exit Search when Is_In_Range (Expr, Etype (Choice),
6509 Assume_Valid => False);
6511 -- Choice is a subtype indication
6513 elsif Nkind (Choice) = N_Subtype_Indication then
6514 declare
6515 C : constant Node_Id := Constraint (Choice);
6516 R : constant Node_Id := Range_Expression (C);
6518 begin
6519 exit Search when
6520 Val >= Expr_Value (Low_Bound (R))
6521 and then
6522 Val <= Expr_Value (High_Bound (R));
6523 end;
6525 -- Choice is a simple expression
6527 else
6528 exit Search when Val = Expr_Value (Choice);
6529 end if;
6531 Next (Choice);
6532 end loop;
6533 end if;
6535 Next (Alt);
6536 pragma Assert (Present (Alt));
6537 end loop Search;
6539 -- The above loop *must* terminate by finding a match, since
6540 -- we know the case statement is valid, and the value of the
6541 -- expression is known at compile time. When we fall out of
6542 -- the loop, Alt points to the alternative that we know will
6543 -- be selected at run time.
6545 return Alt;
6546 end Find_Static_Alternative;
6548 ------------------
6549 -- First_Actual --
6550 ------------------
6552 function First_Actual (Node : Node_Id) return Node_Id is
6553 N : Node_Id;
6555 begin
6556 if No (Parameter_Associations (Node)) then
6557 return Empty;
6558 end if;
6560 N := First (Parameter_Associations (Node));
6562 if Nkind (N) = N_Parameter_Association then
6563 return First_Named_Actual (Node);
6564 else
6565 return N;
6566 end if;
6567 end First_Actual;
6569 -----------------------
6570 -- Gather_Components --
6571 -----------------------
6573 procedure Gather_Components
6574 (Typ : Entity_Id;
6575 Comp_List : Node_Id;
6576 Governed_By : List_Id;
6577 Into : Elist_Id;
6578 Report_Errors : out Boolean)
6580 Assoc : Node_Id;
6581 Variant : Node_Id;
6582 Discrete_Choice : Node_Id;
6583 Comp_Item : Node_Id;
6585 Discrim : Entity_Id;
6586 Discrim_Name : Node_Id;
6587 Discrim_Value : Node_Id;
6589 begin
6590 Report_Errors := False;
6592 if No (Comp_List) or else Null_Present (Comp_List) then
6593 return;
6595 elsif Present (Component_Items (Comp_List)) then
6596 Comp_Item := First (Component_Items (Comp_List));
6598 else
6599 Comp_Item := Empty;
6600 end if;
6602 while Present (Comp_Item) loop
6604 -- Skip the tag of a tagged record, the interface tags, as well
6605 -- as all items that are not user components (anonymous types,
6606 -- rep clauses, Parent field, controller field).
6608 if Nkind (Comp_Item) = N_Component_Declaration then
6609 declare
6610 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
6611 begin
6612 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
6613 Append_Elmt (Comp, Into);
6614 end if;
6615 end;
6616 end if;
6618 Next (Comp_Item);
6619 end loop;
6621 if No (Variant_Part (Comp_List)) then
6622 return;
6623 else
6624 Discrim_Name := Name (Variant_Part (Comp_List));
6625 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
6626 end if;
6628 -- Look for the discriminant that governs this variant part.
6629 -- The discriminant *must* be in the Governed_By List
6631 Assoc := First (Governed_By);
6632 Find_Constraint : loop
6633 Discrim := First (Choices (Assoc));
6634 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
6635 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
6636 and then
6637 Chars (Corresponding_Discriminant (Entity (Discrim))) =
6638 Chars (Discrim_Name))
6639 or else Chars (Original_Record_Component (Entity (Discrim)))
6640 = Chars (Discrim_Name);
6642 if No (Next (Assoc)) then
6643 if not Is_Constrained (Typ)
6644 and then Is_Derived_Type (Typ)
6645 and then Present (Stored_Constraint (Typ))
6646 then
6647 -- If the type is a tagged type with inherited discriminants,
6648 -- use the stored constraint on the parent in order to find
6649 -- the values of discriminants that are otherwise hidden by an
6650 -- explicit constraint. Renamed discriminants are handled in
6651 -- the code above.
6653 -- If several parent discriminants are renamed by a single
6654 -- discriminant of the derived type, the call to obtain the
6655 -- Corresponding_Discriminant field only retrieves the last
6656 -- of them. We recover the constraint on the others from the
6657 -- Stored_Constraint as well.
6659 declare
6660 D : Entity_Id;
6661 C : Elmt_Id;
6663 begin
6664 D := First_Discriminant (Etype (Typ));
6665 C := First_Elmt (Stored_Constraint (Typ));
6666 while Present (D) and then Present (C) loop
6667 if Chars (Discrim_Name) = Chars (D) then
6668 if Is_Entity_Name (Node (C))
6669 and then Entity (Node (C)) = Entity (Discrim)
6670 then
6671 -- D is renamed by Discrim, whose value is given in
6672 -- Assoc.
6674 null;
6676 else
6677 Assoc :=
6678 Make_Component_Association (Sloc (Typ),
6679 New_List
6680 (New_Occurrence_Of (D, Sloc (Typ))),
6681 Duplicate_Subexpr_No_Checks (Node (C)));
6682 end if;
6683 exit Find_Constraint;
6684 end if;
6686 Next_Discriminant (D);
6687 Next_Elmt (C);
6688 end loop;
6689 end;
6690 end if;
6691 end if;
6693 if No (Next (Assoc)) then
6694 Error_Msg_NE (" missing value for discriminant&",
6695 First (Governed_By), Discrim_Name);
6696 Report_Errors := True;
6697 return;
6698 end if;
6700 Next (Assoc);
6701 end loop Find_Constraint;
6703 Discrim_Value := Expression (Assoc);
6705 if not Is_OK_Static_Expression (Discrim_Value) then
6706 Error_Msg_FE
6707 ("value for discriminant & must be static!",
6708 Discrim_Value, Discrim);
6709 Why_Not_Static (Discrim_Value);
6710 Report_Errors := True;
6711 return;
6712 end if;
6714 Search_For_Discriminant_Value : declare
6715 Low : Node_Id;
6716 High : Node_Id;
6718 UI_High : Uint;
6719 UI_Low : Uint;
6720 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
6722 begin
6723 Find_Discrete_Value : while Present (Variant) loop
6724 Discrete_Choice := First (Discrete_Choices (Variant));
6725 while Present (Discrete_Choice) loop
6726 exit Find_Discrete_Value when
6727 Nkind (Discrete_Choice) = N_Others_Choice;
6729 Get_Index_Bounds (Discrete_Choice, Low, High);
6731 UI_Low := Expr_Value (Low);
6732 UI_High := Expr_Value (High);
6734 exit Find_Discrete_Value when
6735 UI_Low <= UI_Discrim_Value
6736 and then
6737 UI_High >= UI_Discrim_Value;
6739 Next (Discrete_Choice);
6740 end loop;
6742 Next_Non_Pragma (Variant);
6743 end loop Find_Discrete_Value;
6744 end Search_For_Discriminant_Value;
6746 if No (Variant) then
6747 Error_Msg_NE
6748 ("value of discriminant & is out of range", Discrim_Value, Discrim);
6749 Report_Errors := True;
6750 return;
6751 end if;
6753 -- If we have found the corresponding choice, recursively add its
6754 -- components to the Into list.
6756 Gather_Components
6757 (Empty, Component_List (Variant), Governed_By, Into, Report_Errors);
6758 end Gather_Components;
6760 ------------------------
6761 -- Get_Actual_Subtype --
6762 ------------------------
6764 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
6765 Typ : constant Entity_Id := Etype (N);
6766 Utyp : Entity_Id := Underlying_Type (Typ);
6767 Decl : Node_Id;
6768 Atyp : Entity_Id;
6770 begin
6771 if No (Utyp) then
6772 Utyp := Typ;
6773 end if;
6775 -- If what we have is an identifier that references a subprogram
6776 -- formal, or a variable or constant object, then we get the actual
6777 -- subtype from the referenced entity if one has been built.
6779 if Nkind (N) = N_Identifier
6780 and then
6781 (Is_Formal (Entity (N))
6782 or else Ekind (Entity (N)) = E_Constant
6783 or else Ekind (Entity (N)) = E_Variable)
6784 and then Present (Actual_Subtype (Entity (N)))
6785 then
6786 return Actual_Subtype (Entity (N));
6788 -- Actual subtype of unchecked union is always itself. We never need
6789 -- the "real" actual subtype. If we did, we couldn't get it anyway
6790 -- because the discriminant is not available. The restrictions on
6791 -- Unchecked_Union are designed to make sure that this is OK.
6793 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
6794 return Typ;
6796 -- Here for the unconstrained case, we must find actual subtype
6797 -- No actual subtype is available, so we must build it on the fly.
6799 -- Checking the type, not the underlying type, for constrainedness
6800 -- seems to be necessary. Maybe all the tests should be on the type???
6802 elsif (not Is_Constrained (Typ))
6803 and then (Is_Array_Type (Utyp)
6804 or else (Is_Record_Type (Utyp)
6805 and then Has_Discriminants (Utyp)))
6806 and then not Has_Unknown_Discriminants (Utyp)
6807 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
6808 then
6809 -- Nothing to do if in spec expression (why not???)
6811 if In_Spec_Expression then
6812 return Typ;
6814 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
6816 -- If the type has no discriminants, there is no subtype to
6817 -- build, even if the underlying type is discriminated.
6819 return Typ;
6821 -- Else build the actual subtype
6823 else
6824 Decl := Build_Actual_Subtype (Typ, N);
6825 Atyp := Defining_Identifier (Decl);
6827 -- If Build_Actual_Subtype generated a new declaration then use it
6829 if Atyp /= Typ then
6831 -- The actual subtype is an Itype, so analyze the declaration,
6832 -- but do not attach it to the tree, to get the type defined.
6834 Set_Parent (Decl, N);
6835 Set_Is_Itype (Atyp);
6836 Analyze (Decl, Suppress => All_Checks);
6837 Set_Associated_Node_For_Itype (Atyp, N);
6838 Set_Has_Delayed_Freeze (Atyp, False);
6840 -- We need to freeze the actual subtype immediately. This is
6841 -- needed, because otherwise this Itype will not get frozen
6842 -- at all, and it is always safe to freeze on creation because
6843 -- any associated types must be frozen at this point.
6845 Freeze_Itype (Atyp, N);
6846 return Atyp;
6848 -- Otherwise we did not build a declaration, so return original
6850 else
6851 return Typ;
6852 end if;
6853 end if;
6855 -- For all remaining cases, the actual subtype is the same as
6856 -- the nominal type.
6858 else
6859 return Typ;
6860 end if;
6861 end Get_Actual_Subtype;
6863 -------------------------------------
6864 -- Get_Actual_Subtype_If_Available --
6865 -------------------------------------
6867 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
6868 Typ : constant Entity_Id := Etype (N);
6870 begin
6871 -- If what we have is an identifier that references a subprogram
6872 -- formal, or a variable or constant object, then we get the actual
6873 -- subtype from the referenced entity if one has been built.
6875 if Nkind (N) = N_Identifier
6876 and then
6877 (Is_Formal (Entity (N))
6878 or else Ekind (Entity (N)) = E_Constant
6879 or else Ekind (Entity (N)) = E_Variable)
6880 and then Present (Actual_Subtype (Entity (N)))
6881 then
6882 return Actual_Subtype (Entity (N));
6884 -- Otherwise the Etype of N is returned unchanged
6886 else
6887 return Typ;
6888 end if;
6889 end Get_Actual_Subtype_If_Available;
6891 ------------------------
6892 -- Get_Body_From_Stub --
6893 ------------------------
6895 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
6896 begin
6897 return Proper_Body (Unit (Library_Unit (N)));
6898 end Get_Body_From_Stub;
6900 ---------------------
6901 -- Get_Cursor_Type --
6902 ---------------------
6904 function Get_Cursor_Type
6905 (Aspect : Node_Id;
6906 Typ : Entity_Id) return Entity_Id
6908 Assoc : Node_Id;
6909 Func : Entity_Id;
6910 First_Op : Entity_Id;
6911 Cursor : Entity_Id;
6913 begin
6914 -- If error already detected, return
6916 if Error_Posted (Aspect) then
6917 return Any_Type;
6918 end if;
6920 -- The cursor type for an Iterable aspect is the return type of a
6921 -- non-overloaded First primitive operation. Locate association for
6922 -- First.
6924 Assoc := First (Component_Associations (Expression (Aspect)));
6925 First_Op := Any_Id;
6926 while Present (Assoc) loop
6927 if Chars (First (Choices (Assoc))) = Name_First then
6928 First_Op := Expression (Assoc);
6929 exit;
6930 end if;
6932 Next (Assoc);
6933 end loop;
6935 if First_Op = Any_Id then
6936 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
6937 return Any_Type;
6938 end if;
6940 Cursor := Any_Type;
6942 -- Locate function with desired name and profile in scope of type
6944 Func := First_Entity (Scope (Typ));
6945 while Present (Func) loop
6946 if Chars (Func) = Chars (First_Op)
6947 and then Ekind (Func) = E_Function
6948 and then Present (First_Formal (Func))
6949 and then Etype (First_Formal (Func)) = Typ
6950 and then No (Next_Formal (First_Formal (Func)))
6951 then
6952 if Cursor /= Any_Type then
6953 Error_Msg_N
6954 ("Operation First for iterable type must be unique", Aspect);
6955 return Any_Type;
6956 else
6957 Cursor := Etype (Func);
6958 end if;
6959 end if;
6961 Next_Entity (Func);
6962 end loop;
6964 -- If not found, no way to resolve remaining primitives.
6966 if Cursor = Any_Type then
6967 Error_Msg_N
6968 ("No legal primitive operation First for Iterable type", Aspect);
6969 end if;
6971 return Cursor;
6972 end Get_Cursor_Type;
6974 -------------------------------
6975 -- Get_Default_External_Name --
6976 -------------------------------
6978 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
6979 begin
6980 Get_Decoded_Name_String (Chars (E));
6982 if Opt.External_Name_Imp_Casing = Uppercase then
6983 Set_Casing (All_Upper_Case);
6984 else
6985 Set_Casing (All_Lower_Case);
6986 end if;
6988 return
6989 Make_String_Literal (Sloc (E),
6990 Strval => String_From_Name_Buffer);
6991 end Get_Default_External_Name;
6993 --------------------------
6994 -- Get_Enclosing_Object --
6995 --------------------------
6997 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
6998 begin
6999 if Is_Entity_Name (N) then
7000 return Entity (N);
7001 else
7002 case Nkind (N) is
7003 when N_Indexed_Component |
7004 N_Slice |
7005 N_Selected_Component =>
7007 -- If not generating code, a dereference may be left implicit.
7008 -- In thoses cases, return Empty.
7010 if Is_Access_Type (Etype (Prefix (N))) then
7011 return Empty;
7012 else
7013 return Get_Enclosing_Object (Prefix (N));
7014 end if;
7016 when N_Type_Conversion =>
7017 return Get_Enclosing_Object (Expression (N));
7019 when others =>
7020 return Empty;
7021 end case;
7022 end if;
7023 end Get_Enclosing_Object;
7025 ---------------------------
7026 -- Get_Enum_Lit_From_Pos --
7027 ---------------------------
7029 function Get_Enum_Lit_From_Pos
7030 (T : Entity_Id;
7031 Pos : Uint;
7032 Loc : Source_Ptr) return Node_Id
7034 Btyp : Entity_Id := Base_Type (T);
7035 Lit : Node_Id;
7037 begin
7038 -- In the case where the literal is of type Character, Wide_Character
7039 -- or Wide_Wide_Character or of a type derived from them, there needs
7040 -- to be some special handling since there is no explicit chain of
7041 -- literals to search. Instead, an N_Character_Literal node is created
7042 -- with the appropriate Char_Code and Chars fields.
7044 if Is_Standard_Character_Type (T) then
7045 Set_Character_Literal_Name (UI_To_CC (Pos));
7046 return
7047 Make_Character_Literal (Loc,
7048 Chars => Name_Find,
7049 Char_Literal_Value => Pos);
7051 -- For all other cases, we have a complete table of literals, and
7052 -- we simply iterate through the chain of literal until the one
7053 -- with the desired position value is found.
7055 else
7056 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7057 Btyp := Full_View (Btyp);
7058 end if;
7060 Lit := First_Literal (Btyp);
7061 for J in 1 .. UI_To_Int (Pos) loop
7062 Next_Literal (Lit);
7063 end loop;
7065 return New_Occurrence_Of (Lit, Loc);
7066 end if;
7067 end Get_Enum_Lit_From_Pos;
7069 ---------------------------------
7070 -- Get_Ensures_From_CTC_Pragma --
7071 ---------------------------------
7073 function Get_Ensures_From_CTC_Pragma (N : Node_Id) return Node_Id is
7074 Args : constant List_Id := Pragma_Argument_Associations (N);
7075 Res : Node_Id;
7077 begin
7078 if List_Length (Args) = 4 then
7079 Res := Pick (Args, 4);
7081 elsif List_Length (Args) = 3 then
7082 Res := Pick (Args, 3);
7084 if Chars (Res) /= Name_Ensures then
7085 Res := Empty;
7086 end if;
7088 else
7089 Res := Empty;
7090 end if;
7092 return Res;
7093 end Get_Ensures_From_CTC_Pragma;
7095 ------------------------
7096 -- Get_Generic_Entity --
7097 ------------------------
7099 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7100 Ent : constant Entity_Id := Entity (Name (N));
7101 begin
7102 if Present (Renamed_Object (Ent)) then
7103 return Renamed_Object (Ent);
7104 else
7105 return Ent;
7106 end if;
7107 end Get_Generic_Entity;
7109 -------------------------------------
7110 -- Get_Incomplete_View_Of_Ancestor --
7111 -------------------------------------
7113 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7114 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7115 Par_Scope : Entity_Id;
7116 Par_Type : Entity_Id;
7118 begin
7119 -- The incomplete view of an ancestor is only relevant for private
7120 -- derived types in child units.
7122 if not Is_Derived_Type (E)
7123 or else not Is_Child_Unit (Cur_Unit)
7124 then
7125 return Empty;
7127 else
7128 Par_Scope := Scope (Cur_Unit);
7129 if No (Par_Scope) then
7130 return Empty;
7131 end if;
7133 Par_Type := Etype (Base_Type (E));
7135 -- Traverse list of ancestor types until we find one declared in
7136 -- a parent or grandparent unit (two levels seem sufficient).
7138 while Present (Par_Type) loop
7139 if Scope (Par_Type) = Par_Scope
7140 or else Scope (Par_Type) = Scope (Par_Scope)
7141 then
7142 return Par_Type;
7144 elsif not Is_Derived_Type (Par_Type) then
7145 return Empty;
7147 else
7148 Par_Type := Etype (Base_Type (Par_Type));
7149 end if;
7150 end loop;
7152 -- If none found, there is no relevant ancestor type.
7154 return Empty;
7155 end if;
7156 end Get_Incomplete_View_Of_Ancestor;
7158 ----------------------
7159 -- Get_Index_Bounds --
7160 ----------------------
7162 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
7163 Kind : constant Node_Kind := Nkind (N);
7164 R : Node_Id;
7166 begin
7167 if Kind = N_Range then
7168 L := Low_Bound (N);
7169 H := High_Bound (N);
7171 elsif Kind = N_Subtype_Indication then
7172 R := Range_Expression (Constraint (N));
7174 if R = Error then
7175 L := Error;
7176 H := Error;
7177 return;
7179 else
7180 L := Low_Bound (Range_Expression (Constraint (N)));
7181 H := High_Bound (Range_Expression (Constraint (N)));
7182 end if;
7184 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
7185 if Error_Posted (Scalar_Range (Entity (N))) then
7186 L := Error;
7187 H := Error;
7189 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
7190 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
7192 else
7193 L := Low_Bound (Scalar_Range (Entity (N)));
7194 H := High_Bound (Scalar_Range (Entity (N)));
7195 end if;
7197 else
7198 -- N is an expression, indicating a range with one value
7200 L := N;
7201 H := N;
7202 end if;
7203 end Get_Index_Bounds;
7205 ---------------------------------
7206 -- Get_Iterable_Type_Primitive --
7207 ---------------------------------
7209 function Get_Iterable_Type_Primitive
7210 (Typ : Entity_Id;
7211 Nam : Name_Id) return Entity_Id
7213 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
7214 Assoc : Node_Id;
7216 begin
7217 if No (Funcs) then
7218 return Empty;
7220 else
7221 Assoc := First (Component_Associations (Funcs));
7222 while Present (Assoc) loop
7223 if Chars (First (Choices (Assoc))) = Nam then
7224 return Entity (Expression (Assoc));
7225 end if;
7227 Assoc := Next (Assoc);
7228 end loop;
7230 return Empty;
7231 end if;
7232 end Get_Iterable_Type_Primitive;
7234 ----------------------------------
7235 -- Get_Library_Unit_Name_string --
7236 ----------------------------------
7238 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
7239 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
7241 begin
7242 Get_Unit_Name_String (Unit_Name_Id);
7244 -- Remove seven last character (" (spec)" or " (body)")
7246 Name_Len := Name_Len - 7;
7247 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
7248 end Get_Library_Unit_Name_String;
7250 ------------------------
7251 -- Get_Name_Entity_Id --
7252 ------------------------
7254 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
7255 begin
7256 return Entity_Id (Get_Name_Table_Int (Id));
7257 end Get_Name_Entity_Id;
7259 ------------------------------
7260 -- Get_Name_From_CTC_Pragma --
7261 ------------------------------
7263 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
7264 Arg : constant Node_Id :=
7265 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
7266 begin
7267 return Strval (Expr_Value_S (Arg));
7268 end Get_Name_From_CTC_Pragma;
7270 -----------------------
7271 -- Get_Parent_Entity --
7272 -----------------------
7274 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
7275 begin
7276 if Nkind (Unit) = N_Package_Body
7277 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
7278 then
7279 return Defining_Entity
7280 (Specification (Instance_Spec (Original_Node (Unit))));
7281 elsif Nkind (Unit) = N_Package_Instantiation then
7282 return Defining_Entity (Specification (Instance_Spec (Unit)));
7283 else
7284 return Defining_Entity (Unit);
7285 end if;
7286 end Get_Parent_Entity;
7287 -------------------
7288 -- Get_Pragma_Id --
7289 -------------------
7291 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
7292 begin
7293 return Get_Pragma_Id (Pragma_Name (N));
7294 end Get_Pragma_Id;
7296 -----------------------
7297 -- Get_Reason_String --
7298 -----------------------
7300 procedure Get_Reason_String (N : Node_Id) is
7301 begin
7302 if Nkind (N) = N_String_Literal then
7303 Store_String_Chars (Strval (N));
7305 elsif Nkind (N) = N_Op_Concat then
7306 Get_Reason_String (Left_Opnd (N));
7307 Get_Reason_String (Right_Opnd (N));
7309 -- If not of required form, error
7311 else
7312 Error_Msg_N
7313 ("Reason for pragma Warnings has wrong form", N);
7314 Error_Msg_N
7315 ("\must be string literal or concatenation of string literals", N);
7316 return;
7317 end if;
7318 end Get_Reason_String;
7320 ---------------------------
7321 -- Get_Referenced_Object --
7322 ---------------------------
7324 function Get_Referenced_Object (N : Node_Id) return Node_Id is
7325 R : Node_Id;
7327 begin
7328 R := N;
7329 while Is_Entity_Name (R)
7330 and then Present (Renamed_Object (Entity (R)))
7331 loop
7332 R := Renamed_Object (Entity (R));
7333 end loop;
7335 return R;
7336 end Get_Referenced_Object;
7338 ------------------------
7339 -- Get_Renamed_Entity --
7340 ------------------------
7342 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
7343 R : Entity_Id;
7345 begin
7346 R := E;
7347 while Present (Renamed_Entity (R)) loop
7348 R := Renamed_Entity (R);
7349 end loop;
7351 return R;
7352 end Get_Renamed_Entity;
7354 ----------------------------------
7355 -- Get_Requires_From_CTC_Pragma --
7356 ----------------------------------
7358 function Get_Requires_From_CTC_Pragma (N : Node_Id) return Node_Id is
7359 Args : constant List_Id := Pragma_Argument_Associations (N);
7360 Res : Node_Id;
7362 begin
7363 if List_Length (Args) >= 3 then
7364 Res := Pick (Args, 3);
7366 if Chars (Res) /= Name_Requires then
7367 Res := Empty;
7368 end if;
7370 else
7371 Res := Empty;
7372 end if;
7374 return Res;
7375 end Get_Requires_From_CTC_Pragma;
7377 -------------------------
7378 -- Get_Subprogram_Body --
7379 -------------------------
7381 function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
7382 Decl : Node_Id;
7384 begin
7385 Decl := Unit_Declaration_Node (E);
7387 if Nkind (Decl) = N_Subprogram_Body then
7388 return Decl;
7390 -- The below comment is bad, because it is possible for
7391 -- Nkind (Decl) to be an N_Subprogram_Body_Stub ???
7393 else -- Nkind (Decl) = N_Subprogram_Declaration
7395 if Present (Corresponding_Body (Decl)) then
7396 return Unit_Declaration_Node (Corresponding_Body (Decl));
7398 -- Imported subprogram case
7400 else
7401 return Empty;
7402 end if;
7403 end if;
7404 end Get_Subprogram_Body;
7406 ---------------------------
7407 -- Get_Subprogram_Entity --
7408 ---------------------------
7410 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
7411 Subp : Node_Id;
7412 Subp_Id : Entity_Id;
7414 begin
7415 if Nkind (Nod) = N_Accept_Statement then
7416 Subp := Entry_Direct_Name (Nod);
7418 elsif Nkind (Nod) = N_Slice then
7419 Subp := Prefix (Nod);
7421 else
7422 Subp := Name (Nod);
7423 end if;
7425 -- Strip the subprogram call
7427 loop
7428 if Nkind_In (Subp, N_Explicit_Dereference,
7429 N_Indexed_Component,
7430 N_Selected_Component)
7431 then
7432 Subp := Prefix (Subp);
7434 elsif Nkind_In (Subp, N_Type_Conversion,
7435 N_Unchecked_Type_Conversion)
7436 then
7437 Subp := Expression (Subp);
7439 else
7440 exit;
7441 end if;
7442 end loop;
7444 -- Extract the entity of the subprogram call
7446 if Is_Entity_Name (Subp) then
7447 Subp_Id := Entity (Subp);
7449 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
7450 Subp_Id := Directly_Designated_Type (Subp_Id);
7451 end if;
7453 if Is_Subprogram (Subp_Id) then
7454 return Subp_Id;
7455 else
7456 return Empty;
7457 end if;
7459 -- The search did not find a construct that denotes a subprogram
7461 else
7462 return Empty;
7463 end if;
7464 end Get_Subprogram_Entity;
7466 -----------------------------
7467 -- Get_Task_Body_Procedure --
7468 -----------------------------
7470 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
7471 begin
7472 -- Note: A task type may be the completion of a private type with
7473 -- discriminants. When performing elaboration checks on a task
7474 -- declaration, the current view of the type may be the private one,
7475 -- and the procedure that holds the body of the task is held in its
7476 -- underlying type.
7478 -- This is an odd function, why not have Task_Body_Procedure do
7479 -- the following digging???
7481 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
7482 end Get_Task_Body_Procedure;
7484 -----------------------
7485 -- Has_Access_Values --
7486 -----------------------
7488 function Has_Access_Values (T : Entity_Id) return Boolean is
7489 Typ : constant Entity_Id := Underlying_Type (T);
7491 begin
7492 -- Case of a private type which is not completed yet. This can only
7493 -- happen in the case of a generic format type appearing directly, or
7494 -- as a component of the type to which this function is being applied
7495 -- at the top level. Return False in this case, since we certainly do
7496 -- not know that the type contains access types.
7498 if No (Typ) then
7499 return False;
7501 elsif Is_Access_Type (Typ) then
7502 return True;
7504 elsif Is_Array_Type (Typ) then
7505 return Has_Access_Values (Component_Type (Typ));
7507 elsif Is_Record_Type (Typ) then
7508 declare
7509 Comp : Entity_Id;
7511 begin
7512 -- Loop to Check components
7514 Comp := First_Component_Or_Discriminant (Typ);
7515 while Present (Comp) loop
7517 -- Check for access component, tag field does not count, even
7518 -- though it is implemented internally using an access type.
7520 if Has_Access_Values (Etype (Comp))
7521 and then Chars (Comp) /= Name_uTag
7522 then
7523 return True;
7524 end if;
7526 Next_Component_Or_Discriminant (Comp);
7527 end loop;
7528 end;
7530 return False;
7532 else
7533 return False;
7534 end if;
7535 end Has_Access_Values;
7537 ------------------------------
7538 -- Has_Compatible_Alignment --
7539 ------------------------------
7541 function Has_Compatible_Alignment
7542 (Obj : Entity_Id;
7543 Expr : Node_Id) return Alignment_Result
7545 function Has_Compatible_Alignment_Internal
7546 (Obj : Entity_Id;
7547 Expr : Node_Id;
7548 Default : Alignment_Result) return Alignment_Result;
7549 -- This is the internal recursive function that actually does the work.
7550 -- There is one additional parameter, which says what the result should
7551 -- be if no alignment information is found, and there is no definite
7552 -- indication of compatible alignments. At the outer level, this is set
7553 -- to Unknown, but for internal recursive calls in the case where types
7554 -- are known to be correct, it is set to Known_Compatible.
7556 ---------------------------------------
7557 -- Has_Compatible_Alignment_Internal --
7558 ---------------------------------------
7560 function Has_Compatible_Alignment_Internal
7561 (Obj : Entity_Id;
7562 Expr : Node_Id;
7563 Default : Alignment_Result) return Alignment_Result
7565 Result : Alignment_Result := Known_Compatible;
7566 -- Holds the current status of the result. Note that once a value of
7567 -- Known_Incompatible is set, it is sticky and does not get changed
7568 -- to Unknown (the value in Result only gets worse as we go along,
7569 -- never better).
7571 Offs : Uint := No_Uint;
7572 -- Set to a factor of the offset from the base object when Expr is a
7573 -- selected or indexed component, based on Component_Bit_Offset and
7574 -- Component_Size respectively. A negative value is used to represent
7575 -- a value which is not known at compile time.
7577 procedure Check_Prefix;
7578 -- Checks the prefix recursively in the case where the expression
7579 -- is an indexed or selected component.
7581 procedure Set_Result (R : Alignment_Result);
7582 -- If R represents a worse outcome (unknown instead of known
7583 -- compatible, or known incompatible), then set Result to R.
7585 ------------------
7586 -- Check_Prefix --
7587 ------------------
7589 procedure Check_Prefix is
7590 begin
7591 -- The subtlety here is that in doing a recursive call to check
7592 -- the prefix, we have to decide what to do in the case where we
7593 -- don't find any specific indication of an alignment problem.
7595 -- At the outer level, we normally set Unknown as the result in
7596 -- this case, since we can only set Known_Compatible if we really
7597 -- know that the alignment value is OK, but for the recursive
7598 -- call, in the case where the types match, and we have not
7599 -- specified a peculiar alignment for the object, we are only
7600 -- concerned about suspicious rep clauses, the default case does
7601 -- not affect us, since the compiler will, in the absence of such
7602 -- rep clauses, ensure that the alignment is correct.
7604 if Default = Known_Compatible
7605 or else
7606 (Etype (Obj) = Etype (Expr)
7607 and then (Unknown_Alignment (Obj)
7608 or else
7609 Alignment (Obj) = Alignment (Etype (Obj))))
7610 then
7611 Set_Result
7612 (Has_Compatible_Alignment_Internal
7613 (Obj, Prefix (Expr), Known_Compatible));
7615 -- In all other cases, we need a full check on the prefix
7617 else
7618 Set_Result
7619 (Has_Compatible_Alignment_Internal
7620 (Obj, Prefix (Expr), Unknown));
7621 end if;
7622 end Check_Prefix;
7624 ----------------
7625 -- Set_Result --
7626 ----------------
7628 procedure Set_Result (R : Alignment_Result) is
7629 begin
7630 if R > Result then
7631 Result := R;
7632 end if;
7633 end Set_Result;
7635 -- Start of processing for Has_Compatible_Alignment_Internal
7637 begin
7638 -- If Expr is a selected component, we must make sure there is no
7639 -- potentially troublesome component clause, and that the record is
7640 -- not packed.
7642 if Nkind (Expr) = N_Selected_Component then
7644 -- Packed record always generate unknown alignment
7646 if Is_Packed (Etype (Prefix (Expr))) then
7647 Set_Result (Unknown);
7648 end if;
7650 -- Check prefix and component offset
7652 Check_Prefix;
7653 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
7655 -- If Expr is an indexed component, we must make sure there is no
7656 -- potentially troublesome Component_Size clause and that the array
7657 -- is not bit-packed.
7659 elsif Nkind (Expr) = N_Indexed_Component then
7660 declare
7661 Typ : constant Entity_Id := Etype (Prefix (Expr));
7662 Ind : constant Node_Id := First_Index (Typ);
7664 begin
7665 -- Bit packed array always generates unknown alignment
7667 if Is_Bit_Packed_Array (Typ) then
7668 Set_Result (Unknown);
7669 end if;
7671 -- Check prefix and component offset
7673 Check_Prefix;
7674 Offs := Component_Size (Typ);
7676 -- Small optimization: compute the full offset when possible
7678 if Offs /= No_Uint
7679 and then Offs > Uint_0
7680 and then Present (Ind)
7681 and then Nkind (Ind) = N_Range
7682 and then Compile_Time_Known_Value (Low_Bound (Ind))
7683 and then Compile_Time_Known_Value (First (Expressions (Expr)))
7684 then
7685 Offs := Offs * (Expr_Value (First (Expressions (Expr)))
7686 - Expr_Value (Low_Bound ((Ind))));
7687 end if;
7688 end;
7689 end if;
7691 -- If we have a null offset, the result is entirely determined by
7692 -- the base object and has already been computed recursively.
7694 if Offs = Uint_0 then
7695 null;
7697 -- Case where we know the alignment of the object
7699 elsif Known_Alignment (Obj) then
7700 declare
7701 ObjA : constant Uint := Alignment (Obj);
7702 ExpA : Uint := No_Uint;
7703 SizA : Uint := No_Uint;
7705 begin
7706 -- If alignment of Obj is 1, then we are always OK
7708 if ObjA = 1 then
7709 Set_Result (Known_Compatible);
7711 -- Alignment of Obj is greater than 1, so we need to check
7713 else
7714 -- If we have an offset, see if it is compatible
7716 if Offs /= No_Uint and Offs > Uint_0 then
7717 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
7718 Set_Result (Known_Incompatible);
7719 end if;
7721 -- See if Expr is an object with known alignment
7723 elsif Is_Entity_Name (Expr)
7724 and then Known_Alignment (Entity (Expr))
7725 then
7726 ExpA := Alignment (Entity (Expr));
7728 -- Otherwise, we can use the alignment of the type of
7729 -- Expr given that we already checked for
7730 -- discombobulating rep clauses for the cases of indexed
7731 -- and selected components above.
7733 elsif Known_Alignment (Etype (Expr)) then
7734 ExpA := Alignment (Etype (Expr));
7736 -- Otherwise the alignment is unknown
7738 else
7739 Set_Result (Default);
7740 end if;
7742 -- If we got an alignment, see if it is acceptable
7744 if ExpA /= No_Uint and then ExpA < ObjA then
7745 Set_Result (Known_Incompatible);
7746 end if;
7748 -- If Expr is not a piece of a larger object, see if size
7749 -- is given. If so, check that it is not too small for the
7750 -- required alignment.
7752 if Offs /= No_Uint then
7753 null;
7755 -- See if Expr is an object with known size
7757 elsif Is_Entity_Name (Expr)
7758 and then Known_Static_Esize (Entity (Expr))
7759 then
7760 SizA := Esize (Entity (Expr));
7762 -- Otherwise, we check the object size of the Expr type
7764 elsif Known_Static_Esize (Etype (Expr)) then
7765 SizA := Esize (Etype (Expr));
7766 end if;
7768 -- If we got a size, see if it is a multiple of the Obj
7769 -- alignment, if not, then the alignment cannot be
7770 -- acceptable, since the size is always a multiple of the
7771 -- alignment.
7773 if SizA /= No_Uint then
7774 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
7775 Set_Result (Known_Incompatible);
7776 end if;
7777 end if;
7778 end if;
7779 end;
7781 -- If we do not know required alignment, any non-zero offset is a
7782 -- potential problem (but certainly may be OK, so result is unknown).
7784 elsif Offs /= No_Uint then
7785 Set_Result (Unknown);
7787 -- If we can't find the result by direct comparison of alignment
7788 -- values, then there is still one case that we can determine known
7789 -- result, and that is when we can determine that the types are the
7790 -- same, and no alignments are specified. Then we known that the
7791 -- alignments are compatible, even if we don't know the alignment
7792 -- value in the front end.
7794 elsif Etype (Obj) = Etype (Expr) then
7796 -- Types are the same, but we have to check for possible size
7797 -- and alignments on the Expr object that may make the alignment
7798 -- different, even though the types are the same.
7800 if Is_Entity_Name (Expr) then
7802 -- First check alignment of the Expr object. Any alignment less
7803 -- than Maximum_Alignment is worrisome since this is the case
7804 -- where we do not know the alignment of Obj.
7806 if Known_Alignment (Entity (Expr))
7807 and then UI_To_Int (Alignment (Entity (Expr))) <
7808 Ttypes.Maximum_Alignment
7809 then
7810 Set_Result (Unknown);
7812 -- Now check size of Expr object. Any size that is not an
7813 -- even multiple of Maximum_Alignment is also worrisome
7814 -- since it may cause the alignment of the object to be less
7815 -- than the alignment of the type.
7817 elsif Known_Static_Esize (Entity (Expr))
7818 and then
7819 (UI_To_Int (Esize (Entity (Expr))) mod
7820 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
7821 /= 0
7822 then
7823 Set_Result (Unknown);
7825 -- Otherwise same type is decisive
7827 else
7828 Set_Result (Known_Compatible);
7829 end if;
7830 end if;
7832 -- Another case to deal with is when there is an explicit size or
7833 -- alignment clause when the types are not the same. If so, then the
7834 -- result is Unknown. We don't need to do this test if the Default is
7835 -- Unknown, since that result will be set in any case.
7837 elsif Default /= Unknown
7838 and then (Has_Size_Clause (Etype (Expr))
7839 or else
7840 Has_Alignment_Clause (Etype (Expr)))
7841 then
7842 Set_Result (Unknown);
7844 -- If no indication found, set default
7846 else
7847 Set_Result (Default);
7848 end if;
7850 -- Return worst result found
7852 return Result;
7853 end Has_Compatible_Alignment_Internal;
7855 -- Start of processing for Has_Compatible_Alignment
7857 begin
7858 -- If Obj has no specified alignment, then set alignment from the type
7859 -- alignment. Perhaps we should always do this, but for sure we should
7860 -- do it when there is an address clause since we can do more if the
7861 -- alignment is known.
7863 if Unknown_Alignment (Obj) then
7864 Set_Alignment (Obj, Alignment (Etype (Obj)));
7865 end if;
7867 -- Now do the internal call that does all the work
7869 return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
7870 end Has_Compatible_Alignment;
7872 ----------------------
7873 -- Has_Declarations --
7874 ----------------------
7876 function Has_Declarations (N : Node_Id) return Boolean is
7877 begin
7878 return Nkind_In (Nkind (N), N_Accept_Statement,
7879 N_Block_Statement,
7880 N_Compilation_Unit_Aux,
7881 N_Entry_Body,
7882 N_Package_Body,
7883 N_Protected_Body,
7884 N_Subprogram_Body,
7885 N_Task_Body,
7886 N_Package_Specification);
7887 end Has_Declarations;
7889 ---------------------------------
7890 -- Has_Defaulted_Discriminants --
7891 ---------------------------------
7893 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
7894 begin
7895 return Has_Discriminants (Typ)
7896 and then Present (First_Discriminant (Typ))
7897 and then Present (Discriminant_Default_Value
7898 (First_Discriminant (Typ)));
7899 end Has_Defaulted_Discriminants;
7901 -------------------
7902 -- Has_Denormals --
7903 -------------------
7905 function Has_Denormals (E : Entity_Id) return Boolean is
7906 begin
7907 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
7908 end Has_Denormals;
7910 -------------------------------------------
7911 -- Has_Discriminant_Dependent_Constraint --
7912 -------------------------------------------
7914 function Has_Discriminant_Dependent_Constraint
7915 (Comp : Entity_Id) return Boolean
7917 Comp_Decl : constant Node_Id := Parent (Comp);
7918 Subt_Indic : Node_Id;
7919 Constr : Node_Id;
7920 Assn : Node_Id;
7922 begin
7923 -- Discriminants can't depend on discriminants
7925 if Ekind (Comp) = E_Discriminant then
7926 return False;
7928 else
7929 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
7931 if Nkind (Subt_Indic) = N_Subtype_Indication then
7932 Constr := Constraint (Subt_Indic);
7934 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
7935 Assn := First (Constraints (Constr));
7936 while Present (Assn) loop
7937 case Nkind (Assn) is
7938 when N_Subtype_Indication |
7939 N_Range |
7940 N_Identifier
7942 if Depends_On_Discriminant (Assn) then
7943 return True;
7944 end if;
7946 when N_Discriminant_Association =>
7947 if Depends_On_Discriminant (Expression (Assn)) then
7948 return True;
7949 end if;
7951 when others =>
7952 null;
7953 end case;
7955 Next (Assn);
7956 end loop;
7957 end if;
7958 end if;
7959 end if;
7961 return False;
7962 end Has_Discriminant_Dependent_Constraint;
7964 --------------------------
7965 -- Has_Enabled_Property --
7966 --------------------------
7968 function Has_Enabled_Property
7969 (Item_Id : Entity_Id;
7970 Property : Name_Id) return Boolean
7972 function State_Has_Enabled_Property return Boolean;
7973 -- Determine whether a state denoted by Item_Id has the property enabled
7975 function Variable_Has_Enabled_Property return Boolean;
7976 -- Determine whether a variable denoted by Item_Id has the property
7977 -- enabled.
7979 --------------------------------
7980 -- State_Has_Enabled_Property --
7981 --------------------------------
7983 function State_Has_Enabled_Property return Boolean is
7984 Decl : constant Node_Id := Parent (Item_Id);
7985 Opt : Node_Id;
7986 Opt_Nam : Node_Id;
7987 Prop : Node_Id;
7988 Prop_Nam : Node_Id;
7989 Props : Node_Id;
7991 begin
7992 -- The declaration of an external abstract state appears as an
7993 -- extension aggregate. If this is not the case, properties can never
7994 -- be set.
7996 if Nkind (Decl) /= N_Extension_Aggregate then
7997 return False;
7998 end if;
8000 -- When External appears as a simple option, it automatically enables
8001 -- all properties.
8003 Opt := First (Expressions (Decl));
8004 while Present (Opt) loop
8005 if Nkind (Opt) = N_Identifier
8006 and then Chars (Opt) = Name_External
8007 then
8008 return True;
8009 end if;
8011 Next (Opt);
8012 end loop;
8014 -- When External specifies particular properties, inspect those and
8015 -- find the desired one (if any).
8017 Opt := First (Component_Associations (Decl));
8018 while Present (Opt) loop
8019 Opt_Nam := First (Choices (Opt));
8021 if Nkind (Opt_Nam) = N_Identifier
8022 and then Chars (Opt_Nam) = Name_External
8023 then
8024 Props := Expression (Opt);
8026 -- Multiple properties appear as an aggregate
8028 if Nkind (Props) = N_Aggregate then
8030 -- Simple property form
8032 Prop := First (Expressions (Props));
8033 while Present (Prop) loop
8034 if Chars (Prop) = Property then
8035 return True;
8036 end if;
8038 Next (Prop);
8039 end loop;
8041 -- Property with expression form
8043 Prop := First (Component_Associations (Props));
8044 while Present (Prop) loop
8045 Prop_Nam := First (Choices (Prop));
8047 -- The property can be represented in two ways:
8048 -- others => <value>
8049 -- <property> => <value>
8051 if Nkind (Prop_Nam) = N_Others_Choice
8052 or else (Nkind (Prop_Nam) = N_Identifier
8053 and then Chars (Prop_Nam) = Property)
8054 then
8055 return Is_True (Expr_Value (Expression (Prop)));
8056 end if;
8058 Next (Prop);
8059 end loop;
8061 -- Single property
8063 else
8064 return Chars (Props) = Property;
8065 end if;
8066 end if;
8068 Next (Opt);
8069 end loop;
8071 return False;
8072 end State_Has_Enabled_Property;
8074 -----------------------------------
8075 -- Variable_Has_Enabled_Property --
8076 -----------------------------------
8078 function Variable_Has_Enabled_Property return Boolean is
8079 function Is_Enabled (Prag : Node_Id) return Boolean;
8080 -- Determine whether property pragma Prag (if present) denotes an
8081 -- enabled property.
8083 ----------------
8084 -- Is_Enabled --
8085 ----------------
8087 function Is_Enabled (Prag : Node_Id) return Boolean is
8088 Arg2 : Node_Id;
8090 begin
8091 if Present (Prag) then
8092 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
8094 -- The pragma has an optional Boolean expression, the related
8095 -- property is enabled only when the expression evaluates to
8096 -- True.
8098 if Present (Arg2) then
8099 return Is_True (Expr_Value (Get_Pragma_Arg (Arg2)));
8101 -- Otherwise the lack of expression enables the property by
8102 -- default.
8104 else
8105 return True;
8106 end if;
8108 -- The property was never set in the first place
8110 else
8111 return False;
8112 end if;
8113 end Is_Enabled;
8115 -- Local variables
8117 AR : constant Node_Id :=
8118 Get_Pragma (Item_Id, Pragma_Async_Readers);
8119 AW : constant Node_Id :=
8120 Get_Pragma (Item_Id, Pragma_Async_Writers);
8121 ER : constant Node_Id :=
8122 Get_Pragma (Item_Id, Pragma_Effective_Reads);
8123 EW : constant Node_Id :=
8124 Get_Pragma (Item_Id, Pragma_Effective_Writes);
8126 -- Start of processing for Variable_Has_Enabled_Property
8128 begin
8129 -- A non-effectively volatile object can never possess external
8130 -- properties.
8132 if not Is_Effectively_Volatile (Item_Id) then
8133 return False;
8135 -- External properties related to variables come in two flavors -
8136 -- explicit and implicit. The explicit case is characterized by the
8137 -- presence of a property pragma with an optional Boolean flag. The
8138 -- property is enabled when the flag evaluates to True or the flag is
8139 -- missing altogether.
8141 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
8142 return True;
8144 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
8145 return True;
8147 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
8148 return True;
8150 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
8151 return True;
8153 -- The implicit case lacks all property pragmas
8155 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
8156 return True;
8158 else
8159 return False;
8160 end if;
8161 end Variable_Has_Enabled_Property;
8163 -- Start of processing for Has_Enabled_Property
8165 begin
8166 -- Abstract states and variables have a flexible scheme of specifying
8167 -- external properties.
8169 if Ekind (Item_Id) = E_Abstract_State then
8170 return State_Has_Enabled_Property;
8172 elsif Ekind (Item_Id) = E_Variable then
8173 return Variable_Has_Enabled_Property;
8175 -- Otherwise a property is enabled when the related item is effectively
8176 -- volatile.
8178 else
8179 return Is_Effectively_Volatile (Item_Id);
8180 end if;
8181 end Has_Enabled_Property;
8183 --------------------
8184 -- Has_Infinities --
8185 --------------------
8187 function Has_Infinities (E : Entity_Id) return Boolean is
8188 begin
8189 return
8190 Is_Floating_Point_Type (E)
8191 and then Nkind (Scalar_Range (E)) = N_Range
8192 and then Includes_Infinities (Scalar_Range (E));
8193 end Has_Infinities;
8195 --------------------
8196 -- Has_Interfaces --
8197 --------------------
8199 function Has_Interfaces
8200 (T : Entity_Id;
8201 Use_Full_View : Boolean := True) return Boolean
8203 Typ : Entity_Id := Base_Type (T);
8205 begin
8206 -- Handle concurrent types
8208 if Is_Concurrent_Type (Typ) then
8209 Typ := Corresponding_Record_Type (Typ);
8210 end if;
8212 if not Present (Typ)
8213 or else not Is_Record_Type (Typ)
8214 or else not Is_Tagged_Type (Typ)
8215 then
8216 return False;
8217 end if;
8219 -- Handle private types
8221 if Use_Full_View and then Present (Full_View (Typ)) then
8222 Typ := Full_View (Typ);
8223 end if;
8225 -- Handle concurrent record types
8227 if Is_Concurrent_Record_Type (Typ)
8228 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
8229 then
8230 return True;
8231 end if;
8233 loop
8234 if Is_Interface (Typ)
8235 or else
8236 (Is_Record_Type (Typ)
8237 and then Present (Interfaces (Typ))
8238 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
8239 then
8240 return True;
8241 end if;
8243 exit when Etype (Typ) = Typ
8245 -- Handle private types
8247 or else (Present (Full_View (Etype (Typ)))
8248 and then Full_View (Etype (Typ)) = Typ)
8250 -- Protect frontend against wrong sources with cyclic derivations
8252 or else Etype (Typ) = T;
8254 -- Climb to the ancestor type handling private types
8256 if Present (Full_View (Etype (Typ))) then
8257 Typ := Full_View (Etype (Typ));
8258 else
8259 Typ := Etype (Typ);
8260 end if;
8261 end loop;
8263 return False;
8264 end Has_Interfaces;
8266 ---------------------------------
8267 -- Has_No_Obvious_Side_Effects --
8268 ---------------------------------
8270 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
8271 begin
8272 -- For now, just handle literals, constants, and non-volatile
8273 -- variables and expressions combining these with operators or
8274 -- short circuit forms.
8276 if Nkind (N) in N_Numeric_Or_String_Literal then
8277 return True;
8279 elsif Nkind (N) = N_Character_Literal then
8280 return True;
8282 elsif Nkind (N) in N_Unary_Op then
8283 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
8285 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
8286 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
8287 and then
8288 Has_No_Obvious_Side_Effects (Right_Opnd (N));
8290 elsif Nkind (N) = N_Expression_With_Actions
8291 and then Is_Empty_List (Actions (N))
8292 then
8293 return Has_No_Obvious_Side_Effects (Expression (N));
8295 elsif Nkind (N) in N_Has_Entity then
8296 return Present (Entity (N))
8297 and then Ekind_In (Entity (N), E_Variable,
8298 E_Constant,
8299 E_Enumeration_Literal,
8300 E_In_Parameter,
8301 E_Out_Parameter,
8302 E_In_Out_Parameter)
8303 and then not Is_Volatile (Entity (N));
8305 else
8306 return False;
8307 end if;
8308 end Has_No_Obvious_Side_Effects;
8310 ------------------------
8311 -- Has_Null_Exclusion --
8312 ------------------------
8314 function Has_Null_Exclusion (N : Node_Id) return Boolean is
8315 begin
8316 case Nkind (N) is
8317 when N_Access_Definition |
8318 N_Access_Function_Definition |
8319 N_Access_Procedure_Definition |
8320 N_Access_To_Object_Definition |
8321 N_Allocator |
8322 N_Derived_Type_Definition |
8323 N_Function_Specification |
8324 N_Subtype_Declaration =>
8325 return Null_Exclusion_Present (N);
8327 when N_Component_Definition |
8328 N_Formal_Object_Declaration |
8329 N_Object_Renaming_Declaration =>
8330 if Present (Subtype_Mark (N)) then
8331 return Null_Exclusion_Present (N);
8332 else pragma Assert (Present (Access_Definition (N)));
8333 return Null_Exclusion_Present (Access_Definition (N));
8334 end if;
8336 when N_Discriminant_Specification =>
8337 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
8338 return Null_Exclusion_Present (Discriminant_Type (N));
8339 else
8340 return Null_Exclusion_Present (N);
8341 end if;
8343 when N_Object_Declaration =>
8344 if Nkind (Object_Definition (N)) = N_Access_Definition then
8345 return Null_Exclusion_Present (Object_Definition (N));
8346 else
8347 return Null_Exclusion_Present (N);
8348 end if;
8350 when N_Parameter_Specification =>
8351 if Nkind (Parameter_Type (N)) = N_Access_Definition then
8352 return Null_Exclusion_Present (Parameter_Type (N));
8353 else
8354 return Null_Exclusion_Present (N);
8355 end if;
8357 when others =>
8358 return False;
8360 end case;
8361 end Has_Null_Exclusion;
8363 ------------------------
8364 -- Has_Null_Extension --
8365 ------------------------
8367 function Has_Null_Extension (T : Entity_Id) return Boolean is
8368 B : constant Entity_Id := Base_Type (T);
8369 Comps : Node_Id;
8370 Ext : Node_Id;
8372 begin
8373 if Nkind (Parent (B)) = N_Full_Type_Declaration
8374 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
8375 then
8376 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
8378 if Present (Ext) then
8379 if Null_Present (Ext) then
8380 return True;
8381 else
8382 Comps := Component_List (Ext);
8384 -- The null component list is rewritten during analysis to
8385 -- include the parent component. Any other component indicates
8386 -- that the extension was not originally null.
8388 return Null_Present (Comps)
8389 or else No (Next (First (Component_Items (Comps))));
8390 end if;
8391 else
8392 return False;
8393 end if;
8395 else
8396 return False;
8397 end if;
8398 end Has_Null_Extension;
8400 -------------------------------
8401 -- Has_Overriding_Initialize --
8402 -------------------------------
8404 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
8405 BT : constant Entity_Id := Base_Type (T);
8406 P : Elmt_Id;
8408 begin
8409 if Is_Controlled (BT) then
8410 if Is_RTU (Scope (BT), Ada_Finalization) then
8411 return False;
8413 elsif Present (Primitive_Operations (BT)) then
8414 P := First_Elmt (Primitive_Operations (BT));
8415 while Present (P) loop
8416 declare
8417 Init : constant Entity_Id := Node (P);
8418 Formal : constant Entity_Id := First_Formal (Init);
8419 begin
8420 if Ekind (Init) = E_Procedure
8421 and then Chars (Init) = Name_Initialize
8422 and then Comes_From_Source (Init)
8423 and then Present (Formal)
8424 and then Etype (Formal) = BT
8425 and then No (Next_Formal (Formal))
8426 and then (Ada_Version < Ada_2012
8427 or else not Null_Present (Parent (Init)))
8428 then
8429 return True;
8430 end if;
8431 end;
8433 Next_Elmt (P);
8434 end loop;
8435 end if;
8437 -- Here if type itself does not have a non-null Initialize operation:
8438 -- check immediate ancestor.
8440 if Is_Derived_Type (BT)
8441 and then Has_Overriding_Initialize (Etype (BT))
8442 then
8443 return True;
8444 end if;
8445 end if;
8447 return False;
8448 end Has_Overriding_Initialize;
8450 --------------------------------------
8451 -- Has_Preelaborable_Initialization --
8452 --------------------------------------
8454 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
8455 Has_PE : Boolean;
8457 procedure Check_Components (E : Entity_Id);
8458 -- Check component/discriminant chain, sets Has_PE False if a component
8459 -- or discriminant does not meet the preelaborable initialization rules.
8461 ----------------------
8462 -- Check_Components --
8463 ----------------------
8465 procedure Check_Components (E : Entity_Id) is
8466 Ent : Entity_Id;
8467 Exp : Node_Id;
8469 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
8470 -- Returns True if and only if the expression denoted by N does not
8471 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
8473 ---------------------------------
8474 -- Is_Preelaborable_Expression --
8475 ---------------------------------
8477 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
8478 Exp : Node_Id;
8479 Assn : Node_Id;
8480 Choice : Node_Id;
8481 Comp_Type : Entity_Id;
8482 Is_Array_Aggr : Boolean;
8484 begin
8485 if Is_OK_Static_Expression (N) then
8486 return True;
8488 elsif Nkind (N) = N_Null then
8489 return True;
8491 -- Attributes are allowed in general, even if their prefix is a
8492 -- formal type. (It seems that certain attributes known not to be
8493 -- static might not be allowed, but there are no rules to prevent
8494 -- them.)
8496 elsif Nkind (N) = N_Attribute_Reference then
8497 return True;
8499 -- The name of a discriminant evaluated within its parent type is
8500 -- defined to be preelaborable (10.2.1(8)). Note that we test for
8501 -- names that denote discriminals as well as discriminants to
8502 -- catch references occurring within init procs.
8504 elsif Is_Entity_Name (N)
8505 and then
8506 (Ekind (Entity (N)) = E_Discriminant
8507 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
8508 and then Present (Discriminal_Link (Entity (N)))))
8509 then
8510 return True;
8512 elsif Nkind (N) = N_Qualified_Expression then
8513 return Is_Preelaborable_Expression (Expression (N));
8515 -- For aggregates we have to check that each of the associations
8516 -- is preelaborable.
8518 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8519 Is_Array_Aggr := Is_Array_Type (Etype (N));
8521 if Is_Array_Aggr then
8522 Comp_Type := Component_Type (Etype (N));
8523 end if;
8525 -- Check the ancestor part of extension aggregates, which must
8526 -- be either the name of a type that has preelaborable init or
8527 -- an expression that is preelaborable.
8529 if Nkind (N) = N_Extension_Aggregate then
8530 declare
8531 Anc_Part : constant Node_Id := Ancestor_Part (N);
8533 begin
8534 if Is_Entity_Name (Anc_Part)
8535 and then Is_Type (Entity (Anc_Part))
8536 then
8537 if not Has_Preelaborable_Initialization
8538 (Entity (Anc_Part))
8539 then
8540 return False;
8541 end if;
8543 elsif not Is_Preelaborable_Expression (Anc_Part) then
8544 return False;
8545 end if;
8546 end;
8547 end if;
8549 -- Check positional associations
8551 Exp := First (Expressions (N));
8552 while Present (Exp) loop
8553 if not Is_Preelaborable_Expression (Exp) then
8554 return False;
8555 end if;
8557 Next (Exp);
8558 end loop;
8560 -- Check named associations
8562 Assn := First (Component_Associations (N));
8563 while Present (Assn) loop
8564 Choice := First (Choices (Assn));
8565 while Present (Choice) loop
8566 if Is_Array_Aggr then
8567 if Nkind (Choice) = N_Others_Choice then
8568 null;
8570 elsif Nkind (Choice) = N_Range then
8571 if not Is_OK_Static_Range (Choice) then
8572 return False;
8573 end if;
8575 elsif not Is_OK_Static_Expression (Choice) then
8576 return False;
8577 end if;
8579 else
8580 Comp_Type := Etype (Choice);
8581 end if;
8583 Next (Choice);
8584 end loop;
8586 -- If the association has a <> at this point, then we have
8587 -- to check whether the component's type has preelaborable
8588 -- initialization. Note that this only occurs when the
8589 -- association's corresponding component does not have a
8590 -- default expression, the latter case having already been
8591 -- expanded as an expression for the association.
8593 if Box_Present (Assn) then
8594 if not Has_Preelaborable_Initialization (Comp_Type) then
8595 return False;
8596 end if;
8598 -- In the expression case we check whether the expression
8599 -- is preelaborable.
8601 elsif
8602 not Is_Preelaborable_Expression (Expression (Assn))
8603 then
8604 return False;
8605 end if;
8607 Next (Assn);
8608 end loop;
8610 -- If we get here then aggregate as a whole is preelaborable
8612 return True;
8614 -- All other cases are not preelaborable
8616 else
8617 return False;
8618 end if;
8619 end Is_Preelaborable_Expression;
8621 -- Start of processing for Check_Components
8623 begin
8624 -- Loop through entities of record or protected type
8626 Ent := E;
8627 while Present (Ent) loop
8629 -- We are interested only in components and discriminants
8631 Exp := Empty;
8633 case Ekind (Ent) is
8634 when E_Component =>
8636 -- Get default expression if any. If there is no declaration
8637 -- node, it means we have an internal entity. The parent and
8638 -- tag fields are examples of such entities. For such cases,
8639 -- we just test the type of the entity.
8641 if Present (Declaration_Node (Ent)) then
8642 Exp := Expression (Declaration_Node (Ent));
8643 end if;
8645 when E_Discriminant =>
8647 -- Note: for a renamed discriminant, the Declaration_Node
8648 -- may point to the one from the ancestor, and have a
8649 -- different expression, so use the proper attribute to
8650 -- retrieve the expression from the derived constraint.
8652 Exp := Discriminant_Default_Value (Ent);
8654 when others =>
8655 goto Check_Next_Entity;
8656 end case;
8658 -- A component has PI if it has no default expression and the
8659 -- component type has PI.
8661 if No (Exp) then
8662 if not Has_Preelaborable_Initialization (Etype (Ent)) then
8663 Has_PE := False;
8664 exit;
8665 end if;
8667 -- Require the default expression to be preelaborable
8669 elsif not Is_Preelaborable_Expression (Exp) then
8670 Has_PE := False;
8671 exit;
8672 end if;
8674 <<Check_Next_Entity>>
8675 Next_Entity (Ent);
8676 end loop;
8677 end Check_Components;
8679 -- Start of processing for Has_Preelaborable_Initialization
8681 begin
8682 -- Immediate return if already marked as known preelaborable init. This
8683 -- covers types for which this function has already been called once
8684 -- and returned True (in which case the result is cached), and also
8685 -- types to which a pragma Preelaborable_Initialization applies.
8687 if Known_To_Have_Preelab_Init (E) then
8688 return True;
8689 end if;
8691 -- If the type is a subtype representing a generic actual type, then
8692 -- test whether its base type has preelaborable initialization since
8693 -- the subtype representing the actual does not inherit this attribute
8694 -- from the actual or formal. (but maybe it should???)
8696 if Is_Generic_Actual_Type (E) then
8697 return Has_Preelaborable_Initialization (Base_Type (E));
8698 end if;
8700 -- All elementary types have preelaborable initialization
8702 if Is_Elementary_Type (E) then
8703 Has_PE := True;
8705 -- Array types have PI if the component type has PI
8707 elsif Is_Array_Type (E) then
8708 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
8710 -- A derived type has preelaborable initialization if its parent type
8711 -- has preelaborable initialization and (in the case of a derived record
8712 -- extension) if the non-inherited components all have preelaborable
8713 -- initialization. However, a user-defined controlled type with an
8714 -- overriding Initialize procedure does not have preelaborable
8715 -- initialization.
8717 elsif Is_Derived_Type (E) then
8719 -- If the derived type is a private extension then it doesn't have
8720 -- preelaborable initialization.
8722 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
8723 return False;
8724 end if;
8726 -- First check whether ancestor type has preelaborable initialization
8728 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
8730 -- If OK, check extension components (if any)
8732 if Has_PE and then Is_Record_Type (E) then
8733 Check_Components (First_Entity (E));
8734 end if;
8736 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
8737 -- with a user defined Initialize procedure does not have PI. If
8738 -- the type is untagged, the control primitives come from a component
8739 -- that has already been checked.
8741 if Has_PE
8742 and then Is_Controlled (E)
8743 and then Is_Tagged_Type (E)
8744 and then Has_Overriding_Initialize (E)
8745 then
8746 Has_PE := False;
8747 end if;
8749 -- Private types not derived from a type having preelaborable init and
8750 -- that are not marked with pragma Preelaborable_Initialization do not
8751 -- have preelaborable initialization.
8753 elsif Is_Private_Type (E) then
8754 return False;
8756 -- Record type has PI if it is non private and all components have PI
8758 elsif Is_Record_Type (E) then
8759 Has_PE := True;
8760 Check_Components (First_Entity (E));
8762 -- Protected types must not have entries, and components must meet
8763 -- same set of rules as for record components.
8765 elsif Is_Protected_Type (E) then
8766 if Has_Entries (E) then
8767 Has_PE := False;
8768 else
8769 Has_PE := True;
8770 Check_Components (First_Entity (E));
8771 Check_Components (First_Private_Entity (E));
8772 end if;
8774 -- Type System.Address always has preelaborable initialization
8776 elsif Is_RTE (E, RE_Address) then
8777 Has_PE := True;
8779 -- In all other cases, type does not have preelaborable initialization
8781 else
8782 return False;
8783 end if;
8785 -- If type has preelaborable initialization, cache result
8787 if Has_PE then
8788 Set_Known_To_Have_Preelab_Init (E);
8789 end if;
8791 return Has_PE;
8792 end Has_Preelaborable_Initialization;
8794 ---------------------------
8795 -- Has_Private_Component --
8796 ---------------------------
8798 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
8799 Btype : Entity_Id := Base_Type (Type_Id);
8800 Component : Entity_Id;
8802 begin
8803 if Error_Posted (Type_Id)
8804 or else Error_Posted (Btype)
8805 then
8806 return False;
8807 end if;
8809 if Is_Class_Wide_Type (Btype) then
8810 Btype := Root_Type (Btype);
8811 end if;
8813 if Is_Private_Type (Btype) then
8814 declare
8815 UT : constant Entity_Id := Underlying_Type (Btype);
8816 begin
8817 if No (UT) then
8818 if No (Full_View (Btype)) then
8819 return not Is_Generic_Type (Btype)
8820 and then
8821 not Is_Generic_Type (Root_Type (Btype));
8822 else
8823 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
8824 end if;
8825 else
8826 return not Is_Frozen (UT) and then Has_Private_Component (UT);
8827 end if;
8828 end;
8830 elsif Is_Array_Type (Btype) then
8831 return Has_Private_Component (Component_Type (Btype));
8833 elsif Is_Record_Type (Btype) then
8834 Component := First_Component (Btype);
8835 while Present (Component) loop
8836 if Has_Private_Component (Etype (Component)) then
8837 return True;
8838 end if;
8840 Next_Component (Component);
8841 end loop;
8843 return False;
8845 elsif Is_Protected_Type (Btype)
8846 and then Present (Corresponding_Record_Type (Btype))
8847 then
8848 return Has_Private_Component (Corresponding_Record_Type (Btype));
8850 else
8851 return False;
8852 end if;
8853 end Has_Private_Component;
8855 ----------------------
8856 -- Has_Signed_Zeros --
8857 ----------------------
8859 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
8860 begin
8861 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
8862 end Has_Signed_Zeros;
8864 -----------------------------
8865 -- Has_Static_Array_Bounds --
8866 -----------------------------
8868 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
8869 Ndims : constant Nat := Number_Dimensions (Typ);
8871 Index : Node_Id;
8872 Low : Node_Id;
8873 High : Node_Id;
8875 begin
8876 -- Unconstrained types do not have static bounds
8878 if not Is_Constrained (Typ) then
8879 return False;
8880 end if;
8882 -- First treat string literals specially, as the lower bound and length
8883 -- of string literals are not stored like those of arrays.
8885 -- A string literal always has static bounds
8887 if Ekind (Typ) = E_String_Literal_Subtype then
8888 return True;
8889 end if;
8891 -- Treat all dimensions in turn
8893 Index := First_Index (Typ);
8894 for Indx in 1 .. Ndims loop
8896 -- In case of an illegal index which is not a discrete type, return
8897 -- that the type is not static.
8899 if not Is_Discrete_Type (Etype (Index))
8900 or else Etype (Index) = Any_Type
8901 then
8902 return False;
8903 end if;
8905 Get_Index_Bounds (Index, Low, High);
8907 if Error_Posted (Low) or else Error_Posted (High) then
8908 return False;
8909 end if;
8911 if Is_OK_Static_Expression (Low)
8912 and then
8913 Is_OK_Static_Expression (High)
8914 then
8915 null;
8916 else
8917 return False;
8918 end if;
8920 Next (Index);
8921 end loop;
8923 -- If we fall through the loop, all indexes matched
8925 return True;
8926 end Has_Static_Array_Bounds;
8928 ----------------
8929 -- Has_Stream --
8930 ----------------
8932 function Has_Stream (T : Entity_Id) return Boolean is
8933 E : Entity_Id;
8935 begin
8936 if No (T) then
8937 return False;
8939 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
8940 return True;
8942 elsif Is_Array_Type (T) then
8943 return Has_Stream (Component_Type (T));
8945 elsif Is_Record_Type (T) then
8946 E := First_Component (T);
8947 while Present (E) loop
8948 if Has_Stream (Etype (E)) then
8949 return True;
8950 else
8951 Next_Component (E);
8952 end if;
8953 end loop;
8955 return False;
8957 elsif Is_Private_Type (T) then
8958 return Has_Stream (Underlying_Type (T));
8960 else
8961 return False;
8962 end if;
8963 end Has_Stream;
8965 ----------------
8966 -- Has_Suffix --
8967 ----------------
8969 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
8970 begin
8971 Get_Name_String (Chars (E));
8972 return Name_Buffer (Name_Len) = Suffix;
8973 end Has_Suffix;
8975 ----------------
8976 -- Add_Suffix --
8977 ----------------
8979 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
8980 begin
8981 Get_Name_String (Chars (E));
8982 Add_Char_To_Name_Buffer (Suffix);
8983 return Name_Find;
8984 end Add_Suffix;
8986 -------------------
8987 -- Remove_Suffix --
8988 -------------------
8990 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
8991 begin
8992 pragma Assert (Has_Suffix (E, Suffix));
8993 Get_Name_String (Chars (E));
8994 Name_Len := Name_Len - 1;
8995 return Name_Find;
8996 end Remove_Suffix;
8998 --------------------------
8999 -- Has_Tagged_Component --
9000 --------------------------
9002 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
9003 Comp : Entity_Id;
9005 begin
9006 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
9007 return Has_Tagged_Component (Underlying_Type (Typ));
9009 elsif Is_Array_Type (Typ) then
9010 return Has_Tagged_Component (Component_Type (Typ));
9012 elsif Is_Tagged_Type (Typ) then
9013 return True;
9015 elsif Is_Record_Type (Typ) then
9016 Comp := First_Component (Typ);
9017 while Present (Comp) loop
9018 if Has_Tagged_Component (Etype (Comp)) then
9019 return True;
9020 end if;
9022 Next_Component (Comp);
9023 end loop;
9025 return False;
9027 else
9028 return False;
9029 end if;
9030 end Has_Tagged_Component;
9032 ----------------------------
9033 -- Has_Volatile_Component --
9034 ----------------------------
9036 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
9037 Comp : Entity_Id;
9039 begin
9040 if Has_Volatile_Components (Typ) then
9041 return True;
9043 elsif Is_Array_Type (Typ) then
9044 return Is_Volatile (Component_Type (Typ));
9046 elsif Is_Record_Type (Typ) then
9047 Comp := First_Component (Typ);
9048 while Present (Comp) loop
9049 if Is_Volatile_Object (Comp) then
9050 return True;
9051 end if;
9053 Comp := Next_Component (Comp);
9054 end loop;
9055 end if;
9057 return False;
9058 end Has_Volatile_Component;
9060 -------------------------
9061 -- Implementation_Kind --
9062 -------------------------
9064 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
9065 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
9066 Arg : Node_Id;
9067 begin
9068 pragma Assert (Present (Impl_Prag));
9069 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
9070 return Chars (Get_Pragma_Arg (Arg));
9071 end Implementation_Kind;
9073 --------------------------
9074 -- Implements_Interface --
9075 --------------------------
9077 function Implements_Interface
9078 (Typ_Ent : Entity_Id;
9079 Iface_Ent : Entity_Id;
9080 Exclude_Parents : Boolean := False) return Boolean
9082 Ifaces_List : Elist_Id;
9083 Elmt : Elmt_Id;
9084 Iface : Entity_Id := Base_Type (Iface_Ent);
9085 Typ : Entity_Id := Base_Type (Typ_Ent);
9087 begin
9088 if Is_Class_Wide_Type (Typ) then
9089 Typ := Root_Type (Typ);
9090 end if;
9092 if not Has_Interfaces (Typ) then
9093 return False;
9094 end if;
9096 if Is_Class_Wide_Type (Iface) then
9097 Iface := Root_Type (Iface);
9098 end if;
9100 Collect_Interfaces (Typ, Ifaces_List);
9102 Elmt := First_Elmt (Ifaces_List);
9103 while Present (Elmt) loop
9104 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
9105 and then Exclude_Parents
9106 then
9107 null;
9109 elsif Node (Elmt) = Iface then
9110 return True;
9111 end if;
9113 Next_Elmt (Elmt);
9114 end loop;
9116 return False;
9117 end Implements_Interface;
9119 ------------------------------------
9120 -- In_Assertion_Expression_Pragma --
9121 ------------------------------------
9123 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
9124 Par : Node_Id;
9125 Prag : Node_Id := Empty;
9127 begin
9128 -- Climb the parent chain looking for an enclosing pragma
9130 Par := N;
9131 while Present (Par) loop
9132 if Nkind (Par) = N_Pragma then
9133 Prag := Par;
9134 exit;
9136 -- Precondition-like pragmas are expanded into if statements, check
9137 -- the original node instead.
9139 elsif Nkind (Original_Node (Par)) = N_Pragma then
9140 Prag := Original_Node (Par);
9141 exit;
9143 -- The expansion of attribute 'Old generates a constant to capture
9144 -- the result of the prefix. If the parent traversal reaches
9145 -- one of these constants, then the node technically came from a
9146 -- postcondition-like pragma. Note that the Ekind is not tested here
9147 -- because N may be the expression of an object declaration which is
9148 -- currently being analyzed. Such objects carry Ekind of E_Void.
9150 elsif Nkind (Par) = N_Object_Declaration
9151 and then Constant_Present (Par)
9152 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
9153 then
9154 return True;
9156 -- Prevent the search from going too far
9158 elsif Is_Body_Or_Package_Declaration (Par) then
9159 return False;
9160 end if;
9162 Par := Parent (Par);
9163 end loop;
9165 return
9166 Present (Prag)
9167 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
9168 end In_Assertion_Expression_Pragma;
9170 -----------------
9171 -- In_Instance --
9172 -----------------
9174 function In_Instance return Boolean is
9175 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9176 S : Entity_Id;
9178 begin
9179 S := Current_Scope;
9180 while Present (S) and then S /= Standard_Standard loop
9181 if Ekind_In (S, E_Function, E_Package, E_Procedure)
9182 and then Is_Generic_Instance (S)
9183 then
9184 -- A child instance is always compiled in the context of a parent
9185 -- instance. Nevertheless, the actuals are not analyzed in an
9186 -- instance context. We detect this case by examining the current
9187 -- compilation unit, which must be a child instance, and checking
9188 -- that it is not currently on the scope stack.
9190 if Is_Child_Unit (Curr_Unit)
9191 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
9192 N_Package_Instantiation
9193 and then not In_Open_Scopes (Curr_Unit)
9194 then
9195 return False;
9196 else
9197 return True;
9198 end if;
9199 end if;
9201 S := Scope (S);
9202 end loop;
9204 return False;
9205 end In_Instance;
9207 ----------------------
9208 -- In_Instance_Body --
9209 ----------------------
9211 function In_Instance_Body return Boolean is
9212 S : Entity_Id;
9214 begin
9215 S := Current_Scope;
9216 while Present (S) and then S /= Standard_Standard loop
9217 if Ekind_In (S, E_Function, E_Procedure)
9218 and then Is_Generic_Instance (S)
9219 then
9220 return True;
9222 elsif Ekind (S) = E_Package
9223 and then In_Package_Body (S)
9224 and then Is_Generic_Instance (S)
9225 then
9226 return True;
9227 end if;
9229 S := Scope (S);
9230 end loop;
9232 return False;
9233 end In_Instance_Body;
9235 -----------------------------
9236 -- In_Instance_Not_Visible --
9237 -----------------------------
9239 function In_Instance_Not_Visible return Boolean is
9240 S : Entity_Id;
9242 begin
9243 S := Current_Scope;
9244 while Present (S) and then S /= Standard_Standard loop
9245 if Ekind_In (S, E_Function, E_Procedure)
9246 and then Is_Generic_Instance (S)
9247 then
9248 return True;
9250 elsif Ekind (S) = E_Package
9251 and then (In_Package_Body (S) or else In_Private_Part (S))
9252 and then Is_Generic_Instance (S)
9253 then
9254 return True;
9255 end if;
9257 S := Scope (S);
9258 end loop;
9260 return False;
9261 end In_Instance_Not_Visible;
9263 ------------------------------
9264 -- In_Instance_Visible_Part --
9265 ------------------------------
9267 function In_Instance_Visible_Part return Boolean is
9268 S : Entity_Id;
9270 begin
9271 S := Current_Scope;
9272 while Present (S) and then S /= Standard_Standard loop
9273 if Ekind (S) = E_Package
9274 and then Is_Generic_Instance (S)
9275 and then not In_Package_Body (S)
9276 and then not In_Private_Part (S)
9277 then
9278 return True;
9279 end if;
9281 S := Scope (S);
9282 end loop;
9284 return False;
9285 end In_Instance_Visible_Part;
9287 ---------------------
9288 -- In_Package_Body --
9289 ---------------------
9291 function In_Package_Body return Boolean is
9292 S : Entity_Id;
9294 begin
9295 S := Current_Scope;
9296 while Present (S) and then S /= Standard_Standard loop
9297 if Ekind (S) = E_Package and then In_Package_Body (S) then
9298 return True;
9299 else
9300 S := Scope (S);
9301 end if;
9302 end loop;
9304 return False;
9305 end In_Package_Body;
9307 --------------------------------
9308 -- In_Parameter_Specification --
9309 --------------------------------
9311 function In_Parameter_Specification (N : Node_Id) return Boolean is
9312 PN : Node_Id;
9314 begin
9315 PN := Parent (N);
9316 while Present (PN) loop
9317 if Nkind (PN) = N_Parameter_Specification then
9318 return True;
9319 end if;
9321 PN := Parent (PN);
9322 end loop;
9324 return False;
9325 end In_Parameter_Specification;
9327 --------------------------
9328 -- In_Pragma_Expression --
9329 --------------------------
9331 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
9332 P : Node_Id;
9333 begin
9334 P := Parent (N);
9335 loop
9336 if No (P) then
9337 return False;
9338 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
9339 return True;
9340 else
9341 P := Parent (P);
9342 end if;
9343 end loop;
9344 end In_Pragma_Expression;
9346 -------------------------------------
9347 -- In_Reverse_Storage_Order_Object --
9348 -------------------------------------
9350 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
9351 Pref : Node_Id;
9352 Btyp : Entity_Id := Empty;
9354 begin
9355 -- Climb up indexed components
9357 Pref := N;
9358 loop
9359 case Nkind (Pref) is
9360 when N_Selected_Component =>
9361 Pref := Prefix (Pref);
9362 exit;
9364 when N_Indexed_Component =>
9365 Pref := Prefix (Pref);
9367 when others =>
9368 Pref := Empty;
9369 exit;
9370 end case;
9371 end loop;
9373 if Present (Pref) then
9374 Btyp := Base_Type (Etype (Pref));
9375 end if;
9377 return Present (Btyp)
9378 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
9379 and then Reverse_Storage_Order (Btyp);
9380 end In_Reverse_Storage_Order_Object;
9382 --------------------------------------
9383 -- In_Subprogram_Or_Concurrent_Unit --
9384 --------------------------------------
9386 function In_Subprogram_Or_Concurrent_Unit return Boolean is
9387 E : Entity_Id;
9388 K : Entity_Kind;
9390 begin
9391 -- Use scope chain to check successively outer scopes
9393 E := Current_Scope;
9394 loop
9395 K := Ekind (E);
9397 if K in Subprogram_Kind
9398 or else K in Concurrent_Kind
9399 or else K in Generic_Subprogram_Kind
9400 then
9401 return True;
9403 elsif E = Standard_Standard then
9404 return False;
9405 end if;
9407 E := Scope (E);
9408 end loop;
9409 end In_Subprogram_Or_Concurrent_Unit;
9411 ---------------------
9412 -- In_Visible_Part --
9413 ---------------------
9415 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
9416 begin
9417 return Is_Package_Or_Generic_Package (Scope_Id)
9418 and then In_Open_Scopes (Scope_Id)
9419 and then not In_Package_Body (Scope_Id)
9420 and then not In_Private_Part (Scope_Id);
9421 end In_Visible_Part;
9423 --------------------------------
9424 -- Incomplete_Or_Partial_View --
9425 --------------------------------
9427 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
9428 function Inspect_Decls
9429 (Decls : List_Id;
9430 Taft : Boolean := False) return Entity_Id;
9431 -- Check whether a declarative region contains the incomplete or partial
9432 -- view of Id.
9434 -------------------
9435 -- Inspect_Decls --
9436 -------------------
9438 function Inspect_Decls
9439 (Decls : List_Id;
9440 Taft : Boolean := False) return Entity_Id
9442 Decl : Node_Id;
9443 Match : Node_Id;
9445 begin
9446 Decl := First (Decls);
9447 while Present (Decl) loop
9448 Match := Empty;
9450 if Taft then
9451 if Nkind (Decl) = N_Incomplete_Type_Declaration then
9452 Match := Defining_Identifier (Decl);
9453 end if;
9455 else
9456 if Nkind_In (Decl, N_Private_Extension_Declaration,
9457 N_Private_Type_Declaration)
9458 then
9459 Match := Defining_Identifier (Decl);
9460 end if;
9461 end if;
9463 if Present (Match)
9464 and then Present (Full_View (Match))
9465 and then Full_View (Match) = Id
9466 then
9467 return Match;
9468 end if;
9470 Next (Decl);
9471 end loop;
9473 return Empty;
9474 end Inspect_Decls;
9476 -- Local variables
9478 Prev : Entity_Id;
9480 -- Start of processing for Incomplete_Or_Partial_View
9482 begin
9483 -- Deferred constant or incomplete type case
9485 Prev := Current_Entity_In_Scope (Id);
9487 if Present (Prev)
9488 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
9489 and then Present (Full_View (Prev))
9490 and then Full_View (Prev) = Id
9491 then
9492 return Prev;
9493 end if;
9495 -- Private or Taft amendment type case
9497 declare
9498 Pkg : constant Entity_Id := Scope (Id);
9499 Pkg_Decl : Node_Id := Pkg;
9501 begin
9502 if Ekind (Pkg) = E_Package then
9503 while Nkind (Pkg_Decl) /= N_Package_Specification loop
9504 Pkg_Decl := Parent (Pkg_Decl);
9505 end loop;
9507 -- It is knows that Typ has a private view, look for it in the
9508 -- visible declarations of the enclosing scope. A special case
9509 -- of this is when the two views have been exchanged - the full
9510 -- appears earlier than the private.
9512 if Has_Private_Declaration (Id) then
9513 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
9515 -- Exchanged view case, look in the private declarations
9517 if No (Prev) then
9518 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
9519 end if;
9521 return Prev;
9523 -- Otherwise if this is the package body, then Typ is a potential
9524 -- Taft amendment type. The incomplete view should be located in
9525 -- the private declarations of the enclosing scope.
9527 elsif In_Package_Body (Pkg) then
9528 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
9529 end if;
9530 end if;
9531 end;
9533 -- The type has no incomplete or private view
9535 return Empty;
9536 end Incomplete_Or_Partial_View;
9538 -----------------------------------------
9539 -- Inherit_Default_Init_Cond_Procedure --
9540 -----------------------------------------
9542 procedure Inherit_Default_Init_Cond_Procedure (Typ : Entity_Id) is
9543 Par_Typ : constant Entity_Id := Etype (Typ);
9545 begin
9546 -- A derived type inherits the default initial condition procedure of
9547 -- its parent type.
9549 if No (Default_Init_Cond_Procedure (Typ)) then
9550 Set_Default_Init_Cond_Procedure
9551 (Typ, Default_Init_Cond_Procedure (Par_Typ));
9552 end if;
9553 end Inherit_Default_Init_Cond_Procedure;
9555 ----------------------------
9556 -- Inherit_Rep_Item_Chain --
9557 ----------------------------
9559 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
9560 From_Item : constant Node_Id := First_Rep_Item (From_Typ);
9561 Item : Node_Id := Empty;
9562 Last_Item : Node_Id := Empty;
9564 begin
9565 -- Reach the end of the destination type's chain (if any) and capture
9566 -- the last item.
9568 Item := First_Rep_Item (Typ);
9569 while Present (Item) loop
9571 -- Do not inherit a chain that has been inherited already
9573 if Item = From_Item then
9574 return;
9575 end if;
9577 Last_Item := Item;
9578 Item := Next_Rep_Item (Item);
9579 end loop;
9581 -- When the destination type has a rep item chain, the chain of the
9582 -- source type is appended to it.
9584 if Present (Last_Item) then
9585 Set_Next_Rep_Item (Last_Item, From_Item);
9587 -- Otherwise the destination type directly inherits the rep item chain
9588 -- of the source type (if any).
9590 else
9591 Set_First_Rep_Item (Typ, From_Item);
9592 end if;
9593 end Inherit_Rep_Item_Chain;
9595 ---------------------------------
9596 -- Inherit_Subprogram_Contract --
9597 ---------------------------------
9599 procedure Inherit_Subprogram_Contract
9600 (Subp : Entity_Id;
9601 From_Subp : Entity_Id)
9603 procedure Inherit_Pragma (Prag_Id : Pragma_Id);
9604 -- Propagate a pragma denoted by Prag_Id from From_Subp's contract to
9605 -- Subp's contract.
9607 --------------------
9608 -- Inherit_Pragma --
9609 --------------------
9611 procedure Inherit_Pragma (Prag_Id : Pragma_Id) is
9612 Prag : constant Node_Id := Get_Pragma (From_Subp, Prag_Id);
9613 New_Prag : Node_Id;
9615 begin
9616 -- A pragma cannot be part of more than one First_Pragma/Next_Pragma
9617 -- chains, therefore the node must be replicated. The new pragma is
9618 -- flagged is inherited for distrinction purposes.
9620 if Present (Prag) then
9621 New_Prag := New_Copy_Tree (Prag);
9622 Set_Is_Inherited (New_Prag);
9624 Add_Contract_Item (New_Prag, Subp);
9625 end if;
9626 end Inherit_Pragma;
9628 -- Start of processing for Inherit_Subprogram_Contract
9630 begin
9631 -- Inheritance is carried out only when both entities are subprograms
9632 -- with contracts.
9634 if Is_Subprogram_Or_Generic_Subprogram (Subp)
9635 and then Is_Subprogram_Or_Generic_Subprogram (From_Subp)
9636 and then Present (Contract (Subp))
9637 and then Present (Contract (From_Subp))
9638 then
9639 Inherit_Pragma (Pragma_Extensions_Visible);
9640 end if;
9641 end Inherit_Subprogram_Contract;
9643 ---------------------------------
9644 -- Insert_Explicit_Dereference --
9645 ---------------------------------
9647 procedure Insert_Explicit_Dereference (N : Node_Id) is
9648 New_Prefix : constant Node_Id := Relocate_Node (N);
9649 Ent : Entity_Id := Empty;
9650 Pref : Node_Id;
9651 I : Interp_Index;
9652 It : Interp;
9653 T : Entity_Id;
9655 begin
9656 Save_Interps (N, New_Prefix);
9658 Rewrite (N,
9659 Make_Explicit_Dereference (Sloc (Parent (N)),
9660 Prefix => New_Prefix));
9662 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
9664 if Is_Overloaded (New_Prefix) then
9666 -- The dereference is also overloaded, and its interpretations are
9667 -- the designated types of the interpretations of the original node.
9669 Set_Etype (N, Any_Type);
9671 Get_First_Interp (New_Prefix, I, It);
9672 while Present (It.Nam) loop
9673 T := It.Typ;
9675 if Is_Access_Type (T) then
9676 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
9677 end if;
9679 Get_Next_Interp (I, It);
9680 end loop;
9682 End_Interp_List;
9684 else
9685 -- Prefix is unambiguous: mark the original prefix (which might
9686 -- Come_From_Source) as a reference, since the new (relocated) one
9687 -- won't be taken into account.
9689 if Is_Entity_Name (New_Prefix) then
9690 Ent := Entity (New_Prefix);
9691 Pref := New_Prefix;
9693 -- For a retrieval of a subcomponent of some composite object,
9694 -- retrieve the ultimate entity if there is one.
9696 elsif Nkind_In (New_Prefix, N_Selected_Component,
9697 N_Indexed_Component)
9698 then
9699 Pref := Prefix (New_Prefix);
9700 while Present (Pref)
9701 and then Nkind_In (Pref, N_Selected_Component,
9702 N_Indexed_Component)
9703 loop
9704 Pref := Prefix (Pref);
9705 end loop;
9707 if Present (Pref) and then Is_Entity_Name (Pref) then
9708 Ent := Entity (Pref);
9709 end if;
9710 end if;
9712 -- Place the reference on the entity node
9714 if Present (Ent) then
9715 Generate_Reference (Ent, Pref);
9716 end if;
9717 end if;
9718 end Insert_Explicit_Dereference;
9720 ------------------------------------------
9721 -- Inspect_Deferred_Constant_Completion --
9722 ------------------------------------------
9724 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
9725 Decl : Node_Id;
9727 begin
9728 Decl := First (Decls);
9729 while Present (Decl) loop
9731 -- Deferred constant signature
9733 if Nkind (Decl) = N_Object_Declaration
9734 and then Constant_Present (Decl)
9735 and then No (Expression (Decl))
9737 -- No need to check internally generated constants
9739 and then Comes_From_Source (Decl)
9741 -- The constant is not completed. A full object declaration or a
9742 -- pragma Import complete a deferred constant.
9744 and then not Has_Completion (Defining_Identifier (Decl))
9745 then
9746 Error_Msg_N
9747 ("constant declaration requires initialization expression",
9748 Defining_Identifier (Decl));
9749 end if;
9751 Decl := Next (Decl);
9752 end loop;
9753 end Inspect_Deferred_Constant_Completion;
9755 -----------------------------
9756 -- Is_Actual_Out_Parameter --
9757 -----------------------------
9759 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
9760 Formal : Entity_Id;
9761 Call : Node_Id;
9762 begin
9763 Find_Actual (N, Formal, Call);
9764 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
9765 end Is_Actual_Out_Parameter;
9767 -------------------------
9768 -- Is_Actual_Parameter --
9769 -------------------------
9771 function Is_Actual_Parameter (N : Node_Id) return Boolean is
9772 PK : constant Node_Kind := Nkind (Parent (N));
9774 begin
9775 case PK is
9776 when N_Parameter_Association =>
9777 return N = Explicit_Actual_Parameter (Parent (N));
9779 when N_Subprogram_Call =>
9780 return Is_List_Member (N)
9781 and then
9782 List_Containing (N) = Parameter_Associations (Parent (N));
9784 when others =>
9785 return False;
9786 end case;
9787 end Is_Actual_Parameter;
9789 --------------------------------
9790 -- Is_Actual_Tagged_Parameter --
9791 --------------------------------
9793 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
9794 Formal : Entity_Id;
9795 Call : Node_Id;
9796 begin
9797 Find_Actual (N, Formal, Call);
9798 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
9799 end Is_Actual_Tagged_Parameter;
9801 ---------------------
9802 -- Is_Aliased_View --
9803 ---------------------
9805 function Is_Aliased_View (Obj : Node_Id) return Boolean is
9806 E : Entity_Id;
9808 begin
9809 if Is_Entity_Name (Obj) then
9810 E := Entity (Obj);
9812 return
9813 (Is_Object (E)
9814 and then
9815 (Is_Aliased (E)
9816 or else (Present (Renamed_Object (E))
9817 and then Is_Aliased_View (Renamed_Object (E)))))
9819 or else ((Is_Formal (E)
9820 or else Ekind_In (E, E_Generic_In_Out_Parameter,
9821 E_Generic_In_Parameter))
9822 and then Is_Tagged_Type (Etype (E)))
9824 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
9826 -- Current instance of type, either directly or as rewritten
9827 -- reference to the current object.
9829 or else (Is_Entity_Name (Original_Node (Obj))
9830 and then Present (Entity (Original_Node (Obj)))
9831 and then Is_Type (Entity (Original_Node (Obj))))
9833 or else (Is_Type (E) and then E = Current_Scope)
9835 or else (Is_Incomplete_Or_Private_Type (E)
9836 and then Full_View (E) = Current_Scope)
9838 -- Ada 2012 AI05-0053: the return object of an extended return
9839 -- statement is aliased if its type is immutably limited.
9841 or else (Is_Return_Object (E)
9842 and then Is_Limited_View (Etype (E)));
9844 elsif Nkind (Obj) = N_Selected_Component then
9845 return Is_Aliased (Entity (Selector_Name (Obj)));
9847 elsif Nkind (Obj) = N_Indexed_Component then
9848 return Has_Aliased_Components (Etype (Prefix (Obj)))
9849 or else
9850 (Is_Access_Type (Etype (Prefix (Obj)))
9851 and then Has_Aliased_Components
9852 (Designated_Type (Etype (Prefix (Obj)))));
9854 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
9855 return Is_Tagged_Type (Etype (Obj))
9856 and then Is_Aliased_View (Expression (Obj));
9858 elsif Nkind (Obj) = N_Explicit_Dereference then
9859 return Nkind (Original_Node (Obj)) /= N_Function_Call;
9861 else
9862 return False;
9863 end if;
9864 end Is_Aliased_View;
9866 -------------------------
9867 -- Is_Ancestor_Package --
9868 -------------------------
9870 function Is_Ancestor_Package
9871 (E1 : Entity_Id;
9872 E2 : Entity_Id) return Boolean
9874 Par : Entity_Id;
9876 begin
9877 Par := E2;
9878 while Present (Par) and then Par /= Standard_Standard loop
9879 if Par = E1 then
9880 return True;
9881 end if;
9883 Par := Scope (Par);
9884 end loop;
9886 return False;
9887 end Is_Ancestor_Package;
9889 ----------------------
9890 -- Is_Atomic_Object --
9891 ----------------------
9893 function Is_Atomic_Object (N : Node_Id) return Boolean is
9895 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
9896 -- Determines if given object has atomic components
9898 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
9899 -- If prefix is an implicit dereference, examine designated type
9901 ----------------------
9902 -- Is_Atomic_Prefix --
9903 ----------------------
9905 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
9906 begin
9907 if Is_Access_Type (Etype (N)) then
9908 return
9909 Has_Atomic_Components (Designated_Type (Etype (N)));
9910 else
9911 return Object_Has_Atomic_Components (N);
9912 end if;
9913 end Is_Atomic_Prefix;
9915 ----------------------------------
9916 -- Object_Has_Atomic_Components --
9917 ----------------------------------
9919 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
9920 begin
9921 if Has_Atomic_Components (Etype (N))
9922 or else Is_Atomic (Etype (N))
9923 then
9924 return True;
9926 elsif Is_Entity_Name (N)
9927 and then (Has_Atomic_Components (Entity (N))
9928 or else Is_Atomic (Entity (N)))
9929 then
9930 return True;
9932 elsif Nkind (N) = N_Selected_Component
9933 and then Is_Atomic (Entity (Selector_Name (N)))
9934 then
9935 return True;
9937 elsif Nkind (N) = N_Indexed_Component
9938 or else Nkind (N) = N_Selected_Component
9939 then
9940 return Is_Atomic_Prefix (Prefix (N));
9942 else
9943 return False;
9944 end if;
9945 end Object_Has_Atomic_Components;
9947 -- Start of processing for Is_Atomic_Object
9949 begin
9950 -- Predicate is not relevant to subprograms
9952 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
9953 return False;
9955 elsif Is_Atomic (Etype (N))
9956 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
9957 then
9958 return True;
9960 elsif Nkind (N) = N_Selected_Component
9961 and then Is_Atomic (Entity (Selector_Name (N)))
9962 then
9963 return True;
9965 elsif Nkind (N) = N_Indexed_Component
9966 or else Nkind (N) = N_Selected_Component
9967 then
9968 return Is_Atomic_Prefix (Prefix (N));
9970 else
9971 return False;
9972 end if;
9973 end Is_Atomic_Object;
9975 -------------------------
9976 -- Is_Attribute_Result --
9977 -------------------------
9979 function Is_Attribute_Result (N : Node_Id) return Boolean is
9980 begin
9981 return Nkind (N) = N_Attribute_Reference
9982 and then Attribute_Name (N) = Name_Result;
9983 end Is_Attribute_Result;
9985 ------------------------------------
9986 -- Is_Body_Or_Package_Declaration --
9987 ------------------------------------
9989 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
9990 begin
9991 return Nkind_In (N, N_Entry_Body,
9992 N_Package_Body,
9993 N_Package_Declaration,
9994 N_Protected_Body,
9995 N_Subprogram_Body,
9996 N_Task_Body);
9997 end Is_Body_Or_Package_Declaration;
9999 -----------------------
10000 -- Is_Bounded_String --
10001 -----------------------
10003 function Is_Bounded_String (T : Entity_Id) return Boolean is
10004 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
10006 begin
10007 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
10008 -- Super_String, or one of the [Wide_]Wide_ versions. This will
10009 -- be True for all the Bounded_String types in instances of the
10010 -- Generic_Bounded_Length generics, and for types derived from those.
10012 return Present (Under)
10013 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
10014 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
10015 Is_RTE (Root_Type (Under), RO_WW_Super_String));
10016 end Is_Bounded_String;
10018 -------------------------
10019 -- Is_Child_Or_Sibling --
10020 -------------------------
10022 function Is_Child_Or_Sibling
10023 (Pack_1 : Entity_Id;
10024 Pack_2 : Entity_Id) return Boolean
10026 function Distance_From_Standard (Pack : Entity_Id) return Nat;
10027 -- Given an arbitrary package, return the number of "climbs" necessary
10028 -- to reach scope Standard_Standard.
10030 procedure Equalize_Depths
10031 (Pack : in out Entity_Id;
10032 Depth : in out Nat;
10033 Depth_To_Reach : Nat);
10034 -- Given an arbitrary package, its depth and a target depth to reach,
10035 -- climb the scope chain until the said depth is reached. The pointer
10036 -- to the package and its depth a modified during the climb.
10038 ----------------------------
10039 -- Distance_From_Standard --
10040 ----------------------------
10042 function Distance_From_Standard (Pack : Entity_Id) return Nat is
10043 Dist : Nat;
10044 Scop : Entity_Id;
10046 begin
10047 Dist := 0;
10048 Scop := Pack;
10049 while Present (Scop) and then Scop /= Standard_Standard loop
10050 Dist := Dist + 1;
10051 Scop := Scope (Scop);
10052 end loop;
10054 return Dist;
10055 end Distance_From_Standard;
10057 ---------------------
10058 -- Equalize_Depths --
10059 ---------------------
10061 procedure Equalize_Depths
10062 (Pack : in out Entity_Id;
10063 Depth : in out Nat;
10064 Depth_To_Reach : Nat)
10066 begin
10067 -- The package must be at a greater or equal depth
10069 if Depth < Depth_To_Reach then
10070 raise Program_Error;
10071 end if;
10073 -- Climb the scope chain until the desired depth is reached
10075 while Present (Pack) and then Depth /= Depth_To_Reach loop
10076 Pack := Scope (Pack);
10077 Depth := Depth - 1;
10078 end loop;
10079 end Equalize_Depths;
10081 -- Local variables
10083 P_1 : Entity_Id := Pack_1;
10084 P_1_Child : Boolean := False;
10085 P_1_Depth : Nat := Distance_From_Standard (P_1);
10086 P_2 : Entity_Id := Pack_2;
10087 P_2_Child : Boolean := False;
10088 P_2_Depth : Nat := Distance_From_Standard (P_2);
10090 -- Start of processing for Is_Child_Or_Sibling
10092 begin
10093 pragma Assert
10094 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
10096 -- Both packages denote the same entity, therefore they cannot be
10097 -- children or siblings.
10099 if P_1 = P_2 then
10100 return False;
10102 -- One of the packages is at a deeper level than the other. Note that
10103 -- both may still come from differen hierarchies.
10105 -- (root) P_2
10106 -- / \ :
10107 -- X P_2 or X
10108 -- : :
10109 -- P_1 P_1
10111 elsif P_1_Depth > P_2_Depth then
10112 Equalize_Depths
10113 (Pack => P_1,
10114 Depth => P_1_Depth,
10115 Depth_To_Reach => P_2_Depth);
10116 P_1_Child := True;
10118 -- (root) P_1
10119 -- / \ :
10120 -- P_1 X or X
10121 -- : :
10122 -- P_2 P_2
10124 elsif P_2_Depth > P_1_Depth then
10125 Equalize_Depths
10126 (Pack => P_2,
10127 Depth => P_2_Depth,
10128 Depth_To_Reach => P_1_Depth);
10129 P_2_Child := True;
10130 end if;
10132 -- At this stage the package pointers have been elevated to the same
10133 -- depth. If the related entities are the same, then one package is a
10134 -- potential child of the other:
10136 -- P_1
10137 -- :
10138 -- X became P_1 P_2 or vica versa
10139 -- :
10140 -- P_2
10142 if P_1 = P_2 then
10143 if P_1_Child then
10144 return Is_Child_Unit (Pack_1);
10146 else pragma Assert (P_2_Child);
10147 return Is_Child_Unit (Pack_2);
10148 end if;
10150 -- The packages may come from the same package chain or from entirely
10151 -- different hierarcies. To determine this, climb the scope stack until
10152 -- a common root is found.
10154 -- (root) (root 1) (root 2)
10155 -- / \ | |
10156 -- P_1 P_2 P_1 P_2
10158 else
10159 while Present (P_1) and then Present (P_2) loop
10161 -- The two packages may be siblings
10163 if P_1 = P_2 then
10164 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
10165 end if;
10167 P_1 := Scope (P_1);
10168 P_2 := Scope (P_2);
10169 end loop;
10170 end if;
10172 return False;
10173 end Is_Child_Or_Sibling;
10175 -----------------------------
10176 -- Is_Concurrent_Interface --
10177 -----------------------------
10179 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
10180 begin
10181 return Is_Interface (T)
10182 and then
10183 (Is_Protected_Interface (T)
10184 or else Is_Synchronized_Interface (T)
10185 or else Is_Task_Interface (T));
10186 end Is_Concurrent_Interface;
10188 ---------------------------
10189 -- Is_Container_Element --
10190 ---------------------------
10192 function Is_Container_Element (Exp : Node_Id) return Boolean is
10193 Loc : constant Source_Ptr := Sloc (Exp);
10194 Pref : constant Node_Id := Prefix (Exp);
10196 Call : Node_Id;
10197 -- Call to an indexing aspect
10199 Cont_Typ : Entity_Id;
10200 -- The type of the container being accessed
10202 Elem_Typ : Entity_Id;
10203 -- Its element type
10205 Indexing : Entity_Id;
10206 Is_Const : Boolean;
10207 -- Indicates that constant indexing is used, and the element is thus
10208 -- a constant.
10210 Ref_Typ : Entity_Id;
10211 -- The reference type returned by the indexing operation
10213 begin
10214 -- If C is a container, in a context that imposes the element type of
10215 -- that container, the indexing notation C (X) is rewritten as:
10217 -- Indexing (C, X).Discr.all
10219 -- where Indexing is one of the indexing aspects of the container.
10220 -- If the context does not require a reference, the construct can be
10221 -- rewritten as
10223 -- Element (C, X)
10225 -- First, verify that the construct has the proper form
10227 if not Expander_Active then
10228 return False;
10230 elsif Nkind (Pref) /= N_Selected_Component then
10231 return False;
10233 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
10234 return False;
10236 else
10237 Call := Prefix (Pref);
10238 Ref_Typ := Etype (Call);
10239 end if;
10241 if not Has_Implicit_Dereference (Ref_Typ)
10242 or else No (First (Parameter_Associations (Call)))
10243 or else not Is_Entity_Name (Name (Call))
10244 then
10245 return False;
10246 end if;
10248 -- Retrieve type of container object, and its iterator aspects
10250 Cont_Typ := Etype (First (Parameter_Associations (Call)));
10251 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
10252 Is_Const := False;
10254 if No (Indexing) then
10256 -- Container should have at least one indexing operation
10258 return False;
10260 elsif Entity (Name (Call)) /= Entity (Indexing) then
10262 -- This may be a variable indexing operation
10264 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
10266 if No (Indexing)
10267 or else Entity (Name (Call)) /= Entity (Indexing)
10268 then
10269 return False;
10270 end if;
10272 else
10273 Is_Const := True;
10274 end if;
10276 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
10278 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
10279 return False;
10280 end if;
10282 -- Check that the expression is not the target of an assignment, in
10283 -- which case the rewriting is not possible.
10285 if not Is_Const then
10286 declare
10287 Par : Node_Id;
10289 begin
10290 Par := Exp;
10291 while Present (Par)
10292 loop
10293 if Nkind (Parent (Par)) = N_Assignment_Statement
10294 and then Par = Name (Parent (Par))
10295 then
10296 return False;
10298 -- A renaming produces a reference, and the transformation
10299 -- does not apply.
10301 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
10302 return False;
10304 elsif Nkind_In
10305 (Nkind (Parent (Par)), N_Function_Call,
10306 N_Procedure_Call_Statement,
10307 N_Entry_Call_Statement)
10308 then
10309 -- Check that the element is not part of an actual for an
10310 -- in-out parameter.
10312 declare
10313 F : Entity_Id;
10314 A : Node_Id;
10316 begin
10317 F := First_Formal (Entity (Name (Parent (Par))));
10318 A := First (Parameter_Associations (Parent (Par)));
10319 while Present (F) loop
10320 if A = Par and then Ekind (F) /= E_In_Parameter then
10321 return False;
10322 end if;
10324 Next_Formal (F);
10325 Next (A);
10326 end loop;
10327 end;
10329 -- E_In_Parameter in a call: element is not modified.
10331 exit;
10332 end if;
10334 Par := Parent (Par);
10335 end loop;
10336 end;
10337 end if;
10339 -- The expression has the proper form and the context requires the
10340 -- element type. Retrieve the Element function of the container and
10341 -- rewrite the construct as a call to it.
10343 declare
10344 Op : Elmt_Id;
10346 begin
10347 Op := First_Elmt (Primitive_Operations (Cont_Typ));
10348 while Present (Op) loop
10349 exit when Chars (Node (Op)) = Name_Element;
10350 Next_Elmt (Op);
10351 end loop;
10353 if No (Op) then
10354 return False;
10356 else
10357 Rewrite (Exp,
10358 Make_Function_Call (Loc,
10359 Name => New_Occurrence_Of (Node (Op), Loc),
10360 Parameter_Associations => Parameter_Associations (Call)));
10361 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
10362 return True;
10363 end if;
10364 end;
10365 end Is_Container_Element;
10367 -----------------------
10368 -- Is_Constant_Bound --
10369 -----------------------
10371 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
10372 begin
10373 if Compile_Time_Known_Value (Exp) then
10374 return True;
10376 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
10377 return Is_Constant_Object (Entity (Exp))
10378 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
10380 elsif Nkind (Exp) in N_Binary_Op then
10381 return Is_Constant_Bound (Left_Opnd (Exp))
10382 and then Is_Constant_Bound (Right_Opnd (Exp))
10383 and then Scope (Entity (Exp)) = Standard_Standard;
10385 else
10386 return False;
10387 end if;
10388 end Is_Constant_Bound;
10390 --------------------------------------
10391 -- Is_Controlling_Limited_Procedure --
10392 --------------------------------------
10394 function Is_Controlling_Limited_Procedure
10395 (Proc_Nam : Entity_Id) return Boolean
10397 Param_Typ : Entity_Id := Empty;
10399 begin
10400 if Ekind (Proc_Nam) = E_Procedure
10401 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
10402 then
10403 Param_Typ := Etype (Parameter_Type (First (
10404 Parameter_Specifications (Parent (Proc_Nam)))));
10406 -- In this case where an Itype was created, the procedure call has been
10407 -- rewritten.
10409 elsif Present (Associated_Node_For_Itype (Proc_Nam))
10410 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
10411 and then
10412 Present (Parameter_Associations
10413 (Associated_Node_For_Itype (Proc_Nam)))
10414 then
10415 Param_Typ :=
10416 Etype (First (Parameter_Associations
10417 (Associated_Node_For_Itype (Proc_Nam))));
10418 end if;
10420 if Present (Param_Typ) then
10421 return
10422 Is_Interface (Param_Typ)
10423 and then Is_Limited_Record (Param_Typ);
10424 end if;
10426 return False;
10427 end Is_Controlling_Limited_Procedure;
10429 -----------------------------
10430 -- Is_CPP_Constructor_Call --
10431 -----------------------------
10433 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
10434 begin
10435 return Nkind (N) = N_Function_Call
10436 and then Is_CPP_Class (Etype (Etype (N)))
10437 and then Is_Constructor (Entity (Name (N)))
10438 and then Is_Imported (Entity (Name (N)));
10439 end Is_CPP_Constructor_Call;
10441 -----------------
10442 -- Is_Delegate --
10443 -----------------
10445 function Is_Delegate (T : Entity_Id) return Boolean is
10446 Desig_Type : Entity_Id;
10448 begin
10449 if VM_Target /= CLI_Target then
10450 return False;
10451 end if;
10453 -- Access-to-subprograms are delegates in CIL
10455 if Ekind (T) = E_Access_Subprogram_Type then
10456 return True;
10457 end if;
10459 if not Is_Access_Type (T) then
10461 -- A delegate is a managed pointer. If no designated type is defined
10462 -- it means that it's not a delegate.
10464 return False;
10465 end if;
10467 Desig_Type := Etype (Directly_Designated_Type (T));
10469 if not Is_Tagged_Type (Desig_Type) then
10470 return False;
10471 end if;
10473 -- Test if the type is inherited from [mscorlib]System.Delegate
10475 while Etype (Desig_Type) /= Desig_Type loop
10476 if Chars (Scope (Desig_Type)) /= No_Name
10477 and then Is_Imported (Scope (Desig_Type))
10478 and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
10479 then
10480 return True;
10481 end if;
10483 Desig_Type := Etype (Desig_Type);
10484 end loop;
10486 return False;
10487 end Is_Delegate;
10489 ----------------------------------------------
10490 -- Is_Dependent_Component_Of_Mutable_Object --
10491 ----------------------------------------------
10493 function Is_Dependent_Component_Of_Mutable_Object
10494 (Object : Node_Id) return Boolean
10496 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
10497 -- Returns True if and only if Comp is declared within a variant part
10499 --------------------------------
10500 -- Is_Declared_Within_Variant --
10501 --------------------------------
10503 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
10504 Comp_Decl : constant Node_Id := Parent (Comp);
10505 Comp_List : constant Node_Id := Parent (Comp_Decl);
10506 begin
10507 return Nkind (Parent (Comp_List)) = N_Variant;
10508 end Is_Declared_Within_Variant;
10510 P : Node_Id;
10511 Prefix_Type : Entity_Id;
10512 P_Aliased : Boolean := False;
10513 Comp : Entity_Id;
10515 Deref : Node_Id := Object;
10516 -- Dereference node, in something like X.all.Y(2)
10518 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
10520 begin
10521 -- Find the dereference node if any
10523 while Nkind_In (Deref, N_Indexed_Component,
10524 N_Selected_Component,
10525 N_Slice)
10526 loop
10527 Deref := Prefix (Deref);
10528 end loop;
10530 -- Ada 2005: If we have a component or slice of a dereference,
10531 -- something like X.all.Y (2), and the type of X is access-to-constant,
10532 -- Is_Variable will return False, because it is indeed a constant
10533 -- view. But it might be a view of a variable object, so we want the
10534 -- following condition to be True in that case.
10536 if Is_Variable (Object)
10537 or else (Ada_Version >= Ada_2005
10538 and then Nkind (Deref) = N_Explicit_Dereference)
10539 then
10540 if Nkind (Object) = N_Selected_Component then
10541 P := Prefix (Object);
10542 Prefix_Type := Etype (P);
10544 if Is_Entity_Name (P) then
10545 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
10546 Prefix_Type := Base_Type (Prefix_Type);
10547 end if;
10549 if Is_Aliased (Entity (P)) then
10550 P_Aliased := True;
10551 end if;
10553 -- A discriminant check on a selected component may be expanded
10554 -- into a dereference when removing side-effects. Recover the
10555 -- original node and its type, which may be unconstrained.
10557 elsif Nkind (P) = N_Explicit_Dereference
10558 and then not (Comes_From_Source (P))
10559 then
10560 P := Original_Node (P);
10561 Prefix_Type := Etype (P);
10563 else
10564 -- Check for prefix being an aliased component???
10566 null;
10568 end if;
10570 -- A heap object is constrained by its initial value
10572 -- Ada 2005 (AI-363): Always assume the object could be mutable in
10573 -- the dereferenced case, since the access value might denote an
10574 -- unconstrained aliased object, whereas in Ada 95 the designated
10575 -- object is guaranteed to be constrained. A worst-case assumption
10576 -- has to apply in Ada 2005 because we can't tell at compile
10577 -- time whether the object is "constrained by its initial value"
10578 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
10579 -- rules (these rules are acknowledged to need fixing).
10581 if Ada_Version < Ada_2005 then
10582 if Is_Access_Type (Prefix_Type)
10583 or else Nkind (P) = N_Explicit_Dereference
10584 then
10585 return False;
10586 end if;
10588 else pragma Assert (Ada_Version >= Ada_2005);
10589 if Is_Access_Type (Prefix_Type) then
10591 -- If the access type is pool-specific, and there is no
10592 -- constrained partial view of the designated type, then the
10593 -- designated object is known to be constrained.
10595 if Ekind (Prefix_Type) = E_Access_Type
10596 and then not Object_Type_Has_Constrained_Partial_View
10597 (Typ => Designated_Type (Prefix_Type),
10598 Scop => Current_Scope)
10599 then
10600 return False;
10602 -- Otherwise (general access type, or there is a constrained
10603 -- partial view of the designated type), we need to check
10604 -- based on the designated type.
10606 else
10607 Prefix_Type := Designated_Type (Prefix_Type);
10608 end if;
10609 end if;
10610 end if;
10612 Comp :=
10613 Original_Record_Component (Entity (Selector_Name (Object)));
10615 -- As per AI-0017, the renaming is illegal in a generic body, even
10616 -- if the subtype is indefinite.
10618 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
10620 if not Is_Constrained (Prefix_Type)
10621 and then (not Is_Indefinite_Subtype (Prefix_Type)
10622 or else
10623 (Is_Generic_Type (Prefix_Type)
10624 and then Ekind (Current_Scope) = E_Generic_Package
10625 and then In_Package_Body (Current_Scope)))
10627 and then (Is_Declared_Within_Variant (Comp)
10628 or else Has_Discriminant_Dependent_Constraint (Comp))
10629 and then (not P_Aliased or else Ada_Version >= Ada_2005)
10630 then
10631 return True;
10633 -- If the prefix is of an access type at this point, then we want
10634 -- to return False, rather than calling this function recursively
10635 -- on the access object (which itself might be a discriminant-
10636 -- dependent component of some other object, but that isn't
10637 -- relevant to checking the object passed to us). This avoids
10638 -- issuing wrong errors when compiling with -gnatc, where there
10639 -- can be implicit dereferences that have not been expanded.
10641 elsif Is_Access_Type (Etype (Prefix (Object))) then
10642 return False;
10644 else
10645 return
10646 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10647 end if;
10649 elsif Nkind (Object) = N_Indexed_Component
10650 or else Nkind (Object) = N_Slice
10651 then
10652 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10654 -- A type conversion that Is_Variable is a view conversion:
10655 -- go back to the denoted object.
10657 elsif Nkind (Object) = N_Type_Conversion then
10658 return
10659 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
10660 end if;
10661 end if;
10663 return False;
10664 end Is_Dependent_Component_Of_Mutable_Object;
10666 ---------------------
10667 -- Is_Dereferenced --
10668 ---------------------
10670 function Is_Dereferenced (N : Node_Id) return Boolean is
10671 P : constant Node_Id := Parent (N);
10672 begin
10673 return Nkind_In (P, N_Selected_Component,
10674 N_Explicit_Dereference,
10675 N_Indexed_Component,
10676 N_Slice)
10677 and then Prefix (P) = N;
10678 end Is_Dereferenced;
10680 ----------------------
10681 -- Is_Descendent_Of --
10682 ----------------------
10684 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
10685 T : Entity_Id;
10686 Etyp : Entity_Id;
10688 begin
10689 pragma Assert (Nkind (T1) in N_Entity);
10690 pragma Assert (Nkind (T2) in N_Entity);
10692 T := Base_Type (T1);
10694 -- Immediate return if the types match
10696 if T = T2 then
10697 return True;
10699 -- Comment needed here ???
10701 elsif Ekind (T) = E_Class_Wide_Type then
10702 return Etype (T) = T2;
10704 -- All other cases
10706 else
10707 loop
10708 Etyp := Etype (T);
10710 -- Done if we found the type we are looking for
10712 if Etyp = T2 then
10713 return True;
10715 -- Done if no more derivations to check
10717 elsif T = T1
10718 or else T = Etyp
10719 then
10720 return False;
10722 -- Following test catches error cases resulting from prev errors
10724 elsif No (Etyp) then
10725 return False;
10727 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
10728 return False;
10730 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
10731 return False;
10732 end if;
10734 T := Base_Type (Etyp);
10735 end loop;
10736 end if;
10737 end Is_Descendent_Of;
10739 -----------------------------
10740 -- Is_Effectively_Volatile --
10741 -----------------------------
10743 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
10744 begin
10745 if Is_Type (Id) then
10747 -- An arbitrary type is effectively volatile when it is subject to
10748 -- pragma Atomic or Volatile.
10750 if Is_Volatile (Id) then
10751 return True;
10753 -- An array type is effectively volatile when it is subject to pragma
10754 -- Atomic_Components or Volatile_Components or its compolent type is
10755 -- effectively volatile.
10757 elsif Is_Array_Type (Id) then
10758 return
10759 Has_Volatile_Components (Id)
10760 or else
10761 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
10763 else
10764 return False;
10765 end if;
10767 -- Otherwise Id denotes an object
10769 else
10770 return
10771 Is_Volatile (Id)
10772 or else Has_Volatile_Components (Id)
10773 or else Is_Effectively_Volatile (Etype (Id));
10774 end if;
10775 end Is_Effectively_Volatile;
10777 ------------------------------------
10778 -- Is_Effectively_Volatile_Object --
10779 ------------------------------------
10781 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
10782 begin
10783 if Is_Entity_Name (N) then
10784 return Is_Effectively_Volatile (Entity (N));
10786 elsif Nkind (N) = N_Expanded_Name then
10787 return Is_Effectively_Volatile (Entity (N));
10789 elsif Nkind (N) = N_Indexed_Component then
10790 return Is_Effectively_Volatile_Object (Prefix (N));
10792 elsif Nkind (N) = N_Selected_Component then
10793 return
10794 Is_Effectively_Volatile_Object (Prefix (N))
10795 or else
10796 Is_Effectively_Volatile_Object (Selector_Name (N));
10798 else
10799 return False;
10800 end if;
10801 end Is_Effectively_Volatile_Object;
10803 ----------------------------
10804 -- Is_Expression_Function --
10805 ----------------------------
10807 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
10808 Decl : Node_Id;
10810 begin
10811 if Ekind (Subp) /= E_Function then
10812 return False;
10814 else
10815 Decl := Unit_Declaration_Node (Subp);
10816 return Nkind (Decl) = N_Subprogram_Declaration
10817 and then
10818 (Nkind (Original_Node (Decl)) = N_Expression_Function
10819 or else
10820 (Present (Corresponding_Body (Decl))
10821 and then
10822 Nkind (Original_Node
10823 (Unit_Declaration_Node
10824 (Corresponding_Body (Decl)))) =
10825 N_Expression_Function));
10826 end if;
10827 end Is_Expression_Function;
10829 -----------------------
10830 -- Is_EVF_Expression --
10831 -----------------------
10833 function Is_EVF_Expression (N : Node_Id) return Boolean is
10834 Orig_N : constant Node_Id := Original_Node (N);
10835 Alt : Node_Id;
10836 Expr : Node_Id;
10837 Id : Entity_Id;
10839 begin
10840 -- Detect a reference to a formal parameter of a specific tagged type
10841 -- whose related subprogram is subject to pragma Expresions_Visible with
10842 -- value "False".
10844 if Is_Entity_Name (N) and then Present (Entity (N)) then
10845 Id := Entity (N);
10847 return
10848 Is_Formal (Id)
10849 and then Is_Specific_Tagged_Type (Etype (Id))
10850 and then Extensions_Visible_Status (Id) =
10851 Extensions_Visible_False;
10853 -- A case expression is an EVF expression when it contains at least one
10854 -- EVF dependent_expression. Note that a case expression may have been
10855 -- expanded, hence the use of Original_Node.
10857 elsif Nkind (Orig_N) = N_Case_Expression then
10858 Alt := First (Alternatives (Orig_N));
10859 while Present (Alt) loop
10860 if Is_EVF_Expression (Expression (Alt)) then
10861 return True;
10862 end if;
10864 Next (Alt);
10865 end loop;
10867 -- An if expression is an EVF expression when it contains at least one
10868 -- EVF dependent_expression. Note that an if expression may have been
10869 -- expanded, hence the use of Original_Node.
10871 elsif Nkind (Orig_N) = N_If_Expression then
10872 Expr := Next (First (Expressions (Orig_N)));
10873 while Present (Expr) loop
10874 if Is_EVF_Expression (Expr) then
10875 return True;
10876 end if;
10878 Next (Expr);
10879 end loop;
10881 -- A qualified expression or a type conversion is an EVF expression when
10882 -- its operand is an EVF expression.
10884 elsif Nkind_In (N, N_Qualified_Expression,
10885 N_Unchecked_Type_Conversion,
10886 N_Type_Conversion)
10887 then
10888 return Is_EVF_Expression (Expression (N));
10890 -- Attributes 'Loop_Entry, 'Old and 'Update are an EVF expression when
10891 -- their prefix denotes an EVF expression.
10893 elsif Nkind (N) = N_Attribute_Reference
10894 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
10895 Name_Old,
10896 Name_Update)
10897 then
10898 return Is_EVF_Expression (Prefix (N));
10899 end if;
10901 return False;
10902 end Is_EVF_Expression;
10904 --------------
10905 -- Is_False --
10906 --------------
10908 function Is_False (U : Uint) return Boolean is
10909 begin
10910 return (U = 0);
10911 end Is_False;
10913 ---------------------------
10914 -- Is_Fixed_Model_Number --
10915 ---------------------------
10917 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
10918 S : constant Ureal := Small_Value (T);
10919 M : Urealp.Save_Mark;
10920 R : Boolean;
10921 begin
10922 M := Urealp.Mark;
10923 R := (U = UR_Trunc (U / S) * S);
10924 Urealp.Release (M);
10925 return R;
10926 end Is_Fixed_Model_Number;
10928 -------------------------------
10929 -- Is_Fully_Initialized_Type --
10930 -------------------------------
10932 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
10933 begin
10934 -- Scalar types
10936 if Is_Scalar_Type (Typ) then
10938 -- A scalar type with an aspect Default_Value is fully initialized
10940 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
10941 -- of a scalar type, but we don't take that into account here, since
10942 -- we don't want these to affect warnings.
10944 return Has_Default_Aspect (Typ);
10946 elsif Is_Access_Type (Typ) then
10947 return True;
10949 elsif Is_Array_Type (Typ) then
10950 if Is_Fully_Initialized_Type (Component_Type (Typ))
10951 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
10952 then
10953 return True;
10954 end if;
10956 -- An interesting case, if we have a constrained type one of whose
10957 -- bounds is known to be null, then there are no elements to be
10958 -- initialized, so all the elements are initialized.
10960 if Is_Constrained (Typ) then
10961 declare
10962 Indx : Node_Id;
10963 Indx_Typ : Entity_Id;
10964 Lbd, Hbd : Node_Id;
10966 begin
10967 Indx := First_Index (Typ);
10968 while Present (Indx) loop
10969 if Etype (Indx) = Any_Type then
10970 return False;
10972 -- If index is a range, use directly
10974 elsif Nkind (Indx) = N_Range then
10975 Lbd := Low_Bound (Indx);
10976 Hbd := High_Bound (Indx);
10978 else
10979 Indx_Typ := Etype (Indx);
10981 if Is_Private_Type (Indx_Typ) then
10982 Indx_Typ := Full_View (Indx_Typ);
10983 end if;
10985 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
10986 return False;
10987 else
10988 Lbd := Type_Low_Bound (Indx_Typ);
10989 Hbd := Type_High_Bound (Indx_Typ);
10990 end if;
10991 end if;
10993 if Compile_Time_Known_Value (Lbd)
10994 and then
10995 Compile_Time_Known_Value (Hbd)
10996 then
10997 if Expr_Value (Hbd) < Expr_Value (Lbd) then
10998 return True;
10999 end if;
11000 end if;
11002 Next_Index (Indx);
11003 end loop;
11004 end;
11005 end if;
11007 -- If no null indexes, then type is not fully initialized
11009 return False;
11011 -- Record types
11013 elsif Is_Record_Type (Typ) then
11014 if Has_Discriminants (Typ)
11015 and then
11016 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
11017 and then Is_Fully_Initialized_Variant (Typ)
11018 then
11019 return True;
11020 end if;
11022 -- We consider bounded string types to be fully initialized, because
11023 -- otherwise we get false alarms when the Data component is not
11024 -- default-initialized.
11026 if Is_Bounded_String (Typ) then
11027 return True;
11028 end if;
11030 -- Controlled records are considered to be fully initialized if
11031 -- there is a user defined Initialize routine. This may not be
11032 -- entirely correct, but as the spec notes, we are guessing here
11033 -- what is best from the point of view of issuing warnings.
11035 if Is_Controlled (Typ) then
11036 declare
11037 Utyp : constant Entity_Id := Underlying_Type (Typ);
11039 begin
11040 if Present (Utyp) then
11041 declare
11042 Init : constant Entity_Id :=
11043 (Find_Prim_Op
11044 (Underlying_Type (Typ), Name_Initialize));
11046 begin
11047 if Present (Init)
11048 and then Comes_From_Source (Init)
11049 and then not
11050 Is_Predefined_File_Name
11051 (File_Name (Get_Source_File_Index (Sloc (Init))))
11052 then
11053 return True;
11055 elsif Has_Null_Extension (Typ)
11056 and then
11057 Is_Fully_Initialized_Type
11058 (Etype (Base_Type (Typ)))
11059 then
11060 return True;
11061 end if;
11062 end;
11063 end if;
11064 end;
11065 end if;
11067 -- Otherwise see if all record components are initialized
11069 declare
11070 Ent : Entity_Id;
11072 begin
11073 Ent := First_Entity (Typ);
11074 while Present (Ent) loop
11075 if Ekind (Ent) = E_Component
11076 and then (No (Parent (Ent))
11077 or else No (Expression (Parent (Ent))))
11078 and then not Is_Fully_Initialized_Type (Etype (Ent))
11080 -- Special VM case for tag components, which need to be
11081 -- defined in this case, but are never initialized as VMs
11082 -- are using other dispatching mechanisms. Ignore this
11083 -- uninitialized case. Note that this applies both to the
11084 -- uTag entry and the main vtable pointer (CPP_Class case).
11086 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
11087 then
11088 return False;
11089 end if;
11091 Next_Entity (Ent);
11092 end loop;
11093 end;
11095 -- No uninitialized components, so type is fully initialized.
11096 -- Note that this catches the case of no components as well.
11098 return True;
11100 elsif Is_Concurrent_Type (Typ) then
11101 return True;
11103 elsif Is_Private_Type (Typ) then
11104 declare
11105 U : constant Entity_Id := Underlying_Type (Typ);
11107 begin
11108 if No (U) then
11109 return False;
11110 else
11111 return Is_Fully_Initialized_Type (U);
11112 end if;
11113 end;
11115 else
11116 return False;
11117 end if;
11118 end Is_Fully_Initialized_Type;
11120 ----------------------------------
11121 -- Is_Fully_Initialized_Variant --
11122 ----------------------------------
11124 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
11125 Loc : constant Source_Ptr := Sloc (Typ);
11126 Constraints : constant List_Id := New_List;
11127 Components : constant Elist_Id := New_Elmt_List;
11128 Comp_Elmt : Elmt_Id;
11129 Comp_Id : Node_Id;
11130 Comp_List : Node_Id;
11131 Discr : Entity_Id;
11132 Discr_Val : Node_Id;
11134 Report_Errors : Boolean;
11135 pragma Warnings (Off, Report_Errors);
11137 begin
11138 if Serious_Errors_Detected > 0 then
11139 return False;
11140 end if;
11142 if Is_Record_Type (Typ)
11143 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11144 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
11145 then
11146 Comp_List := Component_List (Type_Definition (Parent (Typ)));
11148 Discr := First_Discriminant (Typ);
11149 while Present (Discr) loop
11150 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
11151 Discr_Val := Expression (Parent (Discr));
11153 if Present (Discr_Val)
11154 and then Is_OK_Static_Expression (Discr_Val)
11155 then
11156 Append_To (Constraints,
11157 Make_Component_Association (Loc,
11158 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
11159 Expression => New_Copy (Discr_Val)));
11160 else
11161 return False;
11162 end if;
11163 else
11164 return False;
11165 end if;
11167 Next_Discriminant (Discr);
11168 end loop;
11170 Gather_Components
11171 (Typ => Typ,
11172 Comp_List => Comp_List,
11173 Governed_By => Constraints,
11174 Into => Components,
11175 Report_Errors => Report_Errors);
11177 -- Check that each component present is fully initialized
11179 Comp_Elmt := First_Elmt (Components);
11180 while Present (Comp_Elmt) loop
11181 Comp_Id := Node (Comp_Elmt);
11183 if Ekind (Comp_Id) = E_Component
11184 and then (No (Parent (Comp_Id))
11185 or else No (Expression (Parent (Comp_Id))))
11186 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
11187 then
11188 return False;
11189 end if;
11191 Next_Elmt (Comp_Elmt);
11192 end loop;
11194 return True;
11196 elsif Is_Private_Type (Typ) then
11197 declare
11198 U : constant Entity_Id := Underlying_Type (Typ);
11200 begin
11201 if No (U) then
11202 return False;
11203 else
11204 return Is_Fully_Initialized_Variant (U);
11205 end if;
11206 end;
11208 else
11209 return False;
11210 end if;
11211 end Is_Fully_Initialized_Variant;
11213 ---------------------
11214 -- Is_Ghost_Entity --
11215 ---------------------
11217 function Is_Ghost_Entity (Id : Entity_Id) return Boolean is
11218 begin
11219 return Is_Checked_Ghost_Entity (Id) or else Is_Ignored_Ghost_Entity (Id);
11220 end Is_Ghost_Entity;
11222 ----------------------------------
11223 -- Is_Ghost_Statement_Or_Pragma --
11224 ----------------------------------
11226 function Is_Ghost_Statement_Or_Pragma (N : Node_Id) return Boolean is
11227 function Is_Ghost_Entity_Reference (N : Node_Id) return Boolean;
11228 -- Determine whether an arbitrary node denotes a reference to a Ghost
11229 -- entity.
11231 -------------------------------
11232 -- Is_Ghost_Entity_Reference --
11233 -------------------------------
11235 function Is_Ghost_Entity_Reference (N : Node_Id) return Boolean is
11236 Ref : Node_Id;
11238 begin
11239 Ref := N;
11241 -- When the reference extracts a subcomponent, recover the related
11242 -- object (SPARK RM 6.9(1)).
11244 while Nkind_In (Ref, N_Explicit_Dereference,
11245 N_Indexed_Component,
11246 N_Selected_Component,
11247 N_Slice)
11248 loop
11249 Ref := Prefix (Ref);
11250 end loop;
11252 return
11253 Is_Entity_Name (Ref)
11254 and then Present (Entity (Ref))
11255 and then Is_Ghost_Entity (Entity (Ref));
11256 end Is_Ghost_Entity_Reference;
11258 -- Local variables
11260 Arg : Node_Id;
11261 Stmt : Node_Id;
11263 -- Start of processing for Is_Ghost_Statement_Or_Pragma
11265 begin
11266 if Nkind (N) = N_Pragma then
11268 -- A pragma is Ghost when it appears within a Ghost package or
11269 -- subprogram.
11271 if Within_Ghost_Scope then
11272 return True;
11273 end if;
11275 -- A pragma is Ghost when it mentions a Ghost entity
11277 Arg := First (Pragma_Argument_Associations (N));
11278 while Present (Arg) loop
11279 if Is_Ghost_Entity_Reference (Get_Pragma_Arg (Arg)) then
11280 return True;
11281 end if;
11283 Next (Arg);
11284 end loop;
11285 end if;
11287 Stmt := N;
11288 while Present (Stmt) loop
11290 -- A statement is Ghost when it appears within a Ghost package or
11291 -- subprogram.
11293 if Is_Statement (Stmt) and then Within_Ghost_Scope then
11294 return True;
11296 -- An assignment statement is Ghost when the target is a Ghost
11297 -- variable. A procedure call is Ghost when the invoked procedure
11298 -- is Ghost.
11300 elsif Nkind_In (Stmt, N_Assignment_Statement,
11301 N_Procedure_Call_Statement)
11302 then
11303 return Is_Ghost_Entity_Reference (Name (Stmt));
11305 -- Prevent the search from going too far
11307 elsif Is_Body_Or_Package_Declaration (Stmt) then
11308 return False;
11309 end if;
11311 Stmt := Parent (Stmt);
11312 end loop;
11314 return False;
11315 end Is_Ghost_Statement_Or_Pragma;
11317 ----------------------------
11318 -- Is_Inherited_Operation --
11319 ----------------------------
11321 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
11322 pragma Assert (Is_Overloadable (E));
11323 Kind : constant Node_Kind := Nkind (Parent (E));
11324 begin
11325 return Kind = N_Full_Type_Declaration
11326 or else Kind = N_Private_Extension_Declaration
11327 or else Kind = N_Subtype_Declaration
11328 or else (Ekind (E) = E_Enumeration_Literal
11329 and then Is_Derived_Type (Etype (E)));
11330 end Is_Inherited_Operation;
11332 -------------------------------------
11333 -- Is_Inherited_Operation_For_Type --
11334 -------------------------------------
11336 function Is_Inherited_Operation_For_Type
11337 (E : Entity_Id;
11338 Typ : Entity_Id) return Boolean
11340 begin
11341 -- Check that the operation has been created by the type declaration
11343 return Is_Inherited_Operation (E)
11344 and then Defining_Identifier (Parent (E)) = Typ;
11345 end Is_Inherited_Operation_For_Type;
11347 -----------------
11348 -- Is_Iterator --
11349 -----------------
11351 function Is_Iterator (Typ : Entity_Id) return Boolean is
11352 Ifaces_List : Elist_Id;
11353 Iface_Elmt : Elmt_Id;
11354 Iface : Entity_Id;
11356 begin
11357 if Is_Class_Wide_Type (Typ)
11358 and then Nam_In (Chars (Etype (Typ)), Name_Forward_Iterator,
11359 Name_Reversible_Iterator)
11360 and then
11361 Is_Predefined_File_Name
11362 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
11363 then
11364 return True;
11366 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
11367 return False;
11369 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
11370 return True;
11372 else
11373 Collect_Interfaces (Typ, Ifaces_List);
11375 Iface_Elmt := First_Elmt (Ifaces_List);
11376 while Present (Iface_Elmt) loop
11377 Iface := Node (Iface_Elmt);
11378 if Chars (Iface) = Name_Forward_Iterator
11379 and then
11380 Is_Predefined_File_Name
11381 (Unit_File_Name (Get_Source_Unit (Iface)))
11382 then
11383 return True;
11384 end if;
11386 Next_Elmt (Iface_Elmt);
11387 end loop;
11389 return False;
11390 end if;
11391 end Is_Iterator;
11393 ------------
11394 -- Is_LHS --
11395 ------------
11397 -- We seem to have a lot of overlapping functions that do similar things
11398 -- (testing for left hand sides or lvalues???).
11400 function Is_LHS (N : Node_Id) return Is_LHS_Result is
11401 P : constant Node_Id := Parent (N);
11403 begin
11404 -- Return True if we are the left hand side of an assignment statement
11406 if Nkind (P) = N_Assignment_Statement then
11407 if Name (P) = N then
11408 return Yes;
11409 else
11410 return No;
11411 end if;
11413 -- Case of prefix of indexed or selected component or slice
11415 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
11416 and then N = Prefix (P)
11417 then
11418 -- Here we have the case where the parent P is N.Q or N(Q .. R).
11419 -- If P is an LHS, then N is also effectively an LHS, but there
11420 -- is an important exception. If N is of an access type, then
11421 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
11422 -- case this makes N.all a left hand side but not N itself.
11424 -- If we don't know the type yet, this is the case where we return
11425 -- Unknown, since the answer depends on the type which is unknown.
11427 if No (Etype (N)) then
11428 return Unknown;
11430 -- We have an Etype set, so we can check it
11432 elsif Is_Access_Type (Etype (N)) then
11433 return No;
11435 -- OK, not access type case, so just test whole expression
11437 else
11438 return Is_LHS (P);
11439 end if;
11441 -- All other cases are not left hand sides
11443 else
11444 return No;
11445 end if;
11446 end Is_LHS;
11448 -----------------------------
11449 -- Is_Library_Level_Entity --
11450 -----------------------------
11452 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
11453 begin
11454 -- The following is a small optimization, and it also properly handles
11455 -- discriminals, which in task bodies might appear in expressions before
11456 -- the corresponding procedure has been created, and which therefore do
11457 -- not have an assigned scope.
11459 if Is_Formal (E) then
11460 return False;
11461 end if;
11463 -- Normal test is simply that the enclosing dynamic scope is Standard
11465 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
11466 end Is_Library_Level_Entity;
11468 --------------------------------
11469 -- Is_Limited_Class_Wide_Type --
11470 --------------------------------
11472 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
11473 begin
11474 return
11475 Is_Class_Wide_Type (Typ)
11476 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
11477 end Is_Limited_Class_Wide_Type;
11479 ---------------------------------
11480 -- Is_Local_Variable_Reference --
11481 ---------------------------------
11483 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
11484 begin
11485 if not Is_Entity_Name (Expr) then
11486 return False;
11488 else
11489 declare
11490 Ent : constant Entity_Id := Entity (Expr);
11491 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
11492 begin
11493 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
11494 return False;
11495 else
11496 return Present (Sub) and then Sub = Current_Subprogram;
11497 end if;
11498 end;
11499 end if;
11500 end Is_Local_Variable_Reference;
11502 -------------------------
11503 -- Is_Object_Reference --
11504 -------------------------
11506 function Is_Object_Reference (N : Node_Id) return Boolean is
11508 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
11509 -- Determine whether N is the name of an internally-generated renaming
11511 --------------------------------------
11512 -- Is_Internally_Generated_Renaming --
11513 --------------------------------------
11515 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
11516 P : Node_Id;
11518 begin
11519 P := N;
11520 while Present (P) loop
11521 if Nkind (P) = N_Object_Renaming_Declaration then
11522 return not Comes_From_Source (P);
11523 elsif Is_List_Member (P) then
11524 return False;
11525 end if;
11527 P := Parent (P);
11528 end loop;
11530 return False;
11531 end Is_Internally_Generated_Renaming;
11533 -- Start of processing for Is_Object_Reference
11535 begin
11536 if Is_Entity_Name (N) then
11537 return Present (Entity (N)) and then Is_Object (Entity (N));
11539 else
11540 case Nkind (N) is
11541 when N_Indexed_Component | N_Slice =>
11542 return
11543 Is_Object_Reference (Prefix (N))
11544 or else Is_Access_Type (Etype (Prefix (N)));
11546 -- In Ada 95, a function call is a constant object; a procedure
11547 -- call is not.
11549 when N_Function_Call =>
11550 return Etype (N) /= Standard_Void_Type;
11552 -- Attributes 'Input, 'Old and 'Result produce objects
11554 when N_Attribute_Reference =>
11555 return
11556 Nam_In
11557 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
11559 when N_Selected_Component =>
11560 return
11561 Is_Object_Reference (Selector_Name (N))
11562 and then
11563 (Is_Object_Reference (Prefix (N))
11564 or else Is_Access_Type (Etype (Prefix (N))));
11566 when N_Explicit_Dereference =>
11567 return True;
11569 -- A view conversion of a tagged object is an object reference
11571 when N_Type_Conversion =>
11572 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
11573 and then Is_Tagged_Type (Etype (Expression (N)))
11574 and then Is_Object_Reference (Expression (N));
11576 -- An unchecked type conversion is considered to be an object if
11577 -- the operand is an object (this construction arises only as a
11578 -- result of expansion activities).
11580 when N_Unchecked_Type_Conversion =>
11581 return True;
11583 -- Allow string literals to act as objects as long as they appear
11584 -- in internally-generated renamings. The expansion of iterators
11585 -- may generate such renamings when the range involves a string
11586 -- literal.
11588 when N_String_Literal =>
11589 return Is_Internally_Generated_Renaming (Parent (N));
11591 -- AI05-0003: In Ada 2012 a qualified expression is a name.
11592 -- This allows disambiguation of function calls and the use
11593 -- of aggregates in more contexts.
11595 when N_Qualified_Expression =>
11596 if Ada_Version < Ada_2012 then
11597 return False;
11598 else
11599 return Is_Object_Reference (Expression (N))
11600 or else Nkind (Expression (N)) = N_Aggregate;
11601 end if;
11603 when others =>
11604 return False;
11605 end case;
11606 end if;
11607 end Is_Object_Reference;
11609 -----------------------------------
11610 -- Is_OK_Variable_For_Out_Formal --
11611 -----------------------------------
11613 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
11614 begin
11615 Note_Possible_Modification (AV, Sure => True);
11617 -- We must reject parenthesized variable names. Comes_From_Source is
11618 -- checked because there are currently cases where the compiler violates
11619 -- this rule (e.g. passing a task object to its controlled Initialize
11620 -- routine). This should be properly documented in sinfo???
11622 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
11623 return False;
11625 -- A variable is always allowed
11627 elsif Is_Variable (AV) then
11628 return True;
11630 -- Generalized indexing operations are rewritten as explicit
11631 -- dereferences, and it is only during resolution that we can
11632 -- check whether the context requires an access_to_variable type.
11634 elsif Nkind (AV) = N_Explicit_Dereference
11635 and then Ada_Version >= Ada_2012
11636 and then Nkind (Original_Node (AV)) = N_Indexed_Component
11637 and then Present (Etype (Original_Node (AV)))
11638 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
11639 then
11640 return not Is_Access_Constant (Etype (Prefix (AV)));
11642 -- Unchecked conversions are allowed only if they come from the
11643 -- generated code, which sometimes uses unchecked conversions for out
11644 -- parameters in cases where code generation is unaffected. We tell
11645 -- source unchecked conversions by seeing if they are rewrites of
11646 -- an original Unchecked_Conversion function call, or of an explicit
11647 -- conversion of a function call or an aggregate (as may happen in the
11648 -- expansion of a packed array aggregate).
11650 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
11651 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
11652 return False;
11654 elsif Comes_From_Source (AV)
11655 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
11656 then
11657 return False;
11659 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
11660 return Is_OK_Variable_For_Out_Formal (Expression (AV));
11662 else
11663 return True;
11664 end if;
11666 -- Normal type conversions are allowed if argument is a variable
11668 elsif Nkind (AV) = N_Type_Conversion then
11669 if Is_Variable (Expression (AV))
11670 and then Paren_Count (Expression (AV)) = 0
11671 then
11672 Note_Possible_Modification (Expression (AV), Sure => True);
11673 return True;
11675 -- We also allow a non-parenthesized expression that raises
11676 -- constraint error if it rewrites what used to be a variable
11678 elsif Raises_Constraint_Error (Expression (AV))
11679 and then Paren_Count (Expression (AV)) = 0
11680 and then Is_Variable (Original_Node (Expression (AV)))
11681 then
11682 return True;
11684 -- Type conversion of something other than a variable
11686 else
11687 return False;
11688 end if;
11690 -- If this node is rewritten, then test the original form, if that is
11691 -- OK, then we consider the rewritten node OK (for example, if the
11692 -- original node is a conversion, then Is_Variable will not be true
11693 -- but we still want to allow the conversion if it converts a variable).
11695 elsif Original_Node (AV) /= AV then
11697 -- In Ada 2012, the explicit dereference may be a rewritten call to a
11698 -- Reference function.
11700 if Ada_Version >= Ada_2012
11701 and then Nkind (Original_Node (AV)) = N_Function_Call
11702 and then
11703 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
11704 then
11705 return True;
11707 else
11708 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
11709 end if;
11711 -- All other non-variables are rejected
11713 else
11714 return False;
11715 end if;
11716 end Is_OK_Variable_For_Out_Formal;
11718 -----------------------------------
11719 -- Is_Partially_Initialized_Type --
11720 -----------------------------------
11722 function Is_Partially_Initialized_Type
11723 (Typ : Entity_Id;
11724 Include_Implicit : Boolean := True) return Boolean
11726 begin
11727 if Is_Scalar_Type (Typ) then
11728 return False;
11730 elsif Is_Access_Type (Typ) then
11731 return Include_Implicit;
11733 elsif Is_Array_Type (Typ) then
11735 -- If component type is partially initialized, so is array type
11737 if Is_Partially_Initialized_Type
11738 (Component_Type (Typ), Include_Implicit)
11739 then
11740 return True;
11742 -- Otherwise we are only partially initialized if we are fully
11743 -- initialized (this is the empty array case, no point in us
11744 -- duplicating that code here).
11746 else
11747 return Is_Fully_Initialized_Type (Typ);
11748 end if;
11750 elsif Is_Record_Type (Typ) then
11752 -- A discriminated type is always partially initialized if in
11753 -- all mode
11755 if Has_Discriminants (Typ) and then Include_Implicit then
11756 return True;
11758 -- A tagged type is always partially initialized
11760 elsif Is_Tagged_Type (Typ) then
11761 return True;
11763 -- Case of non-discriminated record
11765 else
11766 declare
11767 Ent : Entity_Id;
11769 Component_Present : Boolean := False;
11770 -- Set True if at least one component is present. If no
11771 -- components are present, then record type is fully
11772 -- initialized (another odd case, like the null array).
11774 begin
11775 -- Loop through components
11777 Ent := First_Entity (Typ);
11778 while Present (Ent) loop
11779 if Ekind (Ent) = E_Component then
11780 Component_Present := True;
11782 -- If a component has an initialization expression then
11783 -- the enclosing record type is partially initialized
11785 if Present (Parent (Ent))
11786 and then Present (Expression (Parent (Ent)))
11787 then
11788 return True;
11790 -- If a component is of a type which is itself partially
11791 -- initialized, then the enclosing record type is also.
11793 elsif Is_Partially_Initialized_Type
11794 (Etype (Ent), Include_Implicit)
11795 then
11796 return True;
11797 end if;
11798 end if;
11800 Next_Entity (Ent);
11801 end loop;
11803 -- No initialized components found. If we found any components
11804 -- they were all uninitialized so the result is false.
11806 if Component_Present then
11807 return False;
11809 -- But if we found no components, then all the components are
11810 -- initialized so we consider the type to be initialized.
11812 else
11813 return True;
11814 end if;
11815 end;
11816 end if;
11818 -- Concurrent types are always fully initialized
11820 elsif Is_Concurrent_Type (Typ) then
11821 return True;
11823 -- For a private type, go to underlying type. If there is no underlying
11824 -- type then just assume this partially initialized. Not clear if this
11825 -- can happen in a non-error case, but no harm in testing for this.
11827 elsif Is_Private_Type (Typ) then
11828 declare
11829 U : constant Entity_Id := Underlying_Type (Typ);
11830 begin
11831 if No (U) then
11832 return True;
11833 else
11834 return Is_Partially_Initialized_Type (U, Include_Implicit);
11835 end if;
11836 end;
11838 -- For any other type (are there any?) assume partially initialized
11840 else
11841 return True;
11842 end if;
11843 end Is_Partially_Initialized_Type;
11845 ------------------------------------
11846 -- Is_Potentially_Persistent_Type --
11847 ------------------------------------
11849 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
11850 Comp : Entity_Id;
11851 Indx : Node_Id;
11853 begin
11854 -- For private type, test corresponding full type
11856 if Is_Private_Type (T) then
11857 return Is_Potentially_Persistent_Type (Full_View (T));
11859 -- Scalar types are potentially persistent
11861 elsif Is_Scalar_Type (T) then
11862 return True;
11864 -- Record type is potentially persistent if not tagged and the types of
11865 -- all it components are potentially persistent, and no component has
11866 -- an initialization expression.
11868 elsif Is_Record_Type (T)
11869 and then not Is_Tagged_Type (T)
11870 and then not Is_Partially_Initialized_Type (T)
11871 then
11872 Comp := First_Component (T);
11873 while Present (Comp) loop
11874 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
11875 return False;
11876 else
11877 Next_Entity (Comp);
11878 end if;
11879 end loop;
11881 return True;
11883 -- Array type is potentially persistent if its component type is
11884 -- potentially persistent and if all its constraints are static.
11886 elsif Is_Array_Type (T) then
11887 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
11888 return False;
11889 end if;
11891 Indx := First_Index (T);
11892 while Present (Indx) loop
11893 if not Is_OK_Static_Subtype (Etype (Indx)) then
11894 return False;
11895 else
11896 Next_Index (Indx);
11897 end if;
11898 end loop;
11900 return True;
11902 -- All other types are not potentially persistent
11904 else
11905 return False;
11906 end if;
11907 end Is_Potentially_Persistent_Type;
11909 --------------------------------
11910 -- Is_Potentially_Unevaluated --
11911 --------------------------------
11913 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
11914 Par : Node_Id;
11915 Expr : Node_Id;
11917 begin
11918 Expr := N;
11919 Par := Parent (N);
11921 -- A postcondition whose expression is a short-circuit is broken down
11922 -- into individual aspects for better exception reporting. The original
11923 -- short-circuit expression is rewritten as the second operand, and an
11924 -- occurrence of 'Old in that operand is potentially unevaluated.
11925 -- See Sem_ch13.adb for details of this transformation.
11927 if Nkind (Original_Node (Par)) = N_And_Then then
11928 return True;
11929 end if;
11931 while not Nkind_In (Par, N_If_Expression,
11932 N_Case_Expression,
11933 N_And_Then,
11934 N_Or_Else,
11935 N_In,
11936 N_Not_In)
11937 loop
11938 Expr := Par;
11939 Par := Parent (Par);
11941 -- If the context is not an expression, or if is the result of
11942 -- expansion of an enclosing construct (such as another attribute)
11943 -- the predicate does not apply.
11945 if Nkind (Par) not in N_Subexpr
11946 or else not Comes_From_Source (Par)
11947 then
11948 return False;
11949 end if;
11950 end loop;
11952 if Nkind (Par) = N_If_Expression then
11953 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
11955 elsif Nkind (Par) = N_Case_Expression then
11956 return Expr /= Expression (Par);
11958 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
11959 return Expr = Right_Opnd (Par);
11961 elsif Nkind_In (Par, N_In, N_Not_In) then
11962 return Expr /= Left_Opnd (Par);
11964 else
11965 return False;
11966 end if;
11967 end Is_Potentially_Unevaluated;
11969 ---------------------------------
11970 -- Is_Protected_Self_Reference --
11971 ---------------------------------
11973 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
11975 function In_Access_Definition (N : Node_Id) return Boolean;
11976 -- Returns true if N belongs to an access definition
11978 --------------------------
11979 -- In_Access_Definition --
11980 --------------------------
11982 function In_Access_Definition (N : Node_Id) return Boolean is
11983 P : Node_Id;
11985 begin
11986 P := Parent (N);
11987 while Present (P) loop
11988 if Nkind (P) = N_Access_Definition then
11989 return True;
11990 end if;
11992 P := Parent (P);
11993 end loop;
11995 return False;
11996 end In_Access_Definition;
11998 -- Start of processing for Is_Protected_Self_Reference
12000 begin
12001 -- Verify that prefix is analyzed and has the proper form. Note that
12002 -- the attributes Elab_Spec, Elab_Body, Elab_Subp_Body and UET_Address,
12003 -- which also produce the address of an entity, do not analyze their
12004 -- prefix because they denote entities that are not necessarily visible.
12005 -- Neither of them can apply to a protected type.
12007 return Ada_Version >= Ada_2005
12008 and then Is_Entity_Name (N)
12009 and then Present (Entity (N))
12010 and then Is_Protected_Type (Entity (N))
12011 and then In_Open_Scopes (Entity (N))
12012 and then not In_Access_Definition (N);
12013 end Is_Protected_Self_Reference;
12015 -----------------------------
12016 -- Is_RCI_Pkg_Spec_Or_Body --
12017 -----------------------------
12019 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
12021 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
12022 -- Return True if the unit of Cunit is an RCI package declaration
12024 ---------------------------
12025 -- Is_RCI_Pkg_Decl_Cunit --
12026 ---------------------------
12028 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
12029 The_Unit : constant Node_Id := Unit (Cunit);
12031 begin
12032 if Nkind (The_Unit) /= N_Package_Declaration then
12033 return False;
12034 end if;
12036 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
12037 end Is_RCI_Pkg_Decl_Cunit;
12039 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
12041 begin
12042 return Is_RCI_Pkg_Decl_Cunit (Cunit)
12043 or else
12044 (Nkind (Unit (Cunit)) = N_Package_Body
12045 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
12046 end Is_RCI_Pkg_Spec_Or_Body;
12048 -----------------------------------------
12049 -- Is_Remote_Access_To_Class_Wide_Type --
12050 -----------------------------------------
12052 function Is_Remote_Access_To_Class_Wide_Type
12053 (E : Entity_Id) return Boolean
12055 begin
12056 -- A remote access to class-wide type is a general access to object type
12057 -- declared in the visible part of a Remote_Types or Remote_Call_
12058 -- Interface unit.
12060 return Ekind (E) = E_General_Access_Type
12061 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12062 end Is_Remote_Access_To_Class_Wide_Type;
12064 -----------------------------------------
12065 -- Is_Remote_Access_To_Subprogram_Type --
12066 -----------------------------------------
12068 function Is_Remote_Access_To_Subprogram_Type
12069 (E : Entity_Id) return Boolean
12071 begin
12072 return (Ekind (E) = E_Access_Subprogram_Type
12073 or else (Ekind (E) = E_Record_Type
12074 and then Present (Corresponding_Remote_Type (E))))
12075 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12076 end Is_Remote_Access_To_Subprogram_Type;
12078 --------------------
12079 -- Is_Remote_Call --
12080 --------------------
12082 function Is_Remote_Call (N : Node_Id) return Boolean is
12083 begin
12084 if Nkind (N) not in N_Subprogram_Call then
12086 -- An entry call cannot be remote
12088 return False;
12090 elsif Nkind (Name (N)) in N_Has_Entity
12091 and then Is_Remote_Call_Interface (Entity (Name (N)))
12092 then
12093 -- A subprogram declared in the spec of a RCI package is remote
12095 return True;
12097 elsif Nkind (Name (N)) = N_Explicit_Dereference
12098 and then Is_Remote_Access_To_Subprogram_Type
12099 (Etype (Prefix (Name (N))))
12100 then
12101 -- The dereference of a RAS is a remote call
12103 return True;
12105 elsif Present (Controlling_Argument (N))
12106 and then Is_Remote_Access_To_Class_Wide_Type
12107 (Etype (Controlling_Argument (N)))
12108 then
12109 -- Any primitive operation call with a controlling argument of
12110 -- a RACW type is a remote call.
12112 return True;
12113 end if;
12115 -- All other calls are local calls
12117 return False;
12118 end Is_Remote_Call;
12120 ----------------------
12121 -- Is_Renamed_Entry --
12122 ----------------------
12124 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
12125 Orig_Node : Node_Id := Empty;
12126 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
12128 function Is_Entry (Nam : Node_Id) return Boolean;
12129 -- Determine whether Nam is an entry. Traverse selectors if there are
12130 -- nested selected components.
12132 --------------
12133 -- Is_Entry --
12134 --------------
12136 function Is_Entry (Nam : Node_Id) return Boolean is
12137 begin
12138 if Nkind (Nam) = N_Selected_Component then
12139 return Is_Entry (Selector_Name (Nam));
12140 end if;
12142 return Ekind (Entity (Nam)) = E_Entry;
12143 end Is_Entry;
12145 -- Start of processing for Is_Renamed_Entry
12147 begin
12148 if Present (Alias (Proc_Nam)) then
12149 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
12150 end if;
12152 -- Look for a rewritten subprogram renaming declaration
12154 if Nkind (Subp_Decl) = N_Subprogram_Declaration
12155 and then Present (Original_Node (Subp_Decl))
12156 then
12157 Orig_Node := Original_Node (Subp_Decl);
12158 end if;
12160 -- The rewritten subprogram is actually an entry
12162 if Present (Orig_Node)
12163 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
12164 and then Is_Entry (Name (Orig_Node))
12165 then
12166 return True;
12167 end if;
12169 return False;
12170 end Is_Renamed_Entry;
12172 ----------------------------
12173 -- Is_Reversible_Iterator --
12174 ----------------------------
12176 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
12177 Ifaces_List : Elist_Id;
12178 Iface_Elmt : Elmt_Id;
12179 Iface : Entity_Id;
12181 begin
12182 if Is_Class_Wide_Type (Typ)
12183 and then Chars (Etype (Typ)) = Name_Reversible_Iterator
12184 and then Is_Predefined_File_Name
12185 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
12186 then
12187 return True;
12189 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
12190 return False;
12192 else
12193 Collect_Interfaces (Typ, Ifaces_List);
12195 Iface_Elmt := First_Elmt (Ifaces_List);
12196 while Present (Iface_Elmt) loop
12197 Iface := Node (Iface_Elmt);
12198 if Chars (Iface) = Name_Reversible_Iterator
12199 and then
12200 Is_Predefined_File_Name
12201 (Unit_File_Name (Get_Source_Unit (Iface)))
12202 then
12203 return True;
12204 end if;
12206 Next_Elmt (Iface_Elmt);
12207 end loop;
12208 end if;
12210 return False;
12211 end Is_Reversible_Iterator;
12213 ----------------------
12214 -- Is_Selector_Name --
12215 ----------------------
12217 function Is_Selector_Name (N : Node_Id) return Boolean is
12218 begin
12219 if not Is_List_Member (N) then
12220 declare
12221 P : constant Node_Id := Parent (N);
12222 begin
12223 return Nkind_In (P, N_Expanded_Name,
12224 N_Generic_Association,
12225 N_Parameter_Association,
12226 N_Selected_Component)
12227 and then Selector_Name (P) = N;
12228 end;
12230 else
12231 declare
12232 L : constant List_Id := List_Containing (N);
12233 P : constant Node_Id := Parent (L);
12234 begin
12235 return (Nkind (P) = N_Discriminant_Association
12236 and then Selector_Names (P) = L)
12237 or else
12238 (Nkind (P) = N_Component_Association
12239 and then Choices (P) = L);
12240 end;
12241 end if;
12242 end Is_Selector_Name;
12244 -------------------------------------
12245 -- Is_SPARK_05_Initialization_Expr --
12246 -------------------------------------
12248 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
12249 Is_Ok : Boolean;
12250 Expr : Node_Id;
12251 Comp_Assn : Node_Id;
12252 Orig_N : constant Node_Id := Original_Node (N);
12254 begin
12255 Is_Ok := True;
12257 if not Comes_From_Source (Orig_N) then
12258 goto Done;
12259 end if;
12261 pragma Assert (Nkind (Orig_N) in N_Subexpr);
12263 case Nkind (Orig_N) is
12264 when N_Character_Literal |
12265 N_Integer_Literal |
12266 N_Real_Literal |
12267 N_String_Literal =>
12268 null;
12270 when N_Identifier |
12271 N_Expanded_Name =>
12272 if Is_Entity_Name (Orig_N)
12273 and then Present (Entity (Orig_N)) -- needed in some cases
12274 then
12275 case Ekind (Entity (Orig_N)) is
12276 when E_Constant |
12277 E_Enumeration_Literal |
12278 E_Named_Integer |
12279 E_Named_Real =>
12280 null;
12281 when others =>
12282 if Is_Type (Entity (Orig_N)) then
12283 null;
12284 else
12285 Is_Ok := False;
12286 end if;
12287 end case;
12288 end if;
12290 when N_Qualified_Expression |
12291 N_Type_Conversion =>
12292 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
12294 when N_Unary_Op =>
12295 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12297 when N_Binary_Op |
12298 N_Short_Circuit |
12299 N_Membership_Test =>
12300 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
12301 and then
12302 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12304 when N_Aggregate |
12305 N_Extension_Aggregate =>
12306 if Nkind (Orig_N) = N_Extension_Aggregate then
12307 Is_Ok :=
12308 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
12309 end if;
12311 Expr := First (Expressions (Orig_N));
12312 while Present (Expr) loop
12313 if not Is_SPARK_05_Initialization_Expr (Expr) then
12314 Is_Ok := False;
12315 goto Done;
12316 end if;
12318 Next (Expr);
12319 end loop;
12321 Comp_Assn := First (Component_Associations (Orig_N));
12322 while Present (Comp_Assn) loop
12323 Expr := Expression (Comp_Assn);
12325 -- Note: test for Present here needed for box assocation
12327 if Present (Expr)
12328 and then not Is_SPARK_05_Initialization_Expr (Expr)
12329 then
12330 Is_Ok := False;
12331 goto Done;
12332 end if;
12334 Next (Comp_Assn);
12335 end loop;
12337 when N_Attribute_Reference =>
12338 if Nkind (Prefix (Orig_N)) in N_Subexpr then
12339 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
12340 end if;
12342 Expr := First (Expressions (Orig_N));
12343 while Present (Expr) loop
12344 if not Is_SPARK_05_Initialization_Expr (Expr) then
12345 Is_Ok := False;
12346 goto Done;
12347 end if;
12349 Next (Expr);
12350 end loop;
12352 -- Selected components might be expanded named not yet resolved, so
12353 -- default on the safe side. (Eg on sparklex.ads)
12355 when N_Selected_Component =>
12356 null;
12358 when others =>
12359 Is_Ok := False;
12360 end case;
12362 <<Done>>
12363 return Is_Ok;
12364 end Is_SPARK_05_Initialization_Expr;
12366 ----------------------------------
12367 -- Is_SPARK_05_Object_Reference --
12368 ----------------------------------
12370 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
12371 begin
12372 if Is_Entity_Name (N) then
12373 return Present (Entity (N))
12374 and then
12375 (Ekind_In (Entity (N), E_Constant, E_Variable)
12376 or else Ekind (Entity (N)) in Formal_Kind);
12378 else
12379 case Nkind (N) is
12380 when N_Selected_Component =>
12381 return Is_SPARK_05_Object_Reference (Prefix (N));
12383 when others =>
12384 return False;
12385 end case;
12386 end if;
12387 end Is_SPARK_05_Object_Reference;
12389 -----------------------------
12390 -- Is_Specific_Tagged_Type --
12391 -----------------------------
12393 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
12394 Full_Typ : Entity_Id;
12396 begin
12397 -- Handle private types
12399 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
12400 Full_Typ := Full_View (Typ);
12401 else
12402 Full_Typ := Typ;
12403 end if;
12405 -- A specific tagged type is a non-class-wide tagged type
12407 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
12408 end Is_Specific_Tagged_Type;
12410 ------------------
12411 -- Is_Statement --
12412 ------------------
12414 function Is_Statement (N : Node_Id) return Boolean is
12415 begin
12416 return
12417 Nkind (N) in N_Statement_Other_Than_Procedure_Call
12418 or else Nkind (N) = N_Procedure_Call_Statement;
12419 end Is_Statement;
12421 -------------------------
12422 -- Is_Subject_To_Ghost --
12423 -------------------------
12425 function Is_Subject_To_Ghost (N : Node_Id) return Boolean is
12426 function Enables_Ghostness (Arg : Node_Id) return Boolean;
12427 -- Determine whether aspect or pragma argument Arg enables "ghostness"
12429 -----------------------
12430 -- Enables_Ghostness --
12431 -----------------------
12433 function Enables_Ghostness (Arg : Node_Id) return Boolean is
12434 Expr : Node_Id;
12436 begin
12437 Expr := Arg;
12439 if Nkind (Expr) = N_Pragma_Argument_Association then
12440 Expr := Get_Pragma_Arg (Expr);
12441 end if;
12443 -- Determine whether the expression of the aspect is static and
12444 -- denotes True.
12446 if Present (Expr) then
12447 Preanalyze_And_Resolve (Expr);
12449 return
12450 Is_OK_Static_Expression (Expr)
12451 and then Is_True (Expr_Value (Expr));
12453 -- Otherwise Ghost defaults to True
12455 else
12456 return True;
12457 end if;
12458 end Enables_Ghostness;
12460 -- Local variables
12462 Id : constant Entity_Id := Defining_Entity (N);
12463 Asp : Node_Id;
12464 Decl : Node_Id;
12465 Prev_Id : Entity_Id;
12467 -- Start of processing for Is_Subject_To_Ghost
12469 begin
12470 if Is_Ghost_Entity (Id) then
12471 return True;
12473 -- The completion of a type or a constant is not fully analyzed when the
12474 -- reference to the Ghost entity is resolved. Because the completion is
12475 -- not marked as Ghost yet, inspect the partial view.
12477 elsif Is_Record_Type (Id)
12478 or else Ekind (Id) = E_Constant
12479 or else (Nkind (N) = N_Object_Declaration
12480 and then Constant_Present (N))
12481 then
12482 Prev_Id := Incomplete_Or_Partial_View (Id);
12484 if Present (Prev_Id) and then Is_Ghost_Entity (Prev_Id) then
12485 return True;
12486 end if;
12487 end if;
12489 -- Examine the aspect specifications (if any) looking for aspect Ghost
12491 if Permits_Aspect_Specifications (N) then
12492 Asp := First (Aspect_Specifications (N));
12493 while Present (Asp) loop
12494 if Chars (Identifier (Asp)) = Name_Ghost then
12495 return Enables_Ghostness (Expression (Asp));
12496 end if;
12498 Next (Asp);
12499 end loop;
12500 end if;
12502 Decl := Empty;
12504 -- When the context is a [generic] package declaration, pragma Ghost
12505 -- resides in the visible declarations.
12507 if Nkind_In (N, N_Generic_Package_Declaration,
12508 N_Package_Declaration)
12509 then
12510 Decl := First (Visible_Declarations (Specification (N)));
12512 -- Otherwise pragma Ghost appears in the declarations following N
12514 elsif Is_List_Member (N) then
12515 Decl := Next (N);
12516 end if;
12518 while Present (Decl) loop
12519 if Nkind (Decl) = N_Pragma
12520 and then Pragma_Name (Decl) = Name_Ghost
12521 then
12522 return
12523 Enables_Ghostness (First (Pragma_Argument_Associations (Decl)));
12525 -- A source construct ends the region where pragma Ghost may appear,
12526 -- stop the traversal.
12528 elsif Comes_From_Source (Decl) then
12529 exit;
12530 end if;
12532 Next (Decl);
12533 end loop;
12535 return False;
12536 end Is_Subject_To_Ghost;
12538 --------------------------------------------------
12539 -- Is_Subprogram_Stub_Without_Prior_Declaration --
12540 --------------------------------------------------
12542 function Is_Subprogram_Stub_Without_Prior_Declaration
12543 (N : Node_Id) return Boolean
12545 begin
12546 -- A subprogram stub without prior declaration serves as declaration for
12547 -- the actual subprogram body. As such, it has an attached defining
12548 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
12550 return Nkind (N) = N_Subprogram_Body_Stub
12551 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
12552 end Is_Subprogram_Stub_Without_Prior_Declaration;
12554 ---------------------------------
12555 -- Is_Synchronized_Tagged_Type --
12556 ---------------------------------
12558 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
12559 Kind : constant Entity_Kind := Ekind (Base_Type (E));
12561 begin
12562 -- A task or protected type derived from an interface is a tagged type.
12563 -- Such a tagged type is called a synchronized tagged type, as are
12564 -- synchronized interfaces and private extensions whose declaration
12565 -- includes the reserved word synchronized.
12567 return (Is_Tagged_Type (E)
12568 and then (Kind = E_Task_Type
12569 or else
12570 Kind = E_Protected_Type))
12571 or else
12572 (Is_Interface (E)
12573 and then Is_Synchronized_Interface (E))
12574 or else
12575 (Ekind (E) = E_Record_Type_With_Private
12576 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
12577 and then (Synchronized_Present (Parent (E))
12578 or else Is_Synchronized_Interface (Etype (E))));
12579 end Is_Synchronized_Tagged_Type;
12581 -----------------
12582 -- Is_Transfer --
12583 -----------------
12585 function Is_Transfer (N : Node_Id) return Boolean is
12586 Kind : constant Node_Kind := Nkind (N);
12588 begin
12589 if Kind = N_Simple_Return_Statement
12590 or else
12591 Kind = N_Extended_Return_Statement
12592 or else
12593 Kind = N_Goto_Statement
12594 or else
12595 Kind = N_Raise_Statement
12596 or else
12597 Kind = N_Requeue_Statement
12598 then
12599 return True;
12601 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
12602 and then No (Condition (N))
12603 then
12604 return True;
12606 elsif Kind = N_Procedure_Call_Statement
12607 and then Is_Entity_Name (Name (N))
12608 and then Present (Entity (Name (N)))
12609 and then No_Return (Entity (Name (N)))
12610 then
12611 return True;
12613 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
12614 return True;
12616 else
12617 return False;
12618 end if;
12619 end Is_Transfer;
12621 -------------
12622 -- Is_True --
12623 -------------
12625 function Is_True (U : Uint) return Boolean is
12626 begin
12627 return (U /= 0);
12628 end Is_True;
12630 --------------------------------------
12631 -- Is_Unchecked_Conversion_Instance --
12632 --------------------------------------
12634 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
12635 Gen_Par : Entity_Id;
12637 begin
12638 -- Look for a function whose generic parent is the predefined intrinsic
12639 -- function Unchecked_Conversion.
12641 if Ekind (Id) = E_Function then
12642 Gen_Par := Generic_Parent (Parent (Id));
12644 return
12645 Present (Gen_Par)
12646 and then Chars (Gen_Par) = Name_Unchecked_Conversion
12647 and then Is_Intrinsic_Subprogram (Gen_Par)
12648 and then Is_Predefined_File_Name
12649 (Unit_File_Name (Get_Source_Unit (Gen_Par)));
12650 end if;
12652 return False;
12653 end Is_Unchecked_Conversion_Instance;
12655 -------------------------------
12656 -- Is_Universal_Numeric_Type --
12657 -------------------------------
12659 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
12660 begin
12661 return T = Universal_Integer or else T = Universal_Real;
12662 end Is_Universal_Numeric_Type;
12664 -------------------
12665 -- Is_Value_Type --
12666 -------------------
12668 function Is_Value_Type (T : Entity_Id) return Boolean is
12669 begin
12670 return VM_Target = CLI_Target
12671 and then Nkind (T) in N_Has_Chars
12672 and then Chars (T) /= No_Name
12673 and then Get_Name_String (Chars (T)) = "valuetype";
12674 end Is_Value_Type;
12676 ----------------------------
12677 -- Is_Variable_Size_Array --
12678 ----------------------------
12680 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
12681 Idx : Node_Id;
12683 begin
12684 pragma Assert (Is_Array_Type (E));
12686 -- Check if some index is initialized with a non-constant value
12688 Idx := First_Index (E);
12689 while Present (Idx) loop
12690 if Nkind (Idx) = N_Range then
12691 if not Is_Constant_Bound (Low_Bound (Idx))
12692 or else not Is_Constant_Bound (High_Bound (Idx))
12693 then
12694 return True;
12695 end if;
12696 end if;
12698 Idx := Next_Index (Idx);
12699 end loop;
12701 return False;
12702 end Is_Variable_Size_Array;
12704 -----------------------------
12705 -- Is_Variable_Size_Record --
12706 -----------------------------
12708 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
12709 Comp : Entity_Id;
12710 Comp_Typ : Entity_Id;
12712 begin
12713 pragma Assert (Is_Record_Type (E));
12715 Comp := First_Entity (E);
12716 while Present (Comp) loop
12717 Comp_Typ := Etype (Comp);
12719 -- Recursive call if the record type has discriminants
12721 if Is_Record_Type (Comp_Typ)
12722 and then Has_Discriminants (Comp_Typ)
12723 and then Is_Variable_Size_Record (Comp_Typ)
12724 then
12725 return True;
12727 elsif Is_Array_Type (Comp_Typ)
12728 and then Is_Variable_Size_Array (Comp_Typ)
12729 then
12730 return True;
12731 end if;
12733 Next_Entity (Comp);
12734 end loop;
12736 return False;
12737 end Is_Variable_Size_Record;
12739 -----------------
12740 -- Is_Variable --
12741 -----------------
12743 function Is_Variable
12744 (N : Node_Id;
12745 Use_Original_Node : Boolean := True) return Boolean
12747 Orig_Node : Node_Id;
12749 function In_Protected_Function (E : Entity_Id) return Boolean;
12750 -- Within a protected function, the private components of the enclosing
12751 -- protected type are constants. A function nested within a (protected)
12752 -- procedure is not itself protected. Within the body of a protected
12753 -- function the current instance of the protected type is a constant.
12755 function Is_Variable_Prefix (P : Node_Id) return Boolean;
12756 -- Prefixes can involve implicit dereferences, in which case we must
12757 -- test for the case of a reference of a constant access type, which can
12758 -- can never be a variable.
12760 ---------------------------
12761 -- In_Protected_Function --
12762 ---------------------------
12764 function In_Protected_Function (E : Entity_Id) return Boolean is
12765 Prot : Entity_Id;
12766 S : Entity_Id;
12768 begin
12769 -- E is the current instance of a type
12771 if Is_Type (E) then
12772 Prot := E;
12774 -- E is an object
12776 else
12777 Prot := Scope (E);
12778 end if;
12780 if not Is_Protected_Type (Prot) then
12781 return False;
12783 else
12784 S := Current_Scope;
12785 while Present (S) and then S /= Prot loop
12786 if Ekind (S) = E_Function and then Scope (S) = Prot then
12787 return True;
12788 end if;
12790 S := Scope (S);
12791 end loop;
12793 return False;
12794 end if;
12795 end In_Protected_Function;
12797 ------------------------
12798 -- Is_Variable_Prefix --
12799 ------------------------
12801 function Is_Variable_Prefix (P : Node_Id) return Boolean is
12802 begin
12803 if Is_Access_Type (Etype (P)) then
12804 return not Is_Access_Constant (Root_Type (Etype (P)));
12806 -- For the case of an indexed component whose prefix has a packed
12807 -- array type, the prefix has been rewritten into a type conversion.
12808 -- Determine variable-ness from the converted expression.
12810 elsif Nkind (P) = N_Type_Conversion
12811 and then not Comes_From_Source (P)
12812 and then Is_Array_Type (Etype (P))
12813 and then Is_Packed (Etype (P))
12814 then
12815 return Is_Variable (Expression (P));
12817 else
12818 return Is_Variable (P);
12819 end if;
12820 end Is_Variable_Prefix;
12822 -- Start of processing for Is_Variable
12824 begin
12825 -- Check if we perform the test on the original node since this may be a
12826 -- test of syntactic categories which must not be disturbed by whatever
12827 -- rewriting might have occurred. For example, an aggregate, which is
12828 -- certainly NOT a variable, could be turned into a variable by
12829 -- expansion.
12831 if Use_Original_Node then
12832 Orig_Node := Original_Node (N);
12833 else
12834 Orig_Node := N;
12835 end if;
12837 -- Definitely OK if Assignment_OK is set. Since this is something that
12838 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
12840 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
12841 return True;
12843 -- Normally we go to the original node, but there is one exception where
12844 -- we use the rewritten node, namely when it is an explicit dereference.
12845 -- The generated code may rewrite a prefix which is an access type with
12846 -- an explicit dereference. The dereference is a variable, even though
12847 -- the original node may not be (since it could be a constant of the
12848 -- access type).
12850 -- In Ada 2005 we have a further case to consider: the prefix may be a
12851 -- function call given in prefix notation. The original node appears to
12852 -- be a selected component, but we need to examine the call.
12854 elsif Nkind (N) = N_Explicit_Dereference
12855 and then Nkind (Orig_Node) /= N_Explicit_Dereference
12856 and then Present (Etype (Orig_Node))
12857 and then Is_Access_Type (Etype (Orig_Node))
12858 then
12859 -- Note that if the prefix is an explicit dereference that does not
12860 -- come from source, we must check for a rewritten function call in
12861 -- prefixed notation before other forms of rewriting, to prevent a
12862 -- compiler crash.
12864 return
12865 (Nkind (Orig_Node) = N_Function_Call
12866 and then not Is_Access_Constant (Etype (Prefix (N))))
12867 or else
12868 Is_Variable_Prefix (Original_Node (Prefix (N)));
12870 -- in Ada 2012, the dereference may have been added for a type with
12871 -- a declared implicit dereference aspect. Check that it is not an
12872 -- access to constant.
12874 elsif Nkind (N) = N_Explicit_Dereference
12875 and then Present (Etype (Orig_Node))
12876 and then Ada_Version >= Ada_2012
12877 and then Has_Implicit_Dereference (Etype (Orig_Node))
12878 then
12879 return not Is_Access_Constant (Etype (Prefix (N)));
12881 -- A function call is never a variable
12883 elsif Nkind (N) = N_Function_Call then
12884 return False;
12886 -- All remaining checks use the original node
12888 elsif Is_Entity_Name (Orig_Node)
12889 and then Present (Entity (Orig_Node))
12890 then
12891 declare
12892 E : constant Entity_Id := Entity (Orig_Node);
12893 K : constant Entity_Kind := Ekind (E);
12895 begin
12896 return (K = E_Variable
12897 and then Nkind (Parent (E)) /= N_Exception_Handler)
12898 or else (K = E_Component
12899 and then not In_Protected_Function (E))
12900 or else K = E_Out_Parameter
12901 or else K = E_In_Out_Parameter
12902 or else K = E_Generic_In_Out_Parameter
12904 -- Current instance of type. If this is a protected type, check
12905 -- we are not within the body of one of its protected functions.
12907 or else (Is_Type (E)
12908 and then In_Open_Scopes (E)
12909 and then not In_Protected_Function (E))
12911 or else (Is_Incomplete_Or_Private_Type (E)
12912 and then In_Open_Scopes (Full_View (E)));
12913 end;
12915 else
12916 case Nkind (Orig_Node) is
12917 when N_Indexed_Component | N_Slice =>
12918 return Is_Variable_Prefix (Prefix (Orig_Node));
12920 when N_Selected_Component =>
12921 return (Is_Variable (Selector_Name (Orig_Node))
12922 and then Is_Variable_Prefix (Prefix (Orig_Node)))
12923 or else
12924 (Nkind (N) = N_Expanded_Name
12925 and then Scope (Entity (N)) = Entity (Prefix (N)));
12927 -- For an explicit dereference, the type of the prefix cannot
12928 -- be an access to constant or an access to subprogram.
12930 when N_Explicit_Dereference =>
12931 declare
12932 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
12933 begin
12934 return Is_Access_Type (Typ)
12935 and then not Is_Access_Constant (Root_Type (Typ))
12936 and then Ekind (Typ) /= E_Access_Subprogram_Type;
12937 end;
12939 -- The type conversion is the case where we do not deal with the
12940 -- context dependent special case of an actual parameter. Thus
12941 -- the type conversion is only considered a variable for the
12942 -- purposes of this routine if the target type is tagged. However,
12943 -- a type conversion is considered to be a variable if it does not
12944 -- come from source (this deals for example with the conversions
12945 -- of expressions to their actual subtypes).
12947 when N_Type_Conversion =>
12948 return Is_Variable (Expression (Orig_Node))
12949 and then
12950 (not Comes_From_Source (Orig_Node)
12951 or else
12952 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
12953 and then
12954 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
12956 -- GNAT allows an unchecked type conversion as a variable. This
12957 -- only affects the generation of internal expanded code, since
12958 -- calls to instantiations of Unchecked_Conversion are never
12959 -- considered variables (since they are function calls).
12961 when N_Unchecked_Type_Conversion =>
12962 return Is_Variable (Expression (Orig_Node));
12964 when others =>
12965 return False;
12966 end case;
12967 end if;
12968 end Is_Variable;
12970 ---------------------------
12971 -- Is_Visibly_Controlled --
12972 ---------------------------
12974 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
12975 Root : constant Entity_Id := Root_Type (T);
12976 begin
12977 return Chars (Scope (Root)) = Name_Finalization
12978 and then Chars (Scope (Scope (Root))) = Name_Ada
12979 and then Scope (Scope (Scope (Root))) = Standard_Standard;
12980 end Is_Visibly_Controlled;
12982 ------------------------
12983 -- Is_Volatile_Object --
12984 ------------------------
12986 function Is_Volatile_Object (N : Node_Id) return Boolean is
12988 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
12989 -- If prefix is an implicit dereference, examine designated type
12991 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
12992 -- Determines if given object has volatile components
12994 ------------------------
12995 -- Is_Volatile_Prefix --
12996 ------------------------
12998 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
12999 Typ : constant Entity_Id := Etype (N);
13001 begin
13002 if Is_Access_Type (Typ) then
13003 declare
13004 Dtyp : constant Entity_Id := Designated_Type (Typ);
13006 begin
13007 return Is_Volatile (Dtyp)
13008 or else Has_Volatile_Components (Dtyp);
13009 end;
13011 else
13012 return Object_Has_Volatile_Components (N);
13013 end if;
13014 end Is_Volatile_Prefix;
13016 ------------------------------------
13017 -- Object_Has_Volatile_Components --
13018 ------------------------------------
13020 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
13021 Typ : constant Entity_Id := Etype (N);
13023 begin
13024 if Is_Volatile (Typ)
13025 or else Has_Volatile_Components (Typ)
13026 then
13027 return True;
13029 elsif Is_Entity_Name (N)
13030 and then (Has_Volatile_Components (Entity (N))
13031 or else Is_Volatile (Entity (N)))
13032 then
13033 return True;
13035 elsif Nkind (N) = N_Indexed_Component
13036 or else Nkind (N) = N_Selected_Component
13037 then
13038 return Is_Volatile_Prefix (Prefix (N));
13040 else
13041 return False;
13042 end if;
13043 end Object_Has_Volatile_Components;
13045 -- Start of processing for Is_Volatile_Object
13047 begin
13048 if Nkind (N) = N_Defining_Identifier then
13049 return Is_Volatile (N) or else Is_Volatile (Etype (N));
13051 elsif Nkind (N) = N_Expanded_Name then
13052 return Is_Volatile_Object (Entity (N));
13054 elsif Is_Volatile (Etype (N))
13055 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
13056 then
13057 return True;
13059 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
13060 and then Is_Volatile_Prefix (Prefix (N))
13061 then
13062 return True;
13064 elsif Nkind (N) = N_Selected_Component
13065 and then Is_Volatile (Entity (Selector_Name (N)))
13066 then
13067 return True;
13069 else
13070 return False;
13071 end if;
13072 end Is_Volatile_Object;
13074 ---------------------------
13075 -- Itype_Has_Declaration --
13076 ---------------------------
13078 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
13079 begin
13080 pragma Assert (Is_Itype (Id));
13081 return Present (Parent (Id))
13082 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
13083 N_Subtype_Declaration)
13084 and then Defining_Entity (Parent (Id)) = Id;
13085 end Itype_Has_Declaration;
13087 -------------------------
13088 -- Kill_Current_Values --
13089 -------------------------
13091 procedure Kill_Current_Values
13092 (Ent : Entity_Id;
13093 Last_Assignment_Only : Boolean := False)
13095 begin
13096 if Is_Assignable (Ent) then
13097 Set_Last_Assignment (Ent, Empty);
13098 end if;
13100 if Is_Object (Ent) then
13101 if not Last_Assignment_Only then
13102 Kill_Checks (Ent);
13103 Set_Current_Value (Ent, Empty);
13105 if not Can_Never_Be_Null (Ent) then
13106 Set_Is_Known_Non_Null (Ent, False);
13107 end if;
13109 Set_Is_Known_Null (Ent, False);
13111 -- Reset Is_Known_Valid unless type is always valid, or if we have
13112 -- a loop parameter (loop parameters are always valid, since their
13113 -- bounds are defined by the bounds given in the loop header).
13115 if not Is_Known_Valid (Etype (Ent))
13116 and then Ekind (Ent) /= E_Loop_Parameter
13117 then
13118 Set_Is_Known_Valid (Ent, False);
13119 end if;
13120 end if;
13121 end if;
13122 end Kill_Current_Values;
13124 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
13125 S : Entity_Id;
13127 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
13128 -- Clear current value for entity E and all entities chained to E
13130 ------------------------------------------
13131 -- Kill_Current_Values_For_Entity_Chain --
13132 ------------------------------------------
13134 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
13135 Ent : Entity_Id;
13136 begin
13137 Ent := E;
13138 while Present (Ent) loop
13139 Kill_Current_Values (Ent, Last_Assignment_Only);
13140 Next_Entity (Ent);
13141 end loop;
13142 end Kill_Current_Values_For_Entity_Chain;
13144 -- Start of processing for Kill_Current_Values
13146 begin
13147 -- Kill all saved checks, a special case of killing saved values
13149 if not Last_Assignment_Only then
13150 Kill_All_Checks;
13151 end if;
13153 -- Loop through relevant scopes, which includes the current scope and
13154 -- any parent scopes if the current scope is a block or a package.
13156 S := Current_Scope;
13157 Scope_Loop : loop
13159 -- Clear current values of all entities in current scope
13161 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
13163 -- If scope is a package, also clear current values of all private
13164 -- entities in the scope.
13166 if Is_Package_Or_Generic_Package (S)
13167 or else Is_Concurrent_Type (S)
13168 then
13169 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
13170 end if;
13172 -- If this is a not a subprogram, deal with parents
13174 if not Is_Subprogram (S) then
13175 S := Scope (S);
13176 exit Scope_Loop when S = Standard_Standard;
13177 else
13178 exit Scope_Loop;
13179 end if;
13180 end loop Scope_Loop;
13181 end Kill_Current_Values;
13183 --------------------------
13184 -- Kill_Size_Check_Code --
13185 --------------------------
13187 procedure Kill_Size_Check_Code (E : Entity_Id) is
13188 begin
13189 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
13190 and then Present (Size_Check_Code (E))
13191 then
13192 Remove (Size_Check_Code (E));
13193 Set_Size_Check_Code (E, Empty);
13194 end if;
13195 end Kill_Size_Check_Code;
13197 --------------------------
13198 -- Known_To_Be_Assigned --
13199 --------------------------
13201 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
13202 P : constant Node_Id := Parent (N);
13204 begin
13205 case Nkind (P) is
13207 -- Test left side of assignment
13209 when N_Assignment_Statement =>
13210 return N = Name (P);
13212 -- Function call arguments are never lvalues
13214 when N_Function_Call =>
13215 return False;
13217 -- Positional parameter for procedure or accept call
13219 when N_Procedure_Call_Statement |
13220 N_Accept_Statement
13222 declare
13223 Proc : Entity_Id;
13224 Form : Entity_Id;
13225 Act : Node_Id;
13227 begin
13228 Proc := Get_Subprogram_Entity (P);
13230 if No (Proc) then
13231 return False;
13232 end if;
13234 -- If we are not a list member, something is strange, so
13235 -- be conservative and return False.
13237 if not Is_List_Member (N) then
13238 return False;
13239 end if;
13241 -- We are going to find the right formal by stepping forward
13242 -- through the formals, as we step backwards in the actuals.
13244 Form := First_Formal (Proc);
13245 Act := N;
13246 loop
13247 -- If no formal, something is weird, so be conservative
13248 -- and return False.
13250 if No (Form) then
13251 return False;
13252 end if;
13254 Prev (Act);
13255 exit when No (Act);
13256 Next_Formal (Form);
13257 end loop;
13259 return Ekind (Form) /= E_In_Parameter;
13260 end;
13262 -- Named parameter for procedure or accept call
13264 when N_Parameter_Association =>
13265 declare
13266 Proc : Entity_Id;
13267 Form : Entity_Id;
13269 begin
13270 Proc := Get_Subprogram_Entity (Parent (P));
13272 if No (Proc) then
13273 return False;
13274 end if;
13276 -- Loop through formals to find the one that matches
13278 Form := First_Formal (Proc);
13279 loop
13280 -- If no matching formal, that's peculiar, some kind of
13281 -- previous error, so return False to be conservative.
13282 -- Actually this also happens in legal code in the case
13283 -- where P is a parameter association for an Extra_Formal???
13285 if No (Form) then
13286 return False;
13287 end if;
13289 -- Else test for match
13291 if Chars (Form) = Chars (Selector_Name (P)) then
13292 return Ekind (Form) /= E_In_Parameter;
13293 end if;
13295 Next_Formal (Form);
13296 end loop;
13297 end;
13299 -- Test for appearing in a conversion that itself appears
13300 -- in an lvalue context, since this should be an lvalue.
13302 when N_Type_Conversion =>
13303 return Known_To_Be_Assigned (P);
13305 -- All other references are definitely not known to be modifications
13307 when others =>
13308 return False;
13310 end case;
13311 end Known_To_Be_Assigned;
13313 ---------------------------
13314 -- Last_Source_Statement --
13315 ---------------------------
13317 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
13318 N : Node_Id;
13320 begin
13321 N := Last (Statements (HSS));
13322 while Present (N) loop
13323 exit when Comes_From_Source (N);
13324 Prev (N);
13325 end loop;
13327 return N;
13328 end Last_Source_Statement;
13330 ----------------------------------
13331 -- Matching_Static_Array_Bounds --
13332 ----------------------------------
13334 function Matching_Static_Array_Bounds
13335 (L_Typ : Node_Id;
13336 R_Typ : Node_Id) return Boolean
13338 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
13339 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
13341 L_Index : Node_Id;
13342 R_Index : Node_Id;
13343 L_Low : Node_Id;
13344 L_High : Node_Id;
13345 L_Len : Uint;
13346 R_Low : Node_Id;
13347 R_High : Node_Id;
13348 R_Len : Uint;
13350 begin
13351 if L_Ndims /= R_Ndims then
13352 return False;
13353 end if;
13355 -- Unconstrained types do not have static bounds
13357 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
13358 return False;
13359 end if;
13361 -- First treat specially the first dimension, as the lower bound and
13362 -- length of string literals are not stored like those of arrays.
13364 if Ekind (L_Typ) = E_String_Literal_Subtype then
13365 L_Low := String_Literal_Low_Bound (L_Typ);
13366 L_Len := String_Literal_Length (L_Typ);
13367 else
13368 L_Index := First_Index (L_Typ);
13369 Get_Index_Bounds (L_Index, L_Low, L_High);
13371 if Is_OK_Static_Expression (L_Low)
13372 and then
13373 Is_OK_Static_Expression (L_High)
13374 then
13375 if Expr_Value (L_High) < Expr_Value (L_Low) then
13376 L_Len := Uint_0;
13377 else
13378 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
13379 end if;
13380 else
13381 return False;
13382 end if;
13383 end if;
13385 if Ekind (R_Typ) = E_String_Literal_Subtype then
13386 R_Low := String_Literal_Low_Bound (R_Typ);
13387 R_Len := String_Literal_Length (R_Typ);
13388 else
13389 R_Index := First_Index (R_Typ);
13390 Get_Index_Bounds (R_Index, R_Low, R_High);
13392 if Is_OK_Static_Expression (R_Low)
13393 and then
13394 Is_OK_Static_Expression (R_High)
13395 then
13396 if Expr_Value (R_High) < Expr_Value (R_Low) then
13397 R_Len := Uint_0;
13398 else
13399 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
13400 end if;
13401 else
13402 return False;
13403 end if;
13404 end if;
13406 if (Is_OK_Static_Expression (L_Low)
13407 and then
13408 Is_OK_Static_Expression (R_Low))
13409 and then Expr_Value (L_Low) = Expr_Value (R_Low)
13410 and then L_Len = R_Len
13411 then
13412 null;
13413 else
13414 return False;
13415 end if;
13417 -- Then treat all other dimensions
13419 for Indx in 2 .. L_Ndims loop
13420 Next (L_Index);
13421 Next (R_Index);
13423 Get_Index_Bounds (L_Index, L_Low, L_High);
13424 Get_Index_Bounds (R_Index, R_Low, R_High);
13426 if (Is_OK_Static_Expression (L_Low) and then
13427 Is_OK_Static_Expression (L_High) and then
13428 Is_OK_Static_Expression (R_Low) and then
13429 Is_OK_Static_Expression (R_High))
13430 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
13431 and then
13432 Expr_Value (L_High) = Expr_Value (R_High))
13433 then
13434 null;
13435 else
13436 return False;
13437 end if;
13438 end loop;
13440 -- If we fall through the loop, all indexes matched
13442 return True;
13443 end Matching_Static_Array_Bounds;
13445 -------------------
13446 -- May_Be_Lvalue --
13447 -------------------
13449 function May_Be_Lvalue (N : Node_Id) return Boolean is
13450 P : constant Node_Id := Parent (N);
13452 begin
13453 case Nkind (P) is
13455 -- Test left side of assignment
13457 when N_Assignment_Statement =>
13458 return N = Name (P);
13460 -- Test prefix of component or attribute. Note that the prefix of an
13461 -- explicit or implicit dereference cannot be an l-value.
13463 when N_Attribute_Reference =>
13464 return N = Prefix (P)
13465 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
13467 -- For an expanded name, the name is an lvalue if the expanded name
13468 -- is an lvalue, but the prefix is never an lvalue, since it is just
13469 -- the scope where the name is found.
13471 when N_Expanded_Name =>
13472 if N = Prefix (P) then
13473 return May_Be_Lvalue (P);
13474 else
13475 return False;
13476 end if;
13478 -- For a selected component A.B, A is certainly an lvalue if A.B is.
13479 -- B is a little interesting, if we have A.B := 3, there is some
13480 -- discussion as to whether B is an lvalue or not, we choose to say
13481 -- it is. Note however that A is not an lvalue if it is of an access
13482 -- type since this is an implicit dereference.
13484 when N_Selected_Component =>
13485 if N = Prefix (P)
13486 and then Present (Etype (N))
13487 and then Is_Access_Type (Etype (N))
13488 then
13489 return False;
13490 else
13491 return May_Be_Lvalue (P);
13492 end if;
13494 -- For an indexed component or slice, the index or slice bounds is
13495 -- never an lvalue. The prefix is an lvalue if the indexed component
13496 -- or slice is an lvalue, except if it is an access type, where we
13497 -- have an implicit dereference.
13499 when N_Indexed_Component | N_Slice =>
13500 if N /= Prefix (P)
13501 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
13502 then
13503 return False;
13504 else
13505 return May_Be_Lvalue (P);
13506 end if;
13508 -- Prefix of a reference is an lvalue if the reference is an lvalue
13510 when N_Reference =>
13511 return May_Be_Lvalue (P);
13513 -- Prefix of explicit dereference is never an lvalue
13515 when N_Explicit_Dereference =>
13516 return False;
13518 -- Positional parameter for subprogram, entry, or accept call.
13519 -- In older versions of Ada function call arguments are never
13520 -- lvalues. In Ada 2012 functions can have in-out parameters.
13522 when N_Subprogram_Call |
13523 N_Entry_Call_Statement |
13524 N_Accept_Statement
13526 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
13527 return False;
13528 end if;
13530 -- The following mechanism is clumsy and fragile. A single flag
13531 -- set in Resolve_Actuals would be preferable ???
13533 declare
13534 Proc : Entity_Id;
13535 Form : Entity_Id;
13536 Act : Node_Id;
13538 begin
13539 Proc := Get_Subprogram_Entity (P);
13541 if No (Proc) then
13542 return True;
13543 end if;
13545 -- If we are not a list member, something is strange, so be
13546 -- conservative and return True.
13548 if not Is_List_Member (N) then
13549 return True;
13550 end if;
13552 -- We are going to find the right formal by stepping forward
13553 -- through the formals, as we step backwards in the actuals.
13555 Form := First_Formal (Proc);
13556 Act := N;
13557 loop
13558 -- If no formal, something is weird, so be conservative and
13559 -- return True.
13561 if No (Form) then
13562 return True;
13563 end if;
13565 Prev (Act);
13566 exit when No (Act);
13567 Next_Formal (Form);
13568 end loop;
13570 return Ekind (Form) /= E_In_Parameter;
13571 end;
13573 -- Named parameter for procedure or accept call
13575 when N_Parameter_Association =>
13576 declare
13577 Proc : Entity_Id;
13578 Form : Entity_Id;
13580 begin
13581 Proc := Get_Subprogram_Entity (Parent (P));
13583 if No (Proc) then
13584 return True;
13585 end if;
13587 -- Loop through formals to find the one that matches
13589 Form := First_Formal (Proc);
13590 loop
13591 -- If no matching formal, that's peculiar, some kind of
13592 -- previous error, so return True to be conservative.
13593 -- Actually happens with legal code for an unresolved call
13594 -- where we may get the wrong homonym???
13596 if No (Form) then
13597 return True;
13598 end if;
13600 -- Else test for match
13602 if Chars (Form) = Chars (Selector_Name (P)) then
13603 return Ekind (Form) /= E_In_Parameter;
13604 end if;
13606 Next_Formal (Form);
13607 end loop;
13608 end;
13610 -- Test for appearing in a conversion that itself appears in an
13611 -- lvalue context, since this should be an lvalue.
13613 when N_Type_Conversion =>
13614 return May_Be_Lvalue (P);
13616 -- Test for appearance in object renaming declaration
13618 when N_Object_Renaming_Declaration =>
13619 return True;
13621 -- All other references are definitely not lvalues
13623 when others =>
13624 return False;
13626 end case;
13627 end May_Be_Lvalue;
13629 -----------------------
13630 -- Mark_Coextensions --
13631 -----------------------
13633 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
13634 Is_Dynamic : Boolean;
13635 -- Indicates whether the context causes nested coextensions to be
13636 -- dynamic or static
13638 function Mark_Allocator (N : Node_Id) return Traverse_Result;
13639 -- Recognize an allocator node and label it as a dynamic coextension
13641 --------------------
13642 -- Mark_Allocator --
13643 --------------------
13645 function Mark_Allocator (N : Node_Id) return Traverse_Result is
13646 begin
13647 if Nkind (N) = N_Allocator then
13648 if Is_Dynamic then
13649 Set_Is_Dynamic_Coextension (N);
13651 -- If the allocator expression is potentially dynamic, it may
13652 -- be expanded out of order and require dynamic allocation
13653 -- anyway, so we treat the coextension itself as dynamic.
13654 -- Potential optimization ???
13656 elsif Nkind (Expression (N)) = N_Qualified_Expression
13657 and then Nkind (Expression (Expression (N))) = N_Op_Concat
13658 then
13659 Set_Is_Dynamic_Coextension (N);
13660 else
13661 Set_Is_Static_Coextension (N);
13662 end if;
13663 end if;
13665 return OK;
13666 end Mark_Allocator;
13668 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
13670 -- Start of processing Mark_Coextensions
13672 begin
13673 case Nkind (Context_Nod) is
13675 -- Comment here ???
13677 when N_Assignment_Statement =>
13678 Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
13680 -- An allocator that is a component of a returned aggregate
13681 -- must be dynamic.
13683 when N_Simple_Return_Statement =>
13684 declare
13685 Expr : constant Node_Id := Expression (Context_Nod);
13686 begin
13687 Is_Dynamic :=
13688 Nkind (Expr) = N_Allocator
13689 or else
13690 (Nkind (Expr) = N_Qualified_Expression
13691 and then Nkind (Expression (Expr)) = N_Aggregate);
13692 end;
13694 -- An alloctor within an object declaration in an extended return
13695 -- statement is of necessity dynamic.
13697 when N_Object_Declaration =>
13698 Is_Dynamic := Nkind (Root_Nod) = N_Allocator
13699 or else
13700 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
13702 -- This routine should not be called for constructs which may not
13703 -- contain coextensions.
13705 when others =>
13706 raise Program_Error;
13707 end case;
13709 Mark_Allocators (Root_Nod);
13710 end Mark_Coextensions;
13712 ----------------------
13713 -- Needs_One_Actual --
13714 ----------------------
13716 function Needs_One_Actual (E : Entity_Id) return Boolean is
13717 Formal : Entity_Id;
13719 begin
13720 -- Ada 2005 or later, and formals present
13722 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
13723 Formal := Next_Formal (First_Formal (E));
13724 while Present (Formal) loop
13725 if No (Default_Value (Formal)) then
13726 return False;
13727 end if;
13729 Next_Formal (Formal);
13730 end loop;
13732 return True;
13734 -- Ada 83/95 or no formals
13736 else
13737 return False;
13738 end if;
13739 end Needs_One_Actual;
13741 ------------------------
13742 -- New_Copy_List_Tree --
13743 ------------------------
13745 function New_Copy_List_Tree (List : List_Id) return List_Id is
13746 NL : List_Id;
13747 E : Node_Id;
13749 begin
13750 if List = No_List then
13751 return No_List;
13753 else
13754 NL := New_List;
13755 E := First (List);
13757 while Present (E) loop
13758 Append (New_Copy_Tree (E), NL);
13759 E := Next (E);
13760 end loop;
13762 return NL;
13763 end if;
13764 end New_Copy_List_Tree;
13766 --------------------------------------------------
13767 -- New_Copy_Tree Auxiliary Data and Subprograms --
13768 --------------------------------------------------
13770 use Atree.Unchecked_Access;
13771 use Atree_Private_Part;
13773 -- Our approach here requires a two pass traversal of the tree. The
13774 -- first pass visits all nodes that eventually will be copied looking
13775 -- for defining Itypes. If any defining Itypes are found, then they are
13776 -- copied, and an entry is added to the replacement map. In the second
13777 -- phase, the tree is copied, using the replacement map to replace any
13778 -- Itype references within the copied tree.
13780 -- The following hash tables are used if the Map supplied has more
13781 -- than hash threshold entries to speed up access to the map. If
13782 -- there are fewer entries, then the map is searched sequentially
13783 -- (because setting up a hash table for only a few entries takes
13784 -- more time than it saves.
13786 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
13787 -- Hash function used for hash operations
13789 -------------------
13790 -- New_Copy_Hash --
13791 -------------------
13793 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
13794 begin
13795 return Nat (E) mod (NCT_Header_Num'Last + 1);
13796 end New_Copy_Hash;
13798 ---------------
13799 -- NCT_Assoc --
13800 ---------------
13802 -- The hash table NCT_Assoc associates old entities in the table
13803 -- with their corresponding new entities (i.e. the pairs of entries
13804 -- presented in the original Map argument are Key-Element pairs).
13806 package NCT_Assoc is new Simple_HTable (
13807 Header_Num => NCT_Header_Num,
13808 Element => Entity_Id,
13809 No_Element => Empty,
13810 Key => Entity_Id,
13811 Hash => New_Copy_Hash,
13812 Equal => Types."=");
13814 ---------------------
13815 -- NCT_Itype_Assoc --
13816 ---------------------
13818 -- The hash table NCT_Itype_Assoc contains entries only for those
13819 -- old nodes which have a non-empty Associated_Node_For_Itype set.
13820 -- The key is the associated node, and the element is the new node
13821 -- itself (NOT the associated node for the new node).
13823 package NCT_Itype_Assoc is new Simple_HTable (
13824 Header_Num => NCT_Header_Num,
13825 Element => Entity_Id,
13826 No_Element => Empty,
13827 Key => Entity_Id,
13828 Hash => New_Copy_Hash,
13829 Equal => Types."=");
13831 -------------------
13832 -- New_Copy_Tree --
13833 -------------------
13835 function New_Copy_Tree
13836 (Source : Node_Id;
13837 Map : Elist_Id := No_Elist;
13838 New_Sloc : Source_Ptr := No_Location;
13839 New_Scope : Entity_Id := Empty) return Node_Id
13841 Actual_Map : Elist_Id := Map;
13842 -- This is the actual map for the copy. It is initialized with the
13843 -- given elements, and then enlarged as required for Itypes that are
13844 -- copied during the first phase of the copy operation. The visit
13845 -- procedures add elements to this map as Itypes are encountered.
13846 -- The reason we cannot use Map directly, is that it may well be
13847 -- (and normally is) initialized to No_Elist, and if we have mapped
13848 -- entities, we have to reset it to point to a real Elist.
13850 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
13851 -- Called during second phase to map entities into their corresponding
13852 -- copies using Actual_Map. If the argument is not an entity, or is not
13853 -- in Actual_Map, then it is returned unchanged.
13855 procedure Build_NCT_Hash_Tables;
13856 -- Builds hash tables (number of elements >= threshold value)
13858 function Copy_Elist_With_Replacement
13859 (Old_Elist : Elist_Id) return Elist_Id;
13860 -- Called during second phase to copy element list doing replacements
13862 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
13863 -- Called during the second phase to process a copied Itype. The actual
13864 -- copy happened during the first phase (so that we could make the entry
13865 -- in the mapping), but we still have to deal with the descendents of
13866 -- the copied Itype and copy them where necessary.
13868 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
13869 -- Called during second phase to copy list doing replacements
13871 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
13872 -- Called during second phase to copy node doing replacements
13874 procedure Visit_Elist (E : Elist_Id);
13875 -- Called during first phase to visit all elements of an Elist
13877 procedure Visit_Field (F : Union_Id; N : Node_Id);
13878 -- Visit a single field, recursing to call Visit_Node or Visit_List
13879 -- if the field is a syntactic descendent of the current node (i.e.
13880 -- its parent is Node N).
13882 procedure Visit_Itype (Old_Itype : Entity_Id);
13883 -- Called during first phase to visit subsidiary fields of a defining
13884 -- Itype, and also create a copy and make an entry in the replacement
13885 -- map for the new copy.
13887 procedure Visit_List (L : List_Id);
13888 -- Called during first phase to visit all elements of a List
13890 procedure Visit_Node (N : Node_Or_Entity_Id);
13891 -- Called during first phase to visit a node and all its subtrees
13893 -----------
13894 -- Assoc --
13895 -----------
13897 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
13898 E : Elmt_Id;
13899 Ent : Entity_Id;
13901 begin
13902 if not Has_Extension (N) or else No (Actual_Map) then
13903 return N;
13905 elsif NCT_Hash_Tables_Used then
13906 Ent := NCT_Assoc.Get (Entity_Id (N));
13908 if Present (Ent) then
13909 return Ent;
13910 else
13911 return N;
13912 end if;
13914 -- No hash table used, do serial search
13916 else
13917 E := First_Elmt (Actual_Map);
13918 while Present (E) loop
13919 if Node (E) = N then
13920 return Node (Next_Elmt (E));
13921 else
13922 E := Next_Elmt (Next_Elmt (E));
13923 end if;
13924 end loop;
13925 end if;
13927 return N;
13928 end Assoc;
13930 ---------------------------
13931 -- Build_NCT_Hash_Tables --
13932 ---------------------------
13934 procedure Build_NCT_Hash_Tables is
13935 Elmt : Elmt_Id;
13936 Ent : Entity_Id;
13937 begin
13938 if NCT_Hash_Table_Setup then
13939 NCT_Assoc.Reset;
13940 NCT_Itype_Assoc.Reset;
13941 end if;
13943 Elmt := First_Elmt (Actual_Map);
13944 while Present (Elmt) loop
13945 Ent := Node (Elmt);
13947 -- Get new entity, and associate old and new
13949 Next_Elmt (Elmt);
13950 NCT_Assoc.Set (Ent, Node (Elmt));
13952 if Is_Type (Ent) then
13953 declare
13954 Anode : constant Entity_Id :=
13955 Associated_Node_For_Itype (Ent);
13957 begin
13958 if Present (Anode) then
13960 -- Enter a link between the associated node of the
13961 -- old Itype and the new Itype, for updating later
13962 -- when node is copied.
13964 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
13965 end if;
13966 end;
13967 end if;
13969 Next_Elmt (Elmt);
13970 end loop;
13972 NCT_Hash_Tables_Used := True;
13973 NCT_Hash_Table_Setup := True;
13974 end Build_NCT_Hash_Tables;
13976 ---------------------------------
13977 -- Copy_Elist_With_Replacement --
13978 ---------------------------------
13980 function Copy_Elist_With_Replacement
13981 (Old_Elist : Elist_Id) return Elist_Id
13983 M : Elmt_Id;
13984 New_Elist : Elist_Id;
13986 begin
13987 if No (Old_Elist) then
13988 return No_Elist;
13990 else
13991 New_Elist := New_Elmt_List;
13993 M := First_Elmt (Old_Elist);
13994 while Present (M) loop
13995 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
13996 Next_Elmt (M);
13997 end loop;
13998 end if;
14000 return New_Elist;
14001 end Copy_Elist_With_Replacement;
14003 ---------------------------------
14004 -- Copy_Itype_With_Replacement --
14005 ---------------------------------
14007 -- This routine exactly parallels its phase one analog Visit_Itype,
14009 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
14010 begin
14011 -- Translate Next_Entity, Scope and Etype fields, in case they
14012 -- reference entities that have been mapped into copies.
14014 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
14015 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
14017 if Present (New_Scope) then
14018 Set_Scope (New_Itype, New_Scope);
14019 else
14020 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
14021 end if;
14023 -- Copy referenced fields
14025 if Is_Discrete_Type (New_Itype) then
14026 Set_Scalar_Range (New_Itype,
14027 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
14029 elsif Has_Discriminants (Base_Type (New_Itype)) then
14030 Set_Discriminant_Constraint (New_Itype,
14031 Copy_Elist_With_Replacement
14032 (Discriminant_Constraint (New_Itype)));
14034 elsif Is_Array_Type (New_Itype) then
14035 if Present (First_Index (New_Itype)) then
14036 Set_First_Index (New_Itype,
14037 First (Copy_List_With_Replacement
14038 (List_Containing (First_Index (New_Itype)))));
14039 end if;
14041 if Is_Packed (New_Itype) then
14042 Set_Packed_Array_Impl_Type (New_Itype,
14043 Copy_Node_With_Replacement
14044 (Packed_Array_Impl_Type (New_Itype)));
14045 end if;
14046 end if;
14047 end Copy_Itype_With_Replacement;
14049 --------------------------------
14050 -- Copy_List_With_Replacement --
14051 --------------------------------
14053 function Copy_List_With_Replacement
14054 (Old_List : List_Id) return List_Id
14056 New_List : List_Id;
14057 E : Node_Id;
14059 begin
14060 if Old_List = No_List then
14061 return No_List;
14063 else
14064 New_List := Empty_List;
14066 E := First (Old_List);
14067 while Present (E) loop
14068 Append (Copy_Node_With_Replacement (E), New_List);
14069 Next (E);
14070 end loop;
14072 return New_List;
14073 end if;
14074 end Copy_List_With_Replacement;
14076 --------------------------------
14077 -- Copy_Node_With_Replacement --
14078 --------------------------------
14080 function Copy_Node_With_Replacement
14081 (Old_Node : Node_Id) return Node_Id
14083 New_Node : Node_Id;
14085 procedure Adjust_Named_Associations
14086 (Old_Node : Node_Id;
14087 New_Node : Node_Id);
14088 -- If a call node has named associations, these are chained through
14089 -- the First_Named_Actual, Next_Named_Actual links. These must be
14090 -- propagated separately to the new parameter list, because these
14091 -- are not syntactic fields.
14093 function Copy_Field_With_Replacement
14094 (Field : Union_Id) return Union_Id;
14095 -- Given Field, which is a field of Old_Node, return a copy of it
14096 -- if it is a syntactic field (i.e. its parent is Node), setting
14097 -- the parent of the copy to poit to New_Node. Otherwise returns
14098 -- the field (possibly mapped if it is an entity).
14100 -------------------------------
14101 -- Adjust_Named_Associations --
14102 -------------------------------
14104 procedure Adjust_Named_Associations
14105 (Old_Node : Node_Id;
14106 New_Node : Node_Id)
14108 Old_E : Node_Id;
14109 New_E : Node_Id;
14111 Old_Next : Node_Id;
14112 New_Next : Node_Id;
14114 begin
14115 Old_E := First (Parameter_Associations (Old_Node));
14116 New_E := First (Parameter_Associations (New_Node));
14117 while Present (Old_E) loop
14118 if Nkind (Old_E) = N_Parameter_Association
14119 and then Present (Next_Named_Actual (Old_E))
14120 then
14121 if First_Named_Actual (Old_Node)
14122 = Explicit_Actual_Parameter (Old_E)
14123 then
14124 Set_First_Named_Actual
14125 (New_Node, Explicit_Actual_Parameter (New_E));
14126 end if;
14128 -- Now scan parameter list from the beginning,to locate
14129 -- next named actual, which can be out of order.
14131 Old_Next := First (Parameter_Associations (Old_Node));
14132 New_Next := First (Parameter_Associations (New_Node));
14134 while Nkind (Old_Next) /= N_Parameter_Association
14135 or else Explicit_Actual_Parameter (Old_Next)
14136 /= Next_Named_Actual (Old_E)
14137 loop
14138 Next (Old_Next);
14139 Next (New_Next);
14140 end loop;
14142 Set_Next_Named_Actual
14143 (New_E, Explicit_Actual_Parameter (New_Next));
14144 end if;
14146 Next (Old_E);
14147 Next (New_E);
14148 end loop;
14149 end Adjust_Named_Associations;
14151 ---------------------------------
14152 -- Copy_Field_With_Replacement --
14153 ---------------------------------
14155 function Copy_Field_With_Replacement
14156 (Field : Union_Id) return Union_Id
14158 begin
14159 if Field = Union_Id (Empty) then
14160 return Field;
14162 elsif Field in Node_Range then
14163 declare
14164 Old_N : constant Node_Id := Node_Id (Field);
14165 New_N : Node_Id;
14167 begin
14168 -- If syntactic field, as indicated by the parent pointer
14169 -- being set, then copy the referenced node recursively.
14171 if Parent (Old_N) = Old_Node then
14172 New_N := Copy_Node_With_Replacement (Old_N);
14174 if New_N /= Old_N then
14175 Set_Parent (New_N, New_Node);
14176 end if;
14178 -- For semantic fields, update possible entity reference
14179 -- from the replacement map.
14181 else
14182 New_N := Assoc (Old_N);
14183 end if;
14185 return Union_Id (New_N);
14186 end;
14188 elsif Field in List_Range then
14189 declare
14190 Old_L : constant List_Id := List_Id (Field);
14191 New_L : List_Id;
14193 begin
14194 -- If syntactic field, as indicated by the parent pointer,
14195 -- then recursively copy the entire referenced list.
14197 if Parent (Old_L) = Old_Node then
14198 New_L := Copy_List_With_Replacement (Old_L);
14199 Set_Parent (New_L, New_Node);
14201 -- For semantic list, just returned unchanged
14203 else
14204 New_L := Old_L;
14205 end if;
14207 return Union_Id (New_L);
14208 end;
14210 -- Anything other than a list or a node is returned unchanged
14212 else
14213 return Field;
14214 end if;
14215 end Copy_Field_With_Replacement;
14217 -- Start of processing for Copy_Node_With_Replacement
14219 begin
14220 if Old_Node <= Empty_Or_Error then
14221 return Old_Node;
14223 elsif Has_Extension (Old_Node) then
14224 return Assoc (Old_Node);
14226 else
14227 New_Node := New_Copy (Old_Node);
14229 -- If the node we are copying is the associated node of a
14230 -- previously copied Itype, then adjust the associated node
14231 -- of the copy of that Itype accordingly.
14233 if Present (Actual_Map) then
14234 declare
14235 E : Elmt_Id;
14236 Ent : Entity_Id;
14238 begin
14239 -- Case of hash table used
14241 if NCT_Hash_Tables_Used then
14242 Ent := NCT_Itype_Assoc.Get (Old_Node);
14244 if Present (Ent) then
14245 Set_Associated_Node_For_Itype (Ent, New_Node);
14246 end if;
14248 -- Case of no hash table used
14250 else
14251 E := First_Elmt (Actual_Map);
14252 while Present (E) loop
14253 if Is_Itype (Node (E))
14254 and then
14255 Old_Node = Associated_Node_For_Itype (Node (E))
14256 then
14257 Set_Associated_Node_For_Itype
14258 (Node (Next_Elmt (E)), New_Node);
14259 end if;
14261 E := Next_Elmt (Next_Elmt (E));
14262 end loop;
14263 end if;
14264 end;
14265 end if;
14267 -- Recursively copy descendents
14269 Set_Field1
14270 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
14271 Set_Field2
14272 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
14273 Set_Field3
14274 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
14275 Set_Field4
14276 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
14277 Set_Field5
14278 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
14280 -- Adjust Sloc of new node if necessary
14282 if New_Sloc /= No_Location then
14283 Set_Sloc (New_Node, New_Sloc);
14285 -- If we adjust the Sloc, then we are essentially making
14286 -- a completely new node, so the Comes_From_Source flag
14287 -- should be reset to the proper default value.
14289 Nodes.Table (New_Node).Comes_From_Source :=
14290 Default_Node.Comes_From_Source;
14291 end if;
14293 -- If the node is call and has named associations,
14294 -- set the corresponding links in the copy.
14296 if (Nkind (Old_Node) = N_Function_Call
14297 or else Nkind (Old_Node) = N_Entry_Call_Statement
14298 or else
14299 Nkind (Old_Node) = N_Procedure_Call_Statement)
14300 and then Present (First_Named_Actual (Old_Node))
14301 then
14302 Adjust_Named_Associations (Old_Node, New_Node);
14303 end if;
14305 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
14306 -- The replacement mechanism applies to entities, and is not used
14307 -- here. Eventually we may need a more general graph-copying
14308 -- routine. For now, do a sequential search to find desired node.
14310 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
14311 and then Present (First_Real_Statement (Old_Node))
14312 then
14313 declare
14314 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
14315 N1, N2 : Node_Id;
14317 begin
14318 N1 := First (Statements (Old_Node));
14319 N2 := First (Statements (New_Node));
14321 while N1 /= Old_F loop
14322 Next (N1);
14323 Next (N2);
14324 end loop;
14326 Set_First_Real_Statement (New_Node, N2);
14327 end;
14328 end if;
14329 end if;
14331 -- All done, return copied node
14333 return New_Node;
14334 end Copy_Node_With_Replacement;
14336 -----------------
14337 -- Visit_Elist --
14338 -----------------
14340 procedure Visit_Elist (E : Elist_Id) is
14341 Elmt : Elmt_Id;
14342 begin
14343 if Present (E) then
14344 Elmt := First_Elmt (E);
14346 while Elmt /= No_Elmt loop
14347 Visit_Node (Node (Elmt));
14348 Next_Elmt (Elmt);
14349 end loop;
14350 end if;
14351 end Visit_Elist;
14353 -----------------
14354 -- Visit_Field --
14355 -----------------
14357 procedure Visit_Field (F : Union_Id; N : Node_Id) is
14358 begin
14359 if F = Union_Id (Empty) then
14360 return;
14362 elsif F in Node_Range then
14364 -- Copy node if it is syntactic, i.e. its parent pointer is
14365 -- set to point to the field that referenced it (certain
14366 -- Itypes will also meet this criterion, which is fine, since
14367 -- these are clearly Itypes that do need to be copied, since
14368 -- we are copying their parent.)
14370 if Parent (Node_Id (F)) = N then
14371 Visit_Node (Node_Id (F));
14372 return;
14374 -- Another case, if we are pointing to an Itype, then we want
14375 -- to copy it if its associated node is somewhere in the tree
14376 -- being copied.
14378 -- Note: the exclusion of self-referential copies is just an
14379 -- optimization, since the search of the already copied list
14380 -- would catch it, but it is a common case (Etype pointing
14381 -- to itself for an Itype that is a base type).
14383 elsif Has_Extension (Node_Id (F))
14384 and then Is_Itype (Entity_Id (F))
14385 and then Node_Id (F) /= N
14386 then
14387 declare
14388 P : Node_Id;
14390 begin
14391 P := Associated_Node_For_Itype (Node_Id (F));
14392 while Present (P) loop
14393 if P = Source then
14394 Visit_Node (Node_Id (F));
14395 return;
14396 else
14397 P := Parent (P);
14398 end if;
14399 end loop;
14401 -- An Itype whose parent is not being copied definitely
14402 -- should NOT be copied, since it does not belong in any
14403 -- sense to the copied subtree.
14405 return;
14406 end;
14407 end if;
14409 elsif F in List_Range and then Parent (List_Id (F)) = N then
14410 Visit_List (List_Id (F));
14411 return;
14412 end if;
14413 end Visit_Field;
14415 -----------------
14416 -- Visit_Itype --
14417 -----------------
14419 procedure Visit_Itype (Old_Itype : Entity_Id) is
14420 New_Itype : Entity_Id;
14421 E : Elmt_Id;
14422 Ent : Entity_Id;
14424 begin
14425 -- Itypes that describe the designated type of access to subprograms
14426 -- have the structure of subprogram declarations, with signatures,
14427 -- etc. Either we duplicate the signatures completely, or choose to
14428 -- share such itypes, which is fine because their elaboration will
14429 -- have no side effects.
14431 if Ekind (Old_Itype) = E_Subprogram_Type then
14432 return;
14433 end if;
14435 New_Itype := New_Copy (Old_Itype);
14437 -- The new Itype has all the attributes of the old one, and
14438 -- we just copy the contents of the entity. However, the back-end
14439 -- needs different names for debugging purposes, so we create a
14440 -- new internal name for it in all cases.
14442 Set_Chars (New_Itype, New_Internal_Name ('T'));
14444 -- If our associated node is an entity that has already been copied,
14445 -- then set the associated node of the copy to point to the right
14446 -- copy. If we have copied an Itype that is itself the associated
14447 -- node of some previously copied Itype, then we set the right
14448 -- pointer in the other direction.
14450 if Present (Actual_Map) then
14452 -- Case of hash tables used
14454 if NCT_Hash_Tables_Used then
14456 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
14458 if Present (Ent) then
14459 Set_Associated_Node_For_Itype (New_Itype, Ent);
14460 end if;
14462 Ent := NCT_Itype_Assoc.Get (Old_Itype);
14463 if Present (Ent) then
14464 Set_Associated_Node_For_Itype (Ent, New_Itype);
14466 -- If the hash table has no association for this Itype and
14467 -- its associated node, enter one now.
14469 else
14470 NCT_Itype_Assoc.Set
14471 (Associated_Node_For_Itype (Old_Itype), New_Itype);
14472 end if;
14474 -- Case of hash tables not used
14476 else
14477 E := First_Elmt (Actual_Map);
14478 while Present (E) loop
14479 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
14480 Set_Associated_Node_For_Itype
14481 (New_Itype, Node (Next_Elmt (E)));
14482 end if;
14484 if Is_Type (Node (E))
14485 and then Old_Itype = Associated_Node_For_Itype (Node (E))
14486 then
14487 Set_Associated_Node_For_Itype
14488 (Node (Next_Elmt (E)), New_Itype);
14489 end if;
14491 E := Next_Elmt (Next_Elmt (E));
14492 end loop;
14493 end if;
14494 end if;
14496 if Present (Freeze_Node (New_Itype)) then
14497 Set_Is_Frozen (New_Itype, False);
14498 Set_Freeze_Node (New_Itype, Empty);
14499 end if;
14501 -- Add new association to map
14503 if No (Actual_Map) then
14504 Actual_Map := New_Elmt_List;
14505 end if;
14507 Append_Elmt (Old_Itype, Actual_Map);
14508 Append_Elmt (New_Itype, Actual_Map);
14510 if NCT_Hash_Tables_Used then
14511 NCT_Assoc.Set (Old_Itype, New_Itype);
14513 else
14514 NCT_Table_Entries := NCT_Table_Entries + 1;
14516 if NCT_Table_Entries > NCT_Hash_Threshold then
14517 Build_NCT_Hash_Tables;
14518 end if;
14519 end if;
14521 -- If a record subtype is simply copied, the entity list will be
14522 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
14524 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
14525 Set_Cloned_Subtype (New_Itype, Old_Itype);
14526 end if;
14528 -- Visit descendents that eventually get copied
14530 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
14532 if Is_Discrete_Type (Old_Itype) then
14533 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
14535 elsif Has_Discriminants (Base_Type (Old_Itype)) then
14536 -- ??? This should involve call to Visit_Field
14537 Visit_Elist (Discriminant_Constraint (Old_Itype));
14539 elsif Is_Array_Type (Old_Itype) then
14540 if Present (First_Index (Old_Itype)) then
14541 Visit_Field (Union_Id (List_Containing
14542 (First_Index (Old_Itype))),
14543 Old_Itype);
14544 end if;
14546 if Is_Packed (Old_Itype) then
14547 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
14548 Old_Itype);
14549 end if;
14550 end if;
14551 end Visit_Itype;
14553 ----------------
14554 -- Visit_List --
14555 ----------------
14557 procedure Visit_List (L : List_Id) is
14558 N : Node_Id;
14559 begin
14560 if L /= No_List then
14561 N := First (L);
14563 while Present (N) loop
14564 Visit_Node (N);
14565 Next (N);
14566 end loop;
14567 end if;
14568 end Visit_List;
14570 ----------------
14571 -- Visit_Node --
14572 ----------------
14574 procedure Visit_Node (N : Node_Or_Entity_Id) is
14576 -- Start of processing for Visit_Node
14578 begin
14579 -- Handle case of an Itype, which must be copied
14581 if Has_Extension (N) and then Is_Itype (N) then
14583 -- Nothing to do if already in the list. This can happen with an
14584 -- Itype entity that appears more than once in the tree.
14585 -- Note that we do not want to visit descendents in this case.
14587 -- Test for already in list when hash table is used
14589 if NCT_Hash_Tables_Used then
14590 if Present (NCT_Assoc.Get (Entity_Id (N))) then
14591 return;
14592 end if;
14594 -- Test for already in list when hash table not used
14596 else
14597 declare
14598 E : Elmt_Id;
14599 begin
14600 if Present (Actual_Map) then
14601 E := First_Elmt (Actual_Map);
14602 while Present (E) loop
14603 if Node (E) = N then
14604 return;
14605 else
14606 E := Next_Elmt (Next_Elmt (E));
14607 end if;
14608 end loop;
14609 end if;
14610 end;
14611 end if;
14613 Visit_Itype (N);
14614 end if;
14616 -- Visit descendents
14618 Visit_Field (Field1 (N), N);
14619 Visit_Field (Field2 (N), N);
14620 Visit_Field (Field3 (N), N);
14621 Visit_Field (Field4 (N), N);
14622 Visit_Field (Field5 (N), N);
14623 end Visit_Node;
14625 -- Start of processing for New_Copy_Tree
14627 begin
14628 Actual_Map := Map;
14630 -- See if we should use hash table
14632 if No (Actual_Map) then
14633 NCT_Hash_Tables_Used := False;
14635 else
14636 declare
14637 Elmt : Elmt_Id;
14639 begin
14640 NCT_Table_Entries := 0;
14642 Elmt := First_Elmt (Actual_Map);
14643 while Present (Elmt) loop
14644 NCT_Table_Entries := NCT_Table_Entries + 1;
14645 Next_Elmt (Elmt);
14646 Next_Elmt (Elmt);
14647 end loop;
14649 if NCT_Table_Entries > NCT_Hash_Threshold then
14650 Build_NCT_Hash_Tables;
14651 else
14652 NCT_Hash_Tables_Used := False;
14653 end if;
14654 end;
14655 end if;
14657 -- Hash table set up if required, now start phase one by visiting
14658 -- top node (we will recursively visit the descendents).
14660 Visit_Node (Source);
14662 -- Now the second phase of the copy can start. First we process
14663 -- all the mapped entities, copying their descendents.
14665 if Present (Actual_Map) then
14666 declare
14667 Elmt : Elmt_Id;
14668 New_Itype : Entity_Id;
14669 begin
14670 Elmt := First_Elmt (Actual_Map);
14671 while Present (Elmt) loop
14672 Next_Elmt (Elmt);
14673 New_Itype := Node (Elmt);
14674 Copy_Itype_With_Replacement (New_Itype);
14675 Next_Elmt (Elmt);
14676 end loop;
14677 end;
14678 end if;
14680 -- Now we can copy the actual tree
14682 return Copy_Node_With_Replacement (Source);
14683 end New_Copy_Tree;
14685 -------------------------
14686 -- New_External_Entity --
14687 -------------------------
14689 function New_External_Entity
14690 (Kind : Entity_Kind;
14691 Scope_Id : Entity_Id;
14692 Sloc_Value : Source_Ptr;
14693 Related_Id : Entity_Id;
14694 Suffix : Character;
14695 Suffix_Index : Nat := 0;
14696 Prefix : Character := ' ') return Entity_Id
14698 N : constant Entity_Id :=
14699 Make_Defining_Identifier (Sloc_Value,
14700 New_External_Name
14701 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
14703 begin
14704 Set_Ekind (N, Kind);
14705 Set_Is_Internal (N, True);
14706 Append_Entity (N, Scope_Id);
14707 Set_Public_Status (N);
14709 if Kind in Type_Kind then
14710 Init_Size_Align (N);
14711 end if;
14713 return N;
14714 end New_External_Entity;
14716 -------------------------
14717 -- New_Internal_Entity --
14718 -------------------------
14720 function New_Internal_Entity
14721 (Kind : Entity_Kind;
14722 Scope_Id : Entity_Id;
14723 Sloc_Value : Source_Ptr;
14724 Id_Char : Character) return Entity_Id
14726 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
14728 begin
14729 Set_Ekind (N, Kind);
14730 Set_Is_Internal (N, True);
14731 Append_Entity (N, Scope_Id);
14733 if Kind in Type_Kind then
14734 Init_Size_Align (N);
14735 end if;
14737 return N;
14738 end New_Internal_Entity;
14740 -----------------
14741 -- Next_Actual --
14742 -----------------
14744 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
14745 N : Node_Id;
14747 begin
14748 -- If we are pointing at a positional parameter, it is a member of a
14749 -- node list (the list of parameters), and the next parameter is the
14750 -- next node on the list, unless we hit a parameter association, then
14751 -- we shift to using the chain whose head is the First_Named_Actual in
14752 -- the parent, and then is threaded using the Next_Named_Actual of the
14753 -- Parameter_Association. All this fiddling is because the original node
14754 -- list is in the textual call order, and what we need is the
14755 -- declaration order.
14757 if Is_List_Member (Actual_Id) then
14758 N := Next (Actual_Id);
14760 if Nkind (N) = N_Parameter_Association then
14761 return First_Named_Actual (Parent (Actual_Id));
14762 else
14763 return N;
14764 end if;
14766 else
14767 return Next_Named_Actual (Parent (Actual_Id));
14768 end if;
14769 end Next_Actual;
14771 procedure Next_Actual (Actual_Id : in out Node_Id) is
14772 begin
14773 Actual_Id := Next_Actual (Actual_Id);
14774 end Next_Actual;
14776 -----------------------
14777 -- Normalize_Actuals --
14778 -----------------------
14780 -- Chain actuals according to formals of subprogram. If there are no named
14781 -- associations, the chain is simply the list of Parameter Associations,
14782 -- since the order is the same as the declaration order. If there are named
14783 -- associations, then the First_Named_Actual field in the N_Function_Call
14784 -- or N_Procedure_Call_Statement node points to the Parameter_Association
14785 -- node for the parameter that comes first in declaration order. The
14786 -- remaining named parameters are then chained in declaration order using
14787 -- Next_Named_Actual.
14789 -- This routine also verifies that the number of actuals is compatible with
14790 -- the number and default values of formals, but performs no type checking
14791 -- (type checking is done by the caller).
14793 -- If the matching succeeds, Success is set to True and the caller proceeds
14794 -- with type-checking. If the match is unsuccessful, then Success is set to
14795 -- False, and the caller attempts a different interpretation, if there is
14796 -- one.
14798 -- If the flag Report is on, the call is not overloaded, and a failure to
14799 -- match can be reported here, rather than in the caller.
14801 procedure Normalize_Actuals
14802 (N : Node_Id;
14803 S : Entity_Id;
14804 Report : Boolean;
14805 Success : out Boolean)
14807 Actuals : constant List_Id := Parameter_Associations (N);
14808 Actual : Node_Id := Empty;
14809 Formal : Entity_Id;
14810 Last : Node_Id := Empty;
14811 First_Named : Node_Id := Empty;
14812 Found : Boolean;
14814 Formals_To_Match : Integer := 0;
14815 Actuals_To_Match : Integer := 0;
14817 procedure Chain (A : Node_Id);
14818 -- Add named actual at the proper place in the list, using the
14819 -- Next_Named_Actual link.
14821 function Reporting return Boolean;
14822 -- Determines if an error is to be reported. To report an error, we
14823 -- need Report to be True, and also we do not report errors caused
14824 -- by calls to init procs that occur within other init procs. Such
14825 -- errors must always be cascaded errors, since if all the types are
14826 -- declared correctly, the compiler will certainly build decent calls.
14828 -----------
14829 -- Chain --
14830 -----------
14832 procedure Chain (A : Node_Id) is
14833 begin
14834 if No (Last) then
14836 -- Call node points to first actual in list
14838 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
14840 else
14841 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
14842 end if;
14844 Last := A;
14845 Set_Next_Named_Actual (Last, Empty);
14846 end Chain;
14848 ---------------
14849 -- Reporting --
14850 ---------------
14852 function Reporting return Boolean is
14853 begin
14854 if not Report then
14855 return False;
14857 elsif not Within_Init_Proc then
14858 return True;
14860 elsif Is_Init_Proc (Entity (Name (N))) then
14861 return False;
14863 else
14864 return True;
14865 end if;
14866 end Reporting;
14868 -- Start of processing for Normalize_Actuals
14870 begin
14871 if Is_Access_Type (S) then
14873 -- The name in the call is a function call that returns an access
14874 -- to subprogram. The designated type has the list of formals.
14876 Formal := First_Formal (Designated_Type (S));
14877 else
14878 Formal := First_Formal (S);
14879 end if;
14881 while Present (Formal) loop
14882 Formals_To_Match := Formals_To_Match + 1;
14883 Next_Formal (Formal);
14884 end loop;
14886 -- Find if there is a named association, and verify that no positional
14887 -- associations appear after named ones.
14889 if Present (Actuals) then
14890 Actual := First (Actuals);
14891 end if;
14893 while Present (Actual)
14894 and then Nkind (Actual) /= N_Parameter_Association
14895 loop
14896 Actuals_To_Match := Actuals_To_Match + 1;
14897 Next (Actual);
14898 end loop;
14900 if No (Actual) and Actuals_To_Match = Formals_To_Match then
14902 -- Most common case: positional notation, no defaults
14904 Success := True;
14905 return;
14907 elsif Actuals_To_Match > Formals_To_Match then
14909 -- Too many actuals: will not work
14911 if Reporting then
14912 if Is_Entity_Name (Name (N)) then
14913 Error_Msg_N ("too many arguments in call to&", Name (N));
14914 else
14915 Error_Msg_N ("too many arguments in call", N);
14916 end if;
14917 end if;
14919 Success := False;
14920 return;
14921 end if;
14923 First_Named := Actual;
14925 while Present (Actual) loop
14926 if Nkind (Actual) /= N_Parameter_Association then
14927 Error_Msg_N
14928 ("positional parameters not allowed after named ones", Actual);
14929 Success := False;
14930 return;
14932 else
14933 Actuals_To_Match := Actuals_To_Match + 1;
14934 end if;
14936 Next (Actual);
14937 end loop;
14939 if Present (Actuals) then
14940 Actual := First (Actuals);
14941 end if;
14943 Formal := First_Formal (S);
14944 while Present (Formal) loop
14946 -- Match the formals in order. If the corresponding actual is
14947 -- positional, nothing to do. Else scan the list of named actuals
14948 -- to find the one with the right name.
14950 if Present (Actual)
14951 and then Nkind (Actual) /= N_Parameter_Association
14952 then
14953 Next (Actual);
14954 Actuals_To_Match := Actuals_To_Match - 1;
14955 Formals_To_Match := Formals_To_Match - 1;
14957 else
14958 -- For named parameters, search the list of actuals to find
14959 -- one that matches the next formal name.
14961 Actual := First_Named;
14962 Found := False;
14963 while Present (Actual) loop
14964 if Chars (Selector_Name (Actual)) = Chars (Formal) then
14965 Found := True;
14966 Chain (Actual);
14967 Actuals_To_Match := Actuals_To_Match - 1;
14968 Formals_To_Match := Formals_To_Match - 1;
14969 exit;
14970 end if;
14972 Next (Actual);
14973 end loop;
14975 if not Found then
14976 if Ekind (Formal) /= E_In_Parameter
14977 or else No (Default_Value (Formal))
14978 then
14979 if Reporting then
14980 if (Comes_From_Source (S)
14981 or else Sloc (S) = Standard_Location)
14982 and then Is_Overloadable (S)
14983 then
14984 if No (Actuals)
14985 and then
14986 Nkind_In (Parent (N), N_Procedure_Call_Statement,
14987 N_Function_Call,
14988 N_Parameter_Association)
14989 and then Ekind (S) /= E_Function
14990 then
14991 Set_Etype (N, Etype (S));
14993 else
14994 Error_Msg_Name_1 := Chars (S);
14995 Error_Msg_Sloc := Sloc (S);
14996 Error_Msg_NE
14997 ("missing argument for parameter & "
14998 & "in call to % declared #", N, Formal);
14999 end if;
15001 elsif Is_Overloadable (S) then
15002 Error_Msg_Name_1 := Chars (S);
15004 -- Point to type derivation that generated the
15005 -- operation.
15007 Error_Msg_Sloc := Sloc (Parent (S));
15009 Error_Msg_NE
15010 ("missing argument for parameter & "
15011 & "in call to % (inherited) #", N, Formal);
15013 else
15014 Error_Msg_NE
15015 ("missing argument for parameter &", N, Formal);
15016 end if;
15017 end if;
15019 Success := False;
15020 return;
15022 else
15023 Formals_To_Match := Formals_To_Match - 1;
15024 end if;
15025 end if;
15026 end if;
15028 Next_Formal (Formal);
15029 end loop;
15031 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
15032 Success := True;
15033 return;
15035 else
15036 if Reporting then
15038 -- Find some superfluous named actual that did not get
15039 -- attached to the list of associations.
15041 Actual := First (Actuals);
15042 while Present (Actual) loop
15043 if Nkind (Actual) = N_Parameter_Association
15044 and then Actual /= Last
15045 and then No (Next_Named_Actual (Actual))
15046 then
15047 Error_Msg_N ("unmatched actual & in call",
15048 Selector_Name (Actual));
15049 exit;
15050 end if;
15052 Next (Actual);
15053 end loop;
15054 end if;
15056 Success := False;
15057 return;
15058 end if;
15059 end Normalize_Actuals;
15061 --------------------------------
15062 -- Note_Possible_Modification --
15063 --------------------------------
15065 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
15066 Modification_Comes_From_Source : constant Boolean :=
15067 Comes_From_Source (Parent (N));
15069 Ent : Entity_Id;
15070 Exp : Node_Id;
15072 begin
15073 -- Loop to find referenced entity, if there is one
15075 Exp := N;
15076 loop
15077 Ent := Empty;
15079 if Is_Entity_Name (Exp) then
15080 Ent := Entity (Exp);
15082 -- If the entity is missing, it is an undeclared identifier,
15083 -- and there is nothing to annotate.
15085 if No (Ent) then
15086 return;
15087 end if;
15089 elsif Nkind (Exp) = N_Explicit_Dereference then
15090 declare
15091 P : constant Node_Id := Prefix (Exp);
15093 begin
15094 -- In formal verification mode, keep track of all reads and
15095 -- writes through explicit dereferences.
15097 if GNATprove_Mode then
15098 SPARK_Specific.Generate_Dereference (N, 'm');
15099 end if;
15101 if Nkind (P) = N_Selected_Component
15102 and then Present (Entry_Formal (Entity (Selector_Name (P))))
15103 then
15104 -- Case of a reference to an entry formal
15106 Ent := Entry_Formal (Entity (Selector_Name (P)));
15108 elsif Nkind (P) = N_Identifier
15109 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
15110 and then Present (Expression (Parent (Entity (P))))
15111 and then Nkind (Expression (Parent (Entity (P)))) =
15112 N_Reference
15113 then
15114 -- Case of a reference to a value on which side effects have
15115 -- been removed.
15117 Exp := Prefix (Expression (Parent (Entity (P))));
15118 goto Continue;
15120 else
15121 return;
15122 end if;
15123 end;
15125 elsif Nkind_In (Exp, N_Type_Conversion,
15126 N_Unchecked_Type_Conversion)
15127 then
15128 Exp := Expression (Exp);
15129 goto Continue;
15131 elsif Nkind_In (Exp, N_Slice,
15132 N_Indexed_Component,
15133 N_Selected_Component)
15134 then
15135 -- Special check, if the prefix is an access type, then return
15136 -- since we are modifying the thing pointed to, not the prefix.
15137 -- When we are expanding, most usually the prefix is replaced
15138 -- by an explicit dereference, and this test is not needed, but
15139 -- in some cases (notably -gnatc mode and generics) when we do
15140 -- not do full expansion, we need this special test.
15142 if Is_Access_Type (Etype (Prefix (Exp))) then
15143 return;
15145 -- Otherwise go to prefix and keep going
15147 else
15148 Exp := Prefix (Exp);
15149 goto Continue;
15150 end if;
15152 -- All other cases, not a modification
15154 else
15155 return;
15156 end if;
15158 -- Now look for entity being referenced
15160 if Present (Ent) then
15161 if Is_Object (Ent) then
15162 if Comes_From_Source (Exp)
15163 or else Modification_Comes_From_Source
15164 then
15165 -- Give warning if pragma unmodified given and we are
15166 -- sure this is a modification.
15168 if Has_Pragma_Unmodified (Ent) and then Sure then
15169 Error_Msg_NE ("??pragma Unmodified given for &!", N, Ent);
15170 end if;
15172 Set_Never_Set_In_Source (Ent, False);
15173 end if;
15175 Set_Is_True_Constant (Ent, False);
15176 Set_Current_Value (Ent, Empty);
15177 Set_Is_Known_Null (Ent, False);
15179 if not Can_Never_Be_Null (Ent) then
15180 Set_Is_Known_Non_Null (Ent, False);
15181 end if;
15183 -- Follow renaming chain
15185 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
15186 and then Present (Renamed_Object (Ent))
15187 then
15188 Exp := Renamed_Object (Ent);
15190 -- If the entity is the loop variable in an iteration over
15191 -- a container, retrieve container expression to indicate
15192 -- possible modificastion.
15194 if Present (Related_Expression (Ent))
15195 and then Nkind (Parent (Related_Expression (Ent))) =
15196 N_Iterator_Specification
15197 then
15198 Exp := Original_Node (Related_Expression (Ent));
15199 end if;
15201 goto Continue;
15203 -- The expression may be the renaming of a subcomponent of an
15204 -- array or container. The assignment to the subcomponent is
15205 -- a modification of the container.
15207 elsif Comes_From_Source (Original_Node (Exp))
15208 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
15209 N_Indexed_Component)
15210 then
15211 Exp := Prefix (Original_Node (Exp));
15212 goto Continue;
15213 end if;
15215 -- Generate a reference only if the assignment comes from
15216 -- source. This excludes, for example, calls to a dispatching
15217 -- assignment operation when the left-hand side is tagged. In
15218 -- GNATprove mode, we need those references also on generated
15219 -- code, as these are used to compute the local effects of
15220 -- subprograms.
15222 if Modification_Comes_From_Source or GNATprove_Mode then
15223 Generate_Reference (Ent, Exp, 'm');
15225 -- If the target of the assignment is the bound variable
15226 -- in an iterator, indicate that the corresponding array
15227 -- or container is also modified.
15229 if Ada_Version >= Ada_2012
15230 and then Nkind (Parent (Ent)) = N_Iterator_Specification
15231 then
15232 declare
15233 Domain : constant Node_Id := Name (Parent (Ent));
15235 begin
15236 -- TBD : in the full version of the construct, the
15237 -- domain of iteration can be given by an expression.
15239 if Is_Entity_Name (Domain) then
15240 Generate_Reference (Entity (Domain), Exp, 'm');
15241 Set_Is_True_Constant (Entity (Domain), False);
15242 Set_Never_Set_In_Source (Entity (Domain), False);
15243 end if;
15244 end;
15245 end if;
15246 end if;
15248 Check_Nested_Access (Ent);
15249 end if;
15251 Kill_Checks (Ent);
15253 -- If we are sure this is a modification from source, and we know
15254 -- this modifies a constant, then give an appropriate warning.
15256 if Overlays_Constant (Ent)
15257 and then (Modification_Comes_From_Source and Sure)
15258 then
15259 declare
15260 A : constant Node_Id := Address_Clause (Ent);
15261 begin
15262 if Present (A) then
15263 declare
15264 Exp : constant Node_Id := Expression (A);
15265 begin
15266 if Nkind (Exp) = N_Attribute_Reference
15267 and then Attribute_Name (Exp) = Name_Address
15268 and then Is_Entity_Name (Prefix (Exp))
15269 then
15270 Error_Msg_Sloc := Sloc (A);
15271 Error_Msg_NE
15272 ("constant& may be modified via address "
15273 & "clause#??", N, Entity (Prefix (Exp)));
15274 end if;
15275 end;
15276 end if;
15277 end;
15278 end if;
15280 return;
15281 end if;
15283 <<Continue>>
15284 null;
15285 end loop;
15286 end Note_Possible_Modification;
15288 -------------------------
15289 -- Object_Access_Level --
15290 -------------------------
15292 -- Returns the static accessibility level of the view denoted by Obj. Note
15293 -- that the value returned is the result of a call to Scope_Depth. Only
15294 -- scope depths associated with dynamic scopes can actually be returned.
15295 -- Since only relative levels matter for accessibility checking, the fact
15296 -- that the distance between successive levels of accessibility is not
15297 -- always one is immaterial (invariant: if level(E2) is deeper than
15298 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
15300 function Object_Access_Level (Obj : Node_Id) return Uint is
15301 function Is_Interface_Conversion (N : Node_Id) return Boolean;
15302 -- Determine whether N is a construct of the form
15303 -- Some_Type (Operand._tag'Address)
15304 -- This construct appears in the context of dispatching calls.
15306 function Reference_To (Obj : Node_Id) return Node_Id;
15307 -- An explicit dereference is created when removing side-effects from
15308 -- expressions for constraint checking purposes. In this case a local
15309 -- access type is created for it. The correct access level is that of
15310 -- the original source node. We detect this case by noting that the
15311 -- prefix of the dereference is created by an object declaration whose
15312 -- initial expression is a reference.
15314 -----------------------------
15315 -- Is_Interface_Conversion --
15316 -----------------------------
15318 function Is_Interface_Conversion (N : Node_Id) return Boolean is
15319 begin
15320 return Nkind (N) = N_Unchecked_Type_Conversion
15321 and then Nkind (Expression (N)) = N_Attribute_Reference
15322 and then Attribute_Name (Expression (N)) = Name_Address;
15323 end Is_Interface_Conversion;
15325 ------------------
15326 -- Reference_To --
15327 ------------------
15329 function Reference_To (Obj : Node_Id) return Node_Id is
15330 Pref : constant Node_Id := Prefix (Obj);
15331 begin
15332 if Is_Entity_Name (Pref)
15333 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
15334 and then Present (Expression (Parent (Entity (Pref))))
15335 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
15336 then
15337 return (Prefix (Expression (Parent (Entity (Pref)))));
15338 else
15339 return Empty;
15340 end if;
15341 end Reference_To;
15343 -- Local variables
15345 E : Entity_Id;
15347 -- Start of processing for Object_Access_Level
15349 begin
15350 if Nkind (Obj) = N_Defining_Identifier
15351 or else Is_Entity_Name (Obj)
15352 then
15353 if Nkind (Obj) = N_Defining_Identifier then
15354 E := Obj;
15355 else
15356 E := Entity (Obj);
15357 end if;
15359 if Is_Prival (E) then
15360 E := Prival_Link (E);
15361 end if;
15363 -- If E is a type then it denotes a current instance. For this case
15364 -- we add one to the normal accessibility level of the type to ensure
15365 -- that current instances are treated as always being deeper than
15366 -- than the level of any visible named access type (see 3.10.2(21)).
15368 if Is_Type (E) then
15369 return Type_Access_Level (E) + 1;
15371 elsif Present (Renamed_Object (E)) then
15372 return Object_Access_Level (Renamed_Object (E));
15374 -- Similarly, if E is a component of the current instance of a
15375 -- protected type, any instance of it is assumed to be at a deeper
15376 -- level than the type. For a protected object (whose type is an
15377 -- anonymous protected type) its components are at the same level
15378 -- as the type itself.
15380 elsif not Is_Overloadable (E)
15381 and then Ekind (Scope (E)) = E_Protected_Type
15382 and then Comes_From_Source (Scope (E))
15383 then
15384 return Type_Access_Level (Scope (E)) + 1;
15386 else
15387 -- Aliased formals take their access level from the point of call.
15388 -- This is smaller than the level of the subprogram itself.
15390 if Is_Formal (E) and then Is_Aliased (E) then
15391 return Type_Access_Level (Etype (E));
15393 else
15394 return Scope_Depth (Enclosing_Dynamic_Scope (E));
15395 end if;
15396 end if;
15398 elsif Nkind (Obj) = N_Selected_Component then
15399 if Is_Access_Type (Etype (Prefix (Obj))) then
15400 return Type_Access_Level (Etype (Prefix (Obj)));
15401 else
15402 return Object_Access_Level (Prefix (Obj));
15403 end if;
15405 elsif Nkind (Obj) = N_Indexed_Component then
15406 if Is_Access_Type (Etype (Prefix (Obj))) then
15407 return Type_Access_Level (Etype (Prefix (Obj)));
15408 else
15409 return Object_Access_Level (Prefix (Obj));
15410 end if;
15412 elsif Nkind (Obj) = N_Explicit_Dereference then
15414 -- If the prefix is a selected access discriminant then we make a
15415 -- recursive call on the prefix, which will in turn check the level
15416 -- of the prefix object of the selected discriminant.
15418 -- In Ada 2012, if the discriminant has implicit dereference and
15419 -- the context is a selected component, treat this as an object of
15420 -- unknown scope (see below). This is necessary in compile-only mode;
15421 -- otherwise expansion will already have transformed the prefix into
15422 -- a temporary.
15424 if Nkind (Prefix (Obj)) = N_Selected_Component
15425 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
15426 and then
15427 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
15428 and then
15429 (not Has_Implicit_Dereference
15430 (Entity (Selector_Name (Prefix (Obj))))
15431 or else Nkind (Parent (Obj)) /= N_Selected_Component)
15432 then
15433 return Object_Access_Level (Prefix (Obj));
15435 -- Detect an interface conversion in the context of a dispatching
15436 -- call. Use the original form of the conversion to find the access
15437 -- level of the operand.
15439 elsif Is_Interface (Etype (Obj))
15440 and then Is_Interface_Conversion (Prefix (Obj))
15441 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
15442 then
15443 return Object_Access_Level (Original_Node (Obj));
15445 elsif not Comes_From_Source (Obj) then
15446 declare
15447 Ref : constant Node_Id := Reference_To (Obj);
15448 begin
15449 if Present (Ref) then
15450 return Object_Access_Level (Ref);
15451 else
15452 return Type_Access_Level (Etype (Prefix (Obj)));
15453 end if;
15454 end;
15456 else
15457 return Type_Access_Level (Etype (Prefix (Obj)));
15458 end if;
15460 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
15461 return Object_Access_Level (Expression (Obj));
15463 elsif Nkind (Obj) = N_Function_Call then
15465 -- Function results are objects, so we get either the access level of
15466 -- the function or, in the case of an indirect call, the level of the
15467 -- access-to-subprogram type. (This code is used for Ada 95, but it
15468 -- looks wrong, because it seems that we should be checking the level
15469 -- of the call itself, even for Ada 95. However, using the Ada 2005
15470 -- version of the code causes regressions in several tests that are
15471 -- compiled with -gnat95. ???)
15473 if Ada_Version < Ada_2005 then
15474 if Is_Entity_Name (Name (Obj)) then
15475 return Subprogram_Access_Level (Entity (Name (Obj)));
15476 else
15477 return Type_Access_Level (Etype (Prefix (Name (Obj))));
15478 end if;
15480 -- For Ada 2005, the level of the result object of a function call is
15481 -- defined to be the level of the call's innermost enclosing master.
15482 -- We determine that by querying the depth of the innermost enclosing
15483 -- dynamic scope.
15485 else
15486 Return_Master_Scope_Depth_Of_Call : declare
15488 function Innermost_Master_Scope_Depth
15489 (N : Node_Id) return Uint;
15490 -- Returns the scope depth of the given node's innermost
15491 -- enclosing dynamic scope (effectively the accessibility
15492 -- level of the innermost enclosing master).
15494 ----------------------------------
15495 -- Innermost_Master_Scope_Depth --
15496 ----------------------------------
15498 function Innermost_Master_Scope_Depth
15499 (N : Node_Id) return Uint
15501 Node_Par : Node_Id := Parent (N);
15503 begin
15504 -- Locate the nearest enclosing node (by traversing Parents)
15505 -- that Defining_Entity can be applied to, and return the
15506 -- depth of that entity's nearest enclosing dynamic scope.
15508 while Present (Node_Par) loop
15509 case Nkind (Node_Par) is
15510 when N_Component_Declaration |
15511 N_Entry_Declaration |
15512 N_Formal_Object_Declaration |
15513 N_Formal_Type_Declaration |
15514 N_Full_Type_Declaration |
15515 N_Incomplete_Type_Declaration |
15516 N_Loop_Parameter_Specification |
15517 N_Object_Declaration |
15518 N_Protected_Type_Declaration |
15519 N_Private_Extension_Declaration |
15520 N_Private_Type_Declaration |
15521 N_Subtype_Declaration |
15522 N_Function_Specification |
15523 N_Procedure_Specification |
15524 N_Task_Type_Declaration |
15525 N_Body_Stub |
15526 N_Generic_Instantiation |
15527 N_Proper_Body |
15528 N_Implicit_Label_Declaration |
15529 N_Package_Declaration |
15530 N_Single_Task_Declaration |
15531 N_Subprogram_Declaration |
15532 N_Generic_Declaration |
15533 N_Renaming_Declaration |
15534 N_Block_Statement |
15535 N_Formal_Subprogram_Declaration |
15536 N_Abstract_Subprogram_Declaration |
15537 N_Entry_Body |
15538 N_Exception_Declaration |
15539 N_Formal_Package_Declaration |
15540 N_Number_Declaration |
15541 N_Package_Specification |
15542 N_Parameter_Specification |
15543 N_Single_Protected_Declaration |
15544 N_Subunit =>
15546 return Scope_Depth
15547 (Nearest_Dynamic_Scope
15548 (Defining_Entity (Node_Par)));
15550 when others =>
15551 null;
15552 end case;
15554 Node_Par := Parent (Node_Par);
15555 end loop;
15557 pragma Assert (False);
15559 -- Should never reach the following return
15561 return Scope_Depth (Current_Scope) + 1;
15562 end Innermost_Master_Scope_Depth;
15564 -- Start of processing for Return_Master_Scope_Depth_Of_Call
15566 begin
15567 return Innermost_Master_Scope_Depth (Obj);
15568 end Return_Master_Scope_Depth_Of_Call;
15569 end if;
15571 -- For convenience we handle qualified expressions, even though they
15572 -- aren't technically object names.
15574 elsif Nkind (Obj) = N_Qualified_Expression then
15575 return Object_Access_Level (Expression (Obj));
15577 -- Ditto for aggregates. They have the level of the temporary that
15578 -- will hold their value.
15580 elsif Nkind (Obj) = N_Aggregate then
15581 return Object_Access_Level (Current_Scope);
15583 -- Otherwise return the scope level of Standard. (If there are cases
15584 -- that fall through to this point they will be treated as having
15585 -- global accessibility for now. ???)
15587 else
15588 return Scope_Depth (Standard_Standard);
15589 end if;
15590 end Object_Access_Level;
15592 --------------------------
15593 -- Original_Aspect_Name --
15594 --------------------------
15596 function Original_Aspect_Name (N : Node_Id) return Name_Id is
15597 Pras : Node_Id;
15598 Name : Name_Id;
15600 begin
15601 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
15602 Pras := N;
15604 if Is_Rewrite_Substitution (Pras)
15605 and then Nkind (Original_Node (Pras)) = N_Pragma
15606 then
15607 Pras := Original_Node (Pras);
15608 end if;
15610 -- Case where we came from aspect specication
15612 if Nkind (Pras) = N_Pragma and then From_Aspect_Specification (Pras) then
15613 Pras := Corresponding_Aspect (Pras);
15614 end if;
15616 -- Get name from aspect or pragma
15618 if Nkind (Pras) = N_Pragma then
15619 Name := Pragma_Name (Pras);
15620 else
15621 Name := Chars (Identifier (Pras));
15622 end if;
15624 -- Deal with 'Class
15626 if Class_Present (Pras) then
15627 case Name is
15629 -- Names that need converting to special _xxx form
15631 when Name_Pre |
15632 Name_Pre_Class =>
15633 Name := Name_uPre;
15635 when Name_Post |
15636 Name_Post_Class =>
15637 Name := Name_uPost;
15639 when Name_Invariant =>
15640 Name := Name_uInvariant;
15642 when Name_Type_Invariant |
15643 Name_Type_Invariant_Class =>
15644 Name := Name_uType_Invariant;
15646 -- Nothing to do for other cases (e.g. a Check that derived
15647 -- from Pre_Class and has the flag set). Also we do nothing
15648 -- if the name is already in special _xxx form.
15650 when others =>
15651 null;
15652 end case;
15653 end if;
15655 return Name;
15656 end Original_Aspect_Name;
15658 --------------------------------------
15659 -- Original_Corresponding_Operation --
15660 --------------------------------------
15662 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
15664 Typ : constant Entity_Id := Find_Dispatching_Type (S);
15666 begin
15667 -- If S is an inherited primitive S2 the original corresponding
15668 -- operation of S is the original corresponding operation of S2
15670 if Present (Alias (S))
15671 and then Find_Dispatching_Type (Alias (S)) /= Typ
15672 then
15673 return Original_Corresponding_Operation (Alias (S));
15675 -- If S overrides an inherited subprogram S2 the original corresponding
15676 -- operation of S is the original corresponding operation of S2
15678 elsif Present (Overridden_Operation (S)) then
15679 return Original_Corresponding_Operation (Overridden_Operation (S));
15681 -- otherwise it is S itself
15683 else
15684 return S;
15685 end if;
15686 end Original_Corresponding_Operation;
15688 ----------------------
15689 -- Policy_In_Effect --
15690 ----------------------
15692 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
15693 function Policy_In_List (List : Node_Id) return Name_Id;
15694 -- Determine the the mode of a policy in a N_Pragma list
15696 --------------------
15697 -- Policy_In_List --
15698 --------------------
15700 function Policy_In_List (List : Node_Id) return Name_Id is
15701 Arg : Node_Id;
15702 Expr : Node_Id;
15703 Prag : Node_Id;
15705 begin
15706 Prag := List;
15707 while Present (Prag) loop
15708 Arg := First (Pragma_Argument_Associations (Prag));
15709 Expr := Get_Pragma_Arg (Arg);
15711 -- The current Check_Policy pragma matches the requested policy,
15712 -- return the second argument which denotes the policy identifier.
15714 if Chars (Expr) = Policy then
15715 return Chars (Get_Pragma_Arg (Next (Arg)));
15716 end if;
15718 Prag := Next_Pragma (Prag);
15719 end loop;
15721 return No_Name;
15722 end Policy_In_List;
15724 -- Local variables
15726 Kind : Name_Id;
15728 -- Start of processing for Policy_In_Effect
15730 begin
15731 if not Is_Valid_Assertion_Kind (Policy) then
15732 raise Program_Error;
15733 end if;
15735 -- Inspect all policy pragmas that appear within scopes (if any)
15737 Kind := Policy_In_List (Check_Policy_List);
15739 -- Inspect all configuration policy pragmas (if any)
15741 if Kind = No_Name then
15742 Kind := Policy_In_List (Check_Policy_List_Config);
15743 end if;
15745 -- The context lacks policy pragmas, determine the mode based on whether
15746 -- assertions are enabled at the configuration level. This ensures that
15747 -- the policy is preserved when analyzing generics.
15749 if Kind = No_Name then
15750 if Assertions_Enabled_Config then
15751 Kind := Name_Check;
15752 else
15753 Kind := Name_Ignore;
15754 end if;
15755 end if;
15757 return Kind;
15758 end Policy_In_Effect;
15760 ----------------------------------
15761 -- Predicate_Tests_On_Arguments --
15762 ----------------------------------
15764 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
15765 begin
15766 -- Always test predicates on indirect call
15768 if Ekind (Subp) = E_Subprogram_Type then
15769 return True;
15771 -- Do not test predicates on call to generated default Finalize, since
15772 -- we are not interested in whether something we are finalizing (and
15773 -- typically destroying) satisfies its predicates.
15775 elsif Chars (Subp) = Name_Finalize
15776 and then not Comes_From_Source (Subp)
15777 then
15778 return False;
15780 -- Do not test predicates on any internally generated routines
15782 elsif Is_Internal_Name (Chars (Subp)) then
15783 return False;
15785 -- Do not test predicates on call to Init_Proc, since if needed the
15786 -- predicate test will occur at some other point.
15788 elsif Is_Init_Proc (Subp) then
15789 return False;
15791 -- Do not test predicates on call to predicate function, since this
15792 -- would cause infinite recursion.
15794 elsif Ekind (Subp) = E_Function
15795 and then (Is_Predicate_Function (Subp)
15796 or else
15797 Is_Predicate_Function_M (Subp))
15798 then
15799 return False;
15801 -- For now, no other exceptions
15803 else
15804 return True;
15805 end if;
15806 end Predicate_Tests_On_Arguments;
15808 -----------------------
15809 -- Private_Component --
15810 -----------------------
15812 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
15813 Ancestor : constant Entity_Id := Base_Type (Type_Id);
15815 function Trace_Components
15816 (T : Entity_Id;
15817 Check : Boolean) return Entity_Id;
15818 -- Recursive function that does the work, and checks against circular
15819 -- definition for each subcomponent type.
15821 ----------------------
15822 -- Trace_Components --
15823 ----------------------
15825 function Trace_Components
15826 (T : Entity_Id;
15827 Check : Boolean) return Entity_Id
15829 Btype : constant Entity_Id := Base_Type (T);
15830 Component : Entity_Id;
15831 P : Entity_Id;
15832 Candidate : Entity_Id := Empty;
15834 begin
15835 if Check and then Btype = Ancestor then
15836 Error_Msg_N ("circular type definition", Type_Id);
15837 return Any_Type;
15838 end if;
15840 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
15841 if Present (Full_View (Btype))
15842 and then Is_Record_Type (Full_View (Btype))
15843 and then not Is_Frozen (Btype)
15844 then
15845 -- To indicate that the ancestor depends on a private type, the
15846 -- current Btype is sufficient. However, to check for circular
15847 -- definition we must recurse on the full view.
15849 Candidate := Trace_Components (Full_View (Btype), True);
15851 if Candidate = Any_Type then
15852 return Any_Type;
15853 else
15854 return Btype;
15855 end if;
15857 else
15858 return Btype;
15859 end if;
15861 elsif Is_Array_Type (Btype) then
15862 return Trace_Components (Component_Type (Btype), True);
15864 elsif Is_Record_Type (Btype) then
15865 Component := First_Entity (Btype);
15866 while Present (Component)
15867 and then Comes_From_Source (Component)
15868 loop
15869 -- Skip anonymous types generated by constrained components
15871 if not Is_Type (Component) then
15872 P := Trace_Components (Etype (Component), True);
15874 if Present (P) then
15875 if P = Any_Type then
15876 return P;
15877 else
15878 Candidate := P;
15879 end if;
15880 end if;
15881 end if;
15883 Next_Entity (Component);
15884 end loop;
15886 return Candidate;
15888 else
15889 return Empty;
15890 end if;
15891 end Trace_Components;
15893 -- Start of processing for Private_Component
15895 begin
15896 return Trace_Components (Type_Id, False);
15897 end Private_Component;
15899 ---------------------------
15900 -- Primitive_Names_Match --
15901 ---------------------------
15903 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
15905 function Non_Internal_Name (E : Entity_Id) return Name_Id;
15906 -- Given an internal name, returns the corresponding non-internal name
15908 ------------------------
15909 -- Non_Internal_Name --
15910 ------------------------
15912 function Non_Internal_Name (E : Entity_Id) return Name_Id is
15913 begin
15914 Get_Name_String (Chars (E));
15915 Name_Len := Name_Len - 1;
15916 return Name_Find;
15917 end Non_Internal_Name;
15919 -- Start of processing for Primitive_Names_Match
15921 begin
15922 pragma Assert (Present (E1) and then Present (E2));
15924 return Chars (E1) = Chars (E2)
15925 or else
15926 (not Is_Internal_Name (Chars (E1))
15927 and then Is_Internal_Name (Chars (E2))
15928 and then Non_Internal_Name (E2) = Chars (E1))
15929 or else
15930 (not Is_Internal_Name (Chars (E2))
15931 and then Is_Internal_Name (Chars (E1))
15932 and then Non_Internal_Name (E1) = Chars (E2))
15933 or else
15934 (Is_Predefined_Dispatching_Operation (E1)
15935 and then Is_Predefined_Dispatching_Operation (E2)
15936 and then Same_TSS (E1, E2))
15937 or else
15938 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
15939 end Primitive_Names_Match;
15941 -----------------------
15942 -- Process_End_Label --
15943 -----------------------
15945 procedure Process_End_Label
15946 (N : Node_Id;
15947 Typ : Character;
15948 Ent : Entity_Id)
15950 Loc : Source_Ptr;
15951 Nam : Node_Id;
15952 Scop : Entity_Id;
15954 Label_Ref : Boolean;
15955 -- Set True if reference to end label itself is required
15957 Endl : Node_Id;
15958 -- Gets set to the operator symbol or identifier that references the
15959 -- entity Ent. For the child unit case, this is the identifier from the
15960 -- designator. For other cases, this is simply Endl.
15962 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
15963 -- N is an identifier node that appears as a parent unit reference in
15964 -- the case where Ent is a child unit. This procedure generates an
15965 -- appropriate cross-reference entry. E is the corresponding entity.
15967 -------------------------
15968 -- Generate_Parent_Ref --
15969 -------------------------
15971 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
15972 begin
15973 -- If names do not match, something weird, skip reference
15975 if Chars (E) = Chars (N) then
15977 -- Generate the reference. We do NOT consider this as a reference
15978 -- for unreferenced symbol purposes.
15980 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
15982 if Style_Check then
15983 Style.Check_Identifier (N, E);
15984 end if;
15985 end if;
15986 end Generate_Parent_Ref;
15988 -- Start of processing for Process_End_Label
15990 begin
15991 -- If no node, ignore. This happens in some error situations, and
15992 -- also for some internally generated structures where no end label
15993 -- references are required in any case.
15995 if No (N) then
15996 return;
15997 end if;
15999 -- Nothing to do if no End_Label, happens for internally generated
16000 -- constructs where we don't want an end label reference anyway. Also
16001 -- nothing to do if Endl is a string literal, which means there was
16002 -- some prior error (bad operator symbol)
16004 Endl := End_Label (N);
16006 if No (Endl) or else Nkind (Endl) = N_String_Literal then
16007 return;
16008 end if;
16010 -- Reference node is not in extended main source unit
16012 if not In_Extended_Main_Source_Unit (N) then
16014 -- Generally we do not collect references except for the extended
16015 -- main source unit. The one exception is the 'e' entry for a
16016 -- package spec, where it is useful for a client to have the
16017 -- ending information to define scopes.
16019 if Typ /= 'e' then
16020 return;
16022 else
16023 Label_Ref := False;
16025 -- For this case, we can ignore any parent references, but we
16026 -- need the package name itself for the 'e' entry.
16028 if Nkind (Endl) = N_Designator then
16029 Endl := Identifier (Endl);
16030 end if;
16031 end if;
16033 -- Reference is in extended main source unit
16035 else
16036 Label_Ref := True;
16038 -- For designator, generate references for the parent entries
16040 if Nkind (Endl) = N_Designator then
16042 -- Generate references for the prefix if the END line comes from
16043 -- source (otherwise we do not need these references) We climb the
16044 -- scope stack to find the expected entities.
16046 if Comes_From_Source (Endl) then
16047 Nam := Name (Endl);
16048 Scop := Current_Scope;
16049 while Nkind (Nam) = N_Selected_Component loop
16050 Scop := Scope (Scop);
16051 exit when No (Scop);
16052 Generate_Parent_Ref (Selector_Name (Nam), Scop);
16053 Nam := Prefix (Nam);
16054 end loop;
16056 if Present (Scop) then
16057 Generate_Parent_Ref (Nam, Scope (Scop));
16058 end if;
16059 end if;
16061 Endl := Identifier (Endl);
16062 end if;
16063 end if;
16065 -- If the end label is not for the given entity, then either we have
16066 -- some previous error, or this is a generic instantiation for which
16067 -- we do not need to make a cross-reference in this case anyway. In
16068 -- either case we simply ignore the call.
16070 if Chars (Ent) /= Chars (Endl) then
16071 return;
16072 end if;
16074 -- If label was really there, then generate a normal reference and then
16075 -- adjust the location in the end label to point past the name (which
16076 -- should almost always be the semicolon).
16078 Loc := Sloc (Endl);
16080 if Comes_From_Source (Endl) then
16082 -- If a label reference is required, then do the style check and
16083 -- generate an l-type cross-reference entry for the label
16085 if Label_Ref then
16086 if Style_Check then
16087 Style.Check_Identifier (Endl, Ent);
16088 end if;
16090 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
16091 end if;
16093 -- Set the location to point past the label (normally this will
16094 -- mean the semicolon immediately following the label). This is
16095 -- done for the sake of the 'e' or 't' entry generated below.
16097 Get_Decoded_Name_String (Chars (Endl));
16098 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
16100 else
16101 -- In SPARK mode, no missing label is allowed for packages and
16102 -- subprogram bodies. Detect those cases by testing whether
16103 -- Process_End_Label was called for a body (Typ = 't') or a package.
16105 if Restriction_Check_Required (SPARK_05)
16106 and then (Typ = 't' or else Ekind (Ent) = E_Package)
16107 then
16108 Error_Msg_Node_1 := Endl;
16109 Check_SPARK_05_Restriction
16110 ("`END &` required", Endl, Force => True);
16111 end if;
16112 end if;
16114 -- Now generate the e/t reference
16116 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
16118 -- Restore Sloc, in case modified above, since we have an identifier
16119 -- and the normal Sloc should be left set in the tree.
16121 Set_Sloc (Endl, Loc);
16122 end Process_End_Label;
16124 ----------------
16125 -- Referenced --
16126 ----------------
16128 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
16129 Seen : Boolean := False;
16131 function Is_Reference (N : Node_Id) return Traverse_Result;
16132 -- Determine whether node N denotes a reference to Id. If this is the
16133 -- case, set global flag Seen to True and stop the traversal.
16135 ------------------
16136 -- Is_Reference --
16137 ------------------
16139 function Is_Reference (N : Node_Id) return Traverse_Result is
16140 begin
16141 if Is_Entity_Name (N)
16142 and then Present (Entity (N))
16143 and then Entity (N) = Id
16144 then
16145 Seen := True;
16146 return Abandon;
16147 else
16148 return OK;
16149 end if;
16150 end Is_Reference;
16152 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
16154 -- Start of processing for Referenced
16156 begin
16157 Inspect_Expression (Expr);
16158 return Seen;
16159 end Referenced;
16161 ------------------------------------
16162 -- References_Generic_Formal_Type --
16163 ------------------------------------
16165 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
16167 function Process (N : Node_Id) return Traverse_Result;
16168 -- Process one node in search for generic formal type
16170 -------------
16171 -- Process --
16172 -------------
16174 function Process (N : Node_Id) return Traverse_Result is
16175 begin
16176 if Nkind (N) in N_Has_Entity then
16177 declare
16178 E : constant Entity_Id := Entity (N);
16179 begin
16180 if Present (E) then
16181 if Is_Generic_Type (E) then
16182 return Abandon;
16183 elsif Present (Etype (E))
16184 and then Is_Generic_Type (Etype (E))
16185 then
16186 return Abandon;
16187 end if;
16188 end if;
16189 end;
16190 end if;
16192 return Atree.OK;
16193 end Process;
16195 function Traverse is new Traverse_Func (Process);
16196 -- Traverse tree to look for generic type
16198 begin
16199 if Inside_A_Generic then
16200 return Traverse (N) = Abandon;
16201 else
16202 return False;
16203 end if;
16204 end References_Generic_Formal_Type;
16206 --------------------
16207 -- Remove_Homonym --
16208 --------------------
16210 procedure Remove_Homonym (E : Entity_Id) is
16211 Prev : Entity_Id := Empty;
16212 H : Entity_Id;
16214 begin
16215 if E = Current_Entity (E) then
16216 if Present (Homonym (E)) then
16217 Set_Current_Entity (Homonym (E));
16218 else
16219 Set_Name_Entity_Id (Chars (E), Empty);
16220 end if;
16222 else
16223 H := Current_Entity (E);
16224 while Present (H) and then H /= E loop
16225 Prev := H;
16226 H := Homonym (H);
16227 end loop;
16229 -- If E is not on the homonym chain, nothing to do
16231 if Present (H) then
16232 Set_Homonym (Prev, Homonym (E));
16233 end if;
16234 end if;
16235 end Remove_Homonym;
16237 ---------------------
16238 -- Rep_To_Pos_Flag --
16239 ---------------------
16241 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
16242 begin
16243 return New_Occurrence_Of
16244 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
16245 end Rep_To_Pos_Flag;
16247 --------------------
16248 -- Require_Entity --
16249 --------------------
16251 procedure Require_Entity (N : Node_Id) is
16252 begin
16253 if Is_Entity_Name (N) and then No (Entity (N)) then
16254 if Total_Errors_Detected /= 0 then
16255 Set_Entity (N, Any_Id);
16256 else
16257 raise Program_Error;
16258 end if;
16259 end if;
16260 end Require_Entity;
16262 -------------------------------
16263 -- Requires_State_Refinement --
16264 -------------------------------
16266 function Requires_State_Refinement
16267 (Spec_Id : Entity_Id;
16268 Body_Id : Entity_Id) return Boolean
16270 function Mode_Is_Off (Prag : Node_Id) return Boolean;
16271 -- Given pragma SPARK_Mode, determine whether the mode is Off
16273 -----------------
16274 -- Mode_Is_Off --
16275 -----------------
16277 function Mode_Is_Off (Prag : Node_Id) return Boolean is
16278 Mode : Node_Id;
16280 begin
16281 -- The default SPARK mode is On
16283 if No (Prag) then
16284 return False;
16285 end if;
16287 Mode := Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
16289 -- Then the pragma lacks an argument, the default mode is On
16291 if No (Mode) then
16292 return False;
16293 else
16294 return Chars (Mode) = Name_Off;
16295 end if;
16296 end Mode_Is_Off;
16298 -- Start of processing for Requires_State_Refinement
16300 begin
16301 -- A package that does not define at least one abstract state cannot
16302 -- possibly require refinement.
16304 if No (Abstract_States (Spec_Id)) then
16305 return False;
16307 -- The package instroduces a single null state which does not merit
16308 -- refinement.
16310 elsif Has_Null_Abstract_State (Spec_Id) then
16311 return False;
16313 -- Check whether the package body is subject to pragma SPARK_Mode. If
16314 -- it is and the mode is Off, the package body is considered to be in
16315 -- regular Ada and does not require refinement.
16317 elsif Mode_Is_Off (SPARK_Pragma (Body_Id)) then
16318 return False;
16320 -- The body's SPARK_Mode may be inherited from a similar pragma that
16321 -- appears in the private declarations of the spec. The pragma we are
16322 -- interested appears as the second entry in SPARK_Pragma.
16324 elsif Present (SPARK_Pragma (Spec_Id))
16325 and then Mode_Is_Off (Next_Pragma (SPARK_Pragma (Spec_Id)))
16326 then
16327 return False;
16329 -- The spec defines at least one abstract state and the body has no way
16330 -- of circumventing the refinement.
16332 else
16333 return True;
16334 end if;
16335 end Requires_State_Refinement;
16337 ------------------------------
16338 -- Requires_Transient_Scope --
16339 ------------------------------
16341 -- A transient scope is required when variable-sized temporaries are
16342 -- allocated in the primary or secondary stack, or when finalization
16343 -- actions must be generated before the next instruction.
16345 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
16346 Typ : constant Entity_Id := Underlying_Type (Id);
16348 -- Start of processing for Requires_Transient_Scope
16350 begin
16351 -- This is a private type which is not completed yet. This can only
16352 -- happen in a default expression (of a formal parameter or of a
16353 -- record component). Do not expand transient scope in this case
16355 if No (Typ) then
16356 return False;
16358 -- Do not expand transient scope for non-existent procedure return
16360 elsif Typ = Standard_Void_Type then
16361 return False;
16363 -- Elementary types do not require a transient scope
16365 elsif Is_Elementary_Type (Typ) then
16366 return False;
16368 -- Generally, indefinite subtypes require a transient scope, since the
16369 -- back end cannot generate temporaries, since this is not a valid type
16370 -- for declaring an object. It might be possible to relax this in the
16371 -- future, e.g. by declaring the maximum possible space for the type.
16373 elsif Is_Indefinite_Subtype (Typ) then
16374 return True;
16376 -- Functions returning tagged types may dispatch on result so their
16377 -- returned value is allocated on the secondary stack. Controlled
16378 -- type temporaries need finalization.
16380 elsif Is_Tagged_Type (Typ)
16381 or else Has_Controlled_Component (Typ)
16382 then
16383 return not Is_Value_Type (Typ);
16385 -- Record type
16387 elsif Is_Record_Type (Typ) then
16388 declare
16389 Comp : Entity_Id;
16390 begin
16391 Comp := First_Entity (Typ);
16392 while Present (Comp) loop
16393 if Ekind (Comp) = E_Component
16394 and then Requires_Transient_Scope (Etype (Comp))
16395 then
16396 return True;
16397 else
16398 Next_Entity (Comp);
16399 end if;
16400 end loop;
16401 end;
16403 return False;
16405 -- String literal types never require transient scope
16407 elsif Ekind (Typ) = E_String_Literal_Subtype then
16408 return False;
16410 -- Array type. Note that we already know that this is a constrained
16411 -- array, since unconstrained arrays will fail the indefinite test.
16413 elsif Is_Array_Type (Typ) then
16415 -- If component type requires a transient scope, the array does too
16417 if Requires_Transient_Scope (Component_Type (Typ)) then
16418 return True;
16420 -- Otherwise, we only need a transient scope if the size depends on
16421 -- the value of one or more discriminants.
16423 else
16424 return Size_Depends_On_Discriminant (Typ);
16425 end if;
16427 -- All other cases do not require a transient scope
16429 else
16430 return False;
16431 end if;
16432 end Requires_Transient_Scope;
16434 --------------------------
16435 -- Reset_Analyzed_Flags --
16436 --------------------------
16438 procedure Reset_Analyzed_Flags (N : Node_Id) is
16440 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
16441 -- Function used to reset Analyzed flags in tree. Note that we do
16442 -- not reset Analyzed flags in entities, since there is no need to
16443 -- reanalyze entities, and indeed, it is wrong to do so, since it
16444 -- can result in generating auxiliary stuff more than once.
16446 --------------------
16447 -- Clear_Analyzed --
16448 --------------------
16450 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
16451 begin
16452 if not Has_Extension (N) then
16453 Set_Analyzed (N, False);
16454 end if;
16456 return OK;
16457 end Clear_Analyzed;
16459 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
16461 -- Start of processing for Reset_Analyzed_Flags
16463 begin
16464 Reset_Analyzed (N);
16465 end Reset_Analyzed_Flags;
16467 ------------------------
16468 -- Restore_SPARK_Mode --
16469 ------------------------
16471 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
16472 begin
16473 SPARK_Mode := Mode;
16474 end Restore_SPARK_Mode;
16476 --------------------------------
16477 -- Returns_Unconstrained_Type --
16478 --------------------------------
16480 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
16481 begin
16482 return Ekind (Subp) = E_Function
16483 and then not Is_Scalar_Type (Etype (Subp))
16484 and then not Is_Access_Type (Etype (Subp))
16485 and then not Is_Constrained (Etype (Subp));
16486 end Returns_Unconstrained_Type;
16488 ----------------------------
16489 -- Root_Type_Of_Full_View --
16490 ----------------------------
16492 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
16493 Rtyp : constant Entity_Id := Root_Type (T);
16495 begin
16496 -- The root type of the full view may itself be a private type. Keep
16497 -- looking for the ultimate derivation parent.
16499 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
16500 return Root_Type_Of_Full_View (Full_View (Rtyp));
16501 else
16502 return Rtyp;
16503 end if;
16504 end Root_Type_Of_Full_View;
16506 ---------------------------
16507 -- Safe_To_Capture_Value --
16508 ---------------------------
16510 function Safe_To_Capture_Value
16511 (N : Node_Id;
16512 Ent : Entity_Id;
16513 Cond : Boolean := False) return Boolean
16515 begin
16516 -- The only entities for which we track constant values are variables
16517 -- which are not renamings, constants, out parameters, and in out
16518 -- parameters, so check if we have this case.
16520 -- Note: it may seem odd to track constant values for constants, but in
16521 -- fact this routine is used for other purposes than simply capturing
16522 -- the value. In particular, the setting of Known[_Non]_Null.
16524 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
16525 or else
16526 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
16527 then
16528 null;
16530 -- For conditionals, we also allow loop parameters and all formals,
16531 -- including in parameters.
16533 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
16534 null;
16536 -- For all other cases, not just unsafe, but impossible to capture
16537 -- Current_Value, since the above are the only entities which have
16538 -- Current_Value fields.
16540 else
16541 return False;
16542 end if;
16544 -- Skip if volatile or aliased, since funny things might be going on in
16545 -- these cases which we cannot necessarily track. Also skip any variable
16546 -- for which an address clause is given, or whose address is taken. Also
16547 -- never capture value of library level variables (an attempt to do so
16548 -- can occur in the case of package elaboration code).
16550 if Treat_As_Volatile (Ent)
16551 or else Is_Aliased (Ent)
16552 or else Present (Address_Clause (Ent))
16553 or else Address_Taken (Ent)
16554 or else (Is_Library_Level_Entity (Ent)
16555 and then Ekind (Ent) = E_Variable)
16556 then
16557 return False;
16558 end if;
16560 -- OK, all above conditions are met. We also require that the scope of
16561 -- the reference be the same as the scope of the entity, not counting
16562 -- packages and blocks and loops.
16564 declare
16565 E_Scope : constant Entity_Id := Scope (Ent);
16566 R_Scope : Entity_Id;
16568 begin
16569 R_Scope := Current_Scope;
16570 while R_Scope /= Standard_Standard loop
16571 exit when R_Scope = E_Scope;
16573 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
16574 return False;
16575 else
16576 R_Scope := Scope (R_Scope);
16577 end if;
16578 end loop;
16579 end;
16581 -- We also require that the reference does not appear in a context
16582 -- where it is not sure to be executed (i.e. a conditional context
16583 -- or an exception handler). We skip this if Cond is True, since the
16584 -- capturing of values from conditional tests handles this ok.
16586 if Cond then
16587 return True;
16588 end if;
16590 declare
16591 Desc : Node_Id;
16592 P : Node_Id;
16594 begin
16595 Desc := N;
16597 -- Seems dubious that case expressions are not handled here ???
16599 P := Parent (N);
16600 while Present (P) loop
16601 if Nkind (P) = N_If_Statement
16602 or else Nkind (P) = N_Case_Statement
16603 or else (Nkind (P) in N_Short_Circuit
16604 and then Desc = Right_Opnd (P))
16605 or else (Nkind (P) = N_If_Expression
16606 and then Desc /= First (Expressions (P)))
16607 or else Nkind (P) = N_Exception_Handler
16608 or else Nkind (P) = N_Selective_Accept
16609 or else Nkind (P) = N_Conditional_Entry_Call
16610 or else Nkind (P) = N_Timed_Entry_Call
16611 or else Nkind (P) = N_Asynchronous_Select
16612 then
16613 return False;
16615 else
16616 Desc := P;
16617 P := Parent (P);
16619 -- A special Ada 2012 case: the original node may be part
16620 -- of the else_actions of a conditional expression, in which
16621 -- case it might not have been expanded yet, and appears in
16622 -- a non-syntactic list of actions. In that case it is clearly
16623 -- not safe to save a value.
16625 if No (P)
16626 and then Is_List_Member (Desc)
16627 and then No (Parent (List_Containing (Desc)))
16628 then
16629 return False;
16630 end if;
16631 end if;
16632 end loop;
16633 end;
16635 -- OK, looks safe to set value
16637 return True;
16638 end Safe_To_Capture_Value;
16640 ---------------
16641 -- Same_Name --
16642 ---------------
16644 function Same_Name (N1, N2 : Node_Id) return Boolean is
16645 K1 : constant Node_Kind := Nkind (N1);
16646 K2 : constant Node_Kind := Nkind (N2);
16648 begin
16649 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
16650 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
16651 then
16652 return Chars (N1) = Chars (N2);
16654 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
16655 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
16656 then
16657 return Same_Name (Selector_Name (N1), Selector_Name (N2))
16658 and then Same_Name (Prefix (N1), Prefix (N2));
16660 else
16661 return False;
16662 end if;
16663 end Same_Name;
16665 -----------------
16666 -- Same_Object --
16667 -----------------
16669 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
16670 N1 : constant Node_Id := Original_Node (Node1);
16671 N2 : constant Node_Id := Original_Node (Node2);
16672 -- We do the tests on original nodes, since we are most interested
16673 -- in the original source, not any expansion that got in the way.
16675 K1 : constant Node_Kind := Nkind (N1);
16676 K2 : constant Node_Kind := Nkind (N2);
16678 begin
16679 -- First case, both are entities with same entity
16681 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
16682 declare
16683 EN1 : constant Entity_Id := Entity (N1);
16684 EN2 : constant Entity_Id := Entity (N2);
16685 begin
16686 if Present (EN1) and then Present (EN2)
16687 and then (Ekind_In (EN1, E_Variable, E_Constant)
16688 or else Is_Formal (EN1))
16689 and then EN1 = EN2
16690 then
16691 return True;
16692 end if;
16693 end;
16694 end if;
16696 -- Second case, selected component with same selector, same record
16698 if K1 = N_Selected_Component
16699 and then K2 = N_Selected_Component
16700 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
16701 then
16702 return Same_Object (Prefix (N1), Prefix (N2));
16704 -- Third case, indexed component with same subscripts, same array
16706 elsif K1 = N_Indexed_Component
16707 and then K2 = N_Indexed_Component
16708 and then Same_Object (Prefix (N1), Prefix (N2))
16709 then
16710 declare
16711 E1, E2 : Node_Id;
16712 begin
16713 E1 := First (Expressions (N1));
16714 E2 := First (Expressions (N2));
16715 while Present (E1) loop
16716 if not Same_Value (E1, E2) then
16717 return False;
16718 else
16719 Next (E1);
16720 Next (E2);
16721 end if;
16722 end loop;
16724 return True;
16725 end;
16727 -- Fourth case, slice of same array with same bounds
16729 elsif K1 = N_Slice
16730 and then K2 = N_Slice
16731 and then Nkind (Discrete_Range (N1)) = N_Range
16732 and then Nkind (Discrete_Range (N2)) = N_Range
16733 and then Same_Value (Low_Bound (Discrete_Range (N1)),
16734 Low_Bound (Discrete_Range (N2)))
16735 and then Same_Value (High_Bound (Discrete_Range (N1)),
16736 High_Bound (Discrete_Range (N2)))
16737 then
16738 return Same_Name (Prefix (N1), Prefix (N2));
16740 -- All other cases, not clearly the same object
16742 else
16743 return False;
16744 end if;
16745 end Same_Object;
16747 ---------------
16748 -- Same_Type --
16749 ---------------
16751 function Same_Type (T1, T2 : Entity_Id) return Boolean is
16752 begin
16753 if T1 = T2 then
16754 return True;
16756 elsif not Is_Constrained (T1)
16757 and then not Is_Constrained (T2)
16758 and then Base_Type (T1) = Base_Type (T2)
16759 then
16760 return True;
16762 -- For now don't bother with case of identical constraints, to be
16763 -- fiddled with later on perhaps (this is only used for optimization
16764 -- purposes, so it is not critical to do a best possible job)
16766 else
16767 return False;
16768 end if;
16769 end Same_Type;
16771 ----------------
16772 -- Same_Value --
16773 ----------------
16775 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
16776 begin
16777 if Compile_Time_Known_Value (Node1)
16778 and then Compile_Time_Known_Value (Node2)
16779 and then Expr_Value (Node1) = Expr_Value (Node2)
16780 then
16781 return True;
16782 elsif Same_Object (Node1, Node2) then
16783 return True;
16784 else
16785 return False;
16786 end if;
16787 end Same_Value;
16789 -----------------------------
16790 -- Save_SPARK_Mode_And_Set --
16791 -----------------------------
16793 procedure Save_SPARK_Mode_And_Set
16794 (Context : Entity_Id;
16795 Mode : out SPARK_Mode_Type)
16797 begin
16798 -- Save the current mode in effect
16800 Mode := SPARK_Mode;
16802 -- Do not consider illegal or partially decorated constructs
16804 if Ekind (Context) = E_Void or else Error_Posted (Context) then
16805 null;
16807 elsif Present (SPARK_Pragma (Context)) then
16808 SPARK_Mode := Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Context));
16809 end if;
16810 end Save_SPARK_Mode_And_Set;
16812 -------------------------
16813 -- Scalar_Part_Present --
16814 -------------------------
16816 function Scalar_Part_Present (T : Entity_Id) return Boolean is
16817 C : Entity_Id;
16819 begin
16820 if Is_Scalar_Type (T) then
16821 return True;
16823 elsif Is_Array_Type (T) then
16824 return Scalar_Part_Present (Component_Type (T));
16826 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
16827 C := First_Component_Or_Discriminant (T);
16828 while Present (C) loop
16829 if Scalar_Part_Present (Etype (C)) then
16830 return True;
16831 else
16832 Next_Component_Or_Discriminant (C);
16833 end if;
16834 end loop;
16835 end if;
16837 return False;
16838 end Scalar_Part_Present;
16840 ------------------------
16841 -- Scope_Is_Transient --
16842 ------------------------
16844 function Scope_Is_Transient return Boolean is
16845 begin
16846 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
16847 end Scope_Is_Transient;
16849 ------------------
16850 -- Scope_Within --
16851 ------------------
16853 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
16854 Scop : Entity_Id;
16856 begin
16857 Scop := Scope1;
16858 while Scop /= Standard_Standard loop
16859 Scop := Scope (Scop);
16861 if Scop = Scope2 then
16862 return True;
16863 end if;
16864 end loop;
16866 return False;
16867 end Scope_Within;
16869 --------------------------
16870 -- Scope_Within_Or_Same --
16871 --------------------------
16873 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
16874 Scop : Entity_Id;
16876 begin
16877 Scop := Scope1;
16878 while Scop /= Standard_Standard loop
16879 if Scop = Scope2 then
16880 return True;
16881 else
16882 Scop := Scope (Scop);
16883 end if;
16884 end loop;
16886 return False;
16887 end Scope_Within_Or_Same;
16889 --------------------
16890 -- Set_Convention --
16891 --------------------
16893 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
16894 begin
16895 Basic_Set_Convention (E, Val);
16897 if Is_Type (E)
16898 and then Is_Access_Subprogram_Type (Base_Type (E))
16899 and then Has_Foreign_Convention (E)
16900 then
16901 Set_Can_Use_Internal_Rep (E, False);
16902 end if;
16904 -- If E is an object or component, and the type of E is an anonymous
16905 -- access type with no convention set, then also set the convention of
16906 -- the anonymous access type. We do not do this for anonymous protected
16907 -- types, since protected types always have the default convention.
16909 if Present (Etype (E))
16910 and then (Is_Object (E)
16911 or else Ekind (E) = E_Component
16913 -- Allow E_Void (happens for pragma Convention appearing
16914 -- in the middle of a record applying to a component)
16916 or else Ekind (E) = E_Void)
16917 then
16918 declare
16919 Typ : constant Entity_Id := Etype (E);
16921 begin
16922 if Ekind_In (Typ, E_Anonymous_Access_Type,
16923 E_Anonymous_Access_Subprogram_Type)
16924 and then not Has_Convention_Pragma (Typ)
16925 then
16926 Basic_Set_Convention (Typ, Val);
16927 Set_Has_Convention_Pragma (Typ);
16929 -- And for the access subprogram type, deal similarly with the
16930 -- designated E_Subprogram_Type if it is also internal (which
16931 -- it always is?)
16933 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
16934 declare
16935 Dtype : constant Entity_Id := Designated_Type (Typ);
16936 begin
16937 if Ekind (Dtype) = E_Subprogram_Type
16938 and then Is_Itype (Dtype)
16939 and then not Has_Convention_Pragma (Dtype)
16940 then
16941 Basic_Set_Convention (Dtype, Val);
16942 Set_Has_Convention_Pragma (Dtype);
16943 end if;
16944 end;
16945 end if;
16946 end if;
16947 end;
16948 end if;
16949 end Set_Convention;
16951 ------------------------
16952 -- Set_Current_Entity --
16953 ------------------------
16955 -- The given entity is to be set as the currently visible definition of its
16956 -- associated name (i.e. the Node_Id associated with its name). All we have
16957 -- to do is to get the name from the identifier, and then set the
16958 -- associated Node_Id to point to the given entity.
16960 procedure Set_Current_Entity (E : Entity_Id) is
16961 begin
16962 Set_Name_Entity_Id (Chars (E), E);
16963 end Set_Current_Entity;
16965 ---------------------------
16966 -- Set_Debug_Info_Needed --
16967 ---------------------------
16969 procedure Set_Debug_Info_Needed (T : Entity_Id) is
16971 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
16972 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
16973 -- Used to set debug info in a related node if not set already
16975 --------------------------------------
16976 -- Set_Debug_Info_Needed_If_Not_Set --
16977 --------------------------------------
16979 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
16980 begin
16981 if Present (E) and then not Needs_Debug_Info (E) then
16982 Set_Debug_Info_Needed (E);
16984 -- For a private type, indicate that the full view also needs
16985 -- debug information.
16987 if Is_Type (E)
16988 and then Is_Private_Type (E)
16989 and then Present (Full_View (E))
16990 then
16991 Set_Debug_Info_Needed (Full_View (E));
16992 end if;
16993 end if;
16994 end Set_Debug_Info_Needed_If_Not_Set;
16996 -- Start of processing for Set_Debug_Info_Needed
16998 begin
16999 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
17000 -- indicates that Debug_Info_Needed is never required for the entity.
17001 -- Nothing to do if entity comes from a predefined file. Library files
17002 -- are compiled without debug information, but inlined bodies of these
17003 -- routines may appear in user code, and debug information on them ends
17004 -- up complicating debugging the user code.
17006 if No (T)
17007 or else Debug_Info_Off (T)
17008 then
17009 return;
17011 elsif In_Inlined_Body
17012 and then Is_Predefined_File_Name
17013 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
17014 then
17015 Set_Needs_Debug_Info (T, False);
17016 end if;
17018 -- Set flag in entity itself. Note that we will go through the following
17019 -- circuitry even if the flag is already set on T. That's intentional,
17020 -- it makes sure that the flag will be set in subsidiary entities.
17022 Set_Needs_Debug_Info (T);
17024 -- Set flag on subsidiary entities if not set already
17026 if Is_Object (T) then
17027 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
17029 elsif Is_Type (T) then
17030 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
17032 if Is_Record_Type (T) then
17033 declare
17034 Ent : Entity_Id := First_Entity (T);
17035 begin
17036 while Present (Ent) loop
17037 Set_Debug_Info_Needed_If_Not_Set (Ent);
17038 Next_Entity (Ent);
17039 end loop;
17040 end;
17042 -- For a class wide subtype, we also need debug information
17043 -- for the equivalent type.
17045 if Ekind (T) = E_Class_Wide_Subtype then
17046 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
17047 end if;
17049 elsif Is_Array_Type (T) then
17050 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
17052 declare
17053 Indx : Node_Id := First_Index (T);
17054 begin
17055 while Present (Indx) loop
17056 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
17057 Indx := Next_Index (Indx);
17058 end loop;
17059 end;
17061 -- For a packed array type, we also need debug information for
17062 -- the type used to represent the packed array. Conversely, we
17063 -- also need it for the former if we need it for the latter.
17065 if Is_Packed (T) then
17066 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
17067 end if;
17069 if Is_Packed_Array_Impl_Type (T) then
17070 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
17071 end if;
17073 elsif Is_Access_Type (T) then
17074 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
17076 elsif Is_Private_Type (T) then
17077 Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
17079 elsif Is_Protected_Type (T) then
17080 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
17082 elsif Is_Scalar_Type (T) then
17084 -- If the subrange bounds are materialized by dedicated constant
17085 -- objects, also include them in the debug info to make sure the
17086 -- debugger can properly use them.
17088 if Present (Scalar_Range (T))
17089 and then Nkind (Scalar_Range (T)) = N_Range
17090 then
17091 declare
17092 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
17093 High_Bnd : constant Node_Id := Type_High_Bound (T);
17095 begin
17096 if Is_Entity_Name (Low_Bnd) then
17097 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
17098 end if;
17100 if Is_Entity_Name (High_Bnd) then
17101 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
17102 end if;
17103 end;
17104 end if;
17105 end if;
17106 end if;
17107 end Set_Debug_Info_Needed;
17109 ----------------------------
17110 -- Set_Entity_With_Checks --
17111 ----------------------------
17113 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
17114 Val_Actual : Entity_Id;
17115 Nod : Node_Id;
17116 Post_Node : Node_Id;
17118 begin
17119 -- Unconditionally set the entity
17121 Set_Entity (N, Val);
17123 -- The node to post on is the selector in the case of an expanded name,
17124 -- and otherwise the node itself.
17126 if Nkind (N) = N_Expanded_Name then
17127 Post_Node := Selector_Name (N);
17128 else
17129 Post_Node := N;
17130 end if;
17132 -- Check for violation of No_Fixed_IO
17134 if Restriction_Check_Required (No_Fixed_IO)
17135 and then
17136 ((RTU_Loaded (Ada_Text_IO)
17137 and then (Is_RTE (Val, RE_Decimal_IO)
17138 or else
17139 Is_RTE (Val, RE_Fixed_IO)))
17141 or else
17142 (RTU_Loaded (Ada_Wide_Text_IO)
17143 and then (Is_RTE (Val, RO_WT_Decimal_IO)
17144 or else
17145 Is_RTE (Val, RO_WT_Fixed_IO)))
17147 or else
17148 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
17149 and then (Is_RTE (Val, RO_WW_Decimal_IO)
17150 or else
17151 Is_RTE (Val, RO_WW_Fixed_IO))))
17153 -- A special extra check, don't complain about a reference from within
17154 -- the Ada.Interrupts package itself!
17156 and then not In_Same_Extended_Unit (N, Val)
17157 then
17158 Check_Restriction (No_Fixed_IO, Post_Node);
17159 end if;
17161 -- Remaining checks are only done on source nodes. Note that we test
17162 -- for violation of No_Fixed_IO even on non-source nodes, because the
17163 -- cases for checking violations of this restriction are instantiations
17164 -- where the reference in the instance has Comes_From_Source False.
17166 if not Comes_From_Source (N) then
17167 return;
17168 end if;
17170 -- Check for violation of No_Abort_Statements, which is triggered by
17171 -- call to Ada.Task_Identification.Abort_Task.
17173 if Restriction_Check_Required (No_Abort_Statements)
17174 and then (Is_RTE (Val, RE_Abort_Task))
17176 -- A special extra check, don't complain about a reference from within
17177 -- the Ada.Task_Identification package itself!
17179 and then not In_Same_Extended_Unit (N, Val)
17180 then
17181 Check_Restriction (No_Abort_Statements, Post_Node);
17182 end if;
17184 if Val = Standard_Long_Long_Integer then
17185 Check_Restriction (No_Long_Long_Integers, Post_Node);
17186 end if;
17188 -- Check for violation of No_Dynamic_Attachment
17190 if Restriction_Check_Required (No_Dynamic_Attachment)
17191 and then RTU_Loaded (Ada_Interrupts)
17192 and then (Is_RTE (Val, RE_Is_Reserved) or else
17193 Is_RTE (Val, RE_Is_Attached) or else
17194 Is_RTE (Val, RE_Current_Handler) or else
17195 Is_RTE (Val, RE_Attach_Handler) or else
17196 Is_RTE (Val, RE_Exchange_Handler) or else
17197 Is_RTE (Val, RE_Detach_Handler) or else
17198 Is_RTE (Val, RE_Reference))
17200 -- A special extra check, don't complain about a reference from within
17201 -- the Ada.Interrupts package itself!
17203 and then not In_Same_Extended_Unit (N, Val)
17204 then
17205 Check_Restriction (No_Dynamic_Attachment, Post_Node);
17206 end if;
17208 -- Check for No_Implementation_Identifiers
17210 if Restriction_Check_Required (No_Implementation_Identifiers) then
17212 -- We have an implementation defined entity if it is marked as
17213 -- implementation defined, or is defined in a package marked as
17214 -- implementation defined. However, library packages themselves
17215 -- are excluded (we don't want to flag Interfaces itself, just
17216 -- the entities within it).
17218 if (Is_Implementation_Defined (Val)
17219 or else
17220 (Present (Scope (Val))
17221 and then Is_Implementation_Defined (Scope (Val))))
17222 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
17223 and then Is_Library_Level_Entity (Val))
17224 then
17225 Check_Restriction (No_Implementation_Identifiers, Post_Node);
17226 end if;
17227 end if;
17229 -- Do the style check
17231 if Style_Check
17232 and then not Suppress_Style_Checks (Val)
17233 and then not In_Instance
17234 then
17235 if Nkind (N) = N_Identifier then
17236 Nod := N;
17237 elsif Nkind (N) = N_Expanded_Name then
17238 Nod := Selector_Name (N);
17239 else
17240 return;
17241 end if;
17243 -- A special situation arises for derived operations, where we want
17244 -- to do the check against the parent (since the Sloc of the derived
17245 -- operation points to the derived type declaration itself).
17247 Val_Actual := Val;
17248 while not Comes_From_Source (Val_Actual)
17249 and then Nkind (Val_Actual) in N_Entity
17250 and then (Ekind (Val_Actual) = E_Enumeration_Literal
17251 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
17252 and then Present (Alias (Val_Actual))
17253 loop
17254 Val_Actual := Alias (Val_Actual);
17255 end loop;
17257 -- Renaming declarations for generic actuals do not come from source,
17258 -- and have a different name from that of the entity they rename, so
17259 -- there is no style check to perform here.
17261 if Chars (Nod) = Chars (Val_Actual) then
17262 Style.Check_Identifier (Nod, Val_Actual);
17263 end if;
17264 end if;
17266 Set_Entity (N, Val);
17267 end Set_Entity_With_Checks;
17269 -------------------------
17270 -- Set_Is_Ghost_Entity --
17271 -------------------------
17273 procedure Set_Is_Ghost_Entity (Id : Entity_Id) is
17274 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
17276 begin
17277 if Policy = Name_Check then
17278 Set_Is_Checked_Ghost_Entity (Id);
17280 elsif Policy = Name_Ignore then
17281 Set_Is_Ignored_Ghost_Entity (Id);
17282 end if;
17283 end Set_Is_Ghost_Entity;
17285 ------------------------
17286 -- Set_Name_Entity_Id --
17287 ------------------------
17289 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
17290 begin
17291 Set_Name_Table_Int (Id, Int (Val));
17292 end Set_Name_Entity_Id;
17294 ---------------------
17295 -- Set_Next_Actual --
17296 ---------------------
17298 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
17299 begin
17300 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
17301 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
17302 end if;
17303 end Set_Next_Actual;
17305 ----------------------------------
17306 -- Set_Optimize_Alignment_Flags --
17307 ----------------------------------
17309 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
17310 begin
17311 if Optimize_Alignment = 'S' then
17312 Set_Optimize_Alignment_Space (E);
17313 elsif Optimize_Alignment = 'T' then
17314 Set_Optimize_Alignment_Time (E);
17315 end if;
17316 end Set_Optimize_Alignment_Flags;
17318 -----------------------
17319 -- Set_Public_Status --
17320 -----------------------
17322 procedure Set_Public_Status (Id : Entity_Id) is
17323 S : constant Entity_Id := Current_Scope;
17325 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
17326 -- Determines if E is defined within handled statement sequence or
17327 -- an if statement, returns True if so, False otherwise.
17329 ----------------------
17330 -- Within_HSS_Or_If --
17331 ----------------------
17333 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
17334 N : Node_Id;
17335 begin
17336 N := Declaration_Node (E);
17337 loop
17338 N := Parent (N);
17340 if No (N) then
17341 return False;
17343 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
17344 N_If_Statement)
17345 then
17346 return True;
17347 end if;
17348 end loop;
17349 end Within_HSS_Or_If;
17351 -- Start of processing for Set_Public_Status
17353 begin
17354 -- Everything in the scope of Standard is public
17356 if S = Standard_Standard then
17357 Set_Is_Public (Id);
17359 -- Entity is definitely not public if enclosing scope is not public
17361 elsif not Is_Public (S) then
17362 return;
17364 -- An object or function declaration that occurs in a handled sequence
17365 -- of statements or within an if statement is the declaration for a
17366 -- temporary object or local subprogram generated by the expander. It
17367 -- never needs to be made public and furthermore, making it public can
17368 -- cause back end problems.
17370 elsif Nkind_In (Parent (Id), N_Object_Declaration,
17371 N_Function_Specification)
17372 and then Within_HSS_Or_If (Id)
17373 then
17374 return;
17376 -- Entities in public packages or records are public
17378 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
17379 Set_Is_Public (Id);
17381 -- The bounds of an entry family declaration can generate object
17382 -- declarations that are visible to the back-end, e.g. in the
17383 -- the declaration of a composite type that contains tasks.
17385 elsif Is_Concurrent_Type (S)
17386 and then not Has_Completion (S)
17387 and then Nkind (Parent (Id)) = N_Object_Declaration
17388 then
17389 Set_Is_Public (Id);
17390 end if;
17391 end Set_Public_Status;
17393 -----------------------------
17394 -- Set_Referenced_Modified --
17395 -----------------------------
17397 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
17398 Pref : Node_Id;
17400 begin
17401 -- Deal with indexed or selected component where prefix is modified
17403 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
17404 Pref := Prefix (N);
17406 -- If prefix is access type, then it is the designated object that is
17407 -- being modified, which means we have no entity to set the flag on.
17409 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
17410 return;
17412 -- Otherwise chase the prefix
17414 else
17415 Set_Referenced_Modified (Pref, Out_Param);
17416 end if;
17418 -- Otherwise see if we have an entity name (only other case to process)
17420 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
17421 Set_Referenced_As_LHS (Entity (N), not Out_Param);
17422 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
17423 end if;
17424 end Set_Referenced_Modified;
17426 ----------------------------
17427 -- Set_Scope_Is_Transient --
17428 ----------------------------
17430 procedure Set_Scope_Is_Transient (V : Boolean := True) is
17431 begin
17432 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
17433 end Set_Scope_Is_Transient;
17435 -------------------
17436 -- Set_Size_Info --
17437 -------------------
17439 procedure Set_Size_Info (T1, T2 : Entity_Id) is
17440 begin
17441 -- We copy Esize, but not RM_Size, since in general RM_Size is
17442 -- subtype specific and does not get inherited by all subtypes.
17444 Set_Esize (T1, Esize (T2));
17445 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
17447 if Is_Discrete_Or_Fixed_Point_Type (T1)
17448 and then
17449 Is_Discrete_Or_Fixed_Point_Type (T2)
17450 then
17451 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
17452 end if;
17454 Set_Alignment (T1, Alignment (T2));
17455 end Set_Size_Info;
17457 --------------------
17458 -- Static_Boolean --
17459 --------------------
17461 function Static_Boolean (N : Node_Id) return Uint is
17462 begin
17463 Analyze_And_Resolve (N, Standard_Boolean);
17465 if N = Error
17466 or else Error_Posted (N)
17467 or else Etype (N) = Any_Type
17468 then
17469 return No_Uint;
17470 end if;
17472 if Is_OK_Static_Expression (N) then
17473 if not Raises_Constraint_Error (N) then
17474 return Expr_Value (N);
17475 else
17476 return No_Uint;
17477 end if;
17479 elsif Etype (N) = Any_Type then
17480 return No_Uint;
17482 else
17483 Flag_Non_Static_Expr
17484 ("static boolean expression required here", N);
17485 return No_Uint;
17486 end if;
17487 end Static_Boolean;
17489 --------------------
17490 -- Static_Integer --
17491 --------------------
17493 function Static_Integer (N : Node_Id) return Uint is
17494 begin
17495 Analyze_And_Resolve (N, Any_Integer);
17497 if N = Error
17498 or else Error_Posted (N)
17499 or else Etype (N) = Any_Type
17500 then
17501 return No_Uint;
17502 end if;
17504 if Is_OK_Static_Expression (N) then
17505 if not Raises_Constraint_Error (N) then
17506 return Expr_Value (N);
17507 else
17508 return No_Uint;
17509 end if;
17511 elsif Etype (N) = Any_Type then
17512 return No_Uint;
17514 else
17515 Flag_Non_Static_Expr
17516 ("static integer expression required here", N);
17517 return No_Uint;
17518 end if;
17519 end Static_Integer;
17521 --------------------------
17522 -- Statically_Different --
17523 --------------------------
17525 function Statically_Different (E1, E2 : Node_Id) return Boolean is
17526 R1 : constant Node_Id := Get_Referenced_Object (E1);
17527 R2 : constant Node_Id := Get_Referenced_Object (E2);
17528 begin
17529 return Is_Entity_Name (R1)
17530 and then Is_Entity_Name (R2)
17531 and then Entity (R1) /= Entity (R2)
17532 and then not Is_Formal (Entity (R1))
17533 and then not Is_Formal (Entity (R2));
17534 end Statically_Different;
17536 --------------------------------------
17537 -- Subject_To_Loop_Entry_Attributes --
17538 --------------------------------------
17540 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
17541 Stmt : Node_Id;
17543 begin
17544 Stmt := N;
17546 -- The expansion mechanism transform a loop subject to at least one
17547 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
17548 -- the conditional part.
17550 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
17551 and then Nkind (Original_Node (N)) = N_Loop_Statement
17552 then
17553 Stmt := Original_Node (N);
17554 end if;
17556 return
17557 Nkind (Stmt) = N_Loop_Statement
17558 and then Present (Identifier (Stmt))
17559 and then Present (Entity (Identifier (Stmt)))
17560 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
17561 end Subject_To_Loop_Entry_Attributes;
17563 -----------------------------
17564 -- Subprogram_Access_Level --
17565 -----------------------------
17567 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
17568 begin
17569 if Present (Alias (Subp)) then
17570 return Subprogram_Access_Level (Alias (Subp));
17571 else
17572 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
17573 end if;
17574 end Subprogram_Access_Level;
17576 -------------------------------
17577 -- Support_Atomic_Primitives --
17578 -------------------------------
17580 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
17581 Size : Int;
17583 begin
17584 -- Verify the alignment of Typ is known
17586 if not Known_Alignment (Typ) then
17587 return False;
17588 end if;
17590 if Known_Static_Esize (Typ) then
17591 Size := UI_To_Int (Esize (Typ));
17593 -- If the Esize (Object_Size) is unknown at compile time, look at the
17594 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
17596 elsif Known_Static_RM_Size (Typ) then
17597 Size := UI_To_Int (RM_Size (Typ));
17599 -- Otherwise, the size is considered to be unknown.
17601 else
17602 return False;
17603 end if;
17605 -- Check that the size of the component is 8, 16, 32 or 64 bits and that
17606 -- Typ is properly aligned.
17608 case Size is
17609 when 8 | 16 | 32 | 64 =>
17610 return Size = UI_To_Int (Alignment (Typ)) * 8;
17611 when others =>
17612 return False;
17613 end case;
17614 end Support_Atomic_Primitives;
17616 -----------------
17617 -- Trace_Scope --
17618 -----------------
17620 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
17621 begin
17622 if Debug_Flag_W then
17623 for J in 0 .. Scope_Stack.Last loop
17624 Write_Str (" ");
17625 end loop;
17627 Write_Str (Msg);
17628 Write_Name (Chars (E));
17629 Write_Str (" from ");
17630 Write_Location (Sloc (N));
17631 Write_Eol;
17632 end if;
17633 end Trace_Scope;
17635 -----------------------
17636 -- Transfer_Entities --
17637 -----------------------
17639 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
17640 procedure Set_Public_Status_Of (Id : Entity_Id);
17641 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
17642 -- Set_Public_Status. If successfull and Id denotes a record type, set
17643 -- the Is_Public attribute of its fields.
17645 --------------------------
17646 -- Set_Public_Status_Of --
17647 --------------------------
17649 procedure Set_Public_Status_Of (Id : Entity_Id) is
17650 Field : Entity_Id;
17652 begin
17653 if not Is_Public (Id) then
17654 Set_Public_Status (Id);
17656 -- When the input entity is a public record type, ensure that all
17657 -- its internal fields are also exposed to the linker. The fields
17658 -- of a class-wide type are never made public.
17660 if Is_Public (Id)
17661 and then Is_Record_Type (Id)
17662 and then not Is_Class_Wide_Type (Id)
17663 then
17664 Field := First_Entity (Id);
17665 while Present (Field) loop
17666 Set_Is_Public (Field);
17667 Next_Entity (Field);
17668 end loop;
17669 end if;
17670 end if;
17671 end Set_Public_Status_Of;
17673 -- Local variables
17675 Full_Id : Entity_Id;
17676 Id : Entity_Id;
17678 -- Start of processing for Transfer_Entities
17680 begin
17681 Id := First_Entity (From);
17683 if Present (Id) then
17685 -- Merge the entity chain of the source scope with that of the
17686 -- destination scope.
17688 if Present (Last_Entity (To)) then
17689 Set_Next_Entity (Last_Entity (To), Id);
17690 else
17691 Set_First_Entity (To, Id);
17692 end if;
17694 Set_Last_Entity (To, Last_Entity (From));
17696 -- Inspect the entities of the source scope and update their Scope
17697 -- attribute.
17699 while Present (Id) loop
17700 Set_Scope (Id, To);
17701 Set_Public_Status_Of (Id);
17703 -- Handle an internally generated full view for a private type
17705 if Is_Private_Type (Id)
17706 and then Present (Full_View (Id))
17707 and then Is_Itype (Full_View (Id))
17708 then
17709 Full_Id := Full_View (Id);
17711 Set_Scope (Full_Id, To);
17712 Set_Public_Status_Of (Full_Id);
17713 end if;
17715 Next_Entity (Id);
17716 end loop;
17718 Set_First_Entity (From, Empty);
17719 Set_Last_Entity (From, Empty);
17720 end if;
17721 end Transfer_Entities;
17723 -----------------------
17724 -- Type_Access_Level --
17725 -----------------------
17727 function Type_Access_Level (Typ : Entity_Id) return Uint is
17728 Btyp : Entity_Id;
17730 begin
17731 Btyp := Base_Type (Typ);
17733 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
17734 -- simply use the level where the type is declared. This is true for
17735 -- stand-alone object declarations, and for anonymous access types
17736 -- associated with components the level is the same as that of the
17737 -- enclosing composite type. However, special treatment is needed for
17738 -- the cases of access parameters, return objects of an anonymous access
17739 -- type, and, in Ada 95, access discriminants of limited types.
17741 if Is_Access_Type (Btyp) then
17742 if Ekind (Btyp) = E_Anonymous_Access_Type then
17744 -- If the type is a nonlocal anonymous access type (such as for
17745 -- an access parameter) we treat it as being declared at the
17746 -- library level to ensure that names such as X.all'access don't
17747 -- fail static accessibility checks.
17749 if not Is_Local_Anonymous_Access (Typ) then
17750 return Scope_Depth (Standard_Standard);
17752 -- If this is a return object, the accessibility level is that of
17753 -- the result subtype of the enclosing function. The test here is
17754 -- little complicated, because we have to account for extended
17755 -- return statements that have been rewritten as blocks, in which
17756 -- case we have to find and the Is_Return_Object attribute of the
17757 -- itype's associated object. It would be nice to find a way to
17758 -- simplify this test, but it doesn't seem worthwhile to add a new
17759 -- flag just for purposes of this test. ???
17761 elsif Ekind (Scope (Btyp)) = E_Return_Statement
17762 or else
17763 (Is_Itype (Btyp)
17764 and then Nkind (Associated_Node_For_Itype (Btyp)) =
17765 N_Object_Declaration
17766 and then Is_Return_Object
17767 (Defining_Identifier
17768 (Associated_Node_For_Itype (Btyp))))
17769 then
17770 declare
17771 Scop : Entity_Id;
17773 begin
17774 Scop := Scope (Scope (Btyp));
17775 while Present (Scop) loop
17776 exit when Ekind (Scop) = E_Function;
17777 Scop := Scope (Scop);
17778 end loop;
17780 -- Treat the return object's type as having the level of the
17781 -- function's result subtype (as per RM05-6.5(5.3/2)).
17783 return Type_Access_Level (Etype (Scop));
17784 end;
17785 end if;
17786 end if;
17788 Btyp := Root_Type (Btyp);
17790 -- The accessibility level of anonymous access types associated with
17791 -- discriminants is that of the current instance of the type, and
17792 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
17794 -- AI-402: access discriminants have accessibility based on the
17795 -- object rather than the type in Ada 2005, so the above paragraph
17796 -- doesn't apply.
17798 -- ??? Needs completion with rules from AI-416
17800 if Ada_Version <= Ada_95
17801 and then Ekind (Typ) = E_Anonymous_Access_Type
17802 and then Present (Associated_Node_For_Itype (Typ))
17803 and then Nkind (Associated_Node_For_Itype (Typ)) =
17804 N_Discriminant_Specification
17805 then
17806 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
17807 end if;
17808 end if;
17810 -- Return library level for a generic formal type. This is done because
17811 -- RM(10.3.2) says that "The statically deeper relationship does not
17812 -- apply to ... a descendant of a generic formal type". Rather than
17813 -- checking at each point where a static accessibility check is
17814 -- performed to see if we are dealing with a formal type, this rule is
17815 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
17816 -- return extreme values for a formal type; Deepest_Type_Access_Level
17817 -- returns Int'Last. By calling the appropriate function from among the
17818 -- two, we ensure that the static accessibility check will pass if we
17819 -- happen to run into a formal type. More specifically, we should call
17820 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
17821 -- call occurs as part of a static accessibility check and the error
17822 -- case is the case where the type's level is too shallow (as opposed
17823 -- to too deep).
17825 if Is_Generic_Type (Root_Type (Btyp)) then
17826 return Scope_Depth (Standard_Standard);
17827 end if;
17829 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
17830 end Type_Access_Level;
17832 ------------------------------------
17833 -- Type_Without_Stream_Operation --
17834 ------------------------------------
17836 function Type_Without_Stream_Operation
17837 (T : Entity_Id;
17838 Op : TSS_Name_Type := TSS_Null) return Entity_Id
17840 BT : constant Entity_Id := Base_Type (T);
17841 Op_Missing : Boolean;
17843 begin
17844 if not Restriction_Active (No_Default_Stream_Attributes) then
17845 return Empty;
17846 end if;
17848 if Is_Elementary_Type (T) then
17849 if Op = TSS_Null then
17850 Op_Missing :=
17851 No (TSS (BT, TSS_Stream_Read))
17852 or else No (TSS (BT, TSS_Stream_Write));
17854 else
17855 Op_Missing := No (TSS (BT, Op));
17856 end if;
17858 if Op_Missing then
17859 return T;
17860 else
17861 return Empty;
17862 end if;
17864 elsif Is_Array_Type (T) then
17865 return Type_Without_Stream_Operation (Component_Type (T), Op);
17867 elsif Is_Record_Type (T) then
17868 declare
17869 Comp : Entity_Id;
17870 C_Typ : Entity_Id;
17872 begin
17873 Comp := First_Component (T);
17874 while Present (Comp) loop
17875 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
17877 if Present (C_Typ) then
17878 return C_Typ;
17879 end if;
17881 Next_Component (Comp);
17882 end loop;
17884 return Empty;
17885 end;
17887 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
17888 return Type_Without_Stream_Operation (Full_View (T), Op);
17889 else
17890 return Empty;
17891 end if;
17892 end Type_Without_Stream_Operation;
17894 ----------------------------
17895 -- Unique_Defining_Entity --
17896 ----------------------------
17898 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
17899 begin
17900 return Unique_Entity (Defining_Entity (N));
17901 end Unique_Defining_Entity;
17903 -------------------
17904 -- Unique_Entity --
17905 -------------------
17907 function Unique_Entity (E : Entity_Id) return Entity_Id is
17908 U : Entity_Id := E;
17909 P : Node_Id;
17911 begin
17912 case Ekind (E) is
17913 when E_Constant =>
17914 if Present (Full_View (E)) then
17915 U := Full_View (E);
17916 end if;
17918 when Type_Kind =>
17919 if Present (Full_View (E)) then
17920 U := Full_View (E);
17921 end if;
17923 when E_Package_Body =>
17924 P := Parent (E);
17926 if Nkind (P) = N_Defining_Program_Unit_Name then
17927 P := Parent (P);
17928 end if;
17930 U := Corresponding_Spec (P);
17932 when E_Subprogram_Body =>
17933 P := Parent (E);
17935 if Nkind (P) = N_Defining_Program_Unit_Name then
17936 P := Parent (P);
17937 end if;
17939 P := Parent (P);
17941 if Nkind (P) = N_Subprogram_Body_Stub then
17942 if Present (Library_Unit (P)) then
17944 -- Get to the function or procedure (generic) entity through
17945 -- the body entity.
17947 U :=
17948 Unique_Entity (Defining_Entity (Get_Body_From_Stub (P)));
17949 end if;
17950 else
17951 U := Corresponding_Spec (P);
17952 end if;
17954 when Formal_Kind =>
17955 if Present (Spec_Entity (E)) then
17956 U := Spec_Entity (E);
17957 end if;
17959 when others =>
17960 null;
17961 end case;
17963 return U;
17964 end Unique_Entity;
17966 -----------------
17967 -- Unique_Name --
17968 -----------------
17970 function Unique_Name (E : Entity_Id) return String is
17972 -- Names of E_Subprogram_Body or E_Package_Body entities are not
17973 -- reliable, as they may not include the overloading suffix. Instead,
17974 -- when looking for the name of E or one of its enclosing scope, we get
17975 -- the name of the corresponding Unique_Entity.
17977 function Get_Scoped_Name (E : Entity_Id) return String;
17978 -- Return the name of E prefixed by all the names of the scopes to which
17979 -- E belongs, except for Standard.
17981 ---------------------
17982 -- Get_Scoped_Name --
17983 ---------------------
17985 function Get_Scoped_Name (E : Entity_Id) return String is
17986 Name : constant String := Get_Name_String (Chars (E));
17987 begin
17988 if Has_Fully_Qualified_Name (E)
17989 or else Scope (E) = Standard_Standard
17990 then
17991 return Name;
17992 else
17993 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
17994 end if;
17995 end Get_Scoped_Name;
17997 -- Start of processing for Unique_Name
17999 begin
18000 if E = Standard_Standard then
18001 return Get_Name_String (Name_Standard);
18003 elsif Scope (E) = Standard_Standard
18004 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
18005 then
18006 return Get_Name_String (Name_Standard) & "__" &
18007 Get_Name_String (Chars (E));
18009 elsif Ekind (E) = E_Enumeration_Literal then
18010 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
18012 else
18013 return Get_Scoped_Name (Unique_Entity (E));
18014 end if;
18015 end Unique_Name;
18017 ---------------------
18018 -- Unit_Is_Visible --
18019 ---------------------
18021 function Unit_Is_Visible (U : Entity_Id) return Boolean is
18022 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
18023 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
18025 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
18026 -- For a child unit, check whether unit appears in a with_clause
18027 -- of a parent.
18029 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
18030 -- Scan the context clause of one compilation unit looking for a
18031 -- with_clause for the unit in question.
18033 ----------------------------
18034 -- Unit_In_Parent_Context --
18035 ----------------------------
18037 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
18038 begin
18039 if Unit_In_Context (Par_Unit) then
18040 return True;
18042 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
18043 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
18045 else
18046 return False;
18047 end if;
18048 end Unit_In_Parent_Context;
18050 ---------------------
18051 -- Unit_In_Context --
18052 ---------------------
18054 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
18055 Clause : Node_Id;
18057 begin
18058 Clause := First (Context_Items (Comp_Unit));
18059 while Present (Clause) loop
18060 if Nkind (Clause) = N_With_Clause then
18061 if Library_Unit (Clause) = U then
18062 return True;
18064 -- The with_clause may denote a renaming of the unit we are
18065 -- looking for, eg. Text_IO which renames Ada.Text_IO.
18067 elsif
18068 Renamed_Entity (Entity (Name (Clause))) =
18069 Defining_Entity (Unit (U))
18070 then
18071 return True;
18072 end if;
18073 end if;
18075 Next (Clause);
18076 end loop;
18078 return False;
18079 end Unit_In_Context;
18081 -- Start of processing for Unit_Is_Visible
18083 begin
18084 -- The currrent unit is directly visible
18086 if Curr = U then
18087 return True;
18089 elsif Unit_In_Context (Curr) then
18090 return True;
18092 -- If the current unit is a body, check the context of the spec
18094 elsif Nkind (Unit (Curr)) = N_Package_Body
18095 or else
18096 (Nkind (Unit (Curr)) = N_Subprogram_Body
18097 and then not Acts_As_Spec (Unit (Curr)))
18098 then
18099 if Unit_In_Context (Library_Unit (Curr)) then
18100 return True;
18101 end if;
18102 end if;
18104 -- If the spec is a child unit, examine the parents
18106 if Is_Child_Unit (Curr_Entity) then
18107 if Nkind (Unit (Curr)) in N_Unit_Body then
18108 return
18109 Unit_In_Parent_Context
18110 (Parent_Spec (Unit (Library_Unit (Curr))));
18111 else
18112 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
18113 end if;
18115 else
18116 return False;
18117 end if;
18118 end Unit_Is_Visible;
18120 ------------------------------
18121 -- Universal_Interpretation --
18122 ------------------------------
18124 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
18125 Index : Interp_Index;
18126 It : Interp;
18128 begin
18129 -- The argument may be a formal parameter of an operator or subprogram
18130 -- with multiple interpretations, or else an expression for an actual.
18132 if Nkind (Opnd) = N_Defining_Identifier
18133 or else not Is_Overloaded (Opnd)
18134 then
18135 if Etype (Opnd) = Universal_Integer
18136 or else Etype (Opnd) = Universal_Real
18137 then
18138 return Etype (Opnd);
18139 else
18140 return Empty;
18141 end if;
18143 else
18144 Get_First_Interp (Opnd, Index, It);
18145 while Present (It.Typ) loop
18146 if It.Typ = Universal_Integer
18147 or else It.Typ = Universal_Real
18148 then
18149 return It.Typ;
18150 end if;
18152 Get_Next_Interp (Index, It);
18153 end loop;
18155 return Empty;
18156 end if;
18157 end Universal_Interpretation;
18159 ---------------
18160 -- Unqualify --
18161 ---------------
18163 function Unqualify (Expr : Node_Id) return Node_Id is
18164 begin
18165 -- Recurse to handle unlikely case of multiple levels of qualification
18167 if Nkind (Expr) = N_Qualified_Expression then
18168 return Unqualify (Expression (Expr));
18170 -- Normal case, not a qualified expression
18172 else
18173 return Expr;
18174 end if;
18175 end Unqualify;
18177 -----------------------
18178 -- Visible_Ancestors --
18179 -----------------------
18181 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
18182 List_1 : Elist_Id;
18183 List_2 : Elist_Id;
18184 Elmt : Elmt_Id;
18186 begin
18187 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
18189 -- Collect all the parents and progenitors of Typ. If the full-view of
18190 -- private parents and progenitors is available then it is used to
18191 -- generate the list of visible ancestors; otherwise their partial
18192 -- view is added to the resulting list.
18194 Collect_Parents
18195 (T => Typ,
18196 List => List_1,
18197 Use_Full_View => True);
18199 Collect_Interfaces
18200 (T => Typ,
18201 Ifaces_List => List_2,
18202 Exclude_Parents => True,
18203 Use_Full_View => True);
18205 -- Join the two lists. Avoid duplications because an interface may
18206 -- simultaneously be parent and progenitor of a type.
18208 Elmt := First_Elmt (List_2);
18209 while Present (Elmt) loop
18210 Append_Unique_Elmt (Node (Elmt), List_1);
18211 Next_Elmt (Elmt);
18212 end loop;
18214 return List_1;
18215 end Visible_Ancestors;
18217 ------------------------
18218 -- Within_Ghost_Scope --
18219 ------------------------
18221 function Within_Ghost_Scope
18222 (Id : Entity_Id := Current_Scope) return Boolean
18224 S : Entity_Id;
18226 begin
18227 -- Climb the scope stack looking for a Ghost scope
18229 S := Id;
18230 while Present (S) and then S /= Standard_Standard loop
18231 if Is_Ghost_Entity (S) then
18232 return True;
18233 end if;
18235 S := Scope (S);
18236 end loop;
18238 return False;
18239 end Within_Ghost_Scope;
18241 ----------------------
18242 -- Within_Init_Proc --
18243 ----------------------
18245 function Within_Init_Proc return Boolean is
18246 S : Entity_Id;
18248 begin
18249 S := Current_Scope;
18250 while not Is_Overloadable (S) loop
18251 if S = Standard_Standard then
18252 return False;
18253 else
18254 S := Scope (S);
18255 end if;
18256 end loop;
18258 return Is_Init_Proc (S);
18259 end Within_Init_Proc;
18261 ------------------
18262 -- Within_Scope --
18263 ------------------
18265 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
18266 SE : Entity_Id;
18267 begin
18268 SE := Scope (E);
18269 loop
18270 if SE = S then
18271 return True;
18272 elsif SE = Standard_Standard then
18273 return False;
18274 else
18275 SE := Scope (SE);
18276 end if;
18277 end loop;
18278 end Within_Scope;
18280 ----------------
18281 -- Wrong_Type --
18282 ----------------
18284 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
18285 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
18286 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
18288 Matching_Field : Entity_Id;
18289 -- Entity to give a more precise suggestion on how to write a one-
18290 -- element positional aggregate.
18292 function Has_One_Matching_Field return Boolean;
18293 -- Determines if Expec_Type is a record type with a single component or
18294 -- discriminant whose type matches the found type or is one dimensional
18295 -- array whose component type matches the found type. In the case of
18296 -- one discriminant, we ignore the variant parts. That's not accurate,
18297 -- but good enough for the warning.
18299 ----------------------------
18300 -- Has_One_Matching_Field --
18301 ----------------------------
18303 function Has_One_Matching_Field return Boolean is
18304 E : Entity_Id;
18306 begin
18307 Matching_Field := Empty;
18309 if Is_Array_Type (Expec_Type)
18310 and then Number_Dimensions (Expec_Type) = 1
18311 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
18312 then
18313 -- Use type name if available. This excludes multidimensional
18314 -- arrays and anonymous arrays.
18316 if Comes_From_Source (Expec_Type) then
18317 Matching_Field := Expec_Type;
18319 -- For an assignment, use name of target
18321 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
18322 and then Is_Entity_Name (Name (Parent (Expr)))
18323 then
18324 Matching_Field := Entity (Name (Parent (Expr)));
18325 end if;
18327 return True;
18329 elsif not Is_Record_Type (Expec_Type) then
18330 return False;
18332 else
18333 E := First_Entity (Expec_Type);
18334 loop
18335 if No (E) then
18336 return False;
18338 elsif not Ekind_In (E, E_Discriminant, E_Component)
18339 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
18340 then
18341 Next_Entity (E);
18343 else
18344 exit;
18345 end if;
18346 end loop;
18348 if not Covers (Etype (E), Found_Type) then
18349 return False;
18351 elsif Present (Next_Entity (E))
18352 and then (Ekind (E) = E_Component
18353 or else Ekind (Next_Entity (E)) = E_Discriminant)
18354 then
18355 return False;
18357 else
18358 Matching_Field := E;
18359 return True;
18360 end if;
18361 end if;
18362 end Has_One_Matching_Field;
18364 -- Start of processing for Wrong_Type
18366 begin
18367 -- Don't output message if either type is Any_Type, or if a message
18368 -- has already been posted for this node. We need to do the latter
18369 -- check explicitly (it is ordinarily done in Errout), because we
18370 -- are using ! to force the output of the error messages.
18372 if Expec_Type = Any_Type
18373 or else Found_Type = Any_Type
18374 or else Error_Posted (Expr)
18375 then
18376 return;
18378 -- If one of the types is a Taft-Amendment type and the other it its
18379 -- completion, it must be an illegal use of a TAT in the spec, for
18380 -- which an error was already emitted. Avoid cascaded errors.
18382 elsif Is_Incomplete_Type (Expec_Type)
18383 and then Has_Completion_In_Body (Expec_Type)
18384 and then Full_View (Expec_Type) = Etype (Expr)
18385 then
18386 return;
18388 elsif Is_Incomplete_Type (Etype (Expr))
18389 and then Has_Completion_In_Body (Etype (Expr))
18390 and then Full_View (Etype (Expr)) = Expec_Type
18391 then
18392 return;
18394 -- In an instance, there is an ongoing problem with completion of
18395 -- type derived from private types. Their structure is what Gigi
18396 -- expects, but the Etype is the parent type rather than the
18397 -- derived private type itself. Do not flag error in this case. The
18398 -- private completion is an entity without a parent, like an Itype.
18399 -- Similarly, full and partial views may be incorrect in the instance.
18400 -- There is no simple way to insure that it is consistent ???
18402 -- A similar view discrepancy can happen in an inlined body, for the
18403 -- same reason: inserted body may be outside of the original package
18404 -- and only partial views are visible at the point of insertion.
18406 elsif In_Instance or else In_Inlined_Body then
18407 if Etype (Etype (Expr)) = Etype (Expected_Type)
18408 and then
18409 (Has_Private_Declaration (Expected_Type)
18410 or else Has_Private_Declaration (Etype (Expr)))
18411 and then No (Parent (Expected_Type))
18412 then
18413 return;
18415 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
18416 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
18417 then
18418 return;
18420 elsif Is_Private_Type (Expected_Type)
18421 and then Present (Full_View (Expected_Type))
18422 and then Covers (Full_View (Expected_Type), Etype (Expr))
18423 then
18424 return;
18425 end if;
18426 end if;
18428 -- An interesting special check. If the expression is parenthesized
18429 -- and its type corresponds to the type of the sole component of the
18430 -- expected record type, or to the component type of the expected one
18431 -- dimensional array type, then assume we have a bad aggregate attempt.
18433 if Nkind (Expr) in N_Subexpr
18434 and then Paren_Count (Expr) /= 0
18435 and then Has_One_Matching_Field
18436 then
18437 Error_Msg_N ("positional aggregate cannot have one component", Expr);
18438 if Present (Matching_Field) then
18439 if Is_Array_Type (Expec_Type) then
18440 Error_Msg_NE
18441 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
18443 else
18444 Error_Msg_NE
18445 ("\write instead `& ='> ...`", Expr, Matching_Field);
18446 end if;
18447 end if;
18449 -- Another special check, if we are looking for a pool-specific access
18450 -- type and we found an E_Access_Attribute_Type, then we have the case
18451 -- of an Access attribute being used in a context which needs a pool-
18452 -- specific type, which is never allowed. The one extra check we make
18453 -- is that the expected designated type covers the Found_Type.
18455 elsif Is_Access_Type (Expec_Type)
18456 and then Ekind (Found_Type) = E_Access_Attribute_Type
18457 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
18458 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
18459 and then Covers
18460 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
18461 then
18462 Error_Msg_N -- CODEFIX
18463 ("result must be general access type!", Expr);
18464 Error_Msg_NE -- CODEFIX
18465 ("add ALL to }!", Expr, Expec_Type);
18467 -- Another special check, if the expected type is an integer type,
18468 -- but the expression is of type System.Address, and the parent is
18469 -- an addition or subtraction operation whose left operand is the
18470 -- expression in question and whose right operand is of an integral
18471 -- type, then this is an attempt at address arithmetic, so give
18472 -- appropriate message.
18474 elsif Is_Integer_Type (Expec_Type)
18475 and then Is_RTE (Found_Type, RE_Address)
18476 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
18477 and then Expr = Left_Opnd (Parent (Expr))
18478 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
18479 then
18480 Error_Msg_N
18481 ("address arithmetic not predefined in package System",
18482 Parent (Expr));
18483 Error_Msg_N
18484 ("\possible missing with/use of System.Storage_Elements",
18485 Parent (Expr));
18486 return;
18488 -- If the expected type is an anonymous access type, as for access
18489 -- parameters and discriminants, the error is on the designated types.
18491 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
18492 if Comes_From_Source (Expec_Type) then
18493 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18494 else
18495 Error_Msg_NE
18496 ("expected an access type with designated}",
18497 Expr, Designated_Type (Expec_Type));
18498 end if;
18500 if Is_Access_Type (Found_Type)
18501 and then not Comes_From_Source (Found_Type)
18502 then
18503 Error_Msg_NE
18504 ("\\found an access type with designated}!",
18505 Expr, Designated_Type (Found_Type));
18506 else
18507 if From_Limited_With (Found_Type) then
18508 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
18509 Error_Msg_Qual_Level := 99;
18510 Error_Msg_NE -- CODEFIX
18511 ("\\missing `WITH &;", Expr, Scope (Found_Type));
18512 Error_Msg_Qual_Level := 0;
18513 else
18514 Error_Msg_NE ("found}!", Expr, Found_Type);
18515 end if;
18516 end if;
18518 -- Normal case of one type found, some other type expected
18520 else
18521 -- If the names of the two types are the same, see if some number
18522 -- of levels of qualification will help. Don't try more than three
18523 -- levels, and if we get to standard, it's no use (and probably
18524 -- represents an error in the compiler) Also do not bother with
18525 -- internal scope names.
18527 declare
18528 Expec_Scope : Entity_Id;
18529 Found_Scope : Entity_Id;
18531 begin
18532 Expec_Scope := Expec_Type;
18533 Found_Scope := Found_Type;
18535 for Levels in Int range 0 .. 3 loop
18536 if Chars (Expec_Scope) /= Chars (Found_Scope) then
18537 Error_Msg_Qual_Level := Levels;
18538 exit;
18539 end if;
18541 Expec_Scope := Scope (Expec_Scope);
18542 Found_Scope := Scope (Found_Scope);
18544 exit when Expec_Scope = Standard_Standard
18545 or else Found_Scope = Standard_Standard
18546 or else not Comes_From_Source (Expec_Scope)
18547 or else not Comes_From_Source (Found_Scope);
18548 end loop;
18549 end;
18551 if Is_Record_Type (Expec_Type)
18552 and then Present (Corresponding_Remote_Type (Expec_Type))
18553 then
18554 Error_Msg_NE ("expected}!", Expr,
18555 Corresponding_Remote_Type (Expec_Type));
18556 else
18557 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18558 end if;
18560 if Is_Entity_Name (Expr)
18561 and then Is_Package_Or_Generic_Package (Entity (Expr))
18562 then
18563 Error_Msg_N ("\\found package name!", Expr);
18565 elsif Is_Entity_Name (Expr)
18566 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
18567 then
18568 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
18569 Error_Msg_N
18570 ("found procedure name, possibly missing Access attribute!",
18571 Expr);
18572 else
18573 Error_Msg_N
18574 ("\\found procedure name instead of function!", Expr);
18575 end if;
18577 elsif Nkind (Expr) = N_Function_Call
18578 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
18579 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
18580 and then No (Parameter_Associations (Expr))
18581 then
18582 Error_Msg_N
18583 ("found function name, possibly missing Access attribute!",
18584 Expr);
18586 -- Catch common error: a prefix or infix operator which is not
18587 -- directly visible because the type isn't.
18589 elsif Nkind (Expr) in N_Op
18590 and then Is_Overloaded (Expr)
18591 and then not Is_Immediately_Visible (Expec_Type)
18592 and then not Is_Potentially_Use_Visible (Expec_Type)
18593 and then not In_Use (Expec_Type)
18594 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
18595 then
18596 Error_Msg_N
18597 ("operator of the type is not directly visible!", Expr);
18599 elsif Ekind (Found_Type) = E_Void
18600 and then Present (Parent (Found_Type))
18601 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
18602 then
18603 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
18605 else
18606 Error_Msg_NE ("\\found}!", Expr, Found_Type);
18607 end if;
18609 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
18610 -- of the same modular type, and (M1 and M2) = 0 was intended.
18612 if Expec_Type = Standard_Boolean
18613 and then Is_Modular_Integer_Type (Found_Type)
18614 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
18615 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
18616 then
18617 declare
18618 Op : constant Node_Id := Right_Opnd (Parent (Expr));
18619 L : constant Node_Id := Left_Opnd (Op);
18620 R : constant Node_Id := Right_Opnd (Op);
18622 begin
18623 -- The case for the message is when the left operand of the
18624 -- comparison is the same modular type, or when it is an
18625 -- integer literal (or other universal integer expression),
18626 -- which would have been typed as the modular type if the
18627 -- parens had been there.
18629 if (Etype (L) = Found_Type
18630 or else
18631 Etype (L) = Universal_Integer)
18632 and then Is_Integer_Type (Etype (R))
18633 then
18634 Error_Msg_N
18635 ("\\possible missing parens for modular operation", Expr);
18636 end if;
18637 end;
18638 end if;
18640 -- Reset error message qualification indication
18642 Error_Msg_Qual_Level := 0;
18643 end if;
18644 end Wrong_Type;
18646 end Sem_Util;