PR testsuite/79036 - gcc.dg/tree-ssa/builtin-sprintf.c fails starting with r244037
[official-gcc.git] / gcc / ada / sem_util.adb
blobba64d896b1d799918a5be06c8d9606f82fb39dcd
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-2016, 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 Treepr; -- ???For debugging code below
28 with Aspects; use Aspects;
29 with Atree; use Atree;
30 with Casing; use Casing;
31 with Checks; use Checks;
32 with Debug; use Debug;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Exp_Ch11; use Exp_Ch11;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet.Sp; use Namet.Sp;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Output; use Output;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Attr; use Sem_Attr;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Prag; use Sem_Prag;
57 with Sem_Res; use Sem_Res;
58 with Sem_Warn; use Sem_Warn;
59 with Sem_Type; use Sem_Type;
60 with Sinfo; use Sinfo;
61 with Sinput; use Sinput;
62 with Stand; use Stand;
63 with Style;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uname; use Uname;
70 with GNAT.HTable; use GNAT.HTable;
72 package body Sem_Util is
74 ----------------------------------------
75 -- Global Variables for New_Copy_Tree --
76 ----------------------------------------
78 -- These global variables are used by New_Copy_Tree. See description of the
79 -- body of this subprogram for details. Global variables can be safely used
80 -- by New_Copy_Tree, since there is no case of a recursive call from the
81 -- processing inside New_Copy_Tree.
83 NCT_Hash_Threshold : constant := 20;
84 -- If there are more than this number of pairs of entries in the map, then
85 -- Hash_Tables_Used will be set, and the hash tables will be initialized
86 -- and used for the searches.
88 NCT_Hash_Tables_Used : Boolean := False;
89 -- Set to True if hash tables are in use
91 NCT_Table_Entries : Nat := 0;
92 -- Count entries in table to see if threshold is reached
94 NCT_Hash_Table_Setup : Boolean := False;
95 -- Set to True if hash table contains data. We set this True if we setup
96 -- the hash table with data, and leave it set permanently from then on,
97 -- this is a signal that second and subsequent users of the hash table
98 -- must clear the old entries before reuse.
100 subtype NCT_Header_Num is Int range 0 .. 511;
101 -- Defines range of headers in hash tables (512 headers)
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 function Build_Component_Subtype
108 (C : List_Id;
109 Loc : Source_Ptr;
110 T : Entity_Id) return Node_Id;
111 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
112 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
113 -- Loc is the source location, T is the original subtype.
115 function Has_Enabled_Property
116 (Item_Id : Entity_Id;
117 Property : Name_Id) return Boolean;
118 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
119 -- Determine whether an abstract state or a variable denoted by entity
120 -- Item_Id has enabled property Property.
122 function Has_Null_Extension (T : Entity_Id) return Boolean;
123 -- T is a derived tagged type. Check whether the type extension is null.
124 -- If the parent type is fully initialized, T can be treated as such.
126 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
127 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
128 -- with discriminants whose default values are static, examine only the
129 -- components in the selected variant to determine whether all of them
130 -- have a default.
132 ------------------------------
133 -- Abstract_Interface_List --
134 ------------------------------
136 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
137 Nod : Node_Id;
139 begin
140 if Is_Concurrent_Type (Typ) then
142 -- If we are dealing with a synchronized subtype, go to the base
143 -- type, whose declaration has the interface list.
145 -- Shouldn't this be Declaration_Node???
147 Nod := Parent (Base_Type (Typ));
149 if Nkind (Nod) = N_Full_Type_Declaration then
150 return Empty_List;
151 end if;
153 elsif Ekind (Typ) = E_Record_Type_With_Private then
154 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
155 Nod := Type_Definition (Parent (Typ));
157 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
158 if Present (Full_View (Typ))
159 and then
160 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
161 then
162 Nod := Type_Definition (Parent (Full_View (Typ)));
164 -- If the full-view is not available we cannot do anything else
165 -- here (the source has errors).
167 else
168 return Empty_List;
169 end if;
171 -- Support for generic formals with interfaces is still missing ???
173 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
174 return Empty_List;
176 else
177 pragma Assert
178 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
179 Nod := Parent (Typ);
180 end if;
182 elsif Ekind (Typ) = E_Record_Subtype then
183 Nod := Type_Definition (Parent (Etype (Typ)));
185 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
187 -- Recurse, because parent may still be a private extension. Also
188 -- note that the full view of the subtype or the full view of its
189 -- base type may (both) be unavailable.
191 return Abstract_Interface_List (Etype (Typ));
193 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
194 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
195 Nod := Formal_Type_Definition (Parent (Typ));
196 else
197 Nod := Type_Definition (Parent (Typ));
198 end if;
199 end if;
201 return Interface_List (Nod);
202 end Abstract_Interface_List;
204 --------------------------------
205 -- Add_Access_Type_To_Process --
206 --------------------------------
208 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
209 L : Elist_Id;
211 begin
212 Ensure_Freeze_Node (E);
213 L := Access_Types_To_Process (Freeze_Node (E));
215 if No (L) then
216 L := New_Elmt_List;
217 Set_Access_Types_To_Process (Freeze_Node (E), L);
218 end if;
220 Append_Elmt (A, L);
221 end Add_Access_Type_To_Process;
223 --------------------------
224 -- Add_Block_Identifier --
225 --------------------------
227 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
228 Loc : constant Source_Ptr := Sloc (N);
230 begin
231 pragma Assert (Nkind (N) = N_Block_Statement);
233 -- The block already has a label, return its entity
235 if Present (Identifier (N)) then
236 Id := Entity (Identifier (N));
238 -- Create a new block label and set its attributes
240 else
241 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
242 Set_Etype (Id, Standard_Void_Type);
243 Set_Parent (Id, N);
245 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
246 Set_Block_Node (Id, Identifier (N));
247 end if;
248 end Add_Block_Identifier;
250 ----------------------------
251 -- Add_Global_Declaration --
252 ----------------------------
254 procedure Add_Global_Declaration (N : Node_Id) is
255 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
257 begin
258 if No (Declarations (Aux_Node)) then
259 Set_Declarations (Aux_Node, New_List);
260 end if;
262 Append_To (Declarations (Aux_Node), N);
263 Analyze (N);
264 end Add_Global_Declaration;
266 --------------------------------
267 -- Address_Integer_Convert_OK --
268 --------------------------------
270 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
271 begin
272 if Allow_Integer_Address
273 and then ((Is_Descendant_Of_Address (T1)
274 and then Is_Private_Type (T1)
275 and then Is_Integer_Type (T2))
276 or else
277 (Is_Descendant_Of_Address (T2)
278 and then Is_Private_Type (T2)
279 and then Is_Integer_Type (T1)))
280 then
281 return True;
282 else
283 return False;
284 end if;
285 end Address_Integer_Convert_OK;
287 -------------------
288 -- Address_Value --
289 -------------------
291 function Address_Value (N : Node_Id) return Node_Id is
292 Expr : Node_Id := N;
294 begin
295 loop
296 -- For constant, get constant expression
298 if Is_Entity_Name (Expr)
299 and then Ekind (Entity (Expr)) = E_Constant
300 then
301 Expr := Constant_Value (Entity (Expr));
303 -- For unchecked conversion, get result to convert
305 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
306 Expr := Expression (Expr);
308 -- For (common case) of To_Address call, get argument
310 elsif Nkind (Expr) = N_Function_Call
311 and then Is_Entity_Name (Name (Expr))
312 and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
313 then
314 Expr := First (Parameter_Associations (Expr));
316 if Nkind (Expr) = N_Parameter_Association then
317 Expr := Explicit_Actual_Parameter (Expr);
318 end if;
320 -- We finally have the real expression
322 else
323 exit;
324 end if;
325 end loop;
327 return Expr;
328 end Address_Value;
330 -----------------
331 -- Addressable --
332 -----------------
334 -- For now, just 8/16/32/64
336 function Addressable (V : Uint) return Boolean is
337 begin
338 return V = Uint_8 or else
339 V = Uint_16 or else
340 V = Uint_32 or else
341 V = Uint_64;
342 end Addressable;
344 function Addressable (V : Int) return Boolean is
345 begin
346 return V = 8 or else
347 V = 16 or else
348 V = 32 or else
349 V = 64;
350 end Addressable;
352 ---------------------------------
353 -- Aggregate_Constraint_Checks --
354 ---------------------------------
356 procedure Aggregate_Constraint_Checks
357 (Exp : Node_Id;
358 Check_Typ : Entity_Id)
360 Exp_Typ : constant Entity_Id := Etype (Exp);
362 begin
363 if Raises_Constraint_Error (Exp) then
364 return;
365 end if;
367 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
368 -- component's type to force the appropriate accessibility checks.
370 -- Ada 2005 (AI-231): Generate conversion to the null-excluding type to
371 -- force the corresponding run-time check
373 if Is_Access_Type (Check_Typ)
374 and then Is_Local_Anonymous_Access (Check_Typ)
375 then
376 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
377 Analyze_And_Resolve (Exp, Check_Typ);
378 Check_Unset_Reference (Exp);
379 end if;
381 -- What follows is really expansion activity, so check that expansion
382 -- is on and is allowed. In GNATprove mode, we also want check flags to
383 -- be added in the tree, so that the formal verification can rely on
384 -- those to be present. In GNATprove mode for formal verification, some
385 -- treatment typically only done during expansion needs to be performed
386 -- on the tree, but it should not be applied inside generics. Otherwise,
387 -- this breaks the name resolution mechanism for generic instances.
389 if not Expander_Active
390 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
391 then
392 return;
393 end if;
395 if Is_Access_Type (Check_Typ)
396 and then Can_Never_Be_Null (Check_Typ)
397 and then not Can_Never_Be_Null (Exp_Typ)
398 then
399 Install_Null_Excluding_Check (Exp);
400 end if;
402 -- First check if we have to insert discriminant checks
404 if Has_Discriminants (Exp_Typ) then
405 Apply_Discriminant_Check (Exp, Check_Typ);
407 -- Next emit length checks for array aggregates
409 elsif Is_Array_Type (Exp_Typ) then
410 Apply_Length_Check (Exp, Check_Typ);
412 -- Finally emit scalar and string checks. If we are dealing with a
413 -- scalar literal we need to check by hand because the Etype of
414 -- literals is not necessarily correct.
416 elsif Is_Scalar_Type (Exp_Typ)
417 and then Compile_Time_Known_Value (Exp)
418 then
419 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
420 Apply_Compile_Time_Constraint_Error
421 (Exp, "value not in range of}??", CE_Range_Check_Failed,
422 Ent => Base_Type (Check_Typ),
423 Typ => Base_Type (Check_Typ));
425 elsif Is_Out_Of_Range (Exp, Check_Typ) then
426 Apply_Compile_Time_Constraint_Error
427 (Exp, "value not in range of}??", CE_Range_Check_Failed,
428 Ent => Check_Typ,
429 Typ => Check_Typ);
431 elsif not Range_Checks_Suppressed (Check_Typ) then
432 Apply_Scalar_Range_Check (Exp, Check_Typ);
433 end if;
435 -- Verify that target type is also scalar, to prevent view anomalies
436 -- in instantiations.
438 elsif (Is_Scalar_Type (Exp_Typ)
439 or else Nkind (Exp) = N_String_Literal)
440 and then Is_Scalar_Type (Check_Typ)
441 and then Exp_Typ /= Check_Typ
442 then
443 if Is_Entity_Name (Exp)
444 and then Ekind (Entity (Exp)) = E_Constant
445 then
446 -- If expression is a constant, it is worthwhile checking whether
447 -- it is a bound of the type.
449 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
450 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
451 or else
452 (Is_Entity_Name (Type_High_Bound (Check_Typ))
453 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
454 then
455 return;
457 else
458 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
459 Analyze_And_Resolve (Exp, Check_Typ);
460 Check_Unset_Reference (Exp);
461 end if;
463 -- Could use a comment on this case ???
465 else
466 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
467 Analyze_And_Resolve (Exp, Check_Typ);
468 Check_Unset_Reference (Exp);
469 end if;
471 end if;
472 end Aggregate_Constraint_Checks;
474 -----------------------
475 -- Alignment_In_Bits --
476 -----------------------
478 function Alignment_In_Bits (E : Entity_Id) return Uint is
479 begin
480 return Alignment (E) * System_Storage_Unit;
481 end Alignment_In_Bits;
483 --------------------------------------
484 -- All_Composite_Constraints_Static --
485 --------------------------------------
487 function All_Composite_Constraints_Static
488 (Constr : Node_Id) return Boolean
490 begin
491 if No (Constr) or else Error_Posted (Constr) then
492 return True;
493 end if;
495 case Nkind (Constr) is
496 when N_Subexpr =>
497 if Nkind (Constr) in N_Has_Entity
498 and then Present (Entity (Constr))
499 then
500 if Is_Type (Entity (Constr)) then
501 return
502 not Is_Discrete_Type (Entity (Constr))
503 or else Is_OK_Static_Subtype (Entity (Constr));
504 end if;
506 elsif Nkind (Constr) = N_Range then
507 return
508 Is_OK_Static_Expression (Low_Bound (Constr))
509 and then
510 Is_OK_Static_Expression (High_Bound (Constr));
512 elsif Nkind (Constr) = N_Attribute_Reference
513 and then Attribute_Name (Constr) = Name_Range
514 then
515 return
516 Is_OK_Static_Expression
517 (Type_Low_Bound (Etype (Prefix (Constr))))
518 and then
519 Is_OK_Static_Expression
520 (Type_High_Bound (Etype (Prefix (Constr))));
521 end if;
523 return
524 not Present (Etype (Constr)) -- previous error
525 or else not Is_Discrete_Type (Etype (Constr))
526 or else Is_OK_Static_Expression (Constr);
528 when N_Discriminant_Association =>
529 return All_Composite_Constraints_Static (Expression (Constr));
531 when N_Range_Constraint =>
532 return
533 All_Composite_Constraints_Static (Range_Expression (Constr));
535 when N_Index_Or_Discriminant_Constraint =>
536 declare
537 One_Cstr : Entity_Id;
538 begin
539 One_Cstr := First (Constraints (Constr));
540 while Present (One_Cstr) loop
541 if not All_Composite_Constraints_Static (One_Cstr) then
542 return False;
543 end if;
545 Next (One_Cstr);
546 end loop;
547 end;
549 return True;
551 when N_Subtype_Indication =>
552 return
553 All_Composite_Constraints_Static (Subtype_Mark (Constr))
554 and then
555 All_Composite_Constraints_Static (Constraint (Constr));
557 when others =>
558 raise Program_Error;
559 end case;
560 end All_Composite_Constraints_Static;
562 ---------------------------------
563 -- Append_Inherited_Subprogram --
564 ---------------------------------
566 procedure Append_Inherited_Subprogram (S : Entity_Id) is
567 Par : constant Entity_Id := Alias (S);
568 -- The parent subprogram
570 Scop : constant Entity_Id := Scope (Par);
571 -- The scope of definition of the parent subprogram
573 Typ : constant Entity_Id := Defining_Entity (Parent (S));
574 -- The derived type of which S is a primitive operation
576 Decl : Node_Id;
577 Next_E : Entity_Id;
579 begin
580 if Ekind (Current_Scope) = E_Package
581 and then In_Private_Part (Current_Scope)
582 and then Has_Private_Declaration (Typ)
583 and then Is_Tagged_Type (Typ)
584 and then Scop = Current_Scope
585 then
586 -- The inherited operation is available at the earliest place after
587 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
588 -- relevant for type extensions. If the parent operation appears
589 -- after the type extension, the operation is not visible.
591 Decl := First
592 (Visible_Declarations
593 (Package_Specification (Current_Scope)));
594 while Present (Decl) loop
595 if Nkind (Decl) = N_Private_Extension_Declaration
596 and then Defining_Entity (Decl) = Typ
597 then
598 if Sloc (Decl) > Sloc (Par) then
599 Next_E := Next_Entity (Par);
600 Set_Next_Entity (Par, S);
601 Set_Next_Entity (S, Next_E);
602 return;
604 else
605 exit;
606 end if;
607 end if;
609 Next (Decl);
610 end loop;
611 end if;
613 -- If partial view is not a type extension, or it appears before the
614 -- subprogram declaration, insert normally at end of entity list.
616 Append_Entity (S, Current_Scope);
617 end Append_Inherited_Subprogram;
619 -----------------------------------------
620 -- Apply_Compile_Time_Constraint_Error --
621 -----------------------------------------
623 procedure Apply_Compile_Time_Constraint_Error
624 (N : Node_Id;
625 Msg : String;
626 Reason : RT_Exception_Code;
627 Ent : Entity_Id := Empty;
628 Typ : Entity_Id := Empty;
629 Loc : Source_Ptr := No_Location;
630 Rep : Boolean := True;
631 Warn : Boolean := False)
633 Stat : constant Boolean := Is_Static_Expression (N);
634 R_Stat : constant Node_Id :=
635 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
636 Rtyp : Entity_Id;
638 begin
639 if No (Typ) then
640 Rtyp := Etype (N);
641 else
642 Rtyp := Typ;
643 end if;
645 Discard_Node
646 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
648 -- In GNATprove mode, do not replace the node with an exception raised.
649 -- In such a case, either the call to Compile_Time_Constraint_Error
650 -- issues an error which stops analysis, or it issues a warning in
651 -- a few cases where a suitable check flag is set for GNATprove to
652 -- generate a check message.
654 if not Rep or GNATprove_Mode then
655 return;
656 end if;
658 -- Now we replace the node by an N_Raise_Constraint_Error node
659 -- This does not need reanalyzing, so set it as analyzed now.
661 Rewrite (N, R_Stat);
662 Set_Analyzed (N, True);
664 Set_Etype (N, Rtyp);
665 Set_Raises_Constraint_Error (N);
667 -- Now deal with possible local raise handling
669 Possible_Local_Raise (N, Standard_Constraint_Error);
671 -- If the original expression was marked as static, the result is
672 -- still marked as static, but the Raises_Constraint_Error flag is
673 -- always set so that further static evaluation is not attempted.
675 if Stat then
676 Set_Is_Static_Expression (N);
677 end if;
678 end Apply_Compile_Time_Constraint_Error;
680 ---------------------------
681 -- Async_Readers_Enabled --
682 ---------------------------
684 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
685 begin
686 return Has_Enabled_Property (Id, Name_Async_Readers);
687 end Async_Readers_Enabled;
689 ---------------------------
690 -- Async_Writers_Enabled --
691 ---------------------------
693 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
694 begin
695 return Has_Enabled_Property (Id, Name_Async_Writers);
696 end Async_Writers_Enabled;
698 --------------------------------------
699 -- Available_Full_View_Of_Component --
700 --------------------------------------
702 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
703 ST : constant Entity_Id := Scope (T);
704 SCT : constant Entity_Id := Scope (Component_Type (T));
705 begin
706 return In_Open_Scopes (ST)
707 and then In_Open_Scopes (SCT)
708 and then Scope_Depth (ST) >= Scope_Depth (SCT);
709 end Available_Full_View_Of_Component;
711 -------------------
712 -- Bad_Attribute --
713 -------------------
715 procedure Bad_Attribute
716 (N : Node_Id;
717 Nam : Name_Id;
718 Warn : Boolean := False)
720 begin
721 Error_Msg_Warn := Warn;
722 Error_Msg_N ("unrecognized attribute&<<", N);
724 -- Check for possible misspelling
726 Error_Msg_Name_1 := First_Attribute_Name;
727 while Error_Msg_Name_1 <= Last_Attribute_Name loop
728 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
729 Error_Msg_N -- CODEFIX
730 ("\possible misspelling of %<<", N);
731 exit;
732 end if;
734 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
735 end loop;
736 end Bad_Attribute;
738 --------------------------------
739 -- Bad_Predicated_Subtype_Use --
740 --------------------------------
742 procedure Bad_Predicated_Subtype_Use
743 (Msg : String;
744 N : Node_Id;
745 Typ : Entity_Id;
746 Suggest_Static : Boolean := False)
748 Gen : Entity_Id;
750 begin
751 -- Avoid cascaded errors
753 if Error_Posted (N) then
754 return;
755 end if;
757 if Inside_A_Generic then
758 Gen := Current_Scope;
759 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
760 Gen := Scope (Gen);
761 end loop;
763 if No (Gen) then
764 return;
765 end if;
767 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
768 Set_No_Predicate_On_Actual (Typ);
769 end if;
771 elsif Has_Predicates (Typ) then
772 if Is_Generic_Actual_Type (Typ) then
774 -- The restriction on loop parameters is only that the type
775 -- should have no dynamic predicates.
777 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
778 and then not Has_Dynamic_Predicate_Aspect (Typ)
779 and then Is_OK_Static_Subtype (Typ)
780 then
781 return;
782 end if;
784 Gen := Current_Scope;
785 while not Is_Generic_Instance (Gen) loop
786 Gen := Scope (Gen);
787 end loop;
789 pragma Assert (Present (Gen));
791 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
792 Error_Msg_Warn := SPARK_Mode /= On;
793 Error_Msg_FE (Msg & "<<", N, Typ);
794 Error_Msg_F ("\Program_Error [<<", N);
796 Insert_Action (N,
797 Make_Raise_Program_Error (Sloc (N),
798 Reason => PE_Bad_Predicated_Generic_Type));
800 else
801 Error_Msg_FE (Msg & "<<", N, Typ);
802 end if;
804 else
805 Error_Msg_FE (Msg, N, Typ);
806 end if;
808 -- Emit an optional suggestion on how to remedy the error if the
809 -- context warrants it.
811 if Suggest_Static and then Has_Static_Predicate (Typ) then
812 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
813 end if;
814 end if;
815 end Bad_Predicated_Subtype_Use;
817 -----------------------------------------
818 -- Bad_Unordered_Enumeration_Reference --
819 -----------------------------------------
821 function Bad_Unordered_Enumeration_Reference
822 (N : Node_Id;
823 T : Entity_Id) return Boolean
825 begin
826 return Is_Enumeration_Type (T)
827 and then Warn_On_Unordered_Enumeration_Type
828 and then not Is_Generic_Type (T)
829 and then Comes_From_Source (N)
830 and then not Has_Pragma_Ordered (T)
831 and then not In_Same_Extended_Unit (N, T);
832 end Bad_Unordered_Enumeration_Reference;
834 --------------------------
835 -- Build_Actual_Subtype --
836 --------------------------
838 function Build_Actual_Subtype
839 (T : Entity_Id;
840 N : Node_Or_Entity_Id) return Node_Id
842 Loc : Source_Ptr;
843 -- Normally Sloc (N), but may point to corresponding body in some cases
845 Constraints : List_Id;
846 Decl : Node_Id;
847 Discr : Entity_Id;
848 Hi : Node_Id;
849 Lo : Node_Id;
850 Subt : Entity_Id;
851 Disc_Type : Entity_Id;
852 Obj : Node_Id;
854 begin
855 Loc := Sloc (N);
857 if Nkind (N) = N_Defining_Identifier then
858 Obj := New_Occurrence_Of (N, Loc);
860 -- If this is a formal parameter of a subprogram declaration, and
861 -- we are compiling the body, we want the declaration for the
862 -- actual subtype to carry the source position of the body, to
863 -- prevent anomalies in gdb when stepping through the code.
865 if Is_Formal (N) then
866 declare
867 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
868 begin
869 if Nkind (Decl) = N_Subprogram_Declaration
870 and then Present (Corresponding_Body (Decl))
871 then
872 Loc := Sloc (Corresponding_Body (Decl));
873 end if;
874 end;
875 end if;
877 else
878 Obj := N;
879 end if;
881 if Is_Array_Type (T) then
882 Constraints := New_List;
883 for J in 1 .. Number_Dimensions (T) loop
885 -- Build an array subtype declaration with the nominal subtype and
886 -- the bounds of the actual. Add the declaration in front of the
887 -- local declarations for the subprogram, for analysis before any
888 -- reference to the formal in the body.
890 Lo :=
891 Make_Attribute_Reference (Loc,
892 Prefix =>
893 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
894 Attribute_Name => Name_First,
895 Expressions => New_List (
896 Make_Integer_Literal (Loc, J)));
898 Hi :=
899 Make_Attribute_Reference (Loc,
900 Prefix =>
901 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
902 Attribute_Name => Name_Last,
903 Expressions => New_List (
904 Make_Integer_Literal (Loc, J)));
906 Append (Make_Range (Loc, Lo, Hi), Constraints);
907 end loop;
909 -- If the type has unknown discriminants there is no constrained
910 -- subtype to build. This is never called for a formal or for a
911 -- lhs, so returning the type is ok ???
913 elsif Has_Unknown_Discriminants (T) then
914 return T;
916 else
917 Constraints := New_List;
919 -- Type T is a generic derived type, inherit the discriminants from
920 -- the parent type.
922 if Is_Private_Type (T)
923 and then No (Full_View (T))
925 -- T was flagged as an error if it was declared as a formal
926 -- derived type with known discriminants. In this case there
927 -- is no need to look at the parent type since T already carries
928 -- its own discriminants.
930 and then not Error_Posted (T)
931 then
932 Disc_Type := Etype (Base_Type (T));
933 else
934 Disc_Type := T;
935 end if;
937 Discr := First_Discriminant (Disc_Type);
938 while Present (Discr) loop
939 Append_To (Constraints,
940 Make_Selected_Component (Loc,
941 Prefix =>
942 Duplicate_Subexpr_No_Checks (Obj),
943 Selector_Name => New_Occurrence_Of (Discr, Loc)));
944 Next_Discriminant (Discr);
945 end loop;
946 end if;
948 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
949 Set_Is_Internal (Subt);
951 Decl :=
952 Make_Subtype_Declaration (Loc,
953 Defining_Identifier => Subt,
954 Subtype_Indication =>
955 Make_Subtype_Indication (Loc,
956 Subtype_Mark => New_Occurrence_Of (T, Loc),
957 Constraint =>
958 Make_Index_Or_Discriminant_Constraint (Loc,
959 Constraints => Constraints)));
961 Mark_Rewrite_Insertion (Decl);
962 return Decl;
963 end Build_Actual_Subtype;
965 ---------------------------------------
966 -- Build_Actual_Subtype_Of_Component --
967 ---------------------------------------
969 function Build_Actual_Subtype_Of_Component
970 (T : Entity_Id;
971 N : Node_Id) return Node_Id
973 Loc : constant Source_Ptr := Sloc (N);
974 P : constant Node_Id := Prefix (N);
975 D : Elmt_Id;
976 Id : Node_Id;
977 Index_Typ : Entity_Id;
979 Desig_Typ : Entity_Id;
980 -- This is either a copy of T, or if T is an access type, then it is
981 -- the directly designated type of this access type.
983 function Build_Actual_Array_Constraint return List_Id;
984 -- If one or more of the bounds of the component depends on
985 -- discriminants, build actual constraint using the discriminants
986 -- of the prefix.
988 function Build_Actual_Record_Constraint return List_Id;
989 -- Similar to previous one, for discriminated components constrained
990 -- by the discriminant of the enclosing object.
992 -----------------------------------
993 -- Build_Actual_Array_Constraint --
994 -----------------------------------
996 function Build_Actual_Array_Constraint return List_Id is
997 Constraints : constant List_Id := New_List;
998 Indx : Node_Id;
999 Hi : Node_Id;
1000 Lo : Node_Id;
1001 Old_Hi : Node_Id;
1002 Old_Lo : Node_Id;
1004 begin
1005 Indx := First_Index (Desig_Typ);
1006 while Present (Indx) loop
1007 Old_Lo := Type_Low_Bound (Etype (Indx));
1008 Old_Hi := Type_High_Bound (Etype (Indx));
1010 if Denotes_Discriminant (Old_Lo) then
1011 Lo :=
1012 Make_Selected_Component (Loc,
1013 Prefix => New_Copy_Tree (P),
1014 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1016 else
1017 Lo := New_Copy_Tree (Old_Lo);
1019 -- The new bound will be reanalyzed in the enclosing
1020 -- declaration. For literal bounds that come from a type
1021 -- declaration, the type of the context must be imposed, so
1022 -- insure that analysis will take place. For non-universal
1023 -- types this is not strictly necessary.
1025 Set_Analyzed (Lo, False);
1026 end if;
1028 if Denotes_Discriminant (Old_Hi) then
1029 Hi :=
1030 Make_Selected_Component (Loc,
1031 Prefix => New_Copy_Tree (P),
1032 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1034 else
1035 Hi := New_Copy_Tree (Old_Hi);
1036 Set_Analyzed (Hi, False);
1037 end if;
1039 Append (Make_Range (Loc, Lo, Hi), Constraints);
1040 Next_Index (Indx);
1041 end loop;
1043 return Constraints;
1044 end Build_Actual_Array_Constraint;
1046 ------------------------------------
1047 -- Build_Actual_Record_Constraint --
1048 ------------------------------------
1050 function Build_Actual_Record_Constraint return List_Id is
1051 Constraints : constant List_Id := New_List;
1052 D : Elmt_Id;
1053 D_Val : Node_Id;
1055 begin
1056 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1057 while Present (D) loop
1058 if Denotes_Discriminant (Node (D)) then
1059 D_Val := Make_Selected_Component (Loc,
1060 Prefix => New_Copy_Tree (P),
1061 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1063 else
1064 D_Val := New_Copy_Tree (Node (D));
1065 end if;
1067 Append (D_Val, Constraints);
1068 Next_Elmt (D);
1069 end loop;
1071 return Constraints;
1072 end Build_Actual_Record_Constraint;
1074 -- Start of processing for Build_Actual_Subtype_Of_Component
1076 begin
1077 -- Why the test for Spec_Expression mode here???
1079 if In_Spec_Expression then
1080 return Empty;
1082 -- More comments for the rest of this body would be good ???
1084 elsif Nkind (N) = N_Explicit_Dereference then
1085 if Is_Composite_Type (T)
1086 and then not Is_Constrained (T)
1087 and then not (Is_Class_Wide_Type (T)
1088 and then Is_Constrained (Root_Type (T)))
1089 and then not Has_Unknown_Discriminants (T)
1090 then
1091 -- If the type of the dereference is already constrained, it is an
1092 -- actual subtype.
1094 if Is_Array_Type (Etype (N))
1095 and then Is_Constrained (Etype (N))
1096 then
1097 return Empty;
1098 else
1099 Remove_Side_Effects (P);
1100 return Build_Actual_Subtype (T, N);
1101 end if;
1102 else
1103 return Empty;
1104 end if;
1105 end if;
1107 if Ekind (T) = E_Access_Subtype then
1108 Desig_Typ := Designated_Type (T);
1109 else
1110 Desig_Typ := T;
1111 end if;
1113 if Ekind (Desig_Typ) = E_Array_Subtype then
1114 Id := First_Index (Desig_Typ);
1115 while Present (Id) loop
1116 Index_Typ := Underlying_Type (Etype (Id));
1118 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1119 or else
1120 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1121 then
1122 Remove_Side_Effects (P);
1123 return
1124 Build_Component_Subtype
1125 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1126 end if;
1128 Next_Index (Id);
1129 end loop;
1131 elsif Is_Composite_Type (Desig_Typ)
1132 and then Has_Discriminants (Desig_Typ)
1133 and then not Has_Unknown_Discriminants (Desig_Typ)
1134 then
1135 if Is_Private_Type (Desig_Typ)
1136 and then No (Discriminant_Constraint (Desig_Typ))
1137 then
1138 Desig_Typ := Full_View (Desig_Typ);
1139 end if;
1141 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1142 while Present (D) loop
1143 if Denotes_Discriminant (Node (D)) then
1144 Remove_Side_Effects (P);
1145 return
1146 Build_Component_Subtype (
1147 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1148 end if;
1150 Next_Elmt (D);
1151 end loop;
1152 end if;
1154 -- If none of the above, the actual and nominal subtypes are the same
1156 return Empty;
1157 end Build_Actual_Subtype_Of_Component;
1159 -----------------------------
1160 -- Build_Component_Subtype --
1161 -----------------------------
1163 function Build_Component_Subtype
1164 (C : List_Id;
1165 Loc : Source_Ptr;
1166 T : Entity_Id) return Node_Id
1168 Subt : Entity_Id;
1169 Decl : Node_Id;
1171 begin
1172 -- Unchecked_Union components do not require component subtypes
1174 if Is_Unchecked_Union (T) then
1175 return Empty;
1176 end if;
1178 Subt := Make_Temporary (Loc, 'S');
1179 Set_Is_Internal (Subt);
1181 Decl :=
1182 Make_Subtype_Declaration (Loc,
1183 Defining_Identifier => Subt,
1184 Subtype_Indication =>
1185 Make_Subtype_Indication (Loc,
1186 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1187 Constraint =>
1188 Make_Index_Or_Discriminant_Constraint (Loc,
1189 Constraints => C)));
1191 Mark_Rewrite_Insertion (Decl);
1192 return Decl;
1193 end Build_Component_Subtype;
1195 ---------------------------
1196 -- Build_Default_Subtype --
1197 ---------------------------
1199 function Build_Default_Subtype
1200 (T : Entity_Id;
1201 N : Node_Id) return Entity_Id
1203 Loc : constant Source_Ptr := Sloc (N);
1204 Disc : Entity_Id;
1206 Bas : Entity_Id;
1207 -- The base type that is to be constrained by the defaults
1209 begin
1210 if not Has_Discriminants (T) or else Is_Constrained (T) then
1211 return T;
1212 end if;
1214 Bas := Base_Type (T);
1216 -- If T is non-private but its base type is private, this is the
1217 -- completion of a subtype declaration whose parent type is private
1218 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1219 -- are to be found in the full view of the base. Check that the private
1220 -- status of T and its base differ.
1222 if Is_Private_Type (Bas)
1223 and then not Is_Private_Type (T)
1224 and then Present (Full_View (Bas))
1225 then
1226 Bas := Full_View (Bas);
1227 end if;
1229 Disc := First_Discriminant (T);
1231 if No (Discriminant_Default_Value (Disc)) then
1232 return T;
1233 end if;
1235 declare
1236 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1237 Constraints : constant List_Id := New_List;
1238 Decl : Node_Id;
1240 begin
1241 while Present (Disc) loop
1242 Append_To (Constraints,
1243 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1244 Next_Discriminant (Disc);
1245 end loop;
1247 Decl :=
1248 Make_Subtype_Declaration (Loc,
1249 Defining_Identifier => Act,
1250 Subtype_Indication =>
1251 Make_Subtype_Indication (Loc,
1252 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1253 Constraint =>
1254 Make_Index_Or_Discriminant_Constraint (Loc,
1255 Constraints => Constraints)));
1257 Insert_Action (N, Decl);
1259 -- If the context is a component declaration the subtype declaration
1260 -- will be analyzed when the enclosing type is frozen, otherwise do
1261 -- it now.
1263 if Ekind (Current_Scope) /= E_Record_Type then
1264 Analyze (Decl);
1265 end if;
1267 return Act;
1268 end;
1269 end Build_Default_Subtype;
1271 --------------------------------------------
1272 -- Build_Discriminal_Subtype_Of_Component --
1273 --------------------------------------------
1275 function Build_Discriminal_Subtype_Of_Component
1276 (T : Entity_Id) return Node_Id
1278 Loc : constant Source_Ptr := Sloc (T);
1279 D : Elmt_Id;
1280 Id : Node_Id;
1282 function Build_Discriminal_Array_Constraint return List_Id;
1283 -- If one or more of the bounds of the component depends on
1284 -- discriminants, build actual constraint using the discriminants
1285 -- of the prefix.
1287 function Build_Discriminal_Record_Constraint return List_Id;
1288 -- Similar to previous one, for discriminated components constrained by
1289 -- the discriminant of the enclosing object.
1291 ----------------------------------------
1292 -- Build_Discriminal_Array_Constraint --
1293 ----------------------------------------
1295 function Build_Discriminal_Array_Constraint return List_Id is
1296 Constraints : constant List_Id := New_List;
1297 Indx : Node_Id;
1298 Hi : Node_Id;
1299 Lo : Node_Id;
1300 Old_Hi : Node_Id;
1301 Old_Lo : Node_Id;
1303 begin
1304 Indx := First_Index (T);
1305 while Present (Indx) loop
1306 Old_Lo := Type_Low_Bound (Etype (Indx));
1307 Old_Hi := Type_High_Bound (Etype (Indx));
1309 if Denotes_Discriminant (Old_Lo) then
1310 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1312 else
1313 Lo := New_Copy_Tree (Old_Lo);
1314 end if;
1316 if Denotes_Discriminant (Old_Hi) then
1317 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1319 else
1320 Hi := New_Copy_Tree (Old_Hi);
1321 end if;
1323 Append (Make_Range (Loc, Lo, Hi), Constraints);
1324 Next_Index (Indx);
1325 end loop;
1327 return Constraints;
1328 end Build_Discriminal_Array_Constraint;
1330 -----------------------------------------
1331 -- Build_Discriminal_Record_Constraint --
1332 -----------------------------------------
1334 function Build_Discriminal_Record_Constraint return List_Id is
1335 Constraints : constant List_Id := New_List;
1336 D : Elmt_Id;
1337 D_Val : Node_Id;
1339 begin
1340 D := First_Elmt (Discriminant_Constraint (T));
1341 while Present (D) loop
1342 if Denotes_Discriminant (Node (D)) then
1343 D_Val :=
1344 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1345 else
1346 D_Val := New_Copy_Tree (Node (D));
1347 end if;
1349 Append (D_Val, Constraints);
1350 Next_Elmt (D);
1351 end loop;
1353 return Constraints;
1354 end Build_Discriminal_Record_Constraint;
1356 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1358 begin
1359 if Ekind (T) = E_Array_Subtype then
1360 Id := First_Index (T);
1361 while Present (Id) loop
1362 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1363 or else
1364 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1365 then
1366 return Build_Component_Subtype
1367 (Build_Discriminal_Array_Constraint, Loc, T);
1368 end if;
1370 Next_Index (Id);
1371 end loop;
1373 elsif Ekind (T) = E_Record_Subtype
1374 and then Has_Discriminants (T)
1375 and then not Has_Unknown_Discriminants (T)
1376 then
1377 D := First_Elmt (Discriminant_Constraint (T));
1378 while Present (D) loop
1379 if Denotes_Discriminant (Node (D)) then
1380 return Build_Component_Subtype
1381 (Build_Discriminal_Record_Constraint, Loc, T);
1382 end if;
1384 Next_Elmt (D);
1385 end loop;
1386 end if;
1388 -- If none of the above, the actual and nominal subtypes are the same
1390 return Empty;
1391 end Build_Discriminal_Subtype_Of_Component;
1393 ------------------------------
1394 -- Build_Elaboration_Entity --
1395 ------------------------------
1397 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1398 Loc : constant Source_Ptr := Sloc (N);
1399 Decl : Node_Id;
1400 Elab_Ent : Entity_Id;
1402 procedure Set_Package_Name (Ent : Entity_Id);
1403 -- Given an entity, sets the fully qualified name of the entity in
1404 -- Name_Buffer, with components separated by double underscores. This
1405 -- is a recursive routine that climbs the scope chain to Standard.
1407 ----------------------
1408 -- Set_Package_Name --
1409 ----------------------
1411 procedure Set_Package_Name (Ent : Entity_Id) is
1412 begin
1413 if Scope (Ent) /= Standard_Standard then
1414 Set_Package_Name (Scope (Ent));
1416 declare
1417 Nam : constant String := Get_Name_String (Chars (Ent));
1418 begin
1419 Name_Buffer (Name_Len + 1) := '_';
1420 Name_Buffer (Name_Len + 2) := '_';
1421 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1422 Name_Len := Name_Len + Nam'Length + 2;
1423 end;
1425 else
1426 Get_Name_String (Chars (Ent));
1427 end if;
1428 end Set_Package_Name;
1430 -- Start of processing for Build_Elaboration_Entity
1432 begin
1433 -- Ignore call if already constructed
1435 if Present (Elaboration_Entity (Spec_Id)) then
1436 return;
1438 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1439 -- no role in analysis.
1441 elsif ASIS_Mode then
1442 return;
1444 -- See if we need elaboration entity.
1446 -- We always need an elaboration entity when preserving control flow, as
1447 -- we want to remain explicit about the unit's elaboration order.
1449 elsif Opt.Suppress_Control_Flow_Optimizations then
1450 null;
1452 -- We always need an elaboration entity for the dynamic elaboration
1453 -- model, since it is needed to properly generate the PE exception for
1454 -- access before elaboration.
1456 elsif Dynamic_Elaboration_Checks then
1457 null;
1459 -- For the static model, we don't need the elaboration counter if this
1460 -- unit is sure to have no elaboration code, since that means there
1461 -- is no elaboration unit to be called. Note that we can't just decide
1462 -- after the fact by looking to see whether there was elaboration code,
1463 -- because that's too late to make this decision.
1465 elsif Restriction_Active (No_Elaboration_Code) then
1466 return;
1468 -- Similarly, for the static model, we can skip the elaboration counter
1469 -- if we have the No_Multiple_Elaboration restriction, since for the
1470 -- static model, that's the only purpose of the counter (to avoid
1471 -- multiple elaboration).
1473 elsif Restriction_Active (No_Multiple_Elaboration) then
1474 return;
1475 end if;
1477 -- Here we need the elaboration entity
1479 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1480 -- name with dots replaced by double underscore. We have to manually
1481 -- construct this name, since it will be elaborated in the outer scope,
1482 -- and thus will not have the unit name automatically prepended.
1484 Set_Package_Name (Spec_Id);
1485 Add_Str_To_Name_Buffer ("_E");
1487 -- Create elaboration counter
1489 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1490 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1492 Decl :=
1493 Make_Object_Declaration (Loc,
1494 Defining_Identifier => Elab_Ent,
1495 Object_Definition =>
1496 New_Occurrence_Of (Standard_Short_Integer, Loc),
1497 Expression => Make_Integer_Literal (Loc, Uint_0));
1499 Push_Scope (Standard_Standard);
1500 Add_Global_Declaration (Decl);
1501 Pop_Scope;
1503 -- Reset True_Constant indication, since we will indeed assign a value
1504 -- to the variable in the binder main. We also kill the Current_Value
1505 -- and Last_Assignment fields for the same reason.
1507 Set_Is_True_Constant (Elab_Ent, False);
1508 Set_Current_Value (Elab_Ent, Empty);
1509 Set_Last_Assignment (Elab_Ent, Empty);
1511 -- We do not want any further qualification of the name (if we did not
1512 -- do this, we would pick up the name of the generic package in the case
1513 -- of a library level generic instantiation).
1515 Set_Has_Qualified_Name (Elab_Ent);
1516 Set_Has_Fully_Qualified_Name (Elab_Ent);
1517 end Build_Elaboration_Entity;
1519 --------------------------------
1520 -- Build_Explicit_Dereference --
1521 --------------------------------
1523 procedure Build_Explicit_Dereference
1524 (Expr : Node_Id;
1525 Disc : Entity_Id)
1527 Loc : constant Source_Ptr := Sloc (Expr);
1528 I : Interp_Index;
1529 It : Interp;
1531 begin
1532 -- An entity of a type with a reference aspect is overloaded with
1533 -- both interpretations: with and without the dereference. Now that
1534 -- the dereference is made explicit, set the type of the node properly,
1535 -- to prevent anomalies in the backend. Same if the expression is an
1536 -- overloaded function call whose return type has a reference aspect.
1538 if Is_Entity_Name (Expr) then
1539 Set_Etype (Expr, Etype (Entity (Expr)));
1541 -- The designated entity will not be examined again when resolving
1542 -- the dereference, so generate a reference to it now.
1544 Generate_Reference (Entity (Expr), Expr);
1546 elsif Nkind (Expr) = N_Function_Call then
1548 -- If the name of the indexing function is overloaded, locate the one
1549 -- whose return type has an implicit dereference on the desired
1550 -- discriminant, and set entity and type of function call.
1552 if Is_Overloaded (Name (Expr)) then
1553 Get_First_Interp (Name (Expr), I, It);
1555 while Present (It.Nam) loop
1556 if Ekind ((It.Typ)) = E_Record_Type
1557 and then First_Entity ((It.Typ)) = Disc
1558 then
1559 Set_Entity (Name (Expr), It.Nam);
1560 Set_Etype (Name (Expr), Etype (It.Nam));
1561 exit;
1562 end if;
1564 Get_Next_Interp (I, It);
1565 end loop;
1566 end if;
1568 -- Set type of call from resolved function name.
1570 Set_Etype (Expr, Etype (Name (Expr)));
1571 end if;
1573 Set_Is_Overloaded (Expr, False);
1575 -- The expression will often be a generalized indexing that yields a
1576 -- container element that is then dereferenced, in which case the
1577 -- generalized indexing call is also non-overloaded.
1579 if Nkind (Expr) = N_Indexed_Component
1580 and then Present (Generalized_Indexing (Expr))
1581 then
1582 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1583 end if;
1585 Rewrite (Expr,
1586 Make_Explicit_Dereference (Loc,
1587 Prefix =>
1588 Make_Selected_Component (Loc,
1589 Prefix => Relocate_Node (Expr),
1590 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1591 Set_Etype (Prefix (Expr), Etype (Disc));
1592 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1593 end Build_Explicit_Dereference;
1595 -----------------------------------
1596 -- Cannot_Raise_Constraint_Error --
1597 -----------------------------------
1599 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1600 begin
1601 if Compile_Time_Known_Value (Expr) then
1602 return True;
1604 elsif Do_Range_Check (Expr) then
1605 return False;
1607 elsif Raises_Constraint_Error (Expr) then
1608 return False;
1610 else
1611 case Nkind (Expr) is
1612 when N_Identifier =>
1613 return True;
1615 when N_Expanded_Name =>
1616 return True;
1618 when N_Selected_Component =>
1619 return not Do_Discriminant_Check (Expr);
1621 when N_Attribute_Reference =>
1622 if Do_Overflow_Check (Expr) then
1623 return False;
1625 elsif No (Expressions (Expr)) then
1626 return True;
1628 else
1629 declare
1630 N : Node_Id;
1632 begin
1633 N := First (Expressions (Expr));
1634 while Present (N) loop
1635 if Cannot_Raise_Constraint_Error (N) then
1636 Next (N);
1637 else
1638 return False;
1639 end if;
1640 end loop;
1642 return True;
1643 end;
1644 end if;
1646 when N_Type_Conversion =>
1647 if Do_Overflow_Check (Expr)
1648 or else Do_Length_Check (Expr)
1649 or else Do_Tag_Check (Expr)
1650 then
1651 return False;
1652 else
1653 return Cannot_Raise_Constraint_Error (Expression (Expr));
1654 end if;
1656 when N_Unchecked_Type_Conversion =>
1657 return Cannot_Raise_Constraint_Error (Expression (Expr));
1659 when N_Unary_Op =>
1660 if Do_Overflow_Check (Expr) then
1661 return False;
1662 else
1663 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1664 end if;
1666 when N_Op_Divide |
1667 N_Op_Mod |
1668 N_Op_Rem
1670 if Do_Division_Check (Expr)
1671 or else
1672 Do_Overflow_Check (Expr)
1673 then
1674 return False;
1675 else
1676 return
1677 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1678 and then
1679 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1680 end if;
1682 when N_Op_Add |
1683 N_Op_And |
1684 N_Op_Concat |
1685 N_Op_Eq |
1686 N_Op_Expon |
1687 N_Op_Ge |
1688 N_Op_Gt |
1689 N_Op_Le |
1690 N_Op_Lt |
1691 N_Op_Multiply |
1692 N_Op_Ne |
1693 N_Op_Or |
1694 N_Op_Rotate_Left |
1695 N_Op_Rotate_Right |
1696 N_Op_Shift_Left |
1697 N_Op_Shift_Right |
1698 N_Op_Shift_Right_Arithmetic |
1699 N_Op_Subtract |
1700 N_Op_Xor
1702 if Do_Overflow_Check (Expr) then
1703 return False;
1704 else
1705 return
1706 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1707 and then
1708 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1709 end if;
1711 when others =>
1712 return False;
1713 end case;
1714 end if;
1715 end Cannot_Raise_Constraint_Error;
1717 -----------------------------
1718 -- Check_Part_Of_Reference --
1719 -----------------------------
1721 procedure Check_Part_Of_Reference (Var_Id : Entity_Id; Ref : Node_Id) is
1722 Conc_Typ : constant Entity_Id := Encapsulating_State (Var_Id);
1723 Decl : Node_Id;
1724 OK_Use : Boolean := False;
1725 Par : Node_Id;
1726 Prag_Nam : Name_Id;
1727 Spec_Id : Entity_Id;
1729 begin
1730 -- Traverse the parent chain looking for a suitable context for the
1731 -- reference to the concurrent constituent.
1733 Par := Parent (Ref);
1734 while Present (Par) loop
1735 if Nkind (Par) = N_Pragma then
1736 Prag_Nam := Pragma_Name_Mapped (Par);
1738 -- A concurrent constituent is allowed to appear in pragmas
1739 -- Initial_Condition and Initializes as this is part of the
1740 -- elaboration checks for the constituent (SPARK RM 9.3).
1742 if Nam_In (Prag_Nam, Name_Initial_Condition, Name_Initializes) then
1743 OK_Use := True;
1744 exit;
1746 -- When the reference appears within pragma Depends or Global,
1747 -- check whether the pragma applies to a single task type. Note
1748 -- that the pragma is not encapsulated by the type definition,
1749 -- but this is still a valid context.
1751 elsif Nam_In (Prag_Nam, Name_Depends, Name_Global) then
1752 Decl := Find_Related_Declaration_Or_Body (Par);
1754 if Nkind (Decl) = N_Object_Declaration
1755 and then Defining_Entity (Decl) = Conc_Typ
1756 then
1757 OK_Use := True;
1758 exit;
1759 end if;
1760 end if;
1762 -- The reference appears somewhere in the definition of the single
1763 -- protected/task type (SPARK RM 9.3).
1765 elsif Nkind_In (Par, N_Single_Protected_Declaration,
1766 N_Single_Task_Declaration)
1767 and then Defining_Entity (Par) = Conc_Typ
1768 then
1769 OK_Use := True;
1770 exit;
1772 -- The reference appears within the expanded declaration or the body
1773 -- of the single protected/task type (SPARK RM 9.3).
1775 elsif Nkind_In (Par, N_Protected_Body,
1776 N_Protected_Type_Declaration,
1777 N_Task_Body,
1778 N_Task_Type_Declaration)
1779 then
1780 Spec_Id := Unique_Defining_Entity (Par);
1782 if Present (Anonymous_Object (Spec_Id))
1783 and then Anonymous_Object (Spec_Id) = Conc_Typ
1784 then
1785 OK_Use := True;
1786 exit;
1787 end if;
1789 -- The reference has been relocated within an internally generated
1790 -- package or subprogram. Assume that the reference is legal as the
1791 -- real check was already performed in the original context of the
1792 -- reference.
1794 elsif Nkind_In (Par, N_Package_Body,
1795 N_Package_Declaration,
1796 N_Subprogram_Body,
1797 N_Subprogram_Declaration)
1798 and then not Comes_From_Source (Par)
1799 then
1800 OK_Use := True;
1801 exit;
1803 -- The reference has been relocated to an inlined body for GNATprove.
1804 -- Assume that the reference is legal as the real check was already
1805 -- performed in the original context of the reference.
1807 elsif GNATprove_Mode
1808 and then Nkind (Par) = N_Subprogram_Body
1809 and then Chars (Defining_Entity (Par)) = Name_uParent
1810 then
1811 OK_Use := True;
1812 exit;
1813 end if;
1815 Par := Parent (Par);
1816 end loop;
1818 -- The reference is illegal as it appears outside the definition or
1819 -- body of the single protected/task type.
1821 if not OK_Use then
1822 Error_Msg_NE
1823 ("reference to variable & cannot appear in this context",
1824 Ref, Var_Id);
1825 Error_Msg_Name_1 := Chars (Var_Id);
1827 if Ekind (Conc_Typ) = E_Protected_Type then
1828 Error_Msg_NE
1829 ("\% is constituent of single protected type &", Ref, Conc_Typ);
1830 else
1831 Error_Msg_NE
1832 ("\% is constituent of single task type &", Ref, Conc_Typ);
1833 end if;
1834 end if;
1835 end Check_Part_Of_Reference;
1837 -----------------------------------------
1838 -- Check_Dynamically_Tagged_Expression --
1839 -----------------------------------------
1841 procedure Check_Dynamically_Tagged_Expression
1842 (Expr : Node_Id;
1843 Typ : Entity_Id;
1844 Related_Nod : Node_Id)
1846 begin
1847 pragma Assert (Is_Tagged_Type (Typ));
1849 -- In order to avoid spurious errors when analyzing the expanded code,
1850 -- this check is done only for nodes that come from source and for
1851 -- actuals of generic instantiations.
1853 if (Comes_From_Source (Related_Nod)
1854 or else In_Generic_Actual (Expr))
1855 and then (Is_Class_Wide_Type (Etype (Expr))
1856 or else Is_Dynamically_Tagged (Expr))
1857 and then Is_Tagged_Type (Typ)
1858 and then not Is_Class_Wide_Type (Typ)
1859 then
1860 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
1861 end if;
1862 end Check_Dynamically_Tagged_Expression;
1864 --------------------------
1865 -- Check_Fully_Declared --
1866 --------------------------
1868 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
1869 begin
1870 if Ekind (T) = E_Incomplete_Type then
1872 -- Ada 2005 (AI-50217): If the type is available through a limited
1873 -- with_clause, verify that its full view has been analyzed.
1875 if From_Limited_With (T)
1876 and then Present (Non_Limited_View (T))
1877 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
1878 then
1879 -- The non-limited view is fully declared
1881 null;
1883 else
1884 Error_Msg_NE
1885 ("premature usage of incomplete}", N, First_Subtype (T));
1886 end if;
1888 -- Need comments for these tests ???
1890 elsif Has_Private_Component (T)
1891 and then not Is_Generic_Type (Root_Type (T))
1892 and then not In_Spec_Expression
1893 then
1894 -- Special case: if T is the anonymous type created for a single
1895 -- task or protected object, use the name of the source object.
1897 if Is_Concurrent_Type (T)
1898 and then not Comes_From_Source (T)
1899 and then Nkind (N) = N_Object_Declaration
1900 then
1901 Error_Msg_NE
1902 ("type of& has incomplete component",
1903 N, Defining_Identifier (N));
1904 else
1905 Error_Msg_NE
1906 ("premature usage of incomplete}",
1907 N, First_Subtype (T));
1908 end if;
1909 end if;
1910 end Check_Fully_Declared;
1912 -------------------------------------------
1913 -- Check_Function_With_Address_Parameter --
1914 -------------------------------------------
1916 procedure Check_Function_With_Address_Parameter (Subp_Id : Entity_Id) is
1917 F : Entity_Id;
1918 T : Entity_Id;
1920 begin
1921 F := First_Formal (Subp_Id);
1922 while Present (F) loop
1923 T := Etype (F);
1925 if Is_Private_Type (T) and then Present (Full_View (T)) then
1926 T := Full_View (T);
1927 end if;
1929 if Is_Descendant_Of_Address (T) or else Is_Limited_Type (T) then
1930 Set_Is_Pure (Subp_Id, False);
1931 exit;
1932 end if;
1934 Next_Formal (F);
1935 end loop;
1936 end Check_Function_With_Address_Parameter;
1938 -------------------------------------
1939 -- Check_Function_Writable_Actuals --
1940 -------------------------------------
1942 procedure Check_Function_Writable_Actuals (N : Node_Id) is
1943 Writable_Actuals_List : Elist_Id := No_Elist;
1944 Identifiers_List : Elist_Id := No_Elist;
1945 Aggr_Error_Node : Node_Id := Empty;
1946 Error_Node : Node_Id := Empty;
1948 procedure Collect_Identifiers (N : Node_Id);
1949 -- In a single traversal of subtree N collect in Writable_Actuals_List
1950 -- all the actuals of functions with writable actuals, and in the list
1951 -- Identifiers_List collect all the identifiers that are not actuals of
1952 -- functions with writable actuals. If a writable actual is referenced
1953 -- twice as writable actual then Error_Node is set to reference its
1954 -- second occurrence, the error is reported, and the tree traversal
1955 -- is abandoned.
1957 function Get_Function_Id (Call : Node_Id) return Entity_Id;
1958 -- Return the entity associated with the function call
1960 procedure Preanalyze_Without_Errors (N : Node_Id);
1961 -- Preanalyze N without reporting errors. Very dubious, you can't just
1962 -- go analyzing things more than once???
1964 -------------------------
1965 -- Collect_Identifiers --
1966 -------------------------
1968 procedure Collect_Identifiers (N : Node_Id) is
1970 function Check_Node (N : Node_Id) return Traverse_Result;
1971 -- Process a single node during the tree traversal to collect the
1972 -- writable actuals of functions and all the identifiers which are
1973 -- not writable actuals of functions.
1975 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
1976 -- Returns True if List has a node whose Entity is Entity (N)
1978 -------------------------
1979 -- Check_Function_Call --
1980 -------------------------
1982 function Check_Node (N : Node_Id) return Traverse_Result is
1983 Is_Writable_Actual : Boolean := False;
1984 Id : Entity_Id;
1986 begin
1987 if Nkind (N) = N_Identifier then
1989 -- No analysis possible if the entity is not decorated
1991 if No (Entity (N)) then
1992 return Skip;
1994 -- Don't collect identifiers of packages, called functions, etc
1996 elsif Ekind_In (Entity (N), E_Package,
1997 E_Function,
1998 E_Procedure,
1999 E_Entry)
2000 then
2001 return Skip;
2003 -- For rewritten nodes, continue the traversal in the original
2004 -- subtree. Needed to handle aggregates in original expressions
2005 -- extracted from the tree by Remove_Side_Effects.
2007 elsif Is_Rewrite_Substitution (N) then
2008 Collect_Identifiers (Original_Node (N));
2009 return Skip;
2011 -- For now we skip aggregate discriminants, since they require
2012 -- performing the analysis in two phases to identify conflicts:
2013 -- first one analyzing discriminants and second one analyzing
2014 -- the rest of components (since at run time, discriminants are
2015 -- evaluated prior to components): too much computation cost
2016 -- to identify a corner case???
2018 elsif Nkind (Parent (N)) = N_Component_Association
2019 and then Nkind_In (Parent (Parent (N)),
2020 N_Aggregate,
2021 N_Extension_Aggregate)
2022 then
2023 declare
2024 Choice : constant Node_Id := First (Choices (Parent (N)));
2026 begin
2027 if Ekind (Entity (N)) = E_Discriminant then
2028 return Skip;
2030 elsif Expression (Parent (N)) = N
2031 and then Nkind (Choice) = N_Identifier
2032 and then Ekind (Entity (Choice)) = E_Discriminant
2033 then
2034 return Skip;
2035 end if;
2036 end;
2038 -- Analyze if N is a writable actual of a function
2040 elsif Nkind (Parent (N)) = N_Function_Call then
2041 declare
2042 Call : constant Node_Id := Parent (N);
2043 Actual : Node_Id;
2044 Formal : Node_Id;
2046 begin
2047 Id := Get_Function_Id (Call);
2049 -- In case of previous error, no check is possible
2051 if No (Id) then
2052 return Abandon;
2053 end if;
2055 if Ekind_In (Id, E_Function, E_Generic_Function)
2056 and then Has_Out_Or_In_Out_Parameter (Id)
2057 then
2058 Formal := First_Formal (Id);
2059 Actual := First_Actual (Call);
2060 while Present (Actual) and then Present (Formal) loop
2061 if Actual = N then
2062 if Ekind_In (Formal, E_Out_Parameter,
2063 E_In_Out_Parameter)
2064 then
2065 Is_Writable_Actual := True;
2066 end if;
2068 exit;
2069 end if;
2071 Next_Formal (Formal);
2072 Next_Actual (Actual);
2073 end loop;
2074 end if;
2075 end;
2076 end if;
2078 if Is_Writable_Actual then
2080 -- Skip checking the error in non-elementary types since
2081 -- RM 6.4.1(6.15/3) is restricted to elementary types, but
2082 -- store this actual in Writable_Actuals_List since it is
2083 -- needed to perform checks on other constructs that have
2084 -- arbitrary order of evaluation (for example, aggregates).
2086 if not Is_Elementary_Type (Etype (N)) then
2087 if not Contains (Writable_Actuals_List, N) then
2088 Append_New_Elmt (N, To => Writable_Actuals_List);
2089 end if;
2091 -- Second occurrence of an elementary type writable actual
2093 elsif Contains (Writable_Actuals_List, N) then
2095 -- Report the error on the second occurrence of the
2096 -- identifier. We cannot assume that N is the second
2097 -- occurrence (according to their location in the
2098 -- sources), since Traverse_Func walks through Field2
2099 -- last (see comment in the body of Traverse_Func).
2101 declare
2102 Elmt : Elmt_Id;
2104 begin
2105 Elmt := First_Elmt (Writable_Actuals_List);
2106 while Present (Elmt)
2107 and then Entity (Node (Elmt)) /= Entity (N)
2108 loop
2109 Next_Elmt (Elmt);
2110 end loop;
2112 if Sloc (N) > Sloc (Node (Elmt)) then
2113 Error_Node := N;
2114 else
2115 Error_Node := Node (Elmt);
2116 end if;
2118 Error_Msg_NE
2119 ("value may be affected by call to & "
2120 & "because order of evaluation is arbitrary",
2121 Error_Node, Id);
2122 return Abandon;
2123 end;
2125 -- First occurrence of a elementary type writable actual
2127 else
2128 Append_New_Elmt (N, To => Writable_Actuals_List);
2129 end if;
2131 else
2132 if Identifiers_List = No_Elist then
2133 Identifiers_List := New_Elmt_List;
2134 end if;
2136 Append_Unique_Elmt (N, Identifiers_List);
2137 end if;
2138 end if;
2140 return OK;
2141 end Check_Node;
2143 --------------
2144 -- Contains --
2145 --------------
2147 function Contains
2148 (List : Elist_Id;
2149 N : Node_Id) return Boolean
2151 pragma Assert (Nkind (N) in N_Has_Entity);
2153 Elmt : Elmt_Id;
2155 begin
2156 if List = No_Elist then
2157 return False;
2158 end if;
2160 Elmt := First_Elmt (List);
2161 while Present (Elmt) loop
2162 if Entity (Node (Elmt)) = Entity (N) then
2163 return True;
2164 else
2165 Next_Elmt (Elmt);
2166 end if;
2167 end loop;
2169 return False;
2170 end Contains;
2172 ------------------
2173 -- Do_Traversal --
2174 ------------------
2176 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2177 -- The traversal procedure
2179 -- Start of processing for Collect_Identifiers
2181 begin
2182 if Present (Error_Node) then
2183 return;
2184 end if;
2186 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2187 return;
2188 end if;
2190 Do_Traversal (N);
2191 end Collect_Identifiers;
2193 ---------------------
2194 -- Get_Function_Id --
2195 ---------------------
2197 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2198 Nam : constant Node_Id := Name (Call);
2199 Id : Entity_Id;
2201 begin
2202 if Nkind (Nam) = N_Explicit_Dereference then
2203 Id := Etype (Nam);
2204 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2206 elsif Nkind (Nam) = N_Selected_Component then
2207 Id := Entity (Selector_Name (Nam));
2209 elsif Nkind (Nam) = N_Indexed_Component then
2210 Id := Entity (Selector_Name (Prefix (Nam)));
2212 else
2213 Id := Entity (Nam);
2214 end if;
2216 return Id;
2217 end Get_Function_Id;
2219 -------------------------------
2220 -- Preanalyze_Without_Errors --
2221 -------------------------------
2223 procedure Preanalyze_Without_Errors (N : Node_Id) is
2224 Status : constant Boolean := Get_Ignore_Errors;
2225 begin
2226 Set_Ignore_Errors (True);
2227 Preanalyze (N);
2228 Set_Ignore_Errors (Status);
2229 end Preanalyze_Without_Errors;
2231 -- Start of processing for Check_Function_Writable_Actuals
2233 begin
2234 -- The check only applies to Ada 2012 code on which Check_Actuals has
2235 -- been set, and only to constructs that have multiple constituents
2236 -- whose order of evaluation is not specified by the language.
2238 if Ada_Version < Ada_2012
2239 or else not Check_Actuals (N)
2240 or else (not (Nkind (N) in N_Op)
2241 and then not (Nkind (N) in N_Membership_Test)
2242 and then not Nkind_In (N, N_Range,
2243 N_Aggregate,
2244 N_Extension_Aggregate,
2245 N_Full_Type_Declaration,
2246 N_Function_Call,
2247 N_Procedure_Call_Statement,
2248 N_Entry_Call_Statement))
2249 or else (Nkind (N) = N_Full_Type_Declaration
2250 and then not Is_Record_Type (Defining_Identifier (N)))
2252 -- In addition, this check only applies to source code, not to code
2253 -- generated by constraint checks.
2255 or else not Comes_From_Source (N)
2256 then
2257 return;
2258 end if;
2260 -- If a construct C has two or more direct constituents that are names
2261 -- or expressions whose evaluation may occur in an arbitrary order, at
2262 -- least one of which contains a function call with an in out or out
2263 -- parameter, then the construct is legal only if: for each name N that
2264 -- is passed as a parameter of mode in out or out to some inner function
2265 -- call C2 (not including the construct C itself), there is no other
2266 -- name anywhere within a direct constituent of the construct C other
2267 -- than the one containing C2, that is known to refer to the same
2268 -- object (RM 6.4.1(6.17/3)).
2270 case Nkind (N) is
2271 when N_Range =>
2272 Collect_Identifiers (Low_Bound (N));
2273 Collect_Identifiers (High_Bound (N));
2275 when N_Op | N_Membership_Test =>
2276 declare
2277 Expr : Node_Id;
2279 begin
2280 Collect_Identifiers (Left_Opnd (N));
2282 if Present (Right_Opnd (N)) then
2283 Collect_Identifiers (Right_Opnd (N));
2284 end if;
2286 if Nkind_In (N, N_In, N_Not_In)
2287 and then Present (Alternatives (N))
2288 then
2289 Expr := First (Alternatives (N));
2290 while Present (Expr) loop
2291 Collect_Identifiers (Expr);
2293 Next (Expr);
2294 end loop;
2295 end if;
2296 end;
2298 when N_Full_Type_Declaration =>
2299 declare
2300 function Get_Record_Part (N : Node_Id) return Node_Id;
2301 -- Return the record part of this record type definition
2303 function Get_Record_Part (N : Node_Id) return Node_Id is
2304 Type_Def : constant Node_Id := Type_Definition (N);
2305 begin
2306 if Nkind (Type_Def) = N_Derived_Type_Definition then
2307 return Record_Extension_Part (Type_Def);
2308 else
2309 return Type_Def;
2310 end if;
2311 end Get_Record_Part;
2313 Comp : Node_Id;
2314 Def_Id : Entity_Id := Defining_Identifier (N);
2315 Rec : Node_Id := Get_Record_Part (N);
2317 begin
2318 -- No need to perform any analysis if the record has no
2319 -- components
2321 if No (Rec) or else No (Component_List (Rec)) then
2322 return;
2323 end if;
2325 -- Collect the identifiers starting from the deepest
2326 -- derivation. Done to report the error in the deepest
2327 -- derivation.
2329 loop
2330 if Present (Component_List (Rec)) then
2331 Comp := First (Component_Items (Component_List (Rec)));
2332 while Present (Comp) loop
2333 if Nkind (Comp) = N_Component_Declaration
2334 and then Present (Expression (Comp))
2335 then
2336 Collect_Identifiers (Expression (Comp));
2337 end if;
2339 Next (Comp);
2340 end loop;
2341 end if;
2343 exit when No (Underlying_Type (Etype (Def_Id)))
2344 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2345 = Def_Id;
2347 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2348 Rec := Get_Record_Part (Parent (Def_Id));
2349 end loop;
2350 end;
2352 when N_Subprogram_Call |
2353 N_Entry_Call_Statement =>
2354 declare
2355 Id : constant Entity_Id := Get_Function_Id (N);
2356 Formal : Node_Id;
2357 Actual : Node_Id;
2359 begin
2360 Formal := First_Formal (Id);
2361 Actual := First_Actual (N);
2362 while Present (Actual) and then Present (Formal) loop
2363 if Ekind_In (Formal, E_Out_Parameter,
2364 E_In_Out_Parameter)
2365 then
2366 Collect_Identifiers (Actual);
2367 end if;
2369 Next_Formal (Formal);
2370 Next_Actual (Actual);
2371 end loop;
2372 end;
2374 when N_Aggregate |
2375 N_Extension_Aggregate =>
2376 declare
2377 Assoc : Node_Id;
2378 Choice : Node_Id;
2379 Comp_Expr : Node_Id;
2381 begin
2382 -- Handle the N_Others_Choice of array aggregates with static
2383 -- bounds. There is no need to perform this analysis in
2384 -- aggregates without static bounds since we cannot evaluate
2385 -- if the N_Others_Choice covers several elements. There is
2386 -- no need to handle the N_Others choice of record aggregates
2387 -- since at this stage it has been already expanded by
2388 -- Resolve_Record_Aggregate.
2390 if Is_Array_Type (Etype (N))
2391 and then Nkind (N) = N_Aggregate
2392 and then Present (Aggregate_Bounds (N))
2393 and then Compile_Time_Known_Bounds (Etype (N))
2394 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2396 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2397 then
2398 declare
2399 Count_Components : Uint := Uint_0;
2400 Num_Components : Uint;
2401 Others_Assoc : Node_Id;
2402 Others_Choice : Node_Id := Empty;
2403 Others_Box_Present : Boolean := False;
2405 begin
2406 -- Count positional associations
2408 if Present (Expressions (N)) then
2409 Comp_Expr := First (Expressions (N));
2410 while Present (Comp_Expr) loop
2411 Count_Components := Count_Components + 1;
2412 Next (Comp_Expr);
2413 end loop;
2414 end if;
2416 -- Count the rest of elements and locate the N_Others
2417 -- choice (if any)
2419 Assoc := First (Component_Associations (N));
2420 while Present (Assoc) loop
2421 Choice := First (Choices (Assoc));
2422 while Present (Choice) loop
2423 if Nkind (Choice) = N_Others_Choice then
2424 Others_Assoc := Assoc;
2425 Others_Choice := Choice;
2426 Others_Box_Present := Box_Present (Assoc);
2428 -- Count several components
2430 elsif Nkind_In (Choice, N_Range,
2431 N_Subtype_Indication)
2432 or else (Is_Entity_Name (Choice)
2433 and then Is_Type (Entity (Choice)))
2434 then
2435 declare
2436 L, H : Node_Id;
2437 begin
2438 Get_Index_Bounds (Choice, L, H);
2439 pragma Assert
2440 (Compile_Time_Known_Value (L)
2441 and then Compile_Time_Known_Value (H));
2442 Count_Components :=
2443 Count_Components
2444 + Expr_Value (H) - Expr_Value (L) + 1;
2445 end;
2447 -- Count single component. No other case available
2448 -- since we are handling an aggregate with static
2449 -- bounds.
2451 else
2452 pragma Assert (Is_OK_Static_Expression (Choice)
2453 or else Nkind (Choice) = N_Identifier
2454 or else Nkind (Choice) = N_Integer_Literal);
2456 Count_Components := Count_Components + 1;
2457 end if;
2459 Next (Choice);
2460 end loop;
2462 Next (Assoc);
2463 end loop;
2465 Num_Components :=
2466 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2467 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2469 pragma Assert (Count_Components <= Num_Components);
2471 -- Handle the N_Others choice if it covers several
2472 -- components
2474 if Present (Others_Choice)
2475 and then (Num_Components - Count_Components) > 1
2476 then
2477 if not Others_Box_Present then
2479 -- At this stage, if expansion is active, the
2480 -- expression of the others choice has not been
2481 -- analyzed. Hence we generate a duplicate and
2482 -- we analyze it silently to have available the
2483 -- minimum decoration required to collect the
2484 -- identifiers.
2486 if not Expander_Active then
2487 Comp_Expr := Expression (Others_Assoc);
2488 else
2489 Comp_Expr :=
2490 New_Copy_Tree (Expression (Others_Assoc));
2491 Preanalyze_Without_Errors (Comp_Expr);
2492 end if;
2494 Collect_Identifiers (Comp_Expr);
2496 if Writable_Actuals_List /= No_Elist then
2498 -- As suggested by Robert, at current stage we
2499 -- report occurrences of this case as warnings.
2501 Error_Msg_N
2502 ("writable function parameter may affect "
2503 & "value in other component because order "
2504 & "of evaluation is unspecified??",
2505 Node (First_Elmt (Writable_Actuals_List)));
2506 end if;
2507 end if;
2508 end if;
2509 end;
2511 -- For an array aggregate, a discrete_choice_list that has
2512 -- a nonstatic range is considered as two or more separate
2513 -- occurrences of the expression (RM 6.4.1(20/3)).
2515 elsif Is_Array_Type (Etype (N))
2516 and then Nkind (N) = N_Aggregate
2517 and then Present (Aggregate_Bounds (N))
2518 and then not Compile_Time_Known_Bounds (Etype (N))
2519 then
2520 -- Collect identifiers found in the dynamic bounds
2522 declare
2523 Count_Components : Natural := 0;
2524 Low, High : Node_Id;
2526 begin
2527 Assoc := First (Component_Associations (N));
2528 while Present (Assoc) loop
2529 Choice := First (Choices (Assoc));
2530 while Present (Choice) loop
2531 if Nkind_In (Choice, N_Range,
2532 N_Subtype_Indication)
2533 or else (Is_Entity_Name (Choice)
2534 and then Is_Type (Entity (Choice)))
2535 then
2536 Get_Index_Bounds (Choice, Low, High);
2538 if not Compile_Time_Known_Value (Low) then
2539 Collect_Identifiers (Low);
2541 if No (Aggr_Error_Node) then
2542 Aggr_Error_Node := Low;
2543 end if;
2544 end if;
2546 if not Compile_Time_Known_Value (High) then
2547 Collect_Identifiers (High);
2549 if No (Aggr_Error_Node) then
2550 Aggr_Error_Node := High;
2551 end if;
2552 end if;
2554 -- The RM rule is violated if there is more than
2555 -- a single choice in a component association.
2557 else
2558 Count_Components := Count_Components + 1;
2560 if No (Aggr_Error_Node)
2561 and then Count_Components > 1
2562 then
2563 Aggr_Error_Node := Choice;
2564 end if;
2566 if not Compile_Time_Known_Value (Choice) then
2567 Collect_Identifiers (Choice);
2568 end if;
2569 end if;
2571 Next (Choice);
2572 end loop;
2574 Next (Assoc);
2575 end loop;
2576 end;
2577 end if;
2579 -- Handle ancestor part of extension aggregates
2581 if Nkind (N) = N_Extension_Aggregate then
2582 Collect_Identifiers (Ancestor_Part (N));
2583 end if;
2585 -- Handle positional associations
2587 if Present (Expressions (N)) then
2588 Comp_Expr := First (Expressions (N));
2589 while Present (Comp_Expr) loop
2590 if not Is_OK_Static_Expression (Comp_Expr) then
2591 Collect_Identifiers (Comp_Expr);
2592 end if;
2594 Next (Comp_Expr);
2595 end loop;
2596 end if;
2598 -- Handle discrete associations
2600 if Present (Component_Associations (N)) then
2601 Assoc := First (Component_Associations (N));
2602 while Present (Assoc) loop
2604 if not Box_Present (Assoc) then
2605 Choice := First (Choices (Assoc));
2606 while Present (Choice) loop
2608 -- For now we skip discriminants since it requires
2609 -- performing the analysis in two phases: first one
2610 -- analyzing discriminants and second one analyzing
2611 -- the rest of components since discriminants are
2612 -- evaluated prior to components: too much extra
2613 -- work to detect a corner case???
2615 if Nkind (Choice) in N_Has_Entity
2616 and then Present (Entity (Choice))
2617 and then Ekind (Entity (Choice)) = E_Discriminant
2618 then
2619 null;
2621 elsif Box_Present (Assoc) then
2622 null;
2624 else
2625 if not Analyzed (Expression (Assoc)) then
2626 Comp_Expr :=
2627 New_Copy_Tree (Expression (Assoc));
2628 Set_Parent (Comp_Expr, Parent (N));
2629 Preanalyze_Without_Errors (Comp_Expr);
2630 else
2631 Comp_Expr := Expression (Assoc);
2632 end if;
2634 Collect_Identifiers (Comp_Expr);
2635 end if;
2637 Next (Choice);
2638 end loop;
2639 end if;
2641 Next (Assoc);
2642 end loop;
2643 end if;
2644 end;
2646 when others =>
2647 return;
2648 end case;
2650 -- No further action needed if we already reported an error
2652 if Present (Error_Node) then
2653 return;
2654 end if;
2656 -- Check violation of RM 6.20/3 in aggregates
2658 if Present (Aggr_Error_Node)
2659 and then Writable_Actuals_List /= No_Elist
2660 then
2661 Error_Msg_N
2662 ("value may be affected by call in other component because they "
2663 & "are evaluated in unspecified order",
2664 Node (First_Elmt (Writable_Actuals_List)));
2665 return;
2666 end if;
2668 -- Check if some writable argument of a function is referenced
2670 if Writable_Actuals_List /= No_Elist
2671 and then Identifiers_List /= No_Elist
2672 then
2673 declare
2674 Elmt_1 : Elmt_Id;
2675 Elmt_2 : Elmt_Id;
2677 begin
2678 Elmt_1 := First_Elmt (Writable_Actuals_List);
2679 while Present (Elmt_1) loop
2680 Elmt_2 := First_Elmt (Identifiers_List);
2681 while Present (Elmt_2) loop
2682 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2683 case Nkind (Parent (Node (Elmt_2))) is
2684 when N_Aggregate |
2685 N_Component_Association |
2686 N_Component_Declaration =>
2687 Error_Msg_N
2688 ("value may be affected by call in other "
2689 & "component because they are evaluated "
2690 & "in unspecified order",
2691 Node (Elmt_2));
2693 when N_In | N_Not_In =>
2694 Error_Msg_N
2695 ("value may be affected by call in other "
2696 & "alternative because they are evaluated "
2697 & "in unspecified order",
2698 Node (Elmt_2));
2700 when others =>
2701 Error_Msg_N
2702 ("value of actual may be affected by call in "
2703 & "other actual because they are evaluated "
2704 & "in unspecified order",
2705 Node (Elmt_2));
2706 end case;
2707 end if;
2709 Next_Elmt (Elmt_2);
2710 end loop;
2712 Next_Elmt (Elmt_1);
2713 end loop;
2714 end;
2715 end if;
2716 end Check_Function_Writable_Actuals;
2718 --------------------------------
2719 -- Check_Implicit_Dereference --
2720 --------------------------------
2722 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2723 Disc : Entity_Id;
2724 Desig : Entity_Id;
2725 Nam : Node_Id;
2727 begin
2728 if Nkind (N) = N_Indexed_Component
2729 and then Present (Generalized_Indexing (N))
2730 then
2731 Nam := Generalized_Indexing (N);
2732 else
2733 Nam := N;
2734 end if;
2736 if Ada_Version < Ada_2012
2737 or else not Has_Implicit_Dereference (Base_Type (Typ))
2738 then
2739 return;
2741 elsif not Comes_From_Source (N)
2742 and then Nkind (N) /= N_Indexed_Component
2743 then
2744 return;
2746 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2747 null;
2749 else
2750 Disc := First_Discriminant (Typ);
2751 while Present (Disc) loop
2752 if Has_Implicit_Dereference (Disc) then
2753 Desig := Designated_Type (Etype (Disc));
2754 Add_One_Interp (Nam, Disc, Desig);
2756 -- If the node is a generalized indexing, add interpretation
2757 -- to that node as well, for subsequent resolution.
2759 if Nkind (N) = N_Indexed_Component then
2760 Add_One_Interp (N, Disc, Desig);
2761 end if;
2763 -- If the operation comes from a generic unit and the context
2764 -- is a selected component, the selector name may be global
2765 -- and set in the instance already. Remove the entity to
2766 -- force resolution of the selected component, and the
2767 -- generation of an explicit dereference if needed.
2769 if In_Instance
2770 and then Nkind (Parent (Nam)) = N_Selected_Component
2771 then
2772 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2773 end if;
2775 exit;
2776 end if;
2778 Next_Discriminant (Disc);
2779 end loop;
2780 end if;
2781 end Check_Implicit_Dereference;
2783 ----------------------------------
2784 -- Check_Internal_Protected_Use --
2785 ----------------------------------
2787 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2788 S : Entity_Id;
2789 Prot : Entity_Id;
2791 begin
2792 S := Current_Scope;
2793 while Present (S) loop
2794 if S = Standard_Standard then
2795 return;
2797 elsif Ekind (S) = E_Function
2798 and then Ekind (Scope (S)) = E_Protected_Type
2799 then
2800 Prot := Scope (S);
2801 exit;
2802 end if;
2804 S := Scope (S);
2805 end loop;
2807 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2809 -- An indirect function call (e.g. a callback within a protected
2810 -- function body) is not statically illegal. If the access type is
2811 -- anonymous and is the type of an access parameter, the scope of Nam
2812 -- will be the protected type, but it is not a protected operation.
2814 if Ekind (Nam) = E_Subprogram_Type
2815 and then
2816 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2817 then
2818 null;
2820 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2821 Error_Msg_N
2822 ("within protected function cannot use protected "
2823 & "procedure in renaming or as generic actual", N);
2825 elsif Nkind (N) = N_Attribute_Reference then
2826 Error_Msg_N
2827 ("within protected function cannot take access of "
2828 & " protected procedure", N);
2830 else
2831 Error_Msg_N
2832 ("within protected function, protected object is constant", N);
2833 Error_Msg_N
2834 ("\cannot call operation that may modify it", N);
2835 end if;
2836 end if;
2837 end Check_Internal_Protected_Use;
2839 ---------------------------------------
2840 -- Check_Later_Vs_Basic_Declarations --
2841 ---------------------------------------
2843 procedure Check_Later_Vs_Basic_Declarations
2844 (Decls : List_Id;
2845 During_Parsing : Boolean)
2847 Body_Sloc : Source_Ptr;
2848 Decl : Node_Id;
2850 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2851 -- Return whether Decl is considered as a declarative item.
2852 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2853 -- When During_Parsing is False, the semantics of SPARK is followed.
2855 -------------------------------
2856 -- Is_Later_Declarative_Item --
2857 -------------------------------
2859 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2860 begin
2861 if Nkind (Decl) in N_Later_Decl_Item then
2862 return True;
2864 elsif Nkind (Decl) = N_Pragma then
2865 return True;
2867 elsif During_Parsing then
2868 return False;
2870 -- In SPARK, a package declaration is not considered as a later
2871 -- declarative item.
2873 elsif Nkind (Decl) = N_Package_Declaration then
2874 return False;
2876 -- In SPARK, a renaming is considered as a later declarative item
2878 elsif Nkind (Decl) in N_Renaming_Declaration then
2879 return True;
2881 else
2882 return False;
2883 end if;
2884 end Is_Later_Declarative_Item;
2886 -- Start of processing for Check_Later_Vs_Basic_Declarations
2888 begin
2889 Decl := First (Decls);
2891 -- Loop through sequence of basic declarative items
2893 Outer : while Present (Decl) loop
2894 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
2895 and then Nkind (Decl) not in N_Body_Stub
2896 then
2897 Next (Decl);
2899 -- Once a body is encountered, we only allow later declarative
2900 -- items. The inner loop checks the rest of the list.
2902 else
2903 Body_Sloc := Sloc (Decl);
2905 Inner : while Present (Decl) loop
2906 if not Is_Later_Declarative_Item (Decl) then
2907 if During_Parsing then
2908 if Ada_Version = Ada_83 then
2909 Error_Msg_Sloc := Body_Sloc;
2910 Error_Msg_N
2911 ("(Ada 83) decl cannot appear after body#", Decl);
2912 end if;
2913 else
2914 Error_Msg_Sloc := Body_Sloc;
2915 Check_SPARK_05_Restriction
2916 ("decl cannot appear after body#", Decl);
2917 end if;
2918 end if;
2920 Next (Decl);
2921 end loop Inner;
2922 end if;
2923 end loop Outer;
2924 end Check_Later_Vs_Basic_Declarations;
2926 ---------------------------
2927 -- Check_No_Hidden_State --
2928 ---------------------------
2930 procedure Check_No_Hidden_State (Id : Entity_Id) is
2931 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
2932 -- Determine whether the entity of a package denoted by Pkg has a null
2933 -- abstract state.
2935 -----------------------------
2936 -- Has_Null_Abstract_State --
2937 -----------------------------
2939 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
2940 States : constant Elist_Id := Abstract_States (Pkg);
2942 begin
2943 -- Check first available state of related package. A null abstract
2944 -- state always appears as the sole element of the state list.
2946 return
2947 Present (States)
2948 and then Is_Null_State (Node (First_Elmt (States)));
2949 end Has_Null_Abstract_State;
2951 -- Local variables
2953 Context : Entity_Id := Empty;
2954 Not_Visible : Boolean := False;
2955 Scop : Entity_Id;
2957 -- Start of processing for Check_No_Hidden_State
2959 begin
2960 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
2962 -- Find the proper context where the object or state appears
2964 Scop := Scope (Id);
2965 while Present (Scop) loop
2966 Context := Scop;
2968 -- Keep track of the context's visibility
2970 Not_Visible := Not_Visible or else In_Private_Part (Context);
2972 -- Prevent the search from going too far
2974 if Context = Standard_Standard then
2975 return;
2977 -- Objects and states that appear immediately within a subprogram or
2978 -- inside a construct nested within a subprogram do not introduce a
2979 -- hidden state. They behave as local variable declarations.
2981 elsif Is_Subprogram (Context) then
2982 return;
2984 -- When examining a package body, use the entity of the spec as it
2985 -- carries the abstract state declarations.
2987 elsif Ekind (Context) = E_Package_Body then
2988 Context := Spec_Entity (Context);
2989 end if;
2991 -- Stop the traversal when a package subject to a null abstract state
2992 -- has been found.
2994 if Ekind_In (Context, E_Generic_Package, E_Package)
2995 and then Has_Null_Abstract_State (Context)
2996 then
2997 exit;
2998 end if;
3000 Scop := Scope (Scop);
3001 end loop;
3003 -- At this point we know that there is at least one package with a null
3004 -- abstract state in visibility. Emit an error message unconditionally
3005 -- if the entity being processed is a state because the placement of the
3006 -- related package is irrelevant. This is not the case for objects as
3007 -- the intermediate context matters.
3009 if Present (Context)
3010 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3011 then
3012 Error_Msg_N ("cannot introduce hidden state &", Id);
3013 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3014 end if;
3015 end Check_No_Hidden_State;
3017 ----------------------------------------
3018 -- Check_Nonvolatile_Function_Profile --
3019 ----------------------------------------
3021 procedure Check_Nonvolatile_Function_Profile (Func_Id : Entity_Id) is
3022 Formal : Entity_Id;
3024 begin
3025 -- Inspect all formal parameters
3027 Formal := First_Formal (Func_Id);
3028 while Present (Formal) loop
3029 if Is_Effectively_Volatile (Etype (Formal)) then
3030 Error_Msg_NE
3031 ("nonvolatile function & cannot have a volatile parameter",
3032 Formal, Func_Id);
3033 end if;
3035 Next_Formal (Formal);
3036 end loop;
3038 -- Inspect the return type
3040 if Is_Effectively_Volatile (Etype (Func_Id)) then
3041 Error_Msg_NE
3042 ("nonvolatile function & cannot have a volatile return type",
3043 Result_Definition (Parent (Func_Id)), Func_Id);
3044 end if;
3045 end Check_Nonvolatile_Function_Profile;
3047 ------------------------------------------
3048 -- Check_Potentially_Blocking_Operation --
3049 ------------------------------------------
3051 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3052 S : Entity_Id;
3054 begin
3055 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3056 -- When pragma Detect_Blocking is active, the run time will raise
3057 -- Program_Error. Here we only issue a warning, since we generally
3058 -- support the use of potentially blocking operations in the absence
3059 -- of the pragma.
3061 -- Indirect blocking through a subprogram call cannot be diagnosed
3062 -- statically without interprocedural analysis, so we do not attempt
3063 -- to do it here.
3065 S := Scope (Current_Scope);
3066 while Present (S) and then S /= Standard_Standard loop
3067 if Is_Protected_Type (S) then
3068 Error_Msg_N
3069 ("potentially blocking operation in protected operation??", N);
3070 return;
3071 end if;
3073 S := Scope (S);
3074 end loop;
3075 end Check_Potentially_Blocking_Operation;
3077 ---------------------------------
3078 -- Check_Result_And_Post_State --
3079 ---------------------------------
3081 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3082 procedure Check_Result_And_Post_State_In_Pragma
3083 (Prag : Node_Id;
3084 Result_Seen : in out Boolean);
3085 -- Determine whether pragma Prag mentions attribute 'Result and whether
3086 -- the pragma contains an expression that evaluates differently in pre-
3087 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3088 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3090 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3091 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3092 -- formal parameter.
3094 -------------------------------------------
3095 -- Check_Result_And_Post_State_In_Pragma --
3096 -------------------------------------------
3098 procedure Check_Result_And_Post_State_In_Pragma
3099 (Prag : Node_Id;
3100 Result_Seen : in out Boolean)
3102 procedure Check_Expression (Expr : Node_Id);
3103 -- Perform the 'Result and post-state checks on a given expression
3105 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3106 -- Attempt to find attribute 'Result in a subtree denoted by N
3108 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3109 -- Determine whether source node N denotes "True" or "False"
3111 function Mentions_Post_State (N : Node_Id) return Boolean;
3112 -- Determine whether a subtree denoted by N mentions any construct
3113 -- that denotes a post-state.
3115 procedure Check_Function_Result is
3116 new Traverse_Proc (Is_Function_Result);
3118 ----------------------
3119 -- Check_Expression --
3120 ----------------------
3122 procedure Check_Expression (Expr : Node_Id) is
3123 begin
3124 if not Is_Trivial_Boolean (Expr) then
3125 Check_Function_Result (Expr);
3127 if not Mentions_Post_State (Expr) then
3128 if Pragma_Name_Mapped (Prag) = Name_Contract_Cases then
3129 Error_Msg_NE
3130 ("contract case does not check the outcome of calling "
3131 & "&?T?", Expr, Subp_Id);
3133 elsif Pragma_Name_Mapped (Prag) = Name_Refined_Post then
3134 Error_Msg_NE
3135 ("refined postcondition does not check the outcome of "
3136 & "calling &?T?", Prag, Subp_Id);
3138 else
3139 Error_Msg_NE
3140 ("postcondition does not check the outcome of calling "
3141 & "&?T?", Prag, Subp_Id);
3142 end if;
3143 end if;
3144 end if;
3145 end Check_Expression;
3147 ------------------------
3148 -- Is_Function_Result --
3149 ------------------------
3151 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3152 begin
3153 if Is_Attribute_Result (N) then
3154 Result_Seen := True;
3155 return Abandon;
3157 -- Continue the traversal
3159 else
3160 return OK;
3161 end if;
3162 end Is_Function_Result;
3164 ------------------------
3165 -- Is_Trivial_Boolean --
3166 ------------------------
3168 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3169 begin
3170 return
3171 Comes_From_Source (N)
3172 and then Is_Entity_Name (N)
3173 and then (Entity (N) = Standard_True
3174 or else
3175 Entity (N) = Standard_False);
3176 end Is_Trivial_Boolean;
3178 -------------------------
3179 -- Mentions_Post_State --
3180 -------------------------
3182 function Mentions_Post_State (N : Node_Id) return Boolean is
3183 Post_State_Seen : Boolean := False;
3185 function Is_Post_State (N : Node_Id) return Traverse_Result;
3186 -- Attempt to find a construct that denotes a post-state. If this
3187 -- is the case, set flag Post_State_Seen.
3189 -------------------
3190 -- Is_Post_State --
3191 -------------------
3193 function Is_Post_State (N : Node_Id) return Traverse_Result is
3194 Ent : Entity_Id;
3196 begin
3197 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3198 Post_State_Seen := True;
3199 return Abandon;
3201 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3202 Ent := Entity (N);
3204 -- The entity may be modifiable through an implicit
3205 -- dereference.
3207 if No (Ent)
3208 or else Ekind (Ent) in Assignable_Kind
3209 or else (Is_Access_Type (Etype (Ent))
3210 and then Nkind (Parent (N)) =
3211 N_Selected_Component)
3212 then
3213 Post_State_Seen := True;
3214 return Abandon;
3215 end if;
3217 elsif Nkind (N) = N_Attribute_Reference then
3218 if Attribute_Name (N) = Name_Old then
3219 return Skip;
3221 elsif Attribute_Name (N) = Name_Result then
3222 Post_State_Seen := True;
3223 return Abandon;
3224 end if;
3225 end if;
3227 return OK;
3228 end Is_Post_State;
3230 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3232 -- Start of processing for Mentions_Post_State
3234 begin
3235 Find_Post_State (N);
3237 return Post_State_Seen;
3238 end Mentions_Post_State;
3240 -- Local variables
3242 Expr : constant Node_Id :=
3243 Get_Pragma_Arg
3244 (First (Pragma_Argument_Associations (Prag)));
3245 Nam : constant Name_Id := Pragma_Name_Mapped (Prag);
3246 CCase : Node_Id;
3248 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3250 begin
3251 -- Examine all consequences
3253 if Nam = Name_Contract_Cases then
3254 CCase := First (Component_Associations (Expr));
3255 while Present (CCase) loop
3256 Check_Expression (Expression (CCase));
3258 Next (CCase);
3259 end loop;
3261 -- Examine the expression of a postcondition
3263 else pragma Assert (Nam_In (Nam, Name_Postcondition,
3264 Name_Refined_Post));
3265 Check_Expression (Expr);
3266 end if;
3267 end Check_Result_And_Post_State_In_Pragma;
3269 --------------------------
3270 -- Has_In_Out_Parameter --
3271 --------------------------
3273 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
3274 Formal : Entity_Id;
3276 begin
3277 -- Traverse the formals looking for an IN OUT parameter
3279 Formal := First_Formal (Subp_Id);
3280 while Present (Formal) loop
3281 if Ekind (Formal) = E_In_Out_Parameter then
3282 return True;
3283 end if;
3285 Next_Formal (Formal);
3286 end loop;
3288 return False;
3289 end Has_In_Out_Parameter;
3291 -- Local variables
3293 Items : constant Node_Id := Contract (Subp_Id);
3294 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3295 Case_Prag : Node_Id := Empty;
3296 Post_Prag : Node_Id := Empty;
3297 Prag : Node_Id;
3298 Seen_In_Case : Boolean := False;
3299 Seen_In_Post : Boolean := False;
3300 Spec_Id : Entity_Id;
3302 -- Start of processing for Check_Result_And_Post_State
3304 begin
3305 -- The lack of attribute 'Result or a post-state is classified as a
3306 -- suspicious contract. Do not perform the check if the corresponding
3307 -- swich is not set.
3309 if not Warn_On_Suspicious_Contract then
3310 return;
3312 -- Nothing to do if there is no contract
3314 elsif No (Items) then
3315 return;
3316 end if;
3318 -- Retrieve the entity of the subprogram spec (if any)
3320 if Nkind (Subp_Decl) = N_Subprogram_Body
3321 and then Present (Corresponding_Spec (Subp_Decl))
3322 then
3323 Spec_Id := Corresponding_Spec (Subp_Decl);
3325 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
3326 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
3327 then
3328 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
3330 else
3331 Spec_Id := Subp_Id;
3332 end if;
3334 -- Examine all postconditions for attribute 'Result and a post-state
3336 Prag := Pre_Post_Conditions (Items);
3337 while Present (Prag) loop
3338 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
3339 Name_Refined_Post)
3340 and then not Error_Posted (Prag)
3341 then
3342 Post_Prag := Prag;
3343 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
3344 end if;
3346 Prag := Next_Pragma (Prag);
3347 end loop;
3349 -- Examine the contract cases of the subprogram for attribute 'Result
3350 -- and a post-state.
3352 Prag := Contract_Test_Cases (Items);
3353 while Present (Prag) loop
3354 if Pragma_Name_Mapped (Prag) = Name_Contract_Cases
3355 and then not Error_Posted (Prag)
3356 then
3357 Case_Prag := Prag;
3358 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
3359 end if;
3361 Prag := Next_Pragma (Prag);
3362 end loop;
3364 -- Do not emit any errors if the subprogram is not a function
3366 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
3367 null;
3369 -- Regardless of whether the function has postconditions or contract
3370 -- cases, or whether they mention attribute 'Result, an IN OUT formal
3371 -- parameter is always treated as a result.
3373 elsif Has_In_Out_Parameter (Spec_Id) then
3374 null;
3376 -- The function has both a postcondition and contract cases and they do
3377 -- not mention attribute 'Result.
3379 elsif Present (Case_Prag)
3380 and then not Seen_In_Case
3381 and then Present (Post_Prag)
3382 and then not Seen_In_Post
3383 then
3384 Error_Msg_N
3385 ("neither postcondition nor contract cases mention function "
3386 & "result?T?", Post_Prag);
3388 -- The function has contract cases only and they do not mention
3389 -- attribute 'Result.
3391 elsif Present (Case_Prag) and then not Seen_In_Case then
3392 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
3394 -- The function has postconditions only and they do not mention
3395 -- attribute 'Result.
3397 elsif Present (Post_Prag) and then not Seen_In_Post then
3398 Error_Msg_N
3399 ("postcondition does not mention function result?T?", Post_Prag);
3400 end if;
3401 end Check_Result_And_Post_State;
3403 -----------------------------
3404 -- Check_State_Refinements --
3405 -----------------------------
3407 procedure Check_State_Refinements
3408 (Context : Node_Id;
3409 Is_Main_Unit : Boolean := False)
3411 procedure Check_Package (Pack : Node_Id);
3412 -- Verify that all abstract states of a [generic] package denoted by its
3413 -- declarative node Pack have proper refinement. Recursively verify the
3414 -- visible and private declarations of the [generic] package for other
3415 -- nested packages.
3417 procedure Check_Packages_In (Decls : List_Id);
3418 -- Seek out [generic] package declarations within declarative list Decls
3419 -- and verify the status of their abstract state refinement.
3421 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean;
3422 -- Determine whether construct N is subject to pragma SPARK_Mode Off
3424 -------------------
3425 -- Check_Package --
3426 -------------------
3428 procedure Check_Package (Pack : Node_Id) is
3429 Body_Id : constant Entity_Id := Corresponding_Body (Pack);
3430 Spec : constant Node_Id := Specification (Pack);
3431 States : constant Elist_Id :=
3432 Abstract_States (Defining_Entity (Pack));
3434 State_Elmt : Elmt_Id;
3435 State_Id : Entity_Id;
3437 begin
3438 -- Do not verify proper state refinement when the package is subject
3439 -- to pragma SPARK_Mode Off because this disables the requirement for
3440 -- state refinement.
3442 if SPARK_Mode_Is_Off (Pack) then
3443 null;
3445 -- State refinement can only occur in a completing packge body. Do
3446 -- not verify proper state refinement when the body is subject to
3447 -- pragma SPARK_Mode Off because this disables the requirement for
3448 -- state refinement.
3450 elsif Present (Body_Id)
3451 and then SPARK_Mode_Is_Off (Unit_Declaration_Node (Body_Id))
3452 then
3453 null;
3455 -- Do not verify proper state refinement when the package is an
3456 -- instance as this check was already performed in the generic.
3458 elsif Present (Generic_Parent (Spec)) then
3459 null;
3461 -- Otherwise examine the contents of the package
3463 else
3464 if Present (States) then
3465 State_Elmt := First_Elmt (States);
3466 while Present (State_Elmt) loop
3467 State_Id := Node (State_Elmt);
3469 -- Emit an error when a non-null state lacks any form of
3470 -- refinement.
3472 if not Is_Null_State (State_Id)
3473 and then not Has_Null_Refinement (State_Id)
3474 and then not Has_Non_Null_Refinement (State_Id)
3475 then
3476 Error_Msg_N ("state & requires refinement", State_Id);
3477 end if;
3479 Next_Elmt (State_Elmt);
3480 end loop;
3481 end if;
3483 Check_Packages_In (Visible_Declarations (Spec));
3484 Check_Packages_In (Private_Declarations (Spec));
3485 end if;
3486 end Check_Package;
3488 -----------------------
3489 -- Check_Packages_In --
3490 -----------------------
3492 procedure Check_Packages_In (Decls : List_Id) is
3493 Decl : Node_Id;
3495 begin
3496 if Present (Decls) then
3497 Decl := First (Decls);
3498 while Present (Decl) loop
3499 if Nkind_In (Decl, N_Generic_Package_Declaration,
3500 N_Package_Declaration)
3501 then
3502 Check_Package (Decl);
3503 end if;
3505 Next (Decl);
3506 end loop;
3507 end if;
3508 end Check_Packages_In;
3510 -----------------------
3511 -- SPARK_Mode_Is_Off --
3512 -----------------------
3514 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean is
3515 Prag : constant Node_Id := SPARK_Pragma (Defining_Entity (N));
3517 begin
3518 return
3519 Present (Prag) and then Get_SPARK_Mode_From_Annotation (Prag) = Off;
3520 end SPARK_Mode_Is_Off;
3522 -- Start of processing for Check_State_Refinements
3524 begin
3525 -- A block may declare a nested package
3527 if Nkind (Context) = N_Block_Statement then
3528 Check_Packages_In (Declarations (Context));
3530 -- An entry, protected, subprogram, or task body may declare a nested
3531 -- package.
3533 elsif Nkind_In (Context, N_Entry_Body,
3534 N_Protected_Body,
3535 N_Subprogram_Body,
3536 N_Task_Body)
3537 then
3538 -- Do not verify proper state refinement when the body is subject to
3539 -- pragma SPARK_Mode Off because this disables the requirement for
3540 -- state refinement.
3542 if not SPARK_Mode_Is_Off (Context) then
3543 Check_Packages_In (Declarations (Context));
3544 end if;
3546 -- A package body may declare a nested package
3548 elsif Nkind (Context) = N_Package_Body then
3549 Check_Package (Unit_Declaration_Node (Corresponding_Spec (Context)));
3551 -- Do not verify proper state refinement when the body is subject to
3552 -- pragma SPARK_Mode Off because this disables the requirement for
3553 -- state refinement.
3555 if not SPARK_Mode_Is_Off (Context) then
3556 Check_Packages_In (Declarations (Context));
3557 end if;
3559 -- A library level [generic] package may declare a nested package
3561 elsif Nkind_In (Context, N_Generic_Package_Declaration,
3562 N_Package_Declaration)
3563 and then Is_Main_Unit
3564 then
3565 Check_Package (Context);
3566 end if;
3567 end Check_State_Refinements;
3569 ------------------------------
3570 -- Check_Unprotected_Access --
3571 ------------------------------
3573 procedure Check_Unprotected_Access
3574 (Context : Node_Id;
3575 Expr : Node_Id)
3577 Cont_Encl_Typ : Entity_Id;
3578 Pref_Encl_Typ : Entity_Id;
3580 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3581 -- Check whether Obj is a private component of a protected object.
3582 -- Return the protected type where the component resides, Empty
3583 -- otherwise.
3585 function Is_Public_Operation return Boolean;
3586 -- Verify that the enclosing operation is callable from outside the
3587 -- protected object, to minimize false positives.
3589 ------------------------------
3590 -- Enclosing_Protected_Type --
3591 ------------------------------
3593 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3594 begin
3595 if Is_Entity_Name (Obj) then
3596 declare
3597 Ent : Entity_Id := Entity (Obj);
3599 begin
3600 -- The object can be a renaming of a private component, use
3601 -- the original record component.
3603 if Is_Prival (Ent) then
3604 Ent := Prival_Link (Ent);
3605 end if;
3607 if Is_Protected_Type (Scope (Ent)) then
3608 return Scope (Ent);
3609 end if;
3610 end;
3611 end if;
3613 -- For indexed and selected components, recursively check the prefix
3615 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3616 return Enclosing_Protected_Type (Prefix (Obj));
3618 -- The object does not denote a protected component
3620 else
3621 return Empty;
3622 end if;
3623 end Enclosing_Protected_Type;
3625 -------------------------
3626 -- Is_Public_Operation --
3627 -------------------------
3629 function Is_Public_Operation return Boolean is
3630 S : Entity_Id;
3631 E : Entity_Id;
3633 begin
3634 S := Current_Scope;
3635 while Present (S) and then S /= Pref_Encl_Typ loop
3636 if Scope (S) = Pref_Encl_Typ then
3637 E := First_Entity (Pref_Encl_Typ);
3638 while Present (E)
3639 and then E /= First_Private_Entity (Pref_Encl_Typ)
3640 loop
3641 if E = S then
3642 return True;
3643 end if;
3645 Next_Entity (E);
3646 end loop;
3647 end if;
3649 S := Scope (S);
3650 end loop;
3652 return False;
3653 end Is_Public_Operation;
3655 -- Start of processing for Check_Unprotected_Access
3657 begin
3658 if Nkind (Expr) = N_Attribute_Reference
3659 and then Attribute_Name (Expr) = Name_Unchecked_Access
3660 then
3661 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3662 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3664 -- Check whether we are trying to export a protected component to a
3665 -- context with an equal or lower access level.
3667 if Present (Pref_Encl_Typ)
3668 and then No (Cont_Encl_Typ)
3669 and then Is_Public_Operation
3670 and then Scope_Depth (Pref_Encl_Typ) >=
3671 Object_Access_Level (Context)
3672 then
3673 Error_Msg_N
3674 ("??possible unprotected access to protected data", Expr);
3675 end if;
3676 end if;
3677 end Check_Unprotected_Access;
3679 ------------------------------
3680 -- Check_Unused_Body_States --
3681 ------------------------------
3683 procedure Check_Unused_Body_States (Body_Id : Entity_Id) is
3684 procedure Process_Refinement_Clause
3685 (Clause : Node_Id;
3686 States : Elist_Id);
3687 -- Inspect all constituents of refinement clause Clause and remove any
3688 -- matches from body state list States.
3690 procedure Report_Unused_Body_States (States : Elist_Id);
3691 -- Emit errors for each abstract state or object found in list States
3693 -------------------------------
3694 -- Process_Refinement_Clause --
3695 -------------------------------
3697 procedure Process_Refinement_Clause
3698 (Clause : Node_Id;
3699 States : Elist_Id)
3701 procedure Process_Constituent (Constit : Node_Id);
3702 -- Remove constituent Constit from body state list States
3704 -------------------------
3705 -- Process_Constituent --
3706 -------------------------
3708 procedure Process_Constituent (Constit : Node_Id) is
3709 Constit_Id : Entity_Id;
3711 begin
3712 -- Guard against illegal constituents. Only abstract states and
3713 -- objects can appear on the right hand side of a refinement.
3715 if Is_Entity_Name (Constit) then
3716 Constit_Id := Entity_Of (Constit);
3718 if Present (Constit_Id)
3719 and then Ekind_In (Constit_Id, E_Abstract_State,
3720 E_Constant,
3721 E_Variable)
3722 then
3723 Remove (States, Constit_Id);
3724 end if;
3725 end if;
3726 end Process_Constituent;
3728 -- Local variables
3730 Constit : Node_Id;
3732 -- Start of processing for Process_Refinement_Clause
3734 begin
3735 if Nkind (Clause) = N_Component_Association then
3736 Constit := Expression (Clause);
3738 -- Multiple constituents appear as an aggregate
3740 if Nkind (Constit) = N_Aggregate then
3741 Constit := First (Expressions (Constit));
3742 while Present (Constit) loop
3743 Process_Constituent (Constit);
3744 Next (Constit);
3745 end loop;
3747 -- Various forms of a single constituent
3749 else
3750 Process_Constituent (Constit);
3751 end if;
3752 end if;
3753 end Process_Refinement_Clause;
3755 -------------------------------
3756 -- Report_Unused_Body_States --
3757 -------------------------------
3759 procedure Report_Unused_Body_States (States : Elist_Id) is
3760 Posted : Boolean := False;
3761 State_Elmt : Elmt_Id;
3762 State_Id : Entity_Id;
3764 begin
3765 if Present (States) then
3766 State_Elmt := First_Elmt (States);
3767 while Present (State_Elmt) loop
3768 State_Id := Node (State_Elmt);
3770 -- Constants are part of the hidden state of a package, but the
3771 -- compiler cannot determine whether they have variable input
3772 -- (SPARK RM 7.1.1(2)) and cannot classify them properly as a
3773 -- hidden state. Do not emit an error when a constant does not
3774 -- participate in a state refinement, even though it acts as a
3775 -- hidden state.
3777 if Ekind (State_Id) = E_Constant then
3778 null;
3780 -- Generate an error message of the form:
3782 -- body of package ... has unused hidden states
3783 -- abstract state ... defined at ...
3784 -- variable ... defined at ...
3786 else
3787 if not Posted then
3788 Posted := True;
3789 SPARK_Msg_N
3790 ("body of package & has unused hidden states", Body_Id);
3791 end if;
3793 Error_Msg_Sloc := Sloc (State_Id);
3795 if Ekind (State_Id) = E_Abstract_State then
3796 SPARK_Msg_NE
3797 ("\abstract state & defined #", Body_Id, State_Id);
3799 else
3800 SPARK_Msg_NE ("\variable & defined #", Body_Id, State_Id);
3801 end if;
3802 end if;
3804 Next_Elmt (State_Elmt);
3805 end loop;
3806 end if;
3807 end Report_Unused_Body_States;
3809 -- Local variables
3811 Prag : constant Node_Id := Get_Pragma (Body_Id, Pragma_Refined_State);
3812 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
3813 Clause : Node_Id;
3814 States : Elist_Id;
3816 -- Start of processing for Check_Unused_Body_States
3818 begin
3819 -- Inspect the clauses of pragma Refined_State and determine whether all
3820 -- visible states declared within the package body participate in the
3821 -- refinement.
3823 if Present (Prag) then
3824 Clause := Expression (Get_Argument (Prag, Spec_Id));
3825 States := Collect_Body_States (Body_Id);
3827 -- Multiple non-null state refinements appear as an aggregate
3829 if Nkind (Clause) = N_Aggregate then
3830 Clause := First (Component_Associations (Clause));
3831 while Present (Clause) loop
3832 Process_Refinement_Clause (Clause, States);
3833 Next (Clause);
3834 end loop;
3836 -- Various forms of a single state refinement
3838 else
3839 Process_Refinement_Clause (Clause, States);
3840 end if;
3842 -- Ensure that all abstract states and objects declared in the
3843 -- package body state space are utilized as constituents.
3845 Report_Unused_Body_States (States);
3846 end if;
3847 end Check_Unused_Body_States;
3849 -------------------------
3850 -- Collect_Body_States --
3851 -------------------------
3853 function Collect_Body_States (Body_Id : Entity_Id) return Elist_Id is
3854 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean;
3855 -- Determine whether object Obj_Id is a suitable visible state of a
3856 -- package body.
3858 procedure Collect_Visible_States
3859 (Pack_Id : Entity_Id;
3860 States : in out Elist_Id);
3861 -- Gather the entities of all abstract states and objects declared in
3862 -- the visible state space of package Pack_Id.
3864 ----------------------------
3865 -- Collect_Visible_States --
3866 ----------------------------
3868 procedure Collect_Visible_States
3869 (Pack_Id : Entity_Id;
3870 States : in out Elist_Id)
3872 Item_Id : Entity_Id;
3874 begin
3875 -- Traverse the entity chain of the package and inspect all visible
3876 -- items.
3878 Item_Id := First_Entity (Pack_Id);
3879 while Present (Item_Id) and then not In_Private_Part (Item_Id) loop
3881 -- Do not consider internally generated items as those cannot be
3882 -- named and participate in refinement.
3884 if not Comes_From_Source (Item_Id) then
3885 null;
3887 elsif Ekind (Item_Id) = E_Abstract_State then
3888 Append_New_Elmt (Item_Id, States);
3890 elsif Ekind_In (Item_Id, E_Constant, E_Variable)
3891 and then Is_Visible_Object (Item_Id)
3892 then
3893 Append_New_Elmt (Item_Id, States);
3895 -- Recursively gather the visible states of a nested package
3897 elsif Ekind (Item_Id) = E_Package then
3898 Collect_Visible_States (Item_Id, States);
3899 end if;
3901 Next_Entity (Item_Id);
3902 end loop;
3903 end Collect_Visible_States;
3905 -----------------------
3906 -- Is_Visible_Object --
3907 -----------------------
3909 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean is
3910 begin
3911 -- Objects that map generic formals to their actuals are not visible
3912 -- from outside the generic instantiation.
3914 if Present (Corresponding_Generic_Association
3915 (Declaration_Node (Obj_Id)))
3916 then
3917 return False;
3919 -- Constituents of a single protected/task type act as components of
3920 -- the type and are not visible from outside the type.
3922 elsif Ekind (Obj_Id) = E_Variable
3923 and then Present (Encapsulating_State (Obj_Id))
3924 and then Is_Single_Concurrent_Object (Encapsulating_State (Obj_Id))
3925 then
3926 return False;
3928 else
3929 return True;
3930 end if;
3931 end Is_Visible_Object;
3933 -- Local variables
3935 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
3936 Decl : Node_Id;
3937 Item_Id : Entity_Id;
3938 States : Elist_Id := No_Elist;
3940 -- Start of processing for Collect_Body_States
3942 begin
3943 -- Inspect the declarations of the body looking for source objects,
3944 -- packages and package instantiations. Note that even though this
3945 -- processing is very similar to Collect_Visible_States, a package
3946 -- body does not have a First/Next_Entity list.
3948 Decl := First (Declarations (Body_Decl));
3949 while Present (Decl) loop
3951 -- Capture source objects as internally generated temporaries cannot
3952 -- be named and participate in refinement.
3954 if Nkind (Decl) = N_Object_Declaration then
3955 Item_Id := Defining_Entity (Decl);
3957 if Comes_From_Source (Item_Id)
3958 and then Is_Visible_Object (Item_Id)
3959 then
3960 Append_New_Elmt (Item_Id, States);
3961 end if;
3963 -- Capture the visible abstract states and objects of a source
3964 -- package [instantiation].
3966 elsif Nkind (Decl) = N_Package_Declaration then
3967 Item_Id := Defining_Entity (Decl);
3969 if Comes_From_Source (Item_Id) then
3970 Collect_Visible_States (Item_Id, States);
3971 end if;
3972 end if;
3974 Next (Decl);
3975 end loop;
3977 return States;
3978 end Collect_Body_States;
3980 ------------------------
3981 -- Collect_Interfaces --
3982 ------------------------
3984 procedure Collect_Interfaces
3985 (T : Entity_Id;
3986 Ifaces_List : out Elist_Id;
3987 Exclude_Parents : Boolean := False;
3988 Use_Full_View : Boolean := True)
3990 procedure Collect (Typ : Entity_Id);
3991 -- Subsidiary subprogram used to traverse the whole list
3992 -- of directly and indirectly implemented interfaces
3994 -------------
3995 -- Collect --
3996 -------------
3998 procedure Collect (Typ : Entity_Id) is
3999 Ancestor : Entity_Id;
4000 Full_T : Entity_Id;
4001 Id : Node_Id;
4002 Iface : Entity_Id;
4004 begin
4005 Full_T := Typ;
4007 -- Handle private types and subtypes
4009 if Use_Full_View
4010 and then Is_Private_Type (Typ)
4011 and then Present (Full_View (Typ))
4012 then
4013 Full_T := Full_View (Typ);
4015 if Ekind (Full_T) = E_Record_Subtype then
4016 Full_T := Etype (Typ);
4018 if Present (Full_View (Full_T)) then
4019 Full_T := Full_View (Full_T);
4020 end if;
4021 end if;
4022 end if;
4024 -- Include the ancestor if we are generating the whole list of
4025 -- abstract interfaces.
4027 if Etype (Full_T) /= Typ
4029 -- Protect the frontend against wrong sources. For example:
4031 -- package P is
4032 -- type A is tagged null record;
4033 -- type B is new A with private;
4034 -- type C is new A with private;
4035 -- private
4036 -- type B is new C with null record;
4037 -- type C is new B with null record;
4038 -- end P;
4040 and then Etype (Full_T) /= T
4041 then
4042 Ancestor := Etype (Full_T);
4043 Collect (Ancestor);
4045 if Is_Interface (Ancestor) and then not Exclude_Parents then
4046 Append_Unique_Elmt (Ancestor, Ifaces_List);
4047 end if;
4048 end if;
4050 -- Traverse the graph of ancestor interfaces
4052 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
4053 Id := First (Abstract_Interface_List (Full_T));
4054 while Present (Id) loop
4055 Iface := Etype (Id);
4057 -- Protect against wrong uses. For example:
4058 -- type I is interface;
4059 -- type O is tagged null record;
4060 -- type Wrong is new I and O with null record; -- ERROR
4062 if Is_Interface (Iface) then
4063 if Exclude_Parents
4064 and then Etype (T) /= T
4065 and then Interface_Present_In_Ancestor (Etype (T), Iface)
4066 then
4067 null;
4068 else
4069 Collect (Iface);
4070 Append_Unique_Elmt (Iface, Ifaces_List);
4071 end if;
4072 end if;
4074 Next (Id);
4075 end loop;
4076 end if;
4077 end Collect;
4079 -- Start of processing for Collect_Interfaces
4081 begin
4082 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
4083 Ifaces_List := New_Elmt_List;
4084 Collect (T);
4085 end Collect_Interfaces;
4087 ----------------------------------
4088 -- Collect_Interface_Components --
4089 ----------------------------------
4091 procedure Collect_Interface_Components
4092 (Tagged_Type : Entity_Id;
4093 Components_List : out Elist_Id)
4095 procedure Collect (Typ : Entity_Id);
4096 -- Subsidiary subprogram used to climb to the parents
4098 -------------
4099 -- Collect --
4100 -------------
4102 procedure Collect (Typ : Entity_Id) is
4103 Tag_Comp : Entity_Id;
4104 Parent_Typ : Entity_Id;
4106 begin
4107 -- Handle private types
4109 if Present (Full_View (Etype (Typ))) then
4110 Parent_Typ := Full_View (Etype (Typ));
4111 else
4112 Parent_Typ := Etype (Typ);
4113 end if;
4115 if Parent_Typ /= Typ
4117 -- Protect the frontend against wrong sources. For example:
4119 -- package P is
4120 -- type A is tagged null record;
4121 -- type B is new A with private;
4122 -- type C is new A with private;
4123 -- private
4124 -- type B is new C with null record;
4125 -- type C is new B with null record;
4126 -- end P;
4128 and then Parent_Typ /= Tagged_Type
4129 then
4130 Collect (Parent_Typ);
4131 end if;
4133 -- Collect the components containing tags of secondary dispatch
4134 -- tables.
4136 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
4137 while Present (Tag_Comp) loop
4138 pragma Assert (Present (Related_Type (Tag_Comp)));
4139 Append_Elmt (Tag_Comp, Components_List);
4141 Tag_Comp := Next_Tag_Component (Tag_Comp);
4142 end loop;
4143 end Collect;
4145 -- Start of processing for Collect_Interface_Components
4147 begin
4148 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
4149 and then Is_Tagged_Type (Tagged_Type));
4151 Components_List := New_Elmt_List;
4152 Collect (Tagged_Type);
4153 end Collect_Interface_Components;
4155 -----------------------------
4156 -- Collect_Interfaces_Info --
4157 -----------------------------
4159 procedure Collect_Interfaces_Info
4160 (T : Entity_Id;
4161 Ifaces_List : out Elist_Id;
4162 Components_List : out Elist_Id;
4163 Tags_List : out Elist_Id)
4165 Comps_List : Elist_Id;
4166 Comp_Elmt : Elmt_Id;
4167 Comp_Iface : Entity_Id;
4168 Iface_Elmt : Elmt_Id;
4169 Iface : Entity_Id;
4171 function Search_Tag (Iface : Entity_Id) return Entity_Id;
4172 -- Search for the secondary tag associated with the interface type
4173 -- Iface that is implemented by T.
4175 ----------------
4176 -- Search_Tag --
4177 ----------------
4179 function Search_Tag (Iface : Entity_Id) return Entity_Id is
4180 ADT : Elmt_Id;
4181 begin
4182 if not Is_CPP_Class (T) then
4183 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
4184 else
4185 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
4186 end if;
4188 while Present (ADT)
4189 and then Is_Tag (Node (ADT))
4190 and then Related_Type (Node (ADT)) /= Iface
4191 loop
4192 -- Skip secondary dispatch table referencing thunks to user
4193 -- defined primitives covered by this interface.
4195 pragma Assert (Has_Suffix (Node (ADT), 'P'));
4196 Next_Elmt (ADT);
4198 -- Skip secondary dispatch tables of Ada types
4200 if not Is_CPP_Class (T) then
4202 -- Skip secondary dispatch table referencing thunks to
4203 -- predefined primitives.
4205 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
4206 Next_Elmt (ADT);
4208 -- Skip secondary dispatch table referencing user-defined
4209 -- primitives covered by this interface.
4211 pragma Assert (Has_Suffix (Node (ADT), 'D'));
4212 Next_Elmt (ADT);
4214 -- Skip secondary dispatch table referencing predefined
4215 -- primitives.
4217 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
4218 Next_Elmt (ADT);
4219 end if;
4220 end loop;
4222 pragma Assert (Is_Tag (Node (ADT)));
4223 return Node (ADT);
4224 end Search_Tag;
4226 -- Start of processing for Collect_Interfaces_Info
4228 begin
4229 Collect_Interfaces (T, Ifaces_List);
4230 Collect_Interface_Components (T, Comps_List);
4232 -- Search for the record component and tag associated with each
4233 -- interface type of T.
4235 Components_List := New_Elmt_List;
4236 Tags_List := New_Elmt_List;
4238 Iface_Elmt := First_Elmt (Ifaces_List);
4239 while Present (Iface_Elmt) loop
4240 Iface := Node (Iface_Elmt);
4242 -- Associate the primary tag component and the primary dispatch table
4243 -- with all the interfaces that are parents of T
4245 if Is_Ancestor (Iface, T, Use_Full_View => True) then
4246 Append_Elmt (First_Tag_Component (T), Components_List);
4247 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
4249 -- Otherwise search for the tag component and secondary dispatch
4250 -- table of Iface
4252 else
4253 Comp_Elmt := First_Elmt (Comps_List);
4254 while Present (Comp_Elmt) loop
4255 Comp_Iface := Related_Type (Node (Comp_Elmt));
4257 if Comp_Iface = Iface
4258 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
4259 then
4260 Append_Elmt (Node (Comp_Elmt), Components_List);
4261 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
4262 exit;
4263 end if;
4265 Next_Elmt (Comp_Elmt);
4266 end loop;
4267 pragma Assert (Present (Comp_Elmt));
4268 end if;
4270 Next_Elmt (Iface_Elmt);
4271 end loop;
4272 end Collect_Interfaces_Info;
4274 ---------------------
4275 -- Collect_Parents --
4276 ---------------------
4278 procedure Collect_Parents
4279 (T : Entity_Id;
4280 List : out Elist_Id;
4281 Use_Full_View : Boolean := True)
4283 Current_Typ : Entity_Id := T;
4284 Parent_Typ : Entity_Id;
4286 begin
4287 List := New_Elmt_List;
4289 -- No action if the if the type has no parents
4291 if T = Etype (T) then
4292 return;
4293 end if;
4295 loop
4296 Parent_Typ := Etype (Current_Typ);
4298 if Is_Private_Type (Parent_Typ)
4299 and then Present (Full_View (Parent_Typ))
4300 and then Use_Full_View
4301 then
4302 Parent_Typ := Full_View (Base_Type (Parent_Typ));
4303 end if;
4305 Append_Elmt (Parent_Typ, List);
4307 exit when Parent_Typ = Current_Typ;
4308 Current_Typ := Parent_Typ;
4309 end loop;
4310 end Collect_Parents;
4312 ----------------------------------
4313 -- Collect_Primitive_Operations --
4314 ----------------------------------
4316 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
4317 B_Type : constant Entity_Id := Base_Type (T);
4318 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
4319 B_Scope : Entity_Id := Scope (B_Type);
4320 Op_List : Elist_Id;
4321 Formal : Entity_Id;
4322 Is_Prim : Boolean;
4323 Is_Type_In_Pkg : Boolean;
4324 Formal_Derived : Boolean := False;
4325 Id : Entity_Id;
4327 function Match (E : Entity_Id) return Boolean;
4328 -- True if E's base type is B_Type, or E is of an anonymous access type
4329 -- and the base type of its designated type is B_Type.
4331 -----------
4332 -- Match --
4333 -----------
4335 function Match (E : Entity_Id) return Boolean is
4336 Etyp : Entity_Id := Etype (E);
4338 begin
4339 if Ekind (Etyp) = E_Anonymous_Access_Type then
4340 Etyp := Designated_Type (Etyp);
4341 end if;
4343 -- In Ada 2012 a primitive operation may have a formal of an
4344 -- incomplete view of the parent type.
4346 return Base_Type (Etyp) = B_Type
4347 or else
4348 (Ada_Version >= Ada_2012
4349 and then Ekind (Etyp) = E_Incomplete_Type
4350 and then Full_View (Etyp) = B_Type);
4351 end Match;
4353 -- Start of processing for Collect_Primitive_Operations
4355 begin
4356 -- For tagged types, the primitive operations are collected as they
4357 -- are declared, and held in an explicit list which is simply returned.
4359 if Is_Tagged_Type (B_Type) then
4360 return Primitive_Operations (B_Type);
4362 -- An untagged generic type that is a derived type inherits the
4363 -- primitive operations of its parent type. Other formal types only
4364 -- have predefined operators, which are not explicitly represented.
4366 elsif Is_Generic_Type (B_Type) then
4367 if Nkind (B_Decl) = N_Formal_Type_Declaration
4368 and then Nkind (Formal_Type_Definition (B_Decl)) =
4369 N_Formal_Derived_Type_Definition
4370 then
4371 Formal_Derived := True;
4372 else
4373 return New_Elmt_List;
4374 end if;
4375 end if;
4377 Op_List := New_Elmt_List;
4379 if B_Scope = Standard_Standard then
4380 if B_Type = Standard_String then
4381 Append_Elmt (Standard_Op_Concat, Op_List);
4383 elsif B_Type = Standard_Wide_String then
4384 Append_Elmt (Standard_Op_Concatw, Op_List);
4386 else
4387 null;
4388 end if;
4390 -- Locate the primitive subprograms of the type
4392 else
4393 -- The primitive operations appear after the base type, except
4394 -- if the derivation happens within the private part of B_Scope
4395 -- and the type is a private type, in which case both the type
4396 -- and some primitive operations may appear before the base
4397 -- type, and the list of candidates starts after the type.
4399 if In_Open_Scopes (B_Scope)
4400 and then Scope (T) = B_Scope
4401 and then In_Private_Part (B_Scope)
4402 then
4403 Id := Next_Entity (T);
4405 -- In Ada 2012, If the type has an incomplete partial view, there
4406 -- may be primitive operations declared before the full view, so
4407 -- we need to start scanning from the incomplete view, which is
4408 -- earlier on the entity chain.
4410 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
4411 and then Present (Incomplete_View (Parent (B_Type)))
4412 then
4413 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
4415 -- If T is a derived from a type with an incomplete view declared
4416 -- elsewhere, that incomplete view is irrelevant, we want the
4417 -- operations in the scope of T.
4419 if Scope (Id) /= Scope (B_Type) then
4420 Id := Next_Entity (B_Type);
4421 end if;
4423 else
4424 Id := Next_Entity (B_Type);
4425 end if;
4427 -- Set flag if this is a type in a package spec
4429 Is_Type_In_Pkg :=
4430 Is_Package_Or_Generic_Package (B_Scope)
4431 and then
4432 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
4433 N_Package_Body;
4435 while Present (Id) loop
4437 -- Test whether the result type or any of the parameter types of
4438 -- each subprogram following the type match that type when the
4439 -- type is declared in a package spec, is a derived type, or the
4440 -- subprogram is marked as primitive. (The Is_Primitive test is
4441 -- needed to find primitives of nonderived types in declarative
4442 -- parts that happen to override the predefined "=" operator.)
4444 -- Note that generic formal subprograms are not considered to be
4445 -- primitive operations and thus are never inherited.
4447 if Is_Overloadable (Id)
4448 and then (Is_Type_In_Pkg
4449 or else Is_Derived_Type (B_Type)
4450 or else Is_Primitive (Id))
4451 and then Nkind (Parent (Parent (Id)))
4452 not in N_Formal_Subprogram_Declaration
4453 then
4454 Is_Prim := False;
4456 if Match (Id) then
4457 Is_Prim := True;
4459 else
4460 Formal := First_Formal (Id);
4461 while Present (Formal) loop
4462 if Match (Formal) then
4463 Is_Prim := True;
4464 exit;
4465 end if;
4467 Next_Formal (Formal);
4468 end loop;
4469 end if;
4471 -- For a formal derived type, the only primitives are the ones
4472 -- inherited from the parent type. Operations appearing in the
4473 -- package declaration are not primitive for it.
4475 if Is_Prim
4476 and then (not Formal_Derived or else Present (Alias (Id)))
4477 then
4478 -- In the special case of an equality operator aliased to
4479 -- an overriding dispatching equality belonging to the same
4480 -- type, we don't include it in the list of primitives.
4481 -- This avoids inheriting multiple equality operators when
4482 -- deriving from untagged private types whose full type is
4483 -- tagged, which can otherwise cause ambiguities. Note that
4484 -- this should only happen for this kind of untagged parent
4485 -- type, since normally dispatching operations are inherited
4486 -- using the type's Primitive_Operations list.
4488 if Chars (Id) = Name_Op_Eq
4489 and then Is_Dispatching_Operation (Id)
4490 and then Present (Alias (Id))
4491 and then Present (Overridden_Operation (Alias (Id)))
4492 and then Base_Type (Etype (First_Entity (Id))) =
4493 Base_Type (Etype (First_Entity (Alias (Id))))
4494 then
4495 null;
4497 -- Include the subprogram in the list of primitives
4499 else
4500 Append_Elmt (Id, Op_List);
4501 end if;
4502 end if;
4503 end if;
4505 Next_Entity (Id);
4507 -- For a type declared in System, some of its operations may
4508 -- appear in the target-specific extension to System.
4510 if No (Id)
4511 and then B_Scope = RTU_Entity (System)
4512 and then Present_System_Aux
4513 then
4514 B_Scope := System_Aux_Id;
4515 Id := First_Entity (System_Aux_Id);
4516 end if;
4517 end loop;
4518 end if;
4520 return Op_List;
4521 end Collect_Primitive_Operations;
4523 -----------------------------------
4524 -- Compile_Time_Constraint_Error --
4525 -----------------------------------
4527 function Compile_Time_Constraint_Error
4528 (N : Node_Id;
4529 Msg : String;
4530 Ent : Entity_Id := Empty;
4531 Loc : Source_Ptr := No_Location;
4532 Warn : Boolean := False) return Node_Id
4534 Msgc : String (1 .. Msg'Length + 3);
4535 -- Copy of message, with room for possible ?? or << and ! at end
4537 Msgl : Natural;
4538 Wmsg : Boolean;
4539 Eloc : Source_Ptr;
4541 -- Start of processing for Compile_Time_Constraint_Error
4543 begin
4544 -- If this is a warning, convert it into an error if we are in code
4545 -- subject to SPARK_Mode being set On, unless Warn is True to force a
4546 -- warning. The rationale is that a compile-time constraint error should
4547 -- lead to an error instead of a warning when SPARK_Mode is On, but in
4548 -- a few cases we prefer to issue a warning and generate both a suitable
4549 -- run-time error in GNAT and a suitable check message in GNATprove.
4550 -- Those cases are those that likely correspond to deactivated SPARK
4551 -- code, so that this kind of code can be compiled and analyzed instead
4552 -- of being rejected.
4554 Error_Msg_Warn := Warn or SPARK_Mode /= On;
4556 -- A static constraint error in an instance body is not a fatal error.
4557 -- we choose to inhibit the message altogether, because there is no
4558 -- obvious node (for now) on which to post it. On the other hand the
4559 -- offending node must be replaced with a constraint_error in any case.
4561 -- No messages are generated if we already posted an error on this node
4563 if not Error_Posted (N) then
4564 if Loc /= No_Location then
4565 Eloc := Loc;
4566 else
4567 Eloc := Sloc (N);
4568 end if;
4570 -- Copy message to Msgc, converting any ? in the message into
4571 -- < instead, so that we have an error in GNATprove mode.
4573 Msgl := Msg'Length;
4575 for J in 1 .. Msgl loop
4576 if Msg (J) = '?' and then (J = 1 or else Msg (J - 1) /= ''') then
4577 Msgc (J) := '<';
4578 else
4579 Msgc (J) := Msg (J);
4580 end if;
4581 end loop;
4583 -- Message is a warning, even in Ada 95 case
4585 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
4586 Wmsg := True;
4588 -- In Ada 83, all messages are warnings. In the private part and
4589 -- the body of an instance, constraint_checks are only warnings.
4590 -- We also make this a warning if the Warn parameter is set.
4592 elsif Warn
4593 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
4594 then
4595 Msgl := Msgl + 1;
4596 Msgc (Msgl) := '<';
4597 Msgl := Msgl + 1;
4598 Msgc (Msgl) := '<';
4599 Wmsg := True;
4601 elsif In_Instance_Not_Visible then
4602 Msgl := Msgl + 1;
4603 Msgc (Msgl) := '<';
4604 Msgl := Msgl + 1;
4605 Msgc (Msgl) := '<';
4606 Wmsg := True;
4608 -- Otherwise we have a real error message (Ada 95 static case)
4609 -- and we make this an unconditional message. Note that in the
4610 -- warning case we do not make the message unconditional, it seems
4611 -- quite reasonable to delete messages like this (about exceptions
4612 -- that will be raised) in dead code.
4614 else
4615 Wmsg := False;
4616 Msgl := Msgl + 1;
4617 Msgc (Msgl) := '!';
4618 end if;
4620 -- One more test, skip the warning if the related expression is
4621 -- statically unevaluated, since we don't want to warn about what
4622 -- will happen when something is evaluated if it never will be
4623 -- evaluated.
4625 if not Is_Statically_Unevaluated (N) then
4626 if Present (Ent) then
4627 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4628 else
4629 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4630 end if;
4632 if Wmsg then
4634 -- Check whether the context is an Init_Proc
4636 if Inside_Init_Proc then
4637 declare
4638 Conc_Typ : constant Entity_Id :=
4639 Corresponding_Concurrent_Type
4640 (Entity (Parameter_Type (First
4641 (Parameter_Specifications
4642 (Parent (Current_Scope))))));
4644 begin
4645 -- Don't complain if the corresponding concurrent type
4646 -- doesn't come from source (i.e. a single task/protected
4647 -- object).
4649 if Present (Conc_Typ)
4650 and then not Comes_From_Source (Conc_Typ)
4651 then
4652 Error_Msg_NEL
4653 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4655 else
4656 if GNATprove_Mode then
4657 Error_Msg_NEL
4658 ("\& would have been raised for objects of this "
4659 & "type", N, Standard_Constraint_Error, Eloc);
4660 else
4661 Error_Msg_NEL
4662 ("\& will be raised for objects of this type??",
4663 N, Standard_Constraint_Error, Eloc);
4664 end if;
4665 end if;
4666 end;
4668 else
4669 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4670 end if;
4672 else
4673 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4674 Set_Error_Posted (N);
4675 end if;
4676 end if;
4677 end if;
4679 return N;
4680 end Compile_Time_Constraint_Error;
4682 -----------------------
4683 -- Conditional_Delay --
4684 -----------------------
4686 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4687 begin
4688 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4689 Set_Has_Delayed_Freeze (New_Ent);
4690 end if;
4691 end Conditional_Delay;
4693 ----------------------------
4694 -- Contains_Refined_State --
4695 ----------------------------
4697 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4698 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4699 -- Determine whether a dependency list mentions a state with a visible
4700 -- refinement.
4702 function Has_State_In_Global (List : Node_Id) return Boolean;
4703 -- Determine whether a global list mentions a state with a visible
4704 -- refinement.
4706 function Is_Refined_State (Item : Node_Id) return Boolean;
4707 -- Determine whether Item is a reference to an abstract state with a
4708 -- visible refinement.
4710 -----------------------------
4711 -- Has_State_In_Dependency --
4712 -----------------------------
4714 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4715 Clause : Node_Id;
4716 Output : Node_Id;
4718 begin
4719 -- A null dependency list does not mention any states
4721 if Nkind (List) = N_Null then
4722 return False;
4724 -- Dependency clauses appear as component associations of an
4725 -- aggregate.
4727 elsif Nkind (List) = N_Aggregate
4728 and then Present (Component_Associations (List))
4729 then
4730 Clause := First (Component_Associations (List));
4731 while Present (Clause) loop
4733 -- Inspect the outputs of a dependency clause
4735 Output := First (Choices (Clause));
4736 while Present (Output) loop
4737 if Is_Refined_State (Output) then
4738 return True;
4739 end if;
4741 Next (Output);
4742 end loop;
4744 -- Inspect the outputs of a dependency clause
4746 if Is_Refined_State (Expression (Clause)) then
4747 return True;
4748 end if;
4750 Next (Clause);
4751 end loop;
4753 -- If we get here, then none of the dependency clauses mention a
4754 -- state with visible refinement.
4756 return False;
4758 -- An illegal pragma managed to sneak in
4760 else
4761 raise Program_Error;
4762 end if;
4763 end Has_State_In_Dependency;
4765 -------------------------
4766 -- Has_State_In_Global --
4767 -------------------------
4769 function Has_State_In_Global (List : Node_Id) return Boolean is
4770 Item : Node_Id;
4772 begin
4773 -- A null global list does not mention any states
4775 if Nkind (List) = N_Null then
4776 return False;
4778 -- Simple global list or moded global list declaration
4780 elsif Nkind (List) = N_Aggregate then
4782 -- The declaration of a simple global list appear as a collection
4783 -- of expressions.
4785 if Present (Expressions (List)) then
4786 Item := First (Expressions (List));
4787 while Present (Item) loop
4788 if Is_Refined_State (Item) then
4789 return True;
4790 end if;
4792 Next (Item);
4793 end loop;
4795 -- The declaration of a moded global list appears as a collection
4796 -- of component associations where individual choices denote
4797 -- modes.
4799 else
4800 Item := First (Component_Associations (List));
4801 while Present (Item) loop
4802 if Has_State_In_Global (Expression (Item)) then
4803 return True;
4804 end if;
4806 Next (Item);
4807 end loop;
4808 end if;
4810 -- If we get here, then the simple/moded global list did not
4811 -- mention any states with a visible refinement.
4813 return False;
4815 -- Single global item declaration
4817 elsif Is_Entity_Name (List) then
4818 return Is_Refined_State (List);
4820 -- An illegal pragma managed to sneak in
4822 else
4823 raise Program_Error;
4824 end if;
4825 end Has_State_In_Global;
4827 ----------------------
4828 -- Is_Refined_State --
4829 ----------------------
4831 function Is_Refined_State (Item : Node_Id) return Boolean is
4832 Elmt : Node_Id;
4833 Item_Id : Entity_Id;
4835 begin
4836 if Nkind (Item) = N_Null then
4837 return False;
4839 -- States cannot be subject to attribute 'Result. This case arises
4840 -- in dependency relations.
4842 elsif Nkind (Item) = N_Attribute_Reference
4843 and then Attribute_Name (Item) = Name_Result
4844 then
4845 return False;
4847 -- Multiple items appear as an aggregate. This case arises in
4848 -- dependency relations.
4850 elsif Nkind (Item) = N_Aggregate
4851 and then Present (Expressions (Item))
4852 then
4853 Elmt := First (Expressions (Item));
4854 while Present (Elmt) loop
4855 if Is_Refined_State (Elmt) then
4856 return True;
4857 end if;
4859 Next (Elmt);
4860 end loop;
4862 -- If we get here, then none of the inputs or outputs reference a
4863 -- state with visible refinement.
4865 return False;
4867 -- Single item
4869 else
4870 Item_Id := Entity_Of (Item);
4872 return
4873 Present (Item_Id)
4874 and then Ekind (Item_Id) = E_Abstract_State
4875 and then Has_Visible_Refinement (Item_Id);
4876 end if;
4877 end Is_Refined_State;
4879 -- Local variables
4881 Arg : constant Node_Id :=
4882 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4883 Nam : constant Name_Id := Pragma_Name_Mapped (Prag);
4885 -- Start of processing for Contains_Refined_State
4887 begin
4888 if Nam = Name_Depends then
4889 return Has_State_In_Dependency (Arg);
4891 else pragma Assert (Nam = Name_Global);
4892 return Has_State_In_Global (Arg);
4893 end if;
4894 end Contains_Refined_State;
4896 -------------------------
4897 -- Copy_Component_List --
4898 -------------------------
4900 function Copy_Component_List
4901 (R_Typ : Entity_Id;
4902 Loc : Source_Ptr) return List_Id
4904 Comp : Node_Id;
4905 Comps : constant List_Id := New_List;
4907 begin
4908 Comp := First_Component (Underlying_Type (R_Typ));
4909 while Present (Comp) loop
4910 if Comes_From_Source (Comp) then
4911 declare
4912 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4913 begin
4914 Append_To (Comps,
4915 Make_Component_Declaration (Loc,
4916 Defining_Identifier =>
4917 Make_Defining_Identifier (Loc, Chars (Comp)),
4918 Component_Definition =>
4919 New_Copy_Tree
4920 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4921 end;
4922 end if;
4924 Next_Component (Comp);
4925 end loop;
4927 return Comps;
4928 end Copy_Component_List;
4930 -------------------------
4931 -- Copy_Parameter_List --
4932 -------------------------
4934 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4935 Loc : constant Source_Ptr := Sloc (Subp_Id);
4936 Plist : List_Id;
4937 Formal : Entity_Id;
4939 begin
4940 if No (First_Formal (Subp_Id)) then
4941 return No_List;
4942 else
4943 Plist := New_List;
4944 Formal := First_Formal (Subp_Id);
4945 while Present (Formal) loop
4946 Append_To (Plist,
4947 Make_Parameter_Specification (Loc,
4948 Defining_Identifier =>
4949 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4950 In_Present => In_Present (Parent (Formal)),
4951 Out_Present => Out_Present (Parent (Formal)),
4952 Parameter_Type =>
4953 New_Occurrence_Of (Etype (Formal), Loc),
4954 Expression =>
4955 New_Copy_Tree (Expression (Parent (Formal)))));
4957 Next_Formal (Formal);
4958 end loop;
4959 end if;
4961 return Plist;
4962 end Copy_Parameter_List;
4964 --------------------------
4965 -- Copy_Subprogram_Spec --
4966 --------------------------
4968 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
4969 Def_Id : Node_Id;
4970 Formal_Spec : Node_Id;
4971 Result : Node_Id;
4973 begin
4974 -- The structure of the original tree must be replicated without any
4975 -- alterations. Use New_Copy_Tree for this purpose.
4977 Result := New_Copy_Tree (Spec);
4979 -- Create a new entity for the defining unit name
4981 Def_Id := Defining_Unit_Name (Result);
4982 Set_Defining_Unit_Name (Result,
4983 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
4985 -- Create new entities for the formal parameters
4987 if Present (Parameter_Specifications (Result)) then
4988 Formal_Spec := First (Parameter_Specifications (Result));
4989 while Present (Formal_Spec) loop
4990 Def_Id := Defining_Identifier (Formal_Spec);
4991 Set_Defining_Identifier (Formal_Spec,
4992 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
4994 Next (Formal_Spec);
4995 end loop;
4996 end if;
4998 return Result;
4999 end Copy_Subprogram_Spec;
5001 --------------------------------
5002 -- Corresponding_Generic_Type --
5003 --------------------------------
5005 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
5006 Inst : Entity_Id;
5007 Gen : Entity_Id;
5008 Typ : Entity_Id;
5010 begin
5011 if not Is_Generic_Actual_Type (T) then
5012 return Any_Type;
5014 -- If the actual is the actual of an enclosing instance, resolution
5015 -- was correct in the generic.
5017 elsif Nkind (Parent (T)) = N_Subtype_Declaration
5018 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
5019 and then
5020 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
5021 then
5022 return Any_Type;
5024 else
5025 Inst := Scope (T);
5027 if Is_Wrapper_Package (Inst) then
5028 Inst := Related_Instance (Inst);
5029 end if;
5031 Gen :=
5032 Generic_Parent
5033 (Specification (Unit_Declaration_Node (Inst)));
5035 -- Generic actual has the same name as the corresponding formal
5037 Typ := First_Entity (Gen);
5038 while Present (Typ) loop
5039 if Chars (Typ) = Chars (T) then
5040 return Typ;
5041 end if;
5043 Next_Entity (Typ);
5044 end loop;
5046 return Any_Type;
5047 end if;
5048 end Corresponding_Generic_Type;
5050 --------------------
5051 -- Current_Entity --
5052 --------------------
5054 -- The currently visible definition for a given identifier is the
5055 -- one most chained at the start of the visibility chain, i.e. the
5056 -- one that is referenced by the Node_Id value of the name of the
5057 -- given identifier.
5059 function Current_Entity (N : Node_Id) return Entity_Id is
5060 begin
5061 return Get_Name_Entity_Id (Chars (N));
5062 end Current_Entity;
5064 -----------------------------
5065 -- Current_Entity_In_Scope --
5066 -----------------------------
5068 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
5069 E : Entity_Id;
5070 CS : constant Entity_Id := Current_Scope;
5072 Transient_Case : constant Boolean := Scope_Is_Transient;
5074 begin
5075 E := Get_Name_Entity_Id (Chars (N));
5076 while Present (E)
5077 and then Scope (E) /= CS
5078 and then (not Transient_Case or else Scope (E) /= Scope (CS))
5079 loop
5080 E := Homonym (E);
5081 end loop;
5083 return E;
5084 end Current_Entity_In_Scope;
5086 -------------------
5087 -- Current_Scope --
5088 -------------------
5090 function Current_Scope return Entity_Id is
5091 begin
5092 if Scope_Stack.Last = -1 then
5093 return Standard_Standard;
5094 else
5095 declare
5096 C : constant Entity_Id :=
5097 Scope_Stack.Table (Scope_Stack.Last).Entity;
5098 begin
5099 if Present (C) then
5100 return C;
5101 else
5102 return Standard_Standard;
5103 end if;
5104 end;
5105 end if;
5106 end Current_Scope;
5108 ----------------------------
5109 -- Current_Scope_No_Loops --
5110 ----------------------------
5112 function Current_Scope_No_Loops return Entity_Id is
5113 S : Entity_Id;
5115 begin
5116 -- Examine the scope stack starting from the current scope and skip any
5117 -- internally generated loops.
5119 S := Current_Scope;
5120 while Present (S) and then S /= Standard_Standard loop
5121 if Ekind (S) = E_Loop and then not Comes_From_Source (S) then
5122 S := Scope (S);
5123 else
5124 exit;
5125 end if;
5126 end loop;
5128 return S;
5129 end Current_Scope_No_Loops;
5131 ------------------------
5132 -- Current_Subprogram --
5133 ------------------------
5135 function Current_Subprogram return Entity_Id is
5136 Scop : constant Entity_Id := Current_Scope;
5137 begin
5138 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
5139 return Scop;
5140 else
5141 return Enclosing_Subprogram (Scop);
5142 end if;
5143 end Current_Subprogram;
5145 ----------------------------------
5146 -- Deepest_Type_Access_Level --
5147 ----------------------------------
5149 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
5150 begin
5151 if Ekind (Typ) = E_Anonymous_Access_Type
5152 and then not Is_Local_Anonymous_Access (Typ)
5153 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
5154 then
5155 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
5156 -- access type.
5158 return
5159 Scope_Depth (Enclosing_Dynamic_Scope
5160 (Defining_Identifier
5161 (Associated_Node_For_Itype (Typ))));
5163 -- For generic formal type, return Int'Last (infinite).
5164 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
5166 elsif Is_Generic_Type (Root_Type (Typ)) then
5167 return UI_From_Int (Int'Last);
5169 else
5170 return Type_Access_Level (Typ);
5171 end if;
5172 end Deepest_Type_Access_Level;
5174 ---------------------
5175 -- Defining_Entity --
5176 ---------------------
5178 function Defining_Entity
5179 (N : Node_Id;
5180 Empty_On_Errors : Boolean := False) return Entity_Id
5182 Err : Entity_Id := Empty;
5184 begin
5185 case Nkind (N) is
5186 when N_Abstract_Subprogram_Declaration |
5187 N_Expression_Function |
5188 N_Formal_Subprogram_Declaration |
5189 N_Generic_Package_Declaration |
5190 N_Generic_Subprogram_Declaration |
5191 N_Package_Declaration |
5192 N_Subprogram_Body |
5193 N_Subprogram_Body_Stub |
5194 N_Subprogram_Declaration |
5195 N_Subprogram_Renaming_Declaration
5197 return Defining_Entity (Specification (N));
5199 when N_Component_Declaration |
5200 N_Defining_Program_Unit_Name |
5201 N_Discriminant_Specification |
5202 N_Entry_Body |
5203 N_Entry_Declaration |
5204 N_Entry_Index_Specification |
5205 N_Exception_Declaration |
5206 N_Exception_Renaming_Declaration |
5207 N_Formal_Object_Declaration |
5208 N_Formal_Package_Declaration |
5209 N_Formal_Type_Declaration |
5210 N_Full_Type_Declaration |
5211 N_Implicit_Label_Declaration |
5212 N_Incomplete_Type_Declaration |
5213 N_Loop_Parameter_Specification |
5214 N_Number_Declaration |
5215 N_Object_Declaration |
5216 N_Object_Renaming_Declaration |
5217 N_Package_Body_Stub |
5218 N_Parameter_Specification |
5219 N_Private_Extension_Declaration |
5220 N_Private_Type_Declaration |
5221 N_Protected_Body |
5222 N_Protected_Body_Stub |
5223 N_Protected_Type_Declaration |
5224 N_Single_Protected_Declaration |
5225 N_Single_Task_Declaration |
5226 N_Subtype_Declaration |
5227 N_Task_Body |
5228 N_Task_Body_Stub |
5229 N_Task_Type_Declaration
5231 return Defining_Identifier (N);
5233 when N_Subunit =>
5234 return Defining_Entity (Proper_Body (N));
5236 when N_Function_Instantiation |
5237 N_Function_Specification |
5238 N_Generic_Function_Renaming_Declaration |
5239 N_Generic_Package_Renaming_Declaration |
5240 N_Generic_Procedure_Renaming_Declaration |
5241 N_Package_Body |
5242 N_Package_Instantiation |
5243 N_Package_Renaming_Declaration |
5244 N_Package_Specification |
5245 N_Procedure_Instantiation |
5246 N_Procedure_Specification
5248 declare
5249 Nam : constant Node_Id := Defining_Unit_Name (N);
5251 begin
5252 if Nkind (Nam) in N_Entity then
5253 return Nam;
5255 -- For Error, make up a name and attach to declaration so we
5256 -- can continue semantic analysis.
5258 elsif Nam = Error then
5259 if Empty_On_Errors then
5260 return Empty;
5261 else
5262 Err := Make_Temporary (Sloc (N), 'T');
5263 Set_Defining_Unit_Name (N, Err);
5265 return Err;
5266 end if;
5268 -- If not an entity, get defining identifier
5270 else
5271 return Defining_Identifier (Nam);
5272 end if;
5273 end;
5275 when N_Block_Statement |
5276 N_Loop_Statement =>
5277 return Entity (Identifier (N));
5279 when others =>
5280 if Empty_On_Errors then
5281 return Empty;
5282 else
5283 raise Program_Error;
5284 end if;
5286 end case;
5287 end Defining_Entity;
5289 --------------------------
5290 -- Denotes_Discriminant --
5291 --------------------------
5293 function Denotes_Discriminant
5294 (N : Node_Id;
5295 Check_Concurrent : Boolean := False) return Boolean
5297 E : Entity_Id;
5299 begin
5300 if not Is_Entity_Name (N) or else No (Entity (N)) then
5301 return False;
5302 else
5303 E := Entity (N);
5304 end if;
5306 -- If we are checking for a protected type, the discriminant may have
5307 -- been rewritten as the corresponding discriminal of the original type
5308 -- or of the corresponding concurrent record, depending on whether we
5309 -- are in the spec or body of the protected type.
5311 return Ekind (E) = E_Discriminant
5312 or else
5313 (Check_Concurrent
5314 and then Ekind (E) = E_In_Parameter
5315 and then Present (Discriminal_Link (E))
5316 and then
5317 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
5318 or else
5319 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
5320 end Denotes_Discriminant;
5322 -------------------------
5323 -- Denotes_Same_Object --
5324 -------------------------
5326 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
5327 Obj1 : Node_Id := A1;
5328 Obj2 : Node_Id := A2;
5330 function Has_Prefix (N : Node_Id) return Boolean;
5331 -- Return True if N has attribute Prefix
5333 function Is_Renaming (N : Node_Id) return Boolean;
5334 -- Return true if N names a renaming entity
5336 function Is_Valid_Renaming (N : Node_Id) return Boolean;
5337 -- For renamings, return False if the prefix of any dereference within
5338 -- the renamed object_name is a variable, or any expression within the
5339 -- renamed object_name contains references to variables or calls on
5340 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
5342 ----------------
5343 -- Has_Prefix --
5344 ----------------
5346 function Has_Prefix (N : Node_Id) return Boolean is
5347 begin
5348 return
5349 Nkind_In (N,
5350 N_Attribute_Reference,
5351 N_Expanded_Name,
5352 N_Explicit_Dereference,
5353 N_Indexed_Component,
5354 N_Reference,
5355 N_Selected_Component,
5356 N_Slice);
5357 end Has_Prefix;
5359 -----------------
5360 -- Is_Renaming --
5361 -----------------
5363 function Is_Renaming (N : Node_Id) return Boolean is
5364 begin
5365 return Is_Entity_Name (N)
5366 and then Present (Renamed_Entity (Entity (N)));
5367 end Is_Renaming;
5369 -----------------------
5370 -- Is_Valid_Renaming --
5371 -----------------------
5373 function Is_Valid_Renaming (N : Node_Id) return Boolean is
5375 function Check_Renaming (N : Node_Id) return Boolean;
5376 -- Recursive function used to traverse all the prefixes of N
5378 function Check_Renaming (N : Node_Id) return Boolean is
5379 begin
5380 if Is_Renaming (N)
5381 and then not Check_Renaming (Renamed_Entity (Entity (N)))
5382 then
5383 return False;
5384 end if;
5386 if Nkind (N) = N_Indexed_Component then
5387 declare
5388 Indx : Node_Id;
5390 begin
5391 Indx := First (Expressions (N));
5392 while Present (Indx) loop
5393 if not Is_OK_Static_Expression (Indx) then
5394 return False;
5395 end if;
5397 Next_Index (Indx);
5398 end loop;
5399 end;
5400 end if;
5402 if Has_Prefix (N) then
5403 declare
5404 P : constant Node_Id := Prefix (N);
5406 begin
5407 if Nkind (N) = N_Explicit_Dereference
5408 and then Is_Variable (P)
5409 then
5410 return False;
5412 elsif Is_Entity_Name (P)
5413 and then Ekind (Entity (P)) = E_Function
5414 then
5415 return False;
5417 elsif Nkind (P) = N_Function_Call then
5418 return False;
5419 end if;
5421 -- Recursion to continue traversing the prefix of the
5422 -- renaming expression
5424 return Check_Renaming (P);
5425 end;
5426 end if;
5428 return True;
5429 end Check_Renaming;
5431 -- Start of processing for Is_Valid_Renaming
5433 begin
5434 return Check_Renaming (N);
5435 end Is_Valid_Renaming;
5437 -- Start of processing for Denotes_Same_Object
5439 begin
5440 -- Both names statically denote the same stand-alone object or parameter
5441 -- (RM 6.4.1(6.5/3))
5443 if Is_Entity_Name (Obj1)
5444 and then Is_Entity_Name (Obj2)
5445 and then Entity (Obj1) = Entity (Obj2)
5446 then
5447 return True;
5448 end if;
5450 -- For renamings, the prefix of any dereference within the renamed
5451 -- object_name is not a variable, and any expression within the
5452 -- renamed object_name contains no references to variables nor
5453 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
5455 if Is_Renaming (Obj1) then
5456 if Is_Valid_Renaming (Obj1) then
5457 Obj1 := Renamed_Entity (Entity (Obj1));
5458 else
5459 return False;
5460 end if;
5461 end if;
5463 if Is_Renaming (Obj2) then
5464 if Is_Valid_Renaming (Obj2) then
5465 Obj2 := Renamed_Entity (Entity (Obj2));
5466 else
5467 return False;
5468 end if;
5469 end if;
5471 -- No match if not same node kind (such cases are handled by
5472 -- Denotes_Same_Prefix)
5474 if Nkind (Obj1) /= Nkind (Obj2) then
5475 return False;
5477 -- After handling valid renamings, one of the two names statically
5478 -- denoted a renaming declaration whose renamed object_name is known
5479 -- to denote the same object as the other (RM 6.4.1(6.10/3))
5481 elsif Is_Entity_Name (Obj1) then
5482 if Is_Entity_Name (Obj2) then
5483 return Entity (Obj1) = Entity (Obj2);
5484 else
5485 return False;
5486 end if;
5488 -- Both names are selected_components, their prefixes are known to
5489 -- denote the same object, and their selector_names denote the same
5490 -- component (RM 6.4.1(6.6/3)).
5492 elsif Nkind (Obj1) = N_Selected_Component then
5493 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5494 and then
5495 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
5497 -- Both names are dereferences and the dereferenced names are known to
5498 -- denote the same object (RM 6.4.1(6.7/3))
5500 elsif Nkind (Obj1) = N_Explicit_Dereference then
5501 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
5503 -- Both names are indexed_components, their prefixes are known to denote
5504 -- the same object, and each of the pairs of corresponding index values
5505 -- are either both static expressions with the same static value or both
5506 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
5508 elsif Nkind (Obj1) = N_Indexed_Component then
5509 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
5510 return False;
5511 else
5512 declare
5513 Indx1 : Node_Id;
5514 Indx2 : Node_Id;
5516 begin
5517 Indx1 := First (Expressions (Obj1));
5518 Indx2 := First (Expressions (Obj2));
5519 while Present (Indx1) loop
5521 -- Indexes must denote the same static value or same object
5523 if Is_OK_Static_Expression (Indx1) then
5524 if not Is_OK_Static_Expression (Indx2) then
5525 return False;
5527 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
5528 return False;
5529 end if;
5531 elsif not Denotes_Same_Object (Indx1, Indx2) then
5532 return False;
5533 end if;
5535 Next (Indx1);
5536 Next (Indx2);
5537 end loop;
5539 return True;
5540 end;
5541 end if;
5543 -- Both names are slices, their prefixes are known to denote the same
5544 -- object, and the two slices have statically matching index constraints
5545 -- (RM 6.4.1(6.9/3))
5547 elsif Nkind (Obj1) = N_Slice
5548 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5549 then
5550 declare
5551 Lo1, Lo2, Hi1, Hi2 : Node_Id;
5553 begin
5554 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
5555 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
5557 -- Check whether bounds are statically identical. There is no
5558 -- attempt to detect partial overlap of slices.
5560 return Denotes_Same_Object (Lo1, Lo2)
5561 and then
5562 Denotes_Same_Object (Hi1, Hi2);
5563 end;
5565 -- In the recursion, literals appear as indexes
5567 elsif Nkind (Obj1) = N_Integer_Literal
5568 and then
5569 Nkind (Obj2) = N_Integer_Literal
5570 then
5571 return Intval (Obj1) = Intval (Obj2);
5573 else
5574 return False;
5575 end if;
5576 end Denotes_Same_Object;
5578 -------------------------
5579 -- Denotes_Same_Prefix --
5580 -------------------------
5582 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
5583 begin
5584 if Is_Entity_Name (A1) then
5585 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
5586 and then not Is_Access_Type (Etype (A1))
5587 then
5588 return Denotes_Same_Object (A1, Prefix (A2))
5589 or else Denotes_Same_Prefix (A1, Prefix (A2));
5590 else
5591 return False;
5592 end if;
5594 elsif Is_Entity_Name (A2) then
5595 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
5597 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
5598 and then
5599 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
5600 then
5601 declare
5602 Root1, Root2 : Node_Id;
5603 Depth1, Depth2 : Nat := 0;
5605 begin
5606 Root1 := Prefix (A1);
5607 while not Is_Entity_Name (Root1) loop
5608 if not Nkind_In
5609 (Root1, N_Selected_Component, N_Indexed_Component)
5610 then
5611 return False;
5612 else
5613 Root1 := Prefix (Root1);
5614 end if;
5616 Depth1 := Depth1 + 1;
5617 end loop;
5619 Root2 := Prefix (A2);
5620 while not Is_Entity_Name (Root2) loop
5621 if not Nkind_In (Root2, N_Selected_Component,
5622 N_Indexed_Component)
5623 then
5624 return False;
5625 else
5626 Root2 := Prefix (Root2);
5627 end if;
5629 Depth2 := Depth2 + 1;
5630 end loop;
5632 -- If both have the same depth and they do not denote the same
5633 -- object, they are disjoint and no warning is needed.
5635 if Depth1 = Depth2 then
5636 return False;
5638 elsif Depth1 > Depth2 then
5639 Root1 := Prefix (A1);
5640 for J in 1 .. Depth1 - Depth2 - 1 loop
5641 Root1 := Prefix (Root1);
5642 end loop;
5644 return Denotes_Same_Object (Root1, A2);
5646 else
5647 Root2 := Prefix (A2);
5648 for J in 1 .. Depth2 - Depth1 - 1 loop
5649 Root2 := Prefix (Root2);
5650 end loop;
5652 return Denotes_Same_Object (A1, Root2);
5653 end if;
5654 end;
5656 else
5657 return False;
5658 end if;
5659 end Denotes_Same_Prefix;
5661 ----------------------
5662 -- Denotes_Variable --
5663 ----------------------
5665 function Denotes_Variable (N : Node_Id) return Boolean is
5666 begin
5667 return Is_Variable (N) and then Paren_Count (N) = 0;
5668 end Denotes_Variable;
5670 -----------------------------
5671 -- Depends_On_Discriminant --
5672 -----------------------------
5674 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5675 L : Node_Id;
5676 H : Node_Id;
5678 begin
5679 Get_Index_Bounds (N, L, H);
5680 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5681 end Depends_On_Discriminant;
5683 -------------------------
5684 -- Designate_Same_Unit --
5685 -------------------------
5687 function Designate_Same_Unit
5688 (Name1 : Node_Id;
5689 Name2 : Node_Id) return Boolean
5691 K1 : constant Node_Kind := Nkind (Name1);
5692 K2 : constant Node_Kind := Nkind (Name2);
5694 function Prefix_Node (N : Node_Id) return Node_Id;
5695 -- Returns the parent unit name node of a defining program unit name
5696 -- or the prefix if N is a selected component or an expanded name.
5698 function Select_Node (N : Node_Id) return Node_Id;
5699 -- Returns the defining identifier node of a defining program unit
5700 -- name or the selector node if N is a selected component or an
5701 -- expanded name.
5703 -----------------
5704 -- Prefix_Node --
5705 -----------------
5707 function Prefix_Node (N : Node_Id) return Node_Id is
5708 begin
5709 if Nkind (N) = N_Defining_Program_Unit_Name then
5710 return Name (N);
5711 else
5712 return Prefix (N);
5713 end if;
5714 end Prefix_Node;
5716 -----------------
5717 -- Select_Node --
5718 -----------------
5720 function Select_Node (N : Node_Id) return Node_Id is
5721 begin
5722 if Nkind (N) = N_Defining_Program_Unit_Name then
5723 return Defining_Identifier (N);
5724 else
5725 return Selector_Name (N);
5726 end if;
5727 end Select_Node;
5729 -- Start of processing for Designate_Same_Unit
5731 begin
5732 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
5733 and then
5734 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
5735 then
5736 return Chars (Name1) = Chars (Name2);
5738 elsif Nkind_In (K1, N_Expanded_Name,
5739 N_Selected_Component,
5740 N_Defining_Program_Unit_Name)
5741 and then
5742 Nkind_In (K2, N_Expanded_Name,
5743 N_Selected_Component,
5744 N_Defining_Program_Unit_Name)
5745 then
5746 return
5747 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5748 and then
5749 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5751 else
5752 return False;
5753 end if;
5754 end Designate_Same_Unit;
5756 ------------------------------------------
5757 -- function Dynamic_Accessibility_Level --
5758 ------------------------------------------
5760 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5761 E : Entity_Id;
5762 Loc : constant Source_Ptr := Sloc (Expr);
5764 function Make_Level_Literal (Level : Uint) return Node_Id;
5765 -- Construct an integer literal representing an accessibility level
5766 -- with its type set to Natural.
5768 ------------------------
5769 -- Make_Level_Literal --
5770 ------------------------
5772 function Make_Level_Literal (Level : Uint) return Node_Id is
5773 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5774 begin
5775 Set_Etype (Result, Standard_Natural);
5776 return Result;
5777 end Make_Level_Literal;
5779 -- Start of processing for Dynamic_Accessibility_Level
5781 begin
5782 if Is_Entity_Name (Expr) then
5783 E := Entity (Expr);
5785 if Present (Renamed_Object (E)) then
5786 return Dynamic_Accessibility_Level (Renamed_Object (E));
5787 end if;
5789 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5790 if Present (Extra_Accessibility (E)) then
5791 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5792 end if;
5793 end if;
5794 end if;
5796 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5798 case Nkind (Expr) is
5800 -- For access discriminant, the level of the enclosing object
5802 when N_Selected_Component =>
5803 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5804 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5805 E_Anonymous_Access_Type
5806 then
5807 return Make_Level_Literal (Object_Access_Level (Expr));
5808 end if;
5810 when N_Attribute_Reference =>
5811 case Get_Attribute_Id (Attribute_Name (Expr)) is
5813 -- For X'Access, the level of the prefix X
5815 when Attribute_Access =>
5816 return Make_Level_Literal
5817 (Object_Access_Level (Prefix (Expr)));
5819 -- Treat the unchecked attributes as library-level
5821 when Attribute_Unchecked_Access |
5822 Attribute_Unrestricted_Access =>
5823 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5825 -- No other access-valued attributes
5827 when others =>
5828 raise Program_Error;
5829 end case;
5831 when N_Allocator =>
5833 -- Unimplemented: depends on context. As an actual parameter where
5834 -- formal type is anonymous, use
5835 -- Scope_Depth (Current_Scope) + 1.
5836 -- For other cases, see 3.10.2(14/3) and following. ???
5838 null;
5840 when N_Type_Conversion =>
5841 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5843 -- Handle type conversions introduced for a rename of an
5844 -- Ada 2012 stand-alone object of an anonymous access type.
5846 return Dynamic_Accessibility_Level (Expression (Expr));
5847 end if;
5849 when others =>
5850 null;
5851 end case;
5853 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5854 end Dynamic_Accessibility_Level;
5856 -----------------------------------
5857 -- Effective_Extra_Accessibility --
5858 -----------------------------------
5860 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5861 begin
5862 if Present (Renamed_Object (Id))
5863 and then Is_Entity_Name (Renamed_Object (Id))
5864 then
5865 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5866 else
5867 return Extra_Accessibility (Id);
5868 end if;
5869 end Effective_Extra_Accessibility;
5871 -----------------------------
5872 -- Effective_Reads_Enabled --
5873 -----------------------------
5875 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5876 begin
5877 return Has_Enabled_Property (Id, Name_Effective_Reads);
5878 end Effective_Reads_Enabled;
5880 ------------------------------
5881 -- Effective_Writes_Enabled --
5882 ------------------------------
5884 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5885 begin
5886 return Has_Enabled_Property (Id, Name_Effective_Writes);
5887 end Effective_Writes_Enabled;
5889 ------------------------------
5890 -- Enclosing_Comp_Unit_Node --
5891 ------------------------------
5893 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5894 Current_Node : Node_Id;
5896 begin
5897 Current_Node := N;
5898 while Present (Current_Node)
5899 and then Nkind (Current_Node) /= N_Compilation_Unit
5900 loop
5901 Current_Node := Parent (Current_Node);
5902 end loop;
5904 if Nkind (Current_Node) /= N_Compilation_Unit then
5905 return Empty;
5906 else
5907 return Current_Node;
5908 end if;
5909 end Enclosing_Comp_Unit_Node;
5911 --------------------------
5912 -- Enclosing_CPP_Parent --
5913 --------------------------
5915 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5916 Parent_Typ : Entity_Id := Typ;
5918 begin
5919 while not Is_CPP_Class (Parent_Typ)
5920 and then Etype (Parent_Typ) /= Parent_Typ
5921 loop
5922 Parent_Typ := Etype (Parent_Typ);
5924 if Is_Private_Type (Parent_Typ) then
5925 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5926 end if;
5927 end loop;
5929 pragma Assert (Is_CPP_Class (Parent_Typ));
5930 return Parent_Typ;
5931 end Enclosing_CPP_Parent;
5933 ---------------------------
5934 -- Enclosing_Declaration --
5935 ---------------------------
5937 function Enclosing_Declaration (N : Node_Id) return Node_Id is
5938 Decl : Node_Id := N;
5940 begin
5941 while Present (Decl)
5942 and then not (Nkind (Decl) in N_Declaration
5943 or else
5944 Nkind (Decl) in N_Later_Decl_Item)
5945 loop
5946 Decl := Parent (Decl);
5947 end loop;
5949 return Decl;
5950 end Enclosing_Declaration;
5952 ----------------------------
5953 -- Enclosing_Generic_Body --
5954 ----------------------------
5956 function Enclosing_Generic_Body
5957 (N : Node_Id) return Node_Id
5959 P : Node_Id;
5960 Decl : Node_Id;
5961 Spec : Node_Id;
5963 begin
5964 P := Parent (N);
5965 while Present (P) loop
5966 if Nkind (P) = N_Package_Body
5967 or else Nkind (P) = N_Subprogram_Body
5968 then
5969 Spec := Corresponding_Spec (P);
5971 if Present (Spec) then
5972 Decl := Unit_Declaration_Node (Spec);
5974 if Nkind (Decl) = N_Generic_Package_Declaration
5975 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5976 then
5977 return P;
5978 end if;
5979 end if;
5980 end if;
5982 P := Parent (P);
5983 end loop;
5985 return Empty;
5986 end Enclosing_Generic_Body;
5988 ----------------------------
5989 -- Enclosing_Generic_Unit --
5990 ----------------------------
5992 function Enclosing_Generic_Unit
5993 (N : Node_Id) return Node_Id
5995 P : Node_Id;
5996 Decl : Node_Id;
5997 Spec : Node_Id;
5999 begin
6000 P := Parent (N);
6001 while Present (P) loop
6002 if Nkind (P) = N_Generic_Package_Declaration
6003 or else Nkind (P) = N_Generic_Subprogram_Declaration
6004 then
6005 return P;
6007 elsif Nkind (P) = N_Package_Body
6008 or else Nkind (P) = N_Subprogram_Body
6009 then
6010 Spec := Corresponding_Spec (P);
6012 if Present (Spec) then
6013 Decl := Unit_Declaration_Node (Spec);
6015 if Nkind (Decl) = N_Generic_Package_Declaration
6016 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6017 then
6018 return Decl;
6019 end if;
6020 end if;
6021 end if;
6023 P := Parent (P);
6024 end loop;
6026 return Empty;
6027 end Enclosing_Generic_Unit;
6029 -------------------------------
6030 -- Enclosing_Lib_Unit_Entity --
6031 -------------------------------
6033 function Enclosing_Lib_Unit_Entity
6034 (E : Entity_Id := Current_Scope) return Entity_Id
6036 Unit_Entity : Entity_Id;
6038 begin
6039 -- Look for enclosing library unit entity by following scope links.
6040 -- Equivalent to, but faster than indexing through the scope stack.
6042 Unit_Entity := E;
6043 while (Present (Scope (Unit_Entity))
6044 and then Scope (Unit_Entity) /= Standard_Standard)
6045 and not Is_Child_Unit (Unit_Entity)
6046 loop
6047 Unit_Entity := Scope (Unit_Entity);
6048 end loop;
6050 return Unit_Entity;
6051 end Enclosing_Lib_Unit_Entity;
6053 -----------------------------
6054 -- Enclosing_Lib_Unit_Node --
6055 -----------------------------
6057 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
6058 Encl_Unit : Node_Id;
6060 begin
6061 Encl_Unit := Enclosing_Comp_Unit_Node (N);
6062 while Present (Encl_Unit)
6063 and then Nkind (Unit (Encl_Unit)) = N_Subunit
6064 loop
6065 Encl_Unit := Library_Unit (Encl_Unit);
6066 end loop;
6068 pragma Assert (Nkind (Encl_Unit) = N_Compilation_Unit);
6069 return Encl_Unit;
6070 end Enclosing_Lib_Unit_Node;
6072 -----------------------
6073 -- Enclosing_Package --
6074 -----------------------
6076 function Enclosing_Package (E : Entity_Id) return Entity_Id is
6077 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6079 begin
6080 if Dynamic_Scope = Standard_Standard then
6081 return Standard_Standard;
6083 elsif Dynamic_Scope = Empty then
6084 return Empty;
6086 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
6087 E_Generic_Package)
6088 then
6089 return Dynamic_Scope;
6091 else
6092 return Enclosing_Package (Dynamic_Scope);
6093 end if;
6094 end Enclosing_Package;
6096 -------------------------------------
6097 -- Enclosing_Package_Or_Subprogram --
6098 -------------------------------------
6100 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
6101 S : Entity_Id;
6103 begin
6104 S := Scope (E);
6105 while Present (S) loop
6106 if Is_Package_Or_Generic_Package (S)
6107 or else Ekind (S) = E_Package_Body
6108 then
6109 return S;
6111 elsif Is_Subprogram_Or_Generic_Subprogram (S)
6112 or else Ekind (S) = E_Subprogram_Body
6113 then
6114 return S;
6116 else
6117 S := Scope (S);
6118 end if;
6119 end loop;
6121 return Empty;
6122 end Enclosing_Package_Or_Subprogram;
6124 --------------------------
6125 -- Enclosing_Subprogram --
6126 --------------------------
6128 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
6129 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6131 begin
6132 if Dynamic_Scope = Standard_Standard then
6133 return Empty;
6135 elsif Dynamic_Scope = Empty then
6136 return Empty;
6138 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
6139 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
6141 elsif Ekind (Dynamic_Scope) = E_Block
6142 or else Ekind (Dynamic_Scope) = E_Return_Statement
6143 then
6144 return Enclosing_Subprogram (Dynamic_Scope);
6146 elsif Ekind (Dynamic_Scope) = E_Task_Type then
6147 return Get_Task_Body_Procedure (Dynamic_Scope);
6149 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
6150 and then Present (Full_View (Dynamic_Scope))
6151 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
6152 then
6153 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
6155 -- No body is generated if the protected operation is eliminated
6157 elsif Convention (Dynamic_Scope) = Convention_Protected
6158 and then not Is_Eliminated (Dynamic_Scope)
6159 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
6160 then
6161 return Protected_Body_Subprogram (Dynamic_Scope);
6163 else
6164 return Dynamic_Scope;
6165 end if;
6166 end Enclosing_Subprogram;
6168 ------------------------
6169 -- Ensure_Freeze_Node --
6170 ------------------------
6172 procedure Ensure_Freeze_Node (E : Entity_Id) is
6173 FN : Node_Id;
6174 begin
6175 if No (Freeze_Node (E)) then
6176 FN := Make_Freeze_Entity (Sloc (E));
6177 Set_Has_Delayed_Freeze (E);
6178 Set_Freeze_Node (E, FN);
6179 Set_Access_Types_To_Process (FN, No_Elist);
6180 Set_TSS_Elist (FN, No_Elist);
6181 Set_Entity (FN, E);
6182 end if;
6183 end Ensure_Freeze_Node;
6185 ----------------
6186 -- Enter_Name --
6187 ----------------
6189 procedure Enter_Name (Def_Id : Entity_Id) is
6190 C : constant Entity_Id := Current_Entity (Def_Id);
6191 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
6192 S : constant Entity_Id := Current_Scope;
6194 begin
6195 Generate_Definition (Def_Id);
6197 -- Add new name to current scope declarations. Check for duplicate
6198 -- declaration, which may or may not be a genuine error.
6200 if Present (E) then
6202 -- Case of previous entity entered because of a missing declaration
6203 -- or else a bad subtype indication. Best is to use the new entity,
6204 -- and make the previous one invisible.
6206 if Etype (E) = Any_Type then
6207 Set_Is_Immediately_Visible (E, False);
6209 -- Case of renaming declaration constructed for package instances.
6210 -- if there is an explicit declaration with the same identifier,
6211 -- the renaming is not immediately visible any longer, but remains
6212 -- visible through selected component notation.
6214 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
6215 and then not Comes_From_Source (E)
6216 then
6217 Set_Is_Immediately_Visible (E, False);
6219 -- The new entity may be the package renaming, which has the same
6220 -- same name as a generic formal which has been seen already.
6222 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
6223 and then not Comes_From_Source (Def_Id)
6224 then
6225 Set_Is_Immediately_Visible (E, False);
6227 -- For a fat pointer corresponding to a remote access to subprogram,
6228 -- we use the same identifier as the RAS type, so that the proper
6229 -- name appears in the stub. This type is only retrieved through
6230 -- the RAS type and never by visibility, and is not added to the
6231 -- visibility list (see below).
6233 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
6234 and then Ekind (Def_Id) = E_Record_Type
6235 and then Present (Corresponding_Remote_Type (Def_Id))
6236 then
6237 null;
6239 -- Case of an implicit operation or derived literal. The new entity
6240 -- hides the implicit one, which is removed from all visibility,
6241 -- i.e. the entity list of its scope, and homonym chain of its name.
6243 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
6244 or else Is_Internal (E)
6245 then
6246 declare
6247 Decl : constant Node_Id := Parent (E);
6248 Prev : Entity_Id;
6249 Prev_Vis : Entity_Id;
6251 begin
6252 -- If E is an implicit declaration, it cannot be the first
6253 -- entity in the scope.
6255 Prev := First_Entity (Current_Scope);
6256 while Present (Prev) and then Next_Entity (Prev) /= E loop
6257 Next_Entity (Prev);
6258 end loop;
6260 if No (Prev) then
6262 -- If E is not on the entity chain of the current scope,
6263 -- it is an implicit declaration in the generic formal
6264 -- part of a generic subprogram. When analyzing the body,
6265 -- the generic formals are visible but not on the entity
6266 -- chain of the subprogram. The new entity will become
6267 -- the visible one in the body.
6269 pragma Assert
6270 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
6271 null;
6273 else
6274 Set_Next_Entity (Prev, Next_Entity (E));
6276 if No (Next_Entity (Prev)) then
6277 Set_Last_Entity (Current_Scope, Prev);
6278 end if;
6280 if E = Current_Entity (E) then
6281 Prev_Vis := Empty;
6283 else
6284 Prev_Vis := Current_Entity (E);
6285 while Homonym (Prev_Vis) /= E loop
6286 Prev_Vis := Homonym (Prev_Vis);
6287 end loop;
6288 end if;
6290 if Present (Prev_Vis) then
6292 -- Skip E in the visibility chain
6294 Set_Homonym (Prev_Vis, Homonym (E));
6296 else
6297 Set_Name_Entity_Id (Chars (E), Homonym (E));
6298 end if;
6299 end if;
6300 end;
6302 -- This section of code could use a comment ???
6304 elsif Present (Etype (E))
6305 and then Is_Concurrent_Type (Etype (E))
6306 and then E = Def_Id
6307 then
6308 return;
6310 -- If the homograph is a protected component renaming, it should not
6311 -- be hiding the current entity. Such renamings are treated as weak
6312 -- declarations.
6314 elsif Is_Prival (E) then
6315 Set_Is_Immediately_Visible (E, False);
6317 -- In this case the current entity is a protected component renaming.
6318 -- Perform minimal decoration by setting the scope and return since
6319 -- the prival should not be hiding other visible entities.
6321 elsif Is_Prival (Def_Id) then
6322 Set_Scope (Def_Id, Current_Scope);
6323 return;
6325 -- Analogous to privals, the discriminal generated for an entry index
6326 -- parameter acts as a weak declaration. Perform minimal decoration
6327 -- to avoid bogus errors.
6329 elsif Is_Discriminal (Def_Id)
6330 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
6331 then
6332 Set_Scope (Def_Id, Current_Scope);
6333 return;
6335 -- In the body or private part of an instance, a type extension may
6336 -- introduce a component with the same name as that of an actual. The
6337 -- legality rule is not enforced, but the semantics of the full type
6338 -- with two components of same name are not clear at this point???
6340 elsif In_Instance_Not_Visible then
6341 null;
6343 -- When compiling a package body, some child units may have become
6344 -- visible. They cannot conflict with local entities that hide them.
6346 elsif Is_Child_Unit (E)
6347 and then In_Open_Scopes (Scope (E))
6348 and then not Is_Immediately_Visible (E)
6349 then
6350 null;
6352 -- Conversely, with front-end inlining we may compile the parent body
6353 -- first, and a child unit subsequently. The context is now the
6354 -- parent spec, and body entities are not visible.
6356 elsif Is_Child_Unit (Def_Id)
6357 and then Is_Package_Body_Entity (E)
6358 and then not In_Package_Body (Current_Scope)
6359 then
6360 null;
6362 -- Case of genuine duplicate declaration
6364 else
6365 Error_Msg_Sloc := Sloc (E);
6367 -- If the previous declaration is an incomplete type declaration
6368 -- this may be an attempt to complete it with a private type. The
6369 -- following avoids confusing cascaded errors.
6371 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
6372 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
6373 then
6374 Error_Msg_N
6375 ("incomplete type cannot be completed with a private " &
6376 "declaration", Parent (Def_Id));
6377 Set_Is_Immediately_Visible (E, False);
6378 Set_Full_View (E, Def_Id);
6380 -- An inherited component of a record conflicts with a new
6381 -- discriminant. The discriminant is inserted first in the scope,
6382 -- but the error should be posted on it, not on the component.
6384 elsif Ekind (E) = E_Discriminant
6385 and then Present (Scope (Def_Id))
6386 and then Scope (Def_Id) /= Current_Scope
6387 then
6388 Error_Msg_Sloc := Sloc (Def_Id);
6389 Error_Msg_N ("& conflicts with declaration#", E);
6390 return;
6392 -- If the name of the unit appears in its own context clause, a
6393 -- dummy package with the name has already been created, and the
6394 -- error emitted. Try to continue quietly.
6396 elsif Error_Posted (E)
6397 and then Sloc (E) = No_Location
6398 and then Nkind (Parent (E)) = N_Package_Specification
6399 and then Current_Scope = Standard_Standard
6400 then
6401 Set_Scope (Def_Id, Current_Scope);
6402 return;
6404 else
6405 Error_Msg_N ("& conflicts with declaration#", Def_Id);
6407 -- Avoid cascaded messages with duplicate components in
6408 -- derived types.
6410 if Ekind_In (E, E_Component, E_Discriminant) then
6411 return;
6412 end if;
6413 end if;
6415 if Nkind (Parent (Parent (Def_Id))) =
6416 N_Generic_Subprogram_Declaration
6417 and then Def_Id =
6418 Defining_Entity (Specification (Parent (Parent (Def_Id))))
6419 then
6420 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
6421 end if;
6423 -- If entity is in standard, then we are in trouble, because it
6424 -- means that we have a library package with a duplicated name.
6425 -- That's hard to recover from, so abort.
6427 if S = Standard_Standard then
6428 raise Unrecoverable_Error;
6430 -- Otherwise we continue with the declaration. Having two
6431 -- identical declarations should not cause us too much trouble.
6433 else
6434 null;
6435 end if;
6436 end if;
6437 end if;
6439 -- If we fall through, declaration is OK, at least OK enough to continue
6441 -- If Def_Id is a discriminant or a record component we are in the midst
6442 -- of inheriting components in a derived record definition. Preserve
6443 -- their Ekind and Etype.
6445 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
6446 null;
6448 -- If a type is already set, leave it alone (happens when a type
6449 -- declaration is reanalyzed following a call to the optimizer).
6451 elsif Present (Etype (Def_Id)) then
6452 null;
6454 -- Otherwise, the kind E_Void insures that premature uses of the entity
6455 -- will be detected. Any_Type insures that no cascaded errors will occur
6457 else
6458 Set_Ekind (Def_Id, E_Void);
6459 Set_Etype (Def_Id, Any_Type);
6460 end if;
6462 -- Inherited discriminants and components in derived record types are
6463 -- immediately visible. Itypes are not.
6465 -- Unless the Itype is for a record type with a corresponding remote
6466 -- type (what is that about, it was not commented ???)
6468 if Ekind_In (Def_Id, E_Discriminant, E_Component)
6469 or else
6470 ((not Is_Record_Type (Def_Id)
6471 or else No (Corresponding_Remote_Type (Def_Id)))
6472 and then not Is_Itype (Def_Id))
6473 then
6474 Set_Is_Immediately_Visible (Def_Id);
6475 Set_Current_Entity (Def_Id);
6476 end if;
6478 Set_Homonym (Def_Id, C);
6479 Append_Entity (Def_Id, S);
6480 Set_Public_Status (Def_Id);
6482 -- Declaring a homonym is not allowed in SPARK ...
6484 if Present (C) and then Restriction_Check_Required (SPARK_05) then
6485 declare
6486 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
6487 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
6488 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
6490 begin
6491 -- ... unless the new declaration is in a subprogram, and the
6492 -- visible declaration is a variable declaration or a parameter
6493 -- specification outside that subprogram.
6495 if Present (Enclosing_Subp)
6496 and then Nkind_In (Parent (C), N_Object_Declaration,
6497 N_Parameter_Specification)
6498 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
6499 then
6500 null;
6502 -- ... or the new declaration is in a package, and the visible
6503 -- declaration occurs outside that package.
6505 elsif Present (Enclosing_Pack)
6506 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
6507 then
6508 null;
6510 -- ... or the new declaration is a component declaration in a
6511 -- record type definition.
6513 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
6514 null;
6516 -- Don't issue error for non-source entities
6518 elsif Comes_From_Source (Def_Id)
6519 and then Comes_From_Source (C)
6520 then
6521 Error_Msg_Sloc := Sloc (C);
6522 Check_SPARK_05_Restriction
6523 ("redeclaration of identifier &#", Def_Id);
6524 end if;
6525 end;
6526 end if;
6528 -- Warn if new entity hides an old one
6530 if Warn_On_Hiding and then Present (C)
6532 -- Don't warn for record components since they always have a well
6533 -- defined scope which does not confuse other uses. Note that in
6534 -- some cases, Ekind has not been set yet.
6536 and then Ekind (C) /= E_Component
6537 and then Ekind (C) /= E_Discriminant
6538 and then Nkind (Parent (C)) /= N_Component_Declaration
6539 and then Ekind (Def_Id) /= E_Component
6540 and then Ekind (Def_Id) /= E_Discriminant
6541 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
6543 -- Don't warn for one character variables. It is too common to use
6544 -- such variables as locals and will just cause too many false hits.
6546 and then Length_Of_Name (Chars (C)) /= 1
6548 -- Don't warn for non-source entities
6550 and then Comes_From_Source (C)
6551 and then Comes_From_Source (Def_Id)
6553 -- Don't warn unless entity in question is in extended main source
6555 and then In_Extended_Main_Source_Unit (Def_Id)
6557 -- Finally, the hidden entity must be either immediately visible or
6558 -- use visible (i.e. from a used package).
6560 and then
6561 (Is_Immediately_Visible (C)
6562 or else
6563 Is_Potentially_Use_Visible (C))
6564 then
6565 Error_Msg_Sloc := Sloc (C);
6566 Error_Msg_N ("declaration hides &#?h?", Def_Id);
6567 end if;
6568 end Enter_Name;
6570 ---------------
6571 -- Entity_Of --
6572 ---------------
6574 function Entity_Of (N : Node_Id) return Entity_Id is
6575 Id : Entity_Id;
6577 begin
6578 Id := Empty;
6580 if Is_Entity_Name (N) then
6581 Id := Entity (N);
6583 -- Follow a possible chain of renamings to reach the root renamed
6584 -- object.
6586 while Present (Id)
6587 and then Is_Object (Id)
6588 and then Present (Renamed_Object (Id))
6589 loop
6590 if Is_Entity_Name (Renamed_Object (Id)) then
6591 Id := Entity (Renamed_Object (Id));
6592 else
6593 Id := Empty;
6594 exit;
6595 end if;
6596 end loop;
6597 end if;
6599 return Id;
6600 end Entity_Of;
6602 --------------------------
6603 -- Explain_Limited_Type --
6604 --------------------------
6606 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
6607 C : Entity_Id;
6609 begin
6610 -- For array, component type must be limited
6612 if Is_Array_Type (T) then
6613 Error_Msg_Node_2 := T;
6614 Error_Msg_NE
6615 ("\component type& of type& is limited", N, Component_Type (T));
6616 Explain_Limited_Type (Component_Type (T), N);
6618 elsif Is_Record_Type (T) then
6620 -- No need for extra messages if explicit limited record
6622 if Is_Limited_Record (Base_Type (T)) then
6623 return;
6624 end if;
6626 -- Otherwise find a limited component. Check only components that
6627 -- come from source, or inherited components that appear in the
6628 -- source of the ancestor.
6630 C := First_Component (T);
6631 while Present (C) loop
6632 if Is_Limited_Type (Etype (C))
6633 and then
6634 (Comes_From_Source (C)
6635 or else
6636 (Present (Original_Record_Component (C))
6637 and then
6638 Comes_From_Source (Original_Record_Component (C))))
6639 then
6640 Error_Msg_Node_2 := T;
6641 Error_Msg_NE ("\component& of type& has limited type", N, C);
6642 Explain_Limited_Type (Etype (C), N);
6643 return;
6644 end if;
6646 Next_Component (C);
6647 end loop;
6649 -- The type may be declared explicitly limited, even if no component
6650 -- of it is limited, in which case we fall out of the loop.
6651 return;
6652 end if;
6653 end Explain_Limited_Type;
6655 -------------------------------
6656 -- Extensions_Visible_Status --
6657 -------------------------------
6659 function Extensions_Visible_Status
6660 (Id : Entity_Id) return Extensions_Visible_Mode
6662 Arg : Node_Id;
6663 Decl : Node_Id;
6664 Expr : Node_Id;
6665 Prag : Node_Id;
6666 Subp : Entity_Id;
6668 begin
6669 -- When a formal parameter is subject to Extensions_Visible, the pragma
6670 -- is stored in the contract of related subprogram.
6672 if Is_Formal (Id) then
6673 Subp := Scope (Id);
6675 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
6676 Subp := Id;
6678 -- No other construct carries this pragma
6680 else
6681 return Extensions_Visible_None;
6682 end if;
6684 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
6686 -- In certain cases analysis may request the Extensions_Visible status
6687 -- of an expression function before the pragma has been analyzed yet.
6688 -- Inspect the declarative items after the expression function looking
6689 -- for the pragma (if any).
6691 if No (Prag) and then Is_Expression_Function (Subp) then
6692 Decl := Next (Unit_Declaration_Node (Subp));
6693 while Present (Decl) loop
6694 if Nkind (Decl) = N_Pragma
6695 and then Pragma_Name_Mapped (Decl) = Name_Extensions_Visible
6696 then
6697 Prag := Decl;
6698 exit;
6700 -- A source construct ends the region where Extensions_Visible may
6701 -- appear, stop the traversal. An expanded expression function is
6702 -- no longer a source construct, but it must still be recognized.
6704 elsif Comes_From_Source (Decl)
6705 or else
6706 (Nkind_In (Decl, N_Subprogram_Body,
6707 N_Subprogram_Declaration)
6708 and then Is_Expression_Function (Defining_Entity (Decl)))
6709 then
6710 exit;
6711 end if;
6713 Next (Decl);
6714 end loop;
6715 end if;
6717 -- Extract the value from the Boolean expression (if any)
6719 if Present (Prag) then
6720 Arg := First (Pragma_Argument_Associations (Prag));
6722 if Present (Arg) then
6723 Expr := Get_Pragma_Arg (Arg);
6725 -- When the associated subprogram is an expression function, the
6726 -- argument of the pragma may not have been analyzed.
6728 if not Analyzed (Expr) then
6729 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6730 end if;
6732 -- Guard against cascading errors when the argument of pragma
6733 -- Extensions_Visible is not a valid static Boolean expression.
6735 if Error_Posted (Expr) then
6736 return Extensions_Visible_None;
6738 elsif Is_True (Expr_Value (Expr)) then
6739 return Extensions_Visible_True;
6741 else
6742 return Extensions_Visible_False;
6743 end if;
6745 -- Otherwise the aspect or pragma defaults to True
6747 else
6748 return Extensions_Visible_True;
6749 end if;
6751 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
6752 -- directly specified. In SPARK code, its value defaults to "False".
6754 elsif SPARK_Mode = On then
6755 return Extensions_Visible_False;
6757 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
6758 -- "True".
6760 else
6761 return Extensions_Visible_True;
6762 end if;
6763 end Extensions_Visible_Status;
6765 -----------------
6766 -- Find_Actual --
6767 -----------------
6769 procedure Find_Actual
6770 (N : Node_Id;
6771 Formal : out Entity_Id;
6772 Call : out Node_Id)
6774 Context : constant Node_Id := Parent (N);
6775 Actual : Node_Id;
6776 Call_Nam : Node_Id;
6778 begin
6779 if Nkind_In (Context, N_Indexed_Component, N_Selected_Component)
6780 and then N = Prefix (Context)
6781 then
6782 Find_Actual (Context, Formal, Call);
6783 return;
6785 elsif Nkind (Context) = N_Parameter_Association
6786 and then N = Explicit_Actual_Parameter (Context)
6787 then
6788 Call := Parent (Context);
6790 elsif Nkind_In (Context, N_Entry_Call_Statement,
6791 N_Function_Call,
6792 N_Procedure_Call_Statement)
6793 then
6794 Call := Context;
6796 else
6797 Formal := Empty;
6798 Call := Empty;
6799 return;
6800 end if;
6802 -- If we have a call to a subprogram look for the parameter. Note that
6803 -- we exclude overloaded calls, since we don't know enough to be sure
6804 -- of giving the right answer in this case.
6806 if Nkind_In (Call, N_Entry_Call_Statement,
6807 N_Function_Call,
6808 N_Procedure_Call_Statement)
6809 then
6810 Call_Nam := Name (Call);
6812 -- A call to a protected or task entry appears as a selected
6813 -- component rather than an expanded name.
6815 if Nkind (Call_Nam) = N_Selected_Component then
6816 Call_Nam := Selector_Name (Call_Nam);
6817 end if;
6819 if Is_Entity_Name (Call_Nam)
6820 and then Present (Entity (Call_Nam))
6821 and then Is_Overloadable (Entity (Call_Nam))
6822 and then not Is_Overloaded (Call_Nam)
6823 then
6824 -- If node is name in call it is not an actual
6826 if N = Call_Nam then
6827 Formal := Empty;
6828 Call := Empty;
6829 return;
6830 end if;
6832 -- Fall here if we are definitely a parameter
6834 Actual := First_Actual (Call);
6835 Formal := First_Formal (Entity (Call_Nam));
6836 while Present (Formal) and then Present (Actual) loop
6837 if Actual = N then
6838 return;
6840 -- An actual that is the prefix in a prefixed call may have
6841 -- been rewritten in the call, after the deferred reference
6842 -- was collected. Check if sloc and kinds and names match.
6844 elsif Sloc (Actual) = Sloc (N)
6845 and then Nkind (Actual) = N_Identifier
6846 and then Nkind (Actual) = Nkind (N)
6847 and then Chars (Actual) = Chars (N)
6848 then
6849 return;
6851 else
6852 Actual := Next_Actual (Actual);
6853 Formal := Next_Formal (Formal);
6854 end if;
6855 end loop;
6856 end if;
6857 end if;
6859 -- Fall through here if we did not find matching actual
6861 Formal := Empty;
6862 Call := Empty;
6863 end Find_Actual;
6865 ---------------------------
6866 -- Find_Body_Discriminal --
6867 ---------------------------
6869 function Find_Body_Discriminal
6870 (Spec_Discriminant : Entity_Id) return Entity_Id
6872 Tsk : Entity_Id;
6873 Disc : Entity_Id;
6875 begin
6876 -- If expansion is suppressed, then the scope can be the concurrent type
6877 -- itself rather than a corresponding concurrent record type.
6879 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6880 Tsk := Scope (Spec_Discriminant);
6882 else
6883 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6885 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6886 end if;
6888 -- Find discriminant of original concurrent type, and use its current
6889 -- discriminal, which is the renaming within the task/protected body.
6891 Disc := First_Discriminant (Tsk);
6892 while Present (Disc) loop
6893 if Chars (Disc) = Chars (Spec_Discriminant) then
6894 return Discriminal (Disc);
6895 end if;
6897 Next_Discriminant (Disc);
6898 end loop;
6900 -- That loop should always succeed in finding a matching entry and
6901 -- returning. Fatal error if not.
6903 raise Program_Error;
6904 end Find_Body_Discriminal;
6906 -------------------------------------
6907 -- Find_Corresponding_Discriminant --
6908 -------------------------------------
6910 function Find_Corresponding_Discriminant
6911 (Id : Node_Id;
6912 Typ : Entity_Id) return Entity_Id
6914 Par_Disc : Entity_Id;
6915 Old_Disc : Entity_Id;
6916 New_Disc : Entity_Id;
6918 begin
6919 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6921 -- The original type may currently be private, and the discriminant
6922 -- only appear on its full view.
6924 if Is_Private_Type (Scope (Par_Disc))
6925 and then not Has_Discriminants (Scope (Par_Disc))
6926 and then Present (Full_View (Scope (Par_Disc)))
6927 then
6928 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6929 else
6930 Old_Disc := First_Discriminant (Scope (Par_Disc));
6931 end if;
6933 if Is_Class_Wide_Type (Typ) then
6934 New_Disc := First_Discriminant (Root_Type (Typ));
6935 else
6936 New_Disc := First_Discriminant (Typ);
6937 end if;
6939 while Present (Old_Disc) and then Present (New_Disc) loop
6940 if Old_Disc = Par_Disc then
6941 return New_Disc;
6942 end if;
6944 Next_Discriminant (Old_Disc);
6945 Next_Discriminant (New_Disc);
6946 end loop;
6948 -- Should always find it
6950 raise Program_Error;
6951 end Find_Corresponding_Discriminant;
6953 ----------------------------------
6954 -- Find_Enclosing_Iterator_Loop --
6955 ----------------------------------
6957 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
6958 Constr : Node_Id;
6959 S : Entity_Id;
6961 begin
6962 -- Traverse the scope chain looking for an iterator loop. Such loops are
6963 -- usually transformed into blocks, hence the use of Original_Node.
6965 S := Id;
6966 while Present (S) and then S /= Standard_Standard loop
6967 if Ekind (S) = E_Loop
6968 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
6969 then
6970 Constr := Original_Node (Label_Construct (Parent (S)));
6972 if Nkind (Constr) = N_Loop_Statement
6973 and then Present (Iteration_Scheme (Constr))
6974 and then Nkind (Iterator_Specification
6975 (Iteration_Scheme (Constr))) =
6976 N_Iterator_Specification
6977 then
6978 return S;
6979 end if;
6980 end if;
6982 S := Scope (S);
6983 end loop;
6985 return Empty;
6986 end Find_Enclosing_Iterator_Loop;
6988 ------------------------------------
6989 -- Find_Loop_In_Conditional_Block --
6990 ------------------------------------
6992 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
6993 Stmt : Node_Id;
6995 begin
6996 Stmt := N;
6998 if Nkind (Stmt) = N_If_Statement then
6999 Stmt := First (Then_Statements (Stmt));
7000 end if;
7002 pragma Assert (Nkind (Stmt) = N_Block_Statement);
7004 -- Inspect the statements of the conditional block. In general the loop
7005 -- should be the first statement in the statement sequence of the block,
7006 -- but the finalization machinery may have introduced extra object
7007 -- declarations.
7009 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
7010 while Present (Stmt) loop
7011 if Nkind (Stmt) = N_Loop_Statement then
7012 return Stmt;
7013 end if;
7015 Next (Stmt);
7016 end loop;
7018 -- The expansion of attribute 'Loop_Entry produced a malformed block
7020 raise Program_Error;
7021 end Find_Loop_In_Conditional_Block;
7023 --------------------------
7024 -- Find_Overlaid_Entity --
7025 --------------------------
7027 procedure Find_Overlaid_Entity
7028 (N : Node_Id;
7029 Ent : out Entity_Id;
7030 Off : out Boolean)
7032 Expr : Node_Id;
7034 begin
7035 -- We are looking for one of the two following forms:
7037 -- for X'Address use Y'Address
7039 -- or
7041 -- Const : constant Address := expr;
7042 -- ...
7043 -- for X'Address use Const;
7045 -- In the second case, the expr is either Y'Address, or recursively a
7046 -- constant that eventually references Y'Address.
7048 Ent := Empty;
7049 Off := False;
7051 if Nkind (N) = N_Attribute_Definition_Clause
7052 and then Chars (N) = Name_Address
7053 then
7054 Expr := Expression (N);
7056 -- This loop checks the form of the expression for Y'Address,
7057 -- using recursion to deal with intermediate constants.
7059 loop
7060 -- Check for Y'Address
7062 if Nkind (Expr) = N_Attribute_Reference
7063 and then Attribute_Name (Expr) = Name_Address
7064 then
7065 Expr := Prefix (Expr);
7066 exit;
7068 -- Check for Const where Const is a constant entity
7070 elsif Is_Entity_Name (Expr)
7071 and then Ekind (Entity (Expr)) = E_Constant
7072 then
7073 Expr := Constant_Value (Entity (Expr));
7075 -- Anything else does not need checking
7077 else
7078 return;
7079 end if;
7080 end loop;
7082 -- This loop checks the form of the prefix for an entity, using
7083 -- recursion to deal with intermediate components.
7085 loop
7086 -- Check for Y where Y is an entity
7088 if Is_Entity_Name (Expr) then
7089 Ent := Entity (Expr);
7090 return;
7092 -- Check for components
7094 elsif
7095 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
7096 then
7097 Expr := Prefix (Expr);
7098 Off := True;
7100 -- Anything else does not need checking
7102 else
7103 return;
7104 end if;
7105 end loop;
7106 end if;
7107 end Find_Overlaid_Entity;
7109 -------------------------
7110 -- Find_Parameter_Type --
7111 -------------------------
7113 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
7114 begin
7115 if Nkind (Param) /= N_Parameter_Specification then
7116 return Empty;
7118 -- For an access parameter, obtain the type from the formal entity
7119 -- itself, because access to subprogram nodes do not carry a type.
7120 -- Shouldn't we always use the formal entity ???
7122 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
7123 return Etype (Defining_Identifier (Param));
7125 else
7126 return Etype (Parameter_Type (Param));
7127 end if;
7128 end Find_Parameter_Type;
7130 -----------------------------------
7131 -- Find_Placement_In_State_Space --
7132 -----------------------------------
7134 procedure Find_Placement_In_State_Space
7135 (Item_Id : Entity_Id;
7136 Placement : out State_Space_Kind;
7137 Pack_Id : out Entity_Id)
7139 Context : Entity_Id;
7141 begin
7142 -- Assume that the item does not appear in the state space of a package
7144 Placement := Not_In_Package;
7145 Pack_Id := Empty;
7147 -- Climb the scope stack and examine the enclosing context
7149 Context := Scope (Item_Id);
7150 while Present (Context) and then Context /= Standard_Standard loop
7151 if Ekind (Context) = E_Package then
7152 Pack_Id := Context;
7154 -- A package body is a cut off point for the traversal as the item
7155 -- cannot be visible to the outside from this point on. Note that
7156 -- this test must be done first as a body is also classified as a
7157 -- private part.
7159 if In_Package_Body (Context) then
7160 Placement := Body_State_Space;
7161 return;
7163 -- The private part of a package is a cut off point for the
7164 -- traversal as the item cannot be visible to the outside from
7165 -- this point on.
7167 elsif In_Private_Part (Context) then
7168 Placement := Private_State_Space;
7169 return;
7171 -- When the item appears in the visible state space of a package,
7172 -- continue to climb the scope stack as this may not be the final
7173 -- state space.
7175 else
7176 Placement := Visible_State_Space;
7178 -- The visible state space of a child unit acts as the proper
7179 -- placement of an item.
7181 if Is_Child_Unit (Context) then
7182 return;
7183 end if;
7184 end if;
7186 -- The item or its enclosing package appear in a construct that has
7187 -- no state space.
7189 else
7190 Placement := Not_In_Package;
7191 return;
7192 end if;
7194 Context := Scope (Context);
7195 end loop;
7196 end Find_Placement_In_State_Space;
7198 ------------------------
7199 -- Find_Specific_Type --
7200 ------------------------
7202 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
7203 Typ : Entity_Id := Root_Type (CW);
7205 begin
7206 if Ekind (Typ) = E_Incomplete_Type then
7207 if From_Limited_With (Typ) then
7208 Typ := Non_Limited_View (Typ);
7209 else
7210 Typ := Full_View (Typ);
7211 end if;
7212 end if;
7214 if Is_Private_Type (Typ)
7215 and then not Is_Tagged_Type (Typ)
7216 and then Present (Full_View (Typ))
7217 then
7218 return Full_View (Typ);
7219 else
7220 return Typ;
7221 end if;
7222 end Find_Specific_Type;
7224 -----------------------------
7225 -- Find_Static_Alternative --
7226 -----------------------------
7228 function Find_Static_Alternative (N : Node_Id) return Node_Id is
7229 Expr : constant Node_Id := Expression (N);
7230 Val : constant Uint := Expr_Value (Expr);
7231 Alt : Node_Id;
7232 Choice : Node_Id;
7234 begin
7235 Alt := First (Alternatives (N));
7237 Search : loop
7238 if Nkind (Alt) /= N_Pragma then
7239 Choice := First (Discrete_Choices (Alt));
7240 while Present (Choice) loop
7242 -- Others choice, always matches
7244 if Nkind (Choice) = N_Others_Choice then
7245 exit Search;
7247 -- Range, check if value is in the range
7249 elsif Nkind (Choice) = N_Range then
7250 exit Search when
7251 Val >= Expr_Value (Low_Bound (Choice))
7252 and then
7253 Val <= Expr_Value (High_Bound (Choice));
7255 -- Choice is a subtype name. Note that we know it must
7256 -- be a static subtype, since otherwise it would have
7257 -- been diagnosed as illegal.
7259 elsif Is_Entity_Name (Choice)
7260 and then Is_Type (Entity (Choice))
7261 then
7262 exit Search when Is_In_Range (Expr, Etype (Choice),
7263 Assume_Valid => False);
7265 -- Choice is a subtype indication
7267 elsif Nkind (Choice) = N_Subtype_Indication then
7268 declare
7269 C : constant Node_Id := Constraint (Choice);
7270 R : constant Node_Id := Range_Expression (C);
7272 begin
7273 exit Search when
7274 Val >= Expr_Value (Low_Bound (R))
7275 and then
7276 Val <= Expr_Value (High_Bound (R));
7277 end;
7279 -- Choice is a simple expression
7281 else
7282 exit Search when Val = Expr_Value (Choice);
7283 end if;
7285 Next (Choice);
7286 end loop;
7287 end if;
7289 Next (Alt);
7290 pragma Assert (Present (Alt));
7291 end loop Search;
7293 -- The above loop *must* terminate by finding a match, since
7294 -- we know the case statement is valid, and the value of the
7295 -- expression is known at compile time. When we fall out of
7296 -- the loop, Alt points to the alternative that we know will
7297 -- be selected at run time.
7299 return Alt;
7300 end Find_Static_Alternative;
7302 ------------------
7303 -- First_Actual --
7304 ------------------
7306 function First_Actual (Node : Node_Id) return Node_Id is
7307 N : Node_Id;
7309 begin
7310 if No (Parameter_Associations (Node)) then
7311 return Empty;
7312 end if;
7314 N := First (Parameter_Associations (Node));
7316 if Nkind (N) = N_Parameter_Association then
7317 return First_Named_Actual (Node);
7318 else
7319 return N;
7320 end if;
7321 end First_Actual;
7323 -------------
7324 -- Fix_Msg --
7325 -------------
7327 function Fix_Msg (Id : Entity_Id; Msg : String) return String is
7328 Is_Task : constant Boolean :=
7329 Ekind_In (Id, E_Task_Body, E_Task_Type)
7330 or else Is_Single_Task_Object (Id);
7331 Msg_Last : constant Natural := Msg'Last;
7332 Msg_Index : Natural;
7333 Res : String (Msg'Range) := (others => ' ');
7334 Res_Index : Natural;
7336 begin
7337 -- Copy all characters from the input message Msg to result Res with
7338 -- suitable replacements.
7340 Msg_Index := Msg'First;
7341 Res_Index := Res'First;
7342 while Msg_Index <= Msg_Last loop
7344 -- Replace "subprogram" with a different word
7346 if Msg_Index <= Msg_Last - 10
7347 and then Msg (Msg_Index .. Msg_Index + 9) = "subprogram"
7348 then
7349 if Ekind_In (Id, E_Entry, E_Entry_Family) then
7350 Res (Res_Index .. Res_Index + 4) := "entry";
7351 Res_Index := Res_Index + 5;
7353 elsif Is_Task then
7354 Res (Res_Index .. Res_Index + 8) := "task type";
7355 Res_Index := Res_Index + 9;
7357 else
7358 Res (Res_Index .. Res_Index + 9) := "subprogram";
7359 Res_Index := Res_Index + 10;
7360 end if;
7362 Msg_Index := Msg_Index + 10;
7364 -- Replace "protected" with a different word
7366 elsif Msg_Index <= Msg_Last - 9
7367 and then Msg (Msg_Index .. Msg_Index + 8) = "protected"
7368 and then Is_Task
7369 then
7370 Res (Res_Index .. Res_Index + 3) := "task";
7371 Res_Index := Res_Index + 4;
7372 Msg_Index := Msg_Index + 9;
7374 -- Otherwise copy the character
7376 else
7377 Res (Res_Index) := Msg (Msg_Index);
7378 Msg_Index := Msg_Index + 1;
7379 Res_Index := Res_Index + 1;
7380 end if;
7381 end loop;
7383 return Res (Res'First .. Res_Index - 1);
7384 end Fix_Msg;
7386 -----------------------
7387 -- Gather_Components --
7388 -----------------------
7390 procedure Gather_Components
7391 (Typ : Entity_Id;
7392 Comp_List : Node_Id;
7393 Governed_By : List_Id;
7394 Into : Elist_Id;
7395 Report_Errors : out Boolean)
7397 Assoc : Node_Id;
7398 Variant : Node_Id;
7399 Discrete_Choice : Node_Id;
7400 Comp_Item : Node_Id;
7402 Discrim : Entity_Id;
7403 Discrim_Name : Node_Id;
7404 Discrim_Value : Node_Id;
7406 begin
7407 Report_Errors := False;
7409 if No (Comp_List) or else Null_Present (Comp_List) then
7410 return;
7412 elsif Present (Component_Items (Comp_List)) then
7413 Comp_Item := First (Component_Items (Comp_List));
7415 else
7416 Comp_Item := Empty;
7417 end if;
7419 while Present (Comp_Item) loop
7421 -- Skip the tag of a tagged record, the interface tags, as well
7422 -- as all items that are not user components (anonymous types,
7423 -- rep clauses, Parent field, controller field).
7425 if Nkind (Comp_Item) = N_Component_Declaration then
7426 declare
7427 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
7428 begin
7429 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
7430 Append_Elmt (Comp, Into);
7431 end if;
7432 end;
7433 end if;
7435 Next (Comp_Item);
7436 end loop;
7438 if No (Variant_Part (Comp_List)) then
7439 return;
7440 else
7441 Discrim_Name := Name (Variant_Part (Comp_List));
7442 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
7443 end if;
7445 -- Look for the discriminant that governs this variant part.
7446 -- The discriminant *must* be in the Governed_By List
7448 Assoc := First (Governed_By);
7449 Find_Constraint : loop
7450 Discrim := First (Choices (Assoc));
7451 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
7452 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
7453 and then
7454 Chars (Corresponding_Discriminant (Entity (Discrim))) =
7455 Chars (Discrim_Name))
7456 or else Chars (Original_Record_Component (Entity (Discrim)))
7457 = Chars (Discrim_Name);
7459 if No (Next (Assoc)) then
7460 if not Is_Constrained (Typ)
7461 and then Is_Derived_Type (Typ)
7462 and then Present (Stored_Constraint (Typ))
7463 then
7464 -- If the type is a tagged type with inherited discriminants,
7465 -- use the stored constraint on the parent in order to find
7466 -- the values of discriminants that are otherwise hidden by an
7467 -- explicit constraint. Renamed discriminants are handled in
7468 -- the code above.
7470 -- If several parent discriminants are renamed by a single
7471 -- discriminant of the derived type, the call to obtain the
7472 -- Corresponding_Discriminant field only retrieves the last
7473 -- of them. We recover the constraint on the others from the
7474 -- Stored_Constraint as well.
7476 declare
7477 D : Entity_Id;
7478 C : Elmt_Id;
7480 begin
7481 D := First_Discriminant (Etype (Typ));
7482 C := First_Elmt (Stored_Constraint (Typ));
7483 while Present (D) and then Present (C) loop
7484 if Chars (Discrim_Name) = Chars (D) then
7485 if Is_Entity_Name (Node (C))
7486 and then Entity (Node (C)) = Entity (Discrim)
7487 then
7488 -- D is renamed by Discrim, whose value is given in
7489 -- Assoc.
7491 null;
7493 else
7494 Assoc :=
7495 Make_Component_Association (Sloc (Typ),
7496 New_List
7497 (New_Occurrence_Of (D, Sloc (Typ))),
7498 Duplicate_Subexpr_No_Checks (Node (C)));
7499 end if;
7500 exit Find_Constraint;
7501 end if;
7503 Next_Discriminant (D);
7504 Next_Elmt (C);
7505 end loop;
7506 end;
7507 end if;
7508 end if;
7510 if No (Next (Assoc)) then
7511 Error_Msg_NE (" missing value for discriminant&",
7512 First (Governed_By), Discrim_Name);
7513 Report_Errors := True;
7514 return;
7515 end if;
7517 Next (Assoc);
7518 end loop Find_Constraint;
7520 Discrim_Value := Expression (Assoc);
7522 if not Is_OK_Static_Expression (Discrim_Value) then
7524 -- If the variant part is governed by a discriminant of the type
7525 -- this is an error. If the variant part and the discriminant are
7526 -- inherited from an ancestor this is legal (AI05-120) unless the
7527 -- components are being gathered for an aggregate, in which case
7528 -- the caller must check Report_Errors.
7530 if Scope (Original_Record_Component
7531 ((Entity (First (Choices (Assoc)))))) = Typ
7532 then
7533 Error_Msg_FE
7534 ("value for discriminant & must be static!",
7535 Discrim_Value, Discrim);
7536 Why_Not_Static (Discrim_Value);
7537 end if;
7539 Report_Errors := True;
7540 return;
7541 end if;
7543 Search_For_Discriminant_Value : declare
7544 Low : Node_Id;
7545 High : Node_Id;
7547 UI_High : Uint;
7548 UI_Low : Uint;
7549 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
7551 begin
7552 Find_Discrete_Value : while Present (Variant) loop
7553 Discrete_Choice := First (Discrete_Choices (Variant));
7554 while Present (Discrete_Choice) loop
7555 exit Find_Discrete_Value when
7556 Nkind (Discrete_Choice) = N_Others_Choice;
7558 Get_Index_Bounds (Discrete_Choice, Low, High);
7560 UI_Low := Expr_Value (Low);
7561 UI_High := Expr_Value (High);
7563 exit Find_Discrete_Value when
7564 UI_Low <= UI_Discrim_Value
7565 and then
7566 UI_High >= UI_Discrim_Value;
7568 Next (Discrete_Choice);
7569 end loop;
7571 Next_Non_Pragma (Variant);
7572 end loop Find_Discrete_Value;
7573 end Search_For_Discriminant_Value;
7575 if No (Variant) then
7576 Error_Msg_NE
7577 ("value of discriminant & is out of range", Discrim_Value, Discrim);
7578 Report_Errors := True;
7579 return;
7580 end if;
7582 -- If we have found the corresponding choice, recursively add its
7583 -- components to the Into list. The nested components are part of
7584 -- the same record type.
7586 Gather_Components
7587 (Typ, Component_List (Variant), Governed_By, Into, Report_Errors);
7588 end Gather_Components;
7590 ------------------------
7591 -- Get_Actual_Subtype --
7592 ------------------------
7594 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
7595 Typ : constant Entity_Id := Etype (N);
7596 Utyp : Entity_Id := Underlying_Type (Typ);
7597 Decl : Node_Id;
7598 Atyp : Entity_Id;
7600 begin
7601 if No (Utyp) then
7602 Utyp := Typ;
7603 end if;
7605 -- If what we have is an identifier that references a subprogram
7606 -- formal, or a variable or constant object, then we get the actual
7607 -- subtype from the referenced entity if one has been built.
7609 if Nkind (N) = N_Identifier
7610 and then
7611 (Is_Formal (Entity (N))
7612 or else Ekind (Entity (N)) = E_Constant
7613 or else Ekind (Entity (N)) = E_Variable)
7614 and then Present (Actual_Subtype (Entity (N)))
7615 then
7616 return Actual_Subtype (Entity (N));
7618 -- Actual subtype of unchecked union is always itself. We never need
7619 -- the "real" actual subtype. If we did, we couldn't get it anyway
7620 -- because the discriminant is not available. The restrictions on
7621 -- Unchecked_Union are designed to make sure that this is OK.
7623 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
7624 return Typ;
7626 -- Here for the unconstrained case, we must find actual subtype
7627 -- No actual subtype is available, so we must build it on the fly.
7629 -- Checking the type, not the underlying type, for constrainedness
7630 -- seems to be necessary. Maybe all the tests should be on the type???
7632 elsif (not Is_Constrained (Typ))
7633 and then (Is_Array_Type (Utyp)
7634 or else (Is_Record_Type (Utyp)
7635 and then Has_Discriminants (Utyp)))
7636 and then not Has_Unknown_Discriminants (Utyp)
7637 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
7638 then
7639 -- Nothing to do if in spec expression (why not???)
7641 if In_Spec_Expression then
7642 return Typ;
7644 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
7646 -- If the type has no discriminants, there is no subtype to
7647 -- build, even if the underlying type is discriminated.
7649 return Typ;
7651 -- Else build the actual subtype
7653 else
7654 Decl := Build_Actual_Subtype (Typ, N);
7655 Atyp := Defining_Identifier (Decl);
7657 -- If Build_Actual_Subtype generated a new declaration then use it
7659 if Atyp /= Typ then
7661 -- The actual subtype is an Itype, so analyze the declaration,
7662 -- but do not attach it to the tree, to get the type defined.
7664 Set_Parent (Decl, N);
7665 Set_Is_Itype (Atyp);
7666 Analyze (Decl, Suppress => All_Checks);
7667 Set_Associated_Node_For_Itype (Atyp, N);
7668 Set_Has_Delayed_Freeze (Atyp, False);
7670 -- We need to freeze the actual subtype immediately. This is
7671 -- needed, because otherwise this Itype will not get frozen
7672 -- at all, and it is always safe to freeze on creation because
7673 -- any associated types must be frozen at this point.
7675 Freeze_Itype (Atyp, N);
7676 return Atyp;
7678 -- Otherwise we did not build a declaration, so return original
7680 else
7681 return Typ;
7682 end if;
7683 end if;
7685 -- For all remaining cases, the actual subtype is the same as
7686 -- the nominal type.
7688 else
7689 return Typ;
7690 end if;
7691 end Get_Actual_Subtype;
7693 -------------------------------------
7694 -- Get_Actual_Subtype_If_Available --
7695 -------------------------------------
7697 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
7698 Typ : constant Entity_Id := Etype (N);
7700 begin
7701 -- If what we have is an identifier that references a subprogram
7702 -- formal, or a variable or constant object, then we get the actual
7703 -- subtype from the referenced entity if one has been built.
7705 if Nkind (N) = N_Identifier
7706 and then
7707 (Is_Formal (Entity (N))
7708 or else Ekind (Entity (N)) = E_Constant
7709 or else Ekind (Entity (N)) = E_Variable)
7710 and then Present (Actual_Subtype (Entity (N)))
7711 then
7712 return Actual_Subtype (Entity (N));
7714 -- Otherwise the Etype of N is returned unchanged
7716 else
7717 return Typ;
7718 end if;
7719 end Get_Actual_Subtype_If_Available;
7721 ------------------------
7722 -- Get_Body_From_Stub --
7723 ------------------------
7725 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
7726 begin
7727 return Proper_Body (Unit (Library_Unit (N)));
7728 end Get_Body_From_Stub;
7730 ---------------------
7731 -- Get_Cursor_Type --
7732 ---------------------
7734 function Get_Cursor_Type
7735 (Aspect : Node_Id;
7736 Typ : Entity_Id) return Entity_Id
7738 Assoc : Node_Id;
7739 Func : Entity_Id;
7740 First_Op : Entity_Id;
7741 Cursor : Entity_Id;
7743 begin
7744 -- If error already detected, return
7746 if Error_Posted (Aspect) then
7747 return Any_Type;
7748 end if;
7750 -- The cursor type for an Iterable aspect is the return type of a
7751 -- non-overloaded First primitive operation. Locate association for
7752 -- First.
7754 Assoc := First (Component_Associations (Expression (Aspect)));
7755 First_Op := Any_Id;
7756 while Present (Assoc) loop
7757 if Chars (First (Choices (Assoc))) = Name_First then
7758 First_Op := Expression (Assoc);
7759 exit;
7760 end if;
7762 Next (Assoc);
7763 end loop;
7765 if First_Op = Any_Id then
7766 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
7767 return Any_Type;
7768 end if;
7770 Cursor := Any_Type;
7772 -- Locate function with desired name and profile in scope of type
7773 -- In the rare case where the type is an integer type, a base type
7774 -- is created for it, check that the base type of the first formal
7775 -- of First matches the base type of the domain.
7777 Func := First_Entity (Scope (Typ));
7778 while Present (Func) loop
7779 if Chars (Func) = Chars (First_Op)
7780 and then Ekind (Func) = E_Function
7781 and then Present (First_Formal (Func))
7782 and then Base_Type (Etype (First_Formal (Func))) = Base_Type (Typ)
7783 and then No (Next_Formal (First_Formal (Func)))
7784 then
7785 if Cursor /= Any_Type then
7786 Error_Msg_N
7787 ("Operation First for iterable type must be unique", Aspect);
7788 return Any_Type;
7789 else
7790 Cursor := Etype (Func);
7791 end if;
7792 end if;
7794 Next_Entity (Func);
7795 end loop;
7797 -- If not found, no way to resolve remaining primitives.
7799 if Cursor = Any_Type then
7800 Error_Msg_N
7801 ("No legal primitive operation First for Iterable type", Aspect);
7802 end if;
7804 return Cursor;
7805 end Get_Cursor_Type;
7807 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
7808 begin
7809 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
7810 end Get_Cursor_Type;
7812 -------------------------------
7813 -- Get_Default_External_Name --
7814 -------------------------------
7816 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
7817 begin
7818 Get_Decoded_Name_String (Chars (E));
7820 if Opt.External_Name_Imp_Casing = Uppercase then
7821 Set_Casing (All_Upper_Case);
7822 else
7823 Set_Casing (All_Lower_Case);
7824 end if;
7826 return
7827 Make_String_Literal (Sloc (E),
7828 Strval => String_From_Name_Buffer);
7829 end Get_Default_External_Name;
7831 --------------------------
7832 -- Get_Enclosing_Object --
7833 --------------------------
7835 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
7836 begin
7837 if Is_Entity_Name (N) then
7838 return Entity (N);
7839 else
7840 case Nkind (N) is
7841 when N_Indexed_Component |
7842 N_Slice |
7843 N_Selected_Component =>
7845 -- If not generating code, a dereference may be left implicit.
7846 -- In thoses cases, return Empty.
7848 if Is_Access_Type (Etype (Prefix (N))) then
7849 return Empty;
7850 else
7851 return Get_Enclosing_Object (Prefix (N));
7852 end if;
7854 when N_Type_Conversion =>
7855 return Get_Enclosing_Object (Expression (N));
7857 when others =>
7858 return Empty;
7859 end case;
7860 end if;
7861 end Get_Enclosing_Object;
7863 ---------------------------
7864 -- Get_Enum_Lit_From_Pos --
7865 ---------------------------
7867 function Get_Enum_Lit_From_Pos
7868 (T : Entity_Id;
7869 Pos : Uint;
7870 Loc : Source_Ptr) return Node_Id
7872 Btyp : Entity_Id := Base_Type (T);
7873 Lit : Node_Id;
7874 LLoc : Source_Ptr;
7876 begin
7877 -- In the case where the literal is of type Character, Wide_Character
7878 -- or Wide_Wide_Character or of a type derived from them, there needs
7879 -- to be some special handling since there is no explicit chain of
7880 -- literals to search. Instead, an N_Character_Literal node is created
7881 -- with the appropriate Char_Code and Chars fields.
7883 if Is_Standard_Character_Type (T) then
7884 Set_Character_Literal_Name (UI_To_CC (Pos));
7886 return
7887 Make_Character_Literal (Loc,
7888 Chars => Name_Find,
7889 Char_Literal_Value => Pos);
7891 -- For all other cases, we have a complete table of literals, and
7892 -- we simply iterate through the chain of literal until the one
7893 -- with the desired position value is found.
7895 else
7896 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7897 Btyp := Full_View (Btyp);
7898 end if;
7900 Lit := First_Literal (Btyp);
7901 for J in 1 .. UI_To_Int (Pos) loop
7902 Next_Literal (Lit);
7904 -- If Lit is Empty, Pos is not in range, so raise Constraint_Error
7905 -- inside the loop to avoid calling Next_Literal on Empty.
7907 if No (Lit) then
7908 raise Constraint_Error;
7909 end if;
7910 end loop;
7912 -- Create a new node from Lit, with source location provided by Loc
7913 -- if not equal to No_Location, or by copying the source location of
7914 -- Lit otherwise.
7916 LLoc := Loc;
7918 if LLoc = No_Location then
7919 LLoc := Sloc (Lit);
7920 end if;
7922 return New_Occurrence_Of (Lit, LLoc);
7923 end if;
7924 end Get_Enum_Lit_From_Pos;
7926 ------------------------
7927 -- Get_Generic_Entity --
7928 ------------------------
7930 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7931 Ent : constant Entity_Id := Entity (Name (N));
7932 begin
7933 if Present (Renamed_Object (Ent)) then
7934 return Renamed_Object (Ent);
7935 else
7936 return Ent;
7937 end if;
7938 end Get_Generic_Entity;
7940 -------------------------------------
7941 -- Get_Incomplete_View_Of_Ancestor --
7942 -------------------------------------
7944 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7945 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7946 Par_Scope : Entity_Id;
7947 Par_Type : Entity_Id;
7949 begin
7950 -- The incomplete view of an ancestor is only relevant for private
7951 -- derived types in child units.
7953 if not Is_Derived_Type (E)
7954 or else not Is_Child_Unit (Cur_Unit)
7955 then
7956 return Empty;
7958 else
7959 Par_Scope := Scope (Cur_Unit);
7960 if No (Par_Scope) then
7961 return Empty;
7962 end if;
7964 Par_Type := Etype (Base_Type (E));
7966 -- Traverse list of ancestor types until we find one declared in
7967 -- a parent or grandparent unit (two levels seem sufficient).
7969 while Present (Par_Type) loop
7970 if Scope (Par_Type) = Par_Scope
7971 or else Scope (Par_Type) = Scope (Par_Scope)
7972 then
7973 return Par_Type;
7975 elsif not Is_Derived_Type (Par_Type) then
7976 return Empty;
7978 else
7979 Par_Type := Etype (Base_Type (Par_Type));
7980 end if;
7981 end loop;
7983 -- If none found, there is no relevant ancestor type.
7985 return Empty;
7986 end if;
7987 end Get_Incomplete_View_Of_Ancestor;
7989 ----------------------
7990 -- Get_Index_Bounds --
7991 ----------------------
7993 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
7994 Kind : constant Node_Kind := Nkind (N);
7995 R : Node_Id;
7997 begin
7998 if Kind = N_Range then
7999 L := Low_Bound (N);
8000 H := High_Bound (N);
8002 elsif Kind = N_Subtype_Indication then
8003 R := Range_Expression (Constraint (N));
8005 if R = Error then
8006 L := Error;
8007 H := Error;
8008 return;
8010 else
8011 L := Low_Bound (Range_Expression (Constraint (N)));
8012 H := High_Bound (Range_Expression (Constraint (N)));
8013 end if;
8015 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8016 if Error_Posted (Scalar_Range (Entity (N))) then
8017 L := Error;
8018 H := Error;
8020 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
8021 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
8023 else
8024 L := Low_Bound (Scalar_Range (Entity (N)));
8025 H := High_Bound (Scalar_Range (Entity (N)));
8026 end if;
8028 else
8029 -- N is an expression, indicating a range with one value
8031 L := N;
8032 H := N;
8033 end if;
8034 end Get_Index_Bounds;
8036 ---------------------------------
8037 -- Get_Iterable_Type_Primitive --
8038 ---------------------------------
8040 function Get_Iterable_Type_Primitive
8041 (Typ : Entity_Id;
8042 Nam : Name_Id) return Entity_Id
8044 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
8045 Assoc : Node_Id;
8047 begin
8048 if No (Funcs) then
8049 return Empty;
8051 else
8052 Assoc := First (Component_Associations (Funcs));
8053 while Present (Assoc) loop
8054 if Chars (First (Choices (Assoc))) = Nam then
8055 return Entity (Expression (Assoc));
8056 end if;
8058 Assoc := Next (Assoc);
8059 end loop;
8061 return Empty;
8062 end if;
8063 end Get_Iterable_Type_Primitive;
8065 ----------------------------------
8066 -- Get_Library_Unit_Name_string --
8067 ----------------------------------
8069 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
8070 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
8072 begin
8073 Get_Unit_Name_String (Unit_Name_Id);
8075 -- Remove seven last character (" (spec)" or " (body)")
8077 Name_Len := Name_Len - 7;
8078 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
8079 end Get_Library_Unit_Name_String;
8081 --------------------------
8082 -- Get_Max_Queue_Length --
8083 --------------------------
8085 function Get_Max_Queue_Length (Id : Entity_Id) return Uint is
8086 pragma Assert (Is_Entry (Id));
8087 Prag : constant Entity_Id := Get_Pragma (Id, Pragma_Max_Queue_Length);
8089 begin
8090 -- A value of 0 represents no maximum specified, and entries and entry
8091 -- families with no Max_Queue_Length aspect or pragma default to it.
8093 if not Present (Prag) then
8094 return Uint_0;
8095 end if;
8097 return Intval (Expression (First (Pragma_Argument_Associations (Prag))));
8098 end Get_Max_Queue_Length;
8100 ------------------------
8101 -- Get_Name_Entity_Id --
8102 ------------------------
8104 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
8105 begin
8106 return Entity_Id (Get_Name_Table_Int (Id));
8107 end Get_Name_Entity_Id;
8109 ------------------------------
8110 -- Get_Name_From_CTC_Pragma --
8111 ------------------------------
8113 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
8114 Arg : constant Node_Id :=
8115 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
8116 begin
8117 return Strval (Expr_Value_S (Arg));
8118 end Get_Name_From_CTC_Pragma;
8120 -----------------------
8121 -- Get_Parent_Entity --
8122 -----------------------
8124 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
8125 begin
8126 if Nkind (Unit) = N_Package_Body
8127 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
8128 then
8129 return Defining_Entity
8130 (Specification (Instance_Spec (Original_Node (Unit))));
8131 elsif Nkind (Unit) = N_Package_Instantiation then
8132 return Defining_Entity (Specification (Instance_Spec (Unit)));
8133 else
8134 return Defining_Entity (Unit);
8135 end if;
8136 end Get_Parent_Entity;
8138 -------------------
8139 -- Get_Pragma_Id --
8140 -------------------
8142 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
8143 begin
8144 return Get_Pragma_Id (Pragma_Name (N));
8145 end Get_Pragma_Id;
8147 ------------------------
8148 -- Get_Qualified_Name --
8149 ------------------------
8151 function Get_Qualified_Name
8152 (Id : Entity_Id;
8153 Suffix : Entity_Id := Empty) return Name_Id
8155 Suffix_Nam : Name_Id := No_Name;
8157 begin
8158 if Present (Suffix) then
8159 Suffix_Nam := Chars (Suffix);
8160 end if;
8162 return Get_Qualified_Name (Chars (Id), Suffix_Nam, Scope (Id));
8163 end Get_Qualified_Name;
8165 function Get_Qualified_Name
8166 (Nam : Name_Id;
8167 Suffix : Name_Id := No_Name;
8168 Scop : Entity_Id := Current_Scope) return Name_Id
8170 procedure Add_Scope (S : Entity_Id);
8171 -- Add the fully qualified form of scope S to the name buffer. The
8172 -- format is:
8173 -- s-1__s__
8175 ---------------
8176 -- Add_Scope --
8177 ---------------
8179 procedure Add_Scope (S : Entity_Id) is
8180 begin
8181 if S = Empty then
8182 null;
8184 elsif S = Standard_Standard then
8185 null;
8187 else
8188 Add_Scope (Scope (S));
8189 Get_Name_String_And_Append (Chars (S));
8190 Add_Str_To_Name_Buffer ("__");
8191 end if;
8192 end Add_Scope;
8194 -- Start of processing for Get_Qualified_Name
8196 begin
8197 Name_Len := 0;
8198 Add_Scope (Scop);
8200 -- Append the base name after all scopes have been chained
8202 Get_Name_String_And_Append (Nam);
8204 -- Append the suffix (if present)
8206 if Suffix /= No_Name then
8207 Add_Str_To_Name_Buffer ("__");
8208 Get_Name_String_And_Append (Suffix);
8209 end if;
8211 return Name_Find;
8212 end Get_Qualified_Name;
8214 -----------------------
8215 -- Get_Reason_String --
8216 -----------------------
8218 procedure Get_Reason_String (N : Node_Id) is
8219 begin
8220 if Nkind (N) = N_String_Literal then
8221 Store_String_Chars (Strval (N));
8223 elsif Nkind (N) = N_Op_Concat then
8224 Get_Reason_String (Left_Opnd (N));
8225 Get_Reason_String (Right_Opnd (N));
8227 -- If not of required form, error
8229 else
8230 Error_Msg_N
8231 ("Reason for pragma Warnings has wrong form", N);
8232 Error_Msg_N
8233 ("\must be string literal or concatenation of string literals", N);
8234 return;
8235 end if;
8236 end Get_Reason_String;
8238 --------------------------------
8239 -- Get_Reference_Discriminant --
8240 --------------------------------
8242 function Get_Reference_Discriminant (Typ : Entity_Id) return Entity_Id is
8243 D : Entity_Id;
8245 begin
8246 D := First_Discriminant (Typ);
8247 while Present (D) loop
8248 if Has_Implicit_Dereference (D) then
8249 return D;
8250 end if;
8251 Next_Discriminant (D);
8252 end loop;
8254 return Empty;
8255 end Get_Reference_Discriminant;
8257 ---------------------------
8258 -- Get_Referenced_Object --
8259 ---------------------------
8261 function Get_Referenced_Object (N : Node_Id) return Node_Id is
8262 R : Node_Id;
8264 begin
8265 R := N;
8266 while Is_Entity_Name (R)
8267 and then Present (Renamed_Object (Entity (R)))
8268 loop
8269 R := Renamed_Object (Entity (R));
8270 end loop;
8272 return R;
8273 end Get_Referenced_Object;
8275 ------------------------
8276 -- Get_Renamed_Entity --
8277 ------------------------
8279 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
8280 R : Entity_Id;
8282 begin
8283 R := E;
8284 while Present (Renamed_Entity (R)) loop
8285 R := Renamed_Entity (R);
8286 end loop;
8288 return R;
8289 end Get_Renamed_Entity;
8291 -----------------------
8292 -- Get_Return_Object --
8293 -----------------------
8295 function Get_Return_Object (N : Node_Id) return Entity_Id is
8296 Decl : Node_Id;
8298 begin
8299 Decl := First (Return_Object_Declarations (N));
8300 while Present (Decl) loop
8301 exit when Nkind (Decl) = N_Object_Declaration
8302 and then Is_Return_Object (Defining_Identifier (Decl));
8303 Next (Decl);
8304 end loop;
8306 pragma Assert (Present (Decl));
8307 return Defining_Identifier (Decl);
8308 end Get_Return_Object;
8310 ---------------------------
8311 -- Get_Subprogram_Entity --
8312 ---------------------------
8314 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
8315 Subp : Node_Id;
8316 Subp_Id : Entity_Id;
8318 begin
8319 if Nkind (Nod) = N_Accept_Statement then
8320 Subp := Entry_Direct_Name (Nod);
8322 elsif Nkind (Nod) = N_Slice then
8323 Subp := Prefix (Nod);
8325 else
8326 Subp := Name (Nod);
8327 end if;
8329 -- Strip the subprogram call
8331 loop
8332 if Nkind_In (Subp, N_Explicit_Dereference,
8333 N_Indexed_Component,
8334 N_Selected_Component)
8335 then
8336 Subp := Prefix (Subp);
8338 elsif Nkind_In (Subp, N_Type_Conversion,
8339 N_Unchecked_Type_Conversion)
8340 then
8341 Subp := Expression (Subp);
8343 else
8344 exit;
8345 end if;
8346 end loop;
8348 -- Extract the entity of the subprogram call
8350 if Is_Entity_Name (Subp) then
8351 Subp_Id := Entity (Subp);
8353 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
8354 Subp_Id := Directly_Designated_Type (Subp_Id);
8355 end if;
8357 if Is_Subprogram (Subp_Id) then
8358 return Subp_Id;
8359 else
8360 return Empty;
8361 end if;
8363 -- The search did not find a construct that denotes a subprogram
8365 else
8366 return Empty;
8367 end if;
8368 end Get_Subprogram_Entity;
8370 -----------------------------
8371 -- Get_Task_Body_Procedure --
8372 -----------------------------
8374 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
8375 begin
8376 -- Note: A task type may be the completion of a private type with
8377 -- discriminants. When performing elaboration checks on a task
8378 -- declaration, the current view of the type may be the private one,
8379 -- and the procedure that holds the body of the task is held in its
8380 -- underlying type.
8382 -- This is an odd function, why not have Task_Body_Procedure do
8383 -- the following digging???
8385 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
8386 end Get_Task_Body_Procedure;
8388 -------------------------
8389 -- Get_User_Defined_Eq --
8390 -------------------------
8392 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
8393 Prim : Elmt_Id;
8394 Op : Entity_Id;
8396 begin
8397 Prim := First_Elmt (Collect_Primitive_Operations (E));
8398 while Present (Prim) loop
8399 Op := Node (Prim);
8401 if Chars (Op) = Name_Op_Eq
8402 and then Etype (Op) = Standard_Boolean
8403 and then Etype (First_Formal (Op)) = E
8404 and then Etype (Next_Formal (First_Formal (Op))) = E
8405 then
8406 return Op;
8407 end if;
8409 Next_Elmt (Prim);
8410 end loop;
8412 return Empty;
8413 end Get_User_Defined_Eq;
8415 ---------------
8416 -- Get_Views --
8417 ---------------
8419 procedure Get_Views
8420 (Typ : Entity_Id;
8421 Priv_Typ : out Entity_Id;
8422 Full_Typ : out Entity_Id;
8423 Full_Base : out Entity_Id;
8424 CRec_Typ : out Entity_Id)
8426 IP_View : Entity_Id;
8428 begin
8429 -- Assume that none of the views can be recovered
8431 Priv_Typ := Empty;
8432 Full_Typ := Empty;
8433 Full_Base := Empty;
8434 CRec_Typ := Empty;
8436 -- The input type is the corresponding record type of a protected or a
8437 -- task type.
8439 if Ekind (Typ) = E_Record_Type
8440 and then Is_Concurrent_Record_Type (Typ)
8441 then
8442 CRec_Typ := Typ;
8443 Full_Typ := Corresponding_Concurrent_Type (CRec_Typ);
8444 Full_Base := Base_Type (Full_Typ);
8445 Priv_Typ := Incomplete_Or_Partial_View (Full_Typ);
8447 -- Otherwise the input type denotes an arbitrary type
8449 else
8450 IP_View := Incomplete_Or_Partial_View (Typ);
8452 -- The input type denotes the full view of a private type
8454 if Present (IP_View) then
8455 Priv_Typ := IP_View;
8456 Full_Typ := Typ;
8458 -- The input type is a private type
8460 elsif Is_Private_Type (Typ) then
8461 Priv_Typ := Typ;
8462 Full_Typ := Full_View (Priv_Typ);
8464 -- Otherwise the input type does not have any views
8466 else
8467 Full_Typ := Typ;
8468 end if;
8470 if Present (Full_Typ) then
8471 Full_Base := Base_Type (Full_Typ);
8473 if Ekind_In (Full_Typ, E_Protected_Type, E_Task_Type) then
8474 CRec_Typ := Corresponding_Record_Type (Full_Typ);
8475 end if;
8476 end if;
8477 end if;
8478 end Get_Views;
8480 -----------------------
8481 -- Has_Access_Values --
8482 -----------------------
8484 function Has_Access_Values (T : Entity_Id) return Boolean is
8485 Typ : constant Entity_Id := Underlying_Type (T);
8487 begin
8488 -- Case of a private type which is not completed yet. This can only
8489 -- happen in the case of a generic format type appearing directly, or
8490 -- as a component of the type to which this function is being applied
8491 -- at the top level. Return False in this case, since we certainly do
8492 -- not know that the type contains access types.
8494 if No (Typ) then
8495 return False;
8497 elsif Is_Access_Type (Typ) then
8498 return True;
8500 elsif Is_Array_Type (Typ) then
8501 return Has_Access_Values (Component_Type (Typ));
8503 elsif Is_Record_Type (Typ) then
8504 declare
8505 Comp : Entity_Id;
8507 begin
8508 -- Loop to Check components
8510 Comp := First_Component_Or_Discriminant (Typ);
8511 while Present (Comp) loop
8513 -- Check for access component, tag field does not count, even
8514 -- though it is implemented internally using an access type.
8516 if Has_Access_Values (Etype (Comp))
8517 and then Chars (Comp) /= Name_uTag
8518 then
8519 return True;
8520 end if;
8522 Next_Component_Or_Discriminant (Comp);
8523 end loop;
8524 end;
8526 return False;
8528 else
8529 return False;
8530 end if;
8531 end Has_Access_Values;
8533 ------------------------------
8534 -- Has_Compatible_Alignment --
8535 ------------------------------
8537 function Has_Compatible_Alignment
8538 (Obj : Entity_Id;
8539 Expr : Node_Id;
8540 Layout_Done : Boolean) return Alignment_Result
8542 function Has_Compatible_Alignment_Internal
8543 (Obj : Entity_Id;
8544 Expr : Node_Id;
8545 Layout_Done : Boolean;
8546 Default : Alignment_Result) return Alignment_Result;
8547 -- This is the internal recursive function that actually does the work.
8548 -- There is one additional parameter, which says what the result should
8549 -- be if no alignment information is found, and there is no definite
8550 -- indication of compatible alignments. At the outer level, this is set
8551 -- to Unknown, but for internal recursive calls in the case where types
8552 -- are known to be correct, it is set to Known_Compatible.
8554 ---------------------------------------
8555 -- Has_Compatible_Alignment_Internal --
8556 ---------------------------------------
8558 function Has_Compatible_Alignment_Internal
8559 (Obj : Entity_Id;
8560 Expr : Node_Id;
8561 Layout_Done : Boolean;
8562 Default : Alignment_Result) return Alignment_Result
8564 Result : Alignment_Result := Known_Compatible;
8565 -- Holds the current status of the result. Note that once a value of
8566 -- Known_Incompatible is set, it is sticky and does not get changed
8567 -- to Unknown (the value in Result only gets worse as we go along,
8568 -- never better).
8570 Offs : Uint := No_Uint;
8571 -- Set to a factor of the offset from the base object when Expr is a
8572 -- selected or indexed component, based on Component_Bit_Offset and
8573 -- Component_Size respectively. A negative value is used to represent
8574 -- a value which is not known at compile time.
8576 procedure Check_Prefix;
8577 -- Checks the prefix recursively in the case where the expression
8578 -- is an indexed or selected component.
8580 procedure Set_Result (R : Alignment_Result);
8581 -- If R represents a worse outcome (unknown instead of known
8582 -- compatible, or known incompatible), then set Result to R.
8584 ------------------
8585 -- Check_Prefix --
8586 ------------------
8588 procedure Check_Prefix is
8589 begin
8590 -- The subtlety here is that in doing a recursive call to check
8591 -- the prefix, we have to decide what to do in the case where we
8592 -- don't find any specific indication of an alignment problem.
8594 -- At the outer level, we normally set Unknown as the result in
8595 -- this case, since we can only set Known_Compatible if we really
8596 -- know that the alignment value is OK, but for the recursive
8597 -- call, in the case where the types match, and we have not
8598 -- specified a peculiar alignment for the object, we are only
8599 -- concerned about suspicious rep clauses, the default case does
8600 -- not affect us, since the compiler will, in the absence of such
8601 -- rep clauses, ensure that the alignment is correct.
8603 if Default = Known_Compatible
8604 or else
8605 (Etype (Obj) = Etype (Expr)
8606 and then (Unknown_Alignment (Obj)
8607 or else
8608 Alignment (Obj) = Alignment (Etype (Obj))))
8609 then
8610 Set_Result
8611 (Has_Compatible_Alignment_Internal
8612 (Obj, Prefix (Expr), Layout_Done, Known_Compatible));
8614 -- In all other cases, we need a full check on the prefix
8616 else
8617 Set_Result
8618 (Has_Compatible_Alignment_Internal
8619 (Obj, Prefix (Expr), Layout_Done, Unknown));
8620 end if;
8621 end Check_Prefix;
8623 ----------------
8624 -- Set_Result --
8625 ----------------
8627 procedure Set_Result (R : Alignment_Result) is
8628 begin
8629 if R > Result then
8630 Result := R;
8631 end if;
8632 end Set_Result;
8634 -- Start of processing for Has_Compatible_Alignment_Internal
8636 begin
8637 -- If Expr is a selected component, we must make sure there is no
8638 -- potentially troublesome component clause and that the record is
8639 -- not packed if the layout is not done.
8641 if Nkind (Expr) = N_Selected_Component then
8643 -- Packing generates unknown alignment if layout is not done
8645 if Is_Packed (Etype (Prefix (Expr))) and then not Layout_Done then
8646 Set_Result (Unknown);
8647 end if;
8649 -- Check prefix and component offset
8651 Check_Prefix;
8652 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
8654 -- If Expr is an indexed component, we must make sure there is no
8655 -- potentially troublesome Component_Size clause and that the array
8656 -- is not bit-packed if the layout is not done.
8658 elsif Nkind (Expr) = N_Indexed_Component then
8659 declare
8660 Typ : constant Entity_Id := Etype (Prefix (Expr));
8662 begin
8663 -- Packing generates unknown alignment if layout is not done
8665 if Is_Bit_Packed_Array (Typ) and then not Layout_Done then
8666 Set_Result (Unknown);
8667 end if;
8669 -- Check prefix and component offset (or at least size)
8671 Check_Prefix;
8672 Offs := Indexed_Component_Bit_Offset (Expr);
8673 if Offs = No_Uint then
8674 Offs := Component_Size (Typ);
8675 end if;
8676 end;
8677 end if;
8679 -- If we have a null offset, the result is entirely determined by
8680 -- the base object and has already been computed recursively.
8682 if Offs = Uint_0 then
8683 null;
8685 -- Case where we know the alignment of the object
8687 elsif Known_Alignment (Obj) then
8688 declare
8689 ObjA : constant Uint := Alignment (Obj);
8690 ExpA : Uint := No_Uint;
8691 SizA : Uint := No_Uint;
8693 begin
8694 -- If alignment of Obj is 1, then we are always OK
8696 if ObjA = 1 then
8697 Set_Result (Known_Compatible);
8699 -- Alignment of Obj is greater than 1, so we need to check
8701 else
8702 -- If we have an offset, see if it is compatible
8704 if Offs /= No_Uint and Offs > Uint_0 then
8705 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
8706 Set_Result (Known_Incompatible);
8707 end if;
8709 -- See if Expr is an object with known alignment
8711 elsif Is_Entity_Name (Expr)
8712 and then Known_Alignment (Entity (Expr))
8713 then
8714 ExpA := Alignment (Entity (Expr));
8716 -- Otherwise, we can use the alignment of the type of
8717 -- Expr given that we already checked for
8718 -- discombobulating rep clauses for the cases of indexed
8719 -- and selected components above.
8721 elsif Known_Alignment (Etype (Expr)) then
8722 ExpA := Alignment (Etype (Expr));
8724 -- Otherwise the alignment is unknown
8726 else
8727 Set_Result (Default);
8728 end if;
8730 -- If we got an alignment, see if it is acceptable
8732 if ExpA /= No_Uint and then ExpA < ObjA then
8733 Set_Result (Known_Incompatible);
8734 end if;
8736 -- If Expr is not a piece of a larger object, see if size
8737 -- is given. If so, check that it is not too small for the
8738 -- required alignment.
8740 if Offs /= No_Uint then
8741 null;
8743 -- See if Expr is an object with known size
8745 elsif Is_Entity_Name (Expr)
8746 and then Known_Static_Esize (Entity (Expr))
8747 then
8748 SizA := Esize (Entity (Expr));
8750 -- Otherwise, we check the object size of the Expr type
8752 elsif Known_Static_Esize (Etype (Expr)) then
8753 SizA := Esize (Etype (Expr));
8754 end if;
8756 -- If we got a size, see if it is a multiple of the Obj
8757 -- alignment, if not, then the alignment cannot be
8758 -- acceptable, since the size is always a multiple of the
8759 -- alignment.
8761 if SizA /= No_Uint then
8762 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
8763 Set_Result (Known_Incompatible);
8764 end if;
8765 end if;
8766 end if;
8767 end;
8769 -- If we do not know required alignment, any non-zero offset is a
8770 -- potential problem (but certainly may be OK, so result is unknown).
8772 elsif Offs /= No_Uint then
8773 Set_Result (Unknown);
8775 -- If we can't find the result by direct comparison of alignment
8776 -- values, then there is still one case that we can determine known
8777 -- result, and that is when we can determine that the types are the
8778 -- same, and no alignments are specified. Then we known that the
8779 -- alignments are compatible, even if we don't know the alignment
8780 -- value in the front end.
8782 elsif Etype (Obj) = Etype (Expr) then
8784 -- Types are the same, but we have to check for possible size
8785 -- and alignments on the Expr object that may make the alignment
8786 -- different, even though the types are the same.
8788 if Is_Entity_Name (Expr) then
8790 -- First check alignment of the Expr object. Any alignment less
8791 -- than Maximum_Alignment is worrisome since this is the case
8792 -- where we do not know the alignment of Obj.
8794 if Known_Alignment (Entity (Expr))
8795 and then UI_To_Int (Alignment (Entity (Expr))) <
8796 Ttypes.Maximum_Alignment
8797 then
8798 Set_Result (Unknown);
8800 -- Now check size of Expr object. Any size that is not an
8801 -- even multiple of Maximum_Alignment is also worrisome
8802 -- since it may cause the alignment of the object to be less
8803 -- than the alignment of the type.
8805 elsif Known_Static_Esize (Entity (Expr))
8806 and then
8807 (UI_To_Int (Esize (Entity (Expr))) mod
8808 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
8809 /= 0
8810 then
8811 Set_Result (Unknown);
8813 -- Otherwise same type is decisive
8815 else
8816 Set_Result (Known_Compatible);
8817 end if;
8818 end if;
8820 -- Another case to deal with is when there is an explicit size or
8821 -- alignment clause when the types are not the same. If so, then the
8822 -- result is Unknown. We don't need to do this test if the Default is
8823 -- Unknown, since that result will be set in any case.
8825 elsif Default /= Unknown
8826 and then (Has_Size_Clause (Etype (Expr))
8827 or else
8828 Has_Alignment_Clause (Etype (Expr)))
8829 then
8830 Set_Result (Unknown);
8832 -- If no indication found, set default
8834 else
8835 Set_Result (Default);
8836 end if;
8838 -- Return worst result found
8840 return Result;
8841 end Has_Compatible_Alignment_Internal;
8843 -- Start of processing for Has_Compatible_Alignment
8845 begin
8846 -- If Obj has no specified alignment, then set alignment from the type
8847 -- alignment. Perhaps we should always do this, but for sure we should
8848 -- do it when there is an address clause since we can do more if the
8849 -- alignment is known.
8851 if Unknown_Alignment (Obj) then
8852 Set_Alignment (Obj, Alignment (Etype (Obj)));
8853 end if;
8855 -- Now do the internal call that does all the work
8857 return
8858 Has_Compatible_Alignment_Internal (Obj, Expr, Layout_Done, Unknown);
8859 end Has_Compatible_Alignment;
8861 ----------------------
8862 -- Has_Declarations --
8863 ----------------------
8865 function Has_Declarations (N : Node_Id) return Boolean is
8866 begin
8867 return Nkind_In (Nkind (N), N_Accept_Statement,
8868 N_Block_Statement,
8869 N_Compilation_Unit_Aux,
8870 N_Entry_Body,
8871 N_Package_Body,
8872 N_Protected_Body,
8873 N_Subprogram_Body,
8874 N_Task_Body,
8875 N_Package_Specification);
8876 end Has_Declarations;
8878 ---------------------------------
8879 -- Has_Defaulted_Discriminants --
8880 ---------------------------------
8882 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
8883 begin
8884 return Has_Discriminants (Typ)
8885 and then Present (First_Discriminant (Typ))
8886 and then Present (Discriminant_Default_Value
8887 (First_Discriminant (Typ)));
8888 end Has_Defaulted_Discriminants;
8890 -------------------
8891 -- Has_Denormals --
8892 -------------------
8894 function Has_Denormals (E : Entity_Id) return Boolean is
8895 begin
8896 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
8897 end Has_Denormals;
8899 -------------------------------------------
8900 -- Has_Discriminant_Dependent_Constraint --
8901 -------------------------------------------
8903 function Has_Discriminant_Dependent_Constraint
8904 (Comp : Entity_Id) return Boolean
8906 Comp_Decl : constant Node_Id := Parent (Comp);
8907 Subt_Indic : Node_Id;
8908 Constr : Node_Id;
8909 Assn : Node_Id;
8911 begin
8912 -- Discriminants can't depend on discriminants
8914 if Ekind (Comp) = E_Discriminant then
8915 return False;
8917 else
8918 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
8920 if Nkind (Subt_Indic) = N_Subtype_Indication then
8921 Constr := Constraint (Subt_Indic);
8923 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
8924 Assn := First (Constraints (Constr));
8925 while Present (Assn) loop
8926 case Nkind (Assn) is
8927 when N_Subtype_Indication |
8928 N_Range |
8929 N_Identifier
8931 if Depends_On_Discriminant (Assn) then
8932 return True;
8933 end if;
8935 when N_Discriminant_Association =>
8936 if Depends_On_Discriminant (Expression (Assn)) then
8937 return True;
8938 end if;
8940 when others =>
8941 null;
8942 end case;
8944 Next (Assn);
8945 end loop;
8946 end if;
8947 end if;
8948 end if;
8950 return False;
8951 end Has_Discriminant_Dependent_Constraint;
8953 --------------------------------------
8954 -- Has_Effectively_Volatile_Profile --
8955 --------------------------------------
8957 function Has_Effectively_Volatile_Profile
8958 (Subp_Id : Entity_Id) return Boolean
8960 Formal : Entity_Id;
8962 begin
8963 -- Inspect the formal parameters looking for an effectively volatile
8964 -- type.
8966 Formal := First_Formal (Subp_Id);
8967 while Present (Formal) loop
8968 if Is_Effectively_Volatile (Etype (Formal)) then
8969 return True;
8970 end if;
8972 Next_Formal (Formal);
8973 end loop;
8975 -- Inspect the return type of functions
8977 if Ekind_In (Subp_Id, E_Function, E_Generic_Function)
8978 and then Is_Effectively_Volatile (Etype (Subp_Id))
8979 then
8980 return True;
8981 end if;
8983 return False;
8984 end Has_Effectively_Volatile_Profile;
8986 --------------------------
8987 -- Has_Enabled_Property --
8988 --------------------------
8990 function Has_Enabled_Property
8991 (Item_Id : Entity_Id;
8992 Property : Name_Id) return Boolean
8994 function State_Has_Enabled_Property return Boolean;
8995 -- Determine whether a state denoted by Item_Id has the property enabled
8997 function Variable_Has_Enabled_Property return Boolean;
8998 -- Determine whether a variable denoted by Item_Id has the property
8999 -- enabled.
9001 --------------------------------
9002 -- State_Has_Enabled_Property --
9003 --------------------------------
9005 function State_Has_Enabled_Property return Boolean is
9006 Decl : constant Node_Id := Parent (Item_Id);
9007 Opt : Node_Id;
9008 Opt_Nam : Node_Id;
9009 Prop : Node_Id;
9010 Prop_Nam : Node_Id;
9011 Props : Node_Id;
9013 begin
9014 -- The declaration of an external abstract state appears as an
9015 -- extension aggregate. If this is not the case, properties can never
9016 -- be set.
9018 if Nkind (Decl) /= N_Extension_Aggregate then
9019 return False;
9020 end if;
9022 -- When External appears as a simple option, it automatically enables
9023 -- all properties.
9025 Opt := First (Expressions (Decl));
9026 while Present (Opt) loop
9027 if Nkind (Opt) = N_Identifier
9028 and then Chars (Opt) = Name_External
9029 then
9030 return True;
9031 end if;
9033 Next (Opt);
9034 end loop;
9036 -- When External specifies particular properties, inspect those and
9037 -- find the desired one (if any).
9039 Opt := First (Component_Associations (Decl));
9040 while Present (Opt) loop
9041 Opt_Nam := First (Choices (Opt));
9043 if Nkind (Opt_Nam) = N_Identifier
9044 and then Chars (Opt_Nam) = Name_External
9045 then
9046 Props := Expression (Opt);
9048 -- Multiple properties appear as an aggregate
9050 if Nkind (Props) = N_Aggregate then
9052 -- Simple property form
9054 Prop := First (Expressions (Props));
9055 while Present (Prop) loop
9056 if Chars (Prop) = Property then
9057 return True;
9058 end if;
9060 Next (Prop);
9061 end loop;
9063 -- Property with expression form
9065 Prop := First (Component_Associations (Props));
9066 while Present (Prop) loop
9067 Prop_Nam := First (Choices (Prop));
9069 -- The property can be represented in two ways:
9070 -- others => <value>
9071 -- <property> => <value>
9073 if Nkind (Prop_Nam) = N_Others_Choice
9074 or else (Nkind (Prop_Nam) = N_Identifier
9075 and then Chars (Prop_Nam) = Property)
9076 then
9077 return Is_True (Expr_Value (Expression (Prop)));
9078 end if;
9080 Next (Prop);
9081 end loop;
9083 -- Single property
9085 else
9086 return Chars (Props) = Property;
9087 end if;
9088 end if;
9090 Next (Opt);
9091 end loop;
9093 return False;
9094 end State_Has_Enabled_Property;
9096 -----------------------------------
9097 -- Variable_Has_Enabled_Property --
9098 -----------------------------------
9100 function Variable_Has_Enabled_Property return Boolean is
9101 function Is_Enabled (Prag : Node_Id) return Boolean;
9102 -- Determine whether property pragma Prag (if present) denotes an
9103 -- enabled property.
9105 ----------------
9106 -- Is_Enabled --
9107 ----------------
9109 function Is_Enabled (Prag : Node_Id) return Boolean is
9110 Arg1 : Node_Id;
9112 begin
9113 if Present (Prag) then
9114 Arg1 := First (Pragma_Argument_Associations (Prag));
9116 -- The pragma has an optional Boolean expression, the related
9117 -- property is enabled only when the expression evaluates to
9118 -- True.
9120 if Present (Arg1) then
9121 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
9123 -- Otherwise the lack of expression enables the property by
9124 -- default.
9126 else
9127 return True;
9128 end if;
9130 -- The property was never set in the first place
9132 else
9133 return False;
9134 end if;
9135 end Is_Enabled;
9137 -- Local variables
9139 AR : constant Node_Id :=
9140 Get_Pragma (Item_Id, Pragma_Async_Readers);
9141 AW : constant Node_Id :=
9142 Get_Pragma (Item_Id, Pragma_Async_Writers);
9143 ER : constant Node_Id :=
9144 Get_Pragma (Item_Id, Pragma_Effective_Reads);
9145 EW : constant Node_Id :=
9146 Get_Pragma (Item_Id, Pragma_Effective_Writes);
9148 -- Start of processing for Variable_Has_Enabled_Property
9150 begin
9151 -- A non-effectively volatile object can never possess external
9152 -- properties.
9154 if not Is_Effectively_Volatile (Item_Id) then
9155 return False;
9157 -- External properties related to variables come in two flavors -
9158 -- explicit and implicit. The explicit case is characterized by the
9159 -- presence of a property pragma with an optional Boolean flag. The
9160 -- property is enabled when the flag evaluates to True or the flag is
9161 -- missing altogether.
9163 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
9164 return True;
9166 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
9167 return True;
9169 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
9170 return True;
9172 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
9173 return True;
9175 -- The implicit case lacks all property pragmas
9177 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
9178 return True;
9180 else
9181 return False;
9182 end if;
9183 end Variable_Has_Enabled_Property;
9185 -- Start of processing for Has_Enabled_Property
9187 begin
9188 -- Abstract states and variables have a flexible scheme of specifying
9189 -- external properties.
9191 if Ekind (Item_Id) = E_Abstract_State then
9192 return State_Has_Enabled_Property;
9194 elsif Ekind (Item_Id) = E_Variable then
9195 return Variable_Has_Enabled_Property;
9197 -- Otherwise a property is enabled when the related item is effectively
9198 -- volatile.
9200 else
9201 return Is_Effectively_Volatile (Item_Id);
9202 end if;
9203 end Has_Enabled_Property;
9205 -------------------------------------
9206 -- Has_Full_Default_Initialization --
9207 -------------------------------------
9209 function Has_Full_Default_Initialization (Typ : Entity_Id) return Boolean is
9210 Comp : Entity_Id;
9211 Prag : Node_Id;
9213 begin
9214 -- A type subject to pragma Default_Initial_Condition is fully default
9215 -- initialized when the pragma appears with a non-null argument. Since
9216 -- any type may act as the full view of a private type, this check must
9217 -- be performed prior to the specialized tests below.
9219 if Has_DIC (Typ) then
9220 Prag := Get_Pragma (Typ, Pragma_Default_Initial_Condition);
9221 pragma Assert (Present (Prag));
9223 return Is_Verifiable_DIC_Pragma (Prag);
9224 end if;
9226 -- A scalar type is fully default initialized if it is subject to aspect
9227 -- Default_Value.
9229 if Is_Scalar_Type (Typ) then
9230 return Has_Default_Aspect (Typ);
9232 -- An array type is fully default initialized if its element type is
9233 -- scalar and the array type carries aspect Default_Component_Value or
9234 -- the element type is fully default initialized.
9236 elsif Is_Array_Type (Typ) then
9237 return
9238 Has_Default_Aspect (Typ)
9239 or else Has_Full_Default_Initialization (Component_Type (Typ));
9241 -- A protected type, record type, or type extension is fully default
9242 -- initialized if all its components either carry an initialization
9243 -- expression or have a type that is fully default initialized. The
9244 -- parent type of a type extension must be fully default initialized.
9246 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
9248 -- Inspect all entities defined in the scope of the type, looking for
9249 -- uninitialized components.
9251 Comp := First_Entity (Typ);
9252 while Present (Comp) loop
9253 if Ekind (Comp) = E_Component
9254 and then Comes_From_Source (Comp)
9255 and then No (Expression (Parent (Comp)))
9256 and then not Has_Full_Default_Initialization (Etype (Comp))
9257 then
9258 return False;
9259 end if;
9261 Next_Entity (Comp);
9262 end loop;
9264 -- Ensure that the parent type of a type extension is fully default
9265 -- initialized.
9267 if Etype (Typ) /= Typ
9268 and then not Has_Full_Default_Initialization (Etype (Typ))
9269 then
9270 return False;
9271 end if;
9273 -- If we get here, then all components and parent portion are fully
9274 -- default initialized.
9276 return True;
9278 -- A task type is fully default initialized by default
9280 elsif Is_Task_Type (Typ) then
9281 return True;
9283 -- Otherwise the type is not fully default initialized
9285 else
9286 return False;
9287 end if;
9288 end Has_Full_Default_Initialization;
9290 --------------------
9291 -- Has_Infinities --
9292 --------------------
9294 function Has_Infinities (E : Entity_Id) return Boolean is
9295 begin
9296 return
9297 Is_Floating_Point_Type (E)
9298 and then Nkind (Scalar_Range (E)) = N_Range
9299 and then Includes_Infinities (Scalar_Range (E));
9300 end Has_Infinities;
9302 --------------------
9303 -- Has_Interfaces --
9304 --------------------
9306 function Has_Interfaces
9307 (T : Entity_Id;
9308 Use_Full_View : Boolean := True) return Boolean
9310 Typ : Entity_Id := Base_Type (T);
9312 begin
9313 -- Handle concurrent types
9315 if Is_Concurrent_Type (Typ) then
9316 Typ := Corresponding_Record_Type (Typ);
9317 end if;
9319 if not Present (Typ)
9320 or else not Is_Record_Type (Typ)
9321 or else not Is_Tagged_Type (Typ)
9322 then
9323 return False;
9324 end if;
9326 -- Handle private types
9328 if Use_Full_View and then Present (Full_View (Typ)) then
9329 Typ := Full_View (Typ);
9330 end if;
9332 -- Handle concurrent record types
9334 if Is_Concurrent_Record_Type (Typ)
9335 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
9336 then
9337 return True;
9338 end if;
9340 loop
9341 if Is_Interface (Typ)
9342 or else
9343 (Is_Record_Type (Typ)
9344 and then Present (Interfaces (Typ))
9345 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
9346 then
9347 return True;
9348 end if;
9350 exit when Etype (Typ) = Typ
9352 -- Handle private types
9354 or else (Present (Full_View (Etype (Typ)))
9355 and then Full_View (Etype (Typ)) = Typ)
9357 -- Protect frontend against wrong sources with cyclic derivations
9359 or else Etype (Typ) = T;
9361 -- Climb to the ancestor type handling private types
9363 if Present (Full_View (Etype (Typ))) then
9364 Typ := Full_View (Etype (Typ));
9365 else
9366 Typ := Etype (Typ);
9367 end if;
9368 end loop;
9370 return False;
9371 end Has_Interfaces;
9373 --------------------------
9374 -- Has_Max_Queue_Length --
9375 --------------------------
9377 function Has_Max_Queue_Length (Id : Entity_Id) return Boolean is
9378 begin
9379 return
9380 Ekind (Id) = E_Entry
9381 and then Present (Get_Pragma (Id, Pragma_Max_Queue_Length));
9382 end Has_Max_Queue_Length;
9384 ---------------------------------
9385 -- Has_No_Obvious_Side_Effects --
9386 ---------------------------------
9388 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
9389 begin
9390 -- For now handle literals, constants, and non-volatile variables and
9391 -- expressions combining these with operators or short circuit forms.
9393 if Nkind (N) in N_Numeric_Or_String_Literal then
9394 return True;
9396 elsif Nkind (N) = N_Character_Literal then
9397 return True;
9399 elsif Nkind (N) in N_Unary_Op then
9400 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
9402 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
9403 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
9404 and then
9405 Has_No_Obvious_Side_Effects (Right_Opnd (N));
9407 elsif Nkind (N) = N_Expression_With_Actions
9408 and then Is_Empty_List (Actions (N))
9409 then
9410 return Has_No_Obvious_Side_Effects (Expression (N));
9412 elsif Nkind (N) in N_Has_Entity then
9413 return Present (Entity (N))
9414 and then Ekind_In (Entity (N), E_Variable,
9415 E_Constant,
9416 E_Enumeration_Literal,
9417 E_In_Parameter,
9418 E_Out_Parameter,
9419 E_In_Out_Parameter)
9420 and then not Is_Volatile (Entity (N));
9422 else
9423 return False;
9424 end if;
9425 end Has_No_Obvious_Side_Effects;
9427 -----------------------------
9428 -- Has_Non_Null_Refinement --
9429 -----------------------------
9431 function Has_Non_Null_Refinement (Id : Entity_Id) return Boolean is
9432 Constits : Elist_Id;
9434 begin
9435 pragma Assert (Ekind (Id) = E_Abstract_State);
9436 Constits := Refinement_Constituents (Id);
9438 -- For a refinement to be non-null, the first constituent must be
9439 -- anything other than null.
9441 return
9442 Present (Constits)
9443 and then Nkind (Node (First_Elmt (Constits))) /= N_Null;
9444 end Has_Non_Null_Refinement;
9446 -------------------
9447 -- Has_Null_Body --
9448 -------------------
9450 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
9451 Body_Id : Entity_Id;
9452 Decl : Node_Id;
9453 Spec : Node_Id;
9454 Stmt1 : Node_Id;
9455 Stmt2 : Node_Id;
9457 begin
9458 Spec := Parent (Proc_Id);
9459 Decl := Parent (Spec);
9461 -- Retrieve the entity of the procedure body (e.g. invariant proc).
9463 if Nkind (Spec) = N_Procedure_Specification
9464 and then Nkind (Decl) = N_Subprogram_Declaration
9465 then
9466 Body_Id := Corresponding_Body (Decl);
9468 -- The body acts as a spec
9470 else
9471 Body_Id := Proc_Id;
9472 end if;
9474 -- The body will be generated later
9476 if No (Body_Id) then
9477 return False;
9478 end if;
9480 Spec := Parent (Body_Id);
9481 Decl := Parent (Spec);
9483 pragma Assert
9484 (Nkind (Spec) = N_Procedure_Specification
9485 and then Nkind (Decl) = N_Subprogram_Body);
9487 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
9489 -- Look for a null statement followed by an optional return
9490 -- statement.
9492 if Nkind (Stmt1) = N_Null_Statement then
9493 Stmt2 := Next (Stmt1);
9495 if Present (Stmt2) then
9496 return Nkind (Stmt2) = N_Simple_Return_Statement;
9497 else
9498 return True;
9499 end if;
9500 end if;
9502 return False;
9503 end Has_Null_Body;
9505 ------------------------
9506 -- Has_Null_Exclusion --
9507 ------------------------
9509 function Has_Null_Exclusion (N : Node_Id) return Boolean is
9510 begin
9511 case Nkind (N) is
9512 when N_Access_Definition |
9513 N_Access_Function_Definition |
9514 N_Access_Procedure_Definition |
9515 N_Access_To_Object_Definition |
9516 N_Allocator |
9517 N_Derived_Type_Definition |
9518 N_Function_Specification |
9519 N_Subtype_Declaration =>
9520 return Null_Exclusion_Present (N);
9522 when N_Component_Definition |
9523 N_Formal_Object_Declaration |
9524 N_Object_Renaming_Declaration =>
9525 if Present (Subtype_Mark (N)) then
9526 return Null_Exclusion_Present (N);
9527 else pragma Assert (Present (Access_Definition (N)));
9528 return Null_Exclusion_Present (Access_Definition (N));
9529 end if;
9531 when N_Discriminant_Specification =>
9532 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
9533 return Null_Exclusion_Present (Discriminant_Type (N));
9534 else
9535 return Null_Exclusion_Present (N);
9536 end if;
9538 when N_Object_Declaration =>
9539 if Nkind (Object_Definition (N)) = N_Access_Definition then
9540 return Null_Exclusion_Present (Object_Definition (N));
9541 else
9542 return Null_Exclusion_Present (N);
9543 end if;
9545 when N_Parameter_Specification =>
9546 if Nkind (Parameter_Type (N)) = N_Access_Definition then
9547 return Null_Exclusion_Present (Parameter_Type (N));
9548 else
9549 return Null_Exclusion_Present (N);
9550 end if;
9552 when others =>
9553 return False;
9555 end case;
9556 end Has_Null_Exclusion;
9558 ------------------------
9559 -- Has_Null_Extension --
9560 ------------------------
9562 function Has_Null_Extension (T : Entity_Id) return Boolean is
9563 B : constant Entity_Id := Base_Type (T);
9564 Comps : Node_Id;
9565 Ext : Node_Id;
9567 begin
9568 if Nkind (Parent (B)) = N_Full_Type_Declaration
9569 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
9570 then
9571 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
9573 if Present (Ext) then
9574 if Null_Present (Ext) then
9575 return True;
9576 else
9577 Comps := Component_List (Ext);
9579 -- The null component list is rewritten during analysis to
9580 -- include the parent component. Any other component indicates
9581 -- that the extension was not originally null.
9583 return Null_Present (Comps)
9584 or else No (Next (First (Component_Items (Comps))));
9585 end if;
9586 else
9587 return False;
9588 end if;
9590 else
9591 return False;
9592 end if;
9593 end Has_Null_Extension;
9595 -------------------------
9596 -- Has_Null_Refinement --
9597 -------------------------
9599 function Has_Null_Refinement (Id : Entity_Id) return Boolean is
9600 Constits : Elist_Id;
9602 begin
9603 pragma Assert (Ekind (Id) = E_Abstract_State);
9604 Constits := Refinement_Constituents (Id);
9606 -- For a refinement to be null, the state's sole constituent must be a
9607 -- null.
9609 return
9610 Present (Constits)
9611 and then Nkind (Node (First_Elmt (Constits))) = N_Null;
9612 end Has_Null_Refinement;
9614 -------------------------------
9615 -- Has_Overriding_Initialize --
9616 -------------------------------
9618 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
9619 BT : constant Entity_Id := Base_Type (T);
9620 P : Elmt_Id;
9622 begin
9623 if Is_Controlled (BT) then
9624 if Is_RTU (Scope (BT), Ada_Finalization) then
9625 return False;
9627 elsif Present (Primitive_Operations (BT)) then
9628 P := First_Elmt (Primitive_Operations (BT));
9629 while Present (P) loop
9630 declare
9631 Init : constant Entity_Id := Node (P);
9632 Formal : constant Entity_Id := First_Formal (Init);
9633 begin
9634 if Ekind (Init) = E_Procedure
9635 and then Chars (Init) = Name_Initialize
9636 and then Comes_From_Source (Init)
9637 and then Present (Formal)
9638 and then Etype (Formal) = BT
9639 and then No (Next_Formal (Formal))
9640 and then (Ada_Version < Ada_2012
9641 or else not Null_Present (Parent (Init)))
9642 then
9643 return True;
9644 end if;
9645 end;
9647 Next_Elmt (P);
9648 end loop;
9649 end if;
9651 -- Here if type itself does not have a non-null Initialize operation:
9652 -- check immediate ancestor.
9654 if Is_Derived_Type (BT)
9655 and then Has_Overriding_Initialize (Etype (BT))
9656 then
9657 return True;
9658 end if;
9659 end if;
9661 return False;
9662 end Has_Overriding_Initialize;
9664 --------------------------------------
9665 -- Has_Preelaborable_Initialization --
9666 --------------------------------------
9668 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
9669 Has_PE : Boolean;
9671 procedure Check_Components (E : Entity_Id);
9672 -- Check component/discriminant chain, sets Has_PE False if a component
9673 -- or discriminant does not meet the preelaborable initialization rules.
9675 ----------------------
9676 -- Check_Components --
9677 ----------------------
9679 procedure Check_Components (E : Entity_Id) is
9680 Ent : Entity_Id;
9681 Exp : Node_Id;
9683 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
9684 -- Returns True if and only if the expression denoted by N does not
9685 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
9687 ---------------------------------
9688 -- Is_Preelaborable_Expression --
9689 ---------------------------------
9691 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
9692 Exp : Node_Id;
9693 Assn : Node_Id;
9694 Choice : Node_Id;
9695 Comp_Type : Entity_Id;
9696 Is_Array_Aggr : Boolean;
9698 begin
9699 if Is_OK_Static_Expression (N) then
9700 return True;
9702 elsif Nkind (N) = N_Null then
9703 return True;
9705 -- Attributes are allowed in general, even if their prefix is a
9706 -- formal type. (It seems that certain attributes known not to be
9707 -- static might not be allowed, but there are no rules to prevent
9708 -- them.)
9710 elsif Nkind (N) = N_Attribute_Reference then
9711 return True;
9713 -- The name of a discriminant evaluated within its parent type is
9714 -- defined to be preelaborable (10.2.1(8)). Note that we test for
9715 -- names that denote discriminals as well as discriminants to
9716 -- catch references occurring within init procs.
9718 elsif Is_Entity_Name (N)
9719 and then
9720 (Ekind (Entity (N)) = E_Discriminant
9721 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
9722 and then Present (Discriminal_Link (Entity (N)))))
9723 then
9724 return True;
9726 elsif Nkind (N) = N_Qualified_Expression then
9727 return Is_Preelaborable_Expression (Expression (N));
9729 -- For aggregates we have to check that each of the associations
9730 -- is preelaborable.
9732 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
9733 Is_Array_Aggr := Is_Array_Type (Etype (N));
9735 if Is_Array_Aggr then
9736 Comp_Type := Component_Type (Etype (N));
9737 end if;
9739 -- Check the ancestor part of extension aggregates, which must
9740 -- be either the name of a type that has preelaborable init or
9741 -- an expression that is preelaborable.
9743 if Nkind (N) = N_Extension_Aggregate then
9744 declare
9745 Anc_Part : constant Node_Id := Ancestor_Part (N);
9747 begin
9748 if Is_Entity_Name (Anc_Part)
9749 and then Is_Type (Entity (Anc_Part))
9750 then
9751 if not Has_Preelaborable_Initialization
9752 (Entity (Anc_Part))
9753 then
9754 return False;
9755 end if;
9757 elsif not Is_Preelaborable_Expression (Anc_Part) then
9758 return False;
9759 end if;
9760 end;
9761 end if;
9763 -- Check positional associations
9765 Exp := First (Expressions (N));
9766 while Present (Exp) loop
9767 if not Is_Preelaborable_Expression (Exp) then
9768 return False;
9769 end if;
9771 Next (Exp);
9772 end loop;
9774 -- Check named associations
9776 Assn := First (Component_Associations (N));
9777 while Present (Assn) loop
9778 Choice := First (Choices (Assn));
9779 while Present (Choice) loop
9780 if Is_Array_Aggr then
9781 if Nkind (Choice) = N_Others_Choice then
9782 null;
9784 elsif Nkind (Choice) = N_Range then
9785 if not Is_OK_Static_Range (Choice) then
9786 return False;
9787 end if;
9789 elsif not Is_OK_Static_Expression (Choice) then
9790 return False;
9791 end if;
9793 else
9794 Comp_Type := Etype (Choice);
9795 end if;
9797 Next (Choice);
9798 end loop;
9800 -- If the association has a <> at this point, then we have
9801 -- to check whether the component's type has preelaborable
9802 -- initialization. Note that this only occurs when the
9803 -- association's corresponding component does not have a
9804 -- default expression, the latter case having already been
9805 -- expanded as an expression for the association.
9807 if Box_Present (Assn) then
9808 if not Has_Preelaborable_Initialization (Comp_Type) then
9809 return False;
9810 end if;
9812 -- In the expression case we check whether the expression
9813 -- is preelaborable.
9815 elsif
9816 not Is_Preelaborable_Expression (Expression (Assn))
9817 then
9818 return False;
9819 end if;
9821 Next (Assn);
9822 end loop;
9824 -- If we get here then aggregate as a whole is preelaborable
9826 return True;
9828 -- All other cases are not preelaborable
9830 else
9831 return False;
9832 end if;
9833 end Is_Preelaborable_Expression;
9835 -- Start of processing for Check_Components
9837 begin
9838 -- Loop through entities of record or protected type
9840 Ent := E;
9841 while Present (Ent) loop
9843 -- We are interested only in components and discriminants
9845 Exp := Empty;
9847 case Ekind (Ent) is
9848 when E_Component =>
9850 -- Get default expression if any. If there is no declaration
9851 -- node, it means we have an internal entity. The parent and
9852 -- tag fields are examples of such entities. For such cases,
9853 -- we just test the type of the entity.
9855 if Present (Declaration_Node (Ent)) then
9856 Exp := Expression (Declaration_Node (Ent));
9857 end if;
9859 when E_Discriminant =>
9861 -- Note: for a renamed discriminant, the Declaration_Node
9862 -- may point to the one from the ancestor, and have a
9863 -- different expression, so use the proper attribute to
9864 -- retrieve the expression from the derived constraint.
9866 Exp := Discriminant_Default_Value (Ent);
9868 when others =>
9869 goto Check_Next_Entity;
9870 end case;
9872 -- A component has PI if it has no default expression and the
9873 -- component type has PI.
9875 if No (Exp) then
9876 if not Has_Preelaborable_Initialization (Etype (Ent)) then
9877 Has_PE := False;
9878 exit;
9879 end if;
9881 -- Require the default expression to be preelaborable
9883 elsif not Is_Preelaborable_Expression (Exp) then
9884 Has_PE := False;
9885 exit;
9886 end if;
9888 <<Check_Next_Entity>>
9889 Next_Entity (Ent);
9890 end loop;
9891 end Check_Components;
9893 -- Start of processing for Has_Preelaborable_Initialization
9895 begin
9896 -- Immediate return if already marked as known preelaborable init. This
9897 -- covers types for which this function has already been called once
9898 -- and returned True (in which case the result is cached), and also
9899 -- types to which a pragma Preelaborable_Initialization applies.
9901 if Known_To_Have_Preelab_Init (E) then
9902 return True;
9903 end if;
9905 -- If the type is a subtype representing a generic actual type, then
9906 -- test whether its base type has preelaborable initialization since
9907 -- the subtype representing the actual does not inherit this attribute
9908 -- from the actual or formal. (but maybe it should???)
9910 if Is_Generic_Actual_Type (E) then
9911 return Has_Preelaborable_Initialization (Base_Type (E));
9912 end if;
9914 -- All elementary types have preelaborable initialization
9916 if Is_Elementary_Type (E) then
9917 Has_PE := True;
9919 -- Array types have PI if the component type has PI
9921 elsif Is_Array_Type (E) then
9922 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
9924 -- A derived type has preelaborable initialization if its parent type
9925 -- has preelaborable initialization and (in the case of a derived record
9926 -- extension) if the non-inherited components all have preelaborable
9927 -- initialization. However, a user-defined controlled type with an
9928 -- overriding Initialize procedure does not have preelaborable
9929 -- initialization.
9931 elsif Is_Derived_Type (E) then
9933 -- If the derived type is a private extension then it doesn't have
9934 -- preelaborable initialization.
9936 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
9937 return False;
9938 end if;
9940 -- First check whether ancestor type has preelaborable initialization
9942 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
9944 -- If OK, check extension components (if any)
9946 if Has_PE and then Is_Record_Type (E) then
9947 Check_Components (First_Entity (E));
9948 end if;
9950 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
9951 -- with a user defined Initialize procedure does not have PI. If
9952 -- the type is untagged, the control primitives come from a component
9953 -- that has already been checked.
9955 if Has_PE
9956 and then Is_Controlled (E)
9957 and then Is_Tagged_Type (E)
9958 and then Has_Overriding_Initialize (E)
9959 then
9960 Has_PE := False;
9961 end if;
9963 -- Private types not derived from a type having preelaborable init and
9964 -- that are not marked with pragma Preelaborable_Initialization do not
9965 -- have preelaborable initialization.
9967 elsif Is_Private_Type (E) then
9968 return False;
9970 -- Record type has PI if it is non private and all components have PI
9972 elsif Is_Record_Type (E) then
9973 Has_PE := True;
9974 Check_Components (First_Entity (E));
9976 -- Protected types must not have entries, and components must meet
9977 -- same set of rules as for record components.
9979 elsif Is_Protected_Type (E) then
9980 if Has_Entries (E) then
9981 Has_PE := False;
9982 else
9983 Has_PE := True;
9984 Check_Components (First_Entity (E));
9985 Check_Components (First_Private_Entity (E));
9986 end if;
9988 -- Type System.Address always has preelaborable initialization
9990 elsif Is_RTE (E, RE_Address) then
9991 Has_PE := True;
9993 -- In all other cases, type does not have preelaborable initialization
9995 else
9996 return False;
9997 end if;
9999 -- If type has preelaborable initialization, cache result
10001 if Has_PE then
10002 Set_Known_To_Have_Preelab_Init (E);
10003 end if;
10005 return Has_PE;
10006 end Has_Preelaborable_Initialization;
10008 ---------------------------
10009 -- Has_Private_Component --
10010 ---------------------------
10012 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
10013 Btype : Entity_Id := Base_Type (Type_Id);
10014 Component : Entity_Id;
10016 begin
10017 if Error_Posted (Type_Id)
10018 or else Error_Posted (Btype)
10019 then
10020 return False;
10021 end if;
10023 if Is_Class_Wide_Type (Btype) then
10024 Btype := Root_Type (Btype);
10025 end if;
10027 if Is_Private_Type (Btype) then
10028 declare
10029 UT : constant Entity_Id := Underlying_Type (Btype);
10030 begin
10031 if No (UT) then
10032 if No (Full_View (Btype)) then
10033 return not Is_Generic_Type (Btype)
10034 and then
10035 not Is_Generic_Type (Root_Type (Btype));
10036 else
10037 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
10038 end if;
10039 else
10040 return not Is_Frozen (UT) and then Has_Private_Component (UT);
10041 end if;
10042 end;
10044 elsif Is_Array_Type (Btype) then
10045 return Has_Private_Component (Component_Type (Btype));
10047 elsif Is_Record_Type (Btype) then
10048 Component := First_Component (Btype);
10049 while Present (Component) loop
10050 if Has_Private_Component (Etype (Component)) then
10051 return True;
10052 end if;
10054 Next_Component (Component);
10055 end loop;
10057 return False;
10059 elsif Is_Protected_Type (Btype)
10060 and then Present (Corresponding_Record_Type (Btype))
10061 then
10062 return Has_Private_Component (Corresponding_Record_Type (Btype));
10064 else
10065 return False;
10066 end if;
10067 end Has_Private_Component;
10069 ----------------------
10070 -- Has_Signed_Zeros --
10071 ----------------------
10073 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
10074 begin
10075 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
10076 end Has_Signed_Zeros;
10078 ------------------------------
10079 -- Has_Significant_Contract --
10080 ------------------------------
10082 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
10083 Subp_Nam : constant Name_Id := Chars (Subp_Id);
10085 begin
10086 -- _Finalizer procedure
10088 if Subp_Nam = Name_uFinalizer then
10089 return False;
10091 -- _Postconditions procedure
10093 elsif Subp_Nam = Name_uPostconditions then
10094 return False;
10096 -- Predicate function
10098 elsif Ekind (Subp_Id) = E_Function
10099 and then Is_Predicate_Function (Subp_Id)
10100 then
10101 return False;
10103 -- TSS subprogram
10105 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
10106 return False;
10108 else
10109 return True;
10110 end if;
10111 end Has_Significant_Contract;
10113 -----------------------------
10114 -- Has_Static_Array_Bounds --
10115 -----------------------------
10117 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
10118 Ndims : constant Nat := Number_Dimensions (Typ);
10120 Index : Node_Id;
10121 Low : Node_Id;
10122 High : Node_Id;
10124 begin
10125 -- Unconstrained types do not have static bounds
10127 if not Is_Constrained (Typ) then
10128 return False;
10129 end if;
10131 -- First treat string literals specially, as the lower bound and length
10132 -- of string literals are not stored like those of arrays.
10134 -- A string literal always has static bounds
10136 if Ekind (Typ) = E_String_Literal_Subtype then
10137 return True;
10138 end if;
10140 -- Treat all dimensions in turn
10142 Index := First_Index (Typ);
10143 for Indx in 1 .. Ndims loop
10145 -- In case of an illegal index which is not a discrete type, return
10146 -- that the type is not static.
10148 if not Is_Discrete_Type (Etype (Index))
10149 or else Etype (Index) = Any_Type
10150 then
10151 return False;
10152 end if;
10154 Get_Index_Bounds (Index, Low, High);
10156 if Error_Posted (Low) or else Error_Posted (High) then
10157 return False;
10158 end if;
10160 if Is_OK_Static_Expression (Low)
10161 and then
10162 Is_OK_Static_Expression (High)
10163 then
10164 null;
10165 else
10166 return False;
10167 end if;
10169 Next (Index);
10170 end loop;
10172 -- If we fall through the loop, all indexes matched
10174 return True;
10175 end Has_Static_Array_Bounds;
10177 ----------------
10178 -- Has_Stream --
10179 ----------------
10181 function Has_Stream (T : Entity_Id) return Boolean is
10182 E : Entity_Id;
10184 begin
10185 if No (T) then
10186 return False;
10188 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
10189 return True;
10191 elsif Is_Array_Type (T) then
10192 return Has_Stream (Component_Type (T));
10194 elsif Is_Record_Type (T) then
10195 E := First_Component (T);
10196 while Present (E) loop
10197 if Has_Stream (Etype (E)) then
10198 return True;
10199 else
10200 Next_Component (E);
10201 end if;
10202 end loop;
10204 return False;
10206 elsif Is_Private_Type (T) then
10207 return Has_Stream (Underlying_Type (T));
10209 else
10210 return False;
10211 end if;
10212 end Has_Stream;
10214 ----------------
10215 -- Has_Suffix --
10216 ----------------
10218 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
10219 begin
10220 Get_Name_String (Chars (E));
10221 return Name_Buffer (Name_Len) = Suffix;
10222 end Has_Suffix;
10224 ----------------
10225 -- Add_Suffix --
10226 ----------------
10228 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10229 begin
10230 Get_Name_String (Chars (E));
10231 Add_Char_To_Name_Buffer (Suffix);
10232 return Name_Find;
10233 end Add_Suffix;
10235 -------------------
10236 -- Remove_Suffix --
10237 -------------------
10239 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10240 begin
10241 pragma Assert (Has_Suffix (E, Suffix));
10242 Get_Name_String (Chars (E));
10243 Name_Len := Name_Len - 1;
10244 return Name_Find;
10245 end Remove_Suffix;
10247 ----------------------------------
10248 -- Replace_Null_By_Null_Address --
10249 ----------------------------------
10251 procedure Replace_Null_By_Null_Address (N : Node_Id) is
10252 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id);
10253 -- Replace operand Op with a reference to Null_Address when the operand
10254 -- denotes a null Address. Other_Op denotes the other operand.
10256 --------------------------
10257 -- Replace_Null_Operand --
10258 --------------------------
10260 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id) is
10261 begin
10262 -- Check the type of the complementary operand since the N_Null node
10263 -- has not been decorated yet.
10265 if Nkind (Op) = N_Null
10266 and then Is_Descendant_Of_Address (Etype (Other_Op))
10267 then
10268 Rewrite (Op, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (Op)));
10269 end if;
10270 end Replace_Null_Operand;
10272 -- Start of processing for Replace_Null_By_Null_Address
10274 begin
10275 pragma Assert (Relaxed_RM_Semantics);
10276 pragma Assert (Nkind_In (N, N_Null,
10277 N_Op_Eq,
10278 N_Op_Ge,
10279 N_Op_Gt,
10280 N_Op_Le,
10281 N_Op_Lt,
10282 N_Op_Ne));
10284 if Nkind (N) = N_Null then
10285 Rewrite (N, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
10287 else
10288 declare
10289 L : constant Node_Id := Left_Opnd (N);
10290 R : constant Node_Id := Right_Opnd (N);
10292 begin
10293 Replace_Null_Operand (L, Other_Op => R);
10294 Replace_Null_Operand (R, Other_Op => L);
10295 end;
10296 end if;
10297 end Replace_Null_By_Null_Address;
10299 --------------------------
10300 -- Has_Tagged_Component --
10301 --------------------------
10303 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
10304 Comp : Entity_Id;
10306 begin
10307 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
10308 return Has_Tagged_Component (Underlying_Type (Typ));
10310 elsif Is_Array_Type (Typ) then
10311 return Has_Tagged_Component (Component_Type (Typ));
10313 elsif Is_Tagged_Type (Typ) then
10314 return True;
10316 elsif Is_Record_Type (Typ) then
10317 Comp := First_Component (Typ);
10318 while Present (Comp) loop
10319 if Has_Tagged_Component (Etype (Comp)) then
10320 return True;
10321 end if;
10323 Next_Component (Comp);
10324 end loop;
10326 return False;
10328 else
10329 return False;
10330 end if;
10331 end Has_Tagged_Component;
10333 -----------------------------
10334 -- Has_Undefined_Reference --
10335 -----------------------------
10337 function Has_Undefined_Reference (Expr : Node_Id) return Boolean is
10338 Has_Undef_Ref : Boolean := False;
10339 -- Flag set when expression Expr contains at least one undefined
10340 -- reference.
10342 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result;
10343 -- Determine whether N denotes a reference and if it does, whether it is
10344 -- undefined.
10346 ----------------------------
10347 -- Is_Undefined_Reference --
10348 ----------------------------
10350 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result is
10351 begin
10352 if Is_Entity_Name (N)
10353 and then Present (Entity (N))
10354 and then Entity (N) = Any_Id
10355 then
10356 Has_Undef_Ref := True;
10357 return Abandon;
10358 end if;
10360 return OK;
10361 end Is_Undefined_Reference;
10363 procedure Find_Undefined_References is
10364 new Traverse_Proc (Is_Undefined_Reference);
10366 -- Start of processing for Has_Undefined_Reference
10368 begin
10369 Find_Undefined_References (Expr);
10371 return Has_Undef_Ref;
10372 end Has_Undefined_Reference;
10374 ----------------------------
10375 -- Has_Volatile_Component --
10376 ----------------------------
10378 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
10379 Comp : Entity_Id;
10381 begin
10382 if Has_Volatile_Components (Typ) then
10383 return True;
10385 elsif Is_Array_Type (Typ) then
10386 return Is_Volatile (Component_Type (Typ));
10388 elsif Is_Record_Type (Typ) then
10389 Comp := First_Component (Typ);
10390 while Present (Comp) loop
10391 if Is_Volatile_Object (Comp) then
10392 return True;
10393 end if;
10395 Comp := Next_Component (Comp);
10396 end loop;
10397 end if;
10399 return False;
10400 end Has_Volatile_Component;
10402 -------------------------
10403 -- Implementation_Kind --
10404 -------------------------
10406 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
10407 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
10408 Arg : Node_Id;
10409 begin
10410 pragma Assert (Present (Impl_Prag));
10411 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
10412 return Chars (Get_Pragma_Arg (Arg));
10413 end Implementation_Kind;
10415 --------------------------
10416 -- Implements_Interface --
10417 --------------------------
10419 function Implements_Interface
10420 (Typ_Ent : Entity_Id;
10421 Iface_Ent : Entity_Id;
10422 Exclude_Parents : Boolean := False) return Boolean
10424 Ifaces_List : Elist_Id;
10425 Elmt : Elmt_Id;
10426 Iface : Entity_Id := Base_Type (Iface_Ent);
10427 Typ : Entity_Id := Base_Type (Typ_Ent);
10429 begin
10430 if Is_Class_Wide_Type (Typ) then
10431 Typ := Root_Type (Typ);
10432 end if;
10434 if not Has_Interfaces (Typ) then
10435 return False;
10436 end if;
10438 if Is_Class_Wide_Type (Iface) then
10439 Iface := Root_Type (Iface);
10440 end if;
10442 Collect_Interfaces (Typ, Ifaces_List);
10444 Elmt := First_Elmt (Ifaces_List);
10445 while Present (Elmt) loop
10446 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
10447 and then Exclude_Parents
10448 then
10449 null;
10451 elsif Node (Elmt) = Iface then
10452 return True;
10453 end if;
10455 Next_Elmt (Elmt);
10456 end loop;
10458 return False;
10459 end Implements_Interface;
10461 ------------------------------------
10462 -- In_Assertion_Expression_Pragma --
10463 ------------------------------------
10465 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
10466 Par : Node_Id;
10467 Prag : Node_Id := Empty;
10469 begin
10470 -- Climb the parent chain looking for an enclosing pragma
10472 Par := N;
10473 while Present (Par) loop
10474 if Nkind (Par) = N_Pragma then
10475 Prag := Par;
10476 exit;
10478 -- Precondition-like pragmas are expanded into if statements, check
10479 -- the original node instead.
10481 elsif Nkind (Original_Node (Par)) = N_Pragma then
10482 Prag := Original_Node (Par);
10483 exit;
10485 -- The expansion of attribute 'Old generates a constant to capture
10486 -- the result of the prefix. If the parent traversal reaches
10487 -- one of these constants, then the node technically came from a
10488 -- postcondition-like pragma. Note that the Ekind is not tested here
10489 -- because N may be the expression of an object declaration which is
10490 -- currently being analyzed. Such objects carry Ekind of E_Void.
10492 elsif Nkind (Par) = N_Object_Declaration
10493 and then Constant_Present (Par)
10494 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
10495 then
10496 return True;
10498 -- Prevent the search from going too far
10500 elsif Is_Body_Or_Package_Declaration (Par) then
10501 return False;
10502 end if;
10504 Par := Parent (Par);
10505 end loop;
10507 return
10508 Present (Prag)
10509 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
10510 end In_Assertion_Expression_Pragma;
10512 -----------------
10513 -- In_Instance --
10514 -----------------
10516 function In_Instance return Boolean is
10517 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
10518 S : Entity_Id;
10520 begin
10521 S := Current_Scope;
10522 while Present (S) and then S /= Standard_Standard loop
10523 if Ekind_In (S, E_Function, E_Package, E_Procedure)
10524 and then Is_Generic_Instance (S)
10525 then
10526 -- A child instance is always compiled in the context of a parent
10527 -- instance. Nevertheless, the actuals are not analyzed in an
10528 -- instance context. We detect this case by examining the current
10529 -- compilation unit, which must be a child instance, and checking
10530 -- that it is not currently on the scope stack.
10532 if Is_Child_Unit (Curr_Unit)
10533 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
10534 N_Package_Instantiation
10535 and then not In_Open_Scopes (Curr_Unit)
10536 then
10537 return False;
10538 else
10539 return True;
10540 end if;
10541 end if;
10543 S := Scope (S);
10544 end loop;
10546 return False;
10547 end In_Instance;
10549 ----------------------
10550 -- In_Instance_Body --
10551 ----------------------
10553 function In_Instance_Body return Boolean is
10554 S : Entity_Id;
10556 begin
10557 S := Current_Scope;
10558 while Present (S) and then S /= Standard_Standard loop
10559 if Ekind_In (S, E_Function, E_Procedure)
10560 and then Is_Generic_Instance (S)
10561 then
10562 return True;
10564 elsif Ekind (S) = E_Package
10565 and then In_Package_Body (S)
10566 and then Is_Generic_Instance (S)
10567 then
10568 return True;
10569 end if;
10571 S := Scope (S);
10572 end loop;
10574 return False;
10575 end In_Instance_Body;
10577 -----------------------------
10578 -- In_Instance_Not_Visible --
10579 -----------------------------
10581 function In_Instance_Not_Visible return Boolean is
10582 S : Entity_Id;
10584 begin
10585 S := Current_Scope;
10586 while Present (S) and then S /= Standard_Standard loop
10587 if Ekind_In (S, E_Function, E_Procedure)
10588 and then Is_Generic_Instance (S)
10589 then
10590 return True;
10592 elsif Ekind (S) = E_Package
10593 and then (In_Package_Body (S) or else In_Private_Part (S))
10594 and then Is_Generic_Instance (S)
10595 then
10596 return True;
10597 end if;
10599 S := Scope (S);
10600 end loop;
10602 return False;
10603 end In_Instance_Not_Visible;
10605 ------------------------------
10606 -- In_Instance_Visible_Part --
10607 ------------------------------
10609 function In_Instance_Visible_Part return Boolean is
10610 S : Entity_Id;
10612 begin
10613 S := Current_Scope;
10614 while Present (S) and then S /= Standard_Standard loop
10615 if Ekind (S) = E_Package
10616 and then Is_Generic_Instance (S)
10617 and then not In_Package_Body (S)
10618 and then not In_Private_Part (S)
10619 then
10620 return True;
10621 end if;
10623 S := Scope (S);
10624 end loop;
10626 return False;
10627 end In_Instance_Visible_Part;
10629 ---------------------
10630 -- In_Package_Body --
10631 ---------------------
10633 function In_Package_Body return Boolean is
10634 S : Entity_Id;
10636 begin
10637 S := Current_Scope;
10638 while Present (S) and then S /= Standard_Standard loop
10639 if Ekind (S) = E_Package and then In_Package_Body (S) then
10640 return True;
10641 else
10642 S := Scope (S);
10643 end if;
10644 end loop;
10646 return False;
10647 end In_Package_Body;
10649 --------------------------------
10650 -- In_Parameter_Specification --
10651 --------------------------------
10653 function In_Parameter_Specification (N : Node_Id) return Boolean is
10654 PN : Node_Id;
10656 begin
10657 PN := Parent (N);
10658 while Present (PN) loop
10659 if Nkind (PN) = N_Parameter_Specification then
10660 return True;
10661 end if;
10663 PN := Parent (PN);
10664 end loop;
10666 return False;
10667 end In_Parameter_Specification;
10669 --------------------------
10670 -- In_Pragma_Expression --
10671 --------------------------
10673 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
10674 P : Node_Id;
10675 begin
10676 P := Parent (N);
10677 loop
10678 if No (P) then
10679 return False;
10680 elsif Nkind (P) = N_Pragma and then Pragma_Name_Mapped (P) = Nam then
10681 return True;
10682 else
10683 P := Parent (P);
10684 end if;
10685 end loop;
10686 end In_Pragma_Expression;
10688 ---------------------------
10689 -- In_Pre_Post_Condition --
10690 ---------------------------
10692 function In_Pre_Post_Condition (N : Node_Id) return Boolean is
10693 Par : Node_Id;
10694 Prag : Node_Id := Empty;
10695 Prag_Id : Pragma_Id;
10697 begin
10698 -- Climb the parent chain looking for an enclosing pragma
10700 Par := N;
10701 while Present (Par) loop
10702 if Nkind (Par) = N_Pragma then
10703 Prag := Par;
10704 exit;
10706 -- Prevent the search from going too far
10708 elsif Is_Body_Or_Package_Declaration (Par) then
10709 exit;
10710 end if;
10712 Par := Parent (Par);
10713 end loop;
10715 if Present (Prag) then
10716 Prag_Id := Get_Pragma_Id (Prag);
10718 return
10719 Prag_Id = Pragma_Post
10720 or else Prag_Id = Pragma_Post_Class
10721 or else Prag_Id = Pragma_Postcondition
10722 or else Prag_Id = Pragma_Pre
10723 or else Prag_Id = Pragma_Pre_Class
10724 or else Prag_Id = Pragma_Precondition;
10726 -- Otherwise the node is not enclosed by a pre/postcondition pragma
10728 else
10729 return False;
10730 end if;
10731 end In_Pre_Post_Condition;
10733 -------------------------------------
10734 -- In_Reverse_Storage_Order_Object --
10735 -------------------------------------
10737 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
10738 Pref : Node_Id;
10739 Btyp : Entity_Id := Empty;
10741 begin
10742 -- Climb up indexed components
10744 Pref := N;
10745 loop
10746 case Nkind (Pref) is
10747 when N_Selected_Component =>
10748 Pref := Prefix (Pref);
10749 exit;
10751 when N_Indexed_Component =>
10752 Pref := Prefix (Pref);
10754 when others =>
10755 Pref := Empty;
10756 exit;
10757 end case;
10758 end loop;
10760 if Present (Pref) then
10761 Btyp := Base_Type (Etype (Pref));
10762 end if;
10764 return Present (Btyp)
10765 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
10766 and then Reverse_Storage_Order (Btyp);
10767 end In_Reverse_Storage_Order_Object;
10769 --------------------------------------
10770 -- In_Subprogram_Or_Concurrent_Unit --
10771 --------------------------------------
10773 function In_Subprogram_Or_Concurrent_Unit return Boolean is
10774 E : Entity_Id;
10775 K : Entity_Kind;
10777 begin
10778 -- Use scope chain to check successively outer scopes
10780 E := Current_Scope;
10781 loop
10782 K := Ekind (E);
10784 if K in Subprogram_Kind
10785 or else K in Concurrent_Kind
10786 or else K in Generic_Subprogram_Kind
10787 then
10788 return True;
10790 elsif E = Standard_Standard then
10791 return False;
10792 end if;
10794 E := Scope (E);
10795 end loop;
10796 end In_Subprogram_Or_Concurrent_Unit;
10798 ---------------------
10799 -- In_Visible_Part --
10800 ---------------------
10802 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
10803 begin
10804 return Is_Package_Or_Generic_Package (Scope_Id)
10805 and then In_Open_Scopes (Scope_Id)
10806 and then not In_Package_Body (Scope_Id)
10807 and then not In_Private_Part (Scope_Id);
10808 end In_Visible_Part;
10810 --------------------------------
10811 -- Incomplete_Or_Partial_View --
10812 --------------------------------
10814 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
10815 function Inspect_Decls
10816 (Decls : List_Id;
10817 Taft : Boolean := False) return Entity_Id;
10818 -- Check whether a declarative region contains the incomplete or partial
10819 -- view of Id.
10821 -------------------
10822 -- Inspect_Decls --
10823 -------------------
10825 function Inspect_Decls
10826 (Decls : List_Id;
10827 Taft : Boolean := False) return Entity_Id
10829 Decl : Node_Id;
10830 Match : Node_Id;
10832 begin
10833 Decl := First (Decls);
10834 while Present (Decl) loop
10835 Match := Empty;
10837 -- The partial view of a Taft-amendment type is an incomplete
10838 -- type.
10840 if Taft then
10841 if Nkind (Decl) = N_Incomplete_Type_Declaration then
10842 Match := Defining_Identifier (Decl);
10843 end if;
10845 -- Otherwise look for a private type whose full view matches the
10846 -- input type. Note that this checks full_type_declaration nodes
10847 -- to account for derivations from a private type where the type
10848 -- declaration hold the partial view and the full view is an
10849 -- itype.
10851 elsif Nkind_In (Decl, N_Full_Type_Declaration,
10852 N_Private_Extension_Declaration,
10853 N_Private_Type_Declaration)
10854 then
10855 Match := Defining_Identifier (Decl);
10856 end if;
10858 -- Guard against unanalyzed entities
10860 if Present (Match)
10861 and then Is_Type (Match)
10862 and then Present (Full_View (Match))
10863 and then Full_View (Match) = Id
10864 then
10865 return Match;
10866 end if;
10868 Next (Decl);
10869 end loop;
10871 return Empty;
10872 end Inspect_Decls;
10874 -- Local variables
10876 Prev : Entity_Id;
10878 -- Start of processing for Incomplete_Or_Partial_View
10880 begin
10881 -- Deferred constant or incomplete type case
10883 Prev := Current_Entity_In_Scope (Id);
10885 if Present (Prev)
10886 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
10887 and then Present (Full_View (Prev))
10888 and then Full_View (Prev) = Id
10889 then
10890 return Prev;
10891 end if;
10893 -- Private or Taft amendment type case
10895 declare
10896 Pkg : constant Entity_Id := Scope (Id);
10897 Pkg_Decl : Node_Id := Pkg;
10899 begin
10900 if Present (Pkg)
10901 and then Ekind_In (Pkg, E_Generic_Package, E_Package)
10902 then
10903 while Nkind (Pkg_Decl) /= N_Package_Specification loop
10904 Pkg_Decl := Parent (Pkg_Decl);
10905 end loop;
10907 -- It is knows that Typ has a private view, look for it in the
10908 -- visible declarations of the enclosing scope. A special case
10909 -- of this is when the two views have been exchanged - the full
10910 -- appears earlier than the private.
10912 if Has_Private_Declaration (Id) then
10913 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
10915 -- Exchanged view case, look in the private declarations
10917 if No (Prev) then
10918 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
10919 end if;
10921 return Prev;
10923 -- Otherwise if this is the package body, then Typ is a potential
10924 -- Taft amendment type. The incomplete view should be located in
10925 -- the private declarations of the enclosing scope.
10927 elsif In_Package_Body (Pkg) then
10928 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
10929 end if;
10930 end if;
10931 end;
10933 -- The type has no incomplete or private view
10935 return Empty;
10936 end Incomplete_Or_Partial_View;
10938 ----------------------------------
10939 -- Indexed_Component_Bit_Offset --
10940 ----------------------------------
10942 function Indexed_Component_Bit_Offset (N : Node_Id) return Uint is
10943 Exp : constant Node_Id := First (Expressions (N));
10944 Typ : constant Entity_Id := Etype (Prefix (N));
10945 Off : constant Uint := Component_Size (Typ);
10946 Ind : Node_Id;
10948 begin
10949 -- Return early if the component size is not known or variable
10951 if Off = No_Uint or else Off < Uint_0 then
10952 return No_Uint;
10953 end if;
10955 -- Deal with the degenerate case of an empty component
10957 if Off = Uint_0 then
10958 return Off;
10959 end if;
10961 -- Check that both the index value and the low bound are known
10963 if not Compile_Time_Known_Value (Exp) then
10964 return No_Uint;
10965 end if;
10967 Ind := First_Index (Typ);
10968 if No (Ind) then
10969 return No_Uint;
10970 end if;
10972 if Nkind (Ind) = N_Subtype_Indication then
10973 Ind := Constraint (Ind);
10975 if Nkind (Ind) = N_Range_Constraint then
10976 Ind := Range_Expression (Ind);
10977 end if;
10978 end if;
10980 if Nkind (Ind) /= N_Range
10981 or else not Compile_Time_Known_Value (Low_Bound (Ind))
10982 then
10983 return No_Uint;
10984 end if;
10986 -- Return the scaled offset
10988 return Off * (Expr_Value (Exp) - Expr_Value (Low_Bound ((Ind))));
10989 end Indexed_Component_Bit_Offset;
10991 ----------------------------
10992 -- Inherit_Rep_Item_Chain --
10993 ----------------------------
10995 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
10996 Item : Node_Id;
10997 Next_Item : Node_Id;
10999 begin
11000 -- There are several inheritance scenarios to consider depending on
11001 -- whether both types have rep item chains and whether the destination
11002 -- type already inherits part of the source type's rep item chain.
11004 -- 1) The source type lacks a rep item chain
11005 -- From_Typ ---> Empty
11007 -- Typ --------> Item (or Empty)
11009 -- In this case inheritance cannot take place because there are no items
11010 -- to inherit.
11012 -- 2) The destination type lacks a rep item chain
11013 -- From_Typ ---> Item ---> ...
11015 -- Typ --------> Empty
11017 -- Inheritance takes place by setting the First_Rep_Item of the
11018 -- destination type to the First_Rep_Item of the source type.
11019 -- From_Typ ---> Item ---> ...
11020 -- ^
11021 -- Typ -----------+
11023 -- 3.1) Both source and destination types have at least one rep item.
11024 -- The destination type does NOT inherit a rep item from the source
11025 -- type.
11026 -- From_Typ ---> Item ---> Item
11028 -- Typ --------> Item ---> Item
11030 -- Inheritance takes place by setting the Next_Rep_Item of the last item
11031 -- of the destination type to the First_Rep_Item of the source type.
11032 -- From_Typ -------------------> Item ---> Item
11033 -- ^
11034 -- Typ --------> Item ---> Item --+
11036 -- 3.2) Both source and destination types have at least one rep item.
11037 -- The destination type DOES inherit part of the rep item chain of the
11038 -- source type.
11039 -- From_Typ ---> Item ---> Item ---> Item
11040 -- ^
11041 -- Typ --------> Item ------+
11043 -- This rare case arises when the full view of a private extension must
11044 -- inherit the rep item chain from the full view of its parent type and
11045 -- the full view of the parent type contains extra rep items. Currently
11046 -- only invariants may lead to such form of inheritance.
11048 -- type From_Typ is tagged private
11049 -- with Type_Invariant'Class => Item_2;
11051 -- type Typ is new From_Typ with private
11052 -- with Type_Invariant => Item_4;
11054 -- At this point the rep item chains contain the following items
11056 -- From_Typ -----------> Item_2 ---> Item_3
11057 -- ^
11058 -- Typ --------> Item_4 --+
11060 -- The full views of both types may introduce extra invariants
11062 -- type From_Typ is tagged null record
11063 -- with Type_Invariant => Item_1;
11065 -- type Typ is new From_Typ with null record;
11067 -- The full view of Typ would have to inherit any new rep items added to
11068 -- the full view of From_Typ.
11070 -- From_Typ -----------> Item_1 ---> Item_2 ---> Item_3
11071 -- ^
11072 -- Typ --------> Item_4 --+
11074 -- To achieve this form of inheritance, the destination type must first
11075 -- sever the link between its own rep chain and that of the source type,
11076 -- then inheritance 3.1 takes place.
11078 -- Case 1: The source type lacks a rep item chain
11080 if No (First_Rep_Item (From_Typ)) then
11081 return;
11083 -- Case 2: The destination type lacks a rep item chain
11085 elsif No (First_Rep_Item (Typ)) then
11086 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
11088 -- Case 3: Both the source and destination types have at least one rep
11089 -- item. Traverse the rep item chain of the destination type to find the
11090 -- last rep item.
11092 else
11093 Item := Empty;
11094 Next_Item := First_Rep_Item (Typ);
11095 while Present (Next_Item) loop
11097 -- Detect a link between the destination type's rep chain and that
11098 -- of the source type. There are two possibilities:
11100 -- Variant 1
11101 -- Next_Item
11102 -- V
11103 -- From_Typ ---> Item_1 --->
11104 -- ^
11105 -- Typ -----------+
11107 -- Item is Empty
11109 -- Variant 2
11110 -- Next_Item
11111 -- V
11112 -- From_Typ ---> Item_1 ---> Item_2 --->
11113 -- ^
11114 -- Typ --------> Item_3 ------+
11115 -- ^
11116 -- Item
11118 if Has_Rep_Item (From_Typ, Next_Item) then
11119 exit;
11120 end if;
11122 Item := Next_Item;
11123 Next_Item := Next_Rep_Item (Next_Item);
11124 end loop;
11126 -- Inherit the source type's rep item chain
11128 if Present (Item) then
11129 Set_Next_Rep_Item (Item, First_Rep_Item (From_Typ));
11130 else
11131 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
11132 end if;
11133 end if;
11134 end Inherit_Rep_Item_Chain;
11136 ---------------------------------
11137 -- Insert_Explicit_Dereference --
11138 ---------------------------------
11140 procedure Insert_Explicit_Dereference (N : Node_Id) is
11141 New_Prefix : constant Node_Id := Relocate_Node (N);
11142 Ent : Entity_Id := Empty;
11143 Pref : Node_Id;
11144 I : Interp_Index;
11145 It : Interp;
11146 T : Entity_Id;
11148 begin
11149 Save_Interps (N, New_Prefix);
11151 Rewrite (N,
11152 Make_Explicit_Dereference (Sloc (Parent (N)),
11153 Prefix => New_Prefix));
11155 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
11157 if Is_Overloaded (New_Prefix) then
11159 -- The dereference is also overloaded, and its interpretations are
11160 -- the designated types of the interpretations of the original node.
11162 Set_Etype (N, Any_Type);
11164 Get_First_Interp (New_Prefix, I, It);
11165 while Present (It.Nam) loop
11166 T := It.Typ;
11168 if Is_Access_Type (T) then
11169 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
11170 end if;
11172 Get_Next_Interp (I, It);
11173 end loop;
11175 End_Interp_List;
11177 else
11178 -- Prefix is unambiguous: mark the original prefix (which might
11179 -- Come_From_Source) as a reference, since the new (relocated) one
11180 -- won't be taken into account.
11182 if Is_Entity_Name (New_Prefix) then
11183 Ent := Entity (New_Prefix);
11184 Pref := New_Prefix;
11186 -- For a retrieval of a subcomponent of some composite object,
11187 -- retrieve the ultimate entity if there is one.
11189 elsif Nkind_In (New_Prefix, N_Selected_Component,
11190 N_Indexed_Component)
11191 then
11192 Pref := Prefix (New_Prefix);
11193 while Present (Pref)
11194 and then Nkind_In (Pref, N_Selected_Component,
11195 N_Indexed_Component)
11196 loop
11197 Pref := Prefix (Pref);
11198 end loop;
11200 if Present (Pref) and then Is_Entity_Name (Pref) then
11201 Ent := Entity (Pref);
11202 end if;
11203 end if;
11205 -- Place the reference on the entity node
11207 if Present (Ent) then
11208 Generate_Reference (Ent, Pref);
11209 end if;
11210 end if;
11211 end Insert_Explicit_Dereference;
11213 ------------------------------------------
11214 -- Inspect_Deferred_Constant_Completion --
11215 ------------------------------------------
11217 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
11218 Decl : Node_Id;
11220 begin
11221 Decl := First (Decls);
11222 while Present (Decl) loop
11224 -- Deferred constant signature
11226 if Nkind (Decl) = N_Object_Declaration
11227 and then Constant_Present (Decl)
11228 and then No (Expression (Decl))
11230 -- No need to check internally generated constants
11232 and then Comes_From_Source (Decl)
11234 -- The constant is not completed. A full object declaration or a
11235 -- pragma Import complete a deferred constant.
11237 and then not Has_Completion (Defining_Identifier (Decl))
11238 then
11239 Error_Msg_N
11240 ("constant declaration requires initialization expression",
11241 Defining_Identifier (Decl));
11242 end if;
11244 Decl := Next (Decl);
11245 end loop;
11246 end Inspect_Deferred_Constant_Completion;
11248 -----------------------------
11249 -- Install_Generic_Formals --
11250 -----------------------------
11252 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
11253 E : Entity_Id;
11255 begin
11256 pragma Assert (Is_Generic_Subprogram (Subp_Id));
11258 E := First_Entity (Subp_Id);
11259 while Present (E) loop
11260 Install_Entity (E);
11261 Next_Entity (E);
11262 end loop;
11263 end Install_Generic_Formals;
11265 -----------------------------
11266 -- Is_Actual_Out_Parameter --
11267 -----------------------------
11269 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
11270 Formal : Entity_Id;
11271 Call : Node_Id;
11272 begin
11273 Find_Actual (N, Formal, Call);
11274 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
11275 end Is_Actual_Out_Parameter;
11277 -------------------------
11278 -- Is_Actual_Parameter --
11279 -------------------------
11281 function Is_Actual_Parameter (N : Node_Id) return Boolean is
11282 PK : constant Node_Kind := Nkind (Parent (N));
11284 begin
11285 case PK is
11286 when N_Parameter_Association =>
11287 return N = Explicit_Actual_Parameter (Parent (N));
11289 when N_Subprogram_Call =>
11290 return Is_List_Member (N)
11291 and then
11292 List_Containing (N) = Parameter_Associations (Parent (N));
11294 when others =>
11295 return False;
11296 end case;
11297 end Is_Actual_Parameter;
11299 --------------------------------
11300 -- Is_Actual_Tagged_Parameter --
11301 --------------------------------
11303 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
11304 Formal : Entity_Id;
11305 Call : Node_Id;
11306 begin
11307 Find_Actual (N, Formal, Call);
11308 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
11309 end Is_Actual_Tagged_Parameter;
11311 ---------------------
11312 -- Is_Aliased_View --
11313 ---------------------
11315 function Is_Aliased_View (Obj : Node_Id) return Boolean is
11316 E : Entity_Id;
11318 begin
11319 if Is_Entity_Name (Obj) then
11320 E := Entity (Obj);
11322 return
11323 (Is_Object (E)
11324 and then
11325 (Is_Aliased (E)
11326 or else (Present (Renamed_Object (E))
11327 and then Is_Aliased_View (Renamed_Object (E)))))
11329 or else ((Is_Formal (E)
11330 or else Ekind_In (E, E_Generic_In_Out_Parameter,
11331 E_Generic_In_Parameter))
11332 and then Is_Tagged_Type (Etype (E)))
11334 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
11336 -- Current instance of type, either directly or as rewritten
11337 -- reference to the current object.
11339 or else (Is_Entity_Name (Original_Node (Obj))
11340 and then Present (Entity (Original_Node (Obj)))
11341 and then Is_Type (Entity (Original_Node (Obj))))
11343 or else (Is_Type (E) and then E = Current_Scope)
11345 or else (Is_Incomplete_Or_Private_Type (E)
11346 and then Full_View (E) = Current_Scope)
11348 -- Ada 2012 AI05-0053: the return object of an extended return
11349 -- statement is aliased if its type is immutably limited.
11351 or else (Is_Return_Object (E)
11352 and then Is_Limited_View (Etype (E)));
11354 elsif Nkind (Obj) = N_Selected_Component then
11355 return Is_Aliased (Entity (Selector_Name (Obj)));
11357 elsif Nkind (Obj) = N_Indexed_Component then
11358 return Has_Aliased_Components (Etype (Prefix (Obj)))
11359 or else
11360 (Is_Access_Type (Etype (Prefix (Obj)))
11361 and then Has_Aliased_Components
11362 (Designated_Type (Etype (Prefix (Obj)))));
11364 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
11365 return Is_Tagged_Type (Etype (Obj))
11366 and then Is_Aliased_View (Expression (Obj));
11368 elsif Nkind (Obj) = N_Explicit_Dereference then
11369 return Nkind (Original_Node (Obj)) /= N_Function_Call;
11371 else
11372 return False;
11373 end if;
11374 end Is_Aliased_View;
11376 -------------------------
11377 -- Is_Ancestor_Package --
11378 -------------------------
11380 function Is_Ancestor_Package
11381 (E1 : Entity_Id;
11382 E2 : Entity_Id) return Boolean
11384 Par : Entity_Id;
11386 begin
11387 Par := E2;
11388 while Present (Par) and then Par /= Standard_Standard loop
11389 if Par = E1 then
11390 return True;
11391 end if;
11393 Par := Scope (Par);
11394 end loop;
11396 return False;
11397 end Is_Ancestor_Package;
11399 ----------------------
11400 -- Is_Atomic_Object --
11401 ----------------------
11403 function Is_Atomic_Object (N : Node_Id) return Boolean is
11405 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
11406 -- Determines if given object has atomic components
11408 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
11409 -- If prefix is an implicit dereference, examine designated type
11411 ----------------------
11412 -- Is_Atomic_Prefix --
11413 ----------------------
11415 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
11416 begin
11417 if Is_Access_Type (Etype (N)) then
11418 return
11419 Has_Atomic_Components (Designated_Type (Etype (N)));
11420 else
11421 return Object_Has_Atomic_Components (N);
11422 end if;
11423 end Is_Atomic_Prefix;
11425 ----------------------------------
11426 -- Object_Has_Atomic_Components --
11427 ----------------------------------
11429 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
11430 begin
11431 if Has_Atomic_Components (Etype (N))
11432 or else Is_Atomic (Etype (N))
11433 then
11434 return True;
11436 elsif Is_Entity_Name (N)
11437 and then (Has_Atomic_Components (Entity (N))
11438 or else Is_Atomic (Entity (N)))
11439 then
11440 return True;
11442 elsif Nkind (N) = N_Selected_Component
11443 and then Is_Atomic (Entity (Selector_Name (N)))
11444 then
11445 return True;
11447 elsif Nkind (N) = N_Indexed_Component
11448 or else Nkind (N) = N_Selected_Component
11449 then
11450 return Is_Atomic_Prefix (Prefix (N));
11452 else
11453 return False;
11454 end if;
11455 end Object_Has_Atomic_Components;
11457 -- Start of processing for Is_Atomic_Object
11459 begin
11460 -- Predicate is not relevant to subprograms
11462 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
11463 return False;
11465 elsif Is_Atomic (Etype (N))
11466 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
11467 then
11468 return True;
11470 elsif Nkind (N) = N_Selected_Component
11471 and then Is_Atomic (Entity (Selector_Name (N)))
11472 then
11473 return True;
11475 elsif Nkind (N) = N_Indexed_Component
11476 or else Nkind (N) = N_Selected_Component
11477 then
11478 return Is_Atomic_Prefix (Prefix (N));
11480 else
11481 return False;
11482 end if;
11483 end Is_Atomic_Object;
11485 -----------------------------
11486 -- Is_Atomic_Or_VFA_Object --
11487 -----------------------------
11489 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
11490 begin
11491 return Is_Atomic_Object (N)
11492 or else (Is_Object_Reference (N)
11493 and then Is_Entity_Name (N)
11494 and then (Is_Volatile_Full_Access (Entity (N))
11495 or else
11496 Is_Volatile_Full_Access (Etype (Entity (N)))));
11497 end Is_Atomic_Or_VFA_Object;
11499 -------------------------
11500 -- Is_Attribute_Result --
11501 -------------------------
11503 function Is_Attribute_Result (N : Node_Id) return Boolean is
11504 begin
11505 return Nkind (N) = N_Attribute_Reference
11506 and then Attribute_Name (N) = Name_Result;
11507 end Is_Attribute_Result;
11509 -------------------------
11510 -- Is_Attribute_Update --
11511 -------------------------
11513 function Is_Attribute_Update (N : Node_Id) return Boolean is
11514 begin
11515 return Nkind (N) = N_Attribute_Reference
11516 and then Attribute_Name (N) = Name_Update;
11517 end Is_Attribute_Update;
11519 ------------------------------------
11520 -- Is_Body_Or_Package_Declaration --
11521 ------------------------------------
11523 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
11524 begin
11525 return Nkind_In (N, N_Entry_Body,
11526 N_Package_Body,
11527 N_Package_Declaration,
11528 N_Protected_Body,
11529 N_Subprogram_Body,
11530 N_Task_Body);
11531 end Is_Body_Or_Package_Declaration;
11533 -----------------------
11534 -- Is_Bounded_String --
11535 -----------------------
11537 function Is_Bounded_String (T : Entity_Id) return Boolean is
11538 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
11540 begin
11541 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
11542 -- Super_String, or one of the [Wide_]Wide_ versions. This will
11543 -- be True for all the Bounded_String types in instances of the
11544 -- Generic_Bounded_Length generics, and for types derived from those.
11546 return Present (Under)
11547 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
11548 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
11549 Is_RTE (Root_Type (Under), RO_WW_Super_String));
11550 end Is_Bounded_String;
11552 -------------------------
11553 -- Is_Child_Or_Sibling --
11554 -------------------------
11556 function Is_Child_Or_Sibling
11557 (Pack_1 : Entity_Id;
11558 Pack_2 : Entity_Id) return Boolean
11560 function Distance_From_Standard (Pack : Entity_Id) return Nat;
11561 -- Given an arbitrary package, return the number of "climbs" necessary
11562 -- to reach scope Standard_Standard.
11564 procedure Equalize_Depths
11565 (Pack : in out Entity_Id;
11566 Depth : in out Nat;
11567 Depth_To_Reach : Nat);
11568 -- Given an arbitrary package, its depth and a target depth to reach,
11569 -- climb the scope chain until the said depth is reached. The pointer
11570 -- to the package and its depth a modified during the climb.
11572 ----------------------------
11573 -- Distance_From_Standard --
11574 ----------------------------
11576 function Distance_From_Standard (Pack : Entity_Id) return Nat is
11577 Dist : Nat;
11578 Scop : Entity_Id;
11580 begin
11581 Dist := 0;
11582 Scop := Pack;
11583 while Present (Scop) and then Scop /= Standard_Standard loop
11584 Dist := Dist + 1;
11585 Scop := Scope (Scop);
11586 end loop;
11588 return Dist;
11589 end Distance_From_Standard;
11591 ---------------------
11592 -- Equalize_Depths --
11593 ---------------------
11595 procedure Equalize_Depths
11596 (Pack : in out Entity_Id;
11597 Depth : in out Nat;
11598 Depth_To_Reach : Nat)
11600 begin
11601 -- The package must be at a greater or equal depth
11603 if Depth < Depth_To_Reach then
11604 raise Program_Error;
11605 end if;
11607 -- Climb the scope chain until the desired depth is reached
11609 while Present (Pack) and then Depth /= Depth_To_Reach loop
11610 Pack := Scope (Pack);
11611 Depth := Depth - 1;
11612 end loop;
11613 end Equalize_Depths;
11615 -- Local variables
11617 P_1 : Entity_Id := Pack_1;
11618 P_1_Child : Boolean := False;
11619 P_1_Depth : Nat := Distance_From_Standard (P_1);
11620 P_2 : Entity_Id := Pack_2;
11621 P_2_Child : Boolean := False;
11622 P_2_Depth : Nat := Distance_From_Standard (P_2);
11624 -- Start of processing for Is_Child_Or_Sibling
11626 begin
11627 pragma Assert
11628 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
11630 -- Both packages denote the same entity, therefore they cannot be
11631 -- children or siblings.
11633 if P_1 = P_2 then
11634 return False;
11636 -- One of the packages is at a deeper level than the other. Note that
11637 -- both may still come from differen hierarchies.
11639 -- (root) P_2
11640 -- / \ :
11641 -- X P_2 or X
11642 -- : :
11643 -- P_1 P_1
11645 elsif P_1_Depth > P_2_Depth then
11646 Equalize_Depths
11647 (Pack => P_1,
11648 Depth => P_1_Depth,
11649 Depth_To_Reach => P_2_Depth);
11650 P_1_Child := True;
11652 -- (root) P_1
11653 -- / \ :
11654 -- P_1 X or X
11655 -- : :
11656 -- P_2 P_2
11658 elsif P_2_Depth > P_1_Depth then
11659 Equalize_Depths
11660 (Pack => P_2,
11661 Depth => P_2_Depth,
11662 Depth_To_Reach => P_1_Depth);
11663 P_2_Child := True;
11664 end if;
11666 -- At this stage the package pointers have been elevated to the same
11667 -- depth. If the related entities are the same, then one package is a
11668 -- potential child of the other:
11670 -- P_1
11671 -- :
11672 -- X became P_1 P_2 or vica versa
11673 -- :
11674 -- P_2
11676 if P_1 = P_2 then
11677 if P_1_Child then
11678 return Is_Child_Unit (Pack_1);
11680 else pragma Assert (P_2_Child);
11681 return Is_Child_Unit (Pack_2);
11682 end if;
11684 -- The packages may come from the same package chain or from entirely
11685 -- different hierarcies. To determine this, climb the scope stack until
11686 -- a common root is found.
11688 -- (root) (root 1) (root 2)
11689 -- / \ | |
11690 -- P_1 P_2 P_1 P_2
11692 else
11693 while Present (P_1) and then Present (P_2) loop
11695 -- The two packages may be siblings
11697 if P_1 = P_2 then
11698 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
11699 end if;
11701 P_1 := Scope (P_1);
11702 P_2 := Scope (P_2);
11703 end loop;
11704 end if;
11706 return False;
11707 end Is_Child_Or_Sibling;
11709 -----------------------------
11710 -- Is_Concurrent_Interface --
11711 -----------------------------
11713 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
11714 begin
11715 return Is_Interface (T)
11716 and then
11717 (Is_Protected_Interface (T)
11718 or else Is_Synchronized_Interface (T)
11719 or else Is_Task_Interface (T));
11720 end Is_Concurrent_Interface;
11722 -----------------------
11723 -- Is_Constant_Bound --
11724 -----------------------
11726 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
11727 begin
11728 if Compile_Time_Known_Value (Exp) then
11729 return True;
11731 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
11732 return Is_Constant_Object (Entity (Exp))
11733 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
11735 elsif Nkind (Exp) in N_Binary_Op then
11736 return Is_Constant_Bound (Left_Opnd (Exp))
11737 and then Is_Constant_Bound (Right_Opnd (Exp))
11738 and then Scope (Entity (Exp)) = Standard_Standard;
11740 else
11741 return False;
11742 end if;
11743 end Is_Constant_Bound;
11745 ---------------------------
11746 -- Is_Container_Element --
11747 ---------------------------
11749 function Is_Container_Element (Exp : Node_Id) return Boolean is
11750 Loc : constant Source_Ptr := Sloc (Exp);
11751 Pref : constant Node_Id := Prefix (Exp);
11753 Call : Node_Id;
11754 -- Call to an indexing aspect
11756 Cont_Typ : Entity_Id;
11757 -- The type of the container being accessed
11759 Elem_Typ : Entity_Id;
11760 -- Its element type
11762 Indexing : Entity_Id;
11763 Is_Const : Boolean;
11764 -- Indicates that constant indexing is used, and the element is thus
11765 -- a constant.
11767 Ref_Typ : Entity_Id;
11768 -- The reference type returned by the indexing operation
11770 begin
11771 -- If C is a container, in a context that imposes the element type of
11772 -- that container, the indexing notation C (X) is rewritten as:
11774 -- Indexing (C, X).Discr.all
11776 -- where Indexing is one of the indexing aspects of the container.
11777 -- If the context does not require a reference, the construct can be
11778 -- rewritten as
11780 -- Element (C, X)
11782 -- First, verify that the construct has the proper form
11784 if not Expander_Active then
11785 return False;
11787 elsif Nkind (Pref) /= N_Selected_Component then
11788 return False;
11790 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
11791 return False;
11793 else
11794 Call := Prefix (Pref);
11795 Ref_Typ := Etype (Call);
11796 end if;
11798 if not Has_Implicit_Dereference (Ref_Typ)
11799 or else No (First (Parameter_Associations (Call)))
11800 or else not Is_Entity_Name (Name (Call))
11801 then
11802 return False;
11803 end if;
11805 -- Retrieve type of container object, and its iterator aspects
11807 Cont_Typ := Etype (First (Parameter_Associations (Call)));
11808 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
11809 Is_Const := False;
11811 if No (Indexing) then
11813 -- Container should have at least one indexing operation
11815 return False;
11817 elsif Entity (Name (Call)) /= Entity (Indexing) then
11819 -- This may be a variable indexing operation
11821 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
11823 if No (Indexing)
11824 or else Entity (Name (Call)) /= Entity (Indexing)
11825 then
11826 return False;
11827 end if;
11829 else
11830 Is_Const := True;
11831 end if;
11833 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
11835 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
11836 return False;
11837 end if;
11839 -- Check that the expression is not the target of an assignment, in
11840 -- which case the rewriting is not possible.
11842 if not Is_Const then
11843 declare
11844 Par : Node_Id;
11846 begin
11847 Par := Exp;
11848 while Present (Par)
11849 loop
11850 if Nkind (Parent (Par)) = N_Assignment_Statement
11851 and then Par = Name (Parent (Par))
11852 then
11853 return False;
11855 -- A renaming produces a reference, and the transformation
11856 -- does not apply.
11858 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
11859 return False;
11861 elsif Nkind_In
11862 (Nkind (Parent (Par)), N_Function_Call,
11863 N_Procedure_Call_Statement,
11864 N_Entry_Call_Statement)
11865 then
11866 -- Check that the element is not part of an actual for an
11867 -- in-out parameter.
11869 declare
11870 F : Entity_Id;
11871 A : Node_Id;
11873 begin
11874 F := First_Formal (Entity (Name (Parent (Par))));
11875 A := First (Parameter_Associations (Parent (Par)));
11876 while Present (F) loop
11877 if A = Par and then Ekind (F) /= E_In_Parameter then
11878 return False;
11879 end if;
11881 Next_Formal (F);
11882 Next (A);
11883 end loop;
11884 end;
11886 -- E_In_Parameter in a call: element is not modified.
11888 exit;
11889 end if;
11891 Par := Parent (Par);
11892 end loop;
11893 end;
11894 end if;
11896 -- The expression has the proper form and the context requires the
11897 -- element type. Retrieve the Element function of the container and
11898 -- rewrite the construct as a call to it.
11900 declare
11901 Op : Elmt_Id;
11903 begin
11904 Op := First_Elmt (Primitive_Operations (Cont_Typ));
11905 while Present (Op) loop
11906 exit when Chars (Node (Op)) = Name_Element;
11907 Next_Elmt (Op);
11908 end loop;
11910 if No (Op) then
11911 return False;
11913 else
11914 Rewrite (Exp,
11915 Make_Function_Call (Loc,
11916 Name => New_Occurrence_Of (Node (Op), Loc),
11917 Parameter_Associations => Parameter_Associations (Call)));
11918 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
11919 return True;
11920 end if;
11921 end;
11922 end Is_Container_Element;
11924 ----------------------------
11925 -- Is_Contract_Annotation --
11926 ----------------------------
11928 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
11929 begin
11930 return Is_Package_Contract_Annotation (Item)
11931 or else
11932 Is_Subprogram_Contract_Annotation (Item);
11933 end Is_Contract_Annotation;
11935 --------------------------------------
11936 -- Is_Controlling_Limited_Procedure --
11937 --------------------------------------
11939 function Is_Controlling_Limited_Procedure
11940 (Proc_Nam : Entity_Id) return Boolean
11942 Param_Typ : Entity_Id := Empty;
11944 begin
11945 if Ekind (Proc_Nam) = E_Procedure
11946 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
11947 then
11948 Param_Typ := Etype (Parameter_Type (First (
11949 Parameter_Specifications (Parent (Proc_Nam)))));
11951 -- In this case where an Itype was created, the procedure call has been
11952 -- rewritten.
11954 elsif Present (Associated_Node_For_Itype (Proc_Nam))
11955 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
11956 and then
11957 Present (Parameter_Associations
11958 (Associated_Node_For_Itype (Proc_Nam)))
11959 then
11960 Param_Typ :=
11961 Etype (First (Parameter_Associations
11962 (Associated_Node_For_Itype (Proc_Nam))));
11963 end if;
11965 if Present (Param_Typ) then
11966 return
11967 Is_Interface (Param_Typ)
11968 and then Is_Limited_Record (Param_Typ);
11969 end if;
11971 return False;
11972 end Is_Controlling_Limited_Procedure;
11974 -----------------------------
11975 -- Is_CPP_Constructor_Call --
11976 -----------------------------
11978 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
11979 begin
11980 return Nkind (N) = N_Function_Call
11981 and then Is_CPP_Class (Etype (Etype (N)))
11982 and then Is_Constructor (Entity (Name (N)))
11983 and then Is_Imported (Entity (Name (N)));
11984 end Is_CPP_Constructor_Call;
11986 -------------------------
11987 -- Is_Current_Instance --
11988 -------------------------
11990 function Is_Current_Instance (N : Node_Id) return Boolean is
11991 Typ : constant Entity_Id := Entity (N);
11992 P : Node_Id;
11994 begin
11995 -- Simplest case: entity is a concurrent type and we are currently
11996 -- inside the body. This will eventually be expanded into a
11997 -- call to Self (for tasks) or _object (for protected objects).
11999 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
12000 return True;
12002 else
12003 -- Check whether the context is a (sub)type declaration for the
12004 -- type entity.
12006 P := Parent (N);
12007 while Present (P) loop
12008 if Nkind_In (P, N_Full_Type_Declaration,
12009 N_Private_Type_Declaration,
12010 N_Subtype_Declaration)
12011 and then Comes_From_Source (P)
12012 and then Defining_Entity (P) = Typ
12013 then
12014 return True;
12016 -- A subtype name may appear in an aspect specification for a
12017 -- Predicate_Failure aspect, for which we do not construct a
12018 -- wrapper procedure. The subtype will be replaced by the
12019 -- expression being tested when the corresponding predicate
12020 -- check is expanded.
12022 elsif Nkind (P) = N_Aspect_Specification
12023 and then Nkind (Parent (P)) = N_Subtype_Declaration
12024 then
12025 return True;
12027 elsif Nkind (P) = N_Pragma
12028 and then
12029 Get_Pragma_Id (P) = Pragma_Predicate_Failure
12030 then
12031 return True;
12032 end if;
12034 P := Parent (P);
12035 end loop;
12036 end if;
12038 -- In any other context this is not a current occurrence
12040 return False;
12041 end Is_Current_Instance;
12043 --------------------
12044 -- Is_Declaration --
12045 --------------------
12047 function Is_Declaration (N : Node_Id) return Boolean is
12048 begin
12049 case Nkind (N) is
12050 when N_Abstract_Subprogram_Declaration |
12051 N_Exception_Declaration |
12052 N_Exception_Renaming_Declaration |
12053 N_Full_Type_Declaration |
12054 N_Generic_Function_Renaming_Declaration |
12055 N_Generic_Package_Declaration |
12056 N_Generic_Package_Renaming_Declaration |
12057 N_Generic_Procedure_Renaming_Declaration |
12058 N_Generic_Subprogram_Declaration |
12059 N_Number_Declaration |
12060 N_Object_Declaration |
12061 N_Object_Renaming_Declaration |
12062 N_Package_Declaration |
12063 N_Package_Renaming_Declaration |
12064 N_Private_Extension_Declaration |
12065 N_Private_Type_Declaration |
12066 N_Subprogram_Declaration |
12067 N_Subprogram_Renaming_Declaration |
12068 N_Subtype_Declaration =>
12069 return True;
12071 when others =>
12072 return False;
12073 end case;
12074 end Is_Declaration;
12076 --------------------------------
12077 -- Is_Declared_Within_Variant --
12078 --------------------------------
12080 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
12081 Comp_Decl : constant Node_Id := Parent (Comp);
12082 Comp_List : constant Node_Id := Parent (Comp_Decl);
12083 begin
12084 return Nkind (Parent (Comp_List)) = N_Variant;
12085 end Is_Declared_Within_Variant;
12087 ----------------------------------------------
12088 -- Is_Dependent_Component_Of_Mutable_Object --
12089 ----------------------------------------------
12091 function Is_Dependent_Component_Of_Mutable_Object
12092 (Object : Node_Id) return Boolean
12094 P : Node_Id;
12095 Prefix_Type : Entity_Id;
12096 P_Aliased : Boolean := False;
12097 Comp : Entity_Id;
12099 Deref : Node_Id := Object;
12100 -- Dereference node, in something like X.all.Y(2)
12102 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
12104 begin
12105 -- Find the dereference node if any
12107 while Nkind_In (Deref, N_Indexed_Component,
12108 N_Selected_Component,
12109 N_Slice)
12110 loop
12111 Deref := Prefix (Deref);
12112 end loop;
12114 -- Ada 2005: If we have a component or slice of a dereference,
12115 -- something like X.all.Y (2), and the type of X is access-to-constant,
12116 -- Is_Variable will return False, because it is indeed a constant
12117 -- view. But it might be a view of a variable object, so we want the
12118 -- following condition to be True in that case.
12120 if Is_Variable (Object)
12121 or else (Ada_Version >= Ada_2005
12122 and then Nkind (Deref) = N_Explicit_Dereference)
12123 then
12124 if Nkind (Object) = N_Selected_Component then
12125 P := Prefix (Object);
12126 Prefix_Type := Etype (P);
12128 if Is_Entity_Name (P) then
12129 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
12130 Prefix_Type := Base_Type (Prefix_Type);
12131 end if;
12133 if Is_Aliased (Entity (P)) then
12134 P_Aliased := True;
12135 end if;
12137 -- A discriminant check on a selected component may be expanded
12138 -- into a dereference when removing side-effects. Recover the
12139 -- original node and its type, which may be unconstrained.
12141 elsif Nkind (P) = N_Explicit_Dereference
12142 and then not (Comes_From_Source (P))
12143 then
12144 P := Original_Node (P);
12145 Prefix_Type := Etype (P);
12147 else
12148 -- Check for prefix being an aliased component???
12150 null;
12152 end if;
12154 -- A heap object is constrained by its initial value
12156 -- Ada 2005 (AI-363): Always assume the object could be mutable in
12157 -- the dereferenced case, since the access value might denote an
12158 -- unconstrained aliased object, whereas in Ada 95 the designated
12159 -- object is guaranteed to be constrained. A worst-case assumption
12160 -- has to apply in Ada 2005 because we can't tell at compile
12161 -- time whether the object is "constrained by its initial value"
12162 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
12163 -- rules (these rules are acknowledged to need fixing).
12165 if Ada_Version < Ada_2005 then
12166 if Is_Access_Type (Prefix_Type)
12167 or else Nkind (P) = N_Explicit_Dereference
12168 then
12169 return False;
12170 end if;
12172 else pragma Assert (Ada_Version >= Ada_2005);
12173 if Is_Access_Type (Prefix_Type) then
12175 -- If the access type is pool-specific, and there is no
12176 -- constrained partial view of the designated type, then the
12177 -- designated object is known to be constrained.
12179 if Ekind (Prefix_Type) = E_Access_Type
12180 and then not Object_Type_Has_Constrained_Partial_View
12181 (Typ => Designated_Type (Prefix_Type),
12182 Scop => Current_Scope)
12183 then
12184 return False;
12186 -- Otherwise (general access type, or there is a constrained
12187 -- partial view of the designated type), we need to check
12188 -- based on the designated type.
12190 else
12191 Prefix_Type := Designated_Type (Prefix_Type);
12192 end if;
12193 end if;
12194 end if;
12196 Comp :=
12197 Original_Record_Component (Entity (Selector_Name (Object)));
12199 -- As per AI-0017, the renaming is illegal in a generic body, even
12200 -- if the subtype is indefinite.
12202 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
12204 if not Is_Constrained (Prefix_Type)
12205 and then (Is_Definite_Subtype (Prefix_Type)
12206 or else
12207 (Is_Generic_Type (Prefix_Type)
12208 and then Ekind (Current_Scope) = E_Generic_Package
12209 and then In_Package_Body (Current_Scope)))
12211 and then (Is_Declared_Within_Variant (Comp)
12212 or else Has_Discriminant_Dependent_Constraint (Comp))
12213 and then (not P_Aliased or else Ada_Version >= Ada_2005)
12214 then
12215 return True;
12217 -- If the prefix is of an access type at this point, then we want
12218 -- to return False, rather than calling this function recursively
12219 -- on the access object (which itself might be a discriminant-
12220 -- dependent component of some other object, but that isn't
12221 -- relevant to checking the object passed to us). This avoids
12222 -- issuing wrong errors when compiling with -gnatc, where there
12223 -- can be implicit dereferences that have not been expanded.
12225 elsif Is_Access_Type (Etype (Prefix (Object))) then
12226 return False;
12228 else
12229 return
12230 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
12231 end if;
12233 elsif Nkind (Object) = N_Indexed_Component
12234 or else Nkind (Object) = N_Slice
12235 then
12236 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
12238 -- A type conversion that Is_Variable is a view conversion:
12239 -- go back to the denoted object.
12241 elsif Nkind (Object) = N_Type_Conversion then
12242 return
12243 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
12244 end if;
12245 end if;
12247 return False;
12248 end Is_Dependent_Component_Of_Mutable_Object;
12250 ---------------------
12251 -- Is_Dereferenced --
12252 ---------------------
12254 function Is_Dereferenced (N : Node_Id) return Boolean is
12255 P : constant Node_Id := Parent (N);
12256 begin
12257 return Nkind_In (P, N_Selected_Component,
12258 N_Explicit_Dereference,
12259 N_Indexed_Component,
12260 N_Slice)
12261 and then Prefix (P) = N;
12262 end Is_Dereferenced;
12264 ----------------------
12265 -- Is_Descendant_Of --
12266 ----------------------
12268 function Is_Descendant_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
12269 T : Entity_Id;
12270 Etyp : Entity_Id;
12272 begin
12273 pragma Assert (Nkind (T1) in N_Entity);
12274 pragma Assert (Nkind (T2) in N_Entity);
12276 T := Base_Type (T1);
12278 -- Immediate return if the types match
12280 if T = T2 then
12281 return True;
12283 -- Comment needed here ???
12285 elsif Ekind (T) = E_Class_Wide_Type then
12286 return Etype (T) = T2;
12288 -- All other cases
12290 else
12291 loop
12292 Etyp := Etype (T);
12294 -- Done if we found the type we are looking for
12296 if Etyp = T2 then
12297 return True;
12299 -- Done if no more derivations to check
12301 elsif T = T1
12302 or else T = Etyp
12303 then
12304 return False;
12306 -- Following test catches error cases resulting from prev errors
12308 elsif No (Etyp) then
12309 return False;
12311 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
12312 return False;
12314 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
12315 return False;
12316 end if;
12318 T := Base_Type (Etyp);
12319 end loop;
12320 end if;
12321 end Is_Descendant_Of;
12323 ----------------------------------------
12324 -- Is_Descendant_Of_Suspension_Object --
12325 ----------------------------------------
12327 function Is_Descendant_Of_Suspension_Object
12328 (Typ : Entity_Id) return Boolean
12330 Cur_Typ : Entity_Id;
12331 Par_Typ : Entity_Id;
12333 begin
12334 -- Climb the type derivation chain checking each parent type against
12335 -- Suspension_Object.
12337 Cur_Typ := Base_Type (Typ);
12338 while Present (Cur_Typ) loop
12339 Par_Typ := Etype (Cur_Typ);
12341 -- The current type is a match
12343 if Is_Suspension_Object (Cur_Typ) then
12344 return True;
12346 -- Stop the traversal once the root of the derivation chain has been
12347 -- reached. In that case the current type is its own base type.
12349 elsif Cur_Typ = Par_Typ then
12350 exit;
12351 end if;
12353 Cur_Typ := Base_Type (Par_Typ);
12354 end loop;
12356 return False;
12357 end Is_Descendant_Of_Suspension_Object;
12359 ---------------------------------------------
12360 -- Is_Double_Precision_Floating_Point_Type --
12361 ---------------------------------------------
12363 function Is_Double_Precision_Floating_Point_Type
12364 (E : Entity_Id) return Boolean is
12365 begin
12366 return Is_Floating_Point_Type (E)
12367 and then Machine_Radix_Value (E) = Uint_2
12368 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
12369 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
12370 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
12371 end Is_Double_Precision_Floating_Point_Type;
12373 -----------------------------
12374 -- Is_Effectively_Volatile --
12375 -----------------------------
12377 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
12378 begin
12379 if Is_Type (Id) then
12381 -- An arbitrary type is effectively volatile when it is subject to
12382 -- pragma Atomic or Volatile.
12384 if Is_Volatile (Id) then
12385 return True;
12387 -- An array type is effectively volatile when it is subject to pragma
12388 -- Atomic_Components or Volatile_Components or its component type is
12389 -- effectively volatile.
12391 elsif Is_Array_Type (Id) then
12392 return
12393 Has_Volatile_Components (Id)
12394 or else
12395 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
12397 -- A protected type is always volatile
12399 elsif Is_Protected_Type (Id) then
12400 return True;
12402 -- A descendant of Ada.Synchronous_Task_Control.Suspension_Object is
12403 -- automatically volatile.
12405 elsif Is_Descendant_Of_Suspension_Object (Id) then
12406 return True;
12408 -- Otherwise the type is not effectively volatile
12410 else
12411 return False;
12412 end if;
12414 -- Otherwise Id denotes an object
12416 else
12417 return
12418 Is_Volatile (Id)
12419 or else Has_Volatile_Components (Id)
12420 or else Is_Effectively_Volatile (Etype (Id));
12421 end if;
12422 end Is_Effectively_Volatile;
12424 ------------------------------------
12425 -- Is_Effectively_Volatile_Object --
12426 ------------------------------------
12428 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
12429 begin
12430 if Is_Entity_Name (N) then
12431 return Is_Effectively_Volatile (Entity (N));
12433 elsif Nkind (N) = N_Indexed_Component then
12434 return Is_Effectively_Volatile_Object (Prefix (N));
12436 elsif Nkind (N) = N_Selected_Component then
12437 return
12438 Is_Effectively_Volatile_Object (Prefix (N))
12439 or else
12440 Is_Effectively_Volatile_Object (Selector_Name (N));
12442 else
12443 return False;
12444 end if;
12445 end Is_Effectively_Volatile_Object;
12447 -------------------
12448 -- Is_Entry_Body --
12449 -------------------
12451 function Is_Entry_Body (Id : Entity_Id) return Boolean is
12452 begin
12453 return
12454 Ekind_In (Id, E_Entry, E_Entry_Family)
12455 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Body;
12456 end Is_Entry_Body;
12458 --------------------------
12459 -- Is_Entry_Declaration --
12460 --------------------------
12462 function Is_Entry_Declaration (Id : Entity_Id) return Boolean is
12463 begin
12464 return
12465 Ekind_In (Id, E_Entry, E_Entry_Family)
12466 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Declaration;
12467 end Is_Entry_Declaration;
12469 ------------------------------------
12470 -- Is_Expanded_Priority_Attribute --
12471 ------------------------------------
12473 function Is_Expanded_Priority_Attribute (E : Entity_Id) return Boolean is
12474 begin
12475 return
12476 Nkind (E) = N_Function_Call
12477 and then not Configurable_Run_Time_Mode
12478 and then (Entity (Name (E)) = RTE (RE_Get_Ceiling)
12479 or else Entity (Name (E)) = RTE (RO_PE_Get_Ceiling));
12480 end Is_Expanded_Priority_Attribute;
12482 ----------------------------
12483 -- Is_Expression_Function --
12484 ----------------------------
12486 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
12487 begin
12488 if Ekind_In (Subp, E_Function, E_Subprogram_Body) then
12489 return
12490 Nkind (Original_Node (Unit_Declaration_Node (Subp))) =
12491 N_Expression_Function;
12492 else
12493 return False;
12494 end if;
12495 end Is_Expression_Function;
12497 ------------------------------------------
12498 -- Is_Expression_Function_Or_Completion --
12499 ------------------------------------------
12501 function Is_Expression_Function_Or_Completion
12502 (Subp : Entity_Id) return Boolean
12504 Subp_Decl : Node_Id;
12506 begin
12507 if Ekind (Subp) = E_Function then
12508 Subp_Decl := Unit_Declaration_Node (Subp);
12510 -- The function declaration is either an expression function or is
12511 -- completed by an expression function body.
12513 return
12514 Is_Expression_Function (Subp)
12515 or else (Nkind (Subp_Decl) = N_Subprogram_Declaration
12516 and then Present (Corresponding_Body (Subp_Decl))
12517 and then Is_Expression_Function
12518 (Corresponding_Body (Subp_Decl)));
12520 elsif Ekind (Subp) = E_Subprogram_Body then
12521 return Is_Expression_Function (Subp);
12523 else
12524 return False;
12525 end if;
12526 end Is_Expression_Function_Or_Completion;
12528 -----------------------
12529 -- Is_EVF_Expression --
12530 -----------------------
12532 function Is_EVF_Expression (N : Node_Id) return Boolean is
12533 Orig_N : constant Node_Id := Original_Node (N);
12534 Alt : Node_Id;
12535 Expr : Node_Id;
12536 Id : Entity_Id;
12538 begin
12539 -- Detect a reference to a formal parameter of a specific tagged type
12540 -- whose related subprogram is subject to pragma Expresions_Visible with
12541 -- value "False".
12543 if Is_Entity_Name (N) and then Present (Entity (N)) then
12544 Id := Entity (N);
12546 return
12547 Is_Formal (Id)
12548 and then Is_Specific_Tagged_Type (Etype (Id))
12549 and then Extensions_Visible_Status (Id) =
12550 Extensions_Visible_False;
12552 -- A case expression is an EVF expression when it contains at least one
12553 -- EVF dependent_expression. Note that a case expression may have been
12554 -- expanded, hence the use of Original_Node.
12556 elsif Nkind (Orig_N) = N_Case_Expression then
12557 Alt := First (Alternatives (Orig_N));
12558 while Present (Alt) loop
12559 if Is_EVF_Expression (Expression (Alt)) then
12560 return True;
12561 end if;
12563 Next (Alt);
12564 end loop;
12566 -- An if expression is an EVF expression when it contains at least one
12567 -- EVF dependent_expression. Note that an if expression may have been
12568 -- expanded, hence the use of Original_Node.
12570 elsif Nkind (Orig_N) = N_If_Expression then
12571 Expr := Next (First (Expressions (Orig_N)));
12572 while Present (Expr) loop
12573 if Is_EVF_Expression (Expr) then
12574 return True;
12575 end if;
12577 Next (Expr);
12578 end loop;
12580 -- A qualified expression or a type conversion is an EVF expression when
12581 -- its operand is an EVF expression.
12583 elsif Nkind_In (N, N_Qualified_Expression,
12584 N_Unchecked_Type_Conversion,
12585 N_Type_Conversion)
12586 then
12587 return Is_EVF_Expression (Expression (N));
12589 -- Attributes 'Loop_Entry, 'Old, and 'Update are EVF expressions when
12590 -- their prefix denotes an EVF expression.
12592 elsif Nkind (N) = N_Attribute_Reference
12593 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
12594 Name_Old,
12595 Name_Update)
12596 then
12597 return Is_EVF_Expression (Prefix (N));
12598 end if;
12600 return False;
12601 end Is_EVF_Expression;
12603 --------------
12604 -- Is_False --
12605 --------------
12607 function Is_False (U : Uint) return Boolean is
12608 begin
12609 return (U = 0);
12610 end Is_False;
12612 ---------------------------
12613 -- Is_Fixed_Model_Number --
12614 ---------------------------
12616 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
12617 S : constant Ureal := Small_Value (T);
12618 M : Urealp.Save_Mark;
12619 R : Boolean;
12620 begin
12621 M := Urealp.Mark;
12622 R := (U = UR_Trunc (U / S) * S);
12623 Urealp.Release (M);
12624 return R;
12625 end Is_Fixed_Model_Number;
12627 -------------------------------
12628 -- Is_Fully_Initialized_Type --
12629 -------------------------------
12631 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
12632 begin
12633 -- Scalar types
12635 if Is_Scalar_Type (Typ) then
12637 -- A scalar type with an aspect Default_Value is fully initialized
12639 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
12640 -- of a scalar type, but we don't take that into account here, since
12641 -- we don't want these to affect warnings.
12643 return Has_Default_Aspect (Typ);
12645 elsif Is_Access_Type (Typ) then
12646 return True;
12648 elsif Is_Array_Type (Typ) then
12649 if Is_Fully_Initialized_Type (Component_Type (Typ))
12650 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
12651 then
12652 return True;
12653 end if;
12655 -- An interesting case, if we have a constrained type one of whose
12656 -- bounds is known to be null, then there are no elements to be
12657 -- initialized, so all the elements are initialized.
12659 if Is_Constrained (Typ) then
12660 declare
12661 Indx : Node_Id;
12662 Indx_Typ : Entity_Id;
12663 Lbd, Hbd : Node_Id;
12665 begin
12666 Indx := First_Index (Typ);
12667 while Present (Indx) loop
12668 if Etype (Indx) = Any_Type then
12669 return False;
12671 -- If index is a range, use directly
12673 elsif Nkind (Indx) = N_Range then
12674 Lbd := Low_Bound (Indx);
12675 Hbd := High_Bound (Indx);
12677 else
12678 Indx_Typ := Etype (Indx);
12680 if Is_Private_Type (Indx_Typ) then
12681 Indx_Typ := Full_View (Indx_Typ);
12682 end if;
12684 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
12685 return False;
12686 else
12687 Lbd := Type_Low_Bound (Indx_Typ);
12688 Hbd := Type_High_Bound (Indx_Typ);
12689 end if;
12690 end if;
12692 if Compile_Time_Known_Value (Lbd)
12693 and then
12694 Compile_Time_Known_Value (Hbd)
12695 then
12696 if Expr_Value (Hbd) < Expr_Value (Lbd) then
12697 return True;
12698 end if;
12699 end if;
12701 Next_Index (Indx);
12702 end loop;
12703 end;
12704 end if;
12706 -- If no null indexes, then type is not fully initialized
12708 return False;
12710 -- Record types
12712 elsif Is_Record_Type (Typ) then
12713 if Has_Discriminants (Typ)
12714 and then
12715 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
12716 and then Is_Fully_Initialized_Variant (Typ)
12717 then
12718 return True;
12719 end if;
12721 -- We consider bounded string types to be fully initialized, because
12722 -- otherwise we get false alarms when the Data component is not
12723 -- default-initialized.
12725 if Is_Bounded_String (Typ) then
12726 return True;
12727 end if;
12729 -- Controlled records are considered to be fully initialized if
12730 -- there is a user defined Initialize routine. This may not be
12731 -- entirely correct, but as the spec notes, we are guessing here
12732 -- what is best from the point of view of issuing warnings.
12734 if Is_Controlled (Typ) then
12735 declare
12736 Utyp : constant Entity_Id := Underlying_Type (Typ);
12738 begin
12739 if Present (Utyp) then
12740 declare
12741 Init : constant Entity_Id :=
12742 (Find_Optional_Prim_Op
12743 (Underlying_Type (Typ), Name_Initialize));
12745 begin
12746 if Present (Init)
12747 and then Comes_From_Source (Init)
12748 and then not
12749 Is_Predefined_File_Name
12750 (File_Name (Get_Source_File_Index (Sloc (Init))))
12751 then
12752 return True;
12754 elsif Has_Null_Extension (Typ)
12755 and then
12756 Is_Fully_Initialized_Type
12757 (Etype (Base_Type (Typ)))
12758 then
12759 return True;
12760 end if;
12761 end;
12762 end if;
12763 end;
12764 end if;
12766 -- Otherwise see if all record components are initialized
12768 declare
12769 Ent : Entity_Id;
12771 begin
12772 Ent := First_Entity (Typ);
12773 while Present (Ent) loop
12774 if Ekind (Ent) = E_Component
12775 and then (No (Parent (Ent))
12776 or else No (Expression (Parent (Ent))))
12777 and then not Is_Fully_Initialized_Type (Etype (Ent))
12779 -- Special VM case for tag components, which need to be
12780 -- defined in this case, but are never initialized as VMs
12781 -- are using other dispatching mechanisms. Ignore this
12782 -- uninitialized case. Note that this applies both to the
12783 -- uTag entry and the main vtable pointer (CPP_Class case).
12785 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
12786 then
12787 return False;
12788 end if;
12790 Next_Entity (Ent);
12791 end loop;
12792 end;
12794 -- No uninitialized components, so type is fully initialized.
12795 -- Note that this catches the case of no components as well.
12797 return True;
12799 elsif Is_Concurrent_Type (Typ) then
12800 return True;
12802 elsif Is_Private_Type (Typ) then
12803 declare
12804 U : constant Entity_Id := Underlying_Type (Typ);
12806 begin
12807 if No (U) then
12808 return False;
12809 else
12810 return Is_Fully_Initialized_Type (U);
12811 end if;
12812 end;
12814 else
12815 return False;
12816 end if;
12817 end Is_Fully_Initialized_Type;
12819 ----------------------------------
12820 -- Is_Fully_Initialized_Variant --
12821 ----------------------------------
12823 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
12824 Loc : constant Source_Ptr := Sloc (Typ);
12825 Constraints : constant List_Id := New_List;
12826 Components : constant Elist_Id := New_Elmt_List;
12827 Comp_Elmt : Elmt_Id;
12828 Comp_Id : Node_Id;
12829 Comp_List : Node_Id;
12830 Discr : Entity_Id;
12831 Discr_Val : Node_Id;
12833 Report_Errors : Boolean;
12834 pragma Warnings (Off, Report_Errors);
12836 begin
12837 if Serious_Errors_Detected > 0 then
12838 return False;
12839 end if;
12841 if Is_Record_Type (Typ)
12842 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12843 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
12844 then
12845 Comp_List := Component_List (Type_Definition (Parent (Typ)));
12847 Discr := First_Discriminant (Typ);
12848 while Present (Discr) loop
12849 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
12850 Discr_Val := Expression (Parent (Discr));
12852 if Present (Discr_Val)
12853 and then Is_OK_Static_Expression (Discr_Val)
12854 then
12855 Append_To (Constraints,
12856 Make_Component_Association (Loc,
12857 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
12858 Expression => New_Copy (Discr_Val)));
12859 else
12860 return False;
12861 end if;
12862 else
12863 return False;
12864 end if;
12866 Next_Discriminant (Discr);
12867 end loop;
12869 Gather_Components
12870 (Typ => Typ,
12871 Comp_List => Comp_List,
12872 Governed_By => Constraints,
12873 Into => Components,
12874 Report_Errors => Report_Errors);
12876 -- Check that each component present is fully initialized
12878 Comp_Elmt := First_Elmt (Components);
12879 while Present (Comp_Elmt) loop
12880 Comp_Id := Node (Comp_Elmt);
12882 if Ekind (Comp_Id) = E_Component
12883 and then (No (Parent (Comp_Id))
12884 or else No (Expression (Parent (Comp_Id))))
12885 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
12886 then
12887 return False;
12888 end if;
12890 Next_Elmt (Comp_Elmt);
12891 end loop;
12893 return True;
12895 elsif Is_Private_Type (Typ) then
12896 declare
12897 U : constant Entity_Id := Underlying_Type (Typ);
12899 begin
12900 if No (U) then
12901 return False;
12902 else
12903 return Is_Fully_Initialized_Variant (U);
12904 end if;
12905 end;
12907 else
12908 return False;
12909 end if;
12910 end Is_Fully_Initialized_Variant;
12912 ------------------------------------
12913 -- Is_Generic_Declaration_Or_Body --
12914 ------------------------------------
12916 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
12917 Spec_Decl : Node_Id;
12919 begin
12920 -- Package/subprogram body
12922 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
12923 and then Present (Corresponding_Spec (Decl))
12924 then
12925 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
12927 -- Package/subprogram body stub
12929 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
12930 and then Present (Corresponding_Spec_Of_Stub (Decl))
12931 then
12932 Spec_Decl :=
12933 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
12935 -- All other cases
12937 else
12938 Spec_Decl := Decl;
12939 end if;
12941 -- Rather than inspecting the defining entity of the spec declaration,
12942 -- look at its Nkind. This takes care of the case where the analysis of
12943 -- a generic body modifies the Ekind of its spec to allow for recursive
12944 -- calls.
12946 return
12947 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
12948 N_Generic_Subprogram_Declaration);
12949 end Is_Generic_Declaration_Or_Body;
12951 ----------------------------
12952 -- Is_Inherited_Operation --
12953 ----------------------------
12955 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
12956 pragma Assert (Is_Overloadable (E));
12957 Kind : constant Node_Kind := Nkind (Parent (E));
12958 begin
12959 return Kind = N_Full_Type_Declaration
12960 or else Kind = N_Private_Extension_Declaration
12961 or else Kind = N_Subtype_Declaration
12962 or else (Ekind (E) = E_Enumeration_Literal
12963 and then Is_Derived_Type (Etype (E)));
12964 end Is_Inherited_Operation;
12966 -------------------------------------
12967 -- Is_Inherited_Operation_For_Type --
12968 -------------------------------------
12970 function Is_Inherited_Operation_For_Type
12971 (E : Entity_Id;
12972 Typ : Entity_Id) return Boolean
12974 begin
12975 -- Check that the operation has been created by the type declaration
12977 return Is_Inherited_Operation (E)
12978 and then Defining_Identifier (Parent (E)) = Typ;
12979 end Is_Inherited_Operation_For_Type;
12981 -----------------
12982 -- Is_Iterator --
12983 -----------------
12985 function Is_Iterator (Typ : Entity_Id) return Boolean is
12986 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean;
12987 -- Determine whether type Iter_Typ is a predefined forward or reversible
12988 -- iterator.
12990 ----------------------
12991 -- Denotes_Iterator --
12992 ----------------------
12994 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean is
12995 begin
12996 -- Check that the name matches, and that the ultimate ancestor is in
12997 -- a predefined unit, i.e the one that declares iterator interfaces.
12999 return
13000 Nam_In (Chars (Iter_Typ), Name_Forward_Iterator,
13001 Name_Reversible_Iterator)
13002 and then Is_Predefined_File_Name
13003 (Unit_File_Name (Get_Source_Unit (Root_Type (Iter_Typ))));
13004 end Denotes_Iterator;
13006 -- Local variables
13008 Iface_Elmt : Elmt_Id;
13009 Ifaces : Elist_Id;
13011 -- Start of processing for Is_Iterator
13013 begin
13014 -- The type may be a subtype of a descendant of the proper instance of
13015 -- the predefined interface type, so we must use the root type of the
13016 -- given type. The same is done for Is_Reversible_Iterator.
13018 if Is_Class_Wide_Type (Typ)
13019 and then Denotes_Iterator (Root_Type (Typ))
13020 then
13021 return True;
13023 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
13024 return False;
13026 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
13027 return True;
13029 else
13030 Collect_Interfaces (Typ, Ifaces);
13032 Iface_Elmt := First_Elmt (Ifaces);
13033 while Present (Iface_Elmt) loop
13034 if Denotes_Iterator (Node (Iface_Elmt)) then
13035 return True;
13036 end if;
13038 Next_Elmt (Iface_Elmt);
13039 end loop;
13041 return False;
13042 end if;
13043 end Is_Iterator;
13045 ----------------------------
13046 -- Is_Iterator_Over_Array --
13047 ----------------------------
13049 function Is_Iterator_Over_Array (N : Node_Id) return Boolean is
13050 Container : constant Node_Id := Name (N);
13051 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
13052 begin
13053 return Is_Array_Type (Container_Typ);
13054 end Is_Iterator_Over_Array;
13056 ------------
13057 -- Is_LHS --
13058 ------------
13060 -- We seem to have a lot of overlapping functions that do similar things
13061 -- (testing for left hand sides or lvalues???).
13063 function Is_LHS (N : Node_Id) return Is_LHS_Result is
13064 P : constant Node_Id := Parent (N);
13066 begin
13067 -- Return True if we are the left hand side of an assignment statement
13069 if Nkind (P) = N_Assignment_Statement then
13070 if Name (P) = N then
13071 return Yes;
13072 else
13073 return No;
13074 end if;
13076 -- Case of prefix of indexed or selected component or slice
13078 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
13079 and then N = Prefix (P)
13080 then
13081 -- Here we have the case where the parent P is N.Q or N(Q .. R).
13082 -- If P is an LHS, then N is also effectively an LHS, but there
13083 -- is an important exception. If N is of an access type, then
13084 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
13085 -- case this makes N.all a left hand side but not N itself.
13087 -- If we don't know the type yet, this is the case where we return
13088 -- Unknown, since the answer depends on the type which is unknown.
13090 if No (Etype (N)) then
13091 return Unknown;
13093 -- We have an Etype set, so we can check it
13095 elsif Is_Access_Type (Etype (N)) then
13096 return No;
13098 -- OK, not access type case, so just test whole expression
13100 else
13101 return Is_LHS (P);
13102 end if;
13104 -- All other cases are not left hand sides
13106 else
13107 return No;
13108 end if;
13109 end Is_LHS;
13111 -----------------------------
13112 -- Is_Library_Level_Entity --
13113 -----------------------------
13115 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
13116 begin
13117 -- The following is a small optimization, and it also properly handles
13118 -- discriminals, which in task bodies might appear in expressions before
13119 -- the corresponding procedure has been created, and which therefore do
13120 -- not have an assigned scope.
13122 if Is_Formal (E) then
13123 return False;
13124 end if;
13126 -- Normal test is simply that the enclosing dynamic scope is Standard
13128 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
13129 end Is_Library_Level_Entity;
13131 --------------------------------
13132 -- Is_Limited_Class_Wide_Type --
13133 --------------------------------
13135 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
13136 begin
13137 return
13138 Is_Class_Wide_Type (Typ)
13139 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
13140 end Is_Limited_Class_Wide_Type;
13142 ---------------------------------
13143 -- Is_Local_Variable_Reference --
13144 ---------------------------------
13146 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
13147 begin
13148 if not Is_Entity_Name (Expr) then
13149 return False;
13151 else
13152 declare
13153 Ent : constant Entity_Id := Entity (Expr);
13154 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
13155 begin
13156 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
13157 return False;
13158 else
13159 return Present (Sub) and then Sub = Current_Subprogram;
13160 end if;
13161 end;
13162 end if;
13163 end Is_Local_Variable_Reference;
13165 ---------------------------------
13166 -- Is_Nontrivial_DIC_Procedure --
13167 ---------------------------------
13169 function Is_Nontrivial_DIC_Procedure (Id : Entity_Id) return Boolean is
13170 Body_Decl : Node_Id;
13171 Stmt : Node_Id;
13173 begin
13174 if Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id) then
13175 Body_Decl :=
13176 Unit_Declaration_Node
13177 (Corresponding_Body (Unit_Declaration_Node (Id)));
13179 -- The body of the Default_Initial_Condition procedure must contain
13180 -- at least one statement, otherwise the generation of the subprogram
13181 -- body failed.
13183 pragma Assert (Present (Handled_Statement_Sequence (Body_Decl)));
13185 -- To qualify as nontrivial, the first statement of the procedure
13186 -- must be a check in the form of an if statement. If the original
13187 -- Default_Initial_Condition expression was folded, then the first
13188 -- statement is not a check.
13190 Stmt := First (Statements (Handled_Statement_Sequence (Body_Decl)));
13192 return
13193 Nkind (Stmt) = N_If_Statement
13194 and then Nkind (Original_Node (Stmt)) = N_Pragma;
13195 end if;
13197 return False;
13198 end Is_Nontrivial_DIC_Procedure;
13200 -------------------------
13201 -- Is_Null_Record_Type --
13202 -------------------------
13204 function Is_Null_Record_Type (T : Entity_Id) return Boolean is
13205 Decl : constant Node_Id := Parent (T);
13206 begin
13207 return Nkind (Decl) = N_Full_Type_Declaration
13208 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
13209 and then
13210 (No (Component_List (Type_Definition (Decl)))
13211 or else Null_Present (Component_List (Type_Definition (Decl))));
13212 end Is_Null_Record_Type;
13214 -------------------------
13215 -- Is_Object_Reference --
13216 -------------------------
13218 function Is_Object_Reference (N : Node_Id) return Boolean is
13219 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
13220 -- Determine whether N is the name of an internally-generated renaming
13222 --------------------------------------
13223 -- Is_Internally_Generated_Renaming --
13224 --------------------------------------
13226 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
13227 P : Node_Id;
13229 begin
13230 P := N;
13231 while Present (P) loop
13232 if Nkind (P) = N_Object_Renaming_Declaration then
13233 return not Comes_From_Source (P);
13234 elsif Is_List_Member (P) then
13235 return False;
13236 end if;
13238 P := Parent (P);
13239 end loop;
13241 return False;
13242 end Is_Internally_Generated_Renaming;
13244 -- Start of processing for Is_Object_Reference
13246 begin
13247 if Is_Entity_Name (N) then
13248 return Present (Entity (N)) and then Is_Object (Entity (N));
13250 else
13251 case Nkind (N) is
13252 when N_Indexed_Component | N_Slice =>
13253 return
13254 Is_Object_Reference (Prefix (N))
13255 or else Is_Access_Type (Etype (Prefix (N)));
13257 -- In Ada 95, a function call is a constant object; a procedure
13258 -- call is not.
13260 when N_Function_Call =>
13261 return Etype (N) /= Standard_Void_Type;
13263 -- Attributes 'Input, 'Loop_Entry, 'Old, and 'Result produce
13264 -- objects.
13266 when N_Attribute_Reference =>
13267 return
13268 Nam_In (Attribute_Name (N), Name_Input,
13269 Name_Loop_Entry,
13270 Name_Old,
13271 Name_Result);
13273 when N_Selected_Component =>
13274 return
13275 Is_Object_Reference (Selector_Name (N))
13276 and then
13277 (Is_Object_Reference (Prefix (N))
13278 or else Is_Access_Type (Etype (Prefix (N))));
13280 when N_Explicit_Dereference =>
13281 return True;
13283 -- A view conversion of a tagged object is an object reference
13285 when N_Type_Conversion =>
13286 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
13287 and then Is_Tagged_Type (Etype (Expression (N)))
13288 and then Is_Object_Reference (Expression (N));
13290 -- An unchecked type conversion is considered to be an object if
13291 -- the operand is an object (this construction arises only as a
13292 -- result of expansion activities).
13294 when N_Unchecked_Type_Conversion =>
13295 return True;
13297 -- Allow string literals to act as objects as long as they appear
13298 -- in internally-generated renamings. The expansion of iterators
13299 -- may generate such renamings when the range involves a string
13300 -- literal.
13302 when N_String_Literal =>
13303 return Is_Internally_Generated_Renaming (Parent (N));
13305 -- AI05-0003: In Ada 2012 a qualified expression is a name.
13306 -- This allows disambiguation of function calls and the use
13307 -- of aggregates in more contexts.
13309 when N_Qualified_Expression =>
13310 if Ada_Version < Ada_2012 then
13311 return False;
13312 else
13313 return Is_Object_Reference (Expression (N))
13314 or else Nkind (Expression (N)) = N_Aggregate;
13315 end if;
13317 when others =>
13318 return False;
13319 end case;
13320 end if;
13321 end Is_Object_Reference;
13323 -----------------------------------
13324 -- Is_OK_Variable_For_Out_Formal --
13325 -----------------------------------
13327 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
13328 begin
13329 Note_Possible_Modification (AV, Sure => True);
13331 -- We must reject parenthesized variable names. Comes_From_Source is
13332 -- checked because there are currently cases where the compiler violates
13333 -- this rule (e.g. passing a task object to its controlled Initialize
13334 -- routine). This should be properly documented in sinfo???
13336 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
13337 return False;
13339 -- A variable is always allowed
13341 elsif Is_Variable (AV) then
13342 return True;
13344 -- Generalized indexing operations are rewritten as explicit
13345 -- dereferences, and it is only during resolution that we can
13346 -- check whether the context requires an access_to_variable type.
13348 elsif Nkind (AV) = N_Explicit_Dereference
13349 and then Ada_Version >= Ada_2012
13350 and then Nkind (Original_Node (AV)) = N_Indexed_Component
13351 and then Present (Etype (Original_Node (AV)))
13352 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
13353 then
13354 return not Is_Access_Constant (Etype (Prefix (AV)));
13356 -- Unchecked conversions are allowed only if they come from the
13357 -- generated code, which sometimes uses unchecked conversions for out
13358 -- parameters in cases where code generation is unaffected. We tell
13359 -- source unchecked conversions by seeing if they are rewrites of
13360 -- an original Unchecked_Conversion function call, or of an explicit
13361 -- conversion of a function call or an aggregate (as may happen in the
13362 -- expansion of a packed array aggregate).
13364 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
13365 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
13366 return False;
13368 elsif Comes_From_Source (AV)
13369 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
13370 then
13371 return False;
13373 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
13374 return Is_OK_Variable_For_Out_Formal (Expression (AV));
13376 else
13377 return True;
13378 end if;
13380 -- Normal type conversions are allowed if argument is a variable
13382 elsif Nkind (AV) = N_Type_Conversion then
13383 if Is_Variable (Expression (AV))
13384 and then Paren_Count (Expression (AV)) = 0
13385 then
13386 Note_Possible_Modification (Expression (AV), Sure => True);
13387 return True;
13389 -- We also allow a non-parenthesized expression that raises
13390 -- constraint error if it rewrites what used to be a variable
13392 elsif Raises_Constraint_Error (Expression (AV))
13393 and then Paren_Count (Expression (AV)) = 0
13394 and then Is_Variable (Original_Node (Expression (AV)))
13395 then
13396 return True;
13398 -- Type conversion of something other than a variable
13400 else
13401 return False;
13402 end if;
13404 -- If this node is rewritten, then test the original form, if that is
13405 -- OK, then we consider the rewritten node OK (for example, if the
13406 -- original node is a conversion, then Is_Variable will not be true
13407 -- but we still want to allow the conversion if it converts a variable).
13409 elsif Original_Node (AV) /= AV then
13411 -- In Ada 2012, the explicit dereference may be a rewritten call to a
13412 -- Reference function.
13414 if Ada_Version >= Ada_2012
13415 and then Nkind (Original_Node (AV)) = N_Function_Call
13416 and then
13417 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
13418 then
13420 -- Check that this is not a constant reference.
13422 return not Is_Access_Constant (Etype (Prefix (AV)));
13424 elsif Has_Implicit_Dereference (Etype (Original_Node (AV))) then
13425 return
13426 not Is_Access_Constant (Etype
13427 (Get_Reference_Discriminant (Etype (Original_Node (AV)))));
13429 else
13430 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
13431 end if;
13433 -- All other non-variables are rejected
13435 else
13436 return False;
13437 end if;
13438 end Is_OK_Variable_For_Out_Formal;
13440 ----------------------------
13441 -- Is_OK_Volatile_Context --
13442 ----------------------------
13444 function Is_OK_Volatile_Context
13445 (Context : Node_Id;
13446 Obj_Ref : Node_Id) return Boolean
13448 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
13449 -- Determine whether an arbitrary node denotes a call to a protected
13450 -- entry, function, or procedure in prefixed form where the prefix is
13451 -- Obj_Ref.
13453 function Within_Check (Nod : Node_Id) return Boolean;
13454 -- Determine whether an arbitrary node appears in a check node
13456 function Within_Subprogram_Call (Nod : Node_Id) return Boolean;
13457 -- Determine whether an arbitrary node appears in an entry, function, or
13458 -- procedure call.
13460 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
13461 -- Determine whether an arbitrary entity appears in a volatile function
13463 ---------------------------------
13464 -- Is_Protected_Operation_Call --
13465 ---------------------------------
13467 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
13468 Pref : Node_Id;
13469 Subp : Node_Id;
13471 begin
13472 -- A call to a protected operations retains its selected component
13473 -- form as opposed to other prefixed calls that are transformed in
13474 -- expanded names.
13476 if Nkind (Nod) = N_Selected_Component then
13477 Pref := Prefix (Nod);
13478 Subp := Selector_Name (Nod);
13480 return
13481 Pref = Obj_Ref
13482 and then Present (Etype (Pref))
13483 and then Is_Protected_Type (Etype (Pref))
13484 and then Is_Entity_Name (Subp)
13485 and then Present (Entity (Subp))
13486 and then Ekind_In (Entity (Subp), E_Entry,
13487 E_Entry_Family,
13488 E_Function,
13489 E_Procedure);
13490 else
13491 return False;
13492 end if;
13493 end Is_Protected_Operation_Call;
13495 ------------------
13496 -- Within_Check --
13497 ------------------
13499 function Within_Check (Nod : Node_Id) return Boolean is
13500 Par : Node_Id;
13502 begin
13503 -- Climb the parent chain looking for a check node
13505 Par := Nod;
13506 while Present (Par) loop
13507 if Nkind (Par) in N_Raise_xxx_Error then
13508 return True;
13510 -- Prevent the search from going too far
13512 elsif Is_Body_Or_Package_Declaration (Par) then
13513 exit;
13514 end if;
13516 Par := Parent (Par);
13517 end loop;
13519 return False;
13520 end Within_Check;
13522 ----------------------------
13523 -- Within_Subprogram_Call --
13524 ----------------------------
13526 function Within_Subprogram_Call (Nod : Node_Id) return Boolean is
13527 Par : Node_Id;
13529 begin
13530 -- Climb the parent chain looking for a function or procedure call
13532 Par := Nod;
13533 while Present (Par) loop
13534 if Nkind_In (Par, N_Entry_Call_Statement,
13535 N_Function_Call,
13536 N_Procedure_Call_Statement)
13537 then
13538 return True;
13540 -- Prevent the search from going too far
13542 elsif Is_Body_Or_Package_Declaration (Par) then
13543 exit;
13544 end if;
13546 Par := Parent (Par);
13547 end loop;
13549 return False;
13550 end Within_Subprogram_Call;
13552 ------------------------------
13553 -- Within_Volatile_Function --
13554 ------------------------------
13556 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
13557 Func_Id : Entity_Id;
13559 begin
13560 -- Traverse the scope stack looking for a [generic] function
13562 Func_Id := Id;
13563 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
13564 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
13565 return Is_Volatile_Function (Func_Id);
13566 end if;
13568 Func_Id := Scope (Func_Id);
13569 end loop;
13571 return False;
13572 end Within_Volatile_Function;
13574 -- Local variables
13576 Obj_Id : Entity_Id;
13578 -- Start of processing for Is_OK_Volatile_Context
13580 begin
13581 -- The volatile object appears on either side of an assignment
13583 if Nkind (Context) = N_Assignment_Statement then
13584 return True;
13586 -- The volatile object is part of the initialization expression of
13587 -- another object.
13589 elsif Nkind (Context) = N_Object_Declaration
13590 and then Present (Expression (Context))
13591 and then Expression (Context) = Obj_Ref
13592 then
13593 Obj_Id := Defining_Entity (Context);
13595 -- The volatile object acts as the initialization expression of an
13596 -- extended return statement. This is valid context as long as the
13597 -- function is volatile.
13599 if Is_Return_Object (Obj_Id) then
13600 return Within_Volatile_Function (Obj_Id);
13602 -- Otherwise this is a normal object initialization
13604 else
13605 return True;
13606 end if;
13608 -- The volatile object acts as the name of a renaming declaration
13610 elsif Nkind (Context) = N_Object_Renaming_Declaration
13611 and then Name (Context) = Obj_Ref
13612 then
13613 return True;
13615 -- The volatile object appears as an actual parameter in a call to an
13616 -- instance of Unchecked_Conversion whose result is renamed.
13618 elsif Nkind (Context) = N_Function_Call
13619 and then Is_Entity_Name (Name (Context))
13620 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
13621 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
13622 then
13623 return True;
13625 -- The volatile object is actually the prefix in a protected entry,
13626 -- function, or procedure call.
13628 elsif Is_Protected_Operation_Call (Context) then
13629 return True;
13631 -- The volatile object appears as the expression of a simple return
13632 -- statement that applies to a volatile function.
13634 elsif Nkind (Context) = N_Simple_Return_Statement
13635 and then Expression (Context) = Obj_Ref
13636 then
13637 return
13638 Within_Volatile_Function (Return_Statement_Entity (Context));
13640 -- The volatile object appears as the prefix of a name occurring in a
13641 -- non-interfering context.
13643 elsif Nkind_In (Context, N_Attribute_Reference,
13644 N_Explicit_Dereference,
13645 N_Indexed_Component,
13646 N_Selected_Component,
13647 N_Slice)
13648 and then Prefix (Context) = Obj_Ref
13649 and then Is_OK_Volatile_Context
13650 (Context => Parent (Context),
13651 Obj_Ref => Context)
13652 then
13653 return True;
13655 -- The volatile object appears as the prefix of attributes Address,
13656 -- Alignment, Component_Size, First_Bit, Last_Bit, Position, Size,
13657 -- Storage_Size.
13659 elsif Nkind (Context) = N_Attribute_Reference
13660 and then Prefix (Context) = Obj_Ref
13661 and then Nam_In (Attribute_Name (Context), Name_Address,
13662 Name_Alignment,
13663 Name_Component_Size,
13664 Name_First_Bit,
13665 Name_Last_Bit,
13666 Name_Position,
13667 Name_Size,
13668 Name_Storage_Size)
13669 then
13670 return True;
13672 -- The volatile object appears as the expression of a type conversion
13673 -- occurring in a non-interfering context.
13675 elsif Nkind_In (Context, N_Type_Conversion,
13676 N_Unchecked_Type_Conversion)
13677 and then Expression (Context) = Obj_Ref
13678 and then Is_OK_Volatile_Context
13679 (Context => Parent (Context),
13680 Obj_Ref => Context)
13681 then
13682 return True;
13684 -- Allow references to volatile objects in various checks. This is not a
13685 -- direct SPARK 2014 requirement.
13687 elsif Within_Check (Context) then
13688 return True;
13690 -- Assume that references to effectively volatile objects that appear
13691 -- as actual parameters in a subprogram call are always legal. A full
13692 -- legality check is done when the actuals are resolved (see routine
13693 -- Resolve_Actuals).
13695 elsif Within_Subprogram_Call (Context) then
13696 return True;
13698 -- Otherwise the context is not suitable for an effectively volatile
13699 -- object.
13701 else
13702 return False;
13703 end if;
13704 end Is_OK_Volatile_Context;
13706 ------------------------------------
13707 -- Is_Package_Contract_Annotation --
13708 ------------------------------------
13710 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
13711 Nam : Name_Id;
13713 begin
13714 if Nkind (Item) = N_Aspect_Specification then
13715 Nam := Chars (Identifier (Item));
13717 else pragma Assert (Nkind (Item) = N_Pragma);
13718 Nam := Pragma_Name_Mapped (Item);
13719 end if;
13721 return Nam = Name_Abstract_State
13722 or else Nam = Name_Initial_Condition
13723 or else Nam = Name_Initializes
13724 or else Nam = Name_Refined_State;
13725 end Is_Package_Contract_Annotation;
13727 -----------------------------------
13728 -- Is_Partially_Initialized_Type --
13729 -----------------------------------
13731 function Is_Partially_Initialized_Type
13732 (Typ : Entity_Id;
13733 Include_Implicit : Boolean := True) return Boolean
13735 begin
13736 if Is_Scalar_Type (Typ) then
13737 return False;
13739 elsif Is_Access_Type (Typ) then
13740 return Include_Implicit;
13742 elsif Is_Array_Type (Typ) then
13744 -- If component type is partially initialized, so is array type
13746 if Is_Partially_Initialized_Type
13747 (Component_Type (Typ), Include_Implicit)
13748 then
13749 return True;
13751 -- Otherwise we are only partially initialized if we are fully
13752 -- initialized (this is the empty array case, no point in us
13753 -- duplicating that code here).
13755 else
13756 return Is_Fully_Initialized_Type (Typ);
13757 end if;
13759 elsif Is_Record_Type (Typ) then
13761 -- A discriminated type is always partially initialized if in
13762 -- all mode
13764 if Has_Discriminants (Typ) and then Include_Implicit then
13765 return True;
13767 -- A tagged type is always partially initialized
13769 elsif Is_Tagged_Type (Typ) then
13770 return True;
13772 -- Case of non-discriminated record
13774 else
13775 declare
13776 Ent : Entity_Id;
13778 Component_Present : Boolean := False;
13779 -- Set True if at least one component is present. If no
13780 -- components are present, then record type is fully
13781 -- initialized (another odd case, like the null array).
13783 begin
13784 -- Loop through components
13786 Ent := First_Entity (Typ);
13787 while Present (Ent) loop
13788 if Ekind (Ent) = E_Component then
13789 Component_Present := True;
13791 -- If a component has an initialization expression then
13792 -- the enclosing record type is partially initialized
13794 if Present (Parent (Ent))
13795 and then Present (Expression (Parent (Ent)))
13796 then
13797 return True;
13799 -- If a component is of a type which is itself partially
13800 -- initialized, then the enclosing record type is also.
13802 elsif Is_Partially_Initialized_Type
13803 (Etype (Ent), Include_Implicit)
13804 then
13805 return True;
13806 end if;
13807 end if;
13809 Next_Entity (Ent);
13810 end loop;
13812 -- No initialized components found. If we found any components
13813 -- they were all uninitialized so the result is false.
13815 if Component_Present then
13816 return False;
13818 -- But if we found no components, then all the components are
13819 -- initialized so we consider the type to be initialized.
13821 else
13822 return True;
13823 end if;
13824 end;
13825 end if;
13827 -- Concurrent types are always fully initialized
13829 elsif Is_Concurrent_Type (Typ) then
13830 return True;
13832 -- For a private type, go to underlying type. If there is no underlying
13833 -- type then just assume this partially initialized. Not clear if this
13834 -- can happen in a non-error case, but no harm in testing for this.
13836 elsif Is_Private_Type (Typ) then
13837 declare
13838 U : constant Entity_Id := Underlying_Type (Typ);
13839 begin
13840 if No (U) then
13841 return True;
13842 else
13843 return Is_Partially_Initialized_Type (U, Include_Implicit);
13844 end if;
13845 end;
13847 -- For any other type (are there any?) assume partially initialized
13849 else
13850 return True;
13851 end if;
13852 end Is_Partially_Initialized_Type;
13854 ------------------------------------
13855 -- Is_Potentially_Persistent_Type --
13856 ------------------------------------
13858 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
13859 Comp : Entity_Id;
13860 Indx : Node_Id;
13862 begin
13863 -- For private type, test corresponding full type
13865 if Is_Private_Type (T) then
13866 return Is_Potentially_Persistent_Type (Full_View (T));
13868 -- Scalar types are potentially persistent
13870 elsif Is_Scalar_Type (T) then
13871 return True;
13873 -- Record type is potentially persistent if not tagged and the types of
13874 -- all it components are potentially persistent, and no component has
13875 -- an initialization expression.
13877 elsif Is_Record_Type (T)
13878 and then not Is_Tagged_Type (T)
13879 and then not Is_Partially_Initialized_Type (T)
13880 then
13881 Comp := First_Component (T);
13882 while Present (Comp) loop
13883 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
13884 return False;
13885 else
13886 Next_Entity (Comp);
13887 end if;
13888 end loop;
13890 return True;
13892 -- Array type is potentially persistent if its component type is
13893 -- potentially persistent and if all its constraints are static.
13895 elsif Is_Array_Type (T) then
13896 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
13897 return False;
13898 end if;
13900 Indx := First_Index (T);
13901 while Present (Indx) loop
13902 if not Is_OK_Static_Subtype (Etype (Indx)) then
13903 return False;
13904 else
13905 Next_Index (Indx);
13906 end if;
13907 end loop;
13909 return True;
13911 -- All other types are not potentially persistent
13913 else
13914 return False;
13915 end if;
13916 end Is_Potentially_Persistent_Type;
13918 --------------------------------
13919 -- Is_Potentially_Unevaluated --
13920 --------------------------------
13922 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
13923 Par : Node_Id;
13924 Expr : Node_Id;
13926 begin
13927 Expr := N;
13928 Par := Parent (N);
13930 -- A postcondition whose expression is a short-circuit is broken down
13931 -- into individual aspects for better exception reporting. The original
13932 -- short-circuit expression is rewritten as the second operand, and an
13933 -- occurrence of 'Old in that operand is potentially unevaluated.
13934 -- See Sem_ch13.adb for details of this transformation.
13936 if Nkind (Original_Node (Par)) = N_And_Then then
13937 return True;
13938 end if;
13940 while not Nkind_In (Par, N_If_Expression,
13941 N_Case_Expression,
13942 N_And_Then,
13943 N_Or_Else,
13944 N_In,
13945 N_Not_In)
13946 loop
13947 Expr := Par;
13948 Par := Parent (Par);
13950 -- If the context is not an expression, or if is the result of
13951 -- expansion of an enclosing construct (such as another attribute)
13952 -- the predicate does not apply.
13954 if Nkind (Par) not in N_Subexpr
13955 or else not Comes_From_Source (Par)
13956 then
13957 return False;
13958 end if;
13959 end loop;
13961 if Nkind (Par) = N_If_Expression then
13962 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
13964 elsif Nkind (Par) = N_Case_Expression then
13965 return Expr /= Expression (Par);
13967 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
13968 return Expr = Right_Opnd (Par);
13970 elsif Nkind_In (Par, N_In, N_Not_In) then
13971 return Expr /= Left_Opnd (Par);
13973 else
13974 return False;
13975 end if;
13976 end Is_Potentially_Unevaluated;
13978 ---------------------------------
13979 -- Is_Protected_Self_Reference --
13980 ---------------------------------
13982 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
13984 function In_Access_Definition (N : Node_Id) return Boolean;
13985 -- Returns true if N belongs to an access definition
13987 --------------------------
13988 -- In_Access_Definition --
13989 --------------------------
13991 function In_Access_Definition (N : Node_Id) return Boolean is
13992 P : Node_Id;
13994 begin
13995 P := Parent (N);
13996 while Present (P) loop
13997 if Nkind (P) = N_Access_Definition then
13998 return True;
13999 end if;
14001 P := Parent (P);
14002 end loop;
14004 return False;
14005 end In_Access_Definition;
14007 -- Start of processing for Is_Protected_Self_Reference
14009 begin
14010 -- Verify that prefix is analyzed and has the proper form. Note that
14011 -- the attributes Elab_Spec, Elab_Body, and Elab_Subp_Body, which also
14012 -- produce the address of an entity, do not analyze their prefix
14013 -- because they denote entities that are not necessarily visible.
14014 -- Neither of them can apply to a protected type.
14016 return Ada_Version >= Ada_2005
14017 and then Is_Entity_Name (N)
14018 and then Present (Entity (N))
14019 and then Is_Protected_Type (Entity (N))
14020 and then In_Open_Scopes (Entity (N))
14021 and then not In_Access_Definition (N);
14022 end Is_Protected_Self_Reference;
14024 -----------------------------
14025 -- Is_RCI_Pkg_Spec_Or_Body --
14026 -----------------------------
14028 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
14030 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
14031 -- Return True if the unit of Cunit is an RCI package declaration
14033 ---------------------------
14034 -- Is_RCI_Pkg_Decl_Cunit --
14035 ---------------------------
14037 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
14038 The_Unit : constant Node_Id := Unit (Cunit);
14040 begin
14041 if Nkind (The_Unit) /= N_Package_Declaration then
14042 return False;
14043 end if;
14045 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
14046 end Is_RCI_Pkg_Decl_Cunit;
14048 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
14050 begin
14051 return Is_RCI_Pkg_Decl_Cunit (Cunit)
14052 or else
14053 (Nkind (Unit (Cunit)) = N_Package_Body
14054 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
14055 end Is_RCI_Pkg_Spec_Or_Body;
14057 -----------------------------------------
14058 -- Is_Remote_Access_To_Class_Wide_Type --
14059 -----------------------------------------
14061 function Is_Remote_Access_To_Class_Wide_Type
14062 (E : Entity_Id) return Boolean
14064 begin
14065 -- A remote access to class-wide type is a general access to object type
14066 -- declared in the visible part of a Remote_Types or Remote_Call_
14067 -- Interface unit.
14069 return Ekind (E) = E_General_Access_Type
14070 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
14071 end Is_Remote_Access_To_Class_Wide_Type;
14073 -----------------------------------------
14074 -- Is_Remote_Access_To_Subprogram_Type --
14075 -----------------------------------------
14077 function Is_Remote_Access_To_Subprogram_Type
14078 (E : Entity_Id) return Boolean
14080 begin
14081 return (Ekind (E) = E_Access_Subprogram_Type
14082 or else (Ekind (E) = E_Record_Type
14083 and then Present (Corresponding_Remote_Type (E))))
14084 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
14085 end Is_Remote_Access_To_Subprogram_Type;
14087 --------------------
14088 -- Is_Remote_Call --
14089 --------------------
14091 function Is_Remote_Call (N : Node_Id) return Boolean is
14092 begin
14093 if Nkind (N) not in N_Subprogram_Call then
14095 -- An entry call cannot be remote
14097 return False;
14099 elsif Nkind (Name (N)) in N_Has_Entity
14100 and then Is_Remote_Call_Interface (Entity (Name (N)))
14101 then
14102 -- A subprogram declared in the spec of a RCI package is remote
14104 return True;
14106 elsif Nkind (Name (N)) = N_Explicit_Dereference
14107 and then Is_Remote_Access_To_Subprogram_Type
14108 (Etype (Prefix (Name (N))))
14109 then
14110 -- The dereference of a RAS is a remote call
14112 return True;
14114 elsif Present (Controlling_Argument (N))
14115 and then Is_Remote_Access_To_Class_Wide_Type
14116 (Etype (Controlling_Argument (N)))
14117 then
14118 -- Any primitive operation call with a controlling argument of
14119 -- a RACW type is a remote call.
14121 return True;
14122 end if;
14124 -- All other calls are local calls
14126 return False;
14127 end Is_Remote_Call;
14129 ----------------------
14130 -- Is_Renamed_Entry --
14131 ----------------------
14133 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
14134 Orig_Node : Node_Id := Empty;
14135 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
14137 function Is_Entry (Nam : Node_Id) return Boolean;
14138 -- Determine whether Nam is an entry. Traverse selectors if there are
14139 -- nested selected components.
14141 --------------
14142 -- Is_Entry --
14143 --------------
14145 function Is_Entry (Nam : Node_Id) return Boolean is
14146 begin
14147 if Nkind (Nam) = N_Selected_Component then
14148 return Is_Entry (Selector_Name (Nam));
14149 end if;
14151 return Ekind (Entity (Nam)) = E_Entry;
14152 end Is_Entry;
14154 -- Start of processing for Is_Renamed_Entry
14156 begin
14157 if Present (Alias (Proc_Nam)) then
14158 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
14159 end if;
14161 -- Look for a rewritten subprogram renaming declaration
14163 if Nkind (Subp_Decl) = N_Subprogram_Declaration
14164 and then Present (Original_Node (Subp_Decl))
14165 then
14166 Orig_Node := Original_Node (Subp_Decl);
14167 end if;
14169 -- The rewritten subprogram is actually an entry
14171 if Present (Orig_Node)
14172 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
14173 and then Is_Entry (Name (Orig_Node))
14174 then
14175 return True;
14176 end if;
14178 return False;
14179 end Is_Renamed_Entry;
14181 -----------------------------
14182 -- Is_Renaming_Declaration --
14183 -----------------------------
14185 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
14186 begin
14187 case Nkind (N) is
14188 when N_Exception_Renaming_Declaration |
14189 N_Generic_Function_Renaming_Declaration |
14190 N_Generic_Package_Renaming_Declaration |
14191 N_Generic_Procedure_Renaming_Declaration |
14192 N_Object_Renaming_Declaration |
14193 N_Package_Renaming_Declaration |
14194 N_Subprogram_Renaming_Declaration =>
14195 return True;
14197 when others =>
14198 return False;
14199 end case;
14200 end Is_Renaming_Declaration;
14202 ----------------------------
14203 -- Is_Reversible_Iterator --
14204 ----------------------------
14206 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
14207 Ifaces_List : Elist_Id;
14208 Iface_Elmt : Elmt_Id;
14209 Iface : Entity_Id;
14211 begin
14212 if Is_Class_Wide_Type (Typ)
14213 and then Chars (Root_Type (Typ)) = Name_Reversible_Iterator
14214 and then Is_Predefined_File_Name
14215 (Unit_File_Name (Get_Source_Unit (Root_Type (Typ))))
14216 then
14217 return True;
14219 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
14220 return False;
14222 else
14223 Collect_Interfaces (Typ, Ifaces_List);
14225 Iface_Elmt := First_Elmt (Ifaces_List);
14226 while Present (Iface_Elmt) loop
14227 Iface := Node (Iface_Elmt);
14228 if Chars (Iface) = Name_Reversible_Iterator
14229 and then
14230 Is_Predefined_File_Name
14231 (Unit_File_Name (Get_Source_Unit (Iface)))
14232 then
14233 return True;
14234 end if;
14236 Next_Elmt (Iface_Elmt);
14237 end loop;
14238 end if;
14240 return False;
14241 end Is_Reversible_Iterator;
14243 ----------------------
14244 -- Is_Selector_Name --
14245 ----------------------
14247 function Is_Selector_Name (N : Node_Id) return Boolean is
14248 begin
14249 if not Is_List_Member (N) then
14250 declare
14251 P : constant Node_Id := Parent (N);
14252 begin
14253 return Nkind_In (P, N_Expanded_Name,
14254 N_Generic_Association,
14255 N_Parameter_Association,
14256 N_Selected_Component)
14257 and then Selector_Name (P) = N;
14258 end;
14260 else
14261 declare
14262 L : constant List_Id := List_Containing (N);
14263 P : constant Node_Id := Parent (L);
14264 begin
14265 return (Nkind (P) = N_Discriminant_Association
14266 and then Selector_Names (P) = L)
14267 or else
14268 (Nkind (P) = N_Component_Association
14269 and then Choices (P) = L);
14270 end;
14271 end if;
14272 end Is_Selector_Name;
14274 ---------------------------------
14275 -- Is_Single_Concurrent_Object --
14276 ---------------------------------
14278 function Is_Single_Concurrent_Object (Id : Entity_Id) return Boolean is
14279 begin
14280 return
14281 Is_Single_Protected_Object (Id) or else Is_Single_Task_Object (Id);
14282 end Is_Single_Concurrent_Object;
14284 -------------------------------
14285 -- Is_Single_Concurrent_Type --
14286 -------------------------------
14288 function Is_Single_Concurrent_Type (Id : Entity_Id) return Boolean is
14289 begin
14290 return
14291 Ekind_In (Id, E_Protected_Type, E_Task_Type)
14292 and then Is_Single_Concurrent_Type_Declaration
14293 (Declaration_Node (Id));
14294 end Is_Single_Concurrent_Type;
14296 -------------------------------------------
14297 -- Is_Single_Concurrent_Type_Declaration --
14298 -------------------------------------------
14300 function Is_Single_Concurrent_Type_Declaration
14301 (N : Node_Id) return Boolean
14303 begin
14304 return Nkind_In (Original_Node (N), N_Single_Protected_Declaration,
14305 N_Single_Task_Declaration);
14306 end Is_Single_Concurrent_Type_Declaration;
14308 ---------------------------------------------
14309 -- Is_Single_Precision_Floating_Point_Type --
14310 ---------------------------------------------
14312 function Is_Single_Precision_Floating_Point_Type
14313 (E : Entity_Id) return Boolean is
14314 begin
14315 return Is_Floating_Point_Type (E)
14316 and then Machine_Radix_Value (E) = Uint_2
14317 and then Machine_Mantissa_Value (E) = Uint_24
14318 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
14319 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
14320 end Is_Single_Precision_Floating_Point_Type;
14322 --------------------------------
14323 -- Is_Single_Protected_Object --
14324 --------------------------------
14326 function Is_Single_Protected_Object (Id : Entity_Id) return Boolean is
14327 begin
14328 return
14329 Ekind (Id) = E_Variable
14330 and then Ekind (Etype (Id)) = E_Protected_Type
14331 and then Is_Single_Concurrent_Type (Etype (Id));
14332 end Is_Single_Protected_Object;
14334 ---------------------------
14335 -- Is_Single_Task_Object --
14336 ---------------------------
14338 function Is_Single_Task_Object (Id : Entity_Id) return Boolean is
14339 begin
14340 return
14341 Ekind (Id) = E_Variable
14342 and then Ekind (Etype (Id)) = E_Task_Type
14343 and then Is_Single_Concurrent_Type (Etype (Id));
14344 end Is_Single_Task_Object;
14346 -------------------------------------
14347 -- Is_SPARK_05_Initialization_Expr --
14348 -------------------------------------
14350 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
14351 Is_Ok : Boolean;
14352 Expr : Node_Id;
14353 Comp_Assn : Node_Id;
14354 Orig_N : constant Node_Id := Original_Node (N);
14356 begin
14357 Is_Ok := True;
14359 if not Comes_From_Source (Orig_N) then
14360 goto Done;
14361 end if;
14363 pragma Assert (Nkind (Orig_N) in N_Subexpr);
14365 case Nkind (Orig_N) is
14366 when N_Character_Literal |
14367 N_Integer_Literal |
14368 N_Real_Literal |
14369 N_String_Literal =>
14370 null;
14372 when N_Identifier |
14373 N_Expanded_Name =>
14374 if Is_Entity_Name (Orig_N)
14375 and then Present (Entity (Orig_N)) -- needed in some cases
14376 then
14377 case Ekind (Entity (Orig_N)) is
14378 when E_Constant |
14379 E_Enumeration_Literal |
14380 E_Named_Integer |
14381 E_Named_Real =>
14382 null;
14383 when others =>
14384 if Is_Type (Entity (Orig_N)) then
14385 null;
14386 else
14387 Is_Ok := False;
14388 end if;
14389 end case;
14390 end if;
14392 when N_Qualified_Expression |
14393 N_Type_Conversion =>
14394 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
14396 when N_Unary_Op =>
14397 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
14399 when N_Binary_Op |
14400 N_Short_Circuit |
14401 N_Membership_Test =>
14402 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
14403 and then
14404 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
14406 when N_Aggregate |
14407 N_Extension_Aggregate =>
14408 if Nkind (Orig_N) = N_Extension_Aggregate then
14409 Is_Ok :=
14410 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
14411 end if;
14413 Expr := First (Expressions (Orig_N));
14414 while Present (Expr) loop
14415 if not Is_SPARK_05_Initialization_Expr (Expr) then
14416 Is_Ok := False;
14417 goto Done;
14418 end if;
14420 Next (Expr);
14421 end loop;
14423 Comp_Assn := First (Component_Associations (Orig_N));
14424 while Present (Comp_Assn) loop
14425 Expr := Expression (Comp_Assn);
14427 -- Note: test for Present here needed for box assocation
14429 if Present (Expr)
14430 and then not Is_SPARK_05_Initialization_Expr (Expr)
14431 then
14432 Is_Ok := False;
14433 goto Done;
14434 end if;
14436 Next (Comp_Assn);
14437 end loop;
14439 when N_Attribute_Reference =>
14440 if Nkind (Prefix (Orig_N)) in N_Subexpr then
14441 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
14442 end if;
14444 Expr := First (Expressions (Orig_N));
14445 while Present (Expr) loop
14446 if not Is_SPARK_05_Initialization_Expr (Expr) then
14447 Is_Ok := False;
14448 goto Done;
14449 end if;
14451 Next (Expr);
14452 end loop;
14454 -- Selected components might be expanded named not yet resolved, so
14455 -- default on the safe side. (Eg on sparklex.ads)
14457 when N_Selected_Component =>
14458 null;
14460 when others =>
14461 Is_Ok := False;
14462 end case;
14464 <<Done>>
14465 return Is_Ok;
14466 end Is_SPARK_05_Initialization_Expr;
14468 ----------------------------------
14469 -- Is_SPARK_05_Object_Reference --
14470 ----------------------------------
14472 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
14473 begin
14474 if Is_Entity_Name (N) then
14475 return Present (Entity (N))
14476 and then
14477 (Ekind_In (Entity (N), E_Constant, E_Variable)
14478 or else Ekind (Entity (N)) in Formal_Kind);
14480 else
14481 case Nkind (N) is
14482 when N_Selected_Component =>
14483 return Is_SPARK_05_Object_Reference (Prefix (N));
14485 when others =>
14486 return False;
14487 end case;
14488 end if;
14489 end Is_SPARK_05_Object_Reference;
14491 -----------------------------
14492 -- Is_Specific_Tagged_Type --
14493 -----------------------------
14495 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
14496 Full_Typ : Entity_Id;
14498 begin
14499 -- Handle private types
14501 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
14502 Full_Typ := Full_View (Typ);
14503 else
14504 Full_Typ := Typ;
14505 end if;
14507 -- A specific tagged type is a non-class-wide tagged type
14509 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
14510 end Is_Specific_Tagged_Type;
14512 ------------------
14513 -- Is_Statement --
14514 ------------------
14516 function Is_Statement (N : Node_Id) return Boolean is
14517 begin
14518 return
14519 Nkind (N) in N_Statement_Other_Than_Procedure_Call
14520 or else Nkind (N) = N_Procedure_Call_Statement;
14521 end Is_Statement;
14523 ---------------------------------------
14524 -- Is_Subprogram_Contract_Annotation --
14525 ---------------------------------------
14527 function Is_Subprogram_Contract_Annotation
14528 (Item : Node_Id) return Boolean
14530 Nam : Name_Id;
14532 begin
14533 if Nkind (Item) = N_Aspect_Specification then
14534 Nam := Chars (Identifier (Item));
14536 else pragma Assert (Nkind (Item) = N_Pragma);
14537 Nam := Pragma_Name_Mapped (Item);
14538 end if;
14540 return Nam = Name_Contract_Cases
14541 or else Nam = Name_Depends
14542 or else Nam = Name_Extensions_Visible
14543 or else Nam = Name_Global
14544 or else Nam = Name_Post
14545 or else Nam = Name_Post_Class
14546 or else Nam = Name_Postcondition
14547 or else Nam = Name_Pre
14548 or else Nam = Name_Pre_Class
14549 or else Nam = Name_Precondition
14550 or else Nam = Name_Refined_Depends
14551 or else Nam = Name_Refined_Global
14552 or else Nam = Name_Refined_Post
14553 or else Nam = Name_Test_Case;
14554 end Is_Subprogram_Contract_Annotation;
14556 --------------------------------------------------
14557 -- Is_Subprogram_Stub_Without_Prior_Declaration --
14558 --------------------------------------------------
14560 function Is_Subprogram_Stub_Without_Prior_Declaration
14561 (N : Node_Id) return Boolean
14563 begin
14564 -- A subprogram stub without prior declaration serves as declaration for
14565 -- the actual subprogram body. As such, it has an attached defining
14566 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
14568 return Nkind (N) = N_Subprogram_Body_Stub
14569 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
14570 end Is_Subprogram_Stub_Without_Prior_Declaration;
14572 --------------------------
14573 -- Is_Suspension_Object --
14574 --------------------------
14576 function Is_Suspension_Object (Id : Entity_Id) return Boolean is
14577 begin
14578 -- This approach does an exact name match rather than to rely on
14579 -- RTSfind. Routine Is_Effectively_Volatile is used by clients of the
14580 -- front end at point where all auxiliary tables are locked and any
14581 -- modifications to them are treated as violations. Do not tamper with
14582 -- the tables, instead examine the Chars fields of all the scopes of Id.
14584 return
14585 Chars (Id) = Name_Suspension_Object
14586 and then Present (Scope (Id))
14587 and then Chars (Scope (Id)) = Name_Synchronous_Task_Control
14588 and then Present (Scope (Scope (Id)))
14589 and then Chars (Scope (Scope (Id))) = Name_Ada
14590 and then Present (Scope (Scope (Scope (Id))))
14591 and then Scope (Scope (Scope (Id))) = Standard_Standard;
14592 end Is_Suspension_Object;
14594 ----------------------------
14595 -- Is_Synchronized_Object --
14596 ----------------------------
14598 function Is_Synchronized_Object (Id : Entity_Id) return Boolean is
14599 Prag : Node_Id;
14601 begin
14602 if Is_Object (Id) then
14604 -- The object is synchronized if it is of a type that yields a
14605 -- synchronized object.
14607 if Yields_Synchronized_Object (Etype (Id)) then
14608 return True;
14610 -- The object is synchronized if it is atomic and Async_Writers is
14611 -- enabled.
14613 elsif Is_Atomic (Id) and then Async_Writers_Enabled (Id) then
14614 return True;
14616 -- A constant is a synchronized object by default
14618 elsif Ekind (Id) = E_Constant then
14619 return True;
14621 -- A variable is a synchronized object if it is subject to pragma
14622 -- Constant_After_Elaboration.
14624 elsif Ekind (Id) = E_Variable then
14625 Prag := Get_Pragma (Id, Pragma_Constant_After_Elaboration);
14627 return Present (Prag) and then Is_Enabled_Pragma (Prag);
14628 end if;
14629 end if;
14631 -- Otherwise the input is not an object or it does not qualify as a
14632 -- synchronized object.
14634 return False;
14635 end Is_Synchronized_Object;
14637 ---------------------------------
14638 -- Is_Synchronized_Tagged_Type --
14639 ---------------------------------
14641 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
14642 Kind : constant Entity_Kind := Ekind (Base_Type (E));
14644 begin
14645 -- A task or protected type derived from an interface is a tagged type.
14646 -- Such a tagged type is called a synchronized tagged type, as are
14647 -- synchronized interfaces and private extensions whose declaration
14648 -- includes the reserved word synchronized.
14650 return (Is_Tagged_Type (E)
14651 and then (Kind = E_Task_Type
14652 or else
14653 Kind = E_Protected_Type))
14654 or else
14655 (Is_Interface (E)
14656 and then Is_Synchronized_Interface (E))
14657 or else
14658 (Ekind (E) = E_Record_Type_With_Private
14659 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
14660 and then (Synchronized_Present (Parent (E))
14661 or else Is_Synchronized_Interface (Etype (E))));
14662 end Is_Synchronized_Tagged_Type;
14664 -----------------
14665 -- Is_Transfer --
14666 -----------------
14668 function Is_Transfer (N : Node_Id) return Boolean is
14669 Kind : constant Node_Kind := Nkind (N);
14671 begin
14672 if Kind = N_Simple_Return_Statement
14673 or else
14674 Kind = N_Extended_Return_Statement
14675 or else
14676 Kind = N_Goto_Statement
14677 or else
14678 Kind = N_Raise_Statement
14679 or else
14680 Kind = N_Requeue_Statement
14681 then
14682 return True;
14684 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
14685 and then No (Condition (N))
14686 then
14687 return True;
14689 elsif Kind = N_Procedure_Call_Statement
14690 and then Is_Entity_Name (Name (N))
14691 and then Present (Entity (Name (N)))
14692 and then No_Return (Entity (Name (N)))
14693 then
14694 return True;
14696 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
14697 return True;
14699 else
14700 return False;
14701 end if;
14702 end Is_Transfer;
14704 -------------
14705 -- Is_True --
14706 -------------
14708 function Is_True (U : Uint) return Boolean is
14709 begin
14710 return (U /= 0);
14711 end Is_True;
14713 --------------------------------------
14714 -- Is_Unchecked_Conversion_Instance --
14715 --------------------------------------
14717 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
14718 Par : Node_Id;
14720 begin
14721 -- Look for a function whose generic parent is the predefined intrinsic
14722 -- function Unchecked_Conversion, or for one that renames such an
14723 -- instance.
14725 if Ekind (Id) = E_Function then
14726 Par := Parent (Id);
14728 if Nkind (Par) = N_Function_Specification then
14729 Par := Generic_Parent (Par);
14731 if Present (Par) then
14732 return
14733 Chars (Par) = Name_Unchecked_Conversion
14734 and then Is_Intrinsic_Subprogram (Par)
14735 and then Is_Predefined_File_Name
14736 (Unit_File_Name (Get_Source_Unit (Par)));
14737 else
14738 return
14739 Present (Alias (Id))
14740 and then Is_Unchecked_Conversion_Instance (Alias (Id));
14741 end if;
14742 end if;
14743 end if;
14745 return False;
14746 end Is_Unchecked_Conversion_Instance;
14748 -------------------------------
14749 -- Is_Universal_Numeric_Type --
14750 -------------------------------
14752 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
14753 begin
14754 return T = Universal_Integer or else T = Universal_Real;
14755 end Is_Universal_Numeric_Type;
14757 ----------------------------
14758 -- Is_Variable_Size_Array --
14759 ----------------------------
14761 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
14762 Idx : Node_Id;
14764 begin
14765 pragma Assert (Is_Array_Type (E));
14767 -- Check if some index is initialized with a non-constant value
14769 Idx := First_Index (E);
14770 while Present (Idx) loop
14771 if Nkind (Idx) = N_Range then
14772 if not Is_Constant_Bound (Low_Bound (Idx))
14773 or else not Is_Constant_Bound (High_Bound (Idx))
14774 then
14775 return True;
14776 end if;
14777 end if;
14779 Idx := Next_Index (Idx);
14780 end loop;
14782 return False;
14783 end Is_Variable_Size_Array;
14785 -----------------------------
14786 -- Is_Variable_Size_Record --
14787 -----------------------------
14789 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
14790 Comp : Entity_Id;
14791 Comp_Typ : Entity_Id;
14793 begin
14794 pragma Assert (Is_Record_Type (E));
14796 Comp := First_Entity (E);
14797 while Present (Comp) loop
14798 Comp_Typ := Etype (Comp);
14800 -- Recursive call if the record type has discriminants
14802 if Is_Record_Type (Comp_Typ)
14803 and then Has_Discriminants (Comp_Typ)
14804 and then Is_Variable_Size_Record (Comp_Typ)
14805 then
14806 return True;
14808 elsif Is_Array_Type (Comp_Typ)
14809 and then Is_Variable_Size_Array (Comp_Typ)
14810 then
14811 return True;
14812 end if;
14814 Next_Entity (Comp);
14815 end loop;
14817 return False;
14818 end Is_Variable_Size_Record;
14820 -----------------
14821 -- Is_Variable --
14822 -----------------
14824 function Is_Variable
14825 (N : Node_Id;
14826 Use_Original_Node : Boolean := True) return Boolean
14828 Orig_Node : Node_Id;
14830 function In_Protected_Function (E : Entity_Id) return Boolean;
14831 -- Within a protected function, the private components of the enclosing
14832 -- protected type are constants. A function nested within a (protected)
14833 -- procedure is not itself protected. Within the body of a protected
14834 -- function the current instance of the protected type is a constant.
14836 function Is_Variable_Prefix (P : Node_Id) return Boolean;
14837 -- Prefixes can involve implicit dereferences, in which case we must
14838 -- test for the case of a reference of a constant access type, which can
14839 -- can never be a variable.
14841 ---------------------------
14842 -- In_Protected_Function --
14843 ---------------------------
14845 function In_Protected_Function (E : Entity_Id) return Boolean is
14846 Prot : Entity_Id;
14847 S : Entity_Id;
14849 begin
14850 -- E is the current instance of a type
14852 if Is_Type (E) then
14853 Prot := E;
14855 -- E is an object
14857 else
14858 Prot := Scope (E);
14859 end if;
14861 if not Is_Protected_Type (Prot) then
14862 return False;
14864 else
14865 S := Current_Scope;
14866 while Present (S) and then S /= Prot loop
14867 if Ekind (S) = E_Function and then Scope (S) = Prot then
14868 return True;
14869 end if;
14871 S := Scope (S);
14872 end loop;
14874 return False;
14875 end if;
14876 end In_Protected_Function;
14878 ------------------------
14879 -- Is_Variable_Prefix --
14880 ------------------------
14882 function Is_Variable_Prefix (P : Node_Id) return Boolean is
14883 begin
14884 if Is_Access_Type (Etype (P)) then
14885 return not Is_Access_Constant (Root_Type (Etype (P)));
14887 -- For the case of an indexed component whose prefix has a packed
14888 -- array type, the prefix has been rewritten into a type conversion.
14889 -- Determine variable-ness from the converted expression.
14891 elsif Nkind (P) = N_Type_Conversion
14892 and then not Comes_From_Source (P)
14893 and then Is_Array_Type (Etype (P))
14894 and then Is_Packed (Etype (P))
14895 then
14896 return Is_Variable (Expression (P));
14898 else
14899 return Is_Variable (P);
14900 end if;
14901 end Is_Variable_Prefix;
14903 -- Start of processing for Is_Variable
14905 begin
14906 -- Special check, allow x'Deref(expr) as a variable
14908 if Nkind (N) = N_Attribute_Reference
14909 and then Attribute_Name (N) = Name_Deref
14910 then
14911 return True;
14912 end if;
14914 -- Check if we perform the test on the original node since this may be a
14915 -- test of syntactic categories which must not be disturbed by whatever
14916 -- rewriting might have occurred. For example, an aggregate, which is
14917 -- certainly NOT a variable, could be turned into a variable by
14918 -- expansion.
14920 if Use_Original_Node then
14921 Orig_Node := Original_Node (N);
14922 else
14923 Orig_Node := N;
14924 end if;
14926 -- Definitely OK if Assignment_OK is set. Since this is something that
14927 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
14929 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
14930 return True;
14932 -- Normally we go to the original node, but there is one exception where
14933 -- we use the rewritten node, namely when it is an explicit dereference.
14934 -- The generated code may rewrite a prefix which is an access type with
14935 -- an explicit dereference. The dereference is a variable, even though
14936 -- the original node may not be (since it could be a constant of the
14937 -- access type).
14939 -- In Ada 2005 we have a further case to consider: the prefix may be a
14940 -- function call given in prefix notation. The original node appears to
14941 -- be a selected component, but we need to examine the call.
14943 elsif Nkind (N) = N_Explicit_Dereference
14944 and then Nkind (Orig_Node) /= N_Explicit_Dereference
14945 and then Present (Etype (Orig_Node))
14946 and then Is_Access_Type (Etype (Orig_Node))
14947 then
14948 -- Note that if the prefix is an explicit dereference that does not
14949 -- come from source, we must check for a rewritten function call in
14950 -- prefixed notation before other forms of rewriting, to prevent a
14951 -- compiler crash.
14953 return
14954 (Nkind (Orig_Node) = N_Function_Call
14955 and then not Is_Access_Constant (Etype (Prefix (N))))
14956 or else
14957 Is_Variable_Prefix (Original_Node (Prefix (N)));
14959 -- in Ada 2012, the dereference may have been added for a type with
14960 -- a declared implicit dereference aspect. Check that it is not an
14961 -- access to constant.
14963 elsif Nkind (N) = N_Explicit_Dereference
14964 and then Present (Etype (Orig_Node))
14965 and then Ada_Version >= Ada_2012
14966 and then Has_Implicit_Dereference (Etype (Orig_Node))
14967 then
14968 return not Is_Access_Constant (Etype (Prefix (N)));
14970 -- A function call is never a variable
14972 elsif Nkind (N) = N_Function_Call then
14973 return False;
14975 -- All remaining checks use the original node
14977 elsif Is_Entity_Name (Orig_Node)
14978 and then Present (Entity (Orig_Node))
14979 then
14980 declare
14981 E : constant Entity_Id := Entity (Orig_Node);
14982 K : constant Entity_Kind := Ekind (E);
14984 begin
14985 return (K = E_Variable
14986 and then Nkind (Parent (E)) /= N_Exception_Handler)
14987 or else (K = E_Component
14988 and then not In_Protected_Function (E))
14989 or else K = E_Out_Parameter
14990 or else K = E_In_Out_Parameter
14991 or else K = E_Generic_In_Out_Parameter
14993 -- Current instance of type. If this is a protected type, check
14994 -- we are not within the body of one of its protected functions.
14996 or else (Is_Type (E)
14997 and then In_Open_Scopes (E)
14998 and then not In_Protected_Function (E))
15000 or else (Is_Incomplete_Or_Private_Type (E)
15001 and then In_Open_Scopes (Full_View (E)));
15002 end;
15004 else
15005 case Nkind (Orig_Node) is
15006 when N_Indexed_Component | N_Slice =>
15007 return Is_Variable_Prefix (Prefix (Orig_Node));
15009 when N_Selected_Component =>
15010 return (Is_Variable (Selector_Name (Orig_Node))
15011 and then Is_Variable_Prefix (Prefix (Orig_Node)))
15012 or else
15013 (Nkind (N) = N_Expanded_Name
15014 and then Scope (Entity (N)) = Entity (Prefix (N)));
15016 -- For an explicit dereference, the type of the prefix cannot
15017 -- be an access to constant or an access to subprogram.
15019 when N_Explicit_Dereference =>
15020 declare
15021 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
15022 begin
15023 return Is_Access_Type (Typ)
15024 and then not Is_Access_Constant (Root_Type (Typ))
15025 and then Ekind (Typ) /= E_Access_Subprogram_Type;
15026 end;
15028 -- The type conversion is the case where we do not deal with the
15029 -- context dependent special case of an actual parameter. Thus
15030 -- the type conversion is only considered a variable for the
15031 -- purposes of this routine if the target type is tagged. However,
15032 -- a type conversion is considered to be a variable if it does not
15033 -- come from source (this deals for example with the conversions
15034 -- of expressions to their actual subtypes).
15036 when N_Type_Conversion =>
15037 return Is_Variable (Expression (Orig_Node))
15038 and then
15039 (not Comes_From_Source (Orig_Node)
15040 or else
15041 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
15042 and then
15043 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
15045 -- GNAT allows an unchecked type conversion as a variable. This
15046 -- only affects the generation of internal expanded code, since
15047 -- calls to instantiations of Unchecked_Conversion are never
15048 -- considered variables (since they are function calls).
15050 when N_Unchecked_Type_Conversion =>
15051 return Is_Variable (Expression (Orig_Node));
15053 when others =>
15054 return False;
15055 end case;
15056 end if;
15057 end Is_Variable;
15059 ------------------------------
15060 -- Is_Verifiable_DIC_Pragma --
15061 ------------------------------
15063 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
15064 Args : constant List_Id := Pragma_Argument_Associations (Prag);
15066 begin
15067 -- To qualify as verifiable, a DIC pragma must have a non-null argument
15069 return
15070 Present (Args)
15071 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
15072 end Is_Verifiable_DIC_Pragma;
15074 ---------------------------
15075 -- Is_Visibly_Controlled --
15076 ---------------------------
15078 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
15079 Root : constant Entity_Id := Root_Type (T);
15080 begin
15081 return Chars (Scope (Root)) = Name_Finalization
15082 and then Chars (Scope (Scope (Root))) = Name_Ada
15083 and then Scope (Scope (Scope (Root))) = Standard_Standard;
15084 end Is_Visibly_Controlled;
15086 --------------------------
15087 -- Is_Volatile_Function --
15088 --------------------------
15090 function Is_Volatile_Function (Func_Id : Entity_Id) return Boolean is
15091 begin
15092 pragma Assert (Ekind_In (Func_Id, E_Function, E_Generic_Function));
15094 -- A function declared within a protected type is volatile
15096 if Is_Protected_Type (Scope (Func_Id)) then
15097 return True;
15099 -- An instance of Ada.Unchecked_Conversion is a volatile function if
15100 -- either the source or the target are effectively volatile.
15102 elsif Is_Unchecked_Conversion_Instance (Func_Id)
15103 and then Has_Effectively_Volatile_Profile (Func_Id)
15104 then
15105 return True;
15107 -- Otherwise the function is treated as volatile if it is subject to
15108 -- enabled pragma Volatile_Function.
15110 else
15111 return
15112 Is_Enabled_Pragma (Get_Pragma (Func_Id, Pragma_Volatile_Function));
15113 end if;
15114 end Is_Volatile_Function;
15116 ------------------------
15117 -- Is_Volatile_Object --
15118 ------------------------
15120 function Is_Volatile_Object (N : Node_Id) return Boolean is
15122 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
15123 -- If prefix is an implicit dereference, examine designated type
15125 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
15126 -- Determines if given object has volatile components
15128 ------------------------
15129 -- Is_Volatile_Prefix --
15130 ------------------------
15132 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
15133 Typ : constant Entity_Id := Etype (N);
15135 begin
15136 if Is_Access_Type (Typ) then
15137 declare
15138 Dtyp : constant Entity_Id := Designated_Type (Typ);
15140 begin
15141 return Is_Volatile (Dtyp)
15142 or else Has_Volatile_Components (Dtyp);
15143 end;
15145 else
15146 return Object_Has_Volatile_Components (N);
15147 end if;
15148 end Is_Volatile_Prefix;
15150 ------------------------------------
15151 -- Object_Has_Volatile_Components --
15152 ------------------------------------
15154 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
15155 Typ : constant Entity_Id := Etype (N);
15157 begin
15158 if Is_Volatile (Typ)
15159 or else Has_Volatile_Components (Typ)
15160 then
15161 return True;
15163 elsif Is_Entity_Name (N)
15164 and then (Has_Volatile_Components (Entity (N))
15165 or else Is_Volatile (Entity (N)))
15166 then
15167 return True;
15169 elsif Nkind (N) = N_Indexed_Component
15170 or else Nkind (N) = N_Selected_Component
15171 then
15172 return Is_Volatile_Prefix (Prefix (N));
15174 else
15175 return False;
15176 end if;
15177 end Object_Has_Volatile_Components;
15179 -- Start of processing for Is_Volatile_Object
15181 begin
15182 if Nkind (N) = N_Defining_Identifier then
15183 return Is_Volatile (N) or else Is_Volatile (Etype (N));
15185 elsif Nkind (N) = N_Expanded_Name then
15186 return Is_Volatile_Object (Entity (N));
15188 elsif Is_Volatile (Etype (N))
15189 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
15190 then
15191 return True;
15193 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
15194 and then Is_Volatile_Prefix (Prefix (N))
15195 then
15196 return True;
15198 elsif Nkind (N) = N_Selected_Component
15199 and then Is_Volatile (Entity (Selector_Name (N)))
15200 then
15201 return True;
15203 else
15204 return False;
15205 end if;
15206 end Is_Volatile_Object;
15208 ---------------------------
15209 -- Itype_Has_Declaration --
15210 ---------------------------
15212 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
15213 begin
15214 pragma Assert (Is_Itype (Id));
15215 return Present (Parent (Id))
15216 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
15217 N_Subtype_Declaration)
15218 and then Defining_Entity (Parent (Id)) = Id;
15219 end Itype_Has_Declaration;
15221 -------------------------
15222 -- Kill_Current_Values --
15223 -------------------------
15225 procedure Kill_Current_Values
15226 (Ent : Entity_Id;
15227 Last_Assignment_Only : Boolean := False)
15229 begin
15230 if Is_Assignable (Ent) then
15231 Set_Last_Assignment (Ent, Empty);
15232 end if;
15234 if Is_Object (Ent) then
15235 if not Last_Assignment_Only then
15236 Kill_Checks (Ent);
15237 Set_Current_Value (Ent, Empty);
15239 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
15240 -- for a constant. Once the constant is elaborated, its value is
15241 -- not changed, therefore the associated flags that describe the
15242 -- value should not be modified either.
15244 if Ekind (Ent) = E_Constant then
15245 null;
15247 -- Non-constant entities
15249 else
15250 if not Can_Never_Be_Null (Ent) then
15251 Set_Is_Known_Non_Null (Ent, False);
15252 end if;
15254 Set_Is_Known_Null (Ent, False);
15256 -- Reset the Is_Known_Valid flag unless the type is always
15257 -- valid. This does not apply to a loop parameter because its
15258 -- bounds are defined by the loop header and therefore always
15259 -- valid.
15261 if not Is_Known_Valid (Etype (Ent))
15262 and then Ekind (Ent) /= E_Loop_Parameter
15263 then
15264 Set_Is_Known_Valid (Ent, False);
15265 end if;
15266 end if;
15267 end if;
15268 end if;
15269 end Kill_Current_Values;
15271 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
15272 S : Entity_Id;
15274 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
15275 -- Clear current value for entity E and all entities chained to E
15277 ------------------------------------------
15278 -- Kill_Current_Values_For_Entity_Chain --
15279 ------------------------------------------
15281 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
15282 Ent : Entity_Id;
15283 begin
15284 Ent := E;
15285 while Present (Ent) loop
15286 Kill_Current_Values (Ent, Last_Assignment_Only);
15287 Next_Entity (Ent);
15288 end loop;
15289 end Kill_Current_Values_For_Entity_Chain;
15291 -- Start of processing for Kill_Current_Values
15293 begin
15294 -- Kill all saved checks, a special case of killing saved values
15296 if not Last_Assignment_Only then
15297 Kill_All_Checks;
15298 end if;
15300 -- Loop through relevant scopes, which includes the current scope and
15301 -- any parent scopes if the current scope is a block or a package.
15303 S := Current_Scope;
15304 Scope_Loop : loop
15306 -- Clear current values of all entities in current scope
15308 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
15310 -- If scope is a package, also clear current values of all private
15311 -- entities in the scope.
15313 if Is_Package_Or_Generic_Package (S)
15314 or else Is_Concurrent_Type (S)
15315 then
15316 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
15317 end if;
15319 -- If this is a not a subprogram, deal with parents
15321 if not Is_Subprogram (S) then
15322 S := Scope (S);
15323 exit Scope_Loop when S = Standard_Standard;
15324 else
15325 exit Scope_Loop;
15326 end if;
15327 end loop Scope_Loop;
15328 end Kill_Current_Values;
15330 --------------------------
15331 -- Kill_Size_Check_Code --
15332 --------------------------
15334 procedure Kill_Size_Check_Code (E : Entity_Id) is
15335 begin
15336 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
15337 and then Present (Size_Check_Code (E))
15338 then
15339 Remove (Size_Check_Code (E));
15340 Set_Size_Check_Code (E, Empty);
15341 end if;
15342 end Kill_Size_Check_Code;
15344 --------------------------
15345 -- Known_To_Be_Assigned --
15346 --------------------------
15348 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
15349 P : constant Node_Id := Parent (N);
15351 begin
15352 case Nkind (P) is
15354 -- Test left side of assignment
15356 when N_Assignment_Statement =>
15357 return N = Name (P);
15359 -- Function call arguments are never lvalues
15361 when N_Function_Call =>
15362 return False;
15364 -- Positional parameter for procedure or accept call
15366 when N_Procedure_Call_Statement |
15367 N_Accept_Statement
15369 declare
15370 Proc : Entity_Id;
15371 Form : Entity_Id;
15372 Act : Node_Id;
15374 begin
15375 Proc := Get_Subprogram_Entity (P);
15377 if No (Proc) then
15378 return False;
15379 end if;
15381 -- If we are not a list member, something is strange, so
15382 -- be conservative and return False.
15384 if not Is_List_Member (N) then
15385 return False;
15386 end if;
15388 -- We are going to find the right formal by stepping forward
15389 -- through the formals, as we step backwards in the actuals.
15391 Form := First_Formal (Proc);
15392 Act := N;
15393 loop
15394 -- If no formal, something is weird, so be conservative
15395 -- and return False.
15397 if No (Form) then
15398 return False;
15399 end if;
15401 Prev (Act);
15402 exit when No (Act);
15403 Next_Formal (Form);
15404 end loop;
15406 return Ekind (Form) /= E_In_Parameter;
15407 end;
15409 -- Named parameter for procedure or accept call
15411 when N_Parameter_Association =>
15412 declare
15413 Proc : Entity_Id;
15414 Form : Entity_Id;
15416 begin
15417 Proc := Get_Subprogram_Entity (Parent (P));
15419 if No (Proc) then
15420 return False;
15421 end if;
15423 -- Loop through formals to find the one that matches
15425 Form := First_Formal (Proc);
15426 loop
15427 -- If no matching formal, that's peculiar, some kind of
15428 -- previous error, so return False to be conservative.
15429 -- Actually this also happens in legal code in the case
15430 -- where P is a parameter association for an Extra_Formal???
15432 if No (Form) then
15433 return False;
15434 end if;
15436 -- Else test for match
15438 if Chars (Form) = Chars (Selector_Name (P)) then
15439 return Ekind (Form) /= E_In_Parameter;
15440 end if;
15442 Next_Formal (Form);
15443 end loop;
15444 end;
15446 -- Test for appearing in a conversion that itself appears
15447 -- in an lvalue context, since this should be an lvalue.
15449 when N_Type_Conversion =>
15450 return Known_To_Be_Assigned (P);
15452 -- All other references are definitely not known to be modifications
15454 when others =>
15455 return False;
15457 end case;
15458 end Known_To_Be_Assigned;
15460 ---------------------------
15461 -- Last_Source_Statement --
15462 ---------------------------
15464 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
15465 N : Node_Id;
15467 begin
15468 N := Last (Statements (HSS));
15469 while Present (N) loop
15470 exit when Comes_From_Source (N);
15471 Prev (N);
15472 end loop;
15474 return N;
15475 end Last_Source_Statement;
15477 ----------------------------------
15478 -- Matching_Static_Array_Bounds --
15479 ----------------------------------
15481 function Matching_Static_Array_Bounds
15482 (L_Typ : Node_Id;
15483 R_Typ : Node_Id) return Boolean
15485 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
15486 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
15488 L_Index : Node_Id;
15489 R_Index : Node_Id;
15490 L_Low : Node_Id;
15491 L_High : Node_Id;
15492 L_Len : Uint;
15493 R_Low : Node_Id;
15494 R_High : Node_Id;
15495 R_Len : Uint;
15497 begin
15498 if L_Ndims /= R_Ndims then
15499 return False;
15500 end if;
15502 -- Unconstrained types do not have static bounds
15504 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
15505 return False;
15506 end if;
15508 -- First treat specially the first dimension, as the lower bound and
15509 -- length of string literals are not stored like those of arrays.
15511 if Ekind (L_Typ) = E_String_Literal_Subtype then
15512 L_Low := String_Literal_Low_Bound (L_Typ);
15513 L_Len := String_Literal_Length (L_Typ);
15514 else
15515 L_Index := First_Index (L_Typ);
15516 Get_Index_Bounds (L_Index, L_Low, L_High);
15518 if Is_OK_Static_Expression (L_Low)
15519 and then
15520 Is_OK_Static_Expression (L_High)
15521 then
15522 if Expr_Value (L_High) < Expr_Value (L_Low) then
15523 L_Len := Uint_0;
15524 else
15525 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
15526 end if;
15527 else
15528 return False;
15529 end if;
15530 end if;
15532 if Ekind (R_Typ) = E_String_Literal_Subtype then
15533 R_Low := String_Literal_Low_Bound (R_Typ);
15534 R_Len := String_Literal_Length (R_Typ);
15535 else
15536 R_Index := First_Index (R_Typ);
15537 Get_Index_Bounds (R_Index, R_Low, R_High);
15539 if Is_OK_Static_Expression (R_Low)
15540 and then
15541 Is_OK_Static_Expression (R_High)
15542 then
15543 if Expr_Value (R_High) < Expr_Value (R_Low) then
15544 R_Len := Uint_0;
15545 else
15546 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
15547 end if;
15548 else
15549 return False;
15550 end if;
15551 end if;
15553 if (Is_OK_Static_Expression (L_Low)
15554 and then
15555 Is_OK_Static_Expression (R_Low))
15556 and then Expr_Value (L_Low) = Expr_Value (R_Low)
15557 and then L_Len = R_Len
15558 then
15559 null;
15560 else
15561 return False;
15562 end if;
15564 -- Then treat all other dimensions
15566 for Indx in 2 .. L_Ndims loop
15567 Next (L_Index);
15568 Next (R_Index);
15570 Get_Index_Bounds (L_Index, L_Low, L_High);
15571 Get_Index_Bounds (R_Index, R_Low, R_High);
15573 if (Is_OK_Static_Expression (L_Low) and then
15574 Is_OK_Static_Expression (L_High) and then
15575 Is_OK_Static_Expression (R_Low) and then
15576 Is_OK_Static_Expression (R_High))
15577 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
15578 and then
15579 Expr_Value (L_High) = Expr_Value (R_High))
15580 then
15581 null;
15582 else
15583 return False;
15584 end if;
15585 end loop;
15587 -- If we fall through the loop, all indexes matched
15589 return True;
15590 end Matching_Static_Array_Bounds;
15592 -------------------
15593 -- May_Be_Lvalue --
15594 -------------------
15596 function May_Be_Lvalue (N : Node_Id) return Boolean is
15597 P : constant Node_Id := Parent (N);
15599 begin
15600 case Nkind (P) is
15602 -- Test left side of assignment
15604 when N_Assignment_Statement =>
15605 return N = Name (P);
15607 -- Test prefix of component or attribute. Note that the prefix of an
15608 -- explicit or implicit dereference cannot be an l-value. In the case
15609 -- of a 'Read attribute, the reference can be an actual in the
15610 -- argument list of the attribute.
15612 when N_Attribute_Reference =>
15613 return (N = Prefix (P)
15614 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P)))
15615 or else
15616 Attribute_Name (P) = Name_Read;
15618 -- For an expanded name, the name is an lvalue if the expanded name
15619 -- is an lvalue, but the prefix is never an lvalue, since it is just
15620 -- the scope where the name is found.
15622 when N_Expanded_Name =>
15623 if N = Prefix (P) then
15624 return May_Be_Lvalue (P);
15625 else
15626 return False;
15627 end if;
15629 -- For a selected component A.B, A is certainly an lvalue if A.B is.
15630 -- B is a little interesting, if we have A.B := 3, there is some
15631 -- discussion as to whether B is an lvalue or not, we choose to say
15632 -- it is. Note however that A is not an lvalue if it is of an access
15633 -- type since this is an implicit dereference.
15635 when N_Selected_Component =>
15636 if N = Prefix (P)
15637 and then Present (Etype (N))
15638 and then Is_Access_Type (Etype (N))
15639 then
15640 return False;
15641 else
15642 return May_Be_Lvalue (P);
15643 end if;
15645 -- For an indexed component or slice, the index or slice bounds is
15646 -- never an lvalue. The prefix is an lvalue if the indexed component
15647 -- or slice is an lvalue, except if it is an access type, where we
15648 -- have an implicit dereference.
15650 when N_Indexed_Component | N_Slice =>
15651 if N /= Prefix (P)
15652 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
15653 then
15654 return False;
15655 else
15656 return May_Be_Lvalue (P);
15657 end if;
15659 -- Prefix of a reference is an lvalue if the reference is an lvalue
15661 when N_Reference =>
15662 return May_Be_Lvalue (P);
15664 -- Prefix of explicit dereference is never an lvalue
15666 when N_Explicit_Dereference =>
15667 return False;
15669 -- Positional parameter for subprogram, entry, or accept call.
15670 -- In older versions of Ada function call arguments are never
15671 -- lvalues. In Ada 2012 functions can have in-out parameters.
15673 when N_Subprogram_Call |
15674 N_Entry_Call_Statement |
15675 N_Accept_Statement
15677 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
15678 return False;
15679 end if;
15681 -- The following mechanism is clumsy and fragile. A single flag
15682 -- set in Resolve_Actuals would be preferable ???
15684 declare
15685 Proc : Entity_Id;
15686 Form : Entity_Id;
15687 Act : Node_Id;
15689 begin
15690 Proc := Get_Subprogram_Entity (P);
15692 if No (Proc) then
15693 return True;
15694 end if;
15696 -- If we are not a list member, something is strange, so be
15697 -- conservative and return True.
15699 if not Is_List_Member (N) then
15700 return True;
15701 end if;
15703 -- We are going to find the right formal by stepping forward
15704 -- through the formals, as we step backwards in the actuals.
15706 Form := First_Formal (Proc);
15707 Act := N;
15708 loop
15709 -- If no formal, something is weird, so be conservative and
15710 -- return True.
15712 if No (Form) then
15713 return True;
15714 end if;
15716 Prev (Act);
15717 exit when No (Act);
15718 Next_Formal (Form);
15719 end loop;
15721 return Ekind (Form) /= E_In_Parameter;
15722 end;
15724 -- Named parameter for procedure or accept call
15726 when N_Parameter_Association =>
15727 declare
15728 Proc : Entity_Id;
15729 Form : Entity_Id;
15731 begin
15732 Proc := Get_Subprogram_Entity (Parent (P));
15734 if No (Proc) then
15735 return True;
15736 end if;
15738 -- Loop through formals to find the one that matches
15740 Form := First_Formal (Proc);
15741 loop
15742 -- If no matching formal, that's peculiar, some kind of
15743 -- previous error, so return True to be conservative.
15744 -- Actually happens with legal code for an unresolved call
15745 -- where we may get the wrong homonym???
15747 if No (Form) then
15748 return True;
15749 end if;
15751 -- Else test for match
15753 if Chars (Form) = Chars (Selector_Name (P)) then
15754 return Ekind (Form) /= E_In_Parameter;
15755 end if;
15757 Next_Formal (Form);
15758 end loop;
15759 end;
15761 -- Test for appearing in a conversion that itself appears in an
15762 -- lvalue context, since this should be an lvalue.
15764 when N_Type_Conversion =>
15765 return May_Be_Lvalue (P);
15767 -- Test for appearance in object renaming declaration
15769 when N_Object_Renaming_Declaration =>
15770 return True;
15772 -- All other references are definitely not lvalues
15774 when others =>
15775 return False;
15777 end case;
15778 end May_Be_Lvalue;
15780 -----------------------
15781 -- Mark_Coextensions --
15782 -----------------------
15784 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
15785 Is_Dynamic : Boolean;
15786 -- Indicates whether the context causes nested coextensions to be
15787 -- dynamic or static
15789 function Mark_Allocator (N : Node_Id) return Traverse_Result;
15790 -- Recognize an allocator node and label it as a dynamic coextension
15792 --------------------
15793 -- Mark_Allocator --
15794 --------------------
15796 function Mark_Allocator (N : Node_Id) return Traverse_Result is
15797 begin
15798 if Nkind (N) = N_Allocator then
15799 if Is_Dynamic then
15800 Set_Is_Dynamic_Coextension (N);
15802 -- If the allocator expression is potentially dynamic, it may
15803 -- be expanded out of order and require dynamic allocation
15804 -- anyway, so we treat the coextension itself as dynamic.
15805 -- Potential optimization ???
15807 elsif Nkind (Expression (N)) = N_Qualified_Expression
15808 and then Nkind (Expression (Expression (N))) = N_Op_Concat
15809 then
15810 Set_Is_Dynamic_Coextension (N);
15811 else
15812 Set_Is_Static_Coextension (N);
15813 end if;
15814 end if;
15816 return OK;
15817 end Mark_Allocator;
15819 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
15821 -- Start of processing for Mark_Coextensions
15823 begin
15824 -- An allocator that appears on the right-hand side of an assignment is
15825 -- treated as a potentially dynamic coextension when the right-hand side
15826 -- is an allocator or a qualified expression.
15828 -- Obj := new ...'(new Coextension ...);
15830 if Nkind (Context_Nod) = N_Assignment_Statement then
15831 Is_Dynamic :=
15832 Nkind_In (Expression (Context_Nod), N_Allocator,
15833 N_Qualified_Expression);
15835 -- An allocator that appears within the expression of a simple return
15836 -- statement is treated as a potentially dynamic coextension when the
15837 -- expression is either aggregate, allocator, or qualified expression.
15839 -- return (new Coextension ...);
15840 -- return new ...'(new Coextension ...);
15842 elsif Nkind (Context_Nod) = N_Simple_Return_Statement then
15843 Is_Dynamic :=
15844 Nkind_In (Expression (Context_Nod), N_Aggregate,
15845 N_Allocator,
15846 N_Qualified_Expression);
15848 -- An alloctor that appears within the initialization expression of an
15849 -- object declaration is considered a potentially dynamic coextension
15850 -- when the initialization expression is an allocator or a qualified
15851 -- expression.
15853 -- Obj : ... := new ...'(new Coextension ...);
15855 -- A similar case arises when the object declaration is part of an
15856 -- extended return statement.
15858 -- return Obj : ... := new ...'(new Coextension ...);
15859 -- return Obj : ... := (new Coextension ...);
15861 elsif Nkind (Context_Nod) = N_Object_Declaration then
15862 Is_Dynamic :=
15863 Nkind_In (Root_Nod, N_Allocator, N_Qualified_Expression)
15864 or else
15865 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
15867 -- This routine should not be called with constructs that cannot contain
15868 -- coextensions.
15870 else
15871 raise Program_Error;
15872 end if;
15874 Mark_Allocators (Root_Nod);
15875 end Mark_Coextensions;
15877 ----------------------
15878 -- Needs_One_Actual --
15879 ----------------------
15881 function Needs_One_Actual (E : Entity_Id) return Boolean is
15882 Formal : Entity_Id;
15884 begin
15885 -- Ada 2005 or later, and formals present
15887 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
15888 Formal := Next_Formal (First_Formal (E));
15889 while Present (Formal) loop
15890 if No (Default_Value (Formal)) then
15891 return False;
15892 end if;
15894 Next_Formal (Formal);
15895 end loop;
15897 return True;
15899 -- Ada 83/95 or no formals
15901 else
15902 return False;
15903 end if;
15904 end Needs_One_Actual;
15906 ------------------------
15907 -- New_Copy_List_Tree --
15908 ------------------------
15910 function New_Copy_List_Tree (List : List_Id) return List_Id is
15911 NL : List_Id;
15912 E : Node_Id;
15914 begin
15915 if List = No_List then
15916 return No_List;
15918 else
15919 NL := New_List;
15920 E := First (List);
15922 while Present (E) loop
15923 Append (New_Copy_Tree (E), NL);
15924 E := Next (E);
15925 end loop;
15927 return NL;
15928 end if;
15929 end New_Copy_List_Tree;
15931 --------------------------------------------------
15932 -- New_Copy_Tree Auxiliary Data and Subprograms --
15933 --------------------------------------------------
15935 use Atree.Unchecked_Access;
15936 use Atree_Private_Part;
15938 -- Our approach here requires a two pass traversal of the tree. The
15939 -- first pass visits all nodes that eventually will be copied looking
15940 -- for defining Itypes. If any defining Itypes are found, then they are
15941 -- copied, and an entry is added to the replacement map. In the second
15942 -- phase, the tree is copied, using the replacement map to replace any
15943 -- Itype references within the copied tree.
15945 -- The following hash tables are used if the Map supplied has more
15946 -- than hash threshold entries to speed up access to the map. If
15947 -- there are fewer entries, then the map is searched sequentially
15948 -- (because setting up a hash table for only a few entries takes
15949 -- more time than it saves.
15951 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
15952 -- Hash function used for hash operations
15954 -------------------
15955 -- New_Copy_Hash --
15956 -------------------
15958 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
15959 begin
15960 return Nat (E) mod (NCT_Header_Num'Last + 1);
15961 end New_Copy_Hash;
15963 ---------------
15964 -- NCT_Assoc --
15965 ---------------
15967 -- The hash table NCT_Assoc associates old entities in the table
15968 -- with their corresponding new entities (i.e. the pairs of entries
15969 -- presented in the original Map argument are Key-Element pairs).
15971 package NCT_Assoc is new Simple_HTable (
15972 Header_Num => NCT_Header_Num,
15973 Element => Entity_Id,
15974 No_Element => Empty,
15975 Key => Entity_Id,
15976 Hash => New_Copy_Hash,
15977 Equal => Types."=");
15979 ---------------------
15980 -- NCT_Itype_Assoc --
15981 ---------------------
15983 -- The hash table NCT_Itype_Assoc contains entries only for those
15984 -- old nodes which have a non-empty Associated_Node_For_Itype set.
15985 -- The key is the associated node, and the element is the new node
15986 -- itself (NOT the associated node for the new node).
15988 package NCT_Itype_Assoc is new Simple_HTable (
15989 Header_Num => NCT_Header_Num,
15990 Element => Entity_Id,
15991 No_Element => Empty,
15992 Key => Entity_Id,
15993 Hash => New_Copy_Hash,
15994 Equal => Types."=");
15996 -------------------
15997 -- New_Copy_Tree --
15998 -------------------
16000 function New_Copy_Tree
16001 (Source : Node_Id;
16002 Map : Elist_Id := No_Elist;
16003 New_Sloc : Source_Ptr := No_Location;
16004 New_Scope : Entity_Id := Empty) return Node_Id
16006 Actual_Map : Elist_Id := Map;
16007 -- This is the actual map for the copy. It is initialized with the
16008 -- given elements, and then enlarged as required for Itypes that are
16009 -- copied during the first phase of the copy operation. The visit
16010 -- procedures add elements to this map as Itypes are encountered.
16011 -- The reason we cannot use Map directly, is that it may well be
16012 -- (and normally is) initialized to No_Elist, and if we have mapped
16013 -- entities, we have to reset it to point to a real Elist.
16015 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
16016 -- Called during second phase to map entities into their corresponding
16017 -- copies using Actual_Map. If the argument is not an entity, or is not
16018 -- in Actual_Map, then it is returned unchanged.
16020 procedure Build_NCT_Hash_Tables;
16021 -- Builds hash tables (number of elements >= threshold value)
16023 function Copy_Elist_With_Replacement
16024 (Old_Elist : Elist_Id) return Elist_Id;
16025 -- Called during second phase to copy element list doing replacements
16027 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
16028 -- Called during the second phase to process a copied Itype. The actual
16029 -- copy happened during the first phase (so that we could make the entry
16030 -- in the mapping), but we still have to deal with the descendants of
16031 -- the copied Itype and copy them where necessary.
16033 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
16034 -- Called during second phase to copy list doing replacements
16036 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
16037 -- Called during second phase to copy node doing replacements
16039 procedure Visit_Elist (E : Elist_Id);
16040 -- Called during first phase to visit all elements of an Elist
16042 procedure Visit_Field (F : Union_Id; N : Node_Id);
16043 -- Visit a single field, recursing to call Visit_Node or Visit_List
16044 -- if the field is a syntactic descendant of the current node (i.e.
16045 -- its parent is Node N).
16047 procedure Visit_Itype (Old_Itype : Entity_Id);
16048 -- Called during first phase to visit subsidiary fields of a defining
16049 -- Itype, and also create a copy and make an entry in the replacement
16050 -- map for the new copy.
16052 procedure Visit_List (L : List_Id);
16053 -- Called during first phase to visit all elements of a List
16055 procedure Visit_Node (N : Node_Or_Entity_Id);
16056 -- Called during first phase to visit a node and all its subtrees
16058 -----------
16059 -- Assoc --
16060 -----------
16062 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
16063 E : Elmt_Id;
16064 Ent : Entity_Id;
16066 begin
16067 if not Has_Extension (N) or else No (Actual_Map) then
16068 return N;
16070 elsif NCT_Hash_Tables_Used then
16071 Ent := NCT_Assoc.Get (Entity_Id (N));
16073 if Present (Ent) then
16074 return Ent;
16075 else
16076 return N;
16077 end if;
16079 -- No hash table used, do serial search
16081 else
16082 E := First_Elmt (Actual_Map);
16083 while Present (E) loop
16084 if Node (E) = N then
16085 return Node (Next_Elmt (E));
16086 else
16087 E := Next_Elmt (Next_Elmt (E));
16088 end if;
16089 end loop;
16090 end if;
16092 return N;
16093 end Assoc;
16095 ---------------------------
16096 -- Build_NCT_Hash_Tables --
16097 ---------------------------
16099 procedure Build_NCT_Hash_Tables is
16100 Elmt : Elmt_Id;
16101 Ent : Entity_Id;
16102 begin
16103 if NCT_Hash_Table_Setup then
16104 NCT_Assoc.Reset;
16105 NCT_Itype_Assoc.Reset;
16106 end if;
16108 Elmt := First_Elmt (Actual_Map);
16109 while Present (Elmt) loop
16110 Ent := Node (Elmt);
16112 -- Get new entity, and associate old and new
16114 Next_Elmt (Elmt);
16115 NCT_Assoc.Set (Ent, Node (Elmt));
16117 if Is_Type (Ent) then
16118 declare
16119 Anode : constant Entity_Id :=
16120 Associated_Node_For_Itype (Ent);
16122 begin
16123 if Present (Anode) then
16125 -- Enter a link between the associated node of the
16126 -- old Itype and the new Itype, for updating later
16127 -- when node is copied.
16129 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
16130 end if;
16131 end;
16132 end if;
16134 Next_Elmt (Elmt);
16135 end loop;
16137 NCT_Hash_Tables_Used := True;
16138 NCT_Hash_Table_Setup := True;
16139 end Build_NCT_Hash_Tables;
16141 ---------------------------------
16142 -- Copy_Elist_With_Replacement --
16143 ---------------------------------
16145 function Copy_Elist_With_Replacement
16146 (Old_Elist : Elist_Id) return Elist_Id
16148 M : Elmt_Id;
16149 New_Elist : Elist_Id;
16151 begin
16152 if No (Old_Elist) then
16153 return No_Elist;
16155 else
16156 New_Elist := New_Elmt_List;
16158 M := First_Elmt (Old_Elist);
16159 while Present (M) loop
16160 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
16161 Next_Elmt (M);
16162 end loop;
16163 end if;
16165 return New_Elist;
16166 end Copy_Elist_With_Replacement;
16168 ---------------------------------
16169 -- Copy_Itype_With_Replacement --
16170 ---------------------------------
16172 -- This routine exactly parallels its phase one analog Visit_Itype,
16174 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
16175 begin
16176 -- Translate Next_Entity, Scope, and Etype fields, in case they
16177 -- reference entities that have been mapped into copies.
16179 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
16180 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
16182 if Present (New_Scope) then
16183 Set_Scope (New_Itype, New_Scope);
16184 else
16185 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
16186 end if;
16188 -- Copy referenced fields
16190 if Is_Discrete_Type (New_Itype) then
16191 Set_Scalar_Range (New_Itype,
16192 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
16194 elsif Has_Discriminants (Base_Type (New_Itype)) then
16195 Set_Discriminant_Constraint (New_Itype,
16196 Copy_Elist_With_Replacement
16197 (Discriminant_Constraint (New_Itype)));
16199 elsif Is_Array_Type (New_Itype) then
16200 if Present (First_Index (New_Itype)) then
16201 Set_First_Index (New_Itype,
16202 First (Copy_List_With_Replacement
16203 (List_Containing (First_Index (New_Itype)))));
16204 end if;
16206 if Is_Packed (New_Itype) then
16207 Set_Packed_Array_Impl_Type (New_Itype,
16208 Copy_Node_With_Replacement
16209 (Packed_Array_Impl_Type (New_Itype)));
16210 end if;
16211 end if;
16212 end Copy_Itype_With_Replacement;
16214 --------------------------------
16215 -- Copy_List_With_Replacement --
16216 --------------------------------
16218 function Copy_List_With_Replacement
16219 (Old_List : List_Id) return List_Id
16221 New_List : List_Id;
16222 E : Node_Id;
16224 begin
16225 if Old_List = No_List then
16226 return No_List;
16228 else
16229 New_List := Empty_List;
16231 E := First (Old_List);
16232 while Present (E) loop
16233 Append (Copy_Node_With_Replacement (E), New_List);
16234 Next (E);
16235 end loop;
16237 return New_List;
16238 end if;
16239 end Copy_List_With_Replacement;
16241 --------------------------------
16242 -- Copy_Node_With_Replacement --
16243 --------------------------------
16245 function Copy_Node_With_Replacement
16246 (Old_Node : Node_Id) return Node_Id
16248 New_Node : Node_Id;
16250 procedure Adjust_Named_Associations
16251 (Old_Node : Node_Id;
16252 New_Node : Node_Id);
16253 -- If a call node has named associations, these are chained through
16254 -- the First_Named_Actual, Next_Named_Actual links. These must be
16255 -- propagated separately to the new parameter list, because these
16256 -- are not syntactic fields.
16258 function Copy_Field_With_Replacement
16259 (Field : Union_Id) return Union_Id;
16260 -- Given Field, which is a field of Old_Node, return a copy of it
16261 -- if it is a syntactic field (i.e. its parent is Node), setting
16262 -- the parent of the copy to poit to New_Node. Otherwise returns
16263 -- the field (possibly mapped if it is an entity).
16265 -------------------------------
16266 -- Adjust_Named_Associations --
16267 -------------------------------
16269 procedure Adjust_Named_Associations
16270 (Old_Node : Node_Id;
16271 New_Node : Node_Id)
16273 Old_E : Node_Id;
16274 New_E : Node_Id;
16276 Old_Next : Node_Id;
16277 New_Next : Node_Id;
16279 begin
16280 Old_E := First (Parameter_Associations (Old_Node));
16281 New_E := First (Parameter_Associations (New_Node));
16282 while Present (Old_E) loop
16283 if Nkind (Old_E) = N_Parameter_Association
16284 and then Present (Next_Named_Actual (Old_E))
16285 then
16286 if First_Named_Actual (Old_Node)
16287 = Explicit_Actual_Parameter (Old_E)
16288 then
16289 Set_First_Named_Actual
16290 (New_Node, Explicit_Actual_Parameter (New_E));
16291 end if;
16293 -- Now scan parameter list from the beginning,to locate
16294 -- next named actual, which can be out of order.
16296 Old_Next := First (Parameter_Associations (Old_Node));
16297 New_Next := First (Parameter_Associations (New_Node));
16299 while Nkind (Old_Next) /= N_Parameter_Association
16300 or else Explicit_Actual_Parameter (Old_Next) /=
16301 Next_Named_Actual (Old_E)
16302 loop
16303 Next (Old_Next);
16304 Next (New_Next);
16305 end loop;
16307 Set_Next_Named_Actual
16308 (New_E, Explicit_Actual_Parameter (New_Next));
16309 end if;
16311 Next (Old_E);
16312 Next (New_E);
16313 end loop;
16314 end Adjust_Named_Associations;
16316 ---------------------------------
16317 -- Copy_Field_With_Replacement --
16318 ---------------------------------
16320 function Copy_Field_With_Replacement
16321 (Field : Union_Id) return Union_Id
16323 begin
16324 if Field = Union_Id (Empty) then
16325 return Field;
16327 elsif Field in Node_Range then
16328 declare
16329 Old_N : constant Node_Id := Node_Id (Field);
16330 New_N : Node_Id;
16332 begin
16333 -- If syntactic field, as indicated by the parent pointer
16334 -- being set, then copy the referenced node recursively.
16336 if Parent (Old_N) = Old_Node then
16337 New_N := Copy_Node_With_Replacement (Old_N);
16339 if New_N /= Old_N then
16340 Set_Parent (New_N, New_Node);
16341 end if;
16343 -- For semantic fields, update possible entity reference
16344 -- from the replacement map.
16346 else
16347 New_N := Assoc (Old_N);
16348 end if;
16350 return Union_Id (New_N);
16351 end;
16353 elsif Field in List_Range then
16354 declare
16355 Old_L : constant List_Id := List_Id (Field);
16356 New_L : List_Id;
16358 begin
16359 -- If syntactic field, as indicated by the parent pointer,
16360 -- then recursively copy the entire referenced list.
16362 if Parent (Old_L) = Old_Node then
16363 New_L := Copy_List_With_Replacement (Old_L);
16364 Set_Parent (New_L, New_Node);
16366 -- For semantic list, just returned unchanged
16368 else
16369 New_L := Old_L;
16370 end if;
16372 return Union_Id (New_L);
16373 end;
16375 -- Anything other than a list or a node is returned unchanged
16377 else
16378 return Field;
16379 end if;
16380 end Copy_Field_With_Replacement;
16382 -- Start of processing for Copy_Node_With_Replacement
16384 begin
16385 if Old_Node <= Empty_Or_Error then
16386 return Old_Node;
16388 elsif Has_Extension (Old_Node) then
16389 return Assoc (Old_Node);
16391 else
16392 New_Node := New_Copy (Old_Node);
16394 -- If the node we are copying is the associated node of a
16395 -- previously copied Itype, then adjust the associated node
16396 -- of the copy of that Itype accordingly.
16398 if Present (Actual_Map) then
16399 declare
16400 E : Elmt_Id;
16401 Ent : Entity_Id;
16403 begin
16404 -- Case of hash table used
16406 if NCT_Hash_Tables_Used then
16407 Ent := NCT_Itype_Assoc.Get (Old_Node);
16409 if Present (Ent) then
16410 Set_Associated_Node_For_Itype (Ent, New_Node);
16411 end if;
16413 -- Case of no hash table used
16415 else
16416 E := First_Elmt (Actual_Map);
16417 while Present (E) loop
16418 if Is_Itype (Node (E))
16419 and then
16420 Old_Node = Associated_Node_For_Itype (Node (E))
16421 then
16422 Set_Associated_Node_For_Itype
16423 (Node (Next_Elmt (E)), New_Node);
16424 end if;
16426 E := Next_Elmt (Next_Elmt (E));
16427 end loop;
16428 end if;
16429 end;
16430 end if;
16432 -- Recursively copy descendants
16434 Set_Field1
16435 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
16436 Set_Field2
16437 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
16438 Set_Field3
16439 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
16440 Set_Field4
16441 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
16442 Set_Field5
16443 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
16445 -- Adjust Sloc of new node if necessary
16447 if New_Sloc /= No_Location then
16448 Set_Sloc (New_Node, New_Sloc);
16450 -- If we adjust the Sloc, then we are essentially making a
16451 -- completely new node, so the Comes_From_Source flag should
16452 -- be reset to the proper default value.
16454 Set_Comes_From_Source
16455 (New_Node, Default_Node.Comes_From_Source);
16456 end if;
16458 -- If the node is a call and has named associations, set the
16459 -- corresponding links in the copy.
16461 if Nkind_In (Old_Node, N_Entry_Call_Statement,
16462 N_Function_Call,
16463 N_Procedure_Call_Statement)
16464 and then Present (First_Named_Actual (Old_Node))
16465 then
16466 Adjust_Named_Associations (Old_Node, New_Node);
16467 end if;
16469 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
16470 -- The replacement mechanism applies to entities, and is not used
16471 -- here. Eventually we may need a more general graph-copying
16472 -- routine. For now, do a sequential search to find desired node.
16474 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
16475 and then Present (First_Real_Statement (Old_Node))
16476 then
16477 declare
16478 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
16479 N1, N2 : Node_Id;
16481 begin
16482 N1 := First (Statements (Old_Node));
16483 N2 := First (Statements (New_Node));
16485 while N1 /= Old_F loop
16486 Next (N1);
16487 Next (N2);
16488 end loop;
16490 Set_First_Real_Statement (New_Node, N2);
16491 end;
16492 end if;
16493 end if;
16495 -- All done, return copied node
16497 return New_Node;
16498 end Copy_Node_With_Replacement;
16500 -----------------
16501 -- Visit_Elist --
16502 -----------------
16504 procedure Visit_Elist (E : Elist_Id) is
16505 Elmt : Elmt_Id;
16506 begin
16507 if Present (E) then
16508 Elmt := First_Elmt (E);
16510 while Elmt /= No_Elmt loop
16511 Visit_Node (Node (Elmt));
16512 Next_Elmt (Elmt);
16513 end loop;
16514 end if;
16515 end Visit_Elist;
16517 -----------------
16518 -- Visit_Field --
16519 -----------------
16521 procedure Visit_Field (F : Union_Id; N : Node_Id) is
16522 begin
16523 if F = Union_Id (Empty) then
16524 return;
16526 elsif F in Node_Range then
16528 -- Copy node if it is syntactic, i.e. its parent pointer is
16529 -- set to point to the field that referenced it (certain
16530 -- Itypes will also meet this criterion, which is fine, since
16531 -- these are clearly Itypes that do need to be copied, since
16532 -- we are copying their parent.)
16534 if Parent (Node_Id (F)) = N then
16535 Visit_Node (Node_Id (F));
16536 return;
16538 -- Another case, if we are pointing to an Itype, then we want
16539 -- to copy it if its associated node is somewhere in the tree
16540 -- being copied.
16542 -- Note: the exclusion of self-referential copies is just an
16543 -- optimization, since the search of the already copied list
16544 -- would catch it, but it is a common case (Etype pointing
16545 -- to itself for an Itype that is a base type).
16547 elsif Has_Extension (Node_Id (F))
16548 and then Is_Itype (Entity_Id (F))
16549 and then Node_Id (F) /= N
16550 then
16551 declare
16552 P : Node_Id;
16554 begin
16555 P := Associated_Node_For_Itype (Node_Id (F));
16556 while Present (P) loop
16557 if P = Source then
16558 Visit_Node (Node_Id (F));
16559 return;
16560 else
16561 P := Parent (P);
16562 end if;
16563 end loop;
16565 -- An Itype whose parent is not being copied definitely
16566 -- should NOT be copied, since it does not belong in any
16567 -- sense to the copied subtree.
16569 return;
16570 end;
16571 end if;
16573 elsif F in List_Range and then Parent (List_Id (F)) = N then
16574 Visit_List (List_Id (F));
16575 return;
16576 end if;
16577 end Visit_Field;
16579 -----------------
16580 -- Visit_Itype --
16581 -----------------
16583 procedure Visit_Itype (Old_Itype : Entity_Id) is
16584 New_Itype : Entity_Id;
16585 E : Elmt_Id;
16586 Ent : Entity_Id;
16588 begin
16589 -- Itypes that describe the designated type of access to subprograms
16590 -- have the structure of subprogram declarations, with signatures,
16591 -- etc. Either we duplicate the signatures completely, or choose to
16592 -- share such itypes, which is fine because their elaboration will
16593 -- have no side effects.
16595 if Ekind (Old_Itype) = E_Subprogram_Type then
16596 return;
16597 end if;
16599 New_Itype := New_Copy (Old_Itype);
16601 -- The new Itype has all the attributes of the old one, and
16602 -- we just copy the contents of the entity. However, the back-end
16603 -- needs different names for debugging purposes, so we create a
16604 -- new internal name for it in all cases.
16606 Set_Chars (New_Itype, New_Internal_Name ('T'));
16608 -- If our associated node is an entity that has already been copied,
16609 -- then set the associated node of the copy to point to the right
16610 -- copy. If we have copied an Itype that is itself the associated
16611 -- node of some previously copied Itype, then we set the right
16612 -- pointer in the other direction.
16614 if Present (Actual_Map) then
16616 -- Case of hash tables used
16618 if NCT_Hash_Tables_Used then
16620 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
16622 if Present (Ent) then
16623 Set_Associated_Node_For_Itype (New_Itype, Ent);
16624 end if;
16626 Ent := NCT_Itype_Assoc.Get (Old_Itype);
16627 if Present (Ent) then
16628 Set_Associated_Node_For_Itype (Ent, New_Itype);
16630 -- If the hash table has no association for this Itype and
16631 -- its associated node, enter one now.
16633 else
16634 NCT_Itype_Assoc.Set
16635 (Associated_Node_For_Itype (Old_Itype), New_Itype);
16636 end if;
16638 -- Case of hash tables not used
16640 else
16641 E := First_Elmt (Actual_Map);
16642 while Present (E) loop
16643 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
16644 Set_Associated_Node_For_Itype
16645 (New_Itype, Node (Next_Elmt (E)));
16646 end if;
16648 if Is_Type (Node (E))
16649 and then Old_Itype = Associated_Node_For_Itype (Node (E))
16650 then
16651 Set_Associated_Node_For_Itype
16652 (Node (Next_Elmt (E)), New_Itype);
16653 end if;
16655 E := Next_Elmt (Next_Elmt (E));
16656 end loop;
16657 end if;
16658 end if;
16660 if Present (Freeze_Node (New_Itype)) then
16661 Set_Is_Frozen (New_Itype, False);
16662 Set_Freeze_Node (New_Itype, Empty);
16663 end if;
16665 -- Add new association to map
16667 if No (Actual_Map) then
16668 Actual_Map := New_Elmt_List;
16669 end if;
16671 Append_Elmt (Old_Itype, Actual_Map);
16672 Append_Elmt (New_Itype, Actual_Map);
16674 if NCT_Hash_Tables_Used then
16675 NCT_Assoc.Set (Old_Itype, New_Itype);
16677 else
16678 NCT_Table_Entries := NCT_Table_Entries + 1;
16680 if NCT_Table_Entries > NCT_Hash_Threshold then
16681 Build_NCT_Hash_Tables;
16682 end if;
16683 end if;
16685 -- If a record subtype is simply copied, the entity list will be
16686 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
16688 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
16689 Set_Cloned_Subtype (New_Itype, Old_Itype);
16690 end if;
16692 -- Visit descendants that eventually get copied
16694 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
16696 if Is_Discrete_Type (Old_Itype) then
16697 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
16699 elsif Has_Discriminants (Base_Type (Old_Itype)) then
16700 -- ??? This should involve call to Visit_Field
16701 Visit_Elist (Discriminant_Constraint (Old_Itype));
16703 elsif Is_Array_Type (Old_Itype) then
16704 if Present (First_Index (Old_Itype)) then
16705 Visit_Field (Union_Id (List_Containing
16706 (First_Index (Old_Itype))),
16707 Old_Itype);
16708 end if;
16710 if Is_Packed (Old_Itype) then
16711 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
16712 Old_Itype);
16713 end if;
16714 end if;
16715 end Visit_Itype;
16717 ----------------
16718 -- Visit_List --
16719 ----------------
16721 procedure Visit_List (L : List_Id) is
16722 N : Node_Id;
16723 begin
16724 if L /= No_List then
16725 N := First (L);
16727 while Present (N) loop
16728 Visit_Node (N);
16729 Next (N);
16730 end loop;
16731 end if;
16732 end Visit_List;
16734 ----------------
16735 -- Visit_Node --
16736 ----------------
16738 procedure Visit_Node (N : Node_Or_Entity_Id) is
16740 -- Start of processing for Visit_Node
16742 begin
16743 -- Handle case of an Itype, which must be copied
16745 if Has_Extension (N) and then Is_Itype (N) then
16747 -- Nothing to do if already in the list. This can happen with an
16748 -- Itype entity that appears more than once in the tree.
16749 -- Note that we do not want to visit descendants in this case.
16751 -- Test for already in list when hash table is used
16753 if NCT_Hash_Tables_Used then
16754 if Present (NCT_Assoc.Get (Entity_Id (N))) then
16755 return;
16756 end if;
16758 -- Test for already in list when hash table not used
16760 else
16761 declare
16762 E : Elmt_Id;
16763 begin
16764 if Present (Actual_Map) then
16765 E := First_Elmt (Actual_Map);
16766 while Present (E) loop
16767 if Node (E) = N then
16768 return;
16769 else
16770 E := Next_Elmt (Next_Elmt (E));
16771 end if;
16772 end loop;
16773 end if;
16774 end;
16775 end if;
16777 Visit_Itype (N);
16778 end if;
16780 -- Visit descendants
16782 Visit_Field (Field1 (N), N);
16783 Visit_Field (Field2 (N), N);
16784 Visit_Field (Field3 (N), N);
16785 Visit_Field (Field4 (N), N);
16786 Visit_Field (Field5 (N), N);
16787 end Visit_Node;
16789 -- Start of processing for New_Copy_Tree
16791 begin
16792 Actual_Map := Map;
16794 -- See if we should use hash table
16796 if No (Actual_Map) then
16797 NCT_Hash_Tables_Used := False;
16799 else
16800 declare
16801 Elmt : Elmt_Id;
16803 begin
16804 NCT_Table_Entries := 0;
16806 Elmt := First_Elmt (Actual_Map);
16807 while Present (Elmt) loop
16808 NCT_Table_Entries := NCT_Table_Entries + 1;
16809 Next_Elmt (Elmt);
16810 Next_Elmt (Elmt);
16811 end loop;
16813 if NCT_Table_Entries > NCT_Hash_Threshold then
16814 Build_NCT_Hash_Tables;
16815 else
16816 NCT_Hash_Tables_Used := False;
16817 end if;
16818 end;
16819 end if;
16821 -- Hash table set up if required, now start phase one by visiting
16822 -- top node (we will recursively visit the descendants).
16824 Visit_Node (Source);
16826 -- Now the second phase of the copy can start. First we process
16827 -- all the mapped entities, copying their descendants.
16829 if Present (Actual_Map) then
16830 declare
16831 Elmt : Elmt_Id;
16832 New_Itype : Entity_Id;
16833 begin
16834 Elmt := First_Elmt (Actual_Map);
16835 while Present (Elmt) loop
16836 Next_Elmt (Elmt);
16837 New_Itype := Node (Elmt);
16839 if Is_Itype (New_Itype) then
16840 Copy_Itype_With_Replacement (New_Itype);
16841 end if;
16842 Next_Elmt (Elmt);
16843 end loop;
16844 end;
16845 end if;
16847 -- Now we can copy the actual tree
16849 return Copy_Node_With_Replacement (Source);
16850 end New_Copy_Tree;
16852 -------------------------
16853 -- New_External_Entity --
16854 -------------------------
16856 function New_External_Entity
16857 (Kind : Entity_Kind;
16858 Scope_Id : Entity_Id;
16859 Sloc_Value : Source_Ptr;
16860 Related_Id : Entity_Id;
16861 Suffix : Character;
16862 Suffix_Index : Nat := 0;
16863 Prefix : Character := ' ') return Entity_Id
16865 N : constant Entity_Id :=
16866 Make_Defining_Identifier (Sloc_Value,
16867 New_External_Name
16868 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
16870 begin
16871 Set_Ekind (N, Kind);
16872 Set_Is_Internal (N, True);
16873 Append_Entity (N, Scope_Id);
16874 Set_Public_Status (N);
16876 if Kind in Type_Kind then
16877 Init_Size_Align (N);
16878 end if;
16880 return N;
16881 end New_External_Entity;
16883 -------------------------
16884 -- New_Internal_Entity --
16885 -------------------------
16887 function New_Internal_Entity
16888 (Kind : Entity_Kind;
16889 Scope_Id : Entity_Id;
16890 Sloc_Value : Source_Ptr;
16891 Id_Char : Character) return Entity_Id
16893 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
16895 begin
16896 Set_Ekind (N, Kind);
16897 Set_Is_Internal (N, True);
16898 Append_Entity (N, Scope_Id);
16900 if Kind in Type_Kind then
16901 Init_Size_Align (N);
16902 end if;
16904 return N;
16905 end New_Internal_Entity;
16907 -----------------
16908 -- Next_Actual --
16909 -----------------
16911 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
16912 N : Node_Id;
16914 begin
16915 -- If we are pointing at a positional parameter, it is a member of a
16916 -- node list (the list of parameters), and the next parameter is the
16917 -- next node on the list, unless we hit a parameter association, then
16918 -- we shift to using the chain whose head is the First_Named_Actual in
16919 -- the parent, and then is threaded using the Next_Named_Actual of the
16920 -- Parameter_Association. All this fiddling is because the original node
16921 -- list is in the textual call order, and what we need is the
16922 -- declaration order.
16924 if Is_List_Member (Actual_Id) then
16925 N := Next (Actual_Id);
16927 if Nkind (N) = N_Parameter_Association then
16928 return First_Named_Actual (Parent (Actual_Id));
16929 else
16930 return N;
16931 end if;
16933 else
16934 return Next_Named_Actual (Parent (Actual_Id));
16935 end if;
16936 end Next_Actual;
16938 procedure Next_Actual (Actual_Id : in out Node_Id) is
16939 begin
16940 Actual_Id := Next_Actual (Actual_Id);
16941 end Next_Actual;
16943 -----------------------
16944 -- Normalize_Actuals --
16945 -----------------------
16947 -- Chain actuals according to formals of subprogram. If there are no named
16948 -- associations, the chain is simply the list of Parameter Associations,
16949 -- since the order is the same as the declaration order. If there are named
16950 -- associations, then the First_Named_Actual field in the N_Function_Call
16951 -- or N_Procedure_Call_Statement node points to the Parameter_Association
16952 -- node for the parameter that comes first in declaration order. The
16953 -- remaining named parameters are then chained in declaration order using
16954 -- Next_Named_Actual.
16956 -- This routine also verifies that the number of actuals is compatible with
16957 -- the number and default values of formals, but performs no type checking
16958 -- (type checking is done by the caller).
16960 -- If the matching succeeds, Success is set to True and the caller proceeds
16961 -- with type-checking. If the match is unsuccessful, then Success is set to
16962 -- False, and the caller attempts a different interpretation, if there is
16963 -- one.
16965 -- If the flag Report is on, the call is not overloaded, and a failure to
16966 -- match can be reported here, rather than in the caller.
16968 procedure Normalize_Actuals
16969 (N : Node_Id;
16970 S : Entity_Id;
16971 Report : Boolean;
16972 Success : out Boolean)
16974 Actuals : constant List_Id := Parameter_Associations (N);
16975 Actual : Node_Id := Empty;
16976 Formal : Entity_Id;
16977 Last : Node_Id := Empty;
16978 First_Named : Node_Id := Empty;
16979 Found : Boolean;
16981 Formals_To_Match : Integer := 0;
16982 Actuals_To_Match : Integer := 0;
16984 procedure Chain (A : Node_Id);
16985 -- Add named actual at the proper place in the list, using the
16986 -- Next_Named_Actual link.
16988 function Reporting return Boolean;
16989 -- Determines if an error is to be reported. To report an error, we
16990 -- need Report to be True, and also we do not report errors caused
16991 -- by calls to init procs that occur within other init procs. Such
16992 -- errors must always be cascaded errors, since if all the types are
16993 -- declared correctly, the compiler will certainly build decent calls.
16995 -----------
16996 -- Chain --
16997 -----------
16999 procedure Chain (A : Node_Id) is
17000 begin
17001 if No (Last) then
17003 -- Call node points to first actual in list
17005 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
17007 else
17008 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
17009 end if;
17011 Last := A;
17012 Set_Next_Named_Actual (Last, Empty);
17013 end Chain;
17015 ---------------
17016 -- Reporting --
17017 ---------------
17019 function Reporting return Boolean is
17020 begin
17021 if not Report then
17022 return False;
17024 elsif not Within_Init_Proc then
17025 return True;
17027 elsif Is_Init_Proc (Entity (Name (N))) then
17028 return False;
17030 else
17031 return True;
17032 end if;
17033 end Reporting;
17035 -- Start of processing for Normalize_Actuals
17037 begin
17038 if Is_Access_Type (S) then
17040 -- The name in the call is a function call that returns an access
17041 -- to subprogram. The designated type has the list of formals.
17043 Formal := First_Formal (Designated_Type (S));
17044 else
17045 Formal := First_Formal (S);
17046 end if;
17048 while Present (Formal) loop
17049 Formals_To_Match := Formals_To_Match + 1;
17050 Next_Formal (Formal);
17051 end loop;
17053 -- Find if there is a named association, and verify that no positional
17054 -- associations appear after named ones.
17056 if Present (Actuals) then
17057 Actual := First (Actuals);
17058 end if;
17060 while Present (Actual)
17061 and then Nkind (Actual) /= N_Parameter_Association
17062 loop
17063 Actuals_To_Match := Actuals_To_Match + 1;
17064 Next (Actual);
17065 end loop;
17067 if No (Actual) and Actuals_To_Match = Formals_To_Match then
17069 -- Most common case: positional notation, no defaults
17071 Success := True;
17072 return;
17074 elsif Actuals_To_Match > Formals_To_Match then
17076 -- Too many actuals: will not work
17078 if Reporting then
17079 if Is_Entity_Name (Name (N)) then
17080 Error_Msg_N ("too many arguments in call to&", Name (N));
17081 else
17082 Error_Msg_N ("too many arguments in call", N);
17083 end if;
17084 end if;
17086 Success := False;
17087 return;
17088 end if;
17090 First_Named := Actual;
17092 while Present (Actual) loop
17093 if Nkind (Actual) /= N_Parameter_Association then
17094 Error_Msg_N
17095 ("positional parameters not allowed after named ones", Actual);
17096 Success := False;
17097 return;
17099 else
17100 Actuals_To_Match := Actuals_To_Match + 1;
17101 end if;
17103 Next (Actual);
17104 end loop;
17106 if Present (Actuals) then
17107 Actual := First (Actuals);
17108 end if;
17110 Formal := First_Formal (S);
17111 while Present (Formal) loop
17113 -- Match the formals in order. If the corresponding actual is
17114 -- positional, nothing to do. Else scan the list of named actuals
17115 -- to find the one with the right name.
17117 if Present (Actual)
17118 and then Nkind (Actual) /= N_Parameter_Association
17119 then
17120 Next (Actual);
17121 Actuals_To_Match := Actuals_To_Match - 1;
17122 Formals_To_Match := Formals_To_Match - 1;
17124 else
17125 -- For named parameters, search the list of actuals to find
17126 -- one that matches the next formal name.
17128 Actual := First_Named;
17129 Found := False;
17130 while Present (Actual) loop
17131 if Chars (Selector_Name (Actual)) = Chars (Formal) then
17132 Found := True;
17133 Chain (Actual);
17134 Actuals_To_Match := Actuals_To_Match - 1;
17135 Formals_To_Match := Formals_To_Match - 1;
17136 exit;
17137 end if;
17139 Next (Actual);
17140 end loop;
17142 if not Found then
17143 if Ekind (Formal) /= E_In_Parameter
17144 or else No (Default_Value (Formal))
17145 then
17146 if Reporting then
17147 if (Comes_From_Source (S)
17148 or else Sloc (S) = Standard_Location)
17149 and then Is_Overloadable (S)
17150 then
17151 if No (Actuals)
17152 and then
17153 Nkind_In (Parent (N), N_Procedure_Call_Statement,
17154 N_Function_Call,
17155 N_Parameter_Association)
17156 and then Ekind (S) /= E_Function
17157 then
17158 Set_Etype (N, Etype (S));
17160 else
17161 Error_Msg_Name_1 := Chars (S);
17162 Error_Msg_Sloc := Sloc (S);
17163 Error_Msg_NE
17164 ("missing argument for parameter & "
17165 & "in call to % declared #", N, Formal);
17166 end if;
17168 elsif Is_Overloadable (S) then
17169 Error_Msg_Name_1 := Chars (S);
17171 -- Point to type derivation that generated the
17172 -- operation.
17174 Error_Msg_Sloc := Sloc (Parent (S));
17176 Error_Msg_NE
17177 ("missing argument for parameter & "
17178 & "in call to % (inherited) #", N, Formal);
17180 else
17181 Error_Msg_NE
17182 ("missing argument for parameter &", N, Formal);
17183 end if;
17184 end if;
17186 Success := False;
17187 return;
17189 else
17190 Formals_To_Match := Formals_To_Match - 1;
17191 end if;
17192 end if;
17193 end if;
17195 Next_Formal (Formal);
17196 end loop;
17198 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
17199 Success := True;
17200 return;
17202 else
17203 if Reporting then
17205 -- Find some superfluous named actual that did not get
17206 -- attached to the list of associations.
17208 Actual := First (Actuals);
17209 while Present (Actual) loop
17210 if Nkind (Actual) = N_Parameter_Association
17211 and then Actual /= Last
17212 and then No (Next_Named_Actual (Actual))
17213 then
17214 -- A validity check may introduce a copy of a call that
17215 -- includes an extra actual (for example for an unrelated
17216 -- accessibility check). Check that the extra actual matches
17217 -- some extra formal, which must exist already because
17218 -- subprogram must be frozen at this point.
17220 if Present (Extra_Formals (S))
17221 and then not Comes_From_Source (Actual)
17222 and then Nkind (Actual) = N_Parameter_Association
17223 and then Chars (Extra_Formals (S)) =
17224 Chars (Selector_Name (Actual))
17225 then
17226 null;
17227 else
17228 Error_Msg_N
17229 ("unmatched actual & in call", Selector_Name (Actual));
17230 exit;
17231 end if;
17232 end if;
17234 Next (Actual);
17235 end loop;
17236 end if;
17238 Success := False;
17239 return;
17240 end if;
17241 end Normalize_Actuals;
17243 --------------------------------
17244 -- Note_Possible_Modification --
17245 --------------------------------
17247 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
17248 Modification_Comes_From_Source : constant Boolean :=
17249 Comes_From_Source (Parent (N));
17251 Ent : Entity_Id;
17252 Exp : Node_Id;
17254 begin
17255 -- Loop to find referenced entity, if there is one
17257 Exp := N;
17258 loop
17259 Ent := Empty;
17261 if Is_Entity_Name (Exp) then
17262 Ent := Entity (Exp);
17264 -- If the entity is missing, it is an undeclared identifier,
17265 -- and there is nothing to annotate.
17267 if No (Ent) then
17268 return;
17269 end if;
17271 elsif Nkind (Exp) = N_Explicit_Dereference then
17272 declare
17273 P : constant Node_Id := Prefix (Exp);
17275 begin
17276 -- In formal verification mode, keep track of all reads and
17277 -- writes through explicit dereferences.
17279 if GNATprove_Mode then
17280 SPARK_Specific.Generate_Dereference (N, 'm');
17281 end if;
17283 if Nkind (P) = N_Selected_Component
17284 and then Present (Entry_Formal (Entity (Selector_Name (P))))
17285 then
17286 -- Case of a reference to an entry formal
17288 Ent := Entry_Formal (Entity (Selector_Name (P)));
17290 elsif Nkind (P) = N_Identifier
17291 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
17292 and then Present (Expression (Parent (Entity (P))))
17293 and then Nkind (Expression (Parent (Entity (P)))) =
17294 N_Reference
17295 then
17296 -- Case of a reference to a value on which side effects have
17297 -- been removed.
17299 Exp := Prefix (Expression (Parent (Entity (P))));
17300 goto Continue;
17302 else
17303 return;
17304 end if;
17305 end;
17307 elsif Nkind_In (Exp, N_Type_Conversion,
17308 N_Unchecked_Type_Conversion)
17309 then
17310 Exp := Expression (Exp);
17311 goto Continue;
17313 elsif Nkind_In (Exp, N_Slice,
17314 N_Indexed_Component,
17315 N_Selected_Component)
17316 then
17317 -- Special check, if the prefix is an access type, then return
17318 -- since we are modifying the thing pointed to, not the prefix.
17319 -- When we are expanding, most usually the prefix is replaced
17320 -- by an explicit dereference, and this test is not needed, but
17321 -- in some cases (notably -gnatc mode and generics) when we do
17322 -- not do full expansion, we need this special test.
17324 if Is_Access_Type (Etype (Prefix (Exp))) then
17325 return;
17327 -- Otherwise go to prefix and keep going
17329 else
17330 Exp := Prefix (Exp);
17331 goto Continue;
17332 end if;
17334 -- All other cases, not a modification
17336 else
17337 return;
17338 end if;
17340 -- Now look for entity being referenced
17342 if Present (Ent) then
17343 if Is_Object (Ent) then
17344 if Comes_From_Source (Exp)
17345 or else Modification_Comes_From_Source
17346 then
17347 -- Give warning if pragma unmodified is given and we are
17348 -- sure this is a modification.
17350 if Has_Pragma_Unmodified (Ent) and then Sure then
17352 -- Note that the entity may be present only as a result
17353 -- of pragma Unused.
17355 if Has_Pragma_Unused (Ent) then
17356 Error_Msg_NE ("??pragma Unused given for &!", N, Ent);
17357 else
17358 Error_Msg_NE
17359 ("??pragma Unmodified given for &!", N, Ent);
17360 end if;
17361 end if;
17363 Set_Never_Set_In_Source (Ent, False);
17364 end if;
17366 Set_Is_True_Constant (Ent, False);
17367 Set_Current_Value (Ent, Empty);
17368 Set_Is_Known_Null (Ent, False);
17370 if not Can_Never_Be_Null (Ent) then
17371 Set_Is_Known_Non_Null (Ent, False);
17372 end if;
17374 -- Follow renaming chain
17376 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
17377 and then Present (Renamed_Object (Ent))
17378 then
17379 Exp := Renamed_Object (Ent);
17381 -- If the entity is the loop variable in an iteration over
17382 -- a container, retrieve container expression to indicate
17383 -- possible modification.
17385 if Present (Related_Expression (Ent))
17386 and then Nkind (Parent (Related_Expression (Ent))) =
17387 N_Iterator_Specification
17388 then
17389 Exp := Original_Node (Related_Expression (Ent));
17390 end if;
17392 goto Continue;
17394 -- The expression may be the renaming of a subcomponent of an
17395 -- array or container. The assignment to the subcomponent is
17396 -- a modification of the container.
17398 elsif Comes_From_Source (Original_Node (Exp))
17399 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
17400 N_Indexed_Component)
17401 then
17402 Exp := Prefix (Original_Node (Exp));
17403 goto Continue;
17404 end if;
17406 -- Generate a reference only if the assignment comes from
17407 -- source. This excludes, for example, calls to a dispatching
17408 -- assignment operation when the left-hand side is tagged. In
17409 -- GNATprove mode, we need those references also on generated
17410 -- code, as these are used to compute the local effects of
17411 -- subprograms.
17413 if Modification_Comes_From_Source or GNATprove_Mode then
17414 Generate_Reference (Ent, Exp, 'm');
17416 -- If the target of the assignment is the bound variable
17417 -- in an iterator, indicate that the corresponding array
17418 -- or container is also modified.
17420 if Ada_Version >= Ada_2012
17421 and then Nkind (Parent (Ent)) = N_Iterator_Specification
17422 then
17423 declare
17424 Domain : constant Node_Id := Name (Parent (Ent));
17426 begin
17427 -- TBD : in the full version of the construct, the
17428 -- domain of iteration can be given by an expression.
17430 if Is_Entity_Name (Domain) then
17431 Generate_Reference (Entity (Domain), Exp, 'm');
17432 Set_Is_True_Constant (Entity (Domain), False);
17433 Set_Never_Set_In_Source (Entity (Domain), False);
17434 end if;
17435 end;
17436 end if;
17437 end if;
17438 end if;
17440 Kill_Checks (Ent);
17442 -- If we are sure this is a modification from source, and we know
17443 -- this modifies a constant, then give an appropriate warning.
17445 if Sure
17446 and then Modification_Comes_From_Source
17447 and then Overlays_Constant (Ent)
17448 and then Address_Clause_Overlay_Warnings
17449 then
17450 declare
17451 Addr : constant Node_Id := Address_Clause (Ent);
17452 O_Ent : Entity_Id;
17453 Off : Boolean;
17455 begin
17456 Find_Overlaid_Entity (Addr, O_Ent, Off);
17458 Error_Msg_Sloc := Sloc (Addr);
17459 Error_Msg_NE
17460 ("??constant& may be modified via address clause#",
17461 N, O_Ent);
17462 end;
17463 end if;
17465 return;
17466 end if;
17468 <<Continue>>
17469 null;
17470 end loop;
17471 end Note_Possible_Modification;
17473 --------------------------------------
17474 -- Null_To_Null_Address_Convert_OK --
17475 --------------------------------------
17477 function Null_To_Null_Address_Convert_OK
17478 (N : Node_Id;
17479 Typ : Entity_Id := Empty) return Boolean
17481 begin
17482 if not Relaxed_RM_Semantics then
17483 return False;
17484 end if;
17486 if Nkind (N) = N_Null then
17487 return Present (Typ) and then Is_Descendant_Of_Address (Typ);
17489 elsif Nkind_In (N, N_Op_Eq, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt, N_Op_Ne)
17490 then
17491 declare
17492 L : constant Node_Id := Left_Opnd (N);
17493 R : constant Node_Id := Right_Opnd (N);
17495 begin
17496 -- We check the Etype of the complementary operand since the
17497 -- N_Null node is not decorated at this stage.
17499 return
17500 ((Nkind (L) = N_Null
17501 and then Is_Descendant_Of_Address (Etype (R)))
17502 or else
17503 (Nkind (R) = N_Null
17504 and then Is_Descendant_Of_Address (Etype (L))));
17505 end;
17506 end if;
17508 return False;
17509 end Null_To_Null_Address_Convert_OK;
17511 -------------------------
17512 -- Object_Access_Level --
17513 -------------------------
17515 -- Returns the static accessibility level of the view denoted by Obj. Note
17516 -- that the value returned is the result of a call to Scope_Depth. Only
17517 -- scope depths associated with dynamic scopes can actually be returned.
17518 -- Since only relative levels matter for accessibility checking, the fact
17519 -- that the distance between successive levels of accessibility is not
17520 -- always one is immaterial (invariant: if level(E2) is deeper than
17521 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
17523 function Object_Access_Level (Obj : Node_Id) return Uint is
17524 function Is_Interface_Conversion (N : Node_Id) return Boolean;
17525 -- Determine whether N is a construct of the form
17526 -- Some_Type (Operand._tag'Address)
17527 -- This construct appears in the context of dispatching calls.
17529 function Reference_To (Obj : Node_Id) return Node_Id;
17530 -- An explicit dereference is created when removing side-effects from
17531 -- expressions for constraint checking purposes. In this case a local
17532 -- access type is created for it. The correct access level is that of
17533 -- the original source node. We detect this case by noting that the
17534 -- prefix of the dereference is created by an object declaration whose
17535 -- initial expression is a reference.
17537 -----------------------------
17538 -- Is_Interface_Conversion --
17539 -----------------------------
17541 function Is_Interface_Conversion (N : Node_Id) return Boolean is
17542 begin
17543 return Nkind (N) = N_Unchecked_Type_Conversion
17544 and then Nkind (Expression (N)) = N_Attribute_Reference
17545 and then Attribute_Name (Expression (N)) = Name_Address;
17546 end Is_Interface_Conversion;
17548 ------------------
17549 -- Reference_To --
17550 ------------------
17552 function Reference_To (Obj : Node_Id) return Node_Id is
17553 Pref : constant Node_Id := Prefix (Obj);
17554 begin
17555 if Is_Entity_Name (Pref)
17556 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
17557 and then Present (Expression (Parent (Entity (Pref))))
17558 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
17559 then
17560 return (Prefix (Expression (Parent (Entity (Pref)))));
17561 else
17562 return Empty;
17563 end if;
17564 end Reference_To;
17566 -- Local variables
17568 E : Entity_Id;
17570 -- Start of processing for Object_Access_Level
17572 begin
17573 if Nkind (Obj) = N_Defining_Identifier
17574 or else Is_Entity_Name (Obj)
17575 then
17576 if Nkind (Obj) = N_Defining_Identifier then
17577 E := Obj;
17578 else
17579 E := Entity (Obj);
17580 end if;
17582 if Is_Prival (E) then
17583 E := Prival_Link (E);
17584 end if;
17586 -- If E is a type then it denotes a current instance. For this case
17587 -- we add one to the normal accessibility level of the type to ensure
17588 -- that current instances are treated as always being deeper than
17589 -- than the level of any visible named access type (see 3.10.2(21)).
17591 if Is_Type (E) then
17592 return Type_Access_Level (E) + 1;
17594 elsif Present (Renamed_Object (E)) then
17595 return Object_Access_Level (Renamed_Object (E));
17597 -- Similarly, if E is a component of the current instance of a
17598 -- protected type, any instance of it is assumed to be at a deeper
17599 -- level than the type. For a protected object (whose type is an
17600 -- anonymous protected type) its components are at the same level
17601 -- as the type itself.
17603 elsif not Is_Overloadable (E)
17604 and then Ekind (Scope (E)) = E_Protected_Type
17605 and then Comes_From_Source (Scope (E))
17606 then
17607 return Type_Access_Level (Scope (E)) + 1;
17609 else
17610 -- Aliased formals of functions take their access level from the
17611 -- point of call, i.e. require a dynamic check. For static check
17612 -- purposes, this is smaller than the level of the subprogram
17613 -- itself. For procedures the aliased makes no difference.
17615 if Is_Formal (E)
17616 and then Is_Aliased (E)
17617 and then Ekind (Scope (E)) = E_Function
17618 then
17619 return Type_Access_Level (Etype (E));
17621 else
17622 return Scope_Depth (Enclosing_Dynamic_Scope (E));
17623 end if;
17624 end if;
17626 elsif Nkind (Obj) = N_Selected_Component then
17627 if Is_Access_Type (Etype (Prefix (Obj))) then
17628 return Type_Access_Level (Etype (Prefix (Obj)));
17629 else
17630 return Object_Access_Level (Prefix (Obj));
17631 end if;
17633 elsif Nkind (Obj) = N_Indexed_Component then
17634 if Is_Access_Type (Etype (Prefix (Obj))) then
17635 return Type_Access_Level (Etype (Prefix (Obj)));
17636 else
17637 return Object_Access_Level (Prefix (Obj));
17638 end if;
17640 elsif Nkind (Obj) = N_Explicit_Dereference then
17642 -- If the prefix is a selected access discriminant then we make a
17643 -- recursive call on the prefix, which will in turn check the level
17644 -- of the prefix object of the selected discriminant.
17646 -- In Ada 2012, if the discriminant has implicit dereference and
17647 -- the context is a selected component, treat this as an object of
17648 -- unknown scope (see below). This is necessary in compile-only mode;
17649 -- otherwise expansion will already have transformed the prefix into
17650 -- a temporary.
17652 if Nkind (Prefix (Obj)) = N_Selected_Component
17653 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
17654 and then
17655 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
17656 and then
17657 (not Has_Implicit_Dereference
17658 (Entity (Selector_Name (Prefix (Obj))))
17659 or else Nkind (Parent (Obj)) /= N_Selected_Component)
17660 then
17661 return Object_Access_Level (Prefix (Obj));
17663 -- Detect an interface conversion in the context of a dispatching
17664 -- call. Use the original form of the conversion to find the access
17665 -- level of the operand.
17667 elsif Is_Interface (Etype (Obj))
17668 and then Is_Interface_Conversion (Prefix (Obj))
17669 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
17670 then
17671 return Object_Access_Level (Original_Node (Obj));
17673 elsif not Comes_From_Source (Obj) then
17674 declare
17675 Ref : constant Node_Id := Reference_To (Obj);
17676 begin
17677 if Present (Ref) then
17678 return Object_Access_Level (Ref);
17679 else
17680 return Type_Access_Level (Etype (Prefix (Obj)));
17681 end if;
17682 end;
17684 else
17685 return Type_Access_Level (Etype (Prefix (Obj)));
17686 end if;
17688 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
17689 return Object_Access_Level (Expression (Obj));
17691 elsif Nkind (Obj) = N_Function_Call then
17693 -- Function results are objects, so we get either the access level of
17694 -- the function or, in the case of an indirect call, the level of the
17695 -- access-to-subprogram type. (This code is used for Ada 95, but it
17696 -- looks wrong, because it seems that we should be checking the level
17697 -- of the call itself, even for Ada 95. However, using the Ada 2005
17698 -- version of the code causes regressions in several tests that are
17699 -- compiled with -gnat95. ???)
17701 if Ada_Version < Ada_2005 then
17702 if Is_Entity_Name (Name (Obj)) then
17703 return Subprogram_Access_Level (Entity (Name (Obj)));
17704 else
17705 return Type_Access_Level (Etype (Prefix (Name (Obj))));
17706 end if;
17708 -- For Ada 2005, the level of the result object of a function call is
17709 -- defined to be the level of the call's innermost enclosing master.
17710 -- We determine that by querying the depth of the innermost enclosing
17711 -- dynamic scope.
17713 else
17714 Return_Master_Scope_Depth_Of_Call : declare
17716 function Innermost_Master_Scope_Depth
17717 (N : Node_Id) return Uint;
17718 -- Returns the scope depth of the given node's innermost
17719 -- enclosing dynamic scope (effectively the accessibility
17720 -- level of the innermost enclosing master).
17722 ----------------------------------
17723 -- Innermost_Master_Scope_Depth --
17724 ----------------------------------
17726 function Innermost_Master_Scope_Depth
17727 (N : Node_Id) return Uint
17729 Node_Par : Node_Id := Parent (N);
17731 begin
17732 -- Locate the nearest enclosing node (by traversing Parents)
17733 -- that Defining_Entity can be applied to, and return the
17734 -- depth of that entity's nearest enclosing dynamic scope.
17736 while Present (Node_Par) loop
17737 case Nkind (Node_Par) is
17738 when N_Component_Declaration |
17739 N_Entry_Declaration |
17740 N_Formal_Object_Declaration |
17741 N_Formal_Type_Declaration |
17742 N_Full_Type_Declaration |
17743 N_Incomplete_Type_Declaration |
17744 N_Loop_Parameter_Specification |
17745 N_Object_Declaration |
17746 N_Protected_Type_Declaration |
17747 N_Private_Extension_Declaration |
17748 N_Private_Type_Declaration |
17749 N_Subtype_Declaration |
17750 N_Function_Specification |
17751 N_Procedure_Specification |
17752 N_Task_Type_Declaration |
17753 N_Body_Stub |
17754 N_Generic_Instantiation |
17755 N_Proper_Body |
17756 N_Implicit_Label_Declaration |
17757 N_Package_Declaration |
17758 N_Single_Task_Declaration |
17759 N_Subprogram_Declaration |
17760 N_Generic_Declaration |
17761 N_Renaming_Declaration |
17762 N_Block_Statement |
17763 N_Formal_Subprogram_Declaration |
17764 N_Abstract_Subprogram_Declaration |
17765 N_Entry_Body |
17766 N_Exception_Declaration |
17767 N_Formal_Package_Declaration |
17768 N_Number_Declaration |
17769 N_Package_Specification |
17770 N_Parameter_Specification |
17771 N_Single_Protected_Declaration |
17772 N_Subunit =>
17774 return Scope_Depth
17775 (Nearest_Dynamic_Scope
17776 (Defining_Entity (Node_Par)));
17778 when others =>
17779 null;
17780 end case;
17782 Node_Par := Parent (Node_Par);
17783 end loop;
17785 pragma Assert (False);
17787 -- Should never reach the following return
17789 return Scope_Depth (Current_Scope) + 1;
17790 end Innermost_Master_Scope_Depth;
17792 -- Start of processing for Return_Master_Scope_Depth_Of_Call
17794 begin
17795 return Innermost_Master_Scope_Depth (Obj);
17796 end Return_Master_Scope_Depth_Of_Call;
17797 end if;
17799 -- For convenience we handle qualified expressions, even though they
17800 -- aren't technically object names.
17802 elsif Nkind (Obj) = N_Qualified_Expression then
17803 return Object_Access_Level (Expression (Obj));
17805 -- Ditto for aggregates. They have the level of the temporary that
17806 -- will hold their value.
17808 elsif Nkind (Obj) = N_Aggregate then
17809 return Object_Access_Level (Current_Scope);
17811 -- Otherwise return the scope level of Standard. (If there are cases
17812 -- that fall through to this point they will be treated as having
17813 -- global accessibility for now. ???)
17815 else
17816 return Scope_Depth (Standard_Standard);
17817 end if;
17818 end Object_Access_Level;
17820 ---------------------------------
17821 -- Original_Aspect_Pragma_Name --
17822 ---------------------------------
17824 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
17825 Item : Node_Id;
17826 Item_Nam : Name_Id;
17828 begin
17829 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
17831 Item := N;
17833 -- The pragma was generated to emulate an aspect, use the original
17834 -- aspect specification.
17836 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
17837 Item := Corresponding_Aspect (Item);
17838 end if;
17840 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
17841 -- Post and Post_Class rewrite their pragma identifier to preserve the
17842 -- original name.
17843 -- ??? this is kludgey
17845 if Nkind (Item) = N_Pragma then
17846 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
17848 else
17849 pragma Assert (Nkind (Item) = N_Aspect_Specification);
17850 Item_Nam := Chars (Identifier (Item));
17851 end if;
17853 -- Deal with 'Class by converting the name to its _XXX form
17855 if Class_Present (Item) then
17856 if Item_Nam = Name_Invariant then
17857 Item_Nam := Name_uInvariant;
17859 elsif Item_Nam = Name_Post then
17860 Item_Nam := Name_uPost;
17862 elsif Item_Nam = Name_Pre then
17863 Item_Nam := Name_uPre;
17865 elsif Nam_In (Item_Nam, Name_Type_Invariant,
17866 Name_Type_Invariant_Class)
17867 then
17868 Item_Nam := Name_uType_Invariant;
17870 -- Nothing to do for other cases (e.g. a Check that derived from
17871 -- Pre_Class and has the flag set). Also we do nothing if the name
17872 -- is already in special _xxx form.
17874 end if;
17875 end if;
17877 return Item_Nam;
17878 end Original_Aspect_Pragma_Name;
17880 --------------------------------------
17881 -- Original_Corresponding_Operation --
17882 --------------------------------------
17884 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
17886 Typ : constant Entity_Id := Find_Dispatching_Type (S);
17888 begin
17889 -- If S is an inherited primitive S2 the original corresponding
17890 -- operation of S is the original corresponding operation of S2
17892 if Present (Alias (S))
17893 and then Find_Dispatching_Type (Alias (S)) /= Typ
17894 then
17895 return Original_Corresponding_Operation (Alias (S));
17897 -- If S overrides an inherited subprogram S2 the original corresponding
17898 -- operation of S is the original corresponding operation of S2
17900 elsif Present (Overridden_Operation (S)) then
17901 return Original_Corresponding_Operation (Overridden_Operation (S));
17903 -- otherwise it is S itself
17905 else
17906 return S;
17907 end if;
17908 end Original_Corresponding_Operation;
17910 -------------------
17911 -- Output_Entity --
17912 -------------------
17914 procedure Output_Entity (Id : Entity_Id) is
17915 Scop : Entity_Id;
17917 begin
17918 Scop := Scope (Id);
17920 -- The entity may lack a scope when it is in the process of being
17921 -- analyzed. Use the current scope as an approximation.
17923 if No (Scop) then
17924 Scop := Current_Scope;
17925 end if;
17927 Output_Name (Chars (Id), Scop);
17928 end Output_Entity;
17930 -----------------
17931 -- Output_Name --
17932 -----------------
17934 procedure Output_Name (Nam : Name_Id; Scop : Entity_Id := Current_Scope) is
17935 begin
17936 Write_Str
17937 (Get_Name_String
17938 (Get_Qualified_Name
17939 (Nam => Nam,
17940 Suffix => No_Name,
17941 Scop => Scop)));
17942 Write_Eol;
17943 end Output_Name;
17945 ----------------------
17946 -- Policy_In_Effect --
17947 ----------------------
17949 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
17950 function Policy_In_List (List : Node_Id) return Name_Id;
17951 -- Determine the mode of a policy in a N_Pragma list
17953 --------------------
17954 -- Policy_In_List --
17955 --------------------
17957 function Policy_In_List (List : Node_Id) return Name_Id is
17958 Arg1 : Node_Id;
17959 Arg2 : Node_Id;
17960 Prag : Node_Id;
17962 begin
17963 Prag := List;
17964 while Present (Prag) loop
17965 Arg1 := First (Pragma_Argument_Associations (Prag));
17966 Arg2 := Next (Arg1);
17968 Arg1 := Get_Pragma_Arg (Arg1);
17969 Arg2 := Get_Pragma_Arg (Arg2);
17971 -- The current Check_Policy pragma matches the requested policy or
17972 -- appears in the single argument form (Assertion, policy_id).
17974 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
17975 return Chars (Arg2);
17976 end if;
17978 Prag := Next_Pragma (Prag);
17979 end loop;
17981 return No_Name;
17982 end Policy_In_List;
17984 -- Local variables
17986 Kind : Name_Id;
17988 -- Start of processing for Policy_In_Effect
17990 begin
17991 if not Is_Valid_Assertion_Kind (Policy) then
17992 raise Program_Error;
17993 end if;
17995 -- Inspect all policy pragmas that appear within scopes (if any)
17997 Kind := Policy_In_List (Check_Policy_List);
17999 -- Inspect all configuration policy pragmas (if any)
18001 if Kind = No_Name then
18002 Kind := Policy_In_List (Check_Policy_List_Config);
18003 end if;
18005 -- The context lacks policy pragmas, determine the mode based on whether
18006 -- assertions are enabled at the configuration level. This ensures that
18007 -- the policy is preserved when analyzing generics.
18009 if Kind = No_Name then
18010 if Assertions_Enabled_Config then
18011 Kind := Name_Check;
18012 else
18013 Kind := Name_Ignore;
18014 end if;
18015 end if;
18017 return Kind;
18018 end Policy_In_Effect;
18020 ----------------------------------
18021 -- Predicate_Tests_On_Arguments --
18022 ----------------------------------
18024 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
18025 begin
18026 -- Always test predicates on indirect call
18028 if Ekind (Subp) = E_Subprogram_Type then
18029 return True;
18031 -- Do not test predicates on call to generated default Finalize, since
18032 -- we are not interested in whether something we are finalizing (and
18033 -- typically destroying) satisfies its predicates.
18035 elsif Chars (Subp) = Name_Finalize
18036 and then not Comes_From_Source (Subp)
18037 then
18038 return False;
18040 -- Do not test predicates on any internally generated routines
18042 elsif Is_Internal_Name (Chars (Subp)) then
18043 return False;
18045 -- Do not test predicates on call to Init_Proc, since if needed the
18046 -- predicate test will occur at some other point.
18048 elsif Is_Init_Proc (Subp) then
18049 return False;
18051 -- Do not test predicates on call to predicate function, since this
18052 -- would cause infinite recursion.
18054 elsif Ekind (Subp) = E_Function
18055 and then (Is_Predicate_Function (Subp)
18056 or else
18057 Is_Predicate_Function_M (Subp))
18058 then
18059 return False;
18061 -- For now, no other exceptions
18063 else
18064 return True;
18065 end if;
18066 end Predicate_Tests_On_Arguments;
18068 -----------------------
18069 -- Private_Component --
18070 -----------------------
18072 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
18073 Ancestor : constant Entity_Id := Base_Type (Type_Id);
18075 function Trace_Components
18076 (T : Entity_Id;
18077 Check : Boolean) return Entity_Id;
18078 -- Recursive function that does the work, and checks against circular
18079 -- definition for each subcomponent type.
18081 ----------------------
18082 -- Trace_Components --
18083 ----------------------
18085 function Trace_Components
18086 (T : Entity_Id;
18087 Check : Boolean) return Entity_Id
18089 Btype : constant Entity_Id := Base_Type (T);
18090 Component : Entity_Id;
18091 P : Entity_Id;
18092 Candidate : Entity_Id := Empty;
18094 begin
18095 if Check and then Btype = Ancestor then
18096 Error_Msg_N ("circular type definition", Type_Id);
18097 return Any_Type;
18098 end if;
18100 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
18101 if Present (Full_View (Btype))
18102 and then Is_Record_Type (Full_View (Btype))
18103 and then not Is_Frozen (Btype)
18104 then
18105 -- To indicate that the ancestor depends on a private type, the
18106 -- current Btype is sufficient. However, to check for circular
18107 -- definition we must recurse on the full view.
18109 Candidate := Trace_Components (Full_View (Btype), True);
18111 if Candidate = Any_Type then
18112 return Any_Type;
18113 else
18114 return Btype;
18115 end if;
18117 else
18118 return Btype;
18119 end if;
18121 elsif Is_Array_Type (Btype) then
18122 return Trace_Components (Component_Type (Btype), True);
18124 elsif Is_Record_Type (Btype) then
18125 Component := First_Entity (Btype);
18126 while Present (Component)
18127 and then Comes_From_Source (Component)
18128 loop
18129 -- Skip anonymous types generated by constrained components
18131 if not Is_Type (Component) then
18132 P := Trace_Components (Etype (Component), True);
18134 if Present (P) then
18135 if P = Any_Type then
18136 return P;
18137 else
18138 Candidate := P;
18139 end if;
18140 end if;
18141 end if;
18143 Next_Entity (Component);
18144 end loop;
18146 return Candidate;
18148 else
18149 return Empty;
18150 end if;
18151 end Trace_Components;
18153 -- Start of processing for Private_Component
18155 begin
18156 return Trace_Components (Type_Id, False);
18157 end Private_Component;
18159 ---------------------------
18160 -- Primitive_Names_Match --
18161 ---------------------------
18163 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
18164 function Non_Internal_Name (E : Entity_Id) return Name_Id;
18165 -- Given an internal name, returns the corresponding non-internal name
18167 ------------------------
18168 -- Non_Internal_Name --
18169 ------------------------
18171 function Non_Internal_Name (E : Entity_Id) return Name_Id is
18172 begin
18173 Get_Name_String (Chars (E));
18174 Name_Len := Name_Len - 1;
18175 return Name_Find;
18176 end Non_Internal_Name;
18178 -- Start of processing for Primitive_Names_Match
18180 begin
18181 pragma Assert (Present (E1) and then Present (E2));
18183 return Chars (E1) = Chars (E2)
18184 or else
18185 (not Is_Internal_Name (Chars (E1))
18186 and then Is_Internal_Name (Chars (E2))
18187 and then Non_Internal_Name (E2) = Chars (E1))
18188 or else
18189 (not Is_Internal_Name (Chars (E2))
18190 and then Is_Internal_Name (Chars (E1))
18191 and then Non_Internal_Name (E1) = Chars (E2))
18192 or else
18193 (Is_Predefined_Dispatching_Operation (E1)
18194 and then Is_Predefined_Dispatching_Operation (E2)
18195 and then Same_TSS (E1, E2))
18196 or else
18197 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
18198 end Primitive_Names_Match;
18200 -----------------------
18201 -- Process_End_Label --
18202 -----------------------
18204 procedure Process_End_Label
18205 (N : Node_Id;
18206 Typ : Character;
18207 Ent : Entity_Id)
18209 Loc : Source_Ptr;
18210 Nam : Node_Id;
18211 Scop : Entity_Id;
18213 Label_Ref : Boolean;
18214 -- Set True if reference to end label itself is required
18216 Endl : Node_Id;
18217 -- Gets set to the operator symbol or identifier that references the
18218 -- entity Ent. For the child unit case, this is the identifier from the
18219 -- designator. For other cases, this is simply Endl.
18221 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
18222 -- N is an identifier node that appears as a parent unit reference in
18223 -- the case where Ent is a child unit. This procedure generates an
18224 -- appropriate cross-reference entry. E is the corresponding entity.
18226 -------------------------
18227 -- Generate_Parent_Ref --
18228 -------------------------
18230 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
18231 begin
18232 -- If names do not match, something weird, skip reference
18234 if Chars (E) = Chars (N) then
18236 -- Generate the reference. We do NOT consider this as a reference
18237 -- for unreferenced symbol purposes.
18239 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
18241 if Style_Check then
18242 Style.Check_Identifier (N, E);
18243 end if;
18244 end if;
18245 end Generate_Parent_Ref;
18247 -- Start of processing for Process_End_Label
18249 begin
18250 -- If no node, ignore. This happens in some error situations, and
18251 -- also for some internally generated structures where no end label
18252 -- references are required in any case.
18254 if No (N) then
18255 return;
18256 end if;
18258 -- Nothing to do if no End_Label, happens for internally generated
18259 -- constructs where we don't want an end label reference anyway. Also
18260 -- nothing to do if Endl is a string literal, which means there was
18261 -- some prior error (bad operator symbol)
18263 Endl := End_Label (N);
18265 if No (Endl) or else Nkind (Endl) = N_String_Literal then
18266 return;
18267 end if;
18269 -- Reference node is not in extended main source unit
18271 if not In_Extended_Main_Source_Unit (N) then
18273 -- Generally we do not collect references except for the extended
18274 -- main source unit. The one exception is the 'e' entry for a
18275 -- package spec, where it is useful for a client to have the
18276 -- ending information to define scopes.
18278 if Typ /= 'e' then
18279 return;
18281 else
18282 Label_Ref := False;
18284 -- For this case, we can ignore any parent references, but we
18285 -- need the package name itself for the 'e' entry.
18287 if Nkind (Endl) = N_Designator then
18288 Endl := Identifier (Endl);
18289 end if;
18290 end if;
18292 -- Reference is in extended main source unit
18294 else
18295 Label_Ref := True;
18297 -- For designator, generate references for the parent entries
18299 if Nkind (Endl) = N_Designator then
18301 -- Generate references for the prefix if the END line comes from
18302 -- source (otherwise we do not need these references) We climb the
18303 -- scope stack to find the expected entities.
18305 if Comes_From_Source (Endl) then
18306 Nam := Name (Endl);
18307 Scop := Current_Scope;
18308 while Nkind (Nam) = N_Selected_Component loop
18309 Scop := Scope (Scop);
18310 exit when No (Scop);
18311 Generate_Parent_Ref (Selector_Name (Nam), Scop);
18312 Nam := Prefix (Nam);
18313 end loop;
18315 if Present (Scop) then
18316 Generate_Parent_Ref (Nam, Scope (Scop));
18317 end if;
18318 end if;
18320 Endl := Identifier (Endl);
18321 end if;
18322 end if;
18324 -- If the end label is not for the given entity, then either we have
18325 -- some previous error, or this is a generic instantiation for which
18326 -- we do not need to make a cross-reference in this case anyway. In
18327 -- either case we simply ignore the call.
18329 if Chars (Ent) /= Chars (Endl) then
18330 return;
18331 end if;
18333 -- If label was really there, then generate a normal reference and then
18334 -- adjust the location in the end label to point past the name (which
18335 -- should almost always be the semicolon).
18337 Loc := Sloc (Endl);
18339 if Comes_From_Source (Endl) then
18341 -- If a label reference is required, then do the style check and
18342 -- generate an l-type cross-reference entry for the label
18344 if Label_Ref then
18345 if Style_Check then
18346 Style.Check_Identifier (Endl, Ent);
18347 end if;
18349 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
18350 end if;
18352 -- Set the location to point past the label (normally this will
18353 -- mean the semicolon immediately following the label). This is
18354 -- done for the sake of the 'e' or 't' entry generated below.
18356 Get_Decoded_Name_String (Chars (Endl));
18357 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
18359 else
18360 -- In SPARK mode, no missing label is allowed for packages and
18361 -- subprogram bodies. Detect those cases by testing whether
18362 -- Process_End_Label was called for a body (Typ = 't') or a package.
18364 if Restriction_Check_Required (SPARK_05)
18365 and then (Typ = 't' or else Ekind (Ent) = E_Package)
18366 then
18367 Error_Msg_Node_1 := Endl;
18368 Check_SPARK_05_Restriction
18369 ("`END &` required", Endl, Force => True);
18370 end if;
18371 end if;
18373 -- Now generate the e/t reference
18375 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
18377 -- Restore Sloc, in case modified above, since we have an identifier
18378 -- and the normal Sloc should be left set in the tree.
18380 Set_Sloc (Endl, Loc);
18381 end Process_End_Label;
18383 --------------------------------
18384 -- Propagate_Concurrent_Flags --
18385 --------------------------------
18387 procedure Propagate_Concurrent_Flags
18388 (Typ : Entity_Id;
18389 Comp_Typ : Entity_Id)
18391 begin
18392 if Has_Task (Comp_Typ) then
18393 Set_Has_Task (Typ);
18394 end if;
18396 if Has_Protected (Comp_Typ) then
18397 Set_Has_Protected (Typ);
18398 end if;
18400 if Has_Timing_Event (Comp_Typ) then
18401 Set_Has_Timing_Event (Typ);
18402 end if;
18403 end Propagate_Concurrent_Flags;
18405 ------------------------------
18406 -- Propagate_DIC_Attributes --
18407 ------------------------------
18409 procedure Propagate_DIC_Attributes
18410 (Typ : Entity_Id;
18411 From_Typ : Entity_Id)
18413 DIC_Proc : Entity_Id;
18415 begin
18416 if Present (Typ) and then Present (From_Typ) then
18417 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
18419 -- Nothing to do if both the source and the destination denote the
18420 -- same type.
18422 if From_Typ = Typ then
18423 return;
18424 end if;
18426 DIC_Proc := DIC_Procedure (From_Typ);
18428 -- The setting of the attributes is intentionally conservative. This
18429 -- prevents accidental clobbering of enabled attributes.
18431 if Has_Inherited_DIC (From_Typ)
18432 and then not Has_Inherited_DIC (Typ)
18433 then
18434 Set_Has_Inherited_DIC (Typ);
18435 end if;
18437 if Has_Own_DIC (From_Typ) and then not Has_Own_DIC (Typ) then
18438 Set_Has_Own_DIC (Typ);
18439 end if;
18441 if Present (DIC_Proc) and then No (DIC_Procedure (Typ)) then
18442 Set_DIC_Procedure (Typ, DIC_Proc);
18443 end if;
18444 end if;
18445 end Propagate_DIC_Attributes;
18447 ------------------------------------
18448 -- Propagate_Invariant_Attributes --
18449 ------------------------------------
18451 procedure Propagate_Invariant_Attributes
18452 (Typ : Entity_Id;
18453 From_Typ : Entity_Id)
18455 Full_IP : Entity_Id;
18456 Part_IP : Entity_Id;
18458 begin
18459 if Present (Typ) and then Present (From_Typ) then
18460 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
18462 -- Nothing to do if both the source and the destination denote the
18463 -- same type.
18465 if From_Typ = Typ then
18466 return;
18467 end if;
18469 Full_IP := Invariant_Procedure (From_Typ);
18470 Part_IP := Partial_Invariant_Procedure (From_Typ);
18472 -- The setting of the attributes is intentionally conservative. This
18473 -- prevents accidental clobbering of enabled attributes.
18475 if Has_Inheritable_Invariants (From_Typ)
18476 and then not Has_Inheritable_Invariants (Typ)
18477 then
18478 Set_Has_Inheritable_Invariants (Typ, True);
18479 end if;
18481 if Has_Inherited_Invariants (From_Typ)
18482 and then not Has_Inherited_Invariants (Typ)
18483 then
18484 Set_Has_Inherited_Invariants (Typ, True);
18485 end if;
18487 if Has_Own_Invariants (From_Typ)
18488 and then not Has_Own_Invariants (Typ)
18489 then
18490 Set_Has_Own_Invariants (Typ, True);
18491 end if;
18493 if Present (Full_IP) and then No (Invariant_Procedure (Typ)) then
18494 Set_Invariant_Procedure (Typ, Full_IP);
18495 end if;
18497 if Present (Part_IP) and then No (Partial_Invariant_Procedure (Typ))
18498 then
18499 Set_Partial_Invariant_Procedure (Typ, Part_IP);
18500 end if;
18501 end if;
18502 end Propagate_Invariant_Attributes;
18504 ---------------------------------------
18505 -- Record_Possible_Part_Of_Reference --
18506 ---------------------------------------
18508 procedure Record_Possible_Part_Of_Reference
18509 (Var_Id : Entity_Id;
18510 Ref : Node_Id)
18512 Encap : constant Entity_Id := Encapsulating_State (Var_Id);
18513 Refs : Elist_Id;
18515 begin
18516 -- The variable is a constituent of a single protected/task type. Such
18517 -- a variable acts as a component of the type and must appear within a
18518 -- specific region (SPARK RM 9.3). Instead of recording the reference,
18519 -- verify its legality now.
18521 if Present (Encap) and then Is_Single_Concurrent_Object (Encap) then
18522 Check_Part_Of_Reference (Var_Id, Ref);
18524 -- The variable is subject to pragma Part_Of and may eventually become a
18525 -- constituent of a single protected/task type. Record the reference to
18526 -- verify its placement when the contract of the variable is analyzed.
18528 elsif Present (Get_Pragma (Var_Id, Pragma_Part_Of)) then
18529 Refs := Part_Of_References (Var_Id);
18531 if No (Refs) then
18532 Refs := New_Elmt_List;
18533 Set_Part_Of_References (Var_Id, Refs);
18534 end if;
18536 Append_Elmt (Ref, Refs);
18537 end if;
18538 end Record_Possible_Part_Of_Reference;
18540 ----------------
18541 -- Referenced --
18542 ----------------
18544 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
18545 Seen : Boolean := False;
18547 function Is_Reference (N : Node_Id) return Traverse_Result;
18548 -- Determine whether node N denotes a reference to Id. If this is the
18549 -- case, set global flag Seen to True and stop the traversal.
18551 ------------------
18552 -- Is_Reference --
18553 ------------------
18555 function Is_Reference (N : Node_Id) return Traverse_Result is
18556 begin
18557 if Is_Entity_Name (N)
18558 and then Present (Entity (N))
18559 and then Entity (N) = Id
18560 then
18561 Seen := True;
18562 return Abandon;
18563 else
18564 return OK;
18565 end if;
18566 end Is_Reference;
18568 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
18570 -- Start of processing for Referenced
18572 begin
18573 Inspect_Expression (Expr);
18574 return Seen;
18575 end Referenced;
18577 ------------------------------------
18578 -- References_Generic_Formal_Type --
18579 ------------------------------------
18581 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
18583 function Process (N : Node_Id) return Traverse_Result;
18584 -- Process one node in search for generic formal type
18586 -------------
18587 -- Process --
18588 -------------
18590 function Process (N : Node_Id) return Traverse_Result is
18591 begin
18592 if Nkind (N) in N_Has_Entity then
18593 declare
18594 E : constant Entity_Id := Entity (N);
18595 begin
18596 if Present (E) then
18597 if Is_Generic_Type (E) then
18598 return Abandon;
18599 elsif Present (Etype (E))
18600 and then Is_Generic_Type (Etype (E))
18601 then
18602 return Abandon;
18603 end if;
18604 end if;
18605 end;
18606 end if;
18608 return Atree.OK;
18609 end Process;
18611 function Traverse is new Traverse_Func (Process);
18612 -- Traverse tree to look for generic type
18614 begin
18615 if Inside_A_Generic then
18616 return Traverse (N) = Abandon;
18617 else
18618 return False;
18619 end if;
18620 end References_Generic_Formal_Type;
18622 --------------------
18623 -- Remove_Homonym --
18624 --------------------
18626 procedure Remove_Homonym (E : Entity_Id) is
18627 Prev : Entity_Id := Empty;
18628 H : Entity_Id;
18630 begin
18631 if E = Current_Entity (E) then
18632 if Present (Homonym (E)) then
18633 Set_Current_Entity (Homonym (E));
18634 else
18635 Set_Name_Entity_Id (Chars (E), Empty);
18636 end if;
18638 else
18639 H := Current_Entity (E);
18640 while Present (H) and then H /= E loop
18641 Prev := H;
18642 H := Homonym (H);
18643 end loop;
18645 -- If E is not on the homonym chain, nothing to do
18647 if Present (H) then
18648 Set_Homonym (Prev, Homonym (E));
18649 end if;
18650 end if;
18651 end Remove_Homonym;
18653 ------------------------------
18654 -- Remove_Overloaded_Entity --
18655 ------------------------------
18657 procedure Remove_Overloaded_Entity (Id : Entity_Id) is
18658 procedure Remove_Primitive_Of (Typ : Entity_Id);
18659 -- Remove primitive subprogram Id from the list of primitives that
18660 -- belong to type Typ.
18662 -------------------------
18663 -- Remove_Primitive_Of --
18664 -------------------------
18666 procedure Remove_Primitive_Of (Typ : Entity_Id) is
18667 Prims : Elist_Id;
18669 begin
18670 if Is_Tagged_Type (Typ) then
18671 Prims := Direct_Primitive_Operations (Typ);
18673 if Present (Prims) then
18674 Remove (Prims, Id);
18675 end if;
18676 end if;
18677 end Remove_Primitive_Of;
18679 -- Local variables
18681 Scop : constant Entity_Id := Scope (Id);
18682 Formal : Entity_Id;
18683 Prev_Id : Entity_Id;
18685 -- Start of processing for Remove_Overloaded_Entity
18687 begin
18688 -- Remove the entity from the homonym chain. When the entity is the
18689 -- head of the chain, associate the entry in the name table with its
18690 -- homonym effectively making it the new head of the chain.
18692 if Current_Entity (Id) = Id then
18693 Set_Name_Entity_Id (Chars (Id), Homonym (Id));
18695 -- Otherwise link the previous and next homonyms
18697 else
18698 Prev_Id := Current_Entity (Id);
18699 while Present (Prev_Id) and then Homonym (Prev_Id) /= Id loop
18700 Prev_Id := Homonym (Prev_Id);
18701 end loop;
18703 Set_Homonym (Prev_Id, Homonym (Id));
18704 end if;
18706 -- Remove the entity from the scope entity chain. When the entity is
18707 -- the head of the chain, set the next entity as the new head of the
18708 -- chain.
18710 if First_Entity (Scop) = Id then
18711 Prev_Id := Empty;
18712 Set_First_Entity (Scop, Next_Entity (Id));
18714 -- Otherwise the entity is either in the middle of the chain or it acts
18715 -- as its tail. Traverse and link the previous and next entities.
18717 else
18718 Prev_Id := First_Entity (Scop);
18719 while Present (Prev_Id) and then Next_Entity (Prev_Id) /= Id loop
18720 Next_Entity (Prev_Id);
18721 end loop;
18723 Set_Next_Entity (Prev_Id, Next_Entity (Id));
18724 end if;
18726 -- Handle the case where the entity acts as the tail of the scope entity
18727 -- chain.
18729 if Last_Entity (Scop) = Id then
18730 Set_Last_Entity (Scop, Prev_Id);
18731 end if;
18733 -- The entity denotes a primitive subprogram. Remove it from the list of
18734 -- primitives of the associated controlling type.
18736 if Ekind_In (Id, E_Function, E_Procedure) and then Is_Primitive (Id) then
18737 Formal := First_Formal (Id);
18738 while Present (Formal) loop
18739 if Is_Controlling_Formal (Formal) then
18740 Remove_Primitive_Of (Etype (Formal));
18741 exit;
18742 end if;
18744 Next_Formal (Formal);
18745 end loop;
18747 if Ekind (Id) = E_Function and then Has_Controlling_Result (Id) then
18748 Remove_Primitive_Of (Etype (Id));
18749 end if;
18750 end if;
18751 end Remove_Overloaded_Entity;
18753 ---------------------
18754 -- Rep_To_Pos_Flag --
18755 ---------------------
18757 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
18758 begin
18759 return New_Occurrence_Of
18760 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
18761 end Rep_To_Pos_Flag;
18763 --------------------
18764 -- Require_Entity --
18765 --------------------
18767 procedure Require_Entity (N : Node_Id) is
18768 begin
18769 if Is_Entity_Name (N) and then No (Entity (N)) then
18770 if Total_Errors_Detected /= 0 then
18771 Set_Entity (N, Any_Id);
18772 else
18773 raise Program_Error;
18774 end if;
18775 end if;
18776 end Require_Entity;
18778 ------------------------------
18779 -- Requires_Transient_Scope --
18780 ------------------------------
18782 -- A transient scope is required when variable-sized temporaries are
18783 -- allocated on the secondary stack, or when finalization actions must be
18784 -- generated before the next instruction.
18786 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
18787 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
18788 -- ???We retain the old and new algorithms for Requires_Transient_Scope for
18789 -- the time being. New_Requires_Transient_Scope is used by default; the
18790 -- debug switch -gnatdQ can be used to do Old_Requires_Transient_Scope
18791 -- instead. The intent is to use this temporarily to measure before/after
18792 -- efficiency. Note: when this temporary code is removed, the documentation
18793 -- of dQ in debug.adb should be removed.
18795 procedure Results_Differ (Id : Entity_Id);
18796 -- ???Debugging code. Called when the Old_ and New_ results differ. Will be
18797 -- removed when New_Requires_Transient_Scope becomes
18798 -- Requires_Transient_Scope and Old_Requires_Transient_Scope is eliminated.
18800 procedure Results_Differ (Id : Entity_Id) is
18801 begin
18802 if False then -- False to disable; True for debugging
18803 Treepr.Print_Tree_Node (Id);
18805 if Old_Requires_Transient_Scope (Id) =
18806 New_Requires_Transient_Scope (Id)
18807 then
18808 raise Program_Error;
18809 end if;
18810 end if;
18811 end Results_Differ;
18813 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18814 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
18816 begin
18817 if Debug_Flag_QQ then
18818 return Old_Result;
18819 end if;
18821 declare
18822 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
18824 begin
18825 -- Assert that we're not putting things on the secondary stack if we
18826 -- didn't before; we are trying to AVOID secondary stack when
18827 -- possible.
18829 if not Old_Result then
18830 pragma Assert (not New_Result);
18831 null;
18832 end if;
18834 if New_Result /= Old_Result then
18835 Results_Differ (Id);
18836 end if;
18838 return New_Result;
18839 end;
18840 end Requires_Transient_Scope;
18842 ----------------------------------
18843 -- Old_Requires_Transient_Scope --
18844 ----------------------------------
18846 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18847 Typ : constant Entity_Id := Underlying_Type (Id);
18849 begin
18850 -- This is a private type which is not completed yet. This can only
18851 -- happen in a default expression (of a formal parameter or of a
18852 -- record component). Do not expand transient scope in this case.
18854 if No (Typ) then
18855 return False;
18857 -- Do not expand transient scope for non-existent procedure return
18859 elsif Typ = Standard_Void_Type then
18860 return False;
18862 -- Elementary types do not require a transient scope
18864 elsif Is_Elementary_Type (Typ) then
18865 return False;
18867 -- Generally, indefinite subtypes require a transient scope, since the
18868 -- back end cannot generate temporaries, since this is not a valid type
18869 -- for declaring an object. It might be possible to relax this in the
18870 -- future, e.g. by declaring the maximum possible space for the type.
18872 elsif not Is_Definite_Subtype (Typ) then
18873 return True;
18875 -- Functions returning tagged types may dispatch on result so their
18876 -- returned value is allocated on the secondary stack. Controlled
18877 -- type temporaries need finalization.
18879 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
18880 return True;
18882 -- Record type
18884 elsif Is_Record_Type (Typ) then
18885 declare
18886 Comp : Entity_Id;
18888 begin
18889 Comp := First_Entity (Typ);
18890 while Present (Comp) loop
18891 if Ekind (Comp) = E_Component then
18893 -- ???It's not clear we need a full recursive call to
18894 -- Old_Requires_Transient_Scope here. Note that the
18895 -- following can't happen.
18897 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
18898 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
18900 if Old_Requires_Transient_Scope (Etype (Comp)) then
18901 return True;
18902 end if;
18903 end if;
18905 Next_Entity (Comp);
18906 end loop;
18907 end;
18909 return False;
18911 -- String literal types never require transient scope
18913 elsif Ekind (Typ) = E_String_Literal_Subtype then
18914 return False;
18916 -- Array type. Note that we already know that this is a constrained
18917 -- array, since unconstrained arrays will fail the indefinite test.
18919 elsif Is_Array_Type (Typ) then
18921 -- If component type requires a transient scope, the array does too
18923 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
18924 return True;
18926 -- Otherwise, we only need a transient scope if the size depends on
18927 -- the value of one or more discriminants.
18929 else
18930 return Size_Depends_On_Discriminant (Typ);
18931 end if;
18933 -- All other cases do not require a transient scope
18935 else
18936 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
18937 return False;
18938 end if;
18939 end Old_Requires_Transient_Scope;
18941 ----------------------------------
18942 -- New_Requires_Transient_Scope --
18943 ----------------------------------
18945 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18947 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
18948 -- This is called for untagged records and protected types, with
18949 -- nondefaulted discriminants. Returns True if the size of function
18950 -- results is known at the call site, False otherwise. Returns False
18951 -- if there is a variant part that depends on the discriminants of
18952 -- this type, or if there is an array constrained by the discriminants
18953 -- of this type. ???Currently, this is overly conservative (the array
18954 -- could be nested inside some other record that is constrained by
18955 -- nondiscriminants). That is, the recursive calls are too conservative.
18957 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean;
18958 -- Returns True if Typ is a nonlimited record with defaulted
18959 -- discriminants whose max size makes it unsuitable for allocating on
18960 -- the primary stack.
18962 ------------------------------
18963 -- Caller_Known_Size_Record --
18964 ------------------------------
18966 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
18967 pragma Assert (Typ = Underlying_Type (Typ));
18969 begin
18970 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
18971 return False;
18972 end if;
18974 declare
18975 Comp : Entity_Id;
18977 begin
18978 Comp := First_Entity (Typ);
18979 while Present (Comp) loop
18981 -- Only look at E_Component entities. No need to look at
18982 -- E_Discriminant entities, and we must ignore internal
18983 -- subtypes generated for constrained components.
18985 if Ekind (Comp) = E_Component then
18986 declare
18987 Comp_Type : constant Entity_Id :=
18988 Underlying_Type (Etype (Comp));
18990 begin
18991 if Is_Record_Type (Comp_Type)
18992 or else
18993 Is_Protected_Type (Comp_Type)
18994 then
18995 if not Caller_Known_Size_Record (Comp_Type) then
18996 return False;
18997 end if;
18999 elsif Is_Array_Type (Comp_Type) then
19000 if Size_Depends_On_Discriminant (Comp_Type) then
19001 return False;
19002 end if;
19003 end if;
19004 end;
19005 end if;
19007 Next_Entity (Comp);
19008 end loop;
19009 end;
19011 return True;
19012 end Caller_Known_Size_Record;
19014 ------------------------------
19015 -- Large_Max_Size_Mutable --
19016 ------------------------------
19018 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean is
19019 pragma Assert (Typ = Underlying_Type (Typ));
19021 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean;
19022 -- Returns true if the discrete type T has a large range
19024 ----------------------------
19025 -- Is_Large_Discrete_Type --
19026 ----------------------------
19028 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean is
19029 Threshold : constant Int := 16;
19030 -- Arbitrary threshold above which we consider it "large". We want
19031 -- a fairly large threshold, because these large types really
19032 -- shouldn't have default discriminants in the first place, in
19033 -- most cases.
19035 begin
19036 return UI_To_Int (RM_Size (T)) > Threshold;
19037 end Is_Large_Discrete_Type;
19039 begin
19040 if Is_Record_Type (Typ)
19041 and then not Is_Limited_View (Typ)
19042 and then Has_Defaulted_Discriminants (Typ)
19043 then
19044 -- Loop through the components, looking for an array whose upper
19045 -- bound(s) depends on discriminants, where both the subtype of
19046 -- the discriminant and the index subtype are too large.
19048 declare
19049 Comp : Entity_Id;
19051 begin
19052 Comp := First_Entity (Typ);
19053 while Present (Comp) loop
19054 if Ekind (Comp) = E_Component then
19055 declare
19056 Comp_Type : constant Entity_Id :=
19057 Underlying_Type (Etype (Comp));
19058 Indx : Node_Id;
19059 Ityp : Entity_Id;
19060 Hi : Node_Id;
19062 begin
19063 if Is_Array_Type (Comp_Type) then
19064 Indx := First_Index (Comp_Type);
19066 while Present (Indx) loop
19067 Ityp := Etype (Indx);
19068 Hi := Type_High_Bound (Ityp);
19070 if Nkind (Hi) = N_Identifier
19071 and then Ekind (Entity (Hi)) = E_Discriminant
19072 and then Is_Large_Discrete_Type (Ityp)
19073 and then Is_Large_Discrete_Type
19074 (Etype (Entity (Hi)))
19075 then
19076 return True;
19077 end if;
19079 Next_Index (Indx);
19080 end loop;
19081 end if;
19082 end;
19083 end if;
19085 Next_Entity (Comp);
19086 end loop;
19087 end;
19088 end if;
19090 return False;
19091 end Large_Max_Size_Mutable;
19093 -- Local declarations
19095 Typ : constant Entity_Id := Underlying_Type (Id);
19097 -- Start of processing for New_Requires_Transient_Scope
19099 begin
19100 -- This is a private type which is not completed yet. This can only
19101 -- happen in a default expression (of a formal parameter or of a
19102 -- record component). Do not expand transient scope in this case.
19104 if No (Typ) then
19105 return False;
19107 -- Do not expand transient scope for non-existent procedure return or
19108 -- string literal types.
19110 elsif Typ = Standard_Void_Type
19111 or else Ekind (Typ) = E_String_Literal_Subtype
19112 then
19113 return False;
19115 -- If Typ is a generic formal incomplete type, then we want to look at
19116 -- the actual type.
19118 elsif Ekind (Typ) = E_Record_Subtype
19119 and then Present (Cloned_Subtype (Typ))
19120 then
19121 return New_Requires_Transient_Scope (Cloned_Subtype (Typ));
19123 -- Functions returning specific tagged types may dispatch on result, so
19124 -- their returned value is allocated on the secondary stack, even in the
19125 -- definite case. We must treat nondispatching functions the same way,
19126 -- because access-to-function types can point at both, so the calling
19127 -- conventions must be compatible. Is_Tagged_Type includes controlled
19128 -- types and class-wide types. Controlled type temporaries need
19129 -- finalization.
19131 -- ???It's not clear why we need to return noncontrolled types with
19132 -- controlled components on the secondary stack.
19134 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
19135 return True;
19137 -- Untagged definite subtypes are known size. This includes all
19138 -- elementary [sub]types. Tasks are known size even if they have
19139 -- discriminants. So we return False here, with one exception:
19140 -- For a type like:
19141 -- type T (Last : Natural := 0) is
19142 -- X : String (1 .. Last);
19143 -- end record;
19144 -- we return True. That's because for "P(F(...));", where F returns T,
19145 -- we don't know the size of the result at the call site, so if we
19146 -- allocated it on the primary stack, we would have to allocate the
19147 -- maximum size, which is way too big.
19149 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
19150 return Large_Max_Size_Mutable (Typ);
19152 -- Indefinite (discriminated) untagged record or protected type
19154 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
19155 return not Caller_Known_Size_Record (Typ);
19157 -- Unconstrained array
19159 else
19160 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
19161 return True;
19162 end if;
19163 end New_Requires_Transient_Scope;
19165 --------------------------
19166 -- Reset_Analyzed_Flags --
19167 --------------------------
19169 procedure Reset_Analyzed_Flags (N : Node_Id) is
19171 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
19172 -- Function used to reset Analyzed flags in tree. Note that we do
19173 -- not reset Analyzed flags in entities, since there is no need to
19174 -- reanalyze entities, and indeed, it is wrong to do so, since it
19175 -- can result in generating auxiliary stuff more than once.
19177 --------------------
19178 -- Clear_Analyzed --
19179 --------------------
19181 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
19182 begin
19183 if not Has_Extension (N) then
19184 Set_Analyzed (N, False);
19185 end if;
19187 return OK;
19188 end Clear_Analyzed;
19190 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
19192 -- Start of processing for Reset_Analyzed_Flags
19194 begin
19195 Reset_Analyzed (N);
19196 end Reset_Analyzed_Flags;
19198 ------------------------
19199 -- Restore_SPARK_Mode --
19200 ------------------------
19202 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
19203 begin
19204 SPARK_Mode := Mode;
19205 end Restore_SPARK_Mode;
19207 --------------------------------
19208 -- Returns_Unconstrained_Type --
19209 --------------------------------
19211 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
19212 begin
19213 return Ekind (Subp) = E_Function
19214 and then not Is_Scalar_Type (Etype (Subp))
19215 and then not Is_Access_Type (Etype (Subp))
19216 and then not Is_Constrained (Etype (Subp));
19217 end Returns_Unconstrained_Type;
19219 ----------------------------
19220 -- Root_Type_Of_Full_View --
19221 ----------------------------
19223 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
19224 Rtyp : constant Entity_Id := Root_Type (T);
19226 begin
19227 -- The root type of the full view may itself be a private type. Keep
19228 -- looking for the ultimate derivation parent.
19230 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
19231 return Root_Type_Of_Full_View (Full_View (Rtyp));
19232 else
19233 return Rtyp;
19234 end if;
19235 end Root_Type_Of_Full_View;
19237 ---------------------------
19238 -- Safe_To_Capture_Value --
19239 ---------------------------
19241 function Safe_To_Capture_Value
19242 (N : Node_Id;
19243 Ent : Entity_Id;
19244 Cond : Boolean := False) return Boolean
19246 begin
19247 -- The only entities for which we track constant values are variables
19248 -- which are not renamings, constants, out parameters, and in out
19249 -- parameters, so check if we have this case.
19251 -- Note: it may seem odd to track constant values for constants, but in
19252 -- fact this routine is used for other purposes than simply capturing
19253 -- the value. In particular, the setting of Known[_Non]_Null.
19255 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
19256 or else
19257 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
19258 then
19259 null;
19261 -- For conditionals, we also allow loop parameters and all formals,
19262 -- including in parameters.
19264 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
19265 null;
19267 -- For all other cases, not just unsafe, but impossible to capture
19268 -- Current_Value, since the above are the only entities which have
19269 -- Current_Value fields.
19271 else
19272 return False;
19273 end if;
19275 -- Skip if volatile or aliased, since funny things might be going on in
19276 -- these cases which we cannot necessarily track. Also skip any variable
19277 -- for which an address clause is given, or whose address is taken. Also
19278 -- never capture value of library level variables (an attempt to do so
19279 -- can occur in the case of package elaboration code).
19281 if Treat_As_Volatile (Ent)
19282 or else Is_Aliased (Ent)
19283 or else Present (Address_Clause (Ent))
19284 or else Address_Taken (Ent)
19285 or else (Is_Library_Level_Entity (Ent)
19286 and then Ekind (Ent) = E_Variable)
19287 then
19288 return False;
19289 end if;
19291 -- OK, all above conditions are met. We also require that the scope of
19292 -- the reference be the same as the scope of the entity, not counting
19293 -- packages and blocks and loops.
19295 declare
19296 E_Scope : constant Entity_Id := Scope (Ent);
19297 R_Scope : Entity_Id;
19299 begin
19300 R_Scope := Current_Scope;
19301 while R_Scope /= Standard_Standard loop
19302 exit when R_Scope = E_Scope;
19304 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
19305 return False;
19306 else
19307 R_Scope := Scope (R_Scope);
19308 end if;
19309 end loop;
19310 end;
19312 -- We also require that the reference does not appear in a context
19313 -- where it is not sure to be executed (i.e. a conditional context
19314 -- or an exception handler). We skip this if Cond is True, since the
19315 -- capturing of values from conditional tests handles this ok.
19317 if Cond then
19318 return True;
19319 end if;
19321 declare
19322 Desc : Node_Id;
19323 P : Node_Id;
19325 begin
19326 Desc := N;
19328 -- Seems dubious that case expressions are not handled here ???
19330 P := Parent (N);
19331 while Present (P) loop
19332 if Nkind (P) = N_If_Statement
19333 or else Nkind (P) = N_Case_Statement
19334 or else (Nkind (P) in N_Short_Circuit
19335 and then Desc = Right_Opnd (P))
19336 or else (Nkind (P) = N_If_Expression
19337 and then Desc /= First (Expressions (P)))
19338 or else Nkind (P) = N_Exception_Handler
19339 or else Nkind (P) = N_Selective_Accept
19340 or else Nkind (P) = N_Conditional_Entry_Call
19341 or else Nkind (P) = N_Timed_Entry_Call
19342 or else Nkind (P) = N_Asynchronous_Select
19343 then
19344 return False;
19346 else
19347 Desc := P;
19348 P := Parent (P);
19350 -- A special Ada 2012 case: the original node may be part
19351 -- of the else_actions of a conditional expression, in which
19352 -- case it might not have been expanded yet, and appears in
19353 -- a non-syntactic list of actions. In that case it is clearly
19354 -- not safe to save a value.
19356 if No (P)
19357 and then Is_List_Member (Desc)
19358 and then No (Parent (List_Containing (Desc)))
19359 then
19360 return False;
19361 end if;
19362 end if;
19363 end loop;
19364 end;
19366 -- OK, looks safe to set value
19368 return True;
19369 end Safe_To_Capture_Value;
19371 ---------------
19372 -- Same_Name --
19373 ---------------
19375 function Same_Name (N1, N2 : Node_Id) return Boolean is
19376 K1 : constant Node_Kind := Nkind (N1);
19377 K2 : constant Node_Kind := Nkind (N2);
19379 begin
19380 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
19381 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
19382 then
19383 return Chars (N1) = Chars (N2);
19385 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
19386 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
19387 then
19388 return Same_Name (Selector_Name (N1), Selector_Name (N2))
19389 and then Same_Name (Prefix (N1), Prefix (N2));
19391 else
19392 return False;
19393 end if;
19394 end Same_Name;
19396 -----------------
19397 -- Same_Object --
19398 -----------------
19400 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
19401 N1 : constant Node_Id := Original_Node (Node1);
19402 N2 : constant Node_Id := Original_Node (Node2);
19403 -- We do the tests on original nodes, since we are most interested
19404 -- in the original source, not any expansion that got in the way.
19406 K1 : constant Node_Kind := Nkind (N1);
19407 K2 : constant Node_Kind := Nkind (N2);
19409 begin
19410 -- First case, both are entities with same entity
19412 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
19413 declare
19414 EN1 : constant Entity_Id := Entity (N1);
19415 EN2 : constant Entity_Id := Entity (N2);
19416 begin
19417 if Present (EN1) and then Present (EN2)
19418 and then (Ekind_In (EN1, E_Variable, E_Constant)
19419 or else Is_Formal (EN1))
19420 and then EN1 = EN2
19421 then
19422 return True;
19423 end if;
19424 end;
19425 end if;
19427 -- Second case, selected component with same selector, same record
19429 if K1 = N_Selected_Component
19430 and then K2 = N_Selected_Component
19431 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
19432 then
19433 return Same_Object (Prefix (N1), Prefix (N2));
19435 -- Third case, indexed component with same subscripts, same array
19437 elsif K1 = N_Indexed_Component
19438 and then K2 = N_Indexed_Component
19439 and then Same_Object (Prefix (N1), Prefix (N2))
19440 then
19441 declare
19442 E1, E2 : Node_Id;
19443 begin
19444 E1 := First (Expressions (N1));
19445 E2 := First (Expressions (N2));
19446 while Present (E1) loop
19447 if not Same_Value (E1, E2) then
19448 return False;
19449 else
19450 Next (E1);
19451 Next (E2);
19452 end if;
19453 end loop;
19455 return True;
19456 end;
19458 -- Fourth case, slice of same array with same bounds
19460 elsif K1 = N_Slice
19461 and then K2 = N_Slice
19462 and then Nkind (Discrete_Range (N1)) = N_Range
19463 and then Nkind (Discrete_Range (N2)) = N_Range
19464 and then Same_Value (Low_Bound (Discrete_Range (N1)),
19465 Low_Bound (Discrete_Range (N2)))
19466 and then Same_Value (High_Bound (Discrete_Range (N1)),
19467 High_Bound (Discrete_Range (N2)))
19468 then
19469 return Same_Name (Prefix (N1), Prefix (N2));
19471 -- All other cases, not clearly the same object
19473 else
19474 return False;
19475 end if;
19476 end Same_Object;
19478 ---------------
19479 -- Same_Type --
19480 ---------------
19482 function Same_Type (T1, T2 : Entity_Id) return Boolean is
19483 begin
19484 if T1 = T2 then
19485 return True;
19487 elsif not Is_Constrained (T1)
19488 and then not Is_Constrained (T2)
19489 and then Base_Type (T1) = Base_Type (T2)
19490 then
19491 return True;
19493 -- For now don't bother with case of identical constraints, to be
19494 -- fiddled with later on perhaps (this is only used for optimization
19495 -- purposes, so it is not critical to do a best possible job)
19497 else
19498 return False;
19499 end if;
19500 end Same_Type;
19502 ----------------
19503 -- Same_Value --
19504 ----------------
19506 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
19507 begin
19508 if Compile_Time_Known_Value (Node1)
19509 and then Compile_Time_Known_Value (Node2)
19510 and then Expr_Value (Node1) = Expr_Value (Node2)
19511 then
19512 return True;
19513 elsif Same_Object (Node1, Node2) then
19514 return True;
19515 else
19516 return False;
19517 end if;
19518 end Same_Value;
19520 -----------------------------
19521 -- Save_SPARK_Mode_And_Set --
19522 -----------------------------
19524 procedure Save_SPARK_Mode_And_Set
19525 (Context : Entity_Id;
19526 Mode : out SPARK_Mode_Type)
19528 begin
19529 -- Save the current mode in effect
19531 Mode := SPARK_Mode;
19533 -- Do not consider illegal or partially decorated constructs
19535 if Ekind (Context) = E_Void or else Error_Posted (Context) then
19536 null;
19538 elsif Present (SPARK_Pragma (Context)) then
19539 SPARK_Mode := Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Context));
19540 end if;
19541 end Save_SPARK_Mode_And_Set;
19543 -------------------------
19544 -- Scalar_Part_Present --
19545 -------------------------
19547 function Scalar_Part_Present (T : Entity_Id) return Boolean is
19548 C : Entity_Id;
19550 begin
19551 if Is_Scalar_Type (T) then
19552 return True;
19554 elsif Is_Array_Type (T) then
19555 return Scalar_Part_Present (Component_Type (T));
19557 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
19558 C := First_Component_Or_Discriminant (T);
19559 while Present (C) loop
19560 if Scalar_Part_Present (Etype (C)) then
19561 return True;
19562 else
19563 Next_Component_Or_Discriminant (C);
19564 end if;
19565 end loop;
19566 end if;
19568 return False;
19569 end Scalar_Part_Present;
19571 ------------------------
19572 -- Scope_Is_Transient --
19573 ------------------------
19575 function Scope_Is_Transient return Boolean is
19576 begin
19577 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
19578 end Scope_Is_Transient;
19580 ------------------
19581 -- Scope_Within --
19582 ------------------
19584 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
19585 Scop : Entity_Id;
19587 begin
19588 Scop := Scope1;
19589 while Scop /= Standard_Standard loop
19590 Scop := Scope (Scop);
19592 if Scop = Scope2 then
19593 return True;
19594 end if;
19595 end loop;
19597 return False;
19598 end Scope_Within;
19600 --------------------------
19601 -- Scope_Within_Or_Same --
19602 --------------------------
19604 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
19605 Scop : Entity_Id;
19607 begin
19608 Scop := Scope1;
19609 while Scop /= Standard_Standard loop
19610 if Scop = Scope2 then
19611 return True;
19612 else
19613 Scop := Scope (Scop);
19614 end if;
19615 end loop;
19617 return False;
19618 end Scope_Within_Or_Same;
19620 --------------------
19621 -- Set_Convention --
19622 --------------------
19624 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
19625 begin
19626 Basic_Set_Convention (E, Val);
19628 if Is_Type (E)
19629 and then Is_Access_Subprogram_Type (Base_Type (E))
19630 and then Has_Foreign_Convention (E)
19631 then
19633 -- A pragma Convention in an instance may apply to the subtype
19634 -- created for a formal, in which case we have already verified
19635 -- that conventions of actual and formal match and there is nothing
19636 -- to flag on the subtype.
19638 if In_Instance then
19639 null;
19640 else
19641 Set_Can_Use_Internal_Rep (E, False);
19642 end if;
19643 end if;
19645 -- If E is an object or component, and the type of E is an anonymous
19646 -- access type with no convention set, then also set the convention of
19647 -- the anonymous access type. We do not do this for anonymous protected
19648 -- types, since protected types always have the default convention.
19650 if Present (Etype (E))
19651 and then (Is_Object (E)
19652 or else Ekind (E) = E_Component
19654 -- Allow E_Void (happens for pragma Convention appearing
19655 -- in the middle of a record applying to a component)
19657 or else Ekind (E) = E_Void)
19658 then
19659 declare
19660 Typ : constant Entity_Id := Etype (E);
19662 begin
19663 if Ekind_In (Typ, E_Anonymous_Access_Type,
19664 E_Anonymous_Access_Subprogram_Type)
19665 and then not Has_Convention_Pragma (Typ)
19666 then
19667 Basic_Set_Convention (Typ, Val);
19668 Set_Has_Convention_Pragma (Typ);
19670 -- And for the access subprogram type, deal similarly with the
19671 -- designated E_Subprogram_Type if it is also internal (which
19672 -- it always is?)
19674 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
19675 declare
19676 Dtype : constant Entity_Id := Designated_Type (Typ);
19677 begin
19678 if Ekind (Dtype) = E_Subprogram_Type
19679 and then Is_Itype (Dtype)
19680 and then not Has_Convention_Pragma (Dtype)
19681 then
19682 Basic_Set_Convention (Dtype, Val);
19683 Set_Has_Convention_Pragma (Dtype);
19684 end if;
19685 end;
19686 end if;
19687 end if;
19688 end;
19689 end if;
19690 end Set_Convention;
19692 ------------------------
19693 -- Set_Current_Entity --
19694 ------------------------
19696 -- The given entity is to be set as the currently visible definition of its
19697 -- associated name (i.e. the Node_Id associated with its name). All we have
19698 -- to do is to get the name from the identifier, and then set the
19699 -- associated Node_Id to point to the given entity.
19701 procedure Set_Current_Entity (E : Entity_Id) is
19702 begin
19703 Set_Name_Entity_Id (Chars (E), E);
19704 end Set_Current_Entity;
19706 ---------------------------
19707 -- Set_Debug_Info_Needed --
19708 ---------------------------
19710 procedure Set_Debug_Info_Needed (T : Entity_Id) is
19712 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
19713 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
19714 -- Used to set debug info in a related node if not set already
19716 --------------------------------------
19717 -- Set_Debug_Info_Needed_If_Not_Set --
19718 --------------------------------------
19720 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
19721 begin
19722 if Present (E) and then not Needs_Debug_Info (E) then
19723 Set_Debug_Info_Needed (E);
19725 -- For a private type, indicate that the full view also needs
19726 -- debug information.
19728 if Is_Type (E)
19729 and then Is_Private_Type (E)
19730 and then Present (Full_View (E))
19731 then
19732 Set_Debug_Info_Needed (Full_View (E));
19733 end if;
19734 end if;
19735 end Set_Debug_Info_Needed_If_Not_Set;
19737 -- Start of processing for Set_Debug_Info_Needed
19739 begin
19740 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
19741 -- indicates that Debug_Info_Needed is never required for the entity.
19742 -- Nothing to do if entity comes from a predefined file. Library files
19743 -- are compiled without debug information, but inlined bodies of these
19744 -- routines may appear in user code, and debug information on them ends
19745 -- up complicating debugging the user code.
19747 if No (T)
19748 or else Debug_Info_Off (T)
19749 then
19750 return;
19752 elsif In_Inlined_Body
19753 and then Is_Predefined_File_Name
19754 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
19755 then
19756 Set_Needs_Debug_Info (T, False);
19757 end if;
19759 -- Set flag in entity itself. Note that we will go through the following
19760 -- circuitry even if the flag is already set on T. That's intentional,
19761 -- it makes sure that the flag will be set in subsidiary entities.
19763 Set_Needs_Debug_Info (T);
19765 -- Set flag on subsidiary entities if not set already
19767 if Is_Object (T) then
19768 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
19770 elsif Is_Type (T) then
19771 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
19773 if Is_Record_Type (T) then
19774 declare
19775 Ent : Entity_Id := First_Entity (T);
19776 begin
19777 while Present (Ent) loop
19778 Set_Debug_Info_Needed_If_Not_Set (Ent);
19779 Next_Entity (Ent);
19780 end loop;
19781 end;
19783 -- For a class wide subtype, we also need debug information
19784 -- for the equivalent type.
19786 if Ekind (T) = E_Class_Wide_Subtype then
19787 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
19788 end if;
19790 elsif Is_Array_Type (T) then
19791 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
19793 declare
19794 Indx : Node_Id := First_Index (T);
19795 begin
19796 while Present (Indx) loop
19797 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
19798 Indx := Next_Index (Indx);
19799 end loop;
19800 end;
19802 -- For a packed array type, we also need debug information for
19803 -- the type used to represent the packed array. Conversely, we
19804 -- also need it for the former if we need it for the latter.
19806 if Is_Packed (T) then
19807 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
19808 end if;
19810 if Is_Packed_Array_Impl_Type (T) then
19811 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
19812 end if;
19814 elsif Is_Access_Type (T) then
19815 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
19817 elsif Is_Private_Type (T) then
19818 declare
19819 FV : constant Entity_Id := Full_View (T);
19821 begin
19822 Set_Debug_Info_Needed_If_Not_Set (FV);
19824 -- If the full view is itself a derived private type, we need
19825 -- debug information on its underlying type.
19827 if Present (FV)
19828 and then Is_Private_Type (FV)
19829 and then Present (Underlying_Full_View (FV))
19830 then
19831 Set_Needs_Debug_Info (Underlying_Full_View (FV));
19832 end if;
19833 end;
19835 elsif Is_Protected_Type (T) then
19836 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
19838 elsif Is_Scalar_Type (T) then
19840 -- If the subrange bounds are materialized by dedicated constant
19841 -- objects, also include them in the debug info to make sure the
19842 -- debugger can properly use them.
19844 if Present (Scalar_Range (T))
19845 and then Nkind (Scalar_Range (T)) = N_Range
19846 then
19847 declare
19848 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
19849 High_Bnd : constant Node_Id := Type_High_Bound (T);
19851 begin
19852 if Is_Entity_Name (Low_Bnd) then
19853 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
19854 end if;
19856 if Is_Entity_Name (High_Bnd) then
19857 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
19858 end if;
19859 end;
19860 end if;
19861 end if;
19862 end if;
19863 end Set_Debug_Info_Needed;
19865 ----------------------------
19866 -- Set_Entity_With_Checks --
19867 ----------------------------
19869 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
19870 Val_Actual : Entity_Id;
19871 Nod : Node_Id;
19872 Post_Node : Node_Id;
19874 begin
19875 -- Unconditionally set the entity
19877 Set_Entity (N, Val);
19879 -- The node to post on is the selector in the case of an expanded name,
19880 -- and otherwise the node itself.
19882 if Nkind (N) = N_Expanded_Name then
19883 Post_Node := Selector_Name (N);
19884 else
19885 Post_Node := N;
19886 end if;
19888 -- Check for violation of No_Fixed_IO
19890 if Restriction_Check_Required (No_Fixed_IO)
19891 and then
19892 ((RTU_Loaded (Ada_Text_IO)
19893 and then (Is_RTE (Val, RE_Decimal_IO)
19894 or else
19895 Is_RTE (Val, RE_Fixed_IO)))
19897 or else
19898 (RTU_Loaded (Ada_Wide_Text_IO)
19899 and then (Is_RTE (Val, RO_WT_Decimal_IO)
19900 or else
19901 Is_RTE (Val, RO_WT_Fixed_IO)))
19903 or else
19904 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
19905 and then (Is_RTE (Val, RO_WW_Decimal_IO)
19906 or else
19907 Is_RTE (Val, RO_WW_Fixed_IO))))
19909 -- A special extra check, don't complain about a reference from within
19910 -- the Ada.Interrupts package itself!
19912 and then not In_Same_Extended_Unit (N, Val)
19913 then
19914 Check_Restriction (No_Fixed_IO, Post_Node);
19915 end if;
19917 -- Remaining checks are only done on source nodes. Note that we test
19918 -- for violation of No_Fixed_IO even on non-source nodes, because the
19919 -- cases for checking violations of this restriction are instantiations
19920 -- where the reference in the instance has Comes_From_Source False.
19922 if not Comes_From_Source (N) then
19923 return;
19924 end if;
19926 -- Check for violation of No_Abort_Statements, which is triggered by
19927 -- call to Ada.Task_Identification.Abort_Task.
19929 if Restriction_Check_Required (No_Abort_Statements)
19930 and then (Is_RTE (Val, RE_Abort_Task))
19932 -- A special extra check, don't complain about a reference from within
19933 -- the Ada.Task_Identification package itself!
19935 and then not In_Same_Extended_Unit (N, Val)
19936 then
19937 Check_Restriction (No_Abort_Statements, Post_Node);
19938 end if;
19940 if Val = Standard_Long_Long_Integer then
19941 Check_Restriction (No_Long_Long_Integers, Post_Node);
19942 end if;
19944 -- Check for violation of No_Dynamic_Attachment
19946 if Restriction_Check_Required (No_Dynamic_Attachment)
19947 and then RTU_Loaded (Ada_Interrupts)
19948 and then (Is_RTE (Val, RE_Is_Reserved) or else
19949 Is_RTE (Val, RE_Is_Attached) or else
19950 Is_RTE (Val, RE_Current_Handler) or else
19951 Is_RTE (Val, RE_Attach_Handler) or else
19952 Is_RTE (Val, RE_Exchange_Handler) or else
19953 Is_RTE (Val, RE_Detach_Handler) or else
19954 Is_RTE (Val, RE_Reference))
19956 -- A special extra check, don't complain about a reference from within
19957 -- the Ada.Interrupts package itself!
19959 and then not In_Same_Extended_Unit (N, Val)
19960 then
19961 Check_Restriction (No_Dynamic_Attachment, Post_Node);
19962 end if;
19964 -- Check for No_Implementation_Identifiers
19966 if Restriction_Check_Required (No_Implementation_Identifiers) then
19968 -- We have an implementation defined entity if it is marked as
19969 -- implementation defined, or is defined in a package marked as
19970 -- implementation defined. However, library packages themselves
19971 -- are excluded (we don't want to flag Interfaces itself, just
19972 -- the entities within it).
19974 if (Is_Implementation_Defined (Val)
19975 or else
19976 (Present (Scope (Val))
19977 and then Is_Implementation_Defined (Scope (Val))))
19978 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
19979 and then Is_Library_Level_Entity (Val))
19980 then
19981 Check_Restriction (No_Implementation_Identifiers, Post_Node);
19982 end if;
19983 end if;
19985 -- Do the style check
19987 if Style_Check
19988 and then not Suppress_Style_Checks (Val)
19989 and then not In_Instance
19990 then
19991 if Nkind (N) = N_Identifier then
19992 Nod := N;
19993 elsif Nkind (N) = N_Expanded_Name then
19994 Nod := Selector_Name (N);
19995 else
19996 return;
19997 end if;
19999 -- A special situation arises for derived operations, where we want
20000 -- to do the check against the parent (since the Sloc of the derived
20001 -- operation points to the derived type declaration itself).
20003 Val_Actual := Val;
20004 while not Comes_From_Source (Val_Actual)
20005 and then Nkind (Val_Actual) in N_Entity
20006 and then (Ekind (Val_Actual) = E_Enumeration_Literal
20007 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
20008 and then Present (Alias (Val_Actual))
20009 loop
20010 Val_Actual := Alias (Val_Actual);
20011 end loop;
20013 -- Renaming declarations for generic actuals do not come from source,
20014 -- and have a different name from that of the entity they rename, so
20015 -- there is no style check to perform here.
20017 if Chars (Nod) = Chars (Val_Actual) then
20018 Style.Check_Identifier (Nod, Val_Actual);
20019 end if;
20020 end if;
20022 Set_Entity (N, Val);
20023 end Set_Entity_With_Checks;
20025 ------------------------
20026 -- Set_Name_Entity_Id --
20027 ------------------------
20029 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
20030 begin
20031 Set_Name_Table_Int (Id, Int (Val));
20032 end Set_Name_Entity_Id;
20034 ---------------------
20035 -- Set_Next_Actual --
20036 ---------------------
20038 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
20039 begin
20040 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
20041 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
20042 end if;
20043 end Set_Next_Actual;
20045 ----------------------------------
20046 -- Set_Optimize_Alignment_Flags --
20047 ----------------------------------
20049 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
20050 begin
20051 if Optimize_Alignment = 'S' then
20052 Set_Optimize_Alignment_Space (E);
20053 elsif Optimize_Alignment = 'T' then
20054 Set_Optimize_Alignment_Time (E);
20055 end if;
20056 end Set_Optimize_Alignment_Flags;
20058 -----------------------
20059 -- Set_Public_Status --
20060 -----------------------
20062 procedure Set_Public_Status (Id : Entity_Id) is
20063 S : constant Entity_Id := Current_Scope;
20065 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
20066 -- Determines if E is defined within handled statement sequence or
20067 -- an if statement, returns True if so, False otherwise.
20069 ----------------------
20070 -- Within_HSS_Or_If --
20071 ----------------------
20073 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
20074 N : Node_Id;
20075 begin
20076 N := Declaration_Node (E);
20077 loop
20078 N := Parent (N);
20080 if No (N) then
20081 return False;
20083 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
20084 N_If_Statement)
20085 then
20086 return True;
20087 end if;
20088 end loop;
20089 end Within_HSS_Or_If;
20091 -- Start of processing for Set_Public_Status
20093 begin
20094 -- Everything in the scope of Standard is public
20096 if S = Standard_Standard then
20097 Set_Is_Public (Id);
20099 -- Entity is definitely not public if enclosing scope is not public
20101 elsif not Is_Public (S) then
20102 return;
20104 -- An object or function declaration that occurs in a handled sequence
20105 -- of statements or within an if statement is the declaration for a
20106 -- temporary object or local subprogram generated by the expander. It
20107 -- never needs to be made public and furthermore, making it public can
20108 -- cause back end problems.
20110 elsif Nkind_In (Parent (Id), N_Object_Declaration,
20111 N_Function_Specification)
20112 and then Within_HSS_Or_If (Id)
20113 then
20114 return;
20116 -- Entities in public packages or records are public
20118 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
20119 Set_Is_Public (Id);
20121 -- The bounds of an entry family declaration can generate object
20122 -- declarations that are visible to the back-end, e.g. in the
20123 -- the declaration of a composite type that contains tasks.
20125 elsif Is_Concurrent_Type (S)
20126 and then not Has_Completion (S)
20127 and then Nkind (Parent (Id)) = N_Object_Declaration
20128 then
20129 Set_Is_Public (Id);
20130 end if;
20131 end Set_Public_Status;
20133 -----------------------------
20134 -- Set_Referenced_Modified --
20135 -----------------------------
20137 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
20138 Pref : Node_Id;
20140 begin
20141 -- Deal with indexed or selected component where prefix is modified
20143 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
20144 Pref := Prefix (N);
20146 -- If prefix is access type, then it is the designated object that is
20147 -- being modified, which means we have no entity to set the flag on.
20149 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
20150 return;
20152 -- Otherwise chase the prefix
20154 else
20155 Set_Referenced_Modified (Pref, Out_Param);
20156 end if;
20158 -- Otherwise see if we have an entity name (only other case to process)
20160 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
20161 Set_Referenced_As_LHS (Entity (N), not Out_Param);
20162 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
20163 end if;
20164 end Set_Referenced_Modified;
20166 ----------------------------
20167 -- Set_Scope_Is_Transient --
20168 ----------------------------
20170 procedure Set_Scope_Is_Transient (V : Boolean := True) is
20171 begin
20172 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
20173 end Set_Scope_Is_Transient;
20175 -------------------
20176 -- Set_Size_Info --
20177 -------------------
20179 procedure Set_Size_Info (T1, T2 : Entity_Id) is
20180 begin
20181 -- We copy Esize, but not RM_Size, since in general RM_Size is
20182 -- subtype specific and does not get inherited by all subtypes.
20184 Set_Esize (T1, Esize (T2));
20185 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
20187 if Is_Discrete_Or_Fixed_Point_Type (T1)
20188 and then
20189 Is_Discrete_Or_Fixed_Point_Type (T2)
20190 then
20191 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
20192 end if;
20194 Set_Alignment (T1, Alignment (T2));
20195 end Set_Size_Info;
20197 --------------------
20198 -- Static_Boolean --
20199 --------------------
20201 function Static_Boolean (N : Node_Id) return Uint is
20202 begin
20203 Analyze_And_Resolve (N, Standard_Boolean);
20205 if N = Error
20206 or else Error_Posted (N)
20207 or else Etype (N) = Any_Type
20208 then
20209 return No_Uint;
20210 end if;
20212 if Is_OK_Static_Expression (N) then
20213 if not Raises_Constraint_Error (N) then
20214 return Expr_Value (N);
20215 else
20216 return No_Uint;
20217 end if;
20219 elsif Etype (N) = Any_Type then
20220 return No_Uint;
20222 else
20223 Flag_Non_Static_Expr
20224 ("static boolean expression required here", N);
20225 return No_Uint;
20226 end if;
20227 end Static_Boolean;
20229 --------------------
20230 -- Static_Integer --
20231 --------------------
20233 function Static_Integer (N : Node_Id) return Uint is
20234 begin
20235 Analyze_And_Resolve (N, Any_Integer);
20237 if N = Error
20238 or else Error_Posted (N)
20239 or else Etype (N) = Any_Type
20240 then
20241 return No_Uint;
20242 end if;
20244 if Is_OK_Static_Expression (N) then
20245 if not Raises_Constraint_Error (N) then
20246 return Expr_Value (N);
20247 else
20248 return No_Uint;
20249 end if;
20251 elsif Etype (N) = Any_Type then
20252 return No_Uint;
20254 else
20255 Flag_Non_Static_Expr
20256 ("static integer expression required here", N);
20257 return No_Uint;
20258 end if;
20259 end Static_Integer;
20261 --------------------------
20262 -- Statically_Different --
20263 --------------------------
20265 function Statically_Different (E1, E2 : Node_Id) return Boolean is
20266 R1 : constant Node_Id := Get_Referenced_Object (E1);
20267 R2 : constant Node_Id := Get_Referenced_Object (E2);
20268 begin
20269 return Is_Entity_Name (R1)
20270 and then Is_Entity_Name (R2)
20271 and then Entity (R1) /= Entity (R2)
20272 and then not Is_Formal (Entity (R1))
20273 and then not Is_Formal (Entity (R2));
20274 end Statically_Different;
20276 --------------------------------------
20277 -- Subject_To_Loop_Entry_Attributes --
20278 --------------------------------------
20280 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
20281 Stmt : Node_Id;
20283 begin
20284 Stmt := N;
20286 -- The expansion mechanism transform a loop subject to at least one
20287 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
20288 -- the conditional part.
20290 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
20291 and then Nkind (Original_Node (N)) = N_Loop_Statement
20292 then
20293 Stmt := Original_Node (N);
20294 end if;
20296 return
20297 Nkind (Stmt) = N_Loop_Statement
20298 and then Present (Identifier (Stmt))
20299 and then Present (Entity (Identifier (Stmt)))
20300 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
20301 end Subject_To_Loop_Entry_Attributes;
20303 -----------------------------
20304 -- Subprogram_Access_Level --
20305 -----------------------------
20307 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
20308 begin
20309 if Present (Alias (Subp)) then
20310 return Subprogram_Access_Level (Alias (Subp));
20311 else
20312 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
20313 end if;
20314 end Subprogram_Access_Level;
20316 -------------------------------
20317 -- Support_Atomic_Primitives --
20318 -------------------------------
20320 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
20321 Size : Int;
20323 begin
20324 -- Verify the alignment of Typ is known
20326 if not Known_Alignment (Typ) then
20327 return False;
20328 end if;
20330 if Known_Static_Esize (Typ) then
20331 Size := UI_To_Int (Esize (Typ));
20333 -- If the Esize (Object_Size) is unknown at compile time, look at the
20334 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
20336 elsif Known_Static_RM_Size (Typ) then
20337 Size := UI_To_Int (RM_Size (Typ));
20339 -- Otherwise, the size is considered to be unknown.
20341 else
20342 return False;
20343 end if;
20345 -- Check that the size of the component is 8, 16, 32, or 64 bits and
20346 -- that Typ is properly aligned.
20348 case Size is
20349 when 8 | 16 | 32 | 64 =>
20350 return Size = UI_To_Int (Alignment (Typ)) * 8;
20351 when others =>
20352 return False;
20353 end case;
20354 end Support_Atomic_Primitives;
20356 -----------------
20357 -- Trace_Scope --
20358 -----------------
20360 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
20361 begin
20362 if Debug_Flag_W then
20363 for J in 0 .. Scope_Stack.Last loop
20364 Write_Str (" ");
20365 end loop;
20367 Write_Str (Msg);
20368 Write_Name (Chars (E));
20369 Write_Str (" from ");
20370 Write_Location (Sloc (N));
20371 Write_Eol;
20372 end if;
20373 end Trace_Scope;
20375 -----------------------
20376 -- Transfer_Entities --
20377 -----------------------
20379 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
20380 procedure Set_Public_Status_Of (Id : Entity_Id);
20381 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
20382 -- Set_Public_Status. If successfull and Id denotes a record type, set
20383 -- the Is_Public attribute of its fields.
20385 --------------------------
20386 -- Set_Public_Status_Of --
20387 --------------------------
20389 procedure Set_Public_Status_Of (Id : Entity_Id) is
20390 Field : Entity_Id;
20392 begin
20393 if not Is_Public (Id) then
20394 Set_Public_Status (Id);
20396 -- When the input entity is a public record type, ensure that all
20397 -- its internal fields are also exposed to the linker. The fields
20398 -- of a class-wide type are never made public.
20400 if Is_Public (Id)
20401 and then Is_Record_Type (Id)
20402 and then not Is_Class_Wide_Type (Id)
20403 then
20404 Field := First_Entity (Id);
20405 while Present (Field) loop
20406 Set_Is_Public (Field);
20407 Next_Entity (Field);
20408 end loop;
20409 end if;
20410 end if;
20411 end Set_Public_Status_Of;
20413 -- Local variables
20415 Full_Id : Entity_Id;
20416 Id : Entity_Id;
20418 -- Start of processing for Transfer_Entities
20420 begin
20421 Id := First_Entity (From);
20423 if Present (Id) then
20425 -- Merge the entity chain of the source scope with that of the
20426 -- destination scope.
20428 if Present (Last_Entity (To)) then
20429 Set_Next_Entity (Last_Entity (To), Id);
20430 else
20431 Set_First_Entity (To, Id);
20432 end if;
20434 Set_Last_Entity (To, Last_Entity (From));
20436 -- Inspect the entities of the source scope and update their Scope
20437 -- attribute.
20439 while Present (Id) loop
20440 Set_Scope (Id, To);
20441 Set_Public_Status_Of (Id);
20443 -- Handle an internally generated full view for a private type
20445 if Is_Private_Type (Id)
20446 and then Present (Full_View (Id))
20447 and then Is_Itype (Full_View (Id))
20448 then
20449 Full_Id := Full_View (Id);
20451 Set_Scope (Full_Id, To);
20452 Set_Public_Status_Of (Full_Id);
20453 end if;
20455 Next_Entity (Id);
20456 end loop;
20458 Set_First_Entity (From, Empty);
20459 Set_Last_Entity (From, Empty);
20460 end if;
20461 end Transfer_Entities;
20463 -----------------------
20464 -- Type_Access_Level --
20465 -----------------------
20467 function Type_Access_Level (Typ : Entity_Id) return Uint is
20468 Btyp : Entity_Id;
20470 begin
20471 Btyp := Base_Type (Typ);
20473 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
20474 -- simply use the level where the type is declared. This is true for
20475 -- stand-alone object declarations, and for anonymous access types
20476 -- associated with components the level is the same as that of the
20477 -- enclosing composite type. However, special treatment is needed for
20478 -- the cases of access parameters, return objects of an anonymous access
20479 -- type, and, in Ada 95, access discriminants of limited types.
20481 if Is_Access_Type (Btyp) then
20482 if Ekind (Btyp) = E_Anonymous_Access_Type then
20484 -- If the type is a nonlocal anonymous access type (such as for
20485 -- an access parameter) we treat it as being declared at the
20486 -- library level to ensure that names such as X.all'access don't
20487 -- fail static accessibility checks.
20489 if not Is_Local_Anonymous_Access (Typ) then
20490 return Scope_Depth (Standard_Standard);
20492 -- If this is a return object, the accessibility level is that of
20493 -- the result subtype of the enclosing function. The test here is
20494 -- little complicated, because we have to account for extended
20495 -- return statements that have been rewritten as blocks, in which
20496 -- case we have to find and the Is_Return_Object attribute of the
20497 -- itype's associated object. It would be nice to find a way to
20498 -- simplify this test, but it doesn't seem worthwhile to add a new
20499 -- flag just for purposes of this test. ???
20501 elsif Ekind (Scope (Btyp)) = E_Return_Statement
20502 or else
20503 (Is_Itype (Btyp)
20504 and then Nkind (Associated_Node_For_Itype (Btyp)) =
20505 N_Object_Declaration
20506 and then Is_Return_Object
20507 (Defining_Identifier
20508 (Associated_Node_For_Itype (Btyp))))
20509 then
20510 declare
20511 Scop : Entity_Id;
20513 begin
20514 Scop := Scope (Scope (Btyp));
20515 while Present (Scop) loop
20516 exit when Ekind (Scop) = E_Function;
20517 Scop := Scope (Scop);
20518 end loop;
20520 -- Treat the return object's type as having the level of the
20521 -- function's result subtype (as per RM05-6.5(5.3/2)).
20523 return Type_Access_Level (Etype (Scop));
20524 end;
20525 end if;
20526 end if;
20528 Btyp := Root_Type (Btyp);
20530 -- The accessibility level of anonymous access types associated with
20531 -- discriminants is that of the current instance of the type, and
20532 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
20534 -- AI-402: access discriminants have accessibility based on the
20535 -- object rather than the type in Ada 2005, so the above paragraph
20536 -- doesn't apply.
20538 -- ??? Needs completion with rules from AI-416
20540 if Ada_Version <= Ada_95
20541 and then Ekind (Typ) = E_Anonymous_Access_Type
20542 and then Present (Associated_Node_For_Itype (Typ))
20543 and then Nkind (Associated_Node_For_Itype (Typ)) =
20544 N_Discriminant_Specification
20545 then
20546 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
20547 end if;
20548 end if;
20550 -- Return library level for a generic formal type. This is done because
20551 -- RM(10.3.2) says that "The statically deeper relationship does not
20552 -- apply to ... a descendant of a generic formal type". Rather than
20553 -- checking at each point where a static accessibility check is
20554 -- performed to see if we are dealing with a formal type, this rule is
20555 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
20556 -- return extreme values for a formal type; Deepest_Type_Access_Level
20557 -- returns Int'Last. By calling the appropriate function from among the
20558 -- two, we ensure that the static accessibility check will pass if we
20559 -- happen to run into a formal type. More specifically, we should call
20560 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
20561 -- call occurs as part of a static accessibility check and the error
20562 -- case is the case where the type's level is too shallow (as opposed
20563 -- to too deep).
20565 if Is_Generic_Type (Root_Type (Btyp)) then
20566 return Scope_Depth (Standard_Standard);
20567 end if;
20569 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
20570 end Type_Access_Level;
20572 ------------------------------------
20573 -- Type_Without_Stream_Operation --
20574 ------------------------------------
20576 function Type_Without_Stream_Operation
20577 (T : Entity_Id;
20578 Op : TSS_Name_Type := TSS_Null) return Entity_Id
20580 BT : constant Entity_Id := Base_Type (T);
20581 Op_Missing : Boolean;
20583 begin
20584 if not Restriction_Active (No_Default_Stream_Attributes) then
20585 return Empty;
20586 end if;
20588 if Is_Elementary_Type (T) then
20589 if Op = TSS_Null then
20590 Op_Missing :=
20591 No (TSS (BT, TSS_Stream_Read))
20592 or else No (TSS (BT, TSS_Stream_Write));
20594 else
20595 Op_Missing := No (TSS (BT, Op));
20596 end if;
20598 if Op_Missing then
20599 return T;
20600 else
20601 return Empty;
20602 end if;
20604 elsif Is_Array_Type (T) then
20605 return Type_Without_Stream_Operation (Component_Type (T), Op);
20607 elsif Is_Record_Type (T) then
20608 declare
20609 Comp : Entity_Id;
20610 C_Typ : Entity_Id;
20612 begin
20613 Comp := First_Component (T);
20614 while Present (Comp) loop
20615 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
20617 if Present (C_Typ) then
20618 return C_Typ;
20619 end if;
20621 Next_Component (Comp);
20622 end loop;
20624 return Empty;
20625 end;
20627 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
20628 return Type_Without_Stream_Operation (Full_View (T), Op);
20629 else
20630 return Empty;
20631 end if;
20632 end Type_Without_Stream_Operation;
20634 ----------------------------
20635 -- Unique_Defining_Entity --
20636 ----------------------------
20638 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
20639 begin
20640 return Unique_Entity (Defining_Entity (N));
20641 end Unique_Defining_Entity;
20643 -------------------
20644 -- Unique_Entity --
20645 -------------------
20647 function Unique_Entity (E : Entity_Id) return Entity_Id is
20648 U : Entity_Id := E;
20649 P : Node_Id;
20651 begin
20652 case Ekind (E) is
20653 when E_Constant =>
20654 if Present (Full_View (E)) then
20655 U := Full_View (E);
20656 end if;
20658 when Entry_Kind =>
20659 if Nkind (Parent (E)) = N_Entry_Body then
20660 declare
20661 Prot_Item : Entity_Id;
20662 begin
20663 -- Traverse the entity list of the protected type and locate
20664 -- an entry declaration which matches the entry body.
20666 Prot_Item := First_Entity (Scope (E));
20667 while Present (Prot_Item) loop
20668 if Ekind (Prot_Item) = E_Entry
20669 and then Corresponding_Body (Parent (Prot_Item)) = E
20670 then
20671 U := Prot_Item;
20672 exit;
20673 end if;
20675 Next_Entity (Prot_Item);
20676 end loop;
20677 end;
20678 end if;
20680 when Formal_Kind =>
20681 if Present (Spec_Entity (E)) then
20682 U := Spec_Entity (E);
20683 end if;
20685 when E_Package_Body =>
20686 P := Parent (E);
20688 if Nkind (P) = N_Defining_Program_Unit_Name then
20689 P := Parent (P);
20690 end if;
20692 if Nkind (P) = N_Package_Body
20693 and then Present (Corresponding_Spec (P))
20694 then
20695 U := Corresponding_Spec (P);
20697 elsif Nkind (P) = N_Package_Body_Stub
20698 and then Present (Corresponding_Spec_Of_Stub (P))
20699 then
20700 U := Corresponding_Spec_Of_Stub (P);
20701 end if;
20703 when E_Protected_Body =>
20704 P := Parent (E);
20706 if Nkind (P) = N_Protected_Body
20707 and then Present (Corresponding_Spec (P))
20708 then
20709 U := Corresponding_Spec (P);
20711 elsif Nkind (P) = N_Protected_Body_Stub
20712 and then Present (Corresponding_Spec_Of_Stub (P))
20713 then
20714 U := Corresponding_Spec_Of_Stub (P);
20715 end if;
20717 when E_Subprogram_Body =>
20718 P := Parent (E);
20720 if Nkind (P) = N_Defining_Program_Unit_Name then
20721 P := Parent (P);
20722 end if;
20724 P := Parent (P);
20726 if Nkind (P) = N_Subprogram_Body
20727 and then Present (Corresponding_Spec (P))
20728 then
20729 U := Corresponding_Spec (P);
20731 elsif Nkind (P) = N_Subprogram_Body_Stub
20732 and then Present (Corresponding_Spec_Of_Stub (P))
20733 then
20734 U := Corresponding_Spec_Of_Stub (P);
20736 elsif Nkind (P) = N_Subprogram_Renaming_Declaration then
20737 U := Corresponding_Spec (P);
20738 end if;
20740 when E_Task_Body =>
20741 P := Parent (E);
20743 if Nkind (P) = N_Task_Body
20744 and then Present (Corresponding_Spec (P))
20745 then
20746 U := Corresponding_Spec (P);
20748 elsif Nkind (P) = N_Task_Body_Stub
20749 and then Present (Corresponding_Spec_Of_Stub (P))
20750 then
20751 U := Corresponding_Spec_Of_Stub (P);
20752 end if;
20754 when Type_Kind =>
20755 if Present (Full_View (E)) then
20756 U := Full_View (E);
20757 end if;
20759 when others =>
20760 null;
20761 end case;
20763 return U;
20764 end Unique_Entity;
20766 -----------------
20767 -- Unique_Name --
20768 -----------------
20770 function Unique_Name (E : Entity_Id) return String is
20772 -- Names of E_Subprogram_Body or E_Package_Body entities are not
20773 -- reliable, as they may not include the overloading suffix. Instead,
20774 -- when looking for the name of E or one of its enclosing scope, we get
20775 -- the name of the corresponding Unique_Entity.
20777 function Get_Scoped_Name (E : Entity_Id) return String;
20778 -- Return the name of E prefixed by all the names of the scopes to which
20779 -- E belongs, except for Standard.
20781 ---------------------
20782 -- Get_Scoped_Name --
20783 ---------------------
20785 function Get_Scoped_Name (E : Entity_Id) return String is
20786 Name : constant String := Get_Name_String (Chars (E));
20787 begin
20788 if Has_Fully_Qualified_Name (E)
20789 or else Scope (E) = Standard_Standard
20790 then
20791 return Name;
20792 else
20793 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
20794 end if;
20795 end Get_Scoped_Name;
20797 -- Start of processing for Unique_Name
20799 begin
20800 if E = Standard_Standard then
20801 return Get_Name_String (Name_Standard);
20803 elsif Scope (E) = Standard_Standard
20804 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
20805 then
20806 return Get_Name_String (Name_Standard) & "__" &
20807 Get_Name_String (Chars (E));
20809 elsif Ekind (E) = E_Enumeration_Literal then
20810 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
20812 else
20813 return Get_Scoped_Name (Unique_Entity (E));
20814 end if;
20815 end Unique_Name;
20817 ---------------------
20818 -- Unit_Is_Visible --
20819 ---------------------
20821 function Unit_Is_Visible (U : Entity_Id) return Boolean is
20822 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
20823 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
20825 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
20826 -- For a child unit, check whether unit appears in a with_clause
20827 -- of a parent.
20829 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
20830 -- Scan the context clause of one compilation unit looking for a
20831 -- with_clause for the unit in question.
20833 ----------------------------
20834 -- Unit_In_Parent_Context --
20835 ----------------------------
20837 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
20838 begin
20839 if Unit_In_Context (Par_Unit) then
20840 return True;
20842 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
20843 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
20845 else
20846 return False;
20847 end if;
20848 end Unit_In_Parent_Context;
20850 ---------------------
20851 -- Unit_In_Context --
20852 ---------------------
20854 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
20855 Clause : Node_Id;
20857 begin
20858 Clause := First (Context_Items (Comp_Unit));
20859 while Present (Clause) loop
20860 if Nkind (Clause) = N_With_Clause then
20861 if Library_Unit (Clause) = U then
20862 return True;
20864 -- The with_clause may denote a renaming of the unit we are
20865 -- looking for, eg. Text_IO which renames Ada.Text_IO.
20867 elsif
20868 Renamed_Entity (Entity (Name (Clause))) =
20869 Defining_Entity (Unit (U))
20870 then
20871 return True;
20872 end if;
20873 end if;
20875 Next (Clause);
20876 end loop;
20878 return False;
20879 end Unit_In_Context;
20881 -- Start of processing for Unit_Is_Visible
20883 begin
20884 -- The currrent unit is directly visible
20886 if Curr = U then
20887 return True;
20889 elsif Unit_In_Context (Curr) then
20890 return True;
20892 -- If the current unit is a body, check the context of the spec
20894 elsif Nkind (Unit (Curr)) = N_Package_Body
20895 or else
20896 (Nkind (Unit (Curr)) = N_Subprogram_Body
20897 and then not Acts_As_Spec (Unit (Curr)))
20898 then
20899 if Unit_In_Context (Library_Unit (Curr)) then
20900 return True;
20901 end if;
20902 end if;
20904 -- If the spec is a child unit, examine the parents
20906 if Is_Child_Unit (Curr_Entity) then
20907 if Nkind (Unit (Curr)) in N_Unit_Body then
20908 return
20909 Unit_In_Parent_Context
20910 (Parent_Spec (Unit (Library_Unit (Curr))));
20911 else
20912 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
20913 end if;
20915 else
20916 return False;
20917 end if;
20918 end Unit_Is_Visible;
20920 ------------------------------
20921 -- Universal_Interpretation --
20922 ------------------------------
20924 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
20925 Index : Interp_Index;
20926 It : Interp;
20928 begin
20929 -- The argument may be a formal parameter of an operator or subprogram
20930 -- with multiple interpretations, or else an expression for an actual.
20932 if Nkind (Opnd) = N_Defining_Identifier
20933 or else not Is_Overloaded (Opnd)
20934 then
20935 if Etype (Opnd) = Universal_Integer
20936 or else Etype (Opnd) = Universal_Real
20937 then
20938 return Etype (Opnd);
20939 else
20940 return Empty;
20941 end if;
20943 else
20944 Get_First_Interp (Opnd, Index, It);
20945 while Present (It.Typ) loop
20946 if It.Typ = Universal_Integer
20947 or else It.Typ = Universal_Real
20948 then
20949 return It.Typ;
20950 end if;
20952 Get_Next_Interp (Index, It);
20953 end loop;
20955 return Empty;
20956 end if;
20957 end Universal_Interpretation;
20959 ---------------
20960 -- Unqualify --
20961 ---------------
20963 function Unqualify (Expr : Node_Id) return Node_Id is
20964 begin
20965 -- Recurse to handle unlikely case of multiple levels of qualification
20967 if Nkind (Expr) = N_Qualified_Expression then
20968 return Unqualify (Expression (Expr));
20970 -- Normal case, not a qualified expression
20972 else
20973 return Expr;
20974 end if;
20975 end Unqualify;
20977 -----------------------
20978 -- Visible_Ancestors --
20979 -----------------------
20981 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
20982 List_1 : Elist_Id;
20983 List_2 : Elist_Id;
20984 Elmt : Elmt_Id;
20986 begin
20987 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
20989 -- Collect all the parents and progenitors of Typ. If the full-view of
20990 -- private parents and progenitors is available then it is used to
20991 -- generate the list of visible ancestors; otherwise their partial
20992 -- view is added to the resulting list.
20994 Collect_Parents
20995 (T => Typ,
20996 List => List_1,
20997 Use_Full_View => True);
20999 Collect_Interfaces
21000 (T => Typ,
21001 Ifaces_List => List_2,
21002 Exclude_Parents => True,
21003 Use_Full_View => True);
21005 -- Join the two lists. Avoid duplications because an interface may
21006 -- simultaneously be parent and progenitor of a type.
21008 Elmt := First_Elmt (List_2);
21009 while Present (Elmt) loop
21010 Append_Unique_Elmt (Node (Elmt), List_1);
21011 Next_Elmt (Elmt);
21012 end loop;
21014 return List_1;
21015 end Visible_Ancestors;
21017 ----------------------
21018 -- Within_Init_Proc --
21019 ----------------------
21021 function Within_Init_Proc return Boolean is
21022 S : Entity_Id;
21024 begin
21025 S := Current_Scope;
21026 while not Is_Overloadable (S) loop
21027 if S = Standard_Standard then
21028 return False;
21029 else
21030 S := Scope (S);
21031 end if;
21032 end loop;
21034 return Is_Init_Proc (S);
21035 end Within_Init_Proc;
21037 ------------------
21038 -- Within_Scope --
21039 ------------------
21041 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
21042 begin
21043 return Scope_Within_Or_Same (Scope (E), S);
21044 end Within_Scope;
21046 ----------------
21047 -- Wrong_Type --
21048 ----------------
21050 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
21051 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
21052 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
21054 Matching_Field : Entity_Id;
21055 -- Entity to give a more precise suggestion on how to write a one-
21056 -- element positional aggregate.
21058 function Has_One_Matching_Field return Boolean;
21059 -- Determines if Expec_Type is a record type with a single component or
21060 -- discriminant whose type matches the found type or is one dimensional
21061 -- array whose component type matches the found type. In the case of
21062 -- one discriminant, we ignore the variant parts. That's not accurate,
21063 -- but good enough for the warning.
21065 ----------------------------
21066 -- Has_One_Matching_Field --
21067 ----------------------------
21069 function Has_One_Matching_Field return Boolean is
21070 E : Entity_Id;
21072 begin
21073 Matching_Field := Empty;
21075 if Is_Array_Type (Expec_Type)
21076 and then Number_Dimensions (Expec_Type) = 1
21077 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
21078 then
21079 -- Use type name if available. This excludes multidimensional
21080 -- arrays and anonymous arrays.
21082 if Comes_From_Source (Expec_Type) then
21083 Matching_Field := Expec_Type;
21085 -- For an assignment, use name of target
21087 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
21088 and then Is_Entity_Name (Name (Parent (Expr)))
21089 then
21090 Matching_Field := Entity (Name (Parent (Expr)));
21091 end if;
21093 return True;
21095 elsif not Is_Record_Type (Expec_Type) then
21096 return False;
21098 else
21099 E := First_Entity (Expec_Type);
21100 loop
21101 if No (E) then
21102 return False;
21104 elsif not Ekind_In (E, E_Discriminant, E_Component)
21105 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
21106 then
21107 Next_Entity (E);
21109 else
21110 exit;
21111 end if;
21112 end loop;
21114 if not Covers (Etype (E), Found_Type) then
21115 return False;
21117 elsif Present (Next_Entity (E))
21118 and then (Ekind (E) = E_Component
21119 or else Ekind (Next_Entity (E)) = E_Discriminant)
21120 then
21121 return False;
21123 else
21124 Matching_Field := E;
21125 return True;
21126 end if;
21127 end if;
21128 end Has_One_Matching_Field;
21130 -- Start of processing for Wrong_Type
21132 begin
21133 -- Don't output message if either type is Any_Type, or if a message
21134 -- has already been posted for this node. We need to do the latter
21135 -- check explicitly (it is ordinarily done in Errout), because we
21136 -- are using ! to force the output of the error messages.
21138 if Expec_Type = Any_Type
21139 or else Found_Type = Any_Type
21140 or else Error_Posted (Expr)
21141 then
21142 return;
21144 -- If one of the types is a Taft-Amendment type and the other it its
21145 -- completion, it must be an illegal use of a TAT in the spec, for
21146 -- which an error was already emitted. Avoid cascaded errors.
21148 elsif Is_Incomplete_Type (Expec_Type)
21149 and then Has_Completion_In_Body (Expec_Type)
21150 and then Full_View (Expec_Type) = Etype (Expr)
21151 then
21152 return;
21154 elsif Is_Incomplete_Type (Etype (Expr))
21155 and then Has_Completion_In_Body (Etype (Expr))
21156 and then Full_View (Etype (Expr)) = Expec_Type
21157 then
21158 return;
21160 -- In an instance, there is an ongoing problem with completion of
21161 -- type derived from private types. Their structure is what Gigi
21162 -- expects, but the Etype is the parent type rather than the
21163 -- derived private type itself. Do not flag error in this case. The
21164 -- private completion is an entity without a parent, like an Itype.
21165 -- Similarly, full and partial views may be incorrect in the instance.
21166 -- There is no simple way to insure that it is consistent ???
21168 -- A similar view discrepancy can happen in an inlined body, for the
21169 -- same reason: inserted body may be outside of the original package
21170 -- and only partial views are visible at the point of insertion.
21172 elsif In_Instance or else In_Inlined_Body then
21173 if Etype (Etype (Expr)) = Etype (Expected_Type)
21174 and then
21175 (Has_Private_Declaration (Expected_Type)
21176 or else Has_Private_Declaration (Etype (Expr)))
21177 and then No (Parent (Expected_Type))
21178 then
21179 return;
21181 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
21182 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
21183 then
21184 return;
21186 elsif Is_Private_Type (Expected_Type)
21187 and then Present (Full_View (Expected_Type))
21188 and then Covers (Full_View (Expected_Type), Etype (Expr))
21189 then
21190 return;
21192 -- Conversely, type of expression may be the private one
21194 elsif Is_Private_Type (Base_Type (Etype (Expr)))
21195 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
21196 then
21197 return;
21198 end if;
21199 end if;
21201 -- An interesting special check. If the expression is parenthesized
21202 -- and its type corresponds to the type of the sole component of the
21203 -- expected record type, or to the component type of the expected one
21204 -- dimensional array type, then assume we have a bad aggregate attempt.
21206 if Nkind (Expr) in N_Subexpr
21207 and then Paren_Count (Expr) /= 0
21208 and then Has_One_Matching_Field
21209 then
21210 Error_Msg_N ("positional aggregate cannot have one component", Expr);
21212 if Present (Matching_Field) then
21213 if Is_Array_Type (Expec_Type) then
21214 Error_Msg_NE
21215 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
21216 else
21217 Error_Msg_NE
21218 ("\write instead `& ='> ...`", Expr, Matching_Field);
21219 end if;
21220 end if;
21222 -- Another special check, if we are looking for a pool-specific access
21223 -- type and we found an E_Access_Attribute_Type, then we have the case
21224 -- of an Access attribute being used in a context which needs a pool-
21225 -- specific type, which is never allowed. The one extra check we make
21226 -- is that the expected designated type covers the Found_Type.
21228 elsif Is_Access_Type (Expec_Type)
21229 and then Ekind (Found_Type) = E_Access_Attribute_Type
21230 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
21231 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
21232 and then Covers
21233 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
21234 then
21235 Error_Msg_N -- CODEFIX
21236 ("result must be general access type!", Expr);
21237 Error_Msg_NE -- CODEFIX
21238 ("add ALL to }!", Expr, Expec_Type);
21240 -- Another special check, if the expected type is an integer type,
21241 -- but the expression is of type System.Address, and the parent is
21242 -- an addition or subtraction operation whose left operand is the
21243 -- expression in question and whose right operand is of an integral
21244 -- type, then this is an attempt at address arithmetic, so give
21245 -- appropriate message.
21247 elsif Is_Integer_Type (Expec_Type)
21248 and then Is_RTE (Found_Type, RE_Address)
21249 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
21250 and then Expr = Left_Opnd (Parent (Expr))
21251 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
21252 then
21253 Error_Msg_N
21254 ("address arithmetic not predefined in package System",
21255 Parent (Expr));
21256 Error_Msg_N
21257 ("\possible missing with/use of System.Storage_Elements",
21258 Parent (Expr));
21259 return;
21261 -- If the expected type is an anonymous access type, as for access
21262 -- parameters and discriminants, the error is on the designated types.
21264 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
21265 if Comes_From_Source (Expec_Type) then
21266 Error_Msg_NE ("expected}!", Expr, Expec_Type);
21267 else
21268 Error_Msg_NE
21269 ("expected an access type with designated}",
21270 Expr, Designated_Type (Expec_Type));
21271 end if;
21273 if Is_Access_Type (Found_Type)
21274 and then not Comes_From_Source (Found_Type)
21275 then
21276 Error_Msg_NE
21277 ("\\found an access type with designated}!",
21278 Expr, Designated_Type (Found_Type));
21279 else
21280 if From_Limited_With (Found_Type) then
21281 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
21282 Error_Msg_Qual_Level := 99;
21283 Error_Msg_NE -- CODEFIX
21284 ("\\missing `WITH &;", Expr, Scope (Found_Type));
21285 Error_Msg_Qual_Level := 0;
21286 else
21287 Error_Msg_NE ("found}!", Expr, Found_Type);
21288 end if;
21289 end if;
21291 -- Normal case of one type found, some other type expected
21293 else
21294 -- If the names of the two types are the same, see if some number
21295 -- of levels of qualification will help. Don't try more than three
21296 -- levels, and if we get to standard, it's no use (and probably
21297 -- represents an error in the compiler) Also do not bother with
21298 -- internal scope names.
21300 declare
21301 Expec_Scope : Entity_Id;
21302 Found_Scope : Entity_Id;
21304 begin
21305 Expec_Scope := Expec_Type;
21306 Found_Scope := Found_Type;
21308 for Levels in Nat range 0 .. 3 loop
21309 if Chars (Expec_Scope) /= Chars (Found_Scope) then
21310 Error_Msg_Qual_Level := Levels;
21311 exit;
21312 end if;
21314 Expec_Scope := Scope (Expec_Scope);
21315 Found_Scope := Scope (Found_Scope);
21317 exit when Expec_Scope = Standard_Standard
21318 or else Found_Scope = Standard_Standard
21319 or else not Comes_From_Source (Expec_Scope)
21320 or else not Comes_From_Source (Found_Scope);
21321 end loop;
21322 end;
21324 if Is_Record_Type (Expec_Type)
21325 and then Present (Corresponding_Remote_Type (Expec_Type))
21326 then
21327 Error_Msg_NE ("expected}!", Expr,
21328 Corresponding_Remote_Type (Expec_Type));
21329 else
21330 Error_Msg_NE ("expected}!", Expr, Expec_Type);
21331 end if;
21333 if Is_Entity_Name (Expr)
21334 and then Is_Package_Or_Generic_Package (Entity (Expr))
21335 then
21336 Error_Msg_N ("\\found package name!", Expr);
21338 elsif Is_Entity_Name (Expr)
21339 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
21340 then
21341 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
21342 Error_Msg_N
21343 ("found procedure name, possibly missing Access attribute!",
21344 Expr);
21345 else
21346 Error_Msg_N
21347 ("\\found procedure name instead of function!", Expr);
21348 end if;
21350 elsif Nkind (Expr) = N_Function_Call
21351 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
21352 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
21353 and then No (Parameter_Associations (Expr))
21354 then
21355 Error_Msg_N
21356 ("found function name, possibly missing Access attribute!",
21357 Expr);
21359 -- Catch common error: a prefix or infix operator which is not
21360 -- directly visible because the type isn't.
21362 elsif Nkind (Expr) in N_Op
21363 and then Is_Overloaded (Expr)
21364 and then not Is_Immediately_Visible (Expec_Type)
21365 and then not Is_Potentially_Use_Visible (Expec_Type)
21366 and then not In_Use (Expec_Type)
21367 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
21368 then
21369 Error_Msg_N
21370 ("operator of the type is not directly visible!", Expr);
21372 elsif Ekind (Found_Type) = E_Void
21373 and then Present (Parent (Found_Type))
21374 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
21375 then
21376 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
21378 else
21379 Error_Msg_NE ("\\found}!", Expr, Found_Type);
21380 end if;
21382 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
21383 -- of the same modular type, and (M1 and M2) = 0 was intended.
21385 if Expec_Type = Standard_Boolean
21386 and then Is_Modular_Integer_Type (Found_Type)
21387 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
21388 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
21389 then
21390 declare
21391 Op : constant Node_Id := Right_Opnd (Parent (Expr));
21392 L : constant Node_Id := Left_Opnd (Op);
21393 R : constant Node_Id := Right_Opnd (Op);
21395 begin
21396 -- The case for the message is when the left operand of the
21397 -- comparison is the same modular type, or when it is an
21398 -- integer literal (or other universal integer expression),
21399 -- which would have been typed as the modular type if the
21400 -- parens had been there.
21402 if (Etype (L) = Found_Type
21403 or else
21404 Etype (L) = Universal_Integer)
21405 and then Is_Integer_Type (Etype (R))
21406 then
21407 Error_Msg_N
21408 ("\\possible missing parens for modular operation", Expr);
21409 end if;
21410 end;
21411 end if;
21413 -- Reset error message qualification indication
21415 Error_Msg_Qual_Level := 0;
21416 end if;
21417 end Wrong_Type;
21419 --------------------------------
21420 -- Yields_Synchronized_Object --
21421 --------------------------------
21423 function Yields_Synchronized_Object (Typ : Entity_Id) return Boolean is
21424 Has_Sync_Comp : Boolean := False;
21425 Id : Entity_Id;
21427 begin
21428 -- An array type yields a synchronized object if its component type
21429 -- yields a synchronized object.
21431 if Is_Array_Type (Typ) then
21432 return Yields_Synchronized_Object (Component_Type (Typ));
21434 -- A descendant of type Ada.Synchronous_Task_Control.Suspension_Object
21435 -- yields a synchronized object by default.
21437 elsif Is_Descendant_Of_Suspension_Object (Typ) then
21438 return True;
21440 -- A protected type yields a synchronized object by default
21442 elsif Is_Protected_Type (Typ) then
21443 return True;
21445 -- A record type or type extension yields a synchronized object when its
21446 -- discriminants (if any) lack default values and all components are of
21447 -- a type that yelds a synchronized object.
21449 elsif Is_Record_Type (Typ) then
21451 -- Inspect all entities defined in the scope of the type, looking for
21452 -- components of a type that does not yeld a synchronized object or
21453 -- for discriminants with default values.
21455 Id := First_Entity (Typ);
21456 while Present (Id) loop
21457 if Comes_From_Source (Id) then
21458 if Ekind (Id) = E_Component then
21459 if Yields_Synchronized_Object (Etype (Id)) then
21460 Has_Sync_Comp := True;
21462 -- The component does not yield a synchronized object
21464 else
21465 return False;
21466 end if;
21468 elsif Ekind (Id) = E_Discriminant
21469 and then Present (Expression (Parent (Id)))
21470 then
21471 return False;
21472 end if;
21473 end if;
21475 Next_Entity (Id);
21476 end loop;
21478 -- Ensure that the parent type of a type extension yields a
21479 -- synchronized object.
21481 if Etype (Typ) /= Typ
21482 and then not Yields_Synchronized_Object (Etype (Typ))
21483 then
21484 return False;
21485 end if;
21487 -- If we get here, then all discriminants lack default values and all
21488 -- components are of a type that yields a synchronized object.
21490 return Has_Sync_Comp;
21492 -- A synchronized interface type yields a synchronized object by default
21494 elsif Is_Synchronized_Interface (Typ) then
21495 return True;
21497 -- A task type yelds a synchronized object by default
21499 elsif Is_Task_Type (Typ) then
21500 return True;
21502 -- Otherwise the type does not yield a synchronized object
21504 else
21505 return False;
21506 end if;
21507 end Yields_Synchronized_Object;
21509 ---------------------------
21510 -- Yields_Universal_Type --
21511 ---------------------------
21513 function Yields_Universal_Type (N : Node_Id) return Boolean is
21514 begin
21515 -- Integer and real literals are of a universal type
21517 if Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
21518 return True;
21520 -- The values of certain attributes are of a universal type
21522 elsif Nkind (N) = N_Attribute_Reference then
21523 return
21524 Universal_Type_Attribute (Get_Attribute_Id (Attribute_Name (N)));
21526 -- ??? There are possibly other cases to consider
21528 else
21529 return False;
21530 end if;
21531 end Yields_Universal_Type;
21533 end Sem_Util;