2014-11-07 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / sem_util.adb
blobfc160e17d361ac3df0642b6f624f60aaf43d3d51
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 Comes_From_Source (N)
901 and then Warn_On_Unordered_Enumeration_Type
902 and then not Has_Pragma_Ordered (T)
903 and then not In_Same_Extended_Unit (N, T);
904 end Bad_Unordered_Enumeration_Reference;
906 --------------------------
907 -- Build_Actual_Subtype --
908 --------------------------
910 function Build_Actual_Subtype
911 (T : Entity_Id;
912 N : Node_Or_Entity_Id) return Node_Id
914 Loc : Source_Ptr;
915 -- Normally Sloc (N), but may point to corresponding body in some cases
917 Constraints : List_Id;
918 Decl : Node_Id;
919 Discr : Entity_Id;
920 Hi : Node_Id;
921 Lo : Node_Id;
922 Subt : Entity_Id;
923 Disc_Type : Entity_Id;
924 Obj : Node_Id;
926 begin
927 Loc := Sloc (N);
929 if Nkind (N) = N_Defining_Identifier then
930 Obj := New_Occurrence_Of (N, Loc);
932 -- If this is a formal parameter of a subprogram declaration, and
933 -- we are compiling the body, we want the declaration for the
934 -- actual subtype to carry the source position of the body, to
935 -- prevent anomalies in gdb when stepping through the code.
937 if Is_Formal (N) then
938 declare
939 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
940 begin
941 if Nkind (Decl) = N_Subprogram_Declaration
942 and then Present (Corresponding_Body (Decl))
943 then
944 Loc := Sloc (Corresponding_Body (Decl));
945 end if;
946 end;
947 end if;
949 else
950 Obj := N;
951 end if;
953 if Is_Array_Type (T) then
954 Constraints := New_List;
955 for J in 1 .. Number_Dimensions (T) loop
957 -- Build an array subtype declaration with the nominal subtype and
958 -- the bounds of the actual. Add the declaration in front of the
959 -- local declarations for the subprogram, for analysis before any
960 -- reference to the formal in the body.
962 Lo :=
963 Make_Attribute_Reference (Loc,
964 Prefix =>
965 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
966 Attribute_Name => Name_First,
967 Expressions => New_List (
968 Make_Integer_Literal (Loc, J)));
970 Hi :=
971 Make_Attribute_Reference (Loc,
972 Prefix =>
973 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
974 Attribute_Name => Name_Last,
975 Expressions => New_List (
976 Make_Integer_Literal (Loc, J)));
978 Append (Make_Range (Loc, Lo, Hi), Constraints);
979 end loop;
981 -- If the type has unknown discriminants there is no constrained
982 -- subtype to build. This is never called for a formal or for a
983 -- lhs, so returning the type is ok ???
985 elsif Has_Unknown_Discriminants (T) then
986 return T;
988 else
989 Constraints := New_List;
991 -- Type T is a generic derived type, inherit the discriminants from
992 -- the parent type.
994 if Is_Private_Type (T)
995 and then No (Full_View (T))
997 -- T was flagged as an error if it was declared as a formal
998 -- derived type with known discriminants. In this case there
999 -- is no need to look at the parent type since T already carries
1000 -- its own discriminants.
1002 and then not Error_Posted (T)
1003 then
1004 Disc_Type := Etype (Base_Type (T));
1005 else
1006 Disc_Type := T;
1007 end if;
1009 Discr := First_Discriminant (Disc_Type);
1010 while Present (Discr) loop
1011 Append_To (Constraints,
1012 Make_Selected_Component (Loc,
1013 Prefix =>
1014 Duplicate_Subexpr_No_Checks (Obj),
1015 Selector_Name => New_Occurrence_Of (Discr, Loc)));
1016 Next_Discriminant (Discr);
1017 end loop;
1018 end if;
1020 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
1021 Set_Is_Internal (Subt);
1023 Decl :=
1024 Make_Subtype_Declaration (Loc,
1025 Defining_Identifier => Subt,
1026 Subtype_Indication =>
1027 Make_Subtype_Indication (Loc,
1028 Subtype_Mark => New_Occurrence_Of (T, Loc),
1029 Constraint =>
1030 Make_Index_Or_Discriminant_Constraint (Loc,
1031 Constraints => Constraints)));
1033 Mark_Rewrite_Insertion (Decl);
1034 return Decl;
1035 end Build_Actual_Subtype;
1037 ---------------------------------------
1038 -- Build_Actual_Subtype_Of_Component --
1039 ---------------------------------------
1041 function Build_Actual_Subtype_Of_Component
1042 (T : Entity_Id;
1043 N : Node_Id) return Node_Id
1045 Loc : constant Source_Ptr := Sloc (N);
1046 P : constant Node_Id := Prefix (N);
1047 D : Elmt_Id;
1048 Id : Node_Id;
1049 Index_Typ : Entity_Id;
1051 Desig_Typ : Entity_Id;
1052 -- This is either a copy of T, or if T is an access type, then it is
1053 -- the directly designated type of this access type.
1055 function Build_Actual_Array_Constraint return List_Id;
1056 -- If one or more of the bounds of the component depends on
1057 -- discriminants, build actual constraint using the discriminants
1058 -- of the prefix.
1060 function Build_Actual_Record_Constraint return List_Id;
1061 -- Similar to previous one, for discriminated components constrained
1062 -- by the discriminant of the enclosing object.
1064 -----------------------------------
1065 -- Build_Actual_Array_Constraint --
1066 -----------------------------------
1068 function Build_Actual_Array_Constraint return List_Id is
1069 Constraints : constant List_Id := New_List;
1070 Indx : Node_Id;
1071 Hi : Node_Id;
1072 Lo : Node_Id;
1073 Old_Hi : Node_Id;
1074 Old_Lo : Node_Id;
1076 begin
1077 Indx := First_Index (Desig_Typ);
1078 while Present (Indx) loop
1079 Old_Lo := Type_Low_Bound (Etype (Indx));
1080 Old_Hi := Type_High_Bound (Etype (Indx));
1082 if Denotes_Discriminant (Old_Lo) then
1083 Lo :=
1084 Make_Selected_Component (Loc,
1085 Prefix => New_Copy_Tree (P),
1086 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1088 else
1089 Lo := New_Copy_Tree (Old_Lo);
1091 -- The new bound will be reanalyzed in the enclosing
1092 -- declaration. For literal bounds that come from a type
1093 -- declaration, the type of the context must be imposed, so
1094 -- insure that analysis will take place. For non-universal
1095 -- types this is not strictly necessary.
1097 Set_Analyzed (Lo, False);
1098 end if;
1100 if Denotes_Discriminant (Old_Hi) then
1101 Hi :=
1102 Make_Selected_Component (Loc,
1103 Prefix => New_Copy_Tree (P),
1104 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1106 else
1107 Hi := New_Copy_Tree (Old_Hi);
1108 Set_Analyzed (Hi, False);
1109 end if;
1111 Append (Make_Range (Loc, Lo, Hi), Constraints);
1112 Next_Index (Indx);
1113 end loop;
1115 return Constraints;
1116 end Build_Actual_Array_Constraint;
1118 ------------------------------------
1119 -- Build_Actual_Record_Constraint --
1120 ------------------------------------
1122 function Build_Actual_Record_Constraint return List_Id is
1123 Constraints : constant List_Id := New_List;
1124 D : Elmt_Id;
1125 D_Val : Node_Id;
1127 begin
1128 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1129 while Present (D) loop
1130 if Denotes_Discriminant (Node (D)) then
1131 D_Val := Make_Selected_Component (Loc,
1132 Prefix => New_Copy_Tree (P),
1133 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1135 else
1136 D_Val := New_Copy_Tree (Node (D));
1137 end if;
1139 Append (D_Val, Constraints);
1140 Next_Elmt (D);
1141 end loop;
1143 return Constraints;
1144 end Build_Actual_Record_Constraint;
1146 -- Start of processing for Build_Actual_Subtype_Of_Component
1148 begin
1149 -- Why the test for Spec_Expression mode here???
1151 if In_Spec_Expression then
1152 return Empty;
1154 -- More comments for the rest of this body would be good ???
1156 elsif Nkind (N) = N_Explicit_Dereference then
1157 if Is_Composite_Type (T)
1158 and then not Is_Constrained (T)
1159 and then not (Is_Class_Wide_Type (T)
1160 and then Is_Constrained (Root_Type (T)))
1161 and then not Has_Unknown_Discriminants (T)
1162 then
1163 -- If the type of the dereference is already constrained, it is an
1164 -- actual subtype.
1166 if Is_Array_Type (Etype (N))
1167 and then Is_Constrained (Etype (N))
1168 then
1169 return Empty;
1170 else
1171 Remove_Side_Effects (P);
1172 return Build_Actual_Subtype (T, N);
1173 end if;
1174 else
1175 return Empty;
1176 end if;
1177 end if;
1179 if Ekind (T) = E_Access_Subtype then
1180 Desig_Typ := Designated_Type (T);
1181 else
1182 Desig_Typ := T;
1183 end if;
1185 if Ekind (Desig_Typ) = E_Array_Subtype then
1186 Id := First_Index (Desig_Typ);
1187 while Present (Id) loop
1188 Index_Typ := Underlying_Type (Etype (Id));
1190 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1191 or else
1192 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1193 then
1194 Remove_Side_Effects (P);
1195 return
1196 Build_Component_Subtype
1197 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1198 end if;
1200 Next_Index (Id);
1201 end loop;
1203 elsif Is_Composite_Type (Desig_Typ)
1204 and then Has_Discriminants (Desig_Typ)
1205 and then not Has_Unknown_Discriminants (Desig_Typ)
1206 then
1207 if Is_Private_Type (Desig_Typ)
1208 and then No (Discriminant_Constraint (Desig_Typ))
1209 then
1210 Desig_Typ := Full_View (Desig_Typ);
1211 end if;
1213 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1214 while Present (D) loop
1215 if Denotes_Discriminant (Node (D)) then
1216 Remove_Side_Effects (P);
1217 return
1218 Build_Component_Subtype (
1219 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1220 end if;
1222 Next_Elmt (D);
1223 end loop;
1224 end if;
1226 -- If none of the above, the actual and nominal subtypes are the same
1228 return Empty;
1229 end Build_Actual_Subtype_Of_Component;
1231 -----------------------------
1232 -- Build_Component_Subtype --
1233 -----------------------------
1235 function Build_Component_Subtype
1236 (C : List_Id;
1237 Loc : Source_Ptr;
1238 T : Entity_Id) return Node_Id
1240 Subt : Entity_Id;
1241 Decl : Node_Id;
1243 begin
1244 -- Unchecked_Union components do not require component subtypes
1246 if Is_Unchecked_Union (T) then
1247 return Empty;
1248 end if;
1250 Subt := Make_Temporary (Loc, 'S');
1251 Set_Is_Internal (Subt);
1253 Decl :=
1254 Make_Subtype_Declaration (Loc,
1255 Defining_Identifier => Subt,
1256 Subtype_Indication =>
1257 Make_Subtype_Indication (Loc,
1258 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1259 Constraint =>
1260 Make_Index_Or_Discriminant_Constraint (Loc,
1261 Constraints => C)));
1263 Mark_Rewrite_Insertion (Decl);
1264 return Decl;
1265 end Build_Component_Subtype;
1267 ----------------------------------
1268 -- Build_Default_Init_Cond_Call --
1269 ----------------------------------
1271 function Build_Default_Init_Cond_Call
1272 (Loc : Source_Ptr;
1273 Obj_Id : Entity_Id;
1274 Typ : Entity_Id) return Node_Id
1276 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1277 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1279 begin
1280 return
1281 Make_Procedure_Call_Statement (Loc,
1282 Name => New_Occurrence_Of (Proc_Id, Loc),
1283 Parameter_Associations => New_List (
1284 Make_Unchecked_Type_Conversion (Loc,
1285 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1286 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1287 end Build_Default_Init_Cond_Call;
1289 ----------------------------------------------
1290 -- Build_Default_Init_Cond_Procedure_Bodies --
1291 ----------------------------------------------
1293 procedure Build_Default_Init_Cond_Procedure_Bodies (Priv_Decls : List_Id) is
1294 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id);
1295 -- If type Typ is subject to pragma Default_Initial_Condition, build the
1296 -- body of the procedure which verifies the assumption of the pragma at
1297 -- run time. The generated body is added after the type declaration.
1299 --------------------------------------------
1300 -- Build_Default_Init_Cond_Procedure_Body --
1301 --------------------------------------------
1303 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id) is
1304 Param_Id : Entity_Id;
1305 -- The entity of the sole formal parameter of the default initial
1306 -- condition procedure.
1308 procedure Replace_Type_Reference (N : Node_Id);
1309 -- Replace a single reference to type Typ with a reference to formal
1310 -- parameter Param_Id.
1312 ----------------------------
1313 -- Replace_Type_Reference --
1314 ----------------------------
1316 procedure Replace_Type_Reference (N : Node_Id) is
1317 begin
1318 Rewrite (N, New_Occurrence_Of (Param_Id, Sloc (N)));
1319 end Replace_Type_Reference;
1321 procedure Replace_Type_References is
1322 new Replace_Type_References_Generic (Replace_Type_Reference);
1324 -- Local variables
1326 Loc : constant Source_Ptr := Sloc (Typ);
1327 Prag : constant Node_Id :=
1328 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1329 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1330 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Proc_Id);
1331 Body_Decl : Node_Id;
1332 Expr : Node_Id;
1333 Stmt : Node_Id;
1335 -- Start of processing for Build_Default_Init_Cond_Procedure_Body
1337 begin
1338 -- The procedure should be generated only for [sub]types subject to
1339 -- pragma Default_Initial_Condition. Types that inherit the pragma do
1340 -- not get this specialized procedure.
1342 pragma Assert (Has_Default_Init_Cond (Typ));
1343 pragma Assert (Present (Prag));
1344 pragma Assert (Present (Proc_Id));
1346 -- Nothing to do if the body was already built
1348 if Present (Corresponding_Body (Spec_Decl)) then
1349 return;
1350 end if;
1352 Param_Id := First_Formal (Proc_Id);
1354 -- The pragma has an argument. Note that the argument is analyzed
1355 -- after all references to the current instance of the type are
1356 -- replaced.
1358 if Present (Pragma_Argument_Associations (Prag)) then
1359 Expr :=
1360 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
1362 if Nkind (Expr) = N_Null then
1363 Stmt := Make_Null_Statement (Loc);
1365 -- Preserve the original argument of the pragma by replicating it.
1366 -- Replace all references to the current instance of the type with
1367 -- references to the formal parameter.
1369 else
1370 Expr := New_Copy_Tree (Expr);
1371 Replace_Type_References (Expr, Typ);
1373 -- Generate:
1374 -- pragma Check (Default_Initial_Condition, <Expr>);
1376 Stmt :=
1377 Make_Pragma (Loc,
1378 Pragma_Identifier =>
1379 Make_Identifier (Loc, Name_Check),
1381 Pragma_Argument_Associations => New_List (
1382 Make_Pragma_Argument_Association (Loc,
1383 Expression =>
1384 Make_Identifier (Loc,
1385 Chars => Name_Default_Initial_Condition)),
1386 Make_Pragma_Argument_Association (Loc,
1387 Expression => Expr)));
1388 end if;
1390 -- Otherwise the pragma appears without an argument
1392 else
1393 Stmt := Make_Null_Statement (Loc);
1394 end if;
1396 -- Generate:
1397 -- procedure <Typ>Default_Init_Cond (I : <Typ>) is
1398 -- begin
1399 -- <Stmt>;
1400 -- end <Typ>Default_Init_Cond;
1402 Body_Decl :=
1403 Make_Subprogram_Body (Loc,
1404 Specification =>
1405 Copy_Separate_Tree (Specification (Spec_Decl)),
1406 Declarations => Empty_List,
1407 Handled_Statement_Sequence =>
1408 Make_Handled_Sequence_Of_Statements (Loc,
1409 Statements => New_List (Stmt)));
1411 -- Link the spec and body of the default initial condition procedure
1412 -- to prevent the generation of a duplicate body.
1414 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
1415 Set_Corresponding_Spec (Body_Decl, Proc_Id);
1417 Insert_After_And_Analyze (Declaration_Node (Typ), Body_Decl);
1418 end Build_Default_Init_Cond_Procedure_Body;
1420 -- Local variables
1422 Decl : Node_Id;
1423 Typ : Entity_Id;
1425 -- Start of processing for Build_Default_Init_Cond_Procedure_Bodies
1427 begin
1428 -- Inspect the private declarations looking for [sub]type declarations
1430 Decl := First (Priv_Decls);
1431 while Present (Decl) loop
1432 if Nkind_In (Decl, N_Full_Type_Declaration,
1433 N_Subtype_Declaration)
1434 then
1435 Typ := Defining_Entity (Decl);
1437 -- Guard against partially decorate types due to previous errors
1439 if Is_Type (Typ) then
1441 -- If the type is subject to pragma Default_Initial_Condition,
1442 -- generate the body of the internal procedure which verifies
1443 -- the assertion of the pragma at run time.
1445 if Has_Default_Init_Cond (Typ) then
1446 Build_Default_Init_Cond_Procedure_Body (Typ);
1448 -- A derived type inherits the default initial condition
1449 -- procedure from its parent type.
1451 elsif Has_Inherited_Default_Init_Cond (Typ) then
1452 Inherit_Default_Init_Cond_Procedure (Typ);
1453 end if;
1454 end if;
1455 end if;
1457 Next (Decl);
1458 end loop;
1459 end Build_Default_Init_Cond_Procedure_Bodies;
1461 ---------------------------------------------------
1462 -- Build_Default_Init_Cond_Procedure_Declaration --
1463 ---------------------------------------------------
1465 procedure Build_Default_Init_Cond_Procedure_Declaration (Typ : Entity_Id) is
1466 Loc : constant Source_Ptr := Sloc (Typ);
1467 Prag : constant Node_Id :=
1468 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1469 Proc_Id : Entity_Id;
1471 begin
1472 -- The procedure should be generated only for types subject to pragma
1473 -- Default_Initial_Condition. Types that inherit the pragma do not get
1474 -- this specialized procedure.
1476 pragma Assert (Has_Default_Init_Cond (Typ));
1477 pragma Assert (Present (Prag));
1479 -- Nothing to do if default initial condition procedure already built
1481 if Present (Default_Init_Cond_Procedure (Typ)) then
1482 return;
1483 end if;
1485 Proc_Id :=
1486 Make_Defining_Identifier (Loc,
1487 Chars => New_External_Name (Chars (Typ), "Default_Init_Cond"));
1489 -- Associate default initial condition procedure with the private type
1491 Set_Ekind (Proc_Id, E_Procedure);
1492 Set_Is_Default_Init_Cond_Procedure (Proc_Id);
1493 Set_Default_Init_Cond_Procedure (Typ, Proc_Id);
1495 -- Generate:
1496 -- procedure <Typ>Default_Init_Cond (Inn : <Typ>);
1498 Insert_After_And_Analyze (Prag,
1499 Make_Subprogram_Declaration (Loc,
1500 Specification =>
1501 Make_Procedure_Specification (Loc,
1502 Defining_Unit_Name => Proc_Id,
1503 Parameter_Specifications => New_List (
1504 Make_Parameter_Specification (Loc,
1505 Defining_Identifier => Make_Temporary (Loc, 'I'),
1506 Parameter_Type => New_Occurrence_Of (Typ, Loc))))));
1507 end Build_Default_Init_Cond_Procedure_Declaration;
1509 ---------------------------
1510 -- Build_Default_Subtype --
1511 ---------------------------
1513 function Build_Default_Subtype
1514 (T : Entity_Id;
1515 N : Node_Id) return Entity_Id
1517 Loc : constant Source_Ptr := Sloc (N);
1518 Disc : Entity_Id;
1520 Bas : Entity_Id;
1521 -- The base type that is to be constrained by the defaults
1523 begin
1524 if not Has_Discriminants (T) or else Is_Constrained (T) then
1525 return T;
1526 end if;
1528 Bas := Base_Type (T);
1530 -- If T is non-private but its base type is private, this is the
1531 -- completion of a subtype declaration whose parent type is private
1532 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1533 -- are to be found in the full view of the base. Check that the private
1534 -- status of T and its base differ.
1536 if Is_Private_Type (Bas)
1537 and then not Is_Private_Type (T)
1538 and then Present (Full_View (Bas))
1539 then
1540 Bas := Full_View (Bas);
1541 end if;
1543 Disc := First_Discriminant (T);
1545 if No (Discriminant_Default_Value (Disc)) then
1546 return T;
1547 end if;
1549 declare
1550 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1551 Constraints : constant List_Id := New_List;
1552 Decl : Node_Id;
1554 begin
1555 while Present (Disc) loop
1556 Append_To (Constraints,
1557 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1558 Next_Discriminant (Disc);
1559 end loop;
1561 Decl :=
1562 Make_Subtype_Declaration (Loc,
1563 Defining_Identifier => Act,
1564 Subtype_Indication =>
1565 Make_Subtype_Indication (Loc,
1566 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1567 Constraint =>
1568 Make_Index_Or_Discriminant_Constraint (Loc,
1569 Constraints => Constraints)));
1571 Insert_Action (N, Decl);
1572 Analyze (Decl);
1573 return Act;
1574 end;
1575 end Build_Default_Subtype;
1577 --------------------------------------------
1578 -- Build_Discriminal_Subtype_Of_Component --
1579 --------------------------------------------
1581 function Build_Discriminal_Subtype_Of_Component
1582 (T : Entity_Id) return Node_Id
1584 Loc : constant Source_Ptr := Sloc (T);
1585 D : Elmt_Id;
1586 Id : Node_Id;
1588 function Build_Discriminal_Array_Constraint return List_Id;
1589 -- If one or more of the bounds of the component depends on
1590 -- discriminants, build actual constraint using the discriminants
1591 -- of the prefix.
1593 function Build_Discriminal_Record_Constraint return List_Id;
1594 -- Similar to previous one, for discriminated components constrained by
1595 -- the discriminant of the enclosing object.
1597 ----------------------------------------
1598 -- Build_Discriminal_Array_Constraint --
1599 ----------------------------------------
1601 function Build_Discriminal_Array_Constraint return List_Id is
1602 Constraints : constant List_Id := New_List;
1603 Indx : Node_Id;
1604 Hi : Node_Id;
1605 Lo : Node_Id;
1606 Old_Hi : Node_Id;
1607 Old_Lo : Node_Id;
1609 begin
1610 Indx := First_Index (T);
1611 while Present (Indx) loop
1612 Old_Lo := Type_Low_Bound (Etype (Indx));
1613 Old_Hi := Type_High_Bound (Etype (Indx));
1615 if Denotes_Discriminant (Old_Lo) then
1616 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1618 else
1619 Lo := New_Copy_Tree (Old_Lo);
1620 end if;
1622 if Denotes_Discriminant (Old_Hi) then
1623 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1625 else
1626 Hi := New_Copy_Tree (Old_Hi);
1627 end if;
1629 Append (Make_Range (Loc, Lo, Hi), Constraints);
1630 Next_Index (Indx);
1631 end loop;
1633 return Constraints;
1634 end Build_Discriminal_Array_Constraint;
1636 -----------------------------------------
1637 -- Build_Discriminal_Record_Constraint --
1638 -----------------------------------------
1640 function Build_Discriminal_Record_Constraint return List_Id is
1641 Constraints : constant List_Id := New_List;
1642 D : Elmt_Id;
1643 D_Val : Node_Id;
1645 begin
1646 D := First_Elmt (Discriminant_Constraint (T));
1647 while Present (D) loop
1648 if Denotes_Discriminant (Node (D)) then
1649 D_Val :=
1650 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1651 else
1652 D_Val := New_Copy_Tree (Node (D));
1653 end if;
1655 Append (D_Val, Constraints);
1656 Next_Elmt (D);
1657 end loop;
1659 return Constraints;
1660 end Build_Discriminal_Record_Constraint;
1662 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1664 begin
1665 if Ekind (T) = E_Array_Subtype then
1666 Id := First_Index (T);
1667 while Present (Id) loop
1668 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1669 or else
1670 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1671 then
1672 return Build_Component_Subtype
1673 (Build_Discriminal_Array_Constraint, Loc, T);
1674 end if;
1676 Next_Index (Id);
1677 end loop;
1679 elsif Ekind (T) = E_Record_Subtype
1680 and then Has_Discriminants (T)
1681 and then not Has_Unknown_Discriminants (T)
1682 then
1683 D := First_Elmt (Discriminant_Constraint (T));
1684 while Present (D) loop
1685 if Denotes_Discriminant (Node (D)) then
1686 return Build_Component_Subtype
1687 (Build_Discriminal_Record_Constraint, Loc, T);
1688 end if;
1690 Next_Elmt (D);
1691 end loop;
1692 end if;
1694 -- If none of the above, the actual and nominal subtypes are the same
1696 return Empty;
1697 end Build_Discriminal_Subtype_Of_Component;
1699 ------------------------------
1700 -- Build_Elaboration_Entity --
1701 ------------------------------
1703 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1704 Loc : constant Source_Ptr := Sloc (N);
1705 Decl : Node_Id;
1706 Elab_Ent : Entity_Id;
1708 procedure Set_Package_Name (Ent : Entity_Id);
1709 -- Given an entity, sets the fully qualified name of the entity in
1710 -- Name_Buffer, with components separated by double underscores. This
1711 -- is a recursive routine that climbs the scope chain to Standard.
1713 ----------------------
1714 -- Set_Package_Name --
1715 ----------------------
1717 procedure Set_Package_Name (Ent : Entity_Id) is
1718 begin
1719 if Scope (Ent) /= Standard_Standard then
1720 Set_Package_Name (Scope (Ent));
1722 declare
1723 Nam : constant String := Get_Name_String (Chars (Ent));
1724 begin
1725 Name_Buffer (Name_Len + 1) := '_';
1726 Name_Buffer (Name_Len + 2) := '_';
1727 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1728 Name_Len := Name_Len + Nam'Length + 2;
1729 end;
1731 else
1732 Get_Name_String (Chars (Ent));
1733 end if;
1734 end Set_Package_Name;
1736 -- Start of processing for Build_Elaboration_Entity
1738 begin
1739 -- Ignore call if already constructed
1741 if Present (Elaboration_Entity (Spec_Id)) then
1742 return;
1744 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1745 -- no role in analysis.
1747 elsif ASIS_Mode then
1748 return;
1750 -- See if we need elaboration entity. We always need it for the dynamic
1751 -- elaboration model, since it is needed to properly generate the PE
1752 -- exception for access before elaboration.
1754 elsif Dynamic_Elaboration_Checks then
1755 null;
1757 -- For the static model, we don't need the elaboration counter if this
1758 -- unit is sure to have no elaboration code, since that means there
1759 -- is no elaboration unit to be called. Note that we can't just decide
1760 -- after the fact by looking to see whether there was elaboration code,
1761 -- because that's too late to make this decision.
1763 elsif Restriction_Active (No_Elaboration_Code) then
1764 return;
1766 -- Similarly, for the static model, we can skip the elaboration counter
1767 -- if we have the No_Multiple_Elaboration restriction, since for the
1768 -- static model, that's the only purpose of the counter (to avoid
1769 -- multiple elaboration).
1771 elsif Restriction_Active (No_Multiple_Elaboration) then
1772 return;
1773 end if;
1775 -- Here we need the elaboration entity
1777 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1778 -- name with dots replaced by double underscore. We have to manually
1779 -- construct this name, since it will be elaborated in the outer scope,
1780 -- and thus will not have the unit name automatically prepended.
1782 Set_Package_Name (Spec_Id);
1783 Add_Str_To_Name_Buffer ("_E");
1785 -- Create elaboration counter
1787 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1788 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1790 Decl :=
1791 Make_Object_Declaration (Loc,
1792 Defining_Identifier => Elab_Ent,
1793 Object_Definition =>
1794 New_Occurrence_Of (Standard_Short_Integer, Loc),
1795 Expression => Make_Integer_Literal (Loc, Uint_0));
1797 Push_Scope (Standard_Standard);
1798 Add_Global_Declaration (Decl);
1799 Pop_Scope;
1801 -- Reset True_Constant indication, since we will indeed assign a value
1802 -- to the variable in the binder main. We also kill the Current_Value
1803 -- and Last_Assignment fields for the same reason.
1805 Set_Is_True_Constant (Elab_Ent, False);
1806 Set_Current_Value (Elab_Ent, Empty);
1807 Set_Last_Assignment (Elab_Ent, Empty);
1809 -- We do not want any further qualification of the name (if we did not
1810 -- do this, we would pick up the name of the generic package in the case
1811 -- of a library level generic instantiation).
1813 Set_Has_Qualified_Name (Elab_Ent);
1814 Set_Has_Fully_Qualified_Name (Elab_Ent);
1815 end Build_Elaboration_Entity;
1817 --------------------------------
1818 -- Build_Explicit_Dereference --
1819 --------------------------------
1821 procedure Build_Explicit_Dereference
1822 (Expr : Node_Id;
1823 Disc : Entity_Id)
1825 Loc : constant Source_Ptr := Sloc (Expr);
1827 begin
1828 -- An entity of a type with a reference aspect is overloaded with
1829 -- both interpretations: with and without the dereference. Now that
1830 -- the dereference is made explicit, set the type of the node properly,
1831 -- to prevent anomalies in the backend. Same if the expression is an
1832 -- overloaded function call whose return type has a reference aspect.
1834 if Is_Entity_Name (Expr) then
1835 Set_Etype (Expr, Etype (Entity (Expr)));
1837 elsif Nkind (Expr) = N_Function_Call then
1838 Set_Etype (Expr, Etype (Name (Expr)));
1839 end if;
1841 Set_Is_Overloaded (Expr, False);
1843 -- The expression will often be a generalized indexing that yields a
1844 -- container element that is then dereferenced, in which case the
1845 -- generalized indexing call is also non-overloaded.
1847 if Nkind (Expr) = N_Indexed_Component
1848 and then Present (Generalized_Indexing (Expr))
1849 then
1850 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1851 end if;
1853 Rewrite (Expr,
1854 Make_Explicit_Dereference (Loc,
1855 Prefix =>
1856 Make_Selected_Component (Loc,
1857 Prefix => Relocate_Node (Expr),
1858 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1859 Set_Etype (Prefix (Expr), Etype (Disc));
1860 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1861 end Build_Explicit_Dereference;
1863 -----------------------------------
1864 -- Cannot_Raise_Constraint_Error --
1865 -----------------------------------
1867 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1868 begin
1869 if Compile_Time_Known_Value (Expr) then
1870 return True;
1872 elsif Do_Range_Check (Expr) then
1873 return False;
1875 elsif Raises_Constraint_Error (Expr) then
1876 return False;
1878 else
1879 case Nkind (Expr) is
1880 when N_Identifier =>
1881 return True;
1883 when N_Expanded_Name =>
1884 return True;
1886 when N_Selected_Component =>
1887 return not Do_Discriminant_Check (Expr);
1889 when N_Attribute_Reference =>
1890 if Do_Overflow_Check (Expr) then
1891 return False;
1893 elsif No (Expressions (Expr)) then
1894 return True;
1896 else
1897 declare
1898 N : Node_Id;
1900 begin
1901 N := First (Expressions (Expr));
1902 while Present (N) loop
1903 if Cannot_Raise_Constraint_Error (N) then
1904 Next (N);
1905 else
1906 return False;
1907 end if;
1908 end loop;
1910 return True;
1911 end;
1912 end if;
1914 when N_Type_Conversion =>
1915 if Do_Overflow_Check (Expr)
1916 or else Do_Length_Check (Expr)
1917 or else Do_Tag_Check (Expr)
1918 then
1919 return False;
1920 else
1921 return Cannot_Raise_Constraint_Error (Expression (Expr));
1922 end if;
1924 when N_Unchecked_Type_Conversion =>
1925 return Cannot_Raise_Constraint_Error (Expression (Expr));
1927 when N_Unary_Op =>
1928 if Do_Overflow_Check (Expr) then
1929 return False;
1930 else
1931 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1932 end if;
1934 when N_Op_Divide |
1935 N_Op_Mod |
1936 N_Op_Rem
1938 if Do_Division_Check (Expr)
1939 or else
1940 Do_Overflow_Check (Expr)
1941 then
1942 return False;
1943 else
1944 return
1945 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1946 and then
1947 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1948 end if;
1950 when N_Op_Add |
1951 N_Op_And |
1952 N_Op_Concat |
1953 N_Op_Eq |
1954 N_Op_Expon |
1955 N_Op_Ge |
1956 N_Op_Gt |
1957 N_Op_Le |
1958 N_Op_Lt |
1959 N_Op_Multiply |
1960 N_Op_Ne |
1961 N_Op_Or |
1962 N_Op_Rotate_Left |
1963 N_Op_Rotate_Right |
1964 N_Op_Shift_Left |
1965 N_Op_Shift_Right |
1966 N_Op_Shift_Right_Arithmetic |
1967 N_Op_Subtract |
1968 N_Op_Xor
1970 if Do_Overflow_Check (Expr) then
1971 return False;
1972 else
1973 return
1974 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1975 and then
1976 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1977 end if;
1979 when others =>
1980 return False;
1981 end case;
1982 end if;
1983 end Cannot_Raise_Constraint_Error;
1985 -----------------------------------------
1986 -- Check_Dynamically_Tagged_Expression --
1987 -----------------------------------------
1989 procedure Check_Dynamically_Tagged_Expression
1990 (Expr : Node_Id;
1991 Typ : Entity_Id;
1992 Related_Nod : Node_Id)
1994 begin
1995 pragma Assert (Is_Tagged_Type (Typ));
1997 -- In order to avoid spurious errors when analyzing the expanded code,
1998 -- this check is done only for nodes that come from source and for
1999 -- actuals of generic instantiations.
2001 if (Comes_From_Source (Related_Nod)
2002 or else In_Generic_Actual (Expr))
2003 and then (Is_Class_Wide_Type (Etype (Expr))
2004 or else Is_Dynamically_Tagged (Expr))
2005 and then Is_Tagged_Type (Typ)
2006 and then not Is_Class_Wide_Type (Typ)
2007 then
2008 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
2009 end if;
2010 end Check_Dynamically_Tagged_Expression;
2012 --------------------------
2013 -- Check_Fully_Declared --
2014 --------------------------
2016 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2017 begin
2018 if Ekind (T) = E_Incomplete_Type then
2020 -- Ada 2005 (AI-50217): If the type is available through a limited
2021 -- with_clause, verify that its full view has been analyzed.
2023 if From_Limited_With (T)
2024 and then Present (Non_Limited_View (T))
2025 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2026 then
2027 -- The non-limited view is fully declared
2029 null;
2031 else
2032 Error_Msg_NE
2033 ("premature usage of incomplete}", N, First_Subtype (T));
2034 end if;
2036 -- Need comments for these tests ???
2038 elsif Has_Private_Component (T)
2039 and then not Is_Generic_Type (Root_Type (T))
2040 and then not In_Spec_Expression
2041 then
2042 -- Special case: if T is the anonymous type created for a single
2043 -- task or protected object, use the name of the source object.
2045 if Is_Concurrent_Type (T)
2046 and then not Comes_From_Source (T)
2047 and then Nkind (N) = N_Object_Declaration
2048 then
2049 Error_Msg_NE
2050 ("type of& has incomplete component",
2051 N, Defining_Identifier (N));
2052 else
2053 Error_Msg_NE
2054 ("premature usage of incomplete}",
2055 N, First_Subtype (T));
2056 end if;
2057 end if;
2058 end Check_Fully_Declared;
2060 -------------------------------------
2061 -- Check_Function_Writable_Actuals --
2062 -------------------------------------
2064 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2065 Writable_Actuals_List : Elist_Id := No_Elist;
2066 Identifiers_List : Elist_Id := No_Elist;
2067 Error_Node : Node_Id := Empty;
2069 procedure Collect_Identifiers (N : Node_Id);
2070 -- In a single traversal of subtree N collect in Writable_Actuals_List
2071 -- all the actuals of functions with writable actuals, and in the list
2072 -- Identifiers_List collect all the identifiers that are not actuals of
2073 -- functions with writable actuals. If a writable actual is referenced
2074 -- twice as writable actual then Error_Node is set to reference its
2075 -- second occurrence, the error is reported, and the tree traversal
2076 -- is abandoned.
2078 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2079 -- Return the entity associated with the function call
2081 procedure Preanalyze_Without_Errors (N : Node_Id);
2082 -- Preanalyze N without reporting errors. Very dubious, you can't just
2083 -- go analyzing things more than once???
2085 -------------------------
2086 -- Collect_Identifiers --
2087 -------------------------
2089 procedure Collect_Identifiers (N : Node_Id) is
2091 function Check_Node (N : Node_Id) return Traverse_Result;
2092 -- Process a single node during the tree traversal to collect the
2093 -- writable actuals of functions and all the identifiers which are
2094 -- not writable actuals of functions.
2096 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2097 -- Returns True if List has a node whose Entity is Entity (N)
2099 -------------------------
2100 -- Check_Function_Call --
2101 -------------------------
2103 function Check_Node (N : Node_Id) return Traverse_Result is
2104 Is_Writable_Actual : Boolean := False;
2105 Id : Entity_Id;
2107 begin
2108 if Nkind (N) = N_Identifier then
2110 -- No analysis possible if the entity is not decorated
2112 if No (Entity (N)) then
2113 return Skip;
2115 -- Don't collect identifiers of packages, called functions, etc
2117 elsif Ekind_In (Entity (N), E_Package,
2118 E_Function,
2119 E_Procedure,
2120 E_Entry)
2121 then
2122 return Skip;
2124 -- Analyze if N is a writable actual of a function
2126 elsif Nkind (Parent (N)) = N_Function_Call then
2127 declare
2128 Call : constant Node_Id := Parent (N);
2129 Actual : Node_Id;
2130 Formal : Node_Id;
2132 begin
2133 Id := Get_Function_Id (Call);
2135 Formal := First_Formal (Id);
2136 Actual := First_Actual (Call);
2137 while Present (Actual) and then Present (Formal) loop
2138 if Actual = N then
2139 if Ekind_In (Formal, E_Out_Parameter,
2140 E_In_Out_Parameter)
2141 then
2142 Is_Writable_Actual := True;
2143 end if;
2145 exit;
2146 end if;
2148 Next_Formal (Formal);
2149 Next_Actual (Actual);
2150 end loop;
2151 end;
2152 end if;
2154 if Is_Writable_Actual then
2155 if Contains (Writable_Actuals_List, N) then
2156 Error_Msg_NE
2157 ("value may be affected by call to& "
2158 & "because order of evaluation is arbitrary", N, Id);
2159 Error_Node := N;
2160 return Abandon;
2161 end if;
2163 Append_New_Elmt (N, To => Writable_Actuals_List);
2165 else
2166 if Identifiers_List = No_Elist then
2167 Identifiers_List := New_Elmt_List;
2168 end if;
2170 Append_Unique_Elmt (N, Identifiers_List);
2171 end if;
2172 end if;
2174 return OK;
2175 end Check_Node;
2177 --------------
2178 -- Contains --
2179 --------------
2181 function Contains
2182 (List : Elist_Id;
2183 N : Node_Id) return Boolean
2185 pragma Assert (Nkind (N) in N_Has_Entity);
2187 Elmt : Elmt_Id;
2189 begin
2190 if List = No_Elist then
2191 return False;
2192 end if;
2194 Elmt := First_Elmt (List);
2195 while Present (Elmt) loop
2196 if Entity (Node (Elmt)) = Entity (N) then
2197 return True;
2198 else
2199 Next_Elmt (Elmt);
2200 end if;
2201 end loop;
2203 return False;
2204 end Contains;
2206 ------------------
2207 -- Do_Traversal --
2208 ------------------
2210 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2211 -- The traversal procedure
2213 -- Start of processing for Collect_Identifiers
2215 begin
2216 if Present (Error_Node) then
2217 return;
2218 end if;
2220 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2221 return;
2222 end if;
2224 Do_Traversal (N);
2225 end Collect_Identifiers;
2227 ---------------------
2228 -- Get_Function_Id --
2229 ---------------------
2231 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2232 Nam : constant Node_Id := Name (Call);
2233 Id : Entity_Id;
2235 begin
2236 if Nkind (Nam) = N_Explicit_Dereference then
2237 Id := Etype (Nam);
2238 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2240 elsif Nkind (Nam) = N_Selected_Component then
2241 Id := Entity (Selector_Name (Nam));
2243 elsif Nkind (Nam) = N_Indexed_Component then
2244 Id := Entity (Selector_Name (Prefix (Nam)));
2246 else
2247 Id := Entity (Nam);
2248 end if;
2250 return Id;
2251 end Get_Function_Id;
2253 ---------------------------
2254 -- Preanalyze_Expression --
2255 ---------------------------
2257 procedure Preanalyze_Without_Errors (N : Node_Id) is
2258 Status : constant Boolean := Get_Ignore_Errors;
2259 begin
2260 Set_Ignore_Errors (True);
2261 Preanalyze (N);
2262 Set_Ignore_Errors (Status);
2263 end Preanalyze_Without_Errors;
2265 -- Start of processing for Check_Function_Writable_Actuals
2267 begin
2268 -- The check only applies to Ada 2012 code, and only to constructs that
2269 -- have multiple constituents whose order of evaluation is not specified
2270 -- by the language.
2272 if Ada_Version < Ada_2012
2273 or else (not (Nkind (N) in N_Op)
2274 and then not (Nkind (N) in N_Membership_Test)
2275 and then not Nkind_In (N, N_Range,
2276 N_Aggregate,
2277 N_Extension_Aggregate,
2278 N_Full_Type_Declaration,
2279 N_Function_Call,
2280 N_Procedure_Call_Statement,
2281 N_Entry_Call_Statement))
2282 or else (Nkind (N) = N_Full_Type_Declaration
2283 and then not Is_Record_Type (Defining_Identifier (N)))
2285 -- In addition, this check only applies to source code, not to code
2286 -- generated by constraint checks.
2288 or else not Comes_From_Source (N)
2289 then
2290 return;
2291 end if;
2293 -- If a construct C has two or more direct constituents that are names
2294 -- or expressions whose evaluation may occur in an arbitrary order, at
2295 -- least one of which contains a function call with an in out or out
2296 -- parameter, then the construct is legal only if: for each name N that
2297 -- is passed as a parameter of mode in out or out to some inner function
2298 -- call C2 (not including the construct C itself), there is no other
2299 -- name anywhere within a direct constituent of the construct C other
2300 -- than the one containing C2, that is known to refer to the same
2301 -- object (RM 6.4.1(6.17/3)).
2303 case Nkind (N) is
2304 when N_Range =>
2305 Collect_Identifiers (Low_Bound (N));
2306 Collect_Identifiers (High_Bound (N));
2308 when N_Op | N_Membership_Test =>
2309 declare
2310 Expr : Node_Id;
2312 begin
2313 Collect_Identifiers (Left_Opnd (N));
2315 if Present (Right_Opnd (N)) then
2316 Collect_Identifiers (Right_Opnd (N));
2317 end if;
2319 if Nkind_In (N, N_In, N_Not_In)
2320 and then Present (Alternatives (N))
2321 then
2322 Expr := First (Alternatives (N));
2323 while Present (Expr) loop
2324 Collect_Identifiers (Expr);
2326 Next (Expr);
2327 end loop;
2328 end if;
2329 end;
2331 when N_Full_Type_Declaration =>
2332 declare
2333 function Get_Record_Part (N : Node_Id) return Node_Id;
2334 -- Return the record part of this record type definition
2336 function Get_Record_Part (N : Node_Id) return Node_Id is
2337 Type_Def : constant Node_Id := Type_Definition (N);
2338 begin
2339 if Nkind (Type_Def) = N_Derived_Type_Definition then
2340 return Record_Extension_Part (Type_Def);
2341 else
2342 return Type_Def;
2343 end if;
2344 end Get_Record_Part;
2346 Comp : Node_Id;
2347 Def_Id : Entity_Id := Defining_Identifier (N);
2348 Rec : Node_Id := Get_Record_Part (N);
2350 begin
2351 -- No need to perform any analysis if the record has no
2352 -- components
2354 if No (Rec) or else No (Component_List (Rec)) then
2355 return;
2356 end if;
2358 -- Collect the identifiers starting from the deepest
2359 -- derivation. Done to report the error in the deepest
2360 -- derivation.
2362 loop
2363 if Present (Component_List (Rec)) then
2364 Comp := First (Component_Items (Component_List (Rec)));
2365 while Present (Comp) loop
2366 if Nkind (Comp) = N_Component_Declaration
2367 and then Present (Expression (Comp))
2368 then
2369 Collect_Identifiers (Expression (Comp));
2370 end if;
2372 Next (Comp);
2373 end loop;
2374 end if;
2376 exit when No (Underlying_Type (Etype (Def_Id)))
2377 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2378 = Def_Id;
2380 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2381 Rec := Get_Record_Part (Parent (Def_Id));
2382 end loop;
2383 end;
2385 when N_Subprogram_Call |
2386 N_Entry_Call_Statement =>
2387 declare
2388 Id : constant Entity_Id := Get_Function_Id (N);
2389 Formal : Node_Id;
2390 Actual : Node_Id;
2392 begin
2393 Formal := First_Formal (Id);
2394 Actual := First_Actual (N);
2395 while Present (Actual) and then Present (Formal) loop
2396 if Ekind_In (Formal, E_Out_Parameter,
2397 E_In_Out_Parameter)
2398 then
2399 Collect_Identifiers (Actual);
2400 end if;
2402 Next_Formal (Formal);
2403 Next_Actual (Actual);
2404 end loop;
2405 end;
2407 when N_Aggregate |
2408 N_Extension_Aggregate =>
2409 declare
2410 Assoc : Node_Id;
2411 Choice : Node_Id;
2412 Comp_Expr : Node_Id;
2414 begin
2415 -- Handle the N_Others_Choice of array aggregates with static
2416 -- bounds. There is no need to perform this analysis in
2417 -- aggregates without static bounds since we cannot evaluate
2418 -- if the N_Others_Choice covers several elements. There is
2419 -- no need to handle the N_Others choice of record aggregates
2420 -- since at this stage it has been already expanded by
2421 -- Resolve_Record_Aggregate.
2423 if Is_Array_Type (Etype (N))
2424 and then Nkind (N) = N_Aggregate
2425 and then Present (Aggregate_Bounds (N))
2426 and then Compile_Time_Known_Bounds (Etype (N))
2427 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2429 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2430 then
2431 declare
2432 Count_Components : Uint := Uint_0;
2433 Num_Components : Uint;
2434 Others_Assoc : Node_Id;
2435 Others_Choice : Node_Id := Empty;
2436 Others_Box_Present : Boolean := False;
2438 begin
2439 -- Count positional associations
2441 if Present (Expressions (N)) then
2442 Comp_Expr := First (Expressions (N));
2443 while Present (Comp_Expr) loop
2444 Count_Components := Count_Components + 1;
2445 Next (Comp_Expr);
2446 end loop;
2447 end if;
2449 -- Count the rest of elements and locate the N_Others
2450 -- choice (if any)
2452 Assoc := First (Component_Associations (N));
2453 while Present (Assoc) loop
2454 Choice := First (Choices (Assoc));
2455 while Present (Choice) loop
2456 if Nkind (Choice) = N_Others_Choice then
2457 Others_Assoc := Assoc;
2458 Others_Choice := Choice;
2459 Others_Box_Present := Box_Present (Assoc);
2461 -- Count several components
2463 elsif Nkind_In (Choice, N_Range,
2464 N_Subtype_Indication)
2465 or else (Is_Entity_Name (Choice)
2466 and then Is_Type (Entity (Choice)))
2467 then
2468 declare
2469 L, H : Node_Id;
2470 begin
2471 Get_Index_Bounds (Choice, L, H);
2472 pragma Assert
2473 (Compile_Time_Known_Value (L)
2474 and then Compile_Time_Known_Value (H));
2475 Count_Components :=
2476 Count_Components
2477 + Expr_Value (H) - Expr_Value (L) + 1;
2478 end;
2480 -- Count single component. No other case available
2481 -- since we are handling an aggregate with static
2482 -- bounds.
2484 else
2485 pragma Assert (Is_OK_Static_Expression (Choice)
2486 or else Nkind (Choice) = N_Identifier
2487 or else Nkind (Choice) = N_Integer_Literal);
2489 Count_Components := Count_Components + 1;
2490 end if;
2492 Next (Choice);
2493 end loop;
2495 Next (Assoc);
2496 end loop;
2498 Num_Components :=
2499 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2500 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2502 pragma Assert (Count_Components <= Num_Components);
2504 -- Handle the N_Others choice if it covers several
2505 -- components
2507 if Present (Others_Choice)
2508 and then (Num_Components - Count_Components) > 1
2509 then
2510 if not Others_Box_Present then
2512 -- At this stage, if expansion is active, the
2513 -- expression of the others choice has not been
2514 -- analyzed. Hence we generate a duplicate and
2515 -- we analyze it silently to have available the
2516 -- minimum decoration required to collect the
2517 -- identifiers.
2519 if not Expander_Active then
2520 Comp_Expr := Expression (Others_Assoc);
2521 else
2522 Comp_Expr :=
2523 New_Copy_Tree (Expression (Others_Assoc));
2524 Preanalyze_Without_Errors (Comp_Expr);
2525 end if;
2527 Collect_Identifiers (Comp_Expr);
2529 if Writable_Actuals_List /= No_Elist then
2531 -- As suggested by Robert, at current stage we
2532 -- report occurrences of this case as warnings.
2534 Error_Msg_N
2535 ("writable function parameter may affect "
2536 & "value in other component because order "
2537 & "of evaluation is unspecified??",
2538 Node (First_Elmt (Writable_Actuals_List)));
2539 end if;
2540 end if;
2541 end if;
2542 end;
2543 end if;
2545 -- Handle ancestor part of extension aggregates
2547 if Nkind (N) = N_Extension_Aggregate then
2548 Collect_Identifiers (Ancestor_Part (N));
2549 end if;
2551 -- Handle positional associations
2553 if Present (Expressions (N)) then
2554 Comp_Expr := First (Expressions (N));
2555 while Present (Comp_Expr) loop
2556 if not Is_OK_Static_Expression (Comp_Expr) then
2557 Collect_Identifiers (Comp_Expr);
2558 end if;
2560 Next (Comp_Expr);
2561 end loop;
2562 end if;
2564 -- Handle discrete associations
2566 if Present (Component_Associations (N)) then
2567 Assoc := First (Component_Associations (N));
2568 while Present (Assoc) loop
2570 if not Box_Present (Assoc) then
2571 Choice := First (Choices (Assoc));
2572 while Present (Choice) loop
2574 -- For now we skip discriminants since it requires
2575 -- performing the analysis in two phases: first one
2576 -- analyzing discriminants and second one analyzing
2577 -- the rest of components since discriminants are
2578 -- evaluated prior to components: too much extra
2579 -- work to detect a corner case???
2581 if Nkind (Choice) in N_Has_Entity
2582 and then Present (Entity (Choice))
2583 and then Ekind (Entity (Choice)) = E_Discriminant
2584 then
2585 null;
2587 elsif Box_Present (Assoc) then
2588 null;
2590 else
2591 if not Analyzed (Expression (Assoc)) then
2592 Comp_Expr :=
2593 New_Copy_Tree (Expression (Assoc));
2594 Set_Parent (Comp_Expr, Parent (N));
2595 Preanalyze_Without_Errors (Comp_Expr);
2596 else
2597 Comp_Expr := Expression (Assoc);
2598 end if;
2600 Collect_Identifiers (Comp_Expr);
2601 end if;
2603 Next (Choice);
2604 end loop;
2605 end if;
2607 Next (Assoc);
2608 end loop;
2609 end if;
2610 end;
2612 when others =>
2613 return;
2614 end case;
2616 -- No further action needed if we already reported an error
2618 if Present (Error_Node) then
2619 return;
2620 end if;
2622 -- Check if some writable argument of a function is referenced
2624 if Writable_Actuals_List /= No_Elist
2625 and then Identifiers_List /= No_Elist
2626 then
2627 declare
2628 Elmt_1 : Elmt_Id;
2629 Elmt_2 : Elmt_Id;
2631 begin
2632 Elmt_1 := First_Elmt (Writable_Actuals_List);
2633 while Present (Elmt_1) loop
2634 Elmt_2 := First_Elmt (Identifiers_List);
2635 while Present (Elmt_2) loop
2636 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2637 case Nkind (Parent (Node (Elmt_2))) is
2638 when N_Aggregate |
2639 N_Component_Association |
2640 N_Component_Declaration =>
2641 Error_Msg_N
2642 ("value may be affected by call in other "
2643 & "component because they are evaluated "
2644 & "in unspecified order",
2645 Node (Elmt_2));
2647 when N_In | N_Not_In =>
2648 Error_Msg_N
2649 ("value may be affected by call in other "
2650 & "alternative because they are evaluated "
2651 & "in unspecified order",
2652 Node (Elmt_2));
2654 when others =>
2655 Error_Msg_N
2656 ("value of actual may be affected by call in "
2657 & "other actual because they are evaluated "
2658 & "in unspecified order",
2659 Node (Elmt_2));
2660 end case;
2661 end if;
2663 Next_Elmt (Elmt_2);
2664 end loop;
2666 Next_Elmt (Elmt_1);
2667 end loop;
2668 end;
2669 end if;
2670 end Check_Function_Writable_Actuals;
2672 ----------------------------
2673 -- Check_Ghost_Completion --
2674 ----------------------------
2676 procedure Check_Ghost_Completion
2677 (Partial_View : Entity_Id;
2678 Full_View : Entity_Id)
2680 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
2682 begin
2683 -- The Ghost policy in effect at the point of declaration and at the
2684 -- point of completion must match (SPARK RM 6.9(14)).
2686 if Is_Checked_Ghost_Entity (Partial_View)
2687 and then Policy = Name_Ignore
2688 then
2689 Error_Msg_Sloc := Sloc (Full_View);
2691 Error_Msg_N ("incompatible ghost policies in effect", Partial_View);
2692 Error_Msg_N ("\& declared with ghost policy Check", Partial_View);
2693 Error_Msg_N ("\& completed # with ghost policy Ignore", Partial_View);
2695 elsif Is_Ignored_Ghost_Entity (Partial_View)
2696 and then Policy = Name_Check
2697 then
2698 Error_Msg_Sloc := Sloc (Full_View);
2700 Error_Msg_N ("incompatible ghost policies in effect", Partial_View);
2701 Error_Msg_N ("\& declared with ghost policy Ignore", Partial_View);
2702 Error_Msg_N ("\& completed # with ghost policy Check", Partial_View);
2703 end if;
2704 end Check_Ghost_Completion;
2706 ----------------------------
2707 -- Check_Ghost_Derivation --
2708 ----------------------------
2710 procedure Check_Ghost_Derivation (Typ : Entity_Id) is
2711 Parent_Typ : constant Entity_Id := Etype (Typ);
2712 Iface : Entity_Id;
2713 Iface_Elmt : Elmt_Id;
2715 begin
2716 -- Allow untagged derivations from predefined types such as Integer as
2717 -- those are not Ghost by definition.
2719 if Is_Scalar_Type (Typ) and then Parent_Typ = Base_Type (Typ) then
2720 null;
2722 -- The parent type of a Ghost type extension must be Ghost
2724 elsif not Is_Ghost_Entity (Parent_Typ) then
2725 Error_Msg_N ("type extension & cannot be ghost", Typ);
2726 Error_Msg_NE ("\parent type & is not ghost", Typ, Parent_Typ);
2727 return;
2728 end if;
2730 -- All progenitors (if any) must be Ghost as well
2732 if Is_Tagged_Type (Typ) and then Present (Interfaces (Typ)) then
2733 Iface_Elmt := First_Elmt (Interfaces (Typ));
2734 while Present (Iface_Elmt) loop
2735 Iface := Node (Iface_Elmt);
2737 if not Is_Ghost_Entity (Iface) then
2738 Error_Msg_N ("type extension & cannot be ghost", Typ);
2739 Error_Msg_NE ("\interface type & is not ghost", Typ, Iface);
2740 return;
2741 end if;
2743 Next_Elmt (Iface_Elmt);
2744 end loop;
2745 end if;
2746 end Check_Ghost_Derivation;
2748 --------------------------------
2749 -- Check_Implicit_Dereference --
2750 --------------------------------
2752 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2753 Disc : Entity_Id;
2754 Desig : Entity_Id;
2755 Nam : Node_Id;
2757 begin
2758 if Nkind (N) = N_Indexed_Component
2759 and then Present (Generalized_Indexing (N))
2760 then
2761 Nam := Generalized_Indexing (N);
2763 else
2764 Nam := N;
2765 end if;
2767 if Ada_Version < Ada_2012
2768 or else not Has_Implicit_Dereference (Base_Type (Typ))
2769 then
2770 return;
2772 elsif not Comes_From_Source (N)
2773 and then Nkind (N) /= N_Indexed_Component
2774 then
2775 return;
2777 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2778 null;
2780 else
2781 Disc := First_Discriminant (Typ);
2782 while Present (Disc) loop
2783 if Has_Implicit_Dereference (Disc) then
2784 Desig := Designated_Type (Etype (Disc));
2785 Add_One_Interp (Nam, Disc, Desig);
2787 -- If the node is a generalized indexing, add interpretation
2788 -- to that node as well, for subsequent resolution.
2790 if Nkind (N) = N_Indexed_Component then
2791 Add_One_Interp (N, Disc, Desig);
2792 end if;
2794 -- If the operation comes from a generic unit and the context
2795 -- is a selected component, the selector name may be global
2796 -- and set in the instance already. Remove the entity to
2797 -- force resolution of the selected component, and the
2798 -- generation of an explicit dereference if needed.
2800 if In_Instance
2801 and then Nkind (Parent (Nam)) = N_Selected_Component
2802 then
2803 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2804 end if;
2806 exit;
2807 end if;
2809 Next_Discriminant (Disc);
2810 end loop;
2811 end if;
2812 end Check_Implicit_Dereference;
2814 ----------------------------------
2815 -- Check_Internal_Protected_Use --
2816 ----------------------------------
2818 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2819 S : Entity_Id;
2820 Prot : Entity_Id;
2822 begin
2823 S := Current_Scope;
2824 while Present (S) loop
2825 if S = Standard_Standard then
2826 return;
2828 elsif Ekind (S) = E_Function
2829 and then Ekind (Scope (S)) = E_Protected_Type
2830 then
2831 Prot := Scope (S);
2832 exit;
2833 end if;
2835 S := Scope (S);
2836 end loop;
2838 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2840 -- An indirect function call (e.g. a callback within a protected
2841 -- function body) is not statically illegal. If the access type is
2842 -- anonymous and is the type of an access parameter, the scope of Nam
2843 -- will be the protected type, but it is not a protected operation.
2845 if Ekind (Nam) = E_Subprogram_Type
2846 and then
2847 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2848 then
2849 null;
2851 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2852 Error_Msg_N
2853 ("within protected function cannot use protected "
2854 & "procedure in renaming or as generic actual", N);
2856 elsif Nkind (N) = N_Attribute_Reference then
2857 Error_Msg_N
2858 ("within protected function cannot take access of "
2859 & " protected procedure", N);
2861 else
2862 Error_Msg_N
2863 ("within protected function, protected object is constant", N);
2864 Error_Msg_N
2865 ("\cannot call operation that may modify it", N);
2866 end if;
2867 end if;
2868 end Check_Internal_Protected_Use;
2870 ---------------------------------------
2871 -- Check_Later_Vs_Basic_Declarations --
2872 ---------------------------------------
2874 procedure Check_Later_Vs_Basic_Declarations
2875 (Decls : List_Id;
2876 During_Parsing : Boolean)
2878 Body_Sloc : Source_Ptr;
2879 Decl : Node_Id;
2881 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2882 -- Return whether Decl is considered as a declarative item.
2883 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2884 -- When During_Parsing is False, the semantics of SPARK is followed.
2886 -------------------------------
2887 -- Is_Later_Declarative_Item --
2888 -------------------------------
2890 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2891 begin
2892 if Nkind (Decl) in N_Later_Decl_Item then
2893 return True;
2895 elsif Nkind (Decl) = N_Pragma then
2896 return True;
2898 elsif During_Parsing then
2899 return False;
2901 -- In SPARK, a package declaration is not considered as a later
2902 -- declarative item.
2904 elsif Nkind (Decl) = N_Package_Declaration then
2905 return False;
2907 -- In SPARK, a renaming is considered as a later declarative item
2909 elsif Nkind (Decl) in N_Renaming_Declaration then
2910 return True;
2912 else
2913 return False;
2914 end if;
2915 end Is_Later_Declarative_Item;
2917 -- Start of Check_Later_Vs_Basic_Declarations
2919 begin
2920 Decl := First (Decls);
2922 -- Loop through sequence of basic declarative items
2924 Outer : while Present (Decl) loop
2925 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
2926 and then Nkind (Decl) not in N_Body_Stub
2927 then
2928 Next (Decl);
2930 -- Once a body is encountered, we only allow later declarative
2931 -- items. The inner loop checks the rest of the list.
2933 else
2934 Body_Sloc := Sloc (Decl);
2936 Inner : while Present (Decl) loop
2937 if not Is_Later_Declarative_Item (Decl) then
2938 if During_Parsing then
2939 if Ada_Version = Ada_83 then
2940 Error_Msg_Sloc := Body_Sloc;
2941 Error_Msg_N
2942 ("(Ada 83) decl cannot appear after body#", Decl);
2943 end if;
2944 else
2945 Error_Msg_Sloc := Body_Sloc;
2946 Check_SPARK_05_Restriction
2947 ("decl cannot appear after body#", Decl);
2948 end if;
2949 end if;
2951 Next (Decl);
2952 end loop Inner;
2953 end if;
2954 end loop Outer;
2955 end Check_Later_Vs_Basic_Declarations;
2957 -------------------------
2958 -- Check_Nested_Access --
2959 -------------------------
2961 procedure Check_Nested_Access (Ent : Entity_Id) is
2962 Scop : constant Entity_Id := Current_Scope;
2963 Current_Subp : Entity_Id;
2964 Enclosing : Entity_Id;
2966 begin
2967 -- Currently only enabled for VM back-ends for efficiency, should we
2968 -- enable it more systematically ???
2970 -- Check for Is_Imported needs commenting below ???
2972 if VM_Target /= No_VM
2973 and then Ekind_In (Ent, E_Variable, E_Constant, E_Loop_Parameter)
2974 and then Scope (Ent) /= Empty
2975 and then not Is_Library_Level_Entity (Ent)
2976 and then not Is_Imported (Ent)
2977 then
2978 if Is_Subprogram (Scop)
2979 or else Is_Generic_Subprogram (Scop)
2980 or else Is_Entry (Scop)
2981 then
2982 Current_Subp := Scop;
2983 else
2984 Current_Subp := Current_Subprogram;
2985 end if;
2987 Enclosing := Enclosing_Subprogram (Ent);
2989 if Enclosing /= Empty and then Enclosing /= Current_Subp then
2990 Set_Has_Up_Level_Access (Ent, True);
2991 end if;
2992 end if;
2993 end Check_Nested_Access;
2995 ---------------------------
2996 -- Check_No_Hidden_State --
2997 ---------------------------
2999 procedure Check_No_Hidden_State (Id : Entity_Id) is
3000 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
3001 -- Determine whether the entity of a package denoted by Pkg has a null
3002 -- abstract state.
3004 -----------------------------
3005 -- Has_Null_Abstract_State --
3006 -----------------------------
3008 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
3009 States : constant Elist_Id := Abstract_States (Pkg);
3011 begin
3012 -- Check first available state of related package. A null abstract
3013 -- state always appears as the sole element of the state list.
3015 return
3016 Present (States)
3017 and then Is_Null_State (Node (First_Elmt (States)));
3018 end Has_Null_Abstract_State;
3020 -- Local variables
3022 Context : Entity_Id := Empty;
3023 Not_Visible : Boolean := False;
3024 Scop : Entity_Id;
3026 -- Start of processing for Check_No_Hidden_State
3028 begin
3029 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3031 -- Find the proper context where the object or state appears
3033 Scop := Scope (Id);
3034 while Present (Scop) loop
3035 Context := Scop;
3037 -- Keep track of the context's visibility
3039 Not_Visible := Not_Visible or else In_Private_Part (Context);
3041 -- Prevent the search from going too far
3043 if Context = Standard_Standard then
3044 return;
3046 -- Objects and states that appear immediately within a subprogram or
3047 -- inside a construct nested within a subprogram do not introduce a
3048 -- hidden state. They behave as local variable declarations.
3050 elsif Is_Subprogram (Context) then
3051 return;
3053 -- When examining a package body, use the entity of the spec as it
3054 -- carries the abstract state declarations.
3056 elsif Ekind (Context) = E_Package_Body then
3057 Context := Spec_Entity (Context);
3058 end if;
3060 -- Stop the traversal when a package subject to a null abstract state
3061 -- has been found.
3063 if Ekind_In (Context, E_Generic_Package, E_Package)
3064 and then Has_Null_Abstract_State (Context)
3065 then
3066 exit;
3067 end if;
3069 Scop := Scope (Scop);
3070 end loop;
3072 -- At this point we know that there is at least one package with a null
3073 -- abstract state in visibility. Emit an error message unconditionally
3074 -- if the entity being processed is a state because the placement of the
3075 -- related package is irrelevant. This is not the case for objects as
3076 -- the intermediate context matters.
3078 if Present (Context)
3079 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3080 then
3081 Error_Msg_N ("cannot introduce hidden state &", Id);
3082 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3083 end if;
3084 end Check_No_Hidden_State;
3086 ------------------------------------------
3087 -- Check_Potentially_Blocking_Operation --
3088 ------------------------------------------
3090 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3091 S : Entity_Id;
3093 begin
3094 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3095 -- When pragma Detect_Blocking is active, the run time will raise
3096 -- Program_Error. Here we only issue a warning, since we generally
3097 -- support the use of potentially blocking operations in the absence
3098 -- of the pragma.
3100 -- Indirect blocking through a subprogram call cannot be diagnosed
3101 -- statically without interprocedural analysis, so we do not attempt
3102 -- to do it here.
3104 S := Scope (Current_Scope);
3105 while Present (S) and then S /= Standard_Standard loop
3106 if Is_Protected_Type (S) then
3107 Error_Msg_N
3108 ("potentially blocking operation in protected operation??", N);
3109 return;
3110 end if;
3112 S := Scope (S);
3113 end loop;
3114 end Check_Potentially_Blocking_Operation;
3116 ---------------------------------
3117 -- Check_Result_And_Post_State --
3118 ---------------------------------
3120 procedure Check_Result_And_Post_State
3121 (Prag : Node_Id;
3122 Result_Seen : in out Boolean)
3124 procedure Check_Expression (Expr : Node_Id);
3125 -- Perform the 'Result and post-state checks on a given expression
3127 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3128 -- Attempt to find attribute 'Result in a subtree denoted by N
3130 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3131 -- Determine whether source node N denotes "True" or "False"
3133 function Mentions_Post_State (N : Node_Id) return Boolean;
3134 -- Determine whether a subtree denoted by N mentions any construct that
3135 -- denotes a post-state.
3137 procedure Check_Function_Result is
3138 new Traverse_Proc (Is_Function_Result);
3140 ----------------------
3141 -- Check_Expression --
3142 ----------------------
3144 procedure Check_Expression (Expr : Node_Id) is
3145 begin
3146 if not Is_Trivial_Boolean (Expr) then
3147 Check_Function_Result (Expr);
3149 if not Mentions_Post_State (Expr) then
3150 if Pragma_Name (Prag) = Name_Contract_Cases then
3151 Error_Msg_N
3152 ("contract case refers only to pre-state?T?", Expr);
3154 elsif Pragma_Name (Prag) = Name_Refined_Post then
3155 Error_Msg_N
3156 ("refined postcondition refers only to pre-state?T?",
3157 Prag);
3159 else
3160 Error_Msg_N
3161 ("postcondition refers only to pre-state?T?", Prag);
3162 end if;
3163 end if;
3164 end if;
3165 end Check_Expression;
3167 ------------------------
3168 -- Is_Function_Result --
3169 ------------------------
3171 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3172 begin
3173 if Is_Attribute_Result (N) then
3174 Result_Seen := True;
3175 return Abandon;
3177 -- Continue the traversal
3179 else
3180 return OK;
3181 end if;
3182 end Is_Function_Result;
3184 ------------------------
3185 -- Is_Trivial_Boolean --
3186 ------------------------
3188 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3189 begin
3190 return
3191 Comes_From_Source (N)
3192 and then Is_Entity_Name (N)
3193 and then (Entity (N) = Standard_True
3194 or else
3195 Entity (N) = Standard_False);
3196 end Is_Trivial_Boolean;
3198 -------------------------
3199 -- Mentions_Post_State --
3200 -------------------------
3202 function Mentions_Post_State (N : Node_Id) return Boolean is
3203 Post_State_Seen : Boolean := False;
3205 function Is_Post_State (N : Node_Id) return Traverse_Result;
3206 -- Attempt to find a construct that denotes a post-state. If this is
3207 -- the case, set flag Post_State_Seen.
3209 -------------------
3210 -- Is_Post_State --
3211 -------------------
3213 function Is_Post_State (N : Node_Id) return Traverse_Result is
3214 Ent : Entity_Id;
3216 begin
3217 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3218 Post_State_Seen := True;
3219 return Abandon;
3221 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3222 Ent := Entity (N);
3224 -- The entity may be modifiable through an implicit dereference
3226 if No (Ent)
3227 or else Ekind (Ent) in Assignable_Kind
3228 or else (Is_Access_Type (Etype (Ent))
3229 and then Nkind (Parent (N)) = N_Selected_Component)
3230 then
3231 Post_State_Seen := True;
3232 return Abandon;
3233 end if;
3235 elsif Nkind (N) = N_Attribute_Reference then
3236 if Attribute_Name (N) = Name_Old then
3237 return Skip;
3239 elsif Attribute_Name (N) = Name_Result then
3240 Post_State_Seen := True;
3241 return Abandon;
3242 end if;
3243 end if;
3245 return OK;
3246 end Is_Post_State;
3248 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3250 -- Start of processing for Mentions_Post_State
3252 begin
3253 Find_Post_State (N);
3255 return Post_State_Seen;
3256 end Mentions_Post_State;
3258 -- Local variables
3260 Expr : constant Node_Id :=
3261 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
3262 Nam : constant Name_Id := Pragma_Name (Prag);
3263 CCase : Node_Id;
3265 -- Start of processing for Check_Result_And_Post_State
3267 begin
3268 -- Examine all consequences
3270 if Nam = Name_Contract_Cases then
3271 CCase := First (Component_Associations (Expr));
3272 while Present (CCase) loop
3273 Check_Expression (Expression (CCase));
3275 Next (CCase);
3276 end loop;
3278 -- Examine the expression of a postcondition
3280 else pragma Assert (Nam_In (Nam, Name_Postcondition, Name_Refined_Post));
3281 Check_Expression (Expr);
3282 end if;
3283 end Check_Result_And_Post_State;
3285 ------------------------------
3286 -- Check_Unprotected_Access --
3287 ------------------------------
3289 procedure Check_Unprotected_Access
3290 (Context : Node_Id;
3291 Expr : Node_Id)
3293 Cont_Encl_Typ : Entity_Id;
3294 Pref_Encl_Typ : Entity_Id;
3296 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3297 -- Check whether Obj is a private component of a protected object.
3298 -- Return the protected type where the component resides, Empty
3299 -- otherwise.
3301 function Is_Public_Operation return Boolean;
3302 -- Verify that the enclosing operation is callable from outside the
3303 -- protected object, to minimize false positives.
3305 ------------------------------
3306 -- Enclosing_Protected_Type --
3307 ------------------------------
3309 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3310 begin
3311 if Is_Entity_Name (Obj) then
3312 declare
3313 Ent : Entity_Id := Entity (Obj);
3315 begin
3316 -- The object can be a renaming of a private component, use
3317 -- the original record component.
3319 if Is_Prival (Ent) then
3320 Ent := Prival_Link (Ent);
3321 end if;
3323 if Is_Protected_Type (Scope (Ent)) then
3324 return Scope (Ent);
3325 end if;
3326 end;
3327 end if;
3329 -- For indexed and selected components, recursively check the prefix
3331 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3332 return Enclosing_Protected_Type (Prefix (Obj));
3334 -- The object does not denote a protected component
3336 else
3337 return Empty;
3338 end if;
3339 end Enclosing_Protected_Type;
3341 -------------------------
3342 -- Is_Public_Operation --
3343 -------------------------
3345 function Is_Public_Operation return Boolean is
3346 S : Entity_Id;
3347 E : Entity_Id;
3349 begin
3350 S := Current_Scope;
3351 while Present (S) and then S /= Pref_Encl_Typ loop
3352 if Scope (S) = Pref_Encl_Typ then
3353 E := First_Entity (Pref_Encl_Typ);
3354 while Present (E)
3355 and then E /= First_Private_Entity (Pref_Encl_Typ)
3356 loop
3357 if E = S then
3358 return True;
3359 end if;
3361 Next_Entity (E);
3362 end loop;
3363 end if;
3365 S := Scope (S);
3366 end loop;
3368 return False;
3369 end Is_Public_Operation;
3371 -- Start of processing for Check_Unprotected_Access
3373 begin
3374 if Nkind (Expr) = N_Attribute_Reference
3375 and then Attribute_Name (Expr) = Name_Unchecked_Access
3376 then
3377 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3378 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3380 -- Check whether we are trying to export a protected component to a
3381 -- context with an equal or lower access level.
3383 if Present (Pref_Encl_Typ)
3384 and then No (Cont_Encl_Typ)
3385 and then Is_Public_Operation
3386 and then Scope_Depth (Pref_Encl_Typ) >=
3387 Object_Access_Level (Context)
3388 then
3389 Error_Msg_N
3390 ("??possible unprotected access to protected data", Expr);
3391 end if;
3392 end if;
3393 end Check_Unprotected_Access;
3395 ------------------------
3396 -- Collect_Interfaces --
3397 ------------------------
3399 procedure Collect_Interfaces
3400 (T : Entity_Id;
3401 Ifaces_List : out Elist_Id;
3402 Exclude_Parents : Boolean := False;
3403 Use_Full_View : Boolean := True)
3405 procedure Collect (Typ : Entity_Id);
3406 -- Subsidiary subprogram used to traverse the whole list
3407 -- of directly and indirectly implemented interfaces
3409 -------------
3410 -- Collect --
3411 -------------
3413 procedure Collect (Typ : Entity_Id) is
3414 Ancestor : Entity_Id;
3415 Full_T : Entity_Id;
3416 Id : Node_Id;
3417 Iface : Entity_Id;
3419 begin
3420 Full_T := Typ;
3422 -- Handle private types
3424 if Use_Full_View
3425 and then Is_Private_Type (Typ)
3426 and then Present (Full_View (Typ))
3427 then
3428 Full_T := Full_View (Typ);
3429 end if;
3431 -- Include the ancestor if we are generating the whole list of
3432 -- abstract interfaces.
3434 if Etype (Full_T) /= Typ
3436 -- Protect the frontend against wrong sources. For example:
3438 -- package P is
3439 -- type A is tagged null record;
3440 -- type B is new A with private;
3441 -- type C is new A with private;
3442 -- private
3443 -- type B is new C with null record;
3444 -- type C is new B with null record;
3445 -- end P;
3447 and then Etype (Full_T) /= T
3448 then
3449 Ancestor := Etype (Full_T);
3450 Collect (Ancestor);
3452 if Is_Interface (Ancestor) and then not Exclude_Parents then
3453 Append_Unique_Elmt (Ancestor, Ifaces_List);
3454 end if;
3455 end if;
3457 -- Traverse the graph of ancestor interfaces
3459 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
3460 Id := First (Abstract_Interface_List (Full_T));
3461 while Present (Id) loop
3462 Iface := Etype (Id);
3464 -- Protect against wrong uses. For example:
3465 -- type I is interface;
3466 -- type O is tagged null record;
3467 -- type Wrong is new I and O with null record; -- ERROR
3469 if Is_Interface (Iface) then
3470 if Exclude_Parents
3471 and then Etype (T) /= T
3472 and then Interface_Present_In_Ancestor (Etype (T), Iface)
3473 then
3474 null;
3475 else
3476 Collect (Iface);
3477 Append_Unique_Elmt (Iface, Ifaces_List);
3478 end if;
3479 end if;
3481 Next (Id);
3482 end loop;
3483 end if;
3484 end Collect;
3486 -- Start of processing for Collect_Interfaces
3488 begin
3489 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
3490 Ifaces_List := New_Elmt_List;
3491 Collect (T);
3492 end Collect_Interfaces;
3494 ----------------------------------
3495 -- Collect_Interface_Components --
3496 ----------------------------------
3498 procedure Collect_Interface_Components
3499 (Tagged_Type : Entity_Id;
3500 Components_List : out Elist_Id)
3502 procedure Collect (Typ : Entity_Id);
3503 -- Subsidiary subprogram used to climb to the parents
3505 -------------
3506 -- Collect --
3507 -------------
3509 procedure Collect (Typ : Entity_Id) is
3510 Tag_Comp : Entity_Id;
3511 Parent_Typ : Entity_Id;
3513 begin
3514 -- Handle private types
3516 if Present (Full_View (Etype (Typ))) then
3517 Parent_Typ := Full_View (Etype (Typ));
3518 else
3519 Parent_Typ := Etype (Typ);
3520 end if;
3522 if Parent_Typ /= Typ
3524 -- Protect the frontend against wrong sources. For example:
3526 -- package P is
3527 -- type A is tagged null record;
3528 -- type B is new A with private;
3529 -- type C is new A with private;
3530 -- private
3531 -- type B is new C with null record;
3532 -- type C is new B with null record;
3533 -- end P;
3535 and then Parent_Typ /= Tagged_Type
3536 then
3537 Collect (Parent_Typ);
3538 end if;
3540 -- Collect the components containing tags of secondary dispatch
3541 -- tables.
3543 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
3544 while Present (Tag_Comp) loop
3545 pragma Assert (Present (Related_Type (Tag_Comp)));
3546 Append_Elmt (Tag_Comp, Components_List);
3548 Tag_Comp := Next_Tag_Component (Tag_Comp);
3549 end loop;
3550 end Collect;
3552 -- Start of processing for Collect_Interface_Components
3554 begin
3555 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
3556 and then Is_Tagged_Type (Tagged_Type));
3558 Components_List := New_Elmt_List;
3559 Collect (Tagged_Type);
3560 end Collect_Interface_Components;
3562 -----------------------------
3563 -- Collect_Interfaces_Info --
3564 -----------------------------
3566 procedure Collect_Interfaces_Info
3567 (T : Entity_Id;
3568 Ifaces_List : out Elist_Id;
3569 Components_List : out Elist_Id;
3570 Tags_List : out Elist_Id)
3572 Comps_List : Elist_Id;
3573 Comp_Elmt : Elmt_Id;
3574 Comp_Iface : Entity_Id;
3575 Iface_Elmt : Elmt_Id;
3576 Iface : Entity_Id;
3578 function Search_Tag (Iface : Entity_Id) return Entity_Id;
3579 -- Search for the secondary tag associated with the interface type
3580 -- Iface that is implemented by T.
3582 ----------------
3583 -- Search_Tag --
3584 ----------------
3586 function Search_Tag (Iface : Entity_Id) return Entity_Id is
3587 ADT : Elmt_Id;
3588 begin
3589 if not Is_CPP_Class (T) then
3590 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
3591 else
3592 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
3593 end if;
3595 while Present (ADT)
3596 and then Is_Tag (Node (ADT))
3597 and then Related_Type (Node (ADT)) /= Iface
3598 loop
3599 -- Skip secondary dispatch table referencing thunks to user
3600 -- defined primitives covered by this interface.
3602 pragma Assert (Has_Suffix (Node (ADT), 'P'));
3603 Next_Elmt (ADT);
3605 -- Skip secondary dispatch tables of Ada types
3607 if not Is_CPP_Class (T) then
3609 -- Skip secondary dispatch table referencing thunks to
3610 -- predefined primitives.
3612 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
3613 Next_Elmt (ADT);
3615 -- Skip secondary dispatch table referencing user-defined
3616 -- primitives covered by this interface.
3618 pragma Assert (Has_Suffix (Node (ADT), 'D'));
3619 Next_Elmt (ADT);
3621 -- Skip secondary dispatch table referencing predefined
3622 -- primitives.
3624 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
3625 Next_Elmt (ADT);
3626 end if;
3627 end loop;
3629 pragma Assert (Is_Tag (Node (ADT)));
3630 return Node (ADT);
3631 end Search_Tag;
3633 -- Start of processing for Collect_Interfaces_Info
3635 begin
3636 Collect_Interfaces (T, Ifaces_List);
3637 Collect_Interface_Components (T, Comps_List);
3639 -- Search for the record component and tag associated with each
3640 -- interface type of T.
3642 Components_List := New_Elmt_List;
3643 Tags_List := New_Elmt_List;
3645 Iface_Elmt := First_Elmt (Ifaces_List);
3646 while Present (Iface_Elmt) loop
3647 Iface := Node (Iface_Elmt);
3649 -- Associate the primary tag component and the primary dispatch table
3650 -- with all the interfaces that are parents of T
3652 if Is_Ancestor (Iface, T, Use_Full_View => True) then
3653 Append_Elmt (First_Tag_Component (T), Components_List);
3654 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
3656 -- Otherwise search for the tag component and secondary dispatch
3657 -- table of Iface
3659 else
3660 Comp_Elmt := First_Elmt (Comps_List);
3661 while Present (Comp_Elmt) loop
3662 Comp_Iface := Related_Type (Node (Comp_Elmt));
3664 if Comp_Iface = Iface
3665 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
3666 then
3667 Append_Elmt (Node (Comp_Elmt), Components_List);
3668 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
3669 exit;
3670 end if;
3672 Next_Elmt (Comp_Elmt);
3673 end loop;
3674 pragma Assert (Present (Comp_Elmt));
3675 end if;
3677 Next_Elmt (Iface_Elmt);
3678 end loop;
3679 end Collect_Interfaces_Info;
3681 ---------------------
3682 -- Collect_Parents --
3683 ---------------------
3685 procedure Collect_Parents
3686 (T : Entity_Id;
3687 List : out Elist_Id;
3688 Use_Full_View : Boolean := True)
3690 Current_Typ : Entity_Id := T;
3691 Parent_Typ : Entity_Id;
3693 begin
3694 List := New_Elmt_List;
3696 -- No action if the if the type has no parents
3698 if T = Etype (T) then
3699 return;
3700 end if;
3702 loop
3703 Parent_Typ := Etype (Current_Typ);
3705 if Is_Private_Type (Parent_Typ)
3706 and then Present (Full_View (Parent_Typ))
3707 and then Use_Full_View
3708 then
3709 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3710 end if;
3712 Append_Elmt (Parent_Typ, List);
3714 exit when Parent_Typ = Current_Typ;
3715 Current_Typ := Parent_Typ;
3716 end loop;
3717 end Collect_Parents;
3719 ----------------------------------
3720 -- Collect_Primitive_Operations --
3721 ----------------------------------
3723 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
3724 B_Type : constant Entity_Id := Base_Type (T);
3725 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
3726 B_Scope : Entity_Id := Scope (B_Type);
3727 Op_List : Elist_Id;
3728 Formal : Entity_Id;
3729 Is_Prim : Boolean;
3730 Is_Type_In_Pkg : Boolean;
3731 Formal_Derived : Boolean := False;
3732 Id : Entity_Id;
3734 function Match (E : Entity_Id) return Boolean;
3735 -- True if E's base type is B_Type, or E is of an anonymous access type
3736 -- and the base type of its designated type is B_Type.
3738 -----------
3739 -- Match --
3740 -----------
3742 function Match (E : Entity_Id) return Boolean is
3743 Etyp : Entity_Id := Etype (E);
3745 begin
3746 if Ekind (Etyp) = E_Anonymous_Access_Type then
3747 Etyp := Designated_Type (Etyp);
3748 end if;
3750 -- In Ada 2012 a primitive operation may have a formal of an
3751 -- incomplete view of the parent type.
3753 return Base_Type (Etyp) = B_Type
3754 or else
3755 (Ada_Version >= Ada_2012
3756 and then Ekind (Etyp) = E_Incomplete_Type
3757 and then Full_View (Etyp) = B_Type);
3758 end Match;
3760 -- Start of processing for Collect_Primitive_Operations
3762 begin
3763 -- For tagged types, the primitive operations are collected as they
3764 -- are declared, and held in an explicit list which is simply returned.
3766 if Is_Tagged_Type (B_Type) then
3767 return Primitive_Operations (B_Type);
3769 -- An untagged generic type that is a derived type inherits the
3770 -- primitive operations of its parent type. Other formal types only
3771 -- have predefined operators, which are not explicitly represented.
3773 elsif Is_Generic_Type (B_Type) then
3774 if Nkind (B_Decl) = N_Formal_Type_Declaration
3775 and then Nkind (Formal_Type_Definition (B_Decl)) =
3776 N_Formal_Derived_Type_Definition
3777 then
3778 Formal_Derived := True;
3779 else
3780 return New_Elmt_List;
3781 end if;
3782 end if;
3784 Op_List := New_Elmt_List;
3786 if B_Scope = Standard_Standard then
3787 if B_Type = Standard_String then
3788 Append_Elmt (Standard_Op_Concat, Op_List);
3790 elsif B_Type = Standard_Wide_String then
3791 Append_Elmt (Standard_Op_Concatw, Op_List);
3793 else
3794 null;
3795 end if;
3797 -- Locate the primitive subprograms of the type
3799 else
3800 -- The primitive operations appear after the base type, except
3801 -- if the derivation happens within the private part of B_Scope
3802 -- and the type is a private type, in which case both the type
3803 -- and some primitive operations may appear before the base
3804 -- type, and the list of candidates starts after the type.
3806 if In_Open_Scopes (B_Scope)
3807 and then Scope (T) = B_Scope
3808 and then In_Private_Part (B_Scope)
3809 then
3810 Id := Next_Entity (T);
3812 -- In Ada 2012, If the type has an incomplete partial view, there
3813 -- may be primitive operations declared before the full view, so
3814 -- we need to start scanning from the incomplete view, which is
3815 -- earlier on the entity chain.
3817 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
3818 and then Present (Incomplete_View (Parent (B_Type)))
3819 then
3820 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
3822 else
3823 Id := Next_Entity (B_Type);
3824 end if;
3826 -- Set flag if this is a type in a package spec
3828 Is_Type_In_Pkg :=
3829 Is_Package_Or_Generic_Package (B_Scope)
3830 and then
3831 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
3832 N_Package_Body;
3834 while Present (Id) loop
3836 -- Test whether the result type or any of the parameter types of
3837 -- each subprogram following the type match that type when the
3838 -- type is declared in a package spec, is a derived type, or the
3839 -- subprogram is marked as primitive. (The Is_Primitive test is
3840 -- needed to find primitives of nonderived types in declarative
3841 -- parts that happen to override the predefined "=" operator.)
3843 -- Note that generic formal subprograms are not considered to be
3844 -- primitive operations and thus are never inherited.
3846 if Is_Overloadable (Id)
3847 and then (Is_Type_In_Pkg
3848 or else Is_Derived_Type (B_Type)
3849 or else Is_Primitive (Id))
3850 and then Nkind (Parent (Parent (Id)))
3851 not in N_Formal_Subprogram_Declaration
3852 then
3853 Is_Prim := False;
3855 if Match (Id) then
3856 Is_Prim := True;
3858 else
3859 Formal := First_Formal (Id);
3860 while Present (Formal) loop
3861 if Match (Formal) then
3862 Is_Prim := True;
3863 exit;
3864 end if;
3866 Next_Formal (Formal);
3867 end loop;
3868 end if;
3870 -- For a formal derived type, the only primitives are the ones
3871 -- inherited from the parent type. Operations appearing in the
3872 -- package declaration are not primitive for it.
3874 if Is_Prim
3875 and then (not Formal_Derived or else Present (Alias (Id)))
3876 then
3877 -- In the special case of an equality operator aliased to
3878 -- an overriding dispatching equality belonging to the same
3879 -- type, we don't include it in the list of primitives.
3880 -- This avoids inheriting multiple equality operators when
3881 -- deriving from untagged private types whose full type is
3882 -- tagged, which can otherwise cause ambiguities. Note that
3883 -- this should only happen for this kind of untagged parent
3884 -- type, since normally dispatching operations are inherited
3885 -- using the type's Primitive_Operations list.
3887 if Chars (Id) = Name_Op_Eq
3888 and then Is_Dispatching_Operation (Id)
3889 and then Present (Alias (Id))
3890 and then Present (Overridden_Operation (Alias (Id)))
3891 and then Base_Type (Etype (First_Entity (Id))) =
3892 Base_Type (Etype (First_Entity (Alias (Id))))
3893 then
3894 null;
3896 -- Include the subprogram in the list of primitives
3898 else
3899 Append_Elmt (Id, Op_List);
3900 end if;
3901 end if;
3902 end if;
3904 Next_Entity (Id);
3906 -- For a type declared in System, some of its operations may
3907 -- appear in the target-specific extension to System.
3909 if No (Id)
3910 and then B_Scope = RTU_Entity (System)
3911 and then Present_System_Aux
3912 then
3913 B_Scope := System_Aux_Id;
3914 Id := First_Entity (System_Aux_Id);
3915 end if;
3916 end loop;
3917 end if;
3919 return Op_List;
3920 end Collect_Primitive_Operations;
3922 -----------------------------------
3923 -- Compile_Time_Constraint_Error --
3924 -----------------------------------
3926 function Compile_Time_Constraint_Error
3927 (N : Node_Id;
3928 Msg : String;
3929 Ent : Entity_Id := Empty;
3930 Loc : Source_Ptr := No_Location;
3931 Warn : Boolean := False) return Node_Id
3933 Msgc : String (1 .. Msg'Length + 3);
3934 -- Copy of message, with room for possible ?? or << and ! at end
3936 Msgl : Natural;
3937 Wmsg : Boolean;
3938 Eloc : Source_Ptr;
3940 -- Start of processing for Compile_Time_Constraint_Error
3942 begin
3943 -- If this is a warning, convert it into an error if we are in code
3944 -- subject to SPARK_Mode being set ON.
3946 Error_Msg_Warn := SPARK_Mode /= On;
3948 -- A static constraint error in an instance body is not a fatal error.
3949 -- we choose to inhibit the message altogether, because there is no
3950 -- obvious node (for now) on which to post it. On the other hand the
3951 -- offending node must be replaced with a constraint_error in any case.
3953 -- No messages are generated if we already posted an error on this node
3955 if not Error_Posted (N) then
3956 if Loc /= No_Location then
3957 Eloc := Loc;
3958 else
3959 Eloc := Sloc (N);
3960 end if;
3962 -- Copy message to Msgc, converting any ? in the message into
3963 -- < instead, so that we have an error in GNATprove mode.
3965 Msgl := Msg'Length;
3967 for J in 1 .. Msgl loop
3968 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
3969 Msgc (J) := '<';
3970 else
3971 Msgc (J) := Msg (J);
3972 end if;
3973 end loop;
3975 -- Message is a warning, even in Ada 95 case
3977 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
3978 Wmsg := True;
3980 -- In Ada 83, all messages are warnings. In the private part and
3981 -- the body of an instance, constraint_checks are only warnings.
3982 -- We also make this a warning if the Warn parameter is set.
3984 elsif Warn
3985 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
3986 then
3987 Msgl := Msgl + 1;
3988 Msgc (Msgl) := '<';
3989 Msgl := Msgl + 1;
3990 Msgc (Msgl) := '<';
3991 Wmsg := True;
3993 elsif In_Instance_Not_Visible then
3994 Msgl := Msgl + 1;
3995 Msgc (Msgl) := '<';
3996 Msgl := Msgl + 1;
3997 Msgc (Msgl) := '<';
3998 Wmsg := True;
4000 -- Otherwise we have a real error message (Ada 95 static case)
4001 -- and we make this an unconditional message. Note that in the
4002 -- warning case we do not make the message unconditional, it seems
4003 -- quite reasonable to delete messages like this (about exceptions
4004 -- that will be raised) in dead code.
4006 else
4007 Wmsg := False;
4008 Msgl := Msgl + 1;
4009 Msgc (Msgl) := '!';
4010 end if;
4012 -- One more test, skip the warning if the related expression is
4013 -- statically unevaluated, since we don't want to warn about what
4014 -- will happen when something is evaluated if it never will be
4015 -- evaluated.
4017 if not Is_Statically_Unevaluated (N) then
4018 Error_Msg_Warn := SPARK_Mode /= On;
4020 if Present (Ent) then
4021 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4022 else
4023 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4024 end if;
4026 if Wmsg then
4028 -- Check whether the context is an Init_Proc
4030 if Inside_Init_Proc then
4031 declare
4032 Conc_Typ : constant Entity_Id :=
4033 Corresponding_Concurrent_Type
4034 (Entity (Parameter_Type (First
4035 (Parameter_Specifications
4036 (Parent (Current_Scope))))));
4038 begin
4039 -- Don't complain if the corresponding concurrent type
4040 -- doesn't come from source (i.e. a single task/protected
4041 -- object).
4043 if Present (Conc_Typ)
4044 and then not Comes_From_Source (Conc_Typ)
4045 then
4046 Error_Msg_NEL
4047 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4049 else
4050 if GNATprove_Mode then
4051 Error_Msg_NEL
4052 ("\& would have been raised for objects of this "
4053 & "type", N, Standard_Constraint_Error, Eloc);
4054 else
4055 Error_Msg_NEL
4056 ("\& will be raised for objects of this type??",
4057 N, Standard_Constraint_Error, Eloc);
4058 end if;
4059 end if;
4060 end;
4062 else
4063 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4064 end if;
4066 else
4067 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4068 Set_Error_Posted (N);
4069 end if;
4070 end if;
4071 end if;
4073 return N;
4074 end Compile_Time_Constraint_Error;
4076 -----------------------
4077 -- Conditional_Delay --
4078 -----------------------
4080 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4081 begin
4082 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4083 Set_Has_Delayed_Freeze (New_Ent);
4084 end if;
4085 end Conditional_Delay;
4087 ----------------------------
4088 -- Contains_Refined_State --
4089 ----------------------------
4091 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4092 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4093 -- Determine whether a dependency list mentions a state with a visible
4094 -- refinement.
4096 function Has_State_In_Global (List : Node_Id) return Boolean;
4097 -- Determine whether a global list mentions a state with a visible
4098 -- refinement.
4100 function Is_Refined_State (Item : Node_Id) return Boolean;
4101 -- Determine whether Item is a reference to an abstract state with a
4102 -- visible refinement.
4104 -----------------------------
4105 -- Has_State_In_Dependency --
4106 -----------------------------
4108 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4109 Clause : Node_Id;
4110 Output : Node_Id;
4112 begin
4113 -- A null dependency list does not mention any states
4115 if Nkind (List) = N_Null then
4116 return False;
4118 -- Dependency clauses appear as component associations of an
4119 -- aggregate.
4121 elsif Nkind (List) = N_Aggregate
4122 and then Present (Component_Associations (List))
4123 then
4124 Clause := First (Component_Associations (List));
4125 while Present (Clause) loop
4127 -- Inspect the outputs of a dependency clause
4129 Output := First (Choices (Clause));
4130 while Present (Output) loop
4131 if Is_Refined_State (Output) then
4132 return True;
4133 end if;
4135 Next (Output);
4136 end loop;
4138 -- Inspect the outputs of a dependency clause
4140 if Is_Refined_State (Expression (Clause)) then
4141 return True;
4142 end if;
4144 Next (Clause);
4145 end loop;
4147 -- If we get here, then none of the dependency clauses mention a
4148 -- state with visible refinement.
4150 return False;
4152 -- An illegal pragma managed to sneak in
4154 else
4155 raise Program_Error;
4156 end if;
4157 end Has_State_In_Dependency;
4159 -------------------------
4160 -- Has_State_In_Global --
4161 -------------------------
4163 function Has_State_In_Global (List : Node_Id) return Boolean is
4164 Item : Node_Id;
4166 begin
4167 -- A null global list does not mention any states
4169 if Nkind (List) = N_Null then
4170 return False;
4172 -- Simple global list or moded global list declaration
4174 elsif Nkind (List) = N_Aggregate then
4176 -- The declaration of a simple global list appear as a collection
4177 -- of expressions.
4179 if Present (Expressions (List)) then
4180 Item := First (Expressions (List));
4181 while Present (Item) loop
4182 if Is_Refined_State (Item) then
4183 return True;
4184 end if;
4186 Next (Item);
4187 end loop;
4189 -- The declaration of a moded global list appears as a collection
4190 -- of component associations where individual choices denote
4191 -- modes.
4193 else
4194 Item := First (Component_Associations (List));
4195 while Present (Item) loop
4196 if Has_State_In_Global (Expression (Item)) then
4197 return True;
4198 end if;
4200 Next (Item);
4201 end loop;
4202 end if;
4204 -- If we get here, then the simple/moded global list did not
4205 -- mention any states with a visible refinement.
4207 return False;
4209 -- Single global item declaration
4211 elsif Is_Entity_Name (List) then
4212 return Is_Refined_State (List);
4214 -- An illegal pragma managed to sneak in
4216 else
4217 raise Program_Error;
4218 end if;
4219 end Has_State_In_Global;
4221 ----------------------
4222 -- Is_Refined_State --
4223 ----------------------
4225 function Is_Refined_State (Item : Node_Id) return Boolean is
4226 Elmt : Node_Id;
4227 Item_Id : Entity_Id;
4229 begin
4230 if Nkind (Item) = N_Null then
4231 return False;
4233 -- States cannot be subject to attribute 'Result. This case arises
4234 -- in dependency relations.
4236 elsif Nkind (Item) = N_Attribute_Reference
4237 and then Attribute_Name (Item) = Name_Result
4238 then
4239 return False;
4241 -- Multiple items appear as an aggregate. This case arises in
4242 -- dependency relations.
4244 elsif Nkind (Item) = N_Aggregate
4245 and then Present (Expressions (Item))
4246 then
4247 Elmt := First (Expressions (Item));
4248 while Present (Elmt) loop
4249 if Is_Refined_State (Elmt) then
4250 return True;
4251 end if;
4253 Next (Elmt);
4254 end loop;
4256 -- If we get here, then none of the inputs or outputs reference a
4257 -- state with visible refinement.
4259 return False;
4261 -- Single item
4263 else
4264 Item_Id := Entity_Of (Item);
4266 return
4267 Present (Item_Id)
4268 and then Ekind (Item_Id) = E_Abstract_State
4269 and then Has_Visible_Refinement (Item_Id);
4270 end if;
4271 end Is_Refined_State;
4273 -- Local variables
4275 Arg : constant Node_Id :=
4276 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4277 Nam : constant Name_Id := Pragma_Name (Prag);
4279 -- Start of processing for Contains_Refined_State
4281 begin
4282 if Nam = Name_Depends then
4283 return Has_State_In_Dependency (Arg);
4285 else pragma Assert (Nam = Name_Global);
4286 return Has_State_In_Global (Arg);
4287 end if;
4288 end Contains_Refined_State;
4290 -------------------------
4291 -- Copy_Component_List --
4292 -------------------------
4294 function Copy_Component_List
4295 (R_Typ : Entity_Id;
4296 Loc : Source_Ptr) return List_Id
4298 Comp : Node_Id;
4299 Comps : constant List_Id := New_List;
4301 begin
4302 Comp := First_Component (Underlying_Type (R_Typ));
4303 while Present (Comp) loop
4304 if Comes_From_Source (Comp) then
4305 declare
4306 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4307 begin
4308 Append_To (Comps,
4309 Make_Component_Declaration (Loc,
4310 Defining_Identifier =>
4311 Make_Defining_Identifier (Loc, Chars (Comp)),
4312 Component_Definition =>
4313 New_Copy_Tree
4314 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4315 end;
4316 end if;
4318 Next_Component (Comp);
4319 end loop;
4321 return Comps;
4322 end Copy_Component_List;
4324 -------------------------
4325 -- Copy_Parameter_List --
4326 -------------------------
4328 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4329 Loc : constant Source_Ptr := Sloc (Subp_Id);
4330 Plist : List_Id;
4331 Formal : Entity_Id;
4333 begin
4334 if No (First_Formal (Subp_Id)) then
4335 return No_List;
4336 else
4337 Plist := New_List;
4338 Formal := First_Formal (Subp_Id);
4339 while Present (Formal) loop
4340 Append
4341 (Make_Parameter_Specification (Loc,
4342 Defining_Identifier =>
4343 Make_Defining_Identifier (Sloc (Formal),
4344 Chars => Chars (Formal)),
4345 In_Present => In_Present (Parent (Formal)),
4346 Out_Present => Out_Present (Parent (Formal)),
4347 Parameter_Type =>
4348 New_Occurrence_Of (Etype (Formal), Loc),
4349 Expression =>
4350 New_Copy_Tree (Expression (Parent (Formal)))),
4351 Plist);
4353 Next_Formal (Formal);
4354 end loop;
4355 end if;
4357 return Plist;
4358 end Copy_Parameter_List;
4360 --------------------------------
4361 -- Corresponding_Generic_Type --
4362 --------------------------------
4364 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
4365 Inst : Entity_Id;
4366 Gen : Entity_Id;
4367 Typ : Entity_Id;
4369 begin
4370 if not Is_Generic_Actual_Type (T) then
4371 return Any_Type;
4373 -- If the actual is the actual of an enclosing instance, resolution
4374 -- was correct in the generic.
4376 elsif Nkind (Parent (T)) = N_Subtype_Declaration
4377 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
4378 and then
4379 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
4380 then
4381 return Any_Type;
4383 else
4384 Inst := Scope (T);
4386 if Is_Wrapper_Package (Inst) then
4387 Inst := Related_Instance (Inst);
4388 end if;
4390 Gen :=
4391 Generic_Parent
4392 (Specification (Unit_Declaration_Node (Inst)));
4394 -- Generic actual has the same name as the corresponding formal
4396 Typ := First_Entity (Gen);
4397 while Present (Typ) loop
4398 if Chars (Typ) = Chars (T) then
4399 return Typ;
4400 end if;
4402 Next_Entity (Typ);
4403 end loop;
4405 return Any_Type;
4406 end if;
4407 end Corresponding_Generic_Type;
4409 --------------------
4410 -- Current_Entity --
4411 --------------------
4413 -- The currently visible definition for a given identifier is the
4414 -- one most chained at the start of the visibility chain, i.e. the
4415 -- one that is referenced by the Node_Id value of the name of the
4416 -- given identifier.
4418 function Current_Entity (N : Node_Id) return Entity_Id is
4419 begin
4420 return Get_Name_Entity_Id (Chars (N));
4421 end Current_Entity;
4423 -----------------------------
4424 -- Current_Entity_In_Scope --
4425 -----------------------------
4427 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
4428 E : Entity_Id;
4429 CS : constant Entity_Id := Current_Scope;
4431 Transient_Case : constant Boolean := Scope_Is_Transient;
4433 begin
4434 E := Get_Name_Entity_Id (Chars (N));
4435 while Present (E)
4436 and then Scope (E) /= CS
4437 and then (not Transient_Case or else Scope (E) /= Scope (CS))
4438 loop
4439 E := Homonym (E);
4440 end loop;
4442 return E;
4443 end Current_Entity_In_Scope;
4445 -------------------
4446 -- Current_Scope --
4447 -------------------
4449 function Current_Scope return Entity_Id is
4450 begin
4451 if Scope_Stack.Last = -1 then
4452 return Standard_Standard;
4453 else
4454 declare
4455 C : constant Entity_Id :=
4456 Scope_Stack.Table (Scope_Stack.Last).Entity;
4457 begin
4458 if Present (C) then
4459 return C;
4460 else
4461 return Standard_Standard;
4462 end if;
4463 end;
4464 end if;
4465 end Current_Scope;
4467 ------------------------
4468 -- Current_Subprogram --
4469 ------------------------
4471 function Current_Subprogram return Entity_Id is
4472 Scop : constant Entity_Id := Current_Scope;
4473 begin
4474 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
4475 return Scop;
4476 else
4477 return Enclosing_Subprogram (Scop);
4478 end if;
4479 end Current_Subprogram;
4481 ----------------------------------
4482 -- Deepest_Type_Access_Level --
4483 ----------------------------------
4485 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
4486 begin
4487 if Ekind (Typ) = E_Anonymous_Access_Type
4488 and then not Is_Local_Anonymous_Access (Typ)
4489 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
4490 then
4491 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
4492 -- access type.
4494 return
4495 Scope_Depth (Enclosing_Dynamic_Scope
4496 (Defining_Identifier
4497 (Associated_Node_For_Itype (Typ))));
4499 -- For generic formal type, return Int'Last (infinite).
4500 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
4502 elsif Is_Generic_Type (Root_Type (Typ)) then
4503 return UI_From_Int (Int'Last);
4505 else
4506 return Type_Access_Level (Typ);
4507 end if;
4508 end Deepest_Type_Access_Level;
4510 ---------------------
4511 -- Defining_Entity --
4512 ---------------------
4514 function Defining_Entity (N : Node_Id) return Entity_Id is
4515 K : constant Node_Kind := Nkind (N);
4516 Err : Entity_Id := Empty;
4518 begin
4519 case K is
4520 when
4521 N_Subprogram_Declaration |
4522 N_Abstract_Subprogram_Declaration |
4523 N_Subprogram_Body |
4524 N_Package_Declaration |
4525 N_Subprogram_Renaming_Declaration |
4526 N_Subprogram_Body_Stub |
4527 N_Generic_Subprogram_Declaration |
4528 N_Generic_Package_Declaration |
4529 N_Formal_Subprogram_Declaration |
4530 N_Expression_Function
4532 return Defining_Entity (Specification (N));
4534 when
4535 N_Component_Declaration |
4536 N_Defining_Program_Unit_Name |
4537 N_Discriminant_Specification |
4538 N_Entry_Body |
4539 N_Entry_Declaration |
4540 N_Entry_Index_Specification |
4541 N_Exception_Declaration |
4542 N_Exception_Renaming_Declaration |
4543 N_Formal_Object_Declaration |
4544 N_Formal_Package_Declaration |
4545 N_Formal_Type_Declaration |
4546 N_Full_Type_Declaration |
4547 N_Implicit_Label_Declaration |
4548 N_Incomplete_Type_Declaration |
4549 N_Loop_Parameter_Specification |
4550 N_Number_Declaration |
4551 N_Object_Declaration |
4552 N_Object_Renaming_Declaration |
4553 N_Package_Body_Stub |
4554 N_Parameter_Specification |
4555 N_Private_Extension_Declaration |
4556 N_Private_Type_Declaration |
4557 N_Protected_Body |
4558 N_Protected_Body_Stub |
4559 N_Protected_Type_Declaration |
4560 N_Single_Protected_Declaration |
4561 N_Single_Task_Declaration |
4562 N_Subtype_Declaration |
4563 N_Task_Body |
4564 N_Task_Body_Stub |
4565 N_Task_Type_Declaration
4567 return Defining_Identifier (N);
4569 when N_Subunit =>
4570 return Defining_Entity (Proper_Body (N));
4572 when
4573 N_Function_Instantiation |
4574 N_Function_Specification |
4575 N_Generic_Function_Renaming_Declaration |
4576 N_Generic_Package_Renaming_Declaration |
4577 N_Generic_Procedure_Renaming_Declaration |
4578 N_Package_Body |
4579 N_Package_Instantiation |
4580 N_Package_Renaming_Declaration |
4581 N_Package_Specification |
4582 N_Procedure_Instantiation |
4583 N_Procedure_Specification
4585 declare
4586 Nam : constant Node_Id := Defining_Unit_Name (N);
4588 begin
4589 if Nkind (Nam) in N_Entity then
4590 return Nam;
4592 -- For Error, make up a name and attach to declaration
4593 -- so we can continue semantic analysis
4595 elsif Nam = Error then
4596 Err := Make_Temporary (Sloc (N), 'T');
4597 Set_Defining_Unit_Name (N, Err);
4599 return Err;
4601 -- If not an entity, get defining identifier
4603 else
4604 return Defining_Identifier (Nam);
4605 end if;
4606 end;
4608 when
4609 N_Block_Statement |
4610 N_Loop_Statement
4612 return Entity (Identifier (N));
4614 when others =>
4615 raise Program_Error;
4617 end case;
4618 end Defining_Entity;
4620 --------------------------
4621 -- Denotes_Discriminant --
4622 --------------------------
4624 function Denotes_Discriminant
4625 (N : Node_Id;
4626 Check_Concurrent : Boolean := False) return Boolean
4628 E : Entity_Id;
4630 begin
4631 if not Is_Entity_Name (N) or else No (Entity (N)) then
4632 return False;
4633 else
4634 E := Entity (N);
4635 end if;
4637 -- If we are checking for a protected type, the discriminant may have
4638 -- been rewritten as the corresponding discriminal of the original type
4639 -- or of the corresponding concurrent record, depending on whether we
4640 -- are in the spec or body of the protected type.
4642 return Ekind (E) = E_Discriminant
4643 or else
4644 (Check_Concurrent
4645 and then Ekind (E) = E_In_Parameter
4646 and then Present (Discriminal_Link (E))
4647 and then
4648 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
4649 or else
4650 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
4652 end Denotes_Discriminant;
4654 -------------------------
4655 -- Denotes_Same_Object --
4656 -------------------------
4658 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
4659 Obj1 : Node_Id := A1;
4660 Obj2 : Node_Id := A2;
4662 function Has_Prefix (N : Node_Id) return Boolean;
4663 -- Return True if N has attribute Prefix
4665 function Is_Renaming (N : Node_Id) return Boolean;
4666 -- Return true if N names a renaming entity
4668 function Is_Valid_Renaming (N : Node_Id) return Boolean;
4669 -- For renamings, return False if the prefix of any dereference within
4670 -- the renamed object_name is a variable, or any expression within the
4671 -- renamed object_name contains references to variables or calls on
4672 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
4674 ----------------
4675 -- Has_Prefix --
4676 ----------------
4678 function Has_Prefix (N : Node_Id) return Boolean is
4679 begin
4680 return
4681 Nkind_In (N,
4682 N_Attribute_Reference,
4683 N_Expanded_Name,
4684 N_Explicit_Dereference,
4685 N_Indexed_Component,
4686 N_Reference,
4687 N_Selected_Component,
4688 N_Slice);
4689 end Has_Prefix;
4691 -----------------
4692 -- Is_Renaming --
4693 -----------------
4695 function Is_Renaming (N : Node_Id) return Boolean is
4696 begin
4697 return Is_Entity_Name (N)
4698 and then Present (Renamed_Entity (Entity (N)));
4699 end Is_Renaming;
4701 -----------------------
4702 -- Is_Valid_Renaming --
4703 -----------------------
4705 function Is_Valid_Renaming (N : Node_Id) return Boolean is
4707 function Check_Renaming (N : Node_Id) return Boolean;
4708 -- Recursive function used to traverse all the prefixes of N
4710 function Check_Renaming (N : Node_Id) return Boolean is
4711 begin
4712 if Is_Renaming (N)
4713 and then not Check_Renaming (Renamed_Entity (Entity (N)))
4714 then
4715 return False;
4716 end if;
4718 if Nkind (N) = N_Indexed_Component then
4719 declare
4720 Indx : Node_Id;
4722 begin
4723 Indx := First (Expressions (N));
4724 while Present (Indx) loop
4725 if not Is_OK_Static_Expression (Indx) then
4726 return False;
4727 end if;
4729 Next_Index (Indx);
4730 end loop;
4731 end;
4732 end if;
4734 if Has_Prefix (N) then
4735 declare
4736 P : constant Node_Id := Prefix (N);
4738 begin
4739 if Nkind (N) = N_Explicit_Dereference
4740 and then Is_Variable (P)
4741 then
4742 return False;
4744 elsif Is_Entity_Name (P)
4745 and then Ekind (Entity (P)) = E_Function
4746 then
4747 return False;
4749 elsif Nkind (P) = N_Function_Call then
4750 return False;
4751 end if;
4753 -- Recursion to continue traversing the prefix of the
4754 -- renaming expression
4756 return Check_Renaming (P);
4757 end;
4758 end if;
4760 return True;
4761 end Check_Renaming;
4763 -- Start of processing for Is_Valid_Renaming
4765 begin
4766 return Check_Renaming (N);
4767 end Is_Valid_Renaming;
4769 -- Start of processing for Denotes_Same_Object
4771 begin
4772 -- Both names statically denote the same stand-alone object or parameter
4773 -- (RM 6.4.1(6.5/3))
4775 if Is_Entity_Name (Obj1)
4776 and then Is_Entity_Name (Obj2)
4777 and then Entity (Obj1) = Entity (Obj2)
4778 then
4779 return True;
4780 end if;
4782 -- For renamings, the prefix of any dereference within the renamed
4783 -- object_name is not a variable, and any expression within the
4784 -- renamed object_name contains no references to variables nor
4785 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
4787 if Is_Renaming (Obj1) then
4788 if Is_Valid_Renaming (Obj1) then
4789 Obj1 := Renamed_Entity (Entity (Obj1));
4790 else
4791 return False;
4792 end if;
4793 end if;
4795 if Is_Renaming (Obj2) then
4796 if Is_Valid_Renaming (Obj2) then
4797 Obj2 := Renamed_Entity (Entity (Obj2));
4798 else
4799 return False;
4800 end if;
4801 end if;
4803 -- No match if not same node kind (such cases are handled by
4804 -- Denotes_Same_Prefix)
4806 if Nkind (Obj1) /= Nkind (Obj2) then
4807 return False;
4809 -- After handling valid renamings, one of the two names statically
4810 -- denoted a renaming declaration whose renamed object_name is known
4811 -- to denote the same object as the other (RM 6.4.1(6.10/3))
4813 elsif Is_Entity_Name (Obj1) then
4814 if Is_Entity_Name (Obj2) then
4815 return Entity (Obj1) = Entity (Obj2);
4816 else
4817 return False;
4818 end if;
4820 -- Both names are selected_components, their prefixes are known to
4821 -- denote the same object, and their selector_names denote the same
4822 -- component (RM 6.4.1(6.6/3)
4824 elsif Nkind (Obj1) = N_Selected_Component then
4825 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4826 and then
4827 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
4829 -- Both names are dereferences and the dereferenced names are known to
4830 -- denote the same object (RM 6.4.1(6.7/3))
4832 elsif Nkind (Obj1) = N_Explicit_Dereference then
4833 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
4835 -- Both names are indexed_components, their prefixes are known to denote
4836 -- the same object, and each of the pairs of corresponding index values
4837 -- are either both static expressions with the same static value or both
4838 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
4840 elsif Nkind (Obj1) = N_Indexed_Component then
4841 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
4842 return False;
4843 else
4844 declare
4845 Indx1 : Node_Id;
4846 Indx2 : Node_Id;
4848 begin
4849 Indx1 := First (Expressions (Obj1));
4850 Indx2 := First (Expressions (Obj2));
4851 while Present (Indx1) loop
4853 -- Indexes must denote the same static value or same object
4855 if Is_OK_Static_Expression (Indx1) then
4856 if not Is_OK_Static_Expression (Indx2) then
4857 return False;
4859 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
4860 return False;
4861 end if;
4863 elsif not Denotes_Same_Object (Indx1, Indx2) then
4864 return False;
4865 end if;
4867 Next (Indx1);
4868 Next (Indx2);
4869 end loop;
4871 return True;
4872 end;
4873 end if;
4875 -- Both names are slices, their prefixes are known to denote the same
4876 -- object, and the two slices have statically matching index constraints
4877 -- (RM 6.4.1(6.9/3))
4879 elsif Nkind (Obj1) = N_Slice
4880 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4881 then
4882 declare
4883 Lo1, Lo2, Hi1, Hi2 : Node_Id;
4885 begin
4886 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
4887 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
4889 -- Check whether bounds are statically identical. There is no
4890 -- attempt to detect partial overlap of slices.
4892 return Denotes_Same_Object (Lo1, Lo2)
4893 and then
4894 Denotes_Same_Object (Hi1, Hi2);
4895 end;
4897 -- In the recursion, literals appear as indexes
4899 elsif Nkind (Obj1) = N_Integer_Literal
4900 and then
4901 Nkind (Obj2) = N_Integer_Literal
4902 then
4903 return Intval (Obj1) = Intval (Obj2);
4905 else
4906 return False;
4907 end if;
4908 end Denotes_Same_Object;
4910 -------------------------
4911 -- Denotes_Same_Prefix --
4912 -------------------------
4914 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
4916 begin
4917 if Is_Entity_Name (A1) then
4918 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
4919 and then not Is_Access_Type (Etype (A1))
4920 then
4921 return Denotes_Same_Object (A1, Prefix (A2))
4922 or else Denotes_Same_Prefix (A1, Prefix (A2));
4923 else
4924 return False;
4925 end if;
4927 elsif Is_Entity_Name (A2) then
4928 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
4930 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
4931 and then
4932 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
4933 then
4934 declare
4935 Root1, Root2 : Node_Id;
4936 Depth1, Depth2 : Int := 0;
4938 begin
4939 Root1 := Prefix (A1);
4940 while not Is_Entity_Name (Root1) loop
4941 if not Nkind_In
4942 (Root1, N_Selected_Component, N_Indexed_Component)
4943 then
4944 return False;
4945 else
4946 Root1 := Prefix (Root1);
4947 end if;
4949 Depth1 := Depth1 + 1;
4950 end loop;
4952 Root2 := Prefix (A2);
4953 while not Is_Entity_Name (Root2) loop
4954 if not Nkind_In (Root2, N_Selected_Component,
4955 N_Indexed_Component)
4956 then
4957 return False;
4958 else
4959 Root2 := Prefix (Root2);
4960 end if;
4962 Depth2 := Depth2 + 1;
4963 end loop;
4965 -- If both have the same depth and they do not denote the same
4966 -- object, they are disjoint and no warning is needed.
4968 if Depth1 = Depth2 then
4969 return False;
4971 elsif Depth1 > Depth2 then
4972 Root1 := Prefix (A1);
4973 for J in 1 .. Depth1 - Depth2 - 1 loop
4974 Root1 := Prefix (Root1);
4975 end loop;
4977 return Denotes_Same_Object (Root1, A2);
4979 else
4980 Root2 := Prefix (A2);
4981 for J in 1 .. Depth2 - Depth1 - 1 loop
4982 Root2 := Prefix (Root2);
4983 end loop;
4985 return Denotes_Same_Object (A1, Root2);
4986 end if;
4987 end;
4989 else
4990 return False;
4991 end if;
4992 end Denotes_Same_Prefix;
4994 ----------------------
4995 -- Denotes_Variable --
4996 ----------------------
4998 function Denotes_Variable (N : Node_Id) return Boolean is
4999 begin
5000 return Is_Variable (N) and then Paren_Count (N) = 0;
5001 end Denotes_Variable;
5003 -----------------------------
5004 -- Depends_On_Discriminant --
5005 -----------------------------
5007 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5008 L : Node_Id;
5009 H : Node_Id;
5011 begin
5012 Get_Index_Bounds (N, L, H);
5013 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5014 end Depends_On_Discriminant;
5016 -------------------------
5017 -- Designate_Same_Unit --
5018 -------------------------
5020 function Designate_Same_Unit
5021 (Name1 : Node_Id;
5022 Name2 : Node_Id) return Boolean
5024 K1 : constant Node_Kind := Nkind (Name1);
5025 K2 : constant Node_Kind := Nkind (Name2);
5027 function Prefix_Node (N : Node_Id) return Node_Id;
5028 -- Returns the parent unit name node of a defining program unit name
5029 -- or the prefix if N is a selected component or an expanded name.
5031 function Select_Node (N : Node_Id) return Node_Id;
5032 -- Returns the defining identifier node of a defining program unit
5033 -- name or the selector node if N is a selected component or an
5034 -- expanded name.
5036 -----------------
5037 -- Prefix_Node --
5038 -----------------
5040 function Prefix_Node (N : Node_Id) return Node_Id is
5041 begin
5042 if Nkind (N) = N_Defining_Program_Unit_Name then
5043 return Name (N);
5044 else
5045 return Prefix (N);
5046 end if;
5047 end Prefix_Node;
5049 -----------------
5050 -- Select_Node --
5051 -----------------
5053 function Select_Node (N : Node_Id) return Node_Id is
5054 begin
5055 if Nkind (N) = N_Defining_Program_Unit_Name then
5056 return Defining_Identifier (N);
5057 else
5058 return Selector_Name (N);
5059 end if;
5060 end Select_Node;
5062 -- Start of processing for Designate_Next_Unit
5064 begin
5065 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
5066 and then
5067 (K2 = N_Identifier or else K2 = N_Defining_Identifier)
5068 then
5069 return Chars (Name1) = Chars (Name2);
5071 elsif
5072 (K1 = N_Expanded_Name or else
5073 K1 = N_Selected_Component or else
5074 K1 = N_Defining_Program_Unit_Name)
5075 and then
5076 (K2 = N_Expanded_Name or else
5077 K2 = N_Selected_Component or else
5078 K2 = N_Defining_Program_Unit_Name)
5079 then
5080 return
5081 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5082 and then
5083 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5085 else
5086 return False;
5087 end if;
5088 end Designate_Same_Unit;
5090 ------------------------------------------
5091 -- function Dynamic_Accessibility_Level --
5092 ------------------------------------------
5094 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5095 E : Entity_Id;
5096 Loc : constant Source_Ptr := Sloc (Expr);
5098 function Make_Level_Literal (Level : Uint) return Node_Id;
5099 -- Construct an integer literal representing an accessibility level
5100 -- with its type set to Natural.
5102 ------------------------
5103 -- Make_Level_Literal --
5104 ------------------------
5106 function Make_Level_Literal (Level : Uint) return Node_Id is
5107 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5108 begin
5109 Set_Etype (Result, Standard_Natural);
5110 return Result;
5111 end Make_Level_Literal;
5113 -- Start of processing for Dynamic_Accessibility_Level
5115 begin
5116 if Is_Entity_Name (Expr) then
5117 E := Entity (Expr);
5119 if Present (Renamed_Object (E)) then
5120 return Dynamic_Accessibility_Level (Renamed_Object (E));
5121 end if;
5123 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5124 if Present (Extra_Accessibility (E)) then
5125 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5126 end if;
5127 end if;
5128 end if;
5130 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5132 case Nkind (Expr) is
5134 -- For access discriminant, the level of the enclosing object
5136 when N_Selected_Component =>
5137 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5138 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5139 E_Anonymous_Access_Type
5140 then
5141 return Make_Level_Literal (Object_Access_Level (Expr));
5142 end if;
5144 when N_Attribute_Reference =>
5145 case Get_Attribute_Id (Attribute_Name (Expr)) is
5147 -- For X'Access, the level of the prefix X
5149 when Attribute_Access =>
5150 return Make_Level_Literal
5151 (Object_Access_Level (Prefix (Expr)));
5153 -- Treat the unchecked attributes as library-level
5155 when Attribute_Unchecked_Access |
5156 Attribute_Unrestricted_Access =>
5157 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5159 -- No other access-valued attributes
5161 when others =>
5162 raise Program_Error;
5163 end case;
5165 when N_Allocator =>
5167 -- Unimplemented: depends on context. As an actual parameter where
5168 -- formal type is anonymous, use
5169 -- Scope_Depth (Current_Scope) + 1.
5170 -- For other cases, see 3.10.2(14/3) and following. ???
5172 null;
5174 when N_Type_Conversion =>
5175 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5177 -- Handle type conversions introduced for a rename of an
5178 -- Ada 2012 stand-alone object of an anonymous access type.
5180 return Dynamic_Accessibility_Level (Expression (Expr));
5181 end if;
5183 when others =>
5184 null;
5185 end case;
5187 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5188 end Dynamic_Accessibility_Level;
5190 -----------------------------------
5191 -- Effective_Extra_Accessibility --
5192 -----------------------------------
5194 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5195 begin
5196 if Present (Renamed_Object (Id))
5197 and then Is_Entity_Name (Renamed_Object (Id))
5198 then
5199 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5200 else
5201 return Extra_Accessibility (Id);
5202 end if;
5203 end Effective_Extra_Accessibility;
5205 -----------------------------
5206 -- Effective_Reads_Enabled --
5207 -----------------------------
5209 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5210 begin
5211 return Has_Enabled_Property (Id, Name_Effective_Reads);
5212 end Effective_Reads_Enabled;
5214 ------------------------------
5215 -- Effective_Writes_Enabled --
5216 ------------------------------
5218 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5219 begin
5220 return Has_Enabled_Property (Id, Name_Effective_Writes);
5221 end Effective_Writes_Enabled;
5223 ------------------------------
5224 -- Enclosing_Comp_Unit_Node --
5225 ------------------------------
5227 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5228 Current_Node : Node_Id;
5230 begin
5231 Current_Node := N;
5232 while Present (Current_Node)
5233 and then Nkind (Current_Node) /= N_Compilation_Unit
5234 loop
5235 Current_Node := Parent (Current_Node);
5236 end loop;
5238 if Nkind (Current_Node) /= N_Compilation_Unit then
5239 return Empty;
5240 else
5241 return Current_Node;
5242 end if;
5243 end Enclosing_Comp_Unit_Node;
5245 --------------------------
5246 -- Enclosing_CPP_Parent --
5247 --------------------------
5249 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5250 Parent_Typ : Entity_Id := Typ;
5252 begin
5253 while not Is_CPP_Class (Parent_Typ)
5254 and then Etype (Parent_Typ) /= Parent_Typ
5255 loop
5256 Parent_Typ := Etype (Parent_Typ);
5258 if Is_Private_Type (Parent_Typ) then
5259 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5260 end if;
5261 end loop;
5263 pragma Assert (Is_CPP_Class (Parent_Typ));
5264 return Parent_Typ;
5265 end Enclosing_CPP_Parent;
5267 ----------------------------
5268 -- Enclosing_Generic_Body --
5269 ----------------------------
5271 function Enclosing_Generic_Body
5272 (N : Node_Id) return Node_Id
5274 P : Node_Id;
5275 Decl : Node_Id;
5276 Spec : Node_Id;
5278 begin
5279 P := Parent (N);
5280 while Present (P) loop
5281 if Nkind (P) = N_Package_Body
5282 or else Nkind (P) = N_Subprogram_Body
5283 then
5284 Spec := Corresponding_Spec (P);
5286 if Present (Spec) then
5287 Decl := Unit_Declaration_Node (Spec);
5289 if Nkind (Decl) = N_Generic_Package_Declaration
5290 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5291 then
5292 return P;
5293 end if;
5294 end if;
5295 end if;
5297 P := Parent (P);
5298 end loop;
5300 return Empty;
5301 end Enclosing_Generic_Body;
5303 ----------------------------
5304 -- Enclosing_Generic_Unit --
5305 ----------------------------
5307 function Enclosing_Generic_Unit
5308 (N : Node_Id) return Node_Id
5310 P : Node_Id;
5311 Decl : Node_Id;
5312 Spec : Node_Id;
5314 begin
5315 P := Parent (N);
5316 while Present (P) loop
5317 if Nkind (P) = N_Generic_Package_Declaration
5318 or else Nkind (P) = N_Generic_Subprogram_Declaration
5319 then
5320 return P;
5322 elsif Nkind (P) = N_Package_Body
5323 or else Nkind (P) = N_Subprogram_Body
5324 then
5325 Spec := Corresponding_Spec (P);
5327 if Present (Spec) then
5328 Decl := Unit_Declaration_Node (Spec);
5330 if Nkind (Decl) = N_Generic_Package_Declaration
5331 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5332 then
5333 return Decl;
5334 end if;
5335 end if;
5336 end if;
5338 P := Parent (P);
5339 end loop;
5341 return Empty;
5342 end Enclosing_Generic_Unit;
5344 -------------------------------
5345 -- Enclosing_Lib_Unit_Entity --
5346 -------------------------------
5348 function Enclosing_Lib_Unit_Entity
5349 (E : Entity_Id := Current_Scope) return Entity_Id
5351 Unit_Entity : Entity_Id;
5353 begin
5354 -- Look for enclosing library unit entity by following scope links.
5355 -- Equivalent to, but faster than indexing through the scope stack.
5357 Unit_Entity := E;
5358 while (Present (Scope (Unit_Entity))
5359 and then Scope (Unit_Entity) /= Standard_Standard)
5360 and not Is_Child_Unit (Unit_Entity)
5361 loop
5362 Unit_Entity := Scope (Unit_Entity);
5363 end loop;
5365 return Unit_Entity;
5366 end Enclosing_Lib_Unit_Entity;
5368 -----------------------
5369 -- Enclosing_Package --
5370 -----------------------
5372 function Enclosing_Package (E : Entity_Id) return Entity_Id is
5373 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5375 begin
5376 if Dynamic_Scope = Standard_Standard then
5377 return Standard_Standard;
5379 elsif Dynamic_Scope = Empty then
5380 return Empty;
5382 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
5383 E_Generic_Package)
5384 then
5385 return Dynamic_Scope;
5387 else
5388 return Enclosing_Package (Dynamic_Scope);
5389 end if;
5390 end Enclosing_Package;
5392 --------------------------
5393 -- Enclosing_Subprogram --
5394 --------------------------
5396 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
5397 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5399 begin
5400 if Dynamic_Scope = Standard_Standard then
5401 return Empty;
5403 elsif Dynamic_Scope = Empty then
5404 return Empty;
5406 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
5407 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
5409 elsif Ekind (Dynamic_Scope) = E_Block
5410 or else Ekind (Dynamic_Scope) = E_Return_Statement
5411 then
5412 return Enclosing_Subprogram (Dynamic_Scope);
5414 elsif Ekind (Dynamic_Scope) = E_Task_Type then
5415 return Get_Task_Body_Procedure (Dynamic_Scope);
5417 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
5418 and then Present (Full_View (Dynamic_Scope))
5419 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
5420 then
5421 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
5423 -- No body is generated if the protected operation is eliminated
5425 elsif Convention (Dynamic_Scope) = Convention_Protected
5426 and then not Is_Eliminated (Dynamic_Scope)
5427 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
5428 then
5429 return Protected_Body_Subprogram (Dynamic_Scope);
5431 else
5432 return Dynamic_Scope;
5433 end if;
5434 end Enclosing_Subprogram;
5436 ------------------------
5437 -- Ensure_Freeze_Node --
5438 ------------------------
5440 procedure Ensure_Freeze_Node (E : Entity_Id) is
5441 FN : Node_Id;
5442 begin
5443 if No (Freeze_Node (E)) then
5444 FN := Make_Freeze_Entity (Sloc (E));
5445 Set_Has_Delayed_Freeze (E);
5446 Set_Freeze_Node (E, FN);
5447 Set_Access_Types_To_Process (FN, No_Elist);
5448 Set_TSS_Elist (FN, No_Elist);
5449 Set_Entity (FN, E);
5450 end if;
5451 end Ensure_Freeze_Node;
5453 ----------------
5454 -- Enter_Name --
5455 ----------------
5457 procedure Enter_Name (Def_Id : Entity_Id) is
5458 C : constant Entity_Id := Current_Entity (Def_Id);
5459 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
5460 S : constant Entity_Id := Current_Scope;
5462 begin
5463 Generate_Definition (Def_Id);
5465 -- Add new name to current scope declarations. Check for duplicate
5466 -- declaration, which may or may not be a genuine error.
5468 if Present (E) then
5470 -- Case of previous entity entered because of a missing declaration
5471 -- or else a bad subtype indication. Best is to use the new entity,
5472 -- and make the previous one invisible.
5474 if Etype (E) = Any_Type then
5475 Set_Is_Immediately_Visible (E, False);
5477 -- Case of renaming declaration constructed for package instances.
5478 -- if there is an explicit declaration with the same identifier,
5479 -- the renaming is not immediately visible any longer, but remains
5480 -- visible through selected component notation.
5482 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
5483 and then not Comes_From_Source (E)
5484 then
5485 Set_Is_Immediately_Visible (E, False);
5487 -- The new entity may be the package renaming, which has the same
5488 -- same name as a generic formal which has been seen already.
5490 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
5491 and then not Comes_From_Source (Def_Id)
5492 then
5493 Set_Is_Immediately_Visible (E, False);
5495 -- For a fat pointer corresponding to a remote access to subprogram,
5496 -- we use the same identifier as the RAS type, so that the proper
5497 -- name appears in the stub. This type is only retrieved through
5498 -- the RAS type and never by visibility, and is not added to the
5499 -- visibility list (see below).
5501 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
5502 and then Ekind (Def_Id) = E_Record_Type
5503 and then Present (Corresponding_Remote_Type (Def_Id))
5504 then
5505 null;
5507 -- Case of an implicit operation or derived literal. The new entity
5508 -- hides the implicit one, which is removed from all visibility,
5509 -- i.e. the entity list of its scope, and homonym chain of its name.
5511 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
5512 or else Is_Internal (E)
5513 then
5514 declare
5515 Prev : Entity_Id;
5516 Prev_Vis : Entity_Id;
5517 Decl : constant Node_Id := Parent (E);
5519 begin
5520 -- If E is an implicit declaration, it cannot be the first
5521 -- entity in the scope.
5523 Prev := First_Entity (Current_Scope);
5524 while Present (Prev) and then Next_Entity (Prev) /= E loop
5525 Next_Entity (Prev);
5526 end loop;
5528 if No (Prev) then
5530 -- If E is not on the entity chain of the current scope,
5531 -- it is an implicit declaration in the generic formal
5532 -- part of a generic subprogram. When analyzing the body,
5533 -- the generic formals are visible but not on the entity
5534 -- chain of the subprogram. The new entity will become
5535 -- the visible one in the body.
5537 pragma Assert
5538 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
5539 null;
5541 else
5542 Set_Next_Entity (Prev, Next_Entity (E));
5544 if No (Next_Entity (Prev)) then
5545 Set_Last_Entity (Current_Scope, Prev);
5546 end if;
5548 if E = Current_Entity (E) then
5549 Prev_Vis := Empty;
5551 else
5552 Prev_Vis := Current_Entity (E);
5553 while Homonym (Prev_Vis) /= E loop
5554 Prev_Vis := Homonym (Prev_Vis);
5555 end loop;
5556 end if;
5558 if Present (Prev_Vis) then
5560 -- Skip E in the visibility chain
5562 Set_Homonym (Prev_Vis, Homonym (E));
5564 else
5565 Set_Name_Entity_Id (Chars (E), Homonym (E));
5566 end if;
5567 end if;
5568 end;
5570 -- This section of code could use a comment ???
5572 elsif Present (Etype (E))
5573 and then Is_Concurrent_Type (Etype (E))
5574 and then E = Def_Id
5575 then
5576 return;
5578 -- If the homograph is a protected component renaming, it should not
5579 -- be hiding the current entity. Such renamings are treated as weak
5580 -- declarations.
5582 elsif Is_Prival (E) then
5583 Set_Is_Immediately_Visible (E, False);
5585 -- In this case the current entity is a protected component renaming.
5586 -- Perform minimal decoration by setting the scope and return since
5587 -- the prival should not be hiding other visible entities.
5589 elsif Is_Prival (Def_Id) then
5590 Set_Scope (Def_Id, Current_Scope);
5591 return;
5593 -- Analogous to privals, the discriminal generated for an entry index
5594 -- parameter acts as a weak declaration. Perform minimal decoration
5595 -- to avoid bogus errors.
5597 elsif Is_Discriminal (Def_Id)
5598 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
5599 then
5600 Set_Scope (Def_Id, Current_Scope);
5601 return;
5603 -- In the body or private part of an instance, a type extension may
5604 -- introduce a component with the same name as that of an actual. The
5605 -- legality rule is not enforced, but the semantics of the full type
5606 -- with two components of same name are not clear at this point???
5608 elsif In_Instance_Not_Visible then
5609 null;
5611 -- When compiling a package body, some child units may have become
5612 -- visible. They cannot conflict with local entities that hide them.
5614 elsif Is_Child_Unit (E)
5615 and then In_Open_Scopes (Scope (E))
5616 and then not Is_Immediately_Visible (E)
5617 then
5618 null;
5620 -- Conversely, with front-end inlining we may compile the parent body
5621 -- first, and a child unit subsequently. The context is now the
5622 -- parent spec, and body entities are not visible.
5624 elsif Is_Child_Unit (Def_Id)
5625 and then Is_Package_Body_Entity (E)
5626 and then not In_Package_Body (Current_Scope)
5627 then
5628 null;
5630 -- Case of genuine duplicate declaration
5632 else
5633 Error_Msg_Sloc := Sloc (E);
5635 -- If the previous declaration is an incomplete type declaration
5636 -- this may be an attempt to complete it with a private type. The
5637 -- following avoids confusing cascaded errors.
5639 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
5640 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
5641 then
5642 Error_Msg_N
5643 ("incomplete type cannot be completed with a private " &
5644 "declaration", Parent (Def_Id));
5645 Set_Is_Immediately_Visible (E, False);
5646 Set_Full_View (E, Def_Id);
5648 -- An inherited component of a record conflicts with a new
5649 -- discriminant. The discriminant is inserted first in the scope,
5650 -- but the error should be posted on it, not on the component.
5652 elsif Ekind (E) = E_Discriminant
5653 and then Present (Scope (Def_Id))
5654 and then Scope (Def_Id) /= Current_Scope
5655 then
5656 Error_Msg_Sloc := Sloc (Def_Id);
5657 Error_Msg_N ("& conflicts with declaration#", E);
5658 return;
5660 -- If the name of the unit appears in its own context clause, a
5661 -- dummy package with the name has already been created, and the
5662 -- error emitted. Try to continue quietly.
5664 elsif Error_Posted (E)
5665 and then Sloc (E) = No_Location
5666 and then Nkind (Parent (E)) = N_Package_Specification
5667 and then Current_Scope = Standard_Standard
5668 then
5669 Set_Scope (Def_Id, Current_Scope);
5670 return;
5672 else
5673 Error_Msg_N ("& conflicts with declaration#", Def_Id);
5675 -- Avoid cascaded messages with duplicate components in
5676 -- derived types.
5678 if Ekind_In (E, E_Component, E_Discriminant) then
5679 return;
5680 end if;
5681 end if;
5683 if Nkind (Parent (Parent (Def_Id))) =
5684 N_Generic_Subprogram_Declaration
5685 and then Def_Id =
5686 Defining_Entity (Specification (Parent (Parent (Def_Id))))
5687 then
5688 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
5689 end if;
5691 -- If entity is in standard, then we are in trouble, because it
5692 -- means that we have a library package with a duplicated name.
5693 -- That's hard to recover from, so abort.
5695 if S = Standard_Standard then
5696 raise Unrecoverable_Error;
5698 -- Otherwise we continue with the declaration. Having two
5699 -- identical declarations should not cause us too much trouble.
5701 else
5702 null;
5703 end if;
5704 end if;
5705 end if;
5707 -- If we fall through, declaration is OK, at least OK enough to continue
5709 -- If Def_Id is a discriminant or a record component we are in the midst
5710 -- of inheriting components in a derived record definition. Preserve
5711 -- their Ekind and Etype.
5713 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
5714 null;
5716 -- If a type is already set, leave it alone (happens when a type
5717 -- declaration is reanalyzed following a call to the optimizer).
5719 elsif Present (Etype (Def_Id)) then
5720 null;
5722 -- Otherwise, the kind E_Void insures that premature uses of the entity
5723 -- will be detected. Any_Type insures that no cascaded errors will occur
5725 else
5726 Set_Ekind (Def_Id, E_Void);
5727 Set_Etype (Def_Id, Any_Type);
5728 end if;
5730 -- Inherited discriminants and components in derived record types are
5731 -- immediately visible. Itypes are not.
5733 -- Unless the Itype is for a record type with a corresponding remote
5734 -- type (what is that about, it was not commented ???)
5736 if Ekind_In (Def_Id, E_Discriminant, E_Component)
5737 or else
5738 ((not Is_Record_Type (Def_Id)
5739 or else No (Corresponding_Remote_Type (Def_Id)))
5740 and then not Is_Itype (Def_Id))
5741 then
5742 Set_Is_Immediately_Visible (Def_Id);
5743 Set_Current_Entity (Def_Id);
5744 end if;
5746 Set_Homonym (Def_Id, C);
5747 Append_Entity (Def_Id, S);
5748 Set_Public_Status (Def_Id);
5750 -- Declaring a homonym is not allowed in SPARK ...
5752 if Present (C) and then Restriction_Check_Required (SPARK_05) then
5753 declare
5754 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
5755 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
5756 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
5758 begin
5759 -- ... unless the new declaration is in a subprogram, and the
5760 -- visible declaration is a variable declaration or a parameter
5761 -- specification outside that subprogram.
5763 if Present (Enclosing_Subp)
5764 and then Nkind_In (Parent (C), N_Object_Declaration,
5765 N_Parameter_Specification)
5766 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
5767 then
5768 null;
5770 -- ... or the new declaration is in a package, and the visible
5771 -- declaration occurs outside that package.
5773 elsif Present (Enclosing_Pack)
5774 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
5775 then
5776 null;
5778 -- ... or the new declaration is a component declaration in a
5779 -- record type definition.
5781 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
5782 null;
5784 -- Don't issue error for non-source entities
5786 elsif Comes_From_Source (Def_Id)
5787 and then Comes_From_Source (C)
5788 then
5789 Error_Msg_Sloc := Sloc (C);
5790 Check_SPARK_05_Restriction
5791 ("redeclaration of identifier &#", Def_Id);
5792 end if;
5793 end;
5794 end if;
5796 -- Warn if new entity hides an old one
5798 if Warn_On_Hiding and then Present (C)
5800 -- Don't warn for record components since they always have a well
5801 -- defined scope which does not confuse other uses. Note that in
5802 -- some cases, Ekind has not been set yet.
5804 and then Ekind (C) /= E_Component
5805 and then Ekind (C) /= E_Discriminant
5806 and then Nkind (Parent (C)) /= N_Component_Declaration
5807 and then Ekind (Def_Id) /= E_Component
5808 and then Ekind (Def_Id) /= E_Discriminant
5809 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
5811 -- Don't warn for one character variables. It is too common to use
5812 -- such variables as locals and will just cause too many false hits.
5814 and then Length_Of_Name (Chars (C)) /= 1
5816 -- Don't warn for non-source entities
5818 and then Comes_From_Source (C)
5819 and then Comes_From_Source (Def_Id)
5821 -- Don't warn unless entity in question is in extended main source
5823 and then In_Extended_Main_Source_Unit (Def_Id)
5825 -- Finally, the hidden entity must be either immediately visible or
5826 -- use visible (i.e. from a used package).
5828 and then
5829 (Is_Immediately_Visible (C)
5830 or else
5831 Is_Potentially_Use_Visible (C))
5832 then
5833 Error_Msg_Sloc := Sloc (C);
5834 Error_Msg_N ("declaration hides &#?h?", Def_Id);
5835 end if;
5836 end Enter_Name;
5838 ---------------
5839 -- Entity_Of --
5840 ---------------
5842 function Entity_Of (N : Node_Id) return Entity_Id is
5843 Id : Entity_Id;
5845 begin
5846 Id := Empty;
5848 if Is_Entity_Name (N) then
5849 Id := Entity (N);
5851 -- Follow a possible chain of renamings to reach the root renamed
5852 -- object.
5854 while Present (Id) and then Present (Renamed_Object (Id)) loop
5855 if Is_Entity_Name (Renamed_Object (Id)) then
5856 Id := Entity (Renamed_Object (Id));
5857 else
5858 Id := Empty;
5859 exit;
5860 end if;
5861 end loop;
5862 end if;
5864 return Id;
5865 end Entity_Of;
5867 --------------------------
5868 -- Explain_Limited_Type --
5869 --------------------------
5871 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
5872 C : Entity_Id;
5874 begin
5875 -- For array, component type must be limited
5877 if Is_Array_Type (T) then
5878 Error_Msg_Node_2 := T;
5879 Error_Msg_NE
5880 ("\component type& of type& is limited", N, Component_Type (T));
5881 Explain_Limited_Type (Component_Type (T), N);
5883 elsif Is_Record_Type (T) then
5885 -- No need for extra messages if explicit limited record
5887 if Is_Limited_Record (Base_Type (T)) then
5888 return;
5889 end if;
5891 -- Otherwise find a limited component. Check only components that
5892 -- come from source, or inherited components that appear in the
5893 -- source of the ancestor.
5895 C := First_Component (T);
5896 while Present (C) loop
5897 if Is_Limited_Type (Etype (C))
5898 and then
5899 (Comes_From_Source (C)
5900 or else
5901 (Present (Original_Record_Component (C))
5902 and then
5903 Comes_From_Source (Original_Record_Component (C))))
5904 then
5905 Error_Msg_Node_2 := T;
5906 Error_Msg_NE ("\component& of type& has limited type", N, C);
5907 Explain_Limited_Type (Etype (C), N);
5908 return;
5909 end if;
5911 Next_Component (C);
5912 end loop;
5914 -- The type may be declared explicitly limited, even if no component
5915 -- of it is limited, in which case we fall out of the loop.
5916 return;
5917 end if;
5918 end Explain_Limited_Type;
5920 -------------------------------
5921 -- Extensions_Visible_Status --
5922 -------------------------------
5924 function Extensions_Visible_Status
5925 (Id : Entity_Id) return Extensions_Visible_Mode
5927 Arg1 : Node_Id;
5928 Expr : Node_Id;
5929 Prag : Node_Id;
5930 Subp : Entity_Id;
5932 begin
5933 if SPARK_Mode = On then
5935 -- When a formal parameter is subject to Extensions_Visible, the
5936 -- pragma is stored in the contract of related subprogram.
5938 if Is_Formal (Id) then
5939 Subp := Scope (Id);
5941 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
5942 Subp := Id;
5944 -- No other construct carries this pragma
5946 else
5947 return Extensions_Visible_None;
5948 end if;
5950 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
5952 -- Extract the value from the Boolean expression (if any)
5954 if Present (Prag) then
5955 Arg1 := First (Pragma_Argument_Associations (Prag));
5957 -- The pragma appears with an argument
5959 if Present (Arg1) then
5960 Expr := Get_Pragma_Arg (Arg1);
5962 -- Guarg against cascading errors when the argument of pragma
5963 -- Extensions_Visible is not a valid static Boolean expression.
5965 if Error_Posted (Expr) then
5966 return Extensions_Visible_None;
5968 elsif Is_True (Expr_Value (Expr)) then
5969 return Extensions_Visible_True;
5971 else
5972 return Extensions_Visible_False;
5973 end if;
5975 -- Otherwise the pragma defaults to True
5977 else
5978 return Extensions_Visible_True;
5979 end if;
5981 -- Otherwise pragma Expresions_Visible is not inherited or directly
5982 -- specified, its value defaults to "False".
5984 else
5985 return Extensions_Visible_False;
5986 end if;
5988 -- When SPARK_Mode is disabled, all semantic checks related to pragma
5989 -- Extensions_Visible are disabled as well. Instead of saturating the
5990 -- code with "if SPARK_Mode /= Off then" checks, the predicate returns
5991 -- a default value.
5993 else
5994 return Extensions_Visible_None;
5995 end if;
5996 end Extensions_Visible_Status;
5998 -----------------
5999 -- Find_Actual --
6000 -----------------
6002 procedure Find_Actual
6003 (N : Node_Id;
6004 Formal : out Entity_Id;
6005 Call : out Node_Id)
6007 Parnt : constant Node_Id := Parent (N);
6008 Actual : Node_Id;
6010 begin
6011 if Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6012 and then N = Prefix (Parnt)
6013 then
6014 Find_Actual (Parnt, Formal, Call);
6015 return;
6017 elsif Nkind (Parnt) = N_Parameter_Association
6018 and then N = Explicit_Actual_Parameter (Parnt)
6019 then
6020 Call := Parent (Parnt);
6022 elsif Nkind (Parnt) in N_Subprogram_Call then
6023 Call := Parnt;
6025 else
6026 Formal := Empty;
6027 Call := Empty;
6028 return;
6029 end if;
6031 -- If we have a call to a subprogram look for the parameter. Note that
6032 -- we exclude overloaded calls, since we don't know enough to be sure
6033 -- of giving the right answer in this case.
6035 if Nkind_In (Call, N_Function_Call, N_Procedure_Call_Statement)
6036 and then Is_Entity_Name (Name (Call))
6037 and then Present (Entity (Name (Call)))
6038 and then Is_Overloadable (Entity (Name (Call)))
6039 and then not Is_Overloaded (Name (Call))
6040 then
6041 -- Fall here if we are definitely a parameter
6043 Actual := First_Actual (Call);
6044 Formal := First_Formal (Entity (Name (Call)));
6045 while Present (Formal) and then Present (Actual) loop
6046 if Actual = N then
6047 return;
6049 -- An actual that is the prefix in a prefixed call may have
6050 -- been rewritten in the call, after the deferred reference
6051 -- was collected. Check if sloc and kinds and names match.
6053 elsif Sloc (Actual) = Sloc (N)
6054 and then Nkind (Actual) = N_Identifier
6055 and then Nkind (Actual) = Nkind (N)
6056 and then Chars (Actual) = Chars (N)
6057 then
6058 return;
6060 else
6061 Actual := Next_Actual (Actual);
6062 Formal := Next_Formal (Formal);
6063 end if;
6064 end loop;
6065 end if;
6067 -- Fall through here if we did not find matching actual
6069 Formal := Empty;
6070 Call := Empty;
6071 end Find_Actual;
6073 ---------------------------
6074 -- Find_Body_Discriminal --
6075 ---------------------------
6077 function Find_Body_Discriminal
6078 (Spec_Discriminant : Entity_Id) return Entity_Id
6080 Tsk : Entity_Id;
6081 Disc : Entity_Id;
6083 begin
6084 -- If expansion is suppressed, then the scope can be the concurrent type
6085 -- itself rather than a corresponding concurrent record type.
6087 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6088 Tsk := Scope (Spec_Discriminant);
6090 else
6091 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6093 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6094 end if;
6096 -- Find discriminant of original concurrent type, and use its current
6097 -- discriminal, which is the renaming within the task/protected body.
6099 Disc := First_Discriminant (Tsk);
6100 while Present (Disc) loop
6101 if Chars (Disc) = Chars (Spec_Discriminant) then
6102 return Discriminal (Disc);
6103 end if;
6105 Next_Discriminant (Disc);
6106 end loop;
6108 -- That loop should always succeed in finding a matching entry and
6109 -- returning. Fatal error if not.
6111 raise Program_Error;
6112 end Find_Body_Discriminal;
6114 -------------------------------------
6115 -- Find_Corresponding_Discriminant --
6116 -------------------------------------
6118 function Find_Corresponding_Discriminant
6119 (Id : Node_Id;
6120 Typ : Entity_Id) return Entity_Id
6122 Par_Disc : Entity_Id;
6123 Old_Disc : Entity_Id;
6124 New_Disc : Entity_Id;
6126 begin
6127 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6129 -- The original type may currently be private, and the discriminant
6130 -- only appear on its full view.
6132 if Is_Private_Type (Scope (Par_Disc))
6133 and then not Has_Discriminants (Scope (Par_Disc))
6134 and then Present (Full_View (Scope (Par_Disc)))
6135 then
6136 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6137 else
6138 Old_Disc := First_Discriminant (Scope (Par_Disc));
6139 end if;
6141 if Is_Class_Wide_Type (Typ) then
6142 New_Disc := First_Discriminant (Root_Type (Typ));
6143 else
6144 New_Disc := First_Discriminant (Typ);
6145 end if;
6147 while Present (Old_Disc) and then Present (New_Disc) loop
6148 if Old_Disc = Par_Disc then
6149 return New_Disc;
6150 end if;
6152 Next_Discriminant (Old_Disc);
6153 Next_Discriminant (New_Disc);
6154 end loop;
6156 -- Should always find it
6158 raise Program_Error;
6159 end Find_Corresponding_Discriminant;
6161 ----------------------------------
6162 -- Find_Enclosing_Iterator_Loop --
6163 ----------------------------------
6165 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
6166 Constr : Node_Id;
6167 S : Entity_Id;
6169 begin
6170 -- Traverse the scope chain looking for an iterator loop. Such loops are
6171 -- usually transformed into blocks, hence the use of Original_Node.
6173 S := Id;
6174 while Present (S) and then S /= Standard_Standard loop
6175 if Ekind (S) = E_Loop
6176 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
6177 then
6178 Constr := Original_Node (Label_Construct (Parent (S)));
6180 if Nkind (Constr) = N_Loop_Statement
6181 and then Present (Iteration_Scheme (Constr))
6182 and then Nkind (Iterator_Specification
6183 (Iteration_Scheme (Constr))) =
6184 N_Iterator_Specification
6185 then
6186 return S;
6187 end if;
6188 end if;
6190 S := Scope (S);
6191 end loop;
6193 return Empty;
6194 end Find_Enclosing_Iterator_Loop;
6196 ------------------------------------
6197 -- Find_Loop_In_Conditional_Block --
6198 ------------------------------------
6200 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
6201 Stmt : Node_Id;
6203 begin
6204 Stmt := N;
6206 if Nkind (Stmt) = N_If_Statement then
6207 Stmt := First (Then_Statements (Stmt));
6208 end if;
6210 pragma Assert (Nkind (Stmt) = N_Block_Statement);
6212 -- Inspect the statements of the conditional block. In general the loop
6213 -- should be the first statement in the statement sequence of the block,
6214 -- but the finalization machinery may have introduced extra object
6215 -- declarations.
6217 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
6218 while Present (Stmt) loop
6219 if Nkind (Stmt) = N_Loop_Statement then
6220 return Stmt;
6221 end if;
6223 Next (Stmt);
6224 end loop;
6226 -- The expansion of attribute 'Loop_Entry produced a malformed block
6228 raise Program_Error;
6229 end Find_Loop_In_Conditional_Block;
6231 --------------------------
6232 -- Find_Overlaid_Entity --
6233 --------------------------
6235 procedure Find_Overlaid_Entity
6236 (N : Node_Id;
6237 Ent : out Entity_Id;
6238 Off : out Boolean)
6240 Expr : Node_Id;
6242 begin
6243 -- We are looking for one of the two following forms:
6245 -- for X'Address use Y'Address
6247 -- or
6249 -- Const : constant Address := expr;
6250 -- ...
6251 -- for X'Address use Const;
6253 -- In the second case, the expr is either Y'Address, or recursively a
6254 -- constant that eventually references Y'Address.
6256 Ent := Empty;
6257 Off := False;
6259 if Nkind (N) = N_Attribute_Definition_Clause
6260 and then Chars (N) = Name_Address
6261 then
6262 Expr := Expression (N);
6264 -- This loop checks the form of the expression for Y'Address,
6265 -- using recursion to deal with intermediate constants.
6267 loop
6268 -- Check for Y'Address
6270 if Nkind (Expr) = N_Attribute_Reference
6271 and then Attribute_Name (Expr) = Name_Address
6272 then
6273 Expr := Prefix (Expr);
6274 exit;
6276 -- Check for Const where Const is a constant entity
6278 elsif Is_Entity_Name (Expr)
6279 and then Ekind (Entity (Expr)) = E_Constant
6280 then
6281 Expr := Constant_Value (Entity (Expr));
6283 -- Anything else does not need checking
6285 else
6286 return;
6287 end if;
6288 end loop;
6290 -- This loop checks the form of the prefix for an entity, using
6291 -- recursion to deal with intermediate components.
6293 loop
6294 -- Check for Y where Y is an entity
6296 if Is_Entity_Name (Expr) then
6297 Ent := Entity (Expr);
6298 return;
6300 -- Check for components
6302 elsif
6303 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
6304 then
6305 Expr := Prefix (Expr);
6306 Off := True;
6308 -- Anything else does not need checking
6310 else
6311 return;
6312 end if;
6313 end loop;
6314 end if;
6315 end Find_Overlaid_Entity;
6317 -------------------------
6318 -- Find_Parameter_Type --
6319 -------------------------
6321 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
6322 begin
6323 if Nkind (Param) /= N_Parameter_Specification then
6324 return Empty;
6326 -- For an access parameter, obtain the type from the formal entity
6327 -- itself, because access to subprogram nodes do not carry a type.
6328 -- Shouldn't we always use the formal entity ???
6330 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
6331 return Etype (Defining_Identifier (Param));
6333 else
6334 return Etype (Parameter_Type (Param));
6335 end if;
6336 end Find_Parameter_Type;
6338 -----------------------------------
6339 -- Find_Placement_In_State_Space --
6340 -----------------------------------
6342 procedure Find_Placement_In_State_Space
6343 (Item_Id : Entity_Id;
6344 Placement : out State_Space_Kind;
6345 Pack_Id : out Entity_Id)
6347 Context : Entity_Id;
6349 begin
6350 -- Assume that the item does not appear in the state space of a package
6352 Placement := Not_In_Package;
6353 Pack_Id := Empty;
6355 -- Climb the scope stack and examine the enclosing context
6357 Context := Scope (Item_Id);
6358 while Present (Context) and then Context /= Standard_Standard loop
6359 if Ekind (Context) = E_Package then
6360 Pack_Id := Context;
6362 -- A package body is a cut off point for the traversal as the item
6363 -- cannot be visible to the outside from this point on. Note that
6364 -- this test must be done first as a body is also classified as a
6365 -- private part.
6367 if In_Package_Body (Context) then
6368 Placement := Body_State_Space;
6369 return;
6371 -- The private part of a package is a cut off point for the
6372 -- traversal as the item cannot be visible to the outside from
6373 -- this point on.
6375 elsif In_Private_Part (Context) then
6376 Placement := Private_State_Space;
6377 return;
6379 -- When the item appears in the visible state space of a package,
6380 -- continue to climb the scope stack as this may not be the final
6381 -- state space.
6383 else
6384 Placement := Visible_State_Space;
6386 -- The visible state space of a child unit acts as the proper
6387 -- placement of an item.
6389 if Is_Child_Unit (Context) then
6390 return;
6391 end if;
6392 end if;
6394 -- The item or its enclosing package appear in a construct that has
6395 -- no state space.
6397 else
6398 Placement := Not_In_Package;
6399 return;
6400 end if;
6402 Context := Scope (Context);
6403 end loop;
6404 end Find_Placement_In_State_Space;
6406 ------------------------
6407 -- Find_Specific_Type --
6408 ------------------------
6410 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
6411 Typ : Entity_Id := Root_Type (CW);
6413 begin
6414 if Ekind (Typ) = E_Incomplete_Type then
6415 if From_Limited_With (Typ) then
6416 Typ := Non_Limited_View (Typ);
6417 else
6418 Typ := Full_View (Typ);
6419 end if;
6420 end if;
6422 if Is_Private_Type (Typ)
6423 and then not Is_Tagged_Type (Typ)
6424 and then Present (Full_View (Typ))
6425 then
6426 return Full_View (Typ);
6427 else
6428 return Typ;
6429 end if;
6430 end Find_Specific_Type;
6432 -----------------------------
6433 -- Find_Static_Alternative --
6434 -----------------------------
6436 function Find_Static_Alternative (N : Node_Id) return Node_Id is
6437 Expr : constant Node_Id := Expression (N);
6438 Val : constant Uint := Expr_Value (Expr);
6439 Alt : Node_Id;
6440 Choice : Node_Id;
6442 begin
6443 Alt := First (Alternatives (N));
6445 Search : loop
6446 if Nkind (Alt) /= N_Pragma then
6447 Choice := First (Discrete_Choices (Alt));
6448 while Present (Choice) loop
6450 -- Others choice, always matches
6452 if Nkind (Choice) = N_Others_Choice then
6453 exit Search;
6455 -- Range, check if value is in the range
6457 elsif Nkind (Choice) = N_Range then
6458 exit Search when
6459 Val >= Expr_Value (Low_Bound (Choice))
6460 and then
6461 Val <= Expr_Value (High_Bound (Choice));
6463 -- Choice is a subtype name. Note that we know it must
6464 -- be a static subtype, since otherwise it would have
6465 -- been diagnosed as illegal.
6467 elsif Is_Entity_Name (Choice) and then Is_Type (Entity (Choice))
6468 then
6469 exit Search when Is_In_Range (Expr, Etype (Choice),
6470 Assume_Valid => False);
6472 -- Choice is a subtype indication
6474 elsif Nkind (Choice) = N_Subtype_Indication then
6475 declare
6476 C : constant Node_Id := Constraint (Choice);
6477 R : constant Node_Id := Range_Expression (C);
6479 begin
6480 exit Search when
6481 Val >= Expr_Value (Low_Bound (R))
6482 and then
6483 Val <= Expr_Value (High_Bound (R));
6484 end;
6486 -- Choice is a simple expression
6488 else
6489 exit Search when Val = Expr_Value (Choice);
6490 end if;
6492 Next (Choice);
6493 end loop;
6494 end if;
6496 Next (Alt);
6497 pragma Assert (Present (Alt));
6498 end loop Search;
6500 -- The above loop *must* terminate by finding a match, since
6501 -- we know the case statement is valid, and the value of the
6502 -- expression is known at compile time. When we fall out of
6503 -- the loop, Alt points to the alternative that we know will
6504 -- be selected at run time.
6506 return Alt;
6507 end Find_Static_Alternative;
6509 ------------------
6510 -- First_Actual --
6511 ------------------
6513 function First_Actual (Node : Node_Id) return Node_Id is
6514 N : Node_Id;
6516 begin
6517 if No (Parameter_Associations (Node)) then
6518 return Empty;
6519 end if;
6521 N := First (Parameter_Associations (Node));
6523 if Nkind (N) = N_Parameter_Association then
6524 return First_Named_Actual (Node);
6525 else
6526 return N;
6527 end if;
6528 end First_Actual;
6530 -----------------------
6531 -- Gather_Components --
6532 -----------------------
6534 procedure Gather_Components
6535 (Typ : Entity_Id;
6536 Comp_List : Node_Id;
6537 Governed_By : List_Id;
6538 Into : Elist_Id;
6539 Report_Errors : out Boolean)
6541 Assoc : Node_Id;
6542 Variant : Node_Id;
6543 Discrete_Choice : Node_Id;
6544 Comp_Item : Node_Id;
6546 Discrim : Entity_Id;
6547 Discrim_Name : Node_Id;
6548 Discrim_Value : Node_Id;
6550 begin
6551 Report_Errors := False;
6553 if No (Comp_List) or else Null_Present (Comp_List) then
6554 return;
6556 elsif Present (Component_Items (Comp_List)) then
6557 Comp_Item := First (Component_Items (Comp_List));
6559 else
6560 Comp_Item := Empty;
6561 end if;
6563 while Present (Comp_Item) loop
6565 -- Skip the tag of a tagged record, the interface tags, as well
6566 -- as all items that are not user components (anonymous types,
6567 -- rep clauses, Parent field, controller field).
6569 if Nkind (Comp_Item) = N_Component_Declaration then
6570 declare
6571 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
6572 begin
6573 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
6574 Append_Elmt (Comp, Into);
6575 end if;
6576 end;
6577 end if;
6579 Next (Comp_Item);
6580 end loop;
6582 if No (Variant_Part (Comp_List)) then
6583 return;
6584 else
6585 Discrim_Name := Name (Variant_Part (Comp_List));
6586 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
6587 end if;
6589 -- Look for the discriminant that governs this variant part.
6590 -- The discriminant *must* be in the Governed_By List
6592 Assoc := First (Governed_By);
6593 Find_Constraint : loop
6594 Discrim := First (Choices (Assoc));
6595 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
6596 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
6597 and then
6598 Chars (Corresponding_Discriminant (Entity (Discrim))) =
6599 Chars (Discrim_Name))
6600 or else Chars (Original_Record_Component (Entity (Discrim)))
6601 = Chars (Discrim_Name);
6603 if No (Next (Assoc)) then
6604 if not Is_Constrained (Typ)
6605 and then Is_Derived_Type (Typ)
6606 and then Present (Stored_Constraint (Typ))
6607 then
6608 -- If the type is a tagged type with inherited discriminants,
6609 -- use the stored constraint on the parent in order to find
6610 -- the values of discriminants that are otherwise hidden by an
6611 -- explicit constraint. Renamed discriminants are handled in
6612 -- the code above.
6614 -- If several parent discriminants are renamed by a single
6615 -- discriminant of the derived type, the call to obtain the
6616 -- Corresponding_Discriminant field only retrieves the last
6617 -- of them. We recover the constraint on the others from the
6618 -- Stored_Constraint as well.
6620 declare
6621 D : Entity_Id;
6622 C : Elmt_Id;
6624 begin
6625 D := First_Discriminant (Etype (Typ));
6626 C := First_Elmt (Stored_Constraint (Typ));
6627 while Present (D) and then Present (C) loop
6628 if Chars (Discrim_Name) = Chars (D) then
6629 if Is_Entity_Name (Node (C))
6630 and then Entity (Node (C)) = Entity (Discrim)
6631 then
6632 -- D is renamed by Discrim, whose value is given in
6633 -- Assoc.
6635 null;
6637 else
6638 Assoc :=
6639 Make_Component_Association (Sloc (Typ),
6640 New_List
6641 (New_Occurrence_Of (D, Sloc (Typ))),
6642 Duplicate_Subexpr_No_Checks (Node (C)));
6643 end if;
6644 exit Find_Constraint;
6645 end if;
6647 Next_Discriminant (D);
6648 Next_Elmt (C);
6649 end loop;
6650 end;
6651 end if;
6652 end if;
6654 if No (Next (Assoc)) then
6655 Error_Msg_NE (" missing value for discriminant&",
6656 First (Governed_By), Discrim_Name);
6657 Report_Errors := True;
6658 return;
6659 end if;
6661 Next (Assoc);
6662 end loop Find_Constraint;
6664 Discrim_Value := Expression (Assoc);
6666 if not Is_OK_Static_Expression (Discrim_Value) then
6667 Error_Msg_FE
6668 ("value for discriminant & must be static!",
6669 Discrim_Value, Discrim);
6670 Why_Not_Static (Discrim_Value);
6671 Report_Errors := True;
6672 return;
6673 end if;
6675 Search_For_Discriminant_Value : declare
6676 Low : Node_Id;
6677 High : Node_Id;
6679 UI_High : Uint;
6680 UI_Low : Uint;
6681 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
6683 begin
6684 Find_Discrete_Value : while Present (Variant) loop
6685 Discrete_Choice := First (Discrete_Choices (Variant));
6686 while Present (Discrete_Choice) loop
6687 exit Find_Discrete_Value when
6688 Nkind (Discrete_Choice) = N_Others_Choice;
6690 Get_Index_Bounds (Discrete_Choice, Low, High);
6692 UI_Low := Expr_Value (Low);
6693 UI_High := Expr_Value (High);
6695 exit Find_Discrete_Value when
6696 UI_Low <= UI_Discrim_Value
6697 and then
6698 UI_High >= UI_Discrim_Value;
6700 Next (Discrete_Choice);
6701 end loop;
6703 Next_Non_Pragma (Variant);
6704 end loop Find_Discrete_Value;
6705 end Search_For_Discriminant_Value;
6707 if No (Variant) then
6708 Error_Msg_NE
6709 ("value of discriminant & is out of range", Discrim_Value, Discrim);
6710 Report_Errors := True;
6711 return;
6712 end if;
6714 -- If we have found the corresponding choice, recursively add its
6715 -- components to the Into list.
6717 Gather_Components
6718 (Empty, Component_List (Variant), Governed_By, Into, Report_Errors);
6719 end Gather_Components;
6721 ------------------------
6722 -- Get_Actual_Subtype --
6723 ------------------------
6725 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
6726 Typ : constant Entity_Id := Etype (N);
6727 Utyp : Entity_Id := Underlying_Type (Typ);
6728 Decl : Node_Id;
6729 Atyp : Entity_Id;
6731 begin
6732 if No (Utyp) then
6733 Utyp := Typ;
6734 end if;
6736 -- If what we have is an identifier that references a subprogram
6737 -- formal, or a variable or constant object, then we get the actual
6738 -- subtype from the referenced entity if one has been built.
6740 if Nkind (N) = N_Identifier
6741 and then
6742 (Is_Formal (Entity (N))
6743 or else Ekind (Entity (N)) = E_Constant
6744 or else Ekind (Entity (N)) = E_Variable)
6745 and then Present (Actual_Subtype (Entity (N)))
6746 then
6747 return Actual_Subtype (Entity (N));
6749 -- Actual subtype of unchecked union is always itself. We never need
6750 -- the "real" actual subtype. If we did, we couldn't get it anyway
6751 -- because the discriminant is not available. The restrictions on
6752 -- Unchecked_Union are designed to make sure that this is OK.
6754 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
6755 return Typ;
6757 -- Here for the unconstrained case, we must find actual subtype
6758 -- No actual subtype is available, so we must build it on the fly.
6760 -- Checking the type, not the underlying type, for constrainedness
6761 -- seems to be necessary. Maybe all the tests should be on the type???
6763 elsif (not Is_Constrained (Typ))
6764 and then (Is_Array_Type (Utyp)
6765 or else (Is_Record_Type (Utyp)
6766 and then Has_Discriminants (Utyp)))
6767 and then not Has_Unknown_Discriminants (Utyp)
6768 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
6769 then
6770 -- Nothing to do if in spec expression (why not???)
6772 if In_Spec_Expression then
6773 return Typ;
6775 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
6777 -- If the type has no discriminants, there is no subtype to
6778 -- build, even if the underlying type is discriminated.
6780 return Typ;
6782 -- Else build the actual subtype
6784 else
6785 Decl := Build_Actual_Subtype (Typ, N);
6786 Atyp := Defining_Identifier (Decl);
6788 -- If Build_Actual_Subtype generated a new declaration then use it
6790 if Atyp /= Typ then
6792 -- The actual subtype is an Itype, so analyze the declaration,
6793 -- but do not attach it to the tree, to get the type defined.
6795 Set_Parent (Decl, N);
6796 Set_Is_Itype (Atyp);
6797 Analyze (Decl, Suppress => All_Checks);
6798 Set_Associated_Node_For_Itype (Atyp, N);
6799 Set_Has_Delayed_Freeze (Atyp, False);
6801 -- We need to freeze the actual subtype immediately. This is
6802 -- needed, because otherwise this Itype will not get frozen
6803 -- at all, and it is always safe to freeze on creation because
6804 -- any associated types must be frozen at this point.
6806 Freeze_Itype (Atyp, N);
6807 return Atyp;
6809 -- Otherwise we did not build a declaration, so return original
6811 else
6812 return Typ;
6813 end if;
6814 end if;
6816 -- For all remaining cases, the actual subtype is the same as
6817 -- the nominal type.
6819 else
6820 return Typ;
6821 end if;
6822 end Get_Actual_Subtype;
6824 -------------------------------------
6825 -- Get_Actual_Subtype_If_Available --
6826 -------------------------------------
6828 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
6829 Typ : constant Entity_Id := Etype (N);
6831 begin
6832 -- If what we have is an identifier that references a subprogram
6833 -- formal, or a variable or constant object, then we get the actual
6834 -- subtype from the referenced entity if one has been built.
6836 if Nkind (N) = N_Identifier
6837 and then
6838 (Is_Formal (Entity (N))
6839 or else Ekind (Entity (N)) = E_Constant
6840 or else Ekind (Entity (N)) = E_Variable)
6841 and then Present (Actual_Subtype (Entity (N)))
6842 then
6843 return Actual_Subtype (Entity (N));
6845 -- Otherwise the Etype of N is returned unchanged
6847 else
6848 return Typ;
6849 end if;
6850 end Get_Actual_Subtype_If_Available;
6852 ------------------------
6853 -- Get_Body_From_Stub --
6854 ------------------------
6856 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
6857 begin
6858 return Proper_Body (Unit (Library_Unit (N)));
6859 end Get_Body_From_Stub;
6861 ---------------------
6862 -- Get_Cursor_Type --
6863 ---------------------
6865 function Get_Cursor_Type
6866 (Aspect : Node_Id;
6867 Typ : Entity_Id) return Entity_Id
6869 Assoc : Node_Id;
6870 Func : Entity_Id;
6871 First_Op : Entity_Id;
6872 Cursor : Entity_Id;
6874 begin
6875 -- If error already detected, return
6877 if Error_Posted (Aspect) then
6878 return Any_Type;
6879 end if;
6881 -- The cursor type for an Iterable aspect is the return type of a
6882 -- non-overloaded First primitive operation. Locate association for
6883 -- First.
6885 Assoc := First (Component_Associations (Expression (Aspect)));
6886 First_Op := Any_Id;
6887 while Present (Assoc) loop
6888 if Chars (First (Choices (Assoc))) = Name_First then
6889 First_Op := Expression (Assoc);
6890 exit;
6891 end if;
6893 Next (Assoc);
6894 end loop;
6896 if First_Op = Any_Id then
6897 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
6898 return Any_Type;
6899 end if;
6901 Cursor := Any_Type;
6903 -- Locate function with desired name and profile in scope of type
6905 Func := First_Entity (Scope (Typ));
6906 while Present (Func) loop
6907 if Chars (Func) = Chars (First_Op)
6908 and then Ekind (Func) = E_Function
6909 and then Present (First_Formal (Func))
6910 and then Etype (First_Formal (Func)) = Typ
6911 and then No (Next_Formal (First_Formal (Func)))
6912 then
6913 if Cursor /= Any_Type then
6914 Error_Msg_N
6915 ("Operation First for iterable type must be unique", Aspect);
6916 return Any_Type;
6917 else
6918 Cursor := Etype (Func);
6919 end if;
6920 end if;
6922 Next_Entity (Func);
6923 end loop;
6925 -- If not found, no way to resolve remaining primitives.
6927 if Cursor = Any_Type then
6928 Error_Msg_N
6929 ("No legal primitive operation First for Iterable type", Aspect);
6930 end if;
6932 return Cursor;
6933 end Get_Cursor_Type;
6935 -------------------------------
6936 -- Get_Default_External_Name --
6937 -------------------------------
6939 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
6940 begin
6941 Get_Decoded_Name_String (Chars (E));
6943 if Opt.External_Name_Imp_Casing = Uppercase then
6944 Set_Casing (All_Upper_Case);
6945 else
6946 Set_Casing (All_Lower_Case);
6947 end if;
6949 return
6950 Make_String_Literal (Sloc (E),
6951 Strval => String_From_Name_Buffer);
6952 end Get_Default_External_Name;
6954 --------------------------
6955 -- Get_Enclosing_Object --
6956 --------------------------
6958 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
6959 begin
6960 if Is_Entity_Name (N) then
6961 return Entity (N);
6962 else
6963 case Nkind (N) is
6964 when N_Indexed_Component |
6965 N_Slice |
6966 N_Selected_Component =>
6968 -- If not generating code, a dereference may be left implicit.
6969 -- In thoses cases, return Empty.
6971 if Is_Access_Type (Etype (Prefix (N))) then
6972 return Empty;
6973 else
6974 return Get_Enclosing_Object (Prefix (N));
6975 end if;
6977 when N_Type_Conversion =>
6978 return Get_Enclosing_Object (Expression (N));
6980 when others =>
6981 return Empty;
6982 end case;
6983 end if;
6984 end Get_Enclosing_Object;
6986 ---------------------------
6987 -- Get_Enum_Lit_From_Pos --
6988 ---------------------------
6990 function Get_Enum_Lit_From_Pos
6991 (T : Entity_Id;
6992 Pos : Uint;
6993 Loc : Source_Ptr) return Node_Id
6995 Btyp : Entity_Id := Base_Type (T);
6996 Lit : Node_Id;
6998 begin
6999 -- In the case where the literal is of type Character, Wide_Character
7000 -- or Wide_Wide_Character or of a type derived from them, there needs
7001 -- to be some special handling since there is no explicit chain of
7002 -- literals to search. Instead, an N_Character_Literal node is created
7003 -- with the appropriate Char_Code and Chars fields.
7005 if Is_Standard_Character_Type (T) then
7006 Set_Character_Literal_Name (UI_To_CC (Pos));
7007 return
7008 Make_Character_Literal (Loc,
7009 Chars => Name_Find,
7010 Char_Literal_Value => Pos);
7012 -- For all other cases, we have a complete table of literals, and
7013 -- we simply iterate through the chain of literal until the one
7014 -- with the desired position value is found.
7016 else
7017 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7018 Btyp := Full_View (Btyp);
7019 end if;
7021 Lit := First_Literal (Btyp);
7022 for J in 1 .. UI_To_Int (Pos) loop
7023 Next_Literal (Lit);
7024 end loop;
7026 return New_Occurrence_Of (Lit, Loc);
7027 end if;
7028 end Get_Enum_Lit_From_Pos;
7030 ---------------------------------
7031 -- Get_Ensures_From_CTC_Pragma --
7032 ---------------------------------
7034 function Get_Ensures_From_CTC_Pragma (N : Node_Id) return Node_Id is
7035 Args : constant List_Id := Pragma_Argument_Associations (N);
7036 Res : Node_Id;
7038 begin
7039 if List_Length (Args) = 4 then
7040 Res := Pick (Args, 4);
7042 elsif List_Length (Args) = 3 then
7043 Res := Pick (Args, 3);
7045 if Chars (Res) /= Name_Ensures then
7046 Res := Empty;
7047 end if;
7049 else
7050 Res := Empty;
7051 end if;
7053 return Res;
7054 end Get_Ensures_From_CTC_Pragma;
7056 ------------------------
7057 -- Get_Generic_Entity --
7058 ------------------------
7060 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7061 Ent : constant Entity_Id := Entity (Name (N));
7062 begin
7063 if Present (Renamed_Object (Ent)) then
7064 return Renamed_Object (Ent);
7065 else
7066 return Ent;
7067 end if;
7068 end Get_Generic_Entity;
7070 -------------------------------------
7071 -- Get_Incomplete_View_Of_Ancestor --
7072 -------------------------------------
7074 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7075 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7076 Par_Scope : Entity_Id;
7077 Par_Type : Entity_Id;
7079 begin
7080 -- The incomplete view of an ancestor is only relevant for private
7081 -- derived types in child units.
7083 if not Is_Derived_Type (E)
7084 or else not Is_Child_Unit (Cur_Unit)
7085 then
7086 return Empty;
7088 else
7089 Par_Scope := Scope (Cur_Unit);
7090 if No (Par_Scope) then
7091 return Empty;
7092 end if;
7094 Par_Type := Etype (Base_Type (E));
7096 -- Traverse list of ancestor types until we find one declared in
7097 -- a parent or grandparent unit (two levels seem sufficient).
7099 while Present (Par_Type) loop
7100 if Scope (Par_Type) = Par_Scope
7101 or else Scope (Par_Type) = Scope (Par_Scope)
7102 then
7103 return Par_Type;
7105 elsif not Is_Derived_Type (Par_Type) then
7106 return Empty;
7108 else
7109 Par_Type := Etype (Base_Type (Par_Type));
7110 end if;
7111 end loop;
7113 -- If none found, there is no relevant ancestor type.
7115 return Empty;
7116 end if;
7117 end Get_Incomplete_View_Of_Ancestor;
7119 ----------------------
7120 -- Get_Index_Bounds --
7121 ----------------------
7123 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
7124 Kind : constant Node_Kind := Nkind (N);
7125 R : Node_Id;
7127 begin
7128 if Kind = N_Range then
7129 L := Low_Bound (N);
7130 H := High_Bound (N);
7132 elsif Kind = N_Subtype_Indication then
7133 R := Range_Expression (Constraint (N));
7135 if R = Error then
7136 L := Error;
7137 H := Error;
7138 return;
7140 else
7141 L := Low_Bound (Range_Expression (Constraint (N)));
7142 H := High_Bound (Range_Expression (Constraint (N)));
7143 end if;
7145 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
7146 if Error_Posted (Scalar_Range (Entity (N))) then
7147 L := Error;
7148 H := Error;
7150 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
7151 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
7153 else
7154 L := Low_Bound (Scalar_Range (Entity (N)));
7155 H := High_Bound (Scalar_Range (Entity (N)));
7156 end if;
7158 else
7159 -- N is an expression, indicating a range with one value
7161 L := N;
7162 H := N;
7163 end if;
7164 end Get_Index_Bounds;
7166 ---------------------------------
7167 -- Get_Iterable_Type_Primitive --
7168 ---------------------------------
7170 function Get_Iterable_Type_Primitive
7171 (Typ : Entity_Id;
7172 Nam : Name_Id) return Entity_Id
7174 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
7175 Assoc : Node_Id;
7177 begin
7178 if No (Funcs) then
7179 return Empty;
7181 else
7182 Assoc := First (Component_Associations (Funcs));
7183 while Present (Assoc) loop
7184 if Chars (First (Choices (Assoc))) = Nam then
7185 return Entity (Expression (Assoc));
7186 end if;
7188 Assoc := Next (Assoc);
7189 end loop;
7191 return Empty;
7192 end if;
7193 end Get_Iterable_Type_Primitive;
7195 ----------------------------------
7196 -- Get_Library_Unit_Name_string --
7197 ----------------------------------
7199 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
7200 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
7202 begin
7203 Get_Unit_Name_String (Unit_Name_Id);
7205 -- Remove seven last character (" (spec)" or " (body)")
7207 Name_Len := Name_Len - 7;
7208 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
7209 end Get_Library_Unit_Name_String;
7211 ------------------------
7212 -- Get_Name_Entity_Id --
7213 ------------------------
7215 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
7216 begin
7217 return Entity_Id (Get_Name_Table_Info (Id));
7218 end Get_Name_Entity_Id;
7220 ------------------------------
7221 -- Get_Name_From_CTC_Pragma --
7222 ------------------------------
7224 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
7225 Arg : constant Node_Id :=
7226 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
7227 begin
7228 return Strval (Expr_Value_S (Arg));
7229 end Get_Name_From_CTC_Pragma;
7231 -----------------------
7232 -- Get_Parent_Entity --
7233 -----------------------
7235 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
7236 begin
7237 if Nkind (Unit) = N_Package_Body
7238 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
7239 then
7240 return Defining_Entity
7241 (Specification (Instance_Spec (Original_Node (Unit))));
7242 elsif Nkind (Unit) = N_Package_Instantiation then
7243 return Defining_Entity (Specification (Instance_Spec (Unit)));
7244 else
7245 return Defining_Entity (Unit);
7246 end if;
7247 end Get_Parent_Entity;
7248 -------------------
7249 -- Get_Pragma_Id --
7250 -------------------
7252 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
7253 begin
7254 return Get_Pragma_Id (Pragma_Name (N));
7255 end Get_Pragma_Id;
7257 -----------------------
7258 -- Get_Reason_String --
7259 -----------------------
7261 procedure Get_Reason_String (N : Node_Id) is
7262 begin
7263 if Nkind (N) = N_String_Literal then
7264 Store_String_Chars (Strval (N));
7266 elsif Nkind (N) = N_Op_Concat then
7267 Get_Reason_String (Left_Opnd (N));
7268 Get_Reason_String (Right_Opnd (N));
7270 -- If not of required form, error
7272 else
7273 Error_Msg_N
7274 ("Reason for pragma Warnings has wrong form", N);
7275 Error_Msg_N
7276 ("\must be string literal or concatenation of string literals", N);
7277 return;
7278 end if;
7279 end Get_Reason_String;
7281 ---------------------------
7282 -- Get_Referenced_Object --
7283 ---------------------------
7285 function Get_Referenced_Object (N : Node_Id) return Node_Id is
7286 R : Node_Id;
7288 begin
7289 R := N;
7290 while Is_Entity_Name (R)
7291 and then Present (Renamed_Object (Entity (R)))
7292 loop
7293 R := Renamed_Object (Entity (R));
7294 end loop;
7296 return R;
7297 end Get_Referenced_Object;
7299 ------------------------
7300 -- Get_Renamed_Entity --
7301 ------------------------
7303 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
7304 R : Entity_Id;
7306 begin
7307 R := E;
7308 while Present (Renamed_Entity (R)) loop
7309 R := Renamed_Entity (R);
7310 end loop;
7312 return R;
7313 end Get_Renamed_Entity;
7315 ----------------------------------
7316 -- Get_Requires_From_CTC_Pragma --
7317 ----------------------------------
7319 function Get_Requires_From_CTC_Pragma (N : Node_Id) return Node_Id is
7320 Args : constant List_Id := Pragma_Argument_Associations (N);
7321 Res : Node_Id;
7323 begin
7324 if List_Length (Args) >= 3 then
7325 Res := Pick (Args, 3);
7327 if Chars (Res) /= Name_Requires then
7328 Res := Empty;
7329 end if;
7331 else
7332 Res := Empty;
7333 end if;
7335 return Res;
7336 end Get_Requires_From_CTC_Pragma;
7338 -------------------------
7339 -- Get_Subprogram_Body --
7340 -------------------------
7342 function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
7343 Decl : Node_Id;
7345 begin
7346 Decl := Unit_Declaration_Node (E);
7348 if Nkind (Decl) = N_Subprogram_Body then
7349 return Decl;
7351 -- The below comment is bad, because it is possible for
7352 -- Nkind (Decl) to be an N_Subprogram_Body_Stub ???
7354 else -- Nkind (Decl) = N_Subprogram_Declaration
7356 if Present (Corresponding_Body (Decl)) then
7357 return Unit_Declaration_Node (Corresponding_Body (Decl));
7359 -- Imported subprogram case
7361 else
7362 return Empty;
7363 end if;
7364 end if;
7365 end Get_Subprogram_Body;
7367 ---------------------------
7368 -- Get_Subprogram_Entity --
7369 ---------------------------
7371 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
7372 Subp : Node_Id;
7373 Subp_Id : Entity_Id;
7375 begin
7376 if Nkind (Nod) = N_Accept_Statement then
7377 Subp := Entry_Direct_Name (Nod);
7379 elsif Nkind (Nod) = N_Slice then
7380 Subp := Prefix (Nod);
7382 else
7383 Subp := Name (Nod);
7384 end if;
7386 -- Strip the subprogram call
7388 loop
7389 if Nkind_In (Subp, N_Explicit_Dereference,
7390 N_Indexed_Component,
7391 N_Selected_Component)
7392 then
7393 Subp := Prefix (Subp);
7395 elsif Nkind_In (Subp, N_Type_Conversion,
7396 N_Unchecked_Type_Conversion)
7397 then
7398 Subp := Expression (Subp);
7400 else
7401 exit;
7402 end if;
7403 end loop;
7405 -- Extract the entity of the subprogram call
7407 if Is_Entity_Name (Subp) then
7408 Subp_Id := Entity (Subp);
7410 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
7411 Subp_Id := Directly_Designated_Type (Subp_Id);
7412 end if;
7414 if Is_Subprogram (Subp_Id) then
7415 return Subp_Id;
7416 else
7417 return Empty;
7418 end if;
7420 -- The search did not find a construct that denotes a subprogram
7422 else
7423 return Empty;
7424 end if;
7425 end Get_Subprogram_Entity;
7427 -----------------------------
7428 -- Get_Task_Body_Procedure --
7429 -----------------------------
7431 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
7432 begin
7433 -- Note: A task type may be the completion of a private type with
7434 -- discriminants. When performing elaboration checks on a task
7435 -- declaration, the current view of the type may be the private one,
7436 -- and the procedure that holds the body of the task is held in its
7437 -- underlying type.
7439 -- This is an odd function, why not have Task_Body_Procedure do
7440 -- the following digging???
7442 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
7443 end Get_Task_Body_Procedure;
7445 -----------------------
7446 -- Has_Access_Values --
7447 -----------------------
7449 function Has_Access_Values (T : Entity_Id) return Boolean is
7450 Typ : constant Entity_Id := Underlying_Type (T);
7452 begin
7453 -- Case of a private type which is not completed yet. This can only
7454 -- happen in the case of a generic format type appearing directly, or
7455 -- as a component of the type to which this function is being applied
7456 -- at the top level. Return False in this case, since we certainly do
7457 -- not know that the type contains access types.
7459 if No (Typ) then
7460 return False;
7462 elsif Is_Access_Type (Typ) then
7463 return True;
7465 elsif Is_Array_Type (Typ) then
7466 return Has_Access_Values (Component_Type (Typ));
7468 elsif Is_Record_Type (Typ) then
7469 declare
7470 Comp : Entity_Id;
7472 begin
7473 -- Loop to Check components
7475 Comp := First_Component_Or_Discriminant (Typ);
7476 while Present (Comp) loop
7478 -- Check for access component, tag field does not count, even
7479 -- though it is implemented internally using an access type.
7481 if Has_Access_Values (Etype (Comp))
7482 and then Chars (Comp) /= Name_uTag
7483 then
7484 return True;
7485 end if;
7487 Next_Component_Or_Discriminant (Comp);
7488 end loop;
7489 end;
7491 return False;
7493 else
7494 return False;
7495 end if;
7496 end Has_Access_Values;
7498 ------------------------------
7499 -- Has_Compatible_Alignment --
7500 ------------------------------
7502 function Has_Compatible_Alignment
7503 (Obj : Entity_Id;
7504 Expr : Node_Id) return Alignment_Result
7506 function Has_Compatible_Alignment_Internal
7507 (Obj : Entity_Id;
7508 Expr : Node_Id;
7509 Default : Alignment_Result) return Alignment_Result;
7510 -- This is the internal recursive function that actually does the work.
7511 -- There is one additional parameter, which says what the result should
7512 -- be if no alignment information is found, and there is no definite
7513 -- indication of compatible alignments. At the outer level, this is set
7514 -- to Unknown, but for internal recursive calls in the case where types
7515 -- are known to be correct, it is set to Known_Compatible.
7517 ---------------------------------------
7518 -- Has_Compatible_Alignment_Internal --
7519 ---------------------------------------
7521 function Has_Compatible_Alignment_Internal
7522 (Obj : Entity_Id;
7523 Expr : Node_Id;
7524 Default : Alignment_Result) return Alignment_Result
7526 Result : Alignment_Result := Known_Compatible;
7527 -- Holds the current status of the result. Note that once a value of
7528 -- Known_Incompatible is set, it is sticky and does not get changed
7529 -- to Unknown (the value in Result only gets worse as we go along,
7530 -- never better).
7532 Offs : Uint := No_Uint;
7533 -- Set to a factor of the offset from the base object when Expr is a
7534 -- selected or indexed component, based on Component_Bit_Offset and
7535 -- Component_Size respectively. A negative value is used to represent
7536 -- a value which is not known at compile time.
7538 procedure Check_Prefix;
7539 -- Checks the prefix recursively in the case where the expression
7540 -- is an indexed or selected component.
7542 procedure Set_Result (R : Alignment_Result);
7543 -- If R represents a worse outcome (unknown instead of known
7544 -- compatible, or known incompatible), then set Result to R.
7546 ------------------
7547 -- Check_Prefix --
7548 ------------------
7550 procedure Check_Prefix is
7551 begin
7552 -- The subtlety here is that in doing a recursive call to check
7553 -- the prefix, we have to decide what to do in the case where we
7554 -- don't find any specific indication of an alignment problem.
7556 -- At the outer level, we normally set Unknown as the result in
7557 -- this case, since we can only set Known_Compatible if we really
7558 -- know that the alignment value is OK, but for the recursive
7559 -- call, in the case where the types match, and we have not
7560 -- specified a peculiar alignment for the object, we are only
7561 -- concerned about suspicious rep clauses, the default case does
7562 -- not affect us, since the compiler will, in the absence of such
7563 -- rep clauses, ensure that the alignment is correct.
7565 if Default = Known_Compatible
7566 or else
7567 (Etype (Obj) = Etype (Expr)
7568 and then (Unknown_Alignment (Obj)
7569 or else
7570 Alignment (Obj) = Alignment (Etype (Obj))))
7571 then
7572 Set_Result
7573 (Has_Compatible_Alignment_Internal
7574 (Obj, Prefix (Expr), Known_Compatible));
7576 -- In all other cases, we need a full check on the prefix
7578 else
7579 Set_Result
7580 (Has_Compatible_Alignment_Internal
7581 (Obj, Prefix (Expr), Unknown));
7582 end if;
7583 end Check_Prefix;
7585 ----------------
7586 -- Set_Result --
7587 ----------------
7589 procedure Set_Result (R : Alignment_Result) is
7590 begin
7591 if R > Result then
7592 Result := R;
7593 end if;
7594 end Set_Result;
7596 -- Start of processing for Has_Compatible_Alignment_Internal
7598 begin
7599 -- If Expr is a selected component, we must make sure there is no
7600 -- potentially troublesome component clause, and that the record is
7601 -- not packed.
7603 if Nkind (Expr) = N_Selected_Component then
7605 -- Packed record always generate unknown alignment
7607 if Is_Packed (Etype (Prefix (Expr))) then
7608 Set_Result (Unknown);
7609 end if;
7611 -- Check prefix and component offset
7613 Check_Prefix;
7614 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
7616 -- If Expr is an indexed component, we must make sure there is no
7617 -- potentially troublesome Component_Size clause and that the array
7618 -- is not bit-packed.
7620 elsif Nkind (Expr) = N_Indexed_Component then
7621 declare
7622 Typ : constant Entity_Id := Etype (Prefix (Expr));
7623 Ind : constant Node_Id := First_Index (Typ);
7625 begin
7626 -- Bit packed array always generates unknown alignment
7628 if Is_Bit_Packed_Array (Typ) then
7629 Set_Result (Unknown);
7630 end if;
7632 -- Check prefix and component offset
7634 Check_Prefix;
7635 Offs := Component_Size (Typ);
7637 -- Small optimization: compute the full offset when possible
7639 if Offs /= No_Uint
7640 and then Offs > Uint_0
7641 and then Present (Ind)
7642 and then Nkind (Ind) = N_Range
7643 and then Compile_Time_Known_Value (Low_Bound (Ind))
7644 and then Compile_Time_Known_Value (First (Expressions (Expr)))
7645 then
7646 Offs := Offs * (Expr_Value (First (Expressions (Expr)))
7647 - Expr_Value (Low_Bound ((Ind))));
7648 end if;
7649 end;
7650 end if;
7652 -- If we have a null offset, the result is entirely determined by
7653 -- the base object and has already been computed recursively.
7655 if Offs = Uint_0 then
7656 null;
7658 -- Case where we know the alignment of the object
7660 elsif Known_Alignment (Obj) then
7661 declare
7662 ObjA : constant Uint := Alignment (Obj);
7663 ExpA : Uint := No_Uint;
7664 SizA : Uint := No_Uint;
7666 begin
7667 -- If alignment of Obj is 1, then we are always OK
7669 if ObjA = 1 then
7670 Set_Result (Known_Compatible);
7672 -- Alignment of Obj is greater than 1, so we need to check
7674 else
7675 -- If we have an offset, see if it is compatible
7677 if Offs /= No_Uint and Offs > Uint_0 then
7678 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
7679 Set_Result (Known_Incompatible);
7680 end if;
7682 -- See if Expr is an object with known alignment
7684 elsif Is_Entity_Name (Expr)
7685 and then Known_Alignment (Entity (Expr))
7686 then
7687 ExpA := Alignment (Entity (Expr));
7689 -- Otherwise, we can use the alignment of the type of
7690 -- Expr given that we already checked for
7691 -- discombobulating rep clauses for the cases of indexed
7692 -- and selected components above.
7694 elsif Known_Alignment (Etype (Expr)) then
7695 ExpA := Alignment (Etype (Expr));
7697 -- Otherwise the alignment is unknown
7699 else
7700 Set_Result (Default);
7701 end if;
7703 -- If we got an alignment, see if it is acceptable
7705 if ExpA /= No_Uint and then ExpA < ObjA then
7706 Set_Result (Known_Incompatible);
7707 end if;
7709 -- If Expr is not a piece of a larger object, see if size
7710 -- is given. If so, check that it is not too small for the
7711 -- required alignment.
7713 if Offs /= No_Uint then
7714 null;
7716 -- See if Expr is an object with known size
7718 elsif Is_Entity_Name (Expr)
7719 and then Known_Static_Esize (Entity (Expr))
7720 then
7721 SizA := Esize (Entity (Expr));
7723 -- Otherwise, we check the object size of the Expr type
7725 elsif Known_Static_Esize (Etype (Expr)) then
7726 SizA := Esize (Etype (Expr));
7727 end if;
7729 -- If we got a size, see if it is a multiple of the Obj
7730 -- alignment, if not, then the alignment cannot be
7731 -- acceptable, since the size is always a multiple of the
7732 -- alignment.
7734 if SizA /= No_Uint then
7735 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
7736 Set_Result (Known_Incompatible);
7737 end if;
7738 end if;
7739 end if;
7740 end;
7742 -- If we do not know required alignment, any non-zero offset is a
7743 -- potential problem (but certainly may be OK, so result is unknown).
7745 elsif Offs /= No_Uint then
7746 Set_Result (Unknown);
7748 -- If we can't find the result by direct comparison of alignment
7749 -- values, then there is still one case that we can determine known
7750 -- result, and that is when we can determine that the types are the
7751 -- same, and no alignments are specified. Then we known that the
7752 -- alignments are compatible, even if we don't know the alignment
7753 -- value in the front end.
7755 elsif Etype (Obj) = Etype (Expr) then
7757 -- Types are the same, but we have to check for possible size
7758 -- and alignments on the Expr object that may make the alignment
7759 -- different, even though the types are the same.
7761 if Is_Entity_Name (Expr) then
7763 -- First check alignment of the Expr object. Any alignment less
7764 -- than Maximum_Alignment is worrisome since this is the case
7765 -- where we do not know the alignment of Obj.
7767 if Known_Alignment (Entity (Expr))
7768 and then UI_To_Int (Alignment (Entity (Expr))) <
7769 Ttypes.Maximum_Alignment
7770 then
7771 Set_Result (Unknown);
7773 -- Now check size of Expr object. Any size that is not an
7774 -- even multiple of Maximum_Alignment is also worrisome
7775 -- since it may cause the alignment of the object to be less
7776 -- than the alignment of the type.
7778 elsif Known_Static_Esize (Entity (Expr))
7779 and then
7780 (UI_To_Int (Esize (Entity (Expr))) mod
7781 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
7782 /= 0
7783 then
7784 Set_Result (Unknown);
7786 -- Otherwise same type is decisive
7788 else
7789 Set_Result (Known_Compatible);
7790 end if;
7791 end if;
7793 -- Another case to deal with is when there is an explicit size or
7794 -- alignment clause when the types are not the same. If so, then the
7795 -- result is Unknown. We don't need to do this test if the Default is
7796 -- Unknown, since that result will be set in any case.
7798 elsif Default /= Unknown
7799 and then (Has_Size_Clause (Etype (Expr))
7800 or else
7801 Has_Alignment_Clause (Etype (Expr)))
7802 then
7803 Set_Result (Unknown);
7805 -- If no indication found, set default
7807 else
7808 Set_Result (Default);
7809 end if;
7811 -- Return worst result found
7813 return Result;
7814 end Has_Compatible_Alignment_Internal;
7816 -- Start of processing for Has_Compatible_Alignment
7818 begin
7819 -- If Obj has no specified alignment, then set alignment from the type
7820 -- alignment. Perhaps we should always do this, but for sure we should
7821 -- do it when there is an address clause since we can do more if the
7822 -- alignment is known.
7824 if Unknown_Alignment (Obj) then
7825 Set_Alignment (Obj, Alignment (Etype (Obj)));
7826 end if;
7828 -- Now do the internal call that does all the work
7830 return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
7831 end Has_Compatible_Alignment;
7833 ----------------------
7834 -- Has_Declarations --
7835 ----------------------
7837 function Has_Declarations (N : Node_Id) return Boolean is
7838 begin
7839 return Nkind_In (Nkind (N), N_Accept_Statement,
7840 N_Block_Statement,
7841 N_Compilation_Unit_Aux,
7842 N_Entry_Body,
7843 N_Package_Body,
7844 N_Protected_Body,
7845 N_Subprogram_Body,
7846 N_Task_Body,
7847 N_Package_Specification);
7848 end Has_Declarations;
7850 ---------------------------------
7851 -- Has_Defaulted_Discriminants --
7852 ---------------------------------
7854 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
7855 begin
7856 return Has_Discriminants (Typ)
7857 and then Present (First_Discriminant (Typ))
7858 and then Present (Discriminant_Default_Value
7859 (First_Discriminant (Typ)));
7860 end Has_Defaulted_Discriminants;
7862 -------------------
7863 -- Has_Denormals --
7864 -------------------
7866 function Has_Denormals (E : Entity_Id) return Boolean is
7867 begin
7868 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
7869 end Has_Denormals;
7871 -------------------------------------------
7872 -- Has_Discriminant_Dependent_Constraint --
7873 -------------------------------------------
7875 function Has_Discriminant_Dependent_Constraint
7876 (Comp : Entity_Id) return Boolean
7878 Comp_Decl : constant Node_Id := Parent (Comp);
7879 Subt_Indic : Node_Id;
7880 Constr : Node_Id;
7881 Assn : Node_Id;
7883 begin
7884 -- Discriminants can't depend on discriminants
7886 if Ekind (Comp) = E_Discriminant then
7887 return False;
7889 else
7890 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
7892 if Nkind (Subt_Indic) = N_Subtype_Indication then
7893 Constr := Constraint (Subt_Indic);
7895 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
7896 Assn := First (Constraints (Constr));
7897 while Present (Assn) loop
7898 case Nkind (Assn) is
7899 when N_Subtype_Indication |
7900 N_Range |
7901 N_Identifier
7903 if Depends_On_Discriminant (Assn) then
7904 return True;
7905 end if;
7907 when N_Discriminant_Association =>
7908 if Depends_On_Discriminant (Expression (Assn)) then
7909 return True;
7910 end if;
7912 when others =>
7913 null;
7914 end case;
7916 Next (Assn);
7917 end loop;
7918 end if;
7919 end if;
7920 end if;
7922 return False;
7923 end Has_Discriminant_Dependent_Constraint;
7925 --------------------------
7926 -- Has_Enabled_Property --
7927 --------------------------
7929 function Has_Enabled_Property
7930 (Item_Id : Entity_Id;
7931 Property : Name_Id) return Boolean
7933 function State_Has_Enabled_Property return Boolean;
7934 -- Determine whether a state denoted by Item_Id has the property enabled
7936 function Variable_Has_Enabled_Property return Boolean;
7937 -- Determine whether a variable denoted by Item_Id has the property
7938 -- enabled.
7940 --------------------------------
7941 -- State_Has_Enabled_Property --
7942 --------------------------------
7944 function State_Has_Enabled_Property return Boolean is
7945 Decl : constant Node_Id := Parent (Item_Id);
7946 Opt : Node_Id;
7947 Opt_Nam : Node_Id;
7948 Prop : Node_Id;
7949 Prop_Nam : Node_Id;
7950 Props : Node_Id;
7952 begin
7953 -- The declaration of an external abstract state appears as an
7954 -- extension aggregate. If this is not the case, properties can never
7955 -- be set.
7957 if Nkind (Decl) /= N_Extension_Aggregate then
7958 return False;
7959 end if;
7961 -- When External appears as a simple option, it automatically enables
7962 -- all properties.
7964 Opt := First (Expressions (Decl));
7965 while Present (Opt) loop
7966 if Nkind (Opt) = N_Identifier
7967 and then Chars (Opt) = Name_External
7968 then
7969 return True;
7970 end if;
7972 Next (Opt);
7973 end loop;
7975 -- When External specifies particular properties, inspect those and
7976 -- find the desired one (if any).
7978 Opt := First (Component_Associations (Decl));
7979 while Present (Opt) loop
7980 Opt_Nam := First (Choices (Opt));
7982 if Nkind (Opt_Nam) = N_Identifier
7983 and then Chars (Opt_Nam) = Name_External
7984 then
7985 Props := Expression (Opt);
7987 -- Multiple properties appear as an aggregate
7989 if Nkind (Props) = N_Aggregate then
7991 -- Simple property form
7993 Prop := First (Expressions (Props));
7994 while Present (Prop) loop
7995 if Chars (Prop) = Property then
7996 return True;
7997 end if;
7999 Next (Prop);
8000 end loop;
8002 -- Property with expression form
8004 Prop := First (Component_Associations (Props));
8005 while Present (Prop) loop
8006 Prop_Nam := First (Choices (Prop));
8008 -- The property can be represented in two ways:
8009 -- others => <value>
8010 -- <property> => <value>
8012 if Nkind (Prop_Nam) = N_Others_Choice
8013 or else (Nkind (Prop_Nam) = N_Identifier
8014 and then Chars (Prop_Nam) = Property)
8015 then
8016 return Is_True (Expr_Value (Expression (Prop)));
8017 end if;
8019 Next (Prop);
8020 end loop;
8022 -- Single property
8024 else
8025 return Chars (Props) = Property;
8026 end if;
8027 end if;
8029 Next (Opt);
8030 end loop;
8032 return False;
8033 end State_Has_Enabled_Property;
8035 -----------------------------------
8036 -- Variable_Has_Enabled_Property --
8037 -----------------------------------
8039 function Variable_Has_Enabled_Property return Boolean is
8040 function Is_Enabled (Prag : Node_Id) return Boolean;
8041 -- Determine whether property pragma Prag (if present) denotes an
8042 -- enabled property.
8044 ----------------
8045 -- Is_Enabled --
8046 ----------------
8048 function Is_Enabled (Prag : Node_Id) return Boolean is
8049 Arg2 : Node_Id;
8051 begin
8052 if Present (Prag) then
8053 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
8055 -- The pragma has an optional Boolean expression, the related
8056 -- property is enabled only when the expression evaluates to
8057 -- True.
8059 if Present (Arg2) then
8060 return Is_True (Expr_Value (Get_Pragma_Arg (Arg2)));
8062 -- Otherwise the lack of expression enables the property by
8063 -- default.
8065 else
8066 return True;
8067 end if;
8069 -- The property was never set in the first place
8071 else
8072 return False;
8073 end if;
8074 end Is_Enabled;
8076 -- Local variables
8078 AR : constant Node_Id :=
8079 Get_Pragma (Item_Id, Pragma_Async_Readers);
8080 AW : constant Node_Id :=
8081 Get_Pragma (Item_Id, Pragma_Async_Writers);
8082 ER : constant Node_Id :=
8083 Get_Pragma (Item_Id, Pragma_Effective_Reads);
8084 EW : constant Node_Id :=
8085 Get_Pragma (Item_Id, Pragma_Effective_Writes);
8087 -- Start of processing for Variable_Has_Enabled_Property
8089 begin
8090 -- A non-effectively volatile object can never possess external
8091 -- properties.
8093 if not Is_Effectively_Volatile (Item_Id) then
8094 return False;
8096 -- External properties related to variables come in two flavors -
8097 -- explicit and implicit. The explicit case is characterized by the
8098 -- presence of a property pragma with an optional Boolean flag. The
8099 -- property is enabled when the flag evaluates to True or the flag is
8100 -- missing altogether.
8102 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
8103 return True;
8105 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
8106 return True;
8108 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
8109 return True;
8111 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
8112 return True;
8114 -- The implicit case lacks all property pragmas
8116 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
8117 return True;
8119 else
8120 return False;
8121 end if;
8122 end Variable_Has_Enabled_Property;
8124 -- Start of processing for Has_Enabled_Property
8126 begin
8127 -- Abstract states and variables have a flexible scheme of specifying
8128 -- external properties.
8130 if Ekind (Item_Id) = E_Abstract_State then
8131 return State_Has_Enabled_Property;
8133 elsif Ekind (Item_Id) = E_Variable then
8134 return Variable_Has_Enabled_Property;
8136 -- Otherwise a property is enabled when the related item is effectively
8137 -- volatile.
8139 else
8140 return Is_Effectively_Volatile (Item_Id);
8141 end if;
8142 end Has_Enabled_Property;
8144 --------------------
8145 -- Has_Infinities --
8146 --------------------
8148 function Has_Infinities (E : Entity_Id) return Boolean is
8149 begin
8150 return
8151 Is_Floating_Point_Type (E)
8152 and then Nkind (Scalar_Range (E)) = N_Range
8153 and then Includes_Infinities (Scalar_Range (E));
8154 end Has_Infinities;
8156 --------------------
8157 -- Has_Interfaces --
8158 --------------------
8160 function Has_Interfaces
8161 (T : Entity_Id;
8162 Use_Full_View : Boolean := True) return Boolean
8164 Typ : Entity_Id := Base_Type (T);
8166 begin
8167 -- Handle concurrent types
8169 if Is_Concurrent_Type (Typ) then
8170 Typ := Corresponding_Record_Type (Typ);
8171 end if;
8173 if not Present (Typ)
8174 or else not Is_Record_Type (Typ)
8175 or else not Is_Tagged_Type (Typ)
8176 then
8177 return False;
8178 end if;
8180 -- Handle private types
8182 if Use_Full_View and then Present (Full_View (Typ)) then
8183 Typ := Full_View (Typ);
8184 end if;
8186 -- Handle concurrent record types
8188 if Is_Concurrent_Record_Type (Typ)
8189 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
8190 then
8191 return True;
8192 end if;
8194 loop
8195 if Is_Interface (Typ)
8196 or else
8197 (Is_Record_Type (Typ)
8198 and then Present (Interfaces (Typ))
8199 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
8200 then
8201 return True;
8202 end if;
8204 exit when Etype (Typ) = Typ
8206 -- Handle private types
8208 or else (Present (Full_View (Etype (Typ)))
8209 and then Full_View (Etype (Typ)) = Typ)
8211 -- Protect frontend against wrong sources with cyclic derivations
8213 or else Etype (Typ) = T;
8215 -- Climb to the ancestor type handling private types
8217 if Present (Full_View (Etype (Typ))) then
8218 Typ := Full_View (Etype (Typ));
8219 else
8220 Typ := Etype (Typ);
8221 end if;
8222 end loop;
8224 return False;
8225 end Has_Interfaces;
8227 ---------------------------------
8228 -- Has_No_Obvious_Side_Effects --
8229 ---------------------------------
8231 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
8232 begin
8233 -- For now, just handle literals, constants, and non-volatile
8234 -- variables and expressions combining these with operators or
8235 -- short circuit forms.
8237 if Nkind (N) in N_Numeric_Or_String_Literal then
8238 return True;
8240 elsif Nkind (N) = N_Character_Literal then
8241 return True;
8243 elsif Nkind (N) in N_Unary_Op then
8244 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
8246 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
8247 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
8248 and then
8249 Has_No_Obvious_Side_Effects (Right_Opnd (N));
8251 elsif Nkind (N) = N_Expression_With_Actions
8252 and then Is_Empty_List (Actions (N))
8253 then
8254 return Has_No_Obvious_Side_Effects (Expression (N));
8256 elsif Nkind (N) in N_Has_Entity then
8257 return Present (Entity (N))
8258 and then Ekind_In (Entity (N), E_Variable,
8259 E_Constant,
8260 E_Enumeration_Literal,
8261 E_In_Parameter,
8262 E_Out_Parameter,
8263 E_In_Out_Parameter)
8264 and then not Is_Volatile (Entity (N));
8266 else
8267 return False;
8268 end if;
8269 end Has_No_Obvious_Side_Effects;
8271 ------------------------
8272 -- Has_Null_Exclusion --
8273 ------------------------
8275 function Has_Null_Exclusion (N : Node_Id) return Boolean is
8276 begin
8277 case Nkind (N) is
8278 when N_Access_Definition |
8279 N_Access_Function_Definition |
8280 N_Access_Procedure_Definition |
8281 N_Access_To_Object_Definition |
8282 N_Allocator |
8283 N_Derived_Type_Definition |
8284 N_Function_Specification |
8285 N_Subtype_Declaration =>
8286 return Null_Exclusion_Present (N);
8288 when N_Component_Definition |
8289 N_Formal_Object_Declaration |
8290 N_Object_Renaming_Declaration =>
8291 if Present (Subtype_Mark (N)) then
8292 return Null_Exclusion_Present (N);
8293 else pragma Assert (Present (Access_Definition (N)));
8294 return Null_Exclusion_Present (Access_Definition (N));
8295 end if;
8297 when N_Discriminant_Specification =>
8298 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
8299 return Null_Exclusion_Present (Discriminant_Type (N));
8300 else
8301 return Null_Exclusion_Present (N);
8302 end if;
8304 when N_Object_Declaration =>
8305 if Nkind (Object_Definition (N)) = N_Access_Definition then
8306 return Null_Exclusion_Present (Object_Definition (N));
8307 else
8308 return Null_Exclusion_Present (N);
8309 end if;
8311 when N_Parameter_Specification =>
8312 if Nkind (Parameter_Type (N)) = N_Access_Definition then
8313 return Null_Exclusion_Present (Parameter_Type (N));
8314 else
8315 return Null_Exclusion_Present (N);
8316 end if;
8318 when others =>
8319 return False;
8321 end case;
8322 end Has_Null_Exclusion;
8324 ------------------------
8325 -- Has_Null_Extension --
8326 ------------------------
8328 function Has_Null_Extension (T : Entity_Id) return Boolean is
8329 B : constant Entity_Id := Base_Type (T);
8330 Comps : Node_Id;
8331 Ext : Node_Id;
8333 begin
8334 if Nkind (Parent (B)) = N_Full_Type_Declaration
8335 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
8336 then
8337 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
8339 if Present (Ext) then
8340 if Null_Present (Ext) then
8341 return True;
8342 else
8343 Comps := Component_List (Ext);
8345 -- The null component list is rewritten during analysis to
8346 -- include the parent component. Any other component indicates
8347 -- that the extension was not originally null.
8349 return Null_Present (Comps)
8350 or else No (Next (First (Component_Items (Comps))));
8351 end if;
8352 else
8353 return False;
8354 end if;
8356 else
8357 return False;
8358 end if;
8359 end Has_Null_Extension;
8361 -------------------------------
8362 -- Has_Overriding_Initialize --
8363 -------------------------------
8365 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
8366 BT : constant Entity_Id := Base_Type (T);
8367 P : Elmt_Id;
8369 begin
8370 if Is_Controlled (BT) then
8371 if Is_RTU (Scope (BT), Ada_Finalization) then
8372 return False;
8374 elsif Present (Primitive_Operations (BT)) then
8375 P := First_Elmt (Primitive_Operations (BT));
8376 while Present (P) loop
8377 declare
8378 Init : constant Entity_Id := Node (P);
8379 Formal : constant Entity_Id := First_Formal (Init);
8380 begin
8381 if Ekind (Init) = E_Procedure
8382 and then Chars (Init) = Name_Initialize
8383 and then Comes_From_Source (Init)
8384 and then Present (Formal)
8385 and then Etype (Formal) = BT
8386 and then No (Next_Formal (Formal))
8387 and then (Ada_Version < Ada_2012
8388 or else not Null_Present (Parent (Init)))
8389 then
8390 return True;
8391 end if;
8392 end;
8394 Next_Elmt (P);
8395 end loop;
8396 end if;
8398 -- Here if type itself does not have a non-null Initialize operation:
8399 -- check immediate ancestor.
8401 if Is_Derived_Type (BT)
8402 and then Has_Overriding_Initialize (Etype (BT))
8403 then
8404 return True;
8405 end if;
8406 end if;
8408 return False;
8409 end Has_Overriding_Initialize;
8411 --------------------------------------
8412 -- Has_Preelaborable_Initialization --
8413 --------------------------------------
8415 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
8416 Has_PE : Boolean;
8418 procedure Check_Components (E : Entity_Id);
8419 -- Check component/discriminant chain, sets Has_PE False if a component
8420 -- or discriminant does not meet the preelaborable initialization rules.
8422 ----------------------
8423 -- Check_Components --
8424 ----------------------
8426 procedure Check_Components (E : Entity_Id) is
8427 Ent : Entity_Id;
8428 Exp : Node_Id;
8430 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
8431 -- Returns True if and only if the expression denoted by N does not
8432 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
8434 ---------------------------------
8435 -- Is_Preelaborable_Expression --
8436 ---------------------------------
8438 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
8439 Exp : Node_Id;
8440 Assn : Node_Id;
8441 Choice : Node_Id;
8442 Comp_Type : Entity_Id;
8443 Is_Array_Aggr : Boolean;
8445 begin
8446 if Is_OK_Static_Expression (N) then
8447 return True;
8449 elsif Nkind (N) = N_Null then
8450 return True;
8452 -- Attributes are allowed in general, even if their prefix is a
8453 -- formal type. (It seems that certain attributes known not to be
8454 -- static might not be allowed, but there are no rules to prevent
8455 -- them.)
8457 elsif Nkind (N) = N_Attribute_Reference then
8458 return True;
8460 -- The name of a discriminant evaluated within its parent type is
8461 -- defined to be preelaborable (10.2.1(8)). Note that we test for
8462 -- names that denote discriminals as well as discriminants to
8463 -- catch references occurring within init procs.
8465 elsif Is_Entity_Name (N)
8466 and then
8467 (Ekind (Entity (N)) = E_Discriminant
8468 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
8469 and then Present (Discriminal_Link (Entity (N)))))
8470 then
8471 return True;
8473 elsif Nkind (N) = N_Qualified_Expression then
8474 return Is_Preelaborable_Expression (Expression (N));
8476 -- For aggregates we have to check that each of the associations
8477 -- is preelaborable.
8479 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8480 Is_Array_Aggr := Is_Array_Type (Etype (N));
8482 if Is_Array_Aggr then
8483 Comp_Type := Component_Type (Etype (N));
8484 end if;
8486 -- Check the ancestor part of extension aggregates, which must
8487 -- be either the name of a type that has preelaborable init or
8488 -- an expression that is preelaborable.
8490 if Nkind (N) = N_Extension_Aggregate then
8491 declare
8492 Anc_Part : constant Node_Id := Ancestor_Part (N);
8494 begin
8495 if Is_Entity_Name (Anc_Part)
8496 and then Is_Type (Entity (Anc_Part))
8497 then
8498 if not Has_Preelaborable_Initialization
8499 (Entity (Anc_Part))
8500 then
8501 return False;
8502 end if;
8504 elsif not Is_Preelaborable_Expression (Anc_Part) then
8505 return False;
8506 end if;
8507 end;
8508 end if;
8510 -- Check positional associations
8512 Exp := First (Expressions (N));
8513 while Present (Exp) loop
8514 if not Is_Preelaborable_Expression (Exp) then
8515 return False;
8516 end if;
8518 Next (Exp);
8519 end loop;
8521 -- Check named associations
8523 Assn := First (Component_Associations (N));
8524 while Present (Assn) loop
8525 Choice := First (Choices (Assn));
8526 while Present (Choice) loop
8527 if Is_Array_Aggr then
8528 if Nkind (Choice) = N_Others_Choice then
8529 null;
8531 elsif Nkind (Choice) = N_Range then
8532 if not Is_OK_Static_Range (Choice) then
8533 return False;
8534 end if;
8536 elsif not Is_OK_Static_Expression (Choice) then
8537 return False;
8538 end if;
8540 else
8541 Comp_Type := Etype (Choice);
8542 end if;
8544 Next (Choice);
8545 end loop;
8547 -- If the association has a <> at this point, then we have
8548 -- to check whether the component's type has preelaborable
8549 -- initialization. Note that this only occurs when the
8550 -- association's corresponding component does not have a
8551 -- default expression, the latter case having already been
8552 -- expanded as an expression for the association.
8554 if Box_Present (Assn) then
8555 if not Has_Preelaborable_Initialization (Comp_Type) then
8556 return False;
8557 end if;
8559 -- In the expression case we check whether the expression
8560 -- is preelaborable.
8562 elsif
8563 not Is_Preelaborable_Expression (Expression (Assn))
8564 then
8565 return False;
8566 end if;
8568 Next (Assn);
8569 end loop;
8571 -- If we get here then aggregate as a whole is preelaborable
8573 return True;
8575 -- All other cases are not preelaborable
8577 else
8578 return False;
8579 end if;
8580 end Is_Preelaborable_Expression;
8582 -- Start of processing for Check_Components
8584 begin
8585 -- Loop through entities of record or protected type
8587 Ent := E;
8588 while Present (Ent) loop
8590 -- We are interested only in components and discriminants
8592 Exp := Empty;
8594 case Ekind (Ent) is
8595 when E_Component =>
8597 -- Get default expression if any. If there is no declaration
8598 -- node, it means we have an internal entity. The parent and
8599 -- tag fields are examples of such entities. For such cases,
8600 -- we just test the type of the entity.
8602 if Present (Declaration_Node (Ent)) then
8603 Exp := Expression (Declaration_Node (Ent));
8604 end if;
8606 when E_Discriminant =>
8608 -- Note: for a renamed discriminant, the Declaration_Node
8609 -- may point to the one from the ancestor, and have a
8610 -- different expression, so use the proper attribute to
8611 -- retrieve the expression from the derived constraint.
8613 Exp := Discriminant_Default_Value (Ent);
8615 when others =>
8616 goto Check_Next_Entity;
8617 end case;
8619 -- A component has PI if it has no default expression and the
8620 -- component type has PI.
8622 if No (Exp) then
8623 if not Has_Preelaborable_Initialization (Etype (Ent)) then
8624 Has_PE := False;
8625 exit;
8626 end if;
8628 -- Require the default expression to be preelaborable
8630 elsif not Is_Preelaborable_Expression (Exp) then
8631 Has_PE := False;
8632 exit;
8633 end if;
8635 <<Check_Next_Entity>>
8636 Next_Entity (Ent);
8637 end loop;
8638 end Check_Components;
8640 -- Start of processing for Has_Preelaborable_Initialization
8642 begin
8643 -- Immediate return if already marked as known preelaborable init. This
8644 -- covers types for which this function has already been called once
8645 -- and returned True (in which case the result is cached), and also
8646 -- types to which a pragma Preelaborable_Initialization applies.
8648 if Known_To_Have_Preelab_Init (E) then
8649 return True;
8650 end if;
8652 -- If the type is a subtype representing a generic actual type, then
8653 -- test whether its base type has preelaborable initialization since
8654 -- the subtype representing the actual does not inherit this attribute
8655 -- from the actual or formal. (but maybe it should???)
8657 if Is_Generic_Actual_Type (E) then
8658 return Has_Preelaborable_Initialization (Base_Type (E));
8659 end if;
8661 -- All elementary types have preelaborable initialization
8663 if Is_Elementary_Type (E) then
8664 Has_PE := True;
8666 -- Array types have PI if the component type has PI
8668 elsif Is_Array_Type (E) then
8669 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
8671 -- A derived type has preelaborable initialization if its parent type
8672 -- has preelaborable initialization and (in the case of a derived record
8673 -- extension) if the non-inherited components all have preelaborable
8674 -- initialization. However, a user-defined controlled type with an
8675 -- overriding Initialize procedure does not have preelaborable
8676 -- initialization.
8678 elsif Is_Derived_Type (E) then
8680 -- If the derived type is a private extension then it doesn't have
8681 -- preelaborable initialization.
8683 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
8684 return False;
8685 end if;
8687 -- First check whether ancestor type has preelaborable initialization
8689 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
8691 -- If OK, check extension components (if any)
8693 if Has_PE and then Is_Record_Type (E) then
8694 Check_Components (First_Entity (E));
8695 end if;
8697 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
8698 -- with a user defined Initialize procedure does not have PI. If
8699 -- the type is untagged, the control primitives come from a component
8700 -- that has already been checked.
8702 if Has_PE
8703 and then Is_Controlled (E)
8704 and then Is_Tagged_Type (E)
8705 and then Has_Overriding_Initialize (E)
8706 then
8707 Has_PE := False;
8708 end if;
8710 -- Private types not derived from a type having preelaborable init and
8711 -- that are not marked with pragma Preelaborable_Initialization do not
8712 -- have preelaborable initialization.
8714 elsif Is_Private_Type (E) then
8715 return False;
8717 -- Record type has PI if it is non private and all components have PI
8719 elsif Is_Record_Type (E) then
8720 Has_PE := True;
8721 Check_Components (First_Entity (E));
8723 -- Protected types must not have entries, and components must meet
8724 -- same set of rules as for record components.
8726 elsif Is_Protected_Type (E) then
8727 if Has_Entries (E) then
8728 Has_PE := False;
8729 else
8730 Has_PE := True;
8731 Check_Components (First_Entity (E));
8732 Check_Components (First_Private_Entity (E));
8733 end if;
8735 -- Type System.Address always has preelaborable initialization
8737 elsif Is_RTE (E, RE_Address) then
8738 Has_PE := True;
8740 -- In all other cases, type does not have preelaborable initialization
8742 else
8743 return False;
8744 end if;
8746 -- If type has preelaborable initialization, cache result
8748 if Has_PE then
8749 Set_Known_To_Have_Preelab_Init (E);
8750 end if;
8752 return Has_PE;
8753 end Has_Preelaborable_Initialization;
8755 ---------------------------
8756 -- Has_Private_Component --
8757 ---------------------------
8759 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
8760 Btype : Entity_Id := Base_Type (Type_Id);
8761 Component : Entity_Id;
8763 begin
8764 if Error_Posted (Type_Id)
8765 or else Error_Posted (Btype)
8766 then
8767 return False;
8768 end if;
8770 if Is_Class_Wide_Type (Btype) then
8771 Btype := Root_Type (Btype);
8772 end if;
8774 if Is_Private_Type (Btype) then
8775 declare
8776 UT : constant Entity_Id := Underlying_Type (Btype);
8777 begin
8778 if No (UT) then
8779 if No (Full_View (Btype)) then
8780 return not Is_Generic_Type (Btype)
8781 and then
8782 not Is_Generic_Type (Root_Type (Btype));
8783 else
8784 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
8785 end if;
8786 else
8787 return not Is_Frozen (UT) and then Has_Private_Component (UT);
8788 end if;
8789 end;
8791 elsif Is_Array_Type (Btype) then
8792 return Has_Private_Component (Component_Type (Btype));
8794 elsif Is_Record_Type (Btype) then
8795 Component := First_Component (Btype);
8796 while Present (Component) loop
8797 if Has_Private_Component (Etype (Component)) then
8798 return True;
8799 end if;
8801 Next_Component (Component);
8802 end loop;
8804 return False;
8806 elsif Is_Protected_Type (Btype)
8807 and then Present (Corresponding_Record_Type (Btype))
8808 then
8809 return Has_Private_Component (Corresponding_Record_Type (Btype));
8811 else
8812 return False;
8813 end if;
8814 end Has_Private_Component;
8816 ----------------------
8817 -- Has_Signed_Zeros --
8818 ----------------------
8820 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
8821 begin
8822 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
8823 end Has_Signed_Zeros;
8825 -----------------------------
8826 -- Has_Static_Array_Bounds --
8827 -----------------------------
8829 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
8830 Ndims : constant Nat := Number_Dimensions (Typ);
8832 Index : Node_Id;
8833 Low : Node_Id;
8834 High : Node_Id;
8836 begin
8837 -- Unconstrained types do not have static bounds
8839 if not Is_Constrained (Typ) then
8840 return False;
8841 end if;
8843 -- First treat string literals specially, as the lower bound and length
8844 -- of string literals are not stored like those of arrays.
8846 -- A string literal always has static bounds
8848 if Ekind (Typ) = E_String_Literal_Subtype then
8849 return True;
8850 end if;
8852 -- Treat all dimensions in turn
8854 Index := First_Index (Typ);
8855 for Indx in 1 .. Ndims loop
8857 -- In case of an illegal index which is not a discrete type, return
8858 -- that the type is not static.
8860 if not Is_Discrete_Type (Etype (Index))
8861 or else Etype (Index) = Any_Type
8862 then
8863 return False;
8864 end if;
8866 Get_Index_Bounds (Index, Low, High);
8868 if Error_Posted (Low) or else Error_Posted (High) then
8869 return False;
8870 end if;
8872 if Is_OK_Static_Expression (Low)
8873 and then
8874 Is_OK_Static_Expression (High)
8875 then
8876 null;
8877 else
8878 return False;
8879 end if;
8881 Next (Index);
8882 end loop;
8884 -- If we fall through the loop, all indexes matched
8886 return True;
8887 end Has_Static_Array_Bounds;
8889 ----------------
8890 -- Has_Stream --
8891 ----------------
8893 function Has_Stream (T : Entity_Id) return Boolean is
8894 E : Entity_Id;
8896 begin
8897 if No (T) then
8898 return False;
8900 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
8901 return True;
8903 elsif Is_Array_Type (T) then
8904 return Has_Stream (Component_Type (T));
8906 elsif Is_Record_Type (T) then
8907 E := First_Component (T);
8908 while Present (E) loop
8909 if Has_Stream (Etype (E)) then
8910 return True;
8911 else
8912 Next_Component (E);
8913 end if;
8914 end loop;
8916 return False;
8918 elsif Is_Private_Type (T) then
8919 return Has_Stream (Underlying_Type (T));
8921 else
8922 return False;
8923 end if;
8924 end Has_Stream;
8926 ----------------
8927 -- Has_Suffix --
8928 ----------------
8930 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
8931 begin
8932 Get_Name_String (Chars (E));
8933 return Name_Buffer (Name_Len) = Suffix;
8934 end Has_Suffix;
8936 ----------------
8937 -- Add_Suffix --
8938 ----------------
8940 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
8941 begin
8942 Get_Name_String (Chars (E));
8943 Add_Char_To_Name_Buffer (Suffix);
8944 return Name_Find;
8945 end Add_Suffix;
8947 -------------------
8948 -- Remove_Suffix --
8949 -------------------
8951 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
8952 begin
8953 pragma Assert (Has_Suffix (E, Suffix));
8954 Get_Name_String (Chars (E));
8955 Name_Len := Name_Len - 1;
8956 return Name_Find;
8957 end Remove_Suffix;
8959 --------------------------
8960 -- Has_Tagged_Component --
8961 --------------------------
8963 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
8964 Comp : Entity_Id;
8966 begin
8967 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
8968 return Has_Tagged_Component (Underlying_Type (Typ));
8970 elsif Is_Array_Type (Typ) then
8971 return Has_Tagged_Component (Component_Type (Typ));
8973 elsif Is_Tagged_Type (Typ) then
8974 return True;
8976 elsif Is_Record_Type (Typ) then
8977 Comp := First_Component (Typ);
8978 while Present (Comp) loop
8979 if Has_Tagged_Component (Etype (Comp)) then
8980 return True;
8981 end if;
8983 Next_Component (Comp);
8984 end loop;
8986 return False;
8988 else
8989 return False;
8990 end if;
8991 end Has_Tagged_Component;
8993 ----------------------------
8994 -- Has_Volatile_Component --
8995 ----------------------------
8997 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
8998 Comp : Entity_Id;
9000 begin
9001 if Has_Volatile_Components (Typ) then
9002 return True;
9004 elsif Is_Array_Type (Typ) then
9005 return Is_Volatile (Component_Type (Typ));
9007 elsif Is_Record_Type (Typ) then
9008 Comp := First_Component (Typ);
9009 while Present (Comp) loop
9010 if Is_Volatile_Object (Comp) then
9011 return True;
9012 end if;
9014 Comp := Next_Component (Comp);
9015 end loop;
9016 end if;
9018 return False;
9019 end Has_Volatile_Component;
9021 -------------------------
9022 -- Implementation_Kind --
9023 -------------------------
9025 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
9026 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
9027 Arg : Node_Id;
9028 begin
9029 pragma Assert (Present (Impl_Prag));
9030 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
9031 return Chars (Get_Pragma_Arg (Arg));
9032 end Implementation_Kind;
9034 --------------------------
9035 -- Implements_Interface --
9036 --------------------------
9038 function Implements_Interface
9039 (Typ_Ent : Entity_Id;
9040 Iface_Ent : Entity_Id;
9041 Exclude_Parents : Boolean := False) return Boolean
9043 Ifaces_List : Elist_Id;
9044 Elmt : Elmt_Id;
9045 Iface : Entity_Id := Base_Type (Iface_Ent);
9046 Typ : Entity_Id := Base_Type (Typ_Ent);
9048 begin
9049 if Is_Class_Wide_Type (Typ) then
9050 Typ := Root_Type (Typ);
9051 end if;
9053 if not Has_Interfaces (Typ) then
9054 return False;
9055 end if;
9057 if Is_Class_Wide_Type (Iface) then
9058 Iface := Root_Type (Iface);
9059 end if;
9061 Collect_Interfaces (Typ, Ifaces_List);
9063 Elmt := First_Elmt (Ifaces_List);
9064 while Present (Elmt) loop
9065 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
9066 and then Exclude_Parents
9067 then
9068 null;
9070 elsif Node (Elmt) = Iface then
9071 return True;
9072 end if;
9074 Next_Elmt (Elmt);
9075 end loop;
9077 return False;
9078 end Implements_Interface;
9080 ------------------------------------
9081 -- In_Assertion_Expression_Pragma --
9082 ------------------------------------
9084 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
9085 Par : Node_Id;
9086 Prag : Node_Id := Empty;
9088 begin
9089 -- Climb the parent chain looking for an enclosing pragma
9091 Par := N;
9092 while Present (Par) loop
9093 if Nkind (Par) = N_Pragma then
9094 Prag := Par;
9095 exit;
9097 -- Precondition-like pragmas are expanded into if statements, check
9098 -- the original node instead.
9100 elsif Nkind (Original_Node (Par)) = N_Pragma then
9101 Prag := Original_Node (Par);
9102 exit;
9104 -- The expansion of attribute 'Old generates a constant to capture
9105 -- the result of the prefix. If the parent traversal reaches
9106 -- one of these constants, then the node technically came from a
9107 -- postcondition-like pragma. Note that the Ekind is not tested here
9108 -- because N may be the expression of an object declaration which is
9109 -- currently being analyzed. Such objects carry Ekind of E_Void.
9111 elsif Nkind (Par) = N_Object_Declaration
9112 and then Constant_Present (Par)
9113 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
9114 then
9115 return True;
9117 -- Prevent the search from going too far
9119 elsif Is_Body_Or_Package_Declaration (Par) then
9120 return False;
9121 end if;
9123 Par := Parent (Par);
9124 end loop;
9126 return
9127 Present (Prag)
9128 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
9129 end In_Assertion_Expression_Pragma;
9131 -----------------
9132 -- In_Instance --
9133 -----------------
9135 function In_Instance return Boolean is
9136 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9137 S : Entity_Id;
9139 begin
9140 S := Current_Scope;
9141 while Present (S) and then S /= Standard_Standard loop
9142 if Ekind_In (S, E_Function, E_Package, E_Procedure)
9143 and then Is_Generic_Instance (S)
9144 then
9145 -- A child instance is always compiled in the context of a parent
9146 -- instance. Nevertheless, the actuals are not analyzed in an
9147 -- instance context. We detect this case by examining the current
9148 -- compilation unit, which must be a child instance, and checking
9149 -- that it is not currently on the scope stack.
9151 if Is_Child_Unit (Curr_Unit)
9152 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
9153 N_Package_Instantiation
9154 and then not In_Open_Scopes (Curr_Unit)
9155 then
9156 return False;
9157 else
9158 return True;
9159 end if;
9160 end if;
9162 S := Scope (S);
9163 end loop;
9165 return False;
9166 end In_Instance;
9168 ----------------------
9169 -- In_Instance_Body --
9170 ----------------------
9172 function In_Instance_Body return Boolean is
9173 S : Entity_Id;
9175 begin
9176 S := Current_Scope;
9177 while Present (S) and then S /= Standard_Standard loop
9178 if Ekind_In (S, E_Function, E_Procedure)
9179 and then Is_Generic_Instance (S)
9180 then
9181 return True;
9183 elsif Ekind (S) = E_Package
9184 and then In_Package_Body (S)
9185 and then Is_Generic_Instance (S)
9186 then
9187 return True;
9188 end if;
9190 S := Scope (S);
9191 end loop;
9193 return False;
9194 end In_Instance_Body;
9196 -----------------------------
9197 -- In_Instance_Not_Visible --
9198 -----------------------------
9200 function In_Instance_Not_Visible return Boolean is
9201 S : Entity_Id;
9203 begin
9204 S := Current_Scope;
9205 while Present (S) and then S /= Standard_Standard loop
9206 if Ekind_In (S, E_Function, E_Procedure)
9207 and then Is_Generic_Instance (S)
9208 then
9209 return True;
9211 elsif Ekind (S) = E_Package
9212 and then (In_Package_Body (S) or else In_Private_Part (S))
9213 and then Is_Generic_Instance (S)
9214 then
9215 return True;
9216 end if;
9218 S := Scope (S);
9219 end loop;
9221 return False;
9222 end In_Instance_Not_Visible;
9224 ------------------------------
9225 -- In_Instance_Visible_Part --
9226 ------------------------------
9228 function In_Instance_Visible_Part return Boolean is
9229 S : Entity_Id;
9231 begin
9232 S := Current_Scope;
9233 while Present (S) and then S /= Standard_Standard loop
9234 if Ekind (S) = E_Package
9235 and then Is_Generic_Instance (S)
9236 and then not In_Package_Body (S)
9237 and then not In_Private_Part (S)
9238 then
9239 return True;
9240 end if;
9242 S := Scope (S);
9243 end loop;
9245 return False;
9246 end In_Instance_Visible_Part;
9248 ---------------------
9249 -- In_Package_Body --
9250 ---------------------
9252 function In_Package_Body return Boolean is
9253 S : Entity_Id;
9255 begin
9256 S := Current_Scope;
9257 while Present (S) and then S /= Standard_Standard loop
9258 if Ekind (S) = E_Package and then In_Package_Body (S) then
9259 return True;
9260 else
9261 S := Scope (S);
9262 end if;
9263 end loop;
9265 return False;
9266 end In_Package_Body;
9268 --------------------------------
9269 -- In_Parameter_Specification --
9270 --------------------------------
9272 function In_Parameter_Specification (N : Node_Id) return Boolean is
9273 PN : Node_Id;
9275 begin
9276 PN := Parent (N);
9277 while Present (PN) loop
9278 if Nkind (PN) = N_Parameter_Specification then
9279 return True;
9280 end if;
9282 PN := Parent (PN);
9283 end loop;
9285 return False;
9286 end In_Parameter_Specification;
9288 --------------------------
9289 -- In_Pragma_Expression --
9290 --------------------------
9292 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
9293 P : Node_Id;
9294 begin
9295 P := Parent (N);
9296 loop
9297 if No (P) then
9298 return False;
9299 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
9300 return True;
9301 else
9302 P := Parent (P);
9303 end if;
9304 end loop;
9305 end In_Pragma_Expression;
9307 -------------------------------------
9308 -- In_Reverse_Storage_Order_Object --
9309 -------------------------------------
9311 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
9312 Pref : Node_Id;
9313 Btyp : Entity_Id := Empty;
9315 begin
9316 -- Climb up indexed components
9318 Pref := N;
9319 loop
9320 case Nkind (Pref) is
9321 when N_Selected_Component =>
9322 Pref := Prefix (Pref);
9323 exit;
9325 when N_Indexed_Component =>
9326 Pref := Prefix (Pref);
9328 when others =>
9329 Pref := Empty;
9330 exit;
9331 end case;
9332 end loop;
9334 if Present (Pref) then
9335 Btyp := Base_Type (Etype (Pref));
9336 end if;
9338 return Present (Btyp)
9339 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
9340 and then Reverse_Storage_Order (Btyp);
9341 end In_Reverse_Storage_Order_Object;
9343 --------------------------------------
9344 -- In_Subprogram_Or_Concurrent_Unit --
9345 --------------------------------------
9347 function In_Subprogram_Or_Concurrent_Unit return Boolean is
9348 E : Entity_Id;
9349 K : Entity_Kind;
9351 begin
9352 -- Use scope chain to check successively outer scopes
9354 E := Current_Scope;
9355 loop
9356 K := Ekind (E);
9358 if K in Subprogram_Kind
9359 or else K in Concurrent_Kind
9360 or else K in Generic_Subprogram_Kind
9361 then
9362 return True;
9364 elsif E = Standard_Standard then
9365 return False;
9366 end if;
9368 E := Scope (E);
9369 end loop;
9370 end In_Subprogram_Or_Concurrent_Unit;
9372 ---------------------
9373 -- In_Visible_Part --
9374 ---------------------
9376 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
9377 begin
9378 return Is_Package_Or_Generic_Package (Scope_Id)
9379 and then In_Open_Scopes (Scope_Id)
9380 and then not In_Package_Body (Scope_Id)
9381 and then not In_Private_Part (Scope_Id);
9382 end In_Visible_Part;
9384 --------------------------------
9385 -- Incomplete_Or_Partial_View --
9386 --------------------------------
9388 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
9389 function Inspect_Decls
9390 (Decls : List_Id;
9391 Taft : Boolean := False) return Entity_Id;
9392 -- Check whether a declarative region contains the incomplete or partial
9393 -- view of Id.
9395 -------------------
9396 -- Inspect_Decls --
9397 -------------------
9399 function Inspect_Decls
9400 (Decls : List_Id;
9401 Taft : Boolean := False) return Entity_Id
9403 Decl : Node_Id;
9404 Match : Node_Id;
9406 begin
9407 Decl := First (Decls);
9408 while Present (Decl) loop
9409 Match := Empty;
9411 if Taft then
9412 if Nkind (Decl) = N_Incomplete_Type_Declaration then
9413 Match := Defining_Identifier (Decl);
9414 end if;
9416 else
9417 if Nkind_In (Decl, N_Private_Extension_Declaration,
9418 N_Private_Type_Declaration)
9419 then
9420 Match := Defining_Identifier (Decl);
9421 end if;
9422 end if;
9424 if Present (Match)
9425 and then Present (Full_View (Match))
9426 and then Full_View (Match) = Id
9427 then
9428 return Match;
9429 end if;
9431 Next (Decl);
9432 end loop;
9434 return Empty;
9435 end Inspect_Decls;
9437 -- Local variables
9439 Prev : Entity_Id;
9441 -- Start of processing for Incomplete_Or_Partial_View
9443 begin
9444 -- Deferred constant or incomplete type case
9446 Prev := Current_Entity_In_Scope (Id);
9448 if Present (Prev)
9449 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
9450 and then Present (Full_View (Prev))
9451 and then Full_View (Prev) = Id
9452 then
9453 return Prev;
9454 end if;
9456 -- Private or Taft amendment type case
9458 declare
9459 Pkg : constant Entity_Id := Scope (Id);
9460 Pkg_Decl : Node_Id := Pkg;
9462 begin
9463 if Ekind (Pkg) = E_Package then
9464 while Nkind (Pkg_Decl) /= N_Package_Specification loop
9465 Pkg_Decl := Parent (Pkg_Decl);
9466 end loop;
9468 -- It is knows that Typ has a private view, look for it in the
9469 -- visible declarations of the enclosing scope. A special case
9470 -- of this is when the two views have been exchanged - the full
9471 -- appears earlier than the private.
9473 if Has_Private_Declaration (Id) then
9474 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
9476 -- Exchanged view case, look in the private declarations
9478 if No (Prev) then
9479 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
9480 end if;
9482 return Prev;
9484 -- Otherwise if this is the package body, then Typ is a potential
9485 -- Taft amendment type. The incomplete view should be located in
9486 -- the private declarations of the enclosing scope.
9488 elsif In_Package_Body (Pkg) then
9489 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
9490 end if;
9491 end if;
9492 end;
9494 -- The type has no incomplete or private view
9496 return Empty;
9497 end Incomplete_Or_Partial_View;
9499 -----------------------------------------
9500 -- Inherit_Default_Init_Cond_Procedure --
9501 -----------------------------------------
9503 procedure Inherit_Default_Init_Cond_Procedure (Typ : Entity_Id) is
9504 Par_Typ : constant Entity_Id := Etype (Typ);
9506 begin
9507 -- A derived type inherits the default initial condition procedure of
9508 -- its parent type.
9510 if No (Default_Init_Cond_Procedure (Typ)) then
9511 Set_Default_Init_Cond_Procedure
9512 (Typ, Default_Init_Cond_Procedure (Par_Typ));
9513 end if;
9514 end Inherit_Default_Init_Cond_Procedure;
9516 ----------------------------
9517 -- Inherit_Rep_Item_Chain --
9518 ----------------------------
9520 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
9521 From_Item : constant Node_Id := First_Rep_Item (From_Typ);
9522 Item : Node_Id := Empty;
9523 Last_Item : Node_Id := Empty;
9525 begin
9526 -- Reach the end of the destination type's chain (if any) and capture
9527 -- the last item.
9529 Item := First_Rep_Item (Typ);
9530 while Present (Item) loop
9532 -- Do not inherit a chain that has been inherited already
9534 if Item = From_Item then
9535 return;
9536 end if;
9538 Last_Item := Item;
9539 Item := Next_Rep_Item (Item);
9540 end loop;
9542 -- When the destination type has a rep item chain, the chain of the
9543 -- source type is appended to it.
9545 if Present (Last_Item) then
9546 Set_Next_Rep_Item (Last_Item, From_Item);
9548 -- Otherwise the destination type directly inherits the rep item chain
9549 -- of the source type (if any).
9551 else
9552 Set_First_Rep_Item (Typ, From_Item);
9553 end if;
9554 end Inherit_Rep_Item_Chain;
9556 ---------------------------------
9557 -- Inherit_Subprogram_Contract --
9558 ---------------------------------
9560 procedure Inherit_Subprogram_Contract
9561 (Subp : Entity_Id;
9562 From_Subp : Entity_Id)
9564 procedure Inherit_Pragma (Prag_Id : Pragma_Id);
9565 -- Propagate a pragma denoted by Prag_Id from From_Subp's contract to
9566 -- Subp's contract.
9568 --------------------
9569 -- Inherit_Pragma --
9570 --------------------
9572 procedure Inherit_Pragma (Prag_Id : Pragma_Id) is
9573 Prag : constant Node_Id := Get_Pragma (From_Subp, Prag_Id);
9574 New_Prag : Node_Id;
9576 begin
9577 -- A pragma cannot be part of more than one First_Pragma/Next_Pragma
9578 -- chains, therefore the node must be replicated. The new pragma is
9579 -- flagged is inherited for distrinction purposes.
9581 if Present (Prag) then
9582 New_Prag := New_Copy_Tree (Prag);
9583 Set_Is_Inherited (New_Prag);
9585 Add_Contract_Item (New_Prag, Subp);
9586 end if;
9587 end Inherit_Pragma;
9589 -- Start of processing for Inherit_Subprogram_Contract
9591 begin
9592 -- Inheritance is carried out only when both entities are subprograms
9593 -- with contracts.
9595 if Is_Subprogram_Or_Generic_Subprogram (Subp)
9596 and then Is_Subprogram_Or_Generic_Subprogram (From_Subp)
9597 and then Present (Contract (Subp))
9598 and then Present (Contract (From_Subp))
9599 then
9600 Inherit_Pragma (Pragma_Extensions_Visible);
9601 end if;
9602 end Inherit_Subprogram_Contract;
9604 ---------------------------------
9605 -- Insert_Explicit_Dereference --
9606 ---------------------------------
9608 procedure Insert_Explicit_Dereference (N : Node_Id) is
9609 New_Prefix : constant Node_Id := Relocate_Node (N);
9610 Ent : Entity_Id := Empty;
9611 Pref : Node_Id;
9612 I : Interp_Index;
9613 It : Interp;
9614 T : Entity_Id;
9616 begin
9617 Save_Interps (N, New_Prefix);
9619 Rewrite (N,
9620 Make_Explicit_Dereference (Sloc (Parent (N)),
9621 Prefix => New_Prefix));
9623 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
9625 if Is_Overloaded (New_Prefix) then
9627 -- The dereference is also overloaded, and its interpretations are
9628 -- the designated types of the interpretations of the original node.
9630 Set_Etype (N, Any_Type);
9632 Get_First_Interp (New_Prefix, I, It);
9633 while Present (It.Nam) loop
9634 T := It.Typ;
9636 if Is_Access_Type (T) then
9637 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
9638 end if;
9640 Get_Next_Interp (I, It);
9641 end loop;
9643 End_Interp_List;
9645 else
9646 -- Prefix is unambiguous: mark the original prefix (which might
9647 -- Come_From_Source) as a reference, since the new (relocated) one
9648 -- won't be taken into account.
9650 if Is_Entity_Name (New_Prefix) then
9651 Ent := Entity (New_Prefix);
9652 Pref := New_Prefix;
9654 -- For a retrieval of a subcomponent of some composite object,
9655 -- retrieve the ultimate entity if there is one.
9657 elsif Nkind_In (New_Prefix, N_Selected_Component,
9658 N_Indexed_Component)
9659 then
9660 Pref := Prefix (New_Prefix);
9661 while Present (Pref)
9662 and then Nkind_In (Pref, N_Selected_Component,
9663 N_Indexed_Component)
9664 loop
9665 Pref := Prefix (Pref);
9666 end loop;
9668 if Present (Pref) and then Is_Entity_Name (Pref) then
9669 Ent := Entity (Pref);
9670 end if;
9671 end if;
9673 -- Place the reference on the entity node
9675 if Present (Ent) then
9676 Generate_Reference (Ent, Pref);
9677 end if;
9678 end if;
9679 end Insert_Explicit_Dereference;
9681 ------------------------------------------
9682 -- Inspect_Deferred_Constant_Completion --
9683 ------------------------------------------
9685 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
9686 Decl : Node_Id;
9688 begin
9689 Decl := First (Decls);
9690 while Present (Decl) loop
9692 -- Deferred constant signature
9694 if Nkind (Decl) = N_Object_Declaration
9695 and then Constant_Present (Decl)
9696 and then No (Expression (Decl))
9698 -- No need to check internally generated constants
9700 and then Comes_From_Source (Decl)
9702 -- The constant is not completed. A full object declaration or a
9703 -- pragma Import complete a deferred constant.
9705 and then not Has_Completion (Defining_Identifier (Decl))
9706 then
9707 Error_Msg_N
9708 ("constant declaration requires initialization expression",
9709 Defining_Identifier (Decl));
9710 end if;
9712 Decl := Next (Decl);
9713 end loop;
9714 end Inspect_Deferred_Constant_Completion;
9716 -----------------------------
9717 -- Is_Actual_Out_Parameter --
9718 -----------------------------
9720 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
9721 Formal : Entity_Id;
9722 Call : Node_Id;
9723 begin
9724 Find_Actual (N, Formal, Call);
9725 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
9726 end Is_Actual_Out_Parameter;
9728 -------------------------
9729 -- Is_Actual_Parameter --
9730 -------------------------
9732 function Is_Actual_Parameter (N : Node_Id) return Boolean is
9733 PK : constant Node_Kind := Nkind (Parent (N));
9735 begin
9736 case PK is
9737 when N_Parameter_Association =>
9738 return N = Explicit_Actual_Parameter (Parent (N));
9740 when N_Subprogram_Call =>
9741 return Is_List_Member (N)
9742 and then
9743 List_Containing (N) = Parameter_Associations (Parent (N));
9745 when others =>
9746 return False;
9747 end case;
9748 end Is_Actual_Parameter;
9750 --------------------------------
9751 -- Is_Actual_Tagged_Parameter --
9752 --------------------------------
9754 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
9755 Formal : Entity_Id;
9756 Call : Node_Id;
9757 begin
9758 Find_Actual (N, Formal, Call);
9759 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
9760 end Is_Actual_Tagged_Parameter;
9762 ---------------------
9763 -- Is_Aliased_View --
9764 ---------------------
9766 function Is_Aliased_View (Obj : Node_Id) return Boolean is
9767 E : Entity_Id;
9769 begin
9770 if Is_Entity_Name (Obj) then
9771 E := Entity (Obj);
9773 return
9774 (Is_Object (E)
9775 and then
9776 (Is_Aliased (E)
9777 or else (Present (Renamed_Object (E))
9778 and then Is_Aliased_View (Renamed_Object (E)))))
9780 or else ((Is_Formal (E)
9781 or else Ekind_In (E, E_Generic_In_Out_Parameter,
9782 E_Generic_In_Parameter))
9783 and then Is_Tagged_Type (Etype (E)))
9785 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
9787 -- Current instance of type, either directly or as rewritten
9788 -- reference to the current object.
9790 or else (Is_Entity_Name (Original_Node (Obj))
9791 and then Present (Entity (Original_Node (Obj)))
9792 and then Is_Type (Entity (Original_Node (Obj))))
9794 or else (Is_Type (E) and then E = Current_Scope)
9796 or else (Is_Incomplete_Or_Private_Type (E)
9797 and then Full_View (E) = Current_Scope)
9799 -- Ada 2012 AI05-0053: the return object of an extended return
9800 -- statement is aliased if its type is immutably limited.
9802 or else (Is_Return_Object (E)
9803 and then Is_Limited_View (Etype (E)));
9805 elsif Nkind (Obj) = N_Selected_Component then
9806 return Is_Aliased (Entity (Selector_Name (Obj)));
9808 elsif Nkind (Obj) = N_Indexed_Component then
9809 return Has_Aliased_Components (Etype (Prefix (Obj)))
9810 or else
9811 (Is_Access_Type (Etype (Prefix (Obj)))
9812 and then Has_Aliased_Components
9813 (Designated_Type (Etype (Prefix (Obj)))));
9815 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
9816 return Is_Tagged_Type (Etype (Obj))
9817 and then Is_Aliased_View (Expression (Obj));
9819 elsif Nkind (Obj) = N_Explicit_Dereference then
9820 return Nkind (Original_Node (Obj)) /= N_Function_Call;
9822 else
9823 return False;
9824 end if;
9825 end Is_Aliased_View;
9827 -------------------------
9828 -- Is_Ancestor_Package --
9829 -------------------------
9831 function Is_Ancestor_Package
9832 (E1 : Entity_Id;
9833 E2 : Entity_Id) return Boolean
9835 Par : Entity_Id;
9837 begin
9838 Par := E2;
9839 while Present (Par) and then Par /= Standard_Standard loop
9840 if Par = E1 then
9841 return True;
9842 end if;
9844 Par := Scope (Par);
9845 end loop;
9847 return False;
9848 end Is_Ancestor_Package;
9850 ----------------------
9851 -- Is_Atomic_Object --
9852 ----------------------
9854 function Is_Atomic_Object (N : Node_Id) return Boolean is
9856 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
9857 -- Determines if given object has atomic components
9859 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
9860 -- If prefix is an implicit dereference, examine designated type
9862 ----------------------
9863 -- Is_Atomic_Prefix --
9864 ----------------------
9866 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
9867 begin
9868 if Is_Access_Type (Etype (N)) then
9869 return
9870 Has_Atomic_Components (Designated_Type (Etype (N)));
9871 else
9872 return Object_Has_Atomic_Components (N);
9873 end if;
9874 end Is_Atomic_Prefix;
9876 ----------------------------------
9877 -- Object_Has_Atomic_Components --
9878 ----------------------------------
9880 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
9881 begin
9882 if Has_Atomic_Components (Etype (N))
9883 or else Is_Atomic (Etype (N))
9884 then
9885 return True;
9887 elsif Is_Entity_Name (N)
9888 and then (Has_Atomic_Components (Entity (N))
9889 or else Is_Atomic (Entity (N)))
9890 then
9891 return True;
9893 elsif Nkind (N) = N_Selected_Component
9894 and then Is_Atomic (Entity (Selector_Name (N)))
9895 then
9896 return True;
9898 elsif Nkind (N) = N_Indexed_Component
9899 or else Nkind (N) = N_Selected_Component
9900 then
9901 return Is_Atomic_Prefix (Prefix (N));
9903 else
9904 return False;
9905 end if;
9906 end Object_Has_Atomic_Components;
9908 -- Start of processing for Is_Atomic_Object
9910 begin
9911 -- Predicate is not relevant to subprograms
9913 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
9914 return False;
9916 elsif Is_Atomic (Etype (N))
9917 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
9918 then
9919 return True;
9921 elsif Nkind (N) = N_Selected_Component
9922 and then Is_Atomic (Entity (Selector_Name (N)))
9923 then
9924 return True;
9926 elsif Nkind (N) = N_Indexed_Component
9927 or else Nkind (N) = N_Selected_Component
9928 then
9929 return Is_Atomic_Prefix (Prefix (N));
9931 else
9932 return False;
9933 end if;
9934 end Is_Atomic_Object;
9936 -------------------------
9937 -- Is_Attribute_Result --
9938 -------------------------
9940 function Is_Attribute_Result (N : Node_Id) return Boolean is
9941 begin
9942 return Nkind (N) = N_Attribute_Reference
9943 and then Attribute_Name (N) = Name_Result;
9944 end Is_Attribute_Result;
9946 ------------------------------------
9947 -- Is_Body_Or_Package_Declaration --
9948 ------------------------------------
9950 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
9951 begin
9952 return Nkind_In (N, N_Entry_Body,
9953 N_Package_Body,
9954 N_Package_Declaration,
9955 N_Protected_Body,
9956 N_Subprogram_Body,
9957 N_Task_Body);
9958 end Is_Body_Or_Package_Declaration;
9960 -----------------------
9961 -- Is_Bounded_String --
9962 -----------------------
9964 function Is_Bounded_String (T : Entity_Id) return Boolean is
9965 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
9967 begin
9968 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
9969 -- Super_String, or one of the [Wide_]Wide_ versions. This will
9970 -- be True for all the Bounded_String types in instances of the
9971 -- Generic_Bounded_Length generics, and for types derived from those.
9973 return Present (Under)
9974 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
9975 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
9976 Is_RTE (Root_Type (Under), RO_WW_Super_String));
9977 end Is_Bounded_String;
9979 -------------------------
9980 -- Is_Child_Or_Sibling --
9981 -------------------------
9983 function Is_Child_Or_Sibling
9984 (Pack_1 : Entity_Id;
9985 Pack_2 : Entity_Id) return Boolean
9987 function Distance_From_Standard (Pack : Entity_Id) return Nat;
9988 -- Given an arbitrary package, return the number of "climbs" necessary
9989 -- to reach scope Standard_Standard.
9991 procedure Equalize_Depths
9992 (Pack : in out Entity_Id;
9993 Depth : in out Nat;
9994 Depth_To_Reach : Nat);
9995 -- Given an arbitrary package, its depth and a target depth to reach,
9996 -- climb the scope chain until the said depth is reached. The pointer
9997 -- to the package and its depth a modified during the climb.
9999 ----------------------------
10000 -- Distance_From_Standard --
10001 ----------------------------
10003 function Distance_From_Standard (Pack : Entity_Id) return Nat is
10004 Dist : Nat;
10005 Scop : Entity_Id;
10007 begin
10008 Dist := 0;
10009 Scop := Pack;
10010 while Present (Scop) and then Scop /= Standard_Standard loop
10011 Dist := Dist + 1;
10012 Scop := Scope (Scop);
10013 end loop;
10015 return Dist;
10016 end Distance_From_Standard;
10018 ---------------------
10019 -- Equalize_Depths --
10020 ---------------------
10022 procedure Equalize_Depths
10023 (Pack : in out Entity_Id;
10024 Depth : in out Nat;
10025 Depth_To_Reach : Nat)
10027 begin
10028 -- The package must be at a greater or equal depth
10030 if Depth < Depth_To_Reach then
10031 raise Program_Error;
10032 end if;
10034 -- Climb the scope chain until the desired depth is reached
10036 while Present (Pack) and then Depth /= Depth_To_Reach loop
10037 Pack := Scope (Pack);
10038 Depth := Depth - 1;
10039 end loop;
10040 end Equalize_Depths;
10042 -- Local variables
10044 P_1 : Entity_Id := Pack_1;
10045 P_1_Child : Boolean := False;
10046 P_1_Depth : Nat := Distance_From_Standard (P_1);
10047 P_2 : Entity_Id := Pack_2;
10048 P_2_Child : Boolean := False;
10049 P_2_Depth : Nat := Distance_From_Standard (P_2);
10051 -- Start of processing for Is_Child_Or_Sibling
10053 begin
10054 pragma Assert
10055 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
10057 -- Both packages denote the same entity, therefore they cannot be
10058 -- children or siblings.
10060 if P_1 = P_2 then
10061 return False;
10063 -- One of the packages is at a deeper level than the other. Note that
10064 -- both may still come from differen hierarchies.
10066 -- (root) P_2
10067 -- / \ :
10068 -- X P_2 or X
10069 -- : :
10070 -- P_1 P_1
10072 elsif P_1_Depth > P_2_Depth then
10073 Equalize_Depths
10074 (Pack => P_1,
10075 Depth => P_1_Depth,
10076 Depth_To_Reach => P_2_Depth);
10077 P_1_Child := True;
10079 -- (root) P_1
10080 -- / \ :
10081 -- P_1 X or X
10082 -- : :
10083 -- P_2 P_2
10085 elsif P_2_Depth > P_1_Depth then
10086 Equalize_Depths
10087 (Pack => P_2,
10088 Depth => P_2_Depth,
10089 Depth_To_Reach => P_1_Depth);
10090 P_2_Child := True;
10091 end if;
10093 -- At this stage the package pointers have been elevated to the same
10094 -- depth. If the related entities are the same, then one package is a
10095 -- potential child of the other:
10097 -- P_1
10098 -- :
10099 -- X became P_1 P_2 or vica versa
10100 -- :
10101 -- P_2
10103 if P_1 = P_2 then
10104 if P_1_Child then
10105 return Is_Child_Unit (Pack_1);
10107 else pragma Assert (P_2_Child);
10108 return Is_Child_Unit (Pack_2);
10109 end if;
10111 -- The packages may come from the same package chain or from entirely
10112 -- different hierarcies. To determine this, climb the scope stack until
10113 -- a common root is found.
10115 -- (root) (root 1) (root 2)
10116 -- / \ | |
10117 -- P_1 P_2 P_1 P_2
10119 else
10120 while Present (P_1) and then Present (P_2) loop
10122 -- The two packages may be siblings
10124 if P_1 = P_2 then
10125 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
10126 end if;
10128 P_1 := Scope (P_1);
10129 P_2 := Scope (P_2);
10130 end loop;
10131 end if;
10133 return False;
10134 end Is_Child_Or_Sibling;
10136 -----------------------------
10137 -- Is_Concurrent_Interface --
10138 -----------------------------
10140 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
10141 begin
10142 return Is_Interface (T)
10143 and then
10144 (Is_Protected_Interface (T)
10145 or else Is_Synchronized_Interface (T)
10146 or else Is_Task_Interface (T));
10147 end Is_Concurrent_Interface;
10149 ---------------------------
10150 -- Is_Container_Element --
10151 ---------------------------
10153 function Is_Container_Element (Exp : Node_Id) return Boolean is
10154 Loc : constant Source_Ptr := Sloc (Exp);
10155 Pref : constant Node_Id := Prefix (Exp);
10157 Call : Node_Id;
10158 -- Call to an indexing aspect
10160 Cont_Typ : Entity_Id;
10161 -- The type of the container being accessed
10163 Elem_Typ : Entity_Id;
10164 -- Its element type
10166 Indexing : Entity_Id;
10167 Is_Const : Boolean;
10168 -- Indicates that constant indexing is used, and the element is thus
10169 -- a constant.
10171 Ref_Typ : Entity_Id;
10172 -- The reference type returned by the indexing operation
10174 begin
10175 -- If C is a container, in a context that imposes the element type of
10176 -- that container, the indexing notation C (X) is rewritten as:
10178 -- Indexing (C, X).Discr.all
10180 -- where Indexing is one of the indexing aspects of the container.
10181 -- If the context does not require a reference, the construct can be
10182 -- rewritten as
10184 -- Element (C, X)
10186 -- First, verify that the construct has the proper form
10188 if not Expander_Active then
10189 return False;
10191 elsif Nkind (Pref) /= N_Selected_Component then
10192 return False;
10194 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
10195 return False;
10197 else
10198 Call := Prefix (Pref);
10199 Ref_Typ := Etype (Call);
10200 end if;
10202 if not Has_Implicit_Dereference (Ref_Typ)
10203 or else No (First (Parameter_Associations (Call)))
10204 or else not Is_Entity_Name (Name (Call))
10205 then
10206 return False;
10207 end if;
10209 -- Retrieve type of container object, and its iterator aspects
10211 Cont_Typ := Etype (First (Parameter_Associations (Call)));
10212 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
10213 Is_Const := False;
10215 if No (Indexing) then
10217 -- Container should have at least one indexing operation
10219 return False;
10221 elsif Entity (Name (Call)) /= Entity (Indexing) then
10223 -- This may be a variable indexing operation
10225 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
10227 if No (Indexing)
10228 or else Entity (Name (Call)) /= Entity (Indexing)
10229 then
10230 return False;
10231 end if;
10233 else
10234 Is_Const := True;
10235 end if;
10237 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
10239 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
10240 return False;
10241 end if;
10243 -- Check that the expression is not the target of an assignment, in
10244 -- which case the rewriting is not possible.
10246 if not Is_Const then
10247 declare
10248 Par : Node_Id;
10250 begin
10251 Par := Exp;
10252 while Present (Par)
10253 loop
10254 if Nkind (Parent (Par)) = N_Assignment_Statement
10255 and then Par = Name (Parent (Par))
10256 then
10257 return False;
10259 -- A renaming produces a reference, and the transformation
10260 -- does not apply.
10262 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
10263 return False;
10265 elsif Nkind_In
10266 (Nkind (Parent (Par)), N_Function_Call,
10267 N_Procedure_Call_Statement,
10268 N_Entry_Call_Statement)
10269 then
10270 -- Check that the element is not part of an actual for an
10271 -- in-out parameter.
10273 declare
10274 F : Entity_Id;
10275 A : Node_Id;
10277 begin
10278 F := First_Formal (Entity (Name (Parent (Par))));
10279 A := First (Parameter_Associations (Parent (Par)));
10280 while Present (F) loop
10281 if A = Par and then Ekind (F) /= E_In_Parameter then
10282 return False;
10283 end if;
10285 Next_Formal (F);
10286 Next (A);
10287 end loop;
10288 end;
10290 -- E_In_Parameter in a call: element is not modified.
10292 exit;
10293 end if;
10295 Par := Parent (Par);
10296 end loop;
10297 end;
10298 end if;
10300 -- The expression has the proper form and the context requires the
10301 -- element type. Retrieve the Element function of the container and
10302 -- rewrite the construct as a call to it.
10304 declare
10305 Op : Elmt_Id;
10307 begin
10308 Op := First_Elmt (Primitive_Operations (Cont_Typ));
10309 while Present (Op) loop
10310 exit when Chars (Node (Op)) = Name_Element;
10311 Next_Elmt (Op);
10312 end loop;
10314 if No (Op) then
10315 return False;
10317 else
10318 Rewrite (Exp,
10319 Make_Function_Call (Loc,
10320 Name => New_Occurrence_Of (Node (Op), Loc),
10321 Parameter_Associations => Parameter_Associations (Call)));
10322 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
10323 return True;
10324 end if;
10325 end;
10326 end Is_Container_Element;
10328 -----------------------
10329 -- Is_Constant_Bound --
10330 -----------------------
10332 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
10333 begin
10334 if Compile_Time_Known_Value (Exp) then
10335 return True;
10337 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
10338 return Is_Constant_Object (Entity (Exp))
10339 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
10341 elsif Nkind (Exp) in N_Binary_Op then
10342 return Is_Constant_Bound (Left_Opnd (Exp))
10343 and then Is_Constant_Bound (Right_Opnd (Exp))
10344 and then Scope (Entity (Exp)) = Standard_Standard;
10346 else
10347 return False;
10348 end if;
10349 end Is_Constant_Bound;
10351 --------------------------------------
10352 -- Is_Controlling_Limited_Procedure --
10353 --------------------------------------
10355 function Is_Controlling_Limited_Procedure
10356 (Proc_Nam : Entity_Id) return Boolean
10358 Param_Typ : Entity_Id := Empty;
10360 begin
10361 if Ekind (Proc_Nam) = E_Procedure
10362 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
10363 then
10364 Param_Typ := Etype (Parameter_Type (First (
10365 Parameter_Specifications (Parent (Proc_Nam)))));
10367 -- In this case where an Itype was created, the procedure call has been
10368 -- rewritten.
10370 elsif Present (Associated_Node_For_Itype (Proc_Nam))
10371 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
10372 and then
10373 Present (Parameter_Associations
10374 (Associated_Node_For_Itype (Proc_Nam)))
10375 then
10376 Param_Typ :=
10377 Etype (First (Parameter_Associations
10378 (Associated_Node_For_Itype (Proc_Nam))));
10379 end if;
10381 if Present (Param_Typ) then
10382 return
10383 Is_Interface (Param_Typ)
10384 and then Is_Limited_Record (Param_Typ);
10385 end if;
10387 return False;
10388 end Is_Controlling_Limited_Procedure;
10390 -----------------------------
10391 -- Is_CPP_Constructor_Call --
10392 -----------------------------
10394 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
10395 begin
10396 return Nkind (N) = N_Function_Call
10397 and then Is_CPP_Class (Etype (Etype (N)))
10398 and then Is_Constructor (Entity (Name (N)))
10399 and then Is_Imported (Entity (Name (N)));
10400 end Is_CPP_Constructor_Call;
10402 -----------------
10403 -- Is_Delegate --
10404 -----------------
10406 function Is_Delegate (T : Entity_Id) return Boolean is
10407 Desig_Type : Entity_Id;
10409 begin
10410 if VM_Target /= CLI_Target then
10411 return False;
10412 end if;
10414 -- Access-to-subprograms are delegates in CIL
10416 if Ekind (T) = E_Access_Subprogram_Type then
10417 return True;
10418 end if;
10420 if not Is_Access_Type (T) then
10422 -- A delegate is a managed pointer. If no designated type is defined
10423 -- it means that it's not a delegate.
10425 return False;
10426 end if;
10428 Desig_Type := Etype (Directly_Designated_Type (T));
10430 if not Is_Tagged_Type (Desig_Type) then
10431 return False;
10432 end if;
10434 -- Test if the type is inherited from [mscorlib]System.Delegate
10436 while Etype (Desig_Type) /= Desig_Type loop
10437 if Chars (Scope (Desig_Type)) /= No_Name
10438 and then Is_Imported (Scope (Desig_Type))
10439 and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
10440 then
10441 return True;
10442 end if;
10444 Desig_Type := Etype (Desig_Type);
10445 end loop;
10447 return False;
10448 end Is_Delegate;
10450 ----------------------------------------------
10451 -- Is_Dependent_Component_Of_Mutable_Object --
10452 ----------------------------------------------
10454 function Is_Dependent_Component_Of_Mutable_Object
10455 (Object : Node_Id) return Boolean
10457 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
10458 -- Returns True if and only if Comp is declared within a variant part
10460 --------------------------------
10461 -- Is_Declared_Within_Variant --
10462 --------------------------------
10464 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
10465 Comp_Decl : constant Node_Id := Parent (Comp);
10466 Comp_List : constant Node_Id := Parent (Comp_Decl);
10467 begin
10468 return Nkind (Parent (Comp_List)) = N_Variant;
10469 end Is_Declared_Within_Variant;
10471 P : Node_Id;
10472 Prefix_Type : Entity_Id;
10473 P_Aliased : Boolean := False;
10474 Comp : Entity_Id;
10476 Deref : Node_Id := Object;
10477 -- Dereference node, in something like X.all.Y(2)
10479 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
10481 begin
10482 -- Find the dereference node if any
10484 while Nkind_In (Deref, N_Indexed_Component,
10485 N_Selected_Component,
10486 N_Slice)
10487 loop
10488 Deref := Prefix (Deref);
10489 end loop;
10491 -- Ada 2005: If we have a component or slice of a dereference,
10492 -- something like X.all.Y (2), and the type of X is access-to-constant,
10493 -- Is_Variable will return False, because it is indeed a constant
10494 -- view. But it might be a view of a variable object, so we want the
10495 -- following condition to be True in that case.
10497 if Is_Variable (Object)
10498 or else (Ada_Version >= Ada_2005
10499 and then Nkind (Deref) = N_Explicit_Dereference)
10500 then
10501 if Nkind (Object) = N_Selected_Component then
10502 P := Prefix (Object);
10503 Prefix_Type := Etype (P);
10505 if Is_Entity_Name (P) then
10506 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
10507 Prefix_Type := Base_Type (Prefix_Type);
10508 end if;
10510 if Is_Aliased (Entity (P)) then
10511 P_Aliased := True;
10512 end if;
10514 -- A discriminant check on a selected component may be expanded
10515 -- into a dereference when removing side-effects. Recover the
10516 -- original node and its type, which may be unconstrained.
10518 elsif Nkind (P) = N_Explicit_Dereference
10519 and then not (Comes_From_Source (P))
10520 then
10521 P := Original_Node (P);
10522 Prefix_Type := Etype (P);
10524 else
10525 -- Check for prefix being an aliased component???
10527 null;
10529 end if;
10531 -- A heap object is constrained by its initial value
10533 -- Ada 2005 (AI-363): Always assume the object could be mutable in
10534 -- the dereferenced case, since the access value might denote an
10535 -- unconstrained aliased object, whereas in Ada 95 the designated
10536 -- object is guaranteed to be constrained. A worst-case assumption
10537 -- has to apply in Ada 2005 because we can't tell at compile
10538 -- time whether the object is "constrained by its initial value"
10539 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
10540 -- rules (these rules are acknowledged to need fixing).
10542 if Ada_Version < Ada_2005 then
10543 if Is_Access_Type (Prefix_Type)
10544 or else Nkind (P) = N_Explicit_Dereference
10545 then
10546 return False;
10547 end if;
10549 else pragma Assert (Ada_Version >= Ada_2005);
10550 if Is_Access_Type (Prefix_Type) then
10552 -- If the access type is pool-specific, and there is no
10553 -- constrained partial view of the designated type, then the
10554 -- designated object is known to be constrained.
10556 if Ekind (Prefix_Type) = E_Access_Type
10557 and then not Object_Type_Has_Constrained_Partial_View
10558 (Typ => Designated_Type (Prefix_Type),
10559 Scop => Current_Scope)
10560 then
10561 return False;
10563 -- Otherwise (general access type, or there is a constrained
10564 -- partial view of the designated type), we need to check
10565 -- based on the designated type.
10567 else
10568 Prefix_Type := Designated_Type (Prefix_Type);
10569 end if;
10570 end if;
10571 end if;
10573 Comp :=
10574 Original_Record_Component (Entity (Selector_Name (Object)));
10576 -- As per AI-0017, the renaming is illegal in a generic body, even
10577 -- if the subtype is indefinite.
10579 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
10581 if not Is_Constrained (Prefix_Type)
10582 and then (not Is_Indefinite_Subtype (Prefix_Type)
10583 or else
10584 (Is_Generic_Type (Prefix_Type)
10585 and then Ekind (Current_Scope) = E_Generic_Package
10586 and then In_Package_Body (Current_Scope)))
10588 and then (Is_Declared_Within_Variant (Comp)
10589 or else Has_Discriminant_Dependent_Constraint (Comp))
10590 and then (not P_Aliased or else Ada_Version >= Ada_2005)
10591 then
10592 return True;
10594 -- If the prefix is of an access type at this point, then we want
10595 -- to return False, rather than calling this function recursively
10596 -- on the access object (which itself might be a discriminant-
10597 -- dependent component of some other object, but that isn't
10598 -- relevant to checking the object passed to us). This avoids
10599 -- issuing wrong errors when compiling with -gnatc, where there
10600 -- can be implicit dereferences that have not been expanded.
10602 elsif Is_Access_Type (Etype (Prefix (Object))) then
10603 return False;
10605 else
10606 return
10607 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10608 end if;
10610 elsif Nkind (Object) = N_Indexed_Component
10611 or else Nkind (Object) = N_Slice
10612 then
10613 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10615 -- A type conversion that Is_Variable is a view conversion:
10616 -- go back to the denoted object.
10618 elsif Nkind (Object) = N_Type_Conversion then
10619 return
10620 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
10621 end if;
10622 end if;
10624 return False;
10625 end Is_Dependent_Component_Of_Mutable_Object;
10627 ---------------------
10628 -- Is_Dereferenced --
10629 ---------------------
10631 function Is_Dereferenced (N : Node_Id) return Boolean is
10632 P : constant Node_Id := Parent (N);
10633 begin
10634 return Nkind_In (P, N_Selected_Component,
10635 N_Explicit_Dereference,
10636 N_Indexed_Component,
10637 N_Slice)
10638 and then Prefix (P) = N;
10639 end Is_Dereferenced;
10641 ----------------------
10642 -- Is_Descendent_Of --
10643 ----------------------
10645 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
10646 T : Entity_Id;
10647 Etyp : Entity_Id;
10649 begin
10650 pragma Assert (Nkind (T1) in N_Entity);
10651 pragma Assert (Nkind (T2) in N_Entity);
10653 T := Base_Type (T1);
10655 -- Immediate return if the types match
10657 if T = T2 then
10658 return True;
10660 -- Comment needed here ???
10662 elsif Ekind (T) = E_Class_Wide_Type then
10663 return Etype (T) = T2;
10665 -- All other cases
10667 else
10668 loop
10669 Etyp := Etype (T);
10671 -- Done if we found the type we are looking for
10673 if Etyp = T2 then
10674 return True;
10676 -- Done if no more derivations to check
10678 elsif T = T1
10679 or else T = Etyp
10680 then
10681 return False;
10683 -- Following test catches error cases resulting from prev errors
10685 elsif No (Etyp) then
10686 return False;
10688 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
10689 return False;
10691 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
10692 return False;
10693 end if;
10695 T := Base_Type (Etyp);
10696 end loop;
10697 end if;
10698 end Is_Descendent_Of;
10700 -----------------------------
10701 -- Is_Effectively_Volatile --
10702 -----------------------------
10704 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
10705 begin
10706 if Is_Type (Id) then
10708 -- An arbitrary type is effectively volatile when it is subject to
10709 -- pragma Atomic or Volatile.
10711 if Is_Volatile (Id) then
10712 return True;
10714 -- An array type is effectively volatile when it is subject to pragma
10715 -- Atomic_Components or Volatile_Components or its compolent type is
10716 -- effectively volatile.
10718 elsif Is_Array_Type (Id) then
10719 return
10720 Has_Volatile_Components (Id)
10721 or else
10722 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
10724 else
10725 return False;
10726 end if;
10728 -- Otherwise Id denotes an object
10730 else
10731 return
10732 Is_Volatile (Id)
10733 or else Has_Volatile_Components (Id)
10734 or else Is_Effectively_Volatile (Etype (Id));
10735 end if;
10736 end Is_Effectively_Volatile;
10738 ------------------------------------
10739 -- Is_Effectively_Volatile_Object --
10740 ------------------------------------
10742 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
10743 begin
10744 if Is_Entity_Name (N) then
10745 return Is_Effectively_Volatile (Entity (N));
10747 elsif Nkind (N) = N_Expanded_Name then
10748 return Is_Effectively_Volatile (Entity (N));
10750 elsif Nkind (N) = N_Indexed_Component then
10751 return Is_Effectively_Volatile_Object (Prefix (N));
10753 elsif Nkind (N) = N_Selected_Component then
10754 return
10755 Is_Effectively_Volatile_Object (Prefix (N))
10756 or else
10757 Is_Effectively_Volatile_Object (Selector_Name (N));
10759 else
10760 return False;
10761 end if;
10762 end Is_Effectively_Volatile_Object;
10764 ----------------------------
10765 -- Is_Expression_Function --
10766 ----------------------------
10768 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
10769 Decl : Node_Id;
10771 begin
10772 if Ekind (Subp) /= E_Function then
10773 return False;
10775 else
10776 Decl := Unit_Declaration_Node (Subp);
10777 return Nkind (Decl) = N_Subprogram_Declaration
10778 and then
10779 (Nkind (Original_Node (Decl)) = N_Expression_Function
10780 or else
10781 (Present (Corresponding_Body (Decl))
10782 and then
10783 Nkind (Original_Node
10784 (Unit_Declaration_Node
10785 (Corresponding_Body (Decl)))) =
10786 N_Expression_Function));
10787 end if;
10788 end Is_Expression_Function;
10790 -----------------------
10791 -- Is_EVF_Expression --
10792 -----------------------
10794 function Is_EVF_Expression (N : Node_Id) return Boolean is
10795 Orig_N : constant Node_Id := Original_Node (N);
10796 Alt : Node_Id;
10797 Expr : Node_Id;
10798 Id : Entity_Id;
10800 begin
10801 -- Detect a reference to a formal parameter of a specific tagged type
10802 -- whose related subprogram is subject to pragma Expresions_Visible with
10803 -- value "False".
10805 if Is_Entity_Name (N) and then Present (Entity (N)) then
10806 Id := Entity (N);
10808 return
10809 Is_Formal (Id)
10810 and then Is_Specific_Tagged_Type (Etype (Id))
10811 and then Extensions_Visible_Status (Id) =
10812 Extensions_Visible_False;
10814 -- A case expression is an EVF expression when it contains at least one
10815 -- EVF dependent_expression. Note that a case expression may have been
10816 -- expanded, hence the use of Original_Node.
10818 elsif Nkind (Orig_N) = N_Case_Expression then
10819 Alt := First (Alternatives (Orig_N));
10820 while Present (Alt) loop
10821 if Is_EVF_Expression (Expression (Alt)) then
10822 return True;
10823 end if;
10825 Next (Alt);
10826 end loop;
10828 -- An if expression is an EVF expression when it contains at least one
10829 -- EVF dependent_expression. Note that an if expression may have been
10830 -- expanded, hence the use of Original_Node.
10832 elsif Nkind (Orig_N) = N_If_Expression then
10833 Expr := Next (First (Expressions (Orig_N)));
10834 while Present (Expr) loop
10835 if Is_EVF_Expression (Expr) then
10836 return True;
10837 end if;
10839 Next (Expr);
10840 end loop;
10842 -- A qualified expression or a type conversion is an EVF expression when
10843 -- its operand is an EVF expression.
10845 elsif Nkind_In (N, N_Qualified_Expression,
10846 N_Unchecked_Type_Conversion,
10847 N_Type_Conversion)
10848 then
10849 return Is_EVF_Expression (Expression (N));
10850 end if;
10852 return False;
10853 end Is_EVF_Expression;
10855 --------------
10856 -- Is_False --
10857 --------------
10859 function Is_False (U : Uint) return Boolean is
10860 begin
10861 return (U = 0);
10862 end Is_False;
10864 ---------------------------
10865 -- Is_Fixed_Model_Number --
10866 ---------------------------
10868 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
10869 S : constant Ureal := Small_Value (T);
10870 M : Urealp.Save_Mark;
10871 R : Boolean;
10872 begin
10873 M := Urealp.Mark;
10874 R := (U = UR_Trunc (U / S) * S);
10875 Urealp.Release (M);
10876 return R;
10877 end Is_Fixed_Model_Number;
10879 -------------------------------
10880 -- Is_Fully_Initialized_Type --
10881 -------------------------------
10883 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
10884 begin
10885 -- Scalar types
10887 if Is_Scalar_Type (Typ) then
10889 -- A scalar type with an aspect Default_Value is fully initialized
10891 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
10892 -- of a scalar type, but we don't take that into account here, since
10893 -- we don't want these to affect warnings.
10895 return Has_Default_Aspect (Typ);
10897 elsif Is_Access_Type (Typ) then
10898 return True;
10900 elsif Is_Array_Type (Typ) then
10901 if Is_Fully_Initialized_Type (Component_Type (Typ))
10902 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
10903 then
10904 return True;
10905 end if;
10907 -- An interesting case, if we have a constrained type one of whose
10908 -- bounds is known to be null, then there are no elements to be
10909 -- initialized, so all the elements are initialized.
10911 if Is_Constrained (Typ) then
10912 declare
10913 Indx : Node_Id;
10914 Indx_Typ : Entity_Id;
10915 Lbd, Hbd : Node_Id;
10917 begin
10918 Indx := First_Index (Typ);
10919 while Present (Indx) loop
10920 if Etype (Indx) = Any_Type then
10921 return False;
10923 -- If index is a range, use directly
10925 elsif Nkind (Indx) = N_Range then
10926 Lbd := Low_Bound (Indx);
10927 Hbd := High_Bound (Indx);
10929 else
10930 Indx_Typ := Etype (Indx);
10932 if Is_Private_Type (Indx_Typ) then
10933 Indx_Typ := Full_View (Indx_Typ);
10934 end if;
10936 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
10937 return False;
10938 else
10939 Lbd := Type_Low_Bound (Indx_Typ);
10940 Hbd := Type_High_Bound (Indx_Typ);
10941 end if;
10942 end if;
10944 if Compile_Time_Known_Value (Lbd)
10945 and then
10946 Compile_Time_Known_Value (Hbd)
10947 then
10948 if Expr_Value (Hbd) < Expr_Value (Lbd) then
10949 return True;
10950 end if;
10951 end if;
10953 Next_Index (Indx);
10954 end loop;
10955 end;
10956 end if;
10958 -- If no null indexes, then type is not fully initialized
10960 return False;
10962 -- Record types
10964 elsif Is_Record_Type (Typ) then
10965 if Has_Discriminants (Typ)
10966 and then
10967 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
10968 and then Is_Fully_Initialized_Variant (Typ)
10969 then
10970 return True;
10971 end if;
10973 -- We consider bounded string types to be fully initialized, because
10974 -- otherwise we get false alarms when the Data component is not
10975 -- default-initialized.
10977 if Is_Bounded_String (Typ) then
10978 return True;
10979 end if;
10981 -- Controlled records are considered to be fully initialized if
10982 -- there is a user defined Initialize routine. This may not be
10983 -- entirely correct, but as the spec notes, we are guessing here
10984 -- what is best from the point of view of issuing warnings.
10986 if Is_Controlled (Typ) then
10987 declare
10988 Utyp : constant Entity_Id := Underlying_Type (Typ);
10990 begin
10991 if Present (Utyp) then
10992 declare
10993 Init : constant Entity_Id :=
10994 (Find_Prim_Op
10995 (Underlying_Type (Typ), Name_Initialize));
10997 begin
10998 if Present (Init)
10999 and then Comes_From_Source (Init)
11000 and then not
11001 Is_Predefined_File_Name
11002 (File_Name (Get_Source_File_Index (Sloc (Init))))
11003 then
11004 return True;
11006 elsif Has_Null_Extension (Typ)
11007 and then
11008 Is_Fully_Initialized_Type
11009 (Etype (Base_Type (Typ)))
11010 then
11011 return True;
11012 end if;
11013 end;
11014 end if;
11015 end;
11016 end if;
11018 -- Otherwise see if all record components are initialized
11020 declare
11021 Ent : Entity_Id;
11023 begin
11024 Ent := First_Entity (Typ);
11025 while Present (Ent) loop
11026 if Ekind (Ent) = E_Component
11027 and then (No (Parent (Ent))
11028 or else No (Expression (Parent (Ent))))
11029 and then not Is_Fully_Initialized_Type (Etype (Ent))
11031 -- Special VM case for tag components, which need to be
11032 -- defined in this case, but are never initialized as VMs
11033 -- are using other dispatching mechanisms. Ignore this
11034 -- uninitialized case. Note that this applies both to the
11035 -- uTag entry and the main vtable pointer (CPP_Class case).
11037 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
11038 then
11039 return False;
11040 end if;
11042 Next_Entity (Ent);
11043 end loop;
11044 end;
11046 -- No uninitialized components, so type is fully initialized.
11047 -- Note that this catches the case of no components as well.
11049 return True;
11051 elsif Is_Concurrent_Type (Typ) then
11052 return True;
11054 elsif Is_Private_Type (Typ) then
11055 declare
11056 U : constant Entity_Id := Underlying_Type (Typ);
11058 begin
11059 if No (U) then
11060 return False;
11061 else
11062 return Is_Fully_Initialized_Type (U);
11063 end if;
11064 end;
11066 else
11067 return False;
11068 end if;
11069 end Is_Fully_Initialized_Type;
11071 ----------------------------------
11072 -- Is_Fully_Initialized_Variant --
11073 ----------------------------------
11075 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
11076 Loc : constant Source_Ptr := Sloc (Typ);
11077 Constraints : constant List_Id := New_List;
11078 Components : constant Elist_Id := New_Elmt_List;
11079 Comp_Elmt : Elmt_Id;
11080 Comp_Id : Node_Id;
11081 Comp_List : Node_Id;
11082 Discr : Entity_Id;
11083 Discr_Val : Node_Id;
11085 Report_Errors : Boolean;
11086 pragma Warnings (Off, Report_Errors);
11088 begin
11089 if Serious_Errors_Detected > 0 then
11090 return False;
11091 end if;
11093 if Is_Record_Type (Typ)
11094 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11095 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
11096 then
11097 Comp_List := Component_List (Type_Definition (Parent (Typ)));
11099 Discr := First_Discriminant (Typ);
11100 while Present (Discr) loop
11101 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
11102 Discr_Val := Expression (Parent (Discr));
11104 if Present (Discr_Val)
11105 and then Is_OK_Static_Expression (Discr_Val)
11106 then
11107 Append_To (Constraints,
11108 Make_Component_Association (Loc,
11109 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
11110 Expression => New_Copy (Discr_Val)));
11111 else
11112 return False;
11113 end if;
11114 else
11115 return False;
11116 end if;
11118 Next_Discriminant (Discr);
11119 end loop;
11121 Gather_Components
11122 (Typ => Typ,
11123 Comp_List => Comp_List,
11124 Governed_By => Constraints,
11125 Into => Components,
11126 Report_Errors => Report_Errors);
11128 -- Check that each component present is fully initialized
11130 Comp_Elmt := First_Elmt (Components);
11131 while Present (Comp_Elmt) loop
11132 Comp_Id := Node (Comp_Elmt);
11134 if Ekind (Comp_Id) = E_Component
11135 and then (No (Parent (Comp_Id))
11136 or else No (Expression (Parent (Comp_Id))))
11137 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
11138 then
11139 return False;
11140 end if;
11142 Next_Elmt (Comp_Elmt);
11143 end loop;
11145 return True;
11147 elsif Is_Private_Type (Typ) then
11148 declare
11149 U : constant Entity_Id := Underlying_Type (Typ);
11151 begin
11152 if No (U) then
11153 return False;
11154 else
11155 return Is_Fully_Initialized_Variant (U);
11156 end if;
11157 end;
11159 else
11160 return False;
11161 end if;
11162 end Is_Fully_Initialized_Variant;
11164 ---------------------
11165 -- Is_Ghost_Entity --
11166 ---------------------
11168 function Is_Ghost_Entity (Id : Entity_Id) return Boolean is
11169 begin
11170 return Is_Checked_Ghost_Entity (Id) or else Is_Ignored_Ghost_Entity (Id);
11171 end Is_Ghost_Entity;
11173 ----------------------------------
11174 -- Is_Ghost_Statement_Or_Pragma --
11175 ----------------------------------
11177 function Is_Ghost_Statement_Or_Pragma (N : Node_Id) return Boolean is
11178 function Is_Ghost_Entity_Reference (N : Node_Id) return Boolean;
11179 -- Determine whether an arbitrary node denotes a reference to a Ghost
11180 -- entity.
11182 -------------------------------
11183 -- Is_Ghost_Entity_Reference --
11184 -------------------------------
11186 function Is_Ghost_Entity_Reference (N : Node_Id) return Boolean is
11187 Ref : Node_Id;
11189 begin
11190 Ref := N;
11192 -- When the reference extracts a subcomponent, recover the related
11193 -- object (SPARK RM 6.9(1)).
11195 while Nkind_In (Ref, N_Explicit_Dereference,
11196 N_Indexed_Component,
11197 N_Selected_Component,
11198 N_Slice)
11199 loop
11200 Ref := Prefix (Ref);
11201 end loop;
11203 return
11204 Is_Entity_Name (Ref)
11205 and then Present (Entity (Ref))
11206 and then Is_Ghost_Entity (Entity (Ref));
11207 end Is_Ghost_Entity_Reference;
11209 -- Local variables
11211 Arg : Node_Id;
11212 Stmt : Node_Id;
11214 -- Start of processing for Is_Ghost_Statement_Or_Pragma
11216 begin
11217 if Nkind (N) = N_Pragma then
11219 -- A pragma is Ghost when it appears within a Ghost package or
11220 -- subprogram.
11222 if Within_Ghost_Scope then
11223 return True;
11224 end if;
11226 -- A pragma is Ghost when it mentions a Ghost entity
11228 Arg := First (Pragma_Argument_Associations (N));
11229 while Present (Arg) loop
11230 if Is_Ghost_Entity_Reference (Get_Pragma_Arg (Arg)) then
11231 return True;
11232 end if;
11234 Next (Arg);
11235 end loop;
11236 end if;
11238 Stmt := N;
11239 while Present (Stmt) loop
11241 -- A statement is Ghost when it appears within a Ghost package or
11242 -- subprogram.
11244 if Is_Statement (Stmt) and then Within_Ghost_Scope then
11245 return True;
11247 -- An assignment statement is Ghost when the target is a Ghost
11248 -- variable. A procedure call is Ghost when the invoked procedure
11249 -- is Ghost.
11251 elsif Nkind_In (Stmt, N_Assignment_Statement,
11252 N_Procedure_Call_Statement)
11253 then
11254 return Is_Ghost_Entity_Reference (Name (Stmt));
11256 -- Prevent the search from going too far
11258 elsif Is_Body_Or_Package_Declaration (Stmt) then
11259 return False;
11260 end if;
11262 Stmt := Parent (Stmt);
11263 end loop;
11265 return False;
11266 end Is_Ghost_Statement_Or_Pragma;
11268 ----------------------------
11269 -- Is_Inherited_Operation --
11270 ----------------------------
11272 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
11273 pragma Assert (Is_Overloadable (E));
11274 Kind : constant Node_Kind := Nkind (Parent (E));
11275 begin
11276 return Kind = N_Full_Type_Declaration
11277 or else Kind = N_Private_Extension_Declaration
11278 or else Kind = N_Subtype_Declaration
11279 or else (Ekind (E) = E_Enumeration_Literal
11280 and then Is_Derived_Type (Etype (E)));
11281 end Is_Inherited_Operation;
11283 -------------------------------------
11284 -- Is_Inherited_Operation_For_Type --
11285 -------------------------------------
11287 function Is_Inherited_Operation_For_Type
11288 (E : Entity_Id;
11289 Typ : Entity_Id) return Boolean
11291 begin
11292 -- Check that the operation has been created by the type declaration
11294 return Is_Inherited_Operation (E)
11295 and then Defining_Identifier (Parent (E)) = Typ;
11296 end Is_Inherited_Operation_For_Type;
11298 -----------------
11299 -- Is_Iterator --
11300 -----------------
11302 function Is_Iterator (Typ : Entity_Id) return Boolean is
11303 Ifaces_List : Elist_Id;
11304 Iface_Elmt : Elmt_Id;
11305 Iface : Entity_Id;
11307 begin
11308 if Is_Class_Wide_Type (Typ)
11309 and then Nam_In (Chars (Etype (Typ)), Name_Forward_Iterator,
11310 Name_Reversible_Iterator)
11311 and then
11312 Is_Predefined_File_Name
11313 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
11314 then
11315 return True;
11317 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
11318 return False;
11320 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
11321 return True;
11323 else
11324 Collect_Interfaces (Typ, Ifaces_List);
11326 Iface_Elmt := First_Elmt (Ifaces_List);
11327 while Present (Iface_Elmt) loop
11328 Iface := Node (Iface_Elmt);
11329 if Chars (Iface) = Name_Forward_Iterator
11330 and then
11331 Is_Predefined_File_Name
11332 (Unit_File_Name (Get_Source_Unit (Iface)))
11333 then
11334 return True;
11335 end if;
11337 Next_Elmt (Iface_Elmt);
11338 end loop;
11340 return False;
11341 end if;
11342 end Is_Iterator;
11344 ------------
11345 -- Is_LHS --
11346 ------------
11348 -- We seem to have a lot of overlapping functions that do similar things
11349 -- (testing for left hand sides or lvalues???).
11351 function Is_LHS (N : Node_Id) return Is_LHS_Result is
11352 P : constant Node_Id := Parent (N);
11354 begin
11355 -- Return True if we are the left hand side of an assignment statement
11357 if Nkind (P) = N_Assignment_Statement then
11358 if Name (P) = N then
11359 return Yes;
11360 else
11361 return No;
11362 end if;
11364 -- Case of prefix of indexed or selected component or slice
11366 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
11367 and then N = Prefix (P)
11368 then
11369 -- Here we have the case where the parent P is N.Q or N(Q .. R).
11370 -- If P is an LHS, then N is also effectively an LHS, but there
11371 -- is an important exception. If N is of an access type, then
11372 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
11373 -- case this makes N.all a left hand side but not N itself.
11375 -- If we don't know the type yet, this is the case where we return
11376 -- Unknown, since the answer depends on the type which is unknown.
11378 if No (Etype (N)) then
11379 return Unknown;
11381 -- We have an Etype set, so we can check it
11383 elsif Is_Access_Type (Etype (N)) then
11384 return No;
11386 -- OK, not access type case, so just test whole expression
11388 else
11389 return Is_LHS (P);
11390 end if;
11392 -- All other cases are not left hand sides
11394 else
11395 return No;
11396 end if;
11397 end Is_LHS;
11399 -----------------------------
11400 -- Is_Library_Level_Entity --
11401 -----------------------------
11403 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
11404 begin
11405 -- The following is a small optimization, and it also properly handles
11406 -- discriminals, which in task bodies might appear in expressions before
11407 -- the corresponding procedure has been created, and which therefore do
11408 -- not have an assigned scope.
11410 if Is_Formal (E) then
11411 return False;
11412 end if;
11414 -- Normal test is simply that the enclosing dynamic scope is Standard
11416 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
11417 end Is_Library_Level_Entity;
11419 --------------------------------
11420 -- Is_Limited_Class_Wide_Type --
11421 --------------------------------
11423 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
11424 begin
11425 return
11426 Is_Class_Wide_Type (Typ)
11427 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
11428 end Is_Limited_Class_Wide_Type;
11430 ---------------------------------
11431 -- Is_Local_Variable_Reference --
11432 ---------------------------------
11434 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
11435 begin
11436 if not Is_Entity_Name (Expr) then
11437 return False;
11439 else
11440 declare
11441 Ent : constant Entity_Id := Entity (Expr);
11442 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
11443 begin
11444 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
11445 return False;
11446 else
11447 return Present (Sub) and then Sub = Current_Subprogram;
11448 end if;
11449 end;
11450 end if;
11451 end Is_Local_Variable_Reference;
11453 -------------------------
11454 -- Is_Object_Reference --
11455 -------------------------
11457 function Is_Object_Reference (N : Node_Id) return Boolean is
11459 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
11460 -- Determine whether N is the name of an internally-generated renaming
11462 --------------------------------------
11463 -- Is_Internally_Generated_Renaming --
11464 --------------------------------------
11466 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
11467 P : Node_Id;
11469 begin
11470 P := N;
11471 while Present (P) loop
11472 if Nkind (P) = N_Object_Renaming_Declaration then
11473 return not Comes_From_Source (P);
11474 elsif Is_List_Member (P) then
11475 return False;
11476 end if;
11478 P := Parent (P);
11479 end loop;
11481 return False;
11482 end Is_Internally_Generated_Renaming;
11484 -- Start of processing for Is_Object_Reference
11486 begin
11487 if Is_Entity_Name (N) then
11488 return Present (Entity (N)) and then Is_Object (Entity (N));
11490 else
11491 case Nkind (N) is
11492 when N_Indexed_Component | N_Slice =>
11493 return
11494 Is_Object_Reference (Prefix (N))
11495 or else Is_Access_Type (Etype (Prefix (N)));
11497 -- In Ada 95, a function call is a constant object; a procedure
11498 -- call is not.
11500 when N_Function_Call =>
11501 return Etype (N) /= Standard_Void_Type;
11503 -- Attributes 'Input, 'Old and 'Result produce objects
11505 when N_Attribute_Reference =>
11506 return
11507 Nam_In
11508 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
11510 when N_Selected_Component =>
11511 return
11512 Is_Object_Reference (Selector_Name (N))
11513 and then
11514 (Is_Object_Reference (Prefix (N))
11515 or else Is_Access_Type (Etype (Prefix (N))));
11517 when N_Explicit_Dereference =>
11518 return True;
11520 -- A view conversion of a tagged object is an object reference
11522 when N_Type_Conversion =>
11523 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
11524 and then Is_Tagged_Type (Etype (Expression (N)))
11525 and then Is_Object_Reference (Expression (N));
11527 -- An unchecked type conversion is considered to be an object if
11528 -- the operand is an object (this construction arises only as a
11529 -- result of expansion activities).
11531 when N_Unchecked_Type_Conversion =>
11532 return True;
11534 -- Allow string literals to act as objects as long as they appear
11535 -- in internally-generated renamings. The expansion of iterators
11536 -- may generate such renamings when the range involves a string
11537 -- literal.
11539 when N_String_Literal =>
11540 return Is_Internally_Generated_Renaming (Parent (N));
11542 -- AI05-0003: In Ada 2012 a qualified expression is a name.
11543 -- This allows disambiguation of function calls and the use
11544 -- of aggregates in more contexts.
11546 when N_Qualified_Expression =>
11547 if Ada_Version < Ada_2012 then
11548 return False;
11549 else
11550 return Is_Object_Reference (Expression (N))
11551 or else Nkind (Expression (N)) = N_Aggregate;
11552 end if;
11554 when others =>
11555 return False;
11556 end case;
11557 end if;
11558 end Is_Object_Reference;
11560 -----------------------------------
11561 -- Is_OK_Variable_For_Out_Formal --
11562 -----------------------------------
11564 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
11565 begin
11566 Note_Possible_Modification (AV, Sure => True);
11568 -- We must reject parenthesized variable names. Comes_From_Source is
11569 -- checked because there are currently cases where the compiler violates
11570 -- this rule (e.g. passing a task object to its controlled Initialize
11571 -- routine). This should be properly documented in sinfo???
11573 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
11574 return False;
11576 -- A variable is always allowed
11578 elsif Is_Variable (AV) then
11579 return True;
11581 -- Unchecked conversions are allowed only if they come from the
11582 -- generated code, which sometimes uses unchecked conversions for out
11583 -- parameters in cases where code generation is unaffected. We tell
11584 -- source unchecked conversions by seeing if they are rewrites of
11585 -- an original Unchecked_Conversion function call, or of an explicit
11586 -- conversion of a function call or an aggregate (as may happen in the
11587 -- expansion of a packed array aggregate).
11589 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
11590 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
11591 return False;
11593 elsif Comes_From_Source (AV)
11594 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
11595 then
11596 return False;
11598 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
11599 return Is_OK_Variable_For_Out_Formal (Expression (AV));
11601 else
11602 return True;
11603 end if;
11605 -- Normal type conversions are allowed if argument is a variable
11607 elsif Nkind (AV) = N_Type_Conversion then
11608 if Is_Variable (Expression (AV))
11609 and then Paren_Count (Expression (AV)) = 0
11610 then
11611 Note_Possible_Modification (Expression (AV), Sure => True);
11612 return True;
11614 -- We also allow a non-parenthesized expression that raises
11615 -- constraint error if it rewrites what used to be a variable
11617 elsif Raises_Constraint_Error (Expression (AV))
11618 and then Paren_Count (Expression (AV)) = 0
11619 and then Is_Variable (Original_Node (Expression (AV)))
11620 then
11621 return True;
11623 -- Type conversion of something other than a variable
11625 else
11626 return False;
11627 end if;
11629 -- If this node is rewritten, then test the original form, if that is
11630 -- OK, then we consider the rewritten node OK (for example, if the
11631 -- original node is a conversion, then Is_Variable will not be true
11632 -- but we still want to allow the conversion if it converts a variable).
11634 elsif Original_Node (AV) /= AV then
11636 -- In Ada 2012, the explicit dereference may be a rewritten call to a
11637 -- Reference function.
11639 if Ada_Version >= Ada_2012
11640 and then Nkind (Original_Node (AV)) = N_Function_Call
11641 and then
11642 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
11643 then
11644 return True;
11646 else
11647 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
11648 end if;
11650 -- All other non-variables are rejected
11652 else
11653 return False;
11654 end if;
11655 end Is_OK_Variable_For_Out_Formal;
11657 -----------------------------------
11658 -- Is_Partially_Initialized_Type --
11659 -----------------------------------
11661 function Is_Partially_Initialized_Type
11662 (Typ : Entity_Id;
11663 Include_Implicit : Boolean := True) return Boolean
11665 begin
11666 if Is_Scalar_Type (Typ) then
11667 return False;
11669 elsif Is_Access_Type (Typ) then
11670 return Include_Implicit;
11672 elsif Is_Array_Type (Typ) then
11674 -- If component type is partially initialized, so is array type
11676 if Is_Partially_Initialized_Type
11677 (Component_Type (Typ), Include_Implicit)
11678 then
11679 return True;
11681 -- Otherwise we are only partially initialized if we are fully
11682 -- initialized (this is the empty array case, no point in us
11683 -- duplicating that code here).
11685 else
11686 return Is_Fully_Initialized_Type (Typ);
11687 end if;
11689 elsif Is_Record_Type (Typ) then
11691 -- A discriminated type is always partially initialized if in
11692 -- all mode
11694 if Has_Discriminants (Typ) and then Include_Implicit then
11695 return True;
11697 -- A tagged type is always partially initialized
11699 elsif Is_Tagged_Type (Typ) then
11700 return True;
11702 -- Case of non-discriminated record
11704 else
11705 declare
11706 Ent : Entity_Id;
11708 Component_Present : Boolean := False;
11709 -- Set True if at least one component is present. If no
11710 -- components are present, then record type is fully
11711 -- initialized (another odd case, like the null array).
11713 begin
11714 -- Loop through components
11716 Ent := First_Entity (Typ);
11717 while Present (Ent) loop
11718 if Ekind (Ent) = E_Component then
11719 Component_Present := True;
11721 -- If a component has an initialization expression then
11722 -- the enclosing record type is partially initialized
11724 if Present (Parent (Ent))
11725 and then Present (Expression (Parent (Ent)))
11726 then
11727 return True;
11729 -- If a component is of a type which is itself partially
11730 -- initialized, then the enclosing record type is also.
11732 elsif Is_Partially_Initialized_Type
11733 (Etype (Ent), Include_Implicit)
11734 then
11735 return True;
11736 end if;
11737 end if;
11739 Next_Entity (Ent);
11740 end loop;
11742 -- No initialized components found. If we found any components
11743 -- they were all uninitialized so the result is false.
11745 if Component_Present then
11746 return False;
11748 -- But if we found no components, then all the components are
11749 -- initialized so we consider the type to be initialized.
11751 else
11752 return True;
11753 end if;
11754 end;
11755 end if;
11757 -- Concurrent types are always fully initialized
11759 elsif Is_Concurrent_Type (Typ) then
11760 return True;
11762 -- For a private type, go to underlying type. If there is no underlying
11763 -- type then just assume this partially initialized. Not clear if this
11764 -- can happen in a non-error case, but no harm in testing for this.
11766 elsif Is_Private_Type (Typ) then
11767 declare
11768 U : constant Entity_Id := Underlying_Type (Typ);
11769 begin
11770 if No (U) then
11771 return True;
11772 else
11773 return Is_Partially_Initialized_Type (U, Include_Implicit);
11774 end if;
11775 end;
11777 -- For any other type (are there any?) assume partially initialized
11779 else
11780 return True;
11781 end if;
11782 end Is_Partially_Initialized_Type;
11784 ------------------------------------
11785 -- Is_Potentially_Persistent_Type --
11786 ------------------------------------
11788 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
11789 Comp : Entity_Id;
11790 Indx : Node_Id;
11792 begin
11793 -- For private type, test corresponding full type
11795 if Is_Private_Type (T) then
11796 return Is_Potentially_Persistent_Type (Full_View (T));
11798 -- Scalar types are potentially persistent
11800 elsif Is_Scalar_Type (T) then
11801 return True;
11803 -- Record type is potentially persistent if not tagged and the types of
11804 -- all it components are potentially persistent, and no component has
11805 -- an initialization expression.
11807 elsif Is_Record_Type (T)
11808 and then not Is_Tagged_Type (T)
11809 and then not Is_Partially_Initialized_Type (T)
11810 then
11811 Comp := First_Component (T);
11812 while Present (Comp) loop
11813 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
11814 return False;
11815 else
11816 Next_Entity (Comp);
11817 end if;
11818 end loop;
11820 return True;
11822 -- Array type is potentially persistent if its component type is
11823 -- potentially persistent and if all its constraints are static.
11825 elsif Is_Array_Type (T) then
11826 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
11827 return False;
11828 end if;
11830 Indx := First_Index (T);
11831 while Present (Indx) loop
11832 if not Is_OK_Static_Subtype (Etype (Indx)) then
11833 return False;
11834 else
11835 Next_Index (Indx);
11836 end if;
11837 end loop;
11839 return True;
11841 -- All other types are not potentially persistent
11843 else
11844 return False;
11845 end if;
11846 end Is_Potentially_Persistent_Type;
11848 --------------------------------
11849 -- Is_Potentially_Unevaluated --
11850 --------------------------------
11852 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
11853 Par : Node_Id;
11854 Expr : Node_Id;
11856 begin
11857 Expr := N;
11858 Par := Parent (N);
11860 -- A postcondition whose expression is a short-circuit is broken down
11861 -- into individual aspects for better exception reporting. The original
11862 -- short-circuit expression is rewritten as the second operand, and an
11863 -- occurrence of 'Old in that operand is potentially unevaluated.
11864 -- See Sem_ch13.adb for details of this transformation.
11866 if Nkind (Original_Node (Par)) = N_And_Then then
11867 return True;
11868 end if;
11870 while not Nkind_In (Par, N_If_Expression,
11871 N_Case_Expression,
11872 N_And_Then,
11873 N_Or_Else,
11874 N_In,
11875 N_Not_In)
11876 loop
11877 Expr := Par;
11878 Par := Parent (Par);
11880 -- If the context is not an expression, or if is the result of
11881 -- expansion of an enclosing construct (such as another attribute)
11882 -- the predicate does not apply.
11884 if Nkind (Par) not in N_Subexpr
11885 or else not Comes_From_Source (Par)
11886 then
11887 return False;
11888 end if;
11889 end loop;
11891 if Nkind (Par) = N_If_Expression then
11892 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
11894 elsif Nkind (Par) = N_Case_Expression then
11895 return Expr /= Expression (Par);
11897 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
11898 return Expr = Right_Opnd (Par);
11900 elsif Nkind_In (Par, N_In, N_Not_In) then
11901 return Expr /= Left_Opnd (Par);
11903 else
11904 return False;
11905 end if;
11906 end Is_Potentially_Unevaluated;
11908 ---------------------------------
11909 -- Is_Protected_Self_Reference --
11910 ---------------------------------
11912 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
11914 function In_Access_Definition (N : Node_Id) return Boolean;
11915 -- Returns true if N belongs to an access definition
11917 --------------------------
11918 -- In_Access_Definition --
11919 --------------------------
11921 function In_Access_Definition (N : Node_Id) return Boolean is
11922 P : Node_Id;
11924 begin
11925 P := Parent (N);
11926 while Present (P) loop
11927 if Nkind (P) = N_Access_Definition then
11928 return True;
11929 end if;
11931 P := Parent (P);
11932 end loop;
11934 return False;
11935 end In_Access_Definition;
11937 -- Start of processing for Is_Protected_Self_Reference
11939 begin
11940 -- Verify that prefix is analyzed and has the proper form. Note that
11941 -- the attributes Elab_Spec, Elab_Body, Elab_Subp_Body and UET_Address,
11942 -- which also produce the address of an entity, do not analyze their
11943 -- prefix because they denote entities that are not necessarily visible.
11944 -- Neither of them can apply to a protected type.
11946 return Ada_Version >= Ada_2005
11947 and then Is_Entity_Name (N)
11948 and then Present (Entity (N))
11949 and then Is_Protected_Type (Entity (N))
11950 and then In_Open_Scopes (Entity (N))
11951 and then not In_Access_Definition (N);
11952 end Is_Protected_Self_Reference;
11954 -----------------------------
11955 -- Is_RCI_Pkg_Spec_Or_Body --
11956 -----------------------------
11958 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
11960 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
11961 -- Return True if the unit of Cunit is an RCI package declaration
11963 ---------------------------
11964 -- Is_RCI_Pkg_Decl_Cunit --
11965 ---------------------------
11967 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
11968 The_Unit : constant Node_Id := Unit (Cunit);
11970 begin
11971 if Nkind (The_Unit) /= N_Package_Declaration then
11972 return False;
11973 end if;
11975 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
11976 end Is_RCI_Pkg_Decl_Cunit;
11978 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
11980 begin
11981 return Is_RCI_Pkg_Decl_Cunit (Cunit)
11982 or else
11983 (Nkind (Unit (Cunit)) = N_Package_Body
11984 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
11985 end Is_RCI_Pkg_Spec_Or_Body;
11987 -----------------------------------------
11988 -- Is_Remote_Access_To_Class_Wide_Type --
11989 -----------------------------------------
11991 function Is_Remote_Access_To_Class_Wide_Type
11992 (E : Entity_Id) return Boolean
11994 begin
11995 -- A remote access to class-wide type is a general access to object type
11996 -- declared in the visible part of a Remote_Types or Remote_Call_
11997 -- Interface unit.
11999 return Ekind (E) = E_General_Access_Type
12000 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12001 end Is_Remote_Access_To_Class_Wide_Type;
12003 -----------------------------------------
12004 -- Is_Remote_Access_To_Subprogram_Type --
12005 -----------------------------------------
12007 function Is_Remote_Access_To_Subprogram_Type
12008 (E : Entity_Id) return Boolean
12010 begin
12011 return (Ekind (E) = E_Access_Subprogram_Type
12012 or else (Ekind (E) = E_Record_Type
12013 and then Present (Corresponding_Remote_Type (E))))
12014 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12015 end Is_Remote_Access_To_Subprogram_Type;
12017 --------------------
12018 -- Is_Remote_Call --
12019 --------------------
12021 function Is_Remote_Call (N : Node_Id) return Boolean is
12022 begin
12023 if Nkind (N) not in N_Subprogram_Call then
12025 -- An entry call cannot be remote
12027 return False;
12029 elsif Nkind (Name (N)) in N_Has_Entity
12030 and then Is_Remote_Call_Interface (Entity (Name (N)))
12031 then
12032 -- A subprogram declared in the spec of a RCI package is remote
12034 return True;
12036 elsif Nkind (Name (N)) = N_Explicit_Dereference
12037 and then Is_Remote_Access_To_Subprogram_Type
12038 (Etype (Prefix (Name (N))))
12039 then
12040 -- The dereference of a RAS is a remote call
12042 return True;
12044 elsif Present (Controlling_Argument (N))
12045 and then Is_Remote_Access_To_Class_Wide_Type
12046 (Etype (Controlling_Argument (N)))
12047 then
12048 -- Any primitive operation call with a controlling argument of
12049 -- a RACW type is a remote call.
12051 return True;
12052 end if;
12054 -- All other calls are local calls
12056 return False;
12057 end Is_Remote_Call;
12059 ----------------------
12060 -- Is_Renamed_Entry --
12061 ----------------------
12063 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
12064 Orig_Node : Node_Id := Empty;
12065 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
12067 function Is_Entry (Nam : Node_Id) return Boolean;
12068 -- Determine whether Nam is an entry. Traverse selectors if there are
12069 -- nested selected components.
12071 --------------
12072 -- Is_Entry --
12073 --------------
12075 function Is_Entry (Nam : Node_Id) return Boolean is
12076 begin
12077 if Nkind (Nam) = N_Selected_Component then
12078 return Is_Entry (Selector_Name (Nam));
12079 end if;
12081 return Ekind (Entity (Nam)) = E_Entry;
12082 end Is_Entry;
12084 -- Start of processing for Is_Renamed_Entry
12086 begin
12087 if Present (Alias (Proc_Nam)) then
12088 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
12089 end if;
12091 -- Look for a rewritten subprogram renaming declaration
12093 if Nkind (Subp_Decl) = N_Subprogram_Declaration
12094 and then Present (Original_Node (Subp_Decl))
12095 then
12096 Orig_Node := Original_Node (Subp_Decl);
12097 end if;
12099 -- The rewritten subprogram is actually an entry
12101 if Present (Orig_Node)
12102 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
12103 and then Is_Entry (Name (Orig_Node))
12104 then
12105 return True;
12106 end if;
12108 return False;
12109 end Is_Renamed_Entry;
12111 ----------------------------
12112 -- Is_Reversible_Iterator --
12113 ----------------------------
12115 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
12116 Ifaces_List : Elist_Id;
12117 Iface_Elmt : Elmt_Id;
12118 Iface : Entity_Id;
12120 begin
12121 if Is_Class_Wide_Type (Typ)
12122 and then Chars (Etype (Typ)) = Name_Reversible_Iterator
12123 and then Is_Predefined_File_Name
12124 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
12125 then
12126 return True;
12128 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
12129 return False;
12131 else
12132 Collect_Interfaces (Typ, Ifaces_List);
12134 Iface_Elmt := First_Elmt (Ifaces_List);
12135 while Present (Iface_Elmt) loop
12136 Iface := Node (Iface_Elmt);
12137 if Chars (Iface) = Name_Reversible_Iterator
12138 and then
12139 Is_Predefined_File_Name
12140 (Unit_File_Name (Get_Source_Unit (Iface)))
12141 then
12142 return True;
12143 end if;
12145 Next_Elmt (Iface_Elmt);
12146 end loop;
12147 end if;
12149 return False;
12150 end Is_Reversible_Iterator;
12152 ----------------------
12153 -- Is_Selector_Name --
12154 ----------------------
12156 function Is_Selector_Name (N : Node_Id) return Boolean is
12157 begin
12158 if not Is_List_Member (N) then
12159 declare
12160 P : constant Node_Id := Parent (N);
12161 begin
12162 return Nkind_In (P, N_Expanded_Name,
12163 N_Generic_Association,
12164 N_Parameter_Association,
12165 N_Selected_Component)
12166 and then Selector_Name (P) = N;
12167 end;
12169 else
12170 declare
12171 L : constant List_Id := List_Containing (N);
12172 P : constant Node_Id := Parent (L);
12173 begin
12174 return (Nkind (P) = N_Discriminant_Association
12175 and then Selector_Names (P) = L)
12176 or else
12177 (Nkind (P) = N_Component_Association
12178 and then Choices (P) = L);
12179 end;
12180 end if;
12181 end Is_Selector_Name;
12183 -------------------------------------
12184 -- Is_SPARK_05_Initialization_Expr --
12185 -------------------------------------
12187 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
12188 Is_Ok : Boolean;
12189 Expr : Node_Id;
12190 Comp_Assn : Node_Id;
12191 Orig_N : constant Node_Id := Original_Node (N);
12193 begin
12194 Is_Ok := True;
12196 if not Comes_From_Source (Orig_N) then
12197 goto Done;
12198 end if;
12200 pragma Assert (Nkind (Orig_N) in N_Subexpr);
12202 case Nkind (Orig_N) is
12203 when N_Character_Literal |
12204 N_Integer_Literal |
12205 N_Real_Literal |
12206 N_String_Literal =>
12207 null;
12209 when N_Identifier |
12210 N_Expanded_Name =>
12211 if Is_Entity_Name (Orig_N)
12212 and then Present (Entity (Orig_N)) -- needed in some cases
12213 then
12214 case Ekind (Entity (Orig_N)) is
12215 when E_Constant |
12216 E_Enumeration_Literal |
12217 E_Named_Integer |
12218 E_Named_Real =>
12219 null;
12220 when others =>
12221 if Is_Type (Entity (Orig_N)) then
12222 null;
12223 else
12224 Is_Ok := False;
12225 end if;
12226 end case;
12227 end if;
12229 when N_Qualified_Expression |
12230 N_Type_Conversion =>
12231 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
12233 when N_Unary_Op =>
12234 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12236 when N_Binary_Op |
12237 N_Short_Circuit |
12238 N_Membership_Test =>
12239 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
12240 and then
12241 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12243 when N_Aggregate |
12244 N_Extension_Aggregate =>
12245 if Nkind (Orig_N) = N_Extension_Aggregate then
12246 Is_Ok :=
12247 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
12248 end if;
12250 Expr := First (Expressions (Orig_N));
12251 while Present (Expr) loop
12252 if not Is_SPARK_05_Initialization_Expr (Expr) then
12253 Is_Ok := False;
12254 goto Done;
12255 end if;
12257 Next (Expr);
12258 end loop;
12260 Comp_Assn := First (Component_Associations (Orig_N));
12261 while Present (Comp_Assn) loop
12262 Expr := Expression (Comp_Assn);
12264 -- Note: test for Present here needed for box assocation
12266 if Present (Expr)
12267 and then not Is_SPARK_05_Initialization_Expr (Expr)
12268 then
12269 Is_Ok := False;
12270 goto Done;
12271 end if;
12273 Next (Comp_Assn);
12274 end loop;
12276 when N_Attribute_Reference =>
12277 if Nkind (Prefix (Orig_N)) in N_Subexpr then
12278 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
12279 end if;
12281 Expr := First (Expressions (Orig_N));
12282 while Present (Expr) loop
12283 if not Is_SPARK_05_Initialization_Expr (Expr) then
12284 Is_Ok := False;
12285 goto Done;
12286 end if;
12288 Next (Expr);
12289 end loop;
12291 -- Selected components might be expanded named not yet resolved, so
12292 -- default on the safe side. (Eg on sparklex.ads)
12294 when N_Selected_Component =>
12295 null;
12297 when others =>
12298 Is_Ok := False;
12299 end case;
12301 <<Done>>
12302 return Is_Ok;
12303 end Is_SPARK_05_Initialization_Expr;
12305 ----------------------------------
12306 -- Is_SPARK_05_Object_Reference --
12307 ----------------------------------
12309 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
12310 begin
12311 if Is_Entity_Name (N) then
12312 return Present (Entity (N))
12313 and then
12314 (Ekind_In (Entity (N), E_Constant, E_Variable)
12315 or else Ekind (Entity (N)) in Formal_Kind);
12317 else
12318 case Nkind (N) is
12319 when N_Selected_Component =>
12320 return Is_SPARK_05_Object_Reference (Prefix (N));
12322 when others =>
12323 return False;
12324 end case;
12325 end if;
12326 end Is_SPARK_05_Object_Reference;
12328 -----------------------------
12329 -- Is_Specific_Tagged_Type --
12330 -----------------------------
12332 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
12333 Full_Typ : Entity_Id;
12335 begin
12336 -- Handle private types
12338 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
12339 Full_Typ := Full_View (Typ);
12340 else
12341 Full_Typ := Typ;
12342 end if;
12344 -- A specific tagged type is a non-class-wide tagged type
12346 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
12347 end Is_Specific_Tagged_Type;
12349 ------------------
12350 -- Is_Statement --
12351 ------------------
12353 function Is_Statement (N : Node_Id) return Boolean is
12354 begin
12355 return
12356 Nkind (N) in N_Statement_Other_Than_Procedure_Call
12357 or else Nkind (N) = N_Procedure_Call_Statement;
12358 end Is_Statement;
12360 -------------------------
12361 -- Is_Subject_To_Ghost --
12362 -------------------------
12364 function Is_Subject_To_Ghost (N : Node_Id) return Boolean is
12365 function Enables_Ghostness (Arg : Node_Id) return Boolean;
12366 -- Determine whether aspect or pragma argument Arg enables "ghostness"
12368 -----------------------
12369 -- Enables_Ghostness --
12370 -----------------------
12372 function Enables_Ghostness (Arg : Node_Id) return Boolean is
12373 Expr : Node_Id;
12375 begin
12376 Expr := Arg;
12378 if Nkind (Expr) = N_Pragma_Argument_Association then
12379 Expr := Get_Pragma_Arg (Expr);
12380 end if;
12382 -- Determine whether the expression of the aspect is static and
12383 -- denotes True.
12385 if Present (Expr) then
12386 Preanalyze_And_Resolve (Expr);
12388 return
12389 Is_OK_Static_Expression (Expr)
12390 and then Is_True (Expr_Value (Expr));
12392 -- Otherwise Ghost defaults to True
12394 else
12395 return True;
12396 end if;
12397 end Enables_Ghostness;
12399 -- Local variables
12401 Id : constant Entity_Id := Defining_Entity (N);
12402 Asp : Node_Id;
12403 Decl : Node_Id;
12404 Prev_Id : Entity_Id;
12406 -- Start of processing for Is_Subject_To_Ghost
12408 begin
12409 if Is_Ghost_Entity (Id) then
12410 return True;
12412 -- The completion of a type or a constant is not fully analyzed when the
12413 -- reference to the Ghost entity is resolved. Because the completion is
12414 -- not marked as Ghost yet, inspect the partial view.
12416 elsif Is_Record_Type (Id)
12417 or else Ekind (Id) = E_Constant
12418 or else (Nkind (N) = N_Object_Declaration
12419 and then Constant_Present (N))
12420 then
12421 Prev_Id := Incomplete_Or_Partial_View (Id);
12423 if Present (Prev_Id) and then Is_Ghost_Entity (Prev_Id) then
12424 return True;
12425 end if;
12426 end if;
12428 -- Examine the aspect specifications (if any) looking for aspect Ghost
12430 if Permits_Aspect_Specifications (N) then
12431 Asp := First (Aspect_Specifications (N));
12432 while Present (Asp) loop
12433 if Chars (Identifier (Asp)) = Name_Ghost then
12434 return Enables_Ghostness (Expression (Asp));
12435 end if;
12437 Next (Asp);
12438 end loop;
12439 end if;
12441 Decl := Empty;
12443 -- When the context is a [generic] package declaration, pragma Ghost
12444 -- resides in the visible declarations.
12446 if Nkind_In (N, N_Generic_Package_Declaration,
12447 N_Package_Declaration)
12448 then
12449 Decl := First (Visible_Declarations (Specification (N)));
12451 -- Otherwise pragma Ghost appears in the declarations following N
12453 elsif Is_List_Member (N) then
12454 Decl := Next (N);
12455 end if;
12457 while Present (Decl) loop
12458 if Nkind (Decl) = N_Pragma
12459 and then Pragma_Name (Decl) = Name_Ghost
12460 then
12461 return
12462 Enables_Ghostness (First (Pragma_Argument_Associations (Decl)));
12464 -- A source construct ends the region where pragma Ghost may appear,
12465 -- stop the traversal.
12467 elsif Comes_From_Source (Decl) then
12468 exit;
12469 end if;
12471 Next (Decl);
12472 end loop;
12474 return False;
12475 end Is_Subject_To_Ghost;
12477 --------------------------------------------------
12478 -- Is_Subprogram_Stub_Without_Prior_Declaration --
12479 --------------------------------------------------
12481 function Is_Subprogram_Stub_Without_Prior_Declaration
12482 (N : Node_Id) return Boolean
12484 begin
12485 -- A subprogram stub without prior declaration serves as declaration for
12486 -- the actual subprogram body. As such, it has an attached defining
12487 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
12489 return Nkind (N) = N_Subprogram_Body_Stub
12490 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
12491 end Is_Subprogram_Stub_Without_Prior_Declaration;
12493 ---------------------------------
12494 -- Is_Synchronized_Tagged_Type --
12495 ---------------------------------
12497 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
12498 Kind : constant Entity_Kind := Ekind (Base_Type (E));
12500 begin
12501 -- A task or protected type derived from an interface is a tagged type.
12502 -- Such a tagged type is called a synchronized tagged type, as are
12503 -- synchronized interfaces and private extensions whose declaration
12504 -- includes the reserved word synchronized.
12506 return (Is_Tagged_Type (E)
12507 and then (Kind = E_Task_Type
12508 or else
12509 Kind = E_Protected_Type))
12510 or else
12511 (Is_Interface (E)
12512 and then Is_Synchronized_Interface (E))
12513 or else
12514 (Ekind (E) = E_Record_Type_With_Private
12515 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
12516 and then (Synchronized_Present (Parent (E))
12517 or else Is_Synchronized_Interface (Etype (E))));
12518 end Is_Synchronized_Tagged_Type;
12520 -----------------
12521 -- Is_Transfer --
12522 -----------------
12524 function Is_Transfer (N : Node_Id) return Boolean is
12525 Kind : constant Node_Kind := Nkind (N);
12527 begin
12528 if Kind = N_Simple_Return_Statement
12529 or else
12530 Kind = N_Extended_Return_Statement
12531 or else
12532 Kind = N_Goto_Statement
12533 or else
12534 Kind = N_Raise_Statement
12535 or else
12536 Kind = N_Requeue_Statement
12537 then
12538 return True;
12540 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
12541 and then No (Condition (N))
12542 then
12543 return True;
12545 elsif Kind = N_Procedure_Call_Statement
12546 and then Is_Entity_Name (Name (N))
12547 and then Present (Entity (Name (N)))
12548 and then No_Return (Entity (Name (N)))
12549 then
12550 return True;
12552 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
12553 return True;
12555 else
12556 return False;
12557 end if;
12558 end Is_Transfer;
12560 -------------
12561 -- Is_True --
12562 -------------
12564 function Is_True (U : Uint) return Boolean is
12565 begin
12566 return (U /= 0);
12567 end Is_True;
12569 --------------------------------------
12570 -- Is_Unchecked_Conversion_Instance --
12571 --------------------------------------
12573 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
12574 Gen_Par : Entity_Id;
12576 begin
12577 -- Look for a function whose generic parent is the predefined intrinsic
12578 -- function Unchecked_Conversion.
12580 if Ekind (Id) = E_Function then
12581 Gen_Par := Generic_Parent (Parent (Id));
12583 return
12584 Present (Gen_Par)
12585 and then Chars (Gen_Par) = Name_Unchecked_Conversion
12586 and then Is_Intrinsic_Subprogram (Gen_Par)
12587 and then Is_Predefined_File_Name
12588 (Unit_File_Name (Get_Source_Unit (Gen_Par)));
12589 end if;
12591 return False;
12592 end Is_Unchecked_Conversion_Instance;
12594 -------------------------------
12595 -- Is_Universal_Numeric_Type --
12596 -------------------------------
12598 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
12599 begin
12600 return T = Universal_Integer or else T = Universal_Real;
12601 end Is_Universal_Numeric_Type;
12603 -------------------
12604 -- Is_Value_Type --
12605 -------------------
12607 function Is_Value_Type (T : Entity_Id) return Boolean is
12608 begin
12609 return VM_Target = CLI_Target
12610 and then Nkind (T) in N_Has_Chars
12611 and then Chars (T) /= No_Name
12612 and then Get_Name_String (Chars (T)) = "valuetype";
12613 end Is_Value_Type;
12615 ----------------------------
12616 -- Is_Variable_Size_Array --
12617 ----------------------------
12619 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
12620 Idx : Node_Id;
12622 begin
12623 pragma Assert (Is_Array_Type (E));
12625 -- Check if some index is initialized with a non-constant value
12627 Idx := First_Index (E);
12628 while Present (Idx) loop
12629 if Nkind (Idx) = N_Range then
12630 if not Is_Constant_Bound (Low_Bound (Idx))
12631 or else not Is_Constant_Bound (High_Bound (Idx))
12632 then
12633 return True;
12634 end if;
12635 end if;
12637 Idx := Next_Index (Idx);
12638 end loop;
12640 return False;
12641 end Is_Variable_Size_Array;
12643 -----------------------------
12644 -- Is_Variable_Size_Record --
12645 -----------------------------
12647 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
12648 Comp : Entity_Id;
12649 Comp_Typ : Entity_Id;
12651 begin
12652 pragma Assert (Is_Record_Type (E));
12654 Comp := First_Entity (E);
12655 while Present (Comp) loop
12656 Comp_Typ := Etype (Comp);
12658 -- Recursive call if the record type has discriminants
12660 if Is_Record_Type (Comp_Typ)
12661 and then Has_Discriminants (Comp_Typ)
12662 and then Is_Variable_Size_Record (Comp_Typ)
12663 then
12664 return True;
12666 elsif Is_Array_Type (Comp_Typ)
12667 and then Is_Variable_Size_Array (Comp_Typ)
12668 then
12669 return True;
12670 end if;
12672 Next_Entity (Comp);
12673 end loop;
12675 return False;
12676 end Is_Variable_Size_Record;
12678 -----------------
12679 -- Is_Variable --
12680 -----------------
12682 function Is_Variable
12683 (N : Node_Id;
12684 Use_Original_Node : Boolean := True) return Boolean
12686 Orig_Node : Node_Id;
12688 function In_Protected_Function (E : Entity_Id) return Boolean;
12689 -- Within a protected function, the private components of the enclosing
12690 -- protected type are constants. A function nested within a (protected)
12691 -- procedure is not itself protected. Within the body of a protected
12692 -- function the current instance of the protected type is a constant.
12694 function Is_Variable_Prefix (P : Node_Id) return Boolean;
12695 -- Prefixes can involve implicit dereferences, in which case we must
12696 -- test for the case of a reference of a constant access type, which can
12697 -- can never be a variable.
12699 ---------------------------
12700 -- In_Protected_Function --
12701 ---------------------------
12703 function In_Protected_Function (E : Entity_Id) return Boolean is
12704 Prot : Entity_Id;
12705 S : Entity_Id;
12707 begin
12708 -- E is the current instance of a type
12710 if Is_Type (E) then
12711 Prot := E;
12713 -- E is an object
12715 else
12716 Prot := Scope (E);
12717 end if;
12719 if not Is_Protected_Type (Prot) then
12720 return False;
12722 else
12723 S := Current_Scope;
12724 while Present (S) and then S /= Prot loop
12725 if Ekind (S) = E_Function and then Scope (S) = Prot then
12726 return True;
12727 end if;
12729 S := Scope (S);
12730 end loop;
12732 return False;
12733 end if;
12734 end In_Protected_Function;
12736 ------------------------
12737 -- Is_Variable_Prefix --
12738 ------------------------
12740 function Is_Variable_Prefix (P : Node_Id) return Boolean is
12741 begin
12742 if Is_Access_Type (Etype (P)) then
12743 return not Is_Access_Constant (Root_Type (Etype (P)));
12745 -- For the case of an indexed component whose prefix has a packed
12746 -- array type, the prefix has been rewritten into a type conversion.
12747 -- Determine variable-ness from the converted expression.
12749 elsif Nkind (P) = N_Type_Conversion
12750 and then not Comes_From_Source (P)
12751 and then Is_Array_Type (Etype (P))
12752 and then Is_Packed (Etype (P))
12753 then
12754 return Is_Variable (Expression (P));
12756 else
12757 return Is_Variable (P);
12758 end if;
12759 end Is_Variable_Prefix;
12761 -- Start of processing for Is_Variable
12763 begin
12764 -- Check if we perform the test on the original node since this may be a
12765 -- test of syntactic categories which must not be disturbed by whatever
12766 -- rewriting might have occurred. For example, an aggregate, which is
12767 -- certainly NOT a variable, could be turned into a variable by
12768 -- expansion.
12770 if Use_Original_Node then
12771 Orig_Node := Original_Node (N);
12772 else
12773 Orig_Node := N;
12774 end if;
12776 -- Definitely OK if Assignment_OK is set. Since this is something that
12777 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
12779 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
12780 return True;
12782 -- Normally we go to the original node, but there is one exception where
12783 -- we use the rewritten node, namely when it is an explicit dereference.
12784 -- The generated code may rewrite a prefix which is an access type with
12785 -- an explicit dereference. The dereference is a variable, even though
12786 -- the original node may not be (since it could be a constant of the
12787 -- access type).
12789 -- In Ada 2005 we have a further case to consider: the prefix may be a
12790 -- function call given in prefix notation. The original node appears to
12791 -- be a selected component, but we need to examine the call.
12793 elsif Nkind (N) = N_Explicit_Dereference
12794 and then Nkind (Orig_Node) /= N_Explicit_Dereference
12795 and then Present (Etype (Orig_Node))
12796 and then Is_Access_Type (Etype (Orig_Node))
12797 then
12798 -- Note that if the prefix is an explicit dereference that does not
12799 -- come from source, we must check for a rewritten function call in
12800 -- prefixed notation before other forms of rewriting, to prevent a
12801 -- compiler crash.
12803 return
12804 (Nkind (Orig_Node) = N_Function_Call
12805 and then not Is_Access_Constant (Etype (Prefix (N))))
12806 or else
12807 Is_Variable_Prefix (Original_Node (Prefix (N)));
12809 -- in Ada 2012, the dereference may have been added for a type with
12810 -- a declared implicit dereference aspect.
12812 elsif Nkind (N) = N_Explicit_Dereference
12813 and then Present (Etype (Orig_Node))
12814 and then Ada_Version >= Ada_2012
12815 and then Has_Implicit_Dereference (Etype (Orig_Node))
12816 then
12817 return True;
12819 -- A function call is never a variable
12821 elsif Nkind (N) = N_Function_Call then
12822 return False;
12824 -- All remaining checks use the original node
12826 elsif Is_Entity_Name (Orig_Node)
12827 and then Present (Entity (Orig_Node))
12828 then
12829 declare
12830 E : constant Entity_Id := Entity (Orig_Node);
12831 K : constant Entity_Kind := Ekind (E);
12833 begin
12834 return (K = E_Variable
12835 and then Nkind (Parent (E)) /= N_Exception_Handler)
12836 or else (K = E_Component
12837 and then not In_Protected_Function (E))
12838 or else K = E_Out_Parameter
12839 or else K = E_In_Out_Parameter
12840 or else K = E_Generic_In_Out_Parameter
12842 -- Current instance of type. If this is a protected type, check
12843 -- we are not within the body of one of its protected functions.
12845 or else (Is_Type (E)
12846 and then In_Open_Scopes (E)
12847 and then not In_Protected_Function (E))
12849 or else (Is_Incomplete_Or_Private_Type (E)
12850 and then In_Open_Scopes (Full_View (E)));
12851 end;
12853 else
12854 case Nkind (Orig_Node) is
12855 when N_Indexed_Component | N_Slice =>
12856 return Is_Variable_Prefix (Prefix (Orig_Node));
12858 when N_Selected_Component =>
12859 return (Is_Variable (Selector_Name (Orig_Node))
12860 and then Is_Variable_Prefix (Prefix (Orig_Node)))
12861 or else
12862 (Nkind (N) = N_Expanded_Name
12863 and then Scope (Entity (N)) = Entity (Prefix (N)));
12865 -- For an explicit dereference, the type of the prefix cannot
12866 -- be an access to constant or an access to subprogram.
12868 when N_Explicit_Dereference =>
12869 declare
12870 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
12871 begin
12872 return Is_Access_Type (Typ)
12873 and then not Is_Access_Constant (Root_Type (Typ))
12874 and then Ekind (Typ) /= E_Access_Subprogram_Type;
12875 end;
12877 -- The type conversion is the case where we do not deal with the
12878 -- context dependent special case of an actual parameter. Thus
12879 -- the type conversion is only considered a variable for the
12880 -- purposes of this routine if the target type is tagged. However,
12881 -- a type conversion is considered to be a variable if it does not
12882 -- come from source (this deals for example with the conversions
12883 -- of expressions to their actual subtypes).
12885 when N_Type_Conversion =>
12886 return Is_Variable (Expression (Orig_Node))
12887 and then
12888 (not Comes_From_Source (Orig_Node)
12889 or else
12890 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
12891 and then
12892 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
12894 -- GNAT allows an unchecked type conversion as a variable. This
12895 -- only affects the generation of internal expanded code, since
12896 -- calls to instantiations of Unchecked_Conversion are never
12897 -- considered variables (since they are function calls).
12899 when N_Unchecked_Type_Conversion =>
12900 return Is_Variable (Expression (Orig_Node));
12902 when others =>
12903 return False;
12904 end case;
12905 end if;
12906 end Is_Variable;
12908 ---------------------------
12909 -- Is_Visibly_Controlled --
12910 ---------------------------
12912 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
12913 Root : constant Entity_Id := Root_Type (T);
12914 begin
12915 return Chars (Scope (Root)) = Name_Finalization
12916 and then Chars (Scope (Scope (Root))) = Name_Ada
12917 and then Scope (Scope (Scope (Root))) = Standard_Standard;
12918 end Is_Visibly_Controlled;
12920 ------------------------
12921 -- Is_Volatile_Object --
12922 ------------------------
12924 function Is_Volatile_Object (N : Node_Id) return Boolean is
12926 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
12927 -- If prefix is an implicit dereference, examine designated type
12929 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
12930 -- Determines if given object has volatile components
12932 ------------------------
12933 -- Is_Volatile_Prefix --
12934 ------------------------
12936 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
12937 Typ : constant Entity_Id := Etype (N);
12939 begin
12940 if Is_Access_Type (Typ) then
12941 declare
12942 Dtyp : constant Entity_Id := Designated_Type (Typ);
12944 begin
12945 return Is_Volatile (Dtyp)
12946 or else Has_Volatile_Components (Dtyp);
12947 end;
12949 else
12950 return Object_Has_Volatile_Components (N);
12951 end if;
12952 end Is_Volatile_Prefix;
12954 ------------------------------------
12955 -- Object_Has_Volatile_Components --
12956 ------------------------------------
12958 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
12959 Typ : constant Entity_Id := Etype (N);
12961 begin
12962 if Is_Volatile (Typ)
12963 or else Has_Volatile_Components (Typ)
12964 then
12965 return True;
12967 elsif Is_Entity_Name (N)
12968 and then (Has_Volatile_Components (Entity (N))
12969 or else Is_Volatile (Entity (N)))
12970 then
12971 return True;
12973 elsif Nkind (N) = N_Indexed_Component
12974 or else Nkind (N) = N_Selected_Component
12975 then
12976 return Is_Volatile_Prefix (Prefix (N));
12978 else
12979 return False;
12980 end if;
12981 end Object_Has_Volatile_Components;
12983 -- Start of processing for Is_Volatile_Object
12985 begin
12986 if Nkind (N) = N_Defining_Identifier then
12987 return Is_Volatile (N) or else Is_Volatile (Etype (N));
12989 elsif Nkind (N) = N_Expanded_Name then
12990 return Is_Volatile_Object (Entity (N));
12992 elsif Is_Volatile (Etype (N))
12993 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
12994 then
12995 return True;
12997 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
12998 and then Is_Volatile_Prefix (Prefix (N))
12999 then
13000 return True;
13002 elsif Nkind (N) = N_Selected_Component
13003 and then Is_Volatile (Entity (Selector_Name (N)))
13004 then
13005 return True;
13007 else
13008 return False;
13009 end if;
13010 end Is_Volatile_Object;
13012 ---------------------------
13013 -- Itype_Has_Declaration --
13014 ---------------------------
13016 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
13017 begin
13018 pragma Assert (Is_Itype (Id));
13019 return Present (Parent (Id))
13020 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
13021 N_Subtype_Declaration)
13022 and then Defining_Entity (Parent (Id)) = Id;
13023 end Itype_Has_Declaration;
13025 -------------------------
13026 -- Kill_Current_Values --
13027 -------------------------
13029 procedure Kill_Current_Values
13030 (Ent : Entity_Id;
13031 Last_Assignment_Only : Boolean := False)
13033 begin
13034 if Is_Assignable (Ent) then
13035 Set_Last_Assignment (Ent, Empty);
13036 end if;
13038 if Is_Object (Ent) then
13039 if not Last_Assignment_Only then
13040 Kill_Checks (Ent);
13041 Set_Current_Value (Ent, Empty);
13043 if not Can_Never_Be_Null (Ent) then
13044 Set_Is_Known_Non_Null (Ent, False);
13045 end if;
13047 Set_Is_Known_Null (Ent, False);
13049 -- Reset Is_Known_Valid unless type is always valid, or if we have
13050 -- a loop parameter (loop parameters are always valid, since their
13051 -- bounds are defined by the bounds given in the loop header).
13053 if not Is_Known_Valid (Etype (Ent))
13054 and then Ekind (Ent) /= E_Loop_Parameter
13055 then
13056 Set_Is_Known_Valid (Ent, False);
13057 end if;
13058 end if;
13059 end if;
13060 end Kill_Current_Values;
13062 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
13063 S : Entity_Id;
13065 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
13066 -- Clear current value for entity E and all entities chained to E
13068 ------------------------------------------
13069 -- Kill_Current_Values_For_Entity_Chain --
13070 ------------------------------------------
13072 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
13073 Ent : Entity_Id;
13074 begin
13075 Ent := E;
13076 while Present (Ent) loop
13077 Kill_Current_Values (Ent, Last_Assignment_Only);
13078 Next_Entity (Ent);
13079 end loop;
13080 end Kill_Current_Values_For_Entity_Chain;
13082 -- Start of processing for Kill_Current_Values
13084 begin
13085 -- Kill all saved checks, a special case of killing saved values
13087 if not Last_Assignment_Only then
13088 Kill_All_Checks;
13089 end if;
13091 -- Loop through relevant scopes, which includes the current scope and
13092 -- any parent scopes if the current scope is a block or a package.
13094 S := Current_Scope;
13095 Scope_Loop : loop
13097 -- Clear current values of all entities in current scope
13099 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
13101 -- If scope is a package, also clear current values of all private
13102 -- entities in the scope.
13104 if Is_Package_Or_Generic_Package (S)
13105 or else Is_Concurrent_Type (S)
13106 then
13107 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
13108 end if;
13110 -- If this is a not a subprogram, deal with parents
13112 if not Is_Subprogram (S) then
13113 S := Scope (S);
13114 exit Scope_Loop when S = Standard_Standard;
13115 else
13116 exit Scope_Loop;
13117 end if;
13118 end loop Scope_Loop;
13119 end Kill_Current_Values;
13121 --------------------------
13122 -- Kill_Size_Check_Code --
13123 --------------------------
13125 procedure Kill_Size_Check_Code (E : Entity_Id) is
13126 begin
13127 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
13128 and then Present (Size_Check_Code (E))
13129 then
13130 Remove (Size_Check_Code (E));
13131 Set_Size_Check_Code (E, Empty);
13132 end if;
13133 end Kill_Size_Check_Code;
13135 --------------------------
13136 -- Known_To_Be_Assigned --
13137 --------------------------
13139 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
13140 P : constant Node_Id := Parent (N);
13142 begin
13143 case Nkind (P) is
13145 -- Test left side of assignment
13147 when N_Assignment_Statement =>
13148 return N = Name (P);
13150 -- Function call arguments are never lvalues
13152 when N_Function_Call =>
13153 return False;
13155 -- Positional parameter for procedure or accept call
13157 when N_Procedure_Call_Statement |
13158 N_Accept_Statement
13160 declare
13161 Proc : Entity_Id;
13162 Form : Entity_Id;
13163 Act : Node_Id;
13165 begin
13166 Proc := Get_Subprogram_Entity (P);
13168 if No (Proc) then
13169 return False;
13170 end if;
13172 -- If we are not a list member, something is strange, so
13173 -- be conservative and return False.
13175 if not Is_List_Member (N) then
13176 return False;
13177 end if;
13179 -- We are going to find the right formal by stepping forward
13180 -- through the formals, as we step backwards in the actuals.
13182 Form := First_Formal (Proc);
13183 Act := N;
13184 loop
13185 -- If no formal, something is weird, so be conservative
13186 -- and return False.
13188 if No (Form) then
13189 return False;
13190 end if;
13192 Prev (Act);
13193 exit when No (Act);
13194 Next_Formal (Form);
13195 end loop;
13197 return Ekind (Form) /= E_In_Parameter;
13198 end;
13200 -- Named parameter for procedure or accept call
13202 when N_Parameter_Association =>
13203 declare
13204 Proc : Entity_Id;
13205 Form : Entity_Id;
13207 begin
13208 Proc := Get_Subprogram_Entity (Parent (P));
13210 if No (Proc) then
13211 return False;
13212 end if;
13214 -- Loop through formals to find the one that matches
13216 Form := First_Formal (Proc);
13217 loop
13218 -- If no matching formal, that's peculiar, some kind of
13219 -- previous error, so return False to be conservative.
13220 -- Actually this also happens in legal code in the case
13221 -- where P is a parameter association for an Extra_Formal???
13223 if No (Form) then
13224 return False;
13225 end if;
13227 -- Else test for match
13229 if Chars (Form) = Chars (Selector_Name (P)) then
13230 return Ekind (Form) /= E_In_Parameter;
13231 end if;
13233 Next_Formal (Form);
13234 end loop;
13235 end;
13237 -- Test for appearing in a conversion that itself appears
13238 -- in an lvalue context, since this should be an lvalue.
13240 when N_Type_Conversion =>
13241 return Known_To_Be_Assigned (P);
13243 -- All other references are definitely not known to be modifications
13245 when others =>
13246 return False;
13248 end case;
13249 end Known_To_Be_Assigned;
13251 ---------------------------
13252 -- Last_Source_Statement --
13253 ---------------------------
13255 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
13256 N : Node_Id;
13258 begin
13259 N := Last (Statements (HSS));
13260 while Present (N) loop
13261 exit when Comes_From_Source (N);
13262 Prev (N);
13263 end loop;
13265 return N;
13266 end Last_Source_Statement;
13268 ----------------------------------
13269 -- Matching_Static_Array_Bounds --
13270 ----------------------------------
13272 function Matching_Static_Array_Bounds
13273 (L_Typ : Node_Id;
13274 R_Typ : Node_Id) return Boolean
13276 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
13277 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
13279 L_Index : Node_Id;
13280 R_Index : Node_Id;
13281 L_Low : Node_Id;
13282 L_High : Node_Id;
13283 L_Len : Uint;
13284 R_Low : Node_Id;
13285 R_High : Node_Id;
13286 R_Len : Uint;
13288 begin
13289 if L_Ndims /= R_Ndims then
13290 return False;
13291 end if;
13293 -- Unconstrained types do not have static bounds
13295 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
13296 return False;
13297 end if;
13299 -- First treat specially the first dimension, as the lower bound and
13300 -- length of string literals are not stored like those of arrays.
13302 if Ekind (L_Typ) = E_String_Literal_Subtype then
13303 L_Low := String_Literal_Low_Bound (L_Typ);
13304 L_Len := String_Literal_Length (L_Typ);
13305 else
13306 L_Index := First_Index (L_Typ);
13307 Get_Index_Bounds (L_Index, L_Low, L_High);
13309 if Is_OK_Static_Expression (L_Low)
13310 and then
13311 Is_OK_Static_Expression (L_High)
13312 then
13313 if Expr_Value (L_High) < Expr_Value (L_Low) then
13314 L_Len := Uint_0;
13315 else
13316 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
13317 end if;
13318 else
13319 return False;
13320 end if;
13321 end if;
13323 if Ekind (R_Typ) = E_String_Literal_Subtype then
13324 R_Low := String_Literal_Low_Bound (R_Typ);
13325 R_Len := String_Literal_Length (R_Typ);
13326 else
13327 R_Index := First_Index (R_Typ);
13328 Get_Index_Bounds (R_Index, R_Low, R_High);
13330 if Is_OK_Static_Expression (R_Low)
13331 and then
13332 Is_OK_Static_Expression (R_High)
13333 then
13334 if Expr_Value (R_High) < Expr_Value (R_Low) then
13335 R_Len := Uint_0;
13336 else
13337 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
13338 end if;
13339 else
13340 return False;
13341 end if;
13342 end if;
13344 if (Is_OK_Static_Expression (L_Low)
13345 and then
13346 Is_OK_Static_Expression (R_Low))
13347 and then Expr_Value (L_Low) = Expr_Value (R_Low)
13348 and then L_Len = R_Len
13349 then
13350 null;
13351 else
13352 return False;
13353 end if;
13355 -- Then treat all other dimensions
13357 for Indx in 2 .. L_Ndims loop
13358 Next (L_Index);
13359 Next (R_Index);
13361 Get_Index_Bounds (L_Index, L_Low, L_High);
13362 Get_Index_Bounds (R_Index, R_Low, R_High);
13364 if (Is_OK_Static_Expression (L_Low) and then
13365 Is_OK_Static_Expression (L_High) and then
13366 Is_OK_Static_Expression (R_Low) and then
13367 Is_OK_Static_Expression (R_High))
13368 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
13369 and then
13370 Expr_Value (L_High) = Expr_Value (R_High))
13371 then
13372 null;
13373 else
13374 return False;
13375 end if;
13376 end loop;
13378 -- If we fall through the loop, all indexes matched
13380 return True;
13381 end Matching_Static_Array_Bounds;
13383 -------------------
13384 -- May_Be_Lvalue --
13385 -------------------
13387 function May_Be_Lvalue (N : Node_Id) return Boolean is
13388 P : constant Node_Id := Parent (N);
13390 begin
13391 case Nkind (P) is
13393 -- Test left side of assignment
13395 when N_Assignment_Statement =>
13396 return N = Name (P);
13398 -- Test prefix of component or attribute. Note that the prefix of an
13399 -- explicit or implicit dereference cannot be an l-value.
13401 when N_Attribute_Reference =>
13402 return N = Prefix (P)
13403 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
13405 -- For an expanded name, the name is an lvalue if the expanded name
13406 -- is an lvalue, but the prefix is never an lvalue, since it is just
13407 -- the scope where the name is found.
13409 when N_Expanded_Name =>
13410 if N = Prefix (P) then
13411 return May_Be_Lvalue (P);
13412 else
13413 return False;
13414 end if;
13416 -- For a selected component A.B, A is certainly an lvalue if A.B is.
13417 -- B is a little interesting, if we have A.B := 3, there is some
13418 -- discussion as to whether B is an lvalue or not, we choose to say
13419 -- it is. Note however that A is not an lvalue if it is of an access
13420 -- type since this is an implicit dereference.
13422 when N_Selected_Component =>
13423 if N = Prefix (P)
13424 and then Present (Etype (N))
13425 and then Is_Access_Type (Etype (N))
13426 then
13427 return False;
13428 else
13429 return May_Be_Lvalue (P);
13430 end if;
13432 -- For an indexed component or slice, the index or slice bounds is
13433 -- never an lvalue. The prefix is an lvalue if the indexed component
13434 -- or slice is an lvalue, except if it is an access type, where we
13435 -- have an implicit dereference.
13437 when N_Indexed_Component | N_Slice =>
13438 if N /= Prefix (P)
13439 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
13440 then
13441 return False;
13442 else
13443 return May_Be_Lvalue (P);
13444 end if;
13446 -- Prefix of a reference is an lvalue if the reference is an lvalue
13448 when N_Reference =>
13449 return May_Be_Lvalue (P);
13451 -- Prefix of explicit dereference is never an lvalue
13453 when N_Explicit_Dereference =>
13454 return False;
13456 -- Positional parameter for subprogram, entry, or accept call.
13457 -- In older versions of Ada function call arguments are never
13458 -- lvalues. In Ada 2012 functions can have in-out parameters.
13460 when N_Subprogram_Call |
13461 N_Entry_Call_Statement |
13462 N_Accept_Statement
13464 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
13465 return False;
13466 end if;
13468 -- The following mechanism is clumsy and fragile. A single flag
13469 -- set in Resolve_Actuals would be preferable ???
13471 declare
13472 Proc : Entity_Id;
13473 Form : Entity_Id;
13474 Act : Node_Id;
13476 begin
13477 Proc := Get_Subprogram_Entity (P);
13479 if No (Proc) then
13480 return True;
13481 end if;
13483 -- If we are not a list member, something is strange, so be
13484 -- conservative and return True.
13486 if not Is_List_Member (N) then
13487 return True;
13488 end if;
13490 -- We are going to find the right formal by stepping forward
13491 -- through the formals, as we step backwards in the actuals.
13493 Form := First_Formal (Proc);
13494 Act := N;
13495 loop
13496 -- If no formal, something is weird, so be conservative and
13497 -- return True.
13499 if No (Form) then
13500 return True;
13501 end if;
13503 Prev (Act);
13504 exit when No (Act);
13505 Next_Formal (Form);
13506 end loop;
13508 return Ekind (Form) /= E_In_Parameter;
13509 end;
13511 -- Named parameter for procedure or accept call
13513 when N_Parameter_Association =>
13514 declare
13515 Proc : Entity_Id;
13516 Form : Entity_Id;
13518 begin
13519 Proc := Get_Subprogram_Entity (Parent (P));
13521 if No (Proc) then
13522 return True;
13523 end if;
13525 -- Loop through formals to find the one that matches
13527 Form := First_Formal (Proc);
13528 loop
13529 -- If no matching formal, that's peculiar, some kind of
13530 -- previous error, so return True to be conservative.
13531 -- Actually happens with legal code for an unresolved call
13532 -- where we may get the wrong homonym???
13534 if No (Form) then
13535 return True;
13536 end if;
13538 -- Else test for match
13540 if Chars (Form) = Chars (Selector_Name (P)) then
13541 return Ekind (Form) /= E_In_Parameter;
13542 end if;
13544 Next_Formal (Form);
13545 end loop;
13546 end;
13548 -- Test for appearing in a conversion that itself appears in an
13549 -- lvalue context, since this should be an lvalue.
13551 when N_Type_Conversion =>
13552 return May_Be_Lvalue (P);
13554 -- Test for appearance in object renaming declaration
13556 when N_Object_Renaming_Declaration =>
13557 return True;
13559 -- All other references are definitely not lvalues
13561 when others =>
13562 return False;
13564 end case;
13565 end May_Be_Lvalue;
13567 -----------------------
13568 -- Mark_Coextensions --
13569 -----------------------
13571 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
13572 Is_Dynamic : Boolean;
13573 -- Indicates whether the context causes nested coextensions to be
13574 -- dynamic or static
13576 function Mark_Allocator (N : Node_Id) return Traverse_Result;
13577 -- Recognize an allocator node and label it as a dynamic coextension
13579 --------------------
13580 -- Mark_Allocator --
13581 --------------------
13583 function Mark_Allocator (N : Node_Id) return Traverse_Result is
13584 begin
13585 if Nkind (N) = N_Allocator then
13586 if Is_Dynamic then
13587 Set_Is_Dynamic_Coextension (N);
13589 -- If the allocator expression is potentially dynamic, it may
13590 -- be expanded out of order and require dynamic allocation
13591 -- anyway, so we treat the coextension itself as dynamic.
13592 -- Potential optimization ???
13594 elsif Nkind (Expression (N)) = N_Qualified_Expression
13595 and then Nkind (Expression (Expression (N))) = N_Op_Concat
13596 then
13597 Set_Is_Dynamic_Coextension (N);
13598 else
13599 Set_Is_Static_Coextension (N);
13600 end if;
13601 end if;
13603 return OK;
13604 end Mark_Allocator;
13606 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
13608 -- Start of processing Mark_Coextensions
13610 begin
13611 case Nkind (Context_Nod) is
13613 -- Comment here ???
13615 when N_Assignment_Statement =>
13616 Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
13618 -- An allocator that is a component of a returned aggregate
13619 -- must be dynamic.
13621 when N_Simple_Return_Statement =>
13622 declare
13623 Expr : constant Node_Id := Expression (Context_Nod);
13624 begin
13625 Is_Dynamic :=
13626 Nkind (Expr) = N_Allocator
13627 or else
13628 (Nkind (Expr) = N_Qualified_Expression
13629 and then Nkind (Expression (Expr)) = N_Aggregate);
13630 end;
13632 -- An alloctor within an object declaration in an extended return
13633 -- statement is of necessity dynamic.
13635 when N_Object_Declaration =>
13636 Is_Dynamic := Nkind (Root_Nod) = N_Allocator
13637 or else
13638 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
13640 -- This routine should not be called for constructs which may not
13641 -- contain coextensions.
13643 when others =>
13644 raise Program_Error;
13645 end case;
13647 Mark_Allocators (Root_Nod);
13648 end Mark_Coextensions;
13650 ----------------------
13651 -- Needs_One_Actual --
13652 ----------------------
13654 function Needs_One_Actual (E : Entity_Id) return Boolean is
13655 Formal : Entity_Id;
13657 begin
13658 -- Ada 2005 or later, and formals present
13660 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
13661 Formal := Next_Formal (First_Formal (E));
13662 while Present (Formal) loop
13663 if No (Default_Value (Formal)) then
13664 return False;
13665 end if;
13667 Next_Formal (Formal);
13668 end loop;
13670 return True;
13672 -- Ada 83/95 or no formals
13674 else
13675 return False;
13676 end if;
13677 end Needs_One_Actual;
13679 ------------------------
13680 -- New_Copy_List_Tree --
13681 ------------------------
13683 function New_Copy_List_Tree (List : List_Id) return List_Id is
13684 NL : List_Id;
13685 E : Node_Id;
13687 begin
13688 if List = No_List then
13689 return No_List;
13691 else
13692 NL := New_List;
13693 E := First (List);
13695 while Present (E) loop
13696 Append (New_Copy_Tree (E), NL);
13697 E := Next (E);
13698 end loop;
13700 return NL;
13701 end if;
13702 end New_Copy_List_Tree;
13704 --------------------------------------------------
13705 -- New_Copy_Tree Auxiliary Data and Subprograms --
13706 --------------------------------------------------
13708 use Atree.Unchecked_Access;
13709 use Atree_Private_Part;
13711 -- Our approach here requires a two pass traversal of the tree. The
13712 -- first pass visits all nodes that eventually will be copied looking
13713 -- for defining Itypes. If any defining Itypes are found, then they are
13714 -- copied, and an entry is added to the replacement map. In the second
13715 -- phase, the tree is copied, using the replacement map to replace any
13716 -- Itype references within the copied tree.
13718 -- The following hash tables are used if the Map supplied has more
13719 -- than hash threshold entries to speed up access to the map. If
13720 -- there are fewer entries, then the map is searched sequentially
13721 -- (because setting up a hash table for only a few entries takes
13722 -- more time than it saves.
13724 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
13725 -- Hash function used for hash operations
13727 -------------------
13728 -- New_Copy_Hash --
13729 -------------------
13731 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
13732 begin
13733 return Nat (E) mod (NCT_Header_Num'Last + 1);
13734 end New_Copy_Hash;
13736 ---------------
13737 -- NCT_Assoc --
13738 ---------------
13740 -- The hash table NCT_Assoc associates old entities in the table
13741 -- with their corresponding new entities (i.e. the pairs of entries
13742 -- presented in the original Map argument are Key-Element pairs).
13744 package NCT_Assoc is new Simple_HTable (
13745 Header_Num => NCT_Header_Num,
13746 Element => Entity_Id,
13747 No_Element => Empty,
13748 Key => Entity_Id,
13749 Hash => New_Copy_Hash,
13750 Equal => Types."=");
13752 ---------------------
13753 -- NCT_Itype_Assoc --
13754 ---------------------
13756 -- The hash table NCT_Itype_Assoc contains entries only for those
13757 -- old nodes which have a non-empty Associated_Node_For_Itype set.
13758 -- The key is the associated node, and the element is the new node
13759 -- itself (NOT the associated node for the new node).
13761 package NCT_Itype_Assoc is new Simple_HTable (
13762 Header_Num => NCT_Header_Num,
13763 Element => Entity_Id,
13764 No_Element => Empty,
13765 Key => Entity_Id,
13766 Hash => New_Copy_Hash,
13767 Equal => Types."=");
13769 -------------------
13770 -- New_Copy_Tree --
13771 -------------------
13773 function New_Copy_Tree
13774 (Source : Node_Id;
13775 Map : Elist_Id := No_Elist;
13776 New_Sloc : Source_Ptr := No_Location;
13777 New_Scope : Entity_Id := Empty) return Node_Id
13779 Actual_Map : Elist_Id := Map;
13780 -- This is the actual map for the copy. It is initialized with the
13781 -- given elements, and then enlarged as required for Itypes that are
13782 -- copied during the first phase of the copy operation. The visit
13783 -- procedures add elements to this map as Itypes are encountered.
13784 -- The reason we cannot use Map directly, is that it may well be
13785 -- (and normally is) initialized to No_Elist, and if we have mapped
13786 -- entities, we have to reset it to point to a real Elist.
13788 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
13789 -- Called during second phase to map entities into their corresponding
13790 -- copies using Actual_Map. If the argument is not an entity, or is not
13791 -- in Actual_Map, then it is returned unchanged.
13793 procedure Build_NCT_Hash_Tables;
13794 -- Builds hash tables (number of elements >= threshold value)
13796 function Copy_Elist_With_Replacement
13797 (Old_Elist : Elist_Id) return Elist_Id;
13798 -- Called during second phase to copy element list doing replacements
13800 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
13801 -- Called during the second phase to process a copied Itype. The actual
13802 -- copy happened during the first phase (so that we could make the entry
13803 -- in the mapping), but we still have to deal with the descendents of
13804 -- the copied Itype and copy them where necessary.
13806 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
13807 -- Called during second phase to copy list doing replacements
13809 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
13810 -- Called during second phase to copy node doing replacements
13812 procedure Visit_Elist (E : Elist_Id);
13813 -- Called during first phase to visit all elements of an Elist
13815 procedure Visit_Field (F : Union_Id; N : Node_Id);
13816 -- Visit a single field, recursing to call Visit_Node or Visit_List
13817 -- if the field is a syntactic descendent of the current node (i.e.
13818 -- its parent is Node N).
13820 procedure Visit_Itype (Old_Itype : Entity_Id);
13821 -- Called during first phase to visit subsidiary fields of a defining
13822 -- Itype, and also create a copy and make an entry in the replacement
13823 -- map for the new copy.
13825 procedure Visit_List (L : List_Id);
13826 -- Called during first phase to visit all elements of a List
13828 procedure Visit_Node (N : Node_Or_Entity_Id);
13829 -- Called during first phase to visit a node and all its subtrees
13831 -----------
13832 -- Assoc --
13833 -----------
13835 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
13836 E : Elmt_Id;
13837 Ent : Entity_Id;
13839 begin
13840 if not Has_Extension (N) or else No (Actual_Map) then
13841 return N;
13843 elsif NCT_Hash_Tables_Used then
13844 Ent := NCT_Assoc.Get (Entity_Id (N));
13846 if Present (Ent) then
13847 return Ent;
13848 else
13849 return N;
13850 end if;
13852 -- No hash table used, do serial search
13854 else
13855 E := First_Elmt (Actual_Map);
13856 while Present (E) loop
13857 if Node (E) = N then
13858 return Node (Next_Elmt (E));
13859 else
13860 E := Next_Elmt (Next_Elmt (E));
13861 end if;
13862 end loop;
13863 end if;
13865 return N;
13866 end Assoc;
13868 ---------------------------
13869 -- Build_NCT_Hash_Tables --
13870 ---------------------------
13872 procedure Build_NCT_Hash_Tables is
13873 Elmt : Elmt_Id;
13874 Ent : Entity_Id;
13875 begin
13876 if NCT_Hash_Table_Setup then
13877 NCT_Assoc.Reset;
13878 NCT_Itype_Assoc.Reset;
13879 end if;
13881 Elmt := First_Elmt (Actual_Map);
13882 while Present (Elmt) loop
13883 Ent := Node (Elmt);
13885 -- Get new entity, and associate old and new
13887 Next_Elmt (Elmt);
13888 NCT_Assoc.Set (Ent, Node (Elmt));
13890 if Is_Type (Ent) then
13891 declare
13892 Anode : constant Entity_Id :=
13893 Associated_Node_For_Itype (Ent);
13895 begin
13896 if Present (Anode) then
13898 -- Enter a link between the associated node of the
13899 -- old Itype and the new Itype, for updating later
13900 -- when node is copied.
13902 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
13903 end if;
13904 end;
13905 end if;
13907 Next_Elmt (Elmt);
13908 end loop;
13910 NCT_Hash_Tables_Used := True;
13911 NCT_Hash_Table_Setup := True;
13912 end Build_NCT_Hash_Tables;
13914 ---------------------------------
13915 -- Copy_Elist_With_Replacement --
13916 ---------------------------------
13918 function Copy_Elist_With_Replacement
13919 (Old_Elist : Elist_Id) return Elist_Id
13921 M : Elmt_Id;
13922 New_Elist : Elist_Id;
13924 begin
13925 if No (Old_Elist) then
13926 return No_Elist;
13928 else
13929 New_Elist := New_Elmt_List;
13931 M := First_Elmt (Old_Elist);
13932 while Present (M) loop
13933 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
13934 Next_Elmt (M);
13935 end loop;
13936 end if;
13938 return New_Elist;
13939 end Copy_Elist_With_Replacement;
13941 ---------------------------------
13942 -- Copy_Itype_With_Replacement --
13943 ---------------------------------
13945 -- This routine exactly parallels its phase one analog Visit_Itype,
13947 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
13948 begin
13949 -- Translate Next_Entity, Scope and Etype fields, in case they
13950 -- reference entities that have been mapped into copies.
13952 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
13953 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
13955 if Present (New_Scope) then
13956 Set_Scope (New_Itype, New_Scope);
13957 else
13958 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
13959 end if;
13961 -- Copy referenced fields
13963 if Is_Discrete_Type (New_Itype) then
13964 Set_Scalar_Range (New_Itype,
13965 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
13967 elsif Has_Discriminants (Base_Type (New_Itype)) then
13968 Set_Discriminant_Constraint (New_Itype,
13969 Copy_Elist_With_Replacement
13970 (Discriminant_Constraint (New_Itype)));
13972 elsif Is_Array_Type (New_Itype) then
13973 if Present (First_Index (New_Itype)) then
13974 Set_First_Index (New_Itype,
13975 First (Copy_List_With_Replacement
13976 (List_Containing (First_Index (New_Itype)))));
13977 end if;
13979 if Is_Packed (New_Itype) then
13980 Set_Packed_Array_Impl_Type (New_Itype,
13981 Copy_Node_With_Replacement
13982 (Packed_Array_Impl_Type (New_Itype)));
13983 end if;
13984 end if;
13985 end Copy_Itype_With_Replacement;
13987 --------------------------------
13988 -- Copy_List_With_Replacement --
13989 --------------------------------
13991 function Copy_List_With_Replacement
13992 (Old_List : List_Id) return List_Id
13994 New_List : List_Id;
13995 E : Node_Id;
13997 begin
13998 if Old_List = No_List then
13999 return No_List;
14001 else
14002 New_List := Empty_List;
14004 E := First (Old_List);
14005 while Present (E) loop
14006 Append (Copy_Node_With_Replacement (E), New_List);
14007 Next (E);
14008 end loop;
14010 return New_List;
14011 end if;
14012 end Copy_List_With_Replacement;
14014 --------------------------------
14015 -- Copy_Node_With_Replacement --
14016 --------------------------------
14018 function Copy_Node_With_Replacement
14019 (Old_Node : Node_Id) return Node_Id
14021 New_Node : Node_Id;
14023 procedure Adjust_Named_Associations
14024 (Old_Node : Node_Id;
14025 New_Node : Node_Id);
14026 -- If a call node has named associations, these are chained through
14027 -- the First_Named_Actual, Next_Named_Actual links. These must be
14028 -- propagated separately to the new parameter list, because these
14029 -- are not syntactic fields.
14031 function Copy_Field_With_Replacement
14032 (Field : Union_Id) return Union_Id;
14033 -- Given Field, which is a field of Old_Node, return a copy of it
14034 -- if it is a syntactic field (i.e. its parent is Node), setting
14035 -- the parent of the copy to poit to New_Node. Otherwise returns
14036 -- the field (possibly mapped if it is an entity).
14038 -------------------------------
14039 -- Adjust_Named_Associations --
14040 -------------------------------
14042 procedure Adjust_Named_Associations
14043 (Old_Node : Node_Id;
14044 New_Node : Node_Id)
14046 Old_E : Node_Id;
14047 New_E : Node_Id;
14049 Old_Next : Node_Id;
14050 New_Next : Node_Id;
14052 begin
14053 Old_E := First (Parameter_Associations (Old_Node));
14054 New_E := First (Parameter_Associations (New_Node));
14055 while Present (Old_E) loop
14056 if Nkind (Old_E) = N_Parameter_Association
14057 and then Present (Next_Named_Actual (Old_E))
14058 then
14059 if First_Named_Actual (Old_Node)
14060 = Explicit_Actual_Parameter (Old_E)
14061 then
14062 Set_First_Named_Actual
14063 (New_Node, Explicit_Actual_Parameter (New_E));
14064 end if;
14066 -- Now scan parameter list from the beginning,to locate
14067 -- next named actual, which can be out of order.
14069 Old_Next := First (Parameter_Associations (Old_Node));
14070 New_Next := First (Parameter_Associations (New_Node));
14072 while Nkind (Old_Next) /= N_Parameter_Association
14073 or else Explicit_Actual_Parameter (Old_Next)
14074 /= Next_Named_Actual (Old_E)
14075 loop
14076 Next (Old_Next);
14077 Next (New_Next);
14078 end loop;
14080 Set_Next_Named_Actual
14081 (New_E, Explicit_Actual_Parameter (New_Next));
14082 end if;
14084 Next (Old_E);
14085 Next (New_E);
14086 end loop;
14087 end Adjust_Named_Associations;
14089 ---------------------------------
14090 -- Copy_Field_With_Replacement --
14091 ---------------------------------
14093 function Copy_Field_With_Replacement
14094 (Field : Union_Id) return Union_Id
14096 begin
14097 if Field = Union_Id (Empty) then
14098 return Field;
14100 elsif Field in Node_Range then
14101 declare
14102 Old_N : constant Node_Id := Node_Id (Field);
14103 New_N : Node_Id;
14105 begin
14106 -- If syntactic field, as indicated by the parent pointer
14107 -- being set, then copy the referenced node recursively.
14109 if Parent (Old_N) = Old_Node then
14110 New_N := Copy_Node_With_Replacement (Old_N);
14112 if New_N /= Old_N then
14113 Set_Parent (New_N, New_Node);
14114 end if;
14116 -- For semantic fields, update possible entity reference
14117 -- from the replacement map.
14119 else
14120 New_N := Assoc (Old_N);
14121 end if;
14123 return Union_Id (New_N);
14124 end;
14126 elsif Field in List_Range then
14127 declare
14128 Old_L : constant List_Id := List_Id (Field);
14129 New_L : List_Id;
14131 begin
14132 -- If syntactic field, as indicated by the parent pointer,
14133 -- then recursively copy the entire referenced list.
14135 if Parent (Old_L) = Old_Node then
14136 New_L := Copy_List_With_Replacement (Old_L);
14137 Set_Parent (New_L, New_Node);
14139 -- For semantic list, just returned unchanged
14141 else
14142 New_L := Old_L;
14143 end if;
14145 return Union_Id (New_L);
14146 end;
14148 -- Anything other than a list or a node is returned unchanged
14150 else
14151 return Field;
14152 end if;
14153 end Copy_Field_With_Replacement;
14155 -- Start of processing for Copy_Node_With_Replacement
14157 begin
14158 if Old_Node <= Empty_Or_Error then
14159 return Old_Node;
14161 elsif Has_Extension (Old_Node) then
14162 return Assoc (Old_Node);
14164 else
14165 New_Node := New_Copy (Old_Node);
14167 -- If the node we are copying is the associated node of a
14168 -- previously copied Itype, then adjust the associated node
14169 -- of the copy of that Itype accordingly.
14171 if Present (Actual_Map) then
14172 declare
14173 E : Elmt_Id;
14174 Ent : Entity_Id;
14176 begin
14177 -- Case of hash table used
14179 if NCT_Hash_Tables_Used then
14180 Ent := NCT_Itype_Assoc.Get (Old_Node);
14182 if Present (Ent) then
14183 Set_Associated_Node_For_Itype (Ent, New_Node);
14184 end if;
14186 -- Case of no hash table used
14188 else
14189 E := First_Elmt (Actual_Map);
14190 while Present (E) loop
14191 if Is_Itype (Node (E))
14192 and then
14193 Old_Node = Associated_Node_For_Itype (Node (E))
14194 then
14195 Set_Associated_Node_For_Itype
14196 (Node (Next_Elmt (E)), New_Node);
14197 end if;
14199 E := Next_Elmt (Next_Elmt (E));
14200 end loop;
14201 end if;
14202 end;
14203 end if;
14205 -- Recursively copy descendents
14207 Set_Field1
14208 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
14209 Set_Field2
14210 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
14211 Set_Field3
14212 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
14213 Set_Field4
14214 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
14215 Set_Field5
14216 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
14218 -- Adjust Sloc of new node if necessary
14220 if New_Sloc /= No_Location then
14221 Set_Sloc (New_Node, New_Sloc);
14223 -- If we adjust the Sloc, then we are essentially making
14224 -- a completely new node, so the Comes_From_Source flag
14225 -- should be reset to the proper default value.
14227 Nodes.Table (New_Node).Comes_From_Source :=
14228 Default_Node.Comes_From_Source;
14229 end if;
14231 -- If the node is call and has named associations,
14232 -- set the corresponding links in the copy.
14234 if (Nkind (Old_Node) = N_Function_Call
14235 or else Nkind (Old_Node) = N_Entry_Call_Statement
14236 or else
14237 Nkind (Old_Node) = N_Procedure_Call_Statement)
14238 and then Present (First_Named_Actual (Old_Node))
14239 then
14240 Adjust_Named_Associations (Old_Node, New_Node);
14241 end if;
14243 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
14244 -- The replacement mechanism applies to entities, and is not used
14245 -- here. Eventually we may need a more general graph-copying
14246 -- routine. For now, do a sequential search to find desired node.
14248 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
14249 and then Present (First_Real_Statement (Old_Node))
14250 then
14251 declare
14252 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
14253 N1, N2 : Node_Id;
14255 begin
14256 N1 := First (Statements (Old_Node));
14257 N2 := First (Statements (New_Node));
14259 while N1 /= Old_F loop
14260 Next (N1);
14261 Next (N2);
14262 end loop;
14264 Set_First_Real_Statement (New_Node, N2);
14265 end;
14266 end if;
14267 end if;
14269 -- All done, return copied node
14271 return New_Node;
14272 end Copy_Node_With_Replacement;
14274 -----------------
14275 -- Visit_Elist --
14276 -----------------
14278 procedure Visit_Elist (E : Elist_Id) is
14279 Elmt : Elmt_Id;
14280 begin
14281 if Present (E) then
14282 Elmt := First_Elmt (E);
14284 while Elmt /= No_Elmt loop
14285 Visit_Node (Node (Elmt));
14286 Next_Elmt (Elmt);
14287 end loop;
14288 end if;
14289 end Visit_Elist;
14291 -----------------
14292 -- Visit_Field --
14293 -----------------
14295 procedure Visit_Field (F : Union_Id; N : Node_Id) is
14296 begin
14297 if F = Union_Id (Empty) then
14298 return;
14300 elsif F in Node_Range then
14302 -- Copy node if it is syntactic, i.e. its parent pointer is
14303 -- set to point to the field that referenced it (certain
14304 -- Itypes will also meet this criterion, which is fine, since
14305 -- these are clearly Itypes that do need to be copied, since
14306 -- we are copying their parent.)
14308 if Parent (Node_Id (F)) = N then
14309 Visit_Node (Node_Id (F));
14310 return;
14312 -- Another case, if we are pointing to an Itype, then we want
14313 -- to copy it if its associated node is somewhere in the tree
14314 -- being copied.
14316 -- Note: the exclusion of self-referential copies is just an
14317 -- optimization, since the search of the already copied list
14318 -- would catch it, but it is a common case (Etype pointing
14319 -- to itself for an Itype that is a base type).
14321 elsif Has_Extension (Node_Id (F))
14322 and then Is_Itype (Entity_Id (F))
14323 and then Node_Id (F) /= N
14324 then
14325 declare
14326 P : Node_Id;
14328 begin
14329 P := Associated_Node_For_Itype (Node_Id (F));
14330 while Present (P) loop
14331 if P = Source then
14332 Visit_Node (Node_Id (F));
14333 return;
14334 else
14335 P := Parent (P);
14336 end if;
14337 end loop;
14339 -- An Itype whose parent is not being copied definitely
14340 -- should NOT be copied, since it does not belong in any
14341 -- sense to the copied subtree.
14343 return;
14344 end;
14345 end if;
14347 elsif F in List_Range and then Parent (List_Id (F)) = N then
14348 Visit_List (List_Id (F));
14349 return;
14350 end if;
14351 end Visit_Field;
14353 -----------------
14354 -- Visit_Itype --
14355 -----------------
14357 procedure Visit_Itype (Old_Itype : Entity_Id) is
14358 New_Itype : Entity_Id;
14359 E : Elmt_Id;
14360 Ent : Entity_Id;
14362 begin
14363 -- Itypes that describe the designated type of access to subprograms
14364 -- have the structure of subprogram declarations, with signatures,
14365 -- etc. Either we duplicate the signatures completely, or choose to
14366 -- share such itypes, which is fine because their elaboration will
14367 -- have no side effects.
14369 if Ekind (Old_Itype) = E_Subprogram_Type then
14370 return;
14371 end if;
14373 New_Itype := New_Copy (Old_Itype);
14375 -- The new Itype has all the attributes of the old one, and
14376 -- we just copy the contents of the entity. However, the back-end
14377 -- needs different names for debugging purposes, so we create a
14378 -- new internal name for it in all cases.
14380 Set_Chars (New_Itype, New_Internal_Name ('T'));
14382 -- If our associated node is an entity that has already been copied,
14383 -- then set the associated node of the copy to point to the right
14384 -- copy. If we have copied an Itype that is itself the associated
14385 -- node of some previously copied Itype, then we set the right
14386 -- pointer in the other direction.
14388 if Present (Actual_Map) then
14390 -- Case of hash tables used
14392 if NCT_Hash_Tables_Used then
14394 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
14396 if Present (Ent) then
14397 Set_Associated_Node_For_Itype (New_Itype, Ent);
14398 end if;
14400 Ent := NCT_Itype_Assoc.Get (Old_Itype);
14401 if Present (Ent) then
14402 Set_Associated_Node_For_Itype (Ent, New_Itype);
14404 -- If the hash table has no association for this Itype and
14405 -- its associated node, enter one now.
14407 else
14408 NCT_Itype_Assoc.Set
14409 (Associated_Node_For_Itype (Old_Itype), New_Itype);
14410 end if;
14412 -- Case of hash tables not used
14414 else
14415 E := First_Elmt (Actual_Map);
14416 while Present (E) loop
14417 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
14418 Set_Associated_Node_For_Itype
14419 (New_Itype, Node (Next_Elmt (E)));
14420 end if;
14422 if Is_Type (Node (E))
14423 and then Old_Itype = Associated_Node_For_Itype (Node (E))
14424 then
14425 Set_Associated_Node_For_Itype
14426 (Node (Next_Elmt (E)), New_Itype);
14427 end if;
14429 E := Next_Elmt (Next_Elmt (E));
14430 end loop;
14431 end if;
14432 end if;
14434 if Present (Freeze_Node (New_Itype)) then
14435 Set_Is_Frozen (New_Itype, False);
14436 Set_Freeze_Node (New_Itype, Empty);
14437 end if;
14439 -- Add new association to map
14441 if No (Actual_Map) then
14442 Actual_Map := New_Elmt_List;
14443 end if;
14445 Append_Elmt (Old_Itype, Actual_Map);
14446 Append_Elmt (New_Itype, Actual_Map);
14448 if NCT_Hash_Tables_Used then
14449 NCT_Assoc.Set (Old_Itype, New_Itype);
14451 else
14452 NCT_Table_Entries := NCT_Table_Entries + 1;
14454 if NCT_Table_Entries > NCT_Hash_Threshold then
14455 Build_NCT_Hash_Tables;
14456 end if;
14457 end if;
14459 -- If a record subtype is simply copied, the entity list will be
14460 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
14462 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
14463 Set_Cloned_Subtype (New_Itype, Old_Itype);
14464 end if;
14466 -- Visit descendents that eventually get copied
14468 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
14470 if Is_Discrete_Type (Old_Itype) then
14471 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
14473 elsif Has_Discriminants (Base_Type (Old_Itype)) then
14474 -- ??? This should involve call to Visit_Field
14475 Visit_Elist (Discriminant_Constraint (Old_Itype));
14477 elsif Is_Array_Type (Old_Itype) then
14478 if Present (First_Index (Old_Itype)) then
14479 Visit_Field (Union_Id (List_Containing
14480 (First_Index (Old_Itype))),
14481 Old_Itype);
14482 end if;
14484 if Is_Packed (Old_Itype) then
14485 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
14486 Old_Itype);
14487 end if;
14488 end if;
14489 end Visit_Itype;
14491 ----------------
14492 -- Visit_List --
14493 ----------------
14495 procedure Visit_List (L : List_Id) is
14496 N : Node_Id;
14497 begin
14498 if L /= No_List then
14499 N := First (L);
14501 while Present (N) loop
14502 Visit_Node (N);
14503 Next (N);
14504 end loop;
14505 end if;
14506 end Visit_List;
14508 ----------------
14509 -- Visit_Node --
14510 ----------------
14512 procedure Visit_Node (N : Node_Or_Entity_Id) is
14514 -- Start of processing for Visit_Node
14516 begin
14517 -- Handle case of an Itype, which must be copied
14519 if Has_Extension (N) and then Is_Itype (N) then
14521 -- Nothing to do if already in the list. This can happen with an
14522 -- Itype entity that appears more than once in the tree.
14523 -- Note that we do not want to visit descendents in this case.
14525 -- Test for already in list when hash table is used
14527 if NCT_Hash_Tables_Used then
14528 if Present (NCT_Assoc.Get (Entity_Id (N))) then
14529 return;
14530 end if;
14532 -- Test for already in list when hash table not used
14534 else
14535 declare
14536 E : Elmt_Id;
14537 begin
14538 if Present (Actual_Map) then
14539 E := First_Elmt (Actual_Map);
14540 while Present (E) loop
14541 if Node (E) = N then
14542 return;
14543 else
14544 E := Next_Elmt (Next_Elmt (E));
14545 end if;
14546 end loop;
14547 end if;
14548 end;
14549 end if;
14551 Visit_Itype (N);
14552 end if;
14554 -- Visit descendents
14556 Visit_Field (Field1 (N), N);
14557 Visit_Field (Field2 (N), N);
14558 Visit_Field (Field3 (N), N);
14559 Visit_Field (Field4 (N), N);
14560 Visit_Field (Field5 (N), N);
14561 end Visit_Node;
14563 -- Start of processing for New_Copy_Tree
14565 begin
14566 Actual_Map := Map;
14568 -- See if we should use hash table
14570 if No (Actual_Map) then
14571 NCT_Hash_Tables_Used := False;
14573 else
14574 declare
14575 Elmt : Elmt_Id;
14577 begin
14578 NCT_Table_Entries := 0;
14580 Elmt := First_Elmt (Actual_Map);
14581 while Present (Elmt) loop
14582 NCT_Table_Entries := NCT_Table_Entries + 1;
14583 Next_Elmt (Elmt);
14584 Next_Elmt (Elmt);
14585 end loop;
14587 if NCT_Table_Entries > NCT_Hash_Threshold then
14588 Build_NCT_Hash_Tables;
14589 else
14590 NCT_Hash_Tables_Used := False;
14591 end if;
14592 end;
14593 end if;
14595 -- Hash table set up if required, now start phase one by visiting
14596 -- top node (we will recursively visit the descendents).
14598 Visit_Node (Source);
14600 -- Now the second phase of the copy can start. First we process
14601 -- all the mapped entities, copying their descendents.
14603 if Present (Actual_Map) then
14604 declare
14605 Elmt : Elmt_Id;
14606 New_Itype : Entity_Id;
14607 begin
14608 Elmt := First_Elmt (Actual_Map);
14609 while Present (Elmt) loop
14610 Next_Elmt (Elmt);
14611 New_Itype := Node (Elmt);
14612 Copy_Itype_With_Replacement (New_Itype);
14613 Next_Elmt (Elmt);
14614 end loop;
14615 end;
14616 end if;
14618 -- Now we can copy the actual tree
14620 return Copy_Node_With_Replacement (Source);
14621 end New_Copy_Tree;
14623 -------------------------
14624 -- New_External_Entity --
14625 -------------------------
14627 function New_External_Entity
14628 (Kind : Entity_Kind;
14629 Scope_Id : Entity_Id;
14630 Sloc_Value : Source_Ptr;
14631 Related_Id : Entity_Id;
14632 Suffix : Character;
14633 Suffix_Index : Nat := 0;
14634 Prefix : Character := ' ') return Entity_Id
14636 N : constant Entity_Id :=
14637 Make_Defining_Identifier (Sloc_Value,
14638 New_External_Name
14639 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
14641 begin
14642 Set_Ekind (N, Kind);
14643 Set_Is_Internal (N, True);
14644 Append_Entity (N, Scope_Id);
14645 Set_Public_Status (N);
14647 if Kind in Type_Kind then
14648 Init_Size_Align (N);
14649 end if;
14651 return N;
14652 end New_External_Entity;
14654 -------------------------
14655 -- New_Internal_Entity --
14656 -------------------------
14658 function New_Internal_Entity
14659 (Kind : Entity_Kind;
14660 Scope_Id : Entity_Id;
14661 Sloc_Value : Source_Ptr;
14662 Id_Char : Character) return Entity_Id
14664 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
14666 begin
14667 Set_Ekind (N, Kind);
14668 Set_Is_Internal (N, True);
14669 Append_Entity (N, Scope_Id);
14671 if Kind in Type_Kind then
14672 Init_Size_Align (N);
14673 end if;
14675 return N;
14676 end New_Internal_Entity;
14678 -----------------
14679 -- Next_Actual --
14680 -----------------
14682 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
14683 N : Node_Id;
14685 begin
14686 -- If we are pointing at a positional parameter, it is a member of a
14687 -- node list (the list of parameters), and the next parameter is the
14688 -- next node on the list, unless we hit a parameter association, then
14689 -- we shift to using the chain whose head is the First_Named_Actual in
14690 -- the parent, and then is threaded using the Next_Named_Actual of the
14691 -- Parameter_Association. All this fiddling is because the original node
14692 -- list is in the textual call order, and what we need is the
14693 -- declaration order.
14695 if Is_List_Member (Actual_Id) then
14696 N := Next (Actual_Id);
14698 if Nkind (N) = N_Parameter_Association then
14699 return First_Named_Actual (Parent (Actual_Id));
14700 else
14701 return N;
14702 end if;
14704 else
14705 return Next_Named_Actual (Parent (Actual_Id));
14706 end if;
14707 end Next_Actual;
14709 procedure Next_Actual (Actual_Id : in out Node_Id) is
14710 begin
14711 Actual_Id := Next_Actual (Actual_Id);
14712 end Next_Actual;
14714 -----------------------
14715 -- Normalize_Actuals --
14716 -----------------------
14718 -- Chain actuals according to formals of subprogram. If there are no named
14719 -- associations, the chain is simply the list of Parameter Associations,
14720 -- since the order is the same as the declaration order. If there are named
14721 -- associations, then the First_Named_Actual field in the N_Function_Call
14722 -- or N_Procedure_Call_Statement node points to the Parameter_Association
14723 -- node for the parameter that comes first in declaration order. The
14724 -- remaining named parameters are then chained in declaration order using
14725 -- Next_Named_Actual.
14727 -- This routine also verifies that the number of actuals is compatible with
14728 -- the number and default values of formals, but performs no type checking
14729 -- (type checking is done by the caller).
14731 -- If the matching succeeds, Success is set to True and the caller proceeds
14732 -- with type-checking. If the match is unsuccessful, then Success is set to
14733 -- False, and the caller attempts a different interpretation, if there is
14734 -- one.
14736 -- If the flag Report is on, the call is not overloaded, and a failure to
14737 -- match can be reported here, rather than in the caller.
14739 procedure Normalize_Actuals
14740 (N : Node_Id;
14741 S : Entity_Id;
14742 Report : Boolean;
14743 Success : out Boolean)
14745 Actuals : constant List_Id := Parameter_Associations (N);
14746 Actual : Node_Id := Empty;
14747 Formal : Entity_Id;
14748 Last : Node_Id := Empty;
14749 First_Named : Node_Id := Empty;
14750 Found : Boolean;
14752 Formals_To_Match : Integer := 0;
14753 Actuals_To_Match : Integer := 0;
14755 procedure Chain (A : Node_Id);
14756 -- Add named actual at the proper place in the list, using the
14757 -- Next_Named_Actual link.
14759 function Reporting return Boolean;
14760 -- Determines if an error is to be reported. To report an error, we
14761 -- need Report to be True, and also we do not report errors caused
14762 -- by calls to init procs that occur within other init procs. Such
14763 -- errors must always be cascaded errors, since if all the types are
14764 -- declared correctly, the compiler will certainly build decent calls.
14766 -----------
14767 -- Chain --
14768 -----------
14770 procedure Chain (A : Node_Id) is
14771 begin
14772 if No (Last) then
14774 -- Call node points to first actual in list
14776 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
14778 else
14779 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
14780 end if;
14782 Last := A;
14783 Set_Next_Named_Actual (Last, Empty);
14784 end Chain;
14786 ---------------
14787 -- Reporting --
14788 ---------------
14790 function Reporting return Boolean is
14791 begin
14792 if not Report then
14793 return False;
14795 elsif not Within_Init_Proc then
14796 return True;
14798 elsif Is_Init_Proc (Entity (Name (N))) then
14799 return False;
14801 else
14802 return True;
14803 end if;
14804 end Reporting;
14806 -- Start of processing for Normalize_Actuals
14808 begin
14809 if Is_Access_Type (S) then
14811 -- The name in the call is a function call that returns an access
14812 -- to subprogram. The designated type has the list of formals.
14814 Formal := First_Formal (Designated_Type (S));
14815 else
14816 Formal := First_Formal (S);
14817 end if;
14819 while Present (Formal) loop
14820 Formals_To_Match := Formals_To_Match + 1;
14821 Next_Formal (Formal);
14822 end loop;
14824 -- Find if there is a named association, and verify that no positional
14825 -- associations appear after named ones.
14827 if Present (Actuals) then
14828 Actual := First (Actuals);
14829 end if;
14831 while Present (Actual)
14832 and then Nkind (Actual) /= N_Parameter_Association
14833 loop
14834 Actuals_To_Match := Actuals_To_Match + 1;
14835 Next (Actual);
14836 end loop;
14838 if No (Actual) and Actuals_To_Match = Formals_To_Match then
14840 -- Most common case: positional notation, no defaults
14842 Success := True;
14843 return;
14845 elsif Actuals_To_Match > Formals_To_Match then
14847 -- Too many actuals: will not work
14849 if Reporting then
14850 if Is_Entity_Name (Name (N)) then
14851 Error_Msg_N ("too many arguments in call to&", Name (N));
14852 else
14853 Error_Msg_N ("too many arguments in call", N);
14854 end if;
14855 end if;
14857 Success := False;
14858 return;
14859 end if;
14861 First_Named := Actual;
14863 while Present (Actual) loop
14864 if Nkind (Actual) /= N_Parameter_Association then
14865 Error_Msg_N
14866 ("positional parameters not allowed after named ones", Actual);
14867 Success := False;
14868 return;
14870 else
14871 Actuals_To_Match := Actuals_To_Match + 1;
14872 end if;
14874 Next (Actual);
14875 end loop;
14877 if Present (Actuals) then
14878 Actual := First (Actuals);
14879 end if;
14881 Formal := First_Formal (S);
14882 while Present (Formal) loop
14884 -- Match the formals in order. If the corresponding actual is
14885 -- positional, nothing to do. Else scan the list of named actuals
14886 -- to find the one with the right name.
14888 if Present (Actual)
14889 and then Nkind (Actual) /= N_Parameter_Association
14890 then
14891 Next (Actual);
14892 Actuals_To_Match := Actuals_To_Match - 1;
14893 Formals_To_Match := Formals_To_Match - 1;
14895 else
14896 -- For named parameters, search the list of actuals to find
14897 -- one that matches the next formal name.
14899 Actual := First_Named;
14900 Found := False;
14901 while Present (Actual) loop
14902 if Chars (Selector_Name (Actual)) = Chars (Formal) then
14903 Found := True;
14904 Chain (Actual);
14905 Actuals_To_Match := Actuals_To_Match - 1;
14906 Formals_To_Match := Formals_To_Match - 1;
14907 exit;
14908 end if;
14910 Next (Actual);
14911 end loop;
14913 if not Found then
14914 if Ekind (Formal) /= E_In_Parameter
14915 or else No (Default_Value (Formal))
14916 then
14917 if Reporting then
14918 if (Comes_From_Source (S)
14919 or else Sloc (S) = Standard_Location)
14920 and then Is_Overloadable (S)
14921 then
14922 if No (Actuals)
14923 and then
14924 Nkind_In (Parent (N), N_Procedure_Call_Statement,
14925 N_Function_Call,
14926 N_Parameter_Association)
14927 and then Ekind (S) /= E_Function
14928 then
14929 Set_Etype (N, Etype (S));
14931 else
14932 Error_Msg_Name_1 := Chars (S);
14933 Error_Msg_Sloc := Sloc (S);
14934 Error_Msg_NE
14935 ("missing argument for parameter & "
14936 & "in call to % declared #", N, Formal);
14937 end if;
14939 elsif Is_Overloadable (S) then
14940 Error_Msg_Name_1 := Chars (S);
14942 -- Point to type derivation that generated the
14943 -- operation.
14945 Error_Msg_Sloc := Sloc (Parent (S));
14947 Error_Msg_NE
14948 ("missing argument for parameter & "
14949 & "in call to % (inherited) #", N, Formal);
14951 else
14952 Error_Msg_NE
14953 ("missing argument for parameter &", N, Formal);
14954 end if;
14955 end if;
14957 Success := False;
14958 return;
14960 else
14961 Formals_To_Match := Formals_To_Match - 1;
14962 end if;
14963 end if;
14964 end if;
14966 Next_Formal (Formal);
14967 end loop;
14969 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
14970 Success := True;
14971 return;
14973 else
14974 if Reporting then
14976 -- Find some superfluous named actual that did not get
14977 -- attached to the list of associations.
14979 Actual := First (Actuals);
14980 while Present (Actual) loop
14981 if Nkind (Actual) = N_Parameter_Association
14982 and then Actual /= Last
14983 and then No (Next_Named_Actual (Actual))
14984 then
14985 Error_Msg_N ("unmatched actual & in call",
14986 Selector_Name (Actual));
14987 exit;
14988 end if;
14990 Next (Actual);
14991 end loop;
14992 end if;
14994 Success := False;
14995 return;
14996 end if;
14997 end Normalize_Actuals;
14999 --------------------------------
15000 -- Note_Possible_Modification --
15001 --------------------------------
15003 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
15004 Modification_Comes_From_Source : constant Boolean :=
15005 Comes_From_Source (Parent (N));
15007 Ent : Entity_Id;
15008 Exp : Node_Id;
15010 begin
15011 -- Loop to find referenced entity, if there is one
15013 Exp := N;
15014 loop
15015 Ent := Empty;
15017 if Is_Entity_Name (Exp) then
15018 Ent := Entity (Exp);
15020 -- If the entity is missing, it is an undeclared identifier,
15021 -- and there is nothing to annotate.
15023 if No (Ent) then
15024 return;
15025 end if;
15027 elsif Nkind (Exp) = N_Explicit_Dereference then
15028 declare
15029 P : constant Node_Id := Prefix (Exp);
15031 begin
15032 -- In formal verification mode, keep track of all reads and
15033 -- writes through explicit dereferences.
15035 if GNATprove_Mode then
15036 SPARK_Specific.Generate_Dereference (N, 'm');
15037 end if;
15039 if Nkind (P) = N_Selected_Component
15040 and then Present (Entry_Formal (Entity (Selector_Name (P))))
15041 then
15042 -- Case of a reference to an entry formal
15044 Ent := Entry_Formal (Entity (Selector_Name (P)));
15046 elsif Nkind (P) = N_Identifier
15047 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
15048 and then Present (Expression (Parent (Entity (P))))
15049 and then Nkind (Expression (Parent (Entity (P)))) =
15050 N_Reference
15051 then
15052 -- Case of a reference to a value on which side effects have
15053 -- been removed.
15055 Exp := Prefix (Expression (Parent (Entity (P))));
15056 goto Continue;
15058 else
15059 return;
15060 end if;
15061 end;
15063 elsif Nkind_In (Exp, N_Type_Conversion,
15064 N_Unchecked_Type_Conversion)
15065 then
15066 Exp := Expression (Exp);
15067 goto Continue;
15069 elsif Nkind_In (Exp, N_Slice,
15070 N_Indexed_Component,
15071 N_Selected_Component)
15072 then
15073 -- Special check, if the prefix is an access type, then return
15074 -- since we are modifying the thing pointed to, not the prefix.
15075 -- When we are expanding, most usually the prefix is replaced
15076 -- by an explicit dereference, and this test is not needed, but
15077 -- in some cases (notably -gnatc mode and generics) when we do
15078 -- not do full expansion, we need this special test.
15080 if Is_Access_Type (Etype (Prefix (Exp))) then
15081 return;
15083 -- Otherwise go to prefix and keep going
15085 else
15086 Exp := Prefix (Exp);
15087 goto Continue;
15088 end if;
15090 -- All other cases, not a modification
15092 else
15093 return;
15094 end if;
15096 -- Now look for entity being referenced
15098 if Present (Ent) then
15099 if Is_Object (Ent) then
15100 if Comes_From_Source (Exp)
15101 or else Modification_Comes_From_Source
15102 then
15103 -- Give warning if pragma unmodified given and we are
15104 -- sure this is a modification.
15106 if Has_Pragma_Unmodified (Ent) and then Sure then
15107 Error_Msg_NE ("??pragma Unmodified given for &!", N, Ent);
15108 end if;
15110 Set_Never_Set_In_Source (Ent, False);
15111 end if;
15113 Set_Is_True_Constant (Ent, False);
15114 Set_Current_Value (Ent, Empty);
15115 Set_Is_Known_Null (Ent, False);
15117 if not Can_Never_Be_Null (Ent) then
15118 Set_Is_Known_Non_Null (Ent, False);
15119 end if;
15121 -- Follow renaming chain
15123 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
15124 and then Present (Renamed_Object (Ent))
15125 then
15126 Exp := Renamed_Object (Ent);
15128 -- If the entity is the loop variable in an iteration over
15129 -- a container, retrieve container expression to indicate
15130 -- possible modificastion.
15132 if Present (Related_Expression (Ent))
15133 and then Nkind (Parent (Related_Expression (Ent))) =
15134 N_Iterator_Specification
15135 then
15136 Exp := Original_Node (Related_Expression (Ent));
15137 end if;
15139 goto Continue;
15141 -- The expression may be the renaming of a subcomponent of an
15142 -- array or container. The assignment to the subcomponent is
15143 -- a modification of the container.
15145 elsif Comes_From_Source (Original_Node (Exp))
15146 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
15147 N_Indexed_Component)
15148 then
15149 Exp := Prefix (Original_Node (Exp));
15150 goto Continue;
15151 end if;
15153 -- Generate a reference only if the assignment comes from
15154 -- source. This excludes, for example, calls to a dispatching
15155 -- assignment operation when the left-hand side is tagged. In
15156 -- GNATprove mode, we need those references also on generated
15157 -- code, as these are used to compute the local effects of
15158 -- subprograms.
15160 if Modification_Comes_From_Source or GNATprove_Mode then
15161 Generate_Reference (Ent, Exp, 'm');
15163 -- If the target of the assignment is the bound variable
15164 -- in an iterator, indicate that the corresponding array
15165 -- or container is also modified.
15167 if Ada_Version >= Ada_2012
15168 and then Nkind (Parent (Ent)) = N_Iterator_Specification
15169 then
15170 declare
15171 Domain : constant Node_Id := Name (Parent (Ent));
15173 begin
15174 -- TBD : in the full version of the construct, the
15175 -- domain of iteration can be given by an expression.
15177 if Is_Entity_Name (Domain) then
15178 Generate_Reference (Entity (Domain), Exp, 'm');
15179 Set_Is_True_Constant (Entity (Domain), False);
15180 Set_Never_Set_In_Source (Entity (Domain), False);
15181 end if;
15182 end;
15183 end if;
15184 end if;
15186 Check_Nested_Access (Ent);
15187 end if;
15189 Kill_Checks (Ent);
15191 -- If we are sure this is a modification from source, and we know
15192 -- this modifies a constant, then give an appropriate warning.
15194 if Overlays_Constant (Ent)
15195 and then (Modification_Comes_From_Source and Sure)
15196 then
15197 declare
15198 A : constant Node_Id := Address_Clause (Ent);
15199 begin
15200 if Present (A) then
15201 declare
15202 Exp : constant Node_Id := Expression (A);
15203 begin
15204 if Nkind (Exp) = N_Attribute_Reference
15205 and then Attribute_Name (Exp) = Name_Address
15206 and then Is_Entity_Name (Prefix (Exp))
15207 then
15208 Error_Msg_Sloc := Sloc (A);
15209 Error_Msg_NE
15210 ("constant& may be modified via address "
15211 & "clause#??", N, Entity (Prefix (Exp)));
15212 end if;
15213 end;
15214 end if;
15215 end;
15216 end if;
15218 return;
15219 end if;
15221 <<Continue>>
15222 null;
15223 end loop;
15224 end Note_Possible_Modification;
15226 -------------------------
15227 -- Object_Access_Level --
15228 -------------------------
15230 -- Returns the static accessibility level of the view denoted by Obj. Note
15231 -- that the value returned is the result of a call to Scope_Depth. Only
15232 -- scope depths associated with dynamic scopes can actually be returned.
15233 -- Since only relative levels matter for accessibility checking, the fact
15234 -- that the distance between successive levels of accessibility is not
15235 -- always one is immaterial (invariant: if level(E2) is deeper than
15236 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
15238 function Object_Access_Level (Obj : Node_Id) return Uint is
15239 function Is_Interface_Conversion (N : Node_Id) return Boolean;
15240 -- Determine whether N is a construct of the form
15241 -- Some_Type (Operand._tag'Address)
15242 -- This construct appears in the context of dispatching calls.
15244 function Reference_To (Obj : Node_Id) return Node_Id;
15245 -- An explicit dereference is created when removing side-effects from
15246 -- expressions for constraint checking purposes. In this case a local
15247 -- access type is created for it. The correct access level is that of
15248 -- the original source node. We detect this case by noting that the
15249 -- prefix of the dereference is created by an object declaration whose
15250 -- initial expression is a reference.
15252 -----------------------------
15253 -- Is_Interface_Conversion --
15254 -----------------------------
15256 function Is_Interface_Conversion (N : Node_Id) return Boolean is
15257 begin
15258 return Nkind (N) = N_Unchecked_Type_Conversion
15259 and then Nkind (Expression (N)) = N_Attribute_Reference
15260 and then Attribute_Name (Expression (N)) = Name_Address;
15261 end Is_Interface_Conversion;
15263 ------------------
15264 -- Reference_To --
15265 ------------------
15267 function Reference_To (Obj : Node_Id) return Node_Id is
15268 Pref : constant Node_Id := Prefix (Obj);
15269 begin
15270 if Is_Entity_Name (Pref)
15271 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
15272 and then Present (Expression (Parent (Entity (Pref))))
15273 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
15274 then
15275 return (Prefix (Expression (Parent (Entity (Pref)))));
15276 else
15277 return Empty;
15278 end if;
15279 end Reference_To;
15281 -- Local variables
15283 E : Entity_Id;
15285 -- Start of processing for Object_Access_Level
15287 begin
15288 if Nkind (Obj) = N_Defining_Identifier
15289 or else Is_Entity_Name (Obj)
15290 then
15291 if Nkind (Obj) = N_Defining_Identifier then
15292 E := Obj;
15293 else
15294 E := Entity (Obj);
15295 end if;
15297 if Is_Prival (E) then
15298 E := Prival_Link (E);
15299 end if;
15301 -- If E is a type then it denotes a current instance. For this case
15302 -- we add one to the normal accessibility level of the type to ensure
15303 -- that current instances are treated as always being deeper than
15304 -- than the level of any visible named access type (see 3.10.2(21)).
15306 if Is_Type (E) then
15307 return Type_Access_Level (E) + 1;
15309 elsif Present (Renamed_Object (E)) then
15310 return Object_Access_Level (Renamed_Object (E));
15312 -- Similarly, if E is a component of the current instance of a
15313 -- protected type, any instance of it is assumed to be at a deeper
15314 -- level than the type. For a protected object (whose type is an
15315 -- anonymous protected type) its components are at the same level
15316 -- as the type itself.
15318 elsif not Is_Overloadable (E)
15319 and then Ekind (Scope (E)) = E_Protected_Type
15320 and then Comes_From_Source (Scope (E))
15321 then
15322 return Type_Access_Level (Scope (E)) + 1;
15324 else
15325 -- Aliased formals take their access level from the point of call.
15326 -- This is smaller than the level of the subprogram itself.
15328 if Is_Formal (E) and then Is_Aliased (E) then
15329 return Type_Access_Level (Etype (E));
15331 else
15332 return Scope_Depth (Enclosing_Dynamic_Scope (E));
15333 end if;
15334 end if;
15336 elsif Nkind (Obj) = N_Selected_Component then
15337 if Is_Access_Type (Etype (Prefix (Obj))) then
15338 return Type_Access_Level (Etype (Prefix (Obj)));
15339 else
15340 return Object_Access_Level (Prefix (Obj));
15341 end if;
15343 elsif Nkind (Obj) = N_Indexed_Component then
15344 if Is_Access_Type (Etype (Prefix (Obj))) then
15345 return Type_Access_Level (Etype (Prefix (Obj)));
15346 else
15347 return Object_Access_Level (Prefix (Obj));
15348 end if;
15350 elsif Nkind (Obj) = N_Explicit_Dereference then
15352 -- If the prefix is a selected access discriminant then we make a
15353 -- recursive call on the prefix, which will in turn check the level
15354 -- of the prefix object of the selected discriminant.
15356 if Nkind (Prefix (Obj)) = N_Selected_Component
15357 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
15358 and then
15359 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
15360 then
15361 return Object_Access_Level (Prefix (Obj));
15363 -- Detect an interface conversion in the context of a dispatching
15364 -- call. Use the original form of the conversion to find the access
15365 -- level of the operand.
15367 elsif Is_Interface (Etype (Obj))
15368 and then Is_Interface_Conversion (Prefix (Obj))
15369 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
15370 then
15371 return Object_Access_Level (Original_Node (Obj));
15373 elsif not Comes_From_Source (Obj) then
15374 declare
15375 Ref : constant Node_Id := Reference_To (Obj);
15376 begin
15377 if Present (Ref) then
15378 return Object_Access_Level (Ref);
15379 else
15380 return Type_Access_Level (Etype (Prefix (Obj)));
15381 end if;
15382 end;
15384 else
15385 return Type_Access_Level (Etype (Prefix (Obj)));
15386 end if;
15388 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
15389 return Object_Access_Level (Expression (Obj));
15391 elsif Nkind (Obj) = N_Function_Call then
15393 -- Function results are objects, so we get either the access level of
15394 -- the function or, in the case of an indirect call, the level of the
15395 -- access-to-subprogram type. (This code is used for Ada 95, but it
15396 -- looks wrong, because it seems that we should be checking the level
15397 -- of the call itself, even for Ada 95. However, using the Ada 2005
15398 -- version of the code causes regressions in several tests that are
15399 -- compiled with -gnat95. ???)
15401 if Ada_Version < Ada_2005 then
15402 if Is_Entity_Name (Name (Obj)) then
15403 return Subprogram_Access_Level (Entity (Name (Obj)));
15404 else
15405 return Type_Access_Level (Etype (Prefix (Name (Obj))));
15406 end if;
15408 -- For Ada 2005, the level of the result object of a function call is
15409 -- defined to be the level of the call's innermost enclosing master.
15410 -- We determine that by querying the depth of the innermost enclosing
15411 -- dynamic scope.
15413 else
15414 Return_Master_Scope_Depth_Of_Call : declare
15416 function Innermost_Master_Scope_Depth
15417 (N : Node_Id) return Uint;
15418 -- Returns the scope depth of the given node's innermost
15419 -- enclosing dynamic scope (effectively the accessibility
15420 -- level of the innermost enclosing master).
15422 ----------------------------------
15423 -- Innermost_Master_Scope_Depth --
15424 ----------------------------------
15426 function Innermost_Master_Scope_Depth
15427 (N : Node_Id) return Uint
15429 Node_Par : Node_Id := Parent (N);
15431 begin
15432 -- Locate the nearest enclosing node (by traversing Parents)
15433 -- that Defining_Entity can be applied to, and return the
15434 -- depth of that entity's nearest enclosing dynamic scope.
15436 while Present (Node_Par) loop
15437 case Nkind (Node_Par) is
15438 when N_Component_Declaration |
15439 N_Entry_Declaration |
15440 N_Formal_Object_Declaration |
15441 N_Formal_Type_Declaration |
15442 N_Full_Type_Declaration |
15443 N_Incomplete_Type_Declaration |
15444 N_Loop_Parameter_Specification |
15445 N_Object_Declaration |
15446 N_Protected_Type_Declaration |
15447 N_Private_Extension_Declaration |
15448 N_Private_Type_Declaration |
15449 N_Subtype_Declaration |
15450 N_Function_Specification |
15451 N_Procedure_Specification |
15452 N_Task_Type_Declaration |
15453 N_Body_Stub |
15454 N_Generic_Instantiation |
15455 N_Proper_Body |
15456 N_Implicit_Label_Declaration |
15457 N_Package_Declaration |
15458 N_Single_Task_Declaration |
15459 N_Subprogram_Declaration |
15460 N_Generic_Declaration |
15461 N_Renaming_Declaration |
15462 N_Block_Statement |
15463 N_Formal_Subprogram_Declaration |
15464 N_Abstract_Subprogram_Declaration |
15465 N_Entry_Body |
15466 N_Exception_Declaration |
15467 N_Formal_Package_Declaration |
15468 N_Number_Declaration |
15469 N_Package_Specification |
15470 N_Parameter_Specification |
15471 N_Single_Protected_Declaration |
15472 N_Subunit =>
15474 return Scope_Depth
15475 (Nearest_Dynamic_Scope
15476 (Defining_Entity (Node_Par)));
15478 when others =>
15479 null;
15480 end case;
15482 Node_Par := Parent (Node_Par);
15483 end loop;
15485 pragma Assert (False);
15487 -- Should never reach the following return
15489 return Scope_Depth (Current_Scope) + 1;
15490 end Innermost_Master_Scope_Depth;
15492 -- Start of processing for Return_Master_Scope_Depth_Of_Call
15494 begin
15495 return Innermost_Master_Scope_Depth (Obj);
15496 end Return_Master_Scope_Depth_Of_Call;
15497 end if;
15499 -- For convenience we handle qualified expressions, even though they
15500 -- aren't technically object names.
15502 elsif Nkind (Obj) = N_Qualified_Expression then
15503 return Object_Access_Level (Expression (Obj));
15505 -- Ditto for aggregates. They have the level of the temporary that
15506 -- will hold their value.
15508 elsif Nkind (Obj) = N_Aggregate then
15509 return Object_Access_Level (Current_Scope);
15511 -- Otherwise return the scope level of Standard. (If there are cases
15512 -- that fall through to this point they will be treated as having
15513 -- global accessibility for now. ???)
15515 else
15516 return Scope_Depth (Standard_Standard);
15517 end if;
15518 end Object_Access_Level;
15520 --------------------------
15521 -- Original_Aspect_Name --
15522 --------------------------
15524 function Original_Aspect_Name (N : Node_Id) return Name_Id is
15525 Pras : Node_Id;
15526 Name : Name_Id;
15528 begin
15529 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
15530 Pras := N;
15532 if Is_Rewrite_Substitution (Pras)
15533 and then Nkind (Original_Node (Pras)) = N_Pragma
15534 then
15535 Pras := Original_Node (Pras);
15536 end if;
15538 -- Case where we came from aspect specication
15540 if Nkind (Pras) = N_Pragma and then From_Aspect_Specification (Pras) then
15541 Pras := Corresponding_Aspect (Pras);
15542 end if;
15544 -- Get name from aspect or pragma
15546 if Nkind (Pras) = N_Pragma then
15547 Name := Pragma_Name (Pras);
15548 else
15549 Name := Chars (Identifier (Pras));
15550 end if;
15552 -- Deal with 'Class
15554 if Class_Present (Pras) then
15555 case Name is
15557 -- Names that need converting to special _xxx form
15559 when Name_Pre |
15560 Name_Pre_Class =>
15561 Name := Name_uPre;
15563 when Name_Post |
15564 Name_Post_Class =>
15565 Name := Name_uPost;
15567 when Name_Invariant =>
15568 Name := Name_uInvariant;
15570 when Name_Type_Invariant |
15571 Name_Type_Invariant_Class =>
15572 Name := Name_uType_Invariant;
15574 -- Nothing to do for other cases (e.g. a Check that derived
15575 -- from Pre_Class and has the flag set). Also we do nothing
15576 -- if the name is already in special _xxx form.
15578 when others =>
15579 null;
15580 end case;
15581 end if;
15583 return Name;
15584 end Original_Aspect_Name;
15586 --------------------------------------
15587 -- Original_Corresponding_Operation --
15588 --------------------------------------
15590 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
15592 Typ : constant Entity_Id := Find_Dispatching_Type (S);
15594 begin
15595 -- If S is an inherited primitive S2 the original corresponding
15596 -- operation of S is the original corresponding operation of S2
15598 if Present (Alias (S))
15599 and then Find_Dispatching_Type (Alias (S)) /= Typ
15600 then
15601 return Original_Corresponding_Operation (Alias (S));
15603 -- If S overrides an inherited subprogram S2 the original corresponding
15604 -- operation of S is the original corresponding operation of S2
15606 elsif Present (Overridden_Operation (S)) then
15607 return Original_Corresponding_Operation (Overridden_Operation (S));
15609 -- otherwise it is S itself
15611 else
15612 return S;
15613 end if;
15614 end Original_Corresponding_Operation;
15616 ----------------------
15617 -- Policy_In_Effect --
15618 ----------------------
15620 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
15621 function Policy_In_List (List : Node_Id) return Name_Id;
15622 -- Determine the the mode of a policy in a N_Pragma list
15624 --------------------
15625 -- Policy_In_List --
15626 --------------------
15628 function Policy_In_List (List : Node_Id) return Name_Id is
15629 Arg : Node_Id;
15630 Expr : Node_Id;
15631 Prag : Node_Id;
15633 begin
15634 Prag := List;
15635 while Present (Prag) loop
15636 Arg := First (Pragma_Argument_Associations (Prag));
15637 Expr := Get_Pragma_Arg (Arg);
15639 -- The current Check_Policy pragma matches the requested policy,
15640 -- return the second argument which denotes the policy identifier.
15642 if Chars (Expr) = Policy then
15643 return Chars (Get_Pragma_Arg (Next (Arg)));
15644 end if;
15646 Prag := Next_Pragma (Prag);
15647 end loop;
15649 return No_Name;
15650 end Policy_In_List;
15652 -- Local variables
15654 Kind : Name_Id;
15656 -- Start of processing for Policy_In_Effect
15658 begin
15659 if not Is_Valid_Assertion_Kind (Policy) then
15660 raise Program_Error;
15661 end if;
15663 -- Inspect all policy pragmas that appear within scopes (if any)
15665 Kind := Policy_In_List (Check_Policy_List);
15667 -- Inspect all configuration policy pragmas (if any)
15669 if Kind = No_Name then
15670 Kind := Policy_In_List (Check_Policy_List_Config);
15671 end if;
15673 -- The context lacks policy pragmas, determine the mode based on whether
15674 -- assertions are enabled.
15676 if Kind = No_Name then
15677 if Assertions_Enabled then
15678 Kind := Name_Check;
15679 else
15680 Kind := Name_Ignore;
15681 end if;
15682 end if;
15684 return Kind;
15685 end Policy_In_Effect;
15687 ----------------------------------
15688 -- Predicate_Tests_On_Arguments --
15689 ----------------------------------
15691 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
15692 begin
15693 -- Always test predicates on indirect call
15695 if Ekind (Subp) = E_Subprogram_Type then
15696 return True;
15698 -- Do not test predicates on call to generated default Finalize, since
15699 -- we are not interested in whether something we are finalizing (and
15700 -- typically destroying) satisfies its predicates.
15702 elsif Chars (Subp) = Name_Finalize
15703 and then not Comes_From_Source (Subp)
15704 then
15705 return False;
15707 -- Do not test predicates on any internally generated routines
15709 elsif Is_Internal_Name (Chars (Subp)) then
15710 return False;
15712 -- Do not test predicates on call to Init_Proc, since if needed the
15713 -- predicate test will occur at some other point.
15715 elsif Is_Init_Proc (Subp) then
15716 return False;
15718 -- Do not test predicates on call to predicate function, since this
15719 -- would cause infinite recursion.
15721 elsif Ekind (Subp) = E_Function
15722 and then (Is_Predicate_Function (Subp)
15723 or else
15724 Is_Predicate_Function_M (Subp))
15725 then
15726 return False;
15728 -- For now, no other exceptions
15730 else
15731 return True;
15732 end if;
15733 end Predicate_Tests_On_Arguments;
15735 -----------------------
15736 -- Private_Component --
15737 -----------------------
15739 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
15740 Ancestor : constant Entity_Id := Base_Type (Type_Id);
15742 function Trace_Components
15743 (T : Entity_Id;
15744 Check : Boolean) return Entity_Id;
15745 -- Recursive function that does the work, and checks against circular
15746 -- definition for each subcomponent type.
15748 ----------------------
15749 -- Trace_Components --
15750 ----------------------
15752 function Trace_Components
15753 (T : Entity_Id;
15754 Check : Boolean) return Entity_Id
15756 Btype : constant Entity_Id := Base_Type (T);
15757 Component : Entity_Id;
15758 P : Entity_Id;
15759 Candidate : Entity_Id := Empty;
15761 begin
15762 if Check and then Btype = Ancestor then
15763 Error_Msg_N ("circular type definition", Type_Id);
15764 return Any_Type;
15765 end if;
15767 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
15768 if Present (Full_View (Btype))
15769 and then Is_Record_Type (Full_View (Btype))
15770 and then not Is_Frozen (Btype)
15771 then
15772 -- To indicate that the ancestor depends on a private type, the
15773 -- current Btype is sufficient. However, to check for circular
15774 -- definition we must recurse on the full view.
15776 Candidate := Trace_Components (Full_View (Btype), True);
15778 if Candidate = Any_Type then
15779 return Any_Type;
15780 else
15781 return Btype;
15782 end if;
15784 else
15785 return Btype;
15786 end if;
15788 elsif Is_Array_Type (Btype) then
15789 return Trace_Components (Component_Type (Btype), True);
15791 elsif Is_Record_Type (Btype) then
15792 Component := First_Entity (Btype);
15793 while Present (Component)
15794 and then Comes_From_Source (Component)
15795 loop
15796 -- Skip anonymous types generated by constrained components
15798 if not Is_Type (Component) then
15799 P := Trace_Components (Etype (Component), True);
15801 if Present (P) then
15802 if P = Any_Type then
15803 return P;
15804 else
15805 Candidate := P;
15806 end if;
15807 end if;
15808 end if;
15810 Next_Entity (Component);
15811 end loop;
15813 return Candidate;
15815 else
15816 return Empty;
15817 end if;
15818 end Trace_Components;
15820 -- Start of processing for Private_Component
15822 begin
15823 return Trace_Components (Type_Id, False);
15824 end Private_Component;
15826 ---------------------------
15827 -- Primitive_Names_Match --
15828 ---------------------------
15830 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
15832 function Non_Internal_Name (E : Entity_Id) return Name_Id;
15833 -- Given an internal name, returns the corresponding non-internal name
15835 ------------------------
15836 -- Non_Internal_Name --
15837 ------------------------
15839 function Non_Internal_Name (E : Entity_Id) return Name_Id is
15840 begin
15841 Get_Name_String (Chars (E));
15842 Name_Len := Name_Len - 1;
15843 return Name_Find;
15844 end Non_Internal_Name;
15846 -- Start of processing for Primitive_Names_Match
15848 begin
15849 pragma Assert (Present (E1) and then Present (E2));
15851 return Chars (E1) = Chars (E2)
15852 or else
15853 (not Is_Internal_Name (Chars (E1))
15854 and then Is_Internal_Name (Chars (E2))
15855 and then Non_Internal_Name (E2) = Chars (E1))
15856 or else
15857 (not Is_Internal_Name (Chars (E2))
15858 and then Is_Internal_Name (Chars (E1))
15859 and then Non_Internal_Name (E1) = Chars (E2))
15860 or else
15861 (Is_Predefined_Dispatching_Operation (E1)
15862 and then Is_Predefined_Dispatching_Operation (E2)
15863 and then Same_TSS (E1, E2))
15864 or else
15865 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
15866 end Primitive_Names_Match;
15868 -----------------------
15869 -- Process_End_Label --
15870 -----------------------
15872 procedure Process_End_Label
15873 (N : Node_Id;
15874 Typ : Character;
15875 Ent : Entity_Id)
15877 Loc : Source_Ptr;
15878 Nam : Node_Id;
15879 Scop : Entity_Id;
15881 Label_Ref : Boolean;
15882 -- Set True if reference to end label itself is required
15884 Endl : Node_Id;
15885 -- Gets set to the operator symbol or identifier that references the
15886 -- entity Ent. For the child unit case, this is the identifier from the
15887 -- designator. For other cases, this is simply Endl.
15889 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
15890 -- N is an identifier node that appears as a parent unit reference in
15891 -- the case where Ent is a child unit. This procedure generates an
15892 -- appropriate cross-reference entry. E is the corresponding entity.
15894 -------------------------
15895 -- Generate_Parent_Ref --
15896 -------------------------
15898 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
15899 begin
15900 -- If names do not match, something weird, skip reference
15902 if Chars (E) = Chars (N) then
15904 -- Generate the reference. We do NOT consider this as a reference
15905 -- for unreferenced symbol purposes.
15907 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
15909 if Style_Check then
15910 Style.Check_Identifier (N, E);
15911 end if;
15912 end if;
15913 end Generate_Parent_Ref;
15915 -- Start of processing for Process_End_Label
15917 begin
15918 -- If no node, ignore. This happens in some error situations, and
15919 -- also for some internally generated structures where no end label
15920 -- references are required in any case.
15922 if No (N) then
15923 return;
15924 end if;
15926 -- Nothing to do if no End_Label, happens for internally generated
15927 -- constructs where we don't want an end label reference anyway. Also
15928 -- nothing to do if Endl is a string literal, which means there was
15929 -- some prior error (bad operator symbol)
15931 Endl := End_Label (N);
15933 if No (Endl) or else Nkind (Endl) = N_String_Literal then
15934 return;
15935 end if;
15937 -- Reference node is not in extended main source unit
15939 if not In_Extended_Main_Source_Unit (N) then
15941 -- Generally we do not collect references except for the extended
15942 -- main source unit. The one exception is the 'e' entry for a
15943 -- package spec, where it is useful for a client to have the
15944 -- ending information to define scopes.
15946 if Typ /= 'e' then
15947 return;
15949 else
15950 Label_Ref := False;
15952 -- For this case, we can ignore any parent references, but we
15953 -- need the package name itself for the 'e' entry.
15955 if Nkind (Endl) = N_Designator then
15956 Endl := Identifier (Endl);
15957 end if;
15958 end if;
15960 -- Reference is in extended main source unit
15962 else
15963 Label_Ref := True;
15965 -- For designator, generate references for the parent entries
15967 if Nkind (Endl) = N_Designator then
15969 -- Generate references for the prefix if the END line comes from
15970 -- source (otherwise we do not need these references) We climb the
15971 -- scope stack to find the expected entities.
15973 if Comes_From_Source (Endl) then
15974 Nam := Name (Endl);
15975 Scop := Current_Scope;
15976 while Nkind (Nam) = N_Selected_Component loop
15977 Scop := Scope (Scop);
15978 exit when No (Scop);
15979 Generate_Parent_Ref (Selector_Name (Nam), Scop);
15980 Nam := Prefix (Nam);
15981 end loop;
15983 if Present (Scop) then
15984 Generate_Parent_Ref (Nam, Scope (Scop));
15985 end if;
15986 end if;
15988 Endl := Identifier (Endl);
15989 end if;
15990 end if;
15992 -- If the end label is not for the given entity, then either we have
15993 -- some previous error, or this is a generic instantiation for which
15994 -- we do not need to make a cross-reference in this case anyway. In
15995 -- either case we simply ignore the call.
15997 if Chars (Ent) /= Chars (Endl) then
15998 return;
15999 end if;
16001 -- If label was really there, then generate a normal reference and then
16002 -- adjust the location in the end label to point past the name (which
16003 -- should almost always be the semicolon).
16005 Loc := Sloc (Endl);
16007 if Comes_From_Source (Endl) then
16009 -- If a label reference is required, then do the style check and
16010 -- generate an l-type cross-reference entry for the label
16012 if Label_Ref then
16013 if Style_Check then
16014 Style.Check_Identifier (Endl, Ent);
16015 end if;
16017 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
16018 end if;
16020 -- Set the location to point past the label (normally this will
16021 -- mean the semicolon immediately following the label). This is
16022 -- done for the sake of the 'e' or 't' entry generated below.
16024 Get_Decoded_Name_String (Chars (Endl));
16025 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
16027 else
16028 -- In SPARK mode, no missing label is allowed for packages and
16029 -- subprogram bodies. Detect those cases by testing whether
16030 -- Process_End_Label was called for a body (Typ = 't') or a package.
16032 if Restriction_Check_Required (SPARK_05)
16033 and then (Typ = 't' or else Ekind (Ent) = E_Package)
16034 then
16035 Error_Msg_Node_1 := Endl;
16036 Check_SPARK_05_Restriction
16037 ("`END &` required", Endl, Force => True);
16038 end if;
16039 end if;
16041 -- Now generate the e/t reference
16043 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
16045 -- Restore Sloc, in case modified above, since we have an identifier
16046 -- and the normal Sloc should be left set in the tree.
16048 Set_Sloc (Endl, Loc);
16049 end Process_End_Label;
16051 ----------------
16052 -- Referenced --
16053 ----------------
16055 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
16056 Seen : Boolean := False;
16058 function Is_Reference (N : Node_Id) return Traverse_Result;
16059 -- Determine whether node N denotes a reference to Id. If this is the
16060 -- case, set global flag Seen to True and stop the traversal.
16062 ------------------
16063 -- Is_Reference --
16064 ------------------
16066 function Is_Reference (N : Node_Id) return Traverse_Result is
16067 begin
16068 if Is_Entity_Name (N)
16069 and then Present (Entity (N))
16070 and then Entity (N) = Id
16071 then
16072 Seen := True;
16073 return Abandon;
16074 else
16075 return OK;
16076 end if;
16077 end Is_Reference;
16079 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
16081 -- Start of processing for Referenced
16083 begin
16084 Inspect_Expression (Expr);
16085 return Seen;
16086 end Referenced;
16088 ------------------------------------
16089 -- References_Generic_Formal_Type --
16090 ------------------------------------
16092 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
16094 function Process (N : Node_Id) return Traverse_Result;
16095 -- Process one node in search for generic formal type
16097 -------------
16098 -- Process --
16099 -------------
16101 function Process (N : Node_Id) return Traverse_Result is
16102 begin
16103 if Nkind (N) in N_Has_Entity then
16104 declare
16105 E : constant Entity_Id := Entity (N);
16106 begin
16107 if Present (E) then
16108 if Is_Generic_Type (E) then
16109 return Abandon;
16110 elsif Present (Etype (E))
16111 and then Is_Generic_Type (Etype (E))
16112 then
16113 return Abandon;
16114 end if;
16115 end if;
16116 end;
16117 end if;
16119 return Atree.OK;
16120 end Process;
16122 function Traverse is new Traverse_Func (Process);
16123 -- Traverse tree to look for generic type
16125 begin
16126 if Inside_A_Generic then
16127 return Traverse (N) = Abandon;
16128 else
16129 return False;
16130 end if;
16131 end References_Generic_Formal_Type;
16133 --------------------
16134 -- Remove_Homonym --
16135 --------------------
16137 procedure Remove_Homonym (E : Entity_Id) is
16138 Prev : Entity_Id := Empty;
16139 H : Entity_Id;
16141 begin
16142 if E = Current_Entity (E) then
16143 if Present (Homonym (E)) then
16144 Set_Current_Entity (Homonym (E));
16145 else
16146 Set_Name_Entity_Id (Chars (E), Empty);
16147 end if;
16149 else
16150 H := Current_Entity (E);
16151 while Present (H) and then H /= E loop
16152 Prev := H;
16153 H := Homonym (H);
16154 end loop;
16156 -- If E is not on the homonym chain, nothing to do
16158 if Present (H) then
16159 Set_Homonym (Prev, Homonym (E));
16160 end if;
16161 end if;
16162 end Remove_Homonym;
16164 ---------------------
16165 -- Rep_To_Pos_Flag --
16166 ---------------------
16168 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
16169 begin
16170 return New_Occurrence_Of
16171 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
16172 end Rep_To_Pos_Flag;
16174 --------------------
16175 -- Require_Entity --
16176 --------------------
16178 procedure Require_Entity (N : Node_Id) is
16179 begin
16180 if Is_Entity_Name (N) and then No (Entity (N)) then
16181 if Total_Errors_Detected /= 0 then
16182 Set_Entity (N, Any_Id);
16183 else
16184 raise Program_Error;
16185 end if;
16186 end if;
16187 end Require_Entity;
16189 -------------------------------
16190 -- Requires_State_Refinement --
16191 -------------------------------
16193 function Requires_State_Refinement
16194 (Spec_Id : Entity_Id;
16195 Body_Id : Entity_Id) return Boolean
16197 function Mode_Is_Off (Prag : Node_Id) return Boolean;
16198 -- Given pragma SPARK_Mode, determine whether the mode is Off
16200 -----------------
16201 -- Mode_Is_Off --
16202 -----------------
16204 function Mode_Is_Off (Prag : Node_Id) return Boolean is
16205 Mode : Node_Id;
16207 begin
16208 -- The default SPARK mode is On
16210 if No (Prag) then
16211 return False;
16212 end if;
16214 Mode := Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
16216 -- Then the pragma lacks an argument, the default mode is On
16218 if No (Mode) then
16219 return False;
16220 else
16221 return Chars (Mode) = Name_Off;
16222 end if;
16223 end Mode_Is_Off;
16225 -- Start of processing for Requires_State_Refinement
16227 begin
16228 -- A package that does not define at least one abstract state cannot
16229 -- possibly require refinement.
16231 if No (Abstract_States (Spec_Id)) then
16232 return False;
16234 -- The package instroduces a single null state which does not merit
16235 -- refinement.
16237 elsif Has_Null_Abstract_State (Spec_Id) then
16238 return False;
16240 -- Check whether the package body is subject to pragma SPARK_Mode. If
16241 -- it is and the mode is Off, the package body is considered to be in
16242 -- regular Ada and does not require refinement.
16244 elsif Mode_Is_Off (SPARK_Pragma (Body_Id)) then
16245 return False;
16247 -- The body's SPARK_Mode may be inherited from a similar pragma that
16248 -- appears in the private declarations of the spec. The pragma we are
16249 -- interested appears as the second entry in SPARK_Pragma.
16251 elsif Present (SPARK_Pragma (Spec_Id))
16252 and then Mode_Is_Off (Next_Pragma (SPARK_Pragma (Spec_Id)))
16253 then
16254 return False;
16256 -- The spec defines at least one abstract state and the body has no way
16257 -- of circumventing the refinement.
16259 else
16260 return True;
16261 end if;
16262 end Requires_State_Refinement;
16264 ------------------------------
16265 -- Requires_Transient_Scope --
16266 ------------------------------
16268 -- A transient scope is required when variable-sized temporaries are
16269 -- allocated in the primary or secondary stack, or when finalization
16270 -- actions must be generated before the next instruction.
16272 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
16273 Typ : constant Entity_Id := Underlying_Type (Id);
16275 -- Start of processing for Requires_Transient_Scope
16277 begin
16278 -- This is a private type which is not completed yet. This can only
16279 -- happen in a default expression (of a formal parameter or of a
16280 -- record component). Do not expand transient scope in this case
16282 if No (Typ) then
16283 return False;
16285 -- Do not expand transient scope for non-existent procedure return
16287 elsif Typ = Standard_Void_Type then
16288 return False;
16290 -- Elementary types do not require a transient scope
16292 elsif Is_Elementary_Type (Typ) then
16293 return False;
16295 -- Generally, indefinite subtypes require a transient scope, since the
16296 -- back end cannot generate temporaries, since this is not a valid type
16297 -- for declaring an object. It might be possible to relax this in the
16298 -- future, e.g. by declaring the maximum possible space for the type.
16300 elsif Is_Indefinite_Subtype (Typ) then
16301 return True;
16303 -- Functions returning tagged types may dispatch on result so their
16304 -- returned value is allocated on the secondary stack. Controlled
16305 -- type temporaries need finalization.
16307 elsif Is_Tagged_Type (Typ)
16308 or else Has_Controlled_Component (Typ)
16309 then
16310 return not Is_Value_Type (Typ);
16312 -- Record type
16314 elsif Is_Record_Type (Typ) then
16315 declare
16316 Comp : Entity_Id;
16317 begin
16318 Comp := First_Entity (Typ);
16319 while Present (Comp) loop
16320 if Ekind (Comp) = E_Component
16321 and then Requires_Transient_Scope (Etype (Comp))
16322 then
16323 return True;
16324 else
16325 Next_Entity (Comp);
16326 end if;
16327 end loop;
16328 end;
16330 return False;
16332 -- String literal types never require transient scope
16334 elsif Ekind (Typ) = E_String_Literal_Subtype then
16335 return False;
16337 -- Array type. Note that we already know that this is a constrained
16338 -- array, since unconstrained arrays will fail the indefinite test.
16340 elsif Is_Array_Type (Typ) then
16342 -- If component type requires a transient scope, the array does too
16344 if Requires_Transient_Scope (Component_Type (Typ)) then
16345 return True;
16347 -- Otherwise, we only need a transient scope if the size depends on
16348 -- the value of one or more discriminants.
16350 else
16351 return Size_Depends_On_Discriminant (Typ);
16352 end if;
16354 -- All other cases do not require a transient scope
16356 else
16357 return False;
16358 end if;
16359 end Requires_Transient_Scope;
16361 --------------------------
16362 -- Reset_Analyzed_Flags --
16363 --------------------------
16365 procedure Reset_Analyzed_Flags (N : Node_Id) is
16367 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
16368 -- Function used to reset Analyzed flags in tree. Note that we do
16369 -- not reset Analyzed flags in entities, since there is no need to
16370 -- reanalyze entities, and indeed, it is wrong to do so, since it
16371 -- can result in generating auxiliary stuff more than once.
16373 --------------------
16374 -- Clear_Analyzed --
16375 --------------------
16377 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
16378 begin
16379 if not Has_Extension (N) then
16380 Set_Analyzed (N, False);
16381 end if;
16383 return OK;
16384 end Clear_Analyzed;
16386 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
16388 -- Start of processing for Reset_Analyzed_Flags
16390 begin
16391 Reset_Analyzed (N);
16392 end Reset_Analyzed_Flags;
16394 ------------------------
16395 -- Restore_SPARK_Mode --
16396 ------------------------
16398 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
16399 begin
16400 SPARK_Mode := Mode;
16401 end Restore_SPARK_Mode;
16403 --------------------------------
16404 -- Returns_Unconstrained_Type --
16405 --------------------------------
16407 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
16408 begin
16409 return Ekind (Subp) = E_Function
16410 and then not Is_Scalar_Type (Etype (Subp))
16411 and then not Is_Access_Type (Etype (Subp))
16412 and then not Is_Constrained (Etype (Subp));
16413 end Returns_Unconstrained_Type;
16415 ----------------------------
16416 -- Root_Type_Of_Full_View --
16417 ----------------------------
16419 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
16420 Rtyp : constant Entity_Id := Root_Type (T);
16422 begin
16423 -- The root type of the full view may itself be a private type. Keep
16424 -- looking for the ultimate derivation parent.
16426 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
16427 return Root_Type_Of_Full_View (Full_View (Rtyp));
16428 else
16429 return Rtyp;
16430 end if;
16431 end Root_Type_Of_Full_View;
16433 ---------------------------
16434 -- Safe_To_Capture_Value --
16435 ---------------------------
16437 function Safe_To_Capture_Value
16438 (N : Node_Id;
16439 Ent : Entity_Id;
16440 Cond : Boolean := False) return Boolean
16442 begin
16443 -- The only entities for which we track constant values are variables
16444 -- which are not renamings, constants, out parameters, and in out
16445 -- parameters, so check if we have this case.
16447 -- Note: it may seem odd to track constant values for constants, but in
16448 -- fact this routine is used for other purposes than simply capturing
16449 -- the value. In particular, the setting of Known[_Non]_Null.
16451 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
16452 or else
16453 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
16454 then
16455 null;
16457 -- For conditionals, we also allow loop parameters and all formals,
16458 -- including in parameters.
16460 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
16461 null;
16463 -- For all other cases, not just unsafe, but impossible to capture
16464 -- Current_Value, since the above are the only entities which have
16465 -- Current_Value fields.
16467 else
16468 return False;
16469 end if;
16471 -- Skip if volatile or aliased, since funny things might be going on in
16472 -- these cases which we cannot necessarily track. Also skip any variable
16473 -- for which an address clause is given, or whose address is taken. Also
16474 -- never capture value of library level variables (an attempt to do so
16475 -- can occur in the case of package elaboration code).
16477 if Treat_As_Volatile (Ent)
16478 or else Is_Aliased (Ent)
16479 or else Present (Address_Clause (Ent))
16480 or else Address_Taken (Ent)
16481 or else (Is_Library_Level_Entity (Ent)
16482 and then Ekind (Ent) = E_Variable)
16483 then
16484 return False;
16485 end if;
16487 -- OK, all above conditions are met. We also require that the scope of
16488 -- the reference be the same as the scope of the entity, not counting
16489 -- packages and blocks and loops.
16491 declare
16492 E_Scope : constant Entity_Id := Scope (Ent);
16493 R_Scope : Entity_Id;
16495 begin
16496 R_Scope := Current_Scope;
16497 while R_Scope /= Standard_Standard loop
16498 exit when R_Scope = E_Scope;
16500 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
16501 return False;
16502 else
16503 R_Scope := Scope (R_Scope);
16504 end if;
16505 end loop;
16506 end;
16508 -- We also require that the reference does not appear in a context
16509 -- where it is not sure to be executed (i.e. a conditional context
16510 -- or an exception handler). We skip this if Cond is True, since the
16511 -- capturing of values from conditional tests handles this ok.
16513 if Cond then
16514 return True;
16515 end if;
16517 declare
16518 Desc : Node_Id;
16519 P : Node_Id;
16521 begin
16522 Desc := N;
16524 -- Seems dubious that case expressions are not handled here ???
16526 P := Parent (N);
16527 while Present (P) loop
16528 if Nkind (P) = N_If_Statement
16529 or else Nkind (P) = N_Case_Statement
16530 or else (Nkind (P) in N_Short_Circuit
16531 and then Desc = Right_Opnd (P))
16532 or else (Nkind (P) = N_If_Expression
16533 and then Desc /= First (Expressions (P)))
16534 or else Nkind (P) = N_Exception_Handler
16535 or else Nkind (P) = N_Selective_Accept
16536 or else Nkind (P) = N_Conditional_Entry_Call
16537 or else Nkind (P) = N_Timed_Entry_Call
16538 or else Nkind (P) = N_Asynchronous_Select
16539 then
16540 return False;
16542 else
16543 Desc := P;
16544 P := Parent (P);
16546 -- A special Ada 2012 case: the original node may be part
16547 -- of the else_actions of a conditional expression, in which
16548 -- case it might not have been expanded yet, and appears in
16549 -- a non-syntactic list of actions. In that case it is clearly
16550 -- not safe to save a value.
16552 if No (P)
16553 and then Is_List_Member (Desc)
16554 and then No (Parent (List_Containing (Desc)))
16555 then
16556 return False;
16557 end if;
16558 end if;
16559 end loop;
16560 end;
16562 -- OK, looks safe to set value
16564 return True;
16565 end Safe_To_Capture_Value;
16567 ---------------
16568 -- Same_Name --
16569 ---------------
16571 function Same_Name (N1, N2 : Node_Id) return Boolean is
16572 K1 : constant Node_Kind := Nkind (N1);
16573 K2 : constant Node_Kind := Nkind (N2);
16575 begin
16576 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
16577 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
16578 then
16579 return Chars (N1) = Chars (N2);
16581 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
16582 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
16583 then
16584 return Same_Name (Selector_Name (N1), Selector_Name (N2))
16585 and then Same_Name (Prefix (N1), Prefix (N2));
16587 else
16588 return False;
16589 end if;
16590 end Same_Name;
16592 -----------------
16593 -- Same_Object --
16594 -----------------
16596 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
16597 N1 : constant Node_Id := Original_Node (Node1);
16598 N2 : constant Node_Id := Original_Node (Node2);
16599 -- We do the tests on original nodes, since we are most interested
16600 -- in the original source, not any expansion that got in the way.
16602 K1 : constant Node_Kind := Nkind (N1);
16603 K2 : constant Node_Kind := Nkind (N2);
16605 begin
16606 -- First case, both are entities with same entity
16608 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
16609 declare
16610 EN1 : constant Entity_Id := Entity (N1);
16611 EN2 : constant Entity_Id := Entity (N2);
16612 begin
16613 if Present (EN1) and then Present (EN2)
16614 and then (Ekind_In (EN1, E_Variable, E_Constant)
16615 or else Is_Formal (EN1))
16616 and then EN1 = EN2
16617 then
16618 return True;
16619 end if;
16620 end;
16621 end if;
16623 -- Second case, selected component with same selector, same record
16625 if K1 = N_Selected_Component
16626 and then K2 = N_Selected_Component
16627 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
16628 then
16629 return Same_Object (Prefix (N1), Prefix (N2));
16631 -- Third case, indexed component with same subscripts, same array
16633 elsif K1 = N_Indexed_Component
16634 and then K2 = N_Indexed_Component
16635 and then Same_Object (Prefix (N1), Prefix (N2))
16636 then
16637 declare
16638 E1, E2 : Node_Id;
16639 begin
16640 E1 := First (Expressions (N1));
16641 E2 := First (Expressions (N2));
16642 while Present (E1) loop
16643 if not Same_Value (E1, E2) then
16644 return False;
16645 else
16646 Next (E1);
16647 Next (E2);
16648 end if;
16649 end loop;
16651 return True;
16652 end;
16654 -- Fourth case, slice of same array with same bounds
16656 elsif K1 = N_Slice
16657 and then K2 = N_Slice
16658 and then Nkind (Discrete_Range (N1)) = N_Range
16659 and then Nkind (Discrete_Range (N2)) = N_Range
16660 and then Same_Value (Low_Bound (Discrete_Range (N1)),
16661 Low_Bound (Discrete_Range (N2)))
16662 and then Same_Value (High_Bound (Discrete_Range (N1)),
16663 High_Bound (Discrete_Range (N2)))
16664 then
16665 return Same_Name (Prefix (N1), Prefix (N2));
16667 -- All other cases, not clearly the same object
16669 else
16670 return False;
16671 end if;
16672 end Same_Object;
16674 ---------------
16675 -- Same_Type --
16676 ---------------
16678 function Same_Type (T1, T2 : Entity_Id) return Boolean is
16679 begin
16680 if T1 = T2 then
16681 return True;
16683 elsif not Is_Constrained (T1)
16684 and then not Is_Constrained (T2)
16685 and then Base_Type (T1) = Base_Type (T2)
16686 then
16687 return True;
16689 -- For now don't bother with case of identical constraints, to be
16690 -- fiddled with later on perhaps (this is only used for optimization
16691 -- purposes, so it is not critical to do a best possible job)
16693 else
16694 return False;
16695 end if;
16696 end Same_Type;
16698 ----------------
16699 -- Same_Value --
16700 ----------------
16702 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
16703 begin
16704 if Compile_Time_Known_Value (Node1)
16705 and then Compile_Time_Known_Value (Node2)
16706 and then Expr_Value (Node1) = Expr_Value (Node2)
16707 then
16708 return True;
16709 elsif Same_Object (Node1, Node2) then
16710 return True;
16711 else
16712 return False;
16713 end if;
16714 end Same_Value;
16716 -----------------------------
16717 -- Save_SPARK_Mode_And_Set --
16718 -----------------------------
16720 procedure Save_SPARK_Mode_And_Set
16721 (Context : Entity_Id;
16722 Mode : out SPARK_Mode_Type)
16724 begin
16725 -- Save the current mode in effect
16727 Mode := SPARK_Mode;
16729 -- Do not consider illegal or partially decorated constructs
16731 if Ekind (Context) = E_Void or else Error_Posted (Context) then
16732 null;
16734 elsif Present (SPARK_Pragma (Context)) then
16735 SPARK_Mode := Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Context));
16736 end if;
16737 end Save_SPARK_Mode_And_Set;
16739 -------------------------
16740 -- Scalar_Part_Present --
16741 -------------------------
16743 function Scalar_Part_Present (T : Entity_Id) return Boolean is
16744 C : Entity_Id;
16746 begin
16747 if Is_Scalar_Type (T) then
16748 return True;
16750 elsif Is_Array_Type (T) then
16751 return Scalar_Part_Present (Component_Type (T));
16753 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
16754 C := First_Component_Or_Discriminant (T);
16755 while Present (C) loop
16756 if Scalar_Part_Present (Etype (C)) then
16757 return True;
16758 else
16759 Next_Component_Or_Discriminant (C);
16760 end if;
16761 end loop;
16762 end if;
16764 return False;
16765 end Scalar_Part_Present;
16767 ------------------------
16768 -- Scope_Is_Transient --
16769 ------------------------
16771 function Scope_Is_Transient return Boolean is
16772 begin
16773 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
16774 end Scope_Is_Transient;
16776 ------------------
16777 -- Scope_Within --
16778 ------------------
16780 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
16781 Scop : Entity_Id;
16783 begin
16784 Scop := Scope1;
16785 while Scop /= Standard_Standard loop
16786 Scop := Scope (Scop);
16788 if Scop = Scope2 then
16789 return True;
16790 end if;
16791 end loop;
16793 return False;
16794 end Scope_Within;
16796 --------------------------
16797 -- Scope_Within_Or_Same --
16798 --------------------------
16800 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
16801 Scop : Entity_Id;
16803 begin
16804 Scop := Scope1;
16805 while Scop /= Standard_Standard loop
16806 if Scop = Scope2 then
16807 return True;
16808 else
16809 Scop := Scope (Scop);
16810 end if;
16811 end loop;
16813 return False;
16814 end Scope_Within_Or_Same;
16816 --------------------
16817 -- Set_Convention --
16818 --------------------
16820 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
16821 begin
16822 Basic_Set_Convention (E, Val);
16824 if Is_Type (E)
16825 and then Is_Access_Subprogram_Type (Base_Type (E))
16826 and then Has_Foreign_Convention (E)
16827 then
16828 Set_Can_Use_Internal_Rep (E, False);
16829 end if;
16831 -- If E is an object or component, and the type of E is an anonymous
16832 -- access type with no convention set, then also set the convention of
16833 -- the anonymous access type. We do not do this for anonymous protected
16834 -- types, since protected types always have the default convention.
16836 if Present (Etype (E))
16837 and then (Is_Object (E)
16838 or else Ekind (E) = E_Component
16840 -- Allow E_Void (happens for pragma Convention appearing
16841 -- in the middle of a record applying to a component)
16843 or else Ekind (E) = E_Void)
16844 then
16845 declare
16846 Typ : constant Entity_Id := Etype (E);
16848 begin
16849 if Ekind_In (Typ, E_Anonymous_Access_Type,
16850 E_Anonymous_Access_Subprogram_Type)
16851 and then not Has_Convention_Pragma (Typ)
16852 then
16853 Basic_Set_Convention (Typ, Val);
16854 Set_Has_Convention_Pragma (Typ);
16856 -- And for the access subprogram type, deal similarly with the
16857 -- designated E_Subprogram_Type if it is also internal (which
16858 -- it always is?)
16860 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
16861 declare
16862 Dtype : constant Entity_Id := Designated_Type (Typ);
16863 begin
16864 if Ekind (Dtype) = E_Subprogram_Type
16865 and then Is_Itype (Dtype)
16866 and then not Has_Convention_Pragma (Dtype)
16867 then
16868 Basic_Set_Convention (Dtype, Val);
16869 Set_Has_Convention_Pragma (Dtype);
16870 end if;
16871 end;
16872 end if;
16873 end if;
16874 end;
16875 end if;
16876 end Set_Convention;
16878 ------------------------
16879 -- Set_Current_Entity --
16880 ------------------------
16882 -- The given entity is to be set as the currently visible definition of its
16883 -- associated name (i.e. the Node_Id associated with its name). All we have
16884 -- to do is to get the name from the identifier, and then set the
16885 -- associated Node_Id to point to the given entity.
16887 procedure Set_Current_Entity (E : Entity_Id) is
16888 begin
16889 Set_Name_Entity_Id (Chars (E), E);
16890 end Set_Current_Entity;
16892 ---------------------------
16893 -- Set_Debug_Info_Needed --
16894 ---------------------------
16896 procedure Set_Debug_Info_Needed (T : Entity_Id) is
16898 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
16899 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
16900 -- Used to set debug info in a related node if not set already
16902 --------------------------------------
16903 -- Set_Debug_Info_Needed_If_Not_Set --
16904 --------------------------------------
16906 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
16907 begin
16908 if Present (E) and then not Needs_Debug_Info (E) then
16909 Set_Debug_Info_Needed (E);
16911 -- For a private type, indicate that the full view also needs
16912 -- debug information.
16914 if Is_Type (E)
16915 and then Is_Private_Type (E)
16916 and then Present (Full_View (E))
16917 then
16918 Set_Debug_Info_Needed (Full_View (E));
16919 end if;
16920 end if;
16921 end Set_Debug_Info_Needed_If_Not_Set;
16923 -- Start of processing for Set_Debug_Info_Needed
16925 begin
16926 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
16927 -- indicates that Debug_Info_Needed is never required for the entity.
16928 -- Nothing to do if entity comes from a predefined file. Library files
16929 -- are compiled without debug information, but inlined bodies of these
16930 -- routines may appear in user code, and debug information on them ends
16931 -- up complicating debugging the user code.
16933 if No (T)
16934 or else Debug_Info_Off (T)
16935 then
16936 return;
16938 elsif In_Inlined_Body
16939 and then Is_Predefined_File_Name
16940 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
16941 then
16942 Set_Needs_Debug_Info (T, False);
16943 end if;
16945 -- Set flag in entity itself. Note that we will go through the following
16946 -- circuitry even if the flag is already set on T. That's intentional,
16947 -- it makes sure that the flag will be set in subsidiary entities.
16949 Set_Needs_Debug_Info (T);
16951 -- Set flag on subsidiary entities if not set already
16953 if Is_Object (T) then
16954 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
16956 elsif Is_Type (T) then
16957 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
16959 if Is_Record_Type (T) then
16960 declare
16961 Ent : Entity_Id := First_Entity (T);
16962 begin
16963 while Present (Ent) loop
16964 Set_Debug_Info_Needed_If_Not_Set (Ent);
16965 Next_Entity (Ent);
16966 end loop;
16967 end;
16969 -- For a class wide subtype, we also need debug information
16970 -- for the equivalent type.
16972 if Ekind (T) = E_Class_Wide_Subtype then
16973 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
16974 end if;
16976 elsif Is_Array_Type (T) then
16977 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
16979 declare
16980 Indx : Node_Id := First_Index (T);
16981 begin
16982 while Present (Indx) loop
16983 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
16984 Indx := Next_Index (Indx);
16985 end loop;
16986 end;
16988 -- For a packed array type, we also need debug information for
16989 -- the type used to represent the packed array. Conversely, we
16990 -- also need it for the former if we need it for the latter.
16992 if Is_Packed (T) then
16993 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
16994 end if;
16996 if Is_Packed_Array_Impl_Type (T) then
16997 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
16998 end if;
17000 elsif Is_Access_Type (T) then
17001 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
17003 elsif Is_Private_Type (T) then
17004 Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
17006 elsif Is_Protected_Type (T) then
17007 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
17009 elsif Is_Scalar_Type (T) then
17011 -- If the subrange bounds are materialized by dedicated constant
17012 -- objects, also include them in the debug info to make sure the
17013 -- debugger can properly use them.
17015 if Present (Scalar_Range (T))
17016 and then Nkind (Scalar_Range (T)) = N_Range
17017 then
17018 declare
17019 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
17020 High_Bnd : constant Node_Id := Type_High_Bound (T);
17022 begin
17023 if Is_Entity_Name (Low_Bnd) then
17024 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
17025 end if;
17027 if Is_Entity_Name (High_Bnd) then
17028 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
17029 end if;
17030 end;
17031 end if;
17032 end if;
17033 end if;
17034 end Set_Debug_Info_Needed;
17036 ----------------------------
17037 -- Set_Entity_With_Checks --
17038 ----------------------------
17040 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
17041 Val_Actual : Entity_Id;
17042 Nod : Node_Id;
17043 Post_Node : Node_Id;
17045 begin
17046 -- Unconditionally set the entity
17048 Set_Entity (N, Val);
17050 -- The node to post on is the selector in the case of an expanded name,
17051 -- and otherwise the node itself.
17053 if Nkind (N) = N_Expanded_Name then
17054 Post_Node := Selector_Name (N);
17055 else
17056 Post_Node := N;
17057 end if;
17059 -- Check for violation of No_Fixed_IO
17061 if Restriction_Check_Required (No_Fixed_IO)
17062 and then
17063 ((RTU_Loaded (Ada_Text_IO)
17064 and then (Is_RTE (Val, RE_Decimal_IO)
17065 or else
17066 Is_RTE (Val, RE_Fixed_IO)))
17068 or else
17069 (RTU_Loaded (Ada_Wide_Text_IO)
17070 and then (Is_RTE (Val, RO_WT_Decimal_IO)
17071 or else
17072 Is_RTE (Val, RO_WT_Fixed_IO)))
17074 or else
17075 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
17076 and then (Is_RTE (Val, RO_WW_Decimal_IO)
17077 or else
17078 Is_RTE (Val, RO_WW_Fixed_IO))))
17080 -- A special extra check, don't complain about a reference from within
17081 -- the Ada.Interrupts package itself!
17083 and then not In_Same_Extended_Unit (N, Val)
17084 then
17085 Check_Restriction (No_Fixed_IO, Post_Node);
17086 end if;
17088 -- Remaining checks are only done on source nodes. Note that we test
17089 -- for violation of No_Fixed_IO even on non-source nodes, because the
17090 -- cases for checking violations of this restriction are instantiations
17091 -- where the reference in the instance has Comes_From_Source False.
17093 if not Comes_From_Source (N) then
17094 return;
17095 end if;
17097 -- Check for violation of No_Abort_Statements, which is triggered by
17098 -- call to Ada.Task_Identification.Abort_Task.
17100 if Restriction_Check_Required (No_Abort_Statements)
17101 and then (Is_RTE (Val, RE_Abort_Task))
17103 -- A special extra check, don't complain about a reference from within
17104 -- the Ada.Task_Identification package itself!
17106 and then not In_Same_Extended_Unit (N, Val)
17107 then
17108 Check_Restriction (No_Abort_Statements, Post_Node);
17109 end if;
17111 if Val = Standard_Long_Long_Integer then
17112 Check_Restriction (No_Long_Long_Integers, Post_Node);
17113 end if;
17115 -- Check for violation of No_Dynamic_Attachment
17117 if Restriction_Check_Required (No_Dynamic_Attachment)
17118 and then RTU_Loaded (Ada_Interrupts)
17119 and then (Is_RTE (Val, RE_Is_Reserved) or else
17120 Is_RTE (Val, RE_Is_Attached) or else
17121 Is_RTE (Val, RE_Current_Handler) or else
17122 Is_RTE (Val, RE_Attach_Handler) or else
17123 Is_RTE (Val, RE_Exchange_Handler) or else
17124 Is_RTE (Val, RE_Detach_Handler) or else
17125 Is_RTE (Val, RE_Reference))
17127 -- A special extra check, don't complain about a reference from within
17128 -- the Ada.Interrupts package itself!
17130 and then not In_Same_Extended_Unit (N, Val)
17131 then
17132 Check_Restriction (No_Dynamic_Attachment, Post_Node);
17133 end if;
17135 -- Check for No_Implementation_Identifiers
17137 if Restriction_Check_Required (No_Implementation_Identifiers) then
17139 -- We have an implementation defined entity if it is marked as
17140 -- implementation defined, or is defined in a package marked as
17141 -- implementation defined. However, library packages themselves
17142 -- are excluded (we don't want to flag Interfaces itself, just
17143 -- the entities within it).
17145 if (Is_Implementation_Defined (Val)
17146 or else
17147 (Present (Scope (Val))
17148 and then Is_Implementation_Defined (Scope (Val))))
17149 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
17150 and then Is_Library_Level_Entity (Val))
17151 then
17152 Check_Restriction (No_Implementation_Identifiers, Post_Node);
17153 end if;
17154 end if;
17156 -- Do the style check
17158 if Style_Check
17159 and then not Suppress_Style_Checks (Val)
17160 and then not In_Instance
17161 then
17162 if Nkind (N) = N_Identifier then
17163 Nod := N;
17164 elsif Nkind (N) = N_Expanded_Name then
17165 Nod := Selector_Name (N);
17166 else
17167 return;
17168 end if;
17170 -- A special situation arises for derived operations, where we want
17171 -- to do the check against the parent (since the Sloc of the derived
17172 -- operation points to the derived type declaration itself).
17174 Val_Actual := Val;
17175 while not Comes_From_Source (Val_Actual)
17176 and then Nkind (Val_Actual) in N_Entity
17177 and then (Ekind (Val_Actual) = E_Enumeration_Literal
17178 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
17179 and then Present (Alias (Val_Actual))
17180 loop
17181 Val_Actual := Alias (Val_Actual);
17182 end loop;
17184 -- Renaming declarations for generic actuals do not come from source,
17185 -- and have a different name from that of the entity they rename, so
17186 -- there is no style check to perform here.
17188 if Chars (Nod) = Chars (Val_Actual) then
17189 Style.Check_Identifier (Nod, Val_Actual);
17190 end if;
17191 end if;
17193 Set_Entity (N, Val);
17194 end Set_Entity_With_Checks;
17196 -------------------------
17197 -- Set_Is_Ghost_Entity --
17198 -------------------------
17200 procedure Set_Is_Ghost_Entity (Id : Entity_Id) is
17201 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
17203 begin
17204 if Policy = Name_Check then
17205 Set_Is_Checked_Ghost_Entity (Id);
17207 elsif Policy = Name_Ignore then
17208 Set_Is_Ignored_Ghost_Entity (Id);
17209 end if;
17210 end Set_Is_Ghost_Entity;
17212 ------------------------
17213 -- Set_Name_Entity_Id --
17214 ------------------------
17216 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
17217 begin
17218 Set_Name_Table_Info (Id, Int (Val));
17219 end Set_Name_Entity_Id;
17221 ---------------------
17222 -- Set_Next_Actual --
17223 ---------------------
17225 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
17226 begin
17227 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
17228 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
17229 end if;
17230 end Set_Next_Actual;
17232 ----------------------------------
17233 -- Set_Optimize_Alignment_Flags --
17234 ----------------------------------
17236 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
17237 begin
17238 if Optimize_Alignment = 'S' then
17239 Set_Optimize_Alignment_Space (E);
17240 elsif Optimize_Alignment = 'T' then
17241 Set_Optimize_Alignment_Time (E);
17242 end if;
17243 end Set_Optimize_Alignment_Flags;
17245 -----------------------
17246 -- Set_Public_Status --
17247 -----------------------
17249 procedure Set_Public_Status (Id : Entity_Id) is
17250 S : constant Entity_Id := Current_Scope;
17252 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
17253 -- Determines if E is defined within handled statement sequence or
17254 -- an if statement, returns True if so, False otherwise.
17256 ----------------------
17257 -- Within_HSS_Or_If --
17258 ----------------------
17260 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
17261 N : Node_Id;
17262 begin
17263 N := Declaration_Node (E);
17264 loop
17265 N := Parent (N);
17267 if No (N) then
17268 return False;
17270 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
17271 N_If_Statement)
17272 then
17273 return True;
17274 end if;
17275 end loop;
17276 end Within_HSS_Or_If;
17278 -- Start of processing for Set_Public_Status
17280 begin
17281 -- Everything in the scope of Standard is public
17283 if S = Standard_Standard then
17284 Set_Is_Public (Id);
17286 -- Entity is definitely not public if enclosing scope is not public
17288 elsif not Is_Public (S) then
17289 return;
17291 -- An object or function declaration that occurs in a handled sequence
17292 -- of statements or within an if statement is the declaration for a
17293 -- temporary object or local subprogram generated by the expander. It
17294 -- never needs to be made public and furthermore, making it public can
17295 -- cause back end problems.
17297 elsif Nkind_In (Parent (Id), N_Object_Declaration,
17298 N_Function_Specification)
17299 and then Within_HSS_Or_If (Id)
17300 then
17301 return;
17303 -- Entities in public packages or records are public
17305 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
17306 Set_Is_Public (Id);
17308 -- The bounds of an entry family declaration can generate object
17309 -- declarations that are visible to the back-end, e.g. in the
17310 -- the declaration of a composite type that contains tasks.
17312 elsif Is_Concurrent_Type (S)
17313 and then not Has_Completion (S)
17314 and then Nkind (Parent (Id)) = N_Object_Declaration
17315 then
17316 Set_Is_Public (Id);
17317 end if;
17318 end Set_Public_Status;
17320 -----------------------------
17321 -- Set_Referenced_Modified --
17322 -----------------------------
17324 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
17325 Pref : Node_Id;
17327 begin
17328 -- Deal with indexed or selected component where prefix is modified
17330 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
17331 Pref := Prefix (N);
17333 -- If prefix is access type, then it is the designated object that is
17334 -- being modified, which means we have no entity to set the flag on.
17336 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
17337 return;
17339 -- Otherwise chase the prefix
17341 else
17342 Set_Referenced_Modified (Pref, Out_Param);
17343 end if;
17345 -- Otherwise see if we have an entity name (only other case to process)
17347 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
17348 Set_Referenced_As_LHS (Entity (N), not Out_Param);
17349 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
17350 end if;
17351 end Set_Referenced_Modified;
17353 ----------------------------
17354 -- Set_Scope_Is_Transient --
17355 ----------------------------
17357 procedure Set_Scope_Is_Transient (V : Boolean := True) is
17358 begin
17359 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
17360 end Set_Scope_Is_Transient;
17362 -------------------
17363 -- Set_Size_Info --
17364 -------------------
17366 procedure Set_Size_Info (T1, T2 : Entity_Id) is
17367 begin
17368 -- We copy Esize, but not RM_Size, since in general RM_Size is
17369 -- subtype specific and does not get inherited by all subtypes.
17371 Set_Esize (T1, Esize (T2));
17372 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
17374 if Is_Discrete_Or_Fixed_Point_Type (T1)
17375 and then
17376 Is_Discrete_Or_Fixed_Point_Type (T2)
17377 then
17378 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
17379 end if;
17381 Set_Alignment (T1, Alignment (T2));
17382 end Set_Size_Info;
17384 --------------------
17385 -- Static_Boolean --
17386 --------------------
17388 function Static_Boolean (N : Node_Id) return Uint is
17389 begin
17390 Analyze_And_Resolve (N, Standard_Boolean);
17392 if N = Error
17393 or else Error_Posted (N)
17394 or else Etype (N) = Any_Type
17395 then
17396 return No_Uint;
17397 end if;
17399 if Is_OK_Static_Expression (N) then
17400 if not Raises_Constraint_Error (N) then
17401 return Expr_Value (N);
17402 else
17403 return No_Uint;
17404 end if;
17406 elsif Etype (N) = Any_Type then
17407 return No_Uint;
17409 else
17410 Flag_Non_Static_Expr
17411 ("static boolean expression required here", N);
17412 return No_Uint;
17413 end if;
17414 end Static_Boolean;
17416 --------------------
17417 -- Static_Integer --
17418 --------------------
17420 function Static_Integer (N : Node_Id) return Uint is
17421 begin
17422 Analyze_And_Resolve (N, Any_Integer);
17424 if N = Error
17425 or else Error_Posted (N)
17426 or else Etype (N) = Any_Type
17427 then
17428 return No_Uint;
17429 end if;
17431 if Is_OK_Static_Expression (N) then
17432 if not Raises_Constraint_Error (N) then
17433 return Expr_Value (N);
17434 else
17435 return No_Uint;
17436 end if;
17438 elsif Etype (N) = Any_Type then
17439 return No_Uint;
17441 else
17442 Flag_Non_Static_Expr
17443 ("static integer expression required here", N);
17444 return No_Uint;
17445 end if;
17446 end Static_Integer;
17448 --------------------------
17449 -- Statically_Different --
17450 --------------------------
17452 function Statically_Different (E1, E2 : Node_Id) return Boolean is
17453 R1 : constant Node_Id := Get_Referenced_Object (E1);
17454 R2 : constant Node_Id := Get_Referenced_Object (E2);
17455 begin
17456 return Is_Entity_Name (R1)
17457 and then Is_Entity_Name (R2)
17458 and then Entity (R1) /= Entity (R2)
17459 and then not Is_Formal (Entity (R1))
17460 and then not Is_Formal (Entity (R2));
17461 end Statically_Different;
17463 --------------------------------------
17464 -- Subject_To_Loop_Entry_Attributes --
17465 --------------------------------------
17467 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
17468 Stmt : Node_Id;
17470 begin
17471 Stmt := N;
17473 -- The expansion mechanism transform a loop subject to at least one
17474 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
17475 -- the conditional part.
17477 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
17478 and then Nkind (Original_Node (N)) = N_Loop_Statement
17479 then
17480 Stmt := Original_Node (N);
17481 end if;
17483 return
17484 Nkind (Stmt) = N_Loop_Statement
17485 and then Present (Identifier (Stmt))
17486 and then Present (Entity (Identifier (Stmt)))
17487 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
17488 end Subject_To_Loop_Entry_Attributes;
17490 -----------------------------
17491 -- Subprogram_Access_Level --
17492 -----------------------------
17494 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
17495 begin
17496 if Present (Alias (Subp)) then
17497 return Subprogram_Access_Level (Alias (Subp));
17498 else
17499 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
17500 end if;
17501 end Subprogram_Access_Level;
17503 -------------------------------
17504 -- Support_Atomic_Primitives --
17505 -------------------------------
17507 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
17508 Size : Int;
17510 begin
17511 -- Verify the alignment of Typ is known
17513 if not Known_Alignment (Typ) then
17514 return False;
17515 end if;
17517 if Known_Static_Esize (Typ) then
17518 Size := UI_To_Int (Esize (Typ));
17520 -- If the Esize (Object_Size) is unknown at compile time, look at the
17521 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
17523 elsif Known_Static_RM_Size (Typ) then
17524 Size := UI_To_Int (RM_Size (Typ));
17526 -- Otherwise, the size is considered to be unknown.
17528 else
17529 return False;
17530 end if;
17532 -- Check that the size of the component is 8, 16, 32 or 64 bits and that
17533 -- Typ is properly aligned.
17535 case Size is
17536 when 8 | 16 | 32 | 64 =>
17537 return Size = UI_To_Int (Alignment (Typ)) * 8;
17538 when others =>
17539 return False;
17540 end case;
17541 end Support_Atomic_Primitives;
17543 -----------------
17544 -- Trace_Scope --
17545 -----------------
17547 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
17548 begin
17549 if Debug_Flag_W then
17550 for J in 0 .. Scope_Stack.Last loop
17551 Write_Str (" ");
17552 end loop;
17554 Write_Str (Msg);
17555 Write_Name (Chars (E));
17556 Write_Str (" from ");
17557 Write_Location (Sloc (N));
17558 Write_Eol;
17559 end if;
17560 end Trace_Scope;
17562 -----------------------
17563 -- Transfer_Entities --
17564 -----------------------
17566 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
17567 Ent : Entity_Id := First_Entity (From);
17569 begin
17570 if No (Ent) then
17571 return;
17572 end if;
17574 if (Last_Entity (To)) = Empty then
17575 Set_First_Entity (To, Ent);
17576 else
17577 Set_Next_Entity (Last_Entity (To), Ent);
17578 end if;
17580 Set_Last_Entity (To, Last_Entity (From));
17582 while Present (Ent) loop
17583 Set_Scope (Ent, To);
17585 if not Is_Public (Ent) then
17586 Set_Public_Status (Ent);
17588 if Is_Public (Ent) and then Ekind (Ent) = E_Record_Subtype then
17590 -- The components of the propagated Itype must also be public
17592 declare
17593 Comp : Entity_Id;
17594 begin
17595 Comp := First_Entity (Ent);
17596 while Present (Comp) loop
17597 Set_Is_Public (Comp);
17598 Next_Entity (Comp);
17599 end loop;
17600 end;
17601 end if;
17602 end if;
17604 Next_Entity (Ent);
17605 end loop;
17607 Set_First_Entity (From, Empty);
17608 Set_Last_Entity (From, Empty);
17609 end Transfer_Entities;
17611 -----------------------
17612 -- Type_Access_Level --
17613 -----------------------
17615 function Type_Access_Level (Typ : Entity_Id) return Uint is
17616 Btyp : Entity_Id;
17618 begin
17619 Btyp := Base_Type (Typ);
17621 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
17622 -- simply use the level where the type is declared. This is true for
17623 -- stand-alone object declarations, and for anonymous access types
17624 -- associated with components the level is the same as that of the
17625 -- enclosing composite type. However, special treatment is needed for
17626 -- the cases of access parameters, return objects of an anonymous access
17627 -- type, and, in Ada 95, access discriminants of limited types.
17629 if Is_Access_Type (Btyp) then
17630 if Ekind (Btyp) = E_Anonymous_Access_Type then
17632 -- If the type is a nonlocal anonymous access type (such as for
17633 -- an access parameter) we treat it as being declared at the
17634 -- library level to ensure that names such as X.all'access don't
17635 -- fail static accessibility checks.
17637 if not Is_Local_Anonymous_Access (Typ) then
17638 return Scope_Depth (Standard_Standard);
17640 -- If this is a return object, the accessibility level is that of
17641 -- the result subtype of the enclosing function. The test here is
17642 -- little complicated, because we have to account for extended
17643 -- return statements that have been rewritten as blocks, in which
17644 -- case we have to find and the Is_Return_Object attribute of the
17645 -- itype's associated object. It would be nice to find a way to
17646 -- simplify this test, but it doesn't seem worthwhile to add a new
17647 -- flag just for purposes of this test. ???
17649 elsif Ekind (Scope (Btyp)) = E_Return_Statement
17650 or else
17651 (Is_Itype (Btyp)
17652 and then Nkind (Associated_Node_For_Itype (Btyp)) =
17653 N_Object_Declaration
17654 and then Is_Return_Object
17655 (Defining_Identifier
17656 (Associated_Node_For_Itype (Btyp))))
17657 then
17658 declare
17659 Scop : Entity_Id;
17661 begin
17662 Scop := Scope (Scope (Btyp));
17663 while Present (Scop) loop
17664 exit when Ekind (Scop) = E_Function;
17665 Scop := Scope (Scop);
17666 end loop;
17668 -- Treat the return object's type as having the level of the
17669 -- function's result subtype (as per RM05-6.5(5.3/2)).
17671 return Type_Access_Level (Etype (Scop));
17672 end;
17673 end if;
17674 end if;
17676 Btyp := Root_Type (Btyp);
17678 -- The accessibility level of anonymous access types associated with
17679 -- discriminants is that of the current instance of the type, and
17680 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
17682 -- AI-402: access discriminants have accessibility based on the
17683 -- object rather than the type in Ada 2005, so the above paragraph
17684 -- doesn't apply.
17686 -- ??? Needs completion with rules from AI-416
17688 if Ada_Version <= Ada_95
17689 and then Ekind (Typ) = E_Anonymous_Access_Type
17690 and then Present (Associated_Node_For_Itype (Typ))
17691 and then Nkind (Associated_Node_For_Itype (Typ)) =
17692 N_Discriminant_Specification
17693 then
17694 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
17695 end if;
17696 end if;
17698 -- Return library level for a generic formal type. This is done because
17699 -- RM(10.3.2) says that "The statically deeper relationship does not
17700 -- apply to ... a descendant of a generic formal type". Rather than
17701 -- checking at each point where a static accessibility check is
17702 -- performed to see if we are dealing with a formal type, this rule is
17703 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
17704 -- return extreme values for a formal type; Deepest_Type_Access_Level
17705 -- returns Int'Last. By calling the appropriate function from among the
17706 -- two, we ensure that the static accessibility check will pass if we
17707 -- happen to run into a formal type. More specifically, we should call
17708 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
17709 -- call occurs as part of a static accessibility check and the error
17710 -- case is the case where the type's level is too shallow (as opposed
17711 -- to too deep).
17713 if Is_Generic_Type (Root_Type (Btyp)) then
17714 return Scope_Depth (Standard_Standard);
17715 end if;
17717 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
17718 end Type_Access_Level;
17720 ------------------------------------
17721 -- Type_Without_Stream_Operation --
17722 ------------------------------------
17724 function Type_Without_Stream_Operation
17725 (T : Entity_Id;
17726 Op : TSS_Name_Type := TSS_Null) return Entity_Id
17728 BT : constant Entity_Id := Base_Type (T);
17729 Op_Missing : Boolean;
17731 begin
17732 if not Restriction_Active (No_Default_Stream_Attributes) then
17733 return Empty;
17734 end if;
17736 if Is_Elementary_Type (T) then
17737 if Op = TSS_Null then
17738 Op_Missing :=
17739 No (TSS (BT, TSS_Stream_Read))
17740 or else No (TSS (BT, TSS_Stream_Write));
17742 else
17743 Op_Missing := No (TSS (BT, Op));
17744 end if;
17746 if Op_Missing then
17747 return T;
17748 else
17749 return Empty;
17750 end if;
17752 elsif Is_Array_Type (T) then
17753 return Type_Without_Stream_Operation (Component_Type (T), Op);
17755 elsif Is_Record_Type (T) then
17756 declare
17757 Comp : Entity_Id;
17758 C_Typ : Entity_Id;
17760 begin
17761 Comp := First_Component (T);
17762 while Present (Comp) loop
17763 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
17765 if Present (C_Typ) then
17766 return C_Typ;
17767 end if;
17769 Next_Component (Comp);
17770 end loop;
17772 return Empty;
17773 end;
17775 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
17776 return Type_Without_Stream_Operation (Full_View (T), Op);
17777 else
17778 return Empty;
17779 end if;
17780 end Type_Without_Stream_Operation;
17782 ----------------------------
17783 -- Unique_Defining_Entity --
17784 ----------------------------
17786 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
17787 begin
17788 return Unique_Entity (Defining_Entity (N));
17789 end Unique_Defining_Entity;
17791 -------------------
17792 -- Unique_Entity --
17793 -------------------
17795 function Unique_Entity (E : Entity_Id) return Entity_Id is
17796 U : Entity_Id := E;
17797 P : Node_Id;
17799 begin
17800 case Ekind (E) is
17801 when E_Constant =>
17802 if Present (Full_View (E)) then
17803 U := Full_View (E);
17804 end if;
17806 when Type_Kind =>
17807 if Present (Full_View (E)) then
17808 U := Full_View (E);
17809 end if;
17811 when E_Package_Body =>
17812 P := Parent (E);
17814 if Nkind (P) = N_Defining_Program_Unit_Name then
17815 P := Parent (P);
17816 end if;
17818 U := Corresponding_Spec (P);
17820 when E_Subprogram_Body =>
17821 P := Parent (E);
17823 if Nkind (P) = N_Defining_Program_Unit_Name then
17824 P := Parent (P);
17825 end if;
17827 P := Parent (P);
17829 if Nkind (P) = N_Subprogram_Body_Stub then
17830 if Present (Library_Unit (P)) then
17832 -- Get to the function or procedure (generic) entity through
17833 -- the body entity.
17835 U :=
17836 Unique_Entity (Defining_Entity (Get_Body_From_Stub (P)));
17837 end if;
17838 else
17839 U := Corresponding_Spec (P);
17840 end if;
17842 when Formal_Kind =>
17843 if Present (Spec_Entity (E)) then
17844 U := Spec_Entity (E);
17845 end if;
17847 when others =>
17848 null;
17849 end case;
17851 return U;
17852 end Unique_Entity;
17854 -----------------
17855 -- Unique_Name --
17856 -----------------
17858 function Unique_Name (E : Entity_Id) return String is
17860 -- Names of E_Subprogram_Body or E_Package_Body entities are not
17861 -- reliable, as they may not include the overloading suffix. Instead,
17862 -- when looking for the name of E or one of its enclosing scope, we get
17863 -- the name of the corresponding Unique_Entity.
17865 function Get_Scoped_Name (E : Entity_Id) return String;
17866 -- Return the name of E prefixed by all the names of the scopes to which
17867 -- E belongs, except for Standard.
17869 ---------------------
17870 -- Get_Scoped_Name --
17871 ---------------------
17873 function Get_Scoped_Name (E : Entity_Id) return String is
17874 Name : constant String := Get_Name_String (Chars (E));
17875 begin
17876 if Has_Fully_Qualified_Name (E)
17877 or else Scope (E) = Standard_Standard
17878 then
17879 return Name;
17880 else
17881 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
17882 end if;
17883 end Get_Scoped_Name;
17885 -- Start of processing for Unique_Name
17887 begin
17888 if E = Standard_Standard then
17889 return Get_Name_String (Name_Standard);
17891 elsif Scope (E) = Standard_Standard
17892 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
17893 then
17894 return Get_Name_String (Name_Standard) & "__" &
17895 Get_Name_String (Chars (E));
17897 elsif Ekind (E) = E_Enumeration_Literal then
17898 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
17900 else
17901 return Get_Scoped_Name (Unique_Entity (E));
17902 end if;
17903 end Unique_Name;
17905 ---------------------
17906 -- Unit_Is_Visible --
17907 ---------------------
17909 function Unit_Is_Visible (U : Entity_Id) return Boolean is
17910 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
17911 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
17913 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
17914 -- For a child unit, check whether unit appears in a with_clause
17915 -- of a parent.
17917 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
17918 -- Scan the context clause of one compilation unit looking for a
17919 -- with_clause for the unit in question.
17921 ----------------------------
17922 -- Unit_In_Parent_Context --
17923 ----------------------------
17925 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
17926 begin
17927 if Unit_In_Context (Par_Unit) then
17928 return True;
17930 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
17931 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
17933 else
17934 return False;
17935 end if;
17936 end Unit_In_Parent_Context;
17938 ---------------------
17939 -- Unit_In_Context --
17940 ---------------------
17942 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
17943 Clause : Node_Id;
17945 begin
17946 Clause := First (Context_Items (Comp_Unit));
17947 while Present (Clause) loop
17948 if Nkind (Clause) = N_With_Clause then
17949 if Library_Unit (Clause) = U then
17950 return True;
17952 -- The with_clause may denote a renaming of the unit we are
17953 -- looking for, eg. Text_IO which renames Ada.Text_IO.
17955 elsif
17956 Renamed_Entity (Entity (Name (Clause))) =
17957 Defining_Entity (Unit (U))
17958 then
17959 return True;
17960 end if;
17961 end if;
17963 Next (Clause);
17964 end loop;
17966 return False;
17967 end Unit_In_Context;
17969 -- Start of processing for Unit_Is_Visible
17971 begin
17972 -- The currrent unit is directly visible
17974 if Curr = U then
17975 return True;
17977 elsif Unit_In_Context (Curr) then
17978 return True;
17980 -- If the current unit is a body, check the context of the spec
17982 elsif Nkind (Unit (Curr)) = N_Package_Body
17983 or else
17984 (Nkind (Unit (Curr)) = N_Subprogram_Body
17985 and then not Acts_As_Spec (Unit (Curr)))
17986 then
17987 if Unit_In_Context (Library_Unit (Curr)) then
17988 return True;
17989 end if;
17990 end if;
17992 -- If the spec is a child unit, examine the parents
17994 if Is_Child_Unit (Curr_Entity) then
17995 if Nkind (Unit (Curr)) in N_Unit_Body then
17996 return
17997 Unit_In_Parent_Context
17998 (Parent_Spec (Unit (Library_Unit (Curr))));
17999 else
18000 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
18001 end if;
18003 else
18004 return False;
18005 end if;
18006 end Unit_Is_Visible;
18008 ------------------------------
18009 -- Universal_Interpretation --
18010 ------------------------------
18012 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
18013 Index : Interp_Index;
18014 It : Interp;
18016 begin
18017 -- The argument may be a formal parameter of an operator or subprogram
18018 -- with multiple interpretations, or else an expression for an actual.
18020 if Nkind (Opnd) = N_Defining_Identifier
18021 or else not Is_Overloaded (Opnd)
18022 then
18023 if Etype (Opnd) = Universal_Integer
18024 or else Etype (Opnd) = Universal_Real
18025 then
18026 return Etype (Opnd);
18027 else
18028 return Empty;
18029 end if;
18031 else
18032 Get_First_Interp (Opnd, Index, It);
18033 while Present (It.Typ) loop
18034 if It.Typ = Universal_Integer
18035 or else It.Typ = Universal_Real
18036 then
18037 return It.Typ;
18038 end if;
18040 Get_Next_Interp (Index, It);
18041 end loop;
18043 return Empty;
18044 end if;
18045 end Universal_Interpretation;
18047 ---------------
18048 -- Unqualify --
18049 ---------------
18051 function Unqualify (Expr : Node_Id) return Node_Id is
18052 begin
18053 -- Recurse to handle unlikely case of multiple levels of qualification
18055 if Nkind (Expr) = N_Qualified_Expression then
18056 return Unqualify (Expression (Expr));
18058 -- Normal case, not a qualified expression
18060 else
18061 return Expr;
18062 end if;
18063 end Unqualify;
18065 -----------------------
18066 -- Visible_Ancestors --
18067 -----------------------
18069 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
18070 List_1 : Elist_Id;
18071 List_2 : Elist_Id;
18072 Elmt : Elmt_Id;
18074 begin
18075 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
18077 -- Collect all the parents and progenitors of Typ. If the full-view of
18078 -- private parents and progenitors is available then it is used to
18079 -- generate the list of visible ancestors; otherwise their partial
18080 -- view is added to the resulting list.
18082 Collect_Parents
18083 (T => Typ,
18084 List => List_1,
18085 Use_Full_View => True);
18087 Collect_Interfaces
18088 (T => Typ,
18089 Ifaces_List => List_2,
18090 Exclude_Parents => True,
18091 Use_Full_View => True);
18093 -- Join the two lists. Avoid duplications because an interface may
18094 -- simultaneously be parent and progenitor of a type.
18096 Elmt := First_Elmt (List_2);
18097 while Present (Elmt) loop
18098 Append_Unique_Elmt (Node (Elmt), List_1);
18099 Next_Elmt (Elmt);
18100 end loop;
18102 return List_1;
18103 end Visible_Ancestors;
18105 ------------------------
18106 -- Within_Ghost_Scope --
18107 ------------------------
18109 function Within_Ghost_Scope
18110 (Id : Entity_Id := Current_Scope) return Boolean
18112 S : Entity_Id;
18114 begin
18115 -- Climb the scope stack looking for a Ghost scope
18117 S := Id;
18118 while Present (S) and then S /= Standard_Standard loop
18119 if Is_Ghost_Entity (S) then
18120 return True;
18121 end if;
18123 S := Scope (S);
18124 end loop;
18126 return False;
18127 end Within_Ghost_Scope;
18129 ----------------------
18130 -- Within_Init_Proc --
18131 ----------------------
18133 function Within_Init_Proc return Boolean is
18134 S : Entity_Id;
18136 begin
18137 S := Current_Scope;
18138 while not Is_Overloadable (S) loop
18139 if S = Standard_Standard then
18140 return False;
18141 else
18142 S := Scope (S);
18143 end if;
18144 end loop;
18146 return Is_Init_Proc (S);
18147 end Within_Init_Proc;
18149 ------------------
18150 -- Within_Scope --
18151 ------------------
18153 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
18154 SE : Entity_Id;
18155 begin
18156 SE := Scope (E);
18157 loop
18158 if SE = S then
18159 return True;
18160 elsif SE = Standard_Standard then
18161 return False;
18162 else
18163 SE := Scope (SE);
18164 end if;
18165 end loop;
18166 end Within_Scope;
18168 ----------------
18169 -- Wrong_Type --
18170 ----------------
18172 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
18173 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
18174 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
18176 Matching_Field : Entity_Id;
18177 -- Entity to give a more precise suggestion on how to write a one-
18178 -- element positional aggregate.
18180 function Has_One_Matching_Field return Boolean;
18181 -- Determines if Expec_Type is a record type with a single component or
18182 -- discriminant whose type matches the found type or is one dimensional
18183 -- array whose component type matches the found type. In the case of
18184 -- one discriminant, we ignore the variant parts. That's not accurate,
18185 -- but good enough for the warning.
18187 ----------------------------
18188 -- Has_One_Matching_Field --
18189 ----------------------------
18191 function Has_One_Matching_Field return Boolean is
18192 E : Entity_Id;
18194 begin
18195 Matching_Field := Empty;
18197 if Is_Array_Type (Expec_Type)
18198 and then Number_Dimensions (Expec_Type) = 1
18199 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
18200 then
18201 -- Use type name if available. This excludes multidimensional
18202 -- arrays and anonymous arrays.
18204 if Comes_From_Source (Expec_Type) then
18205 Matching_Field := Expec_Type;
18207 -- For an assignment, use name of target
18209 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
18210 and then Is_Entity_Name (Name (Parent (Expr)))
18211 then
18212 Matching_Field := Entity (Name (Parent (Expr)));
18213 end if;
18215 return True;
18217 elsif not Is_Record_Type (Expec_Type) then
18218 return False;
18220 else
18221 E := First_Entity (Expec_Type);
18222 loop
18223 if No (E) then
18224 return False;
18226 elsif not Ekind_In (E, E_Discriminant, E_Component)
18227 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
18228 then
18229 Next_Entity (E);
18231 else
18232 exit;
18233 end if;
18234 end loop;
18236 if not Covers (Etype (E), Found_Type) then
18237 return False;
18239 elsif Present (Next_Entity (E))
18240 and then (Ekind (E) = E_Component
18241 or else Ekind (Next_Entity (E)) = E_Discriminant)
18242 then
18243 return False;
18245 else
18246 Matching_Field := E;
18247 return True;
18248 end if;
18249 end if;
18250 end Has_One_Matching_Field;
18252 -- Start of processing for Wrong_Type
18254 begin
18255 -- Don't output message if either type is Any_Type, or if a message
18256 -- has already been posted for this node. We need to do the latter
18257 -- check explicitly (it is ordinarily done in Errout), because we
18258 -- are using ! to force the output of the error messages.
18260 if Expec_Type = Any_Type
18261 or else Found_Type = Any_Type
18262 or else Error_Posted (Expr)
18263 then
18264 return;
18266 -- If one of the types is a Taft-Amendment type and the other it its
18267 -- completion, it must be an illegal use of a TAT in the spec, for
18268 -- which an error was already emitted. Avoid cascaded errors.
18270 elsif Is_Incomplete_Type (Expec_Type)
18271 and then Has_Completion_In_Body (Expec_Type)
18272 and then Full_View (Expec_Type) = Etype (Expr)
18273 then
18274 return;
18276 elsif Is_Incomplete_Type (Etype (Expr))
18277 and then Has_Completion_In_Body (Etype (Expr))
18278 and then Full_View (Etype (Expr)) = Expec_Type
18279 then
18280 return;
18282 -- In an instance, there is an ongoing problem with completion of
18283 -- type derived from private types. Their structure is what Gigi
18284 -- expects, but the Etype is the parent type rather than the
18285 -- derived private type itself. Do not flag error in this case. The
18286 -- private completion is an entity without a parent, like an Itype.
18287 -- Similarly, full and partial views may be incorrect in the instance.
18288 -- There is no simple way to insure that it is consistent ???
18290 -- A similar view discrepancy can happen in an inlined body, for the
18291 -- same reason: inserted body may be outside of the original package
18292 -- and only partial views are visible at the point of insertion.
18294 elsif In_Instance or else In_Inlined_Body then
18295 if Etype (Etype (Expr)) = Etype (Expected_Type)
18296 and then
18297 (Has_Private_Declaration (Expected_Type)
18298 or else Has_Private_Declaration (Etype (Expr)))
18299 and then No (Parent (Expected_Type))
18300 then
18301 return;
18303 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
18304 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
18305 then
18306 return;
18308 elsif Is_Private_Type (Expected_Type)
18309 and then Present (Full_View (Expected_Type))
18310 and then Covers (Full_View (Expected_Type), Etype (Expr))
18311 then
18312 return;
18313 end if;
18314 end if;
18316 -- An interesting special check. If the expression is parenthesized
18317 -- and its type corresponds to the type of the sole component of the
18318 -- expected record type, or to the component type of the expected one
18319 -- dimensional array type, then assume we have a bad aggregate attempt.
18321 if Nkind (Expr) in N_Subexpr
18322 and then Paren_Count (Expr) /= 0
18323 and then Has_One_Matching_Field
18324 then
18325 Error_Msg_N ("positional aggregate cannot have one component", Expr);
18326 if Present (Matching_Field) then
18327 if Is_Array_Type (Expec_Type) then
18328 Error_Msg_NE
18329 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
18331 else
18332 Error_Msg_NE
18333 ("\write instead `& ='> ...`", Expr, Matching_Field);
18334 end if;
18335 end if;
18337 -- Another special check, if we are looking for a pool-specific access
18338 -- type and we found an E_Access_Attribute_Type, then we have the case
18339 -- of an Access attribute being used in a context which needs a pool-
18340 -- specific type, which is never allowed. The one extra check we make
18341 -- is that the expected designated type covers the Found_Type.
18343 elsif Is_Access_Type (Expec_Type)
18344 and then Ekind (Found_Type) = E_Access_Attribute_Type
18345 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
18346 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
18347 and then Covers
18348 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
18349 then
18350 Error_Msg_N -- CODEFIX
18351 ("result must be general access type!", Expr);
18352 Error_Msg_NE -- CODEFIX
18353 ("add ALL to }!", Expr, Expec_Type);
18355 -- Another special check, if the expected type is an integer type,
18356 -- but the expression is of type System.Address, and the parent is
18357 -- an addition or subtraction operation whose left operand is the
18358 -- expression in question and whose right operand is of an integral
18359 -- type, then this is an attempt at address arithmetic, so give
18360 -- appropriate message.
18362 elsif Is_Integer_Type (Expec_Type)
18363 and then Is_RTE (Found_Type, RE_Address)
18364 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
18365 and then Expr = Left_Opnd (Parent (Expr))
18366 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
18367 then
18368 Error_Msg_N
18369 ("address arithmetic not predefined in package System",
18370 Parent (Expr));
18371 Error_Msg_N
18372 ("\possible missing with/use of System.Storage_Elements",
18373 Parent (Expr));
18374 return;
18376 -- If the expected type is an anonymous access type, as for access
18377 -- parameters and discriminants, the error is on the designated types.
18379 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
18380 if Comes_From_Source (Expec_Type) then
18381 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18382 else
18383 Error_Msg_NE
18384 ("expected an access type with designated}",
18385 Expr, Designated_Type (Expec_Type));
18386 end if;
18388 if Is_Access_Type (Found_Type)
18389 and then not Comes_From_Source (Found_Type)
18390 then
18391 Error_Msg_NE
18392 ("\\found an access type with designated}!",
18393 Expr, Designated_Type (Found_Type));
18394 else
18395 if From_Limited_With (Found_Type) then
18396 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
18397 Error_Msg_Qual_Level := 99;
18398 Error_Msg_NE -- CODEFIX
18399 ("\\missing `WITH &;", Expr, Scope (Found_Type));
18400 Error_Msg_Qual_Level := 0;
18401 else
18402 Error_Msg_NE ("found}!", Expr, Found_Type);
18403 end if;
18404 end if;
18406 -- Normal case of one type found, some other type expected
18408 else
18409 -- If the names of the two types are the same, see if some number
18410 -- of levels of qualification will help. Don't try more than three
18411 -- levels, and if we get to standard, it's no use (and probably
18412 -- represents an error in the compiler) Also do not bother with
18413 -- internal scope names.
18415 declare
18416 Expec_Scope : Entity_Id;
18417 Found_Scope : Entity_Id;
18419 begin
18420 Expec_Scope := Expec_Type;
18421 Found_Scope := Found_Type;
18423 for Levels in Int range 0 .. 3 loop
18424 if Chars (Expec_Scope) /= Chars (Found_Scope) then
18425 Error_Msg_Qual_Level := Levels;
18426 exit;
18427 end if;
18429 Expec_Scope := Scope (Expec_Scope);
18430 Found_Scope := Scope (Found_Scope);
18432 exit when Expec_Scope = Standard_Standard
18433 or else Found_Scope = Standard_Standard
18434 or else not Comes_From_Source (Expec_Scope)
18435 or else not Comes_From_Source (Found_Scope);
18436 end loop;
18437 end;
18439 if Is_Record_Type (Expec_Type)
18440 and then Present (Corresponding_Remote_Type (Expec_Type))
18441 then
18442 Error_Msg_NE ("expected}!", Expr,
18443 Corresponding_Remote_Type (Expec_Type));
18444 else
18445 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18446 end if;
18448 if Is_Entity_Name (Expr)
18449 and then Is_Package_Or_Generic_Package (Entity (Expr))
18450 then
18451 Error_Msg_N ("\\found package name!", Expr);
18453 elsif Is_Entity_Name (Expr)
18454 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
18455 then
18456 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
18457 Error_Msg_N
18458 ("found procedure name, possibly missing Access attribute!",
18459 Expr);
18460 else
18461 Error_Msg_N
18462 ("\\found procedure name instead of function!", Expr);
18463 end if;
18465 elsif Nkind (Expr) = N_Function_Call
18466 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
18467 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
18468 and then No (Parameter_Associations (Expr))
18469 then
18470 Error_Msg_N
18471 ("found function name, possibly missing Access attribute!",
18472 Expr);
18474 -- Catch common error: a prefix or infix operator which is not
18475 -- directly visible because the type isn't.
18477 elsif Nkind (Expr) in N_Op
18478 and then Is_Overloaded (Expr)
18479 and then not Is_Immediately_Visible (Expec_Type)
18480 and then not Is_Potentially_Use_Visible (Expec_Type)
18481 and then not In_Use (Expec_Type)
18482 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
18483 then
18484 Error_Msg_N
18485 ("operator of the type is not directly visible!", Expr);
18487 elsif Ekind (Found_Type) = E_Void
18488 and then Present (Parent (Found_Type))
18489 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
18490 then
18491 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
18493 else
18494 Error_Msg_NE ("\\found}!", Expr, Found_Type);
18495 end if;
18497 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
18498 -- of the same modular type, and (M1 and M2) = 0 was intended.
18500 if Expec_Type = Standard_Boolean
18501 and then Is_Modular_Integer_Type (Found_Type)
18502 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
18503 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
18504 then
18505 declare
18506 Op : constant Node_Id := Right_Opnd (Parent (Expr));
18507 L : constant Node_Id := Left_Opnd (Op);
18508 R : constant Node_Id := Right_Opnd (Op);
18510 begin
18511 -- The case for the message is when the left operand of the
18512 -- comparison is the same modular type, or when it is an
18513 -- integer literal (or other universal integer expression),
18514 -- which would have been typed as the modular type if the
18515 -- parens had been there.
18517 if (Etype (L) = Found_Type
18518 or else
18519 Etype (L) = Universal_Integer)
18520 and then Is_Integer_Type (Etype (R))
18521 then
18522 Error_Msg_N
18523 ("\\possible missing parens for modular operation", Expr);
18524 end if;
18525 end;
18526 end if;
18528 -- Reset error message qualification indication
18530 Error_Msg_Qual_Level := 0;
18531 end if;
18532 end Wrong_Type;
18534 end Sem_Util;